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.IfMacros.push_back("MYIF"); 529 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 530 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 531 FormatStyle::SIS_WithoutElse; 532 verifyFormat("if (a)\n" 533 " // comment\n" 534 " f();", 535 AllowsMergedIf); 536 verifyFormat("{\n" 537 " if (a)\n" 538 " label:\n" 539 " f();\n" 540 "}", 541 AllowsMergedIf); 542 verifyFormat("#define A \\\n" 543 " if (a) \\\n" 544 " label: \\\n" 545 " f()", 546 AllowsMergedIf); 547 verifyFormat("if (a)\n" 548 " ;", 549 AllowsMergedIf); 550 verifyFormat("if (a)\n" 551 " if (b) return;", 552 AllowsMergedIf); 553 554 verifyFormat("if (a) // Can't merge this\n" 555 " f();\n", 556 AllowsMergedIf); 557 verifyFormat("if (a) /* still don't merge */\n" 558 " f();", 559 AllowsMergedIf); 560 verifyFormat("if (a) { // Never merge this\n" 561 " f();\n" 562 "}", 563 AllowsMergedIf); 564 verifyFormat("if (a) { /* Never merge this */\n" 565 " f();\n" 566 "}", 567 AllowsMergedIf); 568 verifyFormat("MYIF (a)\n" 569 " // comment\n" 570 " f();", 571 AllowsMergedIf); 572 verifyFormat("{\n" 573 " MYIF (a)\n" 574 " label:\n" 575 " f();\n" 576 "}", 577 AllowsMergedIf); 578 verifyFormat("#define A \\\n" 579 " MYIF (a) \\\n" 580 " label: \\\n" 581 " f()", 582 AllowsMergedIf); 583 verifyFormat("MYIF (a)\n" 584 " ;", 585 AllowsMergedIf); 586 verifyFormat("MYIF (a)\n" 587 " MYIF (b) return;", 588 AllowsMergedIf); 589 590 verifyFormat("MYIF (a) // Can't merge this\n" 591 " f();\n", 592 AllowsMergedIf); 593 verifyFormat("MYIF (a) /* still don't merge */\n" 594 " f();", 595 AllowsMergedIf); 596 verifyFormat("MYIF (a) { // Never merge this\n" 597 " f();\n" 598 "}", 599 AllowsMergedIf); 600 verifyFormat("MYIF (a) { /* Never merge this */\n" 601 " f();\n" 602 "}", 603 AllowsMergedIf); 604 605 AllowsMergedIf.ColumnLimit = 14; 606 // Where line-lengths matter, a 2-letter synonym that maintains line length. 607 // Not IF to avoid any confusion that IF is somehow special. 608 AllowsMergedIf.IfMacros.push_back("FI"); 609 verifyFormat("if (a) return;", AllowsMergedIf); 610 verifyFormat("if (aaaaaaaaa)\n" 611 " return;", 612 AllowsMergedIf); 613 verifyFormat("FI (a) return;", AllowsMergedIf); 614 verifyFormat("FI (aaaaaaaaa)\n" 615 " return;", 616 AllowsMergedIf); 617 618 AllowsMergedIf.ColumnLimit = 13; 619 verifyFormat("if (a)\n return;", AllowsMergedIf); 620 verifyFormat("FI (a)\n return;", AllowsMergedIf); 621 622 FormatStyle AllowsMergedIfElse = getLLVMStyle(); 623 AllowsMergedIfElse.IfMacros.push_back("MYIF"); 624 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine = 625 FormatStyle::SIS_AllIfsAndElse; 626 verifyFormat("if (a)\n" 627 " // comment\n" 628 " f();\n" 629 "else\n" 630 " // comment\n" 631 " f();", 632 AllowsMergedIfElse); 633 verifyFormat("{\n" 634 " if (a)\n" 635 " label:\n" 636 " f();\n" 637 " else\n" 638 " label:\n" 639 " f();\n" 640 "}", 641 AllowsMergedIfElse); 642 verifyFormat("if (a)\n" 643 " ;\n" 644 "else\n" 645 " ;", 646 AllowsMergedIfElse); 647 verifyFormat("if (a) {\n" 648 "} else {\n" 649 "}", 650 AllowsMergedIfElse); 651 verifyFormat("if (a) return;\n" 652 "else if (b) return;\n" 653 "else return;", 654 AllowsMergedIfElse); 655 verifyFormat("if (a) {\n" 656 "} else return;", 657 AllowsMergedIfElse); 658 verifyFormat("if (a) {\n" 659 "} else if (b) return;\n" 660 "else return;", 661 AllowsMergedIfElse); 662 verifyFormat("if (a) return;\n" 663 "else if (b) {\n" 664 "} else return;", 665 AllowsMergedIfElse); 666 verifyFormat("if (a)\n" 667 " if (b) return;\n" 668 " else return;", 669 AllowsMergedIfElse); 670 verifyFormat("if constexpr (a)\n" 671 " if constexpr (b) return;\n" 672 " else if constexpr (c) return;\n" 673 " else return;", 674 AllowsMergedIfElse); 675 verifyFormat("MYIF (a)\n" 676 " // comment\n" 677 " f();\n" 678 "else\n" 679 " // comment\n" 680 " f();", 681 AllowsMergedIfElse); 682 verifyFormat("{\n" 683 " MYIF (a)\n" 684 " label:\n" 685 " f();\n" 686 " else\n" 687 " label:\n" 688 " f();\n" 689 "}", 690 AllowsMergedIfElse); 691 verifyFormat("MYIF (a)\n" 692 " ;\n" 693 "else\n" 694 " ;", 695 AllowsMergedIfElse); 696 verifyFormat("MYIF (a) {\n" 697 "} else {\n" 698 "}", 699 AllowsMergedIfElse); 700 verifyFormat("MYIF (a) return;\n" 701 "else MYIF (b) return;\n" 702 "else return;", 703 AllowsMergedIfElse); 704 verifyFormat("MYIF (a) {\n" 705 "} else return;", 706 AllowsMergedIfElse); 707 verifyFormat("MYIF (a) {\n" 708 "} else MYIF (b) return;\n" 709 "else return;", 710 AllowsMergedIfElse); 711 verifyFormat("MYIF (a) return;\n" 712 "else MYIF (b) {\n" 713 "} else return;", 714 AllowsMergedIfElse); 715 verifyFormat("MYIF (a)\n" 716 " MYIF (b) return;\n" 717 " else return;", 718 AllowsMergedIfElse); 719 verifyFormat("MYIF constexpr (a)\n" 720 " MYIF constexpr (b) return;\n" 721 " else MYIF constexpr (c) return;\n" 722 " else return;", 723 AllowsMergedIfElse); 724 } 725 726 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 727 FormatStyle AllowsMergedIf = getLLVMStyle(); 728 AllowsMergedIf.IfMacros.push_back("MYIF"); 729 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 730 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 731 FormatStyle::SIS_WithoutElse; 732 verifyFormat("if (a)\n" 733 " f();\n" 734 "else {\n" 735 " g();\n" 736 "}", 737 AllowsMergedIf); 738 verifyFormat("if (a)\n" 739 " f();\n" 740 "else\n" 741 " g();\n", 742 AllowsMergedIf); 743 744 verifyFormat("if (a) g();", AllowsMergedIf); 745 verifyFormat("if (a) {\n" 746 " g()\n" 747 "};", 748 AllowsMergedIf); 749 verifyFormat("if (a)\n" 750 " g();\n" 751 "else\n" 752 " g();", 753 AllowsMergedIf); 754 verifyFormat("if (a) {\n" 755 " g();\n" 756 "} else\n" 757 " g();", 758 AllowsMergedIf); 759 verifyFormat("if (a)\n" 760 " g();\n" 761 "else {\n" 762 " g();\n" 763 "}", 764 AllowsMergedIf); 765 verifyFormat("if (a) {\n" 766 " g();\n" 767 "} else {\n" 768 " g();\n" 769 "}", 770 AllowsMergedIf); 771 verifyFormat("if (a)\n" 772 " g();\n" 773 "else if (b)\n" 774 " g();\n" 775 "else\n" 776 " g();", 777 AllowsMergedIf); 778 verifyFormat("if (a) {\n" 779 " g();\n" 780 "} else if (b)\n" 781 " g();\n" 782 "else\n" 783 " g();", 784 AllowsMergedIf); 785 verifyFormat("if (a)\n" 786 " g();\n" 787 "else if (b) {\n" 788 " g();\n" 789 "} else\n" 790 " g();", 791 AllowsMergedIf); 792 verifyFormat("if (a)\n" 793 " g();\n" 794 "else if (b)\n" 795 " g();\n" 796 "else {\n" 797 " g();\n" 798 "}", 799 AllowsMergedIf); 800 verifyFormat("if (a)\n" 801 " g();\n" 802 "else if (b) {\n" 803 " g();\n" 804 "} else {\n" 805 " g();\n" 806 "}", 807 AllowsMergedIf); 808 verifyFormat("if (a) {\n" 809 " g();\n" 810 "} else if (b) {\n" 811 " g();\n" 812 "} else {\n" 813 " g();\n" 814 "}", 815 AllowsMergedIf); 816 verifyFormat("MYIF (a)\n" 817 " f();\n" 818 "else {\n" 819 " g();\n" 820 "}", 821 AllowsMergedIf); 822 verifyFormat("MYIF (a)\n" 823 " f();\n" 824 "else\n" 825 " g();\n", 826 AllowsMergedIf); 827 828 verifyFormat("MYIF (a) g();", AllowsMergedIf); 829 verifyFormat("MYIF (a) {\n" 830 " g()\n" 831 "};", 832 AllowsMergedIf); 833 verifyFormat("MYIF (a)\n" 834 " g();\n" 835 "else\n" 836 " g();", 837 AllowsMergedIf); 838 verifyFormat("MYIF (a) {\n" 839 " g();\n" 840 "} else\n" 841 " g();", 842 AllowsMergedIf); 843 verifyFormat("MYIF (a)\n" 844 " g();\n" 845 "else {\n" 846 " g();\n" 847 "}", 848 AllowsMergedIf); 849 verifyFormat("MYIF (a) {\n" 850 " g();\n" 851 "} else {\n" 852 " g();\n" 853 "}", 854 AllowsMergedIf); 855 verifyFormat("MYIF (a)\n" 856 " g();\n" 857 "else MYIF (b)\n" 858 " g();\n" 859 "else\n" 860 " g();", 861 AllowsMergedIf); 862 verifyFormat("MYIF (a)\n" 863 " g();\n" 864 "else if (b)\n" 865 " g();\n" 866 "else\n" 867 " g();", 868 AllowsMergedIf); 869 verifyFormat("MYIF (a) {\n" 870 " g();\n" 871 "} else MYIF (b)\n" 872 " g();\n" 873 "else\n" 874 " g();", 875 AllowsMergedIf); 876 verifyFormat("MYIF (a) {\n" 877 " g();\n" 878 "} else if (b)\n" 879 " g();\n" 880 "else\n" 881 " g();", 882 AllowsMergedIf); 883 verifyFormat("MYIF (a)\n" 884 " g();\n" 885 "else MYIF (b) {\n" 886 " g();\n" 887 "} else\n" 888 " g();", 889 AllowsMergedIf); 890 verifyFormat("MYIF (a)\n" 891 " g();\n" 892 "else if (b) {\n" 893 " g();\n" 894 "} else\n" 895 " g();", 896 AllowsMergedIf); 897 verifyFormat("MYIF (a)\n" 898 " g();\n" 899 "else MYIF (b)\n" 900 " g();\n" 901 "else {\n" 902 " g();\n" 903 "}", 904 AllowsMergedIf); 905 verifyFormat("MYIF (a)\n" 906 " g();\n" 907 "else if (b)\n" 908 " g();\n" 909 "else {\n" 910 " g();\n" 911 "}", 912 AllowsMergedIf); 913 verifyFormat("MYIF (a)\n" 914 " g();\n" 915 "else MYIF (b) {\n" 916 " g();\n" 917 "} else {\n" 918 " g();\n" 919 "}", 920 AllowsMergedIf); 921 verifyFormat("MYIF (a)\n" 922 " g();\n" 923 "else if (b) {\n" 924 " g();\n" 925 "} else {\n" 926 " g();\n" 927 "}", 928 AllowsMergedIf); 929 verifyFormat("MYIF (a) {\n" 930 " g();\n" 931 "} else MYIF (b) {\n" 932 " g();\n" 933 "} else {\n" 934 " g();\n" 935 "}", 936 AllowsMergedIf); 937 verifyFormat("MYIF (a) {\n" 938 " g();\n" 939 "} else if (b) {\n" 940 " g();\n" 941 "} else {\n" 942 " g();\n" 943 "}", 944 AllowsMergedIf); 945 946 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 947 FormatStyle::SIS_OnlyFirstIf; 948 949 verifyFormat("if (a) f();\n" 950 "else {\n" 951 " g();\n" 952 "}", 953 AllowsMergedIf); 954 verifyFormat("if (a) f();\n" 955 "else {\n" 956 " if (a) f();\n" 957 " else {\n" 958 " g();\n" 959 " }\n" 960 " g();\n" 961 "}", 962 AllowsMergedIf); 963 964 verifyFormat("if (a) g();", AllowsMergedIf); 965 verifyFormat("if (a) {\n" 966 " g()\n" 967 "};", 968 AllowsMergedIf); 969 verifyFormat("if (a) g();\n" 970 "else\n" 971 " g();", 972 AllowsMergedIf); 973 verifyFormat("if (a) {\n" 974 " g();\n" 975 "} else\n" 976 " g();", 977 AllowsMergedIf); 978 verifyFormat("if (a) g();\n" 979 "else {\n" 980 " g();\n" 981 "}", 982 AllowsMergedIf); 983 verifyFormat("if (a) {\n" 984 " g();\n" 985 "} else {\n" 986 " g();\n" 987 "}", 988 AllowsMergedIf); 989 verifyFormat("if (a) g();\n" 990 "else if (b)\n" 991 " g();\n" 992 "else\n" 993 " g();", 994 AllowsMergedIf); 995 verifyFormat("if (a) {\n" 996 " g();\n" 997 "} else if (b)\n" 998 " g();\n" 999 "else\n" 1000 " g();", 1001 AllowsMergedIf); 1002 verifyFormat("if (a) g();\n" 1003 "else if (b) {\n" 1004 " g();\n" 1005 "} else\n" 1006 " g();", 1007 AllowsMergedIf); 1008 verifyFormat("if (a) g();\n" 1009 "else if (b)\n" 1010 " g();\n" 1011 "else {\n" 1012 " g();\n" 1013 "}", 1014 AllowsMergedIf); 1015 verifyFormat("if (a) g();\n" 1016 "else if (b) {\n" 1017 " g();\n" 1018 "} else {\n" 1019 " g();\n" 1020 "}", 1021 AllowsMergedIf); 1022 verifyFormat("if (a) {\n" 1023 " g();\n" 1024 "} else if (b) {\n" 1025 " g();\n" 1026 "} else {\n" 1027 " g();\n" 1028 "}", 1029 AllowsMergedIf); 1030 verifyFormat("MYIF (a) f();\n" 1031 "else {\n" 1032 " g();\n" 1033 "}", 1034 AllowsMergedIf); 1035 verifyFormat("MYIF (a) f();\n" 1036 "else {\n" 1037 " if (a) f();\n" 1038 " else {\n" 1039 " g();\n" 1040 " }\n" 1041 " g();\n" 1042 "}", 1043 AllowsMergedIf); 1044 1045 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1046 verifyFormat("MYIF (a) {\n" 1047 " g()\n" 1048 "};", 1049 AllowsMergedIf); 1050 verifyFormat("MYIF (a) g();\n" 1051 "else\n" 1052 " g();", 1053 AllowsMergedIf); 1054 verifyFormat("MYIF (a) {\n" 1055 " g();\n" 1056 "} else\n" 1057 " g();", 1058 AllowsMergedIf); 1059 verifyFormat("MYIF (a) g();\n" 1060 "else {\n" 1061 " g();\n" 1062 "}", 1063 AllowsMergedIf); 1064 verifyFormat("MYIF (a) {\n" 1065 " g();\n" 1066 "} else {\n" 1067 " g();\n" 1068 "}", 1069 AllowsMergedIf); 1070 verifyFormat("MYIF (a) g();\n" 1071 "else MYIF (b)\n" 1072 " g();\n" 1073 "else\n" 1074 " g();", 1075 AllowsMergedIf); 1076 verifyFormat("MYIF (a) g();\n" 1077 "else if (b)\n" 1078 " g();\n" 1079 "else\n" 1080 " g();", 1081 AllowsMergedIf); 1082 verifyFormat("MYIF (a) {\n" 1083 " g();\n" 1084 "} else MYIF (b)\n" 1085 " g();\n" 1086 "else\n" 1087 " g();", 1088 AllowsMergedIf); 1089 verifyFormat("MYIF (a) {\n" 1090 " g();\n" 1091 "} else if (b)\n" 1092 " g();\n" 1093 "else\n" 1094 " g();", 1095 AllowsMergedIf); 1096 verifyFormat("MYIF (a) g();\n" 1097 "else MYIF (b) {\n" 1098 " g();\n" 1099 "} else\n" 1100 " g();", 1101 AllowsMergedIf); 1102 verifyFormat("MYIF (a) g();\n" 1103 "else if (b) {\n" 1104 " g();\n" 1105 "} else\n" 1106 " g();", 1107 AllowsMergedIf); 1108 verifyFormat("MYIF (a) g();\n" 1109 "else MYIF (b)\n" 1110 " g();\n" 1111 "else {\n" 1112 " g();\n" 1113 "}", 1114 AllowsMergedIf); 1115 verifyFormat("MYIF (a) g();\n" 1116 "else if (b)\n" 1117 " g();\n" 1118 "else {\n" 1119 " g();\n" 1120 "}", 1121 AllowsMergedIf); 1122 verifyFormat("MYIF (a) g();\n" 1123 "else MYIF (b) {\n" 1124 " g();\n" 1125 "} else {\n" 1126 " g();\n" 1127 "}", 1128 AllowsMergedIf); 1129 verifyFormat("MYIF (a) g();\n" 1130 "else if (b) {\n" 1131 " g();\n" 1132 "} else {\n" 1133 " g();\n" 1134 "}", 1135 AllowsMergedIf); 1136 verifyFormat("MYIF (a) {\n" 1137 " g();\n" 1138 "} else MYIF (b) {\n" 1139 " g();\n" 1140 "} else {\n" 1141 " g();\n" 1142 "}", 1143 AllowsMergedIf); 1144 verifyFormat("MYIF (a) {\n" 1145 " g();\n" 1146 "} else if (b) {\n" 1147 " g();\n" 1148 "} else {\n" 1149 " g();\n" 1150 "}", 1151 AllowsMergedIf); 1152 1153 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1154 FormatStyle::SIS_AllIfsAndElse; 1155 1156 verifyFormat("if (a) f();\n" 1157 "else {\n" 1158 " g();\n" 1159 "}", 1160 AllowsMergedIf); 1161 verifyFormat("if (a) f();\n" 1162 "else {\n" 1163 " if (a) f();\n" 1164 " else {\n" 1165 " g();\n" 1166 " }\n" 1167 " g();\n" 1168 "}", 1169 AllowsMergedIf); 1170 1171 verifyFormat("if (a) g();", AllowsMergedIf); 1172 verifyFormat("if (a) {\n" 1173 " g()\n" 1174 "};", 1175 AllowsMergedIf); 1176 verifyFormat("if (a) g();\n" 1177 "else g();", 1178 AllowsMergedIf); 1179 verifyFormat("if (a) {\n" 1180 " g();\n" 1181 "} else g();", 1182 AllowsMergedIf); 1183 verifyFormat("if (a) g();\n" 1184 "else {\n" 1185 " g();\n" 1186 "}", 1187 AllowsMergedIf); 1188 verifyFormat("if (a) {\n" 1189 " g();\n" 1190 "} else {\n" 1191 " g();\n" 1192 "}", 1193 AllowsMergedIf); 1194 verifyFormat("if (a) g();\n" 1195 "else if (b) g();\n" 1196 "else g();", 1197 AllowsMergedIf); 1198 verifyFormat("if (a) {\n" 1199 " g();\n" 1200 "} else if (b) g();\n" 1201 "else g();", 1202 AllowsMergedIf); 1203 verifyFormat("if (a) g();\n" 1204 "else if (b) {\n" 1205 " g();\n" 1206 "} else g();", 1207 AllowsMergedIf); 1208 verifyFormat("if (a) g();\n" 1209 "else if (b) g();\n" 1210 "else {\n" 1211 " g();\n" 1212 "}", 1213 AllowsMergedIf); 1214 verifyFormat("if (a) g();\n" 1215 "else if (b) {\n" 1216 " g();\n" 1217 "} else {\n" 1218 " g();\n" 1219 "}", 1220 AllowsMergedIf); 1221 verifyFormat("if (a) {\n" 1222 " g();\n" 1223 "} else if (b) {\n" 1224 " g();\n" 1225 "} else {\n" 1226 " g();\n" 1227 "}", 1228 AllowsMergedIf); 1229 verifyFormat("MYIF (a) f();\n" 1230 "else {\n" 1231 " g();\n" 1232 "}", 1233 AllowsMergedIf); 1234 verifyFormat("MYIF (a) f();\n" 1235 "else {\n" 1236 " if (a) f();\n" 1237 " else {\n" 1238 " g();\n" 1239 " }\n" 1240 " g();\n" 1241 "}", 1242 AllowsMergedIf); 1243 1244 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1245 verifyFormat("MYIF (a) {\n" 1246 " g()\n" 1247 "};", 1248 AllowsMergedIf); 1249 verifyFormat("MYIF (a) g();\n" 1250 "else g();", 1251 AllowsMergedIf); 1252 verifyFormat("MYIF (a) {\n" 1253 " g();\n" 1254 "} else g();", 1255 AllowsMergedIf); 1256 verifyFormat("MYIF (a) g();\n" 1257 "else {\n" 1258 " g();\n" 1259 "}", 1260 AllowsMergedIf); 1261 verifyFormat("MYIF (a) {\n" 1262 " g();\n" 1263 "} else {\n" 1264 " g();\n" 1265 "}", 1266 AllowsMergedIf); 1267 verifyFormat("MYIF (a) g();\n" 1268 "else MYIF (b) g();\n" 1269 "else g();", 1270 AllowsMergedIf); 1271 verifyFormat("MYIF (a) g();\n" 1272 "else if (b) g();\n" 1273 "else g();", 1274 AllowsMergedIf); 1275 verifyFormat("MYIF (a) {\n" 1276 " g();\n" 1277 "} else MYIF (b) g();\n" 1278 "else g();", 1279 AllowsMergedIf); 1280 verifyFormat("MYIF (a) {\n" 1281 " g();\n" 1282 "} else if (b) g();\n" 1283 "else g();", 1284 AllowsMergedIf); 1285 verifyFormat("MYIF (a) g();\n" 1286 "else MYIF (b) {\n" 1287 " g();\n" 1288 "} else g();", 1289 AllowsMergedIf); 1290 verifyFormat("MYIF (a) g();\n" 1291 "else if (b) {\n" 1292 " g();\n" 1293 "} else g();", 1294 AllowsMergedIf); 1295 verifyFormat("MYIF (a) g();\n" 1296 "else MYIF (b) g();\n" 1297 "else {\n" 1298 " g();\n" 1299 "}", 1300 AllowsMergedIf); 1301 verifyFormat("MYIF (a) g();\n" 1302 "else if (b) g();\n" 1303 "else {\n" 1304 " g();\n" 1305 "}", 1306 AllowsMergedIf); 1307 verifyFormat("MYIF (a) g();\n" 1308 "else MYIF (b) {\n" 1309 " g();\n" 1310 "} else {\n" 1311 " g();\n" 1312 "}", 1313 AllowsMergedIf); 1314 verifyFormat("MYIF (a) g();\n" 1315 "else if (b) {\n" 1316 " g();\n" 1317 "} else {\n" 1318 " g();\n" 1319 "}", 1320 AllowsMergedIf); 1321 verifyFormat("MYIF (a) {\n" 1322 " g();\n" 1323 "} else MYIF (b) {\n" 1324 " g();\n" 1325 "} else {\n" 1326 " g();\n" 1327 "}", 1328 AllowsMergedIf); 1329 verifyFormat("MYIF (a) {\n" 1330 " g();\n" 1331 "} else if (b) {\n" 1332 " g();\n" 1333 "} else {\n" 1334 " g();\n" 1335 "}", 1336 AllowsMergedIf); 1337 } 1338 1339 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 1340 FormatStyle AllowsMergedLoops = getLLVMStyle(); 1341 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 1342 verifyFormat("while (true) continue;", AllowsMergedLoops); 1343 verifyFormat("for (;;) continue;", AllowsMergedLoops); 1344 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 1345 verifyFormat("while (true)\n" 1346 " ;", 1347 AllowsMergedLoops); 1348 verifyFormat("for (;;)\n" 1349 " ;", 1350 AllowsMergedLoops); 1351 verifyFormat("for (;;)\n" 1352 " for (;;) continue;", 1353 AllowsMergedLoops); 1354 verifyFormat("for (;;) // Can't merge this\n" 1355 " continue;", 1356 AllowsMergedLoops); 1357 verifyFormat("for (;;) /* still don't merge */\n" 1358 " continue;", 1359 AllowsMergedLoops); 1360 verifyFormat("do a++;\n" 1361 "while (true);", 1362 AllowsMergedLoops); 1363 verifyFormat("do /* Don't merge */\n" 1364 " a++;\n" 1365 "while (true);", 1366 AllowsMergedLoops); 1367 verifyFormat("do // Don't merge\n" 1368 " a++;\n" 1369 "while (true);", 1370 AllowsMergedLoops); 1371 verifyFormat("do\n" 1372 " // Don't merge\n" 1373 " a++;\n" 1374 "while (true);", 1375 AllowsMergedLoops); 1376 // Without braces labels are interpreted differently. 1377 verifyFormat("{\n" 1378 " do\n" 1379 " label:\n" 1380 " a++;\n" 1381 " while (true);\n" 1382 "}", 1383 AllowsMergedLoops); 1384 } 1385 1386 TEST_F(FormatTest, FormatShortBracedStatements) { 1387 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 1388 AllowSimpleBracedStatements.IfMacros.push_back("MYIF"); 1389 // Where line-lengths matter, a 2-letter synonym that maintains line length. 1390 // Not IF to avoid any confusion that IF is somehow special. 1391 AllowSimpleBracedStatements.IfMacros.push_back("FI"); 1392 AllowSimpleBracedStatements.ColumnLimit = 40; 1393 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1394 FormatStyle::SBS_Always; 1395 1396 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1397 FormatStyle::SIS_WithoutElse; 1398 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1399 1400 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 1401 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 1402 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 1403 1404 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1405 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1406 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1407 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1408 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1409 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1410 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1411 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1412 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1413 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1414 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1415 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1416 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1417 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1418 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1419 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1420 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1421 AllowSimpleBracedStatements); 1422 verifyFormat("if (true) {\n" 1423 " ffffffffffffffffffffffff();\n" 1424 "}", 1425 AllowSimpleBracedStatements); 1426 verifyFormat("if (true) {\n" 1427 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1428 "}", 1429 AllowSimpleBracedStatements); 1430 verifyFormat("if (true) { //\n" 1431 " f();\n" 1432 "}", 1433 AllowSimpleBracedStatements); 1434 verifyFormat("if (true) {\n" 1435 " f();\n" 1436 " f();\n" 1437 "}", 1438 AllowSimpleBracedStatements); 1439 verifyFormat("if (true) {\n" 1440 " f();\n" 1441 "} else {\n" 1442 " f();\n" 1443 "}", 1444 AllowSimpleBracedStatements); 1445 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1446 AllowSimpleBracedStatements); 1447 verifyFormat("MYIF (true) {\n" 1448 " ffffffffffffffffffffffff();\n" 1449 "}", 1450 AllowSimpleBracedStatements); 1451 verifyFormat("MYIF (true) {\n" 1452 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1453 "}", 1454 AllowSimpleBracedStatements); 1455 verifyFormat("MYIF (true) { //\n" 1456 " f();\n" 1457 "}", 1458 AllowSimpleBracedStatements); 1459 verifyFormat("MYIF (true) {\n" 1460 " f();\n" 1461 " f();\n" 1462 "}", 1463 AllowSimpleBracedStatements); 1464 verifyFormat("MYIF (true) {\n" 1465 " f();\n" 1466 "} else {\n" 1467 " f();\n" 1468 "}", 1469 AllowSimpleBracedStatements); 1470 1471 verifyFormat("struct A2 {\n" 1472 " int X;\n" 1473 "};", 1474 AllowSimpleBracedStatements); 1475 verifyFormat("typedef struct A2 {\n" 1476 " int X;\n" 1477 "} A2_t;", 1478 AllowSimpleBracedStatements); 1479 verifyFormat("template <int> struct A2 {\n" 1480 " struct B {};\n" 1481 "};", 1482 AllowSimpleBracedStatements); 1483 1484 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1485 FormatStyle::SIS_Never; 1486 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1487 verifyFormat("if (true) {\n" 1488 " f();\n" 1489 "}", 1490 AllowSimpleBracedStatements); 1491 verifyFormat("if (true) {\n" 1492 " f();\n" 1493 "} else {\n" 1494 " f();\n" 1495 "}", 1496 AllowSimpleBracedStatements); 1497 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1498 verifyFormat("MYIF (true) {\n" 1499 " f();\n" 1500 "}", 1501 AllowSimpleBracedStatements); 1502 verifyFormat("MYIF (true) {\n" 1503 " f();\n" 1504 "} else {\n" 1505 " f();\n" 1506 "}", 1507 AllowSimpleBracedStatements); 1508 1509 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1510 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1511 verifyFormat("while (true) {\n" 1512 " f();\n" 1513 "}", 1514 AllowSimpleBracedStatements); 1515 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1516 verifyFormat("for (;;) {\n" 1517 " f();\n" 1518 "}", 1519 AllowSimpleBracedStatements); 1520 1521 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1522 FormatStyle::SIS_WithoutElse; 1523 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1524 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = 1525 FormatStyle::BWACS_Always; 1526 1527 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1528 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1529 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1530 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1531 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1532 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1533 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1534 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1535 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1536 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1537 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1538 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1539 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1540 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1541 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1542 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1543 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1544 AllowSimpleBracedStatements); 1545 verifyFormat("if (true)\n" 1546 "{\n" 1547 " ffffffffffffffffffffffff();\n" 1548 "}", 1549 AllowSimpleBracedStatements); 1550 verifyFormat("if (true)\n" 1551 "{\n" 1552 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1553 "}", 1554 AllowSimpleBracedStatements); 1555 verifyFormat("if (true)\n" 1556 "{ //\n" 1557 " f();\n" 1558 "}", 1559 AllowSimpleBracedStatements); 1560 verifyFormat("if (true)\n" 1561 "{\n" 1562 " f();\n" 1563 " f();\n" 1564 "}", 1565 AllowSimpleBracedStatements); 1566 verifyFormat("if (true)\n" 1567 "{\n" 1568 " f();\n" 1569 "} else\n" 1570 "{\n" 1571 " f();\n" 1572 "}", 1573 AllowSimpleBracedStatements); 1574 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1575 AllowSimpleBracedStatements); 1576 verifyFormat("MYIF (true)\n" 1577 "{\n" 1578 " ffffffffffffffffffffffff();\n" 1579 "}", 1580 AllowSimpleBracedStatements); 1581 verifyFormat("MYIF (true)\n" 1582 "{\n" 1583 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1584 "}", 1585 AllowSimpleBracedStatements); 1586 verifyFormat("MYIF (true)\n" 1587 "{ //\n" 1588 " f();\n" 1589 "}", 1590 AllowSimpleBracedStatements); 1591 verifyFormat("MYIF (true)\n" 1592 "{\n" 1593 " f();\n" 1594 " f();\n" 1595 "}", 1596 AllowSimpleBracedStatements); 1597 verifyFormat("MYIF (true)\n" 1598 "{\n" 1599 " f();\n" 1600 "} else\n" 1601 "{\n" 1602 " f();\n" 1603 "}", 1604 AllowSimpleBracedStatements); 1605 1606 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1607 FormatStyle::SIS_Never; 1608 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1609 verifyFormat("if (true)\n" 1610 "{\n" 1611 " f();\n" 1612 "}", 1613 AllowSimpleBracedStatements); 1614 verifyFormat("if (true)\n" 1615 "{\n" 1616 " f();\n" 1617 "} else\n" 1618 "{\n" 1619 " f();\n" 1620 "}", 1621 AllowSimpleBracedStatements); 1622 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1623 verifyFormat("MYIF (true)\n" 1624 "{\n" 1625 " f();\n" 1626 "}", 1627 AllowSimpleBracedStatements); 1628 verifyFormat("MYIF (true)\n" 1629 "{\n" 1630 " f();\n" 1631 "} else\n" 1632 "{\n" 1633 " f();\n" 1634 "}", 1635 AllowSimpleBracedStatements); 1636 1637 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1638 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1639 verifyFormat("while (true)\n" 1640 "{\n" 1641 " f();\n" 1642 "}", 1643 AllowSimpleBracedStatements); 1644 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1645 verifyFormat("for (;;)\n" 1646 "{\n" 1647 " f();\n" 1648 "}", 1649 AllowSimpleBracedStatements); 1650 } 1651 1652 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 1653 FormatStyle Style = getLLVMStyleWithColumns(60); 1654 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 1655 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 1656 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1657 EXPECT_EQ("#define A \\\n" 1658 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 1659 " { \\\n" 1660 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1661 " }\n" 1662 "X;", 1663 format("#define A \\\n" 1664 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 1665 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1666 " }\n" 1667 "X;", 1668 Style)); 1669 } 1670 1671 TEST_F(FormatTest, ParseIfElse) { 1672 verifyFormat("if (true)\n" 1673 " if (true)\n" 1674 " if (true)\n" 1675 " f();\n" 1676 " else\n" 1677 " g();\n" 1678 " else\n" 1679 " h();\n" 1680 "else\n" 1681 " i();"); 1682 verifyFormat("if (true)\n" 1683 " if (true)\n" 1684 " if (true) {\n" 1685 " if (true)\n" 1686 " f();\n" 1687 " } else {\n" 1688 " g();\n" 1689 " }\n" 1690 " else\n" 1691 " h();\n" 1692 "else {\n" 1693 " i();\n" 1694 "}"); 1695 verifyFormat("if (true)\n" 1696 " if constexpr (true)\n" 1697 " if (true) {\n" 1698 " if constexpr (true)\n" 1699 " f();\n" 1700 " } else {\n" 1701 " g();\n" 1702 " }\n" 1703 " else\n" 1704 " h();\n" 1705 "else {\n" 1706 " i();\n" 1707 "}"); 1708 verifyFormat("if (true)\n" 1709 " if CONSTEXPR (true)\n" 1710 " if (true) {\n" 1711 " if CONSTEXPR (true)\n" 1712 " f();\n" 1713 " } else {\n" 1714 " g();\n" 1715 " }\n" 1716 " else\n" 1717 " h();\n" 1718 "else {\n" 1719 " i();\n" 1720 "}"); 1721 verifyFormat("void f() {\n" 1722 " if (a) {\n" 1723 " } else {\n" 1724 " }\n" 1725 "}"); 1726 } 1727 1728 TEST_F(FormatTest, ElseIf) { 1729 verifyFormat("if (a) {\n} else if (b) {\n}"); 1730 verifyFormat("if (a)\n" 1731 " f();\n" 1732 "else if (b)\n" 1733 " g();\n" 1734 "else\n" 1735 " h();"); 1736 verifyFormat("if (a)\n" 1737 " f();\n" 1738 "else // comment\n" 1739 " if (b) {\n" 1740 " g();\n" 1741 " h();\n" 1742 " }"); 1743 verifyFormat("if constexpr (a)\n" 1744 " f();\n" 1745 "else if constexpr (b)\n" 1746 " g();\n" 1747 "else\n" 1748 " h();"); 1749 verifyFormat("if CONSTEXPR (a)\n" 1750 " f();\n" 1751 "else if CONSTEXPR (b)\n" 1752 " g();\n" 1753 "else\n" 1754 " h();"); 1755 verifyFormat("if (a) {\n" 1756 " f();\n" 1757 "}\n" 1758 "// or else ..\n" 1759 "else {\n" 1760 " g()\n" 1761 "}"); 1762 1763 verifyFormat("if (a) {\n" 1764 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1766 "}"); 1767 verifyFormat("if (a) {\n" 1768 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1769 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1770 "}"); 1771 verifyFormat("if (a) {\n" 1772 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1773 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1774 "}"); 1775 verifyFormat("if (a) {\n" 1776 "} else if (\n" 1777 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1778 "}", 1779 getLLVMStyleWithColumns(62)); 1780 verifyFormat("if (a) {\n" 1781 "} else if constexpr (\n" 1782 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1783 "}", 1784 getLLVMStyleWithColumns(62)); 1785 verifyFormat("if (a) {\n" 1786 "} else if CONSTEXPR (\n" 1787 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1788 "}", 1789 getLLVMStyleWithColumns(62)); 1790 } 1791 1792 TEST_F(FormatTest, SeparatePointerReferenceAlignment) { 1793 FormatStyle Style = getLLVMStyle(); 1794 // Check first the default LLVM style 1795 // Style.PointerAlignment = FormatStyle::PAS_Right; 1796 // Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1797 verifyFormat("int *f1(int *a, int &b, int &&c);", Style); 1798 verifyFormat("int &f2(int &&c, int *a, int &b);", Style); 1799 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style); 1800 verifyFormat("int *f1(int &a) const &;", Style); 1801 verifyFormat("int *f1(int &a) const & = 0;", Style); 1802 verifyFormat("int *a = f1();", Style); 1803 verifyFormat("int &b = f2();", Style); 1804 verifyFormat("int &&c = f3();", Style); 1805 1806 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1807 verifyFormat("Const unsigned int *c;\n" 1808 "const unsigned int *d;\n" 1809 "Const unsigned int &e;\n" 1810 "const unsigned int &f;\n" 1811 "const unsigned &&g;\n" 1812 "Const unsigned h;", 1813 Style); 1814 1815 Style.PointerAlignment = FormatStyle::PAS_Left; 1816 Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1817 verifyFormat("int* f1(int* a, int& b, int&& c);", Style); 1818 verifyFormat("int& f2(int&& c, int* a, int& b);", Style); 1819 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style); 1820 verifyFormat("int* f1(int& a) const& = 0;", Style); 1821 verifyFormat("int* a = f1();", Style); 1822 verifyFormat("int& b = f2();", Style); 1823 verifyFormat("int&& c = f3();", Style); 1824 1825 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1826 verifyFormat("Const unsigned int* c;\n" 1827 "const unsigned int* d;\n" 1828 "Const unsigned int& e;\n" 1829 "const unsigned int& f;\n" 1830 "const unsigned&& g;\n" 1831 "Const unsigned h;", 1832 Style); 1833 1834 Style.PointerAlignment = FormatStyle::PAS_Right; 1835 Style.ReferenceAlignment = FormatStyle::RAS_Left; 1836 verifyFormat("int *f1(int *a, int& b, int&& c);", Style); 1837 verifyFormat("int& f2(int&& c, int *a, int& b);", Style); 1838 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style); 1839 verifyFormat("int *a = f1();", Style); 1840 verifyFormat("int& b = f2();", Style); 1841 verifyFormat("int&& c = f3();", Style); 1842 1843 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1844 verifyFormat("Const unsigned int *c;\n" 1845 "const unsigned int *d;\n" 1846 "Const unsigned int& e;\n" 1847 "const unsigned int& f;\n" 1848 "const unsigned g;\n" 1849 "Const unsigned h;", 1850 Style); 1851 1852 Style.PointerAlignment = FormatStyle::PAS_Left; 1853 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 1854 verifyFormat("int* f1(int* a, int & b, int && c);", Style); 1855 verifyFormat("int & f2(int && c, int* a, int & b);", Style); 1856 verifyFormat("int && f3(int & b, int && c, int* a);", Style); 1857 verifyFormat("int* a = f1();", Style); 1858 verifyFormat("int & b = f2();", Style); 1859 verifyFormat("int && c = f3();", Style); 1860 1861 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1862 verifyFormat("Const unsigned int* c;\n" 1863 "const unsigned int* d;\n" 1864 "Const unsigned int & e;\n" 1865 "const unsigned int & f;\n" 1866 "const unsigned && g;\n" 1867 "Const unsigned h;", 1868 Style); 1869 1870 Style.PointerAlignment = FormatStyle::PAS_Middle; 1871 Style.ReferenceAlignment = FormatStyle::RAS_Right; 1872 verifyFormat("int * f1(int * a, int &b, int &&c);", Style); 1873 verifyFormat("int &f2(int &&c, int * a, int &b);", Style); 1874 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); 1875 verifyFormat("int * a = f1();", Style); 1876 verifyFormat("int &b = f2();", Style); 1877 verifyFormat("int &&c = f3();", Style); 1878 1879 // FIXME: we don't handle this yet, so output may be arbitrary until it's 1880 // specifically handled 1881 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); 1882 } 1883 1884 TEST_F(FormatTest, FormatsForLoop) { 1885 verifyFormat( 1886 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 1887 " ++VeryVeryLongLoopVariable)\n" 1888 " ;"); 1889 verifyFormat("for (;;)\n" 1890 " f();"); 1891 verifyFormat("for (;;) {\n}"); 1892 verifyFormat("for (;;) {\n" 1893 " f();\n" 1894 "}"); 1895 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 1896 1897 verifyFormat( 1898 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 1899 " E = UnwrappedLines.end();\n" 1900 " I != E; ++I) {\n}"); 1901 1902 verifyFormat( 1903 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 1904 " ++IIIII) {\n}"); 1905 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 1906 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 1907 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 1908 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 1909 " I = FD->getDeclsInPrototypeScope().begin(),\n" 1910 " E = FD->getDeclsInPrototypeScope().end();\n" 1911 " I != E; ++I) {\n}"); 1912 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 1913 " I = Container.begin(),\n" 1914 " E = Container.end();\n" 1915 " I != E; ++I) {\n}", 1916 getLLVMStyleWithColumns(76)); 1917 1918 verifyFormat( 1919 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 1920 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 1921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1922 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 1923 " ++aaaaaaaaaaa) {\n}"); 1924 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1925 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 1926 " ++i) {\n}"); 1927 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 1928 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 1929 "}"); 1930 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 1931 " aaaaaaaaaa);\n" 1932 " iter; ++iter) {\n" 1933 "}"); 1934 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1935 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 1936 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 1937 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 1938 1939 // These should not be formatted as Objective-C for-in loops. 1940 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 1941 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 1942 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 1943 verifyFormat( 1944 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 1945 1946 FormatStyle NoBinPacking = getLLVMStyle(); 1947 NoBinPacking.BinPackParameters = false; 1948 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 1949 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 1950 " aaaaaaaaaaaaaaaa,\n" 1951 " aaaaaaaaaaaaaaaa,\n" 1952 " aaaaaaaaaaaaaaaa);\n" 1953 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 1954 "}", 1955 NoBinPacking); 1956 verifyFormat( 1957 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 1958 " E = UnwrappedLines.end();\n" 1959 " I != E;\n" 1960 " ++I) {\n}", 1961 NoBinPacking); 1962 1963 FormatStyle AlignLeft = getLLVMStyle(); 1964 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 1965 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 1966 } 1967 1968 TEST_F(FormatTest, RangeBasedForLoops) { 1969 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 1970 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1971 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 1972 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 1973 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 1974 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1975 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 1976 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 1977 } 1978 1979 TEST_F(FormatTest, ForEachLoops) { 1980 verifyFormat("void f() {\n" 1981 " foreach (Item *item, itemlist) {}\n" 1982 " Q_FOREACH (Item *item, itemlist) {}\n" 1983 " BOOST_FOREACH (Item *item, itemlist) {}\n" 1984 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 1985 "}"); 1986 1987 FormatStyle Style = getLLVMStyle(); 1988 Style.SpaceBeforeParens = 1989 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 1990 verifyFormat("void f() {\n" 1991 " foreach(Item *item, itemlist) {}\n" 1992 " Q_FOREACH(Item *item, itemlist) {}\n" 1993 " BOOST_FOREACH(Item *item, itemlist) {}\n" 1994 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 1995 "}", 1996 Style); 1997 1998 // As function-like macros. 1999 verifyFormat("#define foreach(x, y)\n" 2000 "#define Q_FOREACH(x, y)\n" 2001 "#define BOOST_FOREACH(x, y)\n" 2002 "#define UNKNOWN_FOREACH(x, y)\n"); 2003 2004 // Not as function-like macros. 2005 verifyFormat("#define foreach (x, y)\n" 2006 "#define Q_FOREACH (x, y)\n" 2007 "#define BOOST_FOREACH (x, y)\n" 2008 "#define UNKNOWN_FOREACH (x, y)\n"); 2009 2010 // handle microsoft non standard extension 2011 verifyFormat("for each (char c in x->MyStringProperty)"); 2012 } 2013 2014 TEST_F(FormatTest, FormatsWhileLoop) { 2015 verifyFormat("while (true) {\n}"); 2016 verifyFormat("while (true)\n" 2017 " f();"); 2018 verifyFormat("while () {\n}"); 2019 verifyFormat("while () {\n" 2020 " f();\n" 2021 "}"); 2022 } 2023 2024 TEST_F(FormatTest, FormatsDoWhile) { 2025 verifyFormat("do {\n" 2026 " do_something();\n" 2027 "} while (something());"); 2028 verifyFormat("do\n" 2029 " do_something();\n" 2030 "while (something());"); 2031 } 2032 2033 TEST_F(FormatTest, FormatsSwitchStatement) { 2034 verifyFormat("switch (x) {\n" 2035 "case 1:\n" 2036 " f();\n" 2037 " break;\n" 2038 "case kFoo:\n" 2039 "case ns::kBar:\n" 2040 "case kBaz:\n" 2041 " break;\n" 2042 "default:\n" 2043 " g();\n" 2044 " break;\n" 2045 "}"); 2046 verifyFormat("switch (x) {\n" 2047 "case 1: {\n" 2048 " f();\n" 2049 " break;\n" 2050 "}\n" 2051 "case 2: {\n" 2052 " break;\n" 2053 "}\n" 2054 "}"); 2055 verifyFormat("switch (x) {\n" 2056 "case 1: {\n" 2057 " f();\n" 2058 " {\n" 2059 " g();\n" 2060 " h();\n" 2061 " }\n" 2062 " break;\n" 2063 "}\n" 2064 "}"); 2065 verifyFormat("switch (x) {\n" 2066 "case 1: {\n" 2067 " f();\n" 2068 " if (foo) {\n" 2069 " g();\n" 2070 " h();\n" 2071 " }\n" 2072 " break;\n" 2073 "}\n" 2074 "}"); 2075 verifyFormat("switch (x) {\n" 2076 "case 1: {\n" 2077 " f();\n" 2078 " g();\n" 2079 "} break;\n" 2080 "}"); 2081 verifyFormat("switch (test)\n" 2082 " ;"); 2083 verifyFormat("switch (x) {\n" 2084 "default: {\n" 2085 " // Do nothing.\n" 2086 "}\n" 2087 "}"); 2088 verifyFormat("switch (x) {\n" 2089 "// comment\n" 2090 "// if 1, do f()\n" 2091 "case 1:\n" 2092 " f();\n" 2093 "}"); 2094 verifyFormat("switch (x) {\n" 2095 "case 1:\n" 2096 " // Do amazing stuff\n" 2097 " {\n" 2098 " f();\n" 2099 " g();\n" 2100 " }\n" 2101 " break;\n" 2102 "}"); 2103 verifyFormat("#define A \\\n" 2104 " switch (x) { \\\n" 2105 " case a: \\\n" 2106 " foo = b; \\\n" 2107 " }", 2108 getLLVMStyleWithColumns(20)); 2109 verifyFormat("#define OPERATION_CASE(name) \\\n" 2110 " case OP_name: \\\n" 2111 " return operations::Operation##name\n", 2112 getLLVMStyleWithColumns(40)); 2113 verifyFormat("switch (x) {\n" 2114 "case 1:;\n" 2115 "default:;\n" 2116 " int i;\n" 2117 "}"); 2118 2119 verifyGoogleFormat("switch (x) {\n" 2120 " case 1:\n" 2121 " f();\n" 2122 " break;\n" 2123 " case kFoo:\n" 2124 " case ns::kBar:\n" 2125 " case kBaz:\n" 2126 " break;\n" 2127 " default:\n" 2128 " g();\n" 2129 " break;\n" 2130 "}"); 2131 verifyGoogleFormat("switch (x) {\n" 2132 " case 1: {\n" 2133 " f();\n" 2134 " break;\n" 2135 " }\n" 2136 "}"); 2137 verifyGoogleFormat("switch (test)\n" 2138 " ;"); 2139 2140 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 2141 " case OP_name: \\\n" 2142 " return operations::Operation##name\n"); 2143 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 2144 " // Get the correction operation class.\n" 2145 " switch (OpCode) {\n" 2146 " CASE(Add);\n" 2147 " CASE(Subtract);\n" 2148 " default:\n" 2149 " return operations::Unknown;\n" 2150 " }\n" 2151 "#undef OPERATION_CASE\n" 2152 "}"); 2153 verifyFormat("DEBUG({\n" 2154 " switch (x) {\n" 2155 " case A:\n" 2156 " f();\n" 2157 " break;\n" 2158 " // fallthrough\n" 2159 " case B:\n" 2160 " g();\n" 2161 " break;\n" 2162 " }\n" 2163 "});"); 2164 EXPECT_EQ("DEBUG({\n" 2165 " switch (x) {\n" 2166 " case A:\n" 2167 " f();\n" 2168 " break;\n" 2169 " // On B:\n" 2170 " case B:\n" 2171 " g();\n" 2172 " break;\n" 2173 " }\n" 2174 "});", 2175 format("DEBUG({\n" 2176 " switch (x) {\n" 2177 " case A:\n" 2178 " f();\n" 2179 " break;\n" 2180 " // On B:\n" 2181 " case B:\n" 2182 " g();\n" 2183 " break;\n" 2184 " }\n" 2185 "});", 2186 getLLVMStyle())); 2187 EXPECT_EQ("switch (n) {\n" 2188 "case 0: {\n" 2189 " return false;\n" 2190 "}\n" 2191 "default: {\n" 2192 " return true;\n" 2193 "}\n" 2194 "}", 2195 format("switch (n)\n" 2196 "{\n" 2197 "case 0: {\n" 2198 " return false;\n" 2199 "}\n" 2200 "default: {\n" 2201 " return true;\n" 2202 "}\n" 2203 "}", 2204 getLLVMStyle())); 2205 verifyFormat("switch (a) {\n" 2206 "case (b):\n" 2207 " return;\n" 2208 "}"); 2209 2210 verifyFormat("switch (a) {\n" 2211 "case some_namespace::\n" 2212 " some_constant:\n" 2213 " return;\n" 2214 "}", 2215 getLLVMStyleWithColumns(34)); 2216 2217 FormatStyle Style = getLLVMStyle(); 2218 Style.IndentCaseLabels = true; 2219 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 2220 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2221 Style.BraceWrapping.AfterCaseLabel = true; 2222 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2223 EXPECT_EQ("switch (n)\n" 2224 "{\n" 2225 " case 0:\n" 2226 " {\n" 2227 " return false;\n" 2228 " }\n" 2229 " default:\n" 2230 " {\n" 2231 " return true;\n" 2232 " }\n" 2233 "}", 2234 format("switch (n) {\n" 2235 " case 0: {\n" 2236 " return false;\n" 2237 " }\n" 2238 " default: {\n" 2239 " return true;\n" 2240 " }\n" 2241 "}", 2242 Style)); 2243 Style.BraceWrapping.AfterCaseLabel = false; 2244 EXPECT_EQ("switch (n)\n" 2245 "{\n" 2246 " case 0: {\n" 2247 " return false;\n" 2248 " }\n" 2249 " default: {\n" 2250 " return true;\n" 2251 " }\n" 2252 "}", 2253 format("switch (n) {\n" 2254 " case 0:\n" 2255 " {\n" 2256 " return false;\n" 2257 " }\n" 2258 " default:\n" 2259 " {\n" 2260 " return true;\n" 2261 " }\n" 2262 "}", 2263 Style)); 2264 Style.IndentCaseLabels = false; 2265 Style.IndentCaseBlocks = true; 2266 EXPECT_EQ("switch (n)\n" 2267 "{\n" 2268 "case 0:\n" 2269 " {\n" 2270 " return false;\n" 2271 " }\n" 2272 "case 1:\n" 2273 " break;\n" 2274 "default:\n" 2275 " {\n" 2276 " return true;\n" 2277 " }\n" 2278 "}", 2279 format("switch (n) {\n" 2280 "case 0: {\n" 2281 " return false;\n" 2282 "}\n" 2283 "case 1:\n" 2284 " break;\n" 2285 "default: {\n" 2286 " return true;\n" 2287 "}\n" 2288 "}", 2289 Style)); 2290 Style.IndentCaseLabels = true; 2291 Style.IndentCaseBlocks = true; 2292 EXPECT_EQ("switch (n)\n" 2293 "{\n" 2294 " case 0:\n" 2295 " {\n" 2296 " return false;\n" 2297 " }\n" 2298 " case 1:\n" 2299 " break;\n" 2300 " default:\n" 2301 " {\n" 2302 " return true;\n" 2303 " }\n" 2304 "}", 2305 format("switch (n) {\n" 2306 "case 0: {\n" 2307 " return false;\n" 2308 "}\n" 2309 "case 1:\n" 2310 " break;\n" 2311 "default: {\n" 2312 " return true;\n" 2313 "}\n" 2314 "}", 2315 Style)); 2316 } 2317 2318 TEST_F(FormatTest, CaseRanges) { 2319 verifyFormat("switch (x) {\n" 2320 "case 'A' ... 'Z':\n" 2321 "case 1 ... 5:\n" 2322 "case a ... b:\n" 2323 " break;\n" 2324 "}"); 2325 } 2326 2327 TEST_F(FormatTest, ShortEnums) { 2328 FormatStyle Style = getLLVMStyle(); 2329 Style.AllowShortEnumsOnASingleLine = true; 2330 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2331 Style.AllowShortEnumsOnASingleLine = false; 2332 verifyFormat("enum\n" 2333 "{\n" 2334 " A,\n" 2335 " B,\n" 2336 " C\n" 2337 "} ShortEnum1, ShortEnum2;", 2338 Style); 2339 } 2340 2341 TEST_F(FormatTest, ShortCaseLabels) { 2342 FormatStyle Style = getLLVMStyle(); 2343 Style.AllowShortCaseLabelsOnASingleLine = true; 2344 verifyFormat("switch (a) {\n" 2345 "case 1: x = 1; break;\n" 2346 "case 2: return;\n" 2347 "case 3:\n" 2348 "case 4:\n" 2349 "case 5: return;\n" 2350 "case 6: // comment\n" 2351 " return;\n" 2352 "case 7:\n" 2353 " // comment\n" 2354 " return;\n" 2355 "case 8:\n" 2356 " x = 8; // comment\n" 2357 " break;\n" 2358 "default: y = 1; break;\n" 2359 "}", 2360 Style); 2361 verifyFormat("switch (a) {\n" 2362 "case 0: return; // comment\n" 2363 "case 1: break; // comment\n" 2364 "case 2: return;\n" 2365 "// comment\n" 2366 "case 3: return;\n" 2367 "// comment 1\n" 2368 "// comment 2\n" 2369 "// comment 3\n" 2370 "case 4: break; /* comment */\n" 2371 "case 5:\n" 2372 " // comment\n" 2373 " break;\n" 2374 "case 6: /* comment */ x = 1; break;\n" 2375 "case 7: x = /* comment */ 1; break;\n" 2376 "case 8:\n" 2377 " x = 1; /* comment */\n" 2378 " break;\n" 2379 "case 9:\n" 2380 " break; // comment line 1\n" 2381 " // comment line 2\n" 2382 "}", 2383 Style); 2384 EXPECT_EQ("switch (a) {\n" 2385 "case 1:\n" 2386 " x = 8;\n" 2387 " // fall through\n" 2388 "case 2: x = 8;\n" 2389 "// comment\n" 2390 "case 3:\n" 2391 " return; /* comment line 1\n" 2392 " * comment line 2 */\n" 2393 "case 4: i = 8;\n" 2394 "// something else\n" 2395 "#if FOO\n" 2396 "case 5: break;\n" 2397 "#endif\n" 2398 "}", 2399 format("switch (a) {\n" 2400 "case 1: x = 8;\n" 2401 " // fall through\n" 2402 "case 2:\n" 2403 " x = 8;\n" 2404 "// comment\n" 2405 "case 3:\n" 2406 " return; /* comment line 1\n" 2407 " * comment line 2 */\n" 2408 "case 4:\n" 2409 " i = 8;\n" 2410 "// something else\n" 2411 "#if FOO\n" 2412 "case 5: break;\n" 2413 "#endif\n" 2414 "}", 2415 Style)); 2416 EXPECT_EQ("switch (a) {\n" 2417 "case 0:\n" 2418 " return; // long long long long long long long long long long " 2419 "long long comment\n" 2420 " // line\n" 2421 "}", 2422 format("switch (a) {\n" 2423 "case 0: return; // long long long long long long long long " 2424 "long long long long comment line\n" 2425 "}", 2426 Style)); 2427 EXPECT_EQ("switch (a) {\n" 2428 "case 0:\n" 2429 " return; /* long long long long long long long long long long " 2430 "long long comment\n" 2431 " line */\n" 2432 "}", 2433 format("switch (a) {\n" 2434 "case 0: return; /* long long long long long long long long " 2435 "long long long long comment line */\n" 2436 "}", 2437 Style)); 2438 verifyFormat("switch (a) {\n" 2439 "#if FOO\n" 2440 "case 0: return 0;\n" 2441 "#endif\n" 2442 "}", 2443 Style); 2444 verifyFormat("switch (a) {\n" 2445 "case 1: {\n" 2446 "}\n" 2447 "case 2: {\n" 2448 " return;\n" 2449 "}\n" 2450 "case 3: {\n" 2451 " x = 1;\n" 2452 " return;\n" 2453 "}\n" 2454 "case 4:\n" 2455 " if (x)\n" 2456 " return;\n" 2457 "}", 2458 Style); 2459 Style.ColumnLimit = 21; 2460 verifyFormat("switch (a) {\n" 2461 "case 1: x = 1; break;\n" 2462 "case 2: return;\n" 2463 "case 3:\n" 2464 "case 4:\n" 2465 "case 5: return;\n" 2466 "default:\n" 2467 " y = 1;\n" 2468 " break;\n" 2469 "}", 2470 Style); 2471 Style.ColumnLimit = 80; 2472 Style.AllowShortCaseLabelsOnASingleLine = false; 2473 Style.IndentCaseLabels = true; 2474 EXPECT_EQ("switch (n) {\n" 2475 " default /*comments*/:\n" 2476 " return true;\n" 2477 " case 0:\n" 2478 " return false;\n" 2479 "}", 2480 format("switch (n) {\n" 2481 "default/*comments*/:\n" 2482 " return true;\n" 2483 "case 0:\n" 2484 " return false;\n" 2485 "}", 2486 Style)); 2487 Style.AllowShortCaseLabelsOnASingleLine = true; 2488 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2489 Style.BraceWrapping.AfterCaseLabel = true; 2490 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2491 EXPECT_EQ("switch (n)\n" 2492 "{\n" 2493 " case 0:\n" 2494 " {\n" 2495 " return false;\n" 2496 " }\n" 2497 " default:\n" 2498 " {\n" 2499 " return true;\n" 2500 " }\n" 2501 "}", 2502 format("switch (n) {\n" 2503 " case 0: {\n" 2504 " return false;\n" 2505 " }\n" 2506 " default:\n" 2507 " {\n" 2508 " return true;\n" 2509 " }\n" 2510 "}", 2511 Style)); 2512 } 2513 2514 TEST_F(FormatTest, FormatsLabels) { 2515 verifyFormat("void f() {\n" 2516 " some_code();\n" 2517 "test_label:\n" 2518 " some_other_code();\n" 2519 " {\n" 2520 " some_more_code();\n" 2521 " another_label:\n" 2522 " some_more_code();\n" 2523 " }\n" 2524 "}"); 2525 verifyFormat("{\n" 2526 " some_code();\n" 2527 "test_label:\n" 2528 " some_other_code();\n" 2529 "}"); 2530 verifyFormat("{\n" 2531 " some_code();\n" 2532 "test_label:;\n" 2533 " int i = 0;\n" 2534 "}"); 2535 FormatStyle Style = getLLVMStyle(); 2536 Style.IndentGotoLabels = false; 2537 verifyFormat("void f() {\n" 2538 " some_code();\n" 2539 "test_label:\n" 2540 " some_other_code();\n" 2541 " {\n" 2542 " some_more_code();\n" 2543 "another_label:\n" 2544 " some_more_code();\n" 2545 " }\n" 2546 "}", 2547 Style); 2548 verifyFormat("{\n" 2549 " some_code();\n" 2550 "test_label:\n" 2551 " some_other_code();\n" 2552 "}", 2553 Style); 2554 verifyFormat("{\n" 2555 " some_code();\n" 2556 "test_label:;\n" 2557 " int i = 0;\n" 2558 "}"); 2559 } 2560 2561 TEST_F(FormatTest, MultiLineControlStatements) { 2562 FormatStyle Style = getLLVMStyle(); 2563 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2564 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 2565 Style.ColumnLimit = 20; 2566 // Short lines should keep opening brace on same line. 2567 EXPECT_EQ("if (foo) {\n" 2568 " bar();\n" 2569 "}", 2570 format("if(foo){bar();}", Style)); 2571 EXPECT_EQ("if (foo) {\n" 2572 " bar();\n" 2573 "} else {\n" 2574 " baz();\n" 2575 "}", 2576 format("if(foo){bar();}else{baz();}", Style)); 2577 EXPECT_EQ("if (foo && bar) {\n" 2578 " baz();\n" 2579 "}", 2580 format("if(foo&&bar){baz();}", Style)); 2581 EXPECT_EQ("if (foo) {\n" 2582 " bar();\n" 2583 "} else if (baz) {\n" 2584 " quux();\n" 2585 "}", 2586 format("if(foo){bar();}else if(baz){quux();}", Style)); 2587 EXPECT_EQ( 2588 "if (foo) {\n" 2589 " bar();\n" 2590 "} else if (baz) {\n" 2591 " quux();\n" 2592 "} else {\n" 2593 " foobar();\n" 2594 "}", 2595 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 2596 EXPECT_EQ("for (;;) {\n" 2597 " foo();\n" 2598 "}", 2599 format("for(;;){foo();}")); 2600 EXPECT_EQ("while (1) {\n" 2601 " foo();\n" 2602 "}", 2603 format("while(1){foo();}", Style)); 2604 EXPECT_EQ("switch (foo) {\n" 2605 "case bar:\n" 2606 " return;\n" 2607 "}", 2608 format("switch(foo){case bar:return;}", Style)); 2609 EXPECT_EQ("try {\n" 2610 " foo();\n" 2611 "} catch (...) {\n" 2612 " bar();\n" 2613 "}", 2614 format("try{foo();}catch(...){bar();}", Style)); 2615 EXPECT_EQ("do {\n" 2616 " foo();\n" 2617 "} while (bar &&\n" 2618 " baz);", 2619 format("do{foo();}while(bar&&baz);", Style)); 2620 // Long lines should put opening brace on new line. 2621 EXPECT_EQ("if (foo && bar &&\n" 2622 " baz)\n" 2623 "{\n" 2624 " quux();\n" 2625 "}", 2626 format("if(foo&&bar&&baz){quux();}", Style)); 2627 EXPECT_EQ("if (foo && bar &&\n" 2628 " baz)\n" 2629 "{\n" 2630 " quux();\n" 2631 "}", 2632 format("if (foo && bar &&\n" 2633 " baz) {\n" 2634 " quux();\n" 2635 "}", 2636 Style)); 2637 EXPECT_EQ("if (foo) {\n" 2638 " bar();\n" 2639 "} else if (baz ||\n" 2640 " quux)\n" 2641 "{\n" 2642 " foobar();\n" 2643 "}", 2644 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 2645 EXPECT_EQ( 2646 "if (foo) {\n" 2647 " bar();\n" 2648 "} else if (baz ||\n" 2649 " quux)\n" 2650 "{\n" 2651 " foobar();\n" 2652 "} else {\n" 2653 " barbaz();\n" 2654 "}", 2655 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2656 Style)); 2657 EXPECT_EQ("for (int i = 0;\n" 2658 " i < 10; ++i)\n" 2659 "{\n" 2660 " foo();\n" 2661 "}", 2662 format("for(int i=0;i<10;++i){foo();}", Style)); 2663 EXPECT_EQ("foreach (int i,\n" 2664 " list)\n" 2665 "{\n" 2666 " foo();\n" 2667 "}", 2668 format("foreach(int i, list){foo();}", Style)); 2669 Style.ColumnLimit = 2670 40; // to concentrate at brace wrapping, not line wrap due to column limit 2671 EXPECT_EQ("foreach (int i, list) {\n" 2672 " foo();\n" 2673 "}", 2674 format("foreach(int i, list){foo();}", Style)); 2675 Style.ColumnLimit = 2676 20; // to concentrate at brace wrapping, not line wrap due to column limit 2677 EXPECT_EQ("while (foo || bar ||\n" 2678 " baz)\n" 2679 "{\n" 2680 " quux();\n" 2681 "}", 2682 format("while(foo||bar||baz){quux();}", Style)); 2683 EXPECT_EQ("switch (\n" 2684 " foo = barbaz)\n" 2685 "{\n" 2686 "case quux:\n" 2687 " return;\n" 2688 "}", 2689 format("switch(foo=barbaz){case quux:return;}", Style)); 2690 EXPECT_EQ("try {\n" 2691 " foo();\n" 2692 "} catch (\n" 2693 " Exception &bar)\n" 2694 "{\n" 2695 " baz();\n" 2696 "}", 2697 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2698 Style.ColumnLimit = 2699 40; // to concentrate at brace wrapping, not line wrap due to column limit 2700 EXPECT_EQ("try {\n" 2701 " foo();\n" 2702 "} catch (Exception &bar) {\n" 2703 " baz();\n" 2704 "}", 2705 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2706 Style.ColumnLimit = 2707 20; // to concentrate at brace wrapping, not line wrap due to column limit 2708 2709 Style.BraceWrapping.BeforeElse = true; 2710 EXPECT_EQ( 2711 "if (foo) {\n" 2712 " bar();\n" 2713 "}\n" 2714 "else if (baz ||\n" 2715 " quux)\n" 2716 "{\n" 2717 " foobar();\n" 2718 "}\n" 2719 "else {\n" 2720 " barbaz();\n" 2721 "}", 2722 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2723 Style)); 2724 2725 Style.BraceWrapping.BeforeCatch = true; 2726 EXPECT_EQ("try {\n" 2727 " foo();\n" 2728 "}\n" 2729 "catch (...) {\n" 2730 " baz();\n" 2731 "}", 2732 format("try{foo();}catch(...){baz();}", Style)); 2733 } 2734 2735 TEST_F(FormatTest, BeforeWhile) { 2736 FormatStyle Style = getLLVMStyle(); 2737 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2738 2739 verifyFormat("do {\n" 2740 " foo();\n" 2741 "} while (1);", 2742 Style); 2743 Style.BraceWrapping.BeforeWhile = true; 2744 verifyFormat("do {\n" 2745 " foo();\n" 2746 "}\n" 2747 "while (1);", 2748 Style); 2749 } 2750 2751 //===----------------------------------------------------------------------===// 2752 // Tests for classes, namespaces, etc. 2753 //===----------------------------------------------------------------------===// 2754 2755 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 2756 verifyFormat("class A {};"); 2757 } 2758 2759 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 2760 verifyFormat("class A {\n" 2761 "public:\n" 2762 "public: // comment\n" 2763 "protected:\n" 2764 "private:\n" 2765 " void f() {}\n" 2766 "};"); 2767 verifyFormat("export class A {\n" 2768 "public:\n" 2769 "public: // comment\n" 2770 "protected:\n" 2771 "private:\n" 2772 " void f() {}\n" 2773 "};"); 2774 verifyGoogleFormat("class A {\n" 2775 " public:\n" 2776 " protected:\n" 2777 " private:\n" 2778 " void f() {}\n" 2779 "};"); 2780 verifyGoogleFormat("export class A {\n" 2781 " public:\n" 2782 " protected:\n" 2783 " private:\n" 2784 " void f() {}\n" 2785 "};"); 2786 verifyFormat("class A {\n" 2787 "public slots:\n" 2788 " void f1() {}\n" 2789 "public Q_SLOTS:\n" 2790 " void f2() {}\n" 2791 "protected slots:\n" 2792 " void f3() {}\n" 2793 "protected Q_SLOTS:\n" 2794 " void f4() {}\n" 2795 "private slots:\n" 2796 " void f5() {}\n" 2797 "private Q_SLOTS:\n" 2798 " void f6() {}\n" 2799 "signals:\n" 2800 " void g1();\n" 2801 "Q_SIGNALS:\n" 2802 " void g2();\n" 2803 "};"); 2804 2805 // Don't interpret 'signals' the wrong way. 2806 verifyFormat("signals.set();"); 2807 verifyFormat("for (Signals signals : f()) {\n}"); 2808 verifyFormat("{\n" 2809 " signals.set(); // This needs indentation.\n" 2810 "}"); 2811 verifyFormat("void f() {\n" 2812 "label:\n" 2813 " signals.baz();\n" 2814 "}"); 2815 } 2816 2817 TEST_F(FormatTest, SeparatesLogicalBlocks) { 2818 EXPECT_EQ("class A {\n" 2819 "public:\n" 2820 " void f();\n" 2821 "\n" 2822 "private:\n" 2823 " void g() {}\n" 2824 " // test\n" 2825 "protected:\n" 2826 " int h;\n" 2827 "};", 2828 format("class A {\n" 2829 "public:\n" 2830 "void f();\n" 2831 "private:\n" 2832 "void g() {}\n" 2833 "// test\n" 2834 "protected:\n" 2835 "int h;\n" 2836 "};")); 2837 EXPECT_EQ("class A {\n" 2838 "protected:\n" 2839 "public:\n" 2840 " void f();\n" 2841 "};", 2842 format("class A {\n" 2843 "protected:\n" 2844 "\n" 2845 "public:\n" 2846 "\n" 2847 " void f();\n" 2848 "};")); 2849 2850 // Even ensure proper spacing inside macros. 2851 EXPECT_EQ("#define B \\\n" 2852 " class A { \\\n" 2853 " protected: \\\n" 2854 " public: \\\n" 2855 " void f(); \\\n" 2856 " };", 2857 format("#define B \\\n" 2858 " class A { \\\n" 2859 " protected: \\\n" 2860 " \\\n" 2861 " public: \\\n" 2862 " \\\n" 2863 " void f(); \\\n" 2864 " };", 2865 getGoogleStyle())); 2866 // But don't remove empty lines after macros ending in access specifiers. 2867 EXPECT_EQ("#define A private:\n" 2868 "\n" 2869 "int i;", 2870 format("#define A private:\n" 2871 "\n" 2872 "int i;")); 2873 } 2874 2875 TEST_F(FormatTest, FormatsClasses) { 2876 verifyFormat("class A : public B {};"); 2877 verifyFormat("class A : public ::B {};"); 2878 2879 verifyFormat( 2880 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2881 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2882 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 2883 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2884 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2885 verifyFormat( 2886 "class A : public B, public C, public D, public E, public F {};"); 2887 verifyFormat("class AAAAAAAAAAAA : public B,\n" 2888 " public C,\n" 2889 " public D,\n" 2890 " public E,\n" 2891 " public F,\n" 2892 " public G {};"); 2893 2894 verifyFormat("class\n" 2895 " ReallyReallyLongClassName {\n" 2896 " int i;\n" 2897 "};", 2898 getLLVMStyleWithColumns(32)); 2899 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2900 " aaaaaaaaaaaaaaaa> {};"); 2901 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 2902 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 2903 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 2904 verifyFormat("template <class R, class C>\n" 2905 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 2906 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 2907 verifyFormat("class ::A::B {};"); 2908 } 2909 2910 TEST_F(FormatTest, BreakInheritanceStyle) { 2911 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 2912 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 2913 FormatStyle::BILS_BeforeComma; 2914 verifyFormat("class MyClass : public X {};", 2915 StyleWithInheritanceBreakBeforeComma); 2916 verifyFormat("class MyClass\n" 2917 " : public X\n" 2918 " , public Y {};", 2919 StyleWithInheritanceBreakBeforeComma); 2920 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 2921 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 2922 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 2923 StyleWithInheritanceBreakBeforeComma); 2924 verifyFormat("struct aaaaaaaaaaaaa\n" 2925 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 2926 " aaaaaaaaaaaaaaaa> {};", 2927 StyleWithInheritanceBreakBeforeComma); 2928 2929 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 2930 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 2931 FormatStyle::BILS_AfterColon; 2932 verifyFormat("class MyClass : public X {};", 2933 StyleWithInheritanceBreakAfterColon); 2934 verifyFormat("class MyClass : public X, public Y {};", 2935 StyleWithInheritanceBreakAfterColon); 2936 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 2937 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2938 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 2939 StyleWithInheritanceBreakAfterColon); 2940 verifyFormat("struct aaaaaaaaaaaaa :\n" 2941 " public aaaaaaaaaaaaaaaaaaa< // break\n" 2942 " aaaaaaaaaaaaaaaa> {};", 2943 StyleWithInheritanceBreakAfterColon); 2944 2945 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 2946 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 2947 FormatStyle::BILS_AfterComma; 2948 verifyFormat("class MyClass : public X {};", 2949 StyleWithInheritanceBreakAfterComma); 2950 verifyFormat("class MyClass : public X,\n" 2951 " public Y {};", 2952 StyleWithInheritanceBreakAfterComma); 2953 verifyFormat( 2954 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2955 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 2956 "{};", 2957 StyleWithInheritanceBreakAfterComma); 2958 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2959 " aaaaaaaaaaaaaaaa> {};", 2960 StyleWithInheritanceBreakAfterComma); 2961 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 2962 " : public OnceBreak,\n" 2963 " public AlwaysBreak,\n" 2964 " EvenBasesFitInOneLine {};", 2965 StyleWithInheritanceBreakAfterComma); 2966 } 2967 2968 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 2969 verifyFormat("class A {\n} a, b;"); 2970 verifyFormat("struct A {\n} a, b;"); 2971 verifyFormat("union A {\n} a;"); 2972 } 2973 2974 TEST_F(FormatTest, FormatsEnum) { 2975 verifyFormat("enum {\n" 2976 " Zero,\n" 2977 " One = 1,\n" 2978 " Two = One + 1,\n" 2979 " Three = (One + Two),\n" 2980 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2981 " Five = (One, Two, Three, Four, 5)\n" 2982 "};"); 2983 verifyGoogleFormat("enum {\n" 2984 " Zero,\n" 2985 " One = 1,\n" 2986 " Two = One + 1,\n" 2987 " Three = (One + Two),\n" 2988 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2989 " Five = (One, Two, Three, Four, 5)\n" 2990 "};"); 2991 verifyFormat("enum Enum {};"); 2992 verifyFormat("enum {};"); 2993 verifyFormat("enum X E {} d;"); 2994 verifyFormat("enum __attribute__((...)) E {} d;"); 2995 verifyFormat("enum __declspec__((...)) E {} d;"); 2996 verifyFormat("enum {\n" 2997 " Bar = Foo<int, int>::value\n" 2998 "};", 2999 getLLVMStyleWithColumns(30)); 3000 3001 verifyFormat("enum ShortEnum { A, B, C };"); 3002 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 3003 3004 EXPECT_EQ("enum KeepEmptyLines {\n" 3005 " ONE,\n" 3006 "\n" 3007 " TWO,\n" 3008 "\n" 3009 " THREE\n" 3010 "}", 3011 format("enum KeepEmptyLines {\n" 3012 " ONE,\n" 3013 "\n" 3014 " TWO,\n" 3015 "\n" 3016 "\n" 3017 " THREE\n" 3018 "}")); 3019 verifyFormat("enum E { // comment\n" 3020 " ONE,\n" 3021 " TWO\n" 3022 "};\n" 3023 "int i;"); 3024 3025 FormatStyle EightIndent = getLLVMStyle(); 3026 EightIndent.IndentWidth = 8; 3027 verifyFormat("enum {\n" 3028 " VOID,\n" 3029 " CHAR,\n" 3030 " SHORT,\n" 3031 " INT,\n" 3032 " LONG,\n" 3033 " SIGNED,\n" 3034 " UNSIGNED,\n" 3035 " BOOL,\n" 3036 " FLOAT,\n" 3037 " DOUBLE,\n" 3038 " COMPLEX\n" 3039 "};", 3040 EightIndent); 3041 3042 // Not enums. 3043 verifyFormat("enum X f() {\n" 3044 " a();\n" 3045 " return 42;\n" 3046 "}"); 3047 verifyFormat("enum X Type::f() {\n" 3048 " a();\n" 3049 " return 42;\n" 3050 "}"); 3051 verifyFormat("enum ::X f() {\n" 3052 " a();\n" 3053 " return 42;\n" 3054 "}"); 3055 verifyFormat("enum ns::X f() {\n" 3056 " a();\n" 3057 " return 42;\n" 3058 "}"); 3059 } 3060 3061 TEST_F(FormatTest, FormatsEnumsWithErrors) { 3062 verifyFormat("enum Type {\n" 3063 " One = 0; // These semicolons should be commas.\n" 3064 " Two = 1;\n" 3065 "};"); 3066 verifyFormat("namespace n {\n" 3067 "enum Type {\n" 3068 " One,\n" 3069 " Two, // missing };\n" 3070 " int i;\n" 3071 "}\n" 3072 "void g() {}"); 3073 } 3074 3075 TEST_F(FormatTest, FormatsEnumStruct) { 3076 verifyFormat("enum struct {\n" 3077 " Zero,\n" 3078 " One = 1,\n" 3079 " Two = One + 1,\n" 3080 " Three = (One + Two),\n" 3081 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3082 " Five = (One, Two, Three, Four, 5)\n" 3083 "};"); 3084 verifyFormat("enum struct Enum {};"); 3085 verifyFormat("enum struct {};"); 3086 verifyFormat("enum struct X E {} d;"); 3087 verifyFormat("enum struct __attribute__((...)) E {} d;"); 3088 verifyFormat("enum struct __declspec__((...)) E {} d;"); 3089 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 3090 } 3091 3092 TEST_F(FormatTest, FormatsEnumClass) { 3093 verifyFormat("enum class {\n" 3094 " Zero,\n" 3095 " One = 1,\n" 3096 " Two = One + 1,\n" 3097 " Three = (One + Two),\n" 3098 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3099 " Five = (One, Two, Three, Four, 5)\n" 3100 "};"); 3101 verifyFormat("enum class Enum {};"); 3102 verifyFormat("enum class {};"); 3103 verifyFormat("enum class X E {} d;"); 3104 verifyFormat("enum class __attribute__((...)) E {} d;"); 3105 verifyFormat("enum class __declspec__((...)) E {} d;"); 3106 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 3107 } 3108 3109 TEST_F(FormatTest, FormatsEnumTypes) { 3110 verifyFormat("enum X : int {\n" 3111 " A, // Force multiple lines.\n" 3112 " B\n" 3113 "};"); 3114 verifyFormat("enum X : int { A, B };"); 3115 verifyFormat("enum X : std::uint32_t { A, B };"); 3116 } 3117 3118 TEST_F(FormatTest, FormatsTypedefEnum) { 3119 FormatStyle Style = getLLVMStyle(); 3120 Style.ColumnLimit = 40; 3121 verifyFormat("typedef enum {} EmptyEnum;"); 3122 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3123 verifyFormat("typedef enum {\n" 3124 " ZERO = 0,\n" 3125 " ONE = 1,\n" 3126 " TWO = 2,\n" 3127 " THREE = 3\n" 3128 "} LongEnum;", 3129 Style); 3130 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3131 Style.BraceWrapping.AfterEnum = true; 3132 verifyFormat("typedef enum {} EmptyEnum;"); 3133 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3134 verifyFormat("typedef enum\n" 3135 "{\n" 3136 " ZERO = 0,\n" 3137 " ONE = 1,\n" 3138 " TWO = 2,\n" 3139 " THREE = 3\n" 3140 "} LongEnum;", 3141 Style); 3142 } 3143 3144 TEST_F(FormatTest, FormatsNSEnums) { 3145 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3146 verifyGoogleFormat( 3147 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3148 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 3149 " // Information about someDecentlyLongValue.\n" 3150 " someDecentlyLongValue,\n" 3151 " // Information about anotherDecentlyLongValue.\n" 3152 " anotherDecentlyLongValue,\n" 3153 " // Information about aThirdDecentlyLongValue.\n" 3154 " aThirdDecentlyLongValue\n" 3155 "};"); 3156 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 3157 " // Information about someDecentlyLongValue.\n" 3158 " someDecentlyLongValue,\n" 3159 " // Information about anotherDecentlyLongValue.\n" 3160 " anotherDecentlyLongValue,\n" 3161 " // Information about aThirdDecentlyLongValue.\n" 3162 " aThirdDecentlyLongValue\n" 3163 "};"); 3164 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 3165 " a = 1,\n" 3166 " b = 2,\n" 3167 " c = 3,\n" 3168 "};"); 3169 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 3170 " a = 1,\n" 3171 " b = 2,\n" 3172 " c = 3,\n" 3173 "};"); 3174 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 3175 " a = 1,\n" 3176 " b = 2,\n" 3177 " c = 3,\n" 3178 "};"); 3179 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 3180 " a = 1,\n" 3181 " b = 2,\n" 3182 " c = 3,\n" 3183 "};"); 3184 } 3185 3186 TEST_F(FormatTest, FormatsBitfields) { 3187 verifyFormat("struct Bitfields {\n" 3188 " unsigned sClass : 8;\n" 3189 " unsigned ValueKind : 2;\n" 3190 "};"); 3191 verifyFormat("struct A {\n" 3192 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 3193 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 3194 "};"); 3195 verifyFormat("struct MyStruct {\n" 3196 " uchar data;\n" 3197 " uchar : 8;\n" 3198 " uchar : 8;\n" 3199 " uchar other;\n" 3200 "};"); 3201 FormatStyle Style = getLLVMStyle(); 3202 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 3203 verifyFormat("struct Bitfields {\n" 3204 " unsigned sClass:8;\n" 3205 " unsigned ValueKind:2;\n" 3206 " uchar other;\n" 3207 "};", 3208 Style); 3209 verifyFormat("struct A {\n" 3210 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 3211 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 3212 "};", 3213 Style); 3214 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 3215 verifyFormat("struct Bitfields {\n" 3216 " unsigned sClass :8;\n" 3217 " unsigned ValueKind :2;\n" 3218 " uchar other;\n" 3219 "};", 3220 Style); 3221 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 3222 verifyFormat("struct Bitfields {\n" 3223 " unsigned sClass: 8;\n" 3224 " unsigned ValueKind: 2;\n" 3225 " uchar other;\n" 3226 "};", 3227 Style); 3228 } 3229 3230 TEST_F(FormatTest, FormatsNamespaces) { 3231 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 3232 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 3233 3234 verifyFormat("namespace some_namespace {\n" 3235 "class A {};\n" 3236 "void f() { f(); }\n" 3237 "}", 3238 LLVMWithNoNamespaceFix); 3239 verifyFormat("namespace N::inline D {\n" 3240 "class A {};\n" 3241 "void f() { f(); }\n" 3242 "}", 3243 LLVMWithNoNamespaceFix); 3244 verifyFormat("namespace N::inline D::E {\n" 3245 "class A {};\n" 3246 "void f() { f(); }\n" 3247 "}", 3248 LLVMWithNoNamespaceFix); 3249 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 3250 "class A {};\n" 3251 "void f() { f(); }\n" 3252 "}", 3253 LLVMWithNoNamespaceFix); 3254 verifyFormat("/* something */ namespace some_namespace {\n" 3255 "class A {};\n" 3256 "void f() { f(); }\n" 3257 "}", 3258 LLVMWithNoNamespaceFix); 3259 verifyFormat("namespace {\n" 3260 "class A {};\n" 3261 "void f() { f(); }\n" 3262 "}", 3263 LLVMWithNoNamespaceFix); 3264 verifyFormat("/* something */ namespace {\n" 3265 "class A {};\n" 3266 "void f() { f(); }\n" 3267 "}", 3268 LLVMWithNoNamespaceFix); 3269 verifyFormat("inline namespace X {\n" 3270 "class A {};\n" 3271 "void f() { f(); }\n" 3272 "}", 3273 LLVMWithNoNamespaceFix); 3274 verifyFormat("/* something */ inline namespace X {\n" 3275 "class A {};\n" 3276 "void f() { f(); }\n" 3277 "}", 3278 LLVMWithNoNamespaceFix); 3279 verifyFormat("export namespace X {\n" 3280 "class A {};\n" 3281 "void f() { f(); }\n" 3282 "}", 3283 LLVMWithNoNamespaceFix); 3284 verifyFormat("using namespace some_namespace;\n" 3285 "class A {};\n" 3286 "void f() { f(); }", 3287 LLVMWithNoNamespaceFix); 3288 3289 // This code is more common than we thought; if we 3290 // layout this correctly the semicolon will go into 3291 // its own line, which is undesirable. 3292 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 3293 verifyFormat("namespace {\n" 3294 "class A {};\n" 3295 "};", 3296 LLVMWithNoNamespaceFix); 3297 3298 verifyFormat("namespace {\n" 3299 "int SomeVariable = 0; // comment\n" 3300 "} // namespace", 3301 LLVMWithNoNamespaceFix); 3302 EXPECT_EQ("#ifndef HEADER_GUARD\n" 3303 "#define HEADER_GUARD\n" 3304 "namespace my_namespace {\n" 3305 "int i;\n" 3306 "} // my_namespace\n" 3307 "#endif // HEADER_GUARD", 3308 format("#ifndef HEADER_GUARD\n" 3309 " #define HEADER_GUARD\n" 3310 " namespace my_namespace {\n" 3311 "int i;\n" 3312 "} // my_namespace\n" 3313 "#endif // HEADER_GUARD", 3314 LLVMWithNoNamespaceFix)); 3315 3316 EXPECT_EQ("namespace A::B {\n" 3317 "class C {};\n" 3318 "}", 3319 format("namespace A::B {\n" 3320 "class C {};\n" 3321 "}", 3322 LLVMWithNoNamespaceFix)); 3323 3324 FormatStyle Style = getLLVMStyle(); 3325 Style.NamespaceIndentation = FormatStyle::NI_All; 3326 EXPECT_EQ("namespace out {\n" 3327 " int i;\n" 3328 " namespace in {\n" 3329 " int i;\n" 3330 " } // namespace in\n" 3331 "} // namespace out", 3332 format("namespace out {\n" 3333 "int i;\n" 3334 "namespace in {\n" 3335 "int i;\n" 3336 "} // namespace in\n" 3337 "} // namespace out", 3338 Style)); 3339 3340 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3341 EXPECT_EQ("namespace out {\n" 3342 "int i;\n" 3343 "namespace in {\n" 3344 " int i;\n" 3345 "} // namespace in\n" 3346 "} // namespace out", 3347 format("namespace out {\n" 3348 "int i;\n" 3349 "namespace in {\n" 3350 "int i;\n" 3351 "} // namespace in\n" 3352 "} // namespace out", 3353 Style)); 3354 } 3355 3356 TEST_F(FormatTest, NamespaceMacros) { 3357 FormatStyle Style = getLLVMStyle(); 3358 Style.NamespaceMacros.push_back("TESTSUITE"); 3359 3360 verifyFormat("TESTSUITE(A) {\n" 3361 "int foo();\n" 3362 "} // TESTSUITE(A)", 3363 Style); 3364 3365 verifyFormat("TESTSUITE(A, B) {\n" 3366 "int foo();\n" 3367 "} // TESTSUITE(A)", 3368 Style); 3369 3370 // Properly indent according to NamespaceIndentation style 3371 Style.NamespaceIndentation = FormatStyle::NI_All; 3372 verifyFormat("TESTSUITE(A) {\n" 3373 " int foo();\n" 3374 "} // TESTSUITE(A)", 3375 Style); 3376 verifyFormat("TESTSUITE(A) {\n" 3377 " namespace B {\n" 3378 " int foo();\n" 3379 " } // namespace B\n" 3380 "} // TESTSUITE(A)", 3381 Style); 3382 verifyFormat("namespace A {\n" 3383 " TESTSUITE(B) {\n" 3384 " int foo();\n" 3385 " } // TESTSUITE(B)\n" 3386 "} // namespace A", 3387 Style); 3388 3389 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3390 verifyFormat("TESTSUITE(A) {\n" 3391 "TESTSUITE(B) {\n" 3392 " int foo();\n" 3393 "} // TESTSUITE(B)\n" 3394 "} // TESTSUITE(A)", 3395 Style); 3396 verifyFormat("TESTSUITE(A) {\n" 3397 "namespace B {\n" 3398 " int foo();\n" 3399 "} // namespace B\n" 3400 "} // TESTSUITE(A)", 3401 Style); 3402 verifyFormat("namespace A {\n" 3403 "TESTSUITE(B) {\n" 3404 " int foo();\n" 3405 "} // TESTSUITE(B)\n" 3406 "} // namespace A", 3407 Style); 3408 3409 // Properly merge namespace-macros blocks in CompactNamespaces mode 3410 Style.NamespaceIndentation = FormatStyle::NI_None; 3411 Style.CompactNamespaces = true; 3412 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 3413 "}} // TESTSUITE(A::B)", 3414 Style); 3415 3416 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3417 "}} // TESTSUITE(out::in)", 3418 format("TESTSUITE(out) {\n" 3419 "TESTSUITE(in) {\n" 3420 "} // TESTSUITE(in)\n" 3421 "} // TESTSUITE(out)", 3422 Style)); 3423 3424 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3425 "}} // TESTSUITE(out::in)", 3426 format("TESTSUITE(out) {\n" 3427 "TESTSUITE(in) {\n" 3428 "} // TESTSUITE(in)\n" 3429 "} // TESTSUITE(out)", 3430 Style)); 3431 3432 // Do not merge different namespaces/macros 3433 EXPECT_EQ("namespace out {\n" 3434 "TESTSUITE(in) {\n" 3435 "} // TESTSUITE(in)\n" 3436 "} // namespace out", 3437 format("namespace out {\n" 3438 "TESTSUITE(in) {\n" 3439 "} // TESTSUITE(in)\n" 3440 "} // namespace out", 3441 Style)); 3442 EXPECT_EQ("TESTSUITE(out) {\n" 3443 "namespace in {\n" 3444 "} // namespace in\n" 3445 "} // TESTSUITE(out)", 3446 format("TESTSUITE(out) {\n" 3447 "namespace in {\n" 3448 "} // namespace in\n" 3449 "} // TESTSUITE(out)", 3450 Style)); 3451 Style.NamespaceMacros.push_back("FOOBAR"); 3452 EXPECT_EQ("TESTSUITE(out) {\n" 3453 "FOOBAR(in) {\n" 3454 "} // FOOBAR(in)\n" 3455 "} // TESTSUITE(out)", 3456 format("TESTSUITE(out) {\n" 3457 "FOOBAR(in) {\n" 3458 "} // FOOBAR(in)\n" 3459 "} // TESTSUITE(out)", 3460 Style)); 3461 } 3462 3463 TEST_F(FormatTest, FormatsCompactNamespaces) { 3464 FormatStyle Style = getLLVMStyle(); 3465 Style.CompactNamespaces = true; 3466 Style.NamespaceMacros.push_back("TESTSUITE"); 3467 3468 verifyFormat("namespace A { namespace B {\n" 3469 "}} // namespace A::B", 3470 Style); 3471 3472 EXPECT_EQ("namespace out { namespace in {\n" 3473 "}} // namespace out::in", 3474 format("namespace out {\n" 3475 "namespace in {\n" 3476 "} // namespace in\n" 3477 "} // namespace out", 3478 Style)); 3479 3480 // Only namespaces which have both consecutive opening and end get compacted 3481 EXPECT_EQ("namespace out {\n" 3482 "namespace in1 {\n" 3483 "} // namespace in1\n" 3484 "namespace in2 {\n" 3485 "} // namespace in2\n" 3486 "} // namespace out", 3487 format("namespace out {\n" 3488 "namespace in1 {\n" 3489 "} // namespace in1\n" 3490 "namespace in2 {\n" 3491 "} // namespace in2\n" 3492 "} // namespace out", 3493 Style)); 3494 3495 EXPECT_EQ("namespace out {\n" 3496 "int i;\n" 3497 "namespace in {\n" 3498 "int j;\n" 3499 "} // namespace in\n" 3500 "int k;\n" 3501 "} // namespace out", 3502 format("namespace out { int i;\n" 3503 "namespace in { int j; } // namespace in\n" 3504 "int k; } // namespace out", 3505 Style)); 3506 3507 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 3508 "}}} // namespace A::B::C\n", 3509 format("namespace A { namespace B {\n" 3510 "namespace C {\n" 3511 "}} // namespace B::C\n" 3512 "} // namespace A\n", 3513 Style)); 3514 3515 Style.ColumnLimit = 40; 3516 EXPECT_EQ("namespace aaaaaaaaaa {\n" 3517 "namespace bbbbbbbbbb {\n" 3518 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 3519 format("namespace aaaaaaaaaa {\n" 3520 "namespace bbbbbbbbbb {\n" 3521 "} // namespace bbbbbbbbbb\n" 3522 "} // namespace aaaaaaaaaa", 3523 Style)); 3524 3525 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 3526 "namespace cccccc {\n" 3527 "}}} // namespace aaaaaa::bbbbbb::cccccc", 3528 format("namespace aaaaaa {\n" 3529 "namespace bbbbbb {\n" 3530 "namespace cccccc {\n" 3531 "} // namespace cccccc\n" 3532 "} // namespace bbbbbb\n" 3533 "} // namespace aaaaaa", 3534 Style)); 3535 Style.ColumnLimit = 80; 3536 3537 // Extra semicolon after 'inner' closing brace prevents merging 3538 EXPECT_EQ("namespace out { namespace in {\n" 3539 "}; } // namespace out::in", 3540 format("namespace out {\n" 3541 "namespace in {\n" 3542 "}; // namespace in\n" 3543 "} // namespace out", 3544 Style)); 3545 3546 // Extra semicolon after 'outer' closing brace is conserved 3547 EXPECT_EQ("namespace out { namespace in {\n" 3548 "}}; // namespace out::in", 3549 format("namespace out {\n" 3550 "namespace in {\n" 3551 "} // namespace in\n" 3552 "}; // namespace out", 3553 Style)); 3554 3555 Style.NamespaceIndentation = FormatStyle::NI_All; 3556 EXPECT_EQ("namespace out { namespace in {\n" 3557 " int i;\n" 3558 "}} // namespace out::in", 3559 format("namespace out {\n" 3560 "namespace in {\n" 3561 "int i;\n" 3562 "} // namespace in\n" 3563 "} // namespace out", 3564 Style)); 3565 EXPECT_EQ("namespace out { namespace mid {\n" 3566 " namespace in {\n" 3567 " int j;\n" 3568 " } // namespace in\n" 3569 " int k;\n" 3570 "}} // namespace out::mid", 3571 format("namespace out { namespace mid {\n" 3572 "namespace in { int j; } // namespace in\n" 3573 "int k; }} // namespace out::mid", 3574 Style)); 3575 3576 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3577 EXPECT_EQ("namespace out { namespace in {\n" 3578 " int i;\n" 3579 "}} // namespace out::in", 3580 format("namespace out {\n" 3581 "namespace in {\n" 3582 "int i;\n" 3583 "} // namespace in\n" 3584 "} // namespace out", 3585 Style)); 3586 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 3587 " int i;\n" 3588 "}}} // namespace out::mid::in", 3589 format("namespace out {\n" 3590 "namespace mid {\n" 3591 "namespace in {\n" 3592 "int i;\n" 3593 "} // namespace in\n" 3594 "} // namespace mid\n" 3595 "} // namespace out", 3596 Style)); 3597 } 3598 3599 TEST_F(FormatTest, FormatsExternC) { 3600 verifyFormat("extern \"C\" {\nint a;"); 3601 verifyFormat("extern \"C\" {}"); 3602 verifyFormat("extern \"C\" {\n" 3603 "int foo();\n" 3604 "}"); 3605 verifyFormat("extern \"C\" int foo() {}"); 3606 verifyFormat("extern \"C\" int foo();"); 3607 verifyFormat("extern \"C\" int foo() {\n" 3608 " int i = 42;\n" 3609 " return i;\n" 3610 "}"); 3611 3612 FormatStyle Style = getLLVMStyle(); 3613 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3614 Style.BraceWrapping.AfterFunction = true; 3615 verifyFormat("extern \"C\" int foo() {}", Style); 3616 verifyFormat("extern \"C\" int foo();", Style); 3617 verifyFormat("extern \"C\" int foo()\n" 3618 "{\n" 3619 " int i = 42;\n" 3620 " return i;\n" 3621 "}", 3622 Style); 3623 3624 Style.BraceWrapping.AfterExternBlock = true; 3625 Style.BraceWrapping.SplitEmptyRecord = false; 3626 verifyFormat("extern \"C\"\n" 3627 "{}", 3628 Style); 3629 verifyFormat("extern \"C\"\n" 3630 "{\n" 3631 " int foo();\n" 3632 "}", 3633 Style); 3634 } 3635 3636 TEST_F(FormatTest, IndentExternBlockStyle) { 3637 FormatStyle Style = getLLVMStyle(); 3638 Style.IndentWidth = 2; 3639 3640 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 3641 verifyFormat("extern \"C\" { /*9*/\n}", Style); 3642 verifyFormat("extern \"C\" {\n" 3643 " int foo10();\n" 3644 "}", 3645 Style); 3646 3647 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 3648 verifyFormat("extern \"C\" { /*11*/\n}", Style); 3649 verifyFormat("extern \"C\" {\n" 3650 "int foo12();\n" 3651 "}", 3652 Style); 3653 3654 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 3655 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3656 Style.BraceWrapping.AfterExternBlock = true; 3657 verifyFormat("extern \"C\"\n{ /*13*/\n}", Style); 3658 verifyFormat("extern \"C\"\n{\n" 3659 " int foo14();\n" 3660 "}", 3661 Style); 3662 3663 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 3664 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3665 Style.BraceWrapping.AfterExternBlock = false; 3666 verifyFormat("extern \"C\" { /*15*/\n}", Style); 3667 verifyFormat("extern \"C\" {\n" 3668 "int foo16();\n" 3669 "}", 3670 Style); 3671 } 3672 3673 TEST_F(FormatTest, FormatsInlineASM) { 3674 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 3675 verifyFormat("asm(\"nop\" ::: \"memory\");"); 3676 verifyFormat( 3677 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 3678 " \"cpuid\\n\\t\"\n" 3679 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 3680 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 3681 " : \"a\"(value));"); 3682 EXPECT_EQ( 3683 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3684 " __asm {\n" 3685 " mov edx,[that] // vtable in edx\n" 3686 " mov eax,methodIndex\n" 3687 " call [edx][eax*4] // stdcall\n" 3688 " }\n" 3689 "}", 3690 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3691 " __asm {\n" 3692 " mov edx,[that] // vtable in edx\n" 3693 " mov eax,methodIndex\n" 3694 " call [edx][eax*4] // stdcall\n" 3695 " }\n" 3696 "}")); 3697 EXPECT_EQ("_asm {\n" 3698 " xor eax, eax;\n" 3699 " cpuid;\n" 3700 "}", 3701 format("_asm {\n" 3702 " xor eax, eax;\n" 3703 " cpuid;\n" 3704 "}")); 3705 verifyFormat("void function() {\n" 3706 " // comment\n" 3707 " asm(\"\");\n" 3708 "}"); 3709 EXPECT_EQ("__asm {\n" 3710 "}\n" 3711 "int i;", 3712 format("__asm {\n" 3713 "}\n" 3714 "int i;")); 3715 } 3716 3717 TEST_F(FormatTest, FormatTryCatch) { 3718 verifyFormat("try {\n" 3719 " throw a * b;\n" 3720 "} catch (int a) {\n" 3721 " // Do nothing.\n" 3722 "} catch (...) {\n" 3723 " exit(42);\n" 3724 "}"); 3725 3726 // Function-level try statements. 3727 verifyFormat("int f() try { return 4; } catch (...) {\n" 3728 " return 5;\n" 3729 "}"); 3730 verifyFormat("class A {\n" 3731 " int a;\n" 3732 " A() try : a(0) {\n" 3733 " } catch (...) {\n" 3734 " throw;\n" 3735 " }\n" 3736 "};\n"); 3737 verifyFormat("class A {\n" 3738 " int a;\n" 3739 " A() try : a(0), b{1} {\n" 3740 " } catch (...) {\n" 3741 " throw;\n" 3742 " }\n" 3743 "};\n"); 3744 verifyFormat("class A {\n" 3745 " int a;\n" 3746 " A() try : a(0), b{1}, c{2} {\n" 3747 " } catch (...) {\n" 3748 " throw;\n" 3749 " }\n" 3750 "};\n"); 3751 verifyFormat("class A {\n" 3752 " int a;\n" 3753 " A() try : a(0), b{1}, c{2} {\n" 3754 " { // New scope.\n" 3755 " }\n" 3756 " } catch (...) {\n" 3757 " throw;\n" 3758 " }\n" 3759 "};\n"); 3760 3761 // Incomplete try-catch blocks. 3762 verifyIncompleteFormat("try {} catch ("); 3763 } 3764 3765 TEST_F(FormatTest, FormatTryAsAVariable) { 3766 verifyFormat("int try;"); 3767 verifyFormat("int try, size;"); 3768 verifyFormat("try = foo();"); 3769 verifyFormat("if (try < size) {\n return true;\n}"); 3770 3771 verifyFormat("int catch;"); 3772 verifyFormat("int catch, size;"); 3773 verifyFormat("catch = foo();"); 3774 verifyFormat("if (catch < size) {\n return true;\n}"); 3775 3776 FormatStyle Style = getLLVMStyle(); 3777 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3778 Style.BraceWrapping.AfterFunction = true; 3779 Style.BraceWrapping.BeforeCatch = true; 3780 verifyFormat("try {\n" 3781 " int bar = 1;\n" 3782 "}\n" 3783 "catch (...) {\n" 3784 " int bar = 1;\n" 3785 "}", 3786 Style); 3787 verifyFormat("#if NO_EX\n" 3788 "try\n" 3789 "#endif\n" 3790 "{\n" 3791 "}\n" 3792 "#if NO_EX\n" 3793 "catch (...) {\n" 3794 "}", 3795 Style); 3796 verifyFormat("try /* abc */ {\n" 3797 " int bar = 1;\n" 3798 "}\n" 3799 "catch (...) {\n" 3800 " int bar = 1;\n" 3801 "}", 3802 Style); 3803 verifyFormat("try\n" 3804 "// abc\n" 3805 "{\n" 3806 " int bar = 1;\n" 3807 "}\n" 3808 "catch (...) {\n" 3809 " int bar = 1;\n" 3810 "}", 3811 Style); 3812 } 3813 3814 TEST_F(FormatTest, FormatSEHTryCatch) { 3815 verifyFormat("__try {\n" 3816 " int a = b * c;\n" 3817 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 3818 " // Do nothing.\n" 3819 "}"); 3820 3821 verifyFormat("__try {\n" 3822 " int a = b * c;\n" 3823 "} __finally {\n" 3824 " // Do nothing.\n" 3825 "}"); 3826 3827 verifyFormat("DEBUG({\n" 3828 " __try {\n" 3829 " } __finally {\n" 3830 " }\n" 3831 "});\n"); 3832 } 3833 3834 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 3835 verifyFormat("try {\n" 3836 " f();\n" 3837 "} catch {\n" 3838 " g();\n" 3839 "}"); 3840 verifyFormat("try {\n" 3841 " f();\n" 3842 "} catch (A a) MACRO(x) {\n" 3843 " g();\n" 3844 "} catch (B b) MACRO(x) {\n" 3845 " g();\n" 3846 "}"); 3847 } 3848 3849 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 3850 FormatStyle Style = getLLVMStyle(); 3851 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 3852 FormatStyle::BS_WebKit}) { 3853 Style.BreakBeforeBraces = BraceStyle; 3854 verifyFormat("try {\n" 3855 " // something\n" 3856 "} catch (...) {\n" 3857 " // something\n" 3858 "}", 3859 Style); 3860 } 3861 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 3862 verifyFormat("try {\n" 3863 " // something\n" 3864 "}\n" 3865 "catch (...) {\n" 3866 " // something\n" 3867 "}", 3868 Style); 3869 verifyFormat("__try {\n" 3870 " // something\n" 3871 "}\n" 3872 "__finally {\n" 3873 " // something\n" 3874 "}", 3875 Style); 3876 verifyFormat("@try {\n" 3877 " // something\n" 3878 "}\n" 3879 "@finally {\n" 3880 " // something\n" 3881 "}", 3882 Style); 3883 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3884 verifyFormat("try\n" 3885 "{\n" 3886 " // something\n" 3887 "}\n" 3888 "catch (...)\n" 3889 "{\n" 3890 " // something\n" 3891 "}", 3892 Style); 3893 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 3894 verifyFormat("try\n" 3895 " {\n" 3896 " // something white\n" 3897 " }\n" 3898 "catch (...)\n" 3899 " {\n" 3900 " // something white\n" 3901 " }", 3902 Style); 3903 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 3904 verifyFormat("try\n" 3905 " {\n" 3906 " // something\n" 3907 " }\n" 3908 "catch (...)\n" 3909 " {\n" 3910 " // something\n" 3911 " }", 3912 Style); 3913 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3914 Style.BraceWrapping.BeforeCatch = true; 3915 verifyFormat("try {\n" 3916 " // something\n" 3917 "}\n" 3918 "catch (...) {\n" 3919 " // something\n" 3920 "}", 3921 Style); 3922 } 3923 3924 TEST_F(FormatTest, StaticInitializers) { 3925 verifyFormat("static SomeClass SC = {1, 'a'};"); 3926 3927 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 3928 " 100000000, " 3929 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 3930 3931 // Here, everything other than the "}" would fit on a line. 3932 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 3933 " 10000000000000000000000000};"); 3934 EXPECT_EQ("S s = {a,\n" 3935 "\n" 3936 " b};", 3937 format("S s = {\n" 3938 " a,\n" 3939 "\n" 3940 " b\n" 3941 "};")); 3942 3943 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 3944 // line. However, the formatting looks a bit off and this probably doesn't 3945 // happen often in practice. 3946 verifyFormat("static int Variable[1] = {\n" 3947 " {1000000000000000000000000000000000000}};", 3948 getLLVMStyleWithColumns(40)); 3949 } 3950 3951 TEST_F(FormatTest, DesignatedInitializers) { 3952 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 3953 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 3954 " .bbbbbbbbbb = 2,\n" 3955 " .cccccccccc = 3,\n" 3956 " .dddddddddd = 4,\n" 3957 " .eeeeeeeeee = 5};"); 3958 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 3959 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 3960 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 3961 " .ccccccccccccccccccccccccccc = 3,\n" 3962 " .ddddddddddddddddddddddddddd = 4,\n" 3963 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 3964 3965 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 3966 3967 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 3968 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 3969 " [2] = bbbbbbbbbb,\n" 3970 " [3] = cccccccccc,\n" 3971 " [4] = dddddddddd,\n" 3972 " [5] = eeeeeeeeee};"); 3973 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 3974 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3975 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3976 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 3977 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 3978 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 3979 } 3980 3981 TEST_F(FormatTest, NestedStaticInitializers) { 3982 verifyFormat("static A x = {{{}}};\n"); 3983 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 3984 " {init1, init2, init3, init4}}};", 3985 getLLVMStyleWithColumns(50)); 3986 3987 verifyFormat("somes Status::global_reps[3] = {\n" 3988 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 3989 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 3990 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 3991 getLLVMStyleWithColumns(60)); 3992 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 3993 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 3994 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 3995 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 3996 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 3997 " {rect.fRight - rect.fLeft, rect.fBottom - " 3998 "rect.fTop}};"); 3999 4000 verifyFormat( 4001 "SomeArrayOfSomeType a = {\n" 4002 " {{1, 2, 3},\n" 4003 " {1, 2, 3},\n" 4004 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 4005 " 333333333333333333333333333333},\n" 4006 " {1, 2, 3},\n" 4007 " {1, 2, 3}}};"); 4008 verifyFormat( 4009 "SomeArrayOfSomeType a = {\n" 4010 " {{1, 2, 3}},\n" 4011 " {{1, 2, 3}},\n" 4012 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 4013 " 333333333333333333333333333333}},\n" 4014 " {{1, 2, 3}},\n" 4015 " {{1, 2, 3}}};"); 4016 4017 verifyFormat("struct {\n" 4018 " unsigned bit;\n" 4019 " const char *const name;\n" 4020 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 4021 " {kOsWin, \"Windows\"},\n" 4022 " {kOsLinux, \"Linux\"},\n" 4023 " {kOsCrOS, \"Chrome OS\"}};"); 4024 verifyFormat("struct {\n" 4025 " unsigned bit;\n" 4026 " const char *const name;\n" 4027 "} kBitsToOs[] = {\n" 4028 " {kOsMac, \"Mac\"},\n" 4029 " {kOsWin, \"Windows\"},\n" 4030 " {kOsLinux, \"Linux\"},\n" 4031 " {kOsCrOS, \"Chrome OS\"},\n" 4032 "};"); 4033 } 4034 4035 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 4036 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4037 " \\\n" 4038 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 4039 } 4040 4041 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 4042 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 4043 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 4044 4045 // Do break defaulted and deleted functions. 4046 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4047 " default;", 4048 getLLVMStyleWithColumns(40)); 4049 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4050 " delete;", 4051 getLLVMStyleWithColumns(40)); 4052 } 4053 4054 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 4055 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 4056 getLLVMStyleWithColumns(40)); 4057 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4058 getLLVMStyleWithColumns(40)); 4059 EXPECT_EQ("#define Q \\\n" 4060 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 4061 " \"aaaaaaaa.cpp\"", 4062 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4063 getLLVMStyleWithColumns(40))); 4064 } 4065 4066 TEST_F(FormatTest, UnderstandsLinePPDirective) { 4067 EXPECT_EQ("# 123 \"A string literal\"", 4068 format(" # 123 \"A string literal\"")); 4069 } 4070 4071 TEST_F(FormatTest, LayoutUnknownPPDirective) { 4072 EXPECT_EQ("#;", format("#;")); 4073 verifyFormat("#\n;\n;\n;"); 4074 } 4075 4076 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 4077 EXPECT_EQ("#line 42 \"test\"\n", 4078 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 4079 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 4080 getLLVMStyleWithColumns(12))); 4081 } 4082 4083 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 4084 EXPECT_EQ("#line 42 \"test\"", 4085 format("# \\\n line \\\n 42 \\\n \"test\"")); 4086 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 4087 } 4088 4089 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 4090 verifyFormat("#define A \\x20"); 4091 verifyFormat("#define A \\ x20"); 4092 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 4093 verifyFormat("#define A ''"); 4094 verifyFormat("#define A ''qqq"); 4095 verifyFormat("#define A `qqq"); 4096 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 4097 EXPECT_EQ("const char *c = STRINGIFY(\n" 4098 "\\na : b);", 4099 format("const char * c = STRINGIFY(\n" 4100 "\\na : b);")); 4101 4102 verifyFormat("a\r\\"); 4103 verifyFormat("a\v\\"); 4104 verifyFormat("a\f\\"); 4105 } 4106 4107 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 4108 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 4109 style.IndentWidth = 4; 4110 style.PPIndentWidth = 1; 4111 4112 style.IndentPPDirectives = FormatStyle::PPDIS_None; 4113 verifyFormat("#ifdef __linux__\n" 4114 "void foo() {\n" 4115 " int x = 0;\n" 4116 "}\n" 4117 "#define FOO\n" 4118 "#endif\n" 4119 "void bar() {\n" 4120 " int y = 0;\n" 4121 "}\n", 4122 style); 4123 4124 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4125 verifyFormat("#ifdef __linux__\n" 4126 "void foo() {\n" 4127 " int x = 0;\n" 4128 "}\n" 4129 "# define FOO foo\n" 4130 "#endif\n" 4131 "void bar() {\n" 4132 " int y = 0;\n" 4133 "}\n", 4134 style); 4135 4136 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4137 verifyFormat("#ifdef __linux__\n" 4138 "void foo() {\n" 4139 " int x = 0;\n" 4140 "}\n" 4141 " #define FOO foo\n" 4142 "#endif\n" 4143 "void bar() {\n" 4144 " int y = 0;\n" 4145 "}\n", 4146 style); 4147 } 4148 4149 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 4150 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 4151 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 4152 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 4153 // FIXME: We never break before the macro name. 4154 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 4155 4156 verifyFormat("#define A A\n#define A A"); 4157 verifyFormat("#define A(X) A\n#define A A"); 4158 4159 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 4160 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 4161 } 4162 4163 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 4164 EXPECT_EQ("// somecomment\n" 4165 "#include \"a.h\"\n" 4166 "#define A( \\\n" 4167 " A, B)\n" 4168 "#include \"b.h\"\n" 4169 "// somecomment\n", 4170 format(" // somecomment\n" 4171 " #include \"a.h\"\n" 4172 "#define A(A,\\\n" 4173 " B)\n" 4174 " #include \"b.h\"\n" 4175 " // somecomment\n", 4176 getLLVMStyleWithColumns(13))); 4177 } 4178 4179 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 4180 4181 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 4182 EXPECT_EQ("#define A \\\n" 4183 " c; \\\n" 4184 " e;\n" 4185 "f;", 4186 format("#define A c; e;\n" 4187 "f;", 4188 getLLVMStyleWithColumns(14))); 4189 } 4190 4191 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 4192 4193 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 4194 EXPECT_EQ("int x,\n" 4195 "#define A\n" 4196 " y;", 4197 format("int x,\n#define A\ny;")); 4198 } 4199 4200 TEST_F(FormatTest, HashInMacroDefinition) { 4201 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 4202 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 4203 verifyFormat("#define A \\\n" 4204 " { \\\n" 4205 " f(#c); \\\n" 4206 " }", 4207 getLLVMStyleWithColumns(11)); 4208 4209 verifyFormat("#define A(X) \\\n" 4210 " void function##X()", 4211 getLLVMStyleWithColumns(22)); 4212 4213 verifyFormat("#define A(a, b, c) \\\n" 4214 " void a##b##c()", 4215 getLLVMStyleWithColumns(22)); 4216 4217 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 4218 } 4219 4220 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 4221 EXPECT_EQ("#define A (x)", format("#define A (x)")); 4222 EXPECT_EQ("#define A(x)", format("#define A(x)")); 4223 4224 FormatStyle Style = getLLVMStyle(); 4225 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 4226 verifyFormat("#define true ((foo)1)", Style); 4227 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 4228 verifyFormat("#define false((foo)0)", Style); 4229 } 4230 4231 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 4232 EXPECT_EQ("#define A b;", format("#define A \\\n" 4233 " \\\n" 4234 " b;", 4235 getLLVMStyleWithColumns(25))); 4236 EXPECT_EQ("#define A \\\n" 4237 " \\\n" 4238 " a; \\\n" 4239 " b;", 4240 format("#define A \\\n" 4241 " \\\n" 4242 " a; \\\n" 4243 " b;", 4244 getLLVMStyleWithColumns(11))); 4245 EXPECT_EQ("#define A \\\n" 4246 " a; \\\n" 4247 " \\\n" 4248 " b;", 4249 format("#define A \\\n" 4250 " a; \\\n" 4251 " \\\n" 4252 " b;", 4253 getLLVMStyleWithColumns(11))); 4254 } 4255 4256 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 4257 verifyIncompleteFormat("#define A :"); 4258 verifyFormat("#define SOMECASES \\\n" 4259 " case 1: \\\n" 4260 " case 2\n", 4261 getLLVMStyleWithColumns(20)); 4262 verifyFormat("#define MACRO(a) \\\n" 4263 " if (a) \\\n" 4264 " f(); \\\n" 4265 " else \\\n" 4266 " g()", 4267 getLLVMStyleWithColumns(18)); 4268 verifyFormat("#define A template <typename T>"); 4269 verifyIncompleteFormat("#define STR(x) #x\n" 4270 "f(STR(this_is_a_string_literal{));"); 4271 verifyFormat("#pragma omp threadprivate( \\\n" 4272 " y)), // expected-warning", 4273 getLLVMStyleWithColumns(28)); 4274 verifyFormat("#d, = };"); 4275 verifyFormat("#if \"a"); 4276 verifyIncompleteFormat("({\n" 4277 "#define b \\\n" 4278 " } \\\n" 4279 " a\n" 4280 "a", 4281 getLLVMStyleWithColumns(15)); 4282 verifyFormat("#define A \\\n" 4283 " { \\\n" 4284 " {\n" 4285 "#define B \\\n" 4286 " } \\\n" 4287 " }", 4288 getLLVMStyleWithColumns(15)); 4289 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 4290 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 4291 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 4292 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 4293 } 4294 4295 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 4296 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 4297 EXPECT_EQ("class A : public QObject {\n" 4298 " Q_OBJECT\n" 4299 "\n" 4300 " A() {}\n" 4301 "};", 4302 format("class A : public QObject {\n" 4303 " Q_OBJECT\n" 4304 "\n" 4305 " A() {\n}\n" 4306 "} ;")); 4307 EXPECT_EQ("MACRO\n" 4308 "/*static*/ int i;", 4309 format("MACRO\n" 4310 " /*static*/ int i;")); 4311 EXPECT_EQ("SOME_MACRO\n" 4312 "namespace {\n" 4313 "void f();\n" 4314 "} // namespace", 4315 format("SOME_MACRO\n" 4316 " namespace {\n" 4317 "void f( );\n" 4318 "} // namespace")); 4319 // Only if the identifier contains at least 5 characters. 4320 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 4321 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 4322 // Only if everything is upper case. 4323 EXPECT_EQ("class A : public QObject {\n" 4324 " Q_Object A() {}\n" 4325 "};", 4326 format("class A : public QObject {\n" 4327 " Q_Object\n" 4328 " A() {\n}\n" 4329 "} ;")); 4330 4331 // Only if the next line can actually start an unwrapped line. 4332 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 4333 format("SOME_WEIRD_LOG_MACRO\n" 4334 "<< SomeThing;")); 4335 4336 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 4337 "(n, buffers))\n", 4338 getChromiumStyle(FormatStyle::LK_Cpp)); 4339 4340 // See PR41483 4341 EXPECT_EQ("/**/ FOO(a)\n" 4342 "FOO(b)", 4343 format("/**/ FOO(a)\n" 4344 "FOO(b)")); 4345 } 4346 4347 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 4348 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4349 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4350 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4351 "class X {};\n" 4352 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4353 "int *createScopDetectionPass() { return 0; }", 4354 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4355 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4356 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4357 " class X {};\n" 4358 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4359 " int *createScopDetectionPass() { return 0; }")); 4360 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 4361 // braces, so that inner block is indented one level more. 4362 EXPECT_EQ("int q() {\n" 4363 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4364 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4365 " IPC_END_MESSAGE_MAP()\n" 4366 "}", 4367 format("int q() {\n" 4368 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4369 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4370 " IPC_END_MESSAGE_MAP()\n" 4371 "}")); 4372 4373 // Same inside macros. 4374 EXPECT_EQ("#define LIST(L) \\\n" 4375 " L(A) \\\n" 4376 " L(B) \\\n" 4377 " L(C)", 4378 format("#define LIST(L) \\\n" 4379 " L(A) \\\n" 4380 " L(B) \\\n" 4381 " L(C)", 4382 getGoogleStyle())); 4383 4384 // These must not be recognized as macros. 4385 EXPECT_EQ("int q() {\n" 4386 " f(x);\n" 4387 " f(x) {}\n" 4388 " f(x)->g();\n" 4389 " f(x)->*g();\n" 4390 " f(x).g();\n" 4391 " f(x) = x;\n" 4392 " f(x) += x;\n" 4393 " f(x) -= x;\n" 4394 " f(x) *= x;\n" 4395 " f(x) /= x;\n" 4396 " f(x) %= x;\n" 4397 " f(x) &= x;\n" 4398 " f(x) |= x;\n" 4399 " f(x) ^= x;\n" 4400 " f(x) >>= x;\n" 4401 " f(x) <<= x;\n" 4402 " f(x)[y].z();\n" 4403 " LOG(INFO) << x;\n" 4404 " ifstream(x) >> x;\n" 4405 "}\n", 4406 format("int q() {\n" 4407 " f(x)\n;\n" 4408 " f(x)\n {}\n" 4409 " f(x)\n->g();\n" 4410 " f(x)\n->*g();\n" 4411 " f(x)\n.g();\n" 4412 " f(x)\n = x;\n" 4413 " f(x)\n += x;\n" 4414 " f(x)\n -= x;\n" 4415 " f(x)\n *= x;\n" 4416 " f(x)\n /= x;\n" 4417 " f(x)\n %= x;\n" 4418 " f(x)\n &= x;\n" 4419 " f(x)\n |= x;\n" 4420 " f(x)\n ^= x;\n" 4421 " f(x)\n >>= x;\n" 4422 " f(x)\n <<= x;\n" 4423 " f(x)\n[y].z();\n" 4424 " LOG(INFO)\n << x;\n" 4425 " ifstream(x)\n >> x;\n" 4426 "}\n")); 4427 EXPECT_EQ("int q() {\n" 4428 " F(x)\n" 4429 " if (1) {\n" 4430 " }\n" 4431 " F(x)\n" 4432 " while (1) {\n" 4433 " }\n" 4434 " F(x)\n" 4435 " G(x);\n" 4436 " F(x)\n" 4437 " try {\n" 4438 " Q();\n" 4439 " } catch (...) {\n" 4440 " }\n" 4441 "}\n", 4442 format("int q() {\n" 4443 "F(x)\n" 4444 "if (1) {}\n" 4445 "F(x)\n" 4446 "while (1) {}\n" 4447 "F(x)\n" 4448 "G(x);\n" 4449 "F(x)\n" 4450 "try { Q(); } catch (...) {}\n" 4451 "}\n")); 4452 EXPECT_EQ("class A {\n" 4453 " A() : t(0) {}\n" 4454 " A(int i) noexcept() : {}\n" 4455 " A(X x)\n" // FIXME: function-level try blocks are broken. 4456 " try : t(0) {\n" 4457 " } catch (...) {\n" 4458 " }\n" 4459 "};", 4460 format("class A {\n" 4461 " A()\n : t(0) {}\n" 4462 " A(int i)\n noexcept() : {}\n" 4463 " A(X x)\n" 4464 " try : t(0) {} catch (...) {}\n" 4465 "};")); 4466 FormatStyle Style = getLLVMStyle(); 4467 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4468 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 4469 Style.BraceWrapping.AfterFunction = true; 4470 EXPECT_EQ("void f()\n" 4471 "try\n" 4472 "{\n" 4473 "}", 4474 format("void f() try {\n" 4475 "}", 4476 Style)); 4477 EXPECT_EQ("class SomeClass {\n" 4478 "public:\n" 4479 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4480 "};", 4481 format("class SomeClass {\n" 4482 "public:\n" 4483 " SomeClass()\n" 4484 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4485 "};")); 4486 EXPECT_EQ("class SomeClass {\n" 4487 "public:\n" 4488 " SomeClass()\n" 4489 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4490 "};", 4491 format("class SomeClass {\n" 4492 "public:\n" 4493 " SomeClass()\n" 4494 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4495 "};", 4496 getLLVMStyleWithColumns(40))); 4497 4498 verifyFormat("MACRO(>)"); 4499 4500 // Some macros contain an implicit semicolon. 4501 Style = getLLVMStyle(); 4502 Style.StatementMacros.push_back("FOO"); 4503 verifyFormat("FOO(a) int b = 0;"); 4504 verifyFormat("FOO(a)\n" 4505 "int b = 0;", 4506 Style); 4507 verifyFormat("FOO(a);\n" 4508 "int b = 0;", 4509 Style); 4510 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 4511 "int b = 0;", 4512 Style); 4513 verifyFormat("FOO()\n" 4514 "int b = 0;", 4515 Style); 4516 verifyFormat("FOO\n" 4517 "int b = 0;", 4518 Style); 4519 verifyFormat("void f() {\n" 4520 " FOO(a)\n" 4521 " return a;\n" 4522 "}", 4523 Style); 4524 verifyFormat("FOO(a)\n" 4525 "FOO(b)", 4526 Style); 4527 verifyFormat("int a = 0;\n" 4528 "FOO(b)\n" 4529 "int c = 0;", 4530 Style); 4531 verifyFormat("int a = 0;\n" 4532 "int x = FOO(a)\n" 4533 "int b = 0;", 4534 Style); 4535 verifyFormat("void foo(int a) { FOO(a) }\n" 4536 "uint32_t bar() {}", 4537 Style); 4538 } 4539 4540 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 4541 verifyFormat("#define A \\\n" 4542 " f({ \\\n" 4543 " g(); \\\n" 4544 " });", 4545 getLLVMStyleWithColumns(11)); 4546 } 4547 4548 TEST_F(FormatTest, IndentPreprocessorDirectives) { 4549 FormatStyle Style = getLLVMStyle(); 4550 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 4551 Style.ColumnLimit = 40; 4552 verifyFormat("#ifdef _WIN32\n" 4553 "#define A 0\n" 4554 "#ifdef VAR2\n" 4555 "#define B 1\n" 4556 "#include <someheader.h>\n" 4557 "#define MACRO \\\n" 4558 " some_very_long_func_aaaaaaaaaa();\n" 4559 "#endif\n" 4560 "#else\n" 4561 "#define A 1\n" 4562 "#endif", 4563 Style); 4564 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4565 verifyFormat("#ifdef _WIN32\n" 4566 "# define A 0\n" 4567 "# ifdef VAR2\n" 4568 "# define B 1\n" 4569 "# include <someheader.h>\n" 4570 "# define MACRO \\\n" 4571 " some_very_long_func_aaaaaaaaaa();\n" 4572 "# endif\n" 4573 "#else\n" 4574 "# define A 1\n" 4575 "#endif", 4576 Style); 4577 verifyFormat("#if A\n" 4578 "# define MACRO \\\n" 4579 " void a(int x) { \\\n" 4580 " b(); \\\n" 4581 " c(); \\\n" 4582 " d(); \\\n" 4583 " e(); \\\n" 4584 " f(); \\\n" 4585 " }\n" 4586 "#endif", 4587 Style); 4588 // Comments before include guard. 4589 verifyFormat("// file comment\n" 4590 "// file comment\n" 4591 "#ifndef HEADER_H\n" 4592 "#define HEADER_H\n" 4593 "code();\n" 4594 "#endif", 4595 Style); 4596 // Test with include guards. 4597 verifyFormat("#ifndef HEADER_H\n" 4598 "#define HEADER_H\n" 4599 "code();\n" 4600 "#endif", 4601 Style); 4602 // Include guards must have a #define with the same variable immediately 4603 // after #ifndef. 4604 verifyFormat("#ifndef NOT_GUARD\n" 4605 "# define FOO\n" 4606 "code();\n" 4607 "#endif", 4608 Style); 4609 4610 // Include guards must cover the entire file. 4611 verifyFormat("code();\n" 4612 "code();\n" 4613 "#ifndef NOT_GUARD\n" 4614 "# define NOT_GUARD\n" 4615 "code();\n" 4616 "#endif", 4617 Style); 4618 verifyFormat("#ifndef NOT_GUARD\n" 4619 "# define NOT_GUARD\n" 4620 "code();\n" 4621 "#endif\n" 4622 "code();", 4623 Style); 4624 // Test with trailing blank lines. 4625 verifyFormat("#ifndef HEADER_H\n" 4626 "#define HEADER_H\n" 4627 "code();\n" 4628 "#endif\n", 4629 Style); 4630 // Include guards don't have #else. 4631 verifyFormat("#ifndef NOT_GUARD\n" 4632 "# define NOT_GUARD\n" 4633 "code();\n" 4634 "#else\n" 4635 "#endif", 4636 Style); 4637 verifyFormat("#ifndef NOT_GUARD\n" 4638 "# define NOT_GUARD\n" 4639 "code();\n" 4640 "#elif FOO\n" 4641 "#endif", 4642 Style); 4643 // Non-identifier #define after potential include guard. 4644 verifyFormat("#ifndef FOO\n" 4645 "# define 1\n" 4646 "#endif\n", 4647 Style); 4648 // #if closes past last non-preprocessor line. 4649 verifyFormat("#ifndef FOO\n" 4650 "#define FOO\n" 4651 "#if 1\n" 4652 "int i;\n" 4653 "# define A 0\n" 4654 "#endif\n" 4655 "#endif\n", 4656 Style); 4657 // Don't crash if there is an #elif directive without a condition. 4658 verifyFormat("#if 1\n" 4659 "int x;\n" 4660 "#elif\n" 4661 "int y;\n" 4662 "#else\n" 4663 "int z;\n" 4664 "#endif", 4665 Style); 4666 // FIXME: This doesn't handle the case where there's code between the 4667 // #ifndef and #define but all other conditions hold. This is because when 4668 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 4669 // previous code line yet, so we can't detect it. 4670 EXPECT_EQ("#ifndef NOT_GUARD\n" 4671 "code();\n" 4672 "#define NOT_GUARD\n" 4673 "code();\n" 4674 "#endif", 4675 format("#ifndef NOT_GUARD\n" 4676 "code();\n" 4677 "# define NOT_GUARD\n" 4678 "code();\n" 4679 "#endif", 4680 Style)); 4681 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 4682 // be outside an include guard. Examples are #pragma once and 4683 // #pragma GCC diagnostic, or anything else that does not change the meaning 4684 // of the file if it's included multiple times. 4685 EXPECT_EQ("#ifdef WIN32\n" 4686 "# pragma once\n" 4687 "#endif\n" 4688 "#ifndef HEADER_H\n" 4689 "# define HEADER_H\n" 4690 "code();\n" 4691 "#endif", 4692 format("#ifdef WIN32\n" 4693 "# pragma once\n" 4694 "#endif\n" 4695 "#ifndef HEADER_H\n" 4696 "#define HEADER_H\n" 4697 "code();\n" 4698 "#endif", 4699 Style)); 4700 // FIXME: This does not detect when there is a single non-preprocessor line 4701 // in front of an include-guard-like structure where other conditions hold 4702 // because ScopedLineState hides the line. 4703 EXPECT_EQ("code();\n" 4704 "#ifndef HEADER_H\n" 4705 "#define HEADER_H\n" 4706 "code();\n" 4707 "#endif", 4708 format("code();\n" 4709 "#ifndef HEADER_H\n" 4710 "# define HEADER_H\n" 4711 "code();\n" 4712 "#endif", 4713 Style)); 4714 // Keep comments aligned with #, otherwise indent comments normally. These 4715 // tests cannot use verifyFormat because messUp manipulates leading 4716 // whitespace. 4717 { 4718 const char *Expected = "" 4719 "void f() {\n" 4720 "#if 1\n" 4721 "// Preprocessor aligned.\n" 4722 "# define A 0\n" 4723 " // Code. Separated by blank line.\n" 4724 "\n" 4725 "# define B 0\n" 4726 " // Code. Not aligned with #\n" 4727 "# define C 0\n" 4728 "#endif"; 4729 const char *ToFormat = "" 4730 "void f() {\n" 4731 "#if 1\n" 4732 "// Preprocessor aligned.\n" 4733 "# define A 0\n" 4734 "// Code. Separated by blank line.\n" 4735 "\n" 4736 "# define B 0\n" 4737 " // Code. Not aligned with #\n" 4738 "# define C 0\n" 4739 "#endif"; 4740 EXPECT_EQ(Expected, format(ToFormat, Style)); 4741 EXPECT_EQ(Expected, format(Expected, Style)); 4742 } 4743 // Keep block quotes aligned. 4744 { 4745 const char *Expected = "" 4746 "void f() {\n" 4747 "#if 1\n" 4748 "/* Preprocessor aligned. */\n" 4749 "# define A 0\n" 4750 " /* Code. Separated by blank line. */\n" 4751 "\n" 4752 "# define B 0\n" 4753 " /* Code. Not aligned with # */\n" 4754 "# define C 0\n" 4755 "#endif"; 4756 const char *ToFormat = "" 4757 "void f() {\n" 4758 "#if 1\n" 4759 "/* Preprocessor aligned. */\n" 4760 "# define A 0\n" 4761 "/* Code. Separated by blank line. */\n" 4762 "\n" 4763 "# define B 0\n" 4764 " /* Code. Not aligned with # */\n" 4765 "# define C 0\n" 4766 "#endif"; 4767 EXPECT_EQ(Expected, format(ToFormat, Style)); 4768 EXPECT_EQ(Expected, format(Expected, Style)); 4769 } 4770 // Keep comments aligned with un-indented directives. 4771 { 4772 const char *Expected = "" 4773 "void f() {\n" 4774 "// Preprocessor aligned.\n" 4775 "#define A 0\n" 4776 " // Code. Separated by blank line.\n" 4777 "\n" 4778 "#define B 0\n" 4779 " // Code. Not aligned with #\n" 4780 "#define C 0\n"; 4781 const char *ToFormat = "" 4782 "void f() {\n" 4783 "// Preprocessor aligned.\n" 4784 "#define A 0\n" 4785 "// Code. Separated by blank line.\n" 4786 "\n" 4787 "#define B 0\n" 4788 " // Code. Not aligned with #\n" 4789 "#define C 0\n"; 4790 EXPECT_EQ(Expected, format(ToFormat, Style)); 4791 EXPECT_EQ(Expected, format(Expected, Style)); 4792 } 4793 // Test AfterHash with tabs. 4794 { 4795 FormatStyle Tabbed = Style; 4796 Tabbed.UseTab = FormatStyle::UT_Always; 4797 Tabbed.IndentWidth = 8; 4798 Tabbed.TabWidth = 8; 4799 verifyFormat("#ifdef _WIN32\n" 4800 "#\tdefine A 0\n" 4801 "#\tifdef VAR2\n" 4802 "#\t\tdefine B 1\n" 4803 "#\t\tinclude <someheader.h>\n" 4804 "#\t\tdefine MACRO \\\n" 4805 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 4806 "#\tendif\n" 4807 "#else\n" 4808 "#\tdefine A 1\n" 4809 "#endif", 4810 Tabbed); 4811 } 4812 4813 // Regression test: Multiline-macro inside include guards. 4814 verifyFormat("#ifndef HEADER_H\n" 4815 "#define HEADER_H\n" 4816 "#define A() \\\n" 4817 " int i; \\\n" 4818 " int j;\n" 4819 "#endif // HEADER_H", 4820 getLLVMStyleWithColumns(20)); 4821 4822 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4823 // Basic before hash indent tests 4824 verifyFormat("#ifdef _WIN32\n" 4825 " #define A 0\n" 4826 " #ifdef VAR2\n" 4827 " #define B 1\n" 4828 " #include <someheader.h>\n" 4829 " #define MACRO \\\n" 4830 " some_very_long_func_aaaaaaaaaa();\n" 4831 " #endif\n" 4832 "#else\n" 4833 " #define A 1\n" 4834 "#endif", 4835 Style); 4836 verifyFormat("#if A\n" 4837 " #define MACRO \\\n" 4838 " void a(int x) { \\\n" 4839 " b(); \\\n" 4840 " c(); \\\n" 4841 " d(); \\\n" 4842 " e(); \\\n" 4843 " f(); \\\n" 4844 " }\n" 4845 "#endif", 4846 Style); 4847 // Keep comments aligned with indented directives. These 4848 // tests cannot use verifyFormat because messUp manipulates leading 4849 // whitespace. 4850 { 4851 const char *Expected = "void f() {\n" 4852 "// Aligned to preprocessor.\n" 4853 "#if 1\n" 4854 " // Aligned to code.\n" 4855 " int a;\n" 4856 " #if 1\n" 4857 " // Aligned to preprocessor.\n" 4858 " #define A 0\n" 4859 " // Aligned to code.\n" 4860 " int b;\n" 4861 " #endif\n" 4862 "#endif\n" 4863 "}"; 4864 const char *ToFormat = "void f() {\n" 4865 "// Aligned to preprocessor.\n" 4866 "#if 1\n" 4867 "// Aligned to code.\n" 4868 "int a;\n" 4869 "#if 1\n" 4870 "// Aligned to preprocessor.\n" 4871 "#define A 0\n" 4872 "// Aligned to code.\n" 4873 "int b;\n" 4874 "#endif\n" 4875 "#endif\n" 4876 "}"; 4877 EXPECT_EQ(Expected, format(ToFormat, Style)); 4878 EXPECT_EQ(Expected, format(Expected, Style)); 4879 } 4880 { 4881 const char *Expected = "void f() {\n" 4882 "/* Aligned to preprocessor. */\n" 4883 "#if 1\n" 4884 " /* Aligned to code. */\n" 4885 " int a;\n" 4886 " #if 1\n" 4887 " /* Aligned to preprocessor. */\n" 4888 " #define A 0\n" 4889 " /* Aligned to code. */\n" 4890 " int b;\n" 4891 " #endif\n" 4892 "#endif\n" 4893 "}"; 4894 const char *ToFormat = "void f() {\n" 4895 "/* Aligned to preprocessor. */\n" 4896 "#if 1\n" 4897 "/* Aligned to code. */\n" 4898 "int a;\n" 4899 "#if 1\n" 4900 "/* Aligned to preprocessor. */\n" 4901 "#define A 0\n" 4902 "/* Aligned to code. */\n" 4903 "int b;\n" 4904 "#endif\n" 4905 "#endif\n" 4906 "}"; 4907 EXPECT_EQ(Expected, format(ToFormat, Style)); 4908 EXPECT_EQ(Expected, format(Expected, Style)); 4909 } 4910 4911 // Test single comment before preprocessor 4912 verifyFormat("// Comment\n" 4913 "\n" 4914 "#if 1\n" 4915 "#endif", 4916 Style); 4917 } 4918 4919 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 4920 verifyFormat("{\n { a #c; }\n}"); 4921 } 4922 4923 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 4924 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 4925 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 4926 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 4927 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 4928 } 4929 4930 TEST_F(FormatTest, EscapedNewlines) { 4931 FormatStyle Narrow = getLLVMStyleWithColumns(11); 4932 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 4933 format("#define A \\\nint i;\\\n int j;", Narrow)); 4934 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 4935 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 4936 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 4937 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 4938 4939 FormatStyle AlignLeft = getLLVMStyle(); 4940 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 4941 EXPECT_EQ("#define MACRO(x) \\\n" 4942 "private: \\\n" 4943 " int x(int a);\n", 4944 format("#define MACRO(x) \\\n" 4945 "private: \\\n" 4946 " int x(int a);\n", 4947 AlignLeft)); 4948 4949 // CRLF line endings 4950 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 4951 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 4952 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 4953 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 4954 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 4955 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 4956 EXPECT_EQ("#define MACRO(x) \\\r\n" 4957 "private: \\\r\n" 4958 " int x(int a);\r\n", 4959 format("#define MACRO(x) \\\r\n" 4960 "private: \\\r\n" 4961 " int x(int a);\r\n", 4962 AlignLeft)); 4963 4964 FormatStyle DontAlign = getLLVMStyle(); 4965 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 4966 DontAlign.MaxEmptyLinesToKeep = 3; 4967 // FIXME: can't use verifyFormat here because the newline before 4968 // "public:" is not inserted the first time it's reformatted 4969 EXPECT_EQ("#define A \\\n" 4970 " class Foo { \\\n" 4971 " void bar(); \\\n" 4972 "\\\n" 4973 "\\\n" 4974 "\\\n" 4975 " public: \\\n" 4976 " void baz(); \\\n" 4977 " };", 4978 format("#define A \\\n" 4979 " class Foo { \\\n" 4980 " void bar(); \\\n" 4981 "\\\n" 4982 "\\\n" 4983 "\\\n" 4984 " public: \\\n" 4985 " void baz(); \\\n" 4986 " };", 4987 DontAlign)); 4988 } 4989 4990 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 4991 verifyFormat("#define A \\\n" 4992 " int v( \\\n" 4993 " a); \\\n" 4994 " int i;", 4995 getLLVMStyleWithColumns(11)); 4996 } 4997 4998 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 4999 EXPECT_EQ( 5000 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5001 " \\\n" 5002 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5003 "\n" 5004 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5005 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 5006 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5007 "\\\n" 5008 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5009 " \n" 5010 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5011 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 5012 } 5013 5014 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 5015 EXPECT_EQ("int\n" 5016 "#define A\n" 5017 " a;", 5018 format("int\n#define A\na;")); 5019 verifyFormat("functionCallTo(\n" 5020 " someOtherFunction(\n" 5021 " withSomeParameters, whichInSequence,\n" 5022 " areLongerThanALine(andAnotherCall,\n" 5023 "#define A B\n" 5024 " withMoreParamters,\n" 5025 " whichStronglyInfluenceTheLayout),\n" 5026 " andMoreParameters),\n" 5027 " trailing);", 5028 getLLVMStyleWithColumns(69)); 5029 verifyFormat("Foo::Foo()\n" 5030 "#ifdef BAR\n" 5031 " : baz(0)\n" 5032 "#endif\n" 5033 "{\n" 5034 "}"); 5035 verifyFormat("void f() {\n" 5036 " if (true)\n" 5037 "#ifdef A\n" 5038 " f(42);\n" 5039 " x();\n" 5040 "#else\n" 5041 " g();\n" 5042 " x();\n" 5043 "#endif\n" 5044 "}"); 5045 verifyFormat("void f(param1, param2,\n" 5046 " param3,\n" 5047 "#ifdef A\n" 5048 " param4(param5,\n" 5049 "#ifdef A1\n" 5050 " param6,\n" 5051 "#ifdef A2\n" 5052 " param7),\n" 5053 "#else\n" 5054 " param8),\n" 5055 " param9,\n" 5056 "#endif\n" 5057 " param10,\n" 5058 "#endif\n" 5059 " param11)\n" 5060 "#else\n" 5061 " param12)\n" 5062 "#endif\n" 5063 "{\n" 5064 " x();\n" 5065 "}", 5066 getLLVMStyleWithColumns(28)); 5067 verifyFormat("#if 1\n" 5068 "int i;"); 5069 verifyFormat("#if 1\n" 5070 "#endif\n" 5071 "#if 1\n" 5072 "#else\n" 5073 "#endif\n"); 5074 verifyFormat("DEBUG({\n" 5075 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 5077 "});\n" 5078 "#if a\n" 5079 "#else\n" 5080 "#endif"); 5081 5082 verifyIncompleteFormat("void f(\n" 5083 "#if A\n" 5084 ");\n" 5085 "#else\n" 5086 "#endif"); 5087 } 5088 5089 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 5090 verifyFormat("#endif\n" 5091 "#if B"); 5092 } 5093 5094 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 5095 FormatStyle SingleLine = getLLVMStyle(); 5096 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 5097 verifyFormat("#if 0\n" 5098 "#elif 1\n" 5099 "#endif\n" 5100 "void foo() {\n" 5101 " if (test) foo2();\n" 5102 "}", 5103 SingleLine); 5104 } 5105 5106 TEST_F(FormatTest, LayoutBlockInsideParens) { 5107 verifyFormat("functionCall({ int i; });"); 5108 verifyFormat("functionCall({\n" 5109 " int i;\n" 5110 " int j;\n" 5111 "});"); 5112 verifyFormat("functionCall(\n" 5113 " {\n" 5114 " int i;\n" 5115 " int j;\n" 5116 " },\n" 5117 " aaaa, bbbb, cccc);"); 5118 verifyFormat("functionA(functionB({\n" 5119 " int i;\n" 5120 " int j;\n" 5121 " }),\n" 5122 " aaaa, bbbb, cccc);"); 5123 verifyFormat("functionCall(\n" 5124 " {\n" 5125 " int i;\n" 5126 " int j;\n" 5127 " },\n" 5128 " aaaa, bbbb, // comment\n" 5129 " cccc);"); 5130 verifyFormat("functionA(functionB({\n" 5131 " int i;\n" 5132 " int j;\n" 5133 " }),\n" 5134 " aaaa, bbbb, // comment\n" 5135 " cccc);"); 5136 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 5137 verifyFormat("functionCall(aaaa, bbbb, {\n" 5138 " int i;\n" 5139 " int j;\n" 5140 "});"); 5141 verifyFormat( 5142 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 5143 " {\n" 5144 " int i; // break\n" 5145 " },\n" 5146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5147 " ccccccccccccccccc));"); 5148 verifyFormat("DEBUG({\n" 5149 " if (a)\n" 5150 " f();\n" 5151 "});"); 5152 } 5153 5154 TEST_F(FormatTest, LayoutBlockInsideStatement) { 5155 EXPECT_EQ("SOME_MACRO { int i; }\n" 5156 "int i;", 5157 format(" SOME_MACRO {int i;} int i;")); 5158 } 5159 5160 TEST_F(FormatTest, LayoutNestedBlocks) { 5161 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 5162 " struct s {\n" 5163 " int i;\n" 5164 " };\n" 5165 " s kBitsToOs[] = {{10}};\n" 5166 " for (int i = 0; i < 10; ++i)\n" 5167 " return;\n" 5168 "}"); 5169 verifyFormat("call(parameter, {\n" 5170 " something();\n" 5171 " // Comment using all columns.\n" 5172 " somethingelse();\n" 5173 "});", 5174 getLLVMStyleWithColumns(40)); 5175 verifyFormat("DEBUG( //\n" 5176 " { f(); }, a);"); 5177 verifyFormat("DEBUG( //\n" 5178 " {\n" 5179 " f(); //\n" 5180 " },\n" 5181 " a);"); 5182 5183 EXPECT_EQ("call(parameter, {\n" 5184 " something();\n" 5185 " // Comment too\n" 5186 " // looooooooooong.\n" 5187 " somethingElse();\n" 5188 "});", 5189 format("call(parameter, {\n" 5190 " something();\n" 5191 " // Comment too looooooooooong.\n" 5192 " somethingElse();\n" 5193 "});", 5194 getLLVMStyleWithColumns(29))); 5195 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 5196 EXPECT_EQ("DEBUG({ // comment\n" 5197 " int i;\n" 5198 "});", 5199 format("DEBUG({ // comment\n" 5200 "int i;\n" 5201 "});")); 5202 EXPECT_EQ("DEBUG({\n" 5203 " int i;\n" 5204 "\n" 5205 " // comment\n" 5206 " int j;\n" 5207 "});", 5208 format("DEBUG({\n" 5209 " int i;\n" 5210 "\n" 5211 " // comment\n" 5212 " int j;\n" 5213 "});")); 5214 5215 verifyFormat("DEBUG({\n" 5216 " if (a)\n" 5217 " return;\n" 5218 "});"); 5219 verifyGoogleFormat("DEBUG({\n" 5220 " if (a) return;\n" 5221 "});"); 5222 FormatStyle Style = getGoogleStyle(); 5223 Style.ColumnLimit = 45; 5224 verifyFormat("Debug(\n" 5225 " aaaaa,\n" 5226 " {\n" 5227 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 5228 " },\n" 5229 " a);", 5230 Style); 5231 5232 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 5233 5234 verifyNoCrash("^{v^{a}}"); 5235 } 5236 5237 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 5238 EXPECT_EQ("#define MACRO() \\\n" 5239 " Debug(aaa, /* force line break */ \\\n" 5240 " { \\\n" 5241 " int i; \\\n" 5242 " int j; \\\n" 5243 " })", 5244 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 5245 " { int i; int j; })", 5246 getGoogleStyle())); 5247 5248 EXPECT_EQ("#define A \\\n" 5249 " [] { \\\n" 5250 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5251 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 5252 " }", 5253 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5254 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 5255 getGoogleStyle())); 5256 } 5257 5258 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 5259 EXPECT_EQ("{}", format("{}")); 5260 verifyFormat("enum E {};"); 5261 verifyFormat("enum E {}"); 5262 FormatStyle Style = getLLVMStyle(); 5263 Style.SpaceInEmptyBlock = true; 5264 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 5265 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 5266 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 5267 } 5268 5269 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 5270 FormatStyle Style = getLLVMStyle(); 5271 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 5272 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 5273 verifyFormat("FOO_BEGIN\n" 5274 " FOO_ENTRY\n" 5275 "FOO_END", 5276 Style); 5277 verifyFormat("FOO_BEGIN\n" 5278 " NESTED_FOO_BEGIN\n" 5279 " NESTED_FOO_ENTRY\n" 5280 " NESTED_FOO_END\n" 5281 "FOO_END", 5282 Style); 5283 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 5284 " int x;\n" 5285 " x = 1;\n" 5286 "FOO_END(Baz)", 5287 Style); 5288 } 5289 5290 //===----------------------------------------------------------------------===// 5291 // Line break tests. 5292 //===----------------------------------------------------------------------===// 5293 5294 TEST_F(FormatTest, PreventConfusingIndents) { 5295 verifyFormat( 5296 "void f() {\n" 5297 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 5298 " parameter, parameter, parameter)),\n" 5299 " SecondLongCall(parameter));\n" 5300 "}"); 5301 verifyFormat( 5302 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5303 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5304 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5305 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5306 verifyFormat( 5307 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5308 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 5309 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5310 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 5311 verifyFormat( 5312 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5313 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 5314 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 5315 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 5316 verifyFormat("int a = bbbb && ccc &&\n" 5317 " fffff(\n" 5318 "#define A Just forcing a new line\n" 5319 " ddd);"); 5320 } 5321 5322 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 5323 verifyFormat( 5324 "bool aaaaaaa =\n" 5325 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 5326 " bbbbbbbb();"); 5327 verifyFormat( 5328 "bool aaaaaaa =\n" 5329 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 5330 " bbbbbbbb();"); 5331 5332 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 5334 " ccccccccc == ddddddddddd;"); 5335 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5336 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 5337 " ccccccccc == ddddddddddd;"); 5338 verifyFormat( 5339 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5340 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 5341 " ccccccccc == ddddddddddd;"); 5342 5343 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5344 " aaaaaa) &&\n" 5345 " bbbbbb && cccccc;"); 5346 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5347 " aaaaaa) >>\n" 5348 " bbbbbb;"); 5349 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 5350 " SourceMgr.getSpellingColumnNumber(\n" 5351 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 5352 " 1);"); 5353 5354 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5355 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 5356 " cccccc) {\n}"); 5357 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5358 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5359 " cccccc) {\n}"); 5360 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5361 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5362 " cccccc) {\n}"); 5363 verifyFormat("b = a &&\n" 5364 " // Comment\n" 5365 " b.c && d;"); 5366 5367 // If the LHS of a comparison is not a binary expression itself, the 5368 // additional linebreak confuses many people. 5369 verifyFormat( 5370 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5371 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 5372 "}"); 5373 verifyFormat( 5374 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5375 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5376 "}"); 5377 verifyFormat( 5378 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 5379 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5380 "}"); 5381 verifyFormat( 5382 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5383 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 5384 "}"); 5385 // Even explicit parentheses stress the precedence enough to make the 5386 // additional break unnecessary. 5387 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5388 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5389 "}"); 5390 // This cases is borderline, but with the indentation it is still readable. 5391 verifyFormat( 5392 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5393 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5394 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5395 "}", 5396 getLLVMStyleWithColumns(75)); 5397 5398 // If the LHS is a binary expression, we should still use the additional break 5399 // as otherwise the formatting hides the operator precedence. 5400 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5401 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5402 " 5) {\n" 5403 "}"); 5404 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5405 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 5406 " 5) {\n" 5407 "}"); 5408 5409 FormatStyle OnePerLine = getLLVMStyle(); 5410 OnePerLine.BinPackParameters = false; 5411 verifyFormat( 5412 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5413 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5414 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 5415 OnePerLine); 5416 5417 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 5418 " .aaa(aaaaaaaaaaaaa) *\n" 5419 " aaaaaaa +\n" 5420 " aaaaaaa;", 5421 getLLVMStyleWithColumns(40)); 5422 } 5423 5424 TEST_F(FormatTest, ExpressionIndentation) { 5425 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5426 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5427 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5428 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5429 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 5430 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 5431 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 5433 " ccccccccccccccccccccccccccccccccccccccccc;"); 5434 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5435 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5436 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5437 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5438 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5441 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5442 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5443 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5445 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5446 verifyFormat("if () {\n" 5447 "} else if (aaaaa && bbbbb > // break\n" 5448 " ccccc) {\n" 5449 "}"); 5450 verifyFormat("if () {\n" 5451 "} else if constexpr (aaaaa && bbbbb > // break\n" 5452 " ccccc) {\n" 5453 "}"); 5454 verifyFormat("if () {\n" 5455 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 5456 " ccccc) {\n" 5457 "}"); 5458 verifyFormat("if () {\n" 5459 "} else if (aaaaa &&\n" 5460 " bbbbb > // break\n" 5461 " ccccc &&\n" 5462 " ddddd) {\n" 5463 "}"); 5464 5465 // Presence of a trailing comment used to change indentation of b. 5466 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 5467 " b;\n" 5468 "return aaaaaaaaaaaaaaaaaaa +\n" 5469 " b; //", 5470 getLLVMStyleWithColumns(30)); 5471 } 5472 5473 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 5474 // Not sure what the best system is here. Like this, the LHS can be found 5475 // immediately above an operator (everything with the same or a higher 5476 // indent). The RHS is aligned right of the operator and so compasses 5477 // everything until something with the same indent as the operator is found. 5478 // FIXME: Is this a good system? 5479 FormatStyle Style = getLLVMStyle(); 5480 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5481 verifyFormat( 5482 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5483 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5484 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5485 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5486 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5487 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5488 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5489 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5490 " > ccccccccccccccccccccccccccccccccccccccccc;", 5491 Style); 5492 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5493 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5494 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5495 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5496 Style); 5497 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5498 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5499 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5500 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5501 Style); 5502 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5503 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5504 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5505 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5506 Style); 5507 verifyFormat("if () {\n" 5508 "} else if (aaaaa\n" 5509 " && bbbbb // break\n" 5510 " > ccccc) {\n" 5511 "}", 5512 Style); 5513 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5514 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5515 Style); 5516 verifyFormat("return (a)\n" 5517 " // comment\n" 5518 " + b;", 5519 Style); 5520 verifyFormat( 5521 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5522 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5523 " + cc;", 5524 Style); 5525 5526 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5527 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5528 Style); 5529 5530 // Forced by comments. 5531 verifyFormat( 5532 "unsigned ContentSize =\n" 5533 " sizeof(int16_t) // DWARF ARange version number\n" 5534 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5535 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5536 " + sizeof(int8_t); // Segment Size (in bytes)"); 5537 5538 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5539 " == boost::fusion::at_c<1>(iiii).second;", 5540 Style); 5541 5542 Style.ColumnLimit = 60; 5543 verifyFormat("zzzzzzzzzz\n" 5544 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5545 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5546 Style); 5547 5548 Style.ColumnLimit = 80; 5549 Style.IndentWidth = 4; 5550 Style.TabWidth = 4; 5551 Style.UseTab = FormatStyle::UT_Always; 5552 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5553 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5554 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 5555 "\t&& (someOtherLongishConditionPart1\n" 5556 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 5557 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 5558 "(someOtherLongishConditionPart1 || " 5559 "someOtherEvenLongerNestedConditionPart2);", 5560 Style)); 5561 } 5562 5563 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 5564 FormatStyle Style = getLLVMStyle(); 5565 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5566 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 5567 5568 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5569 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5570 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5571 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5572 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5573 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5574 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5575 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5576 " > ccccccccccccccccccccccccccccccccccccccccc;", 5577 Style); 5578 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5579 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5580 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5581 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5582 Style); 5583 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5584 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5585 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5586 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5587 Style); 5588 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5589 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5590 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5591 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5592 Style); 5593 verifyFormat("if () {\n" 5594 "} else if (aaaaa\n" 5595 " && bbbbb // break\n" 5596 " > ccccc) {\n" 5597 "}", 5598 Style); 5599 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5600 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5601 Style); 5602 verifyFormat("return (a)\n" 5603 " // comment\n" 5604 " + b;", 5605 Style); 5606 verifyFormat( 5607 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5608 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5609 " + cc;", 5610 Style); 5611 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 5612 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5613 " : 3333333333333333;", 5614 Style); 5615 verifyFormat( 5616 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 5617 " : ccccccccccccccc ? dddddddddddddddddd\n" 5618 " : eeeeeeeeeeeeeeeeee)\n" 5619 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5620 " : 3333333333333333;", 5621 Style); 5622 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5623 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5624 Style); 5625 5626 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5627 " == boost::fusion::at_c<1>(iiii).second;", 5628 Style); 5629 5630 Style.ColumnLimit = 60; 5631 verifyFormat("zzzzzzzzzzzzz\n" 5632 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5633 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5634 Style); 5635 5636 // Forced by comments. 5637 Style.ColumnLimit = 80; 5638 verifyFormat( 5639 "unsigned ContentSize\n" 5640 " = sizeof(int16_t) // DWARF ARange version number\n" 5641 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5642 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5643 " + sizeof(int8_t); // Segment Size (in bytes)", 5644 Style); 5645 5646 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5647 verifyFormat( 5648 "unsigned ContentSize =\n" 5649 " sizeof(int16_t) // DWARF ARange version number\n" 5650 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5651 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5652 " + sizeof(int8_t); // Segment Size (in bytes)", 5653 Style); 5654 5655 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5656 verifyFormat( 5657 "unsigned ContentSize =\n" 5658 " sizeof(int16_t) // DWARF ARange version number\n" 5659 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5660 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5661 " + sizeof(int8_t); // Segment Size (in bytes)", 5662 Style); 5663 } 5664 5665 TEST_F(FormatTest, EnforcedOperatorWraps) { 5666 // Here we'd like to wrap after the || operators, but a comment is forcing an 5667 // earlier wrap. 5668 verifyFormat("bool x = aaaaa //\n" 5669 " || bbbbb\n" 5670 " //\n" 5671 " || cccc;"); 5672 } 5673 5674 TEST_F(FormatTest, NoOperandAlignment) { 5675 FormatStyle Style = getLLVMStyle(); 5676 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5677 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 5678 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5679 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5680 Style); 5681 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5682 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5683 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5684 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5685 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5686 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5687 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5688 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5689 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5690 " > ccccccccccccccccccccccccccccccccccccccccc;", 5691 Style); 5692 5693 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5694 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5695 " + cc;", 5696 Style); 5697 verifyFormat("int a = aa\n" 5698 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5699 " * cccccccccccccccccccccccccccccccccccc;\n", 5700 Style); 5701 5702 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5703 verifyFormat("return (a > b\n" 5704 " // comment1\n" 5705 " // comment2\n" 5706 " || c);", 5707 Style); 5708 } 5709 5710 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 5711 FormatStyle Style = getLLVMStyle(); 5712 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5713 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5715 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5716 Style); 5717 } 5718 5719 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 5720 FormatStyle Style = getLLVMStyle(); 5721 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5722 Style.BinPackArguments = false; 5723 Style.ColumnLimit = 40; 5724 verifyFormat("void test() {\n" 5725 " someFunction(\n" 5726 " this + argument + is + quite\n" 5727 " + long + so + it + gets + wrapped\n" 5728 " + but + remains + bin - packed);\n" 5729 "}", 5730 Style); 5731 verifyFormat("void test() {\n" 5732 " someFunction(arg1,\n" 5733 " this + argument + is\n" 5734 " + quite + long + so\n" 5735 " + it + gets + wrapped\n" 5736 " + but + remains + bin\n" 5737 " - packed,\n" 5738 " arg3);\n" 5739 "}", 5740 Style); 5741 verifyFormat("void test() {\n" 5742 " someFunction(\n" 5743 " arg1,\n" 5744 " this + argument + has\n" 5745 " + anotherFunc(nested,\n" 5746 " calls + whose\n" 5747 " + arguments\n" 5748 " + are + also\n" 5749 " + wrapped,\n" 5750 " in + addition)\n" 5751 " + to + being + bin - packed,\n" 5752 " arg3);\n" 5753 "}", 5754 Style); 5755 5756 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5757 verifyFormat("void test() {\n" 5758 " someFunction(\n" 5759 " arg1,\n" 5760 " this + argument + has +\n" 5761 " anotherFunc(nested,\n" 5762 " calls + whose +\n" 5763 " arguments +\n" 5764 " are + also +\n" 5765 " wrapped,\n" 5766 " in + addition) +\n" 5767 " to + being + bin - packed,\n" 5768 " arg3);\n" 5769 "}", 5770 Style); 5771 } 5772 5773 TEST_F(FormatTest, ConstructorInitializers) { 5774 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 5775 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 5776 getLLVMStyleWithColumns(45)); 5777 verifyFormat("Constructor()\n" 5778 " : Inttializer(FitsOnTheLine) {}", 5779 getLLVMStyleWithColumns(44)); 5780 verifyFormat("Constructor()\n" 5781 " : Inttializer(FitsOnTheLine) {}", 5782 getLLVMStyleWithColumns(43)); 5783 5784 verifyFormat("template <typename T>\n" 5785 "Constructor() : Initializer(FitsOnTheLine) {}", 5786 getLLVMStyleWithColumns(45)); 5787 5788 verifyFormat( 5789 "SomeClass::Constructor()\n" 5790 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5791 5792 verifyFormat( 5793 "SomeClass::Constructor()\n" 5794 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5795 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 5796 verifyFormat( 5797 "SomeClass::Constructor()\n" 5798 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5799 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5800 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5801 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5802 " : aaaaaaaaaa(aaaaaa) {}"); 5803 5804 verifyFormat("Constructor()\n" 5805 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5806 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5807 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5808 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 5809 5810 verifyFormat("Constructor()\n" 5811 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5812 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5813 5814 verifyFormat("Constructor(int Parameter = 0)\n" 5815 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 5816 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 5817 verifyFormat("Constructor()\n" 5818 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 5819 "}", 5820 getLLVMStyleWithColumns(60)); 5821 verifyFormat("Constructor()\n" 5822 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5823 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 5824 5825 // Here a line could be saved by splitting the second initializer onto two 5826 // lines, but that is not desirable. 5827 verifyFormat("Constructor()\n" 5828 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 5829 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 5830 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5831 5832 FormatStyle OnePerLine = getLLVMStyle(); 5833 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5834 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 5835 verifyFormat("SomeClass::Constructor()\n" 5836 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5837 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5838 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5839 OnePerLine); 5840 verifyFormat("SomeClass::Constructor()\n" 5841 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 5842 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5843 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5844 OnePerLine); 5845 verifyFormat("MyClass::MyClass(int var)\n" 5846 " : some_var_(var), // 4 space indent\n" 5847 " some_other_var_(var + 1) { // lined up\n" 5848 "}", 5849 OnePerLine); 5850 verifyFormat("Constructor()\n" 5851 " : aaaaa(aaaaaa),\n" 5852 " aaaaa(aaaaaa),\n" 5853 " aaaaa(aaaaaa),\n" 5854 " aaaaa(aaaaaa),\n" 5855 " aaaaa(aaaaaa) {}", 5856 OnePerLine); 5857 verifyFormat("Constructor()\n" 5858 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5859 " aaaaaaaaaaaaaaaaaaaaaa) {}", 5860 OnePerLine); 5861 OnePerLine.BinPackParameters = false; 5862 verifyFormat( 5863 "Constructor()\n" 5864 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5865 " aaaaaaaaaaa().aaa(),\n" 5866 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5867 OnePerLine); 5868 OnePerLine.ColumnLimit = 60; 5869 verifyFormat("Constructor()\n" 5870 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 5871 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 5872 OnePerLine); 5873 5874 EXPECT_EQ("Constructor()\n" 5875 " : // Comment forcing unwanted break.\n" 5876 " aaaa(aaaa) {}", 5877 format("Constructor() :\n" 5878 " // Comment forcing unwanted break.\n" 5879 " aaaa(aaaa) {}")); 5880 } 5881 5882 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 5883 FormatStyle Style = getLLVMStyle(); 5884 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 5885 Style.ColumnLimit = 60; 5886 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5887 Style.AllowAllConstructorInitializersOnNextLine = true; 5888 Style.BinPackParameters = false; 5889 5890 for (int i = 0; i < 4; ++i) { 5891 // Test all combinations of parameters that should not have an effect. 5892 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 5893 Style.AllowAllArgumentsOnNextLine = i & 2; 5894 5895 Style.AllowAllConstructorInitializersOnNextLine = true; 5896 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 5897 verifyFormat("Constructor()\n" 5898 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5899 Style); 5900 verifyFormat("Constructor() : a(a), b(b) {}", Style); 5901 5902 Style.AllowAllConstructorInitializersOnNextLine = false; 5903 verifyFormat("Constructor()\n" 5904 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 5905 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 5906 Style); 5907 verifyFormat("Constructor() : a(a), b(b) {}", Style); 5908 5909 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 5910 Style.AllowAllConstructorInitializersOnNextLine = true; 5911 verifyFormat("Constructor()\n" 5912 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5913 Style); 5914 5915 Style.AllowAllConstructorInitializersOnNextLine = false; 5916 verifyFormat("Constructor()\n" 5917 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 5918 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5919 Style); 5920 5921 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 5922 Style.AllowAllConstructorInitializersOnNextLine = true; 5923 verifyFormat("Constructor() :\n" 5924 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5925 Style); 5926 5927 Style.AllowAllConstructorInitializersOnNextLine = false; 5928 verifyFormat("Constructor() :\n" 5929 " aaaaaaaaaaaaaaaaaa(a),\n" 5930 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5931 Style); 5932 } 5933 5934 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 5935 // AllowAllConstructorInitializersOnNextLine in all 5936 // BreakConstructorInitializers modes 5937 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 5938 Style.AllowAllParametersOfDeclarationOnNextLine = true; 5939 Style.AllowAllConstructorInitializersOnNextLine = false; 5940 verifyFormat("SomeClassWithALongName::Constructor(\n" 5941 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 5942 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 5943 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 5944 Style); 5945 5946 Style.AllowAllConstructorInitializersOnNextLine = true; 5947 verifyFormat("SomeClassWithALongName::Constructor(\n" 5948 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5949 " int bbbbbbbbbbbbb,\n" 5950 " int cccccccccccccccc)\n" 5951 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5952 Style); 5953 5954 Style.AllowAllParametersOfDeclarationOnNextLine = false; 5955 Style.AllowAllConstructorInitializersOnNextLine = false; 5956 verifyFormat("SomeClassWithALongName::Constructor(\n" 5957 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5958 " int bbbbbbbbbbbbb)\n" 5959 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 5960 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 5961 Style); 5962 5963 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 5964 5965 Style.AllowAllParametersOfDeclarationOnNextLine = true; 5966 verifyFormat("SomeClassWithALongName::Constructor(\n" 5967 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 5968 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 5969 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5970 Style); 5971 5972 Style.AllowAllConstructorInitializersOnNextLine = true; 5973 verifyFormat("SomeClassWithALongName::Constructor(\n" 5974 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5975 " int bbbbbbbbbbbbb,\n" 5976 " int cccccccccccccccc)\n" 5977 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5978 Style); 5979 5980 Style.AllowAllParametersOfDeclarationOnNextLine = false; 5981 Style.AllowAllConstructorInitializersOnNextLine = false; 5982 verifyFormat("SomeClassWithALongName::Constructor(\n" 5983 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5984 " int bbbbbbbbbbbbb)\n" 5985 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 5986 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5987 Style); 5988 5989 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 5990 Style.AllowAllParametersOfDeclarationOnNextLine = true; 5991 verifyFormat("SomeClassWithALongName::Constructor(\n" 5992 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 5993 " aaaaaaaaaaaaaaaaaaaa(a),\n" 5994 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5995 Style); 5996 5997 Style.AllowAllConstructorInitializersOnNextLine = true; 5998 verifyFormat("SomeClassWithALongName::Constructor(\n" 5999 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6000 " int bbbbbbbbbbbbb,\n" 6001 " int cccccccccccccccc) :\n" 6002 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6003 Style); 6004 6005 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6006 Style.AllowAllConstructorInitializersOnNextLine = false; 6007 verifyFormat("SomeClassWithALongName::Constructor(\n" 6008 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6009 " int bbbbbbbbbbbbb) :\n" 6010 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6011 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6012 Style); 6013 } 6014 6015 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 6016 FormatStyle Style = getLLVMStyle(); 6017 Style.ColumnLimit = 60; 6018 Style.BinPackArguments = false; 6019 for (int i = 0; i < 4; ++i) { 6020 // Test all combinations of parameters that should not have an effect. 6021 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6022 Style.AllowAllConstructorInitializersOnNextLine = i & 2; 6023 6024 Style.AllowAllArgumentsOnNextLine = true; 6025 verifyFormat("void foo() {\n" 6026 " FunctionCallWithReallyLongName(\n" 6027 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 6028 "}", 6029 Style); 6030 Style.AllowAllArgumentsOnNextLine = false; 6031 verifyFormat("void foo() {\n" 6032 " FunctionCallWithReallyLongName(\n" 6033 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6034 " bbbbbbbbbbbb);\n" 6035 "}", 6036 Style); 6037 6038 Style.AllowAllArgumentsOnNextLine = true; 6039 verifyFormat("void foo() {\n" 6040 " auto VariableWithReallyLongName = {\n" 6041 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 6042 "}", 6043 Style); 6044 Style.AllowAllArgumentsOnNextLine = false; 6045 verifyFormat("void foo() {\n" 6046 " auto VariableWithReallyLongName = {\n" 6047 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6048 " bbbbbbbbbbbb};\n" 6049 "}", 6050 Style); 6051 } 6052 6053 // This parameter should not affect declarations. 6054 Style.BinPackParameters = false; 6055 Style.AllowAllArgumentsOnNextLine = false; 6056 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6057 verifyFormat("void FunctionCallWithReallyLongName(\n" 6058 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 6059 Style); 6060 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6061 verifyFormat("void FunctionCallWithReallyLongName(\n" 6062 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 6063 " int bbbbbbbbbbbb);", 6064 Style); 6065 } 6066 6067 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 6068 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 6069 // and BAS_Align. 6070 auto Style = getLLVMStyle(); 6071 Style.ColumnLimit = 35; 6072 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 6073 "void functionDecl(int A, int B, int C);"; 6074 Style.AllowAllArgumentsOnNextLine = false; 6075 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6076 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6077 " paramC);\n" 6078 "void functionDecl(int A, int B,\n" 6079 " int C);"), 6080 format(Input, Style)); 6081 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6082 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6083 " paramC);\n" 6084 "void functionDecl(int A, int B,\n" 6085 " int C);"), 6086 format(Input, Style)); 6087 // However, BAS_AlwaysBreak should take precedence over 6088 // AllowAllArgumentsOnNextLine. 6089 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6090 EXPECT_EQ(StringRef("functionCall(\n" 6091 " paramA, paramB, paramC);\n" 6092 "void functionDecl(\n" 6093 " int A, int B, int C);"), 6094 format(Input, Style)); 6095 6096 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 6097 // first argument. 6098 Style.AllowAllArgumentsOnNextLine = true; 6099 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6100 EXPECT_EQ(StringRef("functionCall(\n" 6101 " paramA, paramB, paramC);\n" 6102 "void functionDecl(\n" 6103 " int A, int B, int C);"), 6104 format(Input, Style)); 6105 // It wouldn't fit on one line with aligned parameters so this setting 6106 // doesn't change anything for BAS_Align. 6107 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6108 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6109 " paramC);\n" 6110 "void functionDecl(int A, int B,\n" 6111 " int C);"), 6112 format(Input, Style)); 6113 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6114 EXPECT_EQ(StringRef("functionCall(\n" 6115 " paramA, paramB, paramC);\n" 6116 "void functionDecl(\n" 6117 " int A, int B, int C);"), 6118 format(Input, Style)); 6119 } 6120 6121 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 6122 FormatStyle Style = getLLVMStyle(); 6123 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6124 6125 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6126 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 6127 getStyleWithColumns(Style, 45)); 6128 verifyFormat("Constructor() :\n" 6129 " Initializer(FitsOnTheLine) {}", 6130 getStyleWithColumns(Style, 44)); 6131 verifyFormat("Constructor() :\n" 6132 " Initializer(FitsOnTheLine) {}", 6133 getStyleWithColumns(Style, 43)); 6134 6135 verifyFormat("template <typename T>\n" 6136 "Constructor() : Initializer(FitsOnTheLine) {}", 6137 getStyleWithColumns(Style, 50)); 6138 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 6139 verifyFormat( 6140 "SomeClass::Constructor() :\n" 6141 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6142 Style); 6143 6144 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false; 6145 verifyFormat( 6146 "SomeClass::Constructor() :\n" 6147 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6148 Style); 6149 6150 verifyFormat( 6151 "SomeClass::Constructor() :\n" 6152 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6153 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6154 Style); 6155 verifyFormat( 6156 "SomeClass::Constructor() :\n" 6157 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6158 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6159 Style); 6160 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6161 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6162 " aaaaaaaaaa(aaaaaa) {}", 6163 Style); 6164 6165 verifyFormat("Constructor() :\n" 6166 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6167 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6168 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6169 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 6170 Style); 6171 6172 verifyFormat("Constructor() :\n" 6173 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6175 Style); 6176 6177 verifyFormat("Constructor(int Parameter = 0) :\n" 6178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6179 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 6180 Style); 6181 verifyFormat("Constructor() :\n" 6182 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6183 "}", 6184 getStyleWithColumns(Style, 60)); 6185 verifyFormat("Constructor() :\n" 6186 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6187 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 6188 Style); 6189 6190 // Here a line could be saved by splitting the second initializer onto two 6191 // lines, but that is not desirable. 6192 verifyFormat("Constructor() :\n" 6193 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6194 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6195 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6196 Style); 6197 6198 FormatStyle OnePerLine = Style; 6199 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 6200 OnePerLine.AllowAllConstructorInitializersOnNextLine = false; 6201 verifyFormat("SomeClass::Constructor() :\n" 6202 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6203 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6204 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6205 OnePerLine); 6206 verifyFormat("SomeClass::Constructor() :\n" 6207 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6208 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6209 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6210 OnePerLine); 6211 verifyFormat("MyClass::MyClass(int var) :\n" 6212 " some_var_(var), // 4 space indent\n" 6213 " some_other_var_(var + 1) { // lined up\n" 6214 "}", 6215 OnePerLine); 6216 verifyFormat("Constructor() :\n" 6217 " aaaaa(aaaaaa),\n" 6218 " aaaaa(aaaaaa),\n" 6219 " aaaaa(aaaaaa),\n" 6220 " aaaaa(aaaaaa),\n" 6221 " aaaaa(aaaaaa) {}", 6222 OnePerLine); 6223 verifyFormat("Constructor() :\n" 6224 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6225 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6226 OnePerLine); 6227 OnePerLine.BinPackParameters = false; 6228 verifyFormat("Constructor() :\n" 6229 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6230 " aaaaaaaaaaa().aaa(),\n" 6231 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6232 OnePerLine); 6233 OnePerLine.ColumnLimit = 60; 6234 verifyFormat("Constructor() :\n" 6235 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6236 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6237 OnePerLine); 6238 6239 EXPECT_EQ("Constructor() :\n" 6240 " // Comment forcing unwanted break.\n" 6241 " aaaa(aaaa) {}", 6242 format("Constructor() :\n" 6243 " // Comment forcing unwanted break.\n" 6244 " aaaa(aaaa) {}", 6245 Style)); 6246 6247 Style.ColumnLimit = 0; 6248 verifyFormat("SomeClass::Constructor() :\n" 6249 " a(a) {}", 6250 Style); 6251 verifyFormat("SomeClass::Constructor() noexcept :\n" 6252 " a(a) {}", 6253 Style); 6254 verifyFormat("SomeClass::Constructor() :\n" 6255 " a(a), b(b), c(c) {}", 6256 Style); 6257 verifyFormat("SomeClass::Constructor() :\n" 6258 " a(a) {\n" 6259 " foo();\n" 6260 " bar();\n" 6261 "}", 6262 Style); 6263 6264 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6265 verifyFormat("SomeClass::Constructor() :\n" 6266 " a(a), b(b), c(c) {\n" 6267 "}", 6268 Style); 6269 verifyFormat("SomeClass::Constructor() :\n" 6270 " a(a) {\n" 6271 "}", 6272 Style); 6273 6274 Style.ColumnLimit = 80; 6275 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6276 Style.ConstructorInitializerIndentWidth = 2; 6277 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 6278 verifyFormat("SomeClass::Constructor() :\n" 6279 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6280 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 6281 Style); 6282 6283 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 6284 // well 6285 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 6286 verifyFormat( 6287 "class SomeClass\n" 6288 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6289 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6290 Style); 6291 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 6292 verifyFormat( 6293 "class SomeClass\n" 6294 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6295 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6296 Style); 6297 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 6298 verifyFormat( 6299 "class SomeClass :\n" 6300 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6301 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6302 Style); 6303 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 6304 verifyFormat( 6305 "class SomeClass\n" 6306 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6307 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6308 Style); 6309 } 6310 6311 #ifndef EXPENSIVE_CHECKS 6312 // Expensive checks enables libstdc++ checking which includes validating the 6313 // state of ranges used in std::priority_queue - this blows out the 6314 // runtime/scalability of the function and makes this test unacceptably slow. 6315 TEST_F(FormatTest, MemoizationTests) { 6316 // This breaks if the memoization lookup does not take \c Indent and 6317 // \c LastSpace into account. 6318 verifyFormat( 6319 "extern CFRunLoopTimerRef\n" 6320 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 6321 " CFTimeInterval interval, CFOptionFlags flags,\n" 6322 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 6323 " CFRunLoopTimerContext *context) {}"); 6324 6325 // Deep nesting somewhat works around our memoization. 6326 verifyFormat( 6327 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6328 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6329 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6330 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6331 " aaaaa())))))))))))))))))))))))))))))))))))))));", 6332 getLLVMStyleWithColumns(65)); 6333 verifyFormat( 6334 "aaaaa(\n" 6335 " aaaaa,\n" 6336 " aaaaa(\n" 6337 " aaaaa,\n" 6338 " aaaaa(\n" 6339 " aaaaa,\n" 6340 " aaaaa(\n" 6341 " aaaaa,\n" 6342 " aaaaa(\n" 6343 " aaaaa,\n" 6344 " aaaaa(\n" 6345 " aaaaa,\n" 6346 " aaaaa(\n" 6347 " aaaaa,\n" 6348 " aaaaa(\n" 6349 " aaaaa,\n" 6350 " aaaaa(\n" 6351 " aaaaa,\n" 6352 " aaaaa(\n" 6353 " aaaaa,\n" 6354 " aaaaa(\n" 6355 " aaaaa,\n" 6356 " aaaaa(\n" 6357 " aaaaa,\n" 6358 " aaaaa))))))))))));", 6359 getLLVMStyleWithColumns(65)); 6360 verifyFormat( 6361 "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" 6362 " a),\n" 6363 " a),\n" 6364 " a),\n" 6365 " a),\n" 6366 " a),\n" 6367 " a),\n" 6368 " a),\n" 6369 " a),\n" 6370 " a),\n" 6371 " a),\n" 6372 " a),\n" 6373 " a),\n" 6374 " a),\n" 6375 " a),\n" 6376 " a),\n" 6377 " a),\n" 6378 " a)", 6379 getLLVMStyleWithColumns(65)); 6380 6381 // This test takes VERY long when memoization is broken. 6382 FormatStyle OnePerLine = getLLVMStyle(); 6383 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 6384 OnePerLine.BinPackParameters = false; 6385 std::string input = "Constructor()\n" 6386 " : aaaa(a,\n"; 6387 for (unsigned i = 0, e = 80; i != e; ++i) { 6388 input += " a,\n"; 6389 } 6390 input += " a) {}"; 6391 verifyFormat(input, OnePerLine); 6392 } 6393 #endif 6394 6395 TEST_F(FormatTest, BreaksAsHighAsPossible) { 6396 verifyFormat( 6397 "void f() {\n" 6398 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 6399 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 6400 " f();\n" 6401 "}"); 6402 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 6403 " Intervals[i - 1].getRange().getLast()) {\n}"); 6404 } 6405 6406 TEST_F(FormatTest, BreaksFunctionDeclarations) { 6407 // Principially, we break function declarations in a certain order: 6408 // 1) break amongst arguments. 6409 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 6410 " Cccccccccccccc cccccccccccccc);"); 6411 verifyFormat("template <class TemplateIt>\n" 6412 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 6413 " TemplateIt *stop) {}"); 6414 6415 // 2) break after return type. 6416 verifyFormat( 6417 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6418 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 6419 getGoogleStyle()); 6420 6421 // 3) break after (. 6422 verifyFormat( 6423 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 6424 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 6425 getGoogleStyle()); 6426 6427 // 4) break before after nested name specifiers. 6428 verifyFormat( 6429 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6430 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 6431 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 6432 getGoogleStyle()); 6433 6434 // However, there are exceptions, if a sufficient amount of lines can be 6435 // saved. 6436 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 6437 // more adjusting. 6438 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6439 " Cccccccccccccc cccccccccc,\n" 6440 " Cccccccccccccc cccccccccc,\n" 6441 " Cccccccccccccc cccccccccc,\n" 6442 " Cccccccccccccc cccccccccc);"); 6443 verifyFormat( 6444 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6445 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6446 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6447 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 6448 getGoogleStyle()); 6449 verifyFormat( 6450 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6451 " Cccccccccccccc cccccccccc,\n" 6452 " Cccccccccccccc cccccccccc,\n" 6453 " Cccccccccccccc cccccccccc,\n" 6454 " Cccccccccccccc cccccccccc,\n" 6455 " Cccccccccccccc cccccccccc,\n" 6456 " Cccccccccccccc cccccccccc);"); 6457 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6458 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6459 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6460 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6461 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 6462 6463 // Break after multi-line parameters. 6464 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6465 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6466 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6467 " bbbb bbbb);"); 6468 verifyFormat("void SomeLoooooooooooongFunction(\n" 6469 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6470 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6471 " int bbbbbbbbbbbbb);"); 6472 6473 // Treat overloaded operators like other functions. 6474 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6475 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 6476 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6477 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 6478 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6479 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 6480 verifyGoogleFormat( 6481 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 6482 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6483 verifyGoogleFormat( 6484 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 6485 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6486 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6487 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6488 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 6489 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6490 verifyGoogleFormat( 6491 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 6492 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6493 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 6494 verifyGoogleFormat("template <typename T>\n" 6495 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6496 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 6497 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 6498 6499 FormatStyle Style = getLLVMStyle(); 6500 Style.PointerAlignment = FormatStyle::PAS_Left; 6501 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6502 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 6503 Style); 6504 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 6505 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6506 Style); 6507 } 6508 6509 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 6510 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 6511 // Prefer keeping `::` followed by `operator` together. 6512 EXPECT_EQ("const aaaa::bbbbbbb &\n" 6513 "ccccccccc::operator++() {\n" 6514 " stuff();\n" 6515 "}", 6516 format("const aaaa::bbbbbbb\n" 6517 "&ccccccccc::operator++() { stuff(); }", 6518 getLLVMStyleWithColumns(40))); 6519 } 6520 6521 TEST_F(FormatTest, TrailingReturnType) { 6522 verifyFormat("auto foo() -> int;\n"); 6523 // correct trailing return type spacing 6524 verifyFormat("auto operator->() -> int;\n"); 6525 verifyFormat("auto operator++(int) -> int;\n"); 6526 6527 verifyFormat("struct S {\n" 6528 " auto bar() const -> int;\n" 6529 "};"); 6530 verifyFormat("template <size_t Order, typename T>\n" 6531 "auto load_img(const std::string &filename)\n" 6532 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 6533 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 6534 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 6535 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 6536 verifyFormat("template <typename T>\n" 6537 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 6538 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 6539 6540 // Not trailing return types. 6541 verifyFormat("void f() { auto a = b->c(); }"); 6542 } 6543 6544 TEST_F(FormatTest, DeductionGuides) { 6545 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 6546 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 6547 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 6548 verifyFormat( 6549 "template <class... T>\n" 6550 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 6551 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 6552 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 6553 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 6554 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 6555 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 6556 verifyFormat("template <class T> x() -> x<1>;"); 6557 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 6558 6559 // Ensure not deduction guides. 6560 verifyFormat("c()->f<int>();"); 6561 verifyFormat("x()->foo<1>;"); 6562 verifyFormat("x = p->foo<3>();"); 6563 verifyFormat("x()->x<1>();"); 6564 verifyFormat("x()->x<1>;"); 6565 } 6566 6567 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 6568 // Avoid breaking before trailing 'const' or other trailing annotations, if 6569 // they are not function-like. 6570 FormatStyle Style = getGoogleStyle(); 6571 Style.ColumnLimit = 47; 6572 verifyFormat("void someLongFunction(\n" 6573 " int someLoooooooooooooongParameter) const {\n}", 6574 getLLVMStyleWithColumns(47)); 6575 verifyFormat("LoooooongReturnType\n" 6576 "someLoooooooongFunction() const {}", 6577 getLLVMStyleWithColumns(47)); 6578 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 6579 " const {}", 6580 Style); 6581 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6582 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 6583 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6584 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 6585 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6586 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 6587 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 6588 " aaaaaaaaaaa aaaaa) const override;"); 6589 verifyGoogleFormat( 6590 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6591 " const override;"); 6592 6593 // Even if the first parameter has to be wrapped. 6594 verifyFormat("void someLongFunction(\n" 6595 " int someLongParameter) const {}", 6596 getLLVMStyleWithColumns(46)); 6597 verifyFormat("void someLongFunction(\n" 6598 " int someLongParameter) const {}", 6599 Style); 6600 verifyFormat("void someLongFunction(\n" 6601 " int someLongParameter) override {}", 6602 Style); 6603 verifyFormat("void someLongFunction(\n" 6604 " int someLongParameter) OVERRIDE {}", 6605 Style); 6606 verifyFormat("void someLongFunction(\n" 6607 " int someLongParameter) final {}", 6608 Style); 6609 verifyFormat("void someLongFunction(\n" 6610 " int someLongParameter) FINAL {}", 6611 Style); 6612 verifyFormat("void someLongFunction(\n" 6613 " int parameter) const override {}", 6614 Style); 6615 6616 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 6617 verifyFormat("void someLongFunction(\n" 6618 " int someLongParameter) const\n" 6619 "{\n" 6620 "}", 6621 Style); 6622 6623 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 6624 verifyFormat("void someLongFunction(\n" 6625 " int someLongParameter) const\n" 6626 " {\n" 6627 " }", 6628 Style); 6629 6630 // Unless these are unknown annotations. 6631 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 6632 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6633 " LONG_AND_UGLY_ANNOTATION;"); 6634 6635 // Breaking before function-like trailing annotations is fine to keep them 6636 // close to their arguments. 6637 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6638 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6639 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6640 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6641 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6642 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 6643 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 6644 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 6645 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 6646 6647 verifyFormat( 6648 "void aaaaaaaaaaaaaaaaaa()\n" 6649 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 6650 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 6651 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6652 " __attribute__((unused));"); 6653 verifyGoogleFormat( 6654 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6655 " GUARDED_BY(aaaaaaaaaaaa);"); 6656 verifyGoogleFormat( 6657 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6658 " GUARDED_BY(aaaaaaaaaaaa);"); 6659 verifyGoogleFormat( 6660 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6661 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6662 verifyGoogleFormat( 6663 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6664 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6665 } 6666 6667 TEST_F(FormatTest, FunctionAnnotations) { 6668 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6669 "int OldFunction(const string ¶meter) {}"); 6670 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6671 "string OldFunction(const string ¶meter) {}"); 6672 verifyFormat("template <typename T>\n" 6673 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6674 "string OldFunction(const string ¶meter) {}"); 6675 6676 // Not function annotations. 6677 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6678 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 6679 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 6680 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 6681 verifyFormat("MACRO(abc).function() // wrap\n" 6682 " << abc;"); 6683 verifyFormat("MACRO(abc)->function() // wrap\n" 6684 " << abc;"); 6685 verifyFormat("MACRO(abc)::function() // wrap\n" 6686 " << abc;"); 6687 } 6688 6689 TEST_F(FormatTest, BreaksDesireably) { 6690 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6691 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6692 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 6693 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6694 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 6695 "}"); 6696 6697 verifyFormat( 6698 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6699 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6700 6701 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6702 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6703 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6704 6705 verifyFormat( 6706 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6707 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6708 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6709 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 6711 6712 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6713 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6714 6715 verifyFormat( 6716 "void f() {\n" 6717 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6718 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6719 "}"); 6720 verifyFormat( 6721 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6723 verifyFormat( 6724 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6725 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6726 verifyFormat( 6727 "aaaaaa(aaa,\n" 6728 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6730 " aaaa);"); 6731 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6732 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6733 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6734 6735 // Indent consistently independent of call expression and unary operator. 6736 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6737 " dddddddddddddddddddddddddddddd));"); 6738 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6739 " dddddddddddddddddddddddddddddd));"); 6740 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 6741 " dddddddddddddddddddddddddddddd));"); 6742 6743 // This test case breaks on an incorrect memoization, i.e. an optimization not 6744 // taking into account the StopAt value. 6745 verifyFormat( 6746 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6747 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6748 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6749 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6750 6751 verifyFormat("{\n {\n {\n" 6752 " Annotation.SpaceRequiredBefore =\n" 6753 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 6754 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 6755 " }\n }\n}"); 6756 6757 // Break on an outer level if there was a break on an inner level. 6758 EXPECT_EQ("f(g(h(a, // comment\n" 6759 " b, c),\n" 6760 " d, e),\n" 6761 " x, y);", 6762 format("f(g(h(a, // comment\n" 6763 " b, c), d, e), x, y);")); 6764 6765 // Prefer breaking similar line breaks. 6766 verifyFormat( 6767 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 6768 " NSTrackingMouseEnteredAndExited |\n" 6769 " NSTrackingActiveAlways;"); 6770 } 6771 6772 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 6773 FormatStyle NoBinPacking = getGoogleStyle(); 6774 NoBinPacking.BinPackParameters = false; 6775 NoBinPacking.BinPackArguments = true; 6776 verifyFormat("void f() {\n" 6777 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 6778 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6779 "}", 6780 NoBinPacking); 6781 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 6782 " int aaaaaaaaaaaaaaaaaaaa,\n" 6783 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6784 NoBinPacking); 6785 6786 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 6787 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6788 " vector<int> bbbbbbbbbbbbbbb);", 6789 NoBinPacking); 6790 // FIXME: This behavior difference is probably not wanted. However, currently 6791 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 6792 // template arguments from BreakBeforeParameter being set because of the 6793 // one-per-line formatting. 6794 verifyFormat( 6795 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 6796 " aaaaaaaaaa> aaaaaaaaaa);", 6797 NoBinPacking); 6798 verifyFormat( 6799 "void fffffffffff(\n" 6800 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 6801 " aaaaaaaaaa);"); 6802 } 6803 6804 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 6805 FormatStyle NoBinPacking = getGoogleStyle(); 6806 NoBinPacking.BinPackParameters = false; 6807 NoBinPacking.BinPackArguments = false; 6808 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 6809 " aaaaaaaaaaaaaaaaaaaa,\n" 6810 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 6811 NoBinPacking); 6812 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 6813 " aaaaaaaaaaaaa,\n" 6814 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 6815 NoBinPacking); 6816 verifyFormat( 6817 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6818 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6819 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6820 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6821 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 6822 NoBinPacking); 6823 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 6824 " .aaaaaaaaaaaaaaaaaa();", 6825 NoBinPacking); 6826 verifyFormat("void f() {\n" 6827 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6828 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 6829 "}", 6830 NoBinPacking); 6831 6832 verifyFormat( 6833 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6834 " aaaaaaaaaaaa,\n" 6835 " aaaaaaaaaaaa);", 6836 NoBinPacking); 6837 verifyFormat( 6838 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 6839 " ddddddddddddddddddddddddddddd),\n" 6840 " test);", 6841 NoBinPacking); 6842 6843 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 6844 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 6845 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 6846 " aaaaaaaaaaaaaaaaaa;", 6847 NoBinPacking); 6848 verifyFormat("a(\"a\"\n" 6849 " \"a\",\n" 6850 " a);"); 6851 6852 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 6853 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 6854 " aaaaaaaaa,\n" 6855 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6856 NoBinPacking); 6857 verifyFormat( 6858 "void f() {\n" 6859 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 6860 " .aaaaaaa();\n" 6861 "}", 6862 NoBinPacking); 6863 verifyFormat( 6864 "template <class SomeType, class SomeOtherType>\n" 6865 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 6866 NoBinPacking); 6867 } 6868 6869 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 6870 FormatStyle Style = getLLVMStyleWithColumns(15); 6871 Style.ExperimentalAutoDetectBinPacking = true; 6872 EXPECT_EQ("aaa(aaaa,\n" 6873 " aaaa,\n" 6874 " aaaa);\n" 6875 "aaa(aaaa,\n" 6876 " aaaa,\n" 6877 " aaaa);", 6878 format("aaa(aaaa,\n" // one-per-line 6879 " aaaa,\n" 6880 " aaaa );\n" 6881 "aaa(aaaa, aaaa, aaaa);", // inconclusive 6882 Style)); 6883 EXPECT_EQ("aaa(aaaa, aaaa,\n" 6884 " aaaa);\n" 6885 "aaa(aaaa, aaaa,\n" 6886 " aaaa);", 6887 format("aaa(aaaa, aaaa,\n" // bin-packed 6888 " aaaa );\n" 6889 "aaa(aaaa, aaaa, aaaa);", // inconclusive 6890 Style)); 6891 } 6892 6893 TEST_F(FormatTest, FormatsBuilderPattern) { 6894 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 6895 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 6896 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 6897 " .StartsWith(\".init\", ORDER_INIT)\n" 6898 " .StartsWith(\".fini\", ORDER_FINI)\n" 6899 " .StartsWith(\".hash\", ORDER_HASH)\n" 6900 " .Default(ORDER_TEXT);\n"); 6901 6902 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 6903 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 6904 verifyFormat("aaaaaaa->aaaaaaa\n" 6905 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6906 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6907 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 6908 verifyFormat( 6909 "aaaaaaa->aaaaaaa\n" 6910 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6911 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 6912 verifyFormat( 6913 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 6914 " aaaaaaaaaaaaaa);"); 6915 verifyFormat( 6916 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 6917 " aaaaaa->aaaaaaaaaaaa()\n" 6918 " ->aaaaaaaaaaaaaaaa(\n" 6919 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6920 " ->aaaaaaaaaaaaaaaaa();"); 6921 verifyGoogleFormat( 6922 "void f() {\n" 6923 " someo->Add((new util::filetools::Handler(dir))\n" 6924 " ->OnEvent1(NewPermanentCallback(\n" 6925 " this, &HandlerHolderClass::EventHandlerCBA))\n" 6926 " ->OnEvent2(NewPermanentCallback(\n" 6927 " this, &HandlerHolderClass::EventHandlerCBB))\n" 6928 " ->OnEvent3(NewPermanentCallback(\n" 6929 " this, &HandlerHolderClass::EventHandlerCBC))\n" 6930 " ->OnEvent5(NewPermanentCallback(\n" 6931 " this, &HandlerHolderClass::EventHandlerCBD))\n" 6932 " ->OnEvent6(NewPermanentCallback(\n" 6933 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 6934 "}"); 6935 6936 verifyFormat( 6937 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 6938 verifyFormat("aaaaaaaaaaaaaaa()\n" 6939 " .aaaaaaaaaaaaaaa()\n" 6940 " .aaaaaaaaaaaaaaa()\n" 6941 " .aaaaaaaaaaaaaaa()\n" 6942 " .aaaaaaaaaaaaaaa();"); 6943 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 6944 " .aaaaaaaaaaaaaaa()\n" 6945 " .aaaaaaaaaaaaaaa()\n" 6946 " .aaaaaaaaaaaaaaa();"); 6947 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 6948 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 6949 " .aaaaaaaaaaaaaaa();"); 6950 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 6951 " ->aaaaaaaaaaaaaae(0)\n" 6952 " ->aaaaaaaaaaaaaaa();"); 6953 6954 // Don't linewrap after very short segments. 6955 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6956 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6957 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6958 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6959 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6960 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6961 verifyFormat("aaa()\n" 6962 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6963 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6964 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6965 6966 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 6967 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6968 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 6969 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 6970 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6971 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 6972 6973 // Prefer not to break after empty parentheses. 6974 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 6975 " First->LastNewlineOffset);"); 6976 6977 // Prefer not to create "hanging" indents. 6978 verifyFormat( 6979 "return !soooooooooooooome_map\n" 6980 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6981 " .second;"); 6982 verifyFormat( 6983 "return aaaaaaaaaaaaaaaa\n" 6984 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 6985 " .aaaa(aaaaaaaaaaaaaa);"); 6986 // No hanging indent here. 6987 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 6988 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6989 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 6990 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6991 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 6992 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6993 getLLVMStyleWithColumns(60)); 6994 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 6995 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 6996 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6997 getLLVMStyleWithColumns(59)); 6998 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6999 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7000 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7001 7002 // Dont break if only closing statements before member call 7003 verifyFormat("test() {\n" 7004 " ([]() -> {\n" 7005 " int b = 32;\n" 7006 " return 3;\n" 7007 " }).foo();\n" 7008 "}"); 7009 verifyFormat("test() {\n" 7010 " (\n" 7011 " []() -> {\n" 7012 " int b = 32;\n" 7013 " return 3;\n" 7014 " },\n" 7015 " foo, bar)\n" 7016 " .foo();\n" 7017 "}"); 7018 verifyFormat("test() {\n" 7019 " ([]() -> {\n" 7020 " int b = 32;\n" 7021 " return 3;\n" 7022 " })\n" 7023 " .foo()\n" 7024 " .bar();\n" 7025 "}"); 7026 verifyFormat("test() {\n" 7027 " ([]() -> {\n" 7028 " int b = 32;\n" 7029 " return 3;\n" 7030 " })\n" 7031 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 7032 " \"bbbb\");\n" 7033 "}", 7034 getLLVMStyleWithColumns(30)); 7035 } 7036 7037 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 7038 verifyFormat( 7039 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7040 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 7041 verifyFormat( 7042 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 7043 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 7044 7045 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7046 " ccccccccccccccccccccccccc) {\n}"); 7047 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 7048 " ccccccccccccccccccccccccc) {\n}"); 7049 7050 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7051 " ccccccccccccccccccccccccc) {\n}"); 7052 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 7053 " ccccccccccccccccccccccccc) {\n}"); 7054 7055 verifyFormat( 7056 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 7057 " ccccccccccccccccccccccccc) {\n}"); 7058 verifyFormat( 7059 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 7060 " ccccccccccccccccccccccccc) {\n}"); 7061 7062 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 7063 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 7064 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 7065 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7066 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 7067 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 7068 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 7069 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7070 7071 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 7072 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 7073 " aaaaaaaaaaaaaaa != aa) {\n}"); 7074 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 7075 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 7076 " aaaaaaaaaaaaaaa != aa) {\n}"); 7077 } 7078 7079 TEST_F(FormatTest, BreaksAfterAssignments) { 7080 verifyFormat( 7081 "unsigned Cost =\n" 7082 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 7083 " SI->getPointerAddressSpaceee());\n"); 7084 verifyFormat( 7085 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 7086 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 7087 7088 verifyFormat( 7089 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 7090 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 7091 verifyFormat("unsigned OriginalStartColumn =\n" 7092 " SourceMgr.getSpellingColumnNumber(\n" 7093 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 7094 " 1;"); 7095 } 7096 7097 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 7098 FormatStyle Style = getLLVMStyle(); 7099 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7100 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 7101 Style); 7102 7103 Style.PenaltyBreakAssignment = 20; 7104 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7105 " cccccccccccccccccccccccccc;", 7106 Style); 7107 } 7108 7109 TEST_F(FormatTest, AlignsAfterAssignments) { 7110 verifyFormat( 7111 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7112 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7113 verifyFormat( 7114 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7115 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7116 verifyFormat( 7117 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7118 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7119 verifyFormat( 7120 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7121 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7122 verifyFormat( 7123 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7124 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7125 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 7126 } 7127 7128 TEST_F(FormatTest, AlignsAfterReturn) { 7129 verifyFormat( 7130 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7131 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7132 verifyFormat( 7133 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7134 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7135 verifyFormat( 7136 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7137 " aaaaaaaaaaaaaaaaaaaaaa();"); 7138 verifyFormat( 7139 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7140 " aaaaaaaaaaaaaaaaaaaaaa());"); 7141 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7142 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7143 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7144 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 7145 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7146 verifyFormat("return\n" 7147 " // true if code is one of a or b.\n" 7148 " code == a || code == b;"); 7149 } 7150 7151 TEST_F(FormatTest, AlignsAfterOpenBracket) { 7152 verifyFormat( 7153 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7154 " aaaaaaaaa aaaaaaa) {}"); 7155 verifyFormat( 7156 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7157 " aaaaaaaaaaa aaaaaaaaa);"); 7158 verifyFormat( 7159 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7160 " aaaaaaaaaaaaaaaaaaaaa));"); 7161 FormatStyle Style = getLLVMStyle(); 7162 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7163 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7164 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 7165 Style); 7166 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7167 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 7168 Style); 7169 verifyFormat("SomeLongVariableName->someFunction(\n" 7170 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 7171 Style); 7172 verifyFormat( 7173 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7174 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7175 Style); 7176 verifyFormat( 7177 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7178 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7179 Style); 7180 verifyFormat( 7181 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7182 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7183 Style); 7184 7185 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 7186 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 7187 " b));", 7188 Style); 7189 7190 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7191 Style.BinPackArguments = false; 7192 Style.BinPackParameters = false; 7193 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7194 " aaaaaaaaaaa aaaaaaaa,\n" 7195 " aaaaaaaaa aaaaaaa,\n" 7196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7197 Style); 7198 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7199 " aaaaaaaaaaa aaaaaaaaa,\n" 7200 " aaaaaaaaaaa aaaaaaaaa,\n" 7201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7202 Style); 7203 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 7204 " aaaaaaaaaaaaaaa,\n" 7205 " aaaaaaaaaaaaaaaaaaaaa,\n" 7206 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7207 Style); 7208 verifyFormat( 7209 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 7210 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7211 Style); 7212 verifyFormat( 7213 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 7214 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7215 Style); 7216 verifyFormat( 7217 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7218 " aaaaaaaaaaaaaaaaaaaaa(\n" 7219 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 7220 " aaaaaaaaaaaaaaaa);", 7221 Style); 7222 verifyFormat( 7223 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7224 " aaaaaaaaaaaaaaaaaaaaa(\n" 7225 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 7226 " aaaaaaaaaaaaaaaa);", 7227 Style); 7228 } 7229 7230 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 7231 FormatStyle Style = getLLVMStyleWithColumns(40); 7232 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7233 " bbbbbbbbbbbbbbbbbbbbbb);", 7234 Style); 7235 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 7236 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7237 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7238 " bbbbbbbbbbbbbbbbbbbbbb);", 7239 Style); 7240 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7241 Style.AlignOperands = FormatStyle::OAS_Align; 7242 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7243 " bbbbbbbbbbbbbbbbbbbbbb);", 7244 Style); 7245 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7246 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7247 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7248 " bbbbbbbbbbbbbbbbbbbbbb);", 7249 Style); 7250 } 7251 7252 TEST_F(FormatTest, BreaksConditionalExpressions) { 7253 verifyFormat( 7254 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7255 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7256 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7257 verifyFormat( 7258 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7259 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7260 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7261 verifyFormat( 7262 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7263 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7264 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 7265 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7266 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7267 verifyFormat( 7268 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 7269 " : aaaaaaaaaaaaa);"); 7270 verifyFormat( 7271 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7272 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7273 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7274 " aaaaaaaaaaaaa);"); 7275 verifyFormat( 7276 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7277 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7278 " aaaaaaaaaaaaa);"); 7279 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7280 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7281 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7282 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7283 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7284 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7285 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7286 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7287 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7288 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7289 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7290 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7291 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7292 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7293 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7294 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7295 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7296 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7297 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7298 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7299 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7300 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7301 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7302 " : aaaaaaaaaaaaaaaa;"); 7303 verifyFormat( 7304 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7305 " ? aaaaaaaaaaaaaaa\n" 7306 " : aaaaaaaaaaaaaaa;"); 7307 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7308 " aaaaaaaaa\n" 7309 " ? b\n" 7310 " : c);"); 7311 verifyFormat("return aaaa == bbbb\n" 7312 " // comment\n" 7313 " ? aaaa\n" 7314 " : bbbb;"); 7315 verifyFormat("unsigned Indent =\n" 7316 " format(TheLine.First,\n" 7317 " IndentForLevel[TheLine.Level] >= 0\n" 7318 " ? IndentForLevel[TheLine.Level]\n" 7319 " : TheLine * 2,\n" 7320 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7321 getLLVMStyleWithColumns(60)); 7322 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7323 " ? aaaaaaaaaaaaaaa\n" 7324 " : bbbbbbbbbbbbbbb //\n" 7325 " ? ccccccccccccccc\n" 7326 " : ddddddddddddddd;"); 7327 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7328 " ? aaaaaaaaaaaaaaa\n" 7329 " : (bbbbbbbbbbbbbbb //\n" 7330 " ? ccccccccccccccc\n" 7331 " : ddddddddddddddd);"); 7332 verifyFormat( 7333 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7334 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7335 " aaaaaaaaaaaaaaaaaaaaa +\n" 7336 " aaaaaaaaaaaaaaaaaaaaa\n" 7337 " : aaaaaaaaaa;"); 7338 verifyFormat( 7339 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7340 " : aaaaaaaaaaaaaaaaaaaaaa\n" 7341 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7342 7343 FormatStyle NoBinPacking = getLLVMStyle(); 7344 NoBinPacking.BinPackArguments = false; 7345 verifyFormat( 7346 "void f() {\n" 7347 " g(aaa,\n" 7348 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7349 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7350 " ? aaaaaaaaaaaaaaa\n" 7351 " : aaaaaaaaaaaaaaa);\n" 7352 "}", 7353 NoBinPacking); 7354 verifyFormat( 7355 "void f() {\n" 7356 " g(aaa,\n" 7357 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7358 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7359 " ?: aaaaaaaaaaaaaaa);\n" 7360 "}", 7361 NoBinPacking); 7362 7363 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 7364 " // comment.\n" 7365 " ccccccccccccccccccccccccccccccccccccccc\n" 7366 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7367 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 7368 7369 // Assignments in conditional expressions. Apparently not uncommon :-(. 7370 verifyFormat("return a != b\n" 7371 " // comment\n" 7372 " ? a = b\n" 7373 " : a = b;"); 7374 verifyFormat("return a != b\n" 7375 " // comment\n" 7376 " ? a = a != b\n" 7377 " // comment\n" 7378 " ? a = b\n" 7379 " : a\n" 7380 " : a;\n"); 7381 verifyFormat("return a != b\n" 7382 " // comment\n" 7383 " ? a\n" 7384 " : a = a != b\n" 7385 " // comment\n" 7386 " ? a = b\n" 7387 " : a;"); 7388 7389 // Chained conditionals 7390 FormatStyle Style = getLLVMStyle(); 7391 Style.ColumnLimit = 70; 7392 Style.AlignOperands = FormatStyle::OAS_Align; 7393 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7394 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7395 " : 3333333333333333;", 7396 Style); 7397 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7398 " : bbbbbbbbbb ? 2222222222222222\n" 7399 " : 3333333333333333;", 7400 Style); 7401 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 7402 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 7403 " : 3333333333333333;", 7404 Style); 7405 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7406 " : bbbbbbbbbbbbbb ? 222222\n" 7407 " : 333333;", 7408 Style); 7409 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7410 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7411 " : cccccccccccccc ? 3333333333333333\n" 7412 " : 4444444444444444;", 7413 Style); 7414 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 7415 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7416 " : 3333333333333333;", 7417 Style); 7418 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7419 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7420 " : (aaa ? bbb : ccc);", 7421 Style); 7422 verifyFormat( 7423 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7424 " : cccccccccccccccccc)\n" 7425 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7426 " : 3333333333333333;", 7427 Style); 7428 verifyFormat( 7429 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7430 " : cccccccccccccccccc)\n" 7431 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7432 " : 3333333333333333;", 7433 Style); 7434 verifyFormat( 7435 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7436 " : dddddddddddddddddd)\n" 7437 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7438 " : 3333333333333333;", 7439 Style); 7440 verifyFormat( 7441 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7442 " : dddddddddddddddddd)\n" 7443 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7444 " : 3333333333333333;", 7445 Style); 7446 verifyFormat( 7447 "return aaaaaaaaa ? 1111111111111111\n" 7448 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7449 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7450 " : dddddddddddddddddd)\n", 7451 Style); 7452 verifyFormat( 7453 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7454 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7455 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7456 " : cccccccccccccccccc);", 7457 Style); 7458 verifyFormat( 7459 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7460 " : ccccccccccccccc ? dddddddddddddddddd\n" 7461 " : eeeeeeeeeeeeeeeeee)\n" 7462 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7463 " : 3333333333333333;", 7464 Style); 7465 verifyFormat( 7466 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7467 " : ccccccccccccccc ? dddddddddddddddddd\n" 7468 " : eeeeeeeeeeeeeeeeee)\n" 7469 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7470 " : 3333333333333333;", 7471 Style); 7472 verifyFormat( 7473 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7474 " : cccccccccccc ? dddddddddddddddddd\n" 7475 " : eeeeeeeeeeeeeeeeee)\n" 7476 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7477 " : 3333333333333333;", 7478 Style); 7479 verifyFormat( 7480 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7481 " : cccccccccccccccccc\n" 7482 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7483 " : 3333333333333333;", 7484 Style); 7485 verifyFormat( 7486 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7487 " : cccccccccccccccc ? dddddddddddddddddd\n" 7488 " : eeeeeeeeeeeeeeeeee\n" 7489 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7490 " : 3333333333333333;", 7491 Style); 7492 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 7493 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7494 " : cccccccccccccccccc ? dddddddddddddddddd\n" 7495 " : eeeeeeeeeeeeeeeeee)\n" 7496 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7497 " : 3333333333333333;", 7498 Style); 7499 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7500 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7501 " : cccccccccccccccc ? dddddddddddddddddd\n" 7502 " : eeeeeeeeeeeeeeeeee\n" 7503 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7504 " : 3333333333333333;", 7505 Style); 7506 7507 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7508 Style.BreakBeforeTernaryOperators = false; 7509 // FIXME: Aligning the question marks is weird given DontAlign. 7510 // Consider disabling this alignment in this case. Also check whether this 7511 // will render the adjustment from https://reviews.llvm.org/D82199 7512 // unnecessary. 7513 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 7514 " bbbb ? cccccccccccccccccc :\n" 7515 " ddddd;\n", 7516 Style); 7517 7518 EXPECT_EQ( 7519 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7520 " /*\n" 7521 " */\n" 7522 " function() {\n" 7523 " try {\n" 7524 " return JJJJJJJJJJJJJJ(\n" 7525 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7526 " }\n" 7527 " } :\n" 7528 " function() {};", 7529 format( 7530 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7531 " /*\n" 7532 " */\n" 7533 " function() {\n" 7534 " try {\n" 7535 " return JJJJJJJJJJJJJJ(\n" 7536 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7537 " }\n" 7538 " } :\n" 7539 " function() {};", 7540 getGoogleStyle(FormatStyle::LK_JavaScript))); 7541 } 7542 7543 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 7544 FormatStyle Style = getLLVMStyle(); 7545 Style.BreakBeforeTernaryOperators = false; 7546 Style.ColumnLimit = 70; 7547 verifyFormat( 7548 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7549 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7550 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7551 Style); 7552 verifyFormat( 7553 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7554 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7555 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7556 Style); 7557 verifyFormat( 7558 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7559 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7560 Style); 7561 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 7562 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7563 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7564 Style); 7565 verifyFormat( 7566 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 7567 " aaaaaaaaaaaaa);", 7568 Style); 7569 verifyFormat( 7570 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7571 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7572 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7573 " aaaaaaaaaaaaa);", 7574 Style); 7575 verifyFormat( 7576 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7577 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7578 " aaaaaaaaaaaaa);", 7579 Style); 7580 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7581 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7584 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7585 Style); 7586 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7587 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7588 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7589 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7590 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7591 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7592 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7593 Style); 7594 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7595 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 7596 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7597 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7598 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7599 Style); 7600 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7601 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7602 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7603 Style); 7604 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7605 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7606 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7607 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7608 Style); 7609 verifyFormat( 7610 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7611 " aaaaaaaaaaaaaaa :\n" 7612 " aaaaaaaaaaaaaaa;", 7613 Style); 7614 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7615 " aaaaaaaaa ?\n" 7616 " b :\n" 7617 " c);", 7618 Style); 7619 verifyFormat("unsigned Indent =\n" 7620 " format(TheLine.First,\n" 7621 " IndentForLevel[TheLine.Level] >= 0 ?\n" 7622 " IndentForLevel[TheLine.Level] :\n" 7623 " TheLine * 2,\n" 7624 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7625 Style); 7626 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7627 " aaaaaaaaaaaaaaa :\n" 7628 " bbbbbbbbbbbbbbb ? //\n" 7629 " ccccccccccccccc :\n" 7630 " ddddddddddddddd;", 7631 Style); 7632 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7633 " aaaaaaaaaaaaaaa :\n" 7634 " (bbbbbbbbbbbbbbb ? //\n" 7635 " ccccccccccccccc :\n" 7636 " ddddddddddddddd);", 7637 Style); 7638 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7639 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 7640 " ccccccccccccccccccccccccccc;", 7641 Style); 7642 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7643 " aaaaa :\n" 7644 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 7645 Style); 7646 7647 // Chained conditionals 7648 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7649 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7650 " 3333333333333333;", 7651 Style); 7652 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7653 " bbbbbbbbbb ? 2222222222222222 :\n" 7654 " 3333333333333333;", 7655 Style); 7656 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 7657 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7658 " 3333333333333333;", 7659 Style); 7660 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7661 " bbbbbbbbbbbbbbbb ? 222222 :\n" 7662 " 333333;", 7663 Style); 7664 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7665 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7666 " cccccccccccccccc ? 3333333333333333 :\n" 7667 " 4444444444444444;", 7668 Style); 7669 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 7670 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7671 " 3333333333333333;", 7672 Style); 7673 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7674 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7675 " (aaa ? bbb : ccc);", 7676 Style); 7677 verifyFormat( 7678 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7679 " cccccccccccccccccc) :\n" 7680 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7681 " 3333333333333333;", 7682 Style); 7683 verifyFormat( 7684 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7685 " cccccccccccccccccc) :\n" 7686 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7687 " 3333333333333333;", 7688 Style); 7689 verifyFormat( 7690 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7691 " dddddddddddddddddd) :\n" 7692 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7693 " 3333333333333333;", 7694 Style); 7695 verifyFormat( 7696 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7697 " dddddddddddddddddd) :\n" 7698 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7699 " 3333333333333333;", 7700 Style); 7701 verifyFormat( 7702 "return aaaaaaaaa ? 1111111111111111 :\n" 7703 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7704 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7705 " dddddddddddddddddd)\n", 7706 Style); 7707 verifyFormat( 7708 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7709 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7710 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7711 " cccccccccccccccccc);", 7712 Style); 7713 verifyFormat( 7714 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7715 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7716 " eeeeeeeeeeeeeeeeee) :\n" 7717 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7718 " 3333333333333333;", 7719 Style); 7720 verifyFormat( 7721 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7722 " ccccccccccccc ? dddddddddddddddddd :\n" 7723 " eeeeeeeeeeeeeeeeee) :\n" 7724 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7725 " 3333333333333333;", 7726 Style); 7727 verifyFormat( 7728 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7729 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7730 " eeeeeeeeeeeeeeeeee) :\n" 7731 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7732 " 3333333333333333;", 7733 Style); 7734 verifyFormat( 7735 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7736 " cccccccccccccccccc :\n" 7737 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7738 " 3333333333333333;", 7739 Style); 7740 verifyFormat( 7741 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7742 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7743 " eeeeeeeeeeeeeeeeee :\n" 7744 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7745 " 3333333333333333;", 7746 Style); 7747 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7748 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7749 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7750 " eeeeeeeeeeeeeeeeee) :\n" 7751 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7752 " 3333333333333333;", 7753 Style); 7754 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7755 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7756 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 7757 " eeeeeeeeeeeeeeeeee :\n" 7758 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7759 " 3333333333333333;", 7760 Style); 7761 } 7762 7763 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 7764 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 7765 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 7766 verifyFormat("bool a = true, b = false;"); 7767 7768 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7769 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 7770 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 7771 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 7772 verifyFormat( 7773 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 7774 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 7775 " d = e && f;"); 7776 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 7777 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 7778 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7779 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 7780 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 7781 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 7782 7783 FormatStyle Style = getGoogleStyle(); 7784 Style.PointerAlignment = FormatStyle::PAS_Left; 7785 Style.DerivePointerAlignment = false; 7786 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7787 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 7788 " *b = bbbbbbbbbbbbbbbbbbb;", 7789 Style); 7790 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7791 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 7792 Style); 7793 verifyFormat("vector<int*> a, b;", Style); 7794 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 7795 } 7796 7797 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 7798 verifyFormat("arr[foo ? bar : baz];"); 7799 verifyFormat("f()[foo ? bar : baz];"); 7800 verifyFormat("(a + b)[foo ? bar : baz];"); 7801 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 7802 } 7803 7804 TEST_F(FormatTest, AlignsStringLiterals) { 7805 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 7806 " \"short literal\");"); 7807 verifyFormat( 7808 "looooooooooooooooooooooooongFunction(\n" 7809 " \"short literal\"\n" 7810 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 7811 verifyFormat("someFunction(\"Always break between multi-line\"\n" 7812 " \" string literals\",\n" 7813 " and, other, parameters);"); 7814 EXPECT_EQ("fun + \"1243\" /* comment */\n" 7815 " \"5678\";", 7816 format("fun + \"1243\" /* comment */\n" 7817 " \"5678\";", 7818 getLLVMStyleWithColumns(28))); 7819 EXPECT_EQ( 7820 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7821 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 7822 " \"aaaaaaaaaaaaaaaa\";", 7823 format("aaaaaa =" 7824 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7825 "aaaaaaaaaaaaaaaaaaaaa\" " 7826 "\"aaaaaaaaaaaaaaaa\";")); 7827 verifyFormat("a = a + \"a\"\n" 7828 " \"a\"\n" 7829 " \"a\";"); 7830 verifyFormat("f(\"a\", \"b\"\n" 7831 " \"c\");"); 7832 7833 verifyFormat( 7834 "#define LL_FORMAT \"ll\"\n" 7835 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 7836 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 7837 7838 verifyFormat("#define A(X) \\\n" 7839 " \"aaaaa\" #X \"bbbbbb\" \\\n" 7840 " \"ccccc\"", 7841 getLLVMStyleWithColumns(23)); 7842 verifyFormat("#define A \"def\"\n" 7843 "f(\"abc\" A \"ghi\"\n" 7844 " \"jkl\");"); 7845 7846 verifyFormat("f(L\"a\"\n" 7847 " L\"b\");"); 7848 verifyFormat("#define A(X) \\\n" 7849 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 7850 " L\"ccccc\"", 7851 getLLVMStyleWithColumns(25)); 7852 7853 verifyFormat("f(@\"a\"\n" 7854 " @\"b\");"); 7855 verifyFormat("NSString s = @\"a\"\n" 7856 " @\"b\"\n" 7857 " @\"c\";"); 7858 verifyFormat("NSString s = @\"a\"\n" 7859 " \"b\"\n" 7860 " \"c\";"); 7861 } 7862 7863 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 7864 FormatStyle Style = getLLVMStyle(); 7865 // No declarations or definitions should be moved to own line. 7866 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 7867 verifyFormat("class A {\n" 7868 " int f() { return 1; }\n" 7869 " int g();\n" 7870 "};\n" 7871 "int f() { return 1; }\n" 7872 "int g();\n", 7873 Style); 7874 7875 // All declarations and definitions should have the return type moved to its 7876 // own line. 7877 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 7878 Style.TypenameMacros = {"LIST"}; 7879 verifyFormat("SomeType\n" 7880 "funcdecl(LIST(uint64_t));", 7881 Style); 7882 verifyFormat("class E {\n" 7883 " int\n" 7884 " f() {\n" 7885 " return 1;\n" 7886 " }\n" 7887 " int\n" 7888 " g();\n" 7889 "};\n" 7890 "int\n" 7891 "f() {\n" 7892 " return 1;\n" 7893 "}\n" 7894 "int\n" 7895 "g();\n", 7896 Style); 7897 7898 // Top-level definitions, and no kinds of declarations should have the 7899 // return type moved to its own line. 7900 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 7901 verifyFormat("class B {\n" 7902 " int f() { return 1; }\n" 7903 " int g();\n" 7904 "};\n" 7905 "int\n" 7906 "f() {\n" 7907 " return 1;\n" 7908 "}\n" 7909 "int g();\n", 7910 Style); 7911 7912 // Top-level definitions and declarations should have the return type moved 7913 // to its own line. 7914 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 7915 verifyFormat("class C {\n" 7916 " int f() { return 1; }\n" 7917 " int g();\n" 7918 "};\n" 7919 "int\n" 7920 "f() {\n" 7921 " return 1;\n" 7922 "}\n" 7923 "int\n" 7924 "g();\n", 7925 Style); 7926 7927 // All definitions should have the return type moved to its own line, but no 7928 // kinds of declarations. 7929 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 7930 verifyFormat("class D {\n" 7931 " int\n" 7932 " f() {\n" 7933 " return 1;\n" 7934 " }\n" 7935 " int g();\n" 7936 "};\n" 7937 "int\n" 7938 "f() {\n" 7939 " return 1;\n" 7940 "}\n" 7941 "int g();\n", 7942 Style); 7943 verifyFormat("const char *\n" 7944 "f(void) {\n" // Break here. 7945 " return \"\";\n" 7946 "}\n" 7947 "const char *bar(void);\n", // No break here. 7948 Style); 7949 verifyFormat("template <class T>\n" 7950 "T *\n" 7951 "f(T &c) {\n" // Break here. 7952 " return NULL;\n" 7953 "}\n" 7954 "template <class T> T *f(T &c);\n", // No break here. 7955 Style); 7956 verifyFormat("class C {\n" 7957 " int\n" 7958 " operator+() {\n" 7959 " return 1;\n" 7960 " }\n" 7961 " int\n" 7962 " operator()() {\n" 7963 " return 1;\n" 7964 " }\n" 7965 "};\n", 7966 Style); 7967 verifyFormat("void\n" 7968 "A::operator()() {}\n" 7969 "void\n" 7970 "A::operator>>() {}\n" 7971 "void\n" 7972 "A::operator+() {}\n" 7973 "void\n" 7974 "A::operator*() {}\n" 7975 "void\n" 7976 "A::operator->() {}\n" 7977 "void\n" 7978 "A::operator void *() {}\n" 7979 "void\n" 7980 "A::operator void &() {}\n" 7981 "void\n" 7982 "A::operator void &&() {}\n" 7983 "void\n" 7984 "A::operator char *() {}\n" 7985 "void\n" 7986 "A::operator[]() {}\n" 7987 "void\n" 7988 "A::operator!() {}\n" 7989 "void\n" 7990 "A::operator**() {}\n" 7991 "void\n" 7992 "A::operator<Foo> *() {}\n" 7993 "void\n" 7994 "A::operator<Foo> **() {}\n" 7995 "void\n" 7996 "A::operator<Foo> &() {}\n" 7997 "void\n" 7998 "A::operator void **() {}\n", 7999 Style); 8000 verifyFormat("constexpr auto\n" 8001 "operator()() const -> reference {}\n" 8002 "constexpr auto\n" 8003 "operator>>() const -> reference {}\n" 8004 "constexpr auto\n" 8005 "operator+() const -> reference {}\n" 8006 "constexpr auto\n" 8007 "operator*() const -> reference {}\n" 8008 "constexpr auto\n" 8009 "operator->() const -> reference {}\n" 8010 "constexpr auto\n" 8011 "operator++() const -> reference {}\n" 8012 "constexpr auto\n" 8013 "operator void *() const -> reference {}\n" 8014 "constexpr auto\n" 8015 "operator void **() const -> reference {}\n" 8016 "constexpr auto\n" 8017 "operator void *() const -> reference {}\n" 8018 "constexpr auto\n" 8019 "operator void &() const -> reference {}\n" 8020 "constexpr auto\n" 8021 "operator void &&() const -> reference {}\n" 8022 "constexpr auto\n" 8023 "operator char *() const -> reference {}\n" 8024 "constexpr auto\n" 8025 "operator!() const -> reference {}\n" 8026 "constexpr auto\n" 8027 "operator[]() const -> reference {}\n", 8028 Style); 8029 verifyFormat("void *operator new(std::size_t s);", // No break here. 8030 Style); 8031 verifyFormat("void *\n" 8032 "operator new(std::size_t s) {}", 8033 Style); 8034 verifyFormat("void *\n" 8035 "operator delete[](void *ptr) {}", 8036 Style); 8037 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8038 verifyFormat("const char *\n" 8039 "f(void)\n" // Break here. 8040 "{\n" 8041 " return \"\";\n" 8042 "}\n" 8043 "const char *bar(void);\n", // No break here. 8044 Style); 8045 verifyFormat("template <class T>\n" 8046 "T *\n" // Problem here: no line break 8047 "f(T &c)\n" // Break here. 8048 "{\n" 8049 " return NULL;\n" 8050 "}\n" 8051 "template <class T> T *f(T &c);\n", // No break here. 8052 Style); 8053 verifyFormat("int\n" 8054 "foo(A<bool> a)\n" 8055 "{\n" 8056 " return a;\n" 8057 "}\n", 8058 Style); 8059 verifyFormat("int\n" 8060 "foo(A<8> a)\n" 8061 "{\n" 8062 " return a;\n" 8063 "}\n", 8064 Style); 8065 verifyFormat("int\n" 8066 "foo(A<B<bool>, 8> a)\n" 8067 "{\n" 8068 " return a;\n" 8069 "}\n", 8070 Style); 8071 verifyFormat("int\n" 8072 "foo(A<B<8>, bool> a)\n" 8073 "{\n" 8074 " return a;\n" 8075 "}\n", 8076 Style); 8077 verifyFormat("int\n" 8078 "foo(A<B<bool>, bool> a)\n" 8079 "{\n" 8080 " return a;\n" 8081 "}\n", 8082 Style); 8083 verifyFormat("int\n" 8084 "foo(A<B<8>, 8> a)\n" 8085 "{\n" 8086 " return a;\n" 8087 "}\n", 8088 Style); 8089 8090 Style = getGNUStyle(); 8091 8092 // Test for comments at the end of function declarations. 8093 verifyFormat("void\n" 8094 "foo (int a, /*abc*/ int b) // def\n" 8095 "{\n" 8096 "}\n", 8097 Style); 8098 8099 verifyFormat("void\n" 8100 "foo (int a, /* abc */ int b) /* def */\n" 8101 "{\n" 8102 "}\n", 8103 Style); 8104 8105 // Definitions that should not break after return type 8106 verifyFormat("void foo (int a, int b); // def\n", Style); 8107 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8108 verifyFormat("void foo (int a, int b);\n", Style); 8109 } 8110 8111 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8112 FormatStyle NoBreak = getLLVMStyle(); 8113 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8114 FormatStyle Break = getLLVMStyle(); 8115 Break.AlwaysBreakBeforeMultilineStrings = true; 8116 verifyFormat("aaaa = \"bbbb\"\n" 8117 " \"cccc\";", 8118 NoBreak); 8119 verifyFormat("aaaa =\n" 8120 " \"bbbb\"\n" 8121 " \"cccc\";", 8122 Break); 8123 verifyFormat("aaaa(\"bbbb\"\n" 8124 " \"cccc\");", 8125 NoBreak); 8126 verifyFormat("aaaa(\n" 8127 " \"bbbb\"\n" 8128 " \"cccc\");", 8129 Break); 8130 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8131 " \"cccc\");", 8132 NoBreak); 8133 verifyFormat("aaaa(qqq,\n" 8134 " \"bbbb\"\n" 8135 " \"cccc\");", 8136 Break); 8137 verifyFormat("aaaa(qqq,\n" 8138 " L\"bbbb\"\n" 8139 " L\"cccc\");", 8140 Break); 8141 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8142 " \"bbbb\"));", 8143 Break); 8144 verifyFormat("string s = someFunction(\n" 8145 " \"abc\"\n" 8146 " \"abc\");", 8147 Break); 8148 8149 // As we break before unary operators, breaking right after them is bad. 8150 verifyFormat("string foo = abc ? \"x\"\n" 8151 " \"blah blah blah blah blah blah\"\n" 8152 " : \"y\";", 8153 Break); 8154 8155 // Don't break if there is no column gain. 8156 verifyFormat("f(\"aaaa\"\n" 8157 " \"bbbb\");", 8158 Break); 8159 8160 // Treat literals with escaped newlines like multi-line string literals. 8161 EXPECT_EQ("x = \"a\\\n" 8162 "b\\\n" 8163 "c\";", 8164 format("x = \"a\\\n" 8165 "b\\\n" 8166 "c\";", 8167 NoBreak)); 8168 EXPECT_EQ("xxxx =\n" 8169 " \"a\\\n" 8170 "b\\\n" 8171 "c\";", 8172 format("xxxx = \"a\\\n" 8173 "b\\\n" 8174 "c\";", 8175 Break)); 8176 8177 EXPECT_EQ("NSString *const kString =\n" 8178 " @\"aaaa\"\n" 8179 " @\"bbbb\";", 8180 format("NSString *const kString = @\"aaaa\"\n" 8181 "@\"bbbb\";", 8182 Break)); 8183 8184 Break.ColumnLimit = 0; 8185 verifyFormat("const char *hello = \"hello llvm\";", Break); 8186 } 8187 8188 TEST_F(FormatTest, AlignsPipes) { 8189 verifyFormat( 8190 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8191 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8192 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8193 verifyFormat( 8194 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 8195 " << aaaaaaaaaaaaaaaaaaaa;"); 8196 verifyFormat( 8197 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8198 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8199 verifyFormat( 8200 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8201 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8202 verifyFormat( 8203 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 8204 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 8205 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 8206 verifyFormat( 8207 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8208 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8209 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8210 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8211 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8212 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8213 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8214 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 8215 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 8216 verifyFormat( 8217 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8218 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8219 verifyFormat( 8220 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 8221 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8222 8223 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 8224 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 8225 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8227 " aaaaaaaaaaaaaaaaaaaaa)\n" 8228 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8229 verifyFormat("LOG_IF(aaa == //\n" 8230 " bbb)\n" 8231 " << a << b;"); 8232 8233 // But sometimes, breaking before the first "<<" is desirable. 8234 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8235 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 8236 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 8237 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8238 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8239 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 8240 " << BEF << IsTemplate << Description << E->getType();"); 8241 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8242 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8243 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8244 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8245 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8246 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8247 " << aaa;"); 8248 8249 verifyFormat( 8250 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8251 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8252 8253 // Incomplete string literal. 8254 EXPECT_EQ("llvm::errs() << \"\n" 8255 " << a;", 8256 format("llvm::errs() << \"\n<<a;")); 8257 8258 verifyFormat("void f() {\n" 8259 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 8260 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 8261 "}"); 8262 8263 // Handle 'endl'. 8264 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 8265 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8266 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8267 8268 // Handle '\n'. 8269 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 8270 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8271 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 8272 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 8273 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 8274 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 8275 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8276 } 8277 8278 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 8279 verifyFormat("return out << \"somepacket = {\\n\"\n" 8280 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 8281 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 8282 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 8283 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 8284 " << \"}\";"); 8285 8286 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8287 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8288 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 8289 verifyFormat( 8290 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 8291 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 8292 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 8293 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 8294 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 8295 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 8296 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8297 verifyFormat( 8298 "void f() {\n" 8299 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 8300 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8301 "}"); 8302 8303 // Breaking before the first "<<" is generally not desirable. 8304 verifyFormat( 8305 "llvm::errs()\n" 8306 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8307 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8308 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8309 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8310 getLLVMStyleWithColumns(70)); 8311 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8312 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8313 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8314 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8315 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8316 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8317 getLLVMStyleWithColumns(70)); 8318 8319 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8320 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8321 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 8322 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8323 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8324 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 8325 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 8326 " (aaaa + aaaa);", 8327 getLLVMStyleWithColumns(40)); 8328 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 8329 " (aaaaaaa + aaaaa));", 8330 getLLVMStyleWithColumns(40)); 8331 verifyFormat( 8332 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 8333 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 8334 " bbbbbbbbbbbbbbbbbbbbbbb);"); 8335 } 8336 8337 TEST_F(FormatTest, UnderstandsEquals) { 8338 verifyFormat( 8339 "aaaaaaaaaaaaaaaaa =\n" 8340 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8341 verifyFormat( 8342 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8344 verifyFormat( 8345 "if (a) {\n" 8346 " f();\n" 8347 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8348 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 8349 "}"); 8350 8351 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8352 " 100000000 + 10000000) {\n}"); 8353 } 8354 8355 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 8356 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8357 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 8358 8359 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8360 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 8361 8362 verifyFormat( 8363 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 8364 " Parameter2);"); 8365 8366 verifyFormat( 8367 "ShortObject->shortFunction(\n" 8368 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 8369 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 8370 8371 verifyFormat("loooooooooooooongFunction(\n" 8372 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 8373 8374 verifyFormat( 8375 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 8376 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 8377 8378 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8379 " .WillRepeatedly(Return(SomeValue));"); 8380 verifyFormat("void f() {\n" 8381 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8382 " .Times(2)\n" 8383 " .WillRepeatedly(Return(SomeValue));\n" 8384 "}"); 8385 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 8386 " ccccccccccccccccccccccc);"); 8387 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8388 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8389 " .aaaaa(aaaaa),\n" 8390 " aaaaaaaaaaaaaaaaaaaaa);"); 8391 verifyFormat("void f() {\n" 8392 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8393 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 8394 "}"); 8395 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8396 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8397 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8398 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8399 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8400 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8401 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8402 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8403 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 8404 "}"); 8405 8406 // Here, it is not necessary to wrap at "." or "->". 8407 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 8408 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8409 verifyFormat( 8410 "aaaaaaaaaaa->aaaaaaaaa(\n" 8411 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8412 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 8413 8414 verifyFormat( 8415 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 8417 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 8418 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8419 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 8420 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8421 8422 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8424 " .a();"); 8425 8426 FormatStyle NoBinPacking = getLLVMStyle(); 8427 NoBinPacking.BinPackParameters = false; 8428 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8429 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8430 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 8431 " aaaaaaaaaaaaaaaaaaa,\n" 8432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8433 NoBinPacking); 8434 8435 // If there is a subsequent call, change to hanging indentation. 8436 verifyFormat( 8437 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8438 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 8439 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8440 verifyFormat( 8441 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8442 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 8443 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8445 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8446 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8448 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8449 } 8450 8451 TEST_F(FormatTest, WrapsTemplateDeclarations) { 8452 verifyFormat("template <typename T>\n" 8453 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8454 verifyFormat("template <typename T>\n" 8455 "// T should be one of {A, B}.\n" 8456 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8457 verifyFormat( 8458 "template <typename T>\n" 8459 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 8460 verifyFormat("template <typename T>\n" 8461 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 8462 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 8463 verifyFormat( 8464 "template <typename T>\n" 8465 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 8466 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 8467 verifyFormat( 8468 "template <typename T>\n" 8469 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 8470 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 8471 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8472 verifyFormat("template <typename T>\n" 8473 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8474 " int aaaaaaaaaaaaaaaaaaaaaa);"); 8475 verifyFormat( 8476 "template <typename T1, typename T2 = char, typename T3 = char,\n" 8477 " typename T4 = char>\n" 8478 "void f();"); 8479 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 8480 " template <typename> class cccccccccccccccccccccc,\n" 8481 " typename ddddddddddddd>\n" 8482 "class C {};"); 8483 verifyFormat( 8484 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 8485 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8486 8487 verifyFormat("void f() {\n" 8488 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 8489 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 8490 "}"); 8491 8492 verifyFormat("template <typename T> class C {};"); 8493 verifyFormat("template <typename T> void f();"); 8494 verifyFormat("template <typename T> void f() {}"); 8495 verifyFormat( 8496 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8497 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8498 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 8499 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8500 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8501 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 8502 " bbbbbbbbbbbbbbbbbbbbbbbb);", 8503 getLLVMStyleWithColumns(72)); 8504 EXPECT_EQ("static_cast<A< //\n" 8505 " B> *>(\n" 8506 "\n" 8507 ");", 8508 format("static_cast<A<//\n" 8509 " B>*>(\n" 8510 "\n" 8511 " );")); 8512 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8513 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 8514 8515 FormatStyle AlwaysBreak = getLLVMStyle(); 8516 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 8517 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 8518 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 8519 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 8520 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8521 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8522 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 8523 verifyFormat("template <template <typename> class Fooooooo,\n" 8524 " template <typename> class Baaaaaaar>\n" 8525 "struct C {};", 8526 AlwaysBreak); 8527 verifyFormat("template <typename T> // T can be A, B or C.\n" 8528 "struct C {};", 8529 AlwaysBreak); 8530 verifyFormat("template <enum E> class A {\n" 8531 "public:\n" 8532 " E *f();\n" 8533 "};"); 8534 8535 FormatStyle NeverBreak = getLLVMStyle(); 8536 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 8537 verifyFormat("template <typename T> class C {};", NeverBreak); 8538 verifyFormat("template <typename T> void f();", NeverBreak); 8539 verifyFormat("template <typename T> void f() {}", NeverBreak); 8540 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8541 "bbbbbbbbbbbbbbbbbbbb) {}", 8542 NeverBreak); 8543 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8544 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8545 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 8546 NeverBreak); 8547 verifyFormat("template <template <typename> class Fooooooo,\n" 8548 " template <typename> class Baaaaaaar>\n" 8549 "struct C {};", 8550 NeverBreak); 8551 verifyFormat("template <typename T> // T can be A, B or C.\n" 8552 "struct C {};", 8553 NeverBreak); 8554 verifyFormat("template <enum E> class A {\n" 8555 "public:\n" 8556 " E *f();\n" 8557 "};", 8558 NeverBreak); 8559 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 8560 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8561 "bbbbbbbbbbbbbbbbbbbb) {}", 8562 NeverBreak); 8563 } 8564 8565 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 8566 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 8567 Style.ColumnLimit = 60; 8568 EXPECT_EQ("// Baseline - no comments.\n" 8569 "template <\n" 8570 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8571 "void f() {}", 8572 format("// Baseline - no comments.\n" 8573 "template <\n" 8574 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8575 "void f() {}", 8576 Style)); 8577 8578 EXPECT_EQ("template <\n" 8579 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8580 "void f() {}", 8581 format("template <\n" 8582 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8583 "void f() {}", 8584 Style)); 8585 8586 EXPECT_EQ( 8587 "template <\n" 8588 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8589 "void f() {}", 8590 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8591 "void f() {}", 8592 Style)); 8593 8594 EXPECT_EQ( 8595 "template <\n" 8596 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8597 " // multiline\n" 8598 "void f() {}", 8599 format("template <\n" 8600 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8601 " // multiline\n" 8602 "void f() {}", 8603 Style)); 8604 8605 EXPECT_EQ( 8606 "template <typename aaaaaaaaaa<\n" 8607 " bbbbbbbbbbbb>::value> // trailing loooong\n" 8608 "void f() {}", 8609 format( 8610 "template <\n" 8611 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 8612 "void f() {}", 8613 Style)); 8614 } 8615 8616 TEST_F(FormatTest, WrapsTemplateParameters) { 8617 FormatStyle Style = getLLVMStyle(); 8618 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8619 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8620 verifyFormat( 8621 "template <typename... a> struct q {};\n" 8622 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8623 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8624 " y;", 8625 Style); 8626 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8627 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8628 verifyFormat( 8629 "template <typename... a> struct r {};\n" 8630 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8631 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8632 " y;", 8633 Style); 8634 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8635 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8636 verifyFormat("template <typename... a> struct s {};\n" 8637 "extern s<\n" 8638 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8639 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8640 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8641 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8642 " y;", 8643 Style); 8644 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8645 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8646 verifyFormat("template <typename... a> struct t {};\n" 8647 "extern t<\n" 8648 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8649 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8650 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8651 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8652 " y;", 8653 Style); 8654 } 8655 8656 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 8657 verifyFormat( 8658 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8659 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8660 verifyFormat( 8661 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8662 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8664 8665 // FIXME: Should we have the extra indent after the second break? 8666 verifyFormat( 8667 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8668 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8669 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8670 8671 verifyFormat( 8672 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 8673 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 8674 8675 // Breaking at nested name specifiers is generally not desirable. 8676 verifyFormat( 8677 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8678 " aaaaaaaaaaaaaaaaaaaaaaa);"); 8679 8680 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 8681 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8682 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8683 " aaaaaaaaaaaaaaaaaaaaa);", 8684 getLLVMStyleWithColumns(74)); 8685 8686 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8687 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8688 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8689 } 8690 8691 TEST_F(FormatTest, UnderstandsTemplateParameters) { 8692 verifyFormat("A<int> a;"); 8693 verifyFormat("A<A<A<int>>> a;"); 8694 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 8695 verifyFormat("bool x = a < 1 || 2 > a;"); 8696 verifyFormat("bool x = 5 < f<int>();"); 8697 verifyFormat("bool x = f<int>() > 5;"); 8698 verifyFormat("bool x = 5 < a<int>::x;"); 8699 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 8700 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 8701 8702 verifyGoogleFormat("A<A<int>> a;"); 8703 verifyGoogleFormat("A<A<A<int>>> a;"); 8704 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 8705 verifyGoogleFormat("A<A<int> > a;"); 8706 verifyGoogleFormat("A<A<A<int> > > a;"); 8707 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 8708 verifyGoogleFormat("A<::A<int>> a;"); 8709 verifyGoogleFormat("A<::A> a;"); 8710 verifyGoogleFormat("A< ::A> a;"); 8711 verifyGoogleFormat("A< ::A<int> > a;"); 8712 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 8713 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 8714 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 8715 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 8716 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 8717 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 8718 8719 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 8720 8721 // template closer followed by a token that starts with > or = 8722 verifyFormat("bool b = a<1> > 1;"); 8723 verifyFormat("bool b = a<1> >= 1;"); 8724 verifyFormat("int i = a<1> >> 1;"); 8725 FormatStyle Style = getLLVMStyle(); 8726 Style.SpaceBeforeAssignmentOperators = false; 8727 verifyFormat("bool b= a<1> == 1;", Style); 8728 verifyFormat("a<int> = 1;", Style); 8729 verifyFormat("a<int> >>= 1;", Style); 8730 8731 verifyFormat("test < a | b >> c;"); 8732 verifyFormat("test<test<a | b>> c;"); 8733 verifyFormat("test >> a >> b;"); 8734 verifyFormat("test << a >> b;"); 8735 8736 verifyFormat("f<int>();"); 8737 verifyFormat("template <typename T> void f() {}"); 8738 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 8739 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 8740 "sizeof(char)>::type>;"); 8741 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 8742 verifyFormat("f(a.operator()<A>());"); 8743 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8744 " .template operator()<A>());", 8745 getLLVMStyleWithColumns(35)); 8746 8747 // Not template parameters. 8748 verifyFormat("return a < b && c > d;"); 8749 verifyFormat("void f() {\n" 8750 " while (a < b && c > d) {\n" 8751 " }\n" 8752 "}"); 8753 verifyFormat("template <typename... Types>\n" 8754 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 8755 8756 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8757 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 8758 getLLVMStyleWithColumns(60)); 8759 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 8760 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 8761 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 8762 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 8763 } 8764 8765 TEST_F(FormatTest, UnderstandsShiftOperators) { 8766 verifyFormat("if (i < x >> 1)"); 8767 verifyFormat("while (i < x >> 1)"); 8768 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 8769 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 8770 verifyFormat( 8771 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 8772 verifyFormat("Foo.call<Bar<Function>>()"); 8773 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 8774 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 8775 "++i, v = v >> 1)"); 8776 verifyFormat("if (w<u<v<x>>, 1>::t)"); 8777 } 8778 8779 TEST_F(FormatTest, BitshiftOperatorWidth) { 8780 EXPECT_EQ("int a = 1 << 2; /* foo\n" 8781 " bar */", 8782 format("int a=1<<2; /* foo\n" 8783 " bar */")); 8784 8785 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 8786 " bar */", 8787 format("int b =256>>1 ; /* foo\n" 8788 " bar */")); 8789 } 8790 8791 TEST_F(FormatTest, UnderstandsBinaryOperators) { 8792 verifyFormat("COMPARE(a, ==, b);"); 8793 verifyFormat("auto s = sizeof...(Ts) - 1;"); 8794 } 8795 8796 TEST_F(FormatTest, UnderstandsPointersToMembers) { 8797 verifyFormat("int A::*x;"); 8798 verifyFormat("int (S::*func)(void *);"); 8799 verifyFormat("void f() { int (S::*func)(void *); }"); 8800 verifyFormat("typedef bool *(Class::*Member)() const;"); 8801 verifyFormat("void f() {\n" 8802 " (a->*f)();\n" 8803 " a->*x;\n" 8804 " (a.*f)();\n" 8805 " ((*a).*f)();\n" 8806 " a.*x;\n" 8807 "}"); 8808 verifyFormat("void f() {\n" 8809 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 8810 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 8811 "}"); 8812 verifyFormat( 8813 "(aaaaaaaaaa->*bbbbbbb)(\n" 8814 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8815 FormatStyle Style = getLLVMStyle(); 8816 Style.PointerAlignment = FormatStyle::PAS_Left; 8817 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 8818 } 8819 8820 TEST_F(FormatTest, UnderstandsUnaryOperators) { 8821 verifyFormat("int a = -2;"); 8822 verifyFormat("f(-1, -2, -3);"); 8823 verifyFormat("a[-1] = 5;"); 8824 verifyFormat("int a = 5 + -2;"); 8825 verifyFormat("if (i == -1) {\n}"); 8826 verifyFormat("if (i != -1) {\n}"); 8827 verifyFormat("if (i > -1) {\n}"); 8828 verifyFormat("if (i < -1) {\n}"); 8829 verifyFormat("++(a->f());"); 8830 verifyFormat("--(a->f());"); 8831 verifyFormat("(a->f())++;"); 8832 verifyFormat("a[42]++;"); 8833 verifyFormat("if (!(a->f())) {\n}"); 8834 verifyFormat("if (!+i) {\n}"); 8835 verifyFormat("~&a;"); 8836 8837 verifyFormat("a-- > b;"); 8838 verifyFormat("b ? -a : c;"); 8839 verifyFormat("n * sizeof char16;"); 8840 verifyFormat("n * alignof char16;", getGoogleStyle()); 8841 verifyFormat("sizeof(char);"); 8842 verifyFormat("alignof(char);", getGoogleStyle()); 8843 8844 verifyFormat("return -1;"); 8845 verifyFormat("throw -1;"); 8846 verifyFormat("switch (a) {\n" 8847 "case -1:\n" 8848 " break;\n" 8849 "}"); 8850 verifyFormat("#define X -1"); 8851 verifyFormat("#define X -kConstant"); 8852 8853 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 8854 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 8855 8856 verifyFormat("int a = /* confusing comment */ -1;"); 8857 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 8858 verifyFormat("int a = i /* confusing comment */++;"); 8859 8860 verifyFormat("co_yield -1;"); 8861 verifyFormat("co_return -1;"); 8862 8863 // Check that * is not treated as a binary operator when we set 8864 // PointerAlignment as PAS_Left after a keyword and not a declaration. 8865 FormatStyle PASLeftStyle = getLLVMStyle(); 8866 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 8867 verifyFormat("co_return *a;", PASLeftStyle); 8868 verifyFormat("co_await *a;", PASLeftStyle); 8869 verifyFormat("co_yield *a", PASLeftStyle); 8870 verifyFormat("return *a;", PASLeftStyle); 8871 } 8872 8873 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 8874 verifyFormat("if (!aaaaaaaaaa( // break\n" 8875 " aaaaa)) {\n" 8876 "}"); 8877 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 8878 " aaaaa));"); 8879 verifyFormat("*aaa = aaaaaaa( // break\n" 8880 " bbbbbb);"); 8881 } 8882 8883 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 8884 verifyFormat("bool operator<();"); 8885 verifyFormat("bool operator>();"); 8886 verifyFormat("bool operator=();"); 8887 verifyFormat("bool operator==();"); 8888 verifyFormat("bool operator!=();"); 8889 verifyFormat("int operator+();"); 8890 verifyFormat("int operator++();"); 8891 verifyFormat("int operator++(int) volatile noexcept;"); 8892 verifyFormat("bool operator,();"); 8893 verifyFormat("bool operator();"); 8894 verifyFormat("bool operator()();"); 8895 verifyFormat("bool operator[]();"); 8896 verifyFormat("operator bool();"); 8897 verifyFormat("operator int();"); 8898 verifyFormat("operator void *();"); 8899 verifyFormat("operator SomeType<int>();"); 8900 verifyFormat("operator SomeType<int, int>();"); 8901 verifyFormat("operator SomeType<SomeType<int>>();"); 8902 verifyFormat("void *operator new(std::size_t size);"); 8903 verifyFormat("void *operator new[](std::size_t size);"); 8904 verifyFormat("void operator delete(void *ptr);"); 8905 verifyFormat("void operator delete[](void *ptr);"); 8906 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 8907 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 8908 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 8909 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 8910 8911 verifyFormat( 8912 "ostream &operator<<(ostream &OutputStream,\n" 8913 " SomeReallyLongType WithSomeReallyLongValue);"); 8914 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 8915 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 8916 " return left.group < right.group;\n" 8917 "}"); 8918 verifyFormat("SomeType &operator=(const SomeType &S);"); 8919 verifyFormat("f.template operator()<int>();"); 8920 8921 verifyGoogleFormat("operator void*();"); 8922 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 8923 verifyGoogleFormat("operator ::A();"); 8924 8925 verifyFormat("using A::operator+;"); 8926 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 8927 "int i;"); 8928 8929 // Calling an operator as a member function. 8930 verifyFormat("void f() { a.operator*(); }"); 8931 verifyFormat("void f() { a.operator*(b & b); }"); 8932 verifyFormat("void f() { a->operator&(a * b); }"); 8933 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 8934 // TODO: Calling an operator as a non-member function is hard to distinguish. 8935 // https://llvm.org/PR50629 8936 // verifyFormat("void f() { operator*(a & a); }"); 8937 // verifyFormat("void f() { operator&(a, b * b); }"); 8938 } 8939 8940 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 8941 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 8942 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 8943 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 8944 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 8945 verifyFormat("Deleted &operator=(const Deleted &) &;"); 8946 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 8947 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 8948 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 8949 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 8950 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 8951 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 8952 verifyFormat("void Fn(T const &) const &;"); 8953 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 8954 verifyFormat("template <typename T>\n" 8955 "void F(T) && = delete;", 8956 getGoogleStyle()); 8957 8958 FormatStyle AlignLeft = getLLVMStyle(); 8959 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 8960 verifyFormat("void A::b() && {}", AlignLeft); 8961 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 8962 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 8963 AlignLeft); 8964 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 8965 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 8966 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 8967 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 8968 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 8969 verifyFormat("auto Function(T) & -> void;", AlignLeft); 8970 verifyFormat("void Fn(T const&) const&;", AlignLeft); 8971 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 8972 8973 FormatStyle Spaces = getLLVMStyle(); 8974 Spaces.SpacesInCStyleCastParentheses = true; 8975 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 8976 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 8977 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 8978 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 8979 8980 Spaces.SpacesInCStyleCastParentheses = false; 8981 Spaces.SpacesInParentheses = true; 8982 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 8983 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 8984 Spaces); 8985 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 8986 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 8987 8988 FormatStyle BreakTemplate = getLLVMStyle(); 8989 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 8990 8991 verifyFormat("struct f {\n" 8992 " template <class T>\n" 8993 " int &foo(const std::string &str) &noexcept {}\n" 8994 "};", 8995 BreakTemplate); 8996 8997 verifyFormat("struct f {\n" 8998 " template <class T>\n" 8999 " int &foo(const std::string &str) &&noexcept {}\n" 9000 "};", 9001 BreakTemplate); 9002 9003 verifyFormat("struct f {\n" 9004 " template <class T>\n" 9005 " int &foo(const std::string &str) const &noexcept {}\n" 9006 "};", 9007 BreakTemplate); 9008 9009 verifyFormat("struct f {\n" 9010 " template <class T>\n" 9011 " int &foo(const std::string &str) const &noexcept {}\n" 9012 "};", 9013 BreakTemplate); 9014 9015 verifyFormat("struct f {\n" 9016 " template <class T>\n" 9017 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9018 "};", 9019 BreakTemplate); 9020 9021 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9022 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9023 FormatStyle::BTDS_Yes; 9024 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9025 9026 verifyFormat("struct f {\n" 9027 " template <class T>\n" 9028 " int& foo(const std::string& str) & noexcept {}\n" 9029 "};", 9030 AlignLeftBreakTemplate); 9031 9032 verifyFormat("struct f {\n" 9033 " template <class T>\n" 9034 " int& foo(const std::string& str) && noexcept {}\n" 9035 "};", 9036 AlignLeftBreakTemplate); 9037 9038 verifyFormat("struct f {\n" 9039 " template <class T>\n" 9040 " int& foo(const std::string& str) const& noexcept {}\n" 9041 "};", 9042 AlignLeftBreakTemplate); 9043 9044 verifyFormat("struct f {\n" 9045 " template <class T>\n" 9046 " int& foo(const std::string& str) const&& noexcept {}\n" 9047 "};", 9048 AlignLeftBreakTemplate); 9049 9050 verifyFormat("struct f {\n" 9051 " template <class T>\n" 9052 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9053 "};", 9054 AlignLeftBreakTemplate); 9055 9056 // The `&` in `Type&` should not be confused with a trailing `&` of 9057 // DEPRECATED(reason) member function. 9058 verifyFormat("struct f {\n" 9059 " template <class T>\n" 9060 " DEPRECATED(reason)\n" 9061 " Type &foo(arguments) {}\n" 9062 "};", 9063 BreakTemplate); 9064 9065 verifyFormat("struct f {\n" 9066 " template <class T>\n" 9067 " DEPRECATED(reason)\n" 9068 " Type& foo(arguments) {}\n" 9069 "};", 9070 AlignLeftBreakTemplate); 9071 9072 verifyFormat("void (*foopt)(int) = &func;"); 9073 } 9074 9075 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9076 verifyFormat("void f() {\n" 9077 " A *a = new A;\n" 9078 " A *a = new (placement) A;\n" 9079 " delete a;\n" 9080 " delete (A *)a;\n" 9081 "}"); 9082 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9083 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9084 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9085 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9086 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9087 verifyFormat("delete[] h->p;"); 9088 } 9089 9090 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9091 verifyFormat("int *f(int *a) {}"); 9092 verifyFormat("int main(int argc, char **argv) {}"); 9093 verifyFormat("Test::Test(int b) : a(b * b) {}"); 9094 verifyIndependentOfContext("f(a, *a);"); 9095 verifyFormat("void g() { f(*a); }"); 9096 verifyIndependentOfContext("int a = b * 10;"); 9097 verifyIndependentOfContext("int a = 10 * b;"); 9098 verifyIndependentOfContext("int a = b * c;"); 9099 verifyIndependentOfContext("int a += b * c;"); 9100 verifyIndependentOfContext("int a -= b * c;"); 9101 verifyIndependentOfContext("int a *= b * c;"); 9102 verifyIndependentOfContext("int a /= b * c;"); 9103 verifyIndependentOfContext("int a = *b;"); 9104 verifyIndependentOfContext("int a = *b * c;"); 9105 verifyIndependentOfContext("int a = b * *c;"); 9106 verifyIndependentOfContext("int a = b * (10);"); 9107 verifyIndependentOfContext("S << b * (10);"); 9108 verifyIndependentOfContext("return 10 * b;"); 9109 verifyIndependentOfContext("return *b * *c;"); 9110 verifyIndependentOfContext("return a & ~b;"); 9111 verifyIndependentOfContext("f(b ? *c : *d);"); 9112 verifyIndependentOfContext("int a = b ? *c : *d;"); 9113 verifyIndependentOfContext("*b = a;"); 9114 verifyIndependentOfContext("a * ~b;"); 9115 verifyIndependentOfContext("a * !b;"); 9116 verifyIndependentOfContext("a * +b;"); 9117 verifyIndependentOfContext("a * -b;"); 9118 verifyIndependentOfContext("a * ++b;"); 9119 verifyIndependentOfContext("a * --b;"); 9120 verifyIndependentOfContext("a[4] * b;"); 9121 verifyIndependentOfContext("a[a * a] = 1;"); 9122 verifyIndependentOfContext("f() * b;"); 9123 verifyIndependentOfContext("a * [self dostuff];"); 9124 verifyIndependentOfContext("int x = a * (a + b);"); 9125 verifyIndependentOfContext("(a *)(a + b);"); 9126 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 9127 verifyIndependentOfContext("int *pa = (int *)&a;"); 9128 verifyIndependentOfContext("return sizeof(int **);"); 9129 verifyIndependentOfContext("return sizeof(int ******);"); 9130 verifyIndependentOfContext("return (int **&)a;"); 9131 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 9132 verifyFormat("void f(Type (*parameter)[10]) {}"); 9133 verifyFormat("void f(Type (¶meter)[10]) {}"); 9134 verifyGoogleFormat("return sizeof(int**);"); 9135 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 9136 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 9137 verifyFormat("auto a = [](int **&, int ***) {};"); 9138 verifyFormat("auto PointerBinding = [](const char *S) {};"); 9139 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 9140 verifyFormat("[](const decltype(*a) &value) {}"); 9141 verifyFormat("[](const typeof(*a) &value) {}"); 9142 verifyFormat("[](const _Atomic(a *) &value) {}"); 9143 verifyFormat("[](const __underlying_type(a) &value) {}"); 9144 verifyFormat("decltype(a * b) F();"); 9145 verifyFormat("typeof(a * b) F();"); 9146 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 9147 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 9148 verifyIndependentOfContext("typedef void (*f)(int *a);"); 9149 verifyIndependentOfContext("int i{a * b};"); 9150 verifyIndependentOfContext("aaa && aaa->f();"); 9151 verifyIndependentOfContext("int x = ~*p;"); 9152 verifyFormat("Constructor() : a(a), area(width * height) {}"); 9153 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 9154 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 9155 verifyFormat("void f() { f(a, c * d); }"); 9156 verifyFormat("void f() { f(new a(), c * d); }"); 9157 verifyFormat("void f(const MyOverride &override);"); 9158 verifyFormat("void f(const MyFinal &final);"); 9159 verifyIndependentOfContext("bool a = f() && override.f();"); 9160 verifyIndependentOfContext("bool a = f() && final.f();"); 9161 9162 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 9163 9164 verifyIndependentOfContext("A<int *> a;"); 9165 verifyIndependentOfContext("A<int **> a;"); 9166 verifyIndependentOfContext("A<int *, int *> a;"); 9167 verifyIndependentOfContext("A<int *[]> a;"); 9168 verifyIndependentOfContext( 9169 "const char *const p = reinterpret_cast<const char *const>(q);"); 9170 verifyIndependentOfContext("A<int **, int **> a;"); 9171 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 9172 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 9173 verifyFormat("for (; a && b;) {\n}"); 9174 verifyFormat("bool foo = true && [] { return false; }();"); 9175 9176 verifyFormat( 9177 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9179 9180 verifyGoogleFormat("int const* a = &b;"); 9181 verifyGoogleFormat("**outparam = 1;"); 9182 verifyGoogleFormat("*outparam = a * b;"); 9183 verifyGoogleFormat("int main(int argc, char** argv) {}"); 9184 verifyGoogleFormat("A<int*> a;"); 9185 verifyGoogleFormat("A<int**> a;"); 9186 verifyGoogleFormat("A<int*, int*> a;"); 9187 verifyGoogleFormat("A<int**, int**> a;"); 9188 verifyGoogleFormat("f(b ? *c : *d);"); 9189 verifyGoogleFormat("int a = b ? *c : *d;"); 9190 verifyGoogleFormat("Type* t = **x;"); 9191 verifyGoogleFormat("Type* t = *++*x;"); 9192 verifyGoogleFormat("*++*x;"); 9193 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 9194 verifyGoogleFormat("Type* t = x++ * y;"); 9195 verifyGoogleFormat( 9196 "const char* const p = reinterpret_cast<const char* const>(q);"); 9197 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 9198 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 9199 verifyGoogleFormat("template <typename T>\n" 9200 "void f(int i = 0, SomeType** temps = NULL);"); 9201 9202 FormatStyle Left = getLLVMStyle(); 9203 Left.PointerAlignment = FormatStyle::PAS_Left; 9204 verifyFormat("x = *a(x) = *a(y);", Left); 9205 verifyFormat("for (;; *a = b) {\n}", Left); 9206 verifyFormat("return *this += 1;", Left); 9207 verifyFormat("throw *x;", Left); 9208 verifyFormat("delete *x;", Left); 9209 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 9210 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 9211 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 9212 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 9213 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 9214 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 9215 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 9216 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 9217 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 9218 9219 verifyIndependentOfContext("a = *(x + y);"); 9220 verifyIndependentOfContext("a = &(x + y);"); 9221 verifyIndependentOfContext("*(x + y).call();"); 9222 verifyIndependentOfContext("&(x + y)->call();"); 9223 verifyFormat("void f() { &(*I).first; }"); 9224 9225 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 9226 verifyFormat( 9227 "int *MyValues = {\n" 9228 " *A, // Operator detection might be confused by the '{'\n" 9229 " *BB // Operator detection might be confused by previous comment\n" 9230 "};"); 9231 9232 verifyIndependentOfContext("if (int *a = &b)"); 9233 verifyIndependentOfContext("if (int &a = *b)"); 9234 verifyIndependentOfContext("if (a & b[i])"); 9235 verifyIndependentOfContext("if constexpr (a & b[i])"); 9236 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 9237 verifyIndependentOfContext("if (a * (b * c))"); 9238 verifyIndependentOfContext("if constexpr (a * (b * c))"); 9239 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 9240 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 9241 verifyIndependentOfContext("if (*b[i])"); 9242 verifyIndependentOfContext("if (int *a = (&b))"); 9243 verifyIndependentOfContext("while (int *a = &b)"); 9244 verifyIndependentOfContext("while (a * (b * c))"); 9245 verifyIndependentOfContext("size = sizeof *a;"); 9246 verifyIndependentOfContext("if (a && (b = c))"); 9247 verifyFormat("void f() {\n" 9248 " for (const int &v : Values) {\n" 9249 " }\n" 9250 "}"); 9251 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 9252 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 9253 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 9254 9255 verifyFormat("#define A (!a * b)"); 9256 verifyFormat("#define MACRO \\\n" 9257 " int *i = a * b; \\\n" 9258 " void f(a *b);", 9259 getLLVMStyleWithColumns(19)); 9260 9261 verifyIndependentOfContext("A = new SomeType *[Length];"); 9262 verifyIndependentOfContext("A = new SomeType *[Length]();"); 9263 verifyIndependentOfContext("T **t = new T *;"); 9264 verifyIndependentOfContext("T **t = new T *();"); 9265 verifyGoogleFormat("A = new SomeType*[Length]();"); 9266 verifyGoogleFormat("A = new SomeType*[Length];"); 9267 verifyGoogleFormat("T** t = new T*;"); 9268 verifyGoogleFormat("T** t = new T*();"); 9269 9270 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 9271 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 9272 verifyFormat("template <bool a, bool b> " 9273 "typename t::if<x && y>::type f() {}"); 9274 verifyFormat("template <int *y> f() {}"); 9275 verifyFormat("vector<int *> v;"); 9276 verifyFormat("vector<int *const> v;"); 9277 verifyFormat("vector<int *const **const *> v;"); 9278 verifyFormat("vector<int *volatile> v;"); 9279 verifyFormat("vector<a *_Nonnull> v;"); 9280 verifyFormat("vector<a *_Nullable> v;"); 9281 verifyFormat("vector<a *_Null_unspecified> v;"); 9282 verifyFormat("vector<a *__ptr32> v;"); 9283 verifyFormat("vector<a *__ptr64> v;"); 9284 verifyFormat("vector<a *__capability> v;"); 9285 FormatStyle TypeMacros = getLLVMStyle(); 9286 TypeMacros.TypenameMacros = {"LIST"}; 9287 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 9288 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 9289 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 9290 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 9291 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 9292 9293 FormatStyle CustomQualifier = getLLVMStyle(); 9294 // Add indentifers that should not be parsed as a qualifier by default. 9295 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9296 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 9297 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 9298 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 9299 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 9300 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 9301 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 9302 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 9303 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 9304 verifyFormat("vector<a * _NotAQualifier> v;"); 9305 verifyFormat("vector<a * __not_a_qualifier> v;"); 9306 verifyFormat("vector<a * b> v;"); 9307 verifyFormat("foo<b && false>();"); 9308 verifyFormat("foo<b & 1>();"); 9309 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 9310 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 9311 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 9312 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 9313 verifyFormat( 9314 "template <class T, class = typename std::enable_if<\n" 9315 " std::is_integral<T>::value &&\n" 9316 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 9317 "void F();", 9318 getLLVMStyleWithColumns(70)); 9319 verifyFormat("template <class T,\n" 9320 " class = typename std::enable_if<\n" 9321 " std::is_integral<T>::value &&\n" 9322 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 9323 " class U>\n" 9324 "void F();", 9325 getLLVMStyleWithColumns(70)); 9326 verifyFormat( 9327 "template <class T,\n" 9328 " class = typename ::std::enable_if<\n" 9329 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 9330 "void F();", 9331 getGoogleStyleWithColumns(68)); 9332 9333 verifyIndependentOfContext("MACRO(int *i);"); 9334 verifyIndependentOfContext("MACRO(auto *a);"); 9335 verifyIndependentOfContext("MACRO(const A *a);"); 9336 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 9337 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 9338 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 9339 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 9340 verifyIndependentOfContext("MACRO(A *const a);"); 9341 verifyIndependentOfContext("MACRO(A *restrict a);"); 9342 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 9343 verifyIndependentOfContext("MACRO(A *__restrict a);"); 9344 verifyIndependentOfContext("MACRO(A *volatile a);"); 9345 verifyIndependentOfContext("MACRO(A *__volatile a);"); 9346 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 9347 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 9348 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 9349 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 9350 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 9351 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 9352 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 9353 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 9354 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 9355 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 9356 verifyIndependentOfContext("MACRO(A *__capability);"); 9357 verifyIndependentOfContext("MACRO(A &__capability);"); 9358 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 9359 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 9360 // If we add __my_qualifier to AttributeMacros it should always be parsed as 9361 // a type declaration: 9362 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 9363 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 9364 // Also check that TypenameMacros prevents parsing it as multiplication: 9365 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 9366 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 9367 9368 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 9369 verifyFormat("void f() { f(float{1}, a * a); }"); 9370 verifyFormat("void f() { f(float(1), a * a); }"); 9371 9372 verifyFormat("f((void (*)(int))g);"); 9373 verifyFormat("f((void (&)(int))g);"); 9374 verifyFormat("f((void (^)(int))g);"); 9375 9376 // FIXME: Is there a way to make this work? 9377 // verifyIndependentOfContext("MACRO(A *a);"); 9378 verifyFormat("MACRO(A &B);"); 9379 verifyFormat("MACRO(A *B);"); 9380 verifyFormat("void f() { MACRO(A * B); }"); 9381 verifyFormat("void f() { MACRO(A & B); }"); 9382 9383 // This lambda was mis-formatted after D88956 (treating it as a binop): 9384 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 9385 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 9386 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 9387 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 9388 9389 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 9390 verifyFormat("return options != nullptr && operator==(*options);"); 9391 9392 EXPECT_EQ("#define OP(x) \\\n" 9393 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9394 " return s << a.DebugString(); \\\n" 9395 " }", 9396 format("#define OP(x) \\\n" 9397 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9398 " return s << a.DebugString(); \\\n" 9399 " }", 9400 getLLVMStyleWithColumns(50))); 9401 9402 // FIXME: We cannot handle this case yet; we might be able to figure out that 9403 // foo<x> d > v; doesn't make sense. 9404 verifyFormat("foo<a<b && c> d> v;"); 9405 9406 FormatStyle PointerMiddle = getLLVMStyle(); 9407 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 9408 verifyFormat("delete *x;", PointerMiddle); 9409 verifyFormat("int * x;", PointerMiddle); 9410 verifyFormat("int *[] x;", PointerMiddle); 9411 verifyFormat("template <int * y> f() {}", PointerMiddle); 9412 verifyFormat("int * f(int * a) {}", PointerMiddle); 9413 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 9414 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 9415 verifyFormat("A<int *> a;", PointerMiddle); 9416 verifyFormat("A<int **> a;", PointerMiddle); 9417 verifyFormat("A<int *, int *> a;", PointerMiddle); 9418 verifyFormat("A<int *[]> a;", PointerMiddle); 9419 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 9420 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 9421 verifyFormat("T ** t = new T *;", PointerMiddle); 9422 9423 // Member function reference qualifiers aren't binary operators. 9424 verifyFormat("string // break\n" 9425 "operator()() & {}"); 9426 verifyFormat("string // break\n" 9427 "operator()() && {}"); 9428 verifyGoogleFormat("template <typename T>\n" 9429 "auto x() & -> int {}"); 9430 9431 // Should be binary operators when used as an argument expression (overloaded 9432 // operator invoked as a member function). 9433 verifyFormat("void f() { a.operator()(a * a); }"); 9434 verifyFormat("void f() { a->operator()(a & a); }"); 9435 verifyFormat("void f() { a.operator()(*a & *a); }"); 9436 verifyFormat("void f() { a->operator()(*a * *a); }"); 9437 } 9438 9439 TEST_F(FormatTest, UnderstandsAttributes) { 9440 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 9441 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 9442 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9443 FormatStyle AfterType = getLLVMStyle(); 9444 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9445 verifyFormat("__attribute__((nodebug)) void\n" 9446 "foo() {}\n", 9447 AfterType); 9448 verifyFormat("__unused void\n" 9449 "foo() {}", 9450 AfterType); 9451 9452 FormatStyle CustomAttrs = getLLVMStyle(); 9453 CustomAttrs.AttributeMacros.push_back("__unused"); 9454 CustomAttrs.AttributeMacros.push_back("__attr1"); 9455 CustomAttrs.AttributeMacros.push_back("__attr2"); 9456 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 9457 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 9458 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 9459 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 9460 // Check that it is parsed as a multiplication without AttributeMacros and 9461 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 9462 verifyFormat("vector<SomeType * __attr1> v;"); 9463 verifyFormat("vector<SomeType __attr1 *> v;"); 9464 verifyFormat("vector<SomeType __attr1 *const> v;"); 9465 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 9466 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 9467 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 9468 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 9469 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 9470 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 9471 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 9472 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 9473 9474 // Check that these are not parsed as function declarations: 9475 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9476 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 9477 verifyFormat("SomeType s(InitValue);", CustomAttrs); 9478 verifyFormat("SomeType s{InitValue};", CustomAttrs); 9479 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 9480 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 9481 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 9482 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 9483 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 9484 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 9485 } 9486 9487 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 9488 // Check that qualifiers on pointers don't break parsing of casts. 9489 verifyFormat("x = (foo *const)*v;"); 9490 verifyFormat("x = (foo *volatile)*v;"); 9491 verifyFormat("x = (foo *restrict)*v;"); 9492 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 9493 verifyFormat("x = (foo *_Nonnull)*v;"); 9494 verifyFormat("x = (foo *_Nullable)*v;"); 9495 verifyFormat("x = (foo *_Null_unspecified)*v;"); 9496 verifyFormat("x = (foo *_Nonnull)*v;"); 9497 verifyFormat("x = (foo *[[clang::attr]])*v;"); 9498 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 9499 verifyFormat("x = (foo *__ptr32)*v;"); 9500 verifyFormat("x = (foo *__ptr64)*v;"); 9501 verifyFormat("x = (foo *__capability)*v;"); 9502 9503 // Check that we handle multiple trailing qualifiers and skip them all to 9504 // determine that the expression is a cast to a pointer type. 9505 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 9506 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 9507 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 9508 StringRef AllQualifiers = 9509 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 9510 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 9511 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 9512 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 9513 9514 // Also check that address-of is not parsed as a binary bitwise-and: 9515 verifyFormat("x = (foo *const)&v;"); 9516 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 9517 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 9518 9519 // Check custom qualifiers: 9520 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 9521 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9522 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 9523 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 9524 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 9525 CustomQualifier); 9526 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 9527 CustomQualifier); 9528 9529 // Check that unknown identifiers result in binary operator parsing: 9530 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 9531 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 9532 } 9533 9534 TEST_F(FormatTest, UnderstandsSquareAttributes) { 9535 verifyFormat("SomeType s [[unused]] (InitValue);"); 9536 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 9537 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 9538 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 9539 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 9540 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9541 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9542 verifyFormat("[[nodiscard]] bool f() { return false; }"); 9543 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 9544 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 9545 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 9546 9547 // Make sure we do not mistake attributes for array subscripts. 9548 verifyFormat("int a() {}\n" 9549 "[[unused]] int b() {}\n"); 9550 verifyFormat("NSArray *arr;\n" 9551 "arr[[Foo() bar]];"); 9552 9553 // On the other hand, we still need to correctly find array subscripts. 9554 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 9555 9556 // Make sure that we do not mistake Objective-C method inside array literals 9557 // as attributes, even if those method names are also keywords. 9558 verifyFormat("@[ [foo bar] ];"); 9559 verifyFormat("@[ [NSArray class] ];"); 9560 verifyFormat("@[ [foo enum] ];"); 9561 9562 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 9563 9564 // Make sure we do not parse attributes as lambda introducers. 9565 FormatStyle MultiLineFunctions = getLLVMStyle(); 9566 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9567 verifyFormat("[[unused]] int b() {\n" 9568 " return 42;\n" 9569 "}\n", 9570 MultiLineFunctions); 9571 } 9572 9573 TEST_F(FormatTest, AttributeClass) { 9574 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 9575 verifyFormat("class S {\n" 9576 " S(S&&) = default;\n" 9577 "};", 9578 Style); 9579 verifyFormat("class [[nodiscard]] S {\n" 9580 " S(S&&) = default;\n" 9581 "};", 9582 Style); 9583 verifyFormat("class __attribute((maybeunused)) S {\n" 9584 " S(S&&) = default;\n" 9585 "};", 9586 Style); 9587 verifyFormat("struct S {\n" 9588 " S(S&&) = default;\n" 9589 "};", 9590 Style); 9591 verifyFormat("struct [[nodiscard]] S {\n" 9592 " S(S&&) = default;\n" 9593 "};", 9594 Style); 9595 } 9596 9597 TEST_F(FormatTest, AttributesAfterMacro) { 9598 FormatStyle Style = getLLVMStyle(); 9599 verifyFormat("MACRO;\n" 9600 "__attribute__((maybe_unused)) int foo() {\n" 9601 " //...\n" 9602 "}"); 9603 9604 verifyFormat("MACRO;\n" 9605 "[[nodiscard]] int foo() {\n" 9606 " //...\n" 9607 "}"); 9608 9609 EXPECT_EQ("MACRO\n\n" 9610 "__attribute__((maybe_unused)) int foo() {\n" 9611 " //...\n" 9612 "}", 9613 format("MACRO\n\n" 9614 "__attribute__((maybe_unused)) int foo() {\n" 9615 " //...\n" 9616 "}")); 9617 9618 EXPECT_EQ("MACRO\n\n" 9619 "[[nodiscard]] int foo() {\n" 9620 " //...\n" 9621 "}", 9622 format("MACRO\n\n" 9623 "[[nodiscard]] int foo() {\n" 9624 " //...\n" 9625 "}")); 9626 } 9627 9628 TEST_F(FormatTest, AttributePenaltyBreaking) { 9629 FormatStyle Style = getLLVMStyle(); 9630 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 9631 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9632 Style); 9633 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 9634 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9635 Style); 9636 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 9637 "shared_ptr<ALongTypeName> &C d) {\n}", 9638 Style); 9639 } 9640 9641 TEST_F(FormatTest, UnderstandsEllipsis) { 9642 FormatStyle Style = getLLVMStyle(); 9643 verifyFormat("int printf(const char *fmt, ...);"); 9644 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 9645 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 9646 9647 verifyFormat("template <int *...PP> a;", Style); 9648 9649 Style.PointerAlignment = FormatStyle::PAS_Left; 9650 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 9651 9652 verifyFormat("template <int*... PP> a;", Style); 9653 9654 Style.PointerAlignment = FormatStyle::PAS_Middle; 9655 verifyFormat("template <int *... PP> a;", Style); 9656 } 9657 9658 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 9659 EXPECT_EQ("int *a;\n" 9660 "int *a;\n" 9661 "int *a;", 9662 format("int *a;\n" 9663 "int* a;\n" 9664 "int *a;", 9665 getGoogleStyle())); 9666 EXPECT_EQ("int* a;\n" 9667 "int* a;\n" 9668 "int* a;", 9669 format("int* a;\n" 9670 "int* a;\n" 9671 "int *a;", 9672 getGoogleStyle())); 9673 EXPECT_EQ("int *a;\n" 9674 "int *a;\n" 9675 "int *a;", 9676 format("int *a;\n" 9677 "int * a;\n" 9678 "int * a;", 9679 getGoogleStyle())); 9680 EXPECT_EQ("auto x = [] {\n" 9681 " int *a;\n" 9682 " int *a;\n" 9683 " int *a;\n" 9684 "};", 9685 format("auto x=[]{int *a;\n" 9686 "int * a;\n" 9687 "int * a;};", 9688 getGoogleStyle())); 9689 } 9690 9691 TEST_F(FormatTest, UnderstandsRvalueReferences) { 9692 verifyFormat("int f(int &&a) {}"); 9693 verifyFormat("int f(int a, char &&b) {}"); 9694 verifyFormat("void f() { int &&a = b; }"); 9695 verifyGoogleFormat("int f(int a, char&& b) {}"); 9696 verifyGoogleFormat("void f() { int&& a = b; }"); 9697 9698 verifyIndependentOfContext("A<int &&> a;"); 9699 verifyIndependentOfContext("A<int &&, int &&> a;"); 9700 verifyGoogleFormat("A<int&&> a;"); 9701 verifyGoogleFormat("A<int&&, int&&> a;"); 9702 9703 // Not rvalue references: 9704 verifyFormat("template <bool B, bool C> class A {\n" 9705 " static_assert(B && C, \"Something is wrong\");\n" 9706 "};"); 9707 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 9708 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 9709 verifyFormat("#define A(a, b) (a && b)"); 9710 } 9711 9712 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 9713 verifyFormat("void f() {\n" 9714 " x[aaaaaaaaa -\n" 9715 " b] = 23;\n" 9716 "}", 9717 getLLVMStyleWithColumns(15)); 9718 } 9719 9720 TEST_F(FormatTest, FormatsCasts) { 9721 verifyFormat("Type *A = static_cast<Type *>(P);"); 9722 verifyFormat("Type *A = (Type *)P;"); 9723 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 9724 verifyFormat("int a = (int)(2.0f);"); 9725 verifyFormat("int a = (int)2.0f;"); 9726 verifyFormat("x[(int32)y];"); 9727 verifyFormat("x = (int32)y;"); 9728 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 9729 verifyFormat("int a = (int)*b;"); 9730 verifyFormat("int a = (int)2.0f;"); 9731 verifyFormat("int a = (int)~0;"); 9732 verifyFormat("int a = (int)++a;"); 9733 verifyFormat("int a = (int)sizeof(int);"); 9734 verifyFormat("int a = (int)+2;"); 9735 verifyFormat("my_int a = (my_int)2.0f;"); 9736 verifyFormat("my_int a = (my_int)sizeof(int);"); 9737 verifyFormat("return (my_int)aaa;"); 9738 verifyFormat("#define x ((int)-1)"); 9739 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 9740 verifyFormat("#define p(q) ((int *)&q)"); 9741 verifyFormat("fn(a)(b) + 1;"); 9742 9743 verifyFormat("void f() { my_int a = (my_int)*b; }"); 9744 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 9745 verifyFormat("my_int a = (my_int)~0;"); 9746 verifyFormat("my_int a = (my_int)++a;"); 9747 verifyFormat("my_int a = (my_int)-2;"); 9748 verifyFormat("my_int a = (my_int)1;"); 9749 verifyFormat("my_int a = (my_int *)1;"); 9750 verifyFormat("my_int a = (const my_int)-1;"); 9751 verifyFormat("my_int a = (const my_int *)-1;"); 9752 verifyFormat("my_int a = (my_int)(my_int)-1;"); 9753 verifyFormat("my_int a = (ns::my_int)-2;"); 9754 verifyFormat("case (my_int)ONE:"); 9755 verifyFormat("auto x = (X)this;"); 9756 // Casts in Obj-C style calls used to not be recognized as such. 9757 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 9758 9759 // FIXME: single value wrapped with paren will be treated as cast. 9760 verifyFormat("void f(int i = (kValue)*kMask) {}"); 9761 9762 verifyFormat("{ (void)F; }"); 9763 9764 // Don't break after a cast's 9765 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9766 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 9767 " bbbbbbbbbbbbbbbbbbbbbb);"); 9768 9769 // These are not casts. 9770 verifyFormat("void f(int *) {}"); 9771 verifyFormat("f(foo)->b;"); 9772 verifyFormat("f(foo).b;"); 9773 verifyFormat("f(foo)(b);"); 9774 verifyFormat("f(foo)[b];"); 9775 verifyFormat("[](foo) { return 4; }(bar);"); 9776 verifyFormat("(*funptr)(foo)[4];"); 9777 verifyFormat("funptrs[4](foo)[4];"); 9778 verifyFormat("void f(int *);"); 9779 verifyFormat("void f(int *) = 0;"); 9780 verifyFormat("void f(SmallVector<int>) {}"); 9781 verifyFormat("void f(SmallVector<int>);"); 9782 verifyFormat("void f(SmallVector<int>) = 0;"); 9783 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 9784 verifyFormat("int a = sizeof(int) * b;"); 9785 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 9786 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 9787 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 9788 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 9789 9790 // These are not casts, but at some point were confused with casts. 9791 verifyFormat("virtual void foo(int *) override;"); 9792 verifyFormat("virtual void foo(char &) const;"); 9793 verifyFormat("virtual void foo(int *a, char *) const;"); 9794 verifyFormat("int a = sizeof(int *) + b;"); 9795 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 9796 verifyFormat("bool b = f(g<int>) && c;"); 9797 verifyFormat("typedef void (*f)(int i) func;"); 9798 verifyFormat("void operator++(int) noexcept;"); 9799 verifyFormat("void operator++(int &) noexcept;"); 9800 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 9801 "&) noexcept;"); 9802 verifyFormat( 9803 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 9804 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 9805 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 9806 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 9807 verifyFormat("void operator delete(foo &) noexcept;"); 9808 verifyFormat("void operator delete(foo) noexcept;"); 9809 verifyFormat("void operator delete(int) noexcept;"); 9810 verifyFormat("void operator delete(int &) noexcept;"); 9811 verifyFormat("void operator delete(int &) volatile noexcept;"); 9812 verifyFormat("void operator delete(int &) const"); 9813 verifyFormat("void operator delete(int &) = default"); 9814 verifyFormat("void operator delete(int &) = delete"); 9815 verifyFormat("void operator delete(int &) [[noreturn]]"); 9816 verifyFormat("void operator delete(int &) throw();"); 9817 verifyFormat("void operator delete(int &) throw(int);"); 9818 verifyFormat("auto operator delete(int &) -> int;"); 9819 verifyFormat("auto operator delete(int &) override"); 9820 verifyFormat("auto operator delete(int &) final"); 9821 9822 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 9823 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 9824 // FIXME: The indentation here is not ideal. 9825 verifyFormat( 9826 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9827 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 9828 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 9829 } 9830 9831 TEST_F(FormatTest, FormatsFunctionTypes) { 9832 verifyFormat("A<bool()> a;"); 9833 verifyFormat("A<SomeType()> a;"); 9834 verifyFormat("A<void (*)(int, std::string)> a;"); 9835 verifyFormat("A<void *(int)>;"); 9836 verifyFormat("void *(*a)(int *, SomeType *);"); 9837 verifyFormat("int (*func)(void *);"); 9838 verifyFormat("void f() { int (*func)(void *); }"); 9839 verifyFormat("template <class CallbackClass>\n" 9840 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 9841 9842 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 9843 verifyGoogleFormat("void* (*a)(int);"); 9844 verifyGoogleFormat( 9845 "template <class CallbackClass>\n" 9846 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 9847 9848 // Other constructs can look somewhat like function types: 9849 verifyFormat("A<sizeof(*x)> a;"); 9850 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 9851 verifyFormat("some_var = function(*some_pointer_var)[0];"); 9852 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 9853 verifyFormat("int x = f(&h)();"); 9854 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 9855 verifyFormat("std::function<\n" 9856 " LooooooooooongTemplatedType<\n" 9857 " SomeType>*(\n" 9858 " LooooooooooooooooongType type)>\n" 9859 " function;", 9860 getGoogleStyleWithColumns(40)); 9861 } 9862 9863 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 9864 verifyFormat("A (*foo_)[6];"); 9865 verifyFormat("vector<int> (*foo_)[6];"); 9866 } 9867 9868 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 9869 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9870 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 9871 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 9872 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 9873 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9874 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 9875 9876 // Different ways of ()-initializiation. 9877 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9878 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 9879 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9880 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 9881 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9882 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 9883 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9884 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 9885 9886 // Lambdas should not confuse the variable declaration heuristic. 9887 verifyFormat("LooooooooooooooooongType\n" 9888 " variable(nullptr, [](A *a) {});", 9889 getLLVMStyleWithColumns(40)); 9890 } 9891 9892 TEST_F(FormatTest, BreaksLongDeclarations) { 9893 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 9894 " AnotherNameForTheLongType;"); 9895 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 9896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9897 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9898 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 9899 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 9900 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 9901 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9902 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9903 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 9904 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9905 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 9906 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9907 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 9908 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9909 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 9910 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9911 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 9912 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9913 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 9914 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9915 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9916 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 9917 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9918 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 9919 FormatStyle Indented = getLLVMStyle(); 9920 Indented.IndentWrappedFunctionNames = true; 9921 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9922 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 9923 Indented); 9924 verifyFormat( 9925 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9926 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 9927 Indented); 9928 verifyFormat( 9929 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 9930 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 9931 Indented); 9932 verifyFormat( 9933 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 9934 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 9935 Indented); 9936 9937 // FIXME: Without the comment, this breaks after "(". 9938 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 9939 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 9940 getGoogleStyle()); 9941 9942 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 9943 " int LoooooooooooooooooooongParam2) {}"); 9944 verifyFormat( 9945 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 9946 " SourceLocation L, IdentifierIn *II,\n" 9947 " Type *T) {}"); 9948 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 9949 "ReallyReaaallyLongFunctionName(\n" 9950 " const std::string &SomeParameter,\n" 9951 " const SomeType<string, SomeOtherTemplateParameter>\n" 9952 " &ReallyReallyLongParameterName,\n" 9953 " const SomeType<string, SomeOtherTemplateParameter>\n" 9954 " &AnotherLongParameterName) {}"); 9955 verifyFormat("template <typename A>\n" 9956 "SomeLoooooooooooooooooooooongType<\n" 9957 " typename some_namespace::SomeOtherType<A>::Type>\n" 9958 "Function() {}"); 9959 9960 verifyGoogleFormat( 9961 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 9962 " aaaaaaaaaaaaaaaaaaaaaaa;"); 9963 verifyGoogleFormat( 9964 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 9965 " SourceLocation L) {}"); 9966 verifyGoogleFormat( 9967 "some_namespace::LongReturnType\n" 9968 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 9969 " int first_long_parameter, int second_parameter) {}"); 9970 9971 verifyGoogleFormat("template <typename T>\n" 9972 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 9973 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 9974 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9975 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 9976 9977 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 9978 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9979 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9980 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9981 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 9982 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 9983 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9984 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 9985 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 9986 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9987 9988 verifyFormat("template <typename T> // Templates on own line.\n" 9989 "static int // Some comment.\n" 9990 "MyFunction(int a);", 9991 getLLVMStyle()); 9992 } 9993 9994 TEST_F(FormatTest, FormatsAccessModifiers) { 9995 FormatStyle Style = getLLVMStyle(); 9996 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 9997 FormatStyle::ELBAMS_LogicalBlock); 9998 verifyFormat("struct foo {\n" 9999 "private:\n" 10000 " void f() {}\n" 10001 "\n" 10002 "private:\n" 10003 " int i;\n" 10004 "\n" 10005 "protected:\n" 10006 " int j;\n" 10007 "};\n", 10008 Style); 10009 verifyFormat("struct foo {\n" 10010 "private:\n" 10011 " void f() {}\n" 10012 "\n" 10013 "private:\n" 10014 " int i;\n" 10015 "\n" 10016 "protected:\n" 10017 " int j;\n" 10018 "};\n", 10019 "struct foo {\n" 10020 "private:\n" 10021 " void f() {}\n" 10022 "private:\n" 10023 " int i;\n" 10024 "protected:\n" 10025 " int j;\n" 10026 "};\n", 10027 Style); 10028 verifyFormat("struct foo { /* comment */\n" 10029 "private:\n" 10030 " int i;\n" 10031 " // comment\n" 10032 "private:\n" 10033 " int j;\n" 10034 "};\n", 10035 Style); 10036 verifyFormat("struct foo {\n" 10037 "#ifdef FOO\n" 10038 "#endif\n" 10039 "private:\n" 10040 " int i;\n" 10041 "#ifdef FOO\n" 10042 "private:\n" 10043 "#endif\n" 10044 " int j;\n" 10045 "};\n", 10046 Style); 10047 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10048 verifyFormat("struct foo {\n" 10049 "private:\n" 10050 " void f() {}\n" 10051 "private:\n" 10052 " int i;\n" 10053 "protected:\n" 10054 " int j;\n" 10055 "};\n", 10056 Style); 10057 verifyFormat("struct foo {\n" 10058 "private:\n" 10059 " void f() {}\n" 10060 "private:\n" 10061 " int i;\n" 10062 "protected:\n" 10063 " int j;\n" 10064 "};\n", 10065 "struct foo {\n" 10066 "\n" 10067 "private:\n" 10068 " void f() {}\n" 10069 "\n" 10070 "private:\n" 10071 " int i;\n" 10072 "\n" 10073 "protected:\n" 10074 " int j;\n" 10075 "};\n", 10076 Style); 10077 verifyFormat("struct foo { /* comment */\n" 10078 "private:\n" 10079 " int i;\n" 10080 " // comment\n" 10081 "private:\n" 10082 " int j;\n" 10083 "};\n", 10084 "struct foo { /* comment */\n" 10085 "\n" 10086 "private:\n" 10087 " int i;\n" 10088 " // comment\n" 10089 "\n" 10090 "private:\n" 10091 " int j;\n" 10092 "};\n", 10093 Style); 10094 verifyFormat("struct foo {\n" 10095 "#ifdef FOO\n" 10096 "#endif\n" 10097 "private:\n" 10098 " int i;\n" 10099 "#ifdef FOO\n" 10100 "private:\n" 10101 "#endif\n" 10102 " int j;\n" 10103 "};\n", 10104 "struct foo {\n" 10105 "#ifdef FOO\n" 10106 "#endif\n" 10107 "\n" 10108 "private:\n" 10109 " int i;\n" 10110 "#ifdef FOO\n" 10111 "\n" 10112 "private:\n" 10113 "#endif\n" 10114 " int j;\n" 10115 "};\n", 10116 Style); 10117 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10118 verifyFormat("struct foo {\n" 10119 "private:\n" 10120 " void f() {}\n" 10121 "\n" 10122 "private:\n" 10123 " int i;\n" 10124 "\n" 10125 "protected:\n" 10126 " int j;\n" 10127 "};\n", 10128 Style); 10129 verifyFormat("struct foo {\n" 10130 "private:\n" 10131 " void f() {}\n" 10132 "\n" 10133 "private:\n" 10134 " int i;\n" 10135 "\n" 10136 "protected:\n" 10137 " int j;\n" 10138 "};\n", 10139 "struct foo {\n" 10140 "private:\n" 10141 " void f() {}\n" 10142 "private:\n" 10143 " int i;\n" 10144 "protected:\n" 10145 " int j;\n" 10146 "};\n", 10147 Style); 10148 verifyFormat("struct foo { /* comment */\n" 10149 "private:\n" 10150 " int i;\n" 10151 " // comment\n" 10152 "\n" 10153 "private:\n" 10154 " int j;\n" 10155 "};\n", 10156 "struct foo { /* comment */\n" 10157 "private:\n" 10158 " int i;\n" 10159 " // comment\n" 10160 "\n" 10161 "private:\n" 10162 " int j;\n" 10163 "};\n", 10164 Style); 10165 verifyFormat("struct foo {\n" 10166 "#ifdef FOO\n" 10167 "#endif\n" 10168 "\n" 10169 "private:\n" 10170 " int i;\n" 10171 "#ifdef FOO\n" 10172 "\n" 10173 "private:\n" 10174 "#endif\n" 10175 " int j;\n" 10176 "};\n", 10177 "struct foo {\n" 10178 "#ifdef FOO\n" 10179 "#endif\n" 10180 "private:\n" 10181 " int i;\n" 10182 "#ifdef FOO\n" 10183 "private:\n" 10184 "#endif\n" 10185 " int j;\n" 10186 "};\n", 10187 Style); 10188 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10189 EXPECT_EQ("struct foo {\n" 10190 "\n" 10191 "private:\n" 10192 " void f() {}\n" 10193 "\n" 10194 "private:\n" 10195 " int i;\n" 10196 "\n" 10197 "protected:\n" 10198 " int j;\n" 10199 "};\n", 10200 format("struct foo {\n" 10201 "\n" 10202 "private:\n" 10203 " void f() {}\n" 10204 "\n" 10205 "private:\n" 10206 " int i;\n" 10207 "\n" 10208 "protected:\n" 10209 " int j;\n" 10210 "};\n", 10211 Style)); 10212 verifyFormat("struct foo {\n" 10213 "private:\n" 10214 " void f() {}\n" 10215 "private:\n" 10216 " int i;\n" 10217 "protected:\n" 10218 " int j;\n" 10219 "};\n", 10220 Style); 10221 EXPECT_EQ("struct foo { /* comment */\n" 10222 "\n" 10223 "private:\n" 10224 " int i;\n" 10225 " // comment\n" 10226 "\n" 10227 "private:\n" 10228 " int j;\n" 10229 "};\n", 10230 format("struct foo { /* comment */\n" 10231 "\n" 10232 "private:\n" 10233 " int i;\n" 10234 " // comment\n" 10235 "\n" 10236 "private:\n" 10237 " int j;\n" 10238 "};\n", 10239 Style)); 10240 verifyFormat("struct foo { /* comment */\n" 10241 "private:\n" 10242 " int i;\n" 10243 " // comment\n" 10244 "private:\n" 10245 " int j;\n" 10246 "};\n", 10247 Style); 10248 EXPECT_EQ("struct foo {\n" 10249 "#ifdef FOO\n" 10250 "#endif\n" 10251 "\n" 10252 "private:\n" 10253 " int i;\n" 10254 "#ifdef FOO\n" 10255 "\n" 10256 "private:\n" 10257 "#endif\n" 10258 " int j;\n" 10259 "};\n", 10260 format("struct foo {\n" 10261 "#ifdef FOO\n" 10262 "#endif\n" 10263 "\n" 10264 "private:\n" 10265 " int i;\n" 10266 "#ifdef FOO\n" 10267 "\n" 10268 "private:\n" 10269 "#endif\n" 10270 " int j;\n" 10271 "};\n", 10272 Style)); 10273 verifyFormat("struct foo {\n" 10274 "#ifdef FOO\n" 10275 "#endif\n" 10276 "private:\n" 10277 " int i;\n" 10278 "#ifdef FOO\n" 10279 "private:\n" 10280 "#endif\n" 10281 " int j;\n" 10282 "};\n", 10283 Style); 10284 10285 FormatStyle NoEmptyLines = getLLVMStyle(); 10286 NoEmptyLines.MaxEmptyLinesToKeep = 0; 10287 verifyFormat("struct foo {\n" 10288 "private:\n" 10289 " void f() {}\n" 10290 "\n" 10291 "private:\n" 10292 " int i;\n" 10293 "\n" 10294 "public:\n" 10295 "protected:\n" 10296 " int j;\n" 10297 "};\n", 10298 NoEmptyLines); 10299 10300 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10301 verifyFormat("struct foo {\n" 10302 "private:\n" 10303 " void f() {}\n" 10304 "private:\n" 10305 " int i;\n" 10306 "public:\n" 10307 "protected:\n" 10308 " int j;\n" 10309 "};\n", 10310 NoEmptyLines); 10311 10312 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10313 verifyFormat("struct foo {\n" 10314 "private:\n" 10315 " void f() {}\n" 10316 "\n" 10317 "private:\n" 10318 " int i;\n" 10319 "\n" 10320 "public:\n" 10321 "\n" 10322 "protected:\n" 10323 " int j;\n" 10324 "};\n", 10325 NoEmptyLines); 10326 } 10327 10328 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 10329 10330 FormatStyle Style = getLLVMStyle(); 10331 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 10332 verifyFormat("struct foo {\n" 10333 "private:\n" 10334 " void f() {}\n" 10335 "\n" 10336 "private:\n" 10337 " int i;\n" 10338 "\n" 10339 "protected:\n" 10340 " int j;\n" 10341 "};\n", 10342 Style); 10343 10344 // Check if lines are removed. 10345 verifyFormat("struct foo {\n" 10346 "private:\n" 10347 " void f() {}\n" 10348 "\n" 10349 "private:\n" 10350 " int i;\n" 10351 "\n" 10352 "protected:\n" 10353 " int j;\n" 10354 "};\n", 10355 "struct foo {\n" 10356 "private:\n" 10357 "\n" 10358 " void f() {}\n" 10359 "\n" 10360 "private:\n" 10361 "\n" 10362 " int i;\n" 10363 "\n" 10364 "protected:\n" 10365 "\n" 10366 " int j;\n" 10367 "};\n", 10368 Style); 10369 10370 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10371 verifyFormat("struct foo {\n" 10372 "private:\n" 10373 "\n" 10374 " void f() {}\n" 10375 "\n" 10376 "private:\n" 10377 "\n" 10378 " int i;\n" 10379 "\n" 10380 "protected:\n" 10381 "\n" 10382 " int j;\n" 10383 "};\n", 10384 Style); 10385 10386 // Check if lines are added. 10387 verifyFormat("struct foo {\n" 10388 "private:\n" 10389 "\n" 10390 " void f() {}\n" 10391 "\n" 10392 "private:\n" 10393 "\n" 10394 " int i;\n" 10395 "\n" 10396 "protected:\n" 10397 "\n" 10398 " int j;\n" 10399 "};\n", 10400 "struct foo {\n" 10401 "private:\n" 10402 " void f() {}\n" 10403 "\n" 10404 "private:\n" 10405 " int i;\n" 10406 "\n" 10407 "protected:\n" 10408 " int j;\n" 10409 "};\n", 10410 Style); 10411 10412 // Leave tests rely on the code layout, test::messUp can not be used. 10413 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10414 Style.MaxEmptyLinesToKeep = 0u; 10415 verifyFormat("struct foo {\n" 10416 "private:\n" 10417 " void f() {}\n" 10418 "\n" 10419 "private:\n" 10420 " int i;\n" 10421 "\n" 10422 "protected:\n" 10423 " int j;\n" 10424 "};\n", 10425 Style); 10426 10427 // Check if MaxEmptyLinesToKeep is respected. 10428 EXPECT_EQ("struct foo {\n" 10429 "private:\n" 10430 " void f() {}\n" 10431 "\n" 10432 "private:\n" 10433 " int i;\n" 10434 "\n" 10435 "protected:\n" 10436 " int j;\n" 10437 "};\n", 10438 format("struct foo {\n" 10439 "private:\n" 10440 "\n\n\n" 10441 " void f() {}\n" 10442 "\n" 10443 "private:\n" 10444 "\n\n\n" 10445 " int i;\n" 10446 "\n" 10447 "protected:\n" 10448 "\n\n\n" 10449 " int j;\n" 10450 "};\n", 10451 Style)); 10452 10453 Style.MaxEmptyLinesToKeep = 1u; 10454 EXPECT_EQ("struct foo {\n" 10455 "private:\n" 10456 "\n" 10457 " void f() {}\n" 10458 "\n" 10459 "private:\n" 10460 "\n" 10461 " int i;\n" 10462 "\n" 10463 "protected:\n" 10464 "\n" 10465 " int j;\n" 10466 "};\n", 10467 format("struct foo {\n" 10468 "private:\n" 10469 "\n" 10470 " void f() {}\n" 10471 "\n" 10472 "private:\n" 10473 "\n" 10474 " int i;\n" 10475 "\n" 10476 "protected:\n" 10477 "\n" 10478 " int j;\n" 10479 "};\n", 10480 Style)); 10481 // Check if no lines are kept. 10482 EXPECT_EQ("struct foo {\n" 10483 "private:\n" 10484 " void f() {}\n" 10485 "\n" 10486 "private:\n" 10487 " int i;\n" 10488 "\n" 10489 "protected:\n" 10490 " int j;\n" 10491 "};\n", 10492 format("struct foo {\n" 10493 "private:\n" 10494 " void f() {}\n" 10495 "\n" 10496 "private:\n" 10497 " int i;\n" 10498 "\n" 10499 "protected:\n" 10500 " int j;\n" 10501 "};\n", 10502 Style)); 10503 // Check if MaxEmptyLinesToKeep is respected. 10504 EXPECT_EQ("struct foo {\n" 10505 "private:\n" 10506 "\n" 10507 " void f() {}\n" 10508 "\n" 10509 "private:\n" 10510 "\n" 10511 " int i;\n" 10512 "\n" 10513 "protected:\n" 10514 "\n" 10515 " int j;\n" 10516 "};\n", 10517 format("struct foo {\n" 10518 "private:\n" 10519 "\n\n\n" 10520 " void f() {}\n" 10521 "\n" 10522 "private:\n" 10523 "\n\n\n" 10524 " int i;\n" 10525 "\n" 10526 "protected:\n" 10527 "\n\n\n" 10528 " int j;\n" 10529 "};\n", 10530 Style)); 10531 10532 Style.MaxEmptyLinesToKeep = 10u; 10533 EXPECT_EQ("struct foo {\n" 10534 "private:\n" 10535 "\n\n\n" 10536 " void f() {}\n" 10537 "\n" 10538 "private:\n" 10539 "\n\n\n" 10540 " int i;\n" 10541 "\n" 10542 "protected:\n" 10543 "\n\n\n" 10544 " int j;\n" 10545 "};\n", 10546 format("struct foo {\n" 10547 "private:\n" 10548 "\n\n\n" 10549 " void f() {}\n" 10550 "\n" 10551 "private:\n" 10552 "\n\n\n" 10553 " int i;\n" 10554 "\n" 10555 "protected:\n" 10556 "\n\n\n" 10557 " int j;\n" 10558 "};\n", 10559 Style)); 10560 10561 // Test with comments. 10562 Style = getLLVMStyle(); 10563 verifyFormat("struct foo {\n" 10564 "private:\n" 10565 " // comment\n" 10566 " void f() {}\n" 10567 "\n" 10568 "private: /* comment */\n" 10569 " int i;\n" 10570 "};\n", 10571 Style); 10572 verifyFormat("struct foo {\n" 10573 "private:\n" 10574 " // comment\n" 10575 " void f() {}\n" 10576 "\n" 10577 "private: /* comment */\n" 10578 " int i;\n" 10579 "};\n", 10580 "struct foo {\n" 10581 "private:\n" 10582 "\n" 10583 " // comment\n" 10584 " void f() {}\n" 10585 "\n" 10586 "private: /* comment */\n" 10587 "\n" 10588 " int i;\n" 10589 "};\n", 10590 Style); 10591 10592 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10593 verifyFormat("struct foo {\n" 10594 "private:\n" 10595 "\n" 10596 " // comment\n" 10597 " void f() {}\n" 10598 "\n" 10599 "private: /* comment */\n" 10600 "\n" 10601 " int i;\n" 10602 "};\n", 10603 "struct foo {\n" 10604 "private:\n" 10605 " // comment\n" 10606 " void f() {}\n" 10607 "\n" 10608 "private: /* comment */\n" 10609 " int i;\n" 10610 "};\n", 10611 Style); 10612 verifyFormat("struct foo {\n" 10613 "private:\n" 10614 "\n" 10615 " // comment\n" 10616 " void f() {}\n" 10617 "\n" 10618 "private: /* comment */\n" 10619 "\n" 10620 " int i;\n" 10621 "};\n", 10622 Style); 10623 10624 // Test with preprocessor defines. 10625 Style = getLLVMStyle(); 10626 verifyFormat("struct foo {\n" 10627 "private:\n" 10628 "#ifdef FOO\n" 10629 "#endif\n" 10630 " void f() {}\n" 10631 "};\n", 10632 Style); 10633 verifyFormat("struct foo {\n" 10634 "private:\n" 10635 "#ifdef FOO\n" 10636 "#endif\n" 10637 " void f() {}\n" 10638 "};\n", 10639 "struct foo {\n" 10640 "private:\n" 10641 "\n" 10642 "#ifdef FOO\n" 10643 "#endif\n" 10644 " void f() {}\n" 10645 "};\n", 10646 Style); 10647 10648 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10649 verifyFormat("struct foo {\n" 10650 "private:\n" 10651 "\n" 10652 "#ifdef FOO\n" 10653 "#endif\n" 10654 " void f() {}\n" 10655 "};\n", 10656 "struct foo {\n" 10657 "private:\n" 10658 "#ifdef FOO\n" 10659 "#endif\n" 10660 " void f() {}\n" 10661 "};\n", 10662 Style); 10663 verifyFormat("struct foo {\n" 10664 "private:\n" 10665 "\n" 10666 "#ifdef FOO\n" 10667 "#endif\n" 10668 " void f() {}\n" 10669 "};\n", 10670 Style); 10671 } 10672 10673 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 10674 // Combined tests of EmptyLineAfterAccessModifier and 10675 // EmptyLineBeforeAccessModifier. 10676 FormatStyle Style = getLLVMStyle(); 10677 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10678 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10679 verifyFormat("struct foo {\n" 10680 "private:\n" 10681 "\n" 10682 "protected:\n" 10683 "};\n", 10684 Style); 10685 10686 Style.MaxEmptyLinesToKeep = 10u; 10687 // Both remove all new lines. 10688 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10689 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10690 verifyFormat("struct foo {\n" 10691 "private:\n" 10692 "protected:\n" 10693 "};\n", 10694 "struct foo {\n" 10695 "private:\n" 10696 "\n\n\n" 10697 "protected:\n" 10698 "};\n", 10699 Style); 10700 10701 // Leave tests rely on the code layout, test::messUp can not be used. 10702 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10703 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10704 Style.MaxEmptyLinesToKeep = 10u; 10705 EXPECT_EQ("struct foo {\n" 10706 "private:\n" 10707 "\n\n\n" 10708 "protected:\n" 10709 "};\n", 10710 format("struct foo {\n" 10711 "private:\n" 10712 "\n\n\n" 10713 "protected:\n" 10714 "};\n", 10715 Style)); 10716 Style.MaxEmptyLinesToKeep = 3u; 10717 EXPECT_EQ("struct foo {\n" 10718 "private:\n" 10719 "\n\n\n" 10720 "protected:\n" 10721 "};\n", 10722 format("struct foo {\n" 10723 "private:\n" 10724 "\n\n\n" 10725 "protected:\n" 10726 "};\n", 10727 Style)); 10728 Style.MaxEmptyLinesToKeep = 1u; 10729 EXPECT_EQ("struct foo {\n" 10730 "private:\n" 10731 "\n\n\n" 10732 "protected:\n" 10733 "};\n", 10734 format("struct foo {\n" 10735 "private:\n" 10736 "\n\n\n" 10737 "protected:\n" 10738 "};\n", 10739 Style)); // Based on new lines in original document and not 10740 // on the setting. 10741 10742 Style.MaxEmptyLinesToKeep = 10u; 10743 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10744 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10745 // Newlines are kept if they are greater than zero, 10746 // test::messUp removes all new lines which changes the logic 10747 EXPECT_EQ("struct foo {\n" 10748 "private:\n" 10749 "\n\n\n" 10750 "protected:\n" 10751 "};\n", 10752 format("struct foo {\n" 10753 "private:\n" 10754 "\n\n\n" 10755 "protected:\n" 10756 "};\n", 10757 Style)); 10758 10759 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10760 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10761 // test::messUp removes all new lines which changes the logic 10762 EXPECT_EQ("struct foo {\n" 10763 "private:\n" 10764 "\n\n\n" 10765 "protected:\n" 10766 "};\n", 10767 format("struct foo {\n" 10768 "private:\n" 10769 "\n\n\n" 10770 "protected:\n" 10771 "};\n", 10772 Style)); 10773 10774 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10775 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10776 EXPECT_EQ("struct foo {\n" 10777 "private:\n" 10778 "\n\n\n" 10779 "protected:\n" 10780 "};\n", 10781 format("struct foo {\n" 10782 "private:\n" 10783 "\n\n\n" 10784 "protected:\n" 10785 "};\n", 10786 Style)); // test::messUp removes all new lines which changes 10787 // the logic. 10788 10789 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10790 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10791 verifyFormat("struct foo {\n" 10792 "private:\n" 10793 "protected:\n" 10794 "};\n", 10795 "struct foo {\n" 10796 "private:\n" 10797 "\n\n\n" 10798 "protected:\n" 10799 "};\n", 10800 Style); 10801 10802 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10803 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10804 EXPECT_EQ("struct foo {\n" 10805 "private:\n" 10806 "\n\n\n" 10807 "protected:\n" 10808 "};\n", 10809 format("struct foo {\n" 10810 "private:\n" 10811 "\n\n\n" 10812 "protected:\n" 10813 "};\n", 10814 Style)); // test::messUp removes all new lines which changes 10815 // the logic. 10816 10817 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10818 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10819 verifyFormat("struct foo {\n" 10820 "private:\n" 10821 "protected:\n" 10822 "};\n", 10823 "struct foo {\n" 10824 "private:\n" 10825 "\n\n\n" 10826 "protected:\n" 10827 "};\n", 10828 Style); 10829 10830 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 10831 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10832 verifyFormat("struct foo {\n" 10833 "private:\n" 10834 "protected:\n" 10835 "};\n", 10836 "struct foo {\n" 10837 "private:\n" 10838 "\n\n\n" 10839 "protected:\n" 10840 "};\n", 10841 Style); 10842 10843 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 10844 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10845 verifyFormat("struct foo {\n" 10846 "private:\n" 10847 "protected:\n" 10848 "};\n", 10849 "struct foo {\n" 10850 "private:\n" 10851 "\n\n\n" 10852 "protected:\n" 10853 "};\n", 10854 Style); 10855 10856 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 10857 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10858 verifyFormat("struct foo {\n" 10859 "private:\n" 10860 "protected:\n" 10861 "};\n", 10862 "struct foo {\n" 10863 "private:\n" 10864 "\n\n\n" 10865 "protected:\n" 10866 "};\n", 10867 Style); 10868 } 10869 10870 TEST_F(FormatTest, FormatsArrays) { 10871 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 10872 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 10873 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 10874 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 10875 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 10876 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 10877 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10878 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 10879 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10880 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 10881 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10882 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 10883 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 10884 verifyFormat( 10885 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 10886 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 10887 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 10888 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 10889 " .aaaaaaaaaaaaaaaaaaaaaa();"); 10890 10891 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 10892 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 10893 verifyFormat( 10894 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 10895 " .aaaaaaa[0]\n" 10896 " .aaaaaaaaaaaaaaaaaaaaaa();"); 10897 verifyFormat("a[::b::c];"); 10898 10899 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 10900 10901 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 10902 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 10903 } 10904 10905 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 10906 verifyFormat("(a)->b();"); 10907 verifyFormat("--a;"); 10908 } 10909 10910 TEST_F(FormatTest, HandlesIncludeDirectives) { 10911 verifyFormat("#include <string>\n" 10912 "#include <a/b/c.h>\n" 10913 "#include \"a/b/string\"\n" 10914 "#include \"string.h\"\n" 10915 "#include \"string.h\"\n" 10916 "#include <a-a>\n" 10917 "#include < path with space >\n" 10918 "#include_next <test.h>" 10919 "#include \"abc.h\" // this is included for ABC\n" 10920 "#include \"some long include\" // with a comment\n" 10921 "#include \"some very long include path\"\n" 10922 "#include <some/very/long/include/path>\n", 10923 getLLVMStyleWithColumns(35)); 10924 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 10925 EXPECT_EQ("#include <a>", format("#include<a>")); 10926 10927 verifyFormat("#import <string>"); 10928 verifyFormat("#import <a/b/c.h>"); 10929 verifyFormat("#import \"a/b/string\""); 10930 verifyFormat("#import \"string.h\""); 10931 verifyFormat("#import \"string.h\""); 10932 verifyFormat("#if __has_include(<strstream>)\n" 10933 "#include <strstream>\n" 10934 "#endif"); 10935 10936 verifyFormat("#define MY_IMPORT <a/b>"); 10937 10938 verifyFormat("#if __has_include(<a/b>)"); 10939 verifyFormat("#if __has_include_next(<a/b>)"); 10940 verifyFormat("#define F __has_include(<a/b>)"); 10941 verifyFormat("#define F __has_include_next(<a/b>)"); 10942 10943 // Protocol buffer definition or missing "#". 10944 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 10945 getLLVMStyleWithColumns(30)); 10946 10947 FormatStyle Style = getLLVMStyle(); 10948 Style.AlwaysBreakBeforeMultilineStrings = true; 10949 Style.ColumnLimit = 0; 10950 verifyFormat("#import \"abc.h\"", Style); 10951 10952 // But 'import' might also be a regular C++ namespace. 10953 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10954 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10955 } 10956 10957 //===----------------------------------------------------------------------===// 10958 // Error recovery tests. 10959 //===----------------------------------------------------------------------===// 10960 10961 TEST_F(FormatTest, IncompleteParameterLists) { 10962 FormatStyle NoBinPacking = getLLVMStyle(); 10963 NoBinPacking.BinPackParameters = false; 10964 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 10965 " double *min_x,\n" 10966 " double *max_x,\n" 10967 " double *min_y,\n" 10968 " double *max_y,\n" 10969 " double *min_z,\n" 10970 " double *max_z, ) {}", 10971 NoBinPacking); 10972 } 10973 10974 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 10975 verifyFormat("void f() { return; }\n42"); 10976 verifyFormat("void f() {\n" 10977 " if (0)\n" 10978 " return;\n" 10979 "}\n" 10980 "42"); 10981 verifyFormat("void f() { return }\n42"); 10982 verifyFormat("void f() {\n" 10983 " if (0)\n" 10984 " return\n" 10985 "}\n" 10986 "42"); 10987 } 10988 10989 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 10990 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 10991 EXPECT_EQ("void f() {\n" 10992 " if (a)\n" 10993 " return\n" 10994 "}", 10995 format("void f ( ) { if ( a ) return }")); 10996 EXPECT_EQ("namespace N {\n" 10997 "void f()\n" 10998 "}", 10999 format("namespace N { void f() }")); 11000 EXPECT_EQ("namespace N {\n" 11001 "void f() {}\n" 11002 "void g()\n" 11003 "} // namespace N", 11004 format("namespace N { void f( ) { } void g( ) }")); 11005 } 11006 11007 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11008 verifyFormat("int aaaaaaaa =\n" 11009 " // Overlylongcomment\n" 11010 " b;", 11011 getLLVMStyleWithColumns(20)); 11012 verifyFormat("function(\n" 11013 " ShortArgument,\n" 11014 " LoooooooooooongArgument);\n", 11015 getLLVMStyleWithColumns(20)); 11016 } 11017 11018 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11019 verifyFormat("public:"); 11020 verifyFormat("class A {\n" 11021 "public\n" 11022 " void f() {}\n" 11023 "};"); 11024 verifyFormat("public\n" 11025 "int qwerty;"); 11026 verifyFormat("public\n" 11027 "B {}"); 11028 verifyFormat("public\n" 11029 "{}"); 11030 verifyFormat("public\n" 11031 "B { int x; }"); 11032 } 11033 11034 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11035 verifyFormat("{"); 11036 verifyFormat("#})"); 11037 verifyNoCrash("(/**/[:!] ?[)."); 11038 } 11039 11040 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11041 // Found by oss-fuzz: 11042 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11043 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11044 Style.ColumnLimit = 60; 11045 verifyNoCrash( 11046 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11047 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11048 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11049 Style); 11050 } 11051 11052 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11053 verifyFormat("do {\n}"); 11054 verifyFormat("do {\n}\n" 11055 "f();"); 11056 verifyFormat("do {\n}\n" 11057 "wheeee(fun);"); 11058 verifyFormat("do {\n" 11059 " f();\n" 11060 "}"); 11061 } 11062 11063 TEST_F(FormatTest, IncorrectCodeMissingParens) { 11064 verifyFormat("if {\n foo;\n foo();\n}"); 11065 verifyFormat("switch {\n foo;\n foo();\n}"); 11066 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 11067 verifyFormat("while {\n foo;\n foo();\n}"); 11068 verifyFormat("do {\n foo;\n foo();\n} while;"); 11069 } 11070 11071 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 11072 verifyIncompleteFormat("namespace {\n" 11073 "class Foo { Foo (\n" 11074 "};\n" 11075 "} // namespace"); 11076 } 11077 11078 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 11079 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 11080 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 11081 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 11082 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 11083 11084 EXPECT_EQ("{\n" 11085 " {\n" 11086 " breakme(\n" 11087 " qwe);\n" 11088 " }\n", 11089 format("{\n" 11090 " {\n" 11091 " breakme(qwe);\n" 11092 "}\n", 11093 getLLVMStyleWithColumns(10))); 11094 } 11095 11096 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 11097 verifyFormat("int x = {\n" 11098 " avariable,\n" 11099 " b(alongervariable)};", 11100 getLLVMStyleWithColumns(25)); 11101 } 11102 11103 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 11104 verifyFormat("return (a)(b){1, 2, 3};"); 11105 } 11106 11107 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 11108 verifyFormat("vector<int> x{1, 2, 3, 4};"); 11109 verifyFormat("vector<int> x{\n" 11110 " 1,\n" 11111 " 2,\n" 11112 " 3,\n" 11113 " 4,\n" 11114 "};"); 11115 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 11116 verifyFormat("f({1, 2});"); 11117 verifyFormat("auto v = Foo{-1};"); 11118 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 11119 verifyFormat("Class::Class : member{1, 2, 3} {}"); 11120 verifyFormat("new vector<int>{1, 2, 3};"); 11121 verifyFormat("new int[3]{1, 2, 3};"); 11122 verifyFormat("new int{1};"); 11123 verifyFormat("return {arg1, arg2};"); 11124 verifyFormat("return {arg1, SomeType{parameter}};"); 11125 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 11126 verifyFormat("new T{arg1, arg2};"); 11127 verifyFormat("f(MyMap[{composite, key}]);"); 11128 verifyFormat("class Class {\n" 11129 " T member = {arg1, arg2};\n" 11130 "};"); 11131 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 11132 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 11133 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 11134 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 11135 verifyFormat("int a = std::is_integral<int>{} + 0;"); 11136 11137 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11138 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11139 verifyFormat("auto i = decltype(x){};"); 11140 verifyFormat("auto i = typeof(x){};"); 11141 verifyFormat("auto i = _Atomic(x){};"); 11142 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 11143 verifyFormat("Node n{1, Node{1000}, //\n" 11144 " 2};"); 11145 verifyFormat("Aaaa aaaaaaa{\n" 11146 " {\n" 11147 " aaaa,\n" 11148 " },\n" 11149 "};"); 11150 verifyFormat("class C : public D {\n" 11151 " SomeClass SC{2};\n" 11152 "};"); 11153 verifyFormat("class C : public A {\n" 11154 " class D : public B {\n" 11155 " void f() { int i{2}; }\n" 11156 " };\n" 11157 "};"); 11158 verifyFormat("#define A {a, a},"); 11159 11160 // Avoid breaking between equal sign and opening brace 11161 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 11162 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 11163 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 11164 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 11165 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 11166 " {\"ccccccccccccccccccccc\", 2}};", 11167 AvoidBreakingFirstArgument); 11168 11169 // Binpacking only if there is no trailing comma 11170 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 11171 " cccccccccc, dddddddddd};", 11172 getLLVMStyleWithColumns(50)); 11173 verifyFormat("const Aaaaaa aaaaa = {\n" 11174 " aaaaaaaaaaa,\n" 11175 " bbbbbbbbbbb,\n" 11176 " ccccccccccc,\n" 11177 " ddddddddddd,\n" 11178 "};", 11179 getLLVMStyleWithColumns(50)); 11180 11181 // Cases where distinguising braced lists and blocks is hard. 11182 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 11183 verifyFormat("void f() {\n" 11184 " return; // comment\n" 11185 "}\n" 11186 "SomeType t;"); 11187 verifyFormat("void f() {\n" 11188 " if (a) {\n" 11189 " f();\n" 11190 " }\n" 11191 "}\n" 11192 "SomeType t;"); 11193 11194 // In combination with BinPackArguments = false. 11195 FormatStyle NoBinPacking = getLLVMStyle(); 11196 NoBinPacking.BinPackArguments = false; 11197 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 11198 " bbbbb,\n" 11199 " ccccc,\n" 11200 " ddddd,\n" 11201 " eeeee,\n" 11202 " ffffff,\n" 11203 " ggggg,\n" 11204 " hhhhhh,\n" 11205 " iiiiii,\n" 11206 " jjjjjj,\n" 11207 " kkkkkk};", 11208 NoBinPacking); 11209 verifyFormat("const Aaaaaa aaaaa = {\n" 11210 " aaaaa,\n" 11211 " bbbbb,\n" 11212 " ccccc,\n" 11213 " ddddd,\n" 11214 " eeeee,\n" 11215 " ffffff,\n" 11216 " ggggg,\n" 11217 " hhhhhh,\n" 11218 " iiiiii,\n" 11219 " jjjjjj,\n" 11220 " kkkkkk,\n" 11221 "};", 11222 NoBinPacking); 11223 verifyFormat( 11224 "const Aaaaaa aaaaa = {\n" 11225 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 11226 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 11227 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 11228 "};", 11229 NoBinPacking); 11230 11231 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11232 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 11233 " CDDDP83848_BMCR_REGISTER,\n" 11234 " CDDDP83848_BMSR_REGISTER,\n" 11235 " CDDDP83848_RBR_REGISTER};", 11236 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 11237 " CDDDP83848_BMSR_REGISTER,\n" 11238 " CDDDP83848_RBR_REGISTER};", 11239 NoBinPacking)); 11240 11241 // FIXME: The alignment of these trailing comments might be bad. Then again, 11242 // this might be utterly useless in real code. 11243 verifyFormat("Constructor::Constructor()\n" 11244 " : some_value{ //\n" 11245 " aaaaaaa, //\n" 11246 " bbbbbbb} {}"); 11247 11248 // In braced lists, the first comment is always assumed to belong to the 11249 // first element. Thus, it can be moved to the next or previous line as 11250 // appropriate. 11251 EXPECT_EQ("function({// First element:\n" 11252 " 1,\n" 11253 " // Second element:\n" 11254 " 2});", 11255 format("function({\n" 11256 " // First element:\n" 11257 " 1,\n" 11258 " // Second element:\n" 11259 " 2});")); 11260 EXPECT_EQ("std::vector<int> MyNumbers{\n" 11261 " // First element:\n" 11262 " 1,\n" 11263 " // Second element:\n" 11264 " 2};", 11265 format("std::vector<int> MyNumbers{// First element:\n" 11266 " 1,\n" 11267 " // Second element:\n" 11268 " 2};", 11269 getLLVMStyleWithColumns(30))); 11270 // A trailing comma should still lead to an enforced line break and no 11271 // binpacking. 11272 EXPECT_EQ("vector<int> SomeVector = {\n" 11273 " // aaa\n" 11274 " 1,\n" 11275 " 2,\n" 11276 "};", 11277 format("vector<int> SomeVector = { // aaa\n" 11278 " 1, 2, };")); 11279 11280 // C++11 brace initializer list l-braces should not be treated any differently 11281 // when breaking before lambda bodies is enabled 11282 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 11283 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 11284 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 11285 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 11286 verifyFormat( 11287 "std::runtime_error{\n" 11288 " \"Long string which will force a break onto the next line...\"};", 11289 BreakBeforeLambdaBody); 11290 11291 FormatStyle ExtraSpaces = getLLVMStyle(); 11292 ExtraSpaces.Cpp11BracedListStyle = false; 11293 ExtraSpaces.ColumnLimit = 75; 11294 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 11295 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 11296 verifyFormat("f({ 1, 2 });", ExtraSpaces); 11297 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 11298 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 11299 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 11300 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 11301 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 11302 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 11303 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 11304 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 11305 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 11306 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 11307 verifyFormat("class Class {\n" 11308 " T member = { arg1, arg2 };\n" 11309 "};", 11310 ExtraSpaces); 11311 verifyFormat( 11312 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11313 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 11314 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 11315 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 11316 ExtraSpaces); 11317 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 11318 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 11319 ExtraSpaces); 11320 verifyFormat( 11321 "someFunction(OtherParam,\n" 11322 " BracedList{ // comment 1 (Forcing interesting break)\n" 11323 " param1, param2,\n" 11324 " // comment 2\n" 11325 " param3, param4 });", 11326 ExtraSpaces); 11327 verifyFormat( 11328 "std::this_thread::sleep_for(\n" 11329 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 11330 ExtraSpaces); 11331 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 11332 " aaaaaaa,\n" 11333 " aaaaaaaaaa,\n" 11334 " aaaaa,\n" 11335 " aaaaaaaaaaaaaaa,\n" 11336 " aaa,\n" 11337 " aaaaaaaaaa,\n" 11338 " a,\n" 11339 " aaaaaaaaaaaaaaaaaaaaa,\n" 11340 " aaaaaaaaaaaa,\n" 11341 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 11342 " aaaaaaa,\n" 11343 " a};"); 11344 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 11345 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 11346 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 11347 11348 // Avoid breaking between initializer/equal sign and opening brace 11349 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 11350 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 11351 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11352 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11353 " { \"ccccccccccccccccccccc\", 2 }\n" 11354 "};", 11355 ExtraSpaces); 11356 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 11357 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11358 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11359 " { \"ccccccccccccccccccccc\", 2 }\n" 11360 "};", 11361 ExtraSpaces); 11362 11363 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 11364 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 11365 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 11366 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 11367 11368 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 11369 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 11370 SpaceBetweenBraces.SpacesInParentheses = true; 11371 SpaceBetweenBraces.SpacesInSquareBrackets = true; 11372 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 11373 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 11374 verifyFormat("vector< int > x{ // comment 1\n" 11375 " 1, 2, 3, 4 };", 11376 SpaceBetweenBraces); 11377 SpaceBetweenBraces.ColumnLimit = 20; 11378 EXPECT_EQ("vector< int > x{\n" 11379 " 1, 2, 3, 4 };", 11380 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11381 SpaceBetweenBraces.ColumnLimit = 24; 11382 EXPECT_EQ("vector< int > x{ 1, 2,\n" 11383 " 3, 4 };", 11384 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11385 EXPECT_EQ("vector< int > x{\n" 11386 " 1,\n" 11387 " 2,\n" 11388 " 3,\n" 11389 " 4,\n" 11390 "};", 11391 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 11392 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 11393 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 11394 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 11395 } 11396 11397 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 11398 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11399 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11400 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11401 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11402 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11403 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11404 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 11405 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11406 " 1, 22, 333, 4444, 55555, //\n" 11407 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11408 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11409 verifyFormat( 11410 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11411 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11412 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 11413 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11414 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11415 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11416 " 7777777};"); 11417 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11418 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11419 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11420 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11421 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11422 " // Separating comment.\n" 11423 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11424 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11425 " // Leading comment\n" 11426 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11427 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11428 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11429 " 1, 1, 1, 1};", 11430 getLLVMStyleWithColumns(39)); 11431 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11432 " 1, 1, 1, 1};", 11433 getLLVMStyleWithColumns(38)); 11434 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 11435 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 11436 getLLVMStyleWithColumns(43)); 11437 verifyFormat( 11438 "static unsigned SomeValues[10][3] = {\n" 11439 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 11440 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 11441 verifyFormat("static auto fields = new vector<string>{\n" 11442 " \"aaaaaaaaaaaaa\",\n" 11443 " \"aaaaaaaaaaaaa\",\n" 11444 " \"aaaaaaaaaaaa\",\n" 11445 " \"aaaaaaaaaaaaaa\",\n" 11446 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11447 " \"aaaaaaaaaaaa\",\n" 11448 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11449 "};"); 11450 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 11451 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 11452 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 11453 " 3, cccccccccccccccccccccc};", 11454 getLLVMStyleWithColumns(60)); 11455 11456 // Trailing commas. 11457 verifyFormat("vector<int> x = {\n" 11458 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 11459 "};", 11460 getLLVMStyleWithColumns(39)); 11461 verifyFormat("vector<int> x = {\n" 11462 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 11463 "};", 11464 getLLVMStyleWithColumns(39)); 11465 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11466 " 1, 1, 1, 1,\n" 11467 " /**/ /**/};", 11468 getLLVMStyleWithColumns(39)); 11469 11470 // Trailing comment in the first line. 11471 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 11472 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 11473 " 111111111, 222222222, 3333333333, 444444444, //\n" 11474 " 11111111, 22222222, 333333333, 44444444};"); 11475 // Trailing comment in the last line. 11476 verifyFormat("int aaaaa[] = {\n" 11477 " 1, 2, 3, // comment\n" 11478 " 4, 5, 6 // comment\n" 11479 "};"); 11480 11481 // With nested lists, we should either format one item per line or all nested 11482 // lists one on line. 11483 // FIXME: For some nested lists, we can do better. 11484 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 11485 " {aaaaaaaaaaaaaaaaaaa},\n" 11486 " {aaaaaaaaaaaaaaaaaaaaa},\n" 11487 " {aaaaaaaaaaaaaaaaa}};", 11488 getLLVMStyleWithColumns(60)); 11489 verifyFormat( 11490 "SomeStruct my_struct_array = {\n" 11491 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 11492 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 11493 " {aaa, aaa},\n" 11494 " {aaa, aaa},\n" 11495 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 11496 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 11497 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 11498 11499 // No column layout should be used here. 11500 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 11501 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 11502 11503 verifyNoCrash("a<,"); 11504 11505 // No braced initializer here. 11506 verifyFormat("void f() {\n" 11507 " struct Dummy {};\n" 11508 " f(v);\n" 11509 "}"); 11510 11511 // Long lists should be formatted in columns even if they are nested. 11512 verifyFormat( 11513 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11514 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11515 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11516 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11517 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11518 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 11519 11520 // Allow "single-column" layout even if that violates the column limit. There 11521 // isn't going to be a better way. 11522 verifyFormat("std::vector<int> a = {\n" 11523 " aaaaaaaa,\n" 11524 " aaaaaaaa,\n" 11525 " aaaaaaaa,\n" 11526 " aaaaaaaa,\n" 11527 " aaaaaaaaaa,\n" 11528 " aaaaaaaa,\n" 11529 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 11530 getLLVMStyleWithColumns(30)); 11531 verifyFormat("vector<int> aaaa = {\n" 11532 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11533 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11534 " aaaaaa.aaaaaaa,\n" 11535 " aaaaaa.aaaaaaa,\n" 11536 " aaaaaa.aaaaaaa,\n" 11537 " aaaaaa.aaaaaaa,\n" 11538 "};"); 11539 11540 // Don't create hanging lists. 11541 verifyFormat("someFunction(Param, {List1, List2,\n" 11542 " List3});", 11543 getLLVMStyleWithColumns(35)); 11544 verifyFormat("someFunction(Param, Param,\n" 11545 " {List1, List2,\n" 11546 " List3});", 11547 getLLVMStyleWithColumns(35)); 11548 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 11549 " aaaaaaaaaaaaaaaaaaaaaaa);"); 11550 } 11551 11552 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 11553 FormatStyle DoNotMerge = getLLVMStyle(); 11554 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11555 11556 verifyFormat("void f() { return 42; }"); 11557 verifyFormat("void f() {\n" 11558 " return 42;\n" 11559 "}", 11560 DoNotMerge); 11561 verifyFormat("void f() {\n" 11562 " // Comment\n" 11563 "}"); 11564 verifyFormat("{\n" 11565 "#error {\n" 11566 " int a;\n" 11567 "}"); 11568 verifyFormat("{\n" 11569 " int a;\n" 11570 "#error {\n" 11571 "}"); 11572 verifyFormat("void f() {} // comment"); 11573 verifyFormat("void f() { int a; } // comment"); 11574 verifyFormat("void f() {\n" 11575 "} // comment", 11576 DoNotMerge); 11577 verifyFormat("void f() {\n" 11578 " int a;\n" 11579 "} // comment", 11580 DoNotMerge); 11581 verifyFormat("void f() {\n" 11582 "} // comment", 11583 getLLVMStyleWithColumns(15)); 11584 11585 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 11586 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 11587 11588 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 11589 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 11590 verifyFormat("class C {\n" 11591 " C()\n" 11592 " : iiiiiiii(nullptr),\n" 11593 " kkkkkkk(nullptr),\n" 11594 " mmmmmmm(nullptr),\n" 11595 " nnnnnnn(nullptr) {}\n" 11596 "};", 11597 getGoogleStyle()); 11598 11599 FormatStyle NoColumnLimit = getLLVMStyle(); 11600 NoColumnLimit.ColumnLimit = 0; 11601 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 11602 EXPECT_EQ("class C {\n" 11603 " A() : b(0) {}\n" 11604 "};", 11605 format("class C{A():b(0){}};", NoColumnLimit)); 11606 EXPECT_EQ("A()\n" 11607 " : b(0) {\n" 11608 "}", 11609 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 11610 11611 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 11612 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 11613 FormatStyle::SFS_None; 11614 EXPECT_EQ("A()\n" 11615 " : b(0) {\n" 11616 "}", 11617 format("A():b(0){}", DoNotMergeNoColumnLimit)); 11618 EXPECT_EQ("A()\n" 11619 " : b(0) {\n" 11620 "}", 11621 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 11622 11623 verifyFormat("#define A \\\n" 11624 " void f() { \\\n" 11625 " int i; \\\n" 11626 " }", 11627 getLLVMStyleWithColumns(20)); 11628 verifyFormat("#define A \\\n" 11629 " void f() { int i; }", 11630 getLLVMStyleWithColumns(21)); 11631 verifyFormat("#define A \\\n" 11632 " void f() { \\\n" 11633 " int i; \\\n" 11634 " } \\\n" 11635 " int j;", 11636 getLLVMStyleWithColumns(22)); 11637 verifyFormat("#define A \\\n" 11638 " void f() { int i; } \\\n" 11639 " int j;", 11640 getLLVMStyleWithColumns(23)); 11641 } 11642 11643 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 11644 FormatStyle MergeEmptyOnly = getLLVMStyle(); 11645 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11646 verifyFormat("class C {\n" 11647 " int f() {}\n" 11648 "};", 11649 MergeEmptyOnly); 11650 verifyFormat("class C {\n" 11651 " int f() {\n" 11652 " return 42;\n" 11653 " }\n" 11654 "};", 11655 MergeEmptyOnly); 11656 verifyFormat("int f() {}", MergeEmptyOnly); 11657 verifyFormat("int f() {\n" 11658 " return 42;\n" 11659 "}", 11660 MergeEmptyOnly); 11661 11662 // Also verify behavior when BraceWrapping.AfterFunction = true 11663 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11664 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 11665 verifyFormat("int f() {}", MergeEmptyOnly); 11666 verifyFormat("class C {\n" 11667 " int f() {}\n" 11668 "};", 11669 MergeEmptyOnly); 11670 } 11671 11672 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 11673 FormatStyle MergeInlineOnly = getLLVMStyle(); 11674 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11675 verifyFormat("class C {\n" 11676 " int f() { return 42; }\n" 11677 "};", 11678 MergeInlineOnly); 11679 verifyFormat("int f() {\n" 11680 " return 42;\n" 11681 "}", 11682 MergeInlineOnly); 11683 11684 // SFS_Inline implies SFS_Empty 11685 verifyFormat("class C {\n" 11686 " int f() {}\n" 11687 "};", 11688 MergeInlineOnly); 11689 verifyFormat("int f() {}", MergeInlineOnly); 11690 11691 // Also verify behavior when BraceWrapping.AfterFunction = true 11692 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11693 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11694 verifyFormat("class C {\n" 11695 " int f() { return 42; }\n" 11696 "};", 11697 MergeInlineOnly); 11698 verifyFormat("int f()\n" 11699 "{\n" 11700 " return 42;\n" 11701 "}", 11702 MergeInlineOnly); 11703 11704 // SFS_Inline implies SFS_Empty 11705 verifyFormat("int f() {}", MergeInlineOnly); 11706 verifyFormat("class C {\n" 11707 " int f() {}\n" 11708 "};", 11709 MergeInlineOnly); 11710 } 11711 11712 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 11713 FormatStyle MergeInlineOnly = getLLVMStyle(); 11714 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 11715 FormatStyle::SFS_InlineOnly; 11716 verifyFormat("class C {\n" 11717 " int f() { return 42; }\n" 11718 "};", 11719 MergeInlineOnly); 11720 verifyFormat("int f() {\n" 11721 " return 42;\n" 11722 "}", 11723 MergeInlineOnly); 11724 11725 // SFS_InlineOnly does not imply SFS_Empty 11726 verifyFormat("class C {\n" 11727 " int f() {}\n" 11728 "};", 11729 MergeInlineOnly); 11730 verifyFormat("int f() {\n" 11731 "}", 11732 MergeInlineOnly); 11733 11734 // Also verify behavior when BraceWrapping.AfterFunction = true 11735 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11736 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11737 verifyFormat("class C {\n" 11738 " int f() { return 42; }\n" 11739 "};", 11740 MergeInlineOnly); 11741 verifyFormat("int f()\n" 11742 "{\n" 11743 " return 42;\n" 11744 "}", 11745 MergeInlineOnly); 11746 11747 // SFS_InlineOnly does not imply SFS_Empty 11748 verifyFormat("int f()\n" 11749 "{\n" 11750 "}", 11751 MergeInlineOnly); 11752 verifyFormat("class C {\n" 11753 " int f() {}\n" 11754 "};", 11755 MergeInlineOnly); 11756 } 11757 11758 TEST_F(FormatTest, SplitEmptyFunction) { 11759 FormatStyle Style = getLLVMStyle(); 11760 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11761 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11762 Style.BraceWrapping.AfterFunction = true; 11763 Style.BraceWrapping.SplitEmptyFunction = false; 11764 Style.ColumnLimit = 40; 11765 11766 verifyFormat("int f()\n" 11767 "{}", 11768 Style); 11769 verifyFormat("int f()\n" 11770 "{\n" 11771 " return 42;\n" 11772 "}", 11773 Style); 11774 verifyFormat("int f()\n" 11775 "{\n" 11776 " // some comment\n" 11777 "}", 11778 Style); 11779 11780 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11781 verifyFormat("int f() {}", Style); 11782 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11783 "{}", 11784 Style); 11785 verifyFormat("int f()\n" 11786 "{\n" 11787 " return 0;\n" 11788 "}", 11789 Style); 11790 11791 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11792 verifyFormat("class Foo {\n" 11793 " int f() {}\n" 11794 "};\n", 11795 Style); 11796 verifyFormat("class Foo {\n" 11797 " int f() { return 0; }\n" 11798 "};\n", 11799 Style); 11800 verifyFormat("class Foo {\n" 11801 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11802 " {}\n" 11803 "};\n", 11804 Style); 11805 verifyFormat("class Foo {\n" 11806 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11807 " {\n" 11808 " return 0;\n" 11809 " }\n" 11810 "};\n", 11811 Style); 11812 11813 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11814 verifyFormat("int f() {}", Style); 11815 verifyFormat("int f() { return 0; }", Style); 11816 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11817 "{}", 11818 Style); 11819 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11820 "{\n" 11821 " return 0;\n" 11822 "}", 11823 Style); 11824 } 11825 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 11826 FormatStyle Style = getLLVMStyle(); 11827 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11828 verifyFormat("#ifdef A\n" 11829 "int f() {}\n" 11830 "#else\n" 11831 "int g() {}\n" 11832 "#endif", 11833 Style); 11834 } 11835 11836 TEST_F(FormatTest, SplitEmptyClass) { 11837 FormatStyle Style = getLLVMStyle(); 11838 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11839 Style.BraceWrapping.AfterClass = true; 11840 Style.BraceWrapping.SplitEmptyRecord = false; 11841 11842 verifyFormat("class Foo\n" 11843 "{};", 11844 Style); 11845 verifyFormat("/* something */ class Foo\n" 11846 "{};", 11847 Style); 11848 verifyFormat("template <typename X> class Foo\n" 11849 "{};", 11850 Style); 11851 verifyFormat("class Foo\n" 11852 "{\n" 11853 " Foo();\n" 11854 "};", 11855 Style); 11856 verifyFormat("typedef class Foo\n" 11857 "{\n" 11858 "} Foo_t;", 11859 Style); 11860 11861 Style.BraceWrapping.SplitEmptyRecord = true; 11862 Style.BraceWrapping.AfterStruct = true; 11863 verifyFormat("class rep\n" 11864 "{\n" 11865 "};", 11866 Style); 11867 verifyFormat("struct rep\n" 11868 "{\n" 11869 "};", 11870 Style); 11871 verifyFormat("template <typename T> class rep\n" 11872 "{\n" 11873 "};", 11874 Style); 11875 verifyFormat("template <typename T> struct rep\n" 11876 "{\n" 11877 "};", 11878 Style); 11879 verifyFormat("class rep\n" 11880 "{\n" 11881 " int x;\n" 11882 "};", 11883 Style); 11884 verifyFormat("struct rep\n" 11885 "{\n" 11886 " int x;\n" 11887 "};", 11888 Style); 11889 verifyFormat("template <typename T> class rep\n" 11890 "{\n" 11891 " int x;\n" 11892 "};", 11893 Style); 11894 verifyFormat("template <typename T> struct rep\n" 11895 "{\n" 11896 " int x;\n" 11897 "};", 11898 Style); 11899 verifyFormat("template <typename T> class rep // Foo\n" 11900 "{\n" 11901 " int x;\n" 11902 "};", 11903 Style); 11904 verifyFormat("template <typename T> struct rep // Bar\n" 11905 "{\n" 11906 " int x;\n" 11907 "};", 11908 Style); 11909 11910 verifyFormat("template <typename T> class rep<T>\n" 11911 "{\n" 11912 " int x;\n" 11913 "};", 11914 Style); 11915 11916 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 11917 "{\n" 11918 " int x;\n" 11919 "};", 11920 Style); 11921 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 11922 "{\n" 11923 "};", 11924 Style); 11925 11926 verifyFormat("#include \"stdint.h\"\n" 11927 "namespace rep {}", 11928 Style); 11929 verifyFormat("#include <stdint.h>\n" 11930 "namespace rep {}", 11931 Style); 11932 verifyFormat("#include <stdint.h>\n" 11933 "namespace rep {}", 11934 "#include <stdint.h>\n" 11935 "namespace rep {\n" 11936 "\n" 11937 "\n" 11938 "}", 11939 Style); 11940 } 11941 11942 TEST_F(FormatTest, SplitEmptyStruct) { 11943 FormatStyle Style = getLLVMStyle(); 11944 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11945 Style.BraceWrapping.AfterStruct = true; 11946 Style.BraceWrapping.SplitEmptyRecord = false; 11947 11948 verifyFormat("struct Foo\n" 11949 "{};", 11950 Style); 11951 verifyFormat("/* something */ struct Foo\n" 11952 "{};", 11953 Style); 11954 verifyFormat("template <typename X> struct Foo\n" 11955 "{};", 11956 Style); 11957 verifyFormat("struct Foo\n" 11958 "{\n" 11959 " Foo();\n" 11960 "};", 11961 Style); 11962 verifyFormat("typedef struct Foo\n" 11963 "{\n" 11964 "} Foo_t;", 11965 Style); 11966 // typedef struct Bar {} Bar_t; 11967 } 11968 11969 TEST_F(FormatTest, SplitEmptyUnion) { 11970 FormatStyle Style = getLLVMStyle(); 11971 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11972 Style.BraceWrapping.AfterUnion = true; 11973 Style.BraceWrapping.SplitEmptyRecord = false; 11974 11975 verifyFormat("union Foo\n" 11976 "{};", 11977 Style); 11978 verifyFormat("/* something */ union Foo\n" 11979 "{};", 11980 Style); 11981 verifyFormat("union Foo\n" 11982 "{\n" 11983 " A,\n" 11984 "};", 11985 Style); 11986 verifyFormat("typedef union Foo\n" 11987 "{\n" 11988 "} Foo_t;", 11989 Style); 11990 } 11991 11992 TEST_F(FormatTest, SplitEmptyNamespace) { 11993 FormatStyle Style = getLLVMStyle(); 11994 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11995 Style.BraceWrapping.AfterNamespace = true; 11996 Style.BraceWrapping.SplitEmptyNamespace = false; 11997 11998 verifyFormat("namespace Foo\n" 11999 "{};", 12000 Style); 12001 verifyFormat("/* something */ namespace Foo\n" 12002 "{};", 12003 Style); 12004 verifyFormat("inline namespace Foo\n" 12005 "{};", 12006 Style); 12007 verifyFormat("/* something */ inline namespace Foo\n" 12008 "{};", 12009 Style); 12010 verifyFormat("export namespace Foo\n" 12011 "{};", 12012 Style); 12013 verifyFormat("namespace Foo\n" 12014 "{\n" 12015 "void Bar();\n" 12016 "};", 12017 Style); 12018 } 12019 12020 TEST_F(FormatTest, NeverMergeShortRecords) { 12021 FormatStyle Style = getLLVMStyle(); 12022 12023 verifyFormat("class Foo {\n" 12024 " Foo();\n" 12025 "};", 12026 Style); 12027 verifyFormat("typedef class Foo {\n" 12028 " Foo();\n" 12029 "} Foo_t;", 12030 Style); 12031 verifyFormat("struct Foo {\n" 12032 " Foo();\n" 12033 "};", 12034 Style); 12035 verifyFormat("typedef struct Foo {\n" 12036 " Foo();\n" 12037 "} Foo_t;", 12038 Style); 12039 verifyFormat("union Foo {\n" 12040 " A,\n" 12041 "};", 12042 Style); 12043 verifyFormat("typedef union Foo {\n" 12044 " A,\n" 12045 "} Foo_t;", 12046 Style); 12047 verifyFormat("namespace Foo {\n" 12048 "void Bar();\n" 12049 "};", 12050 Style); 12051 12052 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12053 Style.BraceWrapping.AfterClass = true; 12054 Style.BraceWrapping.AfterStruct = true; 12055 Style.BraceWrapping.AfterUnion = true; 12056 Style.BraceWrapping.AfterNamespace = true; 12057 verifyFormat("class Foo\n" 12058 "{\n" 12059 " Foo();\n" 12060 "};", 12061 Style); 12062 verifyFormat("typedef class Foo\n" 12063 "{\n" 12064 " Foo();\n" 12065 "} Foo_t;", 12066 Style); 12067 verifyFormat("struct Foo\n" 12068 "{\n" 12069 " Foo();\n" 12070 "};", 12071 Style); 12072 verifyFormat("typedef struct Foo\n" 12073 "{\n" 12074 " Foo();\n" 12075 "} Foo_t;", 12076 Style); 12077 verifyFormat("union Foo\n" 12078 "{\n" 12079 " A,\n" 12080 "};", 12081 Style); 12082 verifyFormat("typedef union Foo\n" 12083 "{\n" 12084 " A,\n" 12085 "} Foo_t;", 12086 Style); 12087 verifyFormat("namespace Foo\n" 12088 "{\n" 12089 "void Bar();\n" 12090 "};", 12091 Style); 12092 } 12093 12094 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12095 // Elaborate type variable declarations. 12096 verifyFormat("struct foo a = {bar};\nint n;"); 12097 verifyFormat("class foo a = {bar};\nint n;"); 12098 verifyFormat("union foo a = {bar};\nint n;"); 12099 12100 // Elaborate types inside function definitions. 12101 verifyFormat("struct foo f() {}\nint n;"); 12102 verifyFormat("class foo f() {}\nint n;"); 12103 verifyFormat("union foo f() {}\nint n;"); 12104 12105 // Templates. 12106 verifyFormat("template <class X> void f() {}\nint n;"); 12107 verifyFormat("template <struct X> void f() {}\nint n;"); 12108 verifyFormat("template <union X> void f() {}\nint n;"); 12109 12110 // Actual definitions... 12111 verifyFormat("struct {\n} n;"); 12112 verifyFormat( 12113 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12114 verifyFormat("union Z {\n int n;\n} x;"); 12115 verifyFormat("class MACRO Z {\n} n;"); 12116 verifyFormat("class MACRO(X) Z {\n} n;"); 12117 verifyFormat("class __attribute__(X) Z {\n} n;"); 12118 verifyFormat("class __declspec(X) Z {\n} n;"); 12119 verifyFormat("class A##B##C {\n} n;"); 12120 verifyFormat("class alignas(16) Z {\n} n;"); 12121 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12122 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12123 12124 // Redefinition from nested context: 12125 verifyFormat("class A::B::C {\n} n;"); 12126 12127 // Template definitions. 12128 verifyFormat( 12129 "template <typename F>\n" 12130 "Matcher(const Matcher<F> &Other,\n" 12131 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12132 " !is_same<F, T>::value>::type * = 0)\n" 12133 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12134 12135 // FIXME: This is still incorrectly handled at the formatter side. 12136 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 12137 verifyFormat("int i = SomeFunction(a<b, a> b);"); 12138 12139 // FIXME: 12140 // This now gets parsed incorrectly as class definition. 12141 // verifyFormat("class A<int> f() {\n}\nint n;"); 12142 12143 // Elaborate types where incorrectly parsing the structural element would 12144 // break the indent. 12145 verifyFormat("if (true)\n" 12146 " class X x;\n" 12147 "else\n" 12148 " f();\n"); 12149 12150 // This is simply incomplete. Formatting is not important, but must not crash. 12151 verifyFormat("class A:"); 12152 } 12153 12154 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 12155 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 12156 format("#error Leave all white!!!!! space* alone!\n")); 12157 EXPECT_EQ( 12158 "#warning Leave all white!!!!! space* alone!\n", 12159 format("#warning Leave all white!!!!! space* alone!\n")); 12160 EXPECT_EQ("#error 1", format(" # error 1")); 12161 EXPECT_EQ("#warning 1", format(" # warning 1")); 12162 } 12163 12164 TEST_F(FormatTest, FormatHashIfExpressions) { 12165 verifyFormat("#if AAAA && BBBB"); 12166 verifyFormat("#if (AAAA && BBBB)"); 12167 verifyFormat("#elif (AAAA && BBBB)"); 12168 // FIXME: Come up with a better indentation for #elif. 12169 verifyFormat( 12170 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 12171 " defined(BBBBBBBB)\n" 12172 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 12173 " defined(BBBBBBBB)\n" 12174 "#endif", 12175 getLLVMStyleWithColumns(65)); 12176 } 12177 12178 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 12179 FormatStyle AllowsMergedIf = getGoogleStyle(); 12180 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 12181 FormatStyle::SIS_WithoutElse; 12182 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 12183 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 12184 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 12185 EXPECT_EQ("if (true) return 42;", 12186 format("if (true)\nreturn 42;", AllowsMergedIf)); 12187 FormatStyle ShortMergedIf = AllowsMergedIf; 12188 ShortMergedIf.ColumnLimit = 25; 12189 verifyFormat("#define A \\\n" 12190 " if (true) return 42;", 12191 ShortMergedIf); 12192 verifyFormat("#define A \\\n" 12193 " f(); \\\n" 12194 " if (true)\n" 12195 "#define B", 12196 ShortMergedIf); 12197 verifyFormat("#define A \\\n" 12198 " f(); \\\n" 12199 " if (true)\n" 12200 "g();", 12201 ShortMergedIf); 12202 verifyFormat("{\n" 12203 "#ifdef A\n" 12204 " // Comment\n" 12205 " if (true) continue;\n" 12206 "#endif\n" 12207 " // Comment\n" 12208 " if (true) continue;\n" 12209 "}", 12210 ShortMergedIf); 12211 ShortMergedIf.ColumnLimit = 33; 12212 verifyFormat("#define A \\\n" 12213 " if constexpr (true) return 42;", 12214 ShortMergedIf); 12215 verifyFormat("#define A \\\n" 12216 " if CONSTEXPR (true) return 42;", 12217 ShortMergedIf); 12218 ShortMergedIf.ColumnLimit = 29; 12219 verifyFormat("#define A \\\n" 12220 " if (aaaaaaaaaa) return 1; \\\n" 12221 " return 2;", 12222 ShortMergedIf); 12223 ShortMergedIf.ColumnLimit = 28; 12224 verifyFormat("#define A \\\n" 12225 " if (aaaaaaaaaa) \\\n" 12226 " return 1; \\\n" 12227 " return 2;", 12228 ShortMergedIf); 12229 verifyFormat("#define A \\\n" 12230 " if constexpr (aaaaaaa) \\\n" 12231 " return 1; \\\n" 12232 " return 2;", 12233 ShortMergedIf); 12234 verifyFormat("#define A \\\n" 12235 " if CONSTEXPR (aaaaaaa) \\\n" 12236 " return 1; \\\n" 12237 " return 2;", 12238 ShortMergedIf); 12239 } 12240 12241 TEST_F(FormatTest, FormatStarDependingOnContext) { 12242 verifyFormat("void f(int *a);"); 12243 verifyFormat("void f() { f(fint * b); }"); 12244 verifyFormat("class A {\n void f(int *a);\n};"); 12245 verifyFormat("class A {\n int *a;\n};"); 12246 verifyFormat("namespace a {\n" 12247 "namespace b {\n" 12248 "class A {\n" 12249 " void f() {}\n" 12250 " int *a;\n" 12251 "};\n" 12252 "} // namespace b\n" 12253 "} // namespace a"); 12254 } 12255 12256 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 12257 verifyFormat("while"); 12258 verifyFormat("operator"); 12259 } 12260 12261 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 12262 // This code would be painfully slow to format if we didn't skip it. 12263 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 12264 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12265 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12266 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12267 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12268 "A(1, 1)\n" 12269 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 12270 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12271 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12272 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12273 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12274 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12275 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12276 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12277 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12278 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 12279 // Deeply nested part is untouched, rest is formatted. 12280 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 12281 format(std::string("int i;\n") + Code + "int j;\n", 12282 getLLVMStyle(), SC_ExpectIncomplete)); 12283 } 12284 12285 //===----------------------------------------------------------------------===// 12286 // Objective-C tests. 12287 //===----------------------------------------------------------------------===// 12288 12289 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 12290 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 12291 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 12292 format("-(NSUInteger)indexOfObject:(id)anObject;")); 12293 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 12294 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 12295 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 12296 format("-(NSInteger)Method3:(id)anObject;")); 12297 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 12298 format("-(NSInteger)Method4:(id)anObject;")); 12299 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 12300 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 12301 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 12302 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 12303 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12304 "forAllCells:(BOOL)flag;", 12305 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12306 "forAllCells:(BOOL)flag;")); 12307 12308 // Very long objectiveC method declaration. 12309 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 12310 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 12311 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 12312 " inRange:(NSRange)range\n" 12313 " outRange:(NSRange)out_range\n" 12314 " outRange1:(NSRange)out_range1\n" 12315 " outRange2:(NSRange)out_range2\n" 12316 " outRange3:(NSRange)out_range3\n" 12317 " outRange4:(NSRange)out_range4\n" 12318 " outRange5:(NSRange)out_range5\n" 12319 " outRange6:(NSRange)out_range6\n" 12320 " outRange7:(NSRange)out_range7\n" 12321 " outRange8:(NSRange)out_range8\n" 12322 " outRange9:(NSRange)out_range9;"); 12323 12324 // When the function name has to be wrapped. 12325 FormatStyle Style = getLLVMStyle(); 12326 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 12327 // and always indents instead. 12328 Style.IndentWrappedFunctionNames = false; 12329 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12330 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 12331 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 12332 "}", 12333 Style); 12334 Style.IndentWrappedFunctionNames = true; 12335 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12336 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 12337 " anotherName:(NSString)dddddddddddddd {\n" 12338 "}", 12339 Style); 12340 12341 verifyFormat("- (int)sum:(vector<int>)numbers;"); 12342 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 12343 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 12344 // protocol lists (but not for template classes): 12345 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 12346 12347 verifyFormat("- (int (*)())foo:(int (*)())f;"); 12348 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 12349 12350 // If there's no return type (very rare in practice!), LLVM and Google style 12351 // agree. 12352 verifyFormat("- foo;"); 12353 verifyFormat("- foo:(int)f;"); 12354 verifyGoogleFormat("- foo:(int)foo;"); 12355 } 12356 12357 TEST_F(FormatTest, BreaksStringLiterals) { 12358 EXPECT_EQ("\"some text \"\n" 12359 "\"other\";", 12360 format("\"some text other\";", getLLVMStyleWithColumns(12))); 12361 EXPECT_EQ("\"some text \"\n" 12362 "\"other\";", 12363 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 12364 EXPECT_EQ( 12365 "#define A \\\n" 12366 " \"some \" \\\n" 12367 " \"text \" \\\n" 12368 " \"other\";", 12369 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 12370 EXPECT_EQ( 12371 "#define A \\\n" 12372 " \"so \" \\\n" 12373 " \"text \" \\\n" 12374 " \"other\";", 12375 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 12376 12377 EXPECT_EQ("\"some text\"", 12378 format("\"some text\"", getLLVMStyleWithColumns(1))); 12379 EXPECT_EQ("\"some text\"", 12380 format("\"some text\"", getLLVMStyleWithColumns(11))); 12381 EXPECT_EQ("\"some \"\n" 12382 "\"text\"", 12383 format("\"some text\"", getLLVMStyleWithColumns(10))); 12384 EXPECT_EQ("\"some \"\n" 12385 "\"text\"", 12386 format("\"some text\"", getLLVMStyleWithColumns(7))); 12387 EXPECT_EQ("\"some\"\n" 12388 "\" tex\"\n" 12389 "\"t\"", 12390 format("\"some text\"", getLLVMStyleWithColumns(6))); 12391 EXPECT_EQ("\"some\"\n" 12392 "\" tex\"\n" 12393 "\" and\"", 12394 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 12395 EXPECT_EQ("\"some\"\n" 12396 "\"/tex\"\n" 12397 "\"/and\"", 12398 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 12399 12400 EXPECT_EQ("variable =\n" 12401 " \"long string \"\n" 12402 " \"literal\";", 12403 format("variable = \"long string literal\";", 12404 getLLVMStyleWithColumns(20))); 12405 12406 EXPECT_EQ("variable = f(\n" 12407 " \"long string \"\n" 12408 " \"literal\",\n" 12409 " short,\n" 12410 " loooooooooooooooooooong);", 12411 format("variable = f(\"long string literal\", short, " 12412 "loooooooooooooooooooong);", 12413 getLLVMStyleWithColumns(20))); 12414 12415 EXPECT_EQ( 12416 "f(g(\"long string \"\n" 12417 " \"literal\"),\n" 12418 " b);", 12419 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 12420 EXPECT_EQ("f(g(\"long string \"\n" 12421 " \"literal\",\n" 12422 " a),\n" 12423 " b);", 12424 format("f(g(\"long string literal\", a), b);", 12425 getLLVMStyleWithColumns(20))); 12426 EXPECT_EQ( 12427 "f(\"one two\".split(\n" 12428 " variable));", 12429 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 12430 EXPECT_EQ("f(\"one two three four five six \"\n" 12431 " \"seven\".split(\n" 12432 " really_looooong_variable));", 12433 format("f(\"one two three four five six seven\"." 12434 "split(really_looooong_variable));", 12435 getLLVMStyleWithColumns(33))); 12436 12437 EXPECT_EQ("f(\"some \"\n" 12438 " \"text\",\n" 12439 " other);", 12440 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 12441 12442 // Only break as a last resort. 12443 verifyFormat( 12444 "aaaaaaaaaaaaaaaaaaaa(\n" 12445 " aaaaaaaaaaaaaaaaaaaa,\n" 12446 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 12447 12448 EXPECT_EQ("\"splitmea\"\n" 12449 "\"trandomp\"\n" 12450 "\"oint\"", 12451 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 12452 12453 EXPECT_EQ("\"split/\"\n" 12454 "\"pathat/\"\n" 12455 "\"slashes\"", 12456 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12457 12458 EXPECT_EQ("\"split/\"\n" 12459 "\"pathat/\"\n" 12460 "\"slashes\"", 12461 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12462 EXPECT_EQ("\"split at \"\n" 12463 "\"spaces/at/\"\n" 12464 "\"slashes.at.any$\"\n" 12465 "\"non-alphanumeric%\"\n" 12466 "\"1111111111characte\"\n" 12467 "\"rs\"", 12468 format("\"split at " 12469 "spaces/at/" 12470 "slashes.at." 12471 "any$non-" 12472 "alphanumeric%" 12473 "1111111111characte" 12474 "rs\"", 12475 getLLVMStyleWithColumns(20))); 12476 12477 // Verify that splitting the strings understands 12478 // Style::AlwaysBreakBeforeMultilineStrings. 12479 EXPECT_EQ("aaaaaaaaaaaa(\n" 12480 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 12481 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 12482 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 12483 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12484 "aaaaaaaaaaaaaaaaaaaaaa\");", 12485 getGoogleStyle())); 12486 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12487 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 12488 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 12489 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12490 "aaaaaaaaaaaaaaaaaaaaaa\";", 12491 getGoogleStyle())); 12492 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12493 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12494 format("llvm::outs() << " 12495 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 12496 "aaaaaaaaaaaaaaaaaaa\";")); 12497 EXPECT_EQ("ffff(\n" 12498 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12499 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12500 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 12501 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12502 getGoogleStyle())); 12503 12504 FormatStyle Style = getLLVMStyleWithColumns(12); 12505 Style.BreakStringLiterals = false; 12506 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 12507 12508 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 12509 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 12510 EXPECT_EQ("#define A \\\n" 12511 " \"some \" \\\n" 12512 " \"text \" \\\n" 12513 " \"other\";", 12514 format("#define A \"some text other\";", AlignLeft)); 12515 } 12516 12517 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 12518 EXPECT_EQ("C a = \"some more \"\n" 12519 " \"text\";", 12520 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 12521 } 12522 12523 TEST_F(FormatTest, FullyRemoveEmptyLines) { 12524 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 12525 NoEmptyLines.MaxEmptyLinesToKeep = 0; 12526 EXPECT_EQ("int i = a(b());", 12527 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 12528 } 12529 12530 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 12531 EXPECT_EQ( 12532 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12533 "(\n" 12534 " \"x\t\");", 12535 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12536 "aaaaaaa(" 12537 "\"x\t\");")); 12538 } 12539 12540 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 12541 EXPECT_EQ( 12542 "u8\"utf8 string \"\n" 12543 "u8\"literal\";", 12544 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 12545 EXPECT_EQ( 12546 "u\"utf16 string \"\n" 12547 "u\"literal\";", 12548 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 12549 EXPECT_EQ( 12550 "U\"utf32 string \"\n" 12551 "U\"literal\";", 12552 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 12553 EXPECT_EQ("L\"wide string \"\n" 12554 "L\"literal\";", 12555 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 12556 EXPECT_EQ("@\"NSString \"\n" 12557 "@\"literal\";", 12558 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 12559 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 12560 12561 // This input makes clang-format try to split the incomplete unicode escape 12562 // sequence, which used to lead to a crasher. 12563 verifyNoCrash( 12564 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 12565 getLLVMStyleWithColumns(60)); 12566 } 12567 12568 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 12569 FormatStyle Style = getGoogleStyleWithColumns(15); 12570 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 12571 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 12572 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 12573 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 12574 EXPECT_EQ("u8R\"x(raw literal)x\";", 12575 format("u8R\"x(raw literal)x\";", Style)); 12576 } 12577 12578 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 12579 FormatStyle Style = getLLVMStyleWithColumns(20); 12580 EXPECT_EQ( 12581 "_T(\"aaaaaaaaaaaaaa\")\n" 12582 "_T(\"aaaaaaaaaaaaaa\")\n" 12583 "_T(\"aaaaaaaaaaaa\")", 12584 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 12585 EXPECT_EQ("f(x,\n" 12586 " _T(\"aaaaaaaaaaaa\")\n" 12587 " _T(\"aaa\"),\n" 12588 " z);", 12589 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 12590 12591 // FIXME: Handle embedded spaces in one iteration. 12592 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 12593 // "_T(\"aaaaaaaaaaaaa\")\n" 12594 // "_T(\"aaaaaaaaaaaaa\")\n" 12595 // "_T(\"a\")", 12596 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12597 // getLLVMStyleWithColumns(20))); 12598 EXPECT_EQ( 12599 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12600 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 12601 EXPECT_EQ("f(\n" 12602 "#if !TEST\n" 12603 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12604 "#endif\n" 12605 ");", 12606 format("f(\n" 12607 "#if !TEST\n" 12608 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12609 "#endif\n" 12610 ");")); 12611 EXPECT_EQ("f(\n" 12612 "\n" 12613 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 12614 format("f(\n" 12615 "\n" 12616 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 12617 } 12618 12619 TEST_F(FormatTest, BreaksStringLiteralOperands) { 12620 // In a function call with two operands, the second can be broken with no line 12621 // break before it. 12622 EXPECT_EQ( 12623 "func(a, \"long long \"\n" 12624 " \"long long\");", 12625 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 12626 // In a function call with three operands, the second must be broken with a 12627 // line break before it. 12628 EXPECT_EQ("func(a,\n" 12629 " \"long long long \"\n" 12630 " \"long\",\n" 12631 " c);", 12632 format("func(a, \"long long long long\", c);", 12633 getLLVMStyleWithColumns(24))); 12634 // In a function call with three operands, the third must be broken with a 12635 // line break before it. 12636 EXPECT_EQ("func(a, b,\n" 12637 " \"long long long \"\n" 12638 " \"long\");", 12639 format("func(a, b, \"long long long long\");", 12640 getLLVMStyleWithColumns(24))); 12641 // In a function call with three operands, both the second and the third must 12642 // be broken with a line break before them. 12643 EXPECT_EQ("func(a,\n" 12644 " \"long long long \"\n" 12645 " \"long\",\n" 12646 " \"long long long \"\n" 12647 " \"long\");", 12648 format("func(a, \"long long long long\", \"long long long long\");", 12649 getLLVMStyleWithColumns(24))); 12650 // In a chain of << with two operands, the second can be broken with no line 12651 // break before it. 12652 EXPECT_EQ("a << \"line line \"\n" 12653 " \"line\";", 12654 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 12655 // In a chain of << with three operands, the second can be broken with no line 12656 // break before it. 12657 EXPECT_EQ( 12658 "abcde << \"line \"\n" 12659 " \"line line\"\n" 12660 " << c;", 12661 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 12662 // In a chain of << with three operands, the third must be broken with a line 12663 // break before it. 12664 EXPECT_EQ( 12665 "a << b\n" 12666 " << \"line line \"\n" 12667 " \"line\";", 12668 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 12669 // In a chain of << with three operands, the second can be broken with no line 12670 // break before it and the third must be broken with a line break before it. 12671 EXPECT_EQ("abcd << \"line line \"\n" 12672 " \"line\"\n" 12673 " << \"line line \"\n" 12674 " \"line\";", 12675 format("abcd << \"line line line\" << \"line line line\";", 12676 getLLVMStyleWithColumns(20))); 12677 // In a chain of binary operators with two operands, the second can be broken 12678 // with no line break before it. 12679 EXPECT_EQ( 12680 "abcd + \"line line \"\n" 12681 " \"line line\";", 12682 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 12683 // In a chain of binary operators with three operands, the second must be 12684 // broken with a line break before it. 12685 EXPECT_EQ("abcd +\n" 12686 " \"line line \"\n" 12687 " \"line line\" +\n" 12688 " e;", 12689 format("abcd + \"line line line line\" + e;", 12690 getLLVMStyleWithColumns(20))); 12691 // In a function call with two operands, with AlignAfterOpenBracket enabled, 12692 // the first must be broken with a line break before it. 12693 FormatStyle Style = getLLVMStyleWithColumns(25); 12694 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12695 EXPECT_EQ("someFunction(\n" 12696 " \"long long long \"\n" 12697 " \"long\",\n" 12698 " a);", 12699 format("someFunction(\"long long long long\", a);", Style)); 12700 } 12701 12702 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 12703 EXPECT_EQ( 12704 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12705 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12706 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12707 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12708 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 12710 } 12711 12712 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 12713 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 12714 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 12715 EXPECT_EQ("fffffffffff(g(R\"x(\n" 12716 "multiline raw string literal xxxxxxxxxxxxxx\n" 12717 ")x\",\n" 12718 " a),\n" 12719 " b);", 12720 format("fffffffffff(g(R\"x(\n" 12721 "multiline raw string literal xxxxxxxxxxxxxx\n" 12722 ")x\", a), b);", 12723 getGoogleStyleWithColumns(20))); 12724 EXPECT_EQ("fffffffffff(\n" 12725 " g(R\"x(qqq\n" 12726 "multiline raw string literal xxxxxxxxxxxxxx\n" 12727 ")x\",\n" 12728 " a),\n" 12729 " b);", 12730 format("fffffffffff(g(R\"x(qqq\n" 12731 "multiline raw string literal xxxxxxxxxxxxxx\n" 12732 ")x\", a), b);", 12733 getGoogleStyleWithColumns(20))); 12734 12735 EXPECT_EQ("fffffffffff(R\"x(\n" 12736 "multiline raw string literal xxxxxxxxxxxxxx\n" 12737 ")x\");", 12738 format("fffffffffff(R\"x(\n" 12739 "multiline raw string literal xxxxxxxxxxxxxx\n" 12740 ")x\");", 12741 getGoogleStyleWithColumns(20))); 12742 EXPECT_EQ("fffffffffff(R\"x(\n" 12743 "multiline raw string literal xxxxxxxxxxxxxx\n" 12744 ")x\" + bbbbbb);", 12745 format("fffffffffff(R\"x(\n" 12746 "multiline raw string literal xxxxxxxxxxxxxx\n" 12747 ")x\" + bbbbbb);", 12748 getGoogleStyleWithColumns(20))); 12749 EXPECT_EQ("fffffffffff(\n" 12750 " R\"x(\n" 12751 "multiline raw string literal xxxxxxxxxxxxxx\n" 12752 ")x\" +\n" 12753 " bbbbbb);", 12754 format("fffffffffff(\n" 12755 " R\"x(\n" 12756 "multiline raw string literal xxxxxxxxxxxxxx\n" 12757 ")x\" + bbbbbb);", 12758 getGoogleStyleWithColumns(20))); 12759 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 12760 format("fffffffffff(\n" 12761 " R\"(single line raw string)\" + bbbbbb);")); 12762 } 12763 12764 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 12765 verifyFormat("string a = \"unterminated;"); 12766 EXPECT_EQ("function(\"unterminated,\n" 12767 " OtherParameter);", 12768 format("function( \"unterminated,\n" 12769 " OtherParameter);")); 12770 } 12771 12772 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 12773 FormatStyle Style = getLLVMStyle(); 12774 Style.Standard = FormatStyle::LS_Cpp03; 12775 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 12776 format("#define x(_a) printf(\"foo\"_a);", Style)); 12777 } 12778 12779 TEST_F(FormatTest, CppLexVersion) { 12780 FormatStyle Style = getLLVMStyle(); 12781 // Formatting of x * y differs if x is a type. 12782 verifyFormat("void foo() { MACRO(a * b); }", Style); 12783 verifyFormat("void foo() { MACRO(int *b); }", Style); 12784 12785 // LLVM style uses latest lexer. 12786 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 12787 Style.Standard = FormatStyle::LS_Cpp17; 12788 // But in c++17, char8_t isn't a keyword. 12789 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 12790 } 12791 12792 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 12793 12794 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 12795 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 12796 " \"ddeeefff\");", 12797 format("someFunction(\"aaabbbcccdddeeefff\");", 12798 getLLVMStyleWithColumns(25))); 12799 EXPECT_EQ("someFunction1234567890(\n" 12800 " \"aaabbbcccdddeeefff\");", 12801 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12802 getLLVMStyleWithColumns(26))); 12803 EXPECT_EQ("someFunction1234567890(\n" 12804 " \"aaabbbcccdddeeeff\"\n" 12805 " \"f\");", 12806 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12807 getLLVMStyleWithColumns(25))); 12808 EXPECT_EQ("someFunction1234567890(\n" 12809 " \"aaabbbcccdddeeeff\"\n" 12810 " \"f\");", 12811 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12812 getLLVMStyleWithColumns(24))); 12813 EXPECT_EQ("someFunction(\n" 12814 " \"aaabbbcc ddde \"\n" 12815 " \"efff\");", 12816 format("someFunction(\"aaabbbcc ddde efff\");", 12817 getLLVMStyleWithColumns(25))); 12818 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 12819 " \"ddeeefff\");", 12820 format("someFunction(\"aaabbbccc ddeeefff\");", 12821 getLLVMStyleWithColumns(25))); 12822 EXPECT_EQ("someFunction1234567890(\n" 12823 " \"aaabb \"\n" 12824 " \"cccdddeeefff\");", 12825 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 12826 getLLVMStyleWithColumns(25))); 12827 EXPECT_EQ("#define A \\\n" 12828 " string s = \\\n" 12829 " \"123456789\" \\\n" 12830 " \"0\"; \\\n" 12831 " int i;", 12832 format("#define A string s = \"1234567890\"; int i;", 12833 getLLVMStyleWithColumns(20))); 12834 EXPECT_EQ("someFunction(\n" 12835 " \"aaabbbcc \"\n" 12836 " \"dddeeefff\");", 12837 format("someFunction(\"aaabbbcc dddeeefff\");", 12838 getLLVMStyleWithColumns(25))); 12839 } 12840 12841 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 12842 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 12843 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 12844 EXPECT_EQ("\"test\"\n" 12845 "\"\\n\"", 12846 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 12847 EXPECT_EQ("\"tes\\\\\"\n" 12848 "\"n\"", 12849 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 12850 EXPECT_EQ("\"\\\\\\\\\"\n" 12851 "\"\\n\"", 12852 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 12853 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 12854 EXPECT_EQ("\"\\uff01\"\n" 12855 "\"test\"", 12856 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 12857 EXPECT_EQ("\"\\Uff01ff02\"", 12858 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 12859 EXPECT_EQ("\"\\x000000000001\"\n" 12860 "\"next\"", 12861 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 12862 EXPECT_EQ("\"\\x000000000001next\"", 12863 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 12864 EXPECT_EQ("\"\\x000000000001\"", 12865 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 12866 EXPECT_EQ("\"test\"\n" 12867 "\"\\000000\"\n" 12868 "\"000001\"", 12869 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 12870 EXPECT_EQ("\"test\\000\"\n" 12871 "\"00000000\"\n" 12872 "\"1\"", 12873 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 12874 } 12875 12876 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 12877 verifyFormat("void f() {\n" 12878 " return g() {}\n" 12879 " void h() {}"); 12880 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 12881 "g();\n" 12882 "}"); 12883 } 12884 12885 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 12886 verifyFormat( 12887 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 12888 } 12889 12890 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 12891 verifyFormat("class X {\n" 12892 " void f() {\n" 12893 " }\n" 12894 "};", 12895 getLLVMStyleWithColumns(12)); 12896 } 12897 12898 TEST_F(FormatTest, ConfigurableIndentWidth) { 12899 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 12900 EightIndent.IndentWidth = 8; 12901 EightIndent.ContinuationIndentWidth = 8; 12902 verifyFormat("void f() {\n" 12903 " someFunction();\n" 12904 " if (true) {\n" 12905 " f();\n" 12906 " }\n" 12907 "}", 12908 EightIndent); 12909 verifyFormat("class X {\n" 12910 " void f() {\n" 12911 " }\n" 12912 "};", 12913 EightIndent); 12914 verifyFormat("int x[] = {\n" 12915 " call(),\n" 12916 " call()};", 12917 EightIndent); 12918 } 12919 12920 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 12921 verifyFormat("double\n" 12922 "f();", 12923 getLLVMStyleWithColumns(8)); 12924 } 12925 12926 TEST_F(FormatTest, ConfigurableUseOfTab) { 12927 FormatStyle Tab = getLLVMStyleWithColumns(42); 12928 Tab.IndentWidth = 8; 12929 Tab.UseTab = FormatStyle::UT_Always; 12930 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 12931 12932 EXPECT_EQ("if (aaaaaaaa && // q\n" 12933 " bb)\t\t// w\n" 12934 "\t;", 12935 format("if (aaaaaaaa &&// q\n" 12936 "bb)// w\n" 12937 ";", 12938 Tab)); 12939 EXPECT_EQ("if (aaa && bbb) // w\n" 12940 "\t;", 12941 format("if(aaa&&bbb)// w\n" 12942 ";", 12943 Tab)); 12944 12945 verifyFormat("class X {\n" 12946 "\tvoid f() {\n" 12947 "\t\tsomeFunction(parameter1,\n" 12948 "\t\t\t parameter2);\n" 12949 "\t}\n" 12950 "};", 12951 Tab); 12952 verifyFormat("#define A \\\n" 12953 "\tvoid f() { \\\n" 12954 "\t\tsomeFunction( \\\n" 12955 "\t\t parameter1, \\\n" 12956 "\t\t parameter2); \\\n" 12957 "\t}", 12958 Tab); 12959 verifyFormat("int a;\t // x\n" 12960 "int bbbbbbbb; // x\n", 12961 Tab); 12962 12963 Tab.TabWidth = 4; 12964 Tab.IndentWidth = 8; 12965 verifyFormat("class TabWidth4Indent8 {\n" 12966 "\t\tvoid f() {\n" 12967 "\t\t\t\tsomeFunction(parameter1,\n" 12968 "\t\t\t\t\t\t\t parameter2);\n" 12969 "\t\t}\n" 12970 "};", 12971 Tab); 12972 12973 Tab.TabWidth = 4; 12974 Tab.IndentWidth = 4; 12975 verifyFormat("class TabWidth4Indent4 {\n" 12976 "\tvoid f() {\n" 12977 "\t\tsomeFunction(parameter1,\n" 12978 "\t\t\t\t\t parameter2);\n" 12979 "\t}\n" 12980 "};", 12981 Tab); 12982 12983 Tab.TabWidth = 8; 12984 Tab.IndentWidth = 4; 12985 verifyFormat("class TabWidth8Indent4 {\n" 12986 " void f() {\n" 12987 "\tsomeFunction(parameter1,\n" 12988 "\t\t parameter2);\n" 12989 " }\n" 12990 "};", 12991 Tab); 12992 12993 Tab.TabWidth = 8; 12994 Tab.IndentWidth = 8; 12995 EXPECT_EQ("/*\n" 12996 "\t a\t\tcomment\n" 12997 "\t in multiple lines\n" 12998 " */", 12999 format(" /*\t \t \n" 13000 " \t \t a\t\tcomment\t \t\n" 13001 " \t \t in multiple lines\t\n" 13002 " \t */", 13003 Tab)); 13004 13005 Tab.UseTab = FormatStyle::UT_ForIndentation; 13006 verifyFormat("{\n" 13007 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13008 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13009 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13010 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13011 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13012 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13013 "};", 13014 Tab); 13015 verifyFormat("enum AA {\n" 13016 "\ta1, // Force multiple lines\n" 13017 "\ta2,\n" 13018 "\ta3\n" 13019 "};", 13020 Tab); 13021 EXPECT_EQ("if (aaaaaaaa && // q\n" 13022 " bb) // w\n" 13023 "\t;", 13024 format("if (aaaaaaaa &&// q\n" 13025 "bb)// w\n" 13026 ";", 13027 Tab)); 13028 verifyFormat("class X {\n" 13029 "\tvoid f() {\n" 13030 "\t\tsomeFunction(parameter1,\n" 13031 "\t\t parameter2);\n" 13032 "\t}\n" 13033 "};", 13034 Tab); 13035 verifyFormat("{\n" 13036 "\tQ(\n" 13037 "\t {\n" 13038 "\t\t int a;\n" 13039 "\t\t someFunction(aaaaaaaa,\n" 13040 "\t\t bbbbbbb);\n" 13041 "\t },\n" 13042 "\t p);\n" 13043 "}", 13044 Tab); 13045 EXPECT_EQ("{\n" 13046 "\t/* aaaa\n" 13047 "\t bbbb */\n" 13048 "}", 13049 format("{\n" 13050 "/* aaaa\n" 13051 " bbbb */\n" 13052 "}", 13053 Tab)); 13054 EXPECT_EQ("{\n" 13055 "\t/*\n" 13056 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13057 "\t bbbbbbbbbbbbb\n" 13058 "\t*/\n" 13059 "}", 13060 format("{\n" 13061 "/*\n" 13062 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13063 "*/\n" 13064 "}", 13065 Tab)); 13066 EXPECT_EQ("{\n" 13067 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13068 "\t// bbbbbbbbbbbbb\n" 13069 "}", 13070 format("{\n" 13071 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13072 "}", 13073 Tab)); 13074 EXPECT_EQ("{\n" 13075 "\t/*\n" 13076 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13077 "\t bbbbbbbbbbbbb\n" 13078 "\t*/\n" 13079 "}", 13080 format("{\n" 13081 "\t/*\n" 13082 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13083 "\t*/\n" 13084 "}", 13085 Tab)); 13086 EXPECT_EQ("{\n" 13087 "\t/*\n" 13088 "\n" 13089 "\t*/\n" 13090 "}", 13091 format("{\n" 13092 "\t/*\n" 13093 "\n" 13094 "\t*/\n" 13095 "}", 13096 Tab)); 13097 EXPECT_EQ("{\n" 13098 "\t/*\n" 13099 " asdf\n" 13100 "\t*/\n" 13101 "}", 13102 format("{\n" 13103 "\t/*\n" 13104 " asdf\n" 13105 "\t*/\n" 13106 "}", 13107 Tab)); 13108 13109 Tab.UseTab = FormatStyle::UT_Never; 13110 EXPECT_EQ("/*\n" 13111 " a\t\tcomment\n" 13112 " in multiple lines\n" 13113 " */", 13114 format(" /*\t \t \n" 13115 " \t \t a\t\tcomment\t \t\n" 13116 " \t \t in multiple lines\t\n" 13117 " \t */", 13118 Tab)); 13119 EXPECT_EQ("/* some\n" 13120 " comment */", 13121 format(" \t \t /* some\n" 13122 " \t \t comment */", 13123 Tab)); 13124 EXPECT_EQ("int a; /* some\n" 13125 " comment */", 13126 format(" \t \t int a; /* some\n" 13127 " \t \t comment */", 13128 Tab)); 13129 13130 EXPECT_EQ("int a; /* some\n" 13131 "comment */", 13132 format(" \t \t int\ta; /* some\n" 13133 " \t \t comment */", 13134 Tab)); 13135 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13136 " comment */", 13137 format(" \t \t f(\"\t\t\"); /* some\n" 13138 " \t \t comment */", 13139 Tab)); 13140 EXPECT_EQ("{\n" 13141 " /*\n" 13142 " * Comment\n" 13143 " */\n" 13144 " int i;\n" 13145 "}", 13146 format("{\n" 13147 "\t/*\n" 13148 "\t * Comment\n" 13149 "\t */\n" 13150 "\t int i;\n" 13151 "}", 13152 Tab)); 13153 13154 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13155 Tab.TabWidth = 8; 13156 Tab.IndentWidth = 8; 13157 EXPECT_EQ("if (aaaaaaaa && // q\n" 13158 " bb) // w\n" 13159 "\t;", 13160 format("if (aaaaaaaa &&// q\n" 13161 "bb)// w\n" 13162 ";", 13163 Tab)); 13164 EXPECT_EQ("if (aaa && bbb) // w\n" 13165 "\t;", 13166 format("if(aaa&&bbb)// w\n" 13167 ";", 13168 Tab)); 13169 verifyFormat("class X {\n" 13170 "\tvoid f() {\n" 13171 "\t\tsomeFunction(parameter1,\n" 13172 "\t\t\t parameter2);\n" 13173 "\t}\n" 13174 "};", 13175 Tab); 13176 verifyFormat("#define A \\\n" 13177 "\tvoid f() { \\\n" 13178 "\t\tsomeFunction( \\\n" 13179 "\t\t parameter1, \\\n" 13180 "\t\t parameter2); \\\n" 13181 "\t}", 13182 Tab); 13183 Tab.TabWidth = 4; 13184 Tab.IndentWidth = 8; 13185 verifyFormat("class TabWidth4Indent8 {\n" 13186 "\t\tvoid f() {\n" 13187 "\t\t\t\tsomeFunction(parameter1,\n" 13188 "\t\t\t\t\t\t\t parameter2);\n" 13189 "\t\t}\n" 13190 "};", 13191 Tab); 13192 Tab.TabWidth = 4; 13193 Tab.IndentWidth = 4; 13194 verifyFormat("class TabWidth4Indent4 {\n" 13195 "\tvoid f() {\n" 13196 "\t\tsomeFunction(parameter1,\n" 13197 "\t\t\t\t\t parameter2);\n" 13198 "\t}\n" 13199 "};", 13200 Tab); 13201 Tab.TabWidth = 8; 13202 Tab.IndentWidth = 4; 13203 verifyFormat("class TabWidth8Indent4 {\n" 13204 " void f() {\n" 13205 "\tsomeFunction(parameter1,\n" 13206 "\t\t parameter2);\n" 13207 " }\n" 13208 "};", 13209 Tab); 13210 Tab.TabWidth = 8; 13211 Tab.IndentWidth = 8; 13212 EXPECT_EQ("/*\n" 13213 "\t a\t\tcomment\n" 13214 "\t in multiple lines\n" 13215 " */", 13216 format(" /*\t \t \n" 13217 " \t \t a\t\tcomment\t \t\n" 13218 " \t \t in multiple lines\t\n" 13219 " \t */", 13220 Tab)); 13221 verifyFormat("{\n" 13222 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13223 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13224 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13225 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13226 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13227 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13228 "};", 13229 Tab); 13230 verifyFormat("enum AA {\n" 13231 "\ta1, // Force multiple lines\n" 13232 "\ta2,\n" 13233 "\ta3\n" 13234 "};", 13235 Tab); 13236 EXPECT_EQ("if (aaaaaaaa && // q\n" 13237 " bb) // w\n" 13238 "\t;", 13239 format("if (aaaaaaaa &&// q\n" 13240 "bb)// w\n" 13241 ";", 13242 Tab)); 13243 verifyFormat("class X {\n" 13244 "\tvoid f() {\n" 13245 "\t\tsomeFunction(parameter1,\n" 13246 "\t\t\t parameter2);\n" 13247 "\t}\n" 13248 "};", 13249 Tab); 13250 verifyFormat("{\n" 13251 "\tQ(\n" 13252 "\t {\n" 13253 "\t\t int a;\n" 13254 "\t\t someFunction(aaaaaaaa,\n" 13255 "\t\t\t\t bbbbbbb);\n" 13256 "\t },\n" 13257 "\t p);\n" 13258 "}", 13259 Tab); 13260 EXPECT_EQ("{\n" 13261 "\t/* aaaa\n" 13262 "\t bbbb */\n" 13263 "}", 13264 format("{\n" 13265 "/* aaaa\n" 13266 " bbbb */\n" 13267 "}", 13268 Tab)); 13269 EXPECT_EQ("{\n" 13270 "\t/*\n" 13271 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13272 "\t bbbbbbbbbbbbb\n" 13273 "\t*/\n" 13274 "}", 13275 format("{\n" 13276 "/*\n" 13277 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13278 "*/\n" 13279 "}", 13280 Tab)); 13281 EXPECT_EQ("{\n" 13282 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13283 "\t// bbbbbbbbbbbbb\n" 13284 "}", 13285 format("{\n" 13286 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13287 "}", 13288 Tab)); 13289 EXPECT_EQ("{\n" 13290 "\t/*\n" 13291 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13292 "\t bbbbbbbbbbbbb\n" 13293 "\t*/\n" 13294 "}", 13295 format("{\n" 13296 "\t/*\n" 13297 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13298 "\t*/\n" 13299 "}", 13300 Tab)); 13301 EXPECT_EQ("{\n" 13302 "\t/*\n" 13303 "\n" 13304 "\t*/\n" 13305 "}", 13306 format("{\n" 13307 "\t/*\n" 13308 "\n" 13309 "\t*/\n" 13310 "}", 13311 Tab)); 13312 EXPECT_EQ("{\n" 13313 "\t/*\n" 13314 " asdf\n" 13315 "\t*/\n" 13316 "}", 13317 format("{\n" 13318 "\t/*\n" 13319 " asdf\n" 13320 "\t*/\n" 13321 "}", 13322 Tab)); 13323 EXPECT_EQ("/* some\n" 13324 " comment */", 13325 format(" \t \t /* some\n" 13326 " \t \t comment */", 13327 Tab)); 13328 EXPECT_EQ("int a; /* some\n" 13329 " comment */", 13330 format(" \t \t int a; /* some\n" 13331 " \t \t comment */", 13332 Tab)); 13333 EXPECT_EQ("int a; /* some\n" 13334 "comment */", 13335 format(" \t \t int\ta; /* some\n" 13336 " \t \t comment */", 13337 Tab)); 13338 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13339 " comment */", 13340 format(" \t \t f(\"\t\t\"); /* some\n" 13341 " \t \t comment */", 13342 Tab)); 13343 EXPECT_EQ("{\n" 13344 "\t/*\n" 13345 "\t * Comment\n" 13346 "\t */\n" 13347 "\tint i;\n" 13348 "}", 13349 format("{\n" 13350 "\t/*\n" 13351 "\t * Comment\n" 13352 "\t */\n" 13353 "\t int i;\n" 13354 "}", 13355 Tab)); 13356 Tab.TabWidth = 2; 13357 Tab.IndentWidth = 2; 13358 EXPECT_EQ("{\n" 13359 "\t/* aaaa\n" 13360 "\t\t bbbb */\n" 13361 "}", 13362 format("{\n" 13363 "/* aaaa\n" 13364 "\t bbbb */\n" 13365 "}", 13366 Tab)); 13367 EXPECT_EQ("{\n" 13368 "\t/*\n" 13369 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13370 "\t\tbbbbbbbbbbbbb\n" 13371 "\t*/\n" 13372 "}", 13373 format("{\n" 13374 "/*\n" 13375 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13376 "*/\n" 13377 "}", 13378 Tab)); 13379 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13380 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13381 Tab.TabWidth = 4; 13382 Tab.IndentWidth = 4; 13383 verifyFormat("class Assign {\n" 13384 "\tvoid f() {\n" 13385 "\t\tint x = 123;\n" 13386 "\t\tint random = 4;\n" 13387 "\t\tstd::string alphabet =\n" 13388 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13389 "\t}\n" 13390 "};", 13391 Tab); 13392 13393 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13394 Tab.TabWidth = 8; 13395 Tab.IndentWidth = 8; 13396 EXPECT_EQ("if (aaaaaaaa && // q\n" 13397 " bb) // w\n" 13398 "\t;", 13399 format("if (aaaaaaaa &&// q\n" 13400 "bb)// w\n" 13401 ";", 13402 Tab)); 13403 EXPECT_EQ("if (aaa && bbb) // w\n" 13404 "\t;", 13405 format("if(aaa&&bbb)// w\n" 13406 ";", 13407 Tab)); 13408 verifyFormat("class X {\n" 13409 "\tvoid f() {\n" 13410 "\t\tsomeFunction(parameter1,\n" 13411 "\t\t parameter2);\n" 13412 "\t}\n" 13413 "};", 13414 Tab); 13415 verifyFormat("#define A \\\n" 13416 "\tvoid f() { \\\n" 13417 "\t\tsomeFunction( \\\n" 13418 "\t\t parameter1, \\\n" 13419 "\t\t parameter2); \\\n" 13420 "\t}", 13421 Tab); 13422 Tab.TabWidth = 4; 13423 Tab.IndentWidth = 8; 13424 verifyFormat("class TabWidth4Indent8 {\n" 13425 "\t\tvoid f() {\n" 13426 "\t\t\t\tsomeFunction(parameter1,\n" 13427 "\t\t\t\t parameter2);\n" 13428 "\t\t}\n" 13429 "};", 13430 Tab); 13431 Tab.TabWidth = 4; 13432 Tab.IndentWidth = 4; 13433 verifyFormat("class TabWidth4Indent4 {\n" 13434 "\tvoid f() {\n" 13435 "\t\tsomeFunction(parameter1,\n" 13436 "\t\t parameter2);\n" 13437 "\t}\n" 13438 "};", 13439 Tab); 13440 Tab.TabWidth = 8; 13441 Tab.IndentWidth = 4; 13442 verifyFormat("class TabWidth8Indent4 {\n" 13443 " void f() {\n" 13444 "\tsomeFunction(parameter1,\n" 13445 "\t parameter2);\n" 13446 " }\n" 13447 "};", 13448 Tab); 13449 Tab.TabWidth = 8; 13450 Tab.IndentWidth = 8; 13451 EXPECT_EQ("/*\n" 13452 " a\t\tcomment\n" 13453 " in multiple lines\n" 13454 " */", 13455 format(" /*\t \t \n" 13456 " \t \t a\t\tcomment\t \t\n" 13457 " \t \t in multiple lines\t\n" 13458 " \t */", 13459 Tab)); 13460 verifyFormat("{\n" 13461 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13462 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13463 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13464 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13465 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13466 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13467 "};", 13468 Tab); 13469 verifyFormat("enum AA {\n" 13470 "\ta1, // Force multiple lines\n" 13471 "\ta2,\n" 13472 "\ta3\n" 13473 "};", 13474 Tab); 13475 EXPECT_EQ("if (aaaaaaaa && // q\n" 13476 " bb) // w\n" 13477 "\t;", 13478 format("if (aaaaaaaa &&// q\n" 13479 "bb)// w\n" 13480 ";", 13481 Tab)); 13482 verifyFormat("class X {\n" 13483 "\tvoid f() {\n" 13484 "\t\tsomeFunction(parameter1,\n" 13485 "\t\t parameter2);\n" 13486 "\t}\n" 13487 "};", 13488 Tab); 13489 verifyFormat("{\n" 13490 "\tQ(\n" 13491 "\t {\n" 13492 "\t\t int a;\n" 13493 "\t\t someFunction(aaaaaaaa,\n" 13494 "\t\t bbbbbbb);\n" 13495 "\t },\n" 13496 "\t p);\n" 13497 "}", 13498 Tab); 13499 EXPECT_EQ("{\n" 13500 "\t/* aaaa\n" 13501 "\t bbbb */\n" 13502 "}", 13503 format("{\n" 13504 "/* aaaa\n" 13505 " bbbb */\n" 13506 "}", 13507 Tab)); 13508 EXPECT_EQ("{\n" 13509 "\t/*\n" 13510 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13511 "\t bbbbbbbbbbbbb\n" 13512 "\t*/\n" 13513 "}", 13514 format("{\n" 13515 "/*\n" 13516 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13517 "*/\n" 13518 "}", 13519 Tab)); 13520 EXPECT_EQ("{\n" 13521 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13522 "\t// bbbbbbbbbbbbb\n" 13523 "}", 13524 format("{\n" 13525 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13526 "}", 13527 Tab)); 13528 EXPECT_EQ("{\n" 13529 "\t/*\n" 13530 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13531 "\t bbbbbbbbbbbbb\n" 13532 "\t*/\n" 13533 "}", 13534 format("{\n" 13535 "\t/*\n" 13536 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13537 "\t*/\n" 13538 "}", 13539 Tab)); 13540 EXPECT_EQ("{\n" 13541 "\t/*\n" 13542 "\n" 13543 "\t*/\n" 13544 "}", 13545 format("{\n" 13546 "\t/*\n" 13547 "\n" 13548 "\t*/\n" 13549 "}", 13550 Tab)); 13551 EXPECT_EQ("{\n" 13552 "\t/*\n" 13553 " asdf\n" 13554 "\t*/\n" 13555 "}", 13556 format("{\n" 13557 "\t/*\n" 13558 " asdf\n" 13559 "\t*/\n" 13560 "}", 13561 Tab)); 13562 EXPECT_EQ("/* some\n" 13563 " comment */", 13564 format(" \t \t /* some\n" 13565 " \t \t comment */", 13566 Tab)); 13567 EXPECT_EQ("int a; /* some\n" 13568 " comment */", 13569 format(" \t \t int a; /* some\n" 13570 " \t \t comment */", 13571 Tab)); 13572 EXPECT_EQ("int a; /* some\n" 13573 "comment */", 13574 format(" \t \t int\ta; /* some\n" 13575 " \t \t comment */", 13576 Tab)); 13577 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13578 " comment */", 13579 format(" \t \t f(\"\t\t\"); /* some\n" 13580 " \t \t comment */", 13581 Tab)); 13582 EXPECT_EQ("{\n" 13583 "\t/*\n" 13584 "\t * Comment\n" 13585 "\t */\n" 13586 "\tint i;\n" 13587 "}", 13588 format("{\n" 13589 "\t/*\n" 13590 "\t * Comment\n" 13591 "\t */\n" 13592 "\t int i;\n" 13593 "}", 13594 Tab)); 13595 Tab.TabWidth = 2; 13596 Tab.IndentWidth = 2; 13597 EXPECT_EQ("{\n" 13598 "\t/* aaaa\n" 13599 "\t bbbb */\n" 13600 "}", 13601 format("{\n" 13602 "/* aaaa\n" 13603 " bbbb */\n" 13604 "}", 13605 Tab)); 13606 EXPECT_EQ("{\n" 13607 "\t/*\n" 13608 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13609 "\t bbbbbbbbbbbbb\n" 13610 "\t*/\n" 13611 "}", 13612 format("{\n" 13613 "/*\n" 13614 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13615 "*/\n" 13616 "}", 13617 Tab)); 13618 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13619 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13620 Tab.TabWidth = 4; 13621 Tab.IndentWidth = 4; 13622 verifyFormat("class Assign {\n" 13623 "\tvoid f() {\n" 13624 "\t\tint x = 123;\n" 13625 "\t\tint random = 4;\n" 13626 "\t\tstd::string alphabet =\n" 13627 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13628 "\t}\n" 13629 "};", 13630 Tab); 13631 Tab.AlignOperands = FormatStyle::OAS_Align; 13632 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 13633 " cccccccccccccccccccc;", 13634 Tab); 13635 // no alignment 13636 verifyFormat("int aaaaaaaaaa =\n" 13637 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 13638 Tab); 13639 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 13640 " : bbbbbbbbbbbbbb ? 222222222222222\n" 13641 " : 333333333333333;", 13642 Tab); 13643 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 13644 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 13645 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 13646 " + cccccccccccccccccccc;", 13647 Tab); 13648 } 13649 13650 TEST_F(FormatTest, ZeroTabWidth) { 13651 FormatStyle Tab = getLLVMStyleWithColumns(42); 13652 Tab.IndentWidth = 8; 13653 Tab.UseTab = FormatStyle::UT_Never; 13654 Tab.TabWidth = 0; 13655 EXPECT_EQ("void a(){\n" 13656 " // line starts with '\t'\n" 13657 "};", 13658 format("void a(){\n" 13659 "\t// line starts with '\t'\n" 13660 "};", 13661 Tab)); 13662 13663 EXPECT_EQ("void a(){\n" 13664 " // line starts with '\t'\n" 13665 "};", 13666 format("void a(){\n" 13667 "\t\t// line starts with '\t'\n" 13668 "};", 13669 Tab)); 13670 13671 Tab.UseTab = FormatStyle::UT_ForIndentation; 13672 EXPECT_EQ("void a(){\n" 13673 " // line starts with '\t'\n" 13674 "};", 13675 format("void a(){\n" 13676 "\t// line starts with '\t'\n" 13677 "};", 13678 Tab)); 13679 13680 EXPECT_EQ("void a(){\n" 13681 " // line starts with '\t'\n" 13682 "};", 13683 format("void a(){\n" 13684 "\t\t// line starts with '\t'\n" 13685 "};", 13686 Tab)); 13687 13688 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13689 EXPECT_EQ("void a(){\n" 13690 " // line starts with '\t'\n" 13691 "};", 13692 format("void a(){\n" 13693 "\t// line starts with '\t'\n" 13694 "};", 13695 Tab)); 13696 13697 EXPECT_EQ("void a(){\n" 13698 " // line starts with '\t'\n" 13699 "};", 13700 format("void a(){\n" 13701 "\t\t// line starts with '\t'\n" 13702 "};", 13703 Tab)); 13704 13705 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13706 EXPECT_EQ("void a(){\n" 13707 " // line starts with '\t'\n" 13708 "};", 13709 format("void a(){\n" 13710 "\t// line starts with '\t'\n" 13711 "};", 13712 Tab)); 13713 13714 EXPECT_EQ("void a(){\n" 13715 " // line starts with '\t'\n" 13716 "};", 13717 format("void a(){\n" 13718 "\t\t// line starts with '\t'\n" 13719 "};", 13720 Tab)); 13721 13722 Tab.UseTab = FormatStyle::UT_Always; 13723 EXPECT_EQ("void a(){\n" 13724 "// line starts with '\t'\n" 13725 "};", 13726 format("void a(){\n" 13727 "\t// line starts with '\t'\n" 13728 "};", 13729 Tab)); 13730 13731 EXPECT_EQ("void a(){\n" 13732 "// line starts with '\t'\n" 13733 "};", 13734 format("void a(){\n" 13735 "\t\t// line starts with '\t'\n" 13736 "};", 13737 Tab)); 13738 } 13739 13740 TEST_F(FormatTest, CalculatesOriginalColumn) { 13741 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13742 "q\"; /* some\n" 13743 " comment */", 13744 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13745 "q\"; /* some\n" 13746 " comment */", 13747 getLLVMStyle())); 13748 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13749 "/* some\n" 13750 " comment */", 13751 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13752 " /* some\n" 13753 " comment */", 13754 getLLVMStyle())); 13755 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13756 "qqq\n" 13757 "/* some\n" 13758 " comment */", 13759 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13760 "qqq\n" 13761 " /* some\n" 13762 " comment */", 13763 getLLVMStyle())); 13764 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13765 "wwww; /* some\n" 13766 " comment */", 13767 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13768 "wwww; /* some\n" 13769 " comment */", 13770 getLLVMStyle())); 13771 } 13772 13773 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 13774 FormatStyle NoSpace = getLLVMStyle(); 13775 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 13776 13777 verifyFormat("while(true)\n" 13778 " continue;", 13779 NoSpace); 13780 verifyFormat("for(;;)\n" 13781 " continue;", 13782 NoSpace); 13783 verifyFormat("if(true)\n" 13784 " f();\n" 13785 "else if(true)\n" 13786 " f();", 13787 NoSpace); 13788 verifyFormat("do {\n" 13789 " do_something();\n" 13790 "} while(something());", 13791 NoSpace); 13792 verifyFormat("switch(x) {\n" 13793 "default:\n" 13794 " break;\n" 13795 "}", 13796 NoSpace); 13797 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 13798 verifyFormat("size_t x = sizeof(x);", NoSpace); 13799 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 13800 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 13801 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 13802 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 13803 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 13804 verifyFormat("alignas(128) char a[128];", NoSpace); 13805 verifyFormat("size_t x = alignof(MyType);", NoSpace); 13806 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 13807 verifyFormat("int f() throw(Deprecated);", NoSpace); 13808 verifyFormat("typedef void (*cb)(int);", NoSpace); 13809 verifyFormat("T A::operator()();", NoSpace); 13810 verifyFormat("X A::operator++(T);", NoSpace); 13811 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 13812 13813 FormatStyle Space = getLLVMStyle(); 13814 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 13815 13816 verifyFormat("int f ();", Space); 13817 verifyFormat("void f (int a, T b) {\n" 13818 " while (true)\n" 13819 " continue;\n" 13820 "}", 13821 Space); 13822 verifyFormat("if (true)\n" 13823 " f ();\n" 13824 "else if (true)\n" 13825 " f ();", 13826 Space); 13827 verifyFormat("do {\n" 13828 " do_something ();\n" 13829 "} while (something ());", 13830 Space); 13831 verifyFormat("switch (x) {\n" 13832 "default:\n" 13833 " break;\n" 13834 "}", 13835 Space); 13836 verifyFormat("A::A () : a (1) {}", Space); 13837 verifyFormat("void f () __attribute__ ((asdf));", Space); 13838 verifyFormat("*(&a + 1);\n" 13839 "&((&a)[1]);\n" 13840 "a[(b + c) * d];\n" 13841 "(((a + 1) * 2) + 3) * 4;", 13842 Space); 13843 verifyFormat("#define A(x) x", Space); 13844 verifyFormat("#define A (x) x", Space); 13845 verifyFormat("#if defined(x)\n" 13846 "#endif", 13847 Space); 13848 verifyFormat("auto i = std::make_unique<int> (5);", Space); 13849 verifyFormat("size_t x = sizeof (x);", Space); 13850 verifyFormat("auto f (int x) -> decltype (x);", Space); 13851 verifyFormat("auto f (int x) -> typeof (x);", Space); 13852 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 13853 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 13854 verifyFormat("int f (T x) noexcept (x.create ());", Space); 13855 verifyFormat("alignas (128) char a[128];", Space); 13856 verifyFormat("size_t x = alignof (MyType);", Space); 13857 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 13858 verifyFormat("int f () throw (Deprecated);", Space); 13859 verifyFormat("typedef void (*cb) (int);", Space); 13860 verifyFormat("T A::operator() ();", Space); 13861 verifyFormat("X A::operator++ (T);", Space); 13862 verifyFormat("auto lambda = [] () { return 0; };", Space); 13863 verifyFormat("int x = int (y);", Space); 13864 13865 FormatStyle SomeSpace = getLLVMStyle(); 13866 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 13867 13868 verifyFormat("[]() -> float {}", SomeSpace); 13869 verifyFormat("[] (auto foo) {}", SomeSpace); 13870 verifyFormat("[foo]() -> int {}", SomeSpace); 13871 verifyFormat("int f();", SomeSpace); 13872 verifyFormat("void f (int a, T b) {\n" 13873 " while (true)\n" 13874 " continue;\n" 13875 "}", 13876 SomeSpace); 13877 verifyFormat("if (true)\n" 13878 " f();\n" 13879 "else if (true)\n" 13880 " f();", 13881 SomeSpace); 13882 verifyFormat("do {\n" 13883 " do_something();\n" 13884 "} while (something());", 13885 SomeSpace); 13886 verifyFormat("switch (x) {\n" 13887 "default:\n" 13888 " break;\n" 13889 "}", 13890 SomeSpace); 13891 verifyFormat("A::A() : a (1) {}", SomeSpace); 13892 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 13893 verifyFormat("*(&a + 1);\n" 13894 "&((&a)[1]);\n" 13895 "a[(b + c) * d];\n" 13896 "(((a + 1) * 2) + 3) * 4;", 13897 SomeSpace); 13898 verifyFormat("#define A(x) x", SomeSpace); 13899 verifyFormat("#define A (x) x", SomeSpace); 13900 verifyFormat("#if defined(x)\n" 13901 "#endif", 13902 SomeSpace); 13903 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 13904 verifyFormat("size_t x = sizeof (x);", SomeSpace); 13905 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 13906 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 13907 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 13908 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 13909 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 13910 verifyFormat("alignas (128) char a[128];", SomeSpace); 13911 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 13912 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 13913 SomeSpace); 13914 verifyFormat("int f() throw (Deprecated);", SomeSpace); 13915 verifyFormat("typedef void (*cb) (int);", SomeSpace); 13916 verifyFormat("T A::operator()();", SomeSpace); 13917 verifyFormat("X A::operator++ (T);", SomeSpace); 13918 verifyFormat("int x = int (y);", SomeSpace); 13919 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 13920 } 13921 13922 TEST_F(FormatTest, SpaceAfterLogicalNot) { 13923 FormatStyle Spaces = getLLVMStyle(); 13924 Spaces.SpaceAfterLogicalNot = true; 13925 13926 verifyFormat("bool x = ! y", Spaces); 13927 verifyFormat("if (! isFailure())", Spaces); 13928 verifyFormat("if (! (a && b))", Spaces); 13929 verifyFormat("\"Error!\"", Spaces); 13930 verifyFormat("! ! x", Spaces); 13931 } 13932 13933 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 13934 FormatStyle Spaces = getLLVMStyle(); 13935 13936 Spaces.SpacesInParentheses = true; 13937 verifyFormat("do_something( ::globalVar );", Spaces); 13938 verifyFormat("call( x, y, z );", Spaces); 13939 verifyFormat("call();", Spaces); 13940 verifyFormat("std::function<void( int, int )> callback;", Spaces); 13941 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 13942 Spaces); 13943 verifyFormat("while ( (bool)1 )\n" 13944 " continue;", 13945 Spaces); 13946 verifyFormat("for ( ;; )\n" 13947 " continue;", 13948 Spaces); 13949 verifyFormat("if ( true )\n" 13950 " f();\n" 13951 "else if ( true )\n" 13952 " f();", 13953 Spaces); 13954 verifyFormat("do {\n" 13955 " do_something( (int)i );\n" 13956 "} while ( something() );", 13957 Spaces); 13958 verifyFormat("switch ( x ) {\n" 13959 "default:\n" 13960 " break;\n" 13961 "}", 13962 Spaces); 13963 13964 Spaces.SpacesInParentheses = false; 13965 Spaces.SpacesInCStyleCastParentheses = true; 13966 verifyFormat("Type *A = ( Type * )P;", Spaces); 13967 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 13968 verifyFormat("x = ( int32 )y;", Spaces); 13969 verifyFormat("int a = ( int )(2.0f);", Spaces); 13970 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 13971 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 13972 verifyFormat("#define x (( int )-1)", Spaces); 13973 13974 // Run the first set of tests again with: 13975 Spaces.SpacesInParentheses = false; 13976 Spaces.SpaceInEmptyParentheses = true; 13977 Spaces.SpacesInCStyleCastParentheses = true; 13978 verifyFormat("call(x, y, z);", Spaces); 13979 verifyFormat("call( );", Spaces); 13980 verifyFormat("std::function<void(int, int)> callback;", Spaces); 13981 verifyFormat("while (( bool )1)\n" 13982 " continue;", 13983 Spaces); 13984 verifyFormat("for (;;)\n" 13985 " continue;", 13986 Spaces); 13987 verifyFormat("if (true)\n" 13988 " f( );\n" 13989 "else if (true)\n" 13990 " f( );", 13991 Spaces); 13992 verifyFormat("do {\n" 13993 " do_something(( int )i);\n" 13994 "} while (something( ));", 13995 Spaces); 13996 verifyFormat("switch (x) {\n" 13997 "default:\n" 13998 " break;\n" 13999 "}", 14000 Spaces); 14001 14002 // Run the first set of tests again with: 14003 Spaces.SpaceAfterCStyleCast = true; 14004 verifyFormat("call(x, y, z);", Spaces); 14005 verifyFormat("call( );", Spaces); 14006 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14007 verifyFormat("while (( bool ) 1)\n" 14008 " continue;", 14009 Spaces); 14010 verifyFormat("for (;;)\n" 14011 " continue;", 14012 Spaces); 14013 verifyFormat("if (true)\n" 14014 " f( );\n" 14015 "else if (true)\n" 14016 " f( );", 14017 Spaces); 14018 verifyFormat("do {\n" 14019 " do_something(( int ) i);\n" 14020 "} while (something( ));", 14021 Spaces); 14022 verifyFormat("switch (x) {\n" 14023 "default:\n" 14024 " break;\n" 14025 "}", 14026 Spaces); 14027 14028 // Run subset of tests again with: 14029 Spaces.SpacesInCStyleCastParentheses = false; 14030 Spaces.SpaceAfterCStyleCast = true; 14031 verifyFormat("while ((bool) 1)\n" 14032 " continue;", 14033 Spaces); 14034 verifyFormat("do {\n" 14035 " do_something((int) i);\n" 14036 "} while (something( ));", 14037 Spaces); 14038 14039 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 14040 verifyFormat("size_t idx = (size_t) a;", Spaces); 14041 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 14042 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14043 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14044 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14045 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14046 Spaces.ColumnLimit = 80; 14047 Spaces.IndentWidth = 4; 14048 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 14049 verifyFormat("void foo( ) {\n" 14050 " size_t foo = (*(function))(\n" 14051 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14052 "BarrrrrrrrrrrrLong,\n" 14053 " FoooooooooLooooong);\n" 14054 "}", 14055 Spaces); 14056 Spaces.SpaceAfterCStyleCast = false; 14057 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 14058 verifyFormat("size_t idx = (size_t)a;", Spaces); 14059 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 14060 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14061 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14062 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14063 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14064 14065 verifyFormat("void foo( ) {\n" 14066 " size_t foo = (*(function))(\n" 14067 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14068 "BarrrrrrrrrrrrLong,\n" 14069 " FoooooooooLooooong);\n" 14070 "}", 14071 Spaces); 14072 } 14073 14074 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 14075 verifyFormat("int a[5];"); 14076 verifyFormat("a[3] += 42;"); 14077 14078 FormatStyle Spaces = getLLVMStyle(); 14079 Spaces.SpacesInSquareBrackets = true; 14080 // Not lambdas. 14081 verifyFormat("int a[ 5 ];", Spaces); 14082 verifyFormat("a[ 3 ] += 42;", Spaces); 14083 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 14084 verifyFormat("double &operator[](int i) { return 0; }\n" 14085 "int i;", 14086 Spaces); 14087 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 14088 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 14089 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 14090 // Lambdas. 14091 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 14092 verifyFormat("return [ i, args... ] {};", Spaces); 14093 verifyFormat("int foo = [ &bar ]() {};", Spaces); 14094 verifyFormat("int foo = [ = ]() {};", Spaces); 14095 verifyFormat("int foo = [ & ]() {};", Spaces); 14096 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 14097 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 14098 } 14099 14100 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 14101 FormatStyle NoSpaceStyle = getLLVMStyle(); 14102 verifyFormat("int a[5];", NoSpaceStyle); 14103 verifyFormat("a[3] += 42;", NoSpaceStyle); 14104 14105 verifyFormat("int a[1];", NoSpaceStyle); 14106 verifyFormat("int 1 [a];", NoSpaceStyle); 14107 verifyFormat("int a[1][2];", NoSpaceStyle); 14108 verifyFormat("a[7] = 5;", NoSpaceStyle); 14109 verifyFormat("int a = (f())[23];", NoSpaceStyle); 14110 verifyFormat("f([] {})", NoSpaceStyle); 14111 14112 FormatStyle Space = getLLVMStyle(); 14113 Space.SpaceBeforeSquareBrackets = true; 14114 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 14115 verifyFormat("return [i, args...] {};", Space); 14116 14117 verifyFormat("int a [5];", Space); 14118 verifyFormat("a [3] += 42;", Space); 14119 verifyFormat("constexpr char hello []{\"hello\"};", Space); 14120 verifyFormat("double &operator[](int i) { return 0; }\n" 14121 "int i;", 14122 Space); 14123 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 14124 verifyFormat("int i = a [a][a]->f();", Space); 14125 verifyFormat("int i = (*b) [a]->f();", Space); 14126 14127 verifyFormat("int a [1];", Space); 14128 verifyFormat("int 1 [a];", Space); 14129 verifyFormat("int a [1][2];", Space); 14130 verifyFormat("a [7] = 5;", Space); 14131 verifyFormat("int a = (f()) [23];", Space); 14132 verifyFormat("f([] {})", Space); 14133 } 14134 14135 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 14136 verifyFormat("int a = 5;"); 14137 verifyFormat("a += 42;"); 14138 verifyFormat("a or_eq 8;"); 14139 14140 FormatStyle Spaces = getLLVMStyle(); 14141 Spaces.SpaceBeforeAssignmentOperators = false; 14142 verifyFormat("int a= 5;", Spaces); 14143 verifyFormat("a+= 42;", Spaces); 14144 verifyFormat("a or_eq 8;", Spaces); 14145 } 14146 14147 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 14148 verifyFormat("class Foo : public Bar {};"); 14149 verifyFormat("Foo::Foo() : foo(1) {}"); 14150 verifyFormat("for (auto a : b) {\n}"); 14151 verifyFormat("int x = a ? b : c;"); 14152 verifyFormat("{\n" 14153 "label0:\n" 14154 " int x = 0;\n" 14155 "}"); 14156 verifyFormat("switch (x) {\n" 14157 "case 1:\n" 14158 "default:\n" 14159 "}"); 14160 verifyFormat("switch (allBraces) {\n" 14161 "case 1: {\n" 14162 " break;\n" 14163 "}\n" 14164 "case 2: {\n" 14165 " [[fallthrough]];\n" 14166 "}\n" 14167 "default: {\n" 14168 " break;\n" 14169 "}\n" 14170 "}"); 14171 14172 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 14173 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 14174 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 14175 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 14176 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 14177 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 14178 verifyFormat("{\n" 14179 "label1:\n" 14180 " int x = 0;\n" 14181 "}", 14182 CtorInitializerStyle); 14183 verifyFormat("switch (x) {\n" 14184 "case 1:\n" 14185 "default:\n" 14186 "}", 14187 CtorInitializerStyle); 14188 verifyFormat("switch (allBraces) {\n" 14189 "case 1: {\n" 14190 " break;\n" 14191 "}\n" 14192 "case 2: {\n" 14193 " [[fallthrough]];\n" 14194 "}\n" 14195 "default: {\n" 14196 " break;\n" 14197 "}\n" 14198 "}", 14199 CtorInitializerStyle); 14200 CtorInitializerStyle.BreakConstructorInitializers = 14201 FormatStyle::BCIS_AfterColon; 14202 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 14203 " aaaaaaaaaaaaaaaa(1),\n" 14204 " bbbbbbbbbbbbbbbb(2) {}", 14205 CtorInitializerStyle); 14206 CtorInitializerStyle.BreakConstructorInitializers = 14207 FormatStyle::BCIS_BeforeComma; 14208 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14209 " : aaaaaaaaaaaaaaaa(1)\n" 14210 " , bbbbbbbbbbbbbbbb(2) {}", 14211 CtorInitializerStyle); 14212 CtorInitializerStyle.BreakConstructorInitializers = 14213 FormatStyle::BCIS_BeforeColon; 14214 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14215 " : aaaaaaaaaaaaaaaa(1),\n" 14216 " bbbbbbbbbbbbbbbb(2) {}", 14217 CtorInitializerStyle); 14218 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 14219 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14220 ": aaaaaaaaaaaaaaaa(1),\n" 14221 " bbbbbbbbbbbbbbbb(2) {}", 14222 CtorInitializerStyle); 14223 14224 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 14225 InheritanceStyle.SpaceBeforeInheritanceColon = false; 14226 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 14227 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 14228 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 14229 verifyFormat("int x = a ? b : c;", InheritanceStyle); 14230 verifyFormat("{\n" 14231 "label2:\n" 14232 " int x = 0;\n" 14233 "}", 14234 InheritanceStyle); 14235 verifyFormat("switch (x) {\n" 14236 "case 1:\n" 14237 "default:\n" 14238 "}", 14239 InheritanceStyle); 14240 verifyFormat("switch (allBraces) {\n" 14241 "case 1: {\n" 14242 " break;\n" 14243 "}\n" 14244 "case 2: {\n" 14245 " [[fallthrough]];\n" 14246 "}\n" 14247 "default: {\n" 14248 " break;\n" 14249 "}\n" 14250 "}", 14251 InheritanceStyle); 14252 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 14253 verifyFormat("class Foooooooooooooooooooooo\n" 14254 " : public aaaaaaaaaaaaaaaaaa,\n" 14255 " public bbbbbbbbbbbbbbbbbb {\n" 14256 "}", 14257 InheritanceStyle); 14258 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 14259 verifyFormat("class Foooooooooooooooooooooo:\n" 14260 " public aaaaaaaaaaaaaaaaaa,\n" 14261 " public bbbbbbbbbbbbbbbbbb {\n" 14262 "}", 14263 InheritanceStyle); 14264 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 14265 verifyFormat("class Foooooooooooooooooooooo\n" 14266 " : public aaaaaaaaaaaaaaaaaa\n" 14267 " , public bbbbbbbbbbbbbbbbbb {\n" 14268 "}", 14269 InheritanceStyle); 14270 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 14271 verifyFormat("class Foooooooooooooooooooooo\n" 14272 " : public aaaaaaaaaaaaaaaaaa,\n" 14273 " public bbbbbbbbbbbbbbbbbb {\n" 14274 "}", 14275 InheritanceStyle); 14276 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 14277 verifyFormat("class Foooooooooooooooooooooo\n" 14278 ": public aaaaaaaaaaaaaaaaaa,\n" 14279 " public bbbbbbbbbbbbbbbbbb {}", 14280 InheritanceStyle); 14281 14282 FormatStyle ForLoopStyle = getLLVMStyle(); 14283 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 14284 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 14285 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 14286 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 14287 verifyFormat("int x = a ? b : c;", ForLoopStyle); 14288 verifyFormat("{\n" 14289 "label2:\n" 14290 " int x = 0;\n" 14291 "}", 14292 ForLoopStyle); 14293 verifyFormat("switch (x) {\n" 14294 "case 1:\n" 14295 "default:\n" 14296 "}", 14297 ForLoopStyle); 14298 verifyFormat("switch (allBraces) {\n" 14299 "case 1: {\n" 14300 " break;\n" 14301 "}\n" 14302 "case 2: {\n" 14303 " [[fallthrough]];\n" 14304 "}\n" 14305 "default: {\n" 14306 " break;\n" 14307 "}\n" 14308 "}", 14309 ForLoopStyle); 14310 14311 FormatStyle CaseStyle = getLLVMStyle(); 14312 CaseStyle.SpaceBeforeCaseColon = true; 14313 verifyFormat("class Foo : public Bar {};", CaseStyle); 14314 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 14315 verifyFormat("for (auto a : b) {\n}", CaseStyle); 14316 verifyFormat("int x = a ? b : c;", CaseStyle); 14317 verifyFormat("switch (x) {\n" 14318 "case 1 :\n" 14319 "default :\n" 14320 "}", 14321 CaseStyle); 14322 verifyFormat("switch (allBraces) {\n" 14323 "case 1 : {\n" 14324 " break;\n" 14325 "}\n" 14326 "case 2 : {\n" 14327 " [[fallthrough]];\n" 14328 "}\n" 14329 "default : {\n" 14330 " break;\n" 14331 "}\n" 14332 "}", 14333 CaseStyle); 14334 14335 FormatStyle NoSpaceStyle = getLLVMStyle(); 14336 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 14337 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14338 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 14339 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14340 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 14341 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 14342 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 14343 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 14344 verifyFormat("{\n" 14345 "label3:\n" 14346 " int x = 0;\n" 14347 "}", 14348 NoSpaceStyle); 14349 verifyFormat("switch (x) {\n" 14350 "case 1:\n" 14351 "default:\n" 14352 "}", 14353 NoSpaceStyle); 14354 verifyFormat("switch (allBraces) {\n" 14355 "case 1: {\n" 14356 " break;\n" 14357 "}\n" 14358 "case 2: {\n" 14359 " [[fallthrough]];\n" 14360 "}\n" 14361 "default: {\n" 14362 " break;\n" 14363 "}\n" 14364 "}", 14365 NoSpaceStyle); 14366 14367 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 14368 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 14369 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14370 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 14371 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14372 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 14373 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 14374 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 14375 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 14376 verifyFormat("{\n" 14377 "label3:\n" 14378 " int x = 0;\n" 14379 "}", 14380 InvertedSpaceStyle); 14381 verifyFormat("switch (x) {\n" 14382 "case 1 :\n" 14383 "case 2 : {\n" 14384 " break;\n" 14385 "}\n" 14386 "default :\n" 14387 " break;\n" 14388 "}", 14389 InvertedSpaceStyle); 14390 verifyFormat("switch (allBraces) {\n" 14391 "case 1 : {\n" 14392 " break;\n" 14393 "}\n" 14394 "case 2 : {\n" 14395 " [[fallthrough]];\n" 14396 "}\n" 14397 "default : {\n" 14398 " break;\n" 14399 "}\n" 14400 "}", 14401 InvertedSpaceStyle); 14402 } 14403 14404 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 14405 FormatStyle Style = getLLVMStyle(); 14406 14407 Style.PointerAlignment = FormatStyle::PAS_Left; 14408 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14409 verifyFormat("void* const* x = NULL;", Style); 14410 14411 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 14412 do { \ 14413 Style.PointerAlignment = FormatStyle::Pointers; \ 14414 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 14415 verifyFormat(Code, Style); \ 14416 } while (false) 14417 14418 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 14419 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 14420 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 14421 14422 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 14423 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 14424 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 14425 14426 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 14427 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 14428 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 14429 14430 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 14431 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 14432 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 14433 14434 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 14435 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14436 SAPQ_Default); 14437 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14438 SAPQ_Default); 14439 14440 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 14441 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14442 SAPQ_Before); 14443 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14444 SAPQ_Before); 14445 14446 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 14447 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 14448 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14449 SAPQ_After); 14450 14451 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 14452 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 14453 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 14454 14455 #undef verifyQualifierSpaces 14456 14457 FormatStyle Spaces = getLLVMStyle(); 14458 Spaces.AttributeMacros.push_back("qualified"); 14459 Spaces.PointerAlignment = FormatStyle::PAS_Right; 14460 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14461 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 14462 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 14463 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 14464 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 14465 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14466 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14467 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 14468 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 14469 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14470 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14471 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14472 14473 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 14474 Spaces.PointerAlignment = FormatStyle::PAS_Left; 14475 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14476 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 14477 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 14478 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 14479 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 14480 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14481 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 14482 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14483 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 14484 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 14485 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 14486 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 14487 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14488 14489 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 14490 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 14491 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14492 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 14493 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 14494 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14495 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14496 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14497 } 14498 14499 TEST_F(FormatTest, AlignConsecutiveMacros) { 14500 FormatStyle Style = getLLVMStyle(); 14501 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14502 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14503 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14504 14505 verifyFormat("#define a 3\n" 14506 "#define bbbb 4\n" 14507 "#define ccc (5)", 14508 Style); 14509 14510 verifyFormat("#define f(x) (x * x)\n" 14511 "#define fff(x, y, z) (x * y + z)\n" 14512 "#define ffff(x, y) (x - y)", 14513 Style); 14514 14515 verifyFormat("#define foo(x, y) (x + y)\n" 14516 "#define bar (5, 6)(2 + 2)", 14517 Style); 14518 14519 verifyFormat("#define a 3\n" 14520 "#define bbbb 4\n" 14521 "#define ccc (5)\n" 14522 "#define f(x) (x * x)\n" 14523 "#define fff(x, y, z) (x * y + z)\n" 14524 "#define ffff(x, y) (x - y)", 14525 Style); 14526 14527 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14528 verifyFormat("#define a 3\n" 14529 "#define bbbb 4\n" 14530 "#define ccc (5)", 14531 Style); 14532 14533 verifyFormat("#define f(x) (x * x)\n" 14534 "#define fff(x, y, z) (x * y + z)\n" 14535 "#define ffff(x, y) (x - y)", 14536 Style); 14537 14538 verifyFormat("#define foo(x, y) (x + y)\n" 14539 "#define bar (5, 6)(2 + 2)", 14540 Style); 14541 14542 verifyFormat("#define a 3\n" 14543 "#define bbbb 4\n" 14544 "#define ccc (5)\n" 14545 "#define f(x) (x * x)\n" 14546 "#define fff(x, y, z) (x * y + z)\n" 14547 "#define ffff(x, y) (x - y)", 14548 Style); 14549 14550 verifyFormat("#define a 5\n" 14551 "#define foo(x, y) (x + y)\n" 14552 "#define CCC (6)\n" 14553 "auto lambda = []() {\n" 14554 " auto ii = 0;\n" 14555 " float j = 0;\n" 14556 " return 0;\n" 14557 "};\n" 14558 "int i = 0;\n" 14559 "float i2 = 0;\n" 14560 "auto v = type{\n" 14561 " i = 1, //\n" 14562 " (i = 2), //\n" 14563 " i = 3 //\n" 14564 "};", 14565 Style); 14566 14567 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14568 Style.ColumnLimit = 20; 14569 14570 verifyFormat("#define a \\\n" 14571 " \"aabbbbbbbbbbbb\"\n" 14572 "#define D \\\n" 14573 " \"aabbbbbbbbbbbb\" \\\n" 14574 " \"ccddeeeeeeeee\"\n" 14575 "#define B \\\n" 14576 " \"QQQQQQQQQQQQQ\" \\\n" 14577 " \"FFFFFFFFFFFFF\" \\\n" 14578 " \"LLLLLLLL\"\n", 14579 Style); 14580 14581 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14582 verifyFormat("#define a \\\n" 14583 " \"aabbbbbbbbbbbb\"\n" 14584 "#define D \\\n" 14585 " \"aabbbbbbbbbbbb\" \\\n" 14586 " \"ccddeeeeeeeee\"\n" 14587 "#define B \\\n" 14588 " \"QQQQQQQQQQQQQ\" \\\n" 14589 " \"FFFFFFFFFFFFF\" \\\n" 14590 " \"LLLLLLLL\"\n", 14591 Style); 14592 14593 // Test across comments 14594 Style.MaxEmptyLinesToKeep = 10; 14595 Style.ReflowComments = false; 14596 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 14597 EXPECT_EQ("#define a 3\n" 14598 "// line comment\n" 14599 "#define bbbb 4\n" 14600 "#define ccc (5)", 14601 format("#define a 3\n" 14602 "// line comment\n" 14603 "#define bbbb 4\n" 14604 "#define ccc (5)", 14605 Style)); 14606 14607 EXPECT_EQ("#define a 3\n" 14608 "/* block comment */\n" 14609 "#define bbbb 4\n" 14610 "#define ccc (5)", 14611 format("#define a 3\n" 14612 "/* block comment */\n" 14613 "#define bbbb 4\n" 14614 "#define ccc (5)", 14615 Style)); 14616 14617 EXPECT_EQ("#define a 3\n" 14618 "/* multi-line *\n" 14619 " * block comment */\n" 14620 "#define bbbb 4\n" 14621 "#define ccc (5)", 14622 format("#define a 3\n" 14623 "/* multi-line *\n" 14624 " * block comment */\n" 14625 "#define bbbb 4\n" 14626 "#define ccc (5)", 14627 Style)); 14628 14629 EXPECT_EQ("#define a 3\n" 14630 "// multi-line line comment\n" 14631 "//\n" 14632 "#define bbbb 4\n" 14633 "#define ccc (5)", 14634 format("#define a 3\n" 14635 "// multi-line line comment\n" 14636 "//\n" 14637 "#define bbbb 4\n" 14638 "#define ccc (5)", 14639 Style)); 14640 14641 EXPECT_EQ("#define a 3\n" 14642 "// empty lines still break.\n" 14643 "\n" 14644 "#define bbbb 4\n" 14645 "#define ccc (5)", 14646 format("#define a 3\n" 14647 "// empty lines still break.\n" 14648 "\n" 14649 "#define bbbb 4\n" 14650 "#define ccc (5)", 14651 Style)); 14652 14653 // Test across empty lines 14654 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 14655 EXPECT_EQ("#define a 3\n" 14656 "\n" 14657 "#define bbbb 4\n" 14658 "#define ccc (5)", 14659 format("#define a 3\n" 14660 "\n" 14661 "#define bbbb 4\n" 14662 "#define ccc (5)", 14663 Style)); 14664 14665 EXPECT_EQ("#define a 3\n" 14666 "\n" 14667 "\n" 14668 "\n" 14669 "#define bbbb 4\n" 14670 "#define ccc (5)", 14671 format("#define a 3\n" 14672 "\n" 14673 "\n" 14674 "\n" 14675 "#define bbbb 4\n" 14676 "#define ccc (5)", 14677 Style)); 14678 14679 EXPECT_EQ("#define a 3\n" 14680 "// comments should break alignment\n" 14681 "//\n" 14682 "#define bbbb 4\n" 14683 "#define ccc (5)", 14684 format("#define a 3\n" 14685 "// comments should break alignment\n" 14686 "//\n" 14687 "#define bbbb 4\n" 14688 "#define ccc (5)", 14689 Style)); 14690 14691 // Test across empty lines and comments 14692 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 14693 verifyFormat("#define a 3\n" 14694 "\n" 14695 "// line comment\n" 14696 "#define bbbb 4\n" 14697 "#define ccc (5)", 14698 Style); 14699 14700 EXPECT_EQ("#define a 3\n" 14701 "\n" 14702 "\n" 14703 "/* multi-line *\n" 14704 " * block comment */\n" 14705 "\n" 14706 "\n" 14707 "#define bbbb 4\n" 14708 "#define ccc (5)", 14709 format("#define a 3\n" 14710 "\n" 14711 "\n" 14712 "/* multi-line *\n" 14713 " * block comment */\n" 14714 "\n" 14715 "\n" 14716 "#define bbbb 4\n" 14717 "#define ccc (5)", 14718 Style)); 14719 14720 EXPECT_EQ("#define a 3\n" 14721 "\n" 14722 "\n" 14723 "/* multi-line *\n" 14724 " * block comment */\n" 14725 "\n" 14726 "\n" 14727 "#define bbbb 4\n" 14728 "#define ccc (5)", 14729 format("#define a 3\n" 14730 "\n" 14731 "\n" 14732 "/* multi-line *\n" 14733 " * block comment */\n" 14734 "\n" 14735 "\n" 14736 "#define bbbb 4\n" 14737 "#define ccc (5)", 14738 Style)); 14739 } 14740 14741 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 14742 FormatStyle Alignment = getLLVMStyle(); 14743 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14744 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 14745 14746 Alignment.MaxEmptyLinesToKeep = 10; 14747 /* Test alignment across empty lines */ 14748 EXPECT_EQ("int a = 5;\n" 14749 "\n" 14750 "int oneTwoThree = 123;", 14751 format("int a = 5;\n" 14752 "\n" 14753 "int oneTwoThree= 123;", 14754 Alignment)); 14755 EXPECT_EQ("int a = 5;\n" 14756 "int one = 1;\n" 14757 "\n" 14758 "int oneTwoThree = 123;", 14759 format("int a = 5;\n" 14760 "int one = 1;\n" 14761 "\n" 14762 "int oneTwoThree = 123;", 14763 Alignment)); 14764 EXPECT_EQ("int a = 5;\n" 14765 "int one = 1;\n" 14766 "\n" 14767 "int oneTwoThree = 123;\n" 14768 "int oneTwo = 12;", 14769 format("int a = 5;\n" 14770 "int one = 1;\n" 14771 "\n" 14772 "int oneTwoThree = 123;\n" 14773 "int oneTwo = 12;", 14774 Alignment)); 14775 14776 /* Test across comments */ 14777 EXPECT_EQ("int a = 5;\n" 14778 "/* block comment */\n" 14779 "int oneTwoThree = 123;", 14780 format("int a = 5;\n" 14781 "/* block comment */\n" 14782 "int oneTwoThree=123;", 14783 Alignment)); 14784 14785 EXPECT_EQ("int a = 5;\n" 14786 "// line comment\n" 14787 "int oneTwoThree = 123;", 14788 format("int a = 5;\n" 14789 "// line comment\n" 14790 "int oneTwoThree=123;", 14791 Alignment)); 14792 14793 /* Test across comments and newlines */ 14794 EXPECT_EQ("int a = 5;\n" 14795 "\n" 14796 "/* block comment */\n" 14797 "int oneTwoThree = 123;", 14798 format("int a = 5;\n" 14799 "\n" 14800 "/* block comment */\n" 14801 "int oneTwoThree=123;", 14802 Alignment)); 14803 14804 EXPECT_EQ("int a = 5;\n" 14805 "\n" 14806 "// line comment\n" 14807 "int oneTwoThree = 123;", 14808 format("int a = 5;\n" 14809 "\n" 14810 "// line comment\n" 14811 "int oneTwoThree=123;", 14812 Alignment)); 14813 } 14814 14815 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 14816 FormatStyle Alignment = getLLVMStyle(); 14817 Alignment.AlignConsecutiveDeclarations = 14818 FormatStyle::ACS_AcrossEmptyLinesAndComments; 14819 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 14820 14821 Alignment.MaxEmptyLinesToKeep = 10; 14822 /* Test alignment across empty lines */ 14823 EXPECT_EQ("int a = 5;\n" 14824 "\n" 14825 "float const oneTwoThree = 123;", 14826 format("int a = 5;\n" 14827 "\n" 14828 "float const oneTwoThree = 123;", 14829 Alignment)); 14830 EXPECT_EQ("int a = 5;\n" 14831 "float const one = 1;\n" 14832 "\n" 14833 "int oneTwoThree = 123;", 14834 format("int a = 5;\n" 14835 "float const one = 1;\n" 14836 "\n" 14837 "int oneTwoThree = 123;", 14838 Alignment)); 14839 14840 /* Test across comments */ 14841 EXPECT_EQ("float const a = 5;\n" 14842 "/* block comment */\n" 14843 "int oneTwoThree = 123;", 14844 format("float const a = 5;\n" 14845 "/* block comment */\n" 14846 "int oneTwoThree=123;", 14847 Alignment)); 14848 14849 EXPECT_EQ("float const a = 5;\n" 14850 "// line comment\n" 14851 "int oneTwoThree = 123;", 14852 format("float const a = 5;\n" 14853 "// line comment\n" 14854 "int oneTwoThree=123;", 14855 Alignment)); 14856 14857 /* Test across comments and newlines */ 14858 EXPECT_EQ("float const a = 5;\n" 14859 "\n" 14860 "/* block comment */\n" 14861 "int oneTwoThree = 123;", 14862 format("float const a = 5;\n" 14863 "\n" 14864 "/* block comment */\n" 14865 "int oneTwoThree=123;", 14866 Alignment)); 14867 14868 EXPECT_EQ("float const a = 5;\n" 14869 "\n" 14870 "// line comment\n" 14871 "int oneTwoThree = 123;", 14872 format("float const a = 5;\n" 14873 "\n" 14874 "// line comment\n" 14875 "int oneTwoThree=123;", 14876 Alignment)); 14877 } 14878 14879 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 14880 FormatStyle Alignment = getLLVMStyle(); 14881 Alignment.AlignConsecutiveBitFields = 14882 FormatStyle::ACS_AcrossEmptyLinesAndComments; 14883 14884 Alignment.MaxEmptyLinesToKeep = 10; 14885 /* Test alignment across empty lines */ 14886 EXPECT_EQ("int a : 5;\n" 14887 "\n" 14888 "int longbitfield : 6;", 14889 format("int a : 5;\n" 14890 "\n" 14891 "int longbitfield : 6;", 14892 Alignment)); 14893 EXPECT_EQ("int a : 5;\n" 14894 "int one : 1;\n" 14895 "\n" 14896 "int longbitfield : 6;", 14897 format("int a : 5;\n" 14898 "int one : 1;\n" 14899 "\n" 14900 "int longbitfield : 6;", 14901 Alignment)); 14902 14903 /* Test across comments */ 14904 EXPECT_EQ("int a : 5;\n" 14905 "/* block comment */\n" 14906 "int longbitfield : 6;", 14907 format("int a : 5;\n" 14908 "/* block comment */\n" 14909 "int longbitfield : 6;", 14910 Alignment)); 14911 EXPECT_EQ("int a : 5;\n" 14912 "int one : 1;\n" 14913 "// line comment\n" 14914 "int longbitfield : 6;", 14915 format("int a : 5;\n" 14916 "int one : 1;\n" 14917 "// line comment\n" 14918 "int longbitfield : 6;", 14919 Alignment)); 14920 14921 /* Test across comments and newlines */ 14922 EXPECT_EQ("int a : 5;\n" 14923 "/* block comment */\n" 14924 "\n" 14925 "int longbitfield : 6;", 14926 format("int a : 5;\n" 14927 "/* block comment */\n" 14928 "\n" 14929 "int longbitfield : 6;", 14930 Alignment)); 14931 EXPECT_EQ("int a : 5;\n" 14932 "int one : 1;\n" 14933 "\n" 14934 "// line comment\n" 14935 "\n" 14936 "int longbitfield : 6;", 14937 format("int a : 5;\n" 14938 "int one : 1;\n" 14939 "\n" 14940 "// line comment \n" 14941 "\n" 14942 "int longbitfield : 6;", 14943 Alignment)); 14944 } 14945 14946 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 14947 FormatStyle Alignment = getLLVMStyle(); 14948 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14949 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 14950 14951 Alignment.MaxEmptyLinesToKeep = 10; 14952 /* Test alignment across empty lines */ 14953 EXPECT_EQ("int a = 5;\n" 14954 "\n" 14955 "int oneTwoThree = 123;", 14956 format("int a = 5;\n" 14957 "\n" 14958 "int oneTwoThree= 123;", 14959 Alignment)); 14960 EXPECT_EQ("int a = 5;\n" 14961 "int one = 1;\n" 14962 "\n" 14963 "int oneTwoThree = 123;", 14964 format("int a = 5;\n" 14965 "int one = 1;\n" 14966 "\n" 14967 "int oneTwoThree = 123;", 14968 Alignment)); 14969 14970 /* Test across comments */ 14971 EXPECT_EQ("int a = 5;\n" 14972 "/* block comment */\n" 14973 "int oneTwoThree = 123;", 14974 format("int a = 5;\n" 14975 "/* block comment */\n" 14976 "int oneTwoThree=123;", 14977 Alignment)); 14978 14979 EXPECT_EQ("int a = 5;\n" 14980 "// line comment\n" 14981 "int oneTwoThree = 123;", 14982 format("int a = 5;\n" 14983 "// line comment\n" 14984 "int oneTwoThree=123;", 14985 Alignment)); 14986 14987 EXPECT_EQ("int a = 5;\n" 14988 "/*\n" 14989 " * multi-line block comment\n" 14990 " */\n" 14991 "int oneTwoThree = 123;", 14992 format("int a = 5;\n" 14993 "/*\n" 14994 " * multi-line block comment\n" 14995 " */\n" 14996 "int oneTwoThree=123;", 14997 Alignment)); 14998 14999 EXPECT_EQ("int a = 5;\n" 15000 "//\n" 15001 "// multi-line line comment\n" 15002 "//\n" 15003 "int oneTwoThree = 123;", 15004 format("int a = 5;\n" 15005 "//\n" 15006 "// multi-line line comment\n" 15007 "//\n" 15008 "int oneTwoThree=123;", 15009 Alignment)); 15010 15011 /* Test across comments and newlines */ 15012 EXPECT_EQ("int a = 5;\n" 15013 "\n" 15014 "/* block comment */\n" 15015 "int oneTwoThree = 123;", 15016 format("int a = 5;\n" 15017 "\n" 15018 "/* block comment */\n" 15019 "int oneTwoThree=123;", 15020 Alignment)); 15021 15022 EXPECT_EQ("int a = 5;\n" 15023 "\n" 15024 "// line comment\n" 15025 "int oneTwoThree = 123;", 15026 format("int a = 5;\n" 15027 "\n" 15028 "// line comment\n" 15029 "int oneTwoThree=123;", 15030 Alignment)); 15031 } 15032 15033 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 15034 FormatStyle Alignment = getLLVMStyle(); 15035 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15036 Alignment.AlignConsecutiveAssignments = 15037 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15038 verifyFormat("int a = 5;\n" 15039 "int oneTwoThree = 123;", 15040 Alignment); 15041 verifyFormat("int a = method();\n" 15042 "int oneTwoThree = 133;", 15043 Alignment); 15044 verifyFormat("a &= 5;\n" 15045 "bcd *= 5;\n" 15046 "ghtyf += 5;\n" 15047 "dvfvdb -= 5;\n" 15048 "a /= 5;\n" 15049 "vdsvsv %= 5;\n" 15050 "sfdbddfbdfbb ^= 5;\n" 15051 "dvsdsv |= 5;\n" 15052 "int dsvvdvsdvvv = 123;", 15053 Alignment); 15054 verifyFormat("int i = 1, j = 10;\n" 15055 "something = 2000;", 15056 Alignment); 15057 verifyFormat("something = 2000;\n" 15058 "int i = 1, j = 10;\n", 15059 Alignment); 15060 verifyFormat("something = 2000;\n" 15061 "another = 911;\n" 15062 "int i = 1, j = 10;\n" 15063 "oneMore = 1;\n" 15064 "i = 2;", 15065 Alignment); 15066 verifyFormat("int a = 5;\n" 15067 "int one = 1;\n" 15068 "method();\n" 15069 "int oneTwoThree = 123;\n" 15070 "int oneTwo = 12;", 15071 Alignment); 15072 verifyFormat("int oneTwoThree = 123;\n" 15073 "int oneTwo = 12;\n" 15074 "method();\n", 15075 Alignment); 15076 verifyFormat("int oneTwoThree = 123; // comment\n" 15077 "int oneTwo = 12; // comment", 15078 Alignment); 15079 15080 // Bug 25167 15081 /* Uncomment when fixed 15082 verifyFormat("#if A\n" 15083 "#else\n" 15084 "int aaaaaaaa = 12;\n" 15085 "#endif\n" 15086 "#if B\n" 15087 "#else\n" 15088 "int a = 12;\n" 15089 "#endif\n", 15090 Alignment); 15091 verifyFormat("enum foo {\n" 15092 "#if A\n" 15093 "#else\n" 15094 " aaaaaaaa = 12;\n" 15095 "#endif\n" 15096 "#if B\n" 15097 "#else\n" 15098 " a = 12;\n" 15099 "#endif\n" 15100 "};\n", 15101 Alignment); 15102 */ 15103 15104 Alignment.MaxEmptyLinesToKeep = 10; 15105 /* Test alignment across empty lines */ 15106 EXPECT_EQ("int a = 5;\n" 15107 "\n" 15108 "int oneTwoThree = 123;", 15109 format("int a = 5;\n" 15110 "\n" 15111 "int oneTwoThree= 123;", 15112 Alignment)); 15113 EXPECT_EQ("int a = 5;\n" 15114 "int one = 1;\n" 15115 "\n" 15116 "int oneTwoThree = 123;", 15117 format("int a = 5;\n" 15118 "int one = 1;\n" 15119 "\n" 15120 "int oneTwoThree = 123;", 15121 Alignment)); 15122 EXPECT_EQ("int a = 5;\n" 15123 "int one = 1;\n" 15124 "\n" 15125 "int oneTwoThree = 123;\n" 15126 "int oneTwo = 12;", 15127 format("int a = 5;\n" 15128 "int one = 1;\n" 15129 "\n" 15130 "int oneTwoThree = 123;\n" 15131 "int oneTwo = 12;", 15132 Alignment)); 15133 15134 /* Test across comments */ 15135 EXPECT_EQ("int a = 5;\n" 15136 "/* block comment */\n" 15137 "int oneTwoThree = 123;", 15138 format("int a = 5;\n" 15139 "/* block comment */\n" 15140 "int oneTwoThree=123;", 15141 Alignment)); 15142 15143 EXPECT_EQ("int a = 5;\n" 15144 "// line comment\n" 15145 "int oneTwoThree = 123;", 15146 format("int a = 5;\n" 15147 "// line comment\n" 15148 "int oneTwoThree=123;", 15149 Alignment)); 15150 15151 /* Test across comments and newlines */ 15152 EXPECT_EQ("int a = 5;\n" 15153 "\n" 15154 "/* block comment */\n" 15155 "int oneTwoThree = 123;", 15156 format("int a = 5;\n" 15157 "\n" 15158 "/* block comment */\n" 15159 "int oneTwoThree=123;", 15160 Alignment)); 15161 15162 EXPECT_EQ("int a = 5;\n" 15163 "\n" 15164 "// line comment\n" 15165 "int oneTwoThree = 123;", 15166 format("int a = 5;\n" 15167 "\n" 15168 "// line comment\n" 15169 "int oneTwoThree=123;", 15170 Alignment)); 15171 15172 EXPECT_EQ("int a = 5;\n" 15173 "//\n" 15174 "// multi-line line comment\n" 15175 "//\n" 15176 "int oneTwoThree = 123;", 15177 format("int a = 5;\n" 15178 "//\n" 15179 "// multi-line line comment\n" 15180 "//\n" 15181 "int oneTwoThree=123;", 15182 Alignment)); 15183 15184 EXPECT_EQ("int a = 5;\n" 15185 "/*\n" 15186 " * multi-line block comment\n" 15187 " */\n" 15188 "int oneTwoThree = 123;", 15189 format("int a = 5;\n" 15190 "/*\n" 15191 " * multi-line block comment\n" 15192 " */\n" 15193 "int oneTwoThree=123;", 15194 Alignment)); 15195 15196 EXPECT_EQ("int a = 5;\n" 15197 "\n" 15198 "/* block comment */\n" 15199 "\n" 15200 "\n" 15201 "\n" 15202 "int oneTwoThree = 123;", 15203 format("int a = 5;\n" 15204 "\n" 15205 "/* block comment */\n" 15206 "\n" 15207 "\n" 15208 "\n" 15209 "int oneTwoThree=123;", 15210 Alignment)); 15211 15212 EXPECT_EQ("int a = 5;\n" 15213 "\n" 15214 "// line comment\n" 15215 "\n" 15216 "\n" 15217 "\n" 15218 "int oneTwoThree = 123;", 15219 format("int a = 5;\n" 15220 "\n" 15221 "// line comment\n" 15222 "\n" 15223 "\n" 15224 "\n" 15225 "int oneTwoThree=123;", 15226 Alignment)); 15227 15228 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15229 verifyFormat("#define A \\\n" 15230 " int aaaa = 12; \\\n" 15231 " int b = 23; \\\n" 15232 " int ccc = 234; \\\n" 15233 " int dddddddddd = 2345;", 15234 Alignment); 15235 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15236 verifyFormat("#define A \\\n" 15237 " int aaaa = 12; \\\n" 15238 " int b = 23; \\\n" 15239 " int ccc = 234; \\\n" 15240 " int dddddddddd = 2345;", 15241 Alignment); 15242 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15243 verifyFormat("#define A " 15244 " \\\n" 15245 " int aaaa = 12; " 15246 " \\\n" 15247 " int b = 23; " 15248 " \\\n" 15249 " int ccc = 234; " 15250 " \\\n" 15251 " int dddddddddd = 2345;", 15252 Alignment); 15253 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15254 "k = 4, int l = 5,\n" 15255 " int m = 6) {\n" 15256 " int j = 10;\n" 15257 " otherThing = 1;\n" 15258 "}", 15259 Alignment); 15260 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15261 " int i = 1;\n" 15262 " int j = 2;\n" 15263 " int big = 10000;\n" 15264 "}", 15265 Alignment); 15266 verifyFormat("class C {\n" 15267 "public:\n" 15268 " int i = 1;\n" 15269 " virtual void f() = 0;\n" 15270 "};", 15271 Alignment); 15272 verifyFormat("int i = 1;\n" 15273 "if (SomeType t = getSomething()) {\n" 15274 "}\n" 15275 "int j = 2;\n" 15276 "int big = 10000;", 15277 Alignment); 15278 verifyFormat("int j = 7;\n" 15279 "for (int k = 0; k < N; ++k) {\n" 15280 "}\n" 15281 "int j = 2;\n" 15282 "int big = 10000;\n" 15283 "}", 15284 Alignment); 15285 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15286 verifyFormat("int i = 1;\n" 15287 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15288 " = someLooooooooooooooooongFunction();\n" 15289 "int j = 2;", 15290 Alignment); 15291 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15292 verifyFormat("int i = 1;\n" 15293 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15294 " someLooooooooooooooooongFunction();\n" 15295 "int j = 2;", 15296 Alignment); 15297 15298 verifyFormat("auto lambda = []() {\n" 15299 " auto i = 0;\n" 15300 " return 0;\n" 15301 "};\n" 15302 "int i = 0;\n" 15303 "auto v = type{\n" 15304 " i = 1, //\n" 15305 " (i = 2), //\n" 15306 " i = 3 //\n" 15307 "};", 15308 Alignment); 15309 15310 verifyFormat( 15311 "int i = 1;\n" 15312 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15313 " loooooooooooooooooooooongParameterB);\n" 15314 "int j = 2;", 15315 Alignment); 15316 15317 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15318 " typename B = very_long_type_name_1,\n" 15319 " typename T_2 = very_long_type_name_2>\n" 15320 "auto foo() {}\n", 15321 Alignment); 15322 verifyFormat("int a, b = 1;\n" 15323 "int c = 2;\n" 15324 "int dd = 3;\n", 15325 Alignment); 15326 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15327 "float b[1][] = {{3.f}};\n", 15328 Alignment); 15329 verifyFormat("for (int i = 0; i < 1; i++)\n" 15330 " int x = 1;\n", 15331 Alignment); 15332 verifyFormat("for (i = 0; i < 1; i++)\n" 15333 " x = 1;\n" 15334 "y = 1;\n", 15335 Alignment); 15336 15337 Alignment.ReflowComments = true; 15338 Alignment.ColumnLimit = 50; 15339 EXPECT_EQ("int x = 0;\n" 15340 "int yy = 1; /// specificlennospace\n" 15341 "int zzz = 2;\n", 15342 format("int x = 0;\n" 15343 "int yy = 1; ///specificlennospace\n" 15344 "int zzz = 2;\n", 15345 Alignment)); 15346 } 15347 15348 TEST_F(FormatTest, AlignConsecutiveAssignments) { 15349 FormatStyle Alignment = getLLVMStyle(); 15350 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15351 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15352 verifyFormat("int a = 5;\n" 15353 "int oneTwoThree = 123;", 15354 Alignment); 15355 verifyFormat("int a = 5;\n" 15356 "int oneTwoThree = 123;", 15357 Alignment); 15358 15359 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15360 verifyFormat("int a = 5;\n" 15361 "int oneTwoThree = 123;", 15362 Alignment); 15363 verifyFormat("int a = method();\n" 15364 "int oneTwoThree = 133;", 15365 Alignment); 15366 verifyFormat("a &= 5;\n" 15367 "bcd *= 5;\n" 15368 "ghtyf += 5;\n" 15369 "dvfvdb -= 5;\n" 15370 "a /= 5;\n" 15371 "vdsvsv %= 5;\n" 15372 "sfdbddfbdfbb ^= 5;\n" 15373 "dvsdsv |= 5;\n" 15374 "int dsvvdvsdvvv = 123;", 15375 Alignment); 15376 verifyFormat("int i = 1, j = 10;\n" 15377 "something = 2000;", 15378 Alignment); 15379 verifyFormat("something = 2000;\n" 15380 "int i = 1, j = 10;\n", 15381 Alignment); 15382 verifyFormat("something = 2000;\n" 15383 "another = 911;\n" 15384 "int i = 1, j = 10;\n" 15385 "oneMore = 1;\n" 15386 "i = 2;", 15387 Alignment); 15388 verifyFormat("int a = 5;\n" 15389 "int one = 1;\n" 15390 "method();\n" 15391 "int oneTwoThree = 123;\n" 15392 "int oneTwo = 12;", 15393 Alignment); 15394 verifyFormat("int oneTwoThree = 123;\n" 15395 "int oneTwo = 12;\n" 15396 "method();\n", 15397 Alignment); 15398 verifyFormat("int oneTwoThree = 123; // comment\n" 15399 "int oneTwo = 12; // comment", 15400 Alignment); 15401 15402 // Bug 25167 15403 /* Uncomment when fixed 15404 verifyFormat("#if A\n" 15405 "#else\n" 15406 "int aaaaaaaa = 12;\n" 15407 "#endif\n" 15408 "#if B\n" 15409 "#else\n" 15410 "int a = 12;\n" 15411 "#endif\n", 15412 Alignment); 15413 verifyFormat("enum foo {\n" 15414 "#if A\n" 15415 "#else\n" 15416 " aaaaaaaa = 12;\n" 15417 "#endif\n" 15418 "#if B\n" 15419 "#else\n" 15420 " a = 12;\n" 15421 "#endif\n" 15422 "};\n", 15423 Alignment); 15424 */ 15425 15426 EXPECT_EQ("int a = 5;\n" 15427 "\n" 15428 "int oneTwoThree = 123;", 15429 format("int a = 5;\n" 15430 "\n" 15431 "int oneTwoThree= 123;", 15432 Alignment)); 15433 EXPECT_EQ("int a = 5;\n" 15434 "int one = 1;\n" 15435 "\n" 15436 "int oneTwoThree = 123;", 15437 format("int a = 5;\n" 15438 "int one = 1;\n" 15439 "\n" 15440 "int oneTwoThree = 123;", 15441 Alignment)); 15442 EXPECT_EQ("int a = 5;\n" 15443 "int one = 1;\n" 15444 "\n" 15445 "int oneTwoThree = 123;\n" 15446 "int oneTwo = 12;", 15447 format("int a = 5;\n" 15448 "int one = 1;\n" 15449 "\n" 15450 "int oneTwoThree = 123;\n" 15451 "int oneTwo = 12;", 15452 Alignment)); 15453 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15454 verifyFormat("#define A \\\n" 15455 " int aaaa = 12; \\\n" 15456 " int b = 23; \\\n" 15457 " int ccc = 234; \\\n" 15458 " int dddddddddd = 2345;", 15459 Alignment); 15460 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15461 verifyFormat("#define A \\\n" 15462 " int aaaa = 12; \\\n" 15463 " int b = 23; \\\n" 15464 " int ccc = 234; \\\n" 15465 " int dddddddddd = 2345;", 15466 Alignment); 15467 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15468 verifyFormat("#define A " 15469 " \\\n" 15470 " int aaaa = 12; " 15471 " \\\n" 15472 " int b = 23; " 15473 " \\\n" 15474 " int ccc = 234; " 15475 " \\\n" 15476 " int dddddddddd = 2345;", 15477 Alignment); 15478 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15479 "k = 4, int l = 5,\n" 15480 " int m = 6) {\n" 15481 " int j = 10;\n" 15482 " otherThing = 1;\n" 15483 "}", 15484 Alignment); 15485 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15486 " int i = 1;\n" 15487 " int j = 2;\n" 15488 " int big = 10000;\n" 15489 "}", 15490 Alignment); 15491 verifyFormat("class C {\n" 15492 "public:\n" 15493 " int i = 1;\n" 15494 " virtual void f() = 0;\n" 15495 "};", 15496 Alignment); 15497 verifyFormat("int i = 1;\n" 15498 "if (SomeType t = getSomething()) {\n" 15499 "}\n" 15500 "int j = 2;\n" 15501 "int big = 10000;", 15502 Alignment); 15503 verifyFormat("int j = 7;\n" 15504 "for (int k = 0; k < N; ++k) {\n" 15505 "}\n" 15506 "int j = 2;\n" 15507 "int big = 10000;\n" 15508 "}", 15509 Alignment); 15510 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15511 verifyFormat("int i = 1;\n" 15512 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15513 " = someLooooooooooooooooongFunction();\n" 15514 "int j = 2;", 15515 Alignment); 15516 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15517 verifyFormat("int i = 1;\n" 15518 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15519 " someLooooooooooooooooongFunction();\n" 15520 "int j = 2;", 15521 Alignment); 15522 15523 verifyFormat("auto lambda = []() {\n" 15524 " auto i = 0;\n" 15525 " return 0;\n" 15526 "};\n" 15527 "int i = 0;\n" 15528 "auto v = type{\n" 15529 " i = 1, //\n" 15530 " (i = 2), //\n" 15531 " i = 3 //\n" 15532 "};", 15533 Alignment); 15534 15535 verifyFormat( 15536 "int i = 1;\n" 15537 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15538 " loooooooooooooooooooooongParameterB);\n" 15539 "int j = 2;", 15540 Alignment); 15541 15542 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15543 " typename B = very_long_type_name_1,\n" 15544 " typename T_2 = very_long_type_name_2>\n" 15545 "auto foo() {}\n", 15546 Alignment); 15547 verifyFormat("int a, b = 1;\n" 15548 "int c = 2;\n" 15549 "int dd = 3;\n", 15550 Alignment); 15551 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15552 "float b[1][] = {{3.f}};\n", 15553 Alignment); 15554 verifyFormat("for (int i = 0; i < 1; i++)\n" 15555 " int x = 1;\n", 15556 Alignment); 15557 verifyFormat("for (i = 0; i < 1; i++)\n" 15558 " x = 1;\n" 15559 "y = 1;\n", 15560 Alignment); 15561 15562 Alignment.ReflowComments = true; 15563 Alignment.ColumnLimit = 50; 15564 EXPECT_EQ("int x = 0;\n" 15565 "int yy = 1; /// specificlennospace\n" 15566 "int zzz = 2;\n", 15567 format("int x = 0;\n" 15568 "int yy = 1; ///specificlennospace\n" 15569 "int zzz = 2;\n", 15570 Alignment)); 15571 } 15572 15573 TEST_F(FormatTest, AlignConsecutiveBitFields) { 15574 FormatStyle Alignment = getLLVMStyle(); 15575 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 15576 verifyFormat("int const a : 5;\n" 15577 "int oneTwoThree : 23;", 15578 Alignment); 15579 15580 // Initializers are allowed starting with c++2a 15581 verifyFormat("int const a : 5 = 1;\n" 15582 "int oneTwoThree : 23 = 0;", 15583 Alignment); 15584 15585 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15586 verifyFormat("int const a : 5;\n" 15587 "int oneTwoThree : 23;", 15588 Alignment); 15589 15590 verifyFormat("int const a : 5; // comment\n" 15591 "int oneTwoThree : 23; // comment", 15592 Alignment); 15593 15594 verifyFormat("int const a : 5 = 1;\n" 15595 "int oneTwoThree : 23 = 0;", 15596 Alignment); 15597 15598 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15599 verifyFormat("int const a : 5 = 1;\n" 15600 "int oneTwoThree : 23 = 0;", 15601 Alignment); 15602 verifyFormat("int const a : 5 = {1};\n" 15603 "int oneTwoThree : 23 = 0;", 15604 Alignment); 15605 15606 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 15607 verifyFormat("int const a :5;\n" 15608 "int oneTwoThree:23;", 15609 Alignment); 15610 15611 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 15612 verifyFormat("int const a :5;\n" 15613 "int oneTwoThree :23;", 15614 Alignment); 15615 15616 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 15617 verifyFormat("int const a : 5;\n" 15618 "int oneTwoThree: 23;", 15619 Alignment); 15620 15621 // Known limitations: ':' is only recognized as a bitfield colon when 15622 // followed by a number. 15623 /* 15624 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 15625 "int a : 5;", 15626 Alignment); 15627 */ 15628 } 15629 15630 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 15631 FormatStyle Alignment = getLLVMStyle(); 15632 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15633 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 15634 Alignment.PointerAlignment = FormatStyle::PAS_Right; 15635 verifyFormat("float const a = 5;\n" 15636 "int oneTwoThree = 123;", 15637 Alignment); 15638 verifyFormat("int a = 5;\n" 15639 "float const oneTwoThree = 123;", 15640 Alignment); 15641 15642 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15643 verifyFormat("float const a = 5;\n" 15644 "int oneTwoThree = 123;", 15645 Alignment); 15646 verifyFormat("int a = method();\n" 15647 "float const oneTwoThree = 133;", 15648 Alignment); 15649 verifyFormat("int i = 1, j = 10;\n" 15650 "something = 2000;", 15651 Alignment); 15652 verifyFormat("something = 2000;\n" 15653 "int i = 1, j = 10;\n", 15654 Alignment); 15655 verifyFormat("float something = 2000;\n" 15656 "double another = 911;\n" 15657 "int i = 1, j = 10;\n" 15658 "const int *oneMore = 1;\n" 15659 "unsigned i = 2;", 15660 Alignment); 15661 verifyFormat("float a = 5;\n" 15662 "int one = 1;\n" 15663 "method();\n" 15664 "const double oneTwoThree = 123;\n" 15665 "const unsigned int oneTwo = 12;", 15666 Alignment); 15667 verifyFormat("int oneTwoThree{0}; // comment\n" 15668 "unsigned oneTwo; // comment", 15669 Alignment); 15670 verifyFormat("unsigned int *a;\n" 15671 "int *b;\n" 15672 "unsigned int Const *c;\n" 15673 "unsigned int const *d;\n" 15674 "unsigned int Const &e;\n" 15675 "unsigned int const &f;", 15676 Alignment); 15677 verifyFormat("Const unsigned int *c;\n" 15678 "const unsigned int *d;\n" 15679 "Const unsigned int &e;\n" 15680 "const unsigned int &f;\n" 15681 "const unsigned g;\n" 15682 "Const unsigned h;", 15683 Alignment); 15684 EXPECT_EQ("float const a = 5;\n" 15685 "\n" 15686 "int oneTwoThree = 123;", 15687 format("float const a = 5;\n" 15688 "\n" 15689 "int oneTwoThree= 123;", 15690 Alignment)); 15691 EXPECT_EQ("float a = 5;\n" 15692 "int one = 1;\n" 15693 "\n" 15694 "unsigned oneTwoThree = 123;", 15695 format("float a = 5;\n" 15696 "int one = 1;\n" 15697 "\n" 15698 "unsigned oneTwoThree = 123;", 15699 Alignment)); 15700 EXPECT_EQ("float a = 5;\n" 15701 "int one = 1;\n" 15702 "\n" 15703 "unsigned oneTwoThree = 123;\n" 15704 "int oneTwo = 12;", 15705 format("float a = 5;\n" 15706 "int one = 1;\n" 15707 "\n" 15708 "unsigned oneTwoThree = 123;\n" 15709 "int oneTwo = 12;", 15710 Alignment)); 15711 // Function prototype alignment 15712 verifyFormat("int a();\n" 15713 "double b();", 15714 Alignment); 15715 verifyFormat("int a(int x);\n" 15716 "double b();", 15717 Alignment); 15718 unsigned OldColumnLimit = Alignment.ColumnLimit; 15719 // We need to set ColumnLimit to zero, in order to stress nested alignments, 15720 // otherwise the function parameters will be re-flowed onto a single line. 15721 Alignment.ColumnLimit = 0; 15722 EXPECT_EQ("int a(int x,\n" 15723 " float y);\n" 15724 "double b(int x,\n" 15725 " double y);", 15726 format("int a(int x,\n" 15727 " float y);\n" 15728 "double b(int x,\n" 15729 " double y);", 15730 Alignment)); 15731 // This ensures that function parameters of function declarations are 15732 // correctly indented when their owning functions are indented. 15733 // The failure case here is for 'double y' to not be indented enough. 15734 EXPECT_EQ("double a(int x);\n" 15735 "int b(int y,\n" 15736 " double z);", 15737 format("double a(int x);\n" 15738 "int b(int y,\n" 15739 " double z);", 15740 Alignment)); 15741 // Set ColumnLimit low so that we induce wrapping immediately after 15742 // the function name and opening paren. 15743 Alignment.ColumnLimit = 13; 15744 verifyFormat("int function(\n" 15745 " int x,\n" 15746 " bool y);", 15747 Alignment); 15748 Alignment.ColumnLimit = OldColumnLimit; 15749 // Ensure function pointers don't screw up recursive alignment 15750 verifyFormat("int a(int x, void (*fp)(int y));\n" 15751 "double b();", 15752 Alignment); 15753 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15754 // Ensure recursive alignment is broken by function braces, so that the 15755 // "a = 1" does not align with subsequent assignments inside the function 15756 // body. 15757 verifyFormat("int func(int a = 1) {\n" 15758 " int b = 2;\n" 15759 " int cc = 3;\n" 15760 "}", 15761 Alignment); 15762 verifyFormat("float something = 2000;\n" 15763 "double another = 911;\n" 15764 "int i = 1, j = 10;\n" 15765 "const int *oneMore = 1;\n" 15766 "unsigned i = 2;", 15767 Alignment); 15768 verifyFormat("int oneTwoThree = {0}; // comment\n" 15769 "unsigned oneTwo = 0; // comment", 15770 Alignment); 15771 // Make sure that scope is correctly tracked, in the absence of braces 15772 verifyFormat("for (int i = 0; i < n; i++)\n" 15773 " j = i;\n" 15774 "double x = 1;\n", 15775 Alignment); 15776 verifyFormat("if (int i = 0)\n" 15777 " j = i;\n" 15778 "double x = 1;\n", 15779 Alignment); 15780 // Ensure operator[] and operator() are comprehended 15781 verifyFormat("struct test {\n" 15782 " long long int foo();\n" 15783 " int operator[](int a);\n" 15784 " double bar();\n" 15785 "};\n", 15786 Alignment); 15787 verifyFormat("struct test {\n" 15788 " long long int foo();\n" 15789 " int operator()(int a);\n" 15790 " double bar();\n" 15791 "};\n", 15792 Alignment); 15793 15794 // PAS_Right 15795 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15796 " int const i = 1;\n" 15797 " int *j = 2;\n" 15798 " int big = 10000;\n" 15799 "\n" 15800 " unsigned oneTwoThree = 123;\n" 15801 " int oneTwo = 12;\n" 15802 " method();\n" 15803 " float k = 2;\n" 15804 " int ll = 10000;\n" 15805 "}", 15806 format("void SomeFunction(int parameter= 0) {\n" 15807 " int const i= 1;\n" 15808 " int *j=2;\n" 15809 " int big = 10000;\n" 15810 "\n" 15811 "unsigned oneTwoThree =123;\n" 15812 "int oneTwo = 12;\n" 15813 " method();\n" 15814 "float k= 2;\n" 15815 "int ll=10000;\n" 15816 "}", 15817 Alignment)); 15818 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15819 " int const i = 1;\n" 15820 " int **j = 2, ***k;\n" 15821 " int &k = i;\n" 15822 " int &&l = i + j;\n" 15823 " int big = 10000;\n" 15824 "\n" 15825 " unsigned oneTwoThree = 123;\n" 15826 " int oneTwo = 12;\n" 15827 " method();\n" 15828 " float k = 2;\n" 15829 " int ll = 10000;\n" 15830 "}", 15831 format("void SomeFunction(int parameter= 0) {\n" 15832 " int const i= 1;\n" 15833 " int **j=2,***k;\n" 15834 "int &k=i;\n" 15835 "int &&l=i+j;\n" 15836 " int big = 10000;\n" 15837 "\n" 15838 "unsigned oneTwoThree =123;\n" 15839 "int oneTwo = 12;\n" 15840 " method();\n" 15841 "float k= 2;\n" 15842 "int ll=10000;\n" 15843 "}", 15844 Alignment)); 15845 // variables are aligned at their name, pointers are at the right most 15846 // position 15847 verifyFormat("int *a;\n" 15848 "int **b;\n" 15849 "int ***c;\n" 15850 "int foobar;\n", 15851 Alignment); 15852 15853 // PAS_Left 15854 FormatStyle AlignmentLeft = Alignment; 15855 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 15856 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15857 " int const i = 1;\n" 15858 " int* j = 2;\n" 15859 " int big = 10000;\n" 15860 "\n" 15861 " unsigned oneTwoThree = 123;\n" 15862 " int oneTwo = 12;\n" 15863 " method();\n" 15864 " float k = 2;\n" 15865 " int ll = 10000;\n" 15866 "}", 15867 format("void SomeFunction(int parameter= 0) {\n" 15868 " int const i= 1;\n" 15869 " int *j=2;\n" 15870 " int big = 10000;\n" 15871 "\n" 15872 "unsigned oneTwoThree =123;\n" 15873 "int oneTwo = 12;\n" 15874 " method();\n" 15875 "float k= 2;\n" 15876 "int ll=10000;\n" 15877 "}", 15878 AlignmentLeft)); 15879 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15880 " int const i = 1;\n" 15881 " int** j = 2;\n" 15882 " int& k = i;\n" 15883 " int&& l = i + j;\n" 15884 " int big = 10000;\n" 15885 "\n" 15886 " unsigned oneTwoThree = 123;\n" 15887 " int oneTwo = 12;\n" 15888 " method();\n" 15889 " float k = 2;\n" 15890 " int ll = 10000;\n" 15891 "}", 15892 format("void SomeFunction(int parameter= 0) {\n" 15893 " int const i= 1;\n" 15894 " int **j=2;\n" 15895 "int &k=i;\n" 15896 "int &&l=i+j;\n" 15897 " int big = 10000;\n" 15898 "\n" 15899 "unsigned oneTwoThree =123;\n" 15900 "int oneTwo = 12;\n" 15901 " method();\n" 15902 "float k= 2;\n" 15903 "int ll=10000;\n" 15904 "}", 15905 AlignmentLeft)); 15906 // variables are aligned at their name, pointers are at the left most position 15907 verifyFormat("int* a;\n" 15908 "int** b;\n" 15909 "int*** c;\n" 15910 "int foobar;\n", 15911 AlignmentLeft); 15912 15913 // PAS_Middle 15914 FormatStyle AlignmentMiddle = Alignment; 15915 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 15916 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15917 " int const i = 1;\n" 15918 " int * j = 2;\n" 15919 " int big = 10000;\n" 15920 "\n" 15921 " unsigned oneTwoThree = 123;\n" 15922 " int oneTwo = 12;\n" 15923 " method();\n" 15924 " float k = 2;\n" 15925 " int ll = 10000;\n" 15926 "}", 15927 format("void SomeFunction(int parameter= 0) {\n" 15928 " int const i= 1;\n" 15929 " int *j=2;\n" 15930 " int big = 10000;\n" 15931 "\n" 15932 "unsigned oneTwoThree =123;\n" 15933 "int oneTwo = 12;\n" 15934 " method();\n" 15935 "float k= 2;\n" 15936 "int ll=10000;\n" 15937 "}", 15938 AlignmentMiddle)); 15939 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15940 " int const i = 1;\n" 15941 " int ** j = 2, ***k;\n" 15942 " int & k = i;\n" 15943 " int && l = i + j;\n" 15944 " int big = 10000;\n" 15945 "\n" 15946 " unsigned oneTwoThree = 123;\n" 15947 " int oneTwo = 12;\n" 15948 " method();\n" 15949 " float k = 2;\n" 15950 " int ll = 10000;\n" 15951 "}", 15952 format("void SomeFunction(int parameter= 0) {\n" 15953 " int const i= 1;\n" 15954 " int **j=2,***k;\n" 15955 "int &k=i;\n" 15956 "int &&l=i+j;\n" 15957 " int big = 10000;\n" 15958 "\n" 15959 "unsigned oneTwoThree =123;\n" 15960 "int oneTwo = 12;\n" 15961 " method();\n" 15962 "float k= 2;\n" 15963 "int ll=10000;\n" 15964 "}", 15965 AlignmentMiddle)); 15966 // variables are aligned at their name, pointers are in the middle 15967 verifyFormat("int * a;\n" 15968 "int * b;\n" 15969 "int *** c;\n" 15970 "int foobar;\n", 15971 AlignmentMiddle); 15972 15973 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15974 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15975 verifyFormat("#define A \\\n" 15976 " int aaaa = 12; \\\n" 15977 " float b = 23; \\\n" 15978 " const int ccc = 234; \\\n" 15979 " unsigned dddddddddd = 2345;", 15980 Alignment); 15981 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15982 verifyFormat("#define A \\\n" 15983 " int aaaa = 12; \\\n" 15984 " float b = 23; \\\n" 15985 " const int ccc = 234; \\\n" 15986 " unsigned dddddddddd = 2345;", 15987 Alignment); 15988 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15989 Alignment.ColumnLimit = 30; 15990 verifyFormat("#define A \\\n" 15991 " int aaaa = 12; \\\n" 15992 " float b = 23; \\\n" 15993 " const int ccc = 234; \\\n" 15994 " int dddddddddd = 2345;", 15995 Alignment); 15996 Alignment.ColumnLimit = 80; 15997 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15998 "k = 4, int l = 5,\n" 15999 " int m = 6) {\n" 16000 " const int j = 10;\n" 16001 " otherThing = 1;\n" 16002 "}", 16003 Alignment); 16004 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16005 " int const i = 1;\n" 16006 " int *j = 2;\n" 16007 " int big = 10000;\n" 16008 "}", 16009 Alignment); 16010 verifyFormat("class C {\n" 16011 "public:\n" 16012 " int i = 1;\n" 16013 " virtual void f() = 0;\n" 16014 "};", 16015 Alignment); 16016 verifyFormat("float i = 1;\n" 16017 "if (SomeType t = getSomething()) {\n" 16018 "}\n" 16019 "const unsigned j = 2;\n" 16020 "int big = 10000;", 16021 Alignment); 16022 verifyFormat("float j = 7;\n" 16023 "for (int k = 0; k < N; ++k) {\n" 16024 "}\n" 16025 "unsigned j = 2;\n" 16026 "int big = 10000;\n" 16027 "}", 16028 Alignment); 16029 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16030 verifyFormat("float i = 1;\n" 16031 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16032 " = someLooooooooooooooooongFunction();\n" 16033 "int j = 2;", 16034 Alignment); 16035 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16036 verifyFormat("int i = 1;\n" 16037 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16038 " someLooooooooooooooooongFunction();\n" 16039 "int j = 2;", 16040 Alignment); 16041 16042 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16043 verifyFormat("auto lambda = []() {\n" 16044 " auto ii = 0;\n" 16045 " float j = 0;\n" 16046 " return 0;\n" 16047 "};\n" 16048 "int i = 0;\n" 16049 "float i2 = 0;\n" 16050 "auto v = type{\n" 16051 " i = 1, //\n" 16052 " (i = 2), //\n" 16053 " i = 3 //\n" 16054 "};", 16055 Alignment); 16056 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16057 16058 verifyFormat( 16059 "int i = 1;\n" 16060 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16061 " loooooooooooooooooooooongParameterB);\n" 16062 "int j = 2;", 16063 Alignment); 16064 16065 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 16066 // We expect declarations and assignments to align, as long as it doesn't 16067 // exceed the column limit, starting a new alignment sequence whenever it 16068 // happens. 16069 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16070 Alignment.ColumnLimit = 30; 16071 verifyFormat("float ii = 1;\n" 16072 "unsigned j = 2;\n" 16073 "int someVerylongVariable = 1;\n" 16074 "AnotherLongType ll = 123456;\n" 16075 "VeryVeryLongType k = 2;\n" 16076 "int myvar = 1;", 16077 Alignment); 16078 Alignment.ColumnLimit = 80; 16079 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16080 16081 verifyFormat( 16082 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 16083 " typename LongType, typename B>\n" 16084 "auto foo() {}\n", 16085 Alignment); 16086 verifyFormat("float a, b = 1;\n" 16087 "int c = 2;\n" 16088 "int dd = 3;\n", 16089 Alignment); 16090 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16091 "float b[1][] = {{3.f}};\n", 16092 Alignment); 16093 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16094 verifyFormat("float a, b = 1;\n" 16095 "int c = 2;\n" 16096 "int dd = 3;\n", 16097 Alignment); 16098 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16099 "float b[1][] = {{3.f}};\n", 16100 Alignment); 16101 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16102 16103 Alignment.ColumnLimit = 30; 16104 Alignment.BinPackParameters = false; 16105 verifyFormat("void foo(float a,\n" 16106 " float b,\n" 16107 " int c,\n" 16108 " uint32_t *d) {\n" 16109 " int *e = 0;\n" 16110 " float f = 0;\n" 16111 " double g = 0;\n" 16112 "}\n" 16113 "void bar(ino_t a,\n" 16114 " int b,\n" 16115 " uint32_t *c,\n" 16116 " bool d) {}\n", 16117 Alignment); 16118 Alignment.BinPackParameters = true; 16119 Alignment.ColumnLimit = 80; 16120 16121 // Bug 33507 16122 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16123 verifyFormat( 16124 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 16125 " static const Version verVs2017;\n" 16126 " return true;\n" 16127 "});\n", 16128 Alignment); 16129 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16130 16131 // See llvm.org/PR35641 16132 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16133 verifyFormat("int func() { //\n" 16134 " int b;\n" 16135 " unsigned c;\n" 16136 "}", 16137 Alignment); 16138 16139 // See PR37175 16140 FormatStyle Style = getMozillaStyle(); 16141 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16142 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 16143 "foo(int a);", 16144 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 16145 16146 Alignment.PointerAlignment = FormatStyle::PAS_Left; 16147 verifyFormat("unsigned int* a;\n" 16148 "int* b;\n" 16149 "unsigned int Const* c;\n" 16150 "unsigned int const* d;\n" 16151 "unsigned int Const& e;\n" 16152 "unsigned int const& f;", 16153 Alignment); 16154 verifyFormat("Const unsigned int* c;\n" 16155 "const unsigned int* d;\n" 16156 "Const unsigned int& e;\n" 16157 "const unsigned int& f;\n" 16158 "const unsigned g;\n" 16159 "Const unsigned h;", 16160 Alignment); 16161 16162 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16163 verifyFormat("unsigned int * a;\n" 16164 "int * b;\n" 16165 "unsigned int Const * c;\n" 16166 "unsigned int const * d;\n" 16167 "unsigned int Const & e;\n" 16168 "unsigned int const & f;", 16169 Alignment); 16170 verifyFormat("Const unsigned int * c;\n" 16171 "const unsigned int * d;\n" 16172 "Const unsigned int & e;\n" 16173 "const unsigned int & f;\n" 16174 "const unsigned g;\n" 16175 "Const unsigned h;", 16176 Alignment); 16177 } 16178 16179 TEST_F(FormatTest, AlignWithLineBreaks) { 16180 auto Style = getLLVMStyleWithColumns(120); 16181 16182 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 16183 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 16184 verifyFormat("void foo() {\n" 16185 " int myVar = 5;\n" 16186 " double x = 3.14;\n" 16187 " auto str = \"Hello \"\n" 16188 " \"World\";\n" 16189 " auto s = \"Hello \"\n" 16190 " \"Again\";\n" 16191 "}", 16192 Style); 16193 16194 // clang-format off 16195 verifyFormat("void foo() {\n" 16196 " const int capacityBefore = Entries.capacity();\n" 16197 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16198 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16199 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16200 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16201 "}", 16202 Style); 16203 // clang-format on 16204 16205 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16206 verifyFormat("void foo() {\n" 16207 " int myVar = 5;\n" 16208 " double x = 3.14;\n" 16209 " auto str = \"Hello \"\n" 16210 " \"World\";\n" 16211 " auto s = \"Hello \"\n" 16212 " \"Again\";\n" 16213 "}", 16214 Style); 16215 16216 // clang-format off 16217 verifyFormat("void foo() {\n" 16218 " const int capacityBefore = Entries.capacity();\n" 16219 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16220 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16221 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16222 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16223 "}", 16224 Style); 16225 // clang-format on 16226 16227 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16228 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16229 verifyFormat("void foo() {\n" 16230 " int myVar = 5;\n" 16231 " double x = 3.14;\n" 16232 " auto str = \"Hello \"\n" 16233 " \"World\";\n" 16234 " auto s = \"Hello \"\n" 16235 " \"Again\";\n" 16236 "}", 16237 Style); 16238 16239 // clang-format off 16240 verifyFormat("void foo() {\n" 16241 " const int capacityBefore = Entries.capacity();\n" 16242 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16243 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16244 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16245 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16246 "}", 16247 Style); 16248 // clang-format on 16249 16250 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16251 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16252 16253 verifyFormat("void foo() {\n" 16254 " int myVar = 5;\n" 16255 " double x = 3.14;\n" 16256 " auto str = \"Hello \"\n" 16257 " \"World\";\n" 16258 " auto s = \"Hello \"\n" 16259 " \"Again\";\n" 16260 "}", 16261 Style); 16262 16263 // clang-format off 16264 verifyFormat("void foo() {\n" 16265 " const int capacityBefore = Entries.capacity();\n" 16266 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16267 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16268 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16269 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16270 "}", 16271 Style); 16272 // clang-format on 16273 } 16274 16275 TEST_F(FormatTest, LinuxBraceBreaking) { 16276 FormatStyle LinuxBraceStyle = getLLVMStyle(); 16277 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 16278 verifyFormat("namespace a\n" 16279 "{\n" 16280 "class A\n" 16281 "{\n" 16282 " void f()\n" 16283 " {\n" 16284 " if (true) {\n" 16285 " a();\n" 16286 " b();\n" 16287 " } else {\n" 16288 " a();\n" 16289 " }\n" 16290 " }\n" 16291 " void g() { return; }\n" 16292 "};\n" 16293 "struct B {\n" 16294 " int x;\n" 16295 "};\n" 16296 "} // namespace a\n", 16297 LinuxBraceStyle); 16298 verifyFormat("enum X {\n" 16299 " Y = 0,\n" 16300 "}\n", 16301 LinuxBraceStyle); 16302 verifyFormat("struct S {\n" 16303 " int Type;\n" 16304 " union {\n" 16305 " int x;\n" 16306 " double y;\n" 16307 " } Value;\n" 16308 " class C\n" 16309 " {\n" 16310 " MyFavoriteType Value;\n" 16311 " } Class;\n" 16312 "}\n", 16313 LinuxBraceStyle); 16314 } 16315 16316 TEST_F(FormatTest, MozillaBraceBreaking) { 16317 FormatStyle MozillaBraceStyle = getLLVMStyle(); 16318 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 16319 MozillaBraceStyle.FixNamespaceComments = false; 16320 verifyFormat("namespace a {\n" 16321 "class A\n" 16322 "{\n" 16323 " void f()\n" 16324 " {\n" 16325 " if (true) {\n" 16326 " a();\n" 16327 " b();\n" 16328 " }\n" 16329 " }\n" 16330 " void g() { return; }\n" 16331 "};\n" 16332 "enum E\n" 16333 "{\n" 16334 " A,\n" 16335 " // foo\n" 16336 " B,\n" 16337 " C\n" 16338 "};\n" 16339 "struct B\n" 16340 "{\n" 16341 " int x;\n" 16342 "};\n" 16343 "}\n", 16344 MozillaBraceStyle); 16345 verifyFormat("struct S\n" 16346 "{\n" 16347 " int Type;\n" 16348 " union\n" 16349 " {\n" 16350 " int x;\n" 16351 " double y;\n" 16352 " } Value;\n" 16353 " class C\n" 16354 " {\n" 16355 " MyFavoriteType Value;\n" 16356 " } Class;\n" 16357 "}\n", 16358 MozillaBraceStyle); 16359 } 16360 16361 TEST_F(FormatTest, StroustrupBraceBreaking) { 16362 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 16363 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 16364 verifyFormat("namespace a {\n" 16365 "class A {\n" 16366 " void f()\n" 16367 " {\n" 16368 " if (true) {\n" 16369 " a();\n" 16370 " b();\n" 16371 " }\n" 16372 " }\n" 16373 " void g() { return; }\n" 16374 "};\n" 16375 "struct B {\n" 16376 " int x;\n" 16377 "};\n" 16378 "} // namespace a\n", 16379 StroustrupBraceStyle); 16380 16381 verifyFormat("void foo()\n" 16382 "{\n" 16383 " if (a) {\n" 16384 " a();\n" 16385 " }\n" 16386 " else {\n" 16387 " b();\n" 16388 " }\n" 16389 "}\n", 16390 StroustrupBraceStyle); 16391 16392 verifyFormat("#ifdef _DEBUG\n" 16393 "int foo(int i = 0)\n" 16394 "#else\n" 16395 "int foo(int i = 5)\n" 16396 "#endif\n" 16397 "{\n" 16398 " return i;\n" 16399 "}", 16400 StroustrupBraceStyle); 16401 16402 verifyFormat("void foo() {}\n" 16403 "void bar()\n" 16404 "#ifdef _DEBUG\n" 16405 "{\n" 16406 " foo();\n" 16407 "}\n" 16408 "#else\n" 16409 "{\n" 16410 "}\n" 16411 "#endif", 16412 StroustrupBraceStyle); 16413 16414 verifyFormat("void foobar() { int i = 5; }\n" 16415 "#ifdef _DEBUG\n" 16416 "void bar() {}\n" 16417 "#else\n" 16418 "void bar() { foobar(); }\n" 16419 "#endif", 16420 StroustrupBraceStyle); 16421 } 16422 16423 TEST_F(FormatTest, AllmanBraceBreaking) { 16424 FormatStyle AllmanBraceStyle = getLLVMStyle(); 16425 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 16426 16427 EXPECT_EQ("namespace a\n" 16428 "{\n" 16429 "void f();\n" 16430 "void g();\n" 16431 "} // namespace a\n", 16432 format("namespace a\n" 16433 "{\n" 16434 "void f();\n" 16435 "void g();\n" 16436 "}\n", 16437 AllmanBraceStyle)); 16438 16439 verifyFormat("namespace a\n" 16440 "{\n" 16441 "class A\n" 16442 "{\n" 16443 " void f()\n" 16444 " {\n" 16445 " if (true)\n" 16446 " {\n" 16447 " a();\n" 16448 " b();\n" 16449 " }\n" 16450 " }\n" 16451 " void g() { return; }\n" 16452 "};\n" 16453 "struct B\n" 16454 "{\n" 16455 " int x;\n" 16456 "};\n" 16457 "union C\n" 16458 "{\n" 16459 "};\n" 16460 "} // namespace a", 16461 AllmanBraceStyle); 16462 16463 verifyFormat("void f()\n" 16464 "{\n" 16465 " if (true)\n" 16466 " {\n" 16467 " a();\n" 16468 " }\n" 16469 " else if (false)\n" 16470 " {\n" 16471 " b();\n" 16472 " }\n" 16473 " else\n" 16474 " {\n" 16475 " c();\n" 16476 " }\n" 16477 "}\n", 16478 AllmanBraceStyle); 16479 16480 verifyFormat("void f()\n" 16481 "{\n" 16482 " for (int i = 0; i < 10; ++i)\n" 16483 " {\n" 16484 " a();\n" 16485 " }\n" 16486 " while (false)\n" 16487 " {\n" 16488 " b();\n" 16489 " }\n" 16490 " do\n" 16491 " {\n" 16492 " c();\n" 16493 " } while (false)\n" 16494 "}\n", 16495 AllmanBraceStyle); 16496 16497 verifyFormat("void f(int a)\n" 16498 "{\n" 16499 " switch (a)\n" 16500 " {\n" 16501 " case 0:\n" 16502 " break;\n" 16503 " case 1:\n" 16504 " {\n" 16505 " break;\n" 16506 " }\n" 16507 " case 2:\n" 16508 " {\n" 16509 " }\n" 16510 " break;\n" 16511 " default:\n" 16512 " break;\n" 16513 " }\n" 16514 "}\n", 16515 AllmanBraceStyle); 16516 16517 verifyFormat("enum X\n" 16518 "{\n" 16519 " Y = 0,\n" 16520 "}\n", 16521 AllmanBraceStyle); 16522 verifyFormat("enum X\n" 16523 "{\n" 16524 " Y = 0\n" 16525 "}\n", 16526 AllmanBraceStyle); 16527 16528 verifyFormat("@interface BSApplicationController ()\n" 16529 "{\n" 16530 "@private\n" 16531 " id _extraIvar;\n" 16532 "}\n" 16533 "@end\n", 16534 AllmanBraceStyle); 16535 16536 verifyFormat("#ifdef _DEBUG\n" 16537 "int foo(int i = 0)\n" 16538 "#else\n" 16539 "int foo(int i = 5)\n" 16540 "#endif\n" 16541 "{\n" 16542 " return i;\n" 16543 "}", 16544 AllmanBraceStyle); 16545 16546 verifyFormat("void foo() {}\n" 16547 "void bar()\n" 16548 "#ifdef _DEBUG\n" 16549 "{\n" 16550 " foo();\n" 16551 "}\n" 16552 "#else\n" 16553 "{\n" 16554 "}\n" 16555 "#endif", 16556 AllmanBraceStyle); 16557 16558 verifyFormat("void foobar() { int i = 5; }\n" 16559 "#ifdef _DEBUG\n" 16560 "void bar() {}\n" 16561 "#else\n" 16562 "void bar() { foobar(); }\n" 16563 "#endif", 16564 AllmanBraceStyle); 16565 16566 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 16567 FormatStyle::SLS_All); 16568 16569 verifyFormat("[](int i) { return i + 2; };\n" 16570 "[](int i, int j)\n" 16571 "{\n" 16572 " auto x = i + j;\n" 16573 " auto y = i * j;\n" 16574 " return x ^ y;\n" 16575 "};\n" 16576 "void foo()\n" 16577 "{\n" 16578 " auto shortLambda = [](int i) { return i + 2; };\n" 16579 " auto longLambda = [](int i, int j)\n" 16580 " {\n" 16581 " auto x = i + j;\n" 16582 " auto y = i * j;\n" 16583 " return x ^ y;\n" 16584 " };\n" 16585 "}", 16586 AllmanBraceStyle); 16587 16588 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 16589 16590 verifyFormat("[](int i)\n" 16591 "{\n" 16592 " return i + 2;\n" 16593 "};\n" 16594 "[](int i, int j)\n" 16595 "{\n" 16596 " auto x = i + j;\n" 16597 " auto y = i * j;\n" 16598 " return x ^ y;\n" 16599 "};\n" 16600 "void foo()\n" 16601 "{\n" 16602 " auto shortLambda = [](int i)\n" 16603 " {\n" 16604 " return i + 2;\n" 16605 " };\n" 16606 " auto longLambda = [](int i, int j)\n" 16607 " {\n" 16608 " auto x = i + j;\n" 16609 " auto y = i * j;\n" 16610 " return x ^ y;\n" 16611 " };\n" 16612 "}", 16613 AllmanBraceStyle); 16614 16615 // Reset 16616 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 16617 16618 // This shouldn't affect ObjC blocks.. 16619 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 16620 " // ...\n" 16621 " int i;\n" 16622 "}];", 16623 AllmanBraceStyle); 16624 verifyFormat("void (^block)(void) = ^{\n" 16625 " // ...\n" 16626 " int i;\n" 16627 "};", 16628 AllmanBraceStyle); 16629 // .. or dict literals. 16630 verifyFormat("void f()\n" 16631 "{\n" 16632 " // ...\n" 16633 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 16634 "}", 16635 AllmanBraceStyle); 16636 verifyFormat("void f()\n" 16637 "{\n" 16638 " // ...\n" 16639 " [object someMethod:@{a : @\"b\"}];\n" 16640 "}", 16641 AllmanBraceStyle); 16642 verifyFormat("int f()\n" 16643 "{ // comment\n" 16644 " return 42;\n" 16645 "}", 16646 AllmanBraceStyle); 16647 16648 AllmanBraceStyle.ColumnLimit = 19; 16649 verifyFormat("void f() { int i; }", AllmanBraceStyle); 16650 AllmanBraceStyle.ColumnLimit = 18; 16651 verifyFormat("void f()\n" 16652 "{\n" 16653 " int i;\n" 16654 "}", 16655 AllmanBraceStyle); 16656 AllmanBraceStyle.ColumnLimit = 80; 16657 16658 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 16659 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 16660 FormatStyle::SIS_WithoutElse; 16661 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 16662 verifyFormat("void f(bool b)\n" 16663 "{\n" 16664 " if (b)\n" 16665 " {\n" 16666 " return;\n" 16667 " }\n" 16668 "}\n", 16669 BreakBeforeBraceShortIfs); 16670 verifyFormat("void f(bool b)\n" 16671 "{\n" 16672 " if constexpr (b)\n" 16673 " {\n" 16674 " return;\n" 16675 " }\n" 16676 "}\n", 16677 BreakBeforeBraceShortIfs); 16678 verifyFormat("void f(bool b)\n" 16679 "{\n" 16680 " if CONSTEXPR (b)\n" 16681 " {\n" 16682 " return;\n" 16683 " }\n" 16684 "}\n", 16685 BreakBeforeBraceShortIfs); 16686 verifyFormat("void f(bool b)\n" 16687 "{\n" 16688 " if (b) return;\n" 16689 "}\n", 16690 BreakBeforeBraceShortIfs); 16691 verifyFormat("void f(bool b)\n" 16692 "{\n" 16693 " if constexpr (b) return;\n" 16694 "}\n", 16695 BreakBeforeBraceShortIfs); 16696 verifyFormat("void f(bool b)\n" 16697 "{\n" 16698 " if CONSTEXPR (b) return;\n" 16699 "}\n", 16700 BreakBeforeBraceShortIfs); 16701 verifyFormat("void f(bool b)\n" 16702 "{\n" 16703 " while (b)\n" 16704 " {\n" 16705 " return;\n" 16706 " }\n" 16707 "}\n", 16708 BreakBeforeBraceShortIfs); 16709 } 16710 16711 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 16712 FormatStyle WhitesmithsBraceStyle = getLLVMStyle(); 16713 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 16714 16715 // Make a few changes to the style for testing purposes 16716 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 16717 FormatStyle::SFS_Empty; 16718 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 16719 WhitesmithsBraceStyle.ColumnLimit = 0; 16720 16721 // FIXME: this test case can't decide whether there should be a blank line 16722 // after the ~D() line or not. It adds one if one doesn't exist in the test 16723 // and it removes the line if one exists. 16724 /* 16725 verifyFormat("class A;\n" 16726 "namespace B\n" 16727 " {\n" 16728 "class C;\n" 16729 "// Comment\n" 16730 "class D\n" 16731 " {\n" 16732 "public:\n" 16733 " D();\n" 16734 " ~D() {}\n" 16735 "private:\n" 16736 " enum E\n" 16737 " {\n" 16738 " F\n" 16739 " }\n" 16740 " };\n" 16741 " } // namespace B\n", 16742 WhitesmithsBraceStyle); 16743 */ 16744 16745 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 16746 verifyFormat("namespace a\n" 16747 " {\n" 16748 "class A\n" 16749 " {\n" 16750 " void f()\n" 16751 " {\n" 16752 " if (true)\n" 16753 " {\n" 16754 " a();\n" 16755 " b();\n" 16756 " }\n" 16757 " }\n" 16758 " void g()\n" 16759 " {\n" 16760 " return;\n" 16761 " }\n" 16762 " };\n" 16763 "struct B\n" 16764 " {\n" 16765 " int x;\n" 16766 " };\n" 16767 " } // namespace a", 16768 WhitesmithsBraceStyle); 16769 16770 verifyFormat("namespace a\n" 16771 " {\n" 16772 "namespace b\n" 16773 " {\n" 16774 "class A\n" 16775 " {\n" 16776 " void f()\n" 16777 " {\n" 16778 " if (true)\n" 16779 " {\n" 16780 " a();\n" 16781 " b();\n" 16782 " }\n" 16783 " }\n" 16784 " void g()\n" 16785 " {\n" 16786 " return;\n" 16787 " }\n" 16788 " };\n" 16789 "struct B\n" 16790 " {\n" 16791 " int x;\n" 16792 " };\n" 16793 " } // namespace b\n" 16794 " } // namespace a", 16795 WhitesmithsBraceStyle); 16796 16797 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 16798 verifyFormat("namespace a\n" 16799 " {\n" 16800 "namespace b\n" 16801 " {\n" 16802 " class A\n" 16803 " {\n" 16804 " void f()\n" 16805 " {\n" 16806 " if (true)\n" 16807 " {\n" 16808 " a();\n" 16809 " b();\n" 16810 " }\n" 16811 " }\n" 16812 " void g()\n" 16813 " {\n" 16814 " return;\n" 16815 " }\n" 16816 " };\n" 16817 " struct B\n" 16818 " {\n" 16819 " int x;\n" 16820 " };\n" 16821 " } // namespace b\n" 16822 " } // namespace a", 16823 WhitesmithsBraceStyle); 16824 16825 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 16826 verifyFormat("namespace a\n" 16827 " {\n" 16828 " namespace b\n" 16829 " {\n" 16830 " class A\n" 16831 " {\n" 16832 " void f()\n" 16833 " {\n" 16834 " if (true)\n" 16835 " {\n" 16836 " a();\n" 16837 " b();\n" 16838 " }\n" 16839 " }\n" 16840 " void g()\n" 16841 " {\n" 16842 " return;\n" 16843 " }\n" 16844 " };\n" 16845 " struct B\n" 16846 " {\n" 16847 " int x;\n" 16848 " };\n" 16849 " } // namespace b\n" 16850 " } // namespace a", 16851 WhitesmithsBraceStyle); 16852 16853 verifyFormat("void f()\n" 16854 " {\n" 16855 " if (true)\n" 16856 " {\n" 16857 " a();\n" 16858 " }\n" 16859 " else if (false)\n" 16860 " {\n" 16861 " b();\n" 16862 " }\n" 16863 " else\n" 16864 " {\n" 16865 " c();\n" 16866 " }\n" 16867 " }\n", 16868 WhitesmithsBraceStyle); 16869 16870 verifyFormat("void f()\n" 16871 " {\n" 16872 " for (int i = 0; i < 10; ++i)\n" 16873 " {\n" 16874 " a();\n" 16875 " }\n" 16876 " while (false)\n" 16877 " {\n" 16878 " b();\n" 16879 " }\n" 16880 " do\n" 16881 " {\n" 16882 " c();\n" 16883 " } while (false)\n" 16884 " }\n", 16885 WhitesmithsBraceStyle); 16886 16887 WhitesmithsBraceStyle.IndentCaseLabels = true; 16888 verifyFormat("void switchTest1(int a)\n" 16889 " {\n" 16890 " switch (a)\n" 16891 " {\n" 16892 " case 2:\n" 16893 " {\n" 16894 " }\n" 16895 " break;\n" 16896 " }\n" 16897 " }\n", 16898 WhitesmithsBraceStyle); 16899 16900 verifyFormat("void switchTest2(int a)\n" 16901 " {\n" 16902 " switch (a)\n" 16903 " {\n" 16904 " case 0:\n" 16905 " break;\n" 16906 " case 1:\n" 16907 " {\n" 16908 " break;\n" 16909 " }\n" 16910 " case 2:\n" 16911 " {\n" 16912 " }\n" 16913 " break;\n" 16914 " default:\n" 16915 " break;\n" 16916 " }\n" 16917 " }\n", 16918 WhitesmithsBraceStyle); 16919 16920 verifyFormat("void switchTest3(int a)\n" 16921 " {\n" 16922 " switch (a)\n" 16923 " {\n" 16924 " case 0:\n" 16925 " {\n" 16926 " foo(x);\n" 16927 " }\n" 16928 " break;\n" 16929 " default:\n" 16930 " {\n" 16931 " foo(1);\n" 16932 " }\n" 16933 " break;\n" 16934 " }\n" 16935 " }\n", 16936 WhitesmithsBraceStyle); 16937 16938 WhitesmithsBraceStyle.IndentCaseLabels = false; 16939 16940 verifyFormat("void switchTest4(int a)\n" 16941 " {\n" 16942 " switch (a)\n" 16943 " {\n" 16944 " case 2:\n" 16945 " {\n" 16946 " }\n" 16947 " break;\n" 16948 " }\n" 16949 " }\n", 16950 WhitesmithsBraceStyle); 16951 16952 verifyFormat("void switchTest5(int a)\n" 16953 " {\n" 16954 " switch (a)\n" 16955 " {\n" 16956 " case 0:\n" 16957 " break;\n" 16958 " case 1:\n" 16959 " {\n" 16960 " foo();\n" 16961 " break;\n" 16962 " }\n" 16963 " case 2:\n" 16964 " {\n" 16965 " }\n" 16966 " break;\n" 16967 " default:\n" 16968 " break;\n" 16969 " }\n" 16970 " }\n", 16971 WhitesmithsBraceStyle); 16972 16973 verifyFormat("void switchTest6(int a)\n" 16974 " {\n" 16975 " switch (a)\n" 16976 " {\n" 16977 " case 0:\n" 16978 " {\n" 16979 " foo(x);\n" 16980 " }\n" 16981 " break;\n" 16982 " default:\n" 16983 " {\n" 16984 " foo(1);\n" 16985 " }\n" 16986 " break;\n" 16987 " }\n" 16988 " }\n", 16989 WhitesmithsBraceStyle); 16990 16991 verifyFormat("enum X\n" 16992 " {\n" 16993 " Y = 0, // testing\n" 16994 " }\n", 16995 WhitesmithsBraceStyle); 16996 16997 verifyFormat("enum X\n" 16998 " {\n" 16999 " Y = 0\n" 17000 " }\n", 17001 WhitesmithsBraceStyle); 17002 verifyFormat("enum X\n" 17003 " {\n" 17004 " Y = 0,\n" 17005 " Z = 1\n" 17006 " };\n", 17007 WhitesmithsBraceStyle); 17008 17009 verifyFormat("@interface BSApplicationController ()\n" 17010 " {\n" 17011 "@private\n" 17012 " id _extraIvar;\n" 17013 " }\n" 17014 "@end\n", 17015 WhitesmithsBraceStyle); 17016 17017 verifyFormat("#ifdef _DEBUG\n" 17018 "int foo(int i = 0)\n" 17019 "#else\n" 17020 "int foo(int i = 5)\n" 17021 "#endif\n" 17022 " {\n" 17023 " return i;\n" 17024 " }", 17025 WhitesmithsBraceStyle); 17026 17027 verifyFormat("void foo() {}\n" 17028 "void bar()\n" 17029 "#ifdef _DEBUG\n" 17030 " {\n" 17031 " foo();\n" 17032 " }\n" 17033 "#else\n" 17034 " {\n" 17035 " }\n" 17036 "#endif", 17037 WhitesmithsBraceStyle); 17038 17039 verifyFormat("void foobar()\n" 17040 " {\n" 17041 " int i = 5;\n" 17042 " }\n" 17043 "#ifdef _DEBUG\n" 17044 "void bar()\n" 17045 " {\n" 17046 " }\n" 17047 "#else\n" 17048 "void bar()\n" 17049 " {\n" 17050 " foobar();\n" 17051 " }\n" 17052 "#endif", 17053 WhitesmithsBraceStyle); 17054 17055 // This shouldn't affect ObjC blocks.. 17056 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17057 " // ...\n" 17058 " int i;\n" 17059 "}];", 17060 WhitesmithsBraceStyle); 17061 verifyFormat("void (^block)(void) = ^{\n" 17062 " // ...\n" 17063 " int i;\n" 17064 "};", 17065 WhitesmithsBraceStyle); 17066 // .. or dict literals. 17067 verifyFormat("void f()\n" 17068 " {\n" 17069 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17070 " }", 17071 WhitesmithsBraceStyle); 17072 17073 verifyFormat("int f()\n" 17074 " { // comment\n" 17075 " return 42;\n" 17076 " }", 17077 WhitesmithsBraceStyle); 17078 17079 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 17080 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17081 FormatStyle::SIS_OnlyFirstIf; 17082 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17083 verifyFormat("void f(bool b)\n" 17084 " {\n" 17085 " if (b)\n" 17086 " {\n" 17087 " return;\n" 17088 " }\n" 17089 " }\n", 17090 BreakBeforeBraceShortIfs); 17091 verifyFormat("void f(bool b)\n" 17092 " {\n" 17093 " if (b) return;\n" 17094 " }\n", 17095 BreakBeforeBraceShortIfs); 17096 verifyFormat("void f(bool b)\n" 17097 " {\n" 17098 " while (b)\n" 17099 " {\n" 17100 " return;\n" 17101 " }\n" 17102 " }\n", 17103 BreakBeforeBraceShortIfs); 17104 } 17105 17106 TEST_F(FormatTest, GNUBraceBreaking) { 17107 FormatStyle GNUBraceStyle = getLLVMStyle(); 17108 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 17109 verifyFormat("namespace a\n" 17110 "{\n" 17111 "class A\n" 17112 "{\n" 17113 " void f()\n" 17114 " {\n" 17115 " int a;\n" 17116 " {\n" 17117 " int b;\n" 17118 " }\n" 17119 " if (true)\n" 17120 " {\n" 17121 " a();\n" 17122 " b();\n" 17123 " }\n" 17124 " }\n" 17125 " void g() { return; }\n" 17126 "}\n" 17127 "} // namespace a", 17128 GNUBraceStyle); 17129 17130 verifyFormat("void f()\n" 17131 "{\n" 17132 " if (true)\n" 17133 " {\n" 17134 " a();\n" 17135 " }\n" 17136 " else if (false)\n" 17137 " {\n" 17138 " b();\n" 17139 " }\n" 17140 " else\n" 17141 " {\n" 17142 " c();\n" 17143 " }\n" 17144 "}\n", 17145 GNUBraceStyle); 17146 17147 verifyFormat("void f()\n" 17148 "{\n" 17149 " for (int i = 0; i < 10; ++i)\n" 17150 " {\n" 17151 " a();\n" 17152 " }\n" 17153 " while (false)\n" 17154 " {\n" 17155 " b();\n" 17156 " }\n" 17157 " do\n" 17158 " {\n" 17159 " c();\n" 17160 " }\n" 17161 " while (false);\n" 17162 "}\n", 17163 GNUBraceStyle); 17164 17165 verifyFormat("void f(int a)\n" 17166 "{\n" 17167 " switch (a)\n" 17168 " {\n" 17169 " case 0:\n" 17170 " break;\n" 17171 " case 1:\n" 17172 " {\n" 17173 " break;\n" 17174 " }\n" 17175 " case 2:\n" 17176 " {\n" 17177 " }\n" 17178 " break;\n" 17179 " default:\n" 17180 " break;\n" 17181 " }\n" 17182 "}\n", 17183 GNUBraceStyle); 17184 17185 verifyFormat("enum X\n" 17186 "{\n" 17187 " Y = 0,\n" 17188 "}\n", 17189 GNUBraceStyle); 17190 17191 verifyFormat("@interface BSApplicationController ()\n" 17192 "{\n" 17193 "@private\n" 17194 " id _extraIvar;\n" 17195 "}\n" 17196 "@end\n", 17197 GNUBraceStyle); 17198 17199 verifyFormat("#ifdef _DEBUG\n" 17200 "int foo(int i = 0)\n" 17201 "#else\n" 17202 "int foo(int i = 5)\n" 17203 "#endif\n" 17204 "{\n" 17205 " return i;\n" 17206 "}", 17207 GNUBraceStyle); 17208 17209 verifyFormat("void foo() {}\n" 17210 "void bar()\n" 17211 "#ifdef _DEBUG\n" 17212 "{\n" 17213 " foo();\n" 17214 "}\n" 17215 "#else\n" 17216 "{\n" 17217 "}\n" 17218 "#endif", 17219 GNUBraceStyle); 17220 17221 verifyFormat("void foobar() { int i = 5; }\n" 17222 "#ifdef _DEBUG\n" 17223 "void bar() {}\n" 17224 "#else\n" 17225 "void bar() { foobar(); }\n" 17226 "#endif", 17227 GNUBraceStyle); 17228 } 17229 17230 TEST_F(FormatTest, WebKitBraceBreaking) { 17231 FormatStyle WebKitBraceStyle = getLLVMStyle(); 17232 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 17233 WebKitBraceStyle.FixNamespaceComments = false; 17234 verifyFormat("namespace a {\n" 17235 "class A {\n" 17236 " void f()\n" 17237 " {\n" 17238 " if (true) {\n" 17239 " a();\n" 17240 " b();\n" 17241 " }\n" 17242 " }\n" 17243 " void g() { return; }\n" 17244 "};\n" 17245 "enum E {\n" 17246 " A,\n" 17247 " // foo\n" 17248 " B,\n" 17249 " C\n" 17250 "};\n" 17251 "struct B {\n" 17252 " int x;\n" 17253 "};\n" 17254 "}\n", 17255 WebKitBraceStyle); 17256 verifyFormat("struct S {\n" 17257 " int Type;\n" 17258 " union {\n" 17259 " int x;\n" 17260 " double y;\n" 17261 " } Value;\n" 17262 " class C {\n" 17263 " MyFavoriteType Value;\n" 17264 " } Class;\n" 17265 "};\n", 17266 WebKitBraceStyle); 17267 } 17268 17269 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 17270 verifyFormat("void f() {\n" 17271 " try {\n" 17272 " } catch (const Exception &e) {\n" 17273 " }\n" 17274 "}\n", 17275 getLLVMStyle()); 17276 } 17277 17278 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 17279 auto Style = getLLVMStyle(); 17280 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17281 Style.AlignConsecutiveAssignments = 17282 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17283 Style.AlignConsecutiveDeclarations = 17284 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17285 verifyFormat("struct test demo[] = {\n" 17286 " {56, 23, \"hello\"},\n" 17287 " {-1, 93463, \"world\"},\n" 17288 " { 7, 5, \"!!\"}\n" 17289 "};\n", 17290 Style); 17291 17292 verifyFormat("struct test demo[] = {\n" 17293 " {56, 23, \"hello\"}, // first line\n" 17294 " {-1, 93463, \"world\"}, // second line\n" 17295 " { 7, 5, \"!!\"} // third line\n" 17296 "};\n", 17297 Style); 17298 17299 verifyFormat("struct test demo[4] = {\n" 17300 " { 56, 23, 21, \"oh\"}, // first line\n" 17301 " { -1, 93463, 22, \"my\"}, // second line\n" 17302 " { 7, 5, 1, \"goodness\"} // third line\n" 17303 " {234, 5, 1, \"gracious\"} // fourth line\n" 17304 "};\n", 17305 Style); 17306 17307 verifyFormat("struct test demo[3] = {\n" 17308 " {56, 23, \"hello\"},\n" 17309 " {-1, 93463, \"world\"},\n" 17310 " { 7, 5, \"!!\"}\n" 17311 "};\n", 17312 Style); 17313 17314 verifyFormat("struct test demo[3] = {\n" 17315 " {int{56}, 23, \"hello\"},\n" 17316 " {int{-1}, 93463, \"world\"},\n" 17317 " { int{7}, 5, \"!!\"}\n" 17318 "};\n", 17319 Style); 17320 17321 verifyFormat("struct test demo[] = {\n" 17322 " {56, 23, \"hello\"},\n" 17323 " {-1, 93463, \"world\"},\n" 17324 " { 7, 5, \"!!\"},\n" 17325 "};\n", 17326 Style); 17327 17328 verifyFormat("test demo[] = {\n" 17329 " {56, 23, \"hello\"},\n" 17330 " {-1, 93463, \"world\"},\n" 17331 " { 7, 5, \"!!\"},\n" 17332 "};\n", 17333 Style); 17334 17335 verifyFormat("demo = std::array<struct test, 3>{\n" 17336 " test{56, 23, \"hello\"},\n" 17337 " test{-1, 93463, \"world\"},\n" 17338 " test{ 7, 5, \"!!\"},\n" 17339 "};\n", 17340 Style); 17341 17342 verifyFormat("test demo[] = {\n" 17343 " {56, 23, \"hello\"},\n" 17344 "#if X\n" 17345 " {-1, 93463, \"world\"},\n" 17346 "#endif\n" 17347 " { 7, 5, \"!!\"}\n" 17348 "};\n", 17349 Style); 17350 17351 verifyFormat( 17352 "test demo[] = {\n" 17353 " { 7, 23,\n" 17354 " \"hello world i am a very long line that really, in any\"\n" 17355 " \"just world, ought to be split over multiple lines\"},\n" 17356 " {-1, 93463, \"world\"},\n" 17357 " {56, 5, \"!!\"}\n" 17358 "};\n", 17359 Style); 17360 17361 verifyFormat("return GradForUnaryCwise(g, {\n" 17362 " {{\"sign\"}, \"Sign\", " 17363 " {\"x\", \"dy\"}},\n" 17364 " { {\"dx\"}, \"Mul\", {\"dy\"" 17365 ", \"sign\"}},\n" 17366 "});\n", 17367 Style); 17368 17369 Style.ColumnLimit = 0; 17370 EXPECT_EQ( 17371 "test demo[] = {\n" 17372 " {56, 23, \"hello world i am a very long line that really, " 17373 "in any just world, ought to be split over multiple lines\"},\n" 17374 " {-1, 93463, " 17375 " \"world\"},\n" 17376 " { 7, 5, " 17377 " \"!!\"},\n" 17378 "};", 17379 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17380 "that really, in any just world, ought to be split over multiple " 17381 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17382 Style)); 17383 17384 Style.ColumnLimit = 80; 17385 verifyFormat("test demo[] = {\n" 17386 " {56, 23, /* a comment */ \"hello\"},\n" 17387 " {-1, 93463, \"world\"},\n" 17388 " { 7, 5, \"!!\"}\n" 17389 "};\n", 17390 Style); 17391 17392 verifyFormat("test demo[] = {\n" 17393 " {56, 23, \"hello\"},\n" 17394 " {-1, 93463, \"world\" /* comment here */},\n" 17395 " { 7, 5, \"!!\"}\n" 17396 "};\n", 17397 Style); 17398 17399 verifyFormat("test demo[] = {\n" 17400 " {56, /* a comment */ 23, \"hello\"},\n" 17401 " {-1, 93463, \"world\"},\n" 17402 " { 7, 5, \"!!\"}\n" 17403 "};\n", 17404 Style); 17405 17406 Style.ColumnLimit = 20; 17407 EXPECT_EQ( 17408 "demo = std::array<\n" 17409 " struct test, 3>{\n" 17410 " test{\n" 17411 " 56, 23,\n" 17412 " \"hello \"\n" 17413 " \"world i \"\n" 17414 " \"am a very \"\n" 17415 " \"long line \"\n" 17416 " \"that \"\n" 17417 " \"really, \"\n" 17418 " \"in any \"\n" 17419 " \"just \"\n" 17420 " \"world, \"\n" 17421 " \"ought to \"\n" 17422 " \"be split \"\n" 17423 " \"over \"\n" 17424 " \"multiple \"\n" 17425 " \"lines\"},\n" 17426 " test{-1, 93463,\n" 17427 " \"world\"},\n" 17428 " test{ 7, 5,\n" 17429 " \"!!\" },\n" 17430 "};", 17431 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17432 "i am a very long line that really, in any just world, ought " 17433 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17434 "test{7, 5, \"!!\"},};", 17435 Style)); 17436 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17437 Style = getLLVMStyleWithColumns(50); 17438 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17439 verifyFormat("static A x = {\n" 17440 " {{init1, init2, init3, init4},\n" 17441 " {init1, init2, init3, init4}}\n" 17442 "};", 17443 Style); 17444 Style.ColumnLimit = 100; 17445 EXPECT_EQ( 17446 "test demo[] = {\n" 17447 " {56, 23,\n" 17448 " \"hello world i am a very long line that really, in any just world" 17449 ", ought to be split over \"\n" 17450 " \"multiple lines\" },\n" 17451 " {-1, 93463, \"world\"},\n" 17452 " { 7, 5, \"!!\"},\n" 17453 "};", 17454 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17455 "that really, in any just world, ought to be split over multiple " 17456 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17457 Style)); 17458 17459 Style = getLLVMStyleWithColumns(50); 17460 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17461 Style.AlignConsecutiveAssignments = 17462 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17463 Style.AlignConsecutiveDeclarations = 17464 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17465 verifyFormat("struct test demo[] = {\n" 17466 " {56, 23, \"hello\"},\n" 17467 " {-1, 93463, \"world\"},\n" 17468 " { 7, 5, \"!!\"}\n" 17469 "};\n" 17470 "static A x = {\n" 17471 " {{init1, init2, init3, init4},\n" 17472 " {init1, init2, init3, init4}}\n" 17473 "};", 17474 Style); 17475 Style.ColumnLimit = 100; 17476 Style.AlignConsecutiveAssignments = 17477 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17478 Style.AlignConsecutiveDeclarations = 17479 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17480 verifyFormat("struct test demo[] = {\n" 17481 " {56, 23, \"hello\"},\n" 17482 " {-1, 93463, \"world\"},\n" 17483 " { 7, 5, \"!!\"}\n" 17484 "};\n" 17485 "struct test demo[4] = {\n" 17486 " { 56, 23, 21, \"oh\"}, // first line\n" 17487 " { -1, 93463, 22, \"my\"}, // second line\n" 17488 " { 7, 5, 1, \"goodness\"} // third line\n" 17489 " {234, 5, 1, \"gracious\"} // fourth line\n" 17490 "};\n", 17491 Style); 17492 EXPECT_EQ( 17493 "test demo[] = {\n" 17494 " {56,\n" 17495 " \"hello world i am a very long line that really, in any just world" 17496 ", ought to be split over \"\n" 17497 " \"multiple lines\", 23},\n" 17498 " {-1, \"world\", 93463},\n" 17499 " { 7, \"!!\", 5},\n" 17500 "};", 17501 format("test demo[] = {{56, \"hello world i am a very long line " 17502 "that really, in any just world, ought to be split over multiple " 17503 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 17504 Style)); 17505 } 17506 17507 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 17508 auto Style = getLLVMStyle(); 17509 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17510 verifyFormat("struct test demo[] = {\n" 17511 " {56, 23, \"hello\"},\n" 17512 " {-1, 93463, \"world\"},\n" 17513 " {7, 5, \"!!\" }\n" 17514 "};\n", 17515 Style); 17516 17517 verifyFormat("struct test demo[] = {\n" 17518 " {56, 23, \"hello\"}, // first line\n" 17519 " {-1, 93463, \"world\"}, // second line\n" 17520 " {7, 5, \"!!\" } // third line\n" 17521 "};\n", 17522 Style); 17523 verifyFormat("struct test demo[4] = {\n" 17524 " {56, 23, 21, \"oh\" }, // first line\n" 17525 " {-1, 93463, 22, \"my\" }, // second line\n" 17526 " {7, 5, 1, \"goodness\"} // third line\n" 17527 " {234, 5, 1, \"gracious\"} // fourth line\n" 17528 "};\n", 17529 Style); 17530 verifyFormat("struct test demo[3] = {\n" 17531 " {56, 23, \"hello\"},\n" 17532 " {-1, 93463, \"world\"},\n" 17533 " {7, 5, \"!!\" }\n" 17534 "};\n", 17535 Style); 17536 17537 verifyFormat("struct test demo[3] = {\n" 17538 " {int{56}, 23, \"hello\"},\n" 17539 " {int{-1}, 93463, \"world\"},\n" 17540 " {int{7}, 5, \"!!\" }\n" 17541 "};\n", 17542 Style); 17543 verifyFormat("struct test demo[] = {\n" 17544 " {56, 23, \"hello\"},\n" 17545 " {-1, 93463, \"world\"},\n" 17546 " {7, 5, \"!!\" },\n" 17547 "};\n", 17548 Style); 17549 verifyFormat("test demo[] = {\n" 17550 " {56, 23, \"hello\"},\n" 17551 " {-1, 93463, \"world\"},\n" 17552 " {7, 5, \"!!\" },\n" 17553 "};\n", 17554 Style); 17555 verifyFormat("demo = std::array<struct test, 3>{\n" 17556 " test{56, 23, \"hello\"},\n" 17557 " test{-1, 93463, \"world\"},\n" 17558 " test{7, 5, \"!!\" },\n" 17559 "};\n", 17560 Style); 17561 verifyFormat("test demo[] = {\n" 17562 " {56, 23, \"hello\"},\n" 17563 "#if X\n" 17564 " {-1, 93463, \"world\"},\n" 17565 "#endif\n" 17566 " {7, 5, \"!!\" }\n" 17567 "};\n", 17568 Style); 17569 verifyFormat( 17570 "test demo[] = {\n" 17571 " {7, 23,\n" 17572 " \"hello world i am a very long line that really, in any\"\n" 17573 " \"just world, ought to be split over multiple lines\"},\n" 17574 " {-1, 93463, \"world\" },\n" 17575 " {56, 5, \"!!\" }\n" 17576 "};\n", 17577 Style); 17578 17579 verifyFormat("return GradForUnaryCwise(g, {\n" 17580 " {{\"sign\"}, \"Sign\", {\"x\", " 17581 "\"dy\"} },\n" 17582 " {{\"dx\"}, \"Mul\", " 17583 "{\"dy\", \"sign\"}},\n" 17584 "});\n", 17585 Style); 17586 17587 Style.ColumnLimit = 0; 17588 EXPECT_EQ( 17589 "test demo[] = {\n" 17590 " {56, 23, \"hello world i am a very long line that really, in any " 17591 "just world, ought to be split over multiple lines\"},\n" 17592 " {-1, 93463, \"world\" " 17593 " },\n" 17594 " {7, 5, \"!!\" " 17595 " },\n" 17596 "};", 17597 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17598 "that really, in any just world, ought to be split over multiple " 17599 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17600 Style)); 17601 17602 Style.ColumnLimit = 80; 17603 verifyFormat("test demo[] = {\n" 17604 " {56, 23, /* a comment */ \"hello\"},\n" 17605 " {-1, 93463, \"world\" },\n" 17606 " {7, 5, \"!!\" }\n" 17607 "};\n", 17608 Style); 17609 17610 verifyFormat("test demo[] = {\n" 17611 " {56, 23, \"hello\" },\n" 17612 " {-1, 93463, \"world\" /* comment here */},\n" 17613 " {7, 5, \"!!\" }\n" 17614 "};\n", 17615 Style); 17616 17617 verifyFormat("test demo[] = {\n" 17618 " {56, /* a comment */ 23, \"hello\"},\n" 17619 " {-1, 93463, \"world\"},\n" 17620 " {7, 5, \"!!\" }\n" 17621 "};\n", 17622 Style); 17623 17624 Style.ColumnLimit = 20; 17625 EXPECT_EQ( 17626 "demo = std::array<\n" 17627 " struct test, 3>{\n" 17628 " test{\n" 17629 " 56, 23,\n" 17630 " \"hello \"\n" 17631 " \"world i \"\n" 17632 " \"am a very \"\n" 17633 " \"long line \"\n" 17634 " \"that \"\n" 17635 " \"really, \"\n" 17636 " \"in any \"\n" 17637 " \"just \"\n" 17638 " \"world, \"\n" 17639 " \"ought to \"\n" 17640 " \"be split \"\n" 17641 " \"over \"\n" 17642 " \"multiple \"\n" 17643 " \"lines\"},\n" 17644 " test{-1, 93463,\n" 17645 " \"world\"},\n" 17646 " test{7, 5,\n" 17647 " \"!!\" },\n" 17648 "};", 17649 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17650 "i am a very long line that really, in any just world, ought " 17651 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17652 "test{7, 5, \"!!\"},};", 17653 Style)); 17654 17655 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17656 Style = getLLVMStyleWithColumns(50); 17657 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17658 verifyFormat("static A x = {\n" 17659 " {{init1, init2, init3, init4},\n" 17660 " {init1, init2, init3, init4}}\n" 17661 "};", 17662 Style); 17663 Style.ColumnLimit = 100; 17664 EXPECT_EQ( 17665 "test demo[] = {\n" 17666 " {56, 23,\n" 17667 " \"hello world i am a very long line that really, in any just world" 17668 ", ought to be split over \"\n" 17669 " \"multiple lines\" },\n" 17670 " {-1, 93463, \"world\"},\n" 17671 " {7, 5, \"!!\" },\n" 17672 "};", 17673 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17674 "that really, in any just world, ought to be split over multiple " 17675 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17676 Style)); 17677 } 17678 17679 TEST_F(FormatTest, UnderstandsPragmas) { 17680 verifyFormat("#pragma omp reduction(| : var)"); 17681 verifyFormat("#pragma omp reduction(+ : var)"); 17682 17683 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 17684 "(including parentheses).", 17685 format("#pragma mark Any non-hyphenated or hyphenated string " 17686 "(including parentheses).")); 17687 } 17688 17689 TEST_F(FormatTest, UnderstandPragmaOption) { 17690 verifyFormat("#pragma option -C -A"); 17691 17692 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 17693 } 17694 17695 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 17696 FormatStyle Style = getLLVMStyle(); 17697 Style.ColumnLimit = 20; 17698 17699 // See PR41213 17700 EXPECT_EQ("/*\n" 17701 " *\t9012345\n" 17702 " * /8901\n" 17703 " */", 17704 format("/*\n" 17705 " *\t9012345 /8901\n" 17706 " */", 17707 Style)); 17708 EXPECT_EQ("/*\n" 17709 " *345678\n" 17710 " *\t/8901\n" 17711 " */", 17712 format("/*\n" 17713 " *345678\t/8901\n" 17714 " */", 17715 Style)); 17716 17717 verifyFormat("int a; // the\n" 17718 " // comment", 17719 Style); 17720 EXPECT_EQ("int a; /* first line\n" 17721 " * second\n" 17722 " * line third\n" 17723 " * line\n" 17724 " */", 17725 format("int a; /* first line\n" 17726 " * second\n" 17727 " * line third\n" 17728 " * line\n" 17729 " */", 17730 Style)); 17731 EXPECT_EQ("int a; // first line\n" 17732 " // second\n" 17733 " // line third\n" 17734 " // line", 17735 format("int a; // first line\n" 17736 " // second line\n" 17737 " // third line", 17738 Style)); 17739 17740 Style.PenaltyExcessCharacter = 90; 17741 verifyFormat("int a; // the comment", Style); 17742 EXPECT_EQ("int a; // the comment\n" 17743 " // aaa", 17744 format("int a; // the comment aaa", Style)); 17745 EXPECT_EQ("int a; /* first line\n" 17746 " * second line\n" 17747 " * third line\n" 17748 " */", 17749 format("int a; /* first line\n" 17750 " * second line\n" 17751 " * third line\n" 17752 " */", 17753 Style)); 17754 EXPECT_EQ("int a; // first line\n" 17755 " // second line\n" 17756 " // third line", 17757 format("int a; // first line\n" 17758 " // second line\n" 17759 " // third line", 17760 Style)); 17761 // FIXME: Investigate why this is not getting the same layout as the test 17762 // above. 17763 EXPECT_EQ("int a; /* first line\n" 17764 " * second line\n" 17765 " * third line\n" 17766 " */", 17767 format("int a; /* first line second line third line" 17768 "\n*/", 17769 Style)); 17770 17771 EXPECT_EQ("// foo bar baz bazfoo\n" 17772 "// foo bar foo bar\n", 17773 format("// foo bar baz bazfoo\n" 17774 "// foo bar foo bar\n", 17775 Style)); 17776 EXPECT_EQ("// foo bar baz bazfoo\n" 17777 "// foo bar foo bar\n", 17778 format("// foo bar baz bazfoo\n" 17779 "// foo bar foo bar\n", 17780 Style)); 17781 17782 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 17783 // next one. 17784 EXPECT_EQ("// foo bar baz bazfoo\n" 17785 "// bar foo bar\n", 17786 format("// foo bar baz bazfoo bar\n" 17787 "// foo bar\n", 17788 Style)); 17789 17790 EXPECT_EQ("// foo bar baz bazfoo\n" 17791 "// foo bar baz bazfoo\n" 17792 "// bar foo bar\n", 17793 format("// foo bar baz bazfoo\n" 17794 "// foo bar baz bazfoo bar\n" 17795 "// foo bar\n", 17796 Style)); 17797 17798 EXPECT_EQ("// foo bar baz bazfoo\n" 17799 "// foo bar baz bazfoo\n" 17800 "// bar foo bar\n", 17801 format("// foo bar baz bazfoo\n" 17802 "// foo bar baz bazfoo bar\n" 17803 "// foo bar\n", 17804 Style)); 17805 17806 // Make sure we do not keep protruding characters if strict mode reflow is 17807 // cheaper than keeping protruding characters. 17808 Style.ColumnLimit = 21; 17809 EXPECT_EQ( 17810 "// foo foo foo foo\n" 17811 "// foo foo foo foo\n" 17812 "// foo foo foo foo\n", 17813 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 17814 17815 EXPECT_EQ("int a = /* long block\n" 17816 " comment */\n" 17817 " 42;", 17818 format("int a = /* long block comment */ 42;", Style)); 17819 } 17820 17821 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 17822 for (size_t i = 1; i < Styles.size(); ++i) \ 17823 EXPECT_EQ(Styles[0], Styles[i]) \ 17824 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 17825 17826 TEST_F(FormatTest, GetsPredefinedStyleByName) { 17827 SmallVector<FormatStyle, 3> Styles; 17828 Styles.resize(3); 17829 17830 Styles[0] = getLLVMStyle(); 17831 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 17832 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 17833 EXPECT_ALL_STYLES_EQUAL(Styles); 17834 17835 Styles[0] = getGoogleStyle(); 17836 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 17837 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 17838 EXPECT_ALL_STYLES_EQUAL(Styles); 17839 17840 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 17841 EXPECT_TRUE( 17842 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 17843 EXPECT_TRUE( 17844 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 17845 EXPECT_ALL_STYLES_EQUAL(Styles); 17846 17847 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 17848 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 17849 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 17850 EXPECT_ALL_STYLES_EQUAL(Styles); 17851 17852 Styles[0] = getMozillaStyle(); 17853 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 17854 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 17855 EXPECT_ALL_STYLES_EQUAL(Styles); 17856 17857 Styles[0] = getWebKitStyle(); 17858 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 17859 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 17860 EXPECT_ALL_STYLES_EQUAL(Styles); 17861 17862 Styles[0] = getGNUStyle(); 17863 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 17864 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 17865 EXPECT_ALL_STYLES_EQUAL(Styles); 17866 17867 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 17868 } 17869 17870 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 17871 SmallVector<FormatStyle, 8> Styles; 17872 Styles.resize(2); 17873 17874 Styles[0] = getGoogleStyle(); 17875 Styles[1] = getLLVMStyle(); 17876 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 17877 EXPECT_ALL_STYLES_EQUAL(Styles); 17878 17879 Styles.resize(5); 17880 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 17881 Styles[1] = getLLVMStyle(); 17882 Styles[1].Language = FormatStyle::LK_JavaScript; 17883 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 17884 17885 Styles[2] = getLLVMStyle(); 17886 Styles[2].Language = FormatStyle::LK_JavaScript; 17887 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 17888 "BasedOnStyle: Google", 17889 &Styles[2]) 17890 .value()); 17891 17892 Styles[3] = getLLVMStyle(); 17893 Styles[3].Language = FormatStyle::LK_JavaScript; 17894 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 17895 "Language: JavaScript", 17896 &Styles[3]) 17897 .value()); 17898 17899 Styles[4] = getLLVMStyle(); 17900 Styles[4].Language = FormatStyle::LK_JavaScript; 17901 EXPECT_EQ(0, parseConfiguration("---\n" 17902 "BasedOnStyle: LLVM\n" 17903 "IndentWidth: 123\n" 17904 "---\n" 17905 "BasedOnStyle: Google\n" 17906 "Language: JavaScript", 17907 &Styles[4]) 17908 .value()); 17909 EXPECT_ALL_STYLES_EQUAL(Styles); 17910 } 17911 17912 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 17913 Style.FIELD = false; \ 17914 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 17915 EXPECT_TRUE(Style.FIELD); \ 17916 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 17917 EXPECT_FALSE(Style.FIELD); 17918 17919 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 17920 17921 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 17922 Style.STRUCT.FIELD = false; \ 17923 EXPECT_EQ(0, \ 17924 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 17925 .value()); \ 17926 EXPECT_TRUE(Style.STRUCT.FIELD); \ 17927 EXPECT_EQ(0, \ 17928 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 17929 .value()); \ 17930 EXPECT_FALSE(Style.STRUCT.FIELD); 17931 17932 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 17933 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 17934 17935 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 17936 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 17937 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 17938 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 17939 17940 TEST_F(FormatTest, ParsesConfigurationBools) { 17941 FormatStyle Style = {}; 17942 Style.Language = FormatStyle::LK_Cpp; 17943 CHECK_PARSE_BOOL(AlignTrailingComments); 17944 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 17945 CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine); 17946 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 17947 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 17948 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 17949 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 17950 CHECK_PARSE_BOOL(BinPackArguments); 17951 CHECK_PARSE_BOOL(BinPackParameters); 17952 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 17953 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 17954 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 17955 CHECK_PARSE_BOOL(BreakStringLiterals); 17956 CHECK_PARSE_BOOL(CompactNamespaces); 17957 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 17958 CHECK_PARSE_BOOL(DeriveLineEnding); 17959 CHECK_PARSE_BOOL(DerivePointerAlignment); 17960 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 17961 CHECK_PARSE_BOOL(DisableFormat); 17962 CHECK_PARSE_BOOL(IndentAccessModifiers); 17963 CHECK_PARSE_BOOL(IndentCaseLabels); 17964 CHECK_PARSE_BOOL(IndentCaseBlocks); 17965 CHECK_PARSE_BOOL(IndentGotoLabels); 17966 CHECK_PARSE_BOOL(IndentRequires); 17967 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 17968 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 17969 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 17970 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 17971 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 17972 CHECK_PARSE_BOOL(ReflowComments); 17973 CHECK_PARSE_BOOL(SortUsingDeclarations); 17974 CHECK_PARSE_BOOL(SpacesInParentheses); 17975 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 17976 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 17977 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 17978 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 17979 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 17980 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 17981 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 17982 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 17983 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 17984 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 17985 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 17986 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 17987 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 17988 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 17989 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 17990 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 17991 CHECK_PARSE_BOOL(UseCRLF); 17992 17993 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 17994 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 17995 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 17996 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 17997 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 17998 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 17999 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 18000 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 18001 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 18002 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 18003 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 18004 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 18005 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 18006 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 18007 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 18008 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 18009 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 18010 } 18011 18012 #undef CHECK_PARSE_BOOL 18013 18014 TEST_F(FormatTest, ParsesConfiguration) { 18015 FormatStyle Style = {}; 18016 Style.Language = FormatStyle::LK_Cpp; 18017 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 18018 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 18019 ConstructorInitializerIndentWidth, 1234u); 18020 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 18021 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 18022 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 18023 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 18024 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 18025 PenaltyBreakBeforeFirstCallParameter, 1234u); 18026 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 18027 PenaltyBreakTemplateDeclaration, 1234u); 18028 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 18029 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 18030 PenaltyReturnTypeOnItsOwnLine, 1234u); 18031 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 18032 SpacesBeforeTrailingComments, 1234u); 18033 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 18034 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 18035 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 18036 18037 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 18038 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 18039 FormatStyle::ACS_None); 18040 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 18041 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 18042 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 18043 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 18044 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 18045 AlignConsecutiveAssignments, 18046 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18047 // For backwards compability, false / true should still parse 18048 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 18049 FormatStyle::ACS_None); 18050 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 18051 FormatStyle::ACS_Consecutive); 18052 18053 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 18054 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 18055 FormatStyle::ACS_None); 18056 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 18057 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 18058 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 18059 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 18060 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 18061 AlignConsecutiveBitFields, 18062 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18063 // For backwards compability, false / true should still parse 18064 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 18065 FormatStyle::ACS_None); 18066 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 18067 FormatStyle::ACS_Consecutive); 18068 18069 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 18070 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 18071 FormatStyle::ACS_None); 18072 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 18073 FormatStyle::ACS_Consecutive); 18074 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 18075 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 18076 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 18077 AlignConsecutiveMacros, 18078 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18079 // For backwards compability, false / true should still parse 18080 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 18081 FormatStyle::ACS_None); 18082 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 18083 FormatStyle::ACS_Consecutive); 18084 18085 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 18086 CHECK_PARSE("AlignConsecutiveDeclarations: None", 18087 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18088 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 18089 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18090 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 18091 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 18092 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 18093 AlignConsecutiveDeclarations, 18094 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18095 // For backwards compability, false / true should still parse 18096 CHECK_PARSE("AlignConsecutiveDeclarations: false", 18097 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18098 CHECK_PARSE("AlignConsecutiveDeclarations: true", 18099 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18100 18101 Style.PointerAlignment = FormatStyle::PAS_Middle; 18102 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 18103 FormatStyle::PAS_Left); 18104 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 18105 FormatStyle::PAS_Right); 18106 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 18107 FormatStyle::PAS_Middle); 18108 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 18109 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 18110 FormatStyle::RAS_Pointer); 18111 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 18112 FormatStyle::RAS_Left); 18113 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 18114 FormatStyle::RAS_Right); 18115 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 18116 FormatStyle::RAS_Middle); 18117 // For backward compatibility: 18118 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 18119 FormatStyle::PAS_Left); 18120 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 18121 FormatStyle::PAS_Right); 18122 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 18123 FormatStyle::PAS_Middle); 18124 18125 Style.Standard = FormatStyle::LS_Auto; 18126 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 18127 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 18128 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 18129 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 18130 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 18131 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 18132 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 18133 // Legacy aliases: 18134 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 18135 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 18136 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 18137 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 18138 18139 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 18140 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 18141 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 18142 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 18143 FormatStyle::BOS_None); 18144 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 18145 FormatStyle::BOS_All); 18146 // For backward compatibility: 18147 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 18148 FormatStyle::BOS_None); 18149 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 18150 FormatStyle::BOS_All); 18151 18152 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 18153 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 18154 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18155 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 18156 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 18157 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 18158 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 18159 // For backward compatibility: 18160 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 18161 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18162 18163 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 18164 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 18165 FormatStyle::BILS_AfterComma); 18166 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 18167 FormatStyle::BILS_BeforeComma); 18168 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 18169 FormatStyle::BILS_AfterColon); 18170 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 18171 FormatStyle::BILS_BeforeColon); 18172 // For backward compatibility: 18173 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 18174 FormatStyle::BILS_BeforeComma); 18175 18176 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 18177 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 18178 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 18179 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 18180 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 18181 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 18182 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 18183 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 18184 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 18185 18186 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 18187 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 18188 FormatStyle::BAS_Align); 18189 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 18190 FormatStyle::BAS_DontAlign); 18191 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 18192 FormatStyle::BAS_AlwaysBreak); 18193 // For backward compatibility: 18194 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 18195 FormatStyle::BAS_DontAlign); 18196 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 18197 FormatStyle::BAS_Align); 18198 18199 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 18200 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 18201 FormatStyle::ENAS_DontAlign); 18202 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 18203 FormatStyle::ENAS_Left); 18204 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 18205 FormatStyle::ENAS_Right); 18206 // For backward compatibility: 18207 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 18208 FormatStyle::ENAS_Left); 18209 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 18210 FormatStyle::ENAS_Right); 18211 18212 Style.AlignOperands = FormatStyle::OAS_Align; 18213 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 18214 FormatStyle::OAS_DontAlign); 18215 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 18216 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 18217 FormatStyle::OAS_AlignAfterOperator); 18218 // For backward compatibility: 18219 CHECK_PARSE("AlignOperands: false", AlignOperands, 18220 FormatStyle::OAS_DontAlign); 18221 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 18222 18223 Style.UseTab = FormatStyle::UT_ForIndentation; 18224 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 18225 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 18226 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 18227 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 18228 FormatStyle::UT_ForContinuationAndIndentation); 18229 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 18230 FormatStyle::UT_AlignWithSpaces); 18231 // For backward compatibility: 18232 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 18233 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 18234 18235 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 18236 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 18237 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18238 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 18239 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 18240 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 18241 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18242 // For backward compatibility: 18243 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 18244 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18245 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 18246 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18247 18248 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 18249 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 18250 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18251 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 18252 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 18253 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 18254 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 18255 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 18256 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18257 // For backward compatibility: 18258 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 18259 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18260 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 18261 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18262 18263 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 18264 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 18265 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 18266 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 18267 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 18268 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 18269 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 18270 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 18271 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 18272 18273 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 18274 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 18275 FormatStyle::SBPO_Never); 18276 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 18277 FormatStyle::SBPO_Always); 18278 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 18279 FormatStyle::SBPO_ControlStatements); 18280 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 18281 SpaceBeforeParens, 18282 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18283 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 18284 FormatStyle::SBPO_NonEmptyParentheses); 18285 // For backward compatibility: 18286 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 18287 FormatStyle::SBPO_Never); 18288 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 18289 FormatStyle::SBPO_ControlStatements); 18290 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 18291 SpaceBeforeParens, 18292 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18293 18294 Style.ColumnLimit = 123; 18295 FormatStyle BaseStyle = getLLVMStyle(); 18296 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 18297 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 18298 18299 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 18300 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 18301 FormatStyle::BS_Attach); 18302 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 18303 FormatStyle::BS_Linux); 18304 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 18305 FormatStyle::BS_Mozilla); 18306 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 18307 FormatStyle::BS_Stroustrup); 18308 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 18309 FormatStyle::BS_Allman); 18310 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 18311 FormatStyle::BS_Whitesmiths); 18312 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 18313 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 18314 FormatStyle::BS_WebKit); 18315 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 18316 FormatStyle::BS_Custom); 18317 18318 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 18319 CHECK_PARSE("BraceWrapping:\n" 18320 " AfterControlStatement: MultiLine", 18321 BraceWrapping.AfterControlStatement, 18322 FormatStyle::BWACS_MultiLine); 18323 CHECK_PARSE("BraceWrapping:\n" 18324 " AfterControlStatement: Always", 18325 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18326 CHECK_PARSE("BraceWrapping:\n" 18327 " AfterControlStatement: Never", 18328 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18329 // For backward compatibility: 18330 CHECK_PARSE("BraceWrapping:\n" 18331 " AfterControlStatement: true", 18332 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18333 CHECK_PARSE("BraceWrapping:\n" 18334 " AfterControlStatement: false", 18335 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18336 18337 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 18338 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 18339 FormatStyle::RTBS_None); 18340 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 18341 FormatStyle::RTBS_All); 18342 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 18343 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 18344 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 18345 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 18346 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 18347 AlwaysBreakAfterReturnType, 18348 FormatStyle::RTBS_TopLevelDefinitions); 18349 18350 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 18351 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 18352 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 18353 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 18354 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18355 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 18356 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18357 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 18358 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18359 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 18360 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18361 18362 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 18363 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 18364 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 18365 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 18366 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 18367 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 18368 AlwaysBreakAfterDefinitionReturnType, 18369 FormatStyle::DRTBS_TopLevel); 18370 18371 Style.NamespaceIndentation = FormatStyle::NI_All; 18372 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 18373 FormatStyle::NI_None); 18374 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 18375 FormatStyle::NI_Inner); 18376 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 18377 FormatStyle::NI_All); 18378 18379 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 18380 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 18381 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18382 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 18383 AllowShortIfStatementsOnASingleLine, 18384 FormatStyle::SIS_WithoutElse); 18385 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 18386 AllowShortIfStatementsOnASingleLine, 18387 FormatStyle::SIS_OnlyFirstIf); 18388 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 18389 AllowShortIfStatementsOnASingleLine, 18390 FormatStyle::SIS_AllIfsAndElse); 18391 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 18392 AllowShortIfStatementsOnASingleLine, 18393 FormatStyle::SIS_OnlyFirstIf); 18394 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 18395 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18396 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 18397 AllowShortIfStatementsOnASingleLine, 18398 FormatStyle::SIS_WithoutElse); 18399 18400 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 18401 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 18402 FormatStyle::IEBS_AfterExternBlock); 18403 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 18404 FormatStyle::IEBS_Indent); 18405 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 18406 FormatStyle::IEBS_NoIndent); 18407 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 18408 FormatStyle::IEBS_Indent); 18409 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 18410 FormatStyle::IEBS_NoIndent); 18411 18412 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 18413 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 18414 FormatStyle::BFCS_Both); 18415 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 18416 FormatStyle::BFCS_None); 18417 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 18418 FormatStyle::BFCS_Before); 18419 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 18420 FormatStyle::BFCS_After); 18421 18422 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 18423 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 18424 FormatStyle::SJSIO_After); 18425 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 18426 FormatStyle::SJSIO_Before); 18427 18428 // FIXME: This is required because parsing a configuration simply overwrites 18429 // the first N elements of the list instead of resetting it. 18430 Style.ForEachMacros.clear(); 18431 std::vector<std::string> BoostForeach; 18432 BoostForeach.push_back("BOOST_FOREACH"); 18433 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 18434 std::vector<std::string> BoostAndQForeach; 18435 BoostAndQForeach.push_back("BOOST_FOREACH"); 18436 BoostAndQForeach.push_back("Q_FOREACH"); 18437 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 18438 BoostAndQForeach); 18439 18440 Style.IfMacros.clear(); 18441 std::vector<std::string> CustomIfs; 18442 CustomIfs.push_back("MYIF"); 18443 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 18444 18445 Style.AttributeMacros.clear(); 18446 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 18447 std::vector<std::string>{"__capability"}); 18448 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 18449 std::vector<std::string>({"attr1", "attr2"})); 18450 18451 Style.StatementAttributeLikeMacros.clear(); 18452 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 18453 StatementAttributeLikeMacros, 18454 std::vector<std::string>({"emit", "Q_EMIT"})); 18455 18456 Style.StatementMacros.clear(); 18457 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 18458 std::vector<std::string>{"QUNUSED"}); 18459 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 18460 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 18461 18462 Style.NamespaceMacros.clear(); 18463 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 18464 std::vector<std::string>{"TESTSUITE"}); 18465 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 18466 std::vector<std::string>({"TESTSUITE", "SUITE"})); 18467 18468 Style.WhitespaceSensitiveMacros.clear(); 18469 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 18470 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 18471 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 18472 WhitespaceSensitiveMacros, 18473 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 18474 Style.WhitespaceSensitiveMacros.clear(); 18475 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 18476 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 18477 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 18478 WhitespaceSensitiveMacros, 18479 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 18480 18481 Style.IncludeStyle.IncludeCategories.clear(); 18482 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 18483 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 18484 CHECK_PARSE("IncludeCategories:\n" 18485 " - Regex: abc/.*\n" 18486 " Priority: 2\n" 18487 " - Regex: .*\n" 18488 " Priority: 1\n" 18489 " CaseSensitive: true\n", 18490 IncludeStyle.IncludeCategories, ExpectedCategories); 18491 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 18492 "abc$"); 18493 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 18494 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 18495 18496 Style.SortIncludes = FormatStyle::SI_Never; 18497 CHECK_PARSE("SortIncludes: true", SortIncludes, 18498 FormatStyle::SI_CaseSensitive); 18499 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 18500 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 18501 FormatStyle::SI_CaseInsensitive); 18502 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 18503 FormatStyle::SI_CaseSensitive); 18504 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 18505 18506 Style.RawStringFormats.clear(); 18507 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 18508 { 18509 FormatStyle::LK_TextProto, 18510 {"pb", "proto"}, 18511 {"PARSE_TEXT_PROTO"}, 18512 /*CanonicalDelimiter=*/"", 18513 "llvm", 18514 }, 18515 { 18516 FormatStyle::LK_Cpp, 18517 {"cc", "cpp"}, 18518 {"C_CODEBLOCK", "CPPEVAL"}, 18519 /*CanonicalDelimiter=*/"cc", 18520 /*BasedOnStyle=*/"", 18521 }, 18522 }; 18523 18524 CHECK_PARSE("RawStringFormats:\n" 18525 " - Language: TextProto\n" 18526 " Delimiters:\n" 18527 " - 'pb'\n" 18528 " - 'proto'\n" 18529 " EnclosingFunctions:\n" 18530 " - 'PARSE_TEXT_PROTO'\n" 18531 " BasedOnStyle: llvm\n" 18532 " - Language: Cpp\n" 18533 " Delimiters:\n" 18534 " - 'cc'\n" 18535 " - 'cpp'\n" 18536 " EnclosingFunctions:\n" 18537 " - 'C_CODEBLOCK'\n" 18538 " - 'CPPEVAL'\n" 18539 " CanonicalDelimiter: 'cc'", 18540 RawStringFormats, ExpectedRawStringFormats); 18541 18542 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18543 " Minimum: 0\n" 18544 " Maximum: 0", 18545 SpacesInLineCommentPrefix.Minimum, 0u); 18546 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 18547 Style.SpacesInLineCommentPrefix.Minimum = 1; 18548 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18549 " Minimum: 2", 18550 SpacesInLineCommentPrefix.Minimum, 0u); 18551 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18552 " Maximum: -1", 18553 SpacesInLineCommentPrefix.Maximum, -1u); 18554 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18555 " Minimum: 2", 18556 SpacesInLineCommentPrefix.Minimum, 2u); 18557 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18558 " Maximum: 1", 18559 SpacesInLineCommentPrefix.Maximum, 1u); 18560 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 18561 18562 Style.SpacesInAngles = FormatStyle::SIAS_Always; 18563 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 18564 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 18565 FormatStyle::SIAS_Always); 18566 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 18567 // For backward compatibility: 18568 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 18569 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 18570 } 18571 18572 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 18573 FormatStyle Style = {}; 18574 Style.Language = FormatStyle::LK_Cpp; 18575 CHECK_PARSE("Language: Cpp\n" 18576 "IndentWidth: 12", 18577 IndentWidth, 12u); 18578 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 18579 "IndentWidth: 34", 18580 &Style), 18581 ParseError::Unsuitable); 18582 FormatStyle BinPackedTCS = {}; 18583 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 18584 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 18585 "InsertTrailingCommas: Wrapped", 18586 &BinPackedTCS), 18587 ParseError::BinPackTrailingCommaConflict); 18588 EXPECT_EQ(12u, Style.IndentWidth); 18589 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 18590 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18591 18592 Style.Language = FormatStyle::LK_JavaScript; 18593 CHECK_PARSE("Language: JavaScript\n" 18594 "IndentWidth: 12", 18595 IndentWidth, 12u); 18596 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 18597 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 18598 "IndentWidth: 34", 18599 &Style), 18600 ParseError::Unsuitable); 18601 EXPECT_EQ(23u, Style.IndentWidth); 18602 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 18603 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 18604 18605 CHECK_PARSE("BasedOnStyle: LLVM\n" 18606 "IndentWidth: 67", 18607 IndentWidth, 67u); 18608 18609 CHECK_PARSE("---\n" 18610 "Language: JavaScript\n" 18611 "IndentWidth: 12\n" 18612 "---\n" 18613 "Language: Cpp\n" 18614 "IndentWidth: 34\n" 18615 "...\n", 18616 IndentWidth, 12u); 18617 18618 Style.Language = FormatStyle::LK_Cpp; 18619 CHECK_PARSE("---\n" 18620 "Language: JavaScript\n" 18621 "IndentWidth: 12\n" 18622 "---\n" 18623 "Language: Cpp\n" 18624 "IndentWidth: 34\n" 18625 "...\n", 18626 IndentWidth, 34u); 18627 CHECK_PARSE("---\n" 18628 "IndentWidth: 78\n" 18629 "---\n" 18630 "Language: JavaScript\n" 18631 "IndentWidth: 56\n" 18632 "...\n", 18633 IndentWidth, 78u); 18634 18635 Style.ColumnLimit = 123; 18636 Style.IndentWidth = 234; 18637 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 18638 Style.TabWidth = 345; 18639 EXPECT_FALSE(parseConfiguration("---\n" 18640 "IndentWidth: 456\n" 18641 "BreakBeforeBraces: Allman\n" 18642 "---\n" 18643 "Language: JavaScript\n" 18644 "IndentWidth: 111\n" 18645 "TabWidth: 111\n" 18646 "---\n" 18647 "Language: Cpp\n" 18648 "BreakBeforeBraces: Stroustrup\n" 18649 "TabWidth: 789\n" 18650 "...\n", 18651 &Style)); 18652 EXPECT_EQ(123u, Style.ColumnLimit); 18653 EXPECT_EQ(456u, Style.IndentWidth); 18654 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 18655 EXPECT_EQ(789u, Style.TabWidth); 18656 18657 EXPECT_EQ(parseConfiguration("---\n" 18658 "Language: JavaScript\n" 18659 "IndentWidth: 56\n" 18660 "---\n" 18661 "IndentWidth: 78\n" 18662 "...\n", 18663 &Style), 18664 ParseError::Error); 18665 EXPECT_EQ(parseConfiguration("---\n" 18666 "Language: JavaScript\n" 18667 "IndentWidth: 56\n" 18668 "---\n" 18669 "Language: JavaScript\n" 18670 "IndentWidth: 78\n" 18671 "...\n", 18672 &Style), 18673 ParseError::Error); 18674 18675 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18676 } 18677 18678 #undef CHECK_PARSE 18679 18680 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 18681 FormatStyle Style = {}; 18682 Style.Language = FormatStyle::LK_JavaScript; 18683 Style.BreakBeforeTernaryOperators = true; 18684 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 18685 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18686 18687 Style.BreakBeforeTernaryOperators = true; 18688 EXPECT_EQ(0, parseConfiguration("---\n" 18689 "BasedOnStyle: Google\n" 18690 "---\n" 18691 "Language: JavaScript\n" 18692 "IndentWidth: 76\n" 18693 "...\n", 18694 &Style) 18695 .value()); 18696 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18697 EXPECT_EQ(76u, Style.IndentWidth); 18698 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 18699 } 18700 18701 TEST_F(FormatTest, ConfigurationRoundTripTest) { 18702 FormatStyle Style = getLLVMStyle(); 18703 std::string YAML = configurationAsText(Style); 18704 FormatStyle ParsedStyle = {}; 18705 ParsedStyle.Language = FormatStyle::LK_Cpp; 18706 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 18707 EXPECT_EQ(Style, ParsedStyle); 18708 } 18709 18710 TEST_F(FormatTest, WorksFor8bitEncodings) { 18711 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 18712 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 18713 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 18714 "\"\xef\xee\xf0\xf3...\"", 18715 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 18716 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 18717 "\xef\xee\xf0\xf3...\"", 18718 getLLVMStyleWithColumns(12))); 18719 } 18720 18721 TEST_F(FormatTest, HandlesUTF8BOM) { 18722 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 18723 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 18724 format("\xef\xbb\xbf#include <iostream>")); 18725 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 18726 format("\xef\xbb\xbf\n#include <iostream>")); 18727 } 18728 18729 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 18730 #if !defined(_MSC_VER) 18731 18732 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 18733 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 18734 getLLVMStyleWithColumns(35)); 18735 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 18736 getLLVMStyleWithColumns(31)); 18737 verifyFormat("// Однажды в студёную зимнюю пору...", 18738 getLLVMStyleWithColumns(36)); 18739 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 18740 verifyFormat("/* Однажды в студёную зимнюю пору... */", 18741 getLLVMStyleWithColumns(39)); 18742 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 18743 getLLVMStyleWithColumns(35)); 18744 } 18745 18746 TEST_F(FormatTest, SplitsUTF8Strings) { 18747 // Non-printable characters' width is currently considered to be the length in 18748 // bytes in UTF8. The characters can be displayed in very different manner 18749 // (zero-width, single width with a substitution glyph, expanded to their code 18750 // (e.g. "<8d>"), so there's no single correct way to handle them. 18751 EXPECT_EQ("\"aaaaÄ\"\n" 18752 "\"\xc2\x8d\";", 18753 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 18754 EXPECT_EQ("\"aaaaaaaÄ\"\n" 18755 "\"\xc2\x8d\";", 18756 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 18757 EXPECT_EQ("\"Однажды, в \"\n" 18758 "\"студёную \"\n" 18759 "\"зимнюю \"\n" 18760 "\"пору,\"", 18761 format("\"Однажды, в студёную зимнюю пору,\"", 18762 getLLVMStyleWithColumns(13))); 18763 EXPECT_EQ( 18764 "\"一 二 三 \"\n" 18765 "\"四 五六 \"\n" 18766 "\"七 八 九 \"\n" 18767 "\"十\"", 18768 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 18769 EXPECT_EQ("\"一\t\"\n" 18770 "\"二 \t\"\n" 18771 "\"三 四 \"\n" 18772 "\"五\t\"\n" 18773 "\"六 \t\"\n" 18774 "\"七 \"\n" 18775 "\"八九十\tqq\"", 18776 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 18777 getLLVMStyleWithColumns(11))); 18778 18779 // UTF8 character in an escape sequence. 18780 EXPECT_EQ("\"aaaaaa\"\n" 18781 "\"\\\xC2\x8D\"", 18782 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 18783 } 18784 18785 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 18786 EXPECT_EQ("const char *sssss =\n" 18787 " \"一二三四五六七八\\\n" 18788 " 九 十\";", 18789 format("const char *sssss = \"一二三四五六七八\\\n" 18790 " 九 十\";", 18791 getLLVMStyleWithColumns(30))); 18792 } 18793 18794 TEST_F(FormatTest, SplitsUTF8LineComments) { 18795 EXPECT_EQ("// aaaaÄ\xc2\x8d", 18796 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 18797 EXPECT_EQ("// Я из лесу\n" 18798 "// вышел; был\n" 18799 "// сильный\n" 18800 "// мороз.", 18801 format("// Я из лесу вышел; был сильный мороз.", 18802 getLLVMStyleWithColumns(13))); 18803 EXPECT_EQ("// 一二三\n" 18804 "// 四五六七\n" 18805 "// 八 九\n" 18806 "// 十", 18807 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 18808 } 18809 18810 TEST_F(FormatTest, SplitsUTF8BlockComments) { 18811 EXPECT_EQ("/* Гляжу,\n" 18812 " * поднимается\n" 18813 " * медленно в\n" 18814 " * гору\n" 18815 " * Лошадка,\n" 18816 " * везущая\n" 18817 " * хворосту\n" 18818 " * воз. */", 18819 format("/* Гляжу, поднимается медленно в гору\n" 18820 " * Лошадка, везущая хворосту воз. */", 18821 getLLVMStyleWithColumns(13))); 18822 EXPECT_EQ( 18823 "/* 一二三\n" 18824 " * 四五六七\n" 18825 " * 八 九\n" 18826 " * 十 */", 18827 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 18828 EXPECT_EQ("/* \n" 18829 " * \n" 18830 " * - */", 18831 format("/* - */", getLLVMStyleWithColumns(12))); 18832 } 18833 18834 #endif // _MSC_VER 18835 18836 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 18837 FormatStyle Style = getLLVMStyle(); 18838 18839 Style.ConstructorInitializerIndentWidth = 4; 18840 verifyFormat( 18841 "SomeClass::Constructor()\n" 18842 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 18843 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 18844 Style); 18845 18846 Style.ConstructorInitializerIndentWidth = 2; 18847 verifyFormat( 18848 "SomeClass::Constructor()\n" 18849 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 18850 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 18851 Style); 18852 18853 Style.ConstructorInitializerIndentWidth = 0; 18854 verifyFormat( 18855 "SomeClass::Constructor()\n" 18856 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 18857 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 18858 Style); 18859 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 18860 verifyFormat( 18861 "SomeLongTemplateVariableName<\n" 18862 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 18863 Style); 18864 verifyFormat("bool smaller = 1 < " 18865 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 18866 " " 18867 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 18868 Style); 18869 18870 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 18871 verifyFormat("SomeClass::Constructor() :\n" 18872 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 18873 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 18874 Style); 18875 } 18876 18877 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 18878 FormatStyle Style = getLLVMStyle(); 18879 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 18880 Style.ConstructorInitializerIndentWidth = 4; 18881 verifyFormat("SomeClass::Constructor()\n" 18882 " : a(a)\n" 18883 " , b(b)\n" 18884 " , c(c) {}", 18885 Style); 18886 verifyFormat("SomeClass::Constructor()\n" 18887 " : a(a) {}", 18888 Style); 18889 18890 Style.ColumnLimit = 0; 18891 verifyFormat("SomeClass::Constructor()\n" 18892 " : a(a) {}", 18893 Style); 18894 verifyFormat("SomeClass::Constructor() noexcept\n" 18895 " : a(a) {}", 18896 Style); 18897 verifyFormat("SomeClass::Constructor()\n" 18898 " : a(a)\n" 18899 " , b(b)\n" 18900 " , c(c) {}", 18901 Style); 18902 verifyFormat("SomeClass::Constructor()\n" 18903 " : a(a) {\n" 18904 " foo();\n" 18905 " bar();\n" 18906 "}", 18907 Style); 18908 18909 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 18910 verifyFormat("SomeClass::Constructor()\n" 18911 " : a(a)\n" 18912 " , b(b)\n" 18913 " , c(c) {\n}", 18914 Style); 18915 verifyFormat("SomeClass::Constructor()\n" 18916 " : a(a) {\n}", 18917 Style); 18918 18919 Style.ColumnLimit = 80; 18920 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 18921 Style.ConstructorInitializerIndentWidth = 2; 18922 verifyFormat("SomeClass::Constructor()\n" 18923 " : a(a)\n" 18924 " , b(b)\n" 18925 " , c(c) {}", 18926 Style); 18927 18928 Style.ConstructorInitializerIndentWidth = 0; 18929 verifyFormat("SomeClass::Constructor()\n" 18930 ": a(a)\n" 18931 ", b(b)\n" 18932 ", c(c) {}", 18933 Style); 18934 18935 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 18936 Style.ConstructorInitializerIndentWidth = 4; 18937 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 18938 verifyFormat( 18939 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 18940 Style); 18941 verifyFormat( 18942 "SomeClass::Constructor()\n" 18943 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 18944 Style); 18945 Style.ConstructorInitializerIndentWidth = 4; 18946 Style.ColumnLimit = 60; 18947 verifyFormat("SomeClass::Constructor()\n" 18948 " : aaaaaaaa(aaaaaaaa)\n" 18949 " , aaaaaaaa(aaaaaaaa)\n" 18950 " , aaaaaaaa(aaaaaaaa) {}", 18951 Style); 18952 } 18953 18954 TEST_F(FormatTest, Destructors) { 18955 verifyFormat("void F(int &i) { i.~int(); }"); 18956 verifyFormat("void F(int &i) { i->~int(); }"); 18957 } 18958 18959 TEST_F(FormatTest, FormatsWithWebKitStyle) { 18960 FormatStyle Style = getWebKitStyle(); 18961 18962 // Don't indent in outer namespaces. 18963 verifyFormat("namespace outer {\n" 18964 "int i;\n" 18965 "namespace inner {\n" 18966 " int i;\n" 18967 "} // namespace inner\n" 18968 "} // namespace outer\n" 18969 "namespace other_outer {\n" 18970 "int i;\n" 18971 "}", 18972 Style); 18973 18974 // Don't indent case labels. 18975 verifyFormat("switch (variable) {\n" 18976 "case 1:\n" 18977 "case 2:\n" 18978 " doSomething();\n" 18979 " break;\n" 18980 "default:\n" 18981 " ++variable;\n" 18982 "}", 18983 Style); 18984 18985 // Wrap before binary operators. 18986 EXPECT_EQ("void f()\n" 18987 "{\n" 18988 " if (aaaaaaaaaaaaaaaa\n" 18989 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 18990 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 18991 " return;\n" 18992 "}", 18993 format("void f() {\n" 18994 "if (aaaaaaaaaaaaaaaa\n" 18995 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 18996 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 18997 "return;\n" 18998 "}", 18999 Style)); 19000 19001 // Allow functions on a single line. 19002 verifyFormat("void f() { return; }", Style); 19003 19004 // Allow empty blocks on a single line and insert a space in empty blocks. 19005 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 19006 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 19007 // However, don't merge non-empty short loops. 19008 EXPECT_EQ("while (true) {\n" 19009 " continue;\n" 19010 "}", 19011 format("while (true) { continue; }", Style)); 19012 19013 // Constructor initializers are formatted one per line with the "," on the 19014 // new line. 19015 verifyFormat("Constructor()\n" 19016 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 19017 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 19018 " aaaaaaaaaaaaaa)\n" 19019 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 19020 "{\n" 19021 "}", 19022 Style); 19023 verifyFormat("SomeClass::Constructor()\n" 19024 " : a(a)\n" 19025 "{\n" 19026 "}", 19027 Style); 19028 EXPECT_EQ("SomeClass::Constructor()\n" 19029 " : a(a)\n" 19030 "{\n" 19031 "}", 19032 format("SomeClass::Constructor():a(a){}", Style)); 19033 verifyFormat("SomeClass::Constructor()\n" 19034 " : a(a)\n" 19035 " , b(b)\n" 19036 " , c(c)\n" 19037 "{\n" 19038 "}", 19039 Style); 19040 verifyFormat("SomeClass::Constructor()\n" 19041 " : a(a)\n" 19042 "{\n" 19043 " foo();\n" 19044 " bar();\n" 19045 "}", 19046 Style); 19047 19048 // Access specifiers should be aligned left. 19049 verifyFormat("class C {\n" 19050 "public:\n" 19051 " int i;\n" 19052 "};", 19053 Style); 19054 19055 // Do not align comments. 19056 verifyFormat("int a; // Do not\n" 19057 "double b; // align comments.", 19058 Style); 19059 19060 // Do not align operands. 19061 EXPECT_EQ("ASSERT(aaaa\n" 19062 " || bbbb);", 19063 format("ASSERT ( aaaa\n||bbbb);", Style)); 19064 19065 // Accept input's line breaks. 19066 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 19067 " || bbbbbbbbbbbbbbb) {\n" 19068 " i++;\n" 19069 "}", 19070 format("if (aaaaaaaaaaaaaaa\n" 19071 "|| bbbbbbbbbbbbbbb) { i++; }", 19072 Style)); 19073 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 19074 " i++;\n" 19075 "}", 19076 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 19077 19078 // Don't automatically break all macro definitions (llvm.org/PR17842). 19079 verifyFormat("#define aNumber 10", Style); 19080 // However, generally keep the line breaks that the user authored. 19081 EXPECT_EQ("#define aNumber \\\n" 19082 " 10", 19083 format("#define aNumber \\\n" 19084 " 10", 19085 Style)); 19086 19087 // Keep empty and one-element array literals on a single line. 19088 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 19089 " copyItems:YES];", 19090 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 19091 "copyItems:YES];", 19092 Style)); 19093 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 19094 " copyItems:YES];", 19095 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 19096 " copyItems:YES];", 19097 Style)); 19098 // FIXME: This does not seem right, there should be more indentation before 19099 // the array literal's entries. Nested blocks have the same problem. 19100 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19101 " @\"a\",\n" 19102 " @\"a\"\n" 19103 "]\n" 19104 " copyItems:YES];", 19105 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19106 " @\"a\",\n" 19107 " @\"a\"\n" 19108 " ]\n" 19109 " copyItems:YES];", 19110 Style)); 19111 EXPECT_EQ( 19112 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19113 " copyItems:YES];", 19114 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19115 " copyItems:YES];", 19116 Style)); 19117 19118 verifyFormat("[self.a b:c c:d];", Style); 19119 EXPECT_EQ("[self.a b:c\n" 19120 " c:d];", 19121 format("[self.a b:c\n" 19122 "c:d];", 19123 Style)); 19124 } 19125 19126 TEST_F(FormatTest, FormatsLambdas) { 19127 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 19128 verifyFormat( 19129 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 19130 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 19131 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 19132 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 19133 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 19134 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 19135 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 19136 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 19137 verifyFormat("int x = f(*+[] {});"); 19138 verifyFormat("void f() {\n" 19139 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 19140 "}\n"); 19141 verifyFormat("void f() {\n" 19142 " other(x.begin(), //\n" 19143 " x.end(), //\n" 19144 " [&](int, int) { return 1; });\n" 19145 "}\n"); 19146 verifyFormat("void f() {\n" 19147 " other.other.other.other.other(\n" 19148 " x.begin(), x.end(),\n" 19149 " [something, rather](int, int, int, int, int, int, int) { " 19150 "return 1; });\n" 19151 "}\n"); 19152 verifyFormat( 19153 "void f() {\n" 19154 " other.other.other.other.other(\n" 19155 " x.begin(), x.end(),\n" 19156 " [something, rather](int, int, int, int, int, int, int) {\n" 19157 " //\n" 19158 " });\n" 19159 "}\n"); 19160 verifyFormat("SomeFunction([]() { // A cool function...\n" 19161 " return 43;\n" 19162 "});"); 19163 EXPECT_EQ("SomeFunction([]() {\n" 19164 "#define A a\n" 19165 " return 43;\n" 19166 "});", 19167 format("SomeFunction([](){\n" 19168 "#define A a\n" 19169 "return 43;\n" 19170 "});")); 19171 verifyFormat("void f() {\n" 19172 " SomeFunction([](decltype(x), A *a) {});\n" 19173 " SomeFunction([](typeof(x), A *a) {});\n" 19174 " SomeFunction([](_Atomic(x), A *a) {});\n" 19175 " SomeFunction([](__underlying_type(x), A *a) {});\n" 19176 "}"); 19177 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19178 " [](const aaaaaaaaaa &a) { return a; });"); 19179 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 19180 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 19181 "});"); 19182 verifyFormat("Constructor()\n" 19183 " : Field([] { // comment\n" 19184 " int i;\n" 19185 " }) {}"); 19186 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 19187 " return some_parameter.size();\n" 19188 "};"); 19189 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 19190 " [](const string &s) { return s; };"); 19191 verifyFormat("int i = aaaaaa ? 1 //\n" 19192 " : [] {\n" 19193 " return 2; //\n" 19194 " }();"); 19195 verifyFormat("llvm::errs() << \"number of twos is \"\n" 19196 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 19197 " return x == 2; // force break\n" 19198 " });"); 19199 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19200 " [=](int iiiiiiiiiiii) {\n" 19201 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 19202 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 19203 " });", 19204 getLLVMStyleWithColumns(60)); 19205 19206 verifyFormat("SomeFunction({[&] {\n" 19207 " // comment\n" 19208 " },\n" 19209 " [&] {\n" 19210 " // comment\n" 19211 " }});"); 19212 verifyFormat("SomeFunction({[&] {\n" 19213 " // comment\n" 19214 "}});"); 19215 verifyFormat( 19216 "virtual aaaaaaaaaaaaaaaa(\n" 19217 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 19218 " aaaaa aaaaaaaaa);"); 19219 19220 // Lambdas with return types. 19221 verifyFormat("int c = []() -> int { return 2; }();\n"); 19222 verifyFormat("int c = []() -> int * { return 2; }();\n"); 19223 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 19224 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 19225 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 19226 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 19227 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 19228 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 19229 verifyFormat("[a, a]() -> a<1> {};"); 19230 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 19231 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 19232 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 19233 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 19234 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 19235 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 19236 verifyFormat("[]() -> foo<!5> { return {}; };"); 19237 verifyFormat("[]() -> foo<~5> { return {}; };"); 19238 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 19239 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 19240 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 19241 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 19242 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 19243 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 19244 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 19245 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 19246 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 19247 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 19248 verifyFormat("namespace bar {\n" 19249 "// broken:\n" 19250 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 19251 "} // namespace bar"); 19252 verifyFormat("namespace bar {\n" 19253 "// broken:\n" 19254 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 19255 "} // namespace bar"); 19256 verifyFormat("namespace bar {\n" 19257 "// broken:\n" 19258 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 19259 "} // namespace bar"); 19260 verifyFormat("namespace bar {\n" 19261 "// broken:\n" 19262 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 19263 "} // namespace bar"); 19264 verifyFormat("namespace bar {\n" 19265 "// broken:\n" 19266 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 19267 "} // namespace bar"); 19268 verifyFormat("namespace bar {\n" 19269 "// broken:\n" 19270 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 19271 "} // namespace bar"); 19272 verifyFormat("namespace bar {\n" 19273 "// broken:\n" 19274 "auto foo{[]() -> foo<!5> { return {}; }};\n" 19275 "} // namespace bar"); 19276 verifyFormat("namespace bar {\n" 19277 "// broken:\n" 19278 "auto foo{[]() -> foo<~5> { return {}; }};\n" 19279 "} // namespace bar"); 19280 verifyFormat("namespace bar {\n" 19281 "// broken:\n" 19282 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 19283 "} // namespace bar"); 19284 verifyFormat("namespace bar {\n" 19285 "// broken:\n" 19286 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 19287 "} // namespace bar"); 19288 verifyFormat("namespace bar {\n" 19289 "// broken:\n" 19290 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 19291 "} // namespace bar"); 19292 verifyFormat("namespace bar {\n" 19293 "// broken:\n" 19294 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 19295 "} // namespace bar"); 19296 verifyFormat("namespace bar {\n" 19297 "// broken:\n" 19298 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 19299 "} // namespace bar"); 19300 verifyFormat("namespace bar {\n" 19301 "// broken:\n" 19302 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 19303 "} // namespace bar"); 19304 verifyFormat("namespace bar {\n" 19305 "// broken:\n" 19306 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 19307 "} // namespace bar"); 19308 verifyFormat("namespace bar {\n" 19309 "// broken:\n" 19310 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 19311 "} // namespace bar"); 19312 verifyFormat("namespace bar {\n" 19313 "// broken:\n" 19314 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 19315 "} // namespace bar"); 19316 verifyFormat("namespace bar {\n" 19317 "// broken:\n" 19318 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 19319 "} // namespace bar"); 19320 verifyFormat("[]() -> a<1> {};"); 19321 verifyFormat("[]() -> a<1> { ; };"); 19322 verifyFormat("[]() -> a<1> { ; }();"); 19323 verifyFormat("[a, a]() -> a<true> {};"); 19324 verifyFormat("[]() -> a<true> {};"); 19325 verifyFormat("[]() -> a<true> { ; };"); 19326 verifyFormat("[]() -> a<true> { ; }();"); 19327 verifyFormat("[a, a]() -> a<false> {};"); 19328 verifyFormat("[]() -> a<false> {};"); 19329 verifyFormat("[]() -> a<false> { ; };"); 19330 verifyFormat("[]() -> a<false> { ; }();"); 19331 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 19332 verifyFormat("namespace bar {\n" 19333 "auto foo{[]() -> foo<false> { ; }};\n" 19334 "} // namespace bar"); 19335 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 19336 " int j) -> int {\n" 19337 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 19338 "};"); 19339 verifyFormat( 19340 "aaaaaaaaaaaaaaaaaaaaaa(\n" 19341 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 19342 " return aaaaaaaaaaaaaaaaa;\n" 19343 " });", 19344 getLLVMStyleWithColumns(70)); 19345 verifyFormat("[]() //\n" 19346 " -> int {\n" 19347 " return 1; //\n" 19348 "};"); 19349 verifyFormat("[]() -> Void<T...> {};"); 19350 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 19351 19352 // Lambdas with explicit template argument lists. 19353 verifyFormat( 19354 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 19355 19356 // Multiple lambdas in the same parentheses change indentation rules. These 19357 // lambdas are forced to start on new lines. 19358 verifyFormat("SomeFunction(\n" 19359 " []() {\n" 19360 " //\n" 19361 " },\n" 19362 " []() {\n" 19363 " //\n" 19364 " });"); 19365 19366 // A lambda passed as arg0 is always pushed to the next line. 19367 verifyFormat("SomeFunction(\n" 19368 " [this] {\n" 19369 " //\n" 19370 " },\n" 19371 " 1);\n"); 19372 19373 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 19374 // the arg0 case above. 19375 auto Style = getGoogleStyle(); 19376 Style.BinPackArguments = false; 19377 verifyFormat("SomeFunction(\n" 19378 " a,\n" 19379 " [this] {\n" 19380 " //\n" 19381 " },\n" 19382 " b);\n", 19383 Style); 19384 verifyFormat("SomeFunction(\n" 19385 " a,\n" 19386 " [this] {\n" 19387 " //\n" 19388 " },\n" 19389 " b);\n"); 19390 19391 // A lambda with a very long line forces arg0 to be pushed out irrespective of 19392 // the BinPackArguments value (as long as the code is wide enough). 19393 verifyFormat( 19394 "something->SomeFunction(\n" 19395 " a,\n" 19396 " [this] {\n" 19397 " " 19398 "D0000000000000000000000000000000000000000000000000000000000001();\n" 19399 " },\n" 19400 " b);\n"); 19401 19402 // A multi-line lambda is pulled up as long as the introducer fits on the 19403 // previous line and there are no further args. 19404 verifyFormat("function(1, [this, that] {\n" 19405 " //\n" 19406 "});\n"); 19407 verifyFormat("function([this, that] {\n" 19408 " //\n" 19409 "});\n"); 19410 // FIXME: this format is not ideal and we should consider forcing the first 19411 // arg onto its own line. 19412 verifyFormat("function(a, b, c, //\n" 19413 " d, [this, that] {\n" 19414 " //\n" 19415 " });\n"); 19416 19417 // Multiple lambdas are treated correctly even when there is a short arg0. 19418 verifyFormat("SomeFunction(\n" 19419 " 1,\n" 19420 " [this] {\n" 19421 " //\n" 19422 " },\n" 19423 " [this] {\n" 19424 " //\n" 19425 " },\n" 19426 " 1);\n"); 19427 19428 // More complex introducers. 19429 verifyFormat("return [i, args...] {};"); 19430 19431 // Not lambdas. 19432 verifyFormat("constexpr char hello[]{\"hello\"};"); 19433 verifyFormat("double &operator[](int i) { return 0; }\n" 19434 "int i;"); 19435 verifyFormat("std::unique_ptr<int[]> foo() {}"); 19436 verifyFormat("int i = a[a][a]->f();"); 19437 verifyFormat("int i = (*b)[a]->f();"); 19438 19439 // Other corner cases. 19440 verifyFormat("void f() {\n" 19441 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 19442 " );\n" 19443 "}"); 19444 19445 // Lambdas created through weird macros. 19446 verifyFormat("void f() {\n" 19447 " MACRO((const AA &a) { return 1; });\n" 19448 " MACRO((AA &a) { return 1; });\n" 19449 "}"); 19450 19451 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 19452 " doo_dah();\n" 19453 " doo_dah();\n" 19454 " })) {\n" 19455 "}"); 19456 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 19457 " doo_dah();\n" 19458 " doo_dah();\n" 19459 " })) {\n" 19460 "}"); 19461 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 19462 " doo_dah();\n" 19463 " doo_dah();\n" 19464 " })) {\n" 19465 "}"); 19466 verifyFormat("auto lambda = []() {\n" 19467 " int a = 2\n" 19468 "#if A\n" 19469 " + 2\n" 19470 "#endif\n" 19471 " ;\n" 19472 "};"); 19473 19474 // Lambdas with complex multiline introducers. 19475 verifyFormat( 19476 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19477 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 19478 " -> ::std::unordered_set<\n" 19479 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 19480 " //\n" 19481 " });"); 19482 19483 FormatStyle DoNotMerge = getLLVMStyle(); 19484 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 19485 verifyFormat("auto c = []() {\n" 19486 " return b;\n" 19487 "};", 19488 "auto c = []() { return b; };", DoNotMerge); 19489 verifyFormat("auto c = []() {\n" 19490 "};", 19491 " auto c = []() {};", DoNotMerge); 19492 19493 FormatStyle MergeEmptyOnly = getLLVMStyle(); 19494 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 19495 verifyFormat("auto c = []() {\n" 19496 " return b;\n" 19497 "};", 19498 "auto c = []() {\n" 19499 " return b;\n" 19500 " };", 19501 MergeEmptyOnly); 19502 verifyFormat("auto c = []() {};", 19503 "auto c = []() {\n" 19504 "};", 19505 MergeEmptyOnly); 19506 19507 FormatStyle MergeInline = getLLVMStyle(); 19508 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 19509 verifyFormat("auto c = []() {\n" 19510 " return b;\n" 19511 "};", 19512 "auto c = []() { return b; };", MergeInline); 19513 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 19514 MergeInline); 19515 verifyFormat("function([]() { return b; }, a)", 19516 "function([]() { return b; }, a)", MergeInline); 19517 verifyFormat("function(a, []() { return b; })", 19518 "function(a, []() { return b; })", MergeInline); 19519 19520 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 19521 // AllowShortLambdasOnASingleLine 19522 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 19523 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 19524 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 19525 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19526 FormatStyle::ShortLambdaStyle::SLS_None; 19527 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 19528 " []()\n" 19529 " {\n" 19530 " return 17;\n" 19531 " });", 19532 LLVMWithBeforeLambdaBody); 19533 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 19534 " []()\n" 19535 " {\n" 19536 " });", 19537 LLVMWithBeforeLambdaBody); 19538 verifyFormat("auto fct_SLS_None = []()\n" 19539 "{\n" 19540 " return 17;\n" 19541 "};", 19542 LLVMWithBeforeLambdaBody); 19543 verifyFormat("TwoNestedLambdas_SLS_None(\n" 19544 " []()\n" 19545 " {\n" 19546 " return Call(\n" 19547 " []()\n" 19548 " {\n" 19549 " return 17;\n" 19550 " });\n" 19551 " });", 19552 LLVMWithBeforeLambdaBody); 19553 verifyFormat("void Fct()\n" 19554 "{\n" 19555 " return {[]()\n" 19556 " {\n" 19557 " return 17;\n" 19558 " }};\n" 19559 "}", 19560 LLVMWithBeforeLambdaBody); 19561 19562 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19563 FormatStyle::ShortLambdaStyle::SLS_Empty; 19564 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 19565 " []()\n" 19566 " {\n" 19567 " return 17;\n" 19568 " });", 19569 LLVMWithBeforeLambdaBody); 19570 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 19571 LLVMWithBeforeLambdaBody); 19572 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 19573 "ongFunctionName_SLS_Empty(\n" 19574 " []() {});", 19575 LLVMWithBeforeLambdaBody); 19576 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 19577 " []()\n" 19578 " {\n" 19579 " return 17;\n" 19580 " });", 19581 LLVMWithBeforeLambdaBody); 19582 verifyFormat("auto fct_SLS_Empty = []()\n" 19583 "{\n" 19584 " return 17;\n" 19585 "};", 19586 LLVMWithBeforeLambdaBody); 19587 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 19588 " []()\n" 19589 " {\n" 19590 " return Call([]() {});\n" 19591 " });", 19592 LLVMWithBeforeLambdaBody); 19593 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 19594 " []()\n" 19595 " {\n" 19596 " return Call([]() {});\n" 19597 " });", 19598 LLVMWithBeforeLambdaBody); 19599 verifyFormat( 19600 "FctWithLongLineInLambda_SLS_Empty(\n" 19601 " []()\n" 19602 " {\n" 19603 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19604 " AndShouldNotBeConsiderAsInline,\n" 19605 " LambdaBodyMustBeBreak);\n" 19606 " });", 19607 LLVMWithBeforeLambdaBody); 19608 19609 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19610 FormatStyle::ShortLambdaStyle::SLS_Inline; 19611 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 19612 LLVMWithBeforeLambdaBody); 19613 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 19614 LLVMWithBeforeLambdaBody); 19615 verifyFormat("auto fct_SLS_Inline = []()\n" 19616 "{\n" 19617 " return 17;\n" 19618 "};", 19619 LLVMWithBeforeLambdaBody); 19620 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 19621 "17; }); });", 19622 LLVMWithBeforeLambdaBody); 19623 verifyFormat( 19624 "FctWithLongLineInLambda_SLS_Inline(\n" 19625 " []()\n" 19626 " {\n" 19627 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19628 " AndShouldNotBeConsiderAsInline,\n" 19629 " LambdaBodyMustBeBreak);\n" 19630 " });", 19631 LLVMWithBeforeLambdaBody); 19632 verifyFormat("FctWithMultipleParams_SLS_Inline(" 19633 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 19634 " []() { return 17; });", 19635 LLVMWithBeforeLambdaBody); 19636 verifyFormat( 19637 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 19638 LLVMWithBeforeLambdaBody); 19639 19640 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19641 FormatStyle::ShortLambdaStyle::SLS_All; 19642 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 19643 LLVMWithBeforeLambdaBody); 19644 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 19645 LLVMWithBeforeLambdaBody); 19646 verifyFormat("auto fct_SLS_All = []() { return 17; };", 19647 LLVMWithBeforeLambdaBody); 19648 verifyFormat("FctWithOneParam_SLS_All(\n" 19649 " []()\n" 19650 " {\n" 19651 " // A cool function...\n" 19652 " return 43;\n" 19653 " });", 19654 LLVMWithBeforeLambdaBody); 19655 verifyFormat("FctWithMultipleParams_SLS_All(" 19656 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 19657 " []() { return 17; });", 19658 LLVMWithBeforeLambdaBody); 19659 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 19660 LLVMWithBeforeLambdaBody); 19661 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 19662 LLVMWithBeforeLambdaBody); 19663 verifyFormat( 19664 "FctWithLongLineInLambda_SLS_All(\n" 19665 " []()\n" 19666 " {\n" 19667 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19668 " AndShouldNotBeConsiderAsInline,\n" 19669 " LambdaBodyMustBeBreak);\n" 19670 " });", 19671 LLVMWithBeforeLambdaBody); 19672 verifyFormat( 19673 "auto fct_SLS_All = []()\n" 19674 "{\n" 19675 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19676 " AndShouldNotBeConsiderAsInline,\n" 19677 " LambdaBodyMustBeBreak);\n" 19678 "};", 19679 LLVMWithBeforeLambdaBody); 19680 LLVMWithBeforeLambdaBody.BinPackParameters = false; 19681 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 19682 LLVMWithBeforeLambdaBody); 19683 verifyFormat( 19684 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 19685 " FirstParam,\n" 19686 " SecondParam,\n" 19687 " ThirdParam,\n" 19688 " FourthParam);", 19689 LLVMWithBeforeLambdaBody); 19690 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19691 " []() { return " 19692 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 19693 " FirstParam,\n" 19694 " SecondParam,\n" 19695 " ThirdParam,\n" 19696 " FourthParam);", 19697 LLVMWithBeforeLambdaBody); 19698 verifyFormat( 19699 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 19700 " SecondParam,\n" 19701 " ThirdParam,\n" 19702 " FourthParam,\n" 19703 " []() { return SomeValueNotSoLong; });", 19704 LLVMWithBeforeLambdaBody); 19705 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19706 " []()\n" 19707 " {\n" 19708 " return " 19709 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 19710 "eConsiderAsInline;\n" 19711 " });", 19712 LLVMWithBeforeLambdaBody); 19713 verifyFormat( 19714 "FctWithLongLineInLambda_SLS_All(\n" 19715 " []()\n" 19716 " {\n" 19717 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19718 " AndShouldNotBeConsiderAsInline,\n" 19719 " LambdaBodyMustBeBreak);\n" 19720 " });", 19721 LLVMWithBeforeLambdaBody); 19722 verifyFormat("FctWithTwoParams_SLS_All(\n" 19723 " []()\n" 19724 " {\n" 19725 " // A cool function...\n" 19726 " return 43;\n" 19727 " },\n" 19728 " 87);", 19729 LLVMWithBeforeLambdaBody); 19730 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 19731 LLVMWithBeforeLambdaBody); 19732 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 19733 LLVMWithBeforeLambdaBody); 19734 verifyFormat( 19735 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 19736 LLVMWithBeforeLambdaBody); 19737 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 19738 "}); }, x);", 19739 LLVMWithBeforeLambdaBody); 19740 verifyFormat("TwoNestedLambdas_SLS_All(\n" 19741 " []()\n" 19742 " {\n" 19743 " // A cool function...\n" 19744 " return Call([]() { return 17; });\n" 19745 " });", 19746 LLVMWithBeforeLambdaBody); 19747 verifyFormat("TwoNestedLambdas_SLS_All(\n" 19748 " []()\n" 19749 " {\n" 19750 " return Call(\n" 19751 " []()\n" 19752 " {\n" 19753 " // A cool function...\n" 19754 " return 17;\n" 19755 " });\n" 19756 " });", 19757 LLVMWithBeforeLambdaBody); 19758 19759 // Lambdas with different indentation styles. 19760 Style = getLLVMStyleWithColumns(100); 19761 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 19762 " return promise.then(\n" 19763 " [this, &someVariable, someObject = " 19764 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 19765 " return someObject.startAsyncAction().then(\n" 19766 " [this, &someVariable](AsyncActionResult result) " 19767 "mutable { result.processMore(); });\n" 19768 " });\n" 19769 "}\n", 19770 format("SomeResult doSomething(SomeObject promise) {\n" 19771 " return promise.then([this, &someVariable, someObject = " 19772 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 19773 " return someObject.startAsyncAction().then([this, " 19774 "&someVariable](AsyncActionResult result) mutable {\n" 19775 " result.processMore();\n" 19776 " });\n" 19777 " });\n" 19778 "}\n", 19779 Style)); 19780 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 19781 verifyFormat("test() {\n" 19782 " ([]() -> {\n" 19783 " int b = 32;\n" 19784 " return 3;\n" 19785 " }).foo();\n" 19786 "}", 19787 Style); 19788 verifyFormat("test() {\n" 19789 " []() -> {\n" 19790 " int b = 32;\n" 19791 " return 3;\n" 19792 " }\n" 19793 "}", 19794 Style); 19795 verifyFormat("std::sort(v.begin(), v.end(),\n" 19796 " [](const auto &someLongArgumentName, const auto " 19797 "&someOtherLongArgumentName) {\n" 19798 " return someLongArgumentName.someMemberVariable < " 19799 "someOtherLongArgumentName.someMemberVariable;\n" 19800 "});", 19801 Style); 19802 verifyFormat("test() {\n" 19803 " (\n" 19804 " []() -> {\n" 19805 " int b = 32;\n" 19806 " return 3;\n" 19807 " },\n" 19808 " foo, bar)\n" 19809 " .foo();\n" 19810 "}", 19811 Style); 19812 verifyFormat("test() {\n" 19813 " ([]() -> {\n" 19814 " int b = 32;\n" 19815 " return 3;\n" 19816 " })\n" 19817 " .foo()\n" 19818 " .bar();\n" 19819 "}", 19820 Style); 19821 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 19822 " return promise.then(\n" 19823 " [this, &someVariable, someObject = " 19824 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 19825 " return someObject.startAsyncAction().then(\n" 19826 " [this, &someVariable](AsyncActionResult result) mutable { " 19827 "result.processMore(); });\n" 19828 " });\n" 19829 "}\n", 19830 format("SomeResult doSomething(SomeObject promise) {\n" 19831 " return promise.then([this, &someVariable, someObject = " 19832 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 19833 " return someObject.startAsyncAction().then([this, " 19834 "&someVariable](AsyncActionResult result) mutable {\n" 19835 " result.processMore();\n" 19836 " });\n" 19837 " });\n" 19838 "}\n", 19839 Style)); 19840 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 19841 " return promise.then([this, &someVariable] {\n" 19842 " return someObject.startAsyncAction().then(\n" 19843 " [this, &someVariable](AsyncActionResult result) mutable { " 19844 "result.processMore(); });\n" 19845 " });\n" 19846 "}\n", 19847 format("SomeResult doSomething(SomeObject promise) {\n" 19848 " return promise.then([this, &someVariable] {\n" 19849 " return someObject.startAsyncAction().then([this, " 19850 "&someVariable](AsyncActionResult result) mutable {\n" 19851 " result.processMore();\n" 19852 " });\n" 19853 " });\n" 19854 "}\n", 19855 Style)); 19856 Style = getGoogleStyle(); 19857 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 19858 EXPECT_EQ("#define A \\\n" 19859 " [] { \\\n" 19860 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 19861 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 19862 " }", 19863 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 19864 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 19865 Style)); 19866 // TODO: The current formatting has a minor issue that's not worth fixing 19867 // right now whereby the closing brace is indented relative to the signature 19868 // instead of being aligned. This only happens with macros. 19869 } 19870 19871 TEST_F(FormatTest, LambdaWithLineComments) { 19872 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 19873 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 19874 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 19875 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19876 FormatStyle::ShortLambdaStyle::SLS_All; 19877 19878 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 19879 verifyFormat("auto k = []() // comment\n" 19880 "{ return; }", 19881 LLVMWithBeforeLambdaBody); 19882 verifyFormat("auto k = []() /* comment */ { return; }", 19883 LLVMWithBeforeLambdaBody); 19884 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 19885 LLVMWithBeforeLambdaBody); 19886 verifyFormat("auto k = []() // X\n" 19887 "{ return; }", 19888 LLVMWithBeforeLambdaBody); 19889 verifyFormat( 19890 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 19891 "{ return; }", 19892 LLVMWithBeforeLambdaBody); 19893 } 19894 19895 TEST_F(FormatTest, EmptyLinesInLambdas) { 19896 verifyFormat("auto lambda = []() {\n" 19897 " x(); //\n" 19898 "};", 19899 "auto lambda = []() {\n" 19900 "\n" 19901 " x(); //\n" 19902 "\n" 19903 "};"); 19904 } 19905 19906 TEST_F(FormatTest, FormatsBlocks) { 19907 FormatStyle ShortBlocks = getLLVMStyle(); 19908 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 19909 verifyFormat("int (^Block)(int, int);", ShortBlocks); 19910 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 19911 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 19912 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 19913 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 19914 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 19915 19916 verifyFormat("foo(^{ bar(); });", ShortBlocks); 19917 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 19918 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 19919 19920 verifyFormat("[operation setCompletionBlock:^{\n" 19921 " [self onOperationDone];\n" 19922 "}];"); 19923 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 19924 " [self onOperationDone];\n" 19925 "}]};"); 19926 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 19927 " f();\n" 19928 "}];"); 19929 verifyFormat("int a = [operation block:^int(int *i) {\n" 19930 " return 1;\n" 19931 "}];"); 19932 verifyFormat("[myObject doSomethingWith:arg1\n" 19933 " aaa:^int(int *a) {\n" 19934 " return 1;\n" 19935 " }\n" 19936 " bbb:f(a * bbbbbbbb)];"); 19937 19938 verifyFormat("[operation setCompletionBlock:^{\n" 19939 " [self.delegate newDataAvailable];\n" 19940 "}];", 19941 getLLVMStyleWithColumns(60)); 19942 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 19943 " NSString *path = [self sessionFilePath];\n" 19944 " if (path) {\n" 19945 " // ...\n" 19946 " }\n" 19947 "});"); 19948 verifyFormat("[[SessionService sharedService]\n" 19949 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 19950 " if (window) {\n" 19951 " [self windowDidLoad:window];\n" 19952 " } else {\n" 19953 " [self errorLoadingWindow];\n" 19954 " }\n" 19955 " }];"); 19956 verifyFormat("void (^largeBlock)(void) = ^{\n" 19957 " // ...\n" 19958 "};\n", 19959 getLLVMStyleWithColumns(40)); 19960 verifyFormat("[[SessionService sharedService]\n" 19961 " loadWindowWithCompletionBlock: //\n" 19962 " ^(SessionWindow *window) {\n" 19963 " if (window) {\n" 19964 " [self windowDidLoad:window];\n" 19965 " } else {\n" 19966 " [self errorLoadingWindow];\n" 19967 " }\n" 19968 " }];", 19969 getLLVMStyleWithColumns(60)); 19970 verifyFormat("[myObject doSomethingWith:arg1\n" 19971 " firstBlock:^(Foo *a) {\n" 19972 " // ...\n" 19973 " int i;\n" 19974 " }\n" 19975 " secondBlock:^(Bar *b) {\n" 19976 " // ...\n" 19977 " int i;\n" 19978 " }\n" 19979 " thirdBlock:^Foo(Bar *b) {\n" 19980 " // ...\n" 19981 " int i;\n" 19982 " }];"); 19983 verifyFormat("[myObject doSomethingWith:arg1\n" 19984 " firstBlock:-1\n" 19985 " secondBlock:^(Bar *b) {\n" 19986 " // ...\n" 19987 " int i;\n" 19988 " }];"); 19989 19990 verifyFormat("f(^{\n" 19991 " @autoreleasepool {\n" 19992 " if (a) {\n" 19993 " g();\n" 19994 " }\n" 19995 " }\n" 19996 "});"); 19997 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 19998 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 19999 "};"); 20000 20001 FormatStyle FourIndent = getLLVMStyle(); 20002 FourIndent.ObjCBlockIndentWidth = 4; 20003 verifyFormat("[operation setCompletionBlock:^{\n" 20004 " [self onOperationDone];\n" 20005 "}];", 20006 FourIndent); 20007 } 20008 20009 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 20010 FormatStyle ZeroColumn = getLLVMStyle(); 20011 ZeroColumn.ColumnLimit = 0; 20012 20013 verifyFormat("[[SessionService sharedService] " 20014 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20015 " if (window) {\n" 20016 " [self windowDidLoad:window];\n" 20017 " } else {\n" 20018 " [self errorLoadingWindow];\n" 20019 " }\n" 20020 "}];", 20021 ZeroColumn); 20022 EXPECT_EQ("[[SessionService sharedService]\n" 20023 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20024 " if (window) {\n" 20025 " [self windowDidLoad:window];\n" 20026 " } else {\n" 20027 " [self errorLoadingWindow];\n" 20028 " }\n" 20029 " }];", 20030 format("[[SessionService sharedService]\n" 20031 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20032 " if (window) {\n" 20033 " [self windowDidLoad:window];\n" 20034 " } else {\n" 20035 " [self errorLoadingWindow];\n" 20036 " }\n" 20037 "}];", 20038 ZeroColumn)); 20039 verifyFormat("[myObject doSomethingWith:arg1\n" 20040 " firstBlock:^(Foo *a) {\n" 20041 " // ...\n" 20042 " int i;\n" 20043 " }\n" 20044 " secondBlock:^(Bar *b) {\n" 20045 " // ...\n" 20046 " int i;\n" 20047 " }\n" 20048 " thirdBlock:^Foo(Bar *b) {\n" 20049 " // ...\n" 20050 " int i;\n" 20051 " }];", 20052 ZeroColumn); 20053 verifyFormat("f(^{\n" 20054 " @autoreleasepool {\n" 20055 " if (a) {\n" 20056 " g();\n" 20057 " }\n" 20058 " }\n" 20059 "});", 20060 ZeroColumn); 20061 verifyFormat("void (^largeBlock)(void) = ^{\n" 20062 " // ...\n" 20063 "};", 20064 ZeroColumn); 20065 20066 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20067 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 20068 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20069 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 20070 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 20071 " int i;\n" 20072 "};", 20073 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20074 } 20075 20076 TEST_F(FormatTest, SupportsCRLF) { 20077 EXPECT_EQ("int a;\r\n" 20078 "int b;\r\n" 20079 "int c;\r\n", 20080 format("int a;\r\n" 20081 " int b;\r\n" 20082 " int c;\r\n", 20083 getLLVMStyle())); 20084 EXPECT_EQ("int a;\r\n" 20085 "int b;\r\n" 20086 "int c;\r\n", 20087 format("int a;\r\n" 20088 " int b;\n" 20089 " int c;\r\n", 20090 getLLVMStyle())); 20091 EXPECT_EQ("int a;\n" 20092 "int b;\n" 20093 "int c;\n", 20094 format("int a;\r\n" 20095 " int b;\n" 20096 " int c;\n", 20097 getLLVMStyle())); 20098 EXPECT_EQ("\"aaaaaaa \"\r\n" 20099 "\"bbbbbbb\";\r\n", 20100 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 20101 EXPECT_EQ("#define A \\\r\n" 20102 " b; \\\r\n" 20103 " c; \\\r\n" 20104 " d;\r\n", 20105 format("#define A \\\r\n" 20106 " b; \\\r\n" 20107 " c; d; \r\n", 20108 getGoogleStyle())); 20109 20110 EXPECT_EQ("/*\r\n" 20111 "multi line block comments\r\n" 20112 "should not introduce\r\n" 20113 "an extra carriage return\r\n" 20114 "*/\r\n", 20115 format("/*\r\n" 20116 "multi line block comments\r\n" 20117 "should not introduce\r\n" 20118 "an extra carriage return\r\n" 20119 "*/\r\n")); 20120 EXPECT_EQ("/*\r\n" 20121 "\r\n" 20122 "*/", 20123 format("/*\r\n" 20124 " \r\r\r\n" 20125 "*/")); 20126 20127 FormatStyle style = getLLVMStyle(); 20128 20129 style.DeriveLineEnding = true; 20130 style.UseCRLF = false; 20131 EXPECT_EQ("union FooBarBazQux {\n" 20132 " int foo;\n" 20133 " int bar;\n" 20134 " int baz;\n" 20135 "};", 20136 format("union FooBarBazQux {\r\n" 20137 " int foo;\n" 20138 " int bar;\r\n" 20139 " int baz;\n" 20140 "};", 20141 style)); 20142 style.UseCRLF = true; 20143 EXPECT_EQ("union FooBarBazQux {\r\n" 20144 " int foo;\r\n" 20145 " int bar;\r\n" 20146 " int baz;\r\n" 20147 "};", 20148 format("union FooBarBazQux {\r\n" 20149 " int foo;\n" 20150 " int bar;\r\n" 20151 " int baz;\n" 20152 "};", 20153 style)); 20154 20155 style.DeriveLineEnding = false; 20156 style.UseCRLF = false; 20157 EXPECT_EQ("union FooBarBazQux {\n" 20158 " int foo;\n" 20159 " int bar;\n" 20160 " int baz;\n" 20161 " int qux;\n" 20162 "};", 20163 format("union FooBarBazQux {\r\n" 20164 " int foo;\n" 20165 " int bar;\r\n" 20166 " int baz;\n" 20167 " int qux;\r\n" 20168 "};", 20169 style)); 20170 style.UseCRLF = true; 20171 EXPECT_EQ("union FooBarBazQux {\r\n" 20172 " int foo;\r\n" 20173 " int bar;\r\n" 20174 " int baz;\r\n" 20175 " int qux;\r\n" 20176 "};", 20177 format("union FooBarBazQux {\r\n" 20178 " int foo;\n" 20179 " int bar;\r\n" 20180 " int baz;\n" 20181 " int qux;\n" 20182 "};", 20183 style)); 20184 20185 style.DeriveLineEnding = true; 20186 style.UseCRLF = false; 20187 EXPECT_EQ("union FooBarBazQux {\r\n" 20188 " int foo;\r\n" 20189 " int bar;\r\n" 20190 " int baz;\r\n" 20191 " int qux;\r\n" 20192 "};", 20193 format("union FooBarBazQux {\r\n" 20194 " int foo;\n" 20195 " int bar;\r\n" 20196 " int baz;\n" 20197 " int qux;\r\n" 20198 "};", 20199 style)); 20200 style.UseCRLF = true; 20201 EXPECT_EQ("union FooBarBazQux {\n" 20202 " int foo;\n" 20203 " int bar;\n" 20204 " int baz;\n" 20205 " int qux;\n" 20206 "};", 20207 format("union FooBarBazQux {\r\n" 20208 " int foo;\n" 20209 " int bar;\r\n" 20210 " int baz;\n" 20211 " int qux;\n" 20212 "};", 20213 style)); 20214 } 20215 20216 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 20217 verifyFormat("MY_CLASS(C) {\n" 20218 " int i;\n" 20219 " int j;\n" 20220 "};"); 20221 } 20222 20223 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 20224 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 20225 TwoIndent.ContinuationIndentWidth = 2; 20226 20227 EXPECT_EQ("int i =\n" 20228 " longFunction(\n" 20229 " arg);", 20230 format("int i = longFunction(arg);", TwoIndent)); 20231 20232 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 20233 SixIndent.ContinuationIndentWidth = 6; 20234 20235 EXPECT_EQ("int i =\n" 20236 " longFunction(\n" 20237 " arg);", 20238 format("int i = longFunction(arg);", SixIndent)); 20239 } 20240 20241 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 20242 FormatStyle Style = getLLVMStyle(); 20243 verifyFormat("int Foo::getter(\n" 20244 " //\n" 20245 ") const {\n" 20246 " return foo;\n" 20247 "}", 20248 Style); 20249 verifyFormat("void Foo::setter(\n" 20250 " //\n" 20251 ") {\n" 20252 " foo = 1;\n" 20253 "}", 20254 Style); 20255 } 20256 20257 TEST_F(FormatTest, SpacesInAngles) { 20258 FormatStyle Spaces = getLLVMStyle(); 20259 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20260 20261 verifyFormat("vector< ::std::string > x1;", Spaces); 20262 verifyFormat("Foo< int, Bar > x2;", Spaces); 20263 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 20264 20265 verifyFormat("static_cast< int >(arg);", Spaces); 20266 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 20267 verifyFormat("f< int, float >();", Spaces); 20268 verifyFormat("template <> g() {}", Spaces); 20269 verifyFormat("template < std::vector< int > > f() {}", Spaces); 20270 verifyFormat("std::function< void(int, int) > fct;", Spaces); 20271 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 20272 Spaces); 20273 20274 Spaces.Standard = FormatStyle::LS_Cpp03; 20275 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20276 verifyFormat("A< A< int > >();", Spaces); 20277 20278 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20279 verifyFormat("A<A<int> >();", Spaces); 20280 20281 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20282 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 20283 Spaces); 20284 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 20285 Spaces); 20286 20287 verifyFormat("A<A<int> >();", Spaces); 20288 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 20289 verifyFormat("A< A< int > >();", Spaces); 20290 20291 Spaces.Standard = FormatStyle::LS_Cpp11; 20292 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20293 verifyFormat("A< A< int > >();", Spaces); 20294 20295 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20296 verifyFormat("vector<::std::string> x4;", Spaces); 20297 verifyFormat("vector<int> x5;", Spaces); 20298 verifyFormat("Foo<int, Bar> x6;", Spaces); 20299 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20300 20301 verifyFormat("A<A<int>>();", Spaces); 20302 20303 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20304 verifyFormat("vector<::std::string> x4;", Spaces); 20305 verifyFormat("vector< ::std::string > x4;", Spaces); 20306 verifyFormat("vector<int> x5;", Spaces); 20307 verifyFormat("vector< int > x5;", Spaces); 20308 verifyFormat("Foo<int, Bar> x6;", Spaces); 20309 verifyFormat("Foo< int, Bar > x6;", Spaces); 20310 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20311 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 20312 20313 verifyFormat("A<A<int>>();", Spaces); 20314 verifyFormat("A< A< int > >();", Spaces); 20315 verifyFormat("A<A<int > >();", Spaces); 20316 verifyFormat("A< A< int>>();", Spaces); 20317 } 20318 20319 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 20320 FormatStyle Style = getLLVMStyle(); 20321 Style.SpaceAfterTemplateKeyword = false; 20322 verifyFormat("template<int> void foo();", Style); 20323 } 20324 20325 TEST_F(FormatTest, TripleAngleBrackets) { 20326 verifyFormat("f<<<1, 1>>>();"); 20327 verifyFormat("f<<<1, 1, 1, s>>>();"); 20328 verifyFormat("f<<<a, b, c, d>>>();"); 20329 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 20330 verifyFormat("f<param><<<1, 1>>>();"); 20331 verifyFormat("f<1><<<1, 1>>>();"); 20332 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 20333 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20334 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 20335 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 20336 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 20337 } 20338 20339 TEST_F(FormatTest, MergeLessLessAtEnd) { 20340 verifyFormat("<<"); 20341 EXPECT_EQ("< < <", format("\\\n<<<")); 20342 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20343 "aaallvm::outs() <<"); 20344 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20345 "aaaallvm::outs()\n <<"); 20346 } 20347 20348 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 20349 std::string code = "#if A\n" 20350 "#if B\n" 20351 "a.\n" 20352 "#endif\n" 20353 " a = 1;\n" 20354 "#else\n" 20355 "#endif\n" 20356 "#if C\n" 20357 "#else\n" 20358 "#endif\n"; 20359 EXPECT_EQ(code, format(code)); 20360 } 20361 20362 TEST_F(FormatTest, HandleConflictMarkers) { 20363 // Git/SVN conflict markers. 20364 EXPECT_EQ("int a;\n" 20365 "void f() {\n" 20366 " callme(some(parameter1,\n" 20367 "<<<<<<< text by the vcs\n" 20368 " parameter2),\n" 20369 "||||||| text by the vcs\n" 20370 " parameter2),\n" 20371 " parameter3,\n" 20372 "======= text by the vcs\n" 20373 " parameter2, parameter3),\n" 20374 ">>>>>>> text by the vcs\n" 20375 " otherparameter);\n", 20376 format("int a;\n" 20377 "void f() {\n" 20378 " callme(some(parameter1,\n" 20379 "<<<<<<< text by the vcs\n" 20380 " parameter2),\n" 20381 "||||||| text by the vcs\n" 20382 " parameter2),\n" 20383 " parameter3,\n" 20384 "======= text by the vcs\n" 20385 " parameter2,\n" 20386 " parameter3),\n" 20387 ">>>>>>> text by the vcs\n" 20388 " otherparameter);\n")); 20389 20390 // Perforce markers. 20391 EXPECT_EQ("void f() {\n" 20392 " function(\n" 20393 ">>>> text by the vcs\n" 20394 " parameter,\n" 20395 "==== text by the vcs\n" 20396 " parameter,\n" 20397 "==== text by the vcs\n" 20398 " parameter,\n" 20399 "<<<< text by the vcs\n" 20400 " parameter);\n", 20401 format("void f() {\n" 20402 " function(\n" 20403 ">>>> text by the vcs\n" 20404 " parameter,\n" 20405 "==== text by the vcs\n" 20406 " parameter,\n" 20407 "==== text by the vcs\n" 20408 " parameter,\n" 20409 "<<<< text by the vcs\n" 20410 " parameter);\n")); 20411 20412 EXPECT_EQ("<<<<<<<\n" 20413 "|||||||\n" 20414 "=======\n" 20415 ">>>>>>>", 20416 format("<<<<<<<\n" 20417 "|||||||\n" 20418 "=======\n" 20419 ">>>>>>>")); 20420 20421 EXPECT_EQ("<<<<<<<\n" 20422 "|||||||\n" 20423 "int i;\n" 20424 "=======\n" 20425 ">>>>>>>", 20426 format("<<<<<<<\n" 20427 "|||||||\n" 20428 "int i;\n" 20429 "=======\n" 20430 ">>>>>>>")); 20431 20432 // FIXME: Handle parsing of macros around conflict markers correctly: 20433 EXPECT_EQ("#define Macro \\\n" 20434 "<<<<<<<\n" 20435 "Something \\\n" 20436 "|||||||\n" 20437 "Else \\\n" 20438 "=======\n" 20439 "Other \\\n" 20440 ">>>>>>>\n" 20441 " End int i;\n", 20442 format("#define Macro \\\n" 20443 "<<<<<<<\n" 20444 " Something \\\n" 20445 "|||||||\n" 20446 " Else \\\n" 20447 "=======\n" 20448 " Other \\\n" 20449 ">>>>>>>\n" 20450 " End\n" 20451 "int i;\n")); 20452 } 20453 20454 TEST_F(FormatTest, DisableRegions) { 20455 EXPECT_EQ("int i;\n" 20456 "// clang-format off\n" 20457 " int j;\n" 20458 "// clang-format on\n" 20459 "int k;", 20460 format(" int i;\n" 20461 " // clang-format off\n" 20462 " int j;\n" 20463 " // clang-format on\n" 20464 " int k;")); 20465 EXPECT_EQ("int i;\n" 20466 "/* clang-format off */\n" 20467 " int j;\n" 20468 "/* clang-format on */\n" 20469 "int k;", 20470 format(" int i;\n" 20471 " /* clang-format off */\n" 20472 " int j;\n" 20473 " /* clang-format on */\n" 20474 " int k;")); 20475 20476 // Don't reflow comments within disabled regions. 20477 EXPECT_EQ("// clang-format off\n" 20478 "// long long long long long long line\n" 20479 "/* clang-format on */\n" 20480 "/* long long long\n" 20481 " * long long long\n" 20482 " * line */\n" 20483 "int i;\n" 20484 "/* clang-format off */\n" 20485 "/* long long long long long long line */\n", 20486 format("// clang-format off\n" 20487 "// long long long long long long line\n" 20488 "/* clang-format on */\n" 20489 "/* long long long long long long line */\n" 20490 "int i;\n" 20491 "/* clang-format off */\n" 20492 "/* long long long long long long line */\n", 20493 getLLVMStyleWithColumns(20))); 20494 } 20495 20496 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 20497 format("? ) ="); 20498 verifyNoCrash("#define a\\\n /**/}"); 20499 } 20500 20501 TEST_F(FormatTest, FormatsTableGenCode) { 20502 FormatStyle Style = getLLVMStyle(); 20503 Style.Language = FormatStyle::LK_TableGen; 20504 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 20505 } 20506 20507 TEST_F(FormatTest, ArrayOfTemplates) { 20508 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 20509 format("auto a = new unique_ptr<int > [ 10];")); 20510 20511 FormatStyle Spaces = getLLVMStyle(); 20512 Spaces.SpacesInSquareBrackets = true; 20513 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 20514 format("auto a = new unique_ptr<int > [10];", Spaces)); 20515 } 20516 20517 TEST_F(FormatTest, ArrayAsTemplateType) { 20518 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 20519 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 20520 20521 FormatStyle Spaces = getLLVMStyle(); 20522 Spaces.SpacesInSquareBrackets = true; 20523 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 20524 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 20525 } 20526 20527 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 20528 20529 TEST(FormatStyle, GetStyleWithEmptyFileName) { 20530 llvm::vfs::InMemoryFileSystem FS; 20531 auto Style1 = getStyle("file", "", "Google", "", &FS); 20532 ASSERT_TRUE((bool)Style1); 20533 ASSERT_EQ(*Style1, getGoogleStyle()); 20534 } 20535 20536 TEST(FormatStyle, GetStyleOfFile) { 20537 llvm::vfs::InMemoryFileSystem FS; 20538 // Test 1: format file in the same directory. 20539 ASSERT_TRUE( 20540 FS.addFile("/a/.clang-format", 0, 20541 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 20542 ASSERT_TRUE( 20543 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20544 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 20545 ASSERT_TRUE((bool)Style1); 20546 ASSERT_EQ(*Style1, getLLVMStyle()); 20547 20548 // Test 2.1: fallback to default. 20549 ASSERT_TRUE( 20550 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20551 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 20552 ASSERT_TRUE((bool)Style2); 20553 ASSERT_EQ(*Style2, getMozillaStyle()); 20554 20555 // Test 2.2: no format on 'none' fallback style. 20556 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 20557 ASSERT_TRUE((bool)Style2); 20558 ASSERT_EQ(*Style2, getNoStyle()); 20559 20560 // Test 2.3: format if config is found with no based style while fallback is 20561 // 'none'. 20562 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 20563 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 20564 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 20565 ASSERT_TRUE((bool)Style2); 20566 ASSERT_EQ(*Style2, getLLVMStyle()); 20567 20568 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 20569 Style2 = getStyle("{}", "a.h", "none", "", &FS); 20570 ASSERT_TRUE((bool)Style2); 20571 ASSERT_EQ(*Style2, getLLVMStyle()); 20572 20573 // Test 3: format file in parent directory. 20574 ASSERT_TRUE( 20575 FS.addFile("/c/.clang-format", 0, 20576 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 20577 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 20578 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20579 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 20580 ASSERT_TRUE((bool)Style3); 20581 ASSERT_EQ(*Style3, getGoogleStyle()); 20582 20583 // Test 4: error on invalid fallback style 20584 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 20585 ASSERT_FALSE((bool)Style4); 20586 llvm::consumeError(Style4.takeError()); 20587 20588 // Test 5: error on invalid yaml on command line 20589 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 20590 ASSERT_FALSE((bool)Style5); 20591 llvm::consumeError(Style5.takeError()); 20592 20593 // Test 6: error on invalid style 20594 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 20595 ASSERT_FALSE((bool)Style6); 20596 llvm::consumeError(Style6.takeError()); 20597 20598 // Test 7: found config file, error on parsing it 20599 ASSERT_TRUE( 20600 FS.addFile("/d/.clang-format", 0, 20601 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 20602 "InvalidKey: InvalidValue"))); 20603 ASSERT_TRUE( 20604 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20605 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 20606 ASSERT_FALSE((bool)Style7a); 20607 llvm::consumeError(Style7a.takeError()); 20608 20609 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 20610 ASSERT_TRUE((bool)Style7b); 20611 20612 // Test 8: inferred per-language defaults apply. 20613 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 20614 ASSERT_TRUE((bool)StyleTd); 20615 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 20616 20617 // Test 9.1: overwriting a file style, when parent no file exists with no 20618 // fallback style 20619 ASSERT_TRUE(FS.addFile( 20620 "/e/sub/.clang-format", 0, 20621 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 20622 "ColumnLimit: 20"))); 20623 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 20624 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20625 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 20626 ASSERT_TRUE(static_cast<bool>(Style9)); 20627 ASSERT_EQ(*Style9, [] { 20628 auto Style = getNoStyle(); 20629 Style.ColumnLimit = 20; 20630 return Style; 20631 }()); 20632 20633 // Test 9.2: with LLVM fallback style 20634 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 20635 ASSERT_TRUE(static_cast<bool>(Style9)); 20636 ASSERT_EQ(*Style9, [] { 20637 auto Style = getLLVMStyle(); 20638 Style.ColumnLimit = 20; 20639 return Style; 20640 }()); 20641 20642 // Test 9.3: with a parent file 20643 ASSERT_TRUE( 20644 FS.addFile("/e/.clang-format", 0, 20645 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 20646 "UseTab: Always"))); 20647 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 20648 ASSERT_TRUE(static_cast<bool>(Style9)); 20649 ASSERT_EQ(*Style9, [] { 20650 auto Style = getGoogleStyle(); 20651 Style.ColumnLimit = 20; 20652 Style.UseTab = FormatStyle::UT_Always; 20653 return Style; 20654 }()); 20655 20656 // Test 9.4: propagate more than one level 20657 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 20658 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20659 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 20660 llvm::MemoryBuffer::getMemBuffer( 20661 "BasedOnStyle: InheritParentConfig\n" 20662 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 20663 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 20664 20665 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 20666 auto Style = getGoogleStyle(); 20667 Style.ColumnLimit = 20; 20668 Style.UseTab = FormatStyle::UT_Always; 20669 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 20670 return Style; 20671 }(); 20672 20673 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 20674 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 20675 ASSERT_TRUE(static_cast<bool>(Style9)); 20676 ASSERT_EQ(*Style9, SubSubStyle); 20677 20678 // Test 9.5: use InheritParentConfig as style name 20679 Style9 = 20680 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 20681 ASSERT_TRUE(static_cast<bool>(Style9)); 20682 ASSERT_EQ(*Style9, SubSubStyle); 20683 20684 // Test 9.6: use command line style with inheritance 20685 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 20686 "none", "", &FS); 20687 ASSERT_TRUE(static_cast<bool>(Style9)); 20688 ASSERT_EQ(*Style9, SubSubStyle); 20689 20690 // Test 9.7: use command line style with inheritance and own config 20691 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 20692 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 20693 "/e/sub/code.cpp", "none", "", &FS); 20694 ASSERT_TRUE(static_cast<bool>(Style9)); 20695 ASSERT_EQ(*Style9, SubSubStyle); 20696 20697 // Test 9.8: use inheritance from a file without BasedOnStyle 20698 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 20699 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 20700 ASSERT_TRUE( 20701 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 20702 llvm::MemoryBuffer::getMemBuffer( 20703 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 20704 // Make sure we do not use the fallback style 20705 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 20706 ASSERT_TRUE(static_cast<bool>(Style9)); 20707 ASSERT_EQ(*Style9, [] { 20708 auto Style = getLLVMStyle(); 20709 Style.ColumnLimit = 123; 20710 return Style; 20711 }()); 20712 20713 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 20714 ASSERT_TRUE(static_cast<bool>(Style9)); 20715 ASSERT_EQ(*Style9, [] { 20716 auto Style = getLLVMStyle(); 20717 Style.ColumnLimit = 123; 20718 Style.IndentWidth = 7; 20719 return Style; 20720 }()); 20721 } 20722 20723 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 20724 // Column limit is 20. 20725 std::string Code = "Type *a =\n" 20726 " new Type();\n" 20727 "g(iiiii, 0, jjjjj,\n" 20728 " 0, kkkkk, 0, mm);\n" 20729 "int bad = format ;"; 20730 std::string Expected = "auto a = new Type();\n" 20731 "g(iiiii, nullptr,\n" 20732 " jjjjj, nullptr,\n" 20733 " kkkkk, nullptr,\n" 20734 " mm);\n" 20735 "int bad = format ;"; 20736 FileID ID = Context.createInMemoryFile("format.cpp", Code); 20737 tooling::Replacements Replaces = toReplacements( 20738 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 20739 "auto "), 20740 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 20741 "nullptr"), 20742 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 20743 "nullptr"), 20744 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 20745 "nullptr")}); 20746 20747 format::FormatStyle Style = format::getLLVMStyle(); 20748 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 20749 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 20750 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 20751 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 20752 auto Result = applyAllReplacements(Code, *FormattedReplaces); 20753 EXPECT_TRUE(static_cast<bool>(Result)); 20754 EXPECT_EQ(Expected, *Result); 20755 } 20756 20757 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 20758 std::string Code = "#include \"a.h\"\n" 20759 "#include \"c.h\"\n" 20760 "\n" 20761 "int main() {\n" 20762 " return 0;\n" 20763 "}"; 20764 std::string Expected = "#include \"a.h\"\n" 20765 "#include \"b.h\"\n" 20766 "#include \"c.h\"\n" 20767 "\n" 20768 "int main() {\n" 20769 " return 0;\n" 20770 "}"; 20771 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 20772 tooling::Replacements Replaces = toReplacements( 20773 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 20774 "#include \"b.h\"\n")}); 20775 20776 format::FormatStyle Style = format::getLLVMStyle(); 20777 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 20778 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 20779 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 20780 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 20781 auto Result = applyAllReplacements(Code, *FormattedReplaces); 20782 EXPECT_TRUE(static_cast<bool>(Result)); 20783 EXPECT_EQ(Expected, *Result); 20784 } 20785 20786 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 20787 EXPECT_EQ("using std::cin;\n" 20788 "using std::cout;", 20789 format("using std::cout;\n" 20790 "using std::cin;", 20791 getGoogleStyle())); 20792 } 20793 20794 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 20795 format::FormatStyle Style = format::getLLVMStyle(); 20796 Style.Standard = FormatStyle::LS_Cpp03; 20797 // cpp03 recognize this string as identifier u8 and literal character 'a' 20798 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 20799 } 20800 20801 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 20802 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 20803 // all modes, including C++11, C++14 and C++17 20804 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 20805 } 20806 20807 TEST_F(FormatTest, DoNotFormatLikelyXml) { 20808 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 20809 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 20810 } 20811 20812 TEST_F(FormatTest, StructuredBindings) { 20813 // Structured bindings is a C++17 feature. 20814 // all modes, including C++11, C++14 and C++17 20815 verifyFormat("auto [a, b] = f();"); 20816 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 20817 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 20818 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 20819 EXPECT_EQ("auto const volatile [a, b] = f();", 20820 format("auto const volatile[a, b] = f();")); 20821 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 20822 EXPECT_EQ("auto &[a, b, c] = f();", 20823 format("auto &[ a , b,c ] = f();")); 20824 EXPECT_EQ("auto &&[a, b, c] = f();", 20825 format("auto &&[ a , b,c ] = f();")); 20826 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 20827 EXPECT_EQ("auto const volatile &&[a, b] = f();", 20828 format("auto const volatile &&[a, b] = f();")); 20829 EXPECT_EQ("auto const &&[a, b] = f();", 20830 format("auto const && [a, b] = f();")); 20831 EXPECT_EQ("const auto &[a, b] = f();", 20832 format("const auto & [a, b] = f();")); 20833 EXPECT_EQ("const auto volatile &&[a, b] = f();", 20834 format("const auto volatile &&[a, b] = f();")); 20835 EXPECT_EQ("volatile const auto &&[a, b] = f();", 20836 format("volatile const auto &&[a, b] = f();")); 20837 EXPECT_EQ("const auto &&[a, b] = f();", 20838 format("const auto && [a, b] = f();")); 20839 20840 // Make sure we don't mistake structured bindings for lambdas. 20841 FormatStyle PointerMiddle = getLLVMStyle(); 20842 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 20843 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 20844 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 20845 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 20846 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 20847 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 20848 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 20849 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 20850 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 20851 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 20852 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 20853 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 20854 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 20855 20856 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 20857 format("for (const auto && [a, b] : some_range) {\n}")); 20858 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 20859 format("for (const auto & [a, b] : some_range) {\n}")); 20860 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 20861 format("for (const auto[a, b] : some_range) {\n}")); 20862 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 20863 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 20864 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 20865 EXPECT_EQ("auto const &[x, y](expr);", 20866 format("auto const & [x,y] (expr);")); 20867 EXPECT_EQ("auto const &&[x, y](expr);", 20868 format("auto const && [x,y] (expr);")); 20869 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 20870 EXPECT_EQ("auto const &[x, y]{expr};", 20871 format("auto const & [x,y] {expr};")); 20872 EXPECT_EQ("auto const &&[x, y]{expr};", 20873 format("auto const && [x,y] {expr};")); 20874 20875 format::FormatStyle Spaces = format::getLLVMStyle(); 20876 Spaces.SpacesInSquareBrackets = true; 20877 verifyFormat("auto [ a, b ] = f();", Spaces); 20878 verifyFormat("auto &&[ a, b ] = f();", Spaces); 20879 verifyFormat("auto &[ a, b ] = f();", Spaces); 20880 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 20881 verifyFormat("auto const &[ a, b ] = f();", Spaces); 20882 } 20883 20884 TEST_F(FormatTest, FileAndCode) { 20885 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 20886 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 20887 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 20888 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 20889 EXPECT_EQ(FormatStyle::LK_ObjC, 20890 guessLanguage("foo.h", "@interface Foo\n@end\n")); 20891 EXPECT_EQ( 20892 FormatStyle::LK_ObjC, 20893 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 20894 EXPECT_EQ(FormatStyle::LK_ObjC, 20895 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 20896 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 20897 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 20898 EXPECT_EQ(FormatStyle::LK_ObjC, 20899 guessLanguage("foo", "@interface Foo\n@end\n")); 20900 EXPECT_EQ(FormatStyle::LK_ObjC, 20901 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 20902 EXPECT_EQ( 20903 FormatStyle::LK_ObjC, 20904 guessLanguage("foo.h", 20905 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 20906 EXPECT_EQ( 20907 FormatStyle::LK_Cpp, 20908 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 20909 } 20910 20911 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 20912 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 20913 EXPECT_EQ(FormatStyle::LK_ObjC, 20914 guessLanguage("foo.h", "array[[calculator getIndex]];")); 20915 EXPECT_EQ(FormatStyle::LK_Cpp, 20916 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 20917 EXPECT_EQ( 20918 FormatStyle::LK_Cpp, 20919 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 20920 EXPECT_EQ(FormatStyle::LK_ObjC, 20921 guessLanguage("foo.h", "[[noreturn foo] bar];")); 20922 EXPECT_EQ(FormatStyle::LK_Cpp, 20923 guessLanguage("foo.h", "[[clang::fallthrough]];")); 20924 EXPECT_EQ(FormatStyle::LK_ObjC, 20925 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 20926 EXPECT_EQ(FormatStyle::LK_Cpp, 20927 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 20928 EXPECT_EQ(FormatStyle::LK_Cpp, 20929 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 20930 EXPECT_EQ(FormatStyle::LK_ObjC, 20931 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 20932 EXPECT_EQ(FormatStyle::LK_Cpp, 20933 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 20934 EXPECT_EQ( 20935 FormatStyle::LK_Cpp, 20936 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 20937 EXPECT_EQ( 20938 FormatStyle::LK_Cpp, 20939 guessLanguage("foo.h", 20940 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 20941 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 20942 } 20943 20944 TEST_F(FormatTest, GuessLanguageWithCaret) { 20945 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 20946 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 20947 EXPECT_EQ(FormatStyle::LK_ObjC, 20948 guessLanguage("foo.h", "int(^)(char, float);")); 20949 EXPECT_EQ(FormatStyle::LK_ObjC, 20950 guessLanguage("foo.h", "int(^foo)(char, float);")); 20951 EXPECT_EQ(FormatStyle::LK_ObjC, 20952 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 20953 EXPECT_EQ(FormatStyle::LK_ObjC, 20954 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 20955 EXPECT_EQ( 20956 FormatStyle::LK_ObjC, 20957 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 20958 } 20959 20960 TEST_F(FormatTest, GuessLanguageWithPragmas) { 20961 EXPECT_EQ(FormatStyle::LK_Cpp, 20962 guessLanguage("foo.h", "__pragma(warning(disable:))")); 20963 EXPECT_EQ(FormatStyle::LK_Cpp, 20964 guessLanguage("foo.h", "#pragma(warning(disable:))")); 20965 EXPECT_EQ(FormatStyle::LK_Cpp, 20966 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 20967 } 20968 20969 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 20970 // ASM symbolic names are identifiers that must be surrounded by [] without 20971 // space in between: 20972 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 20973 20974 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 20975 verifyFormat(R"(// 20976 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 20977 )"); 20978 20979 // A list of several ASM symbolic names. 20980 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 20981 20982 // ASM symbolic names in inline ASM with inputs and outputs. 20983 verifyFormat(R"(// 20984 asm("cmoveq %1, %2, %[result]" 20985 : [result] "=r"(result) 20986 : "r"(test), "r"(new), "[result]"(old)); 20987 )"); 20988 20989 // ASM symbolic names in inline ASM with no outputs. 20990 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 20991 } 20992 20993 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 20994 EXPECT_EQ(FormatStyle::LK_Cpp, 20995 guessLanguage("foo.h", "void f() {\n" 20996 " asm (\"mov %[e], %[d]\"\n" 20997 " : [d] \"=rm\" (d)\n" 20998 " [e] \"rm\" (*e));\n" 20999 "}")); 21000 EXPECT_EQ(FormatStyle::LK_Cpp, 21001 guessLanguage("foo.h", "void f() {\n" 21002 " _asm (\"mov %[e], %[d]\"\n" 21003 " : [d] \"=rm\" (d)\n" 21004 " [e] \"rm\" (*e));\n" 21005 "}")); 21006 EXPECT_EQ(FormatStyle::LK_Cpp, 21007 guessLanguage("foo.h", "void f() {\n" 21008 " __asm (\"mov %[e], %[d]\"\n" 21009 " : [d] \"=rm\" (d)\n" 21010 " [e] \"rm\" (*e));\n" 21011 "}")); 21012 EXPECT_EQ(FormatStyle::LK_Cpp, 21013 guessLanguage("foo.h", "void f() {\n" 21014 " __asm__ (\"mov %[e], %[d]\"\n" 21015 " : [d] \"=rm\" (d)\n" 21016 " [e] \"rm\" (*e));\n" 21017 "}")); 21018 EXPECT_EQ(FormatStyle::LK_Cpp, 21019 guessLanguage("foo.h", "void f() {\n" 21020 " asm (\"mov %[e], %[d]\"\n" 21021 " : [d] \"=rm\" (d),\n" 21022 " [e] \"rm\" (*e));\n" 21023 "}")); 21024 EXPECT_EQ(FormatStyle::LK_Cpp, 21025 guessLanguage("foo.h", "void f() {\n" 21026 " asm volatile (\"mov %[e], %[d]\"\n" 21027 " : [d] \"=rm\" (d)\n" 21028 " [e] \"rm\" (*e));\n" 21029 "}")); 21030 } 21031 21032 TEST_F(FormatTest, GuessLanguageWithChildLines) { 21033 EXPECT_EQ(FormatStyle::LK_Cpp, 21034 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 21035 EXPECT_EQ(FormatStyle::LK_ObjC, 21036 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 21037 EXPECT_EQ( 21038 FormatStyle::LK_Cpp, 21039 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 21040 EXPECT_EQ( 21041 FormatStyle::LK_ObjC, 21042 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 21043 } 21044 21045 TEST_F(FormatTest, TypenameMacros) { 21046 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 21047 21048 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 21049 FormatStyle Google = getGoogleStyleWithColumns(0); 21050 Google.TypenameMacros = TypenameMacros; 21051 verifyFormat("struct foo {\n" 21052 " int bar;\n" 21053 " TAILQ_ENTRY(a) bleh;\n" 21054 "};", 21055 Google); 21056 21057 FormatStyle Macros = getLLVMStyle(); 21058 Macros.TypenameMacros = TypenameMacros; 21059 21060 verifyFormat("STACK_OF(int) a;", Macros); 21061 verifyFormat("STACK_OF(int) *a;", Macros); 21062 verifyFormat("STACK_OF(int const *) *a;", Macros); 21063 verifyFormat("STACK_OF(int *const) *a;", Macros); 21064 verifyFormat("STACK_OF(int, string) a;", Macros); 21065 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 21066 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 21067 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 21068 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 21069 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 21070 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 21071 21072 Macros.PointerAlignment = FormatStyle::PAS_Left; 21073 verifyFormat("STACK_OF(int)* a;", Macros); 21074 verifyFormat("STACK_OF(int*)* a;", Macros); 21075 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 21076 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 21077 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 21078 } 21079 21080 TEST_F(FormatTest, AtomicQualifier) { 21081 // Check that we treate _Atomic as a type and not a function call 21082 FormatStyle Google = getGoogleStyleWithColumns(0); 21083 verifyFormat("struct foo {\n" 21084 " int a1;\n" 21085 " _Atomic(a) a2;\n" 21086 " _Atomic(_Atomic(int) *const) a3;\n" 21087 "};", 21088 Google); 21089 verifyFormat("_Atomic(uint64_t) a;"); 21090 verifyFormat("_Atomic(uint64_t) *a;"); 21091 verifyFormat("_Atomic(uint64_t const *) *a;"); 21092 verifyFormat("_Atomic(uint64_t *const) *a;"); 21093 verifyFormat("_Atomic(const uint64_t *) *a;"); 21094 verifyFormat("_Atomic(uint64_t) a;"); 21095 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 21096 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 21097 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 21098 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 21099 21100 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 21101 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 21102 FormatStyle Style = getLLVMStyle(); 21103 Style.PointerAlignment = FormatStyle::PAS_Left; 21104 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 21105 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 21106 verifyFormat("_Atomic(int)* a;", Style); 21107 verifyFormat("_Atomic(int*)* a;", Style); 21108 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 21109 21110 Style.SpacesInCStyleCastParentheses = true; 21111 Style.SpacesInParentheses = false; 21112 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 21113 Style.SpacesInCStyleCastParentheses = false; 21114 Style.SpacesInParentheses = true; 21115 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 21116 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 21117 } 21118 21119 TEST_F(FormatTest, AmbersandInLamda) { 21120 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 21121 FormatStyle AlignStyle = getLLVMStyle(); 21122 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 21123 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21124 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 21125 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21126 } 21127 21128 TEST_F(FormatTest, SpacesInConditionalStatement) { 21129 FormatStyle Spaces = getLLVMStyle(); 21130 Spaces.IfMacros.clear(); 21131 Spaces.IfMacros.push_back("MYIF"); 21132 Spaces.SpacesInConditionalStatement = true; 21133 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 21134 verifyFormat("if ( !a )\n return;", Spaces); 21135 verifyFormat("if ( a )\n return;", Spaces); 21136 verifyFormat("if constexpr ( a )\n return;", Spaces); 21137 verifyFormat("MYIF ( a )\n return;", Spaces); 21138 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 21139 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 21140 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 21141 verifyFormat("while ( a )\n return;", Spaces); 21142 verifyFormat("while ( (a && b) )\n return;", Spaces); 21143 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 21144 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 21145 // Check that space on the left of "::" is inserted as expected at beginning 21146 // of condition. 21147 verifyFormat("while ( ::func() )\n return;", Spaces); 21148 21149 // Check impact of ControlStatementsExceptControlMacros is honored. 21150 Spaces.SpaceBeforeParens = 21151 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 21152 verifyFormat("MYIF( a )\n return;", Spaces); 21153 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 21154 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 21155 } 21156 21157 TEST_F(FormatTest, AlternativeOperators) { 21158 // Test case for ensuring alternate operators are not 21159 // combined with their right most neighbour. 21160 verifyFormat("int a and b;"); 21161 verifyFormat("int a and_eq b;"); 21162 verifyFormat("int a bitand b;"); 21163 verifyFormat("int a bitor b;"); 21164 verifyFormat("int a compl b;"); 21165 verifyFormat("int a not b;"); 21166 verifyFormat("int a not_eq b;"); 21167 verifyFormat("int a or b;"); 21168 verifyFormat("int a xor b;"); 21169 verifyFormat("int a xor_eq b;"); 21170 verifyFormat("return this not_eq bitand other;"); 21171 verifyFormat("bool operator not_eq(const X bitand other)"); 21172 21173 verifyFormat("int a and 5;"); 21174 verifyFormat("int a and_eq 5;"); 21175 verifyFormat("int a bitand 5;"); 21176 verifyFormat("int a bitor 5;"); 21177 verifyFormat("int a compl 5;"); 21178 verifyFormat("int a not 5;"); 21179 verifyFormat("int a not_eq 5;"); 21180 verifyFormat("int a or 5;"); 21181 verifyFormat("int a xor 5;"); 21182 verifyFormat("int a xor_eq 5;"); 21183 21184 verifyFormat("int a compl(5);"); 21185 verifyFormat("int a not(5);"); 21186 21187 /* FIXME handle alternate tokens 21188 * https://en.cppreference.com/w/cpp/language/operator_alternative 21189 // alternative tokens 21190 verifyFormat("compl foo();"); // ~foo(); 21191 verifyFormat("foo() <%%>;"); // foo(); 21192 verifyFormat("void foo() <%%>;"); // void foo(){} 21193 verifyFormat("int a <:1:>;"); // int a[1];[ 21194 verifyFormat("%:define ABC abc"); // #define ABC abc 21195 verifyFormat("%:%:"); // ## 21196 */ 21197 } 21198 21199 TEST_F(FormatTest, STLWhileNotDefineChed) { 21200 verifyFormat("#if defined(while)\n" 21201 "#define while EMIT WARNING C4005\n" 21202 "#endif // while"); 21203 } 21204 21205 TEST_F(FormatTest, OperatorSpacing) { 21206 FormatStyle Style = getLLVMStyle(); 21207 Style.PointerAlignment = FormatStyle::PAS_Right; 21208 verifyFormat("Foo::operator*();", Style); 21209 verifyFormat("Foo::operator void *();", Style); 21210 verifyFormat("Foo::operator void **();", Style); 21211 verifyFormat("Foo::operator void *&();", Style); 21212 verifyFormat("Foo::operator void *&&();", Style); 21213 verifyFormat("Foo::operator void const *();", Style); 21214 verifyFormat("Foo::operator void const **();", Style); 21215 verifyFormat("Foo::operator void const *&();", Style); 21216 verifyFormat("Foo::operator void const *&&();", Style); 21217 verifyFormat("Foo::operator()(void *);", Style); 21218 verifyFormat("Foo::operator*(void *);", Style); 21219 verifyFormat("Foo::operator*();", Style); 21220 verifyFormat("Foo::operator**();", Style); 21221 verifyFormat("Foo::operator&();", Style); 21222 verifyFormat("Foo::operator<int> *();", Style); 21223 verifyFormat("Foo::operator<Foo> *();", Style); 21224 verifyFormat("Foo::operator<int> **();", Style); 21225 verifyFormat("Foo::operator<Foo> **();", Style); 21226 verifyFormat("Foo::operator<int> &();", Style); 21227 verifyFormat("Foo::operator<Foo> &();", Style); 21228 verifyFormat("Foo::operator<int> &&();", Style); 21229 verifyFormat("Foo::operator<Foo> &&();", Style); 21230 verifyFormat("Foo::operator<int> *&();", Style); 21231 verifyFormat("Foo::operator<Foo> *&();", Style); 21232 verifyFormat("Foo::operator<int> *&&();", Style); 21233 verifyFormat("Foo::operator<Foo> *&&();", Style); 21234 verifyFormat("operator*(int (*)(), class Foo);", Style); 21235 21236 verifyFormat("Foo::operator&();", Style); 21237 verifyFormat("Foo::operator void &();", Style); 21238 verifyFormat("Foo::operator void const &();", Style); 21239 verifyFormat("Foo::operator()(void &);", Style); 21240 verifyFormat("Foo::operator&(void &);", Style); 21241 verifyFormat("Foo::operator&();", Style); 21242 verifyFormat("operator&(int (&)(), class Foo);", Style); 21243 21244 verifyFormat("Foo::operator&&();", Style); 21245 verifyFormat("Foo::operator**();", Style); 21246 verifyFormat("Foo::operator void &&();", Style); 21247 verifyFormat("Foo::operator void const &&();", Style); 21248 verifyFormat("Foo::operator()(void &&);", Style); 21249 verifyFormat("Foo::operator&&(void &&);", Style); 21250 verifyFormat("Foo::operator&&();", Style); 21251 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21252 verifyFormat("operator const nsTArrayRight<E> &()", Style); 21253 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 21254 Style); 21255 verifyFormat("operator void **()", Style); 21256 verifyFormat("operator const FooRight<Object> &()", Style); 21257 verifyFormat("operator const FooRight<Object> *()", Style); 21258 verifyFormat("operator const FooRight<Object> **()", Style); 21259 verifyFormat("operator const FooRight<Object> *&()", Style); 21260 verifyFormat("operator const FooRight<Object> *&&()", Style); 21261 21262 Style.PointerAlignment = FormatStyle::PAS_Left; 21263 verifyFormat("Foo::operator*();", Style); 21264 verifyFormat("Foo::operator**();", Style); 21265 verifyFormat("Foo::operator void*();", Style); 21266 verifyFormat("Foo::operator void**();", Style); 21267 verifyFormat("Foo::operator void*&();", Style); 21268 verifyFormat("Foo::operator void*&&();", Style); 21269 verifyFormat("Foo::operator void const*();", Style); 21270 verifyFormat("Foo::operator void const**();", Style); 21271 verifyFormat("Foo::operator void const*&();", Style); 21272 verifyFormat("Foo::operator void const*&&();", Style); 21273 verifyFormat("Foo::operator/*comment*/ void*();", Style); 21274 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 21275 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 21276 verifyFormat("Foo::operator()(void*);", Style); 21277 verifyFormat("Foo::operator*(void*);", Style); 21278 verifyFormat("Foo::operator*();", Style); 21279 verifyFormat("Foo::operator<int>*();", Style); 21280 verifyFormat("Foo::operator<Foo>*();", Style); 21281 verifyFormat("Foo::operator<int>**();", Style); 21282 verifyFormat("Foo::operator<Foo>**();", Style); 21283 verifyFormat("Foo::operator<Foo>*&();", Style); 21284 verifyFormat("Foo::operator<int>&();", Style); 21285 verifyFormat("Foo::operator<Foo>&();", Style); 21286 verifyFormat("Foo::operator<int>&&();", Style); 21287 verifyFormat("Foo::operator<Foo>&&();", Style); 21288 verifyFormat("Foo::operator<int>*&();", Style); 21289 verifyFormat("Foo::operator<Foo>*&();", Style); 21290 verifyFormat("operator*(int (*)(), class Foo);", Style); 21291 21292 verifyFormat("Foo::operator&();", Style); 21293 verifyFormat("Foo::operator void&();", Style); 21294 verifyFormat("Foo::operator void const&();", Style); 21295 verifyFormat("Foo::operator/*comment*/ void&();", Style); 21296 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 21297 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 21298 verifyFormat("Foo::operator()(void&);", Style); 21299 verifyFormat("Foo::operator&(void&);", Style); 21300 verifyFormat("Foo::operator&();", Style); 21301 verifyFormat("operator&(int (&)(), class Foo);", Style); 21302 21303 verifyFormat("Foo::operator&&();", Style); 21304 verifyFormat("Foo::operator void&&();", Style); 21305 verifyFormat("Foo::operator void const&&();", Style); 21306 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 21307 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 21308 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 21309 verifyFormat("Foo::operator()(void&&);", Style); 21310 verifyFormat("Foo::operator&&(void&&);", Style); 21311 verifyFormat("Foo::operator&&();", Style); 21312 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21313 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 21314 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 21315 Style); 21316 verifyFormat("operator void**()", Style); 21317 verifyFormat("operator const FooLeft<Object>&()", Style); 21318 verifyFormat("operator const FooLeft<Object>*()", Style); 21319 verifyFormat("operator const FooLeft<Object>**()", Style); 21320 verifyFormat("operator const FooLeft<Object>*&()", Style); 21321 verifyFormat("operator const FooLeft<Object>*&&()", Style); 21322 21323 // PR45107 21324 verifyFormat("operator Vector<String>&();", Style); 21325 verifyFormat("operator const Vector<String>&();", Style); 21326 verifyFormat("operator foo::Bar*();", Style); 21327 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 21328 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 21329 Style); 21330 21331 Style.PointerAlignment = FormatStyle::PAS_Middle; 21332 verifyFormat("Foo::operator*();", Style); 21333 verifyFormat("Foo::operator void *();", Style); 21334 verifyFormat("Foo::operator()(void *);", Style); 21335 verifyFormat("Foo::operator*(void *);", Style); 21336 verifyFormat("Foo::operator*();", Style); 21337 verifyFormat("operator*(int (*)(), class Foo);", Style); 21338 21339 verifyFormat("Foo::operator&();", Style); 21340 verifyFormat("Foo::operator void &();", Style); 21341 verifyFormat("Foo::operator void const &();", Style); 21342 verifyFormat("Foo::operator()(void &);", Style); 21343 verifyFormat("Foo::operator&(void &);", Style); 21344 verifyFormat("Foo::operator&();", Style); 21345 verifyFormat("operator&(int (&)(), class Foo);", Style); 21346 21347 verifyFormat("Foo::operator&&();", Style); 21348 verifyFormat("Foo::operator void &&();", Style); 21349 verifyFormat("Foo::operator void const &&();", Style); 21350 verifyFormat("Foo::operator()(void &&);", Style); 21351 verifyFormat("Foo::operator&&(void &&);", Style); 21352 verifyFormat("Foo::operator&&();", Style); 21353 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21354 } 21355 21356 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 21357 FormatStyle Style = getLLVMStyle(); 21358 // PR46157 21359 verifyFormat("foo(operator+, -42);", Style); 21360 verifyFormat("foo(operator++, -42);", Style); 21361 verifyFormat("foo(operator--, -42);", Style); 21362 verifyFormat("foo(-42, operator--);", Style); 21363 verifyFormat("foo(-42, operator, );", Style); 21364 verifyFormat("foo(operator, , -42);", Style); 21365 } 21366 21367 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 21368 FormatStyle Style = getLLVMStyle(); 21369 Style.WhitespaceSensitiveMacros.push_back("FOO"); 21370 21371 // Don't use the helpers here, since 'mess up' will change the whitespace 21372 // and these are all whitespace sensitive by definition 21373 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 21374 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 21375 EXPECT_EQ( 21376 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 21377 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 21378 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 21379 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 21380 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 21381 " Still=Intentional);", 21382 format("FOO(String-ized&Messy+But,: :\n" 21383 " Still=Intentional);", 21384 Style)); 21385 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 21386 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 21387 " Still=Intentional);", 21388 format("FOO(String-ized=&Messy+But,: :\n" 21389 " Still=Intentional);", 21390 Style)); 21391 21392 Style.ColumnLimit = 21; 21393 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 21394 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 21395 } 21396 21397 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 21398 // These tests are not in NamespaceFixer because that doesn't 21399 // test its interaction with line wrapping 21400 FormatStyle Style = getLLVMStyle(); 21401 Style.ColumnLimit = 80; 21402 verifyFormat("namespace {\n" 21403 "int i;\n" 21404 "int j;\n" 21405 "} // namespace", 21406 Style); 21407 21408 verifyFormat("namespace AAA {\n" 21409 "int i;\n" 21410 "int j;\n" 21411 "} // namespace AAA", 21412 Style); 21413 21414 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 21415 "int i;\n" 21416 "int j;\n" 21417 "} // namespace Averyveryveryverylongnamespace", 21418 format("namespace Averyveryveryverylongnamespace {\n" 21419 "int i;\n" 21420 "int j;\n" 21421 "}", 21422 Style)); 21423 21424 EXPECT_EQ( 21425 "namespace " 21426 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 21427 " went::mad::now {\n" 21428 "int i;\n" 21429 "int j;\n" 21430 "} // namespace\n" 21431 " // " 21432 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 21433 "went::mad::now", 21434 format("namespace " 21435 "would::it::save::you::a::lot::of::time::if_::i::" 21436 "just::gave::up::and_::went::mad::now {\n" 21437 "int i;\n" 21438 "int j;\n" 21439 "}", 21440 Style)); 21441 21442 // This used to duplicate the comment again and again on subsequent runs 21443 EXPECT_EQ( 21444 "namespace " 21445 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 21446 " went::mad::now {\n" 21447 "int i;\n" 21448 "int j;\n" 21449 "} // namespace\n" 21450 " // " 21451 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 21452 "went::mad::now", 21453 format("namespace " 21454 "would::it::save::you::a::lot::of::time::if_::i::" 21455 "just::gave::up::and_::went::mad::now {\n" 21456 "int i;\n" 21457 "int j;\n" 21458 "} // namespace\n" 21459 " // " 21460 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 21461 "and_::went::mad::now", 21462 Style)); 21463 } 21464 21465 TEST_F(FormatTest, LikelyUnlikely) { 21466 FormatStyle Style = getLLVMStyle(); 21467 21468 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21469 " return 29;\n" 21470 "}", 21471 Style); 21472 21473 verifyFormat("if (argc > 5) [[likely]] {\n" 21474 " return 29;\n" 21475 "}", 21476 Style); 21477 21478 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21479 " return 29;\n" 21480 "} else [[likely]] {\n" 21481 " return 42;\n" 21482 "}\n", 21483 Style); 21484 21485 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21486 " return 29;\n" 21487 "} else if (argc > 10) [[likely]] {\n" 21488 " return 99;\n" 21489 "} else {\n" 21490 " return 42;\n" 21491 "}\n", 21492 Style); 21493 21494 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 21495 " return 29;\n" 21496 "}", 21497 Style); 21498 } 21499 21500 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 21501 verifyFormat("Constructor()\n" 21502 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21503 " aaaa(aaaaaaaaaaaaaaaaaa, " 21504 "aaaaaaaaaaaaaaaaaat))"); 21505 verifyFormat("Constructor()\n" 21506 " : aaaaaaaaaaaaa(aaaaaa), " 21507 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 21508 21509 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 21510 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 21511 verifyFormat("Constructor()\n" 21512 " : aaaaaa(aaaaaa),\n" 21513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21514 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 21515 StyleWithWhitespacePenalty); 21516 verifyFormat("Constructor()\n" 21517 " : aaaaaaaaaaaaa(aaaaaa), " 21518 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 21519 StyleWithWhitespacePenalty); 21520 } 21521 21522 TEST_F(FormatTest, LLVMDefaultStyle) { 21523 FormatStyle Style = getLLVMStyle(); 21524 verifyFormat("extern \"C\" {\n" 21525 "int foo();\n" 21526 "}", 21527 Style); 21528 } 21529 TEST_F(FormatTest, GNUDefaultStyle) { 21530 FormatStyle Style = getGNUStyle(); 21531 verifyFormat("extern \"C\"\n" 21532 "{\n" 21533 " int foo ();\n" 21534 "}", 21535 Style); 21536 } 21537 TEST_F(FormatTest, MozillaDefaultStyle) { 21538 FormatStyle Style = getMozillaStyle(); 21539 verifyFormat("extern \"C\"\n" 21540 "{\n" 21541 " int foo();\n" 21542 "}", 21543 Style); 21544 } 21545 TEST_F(FormatTest, GoogleDefaultStyle) { 21546 FormatStyle Style = getGoogleStyle(); 21547 verifyFormat("extern \"C\" {\n" 21548 "int foo();\n" 21549 "}", 21550 Style); 21551 } 21552 TEST_F(FormatTest, ChromiumDefaultStyle) { 21553 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 21554 verifyFormat("extern \"C\" {\n" 21555 "int foo();\n" 21556 "}", 21557 Style); 21558 } 21559 TEST_F(FormatTest, MicrosoftDefaultStyle) { 21560 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 21561 verifyFormat("extern \"C\"\n" 21562 "{\n" 21563 " int foo();\n" 21564 "}", 21565 Style); 21566 } 21567 TEST_F(FormatTest, WebKitDefaultStyle) { 21568 FormatStyle Style = getWebKitStyle(); 21569 verifyFormat("extern \"C\" {\n" 21570 "int foo();\n" 21571 "}", 21572 Style); 21573 } 21574 21575 TEST_F(FormatTest, ConceptsAndRequires) { 21576 FormatStyle Style = getLLVMStyle(); 21577 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 21578 21579 verifyFormat("template <typename T>\n" 21580 "concept Hashable = requires(T a) {\n" 21581 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 21582 "};", 21583 Style); 21584 verifyFormat("template <typename T>\n" 21585 "concept EqualityComparable = requires(T a, T b) {\n" 21586 " { a == b } -> bool;\n" 21587 "};", 21588 Style); 21589 verifyFormat("template <typename T>\n" 21590 "concept EqualityComparable = requires(T a, T b) {\n" 21591 " { a == b } -> bool;\n" 21592 " { a != b } -> bool;\n" 21593 "};", 21594 Style); 21595 verifyFormat("template <typename T>\n" 21596 "concept EqualityComparable = requires(T a, T b) {\n" 21597 " { a == b } -> bool;\n" 21598 " { a != b } -> bool;\n" 21599 "};", 21600 Style); 21601 21602 verifyFormat("template <typename It>\n" 21603 "requires Iterator<It>\n" 21604 "void sort(It begin, It end) {\n" 21605 " //....\n" 21606 "}", 21607 Style); 21608 21609 verifyFormat("template <typename T>\n" 21610 "concept Large = sizeof(T) > 10;", 21611 Style); 21612 21613 verifyFormat("template <typename T, typename U>\n" 21614 "concept FooableWith = requires(T t, U u) {\n" 21615 " typename T::foo_type;\n" 21616 " { t.foo(u) } -> typename T::foo_type;\n" 21617 " t++;\n" 21618 "};\n" 21619 "void doFoo(FooableWith<int> auto t) {\n" 21620 " t.foo(3);\n" 21621 "}", 21622 Style); 21623 verifyFormat("template <typename T>\n" 21624 "concept Context = sizeof(T) == 1;", 21625 Style); 21626 verifyFormat("template <typename T>\n" 21627 "concept Context = is_specialization_of_v<context, T>;", 21628 Style); 21629 verifyFormat("template <typename T>\n" 21630 "concept Node = std::is_object_v<T>;", 21631 Style); 21632 verifyFormat("template <typename T>\n" 21633 "concept Tree = true;", 21634 Style); 21635 21636 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 21637 " //...\n" 21638 "}", 21639 Style); 21640 21641 verifyFormat( 21642 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 21643 " //...\n" 21644 "}", 21645 Style); 21646 21647 verifyFormat( 21648 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 21649 " //...\n" 21650 "}", 21651 Style); 21652 21653 verifyFormat("template <typename T>\n" 21654 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 21655 "Concept2<I> {\n" 21656 " //...\n" 21657 "}", 21658 Style); 21659 21660 verifyFormat("template <typename T>\n" 21661 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 21662 "Concept2<I> {\n" 21663 " //...\n" 21664 "}", 21665 Style); 21666 21667 verifyFormat( 21668 "template <typename T>\n" 21669 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 21670 " //...\n" 21671 "}", 21672 Style); 21673 21674 verifyFormat( 21675 "template <typename T>\n" 21676 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 21677 " //...\n" 21678 "}", 21679 Style); 21680 21681 verifyFormat("template <typename It>\n" 21682 "requires Foo<It>() && Bar<It> {\n" 21683 " //....\n" 21684 "}", 21685 Style); 21686 21687 verifyFormat("template <typename It>\n" 21688 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 21689 " //....\n" 21690 "}", 21691 Style); 21692 21693 verifyFormat("template <typename It>\n" 21694 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 21695 " //....\n" 21696 "}", 21697 Style); 21698 21699 verifyFormat( 21700 "template <typename It>\n" 21701 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 21702 " //....\n" 21703 "}", 21704 Style); 21705 21706 Style.IndentRequires = true; 21707 verifyFormat("template <typename It>\n" 21708 " requires Iterator<It>\n" 21709 "void sort(It begin, It end) {\n" 21710 " //....\n" 21711 "}", 21712 Style); 21713 verifyFormat("template <std::size index_>\n" 21714 " requires(index_ < sizeof...(Children_))\n" 21715 "Tree auto &child() {\n" 21716 " // ...\n" 21717 "}", 21718 Style); 21719 21720 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 21721 verifyFormat("template <typename T>\n" 21722 "concept Hashable = requires (T a) {\n" 21723 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 21724 "};", 21725 Style); 21726 21727 verifyFormat("template <class T = void>\n" 21728 " requires EqualityComparable<T> || Same<T, void>\n" 21729 "struct equal_to;", 21730 Style); 21731 21732 verifyFormat("template <class T>\n" 21733 " requires requires {\n" 21734 " T{};\n" 21735 " T (int);\n" 21736 " }\n", 21737 Style); 21738 21739 Style.ColumnLimit = 78; 21740 verifyFormat("template <typename T>\n" 21741 "concept Context = Traits<typename T::traits_type> and\n" 21742 " Interface<typename T::interface_type> and\n" 21743 " Request<typename T::request_type> and\n" 21744 " Response<typename T::response_type> and\n" 21745 " ContextExtension<typename T::extension_type> and\n" 21746 " ::std::is_copy_constructable<T> and " 21747 "::std::is_move_constructable<T> and\n" 21748 " requires (T c) {\n" 21749 " { c.response; } -> Response;\n" 21750 "} and requires (T c) {\n" 21751 " { c.request; } -> Request;\n" 21752 "}\n", 21753 Style); 21754 21755 verifyFormat("template <typename T>\n" 21756 "concept Context = Traits<typename T::traits_type> or\n" 21757 " Interface<typename T::interface_type> or\n" 21758 " Request<typename T::request_type> or\n" 21759 " Response<typename T::response_type> or\n" 21760 " ContextExtension<typename T::extension_type> or\n" 21761 " ::std::is_copy_constructable<T> or " 21762 "::std::is_move_constructable<T> or\n" 21763 " requires (T c) {\n" 21764 " { c.response; } -> Response;\n" 21765 "} or requires (T c) {\n" 21766 " { c.request; } -> Request;\n" 21767 "}\n", 21768 Style); 21769 21770 verifyFormat("template <typename T>\n" 21771 "concept Context = Traits<typename T::traits_type> &&\n" 21772 " Interface<typename T::interface_type> &&\n" 21773 " Request<typename T::request_type> &&\n" 21774 " Response<typename T::response_type> &&\n" 21775 " ContextExtension<typename T::extension_type> &&\n" 21776 " ::std::is_copy_constructable<T> && " 21777 "::std::is_move_constructable<T> &&\n" 21778 " requires (T c) {\n" 21779 " { c.response; } -> Response;\n" 21780 "} && requires (T c) {\n" 21781 " { c.request; } -> Request;\n" 21782 "}\n", 21783 Style); 21784 21785 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 21786 "Constraint2<T>;"); 21787 21788 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 21789 Style.BraceWrapping.AfterFunction = true; 21790 Style.BraceWrapping.AfterClass = true; 21791 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 21792 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 21793 verifyFormat("void Foo () requires (std::copyable<T>)\n" 21794 "{\n" 21795 " return\n" 21796 "}\n", 21797 Style); 21798 21799 verifyFormat("void Foo () requires std::copyable<T>\n" 21800 "{\n" 21801 " return\n" 21802 "}\n", 21803 Style); 21804 21805 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 21806 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 21807 "struct constant;", 21808 Style); 21809 21810 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 21811 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 21812 "struct constant;", 21813 Style); 21814 21815 verifyFormat("template <class T>\n" 21816 "class plane_with_very_very_very_long_name\n" 21817 "{\n" 21818 " constexpr plane_with_very_very_very_long_name () requires " 21819 "std::copyable<T>\n" 21820 " : plane_with_very_very_very_long_name (1)\n" 21821 " {\n" 21822 " }\n" 21823 "}\n", 21824 Style); 21825 21826 verifyFormat("template <class T>\n" 21827 "class plane_with_long_name\n" 21828 "{\n" 21829 " constexpr plane_with_long_name () requires std::copyable<T>\n" 21830 " : plane_with_long_name (1)\n" 21831 " {\n" 21832 " }\n" 21833 "}\n", 21834 Style); 21835 21836 Style.BreakBeforeConceptDeclarations = false; 21837 verifyFormat("template <typename T> concept Tree = true;", Style); 21838 21839 Style.IndentRequires = false; 21840 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 21841 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 21842 "struct constant;", 21843 Style); 21844 } 21845 21846 TEST_F(FormatTest, StatementAttributeLikeMacros) { 21847 FormatStyle Style = getLLVMStyle(); 21848 StringRef Source = "void Foo::slot() {\n" 21849 " unsigned char MyChar = 'x';\n" 21850 " emit signal(MyChar);\n" 21851 " Q_EMIT signal(MyChar);\n" 21852 "}"; 21853 21854 EXPECT_EQ(Source, format(Source, Style)); 21855 21856 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 21857 EXPECT_EQ("void Foo::slot() {\n" 21858 " unsigned char MyChar = 'x';\n" 21859 " emit signal(MyChar);\n" 21860 " Q_EMIT signal(MyChar);\n" 21861 "}", 21862 format(Source, Style)); 21863 21864 Style.StatementAttributeLikeMacros.push_back("emit"); 21865 EXPECT_EQ(Source, format(Source, Style)); 21866 21867 Style.StatementAttributeLikeMacros = {}; 21868 EXPECT_EQ("void Foo::slot() {\n" 21869 " unsigned char MyChar = 'x';\n" 21870 " emit signal(MyChar);\n" 21871 " Q_EMIT signal(MyChar);\n" 21872 "}", 21873 format(Source, Style)); 21874 } 21875 21876 TEST_F(FormatTest, IndentAccessModifiers) { 21877 FormatStyle Style = getLLVMStyle(); 21878 Style.IndentAccessModifiers = true; 21879 // Members are *two* levels below the record; 21880 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 21881 verifyFormat("class C {\n" 21882 " int i;\n" 21883 "};\n", 21884 Style); 21885 verifyFormat("union C {\n" 21886 " int i;\n" 21887 " unsigned u;\n" 21888 "};\n", 21889 Style); 21890 // Access modifiers should be indented one level below the record. 21891 verifyFormat("class C {\n" 21892 " public:\n" 21893 " int i;\n" 21894 "};\n", 21895 Style); 21896 verifyFormat("struct S {\n" 21897 " private:\n" 21898 " class C {\n" 21899 " int j;\n" 21900 "\n" 21901 " public:\n" 21902 " C();\n" 21903 " };\n" 21904 "\n" 21905 " public:\n" 21906 " int i;\n" 21907 "};\n", 21908 Style); 21909 // Enumerations are not records and should be unaffected. 21910 Style.AllowShortEnumsOnASingleLine = false; 21911 verifyFormat("enum class E\n" 21912 "{\n" 21913 " A,\n" 21914 " B\n" 21915 "};\n", 21916 Style); 21917 // Test with a different indentation width; 21918 // also proves that the result is Style.AccessModifierOffset agnostic. 21919 Style.IndentWidth = 3; 21920 verifyFormat("class C {\n" 21921 " public:\n" 21922 " int i;\n" 21923 "};\n", 21924 Style); 21925 } 21926 21927 TEST_F(FormatTest, LimitlessStringsAndComments) { 21928 auto Style = getLLVMStyleWithColumns(0); 21929 constexpr StringRef Code = 21930 "/**\n" 21931 " * This is a multiline comment with quite some long lines, at least for " 21932 "the LLVM Style.\n" 21933 " * We will redo this with strings and line comments. Just to check if " 21934 "everything is working.\n" 21935 " */\n" 21936 "bool foo() {\n" 21937 " /* Single line multi line comment. */\n" 21938 " const std::string String = \"This is a multiline string with quite " 21939 "some long lines, at least for the LLVM Style.\"\n" 21940 " \"We already did it with multi line " 21941 "comments, and we will do it with line comments. Just to check if " 21942 "everything is working.\";\n" 21943 " // This is a line comment (block) with quite some long lines, at " 21944 "least for the LLVM Style.\n" 21945 " // We already did this with multi line comments and strings. Just to " 21946 "check if everything is working.\n" 21947 " const std::string SmallString = \"Hello World\";\n" 21948 " // Small line comment\n" 21949 " return String.size() > SmallString.size();\n" 21950 "}"; 21951 EXPECT_EQ(Code, format(Code, Style)); 21952 } 21953 } // namespace 21954 } // namespace format 21955 } // namespace clang 21956