1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/Format/Format.h" 10 11 #include "../Tooling/ReplacementTest.h" 12 #include "FormatTestUtils.h" 13 14 #include "llvm/Support/Debug.h" 15 #include "llvm/Support/MemoryBuffer.h" 16 #include "gtest/gtest.h" 17 18 #define DEBUG_TYPE "format-test" 19 20 using clang::tooling::ReplacementTest; 21 using clang::tooling::toReplacements; 22 using testing::internal::ScopedTrace; 23 24 namespace clang { 25 namespace format { 26 namespace { 27 28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 29 30 class FormatTest : public ::testing::Test { 31 protected: 32 enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck }; 33 34 std::string format(llvm::StringRef Code, 35 const FormatStyle &Style = getLLVMStyle(), 36 StatusCheck CheckComplete = SC_ExpectComplete) { 37 LLVM_DEBUG(llvm::errs() << "---\n"); 38 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 39 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 40 FormattingAttemptStatus Status; 41 tooling::Replacements Replaces = 42 reformat(Style, Code, Ranges, "<stdin>", &Status); 43 if (CheckComplete != SC_DoNotCheck) { 44 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 45 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 46 << Code << "\n\n"; 47 } 48 ReplacementCount = Replaces.size(); 49 auto Result = applyAllReplacements(Code, Replaces); 50 EXPECT_TRUE(static_cast<bool>(Result)); 51 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 52 return *Result; 53 } 54 55 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 56 Style.ColumnLimit = ColumnLimit; 57 return Style; 58 } 59 60 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 61 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 62 } 63 64 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 65 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 66 } 67 68 void _verifyFormat(const char *File, int Line, llvm::StringRef Expected, 69 llvm::StringRef Code, 70 const FormatStyle &Style = getLLVMStyle()) { 71 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 72 EXPECT_EQ(Expected.str(), format(Expected, Style)) 73 << "Expected code is not stable"; 74 EXPECT_EQ(Expected.str(), format(Code, Style)); 75 if (Style.Language == FormatStyle::LK_Cpp) { 76 // Objective-C++ is a superset of C++, so everything checked for C++ 77 // needs to be checked for Objective-C++ as well. 78 FormatStyle ObjCStyle = Style; 79 ObjCStyle.Language = FormatStyle::LK_ObjC; 80 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 81 } 82 } 83 84 void _verifyFormat(const char *File, int Line, llvm::StringRef Code, 85 const FormatStyle &Style = getLLVMStyle()) { 86 _verifyFormat(File, Line, Code, test::messUp(Code), Style); 87 } 88 89 void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code, 90 const FormatStyle &Style = getLLVMStyle()) { 91 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 92 EXPECT_EQ(Code.str(), 93 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 94 } 95 96 void _verifyIndependentOfContext(const char *File, int Line, 97 llvm::StringRef Text, 98 const FormatStyle &Style = getLLVMStyle()) { 99 _verifyFormat(File, Line, Text, Style); 100 _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(), 101 Style); 102 } 103 104 /// \brief Verify that clang-format does not crash on the given input. 105 void verifyNoCrash(llvm::StringRef Code, 106 const FormatStyle &Style = getLLVMStyle()) { 107 format(Code, Style, SC_DoNotCheck); 108 } 109 110 int ReplacementCount; 111 }; 112 113 #define verifyIndependentOfContext(...) \ 114 _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__) 115 #define verifyIncompleteFormat(...) \ 116 _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__) 117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__) 118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle()) 119 120 TEST_F(FormatTest, MessUp) { 121 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 122 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 123 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 124 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 125 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 126 } 127 128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { 129 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); 130 } 131 132 TEST_F(FormatTest, LLVMStyleOverride) { 133 EXPECT_EQ(FormatStyle::LK_Proto, 134 getLLVMStyle(FormatStyle::LK_Proto).Language); 135 } 136 137 //===----------------------------------------------------------------------===// 138 // Basic function tests. 139 //===----------------------------------------------------------------------===// 140 141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 142 EXPECT_EQ(";", format(";")); 143 } 144 145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 146 EXPECT_EQ("int i;", format(" int i;")); 147 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 148 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 149 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 150 } 151 152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 153 EXPECT_EQ("int i;", format("int\ni;")); 154 } 155 156 TEST_F(FormatTest, FormatsNestedBlockStatements) { 157 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 158 } 159 160 TEST_F(FormatTest, FormatsNestedCall) { 161 verifyFormat("Method(f1, f2(f3));"); 162 verifyFormat("Method(f1(f2, f3()));"); 163 verifyFormat("Method(f1(f2, (f3())));"); 164 } 165 166 TEST_F(FormatTest, NestedNameSpecifiers) { 167 verifyFormat("vector<::Type> v;"); 168 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 169 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 170 verifyFormat("static constexpr bool Bar = typeof(bar())::value;"); 171 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;"); 172 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;"); 173 verifyFormat("bool a = 2 < ::SomeFunction();"); 174 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 175 verifyFormat("some::string getName();"); 176 } 177 178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 179 EXPECT_EQ("if (a) {\n" 180 " f();\n" 181 "}", 182 format("if(a){f();}")); 183 EXPECT_EQ(4, ReplacementCount); 184 EXPECT_EQ("if (a) {\n" 185 " f();\n" 186 "}", 187 format("if (a) {\n" 188 " f();\n" 189 "}")); 190 EXPECT_EQ(0, ReplacementCount); 191 EXPECT_EQ("/*\r\n" 192 "\r\n" 193 "*/\r\n", 194 format("/*\r\n" 195 "\r\n" 196 "*/\r\n")); 197 EXPECT_EQ(0, ReplacementCount); 198 } 199 200 TEST_F(FormatTest, RemovesEmptyLines) { 201 EXPECT_EQ("class C {\n" 202 " int i;\n" 203 "};", 204 format("class C {\n" 205 " int i;\n" 206 "\n" 207 "};")); 208 209 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 210 EXPECT_EQ("namespace N {\n" 211 "\n" 212 "int i;\n" 213 "}", 214 format("namespace N {\n" 215 "\n" 216 "int i;\n" 217 "}", 218 getGoogleStyle())); 219 EXPECT_EQ("/* something */ namespace N {\n" 220 "\n" 221 "int i;\n" 222 "}", 223 format("/* something */ namespace N {\n" 224 "\n" 225 "int i;\n" 226 "}", 227 getGoogleStyle())); 228 EXPECT_EQ("inline namespace N {\n" 229 "\n" 230 "int i;\n" 231 "}", 232 format("inline namespace N {\n" 233 "\n" 234 "int i;\n" 235 "}", 236 getGoogleStyle())); 237 EXPECT_EQ("/* something */ inline namespace N {\n" 238 "\n" 239 "int i;\n" 240 "}", 241 format("/* something */ inline namespace N {\n" 242 "\n" 243 "int i;\n" 244 "}", 245 getGoogleStyle())); 246 EXPECT_EQ("export namespace N {\n" 247 "\n" 248 "int i;\n" 249 "}", 250 format("export namespace N {\n" 251 "\n" 252 "int i;\n" 253 "}", 254 getGoogleStyle())); 255 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 256 "\n" 257 "int i;\n" 258 "}", 259 format("extern /**/ \"C\" /**/ {\n" 260 "\n" 261 "int i;\n" 262 "}", 263 getGoogleStyle())); 264 265 // ...but do keep inlining and removing empty lines for non-block extern "C" 266 // functions. 267 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 268 EXPECT_EQ("extern \"C\" int f() {\n" 269 " int i = 42;\n" 270 " return i;\n" 271 "}", 272 format("extern \"C\" int f() {\n" 273 "\n" 274 " int i = 42;\n" 275 " return i;\n" 276 "}", 277 getGoogleStyle())); 278 279 // Remove empty lines at the beginning and end of blocks. 280 EXPECT_EQ("void f() {\n" 281 "\n" 282 " if (a) {\n" 283 "\n" 284 " f();\n" 285 " }\n" 286 "}", 287 format("void f() {\n" 288 "\n" 289 " if (a) {\n" 290 "\n" 291 " f();\n" 292 "\n" 293 " }\n" 294 "\n" 295 "}", 296 getLLVMStyle())); 297 EXPECT_EQ("void f() {\n" 298 " if (a) {\n" 299 " f();\n" 300 " }\n" 301 "}", 302 format("void f() {\n" 303 "\n" 304 " if (a) {\n" 305 "\n" 306 " f();\n" 307 "\n" 308 " }\n" 309 "\n" 310 "}", 311 getGoogleStyle())); 312 313 // Don't remove empty lines in more complex control statements. 314 EXPECT_EQ("void f() {\n" 315 " if (a) {\n" 316 " f();\n" 317 "\n" 318 " } else if (b) {\n" 319 " f();\n" 320 " }\n" 321 "}", 322 format("void f() {\n" 323 " if (a) {\n" 324 " f();\n" 325 "\n" 326 " } else if (b) {\n" 327 " f();\n" 328 "\n" 329 " }\n" 330 "\n" 331 "}")); 332 333 // Don't remove empty lines before namespace endings. 334 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 335 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 336 EXPECT_EQ("namespace {\n" 337 "int i;\n" 338 "\n" 339 "}", 340 format("namespace {\n" 341 "int i;\n" 342 "\n" 343 "}", 344 LLVMWithNoNamespaceFix)); 345 EXPECT_EQ("namespace {\n" 346 "int i;\n" 347 "}", 348 format("namespace {\n" 349 "int i;\n" 350 "}", 351 LLVMWithNoNamespaceFix)); 352 EXPECT_EQ("namespace {\n" 353 "int i;\n" 354 "\n" 355 "};", 356 format("namespace {\n" 357 "int i;\n" 358 "\n" 359 "};", 360 LLVMWithNoNamespaceFix)); 361 EXPECT_EQ("namespace {\n" 362 "int i;\n" 363 "};", 364 format("namespace {\n" 365 "int i;\n" 366 "};", 367 LLVMWithNoNamespaceFix)); 368 EXPECT_EQ("namespace {\n" 369 "int i;\n" 370 "\n" 371 "}", 372 format("namespace {\n" 373 "int i;\n" 374 "\n" 375 "}")); 376 EXPECT_EQ("namespace {\n" 377 "int i;\n" 378 "\n" 379 "} // namespace", 380 format("namespace {\n" 381 "int i;\n" 382 "\n" 383 "} // namespace")); 384 385 FormatStyle Style = getLLVMStyle(); 386 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 387 Style.MaxEmptyLinesToKeep = 2; 388 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 389 Style.BraceWrapping.AfterClass = true; 390 Style.BraceWrapping.AfterFunction = true; 391 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 392 393 EXPECT_EQ("class Foo\n" 394 "{\n" 395 " Foo() {}\n" 396 "\n" 397 " void funk() {}\n" 398 "};", 399 format("class Foo\n" 400 "{\n" 401 " Foo()\n" 402 " {\n" 403 " }\n" 404 "\n" 405 " void funk() {}\n" 406 "};", 407 Style)); 408 } 409 410 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 411 verifyFormat("x = (a) and (b);"); 412 verifyFormat("x = (a) or (b);"); 413 verifyFormat("x = (a) bitand (b);"); 414 verifyFormat("x = (a) bitor (b);"); 415 verifyFormat("x = (a) not_eq (b);"); 416 verifyFormat("x = (a) and_eq (b);"); 417 verifyFormat("x = (a) or_eq (b);"); 418 verifyFormat("x = (a) xor (b);"); 419 } 420 421 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 422 verifyFormat("x = compl(a);"); 423 verifyFormat("x = not(a);"); 424 verifyFormat("x = bitand(a);"); 425 // Unary operator must not be merged with the next identifier 426 verifyFormat("x = compl a;"); 427 verifyFormat("x = not a;"); 428 verifyFormat("x = bitand a;"); 429 } 430 431 //===----------------------------------------------------------------------===// 432 // Tests for control statements. 433 //===----------------------------------------------------------------------===// 434 435 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 436 verifyFormat("if (true)\n f();\ng();"); 437 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 438 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 439 verifyFormat("if constexpr (true)\n" 440 " f();\ng();"); 441 verifyFormat("if CONSTEXPR (true)\n" 442 " f();\ng();"); 443 verifyFormat("if constexpr (a)\n" 444 " if constexpr (b)\n" 445 " if constexpr (c)\n" 446 " g();\n" 447 "h();"); 448 verifyFormat("if CONSTEXPR (a)\n" 449 " if CONSTEXPR (b)\n" 450 " if CONSTEXPR (c)\n" 451 " g();\n" 452 "h();"); 453 verifyFormat("if constexpr (a)\n" 454 " if constexpr (b) {\n" 455 " f();\n" 456 " }\n" 457 "g();"); 458 verifyFormat("if CONSTEXPR (a)\n" 459 " if CONSTEXPR (b) {\n" 460 " f();\n" 461 " }\n" 462 "g();"); 463 464 FormatStyle AllowsMergedIf = getLLVMStyle(); 465 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 466 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 467 FormatStyle::SIS_WithoutElse; 468 verifyFormat("if (a)\n" 469 " // comment\n" 470 " f();", 471 AllowsMergedIf); 472 verifyFormat("{\n" 473 " if (a)\n" 474 " label:\n" 475 " f();\n" 476 "}", 477 AllowsMergedIf); 478 verifyFormat("#define A \\\n" 479 " if (a) \\\n" 480 " label: \\\n" 481 " f()", 482 AllowsMergedIf); 483 verifyFormat("if (a)\n" 484 " ;", 485 AllowsMergedIf); 486 verifyFormat("if (a)\n" 487 " if (b) return;", 488 AllowsMergedIf); 489 490 verifyFormat("if (a) // Can't merge this\n" 491 " f();\n", 492 AllowsMergedIf); 493 verifyFormat("if (a) /* still don't merge */\n" 494 " f();", 495 AllowsMergedIf); 496 verifyFormat("if (a) { // Never merge this\n" 497 " f();\n" 498 "}", 499 AllowsMergedIf); 500 verifyFormat("if (a) { /* Never merge this */\n" 501 " f();\n" 502 "}", 503 AllowsMergedIf); 504 505 AllowsMergedIf.ColumnLimit = 14; 506 verifyFormat("if (a) return;", AllowsMergedIf); 507 verifyFormat("if (aaaaaaaaa)\n" 508 " return;", 509 AllowsMergedIf); 510 511 AllowsMergedIf.ColumnLimit = 13; 512 verifyFormat("if (a)\n return;", AllowsMergedIf); 513 } 514 515 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 516 FormatStyle AllowsMergedIf = getLLVMStyle(); 517 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 518 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 519 FormatStyle::SIS_WithoutElse; 520 verifyFormat("if (a)\n" 521 " f();\n" 522 "else {\n" 523 " g();\n" 524 "}", 525 AllowsMergedIf); 526 verifyFormat("if (a)\n" 527 " f();\n" 528 "else\n" 529 " g();\n", 530 AllowsMergedIf); 531 532 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always; 533 534 verifyFormat("if (a) f();\n" 535 "else {\n" 536 " g();\n" 537 "}", 538 AllowsMergedIf); 539 verifyFormat("if (a) f();\n" 540 "else {\n" 541 " if (a) f();\n" 542 " else {\n" 543 " g();\n" 544 " }\n" 545 " g();\n" 546 "}", 547 AllowsMergedIf); 548 } 549 550 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 551 FormatStyle AllowsMergedLoops = getLLVMStyle(); 552 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 553 verifyFormat("while (true) continue;", AllowsMergedLoops); 554 verifyFormat("for (;;) continue;", AllowsMergedLoops); 555 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 556 verifyFormat("while (true)\n" 557 " ;", 558 AllowsMergedLoops); 559 verifyFormat("for (;;)\n" 560 " ;", 561 AllowsMergedLoops); 562 verifyFormat("for (;;)\n" 563 " for (;;) continue;", 564 AllowsMergedLoops); 565 verifyFormat("for (;;) // Can't merge this\n" 566 " continue;", 567 AllowsMergedLoops); 568 verifyFormat("for (;;) /* still don't merge */\n" 569 " continue;", 570 AllowsMergedLoops); 571 verifyFormat("do a++;\n" 572 "while (true);", 573 AllowsMergedLoops); 574 verifyFormat("do /* Don't merge */\n" 575 " a++;\n" 576 "while (true);", 577 AllowsMergedLoops); 578 verifyFormat("do // Don't merge\n" 579 " a++;\n" 580 "while (true);", 581 AllowsMergedLoops); 582 verifyFormat("do\n" 583 " // Don't merge\n" 584 " a++;\n" 585 "while (true);", 586 AllowsMergedLoops); 587 // Without braces labels are interpreted differently. 588 verifyFormat("{\n" 589 " do\n" 590 " label:\n" 591 " a++;\n" 592 " while (true);\n" 593 "}", 594 AllowsMergedLoops); 595 } 596 597 TEST_F(FormatTest, FormatShortBracedStatements) { 598 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 599 AllowSimpleBracedStatements.ColumnLimit = 40; 600 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 601 FormatStyle::SBS_Always; 602 603 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 604 FormatStyle::SIS_WithoutElse; 605 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 606 607 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 608 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 609 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 610 611 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 612 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 613 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 614 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 615 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 616 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 617 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 618 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 619 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 620 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 621 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 622 AllowSimpleBracedStatements); 623 verifyFormat("if (true) {\n" 624 " ffffffffffffffffffffffff();\n" 625 "}", 626 AllowSimpleBracedStatements); 627 verifyFormat("if (true) {\n" 628 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 629 "}", 630 AllowSimpleBracedStatements); 631 verifyFormat("if (true) { //\n" 632 " f();\n" 633 "}", 634 AllowSimpleBracedStatements); 635 verifyFormat("if (true) {\n" 636 " f();\n" 637 " f();\n" 638 "}", 639 AllowSimpleBracedStatements); 640 verifyFormat("if (true) {\n" 641 " f();\n" 642 "} else {\n" 643 " f();\n" 644 "}", 645 AllowSimpleBracedStatements); 646 647 verifyFormat("struct A2 {\n" 648 " int X;\n" 649 "};", 650 AllowSimpleBracedStatements); 651 verifyFormat("typedef struct A2 {\n" 652 " int X;\n" 653 "} A2_t;", 654 AllowSimpleBracedStatements); 655 verifyFormat("template <int> struct A2 {\n" 656 " struct B {};\n" 657 "};", 658 AllowSimpleBracedStatements); 659 660 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 661 FormatStyle::SIS_Never; 662 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 663 verifyFormat("if (true) {\n" 664 " f();\n" 665 "}", 666 AllowSimpleBracedStatements); 667 verifyFormat("if (true) {\n" 668 " f();\n" 669 "} else {\n" 670 " f();\n" 671 "}", 672 AllowSimpleBracedStatements); 673 674 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 675 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 676 verifyFormat("while (true) {\n" 677 " f();\n" 678 "}", 679 AllowSimpleBracedStatements); 680 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 681 verifyFormat("for (;;) {\n" 682 " f();\n" 683 "}", 684 AllowSimpleBracedStatements); 685 686 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 687 FormatStyle::SIS_WithoutElse; 688 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 689 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = 690 FormatStyle::BWACS_Always; 691 692 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 693 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 694 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 695 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 696 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 697 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 698 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 699 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 700 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 701 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 702 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 703 AllowSimpleBracedStatements); 704 verifyFormat("if (true)\n" 705 "{\n" 706 " ffffffffffffffffffffffff();\n" 707 "}", 708 AllowSimpleBracedStatements); 709 verifyFormat("if (true)\n" 710 "{\n" 711 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 712 "}", 713 AllowSimpleBracedStatements); 714 verifyFormat("if (true)\n" 715 "{ //\n" 716 " f();\n" 717 "}", 718 AllowSimpleBracedStatements); 719 verifyFormat("if (true)\n" 720 "{\n" 721 " f();\n" 722 " f();\n" 723 "}", 724 AllowSimpleBracedStatements); 725 verifyFormat("if (true)\n" 726 "{\n" 727 " f();\n" 728 "} else\n" 729 "{\n" 730 " f();\n" 731 "}", 732 AllowSimpleBracedStatements); 733 734 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 735 FormatStyle::SIS_Never; 736 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 737 verifyFormat("if (true)\n" 738 "{\n" 739 " f();\n" 740 "}", 741 AllowSimpleBracedStatements); 742 verifyFormat("if (true)\n" 743 "{\n" 744 " f();\n" 745 "} else\n" 746 "{\n" 747 " f();\n" 748 "}", 749 AllowSimpleBracedStatements); 750 751 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 752 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 753 verifyFormat("while (true)\n" 754 "{\n" 755 " f();\n" 756 "}", 757 AllowSimpleBracedStatements); 758 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 759 verifyFormat("for (;;)\n" 760 "{\n" 761 " f();\n" 762 "}", 763 AllowSimpleBracedStatements); 764 } 765 766 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 767 FormatStyle Style = getLLVMStyleWithColumns(60); 768 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 769 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 770 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 771 EXPECT_EQ("#define A \\\n" 772 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 773 " { \\\n" 774 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 775 " }\n" 776 "X;", 777 format("#define A \\\n" 778 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 779 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 780 " }\n" 781 "X;", 782 Style)); 783 } 784 785 TEST_F(FormatTest, ParseIfElse) { 786 verifyFormat("if (true)\n" 787 " if (true)\n" 788 " if (true)\n" 789 " f();\n" 790 " else\n" 791 " g();\n" 792 " else\n" 793 " h();\n" 794 "else\n" 795 " i();"); 796 verifyFormat("if (true)\n" 797 " if (true)\n" 798 " if (true) {\n" 799 " if (true)\n" 800 " f();\n" 801 " } else {\n" 802 " g();\n" 803 " }\n" 804 " else\n" 805 " h();\n" 806 "else {\n" 807 " i();\n" 808 "}"); 809 verifyFormat("if (true)\n" 810 " if constexpr (true)\n" 811 " if (true) {\n" 812 " if constexpr (true)\n" 813 " f();\n" 814 " } else {\n" 815 " g();\n" 816 " }\n" 817 " else\n" 818 " h();\n" 819 "else {\n" 820 " i();\n" 821 "}"); 822 verifyFormat("if (true)\n" 823 " if CONSTEXPR (true)\n" 824 " if (true) {\n" 825 " if CONSTEXPR (true)\n" 826 " f();\n" 827 " } else {\n" 828 " g();\n" 829 " }\n" 830 " else\n" 831 " h();\n" 832 "else {\n" 833 " i();\n" 834 "}"); 835 verifyFormat("void f() {\n" 836 " if (a) {\n" 837 " } else {\n" 838 " }\n" 839 "}"); 840 } 841 842 TEST_F(FormatTest, ElseIf) { 843 verifyFormat("if (a) {\n} else if (b) {\n}"); 844 verifyFormat("if (a)\n" 845 " f();\n" 846 "else if (b)\n" 847 " g();\n" 848 "else\n" 849 " h();"); 850 verifyFormat("if constexpr (a)\n" 851 " f();\n" 852 "else if constexpr (b)\n" 853 " g();\n" 854 "else\n" 855 " h();"); 856 verifyFormat("if CONSTEXPR (a)\n" 857 " f();\n" 858 "else if CONSTEXPR (b)\n" 859 " g();\n" 860 "else\n" 861 " h();"); 862 verifyFormat("if (a) {\n" 863 " f();\n" 864 "}\n" 865 "// or else ..\n" 866 "else {\n" 867 " g()\n" 868 "}"); 869 870 verifyFormat("if (a) {\n" 871 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 872 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 873 "}"); 874 verifyFormat("if (a) {\n" 875 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 876 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 877 "}"); 878 verifyFormat("if (a) {\n" 879 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 880 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 881 "}"); 882 verifyFormat("if (a) {\n" 883 "} else if (\n" 884 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 885 "}", 886 getLLVMStyleWithColumns(62)); 887 verifyFormat("if (a) {\n" 888 "} else if constexpr (\n" 889 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 890 "}", 891 getLLVMStyleWithColumns(62)); 892 verifyFormat("if (a) {\n" 893 "} else if CONSTEXPR (\n" 894 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 895 "}", 896 getLLVMStyleWithColumns(62)); 897 } 898 899 TEST_F(FormatTest, FormatsForLoop) { 900 verifyFormat( 901 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 902 " ++VeryVeryLongLoopVariable)\n" 903 " ;"); 904 verifyFormat("for (;;)\n" 905 " f();"); 906 verifyFormat("for (;;) {\n}"); 907 verifyFormat("for (;;) {\n" 908 " f();\n" 909 "}"); 910 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 911 912 verifyFormat( 913 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 914 " E = UnwrappedLines.end();\n" 915 " I != E; ++I) {\n}"); 916 917 verifyFormat( 918 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 919 " ++IIIII) {\n}"); 920 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 921 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 922 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 923 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 924 " I = FD->getDeclsInPrototypeScope().begin(),\n" 925 " E = FD->getDeclsInPrototypeScope().end();\n" 926 " I != E; ++I) {\n}"); 927 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 928 " I = Container.begin(),\n" 929 " E = Container.end();\n" 930 " I != E; ++I) {\n}", 931 getLLVMStyleWithColumns(76)); 932 933 verifyFormat( 934 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 935 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 936 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 938 " ++aaaaaaaaaaa) {\n}"); 939 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 940 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 941 " ++i) {\n}"); 942 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 943 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 944 "}"); 945 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 946 " aaaaaaaaaa);\n" 947 " iter; ++iter) {\n" 948 "}"); 949 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 950 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 951 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 952 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 953 954 // These should not be formatted as Objective-C for-in loops. 955 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 956 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 957 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 958 verifyFormat( 959 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 960 961 FormatStyle NoBinPacking = getLLVMStyle(); 962 NoBinPacking.BinPackParameters = false; 963 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 964 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 965 " aaaaaaaaaaaaaaaa,\n" 966 " aaaaaaaaaaaaaaaa,\n" 967 " aaaaaaaaaaaaaaaa);\n" 968 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 969 "}", 970 NoBinPacking); 971 verifyFormat( 972 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 973 " E = UnwrappedLines.end();\n" 974 " I != E;\n" 975 " ++I) {\n}", 976 NoBinPacking); 977 978 FormatStyle AlignLeft = getLLVMStyle(); 979 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 980 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 981 } 982 983 TEST_F(FormatTest, RangeBasedForLoops) { 984 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 985 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 986 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 987 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 988 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 989 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 990 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 991 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 992 } 993 994 TEST_F(FormatTest, ForEachLoops) { 995 verifyFormat("void f() {\n" 996 " foreach (Item *item, itemlist) {}\n" 997 " Q_FOREACH (Item *item, itemlist) {}\n" 998 " BOOST_FOREACH (Item *item, itemlist) {}\n" 999 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 1000 "}"); 1001 1002 FormatStyle Style = getLLVMStyle(); 1003 Style.SpaceBeforeParens = 1004 FormatStyle::SBPO_ControlStatementsExceptForEachMacros; 1005 verifyFormat("void f() {\n" 1006 " foreach(Item *item, itemlist) {}\n" 1007 " Q_FOREACH(Item *item, itemlist) {}\n" 1008 " BOOST_FOREACH(Item *item, itemlist) {}\n" 1009 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 1010 "}", 1011 Style); 1012 1013 // As function-like macros. 1014 verifyFormat("#define foreach(x, y)\n" 1015 "#define Q_FOREACH(x, y)\n" 1016 "#define BOOST_FOREACH(x, y)\n" 1017 "#define UNKNOWN_FOREACH(x, y)\n"); 1018 1019 // Not as function-like macros. 1020 verifyFormat("#define foreach (x, y)\n" 1021 "#define Q_FOREACH (x, y)\n" 1022 "#define BOOST_FOREACH (x, y)\n" 1023 "#define UNKNOWN_FOREACH (x, y)\n"); 1024 1025 // handle microsoft non standard extension 1026 verifyFormat("for each (char c in x->MyStringProperty)"); 1027 } 1028 1029 TEST_F(FormatTest, FormatsWhileLoop) { 1030 verifyFormat("while (true) {\n}"); 1031 verifyFormat("while (true)\n" 1032 " f();"); 1033 verifyFormat("while () {\n}"); 1034 verifyFormat("while () {\n" 1035 " f();\n" 1036 "}"); 1037 } 1038 1039 TEST_F(FormatTest, FormatsDoWhile) { 1040 verifyFormat("do {\n" 1041 " do_something();\n" 1042 "} while (something());"); 1043 verifyFormat("do\n" 1044 " do_something();\n" 1045 "while (something());"); 1046 } 1047 1048 TEST_F(FormatTest, FormatsSwitchStatement) { 1049 verifyFormat("switch (x) {\n" 1050 "case 1:\n" 1051 " f();\n" 1052 " break;\n" 1053 "case kFoo:\n" 1054 "case ns::kBar:\n" 1055 "case kBaz:\n" 1056 " break;\n" 1057 "default:\n" 1058 " g();\n" 1059 " break;\n" 1060 "}"); 1061 verifyFormat("switch (x) {\n" 1062 "case 1: {\n" 1063 " f();\n" 1064 " break;\n" 1065 "}\n" 1066 "case 2: {\n" 1067 " break;\n" 1068 "}\n" 1069 "}"); 1070 verifyFormat("switch (x) {\n" 1071 "case 1: {\n" 1072 " f();\n" 1073 " {\n" 1074 " g();\n" 1075 " h();\n" 1076 " }\n" 1077 " break;\n" 1078 "}\n" 1079 "}"); 1080 verifyFormat("switch (x) {\n" 1081 "case 1: {\n" 1082 " f();\n" 1083 " if (foo) {\n" 1084 " g();\n" 1085 " h();\n" 1086 " }\n" 1087 " break;\n" 1088 "}\n" 1089 "}"); 1090 verifyFormat("switch (x) {\n" 1091 "case 1: {\n" 1092 " f();\n" 1093 " g();\n" 1094 "} break;\n" 1095 "}"); 1096 verifyFormat("switch (test)\n" 1097 " ;"); 1098 verifyFormat("switch (x) {\n" 1099 "default: {\n" 1100 " // Do nothing.\n" 1101 "}\n" 1102 "}"); 1103 verifyFormat("switch (x) {\n" 1104 "// comment\n" 1105 "// if 1, do f()\n" 1106 "case 1:\n" 1107 " f();\n" 1108 "}"); 1109 verifyFormat("switch (x) {\n" 1110 "case 1:\n" 1111 " // Do amazing stuff\n" 1112 " {\n" 1113 " f();\n" 1114 " g();\n" 1115 " }\n" 1116 " break;\n" 1117 "}"); 1118 verifyFormat("#define A \\\n" 1119 " switch (x) { \\\n" 1120 " case a: \\\n" 1121 " foo = b; \\\n" 1122 " }", 1123 getLLVMStyleWithColumns(20)); 1124 verifyFormat("#define OPERATION_CASE(name) \\\n" 1125 " case OP_name: \\\n" 1126 " return operations::Operation##name\n", 1127 getLLVMStyleWithColumns(40)); 1128 verifyFormat("switch (x) {\n" 1129 "case 1:;\n" 1130 "default:;\n" 1131 " int i;\n" 1132 "}"); 1133 1134 verifyGoogleFormat("switch (x) {\n" 1135 " case 1:\n" 1136 " f();\n" 1137 " break;\n" 1138 " case kFoo:\n" 1139 " case ns::kBar:\n" 1140 " case kBaz:\n" 1141 " break;\n" 1142 " default:\n" 1143 " g();\n" 1144 " break;\n" 1145 "}"); 1146 verifyGoogleFormat("switch (x) {\n" 1147 " case 1: {\n" 1148 " f();\n" 1149 " break;\n" 1150 " }\n" 1151 "}"); 1152 verifyGoogleFormat("switch (test)\n" 1153 " ;"); 1154 1155 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 1156 " case OP_name: \\\n" 1157 " return operations::Operation##name\n"); 1158 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 1159 " // Get the correction operation class.\n" 1160 " switch (OpCode) {\n" 1161 " CASE(Add);\n" 1162 " CASE(Subtract);\n" 1163 " default:\n" 1164 " return operations::Unknown;\n" 1165 " }\n" 1166 "#undef OPERATION_CASE\n" 1167 "}"); 1168 verifyFormat("DEBUG({\n" 1169 " switch (x) {\n" 1170 " case A:\n" 1171 " f();\n" 1172 " break;\n" 1173 " // fallthrough\n" 1174 " case B:\n" 1175 " g();\n" 1176 " break;\n" 1177 " }\n" 1178 "});"); 1179 EXPECT_EQ("DEBUG({\n" 1180 " switch (x) {\n" 1181 " case A:\n" 1182 " f();\n" 1183 " break;\n" 1184 " // On B:\n" 1185 " case B:\n" 1186 " g();\n" 1187 " break;\n" 1188 " }\n" 1189 "});", 1190 format("DEBUG({\n" 1191 " switch (x) {\n" 1192 " case A:\n" 1193 " f();\n" 1194 " break;\n" 1195 " // On B:\n" 1196 " case B:\n" 1197 " g();\n" 1198 " break;\n" 1199 " }\n" 1200 "});", 1201 getLLVMStyle())); 1202 EXPECT_EQ("switch (n) {\n" 1203 "case 0: {\n" 1204 " return false;\n" 1205 "}\n" 1206 "default: {\n" 1207 " return true;\n" 1208 "}\n" 1209 "}", 1210 format("switch (n)\n" 1211 "{\n" 1212 "case 0: {\n" 1213 " return false;\n" 1214 "}\n" 1215 "default: {\n" 1216 " return true;\n" 1217 "}\n" 1218 "}", 1219 getLLVMStyle())); 1220 verifyFormat("switch (a) {\n" 1221 "case (b):\n" 1222 " return;\n" 1223 "}"); 1224 1225 verifyFormat("switch (a) {\n" 1226 "case some_namespace::\n" 1227 " some_constant:\n" 1228 " return;\n" 1229 "}", 1230 getLLVMStyleWithColumns(34)); 1231 1232 FormatStyle Style = getLLVMStyle(); 1233 Style.IndentCaseLabels = true; 1234 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 1235 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1236 Style.BraceWrapping.AfterCaseLabel = true; 1237 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 1238 EXPECT_EQ("switch (n)\n" 1239 "{\n" 1240 " case 0:\n" 1241 " {\n" 1242 " return false;\n" 1243 " }\n" 1244 " default:\n" 1245 " {\n" 1246 " return true;\n" 1247 " }\n" 1248 "}", 1249 format("switch (n) {\n" 1250 " case 0: {\n" 1251 " return false;\n" 1252 " }\n" 1253 " default: {\n" 1254 " return true;\n" 1255 " }\n" 1256 "}", 1257 Style)); 1258 Style.BraceWrapping.AfterCaseLabel = false; 1259 EXPECT_EQ("switch (n)\n" 1260 "{\n" 1261 " case 0: {\n" 1262 " return false;\n" 1263 " }\n" 1264 " default: {\n" 1265 " return true;\n" 1266 " }\n" 1267 "}", 1268 format("switch (n) {\n" 1269 " case 0:\n" 1270 " {\n" 1271 " return false;\n" 1272 " }\n" 1273 " default:\n" 1274 " {\n" 1275 " return true;\n" 1276 " }\n" 1277 "}", 1278 Style)); 1279 Style.IndentCaseLabels = false; 1280 Style.IndentCaseBlocks = true; 1281 EXPECT_EQ("switch (n)\n" 1282 "{\n" 1283 "case 0:\n" 1284 " {\n" 1285 " return false;\n" 1286 " }\n" 1287 "case 1:\n" 1288 " break;\n" 1289 "default:\n" 1290 " {\n" 1291 " return true;\n" 1292 " }\n" 1293 "}", 1294 format("switch (n) {\n" 1295 "case 0: {\n" 1296 " return false;\n" 1297 "}\n" 1298 "case 1:\n" 1299 " break;\n" 1300 "default: {\n" 1301 " return true;\n" 1302 "}\n" 1303 "}", 1304 Style)); 1305 Style.IndentCaseLabels = true; 1306 Style.IndentCaseBlocks = true; 1307 EXPECT_EQ("switch (n)\n" 1308 "{\n" 1309 " case 0:\n" 1310 " {\n" 1311 " return false;\n" 1312 " }\n" 1313 " case 1:\n" 1314 " break;\n" 1315 " default:\n" 1316 " {\n" 1317 " return true;\n" 1318 " }\n" 1319 "}", 1320 format("switch (n) {\n" 1321 "case 0: {\n" 1322 " return false;\n" 1323 "}\n" 1324 "case 1:\n" 1325 " break;\n" 1326 "default: {\n" 1327 " return true;\n" 1328 "}\n" 1329 "}", 1330 Style)); 1331 } 1332 1333 TEST_F(FormatTest, CaseRanges) { 1334 verifyFormat("switch (x) {\n" 1335 "case 'A' ... 'Z':\n" 1336 "case 1 ... 5:\n" 1337 "case a ... b:\n" 1338 " break;\n" 1339 "}"); 1340 } 1341 1342 TEST_F(FormatTest, ShortEnums) { 1343 FormatStyle Style = getLLVMStyle(); 1344 Style.AllowShortEnumsOnASingleLine = true; 1345 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 1346 Style.AllowShortEnumsOnASingleLine = false; 1347 verifyFormat("enum\n" 1348 "{\n" 1349 " A,\n" 1350 " B,\n" 1351 " C\n" 1352 "} ShortEnum1, ShortEnum2;", 1353 Style); 1354 } 1355 1356 TEST_F(FormatTest, ShortCaseLabels) { 1357 FormatStyle Style = getLLVMStyle(); 1358 Style.AllowShortCaseLabelsOnASingleLine = true; 1359 verifyFormat("switch (a) {\n" 1360 "case 1: x = 1; break;\n" 1361 "case 2: return;\n" 1362 "case 3:\n" 1363 "case 4:\n" 1364 "case 5: return;\n" 1365 "case 6: // comment\n" 1366 " return;\n" 1367 "case 7:\n" 1368 " // comment\n" 1369 " return;\n" 1370 "case 8:\n" 1371 " x = 8; // comment\n" 1372 " break;\n" 1373 "default: y = 1; break;\n" 1374 "}", 1375 Style); 1376 verifyFormat("switch (a) {\n" 1377 "case 0: return; // comment\n" 1378 "case 1: break; // comment\n" 1379 "case 2: return;\n" 1380 "// comment\n" 1381 "case 3: return;\n" 1382 "// comment 1\n" 1383 "// comment 2\n" 1384 "// comment 3\n" 1385 "case 4: break; /* comment */\n" 1386 "case 5:\n" 1387 " // comment\n" 1388 " break;\n" 1389 "case 6: /* comment */ x = 1; break;\n" 1390 "case 7: x = /* comment */ 1; break;\n" 1391 "case 8:\n" 1392 " x = 1; /* comment */\n" 1393 " break;\n" 1394 "case 9:\n" 1395 " break; // comment line 1\n" 1396 " // comment line 2\n" 1397 "}", 1398 Style); 1399 EXPECT_EQ("switch (a) {\n" 1400 "case 1:\n" 1401 " x = 8;\n" 1402 " // fall through\n" 1403 "case 2: x = 8;\n" 1404 "// comment\n" 1405 "case 3:\n" 1406 " return; /* comment line 1\n" 1407 " * comment line 2 */\n" 1408 "case 4: i = 8;\n" 1409 "// something else\n" 1410 "#if FOO\n" 1411 "case 5: break;\n" 1412 "#endif\n" 1413 "}", 1414 format("switch (a) {\n" 1415 "case 1: x = 8;\n" 1416 " // fall through\n" 1417 "case 2:\n" 1418 " x = 8;\n" 1419 "// comment\n" 1420 "case 3:\n" 1421 " return; /* comment line 1\n" 1422 " * comment line 2 */\n" 1423 "case 4:\n" 1424 " i = 8;\n" 1425 "// something else\n" 1426 "#if FOO\n" 1427 "case 5: break;\n" 1428 "#endif\n" 1429 "}", 1430 Style)); 1431 EXPECT_EQ("switch (a) {\n" 1432 "case 0:\n" 1433 " return; // long long long long long long long long long long " 1434 "long long comment\n" 1435 " // line\n" 1436 "}", 1437 format("switch (a) {\n" 1438 "case 0: return; // long long long long long long long long " 1439 "long long long long comment line\n" 1440 "}", 1441 Style)); 1442 EXPECT_EQ("switch (a) {\n" 1443 "case 0:\n" 1444 " return; /* long long long long long long long long long long " 1445 "long long comment\n" 1446 " line */\n" 1447 "}", 1448 format("switch (a) {\n" 1449 "case 0: return; /* long long long long long long long long " 1450 "long long long long comment line */\n" 1451 "}", 1452 Style)); 1453 verifyFormat("switch (a) {\n" 1454 "#if FOO\n" 1455 "case 0: return 0;\n" 1456 "#endif\n" 1457 "}", 1458 Style); 1459 verifyFormat("switch (a) {\n" 1460 "case 1: {\n" 1461 "}\n" 1462 "case 2: {\n" 1463 " return;\n" 1464 "}\n" 1465 "case 3: {\n" 1466 " x = 1;\n" 1467 " return;\n" 1468 "}\n" 1469 "case 4:\n" 1470 " if (x)\n" 1471 " return;\n" 1472 "}", 1473 Style); 1474 Style.ColumnLimit = 21; 1475 verifyFormat("switch (a) {\n" 1476 "case 1: x = 1; break;\n" 1477 "case 2: return;\n" 1478 "case 3:\n" 1479 "case 4:\n" 1480 "case 5: return;\n" 1481 "default:\n" 1482 " y = 1;\n" 1483 " break;\n" 1484 "}", 1485 Style); 1486 Style.ColumnLimit = 80; 1487 Style.AllowShortCaseLabelsOnASingleLine = false; 1488 Style.IndentCaseLabels = true; 1489 EXPECT_EQ("switch (n) {\n" 1490 " default /*comments*/:\n" 1491 " return true;\n" 1492 " case 0:\n" 1493 " return false;\n" 1494 "}", 1495 format("switch (n) {\n" 1496 "default/*comments*/:\n" 1497 " return true;\n" 1498 "case 0:\n" 1499 " return false;\n" 1500 "}", 1501 Style)); 1502 Style.AllowShortCaseLabelsOnASingleLine = true; 1503 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1504 Style.BraceWrapping.AfterCaseLabel = true; 1505 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 1506 EXPECT_EQ("switch (n)\n" 1507 "{\n" 1508 " case 0:\n" 1509 " {\n" 1510 " return false;\n" 1511 " }\n" 1512 " default:\n" 1513 " {\n" 1514 " return true;\n" 1515 " }\n" 1516 "}", 1517 format("switch (n) {\n" 1518 " case 0: {\n" 1519 " return false;\n" 1520 " }\n" 1521 " default:\n" 1522 " {\n" 1523 " return true;\n" 1524 " }\n" 1525 "}", 1526 Style)); 1527 } 1528 1529 TEST_F(FormatTest, FormatsLabels) { 1530 verifyFormat("void f() {\n" 1531 " some_code();\n" 1532 "test_label:\n" 1533 " some_other_code();\n" 1534 " {\n" 1535 " some_more_code();\n" 1536 " another_label:\n" 1537 " some_more_code();\n" 1538 " }\n" 1539 "}"); 1540 verifyFormat("{\n" 1541 " some_code();\n" 1542 "test_label:\n" 1543 " some_other_code();\n" 1544 "}"); 1545 verifyFormat("{\n" 1546 " some_code();\n" 1547 "test_label:;\n" 1548 " int i = 0;\n" 1549 "}"); 1550 FormatStyle Style = getLLVMStyle(); 1551 Style.IndentGotoLabels = false; 1552 verifyFormat("void f() {\n" 1553 " some_code();\n" 1554 "test_label:\n" 1555 " some_other_code();\n" 1556 " {\n" 1557 " some_more_code();\n" 1558 "another_label:\n" 1559 " some_more_code();\n" 1560 " }\n" 1561 "}", 1562 Style); 1563 verifyFormat("{\n" 1564 " some_code();\n" 1565 "test_label:\n" 1566 " some_other_code();\n" 1567 "}", 1568 Style); 1569 verifyFormat("{\n" 1570 " some_code();\n" 1571 "test_label:;\n" 1572 " int i = 0;\n" 1573 "}"); 1574 } 1575 1576 TEST_F(FormatTest, MultiLineControlStatements) { 1577 FormatStyle Style = getLLVMStyle(); 1578 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 1579 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 1580 Style.ColumnLimit = 20; 1581 // Short lines should keep opening brace on same line. 1582 EXPECT_EQ("if (foo) {\n" 1583 " bar();\n" 1584 "}", 1585 format("if(foo){bar();}", Style)); 1586 EXPECT_EQ("if (foo) {\n" 1587 " bar();\n" 1588 "} else {\n" 1589 " baz();\n" 1590 "}", 1591 format("if(foo){bar();}else{baz();}", Style)); 1592 EXPECT_EQ("if (foo && bar) {\n" 1593 " baz();\n" 1594 "}", 1595 format("if(foo&&bar){baz();}", Style)); 1596 EXPECT_EQ("if (foo) {\n" 1597 " bar();\n" 1598 "} else if (baz) {\n" 1599 " quux();\n" 1600 "}", 1601 format("if(foo){bar();}else if(baz){quux();}", Style)); 1602 EXPECT_EQ( 1603 "if (foo) {\n" 1604 " bar();\n" 1605 "} else if (baz) {\n" 1606 " quux();\n" 1607 "} else {\n" 1608 " foobar();\n" 1609 "}", 1610 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 1611 EXPECT_EQ("for (;;) {\n" 1612 " foo();\n" 1613 "}", 1614 format("for(;;){foo();}")); 1615 EXPECT_EQ("while (1) {\n" 1616 " foo();\n" 1617 "}", 1618 format("while(1){foo();}", Style)); 1619 EXPECT_EQ("switch (foo) {\n" 1620 "case bar:\n" 1621 " return;\n" 1622 "}", 1623 format("switch(foo){case bar:return;}", Style)); 1624 EXPECT_EQ("try {\n" 1625 " foo();\n" 1626 "} catch (...) {\n" 1627 " bar();\n" 1628 "}", 1629 format("try{foo();}catch(...){bar();}", Style)); 1630 EXPECT_EQ("do {\n" 1631 " foo();\n" 1632 "} while (bar &&\n" 1633 " baz);", 1634 format("do{foo();}while(bar&&baz);", Style)); 1635 // Long lines should put opening brace on new line. 1636 EXPECT_EQ("if (foo && bar &&\n" 1637 " baz)\n" 1638 "{\n" 1639 " quux();\n" 1640 "}", 1641 format("if(foo&&bar&&baz){quux();}", Style)); 1642 EXPECT_EQ("if (foo && bar &&\n" 1643 " baz)\n" 1644 "{\n" 1645 " quux();\n" 1646 "}", 1647 format("if (foo && bar &&\n" 1648 " baz) {\n" 1649 " quux();\n" 1650 "}", 1651 Style)); 1652 EXPECT_EQ("if (foo) {\n" 1653 " bar();\n" 1654 "} else if (baz ||\n" 1655 " quux)\n" 1656 "{\n" 1657 " foobar();\n" 1658 "}", 1659 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 1660 EXPECT_EQ( 1661 "if (foo) {\n" 1662 " bar();\n" 1663 "} else if (baz ||\n" 1664 " quux)\n" 1665 "{\n" 1666 " foobar();\n" 1667 "} else {\n" 1668 " barbaz();\n" 1669 "}", 1670 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 1671 Style)); 1672 EXPECT_EQ("for (int i = 0;\n" 1673 " i < 10; ++i)\n" 1674 "{\n" 1675 " foo();\n" 1676 "}", 1677 format("for(int i=0;i<10;++i){foo();}", Style)); 1678 EXPECT_EQ("foreach (int i,\n" 1679 " list)\n" 1680 "{\n" 1681 " foo();\n" 1682 "}", 1683 format("foreach(int i, list){foo();}", Style)); 1684 Style.ColumnLimit = 1685 40; // to concentrate at brace wrapping, not line wrap due to column limit 1686 EXPECT_EQ("foreach (int i, list) {\n" 1687 " foo();\n" 1688 "}", 1689 format("foreach(int i, list){foo();}", Style)); 1690 Style.ColumnLimit = 1691 20; // to concentrate at brace wrapping, not line wrap due to column limit 1692 EXPECT_EQ("while (foo || bar ||\n" 1693 " baz)\n" 1694 "{\n" 1695 " quux();\n" 1696 "}", 1697 format("while(foo||bar||baz){quux();}", Style)); 1698 EXPECT_EQ("switch (\n" 1699 " foo = barbaz)\n" 1700 "{\n" 1701 "case quux:\n" 1702 " return;\n" 1703 "}", 1704 format("switch(foo=barbaz){case quux:return;}", Style)); 1705 EXPECT_EQ("try {\n" 1706 " foo();\n" 1707 "} catch (\n" 1708 " Exception &bar)\n" 1709 "{\n" 1710 " baz();\n" 1711 "}", 1712 format("try{foo();}catch(Exception&bar){baz();}", Style)); 1713 Style.ColumnLimit = 1714 40; // to concentrate at brace wrapping, not line wrap due to column limit 1715 EXPECT_EQ("try {\n" 1716 " foo();\n" 1717 "} catch (Exception &bar) {\n" 1718 " baz();\n" 1719 "}", 1720 format("try{foo();}catch(Exception&bar){baz();}", Style)); 1721 Style.ColumnLimit = 1722 20; // to concentrate at brace wrapping, not line wrap due to column limit 1723 1724 Style.BraceWrapping.BeforeElse = true; 1725 EXPECT_EQ( 1726 "if (foo) {\n" 1727 " bar();\n" 1728 "}\n" 1729 "else if (baz ||\n" 1730 " quux)\n" 1731 "{\n" 1732 " foobar();\n" 1733 "}\n" 1734 "else {\n" 1735 " barbaz();\n" 1736 "}", 1737 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 1738 Style)); 1739 1740 Style.BraceWrapping.BeforeCatch = true; 1741 EXPECT_EQ("try {\n" 1742 " foo();\n" 1743 "}\n" 1744 "catch (...) {\n" 1745 " baz();\n" 1746 "}", 1747 format("try{foo();}catch(...){baz();}", Style)); 1748 } 1749 1750 TEST_F(FormatTest, BeforeWhile) { 1751 FormatStyle Style = getLLVMStyle(); 1752 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 1753 1754 verifyFormat("do {\n" 1755 " foo();\n" 1756 "} while (1);", 1757 Style); 1758 Style.BraceWrapping.BeforeWhile = true; 1759 verifyFormat("do {\n" 1760 " foo();\n" 1761 "}\n" 1762 "while (1);", 1763 Style); 1764 } 1765 1766 //===----------------------------------------------------------------------===// 1767 // Tests for classes, namespaces, etc. 1768 //===----------------------------------------------------------------------===// 1769 1770 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1771 verifyFormat("class A {};"); 1772 } 1773 1774 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1775 verifyFormat("class A {\n" 1776 "public:\n" 1777 "public: // comment\n" 1778 "protected:\n" 1779 "private:\n" 1780 " void f() {}\n" 1781 "};"); 1782 verifyFormat("export class A {\n" 1783 "public:\n" 1784 "public: // comment\n" 1785 "protected:\n" 1786 "private:\n" 1787 " void f() {}\n" 1788 "};"); 1789 verifyGoogleFormat("class A {\n" 1790 " public:\n" 1791 " protected:\n" 1792 " private:\n" 1793 " void f() {}\n" 1794 "};"); 1795 verifyGoogleFormat("export class A {\n" 1796 " public:\n" 1797 " protected:\n" 1798 " private:\n" 1799 " void f() {}\n" 1800 "};"); 1801 verifyFormat("class A {\n" 1802 "public slots:\n" 1803 " void f1() {}\n" 1804 "public Q_SLOTS:\n" 1805 " void f2() {}\n" 1806 "protected slots:\n" 1807 " void f3() {}\n" 1808 "protected Q_SLOTS:\n" 1809 " void f4() {}\n" 1810 "private slots:\n" 1811 " void f5() {}\n" 1812 "private Q_SLOTS:\n" 1813 " void f6() {}\n" 1814 "signals:\n" 1815 " void g1();\n" 1816 "Q_SIGNALS:\n" 1817 " void g2();\n" 1818 "};"); 1819 1820 // Don't interpret 'signals' the wrong way. 1821 verifyFormat("signals.set();"); 1822 verifyFormat("for (Signals signals : f()) {\n}"); 1823 verifyFormat("{\n" 1824 " signals.set(); // This needs indentation.\n" 1825 "}"); 1826 verifyFormat("void f() {\n" 1827 "label:\n" 1828 " signals.baz();\n" 1829 "}"); 1830 } 1831 1832 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1833 EXPECT_EQ("class A {\n" 1834 "public:\n" 1835 " void f();\n" 1836 "\n" 1837 "private:\n" 1838 " void g() {}\n" 1839 " // test\n" 1840 "protected:\n" 1841 " int h;\n" 1842 "};", 1843 format("class A {\n" 1844 "public:\n" 1845 "void f();\n" 1846 "private:\n" 1847 "void g() {}\n" 1848 "// test\n" 1849 "protected:\n" 1850 "int h;\n" 1851 "};")); 1852 EXPECT_EQ("class A {\n" 1853 "protected:\n" 1854 "public:\n" 1855 " void f();\n" 1856 "};", 1857 format("class A {\n" 1858 "protected:\n" 1859 "\n" 1860 "public:\n" 1861 "\n" 1862 " void f();\n" 1863 "};")); 1864 1865 // Even ensure proper spacing inside macros. 1866 EXPECT_EQ("#define B \\\n" 1867 " class A { \\\n" 1868 " protected: \\\n" 1869 " public: \\\n" 1870 " void f(); \\\n" 1871 " };", 1872 format("#define B \\\n" 1873 " class A { \\\n" 1874 " protected: \\\n" 1875 " \\\n" 1876 " public: \\\n" 1877 " \\\n" 1878 " void f(); \\\n" 1879 " };", 1880 getGoogleStyle())); 1881 // But don't remove empty lines after macros ending in access specifiers. 1882 EXPECT_EQ("#define A private:\n" 1883 "\n" 1884 "int i;", 1885 format("#define A private:\n" 1886 "\n" 1887 "int i;")); 1888 } 1889 1890 TEST_F(FormatTest, FormatsClasses) { 1891 verifyFormat("class A : public B {};"); 1892 verifyFormat("class A : public ::B {};"); 1893 1894 verifyFormat( 1895 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1896 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1897 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1898 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1899 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1900 verifyFormat( 1901 "class A : public B, public C, public D, public E, public F {};"); 1902 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1903 " public C,\n" 1904 " public D,\n" 1905 " public E,\n" 1906 " public F,\n" 1907 " public G {};"); 1908 1909 verifyFormat("class\n" 1910 " ReallyReallyLongClassName {\n" 1911 " int i;\n" 1912 "};", 1913 getLLVMStyleWithColumns(32)); 1914 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1915 " aaaaaaaaaaaaaaaa> {};"); 1916 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1917 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1918 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1919 verifyFormat("template <class R, class C>\n" 1920 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1921 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1922 verifyFormat("class ::A::B {};"); 1923 } 1924 1925 TEST_F(FormatTest, BreakInheritanceStyle) { 1926 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 1927 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 1928 FormatStyle::BILS_BeforeComma; 1929 verifyFormat("class MyClass : public X {};", 1930 StyleWithInheritanceBreakBeforeComma); 1931 verifyFormat("class MyClass\n" 1932 " : public X\n" 1933 " , public Y {};", 1934 StyleWithInheritanceBreakBeforeComma); 1935 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 1936 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 1937 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 1938 StyleWithInheritanceBreakBeforeComma); 1939 verifyFormat("struct aaaaaaaaaaaaa\n" 1940 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 1941 " aaaaaaaaaaaaaaaa> {};", 1942 StyleWithInheritanceBreakBeforeComma); 1943 1944 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 1945 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 1946 FormatStyle::BILS_AfterColon; 1947 verifyFormat("class MyClass : public X {};", 1948 StyleWithInheritanceBreakAfterColon); 1949 verifyFormat("class MyClass : public X, public Y {};", 1950 StyleWithInheritanceBreakAfterColon); 1951 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 1952 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1953 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 1954 StyleWithInheritanceBreakAfterColon); 1955 verifyFormat("struct aaaaaaaaaaaaa :\n" 1956 " public aaaaaaaaaaaaaaaaaaa< // break\n" 1957 " aaaaaaaaaaaaaaaa> {};", 1958 StyleWithInheritanceBreakAfterColon); 1959 } 1960 1961 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1962 verifyFormat("class A {\n} a, b;"); 1963 verifyFormat("struct A {\n} a, b;"); 1964 verifyFormat("union A {\n} a;"); 1965 } 1966 1967 TEST_F(FormatTest, FormatsEnum) { 1968 verifyFormat("enum {\n" 1969 " Zero,\n" 1970 " One = 1,\n" 1971 " Two = One + 1,\n" 1972 " Three = (One + Two),\n" 1973 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1974 " Five = (One, Two, Three, Four, 5)\n" 1975 "};"); 1976 verifyGoogleFormat("enum {\n" 1977 " Zero,\n" 1978 " One = 1,\n" 1979 " Two = One + 1,\n" 1980 " Three = (One + Two),\n" 1981 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1982 " Five = (One, Two, Three, Four, 5)\n" 1983 "};"); 1984 verifyFormat("enum Enum {};"); 1985 verifyFormat("enum {};"); 1986 verifyFormat("enum X E {} d;"); 1987 verifyFormat("enum __attribute__((...)) E {} d;"); 1988 verifyFormat("enum __declspec__((...)) E {} d;"); 1989 verifyFormat("enum {\n" 1990 " Bar = Foo<int, int>::value\n" 1991 "};", 1992 getLLVMStyleWithColumns(30)); 1993 1994 verifyFormat("enum ShortEnum { A, B, C };"); 1995 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1996 1997 EXPECT_EQ("enum KeepEmptyLines {\n" 1998 " ONE,\n" 1999 "\n" 2000 " TWO,\n" 2001 "\n" 2002 " THREE\n" 2003 "}", 2004 format("enum KeepEmptyLines {\n" 2005 " ONE,\n" 2006 "\n" 2007 " TWO,\n" 2008 "\n" 2009 "\n" 2010 " THREE\n" 2011 "}")); 2012 verifyFormat("enum E { // comment\n" 2013 " ONE,\n" 2014 " TWO\n" 2015 "};\n" 2016 "int i;"); 2017 2018 FormatStyle EightIndent = getLLVMStyle(); 2019 EightIndent.IndentWidth = 8; 2020 verifyFormat("enum {\n" 2021 " VOID,\n" 2022 " CHAR,\n" 2023 " SHORT,\n" 2024 " INT,\n" 2025 " LONG,\n" 2026 " SIGNED,\n" 2027 " UNSIGNED,\n" 2028 " BOOL,\n" 2029 " FLOAT,\n" 2030 " DOUBLE,\n" 2031 " COMPLEX\n" 2032 "};", 2033 EightIndent); 2034 2035 // Not enums. 2036 verifyFormat("enum X f() {\n" 2037 " a();\n" 2038 " return 42;\n" 2039 "}"); 2040 verifyFormat("enum X Type::f() {\n" 2041 " a();\n" 2042 " return 42;\n" 2043 "}"); 2044 verifyFormat("enum ::X f() {\n" 2045 " a();\n" 2046 " return 42;\n" 2047 "}"); 2048 verifyFormat("enum ns::X f() {\n" 2049 " a();\n" 2050 " return 42;\n" 2051 "}"); 2052 } 2053 2054 TEST_F(FormatTest, FormatsEnumsWithErrors) { 2055 verifyFormat("enum Type {\n" 2056 " One = 0; // These semicolons should be commas.\n" 2057 " Two = 1;\n" 2058 "};"); 2059 verifyFormat("namespace n {\n" 2060 "enum Type {\n" 2061 " One,\n" 2062 " Two, // missing };\n" 2063 " int i;\n" 2064 "}\n" 2065 "void g() {}"); 2066 } 2067 2068 TEST_F(FormatTest, FormatsEnumStruct) { 2069 verifyFormat("enum struct {\n" 2070 " Zero,\n" 2071 " One = 1,\n" 2072 " Two = One + 1,\n" 2073 " Three = (One + Two),\n" 2074 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2075 " Five = (One, Two, Three, Four, 5)\n" 2076 "};"); 2077 verifyFormat("enum struct Enum {};"); 2078 verifyFormat("enum struct {};"); 2079 verifyFormat("enum struct X E {} d;"); 2080 verifyFormat("enum struct __attribute__((...)) E {} d;"); 2081 verifyFormat("enum struct __declspec__((...)) E {} d;"); 2082 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 2083 } 2084 2085 TEST_F(FormatTest, FormatsEnumClass) { 2086 verifyFormat("enum class {\n" 2087 " Zero,\n" 2088 " One = 1,\n" 2089 " Two = One + 1,\n" 2090 " Three = (One + Two),\n" 2091 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2092 " Five = (One, Two, Three, Four, 5)\n" 2093 "};"); 2094 verifyFormat("enum class Enum {};"); 2095 verifyFormat("enum class {};"); 2096 verifyFormat("enum class X E {} d;"); 2097 verifyFormat("enum class __attribute__((...)) E {} d;"); 2098 verifyFormat("enum class __declspec__((...)) E {} d;"); 2099 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 2100 } 2101 2102 TEST_F(FormatTest, FormatsEnumTypes) { 2103 verifyFormat("enum X : int {\n" 2104 " A, // Force multiple lines.\n" 2105 " B\n" 2106 "};"); 2107 verifyFormat("enum X : int { A, B };"); 2108 verifyFormat("enum X : std::uint32_t { A, B };"); 2109 } 2110 2111 TEST_F(FormatTest, FormatsTypedefEnum) { 2112 FormatStyle Style = getLLVMStyle(); 2113 Style.ColumnLimit = 40; 2114 verifyFormat("typedef enum {} EmptyEnum;"); 2115 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 2116 verifyFormat("typedef enum {\n" 2117 " ZERO = 0,\n" 2118 " ONE = 1,\n" 2119 " TWO = 2,\n" 2120 " THREE = 3\n" 2121 "} LongEnum;", 2122 Style); 2123 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2124 Style.BraceWrapping.AfterEnum = true; 2125 verifyFormat("typedef enum {} EmptyEnum;"); 2126 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 2127 verifyFormat("typedef enum\n" 2128 "{\n" 2129 " ZERO = 0,\n" 2130 " ONE = 1,\n" 2131 " TWO = 2,\n" 2132 " THREE = 3\n" 2133 "} LongEnum;", 2134 Style); 2135 } 2136 2137 TEST_F(FormatTest, FormatsNSEnums) { 2138 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2139 verifyGoogleFormat( 2140 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2141 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 2142 " // Information about someDecentlyLongValue.\n" 2143 " someDecentlyLongValue,\n" 2144 " // Information about anotherDecentlyLongValue.\n" 2145 " anotherDecentlyLongValue,\n" 2146 " // Information about aThirdDecentlyLongValue.\n" 2147 " aThirdDecentlyLongValue\n" 2148 "};"); 2149 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 2150 " // Information about someDecentlyLongValue.\n" 2151 " someDecentlyLongValue,\n" 2152 " // Information about anotherDecentlyLongValue.\n" 2153 " anotherDecentlyLongValue,\n" 2154 " // Information about aThirdDecentlyLongValue.\n" 2155 " aThirdDecentlyLongValue\n" 2156 "};"); 2157 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 2158 " a = 1,\n" 2159 " b = 2,\n" 2160 " c = 3,\n" 2161 "};"); 2162 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 2163 " a = 1,\n" 2164 " b = 2,\n" 2165 " c = 3,\n" 2166 "};"); 2167 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 2168 " a = 1,\n" 2169 " b = 2,\n" 2170 " c = 3,\n" 2171 "};"); 2172 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 2173 " a = 1,\n" 2174 " b = 2,\n" 2175 " c = 3,\n" 2176 "};"); 2177 } 2178 2179 TEST_F(FormatTest, FormatsBitfields) { 2180 verifyFormat("struct Bitfields {\n" 2181 " unsigned sClass : 8;\n" 2182 " unsigned ValueKind : 2;\n" 2183 "};"); 2184 verifyFormat("struct A {\n" 2185 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 2186 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 2187 "};"); 2188 verifyFormat("struct MyStruct {\n" 2189 " uchar data;\n" 2190 " uchar : 8;\n" 2191 " uchar : 8;\n" 2192 " uchar other;\n" 2193 "};"); 2194 FormatStyle Style = getLLVMStyle(); 2195 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 2196 verifyFormat("struct Bitfields {\n" 2197 " unsigned sClass:8;\n" 2198 " unsigned ValueKind:2;\n" 2199 " uchar other;\n" 2200 "};", 2201 Style); 2202 verifyFormat("struct A {\n" 2203 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 2204 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 2205 "};", 2206 Style); 2207 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 2208 verifyFormat("struct Bitfields {\n" 2209 " unsigned sClass :8;\n" 2210 " unsigned ValueKind :2;\n" 2211 " uchar other;\n" 2212 "};", 2213 Style); 2214 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 2215 verifyFormat("struct Bitfields {\n" 2216 " unsigned sClass: 8;\n" 2217 " unsigned ValueKind: 2;\n" 2218 " uchar other;\n" 2219 "};", 2220 Style); 2221 } 2222 2223 TEST_F(FormatTest, FormatsNamespaces) { 2224 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 2225 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 2226 2227 verifyFormat("namespace some_namespace {\n" 2228 "class A {};\n" 2229 "void f() { f(); }\n" 2230 "}", 2231 LLVMWithNoNamespaceFix); 2232 verifyFormat("namespace N::inline D {\n" 2233 "class A {};\n" 2234 "void f() { f(); }\n" 2235 "}", 2236 LLVMWithNoNamespaceFix); 2237 verifyFormat("namespace N::inline D::E {\n" 2238 "class A {};\n" 2239 "void f() { f(); }\n" 2240 "}", 2241 LLVMWithNoNamespaceFix); 2242 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 2243 "class A {};\n" 2244 "void f() { f(); }\n" 2245 "}", 2246 LLVMWithNoNamespaceFix); 2247 verifyFormat("/* something */ namespace some_namespace {\n" 2248 "class A {};\n" 2249 "void f() { f(); }\n" 2250 "}", 2251 LLVMWithNoNamespaceFix); 2252 verifyFormat("namespace {\n" 2253 "class A {};\n" 2254 "void f() { f(); }\n" 2255 "}", 2256 LLVMWithNoNamespaceFix); 2257 verifyFormat("/* something */ namespace {\n" 2258 "class A {};\n" 2259 "void f() { f(); }\n" 2260 "}", 2261 LLVMWithNoNamespaceFix); 2262 verifyFormat("inline namespace X {\n" 2263 "class A {};\n" 2264 "void f() { f(); }\n" 2265 "}", 2266 LLVMWithNoNamespaceFix); 2267 verifyFormat("/* something */ inline namespace X {\n" 2268 "class A {};\n" 2269 "void f() { f(); }\n" 2270 "}", 2271 LLVMWithNoNamespaceFix); 2272 verifyFormat("export namespace X {\n" 2273 "class A {};\n" 2274 "void f() { f(); }\n" 2275 "}", 2276 LLVMWithNoNamespaceFix); 2277 verifyFormat("using namespace some_namespace;\n" 2278 "class A {};\n" 2279 "void f() { f(); }", 2280 LLVMWithNoNamespaceFix); 2281 2282 // This code is more common than we thought; if we 2283 // layout this correctly the semicolon will go into 2284 // its own line, which is undesirable. 2285 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 2286 verifyFormat("namespace {\n" 2287 "class A {};\n" 2288 "};", 2289 LLVMWithNoNamespaceFix); 2290 2291 verifyFormat("namespace {\n" 2292 "int SomeVariable = 0; // comment\n" 2293 "} // namespace", 2294 LLVMWithNoNamespaceFix); 2295 EXPECT_EQ("#ifndef HEADER_GUARD\n" 2296 "#define HEADER_GUARD\n" 2297 "namespace my_namespace {\n" 2298 "int i;\n" 2299 "} // my_namespace\n" 2300 "#endif // HEADER_GUARD", 2301 format("#ifndef HEADER_GUARD\n" 2302 " #define HEADER_GUARD\n" 2303 " namespace my_namespace {\n" 2304 "int i;\n" 2305 "} // my_namespace\n" 2306 "#endif // HEADER_GUARD", 2307 LLVMWithNoNamespaceFix)); 2308 2309 EXPECT_EQ("namespace A::B {\n" 2310 "class C {};\n" 2311 "}", 2312 format("namespace A::B {\n" 2313 "class C {};\n" 2314 "}", 2315 LLVMWithNoNamespaceFix)); 2316 2317 FormatStyle Style = getLLVMStyle(); 2318 Style.NamespaceIndentation = FormatStyle::NI_All; 2319 EXPECT_EQ("namespace out {\n" 2320 " int i;\n" 2321 " namespace in {\n" 2322 " int i;\n" 2323 " } // namespace in\n" 2324 "} // namespace out", 2325 format("namespace out {\n" 2326 "int i;\n" 2327 "namespace in {\n" 2328 "int i;\n" 2329 "} // namespace in\n" 2330 "} // namespace out", 2331 Style)); 2332 2333 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2334 EXPECT_EQ("namespace out {\n" 2335 "int i;\n" 2336 "namespace in {\n" 2337 " int i;\n" 2338 "} // namespace in\n" 2339 "} // namespace out", 2340 format("namespace out {\n" 2341 "int i;\n" 2342 "namespace in {\n" 2343 "int i;\n" 2344 "} // namespace in\n" 2345 "} // namespace out", 2346 Style)); 2347 } 2348 2349 TEST_F(FormatTest, NamespaceMacros) { 2350 FormatStyle Style = getLLVMStyle(); 2351 Style.NamespaceMacros.push_back("TESTSUITE"); 2352 2353 verifyFormat("TESTSUITE(A) {\n" 2354 "int foo();\n" 2355 "} // TESTSUITE(A)", 2356 Style); 2357 2358 verifyFormat("TESTSUITE(A, B) {\n" 2359 "int foo();\n" 2360 "} // TESTSUITE(A)", 2361 Style); 2362 2363 // Properly indent according to NamespaceIndentation style 2364 Style.NamespaceIndentation = FormatStyle::NI_All; 2365 verifyFormat("TESTSUITE(A) {\n" 2366 " int foo();\n" 2367 "} // TESTSUITE(A)", 2368 Style); 2369 verifyFormat("TESTSUITE(A) {\n" 2370 " namespace B {\n" 2371 " int foo();\n" 2372 " } // namespace B\n" 2373 "} // TESTSUITE(A)", 2374 Style); 2375 verifyFormat("namespace A {\n" 2376 " TESTSUITE(B) {\n" 2377 " int foo();\n" 2378 " } // TESTSUITE(B)\n" 2379 "} // namespace A", 2380 Style); 2381 2382 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2383 verifyFormat("TESTSUITE(A) {\n" 2384 "TESTSUITE(B) {\n" 2385 " int foo();\n" 2386 "} // TESTSUITE(B)\n" 2387 "} // TESTSUITE(A)", 2388 Style); 2389 verifyFormat("TESTSUITE(A) {\n" 2390 "namespace B {\n" 2391 " int foo();\n" 2392 "} // namespace B\n" 2393 "} // TESTSUITE(A)", 2394 Style); 2395 verifyFormat("namespace A {\n" 2396 "TESTSUITE(B) {\n" 2397 " int foo();\n" 2398 "} // TESTSUITE(B)\n" 2399 "} // namespace A", 2400 Style); 2401 2402 // Properly merge namespace-macros blocks in CompactNamespaces mode 2403 Style.NamespaceIndentation = FormatStyle::NI_None; 2404 Style.CompactNamespaces = true; 2405 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 2406 "}} // TESTSUITE(A::B)", 2407 Style); 2408 2409 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 2410 "}} // TESTSUITE(out::in)", 2411 format("TESTSUITE(out) {\n" 2412 "TESTSUITE(in) {\n" 2413 "} // TESTSUITE(in)\n" 2414 "} // TESTSUITE(out)", 2415 Style)); 2416 2417 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 2418 "}} // TESTSUITE(out::in)", 2419 format("TESTSUITE(out) {\n" 2420 "TESTSUITE(in) {\n" 2421 "} // TESTSUITE(in)\n" 2422 "} // TESTSUITE(out)", 2423 Style)); 2424 2425 // Do not merge different namespaces/macros 2426 EXPECT_EQ("namespace out {\n" 2427 "TESTSUITE(in) {\n" 2428 "} // TESTSUITE(in)\n" 2429 "} // namespace out", 2430 format("namespace out {\n" 2431 "TESTSUITE(in) {\n" 2432 "} // TESTSUITE(in)\n" 2433 "} // namespace out", 2434 Style)); 2435 EXPECT_EQ("TESTSUITE(out) {\n" 2436 "namespace in {\n" 2437 "} // namespace in\n" 2438 "} // TESTSUITE(out)", 2439 format("TESTSUITE(out) {\n" 2440 "namespace in {\n" 2441 "} // namespace in\n" 2442 "} // TESTSUITE(out)", 2443 Style)); 2444 Style.NamespaceMacros.push_back("FOOBAR"); 2445 EXPECT_EQ("TESTSUITE(out) {\n" 2446 "FOOBAR(in) {\n" 2447 "} // FOOBAR(in)\n" 2448 "} // TESTSUITE(out)", 2449 format("TESTSUITE(out) {\n" 2450 "FOOBAR(in) {\n" 2451 "} // FOOBAR(in)\n" 2452 "} // TESTSUITE(out)", 2453 Style)); 2454 } 2455 2456 TEST_F(FormatTest, FormatsCompactNamespaces) { 2457 FormatStyle Style = getLLVMStyle(); 2458 Style.CompactNamespaces = true; 2459 Style.NamespaceMacros.push_back("TESTSUITE"); 2460 2461 verifyFormat("namespace A { namespace B {\n" 2462 "}} // namespace A::B", 2463 Style); 2464 2465 EXPECT_EQ("namespace out { namespace in {\n" 2466 "}} // namespace out::in", 2467 format("namespace out {\n" 2468 "namespace in {\n" 2469 "} // namespace in\n" 2470 "} // namespace out", 2471 Style)); 2472 2473 // Only namespaces which have both consecutive opening and end get compacted 2474 EXPECT_EQ("namespace out {\n" 2475 "namespace in1 {\n" 2476 "} // namespace in1\n" 2477 "namespace in2 {\n" 2478 "} // namespace in2\n" 2479 "} // namespace out", 2480 format("namespace out {\n" 2481 "namespace in1 {\n" 2482 "} // namespace in1\n" 2483 "namespace in2 {\n" 2484 "} // namespace in2\n" 2485 "} // namespace out", 2486 Style)); 2487 2488 EXPECT_EQ("namespace out {\n" 2489 "int i;\n" 2490 "namespace in {\n" 2491 "int j;\n" 2492 "} // namespace in\n" 2493 "int k;\n" 2494 "} // namespace out", 2495 format("namespace out { int i;\n" 2496 "namespace in { int j; } // namespace in\n" 2497 "int k; } // namespace out", 2498 Style)); 2499 2500 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 2501 "}}} // namespace A::B::C\n", 2502 format("namespace A { namespace B {\n" 2503 "namespace C {\n" 2504 "}} // namespace B::C\n" 2505 "} // namespace A\n", 2506 Style)); 2507 2508 Style.ColumnLimit = 40; 2509 EXPECT_EQ("namespace aaaaaaaaaa {\n" 2510 "namespace bbbbbbbbbb {\n" 2511 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 2512 format("namespace aaaaaaaaaa {\n" 2513 "namespace bbbbbbbbbb {\n" 2514 "} // namespace bbbbbbbbbb\n" 2515 "} // namespace aaaaaaaaaa", 2516 Style)); 2517 2518 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 2519 "namespace cccccc {\n" 2520 "}}} // namespace aaaaaa::bbbbbb::cccccc", 2521 format("namespace aaaaaa {\n" 2522 "namespace bbbbbb {\n" 2523 "namespace cccccc {\n" 2524 "} // namespace cccccc\n" 2525 "} // namespace bbbbbb\n" 2526 "} // namespace aaaaaa", 2527 Style)); 2528 Style.ColumnLimit = 80; 2529 2530 // Extra semicolon after 'inner' closing brace prevents merging 2531 EXPECT_EQ("namespace out { namespace in {\n" 2532 "}; } // namespace out::in", 2533 format("namespace out {\n" 2534 "namespace in {\n" 2535 "}; // namespace in\n" 2536 "} // namespace out", 2537 Style)); 2538 2539 // Extra semicolon after 'outer' closing brace is conserved 2540 EXPECT_EQ("namespace out { namespace in {\n" 2541 "}}; // namespace out::in", 2542 format("namespace out {\n" 2543 "namespace in {\n" 2544 "} // namespace in\n" 2545 "}; // namespace out", 2546 Style)); 2547 2548 Style.NamespaceIndentation = FormatStyle::NI_All; 2549 EXPECT_EQ("namespace out { namespace in {\n" 2550 " int i;\n" 2551 "}} // namespace out::in", 2552 format("namespace out {\n" 2553 "namespace in {\n" 2554 "int i;\n" 2555 "} // namespace in\n" 2556 "} // namespace out", 2557 Style)); 2558 EXPECT_EQ("namespace out { namespace mid {\n" 2559 " namespace in {\n" 2560 " int j;\n" 2561 " } // namespace in\n" 2562 " int k;\n" 2563 "}} // namespace out::mid", 2564 format("namespace out { namespace mid {\n" 2565 "namespace in { int j; } // namespace in\n" 2566 "int k; }} // namespace out::mid", 2567 Style)); 2568 2569 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2570 EXPECT_EQ("namespace out { namespace in {\n" 2571 " int i;\n" 2572 "}} // namespace out::in", 2573 format("namespace out {\n" 2574 "namespace in {\n" 2575 "int i;\n" 2576 "} // namespace in\n" 2577 "} // namespace out", 2578 Style)); 2579 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 2580 " int i;\n" 2581 "}}} // namespace out::mid::in", 2582 format("namespace out {\n" 2583 "namespace mid {\n" 2584 "namespace in {\n" 2585 "int i;\n" 2586 "} // namespace in\n" 2587 "} // namespace mid\n" 2588 "} // namespace out", 2589 Style)); 2590 } 2591 2592 TEST_F(FormatTest, FormatsExternC) { 2593 verifyFormat("extern \"C\" {\nint a;"); 2594 verifyFormat("extern \"C\" {}"); 2595 verifyFormat("extern \"C\" {\n" 2596 "int foo();\n" 2597 "}"); 2598 verifyFormat("extern \"C\" int foo() {}"); 2599 verifyFormat("extern \"C\" int foo();"); 2600 verifyFormat("extern \"C\" int foo() {\n" 2601 " int i = 42;\n" 2602 " return i;\n" 2603 "}"); 2604 2605 FormatStyle Style = getLLVMStyle(); 2606 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2607 Style.BraceWrapping.AfterFunction = true; 2608 verifyFormat("extern \"C\" int foo() {}", Style); 2609 verifyFormat("extern \"C\" int foo();", Style); 2610 verifyFormat("extern \"C\" int foo()\n" 2611 "{\n" 2612 " int i = 42;\n" 2613 " return i;\n" 2614 "}", 2615 Style); 2616 2617 Style.BraceWrapping.AfterExternBlock = true; 2618 Style.BraceWrapping.SplitEmptyRecord = false; 2619 verifyFormat("extern \"C\"\n" 2620 "{}", 2621 Style); 2622 verifyFormat("extern \"C\"\n" 2623 "{\n" 2624 " int foo();\n" 2625 "}", 2626 Style); 2627 } 2628 2629 TEST_F(FormatTest, IndentExternBlockStyle) { 2630 FormatStyle Style = getLLVMStyle(); 2631 Style.IndentWidth = 2; 2632 2633 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 2634 verifyFormat("extern \"C\" { /*9*/\n}", Style); 2635 verifyFormat("extern \"C\" {\n" 2636 " int foo10();\n" 2637 "}", 2638 Style); 2639 2640 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 2641 verifyFormat("extern \"C\" { /*11*/\n}", Style); 2642 verifyFormat("extern \"C\" {\n" 2643 "int foo12();\n" 2644 "}", 2645 Style); 2646 2647 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 2648 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2649 Style.BraceWrapping.AfterExternBlock = true; 2650 verifyFormat("extern \"C\"\n{ /*13*/\n}", Style); 2651 verifyFormat("extern \"C\"\n{\n" 2652 " int foo14();\n" 2653 "}", 2654 Style); 2655 2656 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 2657 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2658 Style.BraceWrapping.AfterExternBlock = false; 2659 verifyFormat("extern \"C\" { /*15*/\n}", Style); 2660 verifyFormat("extern \"C\" {\n" 2661 "int foo16();\n" 2662 "}", 2663 Style); 2664 } 2665 2666 TEST_F(FormatTest, FormatsInlineASM) { 2667 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2668 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2669 verifyFormat( 2670 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 2671 " \"cpuid\\n\\t\"\n" 2672 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 2673 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 2674 " : \"a\"(value));"); 2675 EXPECT_EQ( 2676 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2677 " __asm {\n" 2678 " mov edx,[that] // vtable in edx\n" 2679 " mov eax,methodIndex\n" 2680 " call [edx][eax*4] // stdcall\n" 2681 " }\n" 2682 "}", 2683 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2684 " __asm {\n" 2685 " mov edx,[that] // vtable in edx\n" 2686 " mov eax,methodIndex\n" 2687 " call [edx][eax*4] // stdcall\n" 2688 " }\n" 2689 "}")); 2690 EXPECT_EQ("_asm {\n" 2691 " xor eax, eax;\n" 2692 " cpuid;\n" 2693 "}", 2694 format("_asm {\n" 2695 " xor eax, eax;\n" 2696 " cpuid;\n" 2697 "}")); 2698 verifyFormat("void function() {\n" 2699 " // comment\n" 2700 " asm(\"\");\n" 2701 "}"); 2702 EXPECT_EQ("__asm {\n" 2703 "}\n" 2704 "int i;", 2705 format("__asm {\n" 2706 "}\n" 2707 "int i;")); 2708 } 2709 2710 TEST_F(FormatTest, FormatTryCatch) { 2711 verifyFormat("try {\n" 2712 " throw a * b;\n" 2713 "} catch (int a) {\n" 2714 " // Do nothing.\n" 2715 "} catch (...) {\n" 2716 " exit(42);\n" 2717 "}"); 2718 2719 // Function-level try statements. 2720 verifyFormat("int f() try { return 4; } catch (...) {\n" 2721 " return 5;\n" 2722 "}"); 2723 verifyFormat("class A {\n" 2724 " int a;\n" 2725 " A() try : a(0) {\n" 2726 " } catch (...) {\n" 2727 " throw;\n" 2728 " }\n" 2729 "};\n"); 2730 verifyFormat("class A {\n" 2731 " int a;\n" 2732 " A() try : a(0), b{1} {\n" 2733 " } catch (...) {\n" 2734 " throw;\n" 2735 " }\n" 2736 "};\n"); 2737 verifyFormat("class A {\n" 2738 " int a;\n" 2739 " A() try : a(0), b{1}, c{2} {\n" 2740 " } catch (...) {\n" 2741 " throw;\n" 2742 " }\n" 2743 "};\n"); 2744 verifyFormat("class A {\n" 2745 " int a;\n" 2746 " A() try : a(0), b{1}, c{2} {\n" 2747 " { // New scope.\n" 2748 " }\n" 2749 " } catch (...) {\n" 2750 " throw;\n" 2751 " }\n" 2752 "};\n"); 2753 2754 // Incomplete try-catch blocks. 2755 verifyIncompleteFormat("try {} catch ("); 2756 } 2757 2758 TEST_F(FormatTest, FormatTryAsAVariable) { 2759 verifyFormat("int try;"); 2760 verifyFormat("int try, size;"); 2761 verifyFormat("try = foo();"); 2762 verifyFormat("if (try < size) {\n return true;\n}"); 2763 2764 verifyFormat("int catch;"); 2765 verifyFormat("int catch, size;"); 2766 verifyFormat("catch = foo();"); 2767 verifyFormat("if (catch < size) {\n return true;\n}"); 2768 2769 FormatStyle Style = getLLVMStyle(); 2770 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2771 Style.BraceWrapping.AfterFunction = true; 2772 Style.BraceWrapping.BeforeCatch = true; 2773 verifyFormat("try {\n" 2774 " int bar = 1;\n" 2775 "}\n" 2776 "catch (...) {\n" 2777 " int bar = 1;\n" 2778 "}", 2779 Style); 2780 verifyFormat("#if NO_EX\n" 2781 "try\n" 2782 "#endif\n" 2783 "{\n" 2784 "}\n" 2785 "#if NO_EX\n" 2786 "catch (...) {\n" 2787 "}", 2788 Style); 2789 verifyFormat("try /* abc */ {\n" 2790 " int bar = 1;\n" 2791 "}\n" 2792 "catch (...) {\n" 2793 " int bar = 1;\n" 2794 "}", 2795 Style); 2796 verifyFormat("try\n" 2797 "// abc\n" 2798 "{\n" 2799 " int bar = 1;\n" 2800 "}\n" 2801 "catch (...) {\n" 2802 " int bar = 1;\n" 2803 "}", 2804 Style); 2805 } 2806 2807 TEST_F(FormatTest, FormatSEHTryCatch) { 2808 verifyFormat("__try {\n" 2809 " int a = b * c;\n" 2810 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2811 " // Do nothing.\n" 2812 "}"); 2813 2814 verifyFormat("__try {\n" 2815 " int a = b * c;\n" 2816 "} __finally {\n" 2817 " // Do nothing.\n" 2818 "}"); 2819 2820 verifyFormat("DEBUG({\n" 2821 " __try {\n" 2822 " } __finally {\n" 2823 " }\n" 2824 "});\n"); 2825 } 2826 2827 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2828 verifyFormat("try {\n" 2829 " f();\n" 2830 "} catch {\n" 2831 " g();\n" 2832 "}"); 2833 verifyFormat("try {\n" 2834 " f();\n" 2835 "} catch (A a) MACRO(x) {\n" 2836 " g();\n" 2837 "} catch (B b) MACRO(x) {\n" 2838 " g();\n" 2839 "}"); 2840 } 2841 2842 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2843 FormatStyle Style = getLLVMStyle(); 2844 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 2845 FormatStyle::BS_WebKit}) { 2846 Style.BreakBeforeBraces = BraceStyle; 2847 verifyFormat("try {\n" 2848 " // something\n" 2849 "} catch (...) {\n" 2850 " // something\n" 2851 "}", 2852 Style); 2853 } 2854 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2855 verifyFormat("try {\n" 2856 " // something\n" 2857 "}\n" 2858 "catch (...) {\n" 2859 " // something\n" 2860 "}", 2861 Style); 2862 verifyFormat("__try {\n" 2863 " // something\n" 2864 "}\n" 2865 "__finally {\n" 2866 " // something\n" 2867 "}", 2868 Style); 2869 verifyFormat("@try {\n" 2870 " // something\n" 2871 "}\n" 2872 "@finally {\n" 2873 " // something\n" 2874 "}", 2875 Style); 2876 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2877 verifyFormat("try\n" 2878 "{\n" 2879 " // something\n" 2880 "}\n" 2881 "catch (...)\n" 2882 "{\n" 2883 " // something\n" 2884 "}", 2885 Style); 2886 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 2887 verifyFormat("try\n" 2888 " {\n" 2889 " // something white\n" 2890 " }\n" 2891 "catch (...)\n" 2892 " {\n" 2893 " // something white\n" 2894 " }", 2895 Style); 2896 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2897 verifyFormat("try\n" 2898 " {\n" 2899 " // something\n" 2900 " }\n" 2901 "catch (...)\n" 2902 " {\n" 2903 " // something\n" 2904 " }", 2905 Style); 2906 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2907 Style.BraceWrapping.BeforeCatch = true; 2908 verifyFormat("try {\n" 2909 " // something\n" 2910 "}\n" 2911 "catch (...) {\n" 2912 " // something\n" 2913 "}", 2914 Style); 2915 } 2916 2917 TEST_F(FormatTest, StaticInitializers) { 2918 verifyFormat("static SomeClass SC = {1, 'a'};"); 2919 2920 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2921 " 100000000, " 2922 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2923 2924 // Here, everything other than the "}" would fit on a line. 2925 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2926 " 10000000000000000000000000};"); 2927 EXPECT_EQ("S s = {a,\n" 2928 "\n" 2929 " b};", 2930 format("S s = {\n" 2931 " a,\n" 2932 "\n" 2933 " b\n" 2934 "};")); 2935 2936 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2937 // line. However, the formatting looks a bit off and this probably doesn't 2938 // happen often in practice. 2939 verifyFormat("static int Variable[1] = {\n" 2940 " {1000000000000000000000000000000000000}};", 2941 getLLVMStyleWithColumns(40)); 2942 } 2943 2944 TEST_F(FormatTest, DesignatedInitializers) { 2945 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2946 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2947 " .bbbbbbbbbb = 2,\n" 2948 " .cccccccccc = 3,\n" 2949 " .dddddddddd = 4,\n" 2950 " .eeeeeeeeee = 5};"); 2951 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2952 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2953 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2954 " .ccccccccccccccccccccccccccc = 3,\n" 2955 " .ddddddddddddddddddddddddddd = 4,\n" 2956 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2957 2958 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2959 2960 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 2961 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 2962 " [2] = bbbbbbbbbb,\n" 2963 " [3] = cccccccccc,\n" 2964 " [4] = dddddddddd,\n" 2965 " [5] = eeeeeeeeee};"); 2966 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2967 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2968 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2969 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 2970 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 2971 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 2972 } 2973 2974 TEST_F(FormatTest, NestedStaticInitializers) { 2975 verifyFormat("static A x = {{{}}};\n"); 2976 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2977 " {init1, init2, init3, init4}}};", 2978 getLLVMStyleWithColumns(50)); 2979 2980 verifyFormat("somes Status::global_reps[3] = {\n" 2981 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2982 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2983 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2984 getLLVMStyleWithColumns(60)); 2985 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2986 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2987 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2988 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2989 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2990 " {rect.fRight - rect.fLeft, rect.fBottom - " 2991 "rect.fTop}};"); 2992 2993 verifyFormat( 2994 "SomeArrayOfSomeType a = {\n" 2995 " {{1, 2, 3},\n" 2996 " {1, 2, 3},\n" 2997 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2998 " 333333333333333333333333333333},\n" 2999 " {1, 2, 3},\n" 3000 " {1, 2, 3}}};"); 3001 verifyFormat( 3002 "SomeArrayOfSomeType a = {\n" 3003 " {{1, 2, 3}},\n" 3004 " {{1, 2, 3}},\n" 3005 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 3006 " 333333333333333333333333333333}},\n" 3007 " {{1, 2, 3}},\n" 3008 " {{1, 2, 3}}};"); 3009 3010 verifyFormat("struct {\n" 3011 " unsigned bit;\n" 3012 " const char *const name;\n" 3013 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 3014 " {kOsWin, \"Windows\"},\n" 3015 " {kOsLinux, \"Linux\"},\n" 3016 " {kOsCrOS, \"Chrome OS\"}};"); 3017 verifyFormat("struct {\n" 3018 " unsigned bit;\n" 3019 " const char *const name;\n" 3020 "} kBitsToOs[] = {\n" 3021 " {kOsMac, \"Mac\"},\n" 3022 " {kOsWin, \"Windows\"},\n" 3023 " {kOsLinux, \"Linux\"},\n" 3024 " {kOsCrOS, \"Chrome OS\"},\n" 3025 "};"); 3026 } 3027 3028 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 3029 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3030 " \\\n" 3031 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 3032 } 3033 3034 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 3035 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 3036 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 3037 3038 // Do break defaulted and deleted functions. 3039 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 3040 " default;", 3041 getLLVMStyleWithColumns(40)); 3042 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 3043 " delete;", 3044 getLLVMStyleWithColumns(40)); 3045 } 3046 3047 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 3048 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 3049 getLLVMStyleWithColumns(40)); 3050 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 3051 getLLVMStyleWithColumns(40)); 3052 EXPECT_EQ("#define Q \\\n" 3053 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 3054 " \"aaaaaaaa.cpp\"", 3055 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 3056 getLLVMStyleWithColumns(40))); 3057 } 3058 3059 TEST_F(FormatTest, UnderstandsLinePPDirective) { 3060 EXPECT_EQ("# 123 \"A string literal\"", 3061 format(" # 123 \"A string literal\"")); 3062 } 3063 3064 TEST_F(FormatTest, LayoutUnknownPPDirective) { 3065 EXPECT_EQ("#;", format("#;")); 3066 verifyFormat("#\n;\n;\n;"); 3067 } 3068 3069 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 3070 EXPECT_EQ("#line 42 \"test\"\n", 3071 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 3072 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 3073 getLLVMStyleWithColumns(12))); 3074 } 3075 3076 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 3077 EXPECT_EQ("#line 42 \"test\"", 3078 format("# \\\n line \\\n 42 \\\n \"test\"")); 3079 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 3080 } 3081 3082 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 3083 verifyFormat("#define A \\x20"); 3084 verifyFormat("#define A \\ x20"); 3085 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 3086 verifyFormat("#define A ''"); 3087 verifyFormat("#define A ''qqq"); 3088 verifyFormat("#define A `qqq"); 3089 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 3090 EXPECT_EQ("const char *c = STRINGIFY(\n" 3091 "\\na : b);", 3092 format("const char * c = STRINGIFY(\n" 3093 "\\na : b);")); 3094 3095 verifyFormat("a\r\\"); 3096 verifyFormat("a\v\\"); 3097 verifyFormat("a\f\\"); 3098 } 3099 3100 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 3101 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 3102 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 3103 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 3104 // FIXME: We never break before the macro name. 3105 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 3106 3107 verifyFormat("#define A A\n#define A A"); 3108 verifyFormat("#define A(X) A\n#define A A"); 3109 3110 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 3111 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 3112 } 3113 3114 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 3115 EXPECT_EQ("// somecomment\n" 3116 "#include \"a.h\"\n" 3117 "#define A( \\\n" 3118 " A, B)\n" 3119 "#include \"b.h\"\n" 3120 "// somecomment\n", 3121 format(" // somecomment\n" 3122 " #include \"a.h\"\n" 3123 "#define A(A,\\\n" 3124 " B)\n" 3125 " #include \"b.h\"\n" 3126 " // somecomment\n", 3127 getLLVMStyleWithColumns(13))); 3128 } 3129 3130 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 3131 3132 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 3133 EXPECT_EQ("#define A \\\n" 3134 " c; \\\n" 3135 " e;\n" 3136 "f;", 3137 format("#define A c; e;\n" 3138 "f;", 3139 getLLVMStyleWithColumns(14))); 3140 } 3141 3142 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 3143 3144 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 3145 EXPECT_EQ("int x,\n" 3146 "#define A\n" 3147 " y;", 3148 format("int x,\n#define A\ny;")); 3149 } 3150 3151 TEST_F(FormatTest, HashInMacroDefinition) { 3152 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 3153 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 3154 verifyFormat("#define A \\\n" 3155 " { \\\n" 3156 " f(#c); \\\n" 3157 " }", 3158 getLLVMStyleWithColumns(11)); 3159 3160 verifyFormat("#define A(X) \\\n" 3161 " void function##X()", 3162 getLLVMStyleWithColumns(22)); 3163 3164 verifyFormat("#define A(a, b, c) \\\n" 3165 " void a##b##c()", 3166 getLLVMStyleWithColumns(22)); 3167 3168 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 3169 } 3170 3171 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 3172 EXPECT_EQ("#define A (x)", format("#define A (x)")); 3173 EXPECT_EQ("#define A(x)", format("#define A(x)")); 3174 3175 FormatStyle Style = getLLVMStyle(); 3176 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 3177 verifyFormat("#define true ((foo)1)", Style); 3178 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 3179 verifyFormat("#define false((foo)0)", Style); 3180 } 3181 3182 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 3183 EXPECT_EQ("#define A b;", format("#define A \\\n" 3184 " \\\n" 3185 " b;", 3186 getLLVMStyleWithColumns(25))); 3187 EXPECT_EQ("#define A \\\n" 3188 " \\\n" 3189 " a; \\\n" 3190 " b;", 3191 format("#define A \\\n" 3192 " \\\n" 3193 " a; \\\n" 3194 " b;", 3195 getLLVMStyleWithColumns(11))); 3196 EXPECT_EQ("#define A \\\n" 3197 " a; \\\n" 3198 " \\\n" 3199 " b;", 3200 format("#define A \\\n" 3201 " a; \\\n" 3202 " \\\n" 3203 " b;", 3204 getLLVMStyleWithColumns(11))); 3205 } 3206 3207 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 3208 verifyIncompleteFormat("#define A :"); 3209 verifyFormat("#define SOMECASES \\\n" 3210 " case 1: \\\n" 3211 " case 2\n", 3212 getLLVMStyleWithColumns(20)); 3213 verifyFormat("#define MACRO(a) \\\n" 3214 " if (a) \\\n" 3215 " f(); \\\n" 3216 " else \\\n" 3217 " g()", 3218 getLLVMStyleWithColumns(18)); 3219 verifyFormat("#define A template <typename T>"); 3220 verifyIncompleteFormat("#define STR(x) #x\n" 3221 "f(STR(this_is_a_string_literal{));"); 3222 verifyFormat("#pragma omp threadprivate( \\\n" 3223 " y)), // expected-warning", 3224 getLLVMStyleWithColumns(28)); 3225 verifyFormat("#d, = };"); 3226 verifyFormat("#if \"a"); 3227 verifyIncompleteFormat("({\n" 3228 "#define b \\\n" 3229 " } \\\n" 3230 " a\n" 3231 "a", 3232 getLLVMStyleWithColumns(15)); 3233 verifyFormat("#define A \\\n" 3234 " { \\\n" 3235 " {\n" 3236 "#define B \\\n" 3237 " } \\\n" 3238 " }", 3239 getLLVMStyleWithColumns(15)); 3240 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 3241 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 3242 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 3243 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 3244 } 3245 3246 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 3247 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 3248 EXPECT_EQ("class A : public QObject {\n" 3249 " Q_OBJECT\n" 3250 "\n" 3251 " A() {}\n" 3252 "};", 3253 format("class A : public QObject {\n" 3254 " Q_OBJECT\n" 3255 "\n" 3256 " A() {\n}\n" 3257 "} ;")); 3258 EXPECT_EQ("MACRO\n" 3259 "/*static*/ int i;", 3260 format("MACRO\n" 3261 " /*static*/ int i;")); 3262 EXPECT_EQ("SOME_MACRO\n" 3263 "namespace {\n" 3264 "void f();\n" 3265 "} // namespace", 3266 format("SOME_MACRO\n" 3267 " namespace {\n" 3268 "void f( );\n" 3269 "} // namespace")); 3270 // Only if the identifier contains at least 5 characters. 3271 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 3272 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 3273 // Only if everything is upper case. 3274 EXPECT_EQ("class A : public QObject {\n" 3275 " Q_Object A() {}\n" 3276 "};", 3277 format("class A : public QObject {\n" 3278 " Q_Object\n" 3279 " A() {\n}\n" 3280 "} ;")); 3281 3282 // Only if the next line can actually start an unwrapped line. 3283 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 3284 format("SOME_WEIRD_LOG_MACRO\n" 3285 "<< SomeThing;")); 3286 3287 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 3288 "(n, buffers))\n", 3289 getChromiumStyle(FormatStyle::LK_Cpp)); 3290 3291 // See PR41483 3292 EXPECT_EQ("/**/ FOO(a)\n" 3293 "FOO(b)", 3294 format("/**/ FOO(a)\n" 3295 "FOO(b)")); 3296 } 3297 3298 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 3299 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 3300 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 3301 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 3302 "class X {};\n" 3303 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 3304 "int *createScopDetectionPass() { return 0; }", 3305 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 3306 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 3307 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 3308 " class X {};\n" 3309 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 3310 " int *createScopDetectionPass() { return 0; }")); 3311 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 3312 // braces, so that inner block is indented one level more. 3313 EXPECT_EQ("int q() {\n" 3314 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 3315 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 3316 " IPC_END_MESSAGE_MAP()\n" 3317 "}", 3318 format("int q() {\n" 3319 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 3320 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 3321 " IPC_END_MESSAGE_MAP()\n" 3322 "}")); 3323 3324 // Same inside macros. 3325 EXPECT_EQ("#define LIST(L) \\\n" 3326 " L(A) \\\n" 3327 " L(B) \\\n" 3328 " L(C)", 3329 format("#define LIST(L) \\\n" 3330 " L(A) \\\n" 3331 " L(B) \\\n" 3332 " L(C)", 3333 getGoogleStyle())); 3334 3335 // These must not be recognized as macros. 3336 EXPECT_EQ("int q() {\n" 3337 " f(x);\n" 3338 " f(x) {}\n" 3339 " f(x)->g();\n" 3340 " f(x)->*g();\n" 3341 " f(x).g();\n" 3342 " f(x) = x;\n" 3343 " f(x) += x;\n" 3344 " f(x) -= x;\n" 3345 " f(x) *= x;\n" 3346 " f(x) /= x;\n" 3347 " f(x) %= x;\n" 3348 " f(x) &= x;\n" 3349 " f(x) |= x;\n" 3350 " f(x) ^= x;\n" 3351 " f(x) >>= x;\n" 3352 " f(x) <<= x;\n" 3353 " f(x)[y].z();\n" 3354 " LOG(INFO) << x;\n" 3355 " ifstream(x) >> x;\n" 3356 "}\n", 3357 format("int q() {\n" 3358 " f(x)\n;\n" 3359 " f(x)\n {}\n" 3360 " f(x)\n->g();\n" 3361 " f(x)\n->*g();\n" 3362 " f(x)\n.g();\n" 3363 " f(x)\n = x;\n" 3364 " f(x)\n += x;\n" 3365 " f(x)\n -= x;\n" 3366 " f(x)\n *= x;\n" 3367 " f(x)\n /= x;\n" 3368 " f(x)\n %= x;\n" 3369 " f(x)\n &= x;\n" 3370 " f(x)\n |= x;\n" 3371 " f(x)\n ^= x;\n" 3372 " f(x)\n >>= x;\n" 3373 " f(x)\n <<= x;\n" 3374 " f(x)\n[y].z();\n" 3375 " LOG(INFO)\n << x;\n" 3376 " ifstream(x)\n >> x;\n" 3377 "}\n")); 3378 EXPECT_EQ("int q() {\n" 3379 " F(x)\n" 3380 " if (1) {\n" 3381 " }\n" 3382 " F(x)\n" 3383 " while (1) {\n" 3384 " }\n" 3385 " F(x)\n" 3386 " G(x);\n" 3387 " F(x)\n" 3388 " try {\n" 3389 " Q();\n" 3390 " } catch (...) {\n" 3391 " }\n" 3392 "}\n", 3393 format("int q() {\n" 3394 "F(x)\n" 3395 "if (1) {}\n" 3396 "F(x)\n" 3397 "while (1) {}\n" 3398 "F(x)\n" 3399 "G(x);\n" 3400 "F(x)\n" 3401 "try { Q(); } catch (...) {}\n" 3402 "}\n")); 3403 EXPECT_EQ("class A {\n" 3404 " A() : t(0) {}\n" 3405 " A(int i) noexcept() : {}\n" 3406 " A(X x)\n" // FIXME: function-level try blocks are broken. 3407 " try : t(0) {\n" 3408 " } catch (...) {\n" 3409 " }\n" 3410 "};", 3411 format("class A {\n" 3412 " A()\n : t(0) {}\n" 3413 " A(int i)\n noexcept() : {}\n" 3414 " A(X x)\n" 3415 " try : t(0) {} catch (...) {}\n" 3416 "};")); 3417 FormatStyle Style = getLLVMStyle(); 3418 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3419 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 3420 Style.BraceWrapping.AfterFunction = true; 3421 EXPECT_EQ("void f()\n" 3422 "try\n" 3423 "{\n" 3424 "}", 3425 format("void f() try {\n" 3426 "}", 3427 Style)); 3428 EXPECT_EQ("class SomeClass {\n" 3429 "public:\n" 3430 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3431 "};", 3432 format("class SomeClass {\n" 3433 "public:\n" 3434 " SomeClass()\n" 3435 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3436 "};")); 3437 EXPECT_EQ("class SomeClass {\n" 3438 "public:\n" 3439 " SomeClass()\n" 3440 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3441 "};", 3442 format("class SomeClass {\n" 3443 "public:\n" 3444 " SomeClass()\n" 3445 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3446 "};", 3447 getLLVMStyleWithColumns(40))); 3448 3449 verifyFormat("MACRO(>)"); 3450 3451 // Some macros contain an implicit semicolon. 3452 Style = getLLVMStyle(); 3453 Style.StatementMacros.push_back("FOO"); 3454 verifyFormat("FOO(a) int b = 0;"); 3455 verifyFormat("FOO(a)\n" 3456 "int b = 0;", 3457 Style); 3458 verifyFormat("FOO(a);\n" 3459 "int b = 0;", 3460 Style); 3461 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 3462 "int b = 0;", 3463 Style); 3464 verifyFormat("FOO()\n" 3465 "int b = 0;", 3466 Style); 3467 verifyFormat("FOO\n" 3468 "int b = 0;", 3469 Style); 3470 verifyFormat("void f() {\n" 3471 " FOO(a)\n" 3472 " return a;\n" 3473 "}", 3474 Style); 3475 verifyFormat("FOO(a)\n" 3476 "FOO(b)", 3477 Style); 3478 verifyFormat("int a = 0;\n" 3479 "FOO(b)\n" 3480 "int c = 0;", 3481 Style); 3482 verifyFormat("int a = 0;\n" 3483 "int x = FOO(a)\n" 3484 "int b = 0;", 3485 Style); 3486 verifyFormat("void foo(int a) { FOO(a) }\n" 3487 "uint32_t bar() {}", 3488 Style); 3489 } 3490 3491 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 3492 verifyFormat("#define A \\\n" 3493 " f({ \\\n" 3494 " g(); \\\n" 3495 " });", 3496 getLLVMStyleWithColumns(11)); 3497 } 3498 3499 TEST_F(FormatTest, IndentPreprocessorDirectives) { 3500 FormatStyle Style = getLLVMStyle(); 3501 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 3502 Style.ColumnLimit = 40; 3503 verifyFormat("#ifdef _WIN32\n" 3504 "#define A 0\n" 3505 "#ifdef VAR2\n" 3506 "#define B 1\n" 3507 "#include <someheader.h>\n" 3508 "#define MACRO \\\n" 3509 " some_very_long_func_aaaaaaaaaa();\n" 3510 "#endif\n" 3511 "#else\n" 3512 "#define A 1\n" 3513 "#endif", 3514 Style); 3515 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 3516 verifyFormat("#ifdef _WIN32\n" 3517 "# define A 0\n" 3518 "# ifdef VAR2\n" 3519 "# define B 1\n" 3520 "# include <someheader.h>\n" 3521 "# define MACRO \\\n" 3522 " some_very_long_func_aaaaaaaaaa();\n" 3523 "# endif\n" 3524 "#else\n" 3525 "# define A 1\n" 3526 "#endif", 3527 Style); 3528 verifyFormat("#if A\n" 3529 "# define MACRO \\\n" 3530 " void a(int x) { \\\n" 3531 " b(); \\\n" 3532 " c(); \\\n" 3533 " d(); \\\n" 3534 " e(); \\\n" 3535 " f(); \\\n" 3536 " }\n" 3537 "#endif", 3538 Style); 3539 // Comments before include guard. 3540 verifyFormat("// file comment\n" 3541 "// file comment\n" 3542 "#ifndef HEADER_H\n" 3543 "#define HEADER_H\n" 3544 "code();\n" 3545 "#endif", 3546 Style); 3547 // Test with include guards. 3548 verifyFormat("#ifndef HEADER_H\n" 3549 "#define HEADER_H\n" 3550 "code();\n" 3551 "#endif", 3552 Style); 3553 // Include guards must have a #define with the same variable immediately 3554 // after #ifndef. 3555 verifyFormat("#ifndef NOT_GUARD\n" 3556 "# define FOO\n" 3557 "code();\n" 3558 "#endif", 3559 Style); 3560 3561 // Include guards must cover the entire file. 3562 verifyFormat("code();\n" 3563 "code();\n" 3564 "#ifndef NOT_GUARD\n" 3565 "# define NOT_GUARD\n" 3566 "code();\n" 3567 "#endif", 3568 Style); 3569 verifyFormat("#ifndef NOT_GUARD\n" 3570 "# define NOT_GUARD\n" 3571 "code();\n" 3572 "#endif\n" 3573 "code();", 3574 Style); 3575 // Test with trailing blank lines. 3576 verifyFormat("#ifndef HEADER_H\n" 3577 "#define HEADER_H\n" 3578 "code();\n" 3579 "#endif\n", 3580 Style); 3581 // Include guards don't have #else. 3582 verifyFormat("#ifndef NOT_GUARD\n" 3583 "# define NOT_GUARD\n" 3584 "code();\n" 3585 "#else\n" 3586 "#endif", 3587 Style); 3588 verifyFormat("#ifndef NOT_GUARD\n" 3589 "# define NOT_GUARD\n" 3590 "code();\n" 3591 "#elif FOO\n" 3592 "#endif", 3593 Style); 3594 // Non-identifier #define after potential include guard. 3595 verifyFormat("#ifndef FOO\n" 3596 "# define 1\n" 3597 "#endif\n", 3598 Style); 3599 // #if closes past last non-preprocessor line. 3600 verifyFormat("#ifndef FOO\n" 3601 "#define FOO\n" 3602 "#if 1\n" 3603 "int i;\n" 3604 "# define A 0\n" 3605 "#endif\n" 3606 "#endif\n", 3607 Style); 3608 // Don't crash if there is an #elif directive without a condition. 3609 verifyFormat("#if 1\n" 3610 "int x;\n" 3611 "#elif\n" 3612 "int y;\n" 3613 "#else\n" 3614 "int z;\n" 3615 "#endif", 3616 Style); 3617 // FIXME: This doesn't handle the case where there's code between the 3618 // #ifndef and #define but all other conditions hold. This is because when 3619 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 3620 // previous code line yet, so we can't detect it. 3621 EXPECT_EQ("#ifndef NOT_GUARD\n" 3622 "code();\n" 3623 "#define NOT_GUARD\n" 3624 "code();\n" 3625 "#endif", 3626 format("#ifndef NOT_GUARD\n" 3627 "code();\n" 3628 "# define NOT_GUARD\n" 3629 "code();\n" 3630 "#endif", 3631 Style)); 3632 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 3633 // be outside an include guard. Examples are #pragma once and 3634 // #pragma GCC diagnostic, or anything else that does not change the meaning 3635 // of the file if it's included multiple times. 3636 EXPECT_EQ("#ifdef WIN32\n" 3637 "# pragma once\n" 3638 "#endif\n" 3639 "#ifndef HEADER_H\n" 3640 "# define HEADER_H\n" 3641 "code();\n" 3642 "#endif", 3643 format("#ifdef WIN32\n" 3644 "# pragma once\n" 3645 "#endif\n" 3646 "#ifndef HEADER_H\n" 3647 "#define HEADER_H\n" 3648 "code();\n" 3649 "#endif", 3650 Style)); 3651 // FIXME: This does not detect when there is a single non-preprocessor line 3652 // in front of an include-guard-like structure where other conditions hold 3653 // because ScopedLineState hides the line. 3654 EXPECT_EQ("code();\n" 3655 "#ifndef HEADER_H\n" 3656 "#define HEADER_H\n" 3657 "code();\n" 3658 "#endif", 3659 format("code();\n" 3660 "#ifndef HEADER_H\n" 3661 "# define HEADER_H\n" 3662 "code();\n" 3663 "#endif", 3664 Style)); 3665 // Keep comments aligned with #, otherwise indent comments normally. These 3666 // tests cannot use verifyFormat because messUp manipulates leading 3667 // whitespace. 3668 { 3669 const char *Expected = "" 3670 "void f() {\n" 3671 "#if 1\n" 3672 "// Preprocessor aligned.\n" 3673 "# define A 0\n" 3674 " // Code. Separated by blank line.\n" 3675 "\n" 3676 "# define B 0\n" 3677 " // Code. Not aligned with #\n" 3678 "# define C 0\n" 3679 "#endif"; 3680 const char *ToFormat = "" 3681 "void f() {\n" 3682 "#if 1\n" 3683 "// Preprocessor aligned.\n" 3684 "# define A 0\n" 3685 "// Code. Separated by blank line.\n" 3686 "\n" 3687 "# define B 0\n" 3688 " // Code. Not aligned with #\n" 3689 "# define C 0\n" 3690 "#endif"; 3691 EXPECT_EQ(Expected, format(ToFormat, Style)); 3692 EXPECT_EQ(Expected, format(Expected, Style)); 3693 } 3694 // Keep block quotes aligned. 3695 { 3696 const char *Expected = "" 3697 "void f() {\n" 3698 "#if 1\n" 3699 "/* Preprocessor aligned. */\n" 3700 "# define A 0\n" 3701 " /* Code. Separated by blank line. */\n" 3702 "\n" 3703 "# define B 0\n" 3704 " /* Code. Not aligned with # */\n" 3705 "# define C 0\n" 3706 "#endif"; 3707 const char *ToFormat = "" 3708 "void f() {\n" 3709 "#if 1\n" 3710 "/* Preprocessor aligned. */\n" 3711 "# define A 0\n" 3712 "/* Code. Separated by blank line. */\n" 3713 "\n" 3714 "# define B 0\n" 3715 " /* Code. Not aligned with # */\n" 3716 "# define C 0\n" 3717 "#endif"; 3718 EXPECT_EQ(Expected, format(ToFormat, Style)); 3719 EXPECT_EQ(Expected, format(Expected, Style)); 3720 } 3721 // Keep comments aligned with un-indented directives. 3722 { 3723 const char *Expected = "" 3724 "void f() {\n" 3725 "// Preprocessor aligned.\n" 3726 "#define A 0\n" 3727 " // Code. Separated by blank line.\n" 3728 "\n" 3729 "#define B 0\n" 3730 " // Code. Not aligned with #\n" 3731 "#define C 0\n"; 3732 const char *ToFormat = "" 3733 "void f() {\n" 3734 "// Preprocessor aligned.\n" 3735 "#define A 0\n" 3736 "// Code. Separated by blank line.\n" 3737 "\n" 3738 "#define B 0\n" 3739 " // Code. Not aligned with #\n" 3740 "#define C 0\n"; 3741 EXPECT_EQ(Expected, format(ToFormat, Style)); 3742 EXPECT_EQ(Expected, format(Expected, Style)); 3743 } 3744 // Test AfterHash with tabs. 3745 { 3746 FormatStyle Tabbed = Style; 3747 Tabbed.UseTab = FormatStyle::UT_Always; 3748 Tabbed.IndentWidth = 8; 3749 Tabbed.TabWidth = 8; 3750 verifyFormat("#ifdef _WIN32\n" 3751 "#\tdefine A 0\n" 3752 "#\tifdef VAR2\n" 3753 "#\t\tdefine B 1\n" 3754 "#\t\tinclude <someheader.h>\n" 3755 "#\t\tdefine MACRO \\\n" 3756 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 3757 "#\tendif\n" 3758 "#else\n" 3759 "#\tdefine A 1\n" 3760 "#endif", 3761 Tabbed); 3762 } 3763 3764 // Regression test: Multiline-macro inside include guards. 3765 verifyFormat("#ifndef HEADER_H\n" 3766 "#define HEADER_H\n" 3767 "#define A() \\\n" 3768 " int i; \\\n" 3769 " int j;\n" 3770 "#endif // HEADER_H", 3771 getLLVMStyleWithColumns(20)); 3772 3773 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 3774 // Basic before hash indent tests 3775 verifyFormat("#ifdef _WIN32\n" 3776 " #define A 0\n" 3777 " #ifdef VAR2\n" 3778 " #define B 1\n" 3779 " #include <someheader.h>\n" 3780 " #define MACRO \\\n" 3781 " some_very_long_func_aaaaaaaaaa();\n" 3782 " #endif\n" 3783 "#else\n" 3784 " #define A 1\n" 3785 "#endif", 3786 Style); 3787 verifyFormat("#if A\n" 3788 " #define MACRO \\\n" 3789 " void a(int x) { \\\n" 3790 " b(); \\\n" 3791 " c(); \\\n" 3792 " d(); \\\n" 3793 " e(); \\\n" 3794 " f(); \\\n" 3795 " }\n" 3796 "#endif", 3797 Style); 3798 // Keep comments aligned with indented directives. These 3799 // tests cannot use verifyFormat because messUp manipulates leading 3800 // whitespace. 3801 { 3802 const char *Expected = "void f() {\n" 3803 "// Aligned to preprocessor.\n" 3804 "#if 1\n" 3805 " // Aligned to code.\n" 3806 " int a;\n" 3807 " #if 1\n" 3808 " // Aligned to preprocessor.\n" 3809 " #define A 0\n" 3810 " // Aligned to code.\n" 3811 " int b;\n" 3812 " #endif\n" 3813 "#endif\n" 3814 "}"; 3815 const char *ToFormat = "void f() {\n" 3816 "// Aligned to preprocessor.\n" 3817 "#if 1\n" 3818 "// Aligned to code.\n" 3819 "int a;\n" 3820 "#if 1\n" 3821 "// Aligned to preprocessor.\n" 3822 "#define A 0\n" 3823 "// Aligned to code.\n" 3824 "int b;\n" 3825 "#endif\n" 3826 "#endif\n" 3827 "}"; 3828 EXPECT_EQ(Expected, format(ToFormat, Style)); 3829 EXPECT_EQ(Expected, format(Expected, Style)); 3830 } 3831 { 3832 const char *Expected = "void f() {\n" 3833 "/* Aligned to preprocessor. */\n" 3834 "#if 1\n" 3835 " /* Aligned to code. */\n" 3836 " int a;\n" 3837 " #if 1\n" 3838 " /* Aligned to preprocessor. */\n" 3839 " #define A 0\n" 3840 " /* Aligned to code. */\n" 3841 " int b;\n" 3842 " #endif\n" 3843 "#endif\n" 3844 "}"; 3845 const char *ToFormat = "void f() {\n" 3846 "/* Aligned to preprocessor. */\n" 3847 "#if 1\n" 3848 "/* Aligned to code. */\n" 3849 "int a;\n" 3850 "#if 1\n" 3851 "/* Aligned to preprocessor. */\n" 3852 "#define A 0\n" 3853 "/* Aligned to code. */\n" 3854 "int b;\n" 3855 "#endif\n" 3856 "#endif\n" 3857 "}"; 3858 EXPECT_EQ(Expected, format(ToFormat, Style)); 3859 EXPECT_EQ(Expected, format(Expected, Style)); 3860 } 3861 3862 // Test single comment before preprocessor 3863 verifyFormat("// Comment\n" 3864 "\n" 3865 "#if 1\n" 3866 "#endif", 3867 Style); 3868 } 3869 3870 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 3871 verifyFormat("{\n { a #c; }\n}"); 3872 } 3873 3874 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 3875 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 3876 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 3877 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 3878 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 3879 } 3880 3881 TEST_F(FormatTest, EscapedNewlines) { 3882 FormatStyle Narrow = getLLVMStyleWithColumns(11); 3883 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 3884 format("#define A \\\nint i;\\\n int j;", Narrow)); 3885 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 3886 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3887 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 3888 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 3889 3890 FormatStyle AlignLeft = getLLVMStyle(); 3891 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 3892 EXPECT_EQ("#define MACRO(x) \\\n" 3893 "private: \\\n" 3894 " int x(int a);\n", 3895 format("#define MACRO(x) \\\n" 3896 "private: \\\n" 3897 " int x(int a);\n", 3898 AlignLeft)); 3899 3900 // CRLF line endings 3901 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 3902 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 3903 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 3904 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3905 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 3906 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 3907 EXPECT_EQ("#define MACRO(x) \\\r\n" 3908 "private: \\\r\n" 3909 " int x(int a);\r\n", 3910 format("#define MACRO(x) \\\r\n" 3911 "private: \\\r\n" 3912 " int x(int a);\r\n", 3913 AlignLeft)); 3914 3915 FormatStyle DontAlign = getLLVMStyle(); 3916 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 3917 DontAlign.MaxEmptyLinesToKeep = 3; 3918 // FIXME: can't use verifyFormat here because the newline before 3919 // "public:" is not inserted the first time it's reformatted 3920 EXPECT_EQ("#define A \\\n" 3921 " class Foo { \\\n" 3922 " void bar(); \\\n" 3923 "\\\n" 3924 "\\\n" 3925 "\\\n" 3926 " public: \\\n" 3927 " void baz(); \\\n" 3928 " };", 3929 format("#define A \\\n" 3930 " class Foo { \\\n" 3931 " void bar(); \\\n" 3932 "\\\n" 3933 "\\\n" 3934 "\\\n" 3935 " public: \\\n" 3936 " void baz(); \\\n" 3937 " };", 3938 DontAlign)); 3939 } 3940 3941 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3942 verifyFormat("#define A \\\n" 3943 " int v( \\\n" 3944 " a); \\\n" 3945 " int i;", 3946 getLLVMStyleWithColumns(11)); 3947 } 3948 3949 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3950 EXPECT_EQ( 3951 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3952 " \\\n" 3953 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3954 "\n" 3955 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3956 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3957 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3958 "\\\n" 3959 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3960 " \n" 3961 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3962 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3963 } 3964 3965 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3966 EXPECT_EQ("int\n" 3967 "#define A\n" 3968 " a;", 3969 format("int\n#define A\na;")); 3970 verifyFormat("functionCallTo(\n" 3971 " someOtherFunction(\n" 3972 " withSomeParameters, whichInSequence,\n" 3973 " areLongerThanALine(andAnotherCall,\n" 3974 "#define A B\n" 3975 " withMoreParamters,\n" 3976 " whichStronglyInfluenceTheLayout),\n" 3977 " andMoreParameters),\n" 3978 " trailing);", 3979 getLLVMStyleWithColumns(69)); 3980 verifyFormat("Foo::Foo()\n" 3981 "#ifdef BAR\n" 3982 " : baz(0)\n" 3983 "#endif\n" 3984 "{\n" 3985 "}"); 3986 verifyFormat("void f() {\n" 3987 " if (true)\n" 3988 "#ifdef A\n" 3989 " f(42);\n" 3990 " x();\n" 3991 "#else\n" 3992 " g();\n" 3993 " x();\n" 3994 "#endif\n" 3995 "}"); 3996 verifyFormat("void f(param1, param2,\n" 3997 " param3,\n" 3998 "#ifdef A\n" 3999 " param4(param5,\n" 4000 "#ifdef A1\n" 4001 " param6,\n" 4002 "#ifdef A2\n" 4003 " param7),\n" 4004 "#else\n" 4005 " param8),\n" 4006 " param9,\n" 4007 "#endif\n" 4008 " param10,\n" 4009 "#endif\n" 4010 " param11)\n" 4011 "#else\n" 4012 " param12)\n" 4013 "#endif\n" 4014 "{\n" 4015 " x();\n" 4016 "}", 4017 getLLVMStyleWithColumns(28)); 4018 verifyFormat("#if 1\n" 4019 "int i;"); 4020 verifyFormat("#if 1\n" 4021 "#endif\n" 4022 "#if 1\n" 4023 "#else\n" 4024 "#endif\n"); 4025 verifyFormat("DEBUG({\n" 4026 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4027 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 4028 "});\n" 4029 "#if a\n" 4030 "#else\n" 4031 "#endif"); 4032 4033 verifyIncompleteFormat("void f(\n" 4034 "#if A\n" 4035 ");\n" 4036 "#else\n" 4037 "#endif"); 4038 } 4039 4040 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 4041 verifyFormat("#endif\n" 4042 "#if B"); 4043 } 4044 4045 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 4046 FormatStyle SingleLine = getLLVMStyle(); 4047 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 4048 verifyFormat("#if 0\n" 4049 "#elif 1\n" 4050 "#endif\n" 4051 "void foo() {\n" 4052 " if (test) foo2();\n" 4053 "}", 4054 SingleLine); 4055 } 4056 4057 TEST_F(FormatTest, LayoutBlockInsideParens) { 4058 verifyFormat("functionCall({ int i; });"); 4059 verifyFormat("functionCall({\n" 4060 " int i;\n" 4061 " int j;\n" 4062 "});"); 4063 verifyFormat("functionCall(\n" 4064 " {\n" 4065 " int i;\n" 4066 " int j;\n" 4067 " },\n" 4068 " aaaa, bbbb, cccc);"); 4069 verifyFormat("functionA(functionB({\n" 4070 " int i;\n" 4071 " int j;\n" 4072 " }),\n" 4073 " aaaa, bbbb, cccc);"); 4074 verifyFormat("functionCall(\n" 4075 " {\n" 4076 " int i;\n" 4077 " int j;\n" 4078 " },\n" 4079 " aaaa, bbbb, // comment\n" 4080 " cccc);"); 4081 verifyFormat("functionA(functionB({\n" 4082 " int i;\n" 4083 " int j;\n" 4084 " }),\n" 4085 " aaaa, bbbb, // comment\n" 4086 " cccc);"); 4087 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 4088 verifyFormat("functionCall(aaaa, bbbb, {\n" 4089 " int i;\n" 4090 " int j;\n" 4091 "});"); 4092 verifyFormat( 4093 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 4094 " {\n" 4095 " int i; // break\n" 4096 " },\n" 4097 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 4098 " ccccccccccccccccc));"); 4099 verifyFormat("DEBUG({\n" 4100 " if (a)\n" 4101 " f();\n" 4102 "});"); 4103 } 4104 4105 TEST_F(FormatTest, LayoutBlockInsideStatement) { 4106 EXPECT_EQ("SOME_MACRO { int i; }\n" 4107 "int i;", 4108 format(" SOME_MACRO {int i;} int i;")); 4109 } 4110 4111 TEST_F(FormatTest, LayoutNestedBlocks) { 4112 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 4113 " struct s {\n" 4114 " int i;\n" 4115 " };\n" 4116 " s kBitsToOs[] = {{10}};\n" 4117 " for (int i = 0; i < 10; ++i)\n" 4118 " return;\n" 4119 "}"); 4120 verifyFormat("call(parameter, {\n" 4121 " something();\n" 4122 " // Comment using all columns.\n" 4123 " somethingelse();\n" 4124 "});", 4125 getLLVMStyleWithColumns(40)); 4126 verifyFormat("DEBUG( //\n" 4127 " { f(); }, a);"); 4128 verifyFormat("DEBUG( //\n" 4129 " {\n" 4130 " f(); //\n" 4131 " },\n" 4132 " a);"); 4133 4134 EXPECT_EQ("call(parameter, {\n" 4135 " something();\n" 4136 " // Comment too\n" 4137 " // looooooooooong.\n" 4138 " somethingElse();\n" 4139 "});", 4140 format("call(parameter, {\n" 4141 " something();\n" 4142 " // Comment too looooooooooong.\n" 4143 " somethingElse();\n" 4144 "});", 4145 getLLVMStyleWithColumns(29))); 4146 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 4147 EXPECT_EQ("DEBUG({ // comment\n" 4148 " int i;\n" 4149 "});", 4150 format("DEBUG({ // comment\n" 4151 "int i;\n" 4152 "});")); 4153 EXPECT_EQ("DEBUG({\n" 4154 " int i;\n" 4155 "\n" 4156 " // comment\n" 4157 " int j;\n" 4158 "});", 4159 format("DEBUG({\n" 4160 " int i;\n" 4161 "\n" 4162 " // comment\n" 4163 " int j;\n" 4164 "});")); 4165 4166 verifyFormat("DEBUG({\n" 4167 " if (a)\n" 4168 " return;\n" 4169 "});"); 4170 verifyGoogleFormat("DEBUG({\n" 4171 " if (a) return;\n" 4172 "});"); 4173 FormatStyle Style = getGoogleStyle(); 4174 Style.ColumnLimit = 45; 4175 verifyFormat("Debug(\n" 4176 " aaaaa,\n" 4177 " {\n" 4178 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 4179 " },\n" 4180 " a);", 4181 Style); 4182 4183 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 4184 4185 verifyNoCrash("^{v^{a}}"); 4186 } 4187 4188 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 4189 EXPECT_EQ("#define MACRO() \\\n" 4190 " Debug(aaa, /* force line break */ \\\n" 4191 " { \\\n" 4192 " int i; \\\n" 4193 " int j; \\\n" 4194 " })", 4195 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 4196 " { int i; int j; })", 4197 getGoogleStyle())); 4198 4199 EXPECT_EQ("#define A \\\n" 4200 " [] { \\\n" 4201 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 4202 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 4203 " }", 4204 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 4205 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 4206 getGoogleStyle())); 4207 } 4208 4209 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 4210 EXPECT_EQ("{}", format("{}")); 4211 verifyFormat("enum E {};"); 4212 verifyFormat("enum E {}"); 4213 FormatStyle Style = getLLVMStyle(); 4214 Style.SpaceInEmptyBlock = true; 4215 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 4216 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 4217 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 4218 } 4219 4220 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 4221 FormatStyle Style = getLLVMStyle(); 4222 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 4223 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 4224 verifyFormat("FOO_BEGIN\n" 4225 " FOO_ENTRY\n" 4226 "FOO_END", 4227 Style); 4228 verifyFormat("FOO_BEGIN\n" 4229 " NESTED_FOO_BEGIN\n" 4230 " NESTED_FOO_ENTRY\n" 4231 " NESTED_FOO_END\n" 4232 "FOO_END", 4233 Style); 4234 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 4235 " int x;\n" 4236 " x = 1;\n" 4237 "FOO_END(Baz)", 4238 Style); 4239 } 4240 4241 //===----------------------------------------------------------------------===// 4242 // Line break tests. 4243 //===----------------------------------------------------------------------===// 4244 4245 TEST_F(FormatTest, PreventConfusingIndents) { 4246 verifyFormat( 4247 "void f() {\n" 4248 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 4249 " parameter, parameter, parameter)),\n" 4250 " SecondLongCall(parameter));\n" 4251 "}"); 4252 verifyFormat( 4253 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4254 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4255 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4256 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 4257 verifyFormat( 4258 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4259 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 4260 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 4261 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 4262 verifyFormat( 4263 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4264 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 4265 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 4266 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 4267 verifyFormat("int a = bbbb && ccc &&\n" 4268 " fffff(\n" 4269 "#define A Just forcing a new line\n" 4270 " ddd);"); 4271 } 4272 4273 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 4274 verifyFormat( 4275 "bool aaaaaaa =\n" 4276 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 4277 " bbbbbbbb();"); 4278 verifyFormat( 4279 "bool aaaaaaa =\n" 4280 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 4281 " bbbbbbbb();"); 4282 4283 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 4284 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 4285 " ccccccccc == ddddddddddd;"); 4286 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 4287 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 4288 " ccccccccc == ddddddddddd;"); 4289 verifyFormat( 4290 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4291 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 4292 " ccccccccc == ddddddddddd;"); 4293 4294 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 4295 " aaaaaa) &&\n" 4296 " bbbbbb && cccccc;"); 4297 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 4298 " aaaaaa) >>\n" 4299 " bbbbbb;"); 4300 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 4301 " SourceMgr.getSpellingColumnNumber(\n" 4302 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 4303 " 1);"); 4304 4305 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4306 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 4307 " cccccc) {\n}"); 4308 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4309 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 4310 " cccccc) {\n}"); 4311 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4312 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 4313 " cccccc) {\n}"); 4314 verifyFormat("b = a &&\n" 4315 " // Comment\n" 4316 " b.c && d;"); 4317 4318 // If the LHS of a comparison is not a binary expression itself, the 4319 // additional linebreak confuses many people. 4320 verifyFormat( 4321 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4322 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 4323 "}"); 4324 verifyFormat( 4325 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4326 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 4327 "}"); 4328 verifyFormat( 4329 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 4330 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 4331 "}"); 4332 verifyFormat( 4333 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4334 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 4335 "}"); 4336 // Even explicit parentheses stress the precedence enough to make the 4337 // additional break unnecessary. 4338 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4339 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 4340 "}"); 4341 // This cases is borderline, but with the indentation it is still readable. 4342 verifyFormat( 4343 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4344 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4345 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 4346 "}", 4347 getLLVMStyleWithColumns(75)); 4348 4349 // If the LHS is a binary expression, we should still use the additional break 4350 // as otherwise the formatting hides the operator precedence. 4351 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4352 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4353 " 5) {\n" 4354 "}"); 4355 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4356 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 4357 " 5) {\n" 4358 "}"); 4359 4360 FormatStyle OnePerLine = getLLVMStyle(); 4361 OnePerLine.BinPackParameters = false; 4362 verifyFormat( 4363 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4365 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 4366 OnePerLine); 4367 4368 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 4369 " .aaa(aaaaaaaaaaaaa) *\n" 4370 " aaaaaaa +\n" 4371 " aaaaaaa;", 4372 getLLVMStyleWithColumns(40)); 4373 } 4374 4375 TEST_F(FormatTest, ExpressionIndentation) { 4376 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4378 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4379 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4380 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 4381 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 4382 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4383 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 4384 " ccccccccccccccccccccccccccccccccccccccccc;"); 4385 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4387 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4388 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 4389 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4390 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4391 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4392 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 4393 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4394 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4395 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4396 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 4397 verifyFormat("if () {\n" 4398 "} else if (aaaaa && bbbbb > // break\n" 4399 " ccccc) {\n" 4400 "}"); 4401 verifyFormat("if () {\n" 4402 "} else if constexpr (aaaaa && bbbbb > // break\n" 4403 " ccccc) {\n" 4404 "}"); 4405 verifyFormat("if () {\n" 4406 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 4407 " ccccc) {\n" 4408 "}"); 4409 verifyFormat("if () {\n" 4410 "} else if (aaaaa &&\n" 4411 " bbbbb > // break\n" 4412 " ccccc &&\n" 4413 " ddddd) {\n" 4414 "}"); 4415 4416 // Presence of a trailing comment used to change indentation of b. 4417 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 4418 " b;\n" 4419 "return aaaaaaaaaaaaaaaaaaa +\n" 4420 " b; //", 4421 getLLVMStyleWithColumns(30)); 4422 } 4423 4424 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 4425 // Not sure what the best system is here. Like this, the LHS can be found 4426 // immediately above an operator (everything with the same or a higher 4427 // indent). The RHS is aligned right of the operator and so compasses 4428 // everything until something with the same indent as the operator is found. 4429 // FIXME: Is this a good system? 4430 FormatStyle Style = getLLVMStyle(); 4431 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 4432 verifyFormat( 4433 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4434 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4435 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4436 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4437 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4438 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4439 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4440 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4441 " > ccccccccccccccccccccccccccccccccccccccccc;", 4442 Style); 4443 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4444 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4445 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4446 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4447 Style); 4448 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4449 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4450 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4451 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4452 Style); 4453 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4454 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4455 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4456 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4457 Style); 4458 verifyFormat("if () {\n" 4459 "} else if (aaaaa\n" 4460 " && bbbbb // break\n" 4461 " > ccccc) {\n" 4462 "}", 4463 Style); 4464 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4465 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 4466 Style); 4467 verifyFormat("return (a)\n" 4468 " // comment\n" 4469 " + b;", 4470 Style); 4471 verifyFormat( 4472 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4473 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4474 " + cc;", 4475 Style); 4476 4477 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4478 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4479 Style); 4480 4481 // Forced by comments. 4482 verifyFormat( 4483 "unsigned ContentSize =\n" 4484 " sizeof(int16_t) // DWARF ARange version number\n" 4485 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 4486 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 4487 " + sizeof(int8_t); // Segment Size (in bytes)"); 4488 4489 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 4490 " == boost::fusion::at_c<1>(iiii).second;", 4491 Style); 4492 4493 Style.ColumnLimit = 60; 4494 verifyFormat("zzzzzzzzzz\n" 4495 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4496 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 4497 Style); 4498 4499 Style.ColumnLimit = 80; 4500 Style.IndentWidth = 4; 4501 Style.TabWidth = 4; 4502 Style.UseTab = FormatStyle::UT_Always; 4503 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4504 Style.AlignOperands = FormatStyle::OAS_DontAlign; 4505 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 4506 "\t&& (someOtherLongishConditionPart1\n" 4507 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 4508 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 4509 "(someOtherLongishConditionPart1 || " 4510 "someOtherEvenLongerNestedConditionPart2);", 4511 Style)); 4512 } 4513 4514 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 4515 FormatStyle Style = getLLVMStyle(); 4516 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 4517 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 4518 4519 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4520 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4521 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4522 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4523 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4524 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4525 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4526 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4527 " > ccccccccccccccccccccccccccccccccccccccccc;", 4528 Style); 4529 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4530 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4531 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4532 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4533 Style); 4534 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4535 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4536 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4537 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4538 Style); 4539 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4540 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4541 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4542 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4543 Style); 4544 verifyFormat("if () {\n" 4545 "} else if (aaaaa\n" 4546 " && bbbbb // break\n" 4547 " > ccccc) {\n" 4548 "}", 4549 Style); 4550 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4551 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 4552 Style); 4553 verifyFormat("return (a)\n" 4554 " // comment\n" 4555 " + b;", 4556 Style); 4557 verifyFormat( 4558 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4559 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4560 " + cc;", 4561 Style); 4562 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 4563 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 4564 " : 3333333333333333;", 4565 Style); 4566 verifyFormat( 4567 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 4568 " : ccccccccccccccc ? dddddddddddddddddd\n" 4569 " : eeeeeeeeeeeeeeeeee)\n" 4570 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 4571 " : 3333333333333333;", 4572 Style); 4573 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4574 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4575 Style); 4576 4577 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 4578 " == boost::fusion::at_c<1>(iiii).second;", 4579 Style); 4580 4581 Style.ColumnLimit = 60; 4582 verifyFormat("zzzzzzzzzzzzz\n" 4583 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4584 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 4585 Style); 4586 4587 // Forced by comments. 4588 Style.ColumnLimit = 80; 4589 verifyFormat( 4590 "unsigned ContentSize\n" 4591 " = sizeof(int16_t) // DWARF ARange version number\n" 4592 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 4593 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 4594 " + sizeof(int8_t); // Segment Size (in bytes)", 4595 Style); 4596 4597 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 4598 verifyFormat( 4599 "unsigned ContentSize =\n" 4600 " sizeof(int16_t) // DWARF ARange version number\n" 4601 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 4602 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 4603 " + sizeof(int8_t); // Segment Size (in bytes)", 4604 Style); 4605 4606 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 4607 verifyFormat( 4608 "unsigned ContentSize =\n" 4609 " sizeof(int16_t) // DWARF ARange version number\n" 4610 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 4611 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 4612 " + sizeof(int8_t); // Segment Size (in bytes)", 4613 Style); 4614 } 4615 4616 TEST_F(FormatTest, EnforcedOperatorWraps) { 4617 // Here we'd like to wrap after the || operators, but a comment is forcing an 4618 // earlier wrap. 4619 verifyFormat("bool x = aaaaa //\n" 4620 " || bbbbb\n" 4621 " //\n" 4622 " || cccc;"); 4623 } 4624 4625 TEST_F(FormatTest, NoOperandAlignment) { 4626 FormatStyle Style = getLLVMStyle(); 4627 Style.AlignOperands = FormatStyle::OAS_DontAlign; 4628 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 4629 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4630 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4631 Style); 4632 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 4633 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4634 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4635 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4636 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4637 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4638 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4639 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4640 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4641 " > ccccccccccccccccccccccccccccccccccccccccc;", 4642 Style); 4643 4644 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4645 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4646 " + cc;", 4647 Style); 4648 verifyFormat("int a = aa\n" 4649 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4650 " * cccccccccccccccccccccccccccccccccccc;\n", 4651 Style); 4652 4653 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4654 verifyFormat("return (a > b\n" 4655 " // comment1\n" 4656 " // comment2\n" 4657 " || c);", 4658 Style); 4659 } 4660 4661 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 4662 FormatStyle Style = getLLVMStyle(); 4663 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 4664 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4665 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4666 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 4667 Style); 4668 } 4669 4670 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 4671 FormatStyle Style = getLLVMStyle(); 4672 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 4673 Style.BinPackArguments = false; 4674 Style.ColumnLimit = 40; 4675 verifyFormat("void test() {\n" 4676 " someFunction(\n" 4677 " this + argument + is + quite\n" 4678 " + long + so + it + gets + wrapped\n" 4679 " + but + remains + bin - packed);\n" 4680 "}", 4681 Style); 4682 verifyFormat("void test() {\n" 4683 " someFunction(arg1,\n" 4684 " this + argument + is\n" 4685 " + quite + long + so\n" 4686 " + it + gets + wrapped\n" 4687 " + but + remains + bin\n" 4688 " - packed,\n" 4689 " arg3);\n" 4690 "}", 4691 Style); 4692 verifyFormat("void test() {\n" 4693 " someFunction(\n" 4694 " arg1,\n" 4695 " this + argument + has\n" 4696 " + anotherFunc(nested,\n" 4697 " calls + whose\n" 4698 " + arguments\n" 4699 " + are + also\n" 4700 " + wrapped,\n" 4701 " in + addition)\n" 4702 " + to + being + bin - packed,\n" 4703 " arg3);\n" 4704 "}", 4705 Style); 4706 4707 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 4708 verifyFormat("void test() {\n" 4709 " someFunction(\n" 4710 " arg1,\n" 4711 " this + argument + has +\n" 4712 " anotherFunc(nested,\n" 4713 " calls + whose +\n" 4714 " arguments +\n" 4715 " are + also +\n" 4716 " wrapped,\n" 4717 " in + addition) +\n" 4718 " to + being + bin - packed,\n" 4719 " arg3);\n" 4720 "}", 4721 Style); 4722 } 4723 4724 TEST_F(FormatTest, ConstructorInitializers) { 4725 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 4726 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 4727 getLLVMStyleWithColumns(45)); 4728 verifyFormat("Constructor()\n" 4729 " : Inttializer(FitsOnTheLine) {}", 4730 getLLVMStyleWithColumns(44)); 4731 verifyFormat("Constructor()\n" 4732 " : Inttializer(FitsOnTheLine) {}", 4733 getLLVMStyleWithColumns(43)); 4734 4735 verifyFormat("template <typename T>\n" 4736 "Constructor() : Initializer(FitsOnTheLine) {}", 4737 getLLVMStyleWithColumns(45)); 4738 4739 verifyFormat( 4740 "SomeClass::Constructor()\n" 4741 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 4742 4743 verifyFormat( 4744 "SomeClass::Constructor()\n" 4745 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4746 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 4747 verifyFormat( 4748 "SomeClass::Constructor()\n" 4749 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4750 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 4751 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4752 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4753 " : aaaaaaaaaa(aaaaaa) {}"); 4754 4755 verifyFormat("Constructor()\n" 4756 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4757 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4758 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4759 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 4760 4761 verifyFormat("Constructor()\n" 4762 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4763 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4764 4765 verifyFormat("Constructor(int Parameter = 0)\n" 4766 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 4767 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 4768 verifyFormat("Constructor()\n" 4769 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 4770 "}", 4771 getLLVMStyleWithColumns(60)); 4772 verifyFormat("Constructor()\n" 4773 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4774 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 4775 4776 // Here a line could be saved by splitting the second initializer onto two 4777 // lines, but that is not desirable. 4778 verifyFormat("Constructor()\n" 4779 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 4780 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 4781 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4782 4783 FormatStyle OnePerLine = getLLVMStyle(); 4784 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4785 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 4786 verifyFormat("SomeClass::Constructor()\n" 4787 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4788 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4789 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4790 OnePerLine); 4791 verifyFormat("SomeClass::Constructor()\n" 4792 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 4793 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4794 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4795 OnePerLine); 4796 verifyFormat("MyClass::MyClass(int var)\n" 4797 " : some_var_(var), // 4 space indent\n" 4798 " some_other_var_(var + 1) { // lined up\n" 4799 "}", 4800 OnePerLine); 4801 verifyFormat("Constructor()\n" 4802 " : aaaaa(aaaaaa),\n" 4803 " aaaaa(aaaaaa),\n" 4804 " aaaaa(aaaaaa),\n" 4805 " aaaaa(aaaaaa),\n" 4806 " aaaaa(aaaaaa) {}", 4807 OnePerLine); 4808 verifyFormat("Constructor()\n" 4809 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 4810 " aaaaaaaaaaaaaaaaaaaaaa) {}", 4811 OnePerLine); 4812 OnePerLine.BinPackParameters = false; 4813 verifyFormat( 4814 "Constructor()\n" 4815 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4816 " aaaaaaaaaaa().aaa(),\n" 4817 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4818 OnePerLine); 4819 OnePerLine.ColumnLimit = 60; 4820 verifyFormat("Constructor()\n" 4821 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 4822 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 4823 OnePerLine); 4824 4825 EXPECT_EQ("Constructor()\n" 4826 " : // Comment forcing unwanted break.\n" 4827 " aaaa(aaaa) {}", 4828 format("Constructor() :\n" 4829 " // Comment forcing unwanted break.\n" 4830 " aaaa(aaaa) {}")); 4831 } 4832 4833 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 4834 FormatStyle Style = getLLVMStyle(); 4835 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 4836 Style.ColumnLimit = 60; 4837 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4838 Style.AllowAllConstructorInitializersOnNextLine = true; 4839 Style.BinPackParameters = false; 4840 4841 for (int i = 0; i < 4; ++i) { 4842 // Test all combinations of parameters that should not have an effect. 4843 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 4844 Style.AllowAllArgumentsOnNextLine = i & 2; 4845 4846 Style.AllowAllConstructorInitializersOnNextLine = true; 4847 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 4848 verifyFormat("Constructor()\n" 4849 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4850 Style); 4851 verifyFormat("Constructor() : a(a), b(b) {}", Style); 4852 4853 Style.AllowAllConstructorInitializersOnNextLine = false; 4854 verifyFormat("Constructor()\n" 4855 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 4856 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 4857 Style); 4858 verifyFormat("Constructor() : a(a), b(b) {}", Style); 4859 4860 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 4861 Style.AllowAllConstructorInitializersOnNextLine = true; 4862 verifyFormat("Constructor()\n" 4863 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4864 Style); 4865 4866 Style.AllowAllConstructorInitializersOnNextLine = false; 4867 verifyFormat("Constructor()\n" 4868 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 4869 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4870 Style); 4871 4872 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 4873 Style.AllowAllConstructorInitializersOnNextLine = true; 4874 verifyFormat("Constructor() :\n" 4875 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4876 Style); 4877 4878 Style.AllowAllConstructorInitializersOnNextLine = false; 4879 verifyFormat("Constructor() :\n" 4880 " aaaaaaaaaaaaaaaaaa(a),\n" 4881 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4882 Style); 4883 } 4884 4885 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 4886 // AllowAllConstructorInitializersOnNextLine in all 4887 // BreakConstructorInitializers modes 4888 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 4889 Style.AllowAllParametersOfDeclarationOnNextLine = true; 4890 Style.AllowAllConstructorInitializersOnNextLine = false; 4891 verifyFormat("SomeClassWithALongName::Constructor(\n" 4892 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 4893 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 4894 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 4895 Style); 4896 4897 Style.AllowAllConstructorInitializersOnNextLine = true; 4898 verifyFormat("SomeClassWithALongName::Constructor(\n" 4899 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4900 " int bbbbbbbbbbbbb,\n" 4901 " int cccccccccccccccc)\n" 4902 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4903 Style); 4904 4905 Style.AllowAllParametersOfDeclarationOnNextLine = false; 4906 Style.AllowAllConstructorInitializersOnNextLine = false; 4907 verifyFormat("SomeClassWithALongName::Constructor(\n" 4908 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4909 " int bbbbbbbbbbbbb)\n" 4910 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 4911 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 4912 Style); 4913 4914 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 4915 4916 Style.AllowAllParametersOfDeclarationOnNextLine = true; 4917 verifyFormat("SomeClassWithALongName::Constructor(\n" 4918 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 4919 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 4920 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4921 Style); 4922 4923 Style.AllowAllConstructorInitializersOnNextLine = true; 4924 verifyFormat("SomeClassWithALongName::Constructor(\n" 4925 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4926 " int bbbbbbbbbbbbb,\n" 4927 " int cccccccccccccccc)\n" 4928 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4929 Style); 4930 4931 Style.AllowAllParametersOfDeclarationOnNextLine = false; 4932 Style.AllowAllConstructorInitializersOnNextLine = false; 4933 verifyFormat("SomeClassWithALongName::Constructor(\n" 4934 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4935 " int bbbbbbbbbbbbb)\n" 4936 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 4937 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4938 Style); 4939 4940 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 4941 Style.AllowAllParametersOfDeclarationOnNextLine = true; 4942 verifyFormat("SomeClassWithALongName::Constructor(\n" 4943 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 4944 " aaaaaaaaaaaaaaaaaaaa(a),\n" 4945 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4946 Style); 4947 4948 Style.AllowAllConstructorInitializersOnNextLine = true; 4949 verifyFormat("SomeClassWithALongName::Constructor(\n" 4950 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4951 " int bbbbbbbbbbbbb,\n" 4952 " int cccccccccccccccc) :\n" 4953 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4954 Style); 4955 4956 Style.AllowAllParametersOfDeclarationOnNextLine = false; 4957 Style.AllowAllConstructorInitializersOnNextLine = false; 4958 verifyFormat("SomeClassWithALongName::Constructor(\n" 4959 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4960 " int bbbbbbbbbbbbb) :\n" 4961 " aaaaaaaaaaaaaaaaaaaa(a),\n" 4962 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4963 Style); 4964 } 4965 4966 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 4967 FormatStyle Style = getLLVMStyle(); 4968 Style.ColumnLimit = 60; 4969 Style.BinPackArguments = false; 4970 for (int i = 0; i < 4; ++i) { 4971 // Test all combinations of parameters that should not have an effect. 4972 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 4973 Style.AllowAllConstructorInitializersOnNextLine = i & 2; 4974 4975 Style.AllowAllArgumentsOnNextLine = true; 4976 verifyFormat("void foo() {\n" 4977 " FunctionCallWithReallyLongName(\n" 4978 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 4979 "}", 4980 Style); 4981 Style.AllowAllArgumentsOnNextLine = false; 4982 verifyFormat("void foo() {\n" 4983 " FunctionCallWithReallyLongName(\n" 4984 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4985 " bbbbbbbbbbbb);\n" 4986 "}", 4987 Style); 4988 4989 Style.AllowAllArgumentsOnNextLine = true; 4990 verifyFormat("void foo() {\n" 4991 " auto VariableWithReallyLongName = {\n" 4992 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 4993 "}", 4994 Style); 4995 Style.AllowAllArgumentsOnNextLine = false; 4996 verifyFormat("void foo() {\n" 4997 " auto VariableWithReallyLongName = {\n" 4998 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4999 " bbbbbbbbbbbb};\n" 5000 "}", 5001 Style); 5002 } 5003 5004 // This parameter should not affect declarations. 5005 Style.BinPackParameters = false; 5006 Style.AllowAllArgumentsOnNextLine = false; 5007 Style.AllowAllParametersOfDeclarationOnNextLine = true; 5008 verifyFormat("void FunctionCallWithReallyLongName(\n" 5009 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 5010 Style); 5011 Style.AllowAllParametersOfDeclarationOnNextLine = false; 5012 verifyFormat("void FunctionCallWithReallyLongName(\n" 5013 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 5014 " int bbbbbbbbbbbb);", 5015 Style); 5016 } 5017 5018 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 5019 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 5020 // and BAS_Align. 5021 auto Style = getLLVMStyle(); 5022 Style.ColumnLimit = 35; 5023 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 5024 "void functionDecl(int A, int B, int C);"; 5025 Style.AllowAllArgumentsOnNextLine = false; 5026 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5027 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 5028 " paramC);\n" 5029 "void functionDecl(int A, int B,\n" 5030 " int C);"), 5031 format(Input, Style)); 5032 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 5033 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 5034 " paramC);\n" 5035 "void functionDecl(int A, int B,\n" 5036 " int C);"), 5037 format(Input, Style)); 5038 // However, BAS_AlwaysBreak should take precedence over 5039 // AllowAllArgumentsOnNextLine. 5040 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5041 EXPECT_EQ(StringRef("functionCall(\n" 5042 " paramA, paramB, paramC);\n" 5043 "void functionDecl(\n" 5044 " int A, int B, int C);"), 5045 format(Input, Style)); 5046 5047 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 5048 // first argument. 5049 Style.AllowAllArgumentsOnNextLine = true; 5050 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5051 EXPECT_EQ(StringRef("functionCall(\n" 5052 " paramA, paramB, paramC);\n" 5053 "void functionDecl(\n" 5054 " int A, int B, int C);"), 5055 format(Input, Style)); 5056 // It wouldn't fit on one line with aligned parameters so this setting 5057 // doesn't change anything for BAS_Align. 5058 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 5059 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 5060 " paramC);\n" 5061 "void functionDecl(int A, int B,\n" 5062 " int C);"), 5063 format(Input, Style)); 5064 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5065 EXPECT_EQ(StringRef("functionCall(\n" 5066 " paramA, paramB, paramC);\n" 5067 "void functionDecl(\n" 5068 " int A, int B, int C);"), 5069 format(Input, Style)); 5070 } 5071 5072 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 5073 FormatStyle Style = getLLVMStyle(); 5074 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 5075 5076 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 5077 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 5078 getStyleWithColumns(Style, 45)); 5079 verifyFormat("Constructor() :\n" 5080 " Initializer(FitsOnTheLine) {}", 5081 getStyleWithColumns(Style, 44)); 5082 verifyFormat("Constructor() :\n" 5083 " Initializer(FitsOnTheLine) {}", 5084 getStyleWithColumns(Style, 43)); 5085 5086 verifyFormat("template <typename T>\n" 5087 "Constructor() : Initializer(FitsOnTheLine) {}", 5088 getStyleWithColumns(Style, 50)); 5089 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5090 verifyFormat( 5091 "SomeClass::Constructor() :\n" 5092 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 5093 Style); 5094 5095 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false; 5096 verifyFormat( 5097 "SomeClass::Constructor() :\n" 5098 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 5099 Style); 5100 5101 verifyFormat( 5102 "SomeClass::Constructor() :\n" 5103 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5104 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5105 Style); 5106 verifyFormat( 5107 "SomeClass::Constructor() :\n" 5108 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5109 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 5110 Style); 5111 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5112 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5113 " aaaaaaaaaa(aaaaaa) {}", 5114 Style); 5115 5116 verifyFormat("Constructor() :\n" 5117 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5118 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5119 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5120 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 5121 Style); 5122 5123 verifyFormat("Constructor() :\n" 5124 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5125 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5126 Style); 5127 5128 verifyFormat("Constructor(int Parameter = 0) :\n" 5129 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 5130 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 5131 Style); 5132 verifyFormat("Constructor() :\n" 5133 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 5134 "}", 5135 getStyleWithColumns(Style, 60)); 5136 verifyFormat("Constructor() :\n" 5137 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5138 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 5139 Style); 5140 5141 // Here a line could be saved by splitting the second initializer onto two 5142 // lines, but that is not desirable. 5143 verifyFormat("Constructor() :\n" 5144 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 5145 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 5146 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5147 Style); 5148 5149 FormatStyle OnePerLine = Style; 5150 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5151 OnePerLine.AllowAllConstructorInitializersOnNextLine = false; 5152 verifyFormat("SomeClass::Constructor() :\n" 5153 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5154 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5155 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5156 OnePerLine); 5157 verifyFormat("SomeClass::Constructor() :\n" 5158 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 5159 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5160 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5161 OnePerLine); 5162 verifyFormat("MyClass::MyClass(int var) :\n" 5163 " some_var_(var), // 4 space indent\n" 5164 " some_other_var_(var + 1) { // lined up\n" 5165 "}", 5166 OnePerLine); 5167 verifyFormat("Constructor() :\n" 5168 " aaaaa(aaaaaa),\n" 5169 " aaaaa(aaaaaa),\n" 5170 " aaaaa(aaaaaa),\n" 5171 " aaaaa(aaaaaa),\n" 5172 " aaaaa(aaaaaa) {}", 5173 OnePerLine); 5174 verifyFormat("Constructor() :\n" 5175 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5176 " aaaaaaaaaaaaaaaaaaaaaa) {}", 5177 OnePerLine); 5178 OnePerLine.BinPackParameters = false; 5179 verifyFormat("Constructor() :\n" 5180 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5181 " aaaaaaaaaaa().aaa(),\n" 5182 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5183 OnePerLine); 5184 OnePerLine.ColumnLimit = 60; 5185 verifyFormat("Constructor() :\n" 5186 " aaaaaaaaaaaaaaaaaaaa(a),\n" 5187 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 5188 OnePerLine); 5189 5190 EXPECT_EQ("Constructor() :\n" 5191 " // Comment forcing unwanted break.\n" 5192 " aaaa(aaaa) {}", 5193 format("Constructor() :\n" 5194 " // Comment forcing unwanted break.\n" 5195 " aaaa(aaaa) {}", 5196 Style)); 5197 5198 Style.ColumnLimit = 0; 5199 verifyFormat("SomeClass::Constructor() :\n" 5200 " a(a) {}", 5201 Style); 5202 verifyFormat("SomeClass::Constructor() noexcept :\n" 5203 " a(a) {}", 5204 Style); 5205 verifyFormat("SomeClass::Constructor() :\n" 5206 " a(a), b(b), c(c) {}", 5207 Style); 5208 verifyFormat("SomeClass::Constructor() :\n" 5209 " a(a) {\n" 5210 " foo();\n" 5211 " bar();\n" 5212 "}", 5213 Style); 5214 5215 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 5216 verifyFormat("SomeClass::Constructor() :\n" 5217 " a(a), b(b), c(c) {\n" 5218 "}", 5219 Style); 5220 verifyFormat("SomeClass::Constructor() :\n" 5221 " a(a) {\n" 5222 "}", 5223 Style); 5224 5225 Style.ColumnLimit = 80; 5226 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 5227 Style.ConstructorInitializerIndentWidth = 2; 5228 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 5229 verifyFormat("SomeClass::Constructor() :\n" 5230 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5231 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 5232 Style); 5233 5234 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 5235 // well 5236 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 5237 verifyFormat( 5238 "class SomeClass\n" 5239 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5240 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 5241 Style); 5242 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 5243 verifyFormat( 5244 "class SomeClass\n" 5245 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5246 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 5247 Style); 5248 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 5249 verifyFormat( 5250 "class SomeClass :\n" 5251 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5252 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 5253 Style); 5254 } 5255 5256 #ifndef EXPENSIVE_CHECKS 5257 // Expensive checks enables libstdc++ checking which includes validating the 5258 // state of ranges used in std::priority_queue - this blows out the 5259 // runtime/scalability of the function and makes this test unacceptably slow. 5260 TEST_F(FormatTest, MemoizationTests) { 5261 // This breaks if the memoization lookup does not take \c Indent and 5262 // \c LastSpace into account. 5263 verifyFormat( 5264 "extern CFRunLoopTimerRef\n" 5265 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 5266 " CFTimeInterval interval, CFOptionFlags flags,\n" 5267 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 5268 " CFRunLoopTimerContext *context) {}"); 5269 5270 // Deep nesting somewhat works around our memoization. 5271 verifyFormat( 5272 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 5273 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 5274 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 5275 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 5276 " aaaaa())))))))))))))))))))))))))))))))))))))));", 5277 getLLVMStyleWithColumns(65)); 5278 verifyFormat( 5279 "aaaaa(\n" 5280 " aaaaa,\n" 5281 " aaaaa(\n" 5282 " aaaaa,\n" 5283 " aaaaa(\n" 5284 " aaaaa,\n" 5285 " aaaaa(\n" 5286 " aaaaa,\n" 5287 " aaaaa(\n" 5288 " aaaaa,\n" 5289 " aaaaa(\n" 5290 " aaaaa,\n" 5291 " aaaaa(\n" 5292 " aaaaa,\n" 5293 " aaaaa(\n" 5294 " aaaaa,\n" 5295 " aaaaa(\n" 5296 " aaaaa,\n" 5297 " aaaaa(\n" 5298 " aaaaa,\n" 5299 " aaaaa(\n" 5300 " aaaaa,\n" 5301 " aaaaa(\n" 5302 " aaaaa,\n" 5303 " aaaaa))))))))))));", 5304 getLLVMStyleWithColumns(65)); 5305 verifyFormat( 5306 "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" 5307 " a),\n" 5308 " a),\n" 5309 " a),\n" 5310 " a),\n" 5311 " a),\n" 5312 " a),\n" 5313 " a),\n" 5314 " a),\n" 5315 " a),\n" 5316 " a),\n" 5317 " a),\n" 5318 " a),\n" 5319 " a),\n" 5320 " a),\n" 5321 " a),\n" 5322 " a),\n" 5323 " a)", 5324 getLLVMStyleWithColumns(65)); 5325 5326 // This test takes VERY long when memoization is broken. 5327 FormatStyle OnePerLine = getLLVMStyle(); 5328 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5329 OnePerLine.BinPackParameters = false; 5330 std::string input = "Constructor()\n" 5331 " : aaaa(a,\n"; 5332 for (unsigned i = 0, e = 80; i != e; ++i) { 5333 input += " a,\n"; 5334 } 5335 input += " a) {}"; 5336 verifyFormat(input, OnePerLine); 5337 } 5338 #endif 5339 5340 TEST_F(FormatTest, BreaksAsHighAsPossible) { 5341 verifyFormat( 5342 "void f() {\n" 5343 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 5344 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 5345 " f();\n" 5346 "}"); 5347 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 5348 " Intervals[i - 1].getRange().getLast()) {\n}"); 5349 } 5350 5351 TEST_F(FormatTest, BreaksFunctionDeclarations) { 5352 // Principially, we break function declarations in a certain order: 5353 // 1) break amongst arguments. 5354 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 5355 " Cccccccccccccc cccccccccccccc);"); 5356 verifyFormat("template <class TemplateIt>\n" 5357 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 5358 " TemplateIt *stop) {}"); 5359 5360 // 2) break after return type. 5361 verifyFormat( 5362 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5363 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 5364 getGoogleStyle()); 5365 5366 // 3) break after (. 5367 verifyFormat( 5368 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 5369 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 5370 getGoogleStyle()); 5371 5372 // 4) break before after nested name specifiers. 5373 verifyFormat( 5374 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5375 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 5376 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 5377 getGoogleStyle()); 5378 5379 // However, there are exceptions, if a sufficient amount of lines can be 5380 // saved. 5381 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 5382 // more adjusting. 5383 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 5384 " Cccccccccccccc cccccccccc,\n" 5385 " Cccccccccccccc cccccccccc,\n" 5386 " Cccccccccccccc cccccccccc,\n" 5387 " Cccccccccccccc cccccccccc);"); 5388 verifyFormat( 5389 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5390 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 5391 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 5392 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 5393 getGoogleStyle()); 5394 verifyFormat( 5395 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 5396 " Cccccccccccccc cccccccccc,\n" 5397 " Cccccccccccccc cccccccccc,\n" 5398 " Cccccccccccccc cccccccccc,\n" 5399 " Cccccccccccccc cccccccccc,\n" 5400 " Cccccccccccccc cccccccccc,\n" 5401 " Cccccccccccccc cccccccccc);"); 5402 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 5403 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 5404 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 5405 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 5406 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 5407 5408 // Break after multi-line parameters. 5409 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5410 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5412 " bbbb bbbb);"); 5413 verifyFormat("void SomeLoooooooooooongFunction(\n" 5414 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5415 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5416 " int bbbbbbbbbbbbb);"); 5417 5418 // Treat overloaded operators like other functions. 5419 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 5420 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 5421 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 5422 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 5423 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 5424 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 5425 verifyGoogleFormat( 5426 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 5427 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 5428 verifyGoogleFormat( 5429 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 5430 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 5431 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5432 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 5433 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 5434 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 5435 verifyGoogleFormat( 5436 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 5437 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5438 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 5439 verifyGoogleFormat("template <typename T>\n" 5440 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5441 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 5442 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 5443 5444 FormatStyle Style = getLLVMStyle(); 5445 Style.PointerAlignment = FormatStyle::PAS_Left; 5446 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5447 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 5448 Style); 5449 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 5450 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5451 Style); 5452 } 5453 5454 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 5455 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 5456 // Prefer keeping `::` followed by `operator` together. 5457 EXPECT_EQ("const aaaa::bbbbbbb &\n" 5458 "ccccccccc::operator++() {\n" 5459 " stuff();\n" 5460 "}", 5461 format("const aaaa::bbbbbbb\n" 5462 "&ccccccccc::operator++() { stuff(); }", 5463 getLLVMStyleWithColumns(40))); 5464 } 5465 5466 TEST_F(FormatTest, TrailingReturnType) { 5467 verifyFormat("auto foo() -> int;\n"); 5468 // correct trailing return type spacing 5469 verifyFormat("auto operator->() -> int;\n"); 5470 verifyFormat("auto operator++(int) -> int;\n"); 5471 5472 verifyFormat("struct S {\n" 5473 " auto bar() const -> int;\n" 5474 "};"); 5475 verifyFormat("template <size_t Order, typename T>\n" 5476 "auto load_img(const std::string &filename)\n" 5477 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 5478 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 5479 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 5480 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 5481 verifyFormat("template <typename T>\n" 5482 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 5483 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 5484 5485 // Not trailing return types. 5486 verifyFormat("void f() { auto a = b->c(); }"); 5487 } 5488 5489 TEST_F(FormatTest, DeductionGuides) { 5490 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 5491 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 5492 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 5493 verifyFormat( 5494 "template <class... T>\n" 5495 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 5496 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 5497 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 5498 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 5499 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 5500 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 5501 verifyFormat("template <class T> x() -> x<1>;"); 5502 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 5503 5504 // Ensure not deduction guides. 5505 verifyFormat("c()->f<int>();"); 5506 verifyFormat("x()->foo<1>;"); 5507 verifyFormat("x = p->foo<3>();"); 5508 verifyFormat("x()->x<1>();"); 5509 verifyFormat("x()->x<1>;"); 5510 } 5511 5512 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 5513 // Avoid breaking before trailing 'const' or other trailing annotations, if 5514 // they are not function-like. 5515 FormatStyle Style = getGoogleStyle(); 5516 Style.ColumnLimit = 47; 5517 verifyFormat("void someLongFunction(\n" 5518 " int someLoooooooooooooongParameter) const {\n}", 5519 getLLVMStyleWithColumns(47)); 5520 verifyFormat("LoooooongReturnType\n" 5521 "someLoooooooongFunction() const {}", 5522 getLLVMStyleWithColumns(47)); 5523 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 5524 " const {}", 5525 Style); 5526 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 5527 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 5528 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 5529 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 5530 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 5531 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 5532 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 5533 " aaaaaaaaaaa aaaaa) const override;"); 5534 verifyGoogleFormat( 5535 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5536 " const override;"); 5537 5538 // Even if the first parameter has to be wrapped. 5539 verifyFormat("void someLongFunction(\n" 5540 " int someLongParameter) const {}", 5541 getLLVMStyleWithColumns(46)); 5542 verifyFormat("void someLongFunction(\n" 5543 " int someLongParameter) const {}", 5544 Style); 5545 verifyFormat("void someLongFunction(\n" 5546 " int someLongParameter) override {}", 5547 Style); 5548 verifyFormat("void someLongFunction(\n" 5549 " int someLongParameter) OVERRIDE {}", 5550 Style); 5551 verifyFormat("void someLongFunction(\n" 5552 " int someLongParameter) final {}", 5553 Style); 5554 verifyFormat("void someLongFunction(\n" 5555 " int someLongParameter) FINAL {}", 5556 Style); 5557 verifyFormat("void someLongFunction(\n" 5558 " int parameter) const override {}", 5559 Style); 5560 5561 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 5562 verifyFormat("void someLongFunction(\n" 5563 " int someLongParameter) const\n" 5564 "{\n" 5565 "}", 5566 Style); 5567 5568 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 5569 verifyFormat("void someLongFunction(\n" 5570 " int someLongParameter) const\n" 5571 " {\n" 5572 " }", 5573 Style); 5574 5575 // Unless these are unknown annotations. 5576 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 5577 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5578 " LONG_AND_UGLY_ANNOTATION;"); 5579 5580 // Breaking before function-like trailing annotations is fine to keep them 5581 // close to their arguments. 5582 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5583 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 5584 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 5585 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 5586 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 5587 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 5588 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 5589 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 5590 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 5591 5592 verifyFormat( 5593 "void aaaaaaaaaaaaaaaaaa()\n" 5594 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 5595 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 5596 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5597 " __attribute__((unused));"); 5598 verifyGoogleFormat( 5599 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5600 " GUARDED_BY(aaaaaaaaaaaa);"); 5601 verifyGoogleFormat( 5602 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5603 " GUARDED_BY(aaaaaaaaaaaa);"); 5604 verifyGoogleFormat( 5605 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 5606 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5607 verifyGoogleFormat( 5608 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 5609 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5610 } 5611 5612 TEST_F(FormatTest, FunctionAnnotations) { 5613 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 5614 "int OldFunction(const string ¶meter) {}"); 5615 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 5616 "string OldFunction(const string ¶meter) {}"); 5617 verifyFormat("template <typename T>\n" 5618 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 5619 "string OldFunction(const string ¶meter) {}"); 5620 5621 // Not function annotations. 5622 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5623 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 5624 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 5625 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 5626 verifyFormat("MACRO(abc).function() // wrap\n" 5627 " << abc;"); 5628 verifyFormat("MACRO(abc)->function() // wrap\n" 5629 " << abc;"); 5630 verifyFormat("MACRO(abc)::function() // wrap\n" 5631 " << abc;"); 5632 } 5633 5634 TEST_F(FormatTest, BreaksDesireably) { 5635 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 5636 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 5637 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 5638 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5639 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 5640 "}"); 5641 5642 verifyFormat( 5643 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5644 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5645 5646 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5648 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5649 5650 verifyFormat( 5651 "aaaaaaaa(aaaaaaaaaaaaa,\n" 5652 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5653 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 5654 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5655 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 5656 5657 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5658 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5659 5660 verifyFormat( 5661 "void f() {\n" 5662 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 5663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5664 "}"); 5665 verifyFormat( 5666 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5667 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5668 verifyFormat( 5669 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5670 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5671 verifyFormat( 5672 "aaaaaa(aaa,\n" 5673 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5674 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5675 " aaaa);"); 5676 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5677 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5678 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5679 5680 // Indent consistently independent of call expression and unary operator. 5681 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 5682 " dddddddddddddddddddddddddddddd));"); 5683 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 5684 " dddddddddddddddddddddddddddddd));"); 5685 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 5686 " dddddddddddddddddddddddddddddd));"); 5687 5688 // This test case breaks on an incorrect memoization, i.e. an optimization not 5689 // taking into account the StopAt value. 5690 verifyFormat( 5691 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 5692 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 5693 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 5694 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5695 5696 verifyFormat("{\n {\n {\n" 5697 " Annotation.SpaceRequiredBefore =\n" 5698 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 5699 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 5700 " }\n }\n}"); 5701 5702 // Break on an outer level if there was a break on an inner level. 5703 EXPECT_EQ("f(g(h(a, // comment\n" 5704 " b, c),\n" 5705 " d, e),\n" 5706 " x, y);", 5707 format("f(g(h(a, // comment\n" 5708 " b, c), d, e), x, y);")); 5709 5710 // Prefer breaking similar line breaks. 5711 verifyFormat( 5712 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 5713 " NSTrackingMouseEnteredAndExited |\n" 5714 " NSTrackingActiveAlways;"); 5715 } 5716 5717 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 5718 FormatStyle NoBinPacking = getGoogleStyle(); 5719 NoBinPacking.BinPackParameters = false; 5720 NoBinPacking.BinPackArguments = true; 5721 verifyFormat("void f() {\n" 5722 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 5723 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5724 "}", 5725 NoBinPacking); 5726 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 5727 " int aaaaaaaaaaaaaaaaaaaa,\n" 5728 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5729 NoBinPacking); 5730 5731 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 5732 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5733 " vector<int> bbbbbbbbbbbbbbb);", 5734 NoBinPacking); 5735 // FIXME: This behavior difference is probably not wanted. However, currently 5736 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 5737 // template arguments from BreakBeforeParameter being set because of the 5738 // one-per-line formatting. 5739 verifyFormat( 5740 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 5741 " aaaaaaaaaa> aaaaaaaaaa);", 5742 NoBinPacking); 5743 verifyFormat( 5744 "void fffffffffff(\n" 5745 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 5746 " aaaaaaaaaa);"); 5747 } 5748 5749 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 5750 FormatStyle NoBinPacking = getGoogleStyle(); 5751 NoBinPacking.BinPackParameters = false; 5752 NoBinPacking.BinPackArguments = false; 5753 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 5754 " aaaaaaaaaaaaaaaaaaaa,\n" 5755 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 5756 NoBinPacking); 5757 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 5758 " aaaaaaaaaaaaa,\n" 5759 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 5760 NoBinPacking); 5761 verifyFormat( 5762 "aaaaaaaa(aaaaaaaaaaaaa,\n" 5763 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5764 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 5765 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5766 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 5767 NoBinPacking); 5768 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 5769 " .aaaaaaaaaaaaaaaaaa();", 5770 NoBinPacking); 5771 verifyFormat("void f() {\n" 5772 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5773 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 5774 "}", 5775 NoBinPacking); 5776 5777 verifyFormat( 5778 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5779 " aaaaaaaaaaaa,\n" 5780 " aaaaaaaaaaaa);", 5781 NoBinPacking); 5782 verifyFormat( 5783 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 5784 " ddddddddddddddddddddddddddddd),\n" 5785 " test);", 5786 NoBinPacking); 5787 5788 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 5789 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 5790 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 5791 " aaaaaaaaaaaaaaaaaa;", 5792 NoBinPacking); 5793 verifyFormat("a(\"a\"\n" 5794 " \"a\",\n" 5795 " a);"); 5796 5797 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 5798 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 5799 " aaaaaaaaa,\n" 5800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5801 NoBinPacking); 5802 verifyFormat( 5803 "void f() {\n" 5804 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 5805 " .aaaaaaa();\n" 5806 "}", 5807 NoBinPacking); 5808 verifyFormat( 5809 "template <class SomeType, class SomeOtherType>\n" 5810 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 5811 NoBinPacking); 5812 } 5813 5814 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 5815 FormatStyle Style = getLLVMStyleWithColumns(15); 5816 Style.ExperimentalAutoDetectBinPacking = true; 5817 EXPECT_EQ("aaa(aaaa,\n" 5818 " aaaa,\n" 5819 " aaaa);\n" 5820 "aaa(aaaa,\n" 5821 " aaaa,\n" 5822 " aaaa);", 5823 format("aaa(aaaa,\n" // one-per-line 5824 " aaaa,\n" 5825 " aaaa );\n" 5826 "aaa(aaaa, aaaa, aaaa);", // inconclusive 5827 Style)); 5828 EXPECT_EQ("aaa(aaaa, aaaa,\n" 5829 " aaaa);\n" 5830 "aaa(aaaa, aaaa,\n" 5831 " aaaa);", 5832 format("aaa(aaaa, aaaa,\n" // bin-packed 5833 " aaaa );\n" 5834 "aaa(aaaa, aaaa, aaaa);", // inconclusive 5835 Style)); 5836 } 5837 5838 TEST_F(FormatTest, FormatsBuilderPattern) { 5839 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 5840 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 5841 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 5842 " .StartsWith(\".init\", ORDER_INIT)\n" 5843 " .StartsWith(\".fini\", ORDER_FINI)\n" 5844 " .StartsWith(\".hash\", ORDER_HASH)\n" 5845 " .Default(ORDER_TEXT);\n"); 5846 5847 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 5848 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 5849 verifyFormat("aaaaaaa->aaaaaaa\n" 5850 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5851 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5852 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 5853 verifyFormat( 5854 "aaaaaaa->aaaaaaa\n" 5855 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5856 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 5857 verifyFormat( 5858 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 5859 " aaaaaaaaaaaaaa);"); 5860 verifyFormat( 5861 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 5862 " aaaaaa->aaaaaaaaaaaa()\n" 5863 " ->aaaaaaaaaaaaaaaa(\n" 5864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5865 " ->aaaaaaaaaaaaaaaaa();"); 5866 verifyGoogleFormat( 5867 "void f() {\n" 5868 " someo->Add((new util::filetools::Handler(dir))\n" 5869 " ->OnEvent1(NewPermanentCallback(\n" 5870 " this, &HandlerHolderClass::EventHandlerCBA))\n" 5871 " ->OnEvent2(NewPermanentCallback(\n" 5872 " this, &HandlerHolderClass::EventHandlerCBB))\n" 5873 " ->OnEvent3(NewPermanentCallback(\n" 5874 " this, &HandlerHolderClass::EventHandlerCBC))\n" 5875 " ->OnEvent5(NewPermanentCallback(\n" 5876 " this, &HandlerHolderClass::EventHandlerCBD))\n" 5877 " ->OnEvent6(NewPermanentCallback(\n" 5878 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 5879 "}"); 5880 5881 verifyFormat( 5882 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 5883 verifyFormat("aaaaaaaaaaaaaaa()\n" 5884 " .aaaaaaaaaaaaaaa()\n" 5885 " .aaaaaaaaaaaaaaa()\n" 5886 " .aaaaaaaaaaaaaaa()\n" 5887 " .aaaaaaaaaaaaaaa();"); 5888 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 5889 " .aaaaaaaaaaaaaaa()\n" 5890 " .aaaaaaaaaaaaaaa()\n" 5891 " .aaaaaaaaaaaaaaa();"); 5892 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 5893 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 5894 " .aaaaaaaaaaaaaaa();"); 5895 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 5896 " ->aaaaaaaaaaaaaae(0)\n" 5897 " ->aaaaaaaaaaaaaaa();"); 5898 5899 // Don't linewrap after very short segments. 5900 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5901 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5902 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5903 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5904 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5905 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5906 verifyFormat("aaa()\n" 5907 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5908 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5909 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5910 5911 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 5912 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5913 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 5914 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 5915 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 5917 5918 // Prefer not to break after empty parentheses. 5919 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 5920 " First->LastNewlineOffset);"); 5921 5922 // Prefer not to create "hanging" indents. 5923 verifyFormat( 5924 "return !soooooooooooooome_map\n" 5925 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5926 " .second;"); 5927 verifyFormat( 5928 "return aaaaaaaaaaaaaaaa\n" 5929 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 5930 " .aaaa(aaaaaaaaaaaaaa);"); 5931 // No hanging indent here. 5932 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 5933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5934 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 5935 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5936 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 5937 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5938 getLLVMStyleWithColumns(60)); 5939 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 5940 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 5941 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5942 getLLVMStyleWithColumns(59)); 5943 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5944 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5945 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5946 5947 // Dont break if only closing statements before member call 5948 verifyFormat("test() {\n" 5949 " ([]() -> {\n" 5950 " int b = 32;\n" 5951 " return 3;\n" 5952 " }).foo();\n" 5953 "}"); 5954 verifyFormat("test() {\n" 5955 " (\n" 5956 " []() -> {\n" 5957 " int b = 32;\n" 5958 " return 3;\n" 5959 " },\n" 5960 " foo, bar)\n" 5961 " .foo();\n" 5962 "}"); 5963 verifyFormat("test() {\n" 5964 " ([]() -> {\n" 5965 " int b = 32;\n" 5966 " return 3;\n" 5967 " })\n" 5968 " .foo()\n" 5969 " .bar();\n" 5970 "}"); 5971 verifyFormat("test() {\n" 5972 " ([]() -> {\n" 5973 " int b = 32;\n" 5974 " return 3;\n" 5975 " })\n" 5976 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 5977 " \"bbbb\");\n" 5978 "}", 5979 getLLVMStyleWithColumns(30)); 5980 } 5981 5982 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 5983 verifyFormat( 5984 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5985 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 5986 verifyFormat( 5987 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 5988 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 5989 5990 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 5991 " ccccccccccccccccccccccccc) {\n}"); 5992 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 5993 " ccccccccccccccccccccccccc) {\n}"); 5994 5995 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 5996 " ccccccccccccccccccccccccc) {\n}"); 5997 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 5998 " ccccccccccccccccccccccccc) {\n}"); 5999 6000 verifyFormat( 6001 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 6002 " ccccccccccccccccccccccccc) {\n}"); 6003 verifyFormat( 6004 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 6005 " ccccccccccccccccccccccccc) {\n}"); 6006 6007 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 6008 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 6009 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 6010 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 6011 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 6012 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 6013 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 6014 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 6015 6016 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 6017 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 6018 " aaaaaaaaaaaaaaa != aa) {\n}"); 6019 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 6020 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 6021 " aaaaaaaaaaaaaaa != aa) {\n}"); 6022 } 6023 6024 TEST_F(FormatTest, BreaksAfterAssignments) { 6025 verifyFormat( 6026 "unsigned Cost =\n" 6027 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 6028 " SI->getPointerAddressSpaceee());\n"); 6029 verifyFormat( 6030 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 6031 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 6032 6033 verifyFormat( 6034 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 6035 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 6036 verifyFormat("unsigned OriginalStartColumn =\n" 6037 " SourceMgr.getSpellingColumnNumber(\n" 6038 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 6039 " 1;"); 6040 } 6041 6042 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 6043 FormatStyle Style = getLLVMStyle(); 6044 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6045 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 6046 Style); 6047 6048 Style.PenaltyBreakAssignment = 20; 6049 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 6050 " cccccccccccccccccccccccccc;", 6051 Style); 6052 } 6053 6054 TEST_F(FormatTest, AlignsAfterAssignments) { 6055 verifyFormat( 6056 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6057 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6058 verifyFormat( 6059 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6060 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6061 verifyFormat( 6062 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6063 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6064 verifyFormat( 6065 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6066 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 6067 verifyFormat( 6068 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 6069 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 6070 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 6071 } 6072 6073 TEST_F(FormatTest, AlignsAfterReturn) { 6074 verifyFormat( 6075 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6076 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6077 verifyFormat( 6078 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6079 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 6080 verifyFormat( 6081 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 6082 " aaaaaaaaaaaaaaaaaaaaaa();"); 6083 verifyFormat( 6084 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 6085 " aaaaaaaaaaaaaaaaaaaaaa());"); 6086 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6087 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6088 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6089 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 6090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6091 verifyFormat("return\n" 6092 " // true if code is one of a or b.\n" 6093 " code == a || code == b;"); 6094 } 6095 6096 TEST_F(FormatTest, AlignsAfterOpenBracket) { 6097 verifyFormat( 6098 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 6099 " aaaaaaaaa aaaaaaa) {}"); 6100 verifyFormat( 6101 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 6102 " aaaaaaaaaaa aaaaaaaaa);"); 6103 verifyFormat( 6104 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 6105 " aaaaaaaaaaaaaaaaaaaaa));"); 6106 FormatStyle Style = getLLVMStyle(); 6107 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6108 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6109 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 6110 Style); 6111 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 6112 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 6113 Style); 6114 verifyFormat("SomeLongVariableName->someFunction(\n" 6115 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 6116 Style); 6117 verifyFormat( 6118 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 6119 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6120 Style); 6121 verifyFormat( 6122 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 6123 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6124 Style); 6125 verifyFormat( 6126 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 6127 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 6128 Style); 6129 6130 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 6131 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 6132 " b));", 6133 Style); 6134 6135 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6136 Style.BinPackArguments = false; 6137 Style.BinPackParameters = false; 6138 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6139 " aaaaaaaaaaa aaaaaaaa,\n" 6140 " aaaaaaaaa aaaaaaa,\n" 6141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6142 Style); 6143 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 6144 " aaaaaaaaaaa aaaaaaaaa,\n" 6145 " aaaaaaaaaaa aaaaaaaaa,\n" 6146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6147 Style); 6148 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 6149 " aaaaaaaaaaaaaaa,\n" 6150 " aaaaaaaaaaaaaaaaaaaaa,\n" 6151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 6152 Style); 6153 verifyFormat( 6154 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 6155 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 6156 Style); 6157 verifyFormat( 6158 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 6159 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 6160 Style); 6161 verifyFormat( 6162 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6163 " aaaaaaaaaaaaaaaaaaaaa(\n" 6164 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 6165 " aaaaaaaaaaaaaaaa);", 6166 Style); 6167 verifyFormat( 6168 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6169 " aaaaaaaaaaaaaaaaaaaaa(\n" 6170 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 6171 " aaaaaaaaaaaaaaaa);", 6172 Style); 6173 } 6174 6175 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 6176 FormatStyle Style = getLLVMStyleWithColumns(40); 6177 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 6178 " bbbbbbbbbbbbbbbbbbbbbb);", 6179 Style); 6180 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6181 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6182 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 6183 " bbbbbbbbbbbbbbbbbbbbbb);", 6184 Style); 6185 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6186 Style.AlignOperands = FormatStyle::OAS_Align; 6187 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 6188 " bbbbbbbbbbbbbbbbbbbbbb);", 6189 Style); 6190 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6191 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6192 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 6193 " bbbbbbbbbbbbbbbbbbbbbb);", 6194 Style); 6195 } 6196 6197 TEST_F(FormatTest, BreaksConditionalExpressions) { 6198 verifyFormat( 6199 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6200 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6201 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6202 verifyFormat( 6203 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 6204 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6205 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6206 verifyFormat( 6207 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6208 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6209 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 6210 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6211 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6212 verifyFormat( 6213 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 6214 " : aaaaaaaaaaaaa);"); 6215 verifyFormat( 6216 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6217 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6218 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6219 " aaaaaaaaaaaaa);"); 6220 verifyFormat( 6221 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6222 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6223 " aaaaaaaaaaaaa);"); 6224 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6225 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6227 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6228 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6229 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6230 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6231 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6232 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6233 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6234 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6235 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6236 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6237 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6238 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6239 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6240 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6241 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6242 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6243 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6244 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 6245 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6246 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6247 " : aaaaaaaaaaaaaaaa;"); 6248 verifyFormat( 6249 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6250 " ? aaaaaaaaaaaaaaa\n" 6251 " : aaaaaaaaaaaaaaa;"); 6252 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 6253 " aaaaaaaaa\n" 6254 " ? b\n" 6255 " : c);"); 6256 verifyFormat("return aaaa == bbbb\n" 6257 " // comment\n" 6258 " ? aaaa\n" 6259 " : bbbb;"); 6260 verifyFormat("unsigned Indent =\n" 6261 " format(TheLine.First,\n" 6262 " IndentForLevel[TheLine.Level] >= 0\n" 6263 " ? IndentForLevel[TheLine.Level]\n" 6264 " : TheLine * 2,\n" 6265 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 6266 getLLVMStyleWithColumns(60)); 6267 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 6268 " ? aaaaaaaaaaaaaaa\n" 6269 " : bbbbbbbbbbbbbbb //\n" 6270 " ? ccccccccccccccc\n" 6271 " : ddddddddddddddd;"); 6272 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 6273 " ? aaaaaaaaaaaaaaa\n" 6274 " : (bbbbbbbbbbbbbbb //\n" 6275 " ? ccccccccccccccc\n" 6276 " : ddddddddddddddd);"); 6277 verifyFormat( 6278 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6279 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6280 " aaaaaaaaaaaaaaaaaaaaa +\n" 6281 " aaaaaaaaaaaaaaaaaaaaa\n" 6282 " : aaaaaaaaaa;"); 6283 verifyFormat( 6284 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6285 " : aaaaaaaaaaaaaaaaaaaaaa\n" 6286 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6287 6288 FormatStyle NoBinPacking = getLLVMStyle(); 6289 NoBinPacking.BinPackArguments = false; 6290 verifyFormat( 6291 "void f() {\n" 6292 " g(aaa,\n" 6293 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 6294 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6295 " ? aaaaaaaaaaaaaaa\n" 6296 " : aaaaaaaaaaaaaaa);\n" 6297 "}", 6298 NoBinPacking); 6299 verifyFormat( 6300 "void f() {\n" 6301 " g(aaa,\n" 6302 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 6303 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6304 " ?: aaaaaaaaaaaaaaa);\n" 6305 "}", 6306 NoBinPacking); 6307 6308 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 6309 " // comment.\n" 6310 " ccccccccccccccccccccccccccccccccccccccc\n" 6311 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6312 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 6313 6314 // Assignments in conditional expressions. Apparently not uncommon :-(. 6315 verifyFormat("return a != b\n" 6316 " // comment\n" 6317 " ? a = b\n" 6318 " : a = b;"); 6319 verifyFormat("return a != b\n" 6320 " // comment\n" 6321 " ? a = a != b\n" 6322 " // comment\n" 6323 " ? a = b\n" 6324 " : a\n" 6325 " : a;\n"); 6326 verifyFormat("return a != b\n" 6327 " // comment\n" 6328 " ? a\n" 6329 " : a = a != b\n" 6330 " // comment\n" 6331 " ? a = b\n" 6332 " : a;"); 6333 6334 // Chained conditionals 6335 FormatStyle Style = getLLVMStyle(); 6336 Style.ColumnLimit = 70; 6337 Style.AlignOperands = FormatStyle::OAS_Align; 6338 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6339 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6340 " : 3333333333333333;", 6341 Style); 6342 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6343 " : bbbbbbbbbb ? 2222222222222222\n" 6344 " : 3333333333333333;", 6345 Style); 6346 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 6347 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 6348 " : 3333333333333333;", 6349 Style); 6350 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6351 " : bbbbbbbbbbbbbb ? 222222\n" 6352 " : 333333;", 6353 Style); 6354 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6355 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6356 " : cccccccccccccc ? 3333333333333333\n" 6357 " : 4444444444444444;", 6358 Style); 6359 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 6360 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6361 " : 3333333333333333;", 6362 Style); 6363 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6364 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6365 " : (aaa ? bbb : ccc);", 6366 Style); 6367 verifyFormat( 6368 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6369 " : cccccccccccccccccc)\n" 6370 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6371 " : 3333333333333333;", 6372 Style); 6373 verifyFormat( 6374 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6375 " : cccccccccccccccccc)\n" 6376 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6377 " : 3333333333333333;", 6378 Style); 6379 verifyFormat( 6380 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6381 " : dddddddddddddddddd)\n" 6382 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6383 " : 3333333333333333;", 6384 Style); 6385 verifyFormat( 6386 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6387 " : dddddddddddddddddd)\n" 6388 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6389 " : 3333333333333333;", 6390 Style); 6391 verifyFormat( 6392 "return aaaaaaaaa ? 1111111111111111\n" 6393 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6394 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6395 " : dddddddddddddddddd)\n", 6396 Style); 6397 verifyFormat( 6398 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6399 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6400 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6401 " : cccccccccccccccccc);", 6402 Style); 6403 verifyFormat( 6404 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6405 " : ccccccccccccccc ? dddddddddddddddddd\n" 6406 " : eeeeeeeeeeeeeeeeee)\n" 6407 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6408 " : 3333333333333333;", 6409 Style); 6410 verifyFormat( 6411 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6412 " : ccccccccccccccc ? dddddddddddddddddd\n" 6413 " : eeeeeeeeeeeeeeeeee)\n" 6414 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6415 " : 3333333333333333;", 6416 Style); 6417 verifyFormat( 6418 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6419 " : cccccccccccc ? dddddddddddddddddd\n" 6420 " : eeeeeeeeeeeeeeeeee)\n" 6421 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6422 " : 3333333333333333;", 6423 Style); 6424 verifyFormat( 6425 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6426 " : cccccccccccccccccc\n" 6427 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6428 " : 3333333333333333;", 6429 Style); 6430 verifyFormat( 6431 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6432 " : cccccccccccccccc ? dddddddddddddddddd\n" 6433 " : eeeeeeeeeeeeeeeeee\n" 6434 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6435 " : 3333333333333333;", 6436 Style); 6437 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 6438 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6439 " : cccccccccccccccccc ? dddddddddddddddddd\n" 6440 " : eeeeeeeeeeeeeeeeee)\n" 6441 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 6442 " : 3333333333333333;", 6443 Style); 6444 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 6445 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6446 " : cccccccccccccccc ? dddddddddddddddddd\n" 6447 " : eeeeeeeeeeeeeeeeee\n" 6448 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 6449 " : 3333333333333333;", 6450 Style); 6451 6452 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6453 Style.BreakBeforeTernaryOperators = false; 6454 // FIXME: Aligning the question marks is weird given DontAlign. 6455 // Consider disabling this alignment in this case. Also check whether this 6456 // will render the adjustment from https://reviews.llvm.org/D82199 6457 // unnecessary. 6458 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 6459 " bbbb ? cccccccccccccccccc :\n" 6460 " ddddd;\n", 6461 Style); 6462 6463 EXPECT_EQ( 6464 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 6465 " /*\n" 6466 " */\n" 6467 " function() {\n" 6468 " try {\n" 6469 " return JJJJJJJJJJJJJJ(\n" 6470 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 6471 " }\n" 6472 " } :\n" 6473 " function() {};", 6474 format( 6475 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 6476 " /*\n" 6477 " */\n" 6478 " function() {\n" 6479 " try {\n" 6480 " return JJJJJJJJJJJJJJ(\n" 6481 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 6482 " }\n" 6483 " } :\n" 6484 " function() {};", 6485 getGoogleStyle(FormatStyle::LK_JavaScript))); 6486 } 6487 6488 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 6489 FormatStyle Style = getLLVMStyle(); 6490 Style.BreakBeforeTernaryOperators = false; 6491 Style.ColumnLimit = 70; 6492 verifyFormat( 6493 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6494 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6496 Style); 6497 verifyFormat( 6498 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 6499 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6500 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6501 Style); 6502 verifyFormat( 6503 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6504 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6505 Style); 6506 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 6507 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6508 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6509 Style); 6510 verifyFormat( 6511 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 6512 " aaaaaaaaaaaaa);", 6513 Style); 6514 verifyFormat( 6515 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6516 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6517 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6518 " aaaaaaaaaaaaa);", 6519 Style); 6520 verifyFormat( 6521 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6522 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6523 " aaaaaaaaaaaaa);", 6524 Style); 6525 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6526 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6528 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6529 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6530 Style); 6531 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6532 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6533 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6534 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6536 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6537 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6538 Style); 6539 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6540 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 6541 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6542 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6543 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6544 Style); 6545 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6546 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6547 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6548 Style); 6549 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 6550 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6553 Style); 6554 verifyFormat( 6555 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6556 " aaaaaaaaaaaaaaa :\n" 6557 " aaaaaaaaaaaaaaa;", 6558 Style); 6559 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 6560 " aaaaaaaaa ?\n" 6561 " b :\n" 6562 " c);", 6563 Style); 6564 verifyFormat("unsigned Indent =\n" 6565 " format(TheLine.First,\n" 6566 " IndentForLevel[TheLine.Level] >= 0 ?\n" 6567 " IndentForLevel[TheLine.Level] :\n" 6568 " TheLine * 2,\n" 6569 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 6570 Style); 6571 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 6572 " aaaaaaaaaaaaaaa :\n" 6573 " bbbbbbbbbbbbbbb ? //\n" 6574 " ccccccccccccccc :\n" 6575 " ddddddddddddddd;", 6576 Style); 6577 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 6578 " aaaaaaaaaaaaaaa :\n" 6579 " (bbbbbbbbbbbbbbb ? //\n" 6580 " ccccccccccccccc :\n" 6581 " ddddddddddddddd);", 6582 Style); 6583 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6584 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 6585 " ccccccccccccccccccccccccccc;", 6586 Style); 6587 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6588 " aaaaa :\n" 6589 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 6590 Style); 6591 6592 // Chained conditionals 6593 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 6594 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6595 " 3333333333333333;", 6596 Style); 6597 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 6598 " bbbbbbbbbb ? 2222222222222222 :\n" 6599 " 3333333333333333;", 6600 Style); 6601 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 6602 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6603 " 3333333333333333;", 6604 Style); 6605 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 6606 " bbbbbbbbbbbbbbbb ? 222222 :\n" 6607 " 333333;", 6608 Style); 6609 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 6610 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6611 " cccccccccccccccc ? 3333333333333333 :\n" 6612 " 4444444444444444;", 6613 Style); 6614 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 6615 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6616 " 3333333333333333;", 6617 Style); 6618 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 6619 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6620 " (aaa ? bbb : ccc);", 6621 Style); 6622 verifyFormat( 6623 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 6624 " cccccccccccccccccc) :\n" 6625 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6626 " 3333333333333333;", 6627 Style); 6628 verifyFormat( 6629 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 6630 " cccccccccccccccccc) :\n" 6631 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6632 " 3333333333333333;", 6633 Style); 6634 verifyFormat( 6635 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 6636 " dddddddddddddddddd) :\n" 6637 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6638 " 3333333333333333;", 6639 Style); 6640 verifyFormat( 6641 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 6642 " dddddddddddddddddd) :\n" 6643 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6644 " 3333333333333333;", 6645 Style); 6646 verifyFormat( 6647 "return aaaaaaaaa ? 1111111111111111 :\n" 6648 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6649 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 6650 " dddddddddddddddddd)\n", 6651 Style); 6652 verifyFormat( 6653 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 6654 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6655 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 6656 " cccccccccccccccccc);", 6657 Style); 6658 verifyFormat( 6659 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 6660 " ccccccccccccccccc ? dddddddddddddddddd :\n" 6661 " eeeeeeeeeeeeeeeeee) :\n" 6662 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6663 " 3333333333333333;", 6664 Style); 6665 verifyFormat( 6666 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 6667 " ccccccccccccc ? dddddddddddddddddd :\n" 6668 " eeeeeeeeeeeeeeeeee) :\n" 6669 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6670 " 3333333333333333;", 6671 Style); 6672 verifyFormat( 6673 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 6674 " ccccccccccccccccc ? dddddddddddddddddd :\n" 6675 " eeeeeeeeeeeeeeeeee) :\n" 6676 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6677 " 3333333333333333;", 6678 Style); 6679 verifyFormat( 6680 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 6681 " cccccccccccccccccc :\n" 6682 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6683 " 3333333333333333;", 6684 Style); 6685 verifyFormat( 6686 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 6687 " cccccccccccccccccc ? dddddddddddddddddd :\n" 6688 " eeeeeeeeeeeeeeeeee :\n" 6689 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6690 " 3333333333333333;", 6691 Style); 6692 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 6693 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 6694 " cccccccccccccccccc ? dddddddddddddddddd :\n" 6695 " eeeeeeeeeeeeeeeeee) :\n" 6696 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6697 " 3333333333333333;", 6698 Style); 6699 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 6700 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 6701 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 6702 " eeeeeeeeeeeeeeeeee :\n" 6703 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6704 " 3333333333333333;", 6705 Style); 6706 } 6707 6708 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 6709 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 6710 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 6711 verifyFormat("bool a = true, b = false;"); 6712 6713 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 6715 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 6716 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 6717 verifyFormat( 6718 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 6719 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 6720 " d = e && f;"); 6721 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 6722 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 6723 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 6724 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 6725 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 6726 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 6727 6728 FormatStyle Style = getGoogleStyle(); 6729 Style.PointerAlignment = FormatStyle::PAS_Left; 6730 Style.DerivePointerAlignment = false; 6731 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6732 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 6733 " *b = bbbbbbbbbbbbbbbbbbb;", 6734 Style); 6735 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 6736 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 6737 Style); 6738 verifyFormat("vector<int*> a, b;", Style); 6739 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 6740 } 6741 6742 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 6743 verifyFormat("arr[foo ? bar : baz];"); 6744 verifyFormat("f()[foo ? bar : baz];"); 6745 verifyFormat("(a + b)[foo ? bar : baz];"); 6746 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 6747 } 6748 6749 TEST_F(FormatTest, AlignsStringLiterals) { 6750 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 6751 " \"short literal\");"); 6752 verifyFormat( 6753 "looooooooooooooooooooooooongFunction(\n" 6754 " \"short literal\"\n" 6755 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 6756 verifyFormat("someFunction(\"Always break between multi-line\"\n" 6757 " \" string literals\",\n" 6758 " and, other, parameters);"); 6759 EXPECT_EQ("fun + \"1243\" /* comment */\n" 6760 " \"5678\";", 6761 format("fun + \"1243\" /* comment */\n" 6762 " \"5678\";", 6763 getLLVMStyleWithColumns(28))); 6764 EXPECT_EQ( 6765 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 6766 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 6767 " \"aaaaaaaaaaaaaaaa\";", 6768 format("aaaaaa =" 6769 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 6770 "aaaaaaaaaaaaaaaaaaaaa\" " 6771 "\"aaaaaaaaaaaaaaaa\";")); 6772 verifyFormat("a = a + \"a\"\n" 6773 " \"a\"\n" 6774 " \"a\";"); 6775 verifyFormat("f(\"a\", \"b\"\n" 6776 " \"c\");"); 6777 6778 verifyFormat( 6779 "#define LL_FORMAT \"ll\"\n" 6780 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 6781 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 6782 6783 verifyFormat("#define A(X) \\\n" 6784 " \"aaaaa\" #X \"bbbbbb\" \\\n" 6785 " \"ccccc\"", 6786 getLLVMStyleWithColumns(23)); 6787 verifyFormat("#define A \"def\"\n" 6788 "f(\"abc\" A \"ghi\"\n" 6789 " \"jkl\");"); 6790 6791 verifyFormat("f(L\"a\"\n" 6792 " L\"b\");"); 6793 verifyFormat("#define A(X) \\\n" 6794 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 6795 " L\"ccccc\"", 6796 getLLVMStyleWithColumns(25)); 6797 6798 verifyFormat("f(@\"a\"\n" 6799 " @\"b\");"); 6800 verifyFormat("NSString s = @\"a\"\n" 6801 " @\"b\"\n" 6802 " @\"c\";"); 6803 verifyFormat("NSString s = @\"a\"\n" 6804 " \"b\"\n" 6805 " \"c\";"); 6806 } 6807 6808 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 6809 FormatStyle Style = getLLVMStyle(); 6810 // No declarations or definitions should be moved to own line. 6811 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 6812 verifyFormat("class A {\n" 6813 " int f() { return 1; }\n" 6814 " int g();\n" 6815 "};\n" 6816 "int f() { return 1; }\n" 6817 "int g();\n", 6818 Style); 6819 6820 // All declarations and definitions should have the return type moved to its 6821 // own line. 6822 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 6823 Style.TypenameMacros = {"LIST"}; 6824 verifyFormat("SomeType\n" 6825 "funcdecl(LIST(uint64_t));", 6826 Style); 6827 verifyFormat("class E {\n" 6828 " int\n" 6829 " f() {\n" 6830 " return 1;\n" 6831 " }\n" 6832 " int\n" 6833 " g();\n" 6834 "};\n" 6835 "int\n" 6836 "f() {\n" 6837 " return 1;\n" 6838 "}\n" 6839 "int\n" 6840 "g();\n", 6841 Style); 6842 6843 // Top-level definitions, and no kinds of declarations should have the 6844 // return type moved to its own line. 6845 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 6846 verifyFormat("class B {\n" 6847 " int f() { return 1; }\n" 6848 " int g();\n" 6849 "};\n" 6850 "int\n" 6851 "f() {\n" 6852 " return 1;\n" 6853 "}\n" 6854 "int g();\n", 6855 Style); 6856 6857 // Top-level definitions and declarations should have the return type moved 6858 // to its own line. 6859 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 6860 verifyFormat("class C {\n" 6861 " int f() { return 1; }\n" 6862 " int g();\n" 6863 "};\n" 6864 "int\n" 6865 "f() {\n" 6866 " return 1;\n" 6867 "}\n" 6868 "int\n" 6869 "g();\n", 6870 Style); 6871 6872 // All definitions should have the return type moved to its own line, but no 6873 // kinds of declarations. 6874 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 6875 verifyFormat("class D {\n" 6876 " int\n" 6877 " f() {\n" 6878 " return 1;\n" 6879 " }\n" 6880 " int g();\n" 6881 "};\n" 6882 "int\n" 6883 "f() {\n" 6884 " return 1;\n" 6885 "}\n" 6886 "int g();\n", 6887 Style); 6888 verifyFormat("const char *\n" 6889 "f(void) {\n" // Break here. 6890 " return \"\";\n" 6891 "}\n" 6892 "const char *bar(void);\n", // No break here. 6893 Style); 6894 verifyFormat("template <class T>\n" 6895 "T *\n" 6896 "f(T &c) {\n" // Break here. 6897 " return NULL;\n" 6898 "}\n" 6899 "template <class T> T *f(T &c);\n", // No break here. 6900 Style); 6901 verifyFormat("class C {\n" 6902 " int\n" 6903 " operator+() {\n" 6904 " return 1;\n" 6905 " }\n" 6906 " int\n" 6907 " operator()() {\n" 6908 " return 1;\n" 6909 " }\n" 6910 "};\n", 6911 Style); 6912 verifyFormat("void\n" 6913 "A::operator()() {}\n" 6914 "void\n" 6915 "A::operator>>() {}\n" 6916 "void\n" 6917 "A::operator+() {}\n" 6918 "void\n" 6919 "A::operator*() {}\n" 6920 "void\n" 6921 "A::operator->() {}\n" 6922 "void\n" 6923 "A::operator void *() {}\n" 6924 "void\n" 6925 "A::operator void &() {}\n" 6926 "void\n" 6927 "A::operator void &&() {}\n" 6928 "void\n" 6929 "A::operator char *() {}\n" 6930 "void\n" 6931 "A::operator[]() {}\n" 6932 "void\n" 6933 "A::operator!() {}\n" 6934 "void\n" 6935 "A::operator**() {}\n" 6936 "void\n" 6937 "A::operator<Foo> *() {}\n" 6938 "void\n" 6939 "A::operator<Foo> **() {}\n" 6940 "void\n" 6941 "A::operator<Foo> &() {}\n" 6942 "void\n" 6943 "A::operator void **() {}\n", 6944 Style); 6945 verifyFormat("constexpr auto\n" 6946 "operator()() const -> reference {}\n" 6947 "constexpr auto\n" 6948 "operator>>() const -> reference {}\n" 6949 "constexpr auto\n" 6950 "operator+() const -> reference {}\n" 6951 "constexpr auto\n" 6952 "operator*() const -> reference {}\n" 6953 "constexpr auto\n" 6954 "operator->() const -> reference {}\n" 6955 "constexpr auto\n" 6956 "operator++() const -> reference {}\n" 6957 "constexpr auto\n" 6958 "operator void *() const -> reference {}\n" 6959 "constexpr auto\n" 6960 "operator void **() const -> reference {}\n" 6961 "constexpr auto\n" 6962 "operator void *() const -> reference {}\n" 6963 "constexpr auto\n" 6964 "operator void &() const -> reference {}\n" 6965 "constexpr auto\n" 6966 "operator void &&() const -> reference {}\n" 6967 "constexpr auto\n" 6968 "operator char *() const -> reference {}\n" 6969 "constexpr auto\n" 6970 "operator!() const -> reference {}\n" 6971 "constexpr auto\n" 6972 "operator[]() const -> reference {}\n", 6973 Style); 6974 verifyFormat("void *operator new(std::size_t s);", // No break here. 6975 Style); 6976 verifyFormat("void *\n" 6977 "operator new(std::size_t s) {}", 6978 Style); 6979 verifyFormat("void *\n" 6980 "operator delete[](void *ptr) {}", 6981 Style); 6982 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 6983 verifyFormat("const char *\n" 6984 "f(void)\n" // Break here. 6985 "{\n" 6986 " return \"\";\n" 6987 "}\n" 6988 "const char *bar(void);\n", // No break here. 6989 Style); 6990 verifyFormat("template <class T>\n" 6991 "T *\n" // Problem here: no line break 6992 "f(T &c)\n" // Break here. 6993 "{\n" 6994 " return NULL;\n" 6995 "}\n" 6996 "template <class T> T *f(T &c);\n", // No break here. 6997 Style); 6998 verifyFormat("int\n" 6999 "foo(A<bool> a)\n" 7000 "{\n" 7001 " return a;\n" 7002 "}\n", 7003 Style); 7004 verifyFormat("int\n" 7005 "foo(A<8> a)\n" 7006 "{\n" 7007 " return a;\n" 7008 "}\n", 7009 Style); 7010 verifyFormat("int\n" 7011 "foo(A<B<bool>, 8> a)\n" 7012 "{\n" 7013 " return a;\n" 7014 "}\n", 7015 Style); 7016 verifyFormat("int\n" 7017 "foo(A<B<8>, bool> a)\n" 7018 "{\n" 7019 " return a;\n" 7020 "}\n", 7021 Style); 7022 verifyFormat("int\n" 7023 "foo(A<B<bool>, bool> a)\n" 7024 "{\n" 7025 " return a;\n" 7026 "}\n", 7027 Style); 7028 verifyFormat("int\n" 7029 "foo(A<B<8>, 8> a)\n" 7030 "{\n" 7031 " return a;\n" 7032 "}\n", 7033 Style); 7034 7035 Style = getGNUStyle(); 7036 7037 // Test for comments at the end of function declarations. 7038 verifyFormat("void\n" 7039 "foo (int a, /*abc*/ int b) // def\n" 7040 "{\n" 7041 "}\n", 7042 Style); 7043 7044 verifyFormat("void\n" 7045 "foo (int a, /* abc */ int b) /* def */\n" 7046 "{\n" 7047 "}\n", 7048 Style); 7049 7050 // Definitions that should not break after return type 7051 verifyFormat("void foo (int a, int b); // def\n", Style); 7052 verifyFormat("void foo (int a, int b); /* def */\n", Style); 7053 verifyFormat("void foo (int a, int b);\n", Style); 7054 } 7055 7056 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 7057 FormatStyle NoBreak = getLLVMStyle(); 7058 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 7059 FormatStyle Break = getLLVMStyle(); 7060 Break.AlwaysBreakBeforeMultilineStrings = true; 7061 verifyFormat("aaaa = \"bbbb\"\n" 7062 " \"cccc\";", 7063 NoBreak); 7064 verifyFormat("aaaa =\n" 7065 " \"bbbb\"\n" 7066 " \"cccc\";", 7067 Break); 7068 verifyFormat("aaaa(\"bbbb\"\n" 7069 " \"cccc\");", 7070 NoBreak); 7071 verifyFormat("aaaa(\n" 7072 " \"bbbb\"\n" 7073 " \"cccc\");", 7074 Break); 7075 verifyFormat("aaaa(qqq, \"bbbb\"\n" 7076 " \"cccc\");", 7077 NoBreak); 7078 verifyFormat("aaaa(qqq,\n" 7079 " \"bbbb\"\n" 7080 " \"cccc\");", 7081 Break); 7082 verifyFormat("aaaa(qqq,\n" 7083 " L\"bbbb\"\n" 7084 " L\"cccc\");", 7085 Break); 7086 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 7087 " \"bbbb\"));", 7088 Break); 7089 verifyFormat("string s = someFunction(\n" 7090 " \"abc\"\n" 7091 " \"abc\");", 7092 Break); 7093 7094 // As we break before unary operators, breaking right after them is bad. 7095 verifyFormat("string foo = abc ? \"x\"\n" 7096 " \"blah blah blah blah blah blah\"\n" 7097 " : \"y\";", 7098 Break); 7099 7100 // Don't break if there is no column gain. 7101 verifyFormat("f(\"aaaa\"\n" 7102 " \"bbbb\");", 7103 Break); 7104 7105 // Treat literals with escaped newlines like multi-line string literals. 7106 EXPECT_EQ("x = \"a\\\n" 7107 "b\\\n" 7108 "c\";", 7109 format("x = \"a\\\n" 7110 "b\\\n" 7111 "c\";", 7112 NoBreak)); 7113 EXPECT_EQ("xxxx =\n" 7114 " \"a\\\n" 7115 "b\\\n" 7116 "c\";", 7117 format("xxxx = \"a\\\n" 7118 "b\\\n" 7119 "c\";", 7120 Break)); 7121 7122 EXPECT_EQ("NSString *const kString =\n" 7123 " @\"aaaa\"\n" 7124 " @\"bbbb\";", 7125 format("NSString *const kString = @\"aaaa\"\n" 7126 "@\"bbbb\";", 7127 Break)); 7128 7129 Break.ColumnLimit = 0; 7130 verifyFormat("const char *hello = \"hello llvm\";", Break); 7131 } 7132 7133 TEST_F(FormatTest, AlignsPipes) { 7134 verifyFormat( 7135 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7136 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7137 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7138 verifyFormat( 7139 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 7140 " << aaaaaaaaaaaaaaaaaaaa;"); 7141 verifyFormat( 7142 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7143 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7144 verifyFormat( 7145 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7146 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7147 verifyFormat( 7148 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 7149 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 7150 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 7151 verifyFormat( 7152 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7153 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7154 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7155 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7156 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7157 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7158 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 7159 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 7160 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 7161 verifyFormat( 7162 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7163 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7164 verifyFormat( 7165 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 7166 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7167 7168 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 7169 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 7170 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7171 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7172 " aaaaaaaaaaaaaaaaaaaaa)\n" 7173 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7174 verifyFormat("LOG_IF(aaa == //\n" 7175 " bbb)\n" 7176 " << a << b;"); 7177 7178 // But sometimes, breaking before the first "<<" is desirable. 7179 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 7180 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 7181 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 7182 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7183 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7184 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 7185 " << BEF << IsTemplate << Description << E->getType();"); 7186 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 7187 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7188 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7189 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 7190 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7191 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7192 " << aaa;"); 7193 7194 verifyFormat( 7195 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7196 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7197 7198 // Incomplete string literal. 7199 EXPECT_EQ("llvm::errs() << \"\n" 7200 " << a;", 7201 format("llvm::errs() << \"\n<<a;")); 7202 7203 verifyFormat("void f() {\n" 7204 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 7205 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 7206 "}"); 7207 7208 // Handle 'endl'. 7209 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 7210 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 7211 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 7212 7213 // Handle '\n'. 7214 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 7215 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 7216 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 7217 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 7218 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 7219 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 7220 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 7221 } 7222 7223 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 7224 verifyFormat("return out << \"somepacket = {\\n\"\n" 7225 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 7226 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 7227 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 7228 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 7229 " << \"}\";"); 7230 7231 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 7232 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 7233 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 7234 verifyFormat( 7235 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 7236 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 7237 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 7238 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 7239 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 7240 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 7241 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 7242 verifyFormat( 7243 "void f() {\n" 7244 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 7245 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7246 "}"); 7247 7248 // Breaking before the first "<<" is generally not desirable. 7249 verifyFormat( 7250 "llvm::errs()\n" 7251 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7252 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7253 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7254 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7255 getLLVMStyleWithColumns(70)); 7256 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 7257 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7258 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 7259 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7260 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 7261 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7262 getLLVMStyleWithColumns(70)); 7263 7264 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 7265 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 7266 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 7267 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 7268 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 7269 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 7270 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 7271 " (aaaa + aaaa);", 7272 getLLVMStyleWithColumns(40)); 7273 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 7274 " (aaaaaaa + aaaaa));", 7275 getLLVMStyleWithColumns(40)); 7276 verifyFormat( 7277 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 7278 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 7279 " bbbbbbbbbbbbbbbbbbbbbbb);"); 7280 } 7281 7282 TEST_F(FormatTest, UnderstandsEquals) { 7283 verifyFormat( 7284 "aaaaaaaaaaaaaaaaa =\n" 7285 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7286 verifyFormat( 7287 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7288 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 7289 verifyFormat( 7290 "if (a) {\n" 7291 " f();\n" 7292 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7293 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 7294 "}"); 7295 7296 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7297 " 100000000 + 10000000) {\n}"); 7298 } 7299 7300 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 7301 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 7302 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 7303 7304 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 7305 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 7306 7307 verifyFormat( 7308 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 7309 " Parameter2);"); 7310 7311 verifyFormat( 7312 "ShortObject->shortFunction(\n" 7313 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 7314 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 7315 7316 verifyFormat("loooooooooooooongFunction(\n" 7317 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 7318 7319 verifyFormat( 7320 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 7321 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 7322 7323 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 7324 " .WillRepeatedly(Return(SomeValue));"); 7325 verifyFormat("void f() {\n" 7326 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 7327 " .Times(2)\n" 7328 " .WillRepeatedly(Return(SomeValue));\n" 7329 "}"); 7330 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 7331 " ccccccccccccccccccccccc);"); 7332 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7334 " .aaaaa(aaaaa),\n" 7335 " aaaaaaaaaaaaaaaaaaaaa);"); 7336 verifyFormat("void f() {\n" 7337 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7338 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 7339 "}"); 7340 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7341 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7342 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7343 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7344 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7345 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7346 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7347 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7348 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 7349 "}"); 7350 7351 // Here, it is not necessary to wrap at "." or "->". 7352 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 7353 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 7354 verifyFormat( 7355 "aaaaaaaaaaa->aaaaaaaaa(\n" 7356 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7357 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 7358 7359 verifyFormat( 7360 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 7362 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 7363 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 7364 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 7365 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 7366 7367 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7368 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7369 " .a();"); 7370 7371 FormatStyle NoBinPacking = getLLVMStyle(); 7372 NoBinPacking.BinPackParameters = false; 7373 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7374 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7375 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 7376 " aaaaaaaaaaaaaaaaaaa,\n" 7377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7378 NoBinPacking); 7379 7380 // If there is a subsequent call, change to hanging indentation. 7381 verifyFormat( 7382 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7383 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 7384 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7385 verifyFormat( 7386 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7387 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 7388 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7389 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7390 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7391 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7392 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7393 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 7394 } 7395 7396 TEST_F(FormatTest, WrapsTemplateDeclarations) { 7397 verifyFormat("template <typename T>\n" 7398 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 7399 verifyFormat("template <typename T>\n" 7400 "// T should be one of {A, B}.\n" 7401 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 7402 verifyFormat( 7403 "template <typename T>\n" 7404 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 7405 verifyFormat("template <typename T>\n" 7406 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 7407 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 7408 verifyFormat( 7409 "template <typename T>\n" 7410 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 7411 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 7412 verifyFormat( 7413 "template <typename T>\n" 7414 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 7415 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 7416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7417 verifyFormat("template <typename T>\n" 7418 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7419 " int aaaaaaaaaaaaaaaaaaaaaa);"); 7420 verifyFormat( 7421 "template <typename T1, typename T2 = char, typename T3 = char,\n" 7422 " typename T4 = char>\n" 7423 "void f();"); 7424 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 7425 " template <typename> class cccccccccccccccccccccc,\n" 7426 " typename ddddddddddddd>\n" 7427 "class C {};"); 7428 verifyFormat( 7429 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 7430 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7431 7432 verifyFormat("void f() {\n" 7433 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 7434 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 7435 "}"); 7436 7437 verifyFormat("template <typename T> class C {};"); 7438 verifyFormat("template <typename T> void f();"); 7439 verifyFormat("template <typename T> void f() {}"); 7440 verifyFormat( 7441 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 7442 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7443 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 7444 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 7445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7446 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 7447 " bbbbbbbbbbbbbbbbbbbbbbbb);", 7448 getLLVMStyleWithColumns(72)); 7449 EXPECT_EQ("static_cast<A< //\n" 7450 " B> *>(\n" 7451 "\n" 7452 ");", 7453 format("static_cast<A<//\n" 7454 " B>*>(\n" 7455 "\n" 7456 " );")); 7457 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7458 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 7459 7460 FormatStyle AlwaysBreak = getLLVMStyle(); 7461 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 7462 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 7463 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 7464 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 7465 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7466 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 7467 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 7468 verifyFormat("template <template <typename> class Fooooooo,\n" 7469 " template <typename> class Baaaaaaar>\n" 7470 "struct C {};", 7471 AlwaysBreak); 7472 verifyFormat("template <typename T> // T can be A, B or C.\n" 7473 "struct C {};", 7474 AlwaysBreak); 7475 verifyFormat("template <enum E> class A {\n" 7476 "public:\n" 7477 " E *f();\n" 7478 "};"); 7479 7480 FormatStyle NeverBreak = getLLVMStyle(); 7481 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 7482 verifyFormat("template <typename T> class C {};", NeverBreak); 7483 verifyFormat("template <typename T> void f();", NeverBreak); 7484 verifyFormat("template <typename T> void f() {}", NeverBreak); 7485 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 7486 "bbbbbbbbbbbbbbbbbbbb) {}", 7487 NeverBreak); 7488 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7489 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 7490 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 7491 NeverBreak); 7492 verifyFormat("template <template <typename> class Fooooooo,\n" 7493 " template <typename> class Baaaaaaar>\n" 7494 "struct C {};", 7495 NeverBreak); 7496 verifyFormat("template <typename T> // T can be A, B or C.\n" 7497 "struct C {};", 7498 NeverBreak); 7499 verifyFormat("template <enum E> class A {\n" 7500 "public:\n" 7501 " E *f();\n" 7502 "};", 7503 NeverBreak); 7504 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 7505 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 7506 "bbbbbbbbbbbbbbbbbbbb) {}", 7507 NeverBreak); 7508 } 7509 7510 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 7511 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 7512 Style.ColumnLimit = 60; 7513 EXPECT_EQ("// Baseline - no comments.\n" 7514 "template <\n" 7515 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 7516 "void f() {}", 7517 format("// Baseline - no comments.\n" 7518 "template <\n" 7519 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 7520 "void f() {}", 7521 Style)); 7522 7523 EXPECT_EQ("template <\n" 7524 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 7525 "void f() {}", 7526 format("template <\n" 7527 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 7528 "void f() {}", 7529 Style)); 7530 7531 EXPECT_EQ( 7532 "template <\n" 7533 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 7534 "void f() {}", 7535 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 7536 "void f() {}", 7537 Style)); 7538 7539 EXPECT_EQ( 7540 "template <\n" 7541 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 7542 " // multiline\n" 7543 "void f() {}", 7544 format("template <\n" 7545 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 7546 " // multiline\n" 7547 "void f() {}", 7548 Style)); 7549 7550 EXPECT_EQ( 7551 "template <typename aaaaaaaaaa<\n" 7552 " bbbbbbbbbbbb>::value> // trailing loooong\n" 7553 "void f() {}", 7554 format( 7555 "template <\n" 7556 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 7557 "void f() {}", 7558 Style)); 7559 } 7560 7561 TEST_F(FormatTest, WrapsTemplateParameters) { 7562 FormatStyle Style = getLLVMStyle(); 7563 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7564 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 7565 verifyFormat( 7566 "template <typename... a> struct q {};\n" 7567 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 7568 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 7569 " y;", 7570 Style); 7571 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7572 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 7573 verifyFormat( 7574 "template <typename... a> struct r {};\n" 7575 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 7576 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 7577 " y;", 7578 Style); 7579 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7580 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 7581 verifyFormat("template <typename... a> struct s {};\n" 7582 "extern s<\n" 7583 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 7584 "aaaaaaaaaaaaaaaaaaaaaa,\n" 7585 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 7586 "aaaaaaaaaaaaaaaaaaaaaa>\n" 7587 " y;", 7588 Style); 7589 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7590 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 7591 verifyFormat("template <typename... a> struct t {};\n" 7592 "extern t<\n" 7593 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 7594 "aaaaaaaaaaaaaaaaaaaaaa,\n" 7595 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 7596 "aaaaaaaaaaaaaaaaaaaaaa>\n" 7597 " y;", 7598 Style); 7599 } 7600 7601 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 7602 verifyFormat( 7603 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 7604 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7605 verifyFormat( 7606 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 7607 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7608 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 7609 7610 // FIXME: Should we have the extra indent after the second break? 7611 verifyFormat( 7612 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 7613 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 7614 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7615 7616 verifyFormat( 7617 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 7618 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 7619 7620 // Breaking at nested name specifiers is generally not desirable. 7621 verifyFormat( 7622 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7623 " aaaaaaaaaaaaaaaaaaaaaaa);"); 7624 7625 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 7626 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 7627 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7628 " aaaaaaaaaaaaaaaaaaaaa);", 7629 getLLVMStyleWithColumns(74)); 7630 7631 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 7632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7633 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7634 } 7635 7636 TEST_F(FormatTest, UnderstandsTemplateParameters) { 7637 verifyFormat("A<int> a;"); 7638 verifyFormat("A<A<A<int>>> a;"); 7639 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 7640 verifyFormat("bool x = a < 1 || 2 > a;"); 7641 verifyFormat("bool x = 5 < f<int>();"); 7642 verifyFormat("bool x = f<int>() > 5;"); 7643 verifyFormat("bool x = 5 < a<int>::x;"); 7644 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 7645 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 7646 7647 verifyGoogleFormat("A<A<int>> a;"); 7648 verifyGoogleFormat("A<A<A<int>>> a;"); 7649 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 7650 verifyGoogleFormat("A<A<int> > a;"); 7651 verifyGoogleFormat("A<A<A<int> > > a;"); 7652 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 7653 verifyGoogleFormat("A<::A<int>> a;"); 7654 verifyGoogleFormat("A<::A> a;"); 7655 verifyGoogleFormat("A< ::A> a;"); 7656 verifyGoogleFormat("A< ::A<int> > a;"); 7657 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 7658 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 7659 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 7660 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 7661 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 7662 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 7663 7664 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 7665 7666 // template closer followed by a token that starts with > or = 7667 verifyFormat("bool b = a<1> > 1;"); 7668 verifyFormat("bool b = a<1> >= 1;"); 7669 verifyFormat("int i = a<1> >> 1;"); 7670 FormatStyle Style = getLLVMStyle(); 7671 Style.SpaceBeforeAssignmentOperators = false; 7672 verifyFormat("bool b= a<1> == 1;", Style); 7673 verifyFormat("a<int> = 1;", Style); 7674 verifyFormat("a<int> >>= 1;", Style); 7675 7676 verifyFormat("test >> a >> b;"); 7677 verifyFormat("test << a >> b;"); 7678 7679 verifyFormat("f<int>();"); 7680 verifyFormat("template <typename T> void f() {}"); 7681 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 7682 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 7683 "sizeof(char)>::type>;"); 7684 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 7685 verifyFormat("f(a.operator()<A>());"); 7686 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7687 " .template operator()<A>());", 7688 getLLVMStyleWithColumns(35)); 7689 7690 // Not template parameters. 7691 verifyFormat("return a < b && c > d;"); 7692 verifyFormat("void f() {\n" 7693 " while (a < b && c > d) {\n" 7694 " }\n" 7695 "}"); 7696 verifyFormat("template <typename... Types>\n" 7697 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 7698 7699 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7700 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 7701 getLLVMStyleWithColumns(60)); 7702 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 7703 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 7704 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 7705 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 7706 } 7707 7708 TEST_F(FormatTest, UnderstandsShiftOperators) { 7709 verifyFormat("if (i < x >> 1)"); 7710 verifyFormat("while (i < x >> 1)"); 7711 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 7712 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 7713 verifyFormat( 7714 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 7715 verifyFormat("Foo.call<Bar<Function>>()"); 7716 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 7717 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 7718 "++i, v = v >> 1)"); 7719 verifyFormat("if (w<u<v<x>>, 1>::t)"); 7720 } 7721 7722 TEST_F(FormatTest, BitshiftOperatorWidth) { 7723 EXPECT_EQ("int a = 1 << 2; /* foo\n" 7724 " bar */", 7725 format("int a=1<<2; /* foo\n" 7726 " bar */")); 7727 7728 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 7729 " bar */", 7730 format("int b =256>>1 ; /* foo\n" 7731 " bar */")); 7732 } 7733 7734 TEST_F(FormatTest, UnderstandsBinaryOperators) { 7735 verifyFormat("COMPARE(a, ==, b);"); 7736 verifyFormat("auto s = sizeof...(Ts) - 1;"); 7737 } 7738 7739 TEST_F(FormatTest, UnderstandsPointersToMembers) { 7740 verifyFormat("int A::*x;"); 7741 verifyFormat("int (S::*func)(void *);"); 7742 verifyFormat("void f() { int (S::*func)(void *); }"); 7743 verifyFormat("typedef bool *(Class::*Member)() const;"); 7744 verifyFormat("void f() {\n" 7745 " (a->*f)();\n" 7746 " a->*x;\n" 7747 " (a.*f)();\n" 7748 " ((*a).*f)();\n" 7749 " a.*x;\n" 7750 "}"); 7751 verifyFormat("void f() {\n" 7752 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 7753 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 7754 "}"); 7755 verifyFormat( 7756 "(aaaaaaaaaa->*bbbbbbb)(\n" 7757 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7758 FormatStyle Style = getLLVMStyle(); 7759 Style.PointerAlignment = FormatStyle::PAS_Left; 7760 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 7761 } 7762 7763 TEST_F(FormatTest, UnderstandsUnaryOperators) { 7764 verifyFormat("int a = -2;"); 7765 verifyFormat("f(-1, -2, -3);"); 7766 verifyFormat("a[-1] = 5;"); 7767 verifyFormat("int a = 5 + -2;"); 7768 verifyFormat("if (i == -1) {\n}"); 7769 verifyFormat("if (i != -1) {\n}"); 7770 verifyFormat("if (i > -1) {\n}"); 7771 verifyFormat("if (i < -1) {\n}"); 7772 verifyFormat("++(a->f());"); 7773 verifyFormat("--(a->f());"); 7774 verifyFormat("(a->f())++;"); 7775 verifyFormat("a[42]++;"); 7776 verifyFormat("if (!(a->f())) {\n}"); 7777 verifyFormat("if (!+i) {\n}"); 7778 verifyFormat("~&a;"); 7779 7780 verifyFormat("a-- > b;"); 7781 verifyFormat("b ? -a : c;"); 7782 verifyFormat("n * sizeof char16;"); 7783 verifyFormat("n * alignof char16;", getGoogleStyle()); 7784 verifyFormat("sizeof(char);"); 7785 verifyFormat("alignof(char);", getGoogleStyle()); 7786 7787 verifyFormat("return -1;"); 7788 verifyFormat("throw -1;"); 7789 verifyFormat("switch (a) {\n" 7790 "case -1:\n" 7791 " break;\n" 7792 "}"); 7793 verifyFormat("#define X -1"); 7794 verifyFormat("#define X -kConstant"); 7795 7796 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 7797 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 7798 7799 verifyFormat("int a = /* confusing comment */ -1;"); 7800 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 7801 verifyFormat("int a = i /* confusing comment */++;"); 7802 7803 verifyFormat("co_yield -1;"); 7804 verifyFormat("co_return -1;"); 7805 7806 // Check that * is not treated as a binary operator when we set 7807 // PointerAlignment as PAS_Left after a keyword and not a declaration. 7808 FormatStyle PASLeftStyle = getLLVMStyle(); 7809 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 7810 verifyFormat("co_return *a;", PASLeftStyle); 7811 verifyFormat("co_await *a;", PASLeftStyle); 7812 verifyFormat("co_yield *a", PASLeftStyle); 7813 verifyFormat("return *a;", PASLeftStyle); 7814 } 7815 7816 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 7817 verifyFormat("if (!aaaaaaaaaa( // break\n" 7818 " aaaaa)) {\n" 7819 "}"); 7820 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 7821 " aaaaa));"); 7822 verifyFormat("*aaa = aaaaaaa( // break\n" 7823 " bbbbbb);"); 7824 } 7825 7826 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 7827 verifyFormat("bool operator<();"); 7828 verifyFormat("bool operator>();"); 7829 verifyFormat("bool operator=();"); 7830 verifyFormat("bool operator==();"); 7831 verifyFormat("bool operator!=();"); 7832 verifyFormat("int operator+();"); 7833 verifyFormat("int operator++();"); 7834 verifyFormat("int operator++(int) volatile noexcept;"); 7835 verifyFormat("bool operator,();"); 7836 verifyFormat("bool operator();"); 7837 verifyFormat("bool operator()();"); 7838 verifyFormat("bool operator[]();"); 7839 verifyFormat("operator bool();"); 7840 verifyFormat("operator int();"); 7841 verifyFormat("operator void *();"); 7842 verifyFormat("operator SomeType<int>();"); 7843 verifyFormat("operator SomeType<int, int>();"); 7844 verifyFormat("operator SomeType<SomeType<int>>();"); 7845 verifyFormat("void *operator new(std::size_t size);"); 7846 verifyFormat("void *operator new[](std::size_t size);"); 7847 verifyFormat("void operator delete(void *ptr);"); 7848 verifyFormat("void operator delete[](void *ptr);"); 7849 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 7850 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 7851 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 7852 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 7853 7854 verifyFormat( 7855 "ostream &operator<<(ostream &OutputStream,\n" 7856 " SomeReallyLongType WithSomeReallyLongValue);"); 7857 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 7858 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 7859 " return left.group < right.group;\n" 7860 "}"); 7861 verifyFormat("SomeType &operator=(const SomeType &S);"); 7862 verifyFormat("f.template operator()<int>();"); 7863 7864 verifyGoogleFormat("operator void*();"); 7865 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 7866 verifyGoogleFormat("operator ::A();"); 7867 7868 verifyFormat("using A::operator+;"); 7869 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 7870 "int i;"); 7871 } 7872 7873 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 7874 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 7875 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 7876 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 7877 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 7878 verifyFormat("Deleted &operator=(const Deleted &) &;"); 7879 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 7880 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 7881 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 7882 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 7883 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 7884 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 7885 verifyFormat("void Fn(T const &) const &;"); 7886 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 7887 verifyFormat("template <typename T>\n" 7888 "void F(T) && = delete;", 7889 getGoogleStyle()); 7890 7891 FormatStyle AlignLeft = getLLVMStyle(); 7892 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 7893 verifyFormat("void A::b() && {}", AlignLeft); 7894 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 7895 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 7896 AlignLeft); 7897 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 7898 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 7899 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 7900 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 7901 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 7902 verifyFormat("auto Function(T) & -> void;", AlignLeft); 7903 verifyFormat("void Fn(T const&) const&;", AlignLeft); 7904 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 7905 7906 FormatStyle Spaces = getLLVMStyle(); 7907 Spaces.SpacesInCStyleCastParentheses = true; 7908 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 7909 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 7910 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 7911 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 7912 7913 Spaces.SpacesInCStyleCastParentheses = false; 7914 Spaces.SpacesInParentheses = true; 7915 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 7916 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 7917 Spaces); 7918 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 7919 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 7920 7921 FormatStyle BreakTemplate = getLLVMStyle(); 7922 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 7923 7924 verifyFormat("struct f {\n" 7925 " template <class T>\n" 7926 " int &foo(const std::string &str) &noexcept {}\n" 7927 "};", 7928 BreakTemplate); 7929 7930 verifyFormat("struct f {\n" 7931 " template <class T>\n" 7932 " int &foo(const std::string &str) &&noexcept {}\n" 7933 "};", 7934 BreakTemplate); 7935 7936 verifyFormat("struct f {\n" 7937 " template <class T>\n" 7938 " int &foo(const std::string &str) const &noexcept {}\n" 7939 "};", 7940 BreakTemplate); 7941 7942 verifyFormat("struct f {\n" 7943 " template <class T>\n" 7944 " int &foo(const std::string &str) const &noexcept {}\n" 7945 "};", 7946 BreakTemplate); 7947 7948 verifyFormat("struct f {\n" 7949 " template <class T>\n" 7950 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 7951 "};", 7952 BreakTemplate); 7953 7954 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 7955 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 7956 FormatStyle::BTDS_Yes; 7957 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 7958 7959 verifyFormat("struct f {\n" 7960 " template <class T>\n" 7961 " int& foo(const std::string& str) & noexcept {}\n" 7962 "};", 7963 AlignLeftBreakTemplate); 7964 7965 verifyFormat("struct f {\n" 7966 " template <class T>\n" 7967 " int& foo(const std::string& str) && noexcept {}\n" 7968 "};", 7969 AlignLeftBreakTemplate); 7970 7971 verifyFormat("struct f {\n" 7972 " template <class T>\n" 7973 " int& foo(const std::string& str) const& noexcept {}\n" 7974 "};", 7975 AlignLeftBreakTemplate); 7976 7977 verifyFormat("struct f {\n" 7978 " template <class T>\n" 7979 " int& foo(const std::string& str) const&& noexcept {}\n" 7980 "};", 7981 AlignLeftBreakTemplate); 7982 7983 verifyFormat("struct f {\n" 7984 " template <class T>\n" 7985 " auto foo(const std::string& str) && noexcept -> int& {}\n" 7986 "};", 7987 AlignLeftBreakTemplate); 7988 7989 // The `&` in `Type&` should not be confused with a trailing `&` of 7990 // DEPRECATED(reason) member function. 7991 verifyFormat("struct f {\n" 7992 " template <class T>\n" 7993 " DEPRECATED(reason)\n" 7994 " Type &foo(arguments) {}\n" 7995 "};", 7996 BreakTemplate); 7997 7998 verifyFormat("struct f {\n" 7999 " template <class T>\n" 8000 " DEPRECATED(reason)\n" 8001 " Type& foo(arguments) {}\n" 8002 "};", 8003 AlignLeftBreakTemplate); 8004 8005 verifyFormat("void (*foopt)(int) = &func;"); 8006 } 8007 8008 TEST_F(FormatTest, UnderstandsNewAndDelete) { 8009 verifyFormat("void f() {\n" 8010 " A *a = new A;\n" 8011 " A *a = new (placement) A;\n" 8012 " delete a;\n" 8013 " delete (A *)a;\n" 8014 "}"); 8015 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 8016 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 8017 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8018 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 8019 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 8020 verifyFormat("delete[] h->p;"); 8021 } 8022 8023 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 8024 verifyFormat("int *f(int *a) {}"); 8025 verifyFormat("int main(int argc, char **argv) {}"); 8026 verifyFormat("Test::Test(int b) : a(b * b) {}"); 8027 verifyIndependentOfContext("f(a, *a);"); 8028 verifyFormat("void g() { f(*a); }"); 8029 verifyIndependentOfContext("int a = b * 10;"); 8030 verifyIndependentOfContext("int a = 10 * b;"); 8031 verifyIndependentOfContext("int a = b * c;"); 8032 verifyIndependentOfContext("int a += b * c;"); 8033 verifyIndependentOfContext("int a -= b * c;"); 8034 verifyIndependentOfContext("int a *= b * c;"); 8035 verifyIndependentOfContext("int a /= b * c;"); 8036 verifyIndependentOfContext("int a = *b;"); 8037 verifyIndependentOfContext("int a = *b * c;"); 8038 verifyIndependentOfContext("int a = b * *c;"); 8039 verifyIndependentOfContext("int a = b * (10);"); 8040 verifyIndependentOfContext("S << b * (10);"); 8041 verifyIndependentOfContext("return 10 * b;"); 8042 verifyIndependentOfContext("return *b * *c;"); 8043 verifyIndependentOfContext("return a & ~b;"); 8044 verifyIndependentOfContext("f(b ? *c : *d);"); 8045 verifyIndependentOfContext("int a = b ? *c : *d;"); 8046 verifyIndependentOfContext("*b = a;"); 8047 verifyIndependentOfContext("a * ~b;"); 8048 verifyIndependentOfContext("a * !b;"); 8049 verifyIndependentOfContext("a * +b;"); 8050 verifyIndependentOfContext("a * -b;"); 8051 verifyIndependentOfContext("a * ++b;"); 8052 verifyIndependentOfContext("a * --b;"); 8053 verifyIndependentOfContext("a[4] * b;"); 8054 verifyIndependentOfContext("a[a * a] = 1;"); 8055 verifyIndependentOfContext("f() * b;"); 8056 verifyIndependentOfContext("a * [self dostuff];"); 8057 verifyIndependentOfContext("int x = a * (a + b);"); 8058 verifyIndependentOfContext("(a *)(a + b);"); 8059 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 8060 verifyIndependentOfContext("int *pa = (int *)&a;"); 8061 verifyIndependentOfContext("return sizeof(int **);"); 8062 verifyIndependentOfContext("return sizeof(int ******);"); 8063 verifyIndependentOfContext("return (int **&)a;"); 8064 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 8065 verifyFormat("void f(Type (*parameter)[10]) {}"); 8066 verifyFormat("void f(Type (¶meter)[10]) {}"); 8067 verifyGoogleFormat("return sizeof(int**);"); 8068 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 8069 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 8070 verifyFormat("auto a = [](int **&, int ***) {};"); 8071 verifyFormat("auto PointerBinding = [](const char *S) {};"); 8072 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 8073 verifyFormat("[](const decltype(*a) &value) {}"); 8074 verifyFormat("[](const typeof(*a) &value) {}"); 8075 verifyFormat("[](const _Atomic(a *) &value) {}"); 8076 verifyFormat("[](const __underlying_type(a) &value) {}"); 8077 verifyFormat("decltype(a * b) F();"); 8078 verifyFormat("typeof(a * b) F();"); 8079 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 8080 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 8081 verifyIndependentOfContext("typedef void (*f)(int *a);"); 8082 verifyIndependentOfContext("int i{a * b};"); 8083 verifyIndependentOfContext("aaa && aaa->f();"); 8084 verifyIndependentOfContext("int x = ~*p;"); 8085 verifyFormat("Constructor() : a(a), area(width * height) {}"); 8086 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 8087 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 8088 verifyFormat("void f() { f(a, c * d); }"); 8089 verifyFormat("void f() { f(new a(), c * d); }"); 8090 verifyFormat("void f(const MyOverride &override);"); 8091 verifyFormat("void f(const MyFinal &final);"); 8092 verifyIndependentOfContext("bool a = f() && override.f();"); 8093 verifyIndependentOfContext("bool a = f() && final.f();"); 8094 8095 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 8096 8097 verifyIndependentOfContext("A<int *> a;"); 8098 verifyIndependentOfContext("A<int **> a;"); 8099 verifyIndependentOfContext("A<int *, int *> a;"); 8100 verifyIndependentOfContext("A<int *[]> a;"); 8101 verifyIndependentOfContext( 8102 "const char *const p = reinterpret_cast<const char *const>(q);"); 8103 verifyIndependentOfContext("A<int **, int **> a;"); 8104 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 8105 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 8106 verifyFormat("for (; a && b;) {\n}"); 8107 verifyFormat("bool foo = true && [] { return false; }();"); 8108 8109 verifyFormat( 8110 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8111 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8112 8113 verifyGoogleFormat("int const* a = &b;"); 8114 verifyGoogleFormat("**outparam = 1;"); 8115 verifyGoogleFormat("*outparam = a * b;"); 8116 verifyGoogleFormat("int main(int argc, char** argv) {}"); 8117 verifyGoogleFormat("A<int*> a;"); 8118 verifyGoogleFormat("A<int**> a;"); 8119 verifyGoogleFormat("A<int*, int*> a;"); 8120 verifyGoogleFormat("A<int**, int**> a;"); 8121 verifyGoogleFormat("f(b ? *c : *d);"); 8122 verifyGoogleFormat("int a = b ? *c : *d;"); 8123 verifyGoogleFormat("Type* t = **x;"); 8124 verifyGoogleFormat("Type* t = *++*x;"); 8125 verifyGoogleFormat("*++*x;"); 8126 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 8127 verifyGoogleFormat("Type* t = x++ * y;"); 8128 verifyGoogleFormat( 8129 "const char* const p = reinterpret_cast<const char* const>(q);"); 8130 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 8131 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 8132 verifyGoogleFormat("template <typename T>\n" 8133 "void f(int i = 0, SomeType** temps = NULL);"); 8134 8135 FormatStyle Left = getLLVMStyle(); 8136 Left.PointerAlignment = FormatStyle::PAS_Left; 8137 verifyFormat("x = *a(x) = *a(y);", Left); 8138 verifyFormat("for (;; *a = b) {\n}", Left); 8139 verifyFormat("return *this += 1;", Left); 8140 verifyFormat("throw *x;", Left); 8141 verifyFormat("delete *x;", Left); 8142 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 8143 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 8144 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 8145 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 8146 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 8147 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 8148 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 8149 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 8150 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 8151 8152 verifyIndependentOfContext("a = *(x + y);"); 8153 verifyIndependentOfContext("a = &(x + y);"); 8154 verifyIndependentOfContext("*(x + y).call();"); 8155 verifyIndependentOfContext("&(x + y)->call();"); 8156 verifyFormat("void f() { &(*I).first; }"); 8157 8158 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 8159 verifyFormat( 8160 "int *MyValues = {\n" 8161 " *A, // Operator detection might be confused by the '{'\n" 8162 " *BB // Operator detection might be confused by previous comment\n" 8163 "};"); 8164 8165 verifyIndependentOfContext("if (int *a = &b)"); 8166 verifyIndependentOfContext("if (int &a = *b)"); 8167 verifyIndependentOfContext("if (a & b[i])"); 8168 verifyIndependentOfContext("if constexpr (a & b[i])"); 8169 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 8170 verifyIndependentOfContext("if (a * (b * c))"); 8171 verifyIndependentOfContext("if constexpr (a * (b * c))"); 8172 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 8173 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 8174 verifyIndependentOfContext("if (*b[i])"); 8175 verifyIndependentOfContext("if (int *a = (&b))"); 8176 verifyIndependentOfContext("while (int *a = &b)"); 8177 verifyIndependentOfContext("while (a * (b * c))"); 8178 verifyIndependentOfContext("size = sizeof *a;"); 8179 verifyIndependentOfContext("if (a && (b = c))"); 8180 verifyFormat("void f() {\n" 8181 " for (const int &v : Values) {\n" 8182 " }\n" 8183 "}"); 8184 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 8185 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 8186 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 8187 8188 verifyFormat("#define A (!a * b)"); 8189 verifyFormat("#define MACRO \\\n" 8190 " int *i = a * b; \\\n" 8191 " void f(a *b);", 8192 getLLVMStyleWithColumns(19)); 8193 8194 verifyIndependentOfContext("A = new SomeType *[Length];"); 8195 verifyIndependentOfContext("A = new SomeType *[Length]();"); 8196 verifyIndependentOfContext("T **t = new T *;"); 8197 verifyIndependentOfContext("T **t = new T *();"); 8198 verifyGoogleFormat("A = new SomeType*[Length]();"); 8199 verifyGoogleFormat("A = new SomeType*[Length];"); 8200 verifyGoogleFormat("T** t = new T*;"); 8201 verifyGoogleFormat("T** t = new T*();"); 8202 8203 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 8204 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 8205 verifyFormat("template <bool a, bool b> " 8206 "typename t::if<x && y>::type f() {}"); 8207 verifyFormat("template <int *y> f() {}"); 8208 verifyFormat("vector<int *> v;"); 8209 verifyFormat("vector<int *const> v;"); 8210 verifyFormat("vector<int *const **const *> v;"); 8211 verifyFormat("vector<int *volatile> v;"); 8212 verifyFormat("vector<a *_Nonnull> v;"); 8213 verifyFormat("vector<a *_Nullable> v;"); 8214 verifyFormat("vector<a *_Null_unspecified> v;"); 8215 verifyFormat("vector<a *__ptr32> v;"); 8216 verifyFormat("vector<a *__ptr64> v;"); 8217 verifyFormat("vector<a *__capability> v;"); 8218 FormatStyle TypeMacros = getLLVMStyle(); 8219 TypeMacros.TypenameMacros = {"LIST"}; 8220 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 8221 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 8222 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 8223 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 8224 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 8225 8226 FormatStyle CustomQualifier = getLLVMStyle(); 8227 // Add indentifers that should not be parsed as a qualifier by default. 8228 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 8229 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 8230 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 8231 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 8232 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 8233 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 8234 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 8235 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 8236 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 8237 verifyFormat("vector<a * _NotAQualifier> v;"); 8238 verifyFormat("vector<a * __not_a_qualifier> v;"); 8239 verifyFormat("vector<a * b> v;"); 8240 verifyFormat("foo<b && false>();"); 8241 verifyFormat("foo<b & 1>();"); 8242 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 8243 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 8244 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 8245 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 8246 verifyFormat( 8247 "template <class T, class = typename std::enable_if<\n" 8248 " std::is_integral<T>::value &&\n" 8249 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 8250 "void F();", 8251 getLLVMStyleWithColumns(70)); 8252 verifyFormat("template <class T,\n" 8253 " class = typename std::enable_if<\n" 8254 " std::is_integral<T>::value &&\n" 8255 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 8256 " class U>\n" 8257 "void F();", 8258 getLLVMStyleWithColumns(70)); 8259 verifyFormat( 8260 "template <class T,\n" 8261 " class = typename ::std::enable_if<\n" 8262 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 8263 "void F();", 8264 getGoogleStyleWithColumns(68)); 8265 8266 verifyIndependentOfContext("MACRO(int *i);"); 8267 verifyIndependentOfContext("MACRO(auto *a);"); 8268 verifyIndependentOfContext("MACRO(const A *a);"); 8269 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 8270 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 8271 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 8272 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 8273 verifyIndependentOfContext("MACRO(A *const a);"); 8274 verifyIndependentOfContext("MACRO(A *restrict a);"); 8275 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 8276 verifyIndependentOfContext("MACRO(A *__restrict a);"); 8277 verifyIndependentOfContext("MACRO(A *volatile a);"); 8278 verifyIndependentOfContext("MACRO(A *__volatile a);"); 8279 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 8280 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 8281 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 8282 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 8283 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 8284 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 8285 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 8286 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 8287 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 8288 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 8289 verifyIndependentOfContext("MACRO(A *__capability);"); 8290 verifyIndependentOfContext("MACRO(A &__capability);"); 8291 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 8292 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 8293 // If we add __my_qualifier to AttributeMacros it should always be parsed as 8294 // a type declaration: 8295 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 8296 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 8297 // Also check that TypenameMacros prevents parsing it as multiplication: 8298 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 8299 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 8300 8301 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 8302 verifyFormat("void f() { f(float{1}, a * a); }"); 8303 // FIXME: Is there a way to make this work? 8304 // verifyIndependentOfContext("MACRO(A *a);"); 8305 verifyFormat("MACRO(A &B);"); 8306 verifyFormat("MACRO(A *B);"); 8307 verifyFormat("void f() { MACRO(A * B); }"); 8308 verifyFormat("void f() { MACRO(A & B); }"); 8309 8310 // This lambda was mis-formatted after D88956 (treating it as a binop): 8311 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 8312 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 8313 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 8314 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 8315 8316 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 8317 verifyFormat("return options != nullptr && operator==(*options);"); 8318 8319 EXPECT_EQ("#define OP(x) \\\n" 8320 " ostream &operator<<(ostream &s, const A &a) { \\\n" 8321 " return s << a.DebugString(); \\\n" 8322 " }", 8323 format("#define OP(x) \\\n" 8324 " ostream &operator<<(ostream &s, const A &a) { \\\n" 8325 " return s << a.DebugString(); \\\n" 8326 " }", 8327 getLLVMStyleWithColumns(50))); 8328 8329 // FIXME: We cannot handle this case yet; we might be able to figure out that 8330 // foo<x> d > v; doesn't make sense. 8331 verifyFormat("foo<a<b && c> d> v;"); 8332 8333 FormatStyle PointerMiddle = getLLVMStyle(); 8334 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 8335 verifyFormat("delete *x;", PointerMiddle); 8336 verifyFormat("int * x;", PointerMiddle); 8337 verifyFormat("int *[] x;", PointerMiddle); 8338 verifyFormat("template <int * y> f() {}", PointerMiddle); 8339 verifyFormat("int * f(int * a) {}", PointerMiddle); 8340 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 8341 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 8342 verifyFormat("A<int *> a;", PointerMiddle); 8343 verifyFormat("A<int **> a;", PointerMiddle); 8344 verifyFormat("A<int *, int *> a;", PointerMiddle); 8345 verifyFormat("A<int *[]> a;", PointerMiddle); 8346 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 8347 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 8348 verifyFormat("T ** t = new T *;", PointerMiddle); 8349 8350 // Member function reference qualifiers aren't binary operators. 8351 verifyFormat("string // break\n" 8352 "operator()() & {}"); 8353 verifyFormat("string // break\n" 8354 "operator()() && {}"); 8355 verifyGoogleFormat("template <typename T>\n" 8356 "auto x() & -> int {}"); 8357 } 8358 8359 TEST_F(FormatTest, UnderstandsAttributes) { 8360 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 8361 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 8362 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 8363 FormatStyle AfterType = getLLVMStyle(); 8364 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8365 verifyFormat("__attribute__((nodebug)) void\n" 8366 "foo() {}\n", 8367 AfterType); 8368 verifyFormat("__unused void\n" 8369 "foo() {}", 8370 AfterType); 8371 8372 FormatStyle CustomAttrs = getLLVMStyle(); 8373 CustomAttrs.AttributeMacros.push_back("__unused"); 8374 CustomAttrs.AttributeMacros.push_back("__attr1"); 8375 CustomAttrs.AttributeMacros.push_back("__attr2"); 8376 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 8377 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 8378 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 8379 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 8380 // Check that it is parsed as a multiplication without AttributeMacros and 8381 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 8382 verifyFormat("vector<SomeType * __attr1> v;"); 8383 verifyFormat("vector<SomeType __attr1 *> v;"); 8384 verifyFormat("vector<SomeType __attr1 *const> v;"); 8385 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 8386 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 8387 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 8388 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 8389 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 8390 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 8391 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 8392 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 8393 8394 // Check that these are not parsed as function declarations: 8395 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 8396 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 8397 verifyFormat("SomeType s(InitValue);", CustomAttrs); 8398 verifyFormat("SomeType s{InitValue};", CustomAttrs); 8399 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 8400 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 8401 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 8402 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 8403 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 8404 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 8405 } 8406 8407 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 8408 // Check that qualifiers on pointers don't break parsing of casts. 8409 verifyFormat("x = (foo *const)*v;"); 8410 verifyFormat("x = (foo *volatile)*v;"); 8411 verifyFormat("x = (foo *restrict)*v;"); 8412 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 8413 verifyFormat("x = (foo *_Nonnull)*v;"); 8414 verifyFormat("x = (foo *_Nullable)*v;"); 8415 verifyFormat("x = (foo *_Null_unspecified)*v;"); 8416 verifyFormat("x = (foo *_Nonnull)*v;"); 8417 verifyFormat("x = (foo *[[clang::attr]])*v;"); 8418 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 8419 verifyFormat("x = (foo *__ptr32)*v;"); 8420 verifyFormat("x = (foo *__ptr64)*v;"); 8421 verifyFormat("x = (foo *__capability)*v;"); 8422 8423 // Check that we handle multiple trailing qualifiers and skip them all to 8424 // determine that the expression is a cast to a pointer type. 8425 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 8426 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 8427 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 8428 StringRef AllQualifiers = 8429 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 8430 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 8431 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 8432 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 8433 8434 // Also check that address-of is not parsed as a binary bitwise-and: 8435 verifyFormat("x = (foo *const)&v;"); 8436 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 8437 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 8438 8439 // Check custom qualifiers: 8440 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 8441 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 8442 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 8443 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 8444 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 8445 CustomQualifier); 8446 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 8447 CustomQualifier); 8448 8449 // Check that unknown identifiers result in binary operator parsing: 8450 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 8451 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 8452 } 8453 8454 TEST_F(FormatTest, UnderstandsSquareAttributes) { 8455 verifyFormat("SomeType s [[unused]] (InitValue);"); 8456 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 8457 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 8458 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 8459 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 8460 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8461 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 8462 verifyFormat("[[nodiscard]] bool f() { return false; }"); 8463 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 8464 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 8465 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 8466 8467 // Make sure we do not mistake attributes for array subscripts. 8468 verifyFormat("int a() {}\n" 8469 "[[unused]] int b() {}\n"); 8470 verifyFormat("NSArray *arr;\n" 8471 "arr[[Foo() bar]];"); 8472 8473 // On the other hand, we still need to correctly find array subscripts. 8474 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 8475 8476 // Make sure that we do not mistake Objective-C method inside array literals 8477 // as attributes, even if those method names are also keywords. 8478 verifyFormat("@[ [foo bar] ];"); 8479 verifyFormat("@[ [NSArray class] ];"); 8480 verifyFormat("@[ [foo enum] ];"); 8481 8482 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 8483 8484 // Make sure we do not parse attributes as lambda introducers. 8485 FormatStyle MultiLineFunctions = getLLVMStyle(); 8486 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 8487 verifyFormat("[[unused]] int b() {\n" 8488 " return 42;\n" 8489 "}\n", 8490 MultiLineFunctions); 8491 } 8492 8493 TEST_F(FormatTest, AttributeClass) { 8494 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 8495 verifyFormat("class S {\n" 8496 " S(S&&) = default;\n" 8497 "};", 8498 Style); 8499 verifyFormat("class [[nodiscard]] S {\n" 8500 " S(S&&) = default;\n" 8501 "};", 8502 Style); 8503 verifyFormat("class __attribute((maybeunused)) S {\n" 8504 " S(S&&) = default;\n" 8505 "};", 8506 Style); 8507 verifyFormat("struct S {\n" 8508 " S(S&&) = default;\n" 8509 "};", 8510 Style); 8511 verifyFormat("struct [[nodiscard]] S {\n" 8512 " S(S&&) = default;\n" 8513 "};", 8514 Style); 8515 } 8516 8517 TEST_F(FormatTest, AttributesAfterMacro) { 8518 FormatStyle Style = getLLVMStyle(); 8519 verifyFormat("MACRO;\n" 8520 "__attribute__((maybe_unused)) int foo() {\n" 8521 " //...\n" 8522 "}"); 8523 8524 verifyFormat("MACRO;\n" 8525 "[[nodiscard]] int foo() {\n" 8526 " //...\n" 8527 "}"); 8528 8529 EXPECT_EQ("MACRO\n\n" 8530 "__attribute__((maybe_unused)) int foo() {\n" 8531 " //...\n" 8532 "}", 8533 format("MACRO\n\n" 8534 "__attribute__((maybe_unused)) int foo() {\n" 8535 " //...\n" 8536 "}")); 8537 8538 EXPECT_EQ("MACRO\n\n" 8539 "[[nodiscard]] int foo() {\n" 8540 " //...\n" 8541 "}", 8542 format("MACRO\n\n" 8543 "[[nodiscard]] int foo() {\n" 8544 " //...\n" 8545 "}")); 8546 } 8547 8548 TEST_F(FormatTest, AttributePenaltyBreaking) { 8549 FormatStyle Style = getLLVMStyle(); 8550 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 8551 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 8552 Style); 8553 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 8554 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 8555 Style); 8556 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 8557 "shared_ptr<ALongTypeName> &C d) {\n}", 8558 Style); 8559 } 8560 8561 TEST_F(FormatTest, UnderstandsEllipsis) { 8562 FormatStyle Style = getLLVMStyle(); 8563 verifyFormat("int printf(const char *fmt, ...);"); 8564 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 8565 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 8566 8567 verifyFormat("template <int *...PP> a;", Style); 8568 8569 Style.PointerAlignment = FormatStyle::PAS_Left; 8570 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 8571 8572 verifyFormat("template <int*... PP> a;", Style); 8573 8574 Style.PointerAlignment = FormatStyle::PAS_Middle; 8575 verifyFormat("template <int *... PP> a;", Style); 8576 } 8577 8578 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 8579 EXPECT_EQ("int *a;\n" 8580 "int *a;\n" 8581 "int *a;", 8582 format("int *a;\n" 8583 "int* a;\n" 8584 "int *a;", 8585 getGoogleStyle())); 8586 EXPECT_EQ("int* a;\n" 8587 "int* a;\n" 8588 "int* a;", 8589 format("int* a;\n" 8590 "int* a;\n" 8591 "int *a;", 8592 getGoogleStyle())); 8593 EXPECT_EQ("int *a;\n" 8594 "int *a;\n" 8595 "int *a;", 8596 format("int *a;\n" 8597 "int * a;\n" 8598 "int * a;", 8599 getGoogleStyle())); 8600 EXPECT_EQ("auto x = [] {\n" 8601 " int *a;\n" 8602 " int *a;\n" 8603 " int *a;\n" 8604 "};", 8605 format("auto x=[]{int *a;\n" 8606 "int * a;\n" 8607 "int * a;};", 8608 getGoogleStyle())); 8609 } 8610 8611 TEST_F(FormatTest, UnderstandsRvalueReferences) { 8612 verifyFormat("int f(int &&a) {}"); 8613 verifyFormat("int f(int a, char &&b) {}"); 8614 verifyFormat("void f() { int &&a = b; }"); 8615 verifyGoogleFormat("int f(int a, char&& b) {}"); 8616 verifyGoogleFormat("void f() { int&& a = b; }"); 8617 8618 verifyIndependentOfContext("A<int &&> a;"); 8619 verifyIndependentOfContext("A<int &&, int &&> a;"); 8620 verifyGoogleFormat("A<int&&> a;"); 8621 verifyGoogleFormat("A<int&&, int&&> a;"); 8622 8623 // Not rvalue references: 8624 verifyFormat("template <bool B, bool C> class A {\n" 8625 " static_assert(B && C, \"Something is wrong\");\n" 8626 "};"); 8627 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 8628 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 8629 verifyFormat("#define A(a, b) (a && b)"); 8630 } 8631 8632 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 8633 verifyFormat("void f() {\n" 8634 " x[aaaaaaaaa -\n" 8635 " b] = 23;\n" 8636 "}", 8637 getLLVMStyleWithColumns(15)); 8638 } 8639 8640 TEST_F(FormatTest, FormatsCasts) { 8641 verifyFormat("Type *A = static_cast<Type *>(P);"); 8642 verifyFormat("Type *A = (Type *)P;"); 8643 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 8644 verifyFormat("int a = (int)(2.0f);"); 8645 verifyFormat("int a = (int)2.0f;"); 8646 verifyFormat("x[(int32)y];"); 8647 verifyFormat("x = (int32)y;"); 8648 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 8649 verifyFormat("int a = (int)*b;"); 8650 verifyFormat("int a = (int)2.0f;"); 8651 verifyFormat("int a = (int)~0;"); 8652 verifyFormat("int a = (int)++a;"); 8653 verifyFormat("int a = (int)sizeof(int);"); 8654 verifyFormat("int a = (int)+2;"); 8655 verifyFormat("my_int a = (my_int)2.0f;"); 8656 verifyFormat("my_int a = (my_int)sizeof(int);"); 8657 verifyFormat("return (my_int)aaa;"); 8658 verifyFormat("#define x ((int)-1)"); 8659 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 8660 verifyFormat("#define p(q) ((int *)&q)"); 8661 verifyFormat("fn(a)(b) + 1;"); 8662 8663 verifyFormat("void f() { my_int a = (my_int)*b; }"); 8664 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 8665 verifyFormat("my_int a = (my_int)~0;"); 8666 verifyFormat("my_int a = (my_int)++a;"); 8667 verifyFormat("my_int a = (my_int)-2;"); 8668 verifyFormat("my_int a = (my_int)1;"); 8669 verifyFormat("my_int a = (my_int *)1;"); 8670 verifyFormat("my_int a = (const my_int)-1;"); 8671 verifyFormat("my_int a = (const my_int *)-1;"); 8672 verifyFormat("my_int a = (my_int)(my_int)-1;"); 8673 verifyFormat("my_int a = (ns::my_int)-2;"); 8674 verifyFormat("case (my_int)ONE:"); 8675 verifyFormat("auto x = (X)this;"); 8676 // Casts in Obj-C style calls used to not be recognized as such. 8677 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 8678 8679 // FIXME: single value wrapped with paren will be treated as cast. 8680 verifyFormat("void f(int i = (kValue)*kMask) {}"); 8681 8682 verifyFormat("{ (void)F; }"); 8683 8684 // Don't break after a cast's 8685 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8686 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 8687 " bbbbbbbbbbbbbbbbbbbbbb);"); 8688 8689 // These are not casts. 8690 verifyFormat("void f(int *) {}"); 8691 verifyFormat("f(foo)->b;"); 8692 verifyFormat("f(foo).b;"); 8693 verifyFormat("f(foo)(b);"); 8694 verifyFormat("f(foo)[b];"); 8695 verifyFormat("[](foo) { return 4; }(bar);"); 8696 verifyFormat("(*funptr)(foo)[4];"); 8697 verifyFormat("funptrs[4](foo)[4];"); 8698 verifyFormat("void f(int *);"); 8699 verifyFormat("void f(int *) = 0;"); 8700 verifyFormat("void f(SmallVector<int>) {}"); 8701 verifyFormat("void f(SmallVector<int>);"); 8702 verifyFormat("void f(SmallVector<int>) = 0;"); 8703 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 8704 verifyFormat("int a = sizeof(int) * b;"); 8705 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 8706 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 8707 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 8708 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 8709 8710 // These are not casts, but at some point were confused with casts. 8711 verifyFormat("virtual void foo(int *) override;"); 8712 verifyFormat("virtual void foo(char &) const;"); 8713 verifyFormat("virtual void foo(int *a, char *) const;"); 8714 verifyFormat("int a = sizeof(int *) + b;"); 8715 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 8716 verifyFormat("bool b = f(g<int>) && c;"); 8717 verifyFormat("typedef void (*f)(int i) func;"); 8718 verifyFormat("void operator++(int) noexcept;"); 8719 verifyFormat("void operator++(int &) noexcept;"); 8720 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 8721 "&) noexcept;"); 8722 verifyFormat( 8723 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 8724 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 8725 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 8726 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 8727 verifyFormat("void operator delete(foo &) noexcept;"); 8728 verifyFormat("void operator delete(foo) noexcept;"); 8729 verifyFormat("void operator delete(int) noexcept;"); 8730 verifyFormat("void operator delete(int &) noexcept;"); 8731 verifyFormat("void operator delete(int &) volatile noexcept;"); 8732 verifyFormat("void operator delete(int &) const"); 8733 verifyFormat("void operator delete(int &) = default"); 8734 verifyFormat("void operator delete(int &) = delete"); 8735 verifyFormat("void operator delete(int &) [[noreturn]]"); 8736 verifyFormat("void operator delete(int &) throw();"); 8737 verifyFormat("void operator delete(int &) throw(int);"); 8738 verifyFormat("auto operator delete(int &) -> int;"); 8739 verifyFormat("auto operator delete(int &) override"); 8740 verifyFormat("auto operator delete(int &) final"); 8741 8742 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 8743 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8744 // FIXME: The indentation here is not ideal. 8745 verifyFormat( 8746 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8747 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 8748 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 8749 } 8750 8751 TEST_F(FormatTest, FormatsFunctionTypes) { 8752 verifyFormat("A<bool()> a;"); 8753 verifyFormat("A<SomeType()> a;"); 8754 verifyFormat("A<void (*)(int, std::string)> a;"); 8755 verifyFormat("A<void *(int)>;"); 8756 verifyFormat("void *(*a)(int *, SomeType *);"); 8757 verifyFormat("int (*func)(void *);"); 8758 verifyFormat("void f() { int (*func)(void *); }"); 8759 verifyFormat("template <class CallbackClass>\n" 8760 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 8761 8762 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 8763 verifyGoogleFormat("void* (*a)(int);"); 8764 verifyGoogleFormat( 8765 "template <class CallbackClass>\n" 8766 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 8767 8768 // Other constructs can look somewhat like function types: 8769 verifyFormat("A<sizeof(*x)> a;"); 8770 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 8771 verifyFormat("some_var = function(*some_pointer_var)[0];"); 8772 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 8773 verifyFormat("int x = f(&h)();"); 8774 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 8775 verifyFormat("std::function<\n" 8776 " LooooooooooongTemplatedType<\n" 8777 " SomeType>*(\n" 8778 " LooooooooooooooooongType type)>\n" 8779 " function;", 8780 getGoogleStyleWithColumns(40)); 8781 } 8782 8783 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 8784 verifyFormat("A (*foo_)[6];"); 8785 verifyFormat("vector<int> (*foo_)[6];"); 8786 } 8787 8788 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 8789 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 8790 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 8791 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 8792 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 8793 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 8794 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 8795 8796 // Different ways of ()-initializiation. 8797 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 8798 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 8799 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 8800 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 8801 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 8802 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 8803 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 8804 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 8805 8806 // Lambdas should not confuse the variable declaration heuristic. 8807 verifyFormat("LooooooooooooooooongType\n" 8808 " variable(nullptr, [](A *a) {});", 8809 getLLVMStyleWithColumns(40)); 8810 } 8811 8812 TEST_F(FormatTest, BreaksLongDeclarations) { 8813 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 8814 " AnotherNameForTheLongType;"); 8815 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 8816 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8817 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 8818 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 8819 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 8820 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 8821 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 8822 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 8823 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 8824 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 8825 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 8826 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 8827 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 8828 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 8829 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 8830 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 8831 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 8832 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 8833 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 8834 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 8835 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 8836 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 8837 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 8838 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 8839 FormatStyle Indented = getLLVMStyle(); 8840 Indented.IndentWrappedFunctionNames = true; 8841 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 8842 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 8843 Indented); 8844 verifyFormat( 8845 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 8846 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 8847 Indented); 8848 verifyFormat( 8849 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 8850 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 8851 Indented); 8852 verifyFormat( 8853 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 8854 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 8855 Indented); 8856 8857 // FIXME: Without the comment, this breaks after "(". 8858 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 8859 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 8860 getGoogleStyle()); 8861 8862 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 8863 " int LoooooooooooooooooooongParam2) {}"); 8864 verifyFormat( 8865 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 8866 " SourceLocation L, IdentifierIn *II,\n" 8867 " Type *T) {}"); 8868 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 8869 "ReallyReaaallyLongFunctionName(\n" 8870 " const std::string &SomeParameter,\n" 8871 " const SomeType<string, SomeOtherTemplateParameter>\n" 8872 " &ReallyReallyLongParameterName,\n" 8873 " const SomeType<string, SomeOtherTemplateParameter>\n" 8874 " &AnotherLongParameterName) {}"); 8875 verifyFormat("template <typename A>\n" 8876 "SomeLoooooooooooooooooooooongType<\n" 8877 " typename some_namespace::SomeOtherType<A>::Type>\n" 8878 "Function() {}"); 8879 8880 verifyGoogleFormat( 8881 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 8882 " aaaaaaaaaaaaaaaaaaaaaaa;"); 8883 verifyGoogleFormat( 8884 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 8885 " SourceLocation L) {}"); 8886 verifyGoogleFormat( 8887 "some_namespace::LongReturnType\n" 8888 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 8889 " int first_long_parameter, int second_parameter) {}"); 8890 8891 verifyGoogleFormat("template <typename T>\n" 8892 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 8893 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 8894 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8895 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 8896 8897 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 8898 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8899 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8900 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8901 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 8902 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 8903 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8904 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 8905 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 8906 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8907 8908 verifyFormat("template <typename T> // Templates on own line.\n" 8909 "static int // Some comment.\n" 8910 "MyFunction(int a);", 8911 getLLVMStyle()); 8912 } 8913 8914 TEST_F(FormatTest, FormatsAccessModifiers) { 8915 FormatStyle Style = getLLVMStyle(); 8916 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 8917 FormatStyle::ELBAMS_LogicalBlock); 8918 verifyFormat("struct foo {\n" 8919 "private:\n" 8920 " void f() {}\n" 8921 "\n" 8922 "private:\n" 8923 " int i;\n" 8924 "\n" 8925 "protected:\n" 8926 " int j;\n" 8927 "};\n", 8928 Style); 8929 verifyFormat("struct foo {\n" 8930 "private:\n" 8931 " void f() {}\n" 8932 "\n" 8933 "private:\n" 8934 " int i;\n" 8935 "\n" 8936 "protected:\n" 8937 " int j;\n" 8938 "};\n", 8939 "struct foo {\n" 8940 "private:\n" 8941 " void f() {}\n" 8942 "private:\n" 8943 " int i;\n" 8944 "protected:\n" 8945 " int j;\n" 8946 "};\n", 8947 Style); 8948 verifyFormat("struct foo { /* comment */\n" 8949 "private:\n" 8950 " int i;\n" 8951 " // comment\n" 8952 "private:\n" 8953 " int j;\n" 8954 "};\n", 8955 Style); 8956 verifyFormat("struct foo {\n" 8957 "#ifdef FOO\n" 8958 "#endif\n" 8959 "private:\n" 8960 " int i;\n" 8961 "#ifdef FOO\n" 8962 "private:\n" 8963 "#endif\n" 8964 " int j;\n" 8965 "};\n", 8966 Style); 8967 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 8968 verifyFormat("struct foo {\n" 8969 "private:\n" 8970 " void f() {}\n" 8971 "private:\n" 8972 " int i;\n" 8973 "protected:\n" 8974 " int j;\n" 8975 "};\n", 8976 Style); 8977 verifyFormat("struct foo {\n" 8978 "private:\n" 8979 " void f() {}\n" 8980 "private:\n" 8981 " int i;\n" 8982 "protected:\n" 8983 " int j;\n" 8984 "};\n", 8985 "struct foo {\n" 8986 "\n" 8987 "private:\n" 8988 " void f() {}\n" 8989 "\n" 8990 "private:\n" 8991 " int i;\n" 8992 "\n" 8993 "protected:\n" 8994 " int j;\n" 8995 "};\n", 8996 Style); 8997 verifyFormat("struct foo { /* comment */\n" 8998 "private:\n" 8999 " int i;\n" 9000 " // comment\n" 9001 "private:\n" 9002 " int j;\n" 9003 "};\n", 9004 "struct foo { /* comment */\n" 9005 "\n" 9006 "private:\n" 9007 " int i;\n" 9008 " // comment\n" 9009 "\n" 9010 "private:\n" 9011 " int j;\n" 9012 "};\n", 9013 Style); 9014 verifyFormat("struct foo {\n" 9015 "#ifdef FOO\n" 9016 "#endif\n" 9017 "private:\n" 9018 " int i;\n" 9019 "#ifdef FOO\n" 9020 "private:\n" 9021 "#endif\n" 9022 " int j;\n" 9023 "};\n", 9024 "struct foo {\n" 9025 "#ifdef FOO\n" 9026 "#endif\n" 9027 "\n" 9028 "private:\n" 9029 " int i;\n" 9030 "#ifdef FOO\n" 9031 "\n" 9032 "private:\n" 9033 "#endif\n" 9034 " int j;\n" 9035 "};\n", 9036 Style); 9037 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 9038 verifyFormat("struct foo {\n" 9039 "private:\n" 9040 " void f() {}\n" 9041 "\n" 9042 "private:\n" 9043 " int i;\n" 9044 "\n" 9045 "protected:\n" 9046 " int j;\n" 9047 "};\n", 9048 Style); 9049 verifyFormat("struct foo {\n" 9050 "private:\n" 9051 " void f() {}\n" 9052 "\n" 9053 "private:\n" 9054 " int i;\n" 9055 "\n" 9056 "protected:\n" 9057 " int j;\n" 9058 "};\n", 9059 "struct foo {\n" 9060 "private:\n" 9061 " void f() {}\n" 9062 "private:\n" 9063 " int i;\n" 9064 "protected:\n" 9065 " int j;\n" 9066 "};\n", 9067 Style); 9068 verifyFormat("struct foo { /* comment */\n" 9069 "private:\n" 9070 " int i;\n" 9071 " // comment\n" 9072 "\n" 9073 "private:\n" 9074 " int j;\n" 9075 "};\n", 9076 "struct foo { /* comment */\n" 9077 "private:\n" 9078 " int i;\n" 9079 " // comment\n" 9080 "\n" 9081 "private:\n" 9082 " int j;\n" 9083 "};\n", 9084 Style); 9085 verifyFormat("struct foo {\n" 9086 "#ifdef FOO\n" 9087 "#endif\n" 9088 "\n" 9089 "private:\n" 9090 " int i;\n" 9091 "#ifdef FOO\n" 9092 "\n" 9093 "private:\n" 9094 "#endif\n" 9095 " int j;\n" 9096 "};\n", 9097 "struct foo {\n" 9098 "#ifdef FOO\n" 9099 "#endif\n" 9100 "private:\n" 9101 " int i;\n" 9102 "#ifdef FOO\n" 9103 "private:\n" 9104 "#endif\n" 9105 " int j;\n" 9106 "};\n", 9107 Style); 9108 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 9109 EXPECT_EQ("struct foo {\n" 9110 "\n" 9111 "private:\n" 9112 " void f() {}\n" 9113 "\n" 9114 "private:\n" 9115 " int i;\n" 9116 "\n" 9117 "protected:\n" 9118 " int j;\n" 9119 "};\n", 9120 format("struct foo {\n" 9121 "\n" 9122 "private:\n" 9123 " void f() {}\n" 9124 "\n" 9125 "private:\n" 9126 " int i;\n" 9127 "\n" 9128 "protected:\n" 9129 " int j;\n" 9130 "};\n", 9131 Style)); 9132 verifyFormat("struct foo {\n" 9133 "private:\n" 9134 " void f() {}\n" 9135 "private:\n" 9136 " int i;\n" 9137 "protected:\n" 9138 " int j;\n" 9139 "};\n", 9140 Style); 9141 EXPECT_EQ("struct foo { /* comment */\n" 9142 "\n" 9143 "private:\n" 9144 " int i;\n" 9145 " // comment\n" 9146 "\n" 9147 "private:\n" 9148 " int j;\n" 9149 "};\n", 9150 format("struct foo { /* comment */\n" 9151 "\n" 9152 "private:\n" 9153 " int i;\n" 9154 " // comment\n" 9155 "\n" 9156 "private:\n" 9157 " int j;\n" 9158 "};\n", 9159 Style)); 9160 verifyFormat("struct foo { /* comment */\n" 9161 "private:\n" 9162 " int i;\n" 9163 " // comment\n" 9164 "private:\n" 9165 " int j;\n" 9166 "};\n", 9167 Style); 9168 EXPECT_EQ("struct foo {\n" 9169 "#ifdef FOO\n" 9170 "#endif\n" 9171 "\n" 9172 "private:\n" 9173 " int i;\n" 9174 "#ifdef FOO\n" 9175 "\n" 9176 "private:\n" 9177 "#endif\n" 9178 " int j;\n" 9179 "};\n", 9180 format("struct foo {\n" 9181 "#ifdef FOO\n" 9182 "#endif\n" 9183 "\n" 9184 "private:\n" 9185 " int i;\n" 9186 "#ifdef FOO\n" 9187 "\n" 9188 "private:\n" 9189 "#endif\n" 9190 " int j;\n" 9191 "};\n", 9192 Style)); 9193 verifyFormat("struct foo {\n" 9194 "#ifdef FOO\n" 9195 "#endif\n" 9196 "private:\n" 9197 " int i;\n" 9198 "#ifdef FOO\n" 9199 "private:\n" 9200 "#endif\n" 9201 " int j;\n" 9202 "};\n", 9203 Style); 9204 9205 FormatStyle NoEmptyLines = getLLVMStyle(); 9206 NoEmptyLines.MaxEmptyLinesToKeep = 0; 9207 verifyFormat("struct foo {\n" 9208 "private:\n" 9209 " void f() {}\n" 9210 "\n" 9211 "private:\n" 9212 " int i;\n" 9213 "\n" 9214 "public:\n" 9215 "protected:\n" 9216 " int j;\n" 9217 "};\n", 9218 NoEmptyLines); 9219 9220 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 9221 verifyFormat("struct foo {\n" 9222 "private:\n" 9223 " void f() {}\n" 9224 "private:\n" 9225 " int i;\n" 9226 "public:\n" 9227 "protected:\n" 9228 " int j;\n" 9229 "};\n", 9230 NoEmptyLines); 9231 9232 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 9233 verifyFormat("struct foo {\n" 9234 "private:\n" 9235 " void f() {}\n" 9236 "\n" 9237 "private:\n" 9238 " int i;\n" 9239 "\n" 9240 "public:\n" 9241 "\n" 9242 "protected:\n" 9243 " int j;\n" 9244 "};\n", 9245 NoEmptyLines); 9246 } 9247 9248 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 9249 9250 FormatStyle Style = getLLVMStyle(); 9251 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 9252 verifyFormat("struct foo {\n" 9253 "private:\n" 9254 " void f() {}\n" 9255 "\n" 9256 "private:\n" 9257 " int i;\n" 9258 "\n" 9259 "protected:\n" 9260 " int j;\n" 9261 "};\n", 9262 Style); 9263 9264 // Check if lines are removed. 9265 verifyFormat("struct foo {\n" 9266 "private:\n" 9267 " void f() {}\n" 9268 "\n" 9269 "private:\n" 9270 " int i;\n" 9271 "\n" 9272 "protected:\n" 9273 " int j;\n" 9274 "};\n", 9275 "struct foo {\n" 9276 "private:\n" 9277 "\n" 9278 " void f() {}\n" 9279 "\n" 9280 "private:\n" 9281 "\n" 9282 " int i;\n" 9283 "\n" 9284 "protected:\n" 9285 "\n" 9286 " int j;\n" 9287 "};\n", 9288 Style); 9289 9290 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 9291 verifyFormat("struct foo {\n" 9292 "private:\n" 9293 "\n" 9294 " void f() {}\n" 9295 "\n" 9296 "private:\n" 9297 "\n" 9298 " int i;\n" 9299 "\n" 9300 "protected:\n" 9301 "\n" 9302 " int j;\n" 9303 "};\n", 9304 Style); 9305 9306 // Check if lines are added. 9307 verifyFormat("struct foo {\n" 9308 "private:\n" 9309 "\n" 9310 " void f() {}\n" 9311 "\n" 9312 "private:\n" 9313 "\n" 9314 " int i;\n" 9315 "\n" 9316 "protected:\n" 9317 "\n" 9318 " int j;\n" 9319 "};\n", 9320 "struct foo {\n" 9321 "private:\n" 9322 " void f() {}\n" 9323 "\n" 9324 "private:\n" 9325 " int i;\n" 9326 "\n" 9327 "protected:\n" 9328 " int j;\n" 9329 "};\n", 9330 Style); 9331 9332 // Leave tests rely on the code layout, test::messUp can not be used. 9333 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 9334 Style.MaxEmptyLinesToKeep = 0u; 9335 verifyFormat("struct foo {\n" 9336 "private:\n" 9337 " void f() {}\n" 9338 "\n" 9339 "private:\n" 9340 " int i;\n" 9341 "\n" 9342 "protected:\n" 9343 " int j;\n" 9344 "};\n", 9345 Style); 9346 9347 // Check if MaxEmptyLinesToKeep is respected. 9348 EXPECT_EQ("struct foo {\n" 9349 "private:\n" 9350 " void f() {}\n" 9351 "\n" 9352 "private:\n" 9353 " int i;\n" 9354 "\n" 9355 "protected:\n" 9356 " int j;\n" 9357 "};\n", 9358 format("struct foo {\n" 9359 "private:\n" 9360 "\n\n\n" 9361 " void f() {}\n" 9362 "\n" 9363 "private:\n" 9364 "\n\n\n" 9365 " int i;\n" 9366 "\n" 9367 "protected:\n" 9368 "\n\n\n" 9369 " int j;\n" 9370 "};\n", 9371 Style)); 9372 9373 Style.MaxEmptyLinesToKeep = 1u; 9374 EXPECT_EQ("struct foo {\n" 9375 "private:\n" 9376 "\n" 9377 " void f() {}\n" 9378 "\n" 9379 "private:\n" 9380 "\n" 9381 " int i;\n" 9382 "\n" 9383 "protected:\n" 9384 "\n" 9385 " int j;\n" 9386 "};\n", 9387 format("struct foo {\n" 9388 "private:\n" 9389 "\n" 9390 " void f() {}\n" 9391 "\n" 9392 "private:\n" 9393 "\n" 9394 " int i;\n" 9395 "\n" 9396 "protected:\n" 9397 "\n" 9398 " int j;\n" 9399 "};\n", 9400 Style)); 9401 // Check if no lines are kept. 9402 EXPECT_EQ("struct foo {\n" 9403 "private:\n" 9404 " void f() {}\n" 9405 "\n" 9406 "private:\n" 9407 " int i;\n" 9408 "\n" 9409 "protected:\n" 9410 " int j;\n" 9411 "};\n", 9412 format("struct foo {\n" 9413 "private:\n" 9414 " void f() {}\n" 9415 "\n" 9416 "private:\n" 9417 " int i;\n" 9418 "\n" 9419 "protected:\n" 9420 " int j;\n" 9421 "};\n", 9422 Style)); 9423 // Check if MaxEmptyLinesToKeep is respected. 9424 EXPECT_EQ("struct foo {\n" 9425 "private:\n" 9426 "\n" 9427 " void f() {}\n" 9428 "\n" 9429 "private:\n" 9430 "\n" 9431 " int i;\n" 9432 "\n" 9433 "protected:\n" 9434 "\n" 9435 " int j;\n" 9436 "};\n", 9437 format("struct foo {\n" 9438 "private:\n" 9439 "\n\n\n" 9440 " void f() {}\n" 9441 "\n" 9442 "private:\n" 9443 "\n\n\n" 9444 " int i;\n" 9445 "\n" 9446 "protected:\n" 9447 "\n\n\n" 9448 " int j;\n" 9449 "};\n", 9450 Style)); 9451 9452 Style.MaxEmptyLinesToKeep = 10u; 9453 EXPECT_EQ("struct foo {\n" 9454 "private:\n" 9455 "\n\n\n" 9456 " void f() {}\n" 9457 "\n" 9458 "private:\n" 9459 "\n\n\n" 9460 " int i;\n" 9461 "\n" 9462 "protected:\n" 9463 "\n\n\n" 9464 " int j;\n" 9465 "};\n", 9466 format("struct foo {\n" 9467 "private:\n" 9468 "\n\n\n" 9469 " void f() {}\n" 9470 "\n" 9471 "private:\n" 9472 "\n\n\n" 9473 " int i;\n" 9474 "\n" 9475 "protected:\n" 9476 "\n\n\n" 9477 " int j;\n" 9478 "};\n", 9479 Style)); 9480 9481 // Test with comments. 9482 Style = getLLVMStyle(); 9483 verifyFormat("struct foo {\n" 9484 "private:\n" 9485 " // comment\n" 9486 " void f() {}\n" 9487 "\n" 9488 "private: /* comment */\n" 9489 " int i;\n" 9490 "};\n", 9491 Style); 9492 verifyFormat("struct foo {\n" 9493 "private:\n" 9494 " // comment\n" 9495 " void f() {}\n" 9496 "\n" 9497 "private: /* comment */\n" 9498 " int i;\n" 9499 "};\n", 9500 "struct foo {\n" 9501 "private:\n" 9502 "\n" 9503 " // comment\n" 9504 " void f() {}\n" 9505 "\n" 9506 "private: /* comment */\n" 9507 "\n" 9508 " int i;\n" 9509 "};\n", 9510 Style); 9511 9512 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 9513 verifyFormat("struct foo {\n" 9514 "private:\n" 9515 "\n" 9516 " // comment\n" 9517 " void f() {}\n" 9518 "\n" 9519 "private: /* comment */\n" 9520 "\n" 9521 " int i;\n" 9522 "};\n", 9523 "struct foo {\n" 9524 "private:\n" 9525 " // comment\n" 9526 " void f() {}\n" 9527 "\n" 9528 "private: /* comment */\n" 9529 " int i;\n" 9530 "};\n", 9531 Style); 9532 verifyFormat("struct foo {\n" 9533 "private:\n" 9534 "\n" 9535 " // comment\n" 9536 " void f() {}\n" 9537 "\n" 9538 "private: /* comment */\n" 9539 "\n" 9540 " int i;\n" 9541 "};\n", 9542 Style); 9543 9544 // Test with preprocessor defines. 9545 Style = getLLVMStyle(); 9546 verifyFormat("struct foo {\n" 9547 "private:\n" 9548 "#ifdef FOO\n" 9549 "#endif\n" 9550 " void f() {}\n" 9551 "};\n", 9552 Style); 9553 verifyFormat("struct foo {\n" 9554 "private:\n" 9555 "#ifdef FOO\n" 9556 "#endif\n" 9557 " void f() {}\n" 9558 "};\n", 9559 "struct foo {\n" 9560 "private:\n" 9561 "\n" 9562 "#ifdef FOO\n" 9563 "#endif\n" 9564 " void f() {}\n" 9565 "};\n", 9566 Style); 9567 9568 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 9569 verifyFormat("struct foo {\n" 9570 "private:\n" 9571 "\n" 9572 "#ifdef FOO\n" 9573 "#endif\n" 9574 " void f() {}\n" 9575 "};\n", 9576 "struct foo {\n" 9577 "private:\n" 9578 "#ifdef FOO\n" 9579 "#endif\n" 9580 " void f() {}\n" 9581 "};\n", 9582 Style); 9583 verifyFormat("struct foo {\n" 9584 "private:\n" 9585 "\n" 9586 "#ifdef FOO\n" 9587 "#endif\n" 9588 " void f() {}\n" 9589 "};\n", 9590 Style); 9591 } 9592 9593 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 9594 // Combined tests of EmptyLineAfterAccessModifier and 9595 // EmptyLineBeforeAccessModifier. 9596 FormatStyle Style = getLLVMStyle(); 9597 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 9598 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 9599 verifyFormat("struct foo {\n" 9600 "private:\n" 9601 "\n" 9602 "protected:\n" 9603 "};\n", 9604 Style); 9605 9606 Style.MaxEmptyLinesToKeep = 10u; 9607 // Both remove all new lines. 9608 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 9609 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 9610 verifyFormat("struct foo {\n" 9611 "private:\n" 9612 "protected:\n" 9613 "};\n", 9614 "struct foo {\n" 9615 "private:\n" 9616 "\n\n\n" 9617 "protected:\n" 9618 "};\n", 9619 Style); 9620 9621 // Leave tests rely on the code layout, test::messUp can not be used. 9622 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 9623 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 9624 Style.MaxEmptyLinesToKeep = 10u; 9625 EXPECT_EQ("struct foo {\n" 9626 "private:\n" 9627 "\n\n\n" 9628 "protected:\n" 9629 "};\n", 9630 format("struct foo {\n" 9631 "private:\n" 9632 "\n\n\n" 9633 "protected:\n" 9634 "};\n", 9635 Style)); 9636 Style.MaxEmptyLinesToKeep = 3u; 9637 EXPECT_EQ("struct foo {\n" 9638 "private:\n" 9639 "\n\n\n" 9640 "protected:\n" 9641 "};\n", 9642 format("struct foo {\n" 9643 "private:\n" 9644 "\n\n\n" 9645 "protected:\n" 9646 "};\n", 9647 Style)); 9648 Style.MaxEmptyLinesToKeep = 1u; 9649 EXPECT_EQ("struct foo {\n" 9650 "private:\n" 9651 "\n\n\n" 9652 "protected:\n" 9653 "};\n", 9654 format("struct foo {\n" 9655 "private:\n" 9656 "\n\n\n" 9657 "protected:\n" 9658 "};\n", 9659 Style)); // Based on new lines in original document and not 9660 // on the setting. 9661 9662 Style.MaxEmptyLinesToKeep = 10u; 9663 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 9664 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 9665 // Newlines are kept if they are greater than zero, 9666 // test::messUp removes all new lines which changes the logic 9667 EXPECT_EQ("struct foo {\n" 9668 "private:\n" 9669 "\n\n\n" 9670 "protected:\n" 9671 "};\n", 9672 format("struct foo {\n" 9673 "private:\n" 9674 "\n\n\n" 9675 "protected:\n" 9676 "};\n", 9677 Style)); 9678 9679 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 9680 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 9681 // test::messUp removes all new lines which changes the logic 9682 EXPECT_EQ("struct foo {\n" 9683 "private:\n" 9684 "\n\n\n" 9685 "protected:\n" 9686 "};\n", 9687 format("struct foo {\n" 9688 "private:\n" 9689 "\n\n\n" 9690 "protected:\n" 9691 "};\n", 9692 Style)); 9693 9694 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 9695 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 9696 EXPECT_EQ("struct foo {\n" 9697 "private:\n" 9698 "\n\n\n" 9699 "protected:\n" 9700 "};\n", 9701 format("struct foo {\n" 9702 "private:\n" 9703 "\n\n\n" 9704 "protected:\n" 9705 "};\n", 9706 Style)); // test::messUp removes all new lines which changes 9707 // the logic. 9708 9709 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 9710 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 9711 verifyFormat("struct foo {\n" 9712 "private:\n" 9713 "protected:\n" 9714 "};\n", 9715 "struct foo {\n" 9716 "private:\n" 9717 "\n\n\n" 9718 "protected:\n" 9719 "};\n", 9720 Style); 9721 9722 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 9723 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 9724 EXPECT_EQ("struct foo {\n" 9725 "private:\n" 9726 "\n\n\n" 9727 "protected:\n" 9728 "};\n", 9729 format("struct foo {\n" 9730 "private:\n" 9731 "\n\n\n" 9732 "protected:\n" 9733 "};\n", 9734 Style)); // test::messUp removes all new lines which changes 9735 // the logic. 9736 9737 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 9738 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 9739 verifyFormat("struct foo {\n" 9740 "private:\n" 9741 "protected:\n" 9742 "};\n", 9743 "struct foo {\n" 9744 "private:\n" 9745 "\n\n\n" 9746 "protected:\n" 9747 "};\n", 9748 Style); 9749 9750 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 9751 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 9752 verifyFormat("struct foo {\n" 9753 "private:\n" 9754 "protected:\n" 9755 "};\n", 9756 "struct foo {\n" 9757 "private:\n" 9758 "\n\n\n" 9759 "protected:\n" 9760 "};\n", 9761 Style); 9762 9763 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 9764 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 9765 verifyFormat("struct foo {\n" 9766 "private:\n" 9767 "protected:\n" 9768 "};\n", 9769 "struct foo {\n" 9770 "private:\n" 9771 "\n\n\n" 9772 "protected:\n" 9773 "};\n", 9774 Style); 9775 9776 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 9777 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 9778 verifyFormat("struct foo {\n" 9779 "private:\n" 9780 "protected:\n" 9781 "};\n", 9782 "struct foo {\n" 9783 "private:\n" 9784 "\n\n\n" 9785 "protected:\n" 9786 "};\n", 9787 Style); 9788 } 9789 9790 TEST_F(FormatTest, FormatsArrays) { 9791 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 9792 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 9793 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 9794 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 9795 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 9796 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 9797 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9798 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 9799 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9800 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 9801 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9802 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 9803 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 9804 verifyFormat( 9805 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 9806 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 9807 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 9808 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 9809 " .aaaaaaaaaaaaaaaaaaaaaa();"); 9810 9811 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 9812 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 9813 verifyFormat( 9814 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 9815 " .aaaaaaa[0]\n" 9816 " .aaaaaaaaaaaaaaaaaaaaaa();"); 9817 verifyFormat("a[::b::c];"); 9818 9819 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 9820 9821 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 9822 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 9823 } 9824 9825 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 9826 verifyFormat("(a)->b();"); 9827 verifyFormat("--a;"); 9828 } 9829 9830 TEST_F(FormatTest, HandlesIncludeDirectives) { 9831 verifyFormat("#include <string>\n" 9832 "#include <a/b/c.h>\n" 9833 "#include \"a/b/string\"\n" 9834 "#include \"string.h\"\n" 9835 "#include \"string.h\"\n" 9836 "#include <a-a>\n" 9837 "#include < path with space >\n" 9838 "#include_next <test.h>" 9839 "#include \"abc.h\" // this is included for ABC\n" 9840 "#include \"some long include\" // with a comment\n" 9841 "#include \"some very long include path\"\n" 9842 "#include <some/very/long/include/path>\n", 9843 getLLVMStyleWithColumns(35)); 9844 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 9845 EXPECT_EQ("#include <a>", format("#include<a>")); 9846 9847 verifyFormat("#import <string>"); 9848 verifyFormat("#import <a/b/c.h>"); 9849 verifyFormat("#import \"a/b/string\""); 9850 verifyFormat("#import \"string.h\""); 9851 verifyFormat("#import \"string.h\""); 9852 verifyFormat("#if __has_include(<strstream>)\n" 9853 "#include <strstream>\n" 9854 "#endif"); 9855 9856 verifyFormat("#define MY_IMPORT <a/b>"); 9857 9858 verifyFormat("#if __has_include(<a/b>)"); 9859 verifyFormat("#if __has_include_next(<a/b>)"); 9860 verifyFormat("#define F __has_include(<a/b>)"); 9861 verifyFormat("#define F __has_include_next(<a/b>)"); 9862 9863 // Protocol buffer definition or missing "#". 9864 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 9865 getLLVMStyleWithColumns(30)); 9866 9867 FormatStyle Style = getLLVMStyle(); 9868 Style.AlwaysBreakBeforeMultilineStrings = true; 9869 Style.ColumnLimit = 0; 9870 verifyFormat("#import \"abc.h\"", Style); 9871 9872 // But 'import' might also be a regular C++ namespace. 9873 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9874 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9875 } 9876 9877 //===----------------------------------------------------------------------===// 9878 // Error recovery tests. 9879 //===----------------------------------------------------------------------===// 9880 9881 TEST_F(FormatTest, IncompleteParameterLists) { 9882 FormatStyle NoBinPacking = getLLVMStyle(); 9883 NoBinPacking.BinPackParameters = false; 9884 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 9885 " double *min_x,\n" 9886 " double *max_x,\n" 9887 " double *min_y,\n" 9888 " double *max_y,\n" 9889 " double *min_z,\n" 9890 " double *max_z, ) {}", 9891 NoBinPacking); 9892 } 9893 9894 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 9895 verifyFormat("void f() { return; }\n42"); 9896 verifyFormat("void f() {\n" 9897 " if (0)\n" 9898 " return;\n" 9899 "}\n" 9900 "42"); 9901 verifyFormat("void f() { return }\n42"); 9902 verifyFormat("void f() {\n" 9903 " if (0)\n" 9904 " return\n" 9905 "}\n" 9906 "42"); 9907 } 9908 9909 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 9910 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 9911 EXPECT_EQ("void f() {\n" 9912 " if (a)\n" 9913 " return\n" 9914 "}", 9915 format("void f ( ) { if ( a ) return }")); 9916 EXPECT_EQ("namespace N {\n" 9917 "void f()\n" 9918 "}", 9919 format("namespace N { void f() }")); 9920 EXPECT_EQ("namespace N {\n" 9921 "void f() {}\n" 9922 "void g()\n" 9923 "} // namespace N", 9924 format("namespace N { void f( ) { } void g( ) }")); 9925 } 9926 9927 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 9928 verifyFormat("int aaaaaaaa =\n" 9929 " // Overlylongcomment\n" 9930 " b;", 9931 getLLVMStyleWithColumns(20)); 9932 verifyFormat("function(\n" 9933 " ShortArgument,\n" 9934 " LoooooooooooongArgument);\n", 9935 getLLVMStyleWithColumns(20)); 9936 } 9937 9938 TEST_F(FormatTest, IncorrectAccessSpecifier) { 9939 verifyFormat("public:"); 9940 verifyFormat("class A {\n" 9941 "public\n" 9942 " void f() {}\n" 9943 "};"); 9944 verifyFormat("public\n" 9945 "int qwerty;"); 9946 verifyFormat("public\n" 9947 "B {}"); 9948 verifyFormat("public\n" 9949 "{}"); 9950 verifyFormat("public\n" 9951 "B { int x; }"); 9952 } 9953 9954 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 9955 verifyFormat("{"); 9956 verifyFormat("#})"); 9957 verifyNoCrash("(/**/[:!] ?[)."); 9958 } 9959 9960 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 9961 // Found by oss-fuzz: 9962 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 9963 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 9964 Style.ColumnLimit = 60; 9965 verifyNoCrash( 9966 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 9967 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 9968 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 9969 Style); 9970 } 9971 9972 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 9973 verifyFormat("do {\n}"); 9974 verifyFormat("do {\n}\n" 9975 "f();"); 9976 verifyFormat("do {\n}\n" 9977 "wheeee(fun);"); 9978 verifyFormat("do {\n" 9979 " f();\n" 9980 "}"); 9981 } 9982 9983 TEST_F(FormatTest, IncorrectCodeMissingParens) { 9984 verifyFormat("if {\n foo;\n foo();\n}"); 9985 verifyFormat("switch {\n foo;\n foo();\n}"); 9986 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 9987 verifyFormat("while {\n foo;\n foo();\n}"); 9988 verifyFormat("do {\n foo;\n foo();\n} while;"); 9989 } 9990 9991 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 9992 verifyIncompleteFormat("namespace {\n" 9993 "class Foo { Foo (\n" 9994 "};\n" 9995 "} // namespace"); 9996 } 9997 9998 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 9999 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 10000 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 10001 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 10002 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 10003 10004 EXPECT_EQ("{\n" 10005 " {\n" 10006 " breakme(\n" 10007 " qwe);\n" 10008 " }\n", 10009 format("{\n" 10010 " {\n" 10011 " breakme(qwe);\n" 10012 "}\n", 10013 getLLVMStyleWithColumns(10))); 10014 } 10015 10016 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 10017 verifyFormat("int x = {\n" 10018 " avariable,\n" 10019 " b(alongervariable)};", 10020 getLLVMStyleWithColumns(25)); 10021 } 10022 10023 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 10024 verifyFormat("return (a)(b){1, 2, 3};"); 10025 } 10026 10027 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 10028 verifyFormat("vector<int> x{1, 2, 3, 4};"); 10029 verifyFormat("vector<int> x{\n" 10030 " 1,\n" 10031 " 2,\n" 10032 " 3,\n" 10033 " 4,\n" 10034 "};"); 10035 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 10036 verifyFormat("f({1, 2});"); 10037 verifyFormat("auto v = Foo{-1};"); 10038 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 10039 verifyFormat("Class::Class : member{1, 2, 3} {}"); 10040 verifyFormat("new vector<int>{1, 2, 3};"); 10041 verifyFormat("new int[3]{1, 2, 3};"); 10042 verifyFormat("new int{1};"); 10043 verifyFormat("return {arg1, arg2};"); 10044 verifyFormat("return {arg1, SomeType{parameter}};"); 10045 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 10046 verifyFormat("new T{arg1, arg2};"); 10047 verifyFormat("f(MyMap[{composite, key}]);"); 10048 verifyFormat("class Class {\n" 10049 " T member = {arg1, arg2};\n" 10050 "};"); 10051 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 10052 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 10053 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 10054 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 10055 verifyFormat("int a = std::is_integral<int>{} + 0;"); 10056 10057 verifyFormat("int foo(int i) { return fo1{}(i); }"); 10058 verifyFormat("int foo(int i) { return fo1{}(i); }"); 10059 verifyFormat("auto i = decltype(x){};"); 10060 verifyFormat("auto i = typeof(x){};"); 10061 verifyFormat("auto i = _Atomic(x){};"); 10062 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 10063 verifyFormat("Node n{1, Node{1000}, //\n" 10064 " 2};"); 10065 verifyFormat("Aaaa aaaaaaa{\n" 10066 " {\n" 10067 " aaaa,\n" 10068 " },\n" 10069 "};"); 10070 verifyFormat("class C : public D {\n" 10071 " SomeClass SC{2};\n" 10072 "};"); 10073 verifyFormat("class C : public A {\n" 10074 " class D : public B {\n" 10075 " void f() { int i{2}; }\n" 10076 " };\n" 10077 "};"); 10078 verifyFormat("#define A {a, a},"); 10079 10080 // Avoid breaking between equal sign and opening brace 10081 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 10082 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 10083 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 10084 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 10085 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 10086 " {\"ccccccccccccccccccccc\", 2}};", 10087 AvoidBreakingFirstArgument); 10088 10089 // Binpacking only if there is no trailing comma 10090 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 10091 " cccccccccc, dddddddddd};", 10092 getLLVMStyleWithColumns(50)); 10093 verifyFormat("const Aaaaaa aaaaa = {\n" 10094 " aaaaaaaaaaa,\n" 10095 " bbbbbbbbbbb,\n" 10096 " ccccccccccc,\n" 10097 " ddddddddddd,\n" 10098 "};", 10099 getLLVMStyleWithColumns(50)); 10100 10101 // Cases where distinguising braced lists and blocks is hard. 10102 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 10103 verifyFormat("void f() {\n" 10104 " return; // comment\n" 10105 "}\n" 10106 "SomeType t;"); 10107 verifyFormat("void f() {\n" 10108 " if (a) {\n" 10109 " f();\n" 10110 " }\n" 10111 "}\n" 10112 "SomeType t;"); 10113 10114 // In combination with BinPackArguments = false. 10115 FormatStyle NoBinPacking = getLLVMStyle(); 10116 NoBinPacking.BinPackArguments = false; 10117 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 10118 " bbbbb,\n" 10119 " ccccc,\n" 10120 " ddddd,\n" 10121 " eeeee,\n" 10122 " ffffff,\n" 10123 " ggggg,\n" 10124 " hhhhhh,\n" 10125 " iiiiii,\n" 10126 " jjjjjj,\n" 10127 " kkkkkk};", 10128 NoBinPacking); 10129 verifyFormat("const Aaaaaa aaaaa = {\n" 10130 " aaaaa,\n" 10131 " bbbbb,\n" 10132 " ccccc,\n" 10133 " ddddd,\n" 10134 " eeeee,\n" 10135 " ffffff,\n" 10136 " ggggg,\n" 10137 " hhhhhh,\n" 10138 " iiiiii,\n" 10139 " jjjjjj,\n" 10140 " kkkkkk,\n" 10141 "};", 10142 NoBinPacking); 10143 verifyFormat( 10144 "const Aaaaaa aaaaa = {\n" 10145 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 10146 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 10147 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 10148 "};", 10149 NoBinPacking); 10150 10151 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10152 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 10153 " CDDDP83848_BMCR_REGISTER,\n" 10154 " CDDDP83848_BMSR_REGISTER,\n" 10155 " CDDDP83848_RBR_REGISTER};", 10156 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 10157 " CDDDP83848_BMSR_REGISTER,\n" 10158 " CDDDP83848_RBR_REGISTER};", 10159 NoBinPacking)); 10160 10161 // FIXME: The alignment of these trailing comments might be bad. Then again, 10162 // this might be utterly useless in real code. 10163 verifyFormat("Constructor::Constructor()\n" 10164 " : some_value{ //\n" 10165 " aaaaaaa, //\n" 10166 " bbbbbbb} {}"); 10167 10168 // In braced lists, the first comment is always assumed to belong to the 10169 // first element. Thus, it can be moved to the next or previous line as 10170 // appropriate. 10171 EXPECT_EQ("function({// First element:\n" 10172 " 1,\n" 10173 " // Second element:\n" 10174 " 2});", 10175 format("function({\n" 10176 " // First element:\n" 10177 " 1,\n" 10178 " // Second element:\n" 10179 " 2});")); 10180 EXPECT_EQ("std::vector<int> MyNumbers{\n" 10181 " // First element:\n" 10182 " 1,\n" 10183 " // Second element:\n" 10184 " 2};", 10185 format("std::vector<int> MyNumbers{// First element:\n" 10186 " 1,\n" 10187 " // Second element:\n" 10188 " 2};", 10189 getLLVMStyleWithColumns(30))); 10190 // A trailing comma should still lead to an enforced line break and no 10191 // binpacking. 10192 EXPECT_EQ("vector<int> SomeVector = {\n" 10193 " // aaa\n" 10194 " 1,\n" 10195 " 2,\n" 10196 "};", 10197 format("vector<int> SomeVector = { // aaa\n" 10198 " 1, 2, };")); 10199 10200 // C++11 brace initializer list l-braces should not be treated any differently 10201 // when breaking before lambda bodies is enabled 10202 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 10203 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 10204 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 10205 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 10206 verifyFormat( 10207 "std::runtime_error{\n" 10208 " \"Long string which will force a break onto the next line...\"};", 10209 BreakBeforeLambdaBody); 10210 10211 FormatStyle ExtraSpaces = getLLVMStyle(); 10212 ExtraSpaces.Cpp11BracedListStyle = false; 10213 ExtraSpaces.ColumnLimit = 75; 10214 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 10215 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 10216 verifyFormat("f({ 1, 2 });", ExtraSpaces); 10217 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 10218 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 10219 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 10220 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 10221 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 10222 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 10223 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 10224 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 10225 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 10226 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 10227 verifyFormat("class Class {\n" 10228 " T member = { arg1, arg2 };\n" 10229 "};", 10230 ExtraSpaces); 10231 verifyFormat( 10232 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10233 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 10234 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 10235 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 10236 ExtraSpaces); 10237 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 10238 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 10239 ExtraSpaces); 10240 verifyFormat( 10241 "someFunction(OtherParam,\n" 10242 " BracedList{ // comment 1 (Forcing interesting break)\n" 10243 " param1, param2,\n" 10244 " // comment 2\n" 10245 " param3, param4 });", 10246 ExtraSpaces); 10247 verifyFormat( 10248 "std::this_thread::sleep_for(\n" 10249 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 10250 ExtraSpaces); 10251 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 10252 " aaaaaaa,\n" 10253 " aaaaaaaaaa,\n" 10254 " aaaaa,\n" 10255 " aaaaaaaaaaaaaaa,\n" 10256 " aaa,\n" 10257 " aaaaaaaaaa,\n" 10258 " a,\n" 10259 " aaaaaaaaaaaaaaaaaaaaa,\n" 10260 " aaaaaaaaaaaa,\n" 10261 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 10262 " aaaaaaa,\n" 10263 " a};"); 10264 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 10265 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 10266 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 10267 10268 // Avoid breaking between initializer/equal sign and opening brace 10269 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 10270 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 10271 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 10272 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 10273 " { \"ccccccccccccccccccccc\", 2 }\n" 10274 "};", 10275 ExtraSpaces); 10276 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 10277 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 10278 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 10279 " { \"ccccccccccccccccccccc\", 2 }\n" 10280 "};", 10281 ExtraSpaces); 10282 10283 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 10284 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 10285 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 10286 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 10287 10288 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 10289 SpaceBetweenBraces.SpacesInAngles = true; 10290 SpaceBetweenBraces.SpacesInParentheses = true; 10291 SpaceBetweenBraces.SpacesInSquareBrackets = true; 10292 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 10293 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 10294 verifyFormat("vector< int > x{ // comment 1\n" 10295 " 1, 2, 3, 4 };", 10296 SpaceBetweenBraces); 10297 SpaceBetweenBraces.ColumnLimit = 20; 10298 EXPECT_EQ("vector< int > x{\n" 10299 " 1, 2, 3, 4 };", 10300 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 10301 SpaceBetweenBraces.ColumnLimit = 24; 10302 EXPECT_EQ("vector< int > x{ 1, 2,\n" 10303 " 3, 4 };", 10304 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 10305 EXPECT_EQ("vector< int > x{\n" 10306 " 1,\n" 10307 " 2,\n" 10308 " 3,\n" 10309 " 4,\n" 10310 "};", 10311 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 10312 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 10313 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 10314 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 10315 } 10316 10317 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 10318 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10319 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10320 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10321 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10322 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10323 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 10324 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 10325 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10326 " 1, 22, 333, 4444, 55555, //\n" 10327 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10328 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 10329 verifyFormat( 10330 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10331 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10332 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 10333 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 10334 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 10335 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 10336 " 7777777};"); 10337 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 10338 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 10339 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 10340 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 10341 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 10342 " // Separating comment.\n" 10343 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 10344 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 10345 " // Leading comment\n" 10346 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 10347 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 10348 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 10349 " 1, 1, 1, 1};", 10350 getLLVMStyleWithColumns(39)); 10351 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 10352 " 1, 1, 1, 1};", 10353 getLLVMStyleWithColumns(38)); 10354 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 10355 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 10356 getLLVMStyleWithColumns(43)); 10357 verifyFormat( 10358 "static unsigned SomeValues[10][3] = {\n" 10359 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 10360 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 10361 verifyFormat("static auto fields = new vector<string>{\n" 10362 " \"aaaaaaaaaaaaa\",\n" 10363 " \"aaaaaaaaaaaaa\",\n" 10364 " \"aaaaaaaaaaaa\",\n" 10365 " \"aaaaaaaaaaaaaa\",\n" 10366 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 10367 " \"aaaaaaaaaaaa\",\n" 10368 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 10369 "};"); 10370 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 10371 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 10372 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 10373 " 3, cccccccccccccccccccccc};", 10374 getLLVMStyleWithColumns(60)); 10375 10376 // Trailing commas. 10377 verifyFormat("vector<int> x = {\n" 10378 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 10379 "};", 10380 getLLVMStyleWithColumns(39)); 10381 verifyFormat("vector<int> x = {\n" 10382 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 10383 "};", 10384 getLLVMStyleWithColumns(39)); 10385 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 10386 " 1, 1, 1, 1,\n" 10387 " /**/ /**/};", 10388 getLLVMStyleWithColumns(39)); 10389 10390 // Trailing comment in the first line. 10391 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 10392 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 10393 " 111111111, 222222222, 3333333333, 444444444, //\n" 10394 " 11111111, 22222222, 333333333, 44444444};"); 10395 // Trailing comment in the last line. 10396 verifyFormat("int aaaaa[] = {\n" 10397 " 1, 2, 3, // comment\n" 10398 " 4, 5, 6 // comment\n" 10399 "};"); 10400 10401 // With nested lists, we should either format one item per line or all nested 10402 // lists one on line. 10403 // FIXME: For some nested lists, we can do better. 10404 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 10405 " {aaaaaaaaaaaaaaaaaaa},\n" 10406 " {aaaaaaaaaaaaaaaaaaaaa},\n" 10407 " {aaaaaaaaaaaaaaaaa}};", 10408 getLLVMStyleWithColumns(60)); 10409 verifyFormat( 10410 "SomeStruct my_struct_array = {\n" 10411 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 10412 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 10413 " {aaa, aaa},\n" 10414 " {aaa, aaa},\n" 10415 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 10416 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 10417 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 10418 10419 // No column layout should be used here. 10420 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 10421 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 10422 10423 verifyNoCrash("a<,"); 10424 10425 // No braced initializer here. 10426 verifyFormat("void f() {\n" 10427 " struct Dummy {};\n" 10428 " f(v);\n" 10429 "}"); 10430 10431 // Long lists should be formatted in columns even if they are nested. 10432 verifyFormat( 10433 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10434 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10435 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10436 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10437 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10438 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 10439 10440 // Allow "single-column" layout even if that violates the column limit. There 10441 // isn't going to be a better way. 10442 verifyFormat("std::vector<int> a = {\n" 10443 " aaaaaaaa,\n" 10444 " aaaaaaaa,\n" 10445 " aaaaaaaa,\n" 10446 " aaaaaaaa,\n" 10447 " aaaaaaaaaa,\n" 10448 " aaaaaaaa,\n" 10449 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 10450 getLLVMStyleWithColumns(30)); 10451 verifyFormat("vector<int> aaaa = {\n" 10452 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10453 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10454 " aaaaaa.aaaaaaa,\n" 10455 " aaaaaa.aaaaaaa,\n" 10456 " aaaaaa.aaaaaaa,\n" 10457 " aaaaaa.aaaaaaa,\n" 10458 "};"); 10459 10460 // Don't create hanging lists. 10461 verifyFormat("someFunction(Param, {List1, List2,\n" 10462 " List3});", 10463 getLLVMStyleWithColumns(35)); 10464 verifyFormat("someFunction(Param, Param,\n" 10465 " {List1, List2,\n" 10466 " List3});", 10467 getLLVMStyleWithColumns(35)); 10468 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 10469 " aaaaaaaaaaaaaaaaaaaaaaa);"); 10470 } 10471 10472 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 10473 FormatStyle DoNotMerge = getLLVMStyle(); 10474 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10475 10476 verifyFormat("void f() { return 42; }"); 10477 verifyFormat("void f() {\n" 10478 " return 42;\n" 10479 "}", 10480 DoNotMerge); 10481 verifyFormat("void f() {\n" 10482 " // Comment\n" 10483 "}"); 10484 verifyFormat("{\n" 10485 "#error {\n" 10486 " int a;\n" 10487 "}"); 10488 verifyFormat("{\n" 10489 " int a;\n" 10490 "#error {\n" 10491 "}"); 10492 verifyFormat("void f() {} // comment"); 10493 verifyFormat("void f() { int a; } // comment"); 10494 verifyFormat("void f() {\n" 10495 "} // comment", 10496 DoNotMerge); 10497 verifyFormat("void f() {\n" 10498 " int a;\n" 10499 "} // comment", 10500 DoNotMerge); 10501 verifyFormat("void f() {\n" 10502 "} // comment", 10503 getLLVMStyleWithColumns(15)); 10504 10505 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 10506 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 10507 10508 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 10509 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 10510 verifyFormat("class C {\n" 10511 " C()\n" 10512 " : iiiiiiii(nullptr),\n" 10513 " kkkkkkk(nullptr),\n" 10514 " mmmmmmm(nullptr),\n" 10515 " nnnnnnn(nullptr) {}\n" 10516 "};", 10517 getGoogleStyle()); 10518 10519 FormatStyle NoColumnLimit = getLLVMStyle(); 10520 NoColumnLimit.ColumnLimit = 0; 10521 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 10522 EXPECT_EQ("class C {\n" 10523 " A() : b(0) {}\n" 10524 "};", 10525 format("class C{A():b(0){}};", NoColumnLimit)); 10526 EXPECT_EQ("A()\n" 10527 " : b(0) {\n" 10528 "}", 10529 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 10530 10531 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 10532 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 10533 FormatStyle::SFS_None; 10534 EXPECT_EQ("A()\n" 10535 " : b(0) {\n" 10536 "}", 10537 format("A():b(0){}", DoNotMergeNoColumnLimit)); 10538 EXPECT_EQ("A()\n" 10539 " : b(0) {\n" 10540 "}", 10541 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 10542 10543 verifyFormat("#define A \\\n" 10544 " void f() { \\\n" 10545 " int i; \\\n" 10546 " }", 10547 getLLVMStyleWithColumns(20)); 10548 verifyFormat("#define A \\\n" 10549 " void f() { int i; }", 10550 getLLVMStyleWithColumns(21)); 10551 verifyFormat("#define A \\\n" 10552 " void f() { \\\n" 10553 " int i; \\\n" 10554 " } \\\n" 10555 " int j;", 10556 getLLVMStyleWithColumns(22)); 10557 verifyFormat("#define A \\\n" 10558 " void f() { int i; } \\\n" 10559 " int j;", 10560 getLLVMStyleWithColumns(23)); 10561 } 10562 10563 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 10564 FormatStyle MergeEmptyOnly = getLLVMStyle(); 10565 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 10566 verifyFormat("class C {\n" 10567 " int f() {}\n" 10568 "};", 10569 MergeEmptyOnly); 10570 verifyFormat("class C {\n" 10571 " int f() {\n" 10572 " return 42;\n" 10573 " }\n" 10574 "};", 10575 MergeEmptyOnly); 10576 verifyFormat("int f() {}", MergeEmptyOnly); 10577 verifyFormat("int f() {\n" 10578 " return 42;\n" 10579 "}", 10580 MergeEmptyOnly); 10581 10582 // Also verify behavior when BraceWrapping.AfterFunction = true 10583 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 10584 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 10585 verifyFormat("int f() {}", MergeEmptyOnly); 10586 verifyFormat("class C {\n" 10587 " int f() {}\n" 10588 "};", 10589 MergeEmptyOnly); 10590 } 10591 10592 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 10593 FormatStyle MergeInlineOnly = getLLVMStyle(); 10594 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 10595 verifyFormat("class C {\n" 10596 " int f() { return 42; }\n" 10597 "};", 10598 MergeInlineOnly); 10599 verifyFormat("int f() {\n" 10600 " return 42;\n" 10601 "}", 10602 MergeInlineOnly); 10603 10604 // SFS_Inline implies SFS_Empty 10605 verifyFormat("class C {\n" 10606 " int f() {}\n" 10607 "};", 10608 MergeInlineOnly); 10609 verifyFormat("int f() {}", MergeInlineOnly); 10610 10611 // Also verify behavior when BraceWrapping.AfterFunction = true 10612 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 10613 MergeInlineOnly.BraceWrapping.AfterFunction = true; 10614 verifyFormat("class C {\n" 10615 " int f() { return 42; }\n" 10616 "};", 10617 MergeInlineOnly); 10618 verifyFormat("int f()\n" 10619 "{\n" 10620 " return 42;\n" 10621 "}", 10622 MergeInlineOnly); 10623 10624 // SFS_Inline implies SFS_Empty 10625 verifyFormat("int f() {}", MergeInlineOnly); 10626 verifyFormat("class C {\n" 10627 " int f() {}\n" 10628 "};", 10629 MergeInlineOnly); 10630 } 10631 10632 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 10633 FormatStyle MergeInlineOnly = getLLVMStyle(); 10634 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 10635 FormatStyle::SFS_InlineOnly; 10636 verifyFormat("class C {\n" 10637 " int f() { return 42; }\n" 10638 "};", 10639 MergeInlineOnly); 10640 verifyFormat("int f() {\n" 10641 " return 42;\n" 10642 "}", 10643 MergeInlineOnly); 10644 10645 // SFS_InlineOnly does not imply SFS_Empty 10646 verifyFormat("class C {\n" 10647 " int f() {}\n" 10648 "};", 10649 MergeInlineOnly); 10650 verifyFormat("int f() {\n" 10651 "}", 10652 MergeInlineOnly); 10653 10654 // Also verify behavior when BraceWrapping.AfterFunction = true 10655 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 10656 MergeInlineOnly.BraceWrapping.AfterFunction = true; 10657 verifyFormat("class C {\n" 10658 " int f() { return 42; }\n" 10659 "};", 10660 MergeInlineOnly); 10661 verifyFormat("int f()\n" 10662 "{\n" 10663 " return 42;\n" 10664 "}", 10665 MergeInlineOnly); 10666 10667 // SFS_InlineOnly does not imply SFS_Empty 10668 verifyFormat("int f()\n" 10669 "{\n" 10670 "}", 10671 MergeInlineOnly); 10672 verifyFormat("class C {\n" 10673 " int f() {}\n" 10674 "};", 10675 MergeInlineOnly); 10676 } 10677 10678 TEST_F(FormatTest, SplitEmptyFunction) { 10679 FormatStyle Style = getLLVMStyle(); 10680 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10681 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 10682 Style.BraceWrapping.AfterFunction = true; 10683 Style.BraceWrapping.SplitEmptyFunction = false; 10684 Style.ColumnLimit = 40; 10685 10686 verifyFormat("int f()\n" 10687 "{}", 10688 Style); 10689 verifyFormat("int f()\n" 10690 "{\n" 10691 " return 42;\n" 10692 "}", 10693 Style); 10694 verifyFormat("int f()\n" 10695 "{\n" 10696 " // some comment\n" 10697 "}", 10698 Style); 10699 10700 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 10701 verifyFormat("int f() {}", Style); 10702 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 10703 "{}", 10704 Style); 10705 verifyFormat("int f()\n" 10706 "{\n" 10707 " return 0;\n" 10708 "}", 10709 Style); 10710 10711 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 10712 verifyFormat("class Foo {\n" 10713 " int f() {}\n" 10714 "};\n", 10715 Style); 10716 verifyFormat("class Foo {\n" 10717 " int f() { return 0; }\n" 10718 "};\n", 10719 Style); 10720 verifyFormat("class Foo {\n" 10721 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 10722 " {}\n" 10723 "};\n", 10724 Style); 10725 verifyFormat("class Foo {\n" 10726 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 10727 " {\n" 10728 " return 0;\n" 10729 " }\n" 10730 "};\n", 10731 Style); 10732 10733 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10734 verifyFormat("int f() {}", Style); 10735 verifyFormat("int f() { return 0; }", Style); 10736 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 10737 "{}", 10738 Style); 10739 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 10740 "{\n" 10741 " return 0;\n" 10742 "}", 10743 Style); 10744 } 10745 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 10746 FormatStyle Style = getLLVMStyle(); 10747 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10748 verifyFormat("#ifdef A\n" 10749 "int f() {}\n" 10750 "#else\n" 10751 "int g() {}\n" 10752 "#endif", 10753 Style); 10754 } 10755 10756 TEST_F(FormatTest, SplitEmptyClass) { 10757 FormatStyle Style = getLLVMStyle(); 10758 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 10759 Style.BraceWrapping.AfterClass = true; 10760 Style.BraceWrapping.SplitEmptyRecord = false; 10761 10762 verifyFormat("class Foo\n" 10763 "{};", 10764 Style); 10765 verifyFormat("/* something */ class Foo\n" 10766 "{};", 10767 Style); 10768 verifyFormat("template <typename X> class Foo\n" 10769 "{};", 10770 Style); 10771 verifyFormat("class Foo\n" 10772 "{\n" 10773 " Foo();\n" 10774 "};", 10775 Style); 10776 verifyFormat("typedef class Foo\n" 10777 "{\n" 10778 "} Foo_t;", 10779 Style); 10780 10781 Style.BraceWrapping.SplitEmptyRecord = true; 10782 Style.BraceWrapping.AfterStruct = true; 10783 verifyFormat("class rep\n" 10784 "{\n" 10785 "};", 10786 Style); 10787 verifyFormat("struct rep\n" 10788 "{\n" 10789 "};", 10790 Style); 10791 verifyFormat("template <typename T> class rep\n" 10792 "{\n" 10793 "};", 10794 Style); 10795 verifyFormat("template <typename T> struct rep\n" 10796 "{\n" 10797 "};", 10798 Style); 10799 verifyFormat("class rep\n" 10800 "{\n" 10801 " int x;\n" 10802 "};", 10803 Style); 10804 verifyFormat("struct rep\n" 10805 "{\n" 10806 " int x;\n" 10807 "};", 10808 Style); 10809 verifyFormat("template <typename T> class rep\n" 10810 "{\n" 10811 " int x;\n" 10812 "};", 10813 Style); 10814 verifyFormat("template <typename T> struct rep\n" 10815 "{\n" 10816 " int x;\n" 10817 "};", 10818 Style); 10819 verifyFormat("template <typename T> class rep // Foo\n" 10820 "{\n" 10821 " int x;\n" 10822 "};", 10823 Style); 10824 verifyFormat("template <typename T> struct rep // Bar\n" 10825 "{\n" 10826 " int x;\n" 10827 "};", 10828 Style); 10829 10830 verifyFormat("template <typename T> class rep<T>\n" 10831 "{\n" 10832 " int x;\n" 10833 "};", 10834 Style); 10835 10836 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 10837 "{\n" 10838 " int x;\n" 10839 "};", 10840 Style); 10841 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 10842 "{\n" 10843 "};", 10844 Style); 10845 10846 verifyFormat("#include \"stdint.h\"\n" 10847 "namespace rep {}", 10848 Style); 10849 verifyFormat("#include <stdint.h>\n" 10850 "namespace rep {}", 10851 Style); 10852 verifyFormat("#include <stdint.h>\n" 10853 "namespace rep {}", 10854 "#include <stdint.h>\n" 10855 "namespace rep {\n" 10856 "\n" 10857 "\n" 10858 "}", 10859 Style); 10860 } 10861 10862 TEST_F(FormatTest, SplitEmptyStruct) { 10863 FormatStyle Style = getLLVMStyle(); 10864 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 10865 Style.BraceWrapping.AfterStruct = true; 10866 Style.BraceWrapping.SplitEmptyRecord = false; 10867 10868 verifyFormat("struct Foo\n" 10869 "{};", 10870 Style); 10871 verifyFormat("/* something */ struct Foo\n" 10872 "{};", 10873 Style); 10874 verifyFormat("template <typename X> struct Foo\n" 10875 "{};", 10876 Style); 10877 verifyFormat("struct Foo\n" 10878 "{\n" 10879 " Foo();\n" 10880 "};", 10881 Style); 10882 verifyFormat("typedef struct Foo\n" 10883 "{\n" 10884 "} Foo_t;", 10885 Style); 10886 // typedef struct Bar {} Bar_t; 10887 } 10888 10889 TEST_F(FormatTest, SplitEmptyUnion) { 10890 FormatStyle Style = getLLVMStyle(); 10891 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 10892 Style.BraceWrapping.AfterUnion = true; 10893 Style.BraceWrapping.SplitEmptyRecord = false; 10894 10895 verifyFormat("union Foo\n" 10896 "{};", 10897 Style); 10898 verifyFormat("/* something */ union Foo\n" 10899 "{};", 10900 Style); 10901 verifyFormat("union Foo\n" 10902 "{\n" 10903 " A,\n" 10904 "};", 10905 Style); 10906 verifyFormat("typedef union Foo\n" 10907 "{\n" 10908 "} Foo_t;", 10909 Style); 10910 } 10911 10912 TEST_F(FormatTest, SplitEmptyNamespace) { 10913 FormatStyle Style = getLLVMStyle(); 10914 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 10915 Style.BraceWrapping.AfterNamespace = true; 10916 Style.BraceWrapping.SplitEmptyNamespace = false; 10917 10918 verifyFormat("namespace Foo\n" 10919 "{};", 10920 Style); 10921 verifyFormat("/* something */ namespace Foo\n" 10922 "{};", 10923 Style); 10924 verifyFormat("inline namespace Foo\n" 10925 "{};", 10926 Style); 10927 verifyFormat("/* something */ inline namespace Foo\n" 10928 "{};", 10929 Style); 10930 verifyFormat("export namespace Foo\n" 10931 "{};", 10932 Style); 10933 verifyFormat("namespace Foo\n" 10934 "{\n" 10935 "void Bar();\n" 10936 "};", 10937 Style); 10938 } 10939 10940 TEST_F(FormatTest, NeverMergeShortRecords) { 10941 FormatStyle Style = getLLVMStyle(); 10942 10943 verifyFormat("class Foo {\n" 10944 " Foo();\n" 10945 "};", 10946 Style); 10947 verifyFormat("typedef class Foo {\n" 10948 " Foo();\n" 10949 "} Foo_t;", 10950 Style); 10951 verifyFormat("struct Foo {\n" 10952 " Foo();\n" 10953 "};", 10954 Style); 10955 verifyFormat("typedef struct Foo {\n" 10956 " Foo();\n" 10957 "} Foo_t;", 10958 Style); 10959 verifyFormat("union Foo {\n" 10960 " A,\n" 10961 "};", 10962 Style); 10963 verifyFormat("typedef union Foo {\n" 10964 " A,\n" 10965 "} Foo_t;", 10966 Style); 10967 verifyFormat("namespace Foo {\n" 10968 "void Bar();\n" 10969 "};", 10970 Style); 10971 10972 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 10973 Style.BraceWrapping.AfterClass = true; 10974 Style.BraceWrapping.AfterStruct = true; 10975 Style.BraceWrapping.AfterUnion = true; 10976 Style.BraceWrapping.AfterNamespace = true; 10977 verifyFormat("class Foo\n" 10978 "{\n" 10979 " Foo();\n" 10980 "};", 10981 Style); 10982 verifyFormat("typedef class Foo\n" 10983 "{\n" 10984 " Foo();\n" 10985 "} Foo_t;", 10986 Style); 10987 verifyFormat("struct Foo\n" 10988 "{\n" 10989 " Foo();\n" 10990 "};", 10991 Style); 10992 verifyFormat("typedef struct Foo\n" 10993 "{\n" 10994 " Foo();\n" 10995 "} Foo_t;", 10996 Style); 10997 verifyFormat("union Foo\n" 10998 "{\n" 10999 " A,\n" 11000 "};", 11001 Style); 11002 verifyFormat("typedef union Foo\n" 11003 "{\n" 11004 " A,\n" 11005 "} Foo_t;", 11006 Style); 11007 verifyFormat("namespace Foo\n" 11008 "{\n" 11009 "void Bar();\n" 11010 "};", 11011 Style); 11012 } 11013 11014 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 11015 // Elaborate type variable declarations. 11016 verifyFormat("struct foo a = {bar};\nint n;"); 11017 verifyFormat("class foo a = {bar};\nint n;"); 11018 verifyFormat("union foo a = {bar};\nint n;"); 11019 11020 // Elaborate types inside function definitions. 11021 verifyFormat("struct foo f() {}\nint n;"); 11022 verifyFormat("class foo f() {}\nint n;"); 11023 verifyFormat("union foo f() {}\nint n;"); 11024 11025 // Templates. 11026 verifyFormat("template <class X> void f() {}\nint n;"); 11027 verifyFormat("template <struct X> void f() {}\nint n;"); 11028 verifyFormat("template <union X> void f() {}\nint n;"); 11029 11030 // Actual definitions... 11031 verifyFormat("struct {\n} n;"); 11032 verifyFormat( 11033 "template <template <class T, class Y>, class Z> class X {\n} n;"); 11034 verifyFormat("union Z {\n int n;\n} x;"); 11035 verifyFormat("class MACRO Z {\n} n;"); 11036 verifyFormat("class MACRO(X) Z {\n} n;"); 11037 verifyFormat("class __attribute__(X) Z {\n} n;"); 11038 verifyFormat("class __declspec(X) Z {\n} n;"); 11039 verifyFormat("class A##B##C {\n} n;"); 11040 verifyFormat("class alignas(16) Z {\n} n;"); 11041 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 11042 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 11043 11044 // Redefinition from nested context: 11045 verifyFormat("class A::B::C {\n} n;"); 11046 11047 // Template definitions. 11048 verifyFormat( 11049 "template <typename F>\n" 11050 "Matcher(const Matcher<F> &Other,\n" 11051 " typename enable_if_c<is_base_of<F, T>::value &&\n" 11052 " !is_same<F, T>::value>::type * = 0)\n" 11053 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 11054 11055 // FIXME: This is still incorrectly handled at the formatter side. 11056 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 11057 verifyFormat("int i = SomeFunction(a<b, a> b);"); 11058 11059 // FIXME: 11060 // This now gets parsed incorrectly as class definition. 11061 // verifyFormat("class A<int> f() {\n}\nint n;"); 11062 11063 // Elaborate types where incorrectly parsing the structural element would 11064 // break the indent. 11065 verifyFormat("if (true)\n" 11066 " class X x;\n" 11067 "else\n" 11068 " f();\n"); 11069 11070 // This is simply incomplete. Formatting is not important, but must not crash. 11071 verifyFormat("class A:"); 11072 } 11073 11074 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 11075 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 11076 format("#error Leave all white!!!!! space* alone!\n")); 11077 EXPECT_EQ( 11078 "#warning Leave all white!!!!! space* alone!\n", 11079 format("#warning Leave all white!!!!! space* alone!\n")); 11080 EXPECT_EQ("#error 1", format(" # error 1")); 11081 EXPECT_EQ("#warning 1", format(" # warning 1")); 11082 } 11083 11084 TEST_F(FormatTest, FormatHashIfExpressions) { 11085 verifyFormat("#if AAAA && BBBB"); 11086 verifyFormat("#if (AAAA && BBBB)"); 11087 verifyFormat("#elif (AAAA && BBBB)"); 11088 // FIXME: Come up with a better indentation for #elif. 11089 verifyFormat( 11090 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 11091 " defined(BBBBBBBB)\n" 11092 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 11093 " defined(BBBBBBBB)\n" 11094 "#endif", 11095 getLLVMStyleWithColumns(65)); 11096 } 11097 11098 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 11099 FormatStyle AllowsMergedIf = getGoogleStyle(); 11100 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 11101 FormatStyle::SIS_WithoutElse; 11102 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 11103 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 11104 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 11105 EXPECT_EQ("if (true) return 42;", 11106 format("if (true)\nreturn 42;", AllowsMergedIf)); 11107 FormatStyle ShortMergedIf = AllowsMergedIf; 11108 ShortMergedIf.ColumnLimit = 25; 11109 verifyFormat("#define A \\\n" 11110 " if (true) return 42;", 11111 ShortMergedIf); 11112 verifyFormat("#define A \\\n" 11113 " f(); \\\n" 11114 " if (true)\n" 11115 "#define B", 11116 ShortMergedIf); 11117 verifyFormat("#define A \\\n" 11118 " f(); \\\n" 11119 " if (true)\n" 11120 "g();", 11121 ShortMergedIf); 11122 verifyFormat("{\n" 11123 "#ifdef A\n" 11124 " // Comment\n" 11125 " if (true) continue;\n" 11126 "#endif\n" 11127 " // Comment\n" 11128 " if (true) continue;\n" 11129 "}", 11130 ShortMergedIf); 11131 ShortMergedIf.ColumnLimit = 33; 11132 verifyFormat("#define A \\\n" 11133 " if constexpr (true) return 42;", 11134 ShortMergedIf); 11135 verifyFormat("#define A \\\n" 11136 " if CONSTEXPR (true) return 42;", 11137 ShortMergedIf); 11138 ShortMergedIf.ColumnLimit = 29; 11139 verifyFormat("#define A \\\n" 11140 " if (aaaaaaaaaa) return 1; \\\n" 11141 " return 2;", 11142 ShortMergedIf); 11143 ShortMergedIf.ColumnLimit = 28; 11144 verifyFormat("#define A \\\n" 11145 " if (aaaaaaaaaa) \\\n" 11146 " return 1; \\\n" 11147 " return 2;", 11148 ShortMergedIf); 11149 verifyFormat("#define A \\\n" 11150 " if constexpr (aaaaaaa) \\\n" 11151 " return 1; \\\n" 11152 " return 2;", 11153 ShortMergedIf); 11154 verifyFormat("#define A \\\n" 11155 " if CONSTEXPR (aaaaaaa) \\\n" 11156 " return 1; \\\n" 11157 " return 2;", 11158 ShortMergedIf); 11159 } 11160 11161 TEST_F(FormatTest, FormatStarDependingOnContext) { 11162 verifyFormat("void f(int *a);"); 11163 verifyFormat("void f() { f(fint * b); }"); 11164 verifyFormat("class A {\n void f(int *a);\n};"); 11165 verifyFormat("class A {\n int *a;\n};"); 11166 verifyFormat("namespace a {\n" 11167 "namespace b {\n" 11168 "class A {\n" 11169 " void f() {}\n" 11170 " int *a;\n" 11171 "};\n" 11172 "} // namespace b\n" 11173 "} // namespace a"); 11174 } 11175 11176 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 11177 verifyFormat("while"); 11178 verifyFormat("operator"); 11179 } 11180 11181 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 11182 // This code would be painfully slow to format if we didn't skip it. 11183 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 11184 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 11185 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 11186 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 11187 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 11188 "A(1, 1)\n" 11189 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 11190 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11191 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11192 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11193 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11194 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11195 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11196 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11197 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11198 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 11199 // Deeply nested part is untouched, rest is formatted. 11200 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 11201 format(std::string("int i;\n") + Code + "int j;\n", 11202 getLLVMStyle(), SC_ExpectIncomplete)); 11203 } 11204 11205 //===----------------------------------------------------------------------===// 11206 // Objective-C tests. 11207 //===----------------------------------------------------------------------===// 11208 11209 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 11210 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 11211 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 11212 format("-(NSUInteger)indexOfObject:(id)anObject;")); 11213 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 11214 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 11215 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 11216 format("-(NSInteger)Method3:(id)anObject;")); 11217 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 11218 format("-(NSInteger)Method4:(id)anObject;")); 11219 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 11220 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 11221 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 11222 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 11223 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 11224 "forAllCells:(BOOL)flag;", 11225 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 11226 "forAllCells:(BOOL)flag;")); 11227 11228 // Very long objectiveC method declaration. 11229 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 11230 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 11231 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 11232 " inRange:(NSRange)range\n" 11233 " outRange:(NSRange)out_range\n" 11234 " outRange1:(NSRange)out_range1\n" 11235 " outRange2:(NSRange)out_range2\n" 11236 " outRange3:(NSRange)out_range3\n" 11237 " outRange4:(NSRange)out_range4\n" 11238 " outRange5:(NSRange)out_range5\n" 11239 " outRange6:(NSRange)out_range6\n" 11240 " outRange7:(NSRange)out_range7\n" 11241 " outRange8:(NSRange)out_range8\n" 11242 " outRange9:(NSRange)out_range9;"); 11243 11244 // When the function name has to be wrapped. 11245 FormatStyle Style = getLLVMStyle(); 11246 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 11247 // and always indents instead. 11248 Style.IndentWrappedFunctionNames = false; 11249 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 11250 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 11251 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 11252 "}", 11253 Style); 11254 Style.IndentWrappedFunctionNames = true; 11255 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 11256 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 11257 " anotherName:(NSString)dddddddddddddd {\n" 11258 "}", 11259 Style); 11260 11261 verifyFormat("- (int)sum:(vector<int>)numbers;"); 11262 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 11263 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 11264 // protocol lists (but not for template classes): 11265 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 11266 11267 verifyFormat("- (int (*)())foo:(int (*)())f;"); 11268 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 11269 11270 // If there's no return type (very rare in practice!), LLVM and Google style 11271 // agree. 11272 verifyFormat("- foo;"); 11273 verifyFormat("- foo:(int)f;"); 11274 verifyGoogleFormat("- foo:(int)foo;"); 11275 } 11276 11277 TEST_F(FormatTest, BreaksStringLiterals) { 11278 EXPECT_EQ("\"some text \"\n" 11279 "\"other\";", 11280 format("\"some text other\";", getLLVMStyleWithColumns(12))); 11281 EXPECT_EQ("\"some text \"\n" 11282 "\"other\";", 11283 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 11284 EXPECT_EQ( 11285 "#define A \\\n" 11286 " \"some \" \\\n" 11287 " \"text \" \\\n" 11288 " \"other\";", 11289 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 11290 EXPECT_EQ( 11291 "#define A \\\n" 11292 " \"so \" \\\n" 11293 " \"text \" \\\n" 11294 " \"other\";", 11295 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 11296 11297 EXPECT_EQ("\"some text\"", 11298 format("\"some text\"", getLLVMStyleWithColumns(1))); 11299 EXPECT_EQ("\"some text\"", 11300 format("\"some text\"", getLLVMStyleWithColumns(11))); 11301 EXPECT_EQ("\"some \"\n" 11302 "\"text\"", 11303 format("\"some text\"", getLLVMStyleWithColumns(10))); 11304 EXPECT_EQ("\"some \"\n" 11305 "\"text\"", 11306 format("\"some text\"", getLLVMStyleWithColumns(7))); 11307 EXPECT_EQ("\"some\"\n" 11308 "\" tex\"\n" 11309 "\"t\"", 11310 format("\"some text\"", getLLVMStyleWithColumns(6))); 11311 EXPECT_EQ("\"some\"\n" 11312 "\" tex\"\n" 11313 "\" and\"", 11314 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 11315 EXPECT_EQ("\"some\"\n" 11316 "\"/tex\"\n" 11317 "\"/and\"", 11318 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 11319 11320 EXPECT_EQ("variable =\n" 11321 " \"long string \"\n" 11322 " \"literal\";", 11323 format("variable = \"long string literal\";", 11324 getLLVMStyleWithColumns(20))); 11325 11326 EXPECT_EQ("variable = f(\n" 11327 " \"long string \"\n" 11328 " \"literal\",\n" 11329 " short,\n" 11330 " loooooooooooooooooooong);", 11331 format("variable = f(\"long string literal\", short, " 11332 "loooooooooooooooooooong);", 11333 getLLVMStyleWithColumns(20))); 11334 11335 EXPECT_EQ( 11336 "f(g(\"long string \"\n" 11337 " \"literal\"),\n" 11338 " b);", 11339 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 11340 EXPECT_EQ("f(g(\"long string \"\n" 11341 " \"literal\",\n" 11342 " a),\n" 11343 " b);", 11344 format("f(g(\"long string literal\", a), b);", 11345 getLLVMStyleWithColumns(20))); 11346 EXPECT_EQ( 11347 "f(\"one two\".split(\n" 11348 " variable));", 11349 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 11350 EXPECT_EQ("f(\"one two three four five six \"\n" 11351 " \"seven\".split(\n" 11352 " really_looooong_variable));", 11353 format("f(\"one two three four five six seven\"." 11354 "split(really_looooong_variable));", 11355 getLLVMStyleWithColumns(33))); 11356 11357 EXPECT_EQ("f(\"some \"\n" 11358 " \"text\",\n" 11359 " other);", 11360 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 11361 11362 // Only break as a last resort. 11363 verifyFormat( 11364 "aaaaaaaaaaaaaaaaaaaa(\n" 11365 " aaaaaaaaaaaaaaaaaaaa,\n" 11366 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 11367 11368 EXPECT_EQ("\"splitmea\"\n" 11369 "\"trandomp\"\n" 11370 "\"oint\"", 11371 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 11372 11373 EXPECT_EQ("\"split/\"\n" 11374 "\"pathat/\"\n" 11375 "\"slashes\"", 11376 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 11377 11378 EXPECT_EQ("\"split/\"\n" 11379 "\"pathat/\"\n" 11380 "\"slashes\"", 11381 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 11382 EXPECT_EQ("\"split at \"\n" 11383 "\"spaces/at/\"\n" 11384 "\"slashes.at.any$\"\n" 11385 "\"non-alphanumeric%\"\n" 11386 "\"1111111111characte\"\n" 11387 "\"rs\"", 11388 format("\"split at " 11389 "spaces/at/" 11390 "slashes.at." 11391 "any$non-" 11392 "alphanumeric%" 11393 "1111111111characte" 11394 "rs\"", 11395 getLLVMStyleWithColumns(20))); 11396 11397 // Verify that splitting the strings understands 11398 // Style::AlwaysBreakBeforeMultilineStrings. 11399 EXPECT_EQ("aaaaaaaaaaaa(\n" 11400 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 11401 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 11402 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 11403 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 11404 "aaaaaaaaaaaaaaaaaaaaaa\");", 11405 getGoogleStyle())); 11406 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 11407 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 11408 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 11409 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 11410 "aaaaaaaaaaaaaaaaaaaaaa\";", 11411 getGoogleStyle())); 11412 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 11413 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 11414 format("llvm::outs() << " 11415 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 11416 "aaaaaaaaaaaaaaaaaaa\";")); 11417 EXPECT_EQ("ffff(\n" 11418 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 11419 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 11420 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 11421 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 11422 getGoogleStyle())); 11423 11424 FormatStyle Style = getLLVMStyleWithColumns(12); 11425 Style.BreakStringLiterals = false; 11426 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 11427 11428 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 11429 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 11430 EXPECT_EQ("#define A \\\n" 11431 " \"some \" \\\n" 11432 " \"text \" \\\n" 11433 " \"other\";", 11434 format("#define A \"some text other\";", AlignLeft)); 11435 } 11436 11437 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 11438 EXPECT_EQ("C a = \"some more \"\n" 11439 " \"text\";", 11440 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 11441 } 11442 11443 TEST_F(FormatTest, FullyRemoveEmptyLines) { 11444 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 11445 NoEmptyLines.MaxEmptyLinesToKeep = 0; 11446 EXPECT_EQ("int i = a(b());", 11447 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 11448 } 11449 11450 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 11451 EXPECT_EQ( 11452 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11453 "(\n" 11454 " \"x\t\");", 11455 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11456 "aaaaaaa(" 11457 "\"x\t\");")); 11458 } 11459 11460 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 11461 EXPECT_EQ( 11462 "u8\"utf8 string \"\n" 11463 "u8\"literal\";", 11464 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 11465 EXPECT_EQ( 11466 "u\"utf16 string \"\n" 11467 "u\"literal\";", 11468 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 11469 EXPECT_EQ( 11470 "U\"utf32 string \"\n" 11471 "U\"literal\";", 11472 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 11473 EXPECT_EQ("L\"wide string \"\n" 11474 "L\"literal\";", 11475 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 11476 EXPECT_EQ("@\"NSString \"\n" 11477 "@\"literal\";", 11478 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 11479 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 11480 11481 // This input makes clang-format try to split the incomplete unicode escape 11482 // sequence, which used to lead to a crasher. 11483 verifyNoCrash( 11484 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 11485 getLLVMStyleWithColumns(60)); 11486 } 11487 11488 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 11489 FormatStyle Style = getGoogleStyleWithColumns(15); 11490 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 11491 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 11492 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 11493 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 11494 EXPECT_EQ("u8R\"x(raw literal)x\";", 11495 format("u8R\"x(raw literal)x\";", Style)); 11496 } 11497 11498 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 11499 FormatStyle Style = getLLVMStyleWithColumns(20); 11500 EXPECT_EQ( 11501 "_T(\"aaaaaaaaaaaaaa\")\n" 11502 "_T(\"aaaaaaaaaaaaaa\")\n" 11503 "_T(\"aaaaaaaaaaaa\")", 11504 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 11505 EXPECT_EQ("f(x,\n" 11506 " _T(\"aaaaaaaaaaaa\")\n" 11507 " _T(\"aaa\"),\n" 11508 " z);", 11509 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 11510 11511 // FIXME: Handle embedded spaces in one iteration. 11512 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 11513 // "_T(\"aaaaaaaaaaaaa\")\n" 11514 // "_T(\"aaaaaaaaaaaaa\")\n" 11515 // "_T(\"a\")", 11516 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 11517 // getLLVMStyleWithColumns(20))); 11518 EXPECT_EQ( 11519 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 11520 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 11521 EXPECT_EQ("f(\n" 11522 "#if !TEST\n" 11523 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 11524 "#endif\n" 11525 ");", 11526 format("f(\n" 11527 "#if !TEST\n" 11528 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 11529 "#endif\n" 11530 ");")); 11531 EXPECT_EQ("f(\n" 11532 "\n" 11533 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 11534 format("f(\n" 11535 "\n" 11536 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 11537 } 11538 11539 TEST_F(FormatTest, BreaksStringLiteralOperands) { 11540 // In a function call with two operands, the second can be broken with no line 11541 // break before it. 11542 EXPECT_EQ( 11543 "func(a, \"long long \"\n" 11544 " \"long long\");", 11545 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 11546 // In a function call with three operands, the second must be broken with a 11547 // line break before it. 11548 EXPECT_EQ("func(a,\n" 11549 " \"long long long \"\n" 11550 " \"long\",\n" 11551 " c);", 11552 format("func(a, \"long long long long\", c);", 11553 getLLVMStyleWithColumns(24))); 11554 // In a function call with three operands, the third must be broken with a 11555 // line break before it. 11556 EXPECT_EQ("func(a, b,\n" 11557 " \"long long long \"\n" 11558 " \"long\");", 11559 format("func(a, b, \"long long long long\");", 11560 getLLVMStyleWithColumns(24))); 11561 // In a function call with three operands, both the second and the third must 11562 // be broken with a line break before them. 11563 EXPECT_EQ("func(a,\n" 11564 " \"long long long \"\n" 11565 " \"long\",\n" 11566 " \"long long long \"\n" 11567 " \"long\");", 11568 format("func(a, \"long long long long\", \"long long long long\");", 11569 getLLVMStyleWithColumns(24))); 11570 // In a chain of << with two operands, the second can be broken with no line 11571 // break before it. 11572 EXPECT_EQ("a << \"line line \"\n" 11573 " \"line\";", 11574 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 11575 // In a chain of << with three operands, the second can be broken with no line 11576 // break before it. 11577 EXPECT_EQ( 11578 "abcde << \"line \"\n" 11579 " \"line line\"\n" 11580 " << c;", 11581 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 11582 // In a chain of << with three operands, the third must be broken with a line 11583 // break before it. 11584 EXPECT_EQ( 11585 "a << b\n" 11586 " << \"line line \"\n" 11587 " \"line\";", 11588 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 11589 // In a chain of << with three operands, the second can be broken with no line 11590 // break before it and the third must be broken with a line break before it. 11591 EXPECT_EQ("abcd << \"line line \"\n" 11592 " \"line\"\n" 11593 " << \"line line \"\n" 11594 " \"line\";", 11595 format("abcd << \"line line line\" << \"line line line\";", 11596 getLLVMStyleWithColumns(20))); 11597 // In a chain of binary operators with two operands, the second can be broken 11598 // with no line break before it. 11599 EXPECT_EQ( 11600 "abcd + \"line line \"\n" 11601 " \"line line\";", 11602 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 11603 // In a chain of binary operators with three operands, the second must be 11604 // broken with a line break before it. 11605 EXPECT_EQ("abcd +\n" 11606 " \"line line \"\n" 11607 " \"line line\" +\n" 11608 " e;", 11609 format("abcd + \"line line line line\" + e;", 11610 getLLVMStyleWithColumns(20))); 11611 // In a function call with two operands, with AlignAfterOpenBracket enabled, 11612 // the first must be broken with a line break before it. 11613 FormatStyle Style = getLLVMStyleWithColumns(25); 11614 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11615 EXPECT_EQ("someFunction(\n" 11616 " \"long long long \"\n" 11617 " \"long\",\n" 11618 " a);", 11619 format("someFunction(\"long long long long\", a);", Style)); 11620 } 11621 11622 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 11623 EXPECT_EQ( 11624 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 11625 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 11626 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 11627 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 11628 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 11629 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 11630 } 11631 11632 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 11633 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 11634 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 11635 EXPECT_EQ("fffffffffff(g(R\"x(\n" 11636 "multiline raw string literal xxxxxxxxxxxxxx\n" 11637 ")x\",\n" 11638 " a),\n" 11639 " b);", 11640 format("fffffffffff(g(R\"x(\n" 11641 "multiline raw string literal xxxxxxxxxxxxxx\n" 11642 ")x\", a), b);", 11643 getGoogleStyleWithColumns(20))); 11644 EXPECT_EQ("fffffffffff(\n" 11645 " g(R\"x(qqq\n" 11646 "multiline raw string literal xxxxxxxxxxxxxx\n" 11647 ")x\",\n" 11648 " a),\n" 11649 " b);", 11650 format("fffffffffff(g(R\"x(qqq\n" 11651 "multiline raw string literal xxxxxxxxxxxxxx\n" 11652 ")x\", a), b);", 11653 getGoogleStyleWithColumns(20))); 11654 11655 EXPECT_EQ("fffffffffff(R\"x(\n" 11656 "multiline raw string literal xxxxxxxxxxxxxx\n" 11657 ")x\");", 11658 format("fffffffffff(R\"x(\n" 11659 "multiline raw string literal xxxxxxxxxxxxxx\n" 11660 ")x\");", 11661 getGoogleStyleWithColumns(20))); 11662 EXPECT_EQ("fffffffffff(R\"x(\n" 11663 "multiline raw string literal xxxxxxxxxxxxxx\n" 11664 ")x\" + bbbbbb);", 11665 format("fffffffffff(R\"x(\n" 11666 "multiline raw string literal xxxxxxxxxxxxxx\n" 11667 ")x\" + bbbbbb);", 11668 getGoogleStyleWithColumns(20))); 11669 EXPECT_EQ("fffffffffff(\n" 11670 " R\"x(\n" 11671 "multiline raw string literal xxxxxxxxxxxxxx\n" 11672 ")x\" +\n" 11673 " bbbbbb);", 11674 format("fffffffffff(\n" 11675 " R\"x(\n" 11676 "multiline raw string literal xxxxxxxxxxxxxx\n" 11677 ")x\" + bbbbbb);", 11678 getGoogleStyleWithColumns(20))); 11679 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 11680 format("fffffffffff(\n" 11681 " R\"(single line raw string)\" + bbbbbb);")); 11682 } 11683 11684 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 11685 verifyFormat("string a = \"unterminated;"); 11686 EXPECT_EQ("function(\"unterminated,\n" 11687 " OtherParameter);", 11688 format("function( \"unterminated,\n" 11689 " OtherParameter);")); 11690 } 11691 11692 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 11693 FormatStyle Style = getLLVMStyle(); 11694 Style.Standard = FormatStyle::LS_Cpp03; 11695 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 11696 format("#define x(_a) printf(\"foo\"_a);", Style)); 11697 } 11698 11699 TEST_F(FormatTest, CppLexVersion) { 11700 FormatStyle Style = getLLVMStyle(); 11701 // Formatting of x * y differs if x is a type. 11702 verifyFormat("void foo() { MACRO(a * b); }", Style); 11703 verifyFormat("void foo() { MACRO(int *b); }", Style); 11704 11705 // LLVM style uses latest lexer. 11706 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 11707 Style.Standard = FormatStyle::LS_Cpp17; 11708 // But in c++17, char8_t isn't a keyword. 11709 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 11710 } 11711 11712 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 11713 11714 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 11715 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 11716 " \"ddeeefff\");", 11717 format("someFunction(\"aaabbbcccdddeeefff\");", 11718 getLLVMStyleWithColumns(25))); 11719 EXPECT_EQ("someFunction1234567890(\n" 11720 " \"aaabbbcccdddeeefff\");", 11721 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 11722 getLLVMStyleWithColumns(26))); 11723 EXPECT_EQ("someFunction1234567890(\n" 11724 " \"aaabbbcccdddeeeff\"\n" 11725 " \"f\");", 11726 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 11727 getLLVMStyleWithColumns(25))); 11728 EXPECT_EQ("someFunction1234567890(\n" 11729 " \"aaabbbcccdddeeeff\"\n" 11730 " \"f\");", 11731 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 11732 getLLVMStyleWithColumns(24))); 11733 EXPECT_EQ("someFunction(\n" 11734 " \"aaabbbcc ddde \"\n" 11735 " \"efff\");", 11736 format("someFunction(\"aaabbbcc ddde efff\");", 11737 getLLVMStyleWithColumns(25))); 11738 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 11739 " \"ddeeefff\");", 11740 format("someFunction(\"aaabbbccc ddeeefff\");", 11741 getLLVMStyleWithColumns(25))); 11742 EXPECT_EQ("someFunction1234567890(\n" 11743 " \"aaabb \"\n" 11744 " \"cccdddeeefff\");", 11745 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 11746 getLLVMStyleWithColumns(25))); 11747 EXPECT_EQ("#define A \\\n" 11748 " string s = \\\n" 11749 " \"123456789\" \\\n" 11750 " \"0\"; \\\n" 11751 " int i;", 11752 format("#define A string s = \"1234567890\"; int i;", 11753 getLLVMStyleWithColumns(20))); 11754 EXPECT_EQ("someFunction(\n" 11755 " \"aaabbbcc \"\n" 11756 " \"dddeeefff\");", 11757 format("someFunction(\"aaabbbcc dddeeefff\");", 11758 getLLVMStyleWithColumns(25))); 11759 } 11760 11761 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 11762 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 11763 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 11764 EXPECT_EQ("\"test\"\n" 11765 "\"\\n\"", 11766 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 11767 EXPECT_EQ("\"tes\\\\\"\n" 11768 "\"n\"", 11769 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 11770 EXPECT_EQ("\"\\\\\\\\\"\n" 11771 "\"\\n\"", 11772 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 11773 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 11774 EXPECT_EQ("\"\\uff01\"\n" 11775 "\"test\"", 11776 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 11777 EXPECT_EQ("\"\\Uff01ff02\"", 11778 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 11779 EXPECT_EQ("\"\\x000000000001\"\n" 11780 "\"next\"", 11781 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 11782 EXPECT_EQ("\"\\x000000000001next\"", 11783 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 11784 EXPECT_EQ("\"\\x000000000001\"", 11785 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 11786 EXPECT_EQ("\"test\"\n" 11787 "\"\\000000\"\n" 11788 "\"000001\"", 11789 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 11790 EXPECT_EQ("\"test\\000\"\n" 11791 "\"00000000\"\n" 11792 "\"1\"", 11793 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 11794 } 11795 11796 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 11797 verifyFormat("void f() {\n" 11798 " return g() {}\n" 11799 " void h() {}"); 11800 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 11801 "g();\n" 11802 "}"); 11803 } 11804 11805 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 11806 verifyFormat( 11807 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 11808 } 11809 11810 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 11811 verifyFormat("class X {\n" 11812 " void f() {\n" 11813 " }\n" 11814 "};", 11815 getLLVMStyleWithColumns(12)); 11816 } 11817 11818 TEST_F(FormatTest, ConfigurableIndentWidth) { 11819 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 11820 EightIndent.IndentWidth = 8; 11821 EightIndent.ContinuationIndentWidth = 8; 11822 verifyFormat("void f() {\n" 11823 " someFunction();\n" 11824 " if (true) {\n" 11825 " f();\n" 11826 " }\n" 11827 "}", 11828 EightIndent); 11829 verifyFormat("class X {\n" 11830 " void f() {\n" 11831 " }\n" 11832 "};", 11833 EightIndent); 11834 verifyFormat("int x[] = {\n" 11835 " call(),\n" 11836 " call()};", 11837 EightIndent); 11838 } 11839 11840 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 11841 verifyFormat("double\n" 11842 "f();", 11843 getLLVMStyleWithColumns(8)); 11844 } 11845 11846 TEST_F(FormatTest, ConfigurableUseOfTab) { 11847 FormatStyle Tab = getLLVMStyleWithColumns(42); 11848 Tab.IndentWidth = 8; 11849 Tab.UseTab = FormatStyle::UT_Always; 11850 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 11851 11852 EXPECT_EQ("if (aaaaaaaa && // q\n" 11853 " bb)\t\t// w\n" 11854 "\t;", 11855 format("if (aaaaaaaa &&// q\n" 11856 "bb)// w\n" 11857 ";", 11858 Tab)); 11859 EXPECT_EQ("if (aaa && bbb) // w\n" 11860 "\t;", 11861 format("if(aaa&&bbb)// w\n" 11862 ";", 11863 Tab)); 11864 11865 verifyFormat("class X {\n" 11866 "\tvoid f() {\n" 11867 "\t\tsomeFunction(parameter1,\n" 11868 "\t\t\t parameter2);\n" 11869 "\t}\n" 11870 "};", 11871 Tab); 11872 verifyFormat("#define A \\\n" 11873 "\tvoid f() { \\\n" 11874 "\t\tsomeFunction( \\\n" 11875 "\t\t parameter1, \\\n" 11876 "\t\t parameter2); \\\n" 11877 "\t}", 11878 Tab); 11879 verifyFormat("int a;\t // x\n" 11880 "int bbbbbbbb; // x\n", 11881 Tab); 11882 11883 Tab.TabWidth = 4; 11884 Tab.IndentWidth = 8; 11885 verifyFormat("class TabWidth4Indent8 {\n" 11886 "\t\tvoid f() {\n" 11887 "\t\t\t\tsomeFunction(parameter1,\n" 11888 "\t\t\t\t\t\t\t parameter2);\n" 11889 "\t\t}\n" 11890 "};", 11891 Tab); 11892 11893 Tab.TabWidth = 4; 11894 Tab.IndentWidth = 4; 11895 verifyFormat("class TabWidth4Indent4 {\n" 11896 "\tvoid f() {\n" 11897 "\t\tsomeFunction(parameter1,\n" 11898 "\t\t\t\t\t parameter2);\n" 11899 "\t}\n" 11900 "};", 11901 Tab); 11902 11903 Tab.TabWidth = 8; 11904 Tab.IndentWidth = 4; 11905 verifyFormat("class TabWidth8Indent4 {\n" 11906 " void f() {\n" 11907 "\tsomeFunction(parameter1,\n" 11908 "\t\t parameter2);\n" 11909 " }\n" 11910 "};", 11911 Tab); 11912 11913 Tab.TabWidth = 8; 11914 Tab.IndentWidth = 8; 11915 EXPECT_EQ("/*\n" 11916 "\t a\t\tcomment\n" 11917 "\t in multiple lines\n" 11918 " */", 11919 format(" /*\t \t \n" 11920 " \t \t a\t\tcomment\t \t\n" 11921 " \t \t in multiple lines\t\n" 11922 " \t */", 11923 Tab)); 11924 11925 Tab.UseTab = FormatStyle::UT_ForIndentation; 11926 verifyFormat("{\n" 11927 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 11928 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 11929 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 11930 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 11931 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 11932 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 11933 "};", 11934 Tab); 11935 verifyFormat("enum AA {\n" 11936 "\ta1, // Force multiple lines\n" 11937 "\ta2,\n" 11938 "\ta3\n" 11939 "};", 11940 Tab); 11941 EXPECT_EQ("if (aaaaaaaa && // q\n" 11942 " bb) // w\n" 11943 "\t;", 11944 format("if (aaaaaaaa &&// q\n" 11945 "bb)// w\n" 11946 ";", 11947 Tab)); 11948 verifyFormat("class X {\n" 11949 "\tvoid f() {\n" 11950 "\t\tsomeFunction(parameter1,\n" 11951 "\t\t parameter2);\n" 11952 "\t}\n" 11953 "};", 11954 Tab); 11955 verifyFormat("{\n" 11956 "\tQ(\n" 11957 "\t {\n" 11958 "\t\t int a;\n" 11959 "\t\t someFunction(aaaaaaaa,\n" 11960 "\t\t bbbbbbb);\n" 11961 "\t },\n" 11962 "\t p);\n" 11963 "}", 11964 Tab); 11965 EXPECT_EQ("{\n" 11966 "\t/* aaaa\n" 11967 "\t bbbb */\n" 11968 "}", 11969 format("{\n" 11970 "/* aaaa\n" 11971 " bbbb */\n" 11972 "}", 11973 Tab)); 11974 EXPECT_EQ("{\n" 11975 "\t/*\n" 11976 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11977 "\t bbbbbbbbbbbbb\n" 11978 "\t*/\n" 11979 "}", 11980 format("{\n" 11981 "/*\n" 11982 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 11983 "*/\n" 11984 "}", 11985 Tab)); 11986 EXPECT_EQ("{\n" 11987 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11988 "\t// bbbbbbbbbbbbb\n" 11989 "}", 11990 format("{\n" 11991 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 11992 "}", 11993 Tab)); 11994 EXPECT_EQ("{\n" 11995 "\t/*\n" 11996 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11997 "\t bbbbbbbbbbbbb\n" 11998 "\t*/\n" 11999 "}", 12000 format("{\n" 12001 "\t/*\n" 12002 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12003 "\t*/\n" 12004 "}", 12005 Tab)); 12006 EXPECT_EQ("{\n" 12007 "\t/*\n" 12008 "\n" 12009 "\t*/\n" 12010 "}", 12011 format("{\n" 12012 "\t/*\n" 12013 "\n" 12014 "\t*/\n" 12015 "}", 12016 Tab)); 12017 EXPECT_EQ("{\n" 12018 "\t/*\n" 12019 " asdf\n" 12020 "\t*/\n" 12021 "}", 12022 format("{\n" 12023 "\t/*\n" 12024 " asdf\n" 12025 "\t*/\n" 12026 "}", 12027 Tab)); 12028 12029 Tab.UseTab = FormatStyle::UT_Never; 12030 EXPECT_EQ("/*\n" 12031 " a\t\tcomment\n" 12032 " in multiple lines\n" 12033 " */", 12034 format(" /*\t \t \n" 12035 " \t \t a\t\tcomment\t \t\n" 12036 " \t \t in multiple lines\t\n" 12037 " \t */", 12038 Tab)); 12039 EXPECT_EQ("/* some\n" 12040 " comment */", 12041 format(" \t \t /* some\n" 12042 " \t \t comment */", 12043 Tab)); 12044 EXPECT_EQ("int a; /* some\n" 12045 " comment */", 12046 format(" \t \t int a; /* some\n" 12047 " \t \t comment */", 12048 Tab)); 12049 12050 EXPECT_EQ("int a; /* some\n" 12051 "comment */", 12052 format(" \t \t int\ta; /* some\n" 12053 " \t \t comment */", 12054 Tab)); 12055 EXPECT_EQ("f(\"\t\t\"); /* some\n" 12056 " comment */", 12057 format(" \t \t f(\"\t\t\"); /* some\n" 12058 " \t \t comment */", 12059 Tab)); 12060 EXPECT_EQ("{\n" 12061 " /*\n" 12062 " * Comment\n" 12063 " */\n" 12064 " int i;\n" 12065 "}", 12066 format("{\n" 12067 "\t/*\n" 12068 "\t * Comment\n" 12069 "\t */\n" 12070 "\t int i;\n" 12071 "}", 12072 Tab)); 12073 12074 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 12075 Tab.TabWidth = 8; 12076 Tab.IndentWidth = 8; 12077 EXPECT_EQ("if (aaaaaaaa && // q\n" 12078 " bb) // w\n" 12079 "\t;", 12080 format("if (aaaaaaaa &&// q\n" 12081 "bb)// w\n" 12082 ";", 12083 Tab)); 12084 EXPECT_EQ("if (aaa && bbb) // w\n" 12085 "\t;", 12086 format("if(aaa&&bbb)// w\n" 12087 ";", 12088 Tab)); 12089 verifyFormat("class X {\n" 12090 "\tvoid f() {\n" 12091 "\t\tsomeFunction(parameter1,\n" 12092 "\t\t\t parameter2);\n" 12093 "\t}\n" 12094 "};", 12095 Tab); 12096 verifyFormat("#define A \\\n" 12097 "\tvoid f() { \\\n" 12098 "\t\tsomeFunction( \\\n" 12099 "\t\t parameter1, \\\n" 12100 "\t\t parameter2); \\\n" 12101 "\t}", 12102 Tab); 12103 Tab.TabWidth = 4; 12104 Tab.IndentWidth = 8; 12105 verifyFormat("class TabWidth4Indent8 {\n" 12106 "\t\tvoid f() {\n" 12107 "\t\t\t\tsomeFunction(parameter1,\n" 12108 "\t\t\t\t\t\t\t parameter2);\n" 12109 "\t\t}\n" 12110 "};", 12111 Tab); 12112 Tab.TabWidth = 4; 12113 Tab.IndentWidth = 4; 12114 verifyFormat("class TabWidth4Indent4 {\n" 12115 "\tvoid f() {\n" 12116 "\t\tsomeFunction(parameter1,\n" 12117 "\t\t\t\t\t parameter2);\n" 12118 "\t}\n" 12119 "};", 12120 Tab); 12121 Tab.TabWidth = 8; 12122 Tab.IndentWidth = 4; 12123 verifyFormat("class TabWidth8Indent4 {\n" 12124 " void f() {\n" 12125 "\tsomeFunction(parameter1,\n" 12126 "\t\t parameter2);\n" 12127 " }\n" 12128 "};", 12129 Tab); 12130 Tab.TabWidth = 8; 12131 Tab.IndentWidth = 8; 12132 EXPECT_EQ("/*\n" 12133 "\t a\t\tcomment\n" 12134 "\t in multiple lines\n" 12135 " */", 12136 format(" /*\t \t \n" 12137 " \t \t a\t\tcomment\t \t\n" 12138 " \t \t in multiple lines\t\n" 12139 " \t */", 12140 Tab)); 12141 verifyFormat("{\n" 12142 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12143 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12144 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12145 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12146 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12147 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12148 "};", 12149 Tab); 12150 verifyFormat("enum AA {\n" 12151 "\ta1, // Force multiple lines\n" 12152 "\ta2,\n" 12153 "\ta3\n" 12154 "};", 12155 Tab); 12156 EXPECT_EQ("if (aaaaaaaa && // q\n" 12157 " bb) // w\n" 12158 "\t;", 12159 format("if (aaaaaaaa &&// q\n" 12160 "bb)// w\n" 12161 ";", 12162 Tab)); 12163 verifyFormat("class X {\n" 12164 "\tvoid f() {\n" 12165 "\t\tsomeFunction(parameter1,\n" 12166 "\t\t\t parameter2);\n" 12167 "\t}\n" 12168 "};", 12169 Tab); 12170 verifyFormat("{\n" 12171 "\tQ(\n" 12172 "\t {\n" 12173 "\t\t int a;\n" 12174 "\t\t someFunction(aaaaaaaa,\n" 12175 "\t\t\t\t bbbbbbb);\n" 12176 "\t },\n" 12177 "\t p);\n" 12178 "}", 12179 Tab); 12180 EXPECT_EQ("{\n" 12181 "\t/* aaaa\n" 12182 "\t bbbb */\n" 12183 "}", 12184 format("{\n" 12185 "/* aaaa\n" 12186 " bbbb */\n" 12187 "}", 12188 Tab)); 12189 EXPECT_EQ("{\n" 12190 "\t/*\n" 12191 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12192 "\t bbbbbbbbbbbbb\n" 12193 "\t*/\n" 12194 "}", 12195 format("{\n" 12196 "/*\n" 12197 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12198 "*/\n" 12199 "}", 12200 Tab)); 12201 EXPECT_EQ("{\n" 12202 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12203 "\t// bbbbbbbbbbbbb\n" 12204 "}", 12205 format("{\n" 12206 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12207 "}", 12208 Tab)); 12209 EXPECT_EQ("{\n" 12210 "\t/*\n" 12211 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12212 "\t bbbbbbbbbbbbb\n" 12213 "\t*/\n" 12214 "}", 12215 format("{\n" 12216 "\t/*\n" 12217 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12218 "\t*/\n" 12219 "}", 12220 Tab)); 12221 EXPECT_EQ("{\n" 12222 "\t/*\n" 12223 "\n" 12224 "\t*/\n" 12225 "}", 12226 format("{\n" 12227 "\t/*\n" 12228 "\n" 12229 "\t*/\n" 12230 "}", 12231 Tab)); 12232 EXPECT_EQ("{\n" 12233 "\t/*\n" 12234 " asdf\n" 12235 "\t*/\n" 12236 "}", 12237 format("{\n" 12238 "\t/*\n" 12239 " asdf\n" 12240 "\t*/\n" 12241 "}", 12242 Tab)); 12243 EXPECT_EQ("/* some\n" 12244 " comment */", 12245 format(" \t \t /* some\n" 12246 " \t \t comment */", 12247 Tab)); 12248 EXPECT_EQ("int a; /* some\n" 12249 " comment */", 12250 format(" \t \t int a; /* some\n" 12251 " \t \t comment */", 12252 Tab)); 12253 EXPECT_EQ("int a; /* some\n" 12254 "comment */", 12255 format(" \t \t int\ta; /* some\n" 12256 " \t \t comment */", 12257 Tab)); 12258 EXPECT_EQ("f(\"\t\t\"); /* some\n" 12259 " comment */", 12260 format(" \t \t f(\"\t\t\"); /* some\n" 12261 " \t \t comment */", 12262 Tab)); 12263 EXPECT_EQ("{\n" 12264 "\t/*\n" 12265 "\t * Comment\n" 12266 "\t */\n" 12267 "\tint i;\n" 12268 "}", 12269 format("{\n" 12270 "\t/*\n" 12271 "\t * Comment\n" 12272 "\t */\n" 12273 "\t int i;\n" 12274 "}", 12275 Tab)); 12276 Tab.TabWidth = 2; 12277 Tab.IndentWidth = 2; 12278 EXPECT_EQ("{\n" 12279 "\t/* aaaa\n" 12280 "\t\t bbbb */\n" 12281 "}", 12282 format("{\n" 12283 "/* aaaa\n" 12284 "\t bbbb */\n" 12285 "}", 12286 Tab)); 12287 EXPECT_EQ("{\n" 12288 "\t/*\n" 12289 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12290 "\t\tbbbbbbbbbbbbb\n" 12291 "\t*/\n" 12292 "}", 12293 format("{\n" 12294 "/*\n" 12295 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12296 "*/\n" 12297 "}", 12298 Tab)); 12299 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 12300 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 12301 Tab.TabWidth = 4; 12302 Tab.IndentWidth = 4; 12303 verifyFormat("class Assign {\n" 12304 "\tvoid f() {\n" 12305 "\t\tint x = 123;\n" 12306 "\t\tint random = 4;\n" 12307 "\t\tstd::string alphabet =\n" 12308 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 12309 "\t}\n" 12310 "};", 12311 Tab); 12312 12313 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 12314 Tab.TabWidth = 8; 12315 Tab.IndentWidth = 8; 12316 EXPECT_EQ("if (aaaaaaaa && // q\n" 12317 " bb) // w\n" 12318 "\t;", 12319 format("if (aaaaaaaa &&// q\n" 12320 "bb)// w\n" 12321 ";", 12322 Tab)); 12323 EXPECT_EQ("if (aaa && bbb) // w\n" 12324 "\t;", 12325 format("if(aaa&&bbb)// w\n" 12326 ";", 12327 Tab)); 12328 verifyFormat("class X {\n" 12329 "\tvoid f() {\n" 12330 "\t\tsomeFunction(parameter1,\n" 12331 "\t\t parameter2);\n" 12332 "\t}\n" 12333 "};", 12334 Tab); 12335 verifyFormat("#define A \\\n" 12336 "\tvoid f() { \\\n" 12337 "\t\tsomeFunction( \\\n" 12338 "\t\t parameter1, \\\n" 12339 "\t\t parameter2); \\\n" 12340 "\t}", 12341 Tab); 12342 Tab.TabWidth = 4; 12343 Tab.IndentWidth = 8; 12344 verifyFormat("class TabWidth4Indent8 {\n" 12345 "\t\tvoid f() {\n" 12346 "\t\t\t\tsomeFunction(parameter1,\n" 12347 "\t\t\t\t parameter2);\n" 12348 "\t\t}\n" 12349 "};", 12350 Tab); 12351 Tab.TabWidth = 4; 12352 Tab.IndentWidth = 4; 12353 verifyFormat("class TabWidth4Indent4 {\n" 12354 "\tvoid f() {\n" 12355 "\t\tsomeFunction(parameter1,\n" 12356 "\t\t parameter2);\n" 12357 "\t}\n" 12358 "};", 12359 Tab); 12360 Tab.TabWidth = 8; 12361 Tab.IndentWidth = 4; 12362 verifyFormat("class TabWidth8Indent4 {\n" 12363 " void f() {\n" 12364 "\tsomeFunction(parameter1,\n" 12365 "\t parameter2);\n" 12366 " }\n" 12367 "};", 12368 Tab); 12369 Tab.TabWidth = 8; 12370 Tab.IndentWidth = 8; 12371 EXPECT_EQ("/*\n" 12372 " a\t\tcomment\n" 12373 " in multiple lines\n" 12374 " */", 12375 format(" /*\t \t \n" 12376 " \t \t a\t\tcomment\t \t\n" 12377 " \t \t in multiple lines\t\n" 12378 " \t */", 12379 Tab)); 12380 verifyFormat("{\n" 12381 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12382 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12383 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12384 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12385 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12386 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12387 "};", 12388 Tab); 12389 verifyFormat("enum AA {\n" 12390 "\ta1, // Force multiple lines\n" 12391 "\ta2,\n" 12392 "\ta3\n" 12393 "};", 12394 Tab); 12395 EXPECT_EQ("if (aaaaaaaa && // q\n" 12396 " bb) // w\n" 12397 "\t;", 12398 format("if (aaaaaaaa &&// q\n" 12399 "bb)// w\n" 12400 ";", 12401 Tab)); 12402 verifyFormat("class X {\n" 12403 "\tvoid f() {\n" 12404 "\t\tsomeFunction(parameter1,\n" 12405 "\t\t parameter2);\n" 12406 "\t}\n" 12407 "};", 12408 Tab); 12409 verifyFormat("{\n" 12410 "\tQ(\n" 12411 "\t {\n" 12412 "\t\t int a;\n" 12413 "\t\t someFunction(aaaaaaaa,\n" 12414 "\t\t bbbbbbb);\n" 12415 "\t },\n" 12416 "\t p);\n" 12417 "}", 12418 Tab); 12419 EXPECT_EQ("{\n" 12420 "\t/* aaaa\n" 12421 "\t bbbb */\n" 12422 "}", 12423 format("{\n" 12424 "/* aaaa\n" 12425 " bbbb */\n" 12426 "}", 12427 Tab)); 12428 EXPECT_EQ("{\n" 12429 "\t/*\n" 12430 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12431 "\t bbbbbbbbbbbbb\n" 12432 "\t*/\n" 12433 "}", 12434 format("{\n" 12435 "/*\n" 12436 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12437 "*/\n" 12438 "}", 12439 Tab)); 12440 EXPECT_EQ("{\n" 12441 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12442 "\t// bbbbbbbbbbbbb\n" 12443 "}", 12444 format("{\n" 12445 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12446 "}", 12447 Tab)); 12448 EXPECT_EQ("{\n" 12449 "\t/*\n" 12450 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12451 "\t bbbbbbbbbbbbb\n" 12452 "\t*/\n" 12453 "}", 12454 format("{\n" 12455 "\t/*\n" 12456 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12457 "\t*/\n" 12458 "}", 12459 Tab)); 12460 EXPECT_EQ("{\n" 12461 "\t/*\n" 12462 "\n" 12463 "\t*/\n" 12464 "}", 12465 format("{\n" 12466 "\t/*\n" 12467 "\n" 12468 "\t*/\n" 12469 "}", 12470 Tab)); 12471 EXPECT_EQ("{\n" 12472 "\t/*\n" 12473 " asdf\n" 12474 "\t*/\n" 12475 "}", 12476 format("{\n" 12477 "\t/*\n" 12478 " asdf\n" 12479 "\t*/\n" 12480 "}", 12481 Tab)); 12482 EXPECT_EQ("/* some\n" 12483 " comment */", 12484 format(" \t \t /* some\n" 12485 " \t \t comment */", 12486 Tab)); 12487 EXPECT_EQ("int a; /* some\n" 12488 " comment */", 12489 format(" \t \t int a; /* some\n" 12490 " \t \t comment */", 12491 Tab)); 12492 EXPECT_EQ("int a; /* some\n" 12493 "comment */", 12494 format(" \t \t int\ta; /* some\n" 12495 " \t \t comment */", 12496 Tab)); 12497 EXPECT_EQ("f(\"\t\t\"); /* some\n" 12498 " comment */", 12499 format(" \t \t f(\"\t\t\"); /* some\n" 12500 " \t \t comment */", 12501 Tab)); 12502 EXPECT_EQ("{\n" 12503 "\t/*\n" 12504 "\t * Comment\n" 12505 "\t */\n" 12506 "\tint i;\n" 12507 "}", 12508 format("{\n" 12509 "\t/*\n" 12510 "\t * Comment\n" 12511 "\t */\n" 12512 "\t int i;\n" 12513 "}", 12514 Tab)); 12515 Tab.TabWidth = 2; 12516 Tab.IndentWidth = 2; 12517 EXPECT_EQ("{\n" 12518 "\t/* aaaa\n" 12519 "\t bbbb */\n" 12520 "}", 12521 format("{\n" 12522 "/* aaaa\n" 12523 " bbbb */\n" 12524 "}", 12525 Tab)); 12526 EXPECT_EQ("{\n" 12527 "\t/*\n" 12528 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12529 "\t bbbbbbbbbbbbb\n" 12530 "\t*/\n" 12531 "}", 12532 format("{\n" 12533 "/*\n" 12534 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12535 "*/\n" 12536 "}", 12537 Tab)); 12538 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 12539 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 12540 Tab.TabWidth = 4; 12541 Tab.IndentWidth = 4; 12542 verifyFormat("class Assign {\n" 12543 "\tvoid f() {\n" 12544 "\t\tint x = 123;\n" 12545 "\t\tint random = 4;\n" 12546 "\t\tstd::string alphabet =\n" 12547 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 12548 "\t}\n" 12549 "};", 12550 Tab); 12551 Tab.AlignOperands = FormatStyle::OAS_Align; 12552 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 12553 " cccccccccccccccccccc;", 12554 Tab); 12555 // no alignment 12556 verifyFormat("int aaaaaaaaaa =\n" 12557 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 12558 Tab); 12559 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 12560 " : bbbbbbbbbbbbbb ? 222222222222222\n" 12561 " : 333333333333333;", 12562 Tab); 12563 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 12564 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 12565 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 12566 " + cccccccccccccccccccc;", 12567 Tab); 12568 } 12569 12570 TEST_F(FormatTest, ZeroTabWidth) { 12571 FormatStyle Tab = getLLVMStyleWithColumns(42); 12572 Tab.IndentWidth = 8; 12573 Tab.UseTab = FormatStyle::UT_Never; 12574 Tab.TabWidth = 0; 12575 EXPECT_EQ("void a(){\n" 12576 " // line starts with '\t'\n" 12577 "};", 12578 format("void a(){\n" 12579 "\t// line starts with '\t'\n" 12580 "};", 12581 Tab)); 12582 12583 EXPECT_EQ("void a(){\n" 12584 " // line starts with '\t'\n" 12585 "};", 12586 format("void a(){\n" 12587 "\t\t// line starts with '\t'\n" 12588 "};", 12589 Tab)); 12590 12591 Tab.UseTab = FormatStyle::UT_ForIndentation; 12592 EXPECT_EQ("void a(){\n" 12593 " // line starts with '\t'\n" 12594 "};", 12595 format("void a(){\n" 12596 "\t// line starts with '\t'\n" 12597 "};", 12598 Tab)); 12599 12600 EXPECT_EQ("void a(){\n" 12601 " // line starts with '\t'\n" 12602 "};", 12603 format("void a(){\n" 12604 "\t\t// line starts with '\t'\n" 12605 "};", 12606 Tab)); 12607 12608 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 12609 EXPECT_EQ("void a(){\n" 12610 " // line starts with '\t'\n" 12611 "};", 12612 format("void a(){\n" 12613 "\t// line starts with '\t'\n" 12614 "};", 12615 Tab)); 12616 12617 EXPECT_EQ("void a(){\n" 12618 " // line starts with '\t'\n" 12619 "};", 12620 format("void a(){\n" 12621 "\t\t// line starts with '\t'\n" 12622 "};", 12623 Tab)); 12624 12625 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 12626 EXPECT_EQ("void a(){\n" 12627 " // line starts with '\t'\n" 12628 "};", 12629 format("void a(){\n" 12630 "\t// line starts with '\t'\n" 12631 "};", 12632 Tab)); 12633 12634 EXPECT_EQ("void a(){\n" 12635 " // line starts with '\t'\n" 12636 "};", 12637 format("void a(){\n" 12638 "\t\t// line starts with '\t'\n" 12639 "};", 12640 Tab)); 12641 12642 Tab.UseTab = FormatStyle::UT_Always; 12643 EXPECT_EQ("void a(){\n" 12644 "// line starts with '\t'\n" 12645 "};", 12646 format("void a(){\n" 12647 "\t// line starts with '\t'\n" 12648 "};", 12649 Tab)); 12650 12651 EXPECT_EQ("void a(){\n" 12652 "// line starts with '\t'\n" 12653 "};", 12654 format("void a(){\n" 12655 "\t\t// line starts with '\t'\n" 12656 "};", 12657 Tab)); 12658 } 12659 12660 TEST_F(FormatTest, CalculatesOriginalColumn) { 12661 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 12662 "q\"; /* some\n" 12663 " comment */", 12664 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 12665 "q\"; /* some\n" 12666 " comment */", 12667 getLLVMStyle())); 12668 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 12669 "/* some\n" 12670 " comment */", 12671 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 12672 " /* some\n" 12673 " comment */", 12674 getLLVMStyle())); 12675 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 12676 "qqq\n" 12677 "/* some\n" 12678 " comment */", 12679 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 12680 "qqq\n" 12681 " /* some\n" 12682 " comment */", 12683 getLLVMStyle())); 12684 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 12685 "wwww; /* some\n" 12686 " comment */", 12687 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 12688 "wwww; /* some\n" 12689 " comment */", 12690 getLLVMStyle())); 12691 } 12692 12693 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 12694 FormatStyle NoSpace = getLLVMStyle(); 12695 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 12696 12697 verifyFormat("while(true)\n" 12698 " continue;", 12699 NoSpace); 12700 verifyFormat("for(;;)\n" 12701 " continue;", 12702 NoSpace); 12703 verifyFormat("if(true)\n" 12704 " f();\n" 12705 "else if(true)\n" 12706 " f();", 12707 NoSpace); 12708 verifyFormat("do {\n" 12709 " do_something();\n" 12710 "} while(something());", 12711 NoSpace); 12712 verifyFormat("switch(x) {\n" 12713 "default:\n" 12714 " break;\n" 12715 "}", 12716 NoSpace); 12717 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 12718 verifyFormat("size_t x = sizeof(x);", NoSpace); 12719 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 12720 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 12721 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 12722 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 12723 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 12724 verifyFormat("alignas(128) char a[128];", NoSpace); 12725 verifyFormat("size_t x = alignof(MyType);", NoSpace); 12726 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 12727 verifyFormat("int f() throw(Deprecated);", NoSpace); 12728 verifyFormat("typedef void (*cb)(int);", NoSpace); 12729 verifyFormat("T A::operator()();", NoSpace); 12730 verifyFormat("X A::operator++(T);", NoSpace); 12731 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 12732 12733 FormatStyle Space = getLLVMStyle(); 12734 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 12735 12736 verifyFormat("int f ();", Space); 12737 verifyFormat("void f (int a, T b) {\n" 12738 " while (true)\n" 12739 " continue;\n" 12740 "}", 12741 Space); 12742 verifyFormat("if (true)\n" 12743 " f ();\n" 12744 "else if (true)\n" 12745 " f ();", 12746 Space); 12747 verifyFormat("do {\n" 12748 " do_something ();\n" 12749 "} while (something ());", 12750 Space); 12751 verifyFormat("switch (x) {\n" 12752 "default:\n" 12753 " break;\n" 12754 "}", 12755 Space); 12756 verifyFormat("A::A () : a (1) {}", Space); 12757 verifyFormat("void f () __attribute__ ((asdf));", Space); 12758 verifyFormat("*(&a + 1);\n" 12759 "&((&a)[1]);\n" 12760 "a[(b + c) * d];\n" 12761 "(((a + 1) * 2) + 3) * 4;", 12762 Space); 12763 verifyFormat("#define A(x) x", Space); 12764 verifyFormat("#define A (x) x", Space); 12765 verifyFormat("#if defined(x)\n" 12766 "#endif", 12767 Space); 12768 verifyFormat("auto i = std::make_unique<int> (5);", Space); 12769 verifyFormat("size_t x = sizeof (x);", Space); 12770 verifyFormat("auto f (int x) -> decltype (x);", Space); 12771 verifyFormat("auto f (int x) -> typeof (x);", Space); 12772 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 12773 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 12774 verifyFormat("int f (T x) noexcept (x.create ());", Space); 12775 verifyFormat("alignas (128) char a[128];", Space); 12776 verifyFormat("size_t x = alignof (MyType);", Space); 12777 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 12778 verifyFormat("int f () throw (Deprecated);", Space); 12779 verifyFormat("typedef void (*cb) (int);", Space); 12780 verifyFormat("T A::operator() ();", Space); 12781 verifyFormat("X A::operator++ (T);", Space); 12782 verifyFormat("auto lambda = [] () { return 0; };", Space); 12783 verifyFormat("int x = int (y);", Space); 12784 12785 FormatStyle SomeSpace = getLLVMStyle(); 12786 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 12787 12788 verifyFormat("[]() -> float {}", SomeSpace); 12789 verifyFormat("[] (auto foo) {}", SomeSpace); 12790 verifyFormat("[foo]() -> int {}", SomeSpace); 12791 verifyFormat("int f();", SomeSpace); 12792 verifyFormat("void f (int a, T b) {\n" 12793 " while (true)\n" 12794 " continue;\n" 12795 "}", 12796 SomeSpace); 12797 verifyFormat("if (true)\n" 12798 " f();\n" 12799 "else if (true)\n" 12800 " f();", 12801 SomeSpace); 12802 verifyFormat("do {\n" 12803 " do_something();\n" 12804 "} while (something());", 12805 SomeSpace); 12806 verifyFormat("switch (x) {\n" 12807 "default:\n" 12808 " break;\n" 12809 "}", 12810 SomeSpace); 12811 verifyFormat("A::A() : a (1) {}", SomeSpace); 12812 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 12813 verifyFormat("*(&a + 1);\n" 12814 "&((&a)[1]);\n" 12815 "a[(b + c) * d];\n" 12816 "(((a + 1) * 2) + 3) * 4;", 12817 SomeSpace); 12818 verifyFormat("#define A(x) x", SomeSpace); 12819 verifyFormat("#define A (x) x", SomeSpace); 12820 verifyFormat("#if defined(x)\n" 12821 "#endif", 12822 SomeSpace); 12823 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 12824 verifyFormat("size_t x = sizeof (x);", SomeSpace); 12825 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 12826 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 12827 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 12828 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 12829 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 12830 verifyFormat("alignas (128) char a[128];", SomeSpace); 12831 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 12832 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 12833 SomeSpace); 12834 verifyFormat("int f() throw (Deprecated);", SomeSpace); 12835 verifyFormat("typedef void (*cb) (int);", SomeSpace); 12836 verifyFormat("T A::operator()();", SomeSpace); 12837 verifyFormat("X A::operator++ (T);", SomeSpace); 12838 verifyFormat("int x = int (y);", SomeSpace); 12839 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 12840 } 12841 12842 TEST_F(FormatTest, SpaceAfterLogicalNot) { 12843 FormatStyle Spaces = getLLVMStyle(); 12844 Spaces.SpaceAfterLogicalNot = true; 12845 12846 verifyFormat("bool x = ! y", Spaces); 12847 verifyFormat("if (! isFailure())", Spaces); 12848 verifyFormat("if (! (a && b))", Spaces); 12849 verifyFormat("\"Error!\"", Spaces); 12850 verifyFormat("! ! x", Spaces); 12851 } 12852 12853 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 12854 FormatStyle Spaces = getLLVMStyle(); 12855 12856 Spaces.SpacesInParentheses = true; 12857 verifyFormat("do_something( ::globalVar );", Spaces); 12858 verifyFormat("call( x, y, z );", Spaces); 12859 verifyFormat("call();", Spaces); 12860 verifyFormat("std::function<void( int, int )> callback;", Spaces); 12861 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 12862 Spaces); 12863 verifyFormat("while ( (bool)1 )\n" 12864 " continue;", 12865 Spaces); 12866 verifyFormat("for ( ;; )\n" 12867 " continue;", 12868 Spaces); 12869 verifyFormat("if ( true )\n" 12870 " f();\n" 12871 "else if ( true )\n" 12872 " f();", 12873 Spaces); 12874 verifyFormat("do {\n" 12875 " do_something( (int)i );\n" 12876 "} while ( something() );", 12877 Spaces); 12878 verifyFormat("switch ( x ) {\n" 12879 "default:\n" 12880 " break;\n" 12881 "}", 12882 Spaces); 12883 12884 Spaces.SpacesInParentheses = false; 12885 Spaces.SpacesInCStyleCastParentheses = true; 12886 verifyFormat("Type *A = ( Type * )P;", Spaces); 12887 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 12888 verifyFormat("x = ( int32 )y;", Spaces); 12889 verifyFormat("int a = ( int )(2.0f);", Spaces); 12890 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 12891 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 12892 verifyFormat("#define x (( int )-1)", Spaces); 12893 12894 // Run the first set of tests again with: 12895 Spaces.SpacesInParentheses = false; 12896 Spaces.SpaceInEmptyParentheses = true; 12897 Spaces.SpacesInCStyleCastParentheses = true; 12898 verifyFormat("call(x, y, z);", Spaces); 12899 verifyFormat("call( );", Spaces); 12900 verifyFormat("std::function<void(int, int)> callback;", Spaces); 12901 verifyFormat("while (( bool )1)\n" 12902 " continue;", 12903 Spaces); 12904 verifyFormat("for (;;)\n" 12905 " continue;", 12906 Spaces); 12907 verifyFormat("if (true)\n" 12908 " f( );\n" 12909 "else if (true)\n" 12910 " f( );", 12911 Spaces); 12912 verifyFormat("do {\n" 12913 " do_something(( int )i);\n" 12914 "} while (something( ));", 12915 Spaces); 12916 verifyFormat("switch (x) {\n" 12917 "default:\n" 12918 " break;\n" 12919 "}", 12920 Spaces); 12921 12922 // Run the first set of tests again with: 12923 Spaces.SpaceAfterCStyleCast = true; 12924 verifyFormat("call(x, y, z);", Spaces); 12925 verifyFormat("call( );", Spaces); 12926 verifyFormat("std::function<void(int, int)> callback;", Spaces); 12927 verifyFormat("while (( bool ) 1)\n" 12928 " continue;", 12929 Spaces); 12930 verifyFormat("for (;;)\n" 12931 " continue;", 12932 Spaces); 12933 verifyFormat("if (true)\n" 12934 " f( );\n" 12935 "else if (true)\n" 12936 " f( );", 12937 Spaces); 12938 verifyFormat("do {\n" 12939 " do_something(( int ) i);\n" 12940 "} while (something( ));", 12941 Spaces); 12942 verifyFormat("switch (x) {\n" 12943 "default:\n" 12944 " break;\n" 12945 "}", 12946 Spaces); 12947 12948 // Run subset of tests again with: 12949 Spaces.SpacesInCStyleCastParentheses = false; 12950 Spaces.SpaceAfterCStyleCast = true; 12951 verifyFormat("while ((bool) 1)\n" 12952 " continue;", 12953 Spaces); 12954 verifyFormat("do {\n" 12955 " do_something((int) i);\n" 12956 "} while (something( ));", 12957 Spaces); 12958 12959 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 12960 verifyFormat("size_t idx = (size_t) a;", Spaces); 12961 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 12962 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 12963 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 12964 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 12965 Spaces.SpaceAfterCStyleCast = false; 12966 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 12967 verifyFormat("size_t idx = (size_t)a;", Spaces); 12968 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 12969 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 12970 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 12971 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 12972 } 12973 12974 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 12975 verifyFormat("int a[5];"); 12976 verifyFormat("a[3] += 42;"); 12977 12978 FormatStyle Spaces = getLLVMStyle(); 12979 Spaces.SpacesInSquareBrackets = true; 12980 // Not lambdas. 12981 verifyFormat("int a[ 5 ];", Spaces); 12982 verifyFormat("a[ 3 ] += 42;", Spaces); 12983 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 12984 verifyFormat("double &operator[](int i) { return 0; }\n" 12985 "int i;", 12986 Spaces); 12987 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 12988 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 12989 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 12990 // Lambdas. 12991 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 12992 verifyFormat("return [ i, args... ] {};", Spaces); 12993 verifyFormat("int foo = [ &bar ]() {};", Spaces); 12994 verifyFormat("int foo = [ = ]() {};", Spaces); 12995 verifyFormat("int foo = [ & ]() {};", Spaces); 12996 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 12997 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 12998 } 12999 13000 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 13001 FormatStyle NoSpaceStyle = getLLVMStyle(); 13002 verifyFormat("int a[5];", NoSpaceStyle); 13003 verifyFormat("a[3] += 42;", NoSpaceStyle); 13004 13005 verifyFormat("int a[1];", NoSpaceStyle); 13006 verifyFormat("int 1 [a];", NoSpaceStyle); 13007 verifyFormat("int a[1][2];", NoSpaceStyle); 13008 verifyFormat("a[7] = 5;", NoSpaceStyle); 13009 verifyFormat("int a = (f())[23];", NoSpaceStyle); 13010 verifyFormat("f([] {})", NoSpaceStyle); 13011 13012 FormatStyle Space = getLLVMStyle(); 13013 Space.SpaceBeforeSquareBrackets = true; 13014 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 13015 verifyFormat("return [i, args...] {};", Space); 13016 13017 verifyFormat("int a [5];", Space); 13018 verifyFormat("a [3] += 42;", Space); 13019 verifyFormat("constexpr char hello []{\"hello\"};", Space); 13020 verifyFormat("double &operator[](int i) { return 0; }\n" 13021 "int i;", 13022 Space); 13023 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 13024 verifyFormat("int i = a [a][a]->f();", Space); 13025 verifyFormat("int i = (*b) [a]->f();", Space); 13026 13027 verifyFormat("int a [1];", Space); 13028 verifyFormat("int 1 [a];", Space); 13029 verifyFormat("int a [1][2];", Space); 13030 verifyFormat("a [7] = 5;", Space); 13031 verifyFormat("int a = (f()) [23];", Space); 13032 verifyFormat("f([] {})", Space); 13033 } 13034 13035 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 13036 verifyFormat("int a = 5;"); 13037 verifyFormat("a += 42;"); 13038 verifyFormat("a or_eq 8;"); 13039 13040 FormatStyle Spaces = getLLVMStyle(); 13041 Spaces.SpaceBeforeAssignmentOperators = false; 13042 verifyFormat("int a= 5;", Spaces); 13043 verifyFormat("a+= 42;", Spaces); 13044 verifyFormat("a or_eq 8;", Spaces); 13045 } 13046 13047 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 13048 verifyFormat("class Foo : public Bar {};"); 13049 verifyFormat("Foo::Foo() : foo(1) {}"); 13050 verifyFormat("for (auto a : b) {\n}"); 13051 verifyFormat("int x = a ? b : c;"); 13052 verifyFormat("{\n" 13053 "label0:\n" 13054 " int x = 0;\n" 13055 "}"); 13056 verifyFormat("switch (x) {\n" 13057 "case 1:\n" 13058 "default:\n" 13059 "}"); 13060 verifyFormat("switch (allBraces) {\n" 13061 "case 1: {\n" 13062 " break;\n" 13063 "}\n" 13064 "case 2: {\n" 13065 " [[fallthrough]];\n" 13066 "}\n" 13067 "default: {\n" 13068 " break;\n" 13069 "}\n" 13070 "}"); 13071 13072 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 13073 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 13074 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 13075 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 13076 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 13077 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 13078 verifyFormat("{\n" 13079 "label1:\n" 13080 " int x = 0;\n" 13081 "}", 13082 CtorInitializerStyle); 13083 verifyFormat("switch (x) {\n" 13084 "case 1:\n" 13085 "default:\n" 13086 "}", 13087 CtorInitializerStyle); 13088 verifyFormat("switch (allBraces) {\n" 13089 "case 1: {\n" 13090 " break;\n" 13091 "}\n" 13092 "case 2: {\n" 13093 " [[fallthrough]];\n" 13094 "}\n" 13095 "default: {\n" 13096 " break;\n" 13097 "}\n" 13098 "}", 13099 CtorInitializerStyle); 13100 CtorInitializerStyle.BreakConstructorInitializers = 13101 FormatStyle::BCIS_AfterColon; 13102 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 13103 " aaaaaaaaaaaaaaaa(1),\n" 13104 " bbbbbbbbbbbbbbbb(2) {}", 13105 CtorInitializerStyle); 13106 CtorInitializerStyle.BreakConstructorInitializers = 13107 FormatStyle::BCIS_BeforeComma; 13108 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 13109 " : aaaaaaaaaaaaaaaa(1)\n" 13110 " , bbbbbbbbbbbbbbbb(2) {}", 13111 CtorInitializerStyle); 13112 CtorInitializerStyle.BreakConstructorInitializers = 13113 FormatStyle::BCIS_BeforeColon; 13114 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 13115 " : aaaaaaaaaaaaaaaa(1),\n" 13116 " bbbbbbbbbbbbbbbb(2) {}", 13117 CtorInitializerStyle); 13118 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 13119 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 13120 ": aaaaaaaaaaaaaaaa(1),\n" 13121 " bbbbbbbbbbbbbbbb(2) {}", 13122 CtorInitializerStyle); 13123 13124 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 13125 InheritanceStyle.SpaceBeforeInheritanceColon = false; 13126 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 13127 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 13128 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 13129 verifyFormat("int x = a ? b : c;", InheritanceStyle); 13130 verifyFormat("{\n" 13131 "label2:\n" 13132 " int x = 0;\n" 13133 "}", 13134 InheritanceStyle); 13135 verifyFormat("switch (x) {\n" 13136 "case 1:\n" 13137 "default:\n" 13138 "}", 13139 InheritanceStyle); 13140 verifyFormat("switch (allBraces) {\n" 13141 "case 1: {\n" 13142 " break;\n" 13143 "}\n" 13144 "case 2: {\n" 13145 " [[fallthrough]];\n" 13146 "}\n" 13147 "default: {\n" 13148 " break;\n" 13149 "}\n" 13150 "}", 13151 InheritanceStyle); 13152 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 13153 verifyFormat("class Foooooooooooooooooooooo:\n" 13154 " public aaaaaaaaaaaaaaaaaa,\n" 13155 " public bbbbbbbbbbbbbbbbbb {\n" 13156 "}", 13157 InheritanceStyle); 13158 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 13159 verifyFormat("class Foooooooooooooooooooooo\n" 13160 " : public aaaaaaaaaaaaaaaaaa\n" 13161 " , public bbbbbbbbbbbbbbbbbb {\n" 13162 "}", 13163 InheritanceStyle); 13164 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 13165 verifyFormat("class Foooooooooooooooooooooo\n" 13166 " : public aaaaaaaaaaaaaaaaaa,\n" 13167 " public bbbbbbbbbbbbbbbbbb {\n" 13168 "}", 13169 InheritanceStyle); 13170 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 13171 verifyFormat("class Foooooooooooooooooooooo\n" 13172 ": public aaaaaaaaaaaaaaaaaa,\n" 13173 " public bbbbbbbbbbbbbbbbbb {}", 13174 InheritanceStyle); 13175 13176 FormatStyle ForLoopStyle = getLLVMStyle(); 13177 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 13178 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 13179 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 13180 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 13181 verifyFormat("int x = a ? b : c;", ForLoopStyle); 13182 verifyFormat("{\n" 13183 "label2:\n" 13184 " int x = 0;\n" 13185 "}", 13186 ForLoopStyle); 13187 verifyFormat("switch (x) {\n" 13188 "case 1:\n" 13189 "default:\n" 13190 "}", 13191 ForLoopStyle); 13192 verifyFormat("switch (allBraces) {\n" 13193 "case 1: {\n" 13194 " break;\n" 13195 "}\n" 13196 "case 2: {\n" 13197 " [[fallthrough]];\n" 13198 "}\n" 13199 "default: {\n" 13200 " break;\n" 13201 "}\n" 13202 "}", 13203 ForLoopStyle); 13204 13205 FormatStyle CaseStyle = getLLVMStyle(); 13206 CaseStyle.SpaceBeforeCaseColon = true; 13207 verifyFormat("class Foo : public Bar {};", CaseStyle); 13208 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 13209 verifyFormat("for (auto a : b) {\n}", CaseStyle); 13210 verifyFormat("int x = a ? b : c;", CaseStyle); 13211 verifyFormat("switch (x) {\n" 13212 "case 1 :\n" 13213 "default :\n" 13214 "}", 13215 CaseStyle); 13216 verifyFormat("switch (allBraces) {\n" 13217 "case 1 : {\n" 13218 " break;\n" 13219 "}\n" 13220 "case 2 : {\n" 13221 " [[fallthrough]];\n" 13222 "}\n" 13223 "default : {\n" 13224 " break;\n" 13225 "}\n" 13226 "}", 13227 CaseStyle); 13228 13229 FormatStyle NoSpaceStyle = getLLVMStyle(); 13230 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 13231 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 13232 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 13233 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 13234 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 13235 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 13236 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 13237 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 13238 verifyFormat("{\n" 13239 "label3:\n" 13240 " int x = 0;\n" 13241 "}", 13242 NoSpaceStyle); 13243 verifyFormat("switch (x) {\n" 13244 "case 1:\n" 13245 "default:\n" 13246 "}", 13247 NoSpaceStyle); 13248 verifyFormat("switch (allBraces) {\n" 13249 "case 1: {\n" 13250 " break;\n" 13251 "}\n" 13252 "case 2: {\n" 13253 " [[fallthrough]];\n" 13254 "}\n" 13255 "default: {\n" 13256 " break;\n" 13257 "}\n" 13258 "}", 13259 NoSpaceStyle); 13260 13261 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 13262 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 13263 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 13264 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 13265 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 13266 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 13267 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 13268 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 13269 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 13270 verifyFormat("{\n" 13271 "label3:\n" 13272 " int x = 0;\n" 13273 "}", 13274 InvertedSpaceStyle); 13275 verifyFormat("switch (x) {\n" 13276 "case 1 :\n" 13277 "case 2 : {\n" 13278 " break;\n" 13279 "}\n" 13280 "default :\n" 13281 " break;\n" 13282 "}", 13283 InvertedSpaceStyle); 13284 verifyFormat("switch (allBraces) {\n" 13285 "case 1 : {\n" 13286 " break;\n" 13287 "}\n" 13288 "case 2 : {\n" 13289 " [[fallthrough]];\n" 13290 "}\n" 13291 "default : {\n" 13292 " break;\n" 13293 "}\n" 13294 "}", 13295 InvertedSpaceStyle); 13296 } 13297 13298 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 13299 FormatStyle Style = getLLVMStyle(); 13300 13301 Style.PointerAlignment = FormatStyle::PAS_Left; 13302 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 13303 verifyFormat("void* const* x = NULL;", Style); 13304 13305 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 13306 do { \ 13307 Style.PointerAlignment = FormatStyle::Pointers; \ 13308 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 13309 verifyFormat(Code, Style); \ 13310 } while (false) 13311 13312 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 13313 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 13314 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 13315 13316 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 13317 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 13318 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 13319 13320 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 13321 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 13322 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 13323 13324 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 13325 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 13326 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 13327 13328 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 13329 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 13330 SAPQ_Default); 13331 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 13332 SAPQ_Default); 13333 13334 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 13335 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 13336 SAPQ_Before); 13337 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 13338 SAPQ_Before); 13339 13340 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 13341 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 13342 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 13343 SAPQ_After); 13344 13345 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 13346 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 13347 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 13348 13349 #undef verifyQualifierSpaces 13350 13351 FormatStyle Spaces = getLLVMStyle(); 13352 Spaces.AttributeMacros.push_back("qualified"); 13353 Spaces.PointerAlignment = FormatStyle::PAS_Right; 13354 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 13355 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 13356 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 13357 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 13358 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 13359 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 13360 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 13361 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 13362 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 13363 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 13364 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 13365 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 13366 13367 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 13368 Spaces.PointerAlignment = FormatStyle::PAS_Left; 13369 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 13370 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 13371 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 13372 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 13373 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 13374 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 13375 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 13376 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 13377 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 13378 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 13379 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 13380 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 13381 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 13382 13383 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 13384 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 13385 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 13386 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 13387 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 13388 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 13389 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 13390 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 13391 } 13392 13393 TEST_F(FormatTest, AlignConsecutiveMacros) { 13394 FormatStyle Style = getLLVMStyle(); 13395 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13396 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13397 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 13398 13399 verifyFormat("#define a 3\n" 13400 "#define bbbb 4\n" 13401 "#define ccc (5)", 13402 Style); 13403 13404 verifyFormat("#define f(x) (x * x)\n" 13405 "#define fff(x, y, z) (x * y + z)\n" 13406 "#define ffff(x, y) (x - y)", 13407 Style); 13408 13409 verifyFormat("#define foo(x, y) (x + y)\n" 13410 "#define bar (5, 6)(2 + 2)", 13411 Style); 13412 13413 verifyFormat("#define a 3\n" 13414 "#define bbbb 4\n" 13415 "#define ccc (5)\n" 13416 "#define f(x) (x * x)\n" 13417 "#define fff(x, y, z) (x * y + z)\n" 13418 "#define ffff(x, y) (x - y)", 13419 Style); 13420 13421 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 13422 verifyFormat("#define a 3\n" 13423 "#define bbbb 4\n" 13424 "#define ccc (5)", 13425 Style); 13426 13427 verifyFormat("#define f(x) (x * x)\n" 13428 "#define fff(x, y, z) (x * y + z)\n" 13429 "#define ffff(x, y) (x - y)", 13430 Style); 13431 13432 verifyFormat("#define foo(x, y) (x + y)\n" 13433 "#define bar (5, 6)(2 + 2)", 13434 Style); 13435 13436 verifyFormat("#define a 3\n" 13437 "#define bbbb 4\n" 13438 "#define ccc (5)\n" 13439 "#define f(x) (x * x)\n" 13440 "#define fff(x, y, z) (x * y + z)\n" 13441 "#define ffff(x, y) (x - y)", 13442 Style); 13443 13444 verifyFormat("#define a 5\n" 13445 "#define foo(x, y) (x + y)\n" 13446 "#define CCC (6)\n" 13447 "auto lambda = []() {\n" 13448 " auto ii = 0;\n" 13449 " float j = 0;\n" 13450 " return 0;\n" 13451 "};\n" 13452 "int i = 0;\n" 13453 "float i2 = 0;\n" 13454 "auto v = type{\n" 13455 " i = 1, //\n" 13456 " (i = 2), //\n" 13457 " i = 3 //\n" 13458 "};", 13459 Style); 13460 13461 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 13462 Style.ColumnLimit = 20; 13463 13464 verifyFormat("#define a \\\n" 13465 " \"aabbbbbbbbbbbb\"\n" 13466 "#define D \\\n" 13467 " \"aabbbbbbbbbbbb\" \\\n" 13468 " \"ccddeeeeeeeee\"\n" 13469 "#define B \\\n" 13470 " \"QQQQQQQQQQQQQ\" \\\n" 13471 " \"FFFFFFFFFFFFF\" \\\n" 13472 " \"LLLLLLLL\"\n", 13473 Style); 13474 13475 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 13476 verifyFormat("#define a \\\n" 13477 " \"aabbbbbbbbbbbb\"\n" 13478 "#define D \\\n" 13479 " \"aabbbbbbbbbbbb\" \\\n" 13480 " \"ccddeeeeeeeee\"\n" 13481 "#define B \\\n" 13482 " \"QQQQQQQQQQQQQ\" \\\n" 13483 " \"FFFFFFFFFFFFF\" \\\n" 13484 " \"LLLLLLLL\"\n", 13485 Style); 13486 13487 // Test across comments 13488 Style.MaxEmptyLinesToKeep = 10; 13489 Style.ReflowComments = false; 13490 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 13491 EXPECT_EQ("#define a 3\n" 13492 "// line comment\n" 13493 "#define bbbb 4\n" 13494 "#define ccc (5)", 13495 format("#define a 3\n" 13496 "// line comment\n" 13497 "#define bbbb 4\n" 13498 "#define ccc (5)", 13499 Style)); 13500 13501 EXPECT_EQ("#define a 3\n" 13502 "/* block comment */\n" 13503 "#define bbbb 4\n" 13504 "#define ccc (5)", 13505 format("#define a 3\n" 13506 "/* block comment */\n" 13507 "#define bbbb 4\n" 13508 "#define ccc (5)", 13509 Style)); 13510 13511 EXPECT_EQ("#define a 3\n" 13512 "/* multi-line *\n" 13513 " * block comment */\n" 13514 "#define bbbb 4\n" 13515 "#define ccc (5)", 13516 format("#define a 3\n" 13517 "/* multi-line *\n" 13518 " * block comment */\n" 13519 "#define bbbb 4\n" 13520 "#define ccc (5)", 13521 Style)); 13522 13523 EXPECT_EQ("#define a 3\n" 13524 "// multi-line line comment\n" 13525 "//\n" 13526 "#define bbbb 4\n" 13527 "#define ccc (5)", 13528 format("#define a 3\n" 13529 "// multi-line line comment\n" 13530 "//\n" 13531 "#define bbbb 4\n" 13532 "#define ccc (5)", 13533 Style)); 13534 13535 EXPECT_EQ("#define a 3\n" 13536 "// empty lines still break.\n" 13537 "\n" 13538 "#define bbbb 4\n" 13539 "#define ccc (5)", 13540 format("#define a 3\n" 13541 "// empty lines still break.\n" 13542 "\n" 13543 "#define bbbb 4\n" 13544 "#define ccc (5)", 13545 Style)); 13546 13547 // Test across empty lines 13548 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 13549 EXPECT_EQ("#define a 3\n" 13550 "\n" 13551 "#define bbbb 4\n" 13552 "#define ccc (5)", 13553 format("#define a 3\n" 13554 "\n" 13555 "#define bbbb 4\n" 13556 "#define ccc (5)", 13557 Style)); 13558 13559 EXPECT_EQ("#define a 3\n" 13560 "\n" 13561 "\n" 13562 "\n" 13563 "#define bbbb 4\n" 13564 "#define ccc (5)", 13565 format("#define a 3\n" 13566 "\n" 13567 "\n" 13568 "\n" 13569 "#define bbbb 4\n" 13570 "#define ccc (5)", 13571 Style)); 13572 13573 EXPECT_EQ("#define a 3\n" 13574 "// comments should break alignment\n" 13575 "//\n" 13576 "#define bbbb 4\n" 13577 "#define ccc (5)", 13578 format("#define a 3\n" 13579 "// comments should break alignment\n" 13580 "//\n" 13581 "#define bbbb 4\n" 13582 "#define ccc (5)", 13583 Style)); 13584 13585 // Test across empty lines and comments 13586 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 13587 verifyFormat("#define a 3\n" 13588 "\n" 13589 "// line comment\n" 13590 "#define bbbb 4\n" 13591 "#define ccc (5)", 13592 Style); 13593 13594 EXPECT_EQ("#define a 3\n" 13595 "\n" 13596 "\n" 13597 "/* multi-line *\n" 13598 " * block comment */\n" 13599 "\n" 13600 "\n" 13601 "#define bbbb 4\n" 13602 "#define ccc (5)", 13603 format("#define a 3\n" 13604 "\n" 13605 "\n" 13606 "/* multi-line *\n" 13607 " * block comment */\n" 13608 "\n" 13609 "\n" 13610 "#define bbbb 4\n" 13611 "#define ccc (5)", 13612 Style)); 13613 13614 EXPECT_EQ("#define a 3\n" 13615 "\n" 13616 "\n" 13617 "/* multi-line *\n" 13618 " * block comment */\n" 13619 "\n" 13620 "\n" 13621 "#define bbbb 4\n" 13622 "#define ccc (5)", 13623 format("#define a 3\n" 13624 "\n" 13625 "\n" 13626 "/* multi-line *\n" 13627 " * block comment */\n" 13628 "\n" 13629 "\n" 13630 "#define bbbb 4\n" 13631 "#define ccc (5)", 13632 Style)); 13633 } 13634 13635 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 13636 FormatStyle Alignment = getLLVMStyle(); 13637 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 13638 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 13639 13640 Alignment.MaxEmptyLinesToKeep = 10; 13641 /* Test alignment across empty lines */ 13642 EXPECT_EQ("int a = 5;\n" 13643 "\n" 13644 "int oneTwoThree = 123;", 13645 format("int a = 5;\n" 13646 "\n" 13647 "int oneTwoThree= 123;", 13648 Alignment)); 13649 EXPECT_EQ("int a = 5;\n" 13650 "int one = 1;\n" 13651 "\n" 13652 "int oneTwoThree = 123;", 13653 format("int a = 5;\n" 13654 "int one = 1;\n" 13655 "\n" 13656 "int oneTwoThree = 123;", 13657 Alignment)); 13658 EXPECT_EQ("int a = 5;\n" 13659 "int one = 1;\n" 13660 "\n" 13661 "int oneTwoThree = 123;\n" 13662 "int oneTwo = 12;", 13663 format("int a = 5;\n" 13664 "int one = 1;\n" 13665 "\n" 13666 "int oneTwoThree = 123;\n" 13667 "int oneTwo = 12;", 13668 Alignment)); 13669 13670 /* Test across comments */ 13671 EXPECT_EQ("int a = 5;\n" 13672 "/* block comment */\n" 13673 "int oneTwoThree = 123;", 13674 format("int a = 5;\n" 13675 "/* block comment */\n" 13676 "int oneTwoThree=123;", 13677 Alignment)); 13678 13679 EXPECT_EQ("int a = 5;\n" 13680 "// line comment\n" 13681 "int oneTwoThree = 123;", 13682 format("int a = 5;\n" 13683 "// line comment\n" 13684 "int oneTwoThree=123;", 13685 Alignment)); 13686 13687 /* Test across comments and newlines */ 13688 EXPECT_EQ("int a = 5;\n" 13689 "\n" 13690 "/* block comment */\n" 13691 "int oneTwoThree = 123;", 13692 format("int a = 5;\n" 13693 "\n" 13694 "/* block comment */\n" 13695 "int oneTwoThree=123;", 13696 Alignment)); 13697 13698 EXPECT_EQ("int a = 5;\n" 13699 "\n" 13700 "// line comment\n" 13701 "int oneTwoThree = 123;", 13702 format("int a = 5;\n" 13703 "\n" 13704 "// line comment\n" 13705 "int oneTwoThree=123;", 13706 Alignment)); 13707 } 13708 13709 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 13710 FormatStyle Alignment = getLLVMStyle(); 13711 Alignment.AlignConsecutiveDeclarations = 13712 FormatStyle::ACS_AcrossEmptyLinesAndComments; 13713 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 13714 13715 Alignment.MaxEmptyLinesToKeep = 10; 13716 /* Test alignment across empty lines */ 13717 EXPECT_EQ("int a = 5;\n" 13718 "\n" 13719 "float const oneTwoThree = 123;", 13720 format("int a = 5;\n" 13721 "\n" 13722 "float const oneTwoThree = 123;", 13723 Alignment)); 13724 EXPECT_EQ("int a = 5;\n" 13725 "float const one = 1;\n" 13726 "\n" 13727 "int oneTwoThree = 123;", 13728 format("int a = 5;\n" 13729 "float const one = 1;\n" 13730 "\n" 13731 "int oneTwoThree = 123;", 13732 Alignment)); 13733 13734 /* Test across comments */ 13735 EXPECT_EQ("float const a = 5;\n" 13736 "/* block comment */\n" 13737 "int oneTwoThree = 123;", 13738 format("float const a = 5;\n" 13739 "/* block comment */\n" 13740 "int oneTwoThree=123;", 13741 Alignment)); 13742 13743 EXPECT_EQ("float const a = 5;\n" 13744 "// line comment\n" 13745 "int oneTwoThree = 123;", 13746 format("float const a = 5;\n" 13747 "// line comment\n" 13748 "int oneTwoThree=123;", 13749 Alignment)); 13750 13751 /* Test across comments and newlines */ 13752 EXPECT_EQ("float const a = 5;\n" 13753 "\n" 13754 "/* block comment */\n" 13755 "int oneTwoThree = 123;", 13756 format("float const a = 5;\n" 13757 "\n" 13758 "/* block comment */\n" 13759 "int oneTwoThree=123;", 13760 Alignment)); 13761 13762 EXPECT_EQ("float const a = 5;\n" 13763 "\n" 13764 "// line comment\n" 13765 "int oneTwoThree = 123;", 13766 format("float const a = 5;\n" 13767 "\n" 13768 "// line comment\n" 13769 "int oneTwoThree=123;", 13770 Alignment)); 13771 } 13772 13773 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 13774 FormatStyle Alignment = getLLVMStyle(); 13775 Alignment.AlignConsecutiveBitFields = 13776 FormatStyle::ACS_AcrossEmptyLinesAndComments; 13777 13778 Alignment.MaxEmptyLinesToKeep = 10; 13779 /* Test alignment across empty lines */ 13780 EXPECT_EQ("int a : 5;\n" 13781 "\n" 13782 "int longbitfield : 6;", 13783 format("int a : 5;\n" 13784 "\n" 13785 "int longbitfield : 6;", 13786 Alignment)); 13787 EXPECT_EQ("int a : 5;\n" 13788 "int one : 1;\n" 13789 "\n" 13790 "int longbitfield : 6;", 13791 format("int a : 5;\n" 13792 "int one : 1;\n" 13793 "\n" 13794 "int longbitfield : 6;", 13795 Alignment)); 13796 13797 /* Test across comments */ 13798 EXPECT_EQ("int a : 5;\n" 13799 "/* block comment */\n" 13800 "int longbitfield : 6;", 13801 format("int a : 5;\n" 13802 "/* block comment */\n" 13803 "int longbitfield : 6;", 13804 Alignment)); 13805 EXPECT_EQ("int a : 5;\n" 13806 "int one : 1;\n" 13807 "// line comment\n" 13808 "int longbitfield : 6;", 13809 format("int a : 5;\n" 13810 "int one : 1;\n" 13811 "// line comment\n" 13812 "int longbitfield : 6;", 13813 Alignment)); 13814 13815 /* Test across comments and newlines */ 13816 EXPECT_EQ("int a : 5;\n" 13817 "/* block comment */\n" 13818 "\n" 13819 "int longbitfield : 6;", 13820 format("int a : 5;\n" 13821 "/* block comment */\n" 13822 "\n" 13823 "int longbitfield : 6;", 13824 Alignment)); 13825 EXPECT_EQ("int a : 5;\n" 13826 "int one : 1;\n" 13827 "\n" 13828 "// line comment\n" 13829 "\n" 13830 "int longbitfield : 6;", 13831 format("int a : 5;\n" 13832 "int one : 1;\n" 13833 "\n" 13834 "// line comment \n" 13835 "\n" 13836 "int longbitfield : 6;", 13837 Alignment)); 13838 } 13839 13840 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 13841 FormatStyle Alignment = getLLVMStyle(); 13842 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 13843 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 13844 13845 Alignment.MaxEmptyLinesToKeep = 10; 13846 /* Test alignment across empty lines */ 13847 EXPECT_EQ("int a = 5;\n" 13848 "\n" 13849 "int oneTwoThree = 123;", 13850 format("int a = 5;\n" 13851 "\n" 13852 "int oneTwoThree= 123;", 13853 Alignment)); 13854 EXPECT_EQ("int a = 5;\n" 13855 "int one = 1;\n" 13856 "\n" 13857 "int oneTwoThree = 123;", 13858 format("int a = 5;\n" 13859 "int one = 1;\n" 13860 "\n" 13861 "int oneTwoThree = 123;", 13862 Alignment)); 13863 13864 /* Test across comments */ 13865 EXPECT_EQ("int a = 5;\n" 13866 "/* block comment */\n" 13867 "int oneTwoThree = 123;", 13868 format("int a = 5;\n" 13869 "/* block comment */\n" 13870 "int oneTwoThree=123;", 13871 Alignment)); 13872 13873 EXPECT_EQ("int a = 5;\n" 13874 "// line comment\n" 13875 "int oneTwoThree = 123;", 13876 format("int a = 5;\n" 13877 "// line comment\n" 13878 "int oneTwoThree=123;", 13879 Alignment)); 13880 13881 EXPECT_EQ("int a = 5;\n" 13882 "/*\n" 13883 " * multi-line block comment\n" 13884 " */\n" 13885 "int oneTwoThree = 123;", 13886 format("int a = 5;\n" 13887 "/*\n" 13888 " * multi-line block comment\n" 13889 " */\n" 13890 "int oneTwoThree=123;", 13891 Alignment)); 13892 13893 EXPECT_EQ("int a = 5;\n" 13894 "//\n" 13895 "// multi-line line comment\n" 13896 "//\n" 13897 "int oneTwoThree = 123;", 13898 format("int a = 5;\n" 13899 "//\n" 13900 "// multi-line line comment\n" 13901 "//\n" 13902 "int oneTwoThree=123;", 13903 Alignment)); 13904 13905 /* Test across comments and newlines */ 13906 EXPECT_EQ("int a = 5;\n" 13907 "\n" 13908 "/* block comment */\n" 13909 "int oneTwoThree = 123;", 13910 format("int a = 5;\n" 13911 "\n" 13912 "/* block comment */\n" 13913 "int oneTwoThree=123;", 13914 Alignment)); 13915 13916 EXPECT_EQ("int a = 5;\n" 13917 "\n" 13918 "// line comment\n" 13919 "int oneTwoThree = 123;", 13920 format("int a = 5;\n" 13921 "\n" 13922 "// line comment\n" 13923 "int oneTwoThree=123;", 13924 Alignment)); 13925 } 13926 13927 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 13928 FormatStyle Alignment = getLLVMStyle(); 13929 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 13930 Alignment.AlignConsecutiveAssignments = 13931 FormatStyle::ACS_AcrossEmptyLinesAndComments; 13932 verifyFormat("int a = 5;\n" 13933 "int oneTwoThree = 123;", 13934 Alignment); 13935 verifyFormat("int a = method();\n" 13936 "int oneTwoThree = 133;", 13937 Alignment); 13938 verifyFormat("a &= 5;\n" 13939 "bcd *= 5;\n" 13940 "ghtyf += 5;\n" 13941 "dvfvdb -= 5;\n" 13942 "a /= 5;\n" 13943 "vdsvsv %= 5;\n" 13944 "sfdbddfbdfbb ^= 5;\n" 13945 "dvsdsv |= 5;\n" 13946 "int dsvvdvsdvvv = 123;", 13947 Alignment); 13948 verifyFormat("int i = 1, j = 10;\n" 13949 "something = 2000;", 13950 Alignment); 13951 verifyFormat("something = 2000;\n" 13952 "int i = 1, j = 10;\n", 13953 Alignment); 13954 verifyFormat("something = 2000;\n" 13955 "another = 911;\n" 13956 "int i = 1, j = 10;\n" 13957 "oneMore = 1;\n" 13958 "i = 2;", 13959 Alignment); 13960 verifyFormat("int a = 5;\n" 13961 "int one = 1;\n" 13962 "method();\n" 13963 "int oneTwoThree = 123;\n" 13964 "int oneTwo = 12;", 13965 Alignment); 13966 verifyFormat("int oneTwoThree = 123;\n" 13967 "int oneTwo = 12;\n" 13968 "method();\n", 13969 Alignment); 13970 verifyFormat("int oneTwoThree = 123; // comment\n" 13971 "int oneTwo = 12; // comment", 13972 Alignment); 13973 13974 // Bug 25167 13975 /* Uncomment when fixed 13976 verifyFormat("#if A\n" 13977 "#else\n" 13978 "int aaaaaaaa = 12;\n" 13979 "#endif\n" 13980 "#if B\n" 13981 "#else\n" 13982 "int a = 12;\n" 13983 "#endif\n", 13984 Alignment); 13985 verifyFormat("enum foo {\n" 13986 "#if A\n" 13987 "#else\n" 13988 " aaaaaaaa = 12;\n" 13989 "#endif\n" 13990 "#if B\n" 13991 "#else\n" 13992 " a = 12;\n" 13993 "#endif\n" 13994 "};\n", 13995 Alignment); 13996 */ 13997 13998 Alignment.MaxEmptyLinesToKeep = 10; 13999 /* Test alignment across empty lines */ 14000 EXPECT_EQ("int a = 5;\n" 14001 "\n" 14002 "int oneTwoThree = 123;", 14003 format("int a = 5;\n" 14004 "\n" 14005 "int oneTwoThree= 123;", 14006 Alignment)); 14007 EXPECT_EQ("int a = 5;\n" 14008 "int one = 1;\n" 14009 "\n" 14010 "int oneTwoThree = 123;", 14011 format("int a = 5;\n" 14012 "int one = 1;\n" 14013 "\n" 14014 "int oneTwoThree = 123;", 14015 Alignment)); 14016 EXPECT_EQ("int a = 5;\n" 14017 "int one = 1;\n" 14018 "\n" 14019 "int oneTwoThree = 123;\n" 14020 "int oneTwo = 12;", 14021 format("int a = 5;\n" 14022 "int one = 1;\n" 14023 "\n" 14024 "int oneTwoThree = 123;\n" 14025 "int oneTwo = 12;", 14026 Alignment)); 14027 14028 /* Test across comments */ 14029 EXPECT_EQ("int a = 5;\n" 14030 "/* block comment */\n" 14031 "int oneTwoThree = 123;", 14032 format("int a = 5;\n" 14033 "/* block comment */\n" 14034 "int oneTwoThree=123;", 14035 Alignment)); 14036 14037 EXPECT_EQ("int a = 5;\n" 14038 "// line comment\n" 14039 "int oneTwoThree = 123;", 14040 format("int a = 5;\n" 14041 "// line comment\n" 14042 "int oneTwoThree=123;", 14043 Alignment)); 14044 14045 /* Test across comments and newlines */ 14046 EXPECT_EQ("int a = 5;\n" 14047 "\n" 14048 "/* block comment */\n" 14049 "int oneTwoThree = 123;", 14050 format("int a = 5;\n" 14051 "\n" 14052 "/* block comment */\n" 14053 "int oneTwoThree=123;", 14054 Alignment)); 14055 14056 EXPECT_EQ("int a = 5;\n" 14057 "\n" 14058 "// line comment\n" 14059 "int oneTwoThree = 123;", 14060 format("int a = 5;\n" 14061 "\n" 14062 "// line comment\n" 14063 "int oneTwoThree=123;", 14064 Alignment)); 14065 14066 EXPECT_EQ("int a = 5;\n" 14067 "//\n" 14068 "// multi-line line comment\n" 14069 "//\n" 14070 "int oneTwoThree = 123;", 14071 format("int a = 5;\n" 14072 "//\n" 14073 "// multi-line line comment\n" 14074 "//\n" 14075 "int oneTwoThree=123;", 14076 Alignment)); 14077 14078 EXPECT_EQ("int a = 5;\n" 14079 "/*\n" 14080 " * multi-line block comment\n" 14081 " */\n" 14082 "int oneTwoThree = 123;", 14083 format("int a = 5;\n" 14084 "/*\n" 14085 " * multi-line block comment\n" 14086 " */\n" 14087 "int oneTwoThree=123;", 14088 Alignment)); 14089 14090 EXPECT_EQ("int a = 5;\n" 14091 "\n" 14092 "/* block comment */\n" 14093 "\n" 14094 "\n" 14095 "\n" 14096 "int oneTwoThree = 123;", 14097 format("int a = 5;\n" 14098 "\n" 14099 "/* block comment */\n" 14100 "\n" 14101 "\n" 14102 "\n" 14103 "int oneTwoThree=123;", 14104 Alignment)); 14105 14106 EXPECT_EQ("int a = 5;\n" 14107 "\n" 14108 "// line comment\n" 14109 "\n" 14110 "\n" 14111 "\n" 14112 "int oneTwoThree = 123;", 14113 format("int a = 5;\n" 14114 "\n" 14115 "// line comment\n" 14116 "\n" 14117 "\n" 14118 "\n" 14119 "int oneTwoThree=123;", 14120 Alignment)); 14121 14122 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 14123 verifyFormat("#define A \\\n" 14124 " int aaaa = 12; \\\n" 14125 " int b = 23; \\\n" 14126 " int ccc = 234; \\\n" 14127 " int dddddddddd = 2345;", 14128 Alignment); 14129 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 14130 verifyFormat("#define A \\\n" 14131 " int aaaa = 12; \\\n" 14132 " int b = 23; \\\n" 14133 " int ccc = 234; \\\n" 14134 " int dddddddddd = 2345;", 14135 Alignment); 14136 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 14137 verifyFormat("#define A " 14138 " \\\n" 14139 " int aaaa = 12; " 14140 " \\\n" 14141 " int b = 23; " 14142 " \\\n" 14143 " int ccc = 234; " 14144 " \\\n" 14145 " int dddddddddd = 2345;", 14146 Alignment); 14147 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 14148 "k = 4, int l = 5,\n" 14149 " int m = 6) {\n" 14150 " int j = 10;\n" 14151 " otherThing = 1;\n" 14152 "}", 14153 Alignment); 14154 verifyFormat("void SomeFunction(int parameter = 0) {\n" 14155 " int i = 1;\n" 14156 " int j = 2;\n" 14157 " int big = 10000;\n" 14158 "}", 14159 Alignment); 14160 verifyFormat("class C {\n" 14161 "public:\n" 14162 " int i = 1;\n" 14163 " virtual void f() = 0;\n" 14164 "};", 14165 Alignment); 14166 verifyFormat("int i = 1;\n" 14167 "if (SomeType t = getSomething()) {\n" 14168 "}\n" 14169 "int j = 2;\n" 14170 "int big = 10000;", 14171 Alignment); 14172 verifyFormat("int j = 7;\n" 14173 "for (int k = 0; k < N; ++k) {\n" 14174 "}\n" 14175 "int j = 2;\n" 14176 "int big = 10000;\n" 14177 "}", 14178 Alignment); 14179 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14180 verifyFormat("int i = 1;\n" 14181 "LooooooooooongType loooooooooooooooooooooongVariable\n" 14182 " = someLooooooooooooooooongFunction();\n" 14183 "int j = 2;", 14184 Alignment); 14185 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 14186 verifyFormat("int i = 1;\n" 14187 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 14188 " someLooooooooooooooooongFunction();\n" 14189 "int j = 2;", 14190 Alignment); 14191 14192 verifyFormat("auto lambda = []() {\n" 14193 " auto i = 0;\n" 14194 " return 0;\n" 14195 "};\n" 14196 "int i = 0;\n" 14197 "auto v = type{\n" 14198 " i = 1, //\n" 14199 " (i = 2), //\n" 14200 " i = 3 //\n" 14201 "};", 14202 Alignment); 14203 14204 verifyFormat( 14205 "int i = 1;\n" 14206 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 14207 " loooooooooooooooooooooongParameterB);\n" 14208 "int j = 2;", 14209 Alignment); 14210 14211 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 14212 " typename B = very_long_type_name_1,\n" 14213 " typename T_2 = very_long_type_name_2>\n" 14214 "auto foo() {}\n", 14215 Alignment); 14216 verifyFormat("int a, b = 1;\n" 14217 "int c = 2;\n" 14218 "int dd = 3;\n", 14219 Alignment); 14220 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 14221 "float b[1][] = {{3.f}};\n", 14222 Alignment); 14223 verifyFormat("for (int i = 0; i < 1; i++)\n" 14224 " int x = 1;\n", 14225 Alignment); 14226 verifyFormat("for (i = 0; i < 1; i++)\n" 14227 " x = 1;\n" 14228 "y = 1;\n", 14229 Alignment); 14230 14231 Alignment.ReflowComments = true; 14232 Alignment.ColumnLimit = 50; 14233 EXPECT_EQ("int x = 0;\n" 14234 "int yy = 1; /// specificlennospace\n" 14235 "int zzz = 2;\n", 14236 format("int x = 0;\n" 14237 "int yy = 1; ///specificlennospace\n" 14238 "int zzz = 2;\n", 14239 Alignment)); 14240 } 14241 14242 TEST_F(FormatTest, AlignConsecutiveAssignments) { 14243 FormatStyle Alignment = getLLVMStyle(); 14244 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14245 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 14246 verifyFormat("int a = 5;\n" 14247 "int oneTwoThree = 123;", 14248 Alignment); 14249 verifyFormat("int a = 5;\n" 14250 "int oneTwoThree = 123;", 14251 Alignment); 14252 14253 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14254 verifyFormat("int a = 5;\n" 14255 "int oneTwoThree = 123;", 14256 Alignment); 14257 verifyFormat("int a = method();\n" 14258 "int oneTwoThree = 133;", 14259 Alignment); 14260 verifyFormat("a &= 5;\n" 14261 "bcd *= 5;\n" 14262 "ghtyf += 5;\n" 14263 "dvfvdb -= 5;\n" 14264 "a /= 5;\n" 14265 "vdsvsv %= 5;\n" 14266 "sfdbddfbdfbb ^= 5;\n" 14267 "dvsdsv |= 5;\n" 14268 "int dsvvdvsdvvv = 123;", 14269 Alignment); 14270 verifyFormat("int i = 1, j = 10;\n" 14271 "something = 2000;", 14272 Alignment); 14273 verifyFormat("something = 2000;\n" 14274 "int i = 1, j = 10;\n", 14275 Alignment); 14276 verifyFormat("something = 2000;\n" 14277 "another = 911;\n" 14278 "int i = 1, j = 10;\n" 14279 "oneMore = 1;\n" 14280 "i = 2;", 14281 Alignment); 14282 verifyFormat("int a = 5;\n" 14283 "int one = 1;\n" 14284 "method();\n" 14285 "int oneTwoThree = 123;\n" 14286 "int oneTwo = 12;", 14287 Alignment); 14288 verifyFormat("int oneTwoThree = 123;\n" 14289 "int oneTwo = 12;\n" 14290 "method();\n", 14291 Alignment); 14292 verifyFormat("int oneTwoThree = 123; // comment\n" 14293 "int oneTwo = 12; // comment", 14294 Alignment); 14295 14296 // Bug 25167 14297 /* Uncomment when fixed 14298 verifyFormat("#if A\n" 14299 "#else\n" 14300 "int aaaaaaaa = 12;\n" 14301 "#endif\n" 14302 "#if B\n" 14303 "#else\n" 14304 "int a = 12;\n" 14305 "#endif\n", 14306 Alignment); 14307 verifyFormat("enum foo {\n" 14308 "#if A\n" 14309 "#else\n" 14310 " aaaaaaaa = 12;\n" 14311 "#endif\n" 14312 "#if B\n" 14313 "#else\n" 14314 " a = 12;\n" 14315 "#endif\n" 14316 "};\n", 14317 Alignment); 14318 */ 14319 14320 EXPECT_EQ("int a = 5;\n" 14321 "\n" 14322 "int oneTwoThree = 123;", 14323 format("int a = 5;\n" 14324 "\n" 14325 "int oneTwoThree= 123;", 14326 Alignment)); 14327 EXPECT_EQ("int a = 5;\n" 14328 "int one = 1;\n" 14329 "\n" 14330 "int oneTwoThree = 123;", 14331 format("int a = 5;\n" 14332 "int one = 1;\n" 14333 "\n" 14334 "int oneTwoThree = 123;", 14335 Alignment)); 14336 EXPECT_EQ("int a = 5;\n" 14337 "int one = 1;\n" 14338 "\n" 14339 "int oneTwoThree = 123;\n" 14340 "int oneTwo = 12;", 14341 format("int a = 5;\n" 14342 "int one = 1;\n" 14343 "\n" 14344 "int oneTwoThree = 123;\n" 14345 "int oneTwo = 12;", 14346 Alignment)); 14347 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 14348 verifyFormat("#define A \\\n" 14349 " int aaaa = 12; \\\n" 14350 " int b = 23; \\\n" 14351 " int ccc = 234; \\\n" 14352 " int dddddddddd = 2345;", 14353 Alignment); 14354 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 14355 verifyFormat("#define A \\\n" 14356 " int aaaa = 12; \\\n" 14357 " int b = 23; \\\n" 14358 " int ccc = 234; \\\n" 14359 " int dddddddddd = 2345;", 14360 Alignment); 14361 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 14362 verifyFormat("#define A " 14363 " \\\n" 14364 " int aaaa = 12; " 14365 " \\\n" 14366 " int b = 23; " 14367 " \\\n" 14368 " int ccc = 234; " 14369 " \\\n" 14370 " int dddddddddd = 2345;", 14371 Alignment); 14372 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 14373 "k = 4, int l = 5,\n" 14374 " int m = 6) {\n" 14375 " int j = 10;\n" 14376 " otherThing = 1;\n" 14377 "}", 14378 Alignment); 14379 verifyFormat("void SomeFunction(int parameter = 0) {\n" 14380 " int i = 1;\n" 14381 " int j = 2;\n" 14382 " int big = 10000;\n" 14383 "}", 14384 Alignment); 14385 verifyFormat("class C {\n" 14386 "public:\n" 14387 " int i = 1;\n" 14388 " virtual void f() = 0;\n" 14389 "};", 14390 Alignment); 14391 verifyFormat("int i = 1;\n" 14392 "if (SomeType t = getSomething()) {\n" 14393 "}\n" 14394 "int j = 2;\n" 14395 "int big = 10000;", 14396 Alignment); 14397 verifyFormat("int j = 7;\n" 14398 "for (int k = 0; k < N; ++k) {\n" 14399 "}\n" 14400 "int j = 2;\n" 14401 "int big = 10000;\n" 14402 "}", 14403 Alignment); 14404 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14405 verifyFormat("int i = 1;\n" 14406 "LooooooooooongType loooooooooooooooooooooongVariable\n" 14407 " = someLooooooooooooooooongFunction();\n" 14408 "int j = 2;", 14409 Alignment); 14410 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 14411 verifyFormat("int i = 1;\n" 14412 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 14413 " someLooooooooooooooooongFunction();\n" 14414 "int j = 2;", 14415 Alignment); 14416 14417 verifyFormat("auto lambda = []() {\n" 14418 " auto i = 0;\n" 14419 " return 0;\n" 14420 "};\n" 14421 "int i = 0;\n" 14422 "auto v = type{\n" 14423 " i = 1, //\n" 14424 " (i = 2), //\n" 14425 " i = 3 //\n" 14426 "};", 14427 Alignment); 14428 14429 verifyFormat( 14430 "int i = 1;\n" 14431 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 14432 " loooooooooooooooooooooongParameterB);\n" 14433 "int j = 2;", 14434 Alignment); 14435 14436 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 14437 " typename B = very_long_type_name_1,\n" 14438 " typename T_2 = very_long_type_name_2>\n" 14439 "auto foo() {}\n", 14440 Alignment); 14441 verifyFormat("int a, b = 1;\n" 14442 "int c = 2;\n" 14443 "int dd = 3;\n", 14444 Alignment); 14445 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 14446 "float b[1][] = {{3.f}};\n", 14447 Alignment); 14448 verifyFormat("for (int i = 0; i < 1; i++)\n" 14449 " int x = 1;\n", 14450 Alignment); 14451 verifyFormat("for (i = 0; i < 1; i++)\n" 14452 " x = 1;\n" 14453 "y = 1;\n", 14454 Alignment); 14455 14456 Alignment.ReflowComments = true; 14457 Alignment.ColumnLimit = 50; 14458 EXPECT_EQ("int x = 0;\n" 14459 "int yy = 1; /// specificlennospace\n" 14460 "int zzz = 2;\n", 14461 format("int x = 0;\n" 14462 "int yy = 1; ///specificlennospace\n" 14463 "int zzz = 2;\n", 14464 Alignment)); 14465 } 14466 14467 TEST_F(FormatTest, AlignConsecutiveBitFields) { 14468 FormatStyle Alignment = getLLVMStyle(); 14469 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 14470 verifyFormat("int const a : 5;\n" 14471 "int oneTwoThree : 23;", 14472 Alignment); 14473 14474 // Initializers are allowed starting with c++2a 14475 verifyFormat("int const a : 5 = 1;\n" 14476 "int oneTwoThree : 23 = 0;", 14477 Alignment); 14478 14479 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14480 verifyFormat("int const a : 5;\n" 14481 "int oneTwoThree : 23;", 14482 Alignment); 14483 14484 verifyFormat("int const a : 5; // comment\n" 14485 "int oneTwoThree : 23; // comment", 14486 Alignment); 14487 14488 verifyFormat("int const a : 5 = 1;\n" 14489 "int oneTwoThree : 23 = 0;", 14490 Alignment); 14491 14492 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14493 verifyFormat("int const a : 5 = 1;\n" 14494 "int oneTwoThree : 23 = 0;", 14495 Alignment); 14496 verifyFormat("int const a : 5 = {1};\n" 14497 "int oneTwoThree : 23 = 0;", 14498 Alignment); 14499 14500 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 14501 verifyFormat("int const a :5;\n" 14502 "int oneTwoThree:23;", 14503 Alignment); 14504 14505 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 14506 verifyFormat("int const a :5;\n" 14507 "int oneTwoThree :23;", 14508 Alignment); 14509 14510 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 14511 verifyFormat("int const a : 5;\n" 14512 "int oneTwoThree: 23;", 14513 Alignment); 14514 14515 // Known limitations: ':' is only recognized as a bitfield colon when 14516 // followed by a number. 14517 /* 14518 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 14519 "int a : 5;", 14520 Alignment); 14521 */ 14522 } 14523 14524 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 14525 FormatStyle Alignment = getLLVMStyle(); 14526 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14527 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 14528 verifyFormat("float const a = 5;\n" 14529 "int oneTwoThree = 123;", 14530 Alignment); 14531 verifyFormat("int a = 5;\n" 14532 "float const oneTwoThree = 123;", 14533 Alignment); 14534 14535 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14536 verifyFormat("float const a = 5;\n" 14537 "int oneTwoThree = 123;", 14538 Alignment); 14539 verifyFormat("int a = method();\n" 14540 "float const oneTwoThree = 133;", 14541 Alignment); 14542 verifyFormat("int i = 1, j = 10;\n" 14543 "something = 2000;", 14544 Alignment); 14545 verifyFormat("something = 2000;\n" 14546 "int i = 1, j = 10;\n", 14547 Alignment); 14548 verifyFormat("float something = 2000;\n" 14549 "double another = 911;\n" 14550 "int i = 1, j = 10;\n" 14551 "const int *oneMore = 1;\n" 14552 "unsigned i = 2;", 14553 Alignment); 14554 verifyFormat("float a = 5;\n" 14555 "int one = 1;\n" 14556 "method();\n" 14557 "const double oneTwoThree = 123;\n" 14558 "const unsigned int oneTwo = 12;", 14559 Alignment); 14560 verifyFormat("int oneTwoThree{0}; // comment\n" 14561 "unsigned oneTwo; // comment", 14562 Alignment); 14563 verifyFormat("unsigned int * a;\n" 14564 "int * b;\n" 14565 "unsigned int Const *c;\n" 14566 "unsigned int const *d;\n" 14567 "unsigned int Const &e;\n" 14568 "unsigned int const &f;", 14569 Alignment); 14570 verifyFormat("Const unsigned int *c;\n" 14571 "const unsigned int *d;\n" 14572 "Const unsigned int &e;\n" 14573 "const unsigned int &f;\n" 14574 "const unsigned g;\n" 14575 "Const unsigned h;", 14576 Alignment); 14577 EXPECT_EQ("float const a = 5;\n" 14578 "\n" 14579 "int oneTwoThree = 123;", 14580 format("float const a = 5;\n" 14581 "\n" 14582 "int oneTwoThree= 123;", 14583 Alignment)); 14584 EXPECT_EQ("float a = 5;\n" 14585 "int one = 1;\n" 14586 "\n" 14587 "unsigned oneTwoThree = 123;", 14588 format("float a = 5;\n" 14589 "int one = 1;\n" 14590 "\n" 14591 "unsigned oneTwoThree = 123;", 14592 Alignment)); 14593 EXPECT_EQ("float a = 5;\n" 14594 "int one = 1;\n" 14595 "\n" 14596 "unsigned oneTwoThree = 123;\n" 14597 "int oneTwo = 12;", 14598 format("float a = 5;\n" 14599 "int one = 1;\n" 14600 "\n" 14601 "unsigned oneTwoThree = 123;\n" 14602 "int oneTwo = 12;", 14603 Alignment)); 14604 // Function prototype alignment 14605 verifyFormat("int a();\n" 14606 "double b();", 14607 Alignment); 14608 verifyFormat("int a(int x);\n" 14609 "double b();", 14610 Alignment); 14611 unsigned OldColumnLimit = Alignment.ColumnLimit; 14612 // We need to set ColumnLimit to zero, in order to stress nested alignments, 14613 // otherwise the function parameters will be re-flowed onto a single line. 14614 Alignment.ColumnLimit = 0; 14615 EXPECT_EQ("int a(int x,\n" 14616 " float y);\n" 14617 "double b(int x,\n" 14618 " double y);", 14619 format("int a(int x,\n" 14620 " float y);\n" 14621 "double b(int x,\n" 14622 " double y);", 14623 Alignment)); 14624 // This ensures that function parameters of function declarations are 14625 // correctly indented when their owning functions are indented. 14626 // The failure case here is for 'double y' to not be indented enough. 14627 EXPECT_EQ("double a(int x);\n" 14628 "int b(int y,\n" 14629 " double z);", 14630 format("double a(int x);\n" 14631 "int b(int y,\n" 14632 " double z);", 14633 Alignment)); 14634 // Set ColumnLimit low so that we induce wrapping immediately after 14635 // the function name and opening paren. 14636 Alignment.ColumnLimit = 13; 14637 verifyFormat("int function(\n" 14638 " int x,\n" 14639 " bool y);", 14640 Alignment); 14641 Alignment.ColumnLimit = OldColumnLimit; 14642 // Ensure function pointers don't screw up recursive alignment 14643 verifyFormat("int a(int x, void (*fp)(int y));\n" 14644 "double b();", 14645 Alignment); 14646 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14647 // Ensure recursive alignment is broken by function braces, so that the 14648 // "a = 1" does not align with subsequent assignments inside the function 14649 // body. 14650 verifyFormat("int func(int a = 1) {\n" 14651 " int b = 2;\n" 14652 " int cc = 3;\n" 14653 "}", 14654 Alignment); 14655 verifyFormat("float something = 2000;\n" 14656 "double another = 911;\n" 14657 "int i = 1, j = 10;\n" 14658 "const int *oneMore = 1;\n" 14659 "unsigned i = 2;", 14660 Alignment); 14661 verifyFormat("int oneTwoThree = {0}; // comment\n" 14662 "unsigned oneTwo = 0; // comment", 14663 Alignment); 14664 // Make sure that scope is correctly tracked, in the absence of braces 14665 verifyFormat("for (int i = 0; i < n; i++)\n" 14666 " j = i;\n" 14667 "double x = 1;\n", 14668 Alignment); 14669 verifyFormat("if (int i = 0)\n" 14670 " j = i;\n" 14671 "double x = 1;\n", 14672 Alignment); 14673 // Ensure operator[] and operator() are comprehended 14674 verifyFormat("struct test {\n" 14675 " long long int foo();\n" 14676 " int operator[](int a);\n" 14677 " double bar();\n" 14678 "};\n", 14679 Alignment); 14680 verifyFormat("struct test {\n" 14681 " long long int foo();\n" 14682 " int operator()(int a);\n" 14683 " double bar();\n" 14684 "};\n", 14685 Alignment); 14686 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 14687 " int const i = 1;\n" 14688 " int * j = 2;\n" 14689 " int big = 10000;\n" 14690 "\n" 14691 " unsigned oneTwoThree = 123;\n" 14692 " int oneTwo = 12;\n" 14693 " method();\n" 14694 " float k = 2;\n" 14695 " int ll = 10000;\n" 14696 "}", 14697 format("void SomeFunction(int parameter= 0) {\n" 14698 " int const i= 1;\n" 14699 " int *j=2;\n" 14700 " int big = 10000;\n" 14701 "\n" 14702 "unsigned oneTwoThree =123;\n" 14703 "int oneTwo = 12;\n" 14704 " method();\n" 14705 "float k= 2;\n" 14706 "int ll=10000;\n" 14707 "}", 14708 Alignment)); 14709 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 14710 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 14711 verifyFormat("#define A \\\n" 14712 " int aaaa = 12; \\\n" 14713 " float b = 23; \\\n" 14714 " const int ccc = 234; \\\n" 14715 " unsigned dddddddddd = 2345;", 14716 Alignment); 14717 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 14718 verifyFormat("#define A \\\n" 14719 " int aaaa = 12; \\\n" 14720 " float b = 23; \\\n" 14721 " const int ccc = 234; \\\n" 14722 " unsigned dddddddddd = 2345;", 14723 Alignment); 14724 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 14725 Alignment.ColumnLimit = 30; 14726 verifyFormat("#define A \\\n" 14727 " int aaaa = 12; \\\n" 14728 " float b = 23; \\\n" 14729 " const int ccc = 234; \\\n" 14730 " int dddddddddd = 2345;", 14731 Alignment); 14732 Alignment.ColumnLimit = 80; 14733 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 14734 "k = 4, int l = 5,\n" 14735 " int m = 6) {\n" 14736 " const int j = 10;\n" 14737 " otherThing = 1;\n" 14738 "}", 14739 Alignment); 14740 verifyFormat("void SomeFunction(int parameter = 0) {\n" 14741 " int const i = 1;\n" 14742 " int * j = 2;\n" 14743 " int big = 10000;\n" 14744 "}", 14745 Alignment); 14746 verifyFormat("class C {\n" 14747 "public:\n" 14748 " int i = 1;\n" 14749 " virtual void f() = 0;\n" 14750 "};", 14751 Alignment); 14752 verifyFormat("float i = 1;\n" 14753 "if (SomeType t = getSomething()) {\n" 14754 "}\n" 14755 "const unsigned j = 2;\n" 14756 "int big = 10000;", 14757 Alignment); 14758 verifyFormat("float j = 7;\n" 14759 "for (int k = 0; k < N; ++k) {\n" 14760 "}\n" 14761 "unsigned j = 2;\n" 14762 "int big = 10000;\n" 14763 "}", 14764 Alignment); 14765 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14766 verifyFormat("float i = 1;\n" 14767 "LooooooooooongType loooooooooooooooooooooongVariable\n" 14768 " = someLooooooooooooooooongFunction();\n" 14769 "int j = 2;", 14770 Alignment); 14771 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 14772 verifyFormat("int i = 1;\n" 14773 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 14774 " someLooooooooooooooooongFunction();\n" 14775 "int j = 2;", 14776 Alignment); 14777 14778 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14779 verifyFormat("auto lambda = []() {\n" 14780 " auto ii = 0;\n" 14781 " float j = 0;\n" 14782 " return 0;\n" 14783 "};\n" 14784 "int i = 0;\n" 14785 "float i2 = 0;\n" 14786 "auto v = type{\n" 14787 " i = 1, //\n" 14788 " (i = 2), //\n" 14789 " i = 3 //\n" 14790 "};", 14791 Alignment); 14792 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 14793 14794 verifyFormat( 14795 "int i = 1;\n" 14796 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 14797 " loooooooooooooooooooooongParameterB);\n" 14798 "int j = 2;", 14799 Alignment); 14800 14801 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 14802 // We expect declarations and assignments to align, as long as it doesn't 14803 // exceed the column limit, starting a new alignment sequence whenever it 14804 // happens. 14805 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14806 Alignment.ColumnLimit = 30; 14807 verifyFormat("float ii = 1;\n" 14808 "unsigned j = 2;\n" 14809 "int someVerylongVariable = 1;\n" 14810 "AnotherLongType ll = 123456;\n" 14811 "VeryVeryLongType k = 2;\n" 14812 "int myvar = 1;", 14813 Alignment); 14814 Alignment.ColumnLimit = 80; 14815 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 14816 14817 verifyFormat( 14818 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 14819 " typename LongType, typename B>\n" 14820 "auto foo() {}\n", 14821 Alignment); 14822 verifyFormat("float a, b = 1;\n" 14823 "int c = 2;\n" 14824 "int dd = 3;\n", 14825 Alignment); 14826 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 14827 "float b[1][] = {{3.f}};\n", 14828 Alignment); 14829 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14830 verifyFormat("float a, b = 1;\n" 14831 "int c = 2;\n" 14832 "int dd = 3;\n", 14833 Alignment); 14834 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 14835 "float b[1][] = {{3.f}};\n", 14836 Alignment); 14837 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 14838 14839 Alignment.ColumnLimit = 30; 14840 Alignment.BinPackParameters = false; 14841 verifyFormat("void foo(float a,\n" 14842 " float b,\n" 14843 " int c,\n" 14844 " uint32_t *d) {\n" 14845 " int * e = 0;\n" 14846 " float f = 0;\n" 14847 " double g = 0;\n" 14848 "}\n" 14849 "void bar(ino_t a,\n" 14850 " int b,\n" 14851 " uint32_t *c,\n" 14852 " bool d) {}\n", 14853 Alignment); 14854 Alignment.BinPackParameters = true; 14855 Alignment.ColumnLimit = 80; 14856 14857 // Bug 33507 14858 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 14859 verifyFormat( 14860 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 14861 " static const Version verVs2017;\n" 14862 " return true;\n" 14863 "});\n", 14864 Alignment); 14865 Alignment.PointerAlignment = FormatStyle::PAS_Right; 14866 14867 // See llvm.org/PR35641 14868 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14869 verifyFormat("int func() { //\n" 14870 " int b;\n" 14871 " unsigned c;\n" 14872 "}", 14873 Alignment); 14874 14875 // See PR37175 14876 FormatStyle Style = getMozillaStyle(); 14877 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14878 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 14879 "foo(int a);", 14880 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 14881 14882 Alignment.PointerAlignment = FormatStyle::PAS_Left; 14883 verifyFormat("unsigned int* a;\n" 14884 "int* b;\n" 14885 "unsigned int Const* c;\n" 14886 "unsigned int const* d;\n" 14887 "unsigned int Const& e;\n" 14888 "unsigned int const& f;", 14889 Alignment); 14890 verifyFormat("Const unsigned int* c;\n" 14891 "const unsigned int* d;\n" 14892 "Const unsigned int& e;\n" 14893 "const unsigned int& f;\n" 14894 "const unsigned g;\n" 14895 "Const unsigned h;", 14896 Alignment); 14897 14898 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 14899 verifyFormat("unsigned int * a;\n" 14900 "int * b;\n" 14901 "unsigned int Const * c;\n" 14902 "unsigned int const * d;\n" 14903 "unsigned int Const & e;\n" 14904 "unsigned int const & f;", 14905 Alignment); 14906 verifyFormat("Const unsigned int * c;\n" 14907 "const unsigned int * d;\n" 14908 "Const unsigned int & e;\n" 14909 "const unsigned int & f;\n" 14910 "const unsigned g;\n" 14911 "Const unsigned h;", 14912 Alignment); 14913 } 14914 14915 TEST_F(FormatTest, AlignWithLineBreaks) { 14916 auto Style = getLLVMStyleWithColumns(120); 14917 14918 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 14919 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 14920 verifyFormat("void foo() {\n" 14921 " int myVar = 5;\n" 14922 " double x = 3.14;\n" 14923 " auto str = \"Hello \"\n" 14924 " \"World\";\n" 14925 " auto s = \"Hello \"\n" 14926 " \"Again\";\n" 14927 "}", 14928 Style); 14929 14930 // clang-format off 14931 verifyFormat("void foo() {\n" 14932 " const int capacityBefore = Entries.capacity();\n" 14933 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 14934 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 14935 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 14936 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 14937 "}", 14938 Style); 14939 // clang-format on 14940 14941 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14942 verifyFormat("void foo() {\n" 14943 " int myVar = 5;\n" 14944 " double x = 3.14;\n" 14945 " auto str = \"Hello \"\n" 14946 " \"World\";\n" 14947 " auto s = \"Hello \"\n" 14948 " \"Again\";\n" 14949 "}", 14950 Style); 14951 14952 // clang-format off 14953 verifyFormat("void foo() {\n" 14954 " const int capacityBefore = Entries.capacity();\n" 14955 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 14956 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 14957 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 14958 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 14959 "}", 14960 Style); 14961 // clang-format on 14962 14963 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 14964 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14965 verifyFormat("void foo() {\n" 14966 " int myVar = 5;\n" 14967 " double x = 3.14;\n" 14968 " auto str = \"Hello \"\n" 14969 " \"World\";\n" 14970 " auto s = \"Hello \"\n" 14971 " \"Again\";\n" 14972 "}", 14973 Style); 14974 14975 // clang-format off 14976 verifyFormat("void foo() {\n" 14977 " const int capacityBefore = Entries.capacity();\n" 14978 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 14979 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 14980 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 14981 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 14982 "}", 14983 Style); 14984 // clang-format on 14985 14986 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14987 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14988 14989 verifyFormat("void foo() {\n" 14990 " int myVar = 5;\n" 14991 " double x = 3.14;\n" 14992 " auto str = \"Hello \"\n" 14993 " \"World\";\n" 14994 " auto s = \"Hello \"\n" 14995 " \"Again\";\n" 14996 "}", 14997 Style); 14998 14999 // clang-format off 15000 verifyFormat("void foo() {\n" 15001 " const int capacityBefore = Entries.capacity();\n" 15002 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15003 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15004 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15005 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15006 "}", 15007 Style); 15008 // clang-format on 15009 } 15010 15011 TEST_F(FormatTest, LinuxBraceBreaking) { 15012 FormatStyle LinuxBraceStyle = getLLVMStyle(); 15013 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 15014 verifyFormat("namespace a\n" 15015 "{\n" 15016 "class A\n" 15017 "{\n" 15018 " void f()\n" 15019 " {\n" 15020 " if (true) {\n" 15021 " a();\n" 15022 " b();\n" 15023 " } else {\n" 15024 " a();\n" 15025 " }\n" 15026 " }\n" 15027 " void g() { return; }\n" 15028 "};\n" 15029 "struct B {\n" 15030 " int x;\n" 15031 "};\n" 15032 "} // namespace a\n", 15033 LinuxBraceStyle); 15034 verifyFormat("enum X {\n" 15035 " Y = 0,\n" 15036 "}\n", 15037 LinuxBraceStyle); 15038 verifyFormat("struct S {\n" 15039 " int Type;\n" 15040 " union {\n" 15041 " int x;\n" 15042 " double y;\n" 15043 " } Value;\n" 15044 " class C\n" 15045 " {\n" 15046 " MyFavoriteType Value;\n" 15047 " } Class;\n" 15048 "}\n", 15049 LinuxBraceStyle); 15050 } 15051 15052 TEST_F(FormatTest, MozillaBraceBreaking) { 15053 FormatStyle MozillaBraceStyle = getLLVMStyle(); 15054 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 15055 MozillaBraceStyle.FixNamespaceComments = false; 15056 verifyFormat("namespace a {\n" 15057 "class A\n" 15058 "{\n" 15059 " void f()\n" 15060 " {\n" 15061 " if (true) {\n" 15062 " a();\n" 15063 " b();\n" 15064 " }\n" 15065 " }\n" 15066 " void g() { return; }\n" 15067 "};\n" 15068 "enum E\n" 15069 "{\n" 15070 " A,\n" 15071 " // foo\n" 15072 " B,\n" 15073 " C\n" 15074 "};\n" 15075 "struct B\n" 15076 "{\n" 15077 " int x;\n" 15078 "};\n" 15079 "}\n", 15080 MozillaBraceStyle); 15081 verifyFormat("struct S\n" 15082 "{\n" 15083 " int Type;\n" 15084 " union\n" 15085 " {\n" 15086 " int x;\n" 15087 " double y;\n" 15088 " } Value;\n" 15089 " class C\n" 15090 " {\n" 15091 " MyFavoriteType Value;\n" 15092 " } Class;\n" 15093 "}\n", 15094 MozillaBraceStyle); 15095 } 15096 15097 TEST_F(FormatTest, StroustrupBraceBreaking) { 15098 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 15099 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 15100 verifyFormat("namespace a {\n" 15101 "class A {\n" 15102 " void f()\n" 15103 " {\n" 15104 " if (true) {\n" 15105 " a();\n" 15106 " b();\n" 15107 " }\n" 15108 " }\n" 15109 " void g() { return; }\n" 15110 "};\n" 15111 "struct B {\n" 15112 " int x;\n" 15113 "};\n" 15114 "} // namespace a\n", 15115 StroustrupBraceStyle); 15116 15117 verifyFormat("void foo()\n" 15118 "{\n" 15119 " if (a) {\n" 15120 " a();\n" 15121 " }\n" 15122 " else {\n" 15123 " b();\n" 15124 " }\n" 15125 "}\n", 15126 StroustrupBraceStyle); 15127 15128 verifyFormat("#ifdef _DEBUG\n" 15129 "int foo(int i = 0)\n" 15130 "#else\n" 15131 "int foo(int i = 5)\n" 15132 "#endif\n" 15133 "{\n" 15134 " return i;\n" 15135 "}", 15136 StroustrupBraceStyle); 15137 15138 verifyFormat("void foo() {}\n" 15139 "void bar()\n" 15140 "#ifdef _DEBUG\n" 15141 "{\n" 15142 " foo();\n" 15143 "}\n" 15144 "#else\n" 15145 "{\n" 15146 "}\n" 15147 "#endif", 15148 StroustrupBraceStyle); 15149 15150 verifyFormat("void foobar() { int i = 5; }\n" 15151 "#ifdef _DEBUG\n" 15152 "void bar() {}\n" 15153 "#else\n" 15154 "void bar() { foobar(); }\n" 15155 "#endif", 15156 StroustrupBraceStyle); 15157 } 15158 15159 TEST_F(FormatTest, AllmanBraceBreaking) { 15160 FormatStyle AllmanBraceStyle = getLLVMStyle(); 15161 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 15162 15163 EXPECT_EQ("namespace a\n" 15164 "{\n" 15165 "void f();\n" 15166 "void g();\n" 15167 "} // namespace a\n", 15168 format("namespace a\n" 15169 "{\n" 15170 "void f();\n" 15171 "void g();\n" 15172 "}\n", 15173 AllmanBraceStyle)); 15174 15175 verifyFormat("namespace a\n" 15176 "{\n" 15177 "class A\n" 15178 "{\n" 15179 " void f()\n" 15180 " {\n" 15181 " if (true)\n" 15182 " {\n" 15183 " a();\n" 15184 " b();\n" 15185 " }\n" 15186 " }\n" 15187 " void g() { return; }\n" 15188 "};\n" 15189 "struct B\n" 15190 "{\n" 15191 " int x;\n" 15192 "};\n" 15193 "union C\n" 15194 "{\n" 15195 "};\n" 15196 "} // namespace a", 15197 AllmanBraceStyle); 15198 15199 verifyFormat("void f()\n" 15200 "{\n" 15201 " if (true)\n" 15202 " {\n" 15203 " a();\n" 15204 " }\n" 15205 " else if (false)\n" 15206 " {\n" 15207 " b();\n" 15208 " }\n" 15209 " else\n" 15210 " {\n" 15211 " c();\n" 15212 " }\n" 15213 "}\n", 15214 AllmanBraceStyle); 15215 15216 verifyFormat("void f()\n" 15217 "{\n" 15218 " for (int i = 0; i < 10; ++i)\n" 15219 " {\n" 15220 " a();\n" 15221 " }\n" 15222 " while (false)\n" 15223 " {\n" 15224 " b();\n" 15225 " }\n" 15226 " do\n" 15227 " {\n" 15228 " c();\n" 15229 " } while (false)\n" 15230 "}\n", 15231 AllmanBraceStyle); 15232 15233 verifyFormat("void f(int a)\n" 15234 "{\n" 15235 " switch (a)\n" 15236 " {\n" 15237 " case 0:\n" 15238 " break;\n" 15239 " case 1:\n" 15240 " {\n" 15241 " break;\n" 15242 " }\n" 15243 " case 2:\n" 15244 " {\n" 15245 " }\n" 15246 " break;\n" 15247 " default:\n" 15248 " break;\n" 15249 " }\n" 15250 "}\n", 15251 AllmanBraceStyle); 15252 15253 verifyFormat("enum X\n" 15254 "{\n" 15255 " Y = 0,\n" 15256 "}\n", 15257 AllmanBraceStyle); 15258 verifyFormat("enum X\n" 15259 "{\n" 15260 " Y = 0\n" 15261 "}\n", 15262 AllmanBraceStyle); 15263 15264 verifyFormat("@interface BSApplicationController ()\n" 15265 "{\n" 15266 "@private\n" 15267 " id _extraIvar;\n" 15268 "}\n" 15269 "@end\n", 15270 AllmanBraceStyle); 15271 15272 verifyFormat("#ifdef _DEBUG\n" 15273 "int foo(int i = 0)\n" 15274 "#else\n" 15275 "int foo(int i = 5)\n" 15276 "#endif\n" 15277 "{\n" 15278 " return i;\n" 15279 "}", 15280 AllmanBraceStyle); 15281 15282 verifyFormat("void foo() {}\n" 15283 "void bar()\n" 15284 "#ifdef _DEBUG\n" 15285 "{\n" 15286 " foo();\n" 15287 "}\n" 15288 "#else\n" 15289 "{\n" 15290 "}\n" 15291 "#endif", 15292 AllmanBraceStyle); 15293 15294 verifyFormat("void foobar() { int i = 5; }\n" 15295 "#ifdef _DEBUG\n" 15296 "void bar() {}\n" 15297 "#else\n" 15298 "void bar() { foobar(); }\n" 15299 "#endif", 15300 AllmanBraceStyle); 15301 15302 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 15303 FormatStyle::SLS_All); 15304 15305 verifyFormat("[](int i) { return i + 2; };\n" 15306 "[](int i, int j)\n" 15307 "{\n" 15308 " auto x = i + j;\n" 15309 " auto y = i * j;\n" 15310 " return x ^ y;\n" 15311 "};\n" 15312 "void foo()\n" 15313 "{\n" 15314 " auto shortLambda = [](int i) { return i + 2; };\n" 15315 " auto longLambda = [](int i, int j)\n" 15316 " {\n" 15317 " auto x = i + j;\n" 15318 " auto y = i * j;\n" 15319 " return x ^ y;\n" 15320 " };\n" 15321 "}", 15322 AllmanBraceStyle); 15323 15324 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 15325 15326 verifyFormat("[](int i)\n" 15327 "{\n" 15328 " return i + 2;\n" 15329 "};\n" 15330 "[](int i, int j)\n" 15331 "{\n" 15332 " auto x = i + j;\n" 15333 " auto y = i * j;\n" 15334 " return x ^ y;\n" 15335 "};\n" 15336 "void foo()\n" 15337 "{\n" 15338 " auto shortLambda = [](int i)\n" 15339 " {\n" 15340 " return i + 2;\n" 15341 " };\n" 15342 " auto longLambda = [](int i, int j)\n" 15343 " {\n" 15344 " auto x = i + j;\n" 15345 " auto y = i * j;\n" 15346 " return x ^ y;\n" 15347 " };\n" 15348 "}", 15349 AllmanBraceStyle); 15350 15351 // Reset 15352 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 15353 15354 // This shouldn't affect ObjC blocks.. 15355 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 15356 " // ...\n" 15357 " int i;\n" 15358 "}];", 15359 AllmanBraceStyle); 15360 verifyFormat("void (^block)(void) = ^{\n" 15361 " // ...\n" 15362 " int i;\n" 15363 "};", 15364 AllmanBraceStyle); 15365 // .. or dict literals. 15366 verifyFormat("void f()\n" 15367 "{\n" 15368 " // ...\n" 15369 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 15370 "}", 15371 AllmanBraceStyle); 15372 verifyFormat("void f()\n" 15373 "{\n" 15374 " // ...\n" 15375 " [object someMethod:@{a : @\"b\"}];\n" 15376 "}", 15377 AllmanBraceStyle); 15378 verifyFormat("int f()\n" 15379 "{ // comment\n" 15380 " return 42;\n" 15381 "}", 15382 AllmanBraceStyle); 15383 15384 AllmanBraceStyle.ColumnLimit = 19; 15385 verifyFormat("void f() { int i; }", AllmanBraceStyle); 15386 AllmanBraceStyle.ColumnLimit = 18; 15387 verifyFormat("void f()\n" 15388 "{\n" 15389 " int i;\n" 15390 "}", 15391 AllmanBraceStyle); 15392 AllmanBraceStyle.ColumnLimit = 80; 15393 15394 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 15395 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 15396 FormatStyle::SIS_WithoutElse; 15397 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 15398 verifyFormat("void f(bool b)\n" 15399 "{\n" 15400 " if (b)\n" 15401 " {\n" 15402 " return;\n" 15403 " }\n" 15404 "}\n", 15405 BreakBeforeBraceShortIfs); 15406 verifyFormat("void f(bool b)\n" 15407 "{\n" 15408 " if constexpr (b)\n" 15409 " {\n" 15410 " return;\n" 15411 " }\n" 15412 "}\n", 15413 BreakBeforeBraceShortIfs); 15414 verifyFormat("void f(bool b)\n" 15415 "{\n" 15416 " if CONSTEXPR (b)\n" 15417 " {\n" 15418 " return;\n" 15419 " }\n" 15420 "}\n", 15421 BreakBeforeBraceShortIfs); 15422 verifyFormat("void f(bool b)\n" 15423 "{\n" 15424 " if (b) return;\n" 15425 "}\n", 15426 BreakBeforeBraceShortIfs); 15427 verifyFormat("void f(bool b)\n" 15428 "{\n" 15429 " if constexpr (b) return;\n" 15430 "}\n", 15431 BreakBeforeBraceShortIfs); 15432 verifyFormat("void f(bool b)\n" 15433 "{\n" 15434 " if CONSTEXPR (b) return;\n" 15435 "}\n", 15436 BreakBeforeBraceShortIfs); 15437 verifyFormat("void f(bool b)\n" 15438 "{\n" 15439 " while (b)\n" 15440 " {\n" 15441 " return;\n" 15442 " }\n" 15443 "}\n", 15444 BreakBeforeBraceShortIfs); 15445 } 15446 15447 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 15448 FormatStyle WhitesmithsBraceStyle = getLLVMStyle(); 15449 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 15450 15451 // Make a few changes to the style for testing purposes 15452 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 15453 FormatStyle::SFS_Empty; 15454 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 15455 WhitesmithsBraceStyle.ColumnLimit = 0; 15456 15457 // FIXME: this test case can't decide whether there should be a blank line 15458 // after the ~D() line or not. It adds one if one doesn't exist in the test 15459 // and it removes the line if one exists. 15460 /* 15461 verifyFormat("class A;\n" 15462 "namespace B\n" 15463 " {\n" 15464 "class C;\n" 15465 "// Comment\n" 15466 "class D\n" 15467 " {\n" 15468 "public:\n" 15469 " D();\n" 15470 " ~D() {}\n" 15471 "private:\n" 15472 " enum E\n" 15473 " {\n" 15474 " F\n" 15475 " }\n" 15476 " };\n" 15477 " } // namespace B\n", 15478 WhitesmithsBraceStyle); 15479 */ 15480 15481 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 15482 verifyFormat("namespace a\n" 15483 " {\n" 15484 "class A\n" 15485 " {\n" 15486 " void f()\n" 15487 " {\n" 15488 " if (true)\n" 15489 " {\n" 15490 " a();\n" 15491 " b();\n" 15492 " }\n" 15493 " }\n" 15494 " void g()\n" 15495 " {\n" 15496 " return;\n" 15497 " }\n" 15498 " };\n" 15499 "struct B\n" 15500 " {\n" 15501 " int x;\n" 15502 " };\n" 15503 " } // namespace a", 15504 WhitesmithsBraceStyle); 15505 15506 verifyFormat("namespace a\n" 15507 " {\n" 15508 "namespace b\n" 15509 " {\n" 15510 "class A\n" 15511 " {\n" 15512 " void f()\n" 15513 " {\n" 15514 " if (true)\n" 15515 " {\n" 15516 " a();\n" 15517 " b();\n" 15518 " }\n" 15519 " }\n" 15520 " void g()\n" 15521 " {\n" 15522 " return;\n" 15523 " }\n" 15524 " };\n" 15525 "struct B\n" 15526 " {\n" 15527 " int x;\n" 15528 " };\n" 15529 " } // namespace b\n" 15530 " } // namespace a", 15531 WhitesmithsBraceStyle); 15532 15533 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 15534 verifyFormat("namespace a\n" 15535 " {\n" 15536 "namespace b\n" 15537 " {\n" 15538 " class A\n" 15539 " {\n" 15540 " void f()\n" 15541 " {\n" 15542 " if (true)\n" 15543 " {\n" 15544 " a();\n" 15545 " b();\n" 15546 " }\n" 15547 " }\n" 15548 " void g()\n" 15549 " {\n" 15550 " return;\n" 15551 " }\n" 15552 " };\n" 15553 " struct B\n" 15554 " {\n" 15555 " int x;\n" 15556 " };\n" 15557 " } // namespace b\n" 15558 " } // namespace a", 15559 WhitesmithsBraceStyle); 15560 15561 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 15562 verifyFormat("namespace a\n" 15563 " {\n" 15564 " namespace b\n" 15565 " {\n" 15566 " class A\n" 15567 " {\n" 15568 " void f()\n" 15569 " {\n" 15570 " if (true)\n" 15571 " {\n" 15572 " a();\n" 15573 " b();\n" 15574 " }\n" 15575 " }\n" 15576 " void g()\n" 15577 " {\n" 15578 " return;\n" 15579 " }\n" 15580 " };\n" 15581 " struct B\n" 15582 " {\n" 15583 " int x;\n" 15584 " };\n" 15585 " } // namespace b\n" 15586 " } // namespace a", 15587 WhitesmithsBraceStyle); 15588 15589 verifyFormat("void f()\n" 15590 " {\n" 15591 " if (true)\n" 15592 " {\n" 15593 " a();\n" 15594 " }\n" 15595 " else if (false)\n" 15596 " {\n" 15597 " b();\n" 15598 " }\n" 15599 " else\n" 15600 " {\n" 15601 " c();\n" 15602 " }\n" 15603 " }\n", 15604 WhitesmithsBraceStyle); 15605 15606 verifyFormat("void f()\n" 15607 " {\n" 15608 " for (int i = 0; i < 10; ++i)\n" 15609 " {\n" 15610 " a();\n" 15611 " }\n" 15612 " while (false)\n" 15613 " {\n" 15614 " b();\n" 15615 " }\n" 15616 " do\n" 15617 " {\n" 15618 " c();\n" 15619 " } while (false)\n" 15620 " }\n", 15621 WhitesmithsBraceStyle); 15622 15623 WhitesmithsBraceStyle.IndentCaseLabels = true; 15624 verifyFormat("void switchTest1(int a)\n" 15625 " {\n" 15626 " switch (a)\n" 15627 " {\n" 15628 " case 2:\n" 15629 " {\n" 15630 " }\n" 15631 " break;\n" 15632 " }\n" 15633 " }\n", 15634 WhitesmithsBraceStyle); 15635 15636 verifyFormat("void switchTest2(int a)\n" 15637 " {\n" 15638 " switch (a)\n" 15639 " {\n" 15640 " case 0:\n" 15641 " break;\n" 15642 " case 1:\n" 15643 " {\n" 15644 " break;\n" 15645 " }\n" 15646 " case 2:\n" 15647 " {\n" 15648 " }\n" 15649 " break;\n" 15650 " default:\n" 15651 " break;\n" 15652 " }\n" 15653 " }\n", 15654 WhitesmithsBraceStyle); 15655 15656 verifyFormat("void switchTest3(int a)\n" 15657 " {\n" 15658 " switch (a)\n" 15659 " {\n" 15660 " case 0:\n" 15661 " {\n" 15662 " foo(x);\n" 15663 " }\n" 15664 " break;\n" 15665 " default:\n" 15666 " {\n" 15667 " foo(1);\n" 15668 " }\n" 15669 " break;\n" 15670 " }\n" 15671 " }\n", 15672 WhitesmithsBraceStyle); 15673 15674 WhitesmithsBraceStyle.IndentCaseLabels = false; 15675 15676 verifyFormat("void switchTest4(int a)\n" 15677 " {\n" 15678 " switch (a)\n" 15679 " {\n" 15680 " case 2:\n" 15681 " {\n" 15682 " }\n" 15683 " break;\n" 15684 " }\n" 15685 " }\n", 15686 WhitesmithsBraceStyle); 15687 15688 verifyFormat("void switchTest5(int a)\n" 15689 " {\n" 15690 " switch (a)\n" 15691 " {\n" 15692 " case 0:\n" 15693 " break;\n" 15694 " case 1:\n" 15695 " {\n" 15696 " foo();\n" 15697 " break;\n" 15698 " }\n" 15699 " case 2:\n" 15700 " {\n" 15701 " }\n" 15702 " break;\n" 15703 " default:\n" 15704 " break;\n" 15705 " }\n" 15706 " }\n", 15707 WhitesmithsBraceStyle); 15708 15709 verifyFormat("void switchTest6(int a)\n" 15710 " {\n" 15711 " switch (a)\n" 15712 " {\n" 15713 " case 0:\n" 15714 " {\n" 15715 " foo(x);\n" 15716 " }\n" 15717 " break;\n" 15718 " default:\n" 15719 " {\n" 15720 " foo(1);\n" 15721 " }\n" 15722 " break;\n" 15723 " }\n" 15724 " }\n", 15725 WhitesmithsBraceStyle); 15726 15727 verifyFormat("enum X\n" 15728 " {\n" 15729 " Y = 0, // testing\n" 15730 " }\n", 15731 WhitesmithsBraceStyle); 15732 15733 verifyFormat("enum X\n" 15734 " {\n" 15735 " Y = 0\n" 15736 " }\n", 15737 WhitesmithsBraceStyle); 15738 verifyFormat("enum X\n" 15739 " {\n" 15740 " Y = 0,\n" 15741 " Z = 1\n" 15742 " };\n", 15743 WhitesmithsBraceStyle); 15744 15745 verifyFormat("@interface BSApplicationController ()\n" 15746 " {\n" 15747 "@private\n" 15748 " id _extraIvar;\n" 15749 " }\n" 15750 "@end\n", 15751 WhitesmithsBraceStyle); 15752 15753 verifyFormat("#ifdef _DEBUG\n" 15754 "int foo(int i = 0)\n" 15755 "#else\n" 15756 "int foo(int i = 5)\n" 15757 "#endif\n" 15758 " {\n" 15759 " return i;\n" 15760 " }", 15761 WhitesmithsBraceStyle); 15762 15763 verifyFormat("void foo() {}\n" 15764 "void bar()\n" 15765 "#ifdef _DEBUG\n" 15766 " {\n" 15767 " foo();\n" 15768 " }\n" 15769 "#else\n" 15770 " {\n" 15771 " }\n" 15772 "#endif", 15773 WhitesmithsBraceStyle); 15774 15775 verifyFormat("void foobar()\n" 15776 " {\n" 15777 " int i = 5;\n" 15778 " }\n" 15779 "#ifdef _DEBUG\n" 15780 "void bar()\n" 15781 " {\n" 15782 " }\n" 15783 "#else\n" 15784 "void bar()\n" 15785 " {\n" 15786 " foobar();\n" 15787 " }\n" 15788 "#endif", 15789 WhitesmithsBraceStyle); 15790 15791 // This shouldn't affect ObjC blocks.. 15792 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 15793 " // ...\n" 15794 " int i;\n" 15795 "}];", 15796 WhitesmithsBraceStyle); 15797 verifyFormat("void (^block)(void) = ^{\n" 15798 " // ...\n" 15799 " int i;\n" 15800 "};", 15801 WhitesmithsBraceStyle); 15802 // .. or dict literals. 15803 verifyFormat("void f()\n" 15804 " {\n" 15805 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 15806 " }", 15807 WhitesmithsBraceStyle); 15808 15809 verifyFormat("int f()\n" 15810 " { // comment\n" 15811 " return 42;\n" 15812 " }", 15813 WhitesmithsBraceStyle); 15814 15815 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 15816 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 15817 FormatStyle::SIS_Always; 15818 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 15819 verifyFormat("void f(bool b)\n" 15820 " {\n" 15821 " if (b)\n" 15822 " {\n" 15823 " return;\n" 15824 " }\n" 15825 " }\n", 15826 BreakBeforeBraceShortIfs); 15827 verifyFormat("void f(bool b)\n" 15828 " {\n" 15829 " if (b) return;\n" 15830 " }\n", 15831 BreakBeforeBraceShortIfs); 15832 verifyFormat("void f(bool b)\n" 15833 " {\n" 15834 " while (b)\n" 15835 " {\n" 15836 " return;\n" 15837 " }\n" 15838 " }\n", 15839 BreakBeforeBraceShortIfs); 15840 } 15841 15842 TEST_F(FormatTest, GNUBraceBreaking) { 15843 FormatStyle GNUBraceStyle = getLLVMStyle(); 15844 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 15845 verifyFormat("namespace a\n" 15846 "{\n" 15847 "class A\n" 15848 "{\n" 15849 " void f()\n" 15850 " {\n" 15851 " int a;\n" 15852 " {\n" 15853 " int b;\n" 15854 " }\n" 15855 " if (true)\n" 15856 " {\n" 15857 " a();\n" 15858 " b();\n" 15859 " }\n" 15860 " }\n" 15861 " void g() { return; }\n" 15862 "}\n" 15863 "} // namespace a", 15864 GNUBraceStyle); 15865 15866 verifyFormat("void f()\n" 15867 "{\n" 15868 " if (true)\n" 15869 " {\n" 15870 " a();\n" 15871 " }\n" 15872 " else if (false)\n" 15873 " {\n" 15874 " b();\n" 15875 " }\n" 15876 " else\n" 15877 " {\n" 15878 " c();\n" 15879 " }\n" 15880 "}\n", 15881 GNUBraceStyle); 15882 15883 verifyFormat("void f()\n" 15884 "{\n" 15885 " for (int i = 0; i < 10; ++i)\n" 15886 " {\n" 15887 " a();\n" 15888 " }\n" 15889 " while (false)\n" 15890 " {\n" 15891 " b();\n" 15892 " }\n" 15893 " do\n" 15894 " {\n" 15895 " c();\n" 15896 " }\n" 15897 " while (false);\n" 15898 "}\n", 15899 GNUBraceStyle); 15900 15901 verifyFormat("void f(int a)\n" 15902 "{\n" 15903 " switch (a)\n" 15904 " {\n" 15905 " case 0:\n" 15906 " break;\n" 15907 " case 1:\n" 15908 " {\n" 15909 " break;\n" 15910 " }\n" 15911 " case 2:\n" 15912 " {\n" 15913 " }\n" 15914 " break;\n" 15915 " default:\n" 15916 " break;\n" 15917 " }\n" 15918 "}\n", 15919 GNUBraceStyle); 15920 15921 verifyFormat("enum X\n" 15922 "{\n" 15923 " Y = 0,\n" 15924 "}\n", 15925 GNUBraceStyle); 15926 15927 verifyFormat("@interface BSApplicationController ()\n" 15928 "{\n" 15929 "@private\n" 15930 " id _extraIvar;\n" 15931 "}\n" 15932 "@end\n", 15933 GNUBraceStyle); 15934 15935 verifyFormat("#ifdef _DEBUG\n" 15936 "int foo(int i = 0)\n" 15937 "#else\n" 15938 "int foo(int i = 5)\n" 15939 "#endif\n" 15940 "{\n" 15941 " return i;\n" 15942 "}", 15943 GNUBraceStyle); 15944 15945 verifyFormat("void foo() {}\n" 15946 "void bar()\n" 15947 "#ifdef _DEBUG\n" 15948 "{\n" 15949 " foo();\n" 15950 "}\n" 15951 "#else\n" 15952 "{\n" 15953 "}\n" 15954 "#endif", 15955 GNUBraceStyle); 15956 15957 verifyFormat("void foobar() { int i = 5; }\n" 15958 "#ifdef _DEBUG\n" 15959 "void bar() {}\n" 15960 "#else\n" 15961 "void bar() { foobar(); }\n" 15962 "#endif", 15963 GNUBraceStyle); 15964 } 15965 15966 TEST_F(FormatTest, WebKitBraceBreaking) { 15967 FormatStyle WebKitBraceStyle = getLLVMStyle(); 15968 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 15969 WebKitBraceStyle.FixNamespaceComments = false; 15970 verifyFormat("namespace a {\n" 15971 "class A {\n" 15972 " void f()\n" 15973 " {\n" 15974 " if (true) {\n" 15975 " a();\n" 15976 " b();\n" 15977 " }\n" 15978 " }\n" 15979 " void g() { return; }\n" 15980 "};\n" 15981 "enum E {\n" 15982 " A,\n" 15983 " // foo\n" 15984 " B,\n" 15985 " C\n" 15986 "};\n" 15987 "struct B {\n" 15988 " int x;\n" 15989 "};\n" 15990 "}\n", 15991 WebKitBraceStyle); 15992 verifyFormat("struct S {\n" 15993 " int Type;\n" 15994 " union {\n" 15995 " int x;\n" 15996 " double y;\n" 15997 " } Value;\n" 15998 " class C {\n" 15999 " MyFavoriteType Value;\n" 16000 " } Class;\n" 16001 "};\n", 16002 WebKitBraceStyle); 16003 } 16004 16005 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 16006 verifyFormat("void f() {\n" 16007 " try {\n" 16008 " } catch (const Exception &e) {\n" 16009 " }\n" 16010 "}\n", 16011 getLLVMStyle()); 16012 } 16013 16014 TEST_F(FormatTest, UnderstandsPragmas) { 16015 verifyFormat("#pragma omp reduction(| : var)"); 16016 verifyFormat("#pragma omp reduction(+ : var)"); 16017 16018 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 16019 "(including parentheses).", 16020 format("#pragma mark Any non-hyphenated or hyphenated string " 16021 "(including parentheses).")); 16022 } 16023 16024 TEST_F(FormatTest, UnderstandPragmaOption) { 16025 verifyFormat("#pragma option -C -A"); 16026 16027 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 16028 } 16029 16030 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 16031 FormatStyle Style = getLLVMStyle(); 16032 Style.ColumnLimit = 20; 16033 16034 // See PR41213 16035 EXPECT_EQ("/*\n" 16036 " *\t9012345\n" 16037 " * /8901\n" 16038 " */", 16039 format("/*\n" 16040 " *\t9012345 /8901\n" 16041 " */", 16042 Style)); 16043 EXPECT_EQ("/*\n" 16044 " *345678\n" 16045 " *\t/8901\n" 16046 " */", 16047 format("/*\n" 16048 " *345678\t/8901\n" 16049 " */", 16050 Style)); 16051 16052 verifyFormat("int a; // the\n" 16053 " // comment", 16054 Style); 16055 EXPECT_EQ("int a; /* first line\n" 16056 " * second\n" 16057 " * line third\n" 16058 " * line\n" 16059 " */", 16060 format("int a; /* first line\n" 16061 " * second\n" 16062 " * line third\n" 16063 " * line\n" 16064 " */", 16065 Style)); 16066 EXPECT_EQ("int a; // first line\n" 16067 " // second\n" 16068 " // line third\n" 16069 " // line", 16070 format("int a; // first line\n" 16071 " // second line\n" 16072 " // third line", 16073 Style)); 16074 16075 Style.PenaltyExcessCharacter = 90; 16076 verifyFormat("int a; // the comment", Style); 16077 EXPECT_EQ("int a; // the comment\n" 16078 " // aaa", 16079 format("int a; // the comment aaa", Style)); 16080 EXPECT_EQ("int a; /* first line\n" 16081 " * second line\n" 16082 " * third line\n" 16083 " */", 16084 format("int a; /* first line\n" 16085 " * second line\n" 16086 " * third line\n" 16087 " */", 16088 Style)); 16089 EXPECT_EQ("int a; // first line\n" 16090 " // second line\n" 16091 " // third line", 16092 format("int a; // first line\n" 16093 " // second line\n" 16094 " // third line", 16095 Style)); 16096 // FIXME: Investigate why this is not getting the same layout as the test 16097 // above. 16098 EXPECT_EQ("int a; /* first line\n" 16099 " * second line\n" 16100 " * third line\n" 16101 " */", 16102 format("int a; /* first line second line third line" 16103 "\n*/", 16104 Style)); 16105 16106 EXPECT_EQ("// foo bar baz bazfoo\n" 16107 "// foo bar foo bar\n", 16108 format("// foo bar baz bazfoo\n" 16109 "// foo bar foo bar\n", 16110 Style)); 16111 EXPECT_EQ("// foo bar baz bazfoo\n" 16112 "// foo bar foo bar\n", 16113 format("// foo bar baz bazfoo\n" 16114 "// foo bar foo bar\n", 16115 Style)); 16116 16117 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 16118 // next one. 16119 EXPECT_EQ("// foo bar baz bazfoo\n" 16120 "// bar foo bar\n", 16121 format("// foo bar baz bazfoo bar\n" 16122 "// foo bar\n", 16123 Style)); 16124 16125 EXPECT_EQ("// foo bar baz bazfoo\n" 16126 "// foo bar baz bazfoo\n" 16127 "// bar foo bar\n", 16128 format("// foo bar baz bazfoo\n" 16129 "// foo bar baz bazfoo bar\n" 16130 "// foo bar\n", 16131 Style)); 16132 16133 EXPECT_EQ("// foo bar baz bazfoo\n" 16134 "// foo bar baz bazfoo\n" 16135 "// bar foo bar\n", 16136 format("// foo bar baz bazfoo\n" 16137 "// foo bar baz bazfoo bar\n" 16138 "// foo bar\n", 16139 Style)); 16140 16141 // Make sure we do not keep protruding characters if strict mode reflow is 16142 // cheaper than keeping protruding characters. 16143 Style.ColumnLimit = 21; 16144 EXPECT_EQ( 16145 "// foo foo foo foo\n" 16146 "// foo foo foo foo\n" 16147 "// foo foo foo foo\n", 16148 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 16149 16150 EXPECT_EQ("int a = /* long block\n" 16151 " comment */\n" 16152 " 42;", 16153 format("int a = /* long block comment */ 42;", Style)); 16154 } 16155 16156 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 16157 for (size_t i = 1; i < Styles.size(); ++i) \ 16158 EXPECT_EQ(Styles[0], Styles[i]) \ 16159 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 16160 16161 TEST_F(FormatTest, GetsPredefinedStyleByName) { 16162 SmallVector<FormatStyle, 3> Styles; 16163 Styles.resize(3); 16164 16165 Styles[0] = getLLVMStyle(); 16166 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 16167 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 16168 EXPECT_ALL_STYLES_EQUAL(Styles); 16169 16170 Styles[0] = getGoogleStyle(); 16171 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 16172 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 16173 EXPECT_ALL_STYLES_EQUAL(Styles); 16174 16175 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 16176 EXPECT_TRUE( 16177 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 16178 EXPECT_TRUE( 16179 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 16180 EXPECT_ALL_STYLES_EQUAL(Styles); 16181 16182 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 16183 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 16184 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 16185 EXPECT_ALL_STYLES_EQUAL(Styles); 16186 16187 Styles[0] = getMozillaStyle(); 16188 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 16189 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 16190 EXPECT_ALL_STYLES_EQUAL(Styles); 16191 16192 Styles[0] = getWebKitStyle(); 16193 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 16194 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 16195 EXPECT_ALL_STYLES_EQUAL(Styles); 16196 16197 Styles[0] = getGNUStyle(); 16198 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 16199 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 16200 EXPECT_ALL_STYLES_EQUAL(Styles); 16201 16202 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 16203 } 16204 16205 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 16206 SmallVector<FormatStyle, 8> Styles; 16207 Styles.resize(2); 16208 16209 Styles[0] = getGoogleStyle(); 16210 Styles[1] = getLLVMStyle(); 16211 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 16212 EXPECT_ALL_STYLES_EQUAL(Styles); 16213 16214 Styles.resize(5); 16215 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 16216 Styles[1] = getLLVMStyle(); 16217 Styles[1].Language = FormatStyle::LK_JavaScript; 16218 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 16219 16220 Styles[2] = getLLVMStyle(); 16221 Styles[2].Language = FormatStyle::LK_JavaScript; 16222 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 16223 "BasedOnStyle: Google", 16224 &Styles[2]) 16225 .value()); 16226 16227 Styles[3] = getLLVMStyle(); 16228 Styles[3].Language = FormatStyle::LK_JavaScript; 16229 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 16230 "Language: JavaScript", 16231 &Styles[3]) 16232 .value()); 16233 16234 Styles[4] = getLLVMStyle(); 16235 Styles[4].Language = FormatStyle::LK_JavaScript; 16236 EXPECT_EQ(0, parseConfiguration("---\n" 16237 "BasedOnStyle: LLVM\n" 16238 "IndentWidth: 123\n" 16239 "---\n" 16240 "BasedOnStyle: Google\n" 16241 "Language: JavaScript", 16242 &Styles[4]) 16243 .value()); 16244 EXPECT_ALL_STYLES_EQUAL(Styles); 16245 } 16246 16247 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 16248 Style.FIELD = false; \ 16249 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 16250 EXPECT_TRUE(Style.FIELD); \ 16251 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 16252 EXPECT_FALSE(Style.FIELD); 16253 16254 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 16255 16256 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 16257 Style.STRUCT.FIELD = false; \ 16258 EXPECT_EQ(0, \ 16259 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 16260 .value()); \ 16261 EXPECT_TRUE(Style.STRUCT.FIELD); \ 16262 EXPECT_EQ(0, \ 16263 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 16264 .value()); \ 16265 EXPECT_FALSE(Style.STRUCT.FIELD); 16266 16267 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 16268 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 16269 16270 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 16271 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 16272 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 16273 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 16274 16275 TEST_F(FormatTest, ParsesConfigurationBools) { 16276 FormatStyle Style = {}; 16277 Style.Language = FormatStyle::LK_Cpp; 16278 CHECK_PARSE_BOOL(AlignTrailingComments); 16279 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 16280 CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine); 16281 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 16282 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 16283 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 16284 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 16285 CHECK_PARSE_BOOL(BinPackArguments); 16286 CHECK_PARSE_BOOL(BinPackParameters); 16287 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 16288 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 16289 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 16290 CHECK_PARSE_BOOL(BreakStringLiterals); 16291 CHECK_PARSE_BOOL(CompactNamespaces); 16292 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 16293 CHECK_PARSE_BOOL(DeriveLineEnding); 16294 CHECK_PARSE_BOOL(DerivePointerAlignment); 16295 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 16296 CHECK_PARSE_BOOL(DisableFormat); 16297 CHECK_PARSE_BOOL(IndentAccessModifiers); 16298 CHECK_PARSE_BOOL(IndentCaseLabels); 16299 CHECK_PARSE_BOOL(IndentCaseBlocks); 16300 CHECK_PARSE_BOOL(IndentGotoLabels); 16301 CHECK_PARSE_BOOL(IndentRequires); 16302 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 16303 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 16304 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 16305 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 16306 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 16307 CHECK_PARSE_BOOL(ReflowComments); 16308 CHECK_PARSE_BOOL(SortUsingDeclarations); 16309 CHECK_PARSE_BOOL(SpacesInParentheses); 16310 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 16311 CHECK_PARSE_BOOL(SpacesInAngles); 16312 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 16313 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 16314 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 16315 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 16316 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 16317 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 16318 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 16319 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 16320 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 16321 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 16322 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 16323 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 16324 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 16325 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 16326 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 16327 CHECK_PARSE_BOOL(UseCRLF); 16328 16329 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 16330 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 16331 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 16332 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 16333 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 16334 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 16335 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 16336 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 16337 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 16338 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 16339 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 16340 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 16341 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 16342 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 16343 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 16344 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 16345 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 16346 } 16347 16348 #undef CHECK_PARSE_BOOL 16349 16350 TEST_F(FormatTest, ParsesConfiguration) { 16351 FormatStyle Style = {}; 16352 Style.Language = FormatStyle::LK_Cpp; 16353 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 16354 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 16355 ConstructorInitializerIndentWidth, 1234u); 16356 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 16357 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 16358 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 16359 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 16360 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 16361 PenaltyBreakBeforeFirstCallParameter, 1234u); 16362 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 16363 PenaltyBreakTemplateDeclaration, 1234u); 16364 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 16365 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 16366 PenaltyReturnTypeOnItsOwnLine, 1234u); 16367 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 16368 SpacesBeforeTrailingComments, 1234u); 16369 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 16370 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 16371 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 16372 16373 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16374 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 16375 FormatStyle::ACS_None); 16376 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 16377 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 16378 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 16379 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 16380 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 16381 AlignConsecutiveAssignments, 16382 FormatStyle::ACS_AcrossEmptyLinesAndComments); 16383 // For backwards compability, false / true should still parse 16384 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 16385 FormatStyle::ACS_None); 16386 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 16387 FormatStyle::ACS_Consecutive); 16388 16389 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 16390 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 16391 FormatStyle::ACS_None); 16392 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 16393 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 16394 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 16395 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 16396 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 16397 AlignConsecutiveBitFields, 16398 FormatStyle::ACS_AcrossEmptyLinesAndComments); 16399 // For backwards compability, false / true should still parse 16400 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 16401 FormatStyle::ACS_None); 16402 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 16403 FormatStyle::ACS_Consecutive); 16404 16405 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16406 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 16407 FormatStyle::ACS_None); 16408 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 16409 FormatStyle::ACS_Consecutive); 16410 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 16411 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 16412 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 16413 AlignConsecutiveMacros, 16414 FormatStyle::ACS_AcrossEmptyLinesAndComments); 16415 // For backwards compability, false / true should still parse 16416 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 16417 FormatStyle::ACS_None); 16418 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 16419 FormatStyle::ACS_Consecutive); 16420 16421 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16422 CHECK_PARSE("AlignConsecutiveDeclarations: None", 16423 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 16424 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 16425 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 16426 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 16427 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 16428 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 16429 AlignConsecutiveDeclarations, 16430 FormatStyle::ACS_AcrossEmptyLinesAndComments); 16431 // For backwards compability, false / true should still parse 16432 CHECK_PARSE("AlignConsecutiveDeclarations: false", 16433 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 16434 CHECK_PARSE("AlignConsecutiveDeclarations: true", 16435 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 16436 16437 Style.PointerAlignment = FormatStyle::PAS_Middle; 16438 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 16439 FormatStyle::PAS_Left); 16440 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 16441 FormatStyle::PAS_Right); 16442 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 16443 FormatStyle::PAS_Middle); 16444 // For backward compatibility: 16445 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 16446 FormatStyle::PAS_Left); 16447 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 16448 FormatStyle::PAS_Right); 16449 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 16450 FormatStyle::PAS_Middle); 16451 16452 Style.Standard = FormatStyle::LS_Auto; 16453 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 16454 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 16455 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 16456 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 16457 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 16458 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 16459 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 16460 // Legacy aliases: 16461 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 16462 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 16463 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 16464 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 16465 16466 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16467 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 16468 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 16469 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 16470 FormatStyle::BOS_None); 16471 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 16472 FormatStyle::BOS_All); 16473 // For backward compatibility: 16474 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 16475 FormatStyle::BOS_None); 16476 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 16477 FormatStyle::BOS_All); 16478 16479 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 16480 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 16481 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 16482 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 16483 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 16484 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 16485 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 16486 // For backward compatibility: 16487 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 16488 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 16489 16490 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 16491 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 16492 FormatStyle::BILS_BeforeComma); 16493 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 16494 FormatStyle::BILS_AfterColon); 16495 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 16496 FormatStyle::BILS_BeforeColon); 16497 // For backward compatibility: 16498 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 16499 FormatStyle::BILS_BeforeComma); 16500 16501 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 16502 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 16503 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 16504 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 16505 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 16506 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 16507 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 16508 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 16509 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 16510 16511 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 16512 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 16513 FormatStyle::BAS_Align); 16514 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 16515 FormatStyle::BAS_DontAlign); 16516 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 16517 FormatStyle::BAS_AlwaysBreak); 16518 // For backward compatibility: 16519 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 16520 FormatStyle::BAS_DontAlign); 16521 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 16522 FormatStyle::BAS_Align); 16523 16524 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16525 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 16526 FormatStyle::ENAS_DontAlign); 16527 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 16528 FormatStyle::ENAS_Left); 16529 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 16530 FormatStyle::ENAS_Right); 16531 // For backward compatibility: 16532 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 16533 FormatStyle::ENAS_Left); 16534 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 16535 FormatStyle::ENAS_Right); 16536 16537 Style.AlignOperands = FormatStyle::OAS_Align; 16538 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 16539 FormatStyle::OAS_DontAlign); 16540 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 16541 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 16542 FormatStyle::OAS_AlignAfterOperator); 16543 // For backward compatibility: 16544 CHECK_PARSE("AlignOperands: false", AlignOperands, 16545 FormatStyle::OAS_DontAlign); 16546 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 16547 16548 Style.UseTab = FormatStyle::UT_ForIndentation; 16549 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 16550 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 16551 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 16552 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 16553 FormatStyle::UT_ForContinuationAndIndentation); 16554 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 16555 FormatStyle::UT_AlignWithSpaces); 16556 // For backward compatibility: 16557 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 16558 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 16559 16560 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 16561 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 16562 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 16563 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 16564 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 16565 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 16566 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 16567 // For backward compatibility: 16568 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 16569 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 16570 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 16571 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 16572 16573 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 16574 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 16575 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 16576 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 16577 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 16578 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 16579 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 16580 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 16581 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 16582 // For backward compatibility: 16583 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 16584 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 16585 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 16586 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 16587 16588 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 16589 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 16590 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 16591 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 16592 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 16593 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 16594 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 16595 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 16596 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 16597 16598 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 16599 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 16600 FormatStyle::SBPO_Never); 16601 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 16602 FormatStyle::SBPO_Always); 16603 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 16604 FormatStyle::SBPO_ControlStatements); 16605 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 16606 FormatStyle::SBPO_NonEmptyParentheses); 16607 // For backward compatibility: 16608 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 16609 FormatStyle::SBPO_Never); 16610 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 16611 FormatStyle::SBPO_ControlStatements); 16612 16613 Style.ColumnLimit = 123; 16614 FormatStyle BaseStyle = getLLVMStyle(); 16615 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 16616 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 16617 16618 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 16619 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 16620 FormatStyle::BS_Attach); 16621 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 16622 FormatStyle::BS_Linux); 16623 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 16624 FormatStyle::BS_Mozilla); 16625 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 16626 FormatStyle::BS_Stroustrup); 16627 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 16628 FormatStyle::BS_Allman); 16629 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 16630 FormatStyle::BS_Whitesmiths); 16631 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 16632 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 16633 FormatStyle::BS_WebKit); 16634 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 16635 FormatStyle::BS_Custom); 16636 16637 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 16638 CHECK_PARSE("BraceWrapping:\n" 16639 " AfterControlStatement: MultiLine", 16640 BraceWrapping.AfterControlStatement, 16641 FormatStyle::BWACS_MultiLine); 16642 CHECK_PARSE("BraceWrapping:\n" 16643 " AfterControlStatement: Always", 16644 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 16645 CHECK_PARSE("BraceWrapping:\n" 16646 " AfterControlStatement: Never", 16647 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 16648 // For backward compatibility: 16649 CHECK_PARSE("BraceWrapping:\n" 16650 " AfterControlStatement: true", 16651 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 16652 CHECK_PARSE("BraceWrapping:\n" 16653 " AfterControlStatement: false", 16654 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 16655 16656 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 16657 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 16658 FormatStyle::RTBS_None); 16659 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 16660 FormatStyle::RTBS_All); 16661 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 16662 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 16663 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 16664 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 16665 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 16666 AlwaysBreakAfterReturnType, 16667 FormatStyle::RTBS_TopLevelDefinitions); 16668 16669 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 16670 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 16671 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 16672 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 16673 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 16674 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 16675 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 16676 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 16677 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 16678 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 16679 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 16680 16681 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 16682 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 16683 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 16684 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 16685 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 16686 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 16687 AlwaysBreakAfterDefinitionReturnType, 16688 FormatStyle::DRTBS_TopLevel); 16689 16690 Style.NamespaceIndentation = FormatStyle::NI_All; 16691 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 16692 FormatStyle::NI_None); 16693 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 16694 FormatStyle::NI_Inner); 16695 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 16696 FormatStyle::NI_All); 16697 16698 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always; 16699 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 16700 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 16701 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 16702 AllowShortIfStatementsOnASingleLine, 16703 FormatStyle::SIS_WithoutElse); 16704 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 16705 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Always); 16706 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 16707 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 16708 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 16709 AllowShortIfStatementsOnASingleLine, 16710 FormatStyle::SIS_WithoutElse); 16711 16712 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 16713 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 16714 FormatStyle::IEBS_AfterExternBlock); 16715 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 16716 FormatStyle::IEBS_Indent); 16717 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 16718 FormatStyle::IEBS_NoIndent); 16719 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 16720 FormatStyle::IEBS_Indent); 16721 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 16722 FormatStyle::IEBS_NoIndent); 16723 16724 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 16725 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 16726 FormatStyle::BFCS_Both); 16727 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 16728 FormatStyle::BFCS_None); 16729 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 16730 FormatStyle::BFCS_Before); 16731 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 16732 FormatStyle::BFCS_After); 16733 16734 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 16735 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 16736 FormatStyle::SJSIO_After); 16737 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 16738 FormatStyle::SJSIO_Before); 16739 16740 // FIXME: This is required because parsing a configuration simply overwrites 16741 // the first N elements of the list instead of resetting it. 16742 Style.ForEachMacros.clear(); 16743 std::vector<std::string> BoostForeach; 16744 BoostForeach.push_back("BOOST_FOREACH"); 16745 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 16746 std::vector<std::string> BoostAndQForeach; 16747 BoostAndQForeach.push_back("BOOST_FOREACH"); 16748 BoostAndQForeach.push_back("Q_FOREACH"); 16749 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 16750 BoostAndQForeach); 16751 16752 Style.AttributeMacros.clear(); 16753 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 16754 std::vector<std::string>{"__capability"}); 16755 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 16756 std::vector<std::string>({"attr1", "attr2"})); 16757 16758 Style.StatementAttributeLikeMacros.clear(); 16759 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 16760 StatementAttributeLikeMacros, 16761 std::vector<std::string>({"emit", "Q_EMIT"})); 16762 16763 Style.StatementMacros.clear(); 16764 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 16765 std::vector<std::string>{"QUNUSED"}); 16766 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 16767 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 16768 16769 Style.NamespaceMacros.clear(); 16770 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 16771 std::vector<std::string>{"TESTSUITE"}); 16772 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 16773 std::vector<std::string>({"TESTSUITE", "SUITE"})); 16774 16775 Style.WhitespaceSensitiveMacros.clear(); 16776 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 16777 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 16778 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 16779 WhitespaceSensitiveMacros, 16780 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 16781 Style.WhitespaceSensitiveMacros.clear(); 16782 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 16783 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 16784 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 16785 WhitespaceSensitiveMacros, 16786 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 16787 16788 Style.IncludeStyle.IncludeCategories.clear(); 16789 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 16790 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 16791 CHECK_PARSE("IncludeCategories:\n" 16792 " - Regex: abc/.*\n" 16793 " Priority: 2\n" 16794 " - Regex: .*\n" 16795 " Priority: 1\n" 16796 " CaseSensitive: true\n", 16797 IncludeStyle.IncludeCategories, ExpectedCategories); 16798 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 16799 "abc$"); 16800 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 16801 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 16802 16803 Style.SortIncludes = FormatStyle::SI_Never; 16804 CHECK_PARSE("SortIncludes: true", SortIncludes, 16805 FormatStyle::SI_CaseSensitive); 16806 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 16807 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 16808 FormatStyle::SI_CaseInsensitive); 16809 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 16810 FormatStyle::SI_CaseSensitive); 16811 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 16812 16813 Style.RawStringFormats.clear(); 16814 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 16815 { 16816 FormatStyle::LK_TextProto, 16817 {"pb", "proto"}, 16818 {"PARSE_TEXT_PROTO"}, 16819 /*CanonicalDelimiter=*/"", 16820 "llvm", 16821 }, 16822 { 16823 FormatStyle::LK_Cpp, 16824 {"cc", "cpp"}, 16825 {"C_CODEBLOCK", "CPPEVAL"}, 16826 /*CanonicalDelimiter=*/"cc", 16827 /*BasedOnStyle=*/"", 16828 }, 16829 }; 16830 16831 CHECK_PARSE("RawStringFormats:\n" 16832 " - Language: TextProto\n" 16833 " Delimiters:\n" 16834 " - 'pb'\n" 16835 " - 'proto'\n" 16836 " EnclosingFunctions:\n" 16837 " - 'PARSE_TEXT_PROTO'\n" 16838 " BasedOnStyle: llvm\n" 16839 " - Language: Cpp\n" 16840 " Delimiters:\n" 16841 " - 'cc'\n" 16842 " - 'cpp'\n" 16843 " EnclosingFunctions:\n" 16844 " - 'C_CODEBLOCK'\n" 16845 " - 'CPPEVAL'\n" 16846 " CanonicalDelimiter: 'cc'", 16847 RawStringFormats, ExpectedRawStringFormats); 16848 16849 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 16850 " Minimum: 0\n" 16851 " Maximum: 0", 16852 SpacesInLineCommentPrefix.Minimum, 0u); 16853 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 16854 Style.SpacesInLineCommentPrefix.Minimum = 1; 16855 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 16856 " Minimum: 2", 16857 SpacesInLineCommentPrefix.Minimum, 0u); 16858 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 16859 " Maximum: -1", 16860 SpacesInLineCommentPrefix.Maximum, -1u); 16861 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 16862 " Minimum: 2", 16863 SpacesInLineCommentPrefix.Minimum, 2u); 16864 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 16865 " Maximum: 1", 16866 SpacesInLineCommentPrefix.Maximum, 1u); 16867 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 16868 } 16869 16870 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 16871 FormatStyle Style = {}; 16872 Style.Language = FormatStyle::LK_Cpp; 16873 CHECK_PARSE("Language: Cpp\n" 16874 "IndentWidth: 12", 16875 IndentWidth, 12u); 16876 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 16877 "IndentWidth: 34", 16878 &Style), 16879 ParseError::Unsuitable); 16880 FormatStyle BinPackedTCS = {}; 16881 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 16882 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 16883 "InsertTrailingCommas: Wrapped", 16884 &BinPackedTCS), 16885 ParseError::BinPackTrailingCommaConflict); 16886 EXPECT_EQ(12u, Style.IndentWidth); 16887 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 16888 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 16889 16890 Style.Language = FormatStyle::LK_JavaScript; 16891 CHECK_PARSE("Language: JavaScript\n" 16892 "IndentWidth: 12", 16893 IndentWidth, 12u); 16894 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 16895 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 16896 "IndentWidth: 34", 16897 &Style), 16898 ParseError::Unsuitable); 16899 EXPECT_EQ(23u, Style.IndentWidth); 16900 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 16901 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 16902 16903 CHECK_PARSE("BasedOnStyle: LLVM\n" 16904 "IndentWidth: 67", 16905 IndentWidth, 67u); 16906 16907 CHECK_PARSE("---\n" 16908 "Language: JavaScript\n" 16909 "IndentWidth: 12\n" 16910 "---\n" 16911 "Language: Cpp\n" 16912 "IndentWidth: 34\n" 16913 "...\n", 16914 IndentWidth, 12u); 16915 16916 Style.Language = FormatStyle::LK_Cpp; 16917 CHECK_PARSE("---\n" 16918 "Language: JavaScript\n" 16919 "IndentWidth: 12\n" 16920 "---\n" 16921 "Language: Cpp\n" 16922 "IndentWidth: 34\n" 16923 "...\n", 16924 IndentWidth, 34u); 16925 CHECK_PARSE("---\n" 16926 "IndentWidth: 78\n" 16927 "---\n" 16928 "Language: JavaScript\n" 16929 "IndentWidth: 56\n" 16930 "...\n", 16931 IndentWidth, 78u); 16932 16933 Style.ColumnLimit = 123; 16934 Style.IndentWidth = 234; 16935 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 16936 Style.TabWidth = 345; 16937 EXPECT_FALSE(parseConfiguration("---\n" 16938 "IndentWidth: 456\n" 16939 "BreakBeforeBraces: Allman\n" 16940 "---\n" 16941 "Language: JavaScript\n" 16942 "IndentWidth: 111\n" 16943 "TabWidth: 111\n" 16944 "---\n" 16945 "Language: Cpp\n" 16946 "BreakBeforeBraces: Stroustrup\n" 16947 "TabWidth: 789\n" 16948 "...\n", 16949 &Style)); 16950 EXPECT_EQ(123u, Style.ColumnLimit); 16951 EXPECT_EQ(456u, Style.IndentWidth); 16952 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 16953 EXPECT_EQ(789u, Style.TabWidth); 16954 16955 EXPECT_EQ(parseConfiguration("---\n" 16956 "Language: JavaScript\n" 16957 "IndentWidth: 56\n" 16958 "---\n" 16959 "IndentWidth: 78\n" 16960 "...\n", 16961 &Style), 16962 ParseError::Error); 16963 EXPECT_EQ(parseConfiguration("---\n" 16964 "Language: JavaScript\n" 16965 "IndentWidth: 56\n" 16966 "---\n" 16967 "Language: JavaScript\n" 16968 "IndentWidth: 78\n" 16969 "...\n", 16970 &Style), 16971 ParseError::Error); 16972 16973 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 16974 } 16975 16976 #undef CHECK_PARSE 16977 16978 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 16979 FormatStyle Style = {}; 16980 Style.Language = FormatStyle::LK_JavaScript; 16981 Style.BreakBeforeTernaryOperators = true; 16982 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 16983 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 16984 16985 Style.BreakBeforeTernaryOperators = true; 16986 EXPECT_EQ(0, parseConfiguration("---\n" 16987 "BasedOnStyle: Google\n" 16988 "---\n" 16989 "Language: JavaScript\n" 16990 "IndentWidth: 76\n" 16991 "...\n", 16992 &Style) 16993 .value()); 16994 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 16995 EXPECT_EQ(76u, Style.IndentWidth); 16996 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 16997 } 16998 16999 TEST_F(FormatTest, ConfigurationRoundTripTest) { 17000 FormatStyle Style = getLLVMStyle(); 17001 std::string YAML = configurationAsText(Style); 17002 FormatStyle ParsedStyle = {}; 17003 ParsedStyle.Language = FormatStyle::LK_Cpp; 17004 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 17005 EXPECT_EQ(Style, ParsedStyle); 17006 } 17007 17008 TEST_F(FormatTest, WorksFor8bitEncodings) { 17009 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 17010 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 17011 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 17012 "\"\xef\xee\xf0\xf3...\"", 17013 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 17014 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 17015 "\xef\xee\xf0\xf3...\"", 17016 getLLVMStyleWithColumns(12))); 17017 } 17018 17019 TEST_F(FormatTest, HandlesUTF8BOM) { 17020 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 17021 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 17022 format("\xef\xbb\xbf#include <iostream>")); 17023 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 17024 format("\xef\xbb\xbf\n#include <iostream>")); 17025 } 17026 17027 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 17028 #if !defined(_MSC_VER) 17029 17030 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 17031 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 17032 getLLVMStyleWithColumns(35)); 17033 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 17034 getLLVMStyleWithColumns(31)); 17035 verifyFormat("// Однажды в студёную зимнюю пору...", 17036 getLLVMStyleWithColumns(36)); 17037 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 17038 verifyFormat("/* Однажды в студёную зимнюю пору... */", 17039 getLLVMStyleWithColumns(39)); 17040 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 17041 getLLVMStyleWithColumns(35)); 17042 } 17043 17044 TEST_F(FormatTest, SplitsUTF8Strings) { 17045 // Non-printable characters' width is currently considered to be the length in 17046 // bytes in UTF8. The characters can be displayed in very different manner 17047 // (zero-width, single width with a substitution glyph, expanded to their code 17048 // (e.g. "<8d>"), so there's no single correct way to handle them. 17049 EXPECT_EQ("\"aaaaÄ\"\n" 17050 "\"\xc2\x8d\";", 17051 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 17052 EXPECT_EQ("\"aaaaaaaÄ\"\n" 17053 "\"\xc2\x8d\";", 17054 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 17055 EXPECT_EQ("\"Однажды, в \"\n" 17056 "\"студёную \"\n" 17057 "\"зимнюю \"\n" 17058 "\"пору,\"", 17059 format("\"Однажды, в студёную зимнюю пору,\"", 17060 getLLVMStyleWithColumns(13))); 17061 EXPECT_EQ( 17062 "\"一 二 三 \"\n" 17063 "\"四 五六 \"\n" 17064 "\"七 八 九 \"\n" 17065 "\"十\"", 17066 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 17067 EXPECT_EQ("\"一\t\"\n" 17068 "\"二 \t\"\n" 17069 "\"三 四 \"\n" 17070 "\"五\t\"\n" 17071 "\"六 \t\"\n" 17072 "\"七 \"\n" 17073 "\"八九十\tqq\"", 17074 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 17075 getLLVMStyleWithColumns(11))); 17076 17077 // UTF8 character in an escape sequence. 17078 EXPECT_EQ("\"aaaaaa\"\n" 17079 "\"\\\xC2\x8D\"", 17080 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 17081 } 17082 17083 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 17084 EXPECT_EQ("const char *sssss =\n" 17085 " \"一二三四五六七八\\\n" 17086 " 九 十\";", 17087 format("const char *sssss = \"一二三四五六七八\\\n" 17088 " 九 十\";", 17089 getLLVMStyleWithColumns(30))); 17090 } 17091 17092 TEST_F(FormatTest, SplitsUTF8LineComments) { 17093 EXPECT_EQ("// aaaaÄ\xc2\x8d", 17094 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 17095 EXPECT_EQ("// Я из лесу\n" 17096 "// вышел; был\n" 17097 "// сильный\n" 17098 "// мороз.", 17099 format("// Я из лесу вышел; был сильный мороз.", 17100 getLLVMStyleWithColumns(13))); 17101 EXPECT_EQ("// 一二三\n" 17102 "// 四五六七\n" 17103 "// 八 九\n" 17104 "// 十", 17105 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 17106 } 17107 17108 TEST_F(FormatTest, SplitsUTF8BlockComments) { 17109 EXPECT_EQ("/* Гляжу,\n" 17110 " * поднимается\n" 17111 " * медленно в\n" 17112 " * гору\n" 17113 " * Лошадка,\n" 17114 " * везущая\n" 17115 " * хворосту\n" 17116 " * воз. */", 17117 format("/* Гляжу, поднимается медленно в гору\n" 17118 " * Лошадка, везущая хворосту воз. */", 17119 getLLVMStyleWithColumns(13))); 17120 EXPECT_EQ( 17121 "/* 一二三\n" 17122 " * 四五六七\n" 17123 " * 八 九\n" 17124 " * 十 */", 17125 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 17126 EXPECT_EQ("/* \n" 17127 " * \n" 17128 " * - */", 17129 format("/* - */", getLLVMStyleWithColumns(12))); 17130 } 17131 17132 #endif // _MSC_VER 17133 17134 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 17135 FormatStyle Style = getLLVMStyle(); 17136 17137 Style.ConstructorInitializerIndentWidth = 4; 17138 verifyFormat( 17139 "SomeClass::Constructor()\n" 17140 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 17141 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 17142 Style); 17143 17144 Style.ConstructorInitializerIndentWidth = 2; 17145 verifyFormat( 17146 "SomeClass::Constructor()\n" 17147 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 17148 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 17149 Style); 17150 17151 Style.ConstructorInitializerIndentWidth = 0; 17152 verifyFormat( 17153 "SomeClass::Constructor()\n" 17154 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 17155 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 17156 Style); 17157 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 17158 verifyFormat( 17159 "SomeLongTemplateVariableName<\n" 17160 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 17161 Style); 17162 verifyFormat("bool smaller = 1 < " 17163 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 17164 " " 17165 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 17166 Style); 17167 17168 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 17169 verifyFormat("SomeClass::Constructor() :\n" 17170 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 17171 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 17172 Style); 17173 } 17174 17175 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 17176 FormatStyle Style = getLLVMStyle(); 17177 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 17178 Style.ConstructorInitializerIndentWidth = 4; 17179 verifyFormat("SomeClass::Constructor()\n" 17180 " : a(a)\n" 17181 " , b(b)\n" 17182 " , c(c) {}", 17183 Style); 17184 verifyFormat("SomeClass::Constructor()\n" 17185 " : a(a) {}", 17186 Style); 17187 17188 Style.ColumnLimit = 0; 17189 verifyFormat("SomeClass::Constructor()\n" 17190 " : a(a) {}", 17191 Style); 17192 verifyFormat("SomeClass::Constructor() noexcept\n" 17193 " : a(a) {}", 17194 Style); 17195 verifyFormat("SomeClass::Constructor()\n" 17196 " : a(a)\n" 17197 " , b(b)\n" 17198 " , c(c) {}", 17199 Style); 17200 verifyFormat("SomeClass::Constructor()\n" 17201 " : a(a) {\n" 17202 " foo();\n" 17203 " bar();\n" 17204 "}", 17205 Style); 17206 17207 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 17208 verifyFormat("SomeClass::Constructor()\n" 17209 " : a(a)\n" 17210 " , b(b)\n" 17211 " , c(c) {\n}", 17212 Style); 17213 verifyFormat("SomeClass::Constructor()\n" 17214 " : a(a) {\n}", 17215 Style); 17216 17217 Style.ColumnLimit = 80; 17218 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 17219 Style.ConstructorInitializerIndentWidth = 2; 17220 verifyFormat("SomeClass::Constructor()\n" 17221 " : a(a)\n" 17222 " , b(b)\n" 17223 " , c(c) {}", 17224 Style); 17225 17226 Style.ConstructorInitializerIndentWidth = 0; 17227 verifyFormat("SomeClass::Constructor()\n" 17228 ": a(a)\n" 17229 ", b(b)\n" 17230 ", c(c) {}", 17231 Style); 17232 17233 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 17234 Style.ConstructorInitializerIndentWidth = 4; 17235 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 17236 verifyFormat( 17237 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 17238 Style); 17239 verifyFormat( 17240 "SomeClass::Constructor()\n" 17241 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 17242 Style); 17243 Style.ConstructorInitializerIndentWidth = 4; 17244 Style.ColumnLimit = 60; 17245 verifyFormat("SomeClass::Constructor()\n" 17246 " : aaaaaaaa(aaaaaaaa)\n" 17247 " , aaaaaaaa(aaaaaaaa)\n" 17248 " , aaaaaaaa(aaaaaaaa) {}", 17249 Style); 17250 } 17251 17252 TEST_F(FormatTest, Destructors) { 17253 verifyFormat("void F(int &i) { i.~int(); }"); 17254 verifyFormat("void F(int &i) { i->~int(); }"); 17255 } 17256 17257 TEST_F(FormatTest, FormatsWithWebKitStyle) { 17258 FormatStyle Style = getWebKitStyle(); 17259 17260 // Don't indent in outer namespaces. 17261 verifyFormat("namespace outer {\n" 17262 "int i;\n" 17263 "namespace inner {\n" 17264 " int i;\n" 17265 "} // namespace inner\n" 17266 "} // namespace outer\n" 17267 "namespace other_outer {\n" 17268 "int i;\n" 17269 "}", 17270 Style); 17271 17272 // Don't indent case labels. 17273 verifyFormat("switch (variable) {\n" 17274 "case 1:\n" 17275 "case 2:\n" 17276 " doSomething();\n" 17277 " break;\n" 17278 "default:\n" 17279 " ++variable;\n" 17280 "}", 17281 Style); 17282 17283 // Wrap before binary operators. 17284 EXPECT_EQ("void f()\n" 17285 "{\n" 17286 " if (aaaaaaaaaaaaaaaa\n" 17287 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 17288 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 17289 " return;\n" 17290 "}", 17291 format("void f() {\n" 17292 "if (aaaaaaaaaaaaaaaa\n" 17293 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 17294 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 17295 "return;\n" 17296 "}", 17297 Style)); 17298 17299 // Allow functions on a single line. 17300 verifyFormat("void f() { return; }", Style); 17301 17302 // Allow empty blocks on a single line and insert a space in empty blocks. 17303 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 17304 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 17305 // However, don't merge non-empty short loops. 17306 EXPECT_EQ("while (true) {\n" 17307 " continue;\n" 17308 "}", 17309 format("while (true) { continue; }", Style)); 17310 17311 // Constructor initializers are formatted one per line with the "," on the 17312 // new line. 17313 verifyFormat("Constructor()\n" 17314 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 17315 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 17316 " aaaaaaaaaaaaaa)\n" 17317 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 17318 "{\n" 17319 "}", 17320 Style); 17321 verifyFormat("SomeClass::Constructor()\n" 17322 " : a(a)\n" 17323 "{\n" 17324 "}", 17325 Style); 17326 EXPECT_EQ("SomeClass::Constructor()\n" 17327 " : a(a)\n" 17328 "{\n" 17329 "}", 17330 format("SomeClass::Constructor():a(a){}", Style)); 17331 verifyFormat("SomeClass::Constructor()\n" 17332 " : a(a)\n" 17333 " , b(b)\n" 17334 " , c(c)\n" 17335 "{\n" 17336 "}", 17337 Style); 17338 verifyFormat("SomeClass::Constructor()\n" 17339 " : a(a)\n" 17340 "{\n" 17341 " foo();\n" 17342 " bar();\n" 17343 "}", 17344 Style); 17345 17346 // Access specifiers should be aligned left. 17347 verifyFormat("class C {\n" 17348 "public:\n" 17349 " int i;\n" 17350 "};", 17351 Style); 17352 17353 // Do not align comments. 17354 verifyFormat("int a; // Do not\n" 17355 "double b; // align comments.", 17356 Style); 17357 17358 // Do not align operands. 17359 EXPECT_EQ("ASSERT(aaaa\n" 17360 " || bbbb);", 17361 format("ASSERT ( aaaa\n||bbbb);", Style)); 17362 17363 // Accept input's line breaks. 17364 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 17365 " || bbbbbbbbbbbbbbb) {\n" 17366 " i++;\n" 17367 "}", 17368 format("if (aaaaaaaaaaaaaaa\n" 17369 "|| bbbbbbbbbbbbbbb) { i++; }", 17370 Style)); 17371 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 17372 " i++;\n" 17373 "}", 17374 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 17375 17376 // Don't automatically break all macro definitions (llvm.org/PR17842). 17377 verifyFormat("#define aNumber 10", Style); 17378 // However, generally keep the line breaks that the user authored. 17379 EXPECT_EQ("#define aNumber \\\n" 17380 " 10", 17381 format("#define aNumber \\\n" 17382 " 10", 17383 Style)); 17384 17385 // Keep empty and one-element array literals on a single line. 17386 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 17387 " copyItems:YES];", 17388 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 17389 "copyItems:YES];", 17390 Style)); 17391 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 17392 " copyItems:YES];", 17393 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 17394 " copyItems:YES];", 17395 Style)); 17396 // FIXME: This does not seem right, there should be more indentation before 17397 // the array literal's entries. Nested blocks have the same problem. 17398 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 17399 " @\"a\",\n" 17400 " @\"a\"\n" 17401 "]\n" 17402 " copyItems:YES];", 17403 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 17404 " @\"a\",\n" 17405 " @\"a\"\n" 17406 " ]\n" 17407 " copyItems:YES];", 17408 Style)); 17409 EXPECT_EQ( 17410 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 17411 " copyItems:YES];", 17412 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 17413 " copyItems:YES];", 17414 Style)); 17415 17416 verifyFormat("[self.a b:c c:d];", Style); 17417 EXPECT_EQ("[self.a b:c\n" 17418 " c:d];", 17419 format("[self.a b:c\n" 17420 "c:d];", 17421 Style)); 17422 } 17423 17424 TEST_F(FormatTest, FormatsLambdas) { 17425 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 17426 verifyFormat( 17427 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 17428 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 17429 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 17430 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 17431 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 17432 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 17433 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 17434 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 17435 verifyFormat("int x = f(*+[] {});"); 17436 verifyFormat("void f() {\n" 17437 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 17438 "}\n"); 17439 verifyFormat("void f() {\n" 17440 " other(x.begin(), //\n" 17441 " x.end(), //\n" 17442 " [&](int, int) { return 1; });\n" 17443 "}\n"); 17444 verifyFormat("void f() {\n" 17445 " other.other.other.other.other(\n" 17446 " x.begin(), x.end(),\n" 17447 " [something, rather](int, int, int, int, int, int, int) { " 17448 "return 1; });\n" 17449 "}\n"); 17450 verifyFormat( 17451 "void f() {\n" 17452 " other.other.other.other.other(\n" 17453 " x.begin(), x.end(),\n" 17454 " [something, rather](int, int, int, int, int, int, int) {\n" 17455 " //\n" 17456 " });\n" 17457 "}\n"); 17458 verifyFormat("SomeFunction([]() { // A cool function...\n" 17459 " return 43;\n" 17460 "});"); 17461 EXPECT_EQ("SomeFunction([]() {\n" 17462 "#define A a\n" 17463 " return 43;\n" 17464 "});", 17465 format("SomeFunction([](){\n" 17466 "#define A a\n" 17467 "return 43;\n" 17468 "});")); 17469 verifyFormat("void f() {\n" 17470 " SomeFunction([](decltype(x), A *a) {});\n" 17471 " SomeFunction([](typeof(x), A *a) {});\n" 17472 " SomeFunction([](_Atomic(x), A *a) {});\n" 17473 " SomeFunction([](__underlying_type(x), A *a) {});\n" 17474 "}"); 17475 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 17476 " [](const aaaaaaaaaa &a) { return a; });"); 17477 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 17478 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 17479 "});"); 17480 verifyFormat("Constructor()\n" 17481 " : Field([] { // comment\n" 17482 " int i;\n" 17483 " }) {}"); 17484 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 17485 " return some_parameter.size();\n" 17486 "};"); 17487 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 17488 " [](const string &s) { return s; };"); 17489 verifyFormat("int i = aaaaaa ? 1 //\n" 17490 " : [] {\n" 17491 " return 2; //\n" 17492 " }();"); 17493 verifyFormat("llvm::errs() << \"number of twos is \"\n" 17494 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 17495 " return x == 2; // force break\n" 17496 " });"); 17497 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 17498 " [=](int iiiiiiiiiiii) {\n" 17499 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 17500 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 17501 " });", 17502 getLLVMStyleWithColumns(60)); 17503 verifyFormat("SomeFunction({[&] {\n" 17504 " // comment\n" 17505 " },\n" 17506 " [&] {\n" 17507 " // comment\n" 17508 " }});"); 17509 verifyFormat("SomeFunction({[&] {\n" 17510 " // comment\n" 17511 "}});"); 17512 verifyFormat( 17513 "virtual aaaaaaaaaaaaaaaa(\n" 17514 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 17515 " aaaaa aaaaaaaaa);"); 17516 17517 // Lambdas with return types. 17518 verifyFormat("int c = []() -> int { return 2; }();\n"); 17519 verifyFormat("int c = []() -> int * { return 2; }();\n"); 17520 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 17521 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 17522 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 17523 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 17524 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 17525 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 17526 verifyFormat("[a, a]() -> a<1> {};"); 17527 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 17528 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 17529 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 17530 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 17531 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 17532 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 17533 verifyFormat("[]() -> foo<!5> { return {}; };"); 17534 verifyFormat("[]() -> foo<~5> { return {}; };"); 17535 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 17536 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 17537 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 17538 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 17539 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 17540 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 17541 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 17542 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 17543 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 17544 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 17545 verifyFormat("namespace bar {\n" 17546 "// broken:\n" 17547 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 17548 "} // namespace bar"); 17549 verifyFormat("namespace bar {\n" 17550 "// broken:\n" 17551 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 17552 "} // namespace bar"); 17553 verifyFormat("namespace bar {\n" 17554 "// broken:\n" 17555 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 17556 "} // namespace bar"); 17557 verifyFormat("namespace bar {\n" 17558 "// broken:\n" 17559 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 17560 "} // namespace bar"); 17561 verifyFormat("namespace bar {\n" 17562 "// broken:\n" 17563 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 17564 "} // namespace bar"); 17565 verifyFormat("namespace bar {\n" 17566 "// broken:\n" 17567 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 17568 "} // namespace bar"); 17569 verifyFormat("namespace bar {\n" 17570 "// broken:\n" 17571 "auto foo{[]() -> foo<!5> { return {}; }};\n" 17572 "} // namespace bar"); 17573 verifyFormat("namespace bar {\n" 17574 "// broken:\n" 17575 "auto foo{[]() -> foo<~5> { return {}; }};\n" 17576 "} // namespace bar"); 17577 verifyFormat("namespace bar {\n" 17578 "// broken:\n" 17579 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 17580 "} // namespace bar"); 17581 verifyFormat("namespace bar {\n" 17582 "// broken:\n" 17583 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 17584 "} // namespace bar"); 17585 verifyFormat("namespace bar {\n" 17586 "// broken:\n" 17587 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 17588 "} // namespace bar"); 17589 verifyFormat("namespace bar {\n" 17590 "// broken:\n" 17591 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 17592 "} // namespace bar"); 17593 verifyFormat("namespace bar {\n" 17594 "// broken:\n" 17595 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 17596 "} // namespace bar"); 17597 verifyFormat("namespace bar {\n" 17598 "// broken:\n" 17599 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 17600 "} // namespace bar"); 17601 verifyFormat("namespace bar {\n" 17602 "// broken:\n" 17603 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 17604 "} // namespace bar"); 17605 verifyFormat("namespace bar {\n" 17606 "// broken:\n" 17607 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 17608 "} // namespace bar"); 17609 verifyFormat("namespace bar {\n" 17610 "// broken:\n" 17611 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 17612 "} // namespace bar"); 17613 verifyFormat("namespace bar {\n" 17614 "// broken:\n" 17615 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 17616 "} // namespace bar"); 17617 verifyFormat("[]() -> a<1> {};"); 17618 verifyFormat("[]() -> a<1> { ; };"); 17619 verifyFormat("[]() -> a<1> { ; }();"); 17620 verifyFormat("[a, a]() -> a<true> {};"); 17621 verifyFormat("[]() -> a<true> {};"); 17622 verifyFormat("[]() -> a<true> { ; };"); 17623 verifyFormat("[]() -> a<true> { ; }();"); 17624 verifyFormat("[a, a]() -> a<false> {};"); 17625 verifyFormat("[]() -> a<false> {};"); 17626 verifyFormat("[]() -> a<false> { ; };"); 17627 verifyFormat("[]() -> a<false> { ; }();"); 17628 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 17629 verifyFormat("namespace bar {\n" 17630 "auto foo{[]() -> foo<false> { ; }};\n" 17631 "} // namespace bar"); 17632 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 17633 " int j) -> int {\n" 17634 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 17635 "};"); 17636 verifyFormat( 17637 "aaaaaaaaaaaaaaaaaaaaaa(\n" 17638 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 17639 " return aaaaaaaaaaaaaaaaa;\n" 17640 " });", 17641 getLLVMStyleWithColumns(70)); 17642 verifyFormat("[]() //\n" 17643 " -> int {\n" 17644 " return 1; //\n" 17645 "};"); 17646 verifyFormat("[]() -> Void<T...> {};"); 17647 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 17648 17649 // Lambdas with explicit template argument lists. 17650 verifyFormat( 17651 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 17652 17653 // Multiple lambdas in the same parentheses change indentation rules. These 17654 // lambdas are forced to start on new lines. 17655 verifyFormat("SomeFunction(\n" 17656 " []() {\n" 17657 " //\n" 17658 " },\n" 17659 " []() {\n" 17660 " //\n" 17661 " });"); 17662 17663 // A lambda passed as arg0 is always pushed to the next line. 17664 verifyFormat("SomeFunction(\n" 17665 " [this] {\n" 17666 " //\n" 17667 " },\n" 17668 " 1);\n"); 17669 17670 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 17671 // the arg0 case above. 17672 auto Style = getGoogleStyle(); 17673 Style.BinPackArguments = false; 17674 verifyFormat("SomeFunction(\n" 17675 " a,\n" 17676 " [this] {\n" 17677 " //\n" 17678 " },\n" 17679 " b);\n", 17680 Style); 17681 verifyFormat("SomeFunction(\n" 17682 " a,\n" 17683 " [this] {\n" 17684 " //\n" 17685 " },\n" 17686 " b);\n"); 17687 17688 // A lambda with a very long line forces arg0 to be pushed out irrespective of 17689 // the BinPackArguments value (as long as the code is wide enough). 17690 verifyFormat( 17691 "something->SomeFunction(\n" 17692 " a,\n" 17693 " [this] {\n" 17694 " " 17695 "D0000000000000000000000000000000000000000000000000000000000001();\n" 17696 " },\n" 17697 " b);\n"); 17698 17699 // A multi-line lambda is pulled up as long as the introducer fits on the 17700 // previous line and there are no further args. 17701 verifyFormat("function(1, [this, that] {\n" 17702 " //\n" 17703 "});\n"); 17704 verifyFormat("function([this, that] {\n" 17705 " //\n" 17706 "});\n"); 17707 // FIXME: this format is not ideal and we should consider forcing the first 17708 // arg onto its own line. 17709 verifyFormat("function(a, b, c, //\n" 17710 " d, [this, that] {\n" 17711 " //\n" 17712 " });\n"); 17713 17714 // Multiple lambdas are treated correctly even when there is a short arg0. 17715 verifyFormat("SomeFunction(\n" 17716 " 1,\n" 17717 " [this] {\n" 17718 " //\n" 17719 " },\n" 17720 " [this] {\n" 17721 " //\n" 17722 " },\n" 17723 " 1);\n"); 17724 17725 // More complex introducers. 17726 verifyFormat("return [i, args...] {};"); 17727 17728 // Not lambdas. 17729 verifyFormat("constexpr char hello[]{\"hello\"};"); 17730 verifyFormat("double &operator[](int i) { return 0; }\n" 17731 "int i;"); 17732 verifyFormat("std::unique_ptr<int[]> foo() {}"); 17733 verifyFormat("int i = a[a][a]->f();"); 17734 verifyFormat("int i = (*b)[a]->f();"); 17735 17736 // Other corner cases. 17737 verifyFormat("void f() {\n" 17738 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 17739 " );\n" 17740 "}"); 17741 17742 // Lambdas created through weird macros. 17743 verifyFormat("void f() {\n" 17744 " MACRO((const AA &a) { return 1; });\n" 17745 " MACRO((AA &a) { return 1; });\n" 17746 "}"); 17747 17748 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 17749 " doo_dah();\n" 17750 " doo_dah();\n" 17751 " })) {\n" 17752 "}"); 17753 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 17754 " doo_dah();\n" 17755 " doo_dah();\n" 17756 " })) {\n" 17757 "}"); 17758 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 17759 " doo_dah();\n" 17760 " doo_dah();\n" 17761 " })) {\n" 17762 "}"); 17763 verifyFormat("auto lambda = []() {\n" 17764 " int a = 2\n" 17765 "#if A\n" 17766 " + 2\n" 17767 "#endif\n" 17768 " ;\n" 17769 "};"); 17770 17771 // Lambdas with complex multiline introducers. 17772 verifyFormat( 17773 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 17774 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 17775 " -> ::std::unordered_set<\n" 17776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 17777 " //\n" 17778 " });"); 17779 17780 FormatStyle DoNotMerge = getLLVMStyle(); 17781 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17782 verifyFormat("auto c = []() {\n" 17783 " return b;\n" 17784 "};", 17785 "auto c = []() { return b; };", DoNotMerge); 17786 verifyFormat("auto c = []() {\n" 17787 "};", 17788 " auto c = []() {};", DoNotMerge); 17789 17790 FormatStyle MergeEmptyOnly = getLLVMStyle(); 17791 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 17792 verifyFormat("auto c = []() {\n" 17793 " return b;\n" 17794 "};", 17795 "auto c = []() {\n" 17796 " return b;\n" 17797 " };", 17798 MergeEmptyOnly); 17799 verifyFormat("auto c = []() {};", 17800 "auto c = []() {\n" 17801 "};", 17802 MergeEmptyOnly); 17803 17804 FormatStyle MergeInline = getLLVMStyle(); 17805 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 17806 verifyFormat("auto c = []() {\n" 17807 " return b;\n" 17808 "};", 17809 "auto c = []() { return b; };", MergeInline); 17810 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 17811 MergeInline); 17812 verifyFormat("function([]() { return b; }, a)", 17813 "function([]() { return b; }, a)", MergeInline); 17814 verifyFormat("function(a, []() { return b; })", 17815 "function(a, []() { return b; })", MergeInline); 17816 17817 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 17818 // AllowShortLambdasOnASingleLine 17819 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 17820 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 17821 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 17822 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 17823 FormatStyle::ShortLambdaStyle::SLS_None; 17824 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 17825 " []()\n" 17826 " {\n" 17827 " return 17;\n" 17828 " });", 17829 LLVMWithBeforeLambdaBody); 17830 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 17831 " []()\n" 17832 " {\n" 17833 " });", 17834 LLVMWithBeforeLambdaBody); 17835 verifyFormat("auto fct_SLS_None = []()\n" 17836 "{\n" 17837 " return 17;\n" 17838 "};", 17839 LLVMWithBeforeLambdaBody); 17840 verifyFormat("TwoNestedLambdas_SLS_None(\n" 17841 " []()\n" 17842 " {\n" 17843 " return Call(\n" 17844 " []()\n" 17845 " {\n" 17846 " return 17;\n" 17847 " });\n" 17848 " });", 17849 LLVMWithBeforeLambdaBody); 17850 verifyFormat("void Fct()\n" 17851 "{\n" 17852 " return {[]()\n" 17853 " {\n" 17854 " return 17;\n" 17855 " }};\n" 17856 "}", 17857 LLVMWithBeforeLambdaBody); 17858 17859 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 17860 FormatStyle::ShortLambdaStyle::SLS_Empty; 17861 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 17862 " []()\n" 17863 " {\n" 17864 " return 17;\n" 17865 " });", 17866 LLVMWithBeforeLambdaBody); 17867 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 17868 LLVMWithBeforeLambdaBody); 17869 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 17870 "ongFunctionName_SLS_Empty(\n" 17871 " []() {});", 17872 LLVMWithBeforeLambdaBody); 17873 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 17874 " []()\n" 17875 " {\n" 17876 " return 17;\n" 17877 " });", 17878 LLVMWithBeforeLambdaBody); 17879 verifyFormat("auto fct_SLS_Empty = []()\n" 17880 "{\n" 17881 " return 17;\n" 17882 "};", 17883 LLVMWithBeforeLambdaBody); 17884 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 17885 " []()\n" 17886 " {\n" 17887 " return Call([]() {});\n" 17888 " });", 17889 LLVMWithBeforeLambdaBody); 17890 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 17891 " []()\n" 17892 " {\n" 17893 " return Call([]() {});\n" 17894 " });", 17895 LLVMWithBeforeLambdaBody); 17896 verifyFormat( 17897 "FctWithLongLineInLambda_SLS_Empty(\n" 17898 " []()\n" 17899 " {\n" 17900 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 17901 " AndShouldNotBeConsiderAsInline,\n" 17902 " LambdaBodyMustBeBreak);\n" 17903 " });", 17904 LLVMWithBeforeLambdaBody); 17905 17906 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 17907 FormatStyle::ShortLambdaStyle::SLS_Inline; 17908 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 17909 LLVMWithBeforeLambdaBody); 17910 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 17911 LLVMWithBeforeLambdaBody); 17912 verifyFormat("auto fct_SLS_Inline = []()\n" 17913 "{\n" 17914 " return 17;\n" 17915 "};", 17916 LLVMWithBeforeLambdaBody); 17917 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 17918 "17; }); });", 17919 LLVMWithBeforeLambdaBody); 17920 verifyFormat( 17921 "FctWithLongLineInLambda_SLS_Inline(\n" 17922 " []()\n" 17923 " {\n" 17924 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 17925 " AndShouldNotBeConsiderAsInline,\n" 17926 " LambdaBodyMustBeBreak);\n" 17927 " });", 17928 LLVMWithBeforeLambdaBody); 17929 verifyFormat("FctWithMultipleParams_SLS_Inline(" 17930 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 17931 " []() { return 17; });", 17932 LLVMWithBeforeLambdaBody); 17933 verifyFormat( 17934 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 17935 LLVMWithBeforeLambdaBody); 17936 17937 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 17938 FormatStyle::ShortLambdaStyle::SLS_All; 17939 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 17940 LLVMWithBeforeLambdaBody); 17941 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 17942 LLVMWithBeforeLambdaBody); 17943 verifyFormat("auto fct_SLS_All = []() { return 17; };", 17944 LLVMWithBeforeLambdaBody); 17945 verifyFormat("FctWithOneParam_SLS_All(\n" 17946 " []()\n" 17947 " {\n" 17948 " // A cool function...\n" 17949 " return 43;\n" 17950 " });", 17951 LLVMWithBeforeLambdaBody); 17952 verifyFormat("FctWithMultipleParams_SLS_All(" 17953 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 17954 " []() { return 17; });", 17955 LLVMWithBeforeLambdaBody); 17956 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 17957 LLVMWithBeforeLambdaBody); 17958 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 17959 LLVMWithBeforeLambdaBody); 17960 verifyFormat( 17961 "FctWithLongLineInLambda_SLS_All(\n" 17962 " []()\n" 17963 " {\n" 17964 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 17965 " AndShouldNotBeConsiderAsInline,\n" 17966 " LambdaBodyMustBeBreak);\n" 17967 " });", 17968 LLVMWithBeforeLambdaBody); 17969 verifyFormat( 17970 "auto fct_SLS_All = []()\n" 17971 "{\n" 17972 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 17973 " AndShouldNotBeConsiderAsInline,\n" 17974 " LambdaBodyMustBeBreak);\n" 17975 "};", 17976 LLVMWithBeforeLambdaBody); 17977 LLVMWithBeforeLambdaBody.BinPackParameters = false; 17978 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 17979 LLVMWithBeforeLambdaBody); 17980 verifyFormat( 17981 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 17982 " FirstParam,\n" 17983 " SecondParam,\n" 17984 " ThirdParam,\n" 17985 " FourthParam);", 17986 LLVMWithBeforeLambdaBody); 17987 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 17988 " []() { return " 17989 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 17990 " FirstParam,\n" 17991 " SecondParam,\n" 17992 " ThirdParam,\n" 17993 " FourthParam);", 17994 LLVMWithBeforeLambdaBody); 17995 verifyFormat( 17996 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 17997 " SecondParam,\n" 17998 " ThirdParam,\n" 17999 " FourthParam,\n" 18000 " []() { return SomeValueNotSoLong; });", 18001 LLVMWithBeforeLambdaBody); 18002 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 18003 " []()\n" 18004 " {\n" 18005 " return " 18006 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 18007 "eConsiderAsInline;\n" 18008 " });", 18009 LLVMWithBeforeLambdaBody); 18010 verifyFormat( 18011 "FctWithLongLineInLambda_SLS_All(\n" 18012 " []()\n" 18013 " {\n" 18014 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 18015 " AndShouldNotBeConsiderAsInline,\n" 18016 " LambdaBodyMustBeBreak);\n" 18017 " });", 18018 LLVMWithBeforeLambdaBody); 18019 verifyFormat("FctWithTwoParams_SLS_All(\n" 18020 " []()\n" 18021 " {\n" 18022 " // A cool function...\n" 18023 " return 43;\n" 18024 " },\n" 18025 " 87);", 18026 LLVMWithBeforeLambdaBody); 18027 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 18028 LLVMWithBeforeLambdaBody); 18029 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 18030 LLVMWithBeforeLambdaBody); 18031 verifyFormat( 18032 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 18033 LLVMWithBeforeLambdaBody); 18034 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 18035 "}); }, x);", 18036 LLVMWithBeforeLambdaBody); 18037 verifyFormat("TwoNestedLambdas_SLS_All(\n" 18038 " []()\n" 18039 " {\n" 18040 " // A cool function...\n" 18041 " return Call([]() { return 17; });\n" 18042 " });", 18043 LLVMWithBeforeLambdaBody); 18044 verifyFormat("TwoNestedLambdas_SLS_All(\n" 18045 " []()\n" 18046 " {\n" 18047 " return Call(\n" 18048 " []()\n" 18049 " {\n" 18050 " // A cool function...\n" 18051 " return 17;\n" 18052 " });\n" 18053 " });", 18054 LLVMWithBeforeLambdaBody); 18055 } 18056 18057 TEST_F(FormatTest, LambdaWithLineComments) { 18058 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 18059 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 18060 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 18061 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 18062 FormatStyle::ShortLambdaStyle::SLS_All; 18063 18064 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 18065 verifyFormat("auto k = []() // comment\n" 18066 "{ return; }", 18067 LLVMWithBeforeLambdaBody); 18068 verifyFormat("auto k = []() /* comment */ { return; }", 18069 LLVMWithBeforeLambdaBody); 18070 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 18071 LLVMWithBeforeLambdaBody); 18072 verifyFormat("auto k = []() // X\n" 18073 "{ return; }", 18074 LLVMWithBeforeLambdaBody); 18075 verifyFormat( 18076 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 18077 "{ return; }", 18078 LLVMWithBeforeLambdaBody); 18079 } 18080 18081 TEST_F(FormatTest, EmptyLinesInLambdas) { 18082 verifyFormat("auto lambda = []() {\n" 18083 " x(); //\n" 18084 "};", 18085 "auto lambda = []() {\n" 18086 "\n" 18087 " x(); //\n" 18088 "\n" 18089 "};"); 18090 } 18091 18092 TEST_F(FormatTest, FormatsBlocks) { 18093 FormatStyle ShortBlocks = getLLVMStyle(); 18094 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 18095 verifyFormat("int (^Block)(int, int);", ShortBlocks); 18096 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 18097 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 18098 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 18099 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 18100 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 18101 18102 verifyFormat("foo(^{ bar(); });", ShortBlocks); 18103 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 18104 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 18105 18106 verifyFormat("[operation setCompletionBlock:^{\n" 18107 " [self onOperationDone];\n" 18108 "}];"); 18109 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 18110 " [self onOperationDone];\n" 18111 "}]};"); 18112 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 18113 " f();\n" 18114 "}];"); 18115 verifyFormat("int a = [operation block:^int(int *i) {\n" 18116 " return 1;\n" 18117 "}];"); 18118 verifyFormat("[myObject doSomethingWith:arg1\n" 18119 " aaa:^int(int *a) {\n" 18120 " return 1;\n" 18121 " }\n" 18122 " bbb:f(a * bbbbbbbb)];"); 18123 18124 verifyFormat("[operation setCompletionBlock:^{\n" 18125 " [self.delegate newDataAvailable];\n" 18126 "}];", 18127 getLLVMStyleWithColumns(60)); 18128 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 18129 " NSString *path = [self sessionFilePath];\n" 18130 " if (path) {\n" 18131 " // ...\n" 18132 " }\n" 18133 "});"); 18134 verifyFormat("[[SessionService sharedService]\n" 18135 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 18136 " if (window) {\n" 18137 " [self windowDidLoad:window];\n" 18138 " } else {\n" 18139 " [self errorLoadingWindow];\n" 18140 " }\n" 18141 " }];"); 18142 verifyFormat("void (^largeBlock)(void) = ^{\n" 18143 " // ...\n" 18144 "};\n", 18145 getLLVMStyleWithColumns(40)); 18146 verifyFormat("[[SessionService sharedService]\n" 18147 " loadWindowWithCompletionBlock: //\n" 18148 " ^(SessionWindow *window) {\n" 18149 " if (window) {\n" 18150 " [self windowDidLoad:window];\n" 18151 " } else {\n" 18152 " [self errorLoadingWindow];\n" 18153 " }\n" 18154 " }];", 18155 getLLVMStyleWithColumns(60)); 18156 verifyFormat("[myObject doSomethingWith:arg1\n" 18157 " firstBlock:^(Foo *a) {\n" 18158 " // ...\n" 18159 " int i;\n" 18160 " }\n" 18161 " secondBlock:^(Bar *b) {\n" 18162 " // ...\n" 18163 " int i;\n" 18164 " }\n" 18165 " thirdBlock:^Foo(Bar *b) {\n" 18166 " // ...\n" 18167 " int i;\n" 18168 " }];"); 18169 verifyFormat("[myObject doSomethingWith:arg1\n" 18170 " firstBlock:-1\n" 18171 " secondBlock:^(Bar *b) {\n" 18172 " // ...\n" 18173 " int i;\n" 18174 " }];"); 18175 18176 verifyFormat("f(^{\n" 18177 " @autoreleasepool {\n" 18178 " if (a) {\n" 18179 " g();\n" 18180 " }\n" 18181 " }\n" 18182 "});"); 18183 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 18184 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 18185 "};"); 18186 18187 FormatStyle FourIndent = getLLVMStyle(); 18188 FourIndent.ObjCBlockIndentWidth = 4; 18189 verifyFormat("[operation setCompletionBlock:^{\n" 18190 " [self onOperationDone];\n" 18191 "}];", 18192 FourIndent); 18193 } 18194 18195 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 18196 FormatStyle ZeroColumn = getLLVMStyle(); 18197 ZeroColumn.ColumnLimit = 0; 18198 18199 verifyFormat("[[SessionService sharedService] " 18200 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 18201 " if (window) {\n" 18202 " [self windowDidLoad:window];\n" 18203 " } else {\n" 18204 " [self errorLoadingWindow];\n" 18205 " }\n" 18206 "}];", 18207 ZeroColumn); 18208 EXPECT_EQ("[[SessionService sharedService]\n" 18209 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 18210 " if (window) {\n" 18211 " [self windowDidLoad:window];\n" 18212 " } else {\n" 18213 " [self errorLoadingWindow];\n" 18214 " }\n" 18215 " }];", 18216 format("[[SessionService sharedService]\n" 18217 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 18218 " if (window) {\n" 18219 " [self windowDidLoad:window];\n" 18220 " } else {\n" 18221 " [self errorLoadingWindow];\n" 18222 " }\n" 18223 "}];", 18224 ZeroColumn)); 18225 verifyFormat("[myObject doSomethingWith:arg1\n" 18226 " firstBlock:^(Foo *a) {\n" 18227 " // ...\n" 18228 " int i;\n" 18229 " }\n" 18230 " secondBlock:^(Bar *b) {\n" 18231 " // ...\n" 18232 " int i;\n" 18233 " }\n" 18234 " thirdBlock:^Foo(Bar *b) {\n" 18235 " // ...\n" 18236 " int i;\n" 18237 " }];", 18238 ZeroColumn); 18239 verifyFormat("f(^{\n" 18240 " @autoreleasepool {\n" 18241 " if (a) {\n" 18242 " g();\n" 18243 " }\n" 18244 " }\n" 18245 "});", 18246 ZeroColumn); 18247 verifyFormat("void (^largeBlock)(void) = ^{\n" 18248 " // ...\n" 18249 "};", 18250 ZeroColumn); 18251 18252 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 18253 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 18254 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 18255 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 18256 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 18257 " int i;\n" 18258 "};", 18259 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 18260 } 18261 18262 TEST_F(FormatTest, SupportsCRLF) { 18263 EXPECT_EQ("int a;\r\n" 18264 "int b;\r\n" 18265 "int c;\r\n", 18266 format("int a;\r\n" 18267 " int b;\r\n" 18268 " int c;\r\n", 18269 getLLVMStyle())); 18270 EXPECT_EQ("int a;\r\n" 18271 "int b;\r\n" 18272 "int c;\r\n", 18273 format("int a;\r\n" 18274 " int b;\n" 18275 " int c;\r\n", 18276 getLLVMStyle())); 18277 EXPECT_EQ("int a;\n" 18278 "int b;\n" 18279 "int c;\n", 18280 format("int a;\r\n" 18281 " int b;\n" 18282 " int c;\n", 18283 getLLVMStyle())); 18284 EXPECT_EQ("\"aaaaaaa \"\r\n" 18285 "\"bbbbbbb\";\r\n", 18286 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 18287 EXPECT_EQ("#define A \\\r\n" 18288 " b; \\\r\n" 18289 " c; \\\r\n" 18290 " d;\r\n", 18291 format("#define A \\\r\n" 18292 " b; \\\r\n" 18293 " c; d; \r\n", 18294 getGoogleStyle())); 18295 18296 EXPECT_EQ("/*\r\n" 18297 "multi line block comments\r\n" 18298 "should not introduce\r\n" 18299 "an extra carriage return\r\n" 18300 "*/\r\n", 18301 format("/*\r\n" 18302 "multi line block comments\r\n" 18303 "should not introduce\r\n" 18304 "an extra carriage return\r\n" 18305 "*/\r\n")); 18306 EXPECT_EQ("/*\r\n" 18307 "\r\n" 18308 "*/", 18309 format("/*\r\n" 18310 " \r\r\r\n" 18311 "*/")); 18312 18313 FormatStyle style = getLLVMStyle(); 18314 18315 style.DeriveLineEnding = true; 18316 style.UseCRLF = false; 18317 EXPECT_EQ("union FooBarBazQux {\n" 18318 " int foo;\n" 18319 " int bar;\n" 18320 " int baz;\n" 18321 "};", 18322 format("union FooBarBazQux {\r\n" 18323 " int foo;\n" 18324 " int bar;\r\n" 18325 " int baz;\n" 18326 "};", 18327 style)); 18328 style.UseCRLF = true; 18329 EXPECT_EQ("union FooBarBazQux {\r\n" 18330 " int foo;\r\n" 18331 " int bar;\r\n" 18332 " int baz;\r\n" 18333 "};", 18334 format("union FooBarBazQux {\r\n" 18335 " int foo;\n" 18336 " int bar;\r\n" 18337 " int baz;\n" 18338 "};", 18339 style)); 18340 18341 style.DeriveLineEnding = false; 18342 style.UseCRLF = false; 18343 EXPECT_EQ("union FooBarBazQux {\n" 18344 " int foo;\n" 18345 " int bar;\n" 18346 " int baz;\n" 18347 " int qux;\n" 18348 "};", 18349 format("union FooBarBazQux {\r\n" 18350 " int foo;\n" 18351 " int bar;\r\n" 18352 " int baz;\n" 18353 " int qux;\r\n" 18354 "};", 18355 style)); 18356 style.UseCRLF = true; 18357 EXPECT_EQ("union FooBarBazQux {\r\n" 18358 " int foo;\r\n" 18359 " int bar;\r\n" 18360 " int baz;\r\n" 18361 " int qux;\r\n" 18362 "};", 18363 format("union FooBarBazQux {\r\n" 18364 " int foo;\n" 18365 " int bar;\r\n" 18366 " int baz;\n" 18367 " int qux;\n" 18368 "};", 18369 style)); 18370 18371 style.DeriveLineEnding = true; 18372 style.UseCRLF = false; 18373 EXPECT_EQ("union FooBarBazQux {\r\n" 18374 " int foo;\r\n" 18375 " int bar;\r\n" 18376 " int baz;\r\n" 18377 " int qux;\r\n" 18378 "};", 18379 format("union FooBarBazQux {\r\n" 18380 " int foo;\n" 18381 " int bar;\r\n" 18382 " int baz;\n" 18383 " int qux;\r\n" 18384 "};", 18385 style)); 18386 style.UseCRLF = true; 18387 EXPECT_EQ("union FooBarBazQux {\n" 18388 " int foo;\n" 18389 " int bar;\n" 18390 " int baz;\n" 18391 " int qux;\n" 18392 "};", 18393 format("union FooBarBazQux {\r\n" 18394 " int foo;\n" 18395 " int bar;\r\n" 18396 " int baz;\n" 18397 " int qux;\n" 18398 "};", 18399 style)); 18400 } 18401 18402 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 18403 verifyFormat("MY_CLASS(C) {\n" 18404 " int i;\n" 18405 " int j;\n" 18406 "};"); 18407 } 18408 18409 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 18410 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 18411 TwoIndent.ContinuationIndentWidth = 2; 18412 18413 EXPECT_EQ("int i =\n" 18414 " longFunction(\n" 18415 " arg);", 18416 format("int i = longFunction(arg);", TwoIndent)); 18417 18418 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 18419 SixIndent.ContinuationIndentWidth = 6; 18420 18421 EXPECT_EQ("int i =\n" 18422 " longFunction(\n" 18423 " arg);", 18424 format("int i = longFunction(arg);", SixIndent)); 18425 } 18426 18427 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 18428 FormatStyle Style = getLLVMStyle(); 18429 verifyFormat("int Foo::getter(\n" 18430 " //\n" 18431 ") const {\n" 18432 " return foo;\n" 18433 "}", 18434 Style); 18435 verifyFormat("void Foo::setter(\n" 18436 " //\n" 18437 ") {\n" 18438 " foo = 1;\n" 18439 "}", 18440 Style); 18441 } 18442 18443 TEST_F(FormatTest, SpacesInAngles) { 18444 FormatStyle Spaces = getLLVMStyle(); 18445 Spaces.SpacesInAngles = true; 18446 18447 verifyFormat("vector< ::std::string > x1;", Spaces); 18448 verifyFormat("Foo< int, Bar > x2;", Spaces); 18449 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 18450 18451 verifyFormat("static_cast< int >(arg);", Spaces); 18452 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 18453 verifyFormat("f< int, float >();", Spaces); 18454 verifyFormat("template <> g() {}", Spaces); 18455 verifyFormat("template < std::vector< int > > f() {}", Spaces); 18456 verifyFormat("std::function< void(int, int) > fct;", Spaces); 18457 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 18458 Spaces); 18459 18460 Spaces.Standard = FormatStyle::LS_Cpp03; 18461 Spaces.SpacesInAngles = true; 18462 verifyFormat("A< A< int > >();", Spaces); 18463 18464 Spaces.SpacesInAngles = false; 18465 verifyFormat("A<A<int> >();", Spaces); 18466 18467 Spaces.Standard = FormatStyle::LS_Cpp11; 18468 Spaces.SpacesInAngles = true; 18469 verifyFormat("A< A< int > >();", Spaces); 18470 18471 Spaces.SpacesInAngles = false; 18472 verifyFormat("vector<::std::string> x4;", Spaces); 18473 verifyFormat("vector<int> x5;", Spaces); 18474 verifyFormat("Foo<int, Bar> x6;", Spaces); 18475 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 18476 18477 verifyFormat("A<A<int>>();", Spaces); 18478 } 18479 18480 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 18481 FormatStyle Style = getLLVMStyle(); 18482 Style.SpaceAfterTemplateKeyword = false; 18483 verifyFormat("template<int> void foo();", Style); 18484 } 18485 18486 TEST_F(FormatTest, TripleAngleBrackets) { 18487 verifyFormat("f<<<1, 1>>>();"); 18488 verifyFormat("f<<<1, 1, 1, s>>>();"); 18489 verifyFormat("f<<<a, b, c, d>>>();"); 18490 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 18491 verifyFormat("f<param><<<1, 1>>>();"); 18492 verifyFormat("f<1><<<1, 1>>>();"); 18493 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 18494 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 18495 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 18496 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 18497 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 18498 } 18499 18500 TEST_F(FormatTest, MergeLessLessAtEnd) { 18501 verifyFormat("<<"); 18502 EXPECT_EQ("< < <", format("\\\n<<<")); 18503 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 18504 "aaallvm::outs() <<"); 18505 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 18506 "aaaallvm::outs()\n <<"); 18507 } 18508 18509 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 18510 std::string code = "#if A\n" 18511 "#if B\n" 18512 "a.\n" 18513 "#endif\n" 18514 " a = 1;\n" 18515 "#else\n" 18516 "#endif\n" 18517 "#if C\n" 18518 "#else\n" 18519 "#endif\n"; 18520 EXPECT_EQ(code, format(code)); 18521 } 18522 18523 TEST_F(FormatTest, HandleConflictMarkers) { 18524 // Git/SVN conflict markers. 18525 EXPECT_EQ("int a;\n" 18526 "void f() {\n" 18527 " callme(some(parameter1,\n" 18528 "<<<<<<< text by the vcs\n" 18529 " parameter2),\n" 18530 "||||||| text by the vcs\n" 18531 " parameter2),\n" 18532 " parameter3,\n" 18533 "======= text by the vcs\n" 18534 " parameter2, parameter3),\n" 18535 ">>>>>>> text by the vcs\n" 18536 " otherparameter);\n", 18537 format("int a;\n" 18538 "void f() {\n" 18539 " callme(some(parameter1,\n" 18540 "<<<<<<< text by the vcs\n" 18541 " parameter2),\n" 18542 "||||||| text by the vcs\n" 18543 " parameter2),\n" 18544 " parameter3,\n" 18545 "======= text by the vcs\n" 18546 " parameter2,\n" 18547 " parameter3),\n" 18548 ">>>>>>> text by the vcs\n" 18549 " otherparameter);\n")); 18550 18551 // Perforce markers. 18552 EXPECT_EQ("void f() {\n" 18553 " function(\n" 18554 ">>>> text by the vcs\n" 18555 " parameter,\n" 18556 "==== text by the vcs\n" 18557 " parameter,\n" 18558 "==== text by the vcs\n" 18559 " parameter,\n" 18560 "<<<< text by the vcs\n" 18561 " parameter);\n", 18562 format("void f() {\n" 18563 " function(\n" 18564 ">>>> text by the vcs\n" 18565 " parameter,\n" 18566 "==== text by the vcs\n" 18567 " parameter,\n" 18568 "==== text by the vcs\n" 18569 " parameter,\n" 18570 "<<<< text by the vcs\n" 18571 " parameter);\n")); 18572 18573 EXPECT_EQ("<<<<<<<\n" 18574 "|||||||\n" 18575 "=======\n" 18576 ">>>>>>>", 18577 format("<<<<<<<\n" 18578 "|||||||\n" 18579 "=======\n" 18580 ">>>>>>>")); 18581 18582 EXPECT_EQ("<<<<<<<\n" 18583 "|||||||\n" 18584 "int i;\n" 18585 "=======\n" 18586 ">>>>>>>", 18587 format("<<<<<<<\n" 18588 "|||||||\n" 18589 "int i;\n" 18590 "=======\n" 18591 ">>>>>>>")); 18592 18593 // FIXME: Handle parsing of macros around conflict markers correctly: 18594 EXPECT_EQ("#define Macro \\\n" 18595 "<<<<<<<\n" 18596 "Something \\\n" 18597 "|||||||\n" 18598 "Else \\\n" 18599 "=======\n" 18600 "Other \\\n" 18601 ">>>>>>>\n" 18602 " End int i;\n", 18603 format("#define Macro \\\n" 18604 "<<<<<<<\n" 18605 " Something \\\n" 18606 "|||||||\n" 18607 " Else \\\n" 18608 "=======\n" 18609 " Other \\\n" 18610 ">>>>>>>\n" 18611 " End\n" 18612 "int i;\n")); 18613 } 18614 18615 TEST_F(FormatTest, DisableRegions) { 18616 EXPECT_EQ("int i;\n" 18617 "// clang-format off\n" 18618 " int j;\n" 18619 "// clang-format on\n" 18620 "int k;", 18621 format(" int i;\n" 18622 " // clang-format off\n" 18623 " int j;\n" 18624 " // clang-format on\n" 18625 " int k;")); 18626 EXPECT_EQ("int i;\n" 18627 "/* clang-format off */\n" 18628 " int j;\n" 18629 "/* clang-format on */\n" 18630 "int k;", 18631 format(" int i;\n" 18632 " /* clang-format off */\n" 18633 " int j;\n" 18634 " /* clang-format on */\n" 18635 " int k;")); 18636 18637 // Don't reflow comments within disabled regions. 18638 EXPECT_EQ("// clang-format off\n" 18639 "// long long long long long long line\n" 18640 "/* clang-format on */\n" 18641 "/* long long long\n" 18642 " * long long long\n" 18643 " * line */\n" 18644 "int i;\n" 18645 "/* clang-format off */\n" 18646 "/* long long long long long long line */\n", 18647 format("// clang-format off\n" 18648 "// long long long long long long line\n" 18649 "/* clang-format on */\n" 18650 "/* long long long long long long line */\n" 18651 "int i;\n" 18652 "/* clang-format off */\n" 18653 "/* long long long long long long line */\n", 18654 getLLVMStyleWithColumns(20))); 18655 } 18656 18657 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 18658 format("? ) ="); 18659 verifyNoCrash("#define a\\\n /**/}"); 18660 } 18661 18662 TEST_F(FormatTest, FormatsTableGenCode) { 18663 FormatStyle Style = getLLVMStyle(); 18664 Style.Language = FormatStyle::LK_TableGen; 18665 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 18666 } 18667 18668 TEST_F(FormatTest, ArrayOfTemplates) { 18669 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 18670 format("auto a = new unique_ptr<int > [ 10];")); 18671 18672 FormatStyle Spaces = getLLVMStyle(); 18673 Spaces.SpacesInSquareBrackets = true; 18674 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 18675 format("auto a = new unique_ptr<int > [10];", Spaces)); 18676 } 18677 18678 TEST_F(FormatTest, ArrayAsTemplateType) { 18679 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 18680 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 18681 18682 FormatStyle Spaces = getLLVMStyle(); 18683 Spaces.SpacesInSquareBrackets = true; 18684 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 18685 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 18686 } 18687 18688 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 18689 18690 TEST(FormatStyle, GetStyleWithEmptyFileName) { 18691 llvm::vfs::InMemoryFileSystem FS; 18692 auto Style1 = getStyle("file", "", "Google", "", &FS); 18693 ASSERT_TRUE((bool)Style1); 18694 ASSERT_EQ(*Style1, getGoogleStyle()); 18695 } 18696 18697 TEST(FormatStyle, GetStyleOfFile) { 18698 llvm::vfs::InMemoryFileSystem FS; 18699 // Test 1: format file in the same directory. 18700 ASSERT_TRUE( 18701 FS.addFile("/a/.clang-format", 0, 18702 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 18703 ASSERT_TRUE( 18704 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 18705 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 18706 ASSERT_TRUE((bool)Style1); 18707 ASSERT_EQ(*Style1, getLLVMStyle()); 18708 18709 // Test 2.1: fallback to default. 18710 ASSERT_TRUE( 18711 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 18712 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 18713 ASSERT_TRUE((bool)Style2); 18714 ASSERT_EQ(*Style2, getMozillaStyle()); 18715 18716 // Test 2.2: no format on 'none' fallback style. 18717 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 18718 ASSERT_TRUE((bool)Style2); 18719 ASSERT_EQ(*Style2, getNoStyle()); 18720 18721 // Test 2.3: format if config is found with no based style while fallback is 18722 // 'none'. 18723 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 18724 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 18725 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 18726 ASSERT_TRUE((bool)Style2); 18727 ASSERT_EQ(*Style2, getLLVMStyle()); 18728 18729 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 18730 Style2 = getStyle("{}", "a.h", "none", "", &FS); 18731 ASSERT_TRUE((bool)Style2); 18732 ASSERT_EQ(*Style2, getLLVMStyle()); 18733 18734 // Test 3: format file in parent directory. 18735 ASSERT_TRUE( 18736 FS.addFile("/c/.clang-format", 0, 18737 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 18738 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 18739 llvm::MemoryBuffer::getMemBuffer("int i;"))); 18740 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 18741 ASSERT_TRUE((bool)Style3); 18742 ASSERT_EQ(*Style3, getGoogleStyle()); 18743 18744 // Test 4: error on invalid fallback style 18745 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 18746 ASSERT_FALSE((bool)Style4); 18747 llvm::consumeError(Style4.takeError()); 18748 18749 // Test 5: error on invalid yaml on command line 18750 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 18751 ASSERT_FALSE((bool)Style5); 18752 llvm::consumeError(Style5.takeError()); 18753 18754 // Test 6: error on invalid style 18755 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 18756 ASSERT_FALSE((bool)Style6); 18757 llvm::consumeError(Style6.takeError()); 18758 18759 // Test 7: found config file, error on parsing it 18760 ASSERT_TRUE( 18761 FS.addFile("/d/.clang-format", 0, 18762 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 18763 "InvalidKey: InvalidValue"))); 18764 ASSERT_TRUE( 18765 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 18766 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 18767 ASSERT_FALSE((bool)Style7a); 18768 llvm::consumeError(Style7a.takeError()); 18769 18770 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 18771 ASSERT_TRUE((bool)Style7b); 18772 18773 // Test 8: inferred per-language defaults apply. 18774 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 18775 ASSERT_TRUE((bool)StyleTd); 18776 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 18777 18778 // Test 9.1: overwriting a file style, when parent no file exists with no 18779 // fallback style 18780 ASSERT_TRUE(FS.addFile( 18781 "/e/sub/.clang-format", 0, 18782 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 18783 "ColumnLimit: 20"))); 18784 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 18785 llvm::MemoryBuffer::getMemBuffer("int i;"))); 18786 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 18787 ASSERT_TRUE(static_cast<bool>(Style9)); 18788 ASSERT_EQ(*Style9, [] { 18789 auto Style = getNoStyle(); 18790 Style.ColumnLimit = 20; 18791 return Style; 18792 }()); 18793 18794 // Test 9.2: with LLVM fallback style 18795 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 18796 ASSERT_TRUE(static_cast<bool>(Style9)); 18797 ASSERT_EQ(*Style9, [] { 18798 auto Style = getLLVMStyle(); 18799 Style.ColumnLimit = 20; 18800 return Style; 18801 }()); 18802 18803 // Test 9.3: with a parent file 18804 ASSERT_TRUE( 18805 FS.addFile("/e/.clang-format", 0, 18806 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 18807 "UseTab: Always"))); 18808 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 18809 ASSERT_TRUE(static_cast<bool>(Style9)); 18810 ASSERT_EQ(*Style9, [] { 18811 auto Style = getGoogleStyle(); 18812 Style.ColumnLimit = 20; 18813 Style.UseTab = FormatStyle::UT_Always; 18814 return Style; 18815 }()); 18816 18817 // Test 9.4: propagate more than one level 18818 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 18819 llvm::MemoryBuffer::getMemBuffer("int i;"))); 18820 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 18821 llvm::MemoryBuffer::getMemBuffer( 18822 "BasedOnStyle: InheritParentConfig\n" 18823 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 18824 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 18825 18826 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 18827 auto Style = getGoogleStyle(); 18828 Style.ColumnLimit = 20; 18829 Style.UseTab = FormatStyle::UT_Always; 18830 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 18831 return Style; 18832 }(); 18833 18834 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 18835 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 18836 ASSERT_TRUE(static_cast<bool>(Style9)); 18837 ASSERT_EQ(*Style9, SubSubStyle); 18838 18839 // Test 9.5: use InheritParentConfig as style name 18840 Style9 = 18841 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 18842 ASSERT_TRUE(static_cast<bool>(Style9)); 18843 ASSERT_EQ(*Style9, SubSubStyle); 18844 18845 // Test 9.6: use command line style with inheritance 18846 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 18847 "none", "", &FS); 18848 ASSERT_TRUE(static_cast<bool>(Style9)); 18849 ASSERT_EQ(*Style9, SubSubStyle); 18850 18851 // Test 9.7: use command line style with inheritance and own config 18852 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 18853 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 18854 "/e/sub/code.cpp", "none", "", &FS); 18855 ASSERT_TRUE(static_cast<bool>(Style9)); 18856 ASSERT_EQ(*Style9, SubSubStyle); 18857 18858 // Test 9.8: use inheritance from a file without BasedOnStyle 18859 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 18860 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 18861 ASSERT_TRUE( 18862 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 18863 llvm::MemoryBuffer::getMemBuffer( 18864 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 18865 // Make sure we do not use the fallback style 18866 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 18867 ASSERT_TRUE(static_cast<bool>(Style9)); 18868 ASSERT_EQ(*Style9, [] { 18869 auto Style = getLLVMStyle(); 18870 Style.ColumnLimit = 123; 18871 return Style; 18872 }()); 18873 18874 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 18875 ASSERT_TRUE(static_cast<bool>(Style9)); 18876 ASSERT_EQ(*Style9, [] { 18877 auto Style = getLLVMStyle(); 18878 Style.ColumnLimit = 123; 18879 Style.IndentWidth = 7; 18880 return Style; 18881 }()); 18882 } 18883 18884 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 18885 // Column limit is 20. 18886 std::string Code = "Type *a =\n" 18887 " new Type();\n" 18888 "g(iiiii, 0, jjjjj,\n" 18889 " 0, kkkkk, 0, mm);\n" 18890 "int bad = format ;"; 18891 std::string Expected = "auto a = new Type();\n" 18892 "g(iiiii, nullptr,\n" 18893 " jjjjj, nullptr,\n" 18894 " kkkkk, nullptr,\n" 18895 " mm);\n" 18896 "int bad = format ;"; 18897 FileID ID = Context.createInMemoryFile("format.cpp", Code); 18898 tooling::Replacements Replaces = toReplacements( 18899 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 18900 "auto "), 18901 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 18902 "nullptr"), 18903 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 18904 "nullptr"), 18905 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 18906 "nullptr")}); 18907 18908 format::FormatStyle Style = format::getLLVMStyle(); 18909 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 18910 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 18911 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 18912 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 18913 auto Result = applyAllReplacements(Code, *FormattedReplaces); 18914 EXPECT_TRUE(static_cast<bool>(Result)); 18915 EXPECT_EQ(Expected, *Result); 18916 } 18917 18918 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 18919 std::string Code = "#include \"a.h\"\n" 18920 "#include \"c.h\"\n" 18921 "\n" 18922 "int main() {\n" 18923 " return 0;\n" 18924 "}"; 18925 std::string Expected = "#include \"a.h\"\n" 18926 "#include \"b.h\"\n" 18927 "#include \"c.h\"\n" 18928 "\n" 18929 "int main() {\n" 18930 " return 0;\n" 18931 "}"; 18932 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 18933 tooling::Replacements Replaces = toReplacements( 18934 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 18935 "#include \"b.h\"\n")}); 18936 18937 format::FormatStyle Style = format::getLLVMStyle(); 18938 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 18939 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 18940 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 18941 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 18942 auto Result = applyAllReplacements(Code, *FormattedReplaces); 18943 EXPECT_TRUE(static_cast<bool>(Result)); 18944 EXPECT_EQ(Expected, *Result); 18945 } 18946 18947 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 18948 EXPECT_EQ("using std::cin;\n" 18949 "using std::cout;", 18950 format("using std::cout;\n" 18951 "using std::cin;", 18952 getGoogleStyle())); 18953 } 18954 18955 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 18956 format::FormatStyle Style = format::getLLVMStyle(); 18957 Style.Standard = FormatStyle::LS_Cpp03; 18958 // cpp03 recognize this string as identifier u8 and literal character 'a' 18959 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 18960 } 18961 18962 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 18963 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 18964 // all modes, including C++11, C++14 and C++17 18965 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 18966 } 18967 18968 TEST_F(FormatTest, DoNotFormatLikelyXml) { 18969 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 18970 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 18971 } 18972 18973 TEST_F(FormatTest, StructuredBindings) { 18974 // Structured bindings is a C++17 feature. 18975 // all modes, including C++11, C++14 and C++17 18976 verifyFormat("auto [a, b] = f();"); 18977 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 18978 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 18979 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 18980 EXPECT_EQ("auto const volatile [a, b] = f();", 18981 format("auto const volatile[a, b] = f();")); 18982 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 18983 EXPECT_EQ("auto &[a, b, c] = f();", 18984 format("auto &[ a , b,c ] = f();")); 18985 EXPECT_EQ("auto &&[a, b, c] = f();", 18986 format("auto &&[ a , b,c ] = f();")); 18987 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 18988 EXPECT_EQ("auto const volatile &&[a, b] = f();", 18989 format("auto const volatile &&[a, b] = f();")); 18990 EXPECT_EQ("auto const &&[a, b] = f();", 18991 format("auto const && [a, b] = f();")); 18992 EXPECT_EQ("const auto &[a, b] = f();", 18993 format("const auto & [a, b] = f();")); 18994 EXPECT_EQ("const auto volatile &&[a, b] = f();", 18995 format("const auto volatile &&[a, b] = f();")); 18996 EXPECT_EQ("volatile const auto &&[a, b] = f();", 18997 format("volatile const auto &&[a, b] = f();")); 18998 EXPECT_EQ("const auto &&[a, b] = f();", 18999 format("const auto && [a, b] = f();")); 19000 19001 // Make sure we don't mistake structured bindings for lambdas. 19002 FormatStyle PointerMiddle = getLLVMStyle(); 19003 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 19004 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 19005 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 19006 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 19007 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 19008 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 19009 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 19010 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 19011 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 19012 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 19013 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 19014 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 19015 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 19016 19017 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 19018 format("for (const auto && [a, b] : some_range) {\n}")); 19019 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 19020 format("for (const auto & [a, b] : some_range) {\n}")); 19021 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 19022 format("for (const auto[a, b] : some_range) {\n}")); 19023 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 19024 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 19025 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 19026 EXPECT_EQ("auto const &[x, y](expr);", 19027 format("auto const & [x,y] (expr);")); 19028 EXPECT_EQ("auto const &&[x, y](expr);", 19029 format("auto const && [x,y] (expr);")); 19030 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 19031 EXPECT_EQ("auto const &[x, y]{expr};", 19032 format("auto const & [x,y] {expr};")); 19033 EXPECT_EQ("auto const &&[x, y]{expr};", 19034 format("auto const && [x,y] {expr};")); 19035 19036 format::FormatStyle Spaces = format::getLLVMStyle(); 19037 Spaces.SpacesInSquareBrackets = true; 19038 verifyFormat("auto [ a, b ] = f();", Spaces); 19039 verifyFormat("auto &&[ a, b ] = f();", Spaces); 19040 verifyFormat("auto &[ a, b ] = f();", Spaces); 19041 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 19042 verifyFormat("auto const &[ a, b ] = f();", Spaces); 19043 } 19044 19045 TEST_F(FormatTest, FileAndCode) { 19046 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 19047 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 19048 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 19049 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 19050 EXPECT_EQ(FormatStyle::LK_ObjC, 19051 guessLanguage("foo.h", "@interface Foo\n@end\n")); 19052 EXPECT_EQ( 19053 FormatStyle::LK_ObjC, 19054 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 19055 EXPECT_EQ(FormatStyle::LK_ObjC, 19056 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 19057 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 19058 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 19059 EXPECT_EQ(FormatStyle::LK_ObjC, 19060 guessLanguage("foo", "@interface Foo\n@end\n")); 19061 EXPECT_EQ(FormatStyle::LK_ObjC, 19062 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 19063 EXPECT_EQ( 19064 FormatStyle::LK_ObjC, 19065 guessLanguage("foo.h", 19066 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 19067 EXPECT_EQ( 19068 FormatStyle::LK_Cpp, 19069 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 19070 } 19071 19072 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 19073 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 19074 EXPECT_EQ(FormatStyle::LK_ObjC, 19075 guessLanguage("foo.h", "array[[calculator getIndex]];")); 19076 EXPECT_EQ(FormatStyle::LK_Cpp, 19077 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 19078 EXPECT_EQ( 19079 FormatStyle::LK_Cpp, 19080 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 19081 EXPECT_EQ(FormatStyle::LK_ObjC, 19082 guessLanguage("foo.h", "[[noreturn foo] bar];")); 19083 EXPECT_EQ(FormatStyle::LK_Cpp, 19084 guessLanguage("foo.h", "[[clang::fallthrough]];")); 19085 EXPECT_EQ(FormatStyle::LK_ObjC, 19086 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 19087 EXPECT_EQ(FormatStyle::LK_Cpp, 19088 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 19089 EXPECT_EQ(FormatStyle::LK_Cpp, 19090 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 19091 EXPECT_EQ(FormatStyle::LK_ObjC, 19092 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 19093 EXPECT_EQ(FormatStyle::LK_Cpp, 19094 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 19095 EXPECT_EQ( 19096 FormatStyle::LK_Cpp, 19097 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 19098 EXPECT_EQ( 19099 FormatStyle::LK_Cpp, 19100 guessLanguage("foo.h", 19101 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 19102 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 19103 } 19104 19105 TEST_F(FormatTest, GuessLanguageWithCaret) { 19106 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 19107 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 19108 EXPECT_EQ(FormatStyle::LK_ObjC, 19109 guessLanguage("foo.h", "int(^)(char, float);")); 19110 EXPECT_EQ(FormatStyle::LK_ObjC, 19111 guessLanguage("foo.h", "int(^foo)(char, float);")); 19112 EXPECT_EQ(FormatStyle::LK_ObjC, 19113 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 19114 EXPECT_EQ(FormatStyle::LK_ObjC, 19115 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 19116 EXPECT_EQ( 19117 FormatStyle::LK_ObjC, 19118 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 19119 } 19120 19121 TEST_F(FormatTest, GuessLanguageWithPragmas) { 19122 EXPECT_EQ(FormatStyle::LK_Cpp, 19123 guessLanguage("foo.h", "__pragma(warning(disable:))")); 19124 EXPECT_EQ(FormatStyle::LK_Cpp, 19125 guessLanguage("foo.h", "#pragma(warning(disable:))")); 19126 EXPECT_EQ(FormatStyle::LK_Cpp, 19127 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 19128 } 19129 19130 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 19131 // ASM symbolic names are identifiers that must be surrounded by [] without 19132 // space in between: 19133 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 19134 19135 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 19136 verifyFormat(R"(// 19137 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 19138 )"); 19139 19140 // A list of several ASM symbolic names. 19141 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 19142 19143 // ASM symbolic names in inline ASM with inputs and outputs. 19144 verifyFormat(R"(// 19145 asm("cmoveq %1, %2, %[result]" 19146 : [result] "=r"(result) 19147 : "r"(test), "r"(new), "[result]"(old)); 19148 )"); 19149 19150 // ASM symbolic names in inline ASM with no outputs. 19151 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 19152 } 19153 19154 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 19155 EXPECT_EQ(FormatStyle::LK_Cpp, 19156 guessLanguage("foo.h", "void f() {\n" 19157 " asm (\"mov %[e], %[d]\"\n" 19158 " : [d] \"=rm\" (d)\n" 19159 " [e] \"rm\" (*e));\n" 19160 "}")); 19161 EXPECT_EQ(FormatStyle::LK_Cpp, 19162 guessLanguage("foo.h", "void f() {\n" 19163 " _asm (\"mov %[e], %[d]\"\n" 19164 " : [d] \"=rm\" (d)\n" 19165 " [e] \"rm\" (*e));\n" 19166 "}")); 19167 EXPECT_EQ(FormatStyle::LK_Cpp, 19168 guessLanguage("foo.h", "void f() {\n" 19169 " __asm (\"mov %[e], %[d]\"\n" 19170 " : [d] \"=rm\" (d)\n" 19171 " [e] \"rm\" (*e));\n" 19172 "}")); 19173 EXPECT_EQ(FormatStyle::LK_Cpp, 19174 guessLanguage("foo.h", "void f() {\n" 19175 " __asm__ (\"mov %[e], %[d]\"\n" 19176 " : [d] \"=rm\" (d)\n" 19177 " [e] \"rm\" (*e));\n" 19178 "}")); 19179 EXPECT_EQ(FormatStyle::LK_Cpp, 19180 guessLanguage("foo.h", "void f() {\n" 19181 " asm (\"mov %[e], %[d]\"\n" 19182 " : [d] \"=rm\" (d),\n" 19183 " [e] \"rm\" (*e));\n" 19184 "}")); 19185 EXPECT_EQ(FormatStyle::LK_Cpp, 19186 guessLanguage("foo.h", "void f() {\n" 19187 " asm volatile (\"mov %[e], %[d]\"\n" 19188 " : [d] \"=rm\" (d)\n" 19189 " [e] \"rm\" (*e));\n" 19190 "}")); 19191 } 19192 19193 TEST_F(FormatTest, GuessLanguageWithChildLines) { 19194 EXPECT_EQ(FormatStyle::LK_Cpp, 19195 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 19196 EXPECT_EQ(FormatStyle::LK_ObjC, 19197 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 19198 EXPECT_EQ( 19199 FormatStyle::LK_Cpp, 19200 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 19201 EXPECT_EQ( 19202 FormatStyle::LK_ObjC, 19203 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 19204 } 19205 19206 TEST_F(FormatTest, TypenameMacros) { 19207 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 19208 19209 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 19210 FormatStyle Google = getGoogleStyleWithColumns(0); 19211 Google.TypenameMacros = TypenameMacros; 19212 verifyFormat("struct foo {\n" 19213 " int bar;\n" 19214 " TAILQ_ENTRY(a) bleh;\n" 19215 "};", 19216 Google); 19217 19218 FormatStyle Macros = getLLVMStyle(); 19219 Macros.TypenameMacros = TypenameMacros; 19220 19221 verifyFormat("STACK_OF(int) a;", Macros); 19222 verifyFormat("STACK_OF(int) *a;", Macros); 19223 verifyFormat("STACK_OF(int const *) *a;", Macros); 19224 verifyFormat("STACK_OF(int *const) *a;", Macros); 19225 verifyFormat("STACK_OF(int, string) a;", Macros); 19226 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 19227 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 19228 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 19229 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 19230 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 19231 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 19232 19233 Macros.PointerAlignment = FormatStyle::PAS_Left; 19234 verifyFormat("STACK_OF(int)* a;", Macros); 19235 verifyFormat("STACK_OF(int*)* a;", Macros); 19236 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 19237 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 19238 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 19239 } 19240 19241 TEST_F(FormatTest, AtomicQualifier) { 19242 // Check that we treate _Atomic as a type and not a function call 19243 FormatStyle Google = getGoogleStyleWithColumns(0); 19244 verifyFormat("struct foo {\n" 19245 " int a1;\n" 19246 " _Atomic(a) a2;\n" 19247 " _Atomic(_Atomic(int) *const) a3;\n" 19248 "};", 19249 Google); 19250 verifyFormat("_Atomic(uint64_t) a;"); 19251 verifyFormat("_Atomic(uint64_t) *a;"); 19252 verifyFormat("_Atomic(uint64_t const *) *a;"); 19253 verifyFormat("_Atomic(uint64_t *const) *a;"); 19254 verifyFormat("_Atomic(const uint64_t *) *a;"); 19255 verifyFormat("_Atomic(uint64_t) a;"); 19256 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 19257 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 19258 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 19259 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 19260 19261 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 19262 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 19263 FormatStyle Style = getLLVMStyle(); 19264 Style.PointerAlignment = FormatStyle::PAS_Left; 19265 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 19266 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 19267 verifyFormat("_Atomic(int)* a;", Style); 19268 verifyFormat("_Atomic(int*)* a;", Style); 19269 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 19270 19271 Style.SpacesInCStyleCastParentheses = true; 19272 Style.SpacesInParentheses = false; 19273 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 19274 Style.SpacesInCStyleCastParentheses = false; 19275 Style.SpacesInParentheses = true; 19276 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 19277 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 19278 } 19279 19280 TEST_F(FormatTest, AmbersandInLamda) { 19281 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 19282 FormatStyle AlignStyle = getLLVMStyle(); 19283 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 19284 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 19285 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 19286 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 19287 } 19288 19289 TEST_F(FormatTest, SpacesInConditionalStatement) { 19290 FormatStyle Spaces = getLLVMStyle(); 19291 Spaces.SpacesInConditionalStatement = true; 19292 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 19293 verifyFormat("if ( !a )\n return;", Spaces); 19294 verifyFormat("if ( a )\n return;", Spaces); 19295 verifyFormat("if constexpr ( a )\n return;", Spaces); 19296 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 19297 verifyFormat("while ( a )\n return;", Spaces); 19298 verifyFormat("while ( (a && b) )\n return;", Spaces); 19299 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 19300 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 19301 // Check that space on the left of "::" is inserted as expected at beginning 19302 // of condition. 19303 verifyFormat("while ( ::func() )\n return;", Spaces); 19304 } 19305 19306 TEST_F(FormatTest, AlternativeOperators) { 19307 // Test case for ensuring alternate operators are not 19308 // combined with their right most neighbour. 19309 verifyFormat("int a and b;"); 19310 verifyFormat("int a and_eq b;"); 19311 verifyFormat("int a bitand b;"); 19312 verifyFormat("int a bitor b;"); 19313 verifyFormat("int a compl b;"); 19314 verifyFormat("int a not b;"); 19315 verifyFormat("int a not_eq b;"); 19316 verifyFormat("int a or b;"); 19317 verifyFormat("int a xor b;"); 19318 verifyFormat("int a xor_eq b;"); 19319 verifyFormat("return this not_eq bitand other;"); 19320 verifyFormat("bool operator not_eq(const X bitand other)"); 19321 19322 verifyFormat("int a and 5;"); 19323 verifyFormat("int a and_eq 5;"); 19324 verifyFormat("int a bitand 5;"); 19325 verifyFormat("int a bitor 5;"); 19326 verifyFormat("int a compl 5;"); 19327 verifyFormat("int a not 5;"); 19328 verifyFormat("int a not_eq 5;"); 19329 verifyFormat("int a or 5;"); 19330 verifyFormat("int a xor 5;"); 19331 verifyFormat("int a xor_eq 5;"); 19332 19333 verifyFormat("int a compl(5);"); 19334 verifyFormat("int a not(5);"); 19335 19336 /* FIXME handle alternate tokens 19337 * https://en.cppreference.com/w/cpp/language/operator_alternative 19338 // alternative tokens 19339 verifyFormat("compl foo();"); // ~foo(); 19340 verifyFormat("foo() <%%>;"); // foo(); 19341 verifyFormat("void foo() <%%>;"); // void foo(){} 19342 verifyFormat("int a <:1:>;"); // int a[1];[ 19343 verifyFormat("%:define ABC abc"); // #define ABC abc 19344 verifyFormat("%:%:"); // ## 19345 */ 19346 } 19347 19348 TEST_F(FormatTest, STLWhileNotDefineChed) { 19349 verifyFormat("#if defined(while)\n" 19350 "#define while EMIT WARNING C4005\n" 19351 "#endif // while"); 19352 } 19353 19354 TEST_F(FormatTest, OperatorSpacing) { 19355 FormatStyle Style = getLLVMStyle(); 19356 Style.PointerAlignment = FormatStyle::PAS_Right; 19357 verifyFormat("Foo::operator*();", Style); 19358 verifyFormat("Foo::operator void *();", Style); 19359 verifyFormat("Foo::operator void **();", Style); 19360 verifyFormat("Foo::operator void *&();", Style); 19361 verifyFormat("Foo::operator void *&&();", Style); 19362 verifyFormat("Foo::operator void const *();", Style); 19363 verifyFormat("Foo::operator void const **();", Style); 19364 verifyFormat("Foo::operator void const *&();", Style); 19365 verifyFormat("Foo::operator void const *&&();", Style); 19366 verifyFormat("Foo::operator()(void *);", Style); 19367 verifyFormat("Foo::operator*(void *);", Style); 19368 verifyFormat("Foo::operator*();", Style); 19369 verifyFormat("Foo::operator**();", Style); 19370 verifyFormat("Foo::operator&();", Style); 19371 verifyFormat("Foo::operator<int> *();", Style); 19372 verifyFormat("Foo::operator<Foo> *();", Style); 19373 verifyFormat("Foo::operator<int> **();", Style); 19374 verifyFormat("Foo::operator<Foo> **();", Style); 19375 verifyFormat("Foo::operator<int> &();", Style); 19376 verifyFormat("Foo::operator<Foo> &();", Style); 19377 verifyFormat("Foo::operator<int> &&();", Style); 19378 verifyFormat("Foo::operator<Foo> &&();", Style); 19379 verifyFormat("Foo::operator<int> *&();", Style); 19380 verifyFormat("Foo::operator<Foo> *&();", Style); 19381 verifyFormat("Foo::operator<int> *&&();", Style); 19382 verifyFormat("Foo::operator<Foo> *&&();", Style); 19383 verifyFormat("operator*(int (*)(), class Foo);", Style); 19384 19385 verifyFormat("Foo::operator&();", Style); 19386 verifyFormat("Foo::operator void &();", Style); 19387 verifyFormat("Foo::operator void const &();", Style); 19388 verifyFormat("Foo::operator()(void &);", Style); 19389 verifyFormat("Foo::operator&(void &);", Style); 19390 verifyFormat("Foo::operator&();", Style); 19391 verifyFormat("operator&(int (&)(), class Foo);", Style); 19392 19393 verifyFormat("Foo::operator&&();", Style); 19394 verifyFormat("Foo::operator**();", Style); 19395 verifyFormat("Foo::operator void &&();", Style); 19396 verifyFormat("Foo::operator void const &&();", Style); 19397 verifyFormat("Foo::operator()(void &&);", Style); 19398 verifyFormat("Foo::operator&&(void &&);", Style); 19399 verifyFormat("Foo::operator&&();", Style); 19400 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 19401 verifyFormat("operator const nsTArrayRight<E> &()", Style); 19402 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 19403 Style); 19404 verifyFormat("operator void **()", Style); 19405 verifyFormat("operator const FooRight<Object> &()", Style); 19406 verifyFormat("operator const FooRight<Object> *()", Style); 19407 verifyFormat("operator const FooRight<Object> **()", Style); 19408 verifyFormat("operator const FooRight<Object> *&()", Style); 19409 verifyFormat("operator const FooRight<Object> *&&()", Style); 19410 19411 Style.PointerAlignment = FormatStyle::PAS_Left; 19412 verifyFormat("Foo::operator*();", Style); 19413 verifyFormat("Foo::operator**();", Style); 19414 verifyFormat("Foo::operator void*();", Style); 19415 verifyFormat("Foo::operator void**();", Style); 19416 verifyFormat("Foo::operator void*&();", Style); 19417 verifyFormat("Foo::operator void*&&();", Style); 19418 verifyFormat("Foo::operator void const*();", Style); 19419 verifyFormat("Foo::operator void const**();", Style); 19420 verifyFormat("Foo::operator void const*&();", Style); 19421 verifyFormat("Foo::operator void const*&&();", Style); 19422 verifyFormat("Foo::operator/*comment*/ void*();", Style); 19423 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 19424 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 19425 verifyFormat("Foo::operator()(void*);", Style); 19426 verifyFormat("Foo::operator*(void*);", Style); 19427 verifyFormat("Foo::operator*();", Style); 19428 verifyFormat("Foo::operator<int>*();", Style); 19429 verifyFormat("Foo::operator<Foo>*();", Style); 19430 verifyFormat("Foo::operator<int>**();", Style); 19431 verifyFormat("Foo::operator<Foo>**();", Style); 19432 verifyFormat("Foo::operator<Foo>*&();", Style); 19433 verifyFormat("Foo::operator<int>&();", Style); 19434 verifyFormat("Foo::operator<Foo>&();", Style); 19435 verifyFormat("Foo::operator<int>&&();", Style); 19436 verifyFormat("Foo::operator<Foo>&&();", Style); 19437 verifyFormat("Foo::operator<int>*&();", Style); 19438 verifyFormat("Foo::operator<Foo>*&();", Style); 19439 verifyFormat("operator*(int (*)(), class Foo);", Style); 19440 19441 verifyFormat("Foo::operator&();", Style); 19442 verifyFormat("Foo::operator void&();", Style); 19443 verifyFormat("Foo::operator void const&();", Style); 19444 verifyFormat("Foo::operator/*comment*/ void&();", Style); 19445 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 19446 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 19447 verifyFormat("Foo::operator()(void&);", Style); 19448 verifyFormat("Foo::operator&(void&);", Style); 19449 verifyFormat("Foo::operator&();", Style); 19450 verifyFormat("operator&(int (&)(), class Foo);", Style); 19451 19452 verifyFormat("Foo::operator&&();", Style); 19453 verifyFormat("Foo::operator void&&();", Style); 19454 verifyFormat("Foo::operator void const&&();", Style); 19455 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 19456 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 19457 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 19458 verifyFormat("Foo::operator()(void&&);", Style); 19459 verifyFormat("Foo::operator&&(void&&);", Style); 19460 verifyFormat("Foo::operator&&();", Style); 19461 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 19462 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 19463 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 19464 Style); 19465 verifyFormat("operator void**()", Style); 19466 verifyFormat("operator const FooLeft<Object>&()", Style); 19467 verifyFormat("operator const FooLeft<Object>*()", Style); 19468 verifyFormat("operator const FooLeft<Object>**()", Style); 19469 verifyFormat("operator const FooLeft<Object>*&()", Style); 19470 verifyFormat("operator const FooLeft<Object>*&&()", Style); 19471 19472 // PR45107 19473 verifyFormat("operator Vector<String>&();", Style); 19474 verifyFormat("operator const Vector<String>&();", Style); 19475 verifyFormat("operator foo::Bar*();", Style); 19476 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 19477 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 19478 Style); 19479 19480 Style.PointerAlignment = FormatStyle::PAS_Middle; 19481 verifyFormat("Foo::operator*();", Style); 19482 verifyFormat("Foo::operator void *();", Style); 19483 verifyFormat("Foo::operator()(void *);", Style); 19484 verifyFormat("Foo::operator*(void *);", Style); 19485 verifyFormat("Foo::operator*();", Style); 19486 verifyFormat("operator*(int (*)(), class Foo);", Style); 19487 19488 verifyFormat("Foo::operator&();", Style); 19489 verifyFormat("Foo::operator void &();", Style); 19490 verifyFormat("Foo::operator void const &();", Style); 19491 verifyFormat("Foo::operator()(void &);", Style); 19492 verifyFormat("Foo::operator&(void &);", Style); 19493 verifyFormat("Foo::operator&();", Style); 19494 verifyFormat("operator&(int (&)(), class Foo);", Style); 19495 19496 verifyFormat("Foo::operator&&();", Style); 19497 verifyFormat("Foo::operator void &&();", Style); 19498 verifyFormat("Foo::operator void const &&();", Style); 19499 verifyFormat("Foo::operator()(void &&);", Style); 19500 verifyFormat("Foo::operator&&(void &&);", Style); 19501 verifyFormat("Foo::operator&&();", Style); 19502 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 19503 } 19504 19505 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 19506 FormatStyle Style = getLLVMStyle(); 19507 // PR46157 19508 verifyFormat("foo(operator+, -42);", Style); 19509 verifyFormat("foo(operator++, -42);", Style); 19510 verifyFormat("foo(operator--, -42);", Style); 19511 verifyFormat("foo(-42, operator--);", Style); 19512 verifyFormat("foo(-42, operator, );", Style); 19513 verifyFormat("foo(operator, , -42);", Style); 19514 } 19515 19516 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 19517 FormatStyle Style = getLLVMStyle(); 19518 Style.WhitespaceSensitiveMacros.push_back("FOO"); 19519 19520 // Don't use the helpers here, since 'mess up' will change the whitespace 19521 // and these are all whitespace sensitive by definition 19522 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 19523 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 19524 EXPECT_EQ( 19525 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 19526 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 19527 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 19528 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 19529 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 19530 " Still=Intentional);", 19531 format("FOO(String-ized&Messy+But,: :\n" 19532 " Still=Intentional);", 19533 Style)); 19534 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 19535 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 19536 " Still=Intentional);", 19537 format("FOO(String-ized=&Messy+But,: :\n" 19538 " Still=Intentional);", 19539 Style)); 19540 19541 Style.ColumnLimit = 21; 19542 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 19543 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 19544 } 19545 19546 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 19547 // These tests are not in NamespaceFixer because that doesn't 19548 // test its interaction with line wrapping 19549 FormatStyle Style = getLLVMStyle(); 19550 Style.ColumnLimit = 80; 19551 verifyFormat("namespace {\n" 19552 "int i;\n" 19553 "int j;\n" 19554 "} // namespace", 19555 Style); 19556 19557 verifyFormat("namespace AAA {\n" 19558 "int i;\n" 19559 "int j;\n" 19560 "} // namespace AAA", 19561 Style); 19562 19563 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 19564 "int i;\n" 19565 "int j;\n" 19566 "} // namespace Averyveryveryverylongnamespace", 19567 format("namespace Averyveryveryverylongnamespace {\n" 19568 "int i;\n" 19569 "int j;\n" 19570 "}", 19571 Style)); 19572 19573 EXPECT_EQ( 19574 "namespace " 19575 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 19576 " went::mad::now {\n" 19577 "int i;\n" 19578 "int j;\n" 19579 "} // namespace\n" 19580 " // " 19581 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 19582 "went::mad::now", 19583 format("namespace " 19584 "would::it::save::you::a::lot::of::time::if_::i::" 19585 "just::gave::up::and_::went::mad::now {\n" 19586 "int i;\n" 19587 "int j;\n" 19588 "}", 19589 Style)); 19590 19591 // This used to duplicate the comment again and again on subsequent runs 19592 EXPECT_EQ( 19593 "namespace " 19594 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 19595 " went::mad::now {\n" 19596 "int i;\n" 19597 "int j;\n" 19598 "} // namespace\n" 19599 " // " 19600 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 19601 "went::mad::now", 19602 format("namespace " 19603 "would::it::save::you::a::lot::of::time::if_::i::" 19604 "just::gave::up::and_::went::mad::now {\n" 19605 "int i;\n" 19606 "int j;\n" 19607 "} // namespace\n" 19608 " // " 19609 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 19610 "and_::went::mad::now", 19611 Style)); 19612 } 19613 19614 TEST_F(FormatTest, LikelyUnlikely) { 19615 FormatStyle Style = getLLVMStyle(); 19616 19617 verifyFormat("if (argc > 5) [[unlikely]] {\n" 19618 " return 29;\n" 19619 "}", 19620 Style); 19621 19622 verifyFormat("if (argc > 5) [[likely]] {\n" 19623 " return 29;\n" 19624 "}", 19625 Style); 19626 19627 verifyFormat("if (argc > 5) [[unlikely]] {\n" 19628 " return 29;\n" 19629 "} else [[likely]] {\n" 19630 " return 42;\n" 19631 "}\n", 19632 Style); 19633 19634 verifyFormat("if (argc > 5) [[unlikely]] {\n" 19635 " return 29;\n" 19636 "} else if (argc > 10) [[likely]] {\n" 19637 " return 99;\n" 19638 "} else {\n" 19639 " return 42;\n" 19640 "}\n", 19641 Style); 19642 19643 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 19644 " return 29;\n" 19645 "}", 19646 Style); 19647 } 19648 19649 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 19650 verifyFormat("Constructor()\n" 19651 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19652 " aaaa(aaaaaaaaaaaaaaaaaa, " 19653 "aaaaaaaaaaaaaaaaaat))"); 19654 verifyFormat("Constructor()\n" 19655 " : aaaaaaaaaaaaa(aaaaaa), " 19656 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 19657 19658 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 19659 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 19660 verifyFormat("Constructor()\n" 19661 " : aaaaaa(aaaaaa),\n" 19662 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19663 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 19664 StyleWithWhitespacePenalty); 19665 verifyFormat("Constructor()\n" 19666 " : aaaaaaaaaaaaa(aaaaaa), " 19667 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 19668 StyleWithWhitespacePenalty); 19669 } 19670 19671 TEST_F(FormatTest, LLVMDefaultStyle) { 19672 FormatStyle Style = getLLVMStyle(); 19673 verifyFormat("extern \"C\" {\n" 19674 "int foo();\n" 19675 "}", 19676 Style); 19677 } 19678 TEST_F(FormatTest, GNUDefaultStyle) { 19679 FormatStyle Style = getGNUStyle(); 19680 verifyFormat("extern \"C\"\n" 19681 "{\n" 19682 " int foo ();\n" 19683 "}", 19684 Style); 19685 } 19686 TEST_F(FormatTest, MozillaDefaultStyle) { 19687 FormatStyle Style = getMozillaStyle(); 19688 verifyFormat("extern \"C\"\n" 19689 "{\n" 19690 " int foo();\n" 19691 "}", 19692 Style); 19693 } 19694 TEST_F(FormatTest, GoogleDefaultStyle) { 19695 FormatStyle Style = getGoogleStyle(); 19696 verifyFormat("extern \"C\" {\n" 19697 "int foo();\n" 19698 "}", 19699 Style); 19700 } 19701 TEST_F(FormatTest, ChromiumDefaultStyle) { 19702 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 19703 verifyFormat("extern \"C\" {\n" 19704 "int foo();\n" 19705 "}", 19706 Style); 19707 } 19708 TEST_F(FormatTest, MicrosoftDefaultStyle) { 19709 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 19710 verifyFormat("extern \"C\"\n" 19711 "{\n" 19712 " int foo();\n" 19713 "}", 19714 Style); 19715 } 19716 TEST_F(FormatTest, WebKitDefaultStyle) { 19717 FormatStyle Style = getWebKitStyle(); 19718 verifyFormat("extern \"C\" {\n" 19719 "int foo();\n" 19720 "}", 19721 Style); 19722 } 19723 19724 TEST_F(FormatTest, ConceptsAndRequires) { 19725 FormatStyle Style = getLLVMStyle(); 19726 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 19727 19728 verifyFormat("template <typename T>\n" 19729 "concept Hashable = requires(T a) {\n" 19730 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 19731 "};", 19732 Style); 19733 verifyFormat("template <typename T>\n" 19734 "concept EqualityComparable = requires(T a, T b) {\n" 19735 " { a == b } -> bool;\n" 19736 "};", 19737 Style); 19738 verifyFormat("template <typename T>\n" 19739 "concept EqualityComparable = requires(T a, T b) {\n" 19740 " { a == b } -> bool;\n" 19741 " { a != b } -> bool;\n" 19742 "};", 19743 Style); 19744 verifyFormat("template <typename T>\n" 19745 "concept EqualityComparable = requires(T a, T b) {\n" 19746 " { a == b } -> bool;\n" 19747 " { a != b } -> bool;\n" 19748 "};", 19749 Style); 19750 19751 verifyFormat("template <typename It>\n" 19752 "requires Iterator<It>\n" 19753 "void sort(It begin, It end) {\n" 19754 " //....\n" 19755 "}", 19756 Style); 19757 19758 verifyFormat("template <typename T>\n" 19759 "concept Large = sizeof(T) > 10;", 19760 Style); 19761 19762 verifyFormat("template <typename T, typename U>\n" 19763 "concept FooableWith = requires(T t, U u) {\n" 19764 " typename T::foo_type;\n" 19765 " { t.foo(u) } -> typename T::foo_type;\n" 19766 " t++;\n" 19767 "};\n" 19768 "void doFoo(FooableWith<int> auto t) {\n" 19769 " t.foo(3);\n" 19770 "}", 19771 Style); 19772 verifyFormat("template <typename T>\n" 19773 "concept Context = sizeof(T) == 1;", 19774 Style); 19775 verifyFormat("template <typename T>\n" 19776 "concept Context = is_specialization_of_v<context, T>;", 19777 Style); 19778 verifyFormat("template <typename T>\n" 19779 "concept Node = std::is_object_v<T>;", 19780 Style); 19781 verifyFormat("template <typename T>\n" 19782 "concept Tree = true;", 19783 Style); 19784 19785 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 19786 " //...\n" 19787 "}", 19788 Style); 19789 19790 verifyFormat( 19791 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 19792 " //...\n" 19793 "}", 19794 Style); 19795 19796 verifyFormat( 19797 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 19798 " //...\n" 19799 "}", 19800 Style); 19801 19802 verifyFormat("template <typename T>\n" 19803 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 19804 "Concept2<I> {\n" 19805 " //...\n" 19806 "}", 19807 Style); 19808 19809 verifyFormat("template <typename T>\n" 19810 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 19811 "Concept2<I> {\n" 19812 " //...\n" 19813 "}", 19814 Style); 19815 19816 verifyFormat( 19817 "template <typename T>\n" 19818 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 19819 " //...\n" 19820 "}", 19821 Style); 19822 19823 verifyFormat( 19824 "template <typename T>\n" 19825 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 19826 " //...\n" 19827 "}", 19828 Style); 19829 19830 verifyFormat("template <typename It>\n" 19831 "requires Foo<It>() && Bar<It> {\n" 19832 " //....\n" 19833 "}", 19834 Style); 19835 19836 verifyFormat("template <typename It>\n" 19837 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 19838 " //....\n" 19839 "}", 19840 Style); 19841 19842 verifyFormat("template <typename It>\n" 19843 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 19844 " //....\n" 19845 "}", 19846 Style); 19847 19848 verifyFormat( 19849 "template <typename It>\n" 19850 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 19851 " //....\n" 19852 "}", 19853 Style); 19854 19855 Style.IndentRequires = true; 19856 verifyFormat("template <typename It>\n" 19857 " requires Iterator<It>\n" 19858 "void sort(It begin, It end) {\n" 19859 " //....\n" 19860 "}", 19861 Style); 19862 verifyFormat("template <std::size index_>\n" 19863 " requires(index_ < sizeof...(Children_))\n" 19864 "Tree auto &child() {\n" 19865 " // ...\n" 19866 "}", 19867 Style); 19868 19869 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 19870 verifyFormat("template <typename T>\n" 19871 "concept Hashable = requires (T a) {\n" 19872 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 19873 "};", 19874 Style); 19875 19876 verifyFormat("template <class T = void>\n" 19877 " requires EqualityComparable<T> || Same<T, void>\n" 19878 "struct equal_to;", 19879 Style); 19880 19881 verifyFormat("template <class T>\n" 19882 " requires requires {\n" 19883 " T{};\n" 19884 " T (int);\n" 19885 " }\n", 19886 Style); 19887 19888 Style.ColumnLimit = 78; 19889 verifyFormat("template <typename T>\n" 19890 "concept Context = Traits<typename T::traits_type> and\n" 19891 " Interface<typename T::interface_type> and\n" 19892 " Request<typename T::request_type> and\n" 19893 " Response<typename T::response_type> and\n" 19894 " ContextExtension<typename T::extension_type> and\n" 19895 " ::std::is_copy_constructable<T> and " 19896 "::std::is_move_constructable<T> and\n" 19897 " requires (T c) {\n" 19898 " { c.response; } -> Response;\n" 19899 "} and requires (T c) {\n" 19900 " { c.request; } -> Request;\n" 19901 "}\n", 19902 Style); 19903 19904 verifyFormat("template <typename T>\n" 19905 "concept Context = Traits<typename T::traits_type> or\n" 19906 " Interface<typename T::interface_type> or\n" 19907 " Request<typename T::request_type> or\n" 19908 " Response<typename T::response_type> or\n" 19909 " ContextExtension<typename T::extension_type> or\n" 19910 " ::std::is_copy_constructable<T> or " 19911 "::std::is_move_constructable<T> or\n" 19912 " requires (T c) {\n" 19913 " { c.response; } -> Response;\n" 19914 "} or requires (T c) {\n" 19915 " { c.request; } -> Request;\n" 19916 "}\n", 19917 Style); 19918 19919 verifyFormat("template <typename T>\n" 19920 "concept Context = Traits<typename T::traits_type> &&\n" 19921 " Interface<typename T::interface_type> &&\n" 19922 " Request<typename T::request_type> &&\n" 19923 " Response<typename T::response_type> &&\n" 19924 " ContextExtension<typename T::extension_type> &&\n" 19925 " ::std::is_copy_constructable<T> && " 19926 "::std::is_move_constructable<T> &&\n" 19927 " requires (T c) {\n" 19928 " { c.response; } -> Response;\n" 19929 "} && requires (T c) {\n" 19930 " { c.request; } -> Request;\n" 19931 "}\n", 19932 Style); 19933 19934 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 19935 "Constraint2<T>;"); 19936 19937 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 19938 Style.BraceWrapping.AfterFunction = true; 19939 Style.BraceWrapping.AfterClass = true; 19940 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 19941 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 19942 verifyFormat("void Foo () requires (std::copyable<T>)\n" 19943 "{\n" 19944 " return\n" 19945 "}\n", 19946 Style); 19947 19948 verifyFormat("void Foo () requires std::copyable<T>\n" 19949 "{\n" 19950 " return\n" 19951 "}\n", 19952 Style); 19953 19954 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 19955 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 19956 "struct constant;", 19957 Style); 19958 19959 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 19960 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 19961 "struct constant;", 19962 Style); 19963 19964 verifyFormat("template <class T>\n" 19965 "class plane_with_very_very_very_long_name\n" 19966 "{\n" 19967 " constexpr plane_with_very_very_very_long_name () requires " 19968 "std::copyable<T>\n" 19969 " : plane_with_very_very_very_long_name (1)\n" 19970 " {\n" 19971 " }\n" 19972 "}\n", 19973 Style); 19974 19975 verifyFormat("template <class T>\n" 19976 "class plane_with_long_name\n" 19977 "{\n" 19978 " constexpr plane_with_long_name () requires std::copyable<T>\n" 19979 " : plane_with_long_name (1)\n" 19980 " {\n" 19981 " }\n" 19982 "}\n", 19983 Style); 19984 19985 Style.BreakBeforeConceptDeclarations = false; 19986 verifyFormat("template <typename T> concept Tree = true;", Style); 19987 19988 Style.IndentRequires = false; 19989 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 19990 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 19991 "struct constant;", 19992 Style); 19993 } 19994 19995 TEST_F(FormatTest, StatementAttributeLikeMacros) { 19996 FormatStyle Style = getLLVMStyle(); 19997 StringRef Source = "void Foo::slot() {\n" 19998 " unsigned char MyChar = 'x';\n" 19999 " emit signal(MyChar);\n" 20000 " Q_EMIT signal(MyChar);\n" 20001 "}"; 20002 20003 EXPECT_EQ(Source, format(Source, Style)); 20004 20005 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 20006 EXPECT_EQ("void Foo::slot() {\n" 20007 " unsigned char MyChar = 'x';\n" 20008 " emit signal(MyChar);\n" 20009 " Q_EMIT signal(MyChar);\n" 20010 "}", 20011 format(Source, Style)); 20012 20013 Style.StatementAttributeLikeMacros.push_back("emit"); 20014 EXPECT_EQ(Source, format(Source, Style)); 20015 20016 Style.StatementAttributeLikeMacros = {}; 20017 EXPECT_EQ("void Foo::slot() {\n" 20018 " unsigned char MyChar = 'x';\n" 20019 " emit signal(MyChar);\n" 20020 " Q_EMIT signal(MyChar);\n" 20021 "}", 20022 format(Source, Style)); 20023 } 20024 20025 TEST_F(FormatTest, IndentAccessModifiers) { 20026 FormatStyle Style = getLLVMStyle(); 20027 Style.IndentAccessModifiers = true; 20028 // Members are *two* levels below the record; 20029 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 20030 verifyFormat("class C {\n" 20031 " int i;\n" 20032 "};\n", 20033 Style); 20034 verifyFormat("union C {\n" 20035 " int i;\n" 20036 " unsigned u;\n" 20037 "};\n", 20038 Style); 20039 // Access modifiers should be indented one level below the record. 20040 verifyFormat("class C {\n" 20041 " public:\n" 20042 " int i;\n" 20043 "};\n", 20044 Style); 20045 verifyFormat("struct S {\n" 20046 " private:\n" 20047 " class C {\n" 20048 " int j;\n" 20049 "\n" 20050 " public:\n" 20051 " C();\n" 20052 " };\n" 20053 "\n" 20054 " public:\n" 20055 " int i;\n" 20056 "};\n", 20057 Style); 20058 // Enumerations are not records and should be unaffected. 20059 Style.AllowShortEnumsOnASingleLine = false; 20060 verifyFormat("enum class E\n" 20061 "{\n" 20062 " A,\n" 20063 " B\n" 20064 "};\n", 20065 Style); 20066 // Test with a different indentation width; 20067 // also proves that the result is Style.AccessModifierOffset agnostic. 20068 Style.IndentWidth = 3; 20069 verifyFormat("class C {\n" 20070 " public:\n" 20071 " int i;\n" 20072 "};\n", 20073 Style); 20074 } 20075 20076 TEST_F(FormatTest, LimitlessStringsAndComments) { 20077 auto Style = getLLVMStyleWithColumns(0); 20078 constexpr StringRef Code = 20079 "/**\n" 20080 " * This is a multiline comment with quite some long lines, at least for " 20081 "the LLVM Style.\n" 20082 " * We will redo this with strings and line comments. Just to check if " 20083 "everything is working.\n" 20084 " */\n" 20085 "bool foo() {\n" 20086 " /* Single line multi line comment. */\n" 20087 " const std::string String = \"This is a multiline string with quite " 20088 "some long lines, at least for the LLVM Style.\"\n" 20089 " \"We already did it with multi line " 20090 "comments, and we will do it with line comments. Just to check if " 20091 "everything is working.\";\n" 20092 " // This is a line comment (block) with quite some long lines, at " 20093 "least for the LLVM Style.\n" 20094 " // We already did this with multi line comments and strings. Just to " 20095 "check if everything is working.\n" 20096 " const std::string SmallString = \"Hello World\";\n" 20097 " // Small line comment\n" 20098 " return String.size() > SmallString.size();\n" 20099 "}"; 20100 EXPECT_EQ(Code, format(Code, Style)); 20101 } 20102 } // namespace 20103 } // namespace format 20104 } // namespace clang 20105