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 "clang/Frontend/TextDiagnosticPrinter.h" 15 #include "llvm/Support/Debug.h" 16 #include "llvm/Support/MemoryBuffer.h" 17 #include "gtest/gtest.h" 18 19 #define DEBUG_TYPE "format-test" 20 21 using clang::tooling::ReplacementTest; 22 using clang::tooling::toReplacements; 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 { 33 SC_ExpectComplete, 34 SC_ExpectIncomplete, 35 SC_DoNotCheck 36 }; 37 38 std::string format(llvm::StringRef Code, 39 const FormatStyle &Style = getLLVMStyle(), 40 StatusCheck CheckComplete = SC_ExpectComplete) { 41 LLVM_DEBUG(llvm::errs() << "---\n"); 42 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 43 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 44 FormattingAttemptStatus Status; 45 tooling::Replacements Replaces = 46 reformat(Style, Code, Ranges, "<stdin>", &Status); 47 if (CheckComplete != SC_DoNotCheck) { 48 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 49 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 50 << Code << "\n\n"; 51 } 52 ReplacementCount = Replaces.size(); 53 auto Result = applyAllReplacements(Code, Replaces); 54 EXPECT_TRUE(static_cast<bool>(Result)); 55 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 56 return *Result; 57 } 58 59 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 60 Style.ColumnLimit = ColumnLimit; 61 return Style; 62 } 63 64 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 65 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 66 } 67 68 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 69 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 70 } 71 72 void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code, 73 const FormatStyle &Style = getLLVMStyle()) { 74 EXPECT_EQ(Expected.str(), format(Expected, Style)) 75 << "Expected code is not stable"; 76 EXPECT_EQ(Expected.str(), format(Code, Style)); 77 if (Style.Language == FormatStyle::LK_Cpp) { 78 // Objective-C++ is a superset of C++, so everything checked for C++ 79 // needs to be checked for Objective-C++ as well. 80 FormatStyle ObjCStyle = Style; 81 ObjCStyle.Language = FormatStyle::LK_ObjC; 82 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 83 } 84 } 85 86 void verifyFormat(llvm::StringRef Code, 87 const FormatStyle &Style = getLLVMStyle()) { 88 verifyFormat(Code, test::messUp(Code), Style); 89 } 90 91 void verifyIncompleteFormat(llvm::StringRef Code, 92 const FormatStyle &Style = getLLVMStyle()) { 93 EXPECT_EQ(Code.str(), 94 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 95 } 96 97 void verifyGoogleFormat(llvm::StringRef Code) { 98 verifyFormat(Code, getGoogleStyle()); 99 } 100 101 void verifyIndependentOfContext(llvm::StringRef text) { 102 verifyFormat(text); 103 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 104 } 105 106 /// \brief Verify that clang-format does not crash on the given input. 107 void verifyNoCrash(llvm::StringRef Code, 108 const FormatStyle &Style = getLLVMStyle()) { 109 format(Code, Style, SC_DoNotCheck); 110 } 111 112 int ReplacementCount; 113 }; 114 115 TEST_F(FormatTest, MessUp) { 116 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 117 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 118 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 119 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 120 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 121 } 122 123 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { 124 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); 125 } 126 127 TEST_F(FormatTest, LLVMStyleOverride) { 128 EXPECT_EQ(FormatStyle::LK_Proto, 129 getLLVMStyle(FormatStyle::LK_Proto).Language); 130 } 131 132 //===----------------------------------------------------------------------===// 133 // Basic function tests. 134 //===----------------------------------------------------------------------===// 135 136 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 137 EXPECT_EQ(";", format(";")); 138 } 139 140 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 141 EXPECT_EQ("int i;", format(" int i;")); 142 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 143 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 144 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 145 } 146 147 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 148 EXPECT_EQ("int i;", format("int\ni;")); 149 } 150 151 TEST_F(FormatTest, FormatsNestedBlockStatements) { 152 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 153 } 154 155 TEST_F(FormatTest, FormatsNestedCall) { 156 verifyFormat("Method(f1, f2(f3));"); 157 verifyFormat("Method(f1(f2, f3()));"); 158 verifyFormat("Method(f1(f2, (f3())));"); 159 } 160 161 TEST_F(FormatTest, NestedNameSpecifiers) { 162 verifyFormat("vector<::Type> v;"); 163 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 164 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 165 verifyFormat("bool a = 2 < ::SomeFunction();"); 166 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 167 verifyFormat("some::string getName();"); 168 } 169 170 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 171 EXPECT_EQ("if (a) {\n" 172 " f();\n" 173 "}", 174 format("if(a){f();}")); 175 EXPECT_EQ(4, ReplacementCount); 176 EXPECT_EQ("if (a) {\n" 177 " f();\n" 178 "}", 179 format("if (a) {\n" 180 " f();\n" 181 "}")); 182 EXPECT_EQ(0, ReplacementCount); 183 EXPECT_EQ("/*\r\n" 184 "\r\n" 185 "*/\r\n", 186 format("/*\r\n" 187 "\r\n" 188 "*/\r\n")); 189 EXPECT_EQ(0, ReplacementCount); 190 } 191 192 TEST_F(FormatTest, RemovesEmptyLines) { 193 EXPECT_EQ("class C {\n" 194 " int i;\n" 195 "};", 196 format("class C {\n" 197 " int i;\n" 198 "\n" 199 "};")); 200 201 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 202 EXPECT_EQ("namespace N {\n" 203 "\n" 204 "int i;\n" 205 "}", 206 format("namespace N {\n" 207 "\n" 208 "int i;\n" 209 "}", 210 getGoogleStyle())); 211 EXPECT_EQ("/* something */ namespace N {\n" 212 "\n" 213 "int i;\n" 214 "}", 215 format("/* something */ namespace N {\n" 216 "\n" 217 "int i;\n" 218 "}", 219 getGoogleStyle())); 220 EXPECT_EQ("inline namespace N {\n" 221 "\n" 222 "int i;\n" 223 "}", 224 format("inline namespace N {\n" 225 "\n" 226 "int i;\n" 227 "}", 228 getGoogleStyle())); 229 EXPECT_EQ("/* something */ inline namespace N {\n" 230 "\n" 231 "int i;\n" 232 "}", 233 format("/* something */ inline namespace N {\n" 234 "\n" 235 "int i;\n" 236 "}", 237 getGoogleStyle())); 238 EXPECT_EQ("export namespace N {\n" 239 "\n" 240 "int i;\n" 241 "}", 242 format("export namespace N {\n" 243 "\n" 244 "int i;\n" 245 "}", 246 getGoogleStyle())); 247 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 248 "\n" 249 "int i;\n" 250 "}", 251 format("extern /**/ \"C\" /**/ {\n" 252 "\n" 253 "int i;\n" 254 "}", 255 getGoogleStyle())); 256 257 // ...but do keep inlining and removing empty lines for non-block extern "C" 258 // functions. 259 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 260 EXPECT_EQ("extern \"C\" int f() {\n" 261 " int i = 42;\n" 262 " return i;\n" 263 "}", 264 format("extern \"C\" int f() {\n" 265 "\n" 266 " int i = 42;\n" 267 " return i;\n" 268 "}", 269 getGoogleStyle())); 270 271 // Remove empty lines at the beginning and end of blocks. 272 EXPECT_EQ("void f() {\n" 273 "\n" 274 " if (a) {\n" 275 "\n" 276 " f();\n" 277 " }\n" 278 "}", 279 format("void f() {\n" 280 "\n" 281 " if (a) {\n" 282 "\n" 283 " f();\n" 284 "\n" 285 " }\n" 286 "\n" 287 "}", 288 getLLVMStyle())); 289 EXPECT_EQ("void f() {\n" 290 " if (a) {\n" 291 " f();\n" 292 " }\n" 293 "}", 294 format("void f() {\n" 295 "\n" 296 " if (a) {\n" 297 "\n" 298 " f();\n" 299 "\n" 300 " }\n" 301 "\n" 302 "}", 303 getGoogleStyle())); 304 305 // Don't remove empty lines in more complex control statements. 306 EXPECT_EQ("void f() {\n" 307 " if (a) {\n" 308 " f();\n" 309 "\n" 310 " } else if (b) {\n" 311 " f();\n" 312 " }\n" 313 "}", 314 format("void f() {\n" 315 " if (a) {\n" 316 " f();\n" 317 "\n" 318 " } else if (b) {\n" 319 " f();\n" 320 "\n" 321 " }\n" 322 "\n" 323 "}")); 324 325 // Don't remove empty lines before namespace endings. 326 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 327 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 328 EXPECT_EQ("namespace {\n" 329 "int i;\n" 330 "\n" 331 "}", 332 format("namespace {\n" 333 "int i;\n" 334 "\n" 335 "}", LLVMWithNoNamespaceFix)); 336 EXPECT_EQ("namespace {\n" 337 "int i;\n" 338 "}", 339 format("namespace {\n" 340 "int i;\n" 341 "}", LLVMWithNoNamespaceFix)); 342 EXPECT_EQ("namespace {\n" 343 "int i;\n" 344 "\n" 345 "};", 346 format("namespace {\n" 347 "int i;\n" 348 "\n" 349 "};", LLVMWithNoNamespaceFix)); 350 EXPECT_EQ("namespace {\n" 351 "int i;\n" 352 "};", 353 format("namespace {\n" 354 "int i;\n" 355 "};", LLVMWithNoNamespaceFix)); 356 EXPECT_EQ("namespace {\n" 357 "int i;\n" 358 "\n" 359 "}", 360 format("namespace {\n" 361 "int i;\n" 362 "\n" 363 "}")); 364 EXPECT_EQ("namespace {\n" 365 "int i;\n" 366 "\n" 367 "} // namespace", 368 format("namespace {\n" 369 "int i;\n" 370 "\n" 371 "} // namespace")); 372 373 FormatStyle Style = getLLVMStyle(); 374 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 375 Style.MaxEmptyLinesToKeep = 2; 376 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 377 Style.BraceWrapping.AfterClass = true; 378 Style.BraceWrapping.AfterFunction = true; 379 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 380 381 EXPECT_EQ("class Foo\n" 382 "{\n" 383 " Foo() {}\n" 384 "\n" 385 " void funk() {}\n" 386 "};", 387 format("class Foo\n" 388 "{\n" 389 " Foo()\n" 390 " {\n" 391 " }\n" 392 "\n" 393 " void funk() {}\n" 394 "};", 395 Style)); 396 } 397 398 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 399 verifyFormat("x = (a) and (b);"); 400 verifyFormat("x = (a) or (b);"); 401 verifyFormat("x = (a) bitand (b);"); 402 verifyFormat("x = (a) bitor (b);"); 403 verifyFormat("x = (a) not_eq (b);"); 404 verifyFormat("x = (a) and_eq (b);"); 405 verifyFormat("x = (a) or_eq (b);"); 406 verifyFormat("x = (a) xor (b);"); 407 } 408 409 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 410 verifyFormat("x = compl(a);"); 411 verifyFormat("x = not(a);"); 412 verifyFormat("x = bitand(a);"); 413 // Unary operator must not be merged with the next identifier 414 verifyFormat("x = compl a;"); 415 verifyFormat("x = not a;"); 416 verifyFormat("x = bitand a;"); 417 } 418 419 //===----------------------------------------------------------------------===// 420 // Tests for control statements. 421 //===----------------------------------------------------------------------===// 422 423 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 424 verifyFormat("if (true)\n f();\ng();"); 425 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 426 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 427 verifyFormat("if constexpr (true)\n" 428 " f();\ng();"); 429 verifyFormat("if constexpr (a)\n" 430 " if constexpr (b)\n" 431 " if constexpr (c)\n" 432 " g();\n" 433 "h();"); 434 verifyFormat("if constexpr (a)\n" 435 " if constexpr (b) {\n" 436 " f();\n" 437 " }\n" 438 "g();"); 439 440 FormatStyle AllowsMergedIf = getLLVMStyle(); 441 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 442 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 443 FormatStyle::SIS_WithoutElse; 444 verifyFormat("if (a)\n" 445 " // comment\n" 446 " f();", 447 AllowsMergedIf); 448 verifyFormat("{\n" 449 " if (a)\n" 450 " label:\n" 451 " f();\n" 452 "}", 453 AllowsMergedIf); 454 verifyFormat("#define A \\\n" 455 " if (a) \\\n" 456 " label: \\\n" 457 " f()", 458 AllowsMergedIf); 459 verifyFormat("if (a)\n" 460 " ;", 461 AllowsMergedIf); 462 verifyFormat("if (a)\n" 463 " if (b) return;", 464 AllowsMergedIf); 465 466 verifyFormat("if (a) // Can't merge this\n" 467 " f();\n", 468 AllowsMergedIf); 469 verifyFormat("if (a) /* still don't merge */\n" 470 " f();", 471 AllowsMergedIf); 472 verifyFormat("if (a) { // Never merge this\n" 473 " f();\n" 474 "}", 475 AllowsMergedIf); 476 verifyFormat("if (a) { /* Never merge this */\n" 477 " f();\n" 478 "}", 479 AllowsMergedIf); 480 481 AllowsMergedIf.ColumnLimit = 14; 482 verifyFormat("if (a) return;", AllowsMergedIf); 483 verifyFormat("if (aaaaaaaaa)\n" 484 " return;", 485 AllowsMergedIf); 486 487 AllowsMergedIf.ColumnLimit = 13; 488 verifyFormat("if (a)\n return;", AllowsMergedIf); 489 } 490 491 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 492 FormatStyle AllowsMergedIf = getLLVMStyle(); 493 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 494 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 495 FormatStyle::SIS_WithoutElse; 496 verifyFormat("if (a)\n" 497 " f();\n" 498 "else {\n" 499 " g();\n" 500 "}", 501 AllowsMergedIf); 502 verifyFormat("if (a)\n" 503 " f();\n" 504 "else\n" 505 " g();\n", 506 AllowsMergedIf); 507 508 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always; 509 510 verifyFormat("if (a) f();\n" 511 "else {\n" 512 " g();\n" 513 "}", 514 AllowsMergedIf); 515 verifyFormat("if (a) f();\n" 516 "else {\n" 517 " if (a) f();\n" 518 " else {\n" 519 " g();\n" 520 " }\n" 521 " g();\n" 522 "}", 523 AllowsMergedIf); 524 } 525 526 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 527 FormatStyle AllowsMergedLoops = getLLVMStyle(); 528 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 529 verifyFormat("while (true) continue;", AllowsMergedLoops); 530 verifyFormat("for (;;) continue;", AllowsMergedLoops); 531 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 532 verifyFormat("while (true)\n" 533 " ;", 534 AllowsMergedLoops); 535 verifyFormat("for (;;)\n" 536 " ;", 537 AllowsMergedLoops); 538 verifyFormat("for (;;)\n" 539 " for (;;) continue;", 540 AllowsMergedLoops); 541 verifyFormat("for (;;) // Can't merge this\n" 542 " continue;", 543 AllowsMergedLoops); 544 verifyFormat("for (;;) /* still don't merge */\n" 545 " continue;", 546 AllowsMergedLoops); 547 } 548 549 TEST_F(FormatTest, FormatShortBracedStatements) { 550 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 551 AllowSimpleBracedStatements.ColumnLimit = 40; 552 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 553 554 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 555 FormatStyle::SIS_WithoutElse; 556 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 557 558 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 559 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 560 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 561 562 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 563 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 564 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 565 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 566 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 567 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 568 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 569 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 570 verifyFormat("if (true) {\n" 571 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 572 "}", 573 AllowSimpleBracedStatements); 574 verifyFormat("if (true) { //\n" 575 " f();\n" 576 "}", 577 AllowSimpleBracedStatements); 578 verifyFormat("if (true) {\n" 579 " f();\n" 580 " f();\n" 581 "}", 582 AllowSimpleBracedStatements); 583 verifyFormat("if (true) {\n" 584 " f();\n" 585 "} else {\n" 586 " f();\n" 587 "}", 588 AllowSimpleBracedStatements); 589 590 verifyFormat("struct A2 {\n" 591 " int X;\n" 592 "};", 593 AllowSimpleBracedStatements); 594 verifyFormat("typedef struct A2 {\n" 595 " int X;\n" 596 "} A2_t;", 597 AllowSimpleBracedStatements); 598 verifyFormat("template <int> struct A2 {\n" 599 " struct B {};\n" 600 "};", 601 AllowSimpleBracedStatements); 602 603 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 604 FormatStyle::SIS_Never; 605 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 606 verifyFormat("if (true) {\n" 607 " f();\n" 608 "}", 609 AllowSimpleBracedStatements); 610 verifyFormat("if (true) {\n" 611 " f();\n" 612 "} else {\n" 613 " f();\n" 614 "}", 615 AllowSimpleBracedStatements); 616 617 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 618 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 619 verifyFormat("while (true) {\n" 620 " f();\n" 621 "}", 622 AllowSimpleBracedStatements); 623 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 624 verifyFormat("for (;;) {\n" 625 " f();\n" 626 "}", 627 AllowSimpleBracedStatements); 628 629 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 630 FormatStyle::SIS_WithoutElse; 631 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 632 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true; 633 634 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 635 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 636 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 637 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 638 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 639 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 640 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 641 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 642 verifyFormat("if (true)\n" 643 "{\n" 644 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 645 "}", 646 AllowSimpleBracedStatements); 647 verifyFormat("if (true)\n" 648 "{ //\n" 649 " f();\n" 650 "}", 651 AllowSimpleBracedStatements); 652 verifyFormat("if (true)\n" 653 "{\n" 654 " f();\n" 655 " f();\n" 656 "}", 657 AllowSimpleBracedStatements); 658 verifyFormat("if (true)\n" 659 "{\n" 660 " f();\n" 661 "} else\n" 662 "{\n" 663 " f();\n" 664 "}", 665 AllowSimpleBracedStatements); 666 667 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 668 FormatStyle::SIS_Never; 669 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 670 verifyFormat("if (true)\n" 671 "{\n" 672 " f();\n" 673 "}", 674 AllowSimpleBracedStatements); 675 verifyFormat("if (true)\n" 676 "{\n" 677 " f();\n" 678 "} else\n" 679 "{\n" 680 " f();\n" 681 "}", 682 AllowSimpleBracedStatements); 683 684 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 685 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 686 verifyFormat("while (true)\n" 687 "{\n" 688 " f();\n" 689 "}", 690 AllowSimpleBracedStatements); 691 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 692 verifyFormat("for (;;)\n" 693 "{\n" 694 " f();\n" 695 "}", 696 AllowSimpleBracedStatements); 697 } 698 699 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 700 FormatStyle Style = getLLVMStyleWithColumns(60); 701 Style.AllowShortBlocksOnASingleLine = true; 702 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 703 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 704 EXPECT_EQ("#define A \\\n" 705 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 706 " { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n" 707 "X;", 708 format("#define A \\\n" 709 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 710 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 711 " }\n" 712 "X;", 713 Style)); 714 } 715 716 TEST_F(FormatTest, ParseIfElse) { 717 verifyFormat("if (true)\n" 718 " if (true)\n" 719 " if (true)\n" 720 " f();\n" 721 " else\n" 722 " g();\n" 723 " else\n" 724 " h();\n" 725 "else\n" 726 " i();"); 727 verifyFormat("if (true)\n" 728 " if (true)\n" 729 " if (true) {\n" 730 " if (true)\n" 731 " f();\n" 732 " } else {\n" 733 " g();\n" 734 " }\n" 735 " else\n" 736 " h();\n" 737 "else {\n" 738 " i();\n" 739 "}"); 740 verifyFormat("if (true)\n" 741 " if constexpr (true)\n" 742 " if (true) {\n" 743 " if constexpr (true)\n" 744 " f();\n" 745 " } else {\n" 746 " g();\n" 747 " }\n" 748 " else\n" 749 " h();\n" 750 "else {\n" 751 " i();\n" 752 "}"); 753 verifyFormat("void f() {\n" 754 " if (a) {\n" 755 " } else {\n" 756 " }\n" 757 "}"); 758 } 759 760 TEST_F(FormatTest, ElseIf) { 761 verifyFormat("if (a) {\n} else if (b) {\n}"); 762 verifyFormat("if (a)\n" 763 " f();\n" 764 "else if (b)\n" 765 " g();\n" 766 "else\n" 767 " h();"); 768 verifyFormat("if constexpr (a)\n" 769 " f();\n" 770 "else if constexpr (b)\n" 771 " g();\n" 772 "else\n" 773 " h();"); 774 verifyFormat("if (a) {\n" 775 " f();\n" 776 "}\n" 777 "// or else ..\n" 778 "else {\n" 779 " g()\n" 780 "}"); 781 782 verifyFormat("if (a) {\n" 783 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 784 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 785 "}"); 786 verifyFormat("if (a) {\n" 787 "} else if (\n" 788 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 789 "}", 790 getLLVMStyleWithColumns(62)); 791 verifyFormat("if (a) {\n" 792 "} else if constexpr (\n" 793 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 794 "}", 795 getLLVMStyleWithColumns(62)); 796 } 797 798 TEST_F(FormatTest, FormatsForLoop) { 799 verifyFormat( 800 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 801 " ++VeryVeryLongLoopVariable)\n" 802 " ;"); 803 verifyFormat("for (;;)\n" 804 " f();"); 805 verifyFormat("for (;;) {\n}"); 806 verifyFormat("for (;;) {\n" 807 " f();\n" 808 "}"); 809 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 810 811 verifyFormat( 812 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 813 " E = UnwrappedLines.end();\n" 814 " I != E; ++I) {\n}"); 815 816 verifyFormat( 817 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 818 " ++IIIII) {\n}"); 819 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 820 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 821 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 822 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 823 " I = FD->getDeclsInPrototypeScope().begin(),\n" 824 " E = FD->getDeclsInPrototypeScope().end();\n" 825 " I != E; ++I) {\n}"); 826 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 827 " I = Container.begin(),\n" 828 " E = Container.end();\n" 829 " I != E; ++I) {\n}", 830 getLLVMStyleWithColumns(76)); 831 832 verifyFormat( 833 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 834 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 835 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 836 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 837 " ++aaaaaaaaaaa) {\n}"); 838 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 839 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 840 " ++i) {\n}"); 841 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 842 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 843 "}"); 844 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 845 " aaaaaaaaaa);\n" 846 " iter; ++iter) {\n" 847 "}"); 848 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 850 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 851 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 852 853 // These should not be formatted as Objective-C for-in loops. 854 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 855 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 856 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 857 verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 858 859 FormatStyle NoBinPacking = getLLVMStyle(); 860 NoBinPacking.BinPackParameters = false; 861 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 862 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 863 " aaaaaaaaaaaaaaaa,\n" 864 " aaaaaaaaaaaaaaaa,\n" 865 " aaaaaaaaaaaaaaaa);\n" 866 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 867 "}", 868 NoBinPacking); 869 verifyFormat( 870 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 871 " E = UnwrappedLines.end();\n" 872 " I != E;\n" 873 " ++I) {\n}", 874 NoBinPacking); 875 876 FormatStyle AlignLeft = getLLVMStyle(); 877 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 878 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 879 } 880 881 TEST_F(FormatTest, RangeBasedForLoops) { 882 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 883 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 884 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 885 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 886 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 887 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 888 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 889 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 890 } 891 892 TEST_F(FormatTest, ForEachLoops) { 893 verifyFormat("void f() {\n" 894 " foreach (Item *item, itemlist) {}\n" 895 " Q_FOREACH (Item *item, itemlist) {}\n" 896 " BOOST_FOREACH (Item *item, itemlist) {}\n" 897 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 898 "}"); 899 900 // As function-like macros. 901 verifyFormat("#define foreach(x, y)\n" 902 "#define Q_FOREACH(x, y)\n" 903 "#define BOOST_FOREACH(x, y)\n" 904 "#define UNKNOWN_FOREACH(x, y)\n"); 905 906 // Not as function-like macros. 907 verifyFormat("#define foreach (x, y)\n" 908 "#define Q_FOREACH (x, y)\n" 909 "#define BOOST_FOREACH (x, y)\n" 910 "#define UNKNOWN_FOREACH (x, y)\n"); 911 } 912 913 TEST_F(FormatTest, FormatsWhileLoop) { 914 verifyFormat("while (true) {\n}"); 915 verifyFormat("while (true)\n" 916 " f();"); 917 verifyFormat("while () {\n}"); 918 verifyFormat("while () {\n" 919 " f();\n" 920 "}"); 921 } 922 923 TEST_F(FormatTest, FormatsDoWhile) { 924 verifyFormat("do {\n" 925 " do_something();\n" 926 "} while (something());"); 927 verifyFormat("do\n" 928 " do_something();\n" 929 "while (something());"); 930 } 931 932 TEST_F(FormatTest, FormatsSwitchStatement) { 933 verifyFormat("switch (x) {\n" 934 "case 1:\n" 935 " f();\n" 936 " break;\n" 937 "case kFoo:\n" 938 "case ns::kBar:\n" 939 "case kBaz:\n" 940 " break;\n" 941 "default:\n" 942 " g();\n" 943 " break;\n" 944 "}"); 945 verifyFormat("switch (x) {\n" 946 "case 1: {\n" 947 " f();\n" 948 " break;\n" 949 "}\n" 950 "case 2: {\n" 951 " break;\n" 952 "}\n" 953 "}"); 954 verifyFormat("switch (x) {\n" 955 "case 1: {\n" 956 " f();\n" 957 " {\n" 958 " g();\n" 959 " h();\n" 960 " }\n" 961 " break;\n" 962 "}\n" 963 "}"); 964 verifyFormat("switch (x) {\n" 965 "case 1: {\n" 966 " f();\n" 967 " if (foo) {\n" 968 " g();\n" 969 " h();\n" 970 " }\n" 971 " break;\n" 972 "}\n" 973 "}"); 974 verifyFormat("switch (x) {\n" 975 "case 1: {\n" 976 " f();\n" 977 " g();\n" 978 "} break;\n" 979 "}"); 980 verifyFormat("switch (test)\n" 981 " ;"); 982 verifyFormat("switch (x) {\n" 983 "default: {\n" 984 " // Do nothing.\n" 985 "}\n" 986 "}"); 987 verifyFormat("switch (x) {\n" 988 "// comment\n" 989 "// if 1, do f()\n" 990 "case 1:\n" 991 " f();\n" 992 "}"); 993 verifyFormat("switch (x) {\n" 994 "case 1:\n" 995 " // Do amazing stuff\n" 996 " {\n" 997 " f();\n" 998 " g();\n" 999 " }\n" 1000 " break;\n" 1001 "}"); 1002 verifyFormat("#define A \\\n" 1003 " switch (x) { \\\n" 1004 " case a: \\\n" 1005 " foo = b; \\\n" 1006 " }", 1007 getLLVMStyleWithColumns(20)); 1008 verifyFormat("#define OPERATION_CASE(name) \\\n" 1009 " case OP_name: \\\n" 1010 " return operations::Operation##name\n", 1011 getLLVMStyleWithColumns(40)); 1012 verifyFormat("switch (x) {\n" 1013 "case 1:;\n" 1014 "default:;\n" 1015 " int i;\n" 1016 "}"); 1017 1018 verifyGoogleFormat("switch (x) {\n" 1019 " case 1:\n" 1020 " f();\n" 1021 " break;\n" 1022 " case kFoo:\n" 1023 " case ns::kBar:\n" 1024 " case kBaz:\n" 1025 " break;\n" 1026 " default:\n" 1027 " g();\n" 1028 " break;\n" 1029 "}"); 1030 verifyGoogleFormat("switch (x) {\n" 1031 " case 1: {\n" 1032 " f();\n" 1033 " break;\n" 1034 " }\n" 1035 "}"); 1036 verifyGoogleFormat("switch (test)\n" 1037 " ;"); 1038 1039 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 1040 " case OP_name: \\\n" 1041 " return operations::Operation##name\n"); 1042 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 1043 " // Get the correction operation class.\n" 1044 " switch (OpCode) {\n" 1045 " CASE(Add);\n" 1046 " CASE(Subtract);\n" 1047 " default:\n" 1048 " return operations::Unknown;\n" 1049 " }\n" 1050 "#undef OPERATION_CASE\n" 1051 "}"); 1052 verifyFormat("DEBUG({\n" 1053 " switch (x) {\n" 1054 " case A:\n" 1055 " f();\n" 1056 " break;\n" 1057 " // fallthrough\n" 1058 " case B:\n" 1059 " g();\n" 1060 " break;\n" 1061 " }\n" 1062 "});"); 1063 EXPECT_EQ("DEBUG({\n" 1064 " switch (x) {\n" 1065 " case A:\n" 1066 " f();\n" 1067 " break;\n" 1068 " // On B:\n" 1069 " case B:\n" 1070 " g();\n" 1071 " break;\n" 1072 " }\n" 1073 "});", 1074 format("DEBUG({\n" 1075 " switch (x) {\n" 1076 " case A:\n" 1077 " f();\n" 1078 " break;\n" 1079 " // On B:\n" 1080 " case B:\n" 1081 " g();\n" 1082 " break;\n" 1083 " }\n" 1084 "});", 1085 getLLVMStyle())); 1086 EXPECT_EQ("switch (n) {\n" 1087 "case 0: {\n" 1088 " return false;\n" 1089 "}\n" 1090 "default: {\n" 1091 " return true;\n" 1092 "}\n" 1093 "}", 1094 format("switch (n)\n" 1095 "{\n" 1096 "case 0: {\n" 1097 " return false;\n" 1098 "}\n" 1099 "default: {\n" 1100 " return true;\n" 1101 "}\n" 1102 "}", 1103 getLLVMStyle())); 1104 verifyFormat("switch (a) {\n" 1105 "case (b):\n" 1106 " return;\n" 1107 "}"); 1108 1109 verifyFormat("switch (a) {\n" 1110 "case some_namespace::\n" 1111 " some_constant:\n" 1112 " return;\n" 1113 "}", 1114 getLLVMStyleWithColumns(34)); 1115 1116 FormatStyle Style = getLLVMStyle(); 1117 Style.IndentCaseLabels = true; 1118 Style.AllowShortBlocksOnASingleLine = false; 1119 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1120 Style.BraceWrapping.AfterCaseLabel = true; 1121 Style.BraceWrapping.AfterControlStatement = true; 1122 EXPECT_EQ("switch (n)\n" 1123 "{\n" 1124 " case 0:\n" 1125 " {\n" 1126 " return false;\n" 1127 " }\n" 1128 " default:\n" 1129 " {\n" 1130 " return true;\n" 1131 " }\n" 1132 "}", 1133 format("switch (n) {\n" 1134 " case 0: {\n" 1135 " return false;\n" 1136 " }\n" 1137 " default: {\n" 1138 " return true;\n" 1139 " }\n" 1140 "}", 1141 Style)); 1142 Style.BraceWrapping.AfterCaseLabel = false; 1143 EXPECT_EQ("switch (n)\n" 1144 "{\n" 1145 " case 0: {\n" 1146 " return false;\n" 1147 " }\n" 1148 " default: {\n" 1149 " return true;\n" 1150 " }\n" 1151 "}", 1152 format("switch (n) {\n" 1153 " case 0:\n" 1154 " {\n" 1155 " return false;\n" 1156 " }\n" 1157 " default:\n" 1158 " {\n" 1159 " return true;\n" 1160 " }\n" 1161 "}", 1162 Style)); 1163 } 1164 1165 TEST_F(FormatTest, CaseRanges) { 1166 verifyFormat("switch (x) {\n" 1167 "case 'A' ... 'Z':\n" 1168 "case 1 ... 5:\n" 1169 "case a ... b:\n" 1170 " break;\n" 1171 "}"); 1172 } 1173 1174 TEST_F(FormatTest, ShortCaseLabels) { 1175 FormatStyle Style = getLLVMStyle(); 1176 Style.AllowShortCaseLabelsOnASingleLine = true; 1177 verifyFormat("switch (a) {\n" 1178 "case 1: x = 1; break;\n" 1179 "case 2: return;\n" 1180 "case 3:\n" 1181 "case 4:\n" 1182 "case 5: return;\n" 1183 "case 6: // comment\n" 1184 " return;\n" 1185 "case 7:\n" 1186 " // comment\n" 1187 " return;\n" 1188 "case 8:\n" 1189 " x = 8; // comment\n" 1190 " break;\n" 1191 "default: y = 1; break;\n" 1192 "}", 1193 Style); 1194 verifyFormat("switch (a) {\n" 1195 "case 0: return; // comment\n" 1196 "case 1: break; // comment\n" 1197 "case 2: return;\n" 1198 "// comment\n" 1199 "case 3: return;\n" 1200 "// comment 1\n" 1201 "// comment 2\n" 1202 "// comment 3\n" 1203 "case 4: break; /* comment */\n" 1204 "case 5:\n" 1205 " // comment\n" 1206 " break;\n" 1207 "case 6: /* comment */ x = 1; break;\n" 1208 "case 7: x = /* comment */ 1; break;\n" 1209 "case 8:\n" 1210 " x = 1; /* comment */\n" 1211 " break;\n" 1212 "case 9:\n" 1213 " break; // comment line 1\n" 1214 " // comment line 2\n" 1215 "}", 1216 Style); 1217 EXPECT_EQ("switch (a) {\n" 1218 "case 1:\n" 1219 " x = 8;\n" 1220 " // fall through\n" 1221 "case 2: x = 8;\n" 1222 "// comment\n" 1223 "case 3:\n" 1224 " return; /* comment line 1\n" 1225 " * comment line 2 */\n" 1226 "case 4: i = 8;\n" 1227 "// something else\n" 1228 "#if FOO\n" 1229 "case 5: break;\n" 1230 "#endif\n" 1231 "}", 1232 format("switch (a) {\n" 1233 "case 1: x = 8;\n" 1234 " // fall through\n" 1235 "case 2:\n" 1236 " x = 8;\n" 1237 "// comment\n" 1238 "case 3:\n" 1239 " return; /* comment line 1\n" 1240 " * comment line 2 */\n" 1241 "case 4:\n" 1242 " i = 8;\n" 1243 "// something else\n" 1244 "#if FOO\n" 1245 "case 5: break;\n" 1246 "#endif\n" 1247 "}", 1248 Style)); 1249 EXPECT_EQ("switch (a) {\n" "case 0:\n" 1250 " return; // long long long long long long long long long long long long comment\n" 1251 " // line\n" "}", 1252 format("switch (a) {\n" 1253 "case 0: return; // long long long long long long long long long long long long comment line\n" 1254 "}", 1255 Style)); 1256 EXPECT_EQ("switch (a) {\n" 1257 "case 0:\n" 1258 " return; /* long long long long long long long long long long long long comment\n" 1259 " line */\n" 1260 "}", 1261 format("switch (a) {\n" 1262 "case 0: return; /* long long long long long long long long long long long long comment line */\n" 1263 "}", 1264 Style)); 1265 verifyFormat("switch (a) {\n" 1266 "#if FOO\n" 1267 "case 0: return 0;\n" 1268 "#endif\n" 1269 "}", 1270 Style); 1271 verifyFormat("switch (a) {\n" 1272 "case 1: {\n" 1273 "}\n" 1274 "case 2: {\n" 1275 " return;\n" 1276 "}\n" 1277 "case 3: {\n" 1278 " x = 1;\n" 1279 " return;\n" 1280 "}\n" 1281 "case 4:\n" 1282 " if (x)\n" 1283 " return;\n" 1284 "}", 1285 Style); 1286 Style.ColumnLimit = 21; 1287 verifyFormat("switch (a) {\n" 1288 "case 1: x = 1; break;\n" 1289 "case 2: return;\n" 1290 "case 3:\n" 1291 "case 4:\n" 1292 "case 5: return;\n" 1293 "default:\n" 1294 " y = 1;\n" 1295 " break;\n" 1296 "}", 1297 Style); 1298 Style.ColumnLimit = 80; 1299 Style.AllowShortCaseLabelsOnASingleLine = false; 1300 Style.IndentCaseLabels = true; 1301 EXPECT_EQ("switch (n) {\n" 1302 " default /*comments*/:\n" 1303 " return true;\n" 1304 " case 0:\n" 1305 " return false;\n" 1306 "}", 1307 format("switch (n) {\n" 1308 "default/*comments*/:\n" 1309 " return true;\n" 1310 "case 0:\n" 1311 " return false;\n" 1312 "}", 1313 Style)); 1314 Style.AllowShortCaseLabelsOnASingleLine = true; 1315 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1316 Style.BraceWrapping.AfterCaseLabel = true; 1317 Style.BraceWrapping.AfterControlStatement = true; 1318 EXPECT_EQ("switch (n)\n" 1319 "{\n" 1320 " case 0:\n" 1321 " {\n" 1322 " return false;\n" 1323 " }\n" 1324 " default:\n" 1325 " {\n" 1326 " return true;\n" 1327 " }\n" 1328 "}", 1329 format("switch (n) {\n" 1330 " case 0: {\n" 1331 " return false;\n" 1332 " }\n" 1333 " default:\n" 1334 " {\n" 1335 " return true;\n" 1336 " }\n" 1337 "}", 1338 Style)); 1339 } 1340 1341 TEST_F(FormatTest, FormatsLabels) { 1342 verifyFormat("void f() {\n" 1343 " some_code();\n" 1344 "test_label:\n" 1345 " some_other_code();\n" 1346 " {\n" 1347 " some_more_code();\n" 1348 " another_label:\n" 1349 " some_more_code();\n" 1350 " }\n" 1351 "}"); 1352 verifyFormat("{\n" 1353 " some_code();\n" 1354 "test_label:\n" 1355 " some_other_code();\n" 1356 "}"); 1357 verifyFormat("{\n" 1358 " some_code();\n" 1359 "test_label:;\n" 1360 " int i = 0;\n" 1361 "}"); 1362 } 1363 1364 //===----------------------------------------------------------------------===// 1365 // Tests for classes, namespaces, etc. 1366 //===----------------------------------------------------------------------===// 1367 1368 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1369 verifyFormat("class A {};"); 1370 } 1371 1372 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1373 verifyFormat("class A {\n" 1374 "public:\n" 1375 "public: // comment\n" 1376 "protected:\n" 1377 "private:\n" 1378 " void f() {}\n" 1379 "};"); 1380 verifyFormat("export class A {\n" 1381 "public:\n" 1382 "public: // comment\n" 1383 "protected:\n" 1384 "private:\n" 1385 " void f() {}\n" 1386 "};"); 1387 verifyGoogleFormat("class A {\n" 1388 " public:\n" 1389 " protected:\n" 1390 " private:\n" 1391 " void f() {}\n" 1392 "};"); 1393 verifyGoogleFormat("export class A {\n" 1394 " public:\n" 1395 " protected:\n" 1396 " private:\n" 1397 " void f() {}\n" 1398 "};"); 1399 verifyFormat("class A {\n" 1400 "public slots:\n" 1401 " void f1() {}\n" 1402 "public Q_SLOTS:\n" 1403 " void f2() {}\n" 1404 "protected slots:\n" 1405 " void f3() {}\n" 1406 "protected Q_SLOTS:\n" 1407 " void f4() {}\n" 1408 "private slots:\n" 1409 " void f5() {}\n" 1410 "private Q_SLOTS:\n" 1411 " void f6() {}\n" 1412 "signals:\n" 1413 " void g1();\n" 1414 "Q_SIGNALS:\n" 1415 " void g2();\n" 1416 "};"); 1417 1418 // Don't interpret 'signals' the wrong way. 1419 verifyFormat("signals.set();"); 1420 verifyFormat("for (Signals signals : f()) {\n}"); 1421 verifyFormat("{\n" 1422 " signals.set(); // This needs indentation.\n" 1423 "}"); 1424 verifyFormat("void f() {\n" 1425 "label:\n" 1426 " signals.baz();\n" 1427 "}"); 1428 } 1429 1430 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1431 EXPECT_EQ("class A {\n" 1432 "public:\n" 1433 " void f();\n" 1434 "\n" 1435 "private:\n" 1436 " void g() {}\n" 1437 " // test\n" 1438 "protected:\n" 1439 " int h;\n" 1440 "};", 1441 format("class A {\n" 1442 "public:\n" 1443 "void f();\n" 1444 "private:\n" 1445 "void g() {}\n" 1446 "// test\n" 1447 "protected:\n" 1448 "int h;\n" 1449 "};")); 1450 EXPECT_EQ("class A {\n" 1451 "protected:\n" 1452 "public:\n" 1453 " void f();\n" 1454 "};", 1455 format("class A {\n" 1456 "protected:\n" 1457 "\n" 1458 "public:\n" 1459 "\n" 1460 " void f();\n" 1461 "};")); 1462 1463 // Even ensure proper spacing inside macros. 1464 EXPECT_EQ("#define B \\\n" 1465 " class A { \\\n" 1466 " protected: \\\n" 1467 " public: \\\n" 1468 " void f(); \\\n" 1469 " };", 1470 format("#define B \\\n" 1471 " class A { \\\n" 1472 " protected: \\\n" 1473 " \\\n" 1474 " public: \\\n" 1475 " \\\n" 1476 " void f(); \\\n" 1477 " };", 1478 getGoogleStyle())); 1479 // But don't remove empty lines after macros ending in access specifiers. 1480 EXPECT_EQ("#define A private:\n" 1481 "\n" 1482 "int i;", 1483 format("#define A private:\n" 1484 "\n" 1485 "int i;")); 1486 } 1487 1488 TEST_F(FormatTest, FormatsClasses) { 1489 verifyFormat("class A : public B {};"); 1490 verifyFormat("class A : public ::B {};"); 1491 1492 verifyFormat( 1493 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1494 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1495 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1496 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1497 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1498 verifyFormat( 1499 "class A : public B, public C, public D, public E, public F {};"); 1500 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1501 " public C,\n" 1502 " public D,\n" 1503 " public E,\n" 1504 " public F,\n" 1505 " public G {};"); 1506 1507 verifyFormat("class\n" 1508 " ReallyReallyLongClassName {\n" 1509 " int i;\n" 1510 "};", 1511 getLLVMStyleWithColumns(32)); 1512 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1513 " aaaaaaaaaaaaaaaa> {};"); 1514 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1515 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1516 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1517 verifyFormat("template <class R, class C>\n" 1518 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1519 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1520 verifyFormat("class ::A::B {};"); 1521 } 1522 1523 TEST_F(FormatTest, BreakInheritanceStyle) { 1524 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 1525 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 1526 FormatStyle::BILS_BeforeComma; 1527 verifyFormat("class MyClass : public X {};", 1528 StyleWithInheritanceBreakBeforeComma); 1529 verifyFormat("class MyClass\n" 1530 " : public X\n" 1531 " , public Y {};", 1532 StyleWithInheritanceBreakBeforeComma); 1533 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 1534 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 1535 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 1536 StyleWithInheritanceBreakBeforeComma); 1537 verifyFormat("struct aaaaaaaaaaaaa\n" 1538 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 1539 " aaaaaaaaaaaaaaaa> {};", 1540 StyleWithInheritanceBreakBeforeComma); 1541 1542 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 1543 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 1544 FormatStyle::BILS_AfterColon; 1545 verifyFormat("class MyClass : public X {};", 1546 StyleWithInheritanceBreakAfterColon); 1547 verifyFormat("class MyClass : public X, public Y {};", 1548 StyleWithInheritanceBreakAfterColon); 1549 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 1550 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1551 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 1552 StyleWithInheritanceBreakAfterColon); 1553 verifyFormat("struct aaaaaaaaaaaaa :\n" 1554 " public aaaaaaaaaaaaaaaaaaa< // break\n" 1555 " aaaaaaaaaaaaaaaa> {};", 1556 StyleWithInheritanceBreakAfterColon); 1557 } 1558 1559 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1560 verifyFormat("class A {\n} a, b;"); 1561 verifyFormat("struct A {\n} a, b;"); 1562 verifyFormat("union A {\n} a;"); 1563 } 1564 1565 TEST_F(FormatTest, FormatsEnum) { 1566 verifyFormat("enum {\n" 1567 " Zero,\n" 1568 " One = 1,\n" 1569 " Two = One + 1,\n" 1570 " Three = (One + Two),\n" 1571 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1572 " Five = (One, Two, Three, Four, 5)\n" 1573 "};"); 1574 verifyGoogleFormat("enum {\n" 1575 " Zero,\n" 1576 " One = 1,\n" 1577 " Two = One + 1,\n" 1578 " Three = (One + Two),\n" 1579 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1580 " Five = (One, Two, Three, Four, 5)\n" 1581 "};"); 1582 verifyFormat("enum Enum {};"); 1583 verifyFormat("enum {};"); 1584 verifyFormat("enum X E {} d;"); 1585 verifyFormat("enum __attribute__((...)) E {} d;"); 1586 verifyFormat("enum __declspec__((...)) E {} d;"); 1587 verifyFormat("enum {\n" 1588 " Bar = Foo<int, int>::value\n" 1589 "};", 1590 getLLVMStyleWithColumns(30)); 1591 1592 verifyFormat("enum ShortEnum { A, B, C };"); 1593 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1594 1595 EXPECT_EQ("enum KeepEmptyLines {\n" 1596 " ONE,\n" 1597 "\n" 1598 " TWO,\n" 1599 "\n" 1600 " THREE\n" 1601 "}", 1602 format("enum KeepEmptyLines {\n" 1603 " ONE,\n" 1604 "\n" 1605 " TWO,\n" 1606 "\n" 1607 "\n" 1608 " THREE\n" 1609 "}")); 1610 verifyFormat("enum E { // comment\n" 1611 " ONE,\n" 1612 " TWO\n" 1613 "};\n" 1614 "int i;"); 1615 // Not enums. 1616 verifyFormat("enum X f() {\n" 1617 " a();\n" 1618 " return 42;\n" 1619 "}"); 1620 verifyFormat("enum X Type::f() {\n" 1621 " a();\n" 1622 " return 42;\n" 1623 "}"); 1624 verifyFormat("enum ::X f() {\n" 1625 " a();\n" 1626 " return 42;\n" 1627 "}"); 1628 verifyFormat("enum ns::X f() {\n" 1629 " a();\n" 1630 " return 42;\n" 1631 "}"); 1632 } 1633 1634 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1635 verifyFormat("enum Type {\n" 1636 " One = 0; // These semicolons should be commas.\n" 1637 " Two = 1;\n" 1638 "};"); 1639 verifyFormat("namespace n {\n" 1640 "enum Type {\n" 1641 " One,\n" 1642 " Two, // missing };\n" 1643 " int i;\n" 1644 "}\n" 1645 "void g() {}"); 1646 } 1647 1648 TEST_F(FormatTest, FormatsEnumStruct) { 1649 verifyFormat("enum struct {\n" 1650 " Zero,\n" 1651 " One = 1,\n" 1652 " Two = One + 1,\n" 1653 " Three = (One + Two),\n" 1654 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1655 " Five = (One, Two, Three, Four, 5)\n" 1656 "};"); 1657 verifyFormat("enum struct Enum {};"); 1658 verifyFormat("enum struct {};"); 1659 verifyFormat("enum struct X E {} d;"); 1660 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1661 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1662 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1663 } 1664 1665 TEST_F(FormatTest, FormatsEnumClass) { 1666 verifyFormat("enum class {\n" 1667 " Zero,\n" 1668 " One = 1,\n" 1669 " Two = One + 1,\n" 1670 " Three = (One + Two),\n" 1671 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1672 " Five = (One, Two, Three, Four, 5)\n" 1673 "};"); 1674 verifyFormat("enum class Enum {};"); 1675 verifyFormat("enum class {};"); 1676 verifyFormat("enum class X E {} d;"); 1677 verifyFormat("enum class __attribute__((...)) E {} d;"); 1678 verifyFormat("enum class __declspec__((...)) E {} d;"); 1679 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1680 } 1681 1682 TEST_F(FormatTest, FormatsEnumTypes) { 1683 verifyFormat("enum X : int {\n" 1684 " A, // Force multiple lines.\n" 1685 " B\n" 1686 "};"); 1687 verifyFormat("enum X : int { A, B };"); 1688 verifyFormat("enum X : std::uint32_t { A, B };"); 1689 } 1690 1691 TEST_F(FormatTest, FormatsTypedefEnum) { 1692 FormatStyle Style = getLLVMStyle(); 1693 Style.ColumnLimit = 40; 1694 verifyFormat("typedef enum {} EmptyEnum;"); 1695 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1696 verifyFormat("typedef enum {\n" 1697 " ZERO = 0,\n" 1698 " ONE = 1,\n" 1699 " TWO = 2,\n" 1700 " THREE = 3\n" 1701 "} LongEnum;", 1702 Style); 1703 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1704 Style.BraceWrapping.AfterEnum = true; 1705 verifyFormat("typedef enum {} EmptyEnum;"); 1706 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1707 verifyFormat("typedef enum\n" 1708 "{\n" 1709 " ZERO = 0,\n" 1710 " ONE = 1,\n" 1711 " TWO = 2,\n" 1712 " THREE = 3\n" 1713 "} LongEnum;", 1714 Style); 1715 } 1716 1717 TEST_F(FormatTest, FormatsNSEnums) { 1718 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1719 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1720 " // Information about someDecentlyLongValue.\n" 1721 " someDecentlyLongValue,\n" 1722 " // Information about anotherDecentlyLongValue.\n" 1723 " anotherDecentlyLongValue,\n" 1724 " // Information about aThirdDecentlyLongValue.\n" 1725 " aThirdDecentlyLongValue\n" 1726 "};"); 1727 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1728 " a = 1,\n" 1729 " b = 2,\n" 1730 " c = 3,\n" 1731 "};"); 1732 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1733 " a = 1,\n" 1734 " b = 2,\n" 1735 " c = 3,\n" 1736 "};"); 1737 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1738 " a = 1,\n" 1739 " b = 2,\n" 1740 " c = 3,\n" 1741 "};"); 1742 } 1743 1744 TEST_F(FormatTest, FormatsBitfields) { 1745 verifyFormat("struct Bitfields {\n" 1746 " unsigned sClass : 8;\n" 1747 " unsigned ValueKind : 2;\n" 1748 "};"); 1749 verifyFormat("struct A {\n" 1750 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1751 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1752 "};"); 1753 verifyFormat("struct MyStruct {\n" 1754 " uchar data;\n" 1755 " uchar : 8;\n" 1756 " uchar : 8;\n" 1757 " uchar other;\n" 1758 "};"); 1759 } 1760 1761 TEST_F(FormatTest, FormatsNamespaces) { 1762 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1763 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1764 1765 verifyFormat("namespace some_namespace {\n" 1766 "class A {};\n" 1767 "void f() { f(); }\n" 1768 "}", 1769 LLVMWithNoNamespaceFix); 1770 verifyFormat("/* something */ namespace some_namespace {\n" 1771 "class A {};\n" 1772 "void f() { f(); }\n" 1773 "}", 1774 LLVMWithNoNamespaceFix); 1775 verifyFormat("namespace {\n" 1776 "class A {};\n" 1777 "void f() { f(); }\n" 1778 "}", 1779 LLVMWithNoNamespaceFix); 1780 verifyFormat("/* something */ namespace {\n" 1781 "class A {};\n" 1782 "void f() { f(); }\n" 1783 "}", 1784 LLVMWithNoNamespaceFix); 1785 verifyFormat("inline namespace X {\n" 1786 "class A {};\n" 1787 "void f() { f(); }\n" 1788 "}", 1789 LLVMWithNoNamespaceFix); 1790 verifyFormat("/* something */ inline namespace X {\n" 1791 "class A {};\n" 1792 "void f() { f(); }\n" 1793 "}", 1794 LLVMWithNoNamespaceFix); 1795 verifyFormat("export namespace X {\n" 1796 "class A {};\n" 1797 "void f() { f(); }\n" 1798 "}", 1799 LLVMWithNoNamespaceFix); 1800 verifyFormat("using namespace some_namespace;\n" 1801 "class A {};\n" 1802 "void f() { f(); }", 1803 LLVMWithNoNamespaceFix); 1804 1805 // This code is more common than we thought; if we 1806 // layout this correctly the semicolon will go into 1807 // its own line, which is undesirable. 1808 verifyFormat("namespace {};", 1809 LLVMWithNoNamespaceFix); 1810 verifyFormat("namespace {\n" 1811 "class A {};\n" 1812 "};", 1813 LLVMWithNoNamespaceFix); 1814 1815 verifyFormat("namespace {\n" 1816 "int SomeVariable = 0; // comment\n" 1817 "} // namespace", 1818 LLVMWithNoNamespaceFix); 1819 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1820 "#define HEADER_GUARD\n" 1821 "namespace my_namespace {\n" 1822 "int i;\n" 1823 "} // my_namespace\n" 1824 "#endif // HEADER_GUARD", 1825 format("#ifndef HEADER_GUARD\n" 1826 " #define HEADER_GUARD\n" 1827 " namespace my_namespace {\n" 1828 "int i;\n" 1829 "} // my_namespace\n" 1830 "#endif // HEADER_GUARD", 1831 LLVMWithNoNamespaceFix)); 1832 1833 EXPECT_EQ("namespace A::B {\n" 1834 "class C {};\n" 1835 "}", 1836 format("namespace A::B {\n" 1837 "class C {};\n" 1838 "}", 1839 LLVMWithNoNamespaceFix)); 1840 1841 FormatStyle Style = getLLVMStyle(); 1842 Style.NamespaceIndentation = FormatStyle::NI_All; 1843 EXPECT_EQ("namespace out {\n" 1844 " int i;\n" 1845 " namespace in {\n" 1846 " int i;\n" 1847 " } // namespace in\n" 1848 "} // namespace out", 1849 format("namespace out {\n" 1850 "int i;\n" 1851 "namespace in {\n" 1852 "int i;\n" 1853 "} // namespace in\n" 1854 "} // namespace out", 1855 Style)); 1856 1857 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1858 EXPECT_EQ("namespace out {\n" 1859 "int i;\n" 1860 "namespace in {\n" 1861 " int i;\n" 1862 "} // namespace in\n" 1863 "} // namespace out", 1864 format("namespace out {\n" 1865 "int i;\n" 1866 "namespace in {\n" 1867 "int i;\n" 1868 "} // namespace in\n" 1869 "} // namespace out", 1870 Style)); 1871 } 1872 1873 TEST_F(FormatTest, NamespaceMacros) { 1874 FormatStyle Style = getLLVMStyle(); 1875 Style.NamespaceMacros.push_back("TESTSUITE"); 1876 1877 verifyFormat("TESTSUITE(A) {\n" 1878 "int foo();\n" 1879 "} // TESTSUITE(A)", 1880 Style); 1881 1882 verifyFormat("TESTSUITE(A, B) {\n" 1883 "int foo();\n" 1884 "} // TESTSUITE(A)", 1885 Style); 1886 1887 // Properly indent according to NamespaceIndentation style 1888 Style.NamespaceIndentation = FormatStyle::NI_All; 1889 verifyFormat("TESTSUITE(A) {\n" 1890 " int foo();\n" 1891 "} // TESTSUITE(A)", 1892 Style); 1893 verifyFormat("TESTSUITE(A) {\n" 1894 " namespace B {\n" 1895 " int foo();\n" 1896 " } // namespace B\n" 1897 "} // TESTSUITE(A)", 1898 Style); 1899 verifyFormat("namespace A {\n" 1900 " TESTSUITE(B) {\n" 1901 " int foo();\n" 1902 " } // TESTSUITE(B)\n" 1903 "} // namespace A", 1904 Style); 1905 1906 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1907 verifyFormat("TESTSUITE(A) {\n" 1908 "TESTSUITE(B) {\n" 1909 " int foo();\n" 1910 "} // TESTSUITE(B)\n" 1911 "} // TESTSUITE(A)", 1912 Style); 1913 verifyFormat("TESTSUITE(A) {\n" 1914 "namespace B {\n" 1915 " int foo();\n" 1916 "} // namespace B\n" 1917 "} // TESTSUITE(A)", 1918 Style); 1919 verifyFormat("namespace A {\n" 1920 "TESTSUITE(B) {\n" 1921 " int foo();\n" 1922 "} // TESTSUITE(B)\n" 1923 "} // namespace A", 1924 Style); 1925 1926 // Properly merge namespace-macros blocks in CompactNamespaces mode 1927 Style.NamespaceIndentation = FormatStyle::NI_None; 1928 Style.CompactNamespaces = true; 1929 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 1930 "}} // TESTSUITE(A::B)", 1931 Style); 1932 1933 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 1934 "}} // TESTSUITE(out::in)", 1935 format("TESTSUITE(out) {\n" 1936 "TESTSUITE(in) {\n" 1937 "} // TESTSUITE(in)\n" 1938 "} // TESTSUITE(out)", 1939 Style)); 1940 1941 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 1942 "}} // TESTSUITE(out::in)", 1943 format("TESTSUITE(out) {\n" 1944 "TESTSUITE(in) {\n" 1945 "} // TESTSUITE(in)\n" 1946 "} // TESTSUITE(out)", 1947 Style)); 1948 1949 // Do not merge different namespaces/macros 1950 EXPECT_EQ("namespace out {\n" 1951 "TESTSUITE(in) {\n" 1952 "} // TESTSUITE(in)\n" 1953 "} // namespace out", 1954 format("namespace out {\n" 1955 "TESTSUITE(in) {\n" 1956 "} // TESTSUITE(in)\n" 1957 "} // namespace out", 1958 Style)); 1959 EXPECT_EQ("TESTSUITE(out) {\n" 1960 "namespace in {\n" 1961 "} // namespace in\n" 1962 "} // TESTSUITE(out)", 1963 format("TESTSUITE(out) {\n" 1964 "namespace in {\n" 1965 "} // namespace in\n" 1966 "} // TESTSUITE(out)", 1967 Style)); 1968 Style.NamespaceMacros.push_back("FOOBAR"); 1969 EXPECT_EQ("TESTSUITE(out) {\n" 1970 "FOOBAR(in) {\n" 1971 "} // FOOBAR(in)\n" 1972 "} // TESTSUITE(out)", 1973 format("TESTSUITE(out) {\n" 1974 "FOOBAR(in) {\n" 1975 "} // FOOBAR(in)\n" 1976 "} // TESTSUITE(out)", 1977 Style)); 1978 } 1979 1980 TEST_F(FormatTest, FormatsCompactNamespaces) { 1981 FormatStyle Style = getLLVMStyle(); 1982 Style.CompactNamespaces = true; 1983 Style.NamespaceMacros.push_back("TESTSUITE"); 1984 1985 verifyFormat("namespace A { namespace B {\n" 1986 "}} // namespace A::B", 1987 Style); 1988 1989 EXPECT_EQ("namespace out { namespace in {\n" 1990 "}} // namespace out::in", 1991 format("namespace out {\n" 1992 "namespace in {\n" 1993 "} // namespace in\n" 1994 "} // namespace out", 1995 Style)); 1996 1997 // Only namespaces which have both consecutive opening and end get compacted 1998 EXPECT_EQ("namespace out {\n" 1999 "namespace in1 {\n" 2000 "} // namespace in1\n" 2001 "namespace in2 {\n" 2002 "} // namespace in2\n" 2003 "} // namespace out", 2004 format("namespace out {\n" 2005 "namespace in1 {\n" 2006 "} // namespace in1\n" 2007 "namespace in2 {\n" 2008 "} // namespace in2\n" 2009 "} // namespace out", 2010 Style)); 2011 2012 EXPECT_EQ("namespace out {\n" 2013 "int i;\n" 2014 "namespace in {\n" 2015 "int j;\n" 2016 "} // namespace in\n" 2017 "int k;\n" 2018 "} // namespace out", 2019 format("namespace out { int i;\n" 2020 "namespace in { int j; } // namespace in\n" 2021 "int k; } // namespace out", 2022 Style)); 2023 2024 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 2025 "}}} // namespace A::B::C\n", 2026 format("namespace A { namespace B {\n" 2027 "namespace C {\n" 2028 "}} // namespace B::C\n" 2029 "} // namespace A\n", 2030 Style)); 2031 2032 Style.ColumnLimit = 40; 2033 EXPECT_EQ("namespace aaaaaaaaaa {\n" 2034 "namespace bbbbbbbbbb {\n" 2035 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 2036 format("namespace aaaaaaaaaa {\n" 2037 "namespace bbbbbbbbbb {\n" 2038 "} // namespace bbbbbbbbbb\n" 2039 "} // namespace aaaaaaaaaa", 2040 Style)); 2041 2042 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 2043 "namespace cccccc {\n" 2044 "}}} // namespace aaaaaa::bbbbbb::cccccc", 2045 format("namespace aaaaaa {\n" 2046 "namespace bbbbbb {\n" 2047 "namespace cccccc {\n" 2048 "} // namespace cccccc\n" 2049 "} // namespace bbbbbb\n" 2050 "} // namespace aaaaaa", 2051 Style)); 2052 Style.ColumnLimit = 80; 2053 2054 // Extra semicolon after 'inner' closing brace prevents merging 2055 EXPECT_EQ("namespace out { namespace in {\n" 2056 "}; } // namespace out::in", 2057 format("namespace out {\n" 2058 "namespace in {\n" 2059 "}; // namespace in\n" 2060 "} // namespace out", 2061 Style)); 2062 2063 // Extra semicolon after 'outer' closing brace is conserved 2064 EXPECT_EQ("namespace out { namespace in {\n" 2065 "}}; // namespace out::in", 2066 format("namespace out {\n" 2067 "namespace in {\n" 2068 "} // namespace in\n" 2069 "}; // namespace out", 2070 Style)); 2071 2072 Style.NamespaceIndentation = FormatStyle::NI_All; 2073 EXPECT_EQ("namespace out { namespace in {\n" 2074 " int i;\n" 2075 "}} // namespace out::in", 2076 format("namespace out {\n" 2077 "namespace in {\n" 2078 "int i;\n" 2079 "} // namespace in\n" 2080 "} // namespace out", 2081 Style)); 2082 EXPECT_EQ("namespace out { namespace mid {\n" 2083 " namespace in {\n" 2084 " int j;\n" 2085 " } // namespace in\n" 2086 " int k;\n" 2087 "}} // namespace out::mid", 2088 format("namespace out { namespace mid {\n" 2089 "namespace in { int j; } // namespace in\n" 2090 "int k; }} // namespace out::mid", 2091 Style)); 2092 2093 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2094 EXPECT_EQ("namespace out { namespace in {\n" 2095 " int i;\n" 2096 "}} // namespace out::in", 2097 format("namespace out {\n" 2098 "namespace in {\n" 2099 "int i;\n" 2100 "} // namespace in\n" 2101 "} // namespace out", 2102 Style)); 2103 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 2104 " int i;\n" 2105 "}}} // namespace out::mid::in", 2106 format("namespace out {\n" 2107 "namespace mid {\n" 2108 "namespace in {\n" 2109 "int i;\n" 2110 "} // namespace in\n" 2111 "} // namespace mid\n" 2112 "} // namespace out", 2113 Style)); 2114 } 2115 2116 TEST_F(FormatTest, FormatsExternC) { 2117 verifyFormat("extern \"C\" {\nint a;"); 2118 verifyFormat("extern \"C\" {}"); 2119 verifyFormat("extern \"C\" {\n" 2120 "int foo();\n" 2121 "}"); 2122 verifyFormat("extern \"C\" int foo() {}"); 2123 verifyFormat("extern \"C\" int foo();"); 2124 verifyFormat("extern \"C\" int foo() {\n" 2125 " int i = 42;\n" 2126 " return i;\n" 2127 "}"); 2128 2129 FormatStyle Style = getLLVMStyle(); 2130 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2131 Style.BraceWrapping.AfterFunction = true; 2132 verifyFormat("extern \"C\" int foo() {}", Style); 2133 verifyFormat("extern \"C\" int foo();", Style); 2134 verifyFormat("extern \"C\" int foo()\n" 2135 "{\n" 2136 " int i = 42;\n" 2137 " return i;\n" 2138 "}", 2139 Style); 2140 2141 Style.BraceWrapping.AfterExternBlock = true; 2142 Style.BraceWrapping.SplitEmptyRecord = false; 2143 verifyFormat("extern \"C\"\n" 2144 "{}", 2145 Style); 2146 verifyFormat("extern \"C\"\n" 2147 "{\n" 2148 " int foo();\n" 2149 "}", 2150 Style); 2151 } 2152 2153 TEST_F(FormatTest, FormatsInlineASM) { 2154 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2155 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2156 verifyFormat( 2157 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 2158 " \"cpuid\\n\\t\"\n" 2159 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 2160 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 2161 " : \"a\"(value));"); 2162 EXPECT_EQ( 2163 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2164 " __asm {\n" 2165 " mov edx,[that] // vtable in edx\n" 2166 " mov eax,methodIndex\n" 2167 " call [edx][eax*4] // stdcall\n" 2168 " }\n" 2169 "}", 2170 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2171 " __asm {\n" 2172 " mov edx,[that] // vtable in edx\n" 2173 " mov eax,methodIndex\n" 2174 " call [edx][eax*4] // stdcall\n" 2175 " }\n" 2176 "}")); 2177 EXPECT_EQ("_asm {\n" 2178 " xor eax, eax;\n" 2179 " cpuid;\n" 2180 "}", 2181 format("_asm {\n" 2182 " xor eax, eax;\n" 2183 " cpuid;\n" 2184 "}")); 2185 verifyFormat("void function() {\n" 2186 " // comment\n" 2187 " asm(\"\");\n" 2188 "}"); 2189 EXPECT_EQ("__asm {\n" 2190 "}\n" 2191 "int i;", 2192 format("__asm {\n" 2193 "}\n" 2194 "int i;")); 2195 } 2196 2197 TEST_F(FormatTest, FormatTryCatch) { 2198 verifyFormat("try {\n" 2199 " throw a * b;\n" 2200 "} catch (int a) {\n" 2201 " // Do nothing.\n" 2202 "} catch (...) {\n" 2203 " exit(42);\n" 2204 "}"); 2205 2206 // Function-level try statements. 2207 verifyFormat("int f() try { return 4; } catch (...) {\n" 2208 " return 5;\n" 2209 "}"); 2210 verifyFormat("class A {\n" 2211 " int a;\n" 2212 " A() try : a(0) {\n" 2213 " } catch (...) {\n" 2214 " throw;\n" 2215 " }\n" 2216 "};\n"); 2217 2218 // Incomplete try-catch blocks. 2219 verifyIncompleteFormat("try {} catch ("); 2220 } 2221 2222 TEST_F(FormatTest, FormatSEHTryCatch) { 2223 verifyFormat("__try {\n" 2224 " int a = b * c;\n" 2225 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2226 " // Do nothing.\n" 2227 "}"); 2228 2229 verifyFormat("__try {\n" 2230 " int a = b * c;\n" 2231 "} __finally {\n" 2232 " // Do nothing.\n" 2233 "}"); 2234 2235 verifyFormat("DEBUG({\n" 2236 " __try {\n" 2237 " } __finally {\n" 2238 " }\n" 2239 "});\n"); 2240 } 2241 2242 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2243 verifyFormat("try {\n" 2244 " f();\n" 2245 "} catch {\n" 2246 " g();\n" 2247 "}"); 2248 verifyFormat("try {\n" 2249 " f();\n" 2250 "} catch (A a) MACRO(x) {\n" 2251 " g();\n" 2252 "} catch (B b) MACRO(x) {\n" 2253 " g();\n" 2254 "}"); 2255 } 2256 2257 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2258 FormatStyle Style = getLLVMStyle(); 2259 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 2260 FormatStyle::BS_WebKit}) { 2261 Style.BreakBeforeBraces = BraceStyle; 2262 verifyFormat("try {\n" 2263 " // something\n" 2264 "} catch (...) {\n" 2265 " // something\n" 2266 "}", 2267 Style); 2268 } 2269 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2270 verifyFormat("try {\n" 2271 " // something\n" 2272 "}\n" 2273 "catch (...) {\n" 2274 " // something\n" 2275 "}", 2276 Style); 2277 verifyFormat("__try {\n" 2278 " // something\n" 2279 "}\n" 2280 "__finally {\n" 2281 " // something\n" 2282 "}", 2283 Style); 2284 verifyFormat("@try {\n" 2285 " // something\n" 2286 "}\n" 2287 "@finally {\n" 2288 " // something\n" 2289 "}", 2290 Style); 2291 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2292 verifyFormat("try\n" 2293 "{\n" 2294 " // something\n" 2295 "}\n" 2296 "catch (...)\n" 2297 "{\n" 2298 " // something\n" 2299 "}", 2300 Style); 2301 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2302 verifyFormat("try\n" 2303 " {\n" 2304 " // something\n" 2305 " }\n" 2306 "catch (...)\n" 2307 " {\n" 2308 " // something\n" 2309 " }", 2310 Style); 2311 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2312 Style.BraceWrapping.BeforeCatch = true; 2313 verifyFormat("try {\n" 2314 " // something\n" 2315 "}\n" 2316 "catch (...) {\n" 2317 " // something\n" 2318 "}", 2319 Style); 2320 } 2321 2322 TEST_F(FormatTest, StaticInitializers) { 2323 verifyFormat("static SomeClass SC = {1, 'a'};"); 2324 2325 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2326 " 100000000, " 2327 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2328 2329 // Here, everything other than the "}" would fit on a line. 2330 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2331 " 10000000000000000000000000};"); 2332 EXPECT_EQ("S s = {a,\n" 2333 "\n" 2334 " b};", 2335 format("S s = {\n" 2336 " a,\n" 2337 "\n" 2338 " b\n" 2339 "};")); 2340 2341 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2342 // line. However, the formatting looks a bit off and this probably doesn't 2343 // happen often in practice. 2344 verifyFormat("static int Variable[1] = {\n" 2345 " {1000000000000000000000000000000000000}};", 2346 getLLVMStyleWithColumns(40)); 2347 } 2348 2349 TEST_F(FormatTest, DesignatedInitializers) { 2350 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2351 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2352 " .bbbbbbbbbb = 2,\n" 2353 " .cccccccccc = 3,\n" 2354 " .dddddddddd = 4,\n" 2355 " .eeeeeeeeee = 5};"); 2356 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2357 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2358 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2359 " .ccccccccccccccccccccccccccc = 3,\n" 2360 " .ddddddddddddddddddddddddddd = 4,\n" 2361 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2362 2363 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2364 2365 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 2366 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 2367 " [2] = bbbbbbbbbb,\n" 2368 " [3] = cccccccccc,\n" 2369 " [4] = dddddddddd,\n" 2370 " [5] = eeeeeeeeee};"); 2371 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2372 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2373 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2374 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 2375 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 2376 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 2377 } 2378 2379 TEST_F(FormatTest, NestedStaticInitializers) { 2380 verifyFormat("static A x = {{{}}};\n"); 2381 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2382 " {init1, init2, init3, init4}}};", 2383 getLLVMStyleWithColumns(50)); 2384 2385 verifyFormat("somes Status::global_reps[3] = {\n" 2386 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2387 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2388 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2389 getLLVMStyleWithColumns(60)); 2390 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2391 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2392 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2393 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2394 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2395 " {rect.fRight - rect.fLeft, rect.fBottom - " 2396 "rect.fTop}};"); 2397 2398 verifyFormat( 2399 "SomeArrayOfSomeType a = {\n" 2400 " {{1, 2, 3},\n" 2401 " {1, 2, 3},\n" 2402 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2403 " 333333333333333333333333333333},\n" 2404 " {1, 2, 3},\n" 2405 " {1, 2, 3}}};"); 2406 verifyFormat( 2407 "SomeArrayOfSomeType a = {\n" 2408 " {{1, 2, 3}},\n" 2409 " {{1, 2, 3}},\n" 2410 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2411 " 333333333333333333333333333333}},\n" 2412 " {{1, 2, 3}},\n" 2413 " {{1, 2, 3}}};"); 2414 2415 verifyFormat("struct {\n" 2416 " unsigned bit;\n" 2417 " const char *const name;\n" 2418 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2419 " {kOsWin, \"Windows\"},\n" 2420 " {kOsLinux, \"Linux\"},\n" 2421 " {kOsCrOS, \"Chrome OS\"}};"); 2422 verifyFormat("struct {\n" 2423 " unsigned bit;\n" 2424 " const char *const name;\n" 2425 "} kBitsToOs[] = {\n" 2426 " {kOsMac, \"Mac\"},\n" 2427 " {kOsWin, \"Windows\"},\n" 2428 " {kOsLinux, \"Linux\"},\n" 2429 " {kOsCrOS, \"Chrome OS\"},\n" 2430 "};"); 2431 } 2432 2433 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2434 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2435 " \\\n" 2436 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2437 } 2438 2439 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2440 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2441 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2442 2443 // Do break defaulted and deleted functions. 2444 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2445 " default;", 2446 getLLVMStyleWithColumns(40)); 2447 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2448 " delete;", 2449 getLLVMStyleWithColumns(40)); 2450 } 2451 2452 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2453 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2454 getLLVMStyleWithColumns(40)); 2455 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2456 getLLVMStyleWithColumns(40)); 2457 EXPECT_EQ("#define Q \\\n" 2458 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2459 " \"aaaaaaaa.cpp\"", 2460 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2461 getLLVMStyleWithColumns(40))); 2462 } 2463 2464 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2465 EXPECT_EQ("# 123 \"A string literal\"", 2466 format(" # 123 \"A string literal\"")); 2467 } 2468 2469 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2470 EXPECT_EQ("#;", format("#;")); 2471 verifyFormat("#\n;\n;\n;"); 2472 } 2473 2474 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2475 EXPECT_EQ("#line 42 \"test\"\n", 2476 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2477 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2478 getLLVMStyleWithColumns(12))); 2479 } 2480 2481 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2482 EXPECT_EQ("#line 42 \"test\"", 2483 format("# \\\n line \\\n 42 \\\n \"test\"")); 2484 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2485 } 2486 2487 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2488 verifyFormat("#define A \\x20"); 2489 verifyFormat("#define A \\ x20"); 2490 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2491 verifyFormat("#define A ''"); 2492 verifyFormat("#define A ''qqq"); 2493 verifyFormat("#define A `qqq"); 2494 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2495 EXPECT_EQ("const char *c = STRINGIFY(\n" 2496 "\\na : b);", 2497 format("const char * c = STRINGIFY(\n" 2498 "\\na : b);")); 2499 2500 verifyFormat("a\r\\"); 2501 verifyFormat("a\v\\"); 2502 verifyFormat("a\f\\"); 2503 } 2504 2505 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2506 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2507 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2508 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2509 // FIXME: We never break before the macro name. 2510 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2511 2512 verifyFormat("#define A A\n#define A A"); 2513 verifyFormat("#define A(X) A\n#define A A"); 2514 2515 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2516 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2517 } 2518 2519 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2520 EXPECT_EQ("// somecomment\n" 2521 "#include \"a.h\"\n" 2522 "#define A( \\\n" 2523 " A, B)\n" 2524 "#include \"b.h\"\n" 2525 "// somecomment\n", 2526 format(" // somecomment\n" 2527 " #include \"a.h\"\n" 2528 "#define A(A,\\\n" 2529 " B)\n" 2530 " #include \"b.h\"\n" 2531 " // somecomment\n", 2532 getLLVMStyleWithColumns(13))); 2533 } 2534 2535 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2536 2537 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2538 EXPECT_EQ("#define A \\\n" 2539 " c; \\\n" 2540 " e;\n" 2541 "f;", 2542 format("#define A c; e;\n" 2543 "f;", 2544 getLLVMStyleWithColumns(14))); 2545 } 2546 2547 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2548 2549 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2550 EXPECT_EQ("int x,\n" 2551 "#define A\n" 2552 " y;", 2553 format("int x,\n#define A\ny;")); 2554 } 2555 2556 TEST_F(FormatTest, HashInMacroDefinition) { 2557 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2558 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2559 verifyFormat("#define A \\\n" 2560 " { \\\n" 2561 " f(#c); \\\n" 2562 " }", 2563 getLLVMStyleWithColumns(11)); 2564 2565 verifyFormat("#define A(X) \\\n" 2566 " void function##X()", 2567 getLLVMStyleWithColumns(22)); 2568 2569 verifyFormat("#define A(a, b, c) \\\n" 2570 " void a##b##c()", 2571 getLLVMStyleWithColumns(22)); 2572 2573 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2574 } 2575 2576 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2577 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2578 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2579 2580 FormatStyle Style = getLLVMStyle(); 2581 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 2582 verifyFormat("#define true ((foo)1)", Style); 2583 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 2584 verifyFormat("#define false((foo)0)", Style); 2585 } 2586 2587 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2588 EXPECT_EQ("#define A b;", format("#define A \\\n" 2589 " \\\n" 2590 " b;", 2591 getLLVMStyleWithColumns(25))); 2592 EXPECT_EQ("#define A \\\n" 2593 " \\\n" 2594 " a; \\\n" 2595 " b;", 2596 format("#define A \\\n" 2597 " \\\n" 2598 " a; \\\n" 2599 " b;", 2600 getLLVMStyleWithColumns(11))); 2601 EXPECT_EQ("#define A \\\n" 2602 " a; \\\n" 2603 " \\\n" 2604 " b;", 2605 format("#define A \\\n" 2606 " a; \\\n" 2607 " \\\n" 2608 " b;", 2609 getLLVMStyleWithColumns(11))); 2610 } 2611 2612 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2613 verifyIncompleteFormat("#define A :"); 2614 verifyFormat("#define SOMECASES \\\n" 2615 " case 1: \\\n" 2616 " case 2\n", 2617 getLLVMStyleWithColumns(20)); 2618 verifyFormat("#define MACRO(a) \\\n" 2619 " if (a) \\\n" 2620 " f(); \\\n" 2621 " else \\\n" 2622 " g()", 2623 getLLVMStyleWithColumns(18)); 2624 verifyFormat("#define A template <typename T>"); 2625 verifyIncompleteFormat("#define STR(x) #x\n" 2626 "f(STR(this_is_a_string_literal{));"); 2627 verifyFormat("#pragma omp threadprivate( \\\n" 2628 " y)), // expected-warning", 2629 getLLVMStyleWithColumns(28)); 2630 verifyFormat("#d, = };"); 2631 verifyFormat("#if \"a"); 2632 verifyIncompleteFormat("({\n" 2633 "#define b \\\n" 2634 " } \\\n" 2635 " a\n" 2636 "a", 2637 getLLVMStyleWithColumns(15)); 2638 verifyFormat("#define A \\\n" 2639 " { \\\n" 2640 " {\n" 2641 "#define B \\\n" 2642 " } \\\n" 2643 " }", 2644 getLLVMStyleWithColumns(15)); 2645 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2646 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2647 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2648 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2649 } 2650 2651 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2652 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2653 EXPECT_EQ("class A : public QObject {\n" 2654 " Q_OBJECT\n" 2655 "\n" 2656 " A() {}\n" 2657 "};", 2658 format("class A : public QObject {\n" 2659 " Q_OBJECT\n" 2660 "\n" 2661 " A() {\n}\n" 2662 "} ;")); 2663 EXPECT_EQ("MACRO\n" 2664 "/*static*/ int i;", 2665 format("MACRO\n" 2666 " /*static*/ int i;")); 2667 EXPECT_EQ("SOME_MACRO\n" 2668 "namespace {\n" 2669 "void f();\n" 2670 "} // namespace", 2671 format("SOME_MACRO\n" 2672 " namespace {\n" 2673 "void f( );\n" 2674 "} // namespace")); 2675 // Only if the identifier contains at least 5 characters. 2676 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2677 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2678 // Only if everything is upper case. 2679 EXPECT_EQ("class A : public QObject {\n" 2680 " Q_Object A() {}\n" 2681 "};", 2682 format("class A : public QObject {\n" 2683 " Q_Object\n" 2684 " A() {\n}\n" 2685 "} ;")); 2686 2687 // Only if the next line can actually start an unwrapped line. 2688 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2689 format("SOME_WEIRD_LOG_MACRO\n" 2690 "<< SomeThing;")); 2691 2692 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2693 "(n, buffers))\n", 2694 getChromiumStyle(FormatStyle::LK_Cpp)); 2695 2696 // See PR41483 2697 EXPECT_EQ("/**/ FOO(a)\n" 2698 "FOO(b)", 2699 format("/**/ FOO(a)\n" 2700 "FOO(b)")); 2701 } 2702 2703 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2704 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2705 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2706 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2707 "class X {};\n" 2708 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2709 "int *createScopDetectionPass() { return 0; }", 2710 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2711 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2712 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2713 " class X {};\n" 2714 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2715 " int *createScopDetectionPass() { return 0; }")); 2716 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2717 // braces, so that inner block is indented one level more. 2718 EXPECT_EQ("int q() {\n" 2719 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2720 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2721 " IPC_END_MESSAGE_MAP()\n" 2722 "}", 2723 format("int q() {\n" 2724 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2725 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2726 " IPC_END_MESSAGE_MAP()\n" 2727 "}")); 2728 2729 // Same inside macros. 2730 EXPECT_EQ("#define LIST(L) \\\n" 2731 " L(A) \\\n" 2732 " L(B) \\\n" 2733 " L(C)", 2734 format("#define LIST(L) \\\n" 2735 " L(A) \\\n" 2736 " L(B) \\\n" 2737 " L(C)", 2738 getGoogleStyle())); 2739 2740 // These must not be recognized as macros. 2741 EXPECT_EQ("int q() {\n" 2742 " f(x);\n" 2743 " f(x) {}\n" 2744 " f(x)->g();\n" 2745 " f(x)->*g();\n" 2746 " f(x).g();\n" 2747 " f(x) = x;\n" 2748 " f(x) += x;\n" 2749 " f(x) -= x;\n" 2750 " f(x) *= x;\n" 2751 " f(x) /= x;\n" 2752 " f(x) %= x;\n" 2753 " f(x) &= x;\n" 2754 " f(x) |= x;\n" 2755 " f(x) ^= x;\n" 2756 " f(x) >>= x;\n" 2757 " f(x) <<= x;\n" 2758 " f(x)[y].z();\n" 2759 " LOG(INFO) << x;\n" 2760 " ifstream(x) >> x;\n" 2761 "}\n", 2762 format("int q() {\n" 2763 " f(x)\n;\n" 2764 " f(x)\n {}\n" 2765 " f(x)\n->g();\n" 2766 " f(x)\n->*g();\n" 2767 " f(x)\n.g();\n" 2768 " f(x)\n = x;\n" 2769 " f(x)\n += x;\n" 2770 " f(x)\n -= x;\n" 2771 " f(x)\n *= x;\n" 2772 " f(x)\n /= x;\n" 2773 " f(x)\n %= x;\n" 2774 " f(x)\n &= x;\n" 2775 " f(x)\n |= x;\n" 2776 " f(x)\n ^= x;\n" 2777 " f(x)\n >>= x;\n" 2778 " f(x)\n <<= x;\n" 2779 " f(x)\n[y].z();\n" 2780 " LOG(INFO)\n << x;\n" 2781 " ifstream(x)\n >> x;\n" 2782 "}\n")); 2783 EXPECT_EQ("int q() {\n" 2784 " F(x)\n" 2785 " if (1) {\n" 2786 " }\n" 2787 " F(x)\n" 2788 " while (1) {\n" 2789 " }\n" 2790 " F(x)\n" 2791 " G(x);\n" 2792 " F(x)\n" 2793 " try {\n" 2794 " Q();\n" 2795 " } catch (...) {\n" 2796 " }\n" 2797 "}\n", 2798 format("int q() {\n" 2799 "F(x)\n" 2800 "if (1) {}\n" 2801 "F(x)\n" 2802 "while (1) {}\n" 2803 "F(x)\n" 2804 "G(x);\n" 2805 "F(x)\n" 2806 "try { Q(); } catch (...) {}\n" 2807 "}\n")); 2808 EXPECT_EQ("class A {\n" 2809 " A() : t(0) {}\n" 2810 " A(int i) noexcept() : {}\n" 2811 " A(X x)\n" // FIXME: function-level try blocks are broken. 2812 " try : t(0) {\n" 2813 " } catch (...) {\n" 2814 " }\n" 2815 "};", 2816 format("class A {\n" 2817 " A()\n : t(0) {}\n" 2818 " A(int i)\n noexcept() : {}\n" 2819 " A(X x)\n" 2820 " try : t(0) {} catch (...) {}\n" 2821 "};")); 2822 FormatStyle Style = getLLVMStyle(); 2823 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2824 Style.BraceWrapping.AfterControlStatement = true; 2825 Style.BraceWrapping.AfterFunction = true; 2826 EXPECT_EQ("void f()\n" 2827 "try\n" 2828 "{\n" 2829 "}", 2830 format("void f() try {\n" 2831 "}", Style)); 2832 EXPECT_EQ("class SomeClass {\n" 2833 "public:\n" 2834 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2835 "};", 2836 format("class SomeClass {\n" 2837 "public:\n" 2838 " SomeClass()\n" 2839 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2840 "};")); 2841 EXPECT_EQ("class SomeClass {\n" 2842 "public:\n" 2843 " SomeClass()\n" 2844 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2845 "};", 2846 format("class SomeClass {\n" 2847 "public:\n" 2848 " SomeClass()\n" 2849 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2850 "};", 2851 getLLVMStyleWithColumns(40))); 2852 2853 verifyFormat("MACRO(>)"); 2854 2855 // Some macros contain an implicit semicolon. 2856 Style = getLLVMStyle(); 2857 Style.StatementMacros.push_back("FOO"); 2858 verifyFormat("FOO(a) int b = 0;"); 2859 verifyFormat("FOO(a)\n" 2860 "int b = 0;", 2861 Style); 2862 verifyFormat("FOO(a);\n" 2863 "int b = 0;", 2864 Style); 2865 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 2866 "int b = 0;", 2867 Style); 2868 verifyFormat("FOO()\n" 2869 "int b = 0;", 2870 Style); 2871 verifyFormat("FOO\n" 2872 "int b = 0;", 2873 Style); 2874 verifyFormat("void f() {\n" 2875 " FOO(a)\n" 2876 " return a;\n" 2877 "}", 2878 Style); 2879 verifyFormat("FOO(a)\n" 2880 "FOO(b)", 2881 Style); 2882 verifyFormat("int a = 0;\n" 2883 "FOO(b)\n" 2884 "int c = 0;", 2885 Style); 2886 verifyFormat("int a = 0;\n" 2887 "int x = FOO(a)\n" 2888 "int b = 0;", 2889 Style); 2890 verifyFormat("void foo(int a) { FOO(a) }\n" 2891 "uint32_t bar() {}", 2892 Style); 2893 } 2894 2895 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2896 verifyFormat("#define A \\\n" 2897 " f({ \\\n" 2898 " g(); \\\n" 2899 " });", 2900 getLLVMStyleWithColumns(11)); 2901 } 2902 2903 TEST_F(FormatTest, IndentPreprocessorDirectives) { 2904 FormatStyle Style = getLLVMStyle(); 2905 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 2906 Style.ColumnLimit = 40; 2907 verifyFormat("#ifdef _WIN32\n" 2908 "#define A 0\n" 2909 "#ifdef VAR2\n" 2910 "#define B 1\n" 2911 "#include <someheader.h>\n" 2912 "#define MACRO \\\n" 2913 " some_very_long_func_aaaaaaaaaa();\n" 2914 "#endif\n" 2915 "#else\n" 2916 "#define A 1\n" 2917 "#endif", 2918 Style); 2919 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 2920 verifyFormat("#ifdef _WIN32\n" 2921 "# define A 0\n" 2922 "# ifdef VAR2\n" 2923 "# define B 1\n" 2924 "# include <someheader.h>\n" 2925 "# define MACRO \\\n" 2926 " some_very_long_func_aaaaaaaaaa();\n" 2927 "# endif\n" 2928 "#else\n" 2929 "# define A 1\n" 2930 "#endif", 2931 Style); 2932 verifyFormat("#if A\n" 2933 "# define MACRO \\\n" 2934 " void a(int x) { \\\n" 2935 " b(); \\\n" 2936 " c(); \\\n" 2937 " d(); \\\n" 2938 " e(); \\\n" 2939 " f(); \\\n" 2940 " }\n" 2941 "#endif", 2942 Style); 2943 // Comments before include guard. 2944 verifyFormat("// file comment\n" 2945 "// file comment\n" 2946 "#ifndef HEADER_H\n" 2947 "#define HEADER_H\n" 2948 "code();\n" 2949 "#endif", 2950 Style); 2951 // Test with include guards. 2952 verifyFormat("#ifndef HEADER_H\n" 2953 "#define HEADER_H\n" 2954 "code();\n" 2955 "#endif", 2956 Style); 2957 // Include guards must have a #define with the same variable immediately 2958 // after #ifndef. 2959 verifyFormat("#ifndef NOT_GUARD\n" 2960 "# define FOO\n" 2961 "code();\n" 2962 "#endif", 2963 Style); 2964 2965 // Include guards must cover the entire file. 2966 verifyFormat("code();\n" 2967 "code();\n" 2968 "#ifndef NOT_GUARD\n" 2969 "# define NOT_GUARD\n" 2970 "code();\n" 2971 "#endif", 2972 Style); 2973 verifyFormat("#ifndef NOT_GUARD\n" 2974 "# define NOT_GUARD\n" 2975 "code();\n" 2976 "#endif\n" 2977 "code();", 2978 Style); 2979 // Test with trailing blank lines. 2980 verifyFormat("#ifndef HEADER_H\n" 2981 "#define HEADER_H\n" 2982 "code();\n" 2983 "#endif\n", 2984 Style); 2985 // Include guards don't have #else. 2986 verifyFormat("#ifndef NOT_GUARD\n" 2987 "# define NOT_GUARD\n" 2988 "code();\n" 2989 "#else\n" 2990 "#endif", 2991 Style); 2992 verifyFormat("#ifndef NOT_GUARD\n" 2993 "# define NOT_GUARD\n" 2994 "code();\n" 2995 "#elif FOO\n" 2996 "#endif", 2997 Style); 2998 // Non-identifier #define after potential include guard. 2999 verifyFormat("#ifndef FOO\n" 3000 "# define 1\n" 3001 "#endif\n", 3002 Style); 3003 // #if closes past last non-preprocessor line. 3004 verifyFormat("#ifndef FOO\n" 3005 "#define FOO\n" 3006 "#if 1\n" 3007 "int i;\n" 3008 "# define A 0\n" 3009 "#endif\n" 3010 "#endif\n", 3011 Style); 3012 // FIXME: This doesn't handle the case where there's code between the 3013 // #ifndef and #define but all other conditions hold. This is because when 3014 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 3015 // previous code line yet, so we can't detect it. 3016 EXPECT_EQ("#ifndef NOT_GUARD\n" 3017 "code();\n" 3018 "#define NOT_GUARD\n" 3019 "code();\n" 3020 "#endif", 3021 format("#ifndef NOT_GUARD\n" 3022 "code();\n" 3023 "# define NOT_GUARD\n" 3024 "code();\n" 3025 "#endif", 3026 Style)); 3027 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 3028 // be outside an include guard. Examples are #pragma once and 3029 // #pragma GCC diagnostic, or anything else that does not change the meaning 3030 // of the file if it's included multiple times. 3031 EXPECT_EQ("#ifdef WIN32\n" 3032 "# pragma once\n" 3033 "#endif\n" 3034 "#ifndef HEADER_H\n" 3035 "# define HEADER_H\n" 3036 "code();\n" 3037 "#endif", 3038 format("#ifdef WIN32\n" 3039 "# pragma once\n" 3040 "#endif\n" 3041 "#ifndef HEADER_H\n" 3042 "#define HEADER_H\n" 3043 "code();\n" 3044 "#endif", 3045 Style)); 3046 // FIXME: This does not detect when there is a single non-preprocessor line 3047 // in front of an include-guard-like structure where other conditions hold 3048 // because ScopedLineState hides the line. 3049 EXPECT_EQ("code();\n" 3050 "#ifndef HEADER_H\n" 3051 "#define HEADER_H\n" 3052 "code();\n" 3053 "#endif", 3054 format("code();\n" 3055 "#ifndef HEADER_H\n" 3056 "# define HEADER_H\n" 3057 "code();\n" 3058 "#endif", 3059 Style)); 3060 // Keep comments aligned with #, otherwise indent comments normally. These 3061 // tests cannot use verifyFormat because messUp manipulates leading 3062 // whitespace. 3063 { 3064 const char *Expected = "" 3065 "void f() {\n" 3066 "#if 1\n" 3067 "// Preprocessor aligned.\n" 3068 "# define A 0\n" 3069 " // Code. Separated by blank line.\n" 3070 "\n" 3071 "# define B 0\n" 3072 " // Code. Not aligned with #\n" 3073 "# define C 0\n" 3074 "#endif"; 3075 const char *ToFormat = "" 3076 "void f() {\n" 3077 "#if 1\n" 3078 "// Preprocessor aligned.\n" 3079 "# define A 0\n" 3080 "// Code. Separated by blank line.\n" 3081 "\n" 3082 "# define B 0\n" 3083 " // Code. Not aligned with #\n" 3084 "# define C 0\n" 3085 "#endif"; 3086 EXPECT_EQ(Expected, format(ToFormat, Style)); 3087 EXPECT_EQ(Expected, format(Expected, Style)); 3088 } 3089 // Keep block quotes aligned. 3090 { 3091 const char *Expected = "" 3092 "void f() {\n" 3093 "#if 1\n" 3094 "/* Preprocessor aligned. */\n" 3095 "# define A 0\n" 3096 " /* Code. Separated by blank line. */\n" 3097 "\n" 3098 "# define B 0\n" 3099 " /* Code. Not aligned with # */\n" 3100 "# define C 0\n" 3101 "#endif"; 3102 const char *ToFormat = "" 3103 "void f() {\n" 3104 "#if 1\n" 3105 "/* Preprocessor aligned. */\n" 3106 "# define A 0\n" 3107 "/* Code. Separated by blank line. */\n" 3108 "\n" 3109 "# define B 0\n" 3110 " /* Code. Not aligned with # */\n" 3111 "# define C 0\n" 3112 "#endif"; 3113 EXPECT_EQ(Expected, format(ToFormat, Style)); 3114 EXPECT_EQ(Expected, format(Expected, Style)); 3115 } 3116 // Keep comments aligned with un-indented directives. 3117 { 3118 const char *Expected = "" 3119 "void f() {\n" 3120 "// Preprocessor aligned.\n" 3121 "#define A 0\n" 3122 " // Code. Separated by blank line.\n" 3123 "\n" 3124 "#define B 0\n" 3125 " // Code. Not aligned with #\n" 3126 "#define C 0\n"; 3127 const char *ToFormat = "" 3128 "void f() {\n" 3129 "// Preprocessor aligned.\n" 3130 "#define A 0\n" 3131 "// Code. Separated by blank line.\n" 3132 "\n" 3133 "#define B 0\n" 3134 " // Code. Not aligned with #\n" 3135 "#define C 0\n"; 3136 EXPECT_EQ(Expected, format(ToFormat, Style)); 3137 EXPECT_EQ(Expected, format(Expected, Style)); 3138 } 3139 // Test AfterHash with tabs. 3140 { 3141 FormatStyle Tabbed = Style; 3142 Tabbed.UseTab = FormatStyle::UT_Always; 3143 Tabbed.IndentWidth = 8; 3144 Tabbed.TabWidth = 8; 3145 verifyFormat("#ifdef _WIN32\n" 3146 "#\tdefine A 0\n" 3147 "#\tifdef VAR2\n" 3148 "#\t\tdefine B 1\n" 3149 "#\t\tinclude <someheader.h>\n" 3150 "#\t\tdefine MACRO \\\n" 3151 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 3152 "#\tendif\n" 3153 "#else\n" 3154 "#\tdefine A 1\n" 3155 "#endif", 3156 Tabbed); 3157 } 3158 3159 // Regression test: Multiline-macro inside include guards. 3160 verifyFormat("#ifndef HEADER_H\n" 3161 "#define HEADER_H\n" 3162 "#define A() \\\n" 3163 " int i; \\\n" 3164 " int j;\n" 3165 "#endif // HEADER_H", 3166 getLLVMStyleWithColumns(20)); 3167 3168 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 3169 // Basic before hash indent tests 3170 verifyFormat("#ifdef _WIN32\n" 3171 " #define A 0\n" 3172 " #ifdef VAR2\n" 3173 " #define B 1\n" 3174 " #include <someheader.h>\n" 3175 " #define MACRO \\\n" 3176 " some_very_long_func_aaaaaaaaaa();\n" 3177 " #endif\n" 3178 "#else\n" 3179 " #define A 1\n" 3180 "#endif", 3181 Style); 3182 verifyFormat("#if A\n" 3183 " #define MACRO \\\n" 3184 " void a(int x) { \\\n" 3185 " b(); \\\n" 3186 " c(); \\\n" 3187 " d(); \\\n" 3188 " e(); \\\n" 3189 " f(); \\\n" 3190 " }\n" 3191 "#endif", 3192 Style); 3193 // Keep comments aligned with indented directives. These 3194 // tests cannot use verifyFormat because messUp manipulates leading 3195 // whitespace. 3196 { 3197 const char *Expected = "void f() {\n" 3198 "// Aligned to preprocessor.\n" 3199 "#if 1\n" 3200 " // Aligned to code.\n" 3201 " int a;\n" 3202 " #if 1\n" 3203 " // Aligned to preprocessor.\n" 3204 " #define A 0\n" 3205 " // Aligned to code.\n" 3206 " int b;\n" 3207 " #endif\n" 3208 "#endif\n" 3209 "}"; 3210 const char *ToFormat = "void f() {\n" 3211 "// Aligned to preprocessor.\n" 3212 "#if 1\n" 3213 "// Aligned to code.\n" 3214 "int a;\n" 3215 "#if 1\n" 3216 "// Aligned to preprocessor.\n" 3217 "#define A 0\n" 3218 "// Aligned to code.\n" 3219 "int b;\n" 3220 "#endif\n" 3221 "#endif\n" 3222 "}"; 3223 EXPECT_EQ(Expected, format(ToFormat, Style)); 3224 EXPECT_EQ(Expected, format(Expected, Style)); 3225 } 3226 { 3227 const char *Expected = "void f() {\n" 3228 "/* Aligned to preprocessor. */\n" 3229 "#if 1\n" 3230 " /* Aligned to code. */\n" 3231 " int a;\n" 3232 " #if 1\n" 3233 " /* Aligned to preprocessor. */\n" 3234 " #define A 0\n" 3235 " /* Aligned to code. */\n" 3236 " int b;\n" 3237 " #endif\n" 3238 "#endif\n" 3239 "}"; 3240 const char *ToFormat = "void f() {\n" 3241 "/* Aligned to preprocessor. */\n" 3242 "#if 1\n" 3243 "/* Aligned to code. */\n" 3244 "int a;\n" 3245 "#if 1\n" 3246 "/* Aligned to preprocessor. */\n" 3247 "#define A 0\n" 3248 "/* Aligned to code. */\n" 3249 "int b;\n" 3250 "#endif\n" 3251 "#endif\n" 3252 "}"; 3253 EXPECT_EQ(Expected, format(ToFormat, Style)); 3254 EXPECT_EQ(Expected, format(Expected, Style)); 3255 } 3256 3257 // Test single comment before preprocessor 3258 verifyFormat("// Comment\n" 3259 "\n" 3260 "#if 1\n" 3261 "#endif", 3262 Style); 3263 } 3264 3265 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 3266 verifyFormat("{\n { a #c; }\n}"); 3267 } 3268 3269 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 3270 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 3271 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 3272 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 3273 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 3274 } 3275 3276 TEST_F(FormatTest, EscapedNewlines) { 3277 FormatStyle Narrow = getLLVMStyleWithColumns(11); 3278 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 3279 format("#define A \\\nint i;\\\n int j;", Narrow)); 3280 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 3281 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3282 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 3283 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 3284 3285 FormatStyle AlignLeft = getLLVMStyle(); 3286 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 3287 EXPECT_EQ("#define MACRO(x) \\\n" 3288 "private: \\\n" 3289 " int x(int a);\n", 3290 format("#define MACRO(x) \\\n" 3291 "private: \\\n" 3292 " int x(int a);\n", 3293 AlignLeft)); 3294 3295 // CRLF line endings 3296 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 3297 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 3298 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 3299 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3300 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 3301 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 3302 EXPECT_EQ("#define MACRO(x) \\\r\n" 3303 "private: \\\r\n" 3304 " int x(int a);\r\n", 3305 format("#define MACRO(x) \\\r\n" 3306 "private: \\\r\n" 3307 " int x(int a);\r\n", 3308 AlignLeft)); 3309 3310 FormatStyle DontAlign = getLLVMStyle(); 3311 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 3312 DontAlign.MaxEmptyLinesToKeep = 3; 3313 // FIXME: can't use verifyFormat here because the newline before 3314 // "public:" is not inserted the first time it's reformatted 3315 EXPECT_EQ("#define A \\\n" 3316 " class Foo { \\\n" 3317 " void bar(); \\\n" 3318 "\\\n" 3319 "\\\n" 3320 "\\\n" 3321 " public: \\\n" 3322 " void baz(); \\\n" 3323 " };", 3324 format("#define A \\\n" 3325 " class Foo { \\\n" 3326 " void bar(); \\\n" 3327 "\\\n" 3328 "\\\n" 3329 "\\\n" 3330 " public: \\\n" 3331 " void baz(); \\\n" 3332 " };", 3333 DontAlign)); 3334 } 3335 3336 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3337 verifyFormat("#define A \\\n" 3338 " int v( \\\n" 3339 " a); \\\n" 3340 " int i;", 3341 getLLVMStyleWithColumns(11)); 3342 } 3343 3344 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3345 EXPECT_EQ( 3346 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3347 " \\\n" 3348 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3349 "\n" 3350 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3351 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3352 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3353 "\\\n" 3354 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3355 " \n" 3356 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3357 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3358 } 3359 3360 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3361 EXPECT_EQ("int\n" 3362 "#define A\n" 3363 " a;", 3364 format("int\n#define A\na;")); 3365 verifyFormat("functionCallTo(\n" 3366 " someOtherFunction(\n" 3367 " withSomeParameters, whichInSequence,\n" 3368 " areLongerThanALine(andAnotherCall,\n" 3369 "#define A B\n" 3370 " withMoreParamters,\n" 3371 " whichStronglyInfluenceTheLayout),\n" 3372 " andMoreParameters),\n" 3373 " trailing);", 3374 getLLVMStyleWithColumns(69)); 3375 verifyFormat("Foo::Foo()\n" 3376 "#ifdef BAR\n" 3377 " : baz(0)\n" 3378 "#endif\n" 3379 "{\n" 3380 "}"); 3381 verifyFormat("void f() {\n" 3382 " if (true)\n" 3383 "#ifdef A\n" 3384 " f(42);\n" 3385 " x();\n" 3386 "#else\n" 3387 " g();\n" 3388 " x();\n" 3389 "#endif\n" 3390 "}"); 3391 verifyFormat("void f(param1, param2,\n" 3392 " param3,\n" 3393 "#ifdef A\n" 3394 " param4(param5,\n" 3395 "#ifdef A1\n" 3396 " param6,\n" 3397 "#ifdef A2\n" 3398 " param7),\n" 3399 "#else\n" 3400 " param8),\n" 3401 " param9,\n" 3402 "#endif\n" 3403 " param10,\n" 3404 "#endif\n" 3405 " param11)\n" 3406 "#else\n" 3407 " param12)\n" 3408 "#endif\n" 3409 "{\n" 3410 " x();\n" 3411 "}", 3412 getLLVMStyleWithColumns(28)); 3413 verifyFormat("#if 1\n" 3414 "int i;"); 3415 verifyFormat("#if 1\n" 3416 "#endif\n" 3417 "#if 1\n" 3418 "#else\n" 3419 "#endif\n"); 3420 verifyFormat("DEBUG({\n" 3421 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3422 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3423 "});\n" 3424 "#if a\n" 3425 "#else\n" 3426 "#endif"); 3427 3428 verifyIncompleteFormat("void f(\n" 3429 "#if A\n" 3430 ");\n" 3431 "#else\n" 3432 "#endif"); 3433 } 3434 3435 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3436 verifyFormat("#endif\n" 3437 "#if B"); 3438 } 3439 3440 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3441 FormatStyle SingleLine = getLLVMStyle(); 3442 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 3443 verifyFormat("#if 0\n" 3444 "#elif 1\n" 3445 "#endif\n" 3446 "void foo() {\n" 3447 " if (test) foo2();\n" 3448 "}", 3449 SingleLine); 3450 } 3451 3452 TEST_F(FormatTest, LayoutBlockInsideParens) { 3453 verifyFormat("functionCall({ int i; });"); 3454 verifyFormat("functionCall({\n" 3455 " int i;\n" 3456 " int j;\n" 3457 "});"); 3458 verifyFormat("functionCall(\n" 3459 " {\n" 3460 " int i;\n" 3461 " int j;\n" 3462 " },\n" 3463 " aaaa, bbbb, cccc);"); 3464 verifyFormat("functionA(functionB({\n" 3465 " int i;\n" 3466 " int j;\n" 3467 " }),\n" 3468 " aaaa, bbbb, cccc);"); 3469 verifyFormat("functionCall(\n" 3470 " {\n" 3471 " int i;\n" 3472 " int j;\n" 3473 " },\n" 3474 " aaaa, bbbb, // comment\n" 3475 " cccc);"); 3476 verifyFormat("functionA(functionB({\n" 3477 " int i;\n" 3478 " int j;\n" 3479 " }),\n" 3480 " aaaa, bbbb, // comment\n" 3481 " cccc);"); 3482 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3483 verifyFormat("functionCall(aaaa, bbbb, {\n" 3484 " int i;\n" 3485 " int j;\n" 3486 "});"); 3487 verifyFormat( 3488 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3489 " {\n" 3490 " int i; // break\n" 3491 " },\n" 3492 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3493 " ccccccccccccccccc));"); 3494 verifyFormat("DEBUG({\n" 3495 " if (a)\n" 3496 " f();\n" 3497 "});"); 3498 } 3499 3500 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3501 EXPECT_EQ("SOME_MACRO { int i; }\n" 3502 "int i;", 3503 format(" SOME_MACRO {int i;} int i;")); 3504 } 3505 3506 TEST_F(FormatTest, LayoutNestedBlocks) { 3507 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3508 " struct s {\n" 3509 " int i;\n" 3510 " };\n" 3511 " s kBitsToOs[] = {{10}};\n" 3512 " for (int i = 0; i < 10; ++i)\n" 3513 " return;\n" 3514 "}"); 3515 verifyFormat("call(parameter, {\n" 3516 " something();\n" 3517 " // Comment using all columns.\n" 3518 " somethingelse();\n" 3519 "});", 3520 getLLVMStyleWithColumns(40)); 3521 verifyFormat("DEBUG( //\n" 3522 " { f(); }, a);"); 3523 verifyFormat("DEBUG( //\n" 3524 " {\n" 3525 " f(); //\n" 3526 " },\n" 3527 " a);"); 3528 3529 EXPECT_EQ("call(parameter, {\n" 3530 " something();\n" 3531 " // Comment too\n" 3532 " // looooooooooong.\n" 3533 " somethingElse();\n" 3534 "});", 3535 format("call(parameter, {\n" 3536 " something();\n" 3537 " // Comment too looooooooooong.\n" 3538 " somethingElse();\n" 3539 "});", 3540 getLLVMStyleWithColumns(29))); 3541 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3542 EXPECT_EQ("DEBUG({ // comment\n" 3543 " int i;\n" 3544 "});", 3545 format("DEBUG({ // comment\n" 3546 "int i;\n" 3547 "});")); 3548 EXPECT_EQ("DEBUG({\n" 3549 " int i;\n" 3550 "\n" 3551 " // comment\n" 3552 " int j;\n" 3553 "});", 3554 format("DEBUG({\n" 3555 " int i;\n" 3556 "\n" 3557 " // comment\n" 3558 " int j;\n" 3559 "});")); 3560 3561 verifyFormat("DEBUG({\n" 3562 " if (a)\n" 3563 " return;\n" 3564 "});"); 3565 verifyGoogleFormat("DEBUG({\n" 3566 " if (a) return;\n" 3567 "});"); 3568 FormatStyle Style = getGoogleStyle(); 3569 Style.ColumnLimit = 45; 3570 verifyFormat("Debug(\n" 3571 " aaaaa,\n" 3572 " {\n" 3573 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3574 " },\n" 3575 " a);", 3576 Style); 3577 3578 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3579 3580 verifyNoCrash("^{v^{a}}"); 3581 } 3582 3583 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3584 EXPECT_EQ("#define MACRO() \\\n" 3585 " Debug(aaa, /* force line break */ \\\n" 3586 " { \\\n" 3587 " int i; \\\n" 3588 " int j; \\\n" 3589 " })", 3590 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3591 " { int i; int j; })", 3592 getGoogleStyle())); 3593 3594 EXPECT_EQ("#define A \\\n" 3595 " [] { \\\n" 3596 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3597 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3598 " }", 3599 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3600 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3601 getGoogleStyle())); 3602 } 3603 3604 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3605 EXPECT_EQ("{}", format("{}")); 3606 verifyFormat("enum E {};"); 3607 verifyFormat("enum E {}"); 3608 } 3609 3610 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3611 FormatStyle Style = getLLVMStyle(); 3612 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3613 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3614 verifyFormat("FOO_BEGIN\n" 3615 " FOO_ENTRY\n" 3616 "FOO_END", Style); 3617 verifyFormat("FOO_BEGIN\n" 3618 " NESTED_FOO_BEGIN\n" 3619 " NESTED_FOO_ENTRY\n" 3620 " NESTED_FOO_END\n" 3621 "FOO_END", Style); 3622 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3623 " int x;\n" 3624 " x = 1;\n" 3625 "FOO_END(Baz)", Style); 3626 } 3627 3628 //===----------------------------------------------------------------------===// 3629 // Line break tests. 3630 //===----------------------------------------------------------------------===// 3631 3632 TEST_F(FormatTest, PreventConfusingIndents) { 3633 verifyFormat( 3634 "void f() {\n" 3635 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3636 " parameter, parameter, parameter)),\n" 3637 " SecondLongCall(parameter));\n" 3638 "}"); 3639 verifyFormat( 3640 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3641 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3642 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3643 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3644 verifyFormat( 3645 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3646 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3647 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3648 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3649 verifyFormat( 3650 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3651 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3652 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3653 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3654 verifyFormat("int a = bbbb && ccc &&\n" 3655 " fffff(\n" 3656 "#define A Just forcing a new line\n" 3657 " ddd);"); 3658 } 3659 3660 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3661 verifyFormat( 3662 "bool aaaaaaa =\n" 3663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3664 " bbbbbbbb();"); 3665 verifyFormat( 3666 "bool aaaaaaa =\n" 3667 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3668 " bbbbbbbb();"); 3669 3670 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3671 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3672 " ccccccccc == ddddddddddd;"); 3673 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3674 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3675 " ccccccccc == ddddddddddd;"); 3676 verifyFormat( 3677 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3678 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3679 " ccccccccc == ddddddddddd;"); 3680 3681 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3682 " aaaaaa) &&\n" 3683 " bbbbbb && cccccc;"); 3684 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3685 " aaaaaa) >>\n" 3686 " bbbbbb;"); 3687 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3688 " SourceMgr.getSpellingColumnNumber(\n" 3689 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3690 " 1);"); 3691 3692 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3693 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3694 " cccccc) {\n}"); 3695 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3696 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 3697 " cccccc) {\n}"); 3698 verifyFormat("b = a &&\n" 3699 " // Comment\n" 3700 " b.c && d;"); 3701 3702 // If the LHS of a comparison is not a binary expression itself, the 3703 // additional linebreak confuses many people. 3704 verifyFormat( 3705 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3706 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3707 "}"); 3708 verifyFormat( 3709 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3711 "}"); 3712 verifyFormat( 3713 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3715 "}"); 3716 verifyFormat( 3717 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3718 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 3719 "}"); 3720 // Even explicit parentheses stress the precedence enough to make the 3721 // additional break unnecessary. 3722 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3723 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3724 "}"); 3725 // This cases is borderline, but with the indentation it is still readable. 3726 verifyFormat( 3727 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3728 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3730 "}", 3731 getLLVMStyleWithColumns(75)); 3732 3733 // If the LHS is a binary expression, we should still use the additional break 3734 // as otherwise the formatting hides the operator precedence. 3735 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3737 " 5) {\n" 3738 "}"); 3739 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3740 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 3741 " 5) {\n" 3742 "}"); 3743 3744 FormatStyle OnePerLine = getLLVMStyle(); 3745 OnePerLine.BinPackParameters = false; 3746 verifyFormat( 3747 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3748 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3750 OnePerLine); 3751 3752 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 3753 " .aaa(aaaaaaaaaaaaa) *\n" 3754 " aaaaaaa +\n" 3755 " aaaaaaa;", 3756 getLLVMStyleWithColumns(40)); 3757 } 3758 3759 TEST_F(FormatTest, ExpressionIndentation) { 3760 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3761 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3762 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3763 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3764 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3765 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3766 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3767 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3768 " ccccccccccccccccccccccccccccccccccccccccc;"); 3769 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3770 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3771 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3772 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3773 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3774 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3775 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3776 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3777 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3778 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3779 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3780 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3781 verifyFormat("if () {\n" 3782 "} else if (aaaaa && bbbbb > // break\n" 3783 " ccccc) {\n" 3784 "}"); 3785 verifyFormat("if () {\n" 3786 "} else if (aaaaa &&\n" 3787 " bbbbb > // break\n" 3788 " ccccc &&\n" 3789 " ddddd) {\n" 3790 "}"); 3791 3792 // Presence of a trailing comment used to change indentation of b. 3793 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3794 " b;\n" 3795 "return aaaaaaaaaaaaaaaaaaa +\n" 3796 " b; //", 3797 getLLVMStyleWithColumns(30)); 3798 } 3799 3800 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3801 // Not sure what the best system is here. Like this, the LHS can be found 3802 // immediately above an operator (everything with the same or a higher 3803 // indent). The RHS is aligned right of the operator and so compasses 3804 // everything until something with the same indent as the operator is found. 3805 // FIXME: Is this a good system? 3806 FormatStyle Style = getLLVMStyle(); 3807 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3808 verifyFormat( 3809 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3810 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3811 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3812 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3813 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3814 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3815 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3816 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3817 " > ccccccccccccccccccccccccccccccccccccccccc;", 3818 Style); 3819 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3820 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3821 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3822 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3823 Style); 3824 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3825 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3826 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3827 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3828 Style); 3829 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3830 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3831 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3832 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3833 Style); 3834 verifyFormat("if () {\n" 3835 "} else if (aaaaa\n" 3836 " && bbbbb // break\n" 3837 " > ccccc) {\n" 3838 "}", 3839 Style); 3840 verifyFormat("return (a)\n" 3841 " // comment\n" 3842 " + b;", 3843 Style); 3844 verifyFormat( 3845 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3846 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3847 " + cc;", 3848 Style); 3849 3850 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3851 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3852 Style); 3853 3854 // Forced by comments. 3855 verifyFormat( 3856 "unsigned ContentSize =\n" 3857 " sizeof(int16_t) // DWARF ARange version number\n" 3858 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3859 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3860 " + sizeof(int8_t); // Segment Size (in bytes)"); 3861 3862 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3863 " == boost::fusion::at_c<1>(iiii).second;", 3864 Style); 3865 3866 Style.ColumnLimit = 60; 3867 verifyFormat("zzzzzzzzzz\n" 3868 " = bbbbbbbbbbbbbbbbb\n" 3869 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3870 Style); 3871 3872 Style.ColumnLimit = 80; 3873 Style.IndentWidth = 4; 3874 Style.TabWidth = 4; 3875 Style.UseTab = FormatStyle::UT_Always; 3876 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3877 Style.AlignOperands = false; 3878 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 3879 "\t&& (someOtherLongishConditionPart1\n" 3880 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 3881 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && (someOtherLongishConditionPart1 || someOtherEvenLongerNestedConditionPart2);", 3882 Style)); 3883 } 3884 3885 TEST_F(FormatTest, EnforcedOperatorWraps) { 3886 // Here we'd like to wrap after the || operators, but a comment is forcing an 3887 // earlier wrap. 3888 verifyFormat("bool x = aaaaa //\n" 3889 " || bbbbb\n" 3890 " //\n" 3891 " || cccc;"); 3892 } 3893 3894 TEST_F(FormatTest, NoOperandAlignment) { 3895 FormatStyle Style = getLLVMStyle(); 3896 Style.AlignOperands = false; 3897 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 3898 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3900 Style); 3901 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3902 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3903 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3904 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3905 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3906 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3907 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3908 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3909 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3910 " > ccccccccccccccccccccccccccccccccccccccccc;", 3911 Style); 3912 3913 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3914 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3915 " + cc;", 3916 Style); 3917 verifyFormat("int a = aa\n" 3918 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3919 " * cccccccccccccccccccccccccccccccccccc;\n", 3920 Style); 3921 3922 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3923 verifyFormat("return (a > b\n" 3924 " // comment1\n" 3925 " // comment2\n" 3926 " || c);", 3927 Style); 3928 } 3929 3930 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3931 FormatStyle Style = getLLVMStyle(); 3932 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3933 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3934 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3935 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3936 Style); 3937 } 3938 3939 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 3940 FormatStyle Style = getLLVMStyle(); 3941 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3942 Style.BinPackArguments = false; 3943 Style.ColumnLimit = 40; 3944 verifyFormat("void test() {\n" 3945 " someFunction(\n" 3946 " this + argument + is + quite\n" 3947 " + long + so + it + gets + wrapped\n" 3948 " + but + remains + bin - packed);\n" 3949 "}", 3950 Style); 3951 verifyFormat("void test() {\n" 3952 " someFunction(arg1,\n" 3953 " this + argument + is\n" 3954 " + quite + long + so\n" 3955 " + it + gets + wrapped\n" 3956 " + but + remains + bin\n" 3957 " - packed,\n" 3958 " arg3);\n" 3959 "}", 3960 Style); 3961 verifyFormat("void test() {\n" 3962 " someFunction(\n" 3963 " arg1,\n" 3964 " this + argument + has\n" 3965 " + anotherFunc(nested,\n" 3966 " calls + whose\n" 3967 " + arguments\n" 3968 " + are + also\n" 3969 " + wrapped,\n" 3970 " in + addition)\n" 3971 " + to + being + bin - packed,\n" 3972 " arg3);\n" 3973 "}", 3974 Style); 3975 3976 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 3977 verifyFormat("void test() {\n" 3978 " someFunction(\n" 3979 " arg1,\n" 3980 " this + argument + has +\n" 3981 " anotherFunc(nested,\n" 3982 " calls + whose +\n" 3983 " arguments +\n" 3984 " are + also +\n" 3985 " wrapped,\n" 3986 " in + addition) +\n" 3987 " to + being + bin - packed,\n" 3988 " arg3);\n" 3989 "}", 3990 Style); 3991 } 3992 3993 TEST_F(FormatTest, ConstructorInitializers) { 3994 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3995 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3996 getLLVMStyleWithColumns(45)); 3997 verifyFormat("Constructor()\n" 3998 " : Inttializer(FitsOnTheLine) {}", 3999 getLLVMStyleWithColumns(44)); 4000 verifyFormat("Constructor()\n" 4001 " : Inttializer(FitsOnTheLine) {}", 4002 getLLVMStyleWithColumns(43)); 4003 4004 verifyFormat("template <typename T>\n" 4005 "Constructor() : Initializer(FitsOnTheLine) {}", 4006 getLLVMStyleWithColumns(45)); 4007 4008 verifyFormat( 4009 "SomeClass::Constructor()\n" 4010 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 4011 4012 verifyFormat( 4013 "SomeClass::Constructor()\n" 4014 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4015 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 4016 verifyFormat( 4017 "SomeClass::Constructor()\n" 4018 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4019 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 4020 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4021 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4022 " : aaaaaaaaaa(aaaaaa) {}"); 4023 4024 verifyFormat("Constructor()\n" 4025 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4026 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4027 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4028 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 4029 4030 verifyFormat("Constructor()\n" 4031 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4032 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4033 4034 verifyFormat("Constructor(int Parameter = 0)\n" 4035 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 4036 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 4037 verifyFormat("Constructor()\n" 4038 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 4039 "}", 4040 getLLVMStyleWithColumns(60)); 4041 verifyFormat("Constructor()\n" 4042 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4043 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 4044 4045 // Here a line could be saved by splitting the second initializer onto two 4046 // lines, but that is not desirable. 4047 verifyFormat("Constructor()\n" 4048 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 4049 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 4050 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4051 4052 FormatStyle OnePerLine = getLLVMStyle(); 4053 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4054 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 4055 verifyFormat("SomeClass::Constructor()\n" 4056 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4057 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4058 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4059 OnePerLine); 4060 verifyFormat("SomeClass::Constructor()\n" 4061 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 4062 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4063 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4064 OnePerLine); 4065 verifyFormat("MyClass::MyClass(int var)\n" 4066 " : some_var_(var), // 4 space indent\n" 4067 " some_other_var_(var + 1) { // lined up\n" 4068 "}", 4069 OnePerLine); 4070 verifyFormat("Constructor()\n" 4071 " : aaaaa(aaaaaa),\n" 4072 " aaaaa(aaaaaa),\n" 4073 " aaaaa(aaaaaa),\n" 4074 " aaaaa(aaaaaa),\n" 4075 " aaaaa(aaaaaa) {}", 4076 OnePerLine); 4077 verifyFormat("Constructor()\n" 4078 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 4079 " aaaaaaaaaaaaaaaaaaaaaa) {}", 4080 OnePerLine); 4081 OnePerLine.BinPackParameters = false; 4082 verifyFormat( 4083 "Constructor()\n" 4084 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4085 " aaaaaaaaaaa().aaa(),\n" 4086 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4087 OnePerLine); 4088 OnePerLine.ColumnLimit = 60; 4089 verifyFormat("Constructor()\n" 4090 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 4091 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 4092 OnePerLine); 4093 4094 EXPECT_EQ("Constructor()\n" 4095 " : // Comment forcing unwanted break.\n" 4096 " aaaa(aaaa) {}", 4097 format("Constructor() :\n" 4098 " // Comment forcing unwanted break.\n" 4099 " aaaa(aaaa) {}")); 4100 } 4101 4102 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 4103 FormatStyle Style = getLLVMStyle(); 4104 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 4105 Style.ColumnLimit = 60; 4106 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4107 Style.AllowAllConstructorInitializersOnNextLine = true; 4108 Style.BinPackParameters = false; 4109 4110 for (int i = 0; i < 4; ++i) { 4111 // Test all combinations of parameters that should not have an effect. 4112 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 4113 Style.AllowAllArgumentsOnNextLine = i & 2; 4114 4115 Style.AllowAllConstructorInitializersOnNextLine = true; 4116 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 4117 verifyFormat("Constructor()\n" 4118 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4119 Style); 4120 verifyFormat("Constructor() : a(a), b(b) {}", Style); 4121 4122 Style.AllowAllConstructorInitializersOnNextLine = false; 4123 verifyFormat("Constructor()\n" 4124 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 4125 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 4126 Style); 4127 verifyFormat("Constructor() : a(a), b(b) {}", Style); 4128 4129 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 4130 Style.AllowAllConstructorInitializersOnNextLine = true; 4131 verifyFormat("Constructor()\n" 4132 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4133 Style); 4134 4135 Style.AllowAllConstructorInitializersOnNextLine = false; 4136 verifyFormat("Constructor()\n" 4137 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 4138 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4139 Style); 4140 4141 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 4142 Style.AllowAllConstructorInitializersOnNextLine = true; 4143 verifyFormat("Constructor() :\n" 4144 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4145 Style); 4146 4147 Style.AllowAllConstructorInitializersOnNextLine = false; 4148 verifyFormat("Constructor() :\n" 4149 " aaaaaaaaaaaaaaaaaa(a),\n" 4150 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4151 Style); 4152 } 4153 4154 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 4155 // AllowAllConstructorInitializersOnNextLine in all 4156 // BreakConstructorInitializers modes 4157 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 4158 Style.AllowAllParametersOfDeclarationOnNextLine = true; 4159 Style.AllowAllConstructorInitializersOnNextLine = false; 4160 verifyFormat("SomeClassWithALongName::Constructor(\n" 4161 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 4162 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 4163 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 4164 Style); 4165 4166 Style.AllowAllConstructorInitializersOnNextLine = true; 4167 verifyFormat("SomeClassWithALongName::Constructor(\n" 4168 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4169 " int bbbbbbbbbbbbb,\n" 4170 " int cccccccccccccccc)\n" 4171 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4172 Style); 4173 4174 Style.AllowAllParametersOfDeclarationOnNextLine = false; 4175 Style.AllowAllConstructorInitializersOnNextLine = false; 4176 verifyFormat("SomeClassWithALongName::Constructor(\n" 4177 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4178 " int bbbbbbbbbbbbb)\n" 4179 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 4180 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 4181 Style); 4182 4183 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 4184 4185 Style.AllowAllParametersOfDeclarationOnNextLine = true; 4186 verifyFormat("SomeClassWithALongName::Constructor(\n" 4187 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 4188 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 4189 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4190 Style); 4191 4192 Style.AllowAllConstructorInitializersOnNextLine = true; 4193 verifyFormat("SomeClassWithALongName::Constructor(\n" 4194 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4195 " int bbbbbbbbbbbbb,\n" 4196 " int cccccccccccccccc)\n" 4197 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4198 Style); 4199 4200 Style.AllowAllParametersOfDeclarationOnNextLine = false; 4201 Style.AllowAllConstructorInitializersOnNextLine = false; 4202 verifyFormat("SomeClassWithALongName::Constructor(\n" 4203 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4204 " int bbbbbbbbbbbbb)\n" 4205 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 4206 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4207 Style); 4208 4209 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 4210 Style.AllowAllParametersOfDeclarationOnNextLine = true; 4211 verifyFormat("SomeClassWithALongName::Constructor(\n" 4212 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 4213 " aaaaaaaaaaaaaaaaaaaa(a),\n" 4214 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4215 Style); 4216 4217 Style.AllowAllConstructorInitializersOnNextLine = true; 4218 verifyFormat("SomeClassWithALongName::Constructor(\n" 4219 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4220 " int bbbbbbbbbbbbb,\n" 4221 " int cccccccccccccccc) :\n" 4222 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4223 Style); 4224 4225 Style.AllowAllParametersOfDeclarationOnNextLine = false; 4226 Style.AllowAllConstructorInitializersOnNextLine = false; 4227 verifyFormat("SomeClassWithALongName::Constructor(\n" 4228 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4229 " int bbbbbbbbbbbbb) :\n" 4230 " aaaaaaaaaaaaaaaaaaaa(a),\n" 4231 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4232 Style); 4233 } 4234 4235 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 4236 FormatStyle Style = getLLVMStyle(); 4237 Style.ColumnLimit = 60; 4238 Style.BinPackArguments = false; 4239 for (int i = 0; i < 4; ++i) { 4240 // Test all combinations of parameters that should not have an effect. 4241 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 4242 Style.AllowAllConstructorInitializersOnNextLine = i & 2; 4243 4244 Style.AllowAllArgumentsOnNextLine = true; 4245 verifyFormat("void foo() {\n" 4246 " FunctionCallWithReallyLongName(\n" 4247 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 4248 "}", 4249 Style); 4250 Style.AllowAllArgumentsOnNextLine = false; 4251 verifyFormat("void foo() {\n" 4252 " FunctionCallWithReallyLongName(\n" 4253 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4254 " bbbbbbbbbbbb);\n" 4255 "}", 4256 Style); 4257 4258 Style.AllowAllArgumentsOnNextLine = true; 4259 verifyFormat("void foo() {\n" 4260 " auto VariableWithReallyLongName = {\n" 4261 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 4262 "}", 4263 Style); 4264 Style.AllowAllArgumentsOnNextLine = false; 4265 verifyFormat("void foo() {\n" 4266 " auto VariableWithReallyLongName = {\n" 4267 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4268 " bbbbbbbbbbbb};\n" 4269 "}", 4270 Style); 4271 } 4272 4273 // This parameter should not affect declarations. 4274 Style.BinPackParameters = false; 4275 Style.AllowAllArgumentsOnNextLine = false; 4276 Style.AllowAllParametersOfDeclarationOnNextLine = true; 4277 verifyFormat("void FunctionCallWithReallyLongName(\n" 4278 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 4279 Style); 4280 Style.AllowAllParametersOfDeclarationOnNextLine = false; 4281 verifyFormat("void FunctionCallWithReallyLongName(\n" 4282 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 4283 " int bbbbbbbbbbbb);", 4284 Style); 4285 } 4286 4287 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 4288 FormatStyle Style = getLLVMStyle(); 4289 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 4290 4291 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 4292 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 4293 getStyleWithColumns(Style, 45)); 4294 verifyFormat("Constructor() :\n" 4295 " Initializer(FitsOnTheLine) {}", 4296 getStyleWithColumns(Style, 44)); 4297 verifyFormat("Constructor() :\n" 4298 " Initializer(FitsOnTheLine) {}", 4299 getStyleWithColumns(Style, 43)); 4300 4301 verifyFormat("template <typename T>\n" 4302 "Constructor() : Initializer(FitsOnTheLine) {}", 4303 getStyleWithColumns(Style, 50)); 4304 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4305 verifyFormat( 4306 "SomeClass::Constructor() :\n" 4307 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 4308 Style); 4309 4310 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false; 4311 verifyFormat( 4312 "SomeClass::Constructor() :\n" 4313 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 4314 Style); 4315 4316 verifyFormat( 4317 "SomeClass::Constructor() :\n" 4318 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4319 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4320 Style); 4321 verifyFormat( 4322 "SomeClass::Constructor() :\n" 4323 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4324 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 4325 Style); 4326 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4327 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4328 " aaaaaaaaaa(aaaaaa) {}", 4329 Style); 4330 4331 verifyFormat("Constructor() :\n" 4332 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4333 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4334 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4335 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 4336 Style); 4337 4338 verifyFormat("Constructor() :\n" 4339 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4340 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4341 Style); 4342 4343 verifyFormat("Constructor(int Parameter = 0) :\n" 4344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 4345 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 4346 Style); 4347 verifyFormat("Constructor() :\n" 4348 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 4349 "}", 4350 getStyleWithColumns(Style, 60)); 4351 verifyFormat("Constructor() :\n" 4352 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4353 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 4354 Style); 4355 4356 // Here a line could be saved by splitting the second initializer onto two 4357 // lines, but that is not desirable. 4358 verifyFormat("Constructor() :\n" 4359 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 4360 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 4361 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4362 Style); 4363 4364 FormatStyle OnePerLine = Style; 4365 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4366 OnePerLine.AllowAllConstructorInitializersOnNextLine = false; 4367 verifyFormat("SomeClass::Constructor() :\n" 4368 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4369 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4370 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4371 OnePerLine); 4372 verifyFormat("SomeClass::Constructor() :\n" 4373 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 4374 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4375 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4376 OnePerLine); 4377 verifyFormat("MyClass::MyClass(int var) :\n" 4378 " some_var_(var), // 4 space indent\n" 4379 " some_other_var_(var + 1) { // lined up\n" 4380 "}", 4381 OnePerLine); 4382 verifyFormat("Constructor() :\n" 4383 " aaaaa(aaaaaa),\n" 4384 " aaaaa(aaaaaa),\n" 4385 " aaaaa(aaaaaa),\n" 4386 " aaaaa(aaaaaa),\n" 4387 " aaaaa(aaaaaa) {}", 4388 OnePerLine); 4389 verifyFormat("Constructor() :\n" 4390 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 4391 " aaaaaaaaaaaaaaaaaaaaaa) {}", 4392 OnePerLine); 4393 OnePerLine.BinPackParameters = false; 4394 verifyFormat( 4395 "Constructor() :\n" 4396 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4397 " aaaaaaaaaaa().aaa(),\n" 4398 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4399 OnePerLine); 4400 OnePerLine.ColumnLimit = 60; 4401 verifyFormat("Constructor() :\n" 4402 " aaaaaaaaaaaaaaaaaaaa(a),\n" 4403 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 4404 OnePerLine); 4405 4406 EXPECT_EQ("Constructor() :\n" 4407 " // Comment forcing unwanted break.\n" 4408 " aaaa(aaaa) {}", 4409 format("Constructor() :\n" 4410 " // Comment forcing unwanted break.\n" 4411 " aaaa(aaaa) {}", 4412 Style)); 4413 4414 Style.ColumnLimit = 0; 4415 verifyFormat("SomeClass::Constructor() :\n" 4416 " a(a) {}", 4417 Style); 4418 verifyFormat("SomeClass::Constructor() noexcept :\n" 4419 " a(a) {}", 4420 Style); 4421 verifyFormat("SomeClass::Constructor() :\n" 4422 " a(a), b(b), c(c) {}", 4423 Style); 4424 verifyFormat("SomeClass::Constructor() :\n" 4425 " a(a) {\n" 4426 " foo();\n" 4427 " bar();\n" 4428 "}", 4429 Style); 4430 4431 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 4432 verifyFormat("SomeClass::Constructor() :\n" 4433 " a(a), b(b), c(c) {\n" 4434 "}", 4435 Style); 4436 verifyFormat("SomeClass::Constructor() :\n" 4437 " a(a) {\n" 4438 "}", 4439 Style); 4440 4441 Style.ColumnLimit = 80; 4442 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 4443 Style.ConstructorInitializerIndentWidth = 2; 4444 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 4445 Style); 4446 verifyFormat("SomeClass::Constructor() :\n" 4447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4448 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 4449 Style); 4450 4451 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as well 4452 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 4453 verifyFormat("class SomeClass\n" 4454 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4455 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 4456 Style); 4457 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 4458 verifyFormat("class SomeClass\n" 4459 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4460 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 4461 Style); 4462 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 4463 verifyFormat("class SomeClass :\n" 4464 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4465 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 4466 Style); 4467 } 4468 4469 #ifndef EXPENSIVE_CHECKS 4470 // Expensive checks enables libstdc++ checking which includes validating the 4471 // state of ranges used in std::priority_queue - this blows out the 4472 // runtime/scalability of the function and makes this test unacceptably slow. 4473 TEST_F(FormatTest, MemoizationTests) { 4474 // This breaks if the memoization lookup does not take \c Indent and 4475 // \c LastSpace into account. 4476 verifyFormat( 4477 "extern CFRunLoopTimerRef\n" 4478 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 4479 " CFTimeInterval interval, CFOptionFlags flags,\n" 4480 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 4481 " CFRunLoopTimerContext *context) {}"); 4482 4483 // Deep nesting somewhat works around our memoization. 4484 verifyFormat( 4485 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4486 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4487 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4488 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4489 " aaaaa())))))))))))))))))))))))))))))))))))))));", 4490 getLLVMStyleWithColumns(65)); 4491 verifyFormat( 4492 "aaaaa(\n" 4493 " aaaaa,\n" 4494 " aaaaa(\n" 4495 " aaaaa,\n" 4496 " aaaaa(\n" 4497 " aaaaa,\n" 4498 " aaaaa(\n" 4499 " aaaaa,\n" 4500 " aaaaa(\n" 4501 " aaaaa,\n" 4502 " aaaaa(\n" 4503 " aaaaa,\n" 4504 " aaaaa(\n" 4505 " aaaaa,\n" 4506 " aaaaa(\n" 4507 " aaaaa,\n" 4508 " aaaaa(\n" 4509 " aaaaa,\n" 4510 " aaaaa(\n" 4511 " aaaaa,\n" 4512 " aaaaa(\n" 4513 " aaaaa,\n" 4514 " aaaaa(\n" 4515 " aaaaa,\n" 4516 " aaaaa))))))))))));", 4517 getLLVMStyleWithColumns(65)); 4518 verifyFormat( 4519 "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" 4520 " a),\n" 4521 " a),\n" 4522 " a),\n" 4523 " a),\n" 4524 " a),\n" 4525 " a),\n" 4526 " a),\n" 4527 " a),\n" 4528 " a),\n" 4529 " a),\n" 4530 " a),\n" 4531 " a),\n" 4532 " a),\n" 4533 " a),\n" 4534 " a),\n" 4535 " a),\n" 4536 " a)", 4537 getLLVMStyleWithColumns(65)); 4538 4539 // This test takes VERY long when memoization is broken. 4540 FormatStyle OnePerLine = getLLVMStyle(); 4541 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4542 OnePerLine.BinPackParameters = false; 4543 std::string input = "Constructor()\n" 4544 " : aaaa(a,\n"; 4545 for (unsigned i = 0, e = 80; i != e; ++i) { 4546 input += " a,\n"; 4547 } 4548 input += " a) {}"; 4549 verifyFormat(input, OnePerLine); 4550 } 4551 #endif 4552 4553 TEST_F(FormatTest, BreaksAsHighAsPossible) { 4554 verifyFormat( 4555 "void f() {\n" 4556 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 4557 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 4558 " f();\n" 4559 "}"); 4560 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 4561 " Intervals[i - 1].getRange().getLast()) {\n}"); 4562 } 4563 4564 TEST_F(FormatTest, BreaksFunctionDeclarations) { 4565 // Principially, we break function declarations in a certain order: 4566 // 1) break amongst arguments. 4567 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 4568 " Cccccccccccccc cccccccccccccc);"); 4569 verifyFormat("template <class TemplateIt>\n" 4570 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 4571 " TemplateIt *stop) {}"); 4572 4573 // 2) break after return type. 4574 verifyFormat( 4575 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4576 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 4577 getGoogleStyle()); 4578 4579 // 3) break after (. 4580 verifyFormat( 4581 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 4582 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 4583 getGoogleStyle()); 4584 4585 // 4) break before after nested name specifiers. 4586 verifyFormat( 4587 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4588 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 4589 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 4590 getGoogleStyle()); 4591 4592 // However, there are exceptions, if a sufficient amount of lines can be 4593 // saved. 4594 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 4595 // more adjusting. 4596 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 4597 " Cccccccccccccc cccccccccc,\n" 4598 " Cccccccccccccc cccccccccc,\n" 4599 " Cccccccccccccc cccccccccc,\n" 4600 " Cccccccccccccc cccccccccc);"); 4601 verifyFormat( 4602 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4603 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4604 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4605 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 4606 getGoogleStyle()); 4607 verifyFormat( 4608 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 4609 " Cccccccccccccc cccccccccc,\n" 4610 " Cccccccccccccc cccccccccc,\n" 4611 " Cccccccccccccc cccccccccc,\n" 4612 " Cccccccccccccc cccccccccc,\n" 4613 " Cccccccccccccc cccccccccc,\n" 4614 " Cccccccccccccc cccccccccc);"); 4615 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4616 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4617 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4618 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4619 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 4620 4621 // Break after multi-line parameters. 4622 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4623 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4625 " bbbb bbbb);"); 4626 verifyFormat("void SomeLoooooooooooongFunction(\n" 4627 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 4628 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4629 " int bbbbbbbbbbbbb);"); 4630 4631 // Treat overloaded operators like other functions. 4632 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4633 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 4634 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4635 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 4636 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4637 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 4638 verifyGoogleFormat( 4639 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 4640 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 4641 verifyGoogleFormat( 4642 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 4643 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 4644 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4645 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 4646 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 4647 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 4648 verifyGoogleFormat( 4649 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 4650 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4651 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 4652 verifyGoogleFormat( 4653 "template <typename T>\n" 4654 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4655 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 4656 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 4657 4658 FormatStyle Style = getLLVMStyle(); 4659 Style.PointerAlignment = FormatStyle::PAS_Left; 4660 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4661 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 4662 Style); 4663 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 4664 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4665 Style); 4666 } 4667 4668 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 4669 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 4670 // Prefer keeping `::` followed by `operator` together. 4671 EXPECT_EQ("const aaaa::bbbbbbb &\n" 4672 "ccccccccc::operator++() {\n" 4673 " stuff();\n" 4674 "}", 4675 format("const aaaa::bbbbbbb\n" 4676 "&ccccccccc::operator++() { stuff(); }", 4677 getLLVMStyleWithColumns(40))); 4678 } 4679 4680 TEST_F(FormatTest, TrailingReturnType) { 4681 verifyFormat("auto foo() -> int;\n"); 4682 verifyFormat("struct S {\n" 4683 " auto bar() const -> int;\n" 4684 "};"); 4685 verifyFormat("template <size_t Order, typename T>\n" 4686 "auto load_img(const std::string &filename)\n" 4687 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 4688 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 4689 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 4690 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 4691 verifyFormat("template <typename T>\n" 4692 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 4693 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 4694 4695 // Not trailing return types. 4696 verifyFormat("void f() { auto a = b->c(); }"); 4697 } 4698 4699 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 4700 // Avoid breaking before trailing 'const' or other trailing annotations, if 4701 // they are not function-like. 4702 FormatStyle Style = getGoogleStyle(); 4703 Style.ColumnLimit = 47; 4704 verifyFormat("void someLongFunction(\n" 4705 " int someLoooooooooooooongParameter) const {\n}", 4706 getLLVMStyleWithColumns(47)); 4707 verifyFormat("LoooooongReturnType\n" 4708 "someLoooooooongFunction() const {}", 4709 getLLVMStyleWithColumns(47)); 4710 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 4711 " const {}", 4712 Style); 4713 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4714 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 4715 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4716 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 4717 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4718 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 4719 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 4720 " aaaaaaaaaaa aaaaa) const override;"); 4721 verifyGoogleFormat( 4722 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4723 " const override;"); 4724 4725 // Even if the first parameter has to be wrapped. 4726 verifyFormat("void someLongFunction(\n" 4727 " int someLongParameter) const {}", 4728 getLLVMStyleWithColumns(46)); 4729 verifyFormat("void someLongFunction(\n" 4730 " int someLongParameter) const {}", 4731 Style); 4732 verifyFormat("void someLongFunction(\n" 4733 " int someLongParameter) override {}", 4734 Style); 4735 verifyFormat("void someLongFunction(\n" 4736 " int someLongParameter) OVERRIDE {}", 4737 Style); 4738 verifyFormat("void someLongFunction(\n" 4739 " int someLongParameter) final {}", 4740 Style); 4741 verifyFormat("void someLongFunction(\n" 4742 " int someLongParameter) FINAL {}", 4743 Style); 4744 verifyFormat("void someLongFunction(\n" 4745 " int parameter) const override {}", 4746 Style); 4747 4748 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4749 verifyFormat("void someLongFunction(\n" 4750 " int someLongParameter) const\n" 4751 "{\n" 4752 "}", 4753 Style); 4754 4755 // Unless these are unknown annotations. 4756 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 4757 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4758 " LONG_AND_UGLY_ANNOTATION;"); 4759 4760 // Breaking before function-like trailing annotations is fine to keep them 4761 // close to their arguments. 4762 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4763 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4764 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4765 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4766 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4767 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 4768 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 4769 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 4770 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 4771 4772 verifyFormat( 4773 "void aaaaaaaaaaaaaaaaaa()\n" 4774 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 4775 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 4776 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4777 " __attribute__((unused));"); 4778 verifyGoogleFormat( 4779 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4780 " GUARDED_BY(aaaaaaaaaaaa);"); 4781 verifyGoogleFormat( 4782 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4783 " GUARDED_BY(aaaaaaaaaaaa);"); 4784 verifyGoogleFormat( 4785 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4786 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4787 verifyGoogleFormat( 4788 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4789 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4790 } 4791 4792 TEST_F(FormatTest, FunctionAnnotations) { 4793 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4794 "int OldFunction(const string ¶meter) {}"); 4795 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4796 "string OldFunction(const string ¶meter) {}"); 4797 verifyFormat("template <typename T>\n" 4798 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4799 "string OldFunction(const string ¶meter) {}"); 4800 4801 // Not function annotations. 4802 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4803 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4804 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4805 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4806 verifyFormat("MACRO(abc).function() // wrap\n" 4807 " << abc;"); 4808 verifyFormat("MACRO(abc)->function() // wrap\n" 4809 " << abc;"); 4810 verifyFormat("MACRO(abc)::function() // wrap\n" 4811 " << abc;"); 4812 } 4813 4814 TEST_F(FormatTest, BreaksDesireably) { 4815 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4816 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4817 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4818 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4819 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4820 "}"); 4821 4822 verifyFormat( 4823 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4824 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4825 4826 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4827 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4828 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4829 4830 verifyFormat( 4831 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4832 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4833 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4834 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4835 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4836 4837 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4838 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4839 4840 verifyFormat( 4841 "void f() {\n" 4842 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4843 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4844 "}"); 4845 verifyFormat( 4846 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4847 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4848 verifyFormat( 4849 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4850 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4851 verifyFormat( 4852 "aaaaaa(aaa,\n" 4853 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4855 " aaaa);"); 4856 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4859 4860 // Indent consistently independent of call expression and unary operator. 4861 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4862 " dddddddddddddddddddddddddddddd));"); 4863 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4864 " dddddddddddddddddddddddddddddd));"); 4865 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4866 " dddddddddddddddddddddddddddddd));"); 4867 4868 // This test case breaks on an incorrect memoization, i.e. an optimization not 4869 // taking into account the StopAt value. 4870 verifyFormat( 4871 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4872 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4873 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4874 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4875 4876 verifyFormat("{\n {\n {\n" 4877 " Annotation.SpaceRequiredBefore =\n" 4878 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4879 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4880 " }\n }\n}"); 4881 4882 // Break on an outer level if there was a break on an inner level. 4883 EXPECT_EQ("f(g(h(a, // comment\n" 4884 " b, c),\n" 4885 " d, e),\n" 4886 " x, y);", 4887 format("f(g(h(a, // comment\n" 4888 " b, c), d, e), x, y);")); 4889 4890 // Prefer breaking similar line breaks. 4891 verifyFormat( 4892 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4893 " NSTrackingMouseEnteredAndExited |\n" 4894 " NSTrackingActiveAlways;"); 4895 } 4896 4897 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4898 FormatStyle NoBinPacking = getGoogleStyle(); 4899 NoBinPacking.BinPackParameters = false; 4900 NoBinPacking.BinPackArguments = true; 4901 verifyFormat("void f() {\n" 4902 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4903 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4904 "}", 4905 NoBinPacking); 4906 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4907 " int aaaaaaaaaaaaaaaaaaaa,\n" 4908 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4909 NoBinPacking); 4910 4911 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4912 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4913 " vector<int> bbbbbbbbbbbbbbb);", 4914 NoBinPacking); 4915 // FIXME: This behavior difference is probably not wanted. However, currently 4916 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4917 // template arguments from BreakBeforeParameter being set because of the 4918 // one-per-line formatting. 4919 verifyFormat( 4920 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4921 " aaaaaaaaaa> aaaaaaaaaa);", 4922 NoBinPacking); 4923 verifyFormat( 4924 "void fffffffffff(\n" 4925 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4926 " aaaaaaaaaa);"); 4927 } 4928 4929 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4930 FormatStyle NoBinPacking = getGoogleStyle(); 4931 NoBinPacking.BinPackParameters = false; 4932 NoBinPacking.BinPackArguments = false; 4933 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4934 " aaaaaaaaaaaaaaaaaaaa,\n" 4935 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4936 NoBinPacking); 4937 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4938 " aaaaaaaaaaaaa,\n" 4939 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4940 NoBinPacking); 4941 verifyFormat( 4942 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4943 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4944 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4945 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4947 NoBinPacking); 4948 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4949 " .aaaaaaaaaaaaaaaaaa();", 4950 NoBinPacking); 4951 verifyFormat("void f() {\n" 4952 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4953 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4954 "}", 4955 NoBinPacking); 4956 4957 verifyFormat( 4958 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4959 " aaaaaaaaaaaa,\n" 4960 " aaaaaaaaaaaa);", 4961 NoBinPacking); 4962 verifyFormat( 4963 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4964 " ddddddddddddddddddddddddddddd),\n" 4965 " test);", 4966 NoBinPacking); 4967 4968 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4969 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4970 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4971 " aaaaaaaaaaaaaaaaaa;", 4972 NoBinPacking); 4973 verifyFormat("a(\"a\"\n" 4974 " \"a\",\n" 4975 " a);"); 4976 4977 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4978 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4979 " aaaaaaaaa,\n" 4980 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4981 NoBinPacking); 4982 verifyFormat( 4983 "void f() {\n" 4984 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4985 " .aaaaaaa();\n" 4986 "}", 4987 NoBinPacking); 4988 verifyFormat( 4989 "template <class SomeType, class SomeOtherType>\n" 4990 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4991 NoBinPacking); 4992 } 4993 4994 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4995 FormatStyle Style = getLLVMStyleWithColumns(15); 4996 Style.ExperimentalAutoDetectBinPacking = true; 4997 EXPECT_EQ("aaa(aaaa,\n" 4998 " aaaa,\n" 4999 " aaaa);\n" 5000 "aaa(aaaa,\n" 5001 " aaaa,\n" 5002 " aaaa);", 5003 format("aaa(aaaa,\n" // one-per-line 5004 " aaaa,\n" 5005 " aaaa );\n" 5006 "aaa(aaaa, aaaa, aaaa);", // inconclusive 5007 Style)); 5008 EXPECT_EQ("aaa(aaaa, aaaa,\n" 5009 " aaaa);\n" 5010 "aaa(aaaa, aaaa,\n" 5011 " aaaa);", 5012 format("aaa(aaaa, aaaa,\n" // bin-packed 5013 " aaaa );\n" 5014 "aaa(aaaa, aaaa, aaaa);", // inconclusive 5015 Style)); 5016 } 5017 5018 TEST_F(FormatTest, FormatsBuilderPattern) { 5019 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 5020 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 5021 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 5022 " .StartsWith(\".init\", ORDER_INIT)\n" 5023 " .StartsWith(\".fini\", ORDER_FINI)\n" 5024 " .StartsWith(\".hash\", ORDER_HASH)\n" 5025 " .Default(ORDER_TEXT);\n"); 5026 5027 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 5028 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 5029 verifyFormat( 5030 "aaaaaaa->aaaaaaa\n" 5031 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5032 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5033 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 5034 verifyFormat( 5035 "aaaaaaa->aaaaaaa\n" 5036 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5037 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 5038 verifyFormat( 5039 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 5040 " aaaaaaaaaaaaaa);"); 5041 verifyFormat( 5042 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 5043 " aaaaaa->aaaaaaaaaaaa()\n" 5044 " ->aaaaaaaaaaaaaaaa(\n" 5045 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5046 " ->aaaaaaaaaaaaaaaaa();"); 5047 verifyGoogleFormat( 5048 "void f() {\n" 5049 " someo->Add((new util::filetools::Handler(dir))\n" 5050 " ->OnEvent1(NewPermanentCallback(\n" 5051 " this, &HandlerHolderClass::EventHandlerCBA))\n" 5052 " ->OnEvent2(NewPermanentCallback(\n" 5053 " this, &HandlerHolderClass::EventHandlerCBB))\n" 5054 " ->OnEvent3(NewPermanentCallback(\n" 5055 " this, &HandlerHolderClass::EventHandlerCBC))\n" 5056 " ->OnEvent5(NewPermanentCallback(\n" 5057 " this, &HandlerHolderClass::EventHandlerCBD))\n" 5058 " ->OnEvent6(NewPermanentCallback(\n" 5059 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 5060 "}"); 5061 5062 verifyFormat( 5063 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 5064 verifyFormat("aaaaaaaaaaaaaaa()\n" 5065 " .aaaaaaaaaaaaaaa()\n" 5066 " .aaaaaaaaaaaaaaa()\n" 5067 " .aaaaaaaaaaaaaaa()\n" 5068 " .aaaaaaaaaaaaaaa();"); 5069 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 5070 " .aaaaaaaaaaaaaaa()\n" 5071 " .aaaaaaaaaaaaaaa()\n" 5072 " .aaaaaaaaaaaaaaa();"); 5073 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 5074 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 5075 " .aaaaaaaaaaaaaaa();"); 5076 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 5077 " ->aaaaaaaaaaaaaae(0)\n" 5078 " ->aaaaaaaaaaaaaaa();"); 5079 5080 // Don't linewrap after very short segments. 5081 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5082 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5083 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5084 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5085 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5086 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5087 verifyFormat("aaa()\n" 5088 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5089 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5090 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5091 5092 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 5093 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5094 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 5095 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 5096 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5097 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 5098 5099 // Prefer not to break after empty parentheses. 5100 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 5101 " First->LastNewlineOffset);"); 5102 5103 // Prefer not to create "hanging" indents. 5104 verifyFormat( 5105 "return !soooooooooooooome_map\n" 5106 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5107 " .second;"); 5108 verifyFormat( 5109 "return aaaaaaaaaaaaaaaa\n" 5110 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 5111 " .aaaa(aaaaaaaaaaaaaa);"); 5112 // No hanging indent here. 5113 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 5114 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5115 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 5116 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5117 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 5118 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5119 getLLVMStyleWithColumns(60)); 5120 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 5121 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 5122 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5123 getLLVMStyleWithColumns(59)); 5124 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5125 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5126 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5127 5128 // Dont break if only closing statements before member call 5129 verifyFormat("test() {\n" 5130 " ([]() -> {\n" 5131 " int b = 32;\n" 5132 " return 3;\n" 5133 " }).foo();\n" 5134 "}"); 5135 verifyFormat("test() {\n" 5136 " (\n" 5137 " []() -> {\n" 5138 " int b = 32;\n" 5139 " return 3;\n" 5140 " },\n" 5141 " foo, bar)\n" 5142 " .foo();\n" 5143 "}"); 5144 verifyFormat("test() {\n" 5145 " ([]() -> {\n" 5146 " int b = 32;\n" 5147 " return 3;\n" 5148 " })\n" 5149 " .foo()\n" 5150 " .bar();\n" 5151 "}"); 5152 verifyFormat("test() {\n" 5153 " ([]() -> {\n" 5154 " int b = 32;\n" 5155 " return 3;\n" 5156 " })\n" 5157 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 5158 " \"bbbb\");\n" 5159 "}", 5160 getLLVMStyleWithColumns(30)); 5161 } 5162 5163 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 5164 verifyFormat( 5165 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5166 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 5167 verifyFormat( 5168 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 5169 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 5170 5171 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 5172 " ccccccccccccccccccccccccc) {\n}"); 5173 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 5174 " ccccccccccccccccccccccccc) {\n}"); 5175 5176 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 5177 " ccccccccccccccccccccccccc) {\n}"); 5178 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 5179 " ccccccccccccccccccccccccc) {\n}"); 5180 5181 verifyFormat( 5182 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 5183 " ccccccccccccccccccccccccc) {\n}"); 5184 verifyFormat( 5185 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 5186 " ccccccccccccccccccccccccc) {\n}"); 5187 5188 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 5189 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 5190 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 5191 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 5192 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 5193 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 5194 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 5195 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 5196 5197 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 5198 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 5199 " aaaaaaaaaaaaaaa != aa) {\n}"); 5200 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 5201 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 5202 " aaaaaaaaaaaaaaa != aa) {\n}"); 5203 } 5204 5205 TEST_F(FormatTest, BreaksAfterAssignments) { 5206 verifyFormat( 5207 "unsigned Cost =\n" 5208 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 5209 " SI->getPointerAddressSpaceee());\n"); 5210 verifyFormat( 5211 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 5212 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 5213 5214 verifyFormat( 5215 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 5216 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 5217 verifyFormat("unsigned OriginalStartColumn =\n" 5218 " SourceMgr.getSpellingColumnNumber(\n" 5219 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 5220 " 1;"); 5221 } 5222 5223 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 5224 FormatStyle Style = getLLVMStyle(); 5225 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5226 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 5227 Style); 5228 5229 Style.PenaltyBreakAssignment = 20; 5230 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 5231 " cccccccccccccccccccccccccc;", 5232 Style); 5233 } 5234 5235 TEST_F(FormatTest, AlignsAfterAssignments) { 5236 verifyFormat( 5237 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5238 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5239 verifyFormat( 5240 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5241 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5242 verifyFormat( 5243 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5244 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5245 verifyFormat( 5246 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5247 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 5248 verifyFormat( 5249 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 5250 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 5251 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 5252 } 5253 5254 TEST_F(FormatTest, AlignsAfterReturn) { 5255 verifyFormat( 5256 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5257 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5258 verifyFormat( 5259 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5260 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 5261 verifyFormat( 5262 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 5263 " aaaaaaaaaaaaaaaaaaaaaa();"); 5264 verifyFormat( 5265 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 5266 " aaaaaaaaaaaaaaaaaaaaaa());"); 5267 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5268 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5269 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5270 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 5271 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5272 verifyFormat("return\n" 5273 " // true if code is one of a or b.\n" 5274 " code == a || code == b;"); 5275 } 5276 5277 TEST_F(FormatTest, AlignsAfterOpenBracket) { 5278 verifyFormat( 5279 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 5280 " aaaaaaaaa aaaaaaa) {}"); 5281 verifyFormat( 5282 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 5283 " aaaaaaaaaaa aaaaaaaaa);"); 5284 verifyFormat( 5285 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 5286 " aaaaaaaaaaaaaaaaaaaaa));"); 5287 FormatStyle Style = getLLVMStyle(); 5288 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5289 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5290 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 5291 Style); 5292 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 5293 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 5294 Style); 5295 verifyFormat("SomeLongVariableName->someFunction(\n" 5296 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 5297 Style); 5298 verifyFormat( 5299 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 5300 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5301 Style); 5302 verifyFormat( 5303 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 5304 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5305 Style); 5306 verifyFormat( 5307 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 5308 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 5309 Style); 5310 5311 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 5312 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 5313 " b));", 5314 Style); 5315 5316 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5317 Style.BinPackArguments = false; 5318 Style.BinPackParameters = false; 5319 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5320 " aaaaaaaaaaa aaaaaaaa,\n" 5321 " aaaaaaaaa aaaaaaa,\n" 5322 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5323 Style); 5324 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 5325 " aaaaaaaaaaa aaaaaaaaa,\n" 5326 " aaaaaaaaaaa aaaaaaaaa,\n" 5327 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5328 Style); 5329 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 5330 " aaaaaaaaaaaaaaa,\n" 5331 " aaaaaaaaaaaaaaaaaaaaa,\n" 5332 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 5333 Style); 5334 verifyFormat( 5335 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 5336 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 5337 Style); 5338 verifyFormat( 5339 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 5340 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 5341 Style); 5342 verifyFormat( 5343 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5344 " aaaaaaaaaaaaaaaaaaaaa(\n" 5345 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 5346 " aaaaaaaaaaaaaaaa);", 5347 Style); 5348 verifyFormat( 5349 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5350 " aaaaaaaaaaaaaaaaaaaaa(\n" 5351 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 5352 " aaaaaaaaaaaaaaaa);", 5353 Style); 5354 } 5355 5356 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 5357 FormatStyle Style = getLLVMStyleWithColumns(40); 5358 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5359 " bbbbbbbbbbbbbbbbbbbbbb);", 5360 Style); 5361 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 5362 Style.AlignOperands = false; 5363 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5364 " bbbbbbbbbbbbbbbbbbbbbb);", 5365 Style); 5366 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5367 Style.AlignOperands = true; 5368 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5369 " bbbbbbbbbbbbbbbbbbbbbb);", 5370 Style); 5371 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5372 Style.AlignOperands = false; 5373 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5374 " bbbbbbbbbbbbbbbbbbbbbb);", 5375 Style); 5376 } 5377 5378 TEST_F(FormatTest, BreaksConditionalExpressions) { 5379 verifyFormat( 5380 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5381 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5382 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5383 verifyFormat( 5384 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 5385 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5386 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5387 verifyFormat( 5388 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5389 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5390 verifyFormat( 5391 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 5392 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5393 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5394 verifyFormat( 5395 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 5396 " : aaaaaaaaaaaaa);"); 5397 verifyFormat( 5398 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5399 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5400 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5401 " aaaaaaaaaaaaa);"); 5402 verifyFormat( 5403 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5404 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5405 " aaaaaaaaaaaaa);"); 5406 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5407 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5408 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5409 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5410 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5411 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5412 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5413 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5414 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5415 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5417 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5418 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5420 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5422 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5423 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5424 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5425 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5426 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 5427 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5428 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5429 " : aaaaaaaaaaaaaaaa;"); 5430 verifyFormat( 5431 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5432 " ? aaaaaaaaaaaaaaa\n" 5433 " : aaaaaaaaaaaaaaa;"); 5434 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 5435 " aaaaaaaaa\n" 5436 " ? b\n" 5437 " : c);"); 5438 verifyFormat("return aaaa == bbbb\n" 5439 " // comment\n" 5440 " ? aaaa\n" 5441 " : bbbb;"); 5442 verifyFormat("unsigned Indent =\n" 5443 " format(TheLine.First,\n" 5444 " IndentForLevel[TheLine.Level] >= 0\n" 5445 " ? IndentForLevel[TheLine.Level]\n" 5446 " : TheLine * 2,\n" 5447 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 5448 getLLVMStyleWithColumns(60)); 5449 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 5450 " ? aaaaaaaaaaaaaaa\n" 5451 " : bbbbbbbbbbbbbbb //\n" 5452 " ? ccccccccccccccc\n" 5453 " : ddddddddddddddd;"); 5454 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 5455 " ? aaaaaaaaaaaaaaa\n" 5456 " : (bbbbbbbbbbbbbbb //\n" 5457 " ? ccccccccccccccc\n" 5458 " : ddddddddddddddd);"); 5459 verifyFormat( 5460 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5461 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5462 " aaaaaaaaaaaaaaaaaaaaa +\n" 5463 " aaaaaaaaaaaaaaaaaaaaa\n" 5464 " : aaaaaaaaaa;"); 5465 verifyFormat( 5466 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5467 " : aaaaaaaaaaaaaaaaaaaaaa\n" 5468 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5469 5470 FormatStyle NoBinPacking = getLLVMStyle(); 5471 NoBinPacking.BinPackArguments = false; 5472 verifyFormat( 5473 "void f() {\n" 5474 " g(aaa,\n" 5475 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 5476 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5477 " ? aaaaaaaaaaaaaaa\n" 5478 " : aaaaaaaaaaaaaaa);\n" 5479 "}", 5480 NoBinPacking); 5481 verifyFormat( 5482 "void f() {\n" 5483 " g(aaa,\n" 5484 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 5485 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5486 " ?: aaaaaaaaaaaaaaa);\n" 5487 "}", 5488 NoBinPacking); 5489 5490 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 5491 " // comment.\n" 5492 " ccccccccccccccccccccccccccccccccccccccc\n" 5493 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5494 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 5495 5496 // Assignments in conditional expressions. Apparently not uncommon :-(. 5497 verifyFormat("return a != b\n" 5498 " // comment\n" 5499 " ? a = b\n" 5500 " : a = b;"); 5501 verifyFormat("return a != b\n" 5502 " // comment\n" 5503 " ? a = a != b\n" 5504 " // comment\n" 5505 " ? a = b\n" 5506 " : a\n" 5507 " : a;\n"); 5508 verifyFormat("return a != b\n" 5509 " // comment\n" 5510 " ? a\n" 5511 " : a = a != b\n" 5512 " // comment\n" 5513 " ? a = b\n" 5514 " : a;"); 5515 } 5516 5517 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 5518 FormatStyle Style = getLLVMStyle(); 5519 Style.BreakBeforeTernaryOperators = false; 5520 Style.ColumnLimit = 70; 5521 verifyFormat( 5522 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5523 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5524 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5525 Style); 5526 verifyFormat( 5527 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 5528 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5529 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5530 Style); 5531 verifyFormat( 5532 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5533 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5534 Style); 5535 verifyFormat( 5536 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 5537 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5538 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5539 Style); 5540 verifyFormat( 5541 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 5542 " aaaaaaaaaaaaa);", 5543 Style); 5544 verifyFormat( 5545 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5546 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5547 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5548 " aaaaaaaaaaaaa);", 5549 Style); 5550 verifyFormat( 5551 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5552 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5553 " aaaaaaaaaaaaa);", 5554 Style); 5555 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5557 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5559 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5560 Style); 5561 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5562 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5563 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5564 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5566 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5567 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5568 Style); 5569 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5570 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 5571 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5572 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5573 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5574 Style); 5575 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5576 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5577 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5578 Style); 5579 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 5580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5581 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5583 Style); 5584 verifyFormat( 5585 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5586 " aaaaaaaaaaaaaaa :\n" 5587 " aaaaaaaaaaaaaaa;", 5588 Style); 5589 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 5590 " aaaaaaaaa ?\n" 5591 " b :\n" 5592 " c);", 5593 Style); 5594 verifyFormat("unsigned Indent =\n" 5595 " format(TheLine.First,\n" 5596 " IndentForLevel[TheLine.Level] >= 0 ?\n" 5597 " IndentForLevel[TheLine.Level] :\n" 5598 " TheLine * 2,\n" 5599 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 5600 Style); 5601 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 5602 " aaaaaaaaaaaaaaa :\n" 5603 " bbbbbbbbbbbbbbb ? //\n" 5604 " ccccccccccccccc :\n" 5605 " ddddddddddddddd;", 5606 Style); 5607 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 5608 " aaaaaaaaaaaaaaa :\n" 5609 " (bbbbbbbbbbbbbbb ? //\n" 5610 " ccccccccccccccc :\n" 5611 " ddddddddddddddd);", 5612 Style); 5613 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5614 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 5615 " ccccccccccccccccccccccccccc;", 5616 Style); 5617 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5618 " aaaaa :\n" 5619 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 5620 Style); 5621 } 5622 5623 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 5624 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 5625 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 5626 verifyFormat("bool a = true, b = false;"); 5627 5628 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5629 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 5630 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 5631 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 5632 verifyFormat( 5633 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5634 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 5635 " d = e && f;"); 5636 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 5637 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 5638 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 5639 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 5640 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 5641 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 5642 5643 FormatStyle Style = getGoogleStyle(); 5644 Style.PointerAlignment = FormatStyle::PAS_Left; 5645 Style.DerivePointerAlignment = false; 5646 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5647 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 5648 " *b = bbbbbbbbbbbbbbbbbbb;", 5649 Style); 5650 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 5651 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 5652 Style); 5653 verifyFormat("vector<int*> a, b;", Style); 5654 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 5655 } 5656 5657 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 5658 verifyFormat("arr[foo ? bar : baz];"); 5659 verifyFormat("f()[foo ? bar : baz];"); 5660 verifyFormat("(a + b)[foo ? bar : baz];"); 5661 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 5662 } 5663 5664 TEST_F(FormatTest, AlignsStringLiterals) { 5665 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 5666 " \"short literal\");"); 5667 verifyFormat( 5668 "looooooooooooooooooooooooongFunction(\n" 5669 " \"short literal\"\n" 5670 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 5671 verifyFormat("someFunction(\"Always break between multi-line\"\n" 5672 " \" string literals\",\n" 5673 " and, other, parameters);"); 5674 EXPECT_EQ("fun + \"1243\" /* comment */\n" 5675 " \"5678\";", 5676 format("fun + \"1243\" /* comment */\n" 5677 " \"5678\";", 5678 getLLVMStyleWithColumns(28))); 5679 EXPECT_EQ( 5680 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 5681 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 5682 " \"aaaaaaaaaaaaaaaa\";", 5683 format("aaaaaa =" 5684 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 5685 "aaaaaaaaaaaaaaaaaaaaa\" " 5686 "\"aaaaaaaaaaaaaaaa\";")); 5687 verifyFormat("a = a + \"a\"\n" 5688 " \"a\"\n" 5689 " \"a\";"); 5690 verifyFormat("f(\"a\", \"b\"\n" 5691 " \"c\");"); 5692 5693 verifyFormat( 5694 "#define LL_FORMAT \"ll\"\n" 5695 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 5696 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 5697 5698 verifyFormat("#define A(X) \\\n" 5699 " \"aaaaa\" #X \"bbbbbb\" \\\n" 5700 " \"ccccc\"", 5701 getLLVMStyleWithColumns(23)); 5702 verifyFormat("#define A \"def\"\n" 5703 "f(\"abc\" A \"ghi\"\n" 5704 " \"jkl\");"); 5705 5706 verifyFormat("f(L\"a\"\n" 5707 " L\"b\");"); 5708 verifyFormat("#define A(X) \\\n" 5709 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 5710 " L\"ccccc\"", 5711 getLLVMStyleWithColumns(25)); 5712 5713 verifyFormat("f(@\"a\"\n" 5714 " @\"b\");"); 5715 verifyFormat("NSString s = @\"a\"\n" 5716 " @\"b\"\n" 5717 " @\"c\";"); 5718 verifyFormat("NSString s = @\"a\"\n" 5719 " \"b\"\n" 5720 " \"c\";"); 5721 } 5722 5723 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 5724 FormatStyle Style = getLLVMStyle(); 5725 // No declarations or definitions should be moved to own line. 5726 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 5727 verifyFormat("class A {\n" 5728 " int f() { return 1; }\n" 5729 " int g();\n" 5730 "};\n" 5731 "int f() { return 1; }\n" 5732 "int g();\n", 5733 Style); 5734 5735 // All declarations and definitions should have the return type moved to its 5736 // own 5737 // line. 5738 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 5739 verifyFormat("class E {\n" 5740 " int\n" 5741 " f() {\n" 5742 " return 1;\n" 5743 " }\n" 5744 " int\n" 5745 " g();\n" 5746 "};\n" 5747 "int\n" 5748 "f() {\n" 5749 " return 1;\n" 5750 "}\n" 5751 "int\n" 5752 "g();\n", 5753 Style); 5754 5755 // Top-level definitions, and no kinds of declarations should have the 5756 // return type moved to its own line. 5757 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 5758 verifyFormat("class B {\n" 5759 " int f() { return 1; }\n" 5760 " int g();\n" 5761 "};\n" 5762 "int\n" 5763 "f() {\n" 5764 " return 1;\n" 5765 "}\n" 5766 "int g();\n", 5767 Style); 5768 5769 // Top-level definitions and declarations should have the return type moved 5770 // to its own line. 5771 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 5772 verifyFormat("class C {\n" 5773 " int f() { return 1; }\n" 5774 " int g();\n" 5775 "};\n" 5776 "int\n" 5777 "f() {\n" 5778 " return 1;\n" 5779 "}\n" 5780 "int\n" 5781 "g();\n", 5782 Style); 5783 5784 // All definitions should have the return type moved to its own line, but no 5785 // kinds of declarations. 5786 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5787 verifyFormat("class D {\n" 5788 " int\n" 5789 " f() {\n" 5790 " return 1;\n" 5791 " }\n" 5792 " int g();\n" 5793 "};\n" 5794 "int\n" 5795 "f() {\n" 5796 " return 1;\n" 5797 "}\n" 5798 "int g();\n", 5799 Style); 5800 verifyFormat("const char *\n" 5801 "f(void) {\n" // Break here. 5802 " return \"\";\n" 5803 "}\n" 5804 "const char *bar(void);\n", // No break here. 5805 Style); 5806 verifyFormat("template <class T>\n" 5807 "T *\n" 5808 "f(T &c) {\n" // Break here. 5809 " return NULL;\n" 5810 "}\n" 5811 "template <class T> T *f(T &c);\n", // No break here. 5812 Style); 5813 verifyFormat("class C {\n" 5814 " int\n" 5815 " operator+() {\n" 5816 " return 1;\n" 5817 " }\n" 5818 " int\n" 5819 " operator()() {\n" 5820 " return 1;\n" 5821 " }\n" 5822 "};\n", 5823 Style); 5824 verifyFormat("void\n" 5825 "A::operator()() {}\n" 5826 "void\n" 5827 "A::operator>>() {}\n" 5828 "void\n" 5829 "A::operator+() {}\n", 5830 Style); 5831 verifyFormat("void *operator new(std::size_t s);", // No break here. 5832 Style); 5833 verifyFormat("void *\n" 5834 "operator new(std::size_t s) {}", 5835 Style); 5836 verifyFormat("void *\n" 5837 "operator delete[](void *ptr) {}", 5838 Style); 5839 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 5840 verifyFormat("const char *\n" 5841 "f(void)\n" // Break here. 5842 "{\n" 5843 " return \"\";\n" 5844 "}\n" 5845 "const char *bar(void);\n", // No break here. 5846 Style); 5847 verifyFormat("template <class T>\n" 5848 "T *\n" // Problem here: no line break 5849 "f(T &c)\n" // Break here. 5850 "{\n" 5851 " return NULL;\n" 5852 "}\n" 5853 "template <class T> T *f(T &c);\n", // No break here. 5854 Style); 5855 verifyFormat("int\n" 5856 "foo(A<bool> a)\n" 5857 "{\n" 5858 " return a;\n" 5859 "}\n", 5860 Style); 5861 verifyFormat("int\n" 5862 "foo(A<8> a)\n" 5863 "{\n" 5864 " return a;\n" 5865 "}\n", 5866 Style); 5867 verifyFormat("int\n" 5868 "foo(A<B<bool>, 8> a)\n" 5869 "{\n" 5870 " return a;\n" 5871 "}\n", 5872 Style); 5873 verifyFormat("int\n" 5874 "foo(A<B<8>, bool> a)\n" 5875 "{\n" 5876 " return a;\n" 5877 "}\n", 5878 Style); 5879 verifyFormat("int\n" 5880 "foo(A<B<bool>, bool> a)\n" 5881 "{\n" 5882 " return a;\n" 5883 "}\n", 5884 Style); 5885 verifyFormat("int\n" 5886 "foo(A<B<8>, 8> a)\n" 5887 "{\n" 5888 " return a;\n" 5889 "}\n", 5890 Style); 5891 5892 Style = getGNUStyle(); 5893 5894 // Test for comments at the end of function declarations. 5895 verifyFormat("void\n" 5896 "foo (int a, /*abc*/ int b) // def\n" 5897 "{\n" 5898 "}\n", 5899 Style); 5900 5901 verifyFormat("void\n" 5902 "foo (int a, /* abc */ int b) /* def */\n" 5903 "{\n" 5904 "}\n", 5905 Style); 5906 5907 // Definitions that should not break after return type 5908 verifyFormat("void foo (int a, int b); // def\n", Style); 5909 verifyFormat("void foo (int a, int b); /* def */\n", Style); 5910 verifyFormat("void foo (int a, int b);\n", Style); 5911 } 5912 5913 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 5914 FormatStyle NoBreak = getLLVMStyle(); 5915 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 5916 FormatStyle Break = getLLVMStyle(); 5917 Break.AlwaysBreakBeforeMultilineStrings = true; 5918 verifyFormat("aaaa = \"bbbb\"\n" 5919 " \"cccc\";", 5920 NoBreak); 5921 verifyFormat("aaaa =\n" 5922 " \"bbbb\"\n" 5923 " \"cccc\";", 5924 Break); 5925 verifyFormat("aaaa(\"bbbb\"\n" 5926 " \"cccc\");", 5927 NoBreak); 5928 verifyFormat("aaaa(\n" 5929 " \"bbbb\"\n" 5930 " \"cccc\");", 5931 Break); 5932 verifyFormat("aaaa(qqq, \"bbbb\"\n" 5933 " \"cccc\");", 5934 NoBreak); 5935 verifyFormat("aaaa(qqq,\n" 5936 " \"bbbb\"\n" 5937 " \"cccc\");", 5938 Break); 5939 verifyFormat("aaaa(qqq,\n" 5940 " L\"bbbb\"\n" 5941 " L\"cccc\");", 5942 Break); 5943 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 5944 " \"bbbb\"));", 5945 Break); 5946 verifyFormat("string s = someFunction(\n" 5947 " \"abc\"\n" 5948 " \"abc\");", 5949 Break); 5950 5951 // As we break before unary operators, breaking right after them is bad. 5952 verifyFormat("string foo = abc ? \"x\"\n" 5953 " \"blah blah blah blah blah blah\"\n" 5954 " : \"y\";", 5955 Break); 5956 5957 // Don't break if there is no column gain. 5958 verifyFormat("f(\"aaaa\"\n" 5959 " \"bbbb\");", 5960 Break); 5961 5962 // Treat literals with escaped newlines like multi-line string literals. 5963 EXPECT_EQ("x = \"a\\\n" 5964 "b\\\n" 5965 "c\";", 5966 format("x = \"a\\\n" 5967 "b\\\n" 5968 "c\";", 5969 NoBreak)); 5970 EXPECT_EQ("xxxx =\n" 5971 " \"a\\\n" 5972 "b\\\n" 5973 "c\";", 5974 format("xxxx = \"a\\\n" 5975 "b\\\n" 5976 "c\";", 5977 Break)); 5978 5979 EXPECT_EQ("NSString *const kString =\n" 5980 " @\"aaaa\"\n" 5981 " @\"bbbb\";", 5982 format("NSString *const kString = @\"aaaa\"\n" 5983 "@\"bbbb\";", 5984 Break)); 5985 5986 Break.ColumnLimit = 0; 5987 verifyFormat("const char *hello = \"hello llvm\";", Break); 5988 } 5989 5990 TEST_F(FormatTest, AlignsPipes) { 5991 verifyFormat( 5992 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5993 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5994 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5995 verifyFormat( 5996 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5997 " << aaaaaaaaaaaaaaaaaaaa;"); 5998 verifyFormat( 5999 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6000 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6001 verifyFormat( 6002 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6003 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6004 verifyFormat( 6005 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 6006 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 6007 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 6008 verifyFormat( 6009 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6010 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6011 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6012 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6013 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6015 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6016 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 6017 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 6018 verifyFormat( 6019 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6020 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6021 verifyFormat( 6022 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 6023 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6024 6025 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 6026 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 6027 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6028 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6029 " aaaaaaaaaaaaaaaaaaaaa)\n" 6030 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6031 verifyFormat("LOG_IF(aaa == //\n" 6032 " bbb)\n" 6033 " << a << b;"); 6034 6035 // But sometimes, breaking before the first "<<" is desirable. 6036 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 6037 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 6038 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 6039 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6040 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6041 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 6042 " << BEF << IsTemplate << Description << E->getType();"); 6043 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 6044 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6045 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6046 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 6047 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6048 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6049 " << aaa;"); 6050 6051 verifyFormat( 6052 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6053 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6054 6055 // Incomplete string literal. 6056 EXPECT_EQ("llvm::errs() << \"\n" 6057 " << a;", 6058 format("llvm::errs() << \"\n<<a;")); 6059 6060 verifyFormat("void f() {\n" 6061 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 6062 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 6063 "}"); 6064 6065 // Handle 'endl'. 6066 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 6067 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 6068 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 6069 6070 // Handle '\n'. 6071 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 6072 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 6073 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 6074 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 6075 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 6076 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 6077 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 6078 } 6079 6080 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 6081 verifyFormat("return out << \"somepacket = {\\n\"\n" 6082 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 6083 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 6084 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 6085 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 6086 " << \"}\";"); 6087 6088 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 6089 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 6090 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 6091 verifyFormat( 6092 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 6093 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 6094 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 6095 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 6096 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 6097 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 6098 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6099 verifyFormat( 6100 "void f() {\n" 6101 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 6102 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6103 "}"); 6104 6105 // Breaking before the first "<<" is generally not desirable. 6106 verifyFormat( 6107 "llvm::errs()\n" 6108 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6109 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6110 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6111 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6112 getLLVMStyleWithColumns(70)); 6113 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 6114 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6115 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 6116 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6117 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 6118 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6119 getLLVMStyleWithColumns(70)); 6120 6121 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 6122 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 6123 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 6124 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 6125 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 6126 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 6127 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 6128 " (aaaa + aaaa);", 6129 getLLVMStyleWithColumns(40)); 6130 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 6131 " (aaaaaaa + aaaaa));", 6132 getLLVMStyleWithColumns(40)); 6133 verifyFormat( 6134 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 6135 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 6136 " bbbbbbbbbbbbbbbbbbbbbbb);"); 6137 } 6138 6139 TEST_F(FormatTest, UnderstandsEquals) { 6140 verifyFormat( 6141 "aaaaaaaaaaaaaaaaa =\n" 6142 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6143 verifyFormat( 6144 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6145 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 6146 verifyFormat( 6147 "if (a) {\n" 6148 " f();\n" 6149 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 6151 "}"); 6152 6153 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6154 " 100000000 + 10000000) {\n}"); 6155 } 6156 6157 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 6158 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 6159 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 6160 6161 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 6162 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 6163 6164 verifyFormat( 6165 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 6166 " Parameter2);"); 6167 6168 verifyFormat( 6169 "ShortObject->shortFunction(\n" 6170 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 6171 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 6172 6173 verifyFormat("loooooooooooooongFunction(\n" 6174 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 6175 6176 verifyFormat( 6177 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 6178 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 6179 6180 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 6181 " .WillRepeatedly(Return(SomeValue));"); 6182 verifyFormat("void f() {\n" 6183 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 6184 " .Times(2)\n" 6185 " .WillRepeatedly(Return(SomeValue));\n" 6186 "}"); 6187 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 6188 " ccccccccccccccccccccccc);"); 6189 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6190 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6191 " .aaaaa(aaaaa),\n" 6192 " aaaaaaaaaaaaaaaaaaaaa);"); 6193 verifyFormat("void f() {\n" 6194 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6195 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 6196 "}"); 6197 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6198 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6199 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6200 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6201 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6202 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6203 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6204 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6205 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 6206 "}"); 6207 6208 // Here, it is not necessary to wrap at "." or "->". 6209 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 6210 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 6211 verifyFormat( 6212 "aaaaaaaaaaa->aaaaaaaaa(\n" 6213 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6214 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 6215 6216 verifyFormat( 6217 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6218 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 6219 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 6220 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 6221 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 6222 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 6223 6224 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6225 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6226 " .a();"); 6227 6228 FormatStyle NoBinPacking = getLLVMStyle(); 6229 NoBinPacking.BinPackParameters = false; 6230 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 6231 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 6232 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 6233 " aaaaaaaaaaaaaaaaaaa,\n" 6234 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6235 NoBinPacking); 6236 6237 // If there is a subsequent call, change to hanging indentation. 6238 verifyFormat( 6239 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6240 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 6241 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6242 verifyFormat( 6243 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6244 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 6245 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6246 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6247 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6248 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6249 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6250 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 6251 } 6252 6253 TEST_F(FormatTest, WrapsTemplateDeclarations) { 6254 verifyFormat("template <typename T>\n" 6255 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 6256 verifyFormat("template <typename T>\n" 6257 "// T should be one of {A, B}.\n" 6258 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 6259 verifyFormat( 6260 "template <typename T>\n" 6261 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 6262 verifyFormat("template <typename T>\n" 6263 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 6264 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 6265 verifyFormat( 6266 "template <typename T>\n" 6267 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 6268 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 6269 verifyFormat( 6270 "template <typename T>\n" 6271 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 6272 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 6273 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6274 verifyFormat("template <typename T>\n" 6275 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6276 " int aaaaaaaaaaaaaaaaaaaaaa);"); 6277 verifyFormat( 6278 "template <typename T1, typename T2 = char, typename T3 = char,\n" 6279 " typename T4 = char>\n" 6280 "void f();"); 6281 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 6282 " template <typename> class cccccccccccccccccccccc,\n" 6283 " typename ddddddddddddd>\n" 6284 "class C {};"); 6285 verifyFormat( 6286 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 6287 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6288 6289 verifyFormat("void f() {\n" 6290 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 6291 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 6292 "}"); 6293 6294 verifyFormat("template <typename T> class C {};"); 6295 verifyFormat("template <typename T> void f();"); 6296 verifyFormat("template <typename T> void f() {}"); 6297 verifyFormat( 6298 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 6299 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6300 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 6301 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 6302 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6303 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 6304 " bbbbbbbbbbbbbbbbbbbbbbbb);", 6305 getLLVMStyleWithColumns(72)); 6306 EXPECT_EQ("static_cast<A< //\n" 6307 " B> *>(\n" 6308 "\n" 6309 ");", 6310 format("static_cast<A<//\n" 6311 " B>*>(\n" 6312 "\n" 6313 " );")); 6314 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6315 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 6316 6317 FormatStyle AlwaysBreak = getLLVMStyle(); 6318 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 6319 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 6320 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 6321 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 6322 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6323 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 6324 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 6325 verifyFormat("template <template <typename> class Fooooooo,\n" 6326 " template <typename> class Baaaaaaar>\n" 6327 "struct C {};", 6328 AlwaysBreak); 6329 verifyFormat("template <typename T> // T can be A, B or C.\n" 6330 "struct C {};", 6331 AlwaysBreak); 6332 verifyFormat("template <enum E> class A {\n" 6333 "public:\n" 6334 " E *f();\n" 6335 "};"); 6336 6337 FormatStyle NeverBreak = getLLVMStyle(); 6338 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 6339 verifyFormat("template <typename T> class C {};", NeverBreak); 6340 verifyFormat("template <typename T> void f();", NeverBreak); 6341 verifyFormat("template <typename T> void f() {}", NeverBreak); 6342 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}", 6343 NeverBreak); 6344 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6345 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 6346 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 6347 NeverBreak); 6348 verifyFormat("template <template <typename> class Fooooooo,\n" 6349 " template <typename> class Baaaaaaar>\n" 6350 "struct C {};", 6351 NeverBreak); 6352 verifyFormat("template <typename T> // T can be A, B or C.\n" 6353 "struct C {};", 6354 NeverBreak); 6355 verifyFormat("template <enum E> class A {\n" 6356 "public:\n" 6357 " E *f();\n" 6358 "};", NeverBreak); 6359 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 6360 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}", 6361 NeverBreak); 6362 } 6363 6364 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 6365 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 6366 Style.ColumnLimit = 60; 6367 EXPECT_EQ("// Baseline - no comments.\n" 6368 "template <\n" 6369 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 6370 "void f() {}", 6371 format("// Baseline - no comments.\n" 6372 "template <\n" 6373 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 6374 "void f() {}", 6375 Style)); 6376 6377 EXPECT_EQ("template <\n" 6378 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 6379 "void f() {}", 6380 format("template <\n" 6381 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 6382 "void f() {}", 6383 Style)); 6384 6385 EXPECT_EQ( 6386 "template <\n" 6387 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 6388 "void f() {}", 6389 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 6390 "void f() {}", 6391 Style)); 6392 6393 EXPECT_EQ( 6394 "template <\n" 6395 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 6396 " // multiline\n" 6397 "void f() {}", 6398 format("template <\n" 6399 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 6400 " // multiline\n" 6401 "void f() {}", 6402 Style)); 6403 6404 EXPECT_EQ( 6405 "template <typename aaaaaaaaaa<\n" 6406 " bbbbbbbbbbbb>::value> // trailing loooong\n" 6407 "void f() {}", 6408 format( 6409 "template <\n" 6410 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 6411 "void f() {}", 6412 Style)); 6413 } 6414 6415 TEST_F(FormatTest, WrapsTemplateParameters) { 6416 FormatStyle Style = getLLVMStyle(); 6417 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6418 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6419 verifyFormat( 6420 "template <typename... a> struct q {};\n" 6421 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 6422 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 6423 " y;", 6424 Style); 6425 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6426 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6427 verifyFormat( 6428 "template <typename... a> struct r {};\n" 6429 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 6430 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 6431 " y;", 6432 Style); 6433 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6434 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6435 verifyFormat( 6436 "template <typename... a> struct s {};\n" 6437 "extern s<\n" 6438 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6439 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 6440 " y;", 6441 Style); 6442 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6443 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6444 verifyFormat( 6445 "template <typename... a> struct t {};\n" 6446 "extern t<\n" 6447 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6448 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 6449 " y;", 6450 Style); 6451 } 6452 6453 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 6454 verifyFormat( 6455 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6456 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6457 verifyFormat( 6458 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6460 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 6461 6462 // FIXME: Should we have the extra indent after the second break? 6463 verifyFormat( 6464 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6465 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6466 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6467 6468 verifyFormat( 6469 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 6470 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 6471 6472 // Breaking at nested name specifiers is generally not desirable. 6473 verifyFormat( 6474 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6475 " aaaaaaaaaaaaaaaaaaaaaaa);"); 6476 6477 verifyFormat( 6478 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 6479 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6481 " aaaaaaaaaaaaaaaaaaaaa);", 6482 getLLVMStyleWithColumns(74)); 6483 6484 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6485 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6486 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6487 } 6488 6489 TEST_F(FormatTest, UnderstandsTemplateParameters) { 6490 verifyFormat("A<int> a;"); 6491 verifyFormat("A<A<A<int>>> a;"); 6492 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 6493 verifyFormat("bool x = a < 1 || 2 > a;"); 6494 verifyFormat("bool x = 5 < f<int>();"); 6495 verifyFormat("bool x = f<int>() > 5;"); 6496 verifyFormat("bool x = 5 < a<int>::x;"); 6497 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 6498 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 6499 6500 verifyGoogleFormat("A<A<int>> a;"); 6501 verifyGoogleFormat("A<A<A<int>>> a;"); 6502 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 6503 verifyGoogleFormat("A<A<int> > a;"); 6504 verifyGoogleFormat("A<A<A<int> > > a;"); 6505 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 6506 verifyGoogleFormat("A<::A<int>> a;"); 6507 verifyGoogleFormat("A<::A> a;"); 6508 verifyGoogleFormat("A< ::A> a;"); 6509 verifyGoogleFormat("A< ::A<int> > a;"); 6510 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 6511 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 6512 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 6513 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 6514 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 6515 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 6516 6517 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 6518 6519 verifyFormat("test >> a >> b;"); 6520 verifyFormat("test << a >> b;"); 6521 6522 verifyFormat("f<int>();"); 6523 verifyFormat("template <typename T> void f() {}"); 6524 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 6525 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 6526 "sizeof(char)>::type>;"); 6527 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 6528 verifyFormat("f(a.operator()<A>());"); 6529 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6530 " .template operator()<A>());", 6531 getLLVMStyleWithColumns(35)); 6532 6533 // Not template parameters. 6534 verifyFormat("return a < b && c > d;"); 6535 verifyFormat("void f() {\n" 6536 " while (a < b && c > d) {\n" 6537 " }\n" 6538 "}"); 6539 verifyFormat("template <typename... Types>\n" 6540 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 6541 6542 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6543 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 6544 getLLVMStyleWithColumns(60)); 6545 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 6546 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 6547 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 6548 } 6549 6550 TEST_F(FormatTest, BitshiftOperatorWidth) { 6551 EXPECT_EQ("int a = 1 << 2; /* foo\n" 6552 " bar */", 6553 format("int a=1<<2; /* foo\n" 6554 " bar */")); 6555 6556 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 6557 " bar */", 6558 format("int b =256>>1 ; /* foo\n" 6559 " bar */")); 6560 } 6561 6562 TEST_F(FormatTest, UnderstandsBinaryOperators) { 6563 verifyFormat("COMPARE(a, ==, b);"); 6564 verifyFormat("auto s = sizeof...(Ts) - 1;"); 6565 } 6566 6567 TEST_F(FormatTest, UnderstandsPointersToMembers) { 6568 verifyFormat("int A::*x;"); 6569 verifyFormat("int (S::*func)(void *);"); 6570 verifyFormat("void f() { int (S::*func)(void *); }"); 6571 verifyFormat("typedef bool *(Class::*Member)() const;"); 6572 verifyFormat("void f() {\n" 6573 " (a->*f)();\n" 6574 " a->*x;\n" 6575 " (a.*f)();\n" 6576 " ((*a).*f)();\n" 6577 " a.*x;\n" 6578 "}"); 6579 verifyFormat("void f() {\n" 6580 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6581 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 6582 "}"); 6583 verifyFormat( 6584 "(aaaaaaaaaa->*bbbbbbb)(\n" 6585 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6586 FormatStyle Style = getLLVMStyle(); 6587 Style.PointerAlignment = FormatStyle::PAS_Left; 6588 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 6589 } 6590 6591 TEST_F(FormatTest, UnderstandsUnaryOperators) { 6592 verifyFormat("int a = -2;"); 6593 verifyFormat("f(-1, -2, -3);"); 6594 verifyFormat("a[-1] = 5;"); 6595 verifyFormat("int a = 5 + -2;"); 6596 verifyFormat("if (i == -1) {\n}"); 6597 verifyFormat("if (i != -1) {\n}"); 6598 verifyFormat("if (i > -1) {\n}"); 6599 verifyFormat("if (i < -1) {\n}"); 6600 verifyFormat("++(a->f());"); 6601 verifyFormat("--(a->f());"); 6602 verifyFormat("(a->f())++;"); 6603 verifyFormat("a[42]++;"); 6604 verifyFormat("if (!(a->f())) {\n}"); 6605 verifyFormat("if (!+i) {\n}"); 6606 verifyFormat("~&a;"); 6607 6608 verifyFormat("a-- > b;"); 6609 verifyFormat("b ? -a : c;"); 6610 verifyFormat("n * sizeof char16;"); 6611 verifyFormat("n * alignof char16;", getGoogleStyle()); 6612 verifyFormat("sizeof(char);"); 6613 verifyFormat("alignof(char);", getGoogleStyle()); 6614 6615 verifyFormat("return -1;"); 6616 verifyFormat("switch (a) {\n" 6617 "case -1:\n" 6618 " break;\n" 6619 "}"); 6620 verifyFormat("#define X -1"); 6621 verifyFormat("#define X -kConstant"); 6622 6623 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 6624 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 6625 6626 verifyFormat("int a = /* confusing comment */ -1;"); 6627 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 6628 verifyFormat("int a = i /* confusing comment */++;"); 6629 } 6630 6631 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 6632 verifyFormat("if (!aaaaaaaaaa( // break\n" 6633 " aaaaa)) {\n" 6634 "}"); 6635 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 6636 " aaaaa));"); 6637 verifyFormat("*aaa = aaaaaaa( // break\n" 6638 " bbbbbb);"); 6639 } 6640 6641 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 6642 verifyFormat("bool operator<();"); 6643 verifyFormat("bool operator>();"); 6644 verifyFormat("bool operator=();"); 6645 verifyFormat("bool operator==();"); 6646 verifyFormat("bool operator!=();"); 6647 verifyFormat("int operator+();"); 6648 verifyFormat("int operator++();"); 6649 verifyFormat("int operator++(int) volatile noexcept;"); 6650 verifyFormat("bool operator,();"); 6651 verifyFormat("bool operator();"); 6652 verifyFormat("bool operator()();"); 6653 verifyFormat("bool operator[]();"); 6654 verifyFormat("operator bool();"); 6655 verifyFormat("operator int();"); 6656 verifyFormat("operator void *();"); 6657 verifyFormat("operator SomeType<int>();"); 6658 verifyFormat("operator SomeType<int, int>();"); 6659 verifyFormat("operator SomeType<SomeType<int>>();"); 6660 verifyFormat("void *operator new(std::size_t size);"); 6661 verifyFormat("void *operator new[](std::size_t size);"); 6662 verifyFormat("void operator delete(void *ptr);"); 6663 verifyFormat("void operator delete[](void *ptr);"); 6664 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 6665 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 6666 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 6667 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 6668 6669 verifyFormat( 6670 "ostream &operator<<(ostream &OutputStream,\n" 6671 " SomeReallyLongType WithSomeReallyLongValue);"); 6672 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 6673 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 6674 " return left.group < right.group;\n" 6675 "}"); 6676 verifyFormat("SomeType &operator=(const SomeType &S);"); 6677 verifyFormat("f.template operator()<int>();"); 6678 6679 verifyGoogleFormat("operator void*();"); 6680 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 6681 verifyGoogleFormat("operator ::A();"); 6682 6683 verifyFormat("using A::operator+;"); 6684 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 6685 "int i;"); 6686 } 6687 6688 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 6689 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 6690 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 6691 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 6692 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 6693 verifyFormat("Deleted &operator=(const Deleted &) &;"); 6694 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 6695 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 6696 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 6697 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 6698 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 6699 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 6700 verifyFormat("void Fn(T const &) const &;"); 6701 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 6702 verifyFormat("template <typename T>\n" 6703 "void F(T) && = delete;", 6704 getGoogleStyle()); 6705 6706 FormatStyle AlignLeft = getLLVMStyle(); 6707 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 6708 verifyFormat("void A::b() && {}", AlignLeft); 6709 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 6710 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 6711 AlignLeft); 6712 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 6713 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 6714 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 6715 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 6716 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 6717 verifyFormat("auto Function(T) & -> void;", AlignLeft); 6718 verifyFormat("void Fn(T const&) const&;", AlignLeft); 6719 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 6720 6721 FormatStyle Spaces = getLLVMStyle(); 6722 Spaces.SpacesInCStyleCastParentheses = true; 6723 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 6724 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 6725 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 6726 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 6727 6728 Spaces.SpacesInCStyleCastParentheses = false; 6729 Spaces.SpacesInParentheses = true; 6730 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 6731 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 6732 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 6733 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 6734 } 6735 6736 TEST_F(FormatTest, UnderstandsNewAndDelete) { 6737 verifyFormat("void f() {\n" 6738 " A *a = new A;\n" 6739 " A *a = new (placement) A;\n" 6740 " delete a;\n" 6741 " delete (A *)a;\n" 6742 "}"); 6743 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 6744 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 6745 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6746 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 6747 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 6748 verifyFormat("delete[] h->p;"); 6749 } 6750 6751 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 6752 verifyFormat("int *f(int *a) {}"); 6753 verifyFormat("int main(int argc, char **argv) {}"); 6754 verifyFormat("Test::Test(int b) : a(b * b) {}"); 6755 verifyIndependentOfContext("f(a, *a);"); 6756 verifyFormat("void g() { f(*a); }"); 6757 verifyIndependentOfContext("int a = b * 10;"); 6758 verifyIndependentOfContext("int a = 10 * b;"); 6759 verifyIndependentOfContext("int a = b * c;"); 6760 verifyIndependentOfContext("int a += b * c;"); 6761 verifyIndependentOfContext("int a -= b * c;"); 6762 verifyIndependentOfContext("int a *= b * c;"); 6763 verifyIndependentOfContext("int a /= b * c;"); 6764 verifyIndependentOfContext("int a = *b;"); 6765 verifyIndependentOfContext("int a = *b * c;"); 6766 verifyIndependentOfContext("int a = b * *c;"); 6767 verifyIndependentOfContext("int a = b * (10);"); 6768 verifyIndependentOfContext("S << b * (10);"); 6769 verifyIndependentOfContext("return 10 * b;"); 6770 verifyIndependentOfContext("return *b * *c;"); 6771 verifyIndependentOfContext("return a & ~b;"); 6772 verifyIndependentOfContext("f(b ? *c : *d);"); 6773 verifyIndependentOfContext("int a = b ? *c : *d;"); 6774 verifyIndependentOfContext("*b = a;"); 6775 verifyIndependentOfContext("a * ~b;"); 6776 verifyIndependentOfContext("a * !b;"); 6777 verifyIndependentOfContext("a * +b;"); 6778 verifyIndependentOfContext("a * -b;"); 6779 verifyIndependentOfContext("a * ++b;"); 6780 verifyIndependentOfContext("a * --b;"); 6781 verifyIndependentOfContext("a[4] * b;"); 6782 verifyIndependentOfContext("a[a * a] = 1;"); 6783 verifyIndependentOfContext("f() * b;"); 6784 verifyIndependentOfContext("a * [self dostuff];"); 6785 verifyIndependentOfContext("int x = a * (a + b);"); 6786 verifyIndependentOfContext("(a *)(a + b);"); 6787 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 6788 verifyIndependentOfContext("int *pa = (int *)&a;"); 6789 verifyIndependentOfContext("return sizeof(int **);"); 6790 verifyIndependentOfContext("return sizeof(int ******);"); 6791 verifyIndependentOfContext("return (int **&)a;"); 6792 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 6793 verifyFormat("void f(Type (*parameter)[10]) {}"); 6794 verifyFormat("void f(Type (¶meter)[10]) {}"); 6795 verifyGoogleFormat("return sizeof(int**);"); 6796 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 6797 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 6798 verifyFormat("auto a = [](int **&, int ***) {};"); 6799 verifyFormat("auto PointerBinding = [](const char *S) {};"); 6800 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 6801 verifyFormat("[](const decltype(*a) &value) {}"); 6802 verifyFormat("decltype(a * b) F();"); 6803 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 6804 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 6805 verifyIndependentOfContext("typedef void (*f)(int *a);"); 6806 verifyIndependentOfContext("int i{a * b};"); 6807 verifyIndependentOfContext("aaa && aaa->f();"); 6808 verifyIndependentOfContext("int x = ~*p;"); 6809 verifyFormat("Constructor() : a(a), area(width * height) {}"); 6810 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 6811 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 6812 verifyFormat("void f() { f(a, c * d); }"); 6813 verifyFormat("void f() { f(new a(), c * d); }"); 6814 verifyFormat("void f(const MyOverride &override);"); 6815 verifyFormat("void f(const MyFinal &final);"); 6816 verifyIndependentOfContext("bool a = f() && override.f();"); 6817 verifyIndependentOfContext("bool a = f() && final.f();"); 6818 6819 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 6820 6821 verifyIndependentOfContext("A<int *> a;"); 6822 verifyIndependentOfContext("A<int **> a;"); 6823 verifyIndependentOfContext("A<int *, int *> a;"); 6824 verifyIndependentOfContext("A<int *[]> a;"); 6825 verifyIndependentOfContext( 6826 "const char *const p = reinterpret_cast<const char *const>(q);"); 6827 verifyIndependentOfContext("A<int **, int **> a;"); 6828 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 6829 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 6830 verifyFormat("for (; a && b;) {\n}"); 6831 verifyFormat("bool foo = true && [] { return false; }();"); 6832 6833 verifyFormat( 6834 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6835 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6836 6837 verifyGoogleFormat("int const* a = &b;"); 6838 verifyGoogleFormat("**outparam = 1;"); 6839 verifyGoogleFormat("*outparam = a * b;"); 6840 verifyGoogleFormat("int main(int argc, char** argv) {}"); 6841 verifyGoogleFormat("A<int*> a;"); 6842 verifyGoogleFormat("A<int**> a;"); 6843 verifyGoogleFormat("A<int*, int*> a;"); 6844 verifyGoogleFormat("A<int**, int**> a;"); 6845 verifyGoogleFormat("f(b ? *c : *d);"); 6846 verifyGoogleFormat("int a = b ? *c : *d;"); 6847 verifyGoogleFormat("Type* t = **x;"); 6848 verifyGoogleFormat("Type* t = *++*x;"); 6849 verifyGoogleFormat("*++*x;"); 6850 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 6851 verifyGoogleFormat("Type* t = x++ * y;"); 6852 verifyGoogleFormat( 6853 "const char* const p = reinterpret_cast<const char* const>(q);"); 6854 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 6855 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 6856 verifyGoogleFormat("template <typename T>\n" 6857 "void f(int i = 0, SomeType** temps = NULL);"); 6858 6859 FormatStyle Left = getLLVMStyle(); 6860 Left.PointerAlignment = FormatStyle::PAS_Left; 6861 verifyFormat("x = *a(x) = *a(y);", Left); 6862 verifyFormat("for (;; *a = b) {\n}", Left); 6863 verifyFormat("return *this += 1;", Left); 6864 verifyFormat("throw *x;", Left); 6865 verifyFormat("delete *x;", Left); 6866 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 6867 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 6868 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 6869 6870 verifyIndependentOfContext("a = *(x + y);"); 6871 verifyIndependentOfContext("a = &(x + y);"); 6872 verifyIndependentOfContext("*(x + y).call();"); 6873 verifyIndependentOfContext("&(x + y)->call();"); 6874 verifyFormat("void f() { &(*I).first; }"); 6875 6876 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 6877 verifyFormat( 6878 "int *MyValues = {\n" 6879 " *A, // Operator detection might be confused by the '{'\n" 6880 " *BB // Operator detection might be confused by previous comment\n" 6881 "};"); 6882 6883 verifyIndependentOfContext("if (int *a = &b)"); 6884 verifyIndependentOfContext("if (int &a = *b)"); 6885 verifyIndependentOfContext("if (a & b[i])"); 6886 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 6887 verifyIndependentOfContext("if (*b[i])"); 6888 verifyIndependentOfContext("if (int *a = (&b))"); 6889 verifyIndependentOfContext("while (int *a = &b)"); 6890 verifyIndependentOfContext("size = sizeof *a;"); 6891 verifyIndependentOfContext("if (a && (b = c))"); 6892 verifyFormat("void f() {\n" 6893 " for (const int &v : Values) {\n" 6894 " }\n" 6895 "}"); 6896 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 6897 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 6898 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 6899 6900 verifyFormat("#define A (!a * b)"); 6901 verifyFormat("#define MACRO \\\n" 6902 " int *i = a * b; \\\n" 6903 " void f(a *b);", 6904 getLLVMStyleWithColumns(19)); 6905 6906 verifyIndependentOfContext("A = new SomeType *[Length];"); 6907 verifyIndependentOfContext("A = new SomeType *[Length]();"); 6908 verifyIndependentOfContext("T **t = new T *;"); 6909 verifyIndependentOfContext("T **t = new T *();"); 6910 verifyGoogleFormat("A = new SomeType*[Length]();"); 6911 verifyGoogleFormat("A = new SomeType*[Length];"); 6912 verifyGoogleFormat("T** t = new T*;"); 6913 verifyGoogleFormat("T** t = new T*();"); 6914 6915 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 6916 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 6917 verifyFormat("template <bool a, bool b> " 6918 "typename t::if<x && y>::type f() {}"); 6919 verifyFormat("template <int *y> f() {}"); 6920 verifyFormat("vector<int *> v;"); 6921 verifyFormat("vector<int *const> v;"); 6922 verifyFormat("vector<int *const **const *> v;"); 6923 verifyFormat("vector<int *volatile> v;"); 6924 verifyFormat("vector<a * b> v;"); 6925 verifyFormat("foo<b && false>();"); 6926 verifyFormat("foo<b & 1>();"); 6927 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 6928 verifyFormat( 6929 "template <class T, class = typename std::enable_if<\n" 6930 " std::is_integral<T>::value &&\n" 6931 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 6932 "void F();", 6933 getLLVMStyleWithColumns(70)); 6934 verifyFormat( 6935 "template <class T,\n" 6936 " class = typename std::enable_if<\n" 6937 " std::is_integral<T>::value &&\n" 6938 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 6939 " class U>\n" 6940 "void F();", 6941 getLLVMStyleWithColumns(70)); 6942 verifyFormat( 6943 "template <class T,\n" 6944 " class = typename ::std::enable_if<\n" 6945 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 6946 "void F();", 6947 getGoogleStyleWithColumns(68)); 6948 6949 verifyIndependentOfContext("MACRO(int *i);"); 6950 verifyIndependentOfContext("MACRO(auto *a);"); 6951 verifyIndependentOfContext("MACRO(const A *a);"); 6952 verifyIndependentOfContext("MACRO(A *const a);"); 6953 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 6954 verifyFormat("void f() { f(float{1}, a * a); }"); 6955 // FIXME: Is there a way to make this work? 6956 // verifyIndependentOfContext("MACRO(A *a);"); 6957 6958 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 6959 verifyFormat("return options != nullptr && operator==(*options);"); 6960 6961 EXPECT_EQ("#define OP(x) \\\n" 6962 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6963 " return s << a.DebugString(); \\\n" 6964 " }", 6965 format("#define OP(x) \\\n" 6966 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6967 " return s << a.DebugString(); \\\n" 6968 " }", 6969 getLLVMStyleWithColumns(50))); 6970 6971 // FIXME: We cannot handle this case yet; we might be able to figure out that 6972 // foo<x> d > v; doesn't make sense. 6973 verifyFormat("foo<a<b && c> d> v;"); 6974 6975 FormatStyle PointerMiddle = getLLVMStyle(); 6976 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 6977 verifyFormat("delete *x;", PointerMiddle); 6978 verifyFormat("int * x;", PointerMiddle); 6979 verifyFormat("int *[] x;", PointerMiddle); 6980 verifyFormat("template <int * y> f() {}", PointerMiddle); 6981 verifyFormat("int * f(int * a) {}", PointerMiddle); 6982 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 6983 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 6984 verifyFormat("A<int *> a;", PointerMiddle); 6985 verifyFormat("A<int **> a;", PointerMiddle); 6986 verifyFormat("A<int *, int *> a;", PointerMiddle); 6987 verifyFormat("A<int *[]> a;", PointerMiddle); 6988 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 6989 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 6990 verifyFormat("T ** t = new T *;", PointerMiddle); 6991 6992 // Member function reference qualifiers aren't binary operators. 6993 verifyFormat("string // break\n" 6994 "operator()() & {}"); 6995 verifyFormat("string // break\n" 6996 "operator()() && {}"); 6997 verifyGoogleFormat("template <typename T>\n" 6998 "auto x() & -> int {}"); 6999 } 7000 7001 TEST_F(FormatTest, UnderstandsAttributes) { 7002 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 7003 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 7004 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 7005 FormatStyle AfterType = getLLVMStyle(); 7006 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 7007 verifyFormat("__attribute__((nodebug)) void\n" 7008 "foo() {}\n", 7009 AfterType); 7010 } 7011 7012 TEST_F(FormatTest, UnderstandsSquareAttributes) { 7013 verifyFormat("SomeType s [[unused]] (InitValue);"); 7014 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 7015 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 7016 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 7017 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 7018 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7019 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 7020 7021 // Make sure we do not mistake attributes for array subscripts. 7022 verifyFormat("int a() {}\n" 7023 "[[unused]] int b() {}\n"); 7024 verifyFormat("NSArray *arr;\n" 7025 "arr[[Foo() bar]];"); 7026 7027 // On the other hand, we still need to correctly find array subscripts. 7028 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 7029 7030 // Make sure we do not parse attributes as lambda introducers. 7031 FormatStyle MultiLineFunctions = getLLVMStyle(); 7032 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7033 verifyFormat("[[unused]] int b() {\n" 7034 " return 42;\n" 7035 "}\n", 7036 MultiLineFunctions); 7037 } 7038 7039 TEST_F(FormatTest, UnderstandsEllipsis) { 7040 verifyFormat("int printf(const char *fmt, ...);"); 7041 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 7042 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 7043 7044 FormatStyle PointersLeft = getLLVMStyle(); 7045 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 7046 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 7047 } 7048 7049 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 7050 EXPECT_EQ("int *a;\n" 7051 "int *a;\n" 7052 "int *a;", 7053 format("int *a;\n" 7054 "int* a;\n" 7055 "int *a;", 7056 getGoogleStyle())); 7057 EXPECT_EQ("int* a;\n" 7058 "int* a;\n" 7059 "int* a;", 7060 format("int* a;\n" 7061 "int* a;\n" 7062 "int *a;", 7063 getGoogleStyle())); 7064 EXPECT_EQ("int *a;\n" 7065 "int *a;\n" 7066 "int *a;", 7067 format("int *a;\n" 7068 "int * a;\n" 7069 "int * a;", 7070 getGoogleStyle())); 7071 EXPECT_EQ("auto x = [] {\n" 7072 " int *a;\n" 7073 " int *a;\n" 7074 " int *a;\n" 7075 "};", 7076 format("auto x=[]{int *a;\n" 7077 "int * a;\n" 7078 "int * a;};", 7079 getGoogleStyle())); 7080 } 7081 7082 TEST_F(FormatTest, UnderstandsRvalueReferences) { 7083 verifyFormat("int f(int &&a) {}"); 7084 verifyFormat("int f(int a, char &&b) {}"); 7085 verifyFormat("void f() { int &&a = b; }"); 7086 verifyGoogleFormat("int f(int a, char&& b) {}"); 7087 verifyGoogleFormat("void f() { int&& a = b; }"); 7088 7089 verifyIndependentOfContext("A<int &&> a;"); 7090 verifyIndependentOfContext("A<int &&, int &&> a;"); 7091 verifyGoogleFormat("A<int&&> a;"); 7092 verifyGoogleFormat("A<int&&, int&&> a;"); 7093 7094 // Not rvalue references: 7095 verifyFormat("template <bool B, bool C> class A {\n" 7096 " static_assert(B && C, \"Something is wrong\");\n" 7097 "};"); 7098 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 7099 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 7100 verifyFormat("#define A(a, b) (a && b)"); 7101 } 7102 7103 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 7104 verifyFormat("void f() {\n" 7105 " x[aaaaaaaaa -\n" 7106 " b] = 23;\n" 7107 "}", 7108 getLLVMStyleWithColumns(15)); 7109 } 7110 7111 TEST_F(FormatTest, FormatsCasts) { 7112 verifyFormat("Type *A = static_cast<Type *>(P);"); 7113 verifyFormat("Type *A = (Type *)P;"); 7114 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 7115 verifyFormat("int a = (int)(2.0f);"); 7116 verifyFormat("int a = (int)2.0f;"); 7117 verifyFormat("x[(int32)y];"); 7118 verifyFormat("x = (int32)y;"); 7119 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 7120 verifyFormat("int a = (int)*b;"); 7121 verifyFormat("int a = (int)2.0f;"); 7122 verifyFormat("int a = (int)~0;"); 7123 verifyFormat("int a = (int)++a;"); 7124 verifyFormat("int a = (int)sizeof(int);"); 7125 verifyFormat("int a = (int)+2;"); 7126 verifyFormat("my_int a = (my_int)2.0f;"); 7127 verifyFormat("my_int a = (my_int)sizeof(int);"); 7128 verifyFormat("return (my_int)aaa;"); 7129 verifyFormat("#define x ((int)-1)"); 7130 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 7131 verifyFormat("#define p(q) ((int *)&q)"); 7132 verifyFormat("fn(a)(b) + 1;"); 7133 7134 verifyFormat("void f() { my_int a = (my_int)*b; }"); 7135 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 7136 verifyFormat("my_int a = (my_int)~0;"); 7137 verifyFormat("my_int a = (my_int)++a;"); 7138 verifyFormat("my_int a = (my_int)-2;"); 7139 verifyFormat("my_int a = (my_int)1;"); 7140 verifyFormat("my_int a = (my_int *)1;"); 7141 verifyFormat("my_int a = (const my_int)-1;"); 7142 verifyFormat("my_int a = (const my_int *)-1;"); 7143 verifyFormat("my_int a = (my_int)(my_int)-1;"); 7144 verifyFormat("my_int a = (ns::my_int)-2;"); 7145 verifyFormat("case (my_int)ONE:"); 7146 verifyFormat("auto x = (X)this;"); 7147 7148 // FIXME: single value wrapped with paren will be treated as cast. 7149 verifyFormat("void f(int i = (kValue)*kMask) {}"); 7150 7151 verifyFormat("{ (void)F; }"); 7152 7153 // Don't break after a cast's 7154 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7155 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 7156 " bbbbbbbbbbbbbbbbbbbbbb);"); 7157 7158 // These are not casts. 7159 verifyFormat("void f(int *) {}"); 7160 verifyFormat("f(foo)->b;"); 7161 verifyFormat("f(foo).b;"); 7162 verifyFormat("f(foo)(b);"); 7163 verifyFormat("f(foo)[b];"); 7164 verifyFormat("[](foo) { return 4; }(bar);"); 7165 verifyFormat("(*funptr)(foo)[4];"); 7166 verifyFormat("funptrs[4](foo)[4];"); 7167 verifyFormat("void f(int *);"); 7168 verifyFormat("void f(int *) = 0;"); 7169 verifyFormat("void f(SmallVector<int>) {}"); 7170 verifyFormat("void f(SmallVector<int>);"); 7171 verifyFormat("void f(SmallVector<int>) = 0;"); 7172 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 7173 verifyFormat("int a = sizeof(int) * b;"); 7174 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 7175 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 7176 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 7177 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 7178 7179 // These are not casts, but at some point were confused with casts. 7180 verifyFormat("virtual void foo(int *) override;"); 7181 verifyFormat("virtual void foo(char &) const;"); 7182 verifyFormat("virtual void foo(int *a, char *) const;"); 7183 verifyFormat("int a = sizeof(int *) + b;"); 7184 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 7185 verifyFormat("bool b = f(g<int>) && c;"); 7186 verifyFormat("typedef void (*f)(int i) func;"); 7187 7188 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 7189 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 7190 // FIXME: The indentation here is not ideal. 7191 verifyFormat( 7192 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7193 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 7194 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 7195 } 7196 7197 TEST_F(FormatTest, FormatsFunctionTypes) { 7198 verifyFormat("A<bool()> a;"); 7199 verifyFormat("A<SomeType()> a;"); 7200 verifyFormat("A<void (*)(int, std::string)> a;"); 7201 verifyFormat("A<void *(int)>;"); 7202 verifyFormat("void *(*a)(int *, SomeType *);"); 7203 verifyFormat("int (*func)(void *);"); 7204 verifyFormat("void f() { int (*func)(void *); }"); 7205 verifyFormat("template <class CallbackClass>\n" 7206 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 7207 7208 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 7209 verifyGoogleFormat("void* (*a)(int);"); 7210 verifyGoogleFormat( 7211 "template <class CallbackClass>\n" 7212 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 7213 7214 // Other constructs can look somewhat like function types: 7215 verifyFormat("A<sizeof(*x)> a;"); 7216 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 7217 verifyFormat("some_var = function(*some_pointer_var)[0];"); 7218 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 7219 verifyFormat("int x = f(&h)();"); 7220 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 7221 verifyFormat("std::function<\n" 7222 " LooooooooooongTemplatedType<\n" 7223 " SomeType>*(\n" 7224 " LooooooooooooooooongType type)>\n" 7225 " function;", 7226 getGoogleStyleWithColumns(40)); 7227 } 7228 7229 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 7230 verifyFormat("A (*foo_)[6];"); 7231 verifyFormat("vector<int> (*foo_)[6];"); 7232 } 7233 7234 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 7235 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7236 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 7237 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 7238 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 7239 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7240 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 7241 7242 // Different ways of ()-initializiation. 7243 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7244 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 7245 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7246 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 7247 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7248 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 7249 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7250 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 7251 7252 // Lambdas should not confuse the variable declaration heuristic. 7253 verifyFormat("LooooooooooooooooongType\n" 7254 " variable(nullptr, [](A *a) {});", 7255 getLLVMStyleWithColumns(40)); 7256 } 7257 7258 TEST_F(FormatTest, BreaksLongDeclarations) { 7259 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 7260 " AnotherNameForTheLongType;"); 7261 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 7262 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7263 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7264 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 7265 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 7266 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 7267 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7268 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 7269 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 7270 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 7271 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 7272 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 7273 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 7274 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 7275 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7276 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 7277 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7278 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 7279 FormatStyle Indented = getLLVMStyle(); 7280 Indented.IndentWrappedFunctionNames = true; 7281 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7282 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 7283 Indented); 7284 verifyFormat( 7285 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7286 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 7287 Indented); 7288 verifyFormat( 7289 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 7290 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 7291 Indented); 7292 verifyFormat( 7293 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 7294 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 7295 Indented); 7296 7297 // FIXME: Without the comment, this breaks after "(". 7298 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 7299 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 7300 getGoogleStyle()); 7301 7302 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 7303 " int LoooooooooooooooooooongParam2) {}"); 7304 verifyFormat( 7305 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 7306 " SourceLocation L, IdentifierIn *II,\n" 7307 " Type *T) {}"); 7308 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 7309 "ReallyReaaallyLongFunctionName(\n" 7310 " const std::string &SomeParameter,\n" 7311 " const SomeType<string, SomeOtherTemplateParameter>\n" 7312 " &ReallyReallyLongParameterName,\n" 7313 " const SomeType<string, SomeOtherTemplateParameter>\n" 7314 " &AnotherLongParameterName) {}"); 7315 verifyFormat("template <typename A>\n" 7316 "SomeLoooooooooooooooooooooongType<\n" 7317 " typename some_namespace::SomeOtherType<A>::Type>\n" 7318 "Function() {}"); 7319 7320 verifyGoogleFormat( 7321 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 7322 " aaaaaaaaaaaaaaaaaaaaaaa;"); 7323 verifyGoogleFormat( 7324 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 7325 " SourceLocation L) {}"); 7326 verifyGoogleFormat( 7327 "some_namespace::LongReturnType\n" 7328 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 7329 " int first_long_parameter, int second_parameter) {}"); 7330 7331 verifyGoogleFormat("template <typename T>\n" 7332 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 7333 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 7334 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7335 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 7336 7337 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 7338 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7339 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7340 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7341 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 7342 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 7343 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7344 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7345 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 7346 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7347 7348 verifyFormat("template <typename T> // Templates on own line.\n" 7349 "static int // Some comment.\n" 7350 "MyFunction(int a);", 7351 getLLVMStyle()); 7352 } 7353 7354 TEST_F(FormatTest, FormatsArrays) { 7355 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 7356 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 7357 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 7358 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 7359 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 7360 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 7361 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7362 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 7363 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7364 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 7365 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7366 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 7367 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 7368 verifyFormat( 7369 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 7370 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 7371 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7372 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 7373 " .aaaaaaaaaaaaaaaaaaaaaa();"); 7374 7375 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 7376 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 7377 verifyFormat( 7378 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 7379 " .aaaaaaa[0]\n" 7380 " .aaaaaaaaaaaaaaaaaaaaaa();"); 7381 verifyFormat("a[::b::c];"); 7382 7383 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 7384 7385 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 7386 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 7387 } 7388 7389 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 7390 verifyFormat("(a)->b();"); 7391 verifyFormat("--a;"); 7392 } 7393 7394 TEST_F(FormatTest, HandlesIncludeDirectives) { 7395 verifyFormat("#include <string>\n" 7396 "#include <a/b/c.h>\n" 7397 "#include \"a/b/string\"\n" 7398 "#include \"string.h\"\n" 7399 "#include \"string.h\"\n" 7400 "#include <a-a>\n" 7401 "#include < path with space >\n" 7402 "#include_next <test.h>" 7403 "#include \"abc.h\" // this is included for ABC\n" 7404 "#include \"some long include\" // with a comment\n" 7405 "#include \"some very long include path\"\n" 7406 "#include <some/very/long/include/path>\n", 7407 getLLVMStyleWithColumns(35)); 7408 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 7409 EXPECT_EQ("#include <a>", format("#include<a>")); 7410 7411 verifyFormat("#import <string>"); 7412 verifyFormat("#import <a/b/c.h>"); 7413 verifyFormat("#import \"a/b/string\""); 7414 verifyFormat("#import \"string.h\""); 7415 verifyFormat("#import \"string.h\""); 7416 verifyFormat("#if __has_include(<strstream>)\n" 7417 "#include <strstream>\n" 7418 "#endif"); 7419 7420 verifyFormat("#define MY_IMPORT <a/b>"); 7421 7422 verifyFormat("#if __has_include(<a/b>)"); 7423 verifyFormat("#if __has_include_next(<a/b>)"); 7424 verifyFormat("#define F __has_include(<a/b>)"); 7425 verifyFormat("#define F __has_include_next(<a/b>)"); 7426 7427 // Protocol buffer definition or missing "#". 7428 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 7429 getLLVMStyleWithColumns(30)); 7430 7431 FormatStyle Style = getLLVMStyle(); 7432 Style.AlwaysBreakBeforeMultilineStrings = true; 7433 Style.ColumnLimit = 0; 7434 verifyFormat("#import \"abc.h\"", Style); 7435 7436 // But 'import' might also be a regular C++ namespace. 7437 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7438 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7439 } 7440 7441 //===----------------------------------------------------------------------===// 7442 // Error recovery tests. 7443 //===----------------------------------------------------------------------===// 7444 7445 TEST_F(FormatTest, IncompleteParameterLists) { 7446 FormatStyle NoBinPacking = getLLVMStyle(); 7447 NoBinPacking.BinPackParameters = false; 7448 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 7449 " double *min_x,\n" 7450 " double *max_x,\n" 7451 " double *min_y,\n" 7452 " double *max_y,\n" 7453 " double *min_z,\n" 7454 " double *max_z, ) {}", 7455 NoBinPacking); 7456 } 7457 7458 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 7459 verifyFormat("void f() { return; }\n42"); 7460 verifyFormat("void f() {\n" 7461 " if (0)\n" 7462 " return;\n" 7463 "}\n" 7464 "42"); 7465 verifyFormat("void f() { return }\n42"); 7466 verifyFormat("void f() {\n" 7467 " if (0)\n" 7468 " return\n" 7469 "}\n" 7470 "42"); 7471 } 7472 7473 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 7474 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 7475 EXPECT_EQ("void f() {\n" 7476 " if (a)\n" 7477 " return\n" 7478 "}", 7479 format("void f ( ) { if ( a ) return }")); 7480 EXPECT_EQ("namespace N {\n" 7481 "void f()\n" 7482 "}", 7483 format("namespace N { void f() }")); 7484 EXPECT_EQ("namespace N {\n" 7485 "void f() {}\n" 7486 "void g()\n" 7487 "} // namespace N", 7488 format("namespace N { void f( ) { } void g( ) }")); 7489 } 7490 7491 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 7492 verifyFormat("int aaaaaaaa =\n" 7493 " // Overlylongcomment\n" 7494 " b;", 7495 getLLVMStyleWithColumns(20)); 7496 verifyFormat("function(\n" 7497 " ShortArgument,\n" 7498 " LoooooooooooongArgument);\n", 7499 getLLVMStyleWithColumns(20)); 7500 } 7501 7502 TEST_F(FormatTest, IncorrectAccessSpecifier) { 7503 verifyFormat("public:"); 7504 verifyFormat("class A {\n" 7505 "public\n" 7506 " void f() {}\n" 7507 "};"); 7508 verifyFormat("public\n" 7509 "int qwerty;"); 7510 verifyFormat("public\n" 7511 "B {}"); 7512 verifyFormat("public\n" 7513 "{}"); 7514 verifyFormat("public\n" 7515 "B { int x; }"); 7516 } 7517 7518 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 7519 verifyFormat("{"); 7520 verifyFormat("#})"); 7521 verifyNoCrash("(/**/[:!] ?[)."); 7522 } 7523 7524 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 7525 // Found by oss-fuzz: 7526 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 7527 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 7528 Style.ColumnLimit = 60; 7529 verifyNoCrash( 7530 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 7531 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 7532 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 7533 Style); 7534 } 7535 7536 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 7537 verifyFormat("do {\n}"); 7538 verifyFormat("do {\n}\n" 7539 "f();"); 7540 verifyFormat("do {\n}\n" 7541 "wheeee(fun);"); 7542 verifyFormat("do {\n" 7543 " f();\n" 7544 "}"); 7545 } 7546 7547 TEST_F(FormatTest, IncorrectCodeMissingParens) { 7548 verifyFormat("if {\n foo;\n foo();\n}"); 7549 verifyFormat("switch {\n foo;\n foo();\n}"); 7550 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 7551 verifyFormat("while {\n foo;\n foo();\n}"); 7552 verifyFormat("do {\n foo;\n foo();\n} while;"); 7553 } 7554 7555 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 7556 verifyIncompleteFormat("namespace {\n" 7557 "class Foo { Foo (\n" 7558 "};\n" 7559 "} // namespace"); 7560 } 7561 7562 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 7563 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 7564 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 7565 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 7566 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 7567 7568 EXPECT_EQ("{\n" 7569 " {\n" 7570 " breakme(\n" 7571 " qwe);\n" 7572 " }\n", 7573 format("{\n" 7574 " {\n" 7575 " breakme(qwe);\n" 7576 "}\n", 7577 getLLVMStyleWithColumns(10))); 7578 } 7579 7580 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 7581 verifyFormat("int x = {\n" 7582 " avariable,\n" 7583 " b(alongervariable)};", 7584 getLLVMStyleWithColumns(25)); 7585 } 7586 7587 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 7588 verifyFormat("return (a)(b){1, 2, 3};"); 7589 } 7590 7591 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 7592 verifyFormat("vector<int> x{1, 2, 3, 4};"); 7593 verifyFormat("vector<int> x{\n" 7594 " 1,\n" 7595 " 2,\n" 7596 " 3,\n" 7597 " 4,\n" 7598 "};"); 7599 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 7600 verifyFormat("f({1, 2});"); 7601 verifyFormat("auto v = Foo{-1};"); 7602 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 7603 verifyFormat("Class::Class : member{1, 2, 3} {}"); 7604 verifyFormat("new vector<int>{1, 2, 3};"); 7605 verifyFormat("new int[3]{1, 2, 3};"); 7606 verifyFormat("new int{1};"); 7607 verifyFormat("return {arg1, arg2};"); 7608 verifyFormat("return {arg1, SomeType{parameter}};"); 7609 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 7610 verifyFormat("new T{arg1, arg2};"); 7611 verifyFormat("f(MyMap[{composite, key}]);"); 7612 verifyFormat("class Class {\n" 7613 " T member = {arg1, arg2};\n" 7614 "};"); 7615 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 7616 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 7617 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 7618 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 7619 verifyFormat("int a = std::is_integral<int>{} + 0;"); 7620 7621 verifyFormat("int foo(int i) { return fo1{}(i); }"); 7622 verifyFormat("int foo(int i) { return fo1{}(i); }"); 7623 verifyFormat("auto i = decltype(x){};"); 7624 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 7625 verifyFormat("Node n{1, Node{1000}, //\n" 7626 " 2};"); 7627 verifyFormat("Aaaa aaaaaaa{\n" 7628 " {\n" 7629 " aaaa,\n" 7630 " },\n" 7631 "};"); 7632 verifyFormat("class C : public D {\n" 7633 " SomeClass SC{2};\n" 7634 "};"); 7635 verifyFormat("class C : public A {\n" 7636 " class D : public B {\n" 7637 " void f() { int i{2}; }\n" 7638 " };\n" 7639 "};"); 7640 verifyFormat("#define A {a, a},"); 7641 7642 // Avoid breaking between equal sign and opening brace 7643 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 7644 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 7645 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 7646 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 7647 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 7648 " {\"ccccccccccccccccccccc\", 2}};", 7649 AvoidBreakingFirstArgument); 7650 7651 // Binpacking only if there is no trailing comma 7652 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 7653 " cccccccccc, dddddddddd};", 7654 getLLVMStyleWithColumns(50)); 7655 verifyFormat("const Aaaaaa aaaaa = {\n" 7656 " aaaaaaaaaaa,\n" 7657 " bbbbbbbbbbb,\n" 7658 " ccccccccccc,\n" 7659 " ddddddddddd,\n" 7660 "};", getLLVMStyleWithColumns(50)); 7661 7662 // Cases where distinguising braced lists and blocks is hard. 7663 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 7664 verifyFormat("void f() {\n" 7665 " return; // comment\n" 7666 "}\n" 7667 "SomeType t;"); 7668 verifyFormat("void f() {\n" 7669 " if (a) {\n" 7670 " f();\n" 7671 " }\n" 7672 "}\n" 7673 "SomeType t;"); 7674 7675 // In combination with BinPackArguments = false. 7676 FormatStyle NoBinPacking = getLLVMStyle(); 7677 NoBinPacking.BinPackArguments = false; 7678 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 7679 " bbbbb,\n" 7680 " ccccc,\n" 7681 " ddddd,\n" 7682 " eeeee,\n" 7683 " ffffff,\n" 7684 " ggggg,\n" 7685 " hhhhhh,\n" 7686 " iiiiii,\n" 7687 " jjjjjj,\n" 7688 " kkkkkk};", 7689 NoBinPacking); 7690 verifyFormat("const Aaaaaa aaaaa = {\n" 7691 " aaaaa,\n" 7692 " bbbbb,\n" 7693 " ccccc,\n" 7694 " ddddd,\n" 7695 " eeeee,\n" 7696 " ffffff,\n" 7697 " ggggg,\n" 7698 " hhhhhh,\n" 7699 " iiiiii,\n" 7700 " jjjjjj,\n" 7701 " kkkkkk,\n" 7702 "};", 7703 NoBinPacking); 7704 verifyFormat( 7705 "const Aaaaaa aaaaa = {\n" 7706 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 7707 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 7708 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 7709 "};", 7710 NoBinPacking); 7711 7712 // FIXME: The alignment of these trailing comments might be bad. Then again, 7713 // this might be utterly useless in real code. 7714 verifyFormat("Constructor::Constructor()\n" 7715 " : some_value{ //\n" 7716 " aaaaaaa, //\n" 7717 " bbbbbbb} {}"); 7718 7719 // In braced lists, the first comment is always assumed to belong to the 7720 // first element. Thus, it can be moved to the next or previous line as 7721 // appropriate. 7722 EXPECT_EQ("function({// First element:\n" 7723 " 1,\n" 7724 " // Second element:\n" 7725 " 2});", 7726 format("function({\n" 7727 " // First element:\n" 7728 " 1,\n" 7729 " // Second element:\n" 7730 " 2});")); 7731 EXPECT_EQ("std::vector<int> MyNumbers{\n" 7732 " // First element:\n" 7733 " 1,\n" 7734 " // Second element:\n" 7735 " 2};", 7736 format("std::vector<int> MyNumbers{// First element:\n" 7737 " 1,\n" 7738 " // Second element:\n" 7739 " 2};", 7740 getLLVMStyleWithColumns(30))); 7741 // A trailing comma should still lead to an enforced line break and no 7742 // binpacking. 7743 EXPECT_EQ("vector<int> SomeVector = {\n" 7744 " // aaa\n" 7745 " 1,\n" 7746 " 2,\n" 7747 "};", 7748 format("vector<int> SomeVector = { // aaa\n" 7749 " 1, 2, };")); 7750 7751 FormatStyle ExtraSpaces = getLLVMStyle(); 7752 ExtraSpaces.Cpp11BracedListStyle = false; 7753 ExtraSpaces.ColumnLimit = 75; 7754 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 7755 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 7756 verifyFormat("f({ 1, 2 });", ExtraSpaces); 7757 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 7758 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 7759 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 7760 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 7761 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 7762 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 7763 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 7764 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 7765 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 7766 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 7767 verifyFormat("class Class {\n" 7768 " T member = { arg1, arg2 };\n" 7769 "};", 7770 ExtraSpaces); 7771 verifyFormat( 7772 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7773 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 7774 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 7775 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 7776 ExtraSpaces); 7777 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 7778 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 7779 ExtraSpaces); 7780 verifyFormat( 7781 "someFunction(OtherParam,\n" 7782 " BracedList{ // comment 1 (Forcing interesting break)\n" 7783 " param1, param2,\n" 7784 " // comment 2\n" 7785 " param3, param4 });", 7786 ExtraSpaces); 7787 verifyFormat( 7788 "std::this_thread::sleep_for(\n" 7789 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 7790 ExtraSpaces); 7791 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 7792 " aaaaaaa,\n" 7793 " aaaaaaaaaa,\n" 7794 " aaaaa,\n" 7795 " aaaaaaaaaaaaaaa,\n" 7796 " aaa,\n" 7797 " aaaaaaaaaa,\n" 7798 " a,\n" 7799 " aaaaaaaaaaaaaaaaaaaaa,\n" 7800 " aaaaaaaaaaaa,\n" 7801 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 7802 " aaaaaaa,\n" 7803 " a};"); 7804 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 7805 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 7806 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 7807 7808 // Avoid breaking between initializer/equal sign and opening brace 7809 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 7810 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 7811 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 7812 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 7813 " { \"ccccccccccccccccccccc\", 2 }\n" 7814 "};", 7815 ExtraSpaces); 7816 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 7817 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 7818 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 7819 " { \"ccccccccccccccccccccc\", 2 }\n" 7820 "};", 7821 ExtraSpaces); 7822 7823 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 7824 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 7825 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 7826 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 7827 } 7828 7829 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 7830 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7831 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7832 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7833 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7834 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7835 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 7836 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 7837 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7838 " 1, 22, 333, 4444, 55555, //\n" 7839 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7840 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 7841 verifyFormat( 7842 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7843 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7844 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 7845 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7846 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7847 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7848 " 7777777};"); 7849 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7850 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7851 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7852 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7853 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7854 " // Separating comment.\n" 7855 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7856 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7857 " // Leading comment\n" 7858 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7859 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7860 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7861 " 1, 1, 1, 1};", 7862 getLLVMStyleWithColumns(39)); 7863 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7864 " 1, 1, 1, 1};", 7865 getLLVMStyleWithColumns(38)); 7866 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 7867 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 7868 getLLVMStyleWithColumns(43)); 7869 verifyFormat( 7870 "static unsigned SomeValues[10][3] = {\n" 7871 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 7872 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 7873 verifyFormat("static auto fields = new vector<string>{\n" 7874 " \"aaaaaaaaaaaaa\",\n" 7875 " \"aaaaaaaaaaaaa\",\n" 7876 " \"aaaaaaaaaaaa\",\n" 7877 " \"aaaaaaaaaaaaaa\",\n" 7878 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 7879 " \"aaaaaaaaaaaa\",\n" 7880 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 7881 "};"); 7882 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 7883 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 7884 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 7885 " 3, cccccccccccccccccccccc};", 7886 getLLVMStyleWithColumns(60)); 7887 7888 // Trailing commas. 7889 verifyFormat("vector<int> x = {\n" 7890 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 7891 "};", 7892 getLLVMStyleWithColumns(39)); 7893 verifyFormat("vector<int> x = {\n" 7894 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 7895 "};", 7896 getLLVMStyleWithColumns(39)); 7897 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7898 " 1, 1, 1, 1,\n" 7899 " /**/ /**/};", 7900 getLLVMStyleWithColumns(39)); 7901 7902 // Trailing comment in the first line. 7903 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 7904 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 7905 " 111111111, 222222222, 3333333333, 444444444, //\n" 7906 " 11111111, 22222222, 333333333, 44444444};"); 7907 // Trailing comment in the last line. 7908 verifyFormat("int aaaaa[] = {\n" 7909 " 1, 2, 3, // comment\n" 7910 " 4, 5, 6 // comment\n" 7911 "};"); 7912 7913 // With nested lists, we should either format one item per line or all nested 7914 // lists one on line. 7915 // FIXME: For some nested lists, we can do better. 7916 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 7917 " {aaaaaaaaaaaaaaaaaaa},\n" 7918 " {aaaaaaaaaaaaaaaaaaaaa},\n" 7919 " {aaaaaaaaaaaaaaaaa}};", 7920 getLLVMStyleWithColumns(60)); 7921 verifyFormat( 7922 "SomeStruct my_struct_array = {\n" 7923 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 7924 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 7925 " {aaa, aaa},\n" 7926 " {aaa, aaa},\n" 7927 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 7928 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7929 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 7930 7931 // No column layout should be used here. 7932 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 7933 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 7934 7935 verifyNoCrash("a<,"); 7936 7937 // No braced initializer here. 7938 verifyFormat("void f() {\n" 7939 " struct Dummy {};\n" 7940 " f(v);\n" 7941 "}"); 7942 7943 // Long lists should be formatted in columns even if they are nested. 7944 verifyFormat( 7945 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7946 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7947 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7948 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7949 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7950 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 7951 7952 // Allow "single-column" layout even if that violates the column limit. There 7953 // isn't going to be a better way. 7954 verifyFormat("std::vector<int> a = {\n" 7955 " aaaaaaaa,\n" 7956 " aaaaaaaa,\n" 7957 " aaaaaaaa,\n" 7958 " aaaaaaaa,\n" 7959 " aaaaaaaaaa,\n" 7960 " aaaaaaaa,\n" 7961 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 7962 getLLVMStyleWithColumns(30)); 7963 verifyFormat("vector<int> aaaa = {\n" 7964 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7965 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7966 " aaaaaa.aaaaaaa,\n" 7967 " aaaaaa.aaaaaaa,\n" 7968 " aaaaaa.aaaaaaa,\n" 7969 " aaaaaa.aaaaaaa,\n" 7970 "};"); 7971 7972 // Don't create hanging lists. 7973 verifyFormat("someFunction(Param, {List1, List2,\n" 7974 " List3});", 7975 getLLVMStyleWithColumns(35)); 7976 verifyFormat("someFunction(Param, Param,\n" 7977 " {List1, List2,\n" 7978 " List3});", 7979 getLLVMStyleWithColumns(35)); 7980 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 7981 " aaaaaaaaaaaaaaaaaaaaaaa);"); 7982 } 7983 7984 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 7985 FormatStyle DoNotMerge = getLLVMStyle(); 7986 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7987 7988 verifyFormat("void f() { return 42; }"); 7989 verifyFormat("void f() {\n" 7990 " return 42;\n" 7991 "}", 7992 DoNotMerge); 7993 verifyFormat("void f() {\n" 7994 " // Comment\n" 7995 "}"); 7996 verifyFormat("{\n" 7997 "#error {\n" 7998 " int a;\n" 7999 "}"); 8000 verifyFormat("{\n" 8001 " int a;\n" 8002 "#error {\n" 8003 "}"); 8004 verifyFormat("void f() {} // comment"); 8005 verifyFormat("void f() { int a; } // comment"); 8006 verifyFormat("void f() {\n" 8007 "} // comment", 8008 DoNotMerge); 8009 verifyFormat("void f() {\n" 8010 " int a;\n" 8011 "} // comment", 8012 DoNotMerge); 8013 verifyFormat("void f() {\n" 8014 "} // comment", 8015 getLLVMStyleWithColumns(15)); 8016 8017 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 8018 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 8019 8020 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 8021 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 8022 verifyFormat("class C {\n" 8023 " C()\n" 8024 " : iiiiiiii(nullptr),\n" 8025 " kkkkkkk(nullptr),\n" 8026 " mmmmmmm(nullptr),\n" 8027 " nnnnnnn(nullptr) {}\n" 8028 "};", 8029 getGoogleStyle()); 8030 8031 FormatStyle NoColumnLimit = getLLVMStyle(); 8032 NoColumnLimit.ColumnLimit = 0; 8033 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 8034 EXPECT_EQ("class C {\n" 8035 " A() : b(0) {}\n" 8036 "};", 8037 format("class C{A():b(0){}};", NoColumnLimit)); 8038 EXPECT_EQ("A()\n" 8039 " : b(0) {\n" 8040 "}", 8041 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 8042 8043 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 8044 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 8045 FormatStyle::SFS_None; 8046 EXPECT_EQ("A()\n" 8047 " : b(0) {\n" 8048 "}", 8049 format("A():b(0){}", DoNotMergeNoColumnLimit)); 8050 EXPECT_EQ("A()\n" 8051 " : b(0) {\n" 8052 "}", 8053 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 8054 8055 verifyFormat("#define A \\\n" 8056 " void f() { \\\n" 8057 " int i; \\\n" 8058 " }", 8059 getLLVMStyleWithColumns(20)); 8060 verifyFormat("#define A \\\n" 8061 " void f() { int i; }", 8062 getLLVMStyleWithColumns(21)); 8063 verifyFormat("#define A \\\n" 8064 " void f() { \\\n" 8065 " int i; \\\n" 8066 " } \\\n" 8067 " int j;", 8068 getLLVMStyleWithColumns(22)); 8069 verifyFormat("#define A \\\n" 8070 " void f() { int i; } \\\n" 8071 " int j;", 8072 getLLVMStyleWithColumns(23)); 8073 } 8074 8075 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 8076 FormatStyle MergeEmptyOnly = getLLVMStyle(); 8077 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 8078 verifyFormat("class C {\n" 8079 " int f() {}\n" 8080 "};", 8081 MergeEmptyOnly); 8082 verifyFormat("class C {\n" 8083 " int f() {\n" 8084 " return 42;\n" 8085 " }\n" 8086 "};", 8087 MergeEmptyOnly); 8088 verifyFormat("int f() {}", MergeEmptyOnly); 8089 verifyFormat("int f() {\n" 8090 " return 42;\n" 8091 "}", 8092 MergeEmptyOnly); 8093 8094 // Also verify behavior when BraceWrapping.AfterFunction = true 8095 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 8096 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 8097 verifyFormat("int f() {}", MergeEmptyOnly); 8098 verifyFormat("class C {\n" 8099 " int f() {}\n" 8100 "};", 8101 MergeEmptyOnly); 8102 } 8103 8104 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 8105 FormatStyle MergeInlineOnly = getLLVMStyle(); 8106 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 8107 verifyFormat("class C {\n" 8108 " int f() { return 42; }\n" 8109 "};", 8110 MergeInlineOnly); 8111 verifyFormat("int f() {\n" 8112 " return 42;\n" 8113 "}", 8114 MergeInlineOnly); 8115 8116 // SFS_Inline implies SFS_Empty 8117 verifyFormat("class C {\n" 8118 " int f() {}\n" 8119 "};", 8120 MergeInlineOnly); 8121 verifyFormat("int f() {}", MergeInlineOnly); 8122 8123 // Also verify behavior when BraceWrapping.AfterFunction = true 8124 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 8125 MergeInlineOnly.BraceWrapping.AfterFunction = true; 8126 verifyFormat("class C {\n" 8127 " int f() { return 42; }\n" 8128 "};", 8129 MergeInlineOnly); 8130 verifyFormat("int f()\n" 8131 "{\n" 8132 " return 42;\n" 8133 "}", 8134 MergeInlineOnly); 8135 8136 // SFS_Inline implies SFS_Empty 8137 verifyFormat("int f() {}", MergeInlineOnly); 8138 verifyFormat("class C {\n" 8139 " int f() {}\n" 8140 "};", 8141 MergeInlineOnly); 8142 } 8143 8144 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 8145 FormatStyle MergeInlineOnly = getLLVMStyle(); 8146 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 8147 FormatStyle::SFS_InlineOnly; 8148 verifyFormat("class C {\n" 8149 " int f() { return 42; }\n" 8150 "};", 8151 MergeInlineOnly); 8152 verifyFormat("int f() {\n" 8153 " return 42;\n" 8154 "}", 8155 MergeInlineOnly); 8156 8157 // SFS_InlineOnly does not imply SFS_Empty 8158 verifyFormat("class C {\n" 8159 " int f() {}\n" 8160 "};", 8161 MergeInlineOnly); 8162 verifyFormat("int f() {\n" 8163 "}", 8164 MergeInlineOnly); 8165 8166 // Also verify behavior when BraceWrapping.AfterFunction = true 8167 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 8168 MergeInlineOnly.BraceWrapping.AfterFunction = true; 8169 verifyFormat("class C {\n" 8170 " int f() { return 42; }\n" 8171 "};", 8172 MergeInlineOnly); 8173 verifyFormat("int f()\n" 8174 "{\n" 8175 " return 42;\n" 8176 "}", 8177 MergeInlineOnly); 8178 8179 // SFS_InlineOnly does not imply SFS_Empty 8180 verifyFormat("int f()\n" 8181 "{\n" 8182 "}", 8183 MergeInlineOnly); 8184 verifyFormat("class C {\n" 8185 " int f() {}\n" 8186 "};", 8187 MergeInlineOnly); 8188 } 8189 8190 TEST_F(FormatTest, SplitEmptyFunction) { 8191 FormatStyle Style = getLLVMStyle(); 8192 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 8193 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8194 Style.BraceWrapping.AfterFunction = true; 8195 Style.BraceWrapping.SplitEmptyFunction = false; 8196 Style.ColumnLimit = 40; 8197 8198 verifyFormat("int f()\n" 8199 "{}", 8200 Style); 8201 verifyFormat("int f()\n" 8202 "{\n" 8203 " return 42;\n" 8204 "}", 8205 Style); 8206 verifyFormat("int f()\n" 8207 "{\n" 8208 " // some comment\n" 8209 "}", 8210 Style); 8211 8212 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 8213 verifyFormat("int f() {}", Style); 8214 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 8215 "{}", 8216 Style); 8217 verifyFormat("int f()\n" 8218 "{\n" 8219 " return 0;\n" 8220 "}", 8221 Style); 8222 8223 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 8224 verifyFormat("class Foo {\n" 8225 " int f() {}\n" 8226 "};\n", 8227 Style); 8228 verifyFormat("class Foo {\n" 8229 " int f() { return 0; }\n" 8230 "};\n", 8231 Style); 8232 verifyFormat("class Foo {\n" 8233 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 8234 " {}\n" 8235 "};\n", 8236 Style); 8237 verifyFormat("class Foo {\n" 8238 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 8239 " {\n" 8240 " return 0;\n" 8241 " }\n" 8242 "};\n", 8243 Style); 8244 8245 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 8246 verifyFormat("int f() {}", Style); 8247 verifyFormat("int f() { return 0; }", Style); 8248 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 8249 "{}", 8250 Style); 8251 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 8252 "{\n" 8253 " return 0;\n" 8254 "}", 8255 Style); 8256 } 8257 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 8258 FormatStyle Style = getLLVMStyle(); 8259 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 8260 verifyFormat("#ifdef A\n" 8261 "int f() {}\n" 8262 "#else\n" 8263 "int g() {}\n" 8264 "#endif", 8265 Style); 8266 } 8267 8268 TEST_F(FormatTest, SplitEmptyClass) { 8269 FormatStyle Style = getLLVMStyle(); 8270 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8271 Style.BraceWrapping.AfterClass = true; 8272 Style.BraceWrapping.SplitEmptyRecord = false; 8273 8274 verifyFormat("class Foo\n" 8275 "{};", 8276 Style); 8277 verifyFormat("/* something */ class Foo\n" 8278 "{};", 8279 Style); 8280 verifyFormat("template <typename X> class Foo\n" 8281 "{};", 8282 Style); 8283 verifyFormat("class Foo\n" 8284 "{\n" 8285 " Foo();\n" 8286 "};", 8287 Style); 8288 verifyFormat("typedef class Foo\n" 8289 "{\n" 8290 "} Foo_t;", 8291 Style); 8292 } 8293 8294 TEST_F(FormatTest, SplitEmptyStruct) { 8295 FormatStyle Style = getLLVMStyle(); 8296 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8297 Style.BraceWrapping.AfterStruct = true; 8298 Style.BraceWrapping.SplitEmptyRecord = false; 8299 8300 verifyFormat("struct Foo\n" 8301 "{};", 8302 Style); 8303 verifyFormat("/* something */ struct Foo\n" 8304 "{};", 8305 Style); 8306 verifyFormat("template <typename X> struct Foo\n" 8307 "{};", 8308 Style); 8309 verifyFormat("struct Foo\n" 8310 "{\n" 8311 " Foo();\n" 8312 "};", 8313 Style); 8314 verifyFormat("typedef struct Foo\n" 8315 "{\n" 8316 "} Foo_t;", 8317 Style); 8318 //typedef struct Bar {} Bar_t; 8319 } 8320 8321 TEST_F(FormatTest, SplitEmptyUnion) { 8322 FormatStyle Style = getLLVMStyle(); 8323 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8324 Style.BraceWrapping.AfterUnion = true; 8325 Style.BraceWrapping.SplitEmptyRecord = false; 8326 8327 verifyFormat("union Foo\n" 8328 "{};", 8329 Style); 8330 verifyFormat("/* something */ union Foo\n" 8331 "{};", 8332 Style); 8333 verifyFormat("union Foo\n" 8334 "{\n" 8335 " A,\n" 8336 "};", 8337 Style); 8338 verifyFormat("typedef union Foo\n" 8339 "{\n" 8340 "} Foo_t;", 8341 Style); 8342 } 8343 8344 TEST_F(FormatTest, SplitEmptyNamespace) { 8345 FormatStyle Style = getLLVMStyle(); 8346 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8347 Style.BraceWrapping.AfterNamespace = true; 8348 Style.BraceWrapping.SplitEmptyNamespace = false; 8349 8350 verifyFormat("namespace Foo\n" 8351 "{};", 8352 Style); 8353 verifyFormat("/* something */ namespace Foo\n" 8354 "{};", 8355 Style); 8356 verifyFormat("inline namespace Foo\n" 8357 "{};", 8358 Style); 8359 verifyFormat("/* something */ inline namespace Foo\n" 8360 "{};", 8361 Style); 8362 verifyFormat("export namespace Foo\n" 8363 "{};", 8364 Style); 8365 verifyFormat("namespace Foo\n" 8366 "{\n" 8367 "void Bar();\n" 8368 "};", 8369 Style); 8370 } 8371 8372 TEST_F(FormatTest, NeverMergeShortRecords) { 8373 FormatStyle Style = getLLVMStyle(); 8374 8375 verifyFormat("class Foo {\n" 8376 " Foo();\n" 8377 "};", 8378 Style); 8379 verifyFormat("typedef class Foo {\n" 8380 " Foo();\n" 8381 "} Foo_t;", 8382 Style); 8383 verifyFormat("struct Foo {\n" 8384 " Foo();\n" 8385 "};", 8386 Style); 8387 verifyFormat("typedef struct Foo {\n" 8388 " Foo();\n" 8389 "} Foo_t;", 8390 Style); 8391 verifyFormat("union Foo {\n" 8392 " A,\n" 8393 "};", 8394 Style); 8395 verifyFormat("typedef union Foo {\n" 8396 " A,\n" 8397 "} Foo_t;", 8398 Style); 8399 verifyFormat("namespace Foo {\n" 8400 "void Bar();\n" 8401 "};", 8402 Style); 8403 8404 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8405 Style.BraceWrapping.AfterClass = true; 8406 Style.BraceWrapping.AfterStruct = true; 8407 Style.BraceWrapping.AfterUnion = true; 8408 Style.BraceWrapping.AfterNamespace = true; 8409 verifyFormat("class Foo\n" 8410 "{\n" 8411 " Foo();\n" 8412 "};", 8413 Style); 8414 verifyFormat("typedef class Foo\n" 8415 "{\n" 8416 " Foo();\n" 8417 "} Foo_t;", 8418 Style); 8419 verifyFormat("struct Foo\n" 8420 "{\n" 8421 " Foo();\n" 8422 "};", 8423 Style); 8424 verifyFormat("typedef struct Foo\n" 8425 "{\n" 8426 " Foo();\n" 8427 "} Foo_t;", 8428 Style); 8429 verifyFormat("union Foo\n" 8430 "{\n" 8431 " A,\n" 8432 "};", 8433 Style); 8434 verifyFormat("typedef union Foo\n" 8435 "{\n" 8436 " A,\n" 8437 "} Foo_t;", 8438 Style); 8439 verifyFormat("namespace Foo\n" 8440 "{\n" 8441 "void Bar();\n" 8442 "};", 8443 Style); 8444 } 8445 8446 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 8447 // Elaborate type variable declarations. 8448 verifyFormat("struct foo a = {bar};\nint n;"); 8449 verifyFormat("class foo a = {bar};\nint n;"); 8450 verifyFormat("union foo a = {bar};\nint n;"); 8451 8452 // Elaborate types inside function definitions. 8453 verifyFormat("struct foo f() {}\nint n;"); 8454 verifyFormat("class foo f() {}\nint n;"); 8455 verifyFormat("union foo f() {}\nint n;"); 8456 8457 // Templates. 8458 verifyFormat("template <class X> void f() {}\nint n;"); 8459 verifyFormat("template <struct X> void f() {}\nint n;"); 8460 verifyFormat("template <union X> void f() {}\nint n;"); 8461 8462 // Actual definitions... 8463 verifyFormat("struct {\n} n;"); 8464 verifyFormat( 8465 "template <template <class T, class Y>, class Z> class X {\n} n;"); 8466 verifyFormat("union Z {\n int n;\n} x;"); 8467 verifyFormat("class MACRO Z {\n} n;"); 8468 verifyFormat("class MACRO(X) Z {\n} n;"); 8469 verifyFormat("class __attribute__(X) Z {\n} n;"); 8470 verifyFormat("class __declspec(X) Z {\n} n;"); 8471 verifyFormat("class A##B##C {\n} n;"); 8472 verifyFormat("class alignas(16) Z {\n} n;"); 8473 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 8474 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 8475 8476 // Redefinition from nested context: 8477 verifyFormat("class A::B::C {\n} n;"); 8478 8479 // Template definitions. 8480 verifyFormat( 8481 "template <typename F>\n" 8482 "Matcher(const Matcher<F> &Other,\n" 8483 " typename enable_if_c<is_base_of<F, T>::value &&\n" 8484 " !is_same<F, T>::value>::type * = 0)\n" 8485 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 8486 8487 // FIXME: This is still incorrectly handled at the formatter side. 8488 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 8489 verifyFormat("int i = SomeFunction(a<b, a> b);"); 8490 8491 // FIXME: 8492 // This now gets parsed incorrectly as class definition. 8493 // verifyFormat("class A<int> f() {\n}\nint n;"); 8494 8495 // Elaborate types where incorrectly parsing the structural element would 8496 // break the indent. 8497 verifyFormat("if (true)\n" 8498 " class X x;\n" 8499 "else\n" 8500 " f();\n"); 8501 8502 // This is simply incomplete. Formatting is not important, but must not crash. 8503 verifyFormat("class A:"); 8504 } 8505 8506 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 8507 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 8508 format("#error Leave all white!!!!! space* alone!\n")); 8509 EXPECT_EQ( 8510 "#warning Leave all white!!!!! space* alone!\n", 8511 format("#warning Leave all white!!!!! space* alone!\n")); 8512 EXPECT_EQ("#error 1", format(" # error 1")); 8513 EXPECT_EQ("#warning 1", format(" # warning 1")); 8514 } 8515 8516 TEST_F(FormatTest, FormatHashIfExpressions) { 8517 verifyFormat("#if AAAA && BBBB"); 8518 verifyFormat("#if (AAAA && BBBB)"); 8519 verifyFormat("#elif (AAAA && BBBB)"); 8520 // FIXME: Come up with a better indentation for #elif. 8521 verifyFormat( 8522 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 8523 " defined(BBBBBBBB)\n" 8524 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 8525 " defined(BBBBBBBB)\n" 8526 "#endif", 8527 getLLVMStyleWithColumns(65)); 8528 } 8529 8530 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 8531 FormatStyle AllowsMergedIf = getGoogleStyle(); 8532 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 8533 FormatStyle::SIS_WithoutElse; 8534 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 8535 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 8536 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 8537 EXPECT_EQ("if (true) return 42;", 8538 format("if (true)\nreturn 42;", AllowsMergedIf)); 8539 FormatStyle ShortMergedIf = AllowsMergedIf; 8540 ShortMergedIf.ColumnLimit = 25; 8541 verifyFormat("#define A \\\n" 8542 " if (true) return 42;", 8543 ShortMergedIf); 8544 verifyFormat("#define A \\\n" 8545 " f(); \\\n" 8546 " if (true)\n" 8547 "#define B", 8548 ShortMergedIf); 8549 verifyFormat("#define A \\\n" 8550 " f(); \\\n" 8551 " if (true)\n" 8552 "g();", 8553 ShortMergedIf); 8554 verifyFormat("{\n" 8555 "#ifdef A\n" 8556 " // Comment\n" 8557 " if (true) continue;\n" 8558 "#endif\n" 8559 " // Comment\n" 8560 " if (true) continue;\n" 8561 "}", 8562 ShortMergedIf); 8563 ShortMergedIf.ColumnLimit = 33; 8564 verifyFormat("#define A \\\n" 8565 " if constexpr (true) return 42;", 8566 ShortMergedIf); 8567 ShortMergedIf.ColumnLimit = 29; 8568 verifyFormat("#define A \\\n" 8569 " if (aaaaaaaaaa) return 1; \\\n" 8570 " return 2;", 8571 ShortMergedIf); 8572 ShortMergedIf.ColumnLimit = 28; 8573 verifyFormat("#define A \\\n" 8574 " if (aaaaaaaaaa) \\\n" 8575 " return 1; \\\n" 8576 " return 2;", 8577 ShortMergedIf); 8578 verifyFormat("#define A \\\n" 8579 " if constexpr (aaaaaaa) \\\n" 8580 " return 1; \\\n" 8581 " return 2;", 8582 ShortMergedIf); 8583 } 8584 8585 TEST_F(FormatTest, FormatStarDependingOnContext) { 8586 verifyFormat("void f(int *a);"); 8587 verifyFormat("void f() { f(fint * b); }"); 8588 verifyFormat("class A {\n void f(int *a);\n};"); 8589 verifyFormat("class A {\n int *a;\n};"); 8590 verifyFormat("namespace a {\n" 8591 "namespace b {\n" 8592 "class A {\n" 8593 " void f() {}\n" 8594 " int *a;\n" 8595 "};\n" 8596 "} // namespace b\n" 8597 "} // namespace a"); 8598 } 8599 8600 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 8601 verifyFormat("while"); 8602 verifyFormat("operator"); 8603 } 8604 8605 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 8606 // This code would be painfully slow to format if we didn't skip it. 8607 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 8608 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8609 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8610 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8611 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8612 "A(1, 1)\n" 8613 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 8614 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8615 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8616 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8617 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8618 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8619 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8620 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8621 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8622 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 8623 // Deeply nested part is untouched, rest is formatted. 8624 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 8625 format(std::string("int i;\n") + Code + "int j;\n", 8626 getLLVMStyle(), SC_ExpectIncomplete)); 8627 } 8628 8629 //===----------------------------------------------------------------------===// 8630 // Objective-C tests. 8631 //===----------------------------------------------------------------------===// 8632 8633 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 8634 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 8635 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 8636 format("-(NSUInteger)indexOfObject:(id)anObject;")); 8637 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 8638 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 8639 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 8640 format("-(NSInteger)Method3:(id)anObject;")); 8641 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 8642 format("-(NSInteger)Method4:(id)anObject;")); 8643 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 8644 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 8645 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 8646 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 8647 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 8648 "forAllCells:(BOOL)flag;", 8649 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 8650 "forAllCells:(BOOL)flag;")); 8651 8652 // Very long objectiveC method declaration. 8653 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 8654 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 8655 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 8656 " inRange:(NSRange)range\n" 8657 " outRange:(NSRange)out_range\n" 8658 " outRange1:(NSRange)out_range1\n" 8659 " outRange2:(NSRange)out_range2\n" 8660 " outRange3:(NSRange)out_range3\n" 8661 " outRange4:(NSRange)out_range4\n" 8662 " outRange5:(NSRange)out_range5\n" 8663 " outRange6:(NSRange)out_range6\n" 8664 " outRange7:(NSRange)out_range7\n" 8665 " outRange8:(NSRange)out_range8\n" 8666 " outRange9:(NSRange)out_range9;"); 8667 8668 // When the function name has to be wrapped. 8669 FormatStyle Style = getLLVMStyle(); 8670 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 8671 // and always indents instead. 8672 Style.IndentWrappedFunctionNames = false; 8673 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 8674 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 8675 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 8676 "}", 8677 Style); 8678 Style.IndentWrappedFunctionNames = true; 8679 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 8680 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 8681 " anotherName:(NSString)dddddddddddddd {\n" 8682 "}", 8683 Style); 8684 8685 verifyFormat("- (int)sum:(vector<int>)numbers;"); 8686 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 8687 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 8688 // protocol lists (but not for template classes): 8689 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 8690 8691 verifyFormat("- (int (*)())foo:(int (*)())f;"); 8692 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 8693 8694 // If there's no return type (very rare in practice!), LLVM and Google style 8695 // agree. 8696 verifyFormat("- foo;"); 8697 verifyFormat("- foo:(int)f;"); 8698 verifyGoogleFormat("- foo:(int)foo;"); 8699 } 8700 8701 8702 TEST_F(FormatTest, BreaksStringLiterals) { 8703 EXPECT_EQ("\"some text \"\n" 8704 "\"other\";", 8705 format("\"some text other\";", getLLVMStyleWithColumns(12))); 8706 EXPECT_EQ("\"some text \"\n" 8707 "\"other\";", 8708 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 8709 EXPECT_EQ( 8710 "#define A \\\n" 8711 " \"some \" \\\n" 8712 " \"text \" \\\n" 8713 " \"other\";", 8714 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 8715 EXPECT_EQ( 8716 "#define A \\\n" 8717 " \"so \" \\\n" 8718 " \"text \" \\\n" 8719 " \"other\";", 8720 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 8721 8722 EXPECT_EQ("\"some text\"", 8723 format("\"some text\"", getLLVMStyleWithColumns(1))); 8724 EXPECT_EQ("\"some text\"", 8725 format("\"some text\"", getLLVMStyleWithColumns(11))); 8726 EXPECT_EQ("\"some \"\n" 8727 "\"text\"", 8728 format("\"some text\"", getLLVMStyleWithColumns(10))); 8729 EXPECT_EQ("\"some \"\n" 8730 "\"text\"", 8731 format("\"some text\"", getLLVMStyleWithColumns(7))); 8732 EXPECT_EQ("\"some\"\n" 8733 "\" tex\"\n" 8734 "\"t\"", 8735 format("\"some text\"", getLLVMStyleWithColumns(6))); 8736 EXPECT_EQ("\"some\"\n" 8737 "\" tex\"\n" 8738 "\" and\"", 8739 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 8740 EXPECT_EQ("\"some\"\n" 8741 "\"/tex\"\n" 8742 "\"/and\"", 8743 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 8744 8745 EXPECT_EQ("variable =\n" 8746 " \"long string \"\n" 8747 " \"literal\";", 8748 format("variable = \"long string literal\";", 8749 getLLVMStyleWithColumns(20))); 8750 8751 EXPECT_EQ("variable = f(\n" 8752 " \"long string \"\n" 8753 " \"literal\",\n" 8754 " short,\n" 8755 " loooooooooooooooooooong);", 8756 format("variable = f(\"long string literal\", short, " 8757 "loooooooooooooooooooong);", 8758 getLLVMStyleWithColumns(20))); 8759 8760 EXPECT_EQ( 8761 "f(g(\"long string \"\n" 8762 " \"literal\"),\n" 8763 " b);", 8764 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 8765 EXPECT_EQ("f(g(\"long string \"\n" 8766 " \"literal\",\n" 8767 " a),\n" 8768 " b);", 8769 format("f(g(\"long string literal\", a), b);", 8770 getLLVMStyleWithColumns(20))); 8771 EXPECT_EQ( 8772 "f(\"one two\".split(\n" 8773 " variable));", 8774 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 8775 EXPECT_EQ("f(\"one two three four five six \"\n" 8776 " \"seven\".split(\n" 8777 " really_looooong_variable));", 8778 format("f(\"one two three four five six seven\"." 8779 "split(really_looooong_variable));", 8780 getLLVMStyleWithColumns(33))); 8781 8782 EXPECT_EQ("f(\"some \"\n" 8783 " \"text\",\n" 8784 " other);", 8785 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 8786 8787 // Only break as a last resort. 8788 verifyFormat( 8789 "aaaaaaaaaaaaaaaaaaaa(\n" 8790 " aaaaaaaaaaaaaaaaaaaa,\n" 8791 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 8792 8793 EXPECT_EQ("\"splitmea\"\n" 8794 "\"trandomp\"\n" 8795 "\"oint\"", 8796 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 8797 8798 EXPECT_EQ("\"split/\"\n" 8799 "\"pathat/\"\n" 8800 "\"slashes\"", 8801 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8802 8803 EXPECT_EQ("\"split/\"\n" 8804 "\"pathat/\"\n" 8805 "\"slashes\"", 8806 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8807 EXPECT_EQ("\"split at \"\n" 8808 "\"spaces/at/\"\n" 8809 "\"slashes.at.any$\"\n" 8810 "\"non-alphanumeric%\"\n" 8811 "\"1111111111characte\"\n" 8812 "\"rs\"", 8813 format("\"split at " 8814 "spaces/at/" 8815 "slashes.at." 8816 "any$non-" 8817 "alphanumeric%" 8818 "1111111111characte" 8819 "rs\"", 8820 getLLVMStyleWithColumns(20))); 8821 8822 // Verify that splitting the strings understands 8823 // Style::AlwaysBreakBeforeMultilineStrings. 8824 EXPECT_EQ( 8825 "aaaaaaaaaaaa(\n" 8826 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 8827 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 8828 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 8829 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8830 "aaaaaaaaaaaaaaaaaaaaaa\");", 8831 getGoogleStyle())); 8832 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8833 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 8834 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 8835 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8836 "aaaaaaaaaaaaaaaaaaaaaa\";", 8837 getGoogleStyle())); 8838 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8839 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8840 format("llvm::outs() << " 8841 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 8842 "aaaaaaaaaaaaaaaaaaa\";")); 8843 EXPECT_EQ("ffff(\n" 8844 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8845 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8846 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8847 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8848 getGoogleStyle())); 8849 8850 FormatStyle Style = getLLVMStyleWithColumns(12); 8851 Style.BreakStringLiterals = false; 8852 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 8853 8854 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 8855 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8856 EXPECT_EQ("#define A \\\n" 8857 " \"some \" \\\n" 8858 " \"text \" \\\n" 8859 " \"other\";", 8860 format("#define A \"some text other\";", AlignLeft)); 8861 } 8862 8863 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 8864 EXPECT_EQ("C a = \"some more \"\n" 8865 " \"text\";", 8866 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 8867 } 8868 8869 TEST_F(FormatTest, FullyRemoveEmptyLines) { 8870 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 8871 NoEmptyLines.MaxEmptyLinesToKeep = 0; 8872 EXPECT_EQ("int i = a(b());", 8873 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 8874 } 8875 8876 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 8877 EXPECT_EQ( 8878 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8879 "(\n" 8880 " \"x\t\");", 8881 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8882 "aaaaaaa(" 8883 "\"x\t\");")); 8884 } 8885 8886 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 8887 EXPECT_EQ( 8888 "u8\"utf8 string \"\n" 8889 "u8\"literal\";", 8890 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 8891 EXPECT_EQ( 8892 "u\"utf16 string \"\n" 8893 "u\"literal\";", 8894 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 8895 EXPECT_EQ( 8896 "U\"utf32 string \"\n" 8897 "U\"literal\";", 8898 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 8899 EXPECT_EQ("L\"wide string \"\n" 8900 "L\"literal\";", 8901 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 8902 EXPECT_EQ("@\"NSString \"\n" 8903 "@\"literal\";", 8904 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 8905 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 8906 8907 // This input makes clang-format try to split the incomplete unicode escape 8908 // sequence, which used to lead to a crasher. 8909 verifyNoCrash( 8910 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 8911 getLLVMStyleWithColumns(60)); 8912 } 8913 8914 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 8915 FormatStyle Style = getGoogleStyleWithColumns(15); 8916 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 8917 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 8918 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 8919 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 8920 EXPECT_EQ("u8R\"x(raw literal)x\";", 8921 format("u8R\"x(raw literal)x\";", Style)); 8922 } 8923 8924 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 8925 FormatStyle Style = getLLVMStyleWithColumns(20); 8926 EXPECT_EQ( 8927 "_T(\"aaaaaaaaaaaaaa\")\n" 8928 "_T(\"aaaaaaaaaaaaaa\")\n" 8929 "_T(\"aaaaaaaaaaaa\")", 8930 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 8931 EXPECT_EQ("f(x,\n" 8932 " _T(\"aaaaaaaaaaaa\")\n" 8933 " _T(\"aaa\"),\n" 8934 " z);", 8935 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 8936 8937 // FIXME: Handle embedded spaces in one iteration. 8938 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 8939 // "_T(\"aaaaaaaaaaaaa\")\n" 8940 // "_T(\"aaaaaaaaaaaaa\")\n" 8941 // "_T(\"a\")", 8942 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8943 // getLLVMStyleWithColumns(20))); 8944 EXPECT_EQ( 8945 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8946 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 8947 EXPECT_EQ("f(\n" 8948 "#if !TEST\n" 8949 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8950 "#endif\n" 8951 ");", 8952 format("f(\n" 8953 "#if !TEST\n" 8954 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8955 "#endif\n" 8956 ");")); 8957 EXPECT_EQ("f(\n" 8958 "\n" 8959 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 8960 format("f(\n" 8961 "\n" 8962 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 8963 } 8964 8965 TEST_F(FormatTest, BreaksStringLiteralOperands) { 8966 // In a function call with two operands, the second can be broken with no line 8967 // break before it. 8968 EXPECT_EQ("func(a, \"long long \"\n" 8969 " \"long long\");", 8970 format("func(a, \"long long long long\");", 8971 getLLVMStyleWithColumns(24))); 8972 // In a function call with three operands, the second must be broken with a 8973 // line break before it. 8974 EXPECT_EQ("func(a,\n" 8975 " \"long long long \"\n" 8976 " \"long\",\n" 8977 " c);", 8978 format("func(a, \"long long long long\", c);", 8979 getLLVMStyleWithColumns(24))); 8980 // In a function call with three operands, the third must be broken with a 8981 // line break before it. 8982 EXPECT_EQ("func(a, b,\n" 8983 " \"long long long \"\n" 8984 " \"long\");", 8985 format("func(a, b, \"long long long long\");", 8986 getLLVMStyleWithColumns(24))); 8987 // In a function call with three operands, both the second and the third must 8988 // be broken with a line break before them. 8989 EXPECT_EQ("func(a,\n" 8990 " \"long long long \"\n" 8991 " \"long\",\n" 8992 " \"long long long \"\n" 8993 " \"long\");", 8994 format("func(a, \"long long long long\", \"long long long long\");", 8995 getLLVMStyleWithColumns(24))); 8996 // In a chain of << with two operands, the second can be broken with no line 8997 // break before it. 8998 EXPECT_EQ("a << \"line line \"\n" 8999 " \"line\";", 9000 format("a << \"line line line\";", 9001 getLLVMStyleWithColumns(20))); 9002 // In a chain of << with three operands, the second can be broken with no line 9003 // break before it. 9004 EXPECT_EQ("abcde << \"line \"\n" 9005 " \"line line\"\n" 9006 " << c;", 9007 format("abcde << \"line line line\" << c;", 9008 getLLVMStyleWithColumns(20))); 9009 // In a chain of << with three operands, the third must be broken with a line 9010 // break before it. 9011 EXPECT_EQ("a << b\n" 9012 " << \"line line \"\n" 9013 " \"line\";", 9014 format("a << b << \"line line line\";", 9015 getLLVMStyleWithColumns(20))); 9016 // In a chain of << with three operands, the second can be broken with no line 9017 // break before it and the third must be broken with a line break before it. 9018 EXPECT_EQ("abcd << \"line line \"\n" 9019 " \"line\"\n" 9020 " << \"line line \"\n" 9021 " \"line\";", 9022 format("abcd << \"line line line\" << \"line line line\";", 9023 getLLVMStyleWithColumns(20))); 9024 // In a chain of binary operators with two operands, the second can be broken 9025 // with no line break before it. 9026 EXPECT_EQ("abcd + \"line line \"\n" 9027 " \"line line\";", 9028 format("abcd + \"line line line line\";", 9029 getLLVMStyleWithColumns(20))); 9030 // In a chain of binary operators with three operands, the second must be 9031 // broken with a line break before it. 9032 EXPECT_EQ("abcd +\n" 9033 " \"line line \"\n" 9034 " \"line line\" +\n" 9035 " e;", 9036 format("abcd + \"line line line line\" + e;", 9037 getLLVMStyleWithColumns(20))); 9038 // In a function call with two operands, with AlignAfterOpenBracket enabled, 9039 // the first must be broken with a line break before it. 9040 FormatStyle Style = getLLVMStyleWithColumns(25); 9041 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9042 EXPECT_EQ("someFunction(\n" 9043 " \"long long long \"\n" 9044 " \"long\",\n" 9045 " a);", 9046 format("someFunction(\"long long long long\", a);", Style)); 9047 } 9048 9049 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 9050 EXPECT_EQ( 9051 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 9052 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 9053 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 9054 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 9055 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 9056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 9057 } 9058 9059 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 9060 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 9061 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 9062 EXPECT_EQ("fffffffffff(g(R\"x(\n" 9063 "multiline raw string literal xxxxxxxxxxxxxx\n" 9064 ")x\",\n" 9065 " a),\n" 9066 " b);", 9067 format("fffffffffff(g(R\"x(\n" 9068 "multiline raw string literal xxxxxxxxxxxxxx\n" 9069 ")x\", a), b);", 9070 getGoogleStyleWithColumns(20))); 9071 EXPECT_EQ("fffffffffff(\n" 9072 " g(R\"x(qqq\n" 9073 "multiline raw string literal xxxxxxxxxxxxxx\n" 9074 ")x\",\n" 9075 " a),\n" 9076 " b);", 9077 format("fffffffffff(g(R\"x(qqq\n" 9078 "multiline raw string literal xxxxxxxxxxxxxx\n" 9079 ")x\", a), b);", 9080 getGoogleStyleWithColumns(20))); 9081 9082 EXPECT_EQ("fffffffffff(R\"x(\n" 9083 "multiline raw string literal xxxxxxxxxxxxxx\n" 9084 ")x\");", 9085 format("fffffffffff(R\"x(\n" 9086 "multiline raw string literal xxxxxxxxxxxxxx\n" 9087 ")x\");", 9088 getGoogleStyleWithColumns(20))); 9089 EXPECT_EQ("fffffffffff(R\"x(\n" 9090 "multiline raw string literal xxxxxxxxxxxxxx\n" 9091 ")x\" + bbbbbb);", 9092 format("fffffffffff(R\"x(\n" 9093 "multiline raw string literal xxxxxxxxxxxxxx\n" 9094 ")x\" + bbbbbb);", 9095 getGoogleStyleWithColumns(20))); 9096 EXPECT_EQ("fffffffffff(\n" 9097 " R\"x(\n" 9098 "multiline raw string literal xxxxxxxxxxxxxx\n" 9099 ")x\" +\n" 9100 " bbbbbb);", 9101 format("fffffffffff(\n" 9102 " R\"x(\n" 9103 "multiline raw string literal xxxxxxxxxxxxxx\n" 9104 ")x\" + bbbbbb);", 9105 getGoogleStyleWithColumns(20))); 9106 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 9107 format("fffffffffff(\n" 9108 " R\"(single line raw string)\" + bbbbbb);")); 9109 } 9110 9111 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 9112 verifyFormat("string a = \"unterminated;"); 9113 EXPECT_EQ("function(\"unterminated,\n" 9114 " OtherParameter);", 9115 format("function( \"unterminated,\n" 9116 " OtherParameter);")); 9117 } 9118 9119 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 9120 FormatStyle Style = getLLVMStyle(); 9121 Style.Standard = FormatStyle::LS_Cpp03; 9122 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 9123 format("#define x(_a) printf(\"foo\"_a);", Style)); 9124 } 9125 9126 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 9127 9128 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 9129 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 9130 " \"ddeeefff\");", 9131 format("someFunction(\"aaabbbcccdddeeefff\");", 9132 getLLVMStyleWithColumns(25))); 9133 EXPECT_EQ("someFunction1234567890(\n" 9134 " \"aaabbbcccdddeeefff\");", 9135 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 9136 getLLVMStyleWithColumns(26))); 9137 EXPECT_EQ("someFunction1234567890(\n" 9138 " \"aaabbbcccdddeeeff\"\n" 9139 " \"f\");", 9140 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 9141 getLLVMStyleWithColumns(25))); 9142 EXPECT_EQ("someFunction1234567890(\n" 9143 " \"aaabbbcccdddeeeff\"\n" 9144 " \"f\");", 9145 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 9146 getLLVMStyleWithColumns(24))); 9147 EXPECT_EQ("someFunction(\n" 9148 " \"aaabbbcc ddde \"\n" 9149 " \"efff\");", 9150 format("someFunction(\"aaabbbcc ddde efff\");", 9151 getLLVMStyleWithColumns(25))); 9152 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 9153 " \"ddeeefff\");", 9154 format("someFunction(\"aaabbbccc ddeeefff\");", 9155 getLLVMStyleWithColumns(25))); 9156 EXPECT_EQ("someFunction1234567890(\n" 9157 " \"aaabb \"\n" 9158 " \"cccdddeeefff\");", 9159 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 9160 getLLVMStyleWithColumns(25))); 9161 EXPECT_EQ("#define A \\\n" 9162 " string s = \\\n" 9163 " \"123456789\" \\\n" 9164 " \"0\"; \\\n" 9165 " int i;", 9166 format("#define A string s = \"1234567890\"; int i;", 9167 getLLVMStyleWithColumns(20))); 9168 EXPECT_EQ("someFunction(\n" 9169 " \"aaabbbcc \"\n" 9170 " \"dddeeefff\");", 9171 format("someFunction(\"aaabbbcc dddeeefff\");", 9172 getLLVMStyleWithColumns(25))); 9173 } 9174 9175 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 9176 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 9177 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 9178 EXPECT_EQ("\"test\"\n" 9179 "\"\\n\"", 9180 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 9181 EXPECT_EQ("\"tes\\\\\"\n" 9182 "\"n\"", 9183 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 9184 EXPECT_EQ("\"\\\\\\\\\"\n" 9185 "\"\\n\"", 9186 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 9187 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 9188 EXPECT_EQ("\"\\uff01\"\n" 9189 "\"test\"", 9190 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 9191 EXPECT_EQ("\"\\Uff01ff02\"", 9192 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 9193 EXPECT_EQ("\"\\x000000000001\"\n" 9194 "\"next\"", 9195 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 9196 EXPECT_EQ("\"\\x000000000001next\"", 9197 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 9198 EXPECT_EQ("\"\\x000000000001\"", 9199 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 9200 EXPECT_EQ("\"test\"\n" 9201 "\"\\000000\"\n" 9202 "\"000001\"", 9203 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 9204 EXPECT_EQ("\"test\\000\"\n" 9205 "\"00000000\"\n" 9206 "\"1\"", 9207 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 9208 } 9209 9210 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 9211 verifyFormat("void f() {\n" 9212 " return g() {}\n" 9213 " void h() {}"); 9214 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 9215 "g();\n" 9216 "}"); 9217 } 9218 9219 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 9220 verifyFormat( 9221 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 9222 } 9223 9224 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 9225 verifyFormat("class X {\n" 9226 " void f() {\n" 9227 " }\n" 9228 "};", 9229 getLLVMStyleWithColumns(12)); 9230 } 9231 9232 TEST_F(FormatTest, ConfigurableIndentWidth) { 9233 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 9234 EightIndent.IndentWidth = 8; 9235 EightIndent.ContinuationIndentWidth = 8; 9236 verifyFormat("void f() {\n" 9237 " someFunction();\n" 9238 " if (true) {\n" 9239 " f();\n" 9240 " }\n" 9241 "}", 9242 EightIndent); 9243 verifyFormat("class X {\n" 9244 " void f() {\n" 9245 " }\n" 9246 "};", 9247 EightIndent); 9248 verifyFormat("int x[] = {\n" 9249 " call(),\n" 9250 " call()};", 9251 EightIndent); 9252 } 9253 9254 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 9255 verifyFormat("double\n" 9256 "f();", 9257 getLLVMStyleWithColumns(8)); 9258 } 9259 9260 TEST_F(FormatTest, ConfigurableUseOfTab) { 9261 FormatStyle Tab = getLLVMStyleWithColumns(42); 9262 Tab.IndentWidth = 8; 9263 Tab.UseTab = FormatStyle::UT_Always; 9264 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9265 9266 EXPECT_EQ("if (aaaaaaaa && // q\n" 9267 " bb)\t\t// w\n" 9268 "\t;", 9269 format("if (aaaaaaaa &&// q\n" 9270 "bb)// w\n" 9271 ";", 9272 Tab)); 9273 EXPECT_EQ("if (aaa && bbb) // w\n" 9274 "\t;", 9275 format("if(aaa&&bbb)// w\n" 9276 ";", 9277 Tab)); 9278 9279 verifyFormat("class X {\n" 9280 "\tvoid f() {\n" 9281 "\t\tsomeFunction(parameter1,\n" 9282 "\t\t\t parameter2);\n" 9283 "\t}\n" 9284 "};", 9285 Tab); 9286 verifyFormat("#define A \\\n" 9287 "\tvoid f() { \\\n" 9288 "\t\tsomeFunction( \\\n" 9289 "\t\t parameter1, \\\n" 9290 "\t\t parameter2); \\\n" 9291 "\t}", 9292 Tab); 9293 verifyFormat("int a;\t // x\n" 9294 "int bbbbbbbb; // x\n", 9295 Tab); 9296 9297 Tab.TabWidth = 4; 9298 Tab.IndentWidth = 8; 9299 verifyFormat("class TabWidth4Indent8 {\n" 9300 "\t\tvoid f() {\n" 9301 "\t\t\t\tsomeFunction(parameter1,\n" 9302 "\t\t\t\t\t\t\t parameter2);\n" 9303 "\t\t}\n" 9304 "};", 9305 Tab); 9306 9307 Tab.TabWidth = 4; 9308 Tab.IndentWidth = 4; 9309 verifyFormat("class TabWidth4Indent4 {\n" 9310 "\tvoid f() {\n" 9311 "\t\tsomeFunction(parameter1,\n" 9312 "\t\t\t\t\t parameter2);\n" 9313 "\t}\n" 9314 "};", 9315 Tab); 9316 9317 Tab.TabWidth = 8; 9318 Tab.IndentWidth = 4; 9319 verifyFormat("class TabWidth8Indent4 {\n" 9320 " void f() {\n" 9321 "\tsomeFunction(parameter1,\n" 9322 "\t\t parameter2);\n" 9323 " }\n" 9324 "};", 9325 Tab); 9326 9327 Tab.TabWidth = 8; 9328 Tab.IndentWidth = 8; 9329 EXPECT_EQ("/*\n" 9330 "\t a\t\tcomment\n" 9331 "\t in multiple lines\n" 9332 " */", 9333 format(" /*\t \t \n" 9334 " \t \t a\t\tcomment\t \t\n" 9335 " \t \t in multiple lines\t\n" 9336 " \t */", 9337 Tab)); 9338 9339 Tab.UseTab = FormatStyle::UT_ForIndentation; 9340 verifyFormat("{\n" 9341 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9342 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9343 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9344 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9345 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9346 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9347 "};", 9348 Tab); 9349 verifyFormat("enum AA {\n" 9350 "\ta1, // Force multiple lines\n" 9351 "\ta2,\n" 9352 "\ta3\n" 9353 "};", 9354 Tab); 9355 EXPECT_EQ("if (aaaaaaaa && // q\n" 9356 " bb) // w\n" 9357 "\t;", 9358 format("if (aaaaaaaa &&// q\n" 9359 "bb)// w\n" 9360 ";", 9361 Tab)); 9362 verifyFormat("class X {\n" 9363 "\tvoid f() {\n" 9364 "\t\tsomeFunction(parameter1,\n" 9365 "\t\t parameter2);\n" 9366 "\t}\n" 9367 "};", 9368 Tab); 9369 verifyFormat("{\n" 9370 "\tQ(\n" 9371 "\t {\n" 9372 "\t\t int a;\n" 9373 "\t\t someFunction(aaaaaaaa,\n" 9374 "\t\t bbbbbbb);\n" 9375 "\t },\n" 9376 "\t p);\n" 9377 "}", 9378 Tab); 9379 EXPECT_EQ("{\n" 9380 "\t/* aaaa\n" 9381 "\t bbbb */\n" 9382 "}", 9383 format("{\n" 9384 "/* aaaa\n" 9385 " bbbb */\n" 9386 "}", 9387 Tab)); 9388 EXPECT_EQ("{\n" 9389 "\t/*\n" 9390 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9391 "\t bbbbbbbbbbbbb\n" 9392 "\t*/\n" 9393 "}", 9394 format("{\n" 9395 "/*\n" 9396 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9397 "*/\n" 9398 "}", 9399 Tab)); 9400 EXPECT_EQ("{\n" 9401 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9402 "\t// bbbbbbbbbbbbb\n" 9403 "}", 9404 format("{\n" 9405 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9406 "}", 9407 Tab)); 9408 EXPECT_EQ("{\n" 9409 "\t/*\n" 9410 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9411 "\t bbbbbbbbbbbbb\n" 9412 "\t*/\n" 9413 "}", 9414 format("{\n" 9415 "\t/*\n" 9416 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9417 "\t*/\n" 9418 "}", 9419 Tab)); 9420 EXPECT_EQ("{\n" 9421 "\t/*\n" 9422 "\n" 9423 "\t*/\n" 9424 "}", 9425 format("{\n" 9426 "\t/*\n" 9427 "\n" 9428 "\t*/\n" 9429 "}", 9430 Tab)); 9431 EXPECT_EQ("{\n" 9432 "\t/*\n" 9433 " asdf\n" 9434 "\t*/\n" 9435 "}", 9436 format("{\n" 9437 "\t/*\n" 9438 " asdf\n" 9439 "\t*/\n" 9440 "}", 9441 Tab)); 9442 9443 Tab.UseTab = FormatStyle::UT_Never; 9444 EXPECT_EQ("/*\n" 9445 " a\t\tcomment\n" 9446 " in multiple lines\n" 9447 " */", 9448 format(" /*\t \t \n" 9449 " \t \t a\t\tcomment\t \t\n" 9450 " \t \t in multiple lines\t\n" 9451 " \t */", 9452 Tab)); 9453 EXPECT_EQ("/* some\n" 9454 " comment */", 9455 format(" \t \t /* some\n" 9456 " \t \t comment */", 9457 Tab)); 9458 EXPECT_EQ("int a; /* some\n" 9459 " comment */", 9460 format(" \t \t int a; /* some\n" 9461 " \t \t comment */", 9462 Tab)); 9463 9464 EXPECT_EQ("int a; /* some\n" 9465 "comment */", 9466 format(" \t \t int\ta; /* some\n" 9467 " \t \t comment */", 9468 Tab)); 9469 EXPECT_EQ("f(\"\t\t\"); /* some\n" 9470 " comment */", 9471 format(" \t \t f(\"\t\t\"); /* some\n" 9472 " \t \t comment */", 9473 Tab)); 9474 EXPECT_EQ("{\n" 9475 " /*\n" 9476 " * Comment\n" 9477 " */\n" 9478 " int i;\n" 9479 "}", 9480 format("{\n" 9481 "\t/*\n" 9482 "\t * Comment\n" 9483 "\t */\n" 9484 "\t int i;\n" 9485 "}")); 9486 9487 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 9488 Tab.TabWidth = 8; 9489 Tab.IndentWidth = 8; 9490 EXPECT_EQ("if (aaaaaaaa && // q\n" 9491 " bb) // w\n" 9492 "\t;", 9493 format("if (aaaaaaaa &&// q\n" 9494 "bb)// w\n" 9495 ";", 9496 Tab)); 9497 EXPECT_EQ("if (aaa && bbb) // w\n" 9498 "\t;", 9499 format("if(aaa&&bbb)// w\n" 9500 ";", 9501 Tab)); 9502 verifyFormat("class X {\n" 9503 "\tvoid f() {\n" 9504 "\t\tsomeFunction(parameter1,\n" 9505 "\t\t\t parameter2);\n" 9506 "\t}\n" 9507 "};", 9508 Tab); 9509 verifyFormat("#define A \\\n" 9510 "\tvoid f() { \\\n" 9511 "\t\tsomeFunction( \\\n" 9512 "\t\t parameter1, \\\n" 9513 "\t\t parameter2); \\\n" 9514 "\t}", 9515 Tab); 9516 Tab.TabWidth = 4; 9517 Tab.IndentWidth = 8; 9518 verifyFormat("class TabWidth4Indent8 {\n" 9519 "\t\tvoid f() {\n" 9520 "\t\t\t\tsomeFunction(parameter1,\n" 9521 "\t\t\t\t\t\t\t parameter2);\n" 9522 "\t\t}\n" 9523 "};", 9524 Tab); 9525 Tab.TabWidth = 4; 9526 Tab.IndentWidth = 4; 9527 verifyFormat("class TabWidth4Indent4 {\n" 9528 "\tvoid f() {\n" 9529 "\t\tsomeFunction(parameter1,\n" 9530 "\t\t\t\t\t parameter2);\n" 9531 "\t}\n" 9532 "};", 9533 Tab); 9534 Tab.TabWidth = 8; 9535 Tab.IndentWidth = 4; 9536 verifyFormat("class TabWidth8Indent4 {\n" 9537 " void f() {\n" 9538 "\tsomeFunction(parameter1,\n" 9539 "\t\t parameter2);\n" 9540 " }\n" 9541 "};", 9542 Tab); 9543 Tab.TabWidth = 8; 9544 Tab.IndentWidth = 8; 9545 EXPECT_EQ("/*\n" 9546 "\t a\t\tcomment\n" 9547 "\t in multiple lines\n" 9548 " */", 9549 format(" /*\t \t \n" 9550 " \t \t a\t\tcomment\t \t\n" 9551 " \t \t in multiple lines\t\n" 9552 " \t */", 9553 Tab)); 9554 verifyFormat("{\n" 9555 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9556 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9557 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9558 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9559 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9560 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9561 "};", 9562 Tab); 9563 verifyFormat("enum AA {\n" 9564 "\ta1, // Force multiple lines\n" 9565 "\ta2,\n" 9566 "\ta3\n" 9567 "};", 9568 Tab); 9569 EXPECT_EQ("if (aaaaaaaa && // q\n" 9570 " bb) // w\n" 9571 "\t;", 9572 format("if (aaaaaaaa &&// q\n" 9573 "bb)// w\n" 9574 ";", 9575 Tab)); 9576 verifyFormat("class X {\n" 9577 "\tvoid f() {\n" 9578 "\t\tsomeFunction(parameter1,\n" 9579 "\t\t\t parameter2);\n" 9580 "\t}\n" 9581 "};", 9582 Tab); 9583 verifyFormat("{\n" 9584 "\tQ(\n" 9585 "\t {\n" 9586 "\t\t int a;\n" 9587 "\t\t someFunction(aaaaaaaa,\n" 9588 "\t\t\t\t bbbbbbb);\n" 9589 "\t },\n" 9590 "\t p);\n" 9591 "}", 9592 Tab); 9593 EXPECT_EQ("{\n" 9594 "\t/* aaaa\n" 9595 "\t bbbb */\n" 9596 "}", 9597 format("{\n" 9598 "/* aaaa\n" 9599 " bbbb */\n" 9600 "}", 9601 Tab)); 9602 EXPECT_EQ("{\n" 9603 "\t/*\n" 9604 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9605 "\t bbbbbbbbbbbbb\n" 9606 "\t*/\n" 9607 "}", 9608 format("{\n" 9609 "/*\n" 9610 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9611 "*/\n" 9612 "}", 9613 Tab)); 9614 EXPECT_EQ("{\n" 9615 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9616 "\t// bbbbbbbbbbbbb\n" 9617 "}", 9618 format("{\n" 9619 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9620 "}", 9621 Tab)); 9622 EXPECT_EQ("{\n" 9623 "\t/*\n" 9624 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9625 "\t bbbbbbbbbbbbb\n" 9626 "\t*/\n" 9627 "}", 9628 format("{\n" 9629 "\t/*\n" 9630 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9631 "\t*/\n" 9632 "}", 9633 Tab)); 9634 EXPECT_EQ("{\n" 9635 "\t/*\n" 9636 "\n" 9637 "\t*/\n" 9638 "}", 9639 format("{\n" 9640 "\t/*\n" 9641 "\n" 9642 "\t*/\n" 9643 "}", 9644 Tab)); 9645 EXPECT_EQ("{\n" 9646 "\t/*\n" 9647 " asdf\n" 9648 "\t*/\n" 9649 "}", 9650 format("{\n" 9651 "\t/*\n" 9652 " asdf\n" 9653 "\t*/\n" 9654 "}", 9655 Tab)); 9656 EXPECT_EQ("/*\n" 9657 "\t a\t\tcomment\n" 9658 "\t in multiple lines\n" 9659 " */", 9660 format(" /*\t \t \n" 9661 " \t \t a\t\tcomment\t \t\n" 9662 " \t \t in multiple lines\t\n" 9663 " \t */", 9664 Tab)); 9665 EXPECT_EQ("/* some\n" 9666 " comment */", 9667 format(" \t \t /* some\n" 9668 " \t \t comment */", 9669 Tab)); 9670 EXPECT_EQ("int a; /* some\n" 9671 " comment */", 9672 format(" \t \t int a; /* some\n" 9673 " \t \t comment */", 9674 Tab)); 9675 EXPECT_EQ("int a; /* some\n" 9676 "comment */", 9677 format(" \t \t int\ta; /* some\n" 9678 " \t \t comment */", 9679 Tab)); 9680 EXPECT_EQ("f(\"\t\t\"); /* some\n" 9681 " comment */", 9682 format(" \t \t f(\"\t\t\"); /* some\n" 9683 " \t \t comment */", 9684 Tab)); 9685 EXPECT_EQ("{\n" 9686 " /*\n" 9687 " * Comment\n" 9688 " */\n" 9689 " int i;\n" 9690 "}", 9691 format("{\n" 9692 "\t/*\n" 9693 "\t * Comment\n" 9694 "\t */\n" 9695 "\t int i;\n" 9696 "}")); 9697 Tab.AlignConsecutiveAssignments = true; 9698 Tab.AlignConsecutiveDeclarations = true; 9699 Tab.TabWidth = 4; 9700 Tab.IndentWidth = 4; 9701 verifyFormat("class Assign {\n" 9702 "\tvoid f() {\n" 9703 "\t\tint x = 123;\n" 9704 "\t\tint random = 4;\n" 9705 "\t\tstd::string alphabet =\n" 9706 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 9707 "\t}\n" 9708 "};", 9709 Tab); 9710 } 9711 9712 TEST_F(FormatTest, CalculatesOriginalColumn) { 9713 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9714 "q\"; /* some\n" 9715 " comment */", 9716 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9717 "q\"; /* some\n" 9718 " comment */", 9719 getLLVMStyle())); 9720 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 9721 "/* some\n" 9722 " comment */", 9723 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 9724 " /* some\n" 9725 " comment */", 9726 getLLVMStyle())); 9727 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9728 "qqq\n" 9729 "/* some\n" 9730 " comment */", 9731 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9732 "qqq\n" 9733 " /* some\n" 9734 " comment */", 9735 getLLVMStyle())); 9736 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9737 "wwww; /* some\n" 9738 " comment */", 9739 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9740 "wwww; /* some\n" 9741 " comment */", 9742 getLLVMStyle())); 9743 } 9744 9745 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 9746 FormatStyle NoSpace = getLLVMStyle(); 9747 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 9748 9749 verifyFormat("while(true)\n" 9750 " continue;", 9751 NoSpace); 9752 verifyFormat("for(;;)\n" 9753 " continue;", 9754 NoSpace); 9755 verifyFormat("if(true)\n" 9756 " f();\n" 9757 "else if(true)\n" 9758 " f();", 9759 NoSpace); 9760 verifyFormat("do {\n" 9761 " do_something();\n" 9762 "} while(something());", 9763 NoSpace); 9764 verifyFormat("switch(x) {\n" 9765 "default:\n" 9766 " break;\n" 9767 "}", 9768 NoSpace); 9769 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 9770 verifyFormat("size_t x = sizeof(x);", NoSpace); 9771 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 9772 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 9773 verifyFormat("alignas(128) char a[128];", NoSpace); 9774 verifyFormat("size_t x = alignof(MyType);", NoSpace); 9775 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 9776 verifyFormat("int f() throw(Deprecated);", NoSpace); 9777 verifyFormat("typedef void (*cb)(int);", NoSpace); 9778 verifyFormat("T A::operator()();", NoSpace); 9779 verifyFormat("X A::operator++(T);", NoSpace); 9780 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 9781 9782 FormatStyle Space = getLLVMStyle(); 9783 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 9784 9785 verifyFormat("int f ();", Space); 9786 verifyFormat("void f (int a, T b) {\n" 9787 " while (true)\n" 9788 " continue;\n" 9789 "}", 9790 Space); 9791 verifyFormat("if (true)\n" 9792 " f ();\n" 9793 "else if (true)\n" 9794 " f ();", 9795 Space); 9796 verifyFormat("do {\n" 9797 " do_something ();\n" 9798 "} while (something ());", 9799 Space); 9800 verifyFormat("switch (x) {\n" 9801 "default:\n" 9802 " break;\n" 9803 "}", 9804 Space); 9805 verifyFormat("A::A () : a (1) {}", Space); 9806 verifyFormat("void f () __attribute__ ((asdf));", Space); 9807 verifyFormat("*(&a + 1);\n" 9808 "&((&a)[1]);\n" 9809 "a[(b + c) * d];\n" 9810 "(((a + 1) * 2) + 3) * 4;", 9811 Space); 9812 verifyFormat("#define A(x) x", Space); 9813 verifyFormat("#define A (x) x", Space); 9814 verifyFormat("#if defined(x)\n" 9815 "#endif", 9816 Space); 9817 verifyFormat("auto i = std::make_unique<int> (5);", Space); 9818 verifyFormat("size_t x = sizeof (x);", Space); 9819 verifyFormat("auto f (int x) -> decltype (x);", Space); 9820 verifyFormat("int f (T x) noexcept (x.create ());", Space); 9821 verifyFormat("alignas (128) char a[128];", Space); 9822 verifyFormat("size_t x = alignof (MyType);", Space); 9823 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 9824 verifyFormat("int f () throw (Deprecated);", Space); 9825 verifyFormat("typedef void (*cb) (int);", Space); 9826 verifyFormat("T A::operator() ();", Space); 9827 verifyFormat("X A::operator++ (T);", Space); 9828 verifyFormat("auto lambda = [] () { return 0; };", Space); 9829 verifyFormat("int x = int (y);", Space); 9830 9831 FormatStyle SomeSpace = getLLVMStyle(); 9832 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 9833 9834 verifyFormat("[]() -> float {}", SomeSpace); 9835 verifyFormat("[] (auto foo) {}", SomeSpace); 9836 verifyFormat("[foo]() -> int {}", SomeSpace); 9837 verifyFormat("int f();", SomeSpace); 9838 verifyFormat("void f (int a, T b) {\n" 9839 " while (true)\n" 9840 " continue;\n" 9841 "}", 9842 SomeSpace); 9843 verifyFormat("if (true)\n" 9844 " f();\n" 9845 "else if (true)\n" 9846 " f();", 9847 SomeSpace); 9848 verifyFormat("do {\n" 9849 " do_something();\n" 9850 "} while (something());", 9851 SomeSpace); 9852 verifyFormat("switch (x) {\n" 9853 "default:\n" 9854 " break;\n" 9855 "}", 9856 SomeSpace); 9857 verifyFormat("A::A() : a (1) {}", SomeSpace); 9858 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 9859 verifyFormat("*(&a + 1);\n" 9860 "&((&a)[1]);\n" 9861 "a[(b + c) * d];\n" 9862 "(((a + 1) * 2) + 3) * 4;", 9863 SomeSpace); 9864 verifyFormat("#define A(x) x", SomeSpace); 9865 verifyFormat("#define A (x) x", SomeSpace); 9866 verifyFormat("#if defined(x)\n" 9867 "#endif", 9868 SomeSpace); 9869 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 9870 verifyFormat("size_t x = sizeof (x);", SomeSpace); 9871 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 9872 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 9873 verifyFormat("alignas (128) char a[128];", SomeSpace); 9874 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 9875 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 9876 SomeSpace); 9877 verifyFormat("int f() throw (Deprecated);", SomeSpace); 9878 verifyFormat("typedef void (*cb) (int);", SomeSpace); 9879 verifyFormat("T A::operator()();", SomeSpace); 9880 verifyFormat("X A::operator++ (T);", SomeSpace); 9881 verifyFormat("int x = int (y);", SomeSpace); 9882 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 9883 } 9884 9885 TEST_F(FormatTest, SpaceAfterLogicalNot) { 9886 FormatStyle Spaces = getLLVMStyle(); 9887 Spaces.SpaceAfterLogicalNot = true; 9888 9889 verifyFormat("bool x = ! y", Spaces); 9890 verifyFormat("if (! isFailure())", Spaces); 9891 verifyFormat("if (! (a && b))", Spaces); 9892 verifyFormat("\"Error!\"", Spaces); 9893 verifyFormat("! ! x", Spaces); 9894 } 9895 9896 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 9897 FormatStyle Spaces = getLLVMStyle(); 9898 9899 Spaces.SpacesInParentheses = true; 9900 verifyFormat("do_something( ::globalVar );", Spaces); 9901 verifyFormat("call( x, y, z );", Spaces); 9902 verifyFormat("call();", Spaces); 9903 verifyFormat("std::function<void( int, int )> callback;", Spaces); 9904 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 9905 Spaces); 9906 verifyFormat("while ( (bool)1 )\n" 9907 " continue;", 9908 Spaces); 9909 verifyFormat("for ( ;; )\n" 9910 " continue;", 9911 Spaces); 9912 verifyFormat("if ( true )\n" 9913 " f();\n" 9914 "else if ( true )\n" 9915 " f();", 9916 Spaces); 9917 verifyFormat("do {\n" 9918 " do_something( (int)i );\n" 9919 "} while ( something() );", 9920 Spaces); 9921 verifyFormat("switch ( x ) {\n" 9922 "default:\n" 9923 " break;\n" 9924 "}", 9925 Spaces); 9926 9927 Spaces.SpacesInParentheses = false; 9928 Spaces.SpacesInCStyleCastParentheses = true; 9929 verifyFormat("Type *A = ( Type * )P;", Spaces); 9930 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 9931 verifyFormat("x = ( int32 )y;", Spaces); 9932 verifyFormat("int a = ( int )(2.0f);", Spaces); 9933 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 9934 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 9935 verifyFormat("#define x (( int )-1)", Spaces); 9936 9937 // Run the first set of tests again with: 9938 Spaces.SpacesInParentheses = false; 9939 Spaces.SpaceInEmptyParentheses = true; 9940 Spaces.SpacesInCStyleCastParentheses = true; 9941 verifyFormat("call(x, y, z);", Spaces); 9942 verifyFormat("call( );", Spaces); 9943 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9944 verifyFormat("while (( bool )1)\n" 9945 " continue;", 9946 Spaces); 9947 verifyFormat("for (;;)\n" 9948 " continue;", 9949 Spaces); 9950 verifyFormat("if (true)\n" 9951 " f( );\n" 9952 "else if (true)\n" 9953 " f( );", 9954 Spaces); 9955 verifyFormat("do {\n" 9956 " do_something(( int )i);\n" 9957 "} while (something( ));", 9958 Spaces); 9959 verifyFormat("switch (x) {\n" 9960 "default:\n" 9961 " break;\n" 9962 "}", 9963 Spaces); 9964 9965 // Run the first set of tests again with: 9966 Spaces.SpaceAfterCStyleCast = true; 9967 verifyFormat("call(x, y, z);", Spaces); 9968 verifyFormat("call( );", Spaces); 9969 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9970 verifyFormat("while (( bool ) 1)\n" 9971 " continue;", 9972 Spaces); 9973 verifyFormat("for (;;)\n" 9974 " continue;", 9975 Spaces); 9976 verifyFormat("if (true)\n" 9977 " f( );\n" 9978 "else if (true)\n" 9979 " f( );", 9980 Spaces); 9981 verifyFormat("do {\n" 9982 " do_something(( int ) i);\n" 9983 "} while (something( ));", 9984 Spaces); 9985 verifyFormat("switch (x) {\n" 9986 "default:\n" 9987 " break;\n" 9988 "}", 9989 Spaces); 9990 9991 // Run subset of tests again with: 9992 Spaces.SpacesInCStyleCastParentheses = false; 9993 Spaces.SpaceAfterCStyleCast = true; 9994 verifyFormat("while ((bool) 1)\n" 9995 " continue;", 9996 Spaces); 9997 verifyFormat("do {\n" 9998 " do_something((int) i);\n" 9999 "} while (something( ));", 10000 Spaces); 10001 } 10002 10003 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 10004 verifyFormat("int a[5];"); 10005 verifyFormat("a[3] += 42;"); 10006 10007 FormatStyle Spaces = getLLVMStyle(); 10008 Spaces.SpacesInSquareBrackets = true; 10009 // Lambdas unchanged. 10010 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 10011 verifyFormat("return [i, args...] {};", Spaces); 10012 10013 // Not lambdas. 10014 verifyFormat("int a[ 5 ];", Spaces); 10015 verifyFormat("a[ 3 ] += 42;", Spaces); 10016 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 10017 verifyFormat("double &operator[](int i) { return 0; }\n" 10018 "int i;", 10019 Spaces); 10020 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 10021 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 10022 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 10023 } 10024 10025 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 10026 verifyFormat("int a = 5;"); 10027 verifyFormat("a += 42;"); 10028 verifyFormat("a or_eq 8;"); 10029 10030 FormatStyle Spaces = getLLVMStyle(); 10031 Spaces.SpaceBeforeAssignmentOperators = false; 10032 verifyFormat("int a= 5;", Spaces); 10033 verifyFormat("a+= 42;", Spaces); 10034 verifyFormat("a or_eq 8;", Spaces); 10035 } 10036 10037 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 10038 verifyFormat("class Foo : public Bar {};"); 10039 verifyFormat("Foo::Foo() : foo(1) {}"); 10040 verifyFormat("for (auto a : b) {\n}"); 10041 verifyFormat("int x = a ? b : c;"); 10042 verifyFormat("{\n" 10043 "label0:\n" 10044 " int x = 0;\n" 10045 "}"); 10046 verifyFormat("switch (x) {\n" 10047 "case 1:\n" 10048 "default:\n" 10049 "}"); 10050 10051 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 10052 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 10053 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 10054 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 10055 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 10056 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 10057 verifyFormat("{\n" 10058 "label1:\n" 10059 " int x = 0;\n" 10060 "}", 10061 CtorInitializerStyle); 10062 verifyFormat("switch (x) {\n" 10063 "case 1:\n" 10064 "default:\n" 10065 "}", 10066 CtorInitializerStyle); 10067 CtorInitializerStyle.BreakConstructorInitializers = 10068 FormatStyle::BCIS_AfterColon; 10069 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 10070 " aaaaaaaaaaaaaaaa(1),\n" 10071 " bbbbbbbbbbbbbbbb(2) {}", 10072 CtorInitializerStyle); 10073 CtorInitializerStyle.BreakConstructorInitializers = 10074 FormatStyle::BCIS_BeforeComma; 10075 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 10076 " : aaaaaaaaaaaaaaaa(1)\n" 10077 " , bbbbbbbbbbbbbbbb(2) {}", 10078 CtorInitializerStyle); 10079 CtorInitializerStyle.BreakConstructorInitializers = 10080 FormatStyle::BCIS_BeforeColon; 10081 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 10082 " : aaaaaaaaaaaaaaaa(1),\n" 10083 " bbbbbbbbbbbbbbbb(2) {}", 10084 CtorInitializerStyle); 10085 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 10086 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 10087 ": aaaaaaaaaaaaaaaa(1),\n" 10088 " bbbbbbbbbbbbbbbb(2) {}", 10089 CtorInitializerStyle); 10090 10091 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 10092 InheritanceStyle.SpaceBeforeInheritanceColon = false; 10093 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 10094 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 10095 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 10096 verifyFormat("int x = a ? b : c;", InheritanceStyle); 10097 verifyFormat("{\n" 10098 "label2:\n" 10099 " int x = 0;\n" 10100 "}", 10101 InheritanceStyle); 10102 verifyFormat("switch (x) {\n" 10103 "case 1:\n" 10104 "default:\n" 10105 "}", 10106 InheritanceStyle); 10107 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 10108 verifyFormat("class Foooooooooooooooooooooo:\n" 10109 " public aaaaaaaaaaaaaaaaaa,\n" 10110 " public bbbbbbbbbbbbbbbbbb {\n" 10111 "}", 10112 InheritanceStyle); 10113 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 10114 verifyFormat("class Foooooooooooooooooooooo\n" 10115 " : public aaaaaaaaaaaaaaaaaa\n" 10116 " , public bbbbbbbbbbbbbbbbbb {\n" 10117 "}", 10118 InheritanceStyle); 10119 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 10120 verifyFormat("class Foooooooooooooooooooooo\n" 10121 " : public aaaaaaaaaaaaaaaaaa,\n" 10122 " public bbbbbbbbbbbbbbbbbb {\n" 10123 "}", 10124 InheritanceStyle); 10125 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 10126 verifyFormat("class Foooooooooooooooooooooo\n" 10127 ": public aaaaaaaaaaaaaaaaaa,\n" 10128 " public bbbbbbbbbbbbbbbbbb {}", 10129 InheritanceStyle); 10130 10131 FormatStyle ForLoopStyle = getLLVMStyle(); 10132 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 10133 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 10134 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 10135 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 10136 verifyFormat("int x = a ? b : c;", ForLoopStyle); 10137 verifyFormat("{\n" 10138 "label2:\n" 10139 " int x = 0;\n" 10140 "}", 10141 ForLoopStyle); 10142 verifyFormat("switch (x) {\n" 10143 "case 1:\n" 10144 "default:\n" 10145 "}", 10146 ForLoopStyle); 10147 10148 FormatStyle NoSpaceStyle = getLLVMStyle(); 10149 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 10150 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 10151 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 10152 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 10153 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 10154 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 10155 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 10156 verifyFormat("{\n" 10157 "label3:\n" 10158 " int x = 0;\n" 10159 "}", 10160 NoSpaceStyle); 10161 verifyFormat("switch (x) {\n" 10162 "case 1:\n" 10163 "default:\n" 10164 "}", 10165 NoSpaceStyle); 10166 } 10167 10168 TEST_F(FormatTest, AlignConsecutiveAssignments) { 10169 FormatStyle Alignment = getLLVMStyle(); 10170 Alignment.AlignConsecutiveAssignments = false; 10171 verifyFormat("int a = 5;\n" 10172 "int oneTwoThree = 123;", 10173 Alignment); 10174 verifyFormat("int a = 5;\n" 10175 "int oneTwoThree = 123;", 10176 Alignment); 10177 10178 Alignment.AlignConsecutiveAssignments = true; 10179 verifyFormat("int a = 5;\n" 10180 "int oneTwoThree = 123;", 10181 Alignment); 10182 verifyFormat("int a = method();\n" 10183 "int oneTwoThree = 133;", 10184 Alignment); 10185 verifyFormat("a &= 5;\n" 10186 "bcd *= 5;\n" 10187 "ghtyf += 5;\n" 10188 "dvfvdb -= 5;\n" 10189 "a /= 5;\n" 10190 "vdsvsv %= 5;\n" 10191 "sfdbddfbdfbb ^= 5;\n" 10192 "dvsdsv |= 5;\n" 10193 "int dsvvdvsdvvv = 123;", 10194 Alignment); 10195 verifyFormat("int i = 1, j = 10;\n" 10196 "something = 2000;", 10197 Alignment); 10198 verifyFormat("something = 2000;\n" 10199 "int i = 1, j = 10;\n", 10200 Alignment); 10201 verifyFormat("something = 2000;\n" 10202 "another = 911;\n" 10203 "int i = 1, j = 10;\n" 10204 "oneMore = 1;\n" 10205 "i = 2;", 10206 Alignment); 10207 verifyFormat("int a = 5;\n" 10208 "int one = 1;\n" 10209 "method();\n" 10210 "int oneTwoThree = 123;\n" 10211 "int oneTwo = 12;", 10212 Alignment); 10213 verifyFormat("int oneTwoThree = 123;\n" 10214 "int oneTwo = 12;\n" 10215 "method();\n", 10216 Alignment); 10217 verifyFormat("int oneTwoThree = 123; // comment\n" 10218 "int oneTwo = 12; // comment", 10219 Alignment); 10220 EXPECT_EQ("int a = 5;\n" 10221 "\n" 10222 "int oneTwoThree = 123;", 10223 format("int a = 5;\n" 10224 "\n" 10225 "int oneTwoThree= 123;", 10226 Alignment)); 10227 EXPECT_EQ("int a = 5;\n" 10228 "int one = 1;\n" 10229 "\n" 10230 "int oneTwoThree = 123;", 10231 format("int a = 5;\n" 10232 "int one = 1;\n" 10233 "\n" 10234 "int oneTwoThree = 123;", 10235 Alignment)); 10236 EXPECT_EQ("int a = 5;\n" 10237 "int one = 1;\n" 10238 "\n" 10239 "int oneTwoThree = 123;\n" 10240 "int oneTwo = 12;", 10241 format("int a = 5;\n" 10242 "int one = 1;\n" 10243 "\n" 10244 "int oneTwoThree = 123;\n" 10245 "int oneTwo = 12;", 10246 Alignment)); 10247 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 10248 verifyFormat("#define A \\\n" 10249 " int aaaa = 12; \\\n" 10250 " int b = 23; \\\n" 10251 " int ccc = 234; \\\n" 10252 " int dddddddddd = 2345;", 10253 Alignment); 10254 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 10255 verifyFormat("#define A \\\n" 10256 " int aaaa = 12; \\\n" 10257 " int b = 23; \\\n" 10258 " int ccc = 234; \\\n" 10259 " int dddddddddd = 2345;", 10260 Alignment); 10261 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 10262 verifyFormat("#define A " 10263 " \\\n" 10264 " int aaaa = 12; " 10265 " \\\n" 10266 " int b = 23; " 10267 " \\\n" 10268 " int ccc = 234; " 10269 " \\\n" 10270 " int dddddddddd = 2345;", 10271 Alignment); 10272 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 10273 "k = 4, int l = 5,\n" 10274 " int m = 6) {\n" 10275 " int j = 10;\n" 10276 " otherThing = 1;\n" 10277 "}", 10278 Alignment); 10279 verifyFormat("void SomeFunction(int parameter = 0) {\n" 10280 " int i = 1;\n" 10281 " int j = 2;\n" 10282 " int big = 10000;\n" 10283 "}", 10284 Alignment); 10285 verifyFormat("class C {\n" 10286 "public:\n" 10287 " int i = 1;\n" 10288 " virtual void f() = 0;\n" 10289 "};", 10290 Alignment); 10291 verifyFormat("int i = 1;\n" 10292 "if (SomeType t = getSomething()) {\n" 10293 "}\n" 10294 "int j = 2;\n" 10295 "int big = 10000;", 10296 Alignment); 10297 verifyFormat("int j = 7;\n" 10298 "for (int k = 0; k < N; ++k) {\n" 10299 "}\n" 10300 "int j = 2;\n" 10301 "int big = 10000;\n" 10302 "}", 10303 Alignment); 10304 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10305 verifyFormat("int i = 1;\n" 10306 "LooooooooooongType loooooooooooooooooooooongVariable\n" 10307 " = someLooooooooooooooooongFunction();\n" 10308 "int j = 2;", 10309 Alignment); 10310 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 10311 verifyFormat("int i = 1;\n" 10312 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 10313 " someLooooooooooooooooongFunction();\n" 10314 "int j = 2;", 10315 Alignment); 10316 10317 verifyFormat("auto lambda = []() {\n" 10318 " auto i = 0;\n" 10319 " return 0;\n" 10320 "};\n" 10321 "int i = 0;\n" 10322 "auto v = type{\n" 10323 " i = 1, //\n" 10324 " (i = 2), //\n" 10325 " i = 3 //\n" 10326 "};", 10327 Alignment); 10328 10329 verifyFormat( 10330 "int i = 1;\n" 10331 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 10332 " loooooooooooooooooooooongParameterB);\n" 10333 "int j = 2;", 10334 Alignment); 10335 10336 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 10337 " typename B = very_long_type_name_1,\n" 10338 " typename T_2 = very_long_type_name_2>\n" 10339 "auto foo() {}\n", 10340 Alignment); 10341 verifyFormat("int a, b = 1;\n" 10342 "int c = 2;\n" 10343 "int dd = 3;\n", 10344 Alignment); 10345 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 10346 "float b[1][] = {{3.f}};\n", 10347 Alignment); 10348 verifyFormat("for (int i = 0; i < 1; i++)\n" 10349 " int x = 1;\n", 10350 Alignment); 10351 verifyFormat("for (i = 0; i < 1; i++)\n" 10352 " x = 1;\n" 10353 "y = 1;\n", 10354 Alignment); 10355 } 10356 10357 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 10358 FormatStyle Alignment = getLLVMStyle(); 10359 Alignment.AlignConsecutiveDeclarations = false; 10360 verifyFormat("float const a = 5;\n" 10361 "int oneTwoThree = 123;", 10362 Alignment); 10363 verifyFormat("int a = 5;\n" 10364 "float const oneTwoThree = 123;", 10365 Alignment); 10366 10367 Alignment.AlignConsecutiveDeclarations = true; 10368 verifyFormat("float const a = 5;\n" 10369 "int oneTwoThree = 123;", 10370 Alignment); 10371 verifyFormat("int a = method();\n" 10372 "float const oneTwoThree = 133;", 10373 Alignment); 10374 verifyFormat("int i = 1, j = 10;\n" 10375 "something = 2000;", 10376 Alignment); 10377 verifyFormat("something = 2000;\n" 10378 "int i = 1, j = 10;\n", 10379 Alignment); 10380 verifyFormat("float something = 2000;\n" 10381 "double another = 911;\n" 10382 "int i = 1, j = 10;\n" 10383 "const int *oneMore = 1;\n" 10384 "unsigned i = 2;", 10385 Alignment); 10386 verifyFormat("float a = 5;\n" 10387 "int one = 1;\n" 10388 "method();\n" 10389 "const double oneTwoThree = 123;\n" 10390 "const unsigned int oneTwo = 12;", 10391 Alignment); 10392 verifyFormat("int oneTwoThree{0}; // comment\n" 10393 "unsigned oneTwo; // comment", 10394 Alignment); 10395 EXPECT_EQ("float const a = 5;\n" 10396 "\n" 10397 "int oneTwoThree = 123;", 10398 format("float const a = 5;\n" 10399 "\n" 10400 "int oneTwoThree= 123;", 10401 Alignment)); 10402 EXPECT_EQ("float a = 5;\n" 10403 "int one = 1;\n" 10404 "\n" 10405 "unsigned oneTwoThree = 123;", 10406 format("float a = 5;\n" 10407 "int one = 1;\n" 10408 "\n" 10409 "unsigned oneTwoThree = 123;", 10410 Alignment)); 10411 EXPECT_EQ("float a = 5;\n" 10412 "int one = 1;\n" 10413 "\n" 10414 "unsigned oneTwoThree = 123;\n" 10415 "int oneTwo = 12;", 10416 format("float a = 5;\n" 10417 "int one = 1;\n" 10418 "\n" 10419 "unsigned oneTwoThree = 123;\n" 10420 "int oneTwo = 12;", 10421 Alignment)); 10422 // Function prototype alignment 10423 verifyFormat("int a();\n" 10424 "double b();", 10425 Alignment); 10426 verifyFormat("int a(int x);\n" 10427 "double b();", 10428 Alignment); 10429 unsigned OldColumnLimit = Alignment.ColumnLimit; 10430 // We need to set ColumnLimit to zero, in order to stress nested alignments, 10431 // otherwise the function parameters will be re-flowed onto a single line. 10432 Alignment.ColumnLimit = 0; 10433 EXPECT_EQ("int a(int x,\n" 10434 " float y);\n" 10435 "double b(int x,\n" 10436 " double y);", 10437 format("int a(int x,\n" 10438 " float y);\n" 10439 "double b(int x,\n" 10440 " double y);", 10441 Alignment)); 10442 // This ensures that function parameters of function declarations are 10443 // correctly indented when their owning functions are indented. 10444 // The failure case here is for 'double y' to not be indented enough. 10445 EXPECT_EQ("double a(int x);\n" 10446 "int b(int y,\n" 10447 " double z);", 10448 format("double a(int x);\n" 10449 "int b(int y,\n" 10450 " double z);", 10451 Alignment)); 10452 // Set ColumnLimit low so that we induce wrapping immediately after 10453 // the function name and opening paren. 10454 Alignment.ColumnLimit = 13; 10455 verifyFormat("int function(\n" 10456 " int x,\n" 10457 " bool y);", 10458 Alignment); 10459 Alignment.ColumnLimit = OldColumnLimit; 10460 // Ensure function pointers don't screw up recursive alignment 10461 verifyFormat("int a(int x, void (*fp)(int y));\n" 10462 "double b();", 10463 Alignment); 10464 Alignment.AlignConsecutiveAssignments = true; 10465 // Ensure recursive alignment is broken by function braces, so that the 10466 // "a = 1" does not align with subsequent assignments inside the function 10467 // body. 10468 verifyFormat("int func(int a = 1) {\n" 10469 " int b = 2;\n" 10470 " int cc = 3;\n" 10471 "}", 10472 Alignment); 10473 verifyFormat("float something = 2000;\n" 10474 "double another = 911;\n" 10475 "int i = 1, j = 10;\n" 10476 "const int *oneMore = 1;\n" 10477 "unsigned i = 2;", 10478 Alignment); 10479 verifyFormat("int oneTwoThree = {0}; // comment\n" 10480 "unsigned oneTwo = 0; // comment", 10481 Alignment); 10482 // Make sure that scope is correctly tracked, in the absence of braces 10483 verifyFormat("for (int i = 0; i < n; i++)\n" 10484 " j = i;\n" 10485 "double x = 1;\n", 10486 Alignment); 10487 verifyFormat("if (int i = 0)\n" 10488 " j = i;\n" 10489 "double x = 1;\n", 10490 Alignment); 10491 // Ensure operator[] and operator() are comprehended 10492 verifyFormat("struct test {\n" 10493 " long long int foo();\n" 10494 " int operator[](int a);\n" 10495 " double bar();\n" 10496 "};\n", 10497 Alignment); 10498 verifyFormat("struct test {\n" 10499 " long long int foo();\n" 10500 " int operator()(int a);\n" 10501 " double bar();\n" 10502 "};\n", 10503 Alignment); 10504 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 10505 " int const i = 1;\n" 10506 " int * j = 2;\n" 10507 " int big = 10000;\n" 10508 "\n" 10509 " unsigned oneTwoThree = 123;\n" 10510 " int oneTwo = 12;\n" 10511 " method();\n" 10512 " float k = 2;\n" 10513 " int ll = 10000;\n" 10514 "}", 10515 format("void SomeFunction(int parameter= 0) {\n" 10516 " int const i= 1;\n" 10517 " int *j=2;\n" 10518 " int big = 10000;\n" 10519 "\n" 10520 "unsigned oneTwoThree =123;\n" 10521 "int oneTwo = 12;\n" 10522 " method();\n" 10523 "float k= 2;\n" 10524 "int ll=10000;\n" 10525 "}", 10526 Alignment)); 10527 Alignment.AlignConsecutiveAssignments = false; 10528 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 10529 verifyFormat("#define A \\\n" 10530 " int aaaa = 12; \\\n" 10531 " float b = 23; \\\n" 10532 " const int ccc = 234; \\\n" 10533 " unsigned dddddddddd = 2345;", 10534 Alignment); 10535 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 10536 verifyFormat("#define A \\\n" 10537 " int aaaa = 12; \\\n" 10538 " float b = 23; \\\n" 10539 " const int ccc = 234; \\\n" 10540 " unsigned dddddddddd = 2345;", 10541 Alignment); 10542 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 10543 Alignment.ColumnLimit = 30; 10544 verifyFormat("#define A \\\n" 10545 " int aaaa = 12; \\\n" 10546 " float b = 23; \\\n" 10547 " const int ccc = 234; \\\n" 10548 " int dddddddddd = 2345;", 10549 Alignment); 10550 Alignment.ColumnLimit = 80; 10551 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 10552 "k = 4, int l = 5,\n" 10553 " int m = 6) {\n" 10554 " const int j = 10;\n" 10555 " otherThing = 1;\n" 10556 "}", 10557 Alignment); 10558 verifyFormat("void SomeFunction(int parameter = 0) {\n" 10559 " int const i = 1;\n" 10560 " int * j = 2;\n" 10561 " int big = 10000;\n" 10562 "}", 10563 Alignment); 10564 verifyFormat("class C {\n" 10565 "public:\n" 10566 " int i = 1;\n" 10567 " virtual void f() = 0;\n" 10568 "};", 10569 Alignment); 10570 verifyFormat("float i = 1;\n" 10571 "if (SomeType t = getSomething()) {\n" 10572 "}\n" 10573 "const unsigned j = 2;\n" 10574 "int big = 10000;", 10575 Alignment); 10576 verifyFormat("float j = 7;\n" 10577 "for (int k = 0; k < N; ++k) {\n" 10578 "}\n" 10579 "unsigned j = 2;\n" 10580 "int big = 10000;\n" 10581 "}", 10582 Alignment); 10583 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10584 verifyFormat("float i = 1;\n" 10585 "LooooooooooongType loooooooooooooooooooooongVariable\n" 10586 " = someLooooooooooooooooongFunction();\n" 10587 "int j = 2;", 10588 Alignment); 10589 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 10590 verifyFormat("int i = 1;\n" 10591 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 10592 " someLooooooooooooooooongFunction();\n" 10593 "int j = 2;", 10594 Alignment); 10595 10596 Alignment.AlignConsecutiveAssignments = true; 10597 verifyFormat("auto lambda = []() {\n" 10598 " auto ii = 0;\n" 10599 " float j = 0;\n" 10600 " return 0;\n" 10601 "};\n" 10602 "int i = 0;\n" 10603 "float i2 = 0;\n" 10604 "auto v = type{\n" 10605 " i = 1, //\n" 10606 " (i = 2), //\n" 10607 " i = 3 //\n" 10608 "};", 10609 Alignment); 10610 Alignment.AlignConsecutiveAssignments = false; 10611 10612 verifyFormat( 10613 "int i = 1;\n" 10614 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 10615 " loooooooooooooooooooooongParameterB);\n" 10616 "int j = 2;", 10617 Alignment); 10618 10619 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 10620 // We expect declarations and assignments to align, as long as it doesn't 10621 // exceed the column limit, starting a new alignment sequence whenever it 10622 // happens. 10623 Alignment.AlignConsecutiveAssignments = true; 10624 Alignment.ColumnLimit = 30; 10625 verifyFormat("float ii = 1;\n" 10626 "unsigned j = 2;\n" 10627 "int someVerylongVariable = 1;\n" 10628 "AnotherLongType ll = 123456;\n" 10629 "VeryVeryLongType k = 2;\n" 10630 "int myvar = 1;", 10631 Alignment); 10632 Alignment.ColumnLimit = 80; 10633 Alignment.AlignConsecutiveAssignments = false; 10634 10635 verifyFormat( 10636 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 10637 " typename LongType, typename B>\n" 10638 "auto foo() {}\n", 10639 Alignment); 10640 verifyFormat("float a, b = 1;\n" 10641 "int c = 2;\n" 10642 "int dd = 3;\n", 10643 Alignment); 10644 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 10645 "float b[1][] = {{3.f}};\n", 10646 Alignment); 10647 Alignment.AlignConsecutiveAssignments = true; 10648 verifyFormat("float a, b = 1;\n" 10649 "int c = 2;\n" 10650 "int dd = 3;\n", 10651 Alignment); 10652 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 10653 "float b[1][] = {{3.f}};\n", 10654 Alignment); 10655 Alignment.AlignConsecutiveAssignments = false; 10656 10657 Alignment.ColumnLimit = 30; 10658 Alignment.BinPackParameters = false; 10659 verifyFormat("void foo(float a,\n" 10660 " float b,\n" 10661 " int c,\n" 10662 " uint32_t *d) {\n" 10663 " int * e = 0;\n" 10664 " float f = 0;\n" 10665 " double g = 0;\n" 10666 "}\n" 10667 "void bar(ino_t a,\n" 10668 " int b,\n" 10669 " uint32_t *c,\n" 10670 " bool d) {}\n", 10671 Alignment); 10672 Alignment.BinPackParameters = true; 10673 Alignment.ColumnLimit = 80; 10674 10675 // Bug 33507 10676 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 10677 verifyFormat( 10678 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 10679 " static const Version verVs2017;\n" 10680 " return true;\n" 10681 "});\n", 10682 Alignment); 10683 Alignment.PointerAlignment = FormatStyle::PAS_Right; 10684 10685 // See llvm.org/PR35641 10686 Alignment.AlignConsecutiveDeclarations = true; 10687 verifyFormat("int func() { //\n" 10688 " int b;\n" 10689 " unsigned c;\n" 10690 "}", 10691 Alignment); 10692 10693 // See PR37175 10694 FormatStyle Style = getMozillaStyle(); 10695 Style.AlignConsecutiveDeclarations = true; 10696 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 10697 "foo(int a);", 10698 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 10699 } 10700 10701 TEST_F(FormatTest, LinuxBraceBreaking) { 10702 FormatStyle LinuxBraceStyle = getLLVMStyle(); 10703 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 10704 verifyFormat("namespace a\n" 10705 "{\n" 10706 "class A\n" 10707 "{\n" 10708 " void f()\n" 10709 " {\n" 10710 " if (true) {\n" 10711 " a();\n" 10712 " b();\n" 10713 " } else {\n" 10714 " a();\n" 10715 " }\n" 10716 " }\n" 10717 " void g() { return; }\n" 10718 "};\n" 10719 "struct B {\n" 10720 " int x;\n" 10721 "};\n" 10722 "} // namespace a\n", 10723 LinuxBraceStyle); 10724 verifyFormat("enum X {\n" 10725 " Y = 0,\n" 10726 "}\n", 10727 LinuxBraceStyle); 10728 verifyFormat("struct S {\n" 10729 " int Type;\n" 10730 " union {\n" 10731 " int x;\n" 10732 " double y;\n" 10733 " } Value;\n" 10734 " class C\n" 10735 " {\n" 10736 " MyFavoriteType Value;\n" 10737 " } Class;\n" 10738 "}\n", 10739 LinuxBraceStyle); 10740 } 10741 10742 TEST_F(FormatTest, MozillaBraceBreaking) { 10743 FormatStyle MozillaBraceStyle = getLLVMStyle(); 10744 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 10745 MozillaBraceStyle.FixNamespaceComments = false; 10746 verifyFormat("namespace a {\n" 10747 "class A\n" 10748 "{\n" 10749 " void f()\n" 10750 " {\n" 10751 " if (true) {\n" 10752 " a();\n" 10753 " b();\n" 10754 " }\n" 10755 " }\n" 10756 " void g() { return; }\n" 10757 "};\n" 10758 "enum E\n" 10759 "{\n" 10760 " A,\n" 10761 " // foo\n" 10762 " B,\n" 10763 " C\n" 10764 "};\n" 10765 "struct B\n" 10766 "{\n" 10767 " int x;\n" 10768 "};\n" 10769 "}\n", 10770 MozillaBraceStyle); 10771 verifyFormat("struct S\n" 10772 "{\n" 10773 " int Type;\n" 10774 " union\n" 10775 " {\n" 10776 " int x;\n" 10777 " double y;\n" 10778 " } Value;\n" 10779 " class C\n" 10780 " {\n" 10781 " MyFavoriteType Value;\n" 10782 " } Class;\n" 10783 "}\n", 10784 MozillaBraceStyle); 10785 } 10786 10787 TEST_F(FormatTest, StroustrupBraceBreaking) { 10788 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 10789 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10790 verifyFormat("namespace a {\n" 10791 "class A {\n" 10792 " void f()\n" 10793 " {\n" 10794 " if (true) {\n" 10795 " a();\n" 10796 " b();\n" 10797 " }\n" 10798 " }\n" 10799 " void g() { return; }\n" 10800 "};\n" 10801 "struct B {\n" 10802 " int x;\n" 10803 "};\n" 10804 "} // namespace a\n", 10805 StroustrupBraceStyle); 10806 10807 verifyFormat("void foo()\n" 10808 "{\n" 10809 " if (a) {\n" 10810 " a();\n" 10811 " }\n" 10812 " else {\n" 10813 " b();\n" 10814 " }\n" 10815 "}\n", 10816 StroustrupBraceStyle); 10817 10818 verifyFormat("#ifdef _DEBUG\n" 10819 "int foo(int i = 0)\n" 10820 "#else\n" 10821 "int foo(int i = 5)\n" 10822 "#endif\n" 10823 "{\n" 10824 " return i;\n" 10825 "}", 10826 StroustrupBraceStyle); 10827 10828 verifyFormat("void foo() {}\n" 10829 "void bar()\n" 10830 "#ifdef _DEBUG\n" 10831 "{\n" 10832 " foo();\n" 10833 "}\n" 10834 "#else\n" 10835 "{\n" 10836 "}\n" 10837 "#endif", 10838 StroustrupBraceStyle); 10839 10840 verifyFormat("void foobar() { int i = 5; }\n" 10841 "#ifdef _DEBUG\n" 10842 "void bar() {}\n" 10843 "#else\n" 10844 "void bar() { foobar(); }\n" 10845 "#endif", 10846 StroustrupBraceStyle); 10847 } 10848 10849 TEST_F(FormatTest, AllmanBraceBreaking) { 10850 FormatStyle AllmanBraceStyle = getLLVMStyle(); 10851 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 10852 10853 EXPECT_EQ("namespace a\n" 10854 "{\n" 10855 "void f();\n" 10856 "void g();\n" 10857 "} // namespace a\n", 10858 format("namespace a\n" 10859 "{\n" 10860 "void f();\n" 10861 "void g();\n" 10862 "}\n", 10863 AllmanBraceStyle)); 10864 10865 verifyFormat("namespace a\n" 10866 "{\n" 10867 "class A\n" 10868 "{\n" 10869 " void f()\n" 10870 " {\n" 10871 " if (true)\n" 10872 " {\n" 10873 " a();\n" 10874 " b();\n" 10875 " }\n" 10876 " }\n" 10877 " void g() { return; }\n" 10878 "};\n" 10879 "struct B\n" 10880 "{\n" 10881 " int x;\n" 10882 "};\n" 10883 "} // namespace a", 10884 AllmanBraceStyle); 10885 10886 verifyFormat("void f()\n" 10887 "{\n" 10888 " if (true)\n" 10889 " {\n" 10890 " a();\n" 10891 " }\n" 10892 " else if (false)\n" 10893 " {\n" 10894 " b();\n" 10895 " }\n" 10896 " else\n" 10897 " {\n" 10898 " c();\n" 10899 " }\n" 10900 "}\n", 10901 AllmanBraceStyle); 10902 10903 verifyFormat("void f()\n" 10904 "{\n" 10905 " for (int i = 0; i < 10; ++i)\n" 10906 " {\n" 10907 " a();\n" 10908 " }\n" 10909 " while (false)\n" 10910 " {\n" 10911 " b();\n" 10912 " }\n" 10913 " do\n" 10914 " {\n" 10915 " c();\n" 10916 " } while (false)\n" 10917 "}\n", 10918 AllmanBraceStyle); 10919 10920 verifyFormat("void f(int a)\n" 10921 "{\n" 10922 " switch (a)\n" 10923 " {\n" 10924 " case 0:\n" 10925 " break;\n" 10926 " case 1:\n" 10927 " {\n" 10928 " break;\n" 10929 " }\n" 10930 " case 2:\n" 10931 " {\n" 10932 " }\n" 10933 " break;\n" 10934 " default:\n" 10935 " break;\n" 10936 " }\n" 10937 "}\n", 10938 AllmanBraceStyle); 10939 10940 verifyFormat("enum X\n" 10941 "{\n" 10942 " Y = 0,\n" 10943 "}\n", 10944 AllmanBraceStyle); 10945 verifyFormat("enum X\n" 10946 "{\n" 10947 " Y = 0\n" 10948 "}\n", 10949 AllmanBraceStyle); 10950 10951 verifyFormat("@interface BSApplicationController ()\n" 10952 "{\n" 10953 "@private\n" 10954 " id _extraIvar;\n" 10955 "}\n" 10956 "@end\n", 10957 AllmanBraceStyle); 10958 10959 verifyFormat("#ifdef _DEBUG\n" 10960 "int foo(int i = 0)\n" 10961 "#else\n" 10962 "int foo(int i = 5)\n" 10963 "#endif\n" 10964 "{\n" 10965 " return i;\n" 10966 "}", 10967 AllmanBraceStyle); 10968 10969 verifyFormat("void foo() {}\n" 10970 "void bar()\n" 10971 "#ifdef _DEBUG\n" 10972 "{\n" 10973 " foo();\n" 10974 "}\n" 10975 "#else\n" 10976 "{\n" 10977 "}\n" 10978 "#endif", 10979 AllmanBraceStyle); 10980 10981 verifyFormat("void foobar() { int i = 5; }\n" 10982 "#ifdef _DEBUG\n" 10983 "void bar() {}\n" 10984 "#else\n" 10985 "void bar() { foobar(); }\n" 10986 "#endif", 10987 AllmanBraceStyle); 10988 10989 // This shouldn't affect ObjC blocks.. 10990 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 10991 " // ...\n" 10992 " int i;\n" 10993 "}];", 10994 AllmanBraceStyle); 10995 verifyFormat("void (^block)(void) = ^{\n" 10996 " // ...\n" 10997 " int i;\n" 10998 "};", 10999 AllmanBraceStyle); 11000 // .. or dict literals. 11001 verifyFormat("void f()\n" 11002 "{\n" 11003 " // ...\n" 11004 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 11005 "}", 11006 AllmanBraceStyle); 11007 verifyFormat("void f()\n" 11008 "{\n" 11009 " // ...\n" 11010 " [object someMethod:@{a : @\"b\"}];\n" 11011 "}", 11012 AllmanBraceStyle); 11013 verifyFormat("int f()\n" 11014 "{ // comment\n" 11015 " return 42;\n" 11016 "}", 11017 AllmanBraceStyle); 11018 11019 AllmanBraceStyle.ColumnLimit = 19; 11020 verifyFormat("void f() { int i; }", AllmanBraceStyle); 11021 AllmanBraceStyle.ColumnLimit = 18; 11022 verifyFormat("void f()\n" 11023 "{\n" 11024 " int i;\n" 11025 "}", 11026 AllmanBraceStyle); 11027 AllmanBraceStyle.ColumnLimit = 80; 11028 11029 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 11030 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 11031 FormatStyle::SIS_WithoutElse; 11032 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 11033 verifyFormat("void f(bool b)\n" 11034 "{\n" 11035 " if (b)\n" 11036 " {\n" 11037 " return;\n" 11038 " }\n" 11039 "}\n", 11040 BreakBeforeBraceShortIfs); 11041 verifyFormat("void f(bool b)\n" 11042 "{\n" 11043 " if constexpr (b)\n" 11044 " {\n" 11045 " return;\n" 11046 " }\n" 11047 "}\n", 11048 BreakBeforeBraceShortIfs); 11049 verifyFormat("void f(bool b)\n" 11050 "{\n" 11051 " if (b) return;\n" 11052 "}\n", 11053 BreakBeforeBraceShortIfs); 11054 verifyFormat("void f(bool b)\n" 11055 "{\n" 11056 " if constexpr (b) return;\n" 11057 "}\n", 11058 BreakBeforeBraceShortIfs); 11059 verifyFormat("void f(bool b)\n" 11060 "{\n" 11061 " while (b)\n" 11062 " {\n" 11063 " return;\n" 11064 " }\n" 11065 "}\n", 11066 BreakBeforeBraceShortIfs); 11067 } 11068 11069 TEST_F(FormatTest, GNUBraceBreaking) { 11070 FormatStyle GNUBraceStyle = getLLVMStyle(); 11071 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 11072 verifyFormat("namespace a\n" 11073 "{\n" 11074 "class A\n" 11075 "{\n" 11076 " void f()\n" 11077 " {\n" 11078 " int a;\n" 11079 " {\n" 11080 " int b;\n" 11081 " }\n" 11082 " if (true)\n" 11083 " {\n" 11084 " a();\n" 11085 " b();\n" 11086 " }\n" 11087 " }\n" 11088 " void g() { return; }\n" 11089 "}\n" 11090 "} // namespace a", 11091 GNUBraceStyle); 11092 11093 verifyFormat("void f()\n" 11094 "{\n" 11095 " if (true)\n" 11096 " {\n" 11097 " a();\n" 11098 " }\n" 11099 " else if (false)\n" 11100 " {\n" 11101 " b();\n" 11102 " }\n" 11103 " else\n" 11104 " {\n" 11105 " c();\n" 11106 " }\n" 11107 "}\n", 11108 GNUBraceStyle); 11109 11110 verifyFormat("void f()\n" 11111 "{\n" 11112 " for (int i = 0; i < 10; ++i)\n" 11113 " {\n" 11114 " a();\n" 11115 " }\n" 11116 " while (false)\n" 11117 " {\n" 11118 " b();\n" 11119 " }\n" 11120 " do\n" 11121 " {\n" 11122 " c();\n" 11123 " }\n" 11124 " while (false);\n" 11125 "}\n", 11126 GNUBraceStyle); 11127 11128 verifyFormat("void f(int a)\n" 11129 "{\n" 11130 " switch (a)\n" 11131 " {\n" 11132 " case 0:\n" 11133 " break;\n" 11134 " case 1:\n" 11135 " {\n" 11136 " break;\n" 11137 " }\n" 11138 " case 2:\n" 11139 " {\n" 11140 " }\n" 11141 " break;\n" 11142 " default:\n" 11143 " break;\n" 11144 " }\n" 11145 "}\n", 11146 GNUBraceStyle); 11147 11148 verifyFormat("enum X\n" 11149 "{\n" 11150 " Y = 0,\n" 11151 "}\n", 11152 GNUBraceStyle); 11153 11154 verifyFormat("@interface BSApplicationController ()\n" 11155 "{\n" 11156 "@private\n" 11157 " id _extraIvar;\n" 11158 "}\n" 11159 "@end\n", 11160 GNUBraceStyle); 11161 11162 verifyFormat("#ifdef _DEBUG\n" 11163 "int foo(int i = 0)\n" 11164 "#else\n" 11165 "int foo(int i = 5)\n" 11166 "#endif\n" 11167 "{\n" 11168 " return i;\n" 11169 "}", 11170 GNUBraceStyle); 11171 11172 verifyFormat("void foo() {}\n" 11173 "void bar()\n" 11174 "#ifdef _DEBUG\n" 11175 "{\n" 11176 " foo();\n" 11177 "}\n" 11178 "#else\n" 11179 "{\n" 11180 "}\n" 11181 "#endif", 11182 GNUBraceStyle); 11183 11184 verifyFormat("void foobar() { int i = 5; }\n" 11185 "#ifdef _DEBUG\n" 11186 "void bar() {}\n" 11187 "#else\n" 11188 "void bar() { foobar(); }\n" 11189 "#endif", 11190 GNUBraceStyle); 11191 } 11192 11193 TEST_F(FormatTest, WebKitBraceBreaking) { 11194 FormatStyle WebKitBraceStyle = getLLVMStyle(); 11195 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 11196 WebKitBraceStyle.FixNamespaceComments = false; 11197 verifyFormat("namespace a {\n" 11198 "class A {\n" 11199 " void f()\n" 11200 " {\n" 11201 " if (true) {\n" 11202 " a();\n" 11203 " b();\n" 11204 " }\n" 11205 " }\n" 11206 " void g() { return; }\n" 11207 "};\n" 11208 "enum E {\n" 11209 " A,\n" 11210 " // foo\n" 11211 " B,\n" 11212 " C\n" 11213 "};\n" 11214 "struct B {\n" 11215 " int x;\n" 11216 "};\n" 11217 "}\n", 11218 WebKitBraceStyle); 11219 verifyFormat("struct S {\n" 11220 " int Type;\n" 11221 " union {\n" 11222 " int x;\n" 11223 " double y;\n" 11224 " } Value;\n" 11225 " class C {\n" 11226 " MyFavoriteType Value;\n" 11227 " } Class;\n" 11228 "};\n", 11229 WebKitBraceStyle); 11230 } 11231 11232 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 11233 verifyFormat("void f() {\n" 11234 " try {\n" 11235 " } catch (const Exception &e) {\n" 11236 " }\n" 11237 "}\n", 11238 getLLVMStyle()); 11239 } 11240 11241 TEST_F(FormatTest, UnderstandsPragmas) { 11242 verifyFormat("#pragma omp reduction(| : var)"); 11243 verifyFormat("#pragma omp reduction(+ : var)"); 11244 11245 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 11246 "(including parentheses).", 11247 format("#pragma mark Any non-hyphenated or hyphenated string " 11248 "(including parentheses).")); 11249 } 11250 11251 TEST_F(FormatTest, UnderstandPragmaOption) { 11252 verifyFormat("#pragma option -C -A"); 11253 11254 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 11255 } 11256 11257 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 11258 FormatStyle Style = getLLVMStyle(); 11259 Style.ColumnLimit = 20; 11260 11261 // See PR41213 11262 EXPECT_EQ("/*\n" 11263 " *\t9012345\n" 11264 " * /8901\n" 11265 " */", 11266 format("/*\n" 11267 " *\t9012345 /8901\n" 11268 " */", 11269 Style)); 11270 EXPECT_EQ("/*\n" 11271 " *345678\n" 11272 " *\t/8901\n" 11273 " */", 11274 format("/*\n" 11275 " *345678\t/8901\n" 11276 " */", 11277 Style)); 11278 11279 verifyFormat("int a; // the\n" 11280 " // comment", Style); 11281 EXPECT_EQ("int a; /* first line\n" 11282 " * second\n" 11283 " * line third\n" 11284 " * line\n" 11285 " */", 11286 format("int a; /* first line\n" 11287 " * second\n" 11288 " * line third\n" 11289 " * line\n" 11290 " */", 11291 Style)); 11292 EXPECT_EQ("int a; // first line\n" 11293 " // second\n" 11294 " // line third\n" 11295 " // line", 11296 format("int a; // first line\n" 11297 " // second line\n" 11298 " // third line", 11299 Style)); 11300 11301 Style.PenaltyExcessCharacter = 90; 11302 verifyFormat("int a; // the comment", Style); 11303 EXPECT_EQ("int a; // the comment\n" 11304 " // aaa", 11305 format("int a; // the comment aaa", Style)); 11306 EXPECT_EQ("int a; /* first line\n" 11307 " * second line\n" 11308 " * third line\n" 11309 " */", 11310 format("int a; /* first line\n" 11311 " * second line\n" 11312 " * third line\n" 11313 " */", 11314 Style)); 11315 EXPECT_EQ("int a; // first line\n" 11316 " // second line\n" 11317 " // third line", 11318 format("int a; // first line\n" 11319 " // second line\n" 11320 " // third line", 11321 Style)); 11322 // FIXME: Investigate why this is not getting the same layout as the test 11323 // above. 11324 EXPECT_EQ("int a; /* first line\n" 11325 " * second line\n" 11326 " * third line\n" 11327 " */", 11328 format("int a; /* first line second line third line" 11329 "\n*/", 11330 Style)); 11331 11332 EXPECT_EQ("// foo bar baz bazfoo\n" 11333 "// foo bar foo bar\n", 11334 format("// foo bar baz bazfoo\n" 11335 "// foo bar foo bar\n", 11336 Style)); 11337 EXPECT_EQ("// foo bar baz bazfoo\n" 11338 "// foo bar foo bar\n", 11339 format("// foo bar baz bazfoo\n" 11340 "// foo bar foo bar\n", 11341 Style)); 11342 11343 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 11344 // next one. 11345 EXPECT_EQ("// foo bar baz bazfoo\n" 11346 "// bar foo bar\n", 11347 format("// foo bar baz bazfoo bar\n" 11348 "// foo bar\n", 11349 Style)); 11350 11351 EXPECT_EQ("// foo bar baz bazfoo\n" 11352 "// foo bar baz bazfoo\n" 11353 "// bar foo bar\n", 11354 format("// foo bar baz bazfoo\n" 11355 "// foo bar baz bazfoo bar\n" 11356 "// foo bar\n", 11357 Style)); 11358 11359 EXPECT_EQ("// foo bar baz bazfoo\n" 11360 "// foo bar baz bazfoo\n" 11361 "// bar foo bar\n", 11362 format("// foo bar baz bazfoo\n" 11363 "// foo bar baz bazfoo bar\n" 11364 "// foo bar\n", 11365 Style)); 11366 11367 // Make sure we do not keep protruding characters if strict mode reflow is 11368 // cheaper than keeping protruding characters. 11369 Style.ColumnLimit = 21; 11370 EXPECT_EQ("// foo foo foo foo\n" 11371 "// foo foo foo foo\n" 11372 "// foo foo foo foo\n", 11373 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", 11374 Style)); 11375 11376 EXPECT_EQ("int a = /* long block\n" 11377 " comment */\n" 11378 " 42;", 11379 format("int a = /* long block comment */ 42;", Style)); 11380 } 11381 11382 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 11383 for (size_t i = 1; i < Styles.size(); ++i) \ 11384 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 11385 << " differs from Style #0" 11386 11387 TEST_F(FormatTest, GetsPredefinedStyleByName) { 11388 SmallVector<FormatStyle, 3> Styles; 11389 Styles.resize(3); 11390 11391 Styles[0] = getLLVMStyle(); 11392 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 11393 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 11394 EXPECT_ALL_STYLES_EQUAL(Styles); 11395 11396 Styles[0] = getGoogleStyle(); 11397 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 11398 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 11399 EXPECT_ALL_STYLES_EQUAL(Styles); 11400 11401 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 11402 EXPECT_TRUE( 11403 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 11404 EXPECT_TRUE( 11405 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 11406 EXPECT_ALL_STYLES_EQUAL(Styles); 11407 11408 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 11409 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 11410 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 11411 EXPECT_ALL_STYLES_EQUAL(Styles); 11412 11413 Styles[0] = getMozillaStyle(); 11414 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 11415 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 11416 EXPECT_ALL_STYLES_EQUAL(Styles); 11417 11418 Styles[0] = getWebKitStyle(); 11419 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 11420 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 11421 EXPECT_ALL_STYLES_EQUAL(Styles); 11422 11423 Styles[0] = getGNUStyle(); 11424 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 11425 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 11426 EXPECT_ALL_STYLES_EQUAL(Styles); 11427 11428 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 11429 } 11430 11431 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 11432 SmallVector<FormatStyle, 8> Styles; 11433 Styles.resize(2); 11434 11435 Styles[0] = getGoogleStyle(); 11436 Styles[1] = getLLVMStyle(); 11437 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 11438 EXPECT_ALL_STYLES_EQUAL(Styles); 11439 11440 Styles.resize(5); 11441 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 11442 Styles[1] = getLLVMStyle(); 11443 Styles[1].Language = FormatStyle::LK_JavaScript; 11444 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 11445 11446 Styles[2] = getLLVMStyle(); 11447 Styles[2].Language = FormatStyle::LK_JavaScript; 11448 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 11449 "BasedOnStyle: Google", 11450 &Styles[2]) 11451 .value()); 11452 11453 Styles[3] = getLLVMStyle(); 11454 Styles[3].Language = FormatStyle::LK_JavaScript; 11455 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 11456 "Language: JavaScript", 11457 &Styles[3]) 11458 .value()); 11459 11460 Styles[4] = getLLVMStyle(); 11461 Styles[4].Language = FormatStyle::LK_JavaScript; 11462 EXPECT_EQ(0, parseConfiguration("---\n" 11463 "BasedOnStyle: LLVM\n" 11464 "IndentWidth: 123\n" 11465 "---\n" 11466 "BasedOnStyle: Google\n" 11467 "Language: JavaScript", 11468 &Styles[4]) 11469 .value()); 11470 EXPECT_ALL_STYLES_EQUAL(Styles); 11471 } 11472 11473 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 11474 Style.FIELD = false; \ 11475 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 11476 EXPECT_TRUE(Style.FIELD); \ 11477 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 11478 EXPECT_FALSE(Style.FIELD); 11479 11480 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 11481 11482 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 11483 Style.STRUCT.FIELD = false; \ 11484 EXPECT_EQ(0, \ 11485 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 11486 .value()); \ 11487 EXPECT_TRUE(Style.STRUCT.FIELD); \ 11488 EXPECT_EQ(0, \ 11489 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 11490 .value()); \ 11491 EXPECT_FALSE(Style.STRUCT.FIELD); 11492 11493 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 11494 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 11495 11496 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 11497 EXPECT_NE(VALUE, Style.FIELD); \ 11498 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 11499 EXPECT_EQ(VALUE, Style.FIELD) 11500 11501 TEST_F(FormatTest, ParsesConfigurationBools) { 11502 FormatStyle Style = {}; 11503 Style.Language = FormatStyle::LK_Cpp; 11504 CHECK_PARSE_BOOL(AlignOperands); 11505 CHECK_PARSE_BOOL(AlignTrailingComments); 11506 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 11507 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 11508 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 11509 CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine); 11510 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 11511 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 11512 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 11513 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 11514 CHECK_PARSE_BOOL(BinPackArguments); 11515 CHECK_PARSE_BOOL(BinPackParameters); 11516 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 11517 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 11518 CHECK_PARSE_BOOL(BreakStringLiterals); 11519 CHECK_PARSE_BOOL(CompactNamespaces); 11520 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 11521 CHECK_PARSE_BOOL(DerivePointerAlignment); 11522 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 11523 CHECK_PARSE_BOOL(DisableFormat); 11524 CHECK_PARSE_BOOL(IndentCaseLabels); 11525 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 11526 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 11527 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 11528 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 11529 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 11530 CHECK_PARSE_BOOL(ReflowComments); 11531 CHECK_PARSE_BOOL(SortIncludes); 11532 CHECK_PARSE_BOOL(SortUsingDeclarations); 11533 CHECK_PARSE_BOOL(SpacesInParentheses); 11534 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 11535 CHECK_PARSE_BOOL(SpacesInAngles); 11536 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 11537 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 11538 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 11539 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 11540 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 11541 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 11542 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 11543 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 11544 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 11545 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 11546 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 11547 11548 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 11549 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 11550 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 11551 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 11552 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 11553 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 11554 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 11555 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 11556 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 11557 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 11558 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 11559 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 11560 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 11561 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 11562 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 11563 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 11564 } 11565 11566 #undef CHECK_PARSE_BOOL 11567 11568 TEST_F(FormatTest, ParsesConfiguration) { 11569 FormatStyle Style = {}; 11570 Style.Language = FormatStyle::LK_Cpp; 11571 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 11572 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 11573 ConstructorInitializerIndentWidth, 1234u); 11574 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 11575 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 11576 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 11577 CHECK_PARSE("PenaltyBreakAssignment: 1234", 11578 PenaltyBreakAssignment, 1234u); 11579 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 11580 PenaltyBreakBeforeFirstCallParameter, 1234u); 11581 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 11582 PenaltyBreakTemplateDeclaration, 1234u); 11583 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 11584 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 11585 PenaltyReturnTypeOnItsOwnLine, 1234u); 11586 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 11587 SpacesBeforeTrailingComments, 1234u); 11588 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 11589 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 11590 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 11591 11592 Style.PointerAlignment = FormatStyle::PAS_Middle; 11593 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 11594 FormatStyle::PAS_Left); 11595 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 11596 FormatStyle::PAS_Right); 11597 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 11598 FormatStyle::PAS_Middle); 11599 // For backward compatibility: 11600 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 11601 FormatStyle::PAS_Left); 11602 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 11603 FormatStyle::PAS_Right); 11604 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 11605 FormatStyle::PAS_Middle); 11606 11607 Style.Standard = FormatStyle::LS_Auto; 11608 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 11609 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 11610 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 11611 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 11612 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 11613 11614 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 11615 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 11616 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 11617 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 11618 FormatStyle::BOS_None); 11619 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 11620 FormatStyle::BOS_All); 11621 // For backward compatibility: 11622 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 11623 FormatStyle::BOS_None); 11624 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 11625 FormatStyle::BOS_All); 11626 11627 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 11628 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 11629 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 11630 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 11631 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 11632 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 11633 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 11634 // For backward compatibility: 11635 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 11636 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 11637 11638 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 11639 CHECK_PARSE("BreakInheritanceList: BeforeComma", 11640 BreakInheritanceList, FormatStyle::BILS_BeforeComma); 11641 CHECK_PARSE("BreakInheritanceList: AfterColon", 11642 BreakInheritanceList, FormatStyle::BILS_AfterColon); 11643 CHECK_PARSE("BreakInheritanceList: BeforeColon", 11644 BreakInheritanceList, FormatStyle::BILS_BeforeColon); 11645 // For backward compatibility: 11646 CHECK_PARSE("BreakBeforeInheritanceComma: true", 11647 BreakInheritanceList, FormatStyle::BILS_BeforeComma); 11648 11649 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11650 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 11651 FormatStyle::BAS_Align); 11652 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 11653 FormatStyle::BAS_DontAlign); 11654 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 11655 FormatStyle::BAS_AlwaysBreak); 11656 // For backward compatibility: 11657 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 11658 FormatStyle::BAS_DontAlign); 11659 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 11660 FormatStyle::BAS_Align); 11661 11662 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 11663 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 11664 FormatStyle::ENAS_DontAlign); 11665 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 11666 FormatStyle::ENAS_Left); 11667 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 11668 FormatStyle::ENAS_Right); 11669 // For backward compatibility: 11670 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 11671 FormatStyle::ENAS_Left); 11672 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 11673 FormatStyle::ENAS_Right); 11674 11675 Style.UseTab = FormatStyle::UT_ForIndentation; 11676 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 11677 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 11678 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 11679 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 11680 FormatStyle::UT_ForContinuationAndIndentation); 11681 // For backward compatibility: 11682 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 11683 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 11684 11685 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11686 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 11687 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 11688 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 11689 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 11690 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 11691 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 11692 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 11693 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 11694 // For backward compatibility: 11695 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 11696 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 11697 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 11698 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 11699 11700 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 11701 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 11702 FormatStyle::SBPO_Never); 11703 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 11704 FormatStyle::SBPO_Always); 11705 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 11706 FormatStyle::SBPO_ControlStatements); 11707 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 11708 FormatStyle::SBPO_NonEmptyParentheses); 11709 // For backward compatibility: 11710 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 11711 FormatStyle::SBPO_Never); 11712 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 11713 FormatStyle::SBPO_ControlStatements); 11714 11715 Style.ColumnLimit = 123; 11716 FormatStyle BaseStyle = getLLVMStyle(); 11717 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 11718 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 11719 11720 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 11721 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 11722 FormatStyle::BS_Attach); 11723 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 11724 FormatStyle::BS_Linux); 11725 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 11726 FormatStyle::BS_Mozilla); 11727 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 11728 FormatStyle::BS_Stroustrup); 11729 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 11730 FormatStyle::BS_Allman); 11731 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 11732 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 11733 FormatStyle::BS_WebKit); 11734 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 11735 FormatStyle::BS_Custom); 11736 11737 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 11738 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 11739 FormatStyle::RTBS_None); 11740 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 11741 FormatStyle::RTBS_All); 11742 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 11743 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 11744 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 11745 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 11746 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 11747 AlwaysBreakAfterReturnType, 11748 FormatStyle::RTBS_TopLevelDefinitions); 11749 11750 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 11751 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", AlwaysBreakTemplateDeclarations, 11752 FormatStyle::BTDS_No); 11753 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", AlwaysBreakTemplateDeclarations, 11754 FormatStyle::BTDS_MultiLine); 11755 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", AlwaysBreakTemplateDeclarations, 11756 FormatStyle::BTDS_Yes); 11757 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", AlwaysBreakTemplateDeclarations, 11758 FormatStyle::BTDS_MultiLine); 11759 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", AlwaysBreakTemplateDeclarations, 11760 FormatStyle::BTDS_Yes); 11761 11762 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 11763 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 11764 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 11765 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 11766 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 11767 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 11768 AlwaysBreakAfterDefinitionReturnType, 11769 FormatStyle::DRTBS_TopLevel); 11770 11771 Style.NamespaceIndentation = FormatStyle::NI_All; 11772 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 11773 FormatStyle::NI_None); 11774 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 11775 FormatStyle::NI_Inner); 11776 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 11777 FormatStyle::NI_All); 11778 11779 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always; 11780 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 11781 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 11782 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 11783 AllowShortIfStatementsOnASingleLine, 11784 FormatStyle::SIS_WithoutElse); 11785 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 11786 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Always); 11787 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 11788 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 11789 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 11790 AllowShortIfStatementsOnASingleLine, 11791 FormatStyle::SIS_WithoutElse); 11792 11793 // FIXME: This is required because parsing a configuration simply overwrites 11794 // the first N elements of the list instead of resetting it. 11795 Style.ForEachMacros.clear(); 11796 std::vector<std::string> BoostForeach; 11797 BoostForeach.push_back("BOOST_FOREACH"); 11798 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 11799 std::vector<std::string> BoostAndQForeach; 11800 BoostAndQForeach.push_back("BOOST_FOREACH"); 11801 BoostAndQForeach.push_back("Q_FOREACH"); 11802 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 11803 BoostAndQForeach); 11804 11805 Style.StatementMacros.clear(); 11806 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 11807 std::vector<std::string>{"QUNUSED"}); 11808 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 11809 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 11810 11811 Style.NamespaceMacros.clear(); 11812 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 11813 std::vector<std::string>{"TESTSUITE"}); 11814 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 11815 std::vector<std::string>({"TESTSUITE", "SUITE"})); 11816 11817 Style.IncludeStyle.IncludeCategories.clear(); 11818 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 11819 {"abc/.*", 2}, {".*", 1}}; 11820 CHECK_PARSE("IncludeCategories:\n" 11821 " - Regex: abc/.*\n" 11822 " Priority: 2\n" 11823 " - Regex: .*\n" 11824 " Priority: 1", 11825 IncludeStyle.IncludeCategories, ExpectedCategories); 11826 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 11827 "abc$"); 11828 11829 Style.RawStringFormats.clear(); 11830 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 11831 { 11832 FormatStyle::LK_TextProto, 11833 {"pb", "proto"}, 11834 {"PARSE_TEXT_PROTO"}, 11835 /*CanonicalDelimiter=*/"", 11836 "llvm", 11837 }, 11838 { 11839 FormatStyle::LK_Cpp, 11840 {"cc", "cpp"}, 11841 {"C_CODEBLOCK", "CPPEVAL"}, 11842 /*CanonicalDelimiter=*/"cc", 11843 /*BasedOnStyle=*/"", 11844 }, 11845 }; 11846 11847 CHECK_PARSE("RawStringFormats:\n" 11848 " - Language: TextProto\n" 11849 " Delimiters:\n" 11850 " - 'pb'\n" 11851 " - 'proto'\n" 11852 " EnclosingFunctions:\n" 11853 " - 'PARSE_TEXT_PROTO'\n" 11854 " BasedOnStyle: llvm\n" 11855 " - Language: Cpp\n" 11856 " Delimiters:\n" 11857 " - 'cc'\n" 11858 " - 'cpp'\n" 11859 " EnclosingFunctions:\n" 11860 " - 'C_CODEBLOCK'\n" 11861 " - 'CPPEVAL'\n" 11862 " CanonicalDelimiter: 'cc'", 11863 RawStringFormats, ExpectedRawStringFormats); 11864 } 11865 11866 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 11867 FormatStyle Style = {}; 11868 Style.Language = FormatStyle::LK_Cpp; 11869 CHECK_PARSE("Language: Cpp\n" 11870 "IndentWidth: 12", 11871 IndentWidth, 12u); 11872 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 11873 "IndentWidth: 34", 11874 &Style), 11875 ParseError::Unsuitable); 11876 EXPECT_EQ(12u, Style.IndentWidth); 11877 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 11878 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 11879 11880 Style.Language = FormatStyle::LK_JavaScript; 11881 CHECK_PARSE("Language: JavaScript\n" 11882 "IndentWidth: 12", 11883 IndentWidth, 12u); 11884 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 11885 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 11886 "IndentWidth: 34", 11887 &Style), 11888 ParseError::Unsuitable); 11889 EXPECT_EQ(23u, Style.IndentWidth); 11890 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 11891 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 11892 11893 CHECK_PARSE("BasedOnStyle: LLVM\n" 11894 "IndentWidth: 67", 11895 IndentWidth, 67u); 11896 11897 CHECK_PARSE("---\n" 11898 "Language: JavaScript\n" 11899 "IndentWidth: 12\n" 11900 "---\n" 11901 "Language: Cpp\n" 11902 "IndentWidth: 34\n" 11903 "...\n", 11904 IndentWidth, 12u); 11905 11906 Style.Language = FormatStyle::LK_Cpp; 11907 CHECK_PARSE("---\n" 11908 "Language: JavaScript\n" 11909 "IndentWidth: 12\n" 11910 "---\n" 11911 "Language: Cpp\n" 11912 "IndentWidth: 34\n" 11913 "...\n", 11914 IndentWidth, 34u); 11915 CHECK_PARSE("---\n" 11916 "IndentWidth: 78\n" 11917 "---\n" 11918 "Language: JavaScript\n" 11919 "IndentWidth: 56\n" 11920 "...\n", 11921 IndentWidth, 78u); 11922 11923 Style.ColumnLimit = 123; 11924 Style.IndentWidth = 234; 11925 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 11926 Style.TabWidth = 345; 11927 EXPECT_FALSE(parseConfiguration("---\n" 11928 "IndentWidth: 456\n" 11929 "BreakBeforeBraces: Allman\n" 11930 "---\n" 11931 "Language: JavaScript\n" 11932 "IndentWidth: 111\n" 11933 "TabWidth: 111\n" 11934 "---\n" 11935 "Language: Cpp\n" 11936 "BreakBeforeBraces: Stroustrup\n" 11937 "TabWidth: 789\n" 11938 "...\n", 11939 &Style)); 11940 EXPECT_EQ(123u, Style.ColumnLimit); 11941 EXPECT_EQ(456u, Style.IndentWidth); 11942 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 11943 EXPECT_EQ(789u, Style.TabWidth); 11944 11945 EXPECT_EQ(parseConfiguration("---\n" 11946 "Language: JavaScript\n" 11947 "IndentWidth: 56\n" 11948 "---\n" 11949 "IndentWidth: 78\n" 11950 "...\n", 11951 &Style), 11952 ParseError::Error); 11953 EXPECT_EQ(parseConfiguration("---\n" 11954 "Language: JavaScript\n" 11955 "IndentWidth: 56\n" 11956 "---\n" 11957 "Language: JavaScript\n" 11958 "IndentWidth: 78\n" 11959 "...\n", 11960 &Style), 11961 ParseError::Error); 11962 11963 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 11964 } 11965 11966 #undef CHECK_PARSE 11967 11968 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 11969 FormatStyle Style = {}; 11970 Style.Language = FormatStyle::LK_JavaScript; 11971 Style.BreakBeforeTernaryOperators = true; 11972 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 11973 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 11974 11975 Style.BreakBeforeTernaryOperators = true; 11976 EXPECT_EQ(0, parseConfiguration("---\n" 11977 "BasedOnStyle: Google\n" 11978 "---\n" 11979 "Language: JavaScript\n" 11980 "IndentWidth: 76\n" 11981 "...\n", 11982 &Style) 11983 .value()); 11984 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 11985 EXPECT_EQ(76u, Style.IndentWidth); 11986 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 11987 } 11988 11989 TEST_F(FormatTest, ConfigurationRoundTripTest) { 11990 FormatStyle Style = getLLVMStyle(); 11991 std::string YAML = configurationAsText(Style); 11992 FormatStyle ParsedStyle = {}; 11993 ParsedStyle.Language = FormatStyle::LK_Cpp; 11994 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 11995 EXPECT_EQ(Style, ParsedStyle); 11996 } 11997 11998 TEST_F(FormatTest, WorksFor8bitEncodings) { 11999 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 12000 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 12001 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 12002 "\"\xef\xee\xf0\xf3...\"", 12003 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 12004 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 12005 "\xef\xee\xf0\xf3...\"", 12006 getLLVMStyleWithColumns(12))); 12007 } 12008 12009 TEST_F(FormatTest, HandlesUTF8BOM) { 12010 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 12011 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 12012 format("\xef\xbb\xbf#include <iostream>")); 12013 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 12014 format("\xef\xbb\xbf\n#include <iostream>")); 12015 } 12016 12017 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 12018 #if !defined(_MSC_VER) 12019 12020 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 12021 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 12022 getLLVMStyleWithColumns(35)); 12023 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 12024 getLLVMStyleWithColumns(31)); 12025 verifyFormat("// Однажды в студёную зимнюю пору...", 12026 getLLVMStyleWithColumns(36)); 12027 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 12028 verifyFormat("/* Однажды в студёную зимнюю пору... */", 12029 getLLVMStyleWithColumns(39)); 12030 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 12031 getLLVMStyleWithColumns(35)); 12032 } 12033 12034 TEST_F(FormatTest, SplitsUTF8Strings) { 12035 // Non-printable characters' width is currently considered to be the length in 12036 // bytes in UTF8. The characters can be displayed in very different manner 12037 // (zero-width, single width with a substitution glyph, expanded to their code 12038 // (e.g. "<8d>"), so there's no single correct way to handle them. 12039 EXPECT_EQ("\"aaaaÄ\"\n" 12040 "\"\xc2\x8d\";", 12041 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 12042 EXPECT_EQ("\"aaaaaaaÄ\"\n" 12043 "\"\xc2\x8d\";", 12044 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 12045 EXPECT_EQ("\"Однажды, в \"\n" 12046 "\"студёную \"\n" 12047 "\"зимнюю \"\n" 12048 "\"пору,\"", 12049 format("\"Однажды, в студёную зимнюю пору,\"", 12050 getLLVMStyleWithColumns(13))); 12051 EXPECT_EQ( 12052 "\"一 二 三 \"\n" 12053 "\"四 五六 \"\n" 12054 "\"七 八 九 \"\n" 12055 "\"十\"", 12056 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 12057 EXPECT_EQ("\"一\t\"\n" 12058 "\"二 \t\"\n" 12059 "\"三 四 \"\n" 12060 "\"五\t\"\n" 12061 "\"六 \t\"\n" 12062 "\"七 \"\n" 12063 "\"八九十\tqq\"", 12064 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 12065 getLLVMStyleWithColumns(11))); 12066 12067 // UTF8 character in an escape sequence. 12068 EXPECT_EQ("\"aaaaaa\"\n" 12069 "\"\\\xC2\x8D\"", 12070 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 12071 } 12072 12073 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 12074 EXPECT_EQ("const char *sssss =\n" 12075 " \"一二三四五六七八\\\n" 12076 " 九 十\";", 12077 format("const char *sssss = \"一二三四五六七八\\\n" 12078 " 九 十\";", 12079 getLLVMStyleWithColumns(30))); 12080 } 12081 12082 TEST_F(FormatTest, SplitsUTF8LineComments) { 12083 EXPECT_EQ("// aaaaÄ\xc2\x8d", 12084 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 12085 EXPECT_EQ("// Я из лесу\n" 12086 "// вышел; был\n" 12087 "// сильный\n" 12088 "// мороз.", 12089 format("// Я из лесу вышел; был сильный мороз.", 12090 getLLVMStyleWithColumns(13))); 12091 EXPECT_EQ("// 一二三\n" 12092 "// 四五六七\n" 12093 "// 八 九\n" 12094 "// 十", 12095 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 12096 } 12097 12098 TEST_F(FormatTest, SplitsUTF8BlockComments) { 12099 EXPECT_EQ("/* Гляжу,\n" 12100 " * поднимается\n" 12101 " * медленно в\n" 12102 " * гору\n" 12103 " * Лошадка,\n" 12104 " * везущая\n" 12105 " * хворосту\n" 12106 " * воз. */", 12107 format("/* Гляжу, поднимается медленно в гору\n" 12108 " * Лошадка, везущая хворосту воз. */", 12109 getLLVMStyleWithColumns(13))); 12110 EXPECT_EQ( 12111 "/* 一二三\n" 12112 " * 四五六七\n" 12113 " * 八 九\n" 12114 " * 十 */", 12115 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 12116 EXPECT_EQ("/* \n" 12117 " * \n" 12118 " * - */", 12119 format("/* - */", getLLVMStyleWithColumns(12))); 12120 } 12121 12122 #endif // _MSC_VER 12123 12124 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 12125 FormatStyle Style = getLLVMStyle(); 12126 12127 Style.ConstructorInitializerIndentWidth = 4; 12128 verifyFormat( 12129 "SomeClass::Constructor()\n" 12130 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 12131 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 12132 Style); 12133 12134 Style.ConstructorInitializerIndentWidth = 2; 12135 verifyFormat( 12136 "SomeClass::Constructor()\n" 12137 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 12138 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 12139 Style); 12140 12141 Style.ConstructorInitializerIndentWidth = 0; 12142 verifyFormat( 12143 "SomeClass::Constructor()\n" 12144 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 12145 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 12146 Style); 12147 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12148 verifyFormat( 12149 "SomeLongTemplateVariableName<\n" 12150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 12151 Style); 12152 verifyFormat( 12153 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 12154 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 12155 Style); 12156 12157 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 12158 verifyFormat( 12159 "SomeClass::Constructor() :\n" 12160 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 12161 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 12162 Style); 12163 } 12164 12165 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 12166 FormatStyle Style = getLLVMStyle(); 12167 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 12168 Style.ConstructorInitializerIndentWidth = 4; 12169 verifyFormat("SomeClass::Constructor()\n" 12170 " : a(a)\n" 12171 " , b(b)\n" 12172 " , c(c) {}", 12173 Style); 12174 verifyFormat("SomeClass::Constructor()\n" 12175 " : a(a) {}", 12176 Style); 12177 12178 Style.ColumnLimit = 0; 12179 verifyFormat("SomeClass::Constructor()\n" 12180 " : a(a) {}", 12181 Style); 12182 verifyFormat("SomeClass::Constructor() noexcept\n" 12183 " : a(a) {}", 12184 Style); 12185 verifyFormat("SomeClass::Constructor()\n" 12186 " : a(a)\n" 12187 " , b(b)\n" 12188 " , c(c) {}", 12189 Style); 12190 verifyFormat("SomeClass::Constructor()\n" 12191 " : a(a) {\n" 12192 " foo();\n" 12193 " bar();\n" 12194 "}", 12195 Style); 12196 12197 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12198 verifyFormat("SomeClass::Constructor()\n" 12199 " : a(a)\n" 12200 " , b(b)\n" 12201 " , c(c) {\n}", 12202 Style); 12203 verifyFormat("SomeClass::Constructor()\n" 12204 " : a(a) {\n}", 12205 Style); 12206 12207 Style.ColumnLimit = 80; 12208 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12209 Style.ConstructorInitializerIndentWidth = 2; 12210 verifyFormat("SomeClass::Constructor()\n" 12211 " : a(a)\n" 12212 " , b(b)\n" 12213 " , c(c) {}", 12214 Style); 12215 12216 Style.ConstructorInitializerIndentWidth = 0; 12217 verifyFormat("SomeClass::Constructor()\n" 12218 ": a(a)\n" 12219 ", b(b)\n" 12220 ", c(c) {}", 12221 Style); 12222 12223 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 12224 Style.ConstructorInitializerIndentWidth = 4; 12225 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 12226 verifyFormat( 12227 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 12228 Style); 12229 verifyFormat( 12230 "SomeClass::Constructor()\n" 12231 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 12232 Style); 12233 Style.ConstructorInitializerIndentWidth = 4; 12234 Style.ColumnLimit = 60; 12235 verifyFormat("SomeClass::Constructor()\n" 12236 " : aaaaaaaa(aaaaaaaa)\n" 12237 " , aaaaaaaa(aaaaaaaa)\n" 12238 " , aaaaaaaa(aaaaaaaa) {}", 12239 Style); 12240 } 12241 12242 TEST_F(FormatTest, Destructors) { 12243 verifyFormat("void F(int &i) { i.~int(); }"); 12244 verifyFormat("void F(int &i) { i->~int(); }"); 12245 } 12246 12247 TEST_F(FormatTest, FormatsWithWebKitStyle) { 12248 FormatStyle Style = getWebKitStyle(); 12249 12250 // Don't indent in outer namespaces. 12251 verifyFormat("namespace outer {\n" 12252 "int i;\n" 12253 "namespace inner {\n" 12254 " int i;\n" 12255 "} // namespace inner\n" 12256 "} // namespace outer\n" 12257 "namespace other_outer {\n" 12258 "int i;\n" 12259 "}", 12260 Style); 12261 12262 // Don't indent case labels. 12263 verifyFormat("switch (variable) {\n" 12264 "case 1:\n" 12265 "case 2:\n" 12266 " doSomething();\n" 12267 " break;\n" 12268 "default:\n" 12269 " ++variable;\n" 12270 "}", 12271 Style); 12272 12273 // Wrap before binary operators. 12274 EXPECT_EQ("void f()\n" 12275 "{\n" 12276 " if (aaaaaaaaaaaaaaaa\n" 12277 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 12278 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 12279 " return;\n" 12280 "}", 12281 format("void f() {\n" 12282 "if (aaaaaaaaaaaaaaaa\n" 12283 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 12284 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 12285 "return;\n" 12286 "}", 12287 Style)); 12288 12289 // Allow functions on a single line. 12290 verifyFormat("void f() { return; }", Style); 12291 12292 // Constructor initializers are formatted one per line with the "," on the 12293 // new line. 12294 verifyFormat("Constructor()\n" 12295 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 12296 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 12297 " aaaaaaaaaaaaaa)\n" 12298 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 12299 "{\n" 12300 "}", 12301 Style); 12302 verifyFormat("SomeClass::Constructor()\n" 12303 " : a(a)\n" 12304 "{\n" 12305 "}", 12306 Style); 12307 EXPECT_EQ("SomeClass::Constructor()\n" 12308 " : a(a)\n" 12309 "{\n" 12310 "}", 12311 format("SomeClass::Constructor():a(a){}", Style)); 12312 verifyFormat("SomeClass::Constructor()\n" 12313 " : a(a)\n" 12314 " , b(b)\n" 12315 " , c(c)\n" 12316 "{\n" 12317 "}", 12318 Style); 12319 verifyFormat("SomeClass::Constructor()\n" 12320 " : a(a)\n" 12321 "{\n" 12322 " foo();\n" 12323 " bar();\n" 12324 "}", 12325 Style); 12326 12327 // Access specifiers should be aligned left. 12328 verifyFormat("class C {\n" 12329 "public:\n" 12330 " int i;\n" 12331 "};", 12332 Style); 12333 12334 // Do not align comments. 12335 verifyFormat("int a; // Do not\n" 12336 "double b; // align comments.", 12337 Style); 12338 12339 // Do not align operands. 12340 EXPECT_EQ("ASSERT(aaaa\n" 12341 " || bbbb);", 12342 format("ASSERT ( aaaa\n||bbbb);", Style)); 12343 12344 // Accept input's line breaks. 12345 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 12346 " || bbbbbbbbbbbbbbb) {\n" 12347 " i++;\n" 12348 "}", 12349 format("if (aaaaaaaaaaaaaaa\n" 12350 "|| bbbbbbbbbbbbbbb) { i++; }", 12351 Style)); 12352 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 12353 " i++;\n" 12354 "}", 12355 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 12356 12357 // Don't automatically break all macro definitions (llvm.org/PR17842). 12358 verifyFormat("#define aNumber 10", Style); 12359 // However, generally keep the line breaks that the user authored. 12360 EXPECT_EQ("#define aNumber \\\n" 12361 " 10", 12362 format("#define aNumber \\\n" 12363 " 10", 12364 Style)); 12365 12366 // Keep empty and one-element array literals on a single line. 12367 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 12368 " copyItems:YES];", 12369 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 12370 "copyItems:YES];", 12371 Style)); 12372 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 12373 " copyItems:YES];", 12374 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 12375 " copyItems:YES];", 12376 Style)); 12377 // FIXME: This does not seem right, there should be more indentation before 12378 // the array literal's entries. Nested blocks have the same problem. 12379 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 12380 " @\"a\",\n" 12381 " @\"a\"\n" 12382 "]\n" 12383 " copyItems:YES];", 12384 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 12385 " @\"a\",\n" 12386 " @\"a\"\n" 12387 " ]\n" 12388 " copyItems:YES];", 12389 Style)); 12390 EXPECT_EQ( 12391 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 12392 " copyItems:YES];", 12393 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 12394 " copyItems:YES];", 12395 Style)); 12396 12397 verifyFormat("[self.a b:c c:d];", Style); 12398 EXPECT_EQ("[self.a b:c\n" 12399 " c:d];", 12400 format("[self.a b:c\n" 12401 "c:d];", 12402 Style)); 12403 } 12404 12405 TEST_F(FormatTest, FormatsLambdas) { 12406 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 12407 verifyFormat( 12408 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 12409 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 12410 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 12411 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 12412 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 12413 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 12414 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 12415 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 12416 verifyFormat("int x = f(*+[] {});"); 12417 verifyFormat("void f() {\n" 12418 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 12419 "}\n"); 12420 verifyFormat("void f() {\n" 12421 " other(x.begin(), //\n" 12422 " x.end(), //\n" 12423 " [&](int, int) { return 1; });\n" 12424 "}\n"); 12425 verifyFormat("void f() {\n" 12426 " other.other.other.other.other(\n" 12427 " x.begin(), x.end(),\n" 12428 " [something, rather](int, int, int, int, int, int, int) { return 1; });\n" 12429 "}\n"); 12430 verifyFormat("void f() {\n" 12431 " other.other.other.other.other(\n" 12432 " x.begin(), x.end(),\n" 12433 " [something, rather](int, int, int, int, int, int, int) {\n" 12434 " //\n" 12435 " });\n" 12436 "}\n"); 12437 verifyFormat("SomeFunction([]() { // A cool function...\n" 12438 " return 43;\n" 12439 "});"); 12440 EXPECT_EQ("SomeFunction([]() {\n" 12441 "#define A a\n" 12442 " return 43;\n" 12443 "});", 12444 format("SomeFunction([](){\n" 12445 "#define A a\n" 12446 "return 43;\n" 12447 "});")); 12448 verifyFormat("void f() {\n" 12449 " SomeFunction([](decltype(x), A *a) {});\n" 12450 "}"); 12451 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 12452 " [](const aaaaaaaaaa &a) { return a; });"); 12453 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 12454 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 12455 "});"); 12456 verifyFormat("Constructor()\n" 12457 " : Field([] { // comment\n" 12458 " int i;\n" 12459 " }) {}"); 12460 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 12461 " return some_parameter.size();\n" 12462 "};"); 12463 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 12464 " [](const string &s) { return s; };"); 12465 verifyFormat("int i = aaaaaa ? 1 //\n" 12466 " : [] {\n" 12467 " return 2; //\n" 12468 " }();"); 12469 verifyFormat("llvm::errs() << \"number of twos is \"\n" 12470 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 12471 " return x == 2; // force break\n" 12472 " });"); 12473 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 12474 " [=](int iiiiiiiiiiii) {\n" 12475 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 12476 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 12477 " });", 12478 getLLVMStyleWithColumns(60)); 12479 verifyFormat("SomeFunction({[&] {\n" 12480 " // comment\n" 12481 " },\n" 12482 " [&] {\n" 12483 " // comment\n" 12484 " }});"); 12485 verifyFormat("SomeFunction({[&] {\n" 12486 " // comment\n" 12487 "}});"); 12488 verifyFormat("virtual aaaaaaaaaaaaaaaa(\n" 12489 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 12490 " aaaaa aaaaaaaaa);"); 12491 12492 // Lambdas with return types. 12493 verifyFormat("int c = []() -> int { return 2; }();\n"); 12494 verifyFormat("int c = []() -> int * { return 2; }();\n"); 12495 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 12496 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 12497 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 12498 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 12499 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 12500 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 12501 verifyFormat("[a, a]() -> a<1> {};"); 12502 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 12503 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 12504 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 12505 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 12506 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 12507 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 12508 verifyFormat("[]() -> foo<!5> { return {}; };"); 12509 verifyFormat("[]() -> foo<~5> { return {}; };"); 12510 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 12511 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 12512 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 12513 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 12514 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 12515 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 12516 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 12517 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 12518 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 12519 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 12520 verifyFormat("namespace bar {\n" 12521 "// broken:\n" 12522 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 12523 "} // namespace bar"); 12524 verifyFormat("namespace bar {\n" 12525 "// broken:\n" 12526 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 12527 "} // namespace bar"); 12528 verifyFormat("namespace bar {\n" 12529 "// broken:\n" 12530 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 12531 "} // namespace bar"); 12532 verifyFormat("namespace bar {\n" 12533 "// broken:\n" 12534 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 12535 "} // namespace bar"); 12536 verifyFormat("namespace bar {\n" 12537 "// broken:\n" 12538 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 12539 "} // namespace bar"); 12540 verifyFormat("namespace bar {\n" 12541 "// broken:\n" 12542 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 12543 "} // namespace bar"); 12544 verifyFormat("namespace bar {\n" 12545 "// broken:\n" 12546 "auto foo{[]() -> foo<!5> { return {}; }};\n" 12547 "} // namespace bar"); 12548 verifyFormat("namespace bar {\n" 12549 "// broken:\n" 12550 "auto foo{[]() -> foo<~5> { return {}; }};\n" 12551 "} // namespace bar"); 12552 verifyFormat("namespace bar {\n" 12553 "// broken:\n" 12554 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 12555 "} // namespace bar"); 12556 verifyFormat("namespace bar {\n" 12557 "// broken:\n" 12558 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 12559 "} // namespace bar"); 12560 verifyFormat("namespace bar {\n" 12561 "// broken:\n" 12562 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 12563 "} // namespace bar"); 12564 verifyFormat("namespace bar {\n" 12565 "// broken:\n" 12566 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 12567 "} // namespace bar"); 12568 verifyFormat("namespace bar {\n" 12569 "// broken:\n" 12570 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 12571 "} // namespace bar"); 12572 verifyFormat("namespace bar {\n" 12573 "// broken:\n" 12574 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 12575 "} // namespace bar"); 12576 verifyFormat("namespace bar {\n" 12577 "// broken:\n" 12578 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 12579 "} // namespace bar"); 12580 verifyFormat("namespace bar {\n" 12581 "// broken:\n" 12582 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 12583 "} // namespace bar"); 12584 verifyFormat("namespace bar {\n" 12585 "// broken:\n" 12586 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 12587 "} // namespace bar"); 12588 verifyFormat("namespace bar {\n" 12589 "// broken:\n" 12590 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 12591 "} // namespace bar"); 12592 verifyFormat("[]() -> a<1> {};"); 12593 verifyFormat("[]() -> a<1> { ; };"); 12594 verifyFormat("[]() -> a<1> { ; }();"); 12595 verifyFormat("[a, a]() -> a<true> {};"); 12596 verifyFormat("[]() -> a<true> {};"); 12597 verifyFormat("[]() -> a<true> { ; };"); 12598 verifyFormat("[]() -> a<true> { ; }();"); 12599 verifyFormat("[a, a]() -> a<false> {};"); 12600 verifyFormat("[]() -> a<false> {};"); 12601 verifyFormat("[]() -> a<false> { ; };"); 12602 verifyFormat("[]() -> a<false> { ; }();"); 12603 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 12604 verifyFormat("namespace bar {\n" 12605 "auto foo{[]() -> foo<false> { ; }};\n" 12606 "} // namespace bar"); 12607 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 12608 " int j) -> int {\n" 12609 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 12610 "};"); 12611 verifyFormat( 12612 "aaaaaaaaaaaaaaaaaaaaaa(\n" 12613 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 12614 " return aaaaaaaaaaaaaaaaa;\n" 12615 " });", 12616 getLLVMStyleWithColumns(70)); 12617 verifyFormat("[]() //\n" 12618 " -> int {\n" 12619 " return 1; //\n" 12620 "};"); 12621 12622 // Multiple lambdas in the same parentheses change indentation rules. These 12623 // lambdas are forced to start on new lines. 12624 verifyFormat("SomeFunction(\n" 12625 " []() {\n" 12626 " //\n" 12627 " },\n" 12628 " []() {\n" 12629 " //\n" 12630 " });"); 12631 12632 // A lambda passed as arg0 is always pushed to the next line. 12633 verifyFormat("SomeFunction(\n" 12634 " [this] {\n" 12635 " //\n" 12636 " },\n" 12637 " 1);\n"); 12638 12639 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like the arg0 12640 // case above. 12641 auto Style = getGoogleStyle(); 12642 Style.BinPackArguments = false; 12643 verifyFormat("SomeFunction(\n" 12644 " a,\n" 12645 " [this] {\n" 12646 " //\n" 12647 " },\n" 12648 " b);\n", 12649 Style); 12650 verifyFormat("SomeFunction(\n" 12651 " a,\n" 12652 " [this] {\n" 12653 " //\n" 12654 " },\n" 12655 " b);\n"); 12656 12657 // A lambda with a very long line forces arg0 to be pushed out irrespective of 12658 // the BinPackArguments value (as long as the code is wide enough). 12659 verifyFormat("something->SomeFunction(\n" 12660 " a,\n" 12661 " [this] {\n" 12662 " D0000000000000000000000000000000000000000000000000000000000001();\n" 12663 " },\n" 12664 " b);\n"); 12665 12666 // A multi-line lambda is pulled up as long as the introducer fits on the previous 12667 // line and there are no further args. 12668 verifyFormat("function(1, [this, that] {\n" 12669 " //\n" 12670 "});\n"); 12671 verifyFormat("function([this, that] {\n" 12672 " //\n" 12673 "});\n"); 12674 // FIXME: this format is not ideal and we should consider forcing the first arg 12675 // onto its own line. 12676 verifyFormat("function(a, b, c, //\n" 12677 " d, [this, that] {\n" 12678 " //\n" 12679 " });\n"); 12680 12681 // Multiple lambdas are treated correctly even when there is a short arg0. 12682 verifyFormat("SomeFunction(\n" 12683 " 1,\n" 12684 " [this] {\n" 12685 " //\n" 12686 " },\n" 12687 " [this] {\n" 12688 " //\n" 12689 " },\n" 12690 " 1);\n"); 12691 12692 // More complex introducers. 12693 verifyFormat("return [i, args...] {};"); 12694 12695 // Not lambdas. 12696 verifyFormat("constexpr char hello[]{\"hello\"};"); 12697 verifyFormat("double &operator[](int i) { return 0; }\n" 12698 "int i;"); 12699 verifyFormat("std::unique_ptr<int[]> foo() {}"); 12700 verifyFormat("int i = a[a][a]->f();"); 12701 verifyFormat("int i = (*b)[a]->f();"); 12702 12703 // Other corner cases. 12704 verifyFormat("void f() {\n" 12705 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 12706 " );\n" 12707 "}"); 12708 12709 // Lambdas created through weird macros. 12710 verifyFormat("void f() {\n" 12711 " MACRO((const AA &a) { return 1; });\n" 12712 " MACRO((AA &a) { return 1; });\n" 12713 "}"); 12714 12715 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 12716 " doo_dah();\n" 12717 " doo_dah();\n" 12718 " })) {\n" 12719 "}"); 12720 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 12721 " doo_dah();\n" 12722 " doo_dah();\n" 12723 " })) {\n" 12724 "}"); 12725 verifyFormat("auto lambda = []() {\n" 12726 " int a = 2\n" 12727 "#if A\n" 12728 " + 2\n" 12729 "#endif\n" 12730 " ;\n" 12731 "};"); 12732 12733 // Lambdas with complex multiline introducers. 12734 verifyFormat( 12735 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 12736 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 12737 " -> ::std::unordered_set<\n" 12738 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 12739 " //\n" 12740 " });"); 12741 12742 FormatStyle DoNotMerge = getLLVMStyle(); 12743 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 12744 verifyFormat("auto c = []() {\n" 12745 " return b;\n" 12746 "};", 12747 "auto c = []() { return b; };", DoNotMerge); 12748 verifyFormat("auto c = []() {\n" 12749 "};", 12750 " auto c = []() {};", DoNotMerge); 12751 12752 FormatStyle MergeEmptyOnly = getLLVMStyle(); 12753 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 12754 verifyFormat("auto c = []() {\n" 12755 " return b;\n" 12756 "};", 12757 "auto c = []() {\n" 12758 " return b;\n" 12759 " };", 12760 MergeEmptyOnly); 12761 verifyFormat("auto c = []() {};", 12762 "auto c = []() {\n" 12763 "};", 12764 MergeEmptyOnly); 12765 12766 FormatStyle MergeInline = getLLVMStyle(); 12767 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 12768 verifyFormat("auto c = []() {\n" 12769 " return b;\n" 12770 "};", 12771 "auto c = []() { return b; };", MergeInline); 12772 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 12773 MergeInline); 12774 verifyFormat("function([]() { return b; }, a)", 12775 "function([]() { return b; }, a)", MergeInline); 12776 verifyFormat("function(a, []() { return b; })", 12777 "function(a, []() { return b; })", MergeInline); 12778 } 12779 12780 TEST_F(FormatTest, EmptyLinesInLambdas) { 12781 verifyFormat("auto lambda = []() {\n" 12782 " x(); //\n" 12783 "};", 12784 "auto lambda = []() {\n" 12785 "\n" 12786 " x(); //\n" 12787 "\n" 12788 "};"); 12789 } 12790 12791 TEST_F(FormatTest, FormatsBlocks) { 12792 FormatStyle ShortBlocks = getLLVMStyle(); 12793 ShortBlocks.AllowShortBlocksOnASingleLine = true; 12794 verifyFormat("int (^Block)(int, int);", ShortBlocks); 12795 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 12796 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 12797 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 12798 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 12799 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 12800 12801 verifyFormat("foo(^{ bar(); });", ShortBlocks); 12802 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 12803 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 12804 12805 verifyFormat("[operation setCompletionBlock:^{\n" 12806 " [self onOperationDone];\n" 12807 "}];"); 12808 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 12809 " [self onOperationDone];\n" 12810 "}]};"); 12811 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 12812 " f();\n" 12813 "}];"); 12814 verifyFormat("int a = [operation block:^int(int *i) {\n" 12815 " return 1;\n" 12816 "}];"); 12817 verifyFormat("[myObject doSomethingWith:arg1\n" 12818 " aaa:^int(int *a) {\n" 12819 " return 1;\n" 12820 " }\n" 12821 " bbb:f(a * bbbbbbbb)];"); 12822 12823 verifyFormat("[operation setCompletionBlock:^{\n" 12824 " [self.delegate newDataAvailable];\n" 12825 "}];", 12826 getLLVMStyleWithColumns(60)); 12827 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 12828 " NSString *path = [self sessionFilePath];\n" 12829 " if (path) {\n" 12830 " // ...\n" 12831 " }\n" 12832 "});"); 12833 verifyFormat("[[SessionService sharedService]\n" 12834 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12835 " if (window) {\n" 12836 " [self windowDidLoad:window];\n" 12837 " } else {\n" 12838 " [self errorLoadingWindow];\n" 12839 " }\n" 12840 " }];"); 12841 verifyFormat("void (^largeBlock)(void) = ^{\n" 12842 " // ...\n" 12843 "};\n", 12844 getLLVMStyleWithColumns(40)); 12845 verifyFormat("[[SessionService sharedService]\n" 12846 " loadWindowWithCompletionBlock: //\n" 12847 " ^(SessionWindow *window) {\n" 12848 " if (window) {\n" 12849 " [self windowDidLoad:window];\n" 12850 " } else {\n" 12851 " [self errorLoadingWindow];\n" 12852 " }\n" 12853 " }];", 12854 getLLVMStyleWithColumns(60)); 12855 verifyFormat("[myObject doSomethingWith:arg1\n" 12856 " firstBlock:^(Foo *a) {\n" 12857 " // ...\n" 12858 " int i;\n" 12859 " }\n" 12860 " secondBlock:^(Bar *b) {\n" 12861 " // ...\n" 12862 " int i;\n" 12863 " }\n" 12864 " thirdBlock:^Foo(Bar *b) {\n" 12865 " // ...\n" 12866 " int i;\n" 12867 " }];"); 12868 verifyFormat("[myObject doSomethingWith:arg1\n" 12869 " firstBlock:-1\n" 12870 " secondBlock:^(Bar *b) {\n" 12871 " // ...\n" 12872 " int i;\n" 12873 " }];"); 12874 12875 verifyFormat("f(^{\n" 12876 " @autoreleasepool {\n" 12877 " if (a) {\n" 12878 " g();\n" 12879 " }\n" 12880 " }\n" 12881 "});"); 12882 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 12883 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 12884 "};"); 12885 12886 FormatStyle FourIndent = getLLVMStyle(); 12887 FourIndent.ObjCBlockIndentWidth = 4; 12888 verifyFormat("[operation setCompletionBlock:^{\n" 12889 " [self onOperationDone];\n" 12890 "}];", 12891 FourIndent); 12892 } 12893 12894 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 12895 FormatStyle ZeroColumn = getLLVMStyle(); 12896 ZeroColumn.ColumnLimit = 0; 12897 12898 verifyFormat("[[SessionService sharedService] " 12899 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12900 " if (window) {\n" 12901 " [self windowDidLoad:window];\n" 12902 " } else {\n" 12903 " [self errorLoadingWindow];\n" 12904 " }\n" 12905 "}];", 12906 ZeroColumn); 12907 EXPECT_EQ("[[SessionService sharedService]\n" 12908 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12909 " if (window) {\n" 12910 " [self windowDidLoad:window];\n" 12911 " } else {\n" 12912 " [self errorLoadingWindow];\n" 12913 " }\n" 12914 " }];", 12915 format("[[SessionService sharedService]\n" 12916 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12917 " if (window) {\n" 12918 " [self windowDidLoad:window];\n" 12919 " } else {\n" 12920 " [self errorLoadingWindow];\n" 12921 " }\n" 12922 "}];", 12923 ZeroColumn)); 12924 verifyFormat("[myObject doSomethingWith:arg1\n" 12925 " firstBlock:^(Foo *a) {\n" 12926 " // ...\n" 12927 " int i;\n" 12928 " }\n" 12929 " secondBlock:^(Bar *b) {\n" 12930 " // ...\n" 12931 " int i;\n" 12932 " }\n" 12933 " thirdBlock:^Foo(Bar *b) {\n" 12934 " // ...\n" 12935 " int i;\n" 12936 " }];", 12937 ZeroColumn); 12938 verifyFormat("f(^{\n" 12939 " @autoreleasepool {\n" 12940 " if (a) {\n" 12941 " g();\n" 12942 " }\n" 12943 " }\n" 12944 "});", 12945 ZeroColumn); 12946 verifyFormat("void (^largeBlock)(void) = ^{\n" 12947 " // ...\n" 12948 "};", 12949 ZeroColumn); 12950 12951 ZeroColumn.AllowShortBlocksOnASingleLine = true; 12952 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 12953 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 12954 ZeroColumn.AllowShortBlocksOnASingleLine = false; 12955 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 12956 " int i;\n" 12957 "};", 12958 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 12959 } 12960 12961 TEST_F(FormatTest, SupportsCRLF) { 12962 EXPECT_EQ("int a;\r\n" 12963 "int b;\r\n" 12964 "int c;\r\n", 12965 format("int a;\r\n" 12966 " int b;\r\n" 12967 " int c;\r\n", 12968 getLLVMStyle())); 12969 EXPECT_EQ("int a;\r\n" 12970 "int b;\r\n" 12971 "int c;\r\n", 12972 format("int a;\r\n" 12973 " int b;\n" 12974 " int c;\r\n", 12975 getLLVMStyle())); 12976 EXPECT_EQ("int a;\n" 12977 "int b;\n" 12978 "int c;\n", 12979 format("int a;\r\n" 12980 " int b;\n" 12981 " int c;\n", 12982 getLLVMStyle())); 12983 EXPECT_EQ("\"aaaaaaa \"\r\n" 12984 "\"bbbbbbb\";\r\n", 12985 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 12986 EXPECT_EQ("#define A \\\r\n" 12987 " b; \\\r\n" 12988 " c; \\\r\n" 12989 " d;\r\n", 12990 format("#define A \\\r\n" 12991 " b; \\\r\n" 12992 " c; d; \r\n", 12993 getGoogleStyle())); 12994 12995 EXPECT_EQ("/*\r\n" 12996 "multi line block comments\r\n" 12997 "should not introduce\r\n" 12998 "an extra carriage return\r\n" 12999 "*/\r\n", 13000 format("/*\r\n" 13001 "multi line block comments\r\n" 13002 "should not introduce\r\n" 13003 "an extra carriage return\r\n" 13004 "*/\r\n")); 13005 EXPECT_EQ("/*\r\n" 13006 "\r\n" 13007 "*/", 13008 format("/*\r\n" 13009 " \r\r\r\n" 13010 "*/")); 13011 } 13012 13013 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 13014 verifyFormat("MY_CLASS(C) {\n" 13015 " int i;\n" 13016 " int j;\n" 13017 "};"); 13018 } 13019 13020 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 13021 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 13022 TwoIndent.ContinuationIndentWidth = 2; 13023 13024 EXPECT_EQ("int i =\n" 13025 " longFunction(\n" 13026 " arg);", 13027 format("int i = longFunction(arg);", TwoIndent)); 13028 13029 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 13030 SixIndent.ContinuationIndentWidth = 6; 13031 13032 EXPECT_EQ("int i =\n" 13033 " longFunction(\n" 13034 " arg);", 13035 format("int i = longFunction(arg);", SixIndent)); 13036 } 13037 13038 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 13039 FormatStyle Style = getLLVMStyle(); 13040 verifyFormat("int Foo::getter(\n" 13041 " //\n" 13042 ") const {\n" 13043 " return foo;\n" 13044 "}", 13045 Style); 13046 verifyFormat("void Foo::setter(\n" 13047 " //\n" 13048 ") {\n" 13049 " foo = 1;\n" 13050 "}", 13051 Style); 13052 } 13053 13054 TEST_F(FormatTest, SpacesInAngles) { 13055 FormatStyle Spaces = getLLVMStyle(); 13056 Spaces.SpacesInAngles = true; 13057 13058 verifyFormat("static_cast< int >(arg);", Spaces); 13059 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 13060 verifyFormat("f< int, float >();", Spaces); 13061 verifyFormat("template <> g() {}", Spaces); 13062 verifyFormat("template < std::vector< int > > f() {}", Spaces); 13063 verifyFormat("std::function< void(int, int) > fct;", Spaces); 13064 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 13065 Spaces); 13066 13067 Spaces.Standard = FormatStyle::LS_Cpp03; 13068 Spaces.SpacesInAngles = true; 13069 verifyFormat("A< A< int > >();", Spaces); 13070 13071 Spaces.SpacesInAngles = false; 13072 verifyFormat("A<A<int> >();", Spaces); 13073 13074 Spaces.Standard = FormatStyle::LS_Cpp11; 13075 Spaces.SpacesInAngles = true; 13076 verifyFormat("A< A< int > >();", Spaces); 13077 13078 Spaces.SpacesInAngles = false; 13079 verifyFormat("A<A<int>>();", Spaces); 13080 } 13081 13082 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 13083 FormatStyle Style = getLLVMStyle(); 13084 Style.SpaceAfterTemplateKeyword = false; 13085 verifyFormat("template<int> void foo();", Style); 13086 } 13087 13088 TEST_F(FormatTest, TripleAngleBrackets) { 13089 verifyFormat("f<<<1, 1>>>();"); 13090 verifyFormat("f<<<1, 1, 1, s>>>();"); 13091 verifyFormat("f<<<a, b, c, d>>>();"); 13092 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 13093 verifyFormat("f<param><<<1, 1>>>();"); 13094 verifyFormat("f<1><<<1, 1>>>();"); 13095 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 13096 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13097 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 13098 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 13099 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 13100 } 13101 13102 TEST_F(FormatTest, MergeLessLessAtEnd) { 13103 verifyFormat("<<"); 13104 EXPECT_EQ("< < <", format("\\\n<<<")); 13105 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13106 "aaallvm::outs() <<"); 13107 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13108 "aaaallvm::outs()\n <<"); 13109 } 13110 13111 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 13112 std::string code = "#if A\n" 13113 "#if B\n" 13114 "a.\n" 13115 "#endif\n" 13116 " a = 1;\n" 13117 "#else\n" 13118 "#endif\n" 13119 "#if C\n" 13120 "#else\n" 13121 "#endif\n"; 13122 EXPECT_EQ(code, format(code)); 13123 } 13124 13125 TEST_F(FormatTest, HandleConflictMarkers) { 13126 // Git/SVN conflict markers. 13127 EXPECT_EQ("int a;\n" 13128 "void f() {\n" 13129 " callme(some(parameter1,\n" 13130 "<<<<<<< text by the vcs\n" 13131 " parameter2),\n" 13132 "||||||| text by the vcs\n" 13133 " parameter2),\n" 13134 " parameter3,\n" 13135 "======= text by the vcs\n" 13136 " parameter2, parameter3),\n" 13137 ">>>>>>> text by the vcs\n" 13138 " otherparameter);\n", 13139 format("int a;\n" 13140 "void f() {\n" 13141 " callme(some(parameter1,\n" 13142 "<<<<<<< text by the vcs\n" 13143 " parameter2),\n" 13144 "||||||| text by the vcs\n" 13145 " parameter2),\n" 13146 " parameter3,\n" 13147 "======= text by the vcs\n" 13148 " parameter2,\n" 13149 " parameter3),\n" 13150 ">>>>>>> text by the vcs\n" 13151 " otherparameter);\n")); 13152 13153 // Perforce markers. 13154 EXPECT_EQ("void f() {\n" 13155 " function(\n" 13156 ">>>> text by the vcs\n" 13157 " parameter,\n" 13158 "==== text by the vcs\n" 13159 " parameter,\n" 13160 "==== text by the vcs\n" 13161 " parameter,\n" 13162 "<<<< text by the vcs\n" 13163 " parameter);\n", 13164 format("void f() {\n" 13165 " function(\n" 13166 ">>>> text by the vcs\n" 13167 " parameter,\n" 13168 "==== text by the vcs\n" 13169 " parameter,\n" 13170 "==== text by the vcs\n" 13171 " parameter,\n" 13172 "<<<< text by the vcs\n" 13173 " parameter);\n")); 13174 13175 EXPECT_EQ("<<<<<<<\n" 13176 "|||||||\n" 13177 "=======\n" 13178 ">>>>>>>", 13179 format("<<<<<<<\n" 13180 "|||||||\n" 13181 "=======\n" 13182 ">>>>>>>")); 13183 13184 EXPECT_EQ("<<<<<<<\n" 13185 "|||||||\n" 13186 "int i;\n" 13187 "=======\n" 13188 ">>>>>>>", 13189 format("<<<<<<<\n" 13190 "|||||||\n" 13191 "int i;\n" 13192 "=======\n" 13193 ">>>>>>>")); 13194 13195 // FIXME: Handle parsing of macros around conflict markers correctly: 13196 EXPECT_EQ("#define Macro \\\n" 13197 "<<<<<<<\n" 13198 "Something \\\n" 13199 "|||||||\n" 13200 "Else \\\n" 13201 "=======\n" 13202 "Other \\\n" 13203 ">>>>>>>\n" 13204 " End int i;\n", 13205 format("#define Macro \\\n" 13206 "<<<<<<<\n" 13207 " Something \\\n" 13208 "|||||||\n" 13209 " Else \\\n" 13210 "=======\n" 13211 " Other \\\n" 13212 ">>>>>>>\n" 13213 " End\n" 13214 "int i;\n")); 13215 } 13216 13217 TEST_F(FormatTest, DisableRegions) { 13218 EXPECT_EQ("int i;\n" 13219 "// clang-format off\n" 13220 " int j;\n" 13221 "// clang-format on\n" 13222 "int k;", 13223 format(" int i;\n" 13224 " // clang-format off\n" 13225 " int j;\n" 13226 " // clang-format on\n" 13227 " int k;")); 13228 EXPECT_EQ("int i;\n" 13229 "/* clang-format off */\n" 13230 " int j;\n" 13231 "/* clang-format on */\n" 13232 "int k;", 13233 format(" int i;\n" 13234 " /* clang-format off */\n" 13235 " int j;\n" 13236 " /* clang-format on */\n" 13237 " int k;")); 13238 13239 // Don't reflow comments within disabled regions. 13240 EXPECT_EQ( 13241 "// clang-format off\n" 13242 "// long long long long long long line\n" 13243 "/* clang-format on */\n" 13244 "/* long long long\n" 13245 " * long long long\n" 13246 " * line */\n" 13247 "int i;\n" 13248 "/* clang-format off */\n" 13249 "/* long long long long long long line */\n", 13250 format("// clang-format off\n" 13251 "// long long long long long long line\n" 13252 "/* clang-format on */\n" 13253 "/* long long long long long long line */\n" 13254 "int i;\n" 13255 "/* clang-format off */\n" 13256 "/* long long long long long long line */\n", 13257 getLLVMStyleWithColumns(20))); 13258 } 13259 13260 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 13261 format("? ) ="); 13262 verifyNoCrash("#define a\\\n /**/}"); 13263 } 13264 13265 TEST_F(FormatTest, FormatsTableGenCode) { 13266 FormatStyle Style = getLLVMStyle(); 13267 Style.Language = FormatStyle::LK_TableGen; 13268 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 13269 } 13270 13271 TEST_F(FormatTest, ArrayOfTemplates) { 13272 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 13273 format("auto a = new unique_ptr<int > [ 10];")); 13274 13275 FormatStyle Spaces = getLLVMStyle(); 13276 Spaces.SpacesInSquareBrackets = true; 13277 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 13278 format("auto a = new unique_ptr<int > [10];", Spaces)); 13279 } 13280 13281 TEST_F(FormatTest, ArrayAsTemplateType) { 13282 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 13283 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 13284 13285 FormatStyle Spaces = getLLVMStyle(); 13286 Spaces.SpacesInSquareBrackets = true; 13287 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 13288 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 13289 } 13290 13291 TEST_F(FormatTest, NoSpaceAfterSuper) { 13292 verifyFormat("__super::FooBar();"); 13293 } 13294 13295 TEST(FormatStyle, GetStyleWithEmptyFileName) { 13296 llvm::vfs::InMemoryFileSystem FS; 13297 auto Style1 = getStyle("file", "", "Google", "", &FS); 13298 ASSERT_TRUE((bool)Style1); 13299 ASSERT_EQ(*Style1, getGoogleStyle()); 13300 } 13301 13302 TEST(FormatStyle, GetStyleOfFile) { 13303 llvm::vfs::InMemoryFileSystem FS; 13304 // Test 1: format file in the same directory. 13305 ASSERT_TRUE( 13306 FS.addFile("/a/.clang-format", 0, 13307 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 13308 ASSERT_TRUE( 13309 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 13310 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 13311 ASSERT_TRUE((bool)Style1); 13312 ASSERT_EQ(*Style1, getLLVMStyle()); 13313 13314 // Test 2.1: fallback to default. 13315 ASSERT_TRUE( 13316 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 13317 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 13318 ASSERT_TRUE((bool)Style2); 13319 ASSERT_EQ(*Style2, getMozillaStyle()); 13320 13321 // Test 2.2: no format on 'none' fallback style. 13322 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 13323 ASSERT_TRUE((bool)Style2); 13324 ASSERT_EQ(*Style2, getNoStyle()); 13325 13326 // Test 2.3: format if config is found with no based style while fallback is 13327 // 'none'. 13328 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 13329 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 13330 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 13331 ASSERT_TRUE((bool)Style2); 13332 ASSERT_EQ(*Style2, getLLVMStyle()); 13333 13334 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 13335 Style2 = getStyle("{}", "a.h", "none", "", &FS); 13336 ASSERT_TRUE((bool)Style2); 13337 ASSERT_EQ(*Style2, getLLVMStyle()); 13338 13339 // Test 3: format file in parent directory. 13340 ASSERT_TRUE( 13341 FS.addFile("/c/.clang-format", 0, 13342 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 13343 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 13344 llvm::MemoryBuffer::getMemBuffer("int i;"))); 13345 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 13346 ASSERT_TRUE((bool)Style3); 13347 ASSERT_EQ(*Style3, getGoogleStyle()); 13348 13349 // Test 4: error on invalid fallback style 13350 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 13351 ASSERT_FALSE((bool)Style4); 13352 llvm::consumeError(Style4.takeError()); 13353 13354 // Test 5: error on invalid yaml on command line 13355 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 13356 ASSERT_FALSE((bool)Style5); 13357 llvm::consumeError(Style5.takeError()); 13358 13359 // Test 6: error on invalid style 13360 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 13361 ASSERT_FALSE((bool)Style6); 13362 llvm::consumeError(Style6.takeError()); 13363 13364 // Test 7: found config file, error on parsing it 13365 ASSERT_TRUE( 13366 FS.addFile("/d/.clang-format", 0, 13367 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 13368 "InvalidKey: InvalidValue"))); 13369 ASSERT_TRUE( 13370 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 13371 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 13372 ASSERT_FALSE((bool)Style7); 13373 llvm::consumeError(Style7.takeError()); 13374 13375 // Test 8: inferred per-language defaults apply. 13376 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 13377 ASSERT_TRUE((bool)StyleTd); 13378 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 13379 } 13380 13381 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 13382 // Column limit is 20. 13383 std::string Code = "Type *a =\n" 13384 " new Type();\n" 13385 "g(iiiii, 0, jjjjj,\n" 13386 " 0, kkkkk, 0, mm);\n" 13387 "int bad = format ;"; 13388 std::string Expected = "auto a = new Type();\n" 13389 "g(iiiii, nullptr,\n" 13390 " jjjjj, nullptr,\n" 13391 " kkkkk, nullptr,\n" 13392 " mm);\n" 13393 "int bad = format ;"; 13394 FileID ID = Context.createInMemoryFile("format.cpp", Code); 13395 tooling::Replacements Replaces = toReplacements( 13396 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 13397 "auto "), 13398 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 13399 "nullptr"), 13400 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 13401 "nullptr"), 13402 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 13403 "nullptr")}); 13404 13405 format::FormatStyle Style = format::getLLVMStyle(); 13406 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 13407 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 13408 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 13409 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 13410 auto Result = applyAllReplacements(Code, *FormattedReplaces); 13411 EXPECT_TRUE(static_cast<bool>(Result)); 13412 EXPECT_EQ(Expected, *Result); 13413 } 13414 13415 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 13416 std::string Code = "#include \"a.h\"\n" 13417 "#include \"c.h\"\n" 13418 "\n" 13419 "int main() {\n" 13420 " return 0;\n" 13421 "}"; 13422 std::string Expected = "#include \"a.h\"\n" 13423 "#include \"b.h\"\n" 13424 "#include \"c.h\"\n" 13425 "\n" 13426 "int main() {\n" 13427 " return 0;\n" 13428 "}"; 13429 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 13430 tooling::Replacements Replaces = toReplacements( 13431 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 13432 "#include \"b.h\"\n")}); 13433 13434 format::FormatStyle Style = format::getLLVMStyle(); 13435 Style.SortIncludes = true; 13436 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 13437 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 13438 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 13439 auto Result = applyAllReplacements(Code, *FormattedReplaces); 13440 EXPECT_TRUE(static_cast<bool>(Result)); 13441 EXPECT_EQ(Expected, *Result); 13442 } 13443 13444 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 13445 EXPECT_EQ("using std::cin;\n" 13446 "using std::cout;", 13447 format("using std::cout;\n" 13448 "using std::cin;", getGoogleStyle())); 13449 } 13450 13451 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 13452 format::FormatStyle Style = format::getLLVMStyle(); 13453 Style.Standard = FormatStyle::LS_Cpp03; 13454 // cpp03 recognize this string as identifier u8 and literal character 'a' 13455 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 13456 } 13457 13458 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 13459 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 13460 // all modes, including C++11, C++14 and C++17 13461 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 13462 } 13463 13464 TEST_F(FormatTest, DoNotFormatLikelyXml) { 13465 EXPECT_EQ("<!-- ;> -->", 13466 format("<!-- ;> -->", getGoogleStyle())); 13467 EXPECT_EQ(" <!-- >; -->", 13468 format(" <!-- >; -->", getGoogleStyle())); 13469 } 13470 13471 TEST_F(FormatTest, StructuredBindings) { 13472 // Structured bindings is a C++17 feature. 13473 // all modes, including C++11, C++14 and C++17 13474 verifyFormat("auto [a, b] = f();"); 13475 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 13476 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 13477 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 13478 EXPECT_EQ("auto const volatile [a, b] = f();", 13479 format("auto const volatile[a, b] = f();")); 13480 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 13481 EXPECT_EQ("auto &[a, b, c] = f();", 13482 format("auto &[ a , b,c ] = f();")); 13483 EXPECT_EQ("auto &&[a, b, c] = f();", 13484 format("auto &&[ a , b,c ] = f();")); 13485 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 13486 EXPECT_EQ("auto const volatile &&[a, b] = f();", 13487 format("auto const volatile &&[a, b] = f();")); 13488 EXPECT_EQ("auto const &&[a, b] = f();", format("auto const && [a, b] = f();")); 13489 EXPECT_EQ("const auto &[a, b] = f();", format("const auto & [a, b] = f();")); 13490 EXPECT_EQ("const auto volatile &&[a, b] = f();", 13491 format("const auto volatile &&[a, b] = f();")); 13492 EXPECT_EQ("volatile const auto &&[a, b] = f();", 13493 format("volatile const auto &&[a, b] = f();")); 13494 EXPECT_EQ("const auto &&[a, b] = f();", format("const auto && [a, b] = f();")); 13495 13496 // Make sure we don't mistake structured bindings for lambdas. 13497 FormatStyle PointerMiddle = getLLVMStyle(); 13498 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 13499 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 13500 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 13501 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 13502 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 13503 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 13504 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 13505 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 13506 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 13507 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 13508 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 13509 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 13510 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 13511 13512 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 13513 format("for (const auto && [a, b] : some_range) {\n}")); 13514 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 13515 format("for (const auto & [a, b] : some_range) {\n}")); 13516 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 13517 format("for (const auto[a, b] : some_range) {\n}")); 13518 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 13519 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 13520 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 13521 EXPECT_EQ("auto const &[x, y](expr);", format("auto const & [x,y] (expr);")); 13522 EXPECT_EQ("auto const &&[x, y](expr);", format("auto const && [x,y] (expr);")); 13523 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 13524 EXPECT_EQ("auto const &[x, y]{expr};", format("auto const & [x,y] {expr};")); 13525 EXPECT_EQ("auto const &&[x, y]{expr};", format("auto const && [x,y] {expr};")); 13526 13527 format::FormatStyle Spaces = format::getLLVMStyle(); 13528 Spaces.SpacesInSquareBrackets = true; 13529 verifyFormat("auto [ a, b ] = f();", Spaces); 13530 verifyFormat("auto &&[ a, b ] = f();", Spaces); 13531 verifyFormat("auto &[ a, b ] = f();", Spaces); 13532 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 13533 verifyFormat("auto const &[ a, b ] = f();", Spaces); 13534 } 13535 13536 TEST_F(FormatTest, FileAndCode) { 13537 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 13538 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 13539 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 13540 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 13541 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n")); 13542 EXPECT_EQ( 13543 FormatStyle::LK_ObjC, 13544 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 13545 EXPECT_EQ(FormatStyle::LK_ObjC, 13546 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 13547 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 13548 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 13549 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n")); 13550 EXPECT_EQ(FormatStyle::LK_ObjC, 13551 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 13552 EXPECT_EQ( 13553 FormatStyle::LK_ObjC, 13554 guessLanguage("foo.h", 13555 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 13556 EXPECT_EQ( 13557 FormatStyle::LK_Cpp, 13558 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 13559 } 13560 13561 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 13562 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 13563 EXPECT_EQ(FormatStyle::LK_ObjC, 13564 guessLanguage("foo.h", "array[[calculator getIndex]];")); 13565 EXPECT_EQ(FormatStyle::LK_Cpp, 13566 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 13567 EXPECT_EQ( 13568 FormatStyle::LK_Cpp, 13569 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 13570 EXPECT_EQ(FormatStyle::LK_ObjC, 13571 guessLanguage("foo.h", "[[noreturn foo] bar];")); 13572 EXPECT_EQ(FormatStyle::LK_Cpp, 13573 guessLanguage("foo.h", "[[clang::fallthrough]];")); 13574 EXPECT_EQ(FormatStyle::LK_ObjC, 13575 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 13576 EXPECT_EQ(FormatStyle::LK_Cpp, 13577 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 13578 EXPECT_EQ(FormatStyle::LK_Cpp, 13579 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 13580 EXPECT_EQ(FormatStyle::LK_ObjC, 13581 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 13582 EXPECT_EQ(FormatStyle::LK_Cpp, 13583 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 13584 EXPECT_EQ( 13585 FormatStyle::LK_Cpp, 13586 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 13587 EXPECT_EQ( 13588 FormatStyle::LK_Cpp, 13589 guessLanguage("foo.h", 13590 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 13591 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 13592 } 13593 13594 TEST_F(FormatTest, GuessLanguageWithCaret) { 13595 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 13596 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 13597 EXPECT_EQ(FormatStyle::LK_ObjC, 13598 guessLanguage("foo.h", "int(^)(char, float);")); 13599 EXPECT_EQ(FormatStyle::LK_ObjC, 13600 guessLanguage("foo.h", "int(^foo)(char, float);")); 13601 EXPECT_EQ(FormatStyle::LK_ObjC, 13602 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 13603 EXPECT_EQ(FormatStyle::LK_ObjC, 13604 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 13605 EXPECT_EQ( 13606 FormatStyle::LK_ObjC, 13607 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 13608 } 13609 13610 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 13611 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 13612 "void f() {\n" 13613 " asm (\"mov %[e], %[d]\"\n" 13614 " : [d] \"=rm\" (d)\n" 13615 " [e] \"rm\" (*e));\n" 13616 "}")); 13617 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 13618 "void f() {\n" 13619 " _asm (\"mov %[e], %[d]\"\n" 13620 " : [d] \"=rm\" (d)\n" 13621 " [e] \"rm\" (*e));\n" 13622 "}")); 13623 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 13624 "void f() {\n" 13625 " __asm (\"mov %[e], %[d]\"\n" 13626 " : [d] \"=rm\" (d)\n" 13627 " [e] \"rm\" (*e));\n" 13628 "}")); 13629 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 13630 "void f() {\n" 13631 " __asm__ (\"mov %[e], %[d]\"\n" 13632 " : [d] \"=rm\" (d)\n" 13633 " [e] \"rm\" (*e));\n" 13634 "}")); 13635 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 13636 "void f() {\n" 13637 " asm (\"mov %[e], %[d]\"\n" 13638 " : [d] \"=rm\" (d),\n" 13639 " [e] \"rm\" (*e));\n" 13640 "}")); 13641 EXPECT_EQ(FormatStyle::LK_Cpp, 13642 guessLanguage("foo.h", "void f() {\n" 13643 " asm volatile (\"mov %[e], %[d]\"\n" 13644 " : [d] \"=rm\" (d)\n" 13645 " [e] \"rm\" (*e));\n" 13646 "}")); 13647 } 13648 13649 TEST_F(FormatTest, GuessLanguageWithChildLines) { 13650 EXPECT_EQ(FormatStyle::LK_Cpp, 13651 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 13652 EXPECT_EQ(FormatStyle::LK_ObjC, 13653 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 13654 EXPECT_EQ( 13655 FormatStyle::LK_Cpp, 13656 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 13657 EXPECT_EQ( 13658 FormatStyle::LK_ObjC, 13659 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 13660 } 13661 13662 TEST_F(FormatTest, TypenameMacros) { 13663 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 13664 13665 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 13666 FormatStyle Google = getGoogleStyleWithColumns(0); 13667 Google.TypenameMacros = TypenameMacros; 13668 verifyFormat("struct foo {\n" 13669 " int bar;\n" 13670 " TAILQ_ENTRY(a) bleh;\n" 13671 "};", Google); 13672 13673 FormatStyle Macros = getLLVMStyle(); 13674 Macros.TypenameMacros = TypenameMacros; 13675 13676 verifyFormat("STACK_OF(int) a;", Macros); 13677 verifyFormat("STACK_OF(int) *a;", Macros); 13678 verifyFormat("STACK_OF(int const *) *a;", Macros); 13679 verifyFormat("STACK_OF(int *const) *a;", Macros); 13680 verifyFormat("STACK_OF(int, string) a;", Macros); 13681 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 13682 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 13683 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 13684 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 13685 13686 Macros.PointerAlignment = FormatStyle::PAS_Left; 13687 verifyFormat("STACK_OF(int)* a;", Macros); 13688 verifyFormat("STACK_OF(int*)* a;", Macros); 13689 } 13690 13691 } // end namespace 13692 } // end namespace format 13693 } // end namespace clang 13694