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 "FormatTestBase.h" 10 11 #define DEBUG_TYPE "format-test" 12 13 namespace clang { 14 namespace format { 15 namespace test { 16 namespace { 17 18 class FormatTest : public test::FormatTestBase {}; 19 20 TEST_F(FormatTest, MessUp) { 21 EXPECT_EQ("1 2 3", messUp("1 2 3")); 22 EXPECT_EQ("1 2 3", messUp("1\n2\n3")); 23 EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc")); 24 EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc")); 25 EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne")); 26 } 27 28 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { 29 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); 30 } 31 32 TEST_F(FormatTest, LLVMStyleOverride) { 33 EXPECT_EQ(FormatStyle::LK_Proto, 34 getLLVMStyle(FormatStyle::LK_Proto).Language); 35 } 36 37 //===----------------------------------------------------------------------===// 38 // Basic function tests. 39 //===----------------------------------------------------------------------===// 40 41 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { verifyFormat(";"); } 42 43 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 44 verifyFormat("int i;", " int i;"); 45 verifyFormat("\nint i;", " \n\t \v \f int i;"); 46 verifyFormat("int i;\nint j;", " int i; int j;"); 47 verifyFormat("int i;\nint j;", " int i;\n int j;"); 48 49 auto Style = getLLVMStyle(); 50 Style.KeepEmptyLines.AtStartOfFile = false; 51 verifyFormat("int i;", " \n\t \v \f int i;", Style); 52 } 53 54 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 55 verifyFormat("int i;", "int\ni;"); 56 } 57 58 TEST_F(FormatTest, FormatsNestedBlockStatements) { 59 verifyFormat("{\n" 60 " {\n" 61 " {\n" 62 " }\n" 63 " }\n" 64 "}", 65 "{{{}}}"); 66 } 67 68 TEST_F(FormatTest, FormatsNestedCall) { 69 verifyFormat("Method(f1, f2(f3));"); 70 verifyFormat("Method(f1(f2, f3()));"); 71 verifyFormat("Method(f1(f2, (f3())));"); 72 } 73 74 TEST_F(FormatTest, NestedNameSpecifiers) { 75 verifyFormat("vector<::Type> v;"); 76 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 77 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 78 verifyFormat("static constexpr bool Bar = typeof(bar())::value;"); 79 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;"); 80 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;"); 81 verifyFormat("bool a = 2 < ::SomeFunction();"); 82 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 83 verifyFormat("some::string getName();"); 84 } 85 86 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 87 verifyFormat("if (a) {\n" 88 " f();\n" 89 "}", 90 "if(a){f();}"); 91 EXPECT_EQ(4, ReplacementCount); 92 verifyNoChange("if (a) {\n" 93 " f();\n" 94 "}"); 95 EXPECT_EQ(0, ReplacementCount); 96 verifyNoChange("/*\r\n" 97 "\r\n" 98 "*/"); 99 EXPECT_EQ(0, ReplacementCount); 100 } 101 102 TEST_F(FormatTest, RemovesEmptyLines) { 103 verifyFormat("class C {\n" 104 " int i;\n" 105 "};", 106 "class C {\n" 107 " int i;\n" 108 "\n" 109 "};"); 110 111 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 112 verifyFormat("namespace N {\n" 113 "\n" 114 "int i;\n" 115 "}", 116 "namespace N {\n" 117 "\n" 118 "int i;\n" 119 "}", 120 getGoogleStyle()); 121 verifyFormat("/* something */ namespace N {\n" 122 "\n" 123 "int i;\n" 124 "}", 125 "/* something */ namespace N {\n" 126 "\n" 127 "int i;\n" 128 "}", 129 getGoogleStyle()); 130 verifyFormat("inline namespace N {\n" 131 "\n" 132 "int i;\n" 133 "}", 134 "inline namespace N {\n" 135 "\n" 136 "int i;\n" 137 "}", 138 getGoogleStyle()); 139 verifyFormat("/* something */ inline namespace N {\n" 140 "\n" 141 "int i;\n" 142 "}", 143 "/* something */ inline namespace N {\n" 144 "\n" 145 "int i;\n" 146 "}", 147 getGoogleStyle()); 148 verifyFormat("export namespace N {\n" 149 "\n" 150 "int i;\n" 151 "}", 152 "export namespace N {\n" 153 "\n" 154 "int i;\n" 155 "}", 156 getGoogleStyle()); 157 verifyFormat("extern /**/ \"C\" /**/ {\n" 158 "\n" 159 "int i;\n" 160 "}", 161 "extern /**/ \"C\" /**/ {\n" 162 "\n" 163 "int i;\n" 164 "}", 165 getGoogleStyle()); 166 167 auto CustomStyle = getLLVMStyle(); 168 CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom; 169 CustomStyle.BraceWrapping.AfterNamespace = true; 170 CustomStyle.KeepEmptyLines.AtStartOfBlock = false; 171 verifyFormat("namespace N\n" 172 "{\n" 173 "\n" 174 "int i;\n" 175 "}", 176 "namespace N\n" 177 "{\n" 178 "\n" 179 "\n" 180 "int i;\n" 181 "}", 182 CustomStyle); 183 verifyFormat("/* something */ namespace N\n" 184 "{\n" 185 "\n" 186 "int i;\n" 187 "}", 188 "/* something */ namespace N {\n" 189 "\n" 190 "\n" 191 "int i;\n" 192 "}", 193 CustomStyle); 194 verifyFormat("inline namespace N\n" 195 "{\n" 196 "\n" 197 "int i;\n" 198 "}", 199 "inline namespace N\n" 200 "{\n" 201 "\n" 202 "\n" 203 "int i;\n" 204 "}", 205 CustomStyle); 206 verifyFormat("/* something */ inline namespace N\n" 207 "{\n" 208 "\n" 209 "int i;\n" 210 "}", 211 "/* something */ inline namespace N\n" 212 "{\n" 213 "\n" 214 "int i;\n" 215 "}", 216 CustomStyle); 217 verifyFormat("export namespace N\n" 218 "{\n" 219 "\n" 220 "int i;\n" 221 "}", 222 "export namespace N\n" 223 "{\n" 224 "\n" 225 "int i;\n" 226 "}", 227 CustomStyle); 228 verifyFormat("namespace a\n" 229 "{\n" 230 "namespace b\n" 231 "{\n" 232 "\n" 233 "class AA {};\n" 234 "\n" 235 "} // namespace b\n" 236 "} // namespace a", 237 "namespace a\n" 238 "{\n" 239 "namespace b\n" 240 "{\n" 241 "\n" 242 "\n" 243 "class AA {};\n" 244 "\n" 245 "\n" 246 "}\n" 247 "}", 248 CustomStyle); 249 verifyFormat("namespace A /* comment */\n" 250 "{\n" 251 "class B {}\n" 252 "} // namespace A", 253 "namespace A /* comment */ { class B {} }", CustomStyle); 254 verifyFormat("namespace A\n" 255 "{ /* comment */\n" 256 "class B {}\n" 257 "} // namespace A", 258 "namespace A {/* comment */ class B {} }", CustomStyle); 259 verifyFormat("namespace A\n" 260 "{ /* comment */\n" 261 "\n" 262 "class B {}\n" 263 "\n" 264 "" 265 "} // namespace A", 266 "namespace A { /* comment */\n" 267 "\n" 268 "\n" 269 "class B {}\n" 270 "\n" 271 "\n" 272 "}", 273 CustomStyle); 274 verifyFormat("namespace A /* comment */\n" 275 "{\n" 276 "\n" 277 "class B {}\n" 278 "\n" 279 "} // namespace A", 280 "namespace A/* comment */ {\n" 281 "\n" 282 "\n" 283 "class B {}\n" 284 "\n" 285 "\n" 286 "}", 287 CustomStyle); 288 289 // ...but do keep inlining and removing empty lines for non-block extern "C" 290 // functions. 291 verifyGoogleFormat("extern \"C\" int f() { return 42; }"); 292 verifyFormat("extern \"C\" int f() {\n" 293 " int i = 42;\n" 294 " return i;\n" 295 "}", 296 "extern \"C\" int f() {\n" 297 "\n" 298 " int i = 42;\n" 299 " return i;\n" 300 "}", 301 getGoogleStyle()); 302 303 // Remove empty lines at the beginning and end of blocks. 304 verifyFormat("void f() {\n" 305 "\n" 306 " if (a) {\n" 307 "\n" 308 " f();\n" 309 " }\n" 310 "}", 311 "void f() {\n" 312 "\n" 313 " if (a) {\n" 314 "\n" 315 " f();\n" 316 "\n" 317 " }\n" 318 "\n" 319 "}"); 320 verifyFormat("void f() {\n" 321 " if (a) {\n" 322 " f();\n" 323 " }\n" 324 "}", 325 "void f() {\n" 326 "\n" 327 " if (a) {\n" 328 "\n" 329 " f();\n" 330 "\n" 331 " }\n" 332 "\n" 333 "}", 334 getGoogleStyle()); 335 336 // Don't remove empty lines in more complex control statements. 337 verifyFormat("void f() {\n" 338 " if (a) {\n" 339 " f();\n" 340 "\n" 341 " } else if (b) {\n" 342 " f();\n" 343 " }\n" 344 "}", 345 "void f() {\n" 346 " if (a) {\n" 347 " f();\n" 348 "\n" 349 " } else if (b) {\n" 350 " f();\n" 351 "\n" 352 " }\n" 353 "\n" 354 "}"); 355 356 // Don't remove empty lines before namespace endings. 357 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 358 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 359 verifyNoChange("namespace {\n" 360 "int i;\n" 361 "\n" 362 "}", 363 LLVMWithNoNamespaceFix); 364 verifyFormat("namespace {\n" 365 "int i;\n" 366 "}", 367 LLVMWithNoNamespaceFix); 368 verifyNoChange("namespace {\n" 369 "int i;\n" 370 "\n" 371 "};", 372 LLVMWithNoNamespaceFix); 373 verifyFormat("namespace {\n" 374 "int i;\n" 375 "};", 376 LLVMWithNoNamespaceFix); 377 verifyNoChange("namespace {\n" 378 "int i;\n" 379 "\n" 380 "}"); 381 verifyFormat("namespace {\n" 382 "int i;\n" 383 "\n" 384 "} // namespace", 385 "namespace {\n" 386 "int i;\n" 387 "\n" 388 "} // namespace"); 389 390 FormatStyle Style = getLLVMStyle(); 391 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 392 Style.MaxEmptyLinesToKeep = 2; 393 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 394 Style.BraceWrapping.AfterClass = true; 395 Style.BraceWrapping.AfterFunction = true; 396 Style.KeepEmptyLines.AtStartOfBlock = false; 397 398 verifyFormat("class Foo\n" 399 "{\n" 400 " Foo() {}\n" 401 "\n" 402 " void funk() {}\n" 403 "};", 404 "class Foo\n" 405 "{\n" 406 " Foo()\n" 407 " {\n" 408 " }\n" 409 "\n" 410 " void funk() {}\n" 411 "};", 412 Style); 413 } 414 415 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 416 verifyFormat("x = (a) and (b);"); 417 verifyFormat("x = (a) or (b);"); 418 verifyFormat("x = (a) bitand (b);"); 419 verifyFormat("x = (a) bitor (b);"); 420 verifyFormat("x = (a) not_eq (b);"); 421 verifyFormat("x = (a) and_eq (b);"); 422 verifyFormat("x = (a) or_eq (b);"); 423 verifyFormat("x = (a) xor (b);"); 424 } 425 426 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 427 verifyFormat("x = compl(a);"); 428 verifyFormat("x = not(a);"); 429 verifyFormat("x = bitand(a);"); 430 // Unary operator must not be merged with the next identifier 431 verifyFormat("x = compl a;"); 432 verifyFormat("x = not a;"); 433 verifyFormat("x = bitand a;"); 434 } 435 436 //===----------------------------------------------------------------------===// 437 // Tests for control statements. 438 //===----------------------------------------------------------------------===// 439 440 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 441 verifyFormat("if (true)\n f();\ng();"); 442 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 443 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 444 verifyFormat("if constexpr (true)\n" 445 " f();\ng();"); 446 verifyFormat("if CONSTEXPR (true)\n" 447 " f();\ng();"); 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 " if CONSTEXPR (c)\n" 456 " g();\n" 457 "h();"); 458 verifyFormat("if constexpr (a)\n" 459 " if constexpr (b) {\n" 460 " f();\n" 461 " }\n" 462 "g();"); 463 verifyFormat("if CONSTEXPR (a)\n" 464 " if CONSTEXPR (b) {\n" 465 " f();\n" 466 " }\n" 467 "g();"); 468 469 verifyFormat("if consteval {\n}"); 470 verifyFormat("if !consteval {\n}"); 471 verifyFormat("if not consteval {\n}"); 472 verifyFormat("if consteval {\n} else {\n}"); 473 verifyFormat("if !consteval {\n} else {\n}"); 474 verifyFormat("if consteval {\n" 475 " f();\n" 476 "}"); 477 verifyFormat("if !consteval {\n" 478 " f();\n" 479 "}"); 480 verifyFormat("if consteval {\n" 481 " f();\n" 482 "} else {\n" 483 " g();\n" 484 "}"); 485 verifyFormat("if CONSTEVAL {\n" 486 " f();\n" 487 "}"); 488 verifyFormat("if !CONSTEVAL {\n" 489 " f();\n" 490 "}"); 491 492 verifyFormat("if (a)\n" 493 " g();"); 494 verifyFormat("if (a) {\n" 495 " g()\n" 496 "};"); 497 verifyFormat("if (a)\n" 498 " g();\n" 499 "else\n" 500 " g();"); 501 verifyFormat("if (a) {\n" 502 " g();\n" 503 "} else\n" 504 " g();"); 505 verifyFormat("if (a)\n" 506 " g();\n" 507 "else {\n" 508 " g();\n" 509 "}"); 510 verifyFormat("if (a) {\n" 511 " g();\n" 512 "} else {\n" 513 " g();\n" 514 "}"); 515 verifyFormat("if (a)\n" 516 " g();\n" 517 "else if (b)\n" 518 " g();\n" 519 "else\n" 520 " g();"); 521 verifyFormat("if (a) {\n" 522 " g();\n" 523 "} else if (b)\n" 524 " g();\n" 525 "else\n" 526 " g();"); 527 verifyFormat("if (a)\n" 528 " g();\n" 529 "else if (b) {\n" 530 " g();\n" 531 "} else\n" 532 " g();"); 533 verifyFormat("if (a)\n" 534 " g();\n" 535 "else if (b)\n" 536 " g();\n" 537 "else {\n" 538 " g();\n" 539 "}"); 540 verifyFormat("if (a)\n" 541 " g();\n" 542 "else if (b) {\n" 543 " g();\n" 544 "} else {\n" 545 " g();\n" 546 "}"); 547 verifyFormat("if (a) {\n" 548 " g();\n" 549 "} else if (b) {\n" 550 " g();\n" 551 "} else {\n" 552 " g();\n" 553 "}"); 554 555 FormatStyle AllowsMergedIf = getLLVMStyle(); 556 AllowsMergedIf.IfMacros.push_back("MYIF"); 557 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 558 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 559 FormatStyle::SIS_WithoutElse; 560 verifyFormat("if (a)\n" 561 " // comment\n" 562 " f();", 563 AllowsMergedIf); 564 verifyFormat("{\n" 565 " if (a)\n" 566 " label:\n" 567 " f();\n" 568 "}", 569 AllowsMergedIf); 570 verifyFormat("#define A \\\n" 571 " if (a) \\\n" 572 " label: \\\n" 573 " f()", 574 AllowsMergedIf); 575 verifyFormat("if (a)\n" 576 " ;", 577 AllowsMergedIf); 578 verifyFormat("if (a)\n" 579 " if (b) return;", 580 AllowsMergedIf); 581 582 verifyFormat("if (a) // Can't merge this\n" 583 " f();", 584 AllowsMergedIf); 585 verifyFormat("if (a) /* still don't merge */\n" 586 " f();", 587 AllowsMergedIf); 588 verifyFormat("if (a) { // Never merge this\n" 589 " f();\n" 590 "}", 591 AllowsMergedIf); 592 verifyFormat("if (a) { /* Never merge this */\n" 593 " f();\n" 594 "}", 595 AllowsMergedIf); 596 verifyFormat("MYIF (a)\n" 597 " // comment\n" 598 " f();", 599 AllowsMergedIf); 600 verifyFormat("{\n" 601 " MYIF (a)\n" 602 " label:\n" 603 " f();\n" 604 "}", 605 AllowsMergedIf); 606 verifyFormat("#define A \\\n" 607 " MYIF (a) \\\n" 608 " label: \\\n" 609 " f()", 610 AllowsMergedIf); 611 verifyFormat("MYIF (a)\n" 612 " ;", 613 AllowsMergedIf); 614 verifyFormat("MYIF (a)\n" 615 " MYIF (b) return;", 616 AllowsMergedIf); 617 618 verifyFormat("MYIF (a) // Can't merge this\n" 619 " f();", 620 AllowsMergedIf); 621 verifyFormat("MYIF (a) /* still don't merge */\n" 622 " f();", 623 AllowsMergedIf); 624 verifyFormat("MYIF (a) { // Never merge this\n" 625 " f();\n" 626 "}", 627 AllowsMergedIf); 628 verifyFormat("MYIF (a) { /* Never merge this */\n" 629 " f();\n" 630 "}", 631 AllowsMergedIf); 632 633 AllowsMergedIf.ColumnLimit = 14; 634 // Where line-lengths matter, a 2-letter synonym that maintains line length. 635 // Not IF to avoid any confusion that IF is somehow special. 636 AllowsMergedIf.IfMacros.push_back("FI"); 637 verifyFormat("if (a) return;", AllowsMergedIf); 638 verifyFormat("if (aaaaaaaaa)\n" 639 " return;", 640 AllowsMergedIf); 641 verifyFormat("FI (a) return;", AllowsMergedIf); 642 verifyFormat("FI (aaaaaaaaa)\n" 643 " return;", 644 AllowsMergedIf); 645 646 AllowsMergedIf.ColumnLimit = 13; 647 verifyFormat("if (a)\n return;", AllowsMergedIf); 648 verifyFormat("FI (a)\n return;", AllowsMergedIf); 649 650 FormatStyle AllowsMergedIfElse = getLLVMStyle(); 651 AllowsMergedIfElse.IfMacros.push_back("MYIF"); 652 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine = 653 FormatStyle::SIS_AllIfsAndElse; 654 verifyFormat("if (a)\n" 655 " // comment\n" 656 " f();\n" 657 "else\n" 658 " // comment\n" 659 " f();", 660 AllowsMergedIfElse); 661 verifyFormat("{\n" 662 " if (a)\n" 663 " label:\n" 664 " f();\n" 665 " else\n" 666 " label:\n" 667 " f();\n" 668 "}", 669 AllowsMergedIfElse); 670 verifyFormat("if (a)\n" 671 " ;\n" 672 "else\n" 673 " ;", 674 AllowsMergedIfElse); 675 verifyFormat("if (a) {\n" 676 "} else {\n" 677 "}", 678 AllowsMergedIfElse); 679 verifyFormat("if (a) return;\n" 680 "else if (b) return;\n" 681 "else return;", 682 AllowsMergedIfElse); 683 verifyFormat("if (a) {\n" 684 "} else return;", 685 AllowsMergedIfElse); 686 verifyFormat("if (a) {\n" 687 "} else if (b) return;\n" 688 "else return;", 689 AllowsMergedIfElse); 690 verifyFormat("if (a) return;\n" 691 "else if (b) {\n" 692 "} else return;", 693 AllowsMergedIfElse); 694 verifyFormat("if (a)\n" 695 " if (b) return;\n" 696 " else return;", 697 AllowsMergedIfElse); 698 verifyFormat("if constexpr (a)\n" 699 " if constexpr (b) return;\n" 700 " else if constexpr (c) return;\n" 701 " else return;", 702 AllowsMergedIfElse); 703 verifyFormat("MYIF (a)\n" 704 " // comment\n" 705 " f();\n" 706 "else\n" 707 " // comment\n" 708 " f();", 709 AllowsMergedIfElse); 710 verifyFormat("{\n" 711 " MYIF (a)\n" 712 " label:\n" 713 " f();\n" 714 " else\n" 715 " label:\n" 716 " f();\n" 717 "}", 718 AllowsMergedIfElse); 719 verifyFormat("MYIF (a)\n" 720 " ;\n" 721 "else\n" 722 " ;", 723 AllowsMergedIfElse); 724 verifyFormat("MYIF (a) {\n" 725 "} else {\n" 726 "}", 727 AllowsMergedIfElse); 728 verifyFormat("MYIF (a) return;\n" 729 "else MYIF (b) return;\n" 730 "else return;", 731 AllowsMergedIfElse); 732 verifyFormat("MYIF (a) {\n" 733 "} else return;", 734 AllowsMergedIfElse); 735 verifyFormat("MYIF (a) {\n" 736 "} else MYIF (b) return;\n" 737 "else return;", 738 AllowsMergedIfElse); 739 verifyFormat("MYIF (a) return;\n" 740 "else MYIF (b) {\n" 741 "} else return;", 742 AllowsMergedIfElse); 743 verifyFormat("MYIF (a)\n" 744 " MYIF (b) return;\n" 745 " else return;", 746 AllowsMergedIfElse); 747 verifyFormat("MYIF constexpr (a)\n" 748 " MYIF constexpr (b) return;\n" 749 " else MYIF constexpr (c) return;\n" 750 " else return;", 751 AllowsMergedIfElse); 752 } 753 754 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 755 FormatStyle AllowsMergedIf = getLLVMStyle(); 756 AllowsMergedIf.IfMacros.push_back("MYIF"); 757 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 758 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 759 FormatStyle::SIS_WithoutElse; 760 verifyFormat("if (a)\n" 761 " f();\n" 762 "else {\n" 763 " g();\n" 764 "}", 765 AllowsMergedIf); 766 verifyFormat("if (a)\n" 767 " f();\n" 768 "else\n" 769 " g();", 770 AllowsMergedIf); 771 772 verifyFormat("if (a) g();", AllowsMergedIf); 773 verifyFormat("if (a) {\n" 774 " g()\n" 775 "};", 776 AllowsMergedIf); 777 verifyFormat("if (a)\n" 778 " g();\n" 779 "else\n" 780 " g();", 781 AllowsMergedIf); 782 verifyFormat("if (a) {\n" 783 " g();\n" 784 "} else\n" 785 " g();", 786 AllowsMergedIf); 787 verifyFormat("if (a)\n" 788 " g();\n" 789 "else {\n" 790 " g();\n" 791 "}", 792 AllowsMergedIf); 793 verifyFormat("if (a) {\n" 794 " g();\n" 795 "} else {\n" 796 " g();\n" 797 "}", 798 AllowsMergedIf); 799 verifyFormat("if (a)\n" 800 " g();\n" 801 "else if (b)\n" 802 " g();\n" 803 "else\n" 804 " g();", 805 AllowsMergedIf); 806 verifyFormat("if (a) {\n" 807 " g();\n" 808 "} else if (b)\n" 809 " g();\n" 810 "else\n" 811 " g();", 812 AllowsMergedIf); 813 verifyFormat("if (a)\n" 814 " g();\n" 815 "else if (b) {\n" 816 " g();\n" 817 "} else\n" 818 " g();", 819 AllowsMergedIf); 820 verifyFormat("if (a)\n" 821 " g();\n" 822 "else if (b)\n" 823 " g();\n" 824 "else {\n" 825 " g();\n" 826 "}", 827 AllowsMergedIf); 828 verifyFormat("if (a)\n" 829 " g();\n" 830 "else if (b) {\n" 831 " g();\n" 832 "} else {\n" 833 " g();\n" 834 "}", 835 AllowsMergedIf); 836 verifyFormat("if (a) {\n" 837 " g();\n" 838 "} else if (b) {\n" 839 " g();\n" 840 "} else {\n" 841 " g();\n" 842 "}", 843 AllowsMergedIf); 844 verifyFormat("MYIF (a)\n" 845 " f();\n" 846 "else {\n" 847 " g();\n" 848 "}", 849 AllowsMergedIf); 850 verifyFormat("MYIF (a)\n" 851 " f();\n" 852 "else\n" 853 " g();", 854 AllowsMergedIf); 855 856 verifyFormat("MYIF (a) g();", AllowsMergedIf); 857 verifyFormat("MYIF (a) {\n" 858 " g()\n" 859 "};", 860 AllowsMergedIf); 861 verifyFormat("MYIF (a)\n" 862 " g();\n" 863 "else\n" 864 " g();", 865 AllowsMergedIf); 866 verifyFormat("MYIF (a) {\n" 867 " g();\n" 868 "} else\n" 869 " g();", 870 AllowsMergedIf); 871 verifyFormat("MYIF (a)\n" 872 " g();\n" 873 "else {\n" 874 " g();\n" 875 "}", 876 AllowsMergedIf); 877 verifyFormat("MYIF (a) {\n" 878 " g();\n" 879 "} else {\n" 880 " g();\n" 881 "}", 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();", 903 AllowsMergedIf); 904 verifyFormat("MYIF (a) {\n" 905 " g();\n" 906 "} else if (b)\n" 907 " g();\n" 908 "else\n" 909 " g();", 910 AllowsMergedIf); 911 verifyFormat("MYIF (a)\n" 912 " g();\n" 913 "else MYIF (b) {\n" 914 " g();\n" 915 "} else\n" 916 " g();", 917 AllowsMergedIf); 918 verifyFormat("MYIF (a)\n" 919 " g();\n" 920 "else if (b) {\n" 921 " g();\n" 922 "} else\n" 923 " g();", 924 AllowsMergedIf); 925 verifyFormat("MYIF (a)\n" 926 " g();\n" 927 "else MYIF (b)\n" 928 " g();\n" 929 "else {\n" 930 " g();\n" 931 "}", 932 AllowsMergedIf); 933 verifyFormat("MYIF (a)\n" 934 " g();\n" 935 "else if (b)\n" 936 " g();\n" 937 "else {\n" 938 " g();\n" 939 "}", 940 AllowsMergedIf); 941 verifyFormat("MYIF (a)\n" 942 " g();\n" 943 "else MYIF (b) {\n" 944 " g();\n" 945 "} else {\n" 946 " g();\n" 947 "}", 948 AllowsMergedIf); 949 verifyFormat("MYIF (a)\n" 950 " g();\n" 951 "else if (b) {\n" 952 " g();\n" 953 "} else {\n" 954 " g();\n" 955 "}", 956 AllowsMergedIf); 957 verifyFormat("MYIF (a) {\n" 958 " g();\n" 959 "} else MYIF (b) {\n" 960 " g();\n" 961 "} else {\n" 962 " g();\n" 963 "}", 964 AllowsMergedIf); 965 verifyFormat("MYIF (a) {\n" 966 " g();\n" 967 "} else if (b) {\n" 968 " g();\n" 969 "} else {\n" 970 " g();\n" 971 "}", 972 AllowsMergedIf); 973 974 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 975 FormatStyle::SIS_OnlyFirstIf; 976 977 verifyFormat("if (a) f();\n" 978 "else {\n" 979 " g();\n" 980 "}", 981 AllowsMergedIf); 982 verifyFormat("if (a) f();\n" 983 "else {\n" 984 " if (a) f();\n" 985 " else {\n" 986 " g();\n" 987 " }\n" 988 " g();\n" 989 "}", 990 AllowsMergedIf); 991 992 verifyFormat("if (a) g();", AllowsMergedIf); 993 verifyFormat("if (a) {\n" 994 " g()\n" 995 "};", 996 AllowsMergedIf); 997 verifyFormat("if (a) g();\n" 998 "else\n" 999 " g();", 1000 AllowsMergedIf); 1001 verifyFormat("if (a) {\n" 1002 " g();\n" 1003 "} else\n" 1004 " g();", 1005 AllowsMergedIf); 1006 verifyFormat("if (a) g();\n" 1007 "else {\n" 1008 " g();\n" 1009 "}", 1010 AllowsMergedIf); 1011 verifyFormat("if (a) {\n" 1012 " g();\n" 1013 "} else {\n" 1014 " g();\n" 1015 "}", 1016 AllowsMergedIf); 1017 verifyFormat("if (a) g();\n" 1018 "else if (b)\n" 1019 " g();\n" 1020 "else\n" 1021 " g();", 1022 AllowsMergedIf); 1023 verifyFormat("if (a) {\n" 1024 " g();\n" 1025 "} else if (b)\n" 1026 " g();\n" 1027 "else\n" 1028 " g();", 1029 AllowsMergedIf); 1030 verifyFormat("if (a) g();\n" 1031 "else if (b) {\n" 1032 " g();\n" 1033 "} else\n" 1034 " g();", 1035 AllowsMergedIf); 1036 verifyFormat("if (a) g();\n" 1037 "else if (b)\n" 1038 " g();\n" 1039 "else {\n" 1040 " g();\n" 1041 "}", 1042 AllowsMergedIf); 1043 verifyFormat("if (a) g();\n" 1044 "else if (b) {\n" 1045 " g();\n" 1046 "} else {\n" 1047 " g();\n" 1048 "}", 1049 AllowsMergedIf); 1050 verifyFormat("if (a) {\n" 1051 " g();\n" 1052 "} else if (b) {\n" 1053 " g();\n" 1054 "} else {\n" 1055 " g();\n" 1056 "}", 1057 AllowsMergedIf); 1058 verifyFormat("MYIF (a) f();\n" 1059 "else {\n" 1060 " g();\n" 1061 "}", 1062 AllowsMergedIf); 1063 verifyFormat("MYIF (a) f();\n" 1064 "else {\n" 1065 " if (a) f();\n" 1066 " else {\n" 1067 " g();\n" 1068 " }\n" 1069 " g();\n" 1070 "}", 1071 AllowsMergedIf); 1072 1073 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1074 verifyFormat("MYIF (a) {\n" 1075 " g()\n" 1076 "};", 1077 AllowsMergedIf); 1078 verifyFormat("MYIF (a) g();\n" 1079 "else\n" 1080 " g();", 1081 AllowsMergedIf); 1082 verifyFormat("MYIF (a) {\n" 1083 " g();\n" 1084 "} else\n" 1085 " g();", 1086 AllowsMergedIf); 1087 verifyFormat("MYIF (a) g();\n" 1088 "else {\n" 1089 " g();\n" 1090 "}", 1091 AllowsMergedIf); 1092 verifyFormat("MYIF (a) {\n" 1093 " g();\n" 1094 "} else {\n" 1095 " g();\n" 1096 "}", 1097 AllowsMergedIf); 1098 verifyFormat("MYIF (a) g();\n" 1099 "else MYIF (b)\n" 1100 " g();\n" 1101 "else\n" 1102 " g();", 1103 AllowsMergedIf); 1104 verifyFormat("MYIF (a) g();\n" 1105 "else if (b)\n" 1106 " g();\n" 1107 "else\n" 1108 " g();", 1109 AllowsMergedIf); 1110 verifyFormat("MYIF (a) {\n" 1111 " g();\n" 1112 "} else MYIF (b)\n" 1113 " g();\n" 1114 "else\n" 1115 " g();", 1116 AllowsMergedIf); 1117 verifyFormat("MYIF (a) {\n" 1118 " g();\n" 1119 "} else if (b)\n" 1120 " g();\n" 1121 "else\n" 1122 " g();", 1123 AllowsMergedIf); 1124 verifyFormat("MYIF (a) g();\n" 1125 "else MYIF (b) {\n" 1126 " g();\n" 1127 "} else\n" 1128 " g();", 1129 AllowsMergedIf); 1130 verifyFormat("MYIF (a) g();\n" 1131 "else if (b) {\n" 1132 " g();\n" 1133 "} else\n" 1134 " g();", 1135 AllowsMergedIf); 1136 verifyFormat("MYIF (a) g();\n" 1137 "else MYIF (b)\n" 1138 " g();\n" 1139 "else {\n" 1140 " g();\n" 1141 "}", 1142 AllowsMergedIf); 1143 verifyFormat("MYIF (a) g();\n" 1144 "else if (b)\n" 1145 " g();\n" 1146 "else {\n" 1147 " g();\n" 1148 "}", 1149 AllowsMergedIf); 1150 verifyFormat("MYIF (a) g();\n" 1151 "else MYIF (b) {\n" 1152 " g();\n" 1153 "} else {\n" 1154 " g();\n" 1155 "}", 1156 AllowsMergedIf); 1157 verifyFormat("MYIF (a) g();\n" 1158 "else if (b) {\n" 1159 " g();\n" 1160 "} else {\n" 1161 " g();\n" 1162 "}", 1163 AllowsMergedIf); 1164 verifyFormat("MYIF (a) {\n" 1165 " g();\n" 1166 "} else MYIF (b) {\n" 1167 " g();\n" 1168 "} else {\n" 1169 " g();\n" 1170 "}", 1171 AllowsMergedIf); 1172 verifyFormat("MYIF (a) {\n" 1173 " g();\n" 1174 "} else if (b) {\n" 1175 " g();\n" 1176 "} else {\n" 1177 " g();\n" 1178 "}", 1179 AllowsMergedIf); 1180 1181 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1182 FormatStyle::SIS_AllIfsAndElse; 1183 1184 verifyFormat("if (a) f();\n" 1185 "else {\n" 1186 " g();\n" 1187 "}", 1188 AllowsMergedIf); 1189 verifyFormat("if (a) f();\n" 1190 "else {\n" 1191 " if (a) f();\n" 1192 " else {\n" 1193 " g();\n" 1194 " }\n" 1195 " g();\n" 1196 "}", 1197 AllowsMergedIf); 1198 1199 verifyFormat("if (a) g();", AllowsMergedIf); 1200 verifyFormat("if (a) {\n" 1201 " g()\n" 1202 "};", 1203 AllowsMergedIf); 1204 verifyFormat("if (a) g();\n" 1205 "else g();", 1206 AllowsMergedIf); 1207 verifyFormat("if (a) {\n" 1208 " g();\n" 1209 "} else g();", 1210 AllowsMergedIf); 1211 verifyFormat("if (a) g();\n" 1212 "else {\n" 1213 " g();\n" 1214 "}", 1215 AllowsMergedIf); 1216 verifyFormat("if (a) {\n" 1217 " g();\n" 1218 "} else {\n" 1219 " g();\n" 1220 "}", 1221 AllowsMergedIf); 1222 verifyFormat("if (a) g();\n" 1223 "else if (b) g();\n" 1224 "else g();", 1225 AllowsMergedIf); 1226 verifyFormat("if (a) {\n" 1227 " g();\n" 1228 "} else if (b) g();\n" 1229 "else g();", 1230 AllowsMergedIf); 1231 verifyFormat("if (a) g();\n" 1232 "else if (b) {\n" 1233 " g();\n" 1234 "} else g();", 1235 AllowsMergedIf); 1236 verifyFormat("if (a) g();\n" 1237 "else if (b) g();\n" 1238 "else {\n" 1239 " g();\n" 1240 "}", 1241 AllowsMergedIf); 1242 verifyFormat("if (a) g();\n" 1243 "else if (b) {\n" 1244 " g();\n" 1245 "} else {\n" 1246 " g();\n" 1247 "}", 1248 AllowsMergedIf); 1249 verifyFormat("if (a) {\n" 1250 " g();\n" 1251 "} else if (b) {\n" 1252 " g();\n" 1253 "} else {\n" 1254 " g();\n" 1255 "}", 1256 AllowsMergedIf); 1257 verifyFormat("MYIF (a) f();\n" 1258 "else {\n" 1259 " g();\n" 1260 "}", 1261 AllowsMergedIf); 1262 verifyFormat("MYIF (a) f();\n" 1263 "else {\n" 1264 " if (a) f();\n" 1265 " else {\n" 1266 " g();\n" 1267 " }\n" 1268 " g();\n" 1269 "}", 1270 AllowsMergedIf); 1271 1272 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1273 verifyFormat("MYIF (a) {\n" 1274 " g()\n" 1275 "};", 1276 AllowsMergedIf); 1277 verifyFormat("MYIF (a) g();\n" 1278 "else g();", 1279 AllowsMergedIf); 1280 verifyFormat("MYIF (a) {\n" 1281 " g();\n" 1282 "} else g();", 1283 AllowsMergedIf); 1284 verifyFormat("MYIF (a) g();\n" 1285 "else {\n" 1286 " g();\n" 1287 "}", 1288 AllowsMergedIf); 1289 verifyFormat("MYIF (a) {\n" 1290 " g();\n" 1291 "} else {\n" 1292 " g();\n" 1293 "}", 1294 AllowsMergedIf); 1295 verifyFormat("MYIF (a) g();\n" 1296 "else MYIF (b) g();\n" 1297 "else g();", 1298 AllowsMergedIf); 1299 verifyFormat("MYIF (a) g();\n" 1300 "else if (b) g();\n" 1301 "else g();", 1302 AllowsMergedIf); 1303 verifyFormat("MYIF (a) {\n" 1304 " g();\n" 1305 "} else MYIF (b) g();\n" 1306 "else g();", 1307 AllowsMergedIf); 1308 verifyFormat("MYIF (a) {\n" 1309 " g();\n" 1310 "} else if (b) g();\n" 1311 "else g();", 1312 AllowsMergedIf); 1313 verifyFormat("MYIF (a) g();\n" 1314 "else MYIF (b) {\n" 1315 " g();\n" 1316 "} else g();", 1317 AllowsMergedIf); 1318 verifyFormat("MYIF (a) g();\n" 1319 "else if (b) {\n" 1320 " g();\n" 1321 "} else g();", 1322 AllowsMergedIf); 1323 verifyFormat("MYIF (a) g();\n" 1324 "else MYIF (b) g();\n" 1325 "else {\n" 1326 " g();\n" 1327 "}", 1328 AllowsMergedIf); 1329 verifyFormat("MYIF (a) g();\n" 1330 "else if (b) g();\n" 1331 "else {\n" 1332 " g();\n" 1333 "}", 1334 AllowsMergedIf); 1335 verifyFormat("MYIF (a) g();\n" 1336 "else MYIF (b) {\n" 1337 " g();\n" 1338 "} else {\n" 1339 " g();\n" 1340 "}", 1341 AllowsMergedIf); 1342 verifyFormat("MYIF (a) g();\n" 1343 "else if (b) {\n" 1344 " g();\n" 1345 "} else {\n" 1346 " g();\n" 1347 "}", 1348 AllowsMergedIf); 1349 verifyFormat("MYIF (a) {\n" 1350 " g();\n" 1351 "} else MYIF (b) {\n" 1352 " g();\n" 1353 "} else {\n" 1354 " g();\n" 1355 "}", 1356 AllowsMergedIf); 1357 verifyFormat("MYIF (a) {\n" 1358 " g();\n" 1359 "} else if (b) {\n" 1360 " g();\n" 1361 "} else {\n" 1362 " g();\n" 1363 "}", 1364 AllowsMergedIf); 1365 } 1366 1367 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 1368 verifyFormat("while (true)\n" 1369 " ;"); 1370 verifyFormat("for (;;)\n" 1371 " ;"); 1372 1373 FormatStyle AllowsMergedLoops = getLLVMStyle(); 1374 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 1375 1376 verifyFormat("while (true) continue;", AllowsMergedLoops); 1377 verifyFormat("for (;;) continue;", AllowsMergedLoops); 1378 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 1379 verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops); 1380 verifyFormat("while (true);", AllowsMergedLoops); 1381 verifyFormat("for (;;);", AllowsMergedLoops); 1382 verifyFormat("for (;;)\n" 1383 " for (;;) continue;", 1384 AllowsMergedLoops); 1385 verifyFormat("for (;;)\n" 1386 " while (true) continue;", 1387 AllowsMergedLoops); 1388 verifyFormat("while (true)\n" 1389 " for (;;) continue;", 1390 AllowsMergedLoops); 1391 verifyFormat("BOOST_FOREACH (int &v, vec)\n" 1392 " for (;;) continue;", 1393 AllowsMergedLoops); 1394 verifyFormat("for (;;)\n" 1395 " BOOST_FOREACH (int &v, vec) continue;", 1396 AllowsMergedLoops); 1397 verifyFormat("for (;;) // Can't merge this\n" 1398 " continue;", 1399 AllowsMergedLoops); 1400 verifyFormat("for (;;) /* still don't merge */\n" 1401 " continue;", 1402 AllowsMergedLoops); 1403 verifyFormat("do a++;\n" 1404 "while (true);", 1405 AllowsMergedLoops); 1406 verifyFormat("do /* Don't merge */\n" 1407 " a++;\n" 1408 "while (true);", 1409 AllowsMergedLoops); 1410 verifyFormat("do // Don't merge\n" 1411 " a++;\n" 1412 "while (true);", 1413 AllowsMergedLoops); 1414 verifyFormat("do\n" 1415 " // Don't merge\n" 1416 " a++;\n" 1417 "while (true);", 1418 AllowsMergedLoops); 1419 1420 // Without braces labels are interpreted differently. 1421 verifyFormat("{\n" 1422 " do\n" 1423 " label:\n" 1424 " a++;\n" 1425 " while (true);\n" 1426 "}", 1427 AllowsMergedLoops); 1428 1429 // Don't merge if there are comments before the null statement. 1430 verifyFormat("while (1) //\n" 1431 " ;", 1432 AllowsMergedLoops); 1433 verifyFormat("for (;;) /**/\n" 1434 " ;", 1435 AllowsMergedLoops); 1436 verifyFormat("while (true) /**/\n" 1437 " ;", 1438 "while (true) /**/;", AllowsMergedLoops); 1439 } 1440 1441 TEST_F(FormatTest, FormatShortBracedStatements) { 1442 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 1443 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false); 1444 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine, 1445 FormatStyle::SIS_Never); 1446 EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false); 1447 EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false); 1448 verifyFormat("for (;;) {\n" 1449 " f();\n" 1450 "}"); 1451 verifyFormat("/*comment*/ for (;;) {\n" 1452 " f();\n" 1453 "}"); 1454 verifyFormat("BOOST_FOREACH (int v, vec) {\n" 1455 " f();\n" 1456 "}"); 1457 verifyFormat("/*comment*/ BOOST_FOREACH (int v, vec) {\n" 1458 " f();\n" 1459 "}"); 1460 verifyFormat("while (true) {\n" 1461 " f();\n" 1462 "}"); 1463 verifyFormat("/*comment*/ while (true) {\n" 1464 " f();\n" 1465 "}"); 1466 verifyFormat("if (true) {\n" 1467 " f();\n" 1468 "}"); 1469 verifyFormat("/*comment*/ if (true) {\n" 1470 " f();\n" 1471 "}"); 1472 1473 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1474 FormatStyle::SBS_Empty; 1475 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1476 FormatStyle::SIS_WithoutElse; 1477 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1478 verifyFormat("if (i) break;", AllowSimpleBracedStatements); 1479 verifyFormat("if (i > 0) {\n" 1480 " return i;\n" 1481 "}", 1482 AllowSimpleBracedStatements); 1483 1484 AllowSimpleBracedStatements.IfMacros.push_back("MYIF"); 1485 // Where line-lengths matter, a 2-letter synonym that maintains line length. 1486 // Not IF to avoid any confusion that IF is somehow special. 1487 AllowSimpleBracedStatements.IfMacros.push_back("FI"); 1488 AllowSimpleBracedStatements.ColumnLimit = 40; 1489 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1490 FormatStyle::SBS_Always; 1491 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1492 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 1493 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 1494 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 1495 1496 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1497 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1498 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1499 verifyFormat("if consteval {}", AllowSimpleBracedStatements); 1500 verifyFormat("if !consteval {}", AllowSimpleBracedStatements); 1501 verifyFormat("if CONSTEVAL {}", AllowSimpleBracedStatements); 1502 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1503 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1504 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1505 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1506 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1507 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1508 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1509 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1510 verifyFormat("if consteval { f(); }", AllowSimpleBracedStatements); 1511 verifyFormat("if CONSTEVAL { f(); }", AllowSimpleBracedStatements); 1512 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1513 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1514 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1515 verifyFormat("MYIF consteval { f(); }", AllowSimpleBracedStatements); 1516 verifyFormat("MYIF CONSTEVAL { f(); }", AllowSimpleBracedStatements); 1517 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1518 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1519 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1520 AllowSimpleBracedStatements); 1521 verifyFormat("if (true) {\n" 1522 " ffffffffffffffffffffffff();\n" 1523 "}", 1524 AllowSimpleBracedStatements); 1525 verifyFormat("if (true) {\n" 1526 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1527 "}", 1528 AllowSimpleBracedStatements); 1529 verifyFormat("if (true) { //\n" 1530 " f();\n" 1531 "}", 1532 AllowSimpleBracedStatements); 1533 verifyFormat("if (true) {\n" 1534 " f();\n" 1535 " f();\n" 1536 "}", 1537 AllowSimpleBracedStatements); 1538 verifyFormat("if (true) {\n" 1539 " f();\n" 1540 "} else {\n" 1541 " f();\n" 1542 "}", 1543 AllowSimpleBracedStatements); 1544 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1545 AllowSimpleBracedStatements); 1546 verifyFormat("MYIF (true) {\n" 1547 " ffffffffffffffffffffffff();\n" 1548 "}", 1549 AllowSimpleBracedStatements); 1550 verifyFormat("MYIF (true) {\n" 1551 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1552 "}", 1553 AllowSimpleBracedStatements); 1554 verifyFormat("MYIF (true) { //\n" 1555 " f();\n" 1556 "}", 1557 AllowSimpleBracedStatements); 1558 verifyFormat("MYIF (true) {\n" 1559 " f();\n" 1560 " f();\n" 1561 "}", 1562 AllowSimpleBracedStatements); 1563 verifyFormat("MYIF (true) {\n" 1564 " f();\n" 1565 "} else {\n" 1566 " f();\n" 1567 "}", 1568 AllowSimpleBracedStatements); 1569 1570 verifyFormat("struct A2 {\n" 1571 " int X;\n" 1572 "};", 1573 AllowSimpleBracedStatements); 1574 verifyFormat("typedef struct A2 {\n" 1575 " int X;\n" 1576 "} A2_t;", 1577 AllowSimpleBracedStatements); 1578 verifyFormat("template <int> struct A2 {\n" 1579 " struct B {};\n" 1580 "};", 1581 AllowSimpleBracedStatements); 1582 1583 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1584 FormatStyle::SIS_Never; 1585 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1586 verifyFormat("if (true) {\n" 1587 " f();\n" 1588 "}", 1589 AllowSimpleBracedStatements); 1590 verifyFormat("if (true) {\n" 1591 " f();\n" 1592 "} else {\n" 1593 " f();\n" 1594 "}", 1595 AllowSimpleBracedStatements); 1596 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1597 verifyFormat("MYIF (true) {\n" 1598 " f();\n" 1599 "}", 1600 AllowSimpleBracedStatements); 1601 verifyFormat("MYIF (true) {\n" 1602 " f();\n" 1603 "} else {\n" 1604 " f();\n" 1605 "}", 1606 AllowSimpleBracedStatements); 1607 1608 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1609 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1610 verifyFormat("while (true) {\n" 1611 " f();\n" 1612 "}", 1613 AllowSimpleBracedStatements); 1614 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1615 verifyFormat("for (;;) {\n" 1616 " f();\n" 1617 "}", 1618 AllowSimpleBracedStatements); 1619 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); 1620 verifyFormat("BOOST_FOREACH (int v, vec) {\n" 1621 " f();\n" 1622 "}", 1623 AllowSimpleBracedStatements); 1624 1625 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1626 FormatStyle::SIS_WithoutElse; 1627 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1628 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = 1629 FormatStyle::BWACS_Always; 1630 1631 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1632 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1633 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1634 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1635 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1636 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1637 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1638 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1639 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1640 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1641 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1642 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1643 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1644 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1645 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1646 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1647 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1648 AllowSimpleBracedStatements); 1649 verifyFormat("if (true)\n" 1650 "{\n" 1651 " ffffffffffffffffffffffff();\n" 1652 "}", 1653 AllowSimpleBracedStatements); 1654 verifyFormat("if (true)\n" 1655 "{\n" 1656 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1657 "}", 1658 AllowSimpleBracedStatements); 1659 verifyFormat("if (true)\n" 1660 "{ //\n" 1661 " f();\n" 1662 "}", 1663 AllowSimpleBracedStatements); 1664 verifyFormat("if (true)\n" 1665 "{\n" 1666 " f();\n" 1667 " f();\n" 1668 "}", 1669 AllowSimpleBracedStatements); 1670 verifyFormat("if (true)\n" 1671 "{\n" 1672 " f();\n" 1673 "} else\n" 1674 "{\n" 1675 " f();\n" 1676 "}", 1677 AllowSimpleBracedStatements); 1678 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1679 AllowSimpleBracedStatements); 1680 verifyFormat("MYIF (true)\n" 1681 "{\n" 1682 " ffffffffffffffffffffffff();\n" 1683 "}", 1684 AllowSimpleBracedStatements); 1685 verifyFormat("MYIF (true)\n" 1686 "{\n" 1687 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1688 "}", 1689 AllowSimpleBracedStatements); 1690 verifyFormat("MYIF (true)\n" 1691 "{ //\n" 1692 " f();\n" 1693 "}", 1694 AllowSimpleBracedStatements); 1695 verifyFormat("MYIF (true)\n" 1696 "{\n" 1697 " f();\n" 1698 " f();\n" 1699 "}", 1700 AllowSimpleBracedStatements); 1701 verifyFormat("MYIF (true)\n" 1702 "{\n" 1703 " f();\n" 1704 "} else\n" 1705 "{\n" 1706 " f();\n" 1707 "}", 1708 AllowSimpleBracedStatements); 1709 1710 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1711 FormatStyle::SIS_Never; 1712 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1713 verifyFormat("if (true)\n" 1714 "{\n" 1715 " f();\n" 1716 "}", 1717 AllowSimpleBracedStatements); 1718 verifyFormat("if (true)\n" 1719 "{\n" 1720 " f();\n" 1721 "} else\n" 1722 "{\n" 1723 " f();\n" 1724 "}", 1725 AllowSimpleBracedStatements); 1726 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1727 verifyFormat("MYIF (true)\n" 1728 "{\n" 1729 " f();\n" 1730 "}", 1731 AllowSimpleBracedStatements); 1732 verifyFormat("MYIF (true)\n" 1733 "{\n" 1734 " f();\n" 1735 "} else\n" 1736 "{\n" 1737 " f();\n" 1738 "}", 1739 AllowSimpleBracedStatements); 1740 1741 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1742 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1743 verifyFormat("while (true)\n" 1744 "{\n" 1745 " f();\n" 1746 "}", 1747 AllowSimpleBracedStatements); 1748 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1749 verifyFormat("for (;;)\n" 1750 "{\n" 1751 " f();\n" 1752 "}", 1753 AllowSimpleBracedStatements); 1754 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements); 1755 verifyFormat("BOOST_FOREACH (int v, vec)\n" 1756 "{\n" 1757 " f();\n" 1758 "}", 1759 AllowSimpleBracedStatements); 1760 1761 FormatStyle Style = getLLVMStyle(); 1762 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1763 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 1764 1765 verifyFormat("while (i > 0)\n" 1766 "{\n" 1767 " --i;\n" 1768 "}", 1769 Style); 1770 1771 verifyFormat("if (a)\n" 1772 "{\n" 1773 " ++b;\n" 1774 "}", 1775 Style); 1776 1777 verifyFormat("if (a)\n" 1778 "{\n" 1779 " b = 1;\n" 1780 "} else\n" 1781 "{\n" 1782 " b = 0;\n" 1783 "}", 1784 Style); 1785 1786 verifyFormat("if (a)\n" 1787 "{\n" 1788 " b = 1;\n" 1789 "} else if (c)\n" 1790 "{\n" 1791 " b = 2;\n" 1792 "} else\n" 1793 "{\n" 1794 " b = 0;\n" 1795 "}", 1796 Style); 1797 1798 Style.BraceWrapping.BeforeElse = true; 1799 1800 verifyFormat("if (a)\n" 1801 "{\n" 1802 " b = 1;\n" 1803 "}\n" 1804 "else\n" 1805 "{\n" 1806 " b = 0;\n" 1807 "}", 1808 Style); 1809 1810 verifyFormat("if (a)\n" 1811 "{\n" 1812 " b = 1;\n" 1813 "}\n" 1814 "else if (c)\n" 1815 "{\n" 1816 " b = 2;\n" 1817 "}\n" 1818 "else\n" 1819 "{\n" 1820 " b = 0;\n" 1821 "}", 1822 Style); 1823 } 1824 1825 TEST_F(FormatTest, UnderstandsMacros) { 1826 verifyFormat("#define A (parentheses)"); 1827 verifyFormat("/* comment */ #define A (parentheses)"); 1828 verifyFormat("/* comment */ /* another comment */ #define A (parentheses)"); 1829 // Even the partial code should never be merged. 1830 verifyNoChange("/* comment */ #define A (parentheses)\n" 1831 "#"); 1832 verifyFormat("/* comment */ #define A (parentheses)\n" 1833 "#\n"); 1834 verifyFormat("/* comment */ #define A (parentheses)\n" 1835 "#define B (parentheses)"); 1836 verifyFormat("#define true ((int)1)"); 1837 verifyFormat("#define and(x)"); 1838 verifyFormat("#define if(x) x"); 1839 verifyFormat("#define return(x) (x)"); 1840 verifyFormat("#define while(x) for (; x;)"); 1841 verifyFormat("#define xor(x) (^(x))"); 1842 verifyFormat("#define __except(x)"); 1843 verifyFormat("#define __try(x)"); 1844 1845 // https://llvm.org/PR54348. 1846 verifyFormat( 1847 "#define A" 1848 " " 1849 "\\\n" 1850 " class & {}"); 1851 1852 FormatStyle Style = getLLVMStyle(); 1853 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1854 Style.BraceWrapping.AfterFunction = true; 1855 // Test that a macro definition never gets merged with the following 1856 // definition. 1857 // FIXME: The AAA macro definition probably should not be split into 3 lines. 1858 verifyFormat("#define AAA " 1859 " \\\n" 1860 " N " 1861 " \\\n" 1862 " {\n" 1863 "#define BBB }", 1864 Style); 1865 // verifyFormat("#define AAA N { //", Style); 1866 1867 verifyFormat("MACRO(return)"); 1868 verifyFormat("MACRO(co_await)"); 1869 verifyFormat("MACRO(co_return)"); 1870 verifyFormat("MACRO(co_yield)"); 1871 verifyFormat("MACRO(return, something)"); 1872 verifyFormat("MACRO(co_return, something)"); 1873 verifyFormat("MACRO(something##something)"); 1874 verifyFormat("MACRO(return##something)"); 1875 verifyFormat("MACRO(co_return##something)"); 1876 1877 verifyFormat("#define A x:"); 1878 1879 verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n" 1880 " { \\\n" 1881 " #Bar \\\n" 1882 " }"); 1883 verifyFormat("#define Foo(Bar) {#Bar}", "#define Foo(Bar) \\\n" 1884 " { #Bar }"); 1885 } 1886 1887 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 1888 FormatStyle Style = getLLVMStyleWithColumns(60); 1889 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 1890 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 1891 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1892 verifyFormat("#define A \\\n" 1893 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 1894 " { \\\n" 1895 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1896 " }\n" 1897 "X;", 1898 "#define A \\\n" 1899 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 1900 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1901 " }\n" 1902 "X;", 1903 Style); 1904 } 1905 1906 TEST_F(FormatTest, ParseIfElse) { 1907 verifyFormat("if (true)\n" 1908 " if (true)\n" 1909 " if (true)\n" 1910 " f();\n" 1911 " else\n" 1912 " g();\n" 1913 " else\n" 1914 " h();\n" 1915 "else\n" 1916 " i();"); 1917 verifyFormat("if (true)\n" 1918 " if (true)\n" 1919 " if (true) {\n" 1920 " if (true)\n" 1921 " f();\n" 1922 " } else {\n" 1923 " g();\n" 1924 " }\n" 1925 " else\n" 1926 " h();\n" 1927 "else {\n" 1928 " i();\n" 1929 "}"); 1930 verifyFormat("if (true)\n" 1931 " if constexpr (true)\n" 1932 " if (true) {\n" 1933 " if constexpr (true)\n" 1934 " f();\n" 1935 " } else {\n" 1936 " g();\n" 1937 " }\n" 1938 " else\n" 1939 " h();\n" 1940 "else {\n" 1941 " i();\n" 1942 "}"); 1943 verifyFormat("if (true)\n" 1944 " if CONSTEXPR (true)\n" 1945 " if (true) {\n" 1946 " if CONSTEXPR (true)\n" 1947 " f();\n" 1948 " } else {\n" 1949 " g();\n" 1950 " }\n" 1951 " else\n" 1952 " h();\n" 1953 "else {\n" 1954 " i();\n" 1955 "}"); 1956 verifyFormat("void f() {\n" 1957 " if (a) {\n" 1958 " } else {\n" 1959 " }\n" 1960 "}"); 1961 } 1962 1963 TEST_F(FormatTest, ElseIf) { 1964 verifyFormat("if (a) {\n} else if (b) {\n}"); 1965 verifyFormat("if (a)\n" 1966 " f();\n" 1967 "else if (b)\n" 1968 " g();\n" 1969 "else\n" 1970 " h();"); 1971 verifyFormat("if (a)\n" 1972 " f();\n" 1973 "else // comment\n" 1974 " if (b) {\n" 1975 " g();\n" 1976 " h();\n" 1977 " }"); 1978 verifyFormat("if constexpr (a)\n" 1979 " f();\n" 1980 "else if constexpr (b)\n" 1981 " g();\n" 1982 "else\n" 1983 " h();"); 1984 verifyFormat("if CONSTEXPR (a)\n" 1985 " f();\n" 1986 "else if CONSTEXPR (b)\n" 1987 " g();\n" 1988 "else\n" 1989 " h();"); 1990 verifyFormat("if (a) {\n" 1991 " f();\n" 1992 "}\n" 1993 "// or else ..\n" 1994 "else {\n" 1995 " g()\n" 1996 "}"); 1997 1998 verifyFormat("if (a) {\n" 1999 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 2001 "}"); 2002 verifyFormat("if (a) {\n" 2003 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 2005 "}"); 2006 verifyFormat("if (a) {\n" 2007 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2008 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 2009 "}"); 2010 verifyFormat("if (a) {\n" 2011 "} else if (\n" 2012 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2013 "}", 2014 getLLVMStyleWithColumns(62)); 2015 verifyFormat("if (a) {\n" 2016 "} else if constexpr (\n" 2017 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2018 "}", 2019 getLLVMStyleWithColumns(62)); 2020 verifyFormat("if (a) {\n" 2021 "} else if CONSTEXPR (\n" 2022 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2023 "}", 2024 getLLVMStyleWithColumns(62)); 2025 } 2026 2027 TEST_F(FormatTest, SeparatePointerReferenceAlignment) { 2028 FormatStyle Style = getLLVMStyle(); 2029 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right); 2030 EXPECT_EQ(Style.ReferenceAlignment, FormatStyle::RAS_Pointer); 2031 verifyFormat("int *f1(int *a, int &b, int &&c);", Style); 2032 verifyFormat("int &f2(int &&c, int *a, int &b);", Style); 2033 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style); 2034 verifyFormat("int *f1(int &a) const &;", Style); 2035 verifyFormat("int *f1(int &a) const & = 0;", Style); 2036 verifyFormat("int *a = f1();", Style); 2037 verifyFormat("int &b = f2();", Style); 2038 verifyFormat("int &&c = f3();", Style); 2039 verifyFormat("int f3() { return sizeof(Foo &); }", Style); 2040 verifyFormat("int f4() { return sizeof(Foo &&); }", Style); 2041 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style); 2042 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style); 2043 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 2044 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style); 2045 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 2046 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2047 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 2048 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style); 2049 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 2050 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style); 2051 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style); 2052 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style); 2053 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style); 2054 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style); 2055 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style); 2056 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style); 2057 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style); 2058 verifyFormat("for (f(); int &c : {1, 2, 3})", Style); 2059 verifyFormat( 2060 "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n" 2061 " res2 = [](int &a) { return 0000000000000; };", 2062 Style); 2063 2064 Style.AlignConsecutiveDeclarations.Enabled = true; 2065 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; 2066 verifyFormat("Const unsigned int *c;\n" 2067 "const unsigned int *d;\n" 2068 "Const unsigned int &e;\n" 2069 "const unsigned int &f;\n" 2070 "int *f1(int *a, int &b, int &&c);\n" 2071 "double *(*f2)(int *a, double &&b);\n" 2072 "const unsigned &&g;\n" 2073 "Const unsigned h;", 2074 Style); 2075 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false; 2076 verifyFormat("Const unsigned int *c;\n" 2077 "const unsigned int *d;\n" 2078 "Const unsigned int &e;\n" 2079 "const unsigned int &f;\n" 2080 "int *f1(int *a, int &b, int &&c);\n" 2081 "double *(*f2)(int *a, double &&b);\n" 2082 "const unsigned &&g;\n" 2083 "Const unsigned h;", 2084 Style); 2085 2086 Style.PointerAlignment = FormatStyle::PAS_Left; 2087 verifyFormat("int* f1(int* a, int& b, int&& c);", Style); 2088 verifyFormat("int& f2(int&& c, int* a, int& b);", Style); 2089 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style); 2090 verifyFormat("int* f1(int& a) const& = 0;", Style); 2091 verifyFormat("int* a = f1();", Style); 2092 verifyFormat("int& b = f2();", Style); 2093 verifyFormat("int&& c = f3();", Style); 2094 verifyFormat("int f3() { return sizeof(Foo&); }", Style); 2095 verifyFormat("int f4() { return sizeof(Foo&&); }", Style); 2096 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style); 2097 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style); 2098 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 2099 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style); 2100 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 2101 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2102 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 2103 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style); 2104 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 2105 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2106 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style); 2107 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style); 2108 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style); 2109 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2110 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style); 2111 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style); 2112 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style); 2113 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style); 2114 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style); 2115 verifyFormat("for (f(); int& c : {1, 2, 3})", Style); 2116 verifyFormat( 2117 "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n" 2118 " res2 = [](int& a) { return 0000000000000; };", 2119 Style); 2120 verifyFormat("[](decltype(foo)& Bar) {}", Style); 2121 2122 Style.AlignConsecutiveDeclarations.Enabled = true; 2123 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; 2124 verifyFormat("Const unsigned int* c;\n" 2125 "const unsigned int* d;\n" 2126 "Const unsigned int& e;\n" 2127 "const unsigned int& f;\n" 2128 "int* f1(int* a, int& b, int&& c);\n" 2129 "double* (*f2)(int* a, double&& b);\n" 2130 "const unsigned&& g;\n" 2131 "Const unsigned h;", 2132 Style); 2133 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false; 2134 verifyFormat("Const unsigned int* c;\n" 2135 "const unsigned int* d;\n" 2136 "Const unsigned int& e;\n" 2137 "const unsigned int& f;\n" 2138 "int* f1(int* a, int& b, int&& c);\n" 2139 "double* (*f2)(int* a, double&& b);\n" 2140 "const unsigned&& g;\n" 2141 "Const unsigned h;", 2142 Style); 2143 2144 Style.PointerAlignment = FormatStyle::PAS_Right; 2145 Style.ReferenceAlignment = FormatStyle::RAS_Left; 2146 verifyFormat("int *f1(int *a, int& b, int&& c);", Style); 2147 verifyFormat("int& f2(int&& c, int *a, int& b);", Style); 2148 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style); 2149 verifyFormat("int *a = f1();", Style); 2150 verifyFormat("int& b = f2();", Style); 2151 verifyFormat("int&& c = f3();", Style); 2152 verifyFormat("int f3() { return sizeof(Foo&); }", Style); 2153 verifyFormat("int f4() { return sizeof(Foo&&); }", Style); 2154 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style); 2155 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style); 2156 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2157 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2158 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style); 2159 2160 Style.AlignConsecutiveDeclarations.Enabled = true; 2161 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; 2162 verifyFormat("Const unsigned int *c;\n" 2163 "const unsigned int *d;\n" 2164 "Const unsigned int& e;\n" 2165 "const unsigned int& f;\n" 2166 "int *f1(int *a, int& b, int&& c);\n" 2167 "double *(*f2)(int *a, double&& b);\n" 2168 "const unsigned&& g;\n" 2169 "Const unsigned h;", 2170 Style); 2171 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false; 2172 verifyFormat("Const unsigned int *c;\n" 2173 "const unsigned int *d;\n" 2174 "Const unsigned int& e;\n" 2175 "const unsigned int& f;\n" 2176 "int *f1(int *a, int& b, int&& c);\n" 2177 "double *(*f2)(int *a, double&& b);\n" 2178 "const unsigned&& g;\n" 2179 "Const unsigned h;", 2180 Style); 2181 2182 Style.PointerAlignment = FormatStyle::PAS_Left; 2183 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 2184 verifyFormat("int* f1(int* a, int & b, int && c);", Style); 2185 verifyFormat("int & f2(int && c, int* a, int & b);", Style); 2186 verifyFormat("int && f3(int & b, int && c, int* a);", Style); 2187 verifyFormat("int* a = f1();", Style); 2188 verifyFormat("int & b = f2();", Style); 2189 verifyFormat("int && c = f3();", Style); 2190 verifyFormat("int f3() { return sizeof(Foo &); }", Style); 2191 verifyFormat("int f4() { return sizeof(Foo &&); }", Style); 2192 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style); 2193 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style); 2194 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style); 2195 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style); 2196 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style); 2197 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2198 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style); 2199 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style); 2200 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style); 2201 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2202 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style); 2203 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style); 2204 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style); 2205 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style); 2206 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style); 2207 verifyFormat("for (f(); int & c : {1, 2, 3})", Style); 2208 verifyFormat( 2209 "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n" 2210 " res2 = [](int & a) { return 0000000000000; };", 2211 Style); 2212 2213 Style.AlignConsecutiveDeclarations.Enabled = true; 2214 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; 2215 verifyFormat("Const unsigned int* c;\n" 2216 "const unsigned int* d;\n" 2217 "Const unsigned int & e;\n" 2218 "const unsigned int & f;\n" 2219 "int* f1(int* a, int & b, int && c);\n" 2220 "double* (*f2)(int* a, double && b);\n" 2221 "const unsigned && g;\n" 2222 "Const unsigned h;", 2223 Style); 2224 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false; 2225 verifyFormat("Const unsigned int* c;\n" 2226 "const unsigned int* d;\n" 2227 "Const unsigned int & e;\n" 2228 "const unsigned int & f;\n" 2229 "int* f1(int* a, int & b, int && c);\n" 2230 "double* (*f2)(int* a, double && b);\n" 2231 "const unsigned && g;\n" 2232 "Const unsigned h;", 2233 Style); 2234 2235 Style.PointerAlignment = FormatStyle::PAS_Middle; 2236 Style.ReferenceAlignment = FormatStyle::RAS_Right; 2237 verifyFormat("int * f1(int * a, int &b, int &&c);", Style); 2238 verifyFormat("int &f2(int &&c, int * a, int &b);", Style); 2239 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); 2240 verifyFormat("int * a = f1();", Style); 2241 verifyFormat("int &b = f2();", Style); 2242 verifyFormat("int &&c = f3();", Style); 2243 verifyFormat("int f3() { return sizeof(Foo &); }", Style); 2244 verifyFormat("int f4() { return sizeof(Foo &&); }", Style); 2245 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style); 2246 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style); 2247 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2248 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2249 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style); 2250 2251 Style.AlignConsecutiveDeclarations.Enabled = true; 2252 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = true; 2253 verifyFormat("Const unsigned int * c;\n" 2254 "const unsigned int * d;\n" 2255 "Const unsigned int &e;\n" 2256 "const unsigned int &f;\n" 2257 "int * f1(int * a, int &b, int &&c);\n" 2258 "double * (*f2)(int * a, double &&b);\n" 2259 "const unsigned &&g;\n" 2260 "Const unsigned h;", 2261 Style); 2262 Style.AlignConsecutiveDeclarations.AlignFunctionPointers = false; 2263 verifyFormat("Const unsigned int * c;\n" 2264 "const unsigned int * d;\n" 2265 "Const unsigned int &e;\n" 2266 "const unsigned int &f;\n" 2267 "int * f1(int * a, int &b, int &&c);\n" 2268 "double * (*f2)(int * a, double &&b);\n" 2269 "const unsigned &&g;\n" 2270 "Const unsigned h;", 2271 Style); 2272 2273 // FIXME: we don't handle this yet, so output may be arbitrary until it's 2274 // specifically handled 2275 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); 2276 } 2277 2278 TEST_F(FormatTest, FormatsForLoop) { 2279 verifyFormat( 2280 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 2281 " ++VeryVeryLongLoopVariable)\n" 2282 " ;"); 2283 verifyFormat("for (;;)\n" 2284 " f();"); 2285 verifyFormat("for (;;) {\n}"); 2286 verifyFormat("for (;;) {\n" 2287 " f();\n" 2288 "}"); 2289 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 2290 2291 verifyFormat( 2292 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2293 " E = UnwrappedLines.end();\n" 2294 " I != E; ++I) {\n}"); 2295 2296 verifyFormat( 2297 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 2298 " ++IIIII) {\n}"); 2299 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 2300 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 2301 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 2302 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 2303 " I = FD->getDeclsInPrototypeScope().begin(),\n" 2304 " E = FD->getDeclsInPrototypeScope().end();\n" 2305 " I != E; ++I) {\n}"); 2306 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 2307 " I = Container.begin(),\n" 2308 " E = Container.end();\n" 2309 " I != E; ++I) {\n}", 2310 getLLVMStyleWithColumns(76)); 2311 2312 verifyFormat( 2313 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2314 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 2315 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2316 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2317 " ++aaaaaaaaaaa) {\n}"); 2318 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2319 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 2320 " ++i) {\n}"); 2321 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 2322 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2323 "}"); 2324 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 2325 " aaaaaaaaaa);\n" 2326 " iter; ++iter) {\n" 2327 "}"); 2328 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2329 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2330 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 2331 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 2332 2333 // These should not be formatted as Objective-C for-in loops. 2334 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 2335 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 2336 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 2337 verifyFormat( 2338 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 2339 2340 FormatStyle NoBinPacking = getLLVMStyle(); 2341 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; 2342 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 2343 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 2344 " aaaaaaaaaaaaaaaa,\n" 2345 " aaaaaaaaaaaaaaaa,\n" 2346 " aaaaaaaaaaaaaaaa);\n" 2347 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2348 "}", 2349 NoBinPacking); 2350 verifyFormat( 2351 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2352 " E = UnwrappedLines.end();\n" 2353 " I != E;\n" 2354 " ++I) {\n}", 2355 NoBinPacking); 2356 2357 FormatStyle AlignLeft = getLLVMStyle(); 2358 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 2359 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 2360 } 2361 2362 TEST_F(FormatTest, RangeBasedForLoops) { 2363 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 2364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2365 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 2366 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 2367 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 2368 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2369 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 2370 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 2371 } 2372 2373 TEST_F(FormatTest, ForEachLoops) { 2374 FormatStyle Style = getLLVMStyle(); 2375 EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2376 EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false); 2377 verifyFormat("void f() {\n" 2378 " for (;;) {\n" 2379 " }\n" 2380 " foreach (Item *item, itemlist) {\n" 2381 " }\n" 2382 " Q_FOREACH (Item *item, itemlist) {\n" 2383 " }\n" 2384 " BOOST_FOREACH (Item *item, itemlist) {\n" 2385 " }\n" 2386 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2387 "}", 2388 Style); 2389 verifyFormat("void f() {\n" 2390 " for (;;)\n" 2391 " int j = 1;\n" 2392 " Q_FOREACH (int v, vec)\n" 2393 " v *= 2;\n" 2394 " for (;;) {\n" 2395 " int j = 1;\n" 2396 " }\n" 2397 " Q_FOREACH (int v, vec) {\n" 2398 " v *= 2;\n" 2399 " }\n" 2400 "}", 2401 Style); 2402 2403 FormatStyle ShortBlocks = getLLVMStyle(); 2404 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2405 EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false); 2406 verifyFormat("void f() {\n" 2407 " for (;;)\n" 2408 " int j = 1;\n" 2409 " Q_FOREACH (int &v, vec)\n" 2410 " v *= 2;\n" 2411 " for (;;) {\n" 2412 " int j = 1;\n" 2413 " }\n" 2414 " Q_FOREACH (int &v, vec) {\n" 2415 " int j = 1;\n" 2416 " }\n" 2417 "}", 2418 ShortBlocks); 2419 2420 FormatStyle ShortLoops = getLLVMStyle(); 2421 ShortLoops.AllowShortLoopsOnASingleLine = true; 2422 EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2423 verifyFormat("void f() {\n" 2424 " for (;;) int j = 1;\n" 2425 " Q_FOREACH (int &v, vec) int j = 1;\n" 2426 " for (;;) {\n" 2427 " int j = 1;\n" 2428 " }\n" 2429 " Q_FOREACH (int &v, vec) {\n" 2430 " int j = 1;\n" 2431 " }\n" 2432 "}", 2433 ShortLoops); 2434 2435 FormatStyle ShortBlocksAndLoops = getLLVMStyle(); 2436 ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2437 ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true; 2438 verifyFormat("void f() {\n" 2439 " for (;;) int j = 1;\n" 2440 " Q_FOREACH (int &v, vec) int j = 1;\n" 2441 " for (;;) { int j = 1; }\n" 2442 " Q_FOREACH (int &v, vec) { int j = 1; }\n" 2443 "}", 2444 ShortBlocksAndLoops); 2445 2446 Style.SpaceBeforeParens = 2447 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 2448 verifyFormat("void f() {\n" 2449 " for (;;) {\n" 2450 " }\n" 2451 " foreach(Item *item, itemlist) {\n" 2452 " }\n" 2453 " Q_FOREACH(Item *item, itemlist) {\n" 2454 " }\n" 2455 " BOOST_FOREACH(Item *item, itemlist) {\n" 2456 " }\n" 2457 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2458 "}", 2459 Style); 2460 2461 // As function-like macros. 2462 verifyFormat("#define foreach(x, y)\n" 2463 "#define Q_FOREACH(x, y)\n" 2464 "#define BOOST_FOREACH(x, y)\n" 2465 "#define UNKNOWN_FOREACH(x, y)"); 2466 2467 // Not as function-like macros. 2468 verifyFormat("#define foreach (x, y)\n" 2469 "#define Q_FOREACH (x, y)\n" 2470 "#define BOOST_FOREACH (x, y)\n" 2471 "#define UNKNOWN_FOREACH (x, y)"); 2472 2473 // handle microsoft non standard extension 2474 verifyFormat("for each (char c in x->MyStringProperty)"); 2475 } 2476 2477 TEST_F(FormatTest, FormatsWhileLoop) { 2478 verifyFormat("while (true) {\n}"); 2479 verifyFormat("while (true)\n" 2480 " f();"); 2481 verifyFormat("while () {\n}"); 2482 verifyFormat("while () {\n" 2483 " f();\n" 2484 "}"); 2485 } 2486 2487 TEST_F(FormatTest, FormatsDoWhile) { 2488 verifyFormat("do {\n" 2489 " do_something();\n" 2490 "} while (something());"); 2491 verifyFormat("do\n" 2492 " do_something();\n" 2493 "while (something());"); 2494 } 2495 2496 TEST_F(FormatTest, FormatsSwitchStatement) { 2497 verifyFormat("switch (x) {\n" 2498 "case 1:\n" 2499 " f();\n" 2500 " break;\n" 2501 "case kFoo:\n" 2502 "case ns::kBar:\n" 2503 "case kBaz:\n" 2504 " break;\n" 2505 "default:\n" 2506 " g();\n" 2507 " break;\n" 2508 "}"); 2509 verifyFormat("switch (x) {\n" 2510 "case 1: {\n" 2511 " f();\n" 2512 " break;\n" 2513 "}\n" 2514 "case 2: {\n" 2515 " break;\n" 2516 "}\n" 2517 "}"); 2518 verifyFormat("switch (x) {\n" 2519 "case 1: {\n" 2520 " f();\n" 2521 " {\n" 2522 " g();\n" 2523 " h();\n" 2524 " }\n" 2525 " break;\n" 2526 "}\n" 2527 "}"); 2528 verifyFormat("switch (x) {\n" 2529 "case 1: {\n" 2530 " f();\n" 2531 " if (foo) {\n" 2532 " g();\n" 2533 " h();\n" 2534 " }\n" 2535 " break;\n" 2536 "}\n" 2537 "}"); 2538 verifyFormat("switch (x) {\n" 2539 "case 1: {\n" 2540 " f();\n" 2541 " g();\n" 2542 "} break;\n" 2543 "}"); 2544 verifyFormat("switch (test)\n" 2545 " ;"); 2546 verifyFormat("switch (x) {\n" 2547 "default: {\n" 2548 " // Do nothing.\n" 2549 "}\n" 2550 "}"); 2551 verifyFormat("switch (x) {\n" 2552 "// comment\n" 2553 "// if 1, do f()\n" 2554 "case 1:\n" 2555 " f();\n" 2556 "}"); 2557 verifyFormat("switch (x) {\n" 2558 "case 1:\n" 2559 " // Do amazing stuff\n" 2560 " {\n" 2561 " f();\n" 2562 " g();\n" 2563 " }\n" 2564 " break;\n" 2565 "}"); 2566 verifyFormat("#define A \\\n" 2567 " switch (x) { \\\n" 2568 " case a: \\\n" 2569 " foo = b; \\\n" 2570 " }", 2571 getLLVMStyleWithColumns(20)); 2572 verifyFormat("#define OPERATION_CASE(name) \\\n" 2573 " case OP_name: \\\n" 2574 " return operations::Operation##name", 2575 getLLVMStyleWithColumns(40)); 2576 verifyFormat("switch (x) {\n" 2577 "case 1:;\n" 2578 "default:;\n" 2579 " int i;\n" 2580 "}"); 2581 2582 verifyGoogleFormat("switch (x) {\n" 2583 " case 1:\n" 2584 " f();\n" 2585 " break;\n" 2586 " case kFoo:\n" 2587 " case ns::kBar:\n" 2588 " case kBaz:\n" 2589 " break;\n" 2590 " default:\n" 2591 " g();\n" 2592 " break;\n" 2593 "}"); 2594 verifyGoogleFormat("switch (x) {\n" 2595 " case 1: {\n" 2596 " f();\n" 2597 " break;\n" 2598 " }\n" 2599 "}"); 2600 verifyGoogleFormat("switch (test)\n" 2601 " ;"); 2602 2603 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 2604 " case OP_name: \\\n" 2605 " return operations::Operation##name"); 2606 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 2607 " // Get the correction operation class.\n" 2608 " switch (OpCode) {\n" 2609 " CASE(Add);\n" 2610 " CASE(Subtract);\n" 2611 " default:\n" 2612 " return operations::Unknown;\n" 2613 " }\n" 2614 "#undef OPERATION_CASE\n" 2615 "}"); 2616 verifyFormat("DEBUG({\n" 2617 " switch (x) {\n" 2618 " case A:\n" 2619 " f();\n" 2620 " break;\n" 2621 " // fallthrough\n" 2622 " case B:\n" 2623 " g();\n" 2624 " break;\n" 2625 " }\n" 2626 "});"); 2627 verifyNoChange("DEBUG({\n" 2628 " switch (x) {\n" 2629 " case A:\n" 2630 " f();\n" 2631 " break;\n" 2632 " // On B:\n" 2633 " case B:\n" 2634 " g();\n" 2635 " break;\n" 2636 " }\n" 2637 "});"); 2638 verifyFormat("switch (n) {\n" 2639 "case 0: {\n" 2640 " return false;\n" 2641 "}\n" 2642 "default: {\n" 2643 " return true;\n" 2644 "}\n" 2645 "}", 2646 "switch (n)\n" 2647 "{\n" 2648 "case 0: {\n" 2649 " return false;\n" 2650 "}\n" 2651 "default: {\n" 2652 " return true;\n" 2653 "}\n" 2654 "}"); 2655 verifyFormat("switch (a) {\n" 2656 "case (b):\n" 2657 " return;\n" 2658 "}"); 2659 2660 verifyFormat("switch (a) {\n" 2661 "case some_namespace::\n" 2662 " some_constant:\n" 2663 " return;\n" 2664 "}", 2665 getLLVMStyleWithColumns(34)); 2666 2667 verifyFormat("switch (a) {\n" 2668 "[[likely]] case 1:\n" 2669 " return;\n" 2670 "}"); 2671 verifyFormat("switch (a) {\n" 2672 "[[likely]] [[other::likely]] case 1:\n" 2673 " return;\n" 2674 "}"); 2675 verifyFormat("switch (x) {\n" 2676 "case 1:\n" 2677 " return;\n" 2678 "[[likely]] case 2:\n" 2679 " return;\n" 2680 "}"); 2681 verifyFormat("switch (a) {\n" 2682 "case 1:\n" 2683 "[[likely]] case 2:\n" 2684 " return;\n" 2685 "}"); 2686 FormatStyle Attributes = getLLVMStyle(); 2687 Attributes.AttributeMacros.push_back("LIKELY"); 2688 Attributes.AttributeMacros.push_back("OTHER_LIKELY"); 2689 verifyFormat("switch (a) {\n" 2690 "LIKELY case b:\n" 2691 " return;\n" 2692 "}", 2693 Attributes); 2694 verifyFormat("switch (a) {\n" 2695 "LIKELY OTHER_LIKELY() case b:\n" 2696 " return;\n" 2697 "}", 2698 Attributes); 2699 verifyFormat("switch (a) {\n" 2700 "case 1:\n" 2701 " return;\n" 2702 "LIKELY case 2:\n" 2703 " return;\n" 2704 "}", 2705 Attributes); 2706 verifyFormat("switch (a) {\n" 2707 "case 1:\n" 2708 "LIKELY case 2:\n" 2709 " return;\n" 2710 "}", 2711 Attributes); 2712 2713 FormatStyle Style = getLLVMStyle(); 2714 Style.IndentCaseLabels = true; 2715 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 2716 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2717 Style.BraceWrapping.AfterCaseLabel = true; 2718 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2719 verifyFormat("switch (n)\n" 2720 "{\n" 2721 " case 0:\n" 2722 " {\n" 2723 " return false;\n" 2724 " }\n" 2725 " default:\n" 2726 " {\n" 2727 " return true;\n" 2728 " }\n" 2729 "}", 2730 "switch (n) {\n" 2731 " case 0: {\n" 2732 " return false;\n" 2733 " }\n" 2734 " default: {\n" 2735 " return true;\n" 2736 " }\n" 2737 "}", 2738 Style); 2739 Style.BraceWrapping.AfterCaseLabel = false; 2740 verifyFormat("switch (n)\n" 2741 "{\n" 2742 " case 0: {\n" 2743 " return false;\n" 2744 " }\n" 2745 " default: {\n" 2746 " return true;\n" 2747 " }\n" 2748 "}", 2749 "switch (n) {\n" 2750 " case 0:\n" 2751 " {\n" 2752 " return false;\n" 2753 " }\n" 2754 " default:\n" 2755 " {\n" 2756 " return true;\n" 2757 " }\n" 2758 "}", 2759 Style); 2760 Style.IndentCaseLabels = false; 2761 Style.IndentCaseBlocks = true; 2762 verifyFormat("switch (n)\n" 2763 "{\n" 2764 "case 0:\n" 2765 " {\n" 2766 " return false;\n" 2767 " }\n" 2768 "case 1:\n" 2769 " break;\n" 2770 "default:\n" 2771 " {\n" 2772 " return true;\n" 2773 " }\n" 2774 "}", 2775 "switch (n) {\n" 2776 "case 0: {\n" 2777 " return false;\n" 2778 "}\n" 2779 "case 1:\n" 2780 " break;\n" 2781 "default: {\n" 2782 " return true;\n" 2783 "}\n" 2784 "}", 2785 Style); 2786 Style.IndentCaseLabels = true; 2787 Style.IndentCaseBlocks = true; 2788 verifyFormat("switch (n)\n" 2789 "{\n" 2790 " case 0:\n" 2791 " {\n" 2792 " return false;\n" 2793 " }\n" 2794 " case 1:\n" 2795 " break;\n" 2796 " default:\n" 2797 " {\n" 2798 " return true;\n" 2799 " }\n" 2800 "}", 2801 "switch (n) {\n" 2802 "case 0: {\n" 2803 " return false;\n" 2804 "}\n" 2805 "case 1:\n" 2806 " break;\n" 2807 "default: {\n" 2808 " return true;\n" 2809 "}\n" 2810 "}", 2811 Style); 2812 } 2813 2814 TEST_F(FormatTest, CaseRanges) { 2815 verifyFormat("switch (x) {\n" 2816 "case 'A' ... 'Z':\n" 2817 "case 1 ... 5:\n" 2818 "case a ... b:\n" 2819 " break;\n" 2820 "}"); 2821 } 2822 2823 TEST_F(FormatTest, ShortEnums) { 2824 FormatStyle Style = getLLVMStyle(); 2825 EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine); 2826 EXPECT_FALSE(Style.BraceWrapping.AfterEnum); 2827 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2828 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2829 Style.AllowShortEnumsOnASingleLine = false; 2830 verifyFormat("enum {\n" 2831 " A,\n" 2832 " B,\n" 2833 " C\n" 2834 "} ShortEnum1, ShortEnum2;", 2835 Style); 2836 verifyFormat("typedef enum {\n" 2837 " A,\n" 2838 " B,\n" 2839 " C\n" 2840 "} ShortEnum1, ShortEnum2;", 2841 Style); 2842 verifyFormat("enum {\n" 2843 " A,\n" 2844 "} ShortEnum1, ShortEnum2;", 2845 Style); 2846 verifyFormat("typedef enum {\n" 2847 " A,\n" 2848 "} ShortEnum1, ShortEnum2;", 2849 Style); 2850 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2851 Style.BraceWrapping.AfterEnum = true; 2852 verifyFormat("enum\n" 2853 "{\n" 2854 " A,\n" 2855 " B,\n" 2856 " C\n" 2857 "} ShortEnum1, ShortEnum2;", 2858 Style); 2859 verifyFormat("typedef enum\n" 2860 "{\n" 2861 " A,\n" 2862 " B,\n" 2863 " C\n" 2864 "} ShortEnum1, ShortEnum2;", 2865 Style); 2866 } 2867 2868 TEST_F(FormatTest, ShortCompoundRequirement) { 2869 FormatStyle Style = getLLVMStyle(); 2870 EXPECT_TRUE(Style.AllowShortCompoundRequirementOnASingleLine); 2871 verifyFormat("template <typename T>\n" 2872 "concept c = requires(T x) {\n" 2873 " { x + 1 } -> std::same_as<int>;\n" 2874 "};", 2875 Style); 2876 verifyFormat("template <typename T>\n" 2877 "concept c = requires(T x) {\n" 2878 " { x + 1 } -> std::same_as<int>;\n" 2879 " { x + 2 } -> std::same_as<int>;\n" 2880 "};", 2881 Style); 2882 Style.AllowShortCompoundRequirementOnASingleLine = false; 2883 verifyFormat("template <typename T>\n" 2884 "concept c = requires(T x) {\n" 2885 " {\n" 2886 " x + 1\n" 2887 " } -> std::same_as<int>;\n" 2888 "};", 2889 Style); 2890 verifyFormat("template <typename T>\n" 2891 "concept c = requires(T x) {\n" 2892 " {\n" 2893 " x + 1\n" 2894 " } -> std::same_as<int>;\n" 2895 " {\n" 2896 " x + 2\n" 2897 " } -> std::same_as<int>;\n" 2898 "};", 2899 Style); 2900 } 2901 2902 TEST_F(FormatTest, ShortCaseLabels) { 2903 FormatStyle Style = getLLVMStyle(); 2904 Style.AllowShortCaseLabelsOnASingleLine = true; 2905 verifyFormat("switch (a) {\n" 2906 "case 1: x = 1; break;\n" 2907 "case 2: return;\n" 2908 "case 3:\n" 2909 "case 4:\n" 2910 "case 5: return;\n" 2911 "case 6: // comment\n" 2912 " return;\n" 2913 "case 7:\n" 2914 " // comment\n" 2915 " return;\n" 2916 "case 8:\n" 2917 " x = 8; // comment\n" 2918 " break;\n" 2919 "default: y = 1; break;\n" 2920 "}", 2921 Style); 2922 verifyFormat("switch (a) {\n" 2923 "case 0: return; // comment\n" 2924 "case 1: break; // comment\n" 2925 "case 2: return;\n" 2926 "// comment\n" 2927 "case 3: return;\n" 2928 "// comment 1\n" 2929 "// comment 2\n" 2930 "// comment 3\n" 2931 "case 4: break; /* comment */\n" 2932 "case 5:\n" 2933 " // comment\n" 2934 " break;\n" 2935 "case 6: /* comment */ x = 1; break;\n" 2936 "case 7: x = /* comment */ 1; break;\n" 2937 "case 8:\n" 2938 " x = 1; /* comment */\n" 2939 " break;\n" 2940 "case 9:\n" 2941 " break; // comment line 1\n" 2942 " // comment line 2\n" 2943 "}", 2944 Style); 2945 verifyFormat("switch (a) {\n" 2946 "case 1:\n" 2947 " x = 8;\n" 2948 " // fall through\n" 2949 "case 2: x = 8;\n" 2950 "// comment\n" 2951 "case 3:\n" 2952 " return; /* comment line 1\n" 2953 " * comment line 2 */\n" 2954 "case 4: i = 8;\n" 2955 "// something else\n" 2956 "#if FOO\n" 2957 "case 5: break;\n" 2958 "#endif\n" 2959 "}", 2960 "switch (a) {\n" 2961 "case 1: x = 8;\n" 2962 " // fall through\n" 2963 "case 2:\n" 2964 " x = 8;\n" 2965 "// comment\n" 2966 "case 3:\n" 2967 " return; /* comment line 1\n" 2968 " * comment line 2 */\n" 2969 "case 4:\n" 2970 " i = 8;\n" 2971 "// something else\n" 2972 "#if FOO\n" 2973 "case 5: break;\n" 2974 "#endif\n" 2975 "}", 2976 Style); 2977 verifyFormat("switch (a) {\n" 2978 "case 0:\n" 2979 " return; // long long long long long long long long long long " 2980 "long long comment\n" 2981 " // line\n" 2982 "}", 2983 "switch (a) {\n" 2984 "case 0: return; // long long long long long long long long " 2985 "long long long long comment line\n" 2986 "}", 2987 Style); 2988 verifyFormat("switch (a) {\n" 2989 "case 0:\n" 2990 " return; /* long long long long long long long long long long " 2991 "long long comment\n" 2992 " line */\n" 2993 "}", 2994 "switch (a) {\n" 2995 "case 0: return; /* long long long long long long long long " 2996 "long long long long comment line */\n" 2997 "}", 2998 Style); 2999 verifyFormat("switch (a) {\n" 3000 "#if FOO\n" 3001 "case 0: return 0;\n" 3002 "#endif\n" 3003 "}", 3004 Style); 3005 verifyFormat("switch (a) {\n" 3006 "case 1: {\n" 3007 "}\n" 3008 "case 2: {\n" 3009 " return;\n" 3010 "}\n" 3011 "case 3: {\n" 3012 " x = 1;\n" 3013 " return;\n" 3014 "}\n" 3015 "case 4:\n" 3016 " if (x)\n" 3017 " return;\n" 3018 "}", 3019 Style); 3020 Style.ColumnLimit = 21; 3021 verifyFormat("#define X \\\n" 3022 " case 0: break;\n" 3023 "#include \"f\"", 3024 Style); 3025 verifyFormat("switch (a) {\n" 3026 "case 1: x = 1; break;\n" 3027 "case 2: return;\n" 3028 "case 3:\n" 3029 "case 4:\n" 3030 "case 5: return;\n" 3031 "default:\n" 3032 " y = 1;\n" 3033 " break;\n" 3034 "}", 3035 Style); 3036 Style.ColumnLimit = 80; 3037 Style.AllowShortCaseLabelsOnASingleLine = false; 3038 Style.IndentCaseLabels = true; 3039 verifyFormat("switch (n) {\n" 3040 " default /*comments*/:\n" 3041 " return true;\n" 3042 " case 0:\n" 3043 " return false;\n" 3044 "}", 3045 "switch (n) {\n" 3046 "default/*comments*/:\n" 3047 " return true;\n" 3048 "case 0:\n" 3049 " return false;\n" 3050 "}", 3051 Style); 3052 Style.AllowShortCaseLabelsOnASingleLine = true; 3053 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3054 Style.BraceWrapping.AfterCaseLabel = true; 3055 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 3056 verifyFormat("switch (n)\n" 3057 "{\n" 3058 " case 0:\n" 3059 " {\n" 3060 " return false;\n" 3061 " }\n" 3062 " default:\n" 3063 " {\n" 3064 " return true;\n" 3065 " }\n" 3066 "}", 3067 "switch (n) {\n" 3068 " case 0: {\n" 3069 " return false;\n" 3070 " }\n" 3071 " default:\n" 3072 " {\n" 3073 " return true;\n" 3074 " }\n" 3075 "}", 3076 Style); 3077 } 3078 3079 TEST_F(FormatTest, FormatsLabels) { 3080 verifyFormat("void f() {\n" 3081 " some_code();\n" 3082 "test_label:\n" 3083 " some_other_code();\n" 3084 " {\n" 3085 " some_more_code();\n" 3086 " another_label:\n" 3087 " some_more_code();\n" 3088 " }\n" 3089 "}"); 3090 verifyFormat("{\n" 3091 " some_code();\n" 3092 "test_label:\n" 3093 " some_other_code();\n" 3094 "}"); 3095 verifyFormat("{\n" 3096 " some_code();\n" 3097 "test_label:;\n" 3098 " int i = 0;\n" 3099 "}"); 3100 verifyFormat("{\n" 3101 " some_code();\n" 3102 "test_label: { some_other_code(); }\n" 3103 "}"); 3104 verifyFormat("{\n" 3105 " some_code();\n" 3106 "test_label: {\n" 3107 " some_other_code();\n" 3108 " some_other_code();\n" 3109 "}\n" 3110 "}"); 3111 verifyFormat("{\n" 3112 "L0:\n" 3113 "[[foo]] L1:\n" 3114 "[[bar]] [[baz]] L2:\n" 3115 " g();\n" 3116 "}"); 3117 verifyFormat("{\n" 3118 "[[foo]] L1: {\n" 3119 "[[bar]] [[baz]] L2:\n" 3120 " g();\n" 3121 "}\n" 3122 "}"); 3123 verifyFormat("{\n" 3124 "[[foo]] L1:\n" 3125 " f();\n" 3126 " {\n" 3127 " [[bar]] [[baz]] L2:\n" 3128 " g();\n" 3129 " }\n" 3130 "}"); 3131 3132 FormatStyle Style = getLLVMStyle(); 3133 Style.IndentGotoLabels = false; 3134 verifyFormat("void f() {\n" 3135 " some_code();\n" 3136 "test_label:\n" 3137 " some_other_code();\n" 3138 " {\n" 3139 " some_more_code();\n" 3140 "another_label:\n" 3141 " some_more_code();\n" 3142 " }\n" 3143 "}", 3144 Style); 3145 verifyFormat("{\n" 3146 " some_code();\n" 3147 "test_label:\n" 3148 " some_other_code();\n" 3149 "}", 3150 Style); 3151 verifyFormat("{\n" 3152 " some_code();\n" 3153 "test_label:;\n" 3154 " int i = 0;\n" 3155 "}", 3156 Style); 3157 verifyFormat("{\n" 3158 " some_code();\n" 3159 "test_label: { some_other_code(); }\n" 3160 "}", 3161 Style); 3162 verifyFormat("{\n" 3163 "[[foo]] L1:\n" 3164 " f();\n" 3165 " {\n" 3166 "[[bar]] [[baz]] L2:\n" 3167 " g();\n" 3168 " }\n" 3169 "}", 3170 Style); 3171 3172 Style.ColumnLimit = 15; 3173 verifyFormat("#define FOO \\\n" 3174 "label: \\\n" 3175 " break;", 3176 Style); 3177 3178 // The opening brace may either be on the same unwrapped line as the colon or 3179 // on a separate one. The formatter should recognize both. 3180 Style = getLLVMStyle(); 3181 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Allman; 3182 verifyFormat("{\n" 3183 " some_code();\n" 3184 "test_label:\n" 3185 "{\n" 3186 " some_other_code();\n" 3187 "}\n" 3188 "}", 3189 Style); 3190 verifyFormat("{\n" 3191 "[[foo]] L1:\n" 3192 "{\n" 3193 "[[bar]] [[baz]] L2:\n" 3194 " g();\n" 3195 "}\n" 3196 "}", 3197 Style); 3198 } 3199 3200 TEST_F(FormatTest, MultiLineControlStatements) { 3201 FormatStyle Style = getLLVMStyleWithColumns(20); 3202 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 3203 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 3204 // Short lines should keep opening brace on same line. 3205 verifyFormat("if (foo) {\n" 3206 " bar();\n" 3207 "}", 3208 "if(foo){bar();}", Style); 3209 verifyFormat("if (foo) {\n" 3210 " bar();\n" 3211 "} else {\n" 3212 " baz();\n" 3213 "}", 3214 "if(foo){bar();}else{baz();}", Style); 3215 verifyFormat("if (foo && bar) {\n" 3216 " baz();\n" 3217 "}", 3218 "if(foo&&bar){baz();}", Style); 3219 verifyFormat("if (foo) {\n" 3220 " bar();\n" 3221 "} else if (baz) {\n" 3222 " quux();\n" 3223 "}", 3224 "if(foo){bar();}else if(baz){quux();}", Style); 3225 verifyFormat("if (foo) {\n" 3226 " bar();\n" 3227 "} else if (baz) {\n" 3228 " quux();\n" 3229 "} else {\n" 3230 " foobar();\n" 3231 "}", 3232 "if(foo){bar();}else if(baz){quux();}else{foobar();}", Style); 3233 verifyFormat("for (;;) {\n" 3234 " foo();\n" 3235 "}", 3236 "for(;;){foo();}"); 3237 verifyFormat("while (1) {\n" 3238 " foo();\n" 3239 "}", 3240 "while(1){foo();}", Style); 3241 verifyFormat("switch (foo) {\n" 3242 "case bar:\n" 3243 " return;\n" 3244 "}", 3245 "switch(foo){case bar:return;}", Style); 3246 verifyFormat("try {\n" 3247 " foo();\n" 3248 "} catch (...) {\n" 3249 " bar();\n" 3250 "}", 3251 "try{foo();}catch(...){bar();}", Style); 3252 verifyFormat("do {\n" 3253 " foo();\n" 3254 "} while (bar &&\n" 3255 " baz);", 3256 "do{foo();}while(bar&&baz);", Style); 3257 // Long lines should put opening brace on new line. 3258 verifyFormat("void f() {\n" 3259 " if (a1 && a2 &&\n" 3260 " a3)\n" 3261 " {\n" 3262 " quux();\n" 3263 " }\n" 3264 "}", 3265 "void f(){if(a1&&a2&&a3){quux();}}", Style); 3266 verifyFormat("if (foo && bar &&\n" 3267 " baz)\n" 3268 "{\n" 3269 " quux();\n" 3270 "}", 3271 "if(foo&&bar&&baz){quux();}", Style); 3272 verifyFormat("if (foo && bar &&\n" 3273 " baz)\n" 3274 "{\n" 3275 " quux();\n" 3276 "}", 3277 "if (foo && bar &&\n" 3278 " baz) {\n" 3279 " quux();\n" 3280 "}", 3281 Style); 3282 verifyFormat("if (foo) {\n" 3283 " bar();\n" 3284 "} else if (baz ||\n" 3285 " quux)\n" 3286 "{\n" 3287 " foobar();\n" 3288 "}", 3289 "if(foo){bar();}else if(baz||quux){foobar();}", Style); 3290 verifyFormat("if (foo) {\n" 3291 " bar();\n" 3292 "} else if (baz ||\n" 3293 " quux)\n" 3294 "{\n" 3295 " foobar();\n" 3296 "} else {\n" 3297 " barbaz();\n" 3298 "}", 3299 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 3300 Style); 3301 verifyFormat("for (int i = 0;\n" 3302 " i < 10; ++i)\n" 3303 "{\n" 3304 " foo();\n" 3305 "}", 3306 "for(int i=0;i<10;++i){foo();}", Style); 3307 verifyFormat("foreach (int i,\n" 3308 " list)\n" 3309 "{\n" 3310 " foo();\n" 3311 "}", 3312 "foreach(int i, list){foo();}", Style); 3313 Style.ColumnLimit = 3314 40; // to concentrate at brace wrapping, not line wrap due to column limit 3315 verifyFormat("foreach (int i, list) {\n" 3316 " foo();\n" 3317 "}", 3318 "foreach(int i, list){foo();}", Style); 3319 Style.ColumnLimit = 3320 20; // to concentrate at brace wrapping, not line wrap due to column limit 3321 verifyFormat("while (foo || bar ||\n" 3322 " baz)\n" 3323 "{\n" 3324 " quux();\n" 3325 "}", 3326 "while(foo||bar||baz){quux();}", Style); 3327 verifyFormat("switch (\n" 3328 " foo = barbaz)\n" 3329 "{\n" 3330 "case quux:\n" 3331 " return;\n" 3332 "}", 3333 "switch(foo=barbaz){case quux:return;}", Style); 3334 verifyFormat("try {\n" 3335 " foo();\n" 3336 "} catch (\n" 3337 " Exception &bar)\n" 3338 "{\n" 3339 " baz();\n" 3340 "}", 3341 "try{foo();}catch(Exception&bar){baz();}", Style); 3342 Style.ColumnLimit = 3343 40; // to concentrate at brace wrapping, not line wrap due to column limit 3344 verifyFormat("try {\n" 3345 " foo();\n" 3346 "} catch (Exception &bar) {\n" 3347 " baz();\n" 3348 "}", 3349 "try{foo();}catch(Exception&bar){baz();}", Style); 3350 Style.ColumnLimit = 3351 20; // to concentrate at brace wrapping, not line wrap due to column limit 3352 3353 Style.BraceWrapping.BeforeElse = true; 3354 verifyFormat("if (foo) {\n" 3355 " bar();\n" 3356 "}\n" 3357 "else if (baz ||\n" 3358 " quux)\n" 3359 "{\n" 3360 " foobar();\n" 3361 "}\n" 3362 "else {\n" 3363 " barbaz();\n" 3364 "}", 3365 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 3366 Style); 3367 3368 Style.BraceWrapping.BeforeCatch = true; 3369 verifyFormat("try {\n" 3370 " foo();\n" 3371 "}\n" 3372 "catch (...) {\n" 3373 " baz();\n" 3374 "}", 3375 "try{foo();}catch(...){baz();}", Style); 3376 3377 Style.BraceWrapping.AfterFunction = true; 3378 Style.BraceWrapping.AfterStruct = false; 3379 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 3380 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3381 Style.ColumnLimit = 80; 3382 verifyFormat("void shortfunction() { bar(); }", Style); 3383 verifyFormat("struct T shortfunction() { return bar(); }", Style); 3384 verifyFormat("struct T {};", Style); 3385 3386 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3387 verifyFormat("void shortfunction()\n" 3388 "{\n" 3389 " bar();\n" 3390 "}", 3391 Style); 3392 verifyFormat("struct T shortfunction()\n" 3393 "{\n" 3394 " return bar();\n" 3395 "}", 3396 Style); 3397 verifyFormat("struct T {};", Style); 3398 3399 Style.BraceWrapping.AfterFunction = false; 3400 Style.BraceWrapping.AfterStruct = true; 3401 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3402 verifyFormat("void shortfunction() { bar(); }", Style); 3403 verifyFormat("struct T shortfunction() { return bar(); }", Style); 3404 verifyFormat("struct T\n" 3405 "{\n" 3406 "};", 3407 Style); 3408 3409 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3410 verifyFormat("void shortfunction() {\n" 3411 " bar();\n" 3412 "}", 3413 Style); 3414 verifyFormat("struct T shortfunction() {\n" 3415 " return bar();\n" 3416 "}", 3417 Style); 3418 verifyFormat("struct T\n" 3419 "{\n" 3420 "};", 3421 Style); 3422 } 3423 3424 TEST_F(FormatTest, BeforeWhile) { 3425 FormatStyle Style = getLLVMStyle(); 3426 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 3427 3428 verifyFormat("do {\n" 3429 " foo();\n" 3430 "} while (1);", 3431 Style); 3432 Style.BraceWrapping.BeforeWhile = true; 3433 verifyFormat("do {\n" 3434 " foo();\n" 3435 "}\n" 3436 "while (1);", 3437 Style); 3438 } 3439 3440 //===----------------------------------------------------------------------===// 3441 // Tests for classes, namespaces, etc. 3442 //===----------------------------------------------------------------------===// 3443 3444 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 3445 verifyFormat("class A {};"); 3446 } 3447 3448 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 3449 verifyFormat("class A {\n" 3450 "public:\n" 3451 "public: // comment\n" 3452 "protected:\n" 3453 "private:\n" 3454 " void f() {}\n" 3455 "};"); 3456 verifyFormat("export class A {\n" 3457 "public:\n" 3458 "public: // comment\n" 3459 "protected:\n" 3460 "private:\n" 3461 " void f() {}\n" 3462 "};"); 3463 verifyGoogleFormat("class A {\n" 3464 " public:\n" 3465 " protected:\n" 3466 " private:\n" 3467 " void f() {}\n" 3468 "};"); 3469 verifyGoogleFormat("export class A {\n" 3470 " public:\n" 3471 " protected:\n" 3472 " private:\n" 3473 " void f() {}\n" 3474 "};"); 3475 verifyFormat("class A {\n" 3476 "public slots:\n" 3477 " void f1() {}\n" 3478 "public Q_SLOTS:\n" 3479 " void f2() {}\n" 3480 "protected slots:\n" 3481 " void f3() {}\n" 3482 "protected Q_SLOTS:\n" 3483 " void f4() {}\n" 3484 "private slots:\n" 3485 " void f5() {}\n" 3486 "private Q_SLOTS:\n" 3487 " void f6() {}\n" 3488 "signals:\n" 3489 " void g1();\n" 3490 "Q_SIGNALS:\n" 3491 " void g2();\n" 3492 "};"); 3493 3494 // Don't interpret 'signals' the wrong way. 3495 verifyFormat("signals.set();"); 3496 verifyFormat("for (Signals signals : f()) {\n}"); 3497 verifyFormat("{\n" 3498 " signals.set(); // This needs indentation.\n" 3499 "}"); 3500 verifyFormat("void f() {\n" 3501 "label:\n" 3502 " signals.baz();\n" 3503 "}"); 3504 verifyFormat("private[1];"); 3505 verifyFormat("testArray[public] = 1;"); 3506 verifyFormat("public();"); 3507 verifyFormat("myFunc(public);"); 3508 verifyFormat("std::vector<int> testVec = {private};"); 3509 verifyFormat("private.p = 1;"); 3510 verifyFormat("void function(private...) {};"); 3511 verifyFormat("if (private && public)"); 3512 verifyFormat("private &= true;"); 3513 verifyFormat("int x = private * public;"); 3514 verifyFormat("public *= private;"); 3515 verifyFormat("int x = public + private;"); 3516 verifyFormat("private++;"); 3517 verifyFormat("++private;"); 3518 verifyFormat("public += private;"); 3519 verifyFormat("public = public - private;"); 3520 verifyFormat("public->foo();"); 3521 verifyFormat("private--;"); 3522 verifyFormat("--private;"); 3523 verifyFormat("public -= 1;"); 3524 verifyFormat("if (!private && !public)"); 3525 verifyFormat("public != private;"); 3526 verifyFormat("int x = public / private;"); 3527 verifyFormat("public /= 2;"); 3528 verifyFormat("public = public % 2;"); 3529 verifyFormat("public %= 2;"); 3530 verifyFormat("if (public < private)"); 3531 verifyFormat("public << private;"); 3532 verifyFormat("public <<= private;"); 3533 verifyFormat("if (public > private)"); 3534 verifyFormat("public >> private;"); 3535 verifyFormat("public >>= private;"); 3536 verifyFormat("public ^ private;"); 3537 verifyFormat("public ^= private;"); 3538 verifyFormat("public | private;"); 3539 verifyFormat("public |= private;"); 3540 verifyFormat("auto x = private ? 1 : 2;"); 3541 verifyFormat("if (public == private)"); 3542 verifyFormat("void foo(public, private)"); 3543 verifyFormat("public::foo();"); 3544 3545 verifyFormat("class A {\n" 3546 "public:\n" 3547 " std::unique_ptr<int *[]> b() { return nullptr; }\n" 3548 "\n" 3549 "private:\n" 3550 " int c;\n" 3551 "};\n" 3552 "class B {\n" 3553 "public:\n" 3554 " std::unique_ptr<int *[] /* okay */> b() { return nullptr; }\n" 3555 "\n" 3556 "private:\n" 3557 " int c;\n" 3558 "};"); 3559 } 3560 3561 TEST_F(FormatTest, SeparatesLogicalBlocks) { 3562 verifyFormat("class A {\n" 3563 "public:\n" 3564 " void f();\n" 3565 "\n" 3566 "private:\n" 3567 " void g() {}\n" 3568 " // test\n" 3569 "protected:\n" 3570 " int h;\n" 3571 "};", 3572 "class A {\n" 3573 "public:\n" 3574 "void f();\n" 3575 "private:\n" 3576 "void g() {}\n" 3577 "// test\n" 3578 "protected:\n" 3579 "int h;\n" 3580 "};"); 3581 verifyFormat("class A {\n" 3582 "protected:\n" 3583 "public:\n" 3584 " void f();\n" 3585 "};", 3586 "class A {\n" 3587 "protected:\n" 3588 "\n" 3589 "public:\n" 3590 "\n" 3591 " void f();\n" 3592 "};"); 3593 3594 // Even ensure proper spacing inside macros. 3595 verifyFormat("#define B \\\n" 3596 " class A { \\\n" 3597 " protected: \\\n" 3598 " public: \\\n" 3599 " void f(); \\\n" 3600 " };", 3601 "#define B \\\n" 3602 " class A { \\\n" 3603 " protected: \\\n" 3604 " \\\n" 3605 " public: \\\n" 3606 " \\\n" 3607 " void f(); \\\n" 3608 " };", 3609 getGoogleStyle()); 3610 // But don't remove empty lines after macros ending in access specifiers. 3611 verifyFormat("#define A private:\n" 3612 "\n" 3613 "int i;", 3614 "#define A private:\n" 3615 "\n" 3616 "int i;"); 3617 } 3618 3619 TEST_F(FormatTest, FormatsClasses) { 3620 verifyFormat("class A : public B {};"); 3621 verifyFormat("class A : public ::B {};"); 3622 3623 verifyFormat( 3624 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3625 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3626 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3627 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3628 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3629 verifyFormat( 3630 "class A : public B, public C, public D, public E, public F {};"); 3631 verifyFormat("class AAAAAAAAAAAA : public B,\n" 3632 " public C,\n" 3633 " public D,\n" 3634 " public E,\n" 3635 " public F,\n" 3636 " public G {};"); 3637 3638 verifyFormat("class\n" 3639 " ReallyReallyLongClassName {\n" 3640 " int i;\n" 3641 "};", 3642 getLLVMStyleWithColumns(32)); 3643 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3644 " aaaaaaaaaaaaaaaa> {};"); 3645 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 3646 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 3647 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 3648 verifyFormat("template <class R, class C>\n" 3649 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 3650 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 3651 verifyFormat("class ::A::B {};"); 3652 } 3653 3654 TEST_F(FormatTest, BreakInheritanceStyle) { 3655 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 3656 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 3657 FormatStyle::BILS_BeforeComma; 3658 verifyFormat("class MyClass : public X {};", 3659 StyleWithInheritanceBreakBeforeComma); 3660 verifyFormat("class MyClass\n" 3661 " : public X\n" 3662 " , public Y {};", 3663 StyleWithInheritanceBreakBeforeComma); 3664 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 3665 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 3666 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3667 StyleWithInheritanceBreakBeforeComma); 3668 verifyFormat("struct aaaaaaaaaaaaa\n" 3669 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 3670 " aaaaaaaaaaaaaaaa> {};", 3671 StyleWithInheritanceBreakBeforeComma); 3672 3673 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 3674 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 3675 FormatStyle::BILS_AfterColon; 3676 verifyFormat("class MyClass : public X {};", 3677 StyleWithInheritanceBreakAfterColon); 3678 verifyFormat("class MyClass : public X, public Y {};", 3679 StyleWithInheritanceBreakAfterColon); 3680 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 3681 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3682 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3683 StyleWithInheritanceBreakAfterColon); 3684 verifyFormat("struct aaaaaaaaaaaaa :\n" 3685 " public aaaaaaaaaaaaaaaaaaa< // break\n" 3686 " aaaaaaaaaaaaaaaa> {};", 3687 StyleWithInheritanceBreakAfterColon); 3688 3689 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 3690 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 3691 FormatStyle::BILS_AfterComma; 3692 verifyFormat("class MyClass : public X {};", 3693 StyleWithInheritanceBreakAfterComma); 3694 verifyFormat("class MyClass : public X,\n" 3695 " public Y {};", 3696 StyleWithInheritanceBreakAfterComma); 3697 verifyFormat( 3698 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3699 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 3700 "{};", 3701 StyleWithInheritanceBreakAfterComma); 3702 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3703 " aaaaaaaaaaaaaaaa> {};", 3704 StyleWithInheritanceBreakAfterComma); 3705 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3706 " : public OnceBreak,\n" 3707 " public AlwaysBreak,\n" 3708 " EvenBasesFitInOneLine {};", 3709 StyleWithInheritanceBreakAfterComma); 3710 } 3711 3712 TEST_F(FormatTest, FormatsVariableDeclarationsAfterRecord) { 3713 verifyFormat("class A {\n} a, b;"); 3714 verifyFormat("struct A {\n} a, b;"); 3715 verifyFormat("union A {\n} a, b;"); 3716 3717 verifyFormat("constexpr class A {\n} a, b;"); 3718 verifyFormat("constexpr struct A {\n} a, b;"); 3719 verifyFormat("constexpr union A {\n} a, b;"); 3720 3721 verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace"); 3722 verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace"); 3723 verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace"); 3724 3725 verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace"); 3726 verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace"); 3727 verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace"); 3728 3729 verifyFormat("namespace ns {\n" 3730 "class {\n" 3731 "} a, b;\n" 3732 "} // namespace ns"); 3733 verifyFormat("namespace ns {\n" 3734 "const class {\n" 3735 "} a, b;\n" 3736 "} // namespace ns"); 3737 verifyFormat("namespace ns {\n" 3738 "constexpr class C {\n" 3739 "} a, b;\n" 3740 "} // namespace ns"); 3741 verifyFormat("namespace ns {\n" 3742 "class { /* comment */\n" 3743 "} a, b;\n" 3744 "} // namespace ns"); 3745 verifyFormat("namespace ns {\n" 3746 "const class { /* comment */\n" 3747 "} a, b;\n" 3748 "} // namespace ns"); 3749 } 3750 3751 TEST_F(FormatTest, FormatsEnum) { 3752 verifyFormat("enum {\n" 3753 " Zero,\n" 3754 " One = 1,\n" 3755 " Two = One + 1,\n" 3756 " Three = (One + Two),\n" 3757 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3758 " Five = (One, Two, Three, Four, 5)\n" 3759 "};"); 3760 verifyGoogleFormat("enum {\n" 3761 " Zero,\n" 3762 " One = 1,\n" 3763 " Two = One + 1,\n" 3764 " Three = (One + Two),\n" 3765 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3766 " Five = (One, Two, Three, Four, 5)\n" 3767 "};"); 3768 verifyFormat("enum Enum {};"); 3769 verifyFormat("enum {};"); 3770 verifyFormat("enum X E {} d;"); 3771 verifyFormat("enum __attribute__((...)) E {} d;"); 3772 verifyFormat("enum __declspec__((...)) E {} d;"); 3773 verifyFormat("enum [[nodiscard]] E {} d;"); 3774 verifyFormat("enum {\n" 3775 " Bar = Foo<int, int>::value\n" 3776 "};", 3777 getLLVMStyleWithColumns(30)); 3778 3779 verifyFormat("enum ShortEnum { A, B, C };"); 3780 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 3781 3782 verifyFormat("enum KeepEmptyLines {\n" 3783 " ONE,\n" 3784 "\n" 3785 " TWO,\n" 3786 "\n" 3787 " THREE\n" 3788 "}", 3789 "enum KeepEmptyLines {\n" 3790 " ONE,\n" 3791 "\n" 3792 " TWO,\n" 3793 "\n" 3794 "\n" 3795 " THREE\n" 3796 "}"); 3797 verifyFormat("enum E { // comment\n" 3798 " ONE,\n" 3799 " TWO\n" 3800 "};\n" 3801 "int i;"); 3802 3803 FormatStyle EightIndent = getLLVMStyle(); 3804 EightIndent.IndentWidth = 8; 3805 verifyFormat("enum {\n" 3806 " VOID,\n" 3807 " CHAR,\n" 3808 " SHORT,\n" 3809 " INT,\n" 3810 " LONG,\n" 3811 " SIGNED,\n" 3812 " UNSIGNED,\n" 3813 " BOOL,\n" 3814 " FLOAT,\n" 3815 " DOUBLE,\n" 3816 " COMPLEX\n" 3817 "};", 3818 EightIndent); 3819 3820 verifyFormat("enum [[nodiscard]] E {\n" 3821 " ONE,\n" 3822 " TWO,\n" 3823 "};"); 3824 verifyFormat("enum [[nodiscard]] E {\n" 3825 " // Comment 1\n" 3826 " ONE,\n" 3827 " // Comment 2\n" 3828 " TWO,\n" 3829 "};"); 3830 verifyFormat("enum [[clang::enum_extensibility(open)]] E {\n" 3831 " // Comment 1\n" 3832 " ONE,\n" 3833 " // Comment 2\n" 3834 " TWO\n" 3835 "};"); 3836 verifyFormat("enum [[nodiscard]] [[clang::enum_extensibility(open)]] E {\n" 3837 " // Comment 1\n" 3838 " ONE,\n" 3839 " // Comment 2\n" 3840 " TWO\n" 3841 "};"); 3842 verifyFormat("enum [[clang::enum_extensibility(open)]] E { // foo\n" 3843 " A,\n" 3844 " // bar\n" 3845 " B\n" 3846 "};", 3847 "enum [[clang::enum_extensibility(open)]] E{// foo\n" 3848 " A,\n" 3849 " // bar\n" 3850 " B};"); 3851 3852 // Not enums. 3853 verifyFormat("enum X f() {\n" 3854 " a();\n" 3855 " return 42;\n" 3856 "}"); 3857 verifyFormat("enum X Type::f() {\n" 3858 " a();\n" 3859 " return 42;\n" 3860 "}"); 3861 verifyFormat("enum ::X f() {\n" 3862 " a();\n" 3863 " return 42;\n" 3864 "}"); 3865 verifyFormat("enum ns::X f() {\n" 3866 " a();\n" 3867 " return 42;\n" 3868 "}"); 3869 } 3870 3871 TEST_F(FormatTest, FormatsEnumsWithErrors) { 3872 verifyFormat("enum Type {\n" 3873 " One = 0; // These semicolons should be commas.\n" 3874 " Two = 1;\n" 3875 "};"); 3876 verifyFormat("namespace n {\n" 3877 "enum Type {\n" 3878 " One,\n" 3879 " Two, // missing };\n" 3880 " int i;\n" 3881 "}\n" 3882 "void g() {}"); 3883 } 3884 3885 TEST_F(FormatTest, FormatsEnumStruct) { 3886 verifyFormat("enum struct {\n" 3887 " Zero,\n" 3888 " One = 1,\n" 3889 " Two = One + 1,\n" 3890 " Three = (One + Two),\n" 3891 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3892 " Five = (One, Two, Three, Four, 5)\n" 3893 "};"); 3894 verifyFormat("enum struct Enum {};"); 3895 verifyFormat("enum struct {};"); 3896 verifyFormat("enum struct X E {} d;"); 3897 verifyFormat("enum struct __attribute__((...)) E {} d;"); 3898 verifyFormat("enum struct __declspec__((...)) E {} d;"); 3899 verifyFormat("enum struct [[nodiscard]] E {} d;"); 3900 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 3901 3902 verifyFormat("enum struct [[nodiscard]] E {\n" 3903 " ONE,\n" 3904 " TWO,\n" 3905 "};"); 3906 verifyFormat("enum struct [[nodiscard]] E {\n" 3907 " // Comment 1\n" 3908 " ONE,\n" 3909 " // Comment 2\n" 3910 " TWO,\n" 3911 "};"); 3912 } 3913 3914 TEST_F(FormatTest, FormatsEnumClass) { 3915 verifyFormat("enum class {\n" 3916 " Zero,\n" 3917 " One = 1,\n" 3918 " Two = One + 1,\n" 3919 " Three = (One + Two),\n" 3920 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3921 " Five = (One, Two, Three, Four, 5)\n" 3922 "};"); 3923 verifyFormat("enum class Enum {};"); 3924 verifyFormat("enum class {};"); 3925 verifyFormat("enum class X E {} d;"); 3926 verifyFormat("enum class __attribute__((...)) E {} d;"); 3927 verifyFormat("enum class __declspec__((...)) E {} d;"); 3928 verifyFormat("enum class [[nodiscard]] E {} d;"); 3929 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 3930 3931 verifyFormat("enum class [[nodiscard]] E {\n" 3932 " ONE,\n" 3933 " TWO,\n" 3934 "};"); 3935 verifyFormat("enum class [[nodiscard]] E {\n" 3936 " // Comment 1\n" 3937 " ONE,\n" 3938 " // Comment 2\n" 3939 " TWO,\n" 3940 "};"); 3941 } 3942 3943 TEST_F(FormatTest, FormatsEnumTypes) { 3944 verifyFormat("enum X : int {\n" 3945 " A, // Force multiple lines.\n" 3946 " B\n" 3947 "};"); 3948 verifyFormat("enum X : int { A, B };"); 3949 verifyFormat("enum X : std::uint32_t { A, B };"); 3950 } 3951 3952 TEST_F(FormatTest, FormatsTypedefEnum) { 3953 FormatStyle Style = getLLVMStyleWithColumns(40); 3954 verifyFormat("typedef enum {} EmptyEnum;"); 3955 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3956 verifyFormat("typedef enum {\n" 3957 " ZERO = 0,\n" 3958 " ONE = 1,\n" 3959 " TWO = 2,\n" 3960 " THREE = 3\n" 3961 "} LongEnum;", 3962 Style); 3963 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3964 Style.BraceWrapping.AfterEnum = true; 3965 verifyFormat("typedef enum {} EmptyEnum;"); 3966 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3967 verifyFormat("typedef enum\n" 3968 "{\n" 3969 " ZERO = 0,\n" 3970 " ONE = 1,\n" 3971 " TWO = 2,\n" 3972 " THREE = 3\n" 3973 "} LongEnum;", 3974 Style); 3975 } 3976 3977 TEST_F(FormatTest, FormatsNSEnums) { 3978 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3979 verifyGoogleFormat( 3980 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3981 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 3982 " // Information about someDecentlyLongValue.\n" 3983 " someDecentlyLongValue,\n" 3984 " // Information about anotherDecentlyLongValue.\n" 3985 " anotherDecentlyLongValue,\n" 3986 " // Information about aThirdDecentlyLongValue.\n" 3987 " aThirdDecentlyLongValue\n" 3988 "};"); 3989 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 3990 " // Information about someDecentlyLongValue.\n" 3991 " someDecentlyLongValue,\n" 3992 " // Information about anotherDecentlyLongValue.\n" 3993 " anotherDecentlyLongValue,\n" 3994 " // Information about aThirdDecentlyLongValue.\n" 3995 " aThirdDecentlyLongValue\n" 3996 "};"); 3997 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 3998 " a = 1,\n" 3999 " b = 2,\n" 4000 " c = 3,\n" 4001 "};"); 4002 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 4003 " a = 1,\n" 4004 " b = 2,\n" 4005 " c = 3,\n" 4006 "};"); 4007 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 4008 " a = 1,\n" 4009 " b = 2,\n" 4010 " c = 3,\n" 4011 "};"); 4012 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 4013 " a = 1,\n" 4014 " b = 2,\n" 4015 " c = 3,\n" 4016 "};"); 4017 } 4018 4019 TEST_F(FormatTest, FormatsBitfields) { 4020 verifyFormat("struct Bitfields {\n" 4021 " unsigned sClass : 8;\n" 4022 " unsigned ValueKind : 2;\n" 4023 "};"); 4024 verifyFormat("struct A {\n" 4025 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 4026 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 4027 "};"); 4028 verifyFormat("struct MyStruct {\n" 4029 " uchar data;\n" 4030 " uchar : 8;\n" 4031 " uchar : 8;\n" 4032 " uchar other;\n" 4033 "};"); 4034 FormatStyle Style = getLLVMStyle(); 4035 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 4036 verifyFormat("struct Bitfields {\n" 4037 " unsigned sClass:8;\n" 4038 " unsigned ValueKind:2;\n" 4039 " uchar other;\n" 4040 "};", 4041 Style); 4042 verifyFormat("struct A {\n" 4043 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 4044 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 4045 "};", 4046 Style); 4047 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 4048 verifyFormat("struct Bitfields {\n" 4049 " unsigned sClass :8;\n" 4050 " unsigned ValueKind :2;\n" 4051 " uchar other;\n" 4052 "};", 4053 Style); 4054 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 4055 verifyFormat("struct Bitfields {\n" 4056 " unsigned sClass: 8;\n" 4057 " unsigned ValueKind: 2;\n" 4058 " uchar other;\n" 4059 "};", 4060 Style); 4061 } 4062 4063 TEST_F(FormatTest, FormatsNamespaces) { 4064 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 4065 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 4066 4067 verifyFormat("namespace some_namespace {\n" 4068 "class A {};\n" 4069 "void f() { f(); }\n" 4070 "}", 4071 LLVMWithNoNamespaceFix); 4072 verifyFormat("#define M(x) x##x\n" 4073 "namespace M(x) {\n" 4074 "class A {};\n" 4075 "void f() { f(); }\n" 4076 "}", 4077 LLVMWithNoNamespaceFix); 4078 verifyFormat("#define M(x) x##x\n" 4079 "namespace N::inline M(x) {\n" 4080 "class A {};\n" 4081 "void f() { f(); }\n" 4082 "}", 4083 LLVMWithNoNamespaceFix); 4084 verifyFormat("#define M(x) x##x\n" 4085 "namespace M(x)::inline N {\n" 4086 "class A {};\n" 4087 "void f() { f(); }\n" 4088 "}", 4089 LLVMWithNoNamespaceFix); 4090 verifyFormat("#define M(x) x##x\n" 4091 "namespace N::M(x) {\n" 4092 "class A {};\n" 4093 "void f() { f(); }\n" 4094 "}", 4095 LLVMWithNoNamespaceFix); 4096 verifyFormat("#define M(x) x##x\n" 4097 "namespace M::N(x) {\n" 4098 "class A {};\n" 4099 "void f() { f(); }\n" 4100 "}", 4101 LLVMWithNoNamespaceFix); 4102 verifyFormat("namespace N::inline D {\n" 4103 "class A {};\n" 4104 "void f() { f(); }\n" 4105 "}", 4106 LLVMWithNoNamespaceFix); 4107 verifyFormat("namespace N::inline D::E {\n" 4108 "class A {};\n" 4109 "void f() { f(); }\n" 4110 "}", 4111 LLVMWithNoNamespaceFix); 4112 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 4113 "class A {};\n" 4114 "void f() { f(); }\n" 4115 "}", 4116 LLVMWithNoNamespaceFix); 4117 verifyFormat("/* something */ namespace some_namespace {\n" 4118 "class A {};\n" 4119 "void f() { f(); }\n" 4120 "}", 4121 LLVMWithNoNamespaceFix); 4122 verifyFormat("namespace {\n" 4123 "class A {};\n" 4124 "void f() { f(); }\n" 4125 "}", 4126 LLVMWithNoNamespaceFix); 4127 verifyFormat("/* something */ namespace {\n" 4128 "class A {};\n" 4129 "void f() { f(); }\n" 4130 "}", 4131 LLVMWithNoNamespaceFix); 4132 verifyFormat("inline namespace X {\n" 4133 "class A {};\n" 4134 "void f() { f(); }\n" 4135 "}", 4136 LLVMWithNoNamespaceFix); 4137 verifyFormat("/* something */ inline namespace X {\n" 4138 "class A {};\n" 4139 "void f() { f(); }\n" 4140 "}", 4141 LLVMWithNoNamespaceFix); 4142 verifyFormat("export namespace X {\n" 4143 "class A {};\n" 4144 "void f() { f(); }\n" 4145 "}", 4146 LLVMWithNoNamespaceFix); 4147 verifyFormat("using namespace some_namespace;\n" 4148 "class A {};\n" 4149 "void f() { f(); }", 4150 LLVMWithNoNamespaceFix); 4151 4152 // This code is more common than we thought; if we 4153 // layout this correctly the semicolon will go into 4154 // its own line, which is undesirable. 4155 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 4156 verifyFormat("namespace {\n" 4157 "class A {};\n" 4158 "};", 4159 LLVMWithNoNamespaceFix); 4160 4161 verifyFormat("namespace {\n" 4162 "int SomeVariable = 0; // comment\n" 4163 "} // namespace", 4164 LLVMWithNoNamespaceFix); 4165 verifyFormat("#ifndef HEADER_GUARD\n" 4166 "#define HEADER_GUARD\n" 4167 "namespace my_namespace {\n" 4168 "int i;\n" 4169 "} // my_namespace\n" 4170 "#endif // HEADER_GUARD", 4171 "#ifndef HEADER_GUARD\n" 4172 " #define HEADER_GUARD\n" 4173 " namespace my_namespace {\n" 4174 "int i;\n" 4175 "} // my_namespace\n" 4176 "#endif // HEADER_GUARD", 4177 LLVMWithNoNamespaceFix); 4178 4179 verifyFormat("namespace A::B {\n" 4180 "class C {};\n" 4181 "}", 4182 LLVMWithNoNamespaceFix); 4183 4184 FormatStyle Style = getLLVMStyle(); 4185 Style.NamespaceIndentation = FormatStyle::NI_All; 4186 verifyFormat("namespace out {\n" 4187 " int i;\n" 4188 " namespace in {\n" 4189 " int i;\n" 4190 " } // namespace in\n" 4191 "} // namespace out", 4192 "namespace out {\n" 4193 "int i;\n" 4194 "namespace in {\n" 4195 "int i;\n" 4196 "} // namespace in\n" 4197 "} // namespace out", 4198 Style); 4199 4200 FormatStyle ShortInlineFunctions = getLLVMStyle(); 4201 ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All; 4202 ShortInlineFunctions.AllowShortFunctionsOnASingleLine = 4203 FormatStyle::SFS_Inline; 4204 verifyFormat("namespace {\n" 4205 " void f() {\n" 4206 " return;\n" 4207 " }\n" 4208 "} // namespace", 4209 ShortInlineFunctions); 4210 verifyFormat("namespace { /* comment */\n" 4211 " void f() {\n" 4212 " return;\n" 4213 " }\n" 4214 "} // namespace", 4215 ShortInlineFunctions); 4216 verifyFormat("namespace { // comment\n" 4217 " void f() {\n" 4218 " return;\n" 4219 " }\n" 4220 "} // namespace", 4221 ShortInlineFunctions); 4222 verifyFormat("namespace {\n" 4223 " int some_int;\n" 4224 " void f() {\n" 4225 " return;\n" 4226 " }\n" 4227 "} // namespace", 4228 ShortInlineFunctions); 4229 verifyFormat("namespace interface {\n" 4230 " void f() {\n" 4231 " return;\n" 4232 " }\n" 4233 "} // namespace interface", 4234 ShortInlineFunctions); 4235 verifyFormat("namespace {\n" 4236 " class X {\n" 4237 " void f() { return; }\n" 4238 " };\n" 4239 "} // namespace", 4240 ShortInlineFunctions); 4241 verifyFormat("namespace {\n" 4242 " class X { /* comment */\n" 4243 " void f() { return; }\n" 4244 " };\n" 4245 "} // namespace", 4246 ShortInlineFunctions); 4247 verifyFormat("namespace {\n" 4248 " class X { // comment\n" 4249 " void f() { return; }\n" 4250 " };\n" 4251 "} // namespace", 4252 ShortInlineFunctions); 4253 verifyFormat("namespace {\n" 4254 " struct X {\n" 4255 " void f() { return; }\n" 4256 " };\n" 4257 "} // namespace", 4258 ShortInlineFunctions); 4259 verifyFormat("namespace {\n" 4260 " union X {\n" 4261 " void f() { return; }\n" 4262 " };\n" 4263 "} // namespace", 4264 ShortInlineFunctions); 4265 verifyFormat("extern \"C\" {\n" 4266 "void f() {\n" 4267 " return;\n" 4268 "}\n" 4269 "} // namespace", 4270 ShortInlineFunctions); 4271 verifyFormat("namespace {\n" 4272 " class X {\n" 4273 " void f() { return; }\n" 4274 " } x;\n" 4275 "} // namespace", 4276 ShortInlineFunctions); 4277 verifyFormat("namespace {\n" 4278 " [[nodiscard]] class X {\n" 4279 " void f() { return; }\n" 4280 " };\n" 4281 "} // namespace", 4282 ShortInlineFunctions); 4283 verifyFormat("namespace {\n" 4284 " static class X {\n" 4285 " void f() { return; }\n" 4286 " } x;\n" 4287 "} // namespace", 4288 ShortInlineFunctions); 4289 verifyFormat("namespace {\n" 4290 " constexpr class X {\n" 4291 " void f() { return; }\n" 4292 " } x;\n" 4293 "} // namespace", 4294 ShortInlineFunctions); 4295 4296 ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent; 4297 verifyFormat("extern \"C\" {\n" 4298 " void f() {\n" 4299 " return;\n" 4300 " }\n" 4301 "} // namespace", 4302 ShortInlineFunctions); 4303 4304 Style.NamespaceIndentation = FormatStyle::NI_Inner; 4305 verifyFormat("namespace out {\n" 4306 "int i;\n" 4307 "namespace in {\n" 4308 " int i;\n" 4309 "} // namespace in\n" 4310 "} // namespace out", 4311 "namespace out {\n" 4312 "int i;\n" 4313 "namespace in {\n" 4314 "int i;\n" 4315 "} // namespace in\n" 4316 "} // namespace out", 4317 Style); 4318 4319 Style.NamespaceIndentation = FormatStyle::NI_None; 4320 verifyFormat("template <class T>\n" 4321 "concept a_concept = X<>;\n" 4322 "namespace B {\n" 4323 "struct b_struct {};\n" 4324 "} // namespace B", 4325 Style); 4326 verifyFormat("template <int I>\n" 4327 "constexpr void foo()\n" 4328 " requires(I == 42)\n" 4329 "{}\n" 4330 "namespace ns {\n" 4331 "void foo() {}\n" 4332 "} // namespace ns", 4333 Style); 4334 4335 FormatStyle LLVMWithCompactInnerNamespace = getLLVMStyle(); 4336 LLVMWithCompactInnerNamespace.CompactNamespaces = true; 4337 LLVMWithCompactInnerNamespace.NamespaceIndentation = FormatStyle::NI_Inner; 4338 verifyFormat("namespace ns1 { namespace ns2 { namespace ns3 {\n" 4339 "// block for debug mode\n" 4340 "#ifndef NDEBUG\n" 4341 "#endif\n" 4342 "}}} // namespace ns1::ns2::ns3", 4343 LLVMWithCompactInnerNamespace); 4344 } 4345 4346 TEST_F(FormatTest, NamespaceMacros) { 4347 FormatStyle Style = getLLVMStyle(); 4348 Style.NamespaceMacros.push_back("TESTSUITE"); 4349 4350 verifyFormat("TESTSUITE(A) {\n" 4351 "int foo();\n" 4352 "} // TESTSUITE(A)", 4353 Style); 4354 4355 verifyFormat("TESTSUITE(A, B) {\n" 4356 "int foo();\n" 4357 "} // TESTSUITE(A)", 4358 Style); 4359 4360 // Properly indent according to NamespaceIndentation style 4361 Style.NamespaceIndentation = FormatStyle::NI_All; 4362 verifyFormat("TESTSUITE(A) {\n" 4363 " int foo();\n" 4364 "} // TESTSUITE(A)", 4365 Style); 4366 verifyFormat("TESTSUITE(A) {\n" 4367 " namespace B {\n" 4368 " int foo();\n" 4369 " } // namespace B\n" 4370 "} // TESTSUITE(A)", 4371 Style); 4372 verifyFormat("namespace A {\n" 4373 " TESTSUITE(B) {\n" 4374 " int foo();\n" 4375 " } // TESTSUITE(B)\n" 4376 "} // namespace A", 4377 Style); 4378 4379 Style.NamespaceIndentation = FormatStyle::NI_Inner; 4380 verifyFormat("TESTSUITE(A) {\n" 4381 "TESTSUITE(B) {\n" 4382 " int foo();\n" 4383 "} // TESTSUITE(B)\n" 4384 "} // TESTSUITE(A)", 4385 Style); 4386 verifyFormat("TESTSUITE(A) {\n" 4387 "namespace B {\n" 4388 " int foo();\n" 4389 "} // namespace B\n" 4390 "} // TESTSUITE(A)", 4391 Style); 4392 verifyFormat("namespace A {\n" 4393 "TESTSUITE(B) {\n" 4394 " int foo();\n" 4395 "} // TESTSUITE(B)\n" 4396 "} // namespace A", 4397 Style); 4398 4399 // Properly merge namespace-macros blocks in CompactNamespaces mode 4400 Style.NamespaceIndentation = FormatStyle::NI_None; 4401 Style.CompactNamespaces = true; 4402 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 4403 "}} // TESTSUITE(A::B)", 4404 Style); 4405 4406 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n" 4407 "}} // TESTSUITE(out::in)", 4408 "TESTSUITE(out) {\n" 4409 "TESTSUITE(in) {\n" 4410 "} // TESTSUITE(in)\n" 4411 "} // TESTSUITE(out)", 4412 Style); 4413 4414 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n" 4415 "}} // TESTSUITE(out::in)", 4416 "TESTSUITE(out) {\n" 4417 "TESTSUITE(in) {\n" 4418 "} // TESTSUITE(in)\n" 4419 "} // TESTSUITE(out)", 4420 Style); 4421 4422 // Do not merge different namespaces/macros 4423 verifyFormat("namespace out {\n" 4424 "TESTSUITE(in) {\n" 4425 "} // TESTSUITE(in)\n" 4426 "} // namespace out", 4427 Style); 4428 verifyFormat("TESTSUITE(out) {\n" 4429 "namespace in {\n" 4430 "} // namespace in\n" 4431 "} // TESTSUITE(out)", 4432 Style); 4433 Style.NamespaceMacros.push_back("FOOBAR"); 4434 verifyFormat("TESTSUITE(out) {\n" 4435 "FOOBAR(in) {\n" 4436 "} // FOOBAR(in)\n" 4437 "} // TESTSUITE(out)", 4438 Style); 4439 } 4440 4441 TEST_F(FormatTest, FormatsCompactNamespaces) { 4442 FormatStyle Style = getLLVMStyle(); 4443 Style.CompactNamespaces = true; 4444 Style.NamespaceMacros.push_back("TESTSUITE"); 4445 4446 verifyFormat("namespace A { namespace B {\n" 4447 "}} // namespace A::B", 4448 Style); 4449 4450 verifyFormat("namespace out { namespace in {\n" 4451 "}} // namespace out::in", 4452 "namespace out {\n" 4453 "namespace in {\n" 4454 "} // namespace in\n" 4455 "} // namespace out", 4456 Style); 4457 4458 // Only namespaces which have both consecutive opening and end get compacted 4459 verifyFormat("namespace out {\n" 4460 "namespace in1 {\n" 4461 "} // namespace in1\n" 4462 "namespace in2 {\n" 4463 "} // namespace in2\n" 4464 "} // namespace out", 4465 Style); 4466 4467 verifyFormat("namespace out {\n" 4468 "int i;\n" 4469 "namespace in {\n" 4470 "int j;\n" 4471 "} // namespace in\n" 4472 "int k;\n" 4473 "} // namespace out", 4474 "namespace out { int i;\n" 4475 "namespace in { int j; } // namespace in\n" 4476 "int k; } // namespace out", 4477 Style); 4478 4479 Style.ColumnLimit = 41; 4480 verifyFormat("namespace A { namespace B { namespace C {\n" 4481 "}}} // namespace A::B::C", 4482 "namespace A { namespace B {\n" 4483 "namespace C {\n" 4484 "}} // namespace B::C\n" 4485 "} // namespace A", 4486 Style); 4487 4488 Style.ColumnLimit = 40; 4489 verifyFormat("namespace aaaaaaaaaa {\n" 4490 "namespace bbbbbbbbbb {\n" 4491 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 4492 "namespace aaaaaaaaaa {\n" 4493 "namespace bbbbbbbbbb {\n" 4494 "} // namespace bbbbbbbbbb\n" 4495 "} // namespace aaaaaaaaaa", 4496 Style); 4497 4498 verifyFormat("namespace aaaaaa { namespace bbbbbb {\n" 4499 "namespace cccccc {\n" 4500 "}}} // namespace aaaaaa::bbbbbb::cccccc", 4501 "namespace aaaaaa {\n" 4502 "namespace bbbbbb {\n" 4503 "namespace cccccc {\n" 4504 "} // namespace cccccc\n" 4505 "} // namespace bbbbbb\n" 4506 "} // namespace aaaaaa", 4507 Style); 4508 4509 verifyFormat("namespace a { namespace b {\n" 4510 "namespace c {\n" 4511 "}}} // namespace a::b::c", 4512 Style); 4513 4514 Style.ColumnLimit = 80; 4515 4516 // Extra semicolon after 'inner' closing brace prevents merging 4517 verifyFormat("namespace out { namespace in {\n" 4518 "}; } // namespace out::in", 4519 "namespace out {\n" 4520 "namespace in {\n" 4521 "}; // namespace in\n" 4522 "} // namespace out", 4523 Style); 4524 4525 // Extra semicolon after 'outer' closing brace is conserved 4526 verifyFormat("namespace out { namespace in {\n" 4527 "}}; // namespace out::in", 4528 "namespace out {\n" 4529 "namespace in {\n" 4530 "} // namespace in\n" 4531 "}; // namespace out", 4532 Style); 4533 4534 Style.NamespaceIndentation = FormatStyle::NI_All; 4535 verifyFormat("namespace out { namespace in {\n" 4536 " int i;\n" 4537 "}} // namespace out::in", 4538 "namespace out {\n" 4539 "namespace in {\n" 4540 "int i;\n" 4541 "} // namespace in\n" 4542 "} // namespace out", 4543 Style); 4544 verifyFormat("namespace out { namespace mid {\n" 4545 " namespace in {\n" 4546 " int j;\n" 4547 " } // namespace in\n" 4548 " int k;\n" 4549 "}} // namespace out::mid", 4550 "namespace out { namespace mid {\n" 4551 "namespace in { int j; } // namespace in\n" 4552 "int k; }} // namespace out::mid", 4553 Style); 4554 4555 verifyFormat("namespace A { namespace B { namespace C {\n" 4556 " int i;\n" 4557 "}}} // namespace A::B::C\n" 4558 "int main() {\n" 4559 " if (true)\n" 4560 " return 0;\n" 4561 "}", 4562 "namespace A { namespace B {\n" 4563 "namespace C {\n" 4564 " int i;\n" 4565 "}} // namespace B::C\n" 4566 "} // namespace A\n" 4567 "int main() {\n" 4568 " if (true)\n" 4569 " return 0;\n" 4570 "}", 4571 Style); 4572 4573 verifyFormat("namespace A { namespace B { namespace C {\n" 4574 "#ifdef FOO\n" 4575 " int i;\n" 4576 "#endif\n" 4577 "}}} // namespace A::B::C\n" 4578 "int main() {\n" 4579 " if (true)\n" 4580 " return 0;\n" 4581 "}", 4582 "namespace A { namespace B {\n" 4583 "namespace C {\n" 4584 "#ifdef FOO\n" 4585 " int i;\n" 4586 "#endif\n" 4587 "}} // namespace B::C\n" 4588 "} // namespace A\n" 4589 "int main() {\n" 4590 " if (true)\n" 4591 " return 0;\n" 4592 "}", 4593 Style); 4594 4595 Style.NamespaceIndentation = FormatStyle::NI_Inner; 4596 verifyFormat("namespace out { namespace in {\n" 4597 " int i;\n" 4598 "}} // namespace out::in", 4599 "namespace out {\n" 4600 "namespace in {\n" 4601 "int i;\n" 4602 "} // namespace in\n" 4603 "} // namespace out", 4604 Style); 4605 verifyFormat("namespace out { namespace mid { namespace in {\n" 4606 " int i;\n" 4607 "}}} // namespace out::mid::in", 4608 "namespace out {\n" 4609 "namespace mid {\n" 4610 "namespace in {\n" 4611 "int i;\n" 4612 "} // namespace in\n" 4613 "} // namespace mid\n" 4614 "} // namespace out", 4615 Style); 4616 4617 Style.CompactNamespaces = true; 4618 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 4619 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4620 Style.BraceWrapping.BeforeLambdaBody = true; 4621 verifyFormat("namespace out { namespace in {\n" 4622 "}} // namespace out::in", 4623 Style); 4624 verifyFormat("namespace out { namespace in {\n" 4625 "}} // namespace out::in", 4626 "namespace out {\n" 4627 "namespace in {\n" 4628 "} // namespace in\n" 4629 "} // namespace out", 4630 Style); 4631 } 4632 4633 TEST_F(FormatTest, FormatsExternC) { 4634 verifyFormat("extern \"C\" {\nint a;"); 4635 verifyFormat("extern \"C\" {}"); 4636 verifyFormat("extern \"C\" {\n" 4637 "int foo();\n" 4638 "}"); 4639 verifyFormat("extern \"C\" int foo() {}"); 4640 verifyFormat("extern \"C\" int foo();"); 4641 verifyFormat("extern \"C\" int foo() {\n" 4642 " int i = 42;\n" 4643 " return i;\n" 4644 "}"); 4645 4646 FormatStyle Style = getLLVMStyle(); 4647 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4648 Style.BraceWrapping.AfterFunction = true; 4649 verifyFormat("extern \"C\" int foo() {}", Style); 4650 verifyFormat("extern \"C\" int foo();", Style); 4651 verifyFormat("extern \"C\" int foo()\n" 4652 "{\n" 4653 " int i = 42;\n" 4654 " return i;\n" 4655 "}", 4656 Style); 4657 4658 Style.BraceWrapping.AfterExternBlock = true; 4659 Style.BraceWrapping.SplitEmptyRecord = false; 4660 verifyFormat("extern \"C\"\n" 4661 "{}", 4662 Style); 4663 verifyFormat("extern \"C\"\n" 4664 "{\n" 4665 " int foo();\n" 4666 "}", 4667 Style); 4668 } 4669 4670 TEST_F(FormatTest, IndentExternBlockStyle) { 4671 FormatStyle Style = getLLVMStyle(); 4672 Style.IndentWidth = 2; 4673 4674 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4675 verifyFormat("extern \"C\" { /*9*/\n" 4676 "}", 4677 Style); 4678 verifyFormat("extern \"C\" {\n" 4679 " int foo10();\n" 4680 "}", 4681 Style); 4682 4683 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4684 verifyFormat("extern \"C\" { /*11*/\n" 4685 "}", 4686 Style); 4687 verifyFormat("extern \"C\" {\n" 4688 "int foo12();\n" 4689 "}", 4690 Style); 4691 4692 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4693 verifyFormat("extern \"C\"\n" 4694 "{\n" 4695 "int i;\n" 4696 "}", 4697 Style); 4698 4699 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4700 Style.BraceWrapping.AfterExternBlock = true; 4701 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4702 verifyFormat("extern \"C\"\n" 4703 "{ /*13*/\n" 4704 "}", 4705 Style); 4706 verifyFormat("extern \"C\"\n{\n" 4707 " int foo14();\n" 4708 "}", 4709 Style); 4710 4711 Style.BraceWrapping.AfterExternBlock = false; 4712 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4713 verifyFormat("extern \"C\" { /*15*/\n" 4714 "}", 4715 Style); 4716 verifyFormat("extern \"C\" {\n" 4717 "int foo16();\n" 4718 "}", 4719 Style); 4720 4721 Style.BraceWrapping.AfterExternBlock = true; 4722 verifyFormat("extern \"C\"\n" 4723 "{ /*13*/\n" 4724 "}", 4725 Style); 4726 verifyFormat("extern \"C\"\n" 4727 "{\n" 4728 "int foo14();\n" 4729 "}", 4730 Style); 4731 4732 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4733 verifyFormat("extern \"C\"\n" 4734 "{ /*13*/\n" 4735 "}", 4736 Style); 4737 verifyFormat("extern \"C\"\n" 4738 "{\n" 4739 " int foo14();\n" 4740 "}", 4741 Style); 4742 } 4743 4744 TEST_F(FormatTest, FormatsInlineASM) { 4745 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 4746 verifyFormat("asm(\"nop\" ::: \"memory\");"); 4747 verifyFormat( 4748 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 4749 " \"cpuid\\n\\t\"\n" 4750 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 4751 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 4752 " : \"a\"(value));"); 4753 verifyFormat( 4754 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4755 " __asm {\n" 4756 " mov edx,[that] // vtable in edx\n" 4757 " mov eax,methodIndex\n" 4758 " call [edx][eax*4] // stdcall\n" 4759 " }\n" 4760 "}", 4761 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4762 " __asm {\n" 4763 " mov edx,[that] // vtable in edx\n" 4764 " mov eax,methodIndex\n" 4765 " call [edx][eax*4] // stdcall\n" 4766 " }\n" 4767 "}"); 4768 verifyNoChange("_asm {\n" 4769 " xor eax, eax;\n" 4770 " cpuid;\n" 4771 "}"); 4772 verifyFormat("void function() {\n" 4773 " // comment\n" 4774 " asm(\"\");\n" 4775 "}"); 4776 verifyFormat("__asm {\n" 4777 "}\n" 4778 "int i;", 4779 "__asm {\n" 4780 "}\n" 4781 "int i;"); 4782 4783 auto Style = getLLVMStyleWithColumns(0); 4784 const StringRef Code1{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"}; 4785 const StringRef Code2{"asm(\"xyz\"\n" 4786 " : \"=a\"(a), \"=d\"(b)\n" 4787 " : \"a\"(data));"}; 4788 const StringRef Code3{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b)\n" 4789 " : \"a\"(data));"}; 4790 4791 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_OnlyMultiline; 4792 verifyFormat(Code1, Style); 4793 verifyNoChange(Code2, Style); 4794 verifyNoChange(Code3, Style); 4795 4796 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always; 4797 verifyFormat(Code2, Code1, Style); 4798 verifyNoChange(Code2, Style); 4799 verifyFormat(Code2, Code3, Style); 4800 } 4801 4802 TEST_F(FormatTest, FormatTryCatch) { 4803 verifyFormat("try {\n" 4804 " throw a * b;\n" 4805 "} catch (int a) {\n" 4806 " // Do nothing.\n" 4807 "} catch (...) {\n" 4808 " exit(42);\n" 4809 "}"); 4810 4811 // Function-level try statements. 4812 verifyFormat("int f() try { return 4; } catch (...) {\n" 4813 " return 5;\n" 4814 "}"); 4815 verifyFormat("class A {\n" 4816 " int a;\n" 4817 " A() try : a(0) {\n" 4818 " } catch (...) {\n" 4819 " throw;\n" 4820 " }\n" 4821 "};"); 4822 verifyFormat("class A {\n" 4823 " int a;\n" 4824 " A() try : a(0), b{1} {\n" 4825 " } catch (...) {\n" 4826 " throw;\n" 4827 " }\n" 4828 "};"); 4829 verifyFormat("class A {\n" 4830 " int a;\n" 4831 " A() try : a(0), b{1}, c{2} {\n" 4832 " } catch (...) {\n" 4833 " throw;\n" 4834 " }\n" 4835 "};"); 4836 verifyFormat("class A {\n" 4837 " int a;\n" 4838 " A() try : a(0), b{1}, c{2} {\n" 4839 " { // New scope.\n" 4840 " }\n" 4841 " } catch (...) {\n" 4842 " throw;\n" 4843 " }\n" 4844 "};"); 4845 4846 // Incomplete try-catch blocks. 4847 verifyIncompleteFormat("try {} catch ("); 4848 } 4849 4850 TEST_F(FormatTest, FormatTryAsAVariable) { 4851 verifyFormat("int try;"); 4852 verifyFormat("int try, size;"); 4853 verifyFormat("try = foo();"); 4854 verifyFormat("if (try < size) {\n return true;\n}"); 4855 4856 verifyFormat("int catch;"); 4857 verifyFormat("int catch, size;"); 4858 verifyFormat("catch = foo();"); 4859 verifyFormat("if (catch < size) {\n return true;\n}"); 4860 4861 FormatStyle Style = getLLVMStyle(); 4862 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4863 Style.BraceWrapping.AfterFunction = true; 4864 Style.BraceWrapping.BeforeCatch = true; 4865 verifyFormat("try {\n" 4866 " int bar = 1;\n" 4867 "}\n" 4868 "catch (...) {\n" 4869 " int bar = 1;\n" 4870 "}", 4871 Style); 4872 verifyFormat("#if NO_EX\n" 4873 "try\n" 4874 "#endif\n" 4875 "{\n" 4876 "}\n" 4877 "#if NO_EX\n" 4878 "catch (...) {\n" 4879 "}", 4880 Style); 4881 verifyFormat("try /* abc */ {\n" 4882 " int bar = 1;\n" 4883 "}\n" 4884 "catch (...) {\n" 4885 " int bar = 1;\n" 4886 "}", 4887 Style); 4888 verifyFormat("try\n" 4889 "// abc\n" 4890 "{\n" 4891 " int bar = 1;\n" 4892 "}\n" 4893 "catch (...) {\n" 4894 " int bar = 1;\n" 4895 "}", 4896 Style); 4897 } 4898 4899 TEST_F(FormatTest, FormatSEHTryCatch) { 4900 verifyFormat("__try {\n" 4901 " int a = b * c;\n" 4902 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 4903 " // Do nothing.\n" 4904 "}"); 4905 4906 verifyFormat("__try {\n" 4907 " int a = b * c;\n" 4908 "} __finally {\n" 4909 " // Do nothing.\n" 4910 "}"); 4911 4912 verifyFormat("DEBUG({\n" 4913 " __try {\n" 4914 " } __finally {\n" 4915 " }\n" 4916 "});"); 4917 } 4918 4919 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 4920 verifyFormat("try {\n" 4921 " f();\n" 4922 "} catch {\n" 4923 " g();\n" 4924 "}"); 4925 verifyFormat("try {\n" 4926 " f();\n" 4927 "} catch (A a) MACRO(x) {\n" 4928 " g();\n" 4929 "} catch (B b) MACRO(x) {\n" 4930 " g();\n" 4931 "}"); 4932 } 4933 4934 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 4935 FormatStyle Style = getLLVMStyle(); 4936 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 4937 FormatStyle::BS_WebKit}) { 4938 Style.BreakBeforeBraces = BraceStyle; 4939 verifyFormat("try {\n" 4940 " // something\n" 4941 "} catch (...) {\n" 4942 " // something\n" 4943 "}", 4944 Style); 4945 } 4946 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4947 verifyFormat("try {\n" 4948 " // something\n" 4949 "}\n" 4950 "catch (...) {\n" 4951 " // something\n" 4952 "}", 4953 Style); 4954 verifyFormat("__try {\n" 4955 " // something\n" 4956 "}\n" 4957 "__finally {\n" 4958 " // something\n" 4959 "}", 4960 Style); 4961 verifyFormat("@try {\n" 4962 " // something\n" 4963 "}\n" 4964 "@finally {\n" 4965 " // something\n" 4966 "}", 4967 Style); 4968 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4969 verifyFormat("try\n" 4970 "{\n" 4971 " // something\n" 4972 "}\n" 4973 "catch (...)\n" 4974 "{\n" 4975 " // something\n" 4976 "}", 4977 Style); 4978 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 4979 verifyFormat("try\n" 4980 " {\n" 4981 " // something white\n" 4982 " }\n" 4983 "catch (...)\n" 4984 " {\n" 4985 " // something white\n" 4986 " }", 4987 Style); 4988 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 4989 verifyFormat("try\n" 4990 " {\n" 4991 " // something\n" 4992 " }\n" 4993 "catch (...)\n" 4994 " {\n" 4995 " // something\n" 4996 " }", 4997 Style); 4998 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4999 Style.BraceWrapping.BeforeCatch = true; 5000 verifyFormat("try {\n" 5001 " // something\n" 5002 "}\n" 5003 "catch (...) {\n" 5004 " // something\n" 5005 "}", 5006 Style); 5007 } 5008 5009 TEST_F(FormatTest, StaticInitializers) { 5010 verifyFormat("static SomeClass SC = {1, 'a'};"); 5011 5012 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 5013 " 100000000, " 5014 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 5015 5016 // Here, everything other than the "}" would fit on a line. 5017 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 5018 " 10000000000000000000000000};"); 5019 verifyFormat("S s = {a,\n" 5020 "\n" 5021 " b};", 5022 "S s = {\n" 5023 " a,\n" 5024 "\n" 5025 " b\n" 5026 "};"); 5027 5028 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 5029 // line. However, the formatting looks a bit off and this probably doesn't 5030 // happen often in practice. 5031 verifyFormat("static int Variable[1] = {\n" 5032 " {1000000000000000000000000000000000000}};", 5033 getLLVMStyleWithColumns(40)); 5034 } 5035 5036 TEST_F(FormatTest, DesignatedInitializers) { 5037 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 5038 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 5039 " .bbbbbbbbbb = 2,\n" 5040 " .cccccccccc = 3,\n" 5041 " .dddddddddd = 4,\n" 5042 " .eeeeeeeeee = 5};"); 5043 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 5044 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 5045 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 5046 " .ccccccccccccccccccccccccccc = 3,\n" 5047 " .ddddddddddddddddddddddddddd = 4,\n" 5048 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 5049 5050 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 5051 5052 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 5053 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 5054 " [2] = bbbbbbbbbb,\n" 5055 " [3] = cccccccccc,\n" 5056 " [4] = dddddddddd,\n" 5057 " [5] = eeeeeeeeee};"); 5058 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 5059 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5060 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5061 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 5062 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 5063 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 5064 5065 verifyFormat("for (const TestCase &test_case : {\n" 5066 " TestCase{\n" 5067 " .a = 1,\n" 5068 " .b = 1,\n" 5069 " },\n" 5070 " TestCase{\n" 5071 " .a = 2,\n" 5072 " .b = 2,\n" 5073 " },\n" 5074 " }) {\n" 5075 "}"); 5076 } 5077 5078 TEST_F(FormatTest, BracedInitializerIndentWidth) { 5079 auto Style = getLLVMStyleWithColumns(60); 5080 Style.BinPackArguments = true; 5081 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5082 Style.BracedInitializerIndentWidth = 6; 5083 5084 // Non-initializing braces are unaffected by BracedInitializerIndentWidth. 5085 verifyFormat("enum class {\n" 5086 " One,\n" 5087 " Two,\n" 5088 "};", 5089 Style); 5090 verifyFormat("class Foo {\n" 5091 " Foo() {}\n" 5092 " void bar();\n" 5093 "};", 5094 Style); 5095 verifyFormat("void foo() {\n" 5096 " auto bar = baz;\n" 5097 " return baz;\n" 5098 "};", 5099 Style); 5100 verifyFormat("auto foo = [&] {\n" 5101 " auto bar = baz;\n" 5102 " return baz;\n" 5103 "};", 5104 Style); 5105 verifyFormat("{\n" 5106 " auto bar = baz;\n" 5107 " return baz;\n" 5108 "};", 5109 Style); 5110 // Non-brace initialization is unaffected by BracedInitializerIndentWidth. 5111 verifyFormat("SomeClass clazz(\n" 5112 " \"xxxxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyyyy\",\n" 5113 " \"zzzzzzzzzzzzzzzzzz\");", 5114 Style); 5115 5116 // The following types of initialization are all affected by 5117 // BracedInitializerIndentWidth. Aggregate initialization. 5118 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n" 5119 " 10000000, 20000000};", 5120 Style); 5121 verifyFormat("SomeStruct s{\n" 5122 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n" 5123 " \"zzzzzzzzzzzzzzzz\"};", 5124 Style); 5125 // Designated initializers. 5126 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n" 5127 " [0] = 10000000, [1] = 20000000};", 5128 Style); 5129 verifyFormat("SomeStruct s{\n" 5130 " .foo = \"xxxxxxxxxxxxx\",\n" 5131 " .bar = \"yyyyyyyyyyyyy\",\n" 5132 " .baz = \"zzzzzzzzzzzzz\"};", 5133 Style); 5134 // List initialization. 5135 verifyFormat("SomeStruct s{\n" 5136 " \"xxxxxxxxxxxxx\",\n" 5137 " \"yyyyyyyyyyyyy\",\n" 5138 " \"zzzzzzzzzzzzz\",\n" 5139 "};", 5140 Style); 5141 verifyFormat("SomeStruct{\n" 5142 " \"xxxxxxxxxxxxx\",\n" 5143 " \"yyyyyyyyyyyyy\",\n" 5144 " \"zzzzzzzzzzzzz\",\n" 5145 "};", 5146 Style); 5147 verifyFormat("new SomeStruct{\n" 5148 " \"xxxxxxxxxxxxx\",\n" 5149 " \"yyyyyyyyyyyyy\",\n" 5150 " \"zzzzzzzzzzzzz\",\n" 5151 "};", 5152 Style); 5153 // Member initializer. 5154 verifyFormat("class SomeClass {\n" 5155 " SomeStruct s{\n" 5156 " \"xxxxxxxxxxxxx\",\n" 5157 " \"yyyyyyyyyyyyy\",\n" 5158 " \"zzzzzzzzzzzzz\",\n" 5159 " };\n" 5160 "};", 5161 Style); 5162 // Constructor member initializer. 5163 verifyFormat("SomeClass::SomeClass : strct{\n" 5164 " \"xxxxxxxxxxxxx\",\n" 5165 " \"yyyyyyyyyyyyy\",\n" 5166 " \"zzzzzzzzzzzzz\",\n" 5167 " } {}", 5168 Style); 5169 // Copy initialization. 5170 verifyFormat("SomeStruct s = SomeStruct{\n" 5171 " \"xxxxxxxxxxxxx\",\n" 5172 " \"yyyyyyyyyyyyy\",\n" 5173 " \"zzzzzzzzzzzzz\",\n" 5174 "};", 5175 Style); 5176 // Copy list initialization. 5177 verifyFormat("SomeStruct s = {\n" 5178 " \"xxxxxxxxxxxxx\",\n" 5179 " \"yyyyyyyyyyyyy\",\n" 5180 " \"zzzzzzzzzzzzz\",\n" 5181 "};", 5182 Style); 5183 // Assignment operand initialization. 5184 verifyFormat("s = {\n" 5185 " \"xxxxxxxxxxxxx\",\n" 5186 " \"yyyyyyyyyyyyy\",\n" 5187 " \"zzzzzzzzzzzzz\",\n" 5188 "};", 5189 Style); 5190 // Returned object initialization. 5191 verifyFormat("return {\n" 5192 " \"xxxxxxxxxxxxx\",\n" 5193 " \"yyyyyyyyyyyyy\",\n" 5194 " \"zzzzzzzzzzzzz\",\n" 5195 "};", 5196 Style); 5197 // Initializer list. 5198 verifyFormat("auto initializerList = {\n" 5199 " \"xxxxxxxxxxxxx\",\n" 5200 " \"yyyyyyyyyyyyy\",\n" 5201 " \"zzzzzzzzzzzzz\",\n" 5202 "};", 5203 Style); 5204 // Function parameter initialization. 5205 verifyFormat("func({\n" 5206 " \"xxxxxxxxxxxxx\",\n" 5207 " \"yyyyyyyyyyyyy\",\n" 5208 " \"zzzzzzzzzzzzz\",\n" 5209 "});", 5210 Style); 5211 // Nested init lists. 5212 verifyFormat("SomeStruct s = {\n" 5213 " {{init1, init2, init3, init4, init5},\n" 5214 " {init1, init2, init3, init4, init5}}};", 5215 Style); 5216 verifyFormat("SomeStruct s = {\n" 5217 " {{\n" 5218 " .init1 = 1,\n" 5219 " .init2 = 2,\n" 5220 " .init3 = 3,\n" 5221 " .init4 = 4,\n" 5222 " .init5 = 5,\n" 5223 " },\n" 5224 " {init1, init2, init3, init4, init5}}};", 5225 Style); 5226 verifyFormat("SomeArrayT a[3] = {\n" 5227 " {\n" 5228 " foo,\n" 5229 " bar,\n" 5230 " },\n" 5231 " {\n" 5232 " foo,\n" 5233 " bar,\n" 5234 " },\n" 5235 " SomeArrayT{},\n" 5236 "};", 5237 Style); 5238 verifyFormat("SomeArrayT a[3] = {\n" 5239 " {foo},\n" 5240 " {\n" 5241 " {\n" 5242 " init1,\n" 5243 " init2,\n" 5244 " init3,\n" 5245 " },\n" 5246 " {\n" 5247 " init1,\n" 5248 " init2,\n" 5249 " init3,\n" 5250 " },\n" 5251 " },\n" 5252 " {baz},\n" 5253 "};", 5254 Style); 5255 5256 // Aligning after open braces unaffected by BracedInitializerIndentWidth. 5257 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 5258 verifyFormat("SomeStruct s{\"xxxxxxxxxxxxx\", \"yyyyyyyyyyyyy\",\n" 5259 " \"zzzzzzzzzzzzz\"};", 5260 Style); 5261 } 5262 5263 TEST_F(FormatTest, NestedStaticInitializers) { 5264 verifyFormat("static A x = {{{}}};"); 5265 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 5266 " {init1, init2, init3, init4}}};", 5267 getLLVMStyleWithColumns(50)); 5268 5269 verifyFormat("somes Status::global_reps[3] = {\n" 5270 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 5271 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 5272 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 5273 getLLVMStyleWithColumns(60)); 5274 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 5275 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 5276 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 5277 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 5278 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 5279 " {rect.fRight - rect.fLeft, rect.fBottom - " 5280 "rect.fTop}};"); 5281 5282 verifyFormat( 5283 "SomeArrayOfSomeType a = {\n" 5284 " {{1, 2, 3},\n" 5285 " {1, 2, 3},\n" 5286 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 5287 " 333333333333333333333333333333},\n" 5288 " {1, 2, 3},\n" 5289 " {1, 2, 3}}};"); 5290 verifyFormat( 5291 "SomeArrayOfSomeType a = {\n" 5292 " {{1, 2, 3}},\n" 5293 " {{1, 2, 3}},\n" 5294 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 5295 " 333333333333333333333333333333}},\n" 5296 " {{1, 2, 3}},\n" 5297 " {{1, 2, 3}}};"); 5298 5299 verifyFormat("struct {\n" 5300 " unsigned bit;\n" 5301 " const char *const name;\n" 5302 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 5303 " {kOsWin, \"Windows\"},\n" 5304 " {kOsLinux, \"Linux\"},\n" 5305 " {kOsCrOS, \"Chrome OS\"}};"); 5306 verifyFormat("struct {\n" 5307 " unsigned bit;\n" 5308 " const char *const name;\n" 5309 "} kBitsToOs[] = {\n" 5310 " {kOsMac, \"Mac\"},\n" 5311 " {kOsWin, \"Windows\"},\n" 5312 " {kOsLinux, \"Linux\"},\n" 5313 " {kOsCrOS, \"Chrome OS\"},\n" 5314 "};"); 5315 } 5316 5317 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 5318 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5319 " \\\n" 5320 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 5321 } 5322 5323 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 5324 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 5325 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 5326 5327 // Do break defaulted and deleted functions. 5328 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 5329 " default;", 5330 getLLVMStyleWithColumns(40)); 5331 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 5332 " delete;", 5333 getLLVMStyleWithColumns(40)); 5334 } 5335 5336 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 5337 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 5338 getLLVMStyleWithColumns(40)); 5339 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 5340 getLLVMStyleWithColumns(40)); 5341 verifyFormat("#define Q \\\n" 5342 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 5343 " \"aaaaaaaa.cpp\"", 5344 "#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 5345 getLLVMStyleWithColumns(40)); 5346 } 5347 5348 TEST_F(FormatTest, UnderstandsLinePPDirective) { 5349 verifyFormat("# 123 \"A string literal\"", 5350 " # 123 \"A string literal\""); 5351 } 5352 5353 TEST_F(FormatTest, LayoutUnknownPPDirective) { 5354 verifyFormat("#;"); 5355 verifyFormat("#\n;\n;\n;"); 5356 } 5357 5358 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 5359 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\""); 5360 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B", 5361 getLLVMStyleWithColumns(12)); 5362 } 5363 5364 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 5365 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\""); 5366 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B"); 5367 } 5368 5369 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 5370 verifyFormat("#define A \\x20"); 5371 verifyFormat("#define A \\ x20"); 5372 verifyFormat("#define A \\ x20", "#define A \\ x20"); 5373 verifyFormat("#define A ''"); 5374 verifyFormat("#define A ''qqq"); 5375 verifyFormat("#define A `qqq"); 5376 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 5377 verifyFormat("const char *c = STRINGIFY(\n" 5378 "\\na : b);", 5379 "const char * c = STRINGIFY(\n" 5380 "\\na : b);"); 5381 5382 verifyFormat("a\r\\"); 5383 verifyFormat("a\v\\"); 5384 verifyFormat("a\f\\"); 5385 } 5386 5387 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 5388 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 5389 style.IndentWidth = 4; 5390 style.PPIndentWidth = 1; 5391 5392 style.IndentPPDirectives = FormatStyle::PPDIS_None; 5393 verifyFormat("#ifdef __linux__\n" 5394 "void foo() {\n" 5395 " int x = 0;\n" 5396 "}\n" 5397 "#define FOO\n" 5398 "#endif\n" 5399 "void bar() {\n" 5400 " int y = 0;\n" 5401 "}", 5402 style); 5403 5404 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 5405 verifyFormat("#ifdef __linux__\n" 5406 "void foo() {\n" 5407 " int x = 0;\n" 5408 "}\n" 5409 "# define FOO foo\n" 5410 "#endif\n" 5411 "void bar() {\n" 5412 " int y = 0;\n" 5413 "}", 5414 style); 5415 5416 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 5417 verifyFormat("#ifdef __linux__\n" 5418 "void foo() {\n" 5419 " int x = 0;\n" 5420 "}\n" 5421 " #define FOO foo\n" 5422 "#endif\n" 5423 "void bar() {\n" 5424 " int y = 0;\n" 5425 "}", 5426 style); 5427 verifyFormat("#if 1\n" 5428 " // some comments\n" 5429 " // another\n" 5430 " #define foo 1\n" 5431 "// not a define comment\n" 5432 "void bar() {\n" 5433 " // comment\n" 5434 " int y = 0;\n" 5435 "}", 5436 "#if 1\n" 5437 "// some comments\n" 5438 "// another\n" 5439 "#define foo 1\n" 5440 "// not a define comment\n" 5441 "void bar() {\n" 5442 " // comment\n" 5443 " int y = 0;\n" 5444 "}", 5445 style); 5446 5447 style.IndentPPDirectives = FormatStyle::PPDIS_None; 5448 verifyFormat("#ifdef foo\n" 5449 "#define bar() \\\n" 5450 " if (A) { \\\n" 5451 " B(); \\\n" 5452 " } \\\n" 5453 " C();\n" 5454 "#endif", 5455 style); 5456 verifyFormat("if (emacs) {\n" 5457 "#ifdef is\n" 5458 "#define lit \\\n" 5459 " if (af) { \\\n" 5460 " return duh(); \\\n" 5461 " }\n" 5462 "#endif\n" 5463 "}", 5464 style); 5465 verifyFormat("#if abc\n" 5466 "#ifdef foo\n" 5467 "#define bar() \\\n" 5468 " if (A) { \\\n" 5469 " if (B) { \\\n" 5470 " C(); \\\n" 5471 " } \\\n" 5472 " } \\\n" 5473 " D();\n" 5474 "#endif\n" 5475 "#endif", 5476 style); 5477 verifyFormat("#ifndef foo\n" 5478 "#define foo\n" 5479 "if (emacs) {\n" 5480 "#ifdef is\n" 5481 "#define lit \\\n" 5482 " if (af) { \\\n" 5483 " return duh(); \\\n" 5484 " }\n" 5485 "#endif\n" 5486 "}\n" 5487 "#endif", 5488 style); 5489 verifyFormat("#if 1\n" 5490 "#define X \\\n" 5491 " { \\\n" 5492 " x; \\\n" 5493 " x; \\\n" 5494 " }\n" 5495 "#endif", 5496 style); 5497 verifyFormat("#define X \\\n" 5498 " { \\\n" 5499 " x; \\\n" 5500 " x; \\\n" 5501 " }", 5502 style); 5503 5504 style.PPIndentWidth = 2; 5505 verifyFormat("#ifdef foo\n" 5506 "#define bar() \\\n" 5507 " if (A) { \\\n" 5508 " B(); \\\n" 5509 " } \\\n" 5510 " C();\n" 5511 "#endif", 5512 style); 5513 style.IndentWidth = 8; 5514 verifyFormat("#ifdef foo\n" 5515 "#define bar() \\\n" 5516 " if (A) { \\\n" 5517 " B(); \\\n" 5518 " } \\\n" 5519 " C();\n" 5520 "#endif", 5521 style); 5522 5523 style.IndentWidth = 1; 5524 style.PPIndentWidth = 4; 5525 verifyFormat("#if 1\n" 5526 "#define X \\\n" 5527 " { \\\n" 5528 " x; \\\n" 5529 " x; \\\n" 5530 " }\n" 5531 "#endif", 5532 style); 5533 verifyFormat("#define X \\\n" 5534 " { \\\n" 5535 " x; \\\n" 5536 " x; \\\n" 5537 " }", 5538 style); 5539 5540 style.IndentWidth = 4; 5541 style.PPIndentWidth = 1; 5542 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 5543 verifyFormat("#ifdef foo\n" 5544 "# define bar() \\\n" 5545 " if (A) { \\\n" 5546 " B(); \\\n" 5547 " } \\\n" 5548 " C();\n" 5549 "#endif", 5550 style); 5551 verifyFormat("#if abc\n" 5552 "# ifdef foo\n" 5553 "# define bar() \\\n" 5554 " if (A) { \\\n" 5555 " if (B) { \\\n" 5556 " C(); \\\n" 5557 " } \\\n" 5558 " } \\\n" 5559 " D();\n" 5560 "# endif\n" 5561 "#endif", 5562 style); 5563 verifyFormat("#ifndef foo\n" 5564 "#define foo\n" 5565 "if (emacs) {\n" 5566 "#ifdef is\n" 5567 "# define lit \\\n" 5568 " if (af) { \\\n" 5569 " return duh(); \\\n" 5570 " }\n" 5571 "#endif\n" 5572 "}\n" 5573 "#endif", 5574 style); 5575 verifyFormat("#define X \\\n" 5576 " { \\\n" 5577 " x; \\\n" 5578 " x; \\\n" 5579 " }", 5580 style); 5581 5582 style.PPIndentWidth = 2; 5583 style.IndentWidth = 8; 5584 verifyFormat("#ifdef foo\n" 5585 "# define bar() \\\n" 5586 " if (A) { \\\n" 5587 " B(); \\\n" 5588 " } \\\n" 5589 " C();\n" 5590 "#endif", 5591 style); 5592 5593 style.PPIndentWidth = 4; 5594 style.IndentWidth = 1; 5595 verifyFormat("#define X \\\n" 5596 " { \\\n" 5597 " x; \\\n" 5598 " x; \\\n" 5599 " }", 5600 style); 5601 5602 style.IndentWidth = 4; 5603 style.PPIndentWidth = 1; 5604 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 5605 verifyFormat("if (emacs) {\n" 5606 "#ifdef is\n" 5607 " #define lit \\\n" 5608 " if (af) { \\\n" 5609 " return duh(); \\\n" 5610 " }\n" 5611 "#endif\n" 5612 "}", 5613 style); 5614 verifyFormat("#if abc\n" 5615 " #ifdef foo\n" 5616 " #define bar() \\\n" 5617 " if (A) { \\\n" 5618 " B(); \\\n" 5619 " } \\\n" 5620 " C();\n" 5621 " #endif\n" 5622 "#endif", 5623 style); 5624 verifyFormat("#if 1\n" 5625 " #define X \\\n" 5626 " { \\\n" 5627 " x; \\\n" 5628 " x; \\\n" 5629 " }\n" 5630 "#endif", 5631 style); 5632 5633 style.PPIndentWidth = 2; 5634 verifyFormat("#ifdef foo\n" 5635 " #define bar() \\\n" 5636 " if (A) { \\\n" 5637 " B(); \\\n" 5638 " } \\\n" 5639 " C();\n" 5640 "#endif", 5641 style); 5642 5643 style.PPIndentWidth = 4; 5644 style.IndentWidth = 1; 5645 verifyFormat("#if 1\n" 5646 " #define X \\\n" 5647 " { \\\n" 5648 " x; \\\n" 5649 " x; \\\n" 5650 " }\n" 5651 "#endif", 5652 style); 5653 } 5654 5655 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 5656 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 5657 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 5658 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 5659 // FIXME: We never break before the macro name. 5660 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 5661 5662 verifyFormat("#define A A\n#define A A"); 5663 verifyFormat("#define A(X) A\n#define A A"); 5664 5665 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 5666 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 5667 } 5668 5669 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 5670 verifyFormat("// somecomment\n" 5671 "#include \"a.h\"\n" 5672 "#define A( \\\n" 5673 " A, B)\n" 5674 "#include \"b.h\"\n" 5675 "// somecomment", 5676 " // somecomment\n" 5677 " #include \"a.h\"\n" 5678 "#define A(A,\\\n" 5679 " B)\n" 5680 " #include \"b.h\"\n" 5681 " // somecomment", 5682 getLLVMStyleWithColumns(13)); 5683 } 5684 5685 TEST_F(FormatTest, LayoutSingleHash) { verifyFormat("#\na;"); } 5686 5687 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 5688 verifyFormat("#define A \\\n" 5689 " c; \\\n" 5690 " e;\n" 5691 "f;", 5692 "#define A c; e;\n" 5693 "f;", 5694 getLLVMStyleWithColumns(14)); 5695 } 5696 5697 TEST_F(FormatTest, LayoutRemainingTokens) { 5698 verifyFormat("{\n" 5699 "}"); 5700 } 5701 5702 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 5703 verifyFormat("int x,\n" 5704 "#define A\n" 5705 " y;", 5706 "int x,\n#define A\ny;"); 5707 } 5708 5709 TEST_F(FormatTest, HashInMacroDefinition) { 5710 verifyFormat("#define A(c) L#c"); 5711 verifyFormat("#define A(c) u#c"); 5712 verifyFormat("#define A(c) U#c"); 5713 verifyFormat("#define A(c) u8#c"); 5714 verifyFormat("#define A(c) LR#c"); 5715 verifyFormat("#define A(c) uR#c"); 5716 verifyFormat("#define A(c) UR#c"); 5717 verifyFormat("#define A(c) u8R#c"); 5718 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 5719 verifyFormat("#define A \\\n" 5720 " { \\\n" 5721 " f(#c); \\\n" 5722 " }", 5723 getLLVMStyleWithColumns(11)); 5724 5725 verifyFormat("#define A(X) \\\n" 5726 " void function##X()", 5727 getLLVMStyleWithColumns(22)); 5728 5729 verifyFormat("#define A(a, b, c) \\\n" 5730 " void a##b##c()", 5731 getLLVMStyleWithColumns(22)); 5732 5733 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 5734 5735 verifyFormat("{\n" 5736 " {\n" 5737 "#define GEN_ID(_x) char *_x{#_x}\n" 5738 " GEN_ID(one);\n" 5739 " }\n" 5740 "}"); 5741 } 5742 5743 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 5744 verifyFormat("#define A (x)"); 5745 verifyFormat("#define A(x)"); 5746 5747 FormatStyle Style = getLLVMStyle(); 5748 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 5749 verifyFormat("#define true ((foo)1)", Style); 5750 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 5751 verifyFormat("#define false((foo)0)", Style); 5752 } 5753 5754 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 5755 verifyFormat("#define A b;", 5756 "#define A \\\n" 5757 " \\\n" 5758 " b;", 5759 getLLVMStyleWithColumns(25)); 5760 verifyNoChange("#define A \\\n" 5761 " \\\n" 5762 " a; \\\n" 5763 " b;", 5764 getLLVMStyleWithColumns(11)); 5765 verifyNoChange("#define A \\\n" 5766 " a; \\\n" 5767 " \\\n" 5768 " b;", 5769 getLLVMStyleWithColumns(11)); 5770 } 5771 5772 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 5773 verifyIncompleteFormat("#define A :"); 5774 verifyFormat("#define SOMECASES \\\n" 5775 " case 1: \\\n" 5776 " case 2", 5777 getLLVMStyleWithColumns(20)); 5778 verifyFormat("#define MACRO(a) \\\n" 5779 " if (a) \\\n" 5780 " f(); \\\n" 5781 " else \\\n" 5782 " g()", 5783 getLLVMStyleWithColumns(18)); 5784 verifyFormat("#define A template <typename T>"); 5785 verifyIncompleteFormat("#define STR(x) #x\n" 5786 "f(STR(this_is_a_string_literal{));"); 5787 verifyFormat("#pragma omp threadprivate( \\\n" 5788 " y)), // expected-warning", 5789 getLLVMStyleWithColumns(28)); 5790 verifyFormat("#d, = };"); 5791 verifyFormat("#if \"a"); 5792 verifyIncompleteFormat("({\n" 5793 "#define b \\\n" 5794 " } \\\n" 5795 " a\n" 5796 "a", 5797 getLLVMStyleWithColumns(15)); 5798 verifyFormat("#define A \\\n" 5799 " { \\\n" 5800 " {\n" 5801 "#define B \\\n" 5802 " } \\\n" 5803 " }", 5804 getLLVMStyleWithColumns(15)); 5805 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 5806 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 5807 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 5808 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 5809 verifyNoCrash("#else\n" 5810 "#else\n" 5811 "#endif\n" 5812 "#endif"); 5813 verifyNoCrash("#else\n" 5814 "#if X\n" 5815 "#endif\n" 5816 "#endif"); 5817 verifyNoCrash("#else\n" 5818 "#endif\n" 5819 "#if X\n" 5820 "#endif"); 5821 verifyNoCrash("#if X\n" 5822 "#else\n" 5823 "#else\n" 5824 "#endif\n" 5825 "#endif"); 5826 verifyNoCrash("#if X\n" 5827 "#elif Y\n" 5828 "#elif Y\n" 5829 "#endif\n" 5830 "#endif"); 5831 verifyNoCrash("#endif\n" 5832 "#endif"); 5833 verifyNoCrash("#endif\n" 5834 "#else"); 5835 verifyNoCrash("#endif\n" 5836 "#elif Y"); 5837 } 5838 5839 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 5840 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 5841 verifyFormat("class A : public QObject {\n" 5842 " Q_OBJECT\n" 5843 "\n" 5844 " A() {}\n" 5845 "};", 5846 "class A : public QObject {\n" 5847 " Q_OBJECT\n" 5848 "\n" 5849 " A() {\n}\n" 5850 "} ;"); 5851 verifyFormat("MACRO\n" 5852 "/*static*/ int i;", 5853 "MACRO\n" 5854 " /*static*/ int i;"); 5855 verifyFormat("SOME_MACRO\n" 5856 "namespace {\n" 5857 "void f();\n" 5858 "} // namespace", 5859 "SOME_MACRO\n" 5860 " namespace {\n" 5861 "void f( );\n" 5862 "} // namespace"); 5863 // Only if the identifier contains at least 5 characters. 5864 verifyFormat("HTTP f();", "HTTP\nf();"); 5865 verifyNoChange("MACRO\nf();"); 5866 // Only if everything is upper case. 5867 verifyFormat("class A : public QObject {\n" 5868 " Q_Object A() {}\n" 5869 "};", 5870 "class A : public QObject {\n" 5871 " Q_Object\n" 5872 " A() {\n}\n" 5873 "} ;"); 5874 5875 // Only if the next line can actually start an unwrapped line. 5876 verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n" 5877 "<< SomeThing;"); 5878 5879 verifyFormat("GGGG(ffff(xxxxxxxxxxxxxxxxxxxx)->yyyyyyyyyyyyyyyyyyyy)(foo);", 5880 "GGGG(ffff(xxxxxxxxxxxxxxxxxxxx)->yyyyyyyyyyyyyyyyyyyy)\n" 5881 "(foo);", 5882 getLLVMStyleWithColumns(60)); 5883 5884 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 5885 "(n, buffers))", 5886 getChromiumStyle(FormatStyle::LK_Cpp)); 5887 5888 // See PR41483 5889 verifyNoChange("/**/ FOO(a)\n" 5890 "FOO(b)"); 5891 } 5892 5893 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 5894 verifyFormat("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 5895 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 5896 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 5897 "class X {};\n" 5898 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 5899 "int *createScopDetectionPass() { return 0; }", 5900 " INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 5901 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 5902 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 5903 " class X {};\n" 5904 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 5905 " int *createScopDetectionPass() { return 0; }"); 5906 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 5907 // braces, so that inner block is indented one level more. 5908 verifyFormat("int q() {\n" 5909 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 5910 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 5911 " IPC_END_MESSAGE_MAP()\n" 5912 "}", 5913 "int q() {\n" 5914 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 5915 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 5916 " IPC_END_MESSAGE_MAP()\n" 5917 "}"); 5918 5919 // Same inside macros. 5920 verifyFormat("#define LIST(L) \\\n" 5921 " L(A) \\\n" 5922 " L(B) \\\n" 5923 " L(C)", 5924 "#define LIST(L) \\\n" 5925 " L(A) \\\n" 5926 " L(B) \\\n" 5927 " L(C)", 5928 getGoogleStyle()); 5929 5930 // These must not be recognized as macros. 5931 verifyFormat("int q() {\n" 5932 " f(x);\n" 5933 " f(x) {}\n" 5934 " f(x)->g();\n" 5935 " f(x)->*g();\n" 5936 " f(x).g();\n" 5937 " f(x) = x;\n" 5938 " f(x) += x;\n" 5939 " f(x) -= x;\n" 5940 " f(x) *= x;\n" 5941 " f(x) /= x;\n" 5942 " f(x) %= x;\n" 5943 " f(x) &= x;\n" 5944 " f(x) |= x;\n" 5945 " f(x) ^= x;\n" 5946 " f(x) >>= x;\n" 5947 " f(x) <<= x;\n" 5948 " f(x)[y].z();\n" 5949 " LOG(INFO) << x;\n" 5950 " ifstream(x) >> x;\n" 5951 "}", 5952 "int q() {\n" 5953 " f(x)\n;\n" 5954 " f(x)\n {}\n" 5955 " f(x)\n->g();\n" 5956 " f(x)\n->*g();\n" 5957 " f(x)\n.g();\n" 5958 " f(x)\n = x;\n" 5959 " f(x)\n += x;\n" 5960 " f(x)\n -= x;\n" 5961 " f(x)\n *= x;\n" 5962 " f(x)\n /= x;\n" 5963 " f(x)\n %= x;\n" 5964 " f(x)\n &= x;\n" 5965 " f(x)\n |= x;\n" 5966 " f(x)\n ^= x;\n" 5967 " f(x)\n >>= x;\n" 5968 " f(x)\n <<= x;\n" 5969 " f(x)\n[y].z();\n" 5970 " LOG(INFO)\n << x;\n" 5971 " ifstream(x)\n >> x;\n" 5972 "}"); 5973 verifyFormat("int q() {\n" 5974 " F(x)\n" 5975 " if (1) {\n" 5976 " }\n" 5977 " F(x)\n" 5978 " while (1) {\n" 5979 " }\n" 5980 " F(x)\n" 5981 " G(x);\n" 5982 " F(x)\n" 5983 " try {\n" 5984 " Q();\n" 5985 " } catch (...) {\n" 5986 " }\n" 5987 "}", 5988 "int q() {\n" 5989 "F(x)\n" 5990 "if (1) {}\n" 5991 "F(x)\n" 5992 "while (1) {}\n" 5993 "F(x)\n" 5994 "G(x);\n" 5995 "F(x)\n" 5996 "try { Q(); } catch (...) {}\n" 5997 "}"); 5998 verifyFormat("class A {\n" 5999 " A() : t(0) {}\n" 6000 " A(int i) noexcept() : {}\n" 6001 " A(X x)\n" // FIXME: function-level try blocks are broken. 6002 " try : t(0) {\n" 6003 " } catch (...) {\n" 6004 " }\n" 6005 "};", 6006 "class A {\n" 6007 " A()\n : t(0) {}\n" 6008 " A(int i)\n noexcept() : {}\n" 6009 " A(X x)\n" 6010 " try : t(0) {} catch (...) {}\n" 6011 "};"); 6012 FormatStyle Style = getLLVMStyle(); 6013 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6014 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 6015 Style.BraceWrapping.AfterFunction = true; 6016 verifyFormat("void f()\n" 6017 "try\n" 6018 "{\n" 6019 "}", 6020 "void f() try {\n" 6021 "}", 6022 Style); 6023 verifyFormat("class SomeClass {\n" 6024 "public:\n" 6025 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 6026 "};", 6027 "class SomeClass {\n" 6028 "public:\n" 6029 " SomeClass()\n" 6030 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 6031 "};"); 6032 verifyFormat("class SomeClass {\n" 6033 "public:\n" 6034 " SomeClass()\n" 6035 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 6036 "};", 6037 "class SomeClass {\n" 6038 "public:\n" 6039 " SomeClass()\n" 6040 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 6041 "};", 6042 getLLVMStyleWithColumns(40)); 6043 6044 verifyFormat("MACRO(>)"); 6045 6046 // Some macros contain an implicit semicolon. 6047 Style = getLLVMStyle(); 6048 Style.StatementMacros.push_back("FOO"); 6049 verifyFormat("FOO(a) int b = 0;"); 6050 verifyFormat("FOO(a)\n" 6051 "int b = 0;", 6052 Style); 6053 verifyFormat("FOO(a);\n" 6054 "int b = 0;", 6055 Style); 6056 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 6057 "int b = 0;", 6058 Style); 6059 verifyFormat("FOO()\n" 6060 "int b = 0;", 6061 Style); 6062 verifyFormat("FOO\n" 6063 "int b = 0;", 6064 Style); 6065 verifyFormat("void f() {\n" 6066 " FOO(a)\n" 6067 " return a;\n" 6068 "}", 6069 Style); 6070 verifyFormat("FOO(a)\n" 6071 "FOO(b)", 6072 Style); 6073 verifyFormat("int a = 0;\n" 6074 "FOO(b)\n" 6075 "int c = 0;", 6076 Style); 6077 verifyFormat("int a = 0;\n" 6078 "int x = FOO(a)\n" 6079 "int b = 0;", 6080 Style); 6081 verifyFormat("void foo(int a) { FOO(a) }\n" 6082 "uint32_t bar() {}", 6083 Style); 6084 } 6085 6086 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) { 6087 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 6088 6089 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()", 6090 ZeroColumn); 6091 } 6092 6093 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 6094 verifyFormat("#define A \\\n" 6095 " f({ \\\n" 6096 " g(); \\\n" 6097 " });", 6098 getLLVMStyleWithColumns(11)); 6099 } 6100 6101 TEST_F(FormatTest, IndentPreprocessorDirectives) { 6102 FormatStyle Style = getLLVMStyleWithColumns(40); 6103 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 6104 verifyFormat("#ifdef _WIN32\n" 6105 "#define A 0\n" 6106 "#ifdef VAR2\n" 6107 "#define B 1\n" 6108 "#include <someheader.h>\n" 6109 "#define MACRO \\\n" 6110 " some_very_long_func_aaaaaaaaaa();\n" 6111 "#endif\n" 6112 "#else\n" 6113 "#define A 1\n" 6114 "#endif", 6115 Style); 6116 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 6117 verifyFormat("#if 1\n" 6118 "# define __STR(x) #x\n" 6119 "#endif", 6120 Style); 6121 verifyFormat("#ifdef _WIN32\n" 6122 "# define A 0\n" 6123 "# ifdef VAR2\n" 6124 "# define B 1\n" 6125 "# include <someheader.h>\n" 6126 "# define MACRO \\\n" 6127 " some_very_long_func_aaaaaaaaaa();\n" 6128 "# endif\n" 6129 "#else\n" 6130 "# define A 1\n" 6131 "#endif", 6132 Style); 6133 verifyFormat("#if A\n" 6134 "# define MACRO \\\n" 6135 " void a(int x) { \\\n" 6136 " b(); \\\n" 6137 " c(); \\\n" 6138 " d(); \\\n" 6139 " e(); \\\n" 6140 " f(); \\\n" 6141 " }\n" 6142 "#endif", 6143 Style); 6144 // Comments before include guard. 6145 verifyFormat("// file comment\n" 6146 "// file comment\n" 6147 "#ifndef HEADER_H\n" 6148 "#define HEADER_H\n" 6149 "code();\n" 6150 "#endif", 6151 Style); 6152 // Test with include guards. 6153 verifyFormat("#ifndef HEADER_H\n" 6154 "#define HEADER_H\n" 6155 "code();\n" 6156 "#endif", 6157 Style); 6158 // Include guards must have a #define with the same variable immediately 6159 // after #ifndef. 6160 verifyFormat("#ifndef NOT_GUARD\n" 6161 "# define FOO\n" 6162 "code();\n" 6163 "#endif", 6164 Style); 6165 6166 // Include guards must cover the entire file. 6167 verifyFormat("code();\n" 6168 "code();\n" 6169 "#ifndef NOT_GUARD\n" 6170 "# define NOT_GUARD\n" 6171 "code();\n" 6172 "#endif", 6173 Style); 6174 verifyFormat("#ifndef NOT_GUARD\n" 6175 "# define NOT_GUARD\n" 6176 "code();\n" 6177 "#endif\n" 6178 "code();", 6179 Style); 6180 // Test with trailing blank lines. 6181 verifyFormat("#ifndef HEADER_H\n" 6182 "#define HEADER_H\n" 6183 "code();\n" 6184 "#endif", 6185 Style); 6186 // Include guards don't have #else. 6187 verifyFormat("#ifndef NOT_GUARD\n" 6188 "# define NOT_GUARD\n" 6189 "code();\n" 6190 "#else\n" 6191 "#endif", 6192 Style); 6193 verifyFormat("#ifndef NOT_GUARD\n" 6194 "# define NOT_GUARD\n" 6195 "code();\n" 6196 "#elif FOO\n" 6197 "#endif", 6198 Style); 6199 // Non-identifier #define after potential include guard. 6200 verifyFormat("#ifndef FOO\n" 6201 "# define 1\n" 6202 "#endif", 6203 Style); 6204 // #if closes past last non-preprocessor line. 6205 verifyFormat("#ifndef FOO\n" 6206 "#define FOO\n" 6207 "#if 1\n" 6208 "int i;\n" 6209 "# define A 0\n" 6210 "#endif\n" 6211 "#endif", 6212 Style); 6213 // Don't crash if there is an #elif directive without a condition. 6214 verifyFormat("#if 1\n" 6215 "int x;\n" 6216 "#elif\n" 6217 "int y;\n" 6218 "#else\n" 6219 "int z;\n" 6220 "#endif", 6221 Style); 6222 // FIXME: This doesn't handle the case where there's code between the 6223 // #ifndef and #define but all other conditions hold. This is because when 6224 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 6225 // previous code line yet, so we can't detect it. 6226 verifyFormat("#ifndef NOT_GUARD\n" 6227 "code();\n" 6228 "#define NOT_GUARD\n" 6229 "code();\n" 6230 "#endif", 6231 "#ifndef NOT_GUARD\n" 6232 "code();\n" 6233 "# define NOT_GUARD\n" 6234 "code();\n" 6235 "#endif", 6236 Style); 6237 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 6238 // be outside an include guard. Examples are #pragma once and 6239 // #pragma GCC diagnostic, or anything else that does not change the meaning 6240 // of the file if it's included multiple times. 6241 verifyFormat("#ifdef WIN32\n" 6242 "# pragma once\n" 6243 "#endif\n" 6244 "#ifndef HEADER_H\n" 6245 "# define HEADER_H\n" 6246 "code();\n" 6247 "#endif", 6248 "#ifdef WIN32\n" 6249 "# pragma once\n" 6250 "#endif\n" 6251 "#ifndef HEADER_H\n" 6252 "#define HEADER_H\n" 6253 "code();\n" 6254 "#endif", 6255 Style); 6256 // FIXME: This does not detect when there is a single non-preprocessor line 6257 // in front of an include-guard-like structure where other conditions hold 6258 // because ScopedLineState hides the line. 6259 verifyFormat("code();\n" 6260 "#ifndef HEADER_H\n" 6261 "#define HEADER_H\n" 6262 "code();\n" 6263 "#endif", 6264 "code();\n" 6265 "#ifndef HEADER_H\n" 6266 "# define HEADER_H\n" 6267 "code();\n" 6268 "#endif", 6269 Style); 6270 // Keep comments aligned with #, otherwise indent comments normally. These 6271 // tests cannot use verifyFormat because messUp manipulates leading 6272 // whitespace. 6273 { 6274 const char *Expected = "" 6275 "void f() {\n" 6276 "#if 1\n" 6277 "// Preprocessor aligned.\n" 6278 "# define A 0\n" 6279 " // Code. Separated by blank line.\n" 6280 "\n" 6281 "# define B 0\n" 6282 " // Code. Not aligned with #\n" 6283 "# define C 0\n" 6284 "#endif"; 6285 const char *ToFormat = "" 6286 "void f() {\n" 6287 "#if 1\n" 6288 "// Preprocessor aligned.\n" 6289 "# define A 0\n" 6290 "// Code. Separated by blank line.\n" 6291 "\n" 6292 "# define B 0\n" 6293 " // Code. Not aligned with #\n" 6294 "# define C 0\n" 6295 "#endif"; 6296 verifyFormat(Expected, ToFormat, Style); 6297 verifyNoChange(Expected, Style); 6298 } 6299 // Keep block quotes aligned. 6300 { 6301 const char *Expected = "" 6302 "void f() {\n" 6303 "#if 1\n" 6304 "/* Preprocessor aligned. */\n" 6305 "# define A 0\n" 6306 " /* Code. Separated by blank line. */\n" 6307 "\n" 6308 "# define B 0\n" 6309 " /* Code. Not aligned with # */\n" 6310 "# define C 0\n" 6311 "#endif"; 6312 const char *ToFormat = "" 6313 "void f() {\n" 6314 "#if 1\n" 6315 "/* Preprocessor aligned. */\n" 6316 "# define A 0\n" 6317 "/* Code. Separated by blank line. */\n" 6318 "\n" 6319 "# define B 0\n" 6320 " /* Code. Not aligned with # */\n" 6321 "# define C 0\n" 6322 "#endif"; 6323 verifyFormat(Expected, ToFormat, Style); 6324 verifyNoChange(Expected, Style); 6325 } 6326 // Keep comments aligned with un-indented directives. 6327 { 6328 const char *Expected = "" 6329 "void f() {\n" 6330 "// Preprocessor aligned.\n" 6331 "#define A 0\n" 6332 " // Code. Separated by blank line.\n" 6333 "\n" 6334 "#define B 0\n" 6335 " // Code. Not aligned with #\n" 6336 "#define C 0\n"; 6337 const char *ToFormat = "" 6338 "void f() {\n" 6339 "// Preprocessor aligned.\n" 6340 "#define A 0\n" 6341 "// Code. Separated by blank line.\n" 6342 "\n" 6343 "#define B 0\n" 6344 " // Code. Not aligned with #\n" 6345 "#define C 0\n"; 6346 verifyFormat(Expected, ToFormat, Style); 6347 verifyNoChange(Expected, Style); 6348 } 6349 // Test AfterHash with tabs. 6350 { 6351 FormatStyle Tabbed = Style; 6352 Tabbed.UseTab = FormatStyle::UT_Always; 6353 Tabbed.IndentWidth = 8; 6354 Tabbed.TabWidth = 8; 6355 verifyFormat("#ifdef _WIN32\n" 6356 "#\tdefine A 0\n" 6357 "#\tifdef VAR2\n" 6358 "#\t\tdefine B 1\n" 6359 "#\t\tinclude <someheader.h>\n" 6360 "#\t\tdefine MACRO \\\n" 6361 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 6362 "#\tendif\n" 6363 "#else\n" 6364 "#\tdefine A 1\n" 6365 "#endif", 6366 Tabbed); 6367 } 6368 6369 // Regression test: Multiline-macro inside include guards. 6370 verifyFormat("#ifndef HEADER_H\n" 6371 "#define HEADER_H\n" 6372 "#define A() \\\n" 6373 " int i; \\\n" 6374 " int j;\n" 6375 "#endif // HEADER_H", 6376 getLLVMStyleWithColumns(20)); 6377 6378 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 6379 // Basic before hash indent tests 6380 verifyFormat("#ifdef _WIN32\n" 6381 " #define A 0\n" 6382 " #ifdef VAR2\n" 6383 " #define B 1\n" 6384 " #include <someheader.h>\n" 6385 " #define MACRO \\\n" 6386 " some_very_long_func_aaaaaaaaaa();\n" 6387 " #endif\n" 6388 "#else\n" 6389 " #define A 1\n" 6390 "#endif", 6391 Style); 6392 verifyFormat("#if A\n" 6393 " #define MACRO \\\n" 6394 " void a(int x) { \\\n" 6395 " b(); \\\n" 6396 " c(); \\\n" 6397 " d(); \\\n" 6398 " e(); \\\n" 6399 " f(); \\\n" 6400 " }\n" 6401 "#endif", 6402 Style); 6403 // Keep comments aligned with indented directives. These 6404 // tests cannot use verifyFormat because messUp manipulates leading 6405 // whitespace. 6406 { 6407 const char *Expected = "void f() {\n" 6408 "// Aligned to preprocessor.\n" 6409 "#if 1\n" 6410 " // Aligned to code.\n" 6411 " int a;\n" 6412 " #if 1\n" 6413 " // Aligned to preprocessor.\n" 6414 " #define A 0\n" 6415 " // Aligned to code.\n" 6416 " int b;\n" 6417 " #endif\n" 6418 "#endif\n" 6419 "}"; 6420 const char *ToFormat = "void f() {\n" 6421 "// Aligned to preprocessor.\n" 6422 "#if 1\n" 6423 "// Aligned to code.\n" 6424 "int a;\n" 6425 "#if 1\n" 6426 "// Aligned to preprocessor.\n" 6427 "#define A 0\n" 6428 "// Aligned to code.\n" 6429 "int b;\n" 6430 "#endif\n" 6431 "#endif\n" 6432 "}"; 6433 verifyFormat(Expected, ToFormat, Style); 6434 verifyNoChange(Expected, Style); 6435 } 6436 { 6437 const char *Expected = "void f() {\n" 6438 "/* Aligned to preprocessor. */\n" 6439 "#if 1\n" 6440 " /* Aligned to code. */\n" 6441 " int a;\n" 6442 " #if 1\n" 6443 " /* Aligned to preprocessor. */\n" 6444 " #define A 0\n" 6445 " /* Aligned to code. */\n" 6446 " int b;\n" 6447 " #endif\n" 6448 "#endif\n" 6449 "}"; 6450 const char *ToFormat = "void f() {\n" 6451 "/* Aligned to preprocessor. */\n" 6452 "#if 1\n" 6453 "/* Aligned to code. */\n" 6454 "int a;\n" 6455 "#if 1\n" 6456 "/* Aligned to preprocessor. */\n" 6457 "#define A 0\n" 6458 "/* Aligned to code. */\n" 6459 "int b;\n" 6460 "#endif\n" 6461 "#endif\n" 6462 "}"; 6463 verifyFormat(Expected, ToFormat, Style); 6464 verifyNoChange(Expected, Style); 6465 } 6466 6467 // Test single comment before preprocessor 6468 verifyFormat("// Comment\n" 6469 "\n" 6470 "#if 1\n" 6471 "#endif", 6472 Style); 6473 } 6474 6475 TEST_F(FormatTest, FormatAlignInsidePreprocessorElseBlock) { 6476 FormatStyle Style = getLLVMStyle(); 6477 Style.AlignConsecutiveAssignments.Enabled = true; 6478 Style.AlignConsecutiveDeclarations.Enabled = true; 6479 6480 // Test with just #if blocks. 6481 verifyFormat("void f1() {\n" 6482 "#if 1\n" 6483 " int foo = 1;\n" 6484 " int foobar = 2;\n" 6485 "#endif\n" 6486 "}\n" 6487 "#if 1\n" 6488 "int baz = 3;\n" 6489 "#endif\n" 6490 "void f2() {\n" 6491 "#if 1\n" 6492 " char *foobarbaz = \"foobarbaz\";\n" 6493 " int quux = 4;\n" 6494 "}", 6495 Style); 6496 6497 // Test with just #else blocks. 6498 verifyFormat("void f1() {\n" 6499 "#if 1\n" 6500 "#else\n" 6501 " int foo = 1;\n" 6502 " int foobar = 2;\n" 6503 "#endif\n" 6504 "}\n" 6505 "#if 1\n" 6506 "#else\n" 6507 "int baz = 3;\n" 6508 "#endif\n" 6509 "void f2() {\n" 6510 "#if 1\n" 6511 "#else\n" 6512 " char *foobarbaz = \"foobarbaz\";\n" 6513 " int quux = 4;\n" 6514 "}", 6515 Style); 6516 verifyFormat("auto foo = [] { return; };\n" 6517 "#if FOO\n" 6518 "#else\n" 6519 "count = bar;\n" 6520 "mbid = bid;\n" 6521 "#endif", 6522 Style); 6523 6524 // Test with a mix of #if and #else blocks. 6525 verifyFormat("void f1() {\n" 6526 "#if 1\n" 6527 "#else\n" 6528 " int foo = 1;\n" 6529 " int foobar = 2;\n" 6530 "#endif\n" 6531 "}\n" 6532 "#if 1\n" 6533 "int baz = 3;\n" 6534 "#endif\n" 6535 "void f2() {\n" 6536 "#if 1\n" 6537 "#else\n" 6538 " // prevent alignment with #else in f1\n" 6539 " char *foobarbaz = \"foobarbaz\";\n" 6540 " int quux = 4;\n" 6541 "}", 6542 Style); 6543 6544 // Test with nested #if and #else blocks. 6545 verifyFormat("void f1() {\n" 6546 "#if 1\n" 6547 "#else\n" 6548 "#if 2\n" 6549 "#else\n" 6550 " int foo = 1;\n" 6551 " int foobar = 2;\n" 6552 "#endif\n" 6553 "#endif\n" 6554 "}\n" 6555 "#if 1\n" 6556 "#else\n" 6557 "#if 2\n" 6558 "int baz = 3;\n" 6559 "#endif\n" 6560 "#endif\n" 6561 "void f2() {\n" 6562 "#if 1\n" 6563 "#if 2\n" 6564 "#else\n" 6565 " // prevent alignment with #else in f1\n" 6566 " char *foobarbaz = \"foobarbaz\";\n" 6567 " int quux = 4;\n" 6568 "#endif\n" 6569 "#endif\n" 6570 "}", 6571 Style); 6572 6573 verifyFormat("#if FOO\n" 6574 "int a = 1;\n" 6575 "#else\n" 6576 "int ab = 2;\n" 6577 "#endif\n" 6578 "#ifdef BAR\n" 6579 "int abc = 3;\n" 6580 "#elifdef BAZ\n" 6581 "int abcd = 4;\n" 6582 "#endif", 6583 Style); 6584 6585 verifyFormat("void f() {\n" 6586 " if (foo) {\n" 6587 "#if FOO\n" 6588 " int a = 1;\n" 6589 "#else\n" 6590 " bool a = true;\n" 6591 "#endif\n" 6592 " int abc = 3;\n" 6593 "#ifndef BAR\n" 6594 " int abcd = 4;\n" 6595 "#elif BAZ\n" 6596 " bool abcd = true;\n" 6597 "#endif\n" 6598 " }\n" 6599 "}", 6600 Style); 6601 6602 verifyFormat("void f() {\n" 6603 "#if FOO\n" 6604 " a = 1;\n" 6605 "#else\n" 6606 " ab = 2;\n" 6607 "#endif\n" 6608 "}\n" 6609 "void g() {\n" 6610 "#if BAR\n" 6611 " abc = 3;\n" 6612 "#elifndef BAZ\n" 6613 " abcd = 4;\n" 6614 "#endif\n" 6615 "}", 6616 Style); 6617 } 6618 6619 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 6620 verifyFormat("{\n" 6621 " {\n" 6622 " a #c;\n" 6623 " }\n" 6624 "}"); 6625 } 6626 6627 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 6628 verifyFormat("#define A \\\n { \\\n {\nint i;", 6629 "#define A { {\nint i;", getLLVMStyleWithColumns(11)); 6630 verifyFormat("#define A \\\n } \\\n }\nint i;", 6631 "#define A } }\nint i;", getLLVMStyleWithColumns(11)); 6632 } 6633 6634 TEST_F(FormatTest, EscapedNewlines) { 6635 FormatStyle Narrow = getLLVMStyleWithColumns(11); 6636 verifyFormat("#define A \\\n int i; \\\n int j;", 6637 "#define A \\\nint i;\\\n int j;", Narrow); 6638 verifyFormat("#define A\n\nint i;", "#define A \\\n\n int i;"); 6639 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();"); 6640 verifyFormat("/* \\ \\ \\\n */", "\\\n/* \\ \\ \\\n */"); 6641 verifyNoChange("<a\n\\\\\n>"); 6642 6643 FormatStyle AlignLeft = getLLVMStyle(); 6644 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 6645 verifyFormat("#define MACRO(x) \\\n" 6646 "private: \\\n" 6647 " int x(int a);", 6648 AlignLeft); 6649 6650 // CRLF line endings 6651 verifyFormat("#define A \\\r\n int i; \\\r\n int j;", 6652 "#define A \\\r\nint i;\\\r\n int j;", Narrow); 6653 verifyFormat("#define A\r\n\r\nint i;", "#define A \\\r\n\r\n int i;"); 6654 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();"); 6655 verifyFormat("/* \\ \\ \\\r\n */", "\\\r\n/* \\ \\ \\\r\n */"); 6656 verifyNoChange("<a\r\n\\\\\r\n>"); 6657 verifyFormat("#define MACRO(x) \\\r\n" 6658 "private: \\\r\n" 6659 " int x(int a);", 6660 AlignLeft); 6661 6662 constexpr StringRef Code{"#define A \\\n" 6663 " int a123; \\\n" 6664 " int a; \\\n" 6665 " int a1234;"}; 6666 verifyFormat(Code, AlignLeft); 6667 6668 constexpr StringRef Code2{"#define A \\\n" 6669 " int a123; \\\n" 6670 " int a; \\\n" 6671 " int a1234;"}; 6672 auto LastLine = getLLVMStyle(); 6673 LastLine.AlignEscapedNewlines = FormatStyle::ENAS_LeftWithLastLine; 6674 verifyFormat(Code2, LastLine); 6675 6676 LastLine.ColumnLimit = 13; 6677 verifyFormat(Code, LastLine); 6678 6679 LastLine.ColumnLimit = 0; 6680 verifyFormat(Code2, LastLine); 6681 6682 FormatStyle DontAlign = getLLVMStyle(); 6683 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 6684 DontAlign.MaxEmptyLinesToKeep = 3; 6685 // FIXME: can't use verifyFormat here because the newline before 6686 // "public:" is not inserted the first time it's reformatted 6687 verifyNoChange("#define A \\\n" 6688 " class Foo { \\\n" 6689 " void bar(); \\\n" 6690 "\\\n" 6691 "\\\n" 6692 "\\\n" 6693 " public: \\\n" 6694 " void baz(); \\\n" 6695 " };", 6696 DontAlign); 6697 } 6698 6699 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 6700 verifyFormat("#define A \\\n" 6701 " int v( \\\n" 6702 " a); \\\n" 6703 " int i;", 6704 getLLVMStyleWithColumns(11)); 6705 } 6706 6707 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 6708 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 6709 " \\\n" 6710 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 6711 "\n" 6712 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 6713 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);", 6714 " #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 6715 "\\\n" 6716 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 6717 " \n" 6718 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 6719 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);"); 6720 } 6721 6722 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 6723 verifyFormat("int\n" 6724 "#define A\n" 6725 " a;", 6726 "int\n#define A\na;"); 6727 verifyFormat("functionCallTo(\n" 6728 " someOtherFunction(\n" 6729 " withSomeParameters, whichInSequence,\n" 6730 " areLongerThanALine(andAnotherCall,\n" 6731 "#define A B\n" 6732 " withMoreParamters,\n" 6733 " whichStronglyInfluenceTheLayout),\n" 6734 " andMoreParameters),\n" 6735 " trailing);", 6736 getLLVMStyleWithColumns(69)); 6737 verifyFormat("Foo::Foo()\n" 6738 "#ifdef BAR\n" 6739 " : baz(0)\n" 6740 "#endif\n" 6741 "{\n" 6742 "}"); 6743 verifyFormat("void f() {\n" 6744 " if (true)\n" 6745 "#ifdef A\n" 6746 " f(42);\n" 6747 " x();\n" 6748 "#else\n" 6749 " g();\n" 6750 " x();\n" 6751 "#endif\n" 6752 "}"); 6753 verifyFormat("void f(param1, param2,\n" 6754 " param3,\n" 6755 "#ifdef A\n" 6756 " param4(param5,\n" 6757 "#ifdef A1\n" 6758 " param6,\n" 6759 "#ifdef A2\n" 6760 " param7),\n" 6761 "#else\n" 6762 " param8),\n" 6763 " param9,\n" 6764 "#endif\n" 6765 " param10,\n" 6766 "#endif\n" 6767 " param11)\n" 6768 "#else\n" 6769 " param12)\n" 6770 "#endif\n" 6771 "{\n" 6772 " x();\n" 6773 "}", 6774 getLLVMStyleWithColumns(28)); 6775 verifyFormat("#if 1\n" 6776 "int i;"); 6777 verifyFormat("#if 1\n" 6778 "#endif\n" 6779 "#if 1\n" 6780 "#else\n" 6781 "#endif"); 6782 verifyFormat("DEBUG({\n" 6783 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6784 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 6785 "});\n" 6786 "#if a\n" 6787 "#else\n" 6788 "#endif"); 6789 6790 verifyIncompleteFormat("void f(\n" 6791 "#if A\n" 6792 ");\n" 6793 "#else\n" 6794 "#endif"); 6795 6796 // Verify that indentation is correct when there is an `#if 0` with an 6797 // `#else`. 6798 verifyFormat("#if 0\n" 6799 "{\n" 6800 "#else\n" 6801 "{\n" 6802 "#endif\n" 6803 " x;\n" 6804 "}"); 6805 6806 verifyFormat("#if 0\n" 6807 "#endif\n" 6808 "#if X\n" 6809 "int something_fairly_long; // Align here please\n" 6810 "#endif // Should be aligned"); 6811 6812 verifyFormat("#if 0\n" 6813 "#endif\n" 6814 "#if X\n" 6815 "#else // Align\n" 6816 ";\n" 6817 "#endif // Align"); 6818 6819 verifyFormat("void SomeFunction(int param1,\n" 6820 " template <\n" 6821 "#ifdef A\n" 6822 "#if 0\n" 6823 "#endif\n" 6824 " MyType<Some>>\n" 6825 "#else\n" 6826 " Type1, Type2>\n" 6827 "#endif\n" 6828 " param2,\n" 6829 " param3) {\n" 6830 " f();\n" 6831 "}"); 6832 } 6833 6834 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 6835 verifyFormat("#endif\n" 6836 "#if B"); 6837 } 6838 6839 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 6840 FormatStyle SingleLine = getLLVMStyle(); 6841 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 6842 verifyFormat("#if 0\n" 6843 "#elif 1\n" 6844 "#endif\n" 6845 "void foo() {\n" 6846 " if (test) foo2();\n" 6847 "}", 6848 SingleLine); 6849 } 6850 6851 TEST_F(FormatTest, LayoutBlockInsideParens) { 6852 verifyFormat("functionCall({ int i; });"); 6853 verifyFormat("functionCall({\n" 6854 " int i;\n" 6855 " int j;\n" 6856 "});"); 6857 verifyFormat("functionCall(\n" 6858 " {\n" 6859 " int i;\n" 6860 " int j;\n" 6861 " },\n" 6862 " aaaa, bbbb, cccc);"); 6863 verifyFormat("functionA(functionB({\n" 6864 " int i;\n" 6865 " int j;\n" 6866 " }),\n" 6867 " aaaa, bbbb, cccc);"); 6868 verifyFormat("functionCall(\n" 6869 " {\n" 6870 " int i;\n" 6871 " int j;\n" 6872 " },\n" 6873 " aaaa, bbbb, // comment\n" 6874 " cccc);"); 6875 verifyFormat("functionA(functionB({\n" 6876 " int i;\n" 6877 " int j;\n" 6878 " }),\n" 6879 " aaaa, bbbb, // comment\n" 6880 " cccc);"); 6881 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 6882 verifyFormat("functionCall(aaaa, bbbb, {\n" 6883 " int i;\n" 6884 " int j;\n" 6885 "});"); 6886 verifyFormat( 6887 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 6888 " {\n" 6889 " int i; // break\n" 6890 " },\n" 6891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6892 " ccccccccccccccccc));"); 6893 verifyFormat("DEBUG({\n" 6894 " if (a)\n" 6895 " f();\n" 6896 "});"); 6897 } 6898 6899 TEST_F(FormatTest, LayoutBlockInsideStatement) { 6900 verifyFormat("SOME_MACRO { int i; }\n" 6901 "int i;", 6902 " SOME_MACRO {int i;} int i;"); 6903 } 6904 6905 TEST_F(FormatTest, LayoutNestedBlocks) { 6906 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 6907 " struct s {\n" 6908 " int i;\n" 6909 " };\n" 6910 " s kBitsToOs[] = {{10}};\n" 6911 " for (int i = 0; i < 10; ++i)\n" 6912 " return;\n" 6913 "}"); 6914 verifyFormat("call(parameter, {\n" 6915 " something();\n" 6916 " // Comment using all columns.\n" 6917 " somethingelse();\n" 6918 "});", 6919 getLLVMStyleWithColumns(40)); 6920 verifyFormat("DEBUG( //\n" 6921 " { f(); }, a);"); 6922 verifyFormat("DEBUG( //\n" 6923 " {\n" 6924 " f(); //\n" 6925 " },\n" 6926 " a);"); 6927 6928 verifyFormat("call(parameter, {\n" 6929 " something();\n" 6930 " // Comment too\n" 6931 " // looooooooooong.\n" 6932 " somethingElse();\n" 6933 "});", 6934 "call(parameter, {\n" 6935 " something();\n" 6936 " // Comment too looooooooooong.\n" 6937 " somethingElse();\n" 6938 "});", 6939 getLLVMStyleWithColumns(29)); 6940 verifyFormat("DEBUG({ int i; });", "DEBUG({ int i; });"); 6941 verifyFormat("DEBUG({ // comment\n" 6942 " int i;\n" 6943 "});", 6944 "DEBUG({ // comment\n" 6945 "int i;\n" 6946 "});"); 6947 verifyFormat("DEBUG({\n" 6948 " int i;\n" 6949 "\n" 6950 " // comment\n" 6951 " int j;\n" 6952 "});", 6953 "DEBUG({\n" 6954 " int i;\n" 6955 "\n" 6956 " // comment\n" 6957 " int j;\n" 6958 "});"); 6959 6960 verifyFormat("DEBUG({\n" 6961 " if (a)\n" 6962 " return;\n" 6963 "});"); 6964 verifyGoogleFormat("DEBUG({\n" 6965 " if (a) return;\n" 6966 "});"); 6967 FormatStyle Style = getGoogleStyle(); 6968 Style.ColumnLimit = 45; 6969 verifyFormat("Debug(\n" 6970 " aaaaa,\n" 6971 " {\n" 6972 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 6973 " },\n" 6974 " a);", 6975 Style); 6976 6977 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 6978 6979 verifyNoCrash("^{v^{a}}"); 6980 } 6981 6982 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 6983 verifyFormat("#define MACRO() \\\n" 6984 " Debug(aaa, /* force line break */ \\\n" 6985 " { \\\n" 6986 " int i; \\\n" 6987 " int j; \\\n" 6988 " })", 6989 "#define MACRO() Debug(aaa, /* force line break */ \\\n" 6990 " { int i; int j; })", 6991 getGoogleStyle()); 6992 6993 verifyFormat("#define A \\\n" 6994 " [] { \\\n" 6995 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 6996 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 6997 " }", 6998 "#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 6999 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 7000 getGoogleStyle()); 7001 } 7002 7003 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 7004 verifyFormat("enum E {};"); 7005 verifyFormat("enum E {}"); 7006 FormatStyle Style = getLLVMStyle(); 7007 Style.SpaceInEmptyBlock = true; 7008 verifyFormat("void f() { }", "void f() {}", Style); 7009 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 7010 verifyFormat("{ }", Style); 7011 verifyFormat("while (true) { }", "while (true) {}", Style); 7012 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7013 Style.BraceWrapping.BeforeElse = false; 7014 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 7015 verifyFormat("if (a)\n" 7016 "{\n" 7017 "} else if (b)\n" 7018 "{\n" 7019 "} else\n" 7020 "{ }", 7021 Style); 7022 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 7023 verifyFormat("if (a) {\n" 7024 "} else if (b) {\n" 7025 "} else {\n" 7026 "}", 7027 Style); 7028 Style.BraceWrapping.BeforeElse = true; 7029 verifyFormat("if (a) { }\n" 7030 "else if (b) { }\n" 7031 "else { }", 7032 Style); 7033 7034 Style = getLLVMStyle(FormatStyle::LK_CSharp); 7035 Style.SpaceInEmptyBlock = true; 7036 verifyFormat("Event += () => { };", Style); 7037 } 7038 7039 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 7040 FormatStyle Style = getLLVMStyle(); 7041 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 7042 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 7043 verifyFormat("FOO_BEGIN\n" 7044 " FOO_ENTRY\n" 7045 "FOO_END", 7046 Style); 7047 verifyFormat("FOO_BEGIN\n" 7048 " NESTED_FOO_BEGIN\n" 7049 " NESTED_FOO_ENTRY\n" 7050 " NESTED_FOO_END\n" 7051 "FOO_END", 7052 Style); 7053 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 7054 " int x;\n" 7055 " x = 1;\n" 7056 "FOO_END(Baz)", 7057 Style); 7058 7059 Style.RemoveBracesLLVM = true; 7060 verifyNoCrash("for (;;)\n" 7061 " FOO_BEGIN\n" 7062 " foo();\n" 7063 " FOO_END", 7064 Style); 7065 } 7066 7067 //===----------------------------------------------------------------------===// 7068 // Line break tests. 7069 //===----------------------------------------------------------------------===// 7070 7071 TEST_F(FormatTest, PreventConfusingIndents) { 7072 verifyFormat( 7073 "void f() {\n" 7074 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 7075 " parameter, parameter, parameter)),\n" 7076 " SecondLongCall(parameter));\n" 7077 "}"); 7078 verifyFormat( 7079 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7080 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7081 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7082 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 7083 verifyFormat( 7084 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7085 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 7086 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 7087 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 7088 verifyFormat( 7089 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7090 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 7091 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 7092 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 7093 verifyFormat("int a = bbbb && ccc &&\n" 7094 " fffff(\n" 7095 "#define A Just forcing a new line\n" 7096 " ddd);"); 7097 } 7098 7099 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 7100 verifyFormat( 7101 "bool aaaaaaa =\n" 7102 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 7103 " bbbbbbbb();"); 7104 verifyFormat( 7105 "bool aaaaaaa =\n" 7106 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 7107 " bbbbbbbb();"); 7108 7109 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 7110 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 7111 " ccccccccc == ddddddddddd;"); 7112 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 7113 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 7114 " ccccccccc == ddddddddddd;"); 7115 verifyFormat( 7116 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 7117 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 7118 " ccccccccc == ddddddddddd;"); 7119 7120 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 7121 " aaaaaa) &&\n" 7122 " bbbbbb && cccccc;"); 7123 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 7124 " aaaaaa) >>\n" 7125 " bbbbbb;"); 7126 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 7127 " SourceMgr.getSpellingColumnNumber(\n" 7128 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 7129 " 1);"); 7130 7131 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7132 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 7133 " cccccc) {\n}"); 7134 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7135 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 7136 " cccccc) {\n}"); 7137 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7138 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 7139 " cccccc) {\n}"); 7140 verifyFormat("b = a &&\n" 7141 " // Comment\n" 7142 " b.c && d;"); 7143 7144 // If the LHS of a comparison is not a binary expression itself, the 7145 // additional linebreak confuses many people. 7146 verifyFormat( 7147 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 7149 "}"); 7150 verifyFormat( 7151 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7152 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 7153 "}"); 7154 verifyFormat( 7155 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 7156 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 7157 "}"); 7158 verifyFormat( 7159 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7160 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 7161 "}"); 7162 // Even explicit parentheses stress the precedence enough to make the 7163 // additional break unnecessary. 7164 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7165 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 7166 "}"); 7167 // This cases is borderline, but with the indentation it is still readable. 7168 verifyFormat( 7169 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7170 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7171 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 7172 "}", 7173 getLLVMStyleWithColumns(75)); 7174 7175 // If the LHS is a binary expression, we should still use the additional break 7176 // as otherwise the formatting hides the operator precedence. 7177 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 7179 " 5) {\n" 7180 "}"); 7181 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7182 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 7183 " 5) {\n" 7184 "}"); 7185 7186 FormatStyle OnePerLine = getLLVMStyle(); 7187 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; 7188 verifyFormat( 7189 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7191 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 7192 OnePerLine); 7193 7194 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 7195 " .aaa(aaaaaaaaaaaaa) *\n" 7196 " aaaaaaa +\n" 7197 " aaaaaaa;", 7198 getLLVMStyleWithColumns(40)); 7199 } 7200 7201 TEST_F(FormatTest, ExpressionIndentation) { 7202 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7203 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 7205 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 7206 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7207 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 7208 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 7209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 7210 " ccccccccccccccccccccccccccccccccccccccccc;"); 7211 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 7212 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 7214 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 7215 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7216 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 7217 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 7218 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 7219 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 7220 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 7221 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7222 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 7223 verifyFormat("if () {\n" 7224 "} else if (aaaaa && bbbbb > // break\n" 7225 " ccccc) {\n" 7226 "}"); 7227 verifyFormat("if () {\n" 7228 "} else if constexpr (aaaaa && bbbbb > // break\n" 7229 " ccccc) {\n" 7230 "}"); 7231 verifyFormat("if () {\n" 7232 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 7233 " ccccc) {\n" 7234 "}"); 7235 verifyFormat("if () {\n" 7236 "} else if (aaaaa &&\n" 7237 " bbbbb > // break\n" 7238 " ccccc &&\n" 7239 " ddddd) {\n" 7240 "}"); 7241 7242 // Presence of a trailing comment used to change indentation of b. 7243 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 7244 " b;\n" 7245 "return aaaaaaaaaaaaaaaaaaa +\n" 7246 " b; //", 7247 getLLVMStyleWithColumns(30)); 7248 } 7249 7250 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 7251 // Not sure what the best system is here. Like this, the LHS can be found 7252 // immediately above an operator (everything with the same or a higher 7253 // indent). The RHS is aligned right of the operator and so compasses 7254 // everything until something with the same indent as the operator is found. 7255 // FIXME: Is this a good system? 7256 FormatStyle Style = getLLVMStyle(); 7257 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 7258 verifyFormat( 7259 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7260 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7261 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7262 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7263 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7264 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7265 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7266 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7267 " > ccccccccccccccccccccccccccccccccccccccccc;", 7268 Style); 7269 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7270 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7271 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7272 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 7273 Style); 7274 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7275 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7276 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7277 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 7278 Style); 7279 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7280 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7281 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7282 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 7283 Style); 7284 verifyFormat("if () {\n" 7285 "} else if (aaaaa\n" 7286 " && bbbbb // break\n" 7287 " > ccccc) {\n" 7288 "}", 7289 Style); 7290 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7291 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7292 Style); 7293 verifyFormat("return (a)\n" 7294 " // comment\n" 7295 " + b;", 7296 Style); 7297 verifyFormat( 7298 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7299 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7300 " + cc;", 7301 Style); 7302 7303 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7304 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7305 Style); 7306 7307 // Forced by comments. 7308 verifyFormat( 7309 "unsigned ContentSize =\n" 7310 " sizeof(int16_t) // DWARF ARange version number\n" 7311 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 7312 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 7313 " + sizeof(int8_t); // Segment Size (in bytes)"); 7314 7315 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 7316 " == boost::fusion::at_c<1>(iiii).second;", 7317 Style); 7318 7319 Style.ColumnLimit = 60; 7320 verifyFormat("zzzzzzzzzz\n" 7321 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7322 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 7323 Style); 7324 7325 Style.ColumnLimit = 80; 7326 Style.IndentWidth = 4; 7327 Style.TabWidth = 4; 7328 Style.UseTab = FormatStyle::UT_Always; 7329 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7330 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7331 verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 7332 "\t&& (someOtherLongishConditionPart1\n" 7333 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 7334 "return someVeryVeryLongConditionThatBarelyFitsOnALine && " 7335 "(someOtherLongishConditionPart1 || " 7336 "someOtherEvenLongerNestedConditionPart2);", 7337 Style); 7338 7339 Style = getLLVMStyleWithColumns(20); 7340 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7341 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; 7342 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 7343 Style.ContinuationIndentWidth = 2; 7344 verifyFormat("struct Foo {\n" 7345 " Foo(\n" 7346 " int arg1,\n" 7347 " int arg2)\n" 7348 " : Base(\n" 7349 " arg1,\n" 7350 " arg2) {}\n" 7351 "};", 7352 Style); 7353 verifyFormat("return abc\n" 7354 " ? foo(\n" 7355 " a,\n" 7356 " b,\n" 7357 " bar(\n" 7358 " abc))\n" 7359 " : g(abc);", 7360 Style); 7361 } 7362 7363 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 7364 FormatStyle Style = getLLVMStyle(); 7365 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 7366 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 7367 7368 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7369 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7370 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7371 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7372 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7373 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7374 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7375 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7376 " > ccccccccccccccccccccccccccccccccccccccccc;", 7377 Style); 7378 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7379 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7380 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7381 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 7382 Style); 7383 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7384 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7385 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7386 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 7387 Style); 7388 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7389 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7390 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7391 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 7392 Style); 7393 verifyFormat("if () {\n" 7394 "} else if (aaaaa\n" 7395 " && bbbbb // break\n" 7396 " > ccccc) {\n" 7397 "}", 7398 Style); 7399 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7400 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7401 Style); 7402 verifyFormat("return (a)\n" 7403 " // comment\n" 7404 " + b;", 7405 Style); 7406 verifyFormat( 7407 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7408 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7409 " + cc;", 7410 Style); 7411 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7412 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 7413 " : 3333333333333333;", 7414 Style); 7415 verifyFormat( 7416 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7417 " : ccccccccccccccc ? dddddddddddddddddd\n" 7418 " : eeeeeeeeeeeeeeeeee)\n" 7419 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 7420 " : 3333333333333333;", 7421 Style); 7422 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7423 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7424 Style); 7425 7426 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 7427 " == boost::fusion::at_c<1>(iiii).second;", 7428 Style); 7429 7430 Style.ColumnLimit = 60; 7431 verifyFormat("zzzzzzzzzzzzz\n" 7432 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7433 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 7434 Style); 7435 7436 // Forced by comments. 7437 Style.ColumnLimit = 80; 7438 verifyFormat( 7439 "unsigned ContentSize\n" 7440 " = sizeof(int16_t) // DWARF ARange version number\n" 7441 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 7442 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 7443 " + sizeof(int8_t); // Segment Size (in bytes)", 7444 Style); 7445 7446 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 7447 verifyFormat( 7448 "unsigned ContentSize =\n" 7449 " sizeof(int16_t) // DWARF ARange version number\n" 7450 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 7451 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 7452 " + sizeof(int8_t); // Segment Size (in bytes)", 7453 Style); 7454 7455 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 7456 verifyFormat( 7457 "unsigned ContentSize =\n" 7458 " sizeof(int16_t) // DWARF ARange version number\n" 7459 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 7460 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 7461 " + sizeof(int8_t); // Segment Size (in bytes)", 7462 Style); 7463 } 7464 7465 TEST_F(FormatTest, EnforcedOperatorWraps) { 7466 // Here we'd like to wrap after the || operators, but a comment is forcing an 7467 // earlier wrap. 7468 verifyFormat("bool x = aaaaa //\n" 7469 " || bbbbb\n" 7470 " //\n" 7471 " || cccc;"); 7472 } 7473 7474 TEST_F(FormatTest, NoOperandAlignment) { 7475 FormatStyle Style = getLLVMStyle(); 7476 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7477 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 7478 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7479 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7480 Style); 7481 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 7482 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7483 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7484 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7485 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7486 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7487 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7488 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7489 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7490 " > ccccccccccccccccccccccccccccccccccccccccc;", 7491 Style); 7492 7493 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7494 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7495 " + cc;", 7496 Style); 7497 verifyFormat("int a = aa\n" 7498 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 7499 " * cccccccccccccccccccccccccccccccccccc;", 7500 Style); 7501 7502 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7503 verifyFormat("return (a > b\n" 7504 " // comment1\n" 7505 " // comment2\n" 7506 " || c);", 7507 Style); 7508 } 7509 7510 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 7511 FormatStyle Style = getLLVMStyle(); 7512 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 7513 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7514 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7515 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 7516 Style); 7517 } 7518 7519 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 7520 FormatStyle Style = getLLVMStyleWithColumns(40); 7521 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 7522 Style.BinPackArguments = false; 7523 verifyFormat("void test() {\n" 7524 " someFunction(\n" 7525 " this + argument + is + quite\n" 7526 " + long + so + it + gets + wrapped\n" 7527 " + but + remains + bin - packed);\n" 7528 "}", 7529 Style); 7530 verifyFormat("void test() {\n" 7531 " someFunction(arg1,\n" 7532 " this + argument + is\n" 7533 " + quite + long + so\n" 7534 " + it + gets + wrapped\n" 7535 " + but + remains + bin\n" 7536 " - packed,\n" 7537 " arg3);\n" 7538 "}", 7539 Style); 7540 verifyFormat("void test() {\n" 7541 " someFunction(\n" 7542 " arg1,\n" 7543 " this + argument + has\n" 7544 " + anotherFunc(nested,\n" 7545 " calls + whose\n" 7546 " + arguments\n" 7547 " + are + also\n" 7548 " + wrapped,\n" 7549 " in + addition)\n" 7550 " + to + being + bin - packed,\n" 7551 " arg3);\n" 7552 "}", 7553 Style); 7554 7555 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 7556 verifyFormat("void test() {\n" 7557 " someFunction(\n" 7558 " arg1,\n" 7559 " this + argument + has +\n" 7560 " anotherFunc(nested,\n" 7561 " calls + whose +\n" 7562 " arguments +\n" 7563 " are + also +\n" 7564 " wrapped,\n" 7565 " in + addition) +\n" 7566 " to + being + bin - packed,\n" 7567 " arg3);\n" 7568 "}", 7569 Style); 7570 } 7571 7572 TEST_F(FormatTest, BreakBinaryOperatorsInPresenceOfTemplates) { 7573 auto Style = getLLVMStyleWithColumns(45); 7574 EXPECT_EQ(Style.BreakBeforeBinaryOperators, FormatStyle::BOS_None); 7575 verifyFormat("bool b =\n" 7576 " is_default_constructible_v<hash<T>> and\n" 7577 " is_copy_constructible_v<hash<T>> and\n" 7578 " is_move_constructible_v<hash<T>> and\n" 7579 " is_copy_assignable_v<hash<T>> and\n" 7580 " is_move_assignable_v<hash<T>> and\n" 7581 " is_destructible_v<hash<T>> and\n" 7582 " is_swappable_v<hash<T>> and\n" 7583 " is_callable_v<hash<T>(T)>;", 7584 Style); 7585 7586 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 7587 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n" 7588 " and is_copy_constructible_v<hash<T>>\n" 7589 " and is_move_constructible_v<hash<T>>\n" 7590 " and is_copy_assignable_v<hash<T>>\n" 7591 " and is_move_assignable_v<hash<T>>\n" 7592 " and is_destructible_v<hash<T>>\n" 7593 " and is_swappable_v<hash<T>>\n" 7594 " and is_callable_v<hash<T>(T)>;", 7595 Style); 7596 7597 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 7598 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n" 7599 " and is_copy_constructible_v<hash<T>>\n" 7600 " and is_move_constructible_v<hash<T>>\n" 7601 " and is_copy_assignable_v<hash<T>>\n" 7602 " and is_move_assignable_v<hash<T>>\n" 7603 " and is_destructible_v<hash<T>>\n" 7604 " and is_swappable_v<hash<T>>\n" 7605 " and is_callable_v<hash<T>(T)>;", 7606 Style); 7607 } 7608 7609 TEST_F(FormatTest, ConstructorInitializers) { 7610 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 7611 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 7612 getLLVMStyleWithColumns(45)); 7613 verifyFormat("Constructor()\n" 7614 " : Inttializer(FitsOnTheLine) {}", 7615 getLLVMStyleWithColumns(44)); 7616 verifyFormat("Constructor()\n" 7617 " : Inttializer(FitsOnTheLine) {}", 7618 getLLVMStyleWithColumns(43)); 7619 7620 verifyFormat("template <typename T>\n" 7621 "Constructor() : Initializer(FitsOnTheLine) {}", 7622 getLLVMStyleWithColumns(45)); 7623 7624 verifyFormat( 7625 "SomeClass::Constructor()\n" 7626 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 7627 7628 verifyFormat( 7629 "SomeClass::Constructor()\n" 7630 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 7631 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 7632 verifyFormat( 7633 "SomeClass::Constructor()\n" 7634 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7635 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 7636 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7637 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7638 " : aaaaaaaaaa(aaaaaa) {}"); 7639 7640 verifyFormat("Constructor()\n" 7641 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7642 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7643 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7644 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 7645 7646 verifyFormat("Constructor()\n" 7647 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7648 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 7649 7650 verifyFormat("Constructor(int Parameter = 0)\n" 7651 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 7652 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 7653 verifyFormat("Constructor()\n" 7654 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 7655 "}", 7656 getLLVMStyleWithColumns(60)); 7657 verifyFormat("Constructor()\n" 7658 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7659 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 7660 7661 // Here a line could be saved by splitting the second initializer onto two 7662 // lines, but that is not desirable. 7663 verifyFormat("Constructor()\n" 7664 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 7665 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 7666 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 7667 7668 FormatStyle OnePerLine = getLLVMStyle(); 7669 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never; 7670 verifyFormat("MyClass::MyClass()\n" 7671 " : a(a),\n" 7672 " b(b),\n" 7673 " c(c) {}", 7674 OnePerLine); 7675 verifyFormat("MyClass::MyClass()\n" 7676 " : a(a), // comment\n" 7677 " b(b),\n" 7678 " c(c) {}", 7679 OnePerLine); 7680 verifyFormat("MyClass::MyClass(int a)\n" 7681 " : b(a), // comment\n" 7682 " c(a + 1) { // lined up\n" 7683 "}", 7684 OnePerLine); 7685 verifyFormat("Constructor()\n" 7686 " : a(b, b, b) {}", 7687 OnePerLine); 7688 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7689 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 7690 verifyFormat("SomeClass::Constructor()\n" 7691 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 7692 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 7693 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 7694 OnePerLine); 7695 verifyFormat("SomeClass::Constructor()\n" 7696 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 7697 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 7698 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 7699 OnePerLine); 7700 verifyFormat("MyClass::MyClass(int var)\n" 7701 " : some_var_(var), // 4 space indent\n" 7702 " some_other_var_(var + 1) { // lined up\n" 7703 "}", 7704 OnePerLine); 7705 verifyFormat("Constructor()\n" 7706 " : aaaaa(aaaaaa),\n" 7707 " aaaaa(aaaaaa),\n" 7708 " aaaaa(aaaaaa),\n" 7709 " aaaaa(aaaaaa),\n" 7710 " aaaaa(aaaaaa) {}", 7711 OnePerLine); 7712 verifyFormat("Constructor()\n" 7713 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 7714 " aaaaaaaaaaaaaaaaaaaaaa) {}", 7715 OnePerLine); 7716 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; 7717 verifyFormat( 7718 "Constructor()\n" 7719 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7720 " aaaaaaaaaaa().aaa(),\n" 7721 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7722 OnePerLine); 7723 OnePerLine.ColumnLimit = 60; 7724 verifyFormat("Constructor()\n" 7725 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 7726 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 7727 OnePerLine); 7728 7729 verifyFormat("Constructor()\n" 7730 " : // Comment forcing unwanted break.\n" 7731 " aaaa(aaaa) {}", 7732 "Constructor() :\n" 7733 " // Comment forcing unwanted break.\n" 7734 " aaaa(aaaa) {}"); 7735 } 7736 7737 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 7738 FormatStyle Style = getLLVMStyleWithColumns(60); 7739 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 7740 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; 7741 7742 for (int i = 0; i < 4; ++i) { 7743 // Test all combinations of parameters that should not have an effect. 7744 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 7745 Style.AllowAllArgumentsOnNextLine = i & 2; 7746 7747 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7748 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 7749 verifyFormat("Constructor()\n" 7750 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7751 Style); 7752 verifyFormat("Constructor() : a(a), b(b) {}", Style); 7753 7754 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7755 verifyFormat("Constructor()\n" 7756 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 7757 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 7758 Style); 7759 verifyFormat("Constructor() : a(a), b(b) {}", Style); 7760 7761 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 7762 verifyFormat("Constructor()\n" 7763 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7764 Style); 7765 verifyFormat("Constructor()\n" 7766 " : a(a), b(b) {}", 7767 Style); 7768 verifyFormat("Constructor()\n" 7769 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 7770 " , bbbbbbbbbbbbbbbbbbbbb(b)\n" 7771 " , cccccccccccccccccccccc(c) {}", 7772 Style); 7773 7774 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 7775 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7776 verifyFormat("Constructor()\n" 7777 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7778 Style); 7779 7780 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7781 verifyFormat("Constructor()\n" 7782 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 7783 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 7784 Style); 7785 7786 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 7787 verifyFormat("Constructor()\n" 7788 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7789 Style); 7790 verifyFormat("Constructor()\n" 7791 " : a(a), b(b) {}", 7792 Style); 7793 verifyFormat("Constructor()\n" 7794 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 7795 " bbbbbbbbbbbbbbbbbbbbb(b),\n" 7796 " cccccccccccccccccccccc(c) {}", 7797 Style); 7798 7799 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 7800 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7801 verifyFormat("Constructor() :\n" 7802 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7803 Style); 7804 7805 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7806 verifyFormat("Constructor() :\n" 7807 " aaaaaaaaaaaaaaaaaa(a),\n" 7808 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 7809 Style); 7810 7811 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 7812 verifyFormat("Constructor() :\n" 7813 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7814 Style); 7815 verifyFormat("Constructor() :\n" 7816 " a(a), b(b) {}", 7817 Style); 7818 verifyFormat("Constructor() :\n" 7819 " aaaaaaaaaaaaaaaaaaaa(a),\n" 7820 " bbbbbbbbbbbbbbbbbbbbb(b),\n" 7821 " cccccccccccccccccccccc(c) {}", 7822 Style); 7823 } 7824 7825 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 7826 // AllowAllConstructorInitializersOnNextLine in all 7827 // BreakConstructorInitializers modes 7828 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 7829 Style.AllowAllParametersOfDeclarationOnNextLine = true; 7830 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7831 verifyFormat("SomeClassWithALongName::Constructor(\n" 7832 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 7833 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 7834 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 7835 Style); 7836 7837 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7838 verifyFormat("SomeClassWithALongName::Constructor(\n" 7839 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7840 " int bbbbbbbbbbbbb,\n" 7841 " int cccccccccccccccc)\n" 7842 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7843 Style); 7844 7845 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 7846 verifyFormat("SomeClassWithALongName::Constructor(\n" 7847 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7848 " int bbbbbbbbbbbbb,\n" 7849 " int cccccccccccccccc)\n" 7850 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7851 Style); 7852 7853 Style.AllowAllParametersOfDeclarationOnNextLine = false; 7854 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7855 verifyFormat("SomeClassWithALongName::Constructor(\n" 7856 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7857 " int bbbbbbbbbbbbb)\n" 7858 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 7859 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 7860 Style); 7861 7862 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 7863 7864 Style.AllowAllParametersOfDeclarationOnNextLine = true; 7865 verifyFormat("SomeClassWithALongName::Constructor(\n" 7866 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 7867 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 7868 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 7869 Style); 7870 7871 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7872 verifyFormat("SomeClassWithALongName::Constructor(\n" 7873 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7874 " int bbbbbbbbbbbbb,\n" 7875 " int cccccccccccccccc)\n" 7876 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7877 Style); 7878 7879 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 7880 verifyFormat("SomeClassWithALongName::Constructor(\n" 7881 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7882 " int bbbbbbbbbbbbb,\n" 7883 " int cccccccccccccccc)\n" 7884 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7885 Style); 7886 7887 Style.AllowAllParametersOfDeclarationOnNextLine = false; 7888 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7889 verifyFormat("SomeClassWithALongName::Constructor(\n" 7890 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7891 " int bbbbbbbbbbbbb)\n" 7892 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 7893 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 7894 Style); 7895 7896 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 7897 Style.AllowAllParametersOfDeclarationOnNextLine = true; 7898 verifyFormat("SomeClassWithALongName::Constructor(\n" 7899 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 7900 " aaaaaaaaaaaaaaaaaaaa(a),\n" 7901 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 7902 Style); 7903 7904 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 7905 verifyFormat("SomeClassWithALongName::Constructor(\n" 7906 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7907 " int bbbbbbbbbbbbb,\n" 7908 " int cccccccccccccccc) :\n" 7909 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7910 Style); 7911 7912 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 7913 verifyFormat("SomeClassWithALongName::Constructor(\n" 7914 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7915 " int bbbbbbbbbbbbb,\n" 7916 " int cccccccccccccccc) :\n" 7917 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 7918 Style); 7919 7920 Style.AllowAllParametersOfDeclarationOnNextLine = false; 7921 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7922 verifyFormat("SomeClassWithALongName::Constructor(\n" 7923 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7924 " int bbbbbbbbbbbbb) :\n" 7925 " aaaaaaaaaaaaaaaaaaaa(a),\n" 7926 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 7927 Style); 7928 7929 Style = getLLVMStyleWithColumns(0); 7930 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 7931 verifyFormat("Foo(Bar bar, Baz baz) : bar(bar), baz(baz) {}", Style); 7932 verifyNoChange("Foo(Bar bar, Baz baz)\n" 7933 " : bar(bar), baz(baz) {}", 7934 Style); 7935 } 7936 7937 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 7938 FormatStyle Style = getLLVMStyleWithColumns(60); 7939 Style.BinPackArguments = false; 7940 for (int i = 0; i < 4; ++i) { 7941 // Test all combinations of parameters that should not have an effect. 7942 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 7943 Style.PackConstructorInitializers = 7944 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never; 7945 7946 Style.AllowAllArgumentsOnNextLine = true; 7947 verifyFormat("void foo() {\n" 7948 " FunctionCallWithReallyLongName(\n" 7949 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 7950 "}", 7951 Style); 7952 Style.AllowAllArgumentsOnNextLine = false; 7953 verifyFormat("void foo() {\n" 7954 " FunctionCallWithReallyLongName(\n" 7955 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7956 " bbbbbbbbbbbb);\n" 7957 "}", 7958 Style); 7959 7960 Style.AllowAllArgumentsOnNextLine = true; 7961 verifyFormat("void foo() {\n" 7962 " auto VariableWithReallyLongName = {\n" 7963 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 7964 "}", 7965 Style); 7966 Style.AllowAllArgumentsOnNextLine = false; 7967 verifyFormat("void foo() {\n" 7968 " auto VariableWithReallyLongName = {\n" 7969 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7970 " bbbbbbbbbbbb};\n" 7971 "}", 7972 Style); 7973 } 7974 7975 // This parameter should not affect declarations. 7976 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; 7977 Style.AllowAllArgumentsOnNextLine = false; 7978 Style.AllowAllParametersOfDeclarationOnNextLine = true; 7979 verifyFormat("void FunctionCallWithReallyLongName(\n" 7980 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 7981 Style); 7982 Style.AllowAllParametersOfDeclarationOnNextLine = false; 7983 verifyFormat("void FunctionCallWithReallyLongName(\n" 7984 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 7985 " int bbbbbbbbbbbb);", 7986 Style); 7987 } 7988 7989 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 7990 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 7991 // and BAS_Align. 7992 FormatStyle Style = getLLVMStyleWithColumns(35); 7993 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 7994 "void functionDecl(int A, int B, int C);"; 7995 Style.AllowAllArgumentsOnNextLine = false; 7996 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7997 verifyFormat(StringRef("functionCall(paramA, paramB,\n" 7998 " paramC);\n" 7999 "void functionDecl(int A, int B,\n" 8000 " int C);"), 8001 Input, Style); 8002 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 8003 verifyFormat(StringRef("functionCall(paramA, paramB,\n" 8004 " paramC);\n" 8005 "void functionDecl(int A, int B,\n" 8006 " int C);"), 8007 Input, Style); 8008 // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over 8009 // AllowAllArgumentsOnNextLine. 8010 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8011 verifyFormat(StringRef("functionCall(\n" 8012 " paramA, paramB, paramC);\n" 8013 "void functionDecl(\n" 8014 " int A, int B, int C);"), 8015 Input, Style); 8016 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 8017 verifyFormat("functionCall(\n" 8018 " paramA, paramB, paramC\n" 8019 ");\n" 8020 "void functionDecl(\n" 8021 " int A, int B, int C\n" 8022 ");", 8023 Input, Style); 8024 8025 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 8026 // first argument. 8027 Style.AllowAllArgumentsOnNextLine = true; 8028 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8029 verifyFormat(StringRef("functionCall(\n" 8030 " paramA, paramB, paramC);\n" 8031 "void functionDecl(\n" 8032 " int A, int B, int C);"), 8033 Input, Style); 8034 // It wouldn't fit on one line with aligned parameters so this setting 8035 // doesn't change anything for BAS_Align. 8036 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 8037 verifyFormat(StringRef("functionCall(paramA, paramB,\n" 8038 " paramC);\n" 8039 "void functionDecl(int A, int B,\n" 8040 " int C);"), 8041 Input, Style); 8042 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8043 verifyFormat(StringRef("functionCall(\n" 8044 " paramA, paramB, paramC);\n" 8045 "void functionDecl(\n" 8046 " int A, int B, int C);"), 8047 Input, Style); 8048 } 8049 8050 TEST_F(FormatTest, BreakFunctionDefinitionParameters) { 8051 StringRef Input = "void functionDecl(paramA, paramB, paramC);\n" 8052 "void emptyFunctionDefinition() {}\n" 8053 "void functionDefinition(int A, int B, int C) {}\n" 8054 "Class::Class(int A, int B) : m_A(A), m_B(B) {}"; 8055 verifyFormat(Input); 8056 8057 FormatStyle Style = getLLVMStyle(); 8058 EXPECT_FALSE(Style.BreakFunctionDefinitionParameters); 8059 Style.BreakFunctionDefinitionParameters = true; 8060 verifyFormat("void functionDecl(paramA, paramB, paramC);\n" 8061 "void emptyFunctionDefinition() {}\n" 8062 "void functionDefinition(\n" 8063 " int A, int B, int C) {}\n" 8064 "Class::Class(\n" 8065 " int A, int B)\n" 8066 " : m_A(A), m_B(B) {}", 8067 Input, Style); 8068 8069 // Test the style where all parameters are on their own lines. 8070 Style.AllowAllParametersOfDeclarationOnNextLine = false; 8071 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; 8072 verifyFormat("void functionDecl(paramA, paramB, paramC);\n" 8073 "void emptyFunctionDefinition() {}\n" 8074 "void functionDefinition(\n" 8075 " int A,\n" 8076 " int B,\n" 8077 " int C) {}\n" 8078 "Class::Class(\n" 8079 " int A,\n" 8080 " int B)\n" 8081 " : m_A(A), m_B(B) {}", 8082 Input, Style); 8083 } 8084 8085 TEST_F(FormatTest, BreakBeforeInlineASMColon) { 8086 FormatStyle Style = getLLVMStyle(); 8087 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Never; 8088 /* Test the behaviour with long lines */ 8089 Style.ColumnLimit = 40; 8090 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n" 8091 " : : val);", 8092 Style); 8093 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n" 8094 " : val1 : val2);", 8095 Style); 8096 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 8097 " \"cpuid\\n\\t\"\n" 8098 " \"xchgq\\t%%rbx %%rsi\\n\\t\",\n" 8099 " : \"=a\" : \"a\");", 8100 Style); 8101 Style.ColumnLimit = 80; 8102 verifyFormat("asm volatile(\"string\", : : val);", Style); 8103 verifyFormat("asm volatile(\"string\", : val1 : val2);", Style); 8104 8105 Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Always; 8106 verifyFormat("asm volatile(\"string\",\n" 8107 " :\n" 8108 " : val);", 8109 Style); 8110 verifyFormat("asm volatile(\"string\",\n" 8111 " : val1\n" 8112 " : val2);", 8113 Style); 8114 /* Test the behaviour with long lines */ 8115 Style.ColumnLimit = 40; 8116 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 8117 " \"cpuid\\n\\t\"\n" 8118 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 8119 " : \"=a\"(*rEAX)\n" 8120 " : \"a\"(value));", 8121 Style); 8122 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 8123 " \"cpuid\\n\\t\"\n" 8124 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 8125 " :\n" 8126 " : \"a\"(value));", 8127 Style); 8128 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n" 8129 " :\n" 8130 " : val);", 8131 Style); 8132 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n" 8133 " : val1\n" 8134 " : val2);", 8135 Style); 8136 } 8137 8138 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 8139 FormatStyle Style = getLLVMStyle(); 8140 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 8141 8142 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 8143 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 8144 getStyleWithColumns(Style, 45)); 8145 verifyFormat("Constructor() :\n" 8146 " Initializer(FitsOnTheLine) {}", 8147 getStyleWithColumns(Style, 44)); 8148 verifyFormat("Constructor() :\n" 8149 " Initializer(FitsOnTheLine) {}", 8150 getStyleWithColumns(Style, 43)); 8151 8152 verifyFormat("template <typename T>\n" 8153 "Constructor() : Initializer(FitsOnTheLine) {}", 8154 getStyleWithColumns(Style, 50)); 8155 verifyFormat( 8156 "Class::Class(int some, int arguments, int loooooooooooooooooooong,\n" 8157 " int mooooooooooooore) noexcept :\n" 8158 " Super{some, arguments}, Member{5}, Member2{2} {}", 8159 Style); 8160 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 8161 verifyFormat( 8162 "SomeClass::Constructor() :\n" 8163 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 8164 Style); 8165 verifyFormat( 8166 "SomeClass::Constructor() : // NOLINT\n" 8167 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 8168 Style); 8169 8170 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 8171 verifyFormat( 8172 "SomeClass::Constructor() :\n" 8173 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 8174 Style); 8175 verifyFormat( 8176 "SomeClass::Constructor() : // NOLINT\n" 8177 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 8178 Style); 8179 8180 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 8181 verifyFormat( 8182 "SomeClass::Constructor() :\n" 8183 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 8184 Style); 8185 8186 verifyFormat( 8187 "SomeClass::Constructor() :\n" 8188 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 8189 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 8190 Style); 8191 verifyFormat( 8192 "SomeClass::Constructor() :\n" 8193 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8194 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 8195 Style); 8196 verifyFormat( 8197 "Ctor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8198 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) : aaaaaaaaaa(aaaaaa) {}", 8199 Style); 8200 8201 verifyFormat("Constructor() :\n" 8202 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8203 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8204 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8205 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 8206 Style); 8207 8208 verifyFormat("Constructor() :\n" 8209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8210 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 8211 Style); 8212 8213 verifyFormat("Constructor(int Parameter = 0) :\n" 8214 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 8215 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 8216 Style); 8217 verifyFormat("Constructor() :\n" 8218 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 8219 "}", 8220 getStyleWithColumns(Style, 60)); 8221 verifyFormat("Constructor() :\n" 8222 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8223 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 8224 Style); 8225 8226 // Here a line could be saved by splitting the second initializer onto two 8227 // lines, but that is not desirable. 8228 verifyFormat("Constructor() :\n" 8229 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 8230 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 8231 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 8232 Style); 8233 8234 FormatStyle OnePerLine = Style; 8235 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 8236 verifyFormat("SomeClass::Constructor() :\n" 8237 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 8238 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 8239 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 8240 OnePerLine); 8241 verifyFormat("SomeClass::Constructor() :\n" 8242 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 8243 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 8244 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 8245 OnePerLine); 8246 verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n" 8247 " i(i), // comment\n" 8248 " j(j) {}", 8249 OnePerLine); 8250 verifyFormat("MyClass::MyClass(int var) :\n" 8251 " some_var_(var), // 4 space indent\n" 8252 " some_other_var_(var + 1) { // lined up\n" 8253 "}", 8254 OnePerLine); 8255 verifyFormat("Constructor() :\n" 8256 " aaaaa(aaaaaa),\n" 8257 " aaaaa(aaaaaa),\n" 8258 " aaaaa(aaaaaa),\n" 8259 " aaaaa(aaaaaa),\n" 8260 " aaaaa(aaaaaa) {}", 8261 OnePerLine); 8262 verifyFormat("Constructor() :\n" 8263 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 8264 " aaaaaaaaaaaaaaaaaaaaaa) {}", 8265 OnePerLine); 8266 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; 8267 verifyFormat("Constructor() :\n" 8268 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 8269 " aaaaaaaaaaa().aaa(),\n" 8270 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 8271 OnePerLine); 8272 OnePerLine.ColumnLimit = 60; 8273 verifyFormat("Constructor() :\n" 8274 " aaaaaaaaaaaaaaaaaaaa(a),\n" 8275 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 8276 OnePerLine); 8277 8278 verifyFormat("Constructor() :\n" 8279 " // Comment forcing unwanted break.\n" 8280 " aaaa(aaaa) {}", 8281 Style); 8282 verifyFormat("Constructor() : // NOLINT\n" 8283 " aaaa(aaaa) {}", 8284 Style); 8285 verifyFormat("Constructor() : // A very long trailing comment that cannot fit" 8286 " on a single\n" 8287 " // line.\n" 8288 " aaaa(aaaa) {}", 8289 "Constructor() : // A very long trailing comment that cannot fit" 8290 " on a single line.\n" 8291 " aaaa(aaaa) {}", 8292 Style); 8293 8294 Style.ColumnLimit = 0; 8295 verifyFormat("SomeClass::Constructor() :\n" 8296 " a(a) {}", 8297 Style); 8298 verifyFormat("SomeClass::Constructor() noexcept :\n" 8299 " a(a) {}", 8300 Style); 8301 verifyFormat("SomeClass::Constructor() :\n" 8302 " a(a), b(b), c(c) {}", 8303 Style); 8304 verifyFormat("SomeClass::Constructor() :\n" 8305 " a(a) {\n" 8306 " foo();\n" 8307 " bar();\n" 8308 "}", 8309 Style); 8310 8311 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 8312 verifyFormat("SomeClass::Constructor() :\n" 8313 " a(a), b(b), c(c) {\n" 8314 "}", 8315 Style); 8316 verifyFormat("SomeClass::Constructor() :\n" 8317 " a(a) {\n" 8318 "}", 8319 Style); 8320 8321 Style.ColumnLimit = 80; 8322 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 8323 Style.ConstructorInitializerIndentWidth = 2; 8324 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 8325 verifyFormat("SomeClass::Constructor() :\n" 8326 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8327 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 8328 Style); 8329 8330 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 8331 // well 8332 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 8333 verifyFormat( 8334 "class SomeClass\n" 8335 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8336 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 8337 Style); 8338 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 8339 verifyFormat( 8340 "class SomeClass\n" 8341 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8342 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 8343 Style); 8344 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 8345 verifyFormat( 8346 "class SomeClass :\n" 8347 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8348 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 8349 Style); 8350 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 8351 verifyFormat( 8352 "class SomeClass\n" 8353 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8354 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 8355 Style); 8356 } 8357 8358 #ifndef EXPENSIVE_CHECKS 8359 // Expensive checks enables libstdc++ checking which includes validating the 8360 // state of ranges used in std::priority_queue - this blows out the 8361 // runtime/scalability of the function and makes this test unacceptably slow. 8362 TEST_F(FormatTest, MemoizationTests) { 8363 // This breaks if the memoization lookup does not take \c Indent and 8364 // \c LastSpace into account. 8365 verifyFormat( 8366 "extern CFRunLoopTimerRef\n" 8367 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 8368 " CFTimeInterval interval, CFOptionFlags flags,\n" 8369 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 8370 " CFRunLoopTimerContext *context) {}"); 8371 8372 // Deep nesting somewhat works around our memoization. 8373 verifyFormat( 8374 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 8375 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 8376 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 8377 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 8378 " aaaaa())))))))))))))))))))))))))))))))))))))));", 8379 getLLVMStyleWithColumns(65)); 8380 verifyFormat( 8381 "aaaaa(\n" 8382 " aaaaa,\n" 8383 " aaaaa(\n" 8384 " aaaaa,\n" 8385 " aaaaa(\n" 8386 " aaaaa,\n" 8387 " aaaaa(\n" 8388 " aaaaa,\n" 8389 " aaaaa(\n" 8390 " aaaaa,\n" 8391 " aaaaa(\n" 8392 " aaaaa,\n" 8393 " aaaaa(\n" 8394 " aaaaa,\n" 8395 " aaaaa(\n" 8396 " aaaaa,\n" 8397 " aaaaa(\n" 8398 " aaaaa,\n" 8399 " aaaaa(\n" 8400 " aaaaa,\n" 8401 " aaaaa(\n" 8402 " aaaaa,\n" 8403 " aaaaa(\n" 8404 " aaaaa,\n" 8405 " aaaaa))))))))))));", 8406 getLLVMStyleWithColumns(65)); 8407 verifyFormat( 8408 "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" 8409 " a),\n" 8410 " a),\n" 8411 " a),\n" 8412 " a),\n" 8413 " a),\n" 8414 " a),\n" 8415 " a),\n" 8416 " a),\n" 8417 " a),\n" 8418 " a),\n" 8419 " a),\n" 8420 " a),\n" 8421 " a),\n" 8422 " a),\n" 8423 " a),\n" 8424 " a),\n" 8425 " a)", 8426 getLLVMStyleWithColumns(65)); 8427 8428 // This test takes VERY long when memoization is broken. 8429 FormatStyle OnePerLine = getLLVMStyle(); 8430 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 8431 OnePerLine.BinPackParameters = FormatStyle::BPPS_OnePerLine; 8432 std::string input = "Constructor()\n" 8433 " : aaaa(a,\n"; 8434 for (unsigned i = 0, e = 80; i != e; ++i) 8435 input += " a,\n"; 8436 input += " a) {}"; 8437 verifyFormat(input, OnePerLine); 8438 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 8439 verifyFormat(input, OnePerLine); 8440 } 8441 #endif 8442 8443 TEST_F(FormatTest, BreaksAsHighAsPossible) { 8444 verifyFormat( 8445 "void f() {\n" 8446 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 8447 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 8448 " f();\n" 8449 "}"); 8450 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 8451 " Intervals[i - 1].getRange().getLast()) {\n}"); 8452 } 8453 8454 TEST_F(FormatTest, BreaksFunctionDeclarations) { 8455 // Principially, we break function declarations in a certain order: 8456 // 1) break amongst arguments. 8457 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 8458 " Cccccccccccccc cccccccccccccc);"); 8459 verifyFormat("template <class TemplateIt>\n" 8460 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 8461 " TemplateIt *stop) {}"); 8462 8463 // 2) break after return type. 8464 verifyGoogleFormat( 8465 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8466 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);"); 8467 8468 // 3) break after (. 8469 verifyGoogleFormat( 8470 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 8471 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);"); 8472 8473 // 4) break before after nested name specifiers. 8474 verifyGoogleFormat( 8475 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8476 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 8477 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);"); 8478 8479 // However, there are exceptions, if a sufficient amount of lines can be 8480 // saved. 8481 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 8482 // more adjusting. 8483 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 8484 " Cccccccccccccc cccccccccc,\n" 8485 " Cccccccccccccc cccccccccc,\n" 8486 " Cccccccccccccc cccccccccc,\n" 8487 " Cccccccccccccc cccccccccc);"); 8488 verifyGoogleFormat( 8489 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8490 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 8491 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 8492 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 8493 verifyFormat( 8494 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 8495 " Cccccccccccccc cccccccccc,\n" 8496 " Cccccccccccccc cccccccccc,\n" 8497 " Cccccccccccccc cccccccccc,\n" 8498 " Cccccccccccccc cccccccccc,\n" 8499 " Cccccccccccccc cccccccccc,\n" 8500 " Cccccccccccccc cccccccccc);"); 8501 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 8502 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 8503 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 8504 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 8505 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 8506 8507 // Break after multi-line parameters. 8508 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8510 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8511 " bbbb bbbb);"); 8512 verifyFormat("void SomeLoooooooooooongFunction(\n" 8513 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 8514 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8515 " int bbbbbbbbbbbbb);"); 8516 8517 // Treat overloaded operators like other functions. 8518 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 8519 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 8520 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 8521 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 8522 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 8523 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 8524 verifyGoogleFormat( 8525 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 8526 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 8527 verifyGoogleFormat( 8528 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 8529 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 8530 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8531 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 8532 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 8533 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 8534 verifyGoogleFormat( 8535 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 8536 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8537 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 8538 verifyGoogleFormat("template <typename T>\n" 8539 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8540 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 8541 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 8542 8543 FormatStyle Style = getLLVMStyle(); 8544 Style.PointerAlignment = FormatStyle::PAS_Left; 8545 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8546 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 8547 Style); 8548 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 8549 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 8550 Style); 8551 } 8552 8553 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 8554 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 8555 // Prefer keeping `::` followed by `operator` together. 8556 verifyFormat("const aaaa::bbbbbbb &\n" 8557 "ccccccccc::operator++() {\n" 8558 " stuff();\n" 8559 "}", 8560 "const aaaa::bbbbbbb\n" 8561 "&ccccccccc::operator++() { stuff(); }", 8562 getLLVMStyleWithColumns(40)); 8563 } 8564 8565 TEST_F(FormatTest, TrailingReturnType) { 8566 verifyFormat("auto foo() -> int;"); 8567 // correct trailing return type spacing 8568 verifyFormat("auto operator->() -> int;"); 8569 verifyFormat("auto operator++(int) -> int;"); 8570 8571 verifyFormat("struct S {\n" 8572 " auto bar() const -> int;\n" 8573 "};"); 8574 verifyFormat("template <size_t Order, typename T>\n" 8575 "auto load_img(const std::string &filename)\n" 8576 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 8577 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 8578 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 8579 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 8580 verifyFormat("template <typename T>\n" 8581 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 8582 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 8583 8584 FormatStyle Style = getLLVMStyleWithColumns(60); 8585 verifyFormat("#define MAKE_DEF(NAME) \\\n" 8586 " auto NAME() -> int { return 42; }", 8587 Style); 8588 8589 // Not trailing return types. 8590 verifyFormat("void f() { auto a = b->c(); }"); 8591 verifyFormat("auto a = p->foo();"); 8592 verifyFormat("int a = p->foo();"); 8593 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };"); 8594 } 8595 8596 TEST_F(FormatTest, DeductionGuides) { 8597 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 8598 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 8599 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 8600 verifyFormat( 8601 "template <class... T>\n" 8602 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 8603 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 8604 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 8605 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 8606 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 8607 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 8608 verifyFormat("template <class T> x() -> x<1>;"); 8609 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 8610 8611 verifyFormat("A(const char *) -> A<string &>;"); 8612 verifyFormat("A() -> A<int>;"); 8613 8614 // Ensure not deduction guides. 8615 verifyFormat("c()->f<int>();"); 8616 verifyFormat("x()->foo<1>;"); 8617 verifyFormat("x = p->foo<3>();"); 8618 verifyFormat("x()->x<1>();"); 8619 } 8620 8621 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 8622 // Avoid breaking before trailing 'const' or other trailing annotations, if 8623 // they are not function-like. 8624 FormatStyle Style = getGoogleStyleWithColumns(47); 8625 verifyFormat("void someLongFunction(\n" 8626 " int someLoooooooooooooongParameter) const {\n}", 8627 getLLVMStyleWithColumns(47)); 8628 verifyFormat("LoooooongReturnType\n" 8629 "someLoooooooongFunction() const {}", 8630 getLLVMStyleWithColumns(47)); 8631 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 8632 " const {}", 8633 Style); 8634 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 8635 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 8636 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 8637 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 8638 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 8639 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 8640 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 8641 " aaaaaaaaaaa aaaaa) const override;"); 8642 verifyGoogleFormat( 8643 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8644 " const override;"); 8645 8646 // Even if the first parameter has to be wrapped. 8647 verifyFormat("void someLongFunction(\n" 8648 " int someLongParameter) const {}", 8649 getLLVMStyleWithColumns(46)); 8650 verifyFormat("void someLongFunction(\n" 8651 " int someLongParameter) const {}", 8652 Style); 8653 verifyFormat("void someLongFunction(\n" 8654 " int someLongParameter) override {}", 8655 Style); 8656 verifyFormat("void someLongFunction(\n" 8657 " int someLongParameter) OVERRIDE {}", 8658 Style); 8659 verifyFormat("void someLongFunction(\n" 8660 " int someLongParameter) final {}", 8661 Style); 8662 verifyFormat("void someLongFunction(\n" 8663 " int someLongParameter) FINAL {}", 8664 Style); 8665 verifyFormat("void someLongFunction(\n" 8666 " int parameter) const override {}", 8667 Style); 8668 8669 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 8670 verifyFormat("void someLongFunction(\n" 8671 " int someLongParameter) const\n" 8672 "{\n" 8673 "}", 8674 Style); 8675 8676 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 8677 verifyFormat("void someLongFunction(\n" 8678 " int someLongParameter) const\n" 8679 " {\n" 8680 " }", 8681 Style); 8682 8683 // Unless these are unknown annotations. 8684 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 8685 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8686 " LONG_AND_UGLY_ANNOTATION;"); 8687 8688 // Breaking before function-like trailing annotations is fine to keep them 8689 // close to their arguments. 8690 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8691 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 8692 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 8693 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 8694 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 8695 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 8696 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 8697 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 8698 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 8699 8700 verifyFormat( 8701 "void aaaaaaaaaaaaaaaaaa()\n" 8702 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 8703 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 8704 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8705 " __attribute__((unused));"); 8706 8707 Style = getGoogleStyle(); 8708 8709 verifyFormat( 8710 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8711 " GUARDED_BY(aaaaaaaaaaaa);", 8712 Style); 8713 verifyFormat( 8714 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8715 " GUARDED_BY(aaaaaaaaaaaa);", 8716 Style); 8717 verifyFormat( 8718 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 8719 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8720 Style); 8721 verifyFormat( 8722 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 8723 " aaaaaaaaaaaaaaaaaaaaaaaaa;", 8724 Style); 8725 8726 verifyFormat( 8727 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8728 " ABSL_GUARDED_BY(aaaaaaaaaaaa);", 8729 Style); 8730 verifyFormat( 8731 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8732 " ABSL_GUARDED_BY(aaaaaaaaaaaa);", 8733 Style); 8734 verifyFormat( 8735 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n" 8736 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8737 Style); 8738 verifyFormat( 8739 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ABSL_GUARDED_BY(aaaaaaaaaaaa) =\n" 8740 " aaaaaaaaaaaaaaaaaaaaaaaaa;", 8741 Style); 8742 } 8743 8744 TEST_F(FormatTest, FunctionAnnotations) { 8745 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 8746 "int OldFunction(const string ¶meter) {}"); 8747 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 8748 "string OldFunction(const string ¶meter) {}"); 8749 verifyFormat("template <typename T>\n" 8750 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 8751 "string OldFunction(const string ¶meter) {}"); 8752 8753 // Not function annotations. 8754 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8755 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 8756 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 8757 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 8758 verifyFormat("MACRO(abc).function() // wrap\n" 8759 " << abc;"); 8760 verifyFormat("MACRO(abc)->function() // wrap\n" 8761 " << abc;"); 8762 verifyFormat("MACRO(abc)::function() // wrap\n" 8763 " << abc;"); 8764 verifyFormat("FOO(bar)();", getLLVMStyleWithColumns(0)); 8765 } 8766 8767 TEST_F(FormatTest, BreaksDesireably) { 8768 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 8769 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 8770 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 8771 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8772 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 8773 "}"); 8774 8775 verifyFormat( 8776 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8777 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 8778 8779 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8780 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8781 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8782 8783 verifyFormat( 8784 "aaaaaaaa(aaaaaaaaaaaaa,\n" 8785 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8786 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 8787 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8788 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 8789 8790 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 8791 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8792 8793 verifyFormat( 8794 "void f() {\n" 8795 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 8796 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8797 "}"); 8798 verifyFormat( 8799 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8801 verifyFormat( 8802 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8803 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8804 verifyFormat( 8805 "aaaaaa(aaa,\n" 8806 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8807 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8808 " aaaa);"); 8809 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 8810 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8811 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8812 8813 // Indent consistently independent of call expression and unary operator. 8814 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 8815 " dddddddddddddddddddddddddddddd));"); 8816 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 8817 " dddddddddddddddddddddddddddddd));"); 8818 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 8819 " dddddddddddddddddddddddddddddd));"); 8820 8821 // This test case breaks on an incorrect memoization, i.e. an optimization not 8822 // taking into account the StopAt value. 8823 verifyFormat( 8824 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 8825 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 8826 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 8827 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8828 8829 verifyFormat("{\n {\n {\n" 8830 " Annotation.SpaceRequiredBefore =\n" 8831 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 8832 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 8833 " }\n }\n}"); 8834 8835 // Break on an outer level if there was a break on an inner level. 8836 verifyFormat("f(g(h(a, // comment\n" 8837 " b, c),\n" 8838 " d, e),\n" 8839 " x, y);", 8840 "f(g(h(a, // comment\n" 8841 " b, c), d, e), x, y);"); 8842 8843 // Prefer breaking similar line breaks. 8844 verifyFormat( 8845 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 8846 " NSTrackingMouseEnteredAndExited |\n" 8847 " NSTrackingActiveAlways;"); 8848 } 8849 8850 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 8851 FormatStyle NoBinPacking = getGoogleStyle(); 8852 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; 8853 NoBinPacking.BinPackArguments = true; 8854 verifyFormat("void f() {\n" 8855 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 8856 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8857 "}", 8858 NoBinPacking); 8859 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 8860 " int aaaaaaaaaaaaaaaaaaaa,\n" 8861 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 8862 NoBinPacking); 8863 8864 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 8865 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8866 " vector<int> bbbbbbbbbbbbbbb);", 8867 NoBinPacking); 8868 // FIXME: This behavior difference is probably not wanted. However, currently 8869 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 8870 // template arguments from BreakBeforeParameter being set because of the 8871 // one-per-line formatting. 8872 verifyFormat( 8873 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 8874 " aaaaaaaaaa> aaaaaaaaaa);", 8875 NoBinPacking); 8876 verifyFormat( 8877 "void fffffffffff(\n" 8878 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 8879 " aaaaaaaaaa);"); 8880 } 8881 8882 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 8883 FormatStyle NoBinPacking = getGoogleStyle(); 8884 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; 8885 NoBinPacking.BinPackArguments = false; 8886 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 8887 " aaaaaaaaaaaaaaaaaaaa,\n" 8888 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 8889 NoBinPacking); 8890 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 8891 " aaaaaaaaaaaaa,\n" 8892 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 8893 NoBinPacking); 8894 verifyFormat( 8895 "aaaaaaaa(aaaaaaaaaaaaa,\n" 8896 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8897 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 8898 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 8900 NoBinPacking); 8901 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 8902 " .aaaaaaaaaaaaaaaaaa();", 8903 NoBinPacking); 8904 verifyFormat("void f() {\n" 8905 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8906 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 8907 "}", 8908 NoBinPacking); 8909 8910 verifyFormat( 8911 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8912 " aaaaaaaaaaaa,\n" 8913 " aaaaaaaaaaaa);", 8914 NoBinPacking); 8915 verifyFormat( 8916 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 8917 " ddddddddddddddddddddddddddddd),\n" 8918 " test);", 8919 NoBinPacking); 8920 8921 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 8922 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 8923 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 8924 " aaaaaaaaaaaaaaaaaa;", 8925 NoBinPacking); 8926 verifyFormat("a(\"a\"\n" 8927 " \"a\",\n" 8928 " a);"); 8929 8930 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 8931 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 8932 " aaaaaaaaa,\n" 8933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8934 NoBinPacking); 8935 verifyFormat( 8936 "void f() {\n" 8937 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 8938 " .aaaaaaa();\n" 8939 "}", 8940 NoBinPacking); 8941 verifyFormat( 8942 "template <class SomeType, class SomeOtherType>\n" 8943 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 8944 NoBinPacking); 8945 } 8946 8947 TEST_F(FormatTest, FormatsDeclarationBreakAlways) { 8948 FormatStyle BreakAlways = getGoogleStyle(); 8949 BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine; 8950 verifyFormat("void f(int a,\n" 8951 " int b);", 8952 BreakAlways); 8953 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8954 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n" 8955 " int cccccccccccccccccccccccc);", 8956 BreakAlways); 8957 8958 // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set 8959 // to BPPS_AlwaysOnePerLine. 8960 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8961 verifyFormat( 8962 "void someLongFunctionName(\n" 8963 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8964 " int b);", 8965 BreakAlways); 8966 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 8967 verifyFormat( 8968 "void someLongFunctionName(\n" 8969 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8970 " int b\n" 8971 ");", 8972 BreakAlways); 8973 } 8974 8975 TEST_F(FormatTest, FormatsDefinitionBreakAlways) { 8976 FormatStyle BreakAlways = getGoogleStyle(); 8977 BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine; 8978 verifyFormat("void f(int a,\n" 8979 " int b) {\n" 8980 " f(a, b);\n" 8981 "}", 8982 BreakAlways); 8983 8984 // Ensure BinPackArguments interact correctly when BinPackParameters is set to 8985 // BPPS_AlwaysOnePerLine. 8986 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8987 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n" 8988 " int cccccccccccccccccccccccc) {\n" 8989 " f(aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbb,\n" 8990 " cccccccccccccccccccccccc);\n" 8991 "}", 8992 BreakAlways); 8993 BreakAlways.BinPackArguments = false; 8994 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8995 " int bbbbbbbbbbbbbbbbbbbbbbbbb,\n" 8996 " int cccccccccccccccccccccccc) {\n" 8997 " f(aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8998 " bbbbbbbbbbbbbbbbbbbbbbbbb,\n" 8999 " cccccccccccccccccccccccc);\n" 9000 "}", 9001 BreakAlways); 9002 9003 // Ensure BreakFunctionDefinitionParameters interacts correctly when 9004 // BinPackParameters is set to BPPS_AlwaysOnePerLine. 9005 BreakAlways.BreakFunctionDefinitionParameters = true; 9006 verifyFormat("void f(\n" 9007 " int a,\n" 9008 " int b) {\n" 9009 " f(a, b);\n" 9010 "}", 9011 BreakAlways); 9012 BreakAlways.BreakFunctionDefinitionParameters = false; 9013 9014 // Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set 9015 // to BPPS_AlwaysOnePerLine. 9016 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9017 verifyFormat( 9018 "void someLongFunctionName(\n" 9019 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9020 " int b) {\n" 9021 " someLongFunctionName(\n" 9022 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b);\n" 9023 "}", 9024 BreakAlways); 9025 BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 9026 verifyFormat( 9027 "void someLongFunctionName(\n" 9028 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9029 " int b\n" 9030 ") {\n" 9031 " someLongFunctionName(\n" 9032 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b\n" 9033 " );\n" 9034 "}", 9035 BreakAlways); 9036 } 9037 9038 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 9039 FormatStyle Style = getLLVMStyleWithColumns(15); 9040 Style.ExperimentalAutoDetectBinPacking = true; 9041 verifyFormat("aaa(aaaa,\n" 9042 " aaaa,\n" 9043 " aaaa);\n" 9044 "aaa(aaaa,\n" 9045 " aaaa,\n" 9046 " aaaa);", 9047 "aaa(aaaa,\n" // one-per-line 9048 " aaaa,\n" 9049 " aaaa );\n" 9050 "aaa(aaaa, aaaa, aaaa);", // inconclusive 9051 Style); 9052 verifyFormat("aaa(aaaa, aaaa,\n" 9053 " aaaa);\n" 9054 "aaa(aaaa, aaaa,\n" 9055 " aaaa);", 9056 "aaa(aaaa, aaaa,\n" // bin-packed 9057 " aaaa );\n" 9058 "aaa(aaaa, aaaa, aaaa);", // inconclusive 9059 Style); 9060 } 9061 9062 TEST_F(FormatTest, IndentExportBlock) { 9063 FormatStyle Style = getLLVMStyleWithColumns(80); 9064 Style.IndentExportBlock = true; 9065 verifyFormat("export {\n" 9066 " int x;\n" 9067 " int y;\n" 9068 "}", 9069 "export {\n" 9070 "int x;\n" 9071 "int y;\n" 9072 "}", 9073 Style); 9074 9075 Style.IndentExportBlock = false; 9076 verifyFormat("export {\n" 9077 "int x;\n" 9078 "int y;\n" 9079 "}", 9080 "export {\n" 9081 " int x;\n" 9082 " int y;\n" 9083 "}", 9084 Style); 9085 } 9086 9087 TEST_F(FormatTest, ShortExportBlocks) { 9088 FormatStyle Style = getLLVMStyleWithColumns(80); 9089 Style.IndentExportBlock = false; 9090 9091 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 9092 verifyFormat("export {\n" 9093 "}", 9094 Style); 9095 9096 verifyFormat("export {\n" 9097 "int x;\n" 9098 "}", 9099 Style); 9100 9101 verifyFormat("export {\n" 9102 "int x;\n" 9103 "}", 9104 "export\n" 9105 "{\n" 9106 "int x;\n" 9107 "}", 9108 Style); 9109 9110 verifyFormat("export {\n" 9111 "}", 9112 "export {}", Style); 9113 9114 verifyFormat("export {\n" 9115 "int x;\n" 9116 "}", 9117 "export { int x; }", Style); 9118 9119 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 9120 verifyFormat("export {}", 9121 "export {\n" 9122 "}", 9123 Style); 9124 9125 verifyFormat("export { int x; }", 9126 "export {\n" 9127 "int x;\n" 9128 "}", 9129 Style); 9130 9131 verifyFormat("export { int x; }", 9132 "export\n" 9133 "{\n" 9134 "int x;\n" 9135 "}", 9136 Style); 9137 9138 verifyFormat("export {}", 9139 "export {\n" 9140 "}", 9141 Style); 9142 9143 verifyFormat("export { int x; }", 9144 "export {\n" 9145 "int x;\n" 9146 "}", 9147 Style); 9148 9149 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 9150 verifyFormat("export {}", 9151 "export {\n" 9152 "}", 9153 Style); 9154 9155 verifyFormat("export {\n" 9156 "int x;\n" 9157 "}", 9158 Style); 9159 9160 verifyFormat("export {\n" 9161 "int x;\n" 9162 "}", 9163 "export\n" 9164 "{\n" 9165 "int x;\n" 9166 "}", 9167 Style); 9168 9169 verifyFormat("export {}", Style); 9170 9171 verifyFormat("export {\n" 9172 "int x;\n" 9173 "}", 9174 "export { int x; }", Style); 9175 } 9176 9177 TEST_F(FormatTest, FormatsBuilderPattern) { 9178 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 9179 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 9180 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 9181 " .StartsWith(\".init\", ORDER_INIT)\n" 9182 " .StartsWith(\".fini\", ORDER_FINI)\n" 9183 " .StartsWith(\".hash\", ORDER_HASH)\n" 9184 " .Default(ORDER_TEXT);"); 9185 9186 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 9187 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 9188 verifyFormat("aaaaaaa->aaaaaaa\n" 9189 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9191 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 9192 verifyFormat( 9193 "aaaaaaa->aaaaaaa\n" 9194 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9195 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 9196 verifyFormat( 9197 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 9198 " aaaaaaaaaaaaaa);"); 9199 verifyFormat( 9200 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 9201 " aaaaaa->aaaaaaaaaaaa()\n" 9202 " ->aaaaaaaaaaaaaaaa(\n" 9203 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9204 " ->aaaaaaaaaaaaaaaaa();"); 9205 verifyGoogleFormat( 9206 "void f() {\n" 9207 " someo->Add((new util::filetools::Handler(dir))\n" 9208 " ->OnEvent1(NewPermanentCallback(\n" 9209 " this, &HandlerHolderClass::EventHandlerCBA))\n" 9210 " ->OnEvent2(NewPermanentCallback(\n" 9211 " this, &HandlerHolderClass::EventHandlerCBB))\n" 9212 " ->OnEvent3(NewPermanentCallback(\n" 9213 " this, &HandlerHolderClass::EventHandlerCBC))\n" 9214 " ->OnEvent5(NewPermanentCallback(\n" 9215 " this, &HandlerHolderClass::EventHandlerCBD))\n" 9216 " ->OnEvent6(NewPermanentCallback(\n" 9217 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 9218 "}"); 9219 9220 verifyFormat( 9221 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 9222 verifyFormat("aaaaaaaaaaaaaaa()\n" 9223 " .aaaaaaaaaaaaaaa()\n" 9224 " .aaaaaaaaaaaaaaa()\n" 9225 " .aaaaaaaaaaaaaaa()\n" 9226 " .aaaaaaaaaaaaaaa();"); 9227 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 9228 " .aaaaaaaaaaaaaaa()\n" 9229 " .aaaaaaaaaaaaaaa()\n" 9230 " .aaaaaaaaaaaaaaa();"); 9231 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 9232 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 9233 " .aaaaaaaaaaaaaaa();"); 9234 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 9235 " ->aaaaaaaaaaaaaae(0)\n" 9236 " ->aaaaaaaaaaaaaaa();"); 9237 9238 // Don't linewrap after very short segments. 9239 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9240 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9241 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9242 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9243 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9244 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9245 verifyFormat("aaa()\n" 9246 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9247 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9248 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9249 9250 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 9251 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 9252 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 9253 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 9254 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 9255 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 9256 9257 // Prefer not to break after empty parentheses. 9258 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 9259 " First->LastNewlineOffset);"); 9260 9261 // Prefer not to create "hanging" indents. 9262 verifyFormat( 9263 "return !soooooooooooooome_map\n" 9264 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9265 " .second;"); 9266 verifyFormat( 9267 "return aaaaaaaaaaaaaaaa\n" 9268 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 9269 " .aaaa(aaaaaaaaaaaaaa);"); 9270 // No hanging indent here. 9271 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 9272 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9273 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 9274 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9275 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 9276 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9277 getLLVMStyleWithColumns(60)); 9278 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 9279 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 9280 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9281 getLLVMStyleWithColumns(59)); 9282 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9283 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9284 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9285 9286 // Dont break if only closing statements before member call 9287 verifyFormat("test() {\n" 9288 " ([]() -> {\n" 9289 " int b = 32;\n" 9290 " return 3;\n" 9291 " }).foo();\n" 9292 "}"); 9293 verifyFormat("test() {\n" 9294 " (\n" 9295 " []() -> {\n" 9296 " int b = 32;\n" 9297 " return 3;\n" 9298 " },\n" 9299 " foo, bar)\n" 9300 " .foo();\n" 9301 "}"); 9302 verifyFormat("test() {\n" 9303 " ([]() -> {\n" 9304 " int b = 32;\n" 9305 " return 3;\n" 9306 " })\n" 9307 " .foo()\n" 9308 " .bar();\n" 9309 "}"); 9310 verifyFormat("test() {\n" 9311 " ([]() -> {\n" 9312 " int b = 32;\n" 9313 " return 3;\n" 9314 " })\n" 9315 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 9316 " \"bbbb\");\n" 9317 "}", 9318 getLLVMStyleWithColumns(30)); 9319 } 9320 9321 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 9322 verifyFormat( 9323 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 9324 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 9325 verifyFormat( 9326 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 9327 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 9328 9329 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 9330 " ccccccccccccccccccccccccc) {\n}"); 9331 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 9332 " ccccccccccccccccccccccccc) {\n}"); 9333 9334 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 9335 " ccccccccccccccccccccccccc) {\n}"); 9336 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 9337 " ccccccccccccccccccccccccc) {\n}"); 9338 9339 verifyFormat( 9340 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 9341 " ccccccccccccccccccccccccc) {\n}"); 9342 verifyFormat( 9343 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 9344 " ccccccccccccccccccccccccc) {\n}"); 9345 9346 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 9347 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 9348 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 9349 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 9350 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 9351 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 9352 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 9353 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 9354 9355 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 9356 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 9357 " aaaaaaaaaaaaaaa != aa) {\n}"); 9358 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 9359 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 9360 " aaaaaaaaaaaaaaa != aa) {\n}"); 9361 } 9362 9363 TEST_F(FormatTest, BreaksAfterAssignments) { 9364 verifyFormat( 9365 "unsigned Cost =\n" 9366 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 9367 " SI->getPointerAddressSpaceee());"); 9368 verifyFormat( 9369 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 9370 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 9371 9372 verifyFormat( 9373 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 9374 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 9375 verifyFormat("unsigned OriginalStartColumn =\n" 9376 " SourceMgr.getSpellingColumnNumber(\n" 9377 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 9378 " 1;"); 9379 } 9380 9381 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 9382 FormatStyle Style = getLLVMStyle(); 9383 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9384 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 9385 Style); 9386 9387 Style.PenaltyBreakAssignment = 20; 9388 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 9389 " cccccccccccccccccccccccccc;", 9390 Style); 9391 } 9392 9393 TEST_F(FormatTest, AlignsAfterAssignments) { 9394 verifyFormat( 9395 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 9396 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 9397 verifyFormat( 9398 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 9399 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 9400 verifyFormat( 9401 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 9402 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 9403 verifyFormat( 9404 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 9405 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 9406 verifyFormat( 9407 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 9408 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 9409 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 9410 } 9411 9412 TEST_F(FormatTest, AlignsAfterReturn) { 9413 verifyFormat( 9414 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 9415 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 9416 verifyFormat( 9417 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 9418 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 9419 verifyFormat( 9420 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 9421 " aaaaaaaaaaaaaaaaaaaaaa();"); 9422 verifyFormat( 9423 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 9424 " aaaaaaaaaaaaaaaaaaaaaa());"); 9425 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9426 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9427 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9428 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 9429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9430 verifyFormat("return\n" 9431 " // true if code is one of a or b.\n" 9432 " code == a || code == b;"); 9433 } 9434 9435 TEST_F(FormatTest, AlignsAfterOpenBracket) { 9436 verifyFormat( 9437 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 9438 " aaaaaaaaa aaaaaaa) {}"); 9439 verifyFormat( 9440 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 9441 " aaaaaaaaaaa aaaaaaaaa);"); 9442 verifyFormat( 9443 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 9444 " aaaaaaaaaaaaaaaaaaaaa));"); 9445 FormatStyle Style = getLLVMStyle(); 9446 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9447 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9448 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 9449 Style); 9450 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 9451 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 9452 Style); 9453 verifyFormat("SomeLongVariableName->someFunction(\n" 9454 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 9455 Style); 9456 verifyFormat( 9457 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 9458 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 9459 Style); 9460 verifyFormat( 9461 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 9462 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9463 Style); 9464 verifyFormat( 9465 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 9466 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 9467 Style); 9468 9469 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 9470 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 9471 " b));", 9472 Style); 9473 9474 Style.ColumnLimit = 30; 9475 verifyFormat("for (int foo = 0; foo < FOO;\n" 9476 " ++foo) {\n" 9477 " bar(foo);\n" 9478 "}", 9479 Style); 9480 Style.ColumnLimit = 80; 9481 9482 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9483 Style.BinPackArguments = false; 9484 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; 9485 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9486 " aaaaaaaaaaa aaaaaaaa,\n" 9487 " aaaaaaaaa aaaaaaa,\n" 9488 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 9489 Style); 9490 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 9491 " aaaaaaaaaaa aaaaaaaaa,\n" 9492 " aaaaaaaaaaa aaaaaaaaa,\n" 9493 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9494 Style); 9495 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 9496 " aaaaaaaaaaaaaaa,\n" 9497 " aaaaaaaaaaaaaaaaaaaaa,\n" 9498 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 9499 Style); 9500 verifyFormat( 9501 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 9502 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 9503 Style); 9504 verifyFormat( 9505 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 9506 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 9507 Style); 9508 verifyFormat( 9509 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 9510 " aaaaaaaaaaaaaaaaaaaaa(\n" 9511 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 9512 " aaaaaaaaaaaaaaaa);", 9513 Style); 9514 verifyFormat( 9515 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 9516 " aaaaaaaaaaaaaaaaaaaaa(\n" 9517 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 9518 " aaaaaaaaaaaaaaaa);", 9519 Style); 9520 verifyFormat( 9521 "fooooooooooo(new BARRRRRRRRR(\n" 9522 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));", 9523 Style); 9524 verifyFormat( 9525 "fooooooooooo(::new BARRRRRRRRR(\n" 9526 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));", 9527 Style); 9528 verifyFormat( 9529 "fooooooooooo(new FOO::BARRRR(\n" 9530 " XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));", 9531 Style); 9532 9533 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 9534 Style.BinPackArguments = false; 9535 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; 9536 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9537 " aaaaaaaaaaa aaaaaaaa,\n" 9538 " aaaaaaaaa aaaaaaa,\n" 9539 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9540 ") {}", 9541 Style); 9542 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 9543 " aaaaaaaaaaa aaaaaaaaa,\n" 9544 " aaaaaaaaaaa aaaaaaaaa,\n" 9545 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9546 ");", 9547 Style); 9548 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 9549 " aaaaaaaaaaaaaaa,\n" 9550 " aaaaaaaaaaaaaaaaaaaaa,\n" 9551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9552 "));", 9553 Style); 9554 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 9555 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" 9556 "));", 9557 Style); 9558 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 9559 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" 9560 "));", 9561 Style); 9562 verifyFormat( 9563 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 9564 " aaaaaaaaaaaaaaaaaaaaa(\n" 9565 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" 9566 " ),\n" 9567 " aaaaaaaaaaaaaaaa\n" 9568 ");", 9569 Style); 9570 verifyFormat( 9571 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 9572 " aaaaaaaaaaaaaaaaaaaaa(\n" 9573 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" 9574 " ) &&\n" 9575 " aaaaaaaaaaaaaaaa\n" 9576 ");", 9577 Style); 9578 verifyFormat("void foo(\n" 9579 " void (*foobarpntr)(\n" 9580 " aaaaaaaaaaaaaaaaaa *,\n" 9581 " bbbbbbbbbbbbbb *,\n" 9582 " cccccccccccccccccccc *,\n" 9583 " dddddddddddddddddd *\n" 9584 " )\n" 9585 ");", 9586 Style); 9587 verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n" 9588 " aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" 9589 "};", 9590 Style); 9591 9592 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9593 " const bool &aaaaaaaaa, const void *aaaaaaaaaa\n" 9594 ") const {\n" 9595 " return true;\n" 9596 "}", 9597 Style); 9598 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaa(\n" 9599 " const bool &aaaaaaaaaa, const void *aaaaaaaaaa\n" 9600 ") const;", 9601 Style); 9602 verifyFormat("void aaaaaaaaa(\n" 9603 " int aaaaaa, int bbbbbb, int cccccc, int dddddddddd\n" 9604 ") const noexcept -> std::vector<of_very_long_type>;", 9605 Style); 9606 verifyFormat( 9607 "x = aaaaaaaaaaaaaaa(\n" 9608 " \"a aaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa\"\n" 9609 ");", 9610 Style); 9611 Style.ColumnLimit = 60; 9612 verifyFormat("auto lambda =\n" 9613 " [&b](\n" 9614 " auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9615 " ) {};", 9616 Style); 9617 } 9618 9619 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 9620 FormatStyle Style = getLLVMStyleWithColumns(40); 9621 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 9622 " bbbbbbbbbbbbbbbbbbbbbb);", 9623 Style); 9624 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 9625 Style.AlignOperands = FormatStyle::OAS_DontAlign; 9626 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 9627 " bbbbbbbbbbbbbbbbbbbbbb);", 9628 Style); 9629 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9630 Style.AlignOperands = FormatStyle::OAS_Align; 9631 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 9632 " bbbbbbbbbbbbbbbbbbbbbb);", 9633 Style); 9634 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9635 Style.AlignOperands = FormatStyle::OAS_DontAlign; 9636 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 9637 " bbbbbbbbbbbbbbbbbbbbbb);", 9638 Style); 9639 } 9640 9641 TEST_F(FormatTest, BreaksConditionalExpressions) { 9642 verifyFormat( 9643 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9644 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9645 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9646 verifyFormat( 9647 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 9648 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9649 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9650 verifyFormat( 9651 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9652 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9653 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 9654 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9655 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9656 verifyFormat( 9657 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 9658 " : aaaaaaaaaaaaa);"); 9659 verifyFormat( 9660 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9661 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9662 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9663 " aaaaaaaaaaaaa);"); 9664 verifyFormat( 9665 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9666 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9667 " aaaaaaaaaaaaa);"); 9668 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9669 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9670 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9671 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9672 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9673 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9674 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9675 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9676 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9677 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9678 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 9679 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9680 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9681 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9682 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9683 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 9684 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9685 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9686 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9687 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9688 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 9689 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9690 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9691 " : aaaaaaaaaaaaaaaa;"); 9692 verifyFormat( 9693 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9694 " ? aaaaaaaaaaaaaaa\n" 9695 " : aaaaaaaaaaaaaaa;"); 9696 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 9697 " aaaaaaaaa\n" 9698 " ? b\n" 9699 " : c);"); 9700 verifyFormat("return aaaa == bbbb\n" 9701 " // comment\n" 9702 " ? aaaa\n" 9703 " : bbbb;"); 9704 verifyFormat("unsigned Indent =\n" 9705 " format(TheLine.First,\n" 9706 " IndentForLevel[TheLine.Level] >= 0\n" 9707 " ? IndentForLevel[TheLine.Level]\n" 9708 " : TheLine * 2,\n" 9709 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 9710 getLLVMStyleWithColumns(60)); 9711 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 9712 " ? aaaaaaaaaaaaaaa\n" 9713 " : bbbbbbbbbbbbbbb //\n" 9714 " ? ccccccccccccccc\n" 9715 " : ddddddddddddddd;"); 9716 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 9717 " ? aaaaaaaaaaaaaaa\n" 9718 " : (bbbbbbbbbbbbbbb //\n" 9719 " ? ccccccccccccccc\n" 9720 " : ddddddddddddddd);"); 9721 verifyFormat( 9722 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9723 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 9724 " aaaaaaaaaaaaaaaaaaaaa +\n" 9725 " aaaaaaaaaaaaaaaaaaaaa\n" 9726 " : aaaaaaaaaa;"); 9727 verifyFormat( 9728 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9729 " : aaaaaaaaaaaaaaaaaaaaaa\n" 9730 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9731 9732 FormatStyle NoBinPacking = getLLVMStyle(); 9733 NoBinPacking.BinPackArguments = false; 9734 verifyFormat( 9735 "void f() {\n" 9736 " g(aaa,\n" 9737 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 9738 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9739 " ? aaaaaaaaaaaaaaa\n" 9740 " : aaaaaaaaaaaaaaa);\n" 9741 "}", 9742 NoBinPacking); 9743 verifyFormat( 9744 "void f() {\n" 9745 " g(aaa,\n" 9746 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 9747 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9748 " ?: aaaaaaaaaaaaaaa);\n" 9749 "}", 9750 NoBinPacking); 9751 9752 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 9753 " // comment.\n" 9754 " ccccccccccccccccccccccccccccccccccccccc\n" 9755 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9756 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 9757 9758 // Assignments in conditional expressions. Apparently not uncommon :-(. 9759 verifyFormat("return a != b\n" 9760 " // comment\n" 9761 " ? a = b\n" 9762 " : a = b;"); 9763 verifyFormat("return a != b\n" 9764 " // comment\n" 9765 " ? a = a != b\n" 9766 " // comment\n" 9767 " ? a = b\n" 9768 " : a\n" 9769 " : a;"); 9770 verifyFormat("return a != b\n" 9771 " // comment\n" 9772 " ? a\n" 9773 " : a = a != b\n" 9774 " // comment\n" 9775 " ? a = b\n" 9776 " : a;"); 9777 9778 // Chained conditionals 9779 FormatStyle Style = getLLVMStyleWithColumns(70); 9780 Style.AlignOperands = FormatStyle::OAS_Align; 9781 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 9782 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9783 " : 3333333333333333;", 9784 Style); 9785 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 9786 " : bbbbbbbbbb ? 2222222222222222\n" 9787 " : 3333333333333333;", 9788 Style); 9789 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 9790 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 9791 " : 3333333333333333;", 9792 Style); 9793 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 9794 " : bbbbbbbbbbbbbb ? 222222\n" 9795 " : 333333;", 9796 Style); 9797 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 9798 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9799 " : cccccccccccccc ? 3333333333333333\n" 9800 " : 4444444444444444;", 9801 Style); 9802 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 9803 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9804 " : 3333333333333333;", 9805 Style); 9806 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 9807 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9808 " : (aaa ? bbb : ccc);", 9809 Style); 9810 verifyFormat( 9811 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9812 " : cccccccccccccccccc)\n" 9813 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9814 " : 3333333333333333;", 9815 Style); 9816 verifyFormat( 9817 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9818 " : cccccccccccccccccc)\n" 9819 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9820 " : 3333333333333333;", 9821 Style); 9822 verifyFormat( 9823 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9824 " : dddddddddddddddddd)\n" 9825 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9826 " : 3333333333333333;", 9827 Style); 9828 verifyFormat( 9829 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9830 " : dddddddddddddddddd)\n" 9831 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9832 " : 3333333333333333;", 9833 Style); 9834 verifyFormat( 9835 "return aaaaaaaaa ? 1111111111111111\n" 9836 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9837 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9838 " : dddddddddddddddddd)", 9839 Style); 9840 verifyFormat( 9841 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 9842 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9843 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9844 " : cccccccccccccccccc);", 9845 Style); 9846 verifyFormat( 9847 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9848 " : ccccccccccccccc ? dddddddddddddddddd\n" 9849 " : eeeeeeeeeeeeeeeeee)\n" 9850 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9851 " : 3333333333333333;", 9852 Style); 9853 verifyFormat( 9854 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9855 " : ccccccccccccccc ? dddddddddddddddddd\n" 9856 " : eeeeeeeeeeeeeeeeee)\n" 9857 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9858 " : 3333333333333333;", 9859 Style); 9860 verifyFormat( 9861 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9862 " : cccccccccccc ? dddddddddddddddddd\n" 9863 " : eeeeeeeeeeeeeeeeee)\n" 9864 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9865 " : 3333333333333333;", 9866 Style); 9867 verifyFormat( 9868 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9869 " : cccccccccccccccccc\n" 9870 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9871 " : 3333333333333333;", 9872 Style); 9873 verifyFormat( 9874 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9875 " : cccccccccccccccc ? dddddddddddddddddd\n" 9876 " : eeeeeeeeeeeeeeeeee\n" 9877 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 9878 " : 3333333333333333;", 9879 Style); 9880 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 9881 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9882 " : cccccccccccccccccc ? dddddddddddddddddd\n" 9883 " : eeeeeeeeeeeeeeeeee)\n" 9884 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 9885 " : 3333333333333333;", 9886 Style); 9887 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 9888 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 9889 " : cccccccccccccccc ? dddddddddddddddddd\n" 9890 " : eeeeeeeeeeeeeeeeee\n" 9891 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 9892 " : 3333333333333333;", 9893 Style); 9894 9895 Style.AlignOperands = FormatStyle::OAS_DontAlign; 9896 Style.BreakBeforeTernaryOperators = false; 9897 // FIXME: Aligning the question marks is weird given DontAlign. 9898 // Consider disabling this alignment in this case. Also check whether this 9899 // will render the adjustment from https://reviews.llvm.org/D82199 9900 // unnecessary. 9901 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 9902 " bbbb ? cccccccccccccccccc :\n" 9903 " ddddd;", 9904 Style); 9905 9906 verifyFormat( 9907 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 9908 " /*\n" 9909 " */\n" 9910 " function() {\n" 9911 " try {\n" 9912 " return JJJJJJJJJJJJJJ(\n" 9913 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 9914 " }\n" 9915 " } :\n" 9916 " function() {};", 9917 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 9918 " /*\n" 9919 " */\n" 9920 " function() {\n" 9921 " try {\n" 9922 " return JJJJJJJJJJJJJJ(\n" 9923 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 9924 " }\n" 9925 " } :\n" 9926 " function() {};", 9927 getGoogleStyle(FormatStyle::LK_JavaScript)); 9928 } 9929 9930 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 9931 FormatStyle Style = getLLVMStyleWithColumns(70); 9932 Style.BreakBeforeTernaryOperators = false; 9933 verifyFormat( 9934 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 9935 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 9936 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9937 Style); 9938 verifyFormat( 9939 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 9940 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 9941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9942 Style); 9943 verifyFormat( 9944 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 9945 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9946 Style); 9947 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 9948 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 9949 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9950 Style); 9951 verifyFormat( 9952 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 9953 " aaaaaaaaaaaaa);", 9954 Style); 9955 verifyFormat( 9956 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9957 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 9958 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9959 " aaaaaaaaaaaaa);", 9960 Style); 9961 verifyFormat( 9962 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9963 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9964 " aaaaaaaaaaaaa);", 9965 Style); 9966 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 9967 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9968 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 9969 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9970 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9971 Style); 9972 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9973 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 9974 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9975 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 9976 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9977 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 9978 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9979 Style); 9980 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9981 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 9982 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9983 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 9984 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9985 Style); 9986 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 9987 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 9988 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 9989 Style); 9990 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 9991 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 9992 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 9993 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 9994 Style); 9995 verifyFormat( 9996 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 9997 " aaaaaaaaaaaaaaa :\n" 9998 " aaaaaaaaaaaaaaa;", 9999 Style); 10000 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 10001 " aaaaaaaaa ?\n" 10002 " b :\n" 10003 " c);", 10004 Style); 10005 verifyFormat("unsigned Indent =\n" 10006 " format(TheLine.First,\n" 10007 " IndentForLevel[TheLine.Level] >= 0 ?\n" 10008 " IndentForLevel[TheLine.Level] :\n" 10009 " TheLine * 2,\n" 10010 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 10011 Style); 10012 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 10013 " aaaaaaaaaaaaaaa :\n" 10014 " bbbbbbbbbbbbbbb ? //\n" 10015 " ccccccccccccccc :\n" 10016 " ddddddddddddddd;", 10017 Style); 10018 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 10019 " aaaaaaaaaaaaaaa :\n" 10020 " (bbbbbbbbbbbbbbb ? //\n" 10021 " ccccccccccccccc :\n" 10022 " ddddddddddddddd);", 10023 Style); 10024 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 10025 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 10026 " ccccccccccccccccccccccccccc;", 10027 Style); 10028 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 10029 " aaaaa :\n" 10030 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 10031 Style); 10032 10033 // Chained conditionals 10034 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 10035 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10036 " 3333333333333333;", 10037 Style); 10038 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 10039 " bbbbbbbbbb ? 2222222222222222 :\n" 10040 " 3333333333333333;", 10041 Style); 10042 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 10043 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10044 " 3333333333333333;", 10045 Style); 10046 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 10047 " bbbbbbbbbbbbbbbb ? 222222 :\n" 10048 " 333333;", 10049 Style); 10050 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 10051 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10052 " cccccccccccccccc ? 3333333333333333 :\n" 10053 " 4444444444444444;", 10054 Style); 10055 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 10056 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10057 " 3333333333333333;", 10058 Style); 10059 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 10060 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10061 " (aaa ? bbb : ccc);", 10062 Style); 10063 verifyFormat( 10064 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10065 " cccccccccccccccccc) :\n" 10066 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10067 " 3333333333333333;", 10068 Style); 10069 verifyFormat( 10070 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10071 " cccccccccccccccccc) :\n" 10072 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10073 " 3333333333333333;", 10074 Style); 10075 verifyFormat( 10076 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10077 " dddddddddddddddddd) :\n" 10078 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10079 " 3333333333333333;", 10080 Style); 10081 verifyFormat( 10082 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10083 " dddddddddddddddddd) :\n" 10084 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10085 " 3333333333333333;", 10086 Style); 10087 verifyFormat( 10088 "return aaaaaaaaa ? 1111111111111111 :\n" 10089 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10090 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10091 " dddddddddddddddddd)", 10092 Style); 10093 verifyFormat( 10094 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 10095 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10096 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10097 " cccccccccccccccccc);", 10098 Style); 10099 verifyFormat( 10100 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10101 " ccccccccccccccccc ? dddddddddddddddddd :\n" 10102 " eeeeeeeeeeeeeeeeee) :\n" 10103 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10104 " 3333333333333333;", 10105 Style); 10106 verifyFormat( 10107 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10108 " ccccccccccccc ? dddddddddddddddddd :\n" 10109 " eeeeeeeeeeeeeeeeee) :\n" 10110 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10111 " 3333333333333333;", 10112 Style); 10113 verifyFormat( 10114 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10115 " ccccccccccccccccc ? dddddddddddddddddd :\n" 10116 " eeeeeeeeeeeeeeeeee) :\n" 10117 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10118 " 3333333333333333;", 10119 Style); 10120 verifyFormat( 10121 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10122 " cccccccccccccccccc :\n" 10123 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10124 " 3333333333333333;", 10125 Style); 10126 verifyFormat( 10127 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10128 " cccccccccccccccccc ? dddddddddddddddddd :\n" 10129 " eeeeeeeeeeeeeeeeee :\n" 10130 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10131 " 3333333333333333;", 10132 Style); 10133 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 10134 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10135 " cccccccccccccccccc ? dddddddddddddddddd :\n" 10136 " eeeeeeeeeeeeeeeeee) :\n" 10137 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10138 " 3333333333333333;", 10139 Style); 10140 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 10141 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 10142 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 10143 " eeeeeeeeeeeeeeeeee :\n" 10144 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 10145 " 3333333333333333;", 10146 Style); 10147 } 10148 10149 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 10150 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 10151 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 10152 verifyFormat("bool a = true, b = false;"); 10153 10154 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10155 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 10156 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 10157 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 10158 verifyFormat( 10159 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 10160 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 10161 " d = e && f;"); 10162 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 10163 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 10164 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 10165 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 10166 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 10167 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 10168 10169 FormatStyle Style = getGoogleStyle(); 10170 Style.PointerAlignment = FormatStyle::PAS_Left; 10171 Style.DerivePointerAlignment = false; 10172 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10173 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 10174 " *b = bbbbbbbbbbbbbbbbbbb;", 10175 Style); 10176 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 10177 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 10178 Style); 10179 verifyFormat("vector<int*> a, b;", Style); 10180 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 10181 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style); 10182 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style); 10183 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}", 10184 Style); 10185 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}", 10186 Style); 10187 verifyFormat( 10188 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}", 10189 Style); 10190 10191 verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style); 10192 verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style); 10193 verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style); 10194 verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style); 10195 verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}", 10196 Style); 10197 } 10198 10199 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 10200 verifyFormat("arr[foo ? bar : baz];"); 10201 verifyFormat("f()[foo ? bar : baz];"); 10202 verifyFormat("(a + b)[foo ? bar : baz];"); 10203 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 10204 } 10205 10206 TEST_F(FormatTest, AlignsStringLiterals) { 10207 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 10208 " \"short literal\");"); 10209 verifyFormat( 10210 "looooooooooooooooooooooooongFunction(\n" 10211 " \"short literal\"\n" 10212 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 10213 verifyFormat("someFunction(\"Always break between multi-line\"\n" 10214 " \" string literals\",\n" 10215 " also, other, parameters);"); 10216 verifyFormat("fun + \"1243\" /* comment */\n" 10217 " \"5678\";", 10218 "fun + \"1243\" /* comment */\n" 10219 " \"5678\";", 10220 getLLVMStyleWithColumns(28)); 10221 verifyFormat( 10222 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 10223 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 10224 " \"aaaaaaaaaaaaaaaa\";", 10225 "aaaaaa =" 10226 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 10227 "aaaaaaaaaaaaaaaaaaaaa\" " 10228 "\"aaaaaaaaaaaaaaaa\";"); 10229 verifyFormat("a = a + \"a\"\n" 10230 " \"a\"\n" 10231 " \"a\";"); 10232 verifyFormat("f(\"a\", \"b\"\n" 10233 " \"c\");"); 10234 10235 verifyFormat( 10236 "#define LL_FORMAT \"ll\"\n" 10237 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 10238 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 10239 10240 verifyFormat("#define A(X) \\\n" 10241 " \"aaaaa\" #X \"bbbbbb\" \\\n" 10242 " \"ccccc\"", 10243 getLLVMStyleWithColumns(23)); 10244 verifyFormat("#define A \"def\"\n" 10245 "f(\"abc\" A \"ghi\"\n" 10246 " \"jkl\");"); 10247 10248 verifyFormat("f(L\"a\"\n" 10249 " L\"b\");"); 10250 verifyFormat("#define A(X) \\\n" 10251 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 10252 " L\"ccccc\"", 10253 getLLVMStyleWithColumns(25)); 10254 10255 verifyFormat("f(@\"a\"\n" 10256 " @\"b\");"); 10257 verifyFormat("NSString s = @\"a\"\n" 10258 " @\"b\"\n" 10259 " @\"c\";"); 10260 verifyFormat("NSString s = @\"a\"\n" 10261 " \"b\"\n" 10262 " \"c\";"); 10263 } 10264 10265 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 10266 FormatStyle Style = getLLVMStyle(); 10267 Style.ColumnLimit = 60; 10268 10269 // No declarations or definitions should be moved to own line. 10270 Style.BreakAfterReturnType = FormatStyle::RTBS_None; 10271 verifyFormat("class A {\n" 10272 " int f() { return 1; }\n" 10273 " int g();\n" 10274 " long\n" 10275 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n" 10276 "};\n" 10277 "int f() { return 1; }\n" 10278 "int g();\n" 10279 "int foooooooooooooooooooooooooooo::\n" 10280 " baaaaaaaaaaaaaaaaaaaaar();", 10281 Style); 10282 10283 // It is now allowed to break after a short return type if necessary. 10284 Style.BreakAfterReturnType = FormatStyle::RTBS_Automatic; 10285 verifyFormat("class A {\n" 10286 " int f() { return 1; }\n" 10287 " int g();\n" 10288 " long\n" 10289 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n" 10290 "};\n" 10291 "int f() { return 1; }\n" 10292 "int g();\n" 10293 "int\n" 10294 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();", 10295 Style); 10296 10297 // It now must never break after a short return type. 10298 Style.BreakAfterReturnType = FormatStyle::RTBS_ExceptShortType; 10299 verifyFormat("class A {\n" 10300 " int f() { return 1; }\n" 10301 " int g();\n" 10302 " long foooooooooooooooooooooooooooo::\n" 10303 " baaaaaaaaaaaaaaaaaaaar();\n" 10304 "};\n" 10305 "int f() { return 1; }\n" 10306 "int g();\n" 10307 "int foooooooooooooooooooooooooooo::\n" 10308 " baaaaaaaaaaaaaaaaaaaaar();", 10309 Style); 10310 10311 // All declarations and definitions should have the return type moved to its 10312 // own line. 10313 Style.BreakAfterReturnType = FormatStyle::RTBS_All; 10314 Style.TypenameMacros = {"LIST"}; 10315 verifyFormat("SomeType\n" 10316 "funcdecl(LIST(uint64_t));", 10317 Style); 10318 verifyFormat("class E {\n" 10319 " int\n" 10320 " f() {\n" 10321 " return 1;\n" 10322 " }\n" 10323 " int\n" 10324 " g();\n" 10325 " long\n" 10326 " foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaar();\n" 10327 "};\n" 10328 "int\n" 10329 "f() {\n" 10330 " return 1;\n" 10331 "}\n" 10332 "int\n" 10333 "g();\n" 10334 "int\n" 10335 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();", 10336 Style); 10337 10338 // Top-level definitions, and no kinds of declarations should have the 10339 // return type moved to its own line. 10340 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 10341 verifyFormat("class B {\n" 10342 " int f() { return 1; }\n" 10343 " int g();\n" 10344 "};\n" 10345 "int\n" 10346 "f() {\n" 10347 " return 1;\n" 10348 "}\n" 10349 "int g();", 10350 Style); 10351 10352 // Top-level definitions and declarations should have the return type moved 10353 // to its own line. 10354 Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevel; 10355 verifyFormat("class C {\n" 10356 " int f() { return 1; }\n" 10357 " int g();\n" 10358 "};\n" 10359 "int\n" 10360 "f() {\n" 10361 " return 1;\n" 10362 "}\n" 10363 "int\n" 10364 "g();\n" 10365 "int\n" 10366 "foooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();", 10367 Style); 10368 10369 // All definitions should have the return type moved to its own line, but no 10370 // kinds of declarations. 10371 Style.BreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 10372 verifyFormat("class D {\n" 10373 " int\n" 10374 " f() {\n" 10375 " return 1;\n" 10376 " }\n" 10377 " int g();\n" 10378 "};\n" 10379 "int\n" 10380 "f() {\n" 10381 " return 1;\n" 10382 "}\n" 10383 "int g();", 10384 Style); 10385 verifyFormat("const char *\n" 10386 "f(void) {\n" // Break here. 10387 " return \"\";\n" 10388 "}\n" 10389 "const char *bar(void);", // No break here. 10390 Style); 10391 verifyFormat("template <class T>\n" 10392 "T *\n" 10393 "f(T &c) {\n" // Break here. 10394 " return NULL;\n" 10395 "}\n" 10396 "template <class T> T *f(T &c);", // No break here. 10397 Style); 10398 verifyFormat("class C {\n" 10399 " int\n" 10400 " operator+() {\n" 10401 " return 1;\n" 10402 " }\n" 10403 " int\n" 10404 " operator()() {\n" 10405 " return 1;\n" 10406 " }\n" 10407 "};", 10408 Style); 10409 verifyFormat("void\n" 10410 "A::operator()() {}\n" 10411 "void\n" 10412 "A::operator>>() {}\n" 10413 "void\n" 10414 "A::operator+() {}\n" 10415 "void\n" 10416 "A::operator*() {}\n" 10417 "void\n" 10418 "A::operator->() {}\n" 10419 "void\n" 10420 "A::operator void *() {}\n" 10421 "void\n" 10422 "A::operator void &() {}\n" 10423 "void\n" 10424 "A::operator void &&() {}\n" 10425 "void\n" 10426 "A::operator char *() {}\n" 10427 "void\n" 10428 "A::operator[]() {}\n" 10429 "void\n" 10430 "A::operator!() {}\n" 10431 "void\n" 10432 "A::operator**() {}\n" 10433 "void\n" 10434 "A::operator<Foo> *() {}\n" 10435 "void\n" 10436 "A::operator<Foo> **() {}\n" 10437 "void\n" 10438 "A::operator<Foo> &() {}\n" 10439 "void\n" 10440 "A::operator void **() {}", 10441 Style); 10442 verifyFormat("constexpr auto\n" 10443 "operator()() const -> reference {}\n" 10444 "constexpr auto\n" 10445 "operator>>() const -> reference {}\n" 10446 "constexpr auto\n" 10447 "operator+() const -> reference {}\n" 10448 "constexpr auto\n" 10449 "operator*() const -> reference {}\n" 10450 "constexpr auto\n" 10451 "operator->() const -> reference {}\n" 10452 "constexpr auto\n" 10453 "operator++() const -> reference {}\n" 10454 "constexpr auto\n" 10455 "operator void *() const -> reference {}\n" 10456 "constexpr auto\n" 10457 "operator void **() const -> reference {}\n" 10458 "constexpr auto\n" 10459 "operator void *() const -> reference {}\n" 10460 "constexpr auto\n" 10461 "operator void &() const -> reference {}\n" 10462 "constexpr auto\n" 10463 "operator void &&() const -> reference {}\n" 10464 "constexpr auto\n" 10465 "operator char *() const -> reference {}\n" 10466 "constexpr auto\n" 10467 "operator!() const -> reference {}\n" 10468 "constexpr auto\n" 10469 "operator[]() const -> reference {}", 10470 Style); 10471 verifyFormat("void *operator new(std::size_t s);", // No break here. 10472 Style); 10473 verifyFormat("void *\n" 10474 "operator new(std::size_t s) {}", 10475 Style); 10476 verifyFormat("void *\n" 10477 "operator delete[](void *ptr) {}", 10478 Style); 10479 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10480 verifyFormat("const char *\n" 10481 "f(void)\n" // Break here. 10482 "{\n" 10483 " return \"\";\n" 10484 "}\n" 10485 "const char *bar(void);", // No break here. 10486 Style); 10487 verifyFormat("template <class T>\n" 10488 "T *\n" // Problem here: no line break 10489 "f(T &c)\n" // Break here. 10490 "{\n" 10491 " return NULL;\n" 10492 "}\n" 10493 "template <class T> T *f(T &c);", // No break here. 10494 Style); 10495 verifyFormat("int\n" 10496 "foo(A<bool> a)\n" 10497 "{\n" 10498 " return a;\n" 10499 "}", 10500 Style); 10501 verifyFormat("int\n" 10502 "foo(A<8> a)\n" 10503 "{\n" 10504 " return a;\n" 10505 "}", 10506 Style); 10507 verifyFormat("int\n" 10508 "foo(A<B<bool>, 8> a)\n" 10509 "{\n" 10510 " return a;\n" 10511 "}", 10512 Style); 10513 verifyFormat("int\n" 10514 "foo(A<B<8>, bool> a)\n" 10515 "{\n" 10516 " return a;\n" 10517 "}", 10518 Style); 10519 verifyFormat("int\n" 10520 "foo(A<B<bool>, bool> a)\n" 10521 "{\n" 10522 " return a;\n" 10523 "}", 10524 Style); 10525 verifyFormat("int\n" 10526 "foo(A<B<8>, 8> a)\n" 10527 "{\n" 10528 " return a;\n" 10529 "}", 10530 Style); 10531 10532 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 10533 Style.BraceWrapping.AfterFunction = true; 10534 verifyFormat("int f(i);\n" // No break here. 10535 "int\n" // Break here. 10536 "f(i)\n" 10537 "{\n" 10538 " return i + 1;\n" 10539 "}\n" 10540 "int\n" // Break here. 10541 "f(i)\n" 10542 "{\n" 10543 " return i + 1;\n" 10544 "};", 10545 Style); 10546 verifyFormat("int f(a, b, c);\n" // No break here. 10547 "int\n" // Break here. 10548 "f(a, b, c)\n" // Break here. 10549 "short a, b;\n" 10550 "float c;\n" 10551 "{\n" 10552 " return a + b < c;\n" 10553 "}\n" 10554 "int\n" // Break here. 10555 "f(a, b, c)\n" // Break here. 10556 "short a, b;\n" 10557 "float c;\n" 10558 "{\n" 10559 " return a + b < c;\n" 10560 "};", 10561 Style); 10562 verifyFormat("byte *\n" // Break here. 10563 "f(a)\n" // Break here. 10564 "byte a[];\n" 10565 "{\n" 10566 " return a;\n" 10567 "}", 10568 Style); 10569 verifyFormat("byte *\n" 10570 "f(a)\n" 10571 "byte /* K&R C */ a[];\n" 10572 "{\n" 10573 " return a;\n" 10574 "}\n" 10575 "byte *\n" 10576 "g(p)\n" 10577 "byte /* K&R C */ *p;\n" 10578 "{\n" 10579 " return p;\n" 10580 "}", 10581 Style); 10582 verifyFormat("bool f(int a, int) override;\n" 10583 "Bar g(int a, Bar) final;\n" 10584 "Bar h(a, Bar) final;", 10585 Style); 10586 verifyFormat("int\n" 10587 "f(a)", 10588 Style); 10589 verifyFormat("bool\n" 10590 "f(size_t = 0, bool b = false)\n" 10591 "{\n" 10592 " return !b;\n" 10593 "}", 10594 Style); 10595 10596 // The return breaking style doesn't affect: 10597 // * function and object definitions with attribute-like macros 10598 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 10599 " ABSL_GUARDED_BY(mutex) = {};", 10600 getGoogleStyleWithColumns(40)); 10601 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 10602 " ABSL_GUARDED_BY(mutex); // comment", 10603 getGoogleStyleWithColumns(40)); 10604 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 10605 " ABSL_GUARDED_BY(mutex1)\n" 10606 " ABSL_GUARDED_BY(mutex2);", 10607 getGoogleStyleWithColumns(40)); 10608 verifyFormat("Tttttt f(int a, int b)\n" 10609 " ABSL_GUARDED_BY(mutex1)\n" 10610 " ABSL_GUARDED_BY(mutex2);", 10611 getGoogleStyleWithColumns(40)); 10612 // * typedefs 10613 verifyGoogleFormat("typedef ATTR(X) char x;"); 10614 10615 Style = getGNUStyle(); 10616 10617 // Test for comments at the end of function declarations. 10618 verifyFormat("void\n" 10619 "foo (int a, /*abc*/ int b) // def\n" 10620 "{\n" 10621 "}", 10622 Style); 10623 10624 verifyFormat("void\n" 10625 "foo (int a, /* abc */ int b) /* def */\n" 10626 "{\n" 10627 "}", 10628 Style); 10629 10630 // Definitions that should not break after return type 10631 verifyFormat("void foo (int a, int b); // def", Style); 10632 verifyFormat("void foo (int a, int b); /* def */", Style); 10633 verifyFormat("void foo (int a, int b);", Style); 10634 } 10635 10636 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 10637 FormatStyle NoBreak = getLLVMStyle(); 10638 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 10639 FormatStyle Break = getLLVMStyle(); 10640 Break.AlwaysBreakBeforeMultilineStrings = true; 10641 verifyFormat("aaaa = \"bbbb\"\n" 10642 " \"cccc\";", 10643 NoBreak); 10644 verifyFormat("aaaa =\n" 10645 " \"bbbb\"\n" 10646 " \"cccc\";", 10647 Break); 10648 verifyFormat("aaaa(\"bbbb\"\n" 10649 " \"cccc\");", 10650 NoBreak); 10651 verifyFormat("aaaa(\n" 10652 " \"bbbb\"\n" 10653 " \"cccc\");", 10654 Break); 10655 verifyFormat("aaaa(qqq, \"bbbb\"\n" 10656 " \"cccc\");", 10657 NoBreak); 10658 verifyFormat("aaaa(qqq,\n" 10659 " \"bbbb\"\n" 10660 " \"cccc\");", 10661 Break); 10662 verifyFormat("aaaa(qqq,\n" 10663 " L\"bbbb\"\n" 10664 " L\"cccc\");", 10665 Break); 10666 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 10667 " \"bbbb\"));", 10668 Break); 10669 verifyFormat("string s = someFunction(\n" 10670 " \"abc\"\n" 10671 " \"abc\");", 10672 Break); 10673 10674 // As we break before unary operators, breaking right after them is bad. 10675 verifyFormat("string foo = abc ? \"x\"\n" 10676 " \"blah blah blah blah blah blah\"\n" 10677 " : \"y\";", 10678 Break); 10679 10680 // Don't break if there is no column gain. 10681 verifyFormat("f(\"aaaa\"\n" 10682 " \"bbbb\");", 10683 Break); 10684 10685 // Treat literals with escaped newlines like multi-line string literals. 10686 verifyNoChange("x = \"a\\\n" 10687 "b\\\n" 10688 "c\";", 10689 NoBreak); 10690 verifyFormat("xxxx =\n" 10691 " \"a\\\n" 10692 "b\\\n" 10693 "c\";", 10694 "xxxx = \"a\\\n" 10695 "b\\\n" 10696 "c\";", 10697 Break); 10698 10699 verifyFormat("NSString *const kString =\n" 10700 " @\"aaaa\"\n" 10701 " @\"bbbb\";", 10702 "NSString *const kString = @\"aaaa\"\n" 10703 "@\"bbbb\";", 10704 Break); 10705 10706 Break.ColumnLimit = 0; 10707 verifyFormat("const char *hello = \"hello llvm\";", Break); 10708 } 10709 10710 TEST_F(FormatTest, AlignsPipes) { 10711 verifyFormat( 10712 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10713 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10714 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10715 verifyFormat( 10716 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 10717 " << aaaaaaaaaaaaaaaaaaaa;"); 10718 verifyFormat( 10719 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10720 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10721 verifyFormat( 10722 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 10723 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10724 verifyFormat( 10725 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 10726 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 10727 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 10728 verifyFormat( 10729 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10730 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10731 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10732 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10733 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10734 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10735 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10736 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 10737 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 10738 verifyFormat( 10739 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10740 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10741 verifyFormat( 10742 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 10743 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10744 10745 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 10746 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 10747 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10748 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10749 " aaaaaaaaaaaaaaaaaaaaa)\n" 10750 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10751 verifyFormat("LOG_IF(aaa == //\n" 10752 " bbb)\n" 10753 " << a << b;"); 10754 10755 // But sometimes, breaking before the first "<<" is desirable. 10756 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 10757 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 10758 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 10759 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10760 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10761 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 10762 " << BEF << IsTemplate << Description << E->getType();"); 10763 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 10764 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10766 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 10767 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10768 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10769 " << aaa;"); 10770 10771 verifyFormat( 10772 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10773 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 10774 10775 // Incomplete string literal. 10776 verifyFormat("llvm::errs() << \"\n" 10777 " << a;", 10778 "llvm::errs() << \"\n<<a;"); 10779 10780 verifyFormat("void f() {\n" 10781 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 10782 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 10783 "}"); 10784 10785 // Handle 'endl'. 10786 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 10787 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 10788 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 10789 10790 // Handle '\n'. 10791 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 10792 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 10793 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 10794 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 10795 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 10796 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 10797 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 10798 } 10799 10800 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 10801 verifyFormat("return out << \"somepacket = {\\n\"\n" 10802 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 10803 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 10804 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 10805 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 10806 " << \"}\";"); 10807 10808 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 10809 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 10810 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 10811 verifyFormat( 10812 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 10813 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 10814 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 10815 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 10816 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 10817 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 10818 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10819 verifyFormat( 10820 "void f() {\n" 10821 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 10822 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 10823 "}"); 10824 10825 // Breaking before the first "<<" is generally not desirable. 10826 verifyFormat( 10827 "llvm::errs()\n" 10828 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10829 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10830 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10831 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 10832 getLLVMStyleWithColumns(70)); 10833 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 10834 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10835 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 10836 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10837 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 10838 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 10839 getLLVMStyleWithColumns(70)); 10840 10841 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 10842 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 10843 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 10844 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 10845 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 10846 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 10847 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 10848 " (aaaa + aaaa);", 10849 getLLVMStyleWithColumns(40)); 10850 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 10851 " (aaaaaaa + aaaaa));", 10852 getLLVMStyleWithColumns(40)); 10853 verifyFormat( 10854 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 10855 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 10856 " bbbbbbbbbbbbbbbbbbbbbbb);"); 10857 } 10858 10859 TEST_F(FormatTest, WrapBeforeInsertionOperatorbetweenStringLiterals) { 10860 verifyFormat("QStringList() << \"foo\" << \"bar\";"); 10861 10862 verifyNoChange("QStringList() << \"foo\"\n" 10863 " << \"bar\";"); 10864 10865 verifyFormat("log_error(log, \"foo\" << \"bar\");", 10866 "log_error(log, \"foo\"\n" 10867 " << \"bar\");"); 10868 } 10869 10870 TEST_F(FormatTest, UnderstandsEquals) { 10871 verifyFormat( 10872 "aaaaaaaaaaaaaaaaa =\n" 10873 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10874 verifyFormat( 10875 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10876 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 10877 verifyFormat( 10878 "if (a) {\n" 10879 " f();\n" 10880 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10881 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 10882 "}"); 10883 10884 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10885 " 100000000 + 10000000) {\n}"); 10886 } 10887 10888 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 10889 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 10890 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 10891 10892 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 10893 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 10894 10895 verifyFormat( 10896 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 10897 " Parameter2);"); 10898 10899 verifyFormat( 10900 "ShortObject->shortFunction(\n" 10901 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 10902 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 10903 10904 verifyFormat("loooooooooooooongFunction(\n" 10905 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 10906 10907 verifyFormat( 10908 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 10909 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 10910 10911 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 10912 " .WillRepeatedly(Return(SomeValue));"); 10913 verifyFormat("void f() {\n" 10914 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 10915 " .Times(2)\n" 10916 " .WillRepeatedly(Return(SomeValue));\n" 10917 "}"); 10918 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 10919 " ccccccccccccccccccccccc);"); 10920 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10922 " .aaaaa(aaaaa),\n" 10923 " aaaaaaaaaaaaaaaaaaaaa);"); 10924 verifyFormat("void f() {\n" 10925 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10926 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 10927 "}"); 10928 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10929 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10930 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10931 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10932 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 10933 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10934 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10935 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10936 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 10937 "}"); 10938 10939 // Here, it is not necessary to wrap at "." or "->". 10940 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 10941 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 10942 verifyFormat( 10943 "aaaaaaaaaaa->aaaaaaaaa(\n" 10944 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10945 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));"); 10946 10947 verifyFormat( 10948 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10949 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 10950 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 10951 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 10952 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 10953 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 10954 10955 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10956 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10957 " .a();"); 10958 10959 FormatStyle NoBinPacking = getLLVMStyle(); 10960 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; 10961 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 10962 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 10963 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 10964 " aaaaaaaaaaaaaaaaaaa,\n" 10965 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10966 NoBinPacking); 10967 10968 // If there is a subsequent call, change to hanging indentation. 10969 verifyFormat( 10970 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10971 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 10972 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 10973 verifyFormat( 10974 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10975 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 10976 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10977 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10978 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 10979 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10980 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10981 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 10982 } 10983 10984 TEST_F(FormatTest, WrapsTemplateDeclarations) { 10985 verifyFormat("template <typename T>\n" 10986 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 10987 verifyFormat("template <typename T>\n" 10988 "// T should be one of {A, B}.\n" 10989 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 10990 verifyFormat( 10991 "template <typename T>\n" 10992 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 10993 verifyFormat("template <typename T>\n" 10994 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 10995 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 10996 verifyFormat( 10997 "template <typename T>\n" 10998 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 10999 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 11000 verifyFormat( 11001 "template <typename T>\n" 11002 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 11003 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 11004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11005 verifyFormat("template <typename T>\n" 11006 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11007 " int aaaaaaaaaaaaaaaaaaaaaa);"); 11008 verifyFormat( 11009 "template <typename T1, typename T2 = char, typename T3 = char,\n" 11010 " typename T4 = char>\n" 11011 "void f();"); 11012 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 11013 " template <typename> class cccccccccccccccccccccc,\n" 11014 " typename ddddddddddddd>\n" 11015 "class C {};"); 11016 verifyFormat( 11017 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 11018 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11019 11020 verifyFormat("void f() {\n" 11021 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 11022 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 11023 "}"); 11024 11025 verifyFormat("template <typename T> class C {};"); 11026 verifyFormat("template <typename T> void f();"); 11027 verifyFormat("template <typename T> void f() {}"); 11028 verifyFormat( 11029 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 11030 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11031 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 11032 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 11033 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11034 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 11035 " bbbbbbbbbbbbbbbbbbbbbbbb);", 11036 getLLVMStyleWithColumns(72)); 11037 verifyFormat("static_cast<A< //\n" 11038 " B> *>(\n" 11039 "\n" 11040 ");", 11041 "static_cast<A<//\n" 11042 " B>*>(\n" 11043 "\n" 11044 " );"); 11045 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11046 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 11047 11048 FormatStyle AlwaysBreak = getLLVMStyle(); 11049 AlwaysBreak.BreakTemplateDeclarations = FormatStyle::BTDS_Yes; 11050 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 11051 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 11052 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 11053 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11054 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 11055 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 11056 verifyFormat("template <template <typename> class Fooooooo,\n" 11057 " template <typename> class Baaaaaaar>\n" 11058 "struct C {};", 11059 AlwaysBreak); 11060 verifyFormat("template <typename T> // T can be A, B or C.\n" 11061 "struct C {};", 11062 AlwaysBreak); 11063 verifyFormat("template <typename T>\n" 11064 "C(T) noexcept;", 11065 AlwaysBreak); 11066 verifyFormat("template <typename T>\n" 11067 "ClassName(T) noexcept;", 11068 AlwaysBreak); 11069 verifyFormat("template <typename T>\n" 11070 "POOR_NAME(T) noexcept;", 11071 AlwaysBreak); 11072 verifyFormat("template <enum E> class A {\n" 11073 "public:\n" 11074 " E *f();\n" 11075 "};"); 11076 11077 FormatStyle NeverBreak = getLLVMStyle(); 11078 NeverBreak.BreakTemplateDeclarations = FormatStyle::BTDS_No; 11079 verifyFormat("template <typename T> class C {};", NeverBreak); 11080 verifyFormat("template <typename T> void f();", NeverBreak); 11081 verifyFormat("template <typename T> void f() {}", NeverBreak); 11082 verifyFormat("template <typename T> C(T) noexcept;", NeverBreak); 11083 verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak); 11084 verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak); 11085 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 11086 "bbbbbbbbbbbbbbbbbbbb) {}", 11087 NeverBreak); 11088 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11089 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 11090 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 11091 NeverBreak); 11092 verifyFormat("template <template <typename> class Fooooooo,\n" 11093 " template <typename> class Baaaaaaar>\n" 11094 "struct C {};", 11095 NeverBreak); 11096 verifyFormat("template <typename T> // T can be A, B or C.\n" 11097 "struct C {};", 11098 NeverBreak); 11099 verifyFormat("template <enum E> class A {\n" 11100 "public:\n" 11101 " E *f();\n" 11102 "};", 11103 NeverBreak); 11104 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 11105 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 11106 "bbbbbbbbbbbbbbbbbbbb) {}", 11107 NeverBreak); 11108 11109 auto Style = getLLVMStyle(); 11110 Style.BreakTemplateDeclarations = FormatStyle::BTDS_Leave; 11111 11112 verifyNoChange("template <typename T>\n" 11113 "class C {};", 11114 Style); 11115 verifyFormat("template <typename T> class C {};", Style); 11116 11117 verifyNoChange("template <typename T>\n" 11118 "void f();", 11119 Style); 11120 verifyFormat("template <typename T> void f();", Style); 11121 11122 verifyNoChange("template <typename T>\n" 11123 "void f() {}", 11124 Style); 11125 verifyFormat("template <typename T> void f() {}", Style); 11126 11127 verifyNoChange("template <typename T>\n" 11128 "// T can be A, B or C.\n" 11129 "struct C {};", 11130 Style); 11131 verifyFormat("template <typename T> // T can be A, B or C.\n" 11132 "struct C {};", 11133 Style); 11134 11135 verifyNoChange("template <typename T>\n" 11136 "C(T) noexcept;", 11137 Style); 11138 verifyFormat("template <typename T> C(T) noexcept;", Style); 11139 11140 verifyNoChange("template <enum E>\n" 11141 "class A {\n" 11142 "public:\n" 11143 " E *f();\n" 11144 "};", 11145 Style); 11146 verifyFormat("template <enum E> class A {\n" 11147 "public:\n" 11148 " E *f();\n" 11149 "};", 11150 Style); 11151 11152 verifyNoChange("template <auto x>\n" 11153 "constexpr int simple(int) {\n" 11154 " char c;\n" 11155 " return 1;\n" 11156 "}", 11157 Style); 11158 verifyFormat("template <auto x> constexpr int simple(int) {\n" 11159 " char c;\n" 11160 " return 1;\n" 11161 "}", 11162 Style); 11163 11164 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; 11165 verifyNoChange("template <auto x>\n" 11166 "requires(x > 1)\n" 11167 "constexpr int with_req(int) {\n" 11168 " return 1;\n" 11169 "}", 11170 Style); 11171 verifyFormat("template <auto x> requires(x > 1)\n" 11172 "constexpr int with_req(int) {\n" 11173 " return 1;\n" 11174 "}", 11175 Style); 11176 } 11177 11178 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 11179 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11180 Style.ColumnLimit = 60; 11181 verifyFormat("// Baseline - no comments.\n" 11182 "template <\n" 11183 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 11184 "void f() {}", 11185 Style); 11186 11187 verifyFormat("template <\n" 11188 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 11189 "void f() {}", 11190 "template <\n" 11191 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 11192 "void f() {}", 11193 Style); 11194 11195 verifyFormat( 11196 "template <\n" 11197 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 11198 "void f() {}", 11199 "template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 11200 "void f() {}", 11201 Style); 11202 11203 verifyFormat("template <\n" 11204 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 11205 " // multiline\n" 11206 "void f() {}", 11207 "template <\n" 11208 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 11209 " // multiline\n" 11210 "void f() {}", 11211 Style); 11212 11213 verifyFormat( 11214 "template <typename aaaaaaaaaa<\n" 11215 " bbbbbbbbbbbb>::value> // trailing loooong\n" 11216 "void f() {}", 11217 "template <\n" 11218 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 11219 "void f() {}", 11220 Style); 11221 } 11222 11223 TEST_F(FormatTest, WrapsTemplateParameters) { 11224 FormatStyle Style = getLLVMStyle(); 11225 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 11226 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 11227 verifyFormat( 11228 "template <typename... a> struct q {};\n" 11229 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 11230 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 11231 " y;", 11232 Style); 11233 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 11234 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 11235 verifyFormat( 11236 "template <typename... a> struct r {};\n" 11237 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 11238 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 11239 " y;", 11240 Style); 11241 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11242 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 11243 verifyFormat("template <typename... a> struct s {};\n" 11244 "extern s<\n" 11245 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 11246 "aaaaaaaaaaaaaaaaaaaaaa,\n" 11247 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 11248 "aaaaaaaaaaaaaaaaaaaaaa>\n" 11249 " y;", 11250 Style); 11251 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11252 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 11253 verifyFormat("template <typename... a> struct t {};\n" 11254 "extern t<\n" 11255 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 11256 "aaaaaaaaaaaaaaaaaaaaaa,\n" 11257 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 11258 "aaaaaaaaaaaaaaaaaaaaaa>\n" 11259 " y;", 11260 Style); 11261 } 11262 11263 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 11264 verifyFormat( 11265 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 11266 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 11267 verifyFormat( 11268 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 11269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11270 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 11271 11272 // FIXME: Should we have the extra indent after the second break? 11273 verifyFormat( 11274 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 11275 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 11276 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 11277 11278 verifyFormat( 11279 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 11280 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 11281 11282 // Breaking at nested name specifiers is generally not desirable. 11283 verifyFormat( 11284 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11285 " aaaaaaaaaaaaaaaaaaaaaaa);"); 11286 11287 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 11288 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 11289 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11290 " aaaaaaaaaaaaaaaaaaaaa);", 11291 getLLVMStyleWithColumns(74)); 11292 11293 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 11294 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11295 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 11296 11297 verifyFormat( 11298 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n" 11299 " AndAnotherLongClassNameToShowTheIssue() {}\n" 11300 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n" 11301 " ~AndAnotherLongClassNameToShowTheIssue() {}"); 11302 } 11303 11304 TEST_F(FormatTest, UnderstandsTemplateParameters) { 11305 verifyFormat("A<int> a;"); 11306 verifyFormat("A<A<A<int>>> a;"); 11307 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 11308 verifyFormat("bool x = a < 1 || 2 > a;"); 11309 verifyFormat("bool x = 5 < f<int>();"); 11310 verifyFormat("bool x = f<int>() > 5;"); 11311 verifyFormat("bool x = 5 < a<int>::x;"); 11312 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 11313 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 11314 11315 verifyGoogleFormat("A<A<int>> a;"); 11316 verifyGoogleFormat("A<A<A<int>>> a;"); 11317 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 11318 verifyGoogleFormat("A<A<int> > a;"); 11319 verifyGoogleFormat("A<A<A<int> > > a;"); 11320 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 11321 verifyGoogleFormat("A<::A<int>> a;"); 11322 verifyGoogleFormat("A<::A> a;"); 11323 verifyGoogleFormat("A< ::A> a;"); 11324 verifyGoogleFormat("A< ::A<int> > a;"); 11325 verifyFormat("A<A<A<A>>> a;", "A<A<A<A> >> a;", getGoogleStyle()); 11326 verifyFormat("A<A<A<A>>> a;", "A<A<A<A>> > a;", getGoogleStyle()); 11327 verifyFormat("A<::A<int>> a;", "A< ::A<int>> a;", getGoogleStyle()); 11328 verifyFormat("A<::A<int>> a;", "A<::A<int> > a;", getGoogleStyle()); 11329 verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};", 11330 getGoogleStyle()); 11331 11332 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 11333 11334 // template closer followed by a token that starts with > or = 11335 verifyFormat("bool b = a<1> > 1;"); 11336 verifyFormat("bool b = a<1> >= 1;"); 11337 verifyFormat("int i = a<1> >> 1;"); 11338 FormatStyle Style = getLLVMStyle(); 11339 Style.SpaceBeforeAssignmentOperators = false; 11340 verifyFormat("bool b= a<1> == 1;", Style); 11341 verifyFormat("a<int> = 1;", Style); 11342 verifyFormat("a<int> >>= 1;", Style); 11343 11344 verifyFormat("test < a | b >> c;"); 11345 verifyFormat("test<test<a | b>> c;"); 11346 verifyFormat("test >> a >> b;"); 11347 verifyFormat("test << a >> b;"); 11348 11349 verifyFormat("f<int>();"); 11350 verifyFormat("template <typename T> void f() {}"); 11351 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 11352 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 11353 "sizeof(char)>::type>;"); 11354 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 11355 verifyFormat("f(a.operator()<A>());"); 11356 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11357 " .template operator()<A>());", 11358 getLLVMStyleWithColumns(35)); 11359 verifyFormat("bool_constant<a && noexcept(f())>;"); 11360 verifyFormat("bool_constant<a || noexcept(f())>;"); 11361 11362 verifyFormat("if (std::tuple_size_v<T> > 0)"); 11363 11364 // Not template parameters. 11365 verifyFormat("return a < b && c > d;"); 11366 verifyFormat("a < 0 ? b : a > 0 ? c : d;"); 11367 verifyFormat("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;"); 11368 verifyFormat("void f() {\n" 11369 " while (a < b && c > d) {\n" 11370 " }\n" 11371 "}"); 11372 verifyFormat("template <typename... Types>\n" 11373 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 11374 11375 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11376 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 11377 getLLVMStyleWithColumns(60)); 11378 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 11379 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 11380 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 11381 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 11382 11383 verifyFormat("#define FOO(typeName, realClass) \\\n" 11384 " {#typeName, foo<FooType>(new foo<realClass>(#typeName))}", 11385 getLLVMStyleWithColumns(60)); 11386 } 11387 11388 TEST_F(FormatTest, UnderstandsShiftOperators) { 11389 verifyFormat("if (i < x >> 1)"); 11390 verifyFormat("while (i < x >> 1)"); 11391 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 11392 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 11393 verifyFormat( 11394 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 11395 verifyFormat("Foo.call<Bar<Function>>()"); 11396 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 11397 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 11398 "++i, v = v >> 1)"); 11399 verifyFormat("if (w<u<v<x>>, 1>::t)"); 11400 } 11401 11402 TEST_F(FormatTest, BitshiftOperatorWidth) { 11403 verifyFormat("int a = 1 << 2; /* foo\n" 11404 " bar */", 11405 "int a=1<<2; /* foo\n" 11406 " bar */"); 11407 11408 verifyFormat("int b = 256 >> 1; /* foo\n" 11409 " bar */", 11410 "int b =256>>1 ; /* foo\n" 11411 " bar */"); 11412 } 11413 11414 TEST_F(FormatTest, UnderstandsBinaryOperators) { 11415 verifyFormat("COMPARE(a, ==, b);"); 11416 verifyFormat("auto s = sizeof...(Ts) - 1;"); 11417 } 11418 11419 TEST_F(FormatTest, UnderstandsPointersToMembers) { 11420 verifyFormat("int A::*x;"); 11421 verifyFormat("int (S::*func)(void *);"); 11422 verifyFormat("void f() { int (S::*func)(void *); }"); 11423 verifyFormat("typedef bool *(Class::*Member)() const;"); 11424 verifyFormat("void f() {\n" 11425 " (a->*f)();\n" 11426 " a->*x;\n" 11427 " (a.*f)();\n" 11428 " ((*a).*f)();\n" 11429 " a.*x;\n" 11430 "}"); 11431 verifyFormat("void f() {\n" 11432 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 11433 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 11434 "}"); 11435 verifyFormat( 11436 "(aaaaaaaaaa->*bbbbbbb)(\n" 11437 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 11438 11439 FormatStyle Style = getLLVMStyle(); 11440 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right); 11441 verifyFormat("typedef bool *(Class::*Member)() const;", Style); 11442 verifyFormat("void f(int A::*p) { int A::*v = &A::B; }", Style); 11443 11444 Style.PointerAlignment = FormatStyle::PAS_Left; 11445 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 11446 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style); 11447 11448 Style.PointerAlignment = FormatStyle::PAS_Middle; 11449 verifyFormat("typedef bool * (Class::*Member)() const;", Style); 11450 verifyFormat("void f(int A::* p) { int A::* v = &A::B; }", Style); 11451 } 11452 11453 TEST_F(FormatTest, UnderstandsUnaryOperators) { 11454 verifyFormat("int a = -2;"); 11455 verifyFormat("f(-1, -2, -3);"); 11456 verifyFormat("a[-1] = 5;"); 11457 verifyFormat("int a = 5 + -2;"); 11458 verifyFormat("if (i == -1) {\n}"); 11459 verifyFormat("if (i != -1) {\n}"); 11460 verifyFormat("if (i > -1) {\n}"); 11461 verifyFormat("if (i < -1) {\n}"); 11462 verifyFormat("++(a->f());"); 11463 verifyFormat("--(a->f());"); 11464 verifyFormat("(a->f())++;"); 11465 verifyFormat("a[42]++;"); 11466 verifyFormat("if (!(a->f())) {\n}"); 11467 verifyFormat("if (!+i) {\n}"); 11468 verifyFormat("~&a;"); 11469 verifyFormat("for (x = 0; -10 < x; --x) {\n}"); 11470 verifyFormat("sizeof -x"); 11471 verifyFormat("sizeof +x"); 11472 verifyFormat("sizeof *x"); 11473 verifyFormat("sizeof &x"); 11474 verifyFormat("delete +x;"); 11475 verifyFormat("co_await +x;"); 11476 verifyFormat("case *x:"); 11477 verifyFormat("case &x:"); 11478 11479 verifyFormat("a-- > b;"); 11480 verifyFormat("b ? -a : c;"); 11481 verifyFormat("n * sizeof char16;"); 11482 verifyGoogleFormat("n * alignof char16;"); 11483 verifyFormat("sizeof(char);"); 11484 verifyGoogleFormat("alignof(char);"); 11485 11486 verifyFormat("return -1;"); 11487 verifyFormat("throw -1;"); 11488 verifyFormat("switch (a) {\n" 11489 "case -1:\n" 11490 " break;\n" 11491 "}"); 11492 verifyFormat("#define X -1"); 11493 verifyFormat("#define X -kConstant"); 11494 11495 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 11496 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 11497 11498 verifyFormat("int a = /* confusing comment */ -1;"); 11499 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 11500 verifyFormat("int a = i /* confusing comment */++;"); 11501 11502 verifyFormat("co_yield -1;"); 11503 verifyFormat("co_return -1;"); 11504 11505 // Check that * is not treated as a binary operator when we set 11506 // PointerAlignment as PAS_Left after a keyword and not a declaration. 11507 FormatStyle PASLeftStyle = getLLVMStyle(); 11508 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 11509 verifyFormat("co_return *a;", PASLeftStyle); 11510 verifyFormat("co_await *a;", PASLeftStyle); 11511 verifyFormat("co_yield *a", PASLeftStyle); 11512 verifyFormat("return *a;", PASLeftStyle); 11513 } 11514 11515 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 11516 verifyFormat("if (!aaaaaaaaaa( // break\n" 11517 " aaaaa)) {\n" 11518 "}"); 11519 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 11520 " aaaaa));"); 11521 verifyFormat("*aaa = aaaaaaa( // break\n" 11522 " bbbbbb);"); 11523 } 11524 11525 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 11526 verifyFormat("bool operator<();"); 11527 verifyFormat("bool operator>();"); 11528 verifyFormat("bool operator=();"); 11529 verifyFormat("bool operator==();"); 11530 verifyFormat("bool operator!=();"); 11531 verifyFormat("int operator+();"); 11532 verifyFormat("int operator++();"); 11533 verifyFormat("int operator++(int) volatile noexcept;"); 11534 verifyFormat("bool operator,();"); 11535 verifyFormat("bool operator();"); 11536 verifyFormat("bool operator()();"); 11537 verifyFormat("bool operator[]();"); 11538 verifyFormat("operator bool();"); 11539 verifyFormat("operator int();"); 11540 verifyFormat("operator void *();"); 11541 verifyFormat("operator SomeType<int>();"); 11542 verifyFormat("operator SomeType<int, int>();"); 11543 verifyFormat("operator SomeType<SomeType<int>>();"); 11544 verifyFormat("operator< <>();"); 11545 verifyFormat("operator<< <>();"); 11546 verifyFormat("< <>"); 11547 11548 verifyFormat("void *operator new(std::size_t size);"); 11549 verifyFormat("void *operator new[](std::size_t size);"); 11550 verifyFormat("void operator delete(void *ptr);"); 11551 verifyFormat("void operator delete[](void *ptr);"); 11552 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 11553 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 11554 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 11555 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 11556 11557 verifyFormat( 11558 "ostream &operator<<(ostream &OutputStream,\n" 11559 " SomeReallyLongType WithSomeReallyLongValue);"); 11560 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 11561 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 11562 " return left.group < right.group;\n" 11563 "}"); 11564 verifyFormat("SomeType &operator=(const SomeType &S);"); 11565 verifyFormat("f.template operator()<int>();"); 11566 11567 verifyGoogleFormat("operator void*();"); 11568 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 11569 verifyGoogleFormat("operator ::A();"); 11570 11571 verifyFormat("using A::operator+;"); 11572 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 11573 "int i;"); 11574 11575 // Calling an operator as a member function. 11576 verifyFormat("void f() { a.operator*(); }"); 11577 verifyFormat("void f() { a.operator*(b & b); }"); 11578 verifyFormat("void f() { a->operator&(a * b); }"); 11579 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 11580 verifyFormat("void f() { operator*(a & a); }"); 11581 verifyFormat("void f() { operator&(a, b * b); }"); 11582 11583 verifyFormat("void f() { return operator()(x) * b; }"); 11584 verifyFormat("void f() { return operator[](x) * b; }"); 11585 verifyFormat("void f() { return operator\"\"_a(x) * b; }"); 11586 verifyFormat("void f() { return operator\"\" _a(x) * b; }"); 11587 verifyFormat("void f() { return operator\"\"s(x) * b; }"); 11588 verifyFormat("void f() { return operator\"\" s(x) * b; }"); 11589 verifyFormat("void f() { return operator\"\"if(x) * b; }"); 11590 11591 verifyFormat("::operator delete(foo);"); 11592 verifyFormat("::operator new(n * sizeof(foo));"); 11593 verifyFormat("foo() { ::operator delete(foo); }"); 11594 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }"); 11595 } 11596 11597 TEST_F(FormatTest, SpaceBeforeTemplateCloser) { 11598 verifyFormat("C<&operator- > minus;"); 11599 verifyFormat("C<&operator> > gt;"); 11600 verifyFormat("C<&operator>= > ge;"); 11601 verifyFormat("C<&operator<= > le;"); 11602 verifyFormat("C<&operator< <X>> lt;"); 11603 } 11604 11605 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 11606 verifyFormat("void A::b() && {}"); 11607 verifyFormat("void A::b() && noexcept {}"); 11608 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 11609 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 11610 verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;"); 11611 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 11612 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 11613 verifyFormat("Deleted &operator=(const Deleted &) &;"); 11614 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 11615 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 11616 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 11617 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 11618 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 11619 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 11620 verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}"); 11621 verifyFormat("void Fn(T const &) const &;"); 11622 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 11623 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;"); 11624 verifyGoogleFormat("template <typename T>\n" 11625 "void F(T) && = delete;"); 11626 verifyFormat("template <typename T> void operator=(T) &;"); 11627 verifyFormat("template <typename T> void operator=(T) const &;"); 11628 verifyFormat("template <typename T> void operator=(T) & noexcept;"); 11629 verifyFormat("template <typename T> void operator=(T) & = default;"); 11630 verifyFormat("template <typename T> void operator=(T) &&;"); 11631 verifyFormat("template <typename T> void operator=(T) && = delete;"); 11632 verifyFormat("template <typename T> void operator=(T) & {}"); 11633 verifyFormat("template <typename T> void operator=(T) && {}"); 11634 11635 FormatStyle AlignLeft = getLLVMStyle(); 11636 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 11637 verifyFormat("void A::b() && {}", AlignLeft); 11638 verifyFormat("void A::b() && noexcept {}", AlignLeft); 11639 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 11640 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;", 11641 AlignLeft); 11642 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 11643 AlignLeft); 11644 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 11645 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 11646 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 11647 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 11648 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 11649 verifyFormat("auto Function(T) & -> void;", AlignLeft); 11650 verifyFormat("void Fn(T const&) const&;", AlignLeft); 11651 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 11652 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;", 11653 AlignLeft); 11654 verifyFormat("template <typename T> void operator=(T) &;", AlignLeft); 11655 verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft); 11656 verifyFormat("template <typename T> void operator=(T) & noexcept;", 11657 AlignLeft); 11658 verifyFormat("template <typename T> void operator=(T) & = default;", 11659 AlignLeft); 11660 verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft); 11661 verifyFormat("template <typename T> void operator=(T) && = delete;", 11662 AlignLeft); 11663 verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft); 11664 verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft); 11665 verifyFormat("for (foo<void() &&>& cb : X)", AlignLeft); 11666 11667 FormatStyle AlignMiddle = getLLVMStyle(); 11668 AlignMiddle.PointerAlignment = FormatStyle::PAS_Middle; 11669 verifyFormat("void A::b() && {}", AlignMiddle); 11670 verifyFormat("void A::b() && noexcept {}", AlignMiddle); 11671 verifyFormat("Deleted & operator=(const Deleted &) & = default;", 11672 AlignMiddle); 11673 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;", 11674 AlignMiddle); 11675 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", 11676 AlignMiddle); 11677 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle); 11678 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle); 11679 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle); 11680 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle); 11681 verifyFormat("auto Function(T) & -> void {}", AlignMiddle); 11682 verifyFormat("auto Function(T) & -> void;", AlignMiddle); 11683 verifyFormat("void Fn(T const &) const &;", AlignMiddle); 11684 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle); 11685 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;", 11686 AlignMiddle); 11687 verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle); 11688 verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle); 11689 verifyFormat("template <typename T> void operator=(T) & noexcept;", 11690 AlignMiddle); 11691 verifyFormat("template <typename T> void operator=(T) & = default;", 11692 AlignMiddle); 11693 verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle); 11694 verifyFormat("template <typename T> void operator=(T) && = delete;", 11695 AlignMiddle); 11696 verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle); 11697 verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle); 11698 11699 FormatStyle Spaces = getLLVMStyle(); 11700 Spaces.SpacesInParens = FormatStyle::SIPO_Custom; 11701 Spaces.SpacesInParensOptions = {}; 11702 Spaces.SpacesInParensOptions.InCStyleCasts = true; 11703 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 11704 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 11705 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 11706 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 11707 11708 Spaces.SpacesInParensOptions.InCStyleCasts = false; 11709 Spaces.SpacesInParensOptions.Other = true; 11710 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 11711 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 11712 Spaces); 11713 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 11714 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 11715 11716 FormatStyle BreakTemplate = getLLVMStyle(); 11717 BreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes; 11718 11719 verifyFormat("struct f {\n" 11720 " template <class T>\n" 11721 " int &foo(const std::string &str) & noexcept {}\n" 11722 "};", 11723 BreakTemplate); 11724 11725 verifyFormat("struct f {\n" 11726 " template <class T>\n" 11727 " int &foo(const std::string &str) && noexcept {}\n" 11728 "};", 11729 BreakTemplate); 11730 11731 verifyFormat("struct f {\n" 11732 " template <class T>\n" 11733 " int &foo(const std::string &str) const & noexcept {}\n" 11734 "};", 11735 BreakTemplate); 11736 11737 verifyFormat("struct f {\n" 11738 " template <class T>\n" 11739 " int &foo(const std::string &str) const & noexcept {}\n" 11740 "};", 11741 BreakTemplate); 11742 11743 verifyFormat("struct f {\n" 11744 " template <class T>\n" 11745 " auto foo(const std::string &str) && noexcept -> int & {}\n" 11746 "};", 11747 BreakTemplate); 11748 11749 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 11750 AlignLeftBreakTemplate.BreakTemplateDeclarations = FormatStyle::BTDS_Yes; 11751 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 11752 11753 verifyFormat("struct f {\n" 11754 " template <class T>\n" 11755 " int& foo(const std::string& str) & noexcept {}\n" 11756 "};", 11757 AlignLeftBreakTemplate); 11758 11759 verifyFormat("struct f {\n" 11760 " template <class T>\n" 11761 " int& foo(const std::string& str) && noexcept {}\n" 11762 "};", 11763 AlignLeftBreakTemplate); 11764 11765 verifyFormat("struct f {\n" 11766 " template <class T>\n" 11767 " int& foo(const std::string& str) const& noexcept {}\n" 11768 "};", 11769 AlignLeftBreakTemplate); 11770 11771 verifyFormat("struct f {\n" 11772 " template <class T>\n" 11773 " int& foo(const std::string& str) const&& noexcept {}\n" 11774 "};", 11775 AlignLeftBreakTemplate); 11776 11777 verifyFormat("struct f {\n" 11778 " template <class T>\n" 11779 " auto foo(const std::string& str) && noexcept -> int& {}\n" 11780 "};", 11781 AlignLeftBreakTemplate); 11782 11783 // The `&` in `Type&` should not be confused with a trailing `&` of 11784 // DEPRECATED(reason) member function. 11785 verifyFormat("struct f {\n" 11786 " template <class T>\n" 11787 " DEPRECATED(reason)\n" 11788 " Type &foo(arguments) {}\n" 11789 "};", 11790 BreakTemplate); 11791 11792 verifyFormat("struct f {\n" 11793 " template <class T>\n" 11794 " DEPRECATED(reason)\n" 11795 " Type& foo(arguments) {}\n" 11796 "};", 11797 AlignLeftBreakTemplate); 11798 11799 verifyFormat("void (*foopt)(int) = &func;"); 11800 11801 FormatStyle DerivePointerAlignment = getLLVMStyle(); 11802 DerivePointerAlignment.DerivePointerAlignment = true; 11803 // There's always a space between the function and its trailing qualifiers. 11804 // This isn't evidence for PAS_Right (or for PAS_Left). 11805 std::string Prefix = "void a() &;\n" 11806 "void b() &;\n"; 11807 verifyFormat(Prefix + "int* x;", DerivePointerAlignment); 11808 verifyFormat(Prefix + "int *x;", DerivePointerAlignment); 11809 // Same if the function is an overloaded operator, and with &&. 11810 Prefix = "void operator()() &&;\n" 11811 "void operator()() &&;\n"; 11812 verifyFormat(Prefix + "int* x;", DerivePointerAlignment); 11813 verifyFormat(Prefix + "int *x;", DerivePointerAlignment); 11814 // However a space between cv-qualifiers and ref-qualifiers *is* evidence. 11815 Prefix = "void a() const &;\n" 11816 "void b() const &;\n"; 11817 verifyFormat(Prefix + "int *x;", Prefix + "int* x;", DerivePointerAlignment); 11818 } 11819 11820 TEST_F(FormatTest, PointerAlignmentFallback) { 11821 FormatStyle Style = getLLVMStyle(); 11822 Style.DerivePointerAlignment = true; 11823 11824 const StringRef Code("int* p;\n" 11825 "int *q;\n" 11826 "int * r;"); 11827 11828 EXPECT_EQ(Style.PointerAlignment, FormatStyle::PAS_Right); 11829 verifyFormat("int *p;\n" 11830 "int *q;\n" 11831 "int *r;", 11832 Code, Style); 11833 11834 Style.PointerAlignment = FormatStyle::PAS_Left; 11835 verifyFormat("int* p;\n" 11836 "int* q;\n" 11837 "int* r;", 11838 Code, Style); 11839 11840 Style.PointerAlignment = FormatStyle::PAS_Middle; 11841 verifyFormat("int * p;\n" 11842 "int * q;\n" 11843 "int * r;", 11844 Code, Style); 11845 } 11846 11847 TEST_F(FormatTest, UnderstandsNewAndDelete) { 11848 verifyFormat("void f() {\n" 11849 " A *a = new A;\n" 11850 " A *a = new (placement) A;\n" 11851 " delete a;\n" 11852 " delete (A *)a;\n" 11853 "}"); 11854 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 11855 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 11856 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 11857 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 11858 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 11859 verifyFormat("delete[] h->p;"); 11860 verifyFormat("delete[] (void *)p;"); 11861 11862 verifyFormat("void operator delete(void *foo) ATTRIB;"); 11863 verifyFormat("void operator new(void *foo) ATTRIB;"); 11864 verifyFormat("void operator delete[](void *foo) ATTRIB;"); 11865 verifyFormat("void operator delete(void *ptr) noexcept;"); 11866 11867 verifyFormat("void new(link p);\n" 11868 "void delete(link p);", 11869 "void new (link p);\n" 11870 "void delete (link p);"); 11871 11872 verifyFormat("{\n" 11873 " p->new();\n" 11874 "}\n" 11875 "{\n" 11876 " p->delete();\n" 11877 "}", 11878 "{\n" 11879 " p->new ();\n" 11880 "}\n" 11881 "{\n" 11882 " p->delete ();\n" 11883 "}"); 11884 11885 FormatStyle AfterPlacementOperator = getLLVMStyle(); 11886 AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom; 11887 EXPECT_TRUE( 11888 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator); 11889 verifyFormat("new (buf) int;", AfterPlacementOperator); 11890 verifyFormat("struct A {\n" 11891 " int *a;\n" 11892 " A(int *p) : a(new (p) int) {\n" 11893 " new (p) int;\n" 11894 " int *b = new (p) int;\n" 11895 " int *c = new (p) int(3);\n" 11896 " delete (b);\n" 11897 " }\n" 11898 "};", 11899 AfterPlacementOperator); 11900 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator); 11901 verifyFormat("delete (int *)p;", AfterPlacementOperator); 11902 11903 AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator = 11904 false; 11905 verifyFormat("new(buf) int;", AfterPlacementOperator); 11906 verifyFormat("struct A {\n" 11907 " int *a;\n" 11908 " A(int *p) : a(new(p) int) {\n" 11909 " new(p) int;\n" 11910 " int *b = new(p) int;\n" 11911 " int *c = new(p) int(3);\n" 11912 " delete(b);\n" 11913 " }\n" 11914 "};", 11915 AfterPlacementOperator); 11916 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator); 11917 verifyFormat("delete (int *)p;", AfterPlacementOperator); 11918 } 11919 11920 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 11921 verifyFormat("int *f(int *a) {}"); 11922 verifyFormat("int main(int argc, char **argv) {}"); 11923 verifyFormat("Test::Test(int b) : a(b * b) {}"); 11924 verifyIndependentOfContext("f(a, *a);"); 11925 verifyFormat("void g() { f(*a); }"); 11926 verifyIndependentOfContext("int a = b * 10;"); 11927 verifyIndependentOfContext("int a = 10 * b;"); 11928 verifyIndependentOfContext("int a = b * c;"); 11929 verifyIndependentOfContext("int a += b * c;"); 11930 verifyIndependentOfContext("int a -= b * c;"); 11931 verifyIndependentOfContext("int a *= b * c;"); 11932 verifyIndependentOfContext("int a /= b * c;"); 11933 verifyIndependentOfContext("int a = *b;"); 11934 verifyIndependentOfContext("int a = *b * c;"); 11935 verifyIndependentOfContext("int a = b * *c;"); 11936 verifyIndependentOfContext("int a = b * (10);"); 11937 verifyIndependentOfContext("S << b * (10);"); 11938 verifyIndependentOfContext("return 10 * b;"); 11939 verifyIndependentOfContext("return *b * *c;"); 11940 verifyIndependentOfContext("return a & ~b;"); 11941 verifyIndependentOfContext("f(b ? *c : *d);"); 11942 verifyIndependentOfContext("int a = b ? *c : *d;"); 11943 verifyIndependentOfContext("*b = a;"); 11944 verifyIndependentOfContext("a * ~b;"); 11945 verifyIndependentOfContext("a * !b;"); 11946 verifyIndependentOfContext("a * +b;"); 11947 verifyIndependentOfContext("a * -b;"); 11948 verifyIndependentOfContext("a * ++b;"); 11949 verifyIndependentOfContext("a * --b;"); 11950 verifyIndependentOfContext("a[4] * b;"); 11951 verifyIndependentOfContext("a[a * a] = 1;"); 11952 verifyIndependentOfContext("f() * b;"); 11953 verifyIndependentOfContext("a * [self dostuff];"); 11954 verifyIndependentOfContext("int x = a * (a + b);"); 11955 verifyIndependentOfContext("(a *)(a + b);"); 11956 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 11957 verifyIndependentOfContext("int *pa = (int *)&a;"); 11958 verifyIndependentOfContext("return sizeof(int **);"); 11959 verifyIndependentOfContext("return sizeof(int ******);"); 11960 verifyIndependentOfContext("return (int **&)a;"); 11961 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 11962 verifyFormat("void f(Type (*parameter)[10]) {}"); 11963 verifyFormat("void f(Type (¶meter)[10]) {}"); 11964 verifyGoogleFormat("return sizeof(int**);"); 11965 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 11966 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 11967 verifyFormat("auto a = [](int **&, int ***) {};"); 11968 verifyFormat("auto PointerBinding = [](const char *S) {};"); 11969 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 11970 verifyFormat("[](const decltype(*a) &value) {}"); 11971 verifyFormat("[](const typeof(*a) &value) {}"); 11972 verifyFormat("[](const _Atomic(a *) &value) {}"); 11973 verifyFormat("[](const __underlying_type(a) &value) {}"); 11974 verifyFormat("decltype(a * b) F();"); 11975 verifyFormat("typeof(a * b) F();"); 11976 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 11977 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 11978 verifyIndependentOfContext("typedef void (*f)(int *a);"); 11979 verifyIndependentOfContext("typedef void (*f)(Type *a);"); 11980 verifyIndependentOfContext("int i{a * b};"); 11981 verifyIndependentOfContext("aaa && aaa->f();"); 11982 verifyIndependentOfContext("int x = ~*p;"); 11983 verifyFormat("Constructor() : a(a), area(width * height) {}"); 11984 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 11985 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 11986 verifyFormat("void f() { f(a, c * d); }"); 11987 verifyFormat("void f() { f(new a(), c * d); }"); 11988 verifyFormat("void f(const MyOverride &override);"); 11989 verifyFormat("void f(const MyFinal &final);"); 11990 verifyIndependentOfContext("bool a = f() && override.f();"); 11991 verifyIndependentOfContext("bool a = f() && final.f();"); 11992 11993 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 11994 11995 verifyIndependentOfContext("A<int *> a;"); 11996 verifyIndependentOfContext("A<int **> a;"); 11997 verifyIndependentOfContext("A<int *, int *> a;"); 11998 verifyIndependentOfContext("A<int *[]> a;"); 11999 verifyIndependentOfContext( 12000 "const char *const p = reinterpret_cast<const char *const>(q);"); 12001 verifyIndependentOfContext("A<int **, int **> a;"); 12002 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 12003 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 12004 verifyFormat("for (; a && b;) {\n}"); 12005 verifyFormat("bool foo = true && [] { return false; }();"); 12006 12007 verifyFormat( 12008 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 12009 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 12010 12011 verifyGoogleFormat("int const* a = &b;"); 12012 verifyGoogleFormat("**outparam = 1;"); 12013 verifyGoogleFormat("*outparam = a * b;"); 12014 verifyGoogleFormat("int main(int argc, char** argv) {}"); 12015 verifyGoogleFormat("A<int*> a;"); 12016 verifyGoogleFormat("A<int**> a;"); 12017 verifyGoogleFormat("A<int*, int*> a;"); 12018 verifyGoogleFormat("A<int**, int**> a;"); 12019 verifyGoogleFormat("f(b ? *c : *d);"); 12020 verifyGoogleFormat("int a = b ? *c : *d;"); 12021 verifyGoogleFormat("Type* t = **x;"); 12022 verifyGoogleFormat("Type* t = *++*x;"); 12023 verifyGoogleFormat("*++*x;"); 12024 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 12025 verifyGoogleFormat("Type* t = x++ * y;"); 12026 verifyGoogleFormat( 12027 "const char* const p = reinterpret_cast<const char* const>(q);"); 12028 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 12029 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 12030 verifyGoogleFormat("template <typename T>\n" 12031 "void f(int i = 0, SomeType** temps = NULL);"); 12032 12033 FormatStyle Left = getLLVMStyle(); 12034 Left.PointerAlignment = FormatStyle::PAS_Left; 12035 verifyFormat("x = *a(x) = *a(y);", Left); 12036 verifyFormat("for (;; *a = b) {\n}", Left); 12037 verifyFormat("return *this += 1;", Left); 12038 verifyFormat("throw *x;", Left); 12039 verifyFormat("delete *x;", Left); 12040 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 12041 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 12042 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 12043 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 12044 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 12045 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 12046 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 12047 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 12048 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 12049 12050 verifyIndependentOfContext("a = *(x + y);"); 12051 verifyIndependentOfContext("a = &(x + y);"); 12052 verifyIndependentOfContext("*(x + y).call();"); 12053 verifyIndependentOfContext("&(x + y)->call();"); 12054 verifyFormat("void f() { &(*I).first; }"); 12055 12056 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 12057 verifyFormat("f(* /* confusing comment */ foo);"); 12058 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)"); 12059 verifyFormat("void foo(int * // this is the first paramters\n" 12060 " ,\n" 12061 " int second);"); 12062 verifyFormat("double term = a * // first\n" 12063 " b;"); 12064 verifyFormat( 12065 "int *MyValues = {\n" 12066 " *A, // Operator detection might be confused by the '{'\n" 12067 " *BB // Operator detection might be confused by previous comment\n" 12068 "};"); 12069 12070 verifyIndependentOfContext("if (int *a = &b)"); 12071 verifyIndependentOfContext("if (int &a = *b)"); 12072 verifyIndependentOfContext("if (a & b[i])"); 12073 verifyIndependentOfContext("if constexpr (a & b[i])"); 12074 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 12075 verifyIndependentOfContext("if (a * (b * c))"); 12076 verifyIndependentOfContext("if constexpr (a * (b * c))"); 12077 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 12078 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 12079 verifyIndependentOfContext("if (*b[i])"); 12080 verifyIndependentOfContext("if (int *a = (&b))"); 12081 verifyIndependentOfContext("while (int *a = &b)"); 12082 verifyIndependentOfContext("while (a * (b * c))"); 12083 verifyIndependentOfContext("size = sizeof *a;"); 12084 verifyIndependentOfContext("if (a && (b = c))"); 12085 verifyFormat("void f() {\n" 12086 " for (const int &v : Values) {\n" 12087 " }\n" 12088 "}"); 12089 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 12090 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 12091 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 12092 12093 verifyFormat("#define A (!a * b)"); 12094 verifyFormat("#define MACRO \\\n" 12095 " int *i = a * b; \\\n" 12096 " void f(a *b);", 12097 getLLVMStyleWithColumns(19)); 12098 12099 verifyIndependentOfContext("A = new SomeType *[Length];"); 12100 verifyIndependentOfContext("A = new SomeType *[Length]();"); 12101 verifyIndependentOfContext("T **t = new T *;"); 12102 verifyIndependentOfContext("T **t = new T *();"); 12103 verifyGoogleFormat("A = new SomeType*[Length]();"); 12104 verifyGoogleFormat("A = new SomeType*[Length];"); 12105 verifyGoogleFormat("T** t = new T*;"); 12106 verifyGoogleFormat("T** t = new T*();"); 12107 12108 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 12109 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 12110 verifyFormat("template <bool a, bool b> " 12111 "typename t::if<x && y>::type f() {}"); 12112 verifyFormat("template <int *y> f() {}"); 12113 verifyFormat("vector<int *> v;"); 12114 verifyFormat("vector<int *const> v;"); 12115 verifyFormat("vector<int *const **const *> v;"); 12116 verifyFormat("vector<int *volatile> v;"); 12117 verifyFormat("vector<a *_Nonnull> v;"); 12118 verifyFormat("vector<a *_Nullable> v;"); 12119 verifyFormat("vector<a *_Null_unspecified> v;"); 12120 verifyFormat("vector<a *__ptr32> v;"); 12121 verifyFormat("vector<a *__ptr64> v;"); 12122 verifyFormat("vector<a *__capability> v;"); 12123 FormatStyle TypeMacros = getLLVMStyle(); 12124 TypeMacros.TypenameMacros = {"LIST"}; 12125 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 12126 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 12127 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 12128 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 12129 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 12130 12131 FormatStyle CustomQualifier = getLLVMStyle(); 12132 // Add identifiers that should not be parsed as a qualifier by default. 12133 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 12134 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 12135 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 12136 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 12137 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 12138 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 12139 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 12140 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 12141 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 12142 verifyFormat("vector<a * _NotAQualifier> v;"); 12143 verifyFormat("vector<a * __not_a_qualifier> v;"); 12144 verifyFormat("vector<a * b> v;"); 12145 verifyFormat("foo<b && false>();"); 12146 verifyFormat("foo<b & 1>();"); 12147 verifyFormat("foo<b & (1)>();"); 12148 verifyFormat("foo<b & (~0)>();"); 12149 verifyFormat("foo<b & (true)>();"); 12150 verifyFormat("foo<b & ((1))>();"); 12151 verifyFormat("foo<b & (/*comment*/ 1)>();"); 12152 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 12153 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 12154 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 12155 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 12156 verifyFormat( 12157 "template <class T, class = typename std::enable_if<\n" 12158 " std::is_integral<T>::value &&\n" 12159 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 12160 "void F();", 12161 getLLVMStyleWithColumns(70)); 12162 verifyFormat("template <class T,\n" 12163 " class = typename std::enable_if<\n" 12164 " std::is_integral<T>::value &&\n" 12165 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 12166 " class U>\n" 12167 "void F();", 12168 getLLVMStyleWithColumns(70)); 12169 verifyFormat( 12170 "template <class T,\n" 12171 " class = typename ::std::enable_if<\n" 12172 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 12173 "void F();", 12174 getGoogleStyleWithColumns(68)); 12175 12176 FormatStyle Style = getLLVMStyle(); 12177 Style.PointerAlignment = FormatStyle::PAS_Left; 12178 verifyFormat("struct {\n" 12179 "}* ptr;", 12180 Style); 12181 verifyFormat("union {\n" 12182 "}* ptr;", 12183 Style); 12184 verifyFormat("class {\n" 12185 "}* ptr;", 12186 Style); 12187 // Don't confuse a multiplication after a brace-initialized expression with 12188 // a class pointer. 12189 verifyFormat("int i = int{42} * 34;", Style); 12190 verifyFormat("struct {\n" 12191 "}&& ptr = {};", 12192 Style); 12193 verifyFormat("union {\n" 12194 "}&& ptr = {};", 12195 Style); 12196 verifyFormat("class {\n" 12197 "}&& ptr = {};", 12198 Style); 12199 verifyFormat("bool b = 3 == int{3} && true;"); 12200 12201 Style.PointerAlignment = FormatStyle::PAS_Middle; 12202 verifyFormat("struct {\n" 12203 "} * ptr;", 12204 Style); 12205 verifyFormat("union {\n" 12206 "} * ptr;", 12207 Style); 12208 verifyFormat("class {\n" 12209 "} * ptr;", 12210 Style); 12211 verifyFormat("struct {\n" 12212 "} && ptr = {};", 12213 Style); 12214 verifyFormat("union {\n" 12215 "} && ptr = {};", 12216 Style); 12217 verifyFormat("class {\n" 12218 "} && ptr = {};", 12219 Style); 12220 12221 Style.PointerAlignment = FormatStyle::PAS_Right; 12222 verifyFormat("struct {\n" 12223 "} *ptr;", 12224 Style); 12225 verifyFormat("union {\n" 12226 "} *ptr;", 12227 Style); 12228 verifyFormat("class {\n" 12229 "} *ptr;", 12230 Style); 12231 verifyFormat("struct {\n" 12232 "} &&ptr = {};", 12233 Style); 12234 verifyFormat("union {\n" 12235 "} &&ptr = {};", 12236 Style); 12237 verifyFormat("class {\n" 12238 "} &&ptr = {};", 12239 Style); 12240 12241 Style.PointerAlignment = FormatStyle::PAS_Left; 12242 verifyFormat("delete[] *ptr;", Style); 12243 verifyFormat("delete[] **ptr;", Style); 12244 verifyFormat("delete[] *(ptr);", Style); 12245 12246 verifyIndependentOfContext("MACRO(int *i);"); 12247 verifyIndependentOfContext("MACRO(auto *a);"); 12248 verifyIndependentOfContext("MACRO(const A *a);"); 12249 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 12250 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 12251 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 12252 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 12253 verifyIndependentOfContext("MACRO(A *const a);"); 12254 verifyIndependentOfContext("MACRO(A *restrict a);"); 12255 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 12256 verifyIndependentOfContext("MACRO(A *__restrict a);"); 12257 verifyIndependentOfContext("MACRO(A *volatile a);"); 12258 verifyIndependentOfContext("MACRO(A *__volatile a);"); 12259 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 12260 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 12261 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 12262 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 12263 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 12264 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 12265 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 12266 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 12267 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 12268 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 12269 verifyIndependentOfContext("MACRO(A *__capability);"); 12270 verifyIndependentOfContext("MACRO(A &__capability);"); 12271 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 12272 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 12273 // If we add __my_qualifier to AttributeMacros it should always be parsed as 12274 // a type declaration: 12275 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 12276 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 12277 // Also check that TypenameMacros prevents parsing it as multiplication: 12278 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 12279 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 12280 12281 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 12282 verifyFormat("void f() { f(float{1}, a * a); }"); 12283 verifyFormat("void f() { f(float(1), a * a); }"); 12284 12285 verifyFormat("f((void (*)(int))g);"); 12286 verifyFormat("f((void (&)(int))g);"); 12287 verifyFormat("f((void (^)(int))g);"); 12288 12289 // FIXME: Is there a way to make this work? 12290 // verifyIndependentOfContext("MACRO(A *a);"); 12291 verifyFormat("MACRO(A &B);"); 12292 verifyFormat("MACRO(A *B);"); 12293 verifyFormat("void f() { MACRO(A * B); }"); 12294 verifyFormat("void f() { MACRO(A & B); }"); 12295 12296 // This lambda was mis-formatted after D88956 (treating it as a binop): 12297 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 12298 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 12299 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 12300 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 12301 12302 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 12303 verifyFormat("return options != nullptr && operator==(*options);"); 12304 12305 verifyFormat("#define OP(x) \\\n" 12306 " ostream &operator<<(ostream &s, const A &a) { \\\n" 12307 " return s << a.DebugString(); \\\n" 12308 " }", 12309 "#define OP(x) \\\n" 12310 " ostream &operator<<(ostream &s, const A &a) { \\\n" 12311 " return s << a.DebugString(); \\\n" 12312 " }", 12313 getLLVMStyleWithColumns(50)); 12314 12315 verifyFormat("#define FOO \\\n" 12316 " void foo() { \\\n" 12317 " operator+(a * b); \\\n" 12318 " }", 12319 getLLVMStyleWithColumns(25)); 12320 12321 // FIXME: We cannot handle this case yet; we might be able to figure out that 12322 // foo<x> d > v; doesn't make sense. 12323 verifyFormat("foo<a<b && c> d> v;"); 12324 12325 FormatStyle PointerMiddle = getLLVMStyle(); 12326 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 12327 verifyFormat("delete *x;", PointerMiddle); 12328 verifyFormat("int * x;", PointerMiddle); 12329 verifyFormat("int *[] x;", PointerMiddle); 12330 verifyFormat("template <int * y> f() {}", PointerMiddle); 12331 verifyFormat("int * f(int * a) {}", PointerMiddle); 12332 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 12333 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 12334 verifyFormat("A<int *> a;", PointerMiddle); 12335 verifyFormat("A<int **> a;", PointerMiddle); 12336 verifyFormat("A<int *, int *> a;", PointerMiddle); 12337 verifyFormat("A<int *[]> a;", PointerMiddle); 12338 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 12339 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 12340 verifyFormat("T ** t = new T *;", PointerMiddle); 12341 12342 // Member function reference qualifiers aren't binary operators. 12343 verifyFormat("string // break\n" 12344 "operator()() & {}"); 12345 verifyFormat("string // break\n" 12346 "operator()() && {}"); 12347 verifyGoogleFormat("template <typename T>\n" 12348 "auto x() & -> int {}"); 12349 12350 // Should be binary operators when used as an argument expression (overloaded 12351 // operator invoked as a member function). 12352 verifyFormat("void f() { a.operator()(a * a); }"); 12353 verifyFormat("void f() { a->operator()(a & a); }"); 12354 verifyFormat("void f() { a.operator()(*a & *a); }"); 12355 verifyFormat("void f() { a->operator()(*a * *a); }"); 12356 12357 verifyFormat("int operator()(T (&&)[N]) { return 1; }"); 12358 verifyFormat("int operator()(T (&)[N]) { return 0; }"); 12359 12360 verifyFormat("val1 & val2;"); 12361 verifyFormat("val1 & val2 & val3;"); 12362 verifyFormat("class c {\n" 12363 " void func(type &a) { a & member; }\n" 12364 " anotherType &member;\n" 12365 "}"); 12366 } 12367 12368 TEST_F(FormatTest, UnderstandsAttributes) { 12369 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 12370 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 12371 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 12372 verifyFormat("__attribute__((nodebug)) ::qualified_type f();"); 12373 FormatStyle AfterType = getLLVMStyle(); 12374 AfterType.BreakAfterReturnType = FormatStyle::RTBS_All; 12375 verifyFormat("__attribute__((nodebug)) void\n" 12376 "foo() {}", 12377 AfterType); 12378 verifyFormat("__unused void\n" 12379 "foo() {}", 12380 AfterType); 12381 12382 FormatStyle CustomAttrs = getLLVMStyle(); 12383 CustomAttrs.AttributeMacros.push_back("__unused"); 12384 CustomAttrs.AttributeMacros.push_back("__attr1"); 12385 CustomAttrs.AttributeMacros.push_back("__attr2"); 12386 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 12387 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 12388 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 12389 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 12390 // Check that it is parsed as a multiplication without AttributeMacros and 12391 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 12392 verifyFormat("vector<SomeType * __attr1> v;"); 12393 verifyFormat("vector<SomeType __attr1 *> v;"); 12394 verifyFormat("vector<SomeType __attr1 *const> v;"); 12395 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 12396 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 12397 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 12398 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 12399 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 12400 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 12401 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 12402 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 12403 verifyFormat("__attr1 ::qualified_type f();", CustomAttrs); 12404 verifyFormat("__attr1() ::qualified_type f();", CustomAttrs); 12405 verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs); 12406 12407 // Check that these are not parsed as function declarations: 12408 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12409 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 12410 verifyFormat("SomeType s(InitValue);", CustomAttrs); 12411 verifyFormat("SomeType s{InitValue};", CustomAttrs); 12412 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 12413 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 12414 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 12415 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 12416 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 12417 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 12418 } 12419 12420 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 12421 // Check that qualifiers on pointers don't break parsing of casts. 12422 verifyFormat("x = (foo *const)*v;"); 12423 verifyFormat("x = (foo *volatile)*v;"); 12424 verifyFormat("x = (foo *restrict)*v;"); 12425 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 12426 verifyFormat("x = (foo *_Nonnull)*v;"); 12427 verifyFormat("x = (foo *_Nullable)*v;"); 12428 verifyFormat("x = (foo *_Null_unspecified)*v;"); 12429 verifyFormat("x = (foo *_Nonnull)*v;"); 12430 verifyFormat("x = (foo *[[clang::attr]])*v;"); 12431 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 12432 verifyFormat("x = (foo *__ptr32)*v;"); 12433 verifyFormat("x = (foo *__ptr64)*v;"); 12434 verifyFormat("x = (foo *__capability)*v;"); 12435 12436 // Check that we handle multiple trailing qualifiers and skip them all to 12437 // determine that the expression is a cast to a pointer type. 12438 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 12439 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 12440 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 12441 StringRef AllQualifiers = 12442 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 12443 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 12444 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 12445 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 12446 12447 // Also check that address-of is not parsed as a binary bitwise-and: 12448 verifyFormat("x = (foo *const)&v;"); 12449 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 12450 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 12451 12452 // Check custom qualifiers: 12453 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 12454 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 12455 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 12456 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 12457 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 12458 CustomQualifier); 12459 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 12460 CustomQualifier); 12461 12462 // Check that unknown identifiers result in binary operator parsing: 12463 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 12464 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 12465 } 12466 12467 TEST_F(FormatTest, UnderstandsSquareAttributes) { 12468 verifyFormat("SomeType s [[unused]] (InitValue);"); 12469 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 12470 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 12471 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 12472 verifyFormat("[[suppress(type.5)]] int uninitialized_on_purpose;"); 12473 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 12474 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12475 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 12476 verifyFormat("[[nodiscard]] bool f() { return false; }"); 12477 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 12478 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 12479 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 12480 verifyFormat("[[nodiscard]] ::qualified_type f();"); 12481 12482 // Make sure we do not mistake attributes for array subscripts. 12483 verifyFormat("int a() {}\n" 12484 "[[unused]] int b() {}"); 12485 verifyFormat("NSArray *arr;\n" 12486 "arr[[Foo() bar]];"); 12487 12488 // On the other hand, we still need to correctly find array subscripts. 12489 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 12490 12491 // Make sure that we do not mistake Objective-C method inside array literals 12492 // as attributes, even if those method names are also keywords. 12493 verifyFormat("@[ [foo bar] ];"); 12494 verifyFormat("@[ [NSArray class] ];"); 12495 verifyFormat("@[ [foo enum] ];"); 12496 12497 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 12498 12499 // Make sure we do not parse attributes as lambda introducers. 12500 FormatStyle MultiLineFunctions = getLLVMStyle(); 12501 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12502 verifyFormat("[[unused]] int b() {\n" 12503 " return 42;\n" 12504 "}", 12505 MultiLineFunctions); 12506 } 12507 12508 TEST_F(FormatTest, AttributeClass) { 12509 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 12510 verifyFormat("class S {\n" 12511 " S(S&&) = default;\n" 12512 "};", 12513 Style); 12514 verifyFormat("class [[nodiscard]] S {\n" 12515 " S(S&&) = default;\n" 12516 "};", 12517 Style); 12518 verifyFormat("class __attribute((maybeunused)) S {\n" 12519 " S(S&&) = default;\n" 12520 "};", 12521 Style); 12522 verifyFormat("struct S {\n" 12523 " S(S&&) = default;\n" 12524 "};", 12525 Style); 12526 verifyFormat("struct [[nodiscard]] S {\n" 12527 " S(S&&) = default;\n" 12528 "};", 12529 Style); 12530 } 12531 12532 TEST_F(FormatTest, AttributesAfterMacro) { 12533 FormatStyle Style = getLLVMStyle(); 12534 verifyFormat("MACRO;\n" 12535 "__attribute__((maybe_unused)) int foo() {\n" 12536 " //...\n" 12537 "}"); 12538 12539 verifyFormat("MACRO;\n" 12540 "[[nodiscard]] int foo() {\n" 12541 " //...\n" 12542 "}"); 12543 12544 verifyNoChange("MACRO\n\n" 12545 "__attribute__((maybe_unused)) int foo() {\n" 12546 " //...\n" 12547 "}"); 12548 12549 verifyNoChange("MACRO\n\n" 12550 "[[nodiscard]] int foo() {\n" 12551 " //...\n" 12552 "}"); 12553 } 12554 12555 TEST_F(FormatTest, AttributePenaltyBreaking) { 12556 FormatStyle Style = getLLVMStyle(); 12557 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 12558 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 12559 Style); 12560 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 12561 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 12562 Style); 12563 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 12564 "shared_ptr<ALongTypeName> &C d) {\n}", 12565 Style); 12566 } 12567 12568 TEST_F(FormatTest, UnderstandsEllipsis) { 12569 FormatStyle Style = getLLVMStyle(); 12570 verifyFormat("int printf(const char *fmt, ...);"); 12571 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 12572 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 12573 12574 verifyFormat("template <int *...PP> a;", Style); 12575 12576 Style.PointerAlignment = FormatStyle::PAS_Left; 12577 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 12578 12579 verifyFormat("template <int*... PP> a;", Style); 12580 12581 Style.PointerAlignment = FormatStyle::PAS_Middle; 12582 verifyFormat("template <int *... PP> a;", Style); 12583 } 12584 12585 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 12586 verifyFormat("int *a;\n" 12587 "int *a;\n" 12588 "int *a;", 12589 "int *a;\n" 12590 "int* a;\n" 12591 "int *a;", 12592 getGoogleStyle()); 12593 verifyFormat("int* a;\n" 12594 "int* a;\n" 12595 "int* a;", 12596 "int* a;\n" 12597 "int* a;\n" 12598 "int *a;", 12599 getGoogleStyle()); 12600 verifyFormat("int *a;\n" 12601 "int *a;\n" 12602 "int *a;", 12603 "int *a;\n" 12604 "int * a;\n" 12605 "int * a;", 12606 getGoogleStyle()); 12607 verifyFormat("auto x = [] {\n" 12608 " int *a;\n" 12609 " int *a;\n" 12610 " int *a;\n" 12611 "};", 12612 "auto x=[]{int *a;\n" 12613 "int * a;\n" 12614 "int * a;};", 12615 getGoogleStyle()); 12616 } 12617 12618 TEST_F(FormatTest, UnderstandsRvalueReferences) { 12619 verifyFormat("int f(int &&a) {}"); 12620 verifyFormat("int f(int a, char &&b) {}"); 12621 verifyFormat("void f() { int &&a = b; }"); 12622 verifyGoogleFormat("int f(int a, char&& b) {}"); 12623 verifyGoogleFormat("void f() { int&& a = b; }"); 12624 12625 verifyIndependentOfContext("A<int &&> a;"); 12626 verifyIndependentOfContext("A<int &&, int &&> a;"); 12627 verifyGoogleFormat("A<int&&> a;"); 12628 verifyGoogleFormat("A<int&&, int&&> a;"); 12629 12630 // Not rvalue references: 12631 verifyFormat("template <bool B, bool C> class A {\n" 12632 " static_assert(B && C, \"Something is wrong\");\n" 12633 "};"); 12634 verifyFormat("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);"); 12635 verifyFormat("template <typename T> struct S {\n" 12636 " explicit(Bar<T> && Foo<T>) S(const S &);\n" 12637 "};"); 12638 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 12639 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 12640 verifyFormat("#define A(a, b) (a && b)"); 12641 } 12642 12643 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 12644 verifyFormat("void f() {\n" 12645 " x[aaaaaaaaa -\n" 12646 " b] = 23;\n" 12647 "}", 12648 getLLVMStyleWithColumns(15)); 12649 } 12650 12651 TEST_F(FormatTest, FormatsCasts) { 12652 verifyFormat("Type *A = static_cast<Type *>(P);"); 12653 verifyFormat("static_cast<Type *>(P);"); 12654 verifyFormat("static_cast<Type &>(Fun)(Args);"); 12655 verifyFormat("static_cast<Type &>(*Fun)(Args);"); 12656 verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;"); 12657 // Check that static_cast<...>(...) does not require the next token to be on 12658 // the same line. 12659 verifyFormat("some_loooong_output << something_something__ << " 12660 "static_cast<const void *>(R)\n" 12661 " << something;"); 12662 verifyFormat("a = static_cast<Type &>(*Fun)(Args);"); 12663 verifyFormat("const_cast<Type &>(*Fun)(Args);"); 12664 verifyFormat("dynamic_cast<Type &>(*Fun)(Args);"); 12665 verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);"); 12666 verifyFormat("Type *A = (Type *)P;"); 12667 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 12668 verifyFormat("int a = (int)(2.0f);"); 12669 verifyFormat("int a = (int)2.0f;"); 12670 verifyFormat("x[(int32)y];"); 12671 verifyFormat("x = (int32)y;"); 12672 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 12673 verifyFormat("int a = (int)*b;"); 12674 verifyFormat("int a = (int)2.0f;"); 12675 verifyFormat("int a = (int)~0;"); 12676 verifyFormat("int a = (int)++a;"); 12677 verifyFormat("int a = (int)sizeof(int);"); 12678 verifyFormat("int a = (int)+2;"); 12679 verifyFormat("my_int a = (my_int)2.0f;"); 12680 verifyFormat("my_int a = (my_int)sizeof(int);"); 12681 verifyFormat("return (my_int)aaa;"); 12682 verifyFormat("throw (my_int)aaa;"); 12683 verifyFormat("#define x ((int)-1)"); 12684 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 12685 verifyFormat("#define p(q) ((int *)&q)"); 12686 verifyFormat("fn(a)(b) + 1;"); 12687 12688 verifyFormat("void f() { my_int a = (my_int)*b; }"); 12689 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 12690 verifyFormat("my_int a = (my_int)~0;"); 12691 verifyFormat("my_int a = (my_int)++a;"); 12692 verifyFormat("my_int a = (my_int)-2;"); 12693 verifyFormat("my_int a = (my_int)1;"); 12694 verifyFormat("my_int a = (my_int *)1;"); 12695 verifyFormat("my_int a = (const my_int)-1;"); 12696 verifyFormat("my_int a = (const my_int *)-1;"); 12697 verifyFormat("my_int a = (my_int)(my_int)-1;"); 12698 verifyFormat("my_int a = (ns::my_int)-2;"); 12699 verifyFormat("case (my_int)ONE:"); 12700 verifyFormat("auto x = (X)this;"); 12701 // Casts in Obj-C style calls used to not be recognized as such. 12702 verifyGoogleFormat("int a = [(type*)[((type*)val) arg] arg];"); 12703 12704 // FIXME: single value wrapped with paren will be treated as cast. 12705 verifyFormat("void f(int i = (kValue)*kMask) {}"); 12706 12707 verifyFormat("{\n" 12708 " (void)F;\n" 12709 "}"); 12710 12711 // Don't break after a cast's 12712 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 12713 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 12714 " bbbbbbbbbbbbbbbbbbbbbb);"); 12715 12716 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)"); 12717 verifyFormat("#define CONF_BOOL(x) (bool *)(x)"); 12718 verifyFormat("#define CONF_BOOL(x) (bool)(x)"); 12719 verifyFormat("bool *y = (bool *)(void *)(x);"); 12720 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)"); 12721 verifyFormat("bool *y = (bool *)(void *)(int)(x);"); 12722 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)"); 12723 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);"); 12724 12725 // These are not casts. 12726 verifyFormat("void f(int *) {}"); 12727 verifyFormat("f(foo)->b;"); 12728 verifyFormat("f(foo).b;"); 12729 verifyFormat("f(foo)(b);"); 12730 verifyFormat("f(foo)[b];"); 12731 verifyFormat("[](foo) { return 4; }(bar);"); 12732 verifyFormat("(*funptr)(foo)[4];"); 12733 verifyFormat("funptrs[4](foo)[4];"); 12734 verifyFormat("void f(int *);"); 12735 verifyFormat("void f(int *) = 0;"); 12736 verifyFormat("void f(SmallVector<int>) {}"); 12737 verifyFormat("void f(SmallVector<int>);"); 12738 verifyFormat("void f(SmallVector<int>) = 0;"); 12739 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 12740 verifyFormat("int a = sizeof(int) * b;"); 12741 verifyGoogleFormat("int a = alignof(int) * b;"); 12742 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 12743 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 12744 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 12745 12746 // These are not casts, but at some point were confused with casts. 12747 verifyFormat("virtual void foo(int *) override;"); 12748 verifyFormat("virtual void foo(char &) const;"); 12749 verifyFormat("virtual void foo(int *a, char *) const;"); 12750 verifyFormat("int a = sizeof(int *) + b;"); 12751 verifyGoogleFormat("int a = alignof(int *) + b;"); 12752 verifyFormat("bool b = f(g<int>) && c;"); 12753 verifyFormat("typedef void (*f)(int i) func;"); 12754 verifyFormat("void operator++(int) noexcept;"); 12755 verifyFormat("void operator++(int &) noexcept;"); 12756 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 12757 "&) noexcept;"); 12758 verifyFormat( 12759 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 12760 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 12761 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 12762 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 12763 verifyFormat("void operator delete(foo &) noexcept;"); 12764 verifyFormat("void operator delete(foo) noexcept;"); 12765 verifyFormat("void operator delete(int) noexcept;"); 12766 verifyFormat("void operator delete(int &) noexcept;"); 12767 verifyFormat("void operator delete(int &) volatile noexcept;"); 12768 verifyFormat("void operator delete(int &) const"); 12769 verifyFormat("void operator delete(int &) = default"); 12770 verifyFormat("void operator delete(int &) = delete"); 12771 verifyFormat("void operator delete(int &) [[noreturn]]"); 12772 verifyFormat("void operator delete(int &) throw();"); 12773 verifyFormat("void operator delete(int &) throw(int);"); 12774 verifyFormat("auto operator delete(int &) -> int;"); 12775 verifyFormat("auto operator delete(int &) override"); 12776 verifyFormat("auto operator delete(int &) final"); 12777 12778 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 12779 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 12780 // FIXME: The indentation here is not ideal. 12781 verifyFormat( 12782 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12783 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 12784 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 12785 } 12786 12787 TEST_F(FormatTest, FormatsFunctionTypes) { 12788 verifyFormat("A<bool()> a;"); 12789 verifyFormat("A<SomeType()> a;"); 12790 verifyFormat("A<void (*)(int, std::string)> a;"); 12791 verifyFormat("A<void *(int)>;"); 12792 verifyFormat("void *(*a)(int *, SomeType *);"); 12793 verifyFormat("int (*func)(void *);"); 12794 verifyFormat("void f() { int (*func)(void *); }"); 12795 verifyFormat("template <class CallbackClass>\n" 12796 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 12797 12798 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 12799 verifyGoogleFormat("void* (*a)(int);"); 12800 verifyGoogleFormat( 12801 "template <class CallbackClass>\n" 12802 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 12803 12804 // Other constructs can look somewhat like function types: 12805 verifyFormat("A<sizeof(*x)> a;"); 12806 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 12807 verifyFormat("some_var = function(*some_pointer_var)[0];"); 12808 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 12809 verifyFormat("int x = f(&h)();"); 12810 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 12811 verifyFormat("std::function<\n" 12812 " LooooooooooongTemplatedType<\n" 12813 " SomeType>*(\n" 12814 " LooooooooooooooooongType type)>\n" 12815 " function;", 12816 getGoogleStyleWithColumns(40)); 12817 } 12818 12819 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 12820 verifyFormat("A (*foo_)[6];"); 12821 verifyFormat("vector<int> (*foo_)[6];"); 12822 } 12823 12824 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 12825 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 12826 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 12827 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 12828 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 12829 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 12830 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 12831 12832 // Different ways of ()-initializiation. 12833 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 12834 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 12835 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 12836 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 12837 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 12838 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 12839 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 12840 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 12841 12842 // Lambdas should not confuse the variable declaration heuristic. 12843 verifyFormat("LooooooooooooooooongType\n" 12844 " variable(nullptr, [](A *a) {});", 12845 getLLVMStyleWithColumns(40)); 12846 } 12847 12848 TEST_F(FormatTest, BreaksLongDeclarations) { 12849 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 12850 " AnotherNameForTheLongType;"); 12851 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 12852 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 12853 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 12854 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 12855 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 12856 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 12857 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 12858 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 12859 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 12860 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 12861 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 12862 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 12863 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 12864 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 12865 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 12866 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 12867 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 12868 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 12869 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 12870 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 12871 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 12872 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 12873 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 12874 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 12875 FormatStyle Indented = getLLVMStyle(); 12876 Indented.IndentWrappedFunctionNames = true; 12877 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 12878 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 12879 Indented); 12880 verifyFormat( 12881 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 12882 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 12883 Indented); 12884 verifyFormat( 12885 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 12886 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 12887 Indented); 12888 verifyFormat( 12889 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 12890 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 12891 Indented); 12892 12893 // FIXME: Without the comment, this breaks after "(". 12894 verifyGoogleFormat( 12895 "LoooooooooooooooooooooooooooooooooooooooongType // break\n" 12896 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();"); 12897 12898 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 12899 " int LoooooooooooooooooooongParam2) {}"); 12900 verifyFormat( 12901 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 12902 " SourceLocation L, IdentifierIn *II,\n" 12903 " Type *T) {}"); 12904 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 12905 "ReallyReaaallyLongFunctionName(\n" 12906 " const std::string &SomeParameter,\n" 12907 " const SomeType<string, SomeOtherTemplateParameter>\n" 12908 " &ReallyReallyLongParameterName,\n" 12909 " const SomeType<string, SomeOtherTemplateParameter>\n" 12910 " &AnotherLongParameterName) {}"); 12911 verifyFormat("template <typename A>\n" 12912 "SomeLoooooooooooooooooooooongType<\n" 12913 " typename some_namespace::SomeOtherType<A>::Type>\n" 12914 "Function() {}"); 12915 12916 verifyGoogleFormat( 12917 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 12918 " aaaaaaaaaaaaaaaaaaaaaaa;"); 12919 verifyGoogleFormat( 12920 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 12921 " SourceLocation L) {}"); 12922 verifyGoogleFormat( 12923 "some_namespace::LongReturnType\n" 12924 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 12925 " int first_long_parameter, int second_parameter) {}"); 12926 12927 verifyGoogleFormat("template <typename T>\n" 12928 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 12929 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 12930 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12931 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 12932 12933 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 12934 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12935 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 12936 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 12937 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 12938 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 12939 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 12940 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 12941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 12942 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 12943 12944 verifyFormat("template <typename T> // Templates on own line.\n" 12945 "static int // Some comment.\n" 12946 "MyFunction(int a);"); 12947 } 12948 12949 TEST_F(FormatTest, FormatsAccessModifiers) { 12950 FormatStyle Style = getLLVMStyle(); 12951 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 12952 FormatStyle::ELBAMS_LogicalBlock); 12953 verifyFormat("struct foo {\n" 12954 "private:\n" 12955 " void f() {}\n" 12956 "\n" 12957 "private:\n" 12958 " int i;\n" 12959 "\n" 12960 "protected:\n" 12961 " int j;\n" 12962 "};", 12963 Style); 12964 verifyFormat("struct foo {\n" 12965 "private:\n" 12966 " void f() {}\n" 12967 "\n" 12968 "private:\n" 12969 " int i;\n" 12970 "\n" 12971 "protected:\n" 12972 " int j;\n" 12973 "};", 12974 "struct foo {\n" 12975 "private:\n" 12976 " void f() {}\n" 12977 "private:\n" 12978 " int i;\n" 12979 "protected:\n" 12980 " int j;\n" 12981 "};", 12982 Style); 12983 verifyFormat("struct foo { /* comment */\n" 12984 "private:\n" 12985 " int i;\n" 12986 " // comment\n" 12987 "private:\n" 12988 " int j;\n" 12989 "};", 12990 Style); 12991 verifyFormat("struct foo {\n" 12992 "#ifdef FOO\n" 12993 "#endif\n" 12994 "private:\n" 12995 " int i;\n" 12996 "#ifdef FOO\n" 12997 "private:\n" 12998 "#endif\n" 12999 " int j;\n" 13000 "};", 13001 Style); 13002 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 13003 verifyFormat("struct foo {\n" 13004 "private:\n" 13005 " void f() {}\n" 13006 "private:\n" 13007 " int i;\n" 13008 "protected:\n" 13009 " int j;\n" 13010 "};", 13011 Style); 13012 verifyFormat("struct foo {\n" 13013 "private:\n" 13014 " void f() {}\n" 13015 "private:\n" 13016 " int i;\n" 13017 "protected:\n" 13018 " int j;\n" 13019 "};", 13020 "struct foo {\n" 13021 "\n" 13022 "private:\n" 13023 " void f() {}\n" 13024 "\n" 13025 "private:\n" 13026 " int i;\n" 13027 "\n" 13028 "protected:\n" 13029 " int j;\n" 13030 "};", 13031 Style); 13032 verifyFormat("struct foo { /* comment */\n" 13033 "private:\n" 13034 " int i;\n" 13035 " // comment\n" 13036 "private:\n" 13037 " int j;\n" 13038 "};", 13039 "struct foo { /* comment */\n" 13040 "\n" 13041 "private:\n" 13042 " int i;\n" 13043 " // comment\n" 13044 "\n" 13045 "private:\n" 13046 " int j;\n" 13047 "};", 13048 Style); 13049 verifyFormat("struct foo {\n" 13050 "#ifdef FOO\n" 13051 "#endif\n" 13052 "private:\n" 13053 " int i;\n" 13054 "#ifdef FOO\n" 13055 "private:\n" 13056 "#endif\n" 13057 " int j;\n" 13058 "};", 13059 "struct foo {\n" 13060 "#ifdef FOO\n" 13061 "#endif\n" 13062 "\n" 13063 "private:\n" 13064 " int i;\n" 13065 "#ifdef FOO\n" 13066 "\n" 13067 "private:\n" 13068 "#endif\n" 13069 " int j;\n" 13070 "};", 13071 Style); 13072 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 13073 verifyFormat("struct foo {\n" 13074 "private:\n" 13075 " void f() {}\n" 13076 "\n" 13077 "private:\n" 13078 " int i;\n" 13079 "\n" 13080 "protected:\n" 13081 " int j;\n" 13082 "};", 13083 Style); 13084 verifyFormat("struct foo {\n" 13085 "private:\n" 13086 " void f() {}\n" 13087 "\n" 13088 "private:\n" 13089 " int i;\n" 13090 "\n" 13091 "protected:\n" 13092 " int j;\n" 13093 "};", 13094 "struct foo {\n" 13095 "private:\n" 13096 " void f() {}\n" 13097 "private:\n" 13098 " int i;\n" 13099 "protected:\n" 13100 " int j;\n" 13101 "};", 13102 Style); 13103 verifyFormat("struct foo { /* comment */\n" 13104 "private:\n" 13105 " int i;\n" 13106 " // comment\n" 13107 "\n" 13108 "private:\n" 13109 " int j;\n" 13110 "};", 13111 Style); 13112 verifyFormat("struct foo {\n" 13113 "#ifdef FOO\n" 13114 "#endif\n" 13115 "\n" 13116 "private:\n" 13117 " int i;\n" 13118 "#ifdef FOO\n" 13119 "\n" 13120 "private:\n" 13121 "#endif\n" 13122 " int j;\n" 13123 "};", 13124 "struct foo {\n" 13125 "#ifdef FOO\n" 13126 "#endif\n" 13127 "private:\n" 13128 " int i;\n" 13129 "#ifdef FOO\n" 13130 "private:\n" 13131 "#endif\n" 13132 " int j;\n" 13133 "};", 13134 Style); 13135 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 13136 verifyNoChange("struct foo {\n" 13137 "\n" 13138 "private:\n" 13139 " void f() {}\n" 13140 "\n" 13141 "private:\n" 13142 " int i;\n" 13143 "\n" 13144 "protected:\n" 13145 " int j;\n" 13146 "};", 13147 Style); 13148 verifyFormat("struct foo {\n" 13149 "private:\n" 13150 " void f() {}\n" 13151 "private:\n" 13152 " int i;\n" 13153 "protected:\n" 13154 " int j;\n" 13155 "};", 13156 Style); 13157 verifyNoChange("struct foo { /* comment */\n" 13158 "\n" 13159 "private:\n" 13160 " int i;\n" 13161 " // comment\n" 13162 "\n" 13163 "private:\n" 13164 " int j;\n" 13165 "};", 13166 Style); 13167 verifyFormat("struct foo { /* comment */\n" 13168 "private:\n" 13169 " int i;\n" 13170 " // comment\n" 13171 "private:\n" 13172 " int j;\n" 13173 "};", 13174 Style); 13175 verifyNoChange("struct foo {\n" 13176 "#ifdef FOO\n" 13177 "#endif\n" 13178 "\n" 13179 "private:\n" 13180 " int i;\n" 13181 "#ifdef FOO\n" 13182 "\n" 13183 "private:\n" 13184 "#endif\n" 13185 " int j;\n" 13186 "};", 13187 Style); 13188 verifyFormat("struct foo {\n" 13189 "#ifdef FOO\n" 13190 "#endif\n" 13191 "private:\n" 13192 " int i;\n" 13193 "#ifdef FOO\n" 13194 "private:\n" 13195 "#endif\n" 13196 " int j;\n" 13197 "};", 13198 Style); 13199 Style.AttributeMacros.push_back("FOO"); 13200 Style.AttributeMacros.push_back("BAR"); 13201 verifyFormat("struct foo {\n" 13202 "FOO private:\n" 13203 " int i;\n" 13204 "BAR(x) protected:\n" 13205 " int j;\n" 13206 "};", 13207 Style); 13208 13209 FormatStyle NoEmptyLines = getLLVMStyle(); 13210 NoEmptyLines.MaxEmptyLinesToKeep = 0; 13211 verifyFormat("struct foo {\n" 13212 "private:\n" 13213 " void f() {}\n" 13214 "\n" 13215 "private:\n" 13216 " int i;\n" 13217 "\n" 13218 "public:\n" 13219 "protected:\n" 13220 " int j;\n" 13221 "};", 13222 NoEmptyLines); 13223 13224 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 13225 verifyFormat("struct foo {\n" 13226 "private:\n" 13227 " void f() {}\n" 13228 "private:\n" 13229 " int i;\n" 13230 "public:\n" 13231 "protected:\n" 13232 " int j;\n" 13233 "};", 13234 NoEmptyLines); 13235 13236 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 13237 verifyFormat("struct foo {\n" 13238 "private:\n" 13239 " void f() {}\n" 13240 "\n" 13241 "private:\n" 13242 " int i;\n" 13243 "\n" 13244 "public:\n" 13245 "\n" 13246 "protected:\n" 13247 " int j;\n" 13248 "};", 13249 NoEmptyLines); 13250 } 13251 13252 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 13253 13254 FormatStyle Style = getLLVMStyle(); 13255 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 13256 verifyFormat("struct foo {\n" 13257 "private:\n" 13258 " void f() {}\n" 13259 "\n" 13260 "private:\n" 13261 " int i;\n" 13262 "\n" 13263 "protected:\n" 13264 " int j;\n" 13265 "};", 13266 Style); 13267 13268 // Check if lines are removed. 13269 verifyFormat("struct foo {\n" 13270 "private:\n" 13271 " void f() {}\n" 13272 "\n" 13273 "private:\n" 13274 " int i;\n" 13275 "\n" 13276 "protected:\n" 13277 " int j;\n" 13278 "};", 13279 "struct foo {\n" 13280 "private:\n" 13281 "\n" 13282 " void f() {}\n" 13283 "\n" 13284 "private:\n" 13285 "\n" 13286 " int i;\n" 13287 "\n" 13288 "protected:\n" 13289 "\n" 13290 " int j;\n" 13291 "};", 13292 Style); 13293 13294 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 13295 verifyFormat("struct foo {\n" 13296 "private:\n" 13297 "\n" 13298 " void f() {}\n" 13299 "\n" 13300 "private:\n" 13301 "\n" 13302 " int i;\n" 13303 "\n" 13304 "protected:\n" 13305 "\n" 13306 " int j;\n" 13307 "};", 13308 Style); 13309 13310 // Check if lines are added. 13311 verifyFormat("struct foo {\n" 13312 "private:\n" 13313 "\n" 13314 " void f() {}\n" 13315 "\n" 13316 "private:\n" 13317 "\n" 13318 " int i;\n" 13319 "\n" 13320 "protected:\n" 13321 "\n" 13322 " int j;\n" 13323 "};", 13324 "struct foo {\n" 13325 "private:\n" 13326 " void f() {}\n" 13327 "\n" 13328 "private:\n" 13329 " int i;\n" 13330 "\n" 13331 "protected:\n" 13332 " int j;\n" 13333 "};", 13334 Style); 13335 13336 // Leave tests rely on the code layout, test::messUp can not be used. 13337 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 13338 Style.MaxEmptyLinesToKeep = 0u; 13339 verifyFormat("struct foo {\n" 13340 "private:\n" 13341 " void f() {}\n" 13342 "\n" 13343 "private:\n" 13344 " int i;\n" 13345 "\n" 13346 "protected:\n" 13347 " int j;\n" 13348 "};", 13349 Style); 13350 13351 // Check if MaxEmptyLinesToKeep is respected. 13352 verifyFormat("struct foo {\n" 13353 "private:\n" 13354 " void f() {}\n" 13355 "\n" 13356 "private:\n" 13357 " int i;\n" 13358 "\n" 13359 "protected:\n" 13360 " int j;\n" 13361 "};", 13362 "struct foo {\n" 13363 "private:\n" 13364 "\n\n\n" 13365 " void f() {}\n" 13366 "\n" 13367 "private:\n" 13368 "\n\n\n" 13369 " int i;\n" 13370 "\n" 13371 "protected:\n" 13372 "\n\n\n" 13373 " int j;\n" 13374 "};", 13375 Style); 13376 13377 Style.MaxEmptyLinesToKeep = 1u; 13378 verifyNoChange("struct foo {\n" 13379 "private:\n" 13380 "\n" 13381 " void f() {}\n" 13382 "\n" 13383 "private:\n" 13384 "\n" 13385 " int i;\n" 13386 "\n" 13387 "protected:\n" 13388 "\n" 13389 " int j;\n" 13390 "};", 13391 Style); 13392 // Check if no lines are kept. 13393 verifyFormat("struct foo {\n" 13394 "private:\n" 13395 " void f() {}\n" 13396 "\n" 13397 "private:\n" 13398 " int i;\n" 13399 "\n" 13400 "protected:\n" 13401 " int j;\n" 13402 "};", 13403 Style); 13404 // Check if MaxEmptyLinesToKeep is respected. 13405 verifyFormat("struct foo {\n" 13406 "private:\n" 13407 "\n" 13408 " void f() {}\n" 13409 "\n" 13410 "private:\n" 13411 "\n" 13412 " int i;\n" 13413 "\n" 13414 "protected:\n" 13415 "\n" 13416 " int j;\n" 13417 "};", 13418 "struct foo {\n" 13419 "private:\n" 13420 "\n\n\n" 13421 " void f() {}\n" 13422 "\n" 13423 "private:\n" 13424 "\n\n\n" 13425 " int i;\n" 13426 "\n" 13427 "protected:\n" 13428 "\n\n\n" 13429 " int j;\n" 13430 "};", 13431 Style); 13432 13433 Style.MaxEmptyLinesToKeep = 10u; 13434 verifyNoChange("struct foo {\n" 13435 "private:\n" 13436 "\n\n\n" 13437 " void f() {}\n" 13438 "\n" 13439 "private:\n" 13440 "\n\n\n" 13441 " int i;\n" 13442 "\n" 13443 "protected:\n" 13444 "\n\n\n" 13445 " int j;\n" 13446 "};", 13447 Style); 13448 13449 // Test with comments. 13450 Style = getLLVMStyle(); 13451 verifyFormat("struct foo {\n" 13452 "private:\n" 13453 " // comment\n" 13454 " void f() {}\n" 13455 "\n" 13456 "private: /* comment */\n" 13457 " int i;\n" 13458 "};", 13459 Style); 13460 verifyFormat("struct foo {\n" 13461 "private:\n" 13462 " // comment\n" 13463 " void f() {}\n" 13464 "\n" 13465 "private: /* comment */\n" 13466 " int i;\n" 13467 "};", 13468 "struct foo {\n" 13469 "private:\n" 13470 "\n" 13471 " // comment\n" 13472 " void f() {}\n" 13473 "\n" 13474 "private: /* comment */\n" 13475 "\n" 13476 " int i;\n" 13477 "};", 13478 Style); 13479 13480 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 13481 verifyFormat("struct foo {\n" 13482 "private:\n" 13483 "\n" 13484 " // comment\n" 13485 " void f() {}\n" 13486 "\n" 13487 "private: /* comment */\n" 13488 "\n" 13489 " int i;\n" 13490 "};", 13491 "struct foo {\n" 13492 "private:\n" 13493 " // comment\n" 13494 " void f() {}\n" 13495 "\n" 13496 "private: /* comment */\n" 13497 " int i;\n" 13498 "};", 13499 Style); 13500 verifyFormat("struct foo {\n" 13501 "private:\n" 13502 "\n" 13503 " // comment\n" 13504 " void f() {}\n" 13505 "\n" 13506 "private: /* comment */\n" 13507 "\n" 13508 " int i;\n" 13509 "};", 13510 Style); 13511 13512 // Test with preprocessor defines. 13513 Style = getLLVMStyle(); 13514 verifyFormat("struct foo {\n" 13515 "private:\n" 13516 "#ifdef FOO\n" 13517 "#endif\n" 13518 " void f() {}\n" 13519 "};", 13520 Style); 13521 verifyFormat("struct foo {\n" 13522 "private:\n" 13523 "#ifdef FOO\n" 13524 "#endif\n" 13525 " void f() {}\n" 13526 "};", 13527 "struct foo {\n" 13528 "private:\n" 13529 "\n" 13530 "#ifdef FOO\n" 13531 "#endif\n" 13532 " void f() {}\n" 13533 "};", 13534 Style); 13535 verifyNoChange("struct foo {\n" 13536 "#ifdef FOO\n" 13537 "#else\n" 13538 "private:\n" 13539 "\n" 13540 "#endif\n" 13541 "};", 13542 Style); 13543 verifyFormat("struct foo {\n" 13544 "#ifdef FOO\n" 13545 "#else\n" 13546 "private:\n" 13547 "\n" 13548 "#endif\n" 13549 "};", 13550 "struct foo {\n" 13551 "#ifdef FOO\n" 13552 "#else\n" 13553 "private:\n" 13554 "\n" 13555 "\n" 13556 "#endif\n" 13557 "};", 13558 Style); 13559 verifyFormat("struct foo {\n" 13560 "#ifdef FOO\n" 13561 "private:\n" 13562 "#else\n" 13563 "#endif\n" 13564 "};", 13565 "struct foo {\n" 13566 "#ifdef FOO\n" 13567 "private:\n" 13568 "\n" 13569 "\n" 13570 "#else\n" 13571 "#endif\n" 13572 "};", 13573 Style); 13574 verifyFormat("struct foo {\n" 13575 "#if 0\n" 13576 "#else\n" 13577 "#endif\n" 13578 "#ifdef FOO\n" 13579 "private:\n" 13580 "#endif\n" 13581 "};", 13582 "struct foo {\n" 13583 "#if 0\n" 13584 "#else\n" 13585 "#endif\n" 13586 "#ifdef FOO\n" 13587 "private:\n" 13588 "\n" 13589 "\n" 13590 "#endif\n" 13591 "};", 13592 Style); 13593 13594 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 13595 verifyFormat("struct foo {\n" 13596 "private:\n" 13597 "\n" 13598 "#ifdef FOO\n" 13599 "#endif\n" 13600 " void f() {}\n" 13601 "};", 13602 "struct foo {\n" 13603 "private:\n" 13604 "#ifdef FOO\n" 13605 "#endif\n" 13606 " void f() {}\n" 13607 "};", 13608 Style); 13609 verifyFormat("struct foo {\n" 13610 "private:\n" 13611 "\n" 13612 "#ifdef FOO\n" 13613 "#endif\n" 13614 " void f() {}\n" 13615 "};", 13616 Style); 13617 } 13618 13619 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 13620 // Combined tests of EmptyLineAfterAccessModifier and 13621 // EmptyLineBeforeAccessModifier. 13622 FormatStyle Style = getLLVMStyle(); 13623 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 13624 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 13625 verifyFormat("struct foo {\n" 13626 "private:\n" 13627 "\n" 13628 "protected:\n" 13629 "};", 13630 Style); 13631 13632 Style.MaxEmptyLinesToKeep = 10u; 13633 // Both remove all new lines. 13634 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 13635 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 13636 verifyFormat("struct foo {\n" 13637 "private:\n" 13638 "protected:\n" 13639 "};", 13640 "struct foo {\n" 13641 "private:\n" 13642 "\n\n\n" 13643 "protected:\n" 13644 "};", 13645 Style); 13646 13647 // Leave tests rely on the code layout, test::messUp can not be used. 13648 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 13649 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 13650 Style.MaxEmptyLinesToKeep = 10u; 13651 verifyNoChange("struct foo {\n" 13652 "private:\n" 13653 "\n\n\n" 13654 "protected:\n" 13655 "};", 13656 Style); 13657 Style.MaxEmptyLinesToKeep = 3u; 13658 verifyNoChange("struct foo {\n" 13659 "private:\n" 13660 "\n\n\n" 13661 "protected:\n" 13662 "};", 13663 Style); 13664 Style.MaxEmptyLinesToKeep = 1u; 13665 verifyNoChange("struct foo {\n" 13666 "private:\n" 13667 "\n\n\n" 13668 "protected:\n" 13669 "};", 13670 Style); // Based on new lines in original document and not 13671 // on the setting. 13672 13673 Style.MaxEmptyLinesToKeep = 10u; 13674 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 13675 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 13676 // Newlines are kept if they are greater than zero, 13677 // test::messUp removes all new lines which changes the logic 13678 verifyNoChange("struct foo {\n" 13679 "private:\n" 13680 "\n\n\n" 13681 "protected:\n" 13682 "};", 13683 Style); 13684 13685 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 13686 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 13687 // test::messUp removes all new lines which changes the logic 13688 verifyNoChange("struct foo {\n" 13689 "private:\n" 13690 "\n\n\n" 13691 "protected:\n" 13692 "};", 13693 Style); 13694 13695 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 13696 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 13697 verifyNoChange("struct foo {\n" 13698 "private:\n" 13699 "\n\n\n" 13700 "protected:\n" 13701 "};", 13702 Style); // test::messUp removes all new lines which changes 13703 // the logic. 13704 13705 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 13706 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 13707 verifyFormat("struct foo {\n" 13708 "private:\n" 13709 "protected:\n" 13710 "};", 13711 "struct foo {\n" 13712 "private:\n" 13713 "\n\n\n" 13714 "protected:\n" 13715 "};", 13716 Style); 13717 13718 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 13719 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 13720 verifyNoChange("struct foo {\n" 13721 "private:\n" 13722 "\n\n\n" 13723 "protected:\n" 13724 "};", 13725 Style); // test::messUp removes all new lines which changes 13726 // the logic. 13727 13728 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 13729 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 13730 verifyFormat("struct foo {\n" 13731 "private:\n" 13732 "protected:\n" 13733 "};", 13734 "struct foo {\n" 13735 "private:\n" 13736 "\n\n\n" 13737 "protected:\n" 13738 "};", 13739 Style); 13740 13741 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 13742 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 13743 verifyFormat("struct foo {\n" 13744 "private:\n" 13745 "protected:\n" 13746 "};", 13747 "struct foo {\n" 13748 "private:\n" 13749 "\n\n\n" 13750 "protected:\n" 13751 "};", 13752 Style); 13753 13754 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 13755 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 13756 verifyFormat("struct foo {\n" 13757 "private:\n" 13758 "protected:\n" 13759 "};", 13760 "struct foo {\n" 13761 "private:\n" 13762 "\n\n\n" 13763 "protected:\n" 13764 "};", 13765 Style); 13766 13767 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 13768 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 13769 verifyFormat("struct foo {\n" 13770 "private:\n" 13771 "protected:\n" 13772 "};", 13773 "struct foo {\n" 13774 "private:\n" 13775 "\n\n\n" 13776 "protected:\n" 13777 "};", 13778 Style); 13779 } 13780 13781 TEST_F(FormatTest, FormatsArrays) { 13782 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 13783 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 13784 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 13785 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 13786 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 13787 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 13788 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13789 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 13790 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13791 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 13792 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13793 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 13794 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 13795 verifyFormat( 13796 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 13797 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 13798 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 13799 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 13800 " .aaaaaaaaaaaaaaaaaaaaaa();"); 13801 13802 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 13803 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 13804 verifyFormat( 13805 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 13806 " .aaaaaaa[0]\n" 13807 " .aaaaaaaaaaaaaaaaaaaaaa();"); 13808 verifyFormat("a[::b::c];"); 13809 13810 verifyFormat("{\n" 13811 " (*a)[0] = 1;\n" 13812 "}"); 13813 13814 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 13815 13816 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 13817 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 13818 } 13819 13820 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 13821 verifyFormat("(a)->b();"); 13822 verifyFormat("--a;"); 13823 } 13824 13825 TEST_F(FormatTest, HandlesIncludeDirectives) { 13826 verifyFormat("#include <string>\n" 13827 "#include <a/b/c.h>\n" 13828 "#include \"a/b/string\"\n" 13829 "#include \"string.h\"\n" 13830 "#include \"string.h\"\n" 13831 "#include <a-a>\n" 13832 "#include < path with space >\n" 13833 "#include_next <test.h>" 13834 "#include \"abc.h\" // this is included for ABC\n" 13835 "#include \"some long include\" // with a comment\n" 13836 "#include \"some very long include path\"\n" 13837 "#include <some/very/long/include/path>", 13838 getLLVMStyleWithColumns(35)); 13839 verifyFormat("#include \"a.h\"", "#include \"a.h\""); 13840 verifyFormat("#include <a>", "#include<a>"); 13841 13842 verifyFormat("#import <string>"); 13843 verifyFormat("#import <a/b/c.h>"); 13844 verifyFormat("#import \"a/b/string\""); 13845 verifyFormat("#import \"string.h\""); 13846 verifyFormat("#import \"string.h\""); 13847 verifyFormat("#if __has_include(<strstream>)\n" 13848 "#include <strstream>\n" 13849 "#endif"); 13850 13851 verifyFormat("#define MY_IMPORT <a/b>"); 13852 13853 verifyFormat("#if __has_include(<a/b>)"); 13854 verifyFormat("#if __has_include_next(<a/b>)"); 13855 verifyFormat("#define F __has_include(<a/b>)"); 13856 verifyFormat("#define F __has_include_next(<a/b>)"); 13857 13858 // Protocol buffer definition or missing "#". 13859 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 13860 getLLVMStyleWithColumns(30)); 13861 13862 FormatStyle Style = getLLVMStyle(); 13863 Style.AlwaysBreakBeforeMultilineStrings = true; 13864 Style.ColumnLimit = 0; 13865 verifyFormat("#import \"abc.h\"", Style); 13866 13867 // But 'import' might also be a regular C++ namespace. 13868 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 13869 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 13870 verifyFormat("import::Bar foo(val ? 2 : 1);"); 13871 } 13872 13873 //===----------------------------------------------------------------------===// 13874 // Error recovery tests. 13875 //===----------------------------------------------------------------------===// 13876 13877 TEST_F(FormatTest, IncompleteParameterLists) { 13878 FormatStyle NoBinPacking = getLLVMStyle(); 13879 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; 13880 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 13881 " double *min_x,\n" 13882 " double *max_x,\n" 13883 " double *min_y,\n" 13884 " double *max_y,\n" 13885 " double *min_z,\n" 13886 " double *max_z, ) {}", 13887 NoBinPacking); 13888 } 13889 13890 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 13891 verifyFormat("void f() { return; }\n42"); 13892 verifyFormat("void f() {\n" 13893 " if (0)\n" 13894 " return;\n" 13895 "}\n" 13896 "42"); 13897 verifyFormat("void f() { return }\n42"); 13898 verifyFormat("void f() {\n" 13899 " if (0)\n" 13900 " return\n" 13901 "}\n" 13902 "42"); 13903 } 13904 13905 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 13906 verifyFormat("void f() { return }", "void f ( ) { return }"); 13907 verifyFormat("void f() {\n" 13908 " if (a)\n" 13909 " return\n" 13910 "}", 13911 "void f ( ) { if ( a ) return }"); 13912 verifyFormat("namespace N {\n" 13913 "void f()\n" 13914 "}", 13915 "namespace N { void f() }"); 13916 verifyFormat("namespace N {\n" 13917 "void f() {}\n" 13918 "void g()\n" 13919 "} // namespace N", 13920 "namespace N { void f( ) { } void g( ) }"); 13921 } 13922 13923 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 13924 verifyFormat("int aaaaaaaa =\n" 13925 " // Overlylongcomment\n" 13926 " b;", 13927 getLLVMStyleWithColumns(20)); 13928 verifyFormat("function(\n" 13929 " ShortArgument,\n" 13930 " LoooooooooooongArgument);", 13931 getLLVMStyleWithColumns(20)); 13932 } 13933 13934 TEST_F(FormatTest, IncorrectAccessSpecifier) { 13935 verifyFormat("public:"); 13936 verifyFormat("class A {\n" 13937 "public\n" 13938 " void f() {}\n" 13939 "};"); 13940 verifyFormat("public\n" 13941 "int qwerty;"); 13942 verifyFormat("public\n" 13943 "B {}"); 13944 verifyFormat("public\n" 13945 "{\n" 13946 "}"); 13947 verifyFormat("public\n" 13948 "B { int x; }"); 13949 } 13950 13951 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 13952 verifyFormat("{"); 13953 verifyFormat("#})"); 13954 verifyNoCrash("(/**/[:!] ?[)."); 13955 verifyNoCrash("struct X {\n" 13956 " operator iunt(\n" 13957 "};"); 13958 verifyNoCrash("struct Foo {\n" 13959 " operator foo(bar\n" 13960 "};"); 13961 } 13962 13963 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 13964 // Found by oss-fuzz: 13965 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 13966 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 13967 Style.ColumnLimit = 60; 13968 verifyNoCrash( 13969 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 13970 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 13971 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 13972 Style); 13973 } 13974 13975 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 13976 verifyFormat("do {\n}"); 13977 verifyFormat("do {\n}\n" 13978 "f();"); 13979 verifyFormat("do {\n}\n" 13980 "wheeee(fun);"); 13981 verifyFormat("do {\n" 13982 " f();\n" 13983 "}"); 13984 } 13985 13986 TEST_F(FormatTest, IncorrectCodeMissingParens) { 13987 verifyFormat("if {\n foo;\n foo();\n}"); 13988 verifyFormat("switch {\n foo;\n foo();\n}"); 13989 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 13990 verifyIncompleteFormat("ERROR: for target;"); 13991 verifyFormat("while {\n foo;\n foo();\n}"); 13992 verifyFormat("do {\n foo;\n foo();\n} while;"); 13993 } 13994 13995 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 13996 verifyIncompleteFormat("namespace {\n" 13997 "class Foo { Foo (\n" 13998 "};\n" 13999 "} // namespace"); 14000 } 14001 14002 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 14003 verifyFormat("{\n" 14004 " {\n" 14005 " }", 14006 "{\n" 14007 "{\n" 14008 "}"); 14009 verifyFormat("{\n" 14010 " {\n" 14011 " }", 14012 "{\n" 14013 " {\n" 14014 "}"); 14015 verifyFormat("{\n" 14016 " {\n" 14017 " }"); 14018 verifyFormat("{\n" 14019 " {\n" 14020 " }\n" 14021 "}\n" 14022 "}", 14023 "{\n" 14024 " {\n" 14025 " }\n" 14026 " }\n" 14027 "}"); 14028 14029 verifyFormat("{\n" 14030 " {\n" 14031 " breakme(\n" 14032 " qwe);\n" 14033 " }", 14034 "{\n" 14035 " {\n" 14036 " breakme(qwe);\n" 14037 "}", 14038 getLLVMStyleWithColumns(10)); 14039 } 14040 14041 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 14042 verifyFormat("int x = {\n" 14043 " avariable,\n" 14044 " b(alongervariable)};", 14045 getLLVMStyleWithColumns(25)); 14046 } 14047 14048 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 14049 verifyFormat("return (a)(b){1, 2, 3};"); 14050 } 14051 14052 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 14053 verifyFormat("vector<int> x{1, 2, 3, 4};"); 14054 verifyFormat("vector<int> x{\n" 14055 " 1,\n" 14056 " 2,\n" 14057 " 3,\n" 14058 " 4,\n" 14059 "};"); 14060 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 14061 verifyFormat("f({1, 2});"); 14062 verifyFormat("auto v = Foo{-1};"); 14063 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 14064 verifyFormat("Class::Class : member{1, 2, 3} {}"); 14065 verifyFormat("new vector<int>{1, 2, 3};"); 14066 verifyFormat("new int[3]{1, 2, 3};"); 14067 verifyFormat("new int{1};"); 14068 verifyFormat("return {arg1, arg2};"); 14069 verifyFormat("return {arg1, SomeType{parameter}};"); 14070 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 14071 verifyFormat("new T{arg1, arg2};"); 14072 verifyFormat("f(MyMap[{composite, key}]);"); 14073 verifyFormat("class Class {\n" 14074 " T member = {arg1, arg2};\n" 14075 "};"); 14076 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 14077 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 14078 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 14079 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 14080 verifyFormat("int a = std::is_integral<int>{} + 0;"); 14081 14082 verifyFormat("int foo(int i) { return fo1{}(i); }"); 14083 verifyFormat("int foo(int i) { return fo1{}(i); }"); 14084 verifyFormat("auto i = decltype(x){};"); 14085 verifyFormat("auto i = typeof(x){};"); 14086 verifyFormat("auto i = _Atomic(x){};"); 14087 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 14088 verifyFormat("Node n{1, Node{1000}, //\n" 14089 " 2};"); 14090 verifyFormat("Aaaa aaaaaaa{\n" 14091 " {\n" 14092 " aaaa,\n" 14093 " },\n" 14094 "};"); 14095 verifyFormat("class C : public D {\n" 14096 " SomeClass SC{2};\n" 14097 "};"); 14098 verifyFormat("class C : public A {\n" 14099 " class D : public B {\n" 14100 " void f() { int i{2}; }\n" 14101 " };\n" 14102 "};"); 14103 verifyFormat("#define A {a, a},"); 14104 // Don't confuse braced list initializers with compound statements. 14105 verifyFormat( 14106 "class A {\n" 14107 " A() : a{} {}\n" 14108 " A() : Base<int>{} {}\n" 14109 " A() : Base<Foo<int>>{} {}\n" 14110 " A(int b) : b(b) {}\n" 14111 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n" 14112 " int a, b;\n" 14113 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n" 14114 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} " 14115 "{}\n" 14116 "};"); 14117 14118 // Avoid breaking between equal sign and opening brace 14119 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 14120 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 14121 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 14122 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 14123 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 14124 " {\"ccccccccccccccccccccc\", 2}};", 14125 AvoidBreakingFirstArgument); 14126 14127 // Binpacking only if there is no trailing comma 14128 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 14129 " cccccccccc, dddddddddd};", 14130 getLLVMStyleWithColumns(50)); 14131 verifyFormat("const Aaaaaa aaaaa = {\n" 14132 " aaaaaaaaaaa,\n" 14133 " bbbbbbbbbbb,\n" 14134 " ccccccccccc,\n" 14135 " ddddddddddd,\n" 14136 "};", 14137 getLLVMStyleWithColumns(50)); 14138 14139 // Cases where distinguising braced lists and blocks is hard. 14140 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 14141 verifyFormat("void f() {\n" 14142 " return; // comment\n" 14143 "}\n" 14144 "SomeType t;"); 14145 verifyFormat("void f() {\n" 14146 " if (a) {\n" 14147 " f();\n" 14148 " }\n" 14149 "}\n" 14150 "SomeType t;"); 14151 14152 // In combination with BinPackArguments = false. 14153 FormatStyle NoBinPacking = getLLVMStyle(); 14154 NoBinPacking.BinPackArguments = false; 14155 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 14156 " bbbbb,\n" 14157 " ccccc,\n" 14158 " ddddd,\n" 14159 " eeeee,\n" 14160 " ffffff,\n" 14161 " ggggg,\n" 14162 " hhhhhh,\n" 14163 " iiiiii,\n" 14164 " jjjjjj,\n" 14165 " kkkkkk};", 14166 NoBinPacking); 14167 verifyFormat("const Aaaaaa aaaaa = {\n" 14168 " aaaaa,\n" 14169 " bbbbb,\n" 14170 " ccccc,\n" 14171 " ddddd,\n" 14172 " eeeee,\n" 14173 " ffffff,\n" 14174 " ggggg,\n" 14175 " hhhhhh,\n" 14176 " iiiiii,\n" 14177 " jjjjjj,\n" 14178 " kkkkkk,\n" 14179 "};", 14180 NoBinPacking); 14181 verifyFormat( 14182 "const Aaaaaa aaaaa = {\n" 14183 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 14184 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 14185 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 14186 "};", 14187 NoBinPacking); 14188 14189 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 14190 verifyFormat("static uint8 CddDp83848Reg[] = {\n" 14191 " CDDDP83848_BMCR_REGISTER,\n" 14192 " CDDDP83848_BMSR_REGISTER,\n" 14193 " CDDDP83848_RBR_REGISTER};", 14194 "static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 14195 " CDDDP83848_BMSR_REGISTER,\n" 14196 " CDDDP83848_RBR_REGISTER};", 14197 NoBinPacking); 14198 14199 // FIXME: The alignment of these trailing comments might be bad. Then again, 14200 // this might be utterly useless in real code. 14201 verifyFormat("Constructor::Constructor()\n" 14202 " : some_value{ //\n" 14203 " aaaaaaa, //\n" 14204 " bbbbbbb} {}"); 14205 14206 // In braced lists, the first comment is always assumed to belong to the 14207 // first element. Thus, it can be moved to the next or previous line as 14208 // appropriate. 14209 verifyFormat("function({// First element:\n" 14210 " 1,\n" 14211 " // Second element:\n" 14212 " 2});", 14213 "function({\n" 14214 " // First element:\n" 14215 " 1,\n" 14216 " // Second element:\n" 14217 " 2});"); 14218 verifyFormat("std::vector<int> MyNumbers{\n" 14219 " // First element:\n" 14220 " 1,\n" 14221 " // Second element:\n" 14222 " 2};", 14223 "std::vector<int> MyNumbers{// First element:\n" 14224 " 1,\n" 14225 " // Second element:\n" 14226 " 2};", 14227 getLLVMStyleWithColumns(30)); 14228 // A trailing comma should still lead to an enforced line break and no 14229 // binpacking. 14230 verifyFormat("vector<int> SomeVector = {\n" 14231 " // aaa\n" 14232 " 1,\n" 14233 " 2,\n" 14234 "};", 14235 "vector<int> SomeVector = { // aaa\n" 14236 " 1, 2, };"); 14237 14238 // C++11 brace initializer list l-braces should not be treated any differently 14239 // when breaking before lambda bodies is enabled 14240 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 14241 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 14242 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 14243 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 14244 verifyFormat( 14245 "std::runtime_error{\n" 14246 " \"Long string which will force a break onto the next line...\"};", 14247 BreakBeforeLambdaBody); 14248 14249 FormatStyle ExtraSpaces = getLLVMStyle(); 14250 ExtraSpaces.Cpp11BracedListStyle = false; 14251 ExtraSpaces.ColumnLimit = 75; 14252 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 14253 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 14254 verifyFormat("f({ 1, 2 });", ExtraSpaces); 14255 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 14256 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 14257 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 14258 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 14259 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 14260 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 14261 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 14262 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 14263 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 14264 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 14265 verifyFormat("class Class {\n" 14266 " T member = { arg1, arg2 };\n" 14267 "};", 14268 ExtraSpaces); 14269 verifyFormat( 14270 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 14271 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 14272 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 14273 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 14274 ExtraSpaces); 14275 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 14276 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 14277 ExtraSpaces); 14278 verifyFormat( 14279 "someFunction(OtherParam,\n" 14280 " BracedList{ // comment 1 (Forcing interesting break)\n" 14281 " param1, param2,\n" 14282 " // comment 2\n" 14283 " param3, param4 });", 14284 ExtraSpaces); 14285 verifyFormat( 14286 "std::this_thread::sleep_for(\n" 14287 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 14288 ExtraSpaces); 14289 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 14290 " aaaaaaa,\n" 14291 " aaaaaaaaaa,\n" 14292 " aaaaa,\n" 14293 " aaaaaaaaaaaaaaa,\n" 14294 " aaa,\n" 14295 " aaaaaaaaaa,\n" 14296 " a,\n" 14297 " aaaaaaaaaaaaaaaaaaaaa,\n" 14298 " aaaaaaaaaaaa,\n" 14299 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 14300 " aaaaaaa,\n" 14301 " a};"); 14302 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 14303 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 14304 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 14305 14306 // Avoid breaking between initializer/equal sign and opening brace 14307 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 14308 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 14309 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 14310 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 14311 " { \"ccccccccccccccccccccc\", 2 }\n" 14312 "};", 14313 ExtraSpaces); 14314 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 14315 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 14316 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 14317 " { \"ccccccccccccccccccccc\", 2 }\n" 14318 "};", 14319 ExtraSpaces); 14320 14321 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 14322 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 14323 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 14324 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 14325 14326 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 14327 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 14328 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom; 14329 SpaceBetweenBraces.SpacesInParensOptions.Other = true; 14330 SpaceBetweenBraces.SpacesInSquareBrackets = true; 14331 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 14332 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 14333 verifyFormat("vector< int > x{ // comment 1\n" 14334 " 1, 2, 3, 4 };", 14335 SpaceBetweenBraces); 14336 SpaceBetweenBraces.ColumnLimit = 20; 14337 verifyFormat("vector< int > x{\n" 14338 " 1, 2, 3, 4 };", 14339 "vector<int>x{1,2,3,4};", SpaceBetweenBraces); 14340 SpaceBetweenBraces.ColumnLimit = 24; 14341 verifyFormat("vector< int > x{ 1, 2,\n" 14342 " 3, 4 };", 14343 "vector<int>x{1,2,3,4};", SpaceBetweenBraces); 14344 verifyFormat("vector< int > x{\n" 14345 " 1,\n" 14346 " 2,\n" 14347 " 3,\n" 14348 " 4,\n" 14349 "};", 14350 "vector<int>x{1,2,3,4,};", SpaceBetweenBraces); 14351 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 14352 SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom; 14353 SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true; 14354 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 14355 } 14356 14357 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 14358 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14359 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14360 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14361 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14362 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14363 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 14364 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 14365 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14366 " 1, 22, 333, 4444, 55555, //\n" 14367 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14368 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 14369 verifyFormat( 14370 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14371 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14372 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 14373 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 14374 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 14375 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 14376 " 7777777};"); 14377 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 14378 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 14379 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 14380 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 14381 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 14382 " // Separating comment.\n" 14383 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 14384 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 14385 " // Leading comment\n" 14386 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 14387 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 14388 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 14389 " 1, 1, 1, 1};", 14390 getLLVMStyleWithColumns(39)); 14391 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 14392 " 1, 1, 1, 1};", 14393 getLLVMStyleWithColumns(38)); 14394 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 14395 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 14396 getLLVMStyleWithColumns(43)); 14397 verifyFormat( 14398 "static unsigned SomeValues[10][3] = {\n" 14399 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 14400 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 14401 verifyFormat("static auto fields = new vector<string>{\n" 14402 " \"aaaaaaaaaaaaa\",\n" 14403 " \"aaaaaaaaaaaaa\",\n" 14404 " \"aaaaaaaaaaaa\",\n" 14405 " \"aaaaaaaaaaaaaa\",\n" 14406 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 14407 " \"aaaaaaaaaaaa\",\n" 14408 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 14409 "};"); 14410 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 14411 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 14412 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 14413 " 3, cccccccccccccccccccccc};", 14414 getLLVMStyleWithColumns(60)); 14415 14416 // Trailing commas. 14417 verifyFormat("vector<int> x = {\n" 14418 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 14419 "};", 14420 getLLVMStyleWithColumns(39)); 14421 verifyFormat("vector<int> x = {\n" 14422 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 14423 "};", 14424 getLLVMStyleWithColumns(39)); 14425 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 14426 " 1, 1, 1, 1,\n" 14427 " /**/ /**/};", 14428 getLLVMStyleWithColumns(39)); 14429 14430 // Trailing comment in the first line. 14431 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 14432 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 14433 " 111111111, 222222222, 3333333333, 444444444, //\n" 14434 " 11111111, 22222222, 333333333, 44444444};"); 14435 // Trailing comment in the last line. 14436 verifyFormat("int aaaaa[] = {\n" 14437 " 1, 2, 3, // comment\n" 14438 " 4, 5, 6 // comment\n" 14439 "};"); 14440 14441 // With nested lists, we should either format one item per line or all nested 14442 // lists one on line. 14443 // FIXME: For some nested lists, we can do better. 14444 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 14445 " {aaaaaaaaaaaaaaaaaaa},\n" 14446 " {aaaaaaaaaaaaaaaaaaaaa},\n" 14447 " {aaaaaaaaaaaaaaaaa}};", 14448 getLLVMStyleWithColumns(60)); 14449 verifyFormat( 14450 "SomeStruct my_struct_array = {\n" 14451 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 14452 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 14453 " {aaa, aaa},\n" 14454 " {aaa, aaa},\n" 14455 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 14456 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 14457 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 14458 14459 // No column layout should be used here. 14460 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 14461 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 14462 14463 verifyNoCrash("a<,"); 14464 14465 // No braced initializer here. 14466 verifyFormat("void f() {\n" 14467 " struct Dummy {};\n" 14468 " f(v);\n" 14469 "}"); 14470 verifyFormat("void foo() {\n" 14471 " { // asdf\n" 14472 " {\n" 14473 " int a;\n" 14474 " }\n" 14475 " }\n" 14476 " {\n" 14477 " {\n" 14478 " int b;\n" 14479 " }\n" 14480 " }\n" 14481 "}"); 14482 verifyFormat("namespace n {\n" 14483 "void foo() {\n" 14484 " {\n" 14485 " {\n" 14486 " statement();\n" 14487 " if (false) {\n" 14488 " }\n" 14489 " }\n" 14490 " }\n" 14491 " {\n" 14492 " }\n" 14493 "}\n" 14494 "} // namespace n"); 14495 14496 // Long lists should be formatted in columns even if they are nested. 14497 verifyFormat( 14498 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14499 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14500 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14501 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14502 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 14503 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 14504 14505 // Allow "single-column" layout even if that violates the column limit. There 14506 // isn't going to be a better way. 14507 verifyFormat("std::vector<int> a = {\n" 14508 " aaaaaaaa,\n" 14509 " aaaaaaaa,\n" 14510 " aaaaaaaa,\n" 14511 " aaaaaaaa,\n" 14512 " aaaaaaaaaa,\n" 14513 " aaaaaaaa,\n" 14514 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 14515 getLLVMStyleWithColumns(30)); 14516 verifyFormat("vector<int> aaaa = {\n" 14517 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 14518 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 14519 " aaaaaa.aaaaaaa,\n" 14520 " aaaaaa.aaaaaaa,\n" 14521 " aaaaaa.aaaaaaa,\n" 14522 " aaaaaa.aaaaaaa,\n" 14523 "};"); 14524 14525 // Don't create hanging lists. 14526 verifyFormat("someFunction(Param, {List1, List2,\n" 14527 " List3});", 14528 getLLVMStyleWithColumns(35)); 14529 verifyFormat("someFunction(Param, Param,\n" 14530 " {List1, List2,\n" 14531 " List3});", 14532 getLLVMStyleWithColumns(35)); 14533 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 14534 " aaaaaaaaaaaaaaaaaaaaaaa);"); 14535 14536 // No possible column formats, don't want the optimal paths penalized. 14537 verifyFormat( 14538 "waarudo::unit desk = {\n" 14539 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10} * w::m; }};"); 14540 verifyFormat("SomeType something1([](const Input &i) -> Output { return " 14541 "Output{1, 2}; },\n" 14542 " [](const Input &i) -> Output { return " 14543 "Output{1, 2}; });"); 14544 FormatStyle NoBinPacking = getLLVMStyle(); 14545 NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; 14546 verifyFormat("waarudo::unit desk = {\n" 14547 " .s = \"desk\", .p = p, .b = [] { return w::r{3, 10, 1, 1, " 14548 "1, 1} * w::m; }};", 14549 NoBinPacking); 14550 } 14551 14552 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 14553 FormatStyle DoNotMerge = getLLVMStyle(); 14554 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 14555 14556 verifyFormat("void f() { return 42; }"); 14557 verifyFormat("void f() {\n" 14558 " return 42;\n" 14559 "}", 14560 DoNotMerge); 14561 verifyFormat("void f() {\n" 14562 " // Comment\n" 14563 "}"); 14564 verifyFormat("{\n" 14565 "#error {\n" 14566 " int a;\n" 14567 "}"); 14568 verifyFormat("{\n" 14569 " int a;\n" 14570 "#error {\n" 14571 "}"); 14572 verifyFormat("void f() {} // comment"); 14573 verifyFormat("void f() { int a; } // comment"); 14574 verifyFormat("void f() {\n" 14575 "} // comment", 14576 DoNotMerge); 14577 verifyFormat("void f() {\n" 14578 " int a;\n" 14579 "} // comment", 14580 DoNotMerge); 14581 verifyFormat("void f() {\n" 14582 "} // comment", 14583 getLLVMStyleWithColumns(15)); 14584 14585 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 14586 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 14587 14588 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 14589 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 14590 verifyGoogleFormat("class C {\n" 14591 " C()\n" 14592 " : iiiiiiii(nullptr),\n" 14593 " kkkkkkk(nullptr),\n" 14594 " mmmmmmm(nullptr),\n" 14595 " nnnnnnn(nullptr) {}\n" 14596 "};"); 14597 14598 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 14599 verifyFormat("A() : b(0) {}", "A():b(0){}", NoColumnLimit); 14600 verifyFormat("class C {\n" 14601 " A() : b(0) {}\n" 14602 "};", 14603 "class C{A():b(0){}};", NoColumnLimit); 14604 verifyFormat("A()\n" 14605 " : b(0) {\n" 14606 "}", 14607 "A()\n:b(0)\n{\n}", NoColumnLimit); 14608 14609 FormatStyle NoColumnLimitWrapAfterFunction = NoColumnLimit; 14610 NoColumnLimitWrapAfterFunction.BreakBeforeBraces = FormatStyle::BS_Custom; 14611 NoColumnLimitWrapAfterFunction.BraceWrapping.AfterFunction = true; 14612 verifyFormat("class C {\n" 14613 "#pragma foo\n" 14614 " int foo { return 0; }\n" 14615 "};", 14616 NoColumnLimitWrapAfterFunction); 14617 verifyFormat("class C {\n" 14618 "#pragma foo\n" 14619 " void foo {}\n" 14620 "};", 14621 NoColumnLimitWrapAfterFunction); 14622 14623 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 14624 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 14625 FormatStyle::SFS_None; 14626 verifyFormat("A() : b(0) {\n" 14627 "}", 14628 DoNotMergeNoColumnLimit); 14629 verifyNoChange("A()\n" 14630 " : b(0) {\n" 14631 "}", 14632 DoNotMergeNoColumnLimit); 14633 verifyFormat("A()\n" 14634 " : b(0) {\n" 14635 "}", 14636 "A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit); 14637 14638 verifyFormat("#define A \\\n" 14639 " void f() { \\\n" 14640 " int i; \\\n" 14641 " }", 14642 getLLVMStyleWithColumns(20)); 14643 verifyFormat("#define A \\\n" 14644 " void f() { int i; }", 14645 getLLVMStyleWithColumns(21)); 14646 verifyFormat("#define A \\\n" 14647 " void f() { \\\n" 14648 " int i; \\\n" 14649 " } \\\n" 14650 " int j;", 14651 getLLVMStyleWithColumns(22)); 14652 verifyFormat("#define A \\\n" 14653 " void f() { int i; } \\\n" 14654 " int j;", 14655 getLLVMStyleWithColumns(23)); 14656 14657 verifyFormat( 14658 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 14659 " aaaaaaaaaaaaaaaaaa,\n" 14660 " aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {}"); 14661 14662 constexpr StringRef Code{"void foo() { /* Empty */ }"}; 14663 verifyFormat(Code); 14664 verifyFormat(Code, "void foo() { /* Empty */\n" 14665 "}"); 14666 verifyFormat(Code, "void foo() {\n" 14667 "/* Empty */\n" 14668 "}"); 14669 } 14670 14671 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 14672 FormatStyle MergeEmptyOnly = getLLVMStyle(); 14673 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 14674 verifyFormat("class C {\n" 14675 " int f() {}\n" 14676 "};", 14677 MergeEmptyOnly); 14678 verifyFormat("class C {\n" 14679 " int f() {\n" 14680 " return 42;\n" 14681 " }\n" 14682 "};", 14683 MergeEmptyOnly); 14684 verifyFormat("int f() {}", MergeEmptyOnly); 14685 verifyFormat("int f() {\n" 14686 " return 42;\n" 14687 "}", 14688 MergeEmptyOnly); 14689 14690 // Also verify behavior when BraceWrapping.AfterFunction = true 14691 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 14692 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 14693 verifyFormat("int f() {}", MergeEmptyOnly); 14694 verifyFormat("class C {\n" 14695 " int f() {}\n" 14696 "};", 14697 MergeEmptyOnly); 14698 } 14699 14700 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 14701 FormatStyle MergeInlineOnly = getLLVMStyle(); 14702 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 14703 verifyFormat("class C {\n" 14704 " int f() { return 42; }\n" 14705 "};", 14706 MergeInlineOnly); 14707 verifyFormat("int f() {\n" 14708 " return 42;\n" 14709 "}", 14710 MergeInlineOnly); 14711 14712 // SFS_Inline implies SFS_Empty 14713 verifyFormat("class C {\n" 14714 " int f() {}\n" 14715 "};", 14716 MergeInlineOnly); 14717 verifyFormat("int f() {}", MergeInlineOnly); 14718 // https://llvm.org/PR54147 14719 verifyFormat("auto lambda = []() {\n" 14720 " // comment\n" 14721 " f();\n" 14722 " g();\n" 14723 "};", 14724 MergeInlineOnly); 14725 14726 verifyFormat("class C {\n" 14727 "#ifdef A\n" 14728 " int f() { return 42; }\n" 14729 "#endif\n" 14730 "};", 14731 MergeInlineOnly); 14732 14733 verifyFormat("struct S {\n" 14734 "// comment\n" 14735 "#ifdef FOO\n" 14736 " int foo() { bar(); }\n" 14737 "#endif\n" 14738 "};", 14739 MergeInlineOnly); 14740 14741 // Also verify behavior when BraceWrapping.AfterFunction = true 14742 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 14743 MergeInlineOnly.BraceWrapping.AfterFunction = true; 14744 verifyFormat("class C {\n" 14745 " int f() { return 42; }\n" 14746 "};", 14747 MergeInlineOnly); 14748 verifyFormat("int f()\n" 14749 "{\n" 14750 " return 42;\n" 14751 "}", 14752 MergeInlineOnly); 14753 14754 // SFS_Inline implies SFS_Empty 14755 verifyFormat("int f() {}", MergeInlineOnly); 14756 verifyFormat("class C {\n" 14757 " int f() {}\n" 14758 "};", 14759 MergeInlineOnly); 14760 14761 MergeInlineOnly.BraceWrapping.AfterClass = true; 14762 MergeInlineOnly.BraceWrapping.AfterStruct = true; 14763 verifyFormat("class C\n" 14764 "{\n" 14765 " int f() { return 42; }\n" 14766 "};", 14767 MergeInlineOnly); 14768 verifyFormat("struct C\n" 14769 "{\n" 14770 " int f() { return 42; }\n" 14771 "};", 14772 MergeInlineOnly); 14773 verifyFormat("int f()\n" 14774 "{\n" 14775 " return 42;\n" 14776 "}", 14777 MergeInlineOnly); 14778 verifyFormat("int f() {}", MergeInlineOnly); 14779 verifyFormat("class C\n" 14780 "{\n" 14781 " int f() { return 42; }\n" 14782 "};", 14783 MergeInlineOnly); 14784 verifyFormat("struct C\n" 14785 "{\n" 14786 " int f() { return 42; }\n" 14787 "};", 14788 MergeInlineOnly); 14789 verifyFormat("struct C\n" 14790 "// comment\n" 14791 "/* comment */\n" 14792 "// comment\n" 14793 "{\n" 14794 " int f() { return 42; }\n" 14795 "};", 14796 MergeInlineOnly); 14797 verifyFormat("/* comment */ struct C\n" 14798 "{\n" 14799 " int f() { return 42; }\n" 14800 "};", 14801 MergeInlineOnly); 14802 } 14803 14804 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 14805 FormatStyle MergeInlineOnly = getLLVMStyle(); 14806 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 14807 FormatStyle::SFS_InlineOnly; 14808 verifyFormat("class C {\n" 14809 " int f() { return 42; }\n" 14810 "};", 14811 MergeInlineOnly); 14812 verifyFormat("int f() {\n" 14813 " return 42;\n" 14814 "}", 14815 MergeInlineOnly); 14816 14817 // SFS_InlineOnly does not imply SFS_Empty 14818 verifyFormat("class C {\n" 14819 " int f() {}\n" 14820 "};", 14821 MergeInlineOnly); 14822 verifyFormat("int f() {\n" 14823 "}", 14824 MergeInlineOnly); 14825 14826 // Also verify behavior when BraceWrapping.AfterFunction = true 14827 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 14828 MergeInlineOnly.BraceWrapping.AfterFunction = true; 14829 verifyFormat("class C {\n" 14830 " int f() { return 42; }\n" 14831 "};", 14832 MergeInlineOnly); 14833 verifyFormat("int f()\n" 14834 "{\n" 14835 " return 42;\n" 14836 "}", 14837 MergeInlineOnly); 14838 14839 // SFS_InlineOnly does not imply SFS_Empty 14840 verifyFormat("int f()\n" 14841 "{\n" 14842 "}", 14843 MergeInlineOnly); 14844 verifyFormat("class C {\n" 14845 " int f() {}\n" 14846 "};", 14847 MergeInlineOnly); 14848 } 14849 14850 TEST_F(FormatTest, SplitEmptyFunction) { 14851 FormatStyle Style = getLLVMStyleWithColumns(40); 14852 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 14853 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 14854 Style.BraceWrapping.AfterFunction = true; 14855 Style.BraceWrapping.SplitEmptyFunction = false; 14856 14857 verifyFormat("int f()\n" 14858 "{}", 14859 Style); 14860 verifyFormat("int f()\n" 14861 "{\n" 14862 " return 42;\n" 14863 "}", 14864 Style); 14865 verifyFormat("int f()\n" 14866 "{\n" 14867 " // some comment\n" 14868 "}", 14869 Style); 14870 14871 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 14872 verifyFormat("int f() {}", Style); 14873 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 14874 "{}", 14875 Style); 14876 verifyFormat("int f()\n" 14877 "{\n" 14878 " return 0;\n" 14879 "}", 14880 Style); 14881 14882 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 14883 verifyFormat("class Foo {\n" 14884 " int f() {}\n" 14885 "};", 14886 Style); 14887 verifyFormat("class Foo {\n" 14888 " int f() { return 0; }\n" 14889 "};", 14890 Style); 14891 verifyFormat("class Foo {\n" 14892 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 14893 " {}\n" 14894 "};", 14895 Style); 14896 verifyFormat("class Foo {\n" 14897 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 14898 " {\n" 14899 " return 0;\n" 14900 " }\n" 14901 "};", 14902 Style); 14903 14904 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 14905 verifyFormat("int f() {}", Style); 14906 verifyFormat("int f() { return 0; }", Style); 14907 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 14908 "{}", 14909 Style); 14910 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 14911 "{\n" 14912 " return 0;\n" 14913 "}", 14914 Style); 14915 } 14916 14917 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) { 14918 FormatStyle Style = getLLVMStyleWithColumns(40); 14919 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 14920 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 14921 Style.BraceWrapping.AfterFunction = true; 14922 Style.BraceWrapping.SplitEmptyFunction = true; 14923 Style.BraceWrapping.SplitEmptyRecord = false; 14924 14925 verifyFormat("class C {};", Style); 14926 verifyFormat("struct C {};", Style); 14927 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 14928 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 14929 "{\n" 14930 "}", 14931 Style); 14932 verifyFormat("class C {\n" 14933 " C()\n" 14934 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n" 14935 " bbbbbbbbbbbbbbbbbbb()\n" 14936 " {\n" 14937 " }\n" 14938 " void\n" 14939 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 14940 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 14941 " {\n" 14942 " }\n" 14943 "};", 14944 Style); 14945 } 14946 14947 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 14948 FormatStyle Style = getLLVMStyle(); 14949 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 14950 verifyFormat("#ifdef A\n" 14951 "int f() {}\n" 14952 "#else\n" 14953 "int g() {}\n" 14954 "#endif", 14955 Style); 14956 } 14957 14958 TEST_F(FormatTest, SplitEmptyClass) { 14959 FormatStyle Style = getLLVMStyle(); 14960 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 14961 Style.BraceWrapping.AfterClass = true; 14962 Style.BraceWrapping.SplitEmptyRecord = false; 14963 14964 verifyFormat("class Foo\n" 14965 "{};", 14966 Style); 14967 verifyFormat("/* something */ class Foo\n" 14968 "{};", 14969 Style); 14970 verifyFormat("template <typename X> class Foo\n" 14971 "{};", 14972 Style); 14973 verifyFormat("class Foo\n" 14974 "{\n" 14975 " Foo();\n" 14976 "};", 14977 Style); 14978 verifyFormat("typedef class Foo\n" 14979 "{\n" 14980 "} Foo_t;", 14981 Style); 14982 14983 Style.BraceWrapping.SplitEmptyRecord = true; 14984 Style.BraceWrapping.AfterStruct = true; 14985 verifyFormat("class rep\n" 14986 "{\n" 14987 "};", 14988 Style); 14989 verifyFormat("struct rep\n" 14990 "{\n" 14991 "};", 14992 Style); 14993 verifyFormat("template <typename T> class rep\n" 14994 "{\n" 14995 "};", 14996 Style); 14997 verifyFormat("template <typename T> struct rep\n" 14998 "{\n" 14999 "};", 15000 Style); 15001 verifyFormat("class rep\n" 15002 "{\n" 15003 " int x;\n" 15004 "};", 15005 Style); 15006 verifyFormat("struct rep\n" 15007 "{\n" 15008 " int x;\n" 15009 "};", 15010 Style); 15011 verifyFormat("template <typename T> class rep\n" 15012 "{\n" 15013 " int x;\n" 15014 "};", 15015 Style); 15016 verifyFormat("template <typename T> struct rep\n" 15017 "{\n" 15018 " int x;\n" 15019 "};", 15020 Style); 15021 verifyFormat("template <typename T> class rep // Foo\n" 15022 "{\n" 15023 " int x;\n" 15024 "};", 15025 Style); 15026 verifyFormat("template <typename T> struct rep // Bar\n" 15027 "{\n" 15028 " int x;\n" 15029 "};", 15030 Style); 15031 15032 verifyFormat("template <typename T> class rep<T>\n" 15033 "{\n" 15034 " int x;\n" 15035 "};", 15036 Style); 15037 15038 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 15039 "{\n" 15040 " int x;\n" 15041 "};", 15042 Style); 15043 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 15044 "{\n" 15045 "};", 15046 Style); 15047 15048 verifyFormat("#include \"stdint.h\"\n" 15049 "namespace rep {}", 15050 Style); 15051 verifyFormat("#include <stdint.h>\n" 15052 "namespace rep {}", 15053 Style); 15054 verifyFormat("#include <stdint.h>\n" 15055 "namespace rep {}", 15056 "#include <stdint.h>\n" 15057 "namespace rep {\n" 15058 "\n" 15059 "\n" 15060 "}", 15061 Style); 15062 } 15063 15064 TEST_F(FormatTest, SplitEmptyStruct) { 15065 FormatStyle Style = getLLVMStyle(); 15066 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 15067 Style.BraceWrapping.AfterStruct = true; 15068 Style.BraceWrapping.SplitEmptyRecord = false; 15069 15070 verifyFormat("struct Foo\n" 15071 "{};", 15072 Style); 15073 verifyFormat("/* something */ struct Foo\n" 15074 "{};", 15075 Style); 15076 verifyFormat("template <typename X> struct Foo\n" 15077 "{};", 15078 Style); 15079 verifyFormat("struct Foo\n" 15080 "{\n" 15081 " Foo();\n" 15082 "};", 15083 Style); 15084 verifyFormat("typedef struct Foo\n" 15085 "{\n" 15086 "} Foo_t;", 15087 Style); 15088 // typedef struct Bar {} Bar_t; 15089 } 15090 15091 TEST_F(FormatTest, SplitEmptyUnion) { 15092 FormatStyle Style = getLLVMStyle(); 15093 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 15094 Style.BraceWrapping.AfterUnion = true; 15095 Style.BraceWrapping.SplitEmptyRecord = false; 15096 15097 verifyFormat("union Foo\n" 15098 "{};", 15099 Style); 15100 verifyFormat("/* something */ union Foo\n" 15101 "{};", 15102 Style); 15103 verifyFormat("union Foo\n" 15104 "{\n" 15105 " A,\n" 15106 "};", 15107 Style); 15108 verifyFormat("typedef union Foo\n" 15109 "{\n" 15110 "} Foo_t;", 15111 Style); 15112 } 15113 15114 TEST_F(FormatTest, SplitEmptyNamespace) { 15115 FormatStyle Style = getLLVMStyle(); 15116 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 15117 Style.BraceWrapping.AfterNamespace = true; 15118 Style.BraceWrapping.SplitEmptyNamespace = false; 15119 15120 verifyFormat("namespace Foo\n" 15121 "{};", 15122 Style); 15123 verifyFormat("/* something */ namespace Foo\n" 15124 "{};", 15125 Style); 15126 verifyFormat("inline namespace Foo\n" 15127 "{};", 15128 Style); 15129 verifyFormat("/* something */ inline namespace Foo\n" 15130 "{};", 15131 Style); 15132 verifyFormat("export namespace Foo\n" 15133 "{};", 15134 Style); 15135 verifyFormat("namespace Foo\n" 15136 "{\n" 15137 "void Bar();\n" 15138 "};", 15139 Style); 15140 } 15141 15142 TEST_F(FormatTest, NeverMergeShortRecords) { 15143 FormatStyle Style = getLLVMStyle(); 15144 15145 verifyFormat("class Foo {\n" 15146 " Foo();\n" 15147 "};", 15148 Style); 15149 verifyFormat("typedef class Foo {\n" 15150 " Foo();\n" 15151 "} Foo_t;", 15152 Style); 15153 verifyFormat("struct Foo {\n" 15154 " Foo();\n" 15155 "};", 15156 Style); 15157 verifyFormat("typedef struct Foo {\n" 15158 " Foo();\n" 15159 "} Foo_t;", 15160 Style); 15161 verifyFormat("union Foo {\n" 15162 " A,\n" 15163 "};", 15164 Style); 15165 verifyFormat("typedef union Foo {\n" 15166 " A,\n" 15167 "} Foo_t;", 15168 Style); 15169 verifyFormat("namespace Foo {\n" 15170 "void Bar();\n" 15171 "};", 15172 Style); 15173 15174 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 15175 Style.BraceWrapping.AfterClass = true; 15176 Style.BraceWrapping.AfterStruct = true; 15177 Style.BraceWrapping.AfterUnion = true; 15178 Style.BraceWrapping.AfterNamespace = true; 15179 verifyFormat("class Foo\n" 15180 "{\n" 15181 " Foo();\n" 15182 "};", 15183 Style); 15184 verifyFormat("typedef class Foo\n" 15185 "{\n" 15186 " Foo();\n" 15187 "} Foo_t;", 15188 Style); 15189 verifyFormat("struct Foo\n" 15190 "{\n" 15191 " Foo();\n" 15192 "};", 15193 Style); 15194 verifyFormat("typedef struct Foo\n" 15195 "{\n" 15196 " Foo();\n" 15197 "} Foo_t;", 15198 Style); 15199 verifyFormat("union Foo\n" 15200 "{\n" 15201 " A,\n" 15202 "};", 15203 Style); 15204 verifyFormat("typedef union Foo\n" 15205 "{\n" 15206 " A,\n" 15207 "} Foo_t;", 15208 Style); 15209 verifyFormat("namespace Foo\n" 15210 "{\n" 15211 "void Bar();\n" 15212 "};", 15213 Style); 15214 } 15215 15216 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 15217 // Elaborate type variable declarations. 15218 verifyFormat("struct foo a = {bar};\nint n;"); 15219 verifyFormat("class foo a = {bar};\nint n;"); 15220 verifyFormat("union foo a = {bar};\nint n;"); 15221 15222 // Elaborate types inside function definitions. 15223 verifyFormat("struct foo f() {}\nint n;"); 15224 verifyFormat("class foo f() {}\nint n;"); 15225 verifyFormat("union foo f() {}\nint n;"); 15226 15227 // Templates. 15228 verifyFormat("template <class X> void f() {}\nint n;"); 15229 verifyFormat("template <struct X> void f() {}\nint n;"); 15230 verifyFormat("template <union X> void f() {}\nint n;"); 15231 15232 // Actual definitions... 15233 verifyFormat("struct {\n} n;"); 15234 verifyFormat( 15235 "template <template <class T, class Y>, class Z> class X {\n} n;"); 15236 verifyFormat("union Z {\n int n;\n} x;"); 15237 verifyFormat("class MACRO Z {\n} n;"); 15238 verifyFormat("class MACRO(X) Z {\n} n;"); 15239 verifyFormat("class __attribute__((X)) Z {\n} n;"); 15240 verifyFormat("class __declspec(X) Z {\n} n;"); 15241 verifyFormat("class A##B##C {\n} n;"); 15242 verifyFormat("class alignas(16) Z {\n} n;"); 15243 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 15244 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 15245 15246 // Redefinition from nested context: 15247 verifyFormat("class A::B::C {\n} n;"); 15248 15249 // Template definitions. 15250 verifyFormat( 15251 "template <typename F>\n" 15252 "Matcher(const Matcher<F> &Other,\n" 15253 " typename enable_if_c<is_base_of<F, T>::value &&\n" 15254 " !is_same<F, T>::value>::type * = 0)\n" 15255 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 15256 15257 // FIXME: This is still incorrectly handled at the formatter side. 15258 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 15259 verifyFormat("int i = SomeFunction(a<b, a> b);"); 15260 15261 verifyFormat("class A<int> f() {}\n" 15262 "int n;"); 15263 verifyFormat("template <typename T> class A<T> f() {}\n" 15264 "int n;"); 15265 15266 verifyFormat("template <> class Foo<int> F() {\n" 15267 "} n;"); 15268 15269 // Elaborate types where incorrectly parsing the structural element would 15270 // break the indent. 15271 verifyFormat("if (true)\n" 15272 " class X x;\n" 15273 "else\n" 15274 " f();"); 15275 15276 // This is simply incomplete. Formatting is not important, but must not crash. 15277 verifyFormat("class A:"); 15278 } 15279 15280 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 15281 verifyNoChange("#error Leave all white!!!!! space* alone!"); 15282 verifyNoChange("#warning Leave all white!!!!! space* alone!"); 15283 verifyFormat("#error 1", " # error 1"); 15284 verifyFormat("#warning 1", " # warning 1"); 15285 } 15286 15287 TEST_F(FormatTest, FormatHashIfExpressions) { 15288 verifyFormat("#if AAAA && BBBB"); 15289 verifyFormat("#if (AAAA && BBBB)"); 15290 verifyFormat("#elif (AAAA && BBBB)"); 15291 // FIXME: Come up with a better indentation for #elif. 15292 verifyFormat( 15293 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 15294 " defined(BBBBBBBB)\n" 15295 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 15296 " defined(BBBBBBBB)\n" 15297 "#endif", 15298 getLLVMStyleWithColumns(65)); 15299 } 15300 15301 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 15302 FormatStyle AllowsMergedIf = getGoogleStyle(); 15303 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 15304 FormatStyle::SIS_WithoutElse; 15305 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 15306 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 15307 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 15308 verifyFormat("if (true) return 42;", "if (true)\nreturn 42;", AllowsMergedIf); 15309 FormatStyle ShortMergedIf = AllowsMergedIf; 15310 ShortMergedIf.ColumnLimit = 25; 15311 verifyFormat("#define A \\\n" 15312 " if (true) return 42;", 15313 ShortMergedIf); 15314 verifyFormat("#define A \\\n" 15315 " f(); \\\n" 15316 " if (true)\n" 15317 "#define B", 15318 ShortMergedIf); 15319 verifyFormat("#define A \\\n" 15320 " f(); \\\n" 15321 " if (true)\n" 15322 "g();", 15323 ShortMergedIf); 15324 verifyFormat("{\n" 15325 "#ifdef A\n" 15326 " // Comment\n" 15327 " if (true) continue;\n" 15328 "#endif\n" 15329 " // Comment\n" 15330 " if (true) continue;\n" 15331 "}", 15332 ShortMergedIf); 15333 ShortMergedIf.ColumnLimit = 33; 15334 verifyFormat("#define A \\\n" 15335 " if constexpr (true) return 42;", 15336 ShortMergedIf); 15337 verifyFormat("#define A \\\n" 15338 " if CONSTEXPR (true) return 42;", 15339 ShortMergedIf); 15340 ShortMergedIf.ColumnLimit = 29; 15341 verifyFormat("#define A \\\n" 15342 " if (aaaaaaaaaa) return 1; \\\n" 15343 " return 2;", 15344 ShortMergedIf); 15345 ShortMergedIf.ColumnLimit = 28; 15346 verifyFormat("#define A \\\n" 15347 " if (aaaaaaaaaa) \\\n" 15348 " return 1; \\\n" 15349 " return 2;", 15350 ShortMergedIf); 15351 verifyFormat("#define A \\\n" 15352 " if constexpr (aaaaaaa) \\\n" 15353 " return 1; \\\n" 15354 " return 2;", 15355 ShortMergedIf); 15356 verifyFormat("#define A \\\n" 15357 " if CONSTEXPR (aaaaaaa) \\\n" 15358 " return 1; \\\n" 15359 " return 2;", 15360 ShortMergedIf); 15361 15362 verifyFormat("//\n" 15363 "#define a \\\n" 15364 " if \\\n" 15365 " 0", 15366 getChromiumStyle(FormatStyle::LK_Cpp)); 15367 } 15368 15369 TEST_F(FormatTest, FormatStarDependingOnContext) { 15370 verifyFormat("void f(int *a);"); 15371 verifyFormat("void f() { f(fint * b); }"); 15372 verifyFormat("class A {\n void f(int *a);\n};"); 15373 verifyFormat("class A {\n int *a;\n};"); 15374 verifyFormat("namespace a {\n" 15375 "namespace b {\n" 15376 "class A {\n" 15377 " void f() {}\n" 15378 " int *a;\n" 15379 "};\n" 15380 "} // namespace b\n" 15381 "} // namespace a"); 15382 } 15383 15384 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 15385 verifyFormat("while"); 15386 verifyFormat("operator"); 15387 } 15388 15389 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 15390 // This code would be painfully slow to format if we didn't skip it. 15391 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 15392 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 15393 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 15394 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 15395 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 15396 "A(1, 1)\n" 15397 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 15398 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15399 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15400 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15401 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15402 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15403 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15404 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15405 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 15406 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 15407 // Deeply nested part is untouched, rest is formatted. 15408 EXPECT_EQ(std::string("int i;") + Code + "int j;", 15409 format(std::string("int i;") + Code + "int j;", 15410 getLLVMStyle(), SC_ExpectIncomplete)); 15411 } 15412 15413 //===----------------------------------------------------------------------===// 15414 // Objective-C tests. 15415 //===----------------------------------------------------------------------===// 15416 15417 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 15418 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 15419 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject;", 15420 "-(NSUInteger)indexOfObject:(id)anObject;"); 15421 verifyFormat("- (NSInteger)Mthod1;", "-(NSInteger)Mthod1;"); 15422 verifyFormat("+ (id)Mthod2;", "+(id)Mthod2;"); 15423 verifyFormat("- (NSInteger)Method3:(id)anObject;", 15424 "-(NSInteger)Method3:(id)anObject;"); 15425 verifyFormat("- (NSInteger)Method4:(id)anObject;", 15426 "-(NSInteger)Method4:(id)anObject;"); 15427 verifyFormat("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 15428 "-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"); 15429 verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"); 15430 verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject " 15431 "forAllCells:(BOOL)flag;"); 15432 15433 // Very long objectiveC method declaration. 15434 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 15435 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 15436 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 15437 " inRange:(NSRange)range\n" 15438 " outRange:(NSRange)out_range\n" 15439 " outRange1:(NSRange)out_range1\n" 15440 " outRange2:(NSRange)out_range2\n" 15441 " outRange3:(NSRange)out_range3\n" 15442 " outRange4:(NSRange)out_range4\n" 15443 " outRange5:(NSRange)out_range5\n" 15444 " outRange6:(NSRange)out_range6\n" 15445 " outRange7:(NSRange)out_range7\n" 15446 " outRange8:(NSRange)out_range8\n" 15447 " outRange9:(NSRange)out_range9;"); 15448 15449 // When the function name has to be wrapped. 15450 FormatStyle Style = getLLVMStyle(); 15451 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 15452 // and always indents instead. 15453 Style.IndentWrappedFunctionNames = false; 15454 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 15455 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 15456 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 15457 "}", 15458 Style); 15459 Style.IndentWrappedFunctionNames = true; 15460 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 15461 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 15462 " anotherName:(NSString)dddddddddddddd {\n" 15463 "}", 15464 Style); 15465 15466 verifyFormat("- (int)sum:(vector<int>)numbers;"); 15467 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 15468 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 15469 // protocol lists (but not for template classes): 15470 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 15471 15472 verifyFormat("- (int (*)())foo:(int (*)())f;"); 15473 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 15474 15475 // If there's no return type (very rare in practice!), LLVM and Google style 15476 // agree. 15477 verifyFormat("- foo;"); 15478 verifyFormat("- foo:(int)f;"); 15479 verifyGoogleFormat("- foo:(int)foo;"); 15480 } 15481 15482 TEST_F(FormatTest, BreaksStringLiterals) { 15483 // FIXME: unstable test case 15484 EXPECT_EQ("\"some text \"\n" 15485 "\"other\";", 15486 format("\"some text other\";", getLLVMStyleWithColumns(12))); 15487 // FIXME: unstable test case 15488 EXPECT_EQ("\"some text \"\n" 15489 "\"other\";", 15490 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 15491 verifyFormat("#define A \\\n" 15492 " \"some \" \\\n" 15493 " \"text \" \\\n" 15494 " \"other\";", 15495 "#define A \"some text other\";", getLLVMStyleWithColumns(12)); 15496 verifyFormat("#define A \\\n" 15497 " \"so \" \\\n" 15498 " \"text \" \\\n" 15499 " \"other\";", 15500 "#define A \"so text other\";", getLLVMStyleWithColumns(12)); 15501 15502 verifyFormat("\"some text\"", getLLVMStyleWithColumns(1)); 15503 verifyFormat("\"some text\"", getLLVMStyleWithColumns(11)); 15504 // FIXME: unstable test case 15505 EXPECT_EQ("\"some \"\n" 15506 "\"text\"", 15507 format("\"some text\"", getLLVMStyleWithColumns(10))); 15508 // FIXME: unstable test case 15509 EXPECT_EQ("\"some \"\n" 15510 "\"text\"", 15511 format("\"some text\"", getLLVMStyleWithColumns(7))); 15512 // FIXME: unstable test case 15513 EXPECT_EQ("\"some\"\n" 15514 "\" tex\"\n" 15515 "\"t\"", 15516 format("\"some text\"", getLLVMStyleWithColumns(6))); 15517 // FIXME: unstable test case 15518 EXPECT_EQ("\"some\"\n" 15519 "\" tex\"\n" 15520 "\" and\"", 15521 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 15522 // FIXME: unstable test case 15523 EXPECT_EQ("\"some\"\n" 15524 "\"/tex\"\n" 15525 "\"/and\"", 15526 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 15527 15528 verifyFormat("variable =\n" 15529 " \"long string \"\n" 15530 " \"literal\";", 15531 "variable = \"long string literal\";", 15532 getLLVMStyleWithColumns(20)); 15533 15534 verifyFormat("variable = f(\n" 15535 " \"long string \"\n" 15536 " \"literal\",\n" 15537 " short,\n" 15538 " loooooooooooooooooooong);", 15539 "variable = f(\"long string literal\", short, " 15540 "loooooooooooooooooooong);", 15541 getLLVMStyleWithColumns(20)); 15542 15543 verifyFormat("f(g(\"long string \"\n" 15544 " \"literal\"),\n" 15545 " b);", 15546 "f(g(\"long string literal\"), b);", 15547 getLLVMStyleWithColumns(20)); 15548 verifyFormat("f(g(\"long string \"\n" 15549 " \"literal\",\n" 15550 " a),\n" 15551 " b);", 15552 "f(g(\"long string literal\", a), b);", 15553 getLLVMStyleWithColumns(20)); 15554 verifyFormat("f(\"one two\".split(\n" 15555 " variable));", 15556 "f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)); 15557 verifyFormat("f(\"one two three four five six \"\n" 15558 " \"seven\".split(\n" 15559 " really_looooong_variable));", 15560 "f(\"one two three four five six seven\"." 15561 "split(really_looooong_variable));", 15562 getLLVMStyleWithColumns(33)); 15563 15564 verifyFormat("f(\"some \"\n" 15565 " \"text\",\n" 15566 " other);", 15567 "f(\"some text\", other);", getLLVMStyleWithColumns(10)); 15568 15569 // Only break as a last resort. 15570 verifyFormat( 15571 "aaaaaaaaaaaaaaaaaaaa(\n" 15572 " aaaaaaaaaaaaaaaaaaaa,\n" 15573 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 15574 15575 // FIXME: unstable test case 15576 EXPECT_EQ("\"splitmea\"\n" 15577 "\"trandomp\"\n" 15578 "\"oint\"", 15579 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 15580 15581 // FIXME: unstable test case 15582 EXPECT_EQ("\"split/\"\n" 15583 "\"pathat/\"\n" 15584 "\"slashes\"", 15585 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 15586 15587 // FIXME: unstable test case 15588 EXPECT_EQ("\"split/\"\n" 15589 "\"pathat/\"\n" 15590 "\"slashes\"", 15591 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 15592 // FIXME: unstable test case 15593 EXPECT_EQ("\"split at \"\n" 15594 "\"spaces/at/\"\n" 15595 "\"slashes.at.any$\"\n" 15596 "\"non-alphanumeric%\"\n" 15597 "\"1111111111characte\"\n" 15598 "\"rs\"", 15599 format("\"split at " 15600 "spaces/at/" 15601 "slashes.at." 15602 "any$non-" 15603 "alphanumeric%" 15604 "1111111111characte" 15605 "rs\"", 15606 getLLVMStyleWithColumns(20))); 15607 15608 // Verify that splitting the strings understands 15609 // Style::AlwaysBreakBeforeMultilineStrings. 15610 verifyFormat("aaaaaaaaaaaa(\n" 15611 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 15612 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 15613 "aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 15614 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 15615 "aaaaaaaaaaaaaaaaaaaaaa\");", 15616 getGoogleStyle()); 15617 verifyFormat("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 15618 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 15619 "return \"aaaaaaaaaaaaaaaaaaaaaa " 15620 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 15621 "aaaaaaaaaaaaaaaaaaaaaa\";", 15622 getGoogleStyle()); 15623 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 15624 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 15625 "llvm::outs() << " 15626 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 15627 "aaaaaaaaaaaaaaaaaaa\";"); 15628 verifyFormat("ffff(\n" 15629 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 15630 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 15631 "ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 15632 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 15633 getGoogleStyle()); 15634 15635 FormatStyle Style = getLLVMStyleWithColumns(12); 15636 Style.BreakStringLiterals = false; 15637 verifyFormat("\"some text other\";", Style); 15638 15639 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 15640 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15641 verifyFormat("#define A \\\n" 15642 " \"some \" \\\n" 15643 " \"text \" \\\n" 15644 " \"other\";", 15645 "#define A \"some text other\";", AlignLeft); 15646 } 15647 15648 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 15649 verifyFormat("C a = \"some more \"\n" 15650 " \"text\";", 15651 "C a = \"some more text\";", getLLVMStyleWithColumns(18)); 15652 } 15653 15654 TEST_F(FormatTest, FullyRemoveEmptyLines) { 15655 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 15656 NoEmptyLines.MaxEmptyLinesToKeep = 0; 15657 verifyFormat("int i = a(b());", "int i=a(\n\n b(\n\n\n )\n\n);", 15658 NoEmptyLines); 15659 } 15660 15661 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 15662 // FIXME: unstable test case 15663 EXPECT_EQ( 15664 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 15665 "(\n" 15666 " \"x\t\");", 15667 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 15668 "aaaaaaa(" 15669 "\"x\t\");")); 15670 } 15671 15672 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 15673 // FIXME: unstable test case 15674 EXPECT_EQ( 15675 "u8\"utf8 string \"\n" 15676 "u8\"literal\";", 15677 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 15678 // FIXME: unstable test case 15679 EXPECT_EQ( 15680 "u\"utf16 string \"\n" 15681 "u\"literal\";", 15682 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 15683 // FIXME: unstable test case 15684 EXPECT_EQ( 15685 "U\"utf32 string \"\n" 15686 "U\"literal\";", 15687 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 15688 // FIXME: unstable test case 15689 EXPECT_EQ("L\"wide string \"\n" 15690 "L\"literal\";", 15691 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 15692 verifyFormat("@\"NSString \"\n" 15693 "@\"literal\";", 15694 "@\"NSString literal\";", getGoogleStyleWithColumns(19)); 15695 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 15696 15697 // This input makes clang-format try to split the incomplete unicode escape 15698 // sequence, which used to lead to a crasher. 15699 verifyNoCrash( 15700 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 15701 getLLVMStyleWithColumns(60)); 15702 } 15703 15704 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 15705 FormatStyle Style = getGoogleStyleWithColumns(15); 15706 verifyFormat("R\"x(raw literal)x\";", Style); 15707 verifyFormat("uR\"x(raw literal)x\";", Style); 15708 verifyFormat("LR\"x(raw literal)x\";", Style); 15709 verifyFormat("UR\"x(raw literal)x\";", Style); 15710 verifyFormat("u8R\"x(raw literal)x\";", Style); 15711 } 15712 15713 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 15714 FormatStyle Style = getLLVMStyleWithColumns(20); 15715 // FIXME: unstable test case 15716 EXPECT_EQ( 15717 "_T(\"aaaaaaaaaaaaaa\")\n" 15718 "_T(\"aaaaaaaaaaaaaa\")\n" 15719 "_T(\"aaaaaaaaaaaa\")", 15720 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 15721 verifyFormat("f(x,\n" 15722 " _T(\"aaaaaaaaaaaa\")\n" 15723 " _T(\"aaa\"),\n" 15724 " z);", 15725 "f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style); 15726 15727 // FIXME: Handle embedded spaces in one iteration. 15728 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 15729 // "_T(\"aaaaaaaaaaaaa\")\n" 15730 // "_T(\"aaaaaaaaaaaaa\")\n" 15731 // "_T(\"a\")", 15732 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 15733 // getLLVMStyleWithColumns(20))); 15734 verifyFormat("_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 15735 " _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style); 15736 verifyFormat("f(\n" 15737 "#if !TEST\n" 15738 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 15739 "#endif\n" 15740 ");", 15741 "f(\n" 15742 "#if !TEST\n" 15743 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 15744 "#endif\n" 15745 ");"); 15746 verifyFormat("f(\n" 15747 "\n" 15748 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 15749 "f(\n" 15750 "\n" 15751 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"); 15752 // Regression test for accessing tokens past the end of a vector in the 15753 // TokenLexer. 15754 verifyNoCrash(R"(_T( 15755 " 15756 ) 15757 )"); 15758 } 15759 15760 TEST_F(FormatTest, BreaksStringLiteralOperands) { 15761 // In a function call with two operands, the second can be broken with no line 15762 // break before it. 15763 verifyFormat("func(a, \"long long \"\n" 15764 " \"long long\");", 15765 "func(a, \"long long long long\");", 15766 getLLVMStyleWithColumns(24)); 15767 // In a function call with three operands, the second must be broken with a 15768 // line break before it. 15769 verifyFormat("func(a,\n" 15770 " \"long long long \"\n" 15771 " \"long\",\n" 15772 " c);", 15773 "func(a, \"long long long long\", c);", 15774 getLLVMStyleWithColumns(24)); 15775 // In a function call with three operands, the third must be broken with a 15776 // line break before it. 15777 verifyFormat("func(a, b,\n" 15778 " \"long long long \"\n" 15779 " \"long\");", 15780 "func(a, b, \"long long long long\");", 15781 getLLVMStyleWithColumns(24)); 15782 // In a function call with three operands, both the second and the third must 15783 // be broken with a line break before them. 15784 verifyFormat("func(a,\n" 15785 " \"long long long \"\n" 15786 " \"long\",\n" 15787 " \"long long long \"\n" 15788 " \"long\");", 15789 "func(a, \"long long long long\", \"long long long long\");", 15790 getLLVMStyleWithColumns(24)); 15791 // In a chain of << with two operands, the second can be broken with no line 15792 // break before it. 15793 verifyFormat("a << \"line line \"\n" 15794 " \"line\";", 15795 "a << \"line line line\";", getLLVMStyleWithColumns(20)); 15796 // In a chain of << with three operands, the second can be broken with no line 15797 // break before it. 15798 verifyFormat("abcde << \"line \"\n" 15799 " \"line line\"\n" 15800 " << c;", 15801 "abcde << \"line line line\" << c;", 15802 getLLVMStyleWithColumns(20)); 15803 // In a chain of << with three operands, the third must be broken with a line 15804 // break before it. 15805 verifyFormat("a << b\n" 15806 " << \"line line \"\n" 15807 " \"line\";", 15808 "a << b << \"line line line\";", getLLVMStyleWithColumns(20)); 15809 // In a chain of << with three operands, the second can be broken with no line 15810 // break before it and the third must be broken with a line break before it. 15811 verifyFormat("abcd << \"line line \"\n" 15812 " \"line\"\n" 15813 " << \"line line \"\n" 15814 " \"line\";", 15815 "abcd << \"line line line\" << \"line line line\";", 15816 getLLVMStyleWithColumns(20)); 15817 // In a chain of binary operators with two operands, the second can be broken 15818 // with no line break before it. 15819 verifyFormat("abcd + \"line line \"\n" 15820 " \"line line\";", 15821 "abcd + \"line line line line\";", getLLVMStyleWithColumns(20)); 15822 // In a chain of binary operators with three operands, the second must be 15823 // broken with a line break before it. 15824 verifyFormat("abcd +\n" 15825 " \"line line \"\n" 15826 " \"line line\" +\n" 15827 " e;", 15828 "abcd + \"line line line line\" + e;", 15829 getLLVMStyleWithColumns(20)); 15830 // In a function call with two operands, with AlignAfterOpenBracket enabled, 15831 // the first must be broken with a line break before it. 15832 FormatStyle Style = getLLVMStyleWithColumns(25); 15833 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 15834 verifyFormat("someFunction(\n" 15835 " \"long long long \"\n" 15836 " \"long\",\n" 15837 " a);", 15838 "someFunction(\"long long long long\", a);", Style); 15839 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 15840 verifyFormat("someFunction(\n" 15841 " \"long long long \"\n" 15842 " \"long\",\n" 15843 " a\n" 15844 ");", 15845 Style); 15846 } 15847 15848 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 15849 verifyFormat("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 15850 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 15851 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 15852 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 15853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 15854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"); 15855 } 15856 15857 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 15858 verifyFormat("f(g(R\"x(raw literal)x\", a), b);", 15859 "f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()); 15860 verifyFormat("fffffffffff(g(R\"x(\n" 15861 "multiline raw string literal xxxxxxxxxxxxxx\n" 15862 ")x\",\n" 15863 " a),\n" 15864 " b);", 15865 "fffffffffff(g(R\"x(\n" 15866 "multiline raw string literal xxxxxxxxxxxxxx\n" 15867 ")x\", a), b);", 15868 getGoogleStyleWithColumns(20)); 15869 verifyFormat("fffffffffff(\n" 15870 " g(R\"x(qqq\n" 15871 "multiline raw string literal xxxxxxxxxxxxxx\n" 15872 ")x\",\n" 15873 " a),\n" 15874 " b);", 15875 "fffffffffff(g(R\"x(qqq\n" 15876 "multiline raw string literal xxxxxxxxxxxxxx\n" 15877 ")x\", a), b);", 15878 getGoogleStyleWithColumns(20)); 15879 15880 verifyNoChange("fffffffffff(R\"x(\n" 15881 "multiline raw string literal xxxxxxxxxxxxxx\n" 15882 ")x\");", 15883 getGoogleStyleWithColumns(20)); 15884 verifyFormat("fffffffffff(R\"x(\n" 15885 "multiline raw string literal xxxxxxxxxxxxxx\n" 15886 ")x\" + bbbbbb);", 15887 "fffffffffff(R\"x(\n" 15888 "multiline raw string literal xxxxxxxxxxxxxx\n" 15889 ")x\" + bbbbbb);", 15890 getGoogleStyleWithColumns(20)); 15891 verifyFormat("fffffffffff(\n" 15892 " R\"x(\n" 15893 "multiline raw string literal xxxxxxxxxxxxxx\n" 15894 ")x\" +\n" 15895 " bbbbbb);", 15896 "fffffffffff(\n" 15897 " R\"x(\n" 15898 "multiline raw string literal xxxxxxxxxxxxxx\n" 15899 ")x\" + bbbbbb);", 15900 getGoogleStyleWithColumns(20)); 15901 verifyFormat("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 15902 "fffffffffff(\n" 15903 " R\"(single line raw string)\" + bbbbbb);"); 15904 } 15905 15906 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 15907 verifyFormat("string a = \"unterminated;"); 15908 verifyFormat("function(\"unterminated,\n" 15909 " OtherParameter);", 15910 "function( \"unterminated,\n" 15911 " OtherParameter);"); 15912 } 15913 15914 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 15915 FormatStyle Style = getLLVMStyle(); 15916 Style.Standard = FormatStyle::LS_Cpp03; 15917 verifyFormat("#define x(_a) printf(\"foo\" _a);", 15918 "#define x(_a) printf(\"foo\"_a);", Style); 15919 } 15920 15921 TEST_F(FormatTest, CppLexVersion) { 15922 FormatStyle Style = getLLVMStyle(); 15923 // Formatting of x * y differs if x is a type. 15924 verifyFormat("void foo() { MACRO(a * b); }", Style); 15925 verifyFormat("void foo() { MACRO(int *b); }", Style); 15926 15927 // LLVM style uses latest lexer. 15928 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 15929 Style.Standard = FormatStyle::LS_Cpp17; 15930 // But in c++17, char8_t isn't a keyword. 15931 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 15932 } 15933 15934 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 15935 15936 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 15937 verifyFormat("someFunction(\"aaabbbcccd\"\n" 15938 " \"ddeeefff\");", 15939 "someFunction(\"aaabbbcccdddeeefff\");", 15940 getLLVMStyleWithColumns(25)); 15941 verifyFormat("someFunction1234567890(\n" 15942 " \"aaabbbcccdddeeefff\");", 15943 "someFunction1234567890(\"aaabbbcccdddeeefff\");", 15944 getLLVMStyleWithColumns(26)); 15945 verifyFormat("someFunction1234567890(\n" 15946 " \"aaabbbcccdddeeeff\"\n" 15947 " \"f\");", 15948 "someFunction1234567890(\"aaabbbcccdddeeefff\");", 15949 getLLVMStyleWithColumns(25)); 15950 verifyFormat("someFunction1234567890(\n" 15951 " \"aaabbbcccdddeeeff\"\n" 15952 " \"f\");", 15953 "someFunction1234567890(\"aaabbbcccdddeeefff\");", 15954 getLLVMStyleWithColumns(24)); 15955 verifyFormat("someFunction(\n" 15956 " \"aaabbbcc ddde \"\n" 15957 " \"efff\");", 15958 "someFunction(\"aaabbbcc ddde efff\");", 15959 getLLVMStyleWithColumns(25)); 15960 verifyFormat("someFunction(\"aaabbbccc \"\n" 15961 " \"ddeeefff\");", 15962 "someFunction(\"aaabbbccc ddeeefff\");", 15963 getLLVMStyleWithColumns(25)); 15964 verifyFormat("someFunction1234567890(\n" 15965 " \"aaabb \"\n" 15966 " \"cccdddeeefff\");", 15967 "someFunction1234567890(\"aaabb cccdddeeefff\");", 15968 getLLVMStyleWithColumns(25)); 15969 verifyFormat("#define A \\\n" 15970 " string s = \\\n" 15971 " \"123456789\" \\\n" 15972 " \"0\"; \\\n" 15973 " int i;", 15974 "#define A string s = \"1234567890\"; int i;", 15975 getLLVMStyleWithColumns(20)); 15976 verifyFormat("someFunction(\n" 15977 " \"aaabbbcc \"\n" 15978 " \"dddeeefff\");", 15979 "someFunction(\"aaabbbcc dddeeefff\");", 15980 getLLVMStyleWithColumns(25)); 15981 } 15982 15983 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 15984 verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3)); 15985 verifyFormat("\"\\\"", getLLVMStyleWithColumns(2)); 15986 // FIXME: unstable test case 15987 EXPECT_EQ("\"test\"\n" 15988 "\"\\n\"", 15989 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 15990 // FIXME: unstable test case 15991 EXPECT_EQ("\"tes\\\\\"\n" 15992 "\"n\"", 15993 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 15994 // FIXME: unstable test case 15995 EXPECT_EQ("\"\\\\\\\\\"\n" 15996 "\"\\n\"", 15997 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 15998 verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7)); 15999 // FIXME: unstable test case 16000 EXPECT_EQ("\"\\uff01\"\n" 16001 "\"test\"", 16002 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 16003 verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)); 16004 // FIXME: unstable test case 16005 EXPECT_EQ("\"\\x000000000001\"\n" 16006 "\"next\"", 16007 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 16008 verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)); 16009 verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7)); 16010 // FIXME: unstable test case 16011 EXPECT_EQ("\"test\"\n" 16012 "\"\\000000\"\n" 16013 "\"000001\"", 16014 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 16015 // FIXME: unstable test case 16016 EXPECT_EQ("\"test\\000\"\n" 16017 "\"00000000\"\n" 16018 "\"1\"", 16019 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 16020 } 16021 16022 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 16023 verifyFormat("void f() {\n" 16024 " return g() {}\n" 16025 " void h() {}"); 16026 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 16027 "g();\n" 16028 "}"); 16029 } 16030 16031 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 16032 verifyFormat( 16033 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 16034 } 16035 16036 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 16037 verifyFormat("class X {\n" 16038 " void f() {\n" 16039 " }\n" 16040 "};", 16041 getLLVMStyleWithColumns(12)); 16042 } 16043 16044 TEST_F(FormatTest, ConfigurableIndentWidth) { 16045 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 16046 EightIndent.IndentWidth = 8; 16047 EightIndent.ContinuationIndentWidth = 8; 16048 verifyFormat("void f() {\n" 16049 " someFunction();\n" 16050 " if (true) {\n" 16051 " f();\n" 16052 " }\n" 16053 "}", 16054 EightIndent); 16055 verifyFormat("class X {\n" 16056 " void f() {\n" 16057 " }\n" 16058 "};", 16059 EightIndent); 16060 verifyFormat("int x[] = {\n" 16061 " call(),\n" 16062 " call()};", 16063 EightIndent); 16064 } 16065 16066 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 16067 verifyFormat("double\n" 16068 "f();", 16069 getLLVMStyleWithColumns(8)); 16070 } 16071 16072 TEST_F(FormatTest, ConfigurableUseOfTab) { 16073 FormatStyle Tab = getLLVMStyleWithColumns(42); 16074 Tab.IndentWidth = 8; 16075 Tab.UseTab = FormatStyle::UT_Always; 16076 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16077 16078 verifyFormat("if (aaaaaaaa && // q\n" 16079 " bb)\t\t// w\n" 16080 "\t;", 16081 "if (aaaaaaaa &&// q\n" 16082 "bb)// w\n" 16083 ";", 16084 Tab); 16085 verifyFormat("if (aaa && bbb) // w\n" 16086 "\t;", 16087 "if(aaa&&bbb)// w\n" 16088 ";", 16089 Tab); 16090 16091 verifyFormat("class X {\n" 16092 "\tvoid f() {\n" 16093 "\t\tsomeFunction(parameter1,\n" 16094 "\t\t\t parameter2);\n" 16095 "\t}\n" 16096 "};", 16097 Tab); 16098 verifyFormat("#define A \\\n" 16099 "\tvoid f() { \\\n" 16100 "\t\tsomeFunction( \\\n" 16101 "\t\t parameter1, \\\n" 16102 "\t\t parameter2); \\\n" 16103 "\t}", 16104 Tab); 16105 verifyFormat("int a;\t // x\n" 16106 "int bbbbbbbb; // x", 16107 Tab); 16108 16109 FormatStyle TabAlignment = Tab; 16110 TabAlignment.AlignConsecutiveDeclarations.Enabled = true; 16111 TabAlignment.PointerAlignment = FormatStyle::PAS_Left; 16112 verifyFormat("unsigned long long big;\n" 16113 "char*\t\t ptr;", 16114 TabAlignment); 16115 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle; 16116 verifyFormat("unsigned long long big;\n" 16117 "char *\t\t ptr;", 16118 TabAlignment); 16119 TabAlignment.PointerAlignment = FormatStyle::PAS_Right; 16120 verifyFormat("unsigned long long big;\n" 16121 "char\t\t *ptr;", 16122 TabAlignment); 16123 16124 Tab.TabWidth = 4; 16125 Tab.IndentWidth = 8; 16126 verifyFormat("class TabWidth4Indent8 {\n" 16127 "\t\tvoid f() {\n" 16128 "\t\t\t\tsomeFunction(parameter1,\n" 16129 "\t\t\t\t\t\t\t parameter2);\n" 16130 "\t\t}\n" 16131 "};", 16132 Tab); 16133 16134 Tab.TabWidth = 4; 16135 Tab.IndentWidth = 4; 16136 verifyFormat("class TabWidth4Indent4 {\n" 16137 "\tvoid f() {\n" 16138 "\t\tsomeFunction(parameter1,\n" 16139 "\t\t\t\t\t parameter2);\n" 16140 "\t}\n" 16141 "};", 16142 Tab); 16143 16144 Tab.TabWidth = 8; 16145 Tab.IndentWidth = 4; 16146 verifyFormat("class TabWidth8Indent4 {\n" 16147 " void f() {\n" 16148 "\tsomeFunction(parameter1,\n" 16149 "\t\t parameter2);\n" 16150 " }\n" 16151 "};", 16152 Tab); 16153 16154 Tab.TabWidth = 8; 16155 Tab.IndentWidth = 8; 16156 verifyFormat("/*\n" 16157 "\t a\t\tcomment\n" 16158 "\t in multiple lines\n" 16159 " */", 16160 " /*\t \t \n" 16161 " \t \t a\t\tcomment\t \t\n" 16162 " \t \t in multiple lines\t\n" 16163 " \t */", 16164 Tab); 16165 16166 TabAlignment.UseTab = FormatStyle::UT_ForIndentation; 16167 TabAlignment.PointerAlignment = FormatStyle::PAS_Left; 16168 verifyFormat("void f() {\n" 16169 "\tunsigned long long big;\n" 16170 "\tchar* ptr;\n" 16171 "}", 16172 TabAlignment); 16173 TabAlignment.PointerAlignment = FormatStyle::PAS_Middle; 16174 verifyFormat("void f() {\n" 16175 "\tunsigned long long big;\n" 16176 "\tchar * ptr;\n" 16177 "}", 16178 TabAlignment); 16179 TabAlignment.PointerAlignment = FormatStyle::PAS_Right; 16180 verifyFormat("void f() {\n" 16181 "\tunsigned long long big;\n" 16182 "\tchar *ptr;\n" 16183 "}", 16184 TabAlignment); 16185 16186 Tab.UseTab = FormatStyle::UT_ForIndentation; 16187 verifyFormat("{\n" 16188 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16189 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16190 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16191 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16192 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16193 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16194 "};", 16195 Tab); 16196 verifyFormat("enum AA {\n" 16197 "\ta1, // Force multiple lines\n" 16198 "\ta2,\n" 16199 "\ta3\n" 16200 "};", 16201 Tab); 16202 verifyFormat("if (aaaaaaaa && // q\n" 16203 " bb) // w\n" 16204 "\t;", 16205 "if (aaaaaaaa &&// q\n" 16206 "bb)// w\n" 16207 ";", 16208 Tab); 16209 verifyFormat("class X {\n" 16210 "\tvoid f() {\n" 16211 "\t\tsomeFunction(parameter1,\n" 16212 "\t\t parameter2);\n" 16213 "\t}\n" 16214 "};", 16215 Tab); 16216 verifyFormat("{\n" 16217 "\tQ(\n" 16218 "\t {\n" 16219 "\t\t int a;\n" 16220 "\t\t someFunction(aaaaaaaa,\n" 16221 "\t\t bbbbbbb);\n" 16222 "\t },\n" 16223 "\t p);\n" 16224 "}", 16225 Tab); 16226 verifyFormat("{\n" 16227 "\t/* aaaa\n" 16228 "\t bbbb */\n" 16229 "}", 16230 "{\n" 16231 "/* aaaa\n" 16232 " bbbb */\n" 16233 "}", 16234 Tab); 16235 verifyFormat("{\n" 16236 "\t/*\n" 16237 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16238 "\t bbbbbbbbbbbbb\n" 16239 "\t*/\n" 16240 "}", 16241 "{\n" 16242 "/*\n" 16243 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16244 "*/\n" 16245 "}", 16246 Tab); 16247 verifyFormat("{\n" 16248 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16249 "\t// bbbbbbbbbbbbb\n" 16250 "}", 16251 "{\n" 16252 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16253 "}", 16254 Tab); 16255 verifyFormat("{\n" 16256 "\t/*\n" 16257 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16258 "\t bbbbbbbbbbbbb\n" 16259 "\t*/\n" 16260 "}", 16261 "{\n" 16262 "\t/*\n" 16263 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16264 "\t*/\n" 16265 "}", 16266 Tab); 16267 verifyNoChange("{\n" 16268 "\t/*\n" 16269 "\n" 16270 "\t*/\n" 16271 "}", 16272 Tab); 16273 verifyNoChange("{\n" 16274 "\t/*\n" 16275 " asdf\n" 16276 "\t*/\n" 16277 "}", 16278 Tab); 16279 16280 verifyFormat("void f() {\n" 16281 "\treturn true ? aaaaaaaaaaaaaaaaaa\n" 16282 "\t : bbbbbbbbbbbbbbbbbb\n" 16283 "}", 16284 Tab); 16285 FormatStyle TabNoBreak = Tab; 16286 TabNoBreak.BreakBeforeTernaryOperators = false; 16287 verifyFormat("void f() {\n" 16288 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n" 16289 "\t bbbbbbbbbbbbbbbbbb\n" 16290 "}", 16291 TabNoBreak); 16292 verifyFormat("void f() {\n" 16293 "\treturn true ?\n" 16294 "\t aaaaaaaaaaaaaaaaaaaa :\n" 16295 "\t bbbbbbbbbbbbbbbbbbbb\n" 16296 "}", 16297 TabNoBreak); 16298 16299 Tab.UseTab = FormatStyle::UT_Never; 16300 verifyFormat("/*\n" 16301 " a\t\tcomment\n" 16302 " in multiple lines\n" 16303 " */", 16304 " /*\t \t \n" 16305 " \t \t a\t\tcomment\t \t\n" 16306 " \t \t in multiple lines\t\n" 16307 " \t */", 16308 Tab); 16309 verifyFormat("/* some\n" 16310 " comment */", 16311 " \t \t /* some\n" 16312 " \t \t comment */", 16313 Tab); 16314 verifyFormat("int a; /* some\n" 16315 " comment */", 16316 " \t \t int a; /* some\n" 16317 " \t \t comment */", 16318 Tab); 16319 16320 verifyFormat("int a; /* some\n" 16321 "comment */", 16322 " \t \t int\ta; /* some\n" 16323 " \t \t comment */", 16324 Tab); 16325 verifyFormat("f(\"\t\t\"); /* some\n" 16326 " comment */", 16327 " \t \t f(\"\t\t\"); /* some\n" 16328 " \t \t comment */", 16329 Tab); 16330 verifyFormat("{\n" 16331 " /*\n" 16332 " * Comment\n" 16333 " */\n" 16334 " int i;\n" 16335 "}", 16336 "{\n" 16337 "\t/*\n" 16338 "\t * Comment\n" 16339 "\t */\n" 16340 "\t int i;\n" 16341 "}", 16342 Tab); 16343 16344 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 16345 Tab.TabWidth = 8; 16346 Tab.IndentWidth = 8; 16347 verifyFormat("if (aaaaaaaa && // q\n" 16348 " bb) // w\n" 16349 "\t;", 16350 "if (aaaaaaaa &&// q\n" 16351 "bb)// w\n" 16352 ";", 16353 Tab); 16354 verifyFormat("if (aaa && bbb) // w\n" 16355 "\t;", 16356 "if(aaa&&bbb)// w\n" 16357 ";", 16358 Tab); 16359 verifyFormat("class X {\n" 16360 "\tvoid f() {\n" 16361 "\t\tsomeFunction(parameter1,\n" 16362 "\t\t\t parameter2);\n" 16363 "\t}\n" 16364 "};", 16365 Tab); 16366 verifyFormat("#define A \\\n" 16367 "\tvoid f() { \\\n" 16368 "\t\tsomeFunction( \\\n" 16369 "\t\t parameter1, \\\n" 16370 "\t\t parameter2); \\\n" 16371 "\t}", 16372 Tab); 16373 Tab.TabWidth = 4; 16374 Tab.IndentWidth = 8; 16375 verifyFormat("class TabWidth4Indent8 {\n" 16376 "\t\tvoid f() {\n" 16377 "\t\t\t\tsomeFunction(parameter1,\n" 16378 "\t\t\t\t\t\t\t parameter2);\n" 16379 "\t\t}\n" 16380 "};", 16381 Tab); 16382 Tab.TabWidth = 4; 16383 Tab.IndentWidth = 4; 16384 verifyFormat("class TabWidth4Indent4 {\n" 16385 "\tvoid f() {\n" 16386 "\t\tsomeFunction(parameter1,\n" 16387 "\t\t\t\t\t parameter2);\n" 16388 "\t}\n" 16389 "};", 16390 Tab); 16391 Tab.TabWidth = 8; 16392 Tab.IndentWidth = 4; 16393 verifyFormat("class TabWidth8Indent4 {\n" 16394 " void f() {\n" 16395 "\tsomeFunction(parameter1,\n" 16396 "\t\t parameter2);\n" 16397 " }\n" 16398 "};", 16399 Tab); 16400 Tab.TabWidth = 8; 16401 Tab.IndentWidth = 8; 16402 verifyFormat("/*\n" 16403 "\t a\t\tcomment\n" 16404 "\t in multiple lines\n" 16405 " */", 16406 " /*\t \t \n" 16407 " \t \t a\t\tcomment\t \t\n" 16408 " \t \t in multiple lines\t\n" 16409 " \t */", 16410 Tab); 16411 verifyFormat("{\n" 16412 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16413 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16414 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16415 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16416 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16417 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16418 "};", 16419 Tab); 16420 verifyFormat("enum AA {\n" 16421 "\ta1, // Force multiple lines\n" 16422 "\ta2,\n" 16423 "\ta3\n" 16424 "};", 16425 Tab); 16426 verifyFormat("if (aaaaaaaa && // q\n" 16427 " bb) // w\n" 16428 "\t;", 16429 "if (aaaaaaaa &&// q\n" 16430 "bb)// w\n" 16431 ";", 16432 Tab); 16433 verifyFormat("class X {\n" 16434 "\tvoid f() {\n" 16435 "\t\tsomeFunction(parameter1,\n" 16436 "\t\t\t parameter2);\n" 16437 "\t}\n" 16438 "};", 16439 Tab); 16440 verifyFormat("{\n" 16441 "\tQ(\n" 16442 "\t {\n" 16443 "\t\t int a;\n" 16444 "\t\t someFunction(aaaaaaaa,\n" 16445 "\t\t\t\t bbbbbbb);\n" 16446 "\t },\n" 16447 "\t p);\n" 16448 "}", 16449 Tab); 16450 verifyFormat("{\n" 16451 "\t/* aaaa\n" 16452 "\t bbbb */\n" 16453 "}", 16454 "{\n" 16455 "/* aaaa\n" 16456 " bbbb */\n" 16457 "}", 16458 Tab); 16459 verifyFormat("{\n" 16460 "\t/*\n" 16461 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16462 "\t bbbbbbbbbbbbb\n" 16463 "\t*/\n" 16464 "}", 16465 "{\n" 16466 "/*\n" 16467 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16468 "*/\n" 16469 "}", 16470 Tab); 16471 verifyFormat("{\n" 16472 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16473 "\t// bbbbbbbbbbbbb\n" 16474 "}", 16475 "{\n" 16476 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16477 "}", 16478 Tab); 16479 verifyFormat("{\n" 16480 "\t/*\n" 16481 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16482 "\t bbbbbbbbbbbbb\n" 16483 "\t*/\n" 16484 "}", 16485 "{\n" 16486 "\t/*\n" 16487 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16488 "\t*/\n" 16489 "}", 16490 Tab); 16491 verifyNoChange("{\n" 16492 "\t/*\n" 16493 "\n" 16494 "\t*/\n" 16495 "}", 16496 Tab); 16497 verifyNoChange("{\n" 16498 "\t/*\n" 16499 " asdf\n" 16500 "\t*/\n" 16501 "}", 16502 Tab); 16503 verifyFormat("/* some\n" 16504 " comment */", 16505 " \t \t /* some\n" 16506 " \t \t comment */", 16507 Tab); 16508 verifyFormat("int a; /* some\n" 16509 " comment */", 16510 " \t \t int a; /* some\n" 16511 " \t \t comment */", 16512 Tab); 16513 verifyFormat("int a; /* some\n" 16514 "comment */", 16515 " \t \t int\ta; /* some\n" 16516 " \t \t comment */", 16517 Tab); 16518 verifyFormat("f(\"\t\t\"); /* some\n" 16519 " comment */", 16520 " \t \t f(\"\t\t\"); /* some\n" 16521 " \t \t comment */", 16522 Tab); 16523 verifyFormat("{\n" 16524 "\t/*\n" 16525 "\t * Comment\n" 16526 "\t */\n" 16527 "\tint i;\n" 16528 "}", 16529 "{\n" 16530 "\t/*\n" 16531 "\t * Comment\n" 16532 "\t */\n" 16533 "\t int i;\n" 16534 "}", 16535 Tab); 16536 Tab.TabWidth = 2; 16537 Tab.IndentWidth = 2; 16538 verifyFormat("{\n" 16539 "\t/* aaaa\n" 16540 "\t\t bbbb */\n" 16541 "}", 16542 "{\n" 16543 "/* aaaa\n" 16544 "\t bbbb */\n" 16545 "}", 16546 Tab); 16547 verifyFormat("{\n" 16548 "\t/*\n" 16549 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16550 "\t\tbbbbbbbbbbbbb\n" 16551 "\t*/\n" 16552 "}", 16553 "{\n" 16554 "/*\n" 16555 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16556 "*/\n" 16557 "}", 16558 Tab); 16559 Tab.AlignConsecutiveAssignments.Enabled = true; 16560 Tab.AlignConsecutiveDeclarations.Enabled = true; 16561 Tab.TabWidth = 4; 16562 Tab.IndentWidth = 4; 16563 verifyFormat("class Assign {\n" 16564 "\tvoid f() {\n" 16565 "\t\tint x = 123;\n" 16566 "\t\tint random = 4;\n" 16567 "\t\tstd::string alphabet =\n" 16568 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 16569 "\t}\n" 16570 "};", 16571 Tab); 16572 16573 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 16574 Tab.TabWidth = 8; 16575 Tab.IndentWidth = 8; 16576 verifyFormat("if (aaaaaaaa && // q\n" 16577 " bb) // w\n" 16578 "\t;", 16579 "if (aaaaaaaa &&// q\n" 16580 "bb)// w\n" 16581 ";", 16582 Tab); 16583 verifyFormat("if (aaa && bbb) // w\n" 16584 "\t;", 16585 "if(aaa&&bbb)// w\n" 16586 ";", 16587 Tab); 16588 verifyFormat("class X {\n" 16589 "\tvoid f() {\n" 16590 "\t\tsomeFunction(parameter1,\n" 16591 "\t\t parameter2);\n" 16592 "\t}\n" 16593 "};", 16594 Tab); 16595 verifyFormat("#define A \\\n" 16596 "\tvoid f() { \\\n" 16597 "\t\tsomeFunction( \\\n" 16598 "\t\t parameter1, \\\n" 16599 "\t\t parameter2); \\\n" 16600 "\t}", 16601 Tab); 16602 Tab.TabWidth = 4; 16603 Tab.IndentWidth = 8; 16604 verifyFormat("class TabWidth4Indent8 {\n" 16605 "\t\tvoid f() {\n" 16606 "\t\t\t\tsomeFunction(parameter1,\n" 16607 "\t\t\t\t parameter2);\n" 16608 "\t\t}\n" 16609 "};", 16610 Tab); 16611 Tab.TabWidth = 4; 16612 Tab.IndentWidth = 4; 16613 verifyFormat("class TabWidth4Indent4 {\n" 16614 "\tvoid f() {\n" 16615 "\t\tsomeFunction(parameter1,\n" 16616 "\t\t parameter2);\n" 16617 "\t}\n" 16618 "};", 16619 Tab); 16620 Tab.TabWidth = 8; 16621 Tab.IndentWidth = 4; 16622 verifyFormat("class TabWidth8Indent4 {\n" 16623 " void f() {\n" 16624 "\tsomeFunction(parameter1,\n" 16625 "\t parameter2);\n" 16626 " }\n" 16627 "};", 16628 Tab); 16629 Tab.TabWidth = 8; 16630 Tab.IndentWidth = 8; 16631 verifyFormat("/*\n" 16632 " a\t\tcomment\n" 16633 " in multiple lines\n" 16634 " */", 16635 " /*\t \t \n" 16636 " \t \t a\t\tcomment\t \t\n" 16637 " \t \t in multiple lines\t\n" 16638 " \t */", 16639 Tab); 16640 verifyFormat("{\n" 16641 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16642 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16643 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16644 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16645 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16646 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 16647 "};", 16648 Tab); 16649 verifyFormat("enum AA {\n" 16650 "\ta1, // Force multiple lines\n" 16651 "\ta2,\n" 16652 "\ta3\n" 16653 "};", 16654 Tab); 16655 verifyFormat("if (aaaaaaaa && // q\n" 16656 " bb) // w\n" 16657 "\t;", 16658 "if (aaaaaaaa &&// q\n" 16659 "bb)// w\n" 16660 ";", 16661 Tab); 16662 verifyFormat("class X {\n" 16663 "\tvoid f() {\n" 16664 "\t\tsomeFunction(parameter1,\n" 16665 "\t\t parameter2);\n" 16666 "\t}\n" 16667 "};", 16668 Tab); 16669 verifyFormat("{\n" 16670 "\tQ(\n" 16671 "\t {\n" 16672 "\t\t int a;\n" 16673 "\t\t someFunction(aaaaaaaa,\n" 16674 "\t\t bbbbbbb);\n" 16675 "\t },\n" 16676 "\t p);\n" 16677 "}", 16678 Tab); 16679 verifyFormat("{\n" 16680 "\t/* aaaa\n" 16681 "\t bbbb */\n" 16682 "}", 16683 "{\n" 16684 "/* aaaa\n" 16685 " bbbb */\n" 16686 "}", 16687 Tab); 16688 verifyFormat("{\n" 16689 "\t/*\n" 16690 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16691 "\t bbbbbbbbbbbbb\n" 16692 "\t*/\n" 16693 "}", 16694 "{\n" 16695 "/*\n" 16696 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16697 "*/\n" 16698 "}", 16699 Tab); 16700 verifyFormat("{\n" 16701 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16702 "\t// bbbbbbbbbbbbb\n" 16703 "}", 16704 "{\n" 16705 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16706 "}", 16707 Tab); 16708 verifyFormat("{\n" 16709 "\t/*\n" 16710 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16711 "\t bbbbbbbbbbbbb\n" 16712 "\t*/\n" 16713 "}", 16714 "{\n" 16715 "\t/*\n" 16716 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16717 "\t*/\n" 16718 "}", 16719 Tab); 16720 verifyNoChange("{\n" 16721 "\t/*\n" 16722 "\n" 16723 "\t*/\n" 16724 "}", 16725 Tab); 16726 verifyNoChange("{\n" 16727 "\t/*\n" 16728 " asdf\n" 16729 "\t*/\n" 16730 "}", 16731 Tab); 16732 verifyFormat("/* some\n" 16733 " comment */", 16734 " \t \t /* some\n" 16735 " \t \t comment */", 16736 Tab); 16737 verifyFormat("int a; /* some\n" 16738 " comment */", 16739 " \t \t int a; /* some\n" 16740 " \t \t comment */", 16741 Tab); 16742 verifyFormat("int a; /* some\n" 16743 "comment */", 16744 " \t \t int\ta; /* some\n" 16745 " \t \t comment */", 16746 Tab); 16747 verifyFormat("f(\"\t\t\"); /* some\n" 16748 " comment */", 16749 " \t \t f(\"\t\t\"); /* some\n" 16750 " \t \t comment */", 16751 Tab); 16752 verifyFormat("{\n" 16753 "\t/*\n" 16754 "\t * Comment\n" 16755 "\t */\n" 16756 "\tint i;\n" 16757 "}", 16758 "{\n" 16759 "\t/*\n" 16760 "\t * Comment\n" 16761 "\t */\n" 16762 "\t int i;\n" 16763 "}", 16764 Tab); 16765 Tab.TabWidth = 2; 16766 Tab.IndentWidth = 2; 16767 verifyFormat("{\n" 16768 "\t/* aaaa\n" 16769 "\t bbbb */\n" 16770 "}", 16771 "{\n" 16772 "/* aaaa\n" 16773 " bbbb */\n" 16774 "}", 16775 Tab); 16776 verifyFormat("{\n" 16777 "\t/*\n" 16778 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 16779 "\t bbbbbbbbbbbbb\n" 16780 "\t*/\n" 16781 "}", 16782 "{\n" 16783 "/*\n" 16784 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 16785 "*/\n" 16786 "}", 16787 Tab); 16788 Tab.AlignConsecutiveAssignments.Enabled = true; 16789 Tab.AlignConsecutiveDeclarations.Enabled = true; 16790 Tab.TabWidth = 4; 16791 Tab.IndentWidth = 4; 16792 verifyFormat("class Assign {\n" 16793 "\tvoid f() {\n" 16794 "\t\tint x = 123;\n" 16795 "\t\tint random = 4;\n" 16796 "\t\tstd::string alphabet =\n" 16797 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 16798 "\t}\n" 16799 "};", 16800 Tab); 16801 Tab.AlignOperands = FormatStyle::OAS_Align; 16802 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 16803 " cccccccccccccccccccc;", 16804 Tab); 16805 // no alignment 16806 verifyFormat("int aaaaaaaaaa =\n" 16807 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 16808 Tab); 16809 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 16810 " : bbbbbbbbbbbbbb ? 222222222222222\n" 16811 " : 333333333333333;", 16812 Tab); 16813 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16814 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 16815 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 16816 " + cccccccccccccccccccc;", 16817 Tab); 16818 } 16819 16820 TEST_F(FormatTest, ZeroTabWidth) { 16821 FormatStyle Tab = getLLVMStyleWithColumns(42); 16822 Tab.IndentWidth = 8; 16823 Tab.UseTab = FormatStyle::UT_Never; 16824 Tab.TabWidth = 0; 16825 verifyFormat("void a() {\n" 16826 " // line starts with '\t'\n" 16827 "};", 16828 "void a(){\n" 16829 "\t// line starts with '\t'\n" 16830 "};", 16831 Tab); 16832 16833 verifyFormat("void a() {\n" 16834 " // line starts with '\t'\n" 16835 "};", 16836 "void a(){\n" 16837 "\t\t// line starts with '\t'\n" 16838 "};", 16839 Tab); 16840 16841 Tab.UseTab = FormatStyle::UT_ForIndentation; 16842 verifyFormat("void a() {\n" 16843 " // line starts with '\t'\n" 16844 "};", 16845 "void a(){\n" 16846 "\t// line starts with '\t'\n" 16847 "};", 16848 Tab); 16849 16850 verifyFormat("void a() {\n" 16851 " // line starts with '\t'\n" 16852 "};", 16853 "void a(){\n" 16854 "\t\t// line starts with '\t'\n" 16855 "};", 16856 Tab); 16857 16858 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 16859 verifyFormat("void a() {\n" 16860 " // line starts with '\t'\n" 16861 "};", 16862 "void a(){\n" 16863 "\t// line starts with '\t'\n" 16864 "};", 16865 Tab); 16866 16867 verifyFormat("void a() {\n" 16868 " // line starts with '\t'\n" 16869 "};", 16870 "void a(){\n" 16871 "\t\t// line starts with '\t'\n" 16872 "};", 16873 Tab); 16874 16875 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 16876 verifyFormat("void a() {\n" 16877 " // line starts with '\t'\n" 16878 "};", 16879 "void a(){\n" 16880 "\t// line starts with '\t'\n" 16881 "};", 16882 Tab); 16883 16884 verifyFormat("void a() {\n" 16885 " // line starts with '\t'\n" 16886 "};", 16887 "void a(){\n" 16888 "\t\t// line starts with '\t'\n" 16889 "};", 16890 Tab); 16891 16892 Tab.UseTab = FormatStyle::UT_Always; 16893 verifyFormat("void a() {\n" 16894 "// line starts with '\t'\n" 16895 "};", 16896 "void a(){\n" 16897 "\t// line starts with '\t'\n" 16898 "};", 16899 Tab); 16900 16901 verifyFormat("void a() {\n" 16902 "// line starts with '\t'\n" 16903 "};", 16904 "void a(){\n" 16905 "\t\t// line starts with '\t'\n" 16906 "};", 16907 Tab); 16908 } 16909 16910 TEST_F(FormatTest, CalculatesOriginalColumn) { 16911 verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 16912 "q\"; /* some\n" 16913 " comment */", 16914 " \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 16915 "q\"; /* some\n" 16916 " comment */"); 16917 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 16918 "/* some\n" 16919 " comment */", 16920 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 16921 " /* some\n" 16922 " comment */"); 16923 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 16924 "qqq\n" 16925 "/* some\n" 16926 " comment */", 16927 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 16928 "qqq\n" 16929 " /* some\n" 16930 " comment */"); 16931 verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 16932 "wwww; /* some\n" 16933 " comment */", 16934 " inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 16935 "wwww; /* some\n" 16936 " comment */"); 16937 } 16938 16939 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 16940 FormatStyle NoSpace = getLLVMStyle(); 16941 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 16942 16943 verifyFormat("while(true)\n" 16944 " continue;", 16945 NoSpace); 16946 verifyFormat("for(;;)\n" 16947 " continue;", 16948 NoSpace); 16949 verifyFormat("if(true)\n" 16950 " f();\n" 16951 "else if(true)\n" 16952 " f();", 16953 NoSpace); 16954 verifyFormat("do {\n" 16955 " do_something();\n" 16956 "} while(something());", 16957 NoSpace); 16958 verifyFormat("switch(x) {\n" 16959 "default:\n" 16960 " break;\n" 16961 "}", 16962 NoSpace); 16963 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 16964 verifyFormat("size_t x = sizeof(x);", NoSpace); 16965 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 16966 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 16967 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 16968 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 16969 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 16970 verifyFormat("alignas(128) char a[128];", NoSpace); 16971 verifyFormat("size_t x = alignof(MyType);", NoSpace); 16972 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 16973 verifyFormat("int f() throw(Deprecated);", NoSpace); 16974 verifyFormat("typedef void (*cb)(int);", NoSpace); 16975 verifyFormat("T A::operator()();", NoSpace); 16976 verifyFormat("X A::operator++(T);", NoSpace); 16977 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 16978 verifyFormat("#if (foo || bar) && baz\n" 16979 "#elif ((a || b) && c) || d\n" 16980 "#endif", 16981 NoSpace); 16982 16983 FormatStyle Space = getLLVMStyle(); 16984 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 16985 16986 verifyFormat("int f ();", Space); 16987 verifyFormat("bool operator< ();", Space); 16988 verifyFormat("bool operator> ();", Space); 16989 verifyFormat("void f (int a, T b) {\n" 16990 " while (true)\n" 16991 " continue;\n" 16992 "}", 16993 Space); 16994 verifyFormat("if (true)\n" 16995 " f ();\n" 16996 "else if (true)\n" 16997 " f ();", 16998 Space); 16999 verifyFormat("do {\n" 17000 " do_something ();\n" 17001 "} while (something ());", 17002 Space); 17003 verifyFormat("switch (x) {\n" 17004 "default:\n" 17005 " break;\n" 17006 "}", 17007 Space); 17008 verifyFormat("A::A () : a (1) {}", Space); 17009 verifyFormat("void f () __attribute__ ((asdf));", Space); 17010 verifyFormat("*(&a + 1);\n" 17011 "&((&a)[1]);\n" 17012 "a[(b + c) * d];\n" 17013 "(((a + 1) * 2) + 3) * 4;", 17014 Space); 17015 verifyFormat("#define A(x) x", Space); 17016 verifyFormat("#define A (x) x", Space); 17017 verifyFormat("#if defined(x)\n" 17018 "#endif", 17019 Space); 17020 verifyFormat("auto i = std::make_unique<int> (5);", Space); 17021 verifyFormat("size_t x = sizeof (x);", Space); 17022 verifyFormat("auto f (int x) -> decltype (x);", Space); 17023 verifyFormat("auto f (int x) -> typeof (x);", Space); 17024 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 17025 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 17026 verifyFormat("int f (T x) noexcept (x.create ());", Space); 17027 verifyFormat("alignas (128) char a[128];", Space); 17028 verifyFormat("size_t x = alignof (MyType);", Space); 17029 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 17030 verifyFormat("int f () throw (Deprecated);", Space); 17031 verifyFormat("typedef void (*cb) (int);", Space); 17032 verifyFormat("T A::operator() ();", Space); 17033 verifyFormat("X A::operator++ (T);", Space); 17034 verifyFormat("auto lambda = [] () { return 0; };", Space); 17035 verifyFormat("int x = int (y);", Space); 17036 verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space); 17037 verifyFormat("__builtin_LINE ()", Space); 17038 verifyFormat("__builtin_UNKNOWN ()", Space); 17039 17040 FormatStyle SomeSpace = getLLVMStyle(); 17041 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 17042 17043 verifyFormat("[]() -> float {}", SomeSpace); 17044 verifyFormat("[] (auto foo) {}", SomeSpace); 17045 verifyFormat("[foo]() -> int {}", SomeSpace); 17046 verifyFormat("int f();", SomeSpace); 17047 verifyFormat("void f (int a, T b) {\n" 17048 " while (true)\n" 17049 " continue;\n" 17050 "}", 17051 SomeSpace); 17052 verifyFormat("if (true)\n" 17053 " f();\n" 17054 "else if (true)\n" 17055 " f();", 17056 SomeSpace); 17057 verifyFormat("do {\n" 17058 " do_something();\n" 17059 "} while (something());", 17060 SomeSpace); 17061 verifyFormat("switch (x) {\n" 17062 "default:\n" 17063 " break;\n" 17064 "}", 17065 SomeSpace); 17066 verifyFormat("A::A() : a (1) {}", SomeSpace); 17067 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 17068 verifyFormat("*(&a + 1);\n" 17069 "&((&a)[1]);\n" 17070 "a[(b + c) * d];\n" 17071 "(((a + 1) * 2) + 3) * 4;", 17072 SomeSpace); 17073 verifyFormat("#define A(x) x", SomeSpace); 17074 verifyFormat("#define A (x) x", SomeSpace); 17075 verifyFormat("#if defined(x)\n" 17076 "#endif", 17077 SomeSpace); 17078 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 17079 verifyFormat("size_t x = sizeof (x);", SomeSpace); 17080 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 17081 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 17082 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 17083 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 17084 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 17085 verifyFormat("alignas (128) char a[128];", SomeSpace); 17086 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 17087 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 17088 SomeSpace); 17089 verifyFormat("int f() throw (Deprecated);", SomeSpace); 17090 verifyFormat("typedef void (*cb) (int);", SomeSpace); 17091 verifyFormat("T A::operator()();", SomeSpace); 17092 verifyFormat("X A::operator++ (T);", SomeSpace); 17093 verifyFormat("int x = int (y);", SomeSpace); 17094 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 17095 17096 FormatStyle SpaceControlStatements = getLLVMStyle(); 17097 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom; 17098 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true; 17099 17100 verifyFormat("while (true)\n" 17101 " continue;", 17102 SpaceControlStatements); 17103 verifyFormat("if (true)\n" 17104 " f();\n" 17105 "else if (true)\n" 17106 " f();", 17107 SpaceControlStatements); 17108 verifyFormat("for (;;) {\n" 17109 " do_something();\n" 17110 "}", 17111 SpaceControlStatements); 17112 verifyFormat("do {\n" 17113 " do_something();\n" 17114 "} while (something());", 17115 SpaceControlStatements); 17116 verifyFormat("switch (x) {\n" 17117 "default:\n" 17118 " break;\n" 17119 "}", 17120 SpaceControlStatements); 17121 17122 FormatStyle SpaceFuncDecl = getLLVMStyle(); 17123 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom; 17124 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true; 17125 17126 verifyFormat("int f ();", SpaceFuncDecl); 17127 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl); 17128 verifyFormat("void __attribute__((asdf)) f(int a, T b) {}", SpaceFuncDecl); 17129 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl); 17130 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl); 17131 verifyFormat("void __attribute__((asdf)) f ();", SpaceFuncDecl); 17132 verifyFormat("#define A(x) x", SpaceFuncDecl); 17133 verifyFormat("#define A (x) x", SpaceFuncDecl); 17134 verifyFormat("#if defined(x)\n" 17135 "#endif", 17136 SpaceFuncDecl); 17137 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl); 17138 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl); 17139 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl); 17140 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl); 17141 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl); 17142 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl); 17143 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl); 17144 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl); 17145 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl); 17146 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 17147 SpaceFuncDecl); 17148 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl); 17149 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl); 17150 verifyFormat("T A::operator()();", SpaceFuncDecl); 17151 verifyFormat("X A::operator++(T);", SpaceFuncDecl); 17152 verifyFormat("T A::operator()() {}", SpaceFuncDecl); 17153 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl); 17154 verifyFormat("int x = int(y);", SpaceFuncDecl); 17155 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 17156 SpaceFuncDecl); 17157 17158 FormatStyle SpaceFuncDef = getLLVMStyle(); 17159 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom; 17160 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true; 17161 17162 verifyFormat("int f();", SpaceFuncDef); 17163 verifyFormat("void f (int a, T b) {}", SpaceFuncDef); 17164 verifyFormat("void __attribute__((asdf)) f (int a, T b) {}", SpaceFuncDef); 17165 verifyFormat("A::A () : a(1) {}", SpaceFuncDef); 17166 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef); 17167 verifyFormat("void __attribute__((asdf)) f();", SpaceFuncDef); 17168 verifyFormat("#define A(x) x", SpaceFuncDef); 17169 verifyFormat("#define A (x) x", SpaceFuncDef); 17170 verifyFormat("#if defined(x)\n" 17171 "#endif", 17172 SpaceFuncDef); 17173 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef); 17174 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef); 17175 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef); 17176 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef); 17177 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef); 17178 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef); 17179 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef); 17180 verifyFormat("alignas(128) char a[128];", SpaceFuncDef); 17181 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef); 17182 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 17183 SpaceFuncDef); 17184 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef); 17185 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef); 17186 verifyFormat("T A::operator()();", SpaceFuncDef); 17187 verifyFormat("X A::operator++(T);", SpaceFuncDef); 17188 verifyFormat("T A::operator()() {}", SpaceFuncDef); 17189 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef); 17190 verifyFormat("int x = int(y);", SpaceFuncDef); 17191 verifyFormat("void foo::bar () {}", SpaceFuncDef); 17192 verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}", 17193 SpaceFuncDef); 17194 17195 FormatStyle SpaceIfMacros = getLLVMStyle(); 17196 SpaceIfMacros.IfMacros.clear(); 17197 SpaceIfMacros.IfMacros.push_back("MYIF"); 17198 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 17199 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true; 17200 verifyFormat("MYIF (a)\n return;", SpaceIfMacros); 17201 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros); 17202 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros); 17203 17204 FormatStyle SpaceForeachMacros = getLLVMStyle(); 17205 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine, 17206 FormatStyle::SBS_Never); 17207 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false); 17208 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 17209 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true; 17210 verifyFormat("for (;;) {\n" 17211 "}", 17212 SpaceForeachMacros); 17213 verifyFormat("foreach (Item *item, itemlist) {\n" 17214 "}", 17215 SpaceForeachMacros); 17216 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n" 17217 "}", 17218 SpaceForeachMacros); 17219 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n" 17220 "}", 17221 SpaceForeachMacros); 17222 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros); 17223 17224 FormatStyle SomeSpace2 = getLLVMStyle(); 17225 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom; 17226 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true; 17227 verifyFormat("[]() -> float {}", SomeSpace2); 17228 verifyFormat("[] (auto foo) {}", SomeSpace2); 17229 verifyFormat("[foo]() -> int {}", SomeSpace2); 17230 verifyFormat("int f();", SomeSpace2); 17231 verifyFormat("void f (int a, T b) {\n" 17232 " while (true)\n" 17233 " continue;\n" 17234 "}", 17235 SomeSpace2); 17236 verifyFormat("if (true)\n" 17237 " f();\n" 17238 "else if (true)\n" 17239 " f();", 17240 SomeSpace2); 17241 verifyFormat("do {\n" 17242 " do_something();\n" 17243 "} while (something());", 17244 SomeSpace2); 17245 verifyFormat("switch (x) {\n" 17246 "default:\n" 17247 " break;\n" 17248 "}", 17249 SomeSpace2); 17250 verifyFormat("A::A() : a (1) {}", SomeSpace2); 17251 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2); 17252 verifyFormat("*(&a + 1);\n" 17253 "&((&a)[1]);\n" 17254 "a[(b + c) * d];\n" 17255 "(((a + 1) * 2) + 3) * 4;", 17256 SomeSpace2); 17257 verifyFormat("#define A(x) x", SomeSpace2); 17258 verifyFormat("#define A (x) x", SomeSpace2); 17259 verifyFormat("#if defined(x)\n" 17260 "#endif", 17261 SomeSpace2); 17262 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2); 17263 verifyFormat("size_t x = sizeof (x);", SomeSpace2); 17264 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2); 17265 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2); 17266 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2); 17267 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2); 17268 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2); 17269 verifyFormat("alignas (128) char a[128];", SomeSpace2); 17270 verifyFormat("size_t x = alignof (MyType);", SomeSpace2); 17271 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 17272 SomeSpace2); 17273 verifyFormat("int f() throw (Deprecated);", SomeSpace2); 17274 verifyFormat("typedef void (*cb) (int);", SomeSpace2); 17275 verifyFormat("T A::operator()();", SomeSpace2); 17276 verifyFormat("X A::operator++ (T);", SomeSpace2); 17277 verifyFormat("int x = int (y);", SomeSpace2); 17278 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2); 17279 17280 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle(); 17281 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom; 17282 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 17283 .AfterOverloadedOperator = true; 17284 17285 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator); 17286 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator); 17287 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator); 17288 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 17289 17290 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 17291 .AfterOverloadedOperator = false; 17292 17293 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator); 17294 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator); 17295 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator); 17296 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 17297 17298 auto SpaceAfterRequires = getLLVMStyle(); 17299 SpaceAfterRequires.SpaceBeforeParens = FormatStyle::SBPO_Custom; 17300 EXPECT_FALSE( 17301 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause); 17302 EXPECT_FALSE( 17303 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression); 17304 verifyFormat("void f(auto x)\n" 17305 " requires requires(int i) { x + i; }\n" 17306 "{}", 17307 SpaceAfterRequires); 17308 verifyFormat("void f(auto x)\n" 17309 " requires(requires(int i) { x + i; })\n" 17310 "{}", 17311 SpaceAfterRequires); 17312 verifyFormat("if (requires(int i) { x + i; })\n" 17313 " return;", 17314 SpaceAfterRequires); 17315 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires); 17316 verifyFormat("template <typename T>\n" 17317 " requires(Foo<T>)\n" 17318 "class Bar;", 17319 SpaceAfterRequires); 17320 17321 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true; 17322 verifyFormat("void f(auto x)\n" 17323 " requires requires(int i) { x + i; }\n" 17324 "{}", 17325 SpaceAfterRequires); 17326 verifyFormat("void f(auto x)\n" 17327 " requires (requires(int i) { x + i; })\n" 17328 "{}", 17329 SpaceAfterRequires); 17330 verifyFormat("if (requires(int i) { x + i; })\n" 17331 " return;", 17332 SpaceAfterRequires); 17333 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires); 17334 verifyFormat("template <typename T>\n" 17335 " requires (Foo<T>)\n" 17336 "class Bar;", 17337 SpaceAfterRequires); 17338 17339 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = false; 17340 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInExpression = true; 17341 verifyFormat("void f(auto x)\n" 17342 " requires requires (int i) { x + i; }\n" 17343 "{}", 17344 SpaceAfterRequires); 17345 verifyFormat("void f(auto x)\n" 17346 " requires(requires (int i) { x + i; })\n" 17347 "{}", 17348 SpaceAfterRequires); 17349 verifyFormat("if (requires (int i) { x + i; })\n" 17350 " return;", 17351 SpaceAfterRequires); 17352 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires); 17353 verifyFormat("template <typename T>\n" 17354 " requires(Foo<T>)\n" 17355 "class Bar;", 17356 SpaceAfterRequires); 17357 17358 SpaceAfterRequires.SpaceBeforeParensOptions.AfterRequiresInClause = true; 17359 verifyFormat("void f(auto x)\n" 17360 " requires requires (int i) { x + i; }\n" 17361 "{}", 17362 SpaceAfterRequires); 17363 verifyFormat("void f(auto x)\n" 17364 " requires (requires (int i) { x + i; })\n" 17365 "{}", 17366 SpaceAfterRequires); 17367 verifyFormat("if (requires (int i) { x + i; })\n" 17368 " return;", 17369 SpaceAfterRequires); 17370 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires); 17371 verifyFormat("template <typename T>\n" 17372 " requires (Foo<T>)\n" 17373 "class Bar;", 17374 SpaceAfterRequires); 17375 } 17376 17377 TEST_F(FormatTest, SpaceAfterLogicalNot) { 17378 FormatStyle Spaces = getLLVMStyle(); 17379 Spaces.SpaceAfterLogicalNot = true; 17380 17381 verifyFormat("bool x = ! y", Spaces); 17382 verifyFormat("if (! isFailure())", Spaces); 17383 verifyFormat("if (! (a && b))", Spaces); 17384 verifyFormat("\"Error!\"", Spaces); 17385 verifyFormat("! ! x", Spaces); 17386 } 17387 17388 TEST_F(FormatTest, ConfigurableSpacesInParens) { 17389 FormatStyle Spaces = getLLVMStyle(); 17390 17391 verifyFormat("do_something(::globalVar);", Spaces); 17392 verifyFormat("call(x, y, z);", Spaces); 17393 verifyFormat("call();", Spaces); 17394 verifyFormat("std::function<void(int, int)> callback;", Spaces); 17395 verifyFormat("void inFunction() { std::function<void(int, int)> fct; }", 17396 Spaces); 17397 verifyFormat("while ((bool)1)\n" 17398 " continue;", 17399 Spaces); 17400 verifyFormat("for (;;)\n" 17401 " continue;", 17402 Spaces); 17403 verifyFormat("if (true)\n" 17404 " f();\n" 17405 "else if (true)\n" 17406 " f();", 17407 Spaces); 17408 verifyFormat("do {\n" 17409 " do_something((int)i);\n" 17410 "} while (something());", 17411 Spaces); 17412 verifyFormat("switch (x) {\n" 17413 "default:\n" 17414 " break;\n" 17415 "}", 17416 Spaces); 17417 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 17418 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); 17419 verifyFormat("void f() __attribute__((asdf));", Spaces); 17420 verifyFormat("x = (int32)y;", Spaces); 17421 verifyFormat("y = ((int (*)(int))foo)(x);", Spaces); 17422 verifyFormat("decltype(x) y = 42;", Spaces); 17423 verifyFormat("decltype((x)) y = z;", Spaces); 17424 verifyFormat("decltype((foo())) a = foo();", Spaces); 17425 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces); 17426 verifyFormat("if ((x - y) && (a ^ b))\n" 17427 " f();", 17428 Spaces); 17429 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n" 17430 " foo(i);", 17431 Spaces); 17432 verifyFormat("switch (x / (y + z)) {\n" 17433 "default:\n" 17434 " break;\n" 17435 "}", 17436 Spaces); 17437 17438 Spaces.SpacesInParens = FormatStyle::SIPO_Custom; 17439 Spaces.SpacesInParensOptions = {}; 17440 Spaces.SpacesInParensOptions.Other = true; 17441 17442 EXPECT_FALSE(Spaces.SpacesInParensOptions.InConditionalStatements); 17443 verifyFormat("if (a)\n" 17444 " return;", 17445 Spaces); 17446 17447 Spaces.SpacesInParensOptions.InConditionalStatements = true; 17448 verifyFormat("do_something( ::globalVar );", Spaces); 17449 verifyFormat("call( x, y, z );", Spaces); 17450 verifyFormat("call();", Spaces); 17451 verifyFormat("std::function<void( int, int )> callback;", Spaces); 17452 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 17453 Spaces); 17454 verifyFormat("while ( (bool)1 )\n" 17455 " continue;", 17456 Spaces); 17457 verifyFormat("for ( ;; )\n" 17458 " continue;", 17459 Spaces); 17460 verifyFormat("if ( true )\n" 17461 " f();\n" 17462 "else if ( true )\n" 17463 " f();", 17464 Spaces); 17465 verifyFormat("do {\n" 17466 " do_something( (int)i );\n" 17467 "} while ( something() );", 17468 Spaces); 17469 verifyFormat("switch ( x ) {\n" 17470 "default:\n" 17471 " break;\n" 17472 "}", 17473 Spaces); 17474 verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces); 17475 verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces); 17476 verifyFormat("void f() __attribute__( ( asdf ) );", Spaces); 17477 verifyFormat("x = (int32)y;", Spaces); 17478 verifyFormat("y = ( (int ( * )( int ))foo )( x );", Spaces); 17479 verifyFormat("decltype( x ) y = 42;", Spaces); 17480 verifyFormat("decltype( ( x ) ) y = z;", Spaces); 17481 verifyFormat("decltype( ( foo() ) ) a = foo();", Spaces); 17482 verifyFormat("decltype( ( bar( 10 ) ) ) a = bar( 11 );", Spaces); 17483 verifyFormat("if ( ( x - y ) && ( a ^ b ) )\n" 17484 " f();", 17485 Spaces); 17486 verifyFormat("for ( int i = 0; i < 10; i = ( i + 1 ) )\n" 17487 " foo( i );", 17488 Spaces); 17489 verifyFormat("switch ( x / ( y + z ) ) {\n" 17490 "default:\n" 17491 " break;\n" 17492 "}", 17493 Spaces); 17494 17495 Spaces.SpacesInParens = FormatStyle::SIPO_Custom; 17496 Spaces.SpacesInParensOptions = {}; 17497 Spaces.SpacesInParensOptions.InCStyleCasts = true; 17498 verifyFormat("Type *A = ( Type * )P;", Spaces); 17499 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 17500 verifyFormat("x = ( int32 )y;", Spaces); 17501 verifyFormat("throw ( int32 )x;", Spaces); 17502 verifyFormat("int a = ( int )(2.0f);", Spaces); 17503 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 17504 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 17505 verifyFormat("#define x (( int )-1)", Spaces); 17506 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces); 17507 17508 // Run the first set of tests again with: 17509 Spaces.SpacesInParens = FormatStyle::SIPO_Custom; 17510 Spaces.SpacesInParensOptions = {}; 17511 Spaces.SpacesInParensOptions.InEmptyParentheses = true; 17512 Spaces.SpacesInParensOptions.InCStyleCasts = true; 17513 verifyFormat("call(x, y, z);", Spaces); 17514 verifyFormat("call( );", Spaces); 17515 verifyFormat("std::function<void(int, int)> callback;", Spaces); 17516 verifyFormat("while (( bool )1)\n" 17517 " continue;", 17518 Spaces); 17519 verifyFormat("for (;;)\n" 17520 " continue;", 17521 Spaces); 17522 verifyFormat("if (true)\n" 17523 " f( );\n" 17524 "else if (true)\n" 17525 " f( );", 17526 Spaces); 17527 verifyFormat("do {\n" 17528 " do_something(( int )i);\n" 17529 "} while (something( ));", 17530 Spaces); 17531 verifyFormat("switch (x) {\n" 17532 "default:\n" 17533 " break;\n" 17534 "}", 17535 Spaces); 17536 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 17537 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); 17538 verifyFormat("void f( ) __attribute__((asdf));", Spaces); 17539 verifyFormat("x = ( int32 )y;", Spaces); 17540 verifyFormat("y = (( int (*)(int) )foo)(x);", Spaces); 17541 verifyFormat("decltype(x) y = 42;", Spaces); 17542 verifyFormat("decltype((x)) y = z;", Spaces); 17543 verifyFormat("decltype((foo( ))) a = foo( );", Spaces); 17544 verifyFormat("decltype((bar(10))) a = bar(11);", Spaces); 17545 verifyFormat("if ((x - y) && (a ^ b))\n" 17546 " f( );", 17547 Spaces); 17548 verifyFormat("for (int i = 0; i < 10; i = (i + 1))\n" 17549 " foo(i);", 17550 Spaces); 17551 verifyFormat("switch (x / (y + z)) {\n" 17552 "default:\n" 17553 " break;\n" 17554 "}", 17555 Spaces); 17556 17557 // Run the first set of tests again with: 17558 Spaces.SpaceAfterCStyleCast = true; 17559 verifyFormat("call(x, y, z);", Spaces); 17560 verifyFormat("call( );", Spaces); 17561 verifyFormat("std::function<void(int, int)> callback;", Spaces); 17562 verifyFormat("while (( bool ) 1)\n" 17563 " continue;", 17564 Spaces); 17565 verifyFormat("for (;;)\n" 17566 " continue;", 17567 Spaces); 17568 verifyFormat("if (true)\n" 17569 " f( );\n" 17570 "else if (true)\n" 17571 " f( );", 17572 Spaces); 17573 verifyFormat("do {\n" 17574 " do_something(( int ) i);\n" 17575 "} while (something( ));", 17576 Spaces); 17577 verifyFormat("switch (x) {\n" 17578 "default:\n" 17579 " break;\n" 17580 "}", 17581 Spaces); 17582 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces); 17583 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces); 17584 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces); 17585 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces); 17586 verifyFormat("bool *y = ( bool * ) (x);", Spaces); 17587 verifyFormat("throw ( int32 ) x;", Spaces); 17588 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 17589 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); 17590 verifyFormat("void f( ) __attribute__((asdf));", Spaces); 17591 17592 // Run subset of tests again with: 17593 Spaces.SpacesInParensOptions.InCStyleCasts = false; 17594 Spaces.SpaceAfterCStyleCast = true; 17595 verifyFormat("while ((bool) 1)\n" 17596 " continue;", 17597 Spaces); 17598 verifyFormat("do {\n" 17599 " do_something((int) i);\n" 17600 "} while (something( ));", 17601 Spaces); 17602 17603 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 17604 verifyFormat("size_t idx = (size_t) a;", Spaces); 17605 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 17606 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 17607 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 17608 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 17609 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 17610 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces); 17611 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces); 17612 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces); 17613 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces); 17614 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces); 17615 verifyFormat("throw (int32) x;", Spaces); 17616 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 17617 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces); 17618 verifyFormat("void f( ) __attribute__((asdf));", Spaces); 17619 17620 Spaces.ColumnLimit = 80; 17621 Spaces.IndentWidth = 4; 17622 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 17623 verifyFormat("void foo( ) {\n" 17624 " size_t foo = (*(function))(\n" 17625 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 17626 "BarrrrrrrrrrrrLong,\n" 17627 " FoooooooooLooooong);\n" 17628 "}", 17629 Spaces); 17630 Spaces.SpaceAfterCStyleCast = false; 17631 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 17632 verifyFormat("size_t idx = (size_t)a;", Spaces); 17633 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 17634 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 17635 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 17636 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 17637 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 17638 17639 verifyFormat("void foo( ) {\n" 17640 " size_t foo = (*(function))(\n" 17641 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 17642 "BarrrrrrrrrrrrLong,\n" 17643 " FoooooooooLooooong);\n" 17644 "}", 17645 Spaces); 17646 17647 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 17648 verifyFormat("void foo( ) {\n" 17649 " size_t foo = (*(function))(\n" 17650 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 17651 "BarrrrrrrrrrrrLong,\n" 17652 " FoooooooooLooooong\n" 17653 " );\n" 17654 "}", 17655 Spaces); 17656 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 17657 verifyFormat("size_t idx = (size_t)a;", Spaces); 17658 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 17659 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 17660 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 17661 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 17662 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 17663 17664 // Check ExceptDoubleParentheses spaces 17665 Spaces.IndentWidth = 2; 17666 Spaces.SpacesInParens = FormatStyle::SIPO_Custom; 17667 Spaces.SpacesInParensOptions = {}; 17668 Spaces.SpacesInParensOptions.Other = true; 17669 Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true; 17670 verifyFormat("SomeType *__attribute__(( attr )) *a = NULL;", Spaces); 17671 verifyFormat("void __attribute__(( naked )) foo( int bar )", Spaces); 17672 verifyFormat("void f() __attribute__(( asdf ));", Spaces); 17673 verifyFormat("__attribute__(( __aligned__( x ) )) z;", Spaces); 17674 verifyFormat("int x __attribute__(( aligned( 16 ) )) = 0;", Spaces); 17675 verifyFormat("class __declspec( dllimport ) X {};", Spaces); 17676 verifyFormat("class __declspec(( dllimport )) X {};", Spaces); 17677 verifyFormat("int x = ( ( a - 1 ) * 3 );", Spaces); 17678 verifyFormat("int x = ( 3 * ( a - 1 ) );", Spaces); 17679 verifyFormat("decltype( x ) y = 42;", Spaces); 17680 verifyFormat("decltype(( bar( 10 ) )) a = bar( 11 );", Spaces); 17681 verifyFormat("if (( i = j ))\n" 17682 " do_something( i );", 17683 Spaces); 17684 17685 Spaces.SpacesInParens = FormatStyle::SIPO_Custom; 17686 Spaces.SpacesInParensOptions = {}; 17687 Spaces.SpacesInParensOptions.InConditionalStatements = true; 17688 Spaces.SpacesInParensOptions.ExceptDoubleParentheses = true; 17689 verifyFormat("while ( (bool)1 )\n" 17690 " continue;", 17691 Spaces); 17692 verifyFormat("while ((i = j))\n" 17693 " continue;", 17694 Spaces); 17695 verifyFormat("do {\n" 17696 " do_something((int)i);\n" 17697 "} while ( something() );", 17698 Spaces); 17699 verifyFormat("do {\n" 17700 " do_something((int)i);\n" 17701 "} while ((i = i + 1));", 17702 Spaces); 17703 verifyFormat("if ( (x - y) && (a ^ b) )\n" 17704 " f();", 17705 Spaces); 17706 verifyFormat("if ((i = j))\n" 17707 " do_something(i);", 17708 Spaces); 17709 verifyFormat("for ( int i = 0; i < 10; i = (i + 1) )\n" 17710 " foo(i);", 17711 Spaces); 17712 verifyFormat("switch ( x / (y + z) ) {\n" 17713 "default:\n" 17714 " break;\n" 17715 "}", 17716 Spaces); 17717 verifyFormat("if constexpr ((a = b))\n" 17718 " c;", 17719 Spaces); 17720 } 17721 17722 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 17723 verifyFormat("int a[5];"); 17724 verifyFormat("a[3] += 42;"); 17725 17726 FormatStyle Spaces = getLLVMStyle(); 17727 Spaces.SpacesInSquareBrackets = true; 17728 // Not lambdas. 17729 verifyFormat("int a[ 5 ];", Spaces); 17730 verifyFormat("a[ 3 ] += 42;", Spaces); 17731 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 17732 verifyFormat("double &operator[](int i) { return 0; }\n" 17733 "int i;", 17734 Spaces); 17735 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 17736 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 17737 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 17738 // Lambdas. 17739 verifyFormat("int c = []() -> int { return 2; }();", Spaces); 17740 verifyFormat("return [ i, args... ] {};", Spaces); 17741 verifyFormat("int foo = [ &bar ]() {};", Spaces); 17742 verifyFormat("int foo = [ = ]() {};", Spaces); 17743 verifyFormat("int foo = [ & ]() {};", Spaces); 17744 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 17745 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 17746 } 17747 17748 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 17749 FormatStyle NoSpaceStyle = getLLVMStyle(); 17750 verifyFormat("int a[5];", NoSpaceStyle); 17751 verifyFormat("a[3] += 42;", NoSpaceStyle); 17752 17753 verifyFormat("int a[1];", NoSpaceStyle); 17754 verifyFormat("int 1 [a];", NoSpaceStyle); 17755 verifyFormat("int a[1][2];", NoSpaceStyle); 17756 verifyFormat("a[7] = 5;", NoSpaceStyle); 17757 verifyFormat("int a = (f())[23];", NoSpaceStyle); 17758 verifyFormat("f([] {})", NoSpaceStyle); 17759 17760 FormatStyle Space = getLLVMStyle(); 17761 Space.SpaceBeforeSquareBrackets = true; 17762 verifyFormat("int c = []() -> int { return 2; }();", Space); 17763 verifyFormat("return [i, args...] {};", Space); 17764 17765 verifyFormat("int a [5];", Space); 17766 verifyFormat("a [3] += 42;", Space); 17767 verifyFormat("constexpr char hello []{\"hello\"};", Space); 17768 verifyFormat("double &operator[](int i) { return 0; }\n" 17769 "int i;", 17770 Space); 17771 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 17772 verifyFormat("int i = a [a][a]->f();", Space); 17773 verifyFormat("int i = (*b) [a]->f();", Space); 17774 17775 verifyFormat("int a [1];", Space); 17776 verifyFormat("int 1 [a];", Space); 17777 verifyFormat("int a [1][2];", Space); 17778 verifyFormat("a [7] = 5;", Space); 17779 verifyFormat("int a = (f()) [23];", Space); 17780 verifyFormat("f([] {})", Space); 17781 } 17782 17783 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 17784 verifyFormat("int a = 5;"); 17785 verifyFormat("a += 42;"); 17786 verifyFormat("a or_eq 8;"); 17787 verifyFormat("xor = foo;"); 17788 17789 FormatStyle Spaces = getLLVMStyle(); 17790 Spaces.SpaceBeforeAssignmentOperators = false; 17791 verifyFormat("int a= 5;", Spaces); 17792 verifyFormat("a+= 42;", Spaces); 17793 verifyFormat("a or_eq 8;", Spaces); 17794 verifyFormat("xor= foo;", Spaces); 17795 } 17796 17797 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 17798 verifyFormat("class Foo : public Bar {};"); 17799 verifyFormat("Foo::Foo() : foo(1) {}"); 17800 verifyFormat("for (auto a : b) {\n}"); 17801 verifyFormat("int x = a ? b : c;"); 17802 verifyFormat("{\n" 17803 "label0:\n" 17804 " int x = 0;\n" 17805 "}"); 17806 verifyFormat("switch (x) {\n" 17807 "case 1:\n" 17808 "default:\n" 17809 "}"); 17810 verifyFormat("switch (allBraces) {\n" 17811 "case 1: {\n" 17812 " break;\n" 17813 "}\n" 17814 "case 2: {\n" 17815 " [[fallthrough]];\n" 17816 "}\n" 17817 "default: {\n" 17818 " break;\n" 17819 "}\n" 17820 "}"); 17821 17822 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 17823 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 17824 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 17825 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 17826 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 17827 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 17828 verifyFormat("{\n" 17829 "label1:\n" 17830 " int x = 0;\n" 17831 "}", 17832 CtorInitializerStyle); 17833 verifyFormat("switch (x) {\n" 17834 "case 1:\n" 17835 "default:\n" 17836 "}", 17837 CtorInitializerStyle); 17838 verifyFormat("switch (allBraces) {\n" 17839 "case 1: {\n" 17840 " break;\n" 17841 "}\n" 17842 "case 2: {\n" 17843 " [[fallthrough]];\n" 17844 "}\n" 17845 "default: {\n" 17846 " break;\n" 17847 "}\n" 17848 "}", 17849 CtorInitializerStyle); 17850 CtorInitializerStyle.BreakConstructorInitializers = 17851 FormatStyle::BCIS_AfterColon; 17852 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 17853 " aaaaaaaaaaaaaaaa(1),\n" 17854 " bbbbbbbbbbbbbbbb(2) {}", 17855 CtorInitializerStyle); 17856 CtorInitializerStyle.BreakConstructorInitializers = 17857 FormatStyle::BCIS_BeforeComma; 17858 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 17859 " : aaaaaaaaaaaaaaaa(1)\n" 17860 " , bbbbbbbbbbbbbbbb(2) {}", 17861 CtorInitializerStyle); 17862 CtorInitializerStyle.BreakConstructorInitializers = 17863 FormatStyle::BCIS_BeforeColon; 17864 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 17865 " : aaaaaaaaaaaaaaaa(1),\n" 17866 " bbbbbbbbbbbbbbbb(2) {}", 17867 CtorInitializerStyle); 17868 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 17869 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 17870 ": aaaaaaaaaaaaaaaa(1),\n" 17871 " bbbbbbbbbbbbbbbb(2) {}", 17872 CtorInitializerStyle); 17873 17874 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 17875 InheritanceStyle.SpaceBeforeInheritanceColon = false; 17876 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 17877 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 17878 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 17879 verifyFormat("int x = a ? b : c;", InheritanceStyle); 17880 verifyFormat("{\n" 17881 "label2:\n" 17882 " int x = 0;\n" 17883 "}", 17884 InheritanceStyle); 17885 verifyFormat("switch (x) {\n" 17886 "case 1:\n" 17887 "default:\n" 17888 "}", 17889 InheritanceStyle); 17890 verifyFormat("switch (allBraces) {\n" 17891 "case 1: {\n" 17892 " break;\n" 17893 "}\n" 17894 "case 2: {\n" 17895 " [[fallthrough]];\n" 17896 "}\n" 17897 "default: {\n" 17898 " break;\n" 17899 "}\n" 17900 "}", 17901 InheritanceStyle); 17902 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 17903 verifyFormat("class Foooooooooooooooooooooo\n" 17904 " : public aaaaaaaaaaaaaaaaaa,\n" 17905 " public bbbbbbbbbbbbbbbbbb {\n" 17906 "}", 17907 InheritanceStyle); 17908 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 17909 verifyFormat("class Foooooooooooooooooooooo:\n" 17910 " public aaaaaaaaaaaaaaaaaa,\n" 17911 " public bbbbbbbbbbbbbbbbbb {\n" 17912 "}", 17913 InheritanceStyle); 17914 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 17915 verifyFormat("class Foooooooooooooooooooooo\n" 17916 " : public aaaaaaaaaaaaaaaaaa\n" 17917 " , public bbbbbbbbbbbbbbbbbb {\n" 17918 "}", 17919 InheritanceStyle); 17920 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 17921 verifyFormat("class Foooooooooooooooooooooo\n" 17922 " : public aaaaaaaaaaaaaaaaaa,\n" 17923 " public bbbbbbbbbbbbbbbbbb {\n" 17924 "}", 17925 InheritanceStyle); 17926 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 17927 verifyFormat("class Foooooooooooooooooooooo\n" 17928 ": public aaaaaaaaaaaaaaaaaa,\n" 17929 " public bbbbbbbbbbbbbbbbbb {}", 17930 InheritanceStyle); 17931 17932 FormatStyle ForLoopStyle = getLLVMStyle(); 17933 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 17934 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 17935 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 17936 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 17937 verifyFormat("int x = a ? b : c;", ForLoopStyle); 17938 verifyFormat("{\n" 17939 "label2:\n" 17940 " int x = 0;\n" 17941 "}", 17942 ForLoopStyle); 17943 verifyFormat("switch (x) {\n" 17944 "case 1:\n" 17945 "default:\n" 17946 "}", 17947 ForLoopStyle); 17948 verifyFormat("switch (allBraces) {\n" 17949 "case 1: {\n" 17950 " break;\n" 17951 "}\n" 17952 "case 2: {\n" 17953 " [[fallthrough]];\n" 17954 "}\n" 17955 "default: {\n" 17956 " break;\n" 17957 "}\n" 17958 "}", 17959 ForLoopStyle); 17960 17961 FormatStyle CaseStyle = getLLVMStyle(); 17962 CaseStyle.SpaceBeforeCaseColon = true; 17963 verifyFormat("class Foo : public Bar {};", CaseStyle); 17964 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 17965 verifyFormat("for (auto a : b) {\n}", CaseStyle); 17966 verifyFormat("int x = a ? b : c;", CaseStyle); 17967 verifyFormat("switch (x) {\n" 17968 "case 1 :\n" 17969 "default :\n" 17970 "}", 17971 CaseStyle); 17972 verifyFormat("switch (allBraces) {\n" 17973 "case 1 : {\n" 17974 " break;\n" 17975 "}\n" 17976 "case 2 : {\n" 17977 " [[fallthrough]];\n" 17978 "}\n" 17979 "default : {\n" 17980 " break;\n" 17981 "}\n" 17982 "}", 17983 CaseStyle); 17984 // Goto labels should not be affected. 17985 verifyFormat("switch (x) {\n" 17986 "goto_label:\n" 17987 "default :\n" 17988 "}", 17989 CaseStyle); 17990 verifyFormat("switch (x) {\n" 17991 "goto_label: { break; }\n" 17992 "default : {\n" 17993 " break;\n" 17994 "}\n" 17995 "}", 17996 CaseStyle); 17997 17998 FormatStyle NoSpaceStyle = getLLVMStyle(); 17999 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 18000 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 18001 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 18002 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 18003 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 18004 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 18005 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 18006 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 18007 verifyFormat("{\n" 18008 "label3:\n" 18009 " int x = 0;\n" 18010 "}", 18011 NoSpaceStyle); 18012 verifyFormat("switch (x) {\n" 18013 "case 1:\n" 18014 "default:\n" 18015 "}", 18016 NoSpaceStyle); 18017 verifyFormat("switch (allBraces) {\n" 18018 "case 1: {\n" 18019 " break;\n" 18020 "}\n" 18021 "case 2: {\n" 18022 " [[fallthrough]];\n" 18023 "}\n" 18024 "default: {\n" 18025 " break;\n" 18026 "}\n" 18027 "}", 18028 NoSpaceStyle); 18029 18030 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 18031 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 18032 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 18033 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 18034 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 18035 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 18036 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 18037 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 18038 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 18039 verifyFormat("{\n" 18040 "label3:\n" 18041 " int x = 0;\n" 18042 "}", 18043 InvertedSpaceStyle); 18044 verifyFormat("switch (x) {\n" 18045 "case 1 :\n" 18046 "case 2 : {\n" 18047 " break;\n" 18048 "}\n" 18049 "default :\n" 18050 " break;\n" 18051 "}", 18052 InvertedSpaceStyle); 18053 verifyFormat("switch (allBraces) {\n" 18054 "case 1 : {\n" 18055 " break;\n" 18056 "}\n" 18057 "case 2 : {\n" 18058 " [[fallthrough]];\n" 18059 "}\n" 18060 "default : {\n" 18061 " break;\n" 18062 "}\n" 18063 "}", 18064 InvertedSpaceStyle); 18065 } 18066 18067 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 18068 FormatStyle Style = getLLVMStyle(); 18069 18070 Style.PointerAlignment = FormatStyle::PAS_Left; 18071 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 18072 verifyFormat("void* const* x = NULL;", Style); 18073 18074 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 18075 do { \ 18076 Style.PointerAlignment = FormatStyle::Pointers; \ 18077 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 18078 verifyFormat(Code, Style); \ 18079 } while (false) 18080 18081 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 18082 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 18083 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 18084 18085 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 18086 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 18087 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 18088 18089 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 18090 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 18091 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 18092 18093 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 18094 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 18095 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 18096 18097 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 18098 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 18099 SAPQ_Default); 18100 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 18101 SAPQ_Default); 18102 18103 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 18104 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 18105 SAPQ_Before); 18106 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 18107 SAPQ_Before); 18108 18109 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 18110 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 18111 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 18112 SAPQ_After); 18113 18114 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 18115 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 18116 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 18117 18118 #undef verifyQualifierSpaces 18119 18120 FormatStyle Spaces = getLLVMStyle(); 18121 Spaces.AttributeMacros.push_back("qualified"); 18122 Spaces.PointerAlignment = FormatStyle::PAS_Right; 18123 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 18124 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 18125 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 18126 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 18127 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 18128 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 18129 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 18130 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 18131 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 18132 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 18133 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 18134 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 18135 18136 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 18137 Spaces.PointerAlignment = FormatStyle::PAS_Left; 18138 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 18139 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 18140 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 18141 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 18142 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 18143 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 18144 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 18145 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 18146 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 18147 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 18148 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 18149 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 18150 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 18151 18152 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 18153 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 18154 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 18155 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 18156 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 18157 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 18158 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 18159 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 18160 } 18161 18162 TEST_F(FormatTest, AlignConsecutiveMacros) { 18163 FormatStyle Style = getLLVMStyle(); 18164 Style.AlignConsecutiveAssignments.Enabled = true; 18165 Style.AlignConsecutiveDeclarations.Enabled = true; 18166 18167 verifyFormat("#define a 3\n" 18168 "#define bbbb 4\n" 18169 "#define ccc (5)", 18170 Style); 18171 18172 verifyFormat("#define f(x) (x * x)\n" 18173 "#define fff(x, y, z) (x * y + z)\n" 18174 "#define ffff(x, y) (x - y)", 18175 Style); 18176 18177 verifyFormat("#define foo(x, y) (x + y)\n" 18178 "#define bar (5, 6)(2 + 2)", 18179 Style); 18180 18181 verifyFormat("#define a 3\n" 18182 "#define bbbb 4\n" 18183 "#define ccc (5)\n" 18184 "#define f(x) (x * x)\n" 18185 "#define fff(x, y, z) (x * y + z)\n" 18186 "#define ffff(x, y) (x - y)", 18187 Style); 18188 18189 Style.AlignConsecutiveMacros.Enabled = true; 18190 verifyFormat("#define a 3\n" 18191 "#define bbbb 4\n" 18192 "#define ccc (5)", 18193 Style); 18194 18195 verifyFormat("#define true 1\n" 18196 "#define false 0", 18197 Style); 18198 18199 verifyFormat("#define f(x) (x * x)\n" 18200 "#define fff(x, y, z) (x * y + z)\n" 18201 "#define ffff(x, y) (x - y)", 18202 Style); 18203 18204 verifyFormat("#define foo(x, y) (x + y)\n" 18205 "#define bar (5, 6)(2 + 2)", 18206 Style); 18207 18208 verifyFormat("#define a 3\n" 18209 "#define bbbb 4\n" 18210 "#define ccc (5)\n" 18211 "#define f(x) (x * x)\n" 18212 "#define fff(x, y, z) (x * y + z)\n" 18213 "#define ffff(x, y) (x - y)", 18214 Style); 18215 18216 verifyFormat("#define a 5\n" 18217 "#define foo(x, y) (x + y)\n" 18218 "#define CCC (6)\n" 18219 "auto lambda = []() {\n" 18220 " auto ii = 0;\n" 18221 " float j = 0;\n" 18222 " return 0;\n" 18223 "};\n" 18224 "int i = 0;\n" 18225 "float i2 = 0;\n" 18226 "auto v = type{\n" 18227 " i = 1, //\n" 18228 " (i = 2), //\n" 18229 " i = 3 //\n" 18230 "};", 18231 Style); 18232 18233 Style.AlignConsecutiveMacros.Enabled = false; 18234 Style.ColumnLimit = 20; 18235 18236 verifyFormat("#define a \\\n" 18237 " \"aabbbbbbbbbbbb\"\n" 18238 "#define D \\\n" 18239 " \"aabbbbbbbbbbbb\" \\\n" 18240 " \"ccddeeeeeeeee\"\n" 18241 "#define B \\\n" 18242 " \"QQQQQQQQQQQQQ\" \\\n" 18243 " \"FFFFFFFFFFFFF\" \\\n" 18244 " \"LLLLLLLL\"", 18245 Style); 18246 18247 Style.AlignConsecutiveMacros.Enabled = true; 18248 verifyFormat("#define a \\\n" 18249 " \"aabbbbbbbbbbbb\"\n" 18250 "#define D \\\n" 18251 " \"aabbbbbbbbbbbb\" \\\n" 18252 " \"ccddeeeeeeeee\"\n" 18253 "#define B \\\n" 18254 " \"QQQQQQQQQQQQQ\" \\\n" 18255 " \"FFFFFFFFFFFFF\" \\\n" 18256 " \"LLLLLLLL\"", 18257 Style); 18258 18259 // Test across comments 18260 Style.MaxEmptyLinesToKeep = 10; 18261 Style.ReflowComments = FormatStyle::RCS_Never; 18262 Style.AlignConsecutiveMacros.AcrossComments = true; 18263 verifyFormat("#define a 3\n" 18264 "// line comment\n" 18265 "#define bbbb 4\n" 18266 "#define ccc (5)", 18267 "#define a 3\n" 18268 "// line comment\n" 18269 "#define bbbb 4\n" 18270 "#define ccc (5)", 18271 Style); 18272 18273 verifyFormat("#define a 3\n" 18274 "/* block comment */\n" 18275 "#define bbbb 4\n" 18276 "#define ccc (5)", 18277 "#define a 3\n" 18278 "/* block comment */\n" 18279 "#define bbbb 4\n" 18280 "#define ccc (5)", 18281 Style); 18282 18283 verifyFormat("#define a 3\n" 18284 "/* multi-line *\n" 18285 " * block comment */\n" 18286 "#define bbbb 4\n" 18287 "#define ccc (5)", 18288 "#define a 3\n" 18289 "/* multi-line *\n" 18290 " * block comment */\n" 18291 "#define bbbb 4\n" 18292 "#define ccc (5)", 18293 Style); 18294 18295 verifyFormat("#define a 3\n" 18296 "// multi-line line comment\n" 18297 "//\n" 18298 "#define bbbb 4\n" 18299 "#define ccc (5)", 18300 "#define a 3\n" 18301 "// multi-line line comment\n" 18302 "//\n" 18303 "#define bbbb 4\n" 18304 "#define ccc (5)", 18305 Style); 18306 18307 verifyFormat("#define a 3\n" 18308 "// empty lines still break.\n" 18309 "\n" 18310 "#define bbbb 4\n" 18311 "#define ccc (5)", 18312 "#define a 3\n" 18313 "// empty lines still break.\n" 18314 "\n" 18315 "#define bbbb 4\n" 18316 "#define ccc (5)", 18317 Style); 18318 18319 // Test across empty lines 18320 Style.AlignConsecutiveMacros.AcrossComments = false; 18321 Style.AlignConsecutiveMacros.AcrossEmptyLines = true; 18322 verifyFormat("#define a 3\n" 18323 "\n" 18324 "#define bbbb 4\n" 18325 "#define ccc (5)", 18326 "#define a 3\n" 18327 "\n" 18328 "#define bbbb 4\n" 18329 "#define ccc (5)", 18330 Style); 18331 18332 verifyFormat("#define a 3\n" 18333 "\n" 18334 "\n" 18335 "\n" 18336 "#define bbbb 4\n" 18337 "#define ccc (5)", 18338 "#define a 3\n" 18339 "\n" 18340 "\n" 18341 "\n" 18342 "#define bbbb 4\n" 18343 "#define ccc (5)", 18344 Style); 18345 18346 verifyFormat("#define a 3\n" 18347 "// comments should break alignment\n" 18348 "//\n" 18349 "#define bbbb 4\n" 18350 "#define ccc (5)", 18351 "#define a 3\n" 18352 "// comments should break alignment\n" 18353 "//\n" 18354 "#define bbbb 4\n" 18355 "#define ccc (5)", 18356 Style); 18357 18358 // Test across empty lines and comments 18359 Style.AlignConsecutiveMacros.AcrossComments = true; 18360 verifyFormat("#define a 3\n" 18361 "\n" 18362 "// line comment\n" 18363 "#define bbbb 4\n" 18364 "#define ccc (5)", 18365 Style); 18366 18367 verifyFormat("#define a 3\n" 18368 "\n" 18369 "\n" 18370 "/* multi-line *\n" 18371 " * block comment */\n" 18372 "\n" 18373 "\n" 18374 "#define bbbb 4\n" 18375 "#define ccc (5)", 18376 "#define a 3\n" 18377 "\n" 18378 "\n" 18379 "/* multi-line *\n" 18380 " * block comment */\n" 18381 "\n" 18382 "\n" 18383 "#define bbbb 4\n" 18384 "#define ccc (5)", 18385 Style); 18386 18387 verifyFormat("#define a 3\n" 18388 "\n" 18389 "\n" 18390 "/* multi-line *\n" 18391 " * block comment */\n" 18392 "\n" 18393 "\n" 18394 "#define bbbb 4\n" 18395 "#define ccc (5)", 18396 "#define a 3\n" 18397 "\n" 18398 "\n" 18399 "/* multi-line *\n" 18400 " * block comment */\n" 18401 "\n" 18402 "\n" 18403 "#define bbbb 4\n" 18404 "#define ccc (5)", 18405 Style); 18406 } 18407 18408 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 18409 FormatStyle Alignment = getLLVMStyle(); 18410 Alignment.AlignConsecutiveMacros.Enabled = true; 18411 Alignment.AlignConsecutiveAssignments.Enabled = true; 18412 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; 18413 18414 Alignment.MaxEmptyLinesToKeep = 10; 18415 /* Test alignment across empty lines */ 18416 verifyFormat("int a = 5;\n" 18417 "\n" 18418 "int oneTwoThree = 123;", 18419 "int a = 5;\n" 18420 "\n" 18421 "int oneTwoThree= 123;", 18422 Alignment); 18423 verifyFormat("int a = 5;\n" 18424 "int one = 1;\n" 18425 "\n" 18426 "int oneTwoThree = 123;", 18427 "int a = 5;\n" 18428 "int one = 1;\n" 18429 "\n" 18430 "int oneTwoThree = 123;", 18431 Alignment); 18432 verifyFormat("int a = 5;\n" 18433 "int one = 1;\n" 18434 "\n" 18435 "int oneTwoThree = 123;\n" 18436 "int oneTwo = 12;", 18437 "int a = 5;\n" 18438 "int one = 1;\n" 18439 "\n" 18440 "int oneTwoThree = 123;\n" 18441 "int oneTwo = 12;", 18442 Alignment); 18443 18444 /* Test across comments */ 18445 verifyFormat("int a = 5;\n" 18446 "/* block comment */\n" 18447 "int oneTwoThree = 123;", 18448 "int a = 5;\n" 18449 "/* block comment */\n" 18450 "int oneTwoThree=123;", 18451 Alignment); 18452 18453 verifyFormat("int a = 5;\n" 18454 "// line comment\n" 18455 "int oneTwoThree = 123;", 18456 "int a = 5;\n" 18457 "// line comment\n" 18458 "int oneTwoThree=123;", 18459 Alignment); 18460 18461 /* Test across comments and newlines */ 18462 verifyFormat("int a = 5;\n" 18463 "\n" 18464 "/* block comment */\n" 18465 "int oneTwoThree = 123;", 18466 "int a = 5;\n" 18467 "\n" 18468 "/* block comment */\n" 18469 "int oneTwoThree=123;", 18470 Alignment); 18471 18472 verifyFormat("int a = 5;\n" 18473 "\n" 18474 "// line comment\n" 18475 "int oneTwoThree = 123;", 18476 "int a = 5;\n" 18477 "\n" 18478 "// line comment\n" 18479 "int oneTwoThree=123;", 18480 Alignment); 18481 } 18482 18483 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 18484 FormatStyle Alignment = getLLVMStyle(); 18485 Alignment.AlignConsecutiveDeclarations.Enabled = true; 18486 Alignment.AlignConsecutiveDeclarations.AcrossEmptyLines = true; 18487 Alignment.AlignConsecutiveDeclarations.AcrossComments = true; 18488 18489 Alignment.MaxEmptyLinesToKeep = 10; 18490 /* Test alignment across empty lines */ 18491 verifyFormat("int a = 5;\n" 18492 "\n" 18493 "float const oneTwoThree = 123;", 18494 "int a = 5;\n" 18495 "\n" 18496 "float const oneTwoThree = 123;", 18497 Alignment); 18498 verifyFormat("int a = 5;\n" 18499 "float const one = 1;\n" 18500 "\n" 18501 "int oneTwoThree = 123;", 18502 "int a = 5;\n" 18503 "float const one = 1;\n" 18504 "\n" 18505 "int oneTwoThree = 123;", 18506 Alignment); 18507 18508 /* Test across comments */ 18509 verifyFormat("float const a = 5;\n" 18510 "/* block comment */\n" 18511 "int oneTwoThree = 123;", 18512 "float const a = 5;\n" 18513 "/* block comment */\n" 18514 "int oneTwoThree=123;", 18515 Alignment); 18516 18517 verifyFormat("float const a = 5;\n" 18518 "// line comment\n" 18519 "int oneTwoThree = 123;", 18520 "float const a = 5;\n" 18521 "// line comment\n" 18522 "int oneTwoThree=123;", 18523 Alignment); 18524 18525 /* Test across comments and newlines */ 18526 verifyFormat("float const a = 5;\n" 18527 "\n" 18528 "/* block comment */\n" 18529 "int oneTwoThree = 123;", 18530 "float const a = 5;\n" 18531 "\n" 18532 "/* block comment */\n" 18533 "int oneTwoThree=123;", 18534 Alignment); 18535 18536 verifyFormat("float const a = 5;\n" 18537 "\n" 18538 "// line comment\n" 18539 "int oneTwoThree = 123;", 18540 "float const a = 5;\n" 18541 "\n" 18542 "// line comment\n" 18543 "int oneTwoThree=123;", 18544 Alignment); 18545 } 18546 18547 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 18548 FormatStyle Alignment = getLLVMStyle(); 18549 Alignment.AlignConsecutiveBitFields.Enabled = true; 18550 Alignment.AlignConsecutiveBitFields.AcrossEmptyLines = true; 18551 Alignment.AlignConsecutiveBitFields.AcrossComments = true; 18552 18553 Alignment.MaxEmptyLinesToKeep = 10; 18554 /* Test alignment across empty lines */ 18555 verifyFormat("int a : 5;\n" 18556 "\n" 18557 "int longbitfield : 6;", 18558 "int a : 5;\n" 18559 "\n" 18560 "int longbitfield : 6;", 18561 Alignment); 18562 verifyFormat("int a : 5;\n" 18563 "int one : 1;\n" 18564 "\n" 18565 "int longbitfield : 6;", 18566 "int a : 5;\n" 18567 "int one : 1;\n" 18568 "\n" 18569 "int longbitfield : 6;", 18570 Alignment); 18571 18572 /* Test across comments */ 18573 verifyFormat("int a : 5;\n" 18574 "/* block comment */\n" 18575 "int longbitfield : 6;", 18576 "int a : 5;\n" 18577 "/* block comment */\n" 18578 "int longbitfield : 6;", 18579 Alignment); 18580 verifyFormat("int a : 5;\n" 18581 "int one : 1;\n" 18582 "// line comment\n" 18583 "int longbitfield : 6;", 18584 "int a : 5;\n" 18585 "int one : 1;\n" 18586 "// line comment\n" 18587 "int longbitfield : 6;", 18588 Alignment); 18589 18590 /* Test across comments and newlines */ 18591 verifyFormat("int a : 5;\n" 18592 "/* block comment */\n" 18593 "\n" 18594 "int longbitfield : 6;", 18595 "int a : 5;\n" 18596 "/* block comment */\n" 18597 "\n" 18598 "int longbitfield : 6;", 18599 Alignment); 18600 verifyFormat("int a : 5;\n" 18601 "int one : 1;\n" 18602 "\n" 18603 "// line comment\n" 18604 "\n" 18605 "int longbitfield : 6;", 18606 "int a : 5;\n" 18607 "int one : 1;\n" 18608 "\n" 18609 "// line comment \n" 18610 "\n" 18611 "int longbitfield : 6;", 18612 Alignment); 18613 } 18614 18615 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 18616 FormatStyle Alignment = getLLVMStyle(); 18617 Alignment.AlignConsecutiveMacros.Enabled = true; 18618 Alignment.AlignConsecutiveAssignments.Enabled = true; 18619 Alignment.AlignConsecutiveAssignments.AcrossComments = true; 18620 18621 Alignment.MaxEmptyLinesToKeep = 10; 18622 /* Test alignment across empty lines */ 18623 verifyFormat("int a = 5;\n" 18624 "\n" 18625 "int oneTwoThree = 123;", 18626 "int a = 5;\n" 18627 "\n" 18628 "int oneTwoThree= 123;", 18629 Alignment); 18630 verifyFormat("int a = 5;\n" 18631 "int one = 1;\n" 18632 "\n" 18633 "int oneTwoThree = 123;", 18634 "int a = 5;\n" 18635 "int one = 1;\n" 18636 "\n" 18637 "int oneTwoThree = 123;", 18638 Alignment); 18639 18640 /* Test across comments */ 18641 verifyFormat("int a = 5;\n" 18642 "/* block comment */\n" 18643 "int oneTwoThree = 123;", 18644 "int a = 5;\n" 18645 "/* block comment */\n" 18646 "int oneTwoThree=123;", 18647 Alignment); 18648 18649 verifyFormat("int a = 5;\n" 18650 "// line comment\n" 18651 "int oneTwoThree = 123;", 18652 "int a = 5;\n" 18653 "// line comment\n" 18654 "int oneTwoThree=123;", 18655 Alignment); 18656 18657 verifyFormat("int a = 5;\n" 18658 "/*\n" 18659 " * multi-line block comment\n" 18660 " */\n" 18661 "int oneTwoThree = 123;", 18662 "int a = 5;\n" 18663 "/*\n" 18664 " * multi-line block comment\n" 18665 " */\n" 18666 "int oneTwoThree=123;", 18667 Alignment); 18668 18669 verifyFormat("int a = 5;\n" 18670 "//\n" 18671 "// multi-line line comment\n" 18672 "//\n" 18673 "int oneTwoThree = 123;", 18674 "int a = 5;\n" 18675 "//\n" 18676 "// multi-line line comment\n" 18677 "//\n" 18678 "int oneTwoThree=123;", 18679 Alignment); 18680 18681 /* Test across comments and newlines */ 18682 verifyFormat("int a = 5;\n" 18683 "\n" 18684 "/* block comment */\n" 18685 "int oneTwoThree = 123;", 18686 "int a = 5;\n" 18687 "\n" 18688 "/* block comment */\n" 18689 "int oneTwoThree=123;", 18690 Alignment); 18691 18692 verifyFormat("int a = 5;\n" 18693 "\n" 18694 "// line comment\n" 18695 "int oneTwoThree = 123;", 18696 "int a = 5;\n" 18697 "\n" 18698 "// line comment\n" 18699 "int oneTwoThree=123;", 18700 Alignment); 18701 } 18702 18703 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 18704 FormatStyle Alignment = getLLVMStyle(); 18705 Alignment.AlignConsecutiveMacros.Enabled = true; 18706 Alignment.AlignConsecutiveAssignments.Enabled = true; 18707 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; 18708 Alignment.AlignConsecutiveAssignments.AcrossComments = true; 18709 verifyFormat("int a = 5;\n" 18710 "int oneTwoThree = 123;", 18711 Alignment); 18712 verifyFormat("int a = method();\n" 18713 "int oneTwoThree = 133;", 18714 Alignment); 18715 verifyFormat("a &= 5;\n" 18716 "bcd *= 5;\n" 18717 "ghtyf += 5;\n" 18718 "dvfvdb -= 5;\n" 18719 "a /= 5;\n" 18720 "vdsvsv %= 5;\n" 18721 "sfdbddfbdfbb ^= 5;\n" 18722 "dvsdsv |= 5;\n" 18723 "int dsvvdvsdvvv = 123;", 18724 Alignment); 18725 verifyFormat("int i = 1, j = 10;\n" 18726 "something = 2000;", 18727 Alignment); 18728 verifyFormat("something = 2000;\n" 18729 "int i = 1, j = 10;", 18730 Alignment); 18731 verifyFormat("something = 2000;\n" 18732 "another = 911;\n" 18733 "int i = 1, j = 10;\n" 18734 "oneMore = 1;\n" 18735 "i = 2;", 18736 Alignment); 18737 verifyFormat("int a = 5;\n" 18738 "int one = 1;\n" 18739 "method();\n" 18740 "int oneTwoThree = 123;\n" 18741 "int oneTwo = 12;", 18742 Alignment); 18743 verifyFormat("int oneTwoThree = 123;\n" 18744 "int oneTwo = 12;\n" 18745 "method();", 18746 Alignment); 18747 verifyFormat("int oneTwoThree = 123; // comment\n" 18748 "int oneTwo = 12; // comment", 18749 Alignment); 18750 18751 // Bug 25167 18752 /* Uncomment when fixed 18753 verifyFormat("#if A\n" 18754 "#else\n" 18755 "int aaaaaaaa = 12;\n" 18756 "#endif\n" 18757 "#if B\n" 18758 "#else\n" 18759 "int a = 12;\n" 18760 "#endif", 18761 Alignment); 18762 verifyFormat("enum foo {\n" 18763 "#if A\n" 18764 "#else\n" 18765 " aaaaaaaa = 12;\n" 18766 "#endif\n" 18767 "#if B\n" 18768 "#else\n" 18769 " a = 12;\n" 18770 "#endif\n" 18771 "};", 18772 Alignment); 18773 */ 18774 18775 Alignment.MaxEmptyLinesToKeep = 10; 18776 /* Test alignment across empty lines */ 18777 verifyFormat("int a = 5;\n" 18778 "\n" 18779 "int oneTwoThree = 123;", 18780 "int a = 5;\n" 18781 "\n" 18782 "int oneTwoThree= 123;", 18783 Alignment); 18784 verifyFormat("int a = 5;\n" 18785 "int one = 1;\n" 18786 "\n" 18787 "int oneTwoThree = 123;", 18788 "int a = 5;\n" 18789 "int one = 1;\n" 18790 "\n" 18791 "int oneTwoThree = 123;", 18792 Alignment); 18793 verifyFormat("int a = 5;\n" 18794 "int one = 1;\n" 18795 "\n" 18796 "int oneTwoThree = 123;\n" 18797 "int oneTwo = 12;", 18798 "int a = 5;\n" 18799 "int one = 1;\n" 18800 "\n" 18801 "int oneTwoThree = 123;\n" 18802 "int oneTwo = 12;", 18803 Alignment); 18804 18805 /* Test across comments */ 18806 verifyFormat("int a = 5;\n" 18807 "/* block comment */\n" 18808 "int oneTwoThree = 123;", 18809 "int a = 5;\n" 18810 "/* block comment */\n" 18811 "int oneTwoThree=123;", 18812 Alignment); 18813 18814 verifyFormat("int a = 5;\n" 18815 "// line comment\n" 18816 "int oneTwoThree = 123;", 18817 "int a = 5;\n" 18818 "// line comment\n" 18819 "int oneTwoThree=123;", 18820 Alignment); 18821 18822 /* Test across comments and newlines */ 18823 verifyFormat("int a = 5;\n" 18824 "\n" 18825 "/* block comment */\n" 18826 "int oneTwoThree = 123;", 18827 "int a = 5;\n" 18828 "\n" 18829 "/* block comment */\n" 18830 "int oneTwoThree=123;", 18831 Alignment); 18832 18833 verifyFormat("int a = 5;\n" 18834 "\n" 18835 "// line comment\n" 18836 "int oneTwoThree = 123;", 18837 "int a = 5;\n" 18838 "\n" 18839 "// line comment\n" 18840 "int oneTwoThree=123;", 18841 Alignment); 18842 18843 verifyFormat("int a = 5;\n" 18844 "//\n" 18845 "// multi-line line comment\n" 18846 "//\n" 18847 "int oneTwoThree = 123;", 18848 "int a = 5;\n" 18849 "//\n" 18850 "// multi-line line comment\n" 18851 "//\n" 18852 "int oneTwoThree=123;", 18853 Alignment); 18854 18855 verifyFormat("int a = 5;\n" 18856 "/*\n" 18857 " * multi-line block comment\n" 18858 " */\n" 18859 "int oneTwoThree = 123;", 18860 "int a = 5;\n" 18861 "/*\n" 18862 " * multi-line block comment\n" 18863 " */\n" 18864 "int oneTwoThree=123;", 18865 Alignment); 18866 18867 verifyFormat("int a = 5;\n" 18868 "\n" 18869 "/* block comment */\n" 18870 "\n" 18871 "\n" 18872 "\n" 18873 "int oneTwoThree = 123;", 18874 "int a = 5;\n" 18875 "\n" 18876 "/* block comment */\n" 18877 "\n" 18878 "\n" 18879 "\n" 18880 "int oneTwoThree=123;", 18881 Alignment); 18882 18883 verifyFormat("int a = 5;\n" 18884 "\n" 18885 "// line comment\n" 18886 "\n" 18887 "\n" 18888 "\n" 18889 "int oneTwoThree = 123;", 18890 "int a = 5;\n" 18891 "\n" 18892 "// line comment\n" 18893 "\n" 18894 "\n" 18895 "\n" 18896 "int oneTwoThree=123;", 18897 Alignment); 18898 18899 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 18900 verifyFormat("#define A \\\n" 18901 " int aaaa = 12; \\\n" 18902 " int b = 23; \\\n" 18903 " int ccc = 234; \\\n" 18904 " int dddddddddd = 2345;", 18905 Alignment); 18906 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 18907 verifyFormat("#define A \\\n" 18908 " int aaaa = 12; \\\n" 18909 " int b = 23; \\\n" 18910 " int ccc = 234; \\\n" 18911 " int dddddddddd = 2345;", 18912 Alignment); 18913 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 18914 verifyFormat("#define A " 18915 " \\\n" 18916 " int aaaa = 12; " 18917 " \\\n" 18918 " int b = 23; " 18919 " \\\n" 18920 " int ccc = 234; " 18921 " \\\n" 18922 " int dddddddddd = 2345;", 18923 Alignment); 18924 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 18925 "k = 4, int l = 5,\n" 18926 " int m = 6) {\n" 18927 " int j = 10;\n" 18928 " otherThing = 1;\n" 18929 "}", 18930 Alignment); 18931 verifyFormat("void SomeFunction(int parameter = 0) {\n" 18932 " int i = 1;\n" 18933 " int j = 2;\n" 18934 " int big = 10000;\n" 18935 "}", 18936 Alignment); 18937 verifyFormat("class C {\n" 18938 "public:\n" 18939 " int i = 1;\n" 18940 " virtual void f() = 0;\n" 18941 "};", 18942 Alignment); 18943 verifyFormat("int i = 1;\n" 18944 "if (SomeType t = getSomething()) {\n" 18945 "}\n" 18946 "int j = 2;\n" 18947 "int big = 10000;", 18948 Alignment); 18949 verifyFormat("int j = 7;\n" 18950 "for (int k = 0; k < N; ++k) {\n" 18951 "}\n" 18952 "int j = 2;\n" 18953 "int big = 10000;\n" 18954 "}", 18955 Alignment); 18956 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 18957 verifyFormat("int i = 1;\n" 18958 "LooooooooooongType loooooooooooooooooooooongVariable\n" 18959 " = someLooooooooooooooooongFunction();\n" 18960 "int j = 2;", 18961 Alignment); 18962 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 18963 verifyFormat("int i = 1;\n" 18964 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 18965 " someLooooooooooooooooongFunction();\n" 18966 "int j = 2;", 18967 Alignment); 18968 18969 verifyFormat("auto lambda = []() {\n" 18970 " auto i = 0;\n" 18971 " return 0;\n" 18972 "};\n" 18973 "int i = 0;\n" 18974 "auto v = type{\n" 18975 " i = 1, //\n" 18976 " (i = 2), //\n" 18977 " i = 3 //\n" 18978 "};", 18979 Alignment); 18980 18981 verifyFormat( 18982 "int i = 1;\n" 18983 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 18984 " loooooooooooooooooooooongParameterB);\n" 18985 "int j = 2;", 18986 Alignment); 18987 18988 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 18989 " typename B = very_long_type_name_1,\n" 18990 " typename T_2 = very_long_type_name_2>\n" 18991 "auto foo() {}", 18992 Alignment); 18993 verifyFormat("int a, b = 1;\n" 18994 "int c = 2;\n" 18995 "int dd = 3;", 18996 Alignment); 18997 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 18998 "float b[1][] = {{3.f}};", 18999 Alignment); 19000 verifyFormat("for (int i = 0; i < 1; i++)\n" 19001 " int x = 1;", 19002 Alignment); 19003 verifyFormat("for (i = 0; i < 1; i++)\n" 19004 " x = 1;\n" 19005 "y = 1;", 19006 Alignment); 19007 19008 Alignment.ReflowComments = FormatStyle::RCS_Always; 19009 Alignment.ColumnLimit = 50; 19010 verifyFormat("int x = 0;\n" 19011 "int yy = 1; /// specificlennospace\n" 19012 "int zzz = 2;", 19013 "int x = 0;\n" 19014 "int yy = 1; ///specificlennospace\n" 19015 "int zzz = 2;", 19016 Alignment); 19017 } 19018 19019 TEST_F(FormatTest, AlignCompoundAssignments) { 19020 FormatStyle Alignment = getLLVMStyle(); 19021 Alignment.AlignConsecutiveAssignments.Enabled = true; 19022 Alignment.AlignConsecutiveAssignments.AlignCompound = true; 19023 Alignment.AlignConsecutiveAssignments.PadOperators = false; 19024 verifyFormat("sfdbddfbdfbb = 5;\n" 19025 "dvsdsv = 5;\n" 19026 "int dsvvdvsdvvv = 123;", 19027 Alignment); 19028 verifyFormat("sfdbddfbdfbb ^= 5;\n" 19029 "dvsdsv |= 5;\n" 19030 "int dsvvdvsdvvv = 123;", 19031 Alignment); 19032 verifyFormat("sfdbddfbdfbb ^= 5;\n" 19033 "dvsdsv <<= 5;\n" 19034 "int dsvvdvsdvvv = 123;", 19035 Alignment); 19036 verifyFormat("int xxx = 5;\n" 19037 "xxx = 5;\n" 19038 "{\n" 19039 " int yyy = 6;\n" 19040 " yyy = 6;\n" 19041 "}", 19042 Alignment); 19043 verifyFormat("int xxx = 5;\n" 19044 "xxx += 5;\n" 19045 "{\n" 19046 " int yyy = 6;\n" 19047 " yyy += 6;\n" 19048 "}", 19049 Alignment); 19050 // Test that `<=` is not treated as a compound assignment. 19051 verifyFormat("aa &= 5;\n" 19052 "b <= 10;\n" 19053 "c = 15;", 19054 Alignment); 19055 Alignment.AlignConsecutiveAssignments.PadOperators = true; 19056 verifyFormat("sfdbddfbdfbb = 5;\n" 19057 "dvsdsv = 5;\n" 19058 "int dsvvdvsdvvv = 123;", 19059 Alignment); 19060 verifyFormat("sfdbddfbdfbb ^= 5;\n" 19061 "dvsdsv |= 5;\n" 19062 "int dsvvdvsdvvv = 123;", 19063 Alignment); 19064 verifyFormat("sfdbddfbdfbb ^= 5;\n" 19065 "dvsdsv <<= 5;\n" 19066 "int dsvvdvsdvvv = 123;", 19067 Alignment); 19068 verifyFormat("a += 5;\n" 19069 "one = 1;\n" 19070 "\n" 19071 "oneTwoThree = 123;", 19072 "a += 5;\n" 19073 "one = 1;\n" 19074 "\n" 19075 "oneTwoThree = 123;", 19076 Alignment); 19077 verifyFormat("a += 5;\n" 19078 "one = 1;\n" 19079 "//\n" 19080 "oneTwoThree = 123;", 19081 "a += 5;\n" 19082 "one = 1;\n" 19083 "//\n" 19084 "oneTwoThree = 123;", 19085 Alignment); 19086 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; 19087 verifyFormat("a += 5;\n" 19088 "one = 1;\n" 19089 "\n" 19090 "oneTwoThree = 123;", 19091 "a += 5;\n" 19092 "one = 1;\n" 19093 "\n" 19094 "oneTwoThree = 123;", 19095 Alignment); 19096 verifyFormat("a += 5;\n" 19097 "one = 1;\n" 19098 "//\n" 19099 "oneTwoThree = 123;", 19100 "a += 5;\n" 19101 "one = 1;\n" 19102 "//\n" 19103 "oneTwoThree = 123;", 19104 Alignment); 19105 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = false; 19106 Alignment.AlignConsecutiveAssignments.AcrossComments = true; 19107 verifyFormat("a += 5;\n" 19108 "one = 1;\n" 19109 "\n" 19110 "oneTwoThree = 123;", 19111 "a += 5;\n" 19112 "one = 1;\n" 19113 "\n" 19114 "oneTwoThree = 123;", 19115 Alignment); 19116 verifyFormat("a += 5;\n" 19117 "one = 1;\n" 19118 "//\n" 19119 "oneTwoThree = 123;", 19120 "a += 5;\n" 19121 "one = 1;\n" 19122 "//\n" 19123 "oneTwoThree = 123;", 19124 Alignment); 19125 Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; 19126 verifyFormat("a += 5;\n" 19127 "one >>= 1;\n" 19128 "\n" 19129 "oneTwoThree = 123;", 19130 "a += 5;\n" 19131 "one >>= 1;\n" 19132 "\n" 19133 "oneTwoThree = 123;", 19134 Alignment); 19135 verifyFormat("a += 5;\n" 19136 "one = 1;\n" 19137 "//\n" 19138 "oneTwoThree <<= 123;", 19139 "a += 5;\n" 19140 "one = 1;\n" 19141 "//\n" 19142 "oneTwoThree <<= 123;", 19143 Alignment); 19144 } 19145 19146 TEST_F(FormatTest, AlignConsecutiveAssignments) { 19147 FormatStyle Alignment = getLLVMStyle(); 19148 Alignment.AlignConsecutiveMacros.Enabled = true; 19149 verifyFormat("int a = 5;\n" 19150 "int oneTwoThree = 123;", 19151 Alignment); 19152 verifyFormat("int a = 5;\n" 19153 "int oneTwoThree = 123;", 19154 Alignment); 19155 19156 Alignment.AlignConsecutiveAssignments.Enabled = true; 19157 verifyFormat("int a = 5;\n" 19158 "int oneTwoThree = 123;", 19159 Alignment); 19160 verifyFormat("int a = method();\n" 19161 "int oneTwoThree = 133;", 19162 Alignment); 19163 verifyFormat("aa <= 5;\n" 19164 "a &= 5;\n" 19165 "bcd *= 5;\n" 19166 "ghtyf += 5;\n" 19167 "dvfvdb -= 5;\n" 19168 "a /= 5;\n" 19169 "vdsvsv %= 5;\n" 19170 "sfdbddfbdfbb ^= 5;\n" 19171 "dvsdsv |= 5;\n" 19172 "int dsvvdvsdvvv = 123;", 19173 Alignment); 19174 verifyFormat("int i = 1, j = 10;\n" 19175 "something = 2000;", 19176 Alignment); 19177 verifyFormat("something = 2000;\n" 19178 "int i = 1, j = 10;", 19179 Alignment); 19180 verifyFormat("something = 2000;\n" 19181 "another = 911;\n" 19182 "int i = 1, j = 10;\n" 19183 "oneMore = 1;\n" 19184 "i = 2;", 19185 Alignment); 19186 verifyFormat("int a = 5;\n" 19187 "int one = 1;\n" 19188 "method();\n" 19189 "int oneTwoThree = 123;\n" 19190 "int oneTwo = 12;", 19191 Alignment); 19192 verifyFormat("int oneTwoThree = 123;\n" 19193 "int oneTwo = 12;\n" 19194 "method();", 19195 Alignment); 19196 verifyFormat("int oneTwoThree = 123; // comment\n" 19197 "int oneTwo = 12; // comment", 19198 Alignment); 19199 verifyFormat("int f() = default;\n" 19200 "int &operator() = default;\n" 19201 "int &operator=() {", 19202 Alignment); 19203 verifyFormat("int f() = delete;\n" 19204 "int &operator() = delete;\n" 19205 "int &operator=() {", 19206 Alignment); 19207 verifyFormat("int f() = default; // comment\n" 19208 "int &operator() = default; // comment\n" 19209 "int &operator=() {", 19210 Alignment); 19211 verifyFormat("int f() = default;\n" 19212 "int &operator() = default;\n" 19213 "int &operator==() {", 19214 Alignment); 19215 verifyFormat("int f() = default;\n" 19216 "int &operator() = default;\n" 19217 "int &operator<=() {", 19218 Alignment); 19219 verifyFormat("int f() = default;\n" 19220 "int &operator() = default;\n" 19221 "int &operator!=() {", 19222 Alignment); 19223 verifyFormat("int f() = default;\n" 19224 "int &operator() = default;\n" 19225 "int &operator=();", 19226 Alignment); 19227 verifyFormat("int f() = delete;\n" 19228 "int &operator() = delete;\n" 19229 "int &operator=();", 19230 Alignment); 19231 verifyFormat("/* long long padding */ int f() = default;\n" 19232 "int &operator() = default;\n" 19233 "int &operator/**/ =();", 19234 Alignment); 19235 // https://llvm.org/PR33697 19236 FormatStyle AlignmentWithPenalty = getLLVMStyle(); 19237 AlignmentWithPenalty.AlignConsecutiveAssignments.Enabled = true; 19238 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000; 19239 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n" 19240 " void f() = delete;\n" 19241 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n" 19242 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n" 19243 "};", 19244 AlignmentWithPenalty); 19245 19246 // Bug 25167 19247 /* Uncomment when fixed 19248 verifyFormat("#if A\n" 19249 "#else\n" 19250 "int aaaaaaaa = 12;\n" 19251 "#endif\n" 19252 "#if B\n" 19253 "#else\n" 19254 "int a = 12;\n" 19255 "#endif", 19256 Alignment); 19257 verifyFormat("enum foo {\n" 19258 "#if A\n" 19259 "#else\n" 19260 " aaaaaaaa = 12;\n" 19261 "#endif\n" 19262 "#if B\n" 19263 "#else\n" 19264 " a = 12;\n" 19265 "#endif\n" 19266 "};", 19267 Alignment); 19268 */ 19269 19270 verifyFormat("int a = 5;\n" 19271 "\n" 19272 "int oneTwoThree = 123;", 19273 "int a = 5;\n" 19274 "\n" 19275 "int oneTwoThree= 123;", 19276 Alignment); 19277 verifyFormat("int a = 5;\n" 19278 "int one = 1;\n" 19279 "\n" 19280 "int oneTwoThree = 123;", 19281 "int a = 5;\n" 19282 "int one = 1;\n" 19283 "\n" 19284 "int oneTwoThree = 123;", 19285 Alignment); 19286 verifyFormat("int a = 5;\n" 19287 "int one = 1;\n" 19288 "\n" 19289 "int oneTwoThree = 123;\n" 19290 "int oneTwo = 12;", 19291 "int a = 5;\n" 19292 "int one = 1;\n" 19293 "\n" 19294 "int oneTwoThree = 123;\n" 19295 "int oneTwo = 12;", 19296 Alignment); 19297 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 19298 verifyFormat("#define A \\\n" 19299 " int aaaa = 12; \\\n" 19300 " int b = 23; \\\n" 19301 " int ccc = 234; \\\n" 19302 " int dddddddddd = 2345;", 19303 Alignment); 19304 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 19305 verifyFormat("#define A \\\n" 19306 " int aaaa = 12; \\\n" 19307 " int b = 23; \\\n" 19308 " int ccc = 234; \\\n" 19309 " int dddddddddd = 2345;", 19310 Alignment); 19311 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 19312 verifyFormat("#define A " 19313 " \\\n" 19314 " int aaaa = 12; " 19315 " \\\n" 19316 " int b = 23; " 19317 " \\\n" 19318 " int ccc = 234; " 19319 " \\\n" 19320 " int dddddddddd = 2345;", 19321 Alignment); 19322 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 19323 "k = 4, int l = 5,\n" 19324 " int m = 6) {\n" 19325 " int j = 10;\n" 19326 " otherThing = 1;\n" 19327 "}", 19328 Alignment); 19329 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19330 " int i = 1;\n" 19331 " int j = 2;\n" 19332 " int big = 10000;\n" 19333 "}", 19334 Alignment); 19335 verifyFormat("class C {\n" 19336 "public:\n" 19337 " int i = 1;\n" 19338 " virtual void f() = 0;\n" 19339 "};", 19340 Alignment); 19341 verifyFormat("int i = 1;\n" 19342 "if (SomeType t = getSomething()) {\n" 19343 "}\n" 19344 "int j = 2;\n" 19345 "int big = 10000;", 19346 Alignment); 19347 verifyFormat("int j = 7;\n" 19348 "for (int k = 0; k < N; ++k) {\n" 19349 "}\n" 19350 "int j = 2;\n" 19351 "int big = 10000;\n" 19352 "}", 19353 Alignment); 19354 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 19355 verifyFormat("int i = 1;\n" 19356 "LooooooooooongType loooooooooooooooooooooongVariable\n" 19357 " = someLooooooooooooooooongFunction();\n" 19358 "int j = 2;", 19359 Alignment); 19360 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 19361 verifyFormat("int i = 1;\n" 19362 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 19363 " someLooooooooooooooooongFunction();\n" 19364 "int j = 2;", 19365 Alignment); 19366 19367 verifyFormat("auto lambda = []() {\n" 19368 " auto i = 0;\n" 19369 " return 0;\n" 19370 "};\n" 19371 "int i = 0;\n" 19372 "auto v = type{\n" 19373 " i = 1, //\n" 19374 " (i = 2), //\n" 19375 " i = 3 //\n" 19376 "};", 19377 Alignment); 19378 19379 verifyFormat( 19380 "int i = 1;\n" 19381 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 19382 " loooooooooooooooooooooongParameterB);\n" 19383 "int j = 2;", 19384 Alignment); 19385 19386 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 19387 " typename B = very_long_type_name_1,\n" 19388 " typename T_2 = very_long_type_name_2>\n" 19389 "auto foo() {}", 19390 Alignment); 19391 verifyFormat("int a, b = 1;\n" 19392 "int c = 2;\n" 19393 "int dd = 3;", 19394 Alignment); 19395 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 19396 "float b[1][] = {{3.f}};", 19397 Alignment); 19398 verifyFormat("for (int i = 0; i < 1; i++)\n" 19399 " int x = 1;", 19400 Alignment); 19401 verifyFormat("for (i = 0; i < 1; i++)\n" 19402 " x = 1;\n" 19403 "y = 1;", 19404 Alignment); 19405 19406 EXPECT_EQ(Alignment.ReflowComments, FormatStyle::RCS_Always); 19407 Alignment.ColumnLimit = 50; 19408 verifyFormat("int x = 0;\n" 19409 "int yy = 1; /// specificlennospace\n" 19410 "int zzz = 2;", 19411 "int x = 0;\n" 19412 "int yy = 1; ///specificlennospace\n" 19413 "int zzz = 2;", 19414 Alignment); 19415 19416 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 19417 "auto b = [] {\n" 19418 " f();\n" 19419 " return;\n" 19420 "};", 19421 Alignment); 19422 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 19423 "auto b = g([] {\n" 19424 " f();\n" 19425 " return;\n" 19426 "});", 19427 Alignment); 19428 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 19429 "auto b = g(param, [] {\n" 19430 " f();\n" 19431 " return;\n" 19432 "});", 19433 Alignment); 19434 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 19435 "auto b = [] {\n" 19436 " if (condition) {\n" 19437 " return;\n" 19438 " }\n" 19439 "};", 19440 Alignment); 19441 19442 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 19443 " ccc ? aaaaa : bbbbb,\n" 19444 " dddddddddddddddddddddddddd);", 19445 Alignment); 19446 // FIXME: https://llvm.org/PR53497 19447 // verifyFormat("auto aaaaaaaaaaaa = f();\n" 19448 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 19449 // " ccc ? aaaaa : bbbbb,\n" 19450 // " dddddddddddddddddddddddddd);", 19451 // Alignment); 19452 19453 // Confirm proper handling of AlignConsecutiveAssignments with 19454 // BinPackArguments. 19455 // See https://llvm.org/PR55360 19456 Alignment = getLLVMStyleWithColumns(50); 19457 Alignment.AlignConsecutiveAssignments.Enabled = true; 19458 Alignment.BinPackArguments = false; 19459 verifyFormat("int a_long_name = 1;\n" 19460 "auto b = B({a_long_name, a_long_name},\n" 19461 " {a_longer_name_for_wrap,\n" 19462 " a_longer_name_for_wrap});", 19463 Alignment); 19464 verifyFormat("int a_long_name = 1;\n" 19465 "auto b = B{{a_long_name, a_long_name},\n" 19466 " {a_longer_name_for_wrap,\n" 19467 " a_longer_name_for_wrap}};", 19468 Alignment); 19469 19470 Alignment = getLLVMStyleWithColumns(60); 19471 Alignment.AlignConsecutiveAssignments.Enabled = true; 19472 verifyFormat("using II = typename TI<T, std::tuple<Types...>>::I;\n" 19473 "using I = std::conditional_t<II::value >= 0,\n" 19474 " std::ic<int, II::value + 1>,\n" 19475 " std::ic<int, -1>>;", 19476 Alignment); 19477 verifyFormat("SomeName = Foo;\n" 19478 "X = func<Type, Type>(looooooooooooooooooooooooong,\n" 19479 " arrrrrrrrrrg);", 19480 Alignment); 19481 19482 Alignment.ColumnLimit = 80; 19483 Alignment.SpacesInAngles = FormatStyle::SIAS_Always; 19484 verifyFormat("void **ptr = reinterpret_cast< void ** >(unkn);\n" 19485 "ptr = reinterpret_cast< void ** >(ptr[0]);", 19486 Alignment); 19487 verifyFormat("quint32 *dstimg = reinterpret_cast< quint32 * >(out(i));\n" 19488 "quint32 *dstmask = reinterpret_cast< quint32 * >(outmask(i));", 19489 Alignment); 19490 19491 Alignment.SpacesInParens = FormatStyle::SIPO_Custom; 19492 Alignment.SpacesInParensOptions.InCStyleCasts = true; 19493 verifyFormat("void **ptr = ( void ** )unkn;\n" 19494 "ptr = ( void ** )ptr[0];", 19495 Alignment); 19496 verifyFormat("quint32 *dstimg = ( quint32 * )out.scanLine(i);\n" 19497 "quint32 *dstmask = ( quint32 * )outmask.scanLine(i);", 19498 Alignment); 19499 } 19500 19501 TEST_F(FormatTest, AlignConsecutiveBitFields) { 19502 FormatStyle Alignment = getLLVMStyle(); 19503 Alignment.AlignConsecutiveBitFields.Enabled = true; 19504 verifyFormat("int const a : 5;\n" 19505 "int oneTwoThree : 23;", 19506 Alignment); 19507 19508 // Initializers are allowed starting with c++2a 19509 verifyFormat("int const a : 5 = 1;\n" 19510 "int oneTwoThree : 23 = 0;", 19511 Alignment); 19512 19513 Alignment.AlignConsecutiveDeclarations.Enabled = true; 19514 verifyFormat("int const a : 5;\n" 19515 "int oneTwoThree : 23;", 19516 Alignment); 19517 19518 verifyFormat("int const a : 5; // comment\n" 19519 "int oneTwoThree : 23; // comment", 19520 Alignment); 19521 19522 verifyFormat("int const a : 5 = 1;\n" 19523 "int oneTwoThree : 23 = 0;", 19524 Alignment); 19525 19526 Alignment.AlignConsecutiveAssignments.Enabled = true; 19527 verifyFormat("int const a : 5 = 1;\n" 19528 "int oneTwoThree : 23 = 0;", 19529 Alignment); 19530 verifyFormat("int const a : 5 = {1};\n" 19531 "int oneTwoThree : 23 = 0;", 19532 Alignment); 19533 19534 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 19535 verifyFormat("int const a :5;\n" 19536 "int oneTwoThree:23;", 19537 Alignment); 19538 19539 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 19540 verifyFormat("int const a :5;\n" 19541 "int oneTwoThree :23;", 19542 Alignment); 19543 19544 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 19545 verifyFormat("int const a : 5;\n" 19546 "int oneTwoThree: 23;", 19547 Alignment); 19548 19549 // Known limitations: ':' is only recognized as a bitfield colon when 19550 // followed by a number. 19551 /* 19552 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 19553 "int a : 5;", 19554 Alignment); 19555 */ 19556 } 19557 19558 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 19559 FormatStyle Alignment = getLLVMStyle(); 19560 Alignment.AlignConsecutiveMacros.Enabled = true; 19561 Alignment.PointerAlignment = FormatStyle::PAS_Right; 19562 verifyFormat("float const a = 5;\n" 19563 "int oneTwoThree = 123;", 19564 Alignment); 19565 verifyFormat("int a = 5;\n" 19566 "float const oneTwoThree = 123;", 19567 Alignment); 19568 19569 Alignment.AlignConsecutiveDeclarations.Enabled = true; 19570 verifyFormat("float const a = 5;\n" 19571 "int oneTwoThree = 123;", 19572 Alignment); 19573 verifyFormat("int a = method();\n" 19574 "float const oneTwoThree = 133;", 19575 Alignment); 19576 verifyFormat("int i = 1, j = 10;\n" 19577 "something = 2000;", 19578 Alignment); 19579 verifyFormat("something = 2000;\n" 19580 "int i = 1, j = 10;", 19581 Alignment); 19582 verifyFormat("float something = 2000;\n" 19583 "double another = 911;\n" 19584 "int i = 1, j = 10;\n" 19585 "const int *oneMore = 1;\n" 19586 "unsigned i = 2;", 19587 Alignment); 19588 verifyFormat("float a = 5;\n" 19589 "int one = 1;\n" 19590 "method();\n" 19591 "const double oneTwoThree = 123;\n" 19592 "const unsigned int oneTwo = 12;", 19593 Alignment); 19594 verifyFormat("int oneTwoThree{0}; // comment\n" 19595 "unsigned oneTwo; // comment", 19596 Alignment); 19597 verifyFormat("unsigned int *a;\n" 19598 "int *b;\n" 19599 "unsigned int Const *c;\n" 19600 "unsigned int const *d;\n" 19601 "unsigned int Const &e;\n" 19602 "unsigned int const &f;", 19603 Alignment); 19604 verifyFormat("Const unsigned int *c;\n" 19605 "const unsigned int *d;\n" 19606 "Const unsigned int &e;\n" 19607 "const unsigned int &f;\n" 19608 "const unsigned g;\n" 19609 "Const unsigned h;", 19610 Alignment); 19611 verifyFormat("float const a = 5;\n" 19612 "\n" 19613 "int oneTwoThree = 123;", 19614 "float const a = 5;\n" 19615 "\n" 19616 "int oneTwoThree= 123;", 19617 Alignment); 19618 verifyFormat("float a = 5;\n" 19619 "int one = 1;\n" 19620 "\n" 19621 "unsigned oneTwoThree = 123;", 19622 "float a = 5;\n" 19623 "int one = 1;\n" 19624 "\n" 19625 "unsigned oneTwoThree = 123;", 19626 Alignment); 19627 verifyFormat("float a = 5;\n" 19628 "int one = 1;\n" 19629 "\n" 19630 "unsigned oneTwoThree = 123;\n" 19631 "int oneTwo = 12;", 19632 "float a = 5;\n" 19633 "int one = 1;\n" 19634 "\n" 19635 "unsigned oneTwoThree = 123;\n" 19636 "int oneTwo = 12;", 19637 Alignment); 19638 // Function prototype alignment 19639 verifyFormat("int a();\n" 19640 "double b();", 19641 Alignment); 19642 verifyFormat("int a(int x);\n" 19643 "double b();", 19644 Alignment); 19645 verifyFormat("int a(const Test & = Test());\n" 19646 "int a1(int &foo, const Test & = Test());\n" 19647 "int a2(int &foo, const Test &name = Test());\n" 19648 "double b();", 19649 Alignment); 19650 verifyFormat("struct Test {\n" 19651 " Test(const Test &) = default;\n" 19652 " ~Test() = default;\n" 19653 " Test &operator=(const Test &) = default;\n" 19654 "};", 19655 Alignment); 19656 unsigned OldColumnLimit = Alignment.ColumnLimit; 19657 // We need to set ColumnLimit to zero, in order to stress nested alignments, 19658 // otherwise the function parameters will be re-flowed onto a single line. 19659 Alignment.ColumnLimit = 0; 19660 verifyFormat("int a(int x,\n" 19661 " float y);\n" 19662 "double b(int x,\n" 19663 " double y);", 19664 "int a(int x,\n" 19665 " float y);\n" 19666 "double b(int x,\n" 19667 " double y);", 19668 Alignment); 19669 // This ensures that function parameters of function declarations are 19670 // correctly indented when their owning functions are indented. 19671 // The failure case here is for 'double y' to not be indented enough. 19672 verifyFormat("double a(int x);\n" 19673 "int b(int y,\n" 19674 " double z);", 19675 "double a(int x);\n" 19676 "int b(int y,\n" 19677 " double z);", 19678 Alignment); 19679 // Set ColumnLimit low so that we induce wrapping immediately after 19680 // the function name and opening paren. 19681 Alignment.ColumnLimit = 13; 19682 verifyFormat("int function(\n" 19683 " int x,\n" 19684 " bool y);", 19685 Alignment); 19686 // Set ColumnLimit low so that we break the argument list in multiple lines. 19687 Alignment.ColumnLimit = 35; 19688 verifyFormat("int a3(SomeTypeName1 &x,\n" 19689 " SomeTypeName2 &y,\n" 19690 " const Test & = Test());\n" 19691 "double b();", 19692 Alignment); 19693 Alignment.ColumnLimit = OldColumnLimit; 19694 // Ensure function pointers don't screw up recursive alignment 19695 verifyFormat("int a(int x, void (*fp)(int y));\n" 19696 "double b();", 19697 Alignment); 19698 Alignment.AlignConsecutiveAssignments.Enabled = true; 19699 verifyFormat("struct Test {\n" 19700 " Test(const Test &) = default;\n" 19701 " ~Test() = default;\n" 19702 " Test &operator=(const Test &) = default;\n" 19703 "};", 19704 Alignment); 19705 // Ensure recursive alignment is broken by function braces, so that the 19706 // "a = 1" does not align with subsequent assignments inside the function 19707 // body. 19708 verifyFormat("int func(int a = 1) {\n" 19709 " int b = 2;\n" 19710 " int cc = 3;\n" 19711 "}", 19712 Alignment); 19713 verifyFormat("float something = 2000;\n" 19714 "double another = 911;\n" 19715 "int i = 1, j = 10;\n" 19716 "const int *oneMore = 1;\n" 19717 "unsigned i = 2;", 19718 Alignment); 19719 verifyFormat("int oneTwoThree = {0}; // comment\n" 19720 "unsigned oneTwo = 0; // comment", 19721 Alignment); 19722 // Make sure that scope is correctly tracked, in the absence of braces 19723 verifyFormat("for (int i = 0; i < n; i++)\n" 19724 " j = i;\n" 19725 "double x = 1;", 19726 Alignment); 19727 verifyFormat("if (int i = 0)\n" 19728 " j = i;\n" 19729 "double x = 1;", 19730 Alignment); 19731 // Ensure operator[] and operator() are comprehended 19732 verifyFormat("struct test {\n" 19733 " long long int foo();\n" 19734 " int operator[](int a);\n" 19735 " double bar();\n" 19736 "};", 19737 Alignment); 19738 verifyFormat("struct test {\n" 19739 " long long int foo();\n" 19740 " int operator()(int a);\n" 19741 " double bar();\n" 19742 "};", 19743 Alignment); 19744 // http://llvm.org/PR52914 19745 verifyFormat("char *a[] = {\"a\", // comment\n" 19746 " \"bb\"};\n" 19747 "int bbbbbbb = 0;", 19748 Alignment); 19749 // http://llvm.org/PR68079 19750 verifyFormat("using Fn = int (A::*)();\n" 19751 "using RFn = int (A::*)() &;\n" 19752 "using RRFn = int (A::*)() &&;", 19753 Alignment); 19754 verifyFormat("using Fn = int (A::*)();\n" 19755 "using RFn = int *(A::*)() &;\n" 19756 "using RRFn = double (A::*)() &&;", 19757 Alignment); 19758 19759 // PAS_Right 19760 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19761 " int const i = 1;\n" 19762 " int *j = 2;\n" 19763 " int big = 10000;\n" 19764 "\n" 19765 " unsigned oneTwoThree = 123;\n" 19766 " int oneTwo = 12;\n" 19767 " method();\n" 19768 " float k = 2;\n" 19769 " int ll = 10000;\n" 19770 "}", 19771 "void SomeFunction(int parameter= 0) {\n" 19772 " int const i= 1;\n" 19773 " int *j=2;\n" 19774 " int big = 10000;\n" 19775 "\n" 19776 "unsigned oneTwoThree =123;\n" 19777 "int oneTwo = 12;\n" 19778 " method();\n" 19779 "float k= 2;\n" 19780 "int ll=10000;\n" 19781 "}", 19782 Alignment); 19783 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19784 " int const i = 1;\n" 19785 " int **j = 2, ***k;\n" 19786 " int &k = i;\n" 19787 " int &&l = i + j;\n" 19788 " int big = 10000;\n" 19789 "\n" 19790 " unsigned oneTwoThree = 123;\n" 19791 " int oneTwo = 12;\n" 19792 " method();\n" 19793 " float k = 2;\n" 19794 " int ll = 10000;\n" 19795 "}", 19796 "void SomeFunction(int parameter= 0) {\n" 19797 " int const i= 1;\n" 19798 " int **j=2,***k;\n" 19799 "int &k=i;\n" 19800 "int &&l=i+j;\n" 19801 " int big = 10000;\n" 19802 "\n" 19803 "unsigned oneTwoThree =123;\n" 19804 "int oneTwo = 12;\n" 19805 " method();\n" 19806 "float k= 2;\n" 19807 "int ll=10000;\n" 19808 "}", 19809 Alignment); 19810 // variables are aligned at their name, pointers are at the right most 19811 // position 19812 verifyFormat("int *a;\n" 19813 "int **b;\n" 19814 "int ***c;\n" 19815 "int foobar;", 19816 Alignment); 19817 19818 // PAS_Left 19819 FormatStyle AlignmentLeft = Alignment; 19820 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 19821 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19822 " int const i = 1;\n" 19823 " int* j = 2;\n" 19824 " int big = 10000;\n" 19825 "\n" 19826 " unsigned oneTwoThree = 123;\n" 19827 " int oneTwo = 12;\n" 19828 " method();\n" 19829 " float k = 2;\n" 19830 " int ll = 10000;\n" 19831 "}", 19832 "void SomeFunction(int parameter= 0) {\n" 19833 " int const i= 1;\n" 19834 " int *j=2;\n" 19835 " int big = 10000;\n" 19836 "\n" 19837 "unsigned oneTwoThree =123;\n" 19838 "int oneTwo = 12;\n" 19839 " method();\n" 19840 "float k= 2;\n" 19841 "int ll=10000;\n" 19842 "}", 19843 AlignmentLeft); 19844 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19845 " int const i = 1;\n" 19846 " int** j = 2;\n" 19847 " int& k = i;\n" 19848 " int&& l = i + j;\n" 19849 " int big = 10000;\n" 19850 "\n" 19851 " unsigned oneTwoThree = 123;\n" 19852 " int oneTwo = 12;\n" 19853 " method();\n" 19854 " float k = 2;\n" 19855 " int ll = 10000;\n" 19856 "}", 19857 "void SomeFunction(int parameter= 0) {\n" 19858 " int const i= 1;\n" 19859 " int **j=2;\n" 19860 "int &k=i;\n" 19861 "int &&l=i+j;\n" 19862 " int big = 10000;\n" 19863 "\n" 19864 "unsigned oneTwoThree =123;\n" 19865 "int oneTwo = 12;\n" 19866 " method();\n" 19867 "float k= 2;\n" 19868 "int ll=10000;\n" 19869 "}", 19870 AlignmentLeft); 19871 // variables are aligned at their name, pointers are at the left most position 19872 verifyFormat("int* a;\n" 19873 "int** b;\n" 19874 "int*** c;\n" 19875 "int foobar;", 19876 AlignmentLeft); 19877 19878 verifyFormat("int a(SomeType& foo, const Test& = Test());\n" 19879 "double b();", 19880 AlignmentLeft); 19881 19882 // PAS_Middle 19883 FormatStyle AlignmentMiddle = Alignment; 19884 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 19885 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19886 " int const i = 1;\n" 19887 " int * j = 2;\n" 19888 " int big = 10000;\n" 19889 "\n" 19890 " unsigned oneTwoThree = 123;\n" 19891 " int oneTwo = 12;\n" 19892 " method();\n" 19893 " float k = 2;\n" 19894 " int ll = 10000;\n" 19895 "}", 19896 "void SomeFunction(int parameter= 0) {\n" 19897 " int const i= 1;\n" 19898 " int *j=2;\n" 19899 " int big = 10000;\n" 19900 "\n" 19901 "unsigned oneTwoThree =123;\n" 19902 "int oneTwo = 12;\n" 19903 " method();\n" 19904 "float k= 2;\n" 19905 "int ll=10000;\n" 19906 "}", 19907 AlignmentMiddle); 19908 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19909 " int const i = 1;\n" 19910 " int ** j = 2, ***k;\n" 19911 " int & k = i;\n" 19912 " int && l = i + j;\n" 19913 " int big = 10000;\n" 19914 "\n" 19915 " unsigned oneTwoThree = 123;\n" 19916 " int oneTwo = 12;\n" 19917 " method();\n" 19918 " float k = 2;\n" 19919 " int ll = 10000;\n" 19920 "}", 19921 "void SomeFunction(int parameter= 0) {\n" 19922 " int const i= 1;\n" 19923 " int **j=2,***k;\n" 19924 "int &k=i;\n" 19925 "int &&l=i+j;\n" 19926 " int big = 10000;\n" 19927 "\n" 19928 "unsigned oneTwoThree =123;\n" 19929 "int oneTwo = 12;\n" 19930 " method();\n" 19931 "float k= 2;\n" 19932 "int ll=10000;\n" 19933 "}", 19934 AlignmentMiddle); 19935 // variables are aligned at their name, pointers are in the middle 19936 verifyFormat("int * a;\n" 19937 "int * b;\n" 19938 "int *** c;\n" 19939 "int foobar;", 19940 AlignmentMiddle); 19941 19942 verifyFormat("int a(SomeType & foo, const Test & = Test());\n" 19943 "double b();", 19944 AlignmentMiddle); 19945 19946 Alignment.AlignConsecutiveAssignments.Enabled = false; 19947 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 19948 verifyFormat("#define A \\\n" 19949 " int aaaa = 12; \\\n" 19950 " float b = 23; \\\n" 19951 " const int ccc = 234; \\\n" 19952 " unsigned dddddddddd = 2345;", 19953 Alignment); 19954 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 19955 verifyFormat("#define A \\\n" 19956 " int aaaa = 12; \\\n" 19957 " float b = 23; \\\n" 19958 " const int ccc = 234; \\\n" 19959 " unsigned dddddddddd = 2345;", 19960 Alignment); 19961 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 19962 Alignment.ColumnLimit = 30; 19963 verifyFormat("#define A \\\n" 19964 " int aaaa = 12; \\\n" 19965 " float b = 23; \\\n" 19966 " const int ccc = 234; \\\n" 19967 " int dddddddddd = 2345;", 19968 Alignment); 19969 Alignment.ColumnLimit = 80; 19970 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 19971 "k = 4, int l = 5,\n" 19972 " int m = 6) {\n" 19973 " const int j = 10;\n" 19974 " otherThing = 1;\n" 19975 "}", 19976 Alignment); 19977 verifyFormat("void SomeFunction(int parameter = 0) {\n" 19978 " int const i = 1;\n" 19979 " int *j = 2;\n" 19980 " int big = 10000;\n" 19981 "}", 19982 Alignment); 19983 verifyFormat("class C {\n" 19984 "public:\n" 19985 " int i = 1;\n" 19986 " virtual void f() = 0;\n" 19987 "};", 19988 Alignment); 19989 verifyFormat("float i = 1;\n" 19990 "if (SomeType t = getSomething()) {\n" 19991 "}\n" 19992 "const unsigned j = 2;\n" 19993 "int big = 10000;", 19994 Alignment); 19995 verifyFormat("float j = 7;\n" 19996 "for (int k = 0; k < N; ++k) {\n" 19997 "}\n" 19998 "unsigned j = 2;\n" 19999 "int big = 10000;\n" 20000 "}", 20001 Alignment); 20002 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 20003 verifyFormat("float i = 1;\n" 20004 "LooooooooooongType loooooooooooooooooooooongVariable\n" 20005 " = someLooooooooooooooooongFunction();\n" 20006 "int j = 2;", 20007 Alignment); 20008 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 20009 verifyFormat("int i = 1;\n" 20010 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 20011 " someLooooooooooooooooongFunction();\n" 20012 "int j = 2;", 20013 Alignment); 20014 20015 Alignment.AlignConsecutiveAssignments.Enabled = true; 20016 verifyFormat("auto lambda = []() {\n" 20017 " auto ii = 0;\n" 20018 " float j = 0;\n" 20019 " return 0;\n" 20020 "};\n" 20021 "int i = 0;\n" 20022 "float i2 = 0;\n" 20023 "auto v = type{\n" 20024 " i = 1, //\n" 20025 " (i = 2), //\n" 20026 " i = 3 //\n" 20027 "};", 20028 Alignment); 20029 Alignment.AlignConsecutiveAssignments.Enabled = false; 20030 20031 verifyFormat( 20032 "int i = 1;\n" 20033 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 20034 " loooooooooooooooooooooongParameterB);\n" 20035 "int j = 2;", 20036 Alignment); 20037 20038 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 20039 // We expect declarations and assignments to align, as long as it doesn't 20040 // exceed the column limit, starting a new alignment sequence whenever it 20041 // happens. 20042 Alignment.AlignConsecutiveAssignments.Enabled = true; 20043 Alignment.ColumnLimit = 30; 20044 verifyFormat("float ii = 1;\n" 20045 "unsigned j = 2;\n" 20046 "int someVerylongVariable = 1;\n" 20047 "AnotherLongType ll = 123456;\n" 20048 "VeryVeryLongType k = 2;\n" 20049 "int myvar = 1;", 20050 Alignment); 20051 Alignment.ColumnLimit = 80; 20052 Alignment.AlignConsecutiveAssignments.Enabled = false; 20053 20054 verifyFormat( 20055 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 20056 " typename LongType, typename B>\n" 20057 "auto foo() {}", 20058 Alignment); 20059 verifyFormat("float a, b = 1;\n" 20060 "int c = 2;\n" 20061 "int dd = 3;", 20062 Alignment); 20063 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 20064 "float b[1][] = {{3.f}};", 20065 Alignment); 20066 Alignment.AlignConsecutiveAssignments.Enabled = true; 20067 verifyFormat("float a, b = 1;\n" 20068 "int c = 2;\n" 20069 "int dd = 3;", 20070 Alignment); 20071 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 20072 "float b[1][] = {{3.f}};", 20073 Alignment); 20074 Alignment.AlignConsecutiveAssignments.Enabled = false; 20075 20076 Alignment.ColumnLimit = 30; 20077 Alignment.BinPackParameters = FormatStyle::BPPS_OnePerLine; 20078 verifyFormat("void foo(float a,\n" 20079 " float b,\n" 20080 " int c,\n" 20081 " uint32_t *d) {\n" 20082 " int *e = 0;\n" 20083 " float f = 0;\n" 20084 " double g = 0;\n" 20085 "}\n" 20086 "void bar(ino_t a,\n" 20087 " int b,\n" 20088 " uint32_t *c,\n" 20089 " bool d) {}", 20090 Alignment); 20091 Alignment.BinPackParameters = FormatStyle::BPPS_BinPack; 20092 Alignment.ColumnLimit = 80; 20093 20094 // Bug 33507 20095 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 20096 verifyFormat( 20097 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 20098 " static const Version verVs2017;\n" 20099 " return true;\n" 20100 "});", 20101 Alignment); 20102 Alignment.PointerAlignment = FormatStyle::PAS_Right; 20103 20104 // See llvm.org/PR35641 20105 Alignment.AlignConsecutiveDeclarations.Enabled = true; 20106 verifyFormat("int func() { //\n" 20107 " int b;\n" 20108 " unsigned c;\n" 20109 "}", 20110 Alignment); 20111 20112 // See PR37175 20113 FormatStyle Style = getMozillaStyle(); 20114 Style.AlignConsecutiveDeclarations.Enabled = true; 20115 verifyFormat("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 20116 "foo(int a);", 20117 "DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style); 20118 20119 Alignment.PointerAlignment = FormatStyle::PAS_Left; 20120 verifyFormat("unsigned int* a;\n" 20121 "int* b;\n" 20122 "unsigned int Const* c;\n" 20123 "unsigned int const* d;\n" 20124 "unsigned int Const& e;\n" 20125 "unsigned int const& f;", 20126 Alignment); 20127 verifyFormat("Const unsigned int* c;\n" 20128 "const unsigned int* d;\n" 20129 "Const unsigned int& e;\n" 20130 "const unsigned int& f;\n" 20131 "const unsigned g;\n" 20132 "Const unsigned h;", 20133 Alignment); 20134 20135 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 20136 verifyFormat("unsigned int * a;\n" 20137 "int * b;\n" 20138 "unsigned int Const * c;\n" 20139 "unsigned int const * d;\n" 20140 "unsigned int Const & e;\n" 20141 "unsigned int const & f;", 20142 Alignment); 20143 verifyFormat("Const unsigned int * c;\n" 20144 "const unsigned int * d;\n" 20145 "Const unsigned int & e;\n" 20146 "const unsigned int & f;\n" 20147 "const unsigned g;\n" 20148 "Const unsigned h;", 20149 Alignment); 20150 20151 // See PR46529 20152 FormatStyle BracedAlign = getLLVMStyle(); 20153 BracedAlign.AlignConsecutiveDeclarations.Enabled = true; 20154 verifyFormat("const auto result{[]() {\n" 20155 " const auto something = 1;\n" 20156 " return 2;\n" 20157 "}};", 20158 BracedAlign); 20159 verifyFormat("int foo{[]() {\n" 20160 " int bar{0};\n" 20161 " return 0;\n" 20162 "}()};", 20163 BracedAlign); 20164 BracedAlign.Cpp11BracedListStyle = false; 20165 verifyFormat("const auto result{ []() {\n" 20166 " const auto something = 1;\n" 20167 " return 2;\n" 20168 "} };", 20169 BracedAlign); 20170 verifyFormat("int foo{ []() {\n" 20171 " int bar{ 0 };\n" 20172 " return 0;\n" 20173 "}() };", 20174 BracedAlign); 20175 20176 Alignment.AlignConsecutiveDeclarations.AlignFunctionDeclarations = false; 20177 verifyFormat("unsigned int f1(void);\n" 20178 "void f2(void);\n" 20179 "size_t f3(void);", 20180 Alignment); 20181 } 20182 20183 TEST_F(FormatTest, AlignConsecutiveShortCaseStatements) { 20184 FormatStyle Alignment = getLLVMStyle(); 20185 Alignment.AllowShortCaseLabelsOnASingleLine = true; 20186 Alignment.AlignConsecutiveShortCaseStatements.Enabled = true; 20187 20188 verifyFormat("switch (level) {\n" 20189 "case log::info: return \"info\";\n" 20190 "case log::warning: return \"warning\";\n" 20191 "default: return \"default\";\n" 20192 "}", 20193 Alignment); 20194 20195 verifyFormat("switch (level) {\n" 20196 "case log::info: return \"info\";\n" 20197 "case log::warning: return \"warning\";\n" 20198 "}", 20199 "switch (level) {\n" 20200 "case log::info: return \"info\";\n" 20201 "case log::warning:\n" 20202 " return \"warning\";\n" 20203 "}", 20204 Alignment); 20205 20206 // Empty case statements push out the alignment, but non-short case labels 20207 // don't. 20208 verifyFormat("switch (level) {\n" 20209 "case log::info: return \"info\";\n" 20210 "case log::critical:\n" 20211 "case log::warning:\n" 20212 "case log::severe: return \"severe\";\n" 20213 "case log::extra_severe:\n" 20214 " // comment\n" 20215 " return \"extra_severe\";\n" 20216 "}", 20217 Alignment); 20218 20219 // Verify comments and empty lines break the alignment. 20220 verifyNoChange("switch (level) {\n" 20221 "case log::info: return \"info\";\n" 20222 "case log::warning: return \"warning\";\n" 20223 "// comment\n" 20224 "case log::critical: return \"critical\";\n" 20225 "default: return \"default\";\n" 20226 "\n" 20227 "case log::severe: return \"severe\";\n" 20228 "}", 20229 Alignment); 20230 20231 // Empty case statements don't break the alignment, and potentially push it 20232 // out. 20233 verifyFormat("switch (level) {\n" 20234 "case log::info: return \"info\";\n" 20235 "case log::warning:\n" 20236 "case log::critical:\n" 20237 "default: return \"default\";\n" 20238 "}", 20239 Alignment); 20240 20241 // Implicit fallthrough cases can be aligned with either a comment or 20242 // [[fallthrough]] 20243 verifyFormat("switch (level) {\n" 20244 "case log::info: return \"info\";\n" 20245 "case log::warning: // fallthrough\n" 20246 "case log::error: return \"error\";\n" 20247 "case log::critical: /*fallthrough*/\n" 20248 "case log::severe: return \"severe\";\n" 20249 "case log::diag: [[fallthrough]];\n" 20250 "default: return \"default\";\n" 20251 "}", 20252 Alignment); 20253 20254 // Verify trailing comment that needs a reflow also gets aligned properly. 20255 verifyFormat("switch (level) {\n" 20256 "case log::info: return \"info\";\n" 20257 "case log::warning: // fallthrough\n" 20258 "case log::error: return \"error\";\n" 20259 "}", 20260 "switch (level) {\n" 20261 "case log::info: return \"info\";\n" 20262 "case log::warning: //fallthrough\n" 20263 "case log::error: return \"error\";\n" 20264 "}", 20265 Alignment); 20266 20267 // Verify adjacent non-short case statements don't change the alignment, and 20268 // properly break the set of consecutive statements. 20269 verifyFormat("switch (level) {\n" 20270 "case log::critical:\n" 20271 " // comment\n" 20272 " return \"critical\";\n" 20273 "case log::info: return \"info\";\n" 20274 "case log::warning: return \"warning\";\n" 20275 "default:\n" 20276 " // comment\n" 20277 " return \"\";\n" 20278 "case log::error: return \"error\";\n" 20279 "case log::severe: return \"severe\";\n" 20280 "case log::extra_critical:\n" 20281 " // comment\n" 20282 " return \"extra critical\";\n" 20283 "}", 20284 Alignment); 20285 20286 Alignment.SpaceBeforeCaseColon = true; 20287 verifyFormat("switch (level) {\n" 20288 "case log::info : return \"info\";\n" 20289 "case log::warning : return \"warning\";\n" 20290 "default : return \"default\";\n" 20291 "}", 20292 Alignment); 20293 Alignment.SpaceBeforeCaseColon = false; 20294 20295 // Make sure we don't incorrectly align correctly across nested switch cases. 20296 verifyFormat("switch (level) {\n" 20297 "case log::info: return \"info\";\n" 20298 "case log::warning: return \"warning\";\n" 20299 "case log::other:\n" 20300 " switch (sublevel) {\n" 20301 " case log::info: return \"info\";\n" 20302 " case log::warning: return \"warning\";\n" 20303 " }\n" 20304 " break;\n" 20305 "case log::error: return \"error\";\n" 20306 "default: return \"default\";\n" 20307 "}", 20308 "switch (level) {\n" 20309 "case log::info: return \"info\";\n" 20310 "case log::warning: return \"warning\";\n" 20311 "case log::other: switch (sublevel) {\n" 20312 " case log::info: return \"info\";\n" 20313 " case log::warning: return \"warning\";\n" 20314 "}\n" 20315 "break;\n" 20316 "case log::error: return \"error\";\n" 20317 "default: return \"default\";\n" 20318 "}", 20319 Alignment); 20320 20321 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = true; 20322 20323 verifyFormat("switch (level) {\n" 20324 "case log::info: return \"info\";\n" 20325 "\n" 20326 "case log::warning: return \"warning\";\n" 20327 "}", 20328 "switch (level) {\n" 20329 "case log::info: return \"info\";\n" 20330 "\n" 20331 "case log::warning: return \"warning\";\n" 20332 "}", 20333 Alignment); 20334 20335 Alignment.AlignConsecutiveShortCaseStatements.AcrossComments = true; 20336 20337 verifyNoChange("switch (level) {\n" 20338 "case log::info: return \"info\";\n" 20339 "\n" 20340 "/* block comment */\n" 20341 "\n" 20342 "// line comment\n" 20343 "case log::warning: return \"warning\";\n" 20344 "}", 20345 Alignment); 20346 20347 Alignment.AlignConsecutiveShortCaseStatements.AcrossEmptyLines = false; 20348 20349 verifyFormat("switch (level) {\n" 20350 "case log::info: return \"info\";\n" 20351 "//\n" 20352 "case log::warning: return \"warning\";\n" 20353 "}", 20354 Alignment); 20355 20356 Alignment.AlignConsecutiveShortCaseStatements.AlignCaseColons = true; 20357 20358 verifyFormat("switch (level) {\n" 20359 "case log::info : return \"info\";\n" 20360 "case log::warning: return \"warning\";\n" 20361 "default : return \"default\";\n" 20362 "}", 20363 Alignment); 20364 20365 // With AlignCaseColons, empty case statements don't break alignment of 20366 // consecutive case statements (and are aligned). 20367 verifyFormat("switch (level) {\n" 20368 "case log::info : return \"info\";\n" 20369 "case log::warning :\n" 20370 "case log::critical:\n" 20371 "default : return \"default\";\n" 20372 "}", 20373 Alignment); 20374 20375 // Final non-short case labels shouldn't have their colon aligned 20376 verifyFormat("switch (level) {\n" 20377 "case log::info : return \"info\";\n" 20378 "case log::warning :\n" 20379 "case log::critical:\n" 20380 "case log::severe : return \"severe\";\n" 20381 "default:\n" 20382 " // comment\n" 20383 " return \"default\";\n" 20384 "}", 20385 Alignment); 20386 20387 // Verify adjacent non-short case statements break the set of consecutive 20388 // alignments and aren't aligned with adjacent non-short case statements if 20389 // AlignCaseColons is set. 20390 verifyFormat("switch (level) {\n" 20391 "case log::critical:\n" 20392 " // comment\n" 20393 " return \"critical\";\n" 20394 "case log::info : return \"info\";\n" 20395 "case log::warning: return \"warning\";\n" 20396 "default:\n" 20397 " // comment\n" 20398 " return \"\";\n" 20399 "case log::error : return \"error\";\n" 20400 "case log::severe: return \"severe\";\n" 20401 "case log::extra_critical:\n" 20402 " // comment\n" 20403 " return \"extra critical\";\n" 20404 "}", 20405 Alignment); 20406 20407 Alignment.SpaceBeforeCaseColon = true; 20408 verifyFormat("switch (level) {\n" 20409 "case log::info : return \"info\";\n" 20410 "case log::warning : return \"warning\";\n" 20411 "case log::error :\n" 20412 "default : return \"default\";\n" 20413 "}", 20414 Alignment); 20415 } 20416 20417 TEST_F(FormatTest, AlignWithLineBreaks) { 20418 auto Style = getLLVMStyleWithColumns(120); 20419 20420 EXPECT_EQ(Style.AlignConsecutiveAssignments, 20421 FormatStyle::AlignConsecutiveStyle( 20422 {/*Enabled=*/false, /*AcrossEmptyLines=*/false, 20423 /*AcrossComments=*/false, /*AlignCompound=*/false, 20424 /*AlignFunctionDeclarations=*/false, 20425 /*AlignFunctionPointers=*/false, 20426 /*PadOperators=*/true})); 20427 EXPECT_EQ(Style.AlignConsecutiveDeclarations, 20428 FormatStyle::AlignConsecutiveStyle( 20429 {/*Enabled=*/false, /*AcrossEmptyLines=*/false, 20430 /*AcrossComments=*/false, /*AlignCompound=*/false, 20431 /*AlignFunctionDeclarations=*/true, 20432 /*AlignFunctionPointers=*/false, 20433 /*PadOperators=*/false})); 20434 verifyFormat("void foo() {\n" 20435 " int myVar = 5;\n" 20436 " double x = 3.14;\n" 20437 " auto str = \"Hello \"\n" 20438 " \"World\";\n" 20439 " auto s = \"Hello \"\n" 20440 " \"Again\";\n" 20441 "}", 20442 Style); 20443 20444 // clang-format off 20445 verifyFormat("void foo() {\n" 20446 " const int capacityBefore = Entries.capacity();\n" 20447 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20448 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20449 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20450 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20451 "}", 20452 Style); 20453 // clang-format on 20454 20455 Style.AlignConsecutiveAssignments.Enabled = true; 20456 verifyFormat("void foo() {\n" 20457 " int myVar = 5;\n" 20458 " double x = 3.14;\n" 20459 " auto str = \"Hello \"\n" 20460 " \"World\";\n" 20461 " auto s = \"Hello \"\n" 20462 " \"Again\";\n" 20463 "}", 20464 Style); 20465 20466 // clang-format off 20467 verifyFormat("void foo() {\n" 20468 " const int capacityBefore = Entries.capacity();\n" 20469 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20470 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20471 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20472 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20473 "}", 20474 Style); 20475 // clang-format on 20476 20477 Style.AlignConsecutiveAssignments.Enabled = false; 20478 Style.AlignConsecutiveDeclarations.Enabled = true; 20479 verifyFormat("void foo() {\n" 20480 " int myVar = 5;\n" 20481 " double x = 3.14;\n" 20482 " auto str = \"Hello \"\n" 20483 " \"World\";\n" 20484 " auto s = \"Hello \"\n" 20485 " \"Again\";\n" 20486 "}", 20487 Style); 20488 20489 // clang-format off 20490 verifyFormat("void foo() {\n" 20491 " const int capacityBefore = Entries.capacity();\n" 20492 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20493 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20494 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20495 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20496 "}", 20497 Style); 20498 // clang-format on 20499 20500 Style.AlignConsecutiveAssignments.Enabled = true; 20501 Style.AlignConsecutiveDeclarations.Enabled = true; 20502 20503 verifyFormat("void foo() {\n" 20504 " int myVar = 5;\n" 20505 " double x = 3.14;\n" 20506 " auto str = \"Hello \"\n" 20507 " \"World\";\n" 20508 " auto s = \"Hello \"\n" 20509 " \"Again\";\n" 20510 "}", 20511 Style); 20512 20513 // clang-format off 20514 verifyFormat("void foo() {\n" 20515 " const int capacityBefore = Entries.capacity();\n" 20516 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20517 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20518 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 20519 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 20520 "}", 20521 Style); 20522 // clang-format on 20523 20524 Style = getLLVMStyleWithColumns(20); 20525 Style.AlignConsecutiveAssignments.Enabled = true; 20526 Style.IndentWidth = 4; 20527 20528 verifyFormat("void foo() {\n" 20529 " int i1 = 1;\n" 20530 " int j = 0;\n" 20531 " int k = bar(\n" 20532 " argument1,\n" 20533 " argument2);\n" 20534 "}", 20535 Style); 20536 20537 verifyFormat("unsigned i = 0;\n" 20538 "int a[] = {\n" 20539 " 1234567890,\n" 20540 " -1234567890};", 20541 Style); 20542 20543 Style.ColumnLimit = 120; 20544 20545 // clang-format off 20546 verifyFormat("void SomeFunc() {\n" 20547 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 20548 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 20549 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 20550 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 20551 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 20552 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 20553 "}", 20554 Style); 20555 // clang-format on 20556 20557 Style.BinPackArguments = false; 20558 20559 // clang-format off 20560 verifyFormat("void SomeFunc() {\n" 20561 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 20562 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 20563 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 20564 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 20565 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 20566 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 20567 "}", 20568 Style); 20569 // clang-format on 20570 } 20571 20572 TEST_F(FormatTest, AlignWithInitializerPeriods) { 20573 auto Style = getLLVMStyleWithColumns(60); 20574 20575 verifyFormat("void foo1(void) {\n" 20576 " BYTE p[1] = 1;\n" 20577 " A B = {.one_foooooooooooooooo = 2,\n" 20578 " .two_fooooooooooooo = 3,\n" 20579 " .three_fooooooooooooo = 4};\n" 20580 " BYTE payload = 2;\n" 20581 "}", 20582 Style); 20583 20584 Style.AlignConsecutiveAssignments.Enabled = true; 20585 Style.AlignConsecutiveDeclarations.Enabled = false; 20586 verifyFormat("void foo2(void) {\n" 20587 " BYTE p[1] = 1;\n" 20588 " A B = {.one_foooooooooooooooo = 2,\n" 20589 " .two_fooooooooooooo = 3,\n" 20590 " .three_fooooooooooooo = 4};\n" 20591 " BYTE payload = 2;\n" 20592 "}", 20593 Style); 20594 20595 Style.AlignConsecutiveAssignments.Enabled = false; 20596 Style.AlignConsecutiveDeclarations.Enabled = true; 20597 verifyFormat("void foo3(void) {\n" 20598 " BYTE p[1] = 1;\n" 20599 " A B = {.one_foooooooooooooooo = 2,\n" 20600 " .two_fooooooooooooo = 3,\n" 20601 " .three_fooooooooooooo = 4};\n" 20602 " BYTE payload = 2;\n" 20603 "}", 20604 Style); 20605 20606 Style.AlignConsecutiveAssignments.Enabled = true; 20607 Style.AlignConsecutiveDeclarations.Enabled = true; 20608 verifyFormat("void foo4(void) {\n" 20609 " BYTE p[1] = 1;\n" 20610 " A B = {.one_foooooooooooooooo = 2,\n" 20611 " .two_fooooooooooooo = 3,\n" 20612 " .three_fooooooooooooo = 4};\n" 20613 " BYTE payload = 2;\n" 20614 "}", 20615 Style); 20616 } 20617 20618 TEST_F(FormatTest, LinuxBraceBreaking) { 20619 FormatStyle LinuxBraceStyle = getLLVMStyle(); 20620 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 20621 verifyFormat("namespace a\n" 20622 "{\n" 20623 "class A\n" 20624 "{\n" 20625 " void f()\n" 20626 " {\n" 20627 " if (true) {\n" 20628 " a();\n" 20629 " b();\n" 20630 " } else {\n" 20631 " a();\n" 20632 " }\n" 20633 " }\n" 20634 " void g() { return; }\n" 20635 "};\n" 20636 "struct B {\n" 20637 " int x;\n" 20638 "};\n" 20639 "} // namespace a", 20640 LinuxBraceStyle); 20641 verifyFormat("enum X {\n" 20642 " Y = 0,\n" 20643 "}", 20644 LinuxBraceStyle); 20645 verifyFormat("struct S {\n" 20646 " int Type;\n" 20647 " union {\n" 20648 " int x;\n" 20649 " double y;\n" 20650 " } Value;\n" 20651 " class C\n" 20652 " {\n" 20653 " MyFavoriteType Value;\n" 20654 " } Class;\n" 20655 "}", 20656 LinuxBraceStyle); 20657 } 20658 20659 TEST_F(FormatTest, MozillaBraceBreaking) { 20660 FormatStyle MozillaBraceStyle = getLLVMStyle(); 20661 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 20662 MozillaBraceStyle.FixNamespaceComments = false; 20663 verifyFormat("namespace a {\n" 20664 "class A\n" 20665 "{\n" 20666 " void f()\n" 20667 " {\n" 20668 " if (true) {\n" 20669 " a();\n" 20670 " b();\n" 20671 " }\n" 20672 " }\n" 20673 " void g() { return; }\n" 20674 "};\n" 20675 "enum E\n" 20676 "{\n" 20677 " A,\n" 20678 " // foo\n" 20679 " B,\n" 20680 " C\n" 20681 "};\n" 20682 "struct B\n" 20683 "{\n" 20684 " int x;\n" 20685 "};\n" 20686 "}", 20687 MozillaBraceStyle); 20688 verifyFormat("struct S\n" 20689 "{\n" 20690 " int Type;\n" 20691 " union\n" 20692 " {\n" 20693 " int x;\n" 20694 " double y;\n" 20695 " } Value;\n" 20696 " class C\n" 20697 " {\n" 20698 " MyFavoriteType Value;\n" 20699 " } Class;\n" 20700 "}", 20701 MozillaBraceStyle); 20702 } 20703 20704 TEST_F(FormatTest, StroustrupBraceBreaking) { 20705 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 20706 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 20707 verifyFormat("namespace a {\n" 20708 "class A {\n" 20709 " void f()\n" 20710 " {\n" 20711 " if (true) {\n" 20712 " a();\n" 20713 " b();\n" 20714 " }\n" 20715 " }\n" 20716 " void g() { return; }\n" 20717 "};\n" 20718 "struct B {\n" 20719 " int x;\n" 20720 "};\n" 20721 "} // namespace a", 20722 StroustrupBraceStyle); 20723 20724 verifyFormat("void foo()\n" 20725 "{\n" 20726 " if (a) {\n" 20727 " a();\n" 20728 " }\n" 20729 " else {\n" 20730 " b();\n" 20731 " }\n" 20732 "}", 20733 StroustrupBraceStyle); 20734 20735 verifyFormat("#ifdef _DEBUG\n" 20736 "int foo(int i = 0)\n" 20737 "#else\n" 20738 "int foo(int i = 5)\n" 20739 "#endif\n" 20740 "{\n" 20741 " return i;\n" 20742 "}", 20743 StroustrupBraceStyle); 20744 20745 verifyFormat("void foo() {}\n" 20746 "void bar()\n" 20747 "#ifdef _DEBUG\n" 20748 "{\n" 20749 " foo();\n" 20750 "}\n" 20751 "#else\n" 20752 "{\n" 20753 "}\n" 20754 "#endif", 20755 StroustrupBraceStyle); 20756 20757 verifyFormat("void foobar() { int i = 5; }\n" 20758 "#ifdef _DEBUG\n" 20759 "void bar() {}\n" 20760 "#else\n" 20761 "void bar() { foobar(); }\n" 20762 "#endif", 20763 StroustrupBraceStyle); 20764 } 20765 20766 TEST_F(FormatTest, AllmanBraceBreaking) { 20767 FormatStyle AllmanBraceStyle = getLLVMStyle(); 20768 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 20769 20770 verifyFormat("namespace a\n" 20771 "{\n" 20772 "void f();\n" 20773 "void g();\n" 20774 "} // namespace a", 20775 "namespace a\n" 20776 "{\n" 20777 "void f();\n" 20778 "void g();\n" 20779 "}", 20780 AllmanBraceStyle); 20781 20782 verifyFormat("namespace a\n" 20783 "{\n" 20784 "class A\n" 20785 "{\n" 20786 " void f()\n" 20787 " {\n" 20788 " if (true)\n" 20789 " {\n" 20790 " a();\n" 20791 " b();\n" 20792 " }\n" 20793 " }\n" 20794 " void g() { return; }\n" 20795 "};\n" 20796 "struct B\n" 20797 "{\n" 20798 " int x;\n" 20799 "};\n" 20800 "union C\n" 20801 "{\n" 20802 "};\n" 20803 "} // namespace a", 20804 AllmanBraceStyle); 20805 20806 verifyFormat("void f()\n" 20807 "{\n" 20808 " if (true)\n" 20809 " {\n" 20810 " a();\n" 20811 " }\n" 20812 " else if (false)\n" 20813 " {\n" 20814 " b();\n" 20815 " }\n" 20816 " else\n" 20817 " {\n" 20818 " c();\n" 20819 " }\n" 20820 "}", 20821 AllmanBraceStyle); 20822 20823 verifyFormat("void f()\n" 20824 "{\n" 20825 " for (int i = 0; i < 10; ++i)\n" 20826 " {\n" 20827 " a();\n" 20828 " }\n" 20829 " while (false)\n" 20830 " {\n" 20831 " b();\n" 20832 " }\n" 20833 " do\n" 20834 " {\n" 20835 " c();\n" 20836 " } while (false)\n" 20837 "}", 20838 AllmanBraceStyle); 20839 20840 verifyFormat("void f(int a)\n" 20841 "{\n" 20842 " switch (a)\n" 20843 " {\n" 20844 " case 0:\n" 20845 " break;\n" 20846 " case 1:\n" 20847 " {\n" 20848 " break;\n" 20849 " }\n" 20850 " case 2:\n" 20851 " {\n" 20852 " }\n" 20853 " break;\n" 20854 " default:\n" 20855 " break;\n" 20856 " }\n" 20857 "}", 20858 AllmanBraceStyle); 20859 20860 verifyFormat("enum X\n" 20861 "{\n" 20862 " Y = 0,\n" 20863 "}", 20864 AllmanBraceStyle); 20865 verifyFormat("enum X\n" 20866 "{\n" 20867 " Y = 0\n" 20868 "}", 20869 AllmanBraceStyle); 20870 20871 verifyFormat("@interface BSApplicationController ()\n" 20872 "{\n" 20873 "@private\n" 20874 " id _extraIvar;\n" 20875 "}\n" 20876 "@end", 20877 AllmanBraceStyle); 20878 20879 verifyFormat("#ifdef _DEBUG\n" 20880 "int foo(int i = 0)\n" 20881 "#else\n" 20882 "int foo(int i = 5)\n" 20883 "#endif\n" 20884 "{\n" 20885 " return i;\n" 20886 "}", 20887 AllmanBraceStyle); 20888 20889 verifyFormat("void foo() {}\n" 20890 "void bar()\n" 20891 "#ifdef _DEBUG\n" 20892 "{\n" 20893 " foo();\n" 20894 "}\n" 20895 "#else\n" 20896 "{\n" 20897 "}\n" 20898 "#endif", 20899 AllmanBraceStyle); 20900 20901 verifyFormat("void foobar() { int i = 5; }\n" 20902 "#ifdef _DEBUG\n" 20903 "void bar() {}\n" 20904 "#else\n" 20905 "void bar() { foobar(); }\n" 20906 "#endif", 20907 AllmanBraceStyle); 20908 20909 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 20910 FormatStyle::SLS_All); 20911 20912 verifyFormat("[](int i) { return i + 2; };\n" 20913 "[](int i, int j)\n" 20914 "{\n" 20915 " auto x = i + j;\n" 20916 " auto y = i * j;\n" 20917 " return x ^ y;\n" 20918 "};\n" 20919 "void foo()\n" 20920 "{\n" 20921 " auto shortLambda = [](int i) { return i + 2; };\n" 20922 " auto longLambda = [](int i, int j)\n" 20923 " {\n" 20924 " auto x = i + j;\n" 20925 " auto y = i * j;\n" 20926 " return x ^ y;\n" 20927 " };\n" 20928 "}", 20929 AllmanBraceStyle); 20930 20931 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 20932 20933 verifyFormat("[](int i)\n" 20934 "{\n" 20935 " return i + 2;\n" 20936 "};\n" 20937 "[](int i, int j)\n" 20938 "{\n" 20939 " auto x = i + j;\n" 20940 " auto y = i * j;\n" 20941 " return x ^ y;\n" 20942 "};\n" 20943 "void foo()\n" 20944 "{\n" 20945 " auto shortLambda = [](int i)\n" 20946 " {\n" 20947 " return i + 2;\n" 20948 " };\n" 20949 " auto longLambda = [](int i, int j)\n" 20950 " {\n" 20951 " auto x = i + j;\n" 20952 " auto y = i * j;\n" 20953 " return x ^ y;\n" 20954 " };\n" 20955 "}", 20956 AllmanBraceStyle); 20957 20958 // Reset 20959 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 20960 20961 // This shouldn't affect ObjC blocks.. 20962 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 20963 " // ...\n" 20964 " int i;\n" 20965 "}];", 20966 AllmanBraceStyle); 20967 verifyFormat("void (^block)(void) = ^{\n" 20968 " // ...\n" 20969 " int i;\n" 20970 "};", 20971 AllmanBraceStyle); 20972 // .. or dict literals. 20973 verifyFormat("void f()\n" 20974 "{\n" 20975 " // ...\n" 20976 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 20977 "}", 20978 AllmanBraceStyle); 20979 verifyFormat("void f()\n" 20980 "{\n" 20981 " // ...\n" 20982 " [object someMethod:@{a : @\"b\"}];\n" 20983 "}", 20984 AllmanBraceStyle); 20985 verifyFormat("int f()\n" 20986 "{ // comment\n" 20987 " return 42;\n" 20988 "}", 20989 AllmanBraceStyle); 20990 20991 AllmanBraceStyle.ColumnLimit = 19; 20992 verifyFormat("void f() { int i; }", AllmanBraceStyle); 20993 AllmanBraceStyle.ColumnLimit = 18; 20994 verifyFormat("void f()\n" 20995 "{\n" 20996 " int i;\n" 20997 "}", 20998 AllmanBraceStyle); 20999 AllmanBraceStyle.ColumnLimit = 80; 21000 21001 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 21002 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 21003 FormatStyle::SIS_WithoutElse; 21004 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 21005 verifyFormat("void f(bool b)\n" 21006 "{\n" 21007 " if (b)\n" 21008 " {\n" 21009 " return;\n" 21010 " }\n" 21011 "}", 21012 BreakBeforeBraceShortIfs); 21013 verifyFormat("void f(bool b)\n" 21014 "{\n" 21015 " if constexpr (b)\n" 21016 " {\n" 21017 " return;\n" 21018 " }\n" 21019 "}", 21020 BreakBeforeBraceShortIfs); 21021 verifyFormat("void f(bool b)\n" 21022 "{\n" 21023 " if CONSTEXPR (b)\n" 21024 " {\n" 21025 " return;\n" 21026 " }\n" 21027 "}", 21028 BreakBeforeBraceShortIfs); 21029 verifyFormat("void f(bool b)\n" 21030 "{\n" 21031 " if (b) return;\n" 21032 "}", 21033 BreakBeforeBraceShortIfs); 21034 verifyFormat("void f(bool b)\n" 21035 "{\n" 21036 " if constexpr (b) return;\n" 21037 "}", 21038 BreakBeforeBraceShortIfs); 21039 verifyFormat("void f(bool b)\n" 21040 "{\n" 21041 " if CONSTEXPR (b) return;\n" 21042 "}", 21043 BreakBeforeBraceShortIfs); 21044 verifyFormat("void f(bool b)\n" 21045 "{\n" 21046 " while (b)\n" 21047 " {\n" 21048 " return;\n" 21049 " }\n" 21050 "}", 21051 BreakBeforeBraceShortIfs); 21052 } 21053 21054 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 21055 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0); 21056 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 21057 21058 // Make a few changes to the style for testing purposes 21059 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 21060 FormatStyle::SFS_Empty; 21061 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 21062 21063 // FIXME: this test case can't decide whether there should be a blank line 21064 // after the ~D() line or not. It adds one if one doesn't exist in the test 21065 // and it removes the line if one exists. 21066 /* 21067 verifyFormat("class A;\n" 21068 "namespace B\n" 21069 " {\n" 21070 "class C;\n" 21071 "// Comment\n" 21072 "class D\n" 21073 " {\n" 21074 "public:\n" 21075 " D();\n" 21076 " ~D() {}\n" 21077 "private:\n" 21078 " enum E\n" 21079 " {\n" 21080 " F\n" 21081 " }\n" 21082 " };\n" 21083 " } // namespace B", 21084 WhitesmithsBraceStyle); 21085 */ 21086 21087 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 21088 verifyFormat("namespace a\n" 21089 " {\n" 21090 "class A\n" 21091 " {\n" 21092 " void f()\n" 21093 " {\n" 21094 " if (true)\n" 21095 " {\n" 21096 " a();\n" 21097 " b();\n" 21098 " }\n" 21099 " }\n" 21100 " void g()\n" 21101 " {\n" 21102 " return;\n" 21103 " }\n" 21104 " };\n" 21105 "struct B\n" 21106 " {\n" 21107 " int x;\n" 21108 " };\n" 21109 " } // namespace a", 21110 WhitesmithsBraceStyle); 21111 21112 verifyFormat("namespace a\n" 21113 " {\n" 21114 "namespace b\n" 21115 " {\n" 21116 "class A\n" 21117 " {\n" 21118 " void f()\n" 21119 " {\n" 21120 " if (true)\n" 21121 " {\n" 21122 " a();\n" 21123 " b();\n" 21124 " }\n" 21125 " }\n" 21126 " void g()\n" 21127 " {\n" 21128 " return;\n" 21129 " }\n" 21130 " };\n" 21131 "struct B\n" 21132 " {\n" 21133 " int x;\n" 21134 " };\n" 21135 " } // namespace b\n" 21136 " } // namespace a", 21137 WhitesmithsBraceStyle); 21138 21139 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 21140 verifyFormat("namespace a\n" 21141 " {\n" 21142 "namespace b\n" 21143 " {\n" 21144 " class A\n" 21145 " {\n" 21146 " void f()\n" 21147 " {\n" 21148 " if (true)\n" 21149 " {\n" 21150 " a();\n" 21151 " b();\n" 21152 " }\n" 21153 " }\n" 21154 " void g()\n" 21155 " {\n" 21156 " return;\n" 21157 " }\n" 21158 " };\n" 21159 " struct B\n" 21160 " {\n" 21161 " int x;\n" 21162 " };\n" 21163 " } // namespace b\n" 21164 " } // namespace a", 21165 WhitesmithsBraceStyle); 21166 21167 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 21168 verifyFormat("namespace a\n" 21169 " {\n" 21170 " namespace b\n" 21171 " {\n" 21172 " class A\n" 21173 " {\n" 21174 " void f()\n" 21175 " {\n" 21176 " if (true)\n" 21177 " {\n" 21178 " a();\n" 21179 " b();\n" 21180 " }\n" 21181 " }\n" 21182 " void g()\n" 21183 " {\n" 21184 " return;\n" 21185 " }\n" 21186 " };\n" 21187 " struct B\n" 21188 " {\n" 21189 " int x;\n" 21190 " };\n" 21191 " } // namespace b\n" 21192 " } // namespace a", 21193 WhitesmithsBraceStyle); 21194 21195 verifyFormat("void f()\n" 21196 " {\n" 21197 " if (true)\n" 21198 " {\n" 21199 " a();\n" 21200 " }\n" 21201 " else if (false)\n" 21202 " {\n" 21203 " b();\n" 21204 " }\n" 21205 " else\n" 21206 " {\n" 21207 " c();\n" 21208 " }\n" 21209 " }", 21210 WhitesmithsBraceStyle); 21211 21212 verifyFormat("void f()\n" 21213 " {\n" 21214 " for (int i = 0; i < 10; ++i)\n" 21215 " {\n" 21216 " a();\n" 21217 " }\n" 21218 " while (false)\n" 21219 " {\n" 21220 " b();\n" 21221 " }\n" 21222 " do\n" 21223 " {\n" 21224 " c();\n" 21225 " } while (false)\n" 21226 " }", 21227 WhitesmithsBraceStyle); 21228 21229 WhitesmithsBraceStyle.IndentCaseLabels = true; 21230 verifyFormat("void switchTest1(int a)\n" 21231 " {\n" 21232 " switch (a)\n" 21233 " {\n" 21234 " case 2:\n" 21235 " {\n" 21236 " }\n" 21237 " break;\n" 21238 " }\n" 21239 " }", 21240 WhitesmithsBraceStyle); 21241 21242 verifyFormat("void switchTest2(int a)\n" 21243 " {\n" 21244 " switch (a)\n" 21245 " {\n" 21246 " case 0:\n" 21247 " break;\n" 21248 " case 1:\n" 21249 " {\n" 21250 " break;\n" 21251 " }\n" 21252 " case 2:\n" 21253 " {\n" 21254 " }\n" 21255 " break;\n" 21256 " default:\n" 21257 " break;\n" 21258 " }\n" 21259 " }", 21260 WhitesmithsBraceStyle); 21261 21262 verifyFormat("void switchTest3(int a)\n" 21263 " {\n" 21264 " switch (a)\n" 21265 " {\n" 21266 " case 0:\n" 21267 " {\n" 21268 " foo(x);\n" 21269 " }\n" 21270 " break;\n" 21271 " default:\n" 21272 " {\n" 21273 " foo(1);\n" 21274 " }\n" 21275 " break;\n" 21276 " }\n" 21277 " }", 21278 WhitesmithsBraceStyle); 21279 21280 WhitesmithsBraceStyle.IndentCaseLabels = false; 21281 21282 verifyFormat("void switchTest4(int a)\n" 21283 " {\n" 21284 " switch (a)\n" 21285 " {\n" 21286 " case 2:\n" 21287 " {\n" 21288 " }\n" 21289 " break;\n" 21290 " }\n" 21291 " }", 21292 WhitesmithsBraceStyle); 21293 21294 verifyFormat("void switchTest5(int a)\n" 21295 " {\n" 21296 " switch (a)\n" 21297 " {\n" 21298 " case 0:\n" 21299 " break;\n" 21300 " case 1:\n" 21301 " {\n" 21302 " foo();\n" 21303 " break;\n" 21304 " }\n" 21305 " case 2:\n" 21306 " {\n" 21307 " }\n" 21308 " break;\n" 21309 " default:\n" 21310 " break;\n" 21311 " }\n" 21312 " }", 21313 WhitesmithsBraceStyle); 21314 21315 verifyFormat("void switchTest6(int a)\n" 21316 " {\n" 21317 " switch (a)\n" 21318 " {\n" 21319 " case 0:\n" 21320 " {\n" 21321 " foo(x);\n" 21322 " }\n" 21323 " break;\n" 21324 " default:\n" 21325 " {\n" 21326 " foo(1);\n" 21327 " }\n" 21328 " break;\n" 21329 " }\n" 21330 " }", 21331 WhitesmithsBraceStyle); 21332 21333 verifyFormat("enum X\n" 21334 " {\n" 21335 " Y = 0, // testing\n" 21336 " }", 21337 WhitesmithsBraceStyle); 21338 21339 verifyFormat("enum X\n" 21340 " {\n" 21341 " Y = 0\n" 21342 " }", 21343 WhitesmithsBraceStyle); 21344 verifyFormat("enum X\n" 21345 " {\n" 21346 " Y = 0,\n" 21347 " Z = 1\n" 21348 " };", 21349 WhitesmithsBraceStyle); 21350 21351 verifyFormat("@interface BSApplicationController ()\n" 21352 " {\n" 21353 "@private\n" 21354 " id _extraIvar;\n" 21355 " }\n" 21356 "@end", 21357 WhitesmithsBraceStyle); 21358 21359 verifyFormat("#ifdef _DEBUG\n" 21360 "int foo(int i = 0)\n" 21361 "#else\n" 21362 "int foo(int i = 5)\n" 21363 "#endif\n" 21364 " {\n" 21365 " return i;\n" 21366 " }", 21367 WhitesmithsBraceStyle); 21368 21369 verifyFormat("void foo() {}\n" 21370 "void bar()\n" 21371 "#ifdef _DEBUG\n" 21372 " {\n" 21373 " foo();\n" 21374 " }\n" 21375 "#else\n" 21376 " {\n" 21377 " }\n" 21378 "#endif", 21379 WhitesmithsBraceStyle); 21380 21381 verifyFormat("void foobar()\n" 21382 " {\n" 21383 " int i = 5;\n" 21384 " }\n" 21385 "#ifdef _DEBUG\n" 21386 "void bar() {}\n" 21387 "#else\n" 21388 "void bar()\n" 21389 " {\n" 21390 " foobar();\n" 21391 " }\n" 21392 "#endif", 21393 WhitesmithsBraceStyle); 21394 21395 // This shouldn't affect ObjC blocks.. 21396 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 21397 " // ...\n" 21398 " int i;\n" 21399 "}];", 21400 WhitesmithsBraceStyle); 21401 verifyFormat("void (^block)(void) = ^{\n" 21402 " // ...\n" 21403 " int i;\n" 21404 "};", 21405 WhitesmithsBraceStyle); 21406 // .. or dict literals. 21407 verifyFormat("void f()\n" 21408 " {\n" 21409 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 21410 " }", 21411 WhitesmithsBraceStyle); 21412 21413 verifyFormat("int f()\n" 21414 " { // comment\n" 21415 " return 42;\n" 21416 " }", 21417 WhitesmithsBraceStyle); 21418 21419 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 21420 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 21421 FormatStyle::SIS_OnlyFirstIf; 21422 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 21423 verifyFormat("void f(bool b)\n" 21424 " {\n" 21425 " if (b)\n" 21426 " {\n" 21427 " return;\n" 21428 " }\n" 21429 " }", 21430 BreakBeforeBraceShortIfs); 21431 verifyFormat("void f(bool b)\n" 21432 " {\n" 21433 " if (b) return;\n" 21434 " }", 21435 BreakBeforeBraceShortIfs); 21436 verifyFormat("void f(bool b)\n" 21437 " {\n" 21438 " while (b)\n" 21439 " {\n" 21440 " return;\n" 21441 " }\n" 21442 " }", 21443 BreakBeforeBraceShortIfs); 21444 } 21445 21446 TEST_F(FormatTest, GNUBraceBreaking) { 21447 FormatStyle GNUBraceStyle = getLLVMStyle(); 21448 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 21449 verifyFormat("namespace a\n" 21450 "{\n" 21451 "class A\n" 21452 "{\n" 21453 " void f()\n" 21454 " {\n" 21455 " int a;\n" 21456 " {\n" 21457 " int b;\n" 21458 " }\n" 21459 " if (true)\n" 21460 " {\n" 21461 " a();\n" 21462 " b();\n" 21463 " }\n" 21464 " }\n" 21465 " void g() { return; }\n" 21466 "}\n" 21467 "} // namespace a", 21468 GNUBraceStyle); 21469 21470 verifyFormat("void f()\n" 21471 "{\n" 21472 " if (true)\n" 21473 " {\n" 21474 " a();\n" 21475 " }\n" 21476 " else if (false)\n" 21477 " {\n" 21478 " b();\n" 21479 " }\n" 21480 " else\n" 21481 " {\n" 21482 " c();\n" 21483 " }\n" 21484 "}", 21485 GNUBraceStyle); 21486 21487 verifyFormat("void f()\n" 21488 "{\n" 21489 " for (int i = 0; i < 10; ++i)\n" 21490 " {\n" 21491 " a();\n" 21492 " }\n" 21493 " while (false)\n" 21494 " {\n" 21495 " b();\n" 21496 " }\n" 21497 " do\n" 21498 " {\n" 21499 " c();\n" 21500 " }\n" 21501 " while (false);\n" 21502 "}", 21503 GNUBraceStyle); 21504 21505 verifyFormat("void f(int a)\n" 21506 "{\n" 21507 " switch (a)\n" 21508 " {\n" 21509 " case 0:\n" 21510 " break;\n" 21511 " case 1:\n" 21512 " {\n" 21513 " break;\n" 21514 " }\n" 21515 " case 2:\n" 21516 " {\n" 21517 " }\n" 21518 " break;\n" 21519 " default:\n" 21520 " break;\n" 21521 " }\n" 21522 "}", 21523 GNUBraceStyle); 21524 21525 verifyFormat("enum X\n" 21526 "{\n" 21527 " Y = 0,\n" 21528 "}", 21529 GNUBraceStyle); 21530 21531 verifyFormat("@interface BSApplicationController ()\n" 21532 "{\n" 21533 "@private\n" 21534 " id _extraIvar;\n" 21535 "}\n" 21536 "@end", 21537 GNUBraceStyle); 21538 21539 verifyFormat("#ifdef _DEBUG\n" 21540 "int foo(int i = 0)\n" 21541 "#else\n" 21542 "int foo(int i = 5)\n" 21543 "#endif\n" 21544 "{\n" 21545 " return i;\n" 21546 "}", 21547 GNUBraceStyle); 21548 21549 verifyFormat("void foo() {}\n" 21550 "void bar()\n" 21551 "#ifdef _DEBUG\n" 21552 "{\n" 21553 " foo();\n" 21554 "}\n" 21555 "#else\n" 21556 "{\n" 21557 "}\n" 21558 "#endif", 21559 GNUBraceStyle); 21560 21561 verifyFormat("void foobar() { int i = 5; }\n" 21562 "#ifdef _DEBUG\n" 21563 "void bar() {}\n" 21564 "#else\n" 21565 "void bar() { foobar(); }\n" 21566 "#endif", 21567 GNUBraceStyle); 21568 } 21569 21570 TEST_F(FormatTest, WebKitBraceBreaking) { 21571 FormatStyle WebKitBraceStyle = getLLVMStyle(); 21572 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 21573 WebKitBraceStyle.FixNamespaceComments = false; 21574 verifyFormat("namespace a {\n" 21575 "class A {\n" 21576 " void f()\n" 21577 " {\n" 21578 " if (true) {\n" 21579 " a();\n" 21580 " b();\n" 21581 " }\n" 21582 " }\n" 21583 " void g() { return; }\n" 21584 "};\n" 21585 "enum E {\n" 21586 " A,\n" 21587 " // foo\n" 21588 " B,\n" 21589 " C\n" 21590 "};\n" 21591 "struct B {\n" 21592 " int x;\n" 21593 "};\n" 21594 "}", 21595 WebKitBraceStyle); 21596 verifyFormat("struct S {\n" 21597 " int Type;\n" 21598 " union {\n" 21599 " int x;\n" 21600 " double y;\n" 21601 " } Value;\n" 21602 " class C {\n" 21603 " MyFavoriteType Value;\n" 21604 " } Class;\n" 21605 "};", 21606 WebKitBraceStyle); 21607 } 21608 21609 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 21610 verifyFormat("void f() {\n" 21611 " try {\n" 21612 " } catch (const Exception &e) {\n" 21613 " }\n" 21614 "}"); 21615 } 21616 21617 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 21618 auto Style = getLLVMStyle(); 21619 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 21620 verifyNoCrash("f({\n" 21621 "table({}, table({{\"\", false}}, {}))\n" 21622 "});", 21623 Style); 21624 21625 Style.AlignConsecutiveAssignments.Enabled = true; 21626 Style.AlignConsecutiveDeclarations.Enabled = true; 21627 verifyFormat("struct test demo[] = {\n" 21628 " {56, 23, \"hello\"},\n" 21629 " {-1, 93463, \"world\"},\n" 21630 " { 7, 5, \"!!\"}\n" 21631 "};", 21632 Style); 21633 21634 verifyFormat("struct test demo[] = {\n" 21635 " {56, 23, \"hello\"}, // first line\n" 21636 " {-1, 93463, \"world\"}, // second line\n" 21637 " { 7, 5, \"!!\"} // third line\n" 21638 "};", 21639 Style); 21640 21641 verifyFormat("struct test demo[4] = {\n" 21642 " { 56, 23, 21, \"oh\"}, // first line\n" 21643 " { -1, 93463, 22, \"my\"}, // second line\n" 21644 " { 7, 5, 1, \"goodness\"} // third line\n" 21645 " {234, 5, 1, \"gracious\"} // fourth line\n" 21646 "};", 21647 Style); 21648 21649 verifyFormat("struct test demo[3] = {\n" 21650 " {56, 23, \"hello\"},\n" 21651 " {-1, 93463, \"world\"},\n" 21652 " { 7, 5, \"!!\"}\n" 21653 "};", 21654 Style); 21655 21656 verifyFormat("struct test demo[3] = {\n" 21657 " {int{56}, 23, \"hello\"},\n" 21658 " {int{-1}, 93463, \"world\"},\n" 21659 " { int{7}, 5, \"!!\"}\n" 21660 "};", 21661 Style); 21662 21663 verifyFormat("struct test demo[] = {\n" 21664 " {56, 23, \"hello\"},\n" 21665 " {-1, 93463, \"world\"},\n" 21666 " { 7, 5, \"!!\"},\n" 21667 "};", 21668 Style); 21669 21670 verifyFormat("test demo[] = {\n" 21671 " {56, 23, \"hello\"},\n" 21672 " {-1, 93463, \"world\"},\n" 21673 " { 7, 5, \"!!\"},\n" 21674 "};", 21675 Style); 21676 21677 verifyFormat("demo = std::array<struct test, 3>{\n" 21678 " test{56, 23, \"hello\"},\n" 21679 " test{-1, 93463, \"world\"},\n" 21680 " test{ 7, 5, \"!!\"},\n" 21681 "};", 21682 Style); 21683 21684 verifyFormat("test demo[] = {\n" 21685 " {56, 23, \"hello\"},\n" 21686 "#if X\n" 21687 " {-1, 93463, \"world\"},\n" 21688 "#endif\n" 21689 " { 7, 5, \"!!\"}\n" 21690 "};", 21691 Style); 21692 21693 verifyFormat( 21694 "test demo[] = {\n" 21695 " { 7, 23,\n" 21696 " \"hello world i am a very long line that really, in any\"\n" 21697 " \"just world, ought to be split over multiple lines\"},\n" 21698 " {-1, 93463, \"world\"},\n" 21699 " {56, 5, \"!!\"}\n" 21700 "};", 21701 Style); 21702 21703 verifyNoCrash("Foo f[] = {\n" 21704 " [0] = { 1, },\n" 21705 " [i] { 1, },\n" 21706 "};", 21707 Style); 21708 verifyNoCrash("Foo foo[] = {\n" 21709 " [0] = {1, 1},\n" 21710 " [1] { 1, 1, },\n" 21711 " [2] { 1, 1, },\n" 21712 "};", 21713 Style); 21714 verifyNoCrash("test arr[] = {\n" 21715 "#define FOO(i) {i, i},\n" 21716 "SOME_GENERATOR(FOO)\n" 21717 "{2, 2}\n" 21718 "};", 21719 Style); 21720 21721 verifyFormat("return GradForUnaryCwise(g, {\n" 21722 " {{\"sign\"}, \"Sign\", " 21723 " {\"x\", \"dy\"}},\n" 21724 " { {\"dx\"}, \"Mul\", {\"dy\"" 21725 ", \"sign\"}},\n" 21726 "});", 21727 Style); 21728 21729 Style.Cpp11BracedListStyle = false; 21730 verifyFormat("struct test demo[] = {\n" 21731 " { 56, 23, \"hello\" },\n" 21732 " { -1, 93463, \"world\" },\n" 21733 " { 7, 5, \"!!\" }\n" 21734 "};", 21735 Style); 21736 Style.Cpp11BracedListStyle = true; 21737 21738 Style.ColumnLimit = 0; 21739 verifyFormat( 21740 "test demo[] = {\n" 21741 " {56, 23, \"hello world i am a very long line that really, " 21742 "in any just world, ought to be split over multiple lines\"},\n" 21743 " {-1, 93463, " 21744 " \"world\"},\n" 21745 " { 7, 5, " 21746 " \"!!\"},\n" 21747 "};", 21748 "test demo[] = {{56, 23, \"hello world i am a very long line " 21749 "that really, in any just world, ought to be split over multiple " 21750 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 21751 Style); 21752 21753 Style.ColumnLimit = 80; 21754 verifyFormat("test demo[] = {\n" 21755 " {56, 23, /* a comment */ \"hello\"},\n" 21756 " {-1, 93463, \"world\"},\n" 21757 " { 7, 5, \"!!\"}\n" 21758 "};", 21759 Style); 21760 21761 verifyFormat("test demo[] = {\n" 21762 " {56, 23, \"hello\"},\n" 21763 " {-1, 93463, \"world\" /* comment here */},\n" 21764 " { 7, 5, \"!!\"}\n" 21765 "};", 21766 Style); 21767 21768 verifyFormat("test demo[] = {\n" 21769 " {56, /* a comment */ 23, \"hello\"},\n" 21770 " {-1, 93463, \"world\"},\n" 21771 " { 7, 5, \"!!\"}\n" 21772 "};", 21773 Style); 21774 21775 Style.ColumnLimit = 20; 21776 verifyFormat("demo = std::array<\n" 21777 " struct test, 3>{\n" 21778 " test{\n" 21779 " 56, 23,\n" 21780 " \"hello \"\n" 21781 " \"world i \"\n" 21782 " \"am a very \"\n" 21783 " \"long line \"\n" 21784 " \"that \"\n" 21785 " \"really, \"\n" 21786 " \"in any \"\n" 21787 " \"just \"\n" 21788 " \"world, \"\n" 21789 " \"ought to \"\n" 21790 " \"be split \"\n" 21791 " \"over \"\n" 21792 " \"multiple \"\n" 21793 " \"lines\"},\n" 21794 " test{-1, 93463,\n" 21795 " \"world\"},\n" 21796 " test{ 7, 5,\n" 21797 " \"!!\" },\n" 21798 "};", 21799 "demo = std::array<struct test, 3>{test{56, 23, \"hello world " 21800 "i am a very long line that really, in any just world, ought " 21801 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 21802 "test{7, 5, \"!!\"},};", 21803 Style); 21804 // This caused a core dump by enabling Alignment in the LLVMStyle globally 21805 Style = getLLVMStyleWithColumns(50); 21806 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 21807 verifyFormat("static A x = {\n" 21808 " {{init1, init2, init3, init4},\n" 21809 " {init1, init2, init3, init4}}\n" 21810 "};", 21811 Style); 21812 // TODO: Fix the indentations below when this option is fully functional. 21813 #if 0 21814 verifyFormat("int a[][] = {\n" 21815 " {\n" 21816 " {0, 2}, //\n" 21817 " {1, 2} //\n" 21818 " }\n" 21819 "};", 21820 Style); 21821 #endif 21822 Style.ColumnLimit = 100; 21823 verifyFormat( 21824 "test demo[] = {\n" 21825 " {56, 23,\n" 21826 " \"hello world i am a very long line that really, in any just world" 21827 ", ought to be split over \"\n" 21828 " \"multiple lines\" },\n" 21829 " {-1, 93463, \"world\"},\n" 21830 " { 7, 5, \"!!\"},\n" 21831 "};", 21832 "test demo[] = {{56, 23, \"hello world i am a very long line " 21833 "that really, in any just world, ought to be split over multiple " 21834 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 21835 Style); 21836 21837 Style = getLLVMStyleWithColumns(50); 21838 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 21839 verifyFormat("struct test demo[] = {\n" 21840 " {56, 23, \"hello\"},\n" 21841 " {-1, 93463, \"world\"},\n" 21842 " { 7, 5, \"!!\"}\n" 21843 "};\n" 21844 "static A x = {\n" 21845 " {{init1, init2, init3, init4},\n" 21846 " {init1, init2, init3, init4}}\n" 21847 "};", 21848 Style); 21849 Style.ColumnLimit = 100; 21850 Style.AlignConsecutiveAssignments.AcrossComments = true; 21851 Style.AlignConsecutiveDeclarations.AcrossComments = true; 21852 verifyFormat("struct test demo[] = {\n" 21853 " {56, 23, \"hello\"},\n" 21854 " {-1, 93463, \"world\"},\n" 21855 " { 7, 5, \"!!\"}\n" 21856 "};\n" 21857 "struct test demo[4] = {\n" 21858 " { 56, 23, 21, \"oh\"}, // first line\n" 21859 " { -1, 93463, 22, \"my\"}, // second line\n" 21860 " { 7, 5, 1, \"goodness\"} // third line\n" 21861 " {234, 5, 1, \"gracious\"} // fourth line\n" 21862 "};", 21863 Style); 21864 verifyFormat( 21865 "test demo[] = {\n" 21866 " {56,\n" 21867 " \"hello world i am a very long line that really, in any just world" 21868 ", ought to be split over \"\n" 21869 " \"multiple lines\", 23},\n" 21870 " {-1, \"world\", 93463},\n" 21871 " { 7, \"!!\", 5},\n" 21872 "};", 21873 "test demo[] = {{56, \"hello world i am a very long line " 21874 "that really, in any just world, ought to be split over multiple " 21875 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 21876 Style); 21877 } 21878 21879 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 21880 auto Style = getLLVMStyle(); 21881 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 21882 /* FIXME: This case gets misformatted. 21883 verifyFormat("auto foo = Items{\n" 21884 " Section{0, bar(), },\n" 21885 " Section{1, boo() }\n" 21886 "};", 21887 Style); 21888 */ 21889 verifyFormat("auto foo = Items{\n" 21890 " Section{\n" 21891 " 0, bar(),\n" 21892 " }\n" 21893 "};", 21894 Style); 21895 verifyFormat("struct test demo[] = {\n" 21896 " {56, 23, \"hello\"},\n" 21897 " {-1, 93463, \"world\"},\n" 21898 " {7, 5, \"!!\" }\n" 21899 "};", 21900 Style); 21901 verifyFormat("struct test demo[] = {\n" 21902 " {56, 23, \"hello\"}, // first line\n" 21903 " {-1, 93463, \"world\"}, // second line\n" 21904 " {7, 5, \"!!\" } // third line\n" 21905 "};", 21906 Style); 21907 verifyFormat("struct test demo[4] = {\n" 21908 " {56, 23, 21, \"oh\" }, // first line\n" 21909 " {-1, 93463, 22, \"my\" }, // second line\n" 21910 " {7, 5, 1, \"goodness\"} // third line\n" 21911 " {234, 5, 1, \"gracious\"} // fourth line\n" 21912 "};", 21913 Style); 21914 verifyFormat("struct test demo[3] = {\n" 21915 " {56, 23, \"hello\"},\n" 21916 " {-1, 93463, \"world\"},\n" 21917 " {7, 5, \"!!\" }\n" 21918 "};", 21919 Style); 21920 21921 verifyFormat("struct test demo[3] = {\n" 21922 " {int{56}, 23, \"hello\"},\n" 21923 " {int{-1}, 93463, \"world\"},\n" 21924 " {int{7}, 5, \"!!\" }\n" 21925 "};", 21926 Style); 21927 verifyFormat("struct test demo[] = {\n" 21928 " {56, 23, \"hello\"},\n" 21929 " {-1, 93463, \"world\"},\n" 21930 " {7, 5, \"!!\" },\n" 21931 "};", 21932 Style); 21933 verifyFormat("test demo[] = {\n" 21934 " {56, 23, \"hello\"},\n" 21935 " {-1, 93463, \"world\"},\n" 21936 " {7, 5, \"!!\" },\n" 21937 "};", 21938 Style); 21939 verifyFormat("demo = std::array<struct test, 3>{\n" 21940 " test{56, 23, \"hello\"},\n" 21941 " test{-1, 93463, \"world\"},\n" 21942 " test{7, 5, \"!!\" },\n" 21943 "};", 21944 Style); 21945 verifyFormat("test demo[] = {\n" 21946 " {56, 23, \"hello\"},\n" 21947 "#if X\n" 21948 " {-1, 93463, \"world\"},\n" 21949 "#endif\n" 21950 " {7, 5, \"!!\" }\n" 21951 "};", 21952 Style); 21953 verifyFormat( 21954 "test demo[] = {\n" 21955 " {7, 23,\n" 21956 " \"hello world i am a very long line that really, in any\"\n" 21957 " \"just world, ought to be split over multiple lines\"},\n" 21958 " {-1, 93463, \"world\" },\n" 21959 " {56, 5, \"!!\" }\n" 21960 "};", 21961 Style); 21962 21963 verifyNoCrash("Foo f[] = {\n" 21964 " [0] = { 1, },\n" 21965 " [i] { 1, },\n" 21966 "};", 21967 Style); 21968 verifyNoCrash("Foo foo[] = {\n" 21969 " [0] = {1, 1},\n" 21970 " [1] { 1, 1, },\n" 21971 " [2] { 1, 1, },\n" 21972 "};", 21973 Style); 21974 verifyNoCrash("test arr[] = {\n" 21975 "#define FOO(i) {i, i},\n" 21976 "SOME_GENERATOR(FOO)\n" 21977 "{2, 2}\n" 21978 "};", 21979 Style); 21980 21981 verifyFormat("return GradForUnaryCwise(g, {\n" 21982 " {{\"sign\"}, \"Sign\", {\"x\", " 21983 "\"dy\"} },\n" 21984 " {{\"dx\"}, \"Mul\", " 21985 "{\"dy\", \"sign\"}},\n" 21986 "});", 21987 Style); 21988 21989 Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 21990 verifyFormat("#define FOO \\\n" 21991 " int foo[][2] = { \\\n" 21992 " {0, 1} \\\n" 21993 " };", 21994 Style); 21995 21996 Style.Cpp11BracedListStyle = false; 21997 verifyFormat("struct test demo[] = {\n" 21998 " { 56, 23, \"hello\" },\n" 21999 " { -1, 93463, \"world\" },\n" 22000 " { 7, 5, \"!!\" }\n" 22001 "};", 22002 Style); 22003 Style.Cpp11BracedListStyle = true; 22004 22005 Style.ColumnLimit = 0; 22006 verifyFormat( 22007 "test demo[] = {\n" 22008 " {56, 23, \"hello world i am a very long line that really, in any " 22009 "just world, ought to be split over multiple lines\"},\n" 22010 " {-1, 93463, \"world\" " 22011 " },\n" 22012 " {7, 5, \"!!\" " 22013 " },\n" 22014 "};", 22015 "test demo[] = {{56, 23, \"hello world i am a very long line " 22016 "that really, in any just world, ought to be split over multiple " 22017 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 22018 Style); 22019 22020 Style.ColumnLimit = 80; 22021 verifyFormat("test demo[] = {\n" 22022 " {56, 23, /* a comment */ \"hello\"},\n" 22023 " {-1, 93463, \"world\" },\n" 22024 " {7, 5, \"!!\" }\n" 22025 "};", 22026 Style); 22027 22028 verifyFormat("test demo[] = {\n" 22029 " {56, 23, \"hello\" },\n" 22030 " {-1, 93463, \"world\" /* comment here */},\n" 22031 " {7, 5, \"!!\" }\n" 22032 "};", 22033 Style); 22034 22035 verifyFormat("test demo[] = {\n" 22036 " {56, /* a comment */ 23, \"hello\"},\n" 22037 " {-1, 93463, \"world\"},\n" 22038 " {7, 5, \"!!\" }\n" 22039 "};", 22040 Style); 22041 verifyFormat("Foo foo = {\n" 22042 " // comment\n" 22043 " {1, 2}\n" 22044 "};", 22045 Style); 22046 22047 Style.ColumnLimit = 20; 22048 // FIXME: unstable test case 22049 EXPECT_EQ( 22050 "demo = std::array<\n" 22051 " struct test, 3>{\n" 22052 " test{\n" 22053 " 56, 23,\n" 22054 " \"hello \"\n" 22055 " \"world i \"\n" 22056 " \"am a very \"\n" 22057 " \"long line \"\n" 22058 " \"that \"\n" 22059 " \"really, \"\n" 22060 " \"in any \"\n" 22061 " \"just \"\n" 22062 " \"world, \"\n" 22063 " \"ought to \"\n" 22064 " \"be split \"\n" 22065 " \"over \"\n" 22066 " \"multiple \"\n" 22067 " \"lines\"},\n" 22068 " test{-1, 93463,\n" 22069 " \"world\"},\n" 22070 " test{7, 5,\n" 22071 " \"!!\" },\n" 22072 "};", 22073 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 22074 "i am a very long line that really, in any just world, ought " 22075 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 22076 "test{7, 5, \"!!\"},};", 22077 Style)); 22078 22079 // This caused a core dump by enabling Alignment in the LLVMStyle globally 22080 Style = getLLVMStyleWithColumns(50); 22081 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 22082 verifyFormat("static A x = {\n" 22083 " {{init1, init2, init3, init4},\n" 22084 " {init1, init2, init3, init4}}\n" 22085 "};", 22086 Style); 22087 Style.ColumnLimit = 100; 22088 verifyFormat( 22089 "test demo[] = {\n" 22090 " {56, 23,\n" 22091 " \"hello world i am a very long line that really, in any just world" 22092 ", ought to be split over \"\n" 22093 " \"multiple lines\" },\n" 22094 " {-1, 93463, \"world\"},\n" 22095 " {7, 5, \"!!\" },\n" 22096 "};", 22097 "test demo[] = {{56, 23, \"hello world i am a very long line " 22098 "that really, in any just world, ought to be split over multiple " 22099 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 22100 Style); 22101 22102 Style.ColumnLimit = 25; 22103 verifyNoCrash("Type foo{\n" 22104 " {\n" 22105 " 1, // A\n" 22106 " 2, // B\n" 22107 " 3, // C\n" 22108 " },\n" 22109 " \"hello\",\n" 22110 "};", 22111 Style); 22112 verifyNoCrash("Type object[X][Y] = {\n" 22113 " {{val}, {val}, {val}},\n" 22114 " {{val}, {val}, // some comment\n" 22115 " {val}}\n" 22116 "};", 22117 Style); 22118 22119 Style.ColumnLimit = 120; 22120 verifyNoCrash( 22121 "T v[] {\n" 22122 " { AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaa, " 22123 "AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaaaaaaa, 1, 0.000000000f, " 22124 "\"00000000000000000000000000000000000000000000000000000000" 22125 "00000000000000000000000000000000000000000000000000000000\" },\n" 22126 "};", 22127 Style); 22128 22129 Style.SpacesInParens = FormatStyle::SIPO_Custom; 22130 Style.SpacesInParensOptions.Other = true; 22131 verifyFormat("Foo foo[] = {\n" 22132 " {1, 1},\n" 22133 " {1, 1},\n" 22134 "};", 22135 Style); 22136 } 22137 22138 TEST_F(FormatTest, UnderstandsPragmas) { 22139 verifyFormat("#pragma omp reduction(| : var)"); 22140 verifyFormat("#pragma omp reduction(+ : var)"); 22141 22142 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string " 22143 "(including parentheses).", 22144 "#pragma mark Any non-hyphenated or hyphenated string " 22145 "(including parentheses)."); 22146 22147 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string " 22148 "(including parentheses).", 22149 "#pragma mark Any non-hyphenated or hyphenated string " 22150 "(including parentheses)."); 22151 22152 verifyFormat("#pragma comment(linker, \\\n" 22153 " \"argument\" \\\n" 22154 " \"argument\"", 22155 "#pragma comment(linker, \\\n" 22156 " \"argument\" \\\n" 22157 " \"argument\"", 22158 getStyleWithColumns( 22159 getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp), 32)); 22160 } 22161 22162 TEST_F(FormatTest, UnderstandsPragmaOmpTarget) { 22163 verifyFormat("#pragma omp target map(to : var)"); 22164 verifyFormat("#pragma omp target map(to : var[ : N])"); 22165 verifyFormat("#pragma omp target map(to : var[0 : N])"); 22166 verifyFormat("#pragma omp target map(always, to : var[0 : N])"); 22167 22168 verifyFormat( 22169 "#pragma omp target \\\n" 22170 " reduction(+ : var) \\\n" 22171 " map(to : A[0 : N]) \\\n" 22172 " map(to : B[0 : N]) \\\n" 22173 " map(from : C[0 : N]) \\\n" 22174 " firstprivate(i) \\\n" 22175 " firstprivate(j) \\\n" 22176 " firstprivate(k)", 22177 "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) " 22178 "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)", 22179 getLLVMStyleWithColumns(26)); 22180 } 22181 22182 TEST_F(FormatTest, UnderstandPragmaOption) { 22183 verifyFormat("#pragma option -C -A"); 22184 22185 verifyFormat("#pragma option -C -A", "#pragma option -C -A"); 22186 } 22187 22188 TEST_F(FormatTest, UnderstandPragmaRegion) { 22189 auto Style = getLLVMStyleWithColumns(0); 22190 verifyFormat("#pragma region TEST(FOO : BAR)", Style); 22191 verifyFormat("#pragma region TEST(FOO: NOSPACE)", Style); 22192 } 22193 22194 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 22195 FormatStyle Style = getLLVMStyleWithColumns(20); 22196 22197 // See PR41213 22198 verifyFormat("/*\n" 22199 " *\t9012345\n" 22200 " * /8901\n" 22201 " */", 22202 "/*\n" 22203 " *\t9012345 /8901\n" 22204 " */", 22205 Style); 22206 verifyFormat("/*\n" 22207 " *345678\n" 22208 " *\t/8901\n" 22209 " */", 22210 "/*\n" 22211 " *345678\t/8901\n" 22212 " */", 22213 Style); 22214 22215 verifyFormat("int a; // the\n" 22216 " // comment", 22217 Style); 22218 verifyNoChange("int a; /* first line\n" 22219 " * second\n" 22220 " * line third\n" 22221 " * line\n" 22222 " */", 22223 Style); 22224 verifyFormat("int a; // first line\n" 22225 " // second\n" 22226 " // line third\n" 22227 " // line", 22228 "int a; // first line\n" 22229 " // second line\n" 22230 " // third line", 22231 Style); 22232 22233 Style.PenaltyExcessCharacter = 90; 22234 verifyFormat("int a; // the comment", Style); 22235 verifyFormat("int a; // the comment\n" 22236 " // aaa", 22237 "int a; // the comment aaa", Style); 22238 verifyNoChange("int a; /* first line\n" 22239 " * second line\n" 22240 " * third line\n" 22241 " */", 22242 Style); 22243 verifyFormat("int a; // first line\n" 22244 " // second line\n" 22245 " // third line", 22246 Style); 22247 // FIXME: Investigate why this is not getting the same layout as the test 22248 // above. 22249 verifyFormat("int a; /* first line\n" 22250 " * second line\n" 22251 " * third line\n" 22252 " */", 22253 "int a; /* first line second line third line" 22254 "\n*/", 22255 Style); 22256 22257 verifyFormat("// foo bar baz bazfoo\n" 22258 "// foo bar foo bar", 22259 "// foo bar baz bazfoo\n" 22260 "// foo bar foo bar", 22261 Style); 22262 verifyFormat("// foo bar baz bazfoo\n" 22263 "// foo bar foo bar", 22264 "// foo bar baz bazfoo\n" 22265 "// foo bar foo bar", 22266 Style); 22267 22268 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 22269 // next one. 22270 verifyFormat("// foo bar baz bazfoo\n" 22271 "// bar foo bar", 22272 "// foo bar baz bazfoo bar\n" 22273 "// foo bar", 22274 Style); 22275 22276 // FIXME: unstable test case 22277 EXPECT_EQ("// foo bar baz bazfoo\n" 22278 "// foo bar baz bazfoo\n" 22279 "// bar foo bar", 22280 format("// foo bar baz bazfoo\n" 22281 "// foo bar baz bazfoo bar\n" 22282 "// foo bar", 22283 Style)); 22284 22285 // FIXME: unstable test case 22286 EXPECT_EQ("// foo bar baz bazfoo\n" 22287 "// foo bar baz bazfoo\n" 22288 "// bar foo bar", 22289 format("// foo bar baz bazfoo\n" 22290 "// foo bar baz bazfoo bar\n" 22291 "// foo bar", 22292 Style)); 22293 22294 // Make sure we do not keep protruding characters if strict mode reflow is 22295 // cheaper than keeping protruding characters. 22296 Style.ColumnLimit = 21; 22297 verifyFormat("// foo foo foo foo\n" 22298 "// foo foo foo foo\n" 22299 "// foo foo foo foo", 22300 "// foo foo foo foo foo foo foo foo foo foo foo foo", Style); 22301 22302 verifyFormat("int a = /* long block\n" 22303 " comment */\n" 22304 " 42;", 22305 "int a = /* long block comment */ 42;", Style); 22306 } 22307 22308 TEST_F(FormatTest, BreakPenaltyAfterLParen) { 22309 FormatStyle Style = getLLVMStyle(); 22310 Style.ColumnLimit = 8; 22311 Style.PenaltyExcessCharacter = 15; 22312 verifyFormat("int foo(\n" 22313 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 22314 Style); 22315 Style.PenaltyBreakOpenParenthesis = 200; 22316 verifyFormat("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);", 22317 "int foo(\n" 22318 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 22319 Style); 22320 } 22321 22322 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) { 22323 FormatStyle Style = getLLVMStyle(); 22324 Style.ColumnLimit = 5; 22325 Style.PenaltyExcessCharacter = 150; 22326 verifyFormat("foo((\n" 22327 " int)aaaaaaaaaaaaaaaaaaaaaaaa);", 22328 22329 Style); 22330 Style.PenaltyBreakOpenParenthesis = 100'000; 22331 verifyFormat("foo((int)\n" 22332 " aaaaaaaaaaaaaaaaaaaaaaaa);", 22333 "foo((\n" 22334 "int)aaaaaaaaaaaaaaaaaaaaaaaa);", 22335 Style); 22336 } 22337 22338 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) { 22339 FormatStyle Style = getLLVMStyle(); 22340 Style.ColumnLimit = 4; 22341 Style.PenaltyExcessCharacter = 100; 22342 verifyFormat("for (\n" 22343 " int iiiiiiiiiiiiiiiii =\n" 22344 " 0;\n" 22345 " iiiiiiiiiiiiiiiii <\n" 22346 " 2;\n" 22347 " iiiiiiiiiiiiiiiii++) {\n" 22348 "}", 22349 22350 Style); 22351 Style.PenaltyBreakOpenParenthesis = 1250; 22352 verifyFormat("for (int iiiiiiiiiiiiiiiii =\n" 22353 " 0;\n" 22354 " iiiiiiiiiiiiiiiii <\n" 22355 " 2;\n" 22356 " iiiiiiiiiiiiiiiii++) {\n" 22357 "}", 22358 "for (\n" 22359 " int iiiiiiiiiiiiiiiii =\n" 22360 " 0;\n" 22361 " iiiiiiiiiiiiiiiii <\n" 22362 " 2;\n" 22363 " iiiiiiiiiiiiiiiii++) {\n" 22364 "}", 22365 Style); 22366 } 22367 22368 TEST_F(FormatTest, BreakPenaltyBeforeMemberAccess) { 22369 auto Style = getLLVMStyle(); 22370 EXPECT_EQ(Style.PenaltyBreakBeforeMemberAccess, 150u); 22371 22372 Style.ColumnLimit = 60; 22373 Style.PenaltyBreakBeforeMemberAccess = 110; 22374 verifyFormat("aaaaaaaa.aaaaaaaa.bbbbbbbb()\n" 22375 " .ccccccccccccccccccccc(dddddddd);\n" 22376 "aaaaaaaa.aaaaaaaa\n" 22377 " .bbbbbbbb(cccccccccccccccccccccccccccccccc);", 22378 Style); 22379 22380 Style.ColumnLimit = 13; 22381 verifyFormat("foo->bar\n" 22382 " .b(a);", 22383 Style); 22384 } 22385 22386 TEST_F(FormatTest, BreakPenaltyScopeResolution) { 22387 FormatStyle Style = getLLVMStyle(); 22388 Style.ColumnLimit = 20; 22389 Style.PenaltyExcessCharacter = 100; 22390 verifyFormat("unsigned long\n" 22391 "foo::bar();", 22392 Style); 22393 Style.PenaltyBreakScopeResolution = 10; 22394 verifyFormat("unsigned long foo::\n" 22395 " bar();", 22396 Style); 22397 } 22398 22399 TEST_F(FormatTest, WorksFor8bitEncodings) { 22400 // FIXME: unstable test case 22401 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 22402 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 22403 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 22404 "\"\xef\xee\xf0\xf3...\"", 22405 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 22406 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 22407 "\xef\xee\xf0\xf3...\"", 22408 getLLVMStyleWithColumns(12))); 22409 } 22410 22411 TEST_F(FormatTest, HandlesUTF8BOM) { 22412 verifyFormat("\xef\xbb\xbf"); 22413 verifyFormat("\xef\xbb\xbf#include <iostream>"); 22414 verifyFormat("\xef\xbb\xbf\n#include <iostream>"); 22415 22416 auto Style = getLLVMStyle(); 22417 Style.KeepEmptyLines.AtStartOfFile = false; 22418 verifyFormat("\xef\xbb\xbf#include <iostream>", 22419 "\xef\xbb\xbf\n#include <iostream>", Style); 22420 } 22421 22422 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 22423 #if !defined(_MSC_VER) 22424 22425 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 22426 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 22427 getLLVMStyleWithColumns(35)); 22428 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 22429 getLLVMStyleWithColumns(31)); 22430 verifyFormat("// Однажды в студёную зимнюю пору...", 22431 getLLVMStyleWithColumns(36)); 22432 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 22433 verifyFormat("/* Однажды в студёную зимнюю пору... */", 22434 getLLVMStyleWithColumns(39)); 22435 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 22436 getLLVMStyleWithColumns(35)); 22437 } 22438 22439 TEST_F(FormatTest, SplitsUTF8Strings) { 22440 // Non-printable characters' width is currently considered to be the length in 22441 // bytes in UTF8. The characters can be displayed in very different manner 22442 // (zero-width, single width with a substitution glyph, expanded to their code 22443 // (e.g. "<8d>"), so there's no single correct way to handle them. 22444 // FIXME: unstable test case 22445 EXPECT_EQ("\"aaaaÄ\"\n" 22446 "\"\xc2\x8d\";", 22447 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 22448 // FIXME: unstable test case 22449 EXPECT_EQ("\"aaaaaaaÄ\"\n" 22450 "\"\xc2\x8d\";", 22451 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 22452 // FIXME: unstable test case 22453 EXPECT_EQ("\"Однажды, в \"\n" 22454 "\"студёную \"\n" 22455 "\"зимнюю \"\n" 22456 "\"пору,\"", 22457 format("\"Однажды, в студёную зимнюю пору,\"", 22458 getLLVMStyleWithColumns(13))); 22459 // FIXME: unstable test case 22460 EXPECT_EQ( 22461 "\"一 二 三 \"\n" 22462 "\"四 五六 \"\n" 22463 "\"七 八 九 \"\n" 22464 "\"十\"", 22465 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 22466 // FIXME: unstable test case 22467 EXPECT_EQ("\"一\t\"\n" 22468 "\"二 \t\"\n" 22469 "\"三 四 \"\n" 22470 "\"五\t\"\n" 22471 "\"六 \t\"\n" 22472 "\"七 \"\n" 22473 "\"八九十\tqq\"", 22474 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 22475 getLLVMStyleWithColumns(11))); 22476 22477 // UTF8 character in an escape sequence. 22478 // FIXME: unstable test case 22479 EXPECT_EQ("\"aaaaaa\"\n" 22480 "\"\\\xC2\x8D\"", 22481 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 22482 } 22483 22484 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 22485 verifyFormat("const char *sssss =\n" 22486 " \"一二三四五六七八\\\n" 22487 " 九 十\";", 22488 "const char *sssss = \"一二三四五六七八\\\n" 22489 " 九 十\";", 22490 getLLVMStyleWithColumns(30)); 22491 } 22492 22493 TEST_F(FormatTest, SplitsUTF8LineComments) { 22494 verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)); 22495 verifyFormat("// Я из лесу\n" 22496 "// вышел; был\n" 22497 "// сильный\n" 22498 "// мороз.", 22499 "// Я из лесу вышел; был сильный мороз.", 22500 getLLVMStyleWithColumns(13)); 22501 verifyFormat("// 一二三\n" 22502 "// 四五六七\n" 22503 "// 八 九\n" 22504 "// 十", 22505 "// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)); 22506 } 22507 22508 TEST_F(FormatTest, SplitsUTF8BlockComments) { 22509 verifyFormat("/* Гляжу,\n" 22510 " * поднимается\n" 22511 " * медленно в\n" 22512 " * гору\n" 22513 " * Лошадка,\n" 22514 " * везущая\n" 22515 " * хворосту\n" 22516 " * воз. */", 22517 "/* Гляжу, поднимается медленно в гору\n" 22518 " * Лошадка, везущая хворосту воз. */", 22519 getLLVMStyleWithColumns(13)); 22520 verifyFormat("/* 一二三\n" 22521 " * 四五六七\n" 22522 " * 八 九\n" 22523 " * 十 */", 22524 "/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)); 22525 verifyFormat("/* \n" 22526 " * \n" 22527 " * - */", 22528 "/* - */", getLLVMStyleWithColumns(12)); 22529 } 22530 22531 #endif // _MSC_VER 22532 22533 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 22534 FormatStyle Style = getLLVMStyle(); 22535 22536 Style.ConstructorInitializerIndentWidth = 4; 22537 verifyFormat( 22538 "SomeClass::Constructor()\n" 22539 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 22540 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 22541 Style); 22542 22543 Style.ConstructorInitializerIndentWidth = 2; 22544 verifyFormat( 22545 "SomeClass::Constructor()\n" 22546 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 22547 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 22548 Style); 22549 22550 Style.ConstructorInitializerIndentWidth = 0; 22551 verifyFormat( 22552 "SomeClass::Constructor()\n" 22553 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 22554 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 22555 Style); 22556 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 22557 verifyFormat( 22558 "SomeLongTemplateVariableName<\n" 22559 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 22560 Style); 22561 verifyFormat("bool smaller = 1 < " 22562 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 22563 " " 22564 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 22565 Style); 22566 22567 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 22568 verifyFormat("SomeClass::Constructor() :\n" 22569 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 22570 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 22571 Style); 22572 } 22573 22574 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 22575 FormatStyle Style = getLLVMStyle(); 22576 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 22577 Style.ConstructorInitializerIndentWidth = 4; 22578 verifyFormat("SomeClass::Constructor()\n" 22579 " : a(a)\n" 22580 " , b(b)\n" 22581 " , c(c) {}", 22582 Style); 22583 verifyFormat("SomeClass::Constructor()\n" 22584 " : a(a) {}", 22585 Style); 22586 22587 Style.ColumnLimit = 0; 22588 verifyFormat("SomeClass::Constructor()\n" 22589 " : a(a) {}", 22590 Style); 22591 verifyFormat("SomeClass::Constructor() noexcept\n" 22592 " : a(a) {}", 22593 Style); 22594 verifyFormat("SomeClass::Constructor()\n" 22595 " : a(a)\n" 22596 " , b(b)\n" 22597 " , c(c) {}", 22598 Style); 22599 verifyFormat("SomeClass::Constructor()\n" 22600 " : a(a) {\n" 22601 " foo();\n" 22602 " bar();\n" 22603 "}", 22604 Style); 22605 22606 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 22607 verifyFormat("SomeClass::Constructor()\n" 22608 " : a(a)\n" 22609 " , b(b)\n" 22610 " , c(c) {\n}", 22611 Style); 22612 verifyFormat("SomeClass::Constructor()\n" 22613 " : a(a) {\n}", 22614 Style); 22615 22616 Style.ColumnLimit = 80; 22617 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 22618 Style.ConstructorInitializerIndentWidth = 2; 22619 verifyFormat("SomeClass::Constructor()\n" 22620 " : a(a)\n" 22621 " , b(b)\n" 22622 " , c(c) {}", 22623 Style); 22624 22625 Style.ConstructorInitializerIndentWidth = 0; 22626 verifyFormat("SomeClass::Constructor()\n" 22627 ": a(a)\n" 22628 ", b(b)\n" 22629 ", c(c) {}", 22630 Style); 22631 22632 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 22633 Style.ConstructorInitializerIndentWidth = 4; 22634 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 22635 verifyFormat( 22636 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)", 22637 Style); 22638 verifyFormat( 22639 "SomeClass::Constructor()\n" 22640 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 22641 Style); 22642 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 22643 verifyFormat("SomeClass::Constructor()\n" 22644 " : aaaaaaaa(aaaaaaaa) {}", 22645 Style); 22646 verifyFormat("SomeClass::Constructor()\n" 22647 " : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)", 22648 Style); 22649 verifyFormat( 22650 "SomeClass::Constructor()\n" 22651 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 22652 Style); 22653 22654 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 22655 Style.ConstructorInitializerIndentWidth = 4; 22656 Style.ColumnLimit = 60; 22657 verifyFormat("SomeClass::Constructor()\n" 22658 " : aaaaaaaa(aaaaaaaa)\n" 22659 " , aaaaaaaa(aaaaaaaa)\n" 22660 " , aaaaaaaa(aaaaaaaa) {}", 22661 Style); 22662 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLineOnly; 22663 verifyFormat("SomeClass::Constructor()\n" 22664 " : aaaaaaaa(aaaaaaaa)\n" 22665 " , aaaaaaaa(aaaaaaaa)\n" 22666 " , aaaaaaaa(aaaaaaaa) {}", 22667 Style); 22668 } 22669 22670 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) { 22671 FormatStyle Style = getLLVMStyle(); 22672 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 22673 Style.ConstructorInitializerIndentWidth = 4; 22674 verifyFormat("SomeClass::Constructor()\n" 22675 " : a{a}\n" 22676 " , b{b} {}", 22677 Style); 22678 verifyFormat("SomeClass::Constructor()\n" 22679 " : a{a}\n" 22680 "#if CONDITION\n" 22681 " , b{b}\n" 22682 "#endif\n" 22683 "{\n}", 22684 Style); 22685 Style.ConstructorInitializerIndentWidth = 2; 22686 verifyFormat("SomeClass::Constructor()\n" 22687 "#if CONDITION\n" 22688 " : a{a}\n" 22689 "#endif\n" 22690 " , b{b}\n" 22691 " , c{c} {\n}", 22692 Style); 22693 Style.ConstructorInitializerIndentWidth = 0; 22694 verifyFormat("SomeClass::Constructor()\n" 22695 ": a{a}\n" 22696 "#ifdef CONDITION\n" 22697 ", b{b}\n" 22698 "#else\n" 22699 ", c{c}\n" 22700 "#endif\n" 22701 ", d{d} {\n}", 22702 Style); 22703 Style.ConstructorInitializerIndentWidth = 4; 22704 verifyFormat("SomeClass::Constructor()\n" 22705 " : a{a}\n" 22706 "#if WINDOWS\n" 22707 "#if DEBUG\n" 22708 " , b{0}\n" 22709 "#else\n" 22710 " , b{1}\n" 22711 "#endif\n" 22712 "#else\n" 22713 "#if DEBUG\n" 22714 " , b{2}\n" 22715 "#else\n" 22716 " , b{3}\n" 22717 "#endif\n" 22718 "#endif\n" 22719 "{\n}", 22720 Style); 22721 verifyFormat("SomeClass::Constructor()\n" 22722 " : a{a}\n" 22723 "#if WINDOWS\n" 22724 " , b{0}\n" 22725 "#if DEBUG\n" 22726 " , c{0}\n" 22727 "#else\n" 22728 " , c{1}\n" 22729 "#endif\n" 22730 "#else\n" 22731 "#if DEBUG\n" 22732 " , c{2}\n" 22733 "#else\n" 22734 " , c{3}\n" 22735 "#endif\n" 22736 " , b{1}\n" 22737 "#endif\n" 22738 "{\n}", 22739 Style); 22740 } 22741 22742 TEST_F(FormatTest, Destructors) { 22743 verifyFormat("void F(int &i) { i.~int(); }"); 22744 verifyFormat("void F(int &i) { i->~int(); }"); 22745 } 22746 22747 TEST_F(FormatTest, FormatsWithWebKitStyle) { 22748 FormatStyle Style = getWebKitStyle(); 22749 22750 // Don't indent in outer namespaces. 22751 verifyFormat("namespace outer {\n" 22752 "int i;\n" 22753 "namespace inner {\n" 22754 " int i;\n" 22755 "} // namespace inner\n" 22756 "} // namespace outer\n" 22757 "namespace other_outer {\n" 22758 "int i;\n" 22759 "}", 22760 Style); 22761 22762 // Don't indent case labels. 22763 verifyFormat("switch (variable) {\n" 22764 "case 1:\n" 22765 "case 2:\n" 22766 " doSomething();\n" 22767 " break;\n" 22768 "default:\n" 22769 " ++variable;\n" 22770 "}", 22771 Style); 22772 22773 // Wrap before binary operators. 22774 verifyFormat( 22775 "void f()\n" 22776 "{\n" 22777 " if (aaaaaaaaaaaaaaaa\n" 22778 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 22779 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 22780 " return;\n" 22781 "}", 22782 "void f() {\n" 22783 "if (aaaaaaaaaaaaaaaa\n" 22784 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 22785 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 22786 "return;\n" 22787 "}", 22788 Style); 22789 22790 // Allow functions on a single line. 22791 verifyFormat("void f() { return; }", Style); 22792 22793 // Allow empty blocks on a single line and insert a space in empty blocks. 22794 verifyFormat("void f() { }", "void f() {}", Style); 22795 verifyFormat("while (true) { }", "while (true) {}", Style); 22796 // However, don't merge non-empty short loops. 22797 verifyFormat("while (true) {\n" 22798 " continue;\n" 22799 "}", 22800 "while (true) { continue; }", Style); 22801 22802 // Constructor initializers are formatted one per line with the "," on the 22803 // new line. 22804 verifyFormat("Constructor()\n" 22805 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 22806 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 22807 " aaaaaaaaaaaaaa)\n" 22808 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 22809 "{\n" 22810 "}", 22811 Style); 22812 verifyFormat("SomeClass::Constructor()\n" 22813 " : a(a)\n" 22814 "{\n" 22815 "}", 22816 Style); 22817 verifyFormat("SomeClass::Constructor()\n" 22818 " : a(a)\n" 22819 "{\n" 22820 "}", 22821 "SomeClass::Constructor():a(a){}", Style); 22822 verifyFormat("SomeClass::Constructor()\n" 22823 " : a(a)\n" 22824 " , b(b)\n" 22825 " , c(c)\n" 22826 "{\n" 22827 "}", 22828 Style); 22829 verifyFormat("SomeClass::Constructor()\n" 22830 " : a(a)\n" 22831 "{\n" 22832 " foo();\n" 22833 " bar();\n" 22834 "}", 22835 Style); 22836 22837 // Access specifiers should be aligned left. 22838 verifyFormat("class C {\n" 22839 "public:\n" 22840 " int i;\n" 22841 "};", 22842 Style); 22843 22844 // Do not align comments. 22845 verifyFormat("int a; // Do not\n" 22846 "double b; // align comments.", 22847 Style); 22848 22849 // Do not align operands. 22850 verifyFormat("ASSERT(aaaa\n" 22851 " || bbbb);", 22852 "ASSERT ( aaaa\n||bbbb);", Style); 22853 22854 // Accept input's line breaks. 22855 verifyFormat("if (aaaaaaaaaaaaaaa\n" 22856 " || bbbbbbbbbbbbbbb) {\n" 22857 " i++;\n" 22858 "}", 22859 "if (aaaaaaaaaaaaaaa\n" 22860 "|| bbbbbbbbbbbbbbb) { i++; }", 22861 Style); 22862 verifyFormat("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 22863 " i++;\n" 22864 "}", 22865 "if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style); 22866 22867 // Don't automatically break all macro definitions (llvm.org/PR17842). 22868 verifyFormat("#define aNumber 10", Style); 22869 // However, generally keep the line breaks that the user authored. 22870 verifyFormat("#define aNumber \\\n" 22871 " 10", 22872 "#define aNumber \\\n" 22873 " 10", 22874 Style); 22875 22876 // Keep empty and one-element array literals on a single line. 22877 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 22878 " copyItems:YES];", 22879 "NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 22880 "copyItems:YES];", 22881 Style); 22882 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 22883 " copyItems:YES];", 22884 "NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 22885 " copyItems:YES];", 22886 Style); 22887 // FIXME: This does not seem right, there should be more indentation before 22888 // the array literal's entries. Nested blocks have the same problem. 22889 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 22890 " @\"a\",\n" 22891 " @\"a\"\n" 22892 "]\n" 22893 " copyItems:YES];", 22894 "NSArray* a = [[NSArray alloc] initWithArray:@[\n" 22895 " @\"a\",\n" 22896 " @\"a\"\n" 22897 " ]\n" 22898 " copyItems:YES];", 22899 Style); 22900 verifyFormat( 22901 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 22902 " copyItems:YES];", 22903 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 22904 " copyItems:YES];", 22905 Style); 22906 22907 verifyFormat("[self.a b:c c:d];", Style); 22908 verifyFormat("[self.a b:c\n" 22909 " c:d];", 22910 "[self.a b:c\n" 22911 "c:d];", 22912 Style); 22913 } 22914 22915 TEST_F(FormatTest, FormatsLambdas) { 22916 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();"); 22917 verifyFormat( 22918 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();"); 22919 verifyFormat("int c = [&] { [=] { return b++; }(); }();"); 22920 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();"); 22921 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();"); 22922 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}"); 22923 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}"); 22924 verifyFormat("auto c = [a = [b = 42] {}] {};"); 22925 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};"); 22926 verifyFormat("int x = f(*+[] {});"); 22927 verifyFormat("void f() {\n" 22928 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 22929 "}"); 22930 verifyFormat("void f() {\n" 22931 " other(x.begin(), //\n" 22932 " x.end(), //\n" 22933 " [&](int, int) { return 1; });\n" 22934 "}"); 22935 verifyFormat("void f() {\n" 22936 " other.other.other.other.other(\n" 22937 " x.begin(), x.end(),\n" 22938 " [something, rather](int, int, int, int, int, int, int) { " 22939 "return 1; });\n" 22940 "}"); 22941 verifyFormat( 22942 "void f() {\n" 22943 " other.other.other.other.other(\n" 22944 " x.begin(), x.end(),\n" 22945 " [something, rather](int, int, int, int, int, int, int) {\n" 22946 " //\n" 22947 " });\n" 22948 "}"); 22949 verifyFormat("SomeFunction([]() { // A cool function...\n" 22950 " return 43;\n" 22951 "});"); 22952 verifyFormat("SomeFunction([]() {\n" 22953 "#define A a\n" 22954 " return 43;\n" 22955 "});", 22956 "SomeFunction([](){\n" 22957 "#define A a\n" 22958 "return 43;\n" 22959 "});"); 22960 verifyFormat("void f() {\n" 22961 " SomeFunction([](decltype(x), A *a) {});\n" 22962 " SomeFunction([](typeof(x), A *a) {});\n" 22963 " SomeFunction([](_Atomic(x), A *a) {});\n" 22964 " SomeFunction([](__underlying_type(x), A *a) {});\n" 22965 "}"); 22966 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22967 " [](const aaaaaaaaaa &a) { return a; });"); 22968 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 22969 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 22970 "});"); 22971 verifyFormat("Constructor()\n" 22972 " : Field([] { // comment\n" 22973 " int i;\n" 22974 " }) {}"); 22975 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 22976 " return some_parameter.size();\n" 22977 "};"); 22978 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 22979 " [](const string &s) { return s; };"); 22980 verifyFormat("int i = aaaaaa ? 1 //\n" 22981 " : [] {\n" 22982 " return 2; //\n" 22983 " }();"); 22984 verifyFormat("llvm::errs() << \"number of twos is \"\n" 22985 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 22986 " return x == 2; // force break\n" 22987 " });"); 22988 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22989 " [=](int iiiiiiiiiiii) {\n" 22990 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 22991 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 22992 " });", 22993 getLLVMStyleWithColumns(60)); 22994 22995 verifyFormat("SomeFunction({[&] {\n" 22996 " // comment\n" 22997 " },\n" 22998 " [&] {\n" 22999 " // comment\n" 23000 " }});"); 23001 verifyFormat("SomeFunction({[&] {\n" 23002 " // comment\n" 23003 "}});"); 23004 verifyFormat( 23005 "virtual aaaaaaaaaaaaaaaa(\n" 23006 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 23007 " aaaaa aaaaaaaaa);"); 23008 23009 // Lambdas with return types. 23010 verifyFormat("int c = []() -> int { return 2; }();"); 23011 verifyFormat("int c = []() -> int * { return 2; }();"); 23012 verifyFormat("int c = []() -> vector<int> { return {2}; }();"); 23013 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 23014 verifyFormat("foo([]() noexcept -> int {});"); 23015 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 23016 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 23017 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 23018 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 23019 verifyFormat("[a, a]() -> a<1> {};"); 23020 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 23021 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 23022 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 23023 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 23024 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 23025 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 23026 verifyFormat("[]() -> foo<!5> { return {}; };"); 23027 verifyFormat("[]() -> foo<~5> { return {}; };"); 23028 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 23029 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 23030 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 23031 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 23032 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 23033 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 23034 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 23035 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 23036 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 23037 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 23038 verifyFormat("namespace bar {\n" 23039 "// broken:\n" 23040 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 23041 "} // namespace bar"); 23042 verifyFormat("namespace bar {\n" 23043 "// broken:\n" 23044 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 23045 "} // namespace bar"); 23046 verifyFormat("namespace bar {\n" 23047 "// broken:\n" 23048 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 23049 "} // namespace bar"); 23050 verifyFormat("namespace bar {\n" 23051 "// broken:\n" 23052 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 23053 "} // namespace bar"); 23054 verifyFormat("namespace bar {\n" 23055 "// broken:\n" 23056 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 23057 "} // namespace bar"); 23058 verifyFormat("namespace bar {\n" 23059 "// broken:\n" 23060 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 23061 "} // namespace bar"); 23062 verifyFormat("namespace bar {\n" 23063 "// broken:\n" 23064 "auto foo{[]() -> foo<!5> { return {}; }};\n" 23065 "} // namespace bar"); 23066 verifyFormat("namespace bar {\n" 23067 "// broken:\n" 23068 "auto foo{[]() -> foo<~5> { return {}; }};\n" 23069 "} // namespace bar"); 23070 verifyFormat("namespace bar {\n" 23071 "// broken:\n" 23072 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 23073 "} // namespace bar"); 23074 verifyFormat("namespace bar {\n" 23075 "// broken:\n" 23076 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 23077 "} // namespace bar"); 23078 verifyFormat("namespace bar {\n" 23079 "// broken:\n" 23080 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 23081 "} // namespace bar"); 23082 verifyFormat("namespace bar {\n" 23083 "// broken:\n" 23084 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 23085 "} // namespace bar"); 23086 verifyFormat("namespace bar {\n" 23087 "// broken:\n" 23088 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 23089 "} // namespace bar"); 23090 verifyFormat("namespace bar {\n" 23091 "// broken:\n" 23092 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 23093 "} // namespace bar"); 23094 verifyFormat("namespace bar {\n" 23095 "// broken:\n" 23096 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 23097 "} // namespace bar"); 23098 verifyFormat("namespace bar {\n" 23099 "// broken:\n" 23100 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 23101 "} // namespace bar"); 23102 verifyFormat("namespace bar {\n" 23103 "// broken:\n" 23104 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 23105 "} // namespace bar"); 23106 verifyFormat("namespace bar {\n" 23107 "// broken:\n" 23108 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 23109 "} // namespace bar"); 23110 verifyFormat("[]() -> a<1> {};"); 23111 verifyFormat("[]() -> a<1> { ; };"); 23112 verifyFormat("[]() -> a<1> { ; }();"); 23113 verifyFormat("[a, a]() -> a<true> {};"); 23114 verifyFormat("[]() -> a<true> {};"); 23115 verifyFormat("[]() -> a<true> { ; };"); 23116 verifyFormat("[]() -> a<true> { ; }();"); 23117 verifyFormat("[a, a]() -> a<false> {};"); 23118 verifyFormat("[]() -> a<false> {};"); 23119 verifyFormat("[]() -> a<false> { ; };"); 23120 verifyFormat("[]() -> a<false> { ; }();"); 23121 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 23122 verifyFormat("namespace bar {\n" 23123 "auto foo{[]() -> foo<false> { ; }};\n" 23124 "} // namespace bar"); 23125 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 23126 " int j) -> int {\n" 23127 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 23128 "};"); 23129 verifyFormat( 23130 "aaaaaaaaaaaaaaaaaaaaaa(\n" 23131 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 23132 " return aaaaaaaaaaaaaaaaa;\n" 23133 " });", 23134 getLLVMStyleWithColumns(70)); 23135 verifyFormat("[]() //\n" 23136 " -> int {\n" 23137 " return 1; //\n" 23138 "};"); 23139 verifyFormat("[]() -> Void<T...> {};"); 23140 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 23141 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});"); 23142 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});"); 23143 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});"); 23144 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});"); 23145 verifyFormat("foo([&](u32 bar) __attribute__((always_inline)) -> void {});"); 23146 verifyFormat("return int{[x = x]() { return x; }()};"); 23147 23148 // Lambdas with explicit template argument lists. 23149 verifyFormat( 23150 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};"); 23151 verifyFormat("auto L = []<class T>(T) {\n" 23152 " {\n" 23153 " f();\n" 23154 " g();\n" 23155 " }\n" 23156 "};"); 23157 verifyFormat("auto L = []<class... T>(T...) {\n" 23158 " {\n" 23159 " f();\n" 23160 " g();\n" 23161 " }\n" 23162 "};"); 23163 verifyFormat("auto L = []<typename... T>(T...) {\n" 23164 " {\n" 23165 " f();\n" 23166 " g();\n" 23167 " }\n" 23168 "};"); 23169 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n" 23170 " {\n" 23171 " f();\n" 23172 " g();\n" 23173 " }\n" 23174 "};"); 23175 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n" 23176 " {\n" 23177 " f();\n" 23178 " g();\n" 23179 " }\n" 23180 "};"); 23181 verifyFormat("auto L = []<int... T>(T...) {\n" 23182 " {\n" 23183 " f();\n" 23184 " g();\n" 23185 " }\n" 23186 "};"); 23187 verifyFormat("auto L = []<Foo... T>(T...) {\n" 23188 " {\n" 23189 " f();\n" 23190 " g();\n" 23191 " }\n" 23192 "};"); 23193 23194 // Lambdas that fit on a single line within an argument list are not forced 23195 // onto new lines. 23196 verifyFormat("SomeFunction([] {});"); 23197 verifyFormat("SomeFunction(0, [] {});"); 23198 verifyFormat("SomeFunction([] {}, 0);"); 23199 verifyFormat("SomeFunction(0, [] {}, 0);"); 23200 verifyFormat("SomeFunction([] { return 0; }, 0);"); 23201 verifyFormat("SomeFunction(a, [] { return 0; }, b);"); 23202 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; });"); 23203 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; }, b);"); 23204 verifyFormat("auto loooooooooooooooooooooooooooong =\n" 23205 " SomeFunction([] { return 0; }, [] { return 0; }, b);"); 23206 // Exceeded column limit. We need to break. 23207 verifyFormat("auto loooooooooooooooooooooooooooongName = SomeFunction(\n" 23208 " [] { return anotherLooooooooooonoooooooongName; }, [] { " 23209 "return 0; }, b);"); 23210 23211 // Multiple multi-line lambdas in the same parentheses change indentation 23212 // rules. These lambdas are always forced to start on new lines. 23213 verifyFormat("SomeFunction(\n" 23214 " []() {\n" 23215 " //\n" 23216 " },\n" 23217 " []() {\n" 23218 " //\n" 23219 " });"); 23220 23221 // A multi-line lambda passed as arg0 is always pushed to the next line. 23222 verifyFormat("SomeFunction(\n" 23223 " [this] {\n" 23224 " //\n" 23225 " },\n" 23226 " 1);"); 23227 23228 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 23229 // the arg0 case above. 23230 auto Style = getGoogleStyle(); 23231 Style.BinPackArguments = false; 23232 verifyFormat("SomeFunction(\n" 23233 " a,\n" 23234 " [this] {\n" 23235 " //\n" 23236 " },\n" 23237 " b);", 23238 Style); 23239 verifyFormat("SomeFunction(\n" 23240 " a,\n" 23241 " [this] {\n" 23242 " //\n" 23243 " },\n" 23244 " b);"); 23245 23246 // A lambda with a very long line forces arg0 to be pushed out irrespective of 23247 // the BinPackArguments value (as long as the code is wide enough). 23248 verifyFormat( 23249 "something->SomeFunction(\n" 23250 " a,\n" 23251 " [this] {\n" 23252 " " 23253 "D0000000000000000000000000000000000000000000000000000000000001();\n" 23254 " },\n" 23255 " b);"); 23256 23257 // A multi-line lambda is pulled up as long as the introducer fits on the 23258 // previous line and there are no further args. 23259 verifyFormat("function(1, [this, that] {\n" 23260 " //\n" 23261 "});"); 23262 verifyFormat("function([this, that] {\n" 23263 " //\n" 23264 "});"); 23265 // FIXME: this format is not ideal and we should consider forcing the first 23266 // arg onto its own line. 23267 verifyFormat("function(a, b, c, //\n" 23268 " d, [this, that] {\n" 23269 " //\n" 23270 " });"); 23271 23272 // Multiple lambdas are treated correctly even when there is a short arg0. 23273 verifyFormat("SomeFunction(\n" 23274 " 1,\n" 23275 " [this] {\n" 23276 " //\n" 23277 " },\n" 23278 " [this] {\n" 23279 " //\n" 23280 " },\n" 23281 " 1);"); 23282 23283 // More complex introducers. 23284 verifyFormat("return [i, args...] {};"); 23285 23286 // Not lambdas. 23287 verifyFormat("constexpr char hello[]{\"hello\"};"); 23288 verifyFormat("double &operator[](int i) { return 0; }\n" 23289 "int i;"); 23290 verifyFormat("std::unique_ptr<int[]> foo() {}"); 23291 verifyFormat("int i = a[a][a]->f();"); 23292 verifyFormat("int i = (*b)[a]->f();"); 23293 23294 // Other corner cases. 23295 verifyFormat("void f() {\n" 23296 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 23297 " );\n" 23298 "}"); 23299 verifyFormat("auto k = *[](int *j) { return j; }(&i);"); 23300 23301 // Lambdas created through weird macros. 23302 verifyFormat("void f() {\n" 23303 " MACRO((const AA &a) { return 1; });\n" 23304 " MACRO((AA &a) { return 1; });\n" 23305 "}"); 23306 23307 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 23308 " doo_dah();\n" 23309 " doo_dah();\n" 23310 " })) {\n" 23311 "}"); 23312 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 23313 " doo_dah();\n" 23314 " doo_dah();\n" 23315 " })) {\n" 23316 "}"); 23317 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 23318 " doo_dah();\n" 23319 " doo_dah();\n" 23320 " })) {\n" 23321 "}"); 23322 verifyFormat("auto lambda = []() {\n" 23323 " int a = 2\n" 23324 "#if A\n" 23325 " + 2\n" 23326 "#endif\n" 23327 " ;\n" 23328 "};"); 23329 23330 // Lambdas with complex multiline introducers. 23331 verifyFormat( 23332 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 23333 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 23334 " -> ::std::unordered_set<\n" 23335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 23336 " //\n" 23337 " });"); 23338 23339 FormatStyle LLVMStyle = getLLVMStyleWithColumns(60); 23340 verifyFormat("very_long_function_name_yes_it_is_really_long(\n" 23341 " [](auto n) noexcept [[back_attr]]\n" 23342 " -> std::unordered_map<very_long_type_name_A,\n" 23343 " very_long_type_name_B> {\n" 23344 " really_do_something();\n" 23345 " });", 23346 LLVMStyle); 23347 verifyFormat("very_long_function_name_yes_it_is_really_long(\n" 23348 " [](auto n) constexpr\n" 23349 " -> std::unordered_map<very_long_type_name_A,\n" 23350 " very_long_type_name_B> {\n" 23351 " really_do_something();\n" 23352 " });", 23353 LLVMStyle); 23354 23355 FormatStyle DoNotMerge = getLLVMStyle(); 23356 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 23357 verifyFormat("auto c = []() {\n" 23358 " return b;\n" 23359 "};", 23360 "auto c = []() { return b; };", DoNotMerge); 23361 verifyFormat("auto c = []() {\n" 23362 "};", 23363 " auto c = []() {};", DoNotMerge); 23364 23365 FormatStyle MergeEmptyOnly = getLLVMStyle(); 23366 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 23367 verifyFormat("auto c = []() {\n" 23368 " return b;\n" 23369 "};", 23370 "auto c = []() {\n" 23371 " return b;\n" 23372 " };", 23373 MergeEmptyOnly); 23374 verifyFormat("auto c = []() {};", 23375 "auto c = []() {\n" 23376 "};", 23377 MergeEmptyOnly); 23378 23379 FormatStyle MergeInline = getLLVMStyle(); 23380 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 23381 verifyFormat("auto c = []() {\n" 23382 " return b;\n" 23383 "};", 23384 "auto c = []() { return b; };", MergeInline); 23385 verifyFormat("function([]() { return b; })", MergeInline); 23386 verifyFormat("function([]() { return b; }, a)", MergeInline); 23387 verifyFormat("function(a, []() { return b; })", MergeInline); 23388 23389 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 23390 // AllowShortLambdasOnASingleLine 23391 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 23392 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 23393 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 23394 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 23395 FormatStyle::ShortLambdaStyle::SLS_None; 23396 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 23397 " []()\n" 23398 " {\n" 23399 " return 17;\n" 23400 " });", 23401 LLVMWithBeforeLambdaBody); 23402 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 23403 " []()\n" 23404 " {\n" 23405 " });", 23406 LLVMWithBeforeLambdaBody); 23407 verifyFormat("auto fct_SLS_None = []()\n" 23408 "{\n" 23409 " return 17;\n" 23410 "};", 23411 LLVMWithBeforeLambdaBody); 23412 verifyFormat("TwoNestedLambdas_SLS_None(\n" 23413 " []()\n" 23414 " {\n" 23415 " return Call(\n" 23416 " []()\n" 23417 " {\n" 23418 " return 17;\n" 23419 " });\n" 23420 " });", 23421 LLVMWithBeforeLambdaBody); 23422 verifyFormat("void Fct() {\n" 23423 " return {[]()\n" 23424 " {\n" 23425 " return 17;\n" 23426 " }};\n" 23427 "}", 23428 LLVMWithBeforeLambdaBody); 23429 23430 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 23431 FormatStyle::ShortLambdaStyle::SLS_Empty; 23432 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 23433 " []()\n" 23434 " {\n" 23435 " return 17;\n" 23436 " });", 23437 LLVMWithBeforeLambdaBody); 23438 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 23439 LLVMWithBeforeLambdaBody); 23440 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 23441 "ongFunctionName_SLS_Empty(\n" 23442 " []() {});", 23443 LLVMWithBeforeLambdaBody); 23444 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 23445 " []()\n" 23446 " {\n" 23447 " return 17;\n" 23448 " });", 23449 LLVMWithBeforeLambdaBody); 23450 verifyFormat("auto fct_SLS_Empty = []()\n" 23451 "{\n" 23452 " return 17;\n" 23453 "};", 23454 LLVMWithBeforeLambdaBody); 23455 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 23456 " []()\n" 23457 " {\n" 23458 " return Call([]() {});\n" 23459 " });", 23460 LLVMWithBeforeLambdaBody); 23461 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 23462 " []()\n" 23463 " {\n" 23464 " return Call([]() {});\n" 23465 " });", 23466 LLVMWithBeforeLambdaBody); 23467 verifyFormat( 23468 "FctWithLongLineInLambda_SLS_Empty(\n" 23469 " []()\n" 23470 " {\n" 23471 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 23472 " AndShouldNotBeConsiderAsInline,\n" 23473 " LambdaBodyMustBeBreak);\n" 23474 " });", 23475 LLVMWithBeforeLambdaBody); 23476 23477 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 23478 FormatStyle::ShortLambdaStyle::SLS_Inline; 23479 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 23480 LLVMWithBeforeLambdaBody); 23481 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 23482 LLVMWithBeforeLambdaBody); 23483 verifyFormat("auto fct_SLS_Inline = []()\n" 23484 "{\n" 23485 " return 17;\n" 23486 "};", 23487 LLVMWithBeforeLambdaBody); 23488 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 23489 "17; }); });", 23490 LLVMWithBeforeLambdaBody); 23491 verifyFormat( 23492 "FctWithLongLineInLambda_SLS_Inline(\n" 23493 " []()\n" 23494 " {\n" 23495 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 23496 " AndShouldNotBeConsiderAsInline,\n" 23497 " LambdaBodyMustBeBreak);\n" 23498 " });", 23499 LLVMWithBeforeLambdaBody); 23500 verifyFormat("FctWithMultipleParams_SLS_Inline(" 23501 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 23502 " []() { return 17; });", 23503 LLVMWithBeforeLambdaBody); 23504 verifyFormat( 23505 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 23506 LLVMWithBeforeLambdaBody); 23507 23508 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 23509 FormatStyle::ShortLambdaStyle::SLS_All; 23510 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 23511 LLVMWithBeforeLambdaBody); 23512 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 23513 LLVMWithBeforeLambdaBody); 23514 verifyFormat("auto fct_SLS_All = []() { return 17; };", 23515 LLVMWithBeforeLambdaBody); 23516 verifyFormat("FctWithOneParam_SLS_All(\n" 23517 " []()\n" 23518 " {\n" 23519 " // A cool function...\n" 23520 " return 43;\n" 23521 " });", 23522 LLVMWithBeforeLambdaBody); 23523 verifyFormat("FctWithMultipleParams_SLS_All(" 23524 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 23525 " []() { return 17; });", 23526 LLVMWithBeforeLambdaBody); 23527 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 23528 LLVMWithBeforeLambdaBody); 23529 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 23530 LLVMWithBeforeLambdaBody); 23531 verifyFormat( 23532 "FctWithLongLineInLambda_SLS_All(\n" 23533 " []()\n" 23534 " {\n" 23535 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 23536 " AndShouldNotBeConsiderAsInline,\n" 23537 " LambdaBodyMustBeBreak);\n" 23538 " });", 23539 LLVMWithBeforeLambdaBody); 23540 verifyFormat( 23541 "auto fct_SLS_All = []()\n" 23542 "{\n" 23543 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 23544 " AndShouldNotBeConsiderAsInline,\n" 23545 " LambdaBodyMustBeBreak);\n" 23546 "};", 23547 LLVMWithBeforeLambdaBody); 23548 LLVMWithBeforeLambdaBody.BinPackParameters = FormatStyle::BPPS_OnePerLine; 23549 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 23550 LLVMWithBeforeLambdaBody); 23551 verifyFormat( 23552 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 23553 " FirstParam,\n" 23554 " SecondParam,\n" 23555 " ThirdParam,\n" 23556 " FourthParam);", 23557 LLVMWithBeforeLambdaBody); 23558 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 23559 " []() { return " 23560 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 23561 " FirstParam,\n" 23562 " SecondParam,\n" 23563 " ThirdParam,\n" 23564 " FourthParam);", 23565 LLVMWithBeforeLambdaBody); 23566 verifyFormat( 23567 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 23568 " SecondParam,\n" 23569 " ThirdParam,\n" 23570 " FourthParam,\n" 23571 " []() { return SomeValueNotSoLong; });", 23572 LLVMWithBeforeLambdaBody); 23573 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 23574 " []()\n" 23575 " {\n" 23576 " return " 23577 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 23578 "eConsiderAsInline;\n" 23579 " });", 23580 LLVMWithBeforeLambdaBody); 23581 verifyFormat( 23582 "FctWithLongLineInLambda_SLS_All(\n" 23583 " []()\n" 23584 " {\n" 23585 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 23586 " AndShouldNotBeConsiderAsInline,\n" 23587 " LambdaBodyMustBeBreak);\n" 23588 " });", 23589 LLVMWithBeforeLambdaBody); 23590 verifyFormat("FctWithTwoParams_SLS_All(\n" 23591 " []()\n" 23592 " {\n" 23593 " // A cool function...\n" 23594 " return 43;\n" 23595 " },\n" 23596 " 87);", 23597 LLVMWithBeforeLambdaBody); 23598 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 23599 LLVMWithBeforeLambdaBody); 23600 verifyFormat( 23601 "FctWithTwoParams_SLS_All(\n" 23602 " 87, []() { return LongLineThatWillForceBothParamsToNewLine(); });", 23603 LLVMWithBeforeLambdaBody); 23604 verifyFormat( 23605 "FctWithTwoParams_SLS_All(\n" 23606 " 87,\n" 23607 " []()\n" 23608 " {\n" 23609 " return " 23610 "LongLineThatWillForceTheLambdaBodyToBeBrokenIntoMultipleLines();\n" 23611 " });", 23612 LLVMWithBeforeLambdaBody); 23613 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 23614 LLVMWithBeforeLambdaBody); 23615 verifyFormat( 23616 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 23617 LLVMWithBeforeLambdaBody); 23618 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 23619 "}); }, x);", 23620 LLVMWithBeforeLambdaBody); 23621 verifyFormat("TwoNestedLambdas_SLS_All(\n" 23622 " []()\n" 23623 " {\n" 23624 " // A cool function...\n" 23625 " return Call([]() { return 17; });\n" 23626 " });", 23627 LLVMWithBeforeLambdaBody); 23628 verifyFormat("TwoNestedLambdas_SLS_All(\n" 23629 " []()\n" 23630 " {\n" 23631 " return Call(\n" 23632 " []()\n" 23633 " {\n" 23634 " // A cool function...\n" 23635 " return 17;\n" 23636 " });\n" 23637 " });", 23638 LLVMWithBeforeLambdaBody); 23639 23640 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 23641 FormatStyle::ShortLambdaStyle::SLS_None; 23642 23643 verifyFormat("auto select = [this]() -> const Library::Object *\n" 23644 "{\n" 23645 " return MyAssignment::SelectFromList(this);\n" 23646 "};", 23647 LLVMWithBeforeLambdaBody); 23648 23649 verifyFormat("auto select = [this]() -> const Library::Object &\n" 23650 "{\n" 23651 " return MyAssignment::SelectFromList(this);\n" 23652 "};", 23653 LLVMWithBeforeLambdaBody); 23654 23655 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 23656 "{\n" 23657 " return MyAssignment::SelectFromList(this);\n" 23658 "};", 23659 LLVMWithBeforeLambdaBody); 23660 23661 verifyFormat("namespace test {\n" 23662 "class Test {\n" 23663 "public:\n" 23664 " Test() = default;\n" 23665 "};\n" 23666 "} // namespace test", 23667 LLVMWithBeforeLambdaBody); 23668 23669 // Lambdas with different indentation styles. 23670 Style = getLLVMStyleWithColumns(60); 23671 verifyFormat("Result doSomething(Promise promise) {\n" 23672 " return promise.then(\n" 23673 " [this, obj = std::move(s)](int bar) mutable {\n" 23674 " return someObject.startAsyncAction().then(\n" 23675 " [this, &obj](Result result) mutable {\n" 23676 " result.processMore();\n" 23677 " });\n" 23678 " });\n" 23679 "}", 23680 Style); 23681 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 23682 verifyFormat("Result doSomething(Promise promise) {\n" 23683 " return promise.then(\n" 23684 " [this, obj = std::move(s)](int bar) mutable {\n" 23685 " return obj.startAsyncAction().then(\n" 23686 " [this, &obj](Result result) mutable {\n" 23687 " result.processMore();\n" 23688 " });\n" 23689 " });\n" 23690 "}", 23691 Style); 23692 verifyFormat("Result doSomething(Promise promise) {\n" 23693 " return promise.then([this, obj = std::move(s)] {\n" 23694 " return obj.startAsyncAction().then(\n" 23695 " [this, &obj](Result result) mutable {\n" 23696 " result.processMore();\n" 23697 " });\n" 23698 " });\n" 23699 "}", 23700 Style); 23701 verifyFormat("void test() {\n" 23702 " ([]() -> auto {\n" 23703 " int b = 32;\n" 23704 " return 3;\n" 23705 " }).foo();\n" 23706 "}", 23707 Style); 23708 verifyFormat("void test() {\n" 23709 " []() -> auto {\n" 23710 " int b = 32;\n" 23711 " return 3;\n" 23712 " }\n" 23713 "}", 23714 Style); 23715 verifyFormat("void test() {\n" 23716 " std::sort(v.begin(), v.end(),\n" 23717 " [](const auto &foo, const auto &bar) {\n" 23718 " return foo.baz < bar.baz;\n" 23719 " });\n" 23720 "};", 23721 Style); 23722 verifyFormat("void test() {\n" 23723 " (\n" 23724 " []() -> auto {\n" 23725 " int b = 32;\n" 23726 " return 3;\n" 23727 " }, foo, bar)\n" 23728 " .foo();\n" 23729 "}", 23730 Style); 23731 verifyFormat("void test() {\n" 23732 " ([]() -> auto {\n" 23733 " int b = 32;\n" 23734 " return 3;\n" 23735 " })\n" 23736 " .foo()\n" 23737 " .bar();\n" 23738 "}", 23739 Style); 23740 verifyFormat("#define A \\\n" 23741 " [] { \\\n" 23742 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 23743 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 23744 " }", 23745 Style); 23746 verifyFormat("#define SORT(v) \\\n" 23747 " std::sort(v.begin(), v.end(), \\\n" 23748 " [](const auto &foo, const auto &bar) { \\\n" 23749 " return foo.baz < bar.baz; \\\n" 23750 " });", 23751 Style); 23752 verifyFormat("void foo() {\n" 23753 " aFunction(1, b(c(foo, bar, baz, [](d) {\n" 23754 " auto f = e(d);\n" 23755 " return f;\n" 23756 " })));\n" 23757 "}", 23758 Style); 23759 verifyFormat("void foo() {\n" 23760 " aFunction(1, b(c(foo, Bar{}, baz, [](d) -> Foo {\n" 23761 " auto f = e(foo, [&] {\n" 23762 " auto g = h();\n" 23763 " return g;\n" 23764 " }, qux, [&] -> Bar {\n" 23765 " auto i = j();\n" 23766 " return i;\n" 23767 " });\n" 23768 " return f;\n" 23769 " })));\n" 23770 "}", 23771 Style); 23772 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n" 23773 " AnotherLongClassName baz)\n" 23774 " : baz{baz}, func{[&] {\n" 23775 " auto qux = bar;\n" 23776 " return aFunkyFunctionCall(qux);\n" 23777 " }} {}", 23778 Style); 23779 verifyFormat("void foo() {\n" 23780 " class Foo {\n" 23781 " public:\n" 23782 " Foo()\n" 23783 " : qux{[](int quux) {\n" 23784 " auto tmp = quux;\n" 23785 " return tmp;\n" 23786 " }} {}\n" 23787 "\n" 23788 " private:\n" 23789 " std::function<void(int quux)> qux;\n" 23790 " };\n" 23791 "}", 23792 Style); 23793 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 23794 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n" 23795 " AnotherLongClassName baz) :\n" 23796 " baz{baz}, func{[&] {\n" 23797 " auto qux = bar;\n" 23798 " return aFunkyFunctionCall(qux);\n" 23799 " }} {}", 23800 Style); 23801 Style.PackConstructorInitializers = FormatStyle::PCIS_Never; 23802 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n" 23803 " AnotherLongClassName baz) :\n" 23804 " baz{baz},\n" 23805 " func{[&] {\n" 23806 " auto qux = bar;\n" 23807 " return aFunkyFunctionCall(qux);\n" 23808 " }} {}", 23809 Style); 23810 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 23811 // FIXME: The following test should pass, but fails at the time of writing. 23812 #if 0 23813 // As long as all the non-lambda arguments fit on a single line, AlwaysBreak 23814 // doesn't force an initial line break, even if lambdas span multiple lines. 23815 verifyFormat("void foo() {\n" 23816 " aFunction(\n" 23817 " [](d) -> Foo {\n" 23818 " auto f = e(d);\n" 23819 " return f;\n" 23820 " }, foo, Bar{}, [] {\n" 23821 " auto g = h();\n" 23822 " return g;\n" 23823 " }, baz);\n" 23824 "}", 23825 Style); 23826 #endif 23827 // A long non-lambda argument forces arguments to span multiple lines and thus 23828 // forces an initial line break when using AlwaysBreak. 23829 verifyFormat("void foo() {\n" 23830 " aFunction(\n" 23831 " 1,\n" 23832 " [](d) -> Foo {\n" 23833 " auto f = e(d);\n" 23834 " return f;\n" 23835 " }, foo, Bar{},\n" 23836 " [] {\n" 23837 " auto g = h();\n" 23838 " return g;\n" 23839 " }, bazzzzz,\n" 23840 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n" 23841 "}", 23842 Style); 23843 Style.BinPackArguments = false; 23844 verifyFormat("void foo() {\n" 23845 " aFunction(\n" 23846 " 1,\n" 23847 " [](d) -> Foo {\n" 23848 " auto f = e(d);\n" 23849 " return f;\n" 23850 " },\n" 23851 " foo,\n" 23852 " Bar{},\n" 23853 " [] {\n" 23854 " auto g = h();\n" 23855 " return g;\n" 23856 " },\n" 23857 " bazzzzz,\n" 23858 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n" 23859 "}", 23860 Style); 23861 Style.BinPackArguments = true; 23862 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 23863 Style.BraceWrapping.BeforeLambdaBody = true; 23864 verifyFormat("void foo() {\n" 23865 " aFunction(\n" 23866 " 1, b(c(foo, Bar{}, baz, [](d) -> Foo\n" 23867 " {\n" 23868 " auto f = e(\n" 23869 " [&]\n" 23870 " {\n" 23871 " auto g = h();\n" 23872 " return g;\n" 23873 " }, qux, [&] -> Bar\n" 23874 " {\n" 23875 " auto i = j();\n" 23876 " return i;\n" 23877 " });\n" 23878 " return f;\n" 23879 " })));\n" 23880 "}", 23881 Style); 23882 } 23883 23884 TEST_F(FormatTest, LambdaWithLineComments) { 23885 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 23886 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 23887 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 23888 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 23889 FormatStyle::ShortLambdaStyle::SLS_All; 23890 23891 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 23892 verifyFormat("auto k = []() // comment\n" 23893 "{ return; }", 23894 LLVMWithBeforeLambdaBody); 23895 verifyFormat("auto k = []() /* comment */ { return; }", 23896 LLVMWithBeforeLambdaBody); 23897 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 23898 LLVMWithBeforeLambdaBody); 23899 verifyFormat("auto k = []() // X\n" 23900 "{ return; }", 23901 LLVMWithBeforeLambdaBody); 23902 verifyFormat( 23903 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 23904 "{ return; }", 23905 LLVMWithBeforeLambdaBody); 23906 23907 LLVMWithBeforeLambdaBody.ColumnLimit = 0; 23908 23909 verifyFormat("foo([]()\n" 23910 " {\n" 23911 " bar(); //\n" 23912 " return 1; // comment\n" 23913 " }());", 23914 "foo([]() {\n" 23915 " bar(); //\n" 23916 " return 1; // comment\n" 23917 "}());", 23918 LLVMWithBeforeLambdaBody); 23919 verifyFormat("foo(\n" 23920 " 1, MACRO {\n" 23921 " baz();\n" 23922 " bar(); // comment\n" 23923 " },\n" 23924 " []() {});", 23925 "foo(\n" 23926 " 1, MACRO { baz(); bar(); // comment\n" 23927 " }, []() {}\n" 23928 ");", 23929 LLVMWithBeforeLambdaBody); 23930 } 23931 23932 TEST_F(FormatTest, EmptyLinesInLambdas) { 23933 verifyFormat("auto lambda = []() {\n" 23934 " x(); //\n" 23935 "};", 23936 "auto lambda = []() {\n" 23937 "\n" 23938 " x(); //\n" 23939 "\n" 23940 "};"); 23941 } 23942 23943 TEST_F(FormatTest, FormatsBlocks) { 23944 FormatStyle ShortBlocks = getLLVMStyle(); 23945 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 23946 verifyFormat("int (^Block)(int, int);", ShortBlocks); 23947 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 23948 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 23949 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 23950 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 23951 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 23952 23953 verifyFormat("foo(^{ bar(); });", ShortBlocks); 23954 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 23955 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 23956 23957 verifyFormat("[operation setCompletionBlock:^{\n" 23958 " [self onOperationDone];\n" 23959 "}];"); 23960 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 23961 " [self onOperationDone];\n" 23962 "}]};"); 23963 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 23964 " f();\n" 23965 "}];"); 23966 verifyFormat("int a = [operation block:^int(int *i) {\n" 23967 " return 1;\n" 23968 "}];"); 23969 verifyFormat("[myObject doSomethingWith:arg1\n" 23970 " aaa:^int(int *a) {\n" 23971 " return 1;\n" 23972 " }\n" 23973 " bbb:f(a * bbbbbbbb)];"); 23974 23975 verifyFormat("[operation setCompletionBlock:^{\n" 23976 " [self.delegate newDataAvailable];\n" 23977 "}];", 23978 getLLVMStyleWithColumns(60)); 23979 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 23980 " NSString *path = [self sessionFilePath];\n" 23981 " if (path) {\n" 23982 " // ...\n" 23983 " }\n" 23984 "});"); 23985 verifyFormat("[[SessionService sharedService]\n" 23986 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 23987 " if (window) {\n" 23988 " [self windowDidLoad:window];\n" 23989 " } else {\n" 23990 " [self errorLoadingWindow];\n" 23991 " }\n" 23992 " }];"); 23993 verifyFormat("void (^largeBlock)(void) = ^{\n" 23994 " // ...\n" 23995 "};", 23996 getLLVMStyleWithColumns(40)); 23997 verifyFormat("[[SessionService sharedService]\n" 23998 " loadWindowWithCompletionBlock: //\n" 23999 " ^(SessionWindow *window) {\n" 24000 " if (window) {\n" 24001 " [self windowDidLoad:window];\n" 24002 " } else {\n" 24003 " [self errorLoadingWindow];\n" 24004 " }\n" 24005 " }];", 24006 getLLVMStyleWithColumns(60)); 24007 verifyFormat("[myObject doSomethingWith:arg1\n" 24008 " firstBlock:^(Foo *a) {\n" 24009 " // ...\n" 24010 " int i;\n" 24011 " }\n" 24012 " secondBlock:^(Bar *b) {\n" 24013 " // ...\n" 24014 " int i;\n" 24015 " }\n" 24016 " thirdBlock:^Foo(Bar *b) {\n" 24017 " // ...\n" 24018 " int i;\n" 24019 " }];"); 24020 verifyFormat("[myObject doSomethingWith:arg1\n" 24021 " firstBlock:-1\n" 24022 " secondBlock:^(Bar *b) {\n" 24023 " // ...\n" 24024 " int i;\n" 24025 " }];"); 24026 24027 verifyFormat("f(^{\n" 24028 " @autoreleasepool {\n" 24029 " if (a) {\n" 24030 " g();\n" 24031 " }\n" 24032 " }\n" 24033 "});"); 24034 verifyFormat("Block b = ^int *(A *a, B *b) {\n" 24035 "};"); 24036 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 24037 "};"); 24038 24039 FormatStyle FourIndent = getLLVMStyle(); 24040 FourIndent.ObjCBlockIndentWidth = 4; 24041 verifyFormat("[operation setCompletionBlock:^{\n" 24042 " [self onOperationDone];\n" 24043 "}];", 24044 FourIndent); 24045 } 24046 24047 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 24048 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 24049 24050 verifyFormat("[[SessionService sharedService] " 24051 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 24052 " if (window) {\n" 24053 " [self windowDidLoad:window];\n" 24054 " } else {\n" 24055 " [self errorLoadingWindow];\n" 24056 " }\n" 24057 "}];", 24058 ZeroColumn); 24059 verifyFormat("[[SessionService sharedService]\n" 24060 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 24061 " if (window) {\n" 24062 " [self windowDidLoad:window];\n" 24063 " } else {\n" 24064 " [self errorLoadingWindow];\n" 24065 " }\n" 24066 " }];", 24067 "[[SessionService sharedService]\n" 24068 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 24069 " if (window) {\n" 24070 " [self windowDidLoad:window];\n" 24071 " } else {\n" 24072 " [self errorLoadingWindow];\n" 24073 " }\n" 24074 "}];", 24075 ZeroColumn); 24076 verifyFormat("[myObject doSomethingWith:arg1\n" 24077 " firstBlock:^(Foo *a) {\n" 24078 " // ...\n" 24079 " int i;\n" 24080 " }\n" 24081 " secondBlock:^(Bar *b) {\n" 24082 " // ...\n" 24083 " int i;\n" 24084 " }\n" 24085 " thirdBlock:^Foo(Bar *b) {\n" 24086 " // ...\n" 24087 " int i;\n" 24088 " }];", 24089 ZeroColumn); 24090 verifyFormat("f(^{\n" 24091 " @autoreleasepool {\n" 24092 " if (a) {\n" 24093 " g();\n" 24094 " }\n" 24095 " }\n" 24096 "});", 24097 ZeroColumn); 24098 verifyFormat("void (^largeBlock)(void) = ^{\n" 24099 " // ...\n" 24100 "};", 24101 ZeroColumn); 24102 24103 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 24104 verifyFormat("void (^largeBlock)(void) = ^{ int i; };", 24105 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn); 24106 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 24107 verifyFormat("void (^largeBlock)(void) = ^{\n" 24108 " int i;\n" 24109 "};", 24110 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn); 24111 } 24112 24113 TEST_F(FormatTest, SupportsCRLF) { 24114 verifyFormat("int a;\r\n" 24115 "int b;\r\n" 24116 "int c;", 24117 "int a;\r\n" 24118 " int b;\r\n" 24119 " int c;"); 24120 verifyFormat("int a;\r\n" 24121 "int b;\r\n" 24122 "int c;\r\n", 24123 "int a;\r\n" 24124 " int b;\n" 24125 " int c;\r\n"); 24126 verifyFormat("int a;\n" 24127 "int b;\n" 24128 "int c;", 24129 "int a;\r\n" 24130 " int b;\n" 24131 " int c;"); 24132 // FIXME: unstable test case 24133 EXPECT_EQ("\"aaaaaaa \"\r\n" 24134 "\"bbbbbbb\";\r\n", 24135 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 24136 verifyFormat("#define A \\\r\n" 24137 " b; \\\r\n" 24138 " c; \\\r\n" 24139 " d;", 24140 "#define A \\\r\n" 24141 " b; \\\r\n" 24142 " c; d; ", 24143 getGoogleStyle()); 24144 24145 verifyNoChange("/*\r\n" 24146 "multi line block comments\r\n" 24147 "should not introduce\r\n" 24148 "an extra carriage return\r\n" 24149 "*/"); 24150 verifyFormat("/*\r\n" 24151 "\r\n" 24152 "*/", 24153 "/*\r\n" 24154 " \r\r\r\n" 24155 "*/"); 24156 24157 FormatStyle style = getLLVMStyle(); 24158 24159 EXPECT_EQ(style.LineEnding, FormatStyle::LE_DeriveLF); 24160 verifyFormat("union FooBarBazQux {\n" 24161 " int foo;\n" 24162 " int bar;\n" 24163 " int baz;\n" 24164 "};", 24165 "union FooBarBazQux {\r\n" 24166 " int foo;\n" 24167 " int bar;\r\n" 24168 " int baz;\n" 24169 "};", 24170 style); 24171 style.LineEnding = FormatStyle::LE_DeriveCRLF; 24172 verifyFormat("union FooBarBazQux {\r\n" 24173 " int foo;\r\n" 24174 " int bar;\r\n" 24175 " int baz;\r\n" 24176 "};", 24177 "union FooBarBazQux {\r\n" 24178 " int foo;\n" 24179 " int bar;\r\n" 24180 " int baz;\n" 24181 "};", 24182 style); 24183 24184 style.LineEnding = FormatStyle::LE_LF; 24185 verifyFormat("union FooBarBazQux {\n" 24186 " int foo;\n" 24187 " int bar;\n" 24188 " int baz;\n" 24189 " int qux;\n" 24190 "};", 24191 "union FooBarBazQux {\r\n" 24192 " int foo;\n" 24193 " int bar;\r\n" 24194 " int baz;\n" 24195 " int qux;\r\n" 24196 "};", 24197 style); 24198 style.LineEnding = FormatStyle::LE_CRLF; 24199 verifyFormat("union FooBarBazQux {\r\n" 24200 " int foo;\r\n" 24201 " int bar;\r\n" 24202 " int baz;\r\n" 24203 " int qux;\r\n" 24204 "};", 24205 "union FooBarBazQux {\r\n" 24206 " int foo;\n" 24207 " int bar;\r\n" 24208 " int baz;\n" 24209 " int qux;\n" 24210 "};", 24211 style); 24212 24213 style.LineEnding = FormatStyle::LE_DeriveLF; 24214 verifyFormat("union FooBarBazQux {\r\n" 24215 " int foo;\r\n" 24216 " int bar;\r\n" 24217 " int baz;\r\n" 24218 " int qux;\r\n" 24219 "};", 24220 "union FooBarBazQux {\r\n" 24221 " int foo;\n" 24222 " int bar;\r\n" 24223 " int baz;\n" 24224 " int qux;\r\n" 24225 "};", 24226 style); 24227 style.LineEnding = FormatStyle::LE_DeriveCRLF; 24228 verifyFormat("union FooBarBazQux {\n" 24229 " int foo;\n" 24230 " int bar;\n" 24231 " int baz;\n" 24232 " int qux;\n" 24233 "};", 24234 "union FooBarBazQux {\r\n" 24235 " int foo;\n" 24236 " int bar;\r\n" 24237 " int baz;\n" 24238 " int qux;\n" 24239 "};", 24240 style); 24241 } 24242 24243 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 24244 verifyFormat("MY_CLASS(C) {\n" 24245 " int i;\n" 24246 " int j;\n" 24247 "};"); 24248 } 24249 24250 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 24251 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 24252 TwoIndent.ContinuationIndentWidth = 2; 24253 24254 verifyFormat("int i =\n" 24255 " longFunction(\n" 24256 " arg);", 24257 "int i = longFunction(arg);", TwoIndent); 24258 24259 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 24260 SixIndent.ContinuationIndentWidth = 6; 24261 24262 verifyFormat("int i =\n" 24263 " longFunction(\n" 24264 " arg);", 24265 "int i = longFunction(arg);", SixIndent); 24266 } 24267 24268 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 24269 FormatStyle Style = getLLVMStyle(); 24270 verifyFormat("int Foo::getter(\n" 24271 " //\n" 24272 ") const {\n" 24273 " return foo;\n" 24274 "}", 24275 Style); 24276 verifyFormat("void Foo::setter(\n" 24277 " //\n" 24278 ") {\n" 24279 " foo = 1;\n" 24280 "}", 24281 Style); 24282 } 24283 24284 TEST_F(FormatTest, SpacesInAngles) { 24285 FormatStyle Spaces = getLLVMStyle(); 24286 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 24287 24288 verifyFormat("vector< ::std::string > x1;", Spaces); 24289 verifyFormat("Foo< int, Bar > x2;", Spaces); 24290 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 24291 24292 verifyFormat("static_cast< int >(arg);", Spaces); 24293 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 24294 verifyFormat("f< int, float >();", Spaces); 24295 verifyFormat("template <> g() {}", Spaces); 24296 verifyFormat("template < std::vector< int > > f() {}", Spaces); 24297 verifyFormat("std::function< void(int, int) > fct;", Spaces); 24298 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 24299 Spaces); 24300 24301 Spaces.Standard = FormatStyle::LS_Cpp03; 24302 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 24303 verifyFormat("A< A< int > >();", Spaces); 24304 24305 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 24306 verifyFormat("A<A<int> >();", Spaces); 24307 24308 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 24309 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 24310 Spaces); 24311 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 24312 Spaces); 24313 24314 verifyFormat("A<A<int> >();", Spaces); 24315 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 24316 verifyFormat("A< A< int > >();", Spaces); 24317 24318 Spaces.Standard = FormatStyle::LS_Cpp11; 24319 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 24320 verifyFormat("A< A< int > >();", Spaces); 24321 24322 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 24323 verifyFormat("vector<::std::string> x4;", Spaces); 24324 verifyFormat("vector<int> x5;", Spaces); 24325 verifyFormat("Foo<int, Bar> x6;", Spaces); 24326 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 24327 24328 verifyFormat("A<A<int>>();", Spaces); 24329 24330 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 24331 verifyFormat("vector<::std::string> x4;", Spaces); 24332 verifyFormat("vector< ::std::string > x4;", Spaces); 24333 verifyFormat("vector<int> x5;", Spaces); 24334 verifyFormat("vector< int > x5;", Spaces); 24335 verifyFormat("Foo<int, Bar> x6;", Spaces); 24336 verifyFormat("Foo< int, Bar > x6;", Spaces); 24337 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 24338 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 24339 24340 verifyFormat("A<A<int>>();", Spaces); 24341 verifyFormat("A< A< int > >();", Spaces); 24342 verifyFormat("A<A<int > >();", Spaces); 24343 verifyFormat("A< A< int>>();", Spaces); 24344 24345 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 24346 verifyFormat("// clang-format off\n" 24347 "foo<<<1, 1>>>();\n" 24348 "// clang-format on", 24349 Spaces); 24350 verifyFormat("// clang-format off\n" 24351 "foo< < <1, 1> > >();\n" 24352 "// clang-format on", 24353 Spaces); 24354 } 24355 24356 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 24357 FormatStyle Style = getLLVMStyle(); 24358 Style.SpaceAfterTemplateKeyword = false; 24359 verifyFormat("template<int> void foo();", Style); 24360 } 24361 24362 TEST_F(FormatTest, TripleAngleBrackets) { 24363 verifyFormat("f<<<1, 1>>>();"); 24364 verifyFormat("f<<<1, 1, 1, s>>>();"); 24365 verifyFormat("f<<<a, b, c, d>>>();"); 24366 verifyFormat("f<<<1, 1>>>();", "f <<< 1, 1 >>> ();"); 24367 verifyFormat("f<param><<<1, 1>>>();"); 24368 verifyFormat("f<1><<<1, 1>>>();"); 24369 verifyFormat("f<param><<<1, 1>>>();", "f< param > <<< 1, 1 >>> ();"); 24370 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 24371 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 24372 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 24373 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 24374 } 24375 24376 TEST_F(FormatTest, MergeLessLessAtEnd) { 24377 verifyFormat("<<"); 24378 verifyFormat("< < <", "\\\n<<<"); 24379 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 24380 "aaallvm::outs() <<"); 24381 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 24382 "aaaallvm::outs()\n <<"); 24383 } 24384 24385 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 24386 std::string code = "#if A\n" 24387 "#if B\n" 24388 "a.\n" 24389 "#endif\n" 24390 " a = 1;\n" 24391 "#else\n" 24392 "#endif\n" 24393 "#if C\n" 24394 "#else\n" 24395 "#endif\n"; 24396 verifyFormat(code); 24397 } 24398 24399 TEST_F(FormatTest, HandleConflictMarkers) { 24400 // Git/SVN conflict markers. 24401 verifyFormat("int a;\n" 24402 "void f() {\n" 24403 " callme(some(parameter1,\n" 24404 "<<<<<<< text by the vcs\n" 24405 " parameter2),\n" 24406 "||||||| text by the vcs\n" 24407 " parameter2),\n" 24408 " parameter3,\n" 24409 "======= text by the vcs\n" 24410 " parameter2, parameter3),\n" 24411 ">>>>>>> text by the vcs\n" 24412 " otherparameter);", 24413 "int a;\n" 24414 "void f() {\n" 24415 " callme(some(parameter1,\n" 24416 "<<<<<<< text by the vcs\n" 24417 " parameter2),\n" 24418 "||||||| text by the vcs\n" 24419 " parameter2),\n" 24420 " parameter3,\n" 24421 "======= text by the vcs\n" 24422 " parameter2,\n" 24423 " parameter3),\n" 24424 ">>>>>>> text by the vcs\n" 24425 " otherparameter);"); 24426 24427 // Perforce markers. 24428 verifyFormat("void f() {\n" 24429 " function(\n" 24430 ">>>> text by the vcs\n" 24431 " parameter,\n" 24432 "==== text by the vcs\n" 24433 " parameter,\n" 24434 "==== text by the vcs\n" 24435 " parameter,\n" 24436 "<<<< text by the vcs\n" 24437 " parameter);", 24438 "void f() {\n" 24439 " function(\n" 24440 ">>>> text by the vcs\n" 24441 " parameter,\n" 24442 "==== text by the vcs\n" 24443 " parameter,\n" 24444 "==== text by the vcs\n" 24445 " parameter,\n" 24446 "<<<< text by the vcs\n" 24447 " parameter);"); 24448 24449 verifyNoChange("<<<<<<<\n" 24450 "|||||||\n" 24451 "=======\n" 24452 ">>>>>>>"); 24453 24454 verifyNoChange("<<<<<<<\n" 24455 "|||||||\n" 24456 "int i;\n" 24457 "=======\n" 24458 ">>>>>>>"); 24459 24460 // FIXME: Handle parsing of macros around conflict markers correctly: 24461 verifyFormat("#define Macro \\\n" 24462 "<<<<<<<\n" 24463 "Something \\\n" 24464 "|||||||\n" 24465 "Else \\\n" 24466 "=======\n" 24467 "Other \\\n" 24468 ">>>>>>>\n" 24469 " End int i;", 24470 "#define Macro \\\n" 24471 "<<<<<<<\n" 24472 " Something \\\n" 24473 "|||||||\n" 24474 " Else \\\n" 24475 "=======\n" 24476 " Other \\\n" 24477 ">>>>>>>\n" 24478 " End\n" 24479 "int i;"); 24480 24481 verifyFormat(R"(==== 24482 #ifdef A 24483 a 24484 #else 24485 b 24486 #endif 24487 )"); 24488 } 24489 24490 TEST_F(FormatTest, DisableRegions) { 24491 verifyFormat("int i;\n" 24492 "// clang-format off\n" 24493 " int j;\n" 24494 "// clang-format on\n" 24495 "int k;", 24496 " int i;\n" 24497 " // clang-format off\n" 24498 " int j;\n" 24499 " // clang-format on\n" 24500 " int k;"); 24501 verifyFormat("int i;\n" 24502 "/* clang-format off */\n" 24503 " int j;\n" 24504 "/* clang-format on */\n" 24505 "int k;", 24506 " int i;\n" 24507 " /* clang-format off */\n" 24508 " int j;\n" 24509 " /* clang-format on */\n" 24510 " int k;"); 24511 24512 // Don't reflow comments within disabled regions. 24513 verifyFormat("// clang-format off\n" 24514 "// long long long long long long line\n" 24515 "/* clang-format on */\n" 24516 "/* long long long\n" 24517 " * long long long\n" 24518 " * line */\n" 24519 "int i;\n" 24520 "/* clang-format off */\n" 24521 "/* long long long long long long line */", 24522 "// clang-format off\n" 24523 "// long long long long long long line\n" 24524 "/* clang-format on */\n" 24525 "/* long long long long long long line */\n" 24526 "int i;\n" 24527 "/* clang-format off */\n" 24528 "/* long long long long long long line */", 24529 getLLVMStyleWithColumns(20)); 24530 24531 verifyFormat("int *i;\n" 24532 "// clang-format off:\n" 24533 "int* j;\n" 24534 "// clang-format on: 1\n" 24535 "int *k;", 24536 "int* i;\n" 24537 "// clang-format off:\n" 24538 "int* j;\n" 24539 "// clang-format on: 1\n" 24540 "int* k;"); 24541 24542 verifyFormat("int *i;\n" 24543 "// clang-format off:0\n" 24544 "int* j;\n" 24545 "// clang-format only\n" 24546 "int* k;", 24547 "int* i;\n" 24548 "// clang-format off:0\n" 24549 "int* j;\n" 24550 "// clang-format only\n" 24551 "int* k;"); 24552 24553 verifyNoChange("// clang-format off\n" 24554 "#if 0\n" 24555 " #if SHOULD_STAY_INDENTED\n" 24556 " #endif\n" 24557 "#endif\n" 24558 "// clang-format on"); 24559 } 24560 24561 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 24562 format("? ) ="); 24563 verifyNoCrash("#define a\\\n /**/}"); 24564 verifyNoCrash(" tst %o5 ! are we doing the gray case?\n" 24565 "LY52: ! [internal]"); 24566 } 24567 24568 TEST_F(FormatTest, FormatsTableGenCode) { 24569 FormatStyle Style = getLLVMStyle(); 24570 Style.Language = FormatStyle::LK_TableGen; 24571 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 24572 } 24573 24574 TEST_F(FormatTest, ArrayOfTemplates) { 24575 verifyFormat("auto a = new unique_ptr<int>[10];", 24576 "auto a = new unique_ptr<int > [ 10];"); 24577 24578 FormatStyle Spaces = getLLVMStyle(); 24579 Spaces.SpacesInSquareBrackets = true; 24580 verifyFormat("auto a = new unique_ptr<int>[ 10 ];", 24581 "auto a = new unique_ptr<int > [10];", Spaces); 24582 } 24583 24584 TEST_F(FormatTest, ArrayAsTemplateType) { 24585 verifyFormat("auto a = unique_ptr<Foo<Bar>[10]>;", 24586 "auto a = unique_ptr < Foo < Bar>[ 10]> ;"); 24587 24588 FormatStyle Spaces = getLLVMStyle(); 24589 Spaces.SpacesInSquareBrackets = true; 24590 verifyFormat("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 24591 "auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces); 24592 } 24593 24594 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 24595 24596 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 24597 verifyFormat("using std::cin;\n" 24598 "using std::cout;", 24599 "using std::cout;\n" 24600 "using std::cin;", 24601 getGoogleStyle()); 24602 } 24603 24604 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 24605 FormatStyle Style = getLLVMStyle(); 24606 Style.Standard = FormatStyle::LS_Cpp03; 24607 // cpp03 recognize this string as identifier u8 and literal character 'a' 24608 verifyFormat("auto c = u8 'a';", "auto c = u8'a';", Style); 24609 } 24610 24611 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 24612 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 24613 // all modes, including C++11, C++14 and C++17 24614 verifyFormat("auto c = u8'a';"); 24615 } 24616 24617 TEST_F(FormatTest, DoNotFormatLikelyXml) { 24618 verifyGoogleFormat("<!-- ;> -->"); 24619 verifyNoChange(" <!-- >; -->", getGoogleStyle()); 24620 } 24621 24622 TEST_F(FormatTest, StructuredBindings) { 24623 // Structured bindings is a C++17 feature. 24624 // all modes, including C++11, C++14 and C++17 24625 verifyFormat("auto [a, b] = f();"); 24626 verifyFormat("auto [a, b] = f();", "auto[a, b] = f();"); 24627 verifyFormat("const auto [a, b] = f();", "const auto[a, b] = f();"); 24628 verifyFormat("auto const [a, b] = f();", "auto const[a, b] = f();"); 24629 verifyFormat("auto const volatile [a, b] = f();", 24630 "auto const volatile[a, b] = f();"); 24631 verifyFormat("auto [a, b, c] = f();", "auto [ a , b,c ] = f();"); 24632 verifyFormat("auto &[a, b, c] = f();", "auto &[ a , b,c ] = f();"); 24633 verifyFormat("auto &&[a, b, c] = f();", "auto &&[ a , b,c ] = f();"); 24634 verifyFormat("auto const &[a, b] = f();", "auto const&[a, b] = f();"); 24635 verifyFormat("auto const volatile &&[a, b] = f();", 24636 "auto const volatile &&[a, b] = f();"); 24637 verifyFormat("auto const &&[a, b] = f();", "auto const && [a, b] = f();"); 24638 verifyFormat("const auto &[a, b] = f();", "const auto & [a, b] = f();"); 24639 verifyFormat("const auto volatile &&[a, b] = f();", 24640 "const auto volatile &&[a, b] = f();"); 24641 verifyFormat("volatile const auto &&[a, b] = f();", 24642 "volatile const auto &&[a, b] = f();"); 24643 verifyFormat("const auto &&[a, b] = f();", "const auto && [a, b] = f();"); 24644 24645 // Make sure we don't mistake structured bindings for lambdas. 24646 FormatStyle PointerMiddle = getLLVMStyle(); 24647 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 24648 verifyGoogleFormat("auto [a1, b]{A * i};"); 24649 verifyFormat("auto [a2, b]{A * i};"); 24650 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 24651 verifyGoogleFormat("auto const [a1, b]{A * i};"); 24652 verifyFormat("auto const [a2, b]{A * i};"); 24653 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 24654 verifyGoogleFormat("auto const& [a1, b]{A * i};"); 24655 verifyFormat("auto const &[a2, b]{A * i};"); 24656 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 24657 verifyGoogleFormat("auto const&& [a1, b]{A * i};"); 24658 verifyFormat("auto const &&[a2, b]{A * i};"); 24659 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 24660 24661 verifyFormat("for (const auto &&[a, b] : some_range) {\n}", 24662 "for (const auto && [a, b] : some_range) {\n}"); 24663 verifyFormat("for (const auto &[a, b] : some_range) {\n}", 24664 "for (const auto & [a, b] : some_range) {\n}"); 24665 verifyFormat("for (const auto [a, b] : some_range) {\n}", 24666 "for (const auto[a, b] : some_range) {\n}"); 24667 verifyFormat("auto [x, y](expr);", "auto[x,y] (expr);"); 24668 verifyFormat("auto &[x, y](expr);", "auto & [x,y] (expr);"); 24669 verifyFormat("auto &&[x, y](expr);", "auto && [x,y] (expr);"); 24670 verifyFormat("auto const &[x, y](expr);", "auto const & [x,y] (expr);"); 24671 verifyFormat("auto const &&[x, y](expr);", "auto const && [x,y] (expr);"); 24672 verifyFormat("auto [x, y]{expr};", "auto[x,y] {expr};"); 24673 verifyFormat("auto const &[x, y]{expr};", "auto const & [x,y] {expr};"); 24674 verifyFormat("auto const &&[x, y]{expr};", "auto const && [x,y] {expr};"); 24675 24676 FormatStyle Spaces = getLLVMStyle(); 24677 Spaces.SpacesInSquareBrackets = true; 24678 verifyFormat("auto [ a, b ] = f();", Spaces); 24679 verifyFormat("auto &&[ a, b ] = f();", Spaces); 24680 verifyFormat("auto &[ a, b ] = f();", Spaces); 24681 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 24682 verifyFormat("auto const &[ a, b ] = f();", Spaces); 24683 } 24684 24685 TEST_F(FormatTest, FileAndCode) { 24686 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 24687 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 24688 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 24689 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 24690 EXPECT_EQ(FormatStyle::LK_ObjC, 24691 guessLanguage("foo.h", "@interface Foo\n@end")); 24692 EXPECT_EQ( 24693 FormatStyle::LK_ObjC, 24694 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 24695 EXPECT_EQ(FormatStyle::LK_ObjC, 24696 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 24697 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 24698 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 24699 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end")); 24700 EXPECT_EQ(FormatStyle::LK_ObjC, 24701 guessLanguage("foo.h", "int DoStuff(CGRect rect);")); 24702 EXPECT_EQ(FormatStyle::LK_ObjC, 24703 guessLanguage( 24704 "foo.h", "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));")); 24705 EXPECT_EQ( 24706 FormatStyle::LK_Cpp, 24707 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 24708 // Only one of the two preprocessor regions has ObjC-like code. 24709 EXPECT_EQ(FormatStyle::LK_ObjC, 24710 guessLanguage("foo.h", "#if A\n" 24711 "#define B() C\n" 24712 "#else\n" 24713 "#define B() [NSString a:@\"\"]\n" 24714 "#endif")); 24715 } 24716 24717 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 24718 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 24719 EXPECT_EQ(FormatStyle::LK_ObjC, 24720 guessLanguage("foo.h", "array[[calculator getIndex]];")); 24721 EXPECT_EQ(FormatStyle::LK_Cpp, 24722 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 24723 EXPECT_EQ( 24724 FormatStyle::LK_Cpp, 24725 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 24726 EXPECT_EQ(FormatStyle::LK_ObjC, 24727 guessLanguage("foo.h", "[[noreturn foo] bar];")); 24728 EXPECT_EQ(FormatStyle::LK_Cpp, 24729 guessLanguage("foo.h", "[[clang::fallthrough]];")); 24730 EXPECT_EQ(FormatStyle::LK_ObjC, 24731 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 24732 EXPECT_EQ(FormatStyle::LK_Cpp, 24733 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 24734 EXPECT_EQ(FormatStyle::LK_Cpp, 24735 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 24736 EXPECT_EQ(FormatStyle::LK_ObjC, 24737 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 24738 EXPECT_EQ(FormatStyle::LK_Cpp, 24739 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 24740 EXPECT_EQ( 24741 FormatStyle::LK_Cpp, 24742 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 24743 EXPECT_EQ( 24744 FormatStyle::LK_Cpp, 24745 guessLanguage("foo.h", 24746 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 24747 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 24748 } 24749 24750 TEST_F(FormatTest, GuessLanguageWithCaret) { 24751 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 24752 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 24753 EXPECT_EQ(FormatStyle::LK_ObjC, 24754 guessLanguage("foo.h", "int(^)(char, float);")); 24755 EXPECT_EQ(FormatStyle::LK_ObjC, 24756 guessLanguage("foo.h", "int(^foo)(char, float);")); 24757 EXPECT_EQ(FormatStyle::LK_ObjC, 24758 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 24759 EXPECT_EQ(FormatStyle::LK_ObjC, 24760 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 24761 EXPECT_EQ( 24762 FormatStyle::LK_ObjC, 24763 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 24764 } 24765 24766 TEST_F(FormatTest, GuessLanguageWithPragmas) { 24767 EXPECT_EQ(FormatStyle::LK_Cpp, 24768 guessLanguage("foo.h", "__pragma(warning(disable:))")); 24769 EXPECT_EQ(FormatStyle::LK_Cpp, 24770 guessLanguage("foo.h", "#pragma(warning(disable:))")); 24771 EXPECT_EQ(FormatStyle::LK_Cpp, 24772 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 24773 } 24774 24775 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 24776 // ASM symbolic names are identifiers that must be surrounded by [] without 24777 // space in between: 24778 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 24779 24780 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 24781 verifyFormat(R"(// 24782 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 24783 )"); 24784 24785 // A list of several ASM symbolic names. 24786 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 24787 24788 // ASM symbolic names in inline ASM with inputs and outputs. 24789 verifyFormat(R"(// 24790 asm("cmoveq %1, %2, %[result]" 24791 : [result] "=r"(result) 24792 : "r"(test), "r"(new), "[result]"(old)); 24793 )"); 24794 24795 // ASM symbolic names in inline ASM with no outputs. 24796 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 24797 } 24798 24799 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 24800 EXPECT_EQ(FormatStyle::LK_Cpp, 24801 guessLanguage("foo.h", "void f() {\n" 24802 " asm (\"mov %[e], %[d]\"\n" 24803 " : [d] \"=rm\" (d)\n" 24804 " [e] \"rm\" (*e));\n" 24805 "}")); 24806 EXPECT_EQ(FormatStyle::LK_Cpp, 24807 guessLanguage("foo.h", "void f() {\n" 24808 " _asm (\"mov %[e], %[d]\"\n" 24809 " : [d] \"=rm\" (d)\n" 24810 " [e] \"rm\" (*e));\n" 24811 "}")); 24812 EXPECT_EQ(FormatStyle::LK_Cpp, 24813 guessLanguage("foo.h", "void f() {\n" 24814 " __asm (\"mov %[e], %[d]\"\n" 24815 " : [d] \"=rm\" (d)\n" 24816 " [e] \"rm\" (*e));\n" 24817 "}")); 24818 EXPECT_EQ(FormatStyle::LK_Cpp, 24819 guessLanguage("foo.h", "void f() {\n" 24820 " __asm__ (\"mov %[e], %[d]\"\n" 24821 " : [d] \"=rm\" (d)\n" 24822 " [e] \"rm\" (*e));\n" 24823 "}")); 24824 EXPECT_EQ(FormatStyle::LK_Cpp, 24825 guessLanguage("foo.h", "void f() {\n" 24826 " asm (\"mov %[e], %[d]\"\n" 24827 " : [d] \"=rm\" (d),\n" 24828 " [e] \"rm\" (*e));\n" 24829 "}")); 24830 EXPECT_EQ(FormatStyle::LK_Cpp, 24831 guessLanguage("foo.h", "void f() {\n" 24832 " asm volatile (\"mov %[e], %[d]\"\n" 24833 " : [d] \"=rm\" (d)\n" 24834 " [e] \"rm\" (*e));\n" 24835 "}")); 24836 } 24837 24838 TEST_F(FormatTest, GuessLanguageWithChildLines) { 24839 EXPECT_EQ(FormatStyle::LK_Cpp, 24840 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 24841 EXPECT_EQ(FormatStyle::LK_ObjC, 24842 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 24843 EXPECT_EQ( 24844 FormatStyle::LK_Cpp, 24845 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 24846 EXPECT_EQ( 24847 FormatStyle::LK_ObjC, 24848 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 24849 } 24850 24851 TEST_F(FormatTest, TypenameMacros) { 24852 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 24853 24854 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 24855 FormatStyle Google = getGoogleStyleWithColumns(0); 24856 Google.TypenameMacros = TypenameMacros; 24857 verifyFormat("struct foo {\n" 24858 " int bar;\n" 24859 " TAILQ_ENTRY(a) bleh;\n" 24860 "};", 24861 Google); 24862 24863 FormatStyle Macros = getLLVMStyle(); 24864 Macros.TypenameMacros = TypenameMacros; 24865 24866 verifyFormat("STACK_OF(int) a;", Macros); 24867 verifyFormat("STACK_OF(int) *a;", Macros); 24868 verifyFormat("STACK_OF(int const *) *a;", Macros); 24869 verifyFormat("STACK_OF(int *const) *a;", Macros); 24870 verifyFormat("STACK_OF(int, string) a;", Macros); 24871 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 24872 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 24873 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 24874 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 24875 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 24876 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 24877 24878 Macros.PointerAlignment = FormatStyle::PAS_Left; 24879 verifyFormat("STACK_OF(int)* a;", Macros); 24880 verifyFormat("STACK_OF(int*)* a;", Macros); 24881 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 24882 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 24883 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 24884 } 24885 24886 TEST_F(FormatTest, AtomicQualifier) { 24887 // Check that we treate _Atomic as a type and not a function call 24888 FormatStyle Google = getGoogleStyleWithColumns(0); 24889 verifyFormat("struct foo {\n" 24890 " int a1;\n" 24891 " _Atomic(a) a2;\n" 24892 " _Atomic(_Atomic(int) *const) a3;\n" 24893 "};", 24894 Google); 24895 verifyFormat("_Atomic(uint64_t) a;"); 24896 verifyFormat("_Atomic(uint64_t) *a;"); 24897 verifyFormat("_Atomic(uint64_t const *) *a;"); 24898 verifyFormat("_Atomic(uint64_t *const) *a;"); 24899 verifyFormat("_Atomic(const uint64_t *) *a;"); 24900 verifyFormat("_Atomic(uint64_t) a;"); 24901 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 24902 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 24903 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 24904 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 24905 24906 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 24907 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 24908 FormatStyle Style = getLLVMStyle(); 24909 Style.PointerAlignment = FormatStyle::PAS_Left; 24910 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 24911 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 24912 verifyFormat("_Atomic(int)* a;", Style); 24913 verifyFormat("_Atomic(int*)* a;", Style); 24914 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 24915 24916 Style.SpacesInParens = FormatStyle::SIPO_Custom; 24917 Style.SpacesInParensOptions.InCStyleCasts = true; 24918 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 24919 Style.SpacesInParensOptions.InCStyleCasts = false; 24920 Style.SpacesInParensOptions.Other = true; 24921 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 24922 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 24923 } 24924 24925 TEST_F(FormatTest, C11Generic) { 24926 verifyFormat("_Generic(x, int: 1, default: 0)"); 24927 verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)"); 24928 verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);"); 24929 verifyFormat("_Generic(x, int: f1, const int: f2)();"); 24930 verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);"); 24931 24932 verifyFormat("_Generic(x,\n" 24933 " float: f,\n" 24934 " default: d,\n" 24935 " long double: ld,\n" 24936 " float _Complex: fc,\n" 24937 " double _Complex: dc,\n" 24938 " long double _Complex: ldc)"); 24939 24940 verifyFormat("while (_Generic(x, //\n" 24941 " long: x)(x) > x) {\n" 24942 "}"); 24943 verifyFormat("while (_Generic(x, //\n" 24944 " long: x)(x)) {\n" 24945 "}"); 24946 verifyFormat("x(_Generic(x, //\n" 24947 " long: x)(x));"); 24948 24949 FormatStyle Style = getLLVMStyle(); 24950 Style.ColumnLimit = 40; 24951 verifyFormat("#define LIMIT_MAX(T) \\\n" 24952 " _Generic(((T)0), \\\n" 24953 " unsigned int: UINT_MAX, \\\n" 24954 " unsigned long: ULONG_MAX, \\\n" 24955 " unsigned long long: ULLONG_MAX)", 24956 Style); 24957 verifyFormat("_Generic(x,\n" 24958 " struct A: 1,\n" 24959 " void (*)(void): 2);", 24960 Style); 24961 24962 Style.ContinuationIndentWidth = 2; 24963 verifyFormat("_Generic(x,\n" 24964 " struct A: 1,\n" 24965 " void (*)(void): 2);", 24966 Style); 24967 } 24968 24969 TEST_F(FormatTest, AmbersandInLamda) { 24970 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 24971 FormatStyle AlignStyle = getLLVMStyle(); 24972 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 24973 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 24974 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 24975 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 24976 } 24977 24978 TEST_F(FormatTest, TrailingReturnTypeAuto) { 24979 FormatStyle Style = getLLVMStyle(); 24980 verifyFormat("[]() -> auto { return Val; }", Style); 24981 verifyFormat("[]() -> auto * { return Val; }", Style); 24982 verifyFormat("[]() -> auto & { return Val; }", Style); 24983 verifyFormat("auto foo() -> auto { return Val; }", Style); 24984 verifyFormat("auto foo() -> auto * { return Val; }", Style); 24985 verifyFormat("auto foo() -> auto & { return Val; }", Style); 24986 } 24987 24988 TEST_F(FormatTest, SpacesInConditionalStatement) { 24989 FormatStyle Spaces = getLLVMStyle(); 24990 Spaces.IfMacros.clear(); 24991 Spaces.IfMacros.push_back("MYIF"); 24992 Spaces.SpacesInParens = FormatStyle::SIPO_Custom; 24993 Spaces.SpacesInParensOptions.InConditionalStatements = true; 24994 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 24995 verifyFormat("if ( !a )\n return;", Spaces); 24996 verifyFormat("if ( a )\n return;", Spaces); 24997 verifyFormat("if constexpr ( a )\n return;", Spaces); 24998 verifyFormat("MYIF ( a )\n return;", Spaces); 24999 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 25000 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 25001 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 25002 verifyFormat("while ( a )\n return;", Spaces); 25003 verifyFormat("while ( (a && b) )\n return;", Spaces); 25004 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 25005 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 25006 // Check that space on the left of "::" is inserted as expected at beginning 25007 // of condition. 25008 verifyFormat("while ( ::func() )\n return;", Spaces); 25009 25010 // Check impact of ControlStatementsExceptControlMacros is honored. 25011 Spaces.SpaceBeforeParens = 25012 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 25013 verifyFormat("MYIF( a )\n return;", Spaces); 25014 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 25015 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 25016 } 25017 25018 TEST_F(FormatTest, AlternativeOperators) { 25019 // Test case for ensuring alternate operators are not 25020 // combined with their right most neighbour. 25021 verifyFormat("int a and b;"); 25022 verifyFormat("int a and_eq b;"); 25023 verifyFormat("int a bitand b;"); 25024 verifyFormat("int a bitor b;"); 25025 verifyFormat("int a compl b;"); 25026 verifyFormat("int a not b;"); 25027 verifyFormat("int a not_eq b;"); 25028 verifyFormat("int a or b;"); 25029 verifyFormat("int a xor b;"); 25030 verifyFormat("int a xor_eq b;"); 25031 verifyFormat("return this not_eq bitand other;"); 25032 verifyFormat("bool operator not_eq(const X bitand other)"); 25033 25034 verifyFormat("int a and 5;"); 25035 verifyFormat("int a and_eq 5;"); 25036 verifyFormat("int a bitand 5;"); 25037 verifyFormat("int a bitor 5;"); 25038 verifyFormat("int a compl 5;"); 25039 verifyFormat("int a not 5;"); 25040 verifyFormat("int a not_eq 5;"); 25041 verifyFormat("int a or 5;"); 25042 verifyFormat("int a xor 5;"); 25043 verifyFormat("int a xor_eq 5;"); 25044 25045 verifyFormat("int a compl(5);"); 25046 verifyFormat("int a not(5);"); 25047 25048 verifyFormat("compl foo();"); // ~foo(); 25049 verifyFormat("foo() <%%>"); // foo() {} 25050 verifyFormat("void foo() <%%>"); // void foo() {} 25051 verifyFormat("int a<:1:>;"); // int a[1]; 25052 verifyFormat("%:define ABC abc"); // #define ABC abc 25053 verifyFormat("%:%:"); // ## 25054 25055 verifyFormat("a = v(not;);\n" 25056 "b = v(not+);\n" 25057 "c = v(not x);\n" 25058 "d = v(not 1);\n" 25059 "e = v(not 123.f);"); 25060 25061 verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V) \\\n" 25062 " V(and) \\\n" 25063 " V(not) \\\n" 25064 " V(not!) \\\n" 25065 " V(other)", 25066 getLLVMStyleWithColumns(40)); 25067 } 25068 25069 TEST_F(FormatTest, STLWhileNotDefineChed) { 25070 verifyFormat("#if defined(while)\n" 25071 "#define while EMIT WARNING C4005\n" 25072 "#endif // while"); 25073 } 25074 25075 TEST_F(FormatTest, OperatorSpacing) { 25076 FormatStyle Style = getLLVMStyle(); 25077 Style.PointerAlignment = FormatStyle::PAS_Right; 25078 verifyFormat("Foo::operator*();", Style); 25079 verifyFormat("Foo::operator void *();", Style); 25080 verifyFormat("Foo::operator void **();", Style); 25081 verifyFormat("Foo::operator void *&();", Style); 25082 verifyFormat("Foo::operator void *&&();", Style); 25083 verifyFormat("Foo::operator void const *();", Style); 25084 verifyFormat("Foo::operator void const **();", Style); 25085 verifyFormat("Foo::operator void const *&();", Style); 25086 verifyFormat("Foo::operator void const *&&();", Style); 25087 verifyFormat("Foo::operator()(void *);", Style); 25088 verifyFormat("Foo::operator*(void *);", Style); 25089 verifyFormat("Foo::operator*();", Style); 25090 verifyFormat("Foo::operator**();", Style); 25091 verifyFormat("Foo::operator&();", Style); 25092 verifyFormat("Foo::operator<int> *();", Style); 25093 verifyFormat("Foo::operator<Foo> *();", Style); 25094 verifyFormat("Foo::operator<int> **();", Style); 25095 verifyFormat("Foo::operator<Foo> **();", Style); 25096 verifyFormat("Foo::operator<int> &();", Style); 25097 verifyFormat("Foo::operator<Foo> &();", Style); 25098 verifyFormat("Foo::operator<int> &&();", Style); 25099 verifyFormat("Foo::operator<Foo> &&();", Style); 25100 verifyFormat("Foo::operator<int> *&();", Style); 25101 verifyFormat("Foo::operator<Foo> *&();", Style); 25102 verifyFormat("Foo::operator<int> *&&();", Style); 25103 verifyFormat("Foo::operator<Foo> *&&();", Style); 25104 verifyFormat("operator*(int (*)(), class Foo);", Style); 25105 25106 verifyFormat("Foo::operator&();", Style); 25107 verifyFormat("Foo::operator void &();", Style); 25108 verifyFormat("Foo::operator void const &();", Style); 25109 verifyFormat("Foo::operator()(void &);", Style); 25110 verifyFormat("Foo::operator&(void &);", Style); 25111 verifyFormat("Foo::operator&();", Style); 25112 verifyFormat("operator&(int (&)(), class Foo);", Style); 25113 verifyFormat("operator&&(int (&)(), class Foo);", Style); 25114 25115 verifyFormat("Foo::operator&&();", Style); 25116 verifyFormat("Foo::operator**();", Style); 25117 verifyFormat("Foo::operator void &&();", Style); 25118 verifyFormat("Foo::operator void const &&();", Style); 25119 verifyFormat("Foo::operator()(void &&);", Style); 25120 verifyFormat("Foo::operator&&(void &&);", Style); 25121 verifyFormat("Foo::operator&&();", Style); 25122 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 25123 verifyFormat("operator const nsTArrayRight<E> &()", Style); 25124 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 25125 Style); 25126 verifyFormat("operator void **()", Style); 25127 verifyFormat("operator const FooRight<Object> &()", Style); 25128 verifyFormat("operator const FooRight<Object> *()", Style); 25129 verifyFormat("operator const FooRight<Object> **()", Style); 25130 verifyFormat("operator const FooRight<Object> *&()", Style); 25131 verifyFormat("operator const FooRight<Object> *&&()", Style); 25132 25133 Style.PointerAlignment = FormatStyle::PAS_Left; 25134 verifyFormat("Foo::operator*();", Style); 25135 verifyFormat("Foo::operator**();", Style); 25136 verifyFormat("Foo::operator void*();", Style); 25137 verifyFormat("Foo::operator void**();", Style); 25138 verifyFormat("Foo::operator void*&();", Style); 25139 verifyFormat("Foo::operator void*&&();", Style); 25140 verifyFormat("Foo::operator void const*();", Style); 25141 verifyFormat("Foo::operator void const**();", Style); 25142 verifyFormat("Foo::operator void const*&();", Style); 25143 verifyFormat("Foo::operator void const*&&();", Style); 25144 verifyFormat("Foo::operator/*comment*/ void*();", Style); 25145 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 25146 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 25147 verifyFormat("Foo::operator()(void*);", Style); 25148 verifyFormat("Foo::operator*(void*);", Style); 25149 verifyFormat("Foo::operator*();", Style); 25150 verifyFormat("Foo::operator<int>*();", Style); 25151 verifyFormat("Foo::operator<Foo>*();", Style); 25152 verifyFormat("Foo::operator<int>**();", Style); 25153 verifyFormat("Foo::operator<Foo>**();", Style); 25154 verifyFormat("Foo::operator<Foo>*&();", Style); 25155 verifyFormat("Foo::operator<int>&();", Style); 25156 verifyFormat("Foo::operator<Foo>&();", Style); 25157 verifyFormat("Foo::operator<int>&&();", Style); 25158 verifyFormat("Foo::operator<Foo>&&();", Style); 25159 verifyFormat("Foo::operator<int>*&();", Style); 25160 verifyFormat("Foo::operator<Foo>*&();", Style); 25161 verifyFormat("operator*(int (*)(), class Foo);", Style); 25162 25163 verifyFormat("Foo::operator&();", Style); 25164 verifyFormat("Foo::operator void&();", Style); 25165 verifyFormat("Foo::operator void const&();", Style); 25166 verifyFormat("Foo::operator/*comment*/ void&();", Style); 25167 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 25168 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 25169 verifyFormat("Foo::operator()(void&);", Style); 25170 verifyFormat("Foo::operator&(void&);", Style); 25171 verifyFormat("Foo::operator&();", Style); 25172 verifyFormat("operator&(int (&)(), class Foo);", Style); 25173 verifyFormat("operator&(int (&&)(), class Foo);", Style); 25174 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 25175 25176 verifyFormat("Foo::operator&&();", Style); 25177 verifyFormat("Foo::operator void&&();", Style); 25178 verifyFormat("Foo::operator void const&&();", Style); 25179 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 25180 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 25181 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 25182 verifyFormat("Foo::operator()(void&&);", Style); 25183 verifyFormat("Foo::operator&&(void&&);", Style); 25184 verifyFormat("Foo::operator&&();", Style); 25185 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 25186 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 25187 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 25188 Style); 25189 verifyFormat("operator void**()", Style); 25190 verifyFormat("operator const FooLeft<Object>&()", Style); 25191 verifyFormat("operator const FooLeft<Object>*()", Style); 25192 verifyFormat("operator const FooLeft<Object>**()", Style); 25193 verifyFormat("operator const FooLeft<Object>*&()", Style); 25194 verifyFormat("operator const FooLeft<Object>*&&()", Style); 25195 25196 // PR45107 25197 verifyFormat("operator Vector<String>&();", Style); 25198 verifyFormat("operator const Vector<String>&();", Style); 25199 verifyFormat("operator foo::Bar*();", Style); 25200 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 25201 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 25202 Style); 25203 25204 Style.PointerAlignment = FormatStyle::PAS_Middle; 25205 verifyFormat("Foo::operator*();", Style); 25206 verifyFormat("Foo::operator void *();", Style); 25207 verifyFormat("Foo::operator()(void *);", Style); 25208 verifyFormat("Foo::operator*(void *);", Style); 25209 verifyFormat("Foo::operator*();", Style); 25210 verifyFormat("operator*(int (*)(), class Foo);", Style); 25211 25212 verifyFormat("Foo::operator&();", Style); 25213 verifyFormat("Foo::operator void &();", Style); 25214 verifyFormat("Foo::operator void const &();", Style); 25215 verifyFormat("Foo::operator()(void &);", Style); 25216 verifyFormat("Foo::operator&(void &);", Style); 25217 verifyFormat("Foo::operator&();", Style); 25218 verifyFormat("operator&(int (&)(), class Foo);", Style); 25219 25220 verifyFormat("Foo::operator&&();", Style); 25221 verifyFormat("Foo::operator void &&();", Style); 25222 verifyFormat("Foo::operator void const &&();", Style); 25223 verifyFormat("Foo::operator()(void &&);", Style); 25224 verifyFormat("Foo::operator&&(void &&);", Style); 25225 verifyFormat("Foo::operator&&();", Style); 25226 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 25227 } 25228 25229 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 25230 FormatStyle Style = getLLVMStyle(); 25231 // PR46157 25232 verifyFormat("foo(operator+, -42);", Style); 25233 verifyFormat("foo(operator++, -42);", Style); 25234 verifyFormat("foo(operator--, -42);", Style); 25235 verifyFormat("foo(-42, operator--);", Style); 25236 verifyFormat("foo(-42, operator, );", Style); 25237 verifyFormat("foo(operator, , -42);", Style); 25238 } 25239 25240 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 25241 FormatStyle Style = getLLVMStyle(); 25242 Style.WhitespaceSensitiveMacros.push_back("FOO"); 25243 25244 // Newlines are important here. 25245 verifyNoChange("FOO(1+2 )\n", Style); 25246 verifyNoChange("FOO(a:b:c)\n", Style); 25247 25248 // Don't use the helpers here, since 'mess up' will change the whitespace 25249 // and these are all whitespace sensitive by definition 25250 verifyNoChange("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style); 25251 verifyNoChange("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style); 25252 verifyNoChange("FOO(String-ized&Messy+But,: :Still=Intentional);", Style); 25253 verifyNoChange("FOO(String-ized&Messy+But,: :\n" 25254 " Still=Intentional);", 25255 Style); 25256 Style.AlignConsecutiveAssignments.Enabled = true; 25257 verifyNoChange("FOO(String-ized=&Messy+But,: :\n" 25258 " Still=Intentional);", 25259 Style); 25260 25261 Style.ColumnLimit = 21; 25262 verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style); 25263 } 25264 25265 TEST_F(FormatTest, SkipMacroDefinitionBody) { 25266 auto Style = getLLVMStyle(); 25267 Style.SkipMacroDefinitionBody = true; 25268 25269 verifyFormat("#define A", "#define A", Style); 25270 verifyFormat("#define A a aa", "#define A a aa", Style); 25271 verifyNoChange("#define A b", Style); 25272 verifyNoChange("#define A ( args )", Style); 25273 verifyNoChange("#define A ( args ) = func ( args )", Style); 25274 verifyNoChange("#define A ( args ) { int a = 1 ; }", Style); 25275 verifyNoChange("#define A ( args ) \\\n" 25276 " {\\\n" 25277 " int a = 1 ;\\\n" 25278 "}", 25279 Style); 25280 25281 verifyNoChange("#define A x:", Style); 25282 verifyNoChange("#define A a. b", Style); 25283 25284 // Surrounded with formatted code. 25285 verifyFormat("int a;\n" 25286 "#define A a\n" 25287 "int a;", 25288 "int a ;\n" 25289 "#define A a\n" 25290 "int a ;", 25291 Style); 25292 25293 // Columns are not broken when a limit is set. 25294 Style.ColumnLimit = 10; 25295 verifyFormat("#define A a a a a", " # define A a a a a ", Style); 25296 verifyNoChange("#define A a a a a", Style); 25297 25298 Style.ColumnLimit = 15; 25299 verifyFormat("#define A // a\n" 25300 " // very\n" 25301 " // long\n" 25302 " // comment", 25303 "#define A //a very long comment", Style); 25304 Style.ColumnLimit = 0; 25305 25306 // Multiline definition. 25307 verifyNoChange("#define A \\\n" 25308 "Line one with spaces . \\\n" 25309 " Line two.", 25310 Style); 25311 verifyNoChange("#define A \\\n" 25312 "a a \\\n" 25313 "a \\\n" 25314 "a", 25315 Style); 25316 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 25317 verifyNoChange("#define A \\\n" 25318 "a a \\\n" 25319 "a \\\n" 25320 "a", 25321 Style); 25322 Style.AlignEscapedNewlines = FormatStyle::ENAS_Right; 25323 verifyNoChange("#define A \\\n" 25324 "a a \\\n" 25325 "a \\\n" 25326 "a", 25327 Style); 25328 25329 // Adjust indendations but don't change the definition. 25330 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 25331 verifyNoChange("#if A\n" 25332 "#define A a\n" 25333 "#endif", 25334 Style); 25335 verifyFormat("#if A\n" 25336 "#define A a\n" 25337 "#endif", 25338 "#if A\n" 25339 " #define A a\n" 25340 "#endif", 25341 Style); 25342 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 25343 verifyNoChange("#if A\n" 25344 "# define A a\n" 25345 "#endif", 25346 Style); 25347 verifyFormat("#if A\n" 25348 "# define A a\n" 25349 "#endif", 25350 "#if A\n" 25351 " #define A a\n" 25352 "#endif", 25353 Style); 25354 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 25355 verifyNoChange("#if A\n" 25356 " #define A a\n" 25357 "#endif", 25358 Style); 25359 verifyFormat("#if A\n" 25360 " #define A a\n" 25361 "#endif", 25362 "#if A\n" 25363 " # define A a\n" 25364 "#endif", 25365 Style); 25366 25367 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 25368 // SkipMacroDefinitionBody should not affect other PP directives 25369 verifyFormat("#if !defined(A)\n" 25370 "#define A a\n" 25371 "#endif", 25372 "#if ! defined ( A )\n" 25373 " #define A a\n" 25374 "#endif", 25375 Style); 25376 25377 // With comments. 25378 verifyFormat("/* */ #define A a // a a", "/* */ # define A a // a a", 25379 Style); 25380 verifyNoChange("/* */ #define A a // a a", Style); 25381 25382 verifyFormat("int a; // a\n" 25383 "#define A // a\n" 25384 "int aaa; // a", 25385 "int a; // a\n" 25386 "#define A // a\n" 25387 "int aaa; // a", 25388 Style); 25389 25390 verifyNoChange( 25391 "#define MACRO_WITH_COMMENTS() \\\n" 25392 " public: \\\n" 25393 " /* Documentation parsed by Doxygen for the following method. */ \\\n" 25394 " static MyType getClassTypeId(); \\\n" 25395 " /** Normal comment for the following method. */ \\\n" 25396 " virtual MyType getTypeId() const;", 25397 Style); 25398 25399 // multiline macro definitions 25400 verifyNoChange("#define A a\\\n" 25401 " A a \\\n " 25402 " A a", 25403 Style); 25404 } 25405 25406 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 25407 // These tests are not in NamespaceEndCommentsFixerTest because that doesn't 25408 // test its interaction with line wrapping 25409 FormatStyle Style = getLLVMStyleWithColumns(80); 25410 verifyFormat("namespace {\n" 25411 "int i;\n" 25412 "int j;\n" 25413 "} // namespace", 25414 Style); 25415 25416 verifyFormat("namespace AAA {\n" 25417 "int i;\n" 25418 "int j;\n" 25419 "} // namespace AAA", 25420 Style); 25421 25422 verifyFormat("namespace Averyveryveryverylongnamespace {\n" 25423 "int i;\n" 25424 "int j;\n" 25425 "} // namespace Averyveryveryverylongnamespace", 25426 "namespace Averyveryveryverylongnamespace {\n" 25427 "int i;\n" 25428 "int j;\n" 25429 "}", 25430 Style); 25431 25432 verifyFormat( 25433 "namespace " 25434 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 25435 " went::mad::now {\n" 25436 "int i;\n" 25437 "int j;\n" 25438 "} // namespace\n" 25439 " // " 25440 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 25441 "went::mad::now", 25442 "namespace " 25443 "would::it::save::you::a::lot::of::time::if_::i::" 25444 "just::gave::up::and_::went::mad::now {\n" 25445 "int i;\n" 25446 "int j;\n" 25447 "}", 25448 Style); 25449 25450 // This used to duplicate the comment again and again on subsequent runs 25451 verifyFormat( 25452 "namespace " 25453 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 25454 " went::mad::now {\n" 25455 "int i;\n" 25456 "int j;\n" 25457 "} // namespace\n" 25458 " // " 25459 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 25460 "went::mad::now", 25461 "namespace " 25462 "would::it::save::you::a::lot::of::time::if_::i::" 25463 "just::gave::up::and_::went::mad::now {\n" 25464 "int i;\n" 25465 "int j;\n" 25466 "} // namespace\n" 25467 " // " 25468 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 25469 "and_::went::mad::now", 25470 Style); 25471 } 25472 25473 TEST_F(FormatTest, LikelyUnlikely) { 25474 FormatStyle Style = getLLVMStyle(); 25475 25476 verifyFormat("if (argc > 5) [[unlikely]] {\n" 25477 " return 29;\n" 25478 "}", 25479 Style); 25480 25481 verifyFormat("if (argc > 5) [[likely]] {\n" 25482 " return 29;\n" 25483 "}", 25484 Style); 25485 25486 verifyFormat("if (argc > 5) [[unlikely]] {\n" 25487 " return 29;\n" 25488 "} else [[likely]] {\n" 25489 " return 42;\n" 25490 "}", 25491 Style); 25492 25493 verifyFormat("if (argc > 5) [[unlikely]] {\n" 25494 " return 29;\n" 25495 "} else if (argc > 10) [[likely]] {\n" 25496 " return 99;\n" 25497 "} else {\n" 25498 " return 42;\n" 25499 "}", 25500 Style); 25501 25502 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 25503 " return 29;\n" 25504 "}", 25505 Style); 25506 25507 verifyFormat("if (argc > 5) [[unlikely]]\n" 25508 " return 29;", 25509 Style); 25510 verifyFormat("if (argc > 5) [[likely]]\n" 25511 " return 29;", 25512 Style); 25513 25514 verifyFormat("while (limit > 0) [[unlikely]] {\n" 25515 " --limit;\n" 25516 "}", 25517 Style); 25518 verifyFormat("for (auto &limit : limits) [[likely]] {\n" 25519 " --limit;\n" 25520 "}", 25521 Style); 25522 25523 verifyFormat("for (auto &limit : limits) [[unlikely]]\n" 25524 " --limit;", 25525 Style); 25526 verifyFormat("while (limit > 0) [[likely]]\n" 25527 " --limit;", 25528 Style); 25529 25530 Style.AttributeMacros.push_back("UNLIKELY"); 25531 Style.AttributeMacros.push_back("LIKELY"); 25532 verifyFormat("if (argc > 5) UNLIKELY\n" 25533 " return 29;", 25534 Style); 25535 25536 verifyFormat("if (argc > 5) UNLIKELY {\n" 25537 " return 29;\n" 25538 "}", 25539 Style); 25540 verifyFormat("if (argc > 5) UNLIKELY {\n" 25541 " return 29;\n" 25542 "} else [[likely]] {\n" 25543 " return 42;\n" 25544 "}", 25545 Style); 25546 verifyFormat("if (argc > 5) UNLIKELY {\n" 25547 " return 29;\n" 25548 "} else LIKELY {\n" 25549 " return 42;\n" 25550 "}", 25551 Style); 25552 verifyFormat("if (argc > 5) [[unlikely]] {\n" 25553 " return 29;\n" 25554 "} else LIKELY {\n" 25555 " return 42;\n" 25556 "}", 25557 Style); 25558 25559 verifyFormat("for (auto &limit : limits) UNLIKELY {\n" 25560 " --limit;\n" 25561 "}", 25562 Style); 25563 verifyFormat("while (limit > 0) LIKELY {\n" 25564 " --limit;\n" 25565 "}", 25566 Style); 25567 25568 verifyFormat("while (limit > 0) UNLIKELY\n" 25569 " --limit;", 25570 Style); 25571 verifyFormat("for (auto &limit : limits) LIKELY\n" 25572 " --limit;", 25573 Style); 25574 } 25575 25576 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 25577 verifyFormat("Constructor()\n" 25578 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 25579 " aaaa(aaaaaaaaaaaaaaaaaa, " 25580 "aaaaaaaaaaaaaaaaaat))"); 25581 verifyFormat("Constructor()\n" 25582 " : aaaaaaaaaaaaa(aaaaaa), " 25583 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 25584 25585 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 25586 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 25587 verifyFormat("Constructor()\n" 25588 " : aaaaaa(aaaaaa),\n" 25589 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 25590 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 25591 StyleWithWhitespacePenalty); 25592 verifyFormat("Constructor()\n" 25593 " : aaaaaaaaaaaaa(aaaaaa), " 25594 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 25595 StyleWithWhitespacePenalty); 25596 } 25597 25598 TEST_F(FormatTest, LLVMDefaultStyle) { 25599 FormatStyle Style = getLLVMStyle(); 25600 verifyFormat("extern \"C\" {\n" 25601 "int foo();\n" 25602 "}", 25603 Style); 25604 } 25605 TEST_F(FormatTest, GNUDefaultStyle) { 25606 FormatStyle Style = getGNUStyle(); 25607 verifyFormat("extern \"C\"\n" 25608 "{\n" 25609 " int foo ();\n" 25610 "}", 25611 Style); 25612 } 25613 TEST_F(FormatTest, MozillaDefaultStyle) { 25614 FormatStyle Style = getMozillaStyle(); 25615 verifyFormat("extern \"C\"\n" 25616 "{\n" 25617 " int foo();\n" 25618 "}", 25619 Style); 25620 } 25621 TEST_F(FormatTest, GoogleDefaultStyle) { 25622 FormatStyle Style = getGoogleStyle(); 25623 verifyFormat("extern \"C\" {\n" 25624 "int foo();\n" 25625 "}", 25626 Style); 25627 } 25628 TEST_F(FormatTest, ChromiumDefaultStyle) { 25629 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 25630 verifyFormat("extern \"C\" {\n" 25631 "int foo();\n" 25632 "}", 25633 Style); 25634 } 25635 TEST_F(FormatTest, MicrosoftDefaultStyle) { 25636 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 25637 verifyFormat("extern \"C\"\n" 25638 "{\n" 25639 " int foo();\n" 25640 "}", 25641 Style); 25642 } 25643 TEST_F(FormatTest, WebKitDefaultStyle) { 25644 FormatStyle Style = getWebKitStyle(); 25645 verifyFormat("extern \"C\" {\n" 25646 "int foo();\n" 25647 "}", 25648 Style); 25649 } 25650 25651 TEST_F(FormatTest, Concepts) { 25652 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations, 25653 FormatStyle::BBCDS_Always); 25654 25655 // The default in LLVM style is REI_OuterScope, but these tests were written 25656 // when the default was REI_Keyword. 25657 FormatStyle Style = getLLVMStyle(); 25658 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword; 25659 25660 verifyFormat("template <typename T>\n" 25661 "concept True = true;"); 25662 25663 verifyFormat("template <typename T>\n" 25664 "concept C = ((false || foo()) && C2<T>) ||\n" 25665 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;", 25666 getLLVMStyleWithColumns(60)); 25667 25668 verifyFormat("template <typename T>\n" 25669 "concept DelayedCheck = true && requires(T t) { t.bar(); } && " 25670 "sizeof(T) <= 8;"); 25671 25672 verifyFormat("template <typename T>\n" 25673 "concept DelayedCheck = true && requires(T t) {\n" 25674 " t.bar();\n" 25675 " t.baz();\n" 25676 " } && sizeof(T) <= 8;", 25677 Style); 25678 25679 verifyFormat("template <typename T>\n" 25680 "concept DelayedCheck = true && requires(T t) { // Comment\n" 25681 " t.bar();\n" 25682 " t.baz();\n" 25683 " } && sizeof(T) <= 8;", 25684 Style); 25685 25686 verifyFormat("template <typename T>\n" 25687 "concept DelayedCheck = false || requires(T t) { t.bar(); } && " 25688 "sizeof(T) <= 8;"); 25689 25690 verifyFormat("template <typename T>\n" 25691 "concept DelayedCheck = Unit<T> && !DerivedUnit<T>;"); 25692 25693 verifyFormat("template <typename T>\n" 25694 "concept DelayedCheck = Unit<T> && !(DerivedUnit<T>);"); 25695 25696 verifyFormat("template <typename T>\n" 25697 "concept DelayedCheck = Unit<T> && !!DerivedUnit<T>;"); 25698 25699 verifyFormat("template <typename T>\n" 25700 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } " 25701 "&& sizeof(T) <= 8;"); 25702 25703 verifyFormat("template <typename T>\n" 25704 "concept DelayedCheck =\n" 25705 " static_cast<bool>(0) || requires(T t) { t.bar(); } && " 25706 "sizeof(T) <= 8;"); 25707 25708 verifyFormat("template <typename T>\n" 25709 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } " 25710 "&& sizeof(T) <= 8;"); 25711 25712 verifyFormat( 25713 "template <typename T>\n" 25714 "concept DelayedCheck =\n" 25715 " (bool)(0) || requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 25716 25717 verifyFormat("template <typename T>\n" 25718 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } " 25719 "&& sizeof(T) <= 8;"); 25720 25721 verifyFormat("template <typename T>\n" 25722 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && " 25723 "sizeof(T) <= 8;"); 25724 25725 verifyFormat("template <typename T>\n" 25726 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n" 25727 " requires(T t) {\n" 25728 " t.bar();\n" 25729 " t.baz();\n" 25730 " } && sizeof(T) <= 8 && !(4 < 3);", 25731 getLLVMStyleWithColumns(60)); 25732 25733 verifyFormat("template <typename T>\n" 25734 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;"); 25735 25736 verifyFormat("template <typename T>\n" 25737 "concept C = foo();"); 25738 25739 verifyFormat("template <typename T>\n" 25740 "concept C = foo(T());"); 25741 25742 verifyFormat("template <typename T>\n" 25743 "concept C = foo(T{});"); 25744 25745 verifyFormat("template <typename T>\n" 25746 "concept Size = V<sizeof(T)>::Value > 5;"); 25747 25748 verifyFormat("template <typename T>\n" 25749 "concept True = S<T>::Value;"); 25750 25751 verifyFormat("template <S T>\n" 25752 "concept True = T.field;"); 25753 25754 verifyFormat( 25755 "template <typename T>\n" 25756 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n" 25757 " sizeof(T) <= 8;"); 25758 25759 // FIXME: This is misformatted because the fake l paren starts at bool, not at 25760 // the lambda l square. 25761 verifyFormat("template <typename T>\n" 25762 "concept C = [] -> bool { return true; }() && requires(T t) { " 25763 "t.bar(); } &&\n" 25764 " sizeof(T) <= 8;"); 25765 25766 verifyFormat( 25767 "template <typename T>\n" 25768 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n" 25769 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 25770 25771 verifyFormat("template <typename T>\n" 25772 "concept C = decltype([]() { return std::true_type{}; " 25773 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;", 25774 getLLVMStyleWithColumns(120)); 25775 25776 verifyFormat("template <typename T>\n" 25777 "concept C = decltype([]() -> std::true_type { return {}; " 25778 "}())::value &&\n" 25779 " requires(T t) { t.bar(); } && sizeof(T) <= 8;"); 25780 25781 verifyFormat("template <typename T>\n" 25782 "concept C = true;\n" 25783 "Foo Bar;"); 25784 25785 verifyFormat("template <typename T>\n" 25786 "concept Hashable = requires(T a) {\n" 25787 " { std::hash<T>{}(a) } -> " 25788 "std::convertible_to<std::size_t>;\n" 25789 " };", 25790 Style); 25791 25792 verifyFormat( 25793 "template <typename T>\n" 25794 "concept EqualityComparable = requires(T a, T b) {\n" 25795 " { a == b } -> std::same_as<bool>;\n" 25796 " };", 25797 Style); 25798 25799 verifyFormat( 25800 "template <typename T>\n" 25801 "concept EqualityComparable = requires(T a, T b) {\n" 25802 " { a == b } -> std::same_as<bool>;\n" 25803 " { a != b } -> std::same_as<bool>;\n" 25804 " };", 25805 Style); 25806 25807 verifyFormat("template <typename T>\n" 25808 "concept WeakEqualityComparable = requires(T a, T b) {\n" 25809 " { a == b };\n" 25810 " { a != b };\n" 25811 " };", 25812 Style); 25813 25814 verifyFormat("template <typename T>\n" 25815 "concept HasSizeT = requires { typename T::size_t; };"); 25816 25817 verifyFormat("template <typename T>\n" 25818 "concept Semiregular =\n" 25819 " DefaultConstructible<T> && CopyConstructible<T> && " 25820 "CopyAssignable<T> &&\n" 25821 " requires(T a, std::size_t n) {\n" 25822 " requires Same<T *, decltype(&a)>;\n" 25823 " { a.~T() } noexcept;\n" 25824 " requires Same<T *, decltype(new T)>;\n" 25825 " requires Same<T *, decltype(new T[n])>;\n" 25826 " { delete new T; };\n" 25827 " { delete new T[n]; };\n" 25828 " };", 25829 Style); 25830 25831 verifyFormat("template <typename T>\n" 25832 "concept Semiregular =\n" 25833 " requires(T a, std::size_t n) {\n" 25834 " requires Same<T *, decltype(&a)>;\n" 25835 " { a.~T() } noexcept;\n" 25836 " requires Same<T *, decltype(new T)>;\n" 25837 " requires Same<T *, decltype(new T[n])>;\n" 25838 " { delete new T; };\n" 25839 " { delete new T[n]; };\n" 25840 " { new T } -> std::same_as<T *>;\n" 25841 " } && DefaultConstructible<T> && CopyConstructible<T> && " 25842 "CopyAssignable<T>;", 25843 Style); 25844 25845 verifyFormat( 25846 "template <typename T>\n" 25847 "concept Semiregular =\n" 25848 " DefaultConstructible<T> && requires(T a, std::size_t n) {\n" 25849 " requires Same<T *, decltype(&a)>;\n" 25850 " { a.~T() } noexcept;\n" 25851 " requires Same<T *, decltype(new T)>;\n" 25852 " requires Same<T *, decltype(new " 25853 "T[n])>;\n" 25854 " { delete new T; };\n" 25855 " { delete new T[n]; };\n" 25856 " } && CopyConstructible<T> && " 25857 "CopyAssignable<T>;", 25858 Style); 25859 25860 verifyFormat("template <typename T>\n" 25861 "concept Two = requires(T t) {\n" 25862 " { t.foo() } -> std::same_as<Bar>;\n" 25863 " } && requires(T &&t) {\n" 25864 " { t.foo() } -> std::same_as<Bar &&>;\n" 25865 " };", 25866 Style); 25867 25868 verifyFormat( 25869 "template <typename T>\n" 25870 "concept C = requires(T x) {\n" 25871 " { *x } -> std::convertible_to<typename T::inner>;\n" 25872 " { x + 1 } noexcept -> std::same_as<int>;\n" 25873 " { x * 1 } -> std::convertible_to<T>;\n" 25874 " };", 25875 Style); 25876 25877 verifyFormat("template <typename T>\n" 25878 "concept C = requires(T x) {\n" 25879 " {\n" 25880 " long_long_long_function_call(1, 2, 3, 4, 5)\n" 25881 " } -> long_long_concept_name<T>;\n" 25882 " {\n" 25883 " long_long_long_function_call(1, 2, 3, 4, 5)\n" 25884 " } noexcept -> long_long_concept_name<T>;\n" 25885 " };", 25886 Style); 25887 25888 verifyFormat( 25889 "template <typename T, typename U = T>\n" 25890 "concept Swappable = requires(T &&t, U &&u) {\n" 25891 " swap(std::forward<T>(t), std::forward<U>(u));\n" 25892 " swap(std::forward<U>(u), std::forward<T>(t));\n" 25893 " };", 25894 Style); 25895 25896 verifyFormat("template <typename T, typename U>\n" 25897 "concept Common = requires(T &&t, U &&u) {\n" 25898 " typename CommonType<T, U>;\n" 25899 " { CommonType<T, U>(std::forward<T>(t)) };\n" 25900 " };", 25901 Style); 25902 25903 verifyFormat("template <typename T, typename U>\n" 25904 "concept Common = requires(T &&t, U &&u) {\n" 25905 " typename CommonType<T, U>;\n" 25906 " { CommonType<T, U>{std::forward<T>(t)} };\n" 25907 " };", 25908 Style); 25909 25910 verifyFormat( 25911 "template <typename T>\n" 25912 "concept C = requires(T t) {\n" 25913 " requires Bar<T> && Foo<T>;\n" 25914 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" 25915 " };", 25916 Style); 25917 25918 verifyFormat("template <typename T>\n" 25919 "concept HasFoo = requires(T t) {\n" 25920 " { t.foo() };\n" 25921 " t.foo();\n" 25922 " };\n" 25923 "template <typename T>\n" 25924 "concept HasBar = requires(T t) {\n" 25925 " { t.bar() };\n" 25926 " t.bar();\n" 25927 " };", 25928 Style); 25929 25930 verifyFormat("template <typename T>\n" 25931 "concept Large = sizeof(T) > 10;"); 25932 25933 verifyFormat("template <typename T, typename U>\n" 25934 "concept FooableWith = requires(T t, U u) {\n" 25935 " typename T::foo_type;\n" 25936 " { t.foo(u) } -> typename T::foo_type;\n" 25937 " t++;\n" 25938 " };\n" 25939 "void doFoo(FooableWith<int> auto t) { t.foo(3); }", 25940 Style); 25941 25942 verifyFormat("template <typename T>\n" 25943 "concept Context = is_specialization_of_v<context, T>;"); 25944 25945 verifyFormat("template <typename T>\n" 25946 "concept Node = std::is_object_v<T>;"); 25947 25948 verifyFormat("template <class T>\n" 25949 "concept integral = __is_integral(T);"); 25950 25951 verifyFormat("template <class T>\n" 25952 "concept is2D = __array_extent(T, 1) == 2;"); 25953 25954 verifyFormat("template <class T>\n" 25955 "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)"); 25956 25957 verifyFormat("template <class T, class T2>\n" 25958 "concept Same = __is_same_as<T, T2>;"); 25959 25960 verifyFormat( 25961 "template <class _InIt, class _OutIt>\n" 25962 "concept _Can_reread_dest =\n" 25963 " std::forward_iterator<_OutIt> &&\n" 25964 " std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;"); 25965 25966 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Allowed; 25967 25968 verifyFormat( 25969 "template <typename T>\n" 25970 "concept C = requires(T t) {\n" 25971 " requires Bar<T> && Foo<T>;\n" 25972 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" 25973 " };", 25974 Style); 25975 25976 verifyFormat("template <typename T>\n" 25977 "concept HasFoo = requires(T t) {\n" 25978 " { t.foo() };\n" 25979 " t.foo();\n" 25980 " };\n" 25981 "template <typename T>\n" 25982 "concept HasBar = requires(T t) {\n" 25983 " { t.bar() };\n" 25984 " t.bar();\n" 25985 " };", 25986 Style); 25987 25988 verifyFormat("template <typename T> concept True = true;", Style); 25989 25990 verifyFormat("template <typename T>\n" 25991 "concept C = decltype([]() -> std::true_type { return {}; " 25992 "}())::value &&\n" 25993 " requires(T t) { t.bar(); } && sizeof(T) <= 8;", 25994 Style); 25995 25996 verifyFormat("template <typename T>\n" 25997 "concept Semiregular =\n" 25998 " DefaultConstructible<T> && CopyConstructible<T> && " 25999 "CopyAssignable<T> &&\n" 26000 " requires(T a, std::size_t n) {\n" 26001 " requires Same<T *, decltype(&a)>;\n" 26002 " { a.~T() } noexcept;\n" 26003 " requires Same<T *, decltype(new T)>;\n" 26004 " requires Same<T *, decltype(new T[n])>;\n" 26005 " { delete new T; };\n" 26006 " { delete new T[n]; };\n" 26007 " };", 26008 Style); 26009 26010 Style.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Never; 26011 26012 verifyFormat("template <typename T> concept C =\n" 26013 " requires(T t) {\n" 26014 " requires Bar<T> && Foo<T>;\n" 26015 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n" 26016 " };", 26017 Style); 26018 26019 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n" 26020 " { t.foo() };\n" 26021 " t.foo();\n" 26022 " };\n" 26023 "template <typename T> concept HasBar = requires(T t) {\n" 26024 " { t.bar() };\n" 26025 " t.bar();\n" 26026 " };", 26027 Style); 26028 26029 verifyFormat("template <typename T> concept True = true;", Style); 26030 26031 verifyFormat( 26032 "template <typename T> concept C =\n" 26033 " decltype([]() -> std::true_type { return {}; }())::value &&\n" 26034 " requires(T t) { t.bar(); } && sizeof(T) <= 8;", 26035 Style); 26036 26037 verifyFormat("template <typename T> concept Semiregular =\n" 26038 " DefaultConstructible<T> && CopyConstructible<T> && " 26039 "CopyAssignable<T> &&\n" 26040 " requires(T a, std::size_t n) {\n" 26041 " requires Same<T *, decltype(&a)>;\n" 26042 " { a.~T() } noexcept;\n" 26043 " requires Same<T *, decltype(new T)>;\n" 26044 " requires Same<T *, decltype(new T[n])>;\n" 26045 " { delete new T; };\n" 26046 " { delete new T[n]; };\n" 26047 " };", 26048 Style); 26049 26050 // The following tests are invalid C++, we just want to make sure we don't 26051 // assert. 26052 verifyNoCrash("template <typename T>\n" 26053 "concept C = requires C2<T>;"); 26054 26055 verifyNoCrash("template <typename T>\n" 26056 "concept C = 5 + 4;"); 26057 26058 verifyNoCrash("template <typename T>\n" 26059 "concept C = class X;"); 26060 26061 verifyNoCrash("template <typename T>\n" 26062 "concept C = [] && true;"); 26063 26064 verifyNoCrash("template <typename T>\n" 26065 "concept C = [] && requires(T t) { typename T::size_type; };"); 26066 } 26067 26068 TEST_F(FormatTest, RequiresClausesPositions) { 26069 auto Style = getLLVMStyle(); 26070 EXPECT_EQ(Style.RequiresClausePosition, FormatStyle::RCPS_OwnLine); 26071 EXPECT_EQ(Style.IndentRequiresClause, true); 26072 26073 // The default in LLVM style is REI_OuterScope, but these tests were written 26074 // when the default was REI_Keyword. 26075 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword; 26076 26077 verifyFormat("template <typename T>\n" 26078 " requires(Foo<T> && std::trait<T>)\n" 26079 "struct Bar;", 26080 Style); 26081 26082 verifyFormat("template <typename T>\n" 26083 " requires(Foo<T> && std::trait<T>)\n" 26084 "class Bar {\n" 26085 "public:\n" 26086 " Bar(T t);\n" 26087 " bool baz();\n" 26088 "};", 26089 Style); 26090 26091 verifyFormat( 26092 "template <typename T>\n" 26093 " requires requires(T &&t) {\n" 26094 " typename T::I;\n" 26095 " requires(F<typename T::I> && std::trait<typename T::I>);\n" 26096 " }\n" 26097 "Bar(T) -> Bar<typename T::I>;", 26098 Style); 26099 26100 verifyFormat("template <typename T>\n" 26101 " requires(Foo<T> && std::trait<T>)\n" 26102 "constexpr T MyGlobal;", 26103 Style); 26104 26105 verifyFormat("template <typename T>\n" 26106 " requires Foo<T> && requires(T t) {\n" 26107 " { t.baz() } -> std::same_as<bool>;\n" 26108 " requires std::same_as<T::Factor, int>;\n" 26109 " }\n" 26110 "inline int bar(T t) {\n" 26111 " return t.baz() ? T::Factor : 5;\n" 26112 "}", 26113 Style); 26114 26115 verifyFormat("template <typename T>\n" 26116 "inline int bar(T t)\n" 26117 " requires Foo<T> && requires(T t) {\n" 26118 " { t.baz() } -> std::same_as<bool>;\n" 26119 " requires std::same_as<T::Factor, int>;\n" 26120 " }\n" 26121 "{\n" 26122 " return t.baz() ? T::Factor : 5;\n" 26123 "}", 26124 Style); 26125 26126 verifyFormat("template <typename T>\n" 26127 " requires F<T>\n" 26128 "int bar(T t) {\n" 26129 " return 5;\n" 26130 "}", 26131 Style); 26132 26133 verifyFormat("template <typename T>\n" 26134 "int bar(T t)\n" 26135 " requires F<T>\n" 26136 "{\n" 26137 " return 5;\n" 26138 "}", 26139 Style); 26140 26141 verifyFormat("template <typename T>\n" 26142 "int S::bar(T t) &&\n" 26143 " requires F<T>\n" 26144 "{\n" 26145 " return 5;\n" 26146 "}", 26147 Style); 26148 26149 verifyFormat("template <typename T>\n" 26150 "int bar(T t)\n" 26151 " requires F<T>;", 26152 Style); 26153 26154 Style.IndentRequiresClause = false; 26155 verifyFormat("template <typename T>\n" 26156 "requires F<T>\n" 26157 "int bar(T t) {\n" 26158 " return 5;\n" 26159 "}", 26160 Style); 26161 26162 verifyFormat("template <typename T>\n" 26163 "int S::bar(T t) &&\n" 26164 "requires F<T>\n" 26165 "{\n" 26166 " return 5;\n" 26167 "}", 26168 Style); 26169 26170 verifyFormat("template <typename T>\n" 26171 "int bar(T t)\n" 26172 "requires F<T>\n" 26173 "{\n" 26174 " return 5;\n" 26175 "}", 26176 Style); 26177 26178 Style.RequiresClausePosition = FormatStyle::RCPS_OwnLineWithBrace; 26179 Style.IndentRequiresClause = true; 26180 26181 verifyFormat("template <typename T>\n" 26182 " requires(Foo<T> && std::trait<T>)\n" 26183 "struct Bar;", 26184 Style); 26185 26186 verifyFormat("template <typename T>\n" 26187 " requires(Foo<T> && std::trait<T>)\n" 26188 "class Bar {\n" 26189 "public:\n" 26190 " Bar(T t);\n" 26191 " bool baz();\n" 26192 "};", 26193 Style); 26194 26195 verifyFormat( 26196 "template <typename T>\n" 26197 " requires requires(T &&t) {\n" 26198 " typename T::I;\n" 26199 " requires(F<typename T::I> && std::trait<typename T::I>);\n" 26200 " }\n" 26201 "Bar(T) -> Bar<typename T::I>;", 26202 Style); 26203 26204 verifyFormat("template <typename T>\n" 26205 " requires(Foo<T> && std::trait<T>)\n" 26206 "constexpr T MyGlobal;", 26207 Style); 26208 26209 verifyFormat("template <typename T>\n" 26210 " requires Foo<T> && requires(T t) {\n" 26211 " { t.baz() } -> std::same_as<bool>;\n" 26212 " requires std::same_as<T::Factor, int>;\n" 26213 " }\n" 26214 "inline int bar(T t) {\n" 26215 " return t.baz() ? T::Factor : 5;\n" 26216 "}", 26217 Style); 26218 26219 verifyFormat("template <typename T>\n" 26220 "inline int bar(T t)\n" 26221 " requires Foo<T> && requires(T t) {\n" 26222 " { t.baz() } -> std::same_as<bool>;\n" 26223 " requires std::same_as<T::Factor, int>;\n" 26224 " } {\n" 26225 " return t.baz() ? T::Factor : 5;\n" 26226 "}", 26227 Style); 26228 26229 verifyFormat("template <typename T>\n" 26230 " requires F<T>\n" 26231 "int bar(T t) {\n" 26232 " return 5;\n" 26233 "}", 26234 Style); 26235 26236 verifyFormat("template <typename T>\n" 26237 "int bar(T t)\n" 26238 " requires F<T> {\n" 26239 " return 5;\n" 26240 "}", 26241 Style); 26242 26243 verifyFormat("template <typename T>\n" 26244 "int S::bar(T t) &&\n" 26245 " requires F<T> {\n" 26246 " return 5;\n" 26247 "}", 26248 Style); 26249 26250 verifyFormat("template <typename T>\n" 26251 "int bar(T t)\n" 26252 " requires F<T>;", 26253 Style); 26254 26255 verifyFormat("template <typename T>\n" 26256 "int bar(T t)\n" 26257 " requires F<T> {}", 26258 Style); 26259 26260 Style.RequiresClausePosition = FormatStyle::RCPS_SingleLine; 26261 Style.IndentRequiresClause = false; 26262 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n" 26263 "template <typename T> requires Foo<T> void bar() {}\n" 26264 "template <typename T> void bar() requires Foo<T> {}\n" 26265 "template <typename T> void bar() requires Foo<T>;\n" 26266 "template <typename T> void S::bar() && requires Foo<T> {}\n" 26267 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;", 26268 Style); 26269 26270 auto ColumnStyle = Style; 26271 ColumnStyle.ColumnLimit = 40; 26272 verifyFormat("template <typename AAAAAAA>\n" 26273 "requires Foo<T> struct Bar {};\n" 26274 "template <typename AAAAAAA>\n" 26275 "requires Foo<T> void bar() {}\n" 26276 "template <typename AAAAAAA>\n" 26277 "void bar() requires Foo<T> {}\n" 26278 "template <typename T>\n" 26279 "void S::bar() && requires Foo<T> {}\n" 26280 "template <typename AAAAAAA>\n" 26281 "requires Foo<T> Baz(T) -> Baz<T>;", 26282 ColumnStyle); 26283 26284 verifyFormat("template <typename T>\n" 26285 "requires Foo<AAAAAAA> struct Bar {};\n" 26286 "template <typename T>\n" 26287 "requires Foo<AAAAAAA> void bar() {}\n" 26288 "template <typename T>\n" 26289 "void bar() requires Foo<AAAAAAA> {}\n" 26290 "template <typename T>\n" 26291 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;", 26292 ColumnStyle); 26293 26294 verifyFormat("template <typename AAAAAAA>\n" 26295 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26296 "struct Bar {};\n" 26297 "template <typename AAAAAAA>\n" 26298 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26299 "void bar() {}\n" 26300 "template <typename AAAAAAA>\n" 26301 "void bar()\n" 26302 " requires Foo<AAAAAAAAAAAAAAAA> {}\n" 26303 "template <typename AAAAAAA>\n" 26304 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n" 26305 "template <typename AAAAAAA>\n" 26306 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26307 "Bar(T) -> Bar<T>;", 26308 ColumnStyle); 26309 26310 Style.RequiresClausePosition = FormatStyle::RCPS_WithFollowing; 26311 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithFollowing; 26312 26313 verifyFormat("template <typename T>\n" 26314 "requires Foo<T> struct Bar {};\n" 26315 "template <typename T>\n" 26316 "requires Foo<T> void bar() {}\n" 26317 "template <typename T>\n" 26318 "void bar()\n" 26319 "requires Foo<T> {}\n" 26320 "template <typename T>\n" 26321 "void bar()\n" 26322 "requires Foo<T>;\n" 26323 "template <typename T>\n" 26324 "void S::bar() &&\n" 26325 "requires Foo<T> {}\n" 26326 "template <typename T>\n" 26327 "requires Foo<T> Bar(T) -> Bar<T>;", 26328 Style); 26329 26330 verifyFormat("template <typename AAAAAAA>\n" 26331 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26332 "struct Bar {};\n" 26333 "template <typename AAAAAAA>\n" 26334 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26335 "void bar() {}\n" 26336 "template <typename AAAAAAA>\n" 26337 "void bar()\n" 26338 "requires Foo<AAAAAAAAAAAAAAAA> {}\n" 26339 "template <typename AAAAAAA>\n" 26340 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n" 26341 "template <typename AAAAAAA>\n" 26342 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26343 "Bar(T) -> Bar<T>;", 26344 ColumnStyle); 26345 26346 Style.IndentRequiresClause = true; 26347 ColumnStyle.IndentRequiresClause = true; 26348 26349 verifyFormat("template <typename T>\n" 26350 " requires Foo<T> struct Bar {};\n" 26351 "template <typename T>\n" 26352 " requires Foo<T> void bar() {}\n" 26353 "template <typename T>\n" 26354 "void bar()\n" 26355 " requires Foo<T> {}\n" 26356 "template <typename T>\n" 26357 "void S::bar() &&\n" 26358 " requires Foo<T> {}\n" 26359 "template <typename T>\n" 26360 " requires Foo<T> Bar(T) -> Bar<T>;", 26361 Style); 26362 26363 verifyFormat("template <typename AAAAAAA>\n" 26364 " requires Foo<AAAAAAAAAAAAAAAA>\n" 26365 "struct Bar {};\n" 26366 "template <typename AAAAAAA>\n" 26367 " requires Foo<AAAAAAAAAAAAAAAA>\n" 26368 "void bar() {}\n" 26369 "template <typename AAAAAAA>\n" 26370 "void bar()\n" 26371 " requires Foo<AAAAAAAAAAAAAAAA> {}\n" 26372 "template <typename AAAAAAA>\n" 26373 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n" 26374 "template <typename AAAAAAA>\n" 26375 " requires Foo<AAAAAAAAAAAAAAAA>\n" 26376 "Bar(T) -> Bar<T>;", 26377 ColumnStyle); 26378 26379 Style.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; 26380 ColumnStyle.RequiresClausePosition = FormatStyle::RCPS_WithPreceding; 26381 26382 verifyFormat("template <typename T> requires Foo<T>\n" 26383 "struct Bar {};\n" 26384 "template <typename T> requires Foo<T>\n" 26385 "void bar() {}\n" 26386 "template <typename T>\n" 26387 "void bar() requires Foo<T>\n" 26388 "{}\n" 26389 "template <typename T> void bar() requires Foo<T>;\n" 26390 "template <typename T>\n" 26391 "void S::bar() && requires Foo<T>\n" 26392 "{}\n" 26393 "template <typename T> requires Foo<T>\n" 26394 "Bar(T) -> Bar<T>;", 26395 Style); 26396 26397 verifyFormat("template <typename AAAAAAA>\n" 26398 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26399 "struct Bar {};\n" 26400 "template <typename AAAAAAA>\n" 26401 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26402 "void bar() {}\n" 26403 "template <typename AAAAAAA>\n" 26404 "void bar()\n" 26405 " requires Foo<AAAAAAAAAAAAAAAA>\n" 26406 "{}\n" 26407 "template <typename AAAAAAA>\n" 26408 "requires Foo<AAAAAAAA>\n" 26409 "Bar(T) -> Bar<T>;\n" 26410 "template <typename AAAAAAA>\n" 26411 "requires Foo<AAAAAAAAAAAAAAAA>\n" 26412 "Bar(T) -> Bar<T>;", 26413 ColumnStyle); 26414 } 26415 26416 TEST_F(FormatTest, RequiresClauses) { 26417 verifyFormat("struct [[nodiscard]] zero_t {\n" 26418 " template <class T>\n" 26419 " requires requires { number_zero_v<T>; }\n" 26420 " [[nodiscard]] constexpr operator T() const {\n" 26421 " return number_zero_v<T>;\n" 26422 " }\n" 26423 "};"); 26424 26425 verifyFormat("template <class T>\n" 26426 " requires(std::same_as<int, T>)\n" 26427 "decltype(auto) fun() {}"); 26428 26429 auto Style = getLLVMStyle(); 26430 26431 verifyFormat( 26432 "template <typename T>\n" 26433 " requires is_default_constructible_v<hash<T>> and\n" 26434 " is_copy_constructible_v<hash<T>> and\n" 26435 " is_move_constructible_v<hash<T>> and\n" 26436 " is_copy_assignable_v<hash<T>> and " 26437 "is_move_assignable_v<hash<T>> and\n" 26438 " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n" 26439 " is_callable_v<hash<T>(T)> and\n" 26440 " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n" 26441 " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n" 26442 " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n" 26443 "struct S {};", 26444 Style); 26445 26446 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 26447 verifyFormat( 26448 "template <typename T>\n" 26449 " requires is_default_constructible_v<hash<T>>\n" 26450 " and is_copy_constructible_v<hash<T>>\n" 26451 " and is_move_constructible_v<hash<T>>\n" 26452 " and is_copy_assignable_v<hash<T>> and " 26453 "is_move_assignable_v<hash<T>>\n" 26454 " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n" 26455 " and is_callable_v<hash<T>(T)>\n" 26456 " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n" 26457 " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n" 26458 " and is_same_v<size_t, decltype(hash<T>(declval<const T " 26459 "&>()))>\n" 26460 "struct S {};", 26461 Style); 26462 26463 Style = getLLVMStyle(); 26464 Style.ConstructorInitializerIndentWidth = 4; 26465 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 26466 Style.PackConstructorInitializers = FormatStyle::PCIS_Never; 26467 verifyFormat("constexpr Foo(Foo const &other)\n" 26468 " requires std::is_copy_constructible<T>\n" 26469 " : value{other.value} {\n" 26470 " do_magic();\n" 26471 " do_more_magic();\n" 26472 "}", 26473 Style); 26474 26475 // Not a clause, but we once hit an assert. 26476 verifyFormat("#if 0\n" 26477 "#else\n" 26478 "foo();\n" 26479 "#endif\n" 26480 "bar(requires);"); 26481 } 26482 26483 TEST_F(FormatTest, RequiresExpressionIndentation) { 26484 auto Style = getLLVMStyle(); 26485 EXPECT_EQ(Style.RequiresExpressionIndentation, FormatStyle::REI_OuterScope); 26486 26487 verifyFormat("template <typename T>\n" 26488 "concept C = requires(T t) {\n" 26489 " typename T::value;\n" 26490 " requires requires(typename T::value v) {\n" 26491 " { t == v } -> std::same_as<bool>;\n" 26492 " };\n" 26493 "};", 26494 Style); 26495 26496 verifyFormat("template <typename T>\n" 26497 "void bar(T)\n" 26498 " requires Foo<T> && requires(T t) {\n" 26499 " { t.foo() } -> std::same_as<int>;\n" 26500 " } && requires(T t) {\n" 26501 " { t.bar() } -> std::same_as<bool>;\n" 26502 " --t;\n" 26503 " };", 26504 Style); 26505 26506 verifyFormat("template <typename T>\n" 26507 " requires Foo<T> &&\n" 26508 " requires(T t) {\n" 26509 " { t.foo() } -> std::same_as<int>;\n" 26510 " } && requires(T t) {\n" 26511 " { t.bar() } -> std::same_as<bool>;\n" 26512 " --t;\n" 26513 " }\n" 26514 "void bar(T);", 26515 Style); 26516 26517 verifyFormat("template <typename T> void f() {\n" 26518 " if constexpr (requires(T t) {\n" 26519 " { t.bar() } -> std::same_as<bool>;\n" 26520 " }) {\n" 26521 " }\n" 26522 "}", 26523 Style); 26524 26525 verifyFormat("template <typename T> void f() {\n" 26526 " if constexpr (condition && requires(T t) {\n" 26527 " { t.bar() } -> std::same_as<bool>;\n" 26528 " }) {\n" 26529 " }\n" 26530 "}", 26531 Style); 26532 26533 verifyFormat("template <typename T> struct C {\n" 26534 " void f()\n" 26535 " requires requires(T t) {\n" 26536 " { t.bar() } -> std::same_as<bool>;\n" 26537 " };\n" 26538 "};", 26539 Style); 26540 26541 Style.RequiresExpressionIndentation = FormatStyle::REI_Keyword; 26542 26543 verifyFormat("template <typename T>\n" 26544 "concept C = requires(T t) {\n" 26545 " typename T::value;\n" 26546 " requires requires(typename T::value v) {\n" 26547 " { t == v } -> std::same_as<bool>;\n" 26548 " };\n" 26549 " };", 26550 Style); 26551 26552 verifyFormat( 26553 "template <typename T>\n" 26554 "void bar(T)\n" 26555 " requires Foo<T> && requires(T t) {\n" 26556 " { t.foo() } -> std::same_as<int>;\n" 26557 " } && requires(T t) {\n" 26558 " { t.bar() } -> std::same_as<bool>;\n" 26559 " --t;\n" 26560 " };", 26561 Style); 26562 26563 verifyFormat("template <typename T>\n" 26564 " requires Foo<T> &&\n" 26565 " requires(T t) {\n" 26566 " { t.foo() } -> std::same_as<int>;\n" 26567 " } && requires(T t) {\n" 26568 " { t.bar() } -> std::same_as<bool>;\n" 26569 " --t;\n" 26570 " }\n" 26571 "void bar(T);", 26572 Style); 26573 26574 verifyFormat("template <typename T> void f() {\n" 26575 " if constexpr (requires(T t) {\n" 26576 " { t.bar() } -> std::same_as<bool>;\n" 26577 " }) {\n" 26578 " }\n" 26579 "}", 26580 Style); 26581 26582 verifyFormat( 26583 "template <typename T> void f() {\n" 26584 " if constexpr (condition && requires(T t) {\n" 26585 " { t.bar() } -> std::same_as<bool>;\n" 26586 " }) {\n" 26587 " }\n" 26588 "}", 26589 Style); 26590 26591 verifyFormat("template <typename T> struct C {\n" 26592 " void f()\n" 26593 " requires requires(T t) {\n" 26594 " { t.bar() } -> std::same_as<bool>;\n" 26595 " };\n" 26596 "};", 26597 Style); 26598 } 26599 26600 TEST_F(FormatTest, StatementAttributeLikeMacros) { 26601 FormatStyle Style = getLLVMStyle(); 26602 StringRef Source = "void Foo::slot() {\n" 26603 " unsigned char MyChar = 'x';\n" 26604 " emit signal(MyChar);\n" 26605 " Q_EMIT signal(MyChar);\n" 26606 "}"; 26607 26608 verifyFormat(Source, Style); 26609 26610 Style.AlignConsecutiveDeclarations.Enabled = true; 26611 verifyFormat("void Foo::slot() {\n" 26612 " unsigned char MyChar = 'x';\n" 26613 " emit signal(MyChar);\n" 26614 " Q_EMIT signal(MyChar);\n" 26615 "}", 26616 Source, Style); 26617 26618 Style.StatementAttributeLikeMacros.push_back("emit"); 26619 verifyFormat(Source, Style); 26620 26621 Style.StatementAttributeLikeMacros = {}; 26622 verifyFormat("void Foo::slot() {\n" 26623 " unsigned char MyChar = 'x';\n" 26624 " emit signal(MyChar);\n" 26625 " Q_EMIT signal(MyChar);\n" 26626 "}", 26627 Source, Style); 26628 } 26629 26630 TEST_F(FormatTest, IndentAccessModifiers) { 26631 FormatStyle Style = getLLVMStyle(); 26632 Style.IndentAccessModifiers = true; 26633 // Members are *two* levels below the record; 26634 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 26635 verifyFormat("class C {\n" 26636 " int i;\n" 26637 "};", 26638 Style); 26639 verifyFormat("union C {\n" 26640 " int i;\n" 26641 " unsigned u;\n" 26642 "};", 26643 Style); 26644 // Access modifiers should be indented one level below the record. 26645 verifyFormat("class C {\n" 26646 " public:\n" 26647 " int i;\n" 26648 "};", 26649 Style); 26650 verifyFormat("class C {\n" 26651 " public /* comment */:\n" 26652 " int i;\n" 26653 "};", 26654 Style); 26655 verifyFormat("struct S {\n" 26656 " private:\n" 26657 " class C {\n" 26658 " int j;\n" 26659 "\n" 26660 " public:\n" 26661 " C();\n" 26662 " };\n" 26663 "\n" 26664 " public:\n" 26665 " int i;\n" 26666 "};", 26667 Style); 26668 // Enumerations are not records and should be unaffected. 26669 Style.AllowShortEnumsOnASingleLine = false; 26670 verifyFormat("enum class E {\n" 26671 " A,\n" 26672 " B\n" 26673 "};", 26674 Style); 26675 // Test with a different indentation width; 26676 // also proves that the result is Style.AccessModifierOffset agnostic. 26677 Style.IndentWidth = 3; 26678 verifyFormat("class C {\n" 26679 " public:\n" 26680 " int i;\n" 26681 "};", 26682 Style); 26683 verifyFormat("class C {\n" 26684 " public /**/:\n" 26685 " int i;\n" 26686 "};", 26687 Style); 26688 Style.AttributeMacros.push_back("FOO"); 26689 verifyFormat("class C {\n" 26690 " FOO public:\n" 26691 " int i;\n" 26692 "};", 26693 Style); 26694 } 26695 26696 TEST_F(FormatTest, LimitlessStringsAndComments) { 26697 auto Style = getLLVMStyleWithColumns(0); 26698 constexpr StringRef Code = 26699 "/**\n" 26700 " * This is a multiline comment with quite some long lines, at least for " 26701 "the LLVM Style.\n" 26702 " * We will redo this with strings and line comments. Just to check if " 26703 "everything is working.\n" 26704 " */\n" 26705 "bool foo() {\n" 26706 " /* Single line multi line comment. */\n" 26707 " const std::string String = \"This is a multiline string with quite " 26708 "some long lines, at least for the LLVM Style.\"\n" 26709 " \"We already did it with multi line " 26710 "comments, and we will do it with line comments. Just to check if " 26711 "everything is working.\";\n" 26712 " // This is a line comment (block) with quite some long lines, at " 26713 "least for the LLVM Style.\n" 26714 " // We already did this with multi line comments and strings. Just to " 26715 "check if everything is working.\n" 26716 " const std::string SmallString = \"Hello World\";\n" 26717 " // Small line comment\n" 26718 " return String.size() > SmallString.size();\n" 26719 "}"; 26720 verifyNoChange(Code, Style); 26721 } 26722 26723 TEST_F(FormatTest, FormatDecayCopy) { 26724 // error cases from unit tests 26725 verifyFormat("foo(auto())"); 26726 verifyFormat("foo(auto{})"); 26727 verifyFormat("foo(auto({}))"); 26728 verifyFormat("foo(auto{{}})"); 26729 26730 verifyFormat("foo(auto(1))"); 26731 verifyFormat("foo(auto{1})"); 26732 verifyFormat("foo(new auto(1))"); 26733 verifyFormat("foo(new auto{1})"); 26734 verifyFormat("decltype(auto(1)) x;"); 26735 verifyFormat("decltype(auto{1}) x;"); 26736 verifyFormat("auto(x);"); 26737 verifyFormat("auto{x};"); 26738 verifyFormat("new auto{x};"); 26739 verifyFormat("auto{x} = y;"); 26740 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly 26741 // the user's own fault 26742 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is 26743 // clearly the user's own fault 26744 verifyFormat("auto (*p)() = f;"); 26745 } 26746 26747 TEST_F(FormatTest, Cpp20ModulesSupport) { 26748 FormatStyle Style = getLLVMStyle(); 26749 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 26750 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 26751 26752 verifyFormat("export import foo;", Style); 26753 verifyFormat("export import foo:bar;", Style); 26754 verifyFormat("export import foo.bar;", Style); 26755 verifyFormat("export import foo.bar:baz;", Style); 26756 verifyFormat("export import :bar;", Style); 26757 verifyFormat("export module foo:bar;", Style); 26758 verifyFormat("export module foo;", Style); 26759 verifyFormat("export module foo.bar;", Style); 26760 verifyFormat("export module foo.bar:baz;", Style); 26761 verifyFormat("export import <string_view>;", Style); 26762 verifyFormat("export import <Foo/Bar>;", Style); 26763 26764 verifyFormat("export type_name var;", Style); 26765 verifyFormat("template <class T> export using A = B<T>;", Style); 26766 verifyFormat("export using A = B;", Style); 26767 verifyFormat("export int func() {\n" 26768 " foo();\n" 26769 "}", 26770 Style); 26771 verifyFormat("export struct {\n" 26772 " int foo;\n" 26773 "};", 26774 Style); 26775 verifyFormat("export {\n" 26776 " int foo;\n" 26777 "};", 26778 Style); 26779 verifyFormat("export export char const *hello() { return \"hello\"; }"); 26780 26781 verifyFormat("import bar;", Style); 26782 verifyFormat("import foo.bar;", Style); 26783 verifyFormat("import foo:bar;", Style); 26784 verifyFormat("import :bar;", Style); 26785 verifyFormat("import /* module partition */ :bar;", Style); 26786 verifyFormat("import <ctime>;", Style); 26787 verifyFormat("import \"header\";", Style); 26788 26789 verifyFormat("module foo;", Style); 26790 verifyFormat("module foo:bar;", Style); 26791 verifyFormat("module foo.bar;", Style); 26792 verifyFormat("module;", Style); 26793 26794 verifyFormat("export namespace hi {\n" 26795 "const char *sayhi();\n" 26796 "}", 26797 Style); 26798 26799 verifyFormat("module :private;", Style); 26800 verifyFormat("import <foo/bar.h>;", Style); 26801 verifyFormat("import foo...bar;", Style); 26802 verifyFormat("import ..........;", Style); 26803 verifyFormat("module foo:private;", Style); 26804 verifyFormat("import a", Style); 26805 verifyFormat("module a", Style); 26806 verifyFormat("export import a", Style); 26807 verifyFormat("export module a", Style); 26808 26809 verifyFormat("import", Style); 26810 verifyFormat("module", Style); 26811 verifyFormat("export", Style); 26812 26813 verifyFormat("import /* not keyword */ = val ? 2 : 1;"); 26814 } 26815 26816 TEST_F(FormatTest, CoroutineForCoawait) { 26817 FormatStyle Style = getLLVMStyle(); 26818 verifyFormat("for co_await (auto x : range())\n ;"); 26819 verifyFormat("for (auto i : arr) {\n" 26820 "}", 26821 Style); 26822 verifyFormat("for co_await (auto i : arr) {\n" 26823 "}", 26824 Style); 26825 verifyFormat("for co_await (auto i : foo(T{})) {\n" 26826 "}", 26827 Style); 26828 } 26829 26830 TEST_F(FormatTest, CoroutineCoAwait) { 26831 verifyFormat("int x = co_await foo();"); 26832 verifyFormat("int x = (co_await foo());"); 26833 verifyFormat("co_await (42);"); 26834 verifyFormat("void operator co_await(int);"); 26835 verifyFormat("void operator co_await(a);"); 26836 verifyFormat("co_await a;"); 26837 verifyFormat("co_await missing_await_resume{};"); 26838 verifyFormat("co_await a; // comment"); 26839 verifyFormat("void test0() { co_await a; }"); 26840 verifyFormat("co_await co_await co_await foo();"); 26841 verifyFormat("co_await foo().bar();"); 26842 verifyFormat("co_await [this]() -> Task { co_return x; }"); 26843 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await " 26844 "foo(); }(x, y);"); 26845 26846 FormatStyle Style = getLLVMStyleWithColumns(40); 26847 verifyFormat("co_await [this](int a, int b) -> Task {\n" 26848 " co_return co_await foo();\n" 26849 "}(x, y);", 26850 Style); 26851 verifyFormat("co_await;"); 26852 } 26853 26854 TEST_F(FormatTest, CoroutineCoYield) { 26855 verifyFormat("int x = co_yield foo();"); 26856 verifyFormat("int x = (co_yield foo());"); 26857 verifyFormat("co_yield (42);"); 26858 verifyFormat("co_yield {42};"); 26859 verifyFormat("co_yield 42;"); 26860 verifyFormat("co_yield n++;"); 26861 verifyFormat("co_yield ++n;"); 26862 verifyFormat("co_yield;"); 26863 } 26864 26865 TEST_F(FormatTest, CoroutineCoReturn) { 26866 verifyFormat("co_return (42);"); 26867 verifyFormat("co_return;"); 26868 verifyFormat("co_return {};"); 26869 verifyFormat("co_return x;"); 26870 verifyFormat("co_return co_await foo();"); 26871 verifyFormat("co_return co_yield foo();"); 26872 } 26873 26874 TEST_F(FormatTest, EmptyShortBlock) { 26875 auto Style = getLLVMStyle(); 26876 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 26877 26878 verifyFormat("try {\n" 26879 " doA();\n" 26880 "} catch (Exception &e) {\n" 26881 " e.printStackTrace();\n" 26882 "}", 26883 Style); 26884 26885 verifyFormat("try {\n" 26886 " doA();\n" 26887 "} catch (Exception &e) {}", 26888 Style); 26889 } 26890 26891 TEST_F(FormatTest, ShortTemplatedArgumentLists) { 26892 auto Style = getLLVMStyle(); 26893 26894 verifyFormat("template <> struct S : Template<int (*)[]> {};", Style); 26895 verifyFormat("template <> struct S : Template<int (*)[10]> {};", Style); 26896 verifyFormat("struct Y : X<[] { return 0; }> {};", Style); 26897 verifyFormat("struct Y<[] { return 0; }> {};", Style); 26898 26899 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style); 26900 verifyFormat("template <int N> struct Foo<char[N]> {};", Style); 26901 } 26902 26903 TEST_F(FormatTest, MultilineLambdaInConditional) { 26904 auto Style = getLLVMStyleWithColumns(70); 26905 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n" 26906 " ;\n" 26907 " return 5;\n" 26908 "}()\n" 26909 " : 2;", 26910 Style); 26911 verifyFormat( 26912 "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n" 26913 " ;\n" 26914 " return 5;\n" 26915 "}();", 26916 Style); 26917 26918 Style = getLLVMStyleWithColumns(60); 26919 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n" 26920 " ? []() {\n" 26921 " ;\n" 26922 " return 5;\n" 26923 " }()\n" 26924 " : 2;", 26925 Style); 26926 verifyFormat("auto aLengthyIdentifier =\n" 26927 " oneExpressionSoThatWeBreak ? 2 : []() {\n" 26928 " ;\n" 26929 " return 5;\n" 26930 " }();", 26931 Style); 26932 26933 Style = getLLVMStyleWithColumns(40); 26934 verifyFormat("auto aLengthyIdentifier =\n" 26935 " oneExpressionSoThatWeBreak ? []() {\n" 26936 " ;\n" 26937 " return 5;\n" 26938 " }()\n" 26939 " : 2;", 26940 Style); 26941 verifyFormat("auto aLengthyIdentifier =\n" 26942 " oneExpressionSoThatWeBreak\n" 26943 " ? 2\n" 26944 " : []() {\n" 26945 " ;\n" 26946 " return 5;\n" 26947 " };", 26948 Style); 26949 } 26950 26951 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) { 26952 auto Style = getLLVMStyle(); 26953 26954 StringRef Short = "functionCall(paramA, paramB, paramC);\n" 26955 "void functionDecl(int a, int b, int c);"; 26956 26957 StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, " 26958 "paramF, paramG, paramH, paramI);\n" 26959 "void functionDecl(int argumentA, int argumentB, int " 26960 "argumentC, int argumentD, int argumentE);"; 26961 26962 verifyFormat(Short, Style); 26963 26964 StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, " 26965 "paramF, paramG, paramH,\n" 26966 " paramI);\n" 26967 "void functionDecl(int argumentA, int argumentB, int " 26968 "argumentC, int argumentD,\n" 26969 " int argumentE);"; 26970 26971 verifyFormat(NoBreak, Medium, Style); 26972 verifyFormat(NoBreak, 26973 "functionCall(\n" 26974 " paramA,\n" 26975 " paramB,\n" 26976 " paramC,\n" 26977 " paramD,\n" 26978 " paramE,\n" 26979 " paramF,\n" 26980 " paramG,\n" 26981 " paramH,\n" 26982 " paramI\n" 26983 ");\n" 26984 "void functionDecl(\n" 26985 " int argumentA,\n" 26986 " int argumentB,\n" 26987 " int argumentC,\n" 26988 " int argumentD,\n" 26989 " int argumentE\n" 26990 ");", 26991 Style); 26992 26993 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n" 26994 " nestedLongFunctionCall(argument1, " 26995 "argument2, argument3,\n" 26996 " argument4, " 26997 "argument5));", 26998 Style); 26999 27000 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 27001 27002 verifyFormat(Short, Style); 27003 verifyFormat( 27004 "functionCall(\n" 27005 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 27006 "paramI\n" 27007 ");\n" 27008 "void functionDecl(\n" 27009 " int argumentA, int argumentB, int argumentC, int argumentD, int " 27010 "argumentE\n" 27011 ");", 27012 Medium, Style); 27013 27014 Style.AllowAllArgumentsOnNextLine = false; 27015 Style.AllowAllParametersOfDeclarationOnNextLine = false; 27016 27017 verifyFormat(Short, Style); 27018 verifyFormat( 27019 "functionCall(\n" 27020 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 27021 "paramI\n" 27022 ");\n" 27023 "void functionDecl(\n" 27024 " int argumentA, int argumentB, int argumentC, int argumentD, int " 27025 "argumentE\n" 27026 ");", 27027 Medium, Style); 27028 27029 Style.BinPackArguments = false; 27030 Style.BinPackParameters = FormatStyle::BPPS_OnePerLine; 27031 27032 verifyFormat(Short, Style); 27033 27034 verifyFormat("functionCall(\n" 27035 " paramA,\n" 27036 " paramB,\n" 27037 " paramC,\n" 27038 " paramD,\n" 27039 " paramE,\n" 27040 " paramF,\n" 27041 " paramG,\n" 27042 " paramH,\n" 27043 " paramI\n" 27044 ");\n" 27045 "void functionDecl(\n" 27046 " int argumentA,\n" 27047 " int argumentB,\n" 27048 " int argumentC,\n" 27049 " int argumentD,\n" 27050 " int argumentE\n" 27051 ");", 27052 Medium, Style); 27053 27054 verifyFormat("outerFunctionCall(\n" 27055 " nestedFunctionCall(argument1),\n" 27056 " nestedLongFunctionCall(\n" 27057 " argument1,\n" 27058 " argument2,\n" 27059 " argument3,\n" 27060 " argument4,\n" 27061 " argument5\n" 27062 " )\n" 27063 ");", 27064 Style); 27065 27066 verifyFormat("int a = (int)b;", Style); 27067 verifyFormat("int a = (int)b;", 27068 "int a = (\n" 27069 " int\n" 27070 ") b;", 27071 Style); 27072 27073 verifyFormat("return (true);", Style); 27074 verifyFormat("return (true);", 27075 "return (\n" 27076 " true\n" 27077 ");", 27078 Style); 27079 27080 verifyFormat("void foo();", Style); 27081 verifyFormat("void foo();", 27082 "void foo(\n" 27083 ");", 27084 Style); 27085 27086 verifyFormat("void foo() {}", Style); 27087 verifyFormat("void foo() {}", 27088 "void foo(\n" 27089 ") {\n" 27090 "}", 27091 Style); 27092 27093 verifyFormat("auto string = std::string();", Style); 27094 verifyFormat("auto string = std::string();", 27095 "auto string = std::string(\n" 27096 ");", 27097 Style); 27098 27099 verifyFormat("void (*functionPointer)() = nullptr;", Style); 27100 verifyFormat("void (*functionPointer)() = nullptr;", 27101 "void (\n" 27102 " *functionPointer\n" 27103 ")\n" 27104 "(\n" 27105 ") = nullptr;", 27106 Style); 27107 } 27108 27109 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) { 27110 auto Style = getLLVMStyle(); 27111 27112 verifyFormat("if (foo()) {\n" 27113 " return;\n" 27114 "}", 27115 Style); 27116 27117 verifyFormat("if (quiteLongArg !=\n" 27118 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg " 27119 "comment\n" 27120 " return;\n" 27121 "}", 27122 Style); 27123 27124 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 27125 27126 verifyFormat("if (foo()) {\n" 27127 " return;\n" 27128 "}", 27129 Style); 27130 27131 verifyFormat("if (quiteLongArg !=\n" 27132 " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg " 27133 "comment\n" 27134 " return;\n" 27135 "}", 27136 Style); 27137 27138 verifyFormat("void foo() {\n" 27139 " if (camelCaseName < alsoLongName ||\n" 27140 " anotherEvenLongerName <=\n" 27141 " thisReallyReallyReallyReallyReallyReallyLongerName ||" 27142 "\n" 27143 " otherName < thisLastName) {\n" 27144 " return;\n" 27145 " } else if (quiteLongName < alsoLongName ||\n" 27146 " anotherEvenLongerName <=\n" 27147 " thisReallyReallyReallyReallyReallyReallyLonger" 27148 "Name ||\n" 27149 " otherName < thisLastName) {\n" 27150 " return;\n" 27151 " }\n" 27152 "}", 27153 Style); 27154 27155 Style.ContinuationIndentWidth = 2; 27156 verifyFormat("void foo() {\n" 27157 " if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n" 27158 " ontoMultipleLines && whenFormattedCorrectly) {\n" 27159 " if (false) {\n" 27160 " return;\n" 27161 " } else if (thisIsRatherALongIfClause && " 27162 "thatIExpectToBeBroken ||\n" 27163 " ontoMultipleLines && whenFormattedCorrectly) {\n" 27164 " return;\n" 27165 " }\n" 27166 " }\n" 27167 "}", 27168 Style); 27169 } 27170 27171 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) { 27172 auto Style = getLLVMStyle(); 27173 27174 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 27175 " doSomething();\n" 27176 "}", 27177 Style); 27178 27179 verifyFormat("for (int myReallyLongCountVariable = 0; " 27180 "myReallyLongCountVariable < count;\n" 27181 " myReallyLongCountVariable++) {\n" 27182 " doSomething();\n" 27183 "}", 27184 Style); 27185 27186 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 27187 27188 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 27189 " doSomething();\n" 27190 "}", 27191 Style); 27192 27193 verifyFormat("for (int myReallyLongCountVariable = 0; " 27194 "myReallyLongCountVariable < count;\n" 27195 " myReallyLongCountVariable++) {\n" 27196 " doSomething();\n" 27197 "}", 27198 Style); 27199 } 27200 27201 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) { 27202 auto Style = getLLVMStyleWithColumns(60); 27203 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 27204 // Aggregate initialization. 27205 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n" 27206 " 10000000, 20000000\n" 27207 "};", 27208 Style); 27209 verifyFormat("SomeStruct s{\n" 27210 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n" 27211 " \"zzzzzzzzzzzzzzzz\"\n" 27212 "};", 27213 Style); 27214 // Designated initializers. 27215 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n" 27216 " [0] = 10000000, [1] = 20000000\n" 27217 "};", 27218 Style); 27219 verifyFormat("SomeStruct s{\n" 27220 " .foo = \"xxxxxxxxxxxxx\",\n" 27221 " .bar = \"yyyyyyyyyyyyy\",\n" 27222 " .baz = \"zzzzzzzzzzzzz\"\n" 27223 "};", 27224 Style); 27225 // List initialization. 27226 verifyFormat("SomeStruct s{\n" 27227 " \"xxxxxxxxxxxxx\",\n" 27228 " \"yyyyyyyyyyyyy\",\n" 27229 " \"zzzzzzzzzzzzz\",\n" 27230 "};", 27231 Style); 27232 verifyFormat("SomeStruct{\n" 27233 " \"xxxxxxxxxxxxx\",\n" 27234 " \"yyyyyyyyyyyyy\",\n" 27235 " \"zzzzzzzzzzzzz\",\n" 27236 "};", 27237 Style); 27238 verifyFormat("new SomeStruct{\n" 27239 " \"xxxxxxxxxxxxx\",\n" 27240 " \"yyyyyyyyyyyyy\",\n" 27241 " \"zzzzzzzzzzzzz\",\n" 27242 "};", 27243 Style); 27244 // Member initializer. 27245 verifyFormat("class SomeClass {\n" 27246 " SomeStruct s{\n" 27247 " \"xxxxxxxxxxxxx\",\n" 27248 " \"yyyyyyyyyyyyy\",\n" 27249 " \"zzzzzzzzzzzzz\",\n" 27250 " };\n" 27251 "};", 27252 Style); 27253 // Constructor member initializer. 27254 verifyFormat("SomeClass::SomeClass : strct{\n" 27255 " \"xxxxxxxxxxxxx\",\n" 27256 " \"yyyyyyyyyyyyy\",\n" 27257 " \"zzzzzzzzzzzzz\",\n" 27258 " } {}", 27259 Style); 27260 // Copy initialization. 27261 verifyFormat("SomeStruct s = SomeStruct{\n" 27262 " \"xxxxxxxxxxxxx\",\n" 27263 " \"yyyyyyyyyyyyy\",\n" 27264 " \"zzzzzzzzzzzzz\",\n" 27265 "};", 27266 Style); 27267 // Copy list initialization. 27268 verifyFormat("SomeStruct s = {\n" 27269 " \"xxxxxxxxxxxxx\",\n" 27270 " \"yyyyyyyyyyyyy\",\n" 27271 " \"zzzzzzzzzzzzz\",\n" 27272 "};", 27273 Style); 27274 // Assignment operand initialization. 27275 verifyFormat("s = {\n" 27276 " \"xxxxxxxxxxxxx\",\n" 27277 " \"yyyyyyyyyyyyy\",\n" 27278 " \"zzzzzzzzzzzzz\",\n" 27279 "};", 27280 Style); 27281 // Returned object initialization. 27282 verifyFormat("return {\n" 27283 " \"xxxxxxxxxxxxx\",\n" 27284 " \"yyyyyyyyyyyyy\",\n" 27285 " \"zzzzzzzzzzzzz\",\n" 27286 "};", 27287 Style); 27288 // Initializer list. 27289 verifyFormat("auto initializerList = {\n" 27290 " \"xxxxxxxxxxxxx\",\n" 27291 " \"yyyyyyyyyyyyy\",\n" 27292 " \"zzzzzzzzzzzzz\",\n" 27293 "};", 27294 Style); 27295 // Function parameter initialization. 27296 verifyFormat("func({\n" 27297 " \"xxxxxxxxxxxxx\",\n" 27298 " \"yyyyyyyyyyyyy\",\n" 27299 " \"zzzzzzzzzzzzz\",\n" 27300 "});", 27301 Style); 27302 // Nested init lists. 27303 verifyFormat("SomeStruct s = {\n" 27304 " {{init1, init2, init3, init4, init5},\n" 27305 " {init1, init2, init3, init4, init5}}\n" 27306 "};", 27307 Style); 27308 verifyFormat("SomeStruct s = {\n" 27309 " {{\n" 27310 " .init1 = 1,\n" 27311 " .init2 = 2,\n" 27312 " .init3 = 3,\n" 27313 " .init4 = 4,\n" 27314 " .init5 = 5,\n" 27315 " },\n" 27316 " {init1, init2, init3, init4, init5}}\n" 27317 "};", 27318 Style); 27319 verifyFormat("SomeArrayT a[3] = {\n" 27320 " {\n" 27321 " foo,\n" 27322 " bar,\n" 27323 " },\n" 27324 " {\n" 27325 " foo,\n" 27326 " bar,\n" 27327 " },\n" 27328 " SomeArrayT{},\n" 27329 "};", 27330 Style); 27331 verifyFormat("SomeArrayT a[3] = {\n" 27332 " {foo},\n" 27333 " {\n" 27334 " {\n" 27335 " init1,\n" 27336 " init2,\n" 27337 " init3,\n" 27338 " },\n" 27339 " {\n" 27340 " init1,\n" 27341 " init2,\n" 27342 " init3,\n" 27343 " },\n" 27344 " },\n" 27345 " {baz},\n" 27346 "};", 27347 Style); 27348 } 27349 27350 TEST_F(FormatTest, UnderstandsDigraphs) { 27351 verifyFormat("int arr<:5:> = {};"); 27352 verifyFormat("int arr[5] = <%%>;"); 27353 verifyFormat("int arr<:::qualified_variable:> = {};"); 27354 verifyFormat("int arr[::qualified_variable] = <%%>;"); 27355 verifyFormat("%:include <header>"); 27356 verifyFormat("%:define A x##y"); 27357 verifyFormat("#define A x%:%:y"); 27358 } 27359 27360 TEST_F(FormatTest, AlignArrayOfStructuresLeftAlignmentNonSquare) { 27361 auto Style = getLLVMStyle(); 27362 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 27363 Style.AlignConsecutiveAssignments.Enabled = true; 27364 Style.AlignConsecutiveDeclarations.Enabled = true; 27365 27366 // The AlignArray code is incorrect for non square Arrays and can cause 27367 // crashes, these tests assert that the array is not changed but will 27368 // also act as regression tests for when it is properly fixed 27369 verifyFormat("struct test demo[] = {\n" 27370 " {1, 2},\n" 27371 " {3, 4, 5},\n" 27372 " {6, 7, 8}\n" 27373 "};", 27374 Style); 27375 verifyFormat("struct test demo[] = {\n" 27376 " {1, 2, 3, 4, 5},\n" 27377 " {3, 4, 5},\n" 27378 " {6, 7, 8}\n" 27379 "};", 27380 Style); 27381 verifyFormat("struct test demo[] = {\n" 27382 " {1, 2, 3, 4, 5},\n" 27383 " {3, 4, 5},\n" 27384 " {6, 7, 8, 9, 10, 11, 12}\n" 27385 "};", 27386 Style); 27387 verifyFormat("struct test demo[] = {\n" 27388 " {1, 2, 3},\n" 27389 " {3, 4, 5},\n" 27390 " {6, 7, 8, 9, 10, 11, 12}\n" 27391 "};", 27392 Style); 27393 27394 verifyFormat("S{\n" 27395 " {},\n" 27396 " {},\n" 27397 " {a, b}\n" 27398 "};", 27399 Style); 27400 verifyFormat("S{\n" 27401 " {},\n" 27402 " {},\n" 27403 " {a, b},\n" 27404 "};", 27405 Style); 27406 verifyFormat("void foo() {\n" 27407 " auto thing = test{\n" 27408 " {\n" 27409 " {13}, {something}, // A\n" 27410 " }\n" 27411 " };\n" 27412 "}", 27413 "void foo() {\n" 27414 " auto thing = test{\n" 27415 " {\n" 27416 " {13},\n" 27417 " {something}, // A\n" 27418 " }\n" 27419 " };\n" 27420 "}", 27421 Style); 27422 } 27423 27424 TEST_F(FormatTest, AlignArrayOfStructuresRightAlignmentNonSquare) { 27425 auto Style = getLLVMStyle(); 27426 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 27427 Style.AlignConsecutiveAssignments.Enabled = true; 27428 Style.AlignConsecutiveDeclarations.Enabled = true; 27429 27430 // The AlignArray code is incorrect for non square Arrays and can cause 27431 // crashes, these tests assert that the array is not changed but will 27432 // also act as regression tests for when it is properly fixed 27433 verifyFormat("struct test demo[] = {\n" 27434 " {1, 2},\n" 27435 " {3, 4, 5},\n" 27436 " {6, 7, 8}\n" 27437 "};", 27438 Style); 27439 verifyFormat("struct test demo[] = {\n" 27440 " {1, 2, 3, 4, 5},\n" 27441 " {3, 4, 5},\n" 27442 " {6, 7, 8}\n" 27443 "};", 27444 Style); 27445 verifyFormat("struct test demo[] = {\n" 27446 " {1, 2, 3, 4, 5},\n" 27447 " {3, 4, 5},\n" 27448 " {6, 7, 8, 9, 10, 11, 12}\n" 27449 "};", 27450 Style); 27451 verifyFormat("struct test demo[] = {\n" 27452 " {1, 2, 3},\n" 27453 " {3, 4, 5},\n" 27454 " {6, 7, 8, 9, 10, 11, 12}\n" 27455 "};", 27456 Style); 27457 27458 verifyFormat("S{\n" 27459 " {},\n" 27460 " {},\n" 27461 " {a, b}\n" 27462 "};", 27463 Style); 27464 verifyFormat("S{\n" 27465 " {},\n" 27466 " {},\n" 27467 " {a, b},\n" 27468 "};", 27469 Style); 27470 verifyFormat("void foo() {\n" 27471 " auto thing = test{\n" 27472 " {\n" 27473 " {13}, {something}, // A\n" 27474 " }\n" 27475 " };\n" 27476 "}", 27477 "void foo() {\n" 27478 " auto thing = test{\n" 27479 " {\n" 27480 " {13},\n" 27481 " {something}, // A\n" 27482 " }\n" 27483 " };\n" 27484 "}", 27485 Style); 27486 } 27487 27488 TEST_F(FormatTest, FormatsVariableTemplates) { 27489 verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;"); 27490 verifyFormat("template <typename T> " 27491 "inline bool var = is_integral_v<T> && is_signed_v<T>;"); 27492 } 27493 27494 TEST_F(FormatTest, RemoveSemicolon) { 27495 FormatStyle Style = getLLVMStyle(); 27496 Style.RemoveSemicolon = true; 27497 27498 verifyFormat("int max(int a, int b) { return a > b ? a : b; }", 27499 "int max(int a, int b) { return a > b ? a : b; };", Style); 27500 27501 verifyFormat("int max(int a, int b) { return a > b ? a : b; }", 27502 "int max(int a, int b) { return a > b ? a : b; };;", Style); 27503 27504 verifyFormat("class Foo {\n" 27505 " int getSomething() const { return something; }\n" 27506 "};", 27507 "class Foo {\n" 27508 " int getSomething() const { return something; };\n" 27509 "};", 27510 Style); 27511 27512 verifyFormat("class Foo {\n" 27513 " int getSomething() const { return something; }\n" 27514 "};", 27515 "class Foo {\n" 27516 " int getSomething() const { return something; };;\n" 27517 "};", 27518 Style); 27519 27520 verifyFormat("for (;;) {\n" 27521 "}", 27522 Style); 27523 27524 verifyFormat("class [[deprecated(\"\")]] C {\n" 27525 " int i;\n" 27526 "};", 27527 Style); 27528 27529 verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n" 27530 " int i;\n" 27531 "};", 27532 Style); 27533 27534 verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style); 27535 27536 verifyFormat("void main() {}", "void main() {};", Style); 27537 27538 verifyFormat("struct Foo {\n" 27539 " Foo() {}\n" 27540 " ~Foo() {}\n" 27541 "};", 27542 "struct Foo {\n" 27543 " Foo() {};\n" 27544 " ~Foo() {};\n" 27545 "};", 27546 Style); 27547 27548 // We can't (and probably shouldn't) support the following. 27549 #if 0 27550 verifyFormat("void foo() {} //\n" 27551 "int bar;", 27552 "void foo() {}; //\n" 27553 "; int bar;", 27554 Style); 27555 #endif 27556 27557 verifyFormat("auto sgf = [] {\n" 27558 " ogl = {\n" 27559 " a, b, c, d, e,\n" 27560 " };\n" 27561 "};", 27562 Style); 27563 27564 Style.TypenameMacros.push_back("STRUCT"); 27565 verifyFormat("STRUCT(T, B) { int i; };", Style); 27566 } 27567 27568 TEST_F(FormatTest, BreakAfterAttributes) { 27569 constexpr StringRef Code("[[maybe_unused]] const int i;\n" 27570 "[[foo([[]])]] [[maybe_unused]]\n" 27571 "int j;\n" 27572 "[[maybe_unused]]\n" 27573 "foo<int> k;\n" 27574 "[[nodiscard]] inline int f(int &i);\n" 27575 "[[foo([[]])]] [[nodiscard]]\n" 27576 "int g(int &i);\n" 27577 "[[nodiscard]]\n" 27578 "inline int f(int &i) {\n" 27579 " i = 1;\n" 27580 " return 0;\n" 27581 "}\n" 27582 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n" 27583 " i = 0;\n" 27584 " return 1;\n" 27585 "}"); 27586 27587 FormatStyle Style = getLLVMStyle(); 27588 EXPECT_EQ(Style.BreakAfterAttributes, FormatStyle::ABS_Leave); 27589 verifyNoChange(Code, Style); 27590 27591 Style.BreakAfterAttributes = FormatStyle::ABS_Never; 27592 verifyFormat("[[maybe_unused]] const int i;\n" 27593 "[[foo([[]])]] [[maybe_unused]] int j;\n" 27594 "[[maybe_unused]] foo<int> k;\n" 27595 "[[nodiscard]] inline int f(int &i);\n" 27596 "[[foo([[]])]] [[nodiscard]] int g(int &i);\n" 27597 "[[nodiscard]] inline int f(int &i) {\n" 27598 " i = 1;\n" 27599 " return 0;\n" 27600 "}\n" 27601 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n" 27602 " i = 0;\n" 27603 " return 1;\n" 27604 "}", 27605 Code, Style); 27606 27607 Style.BreakAfterAttributes = FormatStyle::ABS_Always; 27608 verifyFormat("[[maybe_unused]]\n" 27609 "const int i;\n" 27610 "[[foo([[]])]] [[maybe_unused]]\n" 27611 "int j;\n" 27612 "[[maybe_unused]]\n" 27613 "foo<int> k;\n" 27614 "[[nodiscard]]\n" 27615 "inline int f(int &i);\n" 27616 "[[foo([[]])]] [[nodiscard]]\n" 27617 "int g(int &i);\n" 27618 "[[nodiscard]]\n" 27619 "inline int f(int &i) {\n" 27620 " i = 1;\n" 27621 " return 0;\n" 27622 "}\n" 27623 "[[foo([[]])]] [[nodiscard]]\n" 27624 "int g(int &i) {\n" 27625 " i = 0;\n" 27626 " return 1;\n" 27627 "}", 27628 Code, Style); 27629 27630 constexpr StringRef CtrlStmtCode("[[likely]] if (a)\n" 27631 " f();\n" 27632 "else\n" 27633 " g();\n" 27634 "[[foo([[]])]]\n" 27635 "switch (b) {\n" 27636 "[[unlikely]] case 1:\n" 27637 " ++b;\n" 27638 " break;\n" 27639 "[[likely]]\n" 27640 "default:\n" 27641 " return;\n" 27642 "}\n" 27643 "[[unlikely]] for (; c > 0; --c)\n" 27644 " h();\n" 27645 "[[likely]]\n" 27646 "while (d > 0)\n" 27647 " --d;"); 27648 27649 Style.BreakAfterAttributes = FormatStyle::ABS_Leave; 27650 verifyNoChange(CtrlStmtCode, Style); 27651 27652 Style.BreakAfterAttributes = FormatStyle::ABS_Never; 27653 verifyFormat("[[likely]] if (a)\n" 27654 " f();\n" 27655 "else\n" 27656 " g();\n" 27657 "[[foo([[]])]] switch (b) {\n" 27658 "[[unlikely]] case 1:\n" 27659 " ++b;\n" 27660 " break;\n" 27661 "[[likely]] default:\n" 27662 " return;\n" 27663 "}\n" 27664 "[[unlikely]] for (; c > 0; --c)\n" 27665 " h();\n" 27666 "[[likely]] while (d > 0)\n" 27667 " --d;", 27668 CtrlStmtCode, Style); 27669 27670 Style.BreakAfterAttributes = FormatStyle::ABS_Always; 27671 verifyFormat("[[likely]]\n" 27672 "if (a)\n" 27673 " f();\n" 27674 "else\n" 27675 " g();\n" 27676 "[[foo([[]])]]\n" 27677 "switch (b) {\n" 27678 "[[unlikely]]\n" 27679 "case 1:\n" 27680 " ++b;\n" 27681 " break;\n" 27682 "[[likely]]\n" 27683 "default:\n" 27684 " return;\n" 27685 "}\n" 27686 "[[unlikely]]\n" 27687 "for (; c > 0; --c)\n" 27688 " h();\n" 27689 "[[likely]]\n" 27690 "while (d > 0)\n" 27691 " --d;", 27692 CtrlStmtCode, Style); 27693 27694 constexpr StringRef CtorDtorCode("struct Foo {\n" 27695 " [[deprecated]] Foo();\n" 27696 " [[deprecated]] Foo() {}\n" 27697 " [[deprecated]] ~Foo();\n" 27698 " [[deprecated]] ~Foo() {}\n" 27699 " [[deprecated]] void f();\n" 27700 " [[deprecated]] void f() {}\n" 27701 "};\n" 27702 "[[deprecated]] Bar::Bar() {}\n" 27703 "[[deprecated]] Bar::~Bar() {}\n" 27704 "[[deprecated]] void g() {}"); 27705 verifyFormat("struct Foo {\n" 27706 " [[deprecated]]\n" 27707 " Foo();\n" 27708 " [[deprecated]]\n" 27709 " Foo() {}\n" 27710 " [[deprecated]]\n" 27711 " ~Foo();\n" 27712 " [[deprecated]]\n" 27713 " ~Foo() {}\n" 27714 " [[deprecated]]\n" 27715 " void f();\n" 27716 " [[deprecated]]\n" 27717 " void f() {}\n" 27718 "};\n" 27719 "[[deprecated]]\n" 27720 "Bar::Bar() {}\n" 27721 "[[deprecated]]\n" 27722 "Bar::~Bar() {}\n" 27723 "[[deprecated]]\n" 27724 "void g() {}", 27725 CtorDtorCode, Style); 27726 27727 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 27728 verifyFormat("struct Foo {\n" 27729 " [[deprecated]]\n" 27730 " Foo();\n" 27731 " [[deprecated]]\n" 27732 " Foo()\n" 27733 " {\n" 27734 " }\n" 27735 " [[deprecated]]\n" 27736 " ~Foo();\n" 27737 " [[deprecated]]\n" 27738 " ~Foo()\n" 27739 " {\n" 27740 " }\n" 27741 " [[deprecated]]\n" 27742 " void f();\n" 27743 " [[deprecated]]\n" 27744 " void f()\n" 27745 " {\n" 27746 " }\n" 27747 "};\n" 27748 "[[deprecated]]\n" 27749 "Bar::Bar()\n" 27750 "{\n" 27751 "}\n" 27752 "[[deprecated]]\n" 27753 "Bar::~Bar()\n" 27754 "{\n" 27755 "}\n" 27756 "[[deprecated]]\n" 27757 "void g()\n" 27758 "{\n" 27759 "}", 27760 CtorDtorCode, Style); 27761 27762 verifyFormat("struct Foo {\n" 27763 " [[maybe_unused]]\n" 27764 " void operator+();\n" 27765 "};\n" 27766 "[[nodiscard]]\n" 27767 "Foo &operator-(Foo &);", 27768 Style); 27769 27770 Style.ReferenceAlignment = FormatStyle::ReferenceAlignmentStyle::RAS_Left; 27771 verifyFormat("[[nodiscard]]\n" 27772 "Foo& operator-(Foo&);", 27773 Style); 27774 } 27775 27776 TEST_F(FormatTest, InsertNewlineAtEOF) { 27777 FormatStyle Style = getLLVMStyle(); 27778 Style.InsertNewlineAtEOF = true; 27779 27780 verifyNoChange("int i;\n", Style); 27781 verifyFormat("int i;\n", "int i;", Style); 27782 27783 constexpr StringRef Code{"namespace {\n" 27784 "int i;\n" 27785 "} // namespace"}; 27786 verifyFormat(Code.str() + '\n', Code, Style, 27787 {tooling::Range(19, 13)}); // line 3 27788 } 27789 27790 TEST_F(FormatTest, KeepEmptyLinesAtEOF) { 27791 FormatStyle Style = getLLVMStyle(); 27792 Style.KeepEmptyLines.AtEndOfFile = true; 27793 27794 const StringRef Code{"int i;\n\n"}; 27795 verifyNoChange(Code, Style); 27796 verifyFormat(Code, "int i;\n\n\n", Style); 27797 } 27798 27799 TEST_F(FormatTest, SpaceAfterUDL) { 27800 verifyFormat("auto c = (4s).count();"); 27801 verifyFormat("auto x = 5s .count() == 5;"); 27802 } 27803 27804 TEST_F(FormatTest, InterfaceAsClassMemberName) { 27805 verifyFormat("class Foo {\n" 27806 " int interface;\n" 27807 " Foo::Foo(int iface) : interface{iface} {}\n" 27808 "}"); 27809 } 27810 27811 TEST_F(FormatTest, PreprocessorOverlappingRegions) { 27812 verifyFormat("#ifdef\n\n" 27813 "#else\n" 27814 "#endif", 27815 "#ifdef \n" 27816 " \n" 27817 "\n" 27818 "#else \n" 27819 "#endif ", 27820 getGoogleStyle()); 27821 } 27822 27823 TEST_F(FormatTest, RemoveParentheses) { 27824 FormatStyle Style = getLLVMStyle(); 27825 EXPECT_EQ(Style.RemoveParentheses, FormatStyle::RPS_Leave); 27826 27827 Style.RemoveParentheses = FormatStyle::RPS_MultipleParentheses; 27828 verifyFormat("#define Foo(...) foo((__VA_ARGS__))", Style); 27829 verifyFormat("int x __attribute__((aligned(16))) = 0;", Style); 27830 verifyFormat("decltype((foo->bar)) baz;", Style); 27831 verifyFormat("class __declspec(dllimport) X {};", 27832 "class __declspec((dllimport)) X {};", Style); 27833 verifyFormat("int x = (({ 0; }));", "int x = ((({ 0; })));", Style); 27834 verifyFormat("while (a)\n" 27835 " b;", 27836 "while (((a)))\n" 27837 " b;", 27838 Style); 27839 verifyFormat("while ((a = b))\n" 27840 " c;", 27841 "while (((a = b)))\n" 27842 " c;", 27843 Style); 27844 verifyFormat("if (a)\n" 27845 " b;", 27846 "if (((a)))\n" 27847 " b;", 27848 Style); 27849 verifyFormat("if constexpr ((a = b))\n" 27850 " c;", 27851 "if constexpr (((a = b)))\n" 27852 " c;", 27853 Style); 27854 verifyFormat("if (({ a; }))\n" 27855 " b;", 27856 "if ((({ a; })))\n" 27857 " b;", 27858 Style); 27859 verifyFormat("static_assert((std::is_constructible_v<T, Args &&> && ...));", 27860 "static_assert(((std::is_constructible_v<T, Args &&> && ...)));", 27861 Style); 27862 verifyFormat("foo((a, b));", "foo(((a, b)));", Style); 27863 verifyFormat("foo((a, b));", "foo(((a), b));", Style); 27864 verifyFormat("foo((a, b));", "foo((a, (b)));", Style); 27865 verifyFormat("foo((a, b, c));", "foo((a, ((b)), c));", Style); 27866 verifyFormat("return (0);", "return (((0)));", Style); 27867 verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style); 27868 verifyFormat("return ((... && std::is_convertible_v<TArgsLocal, TArgs>));", 27869 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));", 27870 Style); 27871 27872 Style.RemoveParentheses = FormatStyle::RPS_ReturnStatement; 27873 verifyFormat("#define Return0 return (0);", Style); 27874 verifyFormat("return 0;", "return (0);", Style); 27875 verifyFormat("co_return 0;", "co_return ((0));", Style); 27876 verifyFormat("return 0;", "return (((0)));", Style); 27877 verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style); 27878 verifyFormat("return (... && std::is_convertible_v<TArgsLocal, TArgs>);", 27879 "return (((... && std::is_convertible_v<TArgsLocal, TArgs>)));", 27880 Style); 27881 verifyFormat("inline decltype(auto) f() {\n" 27882 " if (a) {\n" 27883 " return (a);\n" 27884 " }\n" 27885 " return (b);\n" 27886 "}", 27887 "inline decltype(auto) f() {\n" 27888 " if (a) {\n" 27889 " return ((a));\n" 27890 " }\n" 27891 " return ((b));\n" 27892 "}", 27893 Style); 27894 verifyFormat("auto g() {\n" 27895 " decltype(auto) x = [] {\n" 27896 " auto y = [] {\n" 27897 " if (a) {\n" 27898 " return a;\n" 27899 " }\n" 27900 " return b;\n" 27901 " };\n" 27902 " if (c) {\n" 27903 " return (c);\n" 27904 " }\n" 27905 " return (d);\n" 27906 " };\n" 27907 " if (e) {\n" 27908 " return e;\n" 27909 " }\n" 27910 " return f;\n" 27911 "}", 27912 "auto g() {\n" 27913 " decltype(auto) x = [] {\n" 27914 " auto y = [] {\n" 27915 " if (a) {\n" 27916 " return ((a));\n" 27917 " }\n" 27918 " return ((b));\n" 27919 " };\n" 27920 " if (c) {\n" 27921 " return ((c));\n" 27922 " }\n" 27923 " return ((d));\n" 27924 " };\n" 27925 " if (e) {\n" 27926 " return ((e));\n" 27927 " }\n" 27928 " return ((f));\n" 27929 "}", 27930 Style); 27931 27932 Style.ColumnLimit = 25; 27933 verifyFormat("return (a + b) - (c + d);", 27934 "return (((a + b)) -\n" 27935 " ((c + d)));", 27936 Style); 27937 } 27938 27939 TEST_F(FormatTest, AllowBreakBeforeNoexceptSpecifier) { 27940 auto Style = getLLVMStyleWithColumns(35); 27941 27942 EXPECT_EQ(Style.AllowBreakBeforeNoexceptSpecifier, FormatStyle::BBNSS_Never); 27943 verifyFormat("void foo(int arg1,\n" 27944 " double arg2) noexcept;", 27945 Style); 27946 27947 // The following line does not fit within the 35 column limit, but that's what 27948 // happens with no break allowed. 27949 verifyFormat("void bar(int arg1, double arg2) noexcept(\n" 27950 " noexcept(baz(arg1)) &&\n" 27951 " noexcept(baz(arg2)));", 27952 Style); 27953 27954 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;", 27955 Style); 27956 27957 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_Always; 27958 verifyFormat("void foo(int arg1,\n" 27959 " double arg2) noexcept;", 27960 Style); 27961 27962 verifyFormat("void bar(int arg1, double arg2)\n" 27963 " noexcept(noexcept(baz(arg1)) &&\n" 27964 " noexcept(baz(arg2)));", 27965 Style); 27966 27967 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments()\n" 27968 " noexcept;", 27969 Style); 27970 27971 Style.AllowBreakBeforeNoexceptSpecifier = FormatStyle::BBNSS_OnlyWithParen; 27972 verifyFormat("void foo(int arg1,\n" 27973 " double arg2) noexcept;", 27974 Style); 27975 27976 verifyFormat("void bar(int arg1, double arg2)\n" 27977 " noexcept(noexcept(baz(arg1)) &&\n" 27978 " noexcept(baz(arg2)));", 27979 Style); 27980 27981 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;", 27982 Style); 27983 } 27984 27985 TEST_F(FormatTest, PPBranchesInBracedInit) { 27986 verifyFormat("A a_{kFlag1,\n" 27987 "#if BUILD_FLAG\n" 27988 " kFlag2,\n" 27989 "#else\n" 27990 " kFlag3,\n" 27991 "#endif\n" 27992 " kFlag4};", 27993 "A a_{\n" 27994 " kFlag1,\n" 27995 "#if BUILD_FLAG\n" 27996 " kFlag2,\n" 27997 "#else\n" 27998 " kFlag3,\n" 27999 "#endif\n" 28000 " kFlag4\n" 28001 "};"); 28002 } 28003 28004 TEST_F(FormatTest, PPDirectivesAndCommentsInBracedInit) { 28005 verifyFormat("{\n" 28006 " char *a[] = {\n" 28007 " /* abc */ \"abc\",\n" 28008 "#if FOO\n" 28009 " /* xyz */ \"xyz\",\n" 28010 "#endif\n" 28011 " /* last */ \"last\"};\n" 28012 "}", 28013 getLLVMStyleWithColumns(30)); 28014 } 28015 28016 TEST_F(FormatTest, BreakAdjacentStringLiterals) { 28017 constexpr StringRef Code{ 28018 "return \"Code\" \"\\0\\52\\26\\55\\55\\0\" \"x013\" \"\\02\\xBA\";"}; 28019 28020 verifyFormat("return \"Code\"\n" 28021 " \"\\0\\52\\26\\55\\55\\0\"\n" 28022 " \"x013\"\n" 28023 " \"\\02\\xBA\";", 28024 Code); 28025 28026 auto Style = getLLVMStyle(); 28027 Style.BreakAdjacentStringLiterals = false; 28028 verifyFormat(Code, Style); 28029 } 28030 28031 TEST_F(FormatTest, AlignUTFCommentsAndStringLiterals) { 28032 verifyFormat( 28033 "int rus; // А теперь комментарии, например, на русском, 2-байта\n" 28034 "int long_rus; // Верхний коммент еще не превысил границу в 80, однако\n" 28035 " // уже отодвинут. Перенос, при этом, отрабатывает верно"); 28036 28037 auto Style = getLLVMStyle(); 28038 Style.ColumnLimit = 15; 28039 verifyNoChange("#define test \\\n" 28040 " /* 测试 */ \\\n" 28041 " \"aa\" \\\n" 28042 " \"bb\"", 28043 Style); 28044 28045 Style.ColumnLimit = 25; 28046 verifyFormat("struct foo {\n" 28047 " int iiiiii; ///< iiiiii\n" 28048 " int b; ///< ыыы\n" 28049 " int c; ///< ыыыы\n" 28050 "};", 28051 Style); 28052 28053 Style.ColumnLimit = 35; 28054 verifyFormat("#define SENSOR_DESC_1 \\\n" 28055 " \"{\" \\\n" 28056 " \"unit_of_measurement: \\\"°C\\\",\" \\\n" 28057 " \"}\"", 28058 Style); 28059 28060 Style.ColumnLimit = 80; 28061 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 28062 verifyFormat("Languages languages = {\n" 28063 " Language{{'e', 'n'}, U\"Test English\" },\n" 28064 " Language{{'l', 'v'}, U\"Test Latviešu\"},\n" 28065 " Language{{'r', 'u'}, U\"Test Русский\" },\n" 28066 "};", 28067 Style); 28068 } 28069 28070 TEST_F(FormatTest, SpaceBetweenKeywordAndLiteral) { 28071 verifyFormat("return .5;"); 28072 verifyFormat("return not '5';"); 28073 verifyFormat("return sizeof \"5\";"); 28074 } 28075 28076 TEST_F(FormatTest, BreakBinaryOperations) { 28077 auto Style = getLLVMStyleWithColumns(60); 28078 EXPECT_EQ(Style.BreakBinaryOperations, FormatStyle::BBO_Never); 28079 28080 // Logical operations 28081 verifyFormat("if (condition1 && condition2) {\n" 28082 "}", 28083 Style); 28084 28085 verifyFormat("if (condition1 && condition2 &&\n" 28086 " (condition3 || condition4) && condition5 &&\n" 28087 " condition6) {\n" 28088 "}", 28089 Style); 28090 28091 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n" 28092 " loooooooooooooooooooooongcondition2) {\n" 28093 "}", 28094 Style); 28095 28096 // Arithmetic 28097 verifyFormat("const int result = lhs + rhs;", Style); 28098 28099 verifyFormat("const int result = loooooooongop1 + looooooooongop2 +\n" 28100 " loooooooooooooooooooooongop3;", 28101 Style); 28102 28103 verifyFormat("result = longOperand1 + longOperand2 -\n" 28104 " (longOperand3 + longOperand4) -\n" 28105 " longOperand5 * longOperand6;", 28106 Style); 28107 28108 verifyFormat("const int result =\n" 28109 " operand1 + operand2 - (operand3 + operand4);", 28110 Style); 28111 28112 // Check operator>> special case. 28113 verifyFormat("std::cin >> longOperand_1 >> longOperand_2 >>\n" 28114 " longOperand_3_;", 28115 Style); 28116 28117 Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine; 28118 28119 // Logical operations 28120 verifyFormat("if (condition1 && condition2) {\n" 28121 "}", 28122 Style); 28123 28124 verifyFormat("if (condition1 && // comment\n" 28125 " condition2 &&\n" 28126 " (condition3 || condition4) && // comment\n" 28127 " condition5 &&\n" 28128 " condition6) {\n" 28129 "}", 28130 Style); 28131 28132 verifyFormat("if (loooooooooooooooooooooongcondition1 &&\n" 28133 " loooooooooooooooooooooongcondition2) {\n" 28134 "}", 28135 Style); 28136 28137 // Arithmetic 28138 verifyFormat("const int result = lhs + rhs;", Style); 28139 28140 verifyFormat("result = loooooooooooooooooooooongop1 +\n" 28141 " loooooooooooooooooooooongop2 +\n" 28142 " loooooooooooooooooooooongop3;", 28143 Style); 28144 28145 verifyFormat("const int result =\n" 28146 " operand1 + operand2 - (operand3 + operand4);", 28147 Style); 28148 28149 verifyFormat("result = longOperand1 +\n" 28150 " longOperand2 -\n" 28151 " (longOperand3 + longOperand4) -\n" 28152 " longOperand5 +\n" 28153 " longOperand6;", 28154 Style); 28155 28156 verifyFormat("result = operand1 +\n" 28157 " operand2 -\n" 28158 " operand3 +\n" 28159 " operand4 -\n" 28160 " operand5 +\n" 28161 " operand6;", 28162 Style); 28163 28164 // Ensure mixed precedence operations are handled properly 28165 verifyFormat("result = op1 + op2 * op3 - op4;", Style); 28166 28167 verifyFormat("result = operand1 +\n" 28168 " operand2 /\n" 28169 " operand3 +\n" 28170 " operand4 /\n" 28171 " operand5 *\n" 28172 " operand6;", 28173 Style); 28174 28175 verifyFormat("result = operand1 *\n" 28176 " operand2 -\n" 28177 " operand3 *\n" 28178 " operand4 -\n" 28179 " operand5 +\n" 28180 " operand6;", 28181 Style); 28182 28183 verifyFormat("result = operand1 *\n" 28184 " (operand2 - operand3 * operand4) -\n" 28185 " operand5 +\n" 28186 " operand6;", 28187 Style); 28188 28189 verifyFormat("result = operand1.member *\n" 28190 " (operand2.member() - operand3->mem * operand4) -\n" 28191 " operand5.member() +\n" 28192 " operand6->member;", 28193 Style); 28194 28195 // Check operator>> special case. 28196 verifyFormat("std::cin >>\n" 28197 " longOperand_1 >>\n" 28198 " longOperand_2 >>\n" 28199 " longOperand_3_;", 28200 Style); 28201 28202 Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence; 28203 verifyFormat("result = op1 + op2 * op3 - op4;", Style); 28204 28205 verifyFormat("result = operand1 +\n" 28206 " operand2 / operand3 +\n" 28207 " operand4 / operand5 * operand6;", 28208 Style); 28209 28210 verifyFormat("result = operand1 * operand2 -\n" 28211 " operand3 * operand4 -\n" 28212 " operand5 +\n" 28213 " operand6;", 28214 Style); 28215 28216 verifyFormat("result = operand1 * (operand2 - operand3 * operand4) -\n" 28217 " operand5 +\n" 28218 " operand6;", 28219 Style); 28220 28221 verifyFormat("std::uint32_t a = byte_buffer[0] |\n" 28222 " byte_buffer[1] << 8 |\n" 28223 " byte_buffer[2] << 16 |\n" 28224 " byte_buffer[3] << 24;", 28225 Style); 28226 28227 // Check operator>> special case. 28228 verifyFormat("std::cin >>\n" 28229 " longOperand_1 >>\n" 28230 " longOperand_2 >>\n" 28231 " longOperand_3_;", 28232 Style); 28233 28234 Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine; 28235 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 28236 28237 // Logical operations 28238 verifyFormat("if (condition1 && condition2) {\n" 28239 "}", 28240 Style); 28241 28242 verifyFormat("if (loooooooooooooooooooooongcondition1\n" 28243 " && loooooooooooooooooooooongcondition2) {\n" 28244 "}", 28245 Style); 28246 28247 // Arithmetic 28248 verifyFormat("const int result = lhs + rhs;", Style); 28249 28250 verifyFormat("result = loooooooooooooooooooooongop1\n" 28251 " + loooooooooooooooooooooongop2\n" 28252 " + loooooooooooooooooooooongop3;", 28253 Style); 28254 28255 verifyFormat("const int result =\n" 28256 " operand1 + operand2 - (operand3 + operand4);", 28257 Style); 28258 28259 verifyFormat("result = longOperand1\n" 28260 " + longOperand2\n" 28261 " - (longOperand3 + longOperand4)\n" 28262 " - longOperand5\n" 28263 " + longOperand6;", 28264 Style); 28265 28266 verifyFormat("result = operand1\n" 28267 " + operand2\n" 28268 " - operand3\n" 28269 " + operand4\n" 28270 " - operand5\n" 28271 " + operand6;", 28272 Style); 28273 28274 // Ensure mixed precedence operations are handled properly 28275 verifyFormat("result = op1 + op2 * op3 - op4;", Style); 28276 28277 verifyFormat("result = operand1\n" 28278 " + operand2\n" 28279 " / operand3\n" 28280 " + operand4\n" 28281 " / operand5\n" 28282 " * operand6;", 28283 Style); 28284 28285 verifyFormat("result = operand1\n" 28286 " * operand2\n" 28287 " - operand3\n" 28288 " * operand4\n" 28289 " - operand5\n" 28290 " + operand6;", 28291 Style); 28292 28293 verifyFormat("result = operand1\n" 28294 " * (operand2 - operand3 * operand4)\n" 28295 " - operand5\n" 28296 " + operand6;", 28297 Style); 28298 28299 verifyFormat("std::uint32_t a = byte_buffer[0]\n" 28300 " | byte_buffer[1]\n" 28301 " << 8\n" 28302 " | byte_buffer[2]\n" 28303 " << 16\n" 28304 " | byte_buffer[3]\n" 28305 " << 24;", 28306 Style); 28307 28308 // Check operator>> special case. 28309 verifyFormat("std::cin\n" 28310 " >> longOperand_1\n" 28311 " >> longOperand_2\n" 28312 " >> longOperand_3_;", 28313 Style); 28314 28315 Style.BreakBinaryOperations = FormatStyle::BBO_RespectPrecedence; 28316 verifyFormat("result = op1 + op2 * op3 - op4;", Style); 28317 28318 verifyFormat("result = operand1\n" 28319 " + operand2 / operand3\n" 28320 " + operand4 / operand5 * operand6;", 28321 Style); 28322 28323 verifyFormat("result = operand1 * operand2\n" 28324 " - operand3 * operand4\n" 28325 " - operand5\n" 28326 " + operand6;", 28327 Style); 28328 28329 verifyFormat("result = operand1 * (operand2 - operand3 * operand4)\n" 28330 " - operand5\n" 28331 " + operand6;", 28332 Style); 28333 28334 verifyFormat("std::uint32_t a = byte_buffer[0]\n" 28335 " | byte_buffer[1] << 8\n" 28336 " | byte_buffer[2] << 16\n" 28337 " | byte_buffer[3] << 24;", 28338 Style); 28339 28340 // Check operator>> special case. 28341 verifyFormat("std::cin\n" 28342 " >> longOperand_1\n" 28343 " >> longOperand_2\n" 28344 " >> longOperand_3_;", 28345 Style); 28346 } 28347 28348 TEST_F(FormatTest, RemoveEmptyLinesInUnwrappedLines) { 28349 auto Style = getLLVMStyle(); 28350 Style.RemoveEmptyLinesInUnwrappedLines = true; 28351 28352 verifyFormat("int c = a + b;", 28353 "int c\n" 28354 "\n" 28355 " = a + b;", 28356 Style); 28357 28358 verifyFormat("enum : unsigned { AA = 0, BB } myEnum;", 28359 "enum : unsigned\n" 28360 "\n" 28361 "{\n" 28362 " AA = 0,\n" 28363 " BB\n" 28364 "} myEnum;", 28365 Style); 28366 28367 verifyFormat("class B : public E {\n" 28368 "private:\n" 28369 "};", 28370 "class B : public E\n" 28371 "\n" 28372 "{\n" 28373 "private:\n" 28374 "};", 28375 Style); 28376 28377 verifyFormat( 28378 "struct AAAAAAAAAAAAAAA test[3] = {{56, 23, \"hello\"}, {7, 5, \"!!\"}};", 28379 "struct AAAAAAAAAAAAAAA test[3] = {{56,\n" 28380 "\n" 28381 " 23, \"hello\"},\n" 28382 " {7, 5, \"!!\"}};", 28383 Style); 28384 28385 verifyFormat("int myFunction(int aaaaaaaaaaaaa, int ccccccccccccc, int d);", 28386 "int myFunction(\n" 28387 "\n" 28388 " int aaaaaaaaaaaaa,\n" 28389 "\n" 28390 " int ccccccccccccc, int d);", 28391 Style); 28392 28393 verifyFormat("switch (e) {\n" 28394 "case 1:\n" 28395 " return e;\n" 28396 "case 2:\n" 28397 " return 2;\n" 28398 "}", 28399 "switch (\n" 28400 "\n" 28401 " e) {\n" 28402 "case 1:\n" 28403 " return e;\n" 28404 "case 2:\n" 28405 " return 2;\n" 28406 "}", 28407 Style); 28408 28409 verifyFormat("while (true) {\n" 28410 "}", 28411 "while (\n" 28412 "\n" 28413 " true) {\n" 28414 "}", 28415 Style); 28416 28417 verifyFormat("void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames(\n" 28418 " std::map<int, std::string> *outputMap);", 28419 "void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames\n" 28420 "\n" 28421 " (std::map<int, std::string> *outputMap);", 28422 Style); 28423 } 28424 28425 TEST_F(FormatTest, KeepFormFeed) { 28426 auto Style = getLLVMStyle(); 28427 Style.KeepFormFeed = true; 28428 28429 constexpr StringRef NoFormFeed{"int i;\n" 28430 "\n" 28431 "void f();"}; 28432 verifyFormat(NoFormFeed, 28433 "int i;\n" 28434 " \f\n" 28435 "void f();", 28436 Style); 28437 verifyFormat(NoFormFeed, 28438 "int i;\n" 28439 "\n" 28440 "\fvoid f();", 28441 Style); 28442 verifyFormat(NoFormFeed, 28443 "\fint i;\n" 28444 "\n" 28445 "void f();", 28446 Style); 28447 verifyFormat(NoFormFeed, 28448 "int i;\n" 28449 "\n" 28450 "void f();\f", 28451 Style); 28452 28453 constexpr StringRef FormFeed{"int i;\n" 28454 "\f\n" 28455 "void f();"}; 28456 verifyNoChange(FormFeed, Style); 28457 28458 Style.LineEnding = FormatStyle::LE_LF; 28459 verifyFormat(FormFeed, 28460 "int i;\r\n" 28461 "\f\r\n" 28462 "void f();", 28463 Style); 28464 28465 constexpr StringRef FormFeedBeforeEmptyLine{"int i;\n" 28466 "\f\n" 28467 "\n" 28468 "void f();"}; 28469 Style.MaxEmptyLinesToKeep = 2; 28470 verifyFormat(FormFeedBeforeEmptyLine, 28471 "int i;\n" 28472 "\n" 28473 "\f\n" 28474 "void f();", 28475 Style); 28476 verifyFormat(FormFeedBeforeEmptyLine, 28477 "int i;\n" 28478 "\f\n" 28479 "\f\n" 28480 "void f();", 28481 Style); 28482 } 28483 28484 TEST_F(FormatTest, ShortNamespacesOption) { 28485 auto Style = getLLVMStyle(); 28486 Style.AllowShortNamespacesOnASingleLine = true; 28487 Style.CompactNamespaces = true; 28488 Style.FixNamespaceComments = false; 28489 28490 // Basic functionality. 28491 verifyFormat("namespace foo { class bar; }", Style); 28492 verifyFormat("namespace foo::bar { class baz; }", Style); 28493 verifyFormat("namespace { class bar; }", Style); 28494 verifyFormat("namespace foo {\n" 28495 "class bar;\n" 28496 "class baz;\n" 28497 "}", 28498 Style); 28499 28500 // Trailing comments prevent merging. 28501 verifyFormat("namespace foo { namespace baz {\n" 28502 "class qux;\n" 28503 "} // comment\n" 28504 "}", 28505 Style); 28506 28507 // Make sure code doesn't walk too far on unbalanced code. 28508 verifyFormat("namespace foo {", Style); 28509 verifyFormat("namespace foo {\n" 28510 "class baz;", 28511 Style); 28512 verifyFormat("namespace foo {\n" 28513 "namespace bar { class baz; }", 28514 Style); 28515 28516 // Nested namespaces. 28517 verifyFormat("namespace foo { namespace bar { class baz; } }", Style); 28518 28519 // Without CompactNamespaces, we won't merge consecutive namespace 28520 // declarations. 28521 Style.CompactNamespaces = false; 28522 verifyFormat("namespace foo {\n" 28523 "namespace bar { class baz; }\n" 28524 "}", 28525 Style); 28526 28527 verifyFormat("namespace foo {\n" 28528 "namespace bar { class baz; }\n" 28529 "namespace qux { class quux; }\n" 28530 "}", 28531 Style); 28532 28533 Style.CompactNamespaces = true; 28534 28535 // Varying inner content. 28536 verifyFormat("namespace foo {\n" 28537 "int f() { return 5; }\n" 28538 "}", 28539 Style); 28540 verifyFormat("namespace foo { template <T> struct bar; }", Style); 28541 verifyFormat("namespace foo { constexpr int num = 42; }", Style); 28542 28543 // Validate nested namespace wrapping scenarios around the ColumnLimit. 28544 Style.ColumnLimit = 64; 28545 28546 // Validate just under the ColumnLimit. 28547 verifyFormat( 28548 "namespace foo { namespace bar { namespace baz { class qux; } } }", 28549 Style); 28550 28551 // Validate just over the ColumnLimit. 28552 verifyFormat("namespace foo { namespace baar { namespace baaz {\n" 28553 "class quux;\n" 28554 "}}}", 28555 Style); 28556 28557 verifyFormat( 28558 "namespace foo { namespace bar { namespace baz { namespace qux {\n" 28559 "class quux;\n" 28560 "}}}}", 28561 Style); 28562 28563 // Validate that the ColumnLimit logic accounts for trailing content as well. 28564 verifyFormat("namespace foo { namespace bar { class qux; } } // extra", 28565 Style); 28566 28567 verifyFormat("namespace foo { namespace bar { namespace baz {\n" 28568 "class qux;\n" 28569 "}}} // extra", 28570 Style); 28571 28572 // FIXME: Ideally AllowShortNamespacesOnASingleLine would disable the trailing 28573 // namespace comment from 'FixNamespaceComments', as it's not really necessary 28574 // in this scenario, but the two options work at very different layers of the 28575 // formatter, so I'm not sure how to make them interact. 28576 // 28577 // As it stands, the trailing comment will be added and likely make the line 28578 // too long to fit within the ColumnLimit, reducing the how likely the line 28579 // will still fit on a single line. The recommendation for now is to use the 28580 // concatenated namespace syntax instead. e.g. 'namespace foo::bar' 28581 Style.FixNamespaceComments = true; 28582 verifyFormat( 28583 "namespace foo { namespace bar { namespace baz {\n" 28584 "class qux;\n" 28585 "}}} // namespace foo::bar::baz", 28586 "namespace foo { namespace bar { namespace baz { class qux; } } }", 28587 Style); 28588 } 28589 28590 TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) { 28591 auto Style = getLLVMStyle(); 28592 Style.FixNamespaceComments = false; 28593 Style.MaxEmptyLinesToKeep = 2; 28594 Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never; 28595 28596 // Empty namespace. 28597 verifyFormat("namespace N {}", Style); 28598 28599 // Single namespace. 28600 verifyFormat("namespace N {\n" 28601 "int f1(int a) { return 2 * a; }\n" 28602 "}", 28603 "namespace N {\n" 28604 "\n" 28605 "\n" 28606 "int f1(int a) { return 2 * a; }\n" 28607 "\n" 28608 "\n" 28609 "}", 28610 Style); 28611 28612 // Nested namespace. 28613 verifyFormat("namespace N1 {\n" 28614 "namespace N2 {\n" 28615 "int a = 1;\n" 28616 "}\n" 28617 "}", 28618 "namespace N1 {\n" 28619 "\n" 28620 "\n" 28621 "namespace N2 {\n" 28622 "\n" 28623 "int a = 1;\n" 28624 "\n" 28625 "}\n" 28626 "\n" 28627 "\n" 28628 "}", 28629 Style); 28630 28631 Style.CompactNamespaces = true; 28632 28633 verifyFormat("namespace N1 { namespace N2 {\n" 28634 "int a = 1;\n" 28635 "}}", 28636 "namespace N1 { namespace N2 {\n" 28637 "\n" 28638 "\n" 28639 "int a = 1;\n" 28640 "\n" 28641 "\n" 28642 "}}", 28643 Style); 28644 } 28645 28646 TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) { 28647 auto Style = getLLVMStyle(); 28648 Style.FixNamespaceComments = false; 28649 Style.MaxEmptyLinesToKeep = 2; 28650 Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Always; 28651 28652 // Empty namespace. 28653 verifyFormat("namespace N {}", Style); 28654 28655 // Single namespace. 28656 verifyFormat("namespace N {\n" 28657 "\n" 28658 "int f1(int a) { return 2 * a; }\n" 28659 "\n" 28660 "}", 28661 "namespace N {\n" 28662 "int f1(int a) { return 2 * a; }\n" 28663 "}", 28664 Style); 28665 28666 // Nested namespace. 28667 verifyFormat("namespace N1 {\n" 28668 "namespace N2 {\n" 28669 "\n" 28670 "int a = 1;\n" 28671 "\n" 28672 "}\n" 28673 "}", 28674 "namespace N1 {\n" 28675 "namespace N2 {\n" 28676 "int a = 1;\n" 28677 "}\n" 28678 "}", 28679 Style); 28680 28681 verifyFormat("namespace N1 {\n" 28682 "\n" 28683 "namespace N2 {\n" 28684 "\n" 28685 "\n" 28686 "int a = 1;\n" 28687 "\n" 28688 "\n" 28689 "}\n" 28690 "\n" 28691 "}", 28692 "namespace N1 {\n" 28693 "\n" 28694 "namespace N2 {\n" 28695 "\n" 28696 "\n" 28697 "\n" 28698 "int a = 1;\n" 28699 "\n" 28700 "\n" 28701 "\n" 28702 "}\n" 28703 "\n" 28704 "}", 28705 Style); 28706 28707 Style.CompactNamespaces = true; 28708 28709 verifyFormat("namespace N1 { namespace N2 {\n" 28710 "\n" 28711 "int a = 1;\n" 28712 "\n" 28713 "}}", 28714 "namespace N1 { namespace N2 {\n" 28715 "int a = 1;\n" 28716 "}}", 28717 Style); 28718 } 28719 28720 } // namespace 28721 } // namespace test 28722 } // namespace format 28723 } // namespace clang 28724