1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "clang/Format/Format.h" 11 12 #include "../Tooling/ReplacementTest.h" 13 #include "FormatTestUtils.h" 14 15 #include "clang/Frontend/TextDiagnosticPrinter.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "gtest/gtest.h" 19 20 #define DEBUG_TYPE "format-test" 21 22 using clang::tooling::ReplacementTest; 23 using clang::tooling::toReplacements; 24 25 namespace clang { 26 namespace format { 27 namespace { 28 29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 30 31 class FormatTest : public ::testing::Test { 32 protected: 33 enum StatusCheck { 34 SC_ExpectComplete, 35 SC_ExpectIncomplete, 36 SC_DoNotCheck 37 }; 38 39 std::string format(llvm::StringRef Code, 40 const FormatStyle &Style = getLLVMStyle(), 41 StatusCheck CheckComplete = SC_ExpectComplete) { 42 LLVM_DEBUG(llvm::errs() << "---\n"); 43 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 44 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 45 FormattingAttemptStatus Status; 46 tooling::Replacements Replaces = 47 reformat(Style, Code, Ranges, "<stdin>", &Status); 48 if (CheckComplete != SC_DoNotCheck) { 49 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 50 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 51 << Code << "\n\n"; 52 } 53 ReplacementCount = Replaces.size(); 54 auto Result = applyAllReplacements(Code, Replaces); 55 EXPECT_TRUE(static_cast<bool>(Result)); 56 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 57 return *Result; 58 } 59 60 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 61 Style.ColumnLimit = ColumnLimit; 62 return Style; 63 } 64 65 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 66 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 67 } 68 69 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 70 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 71 } 72 73 void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code, 74 const FormatStyle &Style = getLLVMStyle()) { 75 EXPECT_EQ(Expected.str(), format(Expected, Style)) 76 << "Expected code is not stable"; 77 EXPECT_EQ(Expected.str(), format(Code, Style)); 78 if (Style.Language == FormatStyle::LK_Cpp) { 79 // Objective-C++ is a superset of C++, so everything checked for C++ 80 // needs to be checked for Objective-C++ as well. 81 FormatStyle ObjCStyle = Style; 82 ObjCStyle.Language = FormatStyle::LK_ObjC; 83 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 84 } 85 } 86 87 void verifyFormat(llvm::StringRef Code, 88 const FormatStyle &Style = getLLVMStyle()) { 89 verifyFormat(Code, test::messUp(Code), Style); 90 } 91 92 void verifyIncompleteFormat(llvm::StringRef Code, 93 const FormatStyle &Style = getLLVMStyle()) { 94 EXPECT_EQ(Code.str(), 95 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 96 } 97 98 void verifyGoogleFormat(llvm::StringRef Code) { 99 verifyFormat(Code, getGoogleStyle()); 100 } 101 102 void verifyIndependentOfContext(llvm::StringRef text) { 103 verifyFormat(text); 104 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 105 } 106 107 /// \brief Verify that clang-format does not crash on the given input. 108 void verifyNoCrash(llvm::StringRef Code, 109 const FormatStyle &Style = getLLVMStyle()) { 110 format(Code, Style, SC_DoNotCheck); 111 } 112 113 int ReplacementCount; 114 }; 115 116 TEST_F(FormatTest, MessUp) { 117 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 118 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 119 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 120 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 121 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 122 } 123 124 //===----------------------------------------------------------------------===// 125 // Basic function tests. 126 //===----------------------------------------------------------------------===// 127 128 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 129 EXPECT_EQ(";", format(";")); 130 } 131 132 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 133 EXPECT_EQ("int i;", format(" int i;")); 134 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 135 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 136 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 137 } 138 139 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 140 EXPECT_EQ("int i;", format("int\ni;")); 141 } 142 143 TEST_F(FormatTest, FormatsNestedBlockStatements) { 144 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 145 } 146 147 TEST_F(FormatTest, FormatsNestedCall) { 148 verifyFormat("Method(f1, f2(f3));"); 149 verifyFormat("Method(f1(f2, f3()));"); 150 verifyFormat("Method(f1(f2, (f3())));"); 151 } 152 153 TEST_F(FormatTest, NestedNameSpecifiers) { 154 verifyFormat("vector<::Type> v;"); 155 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 156 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 157 verifyFormat("bool a = 2 < ::SomeFunction();"); 158 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 159 verifyFormat("some::string getName();"); 160 } 161 162 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 163 EXPECT_EQ("if (a) {\n" 164 " f();\n" 165 "}", 166 format("if(a){f();}")); 167 EXPECT_EQ(4, ReplacementCount); 168 EXPECT_EQ("if (a) {\n" 169 " f();\n" 170 "}", 171 format("if (a) {\n" 172 " f();\n" 173 "}")); 174 EXPECT_EQ(0, ReplacementCount); 175 EXPECT_EQ("/*\r\n" 176 "\r\n" 177 "*/\r\n", 178 format("/*\r\n" 179 "\r\n" 180 "*/\r\n")); 181 EXPECT_EQ(0, ReplacementCount); 182 } 183 184 TEST_F(FormatTest, RemovesEmptyLines) { 185 EXPECT_EQ("class C {\n" 186 " int i;\n" 187 "};", 188 format("class C {\n" 189 " int i;\n" 190 "\n" 191 "};")); 192 193 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 194 EXPECT_EQ("namespace N {\n" 195 "\n" 196 "int i;\n" 197 "}", 198 format("namespace N {\n" 199 "\n" 200 "int i;\n" 201 "}", 202 getGoogleStyle())); 203 EXPECT_EQ("/* something */ namespace N {\n" 204 "\n" 205 "int i;\n" 206 "}", 207 format("/* something */ namespace N {\n" 208 "\n" 209 "int i;\n" 210 "}", 211 getGoogleStyle())); 212 EXPECT_EQ("inline namespace N {\n" 213 "\n" 214 "int i;\n" 215 "}", 216 format("inline namespace N {\n" 217 "\n" 218 "int i;\n" 219 "}", 220 getGoogleStyle())); 221 EXPECT_EQ("/* something */ inline namespace N {\n" 222 "\n" 223 "int i;\n" 224 "}", 225 format("/* something */ inline namespace N {\n" 226 "\n" 227 "int i;\n" 228 "}", 229 getGoogleStyle())); 230 EXPECT_EQ("export namespace N {\n" 231 "\n" 232 "int i;\n" 233 "}", 234 format("export namespace N {\n" 235 "\n" 236 "int i;\n" 237 "}", 238 getGoogleStyle())); 239 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 240 "\n" 241 "int i;\n" 242 "}", 243 format("extern /**/ \"C\" /**/ {\n" 244 "\n" 245 "int i;\n" 246 "}", 247 getGoogleStyle())); 248 249 // ...but do keep inlining and removing empty lines for non-block extern "C" 250 // functions. 251 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 252 EXPECT_EQ("extern \"C\" int f() {\n" 253 " int i = 42;\n" 254 " return i;\n" 255 "}", 256 format("extern \"C\" int f() {\n" 257 "\n" 258 " int i = 42;\n" 259 " return i;\n" 260 "}", 261 getGoogleStyle())); 262 263 // Remove empty lines at the beginning and end of blocks. 264 EXPECT_EQ("void f() {\n" 265 "\n" 266 " if (a) {\n" 267 "\n" 268 " f();\n" 269 " }\n" 270 "}", 271 format("void f() {\n" 272 "\n" 273 " if (a) {\n" 274 "\n" 275 " f();\n" 276 "\n" 277 " }\n" 278 "\n" 279 "}", 280 getLLVMStyle())); 281 EXPECT_EQ("void f() {\n" 282 " if (a) {\n" 283 " f();\n" 284 " }\n" 285 "}", 286 format("void f() {\n" 287 "\n" 288 " if (a) {\n" 289 "\n" 290 " f();\n" 291 "\n" 292 " }\n" 293 "\n" 294 "}", 295 getGoogleStyle())); 296 297 // Don't remove empty lines in more complex control statements. 298 EXPECT_EQ("void f() {\n" 299 " if (a) {\n" 300 " f();\n" 301 "\n" 302 " } else if (b) {\n" 303 " f();\n" 304 " }\n" 305 "}", 306 format("void f() {\n" 307 " if (a) {\n" 308 " f();\n" 309 "\n" 310 " } else if (b) {\n" 311 " f();\n" 312 "\n" 313 " }\n" 314 "\n" 315 "}")); 316 317 // Don't remove empty lines before namespace endings. 318 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 319 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 320 EXPECT_EQ("namespace {\n" 321 "int i;\n" 322 "\n" 323 "}", 324 format("namespace {\n" 325 "int i;\n" 326 "\n" 327 "}", LLVMWithNoNamespaceFix)); 328 EXPECT_EQ("namespace {\n" 329 "int i;\n" 330 "}", 331 format("namespace {\n" 332 "int i;\n" 333 "}", LLVMWithNoNamespaceFix)); 334 EXPECT_EQ("namespace {\n" 335 "int i;\n" 336 "\n" 337 "};", 338 format("namespace {\n" 339 "int i;\n" 340 "\n" 341 "};", LLVMWithNoNamespaceFix)); 342 EXPECT_EQ("namespace {\n" 343 "int i;\n" 344 "};", 345 format("namespace {\n" 346 "int i;\n" 347 "};", LLVMWithNoNamespaceFix)); 348 EXPECT_EQ("namespace {\n" 349 "int i;\n" 350 "\n" 351 "}", 352 format("namespace {\n" 353 "int i;\n" 354 "\n" 355 "}")); 356 EXPECT_EQ("namespace {\n" 357 "int i;\n" 358 "\n" 359 "} // namespace", 360 format("namespace {\n" 361 "int i;\n" 362 "\n" 363 "} // namespace")); 364 365 FormatStyle Style = getLLVMStyle(); 366 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 367 Style.MaxEmptyLinesToKeep = 2; 368 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 369 Style.BraceWrapping.AfterClass = true; 370 Style.BraceWrapping.AfterFunction = true; 371 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 372 373 EXPECT_EQ("class Foo\n" 374 "{\n" 375 " Foo() {}\n" 376 "\n" 377 " void funk() {}\n" 378 "};", 379 format("class Foo\n" 380 "{\n" 381 " Foo()\n" 382 " {\n" 383 " }\n" 384 "\n" 385 " void funk() {}\n" 386 "};", 387 Style)); 388 } 389 390 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 391 verifyFormat("x = (a) and (b);"); 392 verifyFormat("x = (a) or (b);"); 393 verifyFormat("x = (a) bitand (b);"); 394 verifyFormat("x = (a) bitor (b);"); 395 verifyFormat("x = (a) not_eq (b);"); 396 verifyFormat("x = (a) and_eq (b);"); 397 verifyFormat("x = (a) or_eq (b);"); 398 verifyFormat("x = (a) xor (b);"); 399 } 400 401 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 402 verifyFormat("x = compl(a);"); 403 verifyFormat("x = not(a);"); 404 verifyFormat("x = bitand(a);"); 405 // Unary operator must not be merged with the next identifier 406 verifyFormat("x = compl a;"); 407 verifyFormat("x = not a;"); 408 verifyFormat("x = bitand a;"); 409 } 410 411 //===----------------------------------------------------------------------===// 412 // Tests for control statements. 413 //===----------------------------------------------------------------------===// 414 415 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 416 verifyFormat("if (true)\n f();\ng();"); 417 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 418 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 419 verifyFormat("if constexpr (true)\n" 420 " f();\ng();"); 421 verifyFormat("if constexpr (a)\n" 422 " if constexpr (b)\n" 423 " if constexpr (c)\n" 424 " g();\n" 425 "h();"); 426 verifyFormat("if constexpr (a)\n" 427 " if constexpr (b) {\n" 428 " f();\n" 429 " }\n" 430 "g();"); 431 432 FormatStyle AllowsMergedIf = getLLVMStyle(); 433 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 434 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 435 verifyFormat("if (a)\n" 436 " // comment\n" 437 " f();", 438 AllowsMergedIf); 439 verifyFormat("{\n" 440 " if (a)\n" 441 " label:\n" 442 " f();\n" 443 "}", 444 AllowsMergedIf); 445 verifyFormat("#define A \\\n" 446 " if (a) \\\n" 447 " label: \\\n" 448 " f()", 449 AllowsMergedIf); 450 verifyFormat("if (a)\n" 451 " ;", 452 AllowsMergedIf); 453 verifyFormat("if (a)\n" 454 " if (b) return;", 455 AllowsMergedIf); 456 457 verifyFormat("if (a) // Can't merge this\n" 458 " f();\n", 459 AllowsMergedIf); 460 verifyFormat("if (a) /* still don't merge */\n" 461 " f();", 462 AllowsMergedIf); 463 verifyFormat("if (a) { // Never merge this\n" 464 " f();\n" 465 "}", 466 AllowsMergedIf); 467 verifyFormat("if (a) { /* Never merge this */\n" 468 " f();\n" 469 "}", 470 AllowsMergedIf); 471 472 AllowsMergedIf.ColumnLimit = 14; 473 verifyFormat("if (a) return;", AllowsMergedIf); 474 verifyFormat("if (aaaaaaaaa)\n" 475 " return;", 476 AllowsMergedIf); 477 478 AllowsMergedIf.ColumnLimit = 13; 479 verifyFormat("if (a)\n return;", AllowsMergedIf); 480 } 481 482 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 483 FormatStyle AllowsMergedLoops = getLLVMStyle(); 484 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 485 verifyFormat("while (true) continue;", AllowsMergedLoops); 486 verifyFormat("for (;;) continue;", AllowsMergedLoops); 487 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 488 verifyFormat("while (true)\n" 489 " ;", 490 AllowsMergedLoops); 491 verifyFormat("for (;;)\n" 492 " ;", 493 AllowsMergedLoops); 494 verifyFormat("for (;;)\n" 495 " for (;;) continue;", 496 AllowsMergedLoops); 497 verifyFormat("for (;;) // Can't merge this\n" 498 " continue;", 499 AllowsMergedLoops); 500 verifyFormat("for (;;) /* still don't merge */\n" 501 " continue;", 502 AllowsMergedLoops); 503 } 504 505 TEST_F(FormatTest, FormatShortBracedStatements) { 506 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 507 AllowSimpleBracedStatements.ColumnLimit = 40; 508 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 509 510 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 511 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 512 513 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 514 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 515 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 516 517 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 518 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 519 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 520 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 521 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 522 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 523 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 524 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 525 verifyFormat("if (true) {\n" 526 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 527 "}", 528 AllowSimpleBracedStatements); 529 verifyFormat("if (true) { //\n" 530 " f();\n" 531 "}", 532 AllowSimpleBracedStatements); 533 verifyFormat("if (true) {\n" 534 " f();\n" 535 " f();\n" 536 "}", 537 AllowSimpleBracedStatements); 538 verifyFormat("if (true) {\n" 539 " f();\n" 540 "} else {\n" 541 " f();\n" 542 "}", 543 AllowSimpleBracedStatements); 544 545 verifyFormat("struct A2 {\n" 546 " int X;\n" 547 "};", 548 AllowSimpleBracedStatements); 549 verifyFormat("typedef struct A2 {\n" 550 " int X;\n" 551 "} A2_t;", 552 AllowSimpleBracedStatements); 553 verifyFormat("template <int> struct A2 {\n" 554 " struct B {};\n" 555 "};", 556 AllowSimpleBracedStatements); 557 558 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 559 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 560 verifyFormat("if (true) {\n" 561 " f();\n" 562 "}", 563 AllowSimpleBracedStatements); 564 verifyFormat("if (true) {\n" 565 " f();\n" 566 "} else {\n" 567 " f();\n" 568 "}", 569 AllowSimpleBracedStatements); 570 571 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 572 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 573 verifyFormat("while (true) {\n" 574 " f();\n" 575 "}", 576 AllowSimpleBracedStatements); 577 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 578 verifyFormat("for (;;) {\n" 579 " f();\n" 580 "}", 581 AllowSimpleBracedStatements); 582 583 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 584 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 585 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true; 586 587 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 588 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 589 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 590 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 591 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 592 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 593 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 594 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 595 verifyFormat("if (true)\n" 596 "{\n" 597 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 598 "}", 599 AllowSimpleBracedStatements); 600 verifyFormat("if (true)\n" 601 "{ //\n" 602 " f();\n" 603 "}", 604 AllowSimpleBracedStatements); 605 verifyFormat("if (true)\n" 606 "{\n" 607 " f();\n" 608 " f();\n" 609 "}", 610 AllowSimpleBracedStatements); 611 verifyFormat("if (true)\n" 612 "{\n" 613 " f();\n" 614 "} else\n" 615 "{\n" 616 " f();\n" 617 "}", 618 AllowSimpleBracedStatements); 619 620 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 621 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 622 verifyFormat("if (true)\n" 623 "{\n" 624 " f();\n" 625 "}", 626 AllowSimpleBracedStatements); 627 verifyFormat("if (true)\n" 628 "{\n" 629 " f();\n" 630 "} else\n" 631 "{\n" 632 " f();\n" 633 "}", 634 AllowSimpleBracedStatements); 635 636 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 637 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 638 verifyFormat("while (true)\n" 639 "{\n" 640 " f();\n" 641 "}", 642 AllowSimpleBracedStatements); 643 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 644 verifyFormat("for (;;)\n" 645 "{\n" 646 " f();\n" 647 "}", 648 AllowSimpleBracedStatements); 649 } 650 651 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 652 FormatStyle Style = getLLVMStyleWithColumns(60); 653 Style.AllowShortBlocksOnASingleLine = true; 654 Style.AllowShortIfStatementsOnASingleLine = true; 655 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 656 EXPECT_EQ("#define A \\\n" 657 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 658 " { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n" 659 "X;", 660 format("#define A \\\n" 661 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 662 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 663 " }\n" 664 "X;", 665 Style)); 666 } 667 668 TEST_F(FormatTest, ParseIfElse) { 669 verifyFormat("if (true)\n" 670 " if (true)\n" 671 " if (true)\n" 672 " f();\n" 673 " else\n" 674 " g();\n" 675 " else\n" 676 " h();\n" 677 "else\n" 678 " i();"); 679 verifyFormat("if (true)\n" 680 " if (true)\n" 681 " if (true) {\n" 682 " if (true)\n" 683 " f();\n" 684 " } else {\n" 685 " g();\n" 686 " }\n" 687 " else\n" 688 " h();\n" 689 "else {\n" 690 " i();\n" 691 "}"); 692 verifyFormat("if (true)\n" 693 " if constexpr (true)\n" 694 " if (true) {\n" 695 " if constexpr (true)\n" 696 " f();\n" 697 " } else {\n" 698 " g();\n" 699 " }\n" 700 " else\n" 701 " h();\n" 702 "else {\n" 703 " i();\n" 704 "}"); 705 verifyFormat("void f() {\n" 706 " if (a) {\n" 707 " } else {\n" 708 " }\n" 709 "}"); 710 } 711 712 TEST_F(FormatTest, ElseIf) { 713 verifyFormat("if (a) {\n} else if (b) {\n}"); 714 verifyFormat("if (a)\n" 715 " f();\n" 716 "else if (b)\n" 717 " g();\n" 718 "else\n" 719 " h();"); 720 verifyFormat("if constexpr (a)\n" 721 " f();\n" 722 "else if constexpr (b)\n" 723 " g();\n" 724 "else\n" 725 " h();"); 726 verifyFormat("if (a) {\n" 727 " f();\n" 728 "}\n" 729 "// or else ..\n" 730 "else {\n" 731 " g()\n" 732 "}"); 733 734 verifyFormat("if (a) {\n" 735 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 737 "}"); 738 verifyFormat("if (a) {\n" 739 "} else if (\n" 740 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 741 "}", 742 getLLVMStyleWithColumns(62)); 743 verifyFormat("if (a) {\n" 744 "} else if constexpr (\n" 745 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 746 "}", 747 getLLVMStyleWithColumns(62)); 748 } 749 750 TEST_F(FormatTest, FormatsForLoop) { 751 verifyFormat( 752 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 753 " ++VeryVeryLongLoopVariable)\n" 754 " ;"); 755 verifyFormat("for (;;)\n" 756 " f();"); 757 verifyFormat("for (;;) {\n}"); 758 verifyFormat("for (;;) {\n" 759 " f();\n" 760 "}"); 761 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 762 763 verifyFormat( 764 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 765 " E = UnwrappedLines.end();\n" 766 " I != E; ++I) {\n}"); 767 768 verifyFormat( 769 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 770 " ++IIIII) {\n}"); 771 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 772 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 773 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 774 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 775 " I = FD->getDeclsInPrototypeScope().begin(),\n" 776 " E = FD->getDeclsInPrototypeScope().end();\n" 777 " I != E; ++I) {\n}"); 778 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 779 " I = Container.begin(),\n" 780 " E = Container.end();\n" 781 " I != E; ++I) {\n}", 782 getLLVMStyleWithColumns(76)); 783 784 verifyFormat( 785 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 786 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 787 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 788 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 789 " ++aaaaaaaaaaa) {\n}"); 790 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 791 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 792 " ++i) {\n}"); 793 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 794 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 795 "}"); 796 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 797 " aaaaaaaaaa);\n" 798 " iter; ++iter) {\n" 799 "}"); 800 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 801 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 802 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 803 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 804 805 // These should not be formatted as Objective-C for-in loops. 806 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 807 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 808 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 809 verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 810 811 FormatStyle NoBinPacking = getLLVMStyle(); 812 NoBinPacking.BinPackParameters = false; 813 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 814 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 815 " aaaaaaaaaaaaaaaa,\n" 816 " aaaaaaaaaaaaaaaa,\n" 817 " aaaaaaaaaaaaaaaa);\n" 818 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 819 "}", 820 NoBinPacking); 821 verifyFormat( 822 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 823 " E = UnwrappedLines.end();\n" 824 " I != E;\n" 825 " ++I) {\n}", 826 NoBinPacking); 827 828 FormatStyle AlignLeft = getLLVMStyle(); 829 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 830 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 831 } 832 833 TEST_F(FormatTest, RangeBasedForLoops) { 834 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 835 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 836 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 837 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 838 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 839 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 840 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 841 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 842 } 843 844 TEST_F(FormatTest, ForEachLoops) { 845 verifyFormat("void f() {\n" 846 " foreach (Item *item, itemlist) {}\n" 847 " Q_FOREACH (Item *item, itemlist) {}\n" 848 " BOOST_FOREACH (Item *item, itemlist) {}\n" 849 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 850 "}"); 851 852 // As function-like macros. 853 verifyFormat("#define foreach(x, y)\n" 854 "#define Q_FOREACH(x, y)\n" 855 "#define BOOST_FOREACH(x, y)\n" 856 "#define UNKNOWN_FOREACH(x, y)\n"); 857 858 // Not as function-like macros. 859 verifyFormat("#define foreach (x, y)\n" 860 "#define Q_FOREACH (x, y)\n" 861 "#define BOOST_FOREACH (x, y)\n" 862 "#define UNKNOWN_FOREACH (x, y)\n"); 863 } 864 865 TEST_F(FormatTest, FormatsWhileLoop) { 866 verifyFormat("while (true) {\n}"); 867 verifyFormat("while (true)\n" 868 " f();"); 869 verifyFormat("while () {\n}"); 870 verifyFormat("while () {\n" 871 " f();\n" 872 "}"); 873 } 874 875 TEST_F(FormatTest, FormatsDoWhile) { 876 verifyFormat("do {\n" 877 " do_something();\n" 878 "} while (something());"); 879 verifyFormat("do\n" 880 " do_something();\n" 881 "while (something());"); 882 } 883 884 TEST_F(FormatTest, FormatsSwitchStatement) { 885 verifyFormat("switch (x) {\n" 886 "case 1:\n" 887 " f();\n" 888 " break;\n" 889 "case kFoo:\n" 890 "case ns::kBar:\n" 891 "case kBaz:\n" 892 " break;\n" 893 "default:\n" 894 " g();\n" 895 " break;\n" 896 "}"); 897 verifyFormat("switch (x) {\n" 898 "case 1: {\n" 899 " f();\n" 900 " break;\n" 901 "}\n" 902 "case 2: {\n" 903 " break;\n" 904 "}\n" 905 "}"); 906 verifyFormat("switch (x) {\n" 907 "case 1: {\n" 908 " f();\n" 909 " {\n" 910 " g();\n" 911 " h();\n" 912 " }\n" 913 " break;\n" 914 "}\n" 915 "}"); 916 verifyFormat("switch (x) {\n" 917 "case 1: {\n" 918 " f();\n" 919 " if (foo) {\n" 920 " g();\n" 921 " h();\n" 922 " }\n" 923 " break;\n" 924 "}\n" 925 "}"); 926 verifyFormat("switch (x) {\n" 927 "case 1: {\n" 928 " f();\n" 929 " g();\n" 930 "} break;\n" 931 "}"); 932 verifyFormat("switch (test)\n" 933 " ;"); 934 verifyFormat("switch (x) {\n" 935 "default: {\n" 936 " // Do nothing.\n" 937 "}\n" 938 "}"); 939 verifyFormat("switch (x) {\n" 940 "// comment\n" 941 "// if 1, do f()\n" 942 "case 1:\n" 943 " f();\n" 944 "}"); 945 verifyFormat("switch (x) {\n" 946 "case 1:\n" 947 " // Do amazing stuff\n" 948 " {\n" 949 " f();\n" 950 " g();\n" 951 " }\n" 952 " break;\n" 953 "}"); 954 verifyFormat("#define A \\\n" 955 " switch (x) { \\\n" 956 " case a: \\\n" 957 " foo = b; \\\n" 958 " }", 959 getLLVMStyleWithColumns(20)); 960 verifyFormat("#define OPERATION_CASE(name) \\\n" 961 " case OP_name: \\\n" 962 " return operations::Operation##name\n", 963 getLLVMStyleWithColumns(40)); 964 verifyFormat("switch (x) {\n" 965 "case 1:;\n" 966 "default:;\n" 967 " int i;\n" 968 "}"); 969 970 verifyGoogleFormat("switch (x) {\n" 971 " case 1:\n" 972 " f();\n" 973 " break;\n" 974 " case kFoo:\n" 975 " case ns::kBar:\n" 976 " case kBaz:\n" 977 " break;\n" 978 " default:\n" 979 " g();\n" 980 " break;\n" 981 "}"); 982 verifyGoogleFormat("switch (x) {\n" 983 " case 1: {\n" 984 " f();\n" 985 " break;\n" 986 " }\n" 987 "}"); 988 verifyGoogleFormat("switch (test)\n" 989 " ;"); 990 991 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 992 " case OP_name: \\\n" 993 " return operations::Operation##name\n"); 994 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 995 " // Get the correction operation class.\n" 996 " switch (OpCode) {\n" 997 " CASE(Add);\n" 998 " CASE(Subtract);\n" 999 " default:\n" 1000 " return operations::Unknown;\n" 1001 " }\n" 1002 "#undef OPERATION_CASE\n" 1003 "}"); 1004 verifyFormat("DEBUG({\n" 1005 " switch (x) {\n" 1006 " case A:\n" 1007 " f();\n" 1008 " break;\n" 1009 " // fallthrough\n" 1010 " case B:\n" 1011 " g();\n" 1012 " break;\n" 1013 " }\n" 1014 "});"); 1015 EXPECT_EQ("DEBUG({\n" 1016 " switch (x) {\n" 1017 " case A:\n" 1018 " f();\n" 1019 " break;\n" 1020 " // On B:\n" 1021 " case B:\n" 1022 " g();\n" 1023 " break;\n" 1024 " }\n" 1025 "});", 1026 format("DEBUG({\n" 1027 " switch (x) {\n" 1028 " case A:\n" 1029 " f();\n" 1030 " break;\n" 1031 " // On B:\n" 1032 " case B:\n" 1033 " g();\n" 1034 " break;\n" 1035 " }\n" 1036 "});", 1037 getLLVMStyle())); 1038 EXPECT_EQ("switch (n) {\n" 1039 "case 0: {\n" 1040 " return false;\n" 1041 "}\n" 1042 "default: {\n" 1043 " return true;\n" 1044 "}\n" 1045 "}", 1046 format("switch (n)\n" 1047 "{\n" 1048 "case 0: {\n" 1049 " return false;\n" 1050 "}\n" 1051 "default: {\n" 1052 " return true;\n" 1053 "}\n" 1054 "}", 1055 getLLVMStyle())); 1056 verifyFormat("switch (a) {\n" 1057 "case (b):\n" 1058 " return;\n" 1059 "}"); 1060 1061 verifyFormat("switch (a) {\n" 1062 "case some_namespace::\n" 1063 " some_constant:\n" 1064 " return;\n" 1065 "}", 1066 getLLVMStyleWithColumns(34)); 1067 1068 FormatStyle Style = getLLVMStyle(); 1069 Style.IndentCaseLabels = true; 1070 Style.AllowShortBlocksOnASingleLine = false; 1071 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1072 Style.BraceWrapping.AfterControlStatement = true; 1073 EXPECT_EQ("switch (n)\n" 1074 "{\n" 1075 " case 0:\n" 1076 " {\n" 1077 " return false;\n" 1078 " }\n" 1079 " default:\n" 1080 " {\n" 1081 " return true;\n" 1082 " }\n" 1083 "}", 1084 format("switch (n) {\n" 1085 " case 0: {\n" 1086 " return false;\n" 1087 " }\n" 1088 " default: {\n" 1089 " return true;\n" 1090 " }\n" 1091 "}", 1092 Style)); 1093 } 1094 1095 TEST_F(FormatTest, CaseRanges) { 1096 verifyFormat("switch (x) {\n" 1097 "case 'A' ... 'Z':\n" 1098 "case 1 ... 5:\n" 1099 "case a ... b:\n" 1100 " break;\n" 1101 "}"); 1102 } 1103 1104 TEST_F(FormatTest, ShortCaseLabels) { 1105 FormatStyle Style = getLLVMStyle(); 1106 Style.AllowShortCaseLabelsOnASingleLine = true; 1107 verifyFormat("switch (a) {\n" 1108 "case 1: x = 1; break;\n" 1109 "case 2: return;\n" 1110 "case 3:\n" 1111 "case 4:\n" 1112 "case 5: return;\n" 1113 "case 6: // comment\n" 1114 " return;\n" 1115 "case 7:\n" 1116 " // comment\n" 1117 " return;\n" 1118 "case 8:\n" 1119 " x = 8; // comment\n" 1120 " break;\n" 1121 "default: y = 1; break;\n" 1122 "}", 1123 Style); 1124 verifyFormat("switch (a) {\n" 1125 "case 0: return; // comment\n" 1126 "case 1: break; // comment\n" 1127 "case 2: return;\n" 1128 "// comment\n" 1129 "case 3: return;\n" 1130 "// comment 1\n" 1131 "// comment 2\n" 1132 "// comment 3\n" 1133 "case 4: break; /* comment */\n" 1134 "case 5:\n" 1135 " // comment\n" 1136 " break;\n" 1137 "case 6: /* comment */ x = 1; break;\n" 1138 "case 7: x = /* comment */ 1; break;\n" 1139 "case 8:\n" 1140 " x = 1; /* comment */\n" 1141 " break;\n" 1142 "case 9:\n" 1143 " break; // comment line 1\n" 1144 " // comment line 2\n" 1145 "}", 1146 Style); 1147 EXPECT_EQ("switch (a) {\n" 1148 "case 1:\n" 1149 " x = 8;\n" 1150 " // fall through\n" 1151 "case 2: x = 8;\n" 1152 "// comment\n" 1153 "case 3:\n" 1154 " return; /* comment line 1\n" 1155 " * comment line 2 */\n" 1156 "case 4: i = 8;\n" 1157 "// something else\n" 1158 "#if FOO\n" 1159 "case 5: break;\n" 1160 "#endif\n" 1161 "}", 1162 format("switch (a) {\n" 1163 "case 1: x = 8;\n" 1164 " // fall through\n" 1165 "case 2:\n" 1166 " x = 8;\n" 1167 "// comment\n" 1168 "case 3:\n" 1169 " return; /* comment line 1\n" 1170 " * comment line 2 */\n" 1171 "case 4:\n" 1172 " i = 8;\n" 1173 "// something else\n" 1174 "#if FOO\n" 1175 "case 5: break;\n" 1176 "#endif\n" 1177 "}", 1178 Style)); 1179 EXPECT_EQ("switch (a) {\n" "case 0:\n" 1180 " return; // long long long long long long long long long long long long comment\n" 1181 " // line\n" "}", 1182 format("switch (a) {\n" 1183 "case 0: return; // long long long long long long long long long long long long comment line\n" 1184 "}", 1185 Style)); 1186 EXPECT_EQ("switch (a) {\n" 1187 "case 0:\n" 1188 " return; /* long long long long long long long long long long long long comment\n" 1189 " line */\n" 1190 "}", 1191 format("switch (a) {\n" 1192 "case 0: return; /* long long long long long long long long long long long long comment line */\n" 1193 "}", 1194 Style)); 1195 verifyFormat("switch (a) {\n" 1196 "#if FOO\n" 1197 "case 0: return 0;\n" 1198 "#endif\n" 1199 "}", 1200 Style); 1201 verifyFormat("switch (a) {\n" 1202 "case 1: {\n" 1203 "}\n" 1204 "case 2: {\n" 1205 " return;\n" 1206 "}\n" 1207 "case 3: {\n" 1208 " x = 1;\n" 1209 " return;\n" 1210 "}\n" 1211 "case 4:\n" 1212 " if (x)\n" 1213 " return;\n" 1214 "}", 1215 Style); 1216 Style.ColumnLimit = 21; 1217 verifyFormat("switch (a) {\n" 1218 "case 1: x = 1; break;\n" 1219 "case 2: return;\n" 1220 "case 3:\n" 1221 "case 4:\n" 1222 "case 5: return;\n" 1223 "default:\n" 1224 " y = 1;\n" 1225 " break;\n" 1226 "}", 1227 Style); 1228 Style.ColumnLimit = 80; 1229 Style.AllowShortCaseLabelsOnASingleLine = false; 1230 Style.IndentCaseLabels = true; 1231 EXPECT_EQ("switch (n) {\n" 1232 " default /*comments*/:\n" 1233 " return true;\n" 1234 " case 0:\n" 1235 " return false;\n" 1236 "}", 1237 format("switch (n) {\n" 1238 "default/*comments*/:\n" 1239 " return true;\n" 1240 "case 0:\n" 1241 " return false;\n" 1242 "}", 1243 Style)); 1244 Style.AllowShortCaseLabelsOnASingleLine = true; 1245 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1246 Style.BraceWrapping.AfterControlStatement = true; 1247 EXPECT_EQ("switch (n)\n" 1248 "{\n" 1249 " case 0:\n" 1250 " {\n" 1251 " return false;\n" 1252 " }\n" 1253 " default:\n" 1254 " {\n" 1255 " return true;\n" 1256 " }\n" 1257 "}", 1258 format("switch (n) {\n" 1259 " case 0: {\n" 1260 " return false;\n" 1261 " }\n" 1262 " default:\n" 1263 " {\n" 1264 " return true;\n" 1265 " }\n" 1266 "}", 1267 Style)); 1268 } 1269 1270 TEST_F(FormatTest, FormatsLabels) { 1271 verifyFormat("void f() {\n" 1272 " some_code();\n" 1273 "test_label:\n" 1274 " some_other_code();\n" 1275 " {\n" 1276 " some_more_code();\n" 1277 " another_label:\n" 1278 " some_more_code();\n" 1279 " }\n" 1280 "}"); 1281 verifyFormat("{\n" 1282 " some_code();\n" 1283 "test_label:\n" 1284 " some_other_code();\n" 1285 "}"); 1286 verifyFormat("{\n" 1287 " some_code();\n" 1288 "test_label:;\n" 1289 " int i = 0;\n" 1290 "}"); 1291 } 1292 1293 //===----------------------------------------------------------------------===// 1294 // Tests for classes, namespaces, etc. 1295 //===----------------------------------------------------------------------===// 1296 1297 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1298 verifyFormat("class A {};"); 1299 } 1300 1301 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1302 verifyFormat("class A {\n" 1303 "public:\n" 1304 "public: // comment\n" 1305 "protected:\n" 1306 "private:\n" 1307 " void f() {}\n" 1308 "};"); 1309 verifyFormat("export class A {\n" 1310 "public:\n" 1311 "public: // comment\n" 1312 "protected:\n" 1313 "private:\n" 1314 " void f() {}\n" 1315 "};"); 1316 verifyGoogleFormat("class A {\n" 1317 " public:\n" 1318 " protected:\n" 1319 " private:\n" 1320 " void f() {}\n" 1321 "};"); 1322 verifyGoogleFormat("export class A {\n" 1323 " public:\n" 1324 " protected:\n" 1325 " private:\n" 1326 " void f() {}\n" 1327 "};"); 1328 verifyFormat("class A {\n" 1329 "public slots:\n" 1330 " void f1() {}\n" 1331 "public Q_SLOTS:\n" 1332 " void f2() {}\n" 1333 "protected slots:\n" 1334 " void f3() {}\n" 1335 "protected Q_SLOTS:\n" 1336 " void f4() {}\n" 1337 "private slots:\n" 1338 " void f5() {}\n" 1339 "private Q_SLOTS:\n" 1340 " void f6() {}\n" 1341 "signals:\n" 1342 " void g1();\n" 1343 "Q_SIGNALS:\n" 1344 " void g2();\n" 1345 "};"); 1346 1347 // Don't interpret 'signals' the wrong way. 1348 verifyFormat("signals.set();"); 1349 verifyFormat("for (Signals signals : f()) {\n}"); 1350 verifyFormat("{\n" 1351 " signals.set(); // This needs indentation.\n" 1352 "}"); 1353 verifyFormat("void f() {\n" 1354 "label:\n" 1355 " signals.baz();\n" 1356 "}"); 1357 } 1358 1359 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1360 EXPECT_EQ("class A {\n" 1361 "public:\n" 1362 " void f();\n" 1363 "\n" 1364 "private:\n" 1365 " void g() {}\n" 1366 " // test\n" 1367 "protected:\n" 1368 " int h;\n" 1369 "};", 1370 format("class A {\n" 1371 "public:\n" 1372 "void f();\n" 1373 "private:\n" 1374 "void g() {}\n" 1375 "// test\n" 1376 "protected:\n" 1377 "int h;\n" 1378 "};")); 1379 EXPECT_EQ("class A {\n" 1380 "protected:\n" 1381 "public:\n" 1382 " void f();\n" 1383 "};", 1384 format("class A {\n" 1385 "protected:\n" 1386 "\n" 1387 "public:\n" 1388 "\n" 1389 " void f();\n" 1390 "};")); 1391 1392 // Even ensure proper spacing inside macros. 1393 EXPECT_EQ("#define B \\\n" 1394 " class A { \\\n" 1395 " protected: \\\n" 1396 " public: \\\n" 1397 " void f(); \\\n" 1398 " };", 1399 format("#define B \\\n" 1400 " class A { \\\n" 1401 " protected: \\\n" 1402 " \\\n" 1403 " public: \\\n" 1404 " \\\n" 1405 " void f(); \\\n" 1406 " };", 1407 getGoogleStyle())); 1408 // But don't remove empty lines after macros ending in access specifiers. 1409 EXPECT_EQ("#define A private:\n" 1410 "\n" 1411 "int i;", 1412 format("#define A private:\n" 1413 "\n" 1414 "int i;")); 1415 } 1416 1417 TEST_F(FormatTest, FormatsClasses) { 1418 verifyFormat("class A : public B {};"); 1419 verifyFormat("class A : public ::B {};"); 1420 1421 verifyFormat( 1422 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1423 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1424 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1425 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1426 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1427 verifyFormat( 1428 "class A : public B, public C, public D, public E, public F {};"); 1429 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1430 " public C,\n" 1431 " public D,\n" 1432 " public E,\n" 1433 " public F,\n" 1434 " public G {};"); 1435 1436 verifyFormat("class\n" 1437 " ReallyReallyLongClassName {\n" 1438 " int i;\n" 1439 "};", 1440 getLLVMStyleWithColumns(32)); 1441 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1442 " aaaaaaaaaaaaaaaa> {};"); 1443 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1444 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1445 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1446 verifyFormat("template <class R, class C>\n" 1447 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1448 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1449 verifyFormat("class ::A::B {};"); 1450 } 1451 1452 TEST_F(FormatTest, BreakInheritanceStyle) { 1453 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 1454 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 1455 FormatStyle::BILS_BeforeComma; 1456 verifyFormat("class MyClass : public X {};", 1457 StyleWithInheritanceBreakBeforeComma); 1458 verifyFormat("class MyClass\n" 1459 " : public X\n" 1460 " , public Y {};", 1461 StyleWithInheritanceBreakBeforeComma); 1462 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 1463 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 1464 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 1465 StyleWithInheritanceBreakBeforeComma); 1466 verifyFormat("struct aaaaaaaaaaaaa\n" 1467 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 1468 " aaaaaaaaaaaaaaaa> {};", 1469 StyleWithInheritanceBreakBeforeComma); 1470 1471 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 1472 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 1473 FormatStyle::BILS_AfterColon; 1474 verifyFormat("class MyClass : public X {};", 1475 StyleWithInheritanceBreakAfterColon); 1476 verifyFormat("class MyClass : public X, public Y {};", 1477 StyleWithInheritanceBreakAfterColon); 1478 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 1479 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1480 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 1481 StyleWithInheritanceBreakAfterColon); 1482 verifyFormat("struct aaaaaaaaaaaaa :\n" 1483 " public aaaaaaaaaaaaaaaaaaa< // break\n" 1484 " aaaaaaaaaaaaaaaa> {};", 1485 StyleWithInheritanceBreakAfterColon); 1486 } 1487 1488 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1489 verifyFormat("class A {\n} a, b;"); 1490 verifyFormat("struct A {\n} a, b;"); 1491 verifyFormat("union A {\n} a;"); 1492 } 1493 1494 TEST_F(FormatTest, FormatsEnum) { 1495 verifyFormat("enum {\n" 1496 " Zero,\n" 1497 " One = 1,\n" 1498 " Two = One + 1,\n" 1499 " Three = (One + Two),\n" 1500 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1501 " Five = (One, Two, Three, Four, 5)\n" 1502 "};"); 1503 verifyGoogleFormat("enum {\n" 1504 " Zero,\n" 1505 " One = 1,\n" 1506 " Two = One + 1,\n" 1507 " Three = (One + Two),\n" 1508 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1509 " Five = (One, Two, Three, Four, 5)\n" 1510 "};"); 1511 verifyFormat("enum Enum {};"); 1512 verifyFormat("enum {};"); 1513 verifyFormat("enum X E {} d;"); 1514 verifyFormat("enum __attribute__((...)) E {} d;"); 1515 verifyFormat("enum __declspec__((...)) E {} d;"); 1516 verifyFormat("enum {\n" 1517 " Bar = Foo<int, int>::value\n" 1518 "};", 1519 getLLVMStyleWithColumns(30)); 1520 1521 verifyFormat("enum ShortEnum { A, B, C };"); 1522 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1523 1524 EXPECT_EQ("enum KeepEmptyLines {\n" 1525 " ONE,\n" 1526 "\n" 1527 " TWO,\n" 1528 "\n" 1529 " THREE\n" 1530 "}", 1531 format("enum KeepEmptyLines {\n" 1532 " ONE,\n" 1533 "\n" 1534 " TWO,\n" 1535 "\n" 1536 "\n" 1537 " THREE\n" 1538 "}")); 1539 verifyFormat("enum E { // comment\n" 1540 " ONE,\n" 1541 " TWO\n" 1542 "};\n" 1543 "int i;"); 1544 // Not enums. 1545 verifyFormat("enum X f() {\n" 1546 " a();\n" 1547 " return 42;\n" 1548 "}"); 1549 verifyFormat("enum X Type::f() {\n" 1550 " a();\n" 1551 " return 42;\n" 1552 "}"); 1553 verifyFormat("enum ::X f() {\n" 1554 " a();\n" 1555 " return 42;\n" 1556 "}"); 1557 verifyFormat("enum ns::X f() {\n" 1558 " a();\n" 1559 " return 42;\n" 1560 "}"); 1561 } 1562 1563 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1564 verifyFormat("enum Type {\n" 1565 " One = 0; // These semicolons should be commas.\n" 1566 " Two = 1;\n" 1567 "};"); 1568 verifyFormat("namespace n {\n" 1569 "enum Type {\n" 1570 " One,\n" 1571 " Two, // missing };\n" 1572 " int i;\n" 1573 "}\n" 1574 "void g() {}"); 1575 } 1576 1577 TEST_F(FormatTest, FormatsEnumStruct) { 1578 verifyFormat("enum struct {\n" 1579 " Zero,\n" 1580 " One = 1,\n" 1581 " Two = One + 1,\n" 1582 " Three = (One + Two),\n" 1583 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1584 " Five = (One, Two, Three, Four, 5)\n" 1585 "};"); 1586 verifyFormat("enum struct Enum {};"); 1587 verifyFormat("enum struct {};"); 1588 verifyFormat("enum struct X E {} d;"); 1589 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1590 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1591 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1592 } 1593 1594 TEST_F(FormatTest, FormatsEnumClass) { 1595 verifyFormat("enum class {\n" 1596 " Zero,\n" 1597 " One = 1,\n" 1598 " Two = One + 1,\n" 1599 " Three = (One + Two),\n" 1600 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1601 " Five = (One, Two, Three, Four, 5)\n" 1602 "};"); 1603 verifyFormat("enum class Enum {};"); 1604 verifyFormat("enum class {};"); 1605 verifyFormat("enum class X E {} d;"); 1606 verifyFormat("enum class __attribute__((...)) E {} d;"); 1607 verifyFormat("enum class __declspec__((...)) E {} d;"); 1608 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1609 } 1610 1611 TEST_F(FormatTest, FormatsEnumTypes) { 1612 verifyFormat("enum X : int {\n" 1613 " A, // Force multiple lines.\n" 1614 " B\n" 1615 "};"); 1616 verifyFormat("enum X : int { A, B };"); 1617 verifyFormat("enum X : std::uint32_t { A, B };"); 1618 } 1619 1620 TEST_F(FormatTest, FormatsTypedefEnum) { 1621 FormatStyle Style = getLLVMStyle(); 1622 Style.ColumnLimit = 40; 1623 verifyFormat("typedef enum {} EmptyEnum;"); 1624 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1625 verifyFormat("typedef enum {\n" 1626 " ZERO = 0,\n" 1627 " ONE = 1,\n" 1628 " TWO = 2,\n" 1629 " THREE = 3\n" 1630 "} LongEnum;", 1631 Style); 1632 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1633 Style.BraceWrapping.AfterEnum = true; 1634 verifyFormat("typedef enum {} EmptyEnum;"); 1635 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1636 verifyFormat("typedef enum\n" 1637 "{\n" 1638 " ZERO = 0,\n" 1639 " ONE = 1,\n" 1640 " TWO = 2,\n" 1641 " THREE = 3\n" 1642 "} LongEnum;", 1643 Style); 1644 } 1645 1646 TEST_F(FormatTest, FormatsNSEnums) { 1647 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1648 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1649 " // Information about someDecentlyLongValue.\n" 1650 " someDecentlyLongValue,\n" 1651 " // Information about anotherDecentlyLongValue.\n" 1652 " anotherDecentlyLongValue,\n" 1653 " // Information about aThirdDecentlyLongValue.\n" 1654 " aThirdDecentlyLongValue\n" 1655 "};"); 1656 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1657 " a = 1,\n" 1658 " b = 2,\n" 1659 " c = 3,\n" 1660 "};"); 1661 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1662 " a = 1,\n" 1663 " b = 2,\n" 1664 " c = 3,\n" 1665 "};"); 1666 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1667 " a = 1,\n" 1668 " b = 2,\n" 1669 " c = 3,\n" 1670 "};"); 1671 } 1672 1673 TEST_F(FormatTest, FormatsBitfields) { 1674 verifyFormat("struct Bitfields {\n" 1675 " unsigned sClass : 8;\n" 1676 " unsigned ValueKind : 2;\n" 1677 "};"); 1678 verifyFormat("struct A {\n" 1679 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1680 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1681 "};"); 1682 verifyFormat("struct MyStruct {\n" 1683 " uchar data;\n" 1684 " uchar : 8;\n" 1685 " uchar : 8;\n" 1686 " uchar other;\n" 1687 "};"); 1688 } 1689 1690 TEST_F(FormatTest, FormatsNamespaces) { 1691 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1692 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1693 1694 verifyFormat("namespace some_namespace {\n" 1695 "class A {};\n" 1696 "void f() { f(); }\n" 1697 "}", 1698 LLVMWithNoNamespaceFix); 1699 verifyFormat("/* something */ namespace some_namespace {\n" 1700 "class A {};\n" 1701 "void f() { f(); }\n" 1702 "}", 1703 LLVMWithNoNamespaceFix); 1704 verifyFormat("namespace {\n" 1705 "class A {};\n" 1706 "void f() { f(); }\n" 1707 "}", 1708 LLVMWithNoNamespaceFix); 1709 verifyFormat("/* something */ namespace {\n" 1710 "class A {};\n" 1711 "void f() { f(); }\n" 1712 "}", 1713 LLVMWithNoNamespaceFix); 1714 verifyFormat("inline namespace X {\n" 1715 "class A {};\n" 1716 "void f() { f(); }\n" 1717 "}", 1718 LLVMWithNoNamespaceFix); 1719 verifyFormat("/* something */ inline namespace X {\n" 1720 "class A {};\n" 1721 "void f() { f(); }\n" 1722 "}", 1723 LLVMWithNoNamespaceFix); 1724 verifyFormat("export namespace X {\n" 1725 "class A {};\n" 1726 "void f() { f(); }\n" 1727 "}", 1728 LLVMWithNoNamespaceFix); 1729 verifyFormat("using namespace some_namespace;\n" 1730 "class A {};\n" 1731 "void f() { f(); }", 1732 LLVMWithNoNamespaceFix); 1733 1734 // This code is more common than we thought; if we 1735 // layout this correctly the semicolon will go into 1736 // its own line, which is undesirable. 1737 verifyFormat("namespace {};", 1738 LLVMWithNoNamespaceFix); 1739 verifyFormat("namespace {\n" 1740 "class A {};\n" 1741 "};", 1742 LLVMWithNoNamespaceFix); 1743 1744 verifyFormat("namespace {\n" 1745 "int SomeVariable = 0; // comment\n" 1746 "} // namespace", 1747 LLVMWithNoNamespaceFix); 1748 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1749 "#define HEADER_GUARD\n" 1750 "namespace my_namespace {\n" 1751 "int i;\n" 1752 "} // my_namespace\n" 1753 "#endif // HEADER_GUARD", 1754 format("#ifndef HEADER_GUARD\n" 1755 " #define HEADER_GUARD\n" 1756 " namespace my_namespace {\n" 1757 "int i;\n" 1758 "} // my_namespace\n" 1759 "#endif // HEADER_GUARD", 1760 LLVMWithNoNamespaceFix)); 1761 1762 EXPECT_EQ("namespace A::B {\n" 1763 "class C {};\n" 1764 "}", 1765 format("namespace A::B {\n" 1766 "class C {};\n" 1767 "}", 1768 LLVMWithNoNamespaceFix)); 1769 1770 FormatStyle Style = getLLVMStyle(); 1771 Style.NamespaceIndentation = FormatStyle::NI_All; 1772 EXPECT_EQ("namespace out {\n" 1773 " int i;\n" 1774 " namespace in {\n" 1775 " int i;\n" 1776 " } // namespace in\n" 1777 "} // namespace out", 1778 format("namespace out {\n" 1779 "int i;\n" 1780 "namespace in {\n" 1781 "int i;\n" 1782 "} // namespace in\n" 1783 "} // namespace out", 1784 Style)); 1785 1786 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1787 EXPECT_EQ("namespace out {\n" 1788 "int i;\n" 1789 "namespace in {\n" 1790 " int i;\n" 1791 "} // namespace in\n" 1792 "} // namespace out", 1793 format("namespace out {\n" 1794 "int i;\n" 1795 "namespace in {\n" 1796 "int i;\n" 1797 "} // namespace in\n" 1798 "} // namespace out", 1799 Style)); 1800 } 1801 1802 TEST_F(FormatTest, FormatsCompactNamespaces) { 1803 FormatStyle Style = getLLVMStyle(); 1804 Style.CompactNamespaces = true; 1805 1806 verifyFormat("namespace A { namespace B {\n" 1807 "}} // namespace A::B", 1808 Style); 1809 1810 EXPECT_EQ("namespace out { namespace in {\n" 1811 "}} // namespace out::in", 1812 format("namespace out {\n" 1813 "namespace in {\n" 1814 "} // namespace in\n" 1815 "} // namespace out", 1816 Style)); 1817 1818 // Only namespaces which have both consecutive opening and end get compacted 1819 EXPECT_EQ("namespace out {\n" 1820 "namespace in1 {\n" 1821 "} // namespace in1\n" 1822 "namespace in2 {\n" 1823 "} // namespace in2\n" 1824 "} // namespace out", 1825 format("namespace out {\n" 1826 "namespace in1 {\n" 1827 "} // namespace in1\n" 1828 "namespace in2 {\n" 1829 "} // namespace in2\n" 1830 "} // namespace out", 1831 Style)); 1832 1833 EXPECT_EQ("namespace out {\n" 1834 "int i;\n" 1835 "namespace in {\n" 1836 "int j;\n" 1837 "} // namespace in\n" 1838 "int k;\n" 1839 "} // namespace out", 1840 format("namespace out { int i;\n" 1841 "namespace in { int j; } // namespace in\n" 1842 "int k; } // namespace out", 1843 Style)); 1844 1845 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 1846 "}}} // namespace A::B::C\n", 1847 format("namespace A { namespace B {\n" 1848 "namespace C {\n" 1849 "}} // namespace B::C\n" 1850 "} // namespace A\n", 1851 Style)); 1852 1853 Style.ColumnLimit = 40; 1854 EXPECT_EQ("namespace aaaaaaaaaa {\n" 1855 "namespace bbbbbbbbbb {\n" 1856 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 1857 format("namespace aaaaaaaaaa {\n" 1858 "namespace bbbbbbbbbb {\n" 1859 "} // namespace bbbbbbbbbb\n" 1860 "} // namespace aaaaaaaaaa", 1861 Style)); 1862 1863 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 1864 "namespace cccccc {\n" 1865 "}}} // namespace aaaaaa::bbbbbb::cccccc", 1866 format("namespace aaaaaa {\n" 1867 "namespace bbbbbb {\n" 1868 "namespace cccccc {\n" 1869 "} // namespace cccccc\n" 1870 "} // namespace bbbbbb\n" 1871 "} // namespace aaaaaa", 1872 Style)); 1873 Style.ColumnLimit = 80; 1874 1875 // Extra semicolon after 'inner' closing brace prevents merging 1876 EXPECT_EQ("namespace out { namespace in {\n" 1877 "}; } // namespace out::in", 1878 format("namespace out {\n" 1879 "namespace in {\n" 1880 "}; // namespace in\n" 1881 "} // namespace out", 1882 Style)); 1883 1884 // Extra semicolon after 'outer' closing brace is conserved 1885 EXPECT_EQ("namespace out { namespace in {\n" 1886 "}}; // namespace out::in", 1887 format("namespace out {\n" 1888 "namespace in {\n" 1889 "} // namespace in\n" 1890 "}; // namespace out", 1891 Style)); 1892 1893 Style.NamespaceIndentation = FormatStyle::NI_All; 1894 EXPECT_EQ("namespace out { namespace in {\n" 1895 " int i;\n" 1896 "}} // namespace out::in", 1897 format("namespace out {\n" 1898 "namespace in {\n" 1899 "int i;\n" 1900 "} // namespace in\n" 1901 "} // namespace out", 1902 Style)); 1903 EXPECT_EQ("namespace out { namespace mid {\n" 1904 " namespace in {\n" 1905 " int j;\n" 1906 " } // namespace in\n" 1907 " int k;\n" 1908 "}} // namespace out::mid", 1909 format("namespace out { namespace mid {\n" 1910 "namespace in { int j; } // namespace in\n" 1911 "int k; }} // namespace out::mid", 1912 Style)); 1913 1914 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1915 EXPECT_EQ("namespace out { namespace in {\n" 1916 " int i;\n" 1917 "}} // namespace out::in", 1918 format("namespace out {\n" 1919 "namespace in {\n" 1920 "int i;\n" 1921 "} // namespace in\n" 1922 "} // namespace out", 1923 Style)); 1924 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 1925 " int i;\n" 1926 "}}} // namespace out::mid::in", 1927 format("namespace out {\n" 1928 "namespace mid {\n" 1929 "namespace in {\n" 1930 "int i;\n" 1931 "} // namespace in\n" 1932 "} // namespace mid\n" 1933 "} // namespace out", 1934 Style)); 1935 } 1936 1937 TEST_F(FormatTest, FormatsExternC) { 1938 verifyFormat("extern \"C\" {\nint a;"); 1939 verifyFormat("extern \"C\" {}"); 1940 verifyFormat("extern \"C\" {\n" 1941 "int foo();\n" 1942 "}"); 1943 verifyFormat("extern \"C\" int foo() {}"); 1944 verifyFormat("extern \"C\" int foo();"); 1945 verifyFormat("extern \"C\" int foo() {\n" 1946 " int i = 42;\n" 1947 " return i;\n" 1948 "}"); 1949 1950 FormatStyle Style = getLLVMStyle(); 1951 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1952 Style.BraceWrapping.AfterFunction = true; 1953 verifyFormat("extern \"C\" int foo() {}", Style); 1954 verifyFormat("extern \"C\" int foo();", Style); 1955 verifyFormat("extern \"C\" int foo()\n" 1956 "{\n" 1957 " int i = 42;\n" 1958 " return i;\n" 1959 "}", 1960 Style); 1961 1962 Style.BraceWrapping.AfterExternBlock = true; 1963 Style.BraceWrapping.SplitEmptyRecord = false; 1964 verifyFormat("extern \"C\"\n" 1965 "{}", 1966 Style); 1967 verifyFormat("extern \"C\"\n" 1968 "{\n" 1969 " int foo();\n" 1970 "}", 1971 Style); 1972 } 1973 1974 TEST_F(FormatTest, FormatsInlineASM) { 1975 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 1976 verifyFormat("asm(\"nop\" ::: \"memory\");"); 1977 verifyFormat( 1978 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 1979 " \"cpuid\\n\\t\"\n" 1980 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 1981 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 1982 " : \"a\"(value));"); 1983 EXPECT_EQ( 1984 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1985 " __asm {\n" 1986 " mov edx,[that] // vtable in edx\n" 1987 " mov eax,methodIndex\n" 1988 " call [edx][eax*4] // stdcall\n" 1989 " }\n" 1990 "}", 1991 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1992 " __asm {\n" 1993 " mov edx,[that] // vtable in edx\n" 1994 " mov eax,methodIndex\n" 1995 " call [edx][eax*4] // stdcall\n" 1996 " }\n" 1997 "}")); 1998 EXPECT_EQ("_asm {\n" 1999 " xor eax, eax;\n" 2000 " cpuid;\n" 2001 "}", 2002 format("_asm {\n" 2003 " xor eax, eax;\n" 2004 " cpuid;\n" 2005 "}")); 2006 verifyFormat("void function() {\n" 2007 " // comment\n" 2008 " asm(\"\");\n" 2009 "}"); 2010 EXPECT_EQ("__asm {\n" 2011 "}\n" 2012 "int i;", 2013 format("__asm {\n" 2014 "}\n" 2015 "int i;")); 2016 } 2017 2018 TEST_F(FormatTest, FormatTryCatch) { 2019 verifyFormat("try {\n" 2020 " throw a * b;\n" 2021 "} catch (int a) {\n" 2022 " // Do nothing.\n" 2023 "} catch (...) {\n" 2024 " exit(42);\n" 2025 "}"); 2026 2027 // Function-level try statements. 2028 verifyFormat("int f() try { return 4; } catch (...) {\n" 2029 " return 5;\n" 2030 "}"); 2031 verifyFormat("class A {\n" 2032 " int a;\n" 2033 " A() try : a(0) {\n" 2034 " } catch (...) {\n" 2035 " throw;\n" 2036 " }\n" 2037 "};\n"); 2038 2039 // Incomplete try-catch blocks. 2040 verifyIncompleteFormat("try {} catch ("); 2041 } 2042 2043 TEST_F(FormatTest, FormatSEHTryCatch) { 2044 verifyFormat("__try {\n" 2045 " int a = b * c;\n" 2046 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2047 " // Do nothing.\n" 2048 "}"); 2049 2050 verifyFormat("__try {\n" 2051 " int a = b * c;\n" 2052 "} __finally {\n" 2053 " // Do nothing.\n" 2054 "}"); 2055 2056 verifyFormat("DEBUG({\n" 2057 " __try {\n" 2058 " } __finally {\n" 2059 " }\n" 2060 "});\n"); 2061 } 2062 2063 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2064 verifyFormat("try {\n" 2065 " f();\n" 2066 "} catch {\n" 2067 " g();\n" 2068 "}"); 2069 verifyFormat("try {\n" 2070 " f();\n" 2071 "} catch (A a) MACRO(x) {\n" 2072 " g();\n" 2073 "} catch (B b) MACRO(x) {\n" 2074 " g();\n" 2075 "}"); 2076 } 2077 2078 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2079 FormatStyle Style = getLLVMStyle(); 2080 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 2081 FormatStyle::BS_WebKit}) { 2082 Style.BreakBeforeBraces = BraceStyle; 2083 verifyFormat("try {\n" 2084 " // something\n" 2085 "} catch (...) {\n" 2086 " // something\n" 2087 "}", 2088 Style); 2089 } 2090 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2091 verifyFormat("try {\n" 2092 " // something\n" 2093 "}\n" 2094 "catch (...) {\n" 2095 " // something\n" 2096 "}", 2097 Style); 2098 verifyFormat("__try {\n" 2099 " // something\n" 2100 "}\n" 2101 "__finally {\n" 2102 " // something\n" 2103 "}", 2104 Style); 2105 verifyFormat("@try {\n" 2106 " // something\n" 2107 "}\n" 2108 "@finally {\n" 2109 " // something\n" 2110 "}", 2111 Style); 2112 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2113 verifyFormat("try\n" 2114 "{\n" 2115 " // something\n" 2116 "}\n" 2117 "catch (...)\n" 2118 "{\n" 2119 " // something\n" 2120 "}", 2121 Style); 2122 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2123 verifyFormat("try\n" 2124 " {\n" 2125 " // something\n" 2126 " }\n" 2127 "catch (...)\n" 2128 " {\n" 2129 " // something\n" 2130 " }", 2131 Style); 2132 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2133 Style.BraceWrapping.BeforeCatch = true; 2134 verifyFormat("try {\n" 2135 " // something\n" 2136 "}\n" 2137 "catch (...) {\n" 2138 " // something\n" 2139 "}", 2140 Style); 2141 } 2142 2143 TEST_F(FormatTest, StaticInitializers) { 2144 verifyFormat("static SomeClass SC = {1, 'a'};"); 2145 2146 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2147 " 100000000, " 2148 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2149 2150 // Here, everything other than the "}" would fit on a line. 2151 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2152 " 10000000000000000000000000};"); 2153 EXPECT_EQ("S s = {a,\n" 2154 "\n" 2155 " b};", 2156 format("S s = {\n" 2157 " a,\n" 2158 "\n" 2159 " b\n" 2160 "};")); 2161 2162 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2163 // line. However, the formatting looks a bit off and this probably doesn't 2164 // happen often in practice. 2165 verifyFormat("static int Variable[1] = {\n" 2166 " {1000000000000000000000000000000000000}};", 2167 getLLVMStyleWithColumns(40)); 2168 } 2169 2170 TEST_F(FormatTest, DesignatedInitializers) { 2171 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2172 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2173 " .bbbbbbbbbb = 2,\n" 2174 " .cccccccccc = 3,\n" 2175 " .dddddddddd = 4,\n" 2176 " .eeeeeeeeee = 5};"); 2177 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2178 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2179 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2180 " .ccccccccccccccccccccccccccc = 3,\n" 2181 " .ddddddddddddddddddddddddddd = 4,\n" 2182 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2183 2184 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2185 2186 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 2187 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 2188 " [2] = bbbbbbbbbb,\n" 2189 " [3] = cccccccccc,\n" 2190 " [4] = dddddddddd,\n" 2191 " [5] = eeeeeeeeee};"); 2192 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2193 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2194 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2195 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 2196 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 2197 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 2198 } 2199 2200 TEST_F(FormatTest, NestedStaticInitializers) { 2201 verifyFormat("static A x = {{{}}};\n"); 2202 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2203 " {init1, init2, init3, init4}}};", 2204 getLLVMStyleWithColumns(50)); 2205 2206 verifyFormat("somes Status::global_reps[3] = {\n" 2207 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2208 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2209 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2210 getLLVMStyleWithColumns(60)); 2211 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2212 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2213 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2214 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2215 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2216 " {rect.fRight - rect.fLeft, rect.fBottom - " 2217 "rect.fTop}};"); 2218 2219 verifyFormat( 2220 "SomeArrayOfSomeType a = {\n" 2221 " {{1, 2, 3},\n" 2222 " {1, 2, 3},\n" 2223 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2224 " 333333333333333333333333333333},\n" 2225 " {1, 2, 3},\n" 2226 " {1, 2, 3}}};"); 2227 verifyFormat( 2228 "SomeArrayOfSomeType a = {\n" 2229 " {{1, 2, 3}},\n" 2230 " {{1, 2, 3}},\n" 2231 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2232 " 333333333333333333333333333333}},\n" 2233 " {{1, 2, 3}},\n" 2234 " {{1, 2, 3}}};"); 2235 2236 verifyFormat("struct {\n" 2237 " unsigned bit;\n" 2238 " const char *const name;\n" 2239 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2240 " {kOsWin, \"Windows\"},\n" 2241 " {kOsLinux, \"Linux\"},\n" 2242 " {kOsCrOS, \"Chrome OS\"}};"); 2243 verifyFormat("struct {\n" 2244 " unsigned bit;\n" 2245 " const char *const name;\n" 2246 "} kBitsToOs[] = {\n" 2247 " {kOsMac, \"Mac\"},\n" 2248 " {kOsWin, \"Windows\"},\n" 2249 " {kOsLinux, \"Linux\"},\n" 2250 " {kOsCrOS, \"Chrome OS\"},\n" 2251 "};"); 2252 } 2253 2254 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2255 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2256 " \\\n" 2257 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2258 } 2259 2260 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2261 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2262 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2263 2264 // Do break defaulted and deleted functions. 2265 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2266 " default;", 2267 getLLVMStyleWithColumns(40)); 2268 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2269 " delete;", 2270 getLLVMStyleWithColumns(40)); 2271 } 2272 2273 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2274 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2275 getLLVMStyleWithColumns(40)); 2276 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2277 getLLVMStyleWithColumns(40)); 2278 EXPECT_EQ("#define Q \\\n" 2279 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2280 " \"aaaaaaaa.cpp\"", 2281 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2282 getLLVMStyleWithColumns(40))); 2283 } 2284 2285 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2286 EXPECT_EQ("# 123 \"A string literal\"", 2287 format(" # 123 \"A string literal\"")); 2288 } 2289 2290 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2291 EXPECT_EQ("#;", format("#;")); 2292 verifyFormat("#\n;\n;\n;"); 2293 } 2294 2295 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2296 EXPECT_EQ("#line 42 \"test\"\n", 2297 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2298 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2299 getLLVMStyleWithColumns(12))); 2300 } 2301 2302 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2303 EXPECT_EQ("#line 42 \"test\"", 2304 format("# \\\n line \\\n 42 \\\n \"test\"")); 2305 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2306 } 2307 2308 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2309 verifyFormat("#define A \\x20"); 2310 verifyFormat("#define A \\ x20"); 2311 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2312 verifyFormat("#define A ''"); 2313 verifyFormat("#define A ''qqq"); 2314 verifyFormat("#define A `qqq"); 2315 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2316 EXPECT_EQ("const char *c = STRINGIFY(\n" 2317 "\\na : b);", 2318 format("const char * c = STRINGIFY(\n" 2319 "\\na : b);")); 2320 2321 verifyFormat("a\r\\"); 2322 verifyFormat("a\v\\"); 2323 verifyFormat("a\f\\"); 2324 } 2325 2326 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2327 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2328 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2329 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2330 // FIXME: We never break before the macro name. 2331 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2332 2333 verifyFormat("#define A A\n#define A A"); 2334 verifyFormat("#define A(X) A\n#define A A"); 2335 2336 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2337 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2338 } 2339 2340 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2341 EXPECT_EQ("// somecomment\n" 2342 "#include \"a.h\"\n" 2343 "#define A( \\\n" 2344 " A, B)\n" 2345 "#include \"b.h\"\n" 2346 "// somecomment\n", 2347 format(" // somecomment\n" 2348 " #include \"a.h\"\n" 2349 "#define A(A,\\\n" 2350 " B)\n" 2351 " #include \"b.h\"\n" 2352 " // somecomment\n", 2353 getLLVMStyleWithColumns(13))); 2354 } 2355 2356 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2357 2358 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2359 EXPECT_EQ("#define A \\\n" 2360 " c; \\\n" 2361 " e;\n" 2362 "f;", 2363 format("#define A c; e;\n" 2364 "f;", 2365 getLLVMStyleWithColumns(14))); 2366 } 2367 2368 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2369 2370 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2371 EXPECT_EQ("int x,\n" 2372 "#define A\n" 2373 " y;", 2374 format("int x,\n#define A\ny;")); 2375 } 2376 2377 TEST_F(FormatTest, HashInMacroDefinition) { 2378 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2379 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2380 verifyFormat("#define A \\\n" 2381 " { \\\n" 2382 " f(#c); \\\n" 2383 " }", 2384 getLLVMStyleWithColumns(11)); 2385 2386 verifyFormat("#define A(X) \\\n" 2387 " void function##X()", 2388 getLLVMStyleWithColumns(22)); 2389 2390 verifyFormat("#define A(a, b, c) \\\n" 2391 " void a##b##c()", 2392 getLLVMStyleWithColumns(22)); 2393 2394 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2395 } 2396 2397 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2398 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2399 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2400 } 2401 2402 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2403 EXPECT_EQ("#define A b;", format("#define A \\\n" 2404 " \\\n" 2405 " b;", 2406 getLLVMStyleWithColumns(25))); 2407 EXPECT_EQ("#define A \\\n" 2408 " \\\n" 2409 " a; \\\n" 2410 " b;", 2411 format("#define A \\\n" 2412 " \\\n" 2413 " a; \\\n" 2414 " b;", 2415 getLLVMStyleWithColumns(11))); 2416 EXPECT_EQ("#define A \\\n" 2417 " a; \\\n" 2418 " \\\n" 2419 " b;", 2420 format("#define A \\\n" 2421 " a; \\\n" 2422 " \\\n" 2423 " b;", 2424 getLLVMStyleWithColumns(11))); 2425 } 2426 2427 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2428 verifyIncompleteFormat("#define A :"); 2429 verifyFormat("#define SOMECASES \\\n" 2430 " case 1: \\\n" 2431 " case 2\n", 2432 getLLVMStyleWithColumns(20)); 2433 verifyFormat("#define MACRO(a) \\\n" 2434 " if (a) \\\n" 2435 " f(); \\\n" 2436 " else \\\n" 2437 " g()", 2438 getLLVMStyleWithColumns(18)); 2439 verifyFormat("#define A template <typename T>"); 2440 verifyIncompleteFormat("#define STR(x) #x\n" 2441 "f(STR(this_is_a_string_literal{));"); 2442 verifyFormat("#pragma omp threadprivate( \\\n" 2443 " y)), // expected-warning", 2444 getLLVMStyleWithColumns(28)); 2445 verifyFormat("#d, = };"); 2446 verifyFormat("#if \"a"); 2447 verifyIncompleteFormat("({\n" 2448 "#define b \\\n" 2449 " } \\\n" 2450 " a\n" 2451 "a", 2452 getLLVMStyleWithColumns(15)); 2453 verifyFormat("#define A \\\n" 2454 " { \\\n" 2455 " {\n" 2456 "#define B \\\n" 2457 " } \\\n" 2458 " }", 2459 getLLVMStyleWithColumns(15)); 2460 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2461 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2462 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2463 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2464 } 2465 2466 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2467 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2468 EXPECT_EQ("class A : public QObject {\n" 2469 " Q_OBJECT\n" 2470 "\n" 2471 " A() {}\n" 2472 "};", 2473 format("class A : public QObject {\n" 2474 " Q_OBJECT\n" 2475 "\n" 2476 " A() {\n}\n" 2477 "} ;")); 2478 EXPECT_EQ("MACRO\n" 2479 "/*static*/ int i;", 2480 format("MACRO\n" 2481 " /*static*/ int i;")); 2482 EXPECT_EQ("SOME_MACRO\n" 2483 "namespace {\n" 2484 "void f();\n" 2485 "} // namespace", 2486 format("SOME_MACRO\n" 2487 " namespace {\n" 2488 "void f( );\n" 2489 "} // namespace")); 2490 // Only if the identifier contains at least 5 characters. 2491 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2492 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2493 // Only if everything is upper case. 2494 EXPECT_EQ("class A : public QObject {\n" 2495 " Q_Object A() {}\n" 2496 "};", 2497 format("class A : public QObject {\n" 2498 " Q_Object\n" 2499 " A() {\n}\n" 2500 "} ;")); 2501 2502 // Only if the next line can actually start an unwrapped line. 2503 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2504 format("SOME_WEIRD_LOG_MACRO\n" 2505 "<< SomeThing;")); 2506 2507 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2508 "(n, buffers))\n", 2509 getChromiumStyle(FormatStyle::LK_Cpp)); 2510 } 2511 2512 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2513 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2514 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2515 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2516 "class X {};\n" 2517 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2518 "int *createScopDetectionPass() { return 0; }", 2519 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2520 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2521 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2522 " class X {};\n" 2523 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2524 " int *createScopDetectionPass() { return 0; }")); 2525 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2526 // braces, so that inner block is indented one level more. 2527 EXPECT_EQ("int q() {\n" 2528 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2529 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2530 " IPC_END_MESSAGE_MAP()\n" 2531 "}", 2532 format("int q() {\n" 2533 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2534 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2535 " IPC_END_MESSAGE_MAP()\n" 2536 "}")); 2537 2538 // Same inside macros. 2539 EXPECT_EQ("#define LIST(L) \\\n" 2540 " L(A) \\\n" 2541 " L(B) \\\n" 2542 " L(C)", 2543 format("#define LIST(L) \\\n" 2544 " L(A) \\\n" 2545 " L(B) \\\n" 2546 " L(C)", 2547 getGoogleStyle())); 2548 2549 // These must not be recognized as macros. 2550 EXPECT_EQ("int q() {\n" 2551 " f(x);\n" 2552 " f(x) {}\n" 2553 " f(x)->g();\n" 2554 " f(x)->*g();\n" 2555 " f(x).g();\n" 2556 " f(x) = x;\n" 2557 " f(x) += x;\n" 2558 " f(x) -= x;\n" 2559 " f(x) *= x;\n" 2560 " f(x) /= x;\n" 2561 " f(x) %= x;\n" 2562 " f(x) &= x;\n" 2563 " f(x) |= x;\n" 2564 " f(x) ^= x;\n" 2565 " f(x) >>= x;\n" 2566 " f(x) <<= x;\n" 2567 " f(x)[y].z();\n" 2568 " LOG(INFO) << x;\n" 2569 " ifstream(x) >> x;\n" 2570 "}\n", 2571 format("int q() {\n" 2572 " f(x)\n;\n" 2573 " f(x)\n {}\n" 2574 " f(x)\n->g();\n" 2575 " f(x)\n->*g();\n" 2576 " f(x)\n.g();\n" 2577 " f(x)\n = x;\n" 2578 " f(x)\n += x;\n" 2579 " f(x)\n -= x;\n" 2580 " f(x)\n *= x;\n" 2581 " f(x)\n /= x;\n" 2582 " f(x)\n %= x;\n" 2583 " f(x)\n &= x;\n" 2584 " f(x)\n |= x;\n" 2585 " f(x)\n ^= x;\n" 2586 " f(x)\n >>= x;\n" 2587 " f(x)\n <<= x;\n" 2588 " f(x)\n[y].z();\n" 2589 " LOG(INFO)\n << x;\n" 2590 " ifstream(x)\n >> x;\n" 2591 "}\n")); 2592 EXPECT_EQ("int q() {\n" 2593 " F(x)\n" 2594 " if (1) {\n" 2595 " }\n" 2596 " F(x)\n" 2597 " while (1) {\n" 2598 " }\n" 2599 " F(x)\n" 2600 " G(x);\n" 2601 " F(x)\n" 2602 " try {\n" 2603 " Q();\n" 2604 " } catch (...) {\n" 2605 " }\n" 2606 "}\n", 2607 format("int q() {\n" 2608 "F(x)\n" 2609 "if (1) {}\n" 2610 "F(x)\n" 2611 "while (1) {}\n" 2612 "F(x)\n" 2613 "G(x);\n" 2614 "F(x)\n" 2615 "try { Q(); } catch (...) {}\n" 2616 "}\n")); 2617 EXPECT_EQ("class A {\n" 2618 " A() : t(0) {}\n" 2619 " A(int i) noexcept() : {}\n" 2620 " A(X x)\n" // FIXME: function-level try blocks are broken. 2621 " try : t(0) {\n" 2622 " } catch (...) {\n" 2623 " }\n" 2624 "};", 2625 format("class A {\n" 2626 " A()\n : t(0) {}\n" 2627 " A(int i)\n noexcept() : {}\n" 2628 " A(X x)\n" 2629 " try : t(0) {} catch (...) {}\n" 2630 "};")); 2631 FormatStyle Style = getLLVMStyle(); 2632 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2633 Style.BraceWrapping.AfterControlStatement = true; 2634 Style.BraceWrapping.AfterFunction = true; 2635 EXPECT_EQ("void f()\n" 2636 "try\n" 2637 "{\n" 2638 "}", 2639 format("void f() try {\n" 2640 "}", Style)); 2641 EXPECT_EQ("class SomeClass {\n" 2642 "public:\n" 2643 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2644 "};", 2645 format("class SomeClass {\n" 2646 "public:\n" 2647 " SomeClass()\n" 2648 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2649 "};")); 2650 EXPECT_EQ("class SomeClass {\n" 2651 "public:\n" 2652 " SomeClass()\n" 2653 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2654 "};", 2655 format("class SomeClass {\n" 2656 "public:\n" 2657 " SomeClass()\n" 2658 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2659 "};", 2660 getLLVMStyleWithColumns(40))); 2661 2662 verifyFormat("MACRO(>)"); 2663 2664 // Some macros contain an implicit semicolon. 2665 Style = getLLVMStyle(); 2666 Style.StatementMacros.push_back("FOO"); 2667 verifyFormat("FOO(a) int b = 0;"); 2668 verifyFormat("FOO(a)\n" 2669 "int b = 0;", 2670 Style); 2671 verifyFormat("FOO(a);\n" 2672 "int b = 0;", 2673 Style); 2674 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 2675 "int b = 0;", 2676 Style); 2677 verifyFormat("FOO()\n" 2678 "int b = 0;", 2679 Style); 2680 verifyFormat("FOO\n" 2681 "int b = 0;", 2682 Style); 2683 verifyFormat("void f() {\n" 2684 " FOO(a)\n" 2685 " return a;\n" 2686 "}", 2687 Style); 2688 verifyFormat("FOO(a)\n" 2689 "FOO(b)", 2690 Style); 2691 verifyFormat("int a = 0;\n" 2692 "FOO(b)\n" 2693 "int c = 0;", 2694 Style); 2695 verifyFormat("int a = 0;\n" 2696 "int x = FOO(a)\n" 2697 "int b = 0;", 2698 Style); 2699 verifyFormat("void foo(int a) { FOO(a) }\n" 2700 "uint32_t bar() {}", 2701 Style); 2702 } 2703 2704 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2705 verifyFormat("#define A \\\n" 2706 " f({ \\\n" 2707 " g(); \\\n" 2708 " });", 2709 getLLVMStyleWithColumns(11)); 2710 } 2711 2712 TEST_F(FormatTest, IndentPreprocessorDirectives) { 2713 FormatStyle Style = getLLVMStyle(); 2714 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 2715 Style.ColumnLimit = 40; 2716 verifyFormat("#ifdef _WIN32\n" 2717 "#define A 0\n" 2718 "#ifdef VAR2\n" 2719 "#define B 1\n" 2720 "#include <someheader.h>\n" 2721 "#define MACRO \\\n" 2722 " some_very_long_func_aaaaaaaaaa();\n" 2723 "#endif\n" 2724 "#else\n" 2725 "#define A 1\n" 2726 "#endif", 2727 Style); 2728 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 2729 verifyFormat("#ifdef _WIN32\n" 2730 "# define A 0\n" 2731 "# ifdef VAR2\n" 2732 "# define B 1\n" 2733 "# include <someheader.h>\n" 2734 "# define MACRO \\\n" 2735 " some_very_long_func_aaaaaaaaaa();\n" 2736 "# endif\n" 2737 "#else\n" 2738 "# define A 1\n" 2739 "#endif", 2740 Style); 2741 verifyFormat("#if A\n" 2742 "# define MACRO \\\n" 2743 " void a(int x) { \\\n" 2744 " b(); \\\n" 2745 " c(); \\\n" 2746 " d(); \\\n" 2747 " e(); \\\n" 2748 " f(); \\\n" 2749 " }\n" 2750 "#endif", 2751 Style); 2752 // Comments before include guard. 2753 verifyFormat("// file comment\n" 2754 "// file comment\n" 2755 "#ifndef HEADER_H\n" 2756 "#define HEADER_H\n" 2757 "code();\n" 2758 "#endif", 2759 Style); 2760 // Test with include guards. 2761 verifyFormat("#ifndef HEADER_H\n" 2762 "#define HEADER_H\n" 2763 "code();\n" 2764 "#endif", 2765 Style); 2766 // Include guards must have a #define with the same variable immediately 2767 // after #ifndef. 2768 verifyFormat("#ifndef NOT_GUARD\n" 2769 "# define FOO\n" 2770 "code();\n" 2771 "#endif", 2772 Style); 2773 2774 // Include guards must cover the entire file. 2775 verifyFormat("code();\n" 2776 "code();\n" 2777 "#ifndef NOT_GUARD\n" 2778 "# define NOT_GUARD\n" 2779 "code();\n" 2780 "#endif", 2781 Style); 2782 verifyFormat("#ifndef NOT_GUARD\n" 2783 "# define NOT_GUARD\n" 2784 "code();\n" 2785 "#endif\n" 2786 "code();", 2787 Style); 2788 // Test with trailing blank lines. 2789 verifyFormat("#ifndef HEADER_H\n" 2790 "#define HEADER_H\n" 2791 "code();\n" 2792 "#endif\n", 2793 Style); 2794 // Include guards don't have #else. 2795 verifyFormat("#ifndef NOT_GUARD\n" 2796 "# define NOT_GUARD\n" 2797 "code();\n" 2798 "#else\n" 2799 "#endif", 2800 Style); 2801 verifyFormat("#ifndef NOT_GUARD\n" 2802 "# define NOT_GUARD\n" 2803 "code();\n" 2804 "#elif FOO\n" 2805 "#endif", 2806 Style); 2807 // Non-identifier #define after potential include guard. 2808 verifyFormat("#ifndef FOO\n" 2809 "# define 1\n" 2810 "#endif\n", 2811 Style); 2812 // #if closes past last non-preprocessor line. 2813 verifyFormat("#ifndef FOO\n" 2814 "#define FOO\n" 2815 "#if 1\n" 2816 "int i;\n" 2817 "# define A 0\n" 2818 "#endif\n" 2819 "#endif\n", 2820 Style); 2821 // FIXME: This doesn't handle the case where there's code between the 2822 // #ifndef and #define but all other conditions hold. This is because when 2823 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 2824 // previous code line yet, so we can't detect it. 2825 EXPECT_EQ("#ifndef NOT_GUARD\n" 2826 "code();\n" 2827 "#define NOT_GUARD\n" 2828 "code();\n" 2829 "#endif", 2830 format("#ifndef NOT_GUARD\n" 2831 "code();\n" 2832 "# define NOT_GUARD\n" 2833 "code();\n" 2834 "#endif", 2835 Style)); 2836 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 2837 // be outside an include guard. Examples are #pragma once and 2838 // #pragma GCC diagnostic, or anything else that does not change the meaning 2839 // of the file if it's included multiple times. 2840 EXPECT_EQ("#ifdef WIN32\n" 2841 "# pragma once\n" 2842 "#endif\n" 2843 "#ifndef HEADER_H\n" 2844 "# define HEADER_H\n" 2845 "code();\n" 2846 "#endif", 2847 format("#ifdef WIN32\n" 2848 "# pragma once\n" 2849 "#endif\n" 2850 "#ifndef HEADER_H\n" 2851 "#define HEADER_H\n" 2852 "code();\n" 2853 "#endif", 2854 Style)); 2855 // FIXME: This does not detect when there is a single non-preprocessor line 2856 // in front of an include-guard-like structure where other conditions hold 2857 // because ScopedLineState hides the line. 2858 EXPECT_EQ("code();\n" 2859 "#ifndef HEADER_H\n" 2860 "#define HEADER_H\n" 2861 "code();\n" 2862 "#endif", 2863 format("code();\n" 2864 "#ifndef HEADER_H\n" 2865 "# define HEADER_H\n" 2866 "code();\n" 2867 "#endif", 2868 Style)); 2869 // Keep comments aligned with #, otherwise indent comments normally. These 2870 // tests cannot use verifyFormat because messUp manipulates leading 2871 // whitespace. 2872 { 2873 const char *Expected = "" 2874 "void f() {\n" 2875 "#if 1\n" 2876 "// Preprocessor aligned.\n" 2877 "# define A 0\n" 2878 " // Code. Separated by blank line.\n" 2879 "\n" 2880 "# define B 0\n" 2881 " // Code. Not aligned with #\n" 2882 "# define C 0\n" 2883 "#endif"; 2884 const char *ToFormat = "" 2885 "void f() {\n" 2886 "#if 1\n" 2887 "// Preprocessor aligned.\n" 2888 "# define A 0\n" 2889 "// Code. Separated by blank line.\n" 2890 "\n" 2891 "# define B 0\n" 2892 " // Code. Not aligned with #\n" 2893 "# define C 0\n" 2894 "#endif"; 2895 EXPECT_EQ(Expected, format(ToFormat, Style)); 2896 EXPECT_EQ(Expected, format(Expected, Style)); 2897 } 2898 // Keep block quotes aligned. 2899 { 2900 const char *Expected = "" 2901 "void f() {\n" 2902 "#if 1\n" 2903 "/* Preprocessor aligned. */\n" 2904 "# define A 0\n" 2905 " /* Code. Separated by blank line. */\n" 2906 "\n" 2907 "# define B 0\n" 2908 " /* Code. Not aligned with # */\n" 2909 "# define C 0\n" 2910 "#endif"; 2911 const char *ToFormat = "" 2912 "void f() {\n" 2913 "#if 1\n" 2914 "/* Preprocessor aligned. */\n" 2915 "# define A 0\n" 2916 "/* Code. Separated by blank line. */\n" 2917 "\n" 2918 "# define B 0\n" 2919 " /* Code. Not aligned with # */\n" 2920 "# define C 0\n" 2921 "#endif"; 2922 EXPECT_EQ(Expected, format(ToFormat, Style)); 2923 EXPECT_EQ(Expected, format(Expected, Style)); 2924 } 2925 // Keep comments aligned with un-indented directives. 2926 { 2927 const char *Expected = "" 2928 "void f() {\n" 2929 "// Preprocessor aligned.\n" 2930 "#define A 0\n" 2931 " // Code. Separated by blank line.\n" 2932 "\n" 2933 "#define B 0\n" 2934 " // Code. Not aligned with #\n" 2935 "#define C 0\n"; 2936 const char *ToFormat = "" 2937 "void f() {\n" 2938 "// Preprocessor aligned.\n" 2939 "#define A 0\n" 2940 "// Code. Separated by blank line.\n" 2941 "\n" 2942 "#define B 0\n" 2943 " // Code. Not aligned with #\n" 2944 "#define C 0\n"; 2945 EXPECT_EQ(Expected, format(ToFormat, Style)); 2946 EXPECT_EQ(Expected, format(Expected, Style)); 2947 } 2948 // Test with tabs. 2949 Style.UseTab = FormatStyle::UT_Always; 2950 Style.IndentWidth = 8; 2951 Style.TabWidth = 8; 2952 verifyFormat("#ifdef _WIN32\n" 2953 "#\tdefine A 0\n" 2954 "#\tifdef VAR2\n" 2955 "#\t\tdefine B 1\n" 2956 "#\t\tinclude <someheader.h>\n" 2957 "#\t\tdefine MACRO \\\n" 2958 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 2959 "#\tendif\n" 2960 "#else\n" 2961 "#\tdefine A 1\n" 2962 "#endif", 2963 Style); 2964 2965 // Regression test: Multiline-macro inside include guards. 2966 verifyFormat("#ifndef HEADER_H\n" 2967 "#define HEADER_H\n" 2968 "#define A() \\\n" 2969 " int i; \\\n" 2970 " int j;\n" 2971 "#endif // HEADER_H", 2972 getLLVMStyleWithColumns(20)); 2973 } 2974 2975 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2976 verifyFormat("{\n { a #c; }\n}"); 2977 } 2978 2979 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2980 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2981 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2982 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2983 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2984 } 2985 2986 TEST_F(FormatTest, EscapedNewlines) { 2987 FormatStyle Narrow = getLLVMStyleWithColumns(11); 2988 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 2989 format("#define A \\\nint i;\\\n int j;", Narrow)); 2990 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2991 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2992 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 2993 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2994 2995 FormatStyle AlignLeft = getLLVMStyle(); 2996 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 2997 EXPECT_EQ("#define MACRO(x) \\\n" 2998 "private: \\\n" 2999 " int x(int a);\n", 3000 format("#define MACRO(x) \\\n" 3001 "private: \\\n" 3002 " int x(int a);\n", 3003 AlignLeft)); 3004 3005 // CRLF line endings 3006 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 3007 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 3008 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 3009 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3010 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 3011 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 3012 EXPECT_EQ("#define MACRO(x) \\\r\n" 3013 "private: \\\r\n" 3014 " int x(int a);\r\n", 3015 format("#define MACRO(x) \\\r\n" 3016 "private: \\\r\n" 3017 " int x(int a);\r\n", 3018 AlignLeft)); 3019 3020 FormatStyle DontAlign = getLLVMStyle(); 3021 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 3022 DontAlign.MaxEmptyLinesToKeep = 3; 3023 // FIXME: can't use verifyFormat here because the newline before 3024 // "public:" is not inserted the first time it's reformatted 3025 EXPECT_EQ("#define A \\\n" 3026 " class Foo { \\\n" 3027 " void bar(); \\\n" 3028 "\\\n" 3029 "\\\n" 3030 "\\\n" 3031 " public: \\\n" 3032 " void baz(); \\\n" 3033 " };", 3034 format("#define A \\\n" 3035 " class Foo { \\\n" 3036 " void bar(); \\\n" 3037 "\\\n" 3038 "\\\n" 3039 "\\\n" 3040 " public: \\\n" 3041 " void baz(); \\\n" 3042 " };", 3043 DontAlign)); 3044 } 3045 3046 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3047 verifyFormat("#define A \\\n" 3048 " int v( \\\n" 3049 " a); \\\n" 3050 " int i;", 3051 getLLVMStyleWithColumns(11)); 3052 } 3053 3054 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3055 EXPECT_EQ( 3056 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3057 " \\\n" 3058 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3059 "\n" 3060 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3061 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3062 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3063 "\\\n" 3064 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3065 " \n" 3066 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3067 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3068 } 3069 3070 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3071 EXPECT_EQ("int\n" 3072 "#define A\n" 3073 " a;", 3074 format("int\n#define A\na;")); 3075 verifyFormat("functionCallTo(\n" 3076 " someOtherFunction(\n" 3077 " withSomeParameters, whichInSequence,\n" 3078 " areLongerThanALine(andAnotherCall,\n" 3079 "#define A B\n" 3080 " withMoreParamters,\n" 3081 " whichStronglyInfluenceTheLayout),\n" 3082 " andMoreParameters),\n" 3083 " trailing);", 3084 getLLVMStyleWithColumns(69)); 3085 verifyFormat("Foo::Foo()\n" 3086 "#ifdef BAR\n" 3087 " : baz(0)\n" 3088 "#endif\n" 3089 "{\n" 3090 "}"); 3091 verifyFormat("void f() {\n" 3092 " if (true)\n" 3093 "#ifdef A\n" 3094 " f(42);\n" 3095 " x();\n" 3096 "#else\n" 3097 " g();\n" 3098 " x();\n" 3099 "#endif\n" 3100 "}"); 3101 verifyFormat("void f(param1, param2,\n" 3102 " param3,\n" 3103 "#ifdef A\n" 3104 " param4(param5,\n" 3105 "#ifdef A1\n" 3106 " param6,\n" 3107 "#ifdef A2\n" 3108 " param7),\n" 3109 "#else\n" 3110 " param8),\n" 3111 " param9,\n" 3112 "#endif\n" 3113 " param10,\n" 3114 "#endif\n" 3115 " param11)\n" 3116 "#else\n" 3117 " param12)\n" 3118 "#endif\n" 3119 "{\n" 3120 " x();\n" 3121 "}", 3122 getLLVMStyleWithColumns(28)); 3123 verifyFormat("#if 1\n" 3124 "int i;"); 3125 verifyFormat("#if 1\n" 3126 "#endif\n" 3127 "#if 1\n" 3128 "#else\n" 3129 "#endif\n"); 3130 verifyFormat("DEBUG({\n" 3131 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3132 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3133 "});\n" 3134 "#if a\n" 3135 "#else\n" 3136 "#endif"); 3137 3138 verifyIncompleteFormat("void f(\n" 3139 "#if A\n" 3140 ");\n" 3141 "#else\n" 3142 "#endif"); 3143 } 3144 3145 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3146 verifyFormat("#endif\n" 3147 "#if B"); 3148 } 3149 3150 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3151 FormatStyle SingleLine = getLLVMStyle(); 3152 SingleLine.AllowShortIfStatementsOnASingleLine = true; 3153 verifyFormat("#if 0\n" 3154 "#elif 1\n" 3155 "#endif\n" 3156 "void foo() {\n" 3157 " if (test) foo2();\n" 3158 "}", 3159 SingleLine); 3160 } 3161 3162 TEST_F(FormatTest, LayoutBlockInsideParens) { 3163 verifyFormat("functionCall({ int i; });"); 3164 verifyFormat("functionCall({\n" 3165 " int i;\n" 3166 " int j;\n" 3167 "});"); 3168 verifyFormat("functionCall(\n" 3169 " {\n" 3170 " int i;\n" 3171 " int j;\n" 3172 " },\n" 3173 " aaaa, bbbb, cccc);"); 3174 verifyFormat("functionA(functionB({\n" 3175 " int i;\n" 3176 " int j;\n" 3177 " }),\n" 3178 " aaaa, bbbb, cccc);"); 3179 verifyFormat("functionCall(\n" 3180 " {\n" 3181 " int i;\n" 3182 " int j;\n" 3183 " },\n" 3184 " aaaa, bbbb, // comment\n" 3185 " cccc);"); 3186 verifyFormat("functionA(functionB({\n" 3187 " int i;\n" 3188 " int j;\n" 3189 " }),\n" 3190 " aaaa, bbbb, // comment\n" 3191 " cccc);"); 3192 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3193 verifyFormat("functionCall(aaaa, bbbb, {\n" 3194 " int i;\n" 3195 " int j;\n" 3196 "});"); 3197 verifyFormat( 3198 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3199 " {\n" 3200 " int i; // break\n" 3201 " },\n" 3202 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3203 " ccccccccccccccccc));"); 3204 verifyFormat("DEBUG({\n" 3205 " if (a)\n" 3206 " f();\n" 3207 "});"); 3208 } 3209 3210 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3211 EXPECT_EQ("SOME_MACRO { int i; }\n" 3212 "int i;", 3213 format(" SOME_MACRO {int i;} int i;")); 3214 } 3215 3216 TEST_F(FormatTest, LayoutNestedBlocks) { 3217 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3218 " struct s {\n" 3219 " int i;\n" 3220 " };\n" 3221 " s kBitsToOs[] = {{10}};\n" 3222 " for (int i = 0; i < 10; ++i)\n" 3223 " return;\n" 3224 "}"); 3225 verifyFormat("call(parameter, {\n" 3226 " something();\n" 3227 " // Comment using all columns.\n" 3228 " somethingelse();\n" 3229 "});", 3230 getLLVMStyleWithColumns(40)); 3231 verifyFormat("DEBUG( //\n" 3232 " { f(); }, a);"); 3233 verifyFormat("DEBUG( //\n" 3234 " {\n" 3235 " f(); //\n" 3236 " },\n" 3237 " a);"); 3238 3239 EXPECT_EQ("call(parameter, {\n" 3240 " something();\n" 3241 " // Comment too\n" 3242 " // looooooooooong.\n" 3243 " somethingElse();\n" 3244 "});", 3245 format("call(parameter, {\n" 3246 " something();\n" 3247 " // Comment too looooooooooong.\n" 3248 " somethingElse();\n" 3249 "});", 3250 getLLVMStyleWithColumns(29))); 3251 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3252 EXPECT_EQ("DEBUG({ // comment\n" 3253 " int i;\n" 3254 "});", 3255 format("DEBUG({ // comment\n" 3256 "int i;\n" 3257 "});")); 3258 EXPECT_EQ("DEBUG({\n" 3259 " int i;\n" 3260 "\n" 3261 " // comment\n" 3262 " int j;\n" 3263 "});", 3264 format("DEBUG({\n" 3265 " int i;\n" 3266 "\n" 3267 " // comment\n" 3268 " int j;\n" 3269 "});")); 3270 3271 verifyFormat("DEBUG({\n" 3272 " if (a)\n" 3273 " return;\n" 3274 "});"); 3275 verifyGoogleFormat("DEBUG({\n" 3276 " if (a) return;\n" 3277 "});"); 3278 FormatStyle Style = getGoogleStyle(); 3279 Style.ColumnLimit = 45; 3280 verifyFormat("Debug(\n" 3281 " aaaaa,\n" 3282 " {\n" 3283 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3284 " },\n" 3285 " a);", 3286 Style); 3287 3288 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3289 3290 verifyNoCrash("^{v^{a}}"); 3291 } 3292 3293 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3294 EXPECT_EQ("#define MACRO() \\\n" 3295 " Debug(aaa, /* force line break */ \\\n" 3296 " { \\\n" 3297 " int i; \\\n" 3298 " int j; \\\n" 3299 " })", 3300 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3301 " { int i; int j; })", 3302 getGoogleStyle())); 3303 3304 EXPECT_EQ("#define A \\\n" 3305 " [] { \\\n" 3306 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3307 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3308 " }", 3309 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3310 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3311 getGoogleStyle())); 3312 } 3313 3314 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3315 EXPECT_EQ("{}", format("{}")); 3316 verifyFormat("enum E {};"); 3317 verifyFormat("enum E {}"); 3318 } 3319 3320 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3321 FormatStyle Style = getLLVMStyle(); 3322 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3323 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3324 verifyFormat("FOO_BEGIN\n" 3325 " FOO_ENTRY\n" 3326 "FOO_END", Style); 3327 verifyFormat("FOO_BEGIN\n" 3328 " NESTED_FOO_BEGIN\n" 3329 " NESTED_FOO_ENTRY\n" 3330 " NESTED_FOO_END\n" 3331 "FOO_END", Style); 3332 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3333 " int x;\n" 3334 " x = 1;\n" 3335 "FOO_END(Baz)", Style); 3336 } 3337 3338 //===----------------------------------------------------------------------===// 3339 // Line break tests. 3340 //===----------------------------------------------------------------------===// 3341 3342 TEST_F(FormatTest, PreventConfusingIndents) { 3343 verifyFormat( 3344 "void f() {\n" 3345 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3346 " parameter, parameter, parameter)),\n" 3347 " SecondLongCall(parameter));\n" 3348 "}"); 3349 verifyFormat( 3350 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3351 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3352 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3353 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3354 verifyFormat( 3355 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3356 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3357 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3358 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3359 verifyFormat( 3360 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3361 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3362 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3363 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3364 verifyFormat("int a = bbbb && ccc &&\n" 3365 " fffff(\n" 3366 "#define A Just forcing a new line\n" 3367 " ddd);"); 3368 } 3369 3370 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3371 verifyFormat( 3372 "bool aaaaaaa =\n" 3373 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3374 " bbbbbbbb();"); 3375 verifyFormat( 3376 "bool aaaaaaa =\n" 3377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3378 " bbbbbbbb();"); 3379 3380 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3381 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3382 " ccccccccc == ddddddddddd;"); 3383 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3384 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3385 " ccccccccc == ddddddddddd;"); 3386 verifyFormat( 3387 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3388 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3389 " ccccccccc == ddddddddddd;"); 3390 3391 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3392 " aaaaaa) &&\n" 3393 " bbbbbb && cccccc;"); 3394 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3395 " aaaaaa) >>\n" 3396 " bbbbbb;"); 3397 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3398 " SourceMgr.getSpellingColumnNumber(\n" 3399 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3400 " 1);"); 3401 3402 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3403 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3404 " cccccc) {\n}"); 3405 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3406 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 3407 " cccccc) {\n}"); 3408 verifyFormat("b = a &&\n" 3409 " // Comment\n" 3410 " b.c && d;"); 3411 3412 // If the LHS of a comparison is not a binary expression itself, the 3413 // additional linebreak confuses many people. 3414 verifyFormat( 3415 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3417 "}"); 3418 verifyFormat( 3419 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3420 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3421 "}"); 3422 verifyFormat( 3423 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3424 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3425 "}"); 3426 verifyFormat( 3427 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3428 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 3429 "}"); 3430 // Even explicit parentheses stress the precedence enough to make the 3431 // additional break unnecessary. 3432 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3434 "}"); 3435 // This cases is borderline, but with the indentation it is still readable. 3436 verifyFormat( 3437 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3438 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3440 "}", 3441 getLLVMStyleWithColumns(75)); 3442 3443 // If the LHS is a binary expression, we should still use the additional break 3444 // as otherwise the formatting hides the operator precedence. 3445 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3446 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3447 " 5) {\n" 3448 "}"); 3449 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3450 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 3451 " 5) {\n" 3452 "}"); 3453 3454 FormatStyle OnePerLine = getLLVMStyle(); 3455 OnePerLine.BinPackParameters = false; 3456 verifyFormat( 3457 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3458 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3460 OnePerLine); 3461 3462 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 3463 " .aaa(aaaaaaaaaaaaa) *\n" 3464 " aaaaaaa +\n" 3465 " aaaaaaa;", 3466 getLLVMStyleWithColumns(40)); 3467 } 3468 3469 TEST_F(FormatTest, ExpressionIndentation) { 3470 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3471 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3472 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3473 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3474 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3475 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3476 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3477 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3478 " ccccccccccccccccccccccccccccccccccccccccc;"); 3479 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3481 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3482 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3483 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3484 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3485 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3486 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3487 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3488 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3489 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3490 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3491 verifyFormat("if () {\n" 3492 "} else if (aaaaa && bbbbb > // break\n" 3493 " ccccc) {\n" 3494 "}"); 3495 verifyFormat("if () {\n" 3496 "} else if (aaaaa &&\n" 3497 " bbbbb > // break\n" 3498 " ccccc &&\n" 3499 " ddddd) {\n" 3500 "}"); 3501 3502 // Presence of a trailing comment used to change indentation of b. 3503 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3504 " b;\n" 3505 "return aaaaaaaaaaaaaaaaaaa +\n" 3506 " b; //", 3507 getLLVMStyleWithColumns(30)); 3508 } 3509 3510 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3511 // Not sure what the best system is here. Like this, the LHS can be found 3512 // immediately above an operator (everything with the same or a higher 3513 // indent). The RHS is aligned right of the operator and so compasses 3514 // everything until something with the same indent as the operator is found. 3515 // FIXME: Is this a good system? 3516 FormatStyle Style = getLLVMStyle(); 3517 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3518 verifyFormat( 3519 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3520 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3521 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3522 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3523 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3524 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3525 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3526 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3527 " > ccccccccccccccccccccccccccccccccccccccccc;", 3528 Style); 3529 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3530 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3531 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3532 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3533 Style); 3534 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3535 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3536 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3537 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3538 Style); 3539 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3540 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3541 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3542 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3543 Style); 3544 verifyFormat("if () {\n" 3545 "} else if (aaaaa\n" 3546 " && bbbbb // break\n" 3547 " > ccccc) {\n" 3548 "}", 3549 Style); 3550 verifyFormat("return (a)\n" 3551 " // comment\n" 3552 " + b;", 3553 Style); 3554 verifyFormat( 3555 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3556 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3557 " + cc;", 3558 Style); 3559 3560 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3561 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3562 Style); 3563 3564 // Forced by comments. 3565 verifyFormat( 3566 "unsigned ContentSize =\n" 3567 " sizeof(int16_t) // DWARF ARange version number\n" 3568 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3569 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3570 " + sizeof(int8_t); // Segment Size (in bytes)"); 3571 3572 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3573 " == boost::fusion::at_c<1>(iiii).second;", 3574 Style); 3575 3576 Style.ColumnLimit = 60; 3577 verifyFormat("zzzzzzzzzz\n" 3578 " = bbbbbbbbbbbbbbbbb\n" 3579 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3580 Style); 3581 3582 Style.ColumnLimit = 80; 3583 Style.IndentWidth = 4; 3584 Style.TabWidth = 4; 3585 Style.UseTab = FormatStyle::UT_Always; 3586 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3587 Style.AlignOperands = false; 3588 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 3589 "\t&& (someOtherLongishConditionPart1\n" 3590 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 3591 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && (someOtherLongishConditionPart1 || someOtherEvenLongerNestedConditionPart2);", 3592 Style)); 3593 } 3594 3595 TEST_F(FormatTest, EnforcedOperatorWraps) { 3596 // Here we'd like to wrap after the || operators, but a comment is forcing an 3597 // earlier wrap. 3598 verifyFormat("bool x = aaaaa //\n" 3599 " || bbbbb\n" 3600 " //\n" 3601 " || cccc;"); 3602 } 3603 3604 TEST_F(FormatTest, NoOperandAlignment) { 3605 FormatStyle Style = getLLVMStyle(); 3606 Style.AlignOperands = false; 3607 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 3608 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3609 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3610 Style); 3611 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3612 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3613 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3614 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3615 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3616 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3617 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3618 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3619 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3620 " > ccccccccccccccccccccccccccccccccccccccccc;", 3621 Style); 3622 3623 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3624 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3625 " + cc;", 3626 Style); 3627 verifyFormat("int a = aa\n" 3628 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3629 " * cccccccccccccccccccccccccccccccccccc;\n", 3630 Style); 3631 3632 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3633 verifyFormat("return (a > b\n" 3634 " // comment1\n" 3635 " // comment2\n" 3636 " || c);", 3637 Style); 3638 } 3639 3640 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3641 FormatStyle Style = getLLVMStyle(); 3642 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3643 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3644 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3645 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3646 Style); 3647 } 3648 3649 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 3650 FormatStyle Style = getLLVMStyle(); 3651 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3652 Style.BinPackArguments = false; 3653 Style.ColumnLimit = 40; 3654 verifyFormat("void test() {\n" 3655 " someFunction(\n" 3656 " this + argument + is + quite\n" 3657 " + long + so + it + gets + wrapped\n" 3658 " + but + remains + bin - packed);\n" 3659 "}", 3660 Style); 3661 verifyFormat("void test() {\n" 3662 " someFunction(arg1,\n" 3663 " this + argument + is\n" 3664 " + quite + long + so\n" 3665 " + it + gets + wrapped\n" 3666 " + but + remains + bin\n" 3667 " - packed,\n" 3668 " arg3);\n" 3669 "}", 3670 Style); 3671 verifyFormat("void test() {\n" 3672 " someFunction(\n" 3673 " arg1,\n" 3674 " this + argument + has\n" 3675 " + anotherFunc(nested,\n" 3676 " calls + whose\n" 3677 " + arguments\n" 3678 " + are + also\n" 3679 " + wrapped,\n" 3680 " in + addition)\n" 3681 " + to + being + bin - packed,\n" 3682 " arg3);\n" 3683 "}", 3684 Style); 3685 3686 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 3687 verifyFormat("void test() {\n" 3688 " someFunction(\n" 3689 " arg1,\n" 3690 " this + argument + has +\n" 3691 " anotherFunc(nested,\n" 3692 " calls + whose +\n" 3693 " arguments +\n" 3694 " are + also +\n" 3695 " wrapped,\n" 3696 " in + addition) +\n" 3697 " to + being + bin - packed,\n" 3698 " arg3);\n" 3699 "}", 3700 Style); 3701 } 3702 3703 TEST_F(FormatTest, ConstructorInitializers) { 3704 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3705 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3706 getLLVMStyleWithColumns(45)); 3707 verifyFormat("Constructor()\n" 3708 " : Inttializer(FitsOnTheLine) {}", 3709 getLLVMStyleWithColumns(44)); 3710 verifyFormat("Constructor()\n" 3711 " : Inttializer(FitsOnTheLine) {}", 3712 getLLVMStyleWithColumns(43)); 3713 3714 verifyFormat("template <typename T>\n" 3715 "Constructor() : Initializer(FitsOnTheLine) {}", 3716 getLLVMStyleWithColumns(45)); 3717 3718 verifyFormat( 3719 "SomeClass::Constructor()\n" 3720 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3721 3722 verifyFormat( 3723 "SomeClass::Constructor()\n" 3724 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3725 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3726 verifyFormat( 3727 "SomeClass::Constructor()\n" 3728 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3729 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3730 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3731 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3732 " : aaaaaaaaaa(aaaaaa) {}"); 3733 3734 verifyFormat("Constructor()\n" 3735 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3736 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3737 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3738 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3739 3740 verifyFormat("Constructor()\n" 3741 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3742 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3743 3744 verifyFormat("Constructor(int Parameter = 0)\n" 3745 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3746 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3747 verifyFormat("Constructor()\n" 3748 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3749 "}", 3750 getLLVMStyleWithColumns(60)); 3751 verifyFormat("Constructor()\n" 3752 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3753 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3754 3755 // Here a line could be saved by splitting the second initializer onto two 3756 // lines, but that is not desirable. 3757 verifyFormat("Constructor()\n" 3758 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3759 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3760 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3761 3762 FormatStyle OnePerLine = getLLVMStyle(); 3763 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3764 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3765 verifyFormat("SomeClass::Constructor()\n" 3766 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3767 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3768 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3769 OnePerLine); 3770 verifyFormat("SomeClass::Constructor()\n" 3771 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3772 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3773 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3774 OnePerLine); 3775 verifyFormat("MyClass::MyClass(int var)\n" 3776 " : some_var_(var), // 4 space indent\n" 3777 " some_other_var_(var + 1) { // lined up\n" 3778 "}", 3779 OnePerLine); 3780 verifyFormat("Constructor()\n" 3781 " : aaaaa(aaaaaa),\n" 3782 " aaaaa(aaaaaa),\n" 3783 " aaaaa(aaaaaa),\n" 3784 " aaaaa(aaaaaa),\n" 3785 " aaaaa(aaaaaa) {}", 3786 OnePerLine); 3787 verifyFormat("Constructor()\n" 3788 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3789 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3790 OnePerLine); 3791 OnePerLine.BinPackParameters = false; 3792 verifyFormat( 3793 "Constructor()\n" 3794 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3795 " aaaaaaaaaaa().aaa(),\n" 3796 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3797 OnePerLine); 3798 OnePerLine.ColumnLimit = 60; 3799 verifyFormat("Constructor()\n" 3800 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3801 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3802 OnePerLine); 3803 3804 EXPECT_EQ("Constructor()\n" 3805 " : // Comment forcing unwanted break.\n" 3806 " aaaa(aaaa) {}", 3807 format("Constructor() :\n" 3808 " // Comment forcing unwanted break.\n" 3809 " aaaa(aaaa) {}")); 3810 } 3811 3812 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 3813 FormatStyle Style = getLLVMStyle(); 3814 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 3815 3816 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3817 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 3818 getStyleWithColumns(Style, 45)); 3819 verifyFormat("Constructor() :\n" 3820 " Initializer(FitsOnTheLine) {}", 3821 getStyleWithColumns(Style, 44)); 3822 verifyFormat("Constructor() :\n" 3823 " Initializer(FitsOnTheLine) {}", 3824 getStyleWithColumns(Style, 43)); 3825 3826 verifyFormat("template <typename T>\n" 3827 "Constructor() : Initializer(FitsOnTheLine) {}", 3828 getStyleWithColumns(Style, 50)); 3829 3830 verifyFormat( 3831 "SomeClass::Constructor() :\n" 3832 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3833 Style); 3834 3835 verifyFormat( 3836 "SomeClass::Constructor() :\n" 3837 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3838 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3839 Style); 3840 verifyFormat( 3841 "SomeClass::Constructor() :\n" 3842 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3843 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3844 Style); 3845 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3846 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 3847 " aaaaaaaaaa(aaaaaa) {}", 3848 Style); 3849 3850 verifyFormat("Constructor() :\n" 3851 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3852 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3853 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3854 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 3855 Style); 3856 3857 verifyFormat("Constructor() :\n" 3858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3859 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3860 Style); 3861 3862 verifyFormat("Constructor(int Parameter = 0) :\n" 3863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3864 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 3865 Style); 3866 verifyFormat("Constructor() :\n" 3867 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3868 "}", 3869 getStyleWithColumns(Style, 60)); 3870 verifyFormat("Constructor() :\n" 3871 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3872 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 3873 Style); 3874 3875 // Here a line could be saved by splitting the second initializer onto two 3876 // lines, but that is not desirable. 3877 verifyFormat("Constructor() :\n" 3878 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3879 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3880 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3881 Style); 3882 3883 FormatStyle OnePerLine = Style; 3884 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3885 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3886 verifyFormat("SomeClass::Constructor() :\n" 3887 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3888 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3889 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3890 OnePerLine); 3891 verifyFormat("SomeClass::Constructor() :\n" 3892 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3893 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3894 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3895 OnePerLine); 3896 verifyFormat("MyClass::MyClass(int var) :\n" 3897 " some_var_(var), // 4 space indent\n" 3898 " some_other_var_(var + 1) { // lined up\n" 3899 "}", 3900 OnePerLine); 3901 verifyFormat("Constructor() :\n" 3902 " aaaaa(aaaaaa),\n" 3903 " aaaaa(aaaaaa),\n" 3904 " aaaaa(aaaaaa),\n" 3905 " aaaaa(aaaaaa),\n" 3906 " aaaaa(aaaaaa) {}", 3907 OnePerLine); 3908 verifyFormat("Constructor() :\n" 3909 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3910 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3911 OnePerLine); 3912 OnePerLine.BinPackParameters = false; 3913 verifyFormat( 3914 "Constructor() :\n" 3915 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3916 " aaaaaaaaaaa().aaa(),\n" 3917 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3918 OnePerLine); 3919 OnePerLine.ColumnLimit = 60; 3920 verifyFormat("Constructor() :\n" 3921 " aaaaaaaaaaaaaaaaaaaa(a),\n" 3922 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3923 OnePerLine); 3924 3925 EXPECT_EQ("Constructor() :\n" 3926 " // Comment forcing unwanted break.\n" 3927 " aaaa(aaaa) {}", 3928 format("Constructor() :\n" 3929 " // Comment forcing unwanted break.\n" 3930 " aaaa(aaaa) {}", 3931 Style)); 3932 3933 Style.ColumnLimit = 0; 3934 verifyFormat("SomeClass::Constructor() :\n" 3935 " a(a) {}", 3936 Style); 3937 verifyFormat("SomeClass::Constructor() noexcept :\n" 3938 " a(a) {}", 3939 Style); 3940 verifyFormat("SomeClass::Constructor() :\n" 3941 " a(a), b(b), c(c) {}", 3942 Style); 3943 verifyFormat("SomeClass::Constructor() :\n" 3944 " a(a) {\n" 3945 " foo();\n" 3946 " bar();\n" 3947 "}", 3948 Style); 3949 3950 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3951 verifyFormat("SomeClass::Constructor() :\n" 3952 " a(a), b(b), c(c) {\n" 3953 "}", 3954 Style); 3955 verifyFormat("SomeClass::Constructor() :\n" 3956 " a(a) {\n" 3957 "}", 3958 Style); 3959 3960 Style.ColumnLimit = 80; 3961 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3962 Style.ConstructorInitializerIndentWidth = 2; 3963 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 3964 Style); 3965 verifyFormat("SomeClass::Constructor() :\n" 3966 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3967 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 3968 Style); 3969 3970 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as well 3971 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 3972 verifyFormat("class SomeClass\n" 3973 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3974 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 3975 Style); 3976 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 3977 verifyFormat("class SomeClass\n" 3978 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3979 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 3980 Style); 3981 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 3982 verifyFormat("class SomeClass :\n" 3983 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3984 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 3985 Style); 3986 } 3987 3988 #ifndef EXPENSIVE_CHECKS 3989 // Expensive checks enables libstdc++ checking which includes validating the 3990 // state of ranges used in std::priority_queue - this blows out the 3991 // runtime/scalability of the function and makes this test unacceptably slow. 3992 TEST_F(FormatTest, MemoizationTests) { 3993 // This breaks if the memoization lookup does not take \c Indent and 3994 // \c LastSpace into account. 3995 verifyFormat( 3996 "extern CFRunLoopTimerRef\n" 3997 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3998 " CFTimeInterval interval, CFOptionFlags flags,\n" 3999 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 4000 " CFRunLoopTimerContext *context) {}"); 4001 4002 // Deep nesting somewhat works around our memoization. 4003 verifyFormat( 4004 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4005 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4006 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4007 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4008 " aaaaa())))))))))))))))))))))))))))))))))))))));", 4009 getLLVMStyleWithColumns(65)); 4010 verifyFormat( 4011 "aaaaa(\n" 4012 " aaaaa,\n" 4013 " aaaaa(\n" 4014 " aaaaa,\n" 4015 " aaaaa(\n" 4016 " aaaaa,\n" 4017 " aaaaa(\n" 4018 " aaaaa,\n" 4019 " aaaaa(\n" 4020 " aaaaa,\n" 4021 " aaaaa(\n" 4022 " aaaaa,\n" 4023 " aaaaa(\n" 4024 " aaaaa,\n" 4025 " aaaaa(\n" 4026 " aaaaa,\n" 4027 " aaaaa(\n" 4028 " aaaaa,\n" 4029 " aaaaa(\n" 4030 " aaaaa,\n" 4031 " aaaaa(\n" 4032 " aaaaa,\n" 4033 " aaaaa(\n" 4034 " aaaaa,\n" 4035 " aaaaa))))))))))));", 4036 getLLVMStyleWithColumns(65)); 4037 verifyFormat( 4038 "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" 4039 " a),\n" 4040 " a),\n" 4041 " a),\n" 4042 " a),\n" 4043 " a),\n" 4044 " a),\n" 4045 " a),\n" 4046 " a),\n" 4047 " a),\n" 4048 " a),\n" 4049 " a),\n" 4050 " a),\n" 4051 " a),\n" 4052 " a),\n" 4053 " a),\n" 4054 " a),\n" 4055 " a)", 4056 getLLVMStyleWithColumns(65)); 4057 4058 // This test takes VERY long when memoization is broken. 4059 FormatStyle OnePerLine = getLLVMStyle(); 4060 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4061 OnePerLine.BinPackParameters = false; 4062 std::string input = "Constructor()\n" 4063 " : aaaa(a,\n"; 4064 for (unsigned i = 0, e = 80; i != e; ++i) { 4065 input += " a,\n"; 4066 } 4067 input += " a) {}"; 4068 verifyFormat(input, OnePerLine); 4069 } 4070 #endif 4071 4072 TEST_F(FormatTest, BreaksAsHighAsPossible) { 4073 verifyFormat( 4074 "void f() {\n" 4075 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 4076 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 4077 " f();\n" 4078 "}"); 4079 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 4080 " Intervals[i - 1].getRange().getLast()) {\n}"); 4081 } 4082 4083 TEST_F(FormatTest, BreaksFunctionDeclarations) { 4084 // Principially, we break function declarations in a certain order: 4085 // 1) break amongst arguments. 4086 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 4087 " Cccccccccccccc cccccccccccccc);"); 4088 verifyFormat("template <class TemplateIt>\n" 4089 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 4090 " TemplateIt *stop) {}"); 4091 4092 // 2) break after return type. 4093 verifyFormat( 4094 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4095 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 4096 getGoogleStyle()); 4097 4098 // 3) break after (. 4099 verifyFormat( 4100 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 4101 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 4102 getGoogleStyle()); 4103 4104 // 4) break before after nested name specifiers. 4105 verifyFormat( 4106 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4107 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 4108 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 4109 getGoogleStyle()); 4110 4111 // However, there are exceptions, if a sufficient amount of lines can be 4112 // saved. 4113 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 4114 // more adjusting. 4115 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 4116 " Cccccccccccccc cccccccccc,\n" 4117 " Cccccccccccccc cccccccccc,\n" 4118 " Cccccccccccccc cccccccccc,\n" 4119 " Cccccccccccccc cccccccccc);"); 4120 verifyFormat( 4121 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4122 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4123 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4124 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 4125 getGoogleStyle()); 4126 verifyFormat( 4127 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 4128 " Cccccccccccccc cccccccccc,\n" 4129 " Cccccccccccccc cccccccccc,\n" 4130 " Cccccccccccccc cccccccccc,\n" 4131 " Cccccccccccccc cccccccccc,\n" 4132 " Cccccccccccccc cccccccccc,\n" 4133 " Cccccccccccccc cccccccccc);"); 4134 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4135 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4136 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4137 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4138 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 4139 4140 // Break after multi-line parameters. 4141 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4142 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4143 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4144 " bbbb bbbb);"); 4145 verifyFormat("void SomeLoooooooooooongFunction(\n" 4146 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 4147 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4148 " int bbbbbbbbbbbbb);"); 4149 4150 // Treat overloaded operators like other functions. 4151 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4152 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 4153 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4154 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 4155 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4156 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 4157 verifyGoogleFormat( 4158 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 4159 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 4160 verifyGoogleFormat( 4161 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 4162 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 4163 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4164 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 4165 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 4166 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 4167 verifyGoogleFormat( 4168 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 4169 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4170 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 4171 verifyGoogleFormat( 4172 "template <typename T>\n" 4173 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4174 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 4175 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 4176 4177 FormatStyle Style = getLLVMStyle(); 4178 Style.PointerAlignment = FormatStyle::PAS_Left; 4179 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4180 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 4181 Style); 4182 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 4183 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4184 Style); 4185 } 4186 4187 TEST_F(FormatTest, TrailingReturnType) { 4188 verifyFormat("auto foo() -> int;\n"); 4189 verifyFormat("struct S {\n" 4190 " auto bar() const -> int;\n" 4191 "};"); 4192 verifyFormat("template <size_t Order, typename T>\n" 4193 "auto load_img(const std::string &filename)\n" 4194 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 4195 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 4196 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 4197 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 4198 verifyFormat("template <typename T>\n" 4199 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 4200 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 4201 4202 // Not trailing return types. 4203 verifyFormat("void f() { auto a = b->c(); }"); 4204 } 4205 4206 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 4207 // Avoid breaking before trailing 'const' or other trailing annotations, if 4208 // they are not function-like. 4209 FormatStyle Style = getGoogleStyle(); 4210 Style.ColumnLimit = 47; 4211 verifyFormat("void someLongFunction(\n" 4212 " int someLoooooooooooooongParameter) const {\n}", 4213 getLLVMStyleWithColumns(47)); 4214 verifyFormat("LoooooongReturnType\n" 4215 "someLoooooooongFunction() const {}", 4216 getLLVMStyleWithColumns(47)); 4217 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 4218 " const {}", 4219 Style); 4220 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4221 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 4222 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4223 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 4224 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4225 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 4226 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 4227 " aaaaaaaaaaa aaaaa) const override;"); 4228 verifyGoogleFormat( 4229 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4230 " const override;"); 4231 4232 // Even if the first parameter has to be wrapped. 4233 verifyFormat("void someLongFunction(\n" 4234 " int someLongParameter) const {}", 4235 getLLVMStyleWithColumns(46)); 4236 verifyFormat("void someLongFunction(\n" 4237 " int someLongParameter) const {}", 4238 Style); 4239 verifyFormat("void someLongFunction(\n" 4240 " int someLongParameter) override {}", 4241 Style); 4242 verifyFormat("void someLongFunction(\n" 4243 " int someLongParameter) OVERRIDE {}", 4244 Style); 4245 verifyFormat("void someLongFunction(\n" 4246 " int someLongParameter) final {}", 4247 Style); 4248 verifyFormat("void someLongFunction(\n" 4249 " int someLongParameter) FINAL {}", 4250 Style); 4251 verifyFormat("void someLongFunction(\n" 4252 " int parameter) const override {}", 4253 Style); 4254 4255 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4256 verifyFormat("void someLongFunction(\n" 4257 " int someLongParameter) const\n" 4258 "{\n" 4259 "}", 4260 Style); 4261 4262 // Unless these are unknown annotations. 4263 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 4264 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4265 " LONG_AND_UGLY_ANNOTATION;"); 4266 4267 // Breaking before function-like trailing annotations is fine to keep them 4268 // close to their arguments. 4269 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4270 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4271 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4272 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4273 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4274 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 4275 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 4276 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 4277 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 4278 4279 verifyFormat( 4280 "void aaaaaaaaaaaaaaaaaa()\n" 4281 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 4282 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 4283 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4284 " __attribute__((unused));"); 4285 verifyGoogleFormat( 4286 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4287 " GUARDED_BY(aaaaaaaaaaaa);"); 4288 verifyGoogleFormat( 4289 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4290 " GUARDED_BY(aaaaaaaaaaaa);"); 4291 verifyGoogleFormat( 4292 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4293 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4294 verifyGoogleFormat( 4295 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4296 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4297 } 4298 4299 TEST_F(FormatTest, FunctionAnnotations) { 4300 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4301 "int OldFunction(const string ¶meter) {}"); 4302 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4303 "string OldFunction(const string ¶meter) {}"); 4304 verifyFormat("template <typename T>\n" 4305 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4306 "string OldFunction(const string ¶meter) {}"); 4307 4308 // Not function annotations. 4309 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4310 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4311 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4312 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4313 verifyFormat("MACRO(abc).function() // wrap\n" 4314 " << abc;"); 4315 verifyFormat("MACRO(abc)->function() // wrap\n" 4316 " << abc;"); 4317 verifyFormat("MACRO(abc)::function() // wrap\n" 4318 " << abc;"); 4319 } 4320 4321 TEST_F(FormatTest, BreaksDesireably) { 4322 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4323 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4324 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4325 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4326 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4327 "}"); 4328 4329 verifyFormat( 4330 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4331 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4332 4333 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4334 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4336 4337 verifyFormat( 4338 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4339 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4340 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4341 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4343 4344 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4345 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4346 4347 verifyFormat( 4348 "void f() {\n" 4349 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4350 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4351 "}"); 4352 verifyFormat( 4353 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4354 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4355 verifyFormat( 4356 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4357 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4358 verifyFormat( 4359 "aaaaaa(aaa,\n" 4360 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4362 " aaaa);"); 4363 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4365 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4366 4367 // Indent consistently independent of call expression and unary operator. 4368 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4369 " dddddddddddddddddddddddddddddd));"); 4370 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4371 " dddddddddddddddddddddddddddddd));"); 4372 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4373 " dddddddddddddddddddddddddddddd));"); 4374 4375 // This test case breaks on an incorrect memoization, i.e. an optimization not 4376 // taking into account the StopAt value. 4377 verifyFormat( 4378 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4379 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4380 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4381 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4382 4383 verifyFormat("{\n {\n {\n" 4384 " Annotation.SpaceRequiredBefore =\n" 4385 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4386 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4387 " }\n }\n}"); 4388 4389 // Break on an outer level if there was a break on an inner level. 4390 EXPECT_EQ("f(g(h(a, // comment\n" 4391 " b, c),\n" 4392 " d, e),\n" 4393 " x, y);", 4394 format("f(g(h(a, // comment\n" 4395 " b, c), d, e), x, y);")); 4396 4397 // Prefer breaking similar line breaks. 4398 verifyFormat( 4399 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4400 " NSTrackingMouseEnteredAndExited |\n" 4401 " NSTrackingActiveAlways;"); 4402 } 4403 4404 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4405 FormatStyle NoBinPacking = getGoogleStyle(); 4406 NoBinPacking.BinPackParameters = false; 4407 NoBinPacking.BinPackArguments = true; 4408 verifyFormat("void f() {\n" 4409 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4410 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4411 "}", 4412 NoBinPacking); 4413 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4414 " int aaaaaaaaaaaaaaaaaaaa,\n" 4415 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4416 NoBinPacking); 4417 4418 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4419 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4420 " vector<int> bbbbbbbbbbbbbbb);", 4421 NoBinPacking); 4422 // FIXME: This behavior difference is probably not wanted. However, currently 4423 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4424 // template arguments from BreakBeforeParameter being set because of the 4425 // one-per-line formatting. 4426 verifyFormat( 4427 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4428 " aaaaaaaaaa> aaaaaaaaaa);", 4429 NoBinPacking); 4430 verifyFormat( 4431 "void fffffffffff(\n" 4432 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4433 " aaaaaaaaaa);"); 4434 } 4435 4436 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4437 FormatStyle NoBinPacking = getGoogleStyle(); 4438 NoBinPacking.BinPackParameters = false; 4439 NoBinPacking.BinPackArguments = false; 4440 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4441 " aaaaaaaaaaaaaaaaaaaa,\n" 4442 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4443 NoBinPacking); 4444 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4445 " aaaaaaaaaaaaa,\n" 4446 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4447 NoBinPacking); 4448 verifyFormat( 4449 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4450 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4452 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4454 NoBinPacking); 4455 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4456 " .aaaaaaaaaaaaaaaaaa();", 4457 NoBinPacking); 4458 verifyFormat("void f() {\n" 4459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4460 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4461 "}", 4462 NoBinPacking); 4463 4464 verifyFormat( 4465 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4466 " aaaaaaaaaaaa,\n" 4467 " aaaaaaaaaaaa);", 4468 NoBinPacking); 4469 verifyFormat( 4470 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4471 " ddddddddddddddddddddddddddddd),\n" 4472 " test);", 4473 NoBinPacking); 4474 4475 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4476 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4477 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4478 " aaaaaaaaaaaaaaaaaa;", 4479 NoBinPacking); 4480 verifyFormat("a(\"a\"\n" 4481 " \"a\",\n" 4482 " a);"); 4483 4484 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4485 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4486 " aaaaaaaaa,\n" 4487 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4488 NoBinPacking); 4489 verifyFormat( 4490 "void f() {\n" 4491 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4492 " .aaaaaaa();\n" 4493 "}", 4494 NoBinPacking); 4495 verifyFormat( 4496 "template <class SomeType, class SomeOtherType>\n" 4497 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4498 NoBinPacking); 4499 } 4500 4501 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4502 FormatStyle Style = getLLVMStyleWithColumns(15); 4503 Style.ExperimentalAutoDetectBinPacking = true; 4504 EXPECT_EQ("aaa(aaaa,\n" 4505 " aaaa,\n" 4506 " aaaa);\n" 4507 "aaa(aaaa,\n" 4508 " aaaa,\n" 4509 " aaaa);", 4510 format("aaa(aaaa,\n" // one-per-line 4511 " aaaa,\n" 4512 " aaaa );\n" 4513 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4514 Style)); 4515 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4516 " aaaa);\n" 4517 "aaa(aaaa, aaaa,\n" 4518 " aaaa);", 4519 format("aaa(aaaa, aaaa,\n" // bin-packed 4520 " aaaa );\n" 4521 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4522 Style)); 4523 } 4524 4525 TEST_F(FormatTest, FormatsBuilderPattern) { 4526 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4527 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4528 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4529 " .StartsWith(\".init\", ORDER_INIT)\n" 4530 " .StartsWith(\".fini\", ORDER_FINI)\n" 4531 " .StartsWith(\".hash\", ORDER_HASH)\n" 4532 " .Default(ORDER_TEXT);\n"); 4533 4534 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4535 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4536 verifyFormat( 4537 "aaaaaaa->aaaaaaa\n" 4538 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4539 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4540 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4541 verifyFormat( 4542 "aaaaaaa->aaaaaaa\n" 4543 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4544 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4545 verifyFormat( 4546 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4547 " aaaaaaaaaaaaaa);"); 4548 verifyFormat( 4549 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4550 " aaaaaa->aaaaaaaaaaaa()\n" 4551 " ->aaaaaaaaaaaaaaaa(\n" 4552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4553 " ->aaaaaaaaaaaaaaaaa();"); 4554 verifyGoogleFormat( 4555 "void f() {\n" 4556 " someo->Add((new util::filetools::Handler(dir))\n" 4557 " ->OnEvent1(NewPermanentCallback(\n" 4558 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4559 " ->OnEvent2(NewPermanentCallback(\n" 4560 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4561 " ->OnEvent3(NewPermanentCallback(\n" 4562 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4563 " ->OnEvent5(NewPermanentCallback(\n" 4564 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4565 " ->OnEvent6(NewPermanentCallback(\n" 4566 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4567 "}"); 4568 4569 verifyFormat( 4570 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4571 verifyFormat("aaaaaaaaaaaaaaa()\n" 4572 " .aaaaaaaaaaaaaaa()\n" 4573 " .aaaaaaaaaaaaaaa()\n" 4574 " .aaaaaaaaaaaaaaa()\n" 4575 " .aaaaaaaaaaaaaaa();"); 4576 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4577 " .aaaaaaaaaaaaaaa()\n" 4578 " .aaaaaaaaaaaaaaa()\n" 4579 " .aaaaaaaaaaaaaaa();"); 4580 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4581 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4582 " .aaaaaaaaaaaaaaa();"); 4583 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4584 " ->aaaaaaaaaaaaaae(0)\n" 4585 " ->aaaaaaaaaaaaaaa();"); 4586 4587 // Don't linewrap after very short segments. 4588 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4589 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4590 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4591 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4592 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4593 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4594 verifyFormat("aaa()\n" 4595 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4596 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4597 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4598 4599 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4600 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4601 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4602 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4603 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4604 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4605 4606 // Prefer not to break after empty parentheses. 4607 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4608 " First->LastNewlineOffset);"); 4609 4610 // Prefer not to create "hanging" indents. 4611 verifyFormat( 4612 "return !soooooooooooooome_map\n" 4613 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4614 " .second;"); 4615 verifyFormat( 4616 "return aaaaaaaaaaaaaaaa\n" 4617 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4618 " .aaaa(aaaaaaaaaaaaaa);"); 4619 // No hanging indent here. 4620 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4621 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4622 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4623 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4624 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4625 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4626 getLLVMStyleWithColumns(60)); 4627 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4628 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4629 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4630 getLLVMStyleWithColumns(59)); 4631 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4633 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4634 4635 // Dont break if only closing statements before member call 4636 verifyFormat("test() {\n" 4637 " ([]() -> {\n" 4638 " int b = 32;\n" 4639 " return 3;\n" 4640 " }).foo();\n" 4641 "}"); 4642 verifyFormat("test() {\n" 4643 " (\n" 4644 " []() -> {\n" 4645 " int b = 32;\n" 4646 " return 3;\n" 4647 " },\n" 4648 " foo, bar)\n" 4649 " .foo();\n" 4650 "}"); 4651 verifyFormat("test() {\n" 4652 " ([]() -> {\n" 4653 " int b = 32;\n" 4654 " return 3;\n" 4655 " })\n" 4656 " .foo()\n" 4657 " .bar();\n" 4658 "}"); 4659 verifyFormat("test() {\n" 4660 " ([]() -> {\n" 4661 " int b = 32;\n" 4662 " return 3;\n" 4663 " })\n" 4664 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 4665 " \"bbbb\");\n" 4666 "}", 4667 getLLVMStyleWithColumns(30)); 4668 } 4669 4670 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4671 verifyFormat( 4672 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4673 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4674 verifyFormat( 4675 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4676 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4677 4678 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4679 " ccccccccccccccccccccccccc) {\n}"); 4680 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4681 " ccccccccccccccccccccccccc) {\n}"); 4682 4683 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4684 " ccccccccccccccccccccccccc) {\n}"); 4685 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4686 " ccccccccccccccccccccccccc) {\n}"); 4687 4688 verifyFormat( 4689 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4690 " ccccccccccccccccccccccccc) {\n}"); 4691 verifyFormat( 4692 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4693 " ccccccccccccccccccccccccc) {\n}"); 4694 4695 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4696 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4697 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4698 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4699 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4700 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4701 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4702 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4703 4704 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4705 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4706 " aaaaaaaaaaaaaaa != aa) {\n}"); 4707 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4708 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4709 " aaaaaaaaaaaaaaa != aa) {\n}"); 4710 } 4711 4712 TEST_F(FormatTest, BreaksAfterAssignments) { 4713 verifyFormat( 4714 "unsigned Cost =\n" 4715 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4716 " SI->getPointerAddressSpaceee());\n"); 4717 verifyFormat( 4718 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4719 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4720 4721 verifyFormat( 4722 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4723 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4724 verifyFormat("unsigned OriginalStartColumn =\n" 4725 " SourceMgr.getSpellingColumnNumber(\n" 4726 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4727 " 1;"); 4728 } 4729 4730 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 4731 FormatStyle Style = getLLVMStyle(); 4732 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4733 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 4734 Style); 4735 4736 Style.PenaltyBreakAssignment = 20; 4737 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 4738 " cccccccccccccccccccccccccc;", 4739 Style); 4740 } 4741 4742 TEST_F(FormatTest, AlignsAfterAssignments) { 4743 verifyFormat( 4744 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4745 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4746 verifyFormat( 4747 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4748 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4749 verifyFormat( 4750 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4751 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4752 verifyFormat( 4753 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4754 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4755 verifyFormat( 4756 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4757 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4758 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4759 } 4760 4761 TEST_F(FormatTest, AlignsAfterReturn) { 4762 verifyFormat( 4763 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4764 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4765 verifyFormat( 4766 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4767 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4768 verifyFormat( 4769 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4770 " aaaaaaaaaaaaaaaaaaaaaa();"); 4771 verifyFormat( 4772 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4773 " aaaaaaaaaaaaaaaaaaaaaa());"); 4774 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4775 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4776 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4777 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4778 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4779 verifyFormat("return\n" 4780 " // true if code is one of a or b.\n" 4781 " code == a || code == b;"); 4782 } 4783 4784 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4785 verifyFormat( 4786 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4787 " aaaaaaaaa aaaaaaa) {}"); 4788 verifyFormat( 4789 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4790 " aaaaaaaaaaa aaaaaaaaa);"); 4791 verifyFormat( 4792 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4793 " aaaaaaaaaaaaaaaaaaaaa));"); 4794 FormatStyle Style = getLLVMStyle(); 4795 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4796 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4797 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4798 Style); 4799 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4800 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4801 Style); 4802 verifyFormat("SomeLongVariableName->someFunction(\n" 4803 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4804 Style); 4805 verifyFormat( 4806 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4807 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4808 Style); 4809 verifyFormat( 4810 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4811 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4812 Style); 4813 verifyFormat( 4814 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4815 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4816 Style); 4817 4818 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4819 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4820 " b));", 4821 Style); 4822 4823 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4824 Style.BinPackArguments = false; 4825 Style.BinPackParameters = false; 4826 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4827 " aaaaaaaaaaa aaaaaaaa,\n" 4828 " aaaaaaaaa aaaaaaa,\n" 4829 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4830 Style); 4831 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4832 " aaaaaaaaaaa aaaaaaaaa,\n" 4833 " aaaaaaaaaaa aaaaaaaaa,\n" 4834 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4835 Style); 4836 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4837 " aaaaaaaaaaaaaaa,\n" 4838 " aaaaaaaaaaaaaaaaaaaaa,\n" 4839 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4840 Style); 4841 verifyFormat( 4842 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4843 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4844 Style); 4845 verifyFormat( 4846 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4847 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4848 Style); 4849 verifyFormat( 4850 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4851 " aaaaaaaaaaaaaaaaaaaaa(\n" 4852 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4853 " aaaaaaaaaaaaaaaa);", 4854 Style); 4855 verifyFormat( 4856 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4857 " aaaaaaaaaaaaaaaaaaaaa(\n" 4858 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4859 " aaaaaaaaaaaaaaaa);", 4860 Style); 4861 } 4862 4863 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4864 FormatStyle Style = getLLVMStyleWithColumns(40); 4865 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4866 " bbbbbbbbbbbbbbbbbbbbbb);", 4867 Style); 4868 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4869 Style.AlignOperands = false; 4870 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4871 " bbbbbbbbbbbbbbbbbbbbbb);", 4872 Style); 4873 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4874 Style.AlignOperands = true; 4875 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4876 " bbbbbbbbbbbbbbbbbbbbbb);", 4877 Style); 4878 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4879 Style.AlignOperands = false; 4880 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4881 " bbbbbbbbbbbbbbbbbbbbbb);", 4882 Style); 4883 } 4884 4885 TEST_F(FormatTest, BreaksConditionalExpressions) { 4886 verifyFormat( 4887 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4888 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4889 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4890 verifyFormat( 4891 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4892 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4893 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4894 verifyFormat( 4895 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4896 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4897 verifyFormat( 4898 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 4899 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4900 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4901 verifyFormat( 4902 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4903 " : aaaaaaaaaaaaa);"); 4904 verifyFormat( 4905 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4906 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4907 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4908 " aaaaaaaaaaaaa);"); 4909 verifyFormat( 4910 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4911 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4912 " aaaaaaaaaaaaa);"); 4913 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4914 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4916 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4917 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4918 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4919 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4920 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4922 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4923 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4924 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4925 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4926 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4927 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4928 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4929 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4930 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4931 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4932 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4933 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4934 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4935 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4936 " : aaaaaaaaaaaaaaaa;"); 4937 verifyFormat( 4938 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4939 " ? aaaaaaaaaaaaaaa\n" 4940 " : aaaaaaaaaaaaaaa;"); 4941 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4942 " aaaaaaaaa\n" 4943 " ? b\n" 4944 " : c);"); 4945 verifyFormat("return aaaa == bbbb\n" 4946 " // comment\n" 4947 " ? aaaa\n" 4948 " : bbbb;"); 4949 verifyFormat("unsigned Indent =\n" 4950 " format(TheLine.First,\n" 4951 " IndentForLevel[TheLine.Level] >= 0\n" 4952 " ? IndentForLevel[TheLine.Level]\n" 4953 " : TheLine * 2,\n" 4954 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4955 getLLVMStyleWithColumns(60)); 4956 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4957 " ? aaaaaaaaaaaaaaa\n" 4958 " : bbbbbbbbbbbbbbb //\n" 4959 " ? ccccccccccccccc\n" 4960 " : ddddddddddddddd;"); 4961 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4962 " ? aaaaaaaaaaaaaaa\n" 4963 " : (bbbbbbbbbbbbbbb //\n" 4964 " ? ccccccccccccccc\n" 4965 " : ddddddddddddddd);"); 4966 verifyFormat( 4967 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4968 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4969 " aaaaaaaaaaaaaaaaaaaaa +\n" 4970 " aaaaaaaaaaaaaaaaaaaaa\n" 4971 " : aaaaaaaaaa;"); 4972 verifyFormat( 4973 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4974 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4975 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4976 4977 FormatStyle NoBinPacking = getLLVMStyle(); 4978 NoBinPacking.BinPackArguments = false; 4979 verifyFormat( 4980 "void f() {\n" 4981 " g(aaa,\n" 4982 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4983 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4984 " ? aaaaaaaaaaaaaaa\n" 4985 " : aaaaaaaaaaaaaaa);\n" 4986 "}", 4987 NoBinPacking); 4988 verifyFormat( 4989 "void f() {\n" 4990 " g(aaa,\n" 4991 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4992 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4993 " ?: aaaaaaaaaaaaaaa);\n" 4994 "}", 4995 NoBinPacking); 4996 4997 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4998 " // comment.\n" 4999 " ccccccccccccccccccccccccccccccccccccccc\n" 5000 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5001 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 5002 5003 // Assignments in conditional expressions. Apparently not uncommon :-(. 5004 verifyFormat("return a != b\n" 5005 " // comment\n" 5006 " ? a = b\n" 5007 " : a = b;"); 5008 verifyFormat("return a != b\n" 5009 " // comment\n" 5010 " ? a = a != b\n" 5011 " // comment\n" 5012 " ? a = b\n" 5013 " : a\n" 5014 " : a;\n"); 5015 verifyFormat("return a != b\n" 5016 " // comment\n" 5017 " ? a\n" 5018 " : a = a != b\n" 5019 " // comment\n" 5020 " ? a = b\n" 5021 " : a;"); 5022 } 5023 5024 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 5025 FormatStyle Style = getLLVMStyle(); 5026 Style.BreakBeforeTernaryOperators = false; 5027 Style.ColumnLimit = 70; 5028 verifyFormat( 5029 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5030 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5031 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5032 Style); 5033 verifyFormat( 5034 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 5035 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5036 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5037 Style); 5038 verifyFormat( 5039 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5040 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5041 Style); 5042 verifyFormat( 5043 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 5044 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5045 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5046 Style); 5047 verifyFormat( 5048 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 5049 " aaaaaaaaaaaaa);", 5050 Style); 5051 verifyFormat( 5052 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5053 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5055 " aaaaaaaaaaaaa);", 5056 Style); 5057 verifyFormat( 5058 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5059 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5060 " aaaaaaaaaaaaa);", 5061 Style); 5062 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5064 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5066 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5067 Style); 5068 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5069 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5071 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5072 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5073 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5074 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5075 Style); 5076 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5077 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 5078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5079 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5080 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5081 Style); 5082 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5083 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5084 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5085 Style); 5086 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 5087 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5088 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5089 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5090 Style); 5091 verifyFormat( 5092 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5093 " aaaaaaaaaaaaaaa :\n" 5094 " aaaaaaaaaaaaaaa;", 5095 Style); 5096 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 5097 " aaaaaaaaa ?\n" 5098 " b :\n" 5099 " c);", 5100 Style); 5101 verifyFormat("unsigned Indent =\n" 5102 " format(TheLine.First,\n" 5103 " IndentForLevel[TheLine.Level] >= 0 ?\n" 5104 " IndentForLevel[TheLine.Level] :\n" 5105 " TheLine * 2,\n" 5106 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 5107 Style); 5108 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 5109 " aaaaaaaaaaaaaaa :\n" 5110 " bbbbbbbbbbbbbbb ? //\n" 5111 " ccccccccccccccc :\n" 5112 " ddddddddddddddd;", 5113 Style); 5114 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 5115 " aaaaaaaaaaaaaaa :\n" 5116 " (bbbbbbbbbbbbbbb ? //\n" 5117 " ccccccccccccccc :\n" 5118 " ddddddddddddddd);", 5119 Style); 5120 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5121 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 5122 " ccccccccccccccccccccccccccc;", 5123 Style); 5124 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5125 " aaaaa :\n" 5126 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 5127 Style); 5128 } 5129 5130 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 5131 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 5132 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 5133 verifyFormat("bool a = true, b = false;"); 5134 5135 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5136 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 5137 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 5138 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 5139 verifyFormat( 5140 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5141 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 5142 " d = e && f;"); 5143 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 5144 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 5145 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 5146 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 5147 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 5148 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 5149 5150 FormatStyle Style = getGoogleStyle(); 5151 Style.PointerAlignment = FormatStyle::PAS_Left; 5152 Style.DerivePointerAlignment = false; 5153 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5154 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 5155 " *b = bbbbbbbbbbbbbbbbbbb;", 5156 Style); 5157 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 5158 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 5159 Style); 5160 verifyFormat("vector<int*> a, b;", Style); 5161 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 5162 } 5163 5164 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 5165 verifyFormat("arr[foo ? bar : baz];"); 5166 verifyFormat("f()[foo ? bar : baz];"); 5167 verifyFormat("(a + b)[foo ? bar : baz];"); 5168 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 5169 } 5170 5171 TEST_F(FormatTest, AlignsStringLiterals) { 5172 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 5173 " \"short literal\");"); 5174 verifyFormat( 5175 "looooooooooooooooooooooooongFunction(\n" 5176 " \"short literal\"\n" 5177 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 5178 verifyFormat("someFunction(\"Always break between multi-line\"\n" 5179 " \" string literals\",\n" 5180 " and, other, parameters);"); 5181 EXPECT_EQ("fun + \"1243\" /* comment */\n" 5182 " \"5678\";", 5183 format("fun + \"1243\" /* comment */\n" 5184 " \"5678\";", 5185 getLLVMStyleWithColumns(28))); 5186 EXPECT_EQ( 5187 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 5188 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 5189 " \"aaaaaaaaaaaaaaaa\";", 5190 format("aaaaaa =" 5191 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 5192 "aaaaaaaaaaaaaaaaaaaaa\" " 5193 "\"aaaaaaaaaaaaaaaa\";")); 5194 verifyFormat("a = a + \"a\"\n" 5195 " \"a\"\n" 5196 " \"a\";"); 5197 verifyFormat("f(\"a\", \"b\"\n" 5198 " \"c\");"); 5199 5200 verifyFormat( 5201 "#define LL_FORMAT \"ll\"\n" 5202 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 5203 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 5204 5205 verifyFormat("#define A(X) \\\n" 5206 " \"aaaaa\" #X \"bbbbbb\" \\\n" 5207 " \"ccccc\"", 5208 getLLVMStyleWithColumns(23)); 5209 verifyFormat("#define A \"def\"\n" 5210 "f(\"abc\" A \"ghi\"\n" 5211 " \"jkl\");"); 5212 5213 verifyFormat("f(L\"a\"\n" 5214 " L\"b\");"); 5215 verifyFormat("#define A(X) \\\n" 5216 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 5217 " L\"ccccc\"", 5218 getLLVMStyleWithColumns(25)); 5219 5220 verifyFormat("f(@\"a\"\n" 5221 " @\"b\");"); 5222 verifyFormat("NSString s = @\"a\"\n" 5223 " @\"b\"\n" 5224 " @\"c\";"); 5225 verifyFormat("NSString s = @\"a\"\n" 5226 " \"b\"\n" 5227 " \"c\";"); 5228 } 5229 5230 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 5231 FormatStyle Style = getLLVMStyle(); 5232 // No declarations or definitions should be moved to own line. 5233 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 5234 verifyFormat("class A {\n" 5235 " int f() { return 1; }\n" 5236 " int g();\n" 5237 "};\n" 5238 "int f() { return 1; }\n" 5239 "int g();\n", 5240 Style); 5241 5242 // All declarations and definitions should have the return type moved to its 5243 // own 5244 // line. 5245 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 5246 verifyFormat("class E {\n" 5247 " int\n" 5248 " f() {\n" 5249 " return 1;\n" 5250 " }\n" 5251 " int\n" 5252 " g();\n" 5253 "};\n" 5254 "int\n" 5255 "f() {\n" 5256 " return 1;\n" 5257 "}\n" 5258 "int\n" 5259 "g();\n", 5260 Style); 5261 5262 // Top-level definitions, and no kinds of declarations should have the 5263 // return type moved to its own line. 5264 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 5265 verifyFormat("class B {\n" 5266 " int f() { return 1; }\n" 5267 " int g();\n" 5268 "};\n" 5269 "int\n" 5270 "f() {\n" 5271 " return 1;\n" 5272 "}\n" 5273 "int g();\n", 5274 Style); 5275 5276 // Top-level definitions and declarations should have the return type moved 5277 // to its own line. 5278 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 5279 verifyFormat("class C {\n" 5280 " int f() { return 1; }\n" 5281 " int g();\n" 5282 "};\n" 5283 "int\n" 5284 "f() {\n" 5285 " return 1;\n" 5286 "}\n" 5287 "int\n" 5288 "g();\n", 5289 Style); 5290 5291 // All definitions should have the return type moved to its own line, but no 5292 // kinds of declarations. 5293 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5294 verifyFormat("class D {\n" 5295 " int\n" 5296 " f() {\n" 5297 " return 1;\n" 5298 " }\n" 5299 " int g();\n" 5300 "};\n" 5301 "int\n" 5302 "f() {\n" 5303 " return 1;\n" 5304 "}\n" 5305 "int g();\n", 5306 Style); 5307 verifyFormat("const char *\n" 5308 "f(void) {\n" // Break here. 5309 " return \"\";\n" 5310 "}\n" 5311 "const char *bar(void);\n", // No break here. 5312 Style); 5313 verifyFormat("template <class T>\n" 5314 "T *\n" 5315 "f(T &c) {\n" // Break here. 5316 " return NULL;\n" 5317 "}\n" 5318 "template <class T> T *f(T &c);\n", // No break here. 5319 Style); 5320 verifyFormat("class C {\n" 5321 " int\n" 5322 " operator+() {\n" 5323 " return 1;\n" 5324 " }\n" 5325 " int\n" 5326 " operator()() {\n" 5327 " return 1;\n" 5328 " }\n" 5329 "};\n", 5330 Style); 5331 verifyFormat("void\n" 5332 "A::operator()() {}\n" 5333 "void\n" 5334 "A::operator>>() {}\n" 5335 "void\n" 5336 "A::operator+() {}\n", 5337 Style); 5338 verifyFormat("void *operator new(std::size_t s);", // No break here. 5339 Style); 5340 verifyFormat("void *\n" 5341 "operator new(std::size_t s) {}", 5342 Style); 5343 verifyFormat("void *\n" 5344 "operator delete[](void *ptr) {}", 5345 Style); 5346 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 5347 verifyFormat("const char *\n" 5348 "f(void)\n" // Break here. 5349 "{\n" 5350 " return \"\";\n" 5351 "}\n" 5352 "const char *bar(void);\n", // No break here. 5353 Style); 5354 verifyFormat("template <class T>\n" 5355 "T *\n" // Problem here: no line break 5356 "f(T &c)\n" // Break here. 5357 "{\n" 5358 " return NULL;\n" 5359 "}\n" 5360 "template <class T> T *f(T &c);\n", // No break here. 5361 Style); 5362 } 5363 5364 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 5365 FormatStyle NoBreak = getLLVMStyle(); 5366 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 5367 FormatStyle Break = getLLVMStyle(); 5368 Break.AlwaysBreakBeforeMultilineStrings = true; 5369 verifyFormat("aaaa = \"bbbb\"\n" 5370 " \"cccc\";", 5371 NoBreak); 5372 verifyFormat("aaaa =\n" 5373 " \"bbbb\"\n" 5374 " \"cccc\";", 5375 Break); 5376 verifyFormat("aaaa(\"bbbb\"\n" 5377 " \"cccc\");", 5378 NoBreak); 5379 verifyFormat("aaaa(\n" 5380 " \"bbbb\"\n" 5381 " \"cccc\");", 5382 Break); 5383 verifyFormat("aaaa(qqq, \"bbbb\"\n" 5384 " \"cccc\");", 5385 NoBreak); 5386 verifyFormat("aaaa(qqq,\n" 5387 " \"bbbb\"\n" 5388 " \"cccc\");", 5389 Break); 5390 verifyFormat("aaaa(qqq,\n" 5391 " L\"bbbb\"\n" 5392 " L\"cccc\");", 5393 Break); 5394 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 5395 " \"bbbb\"));", 5396 Break); 5397 verifyFormat("string s = someFunction(\n" 5398 " \"abc\"\n" 5399 " \"abc\");", 5400 Break); 5401 5402 // As we break before unary operators, breaking right after them is bad. 5403 verifyFormat("string foo = abc ? \"x\"\n" 5404 " \"blah blah blah blah blah blah\"\n" 5405 " : \"y\";", 5406 Break); 5407 5408 // Don't break if there is no column gain. 5409 verifyFormat("f(\"aaaa\"\n" 5410 " \"bbbb\");", 5411 Break); 5412 5413 // Treat literals with escaped newlines like multi-line string literals. 5414 EXPECT_EQ("x = \"a\\\n" 5415 "b\\\n" 5416 "c\";", 5417 format("x = \"a\\\n" 5418 "b\\\n" 5419 "c\";", 5420 NoBreak)); 5421 EXPECT_EQ("xxxx =\n" 5422 " \"a\\\n" 5423 "b\\\n" 5424 "c\";", 5425 format("xxxx = \"a\\\n" 5426 "b\\\n" 5427 "c\";", 5428 Break)); 5429 5430 EXPECT_EQ("NSString *const kString =\n" 5431 " @\"aaaa\"\n" 5432 " @\"bbbb\";", 5433 format("NSString *const kString = @\"aaaa\"\n" 5434 "@\"bbbb\";", 5435 Break)); 5436 5437 Break.ColumnLimit = 0; 5438 verifyFormat("const char *hello = \"hello llvm\";", Break); 5439 } 5440 5441 TEST_F(FormatTest, AlignsPipes) { 5442 verifyFormat( 5443 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5444 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5445 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5446 verifyFormat( 5447 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5448 " << aaaaaaaaaaaaaaaaaaaa;"); 5449 verifyFormat( 5450 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5451 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5452 verifyFormat( 5453 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5454 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5455 verifyFormat( 5456 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5457 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5458 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5459 verifyFormat( 5460 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5461 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5462 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5463 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5464 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5465 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5466 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5467 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5468 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5469 verifyFormat( 5470 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5471 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5472 verifyFormat( 5473 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 5474 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5475 5476 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5477 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5478 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5479 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5480 " aaaaaaaaaaaaaaaaaaaaa)\n" 5481 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5482 verifyFormat("LOG_IF(aaa == //\n" 5483 " bbb)\n" 5484 " << a << b;"); 5485 5486 // But sometimes, breaking before the first "<<" is desirable. 5487 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5488 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5489 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5490 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5491 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5492 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5493 " << BEF << IsTemplate << Description << E->getType();"); 5494 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5495 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5496 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5497 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5498 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5499 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5500 " << aaa;"); 5501 5502 verifyFormat( 5503 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5504 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5505 5506 // Incomplete string literal. 5507 EXPECT_EQ("llvm::errs() << \"\n" 5508 " << a;", 5509 format("llvm::errs() << \"\n<<a;")); 5510 5511 verifyFormat("void f() {\n" 5512 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5513 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5514 "}"); 5515 5516 // Handle 'endl'. 5517 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5518 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5519 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5520 5521 // Handle '\n'. 5522 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5523 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5524 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5525 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5526 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5527 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5528 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5529 } 5530 5531 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 5532 verifyFormat("return out << \"somepacket = {\\n\"\n" 5533 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5534 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5535 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5536 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5537 " << \"}\";"); 5538 5539 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5540 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5541 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5542 verifyFormat( 5543 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5544 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5545 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5546 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5547 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5548 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5549 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5550 verifyFormat( 5551 "void f() {\n" 5552 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5553 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5554 "}"); 5555 5556 // Breaking before the first "<<" is generally not desirable. 5557 verifyFormat( 5558 "llvm::errs()\n" 5559 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5560 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5561 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5562 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5563 getLLVMStyleWithColumns(70)); 5564 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5565 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5566 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5567 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5568 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5569 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5570 getLLVMStyleWithColumns(70)); 5571 5572 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5573 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5574 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 5575 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5576 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5577 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 5578 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 5579 " (aaaa + aaaa);", 5580 getLLVMStyleWithColumns(40)); 5581 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 5582 " (aaaaaaa + aaaaa));", 5583 getLLVMStyleWithColumns(40)); 5584 verifyFormat( 5585 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 5586 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 5587 " bbbbbbbbbbbbbbbbbbbbbbb);"); 5588 } 5589 5590 TEST_F(FormatTest, UnderstandsEquals) { 5591 verifyFormat( 5592 "aaaaaaaaaaaaaaaaa =\n" 5593 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5594 verifyFormat( 5595 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5596 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5597 verifyFormat( 5598 "if (a) {\n" 5599 " f();\n" 5600 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5601 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5602 "}"); 5603 5604 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5605 " 100000000 + 10000000) {\n}"); 5606 } 5607 5608 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5609 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5610 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5611 5612 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5613 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5614 5615 verifyFormat( 5616 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5617 " Parameter2);"); 5618 5619 verifyFormat( 5620 "ShortObject->shortFunction(\n" 5621 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5622 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5623 5624 verifyFormat("loooooooooooooongFunction(\n" 5625 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5626 5627 verifyFormat( 5628 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5629 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5630 5631 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5632 " .WillRepeatedly(Return(SomeValue));"); 5633 verifyFormat("void f() {\n" 5634 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5635 " .Times(2)\n" 5636 " .WillRepeatedly(Return(SomeValue));\n" 5637 "}"); 5638 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5639 " ccccccccccccccccccccccc);"); 5640 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5641 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5642 " .aaaaa(aaaaa),\n" 5643 " aaaaaaaaaaaaaaaaaaaaa);"); 5644 verifyFormat("void f() {\n" 5645 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5646 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5647 "}"); 5648 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5649 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5650 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5651 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5652 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5653 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5654 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5655 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5656 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5657 "}"); 5658 5659 // Here, it is not necessary to wrap at "." or "->". 5660 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5661 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5662 verifyFormat( 5663 "aaaaaaaaaaa->aaaaaaaaa(\n" 5664 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5665 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5666 5667 verifyFormat( 5668 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5669 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5670 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5671 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5672 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5673 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5674 5675 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5676 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5677 " .a();"); 5678 5679 FormatStyle NoBinPacking = getLLVMStyle(); 5680 NoBinPacking.BinPackParameters = false; 5681 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5682 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5683 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5684 " aaaaaaaaaaaaaaaaaaa,\n" 5685 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5686 NoBinPacking); 5687 5688 // If there is a subsequent call, change to hanging indentation. 5689 verifyFormat( 5690 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5691 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5692 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5693 verifyFormat( 5694 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5695 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5696 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5697 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5698 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5699 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5700 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5701 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5702 } 5703 5704 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5705 verifyFormat("template <typename T>\n" 5706 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5707 verifyFormat("template <typename T>\n" 5708 "// T should be one of {A, B}.\n" 5709 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5710 verifyFormat( 5711 "template <typename T>\n" 5712 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5713 verifyFormat("template <typename T>\n" 5714 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5715 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5716 verifyFormat( 5717 "template <typename T>\n" 5718 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5719 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5720 verifyFormat( 5721 "template <typename T>\n" 5722 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5723 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5724 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5725 verifyFormat("template <typename T>\n" 5726 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5727 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5728 verifyFormat( 5729 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5730 " typename T4 = char>\n" 5731 "void f();"); 5732 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5733 " template <typename> class cccccccccccccccccccccc,\n" 5734 " typename ddddddddddddd>\n" 5735 "class C {};"); 5736 verifyFormat( 5737 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5738 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5739 5740 verifyFormat("void f() {\n" 5741 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5742 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5743 "}"); 5744 5745 verifyFormat("template <typename T> class C {};"); 5746 verifyFormat("template <typename T> void f();"); 5747 verifyFormat("template <typename T> void f() {}"); 5748 verifyFormat( 5749 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5750 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5752 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5754 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5755 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5756 getLLVMStyleWithColumns(72)); 5757 EXPECT_EQ("static_cast<A< //\n" 5758 " B> *>(\n" 5759 "\n" 5760 ");", 5761 format("static_cast<A<//\n" 5762 " B>*>(\n" 5763 "\n" 5764 " );")); 5765 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5766 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5767 5768 FormatStyle AlwaysBreak = getLLVMStyle(); 5769 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 5770 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5771 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5772 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5773 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5774 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5775 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5776 verifyFormat("template <template <typename> class Fooooooo,\n" 5777 " template <typename> class Baaaaaaar>\n" 5778 "struct C {};", 5779 AlwaysBreak); 5780 verifyFormat("template <typename T> // T can be A, B or C.\n" 5781 "struct C {};", 5782 AlwaysBreak); 5783 verifyFormat("template <enum E> class A {\n" 5784 "public:\n" 5785 " E *f();\n" 5786 "};"); 5787 5788 FormatStyle NeverBreak = getLLVMStyle(); 5789 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 5790 verifyFormat("template <typename T> class C {};", NeverBreak); 5791 verifyFormat("template <typename T> void f();", NeverBreak); 5792 verifyFormat("template <typename T> void f() {}", NeverBreak); 5793 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}", 5794 NeverBreak); 5795 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5796 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5797 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 5798 NeverBreak); 5799 verifyFormat("template <template <typename> class Fooooooo,\n" 5800 " template <typename> class Baaaaaaar>\n" 5801 "struct C {};", 5802 NeverBreak); 5803 verifyFormat("template <typename T> // T can be A, B or C.\n" 5804 "struct C {};", 5805 NeverBreak); 5806 verifyFormat("template <enum E> class A {\n" 5807 "public:\n" 5808 " E *f();\n" 5809 "};", NeverBreak); 5810 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 5811 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}", 5812 NeverBreak); 5813 } 5814 5815 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 5816 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 5817 Style.ColumnLimit = 60; 5818 EXPECT_EQ("// Baseline - no comments.\n" 5819 "template <\n" 5820 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 5821 "void f() {}", 5822 format("// Baseline - no comments.\n" 5823 "template <\n" 5824 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 5825 "void f() {}", 5826 Style)); 5827 5828 EXPECT_EQ("template <\n" 5829 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 5830 "void f() {}", 5831 format("template <\n" 5832 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 5833 "void f() {}", 5834 Style)); 5835 5836 EXPECT_EQ( 5837 "template <\n" 5838 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 5839 "void f() {}", 5840 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 5841 "void f() {}", 5842 Style)); 5843 5844 EXPECT_EQ( 5845 "template <\n" 5846 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 5847 " // multiline\n" 5848 "void f() {}", 5849 format("template <\n" 5850 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 5851 " // multiline\n" 5852 "void f() {}", 5853 Style)); 5854 5855 EXPECT_EQ( 5856 "template <typename aaaaaaaaaa<\n" 5857 " bbbbbbbbbbbb>::value> // trailing loooong\n" 5858 "void f() {}", 5859 format( 5860 "template <\n" 5861 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 5862 "void f() {}", 5863 Style)); 5864 } 5865 5866 TEST_F(FormatTest, WrapsTemplateParameters) { 5867 FormatStyle Style = getLLVMStyle(); 5868 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5869 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5870 verifyFormat( 5871 "template <typename... a> struct q {};\n" 5872 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5873 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5874 " y;", 5875 Style); 5876 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5877 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5878 verifyFormat( 5879 "template <typename... a> struct r {};\n" 5880 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5881 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5882 " y;", 5883 Style); 5884 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5885 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5886 verifyFormat( 5887 "template <typename... a> struct s {};\n" 5888 "extern s<\n" 5889 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5890 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5891 " y;", 5892 Style); 5893 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5894 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5895 verifyFormat( 5896 "template <typename... a> struct t {};\n" 5897 "extern t<\n" 5898 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5899 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5900 " y;", 5901 Style); 5902 } 5903 5904 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5905 verifyFormat( 5906 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5907 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5908 verifyFormat( 5909 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5910 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5911 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5912 5913 // FIXME: Should we have the extra indent after the second break? 5914 verifyFormat( 5915 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5917 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5918 5919 verifyFormat( 5920 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5921 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5922 5923 // Breaking at nested name specifiers is generally not desirable. 5924 verifyFormat( 5925 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5926 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5927 5928 verifyFormat( 5929 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 5930 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5931 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5932 " aaaaaaaaaaaaaaaaaaaaa);", 5933 getLLVMStyleWithColumns(74)); 5934 5935 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5936 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5937 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5938 } 5939 5940 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5941 verifyFormat("A<int> a;"); 5942 verifyFormat("A<A<A<int>>> a;"); 5943 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5944 verifyFormat("bool x = a < 1 || 2 > a;"); 5945 verifyFormat("bool x = 5 < f<int>();"); 5946 verifyFormat("bool x = f<int>() > 5;"); 5947 verifyFormat("bool x = 5 < a<int>::x;"); 5948 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5949 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5950 5951 verifyGoogleFormat("A<A<int>> a;"); 5952 verifyGoogleFormat("A<A<A<int>>> a;"); 5953 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5954 verifyGoogleFormat("A<A<int> > a;"); 5955 verifyGoogleFormat("A<A<A<int> > > a;"); 5956 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5957 verifyGoogleFormat("A<::A<int>> a;"); 5958 verifyGoogleFormat("A<::A> a;"); 5959 verifyGoogleFormat("A< ::A> a;"); 5960 verifyGoogleFormat("A< ::A<int> > a;"); 5961 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5962 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5963 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5964 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5965 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5966 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5967 5968 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5969 5970 verifyFormat("test >> a >> b;"); 5971 verifyFormat("test << a >> b;"); 5972 5973 verifyFormat("f<int>();"); 5974 verifyFormat("template <typename T> void f() {}"); 5975 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5976 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5977 "sizeof(char)>::type>;"); 5978 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5979 verifyFormat("f(a.operator()<A>());"); 5980 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5981 " .template operator()<A>());", 5982 getLLVMStyleWithColumns(35)); 5983 5984 // Not template parameters. 5985 verifyFormat("return a < b && c > d;"); 5986 verifyFormat("void f() {\n" 5987 " while (a < b && c > d) {\n" 5988 " }\n" 5989 "}"); 5990 verifyFormat("template <typename... Types>\n" 5991 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5992 5993 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5994 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5995 getLLVMStyleWithColumns(60)); 5996 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5997 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5998 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5999 } 6000 6001 TEST_F(FormatTest, BitshiftOperatorWidth) { 6002 EXPECT_EQ("int a = 1 << 2; /* foo\n" 6003 " bar */", 6004 format("int a=1<<2; /* foo\n" 6005 " bar */")); 6006 6007 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 6008 " bar */", 6009 format("int b =256>>1 ; /* foo\n" 6010 " bar */")); 6011 } 6012 6013 TEST_F(FormatTest, UnderstandsBinaryOperators) { 6014 verifyFormat("COMPARE(a, ==, b);"); 6015 verifyFormat("auto s = sizeof...(Ts) - 1;"); 6016 } 6017 6018 TEST_F(FormatTest, UnderstandsPointersToMembers) { 6019 verifyFormat("int A::*x;"); 6020 verifyFormat("int (S::*func)(void *);"); 6021 verifyFormat("void f() { int (S::*func)(void *); }"); 6022 verifyFormat("typedef bool *(Class::*Member)() const;"); 6023 verifyFormat("void f() {\n" 6024 " (a->*f)();\n" 6025 " a->*x;\n" 6026 " (a.*f)();\n" 6027 " ((*a).*f)();\n" 6028 " a.*x;\n" 6029 "}"); 6030 verifyFormat("void f() {\n" 6031 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6032 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 6033 "}"); 6034 verifyFormat( 6035 "(aaaaaaaaaa->*bbbbbbb)(\n" 6036 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6037 FormatStyle Style = getLLVMStyle(); 6038 Style.PointerAlignment = FormatStyle::PAS_Left; 6039 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 6040 } 6041 6042 TEST_F(FormatTest, UnderstandsUnaryOperators) { 6043 verifyFormat("int a = -2;"); 6044 verifyFormat("f(-1, -2, -3);"); 6045 verifyFormat("a[-1] = 5;"); 6046 verifyFormat("int a = 5 + -2;"); 6047 verifyFormat("if (i == -1) {\n}"); 6048 verifyFormat("if (i != -1) {\n}"); 6049 verifyFormat("if (i > -1) {\n}"); 6050 verifyFormat("if (i < -1) {\n}"); 6051 verifyFormat("++(a->f());"); 6052 verifyFormat("--(a->f());"); 6053 verifyFormat("(a->f())++;"); 6054 verifyFormat("a[42]++;"); 6055 verifyFormat("if (!(a->f())) {\n}"); 6056 verifyFormat("if (!+i) {\n}"); 6057 verifyFormat("~&a;"); 6058 6059 verifyFormat("a-- > b;"); 6060 verifyFormat("b ? -a : c;"); 6061 verifyFormat("n * sizeof char16;"); 6062 verifyFormat("n * alignof char16;", getGoogleStyle()); 6063 verifyFormat("sizeof(char);"); 6064 verifyFormat("alignof(char);", getGoogleStyle()); 6065 6066 verifyFormat("return -1;"); 6067 verifyFormat("switch (a) {\n" 6068 "case -1:\n" 6069 " break;\n" 6070 "}"); 6071 verifyFormat("#define X -1"); 6072 verifyFormat("#define X -kConstant"); 6073 6074 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 6075 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 6076 6077 verifyFormat("int a = /* confusing comment */ -1;"); 6078 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 6079 verifyFormat("int a = i /* confusing comment */++;"); 6080 } 6081 6082 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 6083 verifyFormat("if (!aaaaaaaaaa( // break\n" 6084 " aaaaa)) {\n" 6085 "}"); 6086 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 6087 " aaaaa));"); 6088 verifyFormat("*aaa = aaaaaaa( // break\n" 6089 " bbbbbb);"); 6090 } 6091 6092 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 6093 verifyFormat("bool operator<();"); 6094 verifyFormat("bool operator>();"); 6095 verifyFormat("bool operator=();"); 6096 verifyFormat("bool operator==();"); 6097 verifyFormat("bool operator!=();"); 6098 verifyFormat("int operator+();"); 6099 verifyFormat("int operator++();"); 6100 verifyFormat("int operator++(int) volatile noexcept;"); 6101 verifyFormat("bool operator,();"); 6102 verifyFormat("bool operator();"); 6103 verifyFormat("bool operator()();"); 6104 verifyFormat("bool operator[]();"); 6105 verifyFormat("operator bool();"); 6106 verifyFormat("operator int();"); 6107 verifyFormat("operator void *();"); 6108 verifyFormat("operator SomeType<int>();"); 6109 verifyFormat("operator SomeType<int, int>();"); 6110 verifyFormat("operator SomeType<SomeType<int>>();"); 6111 verifyFormat("void *operator new(std::size_t size);"); 6112 verifyFormat("void *operator new[](std::size_t size);"); 6113 verifyFormat("void operator delete(void *ptr);"); 6114 verifyFormat("void operator delete[](void *ptr);"); 6115 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 6116 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 6117 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 6118 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 6119 6120 verifyFormat( 6121 "ostream &operator<<(ostream &OutputStream,\n" 6122 " SomeReallyLongType WithSomeReallyLongValue);"); 6123 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 6124 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 6125 " return left.group < right.group;\n" 6126 "}"); 6127 verifyFormat("SomeType &operator=(const SomeType &S);"); 6128 verifyFormat("f.template operator()<int>();"); 6129 6130 verifyGoogleFormat("operator void*();"); 6131 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 6132 verifyGoogleFormat("operator ::A();"); 6133 6134 verifyFormat("using A::operator+;"); 6135 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 6136 "int i;"); 6137 } 6138 6139 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 6140 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 6141 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 6142 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 6143 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 6144 verifyFormat("Deleted &operator=(const Deleted &) &;"); 6145 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 6146 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 6147 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 6148 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 6149 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 6150 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 6151 verifyFormat("void Fn(T const &) const &;"); 6152 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 6153 verifyFormat("template <typename T>\n" 6154 "void F(T) && = delete;", 6155 getGoogleStyle()); 6156 6157 FormatStyle AlignLeft = getLLVMStyle(); 6158 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 6159 verifyFormat("void A::b() && {}", AlignLeft); 6160 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 6161 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 6162 AlignLeft); 6163 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 6164 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 6165 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 6166 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 6167 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 6168 verifyFormat("auto Function(T) & -> void;", AlignLeft); 6169 verifyFormat("void Fn(T const&) const&;", AlignLeft); 6170 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 6171 6172 FormatStyle Spaces = getLLVMStyle(); 6173 Spaces.SpacesInCStyleCastParentheses = true; 6174 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 6175 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 6176 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 6177 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 6178 6179 Spaces.SpacesInCStyleCastParentheses = false; 6180 Spaces.SpacesInParentheses = true; 6181 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 6182 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 6183 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 6184 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 6185 } 6186 6187 TEST_F(FormatTest, UnderstandsNewAndDelete) { 6188 verifyFormat("void f() {\n" 6189 " A *a = new A;\n" 6190 " A *a = new (placement) A;\n" 6191 " delete a;\n" 6192 " delete (A *)a;\n" 6193 "}"); 6194 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 6195 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 6196 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6197 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 6198 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 6199 verifyFormat("delete[] h->p;"); 6200 } 6201 6202 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 6203 verifyFormat("int *f(int *a) {}"); 6204 verifyFormat("int main(int argc, char **argv) {}"); 6205 verifyFormat("Test::Test(int b) : a(b * b) {}"); 6206 verifyIndependentOfContext("f(a, *a);"); 6207 verifyFormat("void g() { f(*a); }"); 6208 verifyIndependentOfContext("int a = b * 10;"); 6209 verifyIndependentOfContext("int a = 10 * b;"); 6210 verifyIndependentOfContext("int a = b * c;"); 6211 verifyIndependentOfContext("int a += b * c;"); 6212 verifyIndependentOfContext("int a -= b * c;"); 6213 verifyIndependentOfContext("int a *= b * c;"); 6214 verifyIndependentOfContext("int a /= b * c;"); 6215 verifyIndependentOfContext("int a = *b;"); 6216 verifyIndependentOfContext("int a = *b * c;"); 6217 verifyIndependentOfContext("int a = b * *c;"); 6218 verifyIndependentOfContext("int a = b * (10);"); 6219 verifyIndependentOfContext("S << b * (10);"); 6220 verifyIndependentOfContext("return 10 * b;"); 6221 verifyIndependentOfContext("return *b * *c;"); 6222 verifyIndependentOfContext("return a & ~b;"); 6223 verifyIndependentOfContext("f(b ? *c : *d);"); 6224 verifyIndependentOfContext("int a = b ? *c : *d;"); 6225 verifyIndependentOfContext("*b = a;"); 6226 verifyIndependentOfContext("a * ~b;"); 6227 verifyIndependentOfContext("a * !b;"); 6228 verifyIndependentOfContext("a * +b;"); 6229 verifyIndependentOfContext("a * -b;"); 6230 verifyIndependentOfContext("a * ++b;"); 6231 verifyIndependentOfContext("a * --b;"); 6232 verifyIndependentOfContext("a[4] * b;"); 6233 verifyIndependentOfContext("a[a * a] = 1;"); 6234 verifyIndependentOfContext("f() * b;"); 6235 verifyIndependentOfContext("a * [self dostuff];"); 6236 verifyIndependentOfContext("int x = a * (a + b);"); 6237 verifyIndependentOfContext("(a *)(a + b);"); 6238 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 6239 verifyIndependentOfContext("int *pa = (int *)&a;"); 6240 verifyIndependentOfContext("return sizeof(int **);"); 6241 verifyIndependentOfContext("return sizeof(int ******);"); 6242 verifyIndependentOfContext("return (int **&)a;"); 6243 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 6244 verifyFormat("void f(Type (*parameter)[10]) {}"); 6245 verifyFormat("void f(Type (¶meter)[10]) {}"); 6246 verifyGoogleFormat("return sizeof(int**);"); 6247 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 6248 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 6249 verifyFormat("auto a = [](int **&, int ***) {};"); 6250 verifyFormat("auto PointerBinding = [](const char *S) {};"); 6251 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 6252 verifyFormat("[](const decltype(*a) &value) {}"); 6253 verifyFormat("decltype(a * b) F();"); 6254 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 6255 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 6256 verifyIndependentOfContext("typedef void (*f)(int *a);"); 6257 verifyIndependentOfContext("int i{a * b};"); 6258 verifyIndependentOfContext("aaa && aaa->f();"); 6259 verifyIndependentOfContext("int x = ~*p;"); 6260 verifyFormat("Constructor() : a(a), area(width * height) {}"); 6261 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 6262 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 6263 verifyFormat("void f() { f(a, c * d); }"); 6264 verifyFormat("void f() { f(new a(), c * d); }"); 6265 verifyFormat("void f(const MyOverride &override);"); 6266 verifyFormat("void f(const MyFinal &final);"); 6267 verifyIndependentOfContext("bool a = f() && override.f();"); 6268 verifyIndependentOfContext("bool a = f() && final.f();"); 6269 6270 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 6271 6272 verifyIndependentOfContext("A<int *> a;"); 6273 verifyIndependentOfContext("A<int **> a;"); 6274 verifyIndependentOfContext("A<int *, int *> a;"); 6275 verifyIndependentOfContext("A<int *[]> a;"); 6276 verifyIndependentOfContext( 6277 "const char *const p = reinterpret_cast<const char *const>(q);"); 6278 verifyIndependentOfContext("A<int **, int **> a;"); 6279 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 6280 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 6281 verifyFormat("for (; a && b;) {\n}"); 6282 verifyFormat("bool foo = true && [] { return false; }();"); 6283 6284 verifyFormat( 6285 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6286 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6287 6288 verifyGoogleFormat("int const* a = &b;"); 6289 verifyGoogleFormat("**outparam = 1;"); 6290 verifyGoogleFormat("*outparam = a * b;"); 6291 verifyGoogleFormat("int main(int argc, char** argv) {}"); 6292 verifyGoogleFormat("A<int*> a;"); 6293 verifyGoogleFormat("A<int**> a;"); 6294 verifyGoogleFormat("A<int*, int*> a;"); 6295 verifyGoogleFormat("A<int**, int**> a;"); 6296 verifyGoogleFormat("f(b ? *c : *d);"); 6297 verifyGoogleFormat("int a = b ? *c : *d;"); 6298 verifyGoogleFormat("Type* t = **x;"); 6299 verifyGoogleFormat("Type* t = *++*x;"); 6300 verifyGoogleFormat("*++*x;"); 6301 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 6302 verifyGoogleFormat("Type* t = x++ * y;"); 6303 verifyGoogleFormat( 6304 "const char* const p = reinterpret_cast<const char* const>(q);"); 6305 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 6306 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 6307 verifyGoogleFormat("template <typename T>\n" 6308 "void f(int i = 0, SomeType** temps = NULL);"); 6309 6310 FormatStyle Left = getLLVMStyle(); 6311 Left.PointerAlignment = FormatStyle::PAS_Left; 6312 verifyFormat("x = *a(x) = *a(y);", Left); 6313 verifyFormat("for (;; *a = b) {\n}", Left); 6314 verifyFormat("return *this += 1;", Left); 6315 verifyFormat("throw *x;", Left); 6316 verifyFormat("delete *x;", Left); 6317 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 6318 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 6319 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 6320 6321 verifyIndependentOfContext("a = *(x + y);"); 6322 verifyIndependentOfContext("a = &(x + y);"); 6323 verifyIndependentOfContext("*(x + y).call();"); 6324 verifyIndependentOfContext("&(x + y)->call();"); 6325 verifyFormat("void f() { &(*I).first; }"); 6326 6327 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 6328 verifyFormat( 6329 "int *MyValues = {\n" 6330 " *A, // Operator detection might be confused by the '{'\n" 6331 " *BB // Operator detection might be confused by previous comment\n" 6332 "};"); 6333 6334 verifyIndependentOfContext("if (int *a = &b)"); 6335 verifyIndependentOfContext("if (int &a = *b)"); 6336 verifyIndependentOfContext("if (a & b[i])"); 6337 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 6338 verifyIndependentOfContext("if (*b[i])"); 6339 verifyIndependentOfContext("if (int *a = (&b))"); 6340 verifyIndependentOfContext("while (int *a = &b)"); 6341 verifyIndependentOfContext("size = sizeof *a;"); 6342 verifyIndependentOfContext("if (a && (b = c))"); 6343 verifyFormat("void f() {\n" 6344 " for (const int &v : Values) {\n" 6345 " }\n" 6346 "}"); 6347 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 6348 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 6349 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 6350 6351 verifyFormat("#define A (!a * b)"); 6352 verifyFormat("#define MACRO \\\n" 6353 " int *i = a * b; \\\n" 6354 " void f(a *b);", 6355 getLLVMStyleWithColumns(19)); 6356 6357 verifyIndependentOfContext("A = new SomeType *[Length];"); 6358 verifyIndependentOfContext("A = new SomeType *[Length]();"); 6359 verifyIndependentOfContext("T **t = new T *;"); 6360 verifyIndependentOfContext("T **t = new T *();"); 6361 verifyGoogleFormat("A = new SomeType*[Length]();"); 6362 verifyGoogleFormat("A = new SomeType*[Length];"); 6363 verifyGoogleFormat("T** t = new T*;"); 6364 verifyGoogleFormat("T** t = new T*();"); 6365 6366 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 6367 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 6368 verifyFormat("template <bool a, bool b> " 6369 "typename t::if<x && y>::type f() {}"); 6370 verifyFormat("template <int *y> f() {}"); 6371 verifyFormat("vector<int *> v;"); 6372 verifyFormat("vector<int *const> v;"); 6373 verifyFormat("vector<int *const **const *> v;"); 6374 verifyFormat("vector<int *volatile> v;"); 6375 verifyFormat("vector<a * b> v;"); 6376 verifyFormat("foo<b && false>();"); 6377 verifyFormat("foo<b & 1>();"); 6378 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 6379 verifyFormat( 6380 "template <class T, class = typename std::enable_if<\n" 6381 " std::is_integral<T>::value &&\n" 6382 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 6383 "void F();", 6384 getLLVMStyleWithColumns(70)); 6385 verifyFormat( 6386 "template <class T,\n" 6387 " class = typename std::enable_if<\n" 6388 " std::is_integral<T>::value &&\n" 6389 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 6390 " class U>\n" 6391 "void F();", 6392 getLLVMStyleWithColumns(70)); 6393 verifyFormat( 6394 "template <class T,\n" 6395 " class = typename ::std::enable_if<\n" 6396 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 6397 "void F();", 6398 getGoogleStyleWithColumns(68)); 6399 6400 verifyIndependentOfContext("MACRO(int *i);"); 6401 verifyIndependentOfContext("MACRO(auto *a);"); 6402 verifyIndependentOfContext("MACRO(const A *a);"); 6403 verifyIndependentOfContext("MACRO(A *const a);"); 6404 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 6405 verifyFormat("void f() { f(float{1}, a * a); }"); 6406 // FIXME: Is there a way to make this work? 6407 // verifyIndependentOfContext("MACRO(A *a);"); 6408 6409 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 6410 verifyFormat("return options != nullptr && operator==(*options);"); 6411 6412 EXPECT_EQ("#define OP(x) \\\n" 6413 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6414 " return s << a.DebugString(); \\\n" 6415 " }", 6416 format("#define OP(x) \\\n" 6417 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6418 " return s << a.DebugString(); \\\n" 6419 " }", 6420 getLLVMStyleWithColumns(50))); 6421 6422 // FIXME: We cannot handle this case yet; we might be able to figure out that 6423 // foo<x> d > v; doesn't make sense. 6424 verifyFormat("foo<a<b && c> d> v;"); 6425 6426 FormatStyle PointerMiddle = getLLVMStyle(); 6427 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 6428 verifyFormat("delete *x;", PointerMiddle); 6429 verifyFormat("int * x;", PointerMiddle); 6430 verifyFormat("int *[] x;", PointerMiddle); 6431 verifyFormat("template <int * y> f() {}", PointerMiddle); 6432 verifyFormat("int * f(int * a) {}", PointerMiddle); 6433 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 6434 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 6435 verifyFormat("A<int *> a;", PointerMiddle); 6436 verifyFormat("A<int **> a;", PointerMiddle); 6437 verifyFormat("A<int *, int *> a;", PointerMiddle); 6438 verifyFormat("A<int *[]> a;", PointerMiddle); 6439 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 6440 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 6441 verifyFormat("T ** t = new T *;", PointerMiddle); 6442 6443 // Member function reference qualifiers aren't binary operators. 6444 verifyFormat("string // break\n" 6445 "operator()() & {}"); 6446 verifyFormat("string // break\n" 6447 "operator()() && {}"); 6448 verifyGoogleFormat("template <typename T>\n" 6449 "auto x() & -> int {}"); 6450 } 6451 6452 TEST_F(FormatTest, UnderstandsAttributes) { 6453 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 6454 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 6455 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 6456 FormatStyle AfterType = getLLVMStyle(); 6457 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 6458 verifyFormat("__attribute__((nodebug)) void\n" 6459 "foo() {}\n", 6460 AfterType); 6461 } 6462 6463 TEST_F(FormatTest, UnderstandsSquareAttributes) { 6464 verifyFormat("SomeType s [[unused]] (InitValue);"); 6465 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 6466 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 6467 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 6468 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 6469 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6470 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 6471 6472 // Make sure we do not mistake attributes for array subscripts. 6473 verifyFormat("int a() {}\n" 6474 "[[unused]] int b() {}\n"); 6475 verifyFormat("NSArray *arr;\n" 6476 "arr[[Foo() bar]];"); 6477 6478 // On the other hand, we still need to correctly find array subscripts. 6479 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 6480 6481 // Make sure we do not parse attributes as lambda introducers. 6482 FormatStyle MultiLineFunctions = getLLVMStyle(); 6483 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6484 verifyFormat("[[unused]] int b() {\n" 6485 " return 42;\n" 6486 "}\n", 6487 MultiLineFunctions); 6488 } 6489 6490 TEST_F(FormatTest, UnderstandsEllipsis) { 6491 verifyFormat("int printf(const char *fmt, ...);"); 6492 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 6493 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 6494 6495 FormatStyle PointersLeft = getLLVMStyle(); 6496 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 6497 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 6498 } 6499 6500 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 6501 EXPECT_EQ("int *a;\n" 6502 "int *a;\n" 6503 "int *a;", 6504 format("int *a;\n" 6505 "int* a;\n" 6506 "int *a;", 6507 getGoogleStyle())); 6508 EXPECT_EQ("int* a;\n" 6509 "int* a;\n" 6510 "int* a;", 6511 format("int* a;\n" 6512 "int* a;\n" 6513 "int *a;", 6514 getGoogleStyle())); 6515 EXPECT_EQ("int *a;\n" 6516 "int *a;\n" 6517 "int *a;", 6518 format("int *a;\n" 6519 "int * a;\n" 6520 "int * a;", 6521 getGoogleStyle())); 6522 EXPECT_EQ("auto x = [] {\n" 6523 " int *a;\n" 6524 " int *a;\n" 6525 " int *a;\n" 6526 "};", 6527 format("auto x=[]{int *a;\n" 6528 "int * a;\n" 6529 "int * a;};", 6530 getGoogleStyle())); 6531 } 6532 6533 TEST_F(FormatTest, UnderstandsRvalueReferences) { 6534 verifyFormat("int f(int &&a) {}"); 6535 verifyFormat("int f(int a, char &&b) {}"); 6536 verifyFormat("void f() { int &&a = b; }"); 6537 verifyGoogleFormat("int f(int a, char&& b) {}"); 6538 verifyGoogleFormat("void f() { int&& a = b; }"); 6539 6540 verifyIndependentOfContext("A<int &&> a;"); 6541 verifyIndependentOfContext("A<int &&, int &&> a;"); 6542 verifyGoogleFormat("A<int&&> a;"); 6543 verifyGoogleFormat("A<int&&, int&&> a;"); 6544 6545 // Not rvalue references: 6546 verifyFormat("template <bool B, bool C> class A {\n" 6547 " static_assert(B && C, \"Something is wrong\");\n" 6548 "};"); 6549 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 6550 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 6551 verifyFormat("#define A(a, b) (a && b)"); 6552 } 6553 6554 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 6555 verifyFormat("void f() {\n" 6556 " x[aaaaaaaaa -\n" 6557 " b] = 23;\n" 6558 "}", 6559 getLLVMStyleWithColumns(15)); 6560 } 6561 6562 TEST_F(FormatTest, FormatsCasts) { 6563 verifyFormat("Type *A = static_cast<Type *>(P);"); 6564 verifyFormat("Type *A = (Type *)P;"); 6565 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6566 verifyFormat("int a = (int)(2.0f);"); 6567 verifyFormat("int a = (int)2.0f;"); 6568 verifyFormat("x[(int32)y];"); 6569 verifyFormat("x = (int32)y;"); 6570 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6571 verifyFormat("int a = (int)*b;"); 6572 verifyFormat("int a = (int)2.0f;"); 6573 verifyFormat("int a = (int)~0;"); 6574 verifyFormat("int a = (int)++a;"); 6575 verifyFormat("int a = (int)sizeof(int);"); 6576 verifyFormat("int a = (int)+2;"); 6577 verifyFormat("my_int a = (my_int)2.0f;"); 6578 verifyFormat("my_int a = (my_int)sizeof(int);"); 6579 verifyFormat("return (my_int)aaa;"); 6580 verifyFormat("#define x ((int)-1)"); 6581 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6582 verifyFormat("#define p(q) ((int *)&q)"); 6583 verifyFormat("fn(a)(b) + 1;"); 6584 6585 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6586 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6587 verifyFormat("my_int a = (my_int)~0;"); 6588 verifyFormat("my_int a = (my_int)++a;"); 6589 verifyFormat("my_int a = (my_int)-2;"); 6590 verifyFormat("my_int a = (my_int)1;"); 6591 verifyFormat("my_int a = (my_int *)1;"); 6592 verifyFormat("my_int a = (const my_int)-1;"); 6593 verifyFormat("my_int a = (const my_int *)-1;"); 6594 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6595 verifyFormat("my_int a = (ns::my_int)-2;"); 6596 verifyFormat("case (my_int)ONE:"); 6597 verifyFormat("auto x = (X)this;"); 6598 6599 // FIXME: single value wrapped with paren will be treated as cast. 6600 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6601 6602 verifyFormat("{ (void)F; }"); 6603 6604 // Don't break after a cast's 6605 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6606 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6607 " bbbbbbbbbbbbbbbbbbbbbb);"); 6608 6609 // These are not casts. 6610 verifyFormat("void f(int *) {}"); 6611 verifyFormat("f(foo)->b;"); 6612 verifyFormat("f(foo).b;"); 6613 verifyFormat("f(foo)(b);"); 6614 verifyFormat("f(foo)[b];"); 6615 verifyFormat("[](foo) { return 4; }(bar);"); 6616 verifyFormat("(*funptr)(foo)[4];"); 6617 verifyFormat("funptrs[4](foo)[4];"); 6618 verifyFormat("void f(int *);"); 6619 verifyFormat("void f(int *) = 0;"); 6620 verifyFormat("void f(SmallVector<int>) {}"); 6621 verifyFormat("void f(SmallVector<int>);"); 6622 verifyFormat("void f(SmallVector<int>) = 0;"); 6623 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6624 verifyFormat("int a = sizeof(int) * b;"); 6625 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6626 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6627 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6628 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6629 6630 // These are not casts, but at some point were confused with casts. 6631 verifyFormat("virtual void foo(int *) override;"); 6632 verifyFormat("virtual void foo(char &) const;"); 6633 verifyFormat("virtual void foo(int *a, char *) const;"); 6634 verifyFormat("int a = sizeof(int *) + b;"); 6635 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6636 verifyFormat("bool b = f(g<int>) && c;"); 6637 verifyFormat("typedef void (*f)(int i) func;"); 6638 6639 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6640 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6641 // FIXME: The indentation here is not ideal. 6642 verifyFormat( 6643 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6644 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6645 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6646 } 6647 6648 TEST_F(FormatTest, FormatsFunctionTypes) { 6649 verifyFormat("A<bool()> a;"); 6650 verifyFormat("A<SomeType()> a;"); 6651 verifyFormat("A<void (*)(int, std::string)> a;"); 6652 verifyFormat("A<void *(int)>;"); 6653 verifyFormat("void *(*a)(int *, SomeType *);"); 6654 verifyFormat("int (*func)(void *);"); 6655 verifyFormat("void f() { int (*func)(void *); }"); 6656 verifyFormat("template <class CallbackClass>\n" 6657 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6658 6659 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6660 verifyGoogleFormat("void* (*a)(int);"); 6661 verifyGoogleFormat( 6662 "template <class CallbackClass>\n" 6663 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6664 6665 // Other constructs can look somewhat like function types: 6666 verifyFormat("A<sizeof(*x)> a;"); 6667 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6668 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6669 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6670 verifyFormat("int x = f(&h)();"); 6671 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6672 verifyFormat("std::function<\n" 6673 " LooooooooooongTemplatedType<\n" 6674 " SomeType>*(\n" 6675 " LooooooooooooooooongType type)>\n" 6676 " function;", 6677 getGoogleStyleWithColumns(40)); 6678 } 6679 6680 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6681 verifyFormat("A (*foo_)[6];"); 6682 verifyFormat("vector<int> (*foo_)[6];"); 6683 } 6684 6685 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6686 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6687 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6688 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6689 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6690 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6691 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6692 6693 // Different ways of ()-initializiation. 6694 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6695 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6696 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6697 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6698 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6699 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6700 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6701 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6702 6703 // Lambdas should not confuse the variable declaration heuristic. 6704 verifyFormat("LooooooooooooooooongType\n" 6705 " variable(nullptr, [](A *a) {});", 6706 getLLVMStyleWithColumns(40)); 6707 } 6708 6709 TEST_F(FormatTest, BreaksLongDeclarations) { 6710 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6711 " AnotherNameForTheLongType;"); 6712 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6713 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6714 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6715 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6716 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6717 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6718 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6719 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6720 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6721 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6722 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6723 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6724 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6725 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6726 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6727 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6728 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6729 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6730 FormatStyle Indented = getLLVMStyle(); 6731 Indented.IndentWrappedFunctionNames = true; 6732 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6733 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6734 Indented); 6735 verifyFormat( 6736 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6737 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6738 Indented); 6739 verifyFormat( 6740 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6741 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6742 Indented); 6743 verifyFormat( 6744 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6745 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6746 Indented); 6747 6748 // FIXME: Without the comment, this breaks after "(". 6749 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6750 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6751 getGoogleStyle()); 6752 6753 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6754 " int LoooooooooooooooooooongParam2) {}"); 6755 verifyFormat( 6756 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6757 " SourceLocation L, IdentifierIn *II,\n" 6758 " Type *T) {}"); 6759 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6760 "ReallyReaaallyLongFunctionName(\n" 6761 " const std::string &SomeParameter,\n" 6762 " const SomeType<string, SomeOtherTemplateParameter>\n" 6763 " &ReallyReallyLongParameterName,\n" 6764 " const SomeType<string, SomeOtherTemplateParameter>\n" 6765 " &AnotherLongParameterName) {}"); 6766 verifyFormat("template <typename A>\n" 6767 "SomeLoooooooooooooooooooooongType<\n" 6768 " typename some_namespace::SomeOtherType<A>::Type>\n" 6769 "Function() {}"); 6770 6771 verifyGoogleFormat( 6772 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6773 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6774 verifyGoogleFormat( 6775 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6776 " SourceLocation L) {}"); 6777 verifyGoogleFormat( 6778 "some_namespace::LongReturnType\n" 6779 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6780 " int first_long_parameter, int second_parameter) {}"); 6781 6782 verifyGoogleFormat("template <typename T>\n" 6783 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6784 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6785 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6786 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6787 6788 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6789 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6790 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6791 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6792 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6793 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6794 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6795 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6796 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6797 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6798 6799 verifyFormat("template <typename T> // Templates on own line.\n" 6800 "static int // Some comment.\n" 6801 "MyFunction(int a);", 6802 getLLVMStyle()); 6803 } 6804 6805 TEST_F(FormatTest, FormatsArrays) { 6806 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6807 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6808 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6809 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6810 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6811 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6812 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6813 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6814 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6815 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6816 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6817 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6818 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6819 verifyFormat( 6820 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6821 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6822 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6823 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6824 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6825 6826 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6827 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6828 verifyFormat( 6829 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6830 " .aaaaaaa[0]\n" 6831 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6832 verifyFormat("a[::b::c];"); 6833 6834 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6835 6836 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6837 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6838 } 6839 6840 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6841 verifyFormat("(a)->b();"); 6842 verifyFormat("--a;"); 6843 } 6844 6845 TEST_F(FormatTest, HandlesIncludeDirectives) { 6846 verifyFormat("#include <string>\n" 6847 "#include <a/b/c.h>\n" 6848 "#include \"a/b/string\"\n" 6849 "#include \"string.h\"\n" 6850 "#include \"string.h\"\n" 6851 "#include <a-a>\n" 6852 "#include < path with space >\n" 6853 "#include_next <test.h>" 6854 "#include \"abc.h\" // this is included for ABC\n" 6855 "#include \"some long include\" // with a comment\n" 6856 "#include \"some very long include path\"\n" 6857 "#include <some/very/long/include/path>\n", 6858 getLLVMStyleWithColumns(35)); 6859 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6860 EXPECT_EQ("#include <a>", format("#include<a>")); 6861 6862 verifyFormat("#import <string>"); 6863 verifyFormat("#import <a/b/c.h>"); 6864 verifyFormat("#import \"a/b/string\""); 6865 verifyFormat("#import \"string.h\""); 6866 verifyFormat("#import \"string.h\""); 6867 verifyFormat("#if __has_include(<strstream>)\n" 6868 "#include <strstream>\n" 6869 "#endif"); 6870 6871 verifyFormat("#define MY_IMPORT <a/b>"); 6872 6873 verifyFormat("#if __has_include(<a/b>)"); 6874 verifyFormat("#if __has_include_next(<a/b>)"); 6875 verifyFormat("#define F __has_include(<a/b>)"); 6876 verifyFormat("#define F __has_include_next(<a/b>)"); 6877 6878 // Protocol buffer definition or missing "#". 6879 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6880 getLLVMStyleWithColumns(30)); 6881 6882 FormatStyle Style = getLLVMStyle(); 6883 Style.AlwaysBreakBeforeMultilineStrings = true; 6884 Style.ColumnLimit = 0; 6885 verifyFormat("#import \"abc.h\"", Style); 6886 6887 // But 'import' might also be a regular C++ namespace. 6888 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6889 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6890 } 6891 6892 //===----------------------------------------------------------------------===// 6893 // Error recovery tests. 6894 //===----------------------------------------------------------------------===// 6895 6896 TEST_F(FormatTest, IncompleteParameterLists) { 6897 FormatStyle NoBinPacking = getLLVMStyle(); 6898 NoBinPacking.BinPackParameters = false; 6899 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6900 " double *min_x,\n" 6901 " double *max_x,\n" 6902 " double *min_y,\n" 6903 " double *max_y,\n" 6904 " double *min_z,\n" 6905 " double *max_z, ) {}", 6906 NoBinPacking); 6907 } 6908 6909 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6910 verifyFormat("void f() { return; }\n42"); 6911 verifyFormat("void f() {\n" 6912 " if (0)\n" 6913 " return;\n" 6914 "}\n" 6915 "42"); 6916 verifyFormat("void f() { return }\n42"); 6917 verifyFormat("void f() {\n" 6918 " if (0)\n" 6919 " return\n" 6920 "}\n" 6921 "42"); 6922 } 6923 6924 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6925 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6926 EXPECT_EQ("void f() {\n" 6927 " if (a)\n" 6928 " return\n" 6929 "}", 6930 format("void f ( ) { if ( a ) return }")); 6931 EXPECT_EQ("namespace N {\n" 6932 "void f()\n" 6933 "}", 6934 format("namespace N { void f() }")); 6935 EXPECT_EQ("namespace N {\n" 6936 "void f() {}\n" 6937 "void g()\n" 6938 "} // namespace N", 6939 format("namespace N { void f( ) { } void g( ) }")); 6940 } 6941 6942 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6943 verifyFormat("int aaaaaaaa =\n" 6944 " // Overlylongcomment\n" 6945 " b;", 6946 getLLVMStyleWithColumns(20)); 6947 verifyFormat("function(\n" 6948 " ShortArgument,\n" 6949 " LoooooooooooongArgument);\n", 6950 getLLVMStyleWithColumns(20)); 6951 } 6952 6953 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6954 verifyFormat("public:"); 6955 verifyFormat("class A {\n" 6956 "public\n" 6957 " void f() {}\n" 6958 "};"); 6959 verifyFormat("public\n" 6960 "int qwerty;"); 6961 verifyFormat("public\n" 6962 "B {}"); 6963 verifyFormat("public\n" 6964 "{}"); 6965 verifyFormat("public\n" 6966 "B { int x; }"); 6967 } 6968 6969 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6970 verifyFormat("{"); 6971 verifyFormat("#})"); 6972 verifyNoCrash("(/**/[:!] ?[)."); 6973 } 6974 6975 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 6976 // Found by oss-fuzz: 6977 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 6978 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 6979 Style.ColumnLimit = 60; 6980 verifyNoCrash( 6981 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 6982 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 6983 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 6984 Style); 6985 } 6986 6987 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6988 verifyFormat("do {\n}"); 6989 verifyFormat("do {\n}\n" 6990 "f();"); 6991 verifyFormat("do {\n}\n" 6992 "wheeee(fun);"); 6993 verifyFormat("do {\n" 6994 " f();\n" 6995 "}"); 6996 } 6997 6998 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6999 verifyFormat("if {\n foo;\n foo();\n}"); 7000 verifyFormat("switch {\n foo;\n foo();\n}"); 7001 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 7002 verifyFormat("while {\n foo;\n foo();\n}"); 7003 verifyFormat("do {\n foo;\n foo();\n} while;"); 7004 } 7005 7006 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 7007 verifyIncompleteFormat("namespace {\n" 7008 "class Foo { Foo (\n" 7009 "};\n" 7010 "} // namespace"); 7011 } 7012 7013 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 7014 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 7015 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 7016 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 7017 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 7018 7019 EXPECT_EQ("{\n" 7020 " {\n" 7021 " breakme(\n" 7022 " qwe);\n" 7023 " }\n", 7024 format("{\n" 7025 " {\n" 7026 " breakme(qwe);\n" 7027 "}\n", 7028 getLLVMStyleWithColumns(10))); 7029 } 7030 7031 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 7032 verifyFormat("int x = {\n" 7033 " avariable,\n" 7034 " b(alongervariable)};", 7035 getLLVMStyleWithColumns(25)); 7036 } 7037 7038 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 7039 verifyFormat("return (a)(b){1, 2, 3};"); 7040 } 7041 7042 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 7043 verifyFormat("vector<int> x{1, 2, 3, 4};"); 7044 verifyFormat("vector<int> x{\n" 7045 " 1,\n" 7046 " 2,\n" 7047 " 3,\n" 7048 " 4,\n" 7049 "};"); 7050 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 7051 verifyFormat("f({1, 2});"); 7052 verifyFormat("auto v = Foo{-1};"); 7053 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 7054 verifyFormat("Class::Class : member{1, 2, 3} {}"); 7055 verifyFormat("new vector<int>{1, 2, 3};"); 7056 verifyFormat("new int[3]{1, 2, 3};"); 7057 verifyFormat("new int{1};"); 7058 verifyFormat("return {arg1, arg2};"); 7059 verifyFormat("return {arg1, SomeType{parameter}};"); 7060 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 7061 verifyFormat("new T{arg1, arg2};"); 7062 verifyFormat("f(MyMap[{composite, key}]);"); 7063 verifyFormat("class Class {\n" 7064 " T member = {arg1, arg2};\n" 7065 "};"); 7066 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 7067 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 7068 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 7069 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 7070 verifyFormat("int a = std::is_integral<int>{} + 0;"); 7071 7072 verifyFormat("int foo(int i) { return fo1{}(i); }"); 7073 verifyFormat("int foo(int i) { return fo1{}(i); }"); 7074 verifyFormat("auto i = decltype(x){};"); 7075 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 7076 verifyFormat("Node n{1, Node{1000}, //\n" 7077 " 2};"); 7078 verifyFormat("Aaaa aaaaaaa{\n" 7079 " {\n" 7080 " aaaa,\n" 7081 " },\n" 7082 "};"); 7083 verifyFormat("class C : public D {\n" 7084 " SomeClass SC{2};\n" 7085 "};"); 7086 verifyFormat("class C : public A {\n" 7087 " class D : public B {\n" 7088 " void f() { int i{2}; }\n" 7089 " };\n" 7090 "};"); 7091 verifyFormat("#define A {a, a},"); 7092 7093 // Avoid breaking between equal sign and opening brace 7094 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 7095 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 7096 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 7097 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 7098 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 7099 " {\"ccccccccccccccccccccc\", 2}};", 7100 AvoidBreakingFirstArgument); 7101 7102 // Binpacking only if there is no trailing comma 7103 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 7104 " cccccccccc, dddddddddd};", 7105 getLLVMStyleWithColumns(50)); 7106 verifyFormat("const Aaaaaa aaaaa = {\n" 7107 " aaaaaaaaaaa,\n" 7108 " bbbbbbbbbbb,\n" 7109 " ccccccccccc,\n" 7110 " ddddddddddd,\n" 7111 "};", getLLVMStyleWithColumns(50)); 7112 7113 // Cases where distinguising braced lists and blocks is hard. 7114 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 7115 verifyFormat("void f() {\n" 7116 " return; // comment\n" 7117 "}\n" 7118 "SomeType t;"); 7119 verifyFormat("void f() {\n" 7120 " if (a) {\n" 7121 " f();\n" 7122 " }\n" 7123 "}\n" 7124 "SomeType t;"); 7125 7126 // In combination with BinPackArguments = false. 7127 FormatStyle NoBinPacking = getLLVMStyle(); 7128 NoBinPacking.BinPackArguments = false; 7129 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 7130 " bbbbb,\n" 7131 " ccccc,\n" 7132 " ddddd,\n" 7133 " eeeee,\n" 7134 " ffffff,\n" 7135 " ggggg,\n" 7136 " hhhhhh,\n" 7137 " iiiiii,\n" 7138 " jjjjjj,\n" 7139 " kkkkkk};", 7140 NoBinPacking); 7141 verifyFormat("const Aaaaaa aaaaa = {\n" 7142 " aaaaa,\n" 7143 " bbbbb,\n" 7144 " ccccc,\n" 7145 " ddddd,\n" 7146 " eeeee,\n" 7147 " ffffff,\n" 7148 " ggggg,\n" 7149 " hhhhhh,\n" 7150 " iiiiii,\n" 7151 " jjjjjj,\n" 7152 " kkkkkk,\n" 7153 "};", 7154 NoBinPacking); 7155 verifyFormat( 7156 "const Aaaaaa aaaaa = {\n" 7157 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 7158 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 7159 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 7160 "};", 7161 NoBinPacking); 7162 7163 // FIXME: The alignment of these trailing comments might be bad. Then again, 7164 // this might be utterly useless in real code. 7165 verifyFormat("Constructor::Constructor()\n" 7166 " : some_value{ //\n" 7167 " aaaaaaa, //\n" 7168 " bbbbbbb} {}"); 7169 7170 // In braced lists, the first comment is always assumed to belong to the 7171 // first element. Thus, it can be moved to the next or previous line as 7172 // appropriate. 7173 EXPECT_EQ("function({// First element:\n" 7174 " 1,\n" 7175 " // Second element:\n" 7176 " 2});", 7177 format("function({\n" 7178 " // First element:\n" 7179 " 1,\n" 7180 " // Second element:\n" 7181 " 2});")); 7182 EXPECT_EQ("std::vector<int> MyNumbers{\n" 7183 " // First element:\n" 7184 " 1,\n" 7185 " // Second element:\n" 7186 " 2};", 7187 format("std::vector<int> MyNumbers{// First element:\n" 7188 " 1,\n" 7189 " // Second element:\n" 7190 " 2};", 7191 getLLVMStyleWithColumns(30))); 7192 // A trailing comma should still lead to an enforced line break and no 7193 // binpacking. 7194 EXPECT_EQ("vector<int> SomeVector = {\n" 7195 " // aaa\n" 7196 " 1,\n" 7197 " 2,\n" 7198 "};", 7199 format("vector<int> SomeVector = { // aaa\n" 7200 " 1, 2, };")); 7201 7202 FormatStyle ExtraSpaces = getLLVMStyle(); 7203 ExtraSpaces.Cpp11BracedListStyle = false; 7204 ExtraSpaces.ColumnLimit = 75; 7205 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 7206 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 7207 verifyFormat("f({ 1, 2 });", ExtraSpaces); 7208 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 7209 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 7210 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 7211 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 7212 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 7213 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 7214 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 7215 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 7216 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 7217 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 7218 verifyFormat("class Class {\n" 7219 " T member = { arg1, arg2 };\n" 7220 "};", 7221 ExtraSpaces); 7222 verifyFormat( 7223 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7224 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 7225 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 7226 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 7227 ExtraSpaces); 7228 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 7229 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 7230 ExtraSpaces); 7231 verifyFormat( 7232 "someFunction(OtherParam,\n" 7233 " BracedList{ // comment 1 (Forcing interesting break)\n" 7234 " param1, param2,\n" 7235 " // comment 2\n" 7236 " param3, param4 });", 7237 ExtraSpaces); 7238 verifyFormat( 7239 "std::this_thread::sleep_for(\n" 7240 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 7241 ExtraSpaces); 7242 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 7243 " aaaaaaa,\n" 7244 " aaaaaaaaaa,\n" 7245 " aaaaa,\n" 7246 " aaaaaaaaaaaaaaa,\n" 7247 " aaa,\n" 7248 " aaaaaaaaaa,\n" 7249 " a,\n" 7250 " aaaaaaaaaaaaaaaaaaaaa,\n" 7251 " aaaaaaaaaaaa,\n" 7252 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 7253 " aaaaaaa,\n" 7254 " a};"); 7255 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 7256 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 7257 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 7258 7259 // Avoid breaking between initializer/equal sign and opening brace 7260 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 7261 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 7262 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 7263 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 7264 " { \"ccccccccccccccccccccc\", 2 }\n" 7265 "};", 7266 ExtraSpaces); 7267 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 7268 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 7269 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 7270 " { \"ccccccccccccccccccccc\", 2 }\n" 7271 "};", 7272 ExtraSpaces); 7273 7274 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 7275 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 7276 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 7277 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 7278 } 7279 7280 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 7281 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7282 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7283 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7284 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7285 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7286 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 7287 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 7288 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7289 " 1, 22, 333, 4444, 55555, //\n" 7290 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7291 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 7292 verifyFormat( 7293 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7294 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7295 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 7296 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7297 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7298 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7299 " 7777777};"); 7300 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7301 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7302 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7303 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7304 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7305 " // Separating comment.\n" 7306 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7307 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7308 " // Leading comment\n" 7309 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7310 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7311 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7312 " 1, 1, 1, 1};", 7313 getLLVMStyleWithColumns(39)); 7314 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7315 " 1, 1, 1, 1};", 7316 getLLVMStyleWithColumns(38)); 7317 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 7318 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 7319 getLLVMStyleWithColumns(43)); 7320 verifyFormat( 7321 "static unsigned SomeValues[10][3] = {\n" 7322 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 7323 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 7324 verifyFormat("static auto fields = new vector<string>{\n" 7325 " \"aaaaaaaaaaaaa\",\n" 7326 " \"aaaaaaaaaaaaa\",\n" 7327 " \"aaaaaaaaaaaa\",\n" 7328 " \"aaaaaaaaaaaaaa\",\n" 7329 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 7330 " \"aaaaaaaaaaaa\",\n" 7331 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 7332 "};"); 7333 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 7334 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 7335 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 7336 " 3, cccccccccccccccccccccc};", 7337 getLLVMStyleWithColumns(60)); 7338 7339 // Trailing commas. 7340 verifyFormat("vector<int> x = {\n" 7341 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 7342 "};", 7343 getLLVMStyleWithColumns(39)); 7344 verifyFormat("vector<int> x = {\n" 7345 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 7346 "};", 7347 getLLVMStyleWithColumns(39)); 7348 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7349 " 1, 1, 1, 1,\n" 7350 " /**/ /**/};", 7351 getLLVMStyleWithColumns(39)); 7352 7353 // Trailing comment in the first line. 7354 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 7355 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 7356 " 111111111, 222222222, 3333333333, 444444444, //\n" 7357 " 11111111, 22222222, 333333333, 44444444};"); 7358 // Trailing comment in the last line. 7359 verifyFormat("int aaaaa[] = {\n" 7360 " 1, 2, 3, // comment\n" 7361 " 4, 5, 6 // comment\n" 7362 "};"); 7363 7364 // With nested lists, we should either format one item per line or all nested 7365 // lists one on line. 7366 // FIXME: For some nested lists, we can do better. 7367 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 7368 " {aaaaaaaaaaaaaaaaaaa},\n" 7369 " {aaaaaaaaaaaaaaaaaaaaa},\n" 7370 " {aaaaaaaaaaaaaaaaa}};", 7371 getLLVMStyleWithColumns(60)); 7372 verifyFormat( 7373 "SomeStruct my_struct_array = {\n" 7374 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 7375 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 7376 " {aaa, aaa},\n" 7377 " {aaa, aaa},\n" 7378 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 7379 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7380 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 7381 7382 // No column layout should be used here. 7383 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 7384 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 7385 7386 verifyNoCrash("a<,"); 7387 7388 // No braced initializer here. 7389 verifyFormat("void f() {\n" 7390 " struct Dummy {};\n" 7391 " f(v);\n" 7392 "}"); 7393 7394 // Long lists should be formatted in columns even if they are nested. 7395 verifyFormat( 7396 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7397 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7398 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7399 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7400 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7401 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 7402 7403 // Allow "single-column" layout even if that violates the column limit. There 7404 // isn't going to be a better way. 7405 verifyFormat("std::vector<int> a = {\n" 7406 " aaaaaaaa,\n" 7407 " aaaaaaaa,\n" 7408 " aaaaaaaa,\n" 7409 " aaaaaaaa,\n" 7410 " aaaaaaaaaa,\n" 7411 " aaaaaaaa,\n" 7412 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 7413 getLLVMStyleWithColumns(30)); 7414 verifyFormat("vector<int> aaaa = {\n" 7415 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7416 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7417 " aaaaaa.aaaaaaa,\n" 7418 " aaaaaa.aaaaaaa,\n" 7419 " aaaaaa.aaaaaaa,\n" 7420 " aaaaaa.aaaaaaa,\n" 7421 "};"); 7422 7423 // Don't create hanging lists. 7424 verifyFormat("someFunction(Param, {List1, List2,\n" 7425 " List3});", 7426 getLLVMStyleWithColumns(35)); 7427 verifyFormat("someFunction(Param, Param,\n" 7428 " {List1, List2,\n" 7429 " List3});", 7430 getLLVMStyleWithColumns(35)); 7431 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 7432 " aaaaaaaaaaaaaaaaaaaaaaa);"); 7433 } 7434 7435 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 7436 FormatStyle DoNotMerge = getLLVMStyle(); 7437 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7438 7439 verifyFormat("void f() { return 42; }"); 7440 verifyFormat("void f() {\n" 7441 " return 42;\n" 7442 "}", 7443 DoNotMerge); 7444 verifyFormat("void f() {\n" 7445 " // Comment\n" 7446 "}"); 7447 verifyFormat("{\n" 7448 "#error {\n" 7449 " int a;\n" 7450 "}"); 7451 verifyFormat("{\n" 7452 " int a;\n" 7453 "#error {\n" 7454 "}"); 7455 verifyFormat("void f() {} // comment"); 7456 verifyFormat("void f() { int a; } // comment"); 7457 verifyFormat("void f() {\n" 7458 "} // comment", 7459 DoNotMerge); 7460 verifyFormat("void f() {\n" 7461 " int a;\n" 7462 "} // comment", 7463 DoNotMerge); 7464 verifyFormat("void f() {\n" 7465 "} // comment", 7466 getLLVMStyleWithColumns(15)); 7467 7468 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 7469 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 7470 7471 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 7472 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 7473 verifyFormat("class C {\n" 7474 " C()\n" 7475 " : iiiiiiii(nullptr),\n" 7476 " kkkkkkk(nullptr),\n" 7477 " mmmmmmm(nullptr),\n" 7478 " nnnnnnn(nullptr) {}\n" 7479 "};", 7480 getGoogleStyle()); 7481 7482 FormatStyle NoColumnLimit = getLLVMStyle(); 7483 NoColumnLimit.ColumnLimit = 0; 7484 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 7485 EXPECT_EQ("class C {\n" 7486 " A() : b(0) {}\n" 7487 "};", 7488 format("class C{A():b(0){}};", NoColumnLimit)); 7489 EXPECT_EQ("A()\n" 7490 " : b(0) {\n" 7491 "}", 7492 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 7493 7494 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 7495 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 7496 FormatStyle::SFS_None; 7497 EXPECT_EQ("A()\n" 7498 " : b(0) {\n" 7499 "}", 7500 format("A():b(0){}", DoNotMergeNoColumnLimit)); 7501 EXPECT_EQ("A()\n" 7502 " : b(0) {\n" 7503 "}", 7504 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 7505 7506 verifyFormat("#define A \\\n" 7507 " void f() { \\\n" 7508 " int i; \\\n" 7509 " }", 7510 getLLVMStyleWithColumns(20)); 7511 verifyFormat("#define A \\\n" 7512 " void f() { int i; }", 7513 getLLVMStyleWithColumns(21)); 7514 verifyFormat("#define A \\\n" 7515 " void f() { \\\n" 7516 " int i; \\\n" 7517 " } \\\n" 7518 " int j;", 7519 getLLVMStyleWithColumns(22)); 7520 verifyFormat("#define A \\\n" 7521 " void f() { int i; } \\\n" 7522 " int j;", 7523 getLLVMStyleWithColumns(23)); 7524 } 7525 7526 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 7527 FormatStyle MergeEmptyOnly = getLLVMStyle(); 7528 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7529 verifyFormat("class C {\n" 7530 " int f() {}\n" 7531 "};", 7532 MergeEmptyOnly); 7533 verifyFormat("class C {\n" 7534 " int f() {\n" 7535 " return 42;\n" 7536 " }\n" 7537 "};", 7538 MergeEmptyOnly); 7539 verifyFormat("int f() {}", MergeEmptyOnly); 7540 verifyFormat("int f() {\n" 7541 " return 42;\n" 7542 "}", 7543 MergeEmptyOnly); 7544 7545 // Also verify behavior when BraceWrapping.AfterFunction = true 7546 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7547 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 7548 verifyFormat("int f() {}", MergeEmptyOnly); 7549 verifyFormat("class C {\n" 7550 " int f() {}\n" 7551 "};", 7552 MergeEmptyOnly); 7553 } 7554 7555 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 7556 FormatStyle MergeInlineOnly = getLLVMStyle(); 7557 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7558 verifyFormat("class C {\n" 7559 " int f() { return 42; }\n" 7560 "};", 7561 MergeInlineOnly); 7562 verifyFormat("int f() {\n" 7563 " return 42;\n" 7564 "}", 7565 MergeInlineOnly); 7566 7567 // SFS_Inline implies SFS_Empty 7568 verifyFormat("class C {\n" 7569 " int f() {}\n" 7570 "};", 7571 MergeInlineOnly); 7572 verifyFormat("int f() {}", MergeInlineOnly); 7573 7574 // Also verify behavior when BraceWrapping.AfterFunction = true 7575 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7576 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7577 verifyFormat("class C {\n" 7578 " int f() { return 42; }\n" 7579 "};", 7580 MergeInlineOnly); 7581 verifyFormat("int f()\n" 7582 "{\n" 7583 " return 42;\n" 7584 "}", 7585 MergeInlineOnly); 7586 7587 // SFS_Inline implies SFS_Empty 7588 verifyFormat("int f() {}", MergeInlineOnly); 7589 verifyFormat("class C {\n" 7590 " int f() {}\n" 7591 "};", 7592 MergeInlineOnly); 7593 } 7594 7595 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 7596 FormatStyle MergeInlineOnly = getLLVMStyle(); 7597 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 7598 FormatStyle::SFS_InlineOnly; 7599 verifyFormat("class C {\n" 7600 " int f() { return 42; }\n" 7601 "};", 7602 MergeInlineOnly); 7603 verifyFormat("int f() {\n" 7604 " return 42;\n" 7605 "}", 7606 MergeInlineOnly); 7607 7608 // SFS_InlineOnly does not imply SFS_Empty 7609 verifyFormat("class C {\n" 7610 " int f() {}\n" 7611 "};", 7612 MergeInlineOnly); 7613 verifyFormat("int f() {\n" 7614 "}", 7615 MergeInlineOnly); 7616 7617 // Also verify behavior when BraceWrapping.AfterFunction = true 7618 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7619 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7620 verifyFormat("class C {\n" 7621 " int f() { return 42; }\n" 7622 "};", 7623 MergeInlineOnly); 7624 verifyFormat("int f()\n" 7625 "{\n" 7626 " return 42;\n" 7627 "}", 7628 MergeInlineOnly); 7629 7630 // SFS_InlineOnly does not imply SFS_Empty 7631 verifyFormat("int f()\n" 7632 "{\n" 7633 "}", 7634 MergeInlineOnly); 7635 verifyFormat("class C {\n" 7636 " int f() {}\n" 7637 "};", 7638 MergeInlineOnly); 7639 } 7640 7641 TEST_F(FormatTest, SplitEmptyFunction) { 7642 FormatStyle Style = getLLVMStyle(); 7643 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7644 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7645 Style.BraceWrapping.AfterFunction = true; 7646 Style.BraceWrapping.SplitEmptyFunction = false; 7647 Style.ColumnLimit = 40; 7648 7649 verifyFormat("int f()\n" 7650 "{}", 7651 Style); 7652 verifyFormat("int f()\n" 7653 "{\n" 7654 " return 42;\n" 7655 "}", 7656 Style); 7657 verifyFormat("int f()\n" 7658 "{\n" 7659 " // some comment\n" 7660 "}", 7661 Style); 7662 7663 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7664 verifyFormat("int f() {}", Style); 7665 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7666 "{}", 7667 Style); 7668 verifyFormat("int f()\n" 7669 "{\n" 7670 " return 0;\n" 7671 "}", 7672 Style); 7673 7674 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7675 verifyFormat("class Foo {\n" 7676 " int f() {}\n" 7677 "};\n", 7678 Style); 7679 verifyFormat("class Foo {\n" 7680 " int f() { return 0; }\n" 7681 "};\n", 7682 Style); 7683 verifyFormat("class Foo {\n" 7684 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7685 " {}\n" 7686 "};\n", 7687 Style); 7688 verifyFormat("class Foo {\n" 7689 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7690 " {\n" 7691 " return 0;\n" 7692 " }\n" 7693 "};\n", 7694 Style); 7695 7696 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7697 verifyFormat("int f() {}", Style); 7698 verifyFormat("int f() { return 0; }", Style); 7699 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7700 "{}", 7701 Style); 7702 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7703 "{\n" 7704 " return 0;\n" 7705 "}", 7706 Style); 7707 } 7708 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 7709 FormatStyle Style = getLLVMStyle(); 7710 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7711 verifyFormat("#ifdef A\n" 7712 "int f() {}\n" 7713 "#else\n" 7714 "int g() {}\n" 7715 "#endif", 7716 Style); 7717 } 7718 7719 TEST_F(FormatTest, SplitEmptyClass) { 7720 FormatStyle Style = getLLVMStyle(); 7721 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7722 Style.BraceWrapping.AfterClass = true; 7723 Style.BraceWrapping.SplitEmptyRecord = false; 7724 7725 verifyFormat("class Foo\n" 7726 "{};", 7727 Style); 7728 verifyFormat("/* something */ class Foo\n" 7729 "{};", 7730 Style); 7731 verifyFormat("template <typename X> class Foo\n" 7732 "{};", 7733 Style); 7734 verifyFormat("class Foo\n" 7735 "{\n" 7736 " Foo();\n" 7737 "};", 7738 Style); 7739 verifyFormat("typedef class Foo\n" 7740 "{\n" 7741 "} Foo_t;", 7742 Style); 7743 } 7744 7745 TEST_F(FormatTest, SplitEmptyStruct) { 7746 FormatStyle Style = getLLVMStyle(); 7747 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7748 Style.BraceWrapping.AfterStruct = true; 7749 Style.BraceWrapping.SplitEmptyRecord = false; 7750 7751 verifyFormat("struct Foo\n" 7752 "{};", 7753 Style); 7754 verifyFormat("/* something */ struct Foo\n" 7755 "{};", 7756 Style); 7757 verifyFormat("template <typename X> struct Foo\n" 7758 "{};", 7759 Style); 7760 verifyFormat("struct Foo\n" 7761 "{\n" 7762 " Foo();\n" 7763 "};", 7764 Style); 7765 verifyFormat("typedef struct Foo\n" 7766 "{\n" 7767 "} Foo_t;", 7768 Style); 7769 //typedef struct Bar {} Bar_t; 7770 } 7771 7772 TEST_F(FormatTest, SplitEmptyUnion) { 7773 FormatStyle Style = getLLVMStyle(); 7774 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7775 Style.BraceWrapping.AfterUnion = true; 7776 Style.BraceWrapping.SplitEmptyRecord = false; 7777 7778 verifyFormat("union Foo\n" 7779 "{};", 7780 Style); 7781 verifyFormat("/* something */ union Foo\n" 7782 "{};", 7783 Style); 7784 verifyFormat("union Foo\n" 7785 "{\n" 7786 " A,\n" 7787 "};", 7788 Style); 7789 verifyFormat("typedef union Foo\n" 7790 "{\n" 7791 "} Foo_t;", 7792 Style); 7793 } 7794 7795 TEST_F(FormatTest, SplitEmptyNamespace) { 7796 FormatStyle Style = getLLVMStyle(); 7797 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7798 Style.BraceWrapping.AfterNamespace = true; 7799 Style.BraceWrapping.SplitEmptyNamespace = false; 7800 7801 verifyFormat("namespace Foo\n" 7802 "{};", 7803 Style); 7804 verifyFormat("/* something */ namespace Foo\n" 7805 "{};", 7806 Style); 7807 verifyFormat("inline namespace Foo\n" 7808 "{};", 7809 Style); 7810 verifyFormat("/* something */ inline namespace Foo\n" 7811 "{};", 7812 Style); 7813 verifyFormat("export namespace Foo\n" 7814 "{};", 7815 Style); 7816 verifyFormat("namespace Foo\n" 7817 "{\n" 7818 "void Bar();\n" 7819 "};", 7820 Style); 7821 } 7822 7823 TEST_F(FormatTest, NeverMergeShortRecords) { 7824 FormatStyle Style = getLLVMStyle(); 7825 7826 verifyFormat("class Foo {\n" 7827 " Foo();\n" 7828 "};", 7829 Style); 7830 verifyFormat("typedef class Foo {\n" 7831 " Foo();\n" 7832 "} Foo_t;", 7833 Style); 7834 verifyFormat("struct Foo {\n" 7835 " Foo();\n" 7836 "};", 7837 Style); 7838 verifyFormat("typedef struct Foo {\n" 7839 " Foo();\n" 7840 "} Foo_t;", 7841 Style); 7842 verifyFormat("union Foo {\n" 7843 " A,\n" 7844 "};", 7845 Style); 7846 verifyFormat("typedef union Foo {\n" 7847 " A,\n" 7848 "} Foo_t;", 7849 Style); 7850 verifyFormat("namespace Foo {\n" 7851 "void Bar();\n" 7852 "};", 7853 Style); 7854 7855 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7856 Style.BraceWrapping.AfterClass = true; 7857 Style.BraceWrapping.AfterStruct = true; 7858 Style.BraceWrapping.AfterUnion = true; 7859 Style.BraceWrapping.AfterNamespace = true; 7860 verifyFormat("class Foo\n" 7861 "{\n" 7862 " Foo();\n" 7863 "};", 7864 Style); 7865 verifyFormat("typedef class Foo\n" 7866 "{\n" 7867 " Foo();\n" 7868 "} Foo_t;", 7869 Style); 7870 verifyFormat("struct Foo\n" 7871 "{\n" 7872 " Foo();\n" 7873 "};", 7874 Style); 7875 verifyFormat("typedef struct Foo\n" 7876 "{\n" 7877 " Foo();\n" 7878 "} Foo_t;", 7879 Style); 7880 verifyFormat("union Foo\n" 7881 "{\n" 7882 " A,\n" 7883 "};", 7884 Style); 7885 verifyFormat("typedef union Foo\n" 7886 "{\n" 7887 " A,\n" 7888 "} Foo_t;", 7889 Style); 7890 verifyFormat("namespace Foo\n" 7891 "{\n" 7892 "void Bar();\n" 7893 "};", 7894 Style); 7895 } 7896 7897 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 7898 // Elaborate type variable declarations. 7899 verifyFormat("struct foo a = {bar};\nint n;"); 7900 verifyFormat("class foo a = {bar};\nint n;"); 7901 verifyFormat("union foo a = {bar};\nint n;"); 7902 7903 // Elaborate types inside function definitions. 7904 verifyFormat("struct foo f() {}\nint n;"); 7905 verifyFormat("class foo f() {}\nint n;"); 7906 verifyFormat("union foo f() {}\nint n;"); 7907 7908 // Templates. 7909 verifyFormat("template <class X> void f() {}\nint n;"); 7910 verifyFormat("template <struct X> void f() {}\nint n;"); 7911 verifyFormat("template <union X> void f() {}\nint n;"); 7912 7913 // Actual definitions... 7914 verifyFormat("struct {\n} n;"); 7915 verifyFormat( 7916 "template <template <class T, class Y>, class Z> class X {\n} n;"); 7917 verifyFormat("union Z {\n int n;\n} x;"); 7918 verifyFormat("class MACRO Z {\n} n;"); 7919 verifyFormat("class MACRO(X) Z {\n} n;"); 7920 verifyFormat("class __attribute__(X) Z {\n} n;"); 7921 verifyFormat("class __declspec(X) Z {\n} n;"); 7922 verifyFormat("class A##B##C {\n} n;"); 7923 verifyFormat("class alignas(16) Z {\n} n;"); 7924 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 7925 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 7926 7927 // Redefinition from nested context: 7928 verifyFormat("class A::B::C {\n} n;"); 7929 7930 // Template definitions. 7931 verifyFormat( 7932 "template <typename F>\n" 7933 "Matcher(const Matcher<F> &Other,\n" 7934 " typename enable_if_c<is_base_of<F, T>::value &&\n" 7935 " !is_same<F, T>::value>::type * = 0)\n" 7936 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 7937 7938 // FIXME: This is still incorrectly handled at the formatter side. 7939 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 7940 verifyFormat("int i = SomeFunction(a<b, a> b);"); 7941 7942 // FIXME: 7943 // This now gets parsed incorrectly as class definition. 7944 // verifyFormat("class A<int> f() {\n}\nint n;"); 7945 7946 // Elaborate types where incorrectly parsing the structural element would 7947 // break the indent. 7948 verifyFormat("if (true)\n" 7949 " class X x;\n" 7950 "else\n" 7951 " f();\n"); 7952 7953 // This is simply incomplete. Formatting is not important, but must not crash. 7954 verifyFormat("class A:"); 7955 } 7956 7957 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 7958 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 7959 format("#error Leave all white!!!!! space* alone!\n")); 7960 EXPECT_EQ( 7961 "#warning Leave all white!!!!! space* alone!\n", 7962 format("#warning Leave all white!!!!! space* alone!\n")); 7963 EXPECT_EQ("#error 1", format(" # error 1")); 7964 EXPECT_EQ("#warning 1", format(" # warning 1")); 7965 } 7966 7967 TEST_F(FormatTest, FormatHashIfExpressions) { 7968 verifyFormat("#if AAAA && BBBB"); 7969 verifyFormat("#if (AAAA && BBBB)"); 7970 verifyFormat("#elif (AAAA && BBBB)"); 7971 // FIXME: Come up with a better indentation for #elif. 7972 verifyFormat( 7973 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 7974 " defined(BBBBBBBB)\n" 7975 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 7976 " defined(BBBBBBBB)\n" 7977 "#endif", 7978 getLLVMStyleWithColumns(65)); 7979 } 7980 7981 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 7982 FormatStyle AllowsMergedIf = getGoogleStyle(); 7983 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 7984 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 7985 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 7986 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 7987 EXPECT_EQ("if (true) return 42;", 7988 format("if (true)\nreturn 42;", AllowsMergedIf)); 7989 FormatStyle ShortMergedIf = AllowsMergedIf; 7990 ShortMergedIf.ColumnLimit = 25; 7991 verifyFormat("#define A \\\n" 7992 " if (true) return 42;", 7993 ShortMergedIf); 7994 verifyFormat("#define A \\\n" 7995 " f(); \\\n" 7996 " if (true)\n" 7997 "#define B", 7998 ShortMergedIf); 7999 verifyFormat("#define A \\\n" 8000 " f(); \\\n" 8001 " if (true)\n" 8002 "g();", 8003 ShortMergedIf); 8004 verifyFormat("{\n" 8005 "#ifdef A\n" 8006 " // Comment\n" 8007 " if (true) continue;\n" 8008 "#endif\n" 8009 " // Comment\n" 8010 " if (true) continue;\n" 8011 "}", 8012 ShortMergedIf); 8013 ShortMergedIf.ColumnLimit = 33; 8014 verifyFormat("#define A \\\n" 8015 " if constexpr (true) return 42;", 8016 ShortMergedIf); 8017 ShortMergedIf.ColumnLimit = 29; 8018 verifyFormat("#define A \\\n" 8019 " if (aaaaaaaaaa) return 1; \\\n" 8020 " return 2;", 8021 ShortMergedIf); 8022 ShortMergedIf.ColumnLimit = 28; 8023 verifyFormat("#define A \\\n" 8024 " if (aaaaaaaaaa) \\\n" 8025 " return 1; \\\n" 8026 " return 2;", 8027 ShortMergedIf); 8028 verifyFormat("#define A \\\n" 8029 " if constexpr (aaaaaaa) \\\n" 8030 " return 1; \\\n" 8031 " return 2;", 8032 ShortMergedIf); 8033 } 8034 8035 TEST_F(FormatTest, FormatStarDependingOnContext) { 8036 verifyFormat("void f(int *a);"); 8037 verifyFormat("void f() { f(fint * b); }"); 8038 verifyFormat("class A {\n void f(int *a);\n};"); 8039 verifyFormat("class A {\n int *a;\n};"); 8040 verifyFormat("namespace a {\n" 8041 "namespace b {\n" 8042 "class A {\n" 8043 " void f() {}\n" 8044 " int *a;\n" 8045 "};\n" 8046 "} // namespace b\n" 8047 "} // namespace a"); 8048 } 8049 8050 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 8051 verifyFormat("while"); 8052 verifyFormat("operator"); 8053 } 8054 8055 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 8056 // This code would be painfully slow to format if we didn't skip it. 8057 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 8058 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8059 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8060 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8061 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8062 "A(1, 1)\n" 8063 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 8064 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8065 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8066 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8067 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8068 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8069 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8070 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8071 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8072 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 8073 // Deeply nested part is untouched, rest is formatted. 8074 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 8075 format(std::string("int i;\n") + Code + "int j;\n", 8076 getLLVMStyle(), SC_ExpectIncomplete)); 8077 } 8078 8079 //===----------------------------------------------------------------------===// 8080 // Objective-C tests. 8081 //===----------------------------------------------------------------------===// 8082 8083 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 8084 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 8085 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 8086 format("-(NSUInteger)indexOfObject:(id)anObject;")); 8087 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 8088 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 8089 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 8090 format("-(NSInteger)Method3:(id)anObject;")); 8091 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 8092 format("-(NSInteger)Method4:(id)anObject;")); 8093 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 8094 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 8095 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 8096 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 8097 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 8098 "forAllCells:(BOOL)flag;", 8099 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 8100 "forAllCells:(BOOL)flag;")); 8101 8102 // Very long objectiveC method declaration. 8103 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 8104 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 8105 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 8106 " inRange:(NSRange)range\n" 8107 " outRange:(NSRange)out_range\n" 8108 " outRange1:(NSRange)out_range1\n" 8109 " outRange2:(NSRange)out_range2\n" 8110 " outRange3:(NSRange)out_range3\n" 8111 " outRange4:(NSRange)out_range4\n" 8112 " outRange5:(NSRange)out_range5\n" 8113 " outRange6:(NSRange)out_range6\n" 8114 " outRange7:(NSRange)out_range7\n" 8115 " outRange8:(NSRange)out_range8\n" 8116 " outRange9:(NSRange)out_range9;"); 8117 8118 // When the function name has to be wrapped. 8119 FormatStyle Style = getLLVMStyle(); 8120 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 8121 // and always indents instead. 8122 Style.IndentWrappedFunctionNames = false; 8123 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 8124 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 8125 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 8126 "}", 8127 Style); 8128 Style.IndentWrappedFunctionNames = true; 8129 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 8130 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 8131 " anotherName:(NSString)dddddddddddddd {\n" 8132 "}", 8133 Style); 8134 8135 verifyFormat("- (int)sum:(vector<int>)numbers;"); 8136 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 8137 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 8138 // protocol lists (but not for template classes): 8139 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 8140 8141 verifyFormat("- (int (*)())foo:(int (*)())f;"); 8142 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 8143 8144 // If there's no return type (very rare in practice!), LLVM and Google style 8145 // agree. 8146 verifyFormat("- foo;"); 8147 verifyFormat("- foo:(int)f;"); 8148 verifyGoogleFormat("- foo:(int)foo;"); 8149 } 8150 8151 8152 TEST_F(FormatTest, BreaksStringLiterals) { 8153 EXPECT_EQ("\"some text \"\n" 8154 "\"other\";", 8155 format("\"some text other\";", getLLVMStyleWithColumns(12))); 8156 EXPECT_EQ("\"some text \"\n" 8157 "\"other\";", 8158 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 8159 EXPECT_EQ( 8160 "#define A \\\n" 8161 " \"some \" \\\n" 8162 " \"text \" \\\n" 8163 " \"other\";", 8164 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 8165 EXPECT_EQ( 8166 "#define A \\\n" 8167 " \"so \" \\\n" 8168 " \"text \" \\\n" 8169 " \"other\";", 8170 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 8171 8172 EXPECT_EQ("\"some text\"", 8173 format("\"some text\"", getLLVMStyleWithColumns(1))); 8174 EXPECT_EQ("\"some text\"", 8175 format("\"some text\"", getLLVMStyleWithColumns(11))); 8176 EXPECT_EQ("\"some \"\n" 8177 "\"text\"", 8178 format("\"some text\"", getLLVMStyleWithColumns(10))); 8179 EXPECT_EQ("\"some \"\n" 8180 "\"text\"", 8181 format("\"some text\"", getLLVMStyleWithColumns(7))); 8182 EXPECT_EQ("\"some\"\n" 8183 "\" tex\"\n" 8184 "\"t\"", 8185 format("\"some text\"", getLLVMStyleWithColumns(6))); 8186 EXPECT_EQ("\"some\"\n" 8187 "\" tex\"\n" 8188 "\" and\"", 8189 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 8190 EXPECT_EQ("\"some\"\n" 8191 "\"/tex\"\n" 8192 "\"/and\"", 8193 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 8194 8195 EXPECT_EQ("variable =\n" 8196 " \"long string \"\n" 8197 " \"literal\";", 8198 format("variable = \"long string literal\";", 8199 getLLVMStyleWithColumns(20))); 8200 8201 EXPECT_EQ("variable = f(\n" 8202 " \"long string \"\n" 8203 " \"literal\",\n" 8204 " short,\n" 8205 " loooooooooooooooooooong);", 8206 format("variable = f(\"long string literal\", short, " 8207 "loooooooooooooooooooong);", 8208 getLLVMStyleWithColumns(20))); 8209 8210 EXPECT_EQ( 8211 "f(g(\"long string \"\n" 8212 " \"literal\"),\n" 8213 " b);", 8214 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 8215 EXPECT_EQ("f(g(\"long string \"\n" 8216 " \"literal\",\n" 8217 " a),\n" 8218 " b);", 8219 format("f(g(\"long string literal\", a), b);", 8220 getLLVMStyleWithColumns(20))); 8221 EXPECT_EQ( 8222 "f(\"one two\".split(\n" 8223 " variable));", 8224 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 8225 EXPECT_EQ("f(\"one two three four five six \"\n" 8226 " \"seven\".split(\n" 8227 " really_looooong_variable));", 8228 format("f(\"one two three four five six seven\"." 8229 "split(really_looooong_variable));", 8230 getLLVMStyleWithColumns(33))); 8231 8232 EXPECT_EQ("f(\"some \"\n" 8233 " \"text\",\n" 8234 " other);", 8235 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 8236 8237 // Only break as a last resort. 8238 verifyFormat( 8239 "aaaaaaaaaaaaaaaaaaaa(\n" 8240 " aaaaaaaaaaaaaaaaaaaa,\n" 8241 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 8242 8243 EXPECT_EQ("\"splitmea\"\n" 8244 "\"trandomp\"\n" 8245 "\"oint\"", 8246 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 8247 8248 EXPECT_EQ("\"split/\"\n" 8249 "\"pathat/\"\n" 8250 "\"slashes\"", 8251 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8252 8253 EXPECT_EQ("\"split/\"\n" 8254 "\"pathat/\"\n" 8255 "\"slashes\"", 8256 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8257 EXPECT_EQ("\"split at \"\n" 8258 "\"spaces/at/\"\n" 8259 "\"slashes.at.any$\"\n" 8260 "\"non-alphanumeric%\"\n" 8261 "\"1111111111characte\"\n" 8262 "\"rs\"", 8263 format("\"split at " 8264 "spaces/at/" 8265 "slashes.at." 8266 "any$non-" 8267 "alphanumeric%" 8268 "1111111111characte" 8269 "rs\"", 8270 getLLVMStyleWithColumns(20))); 8271 8272 // Verify that splitting the strings understands 8273 // Style::AlwaysBreakBeforeMultilineStrings. 8274 EXPECT_EQ( 8275 "aaaaaaaaaaaa(\n" 8276 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 8277 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 8278 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 8279 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8280 "aaaaaaaaaaaaaaaaaaaaaa\");", 8281 getGoogleStyle())); 8282 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8283 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 8284 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 8285 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8286 "aaaaaaaaaaaaaaaaaaaaaa\";", 8287 getGoogleStyle())); 8288 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8289 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8290 format("llvm::outs() << " 8291 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 8292 "aaaaaaaaaaaaaaaaaaa\";")); 8293 EXPECT_EQ("ffff(\n" 8294 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8295 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8296 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8297 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8298 getGoogleStyle())); 8299 8300 FormatStyle Style = getLLVMStyleWithColumns(12); 8301 Style.BreakStringLiterals = false; 8302 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 8303 8304 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 8305 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8306 EXPECT_EQ("#define A \\\n" 8307 " \"some \" \\\n" 8308 " \"text \" \\\n" 8309 " \"other\";", 8310 format("#define A \"some text other\";", AlignLeft)); 8311 } 8312 8313 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 8314 EXPECT_EQ("C a = \"some more \"\n" 8315 " \"text\";", 8316 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 8317 } 8318 8319 TEST_F(FormatTest, FullyRemoveEmptyLines) { 8320 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 8321 NoEmptyLines.MaxEmptyLinesToKeep = 0; 8322 EXPECT_EQ("int i = a(b());", 8323 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 8324 } 8325 8326 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 8327 EXPECT_EQ( 8328 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8329 "(\n" 8330 " \"x\t\");", 8331 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8332 "aaaaaaa(" 8333 "\"x\t\");")); 8334 } 8335 8336 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 8337 EXPECT_EQ( 8338 "u8\"utf8 string \"\n" 8339 "u8\"literal\";", 8340 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 8341 EXPECT_EQ( 8342 "u\"utf16 string \"\n" 8343 "u\"literal\";", 8344 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 8345 EXPECT_EQ( 8346 "U\"utf32 string \"\n" 8347 "U\"literal\";", 8348 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 8349 EXPECT_EQ("L\"wide string \"\n" 8350 "L\"literal\";", 8351 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 8352 EXPECT_EQ("@\"NSString \"\n" 8353 "@\"literal\";", 8354 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 8355 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 8356 8357 // This input makes clang-format try to split the incomplete unicode escape 8358 // sequence, which used to lead to a crasher. 8359 verifyNoCrash( 8360 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 8361 getLLVMStyleWithColumns(60)); 8362 } 8363 8364 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 8365 FormatStyle Style = getGoogleStyleWithColumns(15); 8366 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 8367 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 8368 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 8369 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 8370 EXPECT_EQ("u8R\"x(raw literal)x\";", 8371 format("u8R\"x(raw literal)x\";", Style)); 8372 } 8373 8374 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 8375 FormatStyle Style = getLLVMStyleWithColumns(20); 8376 EXPECT_EQ( 8377 "_T(\"aaaaaaaaaaaaaa\")\n" 8378 "_T(\"aaaaaaaaaaaaaa\")\n" 8379 "_T(\"aaaaaaaaaaaa\")", 8380 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 8381 EXPECT_EQ("f(x,\n" 8382 " _T(\"aaaaaaaaaaaa\")\n" 8383 " _T(\"aaa\"),\n" 8384 " z);", 8385 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 8386 8387 // FIXME: Handle embedded spaces in one iteration. 8388 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 8389 // "_T(\"aaaaaaaaaaaaa\")\n" 8390 // "_T(\"aaaaaaaaaaaaa\")\n" 8391 // "_T(\"a\")", 8392 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8393 // getLLVMStyleWithColumns(20))); 8394 EXPECT_EQ( 8395 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8396 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 8397 EXPECT_EQ("f(\n" 8398 "#if !TEST\n" 8399 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8400 "#endif\n" 8401 ");", 8402 format("f(\n" 8403 "#if !TEST\n" 8404 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8405 "#endif\n" 8406 ");")); 8407 EXPECT_EQ("f(\n" 8408 "\n" 8409 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 8410 format("f(\n" 8411 "\n" 8412 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 8413 } 8414 8415 TEST_F(FormatTest, BreaksStringLiteralOperands) { 8416 // In a function call with two operands, the second can be broken with no line 8417 // break before it. 8418 EXPECT_EQ("func(a, \"long long \"\n" 8419 " \"long long\");", 8420 format("func(a, \"long long long long\");", 8421 getLLVMStyleWithColumns(24))); 8422 // In a function call with three operands, the second must be broken with a 8423 // line break before it. 8424 EXPECT_EQ("func(a,\n" 8425 " \"long long long \"\n" 8426 " \"long\",\n" 8427 " c);", 8428 format("func(a, \"long long long long\", c);", 8429 getLLVMStyleWithColumns(24))); 8430 // In a function call with three operands, the third must be broken with a 8431 // line break before it. 8432 EXPECT_EQ("func(a, b,\n" 8433 " \"long long long \"\n" 8434 " \"long\");", 8435 format("func(a, b, \"long long long long\");", 8436 getLLVMStyleWithColumns(24))); 8437 // In a function call with three operands, both the second and the third must 8438 // be broken with a line break before them. 8439 EXPECT_EQ("func(a,\n" 8440 " \"long long long \"\n" 8441 " \"long\",\n" 8442 " \"long long long \"\n" 8443 " \"long\");", 8444 format("func(a, \"long long long long\", \"long long long long\");", 8445 getLLVMStyleWithColumns(24))); 8446 // In a chain of << with two operands, the second can be broken with no line 8447 // break before it. 8448 EXPECT_EQ("a << \"line line \"\n" 8449 " \"line\";", 8450 format("a << \"line line line\";", 8451 getLLVMStyleWithColumns(20))); 8452 // In a chain of << with three operands, the second can be broken with no line 8453 // break before it. 8454 EXPECT_EQ("abcde << \"line \"\n" 8455 " \"line line\"\n" 8456 " << c;", 8457 format("abcde << \"line line line\" << c;", 8458 getLLVMStyleWithColumns(20))); 8459 // In a chain of << with three operands, the third must be broken with a line 8460 // break before it. 8461 EXPECT_EQ("a << b\n" 8462 " << \"line line \"\n" 8463 " \"line\";", 8464 format("a << b << \"line line line\";", 8465 getLLVMStyleWithColumns(20))); 8466 // In a chain of << with three operands, the second can be broken with no line 8467 // break before it and the third must be broken with a line break before it. 8468 EXPECT_EQ("abcd << \"line line \"\n" 8469 " \"line\"\n" 8470 " << \"line line \"\n" 8471 " \"line\";", 8472 format("abcd << \"line line line\" << \"line line line\";", 8473 getLLVMStyleWithColumns(20))); 8474 // In a chain of binary operators with two operands, the second can be broken 8475 // with no line break before it. 8476 EXPECT_EQ("abcd + \"line line \"\n" 8477 " \"line line\";", 8478 format("abcd + \"line line line line\";", 8479 getLLVMStyleWithColumns(20))); 8480 // In a chain of binary operators with three operands, the second must be 8481 // broken with a line break before it. 8482 EXPECT_EQ("abcd +\n" 8483 " \"line line \"\n" 8484 " \"line line\" +\n" 8485 " e;", 8486 format("abcd + \"line line line line\" + e;", 8487 getLLVMStyleWithColumns(20))); 8488 // In a function call with two operands, with AlignAfterOpenBracket enabled, 8489 // the first must be broken with a line break before it. 8490 FormatStyle Style = getLLVMStyleWithColumns(25); 8491 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8492 EXPECT_EQ("someFunction(\n" 8493 " \"long long long \"\n" 8494 " \"long\",\n" 8495 " a);", 8496 format("someFunction(\"long long long long\", a);", Style)); 8497 } 8498 8499 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8500 EXPECT_EQ( 8501 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8502 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8503 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8504 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8505 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8506 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8507 } 8508 8509 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8510 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8511 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8512 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8513 "multiline raw string literal xxxxxxxxxxxxxx\n" 8514 ")x\",\n" 8515 " a),\n" 8516 " b);", 8517 format("fffffffffff(g(R\"x(\n" 8518 "multiline raw string literal xxxxxxxxxxxxxx\n" 8519 ")x\", a), b);", 8520 getGoogleStyleWithColumns(20))); 8521 EXPECT_EQ("fffffffffff(\n" 8522 " g(R\"x(qqq\n" 8523 "multiline raw string literal xxxxxxxxxxxxxx\n" 8524 ")x\",\n" 8525 " a),\n" 8526 " b);", 8527 format("fffffffffff(g(R\"x(qqq\n" 8528 "multiline raw string literal xxxxxxxxxxxxxx\n" 8529 ")x\", a), b);", 8530 getGoogleStyleWithColumns(20))); 8531 8532 EXPECT_EQ("fffffffffff(R\"x(\n" 8533 "multiline raw string literal xxxxxxxxxxxxxx\n" 8534 ")x\");", 8535 format("fffffffffff(R\"x(\n" 8536 "multiline raw string literal xxxxxxxxxxxxxx\n" 8537 ")x\");", 8538 getGoogleStyleWithColumns(20))); 8539 EXPECT_EQ("fffffffffff(R\"x(\n" 8540 "multiline raw string literal xxxxxxxxxxxxxx\n" 8541 ")x\" + bbbbbb);", 8542 format("fffffffffff(R\"x(\n" 8543 "multiline raw string literal xxxxxxxxxxxxxx\n" 8544 ")x\" + bbbbbb);", 8545 getGoogleStyleWithColumns(20))); 8546 EXPECT_EQ("fffffffffff(\n" 8547 " R\"x(\n" 8548 "multiline raw string literal xxxxxxxxxxxxxx\n" 8549 ")x\" +\n" 8550 " bbbbbb);", 8551 format("fffffffffff(\n" 8552 " R\"x(\n" 8553 "multiline raw string literal xxxxxxxxxxxxxx\n" 8554 ")x\" + bbbbbb);", 8555 getGoogleStyleWithColumns(20))); 8556 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 8557 format("fffffffffff(\n" 8558 " R\"(single line raw string)\" + bbbbbb);")); 8559 } 8560 8561 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8562 verifyFormat("string a = \"unterminated;"); 8563 EXPECT_EQ("function(\"unterminated,\n" 8564 " OtherParameter);", 8565 format("function( \"unterminated,\n" 8566 " OtherParameter);")); 8567 } 8568 8569 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8570 FormatStyle Style = getLLVMStyle(); 8571 Style.Standard = FormatStyle::LS_Cpp03; 8572 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8573 format("#define x(_a) printf(\"foo\"_a);", Style)); 8574 } 8575 8576 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8577 8578 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8579 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8580 " \"ddeeefff\");", 8581 format("someFunction(\"aaabbbcccdddeeefff\");", 8582 getLLVMStyleWithColumns(25))); 8583 EXPECT_EQ("someFunction1234567890(\n" 8584 " \"aaabbbcccdddeeefff\");", 8585 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8586 getLLVMStyleWithColumns(26))); 8587 EXPECT_EQ("someFunction1234567890(\n" 8588 " \"aaabbbcccdddeeeff\"\n" 8589 " \"f\");", 8590 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8591 getLLVMStyleWithColumns(25))); 8592 EXPECT_EQ("someFunction1234567890(\n" 8593 " \"aaabbbcccdddeeeff\"\n" 8594 " \"f\");", 8595 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8596 getLLVMStyleWithColumns(24))); 8597 EXPECT_EQ("someFunction(\n" 8598 " \"aaabbbcc ddde \"\n" 8599 " \"efff\");", 8600 format("someFunction(\"aaabbbcc ddde efff\");", 8601 getLLVMStyleWithColumns(25))); 8602 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8603 " \"ddeeefff\");", 8604 format("someFunction(\"aaabbbccc ddeeefff\");", 8605 getLLVMStyleWithColumns(25))); 8606 EXPECT_EQ("someFunction1234567890(\n" 8607 " \"aaabb \"\n" 8608 " \"cccdddeeefff\");", 8609 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8610 getLLVMStyleWithColumns(25))); 8611 EXPECT_EQ("#define A \\\n" 8612 " string s = \\\n" 8613 " \"123456789\" \\\n" 8614 " \"0\"; \\\n" 8615 " int i;", 8616 format("#define A string s = \"1234567890\"; int i;", 8617 getLLVMStyleWithColumns(20))); 8618 EXPECT_EQ("someFunction(\n" 8619 " \"aaabbbcc \"\n" 8620 " \"dddeeefff\");", 8621 format("someFunction(\"aaabbbcc dddeeefff\");", 8622 getLLVMStyleWithColumns(25))); 8623 } 8624 8625 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8626 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8627 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8628 EXPECT_EQ("\"test\"\n" 8629 "\"\\n\"", 8630 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8631 EXPECT_EQ("\"tes\\\\\"\n" 8632 "\"n\"", 8633 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8634 EXPECT_EQ("\"\\\\\\\\\"\n" 8635 "\"\\n\"", 8636 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8637 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8638 EXPECT_EQ("\"\\uff01\"\n" 8639 "\"test\"", 8640 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8641 EXPECT_EQ("\"\\Uff01ff02\"", 8642 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8643 EXPECT_EQ("\"\\x000000000001\"\n" 8644 "\"next\"", 8645 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8646 EXPECT_EQ("\"\\x000000000001next\"", 8647 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8648 EXPECT_EQ("\"\\x000000000001\"", 8649 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8650 EXPECT_EQ("\"test\"\n" 8651 "\"\\000000\"\n" 8652 "\"000001\"", 8653 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8654 EXPECT_EQ("\"test\\000\"\n" 8655 "\"00000000\"\n" 8656 "\"1\"", 8657 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8658 } 8659 8660 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8661 verifyFormat("void f() {\n" 8662 " return g() {}\n" 8663 " void h() {}"); 8664 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8665 "g();\n" 8666 "}"); 8667 } 8668 8669 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8670 verifyFormat( 8671 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8672 } 8673 8674 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8675 verifyFormat("class X {\n" 8676 " void f() {\n" 8677 " }\n" 8678 "};", 8679 getLLVMStyleWithColumns(12)); 8680 } 8681 8682 TEST_F(FormatTest, ConfigurableIndentWidth) { 8683 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8684 EightIndent.IndentWidth = 8; 8685 EightIndent.ContinuationIndentWidth = 8; 8686 verifyFormat("void f() {\n" 8687 " someFunction();\n" 8688 " if (true) {\n" 8689 " f();\n" 8690 " }\n" 8691 "}", 8692 EightIndent); 8693 verifyFormat("class X {\n" 8694 " void f() {\n" 8695 " }\n" 8696 "};", 8697 EightIndent); 8698 verifyFormat("int x[] = {\n" 8699 " call(),\n" 8700 " call()};", 8701 EightIndent); 8702 } 8703 8704 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8705 verifyFormat("double\n" 8706 "f();", 8707 getLLVMStyleWithColumns(8)); 8708 } 8709 8710 TEST_F(FormatTest, ConfigurableUseOfTab) { 8711 FormatStyle Tab = getLLVMStyleWithColumns(42); 8712 Tab.IndentWidth = 8; 8713 Tab.UseTab = FormatStyle::UT_Always; 8714 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8715 8716 EXPECT_EQ("if (aaaaaaaa && // q\n" 8717 " bb)\t\t// w\n" 8718 "\t;", 8719 format("if (aaaaaaaa &&// q\n" 8720 "bb)// w\n" 8721 ";", 8722 Tab)); 8723 EXPECT_EQ("if (aaa && bbb) // w\n" 8724 "\t;", 8725 format("if(aaa&&bbb)// w\n" 8726 ";", 8727 Tab)); 8728 8729 verifyFormat("class X {\n" 8730 "\tvoid f() {\n" 8731 "\t\tsomeFunction(parameter1,\n" 8732 "\t\t\t parameter2);\n" 8733 "\t}\n" 8734 "};", 8735 Tab); 8736 verifyFormat("#define A \\\n" 8737 "\tvoid f() { \\\n" 8738 "\t\tsomeFunction( \\\n" 8739 "\t\t parameter1, \\\n" 8740 "\t\t parameter2); \\\n" 8741 "\t}", 8742 Tab); 8743 8744 Tab.TabWidth = 4; 8745 Tab.IndentWidth = 8; 8746 verifyFormat("class TabWidth4Indent8 {\n" 8747 "\t\tvoid f() {\n" 8748 "\t\t\t\tsomeFunction(parameter1,\n" 8749 "\t\t\t\t\t\t\t parameter2);\n" 8750 "\t\t}\n" 8751 "};", 8752 Tab); 8753 8754 Tab.TabWidth = 4; 8755 Tab.IndentWidth = 4; 8756 verifyFormat("class TabWidth4Indent4 {\n" 8757 "\tvoid f() {\n" 8758 "\t\tsomeFunction(parameter1,\n" 8759 "\t\t\t\t\t parameter2);\n" 8760 "\t}\n" 8761 "};", 8762 Tab); 8763 8764 Tab.TabWidth = 8; 8765 Tab.IndentWidth = 4; 8766 verifyFormat("class TabWidth8Indent4 {\n" 8767 " void f() {\n" 8768 "\tsomeFunction(parameter1,\n" 8769 "\t\t parameter2);\n" 8770 " }\n" 8771 "};", 8772 Tab); 8773 8774 Tab.TabWidth = 8; 8775 Tab.IndentWidth = 8; 8776 EXPECT_EQ("/*\n" 8777 "\t a\t\tcomment\n" 8778 "\t in multiple lines\n" 8779 " */", 8780 format(" /*\t \t \n" 8781 " \t \t a\t\tcomment\t \t\n" 8782 " \t \t in multiple lines\t\n" 8783 " \t */", 8784 Tab)); 8785 8786 Tab.UseTab = FormatStyle::UT_ForIndentation; 8787 verifyFormat("{\n" 8788 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8789 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8790 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8791 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8792 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8793 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8794 "};", 8795 Tab); 8796 verifyFormat("enum AA {\n" 8797 "\ta1, // Force multiple lines\n" 8798 "\ta2,\n" 8799 "\ta3\n" 8800 "};", 8801 Tab); 8802 EXPECT_EQ("if (aaaaaaaa && // q\n" 8803 " bb) // w\n" 8804 "\t;", 8805 format("if (aaaaaaaa &&// q\n" 8806 "bb)// w\n" 8807 ";", 8808 Tab)); 8809 verifyFormat("class X {\n" 8810 "\tvoid f() {\n" 8811 "\t\tsomeFunction(parameter1,\n" 8812 "\t\t parameter2);\n" 8813 "\t}\n" 8814 "};", 8815 Tab); 8816 verifyFormat("{\n" 8817 "\tQ(\n" 8818 "\t {\n" 8819 "\t\t int a;\n" 8820 "\t\t someFunction(aaaaaaaa,\n" 8821 "\t\t bbbbbbb);\n" 8822 "\t },\n" 8823 "\t p);\n" 8824 "}", 8825 Tab); 8826 EXPECT_EQ("{\n" 8827 "\t/* aaaa\n" 8828 "\t bbbb */\n" 8829 "}", 8830 format("{\n" 8831 "/* aaaa\n" 8832 " bbbb */\n" 8833 "}", 8834 Tab)); 8835 EXPECT_EQ("{\n" 8836 "\t/*\n" 8837 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8838 "\t bbbbbbbbbbbbb\n" 8839 "\t*/\n" 8840 "}", 8841 format("{\n" 8842 "/*\n" 8843 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8844 "*/\n" 8845 "}", 8846 Tab)); 8847 EXPECT_EQ("{\n" 8848 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8849 "\t// bbbbbbbbbbbbb\n" 8850 "}", 8851 format("{\n" 8852 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8853 "}", 8854 Tab)); 8855 EXPECT_EQ("{\n" 8856 "\t/*\n" 8857 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8858 "\t bbbbbbbbbbbbb\n" 8859 "\t*/\n" 8860 "}", 8861 format("{\n" 8862 "\t/*\n" 8863 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8864 "\t*/\n" 8865 "}", 8866 Tab)); 8867 EXPECT_EQ("{\n" 8868 "\t/*\n" 8869 "\n" 8870 "\t*/\n" 8871 "}", 8872 format("{\n" 8873 "\t/*\n" 8874 "\n" 8875 "\t*/\n" 8876 "}", 8877 Tab)); 8878 EXPECT_EQ("{\n" 8879 "\t/*\n" 8880 " asdf\n" 8881 "\t*/\n" 8882 "}", 8883 format("{\n" 8884 "\t/*\n" 8885 " asdf\n" 8886 "\t*/\n" 8887 "}", 8888 Tab)); 8889 8890 Tab.UseTab = FormatStyle::UT_Never; 8891 EXPECT_EQ("/*\n" 8892 " a\t\tcomment\n" 8893 " in multiple lines\n" 8894 " */", 8895 format(" /*\t \t \n" 8896 " \t \t a\t\tcomment\t \t\n" 8897 " \t \t in multiple lines\t\n" 8898 " \t */", 8899 Tab)); 8900 EXPECT_EQ("/* some\n" 8901 " comment */", 8902 format(" \t \t /* some\n" 8903 " \t \t comment */", 8904 Tab)); 8905 EXPECT_EQ("int a; /* some\n" 8906 " comment */", 8907 format(" \t \t int a; /* some\n" 8908 " \t \t comment */", 8909 Tab)); 8910 8911 EXPECT_EQ("int a; /* some\n" 8912 "comment */", 8913 format(" \t \t int\ta; /* some\n" 8914 " \t \t comment */", 8915 Tab)); 8916 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8917 " comment */", 8918 format(" \t \t f(\"\t\t\"); /* some\n" 8919 " \t \t comment */", 8920 Tab)); 8921 EXPECT_EQ("{\n" 8922 " /*\n" 8923 " * Comment\n" 8924 " */\n" 8925 " int i;\n" 8926 "}", 8927 format("{\n" 8928 "\t/*\n" 8929 "\t * Comment\n" 8930 "\t */\n" 8931 "\t int i;\n" 8932 "}")); 8933 8934 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8935 Tab.TabWidth = 8; 8936 Tab.IndentWidth = 8; 8937 EXPECT_EQ("if (aaaaaaaa && // q\n" 8938 " bb) // w\n" 8939 "\t;", 8940 format("if (aaaaaaaa &&// q\n" 8941 "bb)// w\n" 8942 ";", 8943 Tab)); 8944 EXPECT_EQ("if (aaa && bbb) // w\n" 8945 "\t;", 8946 format("if(aaa&&bbb)// w\n" 8947 ";", 8948 Tab)); 8949 verifyFormat("class X {\n" 8950 "\tvoid f() {\n" 8951 "\t\tsomeFunction(parameter1,\n" 8952 "\t\t\t parameter2);\n" 8953 "\t}\n" 8954 "};", 8955 Tab); 8956 verifyFormat("#define A \\\n" 8957 "\tvoid f() { \\\n" 8958 "\t\tsomeFunction( \\\n" 8959 "\t\t parameter1, \\\n" 8960 "\t\t parameter2); \\\n" 8961 "\t}", 8962 Tab); 8963 Tab.TabWidth = 4; 8964 Tab.IndentWidth = 8; 8965 verifyFormat("class TabWidth4Indent8 {\n" 8966 "\t\tvoid f() {\n" 8967 "\t\t\t\tsomeFunction(parameter1,\n" 8968 "\t\t\t\t\t\t\t parameter2);\n" 8969 "\t\t}\n" 8970 "};", 8971 Tab); 8972 Tab.TabWidth = 4; 8973 Tab.IndentWidth = 4; 8974 verifyFormat("class TabWidth4Indent4 {\n" 8975 "\tvoid f() {\n" 8976 "\t\tsomeFunction(parameter1,\n" 8977 "\t\t\t\t\t parameter2);\n" 8978 "\t}\n" 8979 "};", 8980 Tab); 8981 Tab.TabWidth = 8; 8982 Tab.IndentWidth = 4; 8983 verifyFormat("class TabWidth8Indent4 {\n" 8984 " void f() {\n" 8985 "\tsomeFunction(parameter1,\n" 8986 "\t\t parameter2);\n" 8987 " }\n" 8988 "};", 8989 Tab); 8990 Tab.TabWidth = 8; 8991 Tab.IndentWidth = 8; 8992 EXPECT_EQ("/*\n" 8993 "\t a\t\tcomment\n" 8994 "\t in multiple lines\n" 8995 " */", 8996 format(" /*\t \t \n" 8997 " \t \t a\t\tcomment\t \t\n" 8998 " \t \t in multiple lines\t\n" 8999 " \t */", 9000 Tab)); 9001 verifyFormat("{\n" 9002 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9003 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9004 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9005 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9006 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9007 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9008 "};", 9009 Tab); 9010 verifyFormat("enum AA {\n" 9011 "\ta1, // Force multiple lines\n" 9012 "\ta2,\n" 9013 "\ta3\n" 9014 "};", 9015 Tab); 9016 EXPECT_EQ("if (aaaaaaaa && // q\n" 9017 " bb) // w\n" 9018 "\t;", 9019 format("if (aaaaaaaa &&// q\n" 9020 "bb)// w\n" 9021 ";", 9022 Tab)); 9023 verifyFormat("class X {\n" 9024 "\tvoid f() {\n" 9025 "\t\tsomeFunction(parameter1,\n" 9026 "\t\t\t parameter2);\n" 9027 "\t}\n" 9028 "};", 9029 Tab); 9030 verifyFormat("{\n" 9031 "\tQ(\n" 9032 "\t {\n" 9033 "\t\t int a;\n" 9034 "\t\t someFunction(aaaaaaaa,\n" 9035 "\t\t\t\t bbbbbbb);\n" 9036 "\t },\n" 9037 "\t p);\n" 9038 "}", 9039 Tab); 9040 EXPECT_EQ("{\n" 9041 "\t/* aaaa\n" 9042 "\t bbbb */\n" 9043 "}", 9044 format("{\n" 9045 "/* aaaa\n" 9046 " bbbb */\n" 9047 "}", 9048 Tab)); 9049 EXPECT_EQ("{\n" 9050 "\t/*\n" 9051 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9052 "\t bbbbbbbbbbbbb\n" 9053 "\t*/\n" 9054 "}", 9055 format("{\n" 9056 "/*\n" 9057 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9058 "*/\n" 9059 "}", 9060 Tab)); 9061 EXPECT_EQ("{\n" 9062 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9063 "\t// bbbbbbbbbbbbb\n" 9064 "}", 9065 format("{\n" 9066 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9067 "}", 9068 Tab)); 9069 EXPECT_EQ("{\n" 9070 "\t/*\n" 9071 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9072 "\t bbbbbbbbbbbbb\n" 9073 "\t*/\n" 9074 "}", 9075 format("{\n" 9076 "\t/*\n" 9077 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9078 "\t*/\n" 9079 "}", 9080 Tab)); 9081 EXPECT_EQ("{\n" 9082 "\t/*\n" 9083 "\n" 9084 "\t*/\n" 9085 "}", 9086 format("{\n" 9087 "\t/*\n" 9088 "\n" 9089 "\t*/\n" 9090 "}", 9091 Tab)); 9092 EXPECT_EQ("{\n" 9093 "\t/*\n" 9094 " asdf\n" 9095 "\t*/\n" 9096 "}", 9097 format("{\n" 9098 "\t/*\n" 9099 " asdf\n" 9100 "\t*/\n" 9101 "}", 9102 Tab)); 9103 EXPECT_EQ("/*\n" 9104 "\t a\t\tcomment\n" 9105 "\t in multiple lines\n" 9106 " */", 9107 format(" /*\t \t \n" 9108 " \t \t a\t\tcomment\t \t\n" 9109 " \t \t in multiple lines\t\n" 9110 " \t */", 9111 Tab)); 9112 EXPECT_EQ("/* some\n" 9113 " comment */", 9114 format(" \t \t /* some\n" 9115 " \t \t comment */", 9116 Tab)); 9117 EXPECT_EQ("int a; /* some\n" 9118 " comment */", 9119 format(" \t \t int a; /* some\n" 9120 " \t \t comment */", 9121 Tab)); 9122 EXPECT_EQ("int a; /* some\n" 9123 "comment */", 9124 format(" \t \t int\ta; /* some\n" 9125 " \t \t comment */", 9126 Tab)); 9127 EXPECT_EQ("f(\"\t\t\"); /* some\n" 9128 " comment */", 9129 format(" \t \t f(\"\t\t\"); /* some\n" 9130 " \t \t comment */", 9131 Tab)); 9132 EXPECT_EQ("{\n" 9133 " /*\n" 9134 " * Comment\n" 9135 " */\n" 9136 " int i;\n" 9137 "}", 9138 format("{\n" 9139 "\t/*\n" 9140 "\t * Comment\n" 9141 "\t */\n" 9142 "\t int i;\n" 9143 "}")); 9144 Tab.AlignConsecutiveAssignments = true; 9145 Tab.AlignConsecutiveDeclarations = true; 9146 Tab.TabWidth = 4; 9147 Tab.IndentWidth = 4; 9148 verifyFormat("class Assign {\n" 9149 "\tvoid f() {\n" 9150 "\t\tint x = 123;\n" 9151 "\t\tint random = 4;\n" 9152 "\t\tstd::string alphabet =\n" 9153 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 9154 "\t}\n" 9155 "};", 9156 Tab); 9157 } 9158 9159 TEST_F(FormatTest, CalculatesOriginalColumn) { 9160 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9161 "q\"; /* some\n" 9162 " comment */", 9163 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9164 "q\"; /* some\n" 9165 " comment */", 9166 getLLVMStyle())); 9167 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 9168 "/* some\n" 9169 " comment */", 9170 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 9171 " /* some\n" 9172 " comment */", 9173 getLLVMStyle())); 9174 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9175 "qqq\n" 9176 "/* some\n" 9177 " comment */", 9178 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9179 "qqq\n" 9180 " /* some\n" 9181 " comment */", 9182 getLLVMStyle())); 9183 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9184 "wwww; /* some\n" 9185 " comment */", 9186 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9187 "wwww; /* some\n" 9188 " comment */", 9189 getLLVMStyle())); 9190 } 9191 9192 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 9193 FormatStyle NoSpace = getLLVMStyle(); 9194 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 9195 9196 verifyFormat("while(true)\n" 9197 " continue;", 9198 NoSpace); 9199 verifyFormat("for(;;)\n" 9200 " continue;", 9201 NoSpace); 9202 verifyFormat("if(true)\n" 9203 " f();\n" 9204 "else if(true)\n" 9205 " f();", 9206 NoSpace); 9207 verifyFormat("do {\n" 9208 " do_something();\n" 9209 "} while(something());", 9210 NoSpace); 9211 verifyFormat("switch(x) {\n" 9212 "default:\n" 9213 " break;\n" 9214 "}", 9215 NoSpace); 9216 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 9217 verifyFormat("size_t x = sizeof(x);", NoSpace); 9218 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 9219 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 9220 verifyFormat("alignas(128) char a[128];", NoSpace); 9221 verifyFormat("size_t x = alignof(MyType);", NoSpace); 9222 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 9223 verifyFormat("int f() throw(Deprecated);", NoSpace); 9224 verifyFormat("typedef void (*cb)(int);", NoSpace); 9225 verifyFormat("T A::operator()();", NoSpace); 9226 verifyFormat("X A::operator++(T);", NoSpace); 9227 9228 FormatStyle Space = getLLVMStyle(); 9229 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 9230 9231 verifyFormat("int f ();", Space); 9232 verifyFormat("void f (int a, T b) {\n" 9233 " while (true)\n" 9234 " continue;\n" 9235 "}", 9236 Space); 9237 verifyFormat("if (true)\n" 9238 " f ();\n" 9239 "else if (true)\n" 9240 " f ();", 9241 Space); 9242 verifyFormat("do {\n" 9243 " do_something ();\n" 9244 "} while (something ());", 9245 Space); 9246 verifyFormat("switch (x) {\n" 9247 "default:\n" 9248 " break;\n" 9249 "}", 9250 Space); 9251 verifyFormat("A::A () : a (1) {}", Space); 9252 verifyFormat("void f () __attribute__ ((asdf));", Space); 9253 verifyFormat("*(&a + 1);\n" 9254 "&((&a)[1]);\n" 9255 "a[(b + c) * d];\n" 9256 "(((a + 1) * 2) + 3) * 4;", 9257 Space); 9258 verifyFormat("#define A(x) x", Space); 9259 verifyFormat("#define A (x) x", Space); 9260 verifyFormat("#if defined(x)\n" 9261 "#endif", 9262 Space); 9263 verifyFormat("auto i = std::make_unique<int> (5);", Space); 9264 verifyFormat("size_t x = sizeof (x);", Space); 9265 verifyFormat("auto f (int x) -> decltype (x);", Space); 9266 verifyFormat("int f (T x) noexcept (x.create ());", Space); 9267 verifyFormat("alignas (128) char a[128];", Space); 9268 verifyFormat("size_t x = alignof (MyType);", Space); 9269 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 9270 verifyFormat("int f () throw (Deprecated);", Space); 9271 verifyFormat("typedef void (*cb) (int);", Space); 9272 verifyFormat("T A::operator() ();", Space); 9273 verifyFormat("X A::operator++ (T);", Space); 9274 } 9275 9276 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 9277 FormatStyle Spaces = getLLVMStyle(); 9278 9279 Spaces.SpacesInParentheses = true; 9280 verifyFormat("do_something( ::globalVar );", Spaces); 9281 verifyFormat("call( x, y, z );", Spaces); 9282 verifyFormat("call();", Spaces); 9283 verifyFormat("std::function<void( int, int )> callback;", Spaces); 9284 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 9285 Spaces); 9286 verifyFormat("while ( (bool)1 )\n" 9287 " continue;", 9288 Spaces); 9289 verifyFormat("for ( ;; )\n" 9290 " continue;", 9291 Spaces); 9292 verifyFormat("if ( true )\n" 9293 " f();\n" 9294 "else if ( true )\n" 9295 " f();", 9296 Spaces); 9297 verifyFormat("do {\n" 9298 " do_something( (int)i );\n" 9299 "} while ( something() );", 9300 Spaces); 9301 verifyFormat("switch ( x ) {\n" 9302 "default:\n" 9303 " break;\n" 9304 "}", 9305 Spaces); 9306 9307 Spaces.SpacesInParentheses = false; 9308 Spaces.SpacesInCStyleCastParentheses = true; 9309 verifyFormat("Type *A = ( Type * )P;", Spaces); 9310 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 9311 verifyFormat("x = ( int32 )y;", Spaces); 9312 verifyFormat("int a = ( int )(2.0f);", Spaces); 9313 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 9314 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 9315 verifyFormat("#define x (( int )-1)", Spaces); 9316 9317 // Run the first set of tests again with: 9318 Spaces.SpacesInParentheses = false; 9319 Spaces.SpaceInEmptyParentheses = true; 9320 Spaces.SpacesInCStyleCastParentheses = true; 9321 verifyFormat("call(x, y, z);", Spaces); 9322 verifyFormat("call( );", Spaces); 9323 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9324 verifyFormat("while (( bool )1)\n" 9325 " continue;", 9326 Spaces); 9327 verifyFormat("for (;;)\n" 9328 " continue;", 9329 Spaces); 9330 verifyFormat("if (true)\n" 9331 " f( );\n" 9332 "else if (true)\n" 9333 " f( );", 9334 Spaces); 9335 verifyFormat("do {\n" 9336 " do_something(( int )i);\n" 9337 "} while (something( ));", 9338 Spaces); 9339 verifyFormat("switch (x) {\n" 9340 "default:\n" 9341 " break;\n" 9342 "}", 9343 Spaces); 9344 9345 // Run the first set of tests again with: 9346 Spaces.SpaceAfterCStyleCast = true; 9347 verifyFormat("call(x, y, z);", Spaces); 9348 verifyFormat("call( );", Spaces); 9349 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9350 verifyFormat("while (( bool ) 1)\n" 9351 " continue;", 9352 Spaces); 9353 verifyFormat("for (;;)\n" 9354 " continue;", 9355 Spaces); 9356 verifyFormat("if (true)\n" 9357 " f( );\n" 9358 "else if (true)\n" 9359 " f( );", 9360 Spaces); 9361 verifyFormat("do {\n" 9362 " do_something(( int ) i);\n" 9363 "} while (something( ));", 9364 Spaces); 9365 verifyFormat("switch (x) {\n" 9366 "default:\n" 9367 " break;\n" 9368 "}", 9369 Spaces); 9370 9371 // Run subset of tests again with: 9372 Spaces.SpacesInCStyleCastParentheses = false; 9373 Spaces.SpaceAfterCStyleCast = true; 9374 verifyFormat("while ((bool) 1)\n" 9375 " continue;", 9376 Spaces); 9377 verifyFormat("do {\n" 9378 " do_something((int) i);\n" 9379 "} while (something( ));", 9380 Spaces); 9381 } 9382 9383 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 9384 verifyFormat("int a[5];"); 9385 verifyFormat("a[3] += 42;"); 9386 9387 FormatStyle Spaces = getLLVMStyle(); 9388 Spaces.SpacesInSquareBrackets = true; 9389 // Lambdas unchanged. 9390 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 9391 verifyFormat("return [i, args...] {};", Spaces); 9392 9393 // Not lambdas. 9394 verifyFormat("int a[ 5 ];", Spaces); 9395 verifyFormat("a[ 3 ] += 42;", Spaces); 9396 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 9397 verifyFormat("double &operator[](int i) { return 0; }\n" 9398 "int i;", 9399 Spaces); 9400 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 9401 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 9402 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 9403 } 9404 9405 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 9406 verifyFormat("int a = 5;"); 9407 verifyFormat("a += 42;"); 9408 verifyFormat("a or_eq 8;"); 9409 9410 FormatStyle Spaces = getLLVMStyle(); 9411 Spaces.SpaceBeforeAssignmentOperators = false; 9412 verifyFormat("int a= 5;", Spaces); 9413 verifyFormat("a+= 42;", Spaces); 9414 verifyFormat("a or_eq 8;", Spaces); 9415 } 9416 9417 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 9418 verifyFormat("class Foo : public Bar {};"); 9419 verifyFormat("Foo::Foo() : foo(1) {}"); 9420 verifyFormat("for (auto a : b) {\n}"); 9421 verifyFormat("int x = a ? b : c;"); 9422 verifyFormat("{\n" 9423 "label0:\n" 9424 " int x = 0;\n" 9425 "}"); 9426 verifyFormat("switch (x) {\n" 9427 "case 1:\n" 9428 "default:\n" 9429 "}"); 9430 9431 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 9432 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 9433 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 9434 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 9435 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 9436 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 9437 verifyFormat("{\n" 9438 "label1:\n" 9439 " int x = 0;\n" 9440 "}", 9441 CtorInitializerStyle); 9442 verifyFormat("switch (x) {\n" 9443 "case 1:\n" 9444 "default:\n" 9445 "}", 9446 CtorInitializerStyle); 9447 CtorInitializerStyle.BreakConstructorInitializers = 9448 FormatStyle::BCIS_AfterColon; 9449 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 9450 " aaaaaaaaaaaaaaaa(1),\n" 9451 " bbbbbbbbbbbbbbbb(2) {}", 9452 CtorInitializerStyle); 9453 CtorInitializerStyle.BreakConstructorInitializers = 9454 FormatStyle::BCIS_BeforeComma; 9455 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9456 " : aaaaaaaaaaaaaaaa(1)\n" 9457 " , bbbbbbbbbbbbbbbb(2) {}", 9458 CtorInitializerStyle); 9459 CtorInitializerStyle.BreakConstructorInitializers = 9460 FormatStyle::BCIS_BeforeColon; 9461 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9462 " : aaaaaaaaaaaaaaaa(1),\n" 9463 " bbbbbbbbbbbbbbbb(2) {}", 9464 CtorInitializerStyle); 9465 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 9466 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9467 ": aaaaaaaaaaaaaaaa(1),\n" 9468 " bbbbbbbbbbbbbbbb(2) {}", 9469 CtorInitializerStyle); 9470 9471 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 9472 InheritanceStyle.SpaceBeforeInheritanceColon = false; 9473 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 9474 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 9475 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 9476 verifyFormat("int x = a ? b : c;", InheritanceStyle); 9477 verifyFormat("{\n" 9478 "label2:\n" 9479 " int x = 0;\n" 9480 "}", 9481 InheritanceStyle); 9482 verifyFormat("switch (x) {\n" 9483 "case 1:\n" 9484 "default:\n" 9485 "}", 9486 InheritanceStyle); 9487 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 9488 verifyFormat("class Foooooooooooooooooooooo:\n" 9489 " public aaaaaaaaaaaaaaaaaa,\n" 9490 " public bbbbbbbbbbbbbbbbbb {\n" 9491 "}", 9492 InheritanceStyle); 9493 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 9494 verifyFormat("class Foooooooooooooooooooooo\n" 9495 " : public aaaaaaaaaaaaaaaaaa\n" 9496 " , public bbbbbbbbbbbbbbbbbb {\n" 9497 "}", 9498 InheritanceStyle); 9499 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 9500 verifyFormat("class Foooooooooooooooooooooo\n" 9501 " : public aaaaaaaaaaaaaaaaaa,\n" 9502 " public bbbbbbbbbbbbbbbbbb {\n" 9503 "}", 9504 InheritanceStyle); 9505 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 9506 verifyFormat("class Foooooooooooooooooooooo\n" 9507 ": public aaaaaaaaaaaaaaaaaa,\n" 9508 " public bbbbbbbbbbbbbbbbbb {}", 9509 InheritanceStyle); 9510 9511 FormatStyle ForLoopStyle = getLLVMStyle(); 9512 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 9513 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 9514 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 9515 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 9516 verifyFormat("int x = a ? b : c;", ForLoopStyle); 9517 verifyFormat("{\n" 9518 "label2:\n" 9519 " int x = 0;\n" 9520 "}", 9521 ForLoopStyle); 9522 verifyFormat("switch (x) {\n" 9523 "case 1:\n" 9524 "default:\n" 9525 "}", 9526 ForLoopStyle); 9527 9528 FormatStyle NoSpaceStyle = getLLVMStyle(); 9529 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 9530 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 9531 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 9532 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 9533 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 9534 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 9535 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 9536 verifyFormat("{\n" 9537 "label3:\n" 9538 " int x = 0;\n" 9539 "}", 9540 NoSpaceStyle); 9541 verifyFormat("switch (x) {\n" 9542 "case 1:\n" 9543 "default:\n" 9544 "}", 9545 NoSpaceStyle); 9546 } 9547 9548 TEST_F(FormatTest, AlignConsecutiveAssignments) { 9549 FormatStyle Alignment = getLLVMStyle(); 9550 Alignment.AlignConsecutiveAssignments = false; 9551 verifyFormat("int a = 5;\n" 9552 "int oneTwoThree = 123;", 9553 Alignment); 9554 verifyFormat("int a = 5;\n" 9555 "int oneTwoThree = 123;", 9556 Alignment); 9557 9558 Alignment.AlignConsecutiveAssignments = true; 9559 verifyFormat("int a = 5;\n" 9560 "int oneTwoThree = 123;", 9561 Alignment); 9562 verifyFormat("int a = method();\n" 9563 "int oneTwoThree = 133;", 9564 Alignment); 9565 verifyFormat("a &= 5;\n" 9566 "bcd *= 5;\n" 9567 "ghtyf += 5;\n" 9568 "dvfvdb -= 5;\n" 9569 "a /= 5;\n" 9570 "vdsvsv %= 5;\n" 9571 "sfdbddfbdfbb ^= 5;\n" 9572 "dvsdsv |= 5;\n" 9573 "int dsvvdvsdvvv = 123;", 9574 Alignment); 9575 verifyFormat("int i = 1, j = 10;\n" 9576 "something = 2000;", 9577 Alignment); 9578 verifyFormat("something = 2000;\n" 9579 "int i = 1, j = 10;\n", 9580 Alignment); 9581 verifyFormat("something = 2000;\n" 9582 "another = 911;\n" 9583 "int i = 1, j = 10;\n" 9584 "oneMore = 1;\n" 9585 "i = 2;", 9586 Alignment); 9587 verifyFormat("int a = 5;\n" 9588 "int one = 1;\n" 9589 "method();\n" 9590 "int oneTwoThree = 123;\n" 9591 "int oneTwo = 12;", 9592 Alignment); 9593 verifyFormat("int oneTwoThree = 123;\n" 9594 "int oneTwo = 12;\n" 9595 "method();\n", 9596 Alignment); 9597 verifyFormat("int oneTwoThree = 123; // comment\n" 9598 "int oneTwo = 12; // comment", 9599 Alignment); 9600 EXPECT_EQ("int a = 5;\n" 9601 "\n" 9602 "int oneTwoThree = 123;", 9603 format("int a = 5;\n" 9604 "\n" 9605 "int oneTwoThree= 123;", 9606 Alignment)); 9607 EXPECT_EQ("int a = 5;\n" 9608 "int one = 1;\n" 9609 "\n" 9610 "int oneTwoThree = 123;", 9611 format("int a = 5;\n" 9612 "int one = 1;\n" 9613 "\n" 9614 "int oneTwoThree = 123;", 9615 Alignment)); 9616 EXPECT_EQ("int a = 5;\n" 9617 "int one = 1;\n" 9618 "\n" 9619 "int oneTwoThree = 123;\n" 9620 "int oneTwo = 12;", 9621 format("int a = 5;\n" 9622 "int one = 1;\n" 9623 "\n" 9624 "int oneTwoThree = 123;\n" 9625 "int oneTwo = 12;", 9626 Alignment)); 9627 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9628 verifyFormat("#define A \\\n" 9629 " int aaaa = 12; \\\n" 9630 " int b = 23; \\\n" 9631 " int ccc = 234; \\\n" 9632 " int dddddddddd = 2345;", 9633 Alignment); 9634 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9635 verifyFormat("#define A \\\n" 9636 " int aaaa = 12; \\\n" 9637 " int b = 23; \\\n" 9638 " int ccc = 234; \\\n" 9639 " int dddddddddd = 2345;", 9640 Alignment); 9641 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9642 verifyFormat("#define A " 9643 " \\\n" 9644 " int aaaa = 12; " 9645 " \\\n" 9646 " int b = 23; " 9647 " \\\n" 9648 " int ccc = 234; " 9649 " \\\n" 9650 " int dddddddddd = 2345;", 9651 Alignment); 9652 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9653 "k = 4, int l = 5,\n" 9654 " int m = 6) {\n" 9655 " int j = 10;\n" 9656 " otherThing = 1;\n" 9657 "}", 9658 Alignment); 9659 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9660 " int i = 1;\n" 9661 " int j = 2;\n" 9662 " int big = 10000;\n" 9663 "}", 9664 Alignment); 9665 verifyFormat("class C {\n" 9666 "public:\n" 9667 " int i = 1;\n" 9668 " virtual void f() = 0;\n" 9669 "};", 9670 Alignment); 9671 verifyFormat("int i = 1;\n" 9672 "if (SomeType t = getSomething()) {\n" 9673 "}\n" 9674 "int j = 2;\n" 9675 "int big = 10000;", 9676 Alignment); 9677 verifyFormat("int j = 7;\n" 9678 "for (int k = 0; k < N; ++k) {\n" 9679 "}\n" 9680 "int j = 2;\n" 9681 "int big = 10000;\n" 9682 "}", 9683 Alignment); 9684 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9685 verifyFormat("int i = 1;\n" 9686 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9687 " = someLooooooooooooooooongFunction();\n" 9688 "int j = 2;", 9689 Alignment); 9690 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9691 verifyFormat("int i = 1;\n" 9692 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9693 " someLooooooooooooooooongFunction();\n" 9694 "int j = 2;", 9695 Alignment); 9696 9697 verifyFormat("auto lambda = []() {\n" 9698 " auto i = 0;\n" 9699 " return 0;\n" 9700 "};\n" 9701 "int i = 0;\n" 9702 "auto v = type{\n" 9703 " i = 1, //\n" 9704 " (i = 2), //\n" 9705 " i = 3 //\n" 9706 "};", 9707 Alignment); 9708 9709 verifyFormat( 9710 "int i = 1;\n" 9711 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9712 " loooooooooooooooooooooongParameterB);\n" 9713 "int j = 2;", 9714 Alignment); 9715 9716 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 9717 " typename B = very_long_type_name_1,\n" 9718 " typename T_2 = very_long_type_name_2>\n" 9719 "auto foo() {}\n", 9720 Alignment); 9721 verifyFormat("int a, b = 1;\n" 9722 "int c = 2;\n" 9723 "int dd = 3;\n", 9724 Alignment); 9725 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9726 "float b[1][] = {{3.f}};\n", 9727 Alignment); 9728 verifyFormat("for (int i = 0; i < 1; i++)\n" 9729 " int x = 1;\n", 9730 Alignment); 9731 verifyFormat("for (i = 0; i < 1; i++)\n" 9732 " x = 1;\n" 9733 "y = 1;\n", 9734 Alignment); 9735 } 9736 9737 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 9738 FormatStyle Alignment = getLLVMStyle(); 9739 Alignment.AlignConsecutiveDeclarations = false; 9740 verifyFormat("float const a = 5;\n" 9741 "int oneTwoThree = 123;", 9742 Alignment); 9743 verifyFormat("int a = 5;\n" 9744 "float const oneTwoThree = 123;", 9745 Alignment); 9746 9747 Alignment.AlignConsecutiveDeclarations = true; 9748 verifyFormat("float const a = 5;\n" 9749 "int oneTwoThree = 123;", 9750 Alignment); 9751 verifyFormat("int a = method();\n" 9752 "float const oneTwoThree = 133;", 9753 Alignment); 9754 verifyFormat("int i = 1, j = 10;\n" 9755 "something = 2000;", 9756 Alignment); 9757 verifyFormat("something = 2000;\n" 9758 "int i = 1, j = 10;\n", 9759 Alignment); 9760 verifyFormat("float something = 2000;\n" 9761 "double another = 911;\n" 9762 "int i = 1, j = 10;\n" 9763 "const int *oneMore = 1;\n" 9764 "unsigned i = 2;", 9765 Alignment); 9766 verifyFormat("float a = 5;\n" 9767 "int one = 1;\n" 9768 "method();\n" 9769 "const double oneTwoThree = 123;\n" 9770 "const unsigned int oneTwo = 12;", 9771 Alignment); 9772 verifyFormat("int oneTwoThree{0}; // comment\n" 9773 "unsigned oneTwo; // comment", 9774 Alignment); 9775 EXPECT_EQ("float const a = 5;\n" 9776 "\n" 9777 "int oneTwoThree = 123;", 9778 format("float const a = 5;\n" 9779 "\n" 9780 "int oneTwoThree= 123;", 9781 Alignment)); 9782 EXPECT_EQ("float a = 5;\n" 9783 "int one = 1;\n" 9784 "\n" 9785 "unsigned oneTwoThree = 123;", 9786 format("float a = 5;\n" 9787 "int one = 1;\n" 9788 "\n" 9789 "unsigned oneTwoThree = 123;", 9790 Alignment)); 9791 EXPECT_EQ("float a = 5;\n" 9792 "int one = 1;\n" 9793 "\n" 9794 "unsigned oneTwoThree = 123;\n" 9795 "int oneTwo = 12;", 9796 format("float a = 5;\n" 9797 "int one = 1;\n" 9798 "\n" 9799 "unsigned oneTwoThree = 123;\n" 9800 "int oneTwo = 12;", 9801 Alignment)); 9802 // Function prototype alignment 9803 verifyFormat("int a();\n" 9804 "double b();", 9805 Alignment); 9806 verifyFormat("int a(int x);\n" 9807 "double b();", 9808 Alignment); 9809 unsigned OldColumnLimit = Alignment.ColumnLimit; 9810 // We need to set ColumnLimit to zero, in order to stress nested alignments, 9811 // otherwise the function parameters will be re-flowed onto a single line. 9812 Alignment.ColumnLimit = 0; 9813 EXPECT_EQ("int a(int x,\n" 9814 " float y);\n" 9815 "double b(int x,\n" 9816 " double y);", 9817 format("int a(int x,\n" 9818 " float y);\n" 9819 "double b(int x,\n" 9820 " double y);", 9821 Alignment)); 9822 // This ensures that function parameters of function declarations are 9823 // correctly indented when their owning functions are indented. 9824 // The failure case here is for 'double y' to not be indented enough. 9825 EXPECT_EQ("double a(int x);\n" 9826 "int b(int y,\n" 9827 " double z);", 9828 format("double a(int x);\n" 9829 "int b(int y,\n" 9830 " double z);", 9831 Alignment)); 9832 // Set ColumnLimit low so that we induce wrapping immediately after 9833 // the function name and opening paren. 9834 Alignment.ColumnLimit = 13; 9835 verifyFormat("int function(\n" 9836 " int x,\n" 9837 " bool y);", 9838 Alignment); 9839 Alignment.ColumnLimit = OldColumnLimit; 9840 // Ensure function pointers don't screw up recursive alignment 9841 verifyFormat("int a(int x, void (*fp)(int y));\n" 9842 "double b();", 9843 Alignment); 9844 Alignment.AlignConsecutiveAssignments = true; 9845 // Ensure recursive alignment is broken by function braces, so that the 9846 // "a = 1" does not align with subsequent assignments inside the function 9847 // body. 9848 verifyFormat("int func(int a = 1) {\n" 9849 " int b = 2;\n" 9850 " int cc = 3;\n" 9851 "}", 9852 Alignment); 9853 verifyFormat("float something = 2000;\n" 9854 "double another = 911;\n" 9855 "int i = 1, j = 10;\n" 9856 "const int *oneMore = 1;\n" 9857 "unsigned i = 2;", 9858 Alignment); 9859 verifyFormat("int oneTwoThree = {0}; // comment\n" 9860 "unsigned oneTwo = 0; // comment", 9861 Alignment); 9862 // Make sure that scope is correctly tracked, in the absence of braces 9863 verifyFormat("for (int i = 0; i < n; i++)\n" 9864 " j = i;\n" 9865 "double x = 1;\n", 9866 Alignment); 9867 verifyFormat("if (int i = 0)\n" 9868 " j = i;\n" 9869 "double x = 1;\n", 9870 Alignment); 9871 // Ensure operator[] and operator() are comprehended 9872 verifyFormat("struct test {\n" 9873 " long long int foo();\n" 9874 " int operator[](int a);\n" 9875 " double bar();\n" 9876 "};\n", 9877 Alignment); 9878 verifyFormat("struct test {\n" 9879 " long long int foo();\n" 9880 " int operator()(int a);\n" 9881 " double bar();\n" 9882 "};\n", 9883 Alignment); 9884 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9885 " int const i = 1;\n" 9886 " int * j = 2;\n" 9887 " int big = 10000;\n" 9888 "\n" 9889 " unsigned oneTwoThree = 123;\n" 9890 " int oneTwo = 12;\n" 9891 " method();\n" 9892 " float k = 2;\n" 9893 " int ll = 10000;\n" 9894 "}", 9895 format("void SomeFunction(int parameter= 0) {\n" 9896 " int const i= 1;\n" 9897 " int *j=2;\n" 9898 " int big = 10000;\n" 9899 "\n" 9900 "unsigned oneTwoThree =123;\n" 9901 "int oneTwo = 12;\n" 9902 " method();\n" 9903 "float k= 2;\n" 9904 "int ll=10000;\n" 9905 "}", 9906 Alignment)); 9907 Alignment.AlignConsecutiveAssignments = false; 9908 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9909 verifyFormat("#define A \\\n" 9910 " int aaaa = 12; \\\n" 9911 " float b = 23; \\\n" 9912 " const int ccc = 234; \\\n" 9913 " unsigned dddddddddd = 2345;", 9914 Alignment); 9915 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9916 verifyFormat("#define A \\\n" 9917 " int aaaa = 12; \\\n" 9918 " float b = 23; \\\n" 9919 " const int ccc = 234; \\\n" 9920 " unsigned dddddddddd = 2345;", 9921 Alignment); 9922 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9923 Alignment.ColumnLimit = 30; 9924 verifyFormat("#define A \\\n" 9925 " int aaaa = 12; \\\n" 9926 " float b = 23; \\\n" 9927 " const int ccc = 234; \\\n" 9928 " int dddddddddd = 2345;", 9929 Alignment); 9930 Alignment.ColumnLimit = 80; 9931 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9932 "k = 4, int l = 5,\n" 9933 " int m = 6) {\n" 9934 " const int j = 10;\n" 9935 " otherThing = 1;\n" 9936 "}", 9937 Alignment); 9938 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9939 " int const i = 1;\n" 9940 " int * j = 2;\n" 9941 " int big = 10000;\n" 9942 "}", 9943 Alignment); 9944 verifyFormat("class C {\n" 9945 "public:\n" 9946 " int i = 1;\n" 9947 " virtual void f() = 0;\n" 9948 "};", 9949 Alignment); 9950 verifyFormat("float i = 1;\n" 9951 "if (SomeType t = getSomething()) {\n" 9952 "}\n" 9953 "const unsigned j = 2;\n" 9954 "int big = 10000;", 9955 Alignment); 9956 verifyFormat("float j = 7;\n" 9957 "for (int k = 0; k < N; ++k) {\n" 9958 "}\n" 9959 "unsigned j = 2;\n" 9960 "int big = 10000;\n" 9961 "}", 9962 Alignment); 9963 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9964 verifyFormat("float i = 1;\n" 9965 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9966 " = someLooooooooooooooooongFunction();\n" 9967 "int j = 2;", 9968 Alignment); 9969 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9970 verifyFormat("int i = 1;\n" 9971 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9972 " someLooooooooooooooooongFunction();\n" 9973 "int j = 2;", 9974 Alignment); 9975 9976 Alignment.AlignConsecutiveAssignments = true; 9977 verifyFormat("auto lambda = []() {\n" 9978 " auto ii = 0;\n" 9979 " float j = 0;\n" 9980 " return 0;\n" 9981 "};\n" 9982 "int i = 0;\n" 9983 "float i2 = 0;\n" 9984 "auto v = type{\n" 9985 " i = 1, //\n" 9986 " (i = 2), //\n" 9987 " i = 3 //\n" 9988 "};", 9989 Alignment); 9990 Alignment.AlignConsecutiveAssignments = false; 9991 9992 verifyFormat( 9993 "int i = 1;\n" 9994 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9995 " loooooooooooooooooooooongParameterB);\n" 9996 "int j = 2;", 9997 Alignment); 9998 9999 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 10000 // We expect declarations and assignments to align, as long as it doesn't 10001 // exceed the column limit, starting a new alignment sequence whenever it 10002 // happens. 10003 Alignment.AlignConsecutiveAssignments = true; 10004 Alignment.ColumnLimit = 30; 10005 verifyFormat("float ii = 1;\n" 10006 "unsigned j = 2;\n" 10007 "int someVerylongVariable = 1;\n" 10008 "AnotherLongType ll = 123456;\n" 10009 "VeryVeryLongType k = 2;\n" 10010 "int myvar = 1;", 10011 Alignment); 10012 Alignment.ColumnLimit = 80; 10013 Alignment.AlignConsecutiveAssignments = false; 10014 10015 verifyFormat( 10016 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 10017 " typename LongType, typename B>\n" 10018 "auto foo() {}\n", 10019 Alignment); 10020 verifyFormat("float a, b = 1;\n" 10021 "int c = 2;\n" 10022 "int dd = 3;\n", 10023 Alignment); 10024 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 10025 "float b[1][] = {{3.f}};\n", 10026 Alignment); 10027 Alignment.AlignConsecutiveAssignments = true; 10028 verifyFormat("float a, b = 1;\n" 10029 "int c = 2;\n" 10030 "int dd = 3;\n", 10031 Alignment); 10032 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 10033 "float b[1][] = {{3.f}};\n", 10034 Alignment); 10035 Alignment.AlignConsecutiveAssignments = false; 10036 10037 Alignment.ColumnLimit = 30; 10038 Alignment.BinPackParameters = false; 10039 verifyFormat("void foo(float a,\n" 10040 " float b,\n" 10041 " int c,\n" 10042 " uint32_t *d) {\n" 10043 " int * e = 0;\n" 10044 " float f = 0;\n" 10045 " double g = 0;\n" 10046 "}\n" 10047 "void bar(ino_t a,\n" 10048 " int b,\n" 10049 " uint32_t *c,\n" 10050 " bool d) {}\n", 10051 Alignment); 10052 Alignment.BinPackParameters = true; 10053 Alignment.ColumnLimit = 80; 10054 10055 // Bug 33507 10056 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 10057 verifyFormat( 10058 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 10059 " static const Version verVs2017;\n" 10060 " return true;\n" 10061 "});\n", 10062 Alignment); 10063 Alignment.PointerAlignment = FormatStyle::PAS_Right; 10064 10065 // See llvm.org/PR35641 10066 Alignment.AlignConsecutiveDeclarations = true; 10067 verifyFormat("int func() { //\n" 10068 " int b;\n" 10069 " unsigned c;\n" 10070 "}", 10071 Alignment); 10072 } 10073 10074 TEST_F(FormatTest, LinuxBraceBreaking) { 10075 FormatStyle LinuxBraceStyle = getLLVMStyle(); 10076 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 10077 verifyFormat("namespace a\n" 10078 "{\n" 10079 "class A\n" 10080 "{\n" 10081 " void f()\n" 10082 " {\n" 10083 " if (true) {\n" 10084 " a();\n" 10085 " b();\n" 10086 " } else {\n" 10087 " a();\n" 10088 " }\n" 10089 " }\n" 10090 " void g() { return; }\n" 10091 "};\n" 10092 "struct B {\n" 10093 " int x;\n" 10094 "};\n" 10095 "} // namespace a\n", 10096 LinuxBraceStyle); 10097 verifyFormat("enum X {\n" 10098 " Y = 0,\n" 10099 "}\n", 10100 LinuxBraceStyle); 10101 verifyFormat("struct S {\n" 10102 " int Type;\n" 10103 " union {\n" 10104 " int x;\n" 10105 " double y;\n" 10106 " } Value;\n" 10107 " class C\n" 10108 " {\n" 10109 " MyFavoriteType Value;\n" 10110 " } Class;\n" 10111 "}\n", 10112 LinuxBraceStyle); 10113 } 10114 10115 TEST_F(FormatTest, MozillaBraceBreaking) { 10116 FormatStyle MozillaBraceStyle = getLLVMStyle(); 10117 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 10118 MozillaBraceStyle.FixNamespaceComments = false; 10119 verifyFormat("namespace a {\n" 10120 "class A\n" 10121 "{\n" 10122 " void f()\n" 10123 " {\n" 10124 " if (true) {\n" 10125 " a();\n" 10126 " b();\n" 10127 " }\n" 10128 " }\n" 10129 " void g() { return; }\n" 10130 "};\n" 10131 "enum E\n" 10132 "{\n" 10133 " A,\n" 10134 " // foo\n" 10135 " B,\n" 10136 " C\n" 10137 "};\n" 10138 "struct B\n" 10139 "{\n" 10140 " int x;\n" 10141 "};\n" 10142 "}\n", 10143 MozillaBraceStyle); 10144 verifyFormat("struct S\n" 10145 "{\n" 10146 " int Type;\n" 10147 " union\n" 10148 " {\n" 10149 " int x;\n" 10150 " double y;\n" 10151 " } Value;\n" 10152 " class C\n" 10153 " {\n" 10154 " MyFavoriteType Value;\n" 10155 " } Class;\n" 10156 "}\n", 10157 MozillaBraceStyle); 10158 } 10159 10160 TEST_F(FormatTest, StroustrupBraceBreaking) { 10161 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 10162 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10163 verifyFormat("namespace a {\n" 10164 "class A {\n" 10165 " void f()\n" 10166 " {\n" 10167 " if (true) {\n" 10168 " a();\n" 10169 " b();\n" 10170 " }\n" 10171 " }\n" 10172 " void g() { return; }\n" 10173 "};\n" 10174 "struct B {\n" 10175 " int x;\n" 10176 "};\n" 10177 "} // namespace a\n", 10178 StroustrupBraceStyle); 10179 10180 verifyFormat("void foo()\n" 10181 "{\n" 10182 " if (a) {\n" 10183 " a();\n" 10184 " }\n" 10185 " else {\n" 10186 " b();\n" 10187 " }\n" 10188 "}\n", 10189 StroustrupBraceStyle); 10190 10191 verifyFormat("#ifdef _DEBUG\n" 10192 "int foo(int i = 0)\n" 10193 "#else\n" 10194 "int foo(int i = 5)\n" 10195 "#endif\n" 10196 "{\n" 10197 " return i;\n" 10198 "}", 10199 StroustrupBraceStyle); 10200 10201 verifyFormat("void foo() {}\n" 10202 "void bar()\n" 10203 "#ifdef _DEBUG\n" 10204 "{\n" 10205 " foo();\n" 10206 "}\n" 10207 "#else\n" 10208 "{\n" 10209 "}\n" 10210 "#endif", 10211 StroustrupBraceStyle); 10212 10213 verifyFormat("void foobar() { int i = 5; }\n" 10214 "#ifdef _DEBUG\n" 10215 "void bar() {}\n" 10216 "#else\n" 10217 "void bar() { foobar(); }\n" 10218 "#endif", 10219 StroustrupBraceStyle); 10220 } 10221 10222 TEST_F(FormatTest, AllmanBraceBreaking) { 10223 FormatStyle AllmanBraceStyle = getLLVMStyle(); 10224 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 10225 10226 EXPECT_EQ("namespace a\n" 10227 "{\n" 10228 "void f();\n" 10229 "void g();\n" 10230 "} // namespace a\n", 10231 format("namespace a\n" 10232 "{\n" 10233 "void f();\n" 10234 "void g();\n" 10235 "}\n", 10236 AllmanBraceStyle)); 10237 10238 verifyFormat("namespace a\n" 10239 "{\n" 10240 "class A\n" 10241 "{\n" 10242 " void f()\n" 10243 " {\n" 10244 " if (true)\n" 10245 " {\n" 10246 " a();\n" 10247 " b();\n" 10248 " }\n" 10249 " }\n" 10250 " void g() { return; }\n" 10251 "};\n" 10252 "struct B\n" 10253 "{\n" 10254 " int x;\n" 10255 "};\n" 10256 "} // namespace a", 10257 AllmanBraceStyle); 10258 10259 verifyFormat("void f()\n" 10260 "{\n" 10261 " if (true)\n" 10262 " {\n" 10263 " a();\n" 10264 " }\n" 10265 " else if (false)\n" 10266 " {\n" 10267 " b();\n" 10268 " }\n" 10269 " else\n" 10270 " {\n" 10271 " c();\n" 10272 " }\n" 10273 "}\n", 10274 AllmanBraceStyle); 10275 10276 verifyFormat("void f()\n" 10277 "{\n" 10278 " for (int i = 0; i < 10; ++i)\n" 10279 " {\n" 10280 " a();\n" 10281 " }\n" 10282 " while (false)\n" 10283 " {\n" 10284 " b();\n" 10285 " }\n" 10286 " do\n" 10287 " {\n" 10288 " c();\n" 10289 " } while (false)\n" 10290 "}\n", 10291 AllmanBraceStyle); 10292 10293 verifyFormat("void f(int a)\n" 10294 "{\n" 10295 " switch (a)\n" 10296 " {\n" 10297 " case 0:\n" 10298 " break;\n" 10299 " case 1:\n" 10300 " {\n" 10301 " break;\n" 10302 " }\n" 10303 " case 2:\n" 10304 " {\n" 10305 " }\n" 10306 " break;\n" 10307 " default:\n" 10308 " break;\n" 10309 " }\n" 10310 "}\n", 10311 AllmanBraceStyle); 10312 10313 verifyFormat("enum X\n" 10314 "{\n" 10315 " Y = 0,\n" 10316 "}\n", 10317 AllmanBraceStyle); 10318 verifyFormat("enum X\n" 10319 "{\n" 10320 " Y = 0\n" 10321 "}\n", 10322 AllmanBraceStyle); 10323 10324 verifyFormat("@interface BSApplicationController ()\n" 10325 "{\n" 10326 "@private\n" 10327 " id _extraIvar;\n" 10328 "}\n" 10329 "@end\n", 10330 AllmanBraceStyle); 10331 10332 verifyFormat("#ifdef _DEBUG\n" 10333 "int foo(int i = 0)\n" 10334 "#else\n" 10335 "int foo(int i = 5)\n" 10336 "#endif\n" 10337 "{\n" 10338 " return i;\n" 10339 "}", 10340 AllmanBraceStyle); 10341 10342 verifyFormat("void foo() {}\n" 10343 "void bar()\n" 10344 "#ifdef _DEBUG\n" 10345 "{\n" 10346 " foo();\n" 10347 "}\n" 10348 "#else\n" 10349 "{\n" 10350 "}\n" 10351 "#endif", 10352 AllmanBraceStyle); 10353 10354 verifyFormat("void foobar() { int i = 5; }\n" 10355 "#ifdef _DEBUG\n" 10356 "void bar() {}\n" 10357 "#else\n" 10358 "void bar() { foobar(); }\n" 10359 "#endif", 10360 AllmanBraceStyle); 10361 10362 // This shouldn't affect ObjC blocks.. 10363 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 10364 " // ...\n" 10365 " int i;\n" 10366 "}];", 10367 AllmanBraceStyle); 10368 verifyFormat("void (^block)(void) = ^{\n" 10369 " // ...\n" 10370 " int i;\n" 10371 "};", 10372 AllmanBraceStyle); 10373 // .. or dict literals. 10374 verifyFormat("void f()\n" 10375 "{\n" 10376 " // ...\n" 10377 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 10378 "}", 10379 AllmanBraceStyle); 10380 verifyFormat("void f()\n" 10381 "{\n" 10382 " // ...\n" 10383 " [object someMethod:@{a : @\"b\"}];\n" 10384 "}", 10385 AllmanBraceStyle); 10386 verifyFormat("int f()\n" 10387 "{ // comment\n" 10388 " return 42;\n" 10389 "}", 10390 AllmanBraceStyle); 10391 10392 AllmanBraceStyle.ColumnLimit = 19; 10393 verifyFormat("void f() { int i; }", AllmanBraceStyle); 10394 AllmanBraceStyle.ColumnLimit = 18; 10395 verifyFormat("void f()\n" 10396 "{\n" 10397 " int i;\n" 10398 "}", 10399 AllmanBraceStyle); 10400 AllmanBraceStyle.ColumnLimit = 80; 10401 10402 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 10403 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 10404 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 10405 verifyFormat("void f(bool b)\n" 10406 "{\n" 10407 " if (b)\n" 10408 " {\n" 10409 " return;\n" 10410 " }\n" 10411 "}\n", 10412 BreakBeforeBraceShortIfs); 10413 verifyFormat("void f(bool b)\n" 10414 "{\n" 10415 " if constexpr (b)\n" 10416 " {\n" 10417 " return;\n" 10418 " }\n" 10419 "}\n", 10420 BreakBeforeBraceShortIfs); 10421 verifyFormat("void f(bool b)\n" 10422 "{\n" 10423 " if (b) return;\n" 10424 "}\n", 10425 BreakBeforeBraceShortIfs); 10426 verifyFormat("void f(bool b)\n" 10427 "{\n" 10428 " if constexpr (b) return;\n" 10429 "}\n", 10430 BreakBeforeBraceShortIfs); 10431 verifyFormat("void f(bool b)\n" 10432 "{\n" 10433 " while (b)\n" 10434 " {\n" 10435 " return;\n" 10436 " }\n" 10437 "}\n", 10438 BreakBeforeBraceShortIfs); 10439 } 10440 10441 TEST_F(FormatTest, GNUBraceBreaking) { 10442 FormatStyle GNUBraceStyle = getLLVMStyle(); 10443 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 10444 verifyFormat("namespace a\n" 10445 "{\n" 10446 "class A\n" 10447 "{\n" 10448 " void f()\n" 10449 " {\n" 10450 " int a;\n" 10451 " {\n" 10452 " int b;\n" 10453 " }\n" 10454 " if (true)\n" 10455 " {\n" 10456 " a();\n" 10457 " b();\n" 10458 " }\n" 10459 " }\n" 10460 " void g() { return; }\n" 10461 "}\n" 10462 "} // namespace a", 10463 GNUBraceStyle); 10464 10465 verifyFormat("void f()\n" 10466 "{\n" 10467 " if (true)\n" 10468 " {\n" 10469 " a();\n" 10470 " }\n" 10471 " else if (false)\n" 10472 " {\n" 10473 " b();\n" 10474 " }\n" 10475 " else\n" 10476 " {\n" 10477 " c();\n" 10478 " }\n" 10479 "}\n", 10480 GNUBraceStyle); 10481 10482 verifyFormat("void f()\n" 10483 "{\n" 10484 " for (int i = 0; i < 10; ++i)\n" 10485 " {\n" 10486 " a();\n" 10487 " }\n" 10488 " while (false)\n" 10489 " {\n" 10490 " b();\n" 10491 " }\n" 10492 " do\n" 10493 " {\n" 10494 " c();\n" 10495 " }\n" 10496 " while (false);\n" 10497 "}\n", 10498 GNUBraceStyle); 10499 10500 verifyFormat("void f(int a)\n" 10501 "{\n" 10502 " switch (a)\n" 10503 " {\n" 10504 " case 0:\n" 10505 " break;\n" 10506 " case 1:\n" 10507 " {\n" 10508 " break;\n" 10509 " }\n" 10510 " case 2:\n" 10511 " {\n" 10512 " }\n" 10513 " break;\n" 10514 " default:\n" 10515 " break;\n" 10516 " }\n" 10517 "}\n", 10518 GNUBraceStyle); 10519 10520 verifyFormat("enum X\n" 10521 "{\n" 10522 " Y = 0,\n" 10523 "}\n", 10524 GNUBraceStyle); 10525 10526 verifyFormat("@interface BSApplicationController ()\n" 10527 "{\n" 10528 "@private\n" 10529 " id _extraIvar;\n" 10530 "}\n" 10531 "@end\n", 10532 GNUBraceStyle); 10533 10534 verifyFormat("#ifdef _DEBUG\n" 10535 "int foo(int i = 0)\n" 10536 "#else\n" 10537 "int foo(int i = 5)\n" 10538 "#endif\n" 10539 "{\n" 10540 " return i;\n" 10541 "}", 10542 GNUBraceStyle); 10543 10544 verifyFormat("void foo() {}\n" 10545 "void bar()\n" 10546 "#ifdef _DEBUG\n" 10547 "{\n" 10548 " foo();\n" 10549 "}\n" 10550 "#else\n" 10551 "{\n" 10552 "}\n" 10553 "#endif", 10554 GNUBraceStyle); 10555 10556 verifyFormat("void foobar() { int i = 5; }\n" 10557 "#ifdef _DEBUG\n" 10558 "void bar() {}\n" 10559 "#else\n" 10560 "void bar() { foobar(); }\n" 10561 "#endif", 10562 GNUBraceStyle); 10563 } 10564 10565 TEST_F(FormatTest, WebKitBraceBreaking) { 10566 FormatStyle WebKitBraceStyle = getLLVMStyle(); 10567 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 10568 WebKitBraceStyle.FixNamespaceComments = false; 10569 verifyFormat("namespace a {\n" 10570 "class A {\n" 10571 " void f()\n" 10572 " {\n" 10573 " if (true) {\n" 10574 " a();\n" 10575 " b();\n" 10576 " }\n" 10577 " }\n" 10578 " void g() { return; }\n" 10579 "};\n" 10580 "enum E {\n" 10581 " A,\n" 10582 " // foo\n" 10583 " B,\n" 10584 " C\n" 10585 "};\n" 10586 "struct B {\n" 10587 " int x;\n" 10588 "};\n" 10589 "}\n", 10590 WebKitBraceStyle); 10591 verifyFormat("struct S {\n" 10592 " int Type;\n" 10593 " union {\n" 10594 " int x;\n" 10595 " double y;\n" 10596 " } Value;\n" 10597 " class C {\n" 10598 " MyFavoriteType Value;\n" 10599 " } Class;\n" 10600 "};\n", 10601 WebKitBraceStyle); 10602 } 10603 10604 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 10605 verifyFormat("void f() {\n" 10606 " try {\n" 10607 " } catch (const Exception &e) {\n" 10608 " }\n" 10609 "}\n", 10610 getLLVMStyle()); 10611 } 10612 10613 TEST_F(FormatTest, UnderstandsPragmas) { 10614 verifyFormat("#pragma omp reduction(| : var)"); 10615 verifyFormat("#pragma omp reduction(+ : var)"); 10616 10617 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 10618 "(including parentheses).", 10619 format("#pragma mark Any non-hyphenated or hyphenated string " 10620 "(including parentheses).")); 10621 } 10622 10623 TEST_F(FormatTest, UnderstandPragmaOption) { 10624 verifyFormat("#pragma option -C -A"); 10625 10626 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 10627 } 10628 10629 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 10630 FormatStyle Style = getLLVMStyle(); 10631 Style.ColumnLimit = 20; 10632 10633 verifyFormat("int a; // the\n" 10634 " // comment", Style); 10635 EXPECT_EQ("int a; /* first line\n" 10636 " * second\n" 10637 " * line third\n" 10638 " * line\n" 10639 " */", 10640 format("int a; /* first line\n" 10641 " * second\n" 10642 " * line third\n" 10643 " * line\n" 10644 " */", 10645 Style)); 10646 EXPECT_EQ("int a; // first line\n" 10647 " // second\n" 10648 " // line third\n" 10649 " // line", 10650 format("int a; // first line\n" 10651 " // second line\n" 10652 " // third line", 10653 Style)); 10654 10655 Style.PenaltyExcessCharacter = 90; 10656 verifyFormat("int a; // the comment", Style); 10657 EXPECT_EQ("int a; // the comment\n" 10658 " // aaa", 10659 format("int a; // the comment aaa", Style)); 10660 EXPECT_EQ("int a; /* first line\n" 10661 " * second line\n" 10662 " * third line\n" 10663 " */", 10664 format("int a; /* first line\n" 10665 " * second line\n" 10666 " * third line\n" 10667 " */", 10668 Style)); 10669 EXPECT_EQ("int a; // first line\n" 10670 " // second line\n" 10671 " // third line", 10672 format("int a; // first line\n" 10673 " // second line\n" 10674 " // third line", 10675 Style)); 10676 // FIXME: Investigate why this is not getting the same layout as the test 10677 // above. 10678 EXPECT_EQ("int a; /* first line\n" 10679 " * second line\n" 10680 " * third line\n" 10681 " */", 10682 format("int a; /* first line second line third line" 10683 "\n*/", 10684 Style)); 10685 10686 EXPECT_EQ("// foo bar baz bazfoo\n" 10687 "// foo bar foo bar\n", 10688 format("// foo bar baz bazfoo\n" 10689 "// foo bar foo bar\n", 10690 Style)); 10691 EXPECT_EQ("// foo bar baz bazfoo\n" 10692 "// foo bar foo bar\n", 10693 format("// foo bar baz bazfoo\n" 10694 "// foo bar foo bar\n", 10695 Style)); 10696 10697 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 10698 // next one. 10699 EXPECT_EQ("// foo bar baz bazfoo\n" 10700 "// bar foo bar\n", 10701 format("// foo bar baz bazfoo bar\n" 10702 "// foo bar\n", 10703 Style)); 10704 10705 EXPECT_EQ("// foo bar baz bazfoo\n" 10706 "// foo bar baz bazfoo\n" 10707 "// bar foo bar\n", 10708 format("// foo bar baz bazfoo\n" 10709 "// foo bar baz bazfoo bar\n" 10710 "// foo bar\n", 10711 Style)); 10712 10713 EXPECT_EQ("// foo bar baz bazfoo\n" 10714 "// foo bar baz bazfoo\n" 10715 "// bar foo bar\n", 10716 format("// foo bar baz bazfoo\n" 10717 "// foo bar baz bazfoo bar\n" 10718 "// foo bar\n", 10719 Style)); 10720 10721 // Make sure we do not keep protruding characters if strict mode reflow is 10722 // cheaper than keeping protruding characters. 10723 Style.ColumnLimit = 21; 10724 EXPECT_EQ("// foo foo foo foo\n" 10725 "// foo foo foo foo\n" 10726 "// foo foo foo foo\n", 10727 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", 10728 Style)); 10729 10730 EXPECT_EQ("int a = /* long block\n" 10731 " comment */\n" 10732 " 42;", 10733 format("int a = /* long block comment */ 42;", Style)); 10734 } 10735 10736 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 10737 for (size_t i = 1; i < Styles.size(); ++i) \ 10738 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 10739 << " differs from Style #0" 10740 10741 TEST_F(FormatTest, GetsPredefinedStyleByName) { 10742 SmallVector<FormatStyle, 3> Styles; 10743 Styles.resize(3); 10744 10745 Styles[0] = getLLVMStyle(); 10746 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 10747 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 10748 EXPECT_ALL_STYLES_EQUAL(Styles); 10749 10750 Styles[0] = getGoogleStyle(); 10751 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 10752 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 10753 EXPECT_ALL_STYLES_EQUAL(Styles); 10754 10755 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10756 EXPECT_TRUE( 10757 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 10758 EXPECT_TRUE( 10759 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 10760 EXPECT_ALL_STYLES_EQUAL(Styles); 10761 10762 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 10763 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 10764 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 10765 EXPECT_ALL_STYLES_EQUAL(Styles); 10766 10767 Styles[0] = getMozillaStyle(); 10768 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 10769 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 10770 EXPECT_ALL_STYLES_EQUAL(Styles); 10771 10772 Styles[0] = getWebKitStyle(); 10773 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 10774 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 10775 EXPECT_ALL_STYLES_EQUAL(Styles); 10776 10777 Styles[0] = getGNUStyle(); 10778 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 10779 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 10780 EXPECT_ALL_STYLES_EQUAL(Styles); 10781 10782 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 10783 } 10784 10785 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 10786 SmallVector<FormatStyle, 8> Styles; 10787 Styles.resize(2); 10788 10789 Styles[0] = getGoogleStyle(); 10790 Styles[1] = getLLVMStyle(); 10791 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10792 EXPECT_ALL_STYLES_EQUAL(Styles); 10793 10794 Styles.resize(5); 10795 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10796 Styles[1] = getLLVMStyle(); 10797 Styles[1].Language = FormatStyle::LK_JavaScript; 10798 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10799 10800 Styles[2] = getLLVMStyle(); 10801 Styles[2].Language = FormatStyle::LK_JavaScript; 10802 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 10803 "BasedOnStyle: Google", 10804 &Styles[2]) 10805 .value()); 10806 10807 Styles[3] = getLLVMStyle(); 10808 Styles[3].Language = FormatStyle::LK_JavaScript; 10809 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 10810 "Language: JavaScript", 10811 &Styles[3]) 10812 .value()); 10813 10814 Styles[4] = getLLVMStyle(); 10815 Styles[4].Language = FormatStyle::LK_JavaScript; 10816 EXPECT_EQ(0, parseConfiguration("---\n" 10817 "BasedOnStyle: LLVM\n" 10818 "IndentWidth: 123\n" 10819 "---\n" 10820 "BasedOnStyle: Google\n" 10821 "Language: JavaScript", 10822 &Styles[4]) 10823 .value()); 10824 EXPECT_ALL_STYLES_EQUAL(Styles); 10825 } 10826 10827 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 10828 Style.FIELD = false; \ 10829 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 10830 EXPECT_TRUE(Style.FIELD); \ 10831 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 10832 EXPECT_FALSE(Style.FIELD); 10833 10834 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 10835 10836 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 10837 Style.STRUCT.FIELD = false; \ 10838 EXPECT_EQ(0, \ 10839 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 10840 .value()); \ 10841 EXPECT_TRUE(Style.STRUCT.FIELD); \ 10842 EXPECT_EQ(0, \ 10843 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 10844 .value()); \ 10845 EXPECT_FALSE(Style.STRUCT.FIELD); 10846 10847 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 10848 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 10849 10850 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 10851 EXPECT_NE(VALUE, Style.FIELD); \ 10852 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 10853 EXPECT_EQ(VALUE, Style.FIELD) 10854 10855 TEST_F(FormatTest, ParsesConfigurationBools) { 10856 FormatStyle Style = {}; 10857 Style.Language = FormatStyle::LK_Cpp; 10858 CHECK_PARSE_BOOL(AlignOperands); 10859 CHECK_PARSE_BOOL(AlignTrailingComments); 10860 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 10861 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 10862 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 10863 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 10864 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 10865 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 10866 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 10867 CHECK_PARSE_BOOL(BinPackArguments); 10868 CHECK_PARSE_BOOL(BinPackParameters); 10869 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 10870 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 10871 CHECK_PARSE_BOOL(BreakStringLiterals); 10872 CHECK_PARSE_BOOL(CompactNamespaces); 10873 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 10874 CHECK_PARSE_BOOL(DerivePointerAlignment); 10875 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 10876 CHECK_PARSE_BOOL(DisableFormat); 10877 CHECK_PARSE_BOOL(IndentCaseLabels); 10878 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 10879 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 10880 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 10881 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 10882 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 10883 CHECK_PARSE_BOOL(ReflowComments); 10884 CHECK_PARSE_BOOL(SortIncludes); 10885 CHECK_PARSE_BOOL(SortUsingDeclarations); 10886 CHECK_PARSE_BOOL(SpacesInParentheses); 10887 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 10888 CHECK_PARSE_BOOL(SpacesInAngles); 10889 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 10890 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 10891 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 10892 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 10893 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 10894 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 10895 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 10896 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 10897 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 10898 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 10899 10900 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 10901 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 10902 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 10903 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 10904 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 10905 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 10906 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 10907 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 10908 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 10909 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 10910 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 10911 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 10912 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 10913 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 10914 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 10915 } 10916 10917 #undef CHECK_PARSE_BOOL 10918 10919 TEST_F(FormatTest, ParsesConfiguration) { 10920 FormatStyle Style = {}; 10921 Style.Language = FormatStyle::LK_Cpp; 10922 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 10923 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 10924 ConstructorInitializerIndentWidth, 1234u); 10925 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 10926 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 10927 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 10928 CHECK_PARSE("PenaltyBreakAssignment: 1234", 10929 PenaltyBreakAssignment, 1234u); 10930 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 10931 PenaltyBreakBeforeFirstCallParameter, 1234u); 10932 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 10933 PenaltyBreakTemplateDeclaration, 1234u); 10934 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 10935 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 10936 PenaltyReturnTypeOnItsOwnLine, 1234u); 10937 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 10938 SpacesBeforeTrailingComments, 1234u); 10939 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 10940 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 10941 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 10942 10943 Style.PointerAlignment = FormatStyle::PAS_Middle; 10944 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 10945 FormatStyle::PAS_Left); 10946 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 10947 FormatStyle::PAS_Right); 10948 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 10949 FormatStyle::PAS_Middle); 10950 // For backward compatibility: 10951 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 10952 FormatStyle::PAS_Left); 10953 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 10954 FormatStyle::PAS_Right); 10955 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 10956 FormatStyle::PAS_Middle); 10957 10958 Style.Standard = FormatStyle::LS_Auto; 10959 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 10960 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 10961 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 10962 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 10963 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 10964 10965 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10966 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 10967 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 10968 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 10969 FormatStyle::BOS_None); 10970 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 10971 FormatStyle::BOS_All); 10972 // For backward compatibility: 10973 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 10974 FormatStyle::BOS_None); 10975 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 10976 FormatStyle::BOS_All); 10977 10978 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 10979 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 10980 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10981 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 10982 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 10983 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 10984 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 10985 // For backward compatibility: 10986 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 10987 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10988 10989 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 10990 CHECK_PARSE("BreakInheritanceList: BeforeComma", 10991 BreakInheritanceList, FormatStyle::BILS_BeforeComma); 10992 CHECK_PARSE("BreakInheritanceList: AfterColon", 10993 BreakInheritanceList, FormatStyle::BILS_AfterColon); 10994 CHECK_PARSE("BreakInheritanceList: BeforeColon", 10995 BreakInheritanceList, FormatStyle::BILS_BeforeColon); 10996 // For backward compatibility: 10997 CHECK_PARSE("BreakBeforeInheritanceComma: true", 10998 BreakInheritanceList, FormatStyle::BILS_BeforeComma); 10999 11000 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11001 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 11002 FormatStyle::BAS_Align); 11003 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 11004 FormatStyle::BAS_DontAlign); 11005 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 11006 FormatStyle::BAS_AlwaysBreak); 11007 // For backward compatibility: 11008 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 11009 FormatStyle::BAS_DontAlign); 11010 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 11011 FormatStyle::BAS_Align); 11012 11013 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 11014 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 11015 FormatStyle::ENAS_DontAlign); 11016 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 11017 FormatStyle::ENAS_Left); 11018 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 11019 FormatStyle::ENAS_Right); 11020 // For backward compatibility: 11021 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 11022 FormatStyle::ENAS_Left); 11023 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 11024 FormatStyle::ENAS_Right); 11025 11026 Style.UseTab = FormatStyle::UT_ForIndentation; 11027 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 11028 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 11029 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 11030 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 11031 FormatStyle::UT_ForContinuationAndIndentation); 11032 // For backward compatibility: 11033 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 11034 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 11035 11036 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11037 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 11038 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 11039 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 11040 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 11041 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 11042 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 11043 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 11044 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 11045 // For backward compatibility: 11046 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 11047 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 11048 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 11049 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 11050 11051 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 11052 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 11053 FormatStyle::SBPO_Never); 11054 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 11055 FormatStyle::SBPO_Always); 11056 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 11057 FormatStyle::SBPO_ControlStatements); 11058 // For backward compatibility: 11059 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 11060 FormatStyle::SBPO_Never); 11061 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 11062 FormatStyle::SBPO_ControlStatements); 11063 11064 Style.ColumnLimit = 123; 11065 FormatStyle BaseStyle = getLLVMStyle(); 11066 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 11067 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 11068 11069 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 11070 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 11071 FormatStyle::BS_Attach); 11072 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 11073 FormatStyle::BS_Linux); 11074 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 11075 FormatStyle::BS_Mozilla); 11076 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 11077 FormatStyle::BS_Stroustrup); 11078 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 11079 FormatStyle::BS_Allman); 11080 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 11081 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 11082 FormatStyle::BS_WebKit); 11083 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 11084 FormatStyle::BS_Custom); 11085 11086 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 11087 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 11088 FormatStyle::RTBS_None); 11089 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 11090 FormatStyle::RTBS_All); 11091 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 11092 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 11093 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 11094 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 11095 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 11096 AlwaysBreakAfterReturnType, 11097 FormatStyle::RTBS_TopLevelDefinitions); 11098 11099 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 11100 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", AlwaysBreakTemplateDeclarations, 11101 FormatStyle::BTDS_No); 11102 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", AlwaysBreakTemplateDeclarations, 11103 FormatStyle::BTDS_MultiLine); 11104 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", AlwaysBreakTemplateDeclarations, 11105 FormatStyle::BTDS_Yes); 11106 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", AlwaysBreakTemplateDeclarations, 11107 FormatStyle::BTDS_MultiLine); 11108 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", AlwaysBreakTemplateDeclarations, 11109 FormatStyle::BTDS_Yes); 11110 11111 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 11112 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 11113 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 11114 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 11115 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 11116 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 11117 AlwaysBreakAfterDefinitionReturnType, 11118 FormatStyle::DRTBS_TopLevel); 11119 11120 Style.NamespaceIndentation = FormatStyle::NI_All; 11121 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 11122 FormatStyle::NI_None); 11123 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 11124 FormatStyle::NI_Inner); 11125 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 11126 FormatStyle::NI_All); 11127 11128 // FIXME: This is required because parsing a configuration simply overwrites 11129 // the first N elements of the list instead of resetting it. 11130 Style.ForEachMacros.clear(); 11131 std::vector<std::string> BoostForeach; 11132 BoostForeach.push_back("BOOST_FOREACH"); 11133 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 11134 std::vector<std::string> BoostAndQForeach; 11135 BoostAndQForeach.push_back("BOOST_FOREACH"); 11136 BoostAndQForeach.push_back("Q_FOREACH"); 11137 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 11138 BoostAndQForeach); 11139 11140 Style.StatementMacros.clear(); 11141 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 11142 std::vector<std::string>{"QUNUSED"}); 11143 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 11144 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 11145 11146 Style.IncludeStyle.IncludeCategories.clear(); 11147 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 11148 {"abc/.*", 2}, {".*", 1}}; 11149 CHECK_PARSE("IncludeCategories:\n" 11150 " - Regex: abc/.*\n" 11151 " Priority: 2\n" 11152 " - Regex: .*\n" 11153 " Priority: 1", 11154 IncludeStyle.IncludeCategories, ExpectedCategories); 11155 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 11156 "abc$"); 11157 11158 Style.RawStringFormats.clear(); 11159 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 11160 { 11161 FormatStyle::LK_TextProto, 11162 {"pb", "proto"}, 11163 {"PARSE_TEXT_PROTO"}, 11164 /*CanonicalDelimiter=*/"", 11165 "llvm", 11166 }, 11167 { 11168 FormatStyle::LK_Cpp, 11169 {"cc", "cpp"}, 11170 {"C_CODEBLOCK", "CPPEVAL"}, 11171 /*CanonicalDelimiter=*/"cc", 11172 /*BasedOnStyle=*/"", 11173 }, 11174 }; 11175 11176 CHECK_PARSE("RawStringFormats:\n" 11177 " - Language: TextProto\n" 11178 " Delimiters:\n" 11179 " - 'pb'\n" 11180 " - 'proto'\n" 11181 " EnclosingFunctions:\n" 11182 " - 'PARSE_TEXT_PROTO'\n" 11183 " BasedOnStyle: llvm\n" 11184 " - Language: Cpp\n" 11185 " Delimiters:\n" 11186 " - 'cc'\n" 11187 " - 'cpp'\n" 11188 " EnclosingFunctions:\n" 11189 " - 'C_CODEBLOCK'\n" 11190 " - 'CPPEVAL'\n" 11191 " CanonicalDelimiter: 'cc'", 11192 RawStringFormats, ExpectedRawStringFormats); 11193 } 11194 11195 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 11196 FormatStyle Style = {}; 11197 Style.Language = FormatStyle::LK_Cpp; 11198 CHECK_PARSE("Language: Cpp\n" 11199 "IndentWidth: 12", 11200 IndentWidth, 12u); 11201 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 11202 "IndentWidth: 34", 11203 &Style), 11204 ParseError::Unsuitable); 11205 EXPECT_EQ(12u, Style.IndentWidth); 11206 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 11207 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 11208 11209 Style.Language = FormatStyle::LK_JavaScript; 11210 CHECK_PARSE("Language: JavaScript\n" 11211 "IndentWidth: 12", 11212 IndentWidth, 12u); 11213 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 11214 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 11215 "IndentWidth: 34", 11216 &Style), 11217 ParseError::Unsuitable); 11218 EXPECT_EQ(23u, Style.IndentWidth); 11219 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 11220 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 11221 11222 CHECK_PARSE("BasedOnStyle: LLVM\n" 11223 "IndentWidth: 67", 11224 IndentWidth, 67u); 11225 11226 CHECK_PARSE("---\n" 11227 "Language: JavaScript\n" 11228 "IndentWidth: 12\n" 11229 "---\n" 11230 "Language: Cpp\n" 11231 "IndentWidth: 34\n" 11232 "...\n", 11233 IndentWidth, 12u); 11234 11235 Style.Language = FormatStyle::LK_Cpp; 11236 CHECK_PARSE("---\n" 11237 "Language: JavaScript\n" 11238 "IndentWidth: 12\n" 11239 "---\n" 11240 "Language: Cpp\n" 11241 "IndentWidth: 34\n" 11242 "...\n", 11243 IndentWidth, 34u); 11244 CHECK_PARSE("---\n" 11245 "IndentWidth: 78\n" 11246 "---\n" 11247 "Language: JavaScript\n" 11248 "IndentWidth: 56\n" 11249 "...\n", 11250 IndentWidth, 78u); 11251 11252 Style.ColumnLimit = 123; 11253 Style.IndentWidth = 234; 11254 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 11255 Style.TabWidth = 345; 11256 EXPECT_FALSE(parseConfiguration("---\n" 11257 "IndentWidth: 456\n" 11258 "BreakBeforeBraces: Allman\n" 11259 "---\n" 11260 "Language: JavaScript\n" 11261 "IndentWidth: 111\n" 11262 "TabWidth: 111\n" 11263 "---\n" 11264 "Language: Cpp\n" 11265 "BreakBeforeBraces: Stroustrup\n" 11266 "TabWidth: 789\n" 11267 "...\n", 11268 &Style)); 11269 EXPECT_EQ(123u, Style.ColumnLimit); 11270 EXPECT_EQ(456u, Style.IndentWidth); 11271 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 11272 EXPECT_EQ(789u, Style.TabWidth); 11273 11274 EXPECT_EQ(parseConfiguration("---\n" 11275 "Language: JavaScript\n" 11276 "IndentWidth: 56\n" 11277 "---\n" 11278 "IndentWidth: 78\n" 11279 "...\n", 11280 &Style), 11281 ParseError::Error); 11282 EXPECT_EQ(parseConfiguration("---\n" 11283 "Language: JavaScript\n" 11284 "IndentWidth: 56\n" 11285 "---\n" 11286 "Language: JavaScript\n" 11287 "IndentWidth: 78\n" 11288 "...\n", 11289 &Style), 11290 ParseError::Error); 11291 11292 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 11293 } 11294 11295 #undef CHECK_PARSE 11296 11297 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 11298 FormatStyle Style = {}; 11299 Style.Language = FormatStyle::LK_JavaScript; 11300 Style.BreakBeforeTernaryOperators = true; 11301 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 11302 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 11303 11304 Style.BreakBeforeTernaryOperators = true; 11305 EXPECT_EQ(0, parseConfiguration("---\n" 11306 "BasedOnStyle: Google\n" 11307 "---\n" 11308 "Language: JavaScript\n" 11309 "IndentWidth: 76\n" 11310 "...\n", 11311 &Style) 11312 .value()); 11313 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 11314 EXPECT_EQ(76u, Style.IndentWidth); 11315 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 11316 } 11317 11318 TEST_F(FormatTest, ConfigurationRoundTripTest) { 11319 FormatStyle Style = getLLVMStyle(); 11320 std::string YAML = configurationAsText(Style); 11321 FormatStyle ParsedStyle = {}; 11322 ParsedStyle.Language = FormatStyle::LK_Cpp; 11323 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 11324 EXPECT_EQ(Style, ParsedStyle); 11325 } 11326 11327 TEST_F(FormatTest, WorksFor8bitEncodings) { 11328 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 11329 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 11330 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 11331 "\"\xef\xee\xf0\xf3...\"", 11332 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 11333 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 11334 "\xef\xee\xf0\xf3...\"", 11335 getLLVMStyleWithColumns(12))); 11336 } 11337 11338 TEST_F(FormatTest, HandlesUTF8BOM) { 11339 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 11340 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 11341 format("\xef\xbb\xbf#include <iostream>")); 11342 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 11343 format("\xef\xbb\xbf\n#include <iostream>")); 11344 } 11345 11346 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 11347 #if !defined(_MSC_VER) 11348 11349 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 11350 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 11351 getLLVMStyleWithColumns(35)); 11352 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 11353 getLLVMStyleWithColumns(31)); 11354 verifyFormat("// Однажды в студёную зимнюю пору...", 11355 getLLVMStyleWithColumns(36)); 11356 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 11357 verifyFormat("/* Однажды в студёную зимнюю пору... */", 11358 getLLVMStyleWithColumns(39)); 11359 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 11360 getLLVMStyleWithColumns(35)); 11361 } 11362 11363 TEST_F(FormatTest, SplitsUTF8Strings) { 11364 // Non-printable characters' width is currently considered to be the length in 11365 // bytes in UTF8. The characters can be displayed in very different manner 11366 // (zero-width, single width with a substitution glyph, expanded to their code 11367 // (e.g. "<8d>"), so there's no single correct way to handle them. 11368 EXPECT_EQ("\"aaaaÄ\"\n" 11369 "\"\xc2\x8d\";", 11370 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 11371 EXPECT_EQ("\"aaaaaaaÄ\"\n" 11372 "\"\xc2\x8d\";", 11373 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 11374 EXPECT_EQ("\"Однажды, в \"\n" 11375 "\"студёную \"\n" 11376 "\"зимнюю \"\n" 11377 "\"пору,\"", 11378 format("\"Однажды, в студёную зимнюю пору,\"", 11379 getLLVMStyleWithColumns(13))); 11380 EXPECT_EQ( 11381 "\"一 二 三 \"\n" 11382 "\"四 五六 \"\n" 11383 "\"七 八 九 \"\n" 11384 "\"十\"", 11385 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 11386 EXPECT_EQ("\"一\t\"\n" 11387 "\"二 \t\"\n" 11388 "\"三 四 \"\n" 11389 "\"五\t\"\n" 11390 "\"六 \t\"\n" 11391 "\"七 \"\n" 11392 "\"八九十\tqq\"", 11393 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 11394 getLLVMStyleWithColumns(11))); 11395 11396 // UTF8 character in an escape sequence. 11397 EXPECT_EQ("\"aaaaaa\"\n" 11398 "\"\\\xC2\x8D\"", 11399 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 11400 } 11401 11402 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 11403 EXPECT_EQ("const char *sssss =\n" 11404 " \"一二三四五六七八\\\n" 11405 " 九 十\";", 11406 format("const char *sssss = \"一二三四五六七八\\\n" 11407 " 九 十\";", 11408 getLLVMStyleWithColumns(30))); 11409 } 11410 11411 TEST_F(FormatTest, SplitsUTF8LineComments) { 11412 EXPECT_EQ("// aaaaÄ\xc2\x8d", 11413 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 11414 EXPECT_EQ("// Я из лесу\n" 11415 "// вышел; был\n" 11416 "// сильный\n" 11417 "// мороз.", 11418 format("// Я из лесу вышел; был сильный мороз.", 11419 getLLVMStyleWithColumns(13))); 11420 EXPECT_EQ("// 一二三\n" 11421 "// 四五六七\n" 11422 "// 八 九\n" 11423 "// 十", 11424 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 11425 } 11426 11427 TEST_F(FormatTest, SplitsUTF8BlockComments) { 11428 EXPECT_EQ("/* Гляжу,\n" 11429 " * поднимается\n" 11430 " * медленно в\n" 11431 " * гору\n" 11432 " * Лошадка,\n" 11433 " * везущая\n" 11434 " * хворосту\n" 11435 " * воз. */", 11436 format("/* Гляжу, поднимается медленно в гору\n" 11437 " * Лошадка, везущая хворосту воз. */", 11438 getLLVMStyleWithColumns(13))); 11439 EXPECT_EQ( 11440 "/* 一二三\n" 11441 " * 四五六七\n" 11442 " * 八 九\n" 11443 " * 十 */", 11444 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 11445 EXPECT_EQ("/* \n" 11446 " * \n" 11447 " * - */", 11448 format("/* - */", getLLVMStyleWithColumns(12))); 11449 } 11450 11451 #endif // _MSC_VER 11452 11453 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 11454 FormatStyle Style = getLLVMStyle(); 11455 11456 Style.ConstructorInitializerIndentWidth = 4; 11457 verifyFormat( 11458 "SomeClass::Constructor()\n" 11459 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 11460 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 11461 Style); 11462 11463 Style.ConstructorInitializerIndentWidth = 2; 11464 verifyFormat( 11465 "SomeClass::Constructor()\n" 11466 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 11467 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 11468 Style); 11469 11470 Style.ConstructorInitializerIndentWidth = 0; 11471 verifyFormat( 11472 "SomeClass::Constructor()\n" 11473 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 11474 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 11475 Style); 11476 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11477 verifyFormat( 11478 "SomeLongTemplateVariableName<\n" 11479 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 11480 Style); 11481 verifyFormat( 11482 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 11483 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 11484 Style); 11485 } 11486 11487 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 11488 FormatStyle Style = getLLVMStyle(); 11489 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 11490 Style.ConstructorInitializerIndentWidth = 4; 11491 verifyFormat("SomeClass::Constructor()\n" 11492 " : a(a)\n" 11493 " , b(b)\n" 11494 " , c(c) {}", 11495 Style); 11496 verifyFormat("SomeClass::Constructor()\n" 11497 " : a(a) {}", 11498 Style); 11499 11500 Style.ColumnLimit = 0; 11501 verifyFormat("SomeClass::Constructor()\n" 11502 " : a(a) {}", 11503 Style); 11504 verifyFormat("SomeClass::Constructor() noexcept\n" 11505 " : a(a) {}", 11506 Style); 11507 verifyFormat("SomeClass::Constructor()\n" 11508 " : a(a)\n" 11509 " , b(b)\n" 11510 " , c(c) {}", 11511 Style); 11512 verifyFormat("SomeClass::Constructor()\n" 11513 " : a(a) {\n" 11514 " foo();\n" 11515 " bar();\n" 11516 "}", 11517 Style); 11518 11519 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11520 verifyFormat("SomeClass::Constructor()\n" 11521 " : a(a)\n" 11522 " , b(b)\n" 11523 " , c(c) {\n}", 11524 Style); 11525 verifyFormat("SomeClass::Constructor()\n" 11526 " : a(a) {\n}", 11527 Style); 11528 11529 Style.ColumnLimit = 80; 11530 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11531 Style.ConstructorInitializerIndentWidth = 2; 11532 verifyFormat("SomeClass::Constructor()\n" 11533 " : a(a)\n" 11534 " , b(b)\n" 11535 " , c(c) {}", 11536 Style); 11537 11538 Style.ConstructorInitializerIndentWidth = 0; 11539 verifyFormat("SomeClass::Constructor()\n" 11540 ": a(a)\n" 11541 ", b(b)\n" 11542 ", c(c) {}", 11543 Style); 11544 11545 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 11546 Style.ConstructorInitializerIndentWidth = 4; 11547 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 11548 verifyFormat( 11549 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 11550 Style); 11551 verifyFormat( 11552 "SomeClass::Constructor()\n" 11553 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 11554 Style); 11555 Style.ConstructorInitializerIndentWidth = 4; 11556 Style.ColumnLimit = 60; 11557 verifyFormat("SomeClass::Constructor()\n" 11558 " : aaaaaaaa(aaaaaaaa)\n" 11559 " , aaaaaaaa(aaaaaaaa)\n" 11560 " , aaaaaaaa(aaaaaaaa) {}", 11561 Style); 11562 } 11563 11564 TEST_F(FormatTest, Destructors) { 11565 verifyFormat("void F(int &i) { i.~int(); }"); 11566 verifyFormat("void F(int &i) { i->~int(); }"); 11567 } 11568 11569 TEST_F(FormatTest, FormatsWithWebKitStyle) { 11570 FormatStyle Style = getWebKitStyle(); 11571 11572 // Don't indent in outer namespaces. 11573 verifyFormat("namespace outer {\n" 11574 "int i;\n" 11575 "namespace inner {\n" 11576 " int i;\n" 11577 "} // namespace inner\n" 11578 "} // namespace outer\n" 11579 "namespace other_outer {\n" 11580 "int i;\n" 11581 "}", 11582 Style); 11583 11584 // Don't indent case labels. 11585 verifyFormat("switch (variable) {\n" 11586 "case 1:\n" 11587 "case 2:\n" 11588 " doSomething();\n" 11589 " break;\n" 11590 "default:\n" 11591 " ++variable;\n" 11592 "}", 11593 Style); 11594 11595 // Wrap before binary operators. 11596 EXPECT_EQ("void f()\n" 11597 "{\n" 11598 " if (aaaaaaaaaaaaaaaa\n" 11599 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 11600 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 11601 " return;\n" 11602 "}", 11603 format("void f() {\n" 11604 "if (aaaaaaaaaaaaaaaa\n" 11605 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 11606 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 11607 "return;\n" 11608 "}", 11609 Style)); 11610 11611 // Allow functions on a single line. 11612 verifyFormat("void f() { return; }", Style); 11613 11614 // Constructor initializers are formatted one per line with the "," on the 11615 // new line. 11616 verifyFormat("Constructor()\n" 11617 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 11618 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 11619 " aaaaaaaaaaaaaa)\n" 11620 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 11621 "{\n" 11622 "}", 11623 Style); 11624 verifyFormat("SomeClass::Constructor()\n" 11625 " : a(a)\n" 11626 "{\n" 11627 "}", 11628 Style); 11629 EXPECT_EQ("SomeClass::Constructor()\n" 11630 " : a(a)\n" 11631 "{\n" 11632 "}", 11633 format("SomeClass::Constructor():a(a){}", Style)); 11634 verifyFormat("SomeClass::Constructor()\n" 11635 " : a(a)\n" 11636 " , b(b)\n" 11637 " , c(c)\n" 11638 "{\n" 11639 "}", 11640 Style); 11641 verifyFormat("SomeClass::Constructor()\n" 11642 " : a(a)\n" 11643 "{\n" 11644 " foo();\n" 11645 " bar();\n" 11646 "}", 11647 Style); 11648 11649 // Access specifiers should be aligned left. 11650 verifyFormat("class C {\n" 11651 "public:\n" 11652 " int i;\n" 11653 "};", 11654 Style); 11655 11656 // Do not align comments. 11657 verifyFormat("int a; // Do not\n" 11658 "double b; // align comments.", 11659 Style); 11660 11661 // Do not align operands. 11662 EXPECT_EQ("ASSERT(aaaa\n" 11663 " || bbbb);", 11664 format("ASSERT ( aaaa\n||bbbb);", Style)); 11665 11666 // Accept input's line breaks. 11667 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 11668 " || bbbbbbbbbbbbbbb) {\n" 11669 " i++;\n" 11670 "}", 11671 format("if (aaaaaaaaaaaaaaa\n" 11672 "|| bbbbbbbbbbbbbbb) { i++; }", 11673 Style)); 11674 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 11675 " i++;\n" 11676 "}", 11677 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 11678 11679 // Don't automatically break all macro definitions (llvm.org/PR17842). 11680 verifyFormat("#define aNumber 10", Style); 11681 // However, generally keep the line breaks that the user authored. 11682 EXPECT_EQ("#define aNumber \\\n" 11683 " 10", 11684 format("#define aNumber \\\n" 11685 " 10", 11686 Style)); 11687 11688 // Keep empty and one-element array literals on a single line. 11689 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 11690 " copyItems:YES];", 11691 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 11692 "copyItems:YES];", 11693 Style)); 11694 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 11695 " copyItems:YES];", 11696 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 11697 " copyItems:YES];", 11698 Style)); 11699 // FIXME: This does not seem right, there should be more indentation before 11700 // the array literal's entries. Nested blocks have the same problem. 11701 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11702 " @\"a\",\n" 11703 " @\"a\"\n" 11704 "]\n" 11705 " copyItems:YES];", 11706 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11707 " @\"a\",\n" 11708 " @\"a\"\n" 11709 " ]\n" 11710 " copyItems:YES];", 11711 Style)); 11712 EXPECT_EQ( 11713 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11714 " copyItems:YES];", 11715 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11716 " copyItems:YES];", 11717 Style)); 11718 11719 verifyFormat("[self.a b:c c:d];", Style); 11720 EXPECT_EQ("[self.a b:c\n" 11721 " c:d];", 11722 format("[self.a b:c\n" 11723 "c:d];", 11724 Style)); 11725 } 11726 11727 TEST_F(FormatTest, FormatsLambdas) { 11728 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 11729 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 11730 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 11731 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 11732 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 11733 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 11734 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 11735 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 11736 verifyFormat("int x = f(*+[] {});"); 11737 verifyFormat("void f() {\n" 11738 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 11739 "}\n"); 11740 verifyFormat("void f() {\n" 11741 " other(x.begin(), //\n" 11742 " x.end(), //\n" 11743 " [&](int, int) { return 1; });\n" 11744 "}\n"); 11745 verifyFormat("void f() {\n" 11746 " other.other.other.other.other(\n" 11747 " x.begin(), x.end(),\n" 11748 " [something, rather](int, int, int, int, int, int, int) { return 1; });\n" 11749 "}\n"); 11750 verifyFormat("void f() {\n" 11751 " other.other.other.other.other(\n" 11752 " x.begin(), x.end(),\n" 11753 " [something, rather](int, int, int, int, int, int, int) {\n" 11754 " //\n" 11755 " });\n" 11756 "}\n"); 11757 verifyFormat("SomeFunction([]() { // A cool function...\n" 11758 " return 43;\n" 11759 "});"); 11760 EXPECT_EQ("SomeFunction([]() {\n" 11761 "#define A a\n" 11762 " return 43;\n" 11763 "});", 11764 format("SomeFunction([](){\n" 11765 "#define A a\n" 11766 "return 43;\n" 11767 "});")); 11768 verifyFormat("void f() {\n" 11769 " SomeFunction([](decltype(x), A *a) {});\n" 11770 "}"); 11771 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11772 " [](const aaaaaaaaaa &a) { return a; });"); 11773 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 11774 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 11775 "});"); 11776 verifyFormat("Constructor()\n" 11777 " : Field([] { // comment\n" 11778 " int i;\n" 11779 " }) {}"); 11780 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 11781 " return some_parameter.size();\n" 11782 "};"); 11783 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 11784 " [](const string &s) { return s; };"); 11785 verifyFormat("int i = aaaaaa ? 1 //\n" 11786 " : [] {\n" 11787 " return 2; //\n" 11788 " }();"); 11789 verifyFormat("llvm::errs() << \"number of twos is \"\n" 11790 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 11791 " return x == 2; // force break\n" 11792 " });"); 11793 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11794 " [=](int iiiiiiiiiiii) {\n" 11795 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 11796 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 11797 " });", 11798 getLLVMStyleWithColumns(60)); 11799 verifyFormat("SomeFunction({[&] {\n" 11800 " // comment\n" 11801 " },\n" 11802 " [&] {\n" 11803 " // comment\n" 11804 " }});"); 11805 verifyFormat("SomeFunction({[&] {\n" 11806 " // comment\n" 11807 "}});"); 11808 verifyFormat("virtual aaaaaaaaaaaaaaaa(\n" 11809 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 11810 " aaaaa aaaaaaaaa);"); 11811 11812 // Lambdas with return types. 11813 verifyFormat("int c = []() -> int { return 2; }();\n"); 11814 verifyFormat("int c = []() -> int * { return 2; }();\n"); 11815 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 11816 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 11817 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 11818 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 11819 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 11820 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 11821 verifyFormat("[a, a]() -> a<1> {};"); 11822 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 11823 " int j) -> int {\n" 11824 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 11825 "};"); 11826 verifyFormat( 11827 "aaaaaaaaaaaaaaaaaaaaaa(\n" 11828 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 11829 " return aaaaaaaaaaaaaaaaa;\n" 11830 " });", 11831 getLLVMStyleWithColumns(70)); 11832 verifyFormat("[]() //\n" 11833 " -> int {\n" 11834 " return 1; //\n" 11835 "};"); 11836 11837 // Multiple lambdas in the same parentheses change indentation rules. These 11838 // lambdas are forced to start on new lines. 11839 verifyFormat("SomeFunction(\n" 11840 " []() {\n" 11841 " //\n" 11842 " },\n" 11843 " []() {\n" 11844 " //\n" 11845 " });"); 11846 11847 // A lambda passed as arg0 is always pushed to the next line. 11848 verifyFormat("SomeFunction(\n" 11849 " [this] {\n" 11850 " //\n" 11851 " },\n" 11852 " 1);\n"); 11853 11854 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like the arg0 11855 // case above. 11856 auto Style = getGoogleStyle(); 11857 Style.BinPackArguments = false; 11858 verifyFormat("SomeFunction(\n" 11859 " a,\n" 11860 " [this] {\n" 11861 " //\n" 11862 " },\n" 11863 " b);\n", 11864 Style); 11865 verifyFormat("SomeFunction(\n" 11866 " a,\n" 11867 " [this] {\n" 11868 " //\n" 11869 " },\n" 11870 " b);\n"); 11871 11872 // A lambda with a very long line forces arg0 to be pushed out irrespective of 11873 // the BinPackArguments value (as long as the code is wide enough). 11874 verifyFormat("something->SomeFunction(\n" 11875 " a,\n" 11876 " [this] {\n" 11877 " D0000000000000000000000000000000000000000000000000000000000001();\n" 11878 " },\n" 11879 " b);\n"); 11880 11881 // A multi-line lambda is pulled up as long as the introducer fits on the previous 11882 // line and there are no further args. 11883 verifyFormat("function(1, [this, that] {\n" 11884 " //\n" 11885 "});\n"); 11886 verifyFormat("function([this, that] {\n" 11887 " //\n" 11888 "});\n"); 11889 // FIXME: this format is not ideal and we should consider forcing the first arg 11890 // onto its own line. 11891 verifyFormat("function(a, b, c, //\n" 11892 " d, [this, that] {\n" 11893 " //\n" 11894 " });\n"); 11895 11896 // Multiple lambdas are treated correctly even when there is a short arg0. 11897 verifyFormat("SomeFunction(\n" 11898 " 1,\n" 11899 " [this] {\n" 11900 " //\n" 11901 " },\n" 11902 " [this] {\n" 11903 " //\n" 11904 " },\n" 11905 " 1);\n"); 11906 11907 // More complex introducers. 11908 verifyFormat("return [i, args...] {};"); 11909 11910 // Not lambdas. 11911 verifyFormat("constexpr char hello[]{\"hello\"};"); 11912 verifyFormat("double &operator[](int i) { return 0; }\n" 11913 "int i;"); 11914 verifyFormat("std::unique_ptr<int[]> foo() {}"); 11915 verifyFormat("int i = a[a][a]->f();"); 11916 verifyFormat("int i = (*b)[a]->f();"); 11917 11918 // Other corner cases. 11919 verifyFormat("void f() {\n" 11920 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 11921 " );\n" 11922 "}"); 11923 11924 // Lambdas created through weird macros. 11925 verifyFormat("void f() {\n" 11926 " MACRO((const AA &a) { return 1; });\n" 11927 " MACRO((AA &a) { return 1; });\n" 11928 "}"); 11929 11930 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 11931 " doo_dah();\n" 11932 " doo_dah();\n" 11933 " })) {\n" 11934 "}"); 11935 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 11936 " doo_dah();\n" 11937 " doo_dah();\n" 11938 " })) {\n" 11939 "}"); 11940 verifyFormat("auto lambda = []() {\n" 11941 " int a = 2\n" 11942 "#if A\n" 11943 " + 2\n" 11944 "#endif\n" 11945 " ;\n" 11946 "};"); 11947 11948 // Lambdas with complex multiline introducers. 11949 verifyFormat( 11950 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11951 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 11952 " -> ::std::unordered_set<\n" 11953 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 11954 " //\n" 11955 " });"); 11956 } 11957 11958 TEST_F(FormatTest, EmptyLinesInLambdas) { 11959 verifyFormat("auto lambda = []() {\n" 11960 " x(); //\n" 11961 "};", 11962 "auto lambda = []() {\n" 11963 "\n" 11964 " x(); //\n" 11965 "\n" 11966 "};"); 11967 } 11968 11969 TEST_F(FormatTest, FormatsBlocks) { 11970 FormatStyle ShortBlocks = getLLVMStyle(); 11971 ShortBlocks.AllowShortBlocksOnASingleLine = true; 11972 verifyFormat("int (^Block)(int, int);", ShortBlocks); 11973 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 11974 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 11975 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 11976 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 11977 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 11978 11979 verifyFormat("foo(^{ bar(); });", ShortBlocks); 11980 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 11981 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 11982 11983 verifyFormat("[operation setCompletionBlock:^{\n" 11984 " [self onOperationDone];\n" 11985 "}];"); 11986 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 11987 " [self onOperationDone];\n" 11988 "}]};"); 11989 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 11990 " f();\n" 11991 "}];"); 11992 verifyFormat("int a = [operation block:^int(int *i) {\n" 11993 " return 1;\n" 11994 "}];"); 11995 verifyFormat("[myObject doSomethingWith:arg1\n" 11996 " aaa:^int(int *a) {\n" 11997 " return 1;\n" 11998 " }\n" 11999 " bbb:f(a * bbbbbbbb)];"); 12000 12001 verifyFormat("[operation setCompletionBlock:^{\n" 12002 " [self.delegate newDataAvailable];\n" 12003 "}];", 12004 getLLVMStyleWithColumns(60)); 12005 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 12006 " NSString *path = [self sessionFilePath];\n" 12007 " if (path) {\n" 12008 " // ...\n" 12009 " }\n" 12010 "});"); 12011 verifyFormat("[[SessionService sharedService]\n" 12012 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12013 " if (window) {\n" 12014 " [self windowDidLoad:window];\n" 12015 " } else {\n" 12016 " [self errorLoadingWindow];\n" 12017 " }\n" 12018 " }];"); 12019 verifyFormat("void (^largeBlock)(void) = ^{\n" 12020 " // ...\n" 12021 "};\n", 12022 getLLVMStyleWithColumns(40)); 12023 verifyFormat("[[SessionService sharedService]\n" 12024 " loadWindowWithCompletionBlock: //\n" 12025 " ^(SessionWindow *window) {\n" 12026 " if (window) {\n" 12027 " [self windowDidLoad:window];\n" 12028 " } else {\n" 12029 " [self errorLoadingWindow];\n" 12030 " }\n" 12031 " }];", 12032 getLLVMStyleWithColumns(60)); 12033 verifyFormat("[myObject doSomethingWith:arg1\n" 12034 " firstBlock:^(Foo *a) {\n" 12035 " // ...\n" 12036 " int i;\n" 12037 " }\n" 12038 " secondBlock:^(Bar *b) {\n" 12039 " // ...\n" 12040 " int i;\n" 12041 " }\n" 12042 " thirdBlock:^Foo(Bar *b) {\n" 12043 " // ...\n" 12044 " int i;\n" 12045 " }];"); 12046 verifyFormat("[myObject doSomethingWith:arg1\n" 12047 " firstBlock:-1\n" 12048 " secondBlock:^(Bar *b) {\n" 12049 " // ...\n" 12050 " int i;\n" 12051 " }];"); 12052 12053 verifyFormat("f(^{\n" 12054 " @autoreleasepool {\n" 12055 " if (a) {\n" 12056 " g();\n" 12057 " }\n" 12058 " }\n" 12059 "});"); 12060 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 12061 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 12062 "};"); 12063 12064 FormatStyle FourIndent = getLLVMStyle(); 12065 FourIndent.ObjCBlockIndentWidth = 4; 12066 verifyFormat("[operation setCompletionBlock:^{\n" 12067 " [self onOperationDone];\n" 12068 "}];", 12069 FourIndent); 12070 } 12071 12072 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 12073 FormatStyle ZeroColumn = getLLVMStyle(); 12074 ZeroColumn.ColumnLimit = 0; 12075 12076 verifyFormat("[[SessionService sharedService] " 12077 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12078 " if (window) {\n" 12079 " [self windowDidLoad:window];\n" 12080 " } else {\n" 12081 " [self errorLoadingWindow];\n" 12082 " }\n" 12083 "}];", 12084 ZeroColumn); 12085 EXPECT_EQ("[[SessionService sharedService]\n" 12086 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12087 " if (window) {\n" 12088 " [self windowDidLoad:window];\n" 12089 " } else {\n" 12090 " [self errorLoadingWindow];\n" 12091 " }\n" 12092 " }];", 12093 format("[[SessionService sharedService]\n" 12094 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12095 " if (window) {\n" 12096 " [self windowDidLoad:window];\n" 12097 " } else {\n" 12098 " [self errorLoadingWindow];\n" 12099 " }\n" 12100 "}];", 12101 ZeroColumn)); 12102 verifyFormat("[myObject doSomethingWith:arg1\n" 12103 " firstBlock:^(Foo *a) {\n" 12104 " // ...\n" 12105 " int i;\n" 12106 " }\n" 12107 " secondBlock:^(Bar *b) {\n" 12108 " // ...\n" 12109 " int i;\n" 12110 " }\n" 12111 " thirdBlock:^Foo(Bar *b) {\n" 12112 " // ...\n" 12113 " int i;\n" 12114 " }];", 12115 ZeroColumn); 12116 verifyFormat("f(^{\n" 12117 " @autoreleasepool {\n" 12118 " if (a) {\n" 12119 " g();\n" 12120 " }\n" 12121 " }\n" 12122 "});", 12123 ZeroColumn); 12124 verifyFormat("void (^largeBlock)(void) = ^{\n" 12125 " // ...\n" 12126 "};", 12127 ZeroColumn); 12128 12129 ZeroColumn.AllowShortBlocksOnASingleLine = true; 12130 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 12131 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 12132 ZeroColumn.AllowShortBlocksOnASingleLine = false; 12133 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 12134 " int i;\n" 12135 "};", 12136 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 12137 } 12138 12139 TEST_F(FormatTest, SupportsCRLF) { 12140 EXPECT_EQ("int a;\r\n" 12141 "int b;\r\n" 12142 "int c;\r\n", 12143 format("int a;\r\n" 12144 " int b;\r\n" 12145 " int c;\r\n", 12146 getLLVMStyle())); 12147 EXPECT_EQ("int a;\r\n" 12148 "int b;\r\n" 12149 "int c;\r\n", 12150 format("int a;\r\n" 12151 " int b;\n" 12152 " int c;\r\n", 12153 getLLVMStyle())); 12154 EXPECT_EQ("int a;\n" 12155 "int b;\n" 12156 "int c;\n", 12157 format("int a;\r\n" 12158 " int b;\n" 12159 " int c;\n", 12160 getLLVMStyle())); 12161 EXPECT_EQ("\"aaaaaaa \"\r\n" 12162 "\"bbbbbbb\";\r\n", 12163 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 12164 EXPECT_EQ("#define A \\\r\n" 12165 " b; \\\r\n" 12166 " c; \\\r\n" 12167 " d;\r\n", 12168 format("#define A \\\r\n" 12169 " b; \\\r\n" 12170 " c; d; \r\n", 12171 getGoogleStyle())); 12172 12173 EXPECT_EQ("/*\r\n" 12174 "multi line block comments\r\n" 12175 "should not introduce\r\n" 12176 "an extra carriage return\r\n" 12177 "*/\r\n", 12178 format("/*\r\n" 12179 "multi line block comments\r\n" 12180 "should not introduce\r\n" 12181 "an extra carriage return\r\n" 12182 "*/\r\n")); 12183 } 12184 12185 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 12186 verifyFormat("MY_CLASS(C) {\n" 12187 " int i;\n" 12188 " int j;\n" 12189 "};"); 12190 } 12191 12192 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 12193 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 12194 TwoIndent.ContinuationIndentWidth = 2; 12195 12196 EXPECT_EQ("int i =\n" 12197 " longFunction(\n" 12198 " arg);", 12199 format("int i = longFunction(arg);", TwoIndent)); 12200 12201 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 12202 SixIndent.ContinuationIndentWidth = 6; 12203 12204 EXPECT_EQ("int i =\n" 12205 " longFunction(\n" 12206 " arg);", 12207 format("int i = longFunction(arg);", SixIndent)); 12208 } 12209 12210 TEST_F(FormatTest, SpacesInAngles) { 12211 FormatStyle Spaces = getLLVMStyle(); 12212 Spaces.SpacesInAngles = true; 12213 12214 verifyFormat("static_cast< int >(arg);", Spaces); 12215 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 12216 verifyFormat("f< int, float >();", Spaces); 12217 verifyFormat("template <> g() {}", Spaces); 12218 verifyFormat("template < std::vector< int > > f() {}", Spaces); 12219 verifyFormat("std::function< void(int, int) > fct;", Spaces); 12220 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 12221 Spaces); 12222 12223 Spaces.Standard = FormatStyle::LS_Cpp03; 12224 Spaces.SpacesInAngles = true; 12225 verifyFormat("A< A< int > >();", Spaces); 12226 12227 Spaces.SpacesInAngles = false; 12228 verifyFormat("A<A<int> >();", Spaces); 12229 12230 Spaces.Standard = FormatStyle::LS_Cpp11; 12231 Spaces.SpacesInAngles = true; 12232 verifyFormat("A< A< int > >();", Spaces); 12233 12234 Spaces.SpacesInAngles = false; 12235 verifyFormat("A<A<int>>();", Spaces); 12236 } 12237 12238 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 12239 FormatStyle Style = getLLVMStyle(); 12240 Style.SpaceAfterTemplateKeyword = false; 12241 verifyFormat("template<int> void foo();", Style); 12242 } 12243 12244 TEST_F(FormatTest, TripleAngleBrackets) { 12245 verifyFormat("f<<<1, 1>>>();"); 12246 verifyFormat("f<<<1, 1, 1, s>>>();"); 12247 verifyFormat("f<<<a, b, c, d>>>();"); 12248 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 12249 verifyFormat("f<param><<<1, 1>>>();"); 12250 verifyFormat("f<1><<<1, 1>>>();"); 12251 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 12252 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12253 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 12254 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 12255 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 12256 } 12257 12258 TEST_F(FormatTest, MergeLessLessAtEnd) { 12259 verifyFormat("<<"); 12260 EXPECT_EQ("< < <", format("\\\n<<<")); 12261 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12262 "aaallvm::outs() <<"); 12263 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12264 "aaaallvm::outs()\n <<"); 12265 } 12266 12267 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 12268 std::string code = "#if A\n" 12269 "#if B\n" 12270 "a.\n" 12271 "#endif\n" 12272 " a = 1;\n" 12273 "#else\n" 12274 "#endif\n" 12275 "#if C\n" 12276 "#else\n" 12277 "#endif\n"; 12278 EXPECT_EQ(code, format(code)); 12279 } 12280 12281 TEST_F(FormatTest, HandleConflictMarkers) { 12282 // Git/SVN conflict markers. 12283 EXPECT_EQ("int a;\n" 12284 "void f() {\n" 12285 " callme(some(parameter1,\n" 12286 "<<<<<<< text by the vcs\n" 12287 " parameter2),\n" 12288 "||||||| text by the vcs\n" 12289 " parameter2),\n" 12290 " parameter3,\n" 12291 "======= text by the vcs\n" 12292 " parameter2, parameter3),\n" 12293 ">>>>>>> text by the vcs\n" 12294 " otherparameter);\n", 12295 format("int a;\n" 12296 "void f() {\n" 12297 " callme(some(parameter1,\n" 12298 "<<<<<<< text by the vcs\n" 12299 " parameter2),\n" 12300 "||||||| text by the vcs\n" 12301 " parameter2),\n" 12302 " parameter3,\n" 12303 "======= text by the vcs\n" 12304 " parameter2,\n" 12305 " parameter3),\n" 12306 ">>>>>>> text by the vcs\n" 12307 " otherparameter);\n")); 12308 12309 // Perforce markers. 12310 EXPECT_EQ("void f() {\n" 12311 " function(\n" 12312 ">>>> text by the vcs\n" 12313 " parameter,\n" 12314 "==== text by the vcs\n" 12315 " parameter,\n" 12316 "==== text by the vcs\n" 12317 " parameter,\n" 12318 "<<<< text by the vcs\n" 12319 " parameter);\n", 12320 format("void f() {\n" 12321 " function(\n" 12322 ">>>> text by the vcs\n" 12323 " parameter,\n" 12324 "==== text by the vcs\n" 12325 " parameter,\n" 12326 "==== text by the vcs\n" 12327 " parameter,\n" 12328 "<<<< text by the vcs\n" 12329 " parameter);\n")); 12330 12331 EXPECT_EQ("<<<<<<<\n" 12332 "|||||||\n" 12333 "=======\n" 12334 ">>>>>>>", 12335 format("<<<<<<<\n" 12336 "|||||||\n" 12337 "=======\n" 12338 ">>>>>>>")); 12339 12340 EXPECT_EQ("<<<<<<<\n" 12341 "|||||||\n" 12342 "int i;\n" 12343 "=======\n" 12344 ">>>>>>>", 12345 format("<<<<<<<\n" 12346 "|||||||\n" 12347 "int i;\n" 12348 "=======\n" 12349 ">>>>>>>")); 12350 12351 // FIXME: Handle parsing of macros around conflict markers correctly: 12352 EXPECT_EQ("#define Macro \\\n" 12353 "<<<<<<<\n" 12354 "Something \\\n" 12355 "|||||||\n" 12356 "Else \\\n" 12357 "=======\n" 12358 "Other \\\n" 12359 ">>>>>>>\n" 12360 " End int i;\n", 12361 format("#define Macro \\\n" 12362 "<<<<<<<\n" 12363 " Something \\\n" 12364 "|||||||\n" 12365 " Else \\\n" 12366 "=======\n" 12367 " Other \\\n" 12368 ">>>>>>>\n" 12369 " End\n" 12370 "int i;\n")); 12371 } 12372 12373 TEST_F(FormatTest, DisableRegions) { 12374 EXPECT_EQ("int i;\n" 12375 "// clang-format off\n" 12376 " int j;\n" 12377 "// clang-format on\n" 12378 "int k;", 12379 format(" int i;\n" 12380 " // clang-format off\n" 12381 " int j;\n" 12382 " // clang-format on\n" 12383 " int k;")); 12384 EXPECT_EQ("int i;\n" 12385 "/* clang-format off */\n" 12386 " int j;\n" 12387 "/* clang-format on */\n" 12388 "int k;", 12389 format(" int i;\n" 12390 " /* clang-format off */\n" 12391 " int j;\n" 12392 " /* clang-format on */\n" 12393 " int k;")); 12394 12395 // Don't reflow comments within disabled regions. 12396 EXPECT_EQ( 12397 "// clang-format off\n" 12398 "// long long long long long long line\n" 12399 "/* clang-format on */\n" 12400 "/* long long long\n" 12401 " * long long long\n" 12402 " * line */\n" 12403 "int i;\n" 12404 "/* clang-format off */\n" 12405 "/* long long long long long long line */\n", 12406 format("// clang-format off\n" 12407 "// long long long long long long line\n" 12408 "/* clang-format on */\n" 12409 "/* long long long long long long line */\n" 12410 "int i;\n" 12411 "/* clang-format off */\n" 12412 "/* long long long long long long line */\n", 12413 getLLVMStyleWithColumns(20))); 12414 } 12415 12416 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 12417 format("? ) ="); 12418 verifyNoCrash("#define a\\\n /**/}"); 12419 } 12420 12421 TEST_F(FormatTest, FormatsTableGenCode) { 12422 FormatStyle Style = getLLVMStyle(); 12423 Style.Language = FormatStyle::LK_TableGen; 12424 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 12425 } 12426 12427 TEST_F(FormatTest, ArrayOfTemplates) { 12428 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 12429 format("auto a = new unique_ptr<int > [ 10];")); 12430 12431 FormatStyle Spaces = getLLVMStyle(); 12432 Spaces.SpacesInSquareBrackets = true; 12433 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 12434 format("auto a = new unique_ptr<int > [10];", Spaces)); 12435 } 12436 12437 TEST_F(FormatTest, ArrayAsTemplateType) { 12438 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 12439 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 12440 12441 FormatStyle Spaces = getLLVMStyle(); 12442 Spaces.SpacesInSquareBrackets = true; 12443 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 12444 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 12445 } 12446 12447 TEST_F(FormatTest, NoSpaceAfterSuper) { 12448 verifyFormat("__super::FooBar();"); 12449 } 12450 12451 TEST(FormatStyle, GetStyleWithEmptyFileName) { 12452 llvm::vfs::InMemoryFileSystem FS; 12453 auto Style1 = getStyle("file", "", "Google", "", &FS); 12454 ASSERT_TRUE((bool)Style1); 12455 ASSERT_EQ(*Style1, getGoogleStyle()); 12456 } 12457 12458 TEST(FormatStyle, GetStyleOfFile) { 12459 llvm::vfs::InMemoryFileSystem FS; 12460 // Test 1: format file in the same directory. 12461 ASSERT_TRUE( 12462 FS.addFile("/a/.clang-format", 0, 12463 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 12464 ASSERT_TRUE( 12465 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 12466 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 12467 ASSERT_TRUE((bool)Style1); 12468 ASSERT_EQ(*Style1, getLLVMStyle()); 12469 12470 // Test 2.1: fallback to default. 12471 ASSERT_TRUE( 12472 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 12473 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 12474 ASSERT_TRUE((bool)Style2); 12475 ASSERT_EQ(*Style2, getMozillaStyle()); 12476 12477 // Test 2.2: no format on 'none' fallback style. 12478 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 12479 ASSERT_TRUE((bool)Style2); 12480 ASSERT_EQ(*Style2, getNoStyle()); 12481 12482 // Test 2.3: format if config is found with no based style while fallback is 12483 // 'none'. 12484 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 12485 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 12486 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 12487 ASSERT_TRUE((bool)Style2); 12488 ASSERT_EQ(*Style2, getLLVMStyle()); 12489 12490 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 12491 Style2 = getStyle("{}", "a.h", "none", "", &FS); 12492 ASSERT_TRUE((bool)Style2); 12493 ASSERT_EQ(*Style2, getLLVMStyle()); 12494 12495 // Test 3: format file in parent directory. 12496 ASSERT_TRUE( 12497 FS.addFile("/c/.clang-format", 0, 12498 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 12499 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 12500 llvm::MemoryBuffer::getMemBuffer("int i;"))); 12501 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 12502 ASSERT_TRUE((bool)Style3); 12503 ASSERT_EQ(*Style3, getGoogleStyle()); 12504 12505 // Test 4: error on invalid fallback style 12506 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 12507 ASSERT_FALSE((bool)Style4); 12508 llvm::consumeError(Style4.takeError()); 12509 12510 // Test 5: error on invalid yaml on command line 12511 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 12512 ASSERT_FALSE((bool)Style5); 12513 llvm::consumeError(Style5.takeError()); 12514 12515 // Test 6: error on invalid style 12516 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 12517 ASSERT_FALSE((bool)Style6); 12518 llvm::consumeError(Style6.takeError()); 12519 12520 // Test 7: found config file, error on parsing it 12521 ASSERT_TRUE( 12522 FS.addFile("/d/.clang-format", 0, 12523 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 12524 "InvalidKey: InvalidValue"))); 12525 ASSERT_TRUE( 12526 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 12527 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 12528 ASSERT_FALSE((bool)Style7); 12529 llvm::consumeError(Style7.takeError()); 12530 } 12531 12532 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 12533 // Column limit is 20. 12534 std::string Code = "Type *a =\n" 12535 " new Type();\n" 12536 "g(iiiii, 0, jjjjj,\n" 12537 " 0, kkkkk, 0, mm);\n" 12538 "int bad = format ;"; 12539 std::string Expected = "auto a = new Type();\n" 12540 "g(iiiii, nullptr,\n" 12541 " jjjjj, nullptr,\n" 12542 " kkkkk, nullptr,\n" 12543 " mm);\n" 12544 "int bad = format ;"; 12545 FileID ID = Context.createInMemoryFile("format.cpp", Code); 12546 tooling::Replacements Replaces = toReplacements( 12547 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 12548 "auto "), 12549 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 12550 "nullptr"), 12551 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 12552 "nullptr"), 12553 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 12554 "nullptr")}); 12555 12556 format::FormatStyle Style = format::getLLVMStyle(); 12557 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 12558 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 12559 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 12560 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 12561 auto Result = applyAllReplacements(Code, *FormattedReplaces); 12562 EXPECT_TRUE(static_cast<bool>(Result)); 12563 EXPECT_EQ(Expected, *Result); 12564 } 12565 12566 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 12567 std::string Code = "#include \"a.h\"\n" 12568 "#include \"c.h\"\n" 12569 "\n" 12570 "int main() {\n" 12571 " return 0;\n" 12572 "}"; 12573 std::string Expected = "#include \"a.h\"\n" 12574 "#include \"b.h\"\n" 12575 "#include \"c.h\"\n" 12576 "\n" 12577 "int main() {\n" 12578 " return 0;\n" 12579 "}"; 12580 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 12581 tooling::Replacements Replaces = toReplacements( 12582 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 12583 "#include \"b.h\"\n")}); 12584 12585 format::FormatStyle Style = format::getLLVMStyle(); 12586 Style.SortIncludes = true; 12587 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 12588 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 12589 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 12590 auto Result = applyAllReplacements(Code, *FormattedReplaces); 12591 EXPECT_TRUE(static_cast<bool>(Result)); 12592 EXPECT_EQ(Expected, *Result); 12593 } 12594 12595 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 12596 EXPECT_EQ("using std::cin;\n" 12597 "using std::cout;", 12598 format("using std::cout;\n" 12599 "using std::cin;", getGoogleStyle())); 12600 } 12601 12602 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 12603 format::FormatStyle Style = format::getLLVMStyle(); 12604 Style.Standard = FormatStyle::LS_Cpp03; 12605 // cpp03 recognize this string as identifier u8 and literal character 'a' 12606 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 12607 } 12608 12609 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 12610 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 12611 // all modes, including C++11, C++14 and C++17 12612 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 12613 } 12614 12615 TEST_F(FormatTest, DoNotFormatLikelyXml) { 12616 EXPECT_EQ("<!-- ;> -->", 12617 format("<!-- ;> -->", getGoogleStyle())); 12618 EXPECT_EQ(" <!-- >; -->", 12619 format(" <!-- >; -->", getGoogleStyle())); 12620 } 12621 12622 TEST_F(FormatTest, StructuredBindings) { 12623 // Structured bindings is a C++17 feature. 12624 // all modes, including C++11, C++14 and C++17 12625 verifyFormat("auto [a, b] = f();"); 12626 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 12627 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 12628 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 12629 EXPECT_EQ("auto const volatile [a, b] = f();", 12630 format("auto const volatile[a, b] = f();")); 12631 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 12632 EXPECT_EQ("auto &[a, b, c] = f();", 12633 format("auto &[ a , b,c ] = f();")); 12634 EXPECT_EQ("auto &&[a, b, c] = f();", 12635 format("auto &&[ a , b,c ] = f();")); 12636 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 12637 EXPECT_EQ("auto const volatile &&[a, b] = f();", 12638 format("auto const volatile &&[a, b] = f();")); 12639 EXPECT_EQ("auto const &&[a, b] = f();", format("auto const && [a, b] = f();")); 12640 EXPECT_EQ("const auto &[a, b] = f();", format("const auto & [a, b] = f();")); 12641 EXPECT_EQ("const auto volatile &&[a, b] = f();", 12642 format("const auto volatile &&[a, b] = f();")); 12643 EXPECT_EQ("volatile const auto &&[a, b] = f();", 12644 format("volatile const auto &&[a, b] = f();")); 12645 EXPECT_EQ("const auto &&[a, b] = f();", format("const auto && [a, b] = f();")); 12646 12647 // Make sure we don't mistake structured bindings for lambdas. 12648 FormatStyle PointerMiddle = getLLVMStyle(); 12649 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 12650 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 12651 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 12652 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 12653 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 12654 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 12655 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 12656 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 12657 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 12658 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 12659 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 12660 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 12661 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 12662 12663 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 12664 format("for (const auto && [a, b] : some_range) {\n}")); 12665 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 12666 format("for (const auto & [a, b] : some_range) {\n}")); 12667 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 12668 format("for (const auto[a, b] : some_range) {\n}")); 12669 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 12670 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 12671 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 12672 EXPECT_EQ("auto const &[x, y](expr);", format("auto const & [x,y] (expr);")); 12673 EXPECT_EQ("auto const &&[x, y](expr);", format("auto const && [x,y] (expr);")); 12674 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 12675 EXPECT_EQ("auto const &[x, y]{expr};", format("auto const & [x,y] {expr};")); 12676 EXPECT_EQ("auto const &&[x, y]{expr};", format("auto const && [x,y] {expr};")); 12677 12678 format::FormatStyle Spaces = format::getLLVMStyle(); 12679 Spaces.SpacesInSquareBrackets = true; 12680 verifyFormat("auto [ a, b ] = f();", Spaces); 12681 verifyFormat("auto &&[ a, b ] = f();", Spaces); 12682 verifyFormat("auto &[ a, b ] = f();", Spaces); 12683 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 12684 verifyFormat("auto const &[ a, b ] = f();", Spaces); 12685 } 12686 12687 TEST_F(FormatTest, FileAndCode) { 12688 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 12689 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 12690 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 12691 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 12692 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n")); 12693 EXPECT_EQ( 12694 FormatStyle::LK_ObjC, 12695 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 12696 EXPECT_EQ(FormatStyle::LK_ObjC, 12697 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 12698 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 12699 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 12700 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n")); 12701 EXPECT_EQ(FormatStyle::LK_ObjC, 12702 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 12703 EXPECT_EQ( 12704 FormatStyle::LK_ObjC, 12705 guessLanguage("foo.h", 12706 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 12707 EXPECT_EQ( 12708 FormatStyle::LK_Cpp, 12709 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 12710 } 12711 12712 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 12713 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 12714 EXPECT_EQ(FormatStyle::LK_ObjC, 12715 guessLanguage("foo.h", "array[[calculator getIndex]];")); 12716 EXPECT_EQ(FormatStyle::LK_Cpp, 12717 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 12718 EXPECT_EQ( 12719 FormatStyle::LK_Cpp, 12720 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 12721 EXPECT_EQ(FormatStyle::LK_ObjC, 12722 guessLanguage("foo.h", "[[noreturn foo] bar];")); 12723 EXPECT_EQ(FormatStyle::LK_Cpp, 12724 guessLanguage("foo.h", "[[clang::fallthrough]];")); 12725 EXPECT_EQ(FormatStyle::LK_ObjC, 12726 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 12727 EXPECT_EQ(FormatStyle::LK_Cpp, 12728 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 12729 EXPECT_EQ(FormatStyle::LK_Cpp, 12730 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 12731 EXPECT_EQ(FormatStyle::LK_ObjC, 12732 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 12733 EXPECT_EQ(FormatStyle::LK_Cpp, 12734 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 12735 EXPECT_EQ( 12736 FormatStyle::LK_Cpp, 12737 guessLanguage("foo.h", 12738 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 12739 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 12740 } 12741 12742 TEST_F(FormatTest, GuessLanguageWithCaret) { 12743 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 12744 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 12745 EXPECT_EQ(FormatStyle::LK_ObjC, 12746 guessLanguage("foo.h", "int(^)(char, float);")); 12747 EXPECT_EQ(FormatStyle::LK_ObjC, 12748 guessLanguage("foo.h", "int(^foo)(char, float);")); 12749 EXPECT_EQ(FormatStyle::LK_ObjC, 12750 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 12751 EXPECT_EQ(FormatStyle::LK_ObjC, 12752 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 12753 EXPECT_EQ( 12754 FormatStyle::LK_ObjC, 12755 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 12756 } 12757 12758 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 12759 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 12760 "void f() {\n" 12761 " asm (\"mov %[e], %[d]\"\n" 12762 " : [d] \"=rm\" (d)\n" 12763 " [e] \"rm\" (*e));\n" 12764 "}")); 12765 EXPECT_EQ(FormatStyle::LK_Cpp, 12766 guessLanguage("foo.h", "void f() {\n" 12767 " asm volatile (\"mov %[e], %[d]\"\n" 12768 " : [d] \"=rm\" (d)\n" 12769 " [e] \"rm\" (*e));\n" 12770 "}")); 12771 } 12772 12773 TEST_F(FormatTest, GuessLanguageWithChildLines) { 12774 EXPECT_EQ(FormatStyle::LK_Cpp, 12775 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 12776 EXPECT_EQ(FormatStyle::LK_ObjC, 12777 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 12778 EXPECT_EQ( 12779 FormatStyle::LK_Cpp, 12780 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 12781 EXPECT_EQ( 12782 FormatStyle::LK_ObjC, 12783 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 12784 } 12785 12786 } // end namespace 12787 } // end namespace format 12788 } // end namespace clang 12789