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(aaaaa,\n" 3281 " {\n" 3282 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3283 " },\n" 3284 " a);", 3285 Style); 3286 3287 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3288 3289 verifyNoCrash("^{v^{a}}"); 3290 } 3291 3292 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3293 EXPECT_EQ("#define MACRO() \\\n" 3294 " Debug(aaa, /* force line break */ \\\n" 3295 " { \\\n" 3296 " int i; \\\n" 3297 " int j; \\\n" 3298 " })", 3299 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3300 " { int i; int j; })", 3301 getGoogleStyle())); 3302 3303 EXPECT_EQ("#define A \\\n" 3304 " [] { \\\n" 3305 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3306 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3307 " }", 3308 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3309 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3310 getGoogleStyle())); 3311 } 3312 3313 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3314 EXPECT_EQ("{}", format("{}")); 3315 verifyFormat("enum E {};"); 3316 verifyFormat("enum E {}"); 3317 } 3318 3319 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3320 FormatStyle Style = getLLVMStyle(); 3321 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3322 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3323 verifyFormat("FOO_BEGIN\n" 3324 " FOO_ENTRY\n" 3325 "FOO_END", Style); 3326 verifyFormat("FOO_BEGIN\n" 3327 " NESTED_FOO_BEGIN\n" 3328 " NESTED_FOO_ENTRY\n" 3329 " NESTED_FOO_END\n" 3330 "FOO_END", Style); 3331 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3332 " int x;\n" 3333 " x = 1;\n" 3334 "FOO_END(Baz)", Style); 3335 } 3336 3337 //===----------------------------------------------------------------------===// 3338 // Line break tests. 3339 //===----------------------------------------------------------------------===// 3340 3341 TEST_F(FormatTest, PreventConfusingIndents) { 3342 verifyFormat( 3343 "void f() {\n" 3344 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3345 " parameter, parameter, parameter)),\n" 3346 " SecondLongCall(parameter));\n" 3347 "}"); 3348 verifyFormat( 3349 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3350 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3351 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3352 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3353 verifyFormat( 3354 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3355 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3356 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3357 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3358 verifyFormat( 3359 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3360 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3362 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3363 verifyFormat("int a = bbbb && ccc &&\n" 3364 " fffff(\n" 3365 "#define A Just forcing a new line\n" 3366 " ddd);"); 3367 } 3368 3369 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3370 verifyFormat( 3371 "bool aaaaaaa =\n" 3372 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3373 " bbbbbbbb();"); 3374 verifyFormat( 3375 "bool aaaaaaa =\n" 3376 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3377 " bbbbbbbb();"); 3378 3379 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3380 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3381 " ccccccccc == ddddddddddd;"); 3382 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3383 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3384 " ccccccccc == ddddddddddd;"); 3385 verifyFormat( 3386 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3387 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3388 " ccccccccc == ddddddddddd;"); 3389 3390 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3391 " aaaaaa) &&\n" 3392 " bbbbbb && cccccc;"); 3393 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3394 " aaaaaa) >>\n" 3395 " bbbbbb;"); 3396 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3397 " SourceMgr.getSpellingColumnNumber(\n" 3398 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3399 " 1);"); 3400 3401 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3402 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3403 " cccccc) {\n}"); 3404 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3405 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 3406 " cccccc) {\n}"); 3407 verifyFormat("b = a &&\n" 3408 " // Comment\n" 3409 " b.c && d;"); 3410 3411 // If the LHS of a comparison is not a binary expression itself, the 3412 // additional linebreak confuses many people. 3413 verifyFormat( 3414 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3416 "}"); 3417 verifyFormat( 3418 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3420 "}"); 3421 verifyFormat( 3422 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3424 "}"); 3425 verifyFormat( 3426 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3427 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 3428 "}"); 3429 // Even explicit parentheses stress the precedence enough to make the 3430 // additional break unnecessary. 3431 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3433 "}"); 3434 // This cases is borderline, but with the indentation it is still readable. 3435 verifyFormat( 3436 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3437 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3438 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3439 "}", 3440 getLLVMStyleWithColumns(75)); 3441 3442 // If the LHS is a binary expression, we should still use the additional break 3443 // as otherwise the formatting hides the operator precedence. 3444 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3446 " 5) {\n" 3447 "}"); 3448 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3449 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 3450 " 5) {\n" 3451 "}"); 3452 3453 FormatStyle OnePerLine = getLLVMStyle(); 3454 OnePerLine.BinPackParameters = false; 3455 verifyFormat( 3456 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3457 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3458 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3459 OnePerLine); 3460 3461 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 3462 " .aaa(aaaaaaaaaaaaa) *\n" 3463 " aaaaaaa +\n" 3464 " aaaaaaa;", 3465 getLLVMStyleWithColumns(40)); 3466 } 3467 3468 TEST_F(FormatTest, ExpressionIndentation) { 3469 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3470 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3471 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3472 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3473 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3474 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3475 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3476 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3477 " ccccccccccccccccccccccccccccccccccccccccc;"); 3478 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3479 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3481 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3482 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3483 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3484 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3485 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3486 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3487 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3488 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3489 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3490 verifyFormat("if () {\n" 3491 "} else if (aaaaa && bbbbb > // break\n" 3492 " ccccc) {\n" 3493 "}"); 3494 verifyFormat("if () {\n" 3495 "} else if (aaaaa &&\n" 3496 " bbbbb > // break\n" 3497 " ccccc &&\n" 3498 " ddddd) {\n" 3499 "}"); 3500 3501 // Presence of a trailing comment used to change indentation of b. 3502 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3503 " b;\n" 3504 "return aaaaaaaaaaaaaaaaaaa +\n" 3505 " b; //", 3506 getLLVMStyleWithColumns(30)); 3507 } 3508 3509 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3510 // Not sure what the best system is here. Like this, the LHS can be found 3511 // immediately above an operator (everything with the same or a higher 3512 // indent). The RHS is aligned right of the operator and so compasses 3513 // everything until something with the same indent as the operator is found. 3514 // FIXME: Is this a good system? 3515 FormatStyle Style = getLLVMStyle(); 3516 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3517 verifyFormat( 3518 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3519 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3520 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3521 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3522 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3523 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3524 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3525 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3526 " > ccccccccccccccccccccccccccccccccccccccccc;", 3527 Style); 3528 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3529 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3530 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3531 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3532 Style); 3533 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3534 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3535 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3536 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3537 Style); 3538 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3539 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3540 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3541 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3542 Style); 3543 verifyFormat("if () {\n" 3544 "} else if (aaaaa\n" 3545 " && bbbbb // break\n" 3546 " > ccccc) {\n" 3547 "}", 3548 Style); 3549 verifyFormat("return (a)\n" 3550 " // comment\n" 3551 " + b;", 3552 Style); 3553 verifyFormat( 3554 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3555 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3556 " + cc;", 3557 Style); 3558 3559 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3560 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3561 Style); 3562 3563 // Forced by comments. 3564 verifyFormat( 3565 "unsigned ContentSize =\n" 3566 " sizeof(int16_t) // DWARF ARange version number\n" 3567 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3568 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3569 " + sizeof(int8_t); // Segment Size (in bytes)"); 3570 3571 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3572 " == boost::fusion::at_c<1>(iiii).second;", 3573 Style); 3574 3575 Style.ColumnLimit = 60; 3576 verifyFormat("zzzzzzzzzz\n" 3577 " = bbbbbbbbbbbbbbbbb\n" 3578 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3579 Style); 3580 3581 Style.ColumnLimit = 80; 3582 Style.IndentWidth = 4; 3583 Style.TabWidth = 4; 3584 Style.UseTab = FormatStyle::UT_Always; 3585 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3586 Style.AlignOperands = false; 3587 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 3588 "\t&& (someOtherLongishConditionPart1\n" 3589 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 3590 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && (someOtherLongishConditionPart1 || someOtherEvenLongerNestedConditionPart2);", 3591 Style)); 3592 } 3593 3594 TEST_F(FormatTest, EnforcedOperatorWraps) { 3595 // Here we'd like to wrap after the || operators, but a comment is forcing an 3596 // earlier wrap. 3597 verifyFormat("bool x = aaaaa //\n" 3598 " || bbbbb\n" 3599 " //\n" 3600 " || cccc;"); 3601 } 3602 3603 TEST_F(FormatTest, NoOperandAlignment) { 3604 FormatStyle Style = getLLVMStyle(); 3605 Style.AlignOperands = false; 3606 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 3607 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3608 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3609 Style); 3610 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3611 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3612 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3613 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3614 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3615 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3616 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3617 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3618 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3619 " > ccccccccccccccccccccccccccccccccccccccccc;", 3620 Style); 3621 3622 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3623 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3624 " + cc;", 3625 Style); 3626 verifyFormat("int a = aa\n" 3627 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3628 " * cccccccccccccccccccccccccccccccccccc;\n", 3629 Style); 3630 3631 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3632 verifyFormat("return (a > b\n" 3633 " // comment1\n" 3634 " // comment2\n" 3635 " || c);", 3636 Style); 3637 } 3638 3639 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3640 FormatStyle Style = getLLVMStyle(); 3641 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3642 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3643 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3644 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3645 Style); 3646 } 3647 3648 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 3649 FormatStyle Style = getLLVMStyle(); 3650 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3651 Style.BinPackArguments = false; 3652 Style.ColumnLimit = 40; 3653 verifyFormat("void test() {\n" 3654 " someFunction(\n" 3655 " this + argument + is + quite\n" 3656 " + long + so + it + gets + wrapped\n" 3657 " + but + remains + bin - packed);\n" 3658 "}", 3659 Style); 3660 verifyFormat("void test() {\n" 3661 " someFunction(arg1,\n" 3662 " this + argument + is\n" 3663 " + quite + long + so\n" 3664 " + it + gets + wrapped\n" 3665 " + but + remains + bin\n" 3666 " - packed,\n" 3667 " arg3);\n" 3668 "}", 3669 Style); 3670 verifyFormat("void test() {\n" 3671 " someFunction(\n" 3672 " arg1,\n" 3673 " this + argument + has\n" 3674 " + anotherFunc(nested,\n" 3675 " calls + whose\n" 3676 " + arguments\n" 3677 " + are + also\n" 3678 " + wrapped,\n" 3679 " in + addition)\n" 3680 " + to + being + bin - packed,\n" 3681 " arg3);\n" 3682 "}", 3683 Style); 3684 3685 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 3686 verifyFormat("void test() {\n" 3687 " someFunction(\n" 3688 " arg1,\n" 3689 " this + argument + has +\n" 3690 " anotherFunc(nested,\n" 3691 " calls + whose +\n" 3692 " arguments +\n" 3693 " are + also +\n" 3694 " wrapped,\n" 3695 " in + addition) +\n" 3696 " to + being + bin - packed,\n" 3697 " arg3);\n" 3698 "}", 3699 Style); 3700 } 3701 3702 TEST_F(FormatTest, ConstructorInitializers) { 3703 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3704 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3705 getLLVMStyleWithColumns(45)); 3706 verifyFormat("Constructor()\n" 3707 " : Inttializer(FitsOnTheLine) {}", 3708 getLLVMStyleWithColumns(44)); 3709 verifyFormat("Constructor()\n" 3710 " : Inttializer(FitsOnTheLine) {}", 3711 getLLVMStyleWithColumns(43)); 3712 3713 verifyFormat("template <typename T>\n" 3714 "Constructor() : Initializer(FitsOnTheLine) {}", 3715 getLLVMStyleWithColumns(45)); 3716 3717 verifyFormat( 3718 "SomeClass::Constructor()\n" 3719 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3720 3721 verifyFormat( 3722 "SomeClass::Constructor()\n" 3723 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3724 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3725 verifyFormat( 3726 "SomeClass::Constructor()\n" 3727 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3728 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3729 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3730 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3731 " : aaaaaaaaaa(aaaaaa) {}"); 3732 3733 verifyFormat("Constructor()\n" 3734 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3735 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3736 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3737 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3738 3739 verifyFormat("Constructor()\n" 3740 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3741 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3742 3743 verifyFormat("Constructor(int Parameter = 0)\n" 3744 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3745 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3746 verifyFormat("Constructor()\n" 3747 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3748 "}", 3749 getLLVMStyleWithColumns(60)); 3750 verifyFormat("Constructor()\n" 3751 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3752 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3753 3754 // Here a line could be saved by splitting the second initializer onto two 3755 // lines, but that is not desirable. 3756 verifyFormat("Constructor()\n" 3757 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3758 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3759 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3760 3761 FormatStyle OnePerLine = getLLVMStyle(); 3762 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3763 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3764 verifyFormat("SomeClass::Constructor()\n" 3765 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3766 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3767 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3768 OnePerLine); 3769 verifyFormat("SomeClass::Constructor()\n" 3770 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3771 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3772 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3773 OnePerLine); 3774 verifyFormat("MyClass::MyClass(int var)\n" 3775 " : some_var_(var), // 4 space indent\n" 3776 " some_other_var_(var + 1) { // lined up\n" 3777 "}", 3778 OnePerLine); 3779 verifyFormat("Constructor()\n" 3780 " : aaaaa(aaaaaa),\n" 3781 " aaaaa(aaaaaa),\n" 3782 " aaaaa(aaaaaa),\n" 3783 " aaaaa(aaaaaa),\n" 3784 " aaaaa(aaaaaa) {}", 3785 OnePerLine); 3786 verifyFormat("Constructor()\n" 3787 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3788 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3789 OnePerLine); 3790 OnePerLine.BinPackParameters = false; 3791 verifyFormat( 3792 "Constructor()\n" 3793 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3794 " aaaaaaaaaaa().aaa(),\n" 3795 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3796 OnePerLine); 3797 OnePerLine.ColumnLimit = 60; 3798 verifyFormat("Constructor()\n" 3799 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3800 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3801 OnePerLine); 3802 3803 EXPECT_EQ("Constructor()\n" 3804 " : // Comment forcing unwanted break.\n" 3805 " aaaa(aaaa) {}", 3806 format("Constructor() :\n" 3807 " // Comment forcing unwanted break.\n" 3808 " aaaa(aaaa) {}")); 3809 } 3810 3811 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 3812 FormatStyle Style = getLLVMStyle(); 3813 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 3814 3815 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3816 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 3817 getStyleWithColumns(Style, 45)); 3818 verifyFormat("Constructor() :\n" 3819 " Initializer(FitsOnTheLine) {}", 3820 getStyleWithColumns(Style, 44)); 3821 verifyFormat("Constructor() :\n" 3822 " Initializer(FitsOnTheLine) {}", 3823 getStyleWithColumns(Style, 43)); 3824 3825 verifyFormat("template <typename T>\n" 3826 "Constructor() : Initializer(FitsOnTheLine) {}", 3827 getStyleWithColumns(Style, 50)); 3828 3829 verifyFormat( 3830 "SomeClass::Constructor() :\n" 3831 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3832 Style); 3833 3834 verifyFormat( 3835 "SomeClass::Constructor() :\n" 3836 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3837 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3838 Style); 3839 verifyFormat( 3840 "SomeClass::Constructor() :\n" 3841 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3842 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3843 Style); 3844 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3845 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 3846 " aaaaaaaaaa(aaaaaa) {}", 3847 Style); 3848 3849 verifyFormat("Constructor() :\n" 3850 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3851 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3852 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3853 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 3854 Style); 3855 3856 verifyFormat("Constructor() :\n" 3857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3859 Style); 3860 3861 verifyFormat("Constructor(int Parameter = 0) :\n" 3862 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3863 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 3864 Style); 3865 verifyFormat("Constructor() :\n" 3866 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3867 "}", 3868 getStyleWithColumns(Style, 60)); 3869 verifyFormat("Constructor() :\n" 3870 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3871 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 3872 Style); 3873 3874 // Here a line could be saved by splitting the second initializer onto two 3875 // lines, but that is not desirable. 3876 verifyFormat("Constructor() :\n" 3877 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3878 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3879 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3880 Style); 3881 3882 FormatStyle OnePerLine = Style; 3883 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3884 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3885 verifyFormat("SomeClass::Constructor() :\n" 3886 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3887 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3888 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3889 OnePerLine); 3890 verifyFormat("SomeClass::Constructor() :\n" 3891 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3892 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3893 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3894 OnePerLine); 3895 verifyFormat("MyClass::MyClass(int var) :\n" 3896 " some_var_(var), // 4 space indent\n" 3897 " some_other_var_(var + 1) { // lined up\n" 3898 "}", 3899 OnePerLine); 3900 verifyFormat("Constructor() :\n" 3901 " aaaaa(aaaaaa),\n" 3902 " aaaaa(aaaaaa),\n" 3903 " aaaaa(aaaaaa),\n" 3904 " aaaaa(aaaaaa),\n" 3905 " aaaaa(aaaaaa) {}", 3906 OnePerLine); 3907 verifyFormat("Constructor() :\n" 3908 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3909 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3910 OnePerLine); 3911 OnePerLine.BinPackParameters = false; 3912 verifyFormat( 3913 "Constructor() :\n" 3914 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3915 " aaaaaaaaaaa().aaa(),\n" 3916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3917 OnePerLine); 3918 OnePerLine.ColumnLimit = 60; 3919 verifyFormat("Constructor() :\n" 3920 " aaaaaaaaaaaaaaaaaaaa(a),\n" 3921 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3922 OnePerLine); 3923 3924 EXPECT_EQ("Constructor() :\n" 3925 " // Comment forcing unwanted break.\n" 3926 " aaaa(aaaa) {}", 3927 format("Constructor() :\n" 3928 " // Comment forcing unwanted break.\n" 3929 " aaaa(aaaa) {}", 3930 Style)); 3931 3932 Style.ColumnLimit = 0; 3933 verifyFormat("SomeClass::Constructor() :\n" 3934 " a(a) {}", 3935 Style); 3936 verifyFormat("SomeClass::Constructor() noexcept :\n" 3937 " a(a) {}", 3938 Style); 3939 verifyFormat("SomeClass::Constructor() :\n" 3940 " a(a), b(b), c(c) {}", 3941 Style); 3942 verifyFormat("SomeClass::Constructor() :\n" 3943 " a(a) {\n" 3944 " foo();\n" 3945 " bar();\n" 3946 "}", 3947 Style); 3948 3949 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3950 verifyFormat("SomeClass::Constructor() :\n" 3951 " a(a), b(b), c(c) {\n" 3952 "}", 3953 Style); 3954 verifyFormat("SomeClass::Constructor() :\n" 3955 " a(a) {\n" 3956 "}", 3957 Style); 3958 3959 Style.ColumnLimit = 80; 3960 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3961 Style.ConstructorInitializerIndentWidth = 2; 3962 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 3963 Style); 3964 verifyFormat("SomeClass::Constructor() :\n" 3965 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3966 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 3967 Style); 3968 3969 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as well 3970 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 3971 verifyFormat("class SomeClass\n" 3972 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3973 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 3974 Style); 3975 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 3976 verifyFormat("class SomeClass\n" 3977 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3978 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 3979 Style); 3980 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 3981 verifyFormat("class SomeClass :\n" 3982 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3983 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 3984 Style); 3985 } 3986 3987 #ifndef EXPENSIVE_CHECKS 3988 // Expensive checks enables libstdc++ checking which includes validating the 3989 // state of ranges used in std::priority_queue - this blows out the 3990 // runtime/scalability of the function and makes this test unacceptably slow. 3991 TEST_F(FormatTest, MemoizationTests) { 3992 // This breaks if the memoization lookup does not take \c Indent and 3993 // \c LastSpace into account. 3994 verifyFormat( 3995 "extern CFRunLoopTimerRef\n" 3996 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3997 " CFTimeInterval interval, CFOptionFlags flags,\n" 3998 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3999 " CFRunLoopTimerContext *context) {}"); 4000 4001 // Deep nesting somewhat works around our memoization. 4002 verifyFormat( 4003 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4004 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4005 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4006 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4007 " aaaaa())))))))))))))))))))))))))))))))))))))));", 4008 getLLVMStyleWithColumns(65)); 4009 verifyFormat( 4010 "aaaaa(\n" 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))))))))))));", 4035 getLLVMStyleWithColumns(65)); 4036 verifyFormat( 4037 "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" 4038 " 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)", 4055 getLLVMStyleWithColumns(65)); 4056 4057 // This test takes VERY long when memoization is broken. 4058 FormatStyle OnePerLine = getLLVMStyle(); 4059 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4060 OnePerLine.BinPackParameters = false; 4061 std::string input = "Constructor()\n" 4062 " : aaaa(a,\n"; 4063 for (unsigned i = 0, e = 80; i != e; ++i) { 4064 input += " a,\n"; 4065 } 4066 input += " a) {}"; 4067 verifyFormat(input, OnePerLine); 4068 } 4069 #endif 4070 4071 TEST_F(FormatTest, BreaksAsHighAsPossible) { 4072 verifyFormat( 4073 "void f() {\n" 4074 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 4075 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 4076 " f();\n" 4077 "}"); 4078 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 4079 " Intervals[i - 1].getRange().getLast()) {\n}"); 4080 } 4081 4082 TEST_F(FormatTest, BreaksFunctionDeclarations) { 4083 // Principially, we break function declarations in a certain order: 4084 // 1) break amongst arguments. 4085 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 4086 " Cccccccccccccc cccccccccccccc);"); 4087 verifyFormat("template <class TemplateIt>\n" 4088 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 4089 " TemplateIt *stop) {}"); 4090 4091 // 2) break after return type. 4092 verifyFormat( 4093 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4094 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 4095 getGoogleStyle()); 4096 4097 // 3) break after (. 4098 verifyFormat( 4099 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 4100 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 4101 getGoogleStyle()); 4102 4103 // 4) break before after nested name specifiers. 4104 verifyFormat( 4105 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4106 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 4107 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 4108 getGoogleStyle()); 4109 4110 // However, there are exceptions, if a sufficient amount of lines can be 4111 // saved. 4112 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 4113 // more adjusting. 4114 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 4115 " Cccccccccccccc cccccccccc,\n" 4116 " Cccccccccccccc cccccccccc,\n" 4117 " Cccccccccccccc cccccccccc,\n" 4118 " Cccccccccccccc cccccccccc);"); 4119 verifyFormat( 4120 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4121 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4122 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4123 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 4124 getGoogleStyle()); 4125 verifyFormat( 4126 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 4127 " Cccccccccccccc cccccccccc,\n" 4128 " Cccccccccccccc cccccccccc,\n" 4129 " Cccccccccccccc cccccccccc,\n" 4130 " Cccccccccccccc cccccccccc,\n" 4131 " Cccccccccccccc cccccccccc,\n" 4132 " Cccccccccccccc cccccccccc);"); 4133 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4134 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4135 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4136 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4137 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 4138 4139 // Break after multi-line parameters. 4140 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4142 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4143 " bbbb bbbb);"); 4144 verifyFormat("void SomeLoooooooooooongFunction(\n" 4145 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 4146 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4147 " int bbbbbbbbbbbbb);"); 4148 4149 // Treat overloaded operators like other functions. 4150 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4151 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 4152 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4153 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 4154 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4155 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 4156 verifyGoogleFormat( 4157 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 4158 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 4159 verifyGoogleFormat( 4160 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 4161 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 4162 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4163 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 4164 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 4165 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 4166 verifyGoogleFormat( 4167 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 4168 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4169 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 4170 verifyGoogleFormat( 4171 "template <typename T>\n" 4172 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4173 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 4174 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 4175 4176 FormatStyle Style = getLLVMStyle(); 4177 Style.PointerAlignment = FormatStyle::PAS_Left; 4178 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4179 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 4180 Style); 4181 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 4182 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4183 Style); 4184 } 4185 4186 TEST_F(FormatTest, TrailingReturnType) { 4187 verifyFormat("auto foo() -> int;\n"); 4188 verifyFormat("struct S {\n" 4189 " auto bar() const -> int;\n" 4190 "};"); 4191 verifyFormat("template <size_t Order, typename T>\n" 4192 "auto load_img(const std::string &filename)\n" 4193 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 4194 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 4195 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 4196 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 4197 verifyFormat("template <typename T>\n" 4198 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 4199 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 4200 4201 // Not trailing return types. 4202 verifyFormat("void f() { auto a = b->c(); }"); 4203 } 4204 4205 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 4206 // Avoid breaking before trailing 'const' or other trailing annotations, if 4207 // they are not function-like. 4208 FormatStyle Style = getGoogleStyle(); 4209 Style.ColumnLimit = 47; 4210 verifyFormat("void someLongFunction(\n" 4211 " int someLoooooooooooooongParameter) const {\n}", 4212 getLLVMStyleWithColumns(47)); 4213 verifyFormat("LoooooongReturnType\n" 4214 "someLoooooooongFunction() const {}", 4215 getLLVMStyleWithColumns(47)); 4216 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 4217 " const {}", 4218 Style); 4219 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4220 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 4221 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4222 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 4223 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4224 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 4225 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 4226 " aaaaaaaaaaa aaaaa) const override;"); 4227 verifyGoogleFormat( 4228 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4229 " const override;"); 4230 4231 // Even if the first parameter has to be wrapped. 4232 verifyFormat("void someLongFunction(\n" 4233 " int someLongParameter) const {}", 4234 getLLVMStyleWithColumns(46)); 4235 verifyFormat("void someLongFunction(\n" 4236 " int someLongParameter) const {}", 4237 Style); 4238 verifyFormat("void someLongFunction(\n" 4239 " int someLongParameter) override {}", 4240 Style); 4241 verifyFormat("void someLongFunction(\n" 4242 " int someLongParameter) OVERRIDE {}", 4243 Style); 4244 verifyFormat("void someLongFunction(\n" 4245 " int someLongParameter) final {}", 4246 Style); 4247 verifyFormat("void someLongFunction(\n" 4248 " int someLongParameter) FINAL {}", 4249 Style); 4250 verifyFormat("void someLongFunction(\n" 4251 " int parameter) const override {}", 4252 Style); 4253 4254 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4255 verifyFormat("void someLongFunction(\n" 4256 " int someLongParameter) const\n" 4257 "{\n" 4258 "}", 4259 Style); 4260 4261 // Unless these are unknown annotations. 4262 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 4263 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4264 " LONG_AND_UGLY_ANNOTATION;"); 4265 4266 // Breaking before function-like trailing annotations is fine to keep them 4267 // close to their arguments. 4268 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4269 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4270 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4271 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4272 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4273 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 4274 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 4275 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 4276 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 4277 4278 verifyFormat( 4279 "void aaaaaaaaaaaaaaaaaa()\n" 4280 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 4281 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 4282 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4283 " __attribute__((unused));"); 4284 verifyGoogleFormat( 4285 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4286 " GUARDED_BY(aaaaaaaaaaaa);"); 4287 verifyGoogleFormat( 4288 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4289 " GUARDED_BY(aaaaaaaaaaaa);"); 4290 verifyGoogleFormat( 4291 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4292 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4293 verifyGoogleFormat( 4294 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4295 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4296 } 4297 4298 TEST_F(FormatTest, FunctionAnnotations) { 4299 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4300 "int OldFunction(const string ¶meter) {}"); 4301 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4302 "string OldFunction(const string ¶meter) {}"); 4303 verifyFormat("template <typename T>\n" 4304 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4305 "string OldFunction(const string ¶meter) {}"); 4306 4307 // Not function annotations. 4308 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4309 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4310 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4311 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4312 verifyFormat("MACRO(abc).function() // wrap\n" 4313 " << abc;"); 4314 verifyFormat("MACRO(abc)->function() // wrap\n" 4315 " << abc;"); 4316 verifyFormat("MACRO(abc)::function() // wrap\n" 4317 " << abc;"); 4318 } 4319 4320 TEST_F(FormatTest, BreaksDesireably) { 4321 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4322 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4323 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4324 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4325 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4326 "}"); 4327 4328 verifyFormat( 4329 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4330 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4331 4332 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4334 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4335 4336 verifyFormat( 4337 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4338 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4339 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4340 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4341 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4342 4343 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4344 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4345 4346 verifyFormat( 4347 "void f() {\n" 4348 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4349 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4350 "}"); 4351 verifyFormat( 4352 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4353 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4354 verifyFormat( 4355 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4356 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4357 verifyFormat( 4358 "aaaaaa(aaa,\n" 4359 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4361 " aaaa);"); 4362 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4363 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4365 4366 // Indent consistently independent of call expression and unary operator. 4367 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4368 " dddddddddddddddddddddddddddddd));"); 4369 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4370 " dddddddddddddddddddddddddddddd));"); 4371 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4372 " dddddddddddddddddddddddddddddd));"); 4373 4374 // This test case breaks on an incorrect memoization, i.e. an optimization not 4375 // taking into account the StopAt value. 4376 verifyFormat( 4377 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4378 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4379 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4380 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4381 4382 verifyFormat("{\n {\n {\n" 4383 " Annotation.SpaceRequiredBefore =\n" 4384 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4385 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4386 " }\n }\n}"); 4387 4388 // Break on an outer level if there was a break on an inner level. 4389 EXPECT_EQ("f(g(h(a, // comment\n" 4390 " b, c),\n" 4391 " d, e),\n" 4392 " x, y);", 4393 format("f(g(h(a, // comment\n" 4394 " b, c), d, e), x, y);")); 4395 4396 // Prefer breaking similar line breaks. 4397 verifyFormat( 4398 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4399 " NSTrackingMouseEnteredAndExited |\n" 4400 " NSTrackingActiveAlways;"); 4401 } 4402 4403 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4404 FormatStyle NoBinPacking = getGoogleStyle(); 4405 NoBinPacking.BinPackParameters = false; 4406 NoBinPacking.BinPackArguments = true; 4407 verifyFormat("void f() {\n" 4408 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4409 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4410 "}", 4411 NoBinPacking); 4412 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4413 " int aaaaaaaaaaaaaaaaaaaa,\n" 4414 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4415 NoBinPacking); 4416 4417 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4418 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4419 " vector<int> bbbbbbbbbbbbbbb);", 4420 NoBinPacking); 4421 // FIXME: This behavior difference is probably not wanted. However, currently 4422 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4423 // template arguments from BreakBeforeParameter being set because of the 4424 // one-per-line formatting. 4425 verifyFormat( 4426 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4427 " aaaaaaaaaa> aaaaaaaaaa);", 4428 NoBinPacking); 4429 verifyFormat( 4430 "void fffffffffff(\n" 4431 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4432 " aaaaaaaaaa);"); 4433 } 4434 4435 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4436 FormatStyle NoBinPacking = getGoogleStyle(); 4437 NoBinPacking.BinPackParameters = false; 4438 NoBinPacking.BinPackArguments = false; 4439 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4440 " aaaaaaaaaaaaaaaaaaaa,\n" 4441 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4442 NoBinPacking); 4443 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4444 " aaaaaaaaaaaaa,\n" 4445 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4446 NoBinPacking); 4447 verifyFormat( 4448 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4449 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4450 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4451 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4452 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4453 NoBinPacking); 4454 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4455 " .aaaaaaaaaaaaaaaaaa();", 4456 NoBinPacking); 4457 verifyFormat("void f() {\n" 4458 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4459 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4460 "}", 4461 NoBinPacking); 4462 4463 verifyFormat( 4464 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4465 " aaaaaaaaaaaa,\n" 4466 " aaaaaaaaaaaa);", 4467 NoBinPacking); 4468 verifyFormat( 4469 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4470 " ddddddddddddddddddddddddddddd),\n" 4471 " test);", 4472 NoBinPacking); 4473 4474 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4475 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4476 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4477 " aaaaaaaaaaaaaaaaaa;", 4478 NoBinPacking); 4479 verifyFormat("a(\"a\"\n" 4480 " \"a\",\n" 4481 " a);"); 4482 4483 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4484 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4485 " aaaaaaaaa,\n" 4486 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4487 NoBinPacking); 4488 verifyFormat( 4489 "void f() {\n" 4490 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4491 " .aaaaaaa();\n" 4492 "}", 4493 NoBinPacking); 4494 verifyFormat( 4495 "template <class SomeType, class SomeOtherType>\n" 4496 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4497 NoBinPacking); 4498 } 4499 4500 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4501 FormatStyle Style = getLLVMStyleWithColumns(15); 4502 Style.ExperimentalAutoDetectBinPacking = true; 4503 EXPECT_EQ("aaa(aaaa,\n" 4504 " aaaa,\n" 4505 " aaaa);\n" 4506 "aaa(aaaa,\n" 4507 " aaaa,\n" 4508 " aaaa);", 4509 format("aaa(aaaa,\n" // one-per-line 4510 " aaaa,\n" 4511 " aaaa );\n" 4512 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4513 Style)); 4514 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4515 " aaaa);\n" 4516 "aaa(aaaa, aaaa,\n" 4517 " aaaa);", 4518 format("aaa(aaaa, aaaa,\n" // bin-packed 4519 " aaaa );\n" 4520 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4521 Style)); 4522 } 4523 4524 TEST_F(FormatTest, FormatsBuilderPattern) { 4525 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4526 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4527 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4528 " .StartsWith(\".init\", ORDER_INIT)\n" 4529 " .StartsWith(\".fini\", ORDER_FINI)\n" 4530 " .StartsWith(\".hash\", ORDER_HASH)\n" 4531 " .Default(ORDER_TEXT);\n"); 4532 4533 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4534 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4535 verifyFormat( 4536 "aaaaaaa->aaaaaaa\n" 4537 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4538 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4539 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4540 verifyFormat( 4541 "aaaaaaa->aaaaaaa\n" 4542 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4543 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4544 verifyFormat( 4545 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4546 " aaaaaaaaaaaaaa);"); 4547 verifyFormat( 4548 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4549 " aaaaaa->aaaaaaaaaaaa()\n" 4550 " ->aaaaaaaaaaaaaaaa(\n" 4551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4552 " ->aaaaaaaaaaaaaaaaa();"); 4553 verifyGoogleFormat( 4554 "void f() {\n" 4555 " someo->Add((new util::filetools::Handler(dir))\n" 4556 " ->OnEvent1(NewPermanentCallback(\n" 4557 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4558 " ->OnEvent2(NewPermanentCallback(\n" 4559 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4560 " ->OnEvent3(NewPermanentCallback(\n" 4561 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4562 " ->OnEvent5(NewPermanentCallback(\n" 4563 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4564 " ->OnEvent6(NewPermanentCallback(\n" 4565 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4566 "}"); 4567 4568 verifyFormat( 4569 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4570 verifyFormat("aaaaaaaaaaaaaaa()\n" 4571 " .aaaaaaaaaaaaaaa()\n" 4572 " .aaaaaaaaaaaaaaa()\n" 4573 " .aaaaaaaaaaaaaaa()\n" 4574 " .aaaaaaaaaaaaaaa();"); 4575 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4576 " .aaaaaaaaaaaaaaa()\n" 4577 " .aaaaaaaaaaaaaaa()\n" 4578 " .aaaaaaaaaaaaaaa();"); 4579 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4580 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4581 " .aaaaaaaaaaaaaaa();"); 4582 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4583 " ->aaaaaaaaaaaaaae(0)\n" 4584 " ->aaaaaaaaaaaaaaa();"); 4585 4586 // Don't linewrap after very short segments. 4587 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4588 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4589 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4590 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4591 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4592 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4593 verifyFormat("aaa()\n" 4594 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4595 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4596 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4597 4598 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4599 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4600 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4601 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4602 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4603 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4604 4605 // Prefer not to break after empty parentheses. 4606 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4607 " First->LastNewlineOffset);"); 4608 4609 // Prefer not to create "hanging" indents. 4610 verifyFormat( 4611 "return !soooooooooooooome_map\n" 4612 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4613 " .second;"); 4614 verifyFormat( 4615 "return aaaaaaaaaaaaaaaa\n" 4616 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4617 " .aaaa(aaaaaaaaaaaaaa);"); 4618 // No hanging indent here. 4619 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4620 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4621 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4622 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4623 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4624 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4625 getLLVMStyleWithColumns(60)); 4626 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4627 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4628 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4629 getLLVMStyleWithColumns(59)); 4630 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4631 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4632 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4633 4634 // Dont break if only closing statements before member call 4635 verifyFormat("test() {\n" 4636 " ([]() -> {\n" 4637 " int b = 32;\n" 4638 " return 3;\n" 4639 " }).foo();\n" 4640 "}"); 4641 verifyFormat("test() {\n" 4642 " (\n" 4643 " []() -> {\n" 4644 " int b = 32;\n" 4645 " return 3;\n" 4646 " },\n" 4647 " foo, bar)\n" 4648 " .foo();\n" 4649 "}"); 4650 verifyFormat("test() {\n" 4651 " ([]() -> {\n" 4652 " int b = 32;\n" 4653 " return 3;\n" 4654 " })\n" 4655 " .foo()\n" 4656 " .bar();\n" 4657 "}"); 4658 verifyFormat("test() {\n" 4659 " ([]() -> {\n" 4660 " int b = 32;\n" 4661 " return 3;\n" 4662 " })\n" 4663 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 4664 " \"bbbb\");\n" 4665 "}", 4666 getLLVMStyleWithColumns(30)); 4667 } 4668 4669 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4670 verifyFormat( 4671 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4672 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4673 verifyFormat( 4674 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4675 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4676 4677 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4678 " ccccccccccccccccccccccccc) {\n}"); 4679 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4680 " ccccccccccccccccccccccccc) {\n}"); 4681 4682 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4683 " ccccccccccccccccccccccccc) {\n}"); 4684 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4685 " ccccccccccccccccccccccccc) {\n}"); 4686 4687 verifyFormat( 4688 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4689 " ccccccccccccccccccccccccc) {\n}"); 4690 verifyFormat( 4691 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4692 " ccccccccccccccccccccccccc) {\n}"); 4693 4694 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4695 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4696 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4697 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4698 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4699 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4700 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4701 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4702 4703 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4704 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4705 " aaaaaaaaaaaaaaa != aa) {\n}"); 4706 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4707 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4708 " aaaaaaaaaaaaaaa != aa) {\n}"); 4709 } 4710 4711 TEST_F(FormatTest, BreaksAfterAssignments) { 4712 verifyFormat( 4713 "unsigned Cost =\n" 4714 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4715 " SI->getPointerAddressSpaceee());\n"); 4716 verifyFormat( 4717 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4718 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4719 4720 verifyFormat( 4721 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4722 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4723 verifyFormat("unsigned OriginalStartColumn =\n" 4724 " SourceMgr.getSpellingColumnNumber(\n" 4725 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4726 " 1;"); 4727 } 4728 4729 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 4730 FormatStyle Style = getLLVMStyle(); 4731 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4732 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 4733 Style); 4734 4735 Style.PenaltyBreakAssignment = 20; 4736 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 4737 " cccccccccccccccccccccccccc;", 4738 Style); 4739 } 4740 4741 TEST_F(FormatTest, AlignsAfterAssignments) { 4742 verifyFormat( 4743 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4744 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4745 verifyFormat( 4746 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4747 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4748 verifyFormat( 4749 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4750 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4751 verifyFormat( 4752 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4753 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4754 verifyFormat( 4755 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4756 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4757 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4758 } 4759 4760 TEST_F(FormatTest, AlignsAfterReturn) { 4761 verifyFormat( 4762 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4763 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4764 verifyFormat( 4765 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4766 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4767 verifyFormat( 4768 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4769 " aaaaaaaaaaaaaaaaaaaaaa();"); 4770 verifyFormat( 4771 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4772 " aaaaaaaaaaaaaaaaaaaaaa());"); 4773 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4774 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4775 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4777 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4778 verifyFormat("return\n" 4779 " // true if code is one of a or b.\n" 4780 " code == a || code == b;"); 4781 } 4782 4783 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4784 verifyFormat( 4785 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4786 " aaaaaaaaa aaaaaaa) {}"); 4787 verifyFormat( 4788 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4789 " aaaaaaaaaaa aaaaaaaaa);"); 4790 verifyFormat( 4791 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4792 " aaaaaaaaaaaaaaaaaaaaa));"); 4793 FormatStyle Style = getLLVMStyle(); 4794 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4795 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4796 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4797 Style); 4798 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4799 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4800 Style); 4801 verifyFormat("SomeLongVariableName->someFunction(\n" 4802 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4803 Style); 4804 verifyFormat( 4805 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4806 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4807 Style); 4808 verifyFormat( 4809 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4810 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4811 Style); 4812 verifyFormat( 4813 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4814 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4815 Style); 4816 4817 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4818 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4819 " b));", 4820 Style); 4821 4822 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4823 Style.BinPackArguments = false; 4824 Style.BinPackParameters = false; 4825 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4826 " aaaaaaaaaaa aaaaaaaa,\n" 4827 " aaaaaaaaa aaaaaaa,\n" 4828 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4829 Style); 4830 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4831 " aaaaaaaaaaa aaaaaaaaa,\n" 4832 " aaaaaaaaaaa aaaaaaaaa,\n" 4833 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4834 Style); 4835 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4836 " aaaaaaaaaaaaaaa,\n" 4837 " aaaaaaaaaaaaaaaaaaaaa,\n" 4838 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4839 Style); 4840 verifyFormat( 4841 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4842 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4843 Style); 4844 verifyFormat( 4845 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4846 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4847 Style); 4848 verifyFormat( 4849 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4850 " aaaaaaaaaaaaaaaaaaaaa(\n" 4851 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4852 " aaaaaaaaaaaaaaaa);", 4853 Style); 4854 verifyFormat( 4855 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4856 " aaaaaaaaaaaaaaaaaaaaa(\n" 4857 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4858 " aaaaaaaaaaaaaaaa);", 4859 Style); 4860 } 4861 4862 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4863 FormatStyle Style = getLLVMStyleWithColumns(40); 4864 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4865 " bbbbbbbbbbbbbbbbbbbbbb);", 4866 Style); 4867 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4868 Style.AlignOperands = false; 4869 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4870 " bbbbbbbbbbbbbbbbbbbbbb);", 4871 Style); 4872 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4873 Style.AlignOperands = true; 4874 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4875 " bbbbbbbbbbbbbbbbbbbbbb);", 4876 Style); 4877 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4878 Style.AlignOperands = false; 4879 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4880 " bbbbbbbbbbbbbbbbbbbbbb);", 4881 Style); 4882 } 4883 4884 TEST_F(FormatTest, BreaksConditionalExpressions) { 4885 verifyFormat( 4886 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4887 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4888 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4889 verifyFormat( 4890 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4891 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4892 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4893 verifyFormat( 4894 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4895 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4896 verifyFormat( 4897 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 4898 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4899 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4900 verifyFormat( 4901 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4902 " : aaaaaaaaaaaaa);"); 4903 verifyFormat( 4904 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4905 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4906 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4907 " aaaaaaaaaaaaa);"); 4908 verifyFormat( 4909 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4910 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4911 " aaaaaaaaaaaaa);"); 4912 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4913 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4915 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4917 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4918 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4919 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4920 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4921 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4922 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4923 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4924 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4925 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4926 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4927 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4928 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4929 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4930 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4931 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4932 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4934 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4935 " : aaaaaaaaaaaaaaaa;"); 4936 verifyFormat( 4937 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4938 " ? aaaaaaaaaaaaaaa\n" 4939 " : aaaaaaaaaaaaaaa;"); 4940 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4941 " aaaaaaaaa\n" 4942 " ? b\n" 4943 " : c);"); 4944 verifyFormat("return aaaa == bbbb\n" 4945 " // comment\n" 4946 " ? aaaa\n" 4947 " : bbbb;"); 4948 verifyFormat("unsigned Indent =\n" 4949 " format(TheLine.First,\n" 4950 " IndentForLevel[TheLine.Level] >= 0\n" 4951 " ? IndentForLevel[TheLine.Level]\n" 4952 " : TheLine * 2,\n" 4953 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4954 getLLVMStyleWithColumns(60)); 4955 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4956 " ? aaaaaaaaaaaaaaa\n" 4957 " : bbbbbbbbbbbbbbb //\n" 4958 " ? ccccccccccccccc\n" 4959 " : ddddddddddddddd;"); 4960 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4961 " ? aaaaaaaaaaaaaaa\n" 4962 " : (bbbbbbbbbbbbbbb //\n" 4963 " ? ccccccccccccccc\n" 4964 " : ddddddddddddddd);"); 4965 verifyFormat( 4966 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4967 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4968 " aaaaaaaaaaaaaaaaaaaaa +\n" 4969 " aaaaaaaaaaaaaaaaaaaaa\n" 4970 " : aaaaaaaaaa;"); 4971 verifyFormat( 4972 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4973 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4974 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4975 4976 FormatStyle NoBinPacking = getLLVMStyle(); 4977 NoBinPacking.BinPackArguments = false; 4978 verifyFormat( 4979 "void f() {\n" 4980 " g(aaa,\n" 4981 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4982 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4983 " ? aaaaaaaaaaaaaaa\n" 4984 " : aaaaaaaaaaaaaaa);\n" 4985 "}", 4986 NoBinPacking); 4987 verifyFormat( 4988 "void f() {\n" 4989 " g(aaa,\n" 4990 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4991 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4992 " ?: aaaaaaaaaaaaaaa);\n" 4993 "}", 4994 NoBinPacking); 4995 4996 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4997 " // comment.\n" 4998 " ccccccccccccccccccccccccccccccccccccccc\n" 4999 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5000 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 5001 5002 // Assignments in conditional expressions. Apparently not uncommon :-(. 5003 verifyFormat("return a != b\n" 5004 " // comment\n" 5005 " ? a = b\n" 5006 " : a = b;"); 5007 verifyFormat("return a != b\n" 5008 " // comment\n" 5009 " ? a = a != b\n" 5010 " // comment\n" 5011 " ? a = b\n" 5012 " : a\n" 5013 " : a;\n"); 5014 verifyFormat("return a != b\n" 5015 " // comment\n" 5016 " ? a\n" 5017 " : a = a != b\n" 5018 " // comment\n" 5019 " ? a = b\n" 5020 " : a;"); 5021 } 5022 5023 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 5024 FormatStyle Style = getLLVMStyle(); 5025 Style.BreakBeforeTernaryOperators = false; 5026 Style.ColumnLimit = 70; 5027 verifyFormat( 5028 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5029 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5030 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5031 Style); 5032 verifyFormat( 5033 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 5034 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5036 Style); 5037 verifyFormat( 5038 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5040 Style); 5041 verifyFormat( 5042 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 5043 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5045 Style); 5046 verifyFormat( 5047 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 5048 " aaaaaaaaaaaaa);", 5049 Style); 5050 verifyFormat( 5051 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5052 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5053 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5054 " aaaaaaaaaaaaa);", 5055 Style); 5056 verifyFormat( 5057 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5058 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5059 " aaaaaaaaaaaaa);", 5060 Style); 5061 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5064 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5066 Style); 5067 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5068 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5069 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5071 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5072 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5073 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5074 Style); 5075 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 5077 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5079 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5080 Style); 5081 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5082 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5083 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5084 Style); 5085 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 5086 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5087 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5088 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5089 Style); 5090 verifyFormat( 5091 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5092 " aaaaaaaaaaaaaaa :\n" 5093 " aaaaaaaaaaaaaaa;", 5094 Style); 5095 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 5096 " aaaaaaaaa ?\n" 5097 " b :\n" 5098 " c);", 5099 Style); 5100 verifyFormat("unsigned Indent =\n" 5101 " format(TheLine.First,\n" 5102 " IndentForLevel[TheLine.Level] >= 0 ?\n" 5103 " IndentForLevel[TheLine.Level] :\n" 5104 " TheLine * 2,\n" 5105 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 5106 Style); 5107 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 5108 " aaaaaaaaaaaaaaa :\n" 5109 " bbbbbbbbbbbbbbb ? //\n" 5110 " ccccccccccccccc :\n" 5111 " ddddddddddddddd;", 5112 Style); 5113 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 5114 " aaaaaaaaaaaaaaa :\n" 5115 " (bbbbbbbbbbbbbbb ? //\n" 5116 " ccccccccccccccc :\n" 5117 " ddddddddddddddd);", 5118 Style); 5119 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5120 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 5121 " ccccccccccccccccccccccccccc;", 5122 Style); 5123 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5124 " aaaaa :\n" 5125 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 5126 Style); 5127 } 5128 5129 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 5130 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 5131 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 5132 verifyFormat("bool a = true, b = false;"); 5133 5134 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5135 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 5136 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 5137 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 5138 verifyFormat( 5139 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5140 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 5141 " d = e && f;"); 5142 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 5143 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 5144 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 5145 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 5146 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 5147 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 5148 5149 FormatStyle Style = getGoogleStyle(); 5150 Style.PointerAlignment = FormatStyle::PAS_Left; 5151 Style.DerivePointerAlignment = false; 5152 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5153 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 5154 " *b = bbbbbbbbbbbbbbbbbbb;", 5155 Style); 5156 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 5157 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 5158 Style); 5159 verifyFormat("vector<int*> a, b;", Style); 5160 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 5161 } 5162 5163 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 5164 verifyFormat("arr[foo ? bar : baz];"); 5165 verifyFormat("f()[foo ? bar : baz];"); 5166 verifyFormat("(a + b)[foo ? bar : baz];"); 5167 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 5168 } 5169 5170 TEST_F(FormatTest, AlignsStringLiterals) { 5171 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 5172 " \"short literal\");"); 5173 verifyFormat( 5174 "looooooooooooooooooooooooongFunction(\n" 5175 " \"short literal\"\n" 5176 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 5177 verifyFormat("someFunction(\"Always break between multi-line\"\n" 5178 " \" string literals\",\n" 5179 " and, other, parameters);"); 5180 EXPECT_EQ("fun + \"1243\" /* comment */\n" 5181 " \"5678\";", 5182 format("fun + \"1243\" /* comment */\n" 5183 " \"5678\";", 5184 getLLVMStyleWithColumns(28))); 5185 EXPECT_EQ( 5186 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 5187 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 5188 " \"aaaaaaaaaaaaaaaa\";", 5189 format("aaaaaa =" 5190 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 5191 "aaaaaaaaaaaaaaaaaaaaa\" " 5192 "\"aaaaaaaaaaaaaaaa\";")); 5193 verifyFormat("a = a + \"a\"\n" 5194 " \"a\"\n" 5195 " \"a\";"); 5196 verifyFormat("f(\"a\", \"b\"\n" 5197 " \"c\");"); 5198 5199 verifyFormat( 5200 "#define LL_FORMAT \"ll\"\n" 5201 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 5202 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 5203 5204 verifyFormat("#define A(X) \\\n" 5205 " \"aaaaa\" #X \"bbbbbb\" \\\n" 5206 " \"ccccc\"", 5207 getLLVMStyleWithColumns(23)); 5208 verifyFormat("#define A \"def\"\n" 5209 "f(\"abc\" A \"ghi\"\n" 5210 " \"jkl\");"); 5211 5212 verifyFormat("f(L\"a\"\n" 5213 " L\"b\");"); 5214 verifyFormat("#define A(X) \\\n" 5215 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 5216 " L\"ccccc\"", 5217 getLLVMStyleWithColumns(25)); 5218 5219 verifyFormat("f(@\"a\"\n" 5220 " @\"b\");"); 5221 verifyFormat("NSString s = @\"a\"\n" 5222 " @\"b\"\n" 5223 " @\"c\";"); 5224 verifyFormat("NSString s = @\"a\"\n" 5225 " \"b\"\n" 5226 " \"c\";"); 5227 } 5228 5229 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 5230 FormatStyle Style = getLLVMStyle(); 5231 // No declarations or definitions should be moved to own line. 5232 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 5233 verifyFormat("class A {\n" 5234 " int f() { return 1; }\n" 5235 " int g();\n" 5236 "};\n" 5237 "int f() { return 1; }\n" 5238 "int g();\n", 5239 Style); 5240 5241 // All declarations and definitions should have the return type moved to its 5242 // own 5243 // line. 5244 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 5245 verifyFormat("class E {\n" 5246 " int\n" 5247 " f() {\n" 5248 " return 1;\n" 5249 " }\n" 5250 " int\n" 5251 " g();\n" 5252 "};\n" 5253 "int\n" 5254 "f() {\n" 5255 " return 1;\n" 5256 "}\n" 5257 "int\n" 5258 "g();\n", 5259 Style); 5260 5261 // Top-level definitions, and no kinds of declarations should have the 5262 // return type moved to its own line. 5263 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 5264 verifyFormat("class B {\n" 5265 " int f() { return 1; }\n" 5266 " int g();\n" 5267 "};\n" 5268 "int\n" 5269 "f() {\n" 5270 " return 1;\n" 5271 "}\n" 5272 "int g();\n", 5273 Style); 5274 5275 // Top-level definitions and declarations should have the return type moved 5276 // to its own line. 5277 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 5278 verifyFormat("class C {\n" 5279 " int f() { return 1; }\n" 5280 " int g();\n" 5281 "};\n" 5282 "int\n" 5283 "f() {\n" 5284 " return 1;\n" 5285 "}\n" 5286 "int\n" 5287 "g();\n", 5288 Style); 5289 5290 // All definitions should have the return type moved to its own line, but no 5291 // kinds of declarations. 5292 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5293 verifyFormat("class D {\n" 5294 " int\n" 5295 " f() {\n" 5296 " return 1;\n" 5297 " }\n" 5298 " int g();\n" 5299 "};\n" 5300 "int\n" 5301 "f() {\n" 5302 " return 1;\n" 5303 "}\n" 5304 "int g();\n", 5305 Style); 5306 verifyFormat("const char *\n" 5307 "f(void) {\n" // Break here. 5308 " return \"\";\n" 5309 "}\n" 5310 "const char *bar(void);\n", // No break here. 5311 Style); 5312 verifyFormat("template <class T>\n" 5313 "T *\n" 5314 "f(T &c) {\n" // Break here. 5315 " return NULL;\n" 5316 "}\n" 5317 "template <class T> T *f(T &c);\n", // No break here. 5318 Style); 5319 verifyFormat("class C {\n" 5320 " int\n" 5321 " operator+() {\n" 5322 " return 1;\n" 5323 " }\n" 5324 " int\n" 5325 " operator()() {\n" 5326 " return 1;\n" 5327 " }\n" 5328 "};\n", 5329 Style); 5330 verifyFormat("void\n" 5331 "A::operator()() {}\n" 5332 "void\n" 5333 "A::operator>>() {}\n" 5334 "void\n" 5335 "A::operator+() {}\n", 5336 Style); 5337 verifyFormat("void *operator new(std::size_t s);", // No break here. 5338 Style); 5339 verifyFormat("void *\n" 5340 "operator new(std::size_t s) {}", 5341 Style); 5342 verifyFormat("void *\n" 5343 "operator delete[](void *ptr) {}", 5344 Style); 5345 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 5346 verifyFormat("const char *\n" 5347 "f(void)\n" // Break here. 5348 "{\n" 5349 " return \"\";\n" 5350 "}\n" 5351 "const char *bar(void);\n", // No break here. 5352 Style); 5353 verifyFormat("template <class T>\n" 5354 "T *\n" // Problem here: no line break 5355 "f(T &c)\n" // Break here. 5356 "{\n" 5357 " return NULL;\n" 5358 "}\n" 5359 "template <class T> T *f(T &c);\n", // No break here. 5360 Style); 5361 } 5362 5363 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 5364 FormatStyle NoBreak = getLLVMStyle(); 5365 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 5366 FormatStyle Break = getLLVMStyle(); 5367 Break.AlwaysBreakBeforeMultilineStrings = true; 5368 verifyFormat("aaaa = \"bbbb\"\n" 5369 " \"cccc\";", 5370 NoBreak); 5371 verifyFormat("aaaa =\n" 5372 " \"bbbb\"\n" 5373 " \"cccc\";", 5374 Break); 5375 verifyFormat("aaaa(\"bbbb\"\n" 5376 " \"cccc\");", 5377 NoBreak); 5378 verifyFormat("aaaa(\n" 5379 " \"bbbb\"\n" 5380 " \"cccc\");", 5381 Break); 5382 verifyFormat("aaaa(qqq, \"bbbb\"\n" 5383 " \"cccc\");", 5384 NoBreak); 5385 verifyFormat("aaaa(qqq,\n" 5386 " \"bbbb\"\n" 5387 " \"cccc\");", 5388 Break); 5389 verifyFormat("aaaa(qqq,\n" 5390 " L\"bbbb\"\n" 5391 " L\"cccc\");", 5392 Break); 5393 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 5394 " \"bbbb\"));", 5395 Break); 5396 verifyFormat("string s = someFunction(\n" 5397 " \"abc\"\n" 5398 " \"abc\");", 5399 Break); 5400 5401 // As we break before unary operators, breaking right after them is bad. 5402 verifyFormat("string foo = abc ? \"x\"\n" 5403 " \"blah blah blah blah blah blah\"\n" 5404 " : \"y\";", 5405 Break); 5406 5407 // Don't break if there is no column gain. 5408 verifyFormat("f(\"aaaa\"\n" 5409 " \"bbbb\");", 5410 Break); 5411 5412 // Treat literals with escaped newlines like multi-line string literals. 5413 EXPECT_EQ("x = \"a\\\n" 5414 "b\\\n" 5415 "c\";", 5416 format("x = \"a\\\n" 5417 "b\\\n" 5418 "c\";", 5419 NoBreak)); 5420 EXPECT_EQ("xxxx =\n" 5421 " \"a\\\n" 5422 "b\\\n" 5423 "c\";", 5424 format("xxxx = \"a\\\n" 5425 "b\\\n" 5426 "c\";", 5427 Break)); 5428 5429 EXPECT_EQ("NSString *const kString =\n" 5430 " @\"aaaa\"\n" 5431 " @\"bbbb\";", 5432 format("NSString *const kString = @\"aaaa\"\n" 5433 "@\"bbbb\";", 5434 Break)); 5435 5436 Break.ColumnLimit = 0; 5437 verifyFormat("const char *hello = \"hello llvm\";", Break); 5438 } 5439 5440 TEST_F(FormatTest, AlignsPipes) { 5441 verifyFormat( 5442 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5443 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5444 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5445 verifyFormat( 5446 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5447 " << aaaaaaaaaaaaaaaaaaaa;"); 5448 verifyFormat( 5449 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5450 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5451 verifyFormat( 5452 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5453 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5454 verifyFormat( 5455 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5456 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5457 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5458 verifyFormat( 5459 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5460 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5461 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5462 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5464 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5465 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5466 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5467 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5468 verifyFormat( 5469 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5470 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5471 verifyFormat( 5472 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 5473 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5474 5475 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5476 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5477 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5478 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5479 " aaaaaaaaaaaaaaaaaaaaa)\n" 5480 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5481 verifyFormat("LOG_IF(aaa == //\n" 5482 " bbb)\n" 5483 " << a << b;"); 5484 5485 // But sometimes, breaking before the first "<<" is desirable. 5486 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5487 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5488 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5489 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5490 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5491 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5492 " << BEF << IsTemplate << Description << E->getType();"); 5493 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5494 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5496 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5497 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5498 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5499 " << aaa;"); 5500 5501 verifyFormat( 5502 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5503 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5504 5505 // Incomplete string literal. 5506 EXPECT_EQ("llvm::errs() << \"\n" 5507 " << a;", 5508 format("llvm::errs() << \"\n<<a;")); 5509 5510 verifyFormat("void f() {\n" 5511 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5512 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5513 "}"); 5514 5515 // Handle 'endl'. 5516 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5517 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5518 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5519 5520 // Handle '\n'. 5521 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5522 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5523 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5524 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5525 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5526 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5527 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5528 } 5529 5530 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 5531 verifyFormat("return out << \"somepacket = {\\n\"\n" 5532 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5533 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5534 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5535 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5536 " << \"}\";"); 5537 5538 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5539 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5540 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5541 verifyFormat( 5542 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5543 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5544 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5545 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5546 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5547 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5548 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5549 verifyFormat( 5550 "void f() {\n" 5551 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5552 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5553 "}"); 5554 5555 // Breaking before the first "<<" is generally not desirable. 5556 verifyFormat( 5557 "llvm::errs()\n" 5558 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5559 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5560 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5561 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5562 getLLVMStyleWithColumns(70)); 5563 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5564 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5565 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5566 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5567 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5568 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5569 getLLVMStyleWithColumns(70)); 5570 5571 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5572 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5573 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 5574 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5575 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5576 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 5577 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 5578 " (aaaa + aaaa);", 5579 getLLVMStyleWithColumns(40)); 5580 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 5581 " (aaaaaaa + aaaaa));", 5582 getLLVMStyleWithColumns(40)); 5583 verifyFormat( 5584 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 5585 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 5586 " bbbbbbbbbbbbbbbbbbbbbbb);"); 5587 } 5588 5589 TEST_F(FormatTest, UnderstandsEquals) { 5590 verifyFormat( 5591 "aaaaaaaaaaaaaaaaa =\n" 5592 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5593 verifyFormat( 5594 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5595 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5596 verifyFormat( 5597 "if (a) {\n" 5598 " f();\n" 5599 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5600 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5601 "}"); 5602 5603 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5604 " 100000000 + 10000000) {\n}"); 5605 } 5606 5607 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5608 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5609 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5610 5611 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5612 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5613 5614 verifyFormat( 5615 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5616 " Parameter2);"); 5617 5618 verifyFormat( 5619 "ShortObject->shortFunction(\n" 5620 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5621 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5622 5623 verifyFormat("loooooooooooooongFunction(\n" 5624 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5625 5626 verifyFormat( 5627 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5628 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5629 5630 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5631 " .WillRepeatedly(Return(SomeValue));"); 5632 verifyFormat("void f() {\n" 5633 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5634 " .Times(2)\n" 5635 " .WillRepeatedly(Return(SomeValue));\n" 5636 "}"); 5637 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5638 " ccccccccccccccccccccccc);"); 5639 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5640 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5641 " .aaaaa(aaaaa),\n" 5642 " aaaaaaaaaaaaaaaaaaaaa);"); 5643 verifyFormat("void f() {\n" 5644 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5645 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5646 "}"); 5647 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5648 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5649 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5650 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5651 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5652 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5653 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5654 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5655 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5656 "}"); 5657 5658 // Here, it is not necessary to wrap at "." or "->". 5659 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5660 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5661 verifyFormat( 5662 "aaaaaaaaaaa->aaaaaaaaa(\n" 5663 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5664 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5665 5666 verifyFormat( 5667 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5668 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5669 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5670 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5671 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5672 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5673 5674 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5675 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5676 " .a();"); 5677 5678 FormatStyle NoBinPacking = getLLVMStyle(); 5679 NoBinPacking.BinPackParameters = false; 5680 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5681 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5682 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5683 " aaaaaaaaaaaaaaaaaaa,\n" 5684 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5685 NoBinPacking); 5686 5687 // If there is a subsequent call, change to hanging indentation. 5688 verifyFormat( 5689 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5690 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5691 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5692 verifyFormat( 5693 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5694 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5695 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5696 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5697 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5698 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5699 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5700 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5701 } 5702 5703 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5704 verifyFormat("template <typename T>\n" 5705 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5706 verifyFormat("template <typename T>\n" 5707 "// T should be one of {A, B}.\n" 5708 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5709 verifyFormat( 5710 "template <typename T>\n" 5711 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5712 verifyFormat("template <typename T>\n" 5713 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5714 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5715 verifyFormat( 5716 "template <typename T>\n" 5717 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5718 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5719 verifyFormat( 5720 "template <typename T>\n" 5721 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5722 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5723 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5724 verifyFormat("template <typename T>\n" 5725 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5726 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5727 verifyFormat( 5728 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5729 " typename T4 = char>\n" 5730 "void f();"); 5731 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5732 " template <typename> class cccccccccccccccccccccc,\n" 5733 " typename ddddddddddddd>\n" 5734 "class C {};"); 5735 verifyFormat( 5736 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5737 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5738 5739 verifyFormat("void f() {\n" 5740 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5741 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5742 "}"); 5743 5744 verifyFormat("template <typename T> class C {};"); 5745 verifyFormat("template <typename T> void f();"); 5746 verifyFormat("template <typename T> void f() {}"); 5747 verifyFormat( 5748 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5750 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5751 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5752 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5754 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5755 getLLVMStyleWithColumns(72)); 5756 EXPECT_EQ("static_cast<A< //\n" 5757 " B> *>(\n" 5758 "\n" 5759 ");", 5760 format("static_cast<A<//\n" 5761 " B>*>(\n" 5762 "\n" 5763 " );")); 5764 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5765 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5766 5767 FormatStyle AlwaysBreak = getLLVMStyle(); 5768 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 5769 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5770 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5771 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5772 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5773 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5774 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5775 verifyFormat("template <template <typename> class Fooooooo,\n" 5776 " template <typename> class Baaaaaaar>\n" 5777 "struct C {};", 5778 AlwaysBreak); 5779 verifyFormat("template <typename T> // T can be A, B or C.\n" 5780 "struct C {};", 5781 AlwaysBreak); 5782 verifyFormat("template <enum E> class A {\n" 5783 "public:\n" 5784 " E *f();\n" 5785 "};"); 5786 5787 FormatStyle NeverBreak = getLLVMStyle(); 5788 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 5789 verifyFormat("template <typename T> class C {};", NeverBreak); 5790 verifyFormat("template <typename T> void f();", NeverBreak); 5791 verifyFormat("template <typename T> void f() {}", NeverBreak); 5792 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}", 5793 NeverBreak); 5794 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5795 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5796 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 5797 NeverBreak); 5798 verifyFormat("template <template <typename> class Fooooooo,\n" 5799 " template <typename> class Baaaaaaar>\n" 5800 "struct C {};", 5801 NeverBreak); 5802 verifyFormat("template <typename T> // T can be A, B or C.\n" 5803 "struct C {};", 5804 NeverBreak); 5805 verifyFormat("template <enum E> class A {\n" 5806 "public:\n" 5807 " E *f();\n" 5808 "};", NeverBreak); 5809 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 5810 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}", 5811 NeverBreak); 5812 } 5813 5814 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 5815 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 5816 Style.ColumnLimit = 60; 5817 EXPECT_EQ("// Baseline - no comments.\n" 5818 "template <\n" 5819 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 5820 "void f() {}", 5821 format("// Baseline - no comments.\n" 5822 "template <\n" 5823 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 5824 "void f() {}", 5825 Style)); 5826 5827 EXPECT_EQ("template <\n" 5828 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 5829 "void f() {}", 5830 format("template <\n" 5831 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 5832 "void f() {}", 5833 Style)); 5834 5835 EXPECT_EQ( 5836 "template <\n" 5837 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 5838 "void f() {}", 5839 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 5840 "void f() {}", 5841 Style)); 5842 5843 EXPECT_EQ( 5844 "template <\n" 5845 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 5846 " // multiline\n" 5847 "void f() {}", 5848 format("template <\n" 5849 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 5850 " // multiline\n" 5851 "void f() {}", 5852 Style)); 5853 5854 EXPECT_EQ( 5855 "template <typename aaaaaaaaaa<\n" 5856 " bbbbbbbbbbbb>::value> // trailing loooong\n" 5857 "void f() {}", 5858 format( 5859 "template <\n" 5860 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 5861 "void f() {}", 5862 Style)); 5863 } 5864 5865 TEST_F(FormatTest, WrapsTemplateParameters) { 5866 FormatStyle Style = getLLVMStyle(); 5867 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5868 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5869 verifyFormat( 5870 "template <typename... a> struct q {};\n" 5871 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5872 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5873 " y;", 5874 Style); 5875 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5876 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5877 verifyFormat( 5878 "template <typename... a> struct r {};\n" 5879 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5880 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5881 " y;", 5882 Style); 5883 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5884 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5885 verifyFormat( 5886 "template <typename... a> struct s {};\n" 5887 "extern s<\n" 5888 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5889 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5890 " y;", 5891 Style); 5892 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5893 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5894 verifyFormat( 5895 "template <typename... a> struct t {};\n" 5896 "extern t<\n" 5897 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5898 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5899 " y;", 5900 Style); 5901 } 5902 5903 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5904 verifyFormat( 5905 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5906 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5907 verifyFormat( 5908 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5909 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5910 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5911 5912 // FIXME: Should we have the extra indent after the second break? 5913 verifyFormat( 5914 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5917 5918 verifyFormat( 5919 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5920 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5921 5922 // Breaking at nested name specifiers is generally not desirable. 5923 verifyFormat( 5924 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5925 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5926 5927 verifyFormat( 5928 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 5929 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5930 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5931 " aaaaaaaaaaaaaaaaaaaaa);", 5932 getLLVMStyleWithColumns(74)); 5933 5934 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5935 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5936 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5937 } 5938 5939 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5940 verifyFormat("A<int> a;"); 5941 verifyFormat("A<A<A<int>>> a;"); 5942 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5943 verifyFormat("bool x = a < 1 || 2 > a;"); 5944 verifyFormat("bool x = 5 < f<int>();"); 5945 verifyFormat("bool x = f<int>() > 5;"); 5946 verifyFormat("bool x = 5 < a<int>::x;"); 5947 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5948 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5949 5950 verifyGoogleFormat("A<A<int>> a;"); 5951 verifyGoogleFormat("A<A<A<int>>> a;"); 5952 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5953 verifyGoogleFormat("A<A<int> > a;"); 5954 verifyGoogleFormat("A<A<A<int> > > a;"); 5955 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5956 verifyGoogleFormat("A<::A<int>> a;"); 5957 verifyGoogleFormat("A<::A> a;"); 5958 verifyGoogleFormat("A< ::A> a;"); 5959 verifyGoogleFormat("A< ::A<int> > a;"); 5960 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5961 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5962 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5963 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5964 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5965 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5966 5967 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5968 5969 verifyFormat("test >> a >> b;"); 5970 verifyFormat("test << a >> b;"); 5971 5972 verifyFormat("f<int>();"); 5973 verifyFormat("template <typename T> void f() {}"); 5974 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5975 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5976 "sizeof(char)>::type>;"); 5977 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5978 verifyFormat("f(a.operator()<A>());"); 5979 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5980 " .template operator()<A>());", 5981 getLLVMStyleWithColumns(35)); 5982 5983 // Not template parameters. 5984 verifyFormat("return a < b && c > d;"); 5985 verifyFormat("void f() {\n" 5986 " while (a < b && c > d) {\n" 5987 " }\n" 5988 "}"); 5989 verifyFormat("template <typename... Types>\n" 5990 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5991 5992 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5993 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5994 getLLVMStyleWithColumns(60)); 5995 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5996 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5997 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5998 } 5999 6000 TEST_F(FormatTest, BitshiftOperatorWidth) { 6001 EXPECT_EQ("int a = 1 << 2; /* foo\n" 6002 " bar */", 6003 format("int a=1<<2; /* foo\n" 6004 " bar */")); 6005 6006 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 6007 " bar */", 6008 format("int b =256>>1 ; /* foo\n" 6009 " bar */")); 6010 } 6011 6012 TEST_F(FormatTest, UnderstandsBinaryOperators) { 6013 verifyFormat("COMPARE(a, ==, b);"); 6014 verifyFormat("auto s = sizeof...(Ts) - 1;"); 6015 } 6016 6017 TEST_F(FormatTest, UnderstandsPointersToMembers) { 6018 verifyFormat("int A::*x;"); 6019 verifyFormat("int (S::*func)(void *);"); 6020 verifyFormat("void f() { int (S::*func)(void *); }"); 6021 verifyFormat("typedef bool *(Class::*Member)() const;"); 6022 verifyFormat("void f() {\n" 6023 " (a->*f)();\n" 6024 " a->*x;\n" 6025 " (a.*f)();\n" 6026 " ((*a).*f)();\n" 6027 " a.*x;\n" 6028 "}"); 6029 verifyFormat("void f() {\n" 6030 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6031 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 6032 "}"); 6033 verifyFormat( 6034 "(aaaaaaaaaa->*bbbbbbb)(\n" 6035 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6036 FormatStyle Style = getLLVMStyle(); 6037 Style.PointerAlignment = FormatStyle::PAS_Left; 6038 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 6039 } 6040 6041 TEST_F(FormatTest, UnderstandsUnaryOperators) { 6042 verifyFormat("int a = -2;"); 6043 verifyFormat("f(-1, -2, -3);"); 6044 verifyFormat("a[-1] = 5;"); 6045 verifyFormat("int a = 5 + -2;"); 6046 verifyFormat("if (i == -1) {\n}"); 6047 verifyFormat("if (i != -1) {\n}"); 6048 verifyFormat("if (i > -1) {\n}"); 6049 verifyFormat("if (i < -1) {\n}"); 6050 verifyFormat("++(a->f());"); 6051 verifyFormat("--(a->f());"); 6052 verifyFormat("(a->f())++;"); 6053 verifyFormat("a[42]++;"); 6054 verifyFormat("if (!(a->f())) {\n}"); 6055 verifyFormat("if (!+i) {\n}"); 6056 verifyFormat("~&a;"); 6057 6058 verifyFormat("a-- > b;"); 6059 verifyFormat("b ? -a : c;"); 6060 verifyFormat("n * sizeof char16;"); 6061 verifyFormat("n * alignof char16;", getGoogleStyle()); 6062 verifyFormat("sizeof(char);"); 6063 verifyFormat("alignof(char);", getGoogleStyle()); 6064 6065 verifyFormat("return -1;"); 6066 verifyFormat("switch (a) {\n" 6067 "case -1:\n" 6068 " break;\n" 6069 "}"); 6070 verifyFormat("#define X -1"); 6071 verifyFormat("#define X -kConstant"); 6072 6073 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 6074 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 6075 6076 verifyFormat("int a = /* confusing comment */ -1;"); 6077 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 6078 verifyFormat("int a = i /* confusing comment */++;"); 6079 } 6080 6081 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 6082 verifyFormat("if (!aaaaaaaaaa( // break\n" 6083 " aaaaa)) {\n" 6084 "}"); 6085 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 6086 " aaaaa));"); 6087 verifyFormat("*aaa = aaaaaaa( // break\n" 6088 " bbbbbb);"); 6089 } 6090 6091 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 6092 verifyFormat("bool operator<();"); 6093 verifyFormat("bool operator>();"); 6094 verifyFormat("bool operator=();"); 6095 verifyFormat("bool operator==();"); 6096 verifyFormat("bool operator!=();"); 6097 verifyFormat("int operator+();"); 6098 verifyFormat("int operator++();"); 6099 verifyFormat("int operator++(int) volatile noexcept;"); 6100 verifyFormat("bool operator,();"); 6101 verifyFormat("bool operator();"); 6102 verifyFormat("bool operator()();"); 6103 verifyFormat("bool operator[]();"); 6104 verifyFormat("operator bool();"); 6105 verifyFormat("operator int();"); 6106 verifyFormat("operator void *();"); 6107 verifyFormat("operator SomeType<int>();"); 6108 verifyFormat("operator SomeType<int, int>();"); 6109 verifyFormat("operator SomeType<SomeType<int>>();"); 6110 verifyFormat("void *operator new(std::size_t size);"); 6111 verifyFormat("void *operator new[](std::size_t size);"); 6112 verifyFormat("void operator delete(void *ptr);"); 6113 verifyFormat("void operator delete[](void *ptr);"); 6114 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 6115 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 6116 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 6117 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 6118 6119 verifyFormat( 6120 "ostream &operator<<(ostream &OutputStream,\n" 6121 " SomeReallyLongType WithSomeReallyLongValue);"); 6122 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 6123 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 6124 " return left.group < right.group;\n" 6125 "}"); 6126 verifyFormat("SomeType &operator=(const SomeType &S);"); 6127 verifyFormat("f.template operator()<int>();"); 6128 6129 verifyGoogleFormat("operator void*();"); 6130 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 6131 verifyGoogleFormat("operator ::A();"); 6132 6133 verifyFormat("using A::operator+;"); 6134 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 6135 "int i;"); 6136 } 6137 6138 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 6139 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 6140 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 6141 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 6142 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 6143 verifyFormat("Deleted &operator=(const Deleted &) &;"); 6144 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 6145 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 6146 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 6147 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 6148 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 6149 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 6150 verifyFormat("void Fn(T const &) const &;"); 6151 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 6152 verifyFormat("template <typename T>\n" 6153 "void F(T) && = delete;", 6154 getGoogleStyle()); 6155 6156 FormatStyle AlignLeft = getLLVMStyle(); 6157 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 6158 verifyFormat("void A::b() && {}", AlignLeft); 6159 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 6160 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 6161 AlignLeft); 6162 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 6163 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 6164 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 6165 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 6166 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 6167 verifyFormat("auto Function(T) & -> void;", AlignLeft); 6168 verifyFormat("void Fn(T const&) const&;", AlignLeft); 6169 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 6170 6171 FormatStyle Spaces = getLLVMStyle(); 6172 Spaces.SpacesInCStyleCastParentheses = true; 6173 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 6174 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 6175 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 6176 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 6177 6178 Spaces.SpacesInCStyleCastParentheses = false; 6179 Spaces.SpacesInParentheses = true; 6180 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 6181 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 6182 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 6183 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 6184 } 6185 6186 TEST_F(FormatTest, UnderstandsNewAndDelete) { 6187 verifyFormat("void f() {\n" 6188 " A *a = new A;\n" 6189 " A *a = new (placement) A;\n" 6190 " delete a;\n" 6191 " delete (A *)a;\n" 6192 "}"); 6193 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 6194 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 6195 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6196 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 6197 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 6198 verifyFormat("delete[] h->p;"); 6199 } 6200 6201 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 6202 verifyFormat("int *f(int *a) {}"); 6203 verifyFormat("int main(int argc, char **argv) {}"); 6204 verifyFormat("Test::Test(int b) : a(b * b) {}"); 6205 verifyIndependentOfContext("f(a, *a);"); 6206 verifyFormat("void g() { f(*a); }"); 6207 verifyIndependentOfContext("int a = b * 10;"); 6208 verifyIndependentOfContext("int a = 10 * b;"); 6209 verifyIndependentOfContext("int a = b * c;"); 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;"); 6215 verifyIndependentOfContext("int a = *b * c;"); 6216 verifyIndependentOfContext("int a = b * *c;"); 6217 verifyIndependentOfContext("int a = b * (10);"); 6218 verifyIndependentOfContext("S << b * (10);"); 6219 verifyIndependentOfContext("return 10 * b;"); 6220 verifyIndependentOfContext("return *b * *c;"); 6221 verifyIndependentOfContext("return a & ~b;"); 6222 verifyIndependentOfContext("f(b ? *c : *d);"); 6223 verifyIndependentOfContext("int a = b ? *c : *d;"); 6224 verifyIndependentOfContext("*b = a;"); 6225 verifyIndependentOfContext("a * ~b;"); 6226 verifyIndependentOfContext("a * !b;"); 6227 verifyIndependentOfContext("a * +b;"); 6228 verifyIndependentOfContext("a * -b;"); 6229 verifyIndependentOfContext("a * ++b;"); 6230 verifyIndependentOfContext("a * --b;"); 6231 verifyIndependentOfContext("a[4] * b;"); 6232 verifyIndependentOfContext("a[a * a] = 1;"); 6233 verifyIndependentOfContext("f() * b;"); 6234 verifyIndependentOfContext("a * [self dostuff];"); 6235 verifyIndependentOfContext("int x = a * (a + b);"); 6236 verifyIndependentOfContext("(a *)(a + b);"); 6237 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 6238 verifyIndependentOfContext("int *pa = (int *)&a;"); 6239 verifyIndependentOfContext("return sizeof(int **);"); 6240 verifyIndependentOfContext("return sizeof(int ******);"); 6241 verifyIndependentOfContext("return (int **&)a;"); 6242 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 6243 verifyFormat("void f(Type (*parameter)[10]) {}"); 6244 verifyFormat("void f(Type (¶meter)[10]) {}"); 6245 verifyGoogleFormat("return sizeof(int**);"); 6246 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 6247 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 6248 verifyFormat("auto a = [](int **&, int ***) {};"); 6249 verifyFormat("auto PointerBinding = [](const char *S) {};"); 6250 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 6251 verifyFormat("[](const decltype(*a) &value) {}"); 6252 verifyFormat("decltype(a * b) F();"); 6253 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 6254 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 6255 verifyIndependentOfContext("typedef void (*f)(int *a);"); 6256 verifyIndependentOfContext("int i{a * b};"); 6257 verifyIndependentOfContext("aaa && aaa->f();"); 6258 verifyIndependentOfContext("int x = ~*p;"); 6259 verifyFormat("Constructor() : a(a), area(width * height) {}"); 6260 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 6261 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 6262 verifyFormat("void f() { f(a, c * d); }"); 6263 verifyFormat("void f() { f(new a(), c * d); }"); 6264 verifyFormat("void f(const MyOverride &override);"); 6265 verifyFormat("void f(const MyFinal &final);"); 6266 verifyIndependentOfContext("bool a = f() && override.f();"); 6267 verifyIndependentOfContext("bool a = f() && final.f();"); 6268 6269 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 6270 6271 verifyIndependentOfContext("A<int *> a;"); 6272 verifyIndependentOfContext("A<int **> a;"); 6273 verifyIndependentOfContext("A<int *, int *> a;"); 6274 verifyIndependentOfContext("A<int *[]> a;"); 6275 verifyIndependentOfContext( 6276 "const char *const p = reinterpret_cast<const char *const>(q);"); 6277 verifyIndependentOfContext("A<int **, int **> a;"); 6278 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 6279 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 6280 verifyFormat("for (; a && b;) {\n}"); 6281 verifyFormat("bool foo = true && [] { return false; }();"); 6282 6283 verifyFormat( 6284 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6285 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6286 6287 verifyGoogleFormat("int const* a = &b;"); 6288 verifyGoogleFormat("**outparam = 1;"); 6289 verifyGoogleFormat("*outparam = a * b;"); 6290 verifyGoogleFormat("int main(int argc, char** argv) {}"); 6291 verifyGoogleFormat("A<int*> a;"); 6292 verifyGoogleFormat("A<int**> a;"); 6293 verifyGoogleFormat("A<int*, int*> a;"); 6294 verifyGoogleFormat("A<int**, int**> a;"); 6295 verifyGoogleFormat("f(b ? *c : *d);"); 6296 verifyGoogleFormat("int a = b ? *c : *d;"); 6297 verifyGoogleFormat("Type* t = **x;"); 6298 verifyGoogleFormat("Type* t = *++*x;"); 6299 verifyGoogleFormat("*++*x;"); 6300 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 6301 verifyGoogleFormat("Type* t = x++ * y;"); 6302 verifyGoogleFormat( 6303 "const char* const p = reinterpret_cast<const char* const>(q);"); 6304 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 6305 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 6306 verifyGoogleFormat("template <typename T>\n" 6307 "void f(int i = 0, SomeType** temps = NULL);"); 6308 6309 FormatStyle Left = getLLVMStyle(); 6310 Left.PointerAlignment = FormatStyle::PAS_Left; 6311 verifyFormat("x = *a(x) = *a(y);", Left); 6312 verifyFormat("for (;; *a = b) {\n}", Left); 6313 verifyFormat("return *this += 1;", Left); 6314 verifyFormat("throw *x;", Left); 6315 verifyFormat("delete *x;", Left); 6316 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 6317 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 6318 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 6319 6320 verifyIndependentOfContext("a = *(x + y);"); 6321 verifyIndependentOfContext("a = &(x + y);"); 6322 verifyIndependentOfContext("*(x + y).call();"); 6323 verifyIndependentOfContext("&(x + y)->call();"); 6324 verifyFormat("void f() { &(*I).first; }"); 6325 6326 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 6327 verifyFormat( 6328 "int *MyValues = {\n" 6329 " *A, // Operator detection might be confused by the '{'\n" 6330 " *BB // Operator detection might be confused by previous comment\n" 6331 "};"); 6332 6333 verifyIndependentOfContext("if (int *a = &b)"); 6334 verifyIndependentOfContext("if (int &a = *b)"); 6335 verifyIndependentOfContext("if (a & b[i])"); 6336 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 6337 verifyIndependentOfContext("if (*b[i])"); 6338 verifyIndependentOfContext("if (int *a = (&b))"); 6339 verifyIndependentOfContext("while (int *a = &b)"); 6340 verifyIndependentOfContext("size = sizeof *a;"); 6341 verifyIndependentOfContext("if (a && (b = c))"); 6342 verifyFormat("void f() {\n" 6343 " for (const int &v : Values) {\n" 6344 " }\n" 6345 "}"); 6346 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 6347 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 6348 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 6349 6350 verifyFormat("#define A (!a * b)"); 6351 verifyFormat("#define MACRO \\\n" 6352 " int *i = a * b; \\\n" 6353 " void f(a *b);", 6354 getLLVMStyleWithColumns(19)); 6355 6356 verifyIndependentOfContext("A = new SomeType *[Length];"); 6357 verifyIndependentOfContext("A = new SomeType *[Length]();"); 6358 verifyIndependentOfContext("T **t = new T *;"); 6359 verifyIndependentOfContext("T **t = new T *();"); 6360 verifyGoogleFormat("A = new SomeType*[Length]();"); 6361 verifyGoogleFormat("A = new SomeType*[Length];"); 6362 verifyGoogleFormat("T** t = new T*;"); 6363 verifyGoogleFormat("T** t = new T*();"); 6364 6365 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 6366 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 6367 verifyFormat("template <bool a, bool b> " 6368 "typename t::if<x && y>::type f() {}"); 6369 verifyFormat("template <int *y> f() {}"); 6370 verifyFormat("vector<int *> v;"); 6371 verifyFormat("vector<int *const> v;"); 6372 verifyFormat("vector<int *const **const *> v;"); 6373 verifyFormat("vector<int *volatile> v;"); 6374 verifyFormat("vector<a * b> v;"); 6375 verifyFormat("foo<b && false>();"); 6376 verifyFormat("foo<b & 1>();"); 6377 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 6378 verifyFormat( 6379 "template <class T, class = typename std::enable_if<\n" 6380 " std::is_integral<T>::value &&\n" 6381 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 6382 "void F();", 6383 getLLVMStyleWithColumns(70)); 6384 verifyFormat( 6385 "template <class T,\n" 6386 " class = typename std::enable_if<\n" 6387 " std::is_integral<T>::value &&\n" 6388 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 6389 " class U>\n" 6390 "void F();", 6391 getLLVMStyleWithColumns(70)); 6392 verifyFormat( 6393 "template <class T,\n" 6394 " class = typename ::std::enable_if<\n" 6395 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 6396 "void F();", 6397 getGoogleStyleWithColumns(68)); 6398 6399 verifyIndependentOfContext("MACRO(int *i);"); 6400 verifyIndependentOfContext("MACRO(auto *a);"); 6401 verifyIndependentOfContext("MACRO(const A *a);"); 6402 verifyIndependentOfContext("MACRO(A *const a);"); 6403 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 6404 verifyFormat("void f() { f(float{1}, a * a); }"); 6405 // FIXME: Is there a way to make this work? 6406 // verifyIndependentOfContext("MACRO(A *a);"); 6407 6408 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 6409 verifyFormat("return options != nullptr && operator==(*options);"); 6410 6411 EXPECT_EQ("#define OP(x) \\\n" 6412 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6413 " return s << a.DebugString(); \\\n" 6414 " }", 6415 format("#define OP(x) \\\n" 6416 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6417 " return s << a.DebugString(); \\\n" 6418 " }", 6419 getLLVMStyleWithColumns(50))); 6420 6421 // FIXME: We cannot handle this case yet; we might be able to figure out that 6422 // foo<x> d > v; doesn't make sense. 6423 verifyFormat("foo<a<b && c> d> v;"); 6424 6425 FormatStyle PointerMiddle = getLLVMStyle(); 6426 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 6427 verifyFormat("delete *x;", PointerMiddle); 6428 verifyFormat("int * x;", PointerMiddle); 6429 verifyFormat("int *[] x;", PointerMiddle); 6430 verifyFormat("template <int * y> f() {}", PointerMiddle); 6431 verifyFormat("int * f(int * a) {}", PointerMiddle); 6432 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 6433 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 6434 verifyFormat("A<int *> a;", PointerMiddle); 6435 verifyFormat("A<int **> a;", PointerMiddle); 6436 verifyFormat("A<int *, int *> a;", PointerMiddle); 6437 verifyFormat("A<int *[]> a;", PointerMiddle); 6438 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 6439 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 6440 verifyFormat("T ** t = new T *;", PointerMiddle); 6441 6442 // Member function reference qualifiers aren't binary operators. 6443 verifyFormat("string // break\n" 6444 "operator()() & {}"); 6445 verifyFormat("string // break\n" 6446 "operator()() && {}"); 6447 verifyGoogleFormat("template <typename T>\n" 6448 "auto x() & -> int {}"); 6449 } 6450 6451 TEST_F(FormatTest, UnderstandsAttributes) { 6452 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 6453 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 6454 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 6455 FormatStyle AfterType = getLLVMStyle(); 6456 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 6457 verifyFormat("__attribute__((nodebug)) void\n" 6458 "foo() {}\n", 6459 AfterType); 6460 } 6461 6462 TEST_F(FormatTest, UnderstandsSquareAttributes) { 6463 verifyFormat("SomeType s [[unused]] (InitValue);"); 6464 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 6465 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 6466 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 6467 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 6468 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6469 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 6470 6471 // Make sure we do not mistake attributes for array subscripts. 6472 verifyFormat("int a() {}\n" 6473 "[[unused]] int b() {}\n"); 6474 6475 // On the other hand, we still need to correctly find array subscripts. 6476 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 6477 6478 // Make sure we do not parse attributes as lambda introducers. 6479 FormatStyle MultiLineFunctions = getLLVMStyle(); 6480 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6481 verifyFormat("[[unused]] int b() {\n" 6482 " return 42;\n" 6483 "}\n", 6484 MultiLineFunctions); 6485 } 6486 6487 TEST_F(FormatTest, UnderstandsEllipsis) { 6488 verifyFormat("int printf(const char *fmt, ...);"); 6489 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 6490 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 6491 6492 FormatStyle PointersLeft = getLLVMStyle(); 6493 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 6494 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 6495 } 6496 6497 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 6498 EXPECT_EQ("int *a;\n" 6499 "int *a;\n" 6500 "int *a;", 6501 format("int *a;\n" 6502 "int* a;\n" 6503 "int *a;", 6504 getGoogleStyle())); 6505 EXPECT_EQ("int* a;\n" 6506 "int* a;\n" 6507 "int* a;", 6508 format("int* a;\n" 6509 "int* a;\n" 6510 "int *a;", 6511 getGoogleStyle())); 6512 EXPECT_EQ("int *a;\n" 6513 "int *a;\n" 6514 "int *a;", 6515 format("int *a;\n" 6516 "int * a;\n" 6517 "int * a;", 6518 getGoogleStyle())); 6519 EXPECT_EQ("auto x = [] {\n" 6520 " int *a;\n" 6521 " int *a;\n" 6522 " int *a;\n" 6523 "};", 6524 format("auto x=[]{int *a;\n" 6525 "int * a;\n" 6526 "int * a;};", 6527 getGoogleStyle())); 6528 } 6529 6530 TEST_F(FormatTest, UnderstandsRvalueReferences) { 6531 verifyFormat("int f(int &&a) {}"); 6532 verifyFormat("int f(int a, char &&b) {}"); 6533 verifyFormat("void f() { int &&a = b; }"); 6534 verifyGoogleFormat("int f(int a, char&& b) {}"); 6535 verifyGoogleFormat("void f() { int&& a = b; }"); 6536 6537 verifyIndependentOfContext("A<int &&> a;"); 6538 verifyIndependentOfContext("A<int &&, int &&> a;"); 6539 verifyGoogleFormat("A<int&&> a;"); 6540 verifyGoogleFormat("A<int&&, int&&> a;"); 6541 6542 // Not rvalue references: 6543 verifyFormat("template <bool B, bool C> class A {\n" 6544 " static_assert(B && C, \"Something is wrong\");\n" 6545 "};"); 6546 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 6547 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 6548 verifyFormat("#define A(a, b) (a && b)"); 6549 } 6550 6551 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 6552 verifyFormat("void f() {\n" 6553 " x[aaaaaaaaa -\n" 6554 " b] = 23;\n" 6555 "}", 6556 getLLVMStyleWithColumns(15)); 6557 } 6558 6559 TEST_F(FormatTest, FormatsCasts) { 6560 verifyFormat("Type *A = static_cast<Type *>(P);"); 6561 verifyFormat("Type *A = (Type *)P;"); 6562 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6563 verifyFormat("int a = (int)(2.0f);"); 6564 verifyFormat("int a = (int)2.0f;"); 6565 verifyFormat("x[(int32)y];"); 6566 verifyFormat("x = (int32)y;"); 6567 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6568 verifyFormat("int a = (int)*b;"); 6569 verifyFormat("int a = (int)2.0f;"); 6570 verifyFormat("int a = (int)~0;"); 6571 verifyFormat("int a = (int)++a;"); 6572 verifyFormat("int a = (int)sizeof(int);"); 6573 verifyFormat("int a = (int)+2;"); 6574 verifyFormat("my_int a = (my_int)2.0f;"); 6575 verifyFormat("my_int a = (my_int)sizeof(int);"); 6576 verifyFormat("return (my_int)aaa;"); 6577 verifyFormat("#define x ((int)-1)"); 6578 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6579 verifyFormat("#define p(q) ((int *)&q)"); 6580 verifyFormat("fn(a)(b) + 1;"); 6581 6582 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6583 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6584 verifyFormat("my_int a = (my_int)~0;"); 6585 verifyFormat("my_int a = (my_int)++a;"); 6586 verifyFormat("my_int a = (my_int)-2;"); 6587 verifyFormat("my_int a = (my_int)1;"); 6588 verifyFormat("my_int a = (my_int *)1;"); 6589 verifyFormat("my_int a = (const my_int)-1;"); 6590 verifyFormat("my_int a = (const my_int *)-1;"); 6591 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6592 verifyFormat("my_int a = (ns::my_int)-2;"); 6593 verifyFormat("case (my_int)ONE:"); 6594 verifyFormat("auto x = (X)this;"); 6595 6596 // FIXME: single value wrapped with paren will be treated as cast. 6597 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6598 6599 verifyFormat("{ (void)F; }"); 6600 6601 // Don't break after a cast's 6602 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6603 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6604 " bbbbbbbbbbbbbbbbbbbbbb);"); 6605 6606 // These are not casts. 6607 verifyFormat("void f(int *) {}"); 6608 verifyFormat("f(foo)->b;"); 6609 verifyFormat("f(foo).b;"); 6610 verifyFormat("f(foo)(b);"); 6611 verifyFormat("f(foo)[b];"); 6612 verifyFormat("[](foo) { return 4; }(bar);"); 6613 verifyFormat("(*funptr)(foo)[4];"); 6614 verifyFormat("funptrs[4](foo)[4];"); 6615 verifyFormat("void f(int *);"); 6616 verifyFormat("void f(int *) = 0;"); 6617 verifyFormat("void f(SmallVector<int>) {}"); 6618 verifyFormat("void f(SmallVector<int>);"); 6619 verifyFormat("void f(SmallVector<int>) = 0;"); 6620 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6621 verifyFormat("int a = sizeof(int) * b;"); 6622 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6623 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6624 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6625 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6626 6627 // These are not casts, but at some point were confused with casts. 6628 verifyFormat("virtual void foo(int *) override;"); 6629 verifyFormat("virtual void foo(char &) const;"); 6630 verifyFormat("virtual void foo(int *a, char *) const;"); 6631 verifyFormat("int a = sizeof(int *) + b;"); 6632 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6633 verifyFormat("bool b = f(g<int>) && c;"); 6634 verifyFormat("typedef void (*f)(int i) func;"); 6635 6636 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6637 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6638 // FIXME: The indentation here is not ideal. 6639 verifyFormat( 6640 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6641 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6642 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6643 } 6644 6645 TEST_F(FormatTest, FormatsFunctionTypes) { 6646 verifyFormat("A<bool()> a;"); 6647 verifyFormat("A<SomeType()> a;"); 6648 verifyFormat("A<void (*)(int, std::string)> a;"); 6649 verifyFormat("A<void *(int)>;"); 6650 verifyFormat("void *(*a)(int *, SomeType *);"); 6651 verifyFormat("int (*func)(void *);"); 6652 verifyFormat("void f() { int (*func)(void *); }"); 6653 verifyFormat("template <class CallbackClass>\n" 6654 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6655 6656 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6657 verifyGoogleFormat("void* (*a)(int);"); 6658 verifyGoogleFormat( 6659 "template <class CallbackClass>\n" 6660 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6661 6662 // Other constructs can look somewhat like function types: 6663 verifyFormat("A<sizeof(*x)> a;"); 6664 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6665 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6666 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6667 verifyFormat("int x = f(&h)();"); 6668 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6669 verifyFormat("std::function<\n" 6670 " LooooooooooongTemplatedType<\n" 6671 " SomeType>*(\n" 6672 " LooooooooooooooooongType type)>\n" 6673 " function;", 6674 getGoogleStyleWithColumns(40)); 6675 } 6676 6677 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6678 verifyFormat("A (*foo_)[6];"); 6679 verifyFormat("vector<int> (*foo_)[6];"); 6680 } 6681 6682 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6683 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6684 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6685 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6686 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6687 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6688 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6689 6690 // Different ways of ()-initializiation. 6691 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6692 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6693 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6694 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6695 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6696 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6697 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6698 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6699 6700 // Lambdas should not confuse the variable declaration heuristic. 6701 verifyFormat("LooooooooooooooooongType\n" 6702 " variable(nullptr, [](A *a) {});", 6703 getLLVMStyleWithColumns(40)); 6704 } 6705 6706 TEST_F(FormatTest, BreaksLongDeclarations) { 6707 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6708 " AnotherNameForTheLongType;"); 6709 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6711 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6712 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6713 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6714 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6715 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6716 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6717 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6718 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6719 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6720 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6721 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6722 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6723 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6724 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6725 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6726 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6727 FormatStyle Indented = getLLVMStyle(); 6728 Indented.IndentWrappedFunctionNames = true; 6729 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6730 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6731 Indented); 6732 verifyFormat( 6733 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6734 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6735 Indented); 6736 verifyFormat( 6737 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6738 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6739 Indented); 6740 verifyFormat( 6741 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6742 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6743 Indented); 6744 6745 // FIXME: Without the comment, this breaks after "(". 6746 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6747 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6748 getGoogleStyle()); 6749 6750 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6751 " int LoooooooooooooooooooongParam2) {}"); 6752 verifyFormat( 6753 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6754 " SourceLocation L, IdentifierIn *II,\n" 6755 " Type *T) {}"); 6756 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6757 "ReallyReaaallyLongFunctionName(\n" 6758 " const std::string &SomeParameter,\n" 6759 " const SomeType<string, SomeOtherTemplateParameter>\n" 6760 " &ReallyReallyLongParameterName,\n" 6761 " const SomeType<string, SomeOtherTemplateParameter>\n" 6762 " &AnotherLongParameterName) {}"); 6763 verifyFormat("template <typename A>\n" 6764 "SomeLoooooooooooooooooooooongType<\n" 6765 " typename some_namespace::SomeOtherType<A>::Type>\n" 6766 "Function() {}"); 6767 6768 verifyGoogleFormat( 6769 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6770 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6771 verifyGoogleFormat( 6772 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6773 " SourceLocation L) {}"); 6774 verifyGoogleFormat( 6775 "some_namespace::LongReturnType\n" 6776 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6777 " int first_long_parameter, int second_parameter) {}"); 6778 6779 verifyGoogleFormat("template <typename T>\n" 6780 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6781 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6782 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6783 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6784 6785 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6786 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6787 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6788 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6789 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6790 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6791 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6792 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6793 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6794 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6795 6796 verifyFormat("template <typename T> // Templates on own line.\n" 6797 "static int // Some comment.\n" 6798 "MyFunction(int a);", 6799 getLLVMStyle()); 6800 } 6801 6802 TEST_F(FormatTest, FormatsArrays) { 6803 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6804 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6805 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6806 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6807 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6808 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6809 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6810 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6811 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6812 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6813 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6814 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6815 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6816 verifyFormat( 6817 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6818 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6819 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6820 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6821 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6822 6823 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6824 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6825 verifyFormat( 6826 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6827 " .aaaaaaa[0]\n" 6828 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6829 verifyFormat("a[::b::c];"); 6830 6831 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6832 6833 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6834 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6835 } 6836 6837 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6838 verifyFormat("(a)->b();"); 6839 verifyFormat("--a;"); 6840 } 6841 6842 TEST_F(FormatTest, HandlesIncludeDirectives) { 6843 verifyFormat("#include <string>\n" 6844 "#include <a/b/c.h>\n" 6845 "#include \"a/b/string\"\n" 6846 "#include \"string.h\"\n" 6847 "#include \"string.h\"\n" 6848 "#include <a-a>\n" 6849 "#include < path with space >\n" 6850 "#include_next <test.h>" 6851 "#include \"abc.h\" // this is included for ABC\n" 6852 "#include \"some long include\" // with a comment\n" 6853 "#include \"some very long include path\"\n" 6854 "#include <some/very/long/include/path>\n", 6855 getLLVMStyleWithColumns(35)); 6856 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6857 EXPECT_EQ("#include <a>", format("#include<a>")); 6858 6859 verifyFormat("#import <string>"); 6860 verifyFormat("#import <a/b/c.h>"); 6861 verifyFormat("#import \"a/b/string\""); 6862 verifyFormat("#import \"string.h\""); 6863 verifyFormat("#import \"string.h\""); 6864 verifyFormat("#if __has_include(<strstream>)\n" 6865 "#include <strstream>\n" 6866 "#endif"); 6867 6868 verifyFormat("#define MY_IMPORT <a/b>"); 6869 6870 verifyFormat("#if __has_include(<a/b>)"); 6871 verifyFormat("#if __has_include_next(<a/b>)"); 6872 verifyFormat("#define F __has_include(<a/b>)"); 6873 verifyFormat("#define F __has_include_next(<a/b>)"); 6874 6875 // Protocol buffer definition or missing "#". 6876 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6877 getLLVMStyleWithColumns(30)); 6878 6879 FormatStyle Style = getLLVMStyle(); 6880 Style.AlwaysBreakBeforeMultilineStrings = true; 6881 Style.ColumnLimit = 0; 6882 verifyFormat("#import \"abc.h\"", Style); 6883 6884 // But 'import' might also be a regular C++ namespace. 6885 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6886 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6887 } 6888 6889 //===----------------------------------------------------------------------===// 6890 // Error recovery tests. 6891 //===----------------------------------------------------------------------===// 6892 6893 TEST_F(FormatTest, IncompleteParameterLists) { 6894 FormatStyle NoBinPacking = getLLVMStyle(); 6895 NoBinPacking.BinPackParameters = false; 6896 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6897 " double *min_x,\n" 6898 " double *max_x,\n" 6899 " double *min_y,\n" 6900 " double *max_y,\n" 6901 " double *min_z,\n" 6902 " double *max_z, ) {}", 6903 NoBinPacking); 6904 } 6905 6906 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6907 verifyFormat("void f() { return; }\n42"); 6908 verifyFormat("void f() {\n" 6909 " if (0)\n" 6910 " return;\n" 6911 "}\n" 6912 "42"); 6913 verifyFormat("void f() { return }\n42"); 6914 verifyFormat("void f() {\n" 6915 " if (0)\n" 6916 " return\n" 6917 "}\n" 6918 "42"); 6919 } 6920 6921 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6922 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6923 EXPECT_EQ("void f() {\n" 6924 " if (a)\n" 6925 " return\n" 6926 "}", 6927 format("void f ( ) { if ( a ) return }")); 6928 EXPECT_EQ("namespace N {\n" 6929 "void f()\n" 6930 "}", 6931 format("namespace N { void f() }")); 6932 EXPECT_EQ("namespace N {\n" 6933 "void f() {}\n" 6934 "void g()\n" 6935 "} // namespace N", 6936 format("namespace N { void f( ) { } void g( ) }")); 6937 } 6938 6939 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6940 verifyFormat("int aaaaaaaa =\n" 6941 " // Overlylongcomment\n" 6942 " b;", 6943 getLLVMStyleWithColumns(20)); 6944 verifyFormat("function(\n" 6945 " ShortArgument,\n" 6946 " LoooooooooooongArgument);\n", 6947 getLLVMStyleWithColumns(20)); 6948 } 6949 6950 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6951 verifyFormat("public:"); 6952 verifyFormat("class A {\n" 6953 "public\n" 6954 " void f() {}\n" 6955 "};"); 6956 verifyFormat("public\n" 6957 "int qwerty;"); 6958 verifyFormat("public\n" 6959 "B {}"); 6960 verifyFormat("public\n" 6961 "{}"); 6962 verifyFormat("public\n" 6963 "B { int x; }"); 6964 } 6965 6966 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6967 verifyFormat("{"); 6968 verifyFormat("#})"); 6969 verifyNoCrash("(/**/[:!] ?[)."); 6970 } 6971 6972 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 6973 // Found by oss-fuzz: 6974 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 6975 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 6976 Style.ColumnLimit = 60; 6977 verifyNoCrash( 6978 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 6979 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 6980 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 6981 Style); 6982 } 6983 6984 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6985 verifyFormat("do {\n}"); 6986 verifyFormat("do {\n}\n" 6987 "f();"); 6988 verifyFormat("do {\n}\n" 6989 "wheeee(fun);"); 6990 verifyFormat("do {\n" 6991 " f();\n" 6992 "}"); 6993 } 6994 6995 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6996 verifyFormat("if {\n foo;\n foo();\n}"); 6997 verifyFormat("switch {\n foo;\n foo();\n}"); 6998 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6999 verifyFormat("while {\n foo;\n foo();\n}"); 7000 verifyFormat("do {\n foo;\n foo();\n} while;"); 7001 } 7002 7003 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 7004 verifyIncompleteFormat("namespace {\n" 7005 "class Foo { Foo (\n" 7006 "};\n" 7007 "} // namespace"); 7008 } 7009 7010 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 7011 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 7012 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 7013 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 7014 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 7015 7016 EXPECT_EQ("{\n" 7017 " {\n" 7018 " breakme(\n" 7019 " qwe);\n" 7020 " }\n", 7021 format("{\n" 7022 " {\n" 7023 " breakme(qwe);\n" 7024 "}\n", 7025 getLLVMStyleWithColumns(10))); 7026 } 7027 7028 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 7029 verifyFormat("int x = {\n" 7030 " avariable,\n" 7031 " b(alongervariable)};", 7032 getLLVMStyleWithColumns(25)); 7033 } 7034 7035 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 7036 verifyFormat("return (a)(b){1, 2, 3};"); 7037 } 7038 7039 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 7040 verifyFormat("vector<int> x{1, 2, 3, 4};"); 7041 verifyFormat("vector<int> x{\n" 7042 " 1,\n" 7043 " 2,\n" 7044 " 3,\n" 7045 " 4,\n" 7046 "};"); 7047 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 7048 verifyFormat("f({1, 2});"); 7049 verifyFormat("auto v = Foo{-1};"); 7050 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 7051 verifyFormat("Class::Class : member{1, 2, 3} {}"); 7052 verifyFormat("new vector<int>{1, 2, 3};"); 7053 verifyFormat("new int[3]{1, 2, 3};"); 7054 verifyFormat("new int{1};"); 7055 verifyFormat("return {arg1, arg2};"); 7056 verifyFormat("return {arg1, SomeType{parameter}};"); 7057 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 7058 verifyFormat("new T{arg1, arg2};"); 7059 verifyFormat("f(MyMap[{composite, key}]);"); 7060 verifyFormat("class Class {\n" 7061 " T member = {arg1, arg2};\n" 7062 "};"); 7063 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 7064 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 7065 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 7066 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 7067 verifyFormat("int a = std::is_integral<int>{} + 0;"); 7068 7069 verifyFormat("int foo(int i) { return fo1{}(i); }"); 7070 verifyFormat("int foo(int i) { return fo1{}(i); }"); 7071 verifyFormat("auto i = decltype(x){};"); 7072 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 7073 verifyFormat("Node n{1, Node{1000}, //\n" 7074 " 2};"); 7075 verifyFormat("Aaaa aaaaaaa{\n" 7076 " {\n" 7077 " aaaa,\n" 7078 " },\n" 7079 "};"); 7080 verifyFormat("class C : public D {\n" 7081 " SomeClass SC{2};\n" 7082 "};"); 7083 verifyFormat("class C : public A {\n" 7084 " class D : public B {\n" 7085 " void f() { int i{2}; }\n" 7086 " };\n" 7087 "};"); 7088 verifyFormat("#define A {a, a},"); 7089 7090 // Avoid breaking between equal sign and opening brace 7091 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 7092 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 7093 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 7094 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 7095 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 7096 " {\"ccccccccccccccccccccc\", 2}};", 7097 AvoidBreakingFirstArgument); 7098 7099 // Binpacking only if there is no trailing comma 7100 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 7101 " cccccccccc, dddddddddd};", 7102 getLLVMStyleWithColumns(50)); 7103 verifyFormat("const Aaaaaa aaaaa = {\n" 7104 " aaaaaaaaaaa,\n" 7105 " bbbbbbbbbbb,\n" 7106 " ccccccccccc,\n" 7107 " ddddddddddd,\n" 7108 "};", getLLVMStyleWithColumns(50)); 7109 7110 // Cases where distinguising braced lists and blocks is hard. 7111 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 7112 verifyFormat("void f() {\n" 7113 " return; // comment\n" 7114 "}\n" 7115 "SomeType t;"); 7116 verifyFormat("void f() {\n" 7117 " if (a) {\n" 7118 " f();\n" 7119 " }\n" 7120 "}\n" 7121 "SomeType t;"); 7122 7123 // In combination with BinPackArguments = false. 7124 FormatStyle NoBinPacking = getLLVMStyle(); 7125 NoBinPacking.BinPackArguments = false; 7126 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 7127 " bbbbb,\n" 7128 " ccccc,\n" 7129 " ddddd,\n" 7130 " eeeee,\n" 7131 " ffffff,\n" 7132 " ggggg,\n" 7133 " hhhhhh,\n" 7134 " iiiiii,\n" 7135 " jjjjjj,\n" 7136 " kkkkkk};", 7137 NoBinPacking); 7138 verifyFormat("const Aaaaaa aaaaa = {\n" 7139 " aaaaa,\n" 7140 " bbbbb,\n" 7141 " ccccc,\n" 7142 " ddddd,\n" 7143 " eeeee,\n" 7144 " ffffff,\n" 7145 " ggggg,\n" 7146 " hhhhhh,\n" 7147 " iiiiii,\n" 7148 " jjjjjj,\n" 7149 " kkkkkk,\n" 7150 "};", 7151 NoBinPacking); 7152 verifyFormat( 7153 "const Aaaaaa aaaaa = {\n" 7154 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 7155 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 7156 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 7157 "};", 7158 NoBinPacking); 7159 7160 // FIXME: The alignment of these trailing comments might be bad. Then again, 7161 // this might be utterly useless in real code. 7162 verifyFormat("Constructor::Constructor()\n" 7163 " : some_value{ //\n" 7164 " aaaaaaa, //\n" 7165 " bbbbbbb} {}"); 7166 7167 // In braced lists, the first comment is always assumed to belong to the 7168 // first element. Thus, it can be moved to the next or previous line as 7169 // appropriate. 7170 EXPECT_EQ("function({// First element:\n" 7171 " 1,\n" 7172 " // Second element:\n" 7173 " 2});", 7174 format("function({\n" 7175 " // First element:\n" 7176 " 1,\n" 7177 " // Second element:\n" 7178 " 2});")); 7179 EXPECT_EQ("std::vector<int> MyNumbers{\n" 7180 " // First element:\n" 7181 " 1,\n" 7182 " // Second element:\n" 7183 " 2};", 7184 format("std::vector<int> MyNumbers{// First element:\n" 7185 " 1,\n" 7186 " // Second element:\n" 7187 " 2};", 7188 getLLVMStyleWithColumns(30))); 7189 // A trailing comma should still lead to an enforced line break and no 7190 // binpacking. 7191 EXPECT_EQ("vector<int> SomeVector = {\n" 7192 " // aaa\n" 7193 " 1,\n" 7194 " 2,\n" 7195 "};", 7196 format("vector<int> SomeVector = { // aaa\n" 7197 " 1, 2, };")); 7198 7199 FormatStyle ExtraSpaces = getLLVMStyle(); 7200 ExtraSpaces.Cpp11BracedListStyle = false; 7201 ExtraSpaces.ColumnLimit = 75; 7202 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 7203 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 7204 verifyFormat("f({ 1, 2 });", ExtraSpaces); 7205 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 7206 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 7207 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 7208 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 7209 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 7210 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 7211 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 7212 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 7213 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 7214 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 7215 verifyFormat("class Class {\n" 7216 " T member = { arg1, arg2 };\n" 7217 "};", 7218 ExtraSpaces); 7219 verifyFormat( 7220 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7221 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 7222 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 7223 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 7224 ExtraSpaces); 7225 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 7226 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 7227 ExtraSpaces); 7228 verifyFormat( 7229 "someFunction(OtherParam,\n" 7230 " BracedList{ // comment 1 (Forcing interesting break)\n" 7231 " param1, param2,\n" 7232 " // comment 2\n" 7233 " param3, param4 });", 7234 ExtraSpaces); 7235 verifyFormat( 7236 "std::this_thread::sleep_for(\n" 7237 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 7238 ExtraSpaces); 7239 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 7240 " aaaaaaa,\n" 7241 " aaaaaaaaaa,\n" 7242 " aaaaa,\n" 7243 " aaaaaaaaaaaaaaa,\n" 7244 " aaa,\n" 7245 " aaaaaaaaaa,\n" 7246 " a,\n" 7247 " aaaaaaaaaaaaaaaaaaaaa,\n" 7248 " aaaaaaaaaaaa,\n" 7249 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 7250 " aaaaaaa,\n" 7251 " a};"); 7252 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 7253 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 7254 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 7255 7256 // Avoid breaking between initializer/equal sign and opening brace 7257 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 7258 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 7259 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 7260 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 7261 " { \"ccccccccccccccccccccc\", 2 }\n" 7262 "};", 7263 ExtraSpaces); 7264 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 7265 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 7266 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 7267 " { \"ccccccccccccccccccccc\", 2 }\n" 7268 "};", 7269 ExtraSpaces); 7270 7271 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 7272 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 7273 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 7274 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 7275 } 7276 7277 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 7278 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7279 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7280 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7281 " 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};"); 7284 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 7285 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7286 " 1, 22, 333, 4444, 55555, //\n" 7287 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7288 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 7289 verifyFormat( 7290 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7291 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7292 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 7293 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7294 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7295 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7296 " 7777777};"); 7297 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7298 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7299 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7300 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7301 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7302 " // Separating comment.\n" 7303 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7304 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7305 " // Leading comment\n" 7306 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7307 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7308 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7309 " 1, 1, 1, 1};", 7310 getLLVMStyleWithColumns(39)); 7311 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7312 " 1, 1, 1, 1};", 7313 getLLVMStyleWithColumns(38)); 7314 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 7315 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 7316 getLLVMStyleWithColumns(43)); 7317 verifyFormat( 7318 "static unsigned SomeValues[10][3] = {\n" 7319 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 7320 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 7321 verifyFormat("static auto fields = new vector<string>{\n" 7322 " \"aaaaaaaaaaaaa\",\n" 7323 " \"aaaaaaaaaaaaa\",\n" 7324 " \"aaaaaaaaaaaa\",\n" 7325 " \"aaaaaaaaaaaaaa\",\n" 7326 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 7327 " \"aaaaaaaaaaaa\",\n" 7328 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 7329 "};"); 7330 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 7331 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 7332 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 7333 " 3, cccccccccccccccccccccc};", 7334 getLLVMStyleWithColumns(60)); 7335 7336 // Trailing commas. 7337 verifyFormat("vector<int> x = {\n" 7338 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 7339 "};", 7340 getLLVMStyleWithColumns(39)); 7341 verifyFormat("vector<int> x = {\n" 7342 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 7343 "};", 7344 getLLVMStyleWithColumns(39)); 7345 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7346 " 1, 1, 1, 1,\n" 7347 " /**/ /**/};", 7348 getLLVMStyleWithColumns(39)); 7349 7350 // Trailing comment in the first line. 7351 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 7352 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 7353 " 111111111, 222222222, 3333333333, 444444444, //\n" 7354 " 11111111, 22222222, 333333333, 44444444};"); 7355 // Trailing comment in the last line. 7356 verifyFormat("int aaaaa[] = {\n" 7357 " 1, 2, 3, // comment\n" 7358 " 4, 5, 6 // comment\n" 7359 "};"); 7360 7361 // With nested lists, we should either format one item per line or all nested 7362 // lists one on line. 7363 // FIXME: For some nested lists, we can do better. 7364 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 7365 " {aaaaaaaaaaaaaaaaaaa},\n" 7366 " {aaaaaaaaaaaaaaaaaaaaa},\n" 7367 " {aaaaaaaaaaaaaaaaa}};", 7368 getLLVMStyleWithColumns(60)); 7369 verifyFormat( 7370 "SomeStruct my_struct_array = {\n" 7371 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 7372 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 7373 " {aaa, aaa},\n" 7374 " {aaa, aaa},\n" 7375 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 7376 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7377 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 7378 7379 // No column layout should be used here. 7380 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 7381 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 7382 7383 verifyNoCrash("a<,"); 7384 7385 // No braced initializer here. 7386 verifyFormat("void f() {\n" 7387 " struct Dummy {};\n" 7388 " f(v);\n" 7389 "}"); 7390 7391 // Long lists should be formatted in columns even if they are nested. 7392 verifyFormat( 7393 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7394 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7395 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7396 " 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});"); 7399 7400 // Allow "single-column" layout even if that violates the column limit. There 7401 // isn't going to be a better way. 7402 verifyFormat("std::vector<int> a = {\n" 7403 " aaaaaaaa,\n" 7404 " aaaaaaaa,\n" 7405 " aaaaaaaa,\n" 7406 " aaaaaaaa,\n" 7407 " aaaaaaaaaa,\n" 7408 " aaaaaaaa,\n" 7409 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 7410 getLLVMStyleWithColumns(30)); 7411 verifyFormat("vector<int> aaaa = {\n" 7412 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7413 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7414 " aaaaaa.aaaaaaa,\n" 7415 " aaaaaa.aaaaaaa,\n" 7416 " aaaaaa.aaaaaaa,\n" 7417 " aaaaaa.aaaaaaa,\n" 7418 "};"); 7419 7420 // Don't create hanging lists. 7421 verifyFormat("someFunction(Param, {List1, List2,\n" 7422 " List3});", 7423 getLLVMStyleWithColumns(35)); 7424 verifyFormat("someFunction(Param, Param,\n" 7425 " {List1, List2,\n" 7426 " List3});", 7427 getLLVMStyleWithColumns(35)); 7428 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 7429 " aaaaaaaaaaaaaaaaaaaaaaa);"); 7430 } 7431 7432 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 7433 FormatStyle DoNotMerge = getLLVMStyle(); 7434 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7435 7436 verifyFormat("void f() { return 42; }"); 7437 verifyFormat("void f() {\n" 7438 " return 42;\n" 7439 "}", 7440 DoNotMerge); 7441 verifyFormat("void f() {\n" 7442 " // Comment\n" 7443 "}"); 7444 verifyFormat("{\n" 7445 "#error {\n" 7446 " int a;\n" 7447 "}"); 7448 verifyFormat("{\n" 7449 " int a;\n" 7450 "#error {\n" 7451 "}"); 7452 verifyFormat("void f() {} // comment"); 7453 verifyFormat("void f() { int a; } // comment"); 7454 verifyFormat("void f() {\n" 7455 "} // comment", 7456 DoNotMerge); 7457 verifyFormat("void f() {\n" 7458 " int a;\n" 7459 "} // comment", 7460 DoNotMerge); 7461 verifyFormat("void f() {\n" 7462 "} // comment", 7463 getLLVMStyleWithColumns(15)); 7464 7465 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 7466 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 7467 7468 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 7469 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 7470 verifyFormat("class C {\n" 7471 " C()\n" 7472 " : iiiiiiii(nullptr),\n" 7473 " kkkkkkk(nullptr),\n" 7474 " mmmmmmm(nullptr),\n" 7475 " nnnnnnn(nullptr) {}\n" 7476 "};", 7477 getGoogleStyle()); 7478 7479 FormatStyle NoColumnLimit = getLLVMStyle(); 7480 NoColumnLimit.ColumnLimit = 0; 7481 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 7482 EXPECT_EQ("class C {\n" 7483 " A() : b(0) {}\n" 7484 "};", 7485 format("class C{A():b(0){}};", NoColumnLimit)); 7486 EXPECT_EQ("A()\n" 7487 " : b(0) {\n" 7488 "}", 7489 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 7490 7491 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 7492 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 7493 FormatStyle::SFS_None; 7494 EXPECT_EQ("A()\n" 7495 " : b(0) {\n" 7496 "}", 7497 format("A():b(0){}", DoNotMergeNoColumnLimit)); 7498 EXPECT_EQ("A()\n" 7499 " : b(0) {\n" 7500 "}", 7501 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 7502 7503 verifyFormat("#define A \\\n" 7504 " void f() { \\\n" 7505 " int i; \\\n" 7506 " }", 7507 getLLVMStyleWithColumns(20)); 7508 verifyFormat("#define A \\\n" 7509 " void f() { int i; }", 7510 getLLVMStyleWithColumns(21)); 7511 verifyFormat("#define A \\\n" 7512 " void f() { \\\n" 7513 " int i; \\\n" 7514 " } \\\n" 7515 " int j;", 7516 getLLVMStyleWithColumns(22)); 7517 verifyFormat("#define A \\\n" 7518 " void f() { int i; } \\\n" 7519 " int j;", 7520 getLLVMStyleWithColumns(23)); 7521 } 7522 7523 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 7524 FormatStyle MergeEmptyOnly = getLLVMStyle(); 7525 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7526 verifyFormat("class C {\n" 7527 " int f() {}\n" 7528 "};", 7529 MergeEmptyOnly); 7530 verifyFormat("class C {\n" 7531 " int f() {\n" 7532 " return 42;\n" 7533 " }\n" 7534 "};", 7535 MergeEmptyOnly); 7536 verifyFormat("int f() {}", MergeEmptyOnly); 7537 verifyFormat("int f() {\n" 7538 " return 42;\n" 7539 "}", 7540 MergeEmptyOnly); 7541 7542 // Also verify behavior when BraceWrapping.AfterFunction = true 7543 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7544 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 7545 verifyFormat("int f() {}", MergeEmptyOnly); 7546 verifyFormat("class C {\n" 7547 " int f() {}\n" 7548 "};", 7549 MergeEmptyOnly); 7550 } 7551 7552 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 7553 FormatStyle MergeInlineOnly = getLLVMStyle(); 7554 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7555 verifyFormat("class C {\n" 7556 " int f() { return 42; }\n" 7557 "};", 7558 MergeInlineOnly); 7559 verifyFormat("int f() {\n" 7560 " return 42;\n" 7561 "}", 7562 MergeInlineOnly); 7563 7564 // SFS_Inline implies SFS_Empty 7565 verifyFormat("class C {\n" 7566 " int f() {}\n" 7567 "};", 7568 MergeInlineOnly); 7569 verifyFormat("int f() {}", MergeInlineOnly); 7570 7571 // Also verify behavior when BraceWrapping.AfterFunction = true 7572 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7573 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7574 verifyFormat("class C {\n" 7575 " int f() { return 42; }\n" 7576 "};", 7577 MergeInlineOnly); 7578 verifyFormat("int f()\n" 7579 "{\n" 7580 " return 42;\n" 7581 "}", 7582 MergeInlineOnly); 7583 7584 // SFS_Inline implies SFS_Empty 7585 verifyFormat("int f() {}", MergeInlineOnly); 7586 verifyFormat("class C {\n" 7587 " int f() {}\n" 7588 "};", 7589 MergeInlineOnly); 7590 } 7591 7592 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 7593 FormatStyle MergeInlineOnly = getLLVMStyle(); 7594 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 7595 FormatStyle::SFS_InlineOnly; 7596 verifyFormat("class C {\n" 7597 " int f() { return 42; }\n" 7598 "};", 7599 MergeInlineOnly); 7600 verifyFormat("int f() {\n" 7601 " return 42;\n" 7602 "}", 7603 MergeInlineOnly); 7604 7605 // SFS_InlineOnly does not imply SFS_Empty 7606 verifyFormat("class C {\n" 7607 " int f() {}\n" 7608 "};", 7609 MergeInlineOnly); 7610 verifyFormat("int f() {\n" 7611 "}", 7612 MergeInlineOnly); 7613 7614 // Also verify behavior when BraceWrapping.AfterFunction = true 7615 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7616 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7617 verifyFormat("class C {\n" 7618 " int f() { return 42; }\n" 7619 "};", 7620 MergeInlineOnly); 7621 verifyFormat("int f()\n" 7622 "{\n" 7623 " return 42;\n" 7624 "}", 7625 MergeInlineOnly); 7626 7627 // SFS_InlineOnly does not imply SFS_Empty 7628 verifyFormat("int f()\n" 7629 "{\n" 7630 "}", 7631 MergeInlineOnly); 7632 verifyFormat("class C {\n" 7633 " int f() {}\n" 7634 "};", 7635 MergeInlineOnly); 7636 } 7637 7638 TEST_F(FormatTest, SplitEmptyFunction) { 7639 FormatStyle Style = getLLVMStyle(); 7640 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7641 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7642 Style.BraceWrapping.AfterFunction = true; 7643 Style.BraceWrapping.SplitEmptyFunction = false; 7644 Style.ColumnLimit = 40; 7645 7646 verifyFormat("int f()\n" 7647 "{}", 7648 Style); 7649 verifyFormat("int f()\n" 7650 "{\n" 7651 " return 42;\n" 7652 "}", 7653 Style); 7654 verifyFormat("int f()\n" 7655 "{\n" 7656 " // some comment\n" 7657 "}", 7658 Style); 7659 7660 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7661 verifyFormat("int f() {}", Style); 7662 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7663 "{}", 7664 Style); 7665 verifyFormat("int f()\n" 7666 "{\n" 7667 " return 0;\n" 7668 "}", 7669 Style); 7670 7671 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7672 verifyFormat("class Foo {\n" 7673 " int f() {}\n" 7674 "};\n", 7675 Style); 7676 verifyFormat("class Foo {\n" 7677 " int f() { return 0; }\n" 7678 "};\n", 7679 Style); 7680 verifyFormat("class Foo {\n" 7681 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7682 " {}\n" 7683 "};\n", 7684 Style); 7685 verifyFormat("class Foo {\n" 7686 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7687 " {\n" 7688 " return 0;\n" 7689 " }\n" 7690 "};\n", 7691 Style); 7692 7693 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7694 verifyFormat("int f() {}", Style); 7695 verifyFormat("int f() { return 0; }", Style); 7696 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7697 "{}", 7698 Style); 7699 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7700 "{\n" 7701 " return 0;\n" 7702 "}", 7703 Style); 7704 } 7705 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 7706 FormatStyle Style = getLLVMStyle(); 7707 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7708 verifyFormat("#ifdef A\n" 7709 "int f() {}\n" 7710 "#else\n" 7711 "int g() {}\n" 7712 "#endif", 7713 Style); 7714 } 7715 7716 TEST_F(FormatTest, SplitEmptyClass) { 7717 FormatStyle Style = getLLVMStyle(); 7718 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7719 Style.BraceWrapping.AfterClass = true; 7720 Style.BraceWrapping.SplitEmptyRecord = false; 7721 7722 verifyFormat("class Foo\n" 7723 "{};", 7724 Style); 7725 verifyFormat("/* something */ class Foo\n" 7726 "{};", 7727 Style); 7728 verifyFormat("template <typename X> class Foo\n" 7729 "{};", 7730 Style); 7731 verifyFormat("class Foo\n" 7732 "{\n" 7733 " Foo();\n" 7734 "};", 7735 Style); 7736 verifyFormat("typedef class Foo\n" 7737 "{\n" 7738 "} Foo_t;", 7739 Style); 7740 } 7741 7742 TEST_F(FormatTest, SplitEmptyStruct) { 7743 FormatStyle Style = getLLVMStyle(); 7744 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7745 Style.BraceWrapping.AfterStruct = true; 7746 Style.BraceWrapping.SplitEmptyRecord = false; 7747 7748 verifyFormat("struct Foo\n" 7749 "{};", 7750 Style); 7751 verifyFormat("/* something */ struct Foo\n" 7752 "{};", 7753 Style); 7754 verifyFormat("template <typename X> struct Foo\n" 7755 "{};", 7756 Style); 7757 verifyFormat("struct Foo\n" 7758 "{\n" 7759 " Foo();\n" 7760 "};", 7761 Style); 7762 verifyFormat("typedef struct Foo\n" 7763 "{\n" 7764 "} Foo_t;", 7765 Style); 7766 //typedef struct Bar {} Bar_t; 7767 } 7768 7769 TEST_F(FormatTest, SplitEmptyUnion) { 7770 FormatStyle Style = getLLVMStyle(); 7771 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7772 Style.BraceWrapping.AfterUnion = true; 7773 Style.BraceWrapping.SplitEmptyRecord = false; 7774 7775 verifyFormat("union Foo\n" 7776 "{};", 7777 Style); 7778 verifyFormat("/* something */ union Foo\n" 7779 "{};", 7780 Style); 7781 verifyFormat("union Foo\n" 7782 "{\n" 7783 " A,\n" 7784 "};", 7785 Style); 7786 verifyFormat("typedef union Foo\n" 7787 "{\n" 7788 "} Foo_t;", 7789 Style); 7790 } 7791 7792 TEST_F(FormatTest, SplitEmptyNamespace) { 7793 FormatStyle Style = getLLVMStyle(); 7794 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7795 Style.BraceWrapping.AfterNamespace = true; 7796 Style.BraceWrapping.SplitEmptyNamespace = false; 7797 7798 verifyFormat("namespace Foo\n" 7799 "{};", 7800 Style); 7801 verifyFormat("/* something */ namespace Foo\n" 7802 "{};", 7803 Style); 7804 verifyFormat("inline namespace Foo\n" 7805 "{};", 7806 Style); 7807 verifyFormat("/* something */ inline namespace Foo\n" 7808 "{};", 7809 Style); 7810 verifyFormat("export namespace Foo\n" 7811 "{};", 7812 Style); 7813 verifyFormat("namespace Foo\n" 7814 "{\n" 7815 "void Bar();\n" 7816 "};", 7817 Style); 7818 } 7819 7820 TEST_F(FormatTest, NeverMergeShortRecords) { 7821 FormatStyle Style = getLLVMStyle(); 7822 7823 verifyFormat("class Foo {\n" 7824 " Foo();\n" 7825 "};", 7826 Style); 7827 verifyFormat("typedef class Foo {\n" 7828 " Foo();\n" 7829 "} Foo_t;", 7830 Style); 7831 verifyFormat("struct Foo {\n" 7832 " Foo();\n" 7833 "};", 7834 Style); 7835 verifyFormat("typedef struct Foo {\n" 7836 " Foo();\n" 7837 "} Foo_t;", 7838 Style); 7839 verifyFormat("union Foo {\n" 7840 " A,\n" 7841 "};", 7842 Style); 7843 verifyFormat("typedef union Foo {\n" 7844 " A,\n" 7845 "} Foo_t;", 7846 Style); 7847 verifyFormat("namespace Foo {\n" 7848 "void Bar();\n" 7849 "};", 7850 Style); 7851 7852 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7853 Style.BraceWrapping.AfterClass = true; 7854 Style.BraceWrapping.AfterStruct = true; 7855 Style.BraceWrapping.AfterUnion = true; 7856 Style.BraceWrapping.AfterNamespace = true; 7857 verifyFormat("class Foo\n" 7858 "{\n" 7859 " Foo();\n" 7860 "};", 7861 Style); 7862 verifyFormat("typedef class Foo\n" 7863 "{\n" 7864 " Foo();\n" 7865 "} Foo_t;", 7866 Style); 7867 verifyFormat("struct Foo\n" 7868 "{\n" 7869 " Foo();\n" 7870 "};", 7871 Style); 7872 verifyFormat("typedef struct Foo\n" 7873 "{\n" 7874 " Foo();\n" 7875 "} Foo_t;", 7876 Style); 7877 verifyFormat("union Foo\n" 7878 "{\n" 7879 " A,\n" 7880 "};", 7881 Style); 7882 verifyFormat("typedef union Foo\n" 7883 "{\n" 7884 " A,\n" 7885 "} Foo_t;", 7886 Style); 7887 verifyFormat("namespace Foo\n" 7888 "{\n" 7889 "void Bar();\n" 7890 "};", 7891 Style); 7892 } 7893 7894 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 7895 // Elaborate type variable declarations. 7896 verifyFormat("struct foo a = {bar};\nint n;"); 7897 verifyFormat("class foo a = {bar};\nint n;"); 7898 verifyFormat("union foo a = {bar};\nint n;"); 7899 7900 // Elaborate types inside function definitions. 7901 verifyFormat("struct foo f() {}\nint n;"); 7902 verifyFormat("class foo f() {}\nint n;"); 7903 verifyFormat("union foo f() {}\nint n;"); 7904 7905 // Templates. 7906 verifyFormat("template <class X> void f() {}\nint n;"); 7907 verifyFormat("template <struct X> void f() {}\nint n;"); 7908 verifyFormat("template <union X> void f() {}\nint n;"); 7909 7910 // Actual definitions... 7911 verifyFormat("struct {\n} n;"); 7912 verifyFormat( 7913 "template <template <class T, class Y>, class Z> class X {\n} n;"); 7914 verifyFormat("union Z {\n int n;\n} x;"); 7915 verifyFormat("class MACRO Z {\n} n;"); 7916 verifyFormat("class MACRO(X) Z {\n} n;"); 7917 verifyFormat("class __attribute__(X) Z {\n} n;"); 7918 verifyFormat("class __declspec(X) Z {\n} n;"); 7919 verifyFormat("class A##B##C {\n} n;"); 7920 verifyFormat("class alignas(16) Z {\n} n;"); 7921 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 7922 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 7923 7924 // Redefinition from nested context: 7925 verifyFormat("class A::B::C {\n} n;"); 7926 7927 // Template definitions. 7928 verifyFormat( 7929 "template <typename F>\n" 7930 "Matcher(const Matcher<F> &Other,\n" 7931 " typename enable_if_c<is_base_of<F, T>::value &&\n" 7932 " !is_same<F, T>::value>::type * = 0)\n" 7933 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 7934 7935 // FIXME: This is still incorrectly handled at the formatter side. 7936 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 7937 verifyFormat("int i = SomeFunction(a<b, a> b);"); 7938 7939 // FIXME: 7940 // This now gets parsed incorrectly as class definition. 7941 // verifyFormat("class A<int> f() {\n}\nint n;"); 7942 7943 // Elaborate types where incorrectly parsing the structural element would 7944 // break the indent. 7945 verifyFormat("if (true)\n" 7946 " class X x;\n" 7947 "else\n" 7948 " f();\n"); 7949 7950 // This is simply incomplete. Formatting is not important, but must not crash. 7951 verifyFormat("class A:"); 7952 } 7953 7954 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 7955 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 7956 format("#error Leave all white!!!!! space* alone!\n")); 7957 EXPECT_EQ( 7958 "#warning Leave all white!!!!! space* alone!\n", 7959 format("#warning Leave all white!!!!! space* alone!\n")); 7960 EXPECT_EQ("#error 1", format(" # error 1")); 7961 EXPECT_EQ("#warning 1", format(" # warning 1")); 7962 } 7963 7964 TEST_F(FormatTest, FormatHashIfExpressions) { 7965 verifyFormat("#if AAAA && BBBB"); 7966 verifyFormat("#if (AAAA && BBBB)"); 7967 verifyFormat("#elif (AAAA && BBBB)"); 7968 // FIXME: Come up with a better indentation for #elif. 7969 verifyFormat( 7970 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 7971 " defined(BBBBBBBB)\n" 7972 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 7973 " defined(BBBBBBBB)\n" 7974 "#endif", 7975 getLLVMStyleWithColumns(65)); 7976 } 7977 7978 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 7979 FormatStyle AllowsMergedIf = getGoogleStyle(); 7980 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 7981 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 7982 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 7983 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 7984 EXPECT_EQ("if (true) return 42;", 7985 format("if (true)\nreturn 42;", AllowsMergedIf)); 7986 FormatStyle ShortMergedIf = AllowsMergedIf; 7987 ShortMergedIf.ColumnLimit = 25; 7988 verifyFormat("#define A \\\n" 7989 " if (true) return 42;", 7990 ShortMergedIf); 7991 verifyFormat("#define A \\\n" 7992 " f(); \\\n" 7993 " if (true)\n" 7994 "#define B", 7995 ShortMergedIf); 7996 verifyFormat("#define A \\\n" 7997 " f(); \\\n" 7998 " if (true)\n" 7999 "g();", 8000 ShortMergedIf); 8001 verifyFormat("{\n" 8002 "#ifdef A\n" 8003 " // Comment\n" 8004 " if (true) continue;\n" 8005 "#endif\n" 8006 " // Comment\n" 8007 " if (true) continue;\n" 8008 "}", 8009 ShortMergedIf); 8010 ShortMergedIf.ColumnLimit = 33; 8011 verifyFormat("#define A \\\n" 8012 " if constexpr (true) return 42;", 8013 ShortMergedIf); 8014 ShortMergedIf.ColumnLimit = 29; 8015 verifyFormat("#define A \\\n" 8016 " if (aaaaaaaaaa) return 1; \\\n" 8017 " return 2;", 8018 ShortMergedIf); 8019 ShortMergedIf.ColumnLimit = 28; 8020 verifyFormat("#define A \\\n" 8021 " if (aaaaaaaaaa) \\\n" 8022 " return 1; \\\n" 8023 " return 2;", 8024 ShortMergedIf); 8025 verifyFormat("#define A \\\n" 8026 " if constexpr (aaaaaaa) \\\n" 8027 " return 1; \\\n" 8028 " return 2;", 8029 ShortMergedIf); 8030 } 8031 8032 TEST_F(FormatTest, FormatStarDependingOnContext) { 8033 verifyFormat("void f(int *a);"); 8034 verifyFormat("void f() { f(fint * b); }"); 8035 verifyFormat("class A {\n void f(int *a);\n};"); 8036 verifyFormat("class A {\n int *a;\n};"); 8037 verifyFormat("namespace a {\n" 8038 "namespace b {\n" 8039 "class A {\n" 8040 " void f() {}\n" 8041 " int *a;\n" 8042 "};\n" 8043 "} // namespace b\n" 8044 "} // namespace a"); 8045 } 8046 8047 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 8048 verifyFormat("while"); 8049 verifyFormat("operator"); 8050 } 8051 8052 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 8053 // This code would be painfully slow to format if we didn't skip it. 8054 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 8055 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8056 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8057 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8058 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8059 "A(1, 1)\n" 8060 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 8061 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8062 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8063 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 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 // Deeply nested part is untouched, rest is formatted. 8071 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 8072 format(std::string("int i;\n") + Code + "int j;\n", 8073 getLLVMStyle(), SC_ExpectIncomplete)); 8074 } 8075 8076 //===----------------------------------------------------------------------===// 8077 // Objective-C tests. 8078 //===----------------------------------------------------------------------===// 8079 8080 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 8081 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 8082 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 8083 format("-(NSUInteger)indexOfObject:(id)anObject;")); 8084 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 8085 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 8086 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 8087 format("-(NSInteger)Method3:(id)anObject;")); 8088 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 8089 format("-(NSInteger)Method4:(id)anObject;")); 8090 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 8091 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 8092 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 8093 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 8094 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 8095 "forAllCells:(BOOL)flag;", 8096 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 8097 "forAllCells:(BOOL)flag;")); 8098 8099 // Very long objectiveC method declaration. 8100 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 8101 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 8102 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 8103 " inRange:(NSRange)range\n" 8104 " outRange:(NSRange)out_range\n" 8105 " outRange1:(NSRange)out_range1\n" 8106 " outRange2:(NSRange)out_range2\n" 8107 " outRange3:(NSRange)out_range3\n" 8108 " outRange4:(NSRange)out_range4\n" 8109 " outRange5:(NSRange)out_range5\n" 8110 " outRange6:(NSRange)out_range6\n" 8111 " outRange7:(NSRange)out_range7\n" 8112 " outRange8:(NSRange)out_range8\n" 8113 " outRange9:(NSRange)out_range9;"); 8114 8115 // When the function name has to be wrapped. 8116 FormatStyle Style = getLLVMStyle(); 8117 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 8118 // and always indents instead. 8119 Style.IndentWrappedFunctionNames = false; 8120 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 8121 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 8122 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 8123 "}", 8124 Style); 8125 Style.IndentWrappedFunctionNames = true; 8126 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 8127 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 8128 " anotherName:(NSString)dddddddddddddd {\n" 8129 "}", 8130 Style); 8131 8132 verifyFormat("- (int)sum:(vector<int>)numbers;"); 8133 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 8134 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 8135 // protocol lists (but not for template classes): 8136 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 8137 8138 verifyFormat("- (int (*)())foo:(int (*)())f;"); 8139 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 8140 8141 // If there's no return type (very rare in practice!), LLVM and Google style 8142 // agree. 8143 verifyFormat("- foo;"); 8144 verifyFormat("- foo:(int)f;"); 8145 verifyGoogleFormat("- foo:(int)foo;"); 8146 } 8147 8148 8149 TEST_F(FormatTest, BreaksStringLiterals) { 8150 EXPECT_EQ("\"some text \"\n" 8151 "\"other\";", 8152 format("\"some text other\";", getLLVMStyleWithColumns(12))); 8153 EXPECT_EQ("\"some text \"\n" 8154 "\"other\";", 8155 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 8156 EXPECT_EQ( 8157 "#define A \\\n" 8158 " \"some \" \\\n" 8159 " \"text \" \\\n" 8160 " \"other\";", 8161 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 8162 EXPECT_EQ( 8163 "#define A \\\n" 8164 " \"so \" \\\n" 8165 " \"text \" \\\n" 8166 " \"other\";", 8167 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 8168 8169 EXPECT_EQ("\"some text\"", 8170 format("\"some text\"", getLLVMStyleWithColumns(1))); 8171 EXPECT_EQ("\"some text\"", 8172 format("\"some text\"", getLLVMStyleWithColumns(11))); 8173 EXPECT_EQ("\"some \"\n" 8174 "\"text\"", 8175 format("\"some text\"", getLLVMStyleWithColumns(10))); 8176 EXPECT_EQ("\"some \"\n" 8177 "\"text\"", 8178 format("\"some text\"", getLLVMStyleWithColumns(7))); 8179 EXPECT_EQ("\"some\"\n" 8180 "\" tex\"\n" 8181 "\"t\"", 8182 format("\"some text\"", getLLVMStyleWithColumns(6))); 8183 EXPECT_EQ("\"some\"\n" 8184 "\" tex\"\n" 8185 "\" and\"", 8186 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 8187 EXPECT_EQ("\"some\"\n" 8188 "\"/tex\"\n" 8189 "\"/and\"", 8190 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 8191 8192 EXPECT_EQ("variable =\n" 8193 " \"long string \"\n" 8194 " \"literal\";", 8195 format("variable = \"long string literal\";", 8196 getLLVMStyleWithColumns(20))); 8197 8198 EXPECT_EQ("variable = f(\n" 8199 " \"long string \"\n" 8200 " \"literal\",\n" 8201 " short,\n" 8202 " loooooooooooooooooooong);", 8203 format("variable = f(\"long string literal\", short, " 8204 "loooooooooooooooooooong);", 8205 getLLVMStyleWithColumns(20))); 8206 8207 EXPECT_EQ( 8208 "f(g(\"long string \"\n" 8209 " \"literal\"),\n" 8210 " b);", 8211 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 8212 EXPECT_EQ("f(g(\"long string \"\n" 8213 " \"literal\",\n" 8214 " a),\n" 8215 " b);", 8216 format("f(g(\"long string literal\", a), b);", 8217 getLLVMStyleWithColumns(20))); 8218 EXPECT_EQ( 8219 "f(\"one two\".split(\n" 8220 " variable));", 8221 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 8222 EXPECT_EQ("f(\"one two three four five six \"\n" 8223 " \"seven\".split(\n" 8224 " really_looooong_variable));", 8225 format("f(\"one two three four five six seven\"." 8226 "split(really_looooong_variable));", 8227 getLLVMStyleWithColumns(33))); 8228 8229 EXPECT_EQ("f(\"some \"\n" 8230 " \"text\",\n" 8231 " other);", 8232 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 8233 8234 // Only break as a last resort. 8235 verifyFormat( 8236 "aaaaaaaaaaaaaaaaaaaa(\n" 8237 " aaaaaaaaaaaaaaaaaaaa,\n" 8238 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 8239 8240 EXPECT_EQ("\"splitmea\"\n" 8241 "\"trandomp\"\n" 8242 "\"oint\"", 8243 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 8244 8245 EXPECT_EQ("\"split/\"\n" 8246 "\"pathat/\"\n" 8247 "\"slashes\"", 8248 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8249 8250 EXPECT_EQ("\"split/\"\n" 8251 "\"pathat/\"\n" 8252 "\"slashes\"", 8253 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8254 EXPECT_EQ("\"split at \"\n" 8255 "\"spaces/at/\"\n" 8256 "\"slashes.at.any$\"\n" 8257 "\"non-alphanumeric%\"\n" 8258 "\"1111111111characte\"\n" 8259 "\"rs\"", 8260 format("\"split at " 8261 "spaces/at/" 8262 "slashes.at." 8263 "any$non-" 8264 "alphanumeric%" 8265 "1111111111characte" 8266 "rs\"", 8267 getLLVMStyleWithColumns(20))); 8268 8269 // Verify that splitting the strings understands 8270 // Style::AlwaysBreakBeforeMultilineStrings. 8271 EXPECT_EQ( 8272 "aaaaaaaaaaaa(\n" 8273 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 8274 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 8275 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 8276 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8277 "aaaaaaaaaaaaaaaaaaaaaa\");", 8278 getGoogleStyle())); 8279 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8280 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 8281 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 8282 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8283 "aaaaaaaaaaaaaaaaaaaaaa\";", 8284 getGoogleStyle())); 8285 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8286 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8287 format("llvm::outs() << " 8288 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 8289 "aaaaaaaaaaaaaaaaaaa\";")); 8290 EXPECT_EQ("ffff(\n" 8291 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8292 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8293 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8294 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8295 getGoogleStyle())); 8296 8297 FormatStyle Style = getLLVMStyleWithColumns(12); 8298 Style.BreakStringLiterals = false; 8299 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 8300 8301 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 8302 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8303 EXPECT_EQ("#define A \\\n" 8304 " \"some \" \\\n" 8305 " \"text \" \\\n" 8306 " \"other\";", 8307 format("#define A \"some text other\";", AlignLeft)); 8308 } 8309 8310 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 8311 EXPECT_EQ("C a = \"some more \"\n" 8312 " \"text\";", 8313 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 8314 } 8315 8316 TEST_F(FormatTest, FullyRemoveEmptyLines) { 8317 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 8318 NoEmptyLines.MaxEmptyLinesToKeep = 0; 8319 EXPECT_EQ("int i = a(b());", 8320 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 8321 } 8322 8323 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 8324 EXPECT_EQ( 8325 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8326 "(\n" 8327 " \"x\t\");", 8328 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8329 "aaaaaaa(" 8330 "\"x\t\");")); 8331 } 8332 8333 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 8334 EXPECT_EQ( 8335 "u8\"utf8 string \"\n" 8336 "u8\"literal\";", 8337 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 8338 EXPECT_EQ( 8339 "u\"utf16 string \"\n" 8340 "u\"literal\";", 8341 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 8342 EXPECT_EQ( 8343 "U\"utf32 string \"\n" 8344 "U\"literal\";", 8345 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 8346 EXPECT_EQ("L\"wide string \"\n" 8347 "L\"literal\";", 8348 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 8349 EXPECT_EQ("@\"NSString \"\n" 8350 "@\"literal\";", 8351 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 8352 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 8353 8354 // This input makes clang-format try to split the incomplete unicode escape 8355 // sequence, which used to lead to a crasher. 8356 verifyNoCrash( 8357 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 8358 getLLVMStyleWithColumns(60)); 8359 } 8360 8361 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 8362 FormatStyle Style = getGoogleStyleWithColumns(15); 8363 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 8364 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 8365 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 8366 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 8367 EXPECT_EQ("u8R\"x(raw literal)x\";", 8368 format("u8R\"x(raw literal)x\";", Style)); 8369 } 8370 8371 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 8372 FormatStyle Style = getLLVMStyleWithColumns(20); 8373 EXPECT_EQ( 8374 "_T(\"aaaaaaaaaaaaaa\")\n" 8375 "_T(\"aaaaaaaaaaaaaa\")\n" 8376 "_T(\"aaaaaaaaaaaa\")", 8377 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 8378 EXPECT_EQ("f(x,\n" 8379 " _T(\"aaaaaaaaaaaa\")\n" 8380 " _T(\"aaa\"),\n" 8381 " z);", 8382 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 8383 8384 // FIXME: Handle embedded spaces in one iteration. 8385 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 8386 // "_T(\"aaaaaaaaaaaaa\")\n" 8387 // "_T(\"aaaaaaaaaaaaa\")\n" 8388 // "_T(\"a\")", 8389 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8390 // getLLVMStyleWithColumns(20))); 8391 EXPECT_EQ( 8392 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8393 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 8394 EXPECT_EQ("f(\n" 8395 "#if !TEST\n" 8396 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8397 "#endif\n" 8398 ");", 8399 format("f(\n" 8400 "#if !TEST\n" 8401 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8402 "#endif\n" 8403 ");")); 8404 EXPECT_EQ("f(\n" 8405 "\n" 8406 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 8407 format("f(\n" 8408 "\n" 8409 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 8410 } 8411 8412 TEST_F(FormatTest, BreaksStringLiteralOperands) { 8413 // In a function call with two operands, the second can be broken with no line 8414 // break before it. 8415 EXPECT_EQ("func(a, \"long long \"\n" 8416 " \"long long\");", 8417 format("func(a, \"long long long long\");", 8418 getLLVMStyleWithColumns(24))); 8419 // In a function call with three operands, the second must be broken with a 8420 // line break before it. 8421 EXPECT_EQ("func(a,\n" 8422 " \"long long long \"\n" 8423 " \"long\",\n" 8424 " c);", 8425 format("func(a, \"long long long long\", c);", 8426 getLLVMStyleWithColumns(24))); 8427 // In a function call with three operands, the third must be broken with a 8428 // line break before it. 8429 EXPECT_EQ("func(a, b,\n" 8430 " \"long long long \"\n" 8431 " \"long\");", 8432 format("func(a, b, \"long long long long\");", 8433 getLLVMStyleWithColumns(24))); 8434 // In a function call with three operands, both the second and the third must 8435 // be broken with a line break before them. 8436 EXPECT_EQ("func(a,\n" 8437 " \"long long long \"\n" 8438 " \"long\",\n" 8439 " \"long long long \"\n" 8440 " \"long\");", 8441 format("func(a, \"long long long long\", \"long long long long\");", 8442 getLLVMStyleWithColumns(24))); 8443 // In a chain of << with two operands, the second can be broken with no line 8444 // break before it. 8445 EXPECT_EQ("a << \"line line \"\n" 8446 " \"line\";", 8447 format("a << \"line line line\";", 8448 getLLVMStyleWithColumns(20))); 8449 // In a chain of << with three operands, the second can be broken with no line 8450 // break before it. 8451 EXPECT_EQ("abcde << \"line \"\n" 8452 " \"line line\"\n" 8453 " << c;", 8454 format("abcde << \"line line line\" << c;", 8455 getLLVMStyleWithColumns(20))); 8456 // In a chain of << with three operands, the third must be broken with a line 8457 // break before it. 8458 EXPECT_EQ("a << b\n" 8459 " << \"line line \"\n" 8460 " \"line\";", 8461 format("a << b << \"line line line\";", 8462 getLLVMStyleWithColumns(20))); 8463 // In a chain of << with three operands, the second can be broken with no line 8464 // break before it and the third must be broken with a line break before it. 8465 EXPECT_EQ("abcd << \"line line \"\n" 8466 " \"line\"\n" 8467 " << \"line line \"\n" 8468 " \"line\";", 8469 format("abcd << \"line line line\" << \"line line line\";", 8470 getLLVMStyleWithColumns(20))); 8471 // In a chain of binary operators with two operands, the second can be broken 8472 // with no line break before it. 8473 EXPECT_EQ("abcd + \"line line \"\n" 8474 " \"line line\";", 8475 format("abcd + \"line line line line\";", 8476 getLLVMStyleWithColumns(20))); 8477 // In a chain of binary operators with three operands, the second must be 8478 // broken with a line break before it. 8479 EXPECT_EQ("abcd +\n" 8480 " \"line line \"\n" 8481 " \"line line\" +\n" 8482 " e;", 8483 format("abcd + \"line line line line\" + e;", 8484 getLLVMStyleWithColumns(20))); 8485 // In a function call with two operands, with AlignAfterOpenBracket enabled, 8486 // the first must be broken with a line break before it. 8487 FormatStyle Style = getLLVMStyleWithColumns(25); 8488 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8489 EXPECT_EQ("someFunction(\n" 8490 " \"long long long \"\n" 8491 " \"long\",\n" 8492 " a);", 8493 format("someFunction(\"long long long long\", a);", Style)); 8494 } 8495 8496 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8497 EXPECT_EQ( 8498 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8499 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8500 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8501 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8502 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8503 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8504 } 8505 8506 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8507 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8508 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8509 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8510 "multiline raw string literal xxxxxxxxxxxxxx\n" 8511 ")x\",\n" 8512 " a),\n" 8513 " b);", 8514 format("fffffffffff(g(R\"x(\n" 8515 "multiline raw string literal xxxxxxxxxxxxxx\n" 8516 ")x\", a), b);", 8517 getGoogleStyleWithColumns(20))); 8518 EXPECT_EQ("fffffffffff(\n" 8519 " g(R\"x(qqq\n" 8520 "multiline raw string literal xxxxxxxxxxxxxx\n" 8521 ")x\",\n" 8522 " a),\n" 8523 " b);", 8524 format("fffffffffff(g(R\"x(qqq\n" 8525 "multiline raw string literal xxxxxxxxxxxxxx\n" 8526 ")x\", a), b);", 8527 getGoogleStyleWithColumns(20))); 8528 8529 EXPECT_EQ("fffffffffff(R\"x(\n" 8530 "multiline raw string literal xxxxxxxxxxxxxx\n" 8531 ")x\");", 8532 format("fffffffffff(R\"x(\n" 8533 "multiline raw string literal xxxxxxxxxxxxxx\n" 8534 ")x\");", 8535 getGoogleStyleWithColumns(20))); 8536 EXPECT_EQ("fffffffffff(R\"x(\n" 8537 "multiline raw string literal xxxxxxxxxxxxxx\n" 8538 ")x\" + bbbbbb);", 8539 format("fffffffffff(R\"x(\n" 8540 "multiline raw string literal xxxxxxxxxxxxxx\n" 8541 ")x\" + bbbbbb);", 8542 getGoogleStyleWithColumns(20))); 8543 EXPECT_EQ("fffffffffff(\n" 8544 " R\"x(\n" 8545 "multiline raw string literal xxxxxxxxxxxxxx\n" 8546 ")x\" +\n" 8547 " bbbbbb);", 8548 format("fffffffffff(\n" 8549 " R\"x(\n" 8550 "multiline raw string literal xxxxxxxxxxxxxx\n" 8551 ")x\" + bbbbbb);", 8552 getGoogleStyleWithColumns(20))); 8553 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 8554 format("fffffffffff(\n" 8555 " R\"(single line raw string)\" + bbbbbb);")); 8556 } 8557 8558 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8559 verifyFormat("string a = \"unterminated;"); 8560 EXPECT_EQ("function(\"unterminated,\n" 8561 " OtherParameter);", 8562 format("function( \"unterminated,\n" 8563 " OtherParameter);")); 8564 } 8565 8566 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8567 FormatStyle Style = getLLVMStyle(); 8568 Style.Standard = FormatStyle::LS_Cpp03; 8569 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8570 format("#define x(_a) printf(\"foo\"_a);", Style)); 8571 } 8572 8573 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8574 8575 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8576 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8577 " \"ddeeefff\");", 8578 format("someFunction(\"aaabbbcccdddeeefff\");", 8579 getLLVMStyleWithColumns(25))); 8580 EXPECT_EQ("someFunction1234567890(\n" 8581 " \"aaabbbcccdddeeefff\");", 8582 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8583 getLLVMStyleWithColumns(26))); 8584 EXPECT_EQ("someFunction1234567890(\n" 8585 " \"aaabbbcccdddeeeff\"\n" 8586 " \"f\");", 8587 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8588 getLLVMStyleWithColumns(25))); 8589 EXPECT_EQ("someFunction1234567890(\n" 8590 " \"aaabbbcccdddeeeff\"\n" 8591 " \"f\");", 8592 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8593 getLLVMStyleWithColumns(24))); 8594 EXPECT_EQ("someFunction(\n" 8595 " \"aaabbbcc ddde \"\n" 8596 " \"efff\");", 8597 format("someFunction(\"aaabbbcc ddde efff\");", 8598 getLLVMStyleWithColumns(25))); 8599 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8600 " \"ddeeefff\");", 8601 format("someFunction(\"aaabbbccc ddeeefff\");", 8602 getLLVMStyleWithColumns(25))); 8603 EXPECT_EQ("someFunction1234567890(\n" 8604 " \"aaabb \"\n" 8605 " \"cccdddeeefff\");", 8606 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8607 getLLVMStyleWithColumns(25))); 8608 EXPECT_EQ("#define A \\\n" 8609 " string s = \\\n" 8610 " \"123456789\" \\\n" 8611 " \"0\"; \\\n" 8612 " int i;", 8613 format("#define A string s = \"1234567890\"; int i;", 8614 getLLVMStyleWithColumns(20))); 8615 EXPECT_EQ("someFunction(\n" 8616 " \"aaabbbcc \"\n" 8617 " \"dddeeefff\");", 8618 format("someFunction(\"aaabbbcc dddeeefff\");", 8619 getLLVMStyleWithColumns(25))); 8620 } 8621 8622 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8623 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8624 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8625 EXPECT_EQ("\"test\"\n" 8626 "\"\\n\"", 8627 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8628 EXPECT_EQ("\"tes\\\\\"\n" 8629 "\"n\"", 8630 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8631 EXPECT_EQ("\"\\\\\\\\\"\n" 8632 "\"\\n\"", 8633 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8634 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8635 EXPECT_EQ("\"\\uff01\"\n" 8636 "\"test\"", 8637 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8638 EXPECT_EQ("\"\\Uff01ff02\"", 8639 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8640 EXPECT_EQ("\"\\x000000000001\"\n" 8641 "\"next\"", 8642 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8643 EXPECT_EQ("\"\\x000000000001next\"", 8644 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8645 EXPECT_EQ("\"\\x000000000001\"", 8646 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8647 EXPECT_EQ("\"test\"\n" 8648 "\"\\000000\"\n" 8649 "\"000001\"", 8650 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8651 EXPECT_EQ("\"test\\000\"\n" 8652 "\"00000000\"\n" 8653 "\"1\"", 8654 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8655 } 8656 8657 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8658 verifyFormat("void f() {\n" 8659 " return g() {}\n" 8660 " void h() {}"); 8661 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8662 "g();\n" 8663 "}"); 8664 } 8665 8666 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8667 verifyFormat( 8668 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8669 } 8670 8671 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8672 verifyFormat("class X {\n" 8673 " void f() {\n" 8674 " }\n" 8675 "};", 8676 getLLVMStyleWithColumns(12)); 8677 } 8678 8679 TEST_F(FormatTest, ConfigurableIndentWidth) { 8680 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8681 EightIndent.IndentWidth = 8; 8682 EightIndent.ContinuationIndentWidth = 8; 8683 verifyFormat("void f() {\n" 8684 " someFunction();\n" 8685 " if (true) {\n" 8686 " f();\n" 8687 " }\n" 8688 "}", 8689 EightIndent); 8690 verifyFormat("class X {\n" 8691 " void f() {\n" 8692 " }\n" 8693 "};", 8694 EightIndent); 8695 verifyFormat("int x[] = {\n" 8696 " call(),\n" 8697 " call()};", 8698 EightIndent); 8699 } 8700 8701 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8702 verifyFormat("double\n" 8703 "f();", 8704 getLLVMStyleWithColumns(8)); 8705 } 8706 8707 TEST_F(FormatTest, ConfigurableUseOfTab) { 8708 FormatStyle Tab = getLLVMStyleWithColumns(42); 8709 Tab.IndentWidth = 8; 8710 Tab.UseTab = FormatStyle::UT_Always; 8711 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8712 8713 EXPECT_EQ("if (aaaaaaaa && // q\n" 8714 " bb)\t\t// w\n" 8715 "\t;", 8716 format("if (aaaaaaaa &&// q\n" 8717 "bb)// w\n" 8718 ";", 8719 Tab)); 8720 EXPECT_EQ("if (aaa && bbb) // w\n" 8721 "\t;", 8722 format("if(aaa&&bbb)// w\n" 8723 ";", 8724 Tab)); 8725 8726 verifyFormat("class X {\n" 8727 "\tvoid f() {\n" 8728 "\t\tsomeFunction(parameter1,\n" 8729 "\t\t\t parameter2);\n" 8730 "\t}\n" 8731 "};", 8732 Tab); 8733 verifyFormat("#define A \\\n" 8734 "\tvoid f() { \\\n" 8735 "\t\tsomeFunction( \\\n" 8736 "\t\t parameter1, \\\n" 8737 "\t\t parameter2); \\\n" 8738 "\t}", 8739 Tab); 8740 8741 Tab.TabWidth = 4; 8742 Tab.IndentWidth = 8; 8743 verifyFormat("class TabWidth4Indent8 {\n" 8744 "\t\tvoid f() {\n" 8745 "\t\t\t\tsomeFunction(parameter1,\n" 8746 "\t\t\t\t\t\t\t parameter2);\n" 8747 "\t\t}\n" 8748 "};", 8749 Tab); 8750 8751 Tab.TabWidth = 4; 8752 Tab.IndentWidth = 4; 8753 verifyFormat("class TabWidth4Indent4 {\n" 8754 "\tvoid f() {\n" 8755 "\t\tsomeFunction(parameter1,\n" 8756 "\t\t\t\t\t parameter2);\n" 8757 "\t}\n" 8758 "};", 8759 Tab); 8760 8761 Tab.TabWidth = 8; 8762 Tab.IndentWidth = 4; 8763 verifyFormat("class TabWidth8Indent4 {\n" 8764 " void f() {\n" 8765 "\tsomeFunction(parameter1,\n" 8766 "\t\t parameter2);\n" 8767 " }\n" 8768 "};", 8769 Tab); 8770 8771 Tab.TabWidth = 8; 8772 Tab.IndentWidth = 8; 8773 EXPECT_EQ("/*\n" 8774 "\t a\t\tcomment\n" 8775 "\t in multiple lines\n" 8776 " */", 8777 format(" /*\t \t \n" 8778 " \t \t a\t\tcomment\t \t\n" 8779 " \t \t in multiple lines\t\n" 8780 " \t */", 8781 Tab)); 8782 8783 Tab.UseTab = FormatStyle::UT_ForIndentation; 8784 verifyFormat("{\n" 8785 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8786 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8787 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8788 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8789 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8790 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8791 "};", 8792 Tab); 8793 verifyFormat("enum AA {\n" 8794 "\ta1, // Force multiple lines\n" 8795 "\ta2,\n" 8796 "\ta3\n" 8797 "};", 8798 Tab); 8799 EXPECT_EQ("if (aaaaaaaa && // q\n" 8800 " bb) // w\n" 8801 "\t;", 8802 format("if (aaaaaaaa &&// q\n" 8803 "bb)// w\n" 8804 ";", 8805 Tab)); 8806 verifyFormat("class X {\n" 8807 "\tvoid f() {\n" 8808 "\t\tsomeFunction(parameter1,\n" 8809 "\t\t parameter2);\n" 8810 "\t}\n" 8811 "};", 8812 Tab); 8813 verifyFormat("{\n" 8814 "\tQ(\n" 8815 "\t {\n" 8816 "\t\t int a;\n" 8817 "\t\t someFunction(aaaaaaaa,\n" 8818 "\t\t bbbbbbb);\n" 8819 "\t },\n" 8820 "\t p);\n" 8821 "}", 8822 Tab); 8823 EXPECT_EQ("{\n" 8824 "\t/* aaaa\n" 8825 "\t bbbb */\n" 8826 "}", 8827 format("{\n" 8828 "/* aaaa\n" 8829 " bbbb */\n" 8830 "}", 8831 Tab)); 8832 EXPECT_EQ("{\n" 8833 "\t/*\n" 8834 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8835 "\t bbbbbbbbbbbbb\n" 8836 "\t*/\n" 8837 "}", 8838 format("{\n" 8839 "/*\n" 8840 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8841 "*/\n" 8842 "}", 8843 Tab)); 8844 EXPECT_EQ("{\n" 8845 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8846 "\t// bbbbbbbbbbbbb\n" 8847 "}", 8848 format("{\n" 8849 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8850 "}", 8851 Tab)); 8852 EXPECT_EQ("{\n" 8853 "\t/*\n" 8854 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8855 "\t bbbbbbbbbbbbb\n" 8856 "\t*/\n" 8857 "}", 8858 format("{\n" 8859 "\t/*\n" 8860 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8861 "\t*/\n" 8862 "}", 8863 Tab)); 8864 EXPECT_EQ("{\n" 8865 "\t/*\n" 8866 "\n" 8867 "\t*/\n" 8868 "}", 8869 format("{\n" 8870 "\t/*\n" 8871 "\n" 8872 "\t*/\n" 8873 "}", 8874 Tab)); 8875 EXPECT_EQ("{\n" 8876 "\t/*\n" 8877 " asdf\n" 8878 "\t*/\n" 8879 "}", 8880 format("{\n" 8881 "\t/*\n" 8882 " asdf\n" 8883 "\t*/\n" 8884 "}", 8885 Tab)); 8886 8887 Tab.UseTab = FormatStyle::UT_Never; 8888 EXPECT_EQ("/*\n" 8889 " a\t\tcomment\n" 8890 " in multiple lines\n" 8891 " */", 8892 format(" /*\t \t \n" 8893 " \t \t a\t\tcomment\t \t\n" 8894 " \t \t in multiple lines\t\n" 8895 " \t */", 8896 Tab)); 8897 EXPECT_EQ("/* some\n" 8898 " comment */", 8899 format(" \t \t /* some\n" 8900 " \t \t comment */", 8901 Tab)); 8902 EXPECT_EQ("int a; /* some\n" 8903 " comment */", 8904 format(" \t \t int a; /* some\n" 8905 " \t \t comment */", 8906 Tab)); 8907 8908 EXPECT_EQ("int a; /* some\n" 8909 "comment */", 8910 format(" \t \t int\ta; /* some\n" 8911 " \t \t comment */", 8912 Tab)); 8913 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8914 " comment */", 8915 format(" \t \t f(\"\t\t\"); /* some\n" 8916 " \t \t comment */", 8917 Tab)); 8918 EXPECT_EQ("{\n" 8919 " /*\n" 8920 " * Comment\n" 8921 " */\n" 8922 " int i;\n" 8923 "}", 8924 format("{\n" 8925 "\t/*\n" 8926 "\t * Comment\n" 8927 "\t */\n" 8928 "\t int i;\n" 8929 "}")); 8930 8931 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8932 Tab.TabWidth = 8; 8933 Tab.IndentWidth = 8; 8934 EXPECT_EQ("if (aaaaaaaa && // q\n" 8935 " bb) // w\n" 8936 "\t;", 8937 format("if (aaaaaaaa &&// q\n" 8938 "bb)// w\n" 8939 ";", 8940 Tab)); 8941 EXPECT_EQ("if (aaa && bbb) // w\n" 8942 "\t;", 8943 format("if(aaa&&bbb)// w\n" 8944 ";", 8945 Tab)); 8946 verifyFormat("class X {\n" 8947 "\tvoid f() {\n" 8948 "\t\tsomeFunction(parameter1,\n" 8949 "\t\t\t parameter2);\n" 8950 "\t}\n" 8951 "};", 8952 Tab); 8953 verifyFormat("#define A \\\n" 8954 "\tvoid f() { \\\n" 8955 "\t\tsomeFunction( \\\n" 8956 "\t\t parameter1, \\\n" 8957 "\t\t parameter2); \\\n" 8958 "\t}", 8959 Tab); 8960 Tab.TabWidth = 4; 8961 Tab.IndentWidth = 8; 8962 verifyFormat("class TabWidth4Indent8 {\n" 8963 "\t\tvoid f() {\n" 8964 "\t\t\t\tsomeFunction(parameter1,\n" 8965 "\t\t\t\t\t\t\t parameter2);\n" 8966 "\t\t}\n" 8967 "};", 8968 Tab); 8969 Tab.TabWidth = 4; 8970 Tab.IndentWidth = 4; 8971 verifyFormat("class TabWidth4Indent4 {\n" 8972 "\tvoid f() {\n" 8973 "\t\tsomeFunction(parameter1,\n" 8974 "\t\t\t\t\t parameter2);\n" 8975 "\t}\n" 8976 "};", 8977 Tab); 8978 Tab.TabWidth = 8; 8979 Tab.IndentWidth = 4; 8980 verifyFormat("class TabWidth8Indent4 {\n" 8981 " void f() {\n" 8982 "\tsomeFunction(parameter1,\n" 8983 "\t\t parameter2);\n" 8984 " }\n" 8985 "};", 8986 Tab); 8987 Tab.TabWidth = 8; 8988 Tab.IndentWidth = 8; 8989 EXPECT_EQ("/*\n" 8990 "\t a\t\tcomment\n" 8991 "\t in multiple lines\n" 8992 " */", 8993 format(" /*\t \t \n" 8994 " \t \t a\t\tcomment\t \t\n" 8995 " \t \t in multiple lines\t\n" 8996 " \t */", 8997 Tab)); 8998 verifyFormat("{\n" 8999 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9000 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9001 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9002 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9003 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9004 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9005 "};", 9006 Tab); 9007 verifyFormat("enum AA {\n" 9008 "\ta1, // Force multiple lines\n" 9009 "\ta2,\n" 9010 "\ta3\n" 9011 "};", 9012 Tab); 9013 EXPECT_EQ("if (aaaaaaaa && // q\n" 9014 " bb) // w\n" 9015 "\t;", 9016 format("if (aaaaaaaa &&// q\n" 9017 "bb)// w\n" 9018 ";", 9019 Tab)); 9020 verifyFormat("class X {\n" 9021 "\tvoid f() {\n" 9022 "\t\tsomeFunction(parameter1,\n" 9023 "\t\t\t parameter2);\n" 9024 "\t}\n" 9025 "};", 9026 Tab); 9027 verifyFormat("{\n" 9028 "\tQ(\n" 9029 "\t {\n" 9030 "\t\t int a;\n" 9031 "\t\t someFunction(aaaaaaaa,\n" 9032 "\t\t\t\t bbbbbbb);\n" 9033 "\t },\n" 9034 "\t p);\n" 9035 "}", 9036 Tab); 9037 EXPECT_EQ("{\n" 9038 "\t/* aaaa\n" 9039 "\t bbbb */\n" 9040 "}", 9041 format("{\n" 9042 "/* aaaa\n" 9043 " bbbb */\n" 9044 "}", 9045 Tab)); 9046 EXPECT_EQ("{\n" 9047 "\t/*\n" 9048 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9049 "\t bbbbbbbbbbbbb\n" 9050 "\t*/\n" 9051 "}", 9052 format("{\n" 9053 "/*\n" 9054 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9055 "*/\n" 9056 "}", 9057 Tab)); 9058 EXPECT_EQ("{\n" 9059 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9060 "\t// bbbbbbbbbbbbb\n" 9061 "}", 9062 format("{\n" 9063 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9064 "}", 9065 Tab)); 9066 EXPECT_EQ("{\n" 9067 "\t/*\n" 9068 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9069 "\t bbbbbbbbbbbbb\n" 9070 "\t*/\n" 9071 "}", 9072 format("{\n" 9073 "\t/*\n" 9074 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9075 "\t*/\n" 9076 "}", 9077 Tab)); 9078 EXPECT_EQ("{\n" 9079 "\t/*\n" 9080 "\n" 9081 "\t*/\n" 9082 "}", 9083 format("{\n" 9084 "\t/*\n" 9085 "\n" 9086 "\t*/\n" 9087 "}", 9088 Tab)); 9089 EXPECT_EQ("{\n" 9090 "\t/*\n" 9091 " asdf\n" 9092 "\t*/\n" 9093 "}", 9094 format("{\n" 9095 "\t/*\n" 9096 " asdf\n" 9097 "\t*/\n" 9098 "}", 9099 Tab)); 9100 EXPECT_EQ("/*\n" 9101 "\t a\t\tcomment\n" 9102 "\t in multiple lines\n" 9103 " */", 9104 format(" /*\t \t \n" 9105 " \t \t a\t\tcomment\t \t\n" 9106 " \t \t in multiple lines\t\n" 9107 " \t */", 9108 Tab)); 9109 EXPECT_EQ("/* some\n" 9110 " comment */", 9111 format(" \t \t /* some\n" 9112 " \t \t comment */", 9113 Tab)); 9114 EXPECT_EQ("int a; /* some\n" 9115 " comment */", 9116 format(" \t \t int a; /* some\n" 9117 " \t \t comment */", 9118 Tab)); 9119 EXPECT_EQ("int a; /* some\n" 9120 "comment */", 9121 format(" \t \t int\ta; /* some\n" 9122 " \t \t comment */", 9123 Tab)); 9124 EXPECT_EQ("f(\"\t\t\"); /* some\n" 9125 " comment */", 9126 format(" \t \t f(\"\t\t\"); /* some\n" 9127 " \t \t comment */", 9128 Tab)); 9129 EXPECT_EQ("{\n" 9130 " /*\n" 9131 " * Comment\n" 9132 " */\n" 9133 " int i;\n" 9134 "}", 9135 format("{\n" 9136 "\t/*\n" 9137 "\t * Comment\n" 9138 "\t */\n" 9139 "\t int i;\n" 9140 "}")); 9141 Tab.AlignConsecutiveAssignments = true; 9142 Tab.AlignConsecutiveDeclarations = true; 9143 Tab.TabWidth = 4; 9144 Tab.IndentWidth = 4; 9145 verifyFormat("class Assign {\n" 9146 "\tvoid f() {\n" 9147 "\t\tint x = 123;\n" 9148 "\t\tint random = 4;\n" 9149 "\t\tstd::string alphabet =\n" 9150 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 9151 "\t}\n" 9152 "};", 9153 Tab); 9154 } 9155 9156 TEST_F(FormatTest, CalculatesOriginalColumn) { 9157 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9158 "q\"; /* some\n" 9159 " comment */", 9160 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9161 "q\"; /* some\n" 9162 " comment */", 9163 getLLVMStyle())); 9164 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 9165 "/* some\n" 9166 " comment */", 9167 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 9168 " /* some\n" 9169 " comment */", 9170 getLLVMStyle())); 9171 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9172 "qqq\n" 9173 "/* some\n" 9174 " comment */", 9175 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9176 "qqq\n" 9177 " /* some\n" 9178 " comment */", 9179 getLLVMStyle())); 9180 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9181 "wwww; /* some\n" 9182 " comment */", 9183 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9184 "wwww; /* some\n" 9185 " comment */", 9186 getLLVMStyle())); 9187 } 9188 9189 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 9190 FormatStyle NoSpace = getLLVMStyle(); 9191 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 9192 9193 verifyFormat("while(true)\n" 9194 " continue;", 9195 NoSpace); 9196 verifyFormat("for(;;)\n" 9197 " continue;", 9198 NoSpace); 9199 verifyFormat("if(true)\n" 9200 " f();\n" 9201 "else if(true)\n" 9202 " f();", 9203 NoSpace); 9204 verifyFormat("do {\n" 9205 " do_something();\n" 9206 "} while(something());", 9207 NoSpace); 9208 verifyFormat("switch(x) {\n" 9209 "default:\n" 9210 " break;\n" 9211 "}", 9212 NoSpace); 9213 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 9214 verifyFormat("size_t x = sizeof(x);", NoSpace); 9215 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 9216 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 9217 verifyFormat("alignas(128) char a[128];", NoSpace); 9218 verifyFormat("size_t x = alignof(MyType);", NoSpace); 9219 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 9220 verifyFormat("int f() throw(Deprecated);", NoSpace); 9221 verifyFormat("typedef void (*cb)(int);", NoSpace); 9222 verifyFormat("T A::operator()();", NoSpace); 9223 verifyFormat("X A::operator++(T);", NoSpace); 9224 9225 FormatStyle Space = getLLVMStyle(); 9226 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 9227 9228 verifyFormat("int f ();", Space); 9229 verifyFormat("void f (int a, T b) {\n" 9230 " while (true)\n" 9231 " continue;\n" 9232 "}", 9233 Space); 9234 verifyFormat("if (true)\n" 9235 " f ();\n" 9236 "else if (true)\n" 9237 " f ();", 9238 Space); 9239 verifyFormat("do {\n" 9240 " do_something ();\n" 9241 "} while (something ());", 9242 Space); 9243 verifyFormat("switch (x) {\n" 9244 "default:\n" 9245 " break;\n" 9246 "}", 9247 Space); 9248 verifyFormat("A::A () : a (1) {}", Space); 9249 verifyFormat("void f () __attribute__ ((asdf));", Space); 9250 verifyFormat("*(&a + 1);\n" 9251 "&((&a)[1]);\n" 9252 "a[(b + c) * d];\n" 9253 "(((a + 1) * 2) + 3) * 4;", 9254 Space); 9255 verifyFormat("#define A(x) x", Space); 9256 verifyFormat("#define A (x) x", Space); 9257 verifyFormat("#if defined(x)\n" 9258 "#endif", 9259 Space); 9260 verifyFormat("auto i = std::make_unique<int> (5);", Space); 9261 verifyFormat("size_t x = sizeof (x);", Space); 9262 verifyFormat("auto f (int x) -> decltype (x);", Space); 9263 verifyFormat("int f (T x) noexcept (x.create ());", Space); 9264 verifyFormat("alignas (128) char a[128];", Space); 9265 verifyFormat("size_t x = alignof (MyType);", Space); 9266 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 9267 verifyFormat("int f () throw (Deprecated);", Space); 9268 verifyFormat("typedef void (*cb) (int);", Space); 9269 verifyFormat("T A::operator() ();", Space); 9270 verifyFormat("X A::operator++ (T);", Space); 9271 } 9272 9273 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 9274 FormatStyle Spaces = getLLVMStyle(); 9275 9276 Spaces.SpacesInParentheses = true; 9277 verifyFormat("do_something( ::globalVar );", Spaces); 9278 verifyFormat("call( x, y, z );", Spaces); 9279 verifyFormat("call();", Spaces); 9280 verifyFormat("std::function<void( int, int )> callback;", Spaces); 9281 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 9282 Spaces); 9283 verifyFormat("while ( (bool)1 )\n" 9284 " continue;", 9285 Spaces); 9286 verifyFormat("for ( ;; )\n" 9287 " continue;", 9288 Spaces); 9289 verifyFormat("if ( true )\n" 9290 " f();\n" 9291 "else if ( true )\n" 9292 " f();", 9293 Spaces); 9294 verifyFormat("do {\n" 9295 " do_something( (int)i );\n" 9296 "} while ( something() );", 9297 Spaces); 9298 verifyFormat("switch ( x ) {\n" 9299 "default:\n" 9300 " break;\n" 9301 "}", 9302 Spaces); 9303 9304 Spaces.SpacesInParentheses = false; 9305 Spaces.SpacesInCStyleCastParentheses = true; 9306 verifyFormat("Type *A = ( Type * )P;", Spaces); 9307 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 9308 verifyFormat("x = ( int32 )y;", Spaces); 9309 verifyFormat("int a = ( int )(2.0f);", Spaces); 9310 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 9311 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 9312 verifyFormat("#define x (( int )-1)", Spaces); 9313 9314 // Run the first set of tests again with: 9315 Spaces.SpacesInParentheses = false; 9316 Spaces.SpaceInEmptyParentheses = true; 9317 Spaces.SpacesInCStyleCastParentheses = true; 9318 verifyFormat("call(x, y, z);", Spaces); 9319 verifyFormat("call( );", Spaces); 9320 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9321 verifyFormat("while (( bool )1)\n" 9322 " continue;", 9323 Spaces); 9324 verifyFormat("for (;;)\n" 9325 " continue;", 9326 Spaces); 9327 verifyFormat("if (true)\n" 9328 " f( );\n" 9329 "else if (true)\n" 9330 " f( );", 9331 Spaces); 9332 verifyFormat("do {\n" 9333 " do_something(( int )i);\n" 9334 "} while (something( ));", 9335 Spaces); 9336 verifyFormat("switch (x) {\n" 9337 "default:\n" 9338 " break;\n" 9339 "}", 9340 Spaces); 9341 9342 // Run the first set of tests again with: 9343 Spaces.SpaceAfterCStyleCast = true; 9344 verifyFormat("call(x, y, z);", Spaces); 9345 verifyFormat("call( );", Spaces); 9346 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9347 verifyFormat("while (( bool ) 1)\n" 9348 " continue;", 9349 Spaces); 9350 verifyFormat("for (;;)\n" 9351 " continue;", 9352 Spaces); 9353 verifyFormat("if (true)\n" 9354 " f( );\n" 9355 "else if (true)\n" 9356 " f( );", 9357 Spaces); 9358 verifyFormat("do {\n" 9359 " do_something(( int ) i);\n" 9360 "} while (something( ));", 9361 Spaces); 9362 verifyFormat("switch (x) {\n" 9363 "default:\n" 9364 " break;\n" 9365 "}", 9366 Spaces); 9367 9368 // Run subset of tests again with: 9369 Spaces.SpacesInCStyleCastParentheses = false; 9370 Spaces.SpaceAfterCStyleCast = true; 9371 verifyFormat("while ((bool) 1)\n" 9372 " continue;", 9373 Spaces); 9374 verifyFormat("do {\n" 9375 " do_something((int) i);\n" 9376 "} while (something( ));", 9377 Spaces); 9378 } 9379 9380 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 9381 verifyFormat("int a[5];"); 9382 verifyFormat("a[3] += 42;"); 9383 9384 FormatStyle Spaces = getLLVMStyle(); 9385 Spaces.SpacesInSquareBrackets = true; 9386 // Lambdas unchanged. 9387 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 9388 verifyFormat("return [i, args...] {};", Spaces); 9389 9390 // Not lambdas. 9391 verifyFormat("int a[ 5 ];", Spaces); 9392 verifyFormat("a[ 3 ] += 42;", Spaces); 9393 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 9394 verifyFormat("double &operator[](int i) { return 0; }\n" 9395 "int i;", 9396 Spaces); 9397 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 9398 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 9399 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 9400 } 9401 9402 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 9403 verifyFormat("int a = 5;"); 9404 verifyFormat("a += 42;"); 9405 verifyFormat("a or_eq 8;"); 9406 9407 FormatStyle Spaces = getLLVMStyle(); 9408 Spaces.SpaceBeforeAssignmentOperators = false; 9409 verifyFormat("int a= 5;", Spaces); 9410 verifyFormat("a+= 42;", Spaces); 9411 verifyFormat("a or_eq 8;", Spaces); 9412 } 9413 9414 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 9415 verifyFormat("class Foo : public Bar {};"); 9416 verifyFormat("Foo::Foo() : foo(1) {}"); 9417 verifyFormat("for (auto a : b) {\n}"); 9418 verifyFormat("int x = a ? b : c;"); 9419 verifyFormat("{\n" 9420 "label0:\n" 9421 " int x = 0;\n" 9422 "}"); 9423 verifyFormat("switch (x) {\n" 9424 "case 1:\n" 9425 "default:\n" 9426 "}"); 9427 9428 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 9429 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 9430 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 9431 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 9432 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 9433 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 9434 verifyFormat("{\n" 9435 "label1:\n" 9436 " int x = 0;\n" 9437 "}", 9438 CtorInitializerStyle); 9439 verifyFormat("switch (x) {\n" 9440 "case 1:\n" 9441 "default:\n" 9442 "}", 9443 CtorInitializerStyle); 9444 CtorInitializerStyle.BreakConstructorInitializers = 9445 FormatStyle::BCIS_AfterColon; 9446 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 9447 " aaaaaaaaaaaaaaaa(1),\n" 9448 " bbbbbbbbbbbbbbbb(2) {}", 9449 CtorInitializerStyle); 9450 CtorInitializerStyle.BreakConstructorInitializers = 9451 FormatStyle::BCIS_BeforeComma; 9452 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9453 " : aaaaaaaaaaaaaaaa(1)\n" 9454 " , bbbbbbbbbbbbbbbb(2) {}", 9455 CtorInitializerStyle); 9456 CtorInitializerStyle.BreakConstructorInitializers = 9457 FormatStyle::BCIS_BeforeColon; 9458 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9459 " : aaaaaaaaaaaaaaaa(1),\n" 9460 " bbbbbbbbbbbbbbbb(2) {}", 9461 CtorInitializerStyle); 9462 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 9463 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9464 ": aaaaaaaaaaaaaaaa(1),\n" 9465 " bbbbbbbbbbbbbbbb(2) {}", 9466 CtorInitializerStyle); 9467 9468 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 9469 InheritanceStyle.SpaceBeforeInheritanceColon = false; 9470 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 9471 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 9472 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 9473 verifyFormat("int x = a ? b : c;", InheritanceStyle); 9474 verifyFormat("{\n" 9475 "label2:\n" 9476 " int x = 0;\n" 9477 "}", 9478 InheritanceStyle); 9479 verifyFormat("switch (x) {\n" 9480 "case 1:\n" 9481 "default:\n" 9482 "}", 9483 InheritanceStyle); 9484 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 9485 verifyFormat("class Foooooooooooooooooooooo:\n" 9486 " public aaaaaaaaaaaaaaaaaa,\n" 9487 " public bbbbbbbbbbbbbbbbbb {\n" 9488 "}", 9489 InheritanceStyle); 9490 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 9491 verifyFormat("class Foooooooooooooooooooooo\n" 9492 " : public aaaaaaaaaaaaaaaaaa\n" 9493 " , public bbbbbbbbbbbbbbbbbb {\n" 9494 "}", 9495 InheritanceStyle); 9496 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 9497 verifyFormat("class Foooooooooooooooooooooo\n" 9498 " : public aaaaaaaaaaaaaaaaaa,\n" 9499 " public bbbbbbbbbbbbbbbbbb {\n" 9500 "}", 9501 InheritanceStyle); 9502 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 9503 verifyFormat("class Foooooooooooooooooooooo\n" 9504 ": public aaaaaaaaaaaaaaaaaa,\n" 9505 " public bbbbbbbbbbbbbbbbbb {}", 9506 InheritanceStyle); 9507 9508 FormatStyle ForLoopStyle = getLLVMStyle(); 9509 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 9510 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 9511 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 9512 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 9513 verifyFormat("int x = a ? b : c;", ForLoopStyle); 9514 verifyFormat("{\n" 9515 "label2:\n" 9516 " int x = 0;\n" 9517 "}", 9518 ForLoopStyle); 9519 verifyFormat("switch (x) {\n" 9520 "case 1:\n" 9521 "default:\n" 9522 "}", 9523 ForLoopStyle); 9524 9525 FormatStyle NoSpaceStyle = getLLVMStyle(); 9526 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 9527 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 9528 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 9529 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 9530 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 9531 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 9532 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 9533 verifyFormat("{\n" 9534 "label3:\n" 9535 " int x = 0;\n" 9536 "}", 9537 NoSpaceStyle); 9538 verifyFormat("switch (x) {\n" 9539 "case 1:\n" 9540 "default:\n" 9541 "}", 9542 NoSpaceStyle); 9543 } 9544 9545 TEST_F(FormatTest, AlignConsecutiveAssignments) { 9546 FormatStyle Alignment = getLLVMStyle(); 9547 Alignment.AlignConsecutiveAssignments = false; 9548 verifyFormat("int a = 5;\n" 9549 "int oneTwoThree = 123;", 9550 Alignment); 9551 verifyFormat("int a = 5;\n" 9552 "int oneTwoThree = 123;", 9553 Alignment); 9554 9555 Alignment.AlignConsecutiveAssignments = true; 9556 verifyFormat("int a = 5;\n" 9557 "int oneTwoThree = 123;", 9558 Alignment); 9559 verifyFormat("int a = method();\n" 9560 "int oneTwoThree = 133;", 9561 Alignment); 9562 verifyFormat("a &= 5;\n" 9563 "bcd *= 5;\n" 9564 "ghtyf += 5;\n" 9565 "dvfvdb -= 5;\n" 9566 "a /= 5;\n" 9567 "vdsvsv %= 5;\n" 9568 "sfdbddfbdfbb ^= 5;\n" 9569 "dvsdsv |= 5;\n" 9570 "int dsvvdvsdvvv = 123;", 9571 Alignment); 9572 verifyFormat("int i = 1, j = 10;\n" 9573 "something = 2000;", 9574 Alignment); 9575 verifyFormat("something = 2000;\n" 9576 "int i = 1, j = 10;\n", 9577 Alignment); 9578 verifyFormat("something = 2000;\n" 9579 "another = 911;\n" 9580 "int i = 1, j = 10;\n" 9581 "oneMore = 1;\n" 9582 "i = 2;", 9583 Alignment); 9584 verifyFormat("int a = 5;\n" 9585 "int one = 1;\n" 9586 "method();\n" 9587 "int oneTwoThree = 123;\n" 9588 "int oneTwo = 12;", 9589 Alignment); 9590 verifyFormat("int oneTwoThree = 123;\n" 9591 "int oneTwo = 12;\n" 9592 "method();\n", 9593 Alignment); 9594 verifyFormat("int oneTwoThree = 123; // comment\n" 9595 "int oneTwo = 12; // comment", 9596 Alignment); 9597 EXPECT_EQ("int a = 5;\n" 9598 "\n" 9599 "int oneTwoThree = 123;", 9600 format("int a = 5;\n" 9601 "\n" 9602 "int oneTwoThree= 123;", 9603 Alignment)); 9604 EXPECT_EQ("int a = 5;\n" 9605 "int one = 1;\n" 9606 "\n" 9607 "int oneTwoThree = 123;", 9608 format("int a = 5;\n" 9609 "int one = 1;\n" 9610 "\n" 9611 "int oneTwoThree = 123;", 9612 Alignment)); 9613 EXPECT_EQ("int a = 5;\n" 9614 "int one = 1;\n" 9615 "\n" 9616 "int oneTwoThree = 123;\n" 9617 "int oneTwo = 12;", 9618 format("int a = 5;\n" 9619 "int one = 1;\n" 9620 "\n" 9621 "int oneTwoThree = 123;\n" 9622 "int oneTwo = 12;", 9623 Alignment)); 9624 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9625 verifyFormat("#define A \\\n" 9626 " int aaaa = 12; \\\n" 9627 " int b = 23; \\\n" 9628 " int ccc = 234; \\\n" 9629 " int dddddddddd = 2345;", 9630 Alignment); 9631 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9632 verifyFormat("#define A \\\n" 9633 " int aaaa = 12; \\\n" 9634 " int b = 23; \\\n" 9635 " int ccc = 234; \\\n" 9636 " int dddddddddd = 2345;", 9637 Alignment); 9638 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9639 verifyFormat("#define A " 9640 " \\\n" 9641 " int aaaa = 12; " 9642 " \\\n" 9643 " int b = 23; " 9644 " \\\n" 9645 " int ccc = 234; " 9646 " \\\n" 9647 " int dddddddddd = 2345;", 9648 Alignment); 9649 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9650 "k = 4, int l = 5,\n" 9651 " int m = 6) {\n" 9652 " int j = 10;\n" 9653 " otherThing = 1;\n" 9654 "}", 9655 Alignment); 9656 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9657 " int i = 1;\n" 9658 " int j = 2;\n" 9659 " int big = 10000;\n" 9660 "}", 9661 Alignment); 9662 verifyFormat("class C {\n" 9663 "public:\n" 9664 " int i = 1;\n" 9665 " virtual void f() = 0;\n" 9666 "};", 9667 Alignment); 9668 verifyFormat("int i = 1;\n" 9669 "if (SomeType t = getSomething()) {\n" 9670 "}\n" 9671 "int j = 2;\n" 9672 "int big = 10000;", 9673 Alignment); 9674 verifyFormat("int j = 7;\n" 9675 "for (int k = 0; k < N; ++k) {\n" 9676 "}\n" 9677 "int j = 2;\n" 9678 "int big = 10000;\n" 9679 "}", 9680 Alignment); 9681 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9682 verifyFormat("int i = 1;\n" 9683 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9684 " = someLooooooooooooooooongFunction();\n" 9685 "int j = 2;", 9686 Alignment); 9687 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9688 verifyFormat("int i = 1;\n" 9689 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9690 " someLooooooooooooooooongFunction();\n" 9691 "int j = 2;", 9692 Alignment); 9693 9694 verifyFormat("auto lambda = []() {\n" 9695 " auto i = 0;\n" 9696 " return 0;\n" 9697 "};\n" 9698 "int i = 0;\n" 9699 "auto v = type{\n" 9700 " i = 1, //\n" 9701 " (i = 2), //\n" 9702 " i = 3 //\n" 9703 "};", 9704 Alignment); 9705 9706 verifyFormat( 9707 "int i = 1;\n" 9708 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9709 " loooooooooooooooooooooongParameterB);\n" 9710 "int j = 2;", 9711 Alignment); 9712 9713 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 9714 " typename B = very_long_type_name_1,\n" 9715 " typename T_2 = very_long_type_name_2>\n" 9716 "auto foo() {}\n", 9717 Alignment); 9718 verifyFormat("int a, b = 1;\n" 9719 "int c = 2;\n" 9720 "int dd = 3;\n", 9721 Alignment); 9722 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9723 "float b[1][] = {{3.f}};\n", 9724 Alignment); 9725 verifyFormat("for (int i = 0; i < 1; i++)\n" 9726 " int x = 1;\n", 9727 Alignment); 9728 verifyFormat("for (i = 0; i < 1; i++)\n" 9729 " x = 1;\n" 9730 "y = 1;\n", 9731 Alignment); 9732 } 9733 9734 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 9735 FormatStyle Alignment = getLLVMStyle(); 9736 Alignment.AlignConsecutiveDeclarations = false; 9737 verifyFormat("float const a = 5;\n" 9738 "int oneTwoThree = 123;", 9739 Alignment); 9740 verifyFormat("int a = 5;\n" 9741 "float const oneTwoThree = 123;", 9742 Alignment); 9743 9744 Alignment.AlignConsecutiveDeclarations = true; 9745 verifyFormat("float const a = 5;\n" 9746 "int oneTwoThree = 123;", 9747 Alignment); 9748 verifyFormat("int a = method();\n" 9749 "float const oneTwoThree = 133;", 9750 Alignment); 9751 verifyFormat("int i = 1, j = 10;\n" 9752 "something = 2000;", 9753 Alignment); 9754 verifyFormat("something = 2000;\n" 9755 "int i = 1, j = 10;\n", 9756 Alignment); 9757 verifyFormat("float something = 2000;\n" 9758 "double another = 911;\n" 9759 "int i = 1, j = 10;\n" 9760 "const int *oneMore = 1;\n" 9761 "unsigned i = 2;", 9762 Alignment); 9763 verifyFormat("float a = 5;\n" 9764 "int one = 1;\n" 9765 "method();\n" 9766 "const double oneTwoThree = 123;\n" 9767 "const unsigned int oneTwo = 12;", 9768 Alignment); 9769 verifyFormat("int oneTwoThree{0}; // comment\n" 9770 "unsigned oneTwo; // comment", 9771 Alignment); 9772 EXPECT_EQ("float const a = 5;\n" 9773 "\n" 9774 "int oneTwoThree = 123;", 9775 format("float const a = 5;\n" 9776 "\n" 9777 "int oneTwoThree= 123;", 9778 Alignment)); 9779 EXPECT_EQ("float a = 5;\n" 9780 "int one = 1;\n" 9781 "\n" 9782 "unsigned oneTwoThree = 123;", 9783 format("float a = 5;\n" 9784 "int one = 1;\n" 9785 "\n" 9786 "unsigned oneTwoThree = 123;", 9787 Alignment)); 9788 EXPECT_EQ("float a = 5;\n" 9789 "int one = 1;\n" 9790 "\n" 9791 "unsigned oneTwoThree = 123;\n" 9792 "int oneTwo = 12;", 9793 format("float a = 5;\n" 9794 "int one = 1;\n" 9795 "\n" 9796 "unsigned oneTwoThree = 123;\n" 9797 "int oneTwo = 12;", 9798 Alignment)); 9799 // Function prototype alignment 9800 verifyFormat("int a();\n" 9801 "double b();", 9802 Alignment); 9803 verifyFormat("int a(int x);\n" 9804 "double b();", 9805 Alignment); 9806 unsigned OldColumnLimit = Alignment.ColumnLimit; 9807 // We need to set ColumnLimit to zero, in order to stress nested alignments, 9808 // otherwise the function parameters will be re-flowed onto a single line. 9809 Alignment.ColumnLimit = 0; 9810 EXPECT_EQ("int a(int x,\n" 9811 " float y);\n" 9812 "double b(int x,\n" 9813 " double y);", 9814 format("int a(int x,\n" 9815 " float y);\n" 9816 "double b(int x,\n" 9817 " double y);", 9818 Alignment)); 9819 // This ensures that function parameters of function declarations are 9820 // correctly indented when their owning functions are indented. 9821 // The failure case here is for 'double y' to not be indented enough. 9822 EXPECT_EQ("double a(int x);\n" 9823 "int b(int y,\n" 9824 " double z);", 9825 format("double a(int x);\n" 9826 "int b(int y,\n" 9827 " double z);", 9828 Alignment)); 9829 // Set ColumnLimit low so that we induce wrapping immediately after 9830 // the function name and opening paren. 9831 Alignment.ColumnLimit = 13; 9832 verifyFormat("int function(\n" 9833 " int x,\n" 9834 " bool y);", 9835 Alignment); 9836 Alignment.ColumnLimit = OldColumnLimit; 9837 // Ensure function pointers don't screw up recursive alignment 9838 verifyFormat("int a(int x, void (*fp)(int y));\n" 9839 "double b();", 9840 Alignment); 9841 Alignment.AlignConsecutiveAssignments = true; 9842 // Ensure recursive alignment is broken by function braces, so that the 9843 // "a = 1" does not align with subsequent assignments inside the function 9844 // body. 9845 verifyFormat("int func(int a = 1) {\n" 9846 " int b = 2;\n" 9847 " int cc = 3;\n" 9848 "}", 9849 Alignment); 9850 verifyFormat("float something = 2000;\n" 9851 "double another = 911;\n" 9852 "int i = 1, j = 10;\n" 9853 "const int *oneMore = 1;\n" 9854 "unsigned i = 2;", 9855 Alignment); 9856 verifyFormat("int oneTwoThree = {0}; // comment\n" 9857 "unsigned oneTwo = 0; // comment", 9858 Alignment); 9859 // Make sure that scope is correctly tracked, in the absence of braces 9860 verifyFormat("for (int i = 0; i < n; i++)\n" 9861 " j = i;\n" 9862 "double x = 1;\n", 9863 Alignment); 9864 verifyFormat("if (int i = 0)\n" 9865 " j = i;\n" 9866 "double x = 1;\n", 9867 Alignment); 9868 // Ensure operator[] and operator() are comprehended 9869 verifyFormat("struct test {\n" 9870 " long long int foo();\n" 9871 " int operator[](int a);\n" 9872 " double bar();\n" 9873 "};\n", 9874 Alignment); 9875 verifyFormat("struct test {\n" 9876 " long long int foo();\n" 9877 " int operator()(int a);\n" 9878 " double bar();\n" 9879 "};\n", 9880 Alignment); 9881 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9882 " int const i = 1;\n" 9883 " int * j = 2;\n" 9884 " int big = 10000;\n" 9885 "\n" 9886 " unsigned oneTwoThree = 123;\n" 9887 " int oneTwo = 12;\n" 9888 " method();\n" 9889 " float k = 2;\n" 9890 " int ll = 10000;\n" 9891 "}", 9892 format("void SomeFunction(int parameter= 0) {\n" 9893 " int const i= 1;\n" 9894 " int *j=2;\n" 9895 " int big = 10000;\n" 9896 "\n" 9897 "unsigned oneTwoThree =123;\n" 9898 "int oneTwo = 12;\n" 9899 " method();\n" 9900 "float k= 2;\n" 9901 "int ll=10000;\n" 9902 "}", 9903 Alignment)); 9904 Alignment.AlignConsecutiveAssignments = false; 9905 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9906 verifyFormat("#define A \\\n" 9907 " int aaaa = 12; \\\n" 9908 " float b = 23; \\\n" 9909 " const int ccc = 234; \\\n" 9910 " unsigned dddddddddd = 2345;", 9911 Alignment); 9912 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9913 verifyFormat("#define A \\\n" 9914 " int aaaa = 12; \\\n" 9915 " float b = 23; \\\n" 9916 " const int ccc = 234; \\\n" 9917 " unsigned dddddddddd = 2345;", 9918 Alignment); 9919 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9920 Alignment.ColumnLimit = 30; 9921 verifyFormat("#define A \\\n" 9922 " int aaaa = 12; \\\n" 9923 " float b = 23; \\\n" 9924 " const int ccc = 234; \\\n" 9925 " int dddddddddd = 2345;", 9926 Alignment); 9927 Alignment.ColumnLimit = 80; 9928 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9929 "k = 4, int l = 5,\n" 9930 " int m = 6) {\n" 9931 " const int j = 10;\n" 9932 " otherThing = 1;\n" 9933 "}", 9934 Alignment); 9935 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9936 " int const i = 1;\n" 9937 " int * j = 2;\n" 9938 " int big = 10000;\n" 9939 "}", 9940 Alignment); 9941 verifyFormat("class C {\n" 9942 "public:\n" 9943 " int i = 1;\n" 9944 " virtual void f() = 0;\n" 9945 "};", 9946 Alignment); 9947 verifyFormat("float i = 1;\n" 9948 "if (SomeType t = getSomething()) {\n" 9949 "}\n" 9950 "const unsigned j = 2;\n" 9951 "int big = 10000;", 9952 Alignment); 9953 verifyFormat("float j = 7;\n" 9954 "for (int k = 0; k < N; ++k) {\n" 9955 "}\n" 9956 "unsigned j = 2;\n" 9957 "int big = 10000;\n" 9958 "}", 9959 Alignment); 9960 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9961 verifyFormat("float i = 1;\n" 9962 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9963 " = someLooooooooooooooooongFunction();\n" 9964 "int j = 2;", 9965 Alignment); 9966 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9967 verifyFormat("int i = 1;\n" 9968 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9969 " someLooooooooooooooooongFunction();\n" 9970 "int j = 2;", 9971 Alignment); 9972 9973 Alignment.AlignConsecutiveAssignments = true; 9974 verifyFormat("auto lambda = []() {\n" 9975 " auto ii = 0;\n" 9976 " float j = 0;\n" 9977 " return 0;\n" 9978 "};\n" 9979 "int i = 0;\n" 9980 "float i2 = 0;\n" 9981 "auto v = type{\n" 9982 " i = 1, //\n" 9983 " (i = 2), //\n" 9984 " i = 3 //\n" 9985 "};", 9986 Alignment); 9987 Alignment.AlignConsecutiveAssignments = false; 9988 9989 verifyFormat( 9990 "int i = 1;\n" 9991 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9992 " loooooooooooooooooooooongParameterB);\n" 9993 "int j = 2;", 9994 Alignment); 9995 9996 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9997 // We expect declarations and assignments to align, as long as it doesn't 9998 // exceed the column limit, starting a new alignment sequence whenever it 9999 // happens. 10000 Alignment.AlignConsecutiveAssignments = true; 10001 Alignment.ColumnLimit = 30; 10002 verifyFormat("float ii = 1;\n" 10003 "unsigned j = 2;\n" 10004 "int someVerylongVariable = 1;\n" 10005 "AnotherLongType ll = 123456;\n" 10006 "VeryVeryLongType k = 2;\n" 10007 "int myvar = 1;", 10008 Alignment); 10009 Alignment.ColumnLimit = 80; 10010 Alignment.AlignConsecutiveAssignments = false; 10011 10012 verifyFormat( 10013 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 10014 " typename LongType, typename B>\n" 10015 "auto foo() {}\n", 10016 Alignment); 10017 verifyFormat("float a, b = 1;\n" 10018 "int c = 2;\n" 10019 "int dd = 3;\n", 10020 Alignment); 10021 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 10022 "float b[1][] = {{3.f}};\n", 10023 Alignment); 10024 Alignment.AlignConsecutiveAssignments = true; 10025 verifyFormat("float a, b = 1;\n" 10026 "int c = 2;\n" 10027 "int dd = 3;\n", 10028 Alignment); 10029 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 10030 "float b[1][] = {{3.f}};\n", 10031 Alignment); 10032 Alignment.AlignConsecutiveAssignments = false; 10033 10034 Alignment.ColumnLimit = 30; 10035 Alignment.BinPackParameters = false; 10036 verifyFormat("void foo(float a,\n" 10037 " float b,\n" 10038 " int c,\n" 10039 " uint32_t *d) {\n" 10040 " int * e = 0;\n" 10041 " float f = 0;\n" 10042 " double g = 0;\n" 10043 "}\n" 10044 "void bar(ino_t a,\n" 10045 " int b,\n" 10046 " uint32_t *c,\n" 10047 " bool d) {}\n", 10048 Alignment); 10049 Alignment.BinPackParameters = true; 10050 Alignment.ColumnLimit = 80; 10051 10052 // Bug 33507 10053 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 10054 verifyFormat( 10055 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 10056 " static const Version verVs2017;\n" 10057 " return true;\n" 10058 "});\n", 10059 Alignment); 10060 Alignment.PointerAlignment = FormatStyle::PAS_Right; 10061 10062 // See llvm.org/PR35641 10063 Alignment.AlignConsecutiveDeclarations = true; 10064 verifyFormat("int func() { //\n" 10065 " int b;\n" 10066 " unsigned c;\n" 10067 "}", 10068 Alignment); 10069 } 10070 10071 TEST_F(FormatTest, LinuxBraceBreaking) { 10072 FormatStyle LinuxBraceStyle = getLLVMStyle(); 10073 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 10074 verifyFormat("namespace a\n" 10075 "{\n" 10076 "class A\n" 10077 "{\n" 10078 " void f()\n" 10079 " {\n" 10080 " if (true) {\n" 10081 " a();\n" 10082 " b();\n" 10083 " } else {\n" 10084 " a();\n" 10085 " }\n" 10086 " }\n" 10087 " void g() { return; }\n" 10088 "};\n" 10089 "struct B {\n" 10090 " int x;\n" 10091 "};\n" 10092 "} // namespace a\n", 10093 LinuxBraceStyle); 10094 verifyFormat("enum X {\n" 10095 " Y = 0,\n" 10096 "}\n", 10097 LinuxBraceStyle); 10098 verifyFormat("struct S {\n" 10099 " int Type;\n" 10100 " union {\n" 10101 " int x;\n" 10102 " double y;\n" 10103 " } Value;\n" 10104 " class C\n" 10105 " {\n" 10106 " MyFavoriteType Value;\n" 10107 " } Class;\n" 10108 "}\n", 10109 LinuxBraceStyle); 10110 } 10111 10112 TEST_F(FormatTest, MozillaBraceBreaking) { 10113 FormatStyle MozillaBraceStyle = getLLVMStyle(); 10114 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 10115 MozillaBraceStyle.FixNamespaceComments = false; 10116 verifyFormat("namespace a {\n" 10117 "class A\n" 10118 "{\n" 10119 " void f()\n" 10120 " {\n" 10121 " if (true) {\n" 10122 " a();\n" 10123 " b();\n" 10124 " }\n" 10125 " }\n" 10126 " void g() { return; }\n" 10127 "};\n" 10128 "enum E\n" 10129 "{\n" 10130 " A,\n" 10131 " // foo\n" 10132 " B,\n" 10133 " C\n" 10134 "};\n" 10135 "struct B\n" 10136 "{\n" 10137 " int x;\n" 10138 "};\n" 10139 "}\n", 10140 MozillaBraceStyle); 10141 verifyFormat("struct S\n" 10142 "{\n" 10143 " int Type;\n" 10144 " union\n" 10145 " {\n" 10146 " int x;\n" 10147 " double y;\n" 10148 " } Value;\n" 10149 " class C\n" 10150 " {\n" 10151 " MyFavoriteType Value;\n" 10152 " } Class;\n" 10153 "}\n", 10154 MozillaBraceStyle); 10155 } 10156 10157 TEST_F(FormatTest, StroustrupBraceBreaking) { 10158 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 10159 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10160 verifyFormat("namespace a {\n" 10161 "class A {\n" 10162 " void f()\n" 10163 " {\n" 10164 " if (true) {\n" 10165 " a();\n" 10166 " b();\n" 10167 " }\n" 10168 " }\n" 10169 " void g() { return; }\n" 10170 "};\n" 10171 "struct B {\n" 10172 " int x;\n" 10173 "};\n" 10174 "} // namespace a\n", 10175 StroustrupBraceStyle); 10176 10177 verifyFormat("void foo()\n" 10178 "{\n" 10179 " if (a) {\n" 10180 " a();\n" 10181 " }\n" 10182 " else {\n" 10183 " b();\n" 10184 " }\n" 10185 "}\n", 10186 StroustrupBraceStyle); 10187 10188 verifyFormat("#ifdef _DEBUG\n" 10189 "int foo(int i = 0)\n" 10190 "#else\n" 10191 "int foo(int i = 5)\n" 10192 "#endif\n" 10193 "{\n" 10194 " return i;\n" 10195 "}", 10196 StroustrupBraceStyle); 10197 10198 verifyFormat("void foo() {}\n" 10199 "void bar()\n" 10200 "#ifdef _DEBUG\n" 10201 "{\n" 10202 " foo();\n" 10203 "}\n" 10204 "#else\n" 10205 "{\n" 10206 "}\n" 10207 "#endif", 10208 StroustrupBraceStyle); 10209 10210 verifyFormat("void foobar() { int i = 5; }\n" 10211 "#ifdef _DEBUG\n" 10212 "void bar() {}\n" 10213 "#else\n" 10214 "void bar() { foobar(); }\n" 10215 "#endif", 10216 StroustrupBraceStyle); 10217 } 10218 10219 TEST_F(FormatTest, AllmanBraceBreaking) { 10220 FormatStyle AllmanBraceStyle = getLLVMStyle(); 10221 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 10222 10223 EXPECT_EQ("namespace a\n" 10224 "{\n" 10225 "void f();\n" 10226 "void g();\n" 10227 "} // namespace a\n", 10228 format("namespace a\n" 10229 "{\n" 10230 "void f();\n" 10231 "void g();\n" 10232 "}\n", 10233 AllmanBraceStyle)); 10234 10235 verifyFormat("namespace a\n" 10236 "{\n" 10237 "class A\n" 10238 "{\n" 10239 " void f()\n" 10240 " {\n" 10241 " if (true)\n" 10242 " {\n" 10243 " a();\n" 10244 " b();\n" 10245 " }\n" 10246 " }\n" 10247 " void g() { return; }\n" 10248 "};\n" 10249 "struct B\n" 10250 "{\n" 10251 " int x;\n" 10252 "};\n" 10253 "} // namespace a", 10254 AllmanBraceStyle); 10255 10256 verifyFormat("void f()\n" 10257 "{\n" 10258 " if (true)\n" 10259 " {\n" 10260 " a();\n" 10261 " }\n" 10262 " else if (false)\n" 10263 " {\n" 10264 " b();\n" 10265 " }\n" 10266 " else\n" 10267 " {\n" 10268 " c();\n" 10269 " }\n" 10270 "}\n", 10271 AllmanBraceStyle); 10272 10273 verifyFormat("void f()\n" 10274 "{\n" 10275 " for (int i = 0; i < 10; ++i)\n" 10276 " {\n" 10277 " a();\n" 10278 " }\n" 10279 " while (false)\n" 10280 " {\n" 10281 " b();\n" 10282 " }\n" 10283 " do\n" 10284 " {\n" 10285 " c();\n" 10286 " } while (false)\n" 10287 "}\n", 10288 AllmanBraceStyle); 10289 10290 verifyFormat("void f(int a)\n" 10291 "{\n" 10292 " switch (a)\n" 10293 " {\n" 10294 " case 0:\n" 10295 " break;\n" 10296 " case 1:\n" 10297 " {\n" 10298 " break;\n" 10299 " }\n" 10300 " case 2:\n" 10301 " {\n" 10302 " }\n" 10303 " break;\n" 10304 " default:\n" 10305 " break;\n" 10306 " }\n" 10307 "}\n", 10308 AllmanBraceStyle); 10309 10310 verifyFormat("enum X\n" 10311 "{\n" 10312 " Y = 0,\n" 10313 "}\n", 10314 AllmanBraceStyle); 10315 verifyFormat("enum X\n" 10316 "{\n" 10317 " Y = 0\n" 10318 "}\n", 10319 AllmanBraceStyle); 10320 10321 verifyFormat("@interface BSApplicationController ()\n" 10322 "{\n" 10323 "@private\n" 10324 " id _extraIvar;\n" 10325 "}\n" 10326 "@end\n", 10327 AllmanBraceStyle); 10328 10329 verifyFormat("#ifdef _DEBUG\n" 10330 "int foo(int i = 0)\n" 10331 "#else\n" 10332 "int foo(int i = 5)\n" 10333 "#endif\n" 10334 "{\n" 10335 " return i;\n" 10336 "}", 10337 AllmanBraceStyle); 10338 10339 verifyFormat("void foo() {}\n" 10340 "void bar()\n" 10341 "#ifdef _DEBUG\n" 10342 "{\n" 10343 " foo();\n" 10344 "}\n" 10345 "#else\n" 10346 "{\n" 10347 "}\n" 10348 "#endif", 10349 AllmanBraceStyle); 10350 10351 verifyFormat("void foobar() { int i = 5; }\n" 10352 "#ifdef _DEBUG\n" 10353 "void bar() {}\n" 10354 "#else\n" 10355 "void bar() { foobar(); }\n" 10356 "#endif", 10357 AllmanBraceStyle); 10358 10359 // This shouldn't affect ObjC blocks.. 10360 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 10361 " // ...\n" 10362 " int i;\n" 10363 "}];", 10364 AllmanBraceStyle); 10365 verifyFormat("void (^block)(void) = ^{\n" 10366 " // ...\n" 10367 " int i;\n" 10368 "};", 10369 AllmanBraceStyle); 10370 // .. or dict literals. 10371 verifyFormat("void f()\n" 10372 "{\n" 10373 " // ...\n" 10374 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 10375 "}", 10376 AllmanBraceStyle); 10377 verifyFormat("void f()\n" 10378 "{\n" 10379 " // ...\n" 10380 " [object someMethod:@{a : @\"b\"}];\n" 10381 "}", 10382 AllmanBraceStyle); 10383 verifyFormat("int f()\n" 10384 "{ // comment\n" 10385 " return 42;\n" 10386 "}", 10387 AllmanBraceStyle); 10388 10389 AllmanBraceStyle.ColumnLimit = 19; 10390 verifyFormat("void f() { int i; }", AllmanBraceStyle); 10391 AllmanBraceStyle.ColumnLimit = 18; 10392 verifyFormat("void f()\n" 10393 "{\n" 10394 " int i;\n" 10395 "}", 10396 AllmanBraceStyle); 10397 AllmanBraceStyle.ColumnLimit = 80; 10398 10399 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 10400 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 10401 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 10402 verifyFormat("void f(bool b)\n" 10403 "{\n" 10404 " if (b)\n" 10405 " {\n" 10406 " return;\n" 10407 " }\n" 10408 "}\n", 10409 BreakBeforeBraceShortIfs); 10410 verifyFormat("void f(bool b)\n" 10411 "{\n" 10412 " if constexpr (b)\n" 10413 " {\n" 10414 " return;\n" 10415 " }\n" 10416 "}\n", 10417 BreakBeforeBraceShortIfs); 10418 verifyFormat("void f(bool b)\n" 10419 "{\n" 10420 " if (b) return;\n" 10421 "}\n", 10422 BreakBeforeBraceShortIfs); 10423 verifyFormat("void f(bool b)\n" 10424 "{\n" 10425 " if constexpr (b) return;\n" 10426 "}\n", 10427 BreakBeforeBraceShortIfs); 10428 verifyFormat("void f(bool b)\n" 10429 "{\n" 10430 " while (b)\n" 10431 " {\n" 10432 " return;\n" 10433 " }\n" 10434 "}\n", 10435 BreakBeforeBraceShortIfs); 10436 } 10437 10438 TEST_F(FormatTest, GNUBraceBreaking) { 10439 FormatStyle GNUBraceStyle = getLLVMStyle(); 10440 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 10441 verifyFormat("namespace a\n" 10442 "{\n" 10443 "class A\n" 10444 "{\n" 10445 " void f()\n" 10446 " {\n" 10447 " int a;\n" 10448 " {\n" 10449 " int b;\n" 10450 " }\n" 10451 " if (true)\n" 10452 " {\n" 10453 " a();\n" 10454 " b();\n" 10455 " }\n" 10456 " }\n" 10457 " void g() { return; }\n" 10458 "}\n" 10459 "} // namespace a", 10460 GNUBraceStyle); 10461 10462 verifyFormat("void f()\n" 10463 "{\n" 10464 " if (true)\n" 10465 " {\n" 10466 " a();\n" 10467 " }\n" 10468 " else if (false)\n" 10469 " {\n" 10470 " b();\n" 10471 " }\n" 10472 " else\n" 10473 " {\n" 10474 " c();\n" 10475 " }\n" 10476 "}\n", 10477 GNUBraceStyle); 10478 10479 verifyFormat("void f()\n" 10480 "{\n" 10481 " for (int i = 0; i < 10; ++i)\n" 10482 " {\n" 10483 " a();\n" 10484 " }\n" 10485 " while (false)\n" 10486 " {\n" 10487 " b();\n" 10488 " }\n" 10489 " do\n" 10490 " {\n" 10491 " c();\n" 10492 " }\n" 10493 " while (false);\n" 10494 "}\n", 10495 GNUBraceStyle); 10496 10497 verifyFormat("void f(int a)\n" 10498 "{\n" 10499 " switch (a)\n" 10500 " {\n" 10501 " case 0:\n" 10502 " break;\n" 10503 " case 1:\n" 10504 " {\n" 10505 " break;\n" 10506 " }\n" 10507 " case 2:\n" 10508 " {\n" 10509 " }\n" 10510 " break;\n" 10511 " default:\n" 10512 " break;\n" 10513 " }\n" 10514 "}\n", 10515 GNUBraceStyle); 10516 10517 verifyFormat("enum X\n" 10518 "{\n" 10519 " Y = 0,\n" 10520 "}\n", 10521 GNUBraceStyle); 10522 10523 verifyFormat("@interface BSApplicationController ()\n" 10524 "{\n" 10525 "@private\n" 10526 " id _extraIvar;\n" 10527 "}\n" 10528 "@end\n", 10529 GNUBraceStyle); 10530 10531 verifyFormat("#ifdef _DEBUG\n" 10532 "int foo(int i = 0)\n" 10533 "#else\n" 10534 "int foo(int i = 5)\n" 10535 "#endif\n" 10536 "{\n" 10537 " return i;\n" 10538 "}", 10539 GNUBraceStyle); 10540 10541 verifyFormat("void foo() {}\n" 10542 "void bar()\n" 10543 "#ifdef _DEBUG\n" 10544 "{\n" 10545 " foo();\n" 10546 "}\n" 10547 "#else\n" 10548 "{\n" 10549 "}\n" 10550 "#endif", 10551 GNUBraceStyle); 10552 10553 verifyFormat("void foobar() { int i = 5; }\n" 10554 "#ifdef _DEBUG\n" 10555 "void bar() {}\n" 10556 "#else\n" 10557 "void bar() { foobar(); }\n" 10558 "#endif", 10559 GNUBraceStyle); 10560 } 10561 10562 TEST_F(FormatTest, WebKitBraceBreaking) { 10563 FormatStyle WebKitBraceStyle = getLLVMStyle(); 10564 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 10565 WebKitBraceStyle.FixNamespaceComments = false; 10566 verifyFormat("namespace a {\n" 10567 "class A {\n" 10568 " void f()\n" 10569 " {\n" 10570 " if (true) {\n" 10571 " a();\n" 10572 " b();\n" 10573 " }\n" 10574 " }\n" 10575 " void g() { return; }\n" 10576 "};\n" 10577 "enum E {\n" 10578 " A,\n" 10579 " // foo\n" 10580 " B,\n" 10581 " C\n" 10582 "};\n" 10583 "struct B {\n" 10584 " int x;\n" 10585 "};\n" 10586 "}\n", 10587 WebKitBraceStyle); 10588 verifyFormat("struct S {\n" 10589 " int Type;\n" 10590 " union {\n" 10591 " int x;\n" 10592 " double y;\n" 10593 " } Value;\n" 10594 " class C {\n" 10595 " MyFavoriteType Value;\n" 10596 " } Class;\n" 10597 "};\n", 10598 WebKitBraceStyle); 10599 } 10600 10601 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 10602 verifyFormat("void f() {\n" 10603 " try {\n" 10604 " } catch (const Exception &e) {\n" 10605 " }\n" 10606 "}\n", 10607 getLLVMStyle()); 10608 } 10609 10610 TEST_F(FormatTest, UnderstandsPragmas) { 10611 verifyFormat("#pragma omp reduction(| : var)"); 10612 verifyFormat("#pragma omp reduction(+ : var)"); 10613 10614 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 10615 "(including parentheses).", 10616 format("#pragma mark Any non-hyphenated or hyphenated string " 10617 "(including parentheses).")); 10618 } 10619 10620 TEST_F(FormatTest, UnderstandPragmaOption) { 10621 verifyFormat("#pragma option -C -A"); 10622 10623 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 10624 } 10625 10626 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 10627 FormatStyle Style = getLLVMStyle(); 10628 Style.ColumnLimit = 20; 10629 10630 verifyFormat("int a; // the\n" 10631 " // comment", Style); 10632 EXPECT_EQ("int a; /* first line\n" 10633 " * second\n" 10634 " * line third\n" 10635 " * line\n" 10636 " */", 10637 format("int a; /* first line\n" 10638 " * second\n" 10639 " * line third\n" 10640 " * line\n" 10641 " */", 10642 Style)); 10643 EXPECT_EQ("int a; // first line\n" 10644 " // second\n" 10645 " // line third\n" 10646 " // line", 10647 format("int a; // first line\n" 10648 " // second line\n" 10649 " // third line", 10650 Style)); 10651 10652 Style.PenaltyExcessCharacter = 90; 10653 verifyFormat("int a; // the comment", Style); 10654 EXPECT_EQ("int a; // the comment\n" 10655 " // aaa", 10656 format("int a; // the comment aaa", Style)); 10657 EXPECT_EQ("int a; /* first line\n" 10658 " * second line\n" 10659 " * third line\n" 10660 " */", 10661 format("int a; /* first line\n" 10662 " * second line\n" 10663 " * third line\n" 10664 " */", 10665 Style)); 10666 EXPECT_EQ("int a; // first line\n" 10667 " // second line\n" 10668 " // third line", 10669 format("int a; // first line\n" 10670 " // second line\n" 10671 " // third line", 10672 Style)); 10673 // FIXME: Investigate why this is not getting the same layout as the test 10674 // above. 10675 EXPECT_EQ("int a; /* first line\n" 10676 " * second line\n" 10677 " * third line\n" 10678 " */", 10679 format("int a; /* first line second line third line" 10680 "\n*/", 10681 Style)); 10682 10683 EXPECT_EQ("// foo bar baz bazfoo\n" 10684 "// foo bar foo bar\n", 10685 format("// foo bar baz bazfoo\n" 10686 "// foo bar foo bar\n", 10687 Style)); 10688 EXPECT_EQ("// foo bar baz bazfoo\n" 10689 "// foo bar foo bar\n", 10690 format("// foo bar baz bazfoo\n" 10691 "// foo bar foo bar\n", 10692 Style)); 10693 10694 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 10695 // next one. 10696 EXPECT_EQ("// foo bar baz bazfoo\n" 10697 "// bar foo bar\n", 10698 format("// foo bar baz bazfoo bar\n" 10699 "// foo bar\n", 10700 Style)); 10701 10702 EXPECT_EQ("// foo bar baz bazfoo\n" 10703 "// foo bar baz bazfoo\n" 10704 "// bar foo bar\n", 10705 format("// foo bar baz bazfoo\n" 10706 "// foo bar baz bazfoo bar\n" 10707 "// foo bar\n", 10708 Style)); 10709 10710 EXPECT_EQ("// foo bar baz bazfoo\n" 10711 "// foo bar baz bazfoo\n" 10712 "// bar foo bar\n", 10713 format("// foo bar baz bazfoo\n" 10714 "// foo bar baz bazfoo bar\n" 10715 "// foo bar\n", 10716 Style)); 10717 10718 // Make sure we do not keep protruding characters if strict mode reflow is 10719 // cheaper than keeping protruding characters. 10720 Style.ColumnLimit = 21; 10721 EXPECT_EQ("// foo foo foo foo\n" 10722 "// foo foo foo foo\n" 10723 "// foo foo foo foo\n", 10724 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", 10725 Style)); 10726 10727 EXPECT_EQ("int a = /* long block\n" 10728 " comment */\n" 10729 " 42;", 10730 format("int a = /* long block comment */ 42;", Style)); 10731 } 10732 10733 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 10734 for (size_t i = 1; i < Styles.size(); ++i) \ 10735 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 10736 << " differs from Style #0" 10737 10738 TEST_F(FormatTest, GetsPredefinedStyleByName) { 10739 SmallVector<FormatStyle, 3> Styles; 10740 Styles.resize(3); 10741 10742 Styles[0] = getLLVMStyle(); 10743 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 10744 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 10745 EXPECT_ALL_STYLES_EQUAL(Styles); 10746 10747 Styles[0] = getGoogleStyle(); 10748 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 10749 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 10750 EXPECT_ALL_STYLES_EQUAL(Styles); 10751 10752 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10753 EXPECT_TRUE( 10754 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 10755 EXPECT_TRUE( 10756 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 10757 EXPECT_ALL_STYLES_EQUAL(Styles); 10758 10759 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 10760 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 10761 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 10762 EXPECT_ALL_STYLES_EQUAL(Styles); 10763 10764 Styles[0] = getMozillaStyle(); 10765 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 10766 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 10767 EXPECT_ALL_STYLES_EQUAL(Styles); 10768 10769 Styles[0] = getWebKitStyle(); 10770 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 10771 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 10772 EXPECT_ALL_STYLES_EQUAL(Styles); 10773 10774 Styles[0] = getGNUStyle(); 10775 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 10776 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 10777 EXPECT_ALL_STYLES_EQUAL(Styles); 10778 10779 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 10780 } 10781 10782 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 10783 SmallVector<FormatStyle, 8> Styles; 10784 Styles.resize(2); 10785 10786 Styles[0] = getGoogleStyle(); 10787 Styles[1] = getLLVMStyle(); 10788 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10789 EXPECT_ALL_STYLES_EQUAL(Styles); 10790 10791 Styles.resize(5); 10792 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10793 Styles[1] = getLLVMStyle(); 10794 Styles[1].Language = FormatStyle::LK_JavaScript; 10795 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10796 10797 Styles[2] = getLLVMStyle(); 10798 Styles[2].Language = FormatStyle::LK_JavaScript; 10799 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 10800 "BasedOnStyle: Google", 10801 &Styles[2]) 10802 .value()); 10803 10804 Styles[3] = getLLVMStyle(); 10805 Styles[3].Language = FormatStyle::LK_JavaScript; 10806 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 10807 "Language: JavaScript", 10808 &Styles[3]) 10809 .value()); 10810 10811 Styles[4] = getLLVMStyle(); 10812 Styles[4].Language = FormatStyle::LK_JavaScript; 10813 EXPECT_EQ(0, parseConfiguration("---\n" 10814 "BasedOnStyle: LLVM\n" 10815 "IndentWidth: 123\n" 10816 "---\n" 10817 "BasedOnStyle: Google\n" 10818 "Language: JavaScript", 10819 &Styles[4]) 10820 .value()); 10821 EXPECT_ALL_STYLES_EQUAL(Styles); 10822 } 10823 10824 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 10825 Style.FIELD = false; \ 10826 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 10827 EXPECT_TRUE(Style.FIELD); \ 10828 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 10829 EXPECT_FALSE(Style.FIELD); 10830 10831 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 10832 10833 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 10834 Style.STRUCT.FIELD = false; \ 10835 EXPECT_EQ(0, \ 10836 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 10837 .value()); \ 10838 EXPECT_TRUE(Style.STRUCT.FIELD); \ 10839 EXPECT_EQ(0, \ 10840 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 10841 .value()); \ 10842 EXPECT_FALSE(Style.STRUCT.FIELD); 10843 10844 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 10845 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 10846 10847 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 10848 EXPECT_NE(VALUE, Style.FIELD); \ 10849 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 10850 EXPECT_EQ(VALUE, Style.FIELD) 10851 10852 TEST_F(FormatTest, ParsesConfigurationBools) { 10853 FormatStyle Style = {}; 10854 Style.Language = FormatStyle::LK_Cpp; 10855 CHECK_PARSE_BOOL(AlignOperands); 10856 CHECK_PARSE_BOOL(AlignTrailingComments); 10857 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 10858 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 10859 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 10860 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 10861 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 10862 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 10863 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 10864 CHECK_PARSE_BOOL(BinPackArguments); 10865 CHECK_PARSE_BOOL(BinPackParameters); 10866 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 10867 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 10868 CHECK_PARSE_BOOL(BreakStringLiterals); 10869 CHECK_PARSE_BOOL(CompactNamespaces); 10870 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 10871 CHECK_PARSE_BOOL(DerivePointerAlignment); 10872 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 10873 CHECK_PARSE_BOOL(DisableFormat); 10874 CHECK_PARSE_BOOL(IndentCaseLabels); 10875 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 10876 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 10877 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 10878 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 10879 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 10880 CHECK_PARSE_BOOL(ReflowComments); 10881 CHECK_PARSE_BOOL(SortIncludes); 10882 CHECK_PARSE_BOOL(SortUsingDeclarations); 10883 CHECK_PARSE_BOOL(SpacesInParentheses); 10884 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 10885 CHECK_PARSE_BOOL(SpacesInAngles); 10886 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 10887 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 10888 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 10889 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 10890 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 10891 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 10892 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 10893 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 10894 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 10895 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 10896 10897 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 10898 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 10899 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 10900 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 10901 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 10902 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 10903 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 10904 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 10905 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 10906 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 10907 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 10908 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 10909 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 10910 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 10911 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 10912 } 10913 10914 #undef CHECK_PARSE_BOOL 10915 10916 TEST_F(FormatTest, ParsesConfiguration) { 10917 FormatStyle Style = {}; 10918 Style.Language = FormatStyle::LK_Cpp; 10919 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 10920 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 10921 ConstructorInitializerIndentWidth, 1234u); 10922 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 10923 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 10924 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 10925 CHECK_PARSE("PenaltyBreakAssignment: 1234", 10926 PenaltyBreakAssignment, 1234u); 10927 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 10928 PenaltyBreakBeforeFirstCallParameter, 1234u); 10929 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 10930 PenaltyBreakTemplateDeclaration, 1234u); 10931 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 10932 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 10933 PenaltyReturnTypeOnItsOwnLine, 1234u); 10934 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 10935 SpacesBeforeTrailingComments, 1234u); 10936 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 10937 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 10938 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 10939 10940 Style.PointerAlignment = FormatStyle::PAS_Middle; 10941 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 10942 FormatStyle::PAS_Left); 10943 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 10944 FormatStyle::PAS_Right); 10945 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 10946 FormatStyle::PAS_Middle); 10947 // For backward compatibility: 10948 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 10949 FormatStyle::PAS_Left); 10950 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 10951 FormatStyle::PAS_Right); 10952 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 10953 FormatStyle::PAS_Middle); 10954 10955 Style.Standard = FormatStyle::LS_Auto; 10956 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 10957 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 10958 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 10959 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 10960 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 10961 10962 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10963 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 10964 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 10965 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 10966 FormatStyle::BOS_None); 10967 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 10968 FormatStyle::BOS_All); 10969 // For backward compatibility: 10970 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 10971 FormatStyle::BOS_None); 10972 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 10973 FormatStyle::BOS_All); 10974 10975 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 10976 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 10977 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10978 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 10979 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 10980 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 10981 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 10982 // For backward compatibility: 10983 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 10984 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10985 10986 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 10987 CHECK_PARSE("BreakInheritanceList: BeforeComma", 10988 BreakInheritanceList, FormatStyle::BILS_BeforeComma); 10989 CHECK_PARSE("BreakInheritanceList: AfterColon", 10990 BreakInheritanceList, FormatStyle::BILS_AfterColon); 10991 CHECK_PARSE("BreakInheritanceList: BeforeColon", 10992 BreakInheritanceList, FormatStyle::BILS_BeforeColon); 10993 // For backward compatibility: 10994 CHECK_PARSE("BreakBeforeInheritanceComma: true", 10995 BreakInheritanceList, FormatStyle::BILS_BeforeComma); 10996 10997 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10998 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 10999 FormatStyle::BAS_Align); 11000 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 11001 FormatStyle::BAS_DontAlign); 11002 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 11003 FormatStyle::BAS_AlwaysBreak); 11004 // For backward compatibility: 11005 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 11006 FormatStyle::BAS_DontAlign); 11007 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 11008 FormatStyle::BAS_Align); 11009 11010 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 11011 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 11012 FormatStyle::ENAS_DontAlign); 11013 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 11014 FormatStyle::ENAS_Left); 11015 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 11016 FormatStyle::ENAS_Right); 11017 // For backward compatibility: 11018 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 11019 FormatStyle::ENAS_Left); 11020 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 11021 FormatStyle::ENAS_Right); 11022 11023 Style.UseTab = FormatStyle::UT_ForIndentation; 11024 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 11025 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 11026 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 11027 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 11028 FormatStyle::UT_ForContinuationAndIndentation); 11029 // For backward compatibility: 11030 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 11031 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 11032 11033 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11034 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 11035 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 11036 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 11037 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 11038 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 11039 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 11040 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 11041 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 11042 // For backward compatibility: 11043 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 11044 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 11045 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 11046 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 11047 11048 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 11049 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 11050 FormatStyle::SBPO_Never); 11051 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 11052 FormatStyle::SBPO_Always); 11053 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 11054 FormatStyle::SBPO_ControlStatements); 11055 // For backward compatibility: 11056 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 11057 FormatStyle::SBPO_Never); 11058 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 11059 FormatStyle::SBPO_ControlStatements); 11060 11061 Style.ColumnLimit = 123; 11062 FormatStyle BaseStyle = getLLVMStyle(); 11063 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 11064 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 11065 11066 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 11067 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 11068 FormatStyle::BS_Attach); 11069 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 11070 FormatStyle::BS_Linux); 11071 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 11072 FormatStyle::BS_Mozilla); 11073 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 11074 FormatStyle::BS_Stroustrup); 11075 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 11076 FormatStyle::BS_Allman); 11077 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 11078 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 11079 FormatStyle::BS_WebKit); 11080 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 11081 FormatStyle::BS_Custom); 11082 11083 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 11084 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 11085 FormatStyle::RTBS_None); 11086 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 11087 FormatStyle::RTBS_All); 11088 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 11089 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 11090 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 11091 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 11092 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 11093 AlwaysBreakAfterReturnType, 11094 FormatStyle::RTBS_TopLevelDefinitions); 11095 11096 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 11097 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", AlwaysBreakTemplateDeclarations, 11098 FormatStyle::BTDS_No); 11099 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", AlwaysBreakTemplateDeclarations, 11100 FormatStyle::BTDS_MultiLine); 11101 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", AlwaysBreakTemplateDeclarations, 11102 FormatStyle::BTDS_Yes); 11103 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", AlwaysBreakTemplateDeclarations, 11104 FormatStyle::BTDS_MultiLine); 11105 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", AlwaysBreakTemplateDeclarations, 11106 FormatStyle::BTDS_Yes); 11107 11108 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 11109 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 11110 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 11111 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 11112 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 11113 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 11114 AlwaysBreakAfterDefinitionReturnType, 11115 FormatStyle::DRTBS_TopLevel); 11116 11117 Style.NamespaceIndentation = FormatStyle::NI_All; 11118 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 11119 FormatStyle::NI_None); 11120 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 11121 FormatStyle::NI_Inner); 11122 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 11123 FormatStyle::NI_All); 11124 11125 // FIXME: This is required because parsing a configuration simply overwrites 11126 // the first N elements of the list instead of resetting it. 11127 Style.ForEachMacros.clear(); 11128 std::vector<std::string> BoostForeach; 11129 BoostForeach.push_back("BOOST_FOREACH"); 11130 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 11131 std::vector<std::string> BoostAndQForeach; 11132 BoostAndQForeach.push_back("BOOST_FOREACH"); 11133 BoostAndQForeach.push_back("Q_FOREACH"); 11134 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 11135 BoostAndQForeach); 11136 11137 Style.StatementMacros.clear(); 11138 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 11139 std::vector<std::string>{"QUNUSED"}); 11140 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 11141 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 11142 11143 Style.IncludeStyle.IncludeCategories.clear(); 11144 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 11145 {"abc/.*", 2}, {".*", 1}}; 11146 CHECK_PARSE("IncludeCategories:\n" 11147 " - Regex: abc/.*\n" 11148 " Priority: 2\n" 11149 " - Regex: .*\n" 11150 " Priority: 1", 11151 IncludeStyle.IncludeCategories, ExpectedCategories); 11152 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 11153 "abc$"); 11154 11155 Style.RawStringFormats.clear(); 11156 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 11157 { 11158 FormatStyle::LK_TextProto, 11159 {"pb", "proto"}, 11160 {"PARSE_TEXT_PROTO"}, 11161 /*CanonicalDelimiter=*/"", 11162 "llvm", 11163 }, 11164 { 11165 FormatStyle::LK_Cpp, 11166 {"cc", "cpp"}, 11167 {"C_CODEBLOCK", "CPPEVAL"}, 11168 /*CanonicalDelimiter=*/"cc", 11169 /*BasedOnStyle=*/"", 11170 }, 11171 }; 11172 11173 CHECK_PARSE("RawStringFormats:\n" 11174 " - Language: TextProto\n" 11175 " Delimiters:\n" 11176 " - 'pb'\n" 11177 " - 'proto'\n" 11178 " EnclosingFunctions:\n" 11179 " - 'PARSE_TEXT_PROTO'\n" 11180 " BasedOnStyle: llvm\n" 11181 " - Language: Cpp\n" 11182 " Delimiters:\n" 11183 " - 'cc'\n" 11184 " - 'cpp'\n" 11185 " EnclosingFunctions:\n" 11186 " - 'C_CODEBLOCK'\n" 11187 " - 'CPPEVAL'\n" 11188 " CanonicalDelimiter: 'cc'", 11189 RawStringFormats, ExpectedRawStringFormats); 11190 } 11191 11192 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 11193 FormatStyle Style = {}; 11194 Style.Language = FormatStyle::LK_Cpp; 11195 CHECK_PARSE("Language: Cpp\n" 11196 "IndentWidth: 12", 11197 IndentWidth, 12u); 11198 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 11199 "IndentWidth: 34", 11200 &Style), 11201 ParseError::Unsuitable); 11202 EXPECT_EQ(12u, Style.IndentWidth); 11203 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 11204 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 11205 11206 Style.Language = FormatStyle::LK_JavaScript; 11207 CHECK_PARSE("Language: JavaScript\n" 11208 "IndentWidth: 12", 11209 IndentWidth, 12u); 11210 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 11211 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 11212 "IndentWidth: 34", 11213 &Style), 11214 ParseError::Unsuitable); 11215 EXPECT_EQ(23u, Style.IndentWidth); 11216 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 11217 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 11218 11219 CHECK_PARSE("BasedOnStyle: LLVM\n" 11220 "IndentWidth: 67", 11221 IndentWidth, 67u); 11222 11223 CHECK_PARSE("---\n" 11224 "Language: JavaScript\n" 11225 "IndentWidth: 12\n" 11226 "---\n" 11227 "Language: Cpp\n" 11228 "IndentWidth: 34\n" 11229 "...\n", 11230 IndentWidth, 12u); 11231 11232 Style.Language = FormatStyle::LK_Cpp; 11233 CHECK_PARSE("---\n" 11234 "Language: JavaScript\n" 11235 "IndentWidth: 12\n" 11236 "---\n" 11237 "Language: Cpp\n" 11238 "IndentWidth: 34\n" 11239 "...\n", 11240 IndentWidth, 34u); 11241 CHECK_PARSE("---\n" 11242 "IndentWidth: 78\n" 11243 "---\n" 11244 "Language: JavaScript\n" 11245 "IndentWidth: 56\n" 11246 "...\n", 11247 IndentWidth, 78u); 11248 11249 Style.ColumnLimit = 123; 11250 Style.IndentWidth = 234; 11251 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 11252 Style.TabWidth = 345; 11253 EXPECT_FALSE(parseConfiguration("---\n" 11254 "IndentWidth: 456\n" 11255 "BreakBeforeBraces: Allman\n" 11256 "---\n" 11257 "Language: JavaScript\n" 11258 "IndentWidth: 111\n" 11259 "TabWidth: 111\n" 11260 "---\n" 11261 "Language: Cpp\n" 11262 "BreakBeforeBraces: Stroustrup\n" 11263 "TabWidth: 789\n" 11264 "...\n", 11265 &Style)); 11266 EXPECT_EQ(123u, Style.ColumnLimit); 11267 EXPECT_EQ(456u, Style.IndentWidth); 11268 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 11269 EXPECT_EQ(789u, Style.TabWidth); 11270 11271 EXPECT_EQ(parseConfiguration("---\n" 11272 "Language: JavaScript\n" 11273 "IndentWidth: 56\n" 11274 "---\n" 11275 "IndentWidth: 78\n" 11276 "...\n", 11277 &Style), 11278 ParseError::Error); 11279 EXPECT_EQ(parseConfiguration("---\n" 11280 "Language: JavaScript\n" 11281 "IndentWidth: 56\n" 11282 "---\n" 11283 "Language: JavaScript\n" 11284 "IndentWidth: 78\n" 11285 "...\n", 11286 &Style), 11287 ParseError::Error); 11288 11289 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 11290 } 11291 11292 #undef CHECK_PARSE 11293 11294 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 11295 FormatStyle Style = {}; 11296 Style.Language = FormatStyle::LK_JavaScript; 11297 Style.BreakBeforeTernaryOperators = true; 11298 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 11299 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 11300 11301 Style.BreakBeforeTernaryOperators = true; 11302 EXPECT_EQ(0, parseConfiguration("---\n" 11303 "BasedOnStyle: Google\n" 11304 "---\n" 11305 "Language: JavaScript\n" 11306 "IndentWidth: 76\n" 11307 "...\n", 11308 &Style) 11309 .value()); 11310 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 11311 EXPECT_EQ(76u, Style.IndentWidth); 11312 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 11313 } 11314 11315 TEST_F(FormatTest, ConfigurationRoundTripTest) { 11316 FormatStyle Style = getLLVMStyle(); 11317 std::string YAML = configurationAsText(Style); 11318 FormatStyle ParsedStyle = {}; 11319 ParsedStyle.Language = FormatStyle::LK_Cpp; 11320 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 11321 EXPECT_EQ(Style, ParsedStyle); 11322 } 11323 11324 TEST_F(FormatTest, WorksFor8bitEncodings) { 11325 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 11326 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 11327 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 11328 "\"\xef\xee\xf0\xf3...\"", 11329 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 11330 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 11331 "\xef\xee\xf0\xf3...\"", 11332 getLLVMStyleWithColumns(12))); 11333 } 11334 11335 TEST_F(FormatTest, HandlesUTF8BOM) { 11336 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 11337 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 11338 format("\xef\xbb\xbf#include <iostream>")); 11339 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 11340 format("\xef\xbb\xbf\n#include <iostream>")); 11341 } 11342 11343 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 11344 #if !defined(_MSC_VER) 11345 11346 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 11347 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 11348 getLLVMStyleWithColumns(35)); 11349 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 11350 getLLVMStyleWithColumns(31)); 11351 verifyFormat("// Однажды в студёную зимнюю пору...", 11352 getLLVMStyleWithColumns(36)); 11353 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 11354 verifyFormat("/* Однажды в студёную зимнюю пору... */", 11355 getLLVMStyleWithColumns(39)); 11356 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 11357 getLLVMStyleWithColumns(35)); 11358 } 11359 11360 TEST_F(FormatTest, SplitsUTF8Strings) { 11361 // Non-printable characters' width is currently considered to be the length in 11362 // bytes in UTF8. The characters can be displayed in very different manner 11363 // (zero-width, single width with a substitution glyph, expanded to their code 11364 // (e.g. "<8d>"), so there's no single correct way to handle them. 11365 EXPECT_EQ("\"aaaaÄ\"\n" 11366 "\"\xc2\x8d\";", 11367 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 11368 EXPECT_EQ("\"aaaaaaaÄ\"\n" 11369 "\"\xc2\x8d\";", 11370 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 11371 EXPECT_EQ("\"Однажды, в \"\n" 11372 "\"студёную \"\n" 11373 "\"зимнюю \"\n" 11374 "\"пору,\"", 11375 format("\"Однажды, в студёную зимнюю пору,\"", 11376 getLLVMStyleWithColumns(13))); 11377 EXPECT_EQ( 11378 "\"一 二 三 \"\n" 11379 "\"四 五六 \"\n" 11380 "\"七 八 九 \"\n" 11381 "\"十\"", 11382 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 11383 EXPECT_EQ("\"一\t\"\n" 11384 "\"二 \t\"\n" 11385 "\"三 四 \"\n" 11386 "\"五\t\"\n" 11387 "\"六 \t\"\n" 11388 "\"七 \"\n" 11389 "\"八九十\tqq\"", 11390 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 11391 getLLVMStyleWithColumns(11))); 11392 11393 // UTF8 character in an escape sequence. 11394 EXPECT_EQ("\"aaaaaa\"\n" 11395 "\"\\\xC2\x8D\"", 11396 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 11397 } 11398 11399 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 11400 EXPECT_EQ("const char *sssss =\n" 11401 " \"一二三四五六七八\\\n" 11402 " 九 十\";", 11403 format("const char *sssss = \"一二三四五六七八\\\n" 11404 " 九 十\";", 11405 getLLVMStyleWithColumns(30))); 11406 } 11407 11408 TEST_F(FormatTest, SplitsUTF8LineComments) { 11409 EXPECT_EQ("// aaaaÄ\xc2\x8d", 11410 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 11411 EXPECT_EQ("// Я из лесу\n" 11412 "// вышел; был\n" 11413 "// сильный\n" 11414 "// мороз.", 11415 format("// Я из лесу вышел; был сильный мороз.", 11416 getLLVMStyleWithColumns(13))); 11417 EXPECT_EQ("// 一二三\n" 11418 "// 四五六七\n" 11419 "// 八 九\n" 11420 "// 十", 11421 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 11422 } 11423 11424 TEST_F(FormatTest, SplitsUTF8BlockComments) { 11425 EXPECT_EQ("/* Гляжу,\n" 11426 " * поднимается\n" 11427 " * медленно в\n" 11428 " * гору\n" 11429 " * Лошадка,\n" 11430 " * везущая\n" 11431 " * хворосту\n" 11432 " * воз. */", 11433 format("/* Гляжу, поднимается медленно в гору\n" 11434 " * Лошадка, везущая хворосту воз. */", 11435 getLLVMStyleWithColumns(13))); 11436 EXPECT_EQ( 11437 "/* 一二三\n" 11438 " * 四五六七\n" 11439 " * 八 九\n" 11440 " * 十 */", 11441 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 11442 EXPECT_EQ("/* \n" 11443 " * \n" 11444 " * - */", 11445 format("/* - */", getLLVMStyleWithColumns(12))); 11446 } 11447 11448 #endif // _MSC_VER 11449 11450 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 11451 FormatStyle Style = getLLVMStyle(); 11452 11453 Style.ConstructorInitializerIndentWidth = 4; 11454 verifyFormat( 11455 "SomeClass::Constructor()\n" 11456 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 11457 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 11458 Style); 11459 11460 Style.ConstructorInitializerIndentWidth = 2; 11461 verifyFormat( 11462 "SomeClass::Constructor()\n" 11463 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 11464 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 11465 Style); 11466 11467 Style.ConstructorInitializerIndentWidth = 0; 11468 verifyFormat( 11469 "SomeClass::Constructor()\n" 11470 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 11471 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 11472 Style); 11473 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11474 verifyFormat( 11475 "SomeLongTemplateVariableName<\n" 11476 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 11477 Style); 11478 verifyFormat( 11479 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 11480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 11481 Style); 11482 } 11483 11484 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 11485 FormatStyle Style = getLLVMStyle(); 11486 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 11487 Style.ConstructorInitializerIndentWidth = 4; 11488 verifyFormat("SomeClass::Constructor()\n" 11489 " : a(a)\n" 11490 " , b(b)\n" 11491 " , c(c) {}", 11492 Style); 11493 verifyFormat("SomeClass::Constructor()\n" 11494 " : a(a) {}", 11495 Style); 11496 11497 Style.ColumnLimit = 0; 11498 verifyFormat("SomeClass::Constructor()\n" 11499 " : a(a) {}", 11500 Style); 11501 verifyFormat("SomeClass::Constructor() noexcept\n" 11502 " : a(a) {}", 11503 Style); 11504 verifyFormat("SomeClass::Constructor()\n" 11505 " : a(a)\n" 11506 " , b(b)\n" 11507 " , c(c) {}", 11508 Style); 11509 verifyFormat("SomeClass::Constructor()\n" 11510 " : a(a) {\n" 11511 " foo();\n" 11512 " bar();\n" 11513 "}", 11514 Style); 11515 11516 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11517 verifyFormat("SomeClass::Constructor()\n" 11518 " : a(a)\n" 11519 " , b(b)\n" 11520 " , c(c) {\n}", 11521 Style); 11522 verifyFormat("SomeClass::Constructor()\n" 11523 " : a(a) {\n}", 11524 Style); 11525 11526 Style.ColumnLimit = 80; 11527 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11528 Style.ConstructorInitializerIndentWidth = 2; 11529 verifyFormat("SomeClass::Constructor()\n" 11530 " : a(a)\n" 11531 " , b(b)\n" 11532 " , c(c) {}", 11533 Style); 11534 11535 Style.ConstructorInitializerIndentWidth = 0; 11536 verifyFormat("SomeClass::Constructor()\n" 11537 ": a(a)\n" 11538 ", b(b)\n" 11539 ", c(c) {}", 11540 Style); 11541 11542 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 11543 Style.ConstructorInitializerIndentWidth = 4; 11544 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 11545 verifyFormat( 11546 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 11547 Style); 11548 verifyFormat( 11549 "SomeClass::Constructor()\n" 11550 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 11551 Style); 11552 Style.ConstructorInitializerIndentWidth = 4; 11553 Style.ColumnLimit = 60; 11554 verifyFormat("SomeClass::Constructor()\n" 11555 " : aaaaaaaa(aaaaaaaa)\n" 11556 " , aaaaaaaa(aaaaaaaa)\n" 11557 " , aaaaaaaa(aaaaaaaa) {}", 11558 Style); 11559 } 11560 11561 TEST_F(FormatTest, Destructors) { 11562 verifyFormat("void F(int &i) { i.~int(); }"); 11563 verifyFormat("void F(int &i) { i->~int(); }"); 11564 } 11565 11566 TEST_F(FormatTest, FormatsWithWebKitStyle) { 11567 FormatStyle Style = getWebKitStyle(); 11568 11569 // Don't indent in outer namespaces. 11570 verifyFormat("namespace outer {\n" 11571 "int i;\n" 11572 "namespace inner {\n" 11573 " int i;\n" 11574 "} // namespace inner\n" 11575 "} // namespace outer\n" 11576 "namespace other_outer {\n" 11577 "int i;\n" 11578 "}", 11579 Style); 11580 11581 // Don't indent case labels. 11582 verifyFormat("switch (variable) {\n" 11583 "case 1:\n" 11584 "case 2:\n" 11585 " doSomething();\n" 11586 " break;\n" 11587 "default:\n" 11588 " ++variable;\n" 11589 "}", 11590 Style); 11591 11592 // Wrap before binary operators. 11593 EXPECT_EQ("void f()\n" 11594 "{\n" 11595 " if (aaaaaaaaaaaaaaaa\n" 11596 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 11597 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 11598 " return;\n" 11599 "}", 11600 format("void f() {\n" 11601 "if (aaaaaaaaaaaaaaaa\n" 11602 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 11603 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 11604 "return;\n" 11605 "}", 11606 Style)); 11607 11608 // Allow functions on a single line. 11609 verifyFormat("void f() { return; }", Style); 11610 11611 // Constructor initializers are formatted one per line with the "," on the 11612 // new line. 11613 verifyFormat("Constructor()\n" 11614 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 11615 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 11616 " aaaaaaaaaaaaaa)\n" 11617 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 11618 "{\n" 11619 "}", 11620 Style); 11621 verifyFormat("SomeClass::Constructor()\n" 11622 " : a(a)\n" 11623 "{\n" 11624 "}", 11625 Style); 11626 EXPECT_EQ("SomeClass::Constructor()\n" 11627 " : a(a)\n" 11628 "{\n" 11629 "}", 11630 format("SomeClass::Constructor():a(a){}", Style)); 11631 verifyFormat("SomeClass::Constructor()\n" 11632 " : a(a)\n" 11633 " , b(b)\n" 11634 " , c(c)\n" 11635 "{\n" 11636 "}", 11637 Style); 11638 verifyFormat("SomeClass::Constructor()\n" 11639 " : a(a)\n" 11640 "{\n" 11641 " foo();\n" 11642 " bar();\n" 11643 "}", 11644 Style); 11645 11646 // Access specifiers should be aligned left. 11647 verifyFormat("class C {\n" 11648 "public:\n" 11649 " int i;\n" 11650 "};", 11651 Style); 11652 11653 // Do not align comments. 11654 verifyFormat("int a; // Do not\n" 11655 "double b; // align comments.", 11656 Style); 11657 11658 // Do not align operands. 11659 EXPECT_EQ("ASSERT(aaaa\n" 11660 " || bbbb);", 11661 format("ASSERT ( aaaa\n||bbbb);", Style)); 11662 11663 // Accept input's line breaks. 11664 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 11665 " || bbbbbbbbbbbbbbb) {\n" 11666 " i++;\n" 11667 "}", 11668 format("if (aaaaaaaaaaaaaaa\n" 11669 "|| bbbbbbbbbbbbbbb) { i++; }", 11670 Style)); 11671 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 11672 " i++;\n" 11673 "}", 11674 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 11675 11676 // Don't automatically break all macro definitions (llvm.org/PR17842). 11677 verifyFormat("#define aNumber 10", Style); 11678 // However, generally keep the line breaks that the user authored. 11679 EXPECT_EQ("#define aNumber \\\n" 11680 " 10", 11681 format("#define aNumber \\\n" 11682 " 10", 11683 Style)); 11684 11685 // Keep empty and one-element array literals on a single line. 11686 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 11687 " copyItems:YES];", 11688 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 11689 "copyItems:YES];", 11690 Style)); 11691 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 11692 " copyItems:YES];", 11693 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 11694 " copyItems:YES];", 11695 Style)); 11696 // FIXME: This does not seem right, there should be more indentation before 11697 // the array literal's entries. Nested blocks have the same problem. 11698 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11699 " @\"a\",\n" 11700 " @\"a\"\n" 11701 "]\n" 11702 " copyItems:YES];", 11703 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11704 " @\"a\",\n" 11705 " @\"a\"\n" 11706 " ]\n" 11707 " copyItems:YES];", 11708 Style)); 11709 EXPECT_EQ( 11710 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11711 " copyItems:YES];", 11712 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11713 " copyItems:YES];", 11714 Style)); 11715 11716 verifyFormat("[self.a b:c c:d];", Style); 11717 EXPECT_EQ("[self.a b:c\n" 11718 " c:d];", 11719 format("[self.a b:c\n" 11720 "c:d];", 11721 Style)); 11722 } 11723 11724 TEST_F(FormatTest, FormatsLambdas) { 11725 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 11726 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 11727 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 11728 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 11729 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 11730 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 11731 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 11732 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 11733 verifyFormat("int x = f(*+[] {});"); 11734 verifyFormat("void f() {\n" 11735 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 11736 "}\n"); 11737 verifyFormat("void f() {\n" 11738 " other(x.begin(), //\n" 11739 " x.end(), //\n" 11740 " [&](int, int) { return 1; });\n" 11741 "}\n"); 11742 verifyFormat("SomeFunction([]() { // A cool function...\n" 11743 " return 43;\n" 11744 "});"); 11745 EXPECT_EQ("SomeFunction([]() {\n" 11746 "#define A a\n" 11747 " return 43;\n" 11748 "});", 11749 format("SomeFunction([](){\n" 11750 "#define A a\n" 11751 "return 43;\n" 11752 "});")); 11753 verifyFormat("void f() {\n" 11754 " SomeFunction([](decltype(x), A *a) {});\n" 11755 "}"); 11756 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11757 " [](const aaaaaaaaaa &a) { return a; });"); 11758 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 11759 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 11760 "});"); 11761 verifyFormat("Constructor()\n" 11762 " : Field([] { // comment\n" 11763 " int i;\n" 11764 " }) {}"); 11765 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 11766 " return some_parameter.size();\n" 11767 "};"); 11768 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 11769 " [](const string &s) { return s; };"); 11770 verifyFormat("int i = aaaaaa ? 1 //\n" 11771 " : [] {\n" 11772 " return 2; //\n" 11773 " }();"); 11774 verifyFormat("llvm::errs() << \"number of twos is \"\n" 11775 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 11776 " return x == 2; // force break\n" 11777 " });"); 11778 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11779 " [=](int iiiiiiiiiiii) {\n" 11780 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 11781 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 11782 " });", 11783 getLLVMStyleWithColumns(60)); 11784 verifyFormat("SomeFunction({[&] {\n" 11785 " // comment\n" 11786 " },\n" 11787 " [&] {\n" 11788 " // comment\n" 11789 " }});"); 11790 verifyFormat("SomeFunction({[&] {\n" 11791 " // comment\n" 11792 "}});"); 11793 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 11794 " [&]() { return true; },\n" 11795 " aaaaa aaaaaaaaa);"); 11796 11797 // Lambdas with return types. 11798 verifyFormat("int c = []() -> int { return 2; }();\n"); 11799 verifyFormat("int c = []() -> int * { return 2; }();\n"); 11800 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 11801 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 11802 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 11803 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 11804 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 11805 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 11806 verifyFormat("[a, a]() -> a<1> {};"); 11807 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 11808 " int j) -> int {\n" 11809 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 11810 "};"); 11811 verifyFormat( 11812 "aaaaaaaaaaaaaaaaaaaaaa(\n" 11813 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 11814 " return aaaaaaaaaaaaaaaaa;\n" 11815 " });", 11816 getLLVMStyleWithColumns(70)); 11817 verifyFormat("[]() //\n" 11818 " -> int {\n" 11819 " return 1; //\n" 11820 "};"); 11821 11822 // Multiple lambdas in the same parentheses change indentation rules. 11823 verifyFormat("SomeFunction(\n" 11824 " []() {\n" 11825 " int i = 42;\n" 11826 " return i;\n" 11827 " },\n" 11828 " []() {\n" 11829 " int j = 43;\n" 11830 " return j;\n" 11831 " });"); 11832 11833 // More complex introducers. 11834 verifyFormat("return [i, args...] {};"); 11835 11836 // Not lambdas. 11837 verifyFormat("constexpr char hello[]{\"hello\"};"); 11838 verifyFormat("double &operator[](int i) { return 0; }\n" 11839 "int i;"); 11840 verifyFormat("std::unique_ptr<int[]> foo() {}"); 11841 verifyFormat("int i = a[a][a]->f();"); 11842 verifyFormat("int i = (*b)[a]->f();"); 11843 11844 // Other corner cases. 11845 verifyFormat("void f() {\n" 11846 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 11847 " );\n" 11848 "}"); 11849 11850 // Lambdas created through weird macros. 11851 verifyFormat("void f() {\n" 11852 " MACRO((const AA &a) { return 1; });\n" 11853 " MACRO((AA &a) { return 1; });\n" 11854 "}"); 11855 11856 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 11857 " doo_dah();\n" 11858 " doo_dah();\n" 11859 " })) {\n" 11860 "}"); 11861 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 11862 " doo_dah();\n" 11863 " doo_dah();\n" 11864 " })) {\n" 11865 "}"); 11866 verifyFormat("auto lambda = []() {\n" 11867 " int a = 2\n" 11868 "#if A\n" 11869 " + 2\n" 11870 "#endif\n" 11871 " ;\n" 11872 "};"); 11873 11874 // Lambdas with complex multiline introducers. 11875 verifyFormat( 11876 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11877 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 11878 " -> ::std::unordered_set<\n" 11879 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 11880 " //\n" 11881 " });"); 11882 } 11883 11884 TEST_F(FormatTest, EmptyLinesInLambdas) { 11885 verifyFormat("auto lambda = []() {\n" 11886 " x(); //\n" 11887 "};", 11888 "auto lambda = []() {\n" 11889 "\n" 11890 " x(); //\n" 11891 "\n" 11892 "};"); 11893 } 11894 11895 TEST_F(FormatTest, FormatsBlocks) { 11896 FormatStyle ShortBlocks = getLLVMStyle(); 11897 ShortBlocks.AllowShortBlocksOnASingleLine = true; 11898 verifyFormat("int (^Block)(int, int);", ShortBlocks); 11899 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 11900 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 11901 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 11902 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 11903 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 11904 11905 verifyFormat("foo(^{ bar(); });", ShortBlocks); 11906 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 11907 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 11908 11909 verifyFormat("[operation setCompletionBlock:^{\n" 11910 " [self onOperationDone];\n" 11911 "}];"); 11912 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 11913 " [self onOperationDone];\n" 11914 "}]};"); 11915 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 11916 " f();\n" 11917 "}];"); 11918 verifyFormat("int a = [operation block:^int(int *i) {\n" 11919 " return 1;\n" 11920 "}];"); 11921 verifyFormat("[myObject doSomethingWith:arg1\n" 11922 " aaa:^int(int *a) {\n" 11923 " return 1;\n" 11924 " }\n" 11925 " bbb:f(a * bbbbbbbb)];"); 11926 11927 verifyFormat("[operation setCompletionBlock:^{\n" 11928 " [self.delegate newDataAvailable];\n" 11929 "}];", 11930 getLLVMStyleWithColumns(60)); 11931 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 11932 " NSString *path = [self sessionFilePath];\n" 11933 " if (path) {\n" 11934 " // ...\n" 11935 " }\n" 11936 "});"); 11937 verifyFormat("[[SessionService sharedService]\n" 11938 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11939 " if (window) {\n" 11940 " [self windowDidLoad:window];\n" 11941 " } else {\n" 11942 " [self errorLoadingWindow];\n" 11943 " }\n" 11944 " }];"); 11945 verifyFormat("void (^largeBlock)(void) = ^{\n" 11946 " // ...\n" 11947 "};\n", 11948 getLLVMStyleWithColumns(40)); 11949 verifyFormat("[[SessionService sharedService]\n" 11950 " loadWindowWithCompletionBlock: //\n" 11951 " ^(SessionWindow *window) {\n" 11952 " if (window) {\n" 11953 " [self windowDidLoad:window];\n" 11954 " } else {\n" 11955 " [self errorLoadingWindow];\n" 11956 " }\n" 11957 " }];", 11958 getLLVMStyleWithColumns(60)); 11959 verifyFormat("[myObject doSomethingWith:arg1\n" 11960 " firstBlock:^(Foo *a) {\n" 11961 " // ...\n" 11962 " int i;\n" 11963 " }\n" 11964 " secondBlock:^(Bar *b) {\n" 11965 " // ...\n" 11966 " int i;\n" 11967 " }\n" 11968 " thirdBlock:^Foo(Bar *b) {\n" 11969 " // ...\n" 11970 " int i;\n" 11971 " }];"); 11972 verifyFormat("[myObject doSomethingWith:arg1\n" 11973 " firstBlock:-1\n" 11974 " secondBlock:^(Bar *b) {\n" 11975 " // ...\n" 11976 " int i;\n" 11977 " }];"); 11978 11979 verifyFormat("f(^{\n" 11980 " @autoreleasepool {\n" 11981 " if (a) {\n" 11982 " g();\n" 11983 " }\n" 11984 " }\n" 11985 "});"); 11986 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 11987 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 11988 "};"); 11989 11990 FormatStyle FourIndent = getLLVMStyle(); 11991 FourIndent.ObjCBlockIndentWidth = 4; 11992 verifyFormat("[operation setCompletionBlock:^{\n" 11993 " [self onOperationDone];\n" 11994 "}];", 11995 FourIndent); 11996 } 11997 11998 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 11999 FormatStyle ZeroColumn = getLLVMStyle(); 12000 ZeroColumn.ColumnLimit = 0; 12001 12002 verifyFormat("[[SessionService sharedService] " 12003 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12004 " if (window) {\n" 12005 " [self windowDidLoad:window];\n" 12006 " } else {\n" 12007 " [self errorLoadingWindow];\n" 12008 " }\n" 12009 "}];", 12010 ZeroColumn); 12011 EXPECT_EQ("[[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 format("[[SessionService sharedService]\n" 12020 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12021 " if (window) {\n" 12022 " [self windowDidLoad:window];\n" 12023 " } else {\n" 12024 " [self errorLoadingWindow];\n" 12025 " }\n" 12026 "}];", 12027 ZeroColumn)); 12028 verifyFormat("[myObject doSomethingWith:arg1\n" 12029 " firstBlock:^(Foo *a) {\n" 12030 " // ...\n" 12031 " int i;\n" 12032 " }\n" 12033 " secondBlock:^(Bar *b) {\n" 12034 " // ...\n" 12035 " int i;\n" 12036 " }\n" 12037 " thirdBlock:^Foo(Bar *b) {\n" 12038 " // ...\n" 12039 " int i;\n" 12040 " }];", 12041 ZeroColumn); 12042 verifyFormat("f(^{\n" 12043 " @autoreleasepool {\n" 12044 " if (a) {\n" 12045 " g();\n" 12046 " }\n" 12047 " }\n" 12048 "});", 12049 ZeroColumn); 12050 verifyFormat("void (^largeBlock)(void) = ^{\n" 12051 " // ...\n" 12052 "};", 12053 ZeroColumn); 12054 12055 ZeroColumn.AllowShortBlocksOnASingleLine = true; 12056 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 12057 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 12058 ZeroColumn.AllowShortBlocksOnASingleLine = false; 12059 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 12060 " int i;\n" 12061 "};", 12062 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 12063 } 12064 12065 TEST_F(FormatTest, SupportsCRLF) { 12066 EXPECT_EQ("int a;\r\n" 12067 "int b;\r\n" 12068 "int c;\r\n", 12069 format("int a;\r\n" 12070 " int b;\r\n" 12071 " int c;\r\n", 12072 getLLVMStyle())); 12073 EXPECT_EQ("int a;\r\n" 12074 "int b;\r\n" 12075 "int c;\r\n", 12076 format("int a;\r\n" 12077 " int b;\n" 12078 " int c;\r\n", 12079 getLLVMStyle())); 12080 EXPECT_EQ("int a;\n" 12081 "int b;\n" 12082 "int c;\n", 12083 format("int a;\r\n" 12084 " int b;\n" 12085 " int c;\n", 12086 getLLVMStyle())); 12087 EXPECT_EQ("\"aaaaaaa \"\r\n" 12088 "\"bbbbbbb\";\r\n", 12089 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 12090 EXPECT_EQ("#define A \\\r\n" 12091 " b; \\\r\n" 12092 " c; \\\r\n" 12093 " d;\r\n", 12094 format("#define A \\\r\n" 12095 " b; \\\r\n" 12096 " c; d; \r\n", 12097 getGoogleStyle())); 12098 12099 EXPECT_EQ("/*\r\n" 12100 "multi line block comments\r\n" 12101 "should not introduce\r\n" 12102 "an extra carriage return\r\n" 12103 "*/\r\n", 12104 format("/*\r\n" 12105 "multi line block comments\r\n" 12106 "should not introduce\r\n" 12107 "an extra carriage return\r\n" 12108 "*/\r\n")); 12109 } 12110 12111 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 12112 verifyFormat("MY_CLASS(C) {\n" 12113 " int i;\n" 12114 " int j;\n" 12115 "};"); 12116 } 12117 12118 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 12119 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 12120 TwoIndent.ContinuationIndentWidth = 2; 12121 12122 EXPECT_EQ("int i =\n" 12123 " longFunction(\n" 12124 " arg);", 12125 format("int i = longFunction(arg);", TwoIndent)); 12126 12127 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 12128 SixIndent.ContinuationIndentWidth = 6; 12129 12130 EXPECT_EQ("int i =\n" 12131 " longFunction(\n" 12132 " arg);", 12133 format("int i = longFunction(arg);", SixIndent)); 12134 } 12135 12136 TEST_F(FormatTest, SpacesInAngles) { 12137 FormatStyle Spaces = getLLVMStyle(); 12138 Spaces.SpacesInAngles = true; 12139 12140 verifyFormat("static_cast< int >(arg);", Spaces); 12141 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 12142 verifyFormat("f< int, float >();", Spaces); 12143 verifyFormat("template <> g() {}", Spaces); 12144 verifyFormat("template < std::vector< int > > f() {}", Spaces); 12145 verifyFormat("std::function< void(int, int) > fct;", Spaces); 12146 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 12147 Spaces); 12148 12149 Spaces.Standard = FormatStyle::LS_Cpp03; 12150 Spaces.SpacesInAngles = true; 12151 verifyFormat("A< A< int > >();", Spaces); 12152 12153 Spaces.SpacesInAngles = false; 12154 verifyFormat("A<A<int> >();", Spaces); 12155 12156 Spaces.Standard = FormatStyle::LS_Cpp11; 12157 Spaces.SpacesInAngles = true; 12158 verifyFormat("A< A< int > >();", Spaces); 12159 12160 Spaces.SpacesInAngles = false; 12161 verifyFormat("A<A<int>>();", Spaces); 12162 } 12163 12164 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 12165 FormatStyle Style = getLLVMStyle(); 12166 Style.SpaceAfterTemplateKeyword = false; 12167 verifyFormat("template<int> void foo();", Style); 12168 } 12169 12170 TEST_F(FormatTest, TripleAngleBrackets) { 12171 verifyFormat("f<<<1, 1>>>();"); 12172 verifyFormat("f<<<1, 1, 1, s>>>();"); 12173 verifyFormat("f<<<a, b, c, d>>>();"); 12174 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 12175 verifyFormat("f<param><<<1, 1>>>();"); 12176 verifyFormat("f<1><<<1, 1>>>();"); 12177 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 12178 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12179 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 12180 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 12181 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 12182 } 12183 12184 TEST_F(FormatTest, MergeLessLessAtEnd) { 12185 verifyFormat("<<"); 12186 EXPECT_EQ("< < <", format("\\\n<<<")); 12187 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12188 "aaallvm::outs() <<"); 12189 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12190 "aaaallvm::outs()\n <<"); 12191 } 12192 12193 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 12194 std::string code = "#if A\n" 12195 "#if B\n" 12196 "a.\n" 12197 "#endif\n" 12198 " a = 1;\n" 12199 "#else\n" 12200 "#endif\n" 12201 "#if C\n" 12202 "#else\n" 12203 "#endif\n"; 12204 EXPECT_EQ(code, format(code)); 12205 } 12206 12207 TEST_F(FormatTest, HandleConflictMarkers) { 12208 // Git/SVN conflict markers. 12209 EXPECT_EQ("int a;\n" 12210 "void f() {\n" 12211 " callme(some(parameter1,\n" 12212 "<<<<<<< text by the vcs\n" 12213 " parameter2),\n" 12214 "||||||| text by the vcs\n" 12215 " parameter2),\n" 12216 " parameter3,\n" 12217 "======= text by the vcs\n" 12218 " parameter2, parameter3),\n" 12219 ">>>>>>> text by the vcs\n" 12220 " otherparameter);\n", 12221 format("int a;\n" 12222 "void f() {\n" 12223 " callme(some(parameter1,\n" 12224 "<<<<<<< text by the vcs\n" 12225 " parameter2),\n" 12226 "||||||| text by the vcs\n" 12227 " parameter2),\n" 12228 " parameter3,\n" 12229 "======= text by the vcs\n" 12230 " parameter2,\n" 12231 " parameter3),\n" 12232 ">>>>>>> text by the vcs\n" 12233 " otherparameter);\n")); 12234 12235 // Perforce markers. 12236 EXPECT_EQ("void f() {\n" 12237 " function(\n" 12238 ">>>> text by the vcs\n" 12239 " parameter,\n" 12240 "==== text by the vcs\n" 12241 " parameter,\n" 12242 "==== text by the vcs\n" 12243 " parameter,\n" 12244 "<<<< text by the vcs\n" 12245 " parameter);\n", 12246 format("void f() {\n" 12247 " function(\n" 12248 ">>>> text by the vcs\n" 12249 " parameter,\n" 12250 "==== text by the vcs\n" 12251 " parameter,\n" 12252 "==== text by the vcs\n" 12253 " parameter,\n" 12254 "<<<< text by the vcs\n" 12255 " parameter);\n")); 12256 12257 EXPECT_EQ("<<<<<<<\n" 12258 "|||||||\n" 12259 "=======\n" 12260 ">>>>>>>", 12261 format("<<<<<<<\n" 12262 "|||||||\n" 12263 "=======\n" 12264 ">>>>>>>")); 12265 12266 EXPECT_EQ("<<<<<<<\n" 12267 "|||||||\n" 12268 "int i;\n" 12269 "=======\n" 12270 ">>>>>>>", 12271 format("<<<<<<<\n" 12272 "|||||||\n" 12273 "int i;\n" 12274 "=======\n" 12275 ">>>>>>>")); 12276 12277 // FIXME: Handle parsing of macros around conflict markers correctly: 12278 EXPECT_EQ("#define Macro \\\n" 12279 "<<<<<<<\n" 12280 "Something \\\n" 12281 "|||||||\n" 12282 "Else \\\n" 12283 "=======\n" 12284 "Other \\\n" 12285 ">>>>>>>\n" 12286 " End int i;\n", 12287 format("#define Macro \\\n" 12288 "<<<<<<<\n" 12289 " Something \\\n" 12290 "|||||||\n" 12291 " Else \\\n" 12292 "=======\n" 12293 " Other \\\n" 12294 ">>>>>>>\n" 12295 " End\n" 12296 "int i;\n")); 12297 } 12298 12299 TEST_F(FormatTest, DisableRegions) { 12300 EXPECT_EQ("int i;\n" 12301 "// clang-format off\n" 12302 " int j;\n" 12303 "// clang-format on\n" 12304 "int k;", 12305 format(" int i;\n" 12306 " // clang-format off\n" 12307 " int j;\n" 12308 " // clang-format on\n" 12309 " int k;")); 12310 EXPECT_EQ("int i;\n" 12311 "/* clang-format off */\n" 12312 " int j;\n" 12313 "/* clang-format on */\n" 12314 "int k;", 12315 format(" int i;\n" 12316 " /* clang-format off */\n" 12317 " int j;\n" 12318 " /* clang-format on */\n" 12319 " int k;")); 12320 12321 // Don't reflow comments within disabled regions. 12322 EXPECT_EQ( 12323 "// clang-format off\n" 12324 "// long long long long long long line\n" 12325 "/* clang-format on */\n" 12326 "/* long long long\n" 12327 " * long long long\n" 12328 " * line */\n" 12329 "int i;\n" 12330 "/* clang-format off */\n" 12331 "/* long long long long long long line */\n", 12332 format("// clang-format off\n" 12333 "// long long long long long long line\n" 12334 "/* clang-format on */\n" 12335 "/* long long long long long long line */\n" 12336 "int i;\n" 12337 "/* clang-format off */\n" 12338 "/* long long long long long long line */\n", 12339 getLLVMStyleWithColumns(20))); 12340 } 12341 12342 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 12343 format("? ) ="); 12344 verifyNoCrash("#define a\\\n /**/}"); 12345 } 12346 12347 TEST_F(FormatTest, FormatsTableGenCode) { 12348 FormatStyle Style = getLLVMStyle(); 12349 Style.Language = FormatStyle::LK_TableGen; 12350 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 12351 } 12352 12353 TEST_F(FormatTest, ArrayOfTemplates) { 12354 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 12355 format("auto a = new unique_ptr<int > [ 10];")); 12356 12357 FormatStyle Spaces = getLLVMStyle(); 12358 Spaces.SpacesInSquareBrackets = true; 12359 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 12360 format("auto a = new unique_ptr<int > [10];", Spaces)); 12361 } 12362 12363 TEST_F(FormatTest, ArrayAsTemplateType) { 12364 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 12365 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 12366 12367 FormatStyle Spaces = getLLVMStyle(); 12368 Spaces.SpacesInSquareBrackets = true; 12369 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 12370 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 12371 } 12372 12373 TEST_F(FormatTest, NoSpaceAfterSuper) { 12374 verifyFormat("__super::FooBar();"); 12375 } 12376 12377 TEST(FormatStyle, GetStyleWithEmptyFileName) { 12378 llvm::vfs::InMemoryFileSystem FS; 12379 auto Style1 = getStyle("file", "", "Google", "", &FS); 12380 ASSERT_TRUE((bool)Style1); 12381 ASSERT_EQ(*Style1, getGoogleStyle()); 12382 } 12383 12384 TEST(FormatStyle, GetStyleOfFile) { 12385 llvm::vfs::InMemoryFileSystem FS; 12386 // Test 1: format file in the same directory. 12387 ASSERT_TRUE( 12388 FS.addFile("/a/.clang-format", 0, 12389 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 12390 ASSERT_TRUE( 12391 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 12392 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 12393 ASSERT_TRUE((bool)Style1); 12394 ASSERT_EQ(*Style1, getLLVMStyle()); 12395 12396 // Test 2.1: fallback to default. 12397 ASSERT_TRUE( 12398 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 12399 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 12400 ASSERT_TRUE((bool)Style2); 12401 ASSERT_EQ(*Style2, getMozillaStyle()); 12402 12403 // Test 2.2: no format on 'none' fallback style. 12404 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 12405 ASSERT_TRUE((bool)Style2); 12406 ASSERT_EQ(*Style2, getNoStyle()); 12407 12408 // Test 2.3: format if config is found with no based style while fallback is 12409 // 'none'. 12410 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 12411 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 12412 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 12413 ASSERT_TRUE((bool)Style2); 12414 ASSERT_EQ(*Style2, getLLVMStyle()); 12415 12416 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 12417 Style2 = getStyle("{}", "a.h", "none", "", &FS); 12418 ASSERT_TRUE((bool)Style2); 12419 ASSERT_EQ(*Style2, getLLVMStyle()); 12420 12421 // Test 3: format file in parent directory. 12422 ASSERT_TRUE( 12423 FS.addFile("/c/.clang-format", 0, 12424 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 12425 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 12426 llvm::MemoryBuffer::getMemBuffer("int i;"))); 12427 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 12428 ASSERT_TRUE((bool)Style3); 12429 ASSERT_EQ(*Style3, getGoogleStyle()); 12430 12431 // Test 4: error on invalid fallback style 12432 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 12433 ASSERT_FALSE((bool)Style4); 12434 llvm::consumeError(Style4.takeError()); 12435 12436 // Test 5: error on invalid yaml on command line 12437 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 12438 ASSERT_FALSE((bool)Style5); 12439 llvm::consumeError(Style5.takeError()); 12440 12441 // Test 6: error on invalid style 12442 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 12443 ASSERT_FALSE((bool)Style6); 12444 llvm::consumeError(Style6.takeError()); 12445 12446 // Test 7: found config file, error on parsing it 12447 ASSERT_TRUE( 12448 FS.addFile("/d/.clang-format", 0, 12449 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 12450 "InvalidKey: InvalidValue"))); 12451 ASSERT_TRUE( 12452 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 12453 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 12454 ASSERT_FALSE((bool)Style7); 12455 llvm::consumeError(Style7.takeError()); 12456 } 12457 12458 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 12459 // Column limit is 20. 12460 std::string Code = "Type *a =\n" 12461 " new Type();\n" 12462 "g(iiiii, 0, jjjjj,\n" 12463 " 0, kkkkk, 0, mm);\n" 12464 "int bad = format ;"; 12465 std::string Expected = "auto a = new Type();\n" 12466 "g(iiiii, nullptr,\n" 12467 " jjjjj, nullptr,\n" 12468 " kkkkk, nullptr,\n" 12469 " mm);\n" 12470 "int bad = format ;"; 12471 FileID ID = Context.createInMemoryFile("format.cpp", Code); 12472 tooling::Replacements Replaces = toReplacements( 12473 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 12474 "auto "), 12475 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 12476 "nullptr"), 12477 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 12478 "nullptr"), 12479 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 12480 "nullptr")}); 12481 12482 format::FormatStyle Style = format::getLLVMStyle(); 12483 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 12484 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 12485 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 12486 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 12487 auto Result = applyAllReplacements(Code, *FormattedReplaces); 12488 EXPECT_TRUE(static_cast<bool>(Result)); 12489 EXPECT_EQ(Expected, *Result); 12490 } 12491 12492 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 12493 std::string Code = "#include \"a.h\"\n" 12494 "#include \"c.h\"\n" 12495 "\n" 12496 "int main() {\n" 12497 " return 0;\n" 12498 "}"; 12499 std::string Expected = "#include \"a.h\"\n" 12500 "#include \"b.h\"\n" 12501 "#include \"c.h\"\n" 12502 "\n" 12503 "int main() {\n" 12504 " return 0;\n" 12505 "}"; 12506 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 12507 tooling::Replacements Replaces = toReplacements( 12508 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 12509 "#include \"b.h\"\n")}); 12510 12511 format::FormatStyle Style = format::getLLVMStyle(); 12512 Style.SortIncludes = true; 12513 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 12514 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 12515 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 12516 auto Result = applyAllReplacements(Code, *FormattedReplaces); 12517 EXPECT_TRUE(static_cast<bool>(Result)); 12518 EXPECT_EQ(Expected, *Result); 12519 } 12520 12521 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 12522 EXPECT_EQ("using std::cin;\n" 12523 "using std::cout;", 12524 format("using std::cout;\n" 12525 "using std::cin;", getGoogleStyle())); 12526 } 12527 12528 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 12529 format::FormatStyle Style = format::getLLVMStyle(); 12530 Style.Standard = FormatStyle::LS_Cpp03; 12531 // cpp03 recognize this string as identifier u8 and literal character 'a' 12532 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 12533 } 12534 12535 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 12536 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 12537 // all modes, including C++11, C++14 and C++17 12538 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 12539 } 12540 12541 TEST_F(FormatTest, DoNotFormatLikelyXml) { 12542 EXPECT_EQ("<!-- ;> -->", 12543 format("<!-- ;> -->", getGoogleStyle())); 12544 EXPECT_EQ(" <!-- >; -->", 12545 format(" <!-- >; -->", getGoogleStyle())); 12546 } 12547 12548 TEST_F(FormatTest, StructuredBindings) { 12549 // Structured bindings is a C++17 feature. 12550 // all modes, including C++11, C++14 and C++17 12551 verifyFormat("auto [a, b] = f();"); 12552 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 12553 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 12554 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 12555 EXPECT_EQ("auto const volatile [a, b] = f();", 12556 format("auto const volatile[a, b] = f();")); 12557 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 12558 EXPECT_EQ("auto &[a, b, c] = f();", 12559 format("auto &[ a , b,c ] = f();")); 12560 EXPECT_EQ("auto &&[a, b, c] = f();", 12561 format("auto &&[ a , b,c ] = f();")); 12562 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 12563 EXPECT_EQ("auto const volatile &&[a, b] = f();", 12564 format("auto const volatile &&[a, b] = f();")); 12565 EXPECT_EQ("auto const &&[a, b] = f();", format("auto const && [a, b] = f();")); 12566 EXPECT_EQ("const auto &[a, b] = f();", format("const auto & [a, b] = f();")); 12567 EXPECT_EQ("const auto volatile &&[a, b] = f();", 12568 format("const auto volatile &&[a, b] = f();")); 12569 EXPECT_EQ("volatile const auto &&[a, b] = f();", 12570 format("volatile const auto &&[a, b] = f();")); 12571 EXPECT_EQ("const auto &&[a, b] = f();", format("const auto && [a, b] = f();")); 12572 12573 // Make sure we don't mistake structured bindings for lambdas. 12574 FormatStyle PointerMiddle = getLLVMStyle(); 12575 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 12576 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 12577 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 12578 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 12579 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 12580 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 12581 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 12582 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 12583 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 12584 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 12585 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 12586 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 12587 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 12588 12589 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 12590 format("for (const auto && [a, b] : some_range) {\n}")); 12591 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 12592 format("for (const auto & [a, b] : some_range) {\n}")); 12593 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 12594 format("for (const auto[a, b] : some_range) {\n}")); 12595 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 12596 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 12597 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 12598 EXPECT_EQ("auto const &[x, y](expr);", format("auto const & [x,y] (expr);")); 12599 EXPECT_EQ("auto const &&[x, y](expr);", format("auto const && [x,y] (expr);")); 12600 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 12601 EXPECT_EQ("auto const &[x, y]{expr};", format("auto const & [x,y] {expr};")); 12602 EXPECT_EQ("auto const &&[x, y]{expr};", format("auto const && [x,y] {expr};")); 12603 12604 format::FormatStyle Spaces = format::getLLVMStyle(); 12605 Spaces.SpacesInSquareBrackets = true; 12606 verifyFormat("auto [ a, b ] = f();", Spaces); 12607 verifyFormat("auto &&[ a, b ] = f();", Spaces); 12608 verifyFormat("auto &[ a, b ] = f();", Spaces); 12609 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 12610 verifyFormat("auto const &[ a, b ] = f();", Spaces); 12611 } 12612 12613 TEST_F(FormatTest, FileAndCode) { 12614 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 12615 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 12616 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 12617 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 12618 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n")); 12619 EXPECT_EQ( 12620 FormatStyle::LK_ObjC, 12621 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 12622 EXPECT_EQ(FormatStyle::LK_ObjC, 12623 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 12624 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 12625 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 12626 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n")); 12627 EXPECT_EQ(FormatStyle::LK_ObjC, 12628 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 12629 EXPECT_EQ( 12630 FormatStyle::LK_ObjC, 12631 guessLanguage("foo.h", 12632 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 12633 EXPECT_EQ( 12634 FormatStyle::LK_Cpp, 12635 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 12636 } 12637 12638 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 12639 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 12640 EXPECT_EQ(FormatStyle::LK_ObjC, 12641 guessLanguage("foo.h", "array[[calculator getIndex]];")); 12642 EXPECT_EQ(FormatStyle::LK_Cpp, 12643 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 12644 EXPECT_EQ( 12645 FormatStyle::LK_Cpp, 12646 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 12647 EXPECT_EQ(FormatStyle::LK_ObjC, 12648 guessLanguage("foo.h", "[[noreturn foo] bar];")); 12649 EXPECT_EQ(FormatStyle::LK_Cpp, 12650 guessLanguage("foo.h", "[[clang::fallthrough]];")); 12651 EXPECT_EQ(FormatStyle::LK_ObjC, 12652 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 12653 EXPECT_EQ(FormatStyle::LK_Cpp, 12654 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 12655 EXPECT_EQ(FormatStyle::LK_Cpp, 12656 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 12657 EXPECT_EQ(FormatStyle::LK_ObjC, 12658 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 12659 EXPECT_EQ(FormatStyle::LK_Cpp, 12660 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 12661 EXPECT_EQ( 12662 FormatStyle::LK_Cpp, 12663 guessLanguage("foo.h", 12664 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 12665 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 12666 } 12667 12668 TEST_F(FormatTest, GuessLanguageWithCaret) { 12669 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 12670 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 12671 EXPECT_EQ(FormatStyle::LK_ObjC, 12672 guessLanguage("foo.h", "int(^)(char, float);")); 12673 EXPECT_EQ(FormatStyle::LK_ObjC, 12674 guessLanguage("foo.h", "int(^foo)(char, float);")); 12675 EXPECT_EQ(FormatStyle::LK_ObjC, 12676 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 12677 EXPECT_EQ(FormatStyle::LK_ObjC, 12678 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 12679 EXPECT_EQ( 12680 FormatStyle::LK_ObjC, 12681 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 12682 } 12683 12684 TEST_F(FormatTest, GuessLanguageWithChildLines) { 12685 EXPECT_EQ(FormatStyle::LK_Cpp, 12686 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 12687 EXPECT_EQ(FormatStyle::LK_ObjC, 12688 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 12689 EXPECT_EQ( 12690 FormatStyle::LK_Cpp, 12691 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 12692 EXPECT_EQ( 12693 FormatStyle::LK_ObjC, 12694 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 12695 } 12696 12697 } // end namespace 12698 } // end namespace format 12699 } // end namespace clang 12700