1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/Format/Format.h" 10 11 #include "../Tooling/ReplacementTest.h" 12 #include "FormatTestUtils.h" 13 14 #include "clang/Frontend/TextDiagnosticPrinter.h" 15 #include "llvm/Support/Debug.h" 16 #include "llvm/Support/MemoryBuffer.h" 17 #include "gtest/gtest.h" 18 19 #define DEBUG_TYPE "format-test" 20 21 using clang::tooling::ReplacementTest; 22 using clang::tooling::toReplacements; 23 24 namespace clang { 25 namespace format { 26 namespace { 27 28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 29 30 class FormatTest : public ::testing::Test { 31 protected: 32 enum StatusCheck { 33 SC_ExpectComplete, 34 SC_ExpectIncomplete, 35 SC_DoNotCheck 36 }; 37 38 std::string format(llvm::StringRef Code, 39 const FormatStyle &Style = getLLVMStyle(), 40 StatusCheck CheckComplete = SC_ExpectComplete) { 41 LLVM_DEBUG(llvm::errs() << "---\n"); 42 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 43 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 44 FormattingAttemptStatus Status; 45 tooling::Replacements Replaces = 46 reformat(Style, Code, Ranges, "<stdin>", &Status); 47 if (CheckComplete != SC_DoNotCheck) { 48 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 49 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 50 << Code << "\n\n"; 51 } 52 ReplacementCount = Replaces.size(); 53 auto Result = applyAllReplacements(Code, Replaces); 54 EXPECT_TRUE(static_cast<bool>(Result)); 55 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 56 return *Result; 57 } 58 59 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 60 Style.ColumnLimit = ColumnLimit; 61 return Style; 62 } 63 64 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 65 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 66 } 67 68 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 69 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 70 } 71 72 void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code, 73 const FormatStyle &Style = getLLVMStyle()) { 74 EXPECT_EQ(Expected.str(), format(Expected, Style)) 75 << "Expected code is not stable"; 76 EXPECT_EQ(Expected.str(), format(Code, Style)); 77 if (Style.Language == FormatStyle::LK_Cpp) { 78 // Objective-C++ is a superset of C++, so everything checked for C++ 79 // needs to be checked for Objective-C++ as well. 80 FormatStyle ObjCStyle = Style; 81 ObjCStyle.Language = FormatStyle::LK_ObjC; 82 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 83 } 84 } 85 86 void verifyFormat(llvm::StringRef Code, 87 const FormatStyle &Style = getLLVMStyle()) { 88 verifyFormat(Code, test::messUp(Code), Style); 89 } 90 91 void verifyIncompleteFormat(llvm::StringRef Code, 92 const FormatStyle &Style = getLLVMStyle()) { 93 EXPECT_EQ(Code.str(), 94 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 95 } 96 97 void verifyGoogleFormat(llvm::StringRef Code) { 98 verifyFormat(Code, getGoogleStyle()); 99 } 100 101 void verifyIndependentOfContext(llvm::StringRef text) { 102 verifyFormat(text); 103 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 104 } 105 106 /// \brief Verify that clang-format does not crash on the given input. 107 void verifyNoCrash(llvm::StringRef Code, 108 const FormatStyle &Style = getLLVMStyle()) { 109 format(Code, Style, SC_DoNotCheck); 110 } 111 112 int ReplacementCount; 113 }; 114 115 TEST_F(FormatTest, MessUp) { 116 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 117 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 118 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 119 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 120 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 121 } 122 123 //===----------------------------------------------------------------------===// 124 // Basic function tests. 125 //===----------------------------------------------------------------------===// 126 127 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 128 EXPECT_EQ(";", format(";")); 129 } 130 131 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 132 EXPECT_EQ("int i;", format(" int i;")); 133 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 134 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 135 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 136 } 137 138 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 139 EXPECT_EQ("int i;", format("int\ni;")); 140 } 141 142 TEST_F(FormatTest, FormatsNestedBlockStatements) { 143 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 144 } 145 146 TEST_F(FormatTest, FormatsNestedCall) { 147 verifyFormat("Method(f1, f2(f3));"); 148 verifyFormat("Method(f1(f2, f3()));"); 149 verifyFormat("Method(f1(f2, (f3())));"); 150 } 151 152 TEST_F(FormatTest, NestedNameSpecifiers) { 153 verifyFormat("vector<::Type> v;"); 154 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 155 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 156 verifyFormat("bool a = 2 < ::SomeFunction();"); 157 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 158 verifyFormat("some::string getName();"); 159 } 160 161 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 162 EXPECT_EQ("if (a) {\n" 163 " f();\n" 164 "}", 165 format("if(a){f();}")); 166 EXPECT_EQ(4, ReplacementCount); 167 EXPECT_EQ("if (a) {\n" 168 " f();\n" 169 "}", 170 format("if (a) {\n" 171 " f();\n" 172 "}")); 173 EXPECT_EQ(0, ReplacementCount); 174 EXPECT_EQ("/*\r\n" 175 "\r\n" 176 "*/\r\n", 177 format("/*\r\n" 178 "\r\n" 179 "*/\r\n")); 180 EXPECT_EQ(0, ReplacementCount); 181 } 182 183 TEST_F(FormatTest, RemovesEmptyLines) { 184 EXPECT_EQ("class C {\n" 185 " int i;\n" 186 "};", 187 format("class C {\n" 188 " int i;\n" 189 "\n" 190 "};")); 191 192 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 193 EXPECT_EQ("namespace N {\n" 194 "\n" 195 "int i;\n" 196 "}", 197 format("namespace N {\n" 198 "\n" 199 "int i;\n" 200 "}", 201 getGoogleStyle())); 202 EXPECT_EQ("/* something */ namespace N {\n" 203 "\n" 204 "int i;\n" 205 "}", 206 format("/* something */ namespace N {\n" 207 "\n" 208 "int i;\n" 209 "}", 210 getGoogleStyle())); 211 EXPECT_EQ("inline namespace N {\n" 212 "\n" 213 "int i;\n" 214 "}", 215 format("inline namespace N {\n" 216 "\n" 217 "int i;\n" 218 "}", 219 getGoogleStyle())); 220 EXPECT_EQ("/* something */ inline namespace N {\n" 221 "\n" 222 "int i;\n" 223 "}", 224 format("/* something */ inline namespace N {\n" 225 "\n" 226 "int i;\n" 227 "}", 228 getGoogleStyle())); 229 EXPECT_EQ("export namespace N {\n" 230 "\n" 231 "int i;\n" 232 "}", 233 format("export namespace N {\n" 234 "\n" 235 "int i;\n" 236 "}", 237 getGoogleStyle())); 238 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 239 "\n" 240 "int i;\n" 241 "}", 242 format("extern /**/ \"C\" /**/ {\n" 243 "\n" 244 "int i;\n" 245 "}", 246 getGoogleStyle())); 247 248 // ...but do keep inlining and removing empty lines for non-block extern "C" 249 // functions. 250 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 251 EXPECT_EQ("extern \"C\" int f() {\n" 252 " int i = 42;\n" 253 " return i;\n" 254 "}", 255 format("extern \"C\" int f() {\n" 256 "\n" 257 " int i = 42;\n" 258 " return i;\n" 259 "}", 260 getGoogleStyle())); 261 262 // Remove empty lines at the beginning and end of blocks. 263 EXPECT_EQ("void f() {\n" 264 "\n" 265 " if (a) {\n" 266 "\n" 267 " f();\n" 268 " }\n" 269 "}", 270 format("void f() {\n" 271 "\n" 272 " if (a) {\n" 273 "\n" 274 " f();\n" 275 "\n" 276 " }\n" 277 "\n" 278 "}", 279 getLLVMStyle())); 280 EXPECT_EQ("void f() {\n" 281 " if (a) {\n" 282 " f();\n" 283 " }\n" 284 "}", 285 format("void f() {\n" 286 "\n" 287 " if (a) {\n" 288 "\n" 289 " f();\n" 290 "\n" 291 " }\n" 292 "\n" 293 "}", 294 getGoogleStyle())); 295 296 // Don't remove empty lines in more complex control statements. 297 EXPECT_EQ("void f() {\n" 298 " if (a) {\n" 299 " f();\n" 300 "\n" 301 " } else if (b) {\n" 302 " f();\n" 303 " }\n" 304 "}", 305 format("void f() {\n" 306 " if (a) {\n" 307 " f();\n" 308 "\n" 309 " } else if (b) {\n" 310 " f();\n" 311 "\n" 312 " }\n" 313 "\n" 314 "}")); 315 316 // Don't remove empty lines before namespace endings. 317 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 318 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 319 EXPECT_EQ("namespace {\n" 320 "int i;\n" 321 "\n" 322 "}", 323 format("namespace {\n" 324 "int i;\n" 325 "\n" 326 "}", LLVMWithNoNamespaceFix)); 327 EXPECT_EQ("namespace {\n" 328 "int i;\n" 329 "}", 330 format("namespace {\n" 331 "int i;\n" 332 "}", LLVMWithNoNamespaceFix)); 333 EXPECT_EQ("namespace {\n" 334 "int i;\n" 335 "\n" 336 "};", 337 format("namespace {\n" 338 "int i;\n" 339 "\n" 340 "};", LLVMWithNoNamespaceFix)); 341 EXPECT_EQ("namespace {\n" 342 "int i;\n" 343 "};", 344 format("namespace {\n" 345 "int i;\n" 346 "};", LLVMWithNoNamespaceFix)); 347 EXPECT_EQ("namespace {\n" 348 "int i;\n" 349 "\n" 350 "}", 351 format("namespace {\n" 352 "int i;\n" 353 "\n" 354 "}")); 355 EXPECT_EQ("namespace {\n" 356 "int i;\n" 357 "\n" 358 "} // namespace", 359 format("namespace {\n" 360 "int i;\n" 361 "\n" 362 "} // namespace")); 363 364 FormatStyle Style = getLLVMStyle(); 365 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 366 Style.MaxEmptyLinesToKeep = 2; 367 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 368 Style.BraceWrapping.AfterClass = true; 369 Style.BraceWrapping.AfterFunction = true; 370 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 371 372 EXPECT_EQ("class Foo\n" 373 "{\n" 374 " Foo() {}\n" 375 "\n" 376 " void funk() {}\n" 377 "};", 378 format("class Foo\n" 379 "{\n" 380 " Foo()\n" 381 " {\n" 382 " }\n" 383 "\n" 384 " void funk() {}\n" 385 "};", 386 Style)); 387 } 388 389 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 390 verifyFormat("x = (a) and (b);"); 391 verifyFormat("x = (a) or (b);"); 392 verifyFormat("x = (a) bitand (b);"); 393 verifyFormat("x = (a) bitor (b);"); 394 verifyFormat("x = (a) not_eq (b);"); 395 verifyFormat("x = (a) and_eq (b);"); 396 verifyFormat("x = (a) or_eq (b);"); 397 verifyFormat("x = (a) xor (b);"); 398 } 399 400 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 401 verifyFormat("x = compl(a);"); 402 verifyFormat("x = not(a);"); 403 verifyFormat("x = bitand(a);"); 404 // Unary operator must not be merged with the next identifier 405 verifyFormat("x = compl a;"); 406 verifyFormat("x = not a;"); 407 verifyFormat("x = bitand a;"); 408 } 409 410 //===----------------------------------------------------------------------===// 411 // Tests for control statements. 412 //===----------------------------------------------------------------------===// 413 414 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 415 verifyFormat("if (true)\n f();\ng();"); 416 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 417 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 418 verifyFormat("if constexpr (true)\n" 419 " f();\ng();"); 420 verifyFormat("if constexpr (a)\n" 421 " if constexpr (b)\n" 422 " if constexpr (c)\n" 423 " g();\n" 424 "h();"); 425 verifyFormat("if constexpr (a)\n" 426 " if constexpr (b) {\n" 427 " f();\n" 428 " }\n" 429 "g();"); 430 431 FormatStyle AllowsMergedIf = getLLVMStyle(); 432 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 433 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 434 verifyFormat("if (a)\n" 435 " // comment\n" 436 " f();", 437 AllowsMergedIf); 438 verifyFormat("{\n" 439 " if (a)\n" 440 " label:\n" 441 " f();\n" 442 "}", 443 AllowsMergedIf); 444 verifyFormat("#define A \\\n" 445 " if (a) \\\n" 446 " label: \\\n" 447 " f()", 448 AllowsMergedIf); 449 verifyFormat("if (a)\n" 450 " ;", 451 AllowsMergedIf); 452 verifyFormat("if (a)\n" 453 " if (b) return;", 454 AllowsMergedIf); 455 456 verifyFormat("if (a) // Can't merge this\n" 457 " f();\n", 458 AllowsMergedIf); 459 verifyFormat("if (a) /* still don't merge */\n" 460 " f();", 461 AllowsMergedIf); 462 verifyFormat("if (a) { // Never merge this\n" 463 " f();\n" 464 "}", 465 AllowsMergedIf); 466 verifyFormat("if (a) { /* Never merge this */\n" 467 " f();\n" 468 "}", 469 AllowsMergedIf); 470 471 AllowsMergedIf.ColumnLimit = 14; 472 verifyFormat("if (a) return;", AllowsMergedIf); 473 verifyFormat("if (aaaaaaaaa)\n" 474 " return;", 475 AllowsMergedIf); 476 477 AllowsMergedIf.ColumnLimit = 13; 478 verifyFormat("if (a)\n return;", AllowsMergedIf); 479 } 480 481 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 482 FormatStyle AllowsMergedLoops = getLLVMStyle(); 483 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 484 verifyFormat("while (true) continue;", AllowsMergedLoops); 485 verifyFormat("for (;;) continue;", AllowsMergedLoops); 486 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 487 verifyFormat("while (true)\n" 488 " ;", 489 AllowsMergedLoops); 490 verifyFormat("for (;;)\n" 491 " ;", 492 AllowsMergedLoops); 493 verifyFormat("for (;;)\n" 494 " for (;;) continue;", 495 AllowsMergedLoops); 496 verifyFormat("for (;;) // Can't merge this\n" 497 " continue;", 498 AllowsMergedLoops); 499 verifyFormat("for (;;) /* still don't merge */\n" 500 " continue;", 501 AllowsMergedLoops); 502 } 503 504 TEST_F(FormatTest, FormatShortBracedStatements) { 505 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 506 AllowSimpleBracedStatements.ColumnLimit = 40; 507 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 508 509 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 510 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 511 512 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 513 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 514 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 515 516 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 517 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 518 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 519 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 520 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 521 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 522 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 523 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 524 verifyFormat("if (true) {\n" 525 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 526 "}", 527 AllowSimpleBracedStatements); 528 verifyFormat("if (true) { //\n" 529 " f();\n" 530 "}", 531 AllowSimpleBracedStatements); 532 verifyFormat("if (true) {\n" 533 " f();\n" 534 " f();\n" 535 "}", 536 AllowSimpleBracedStatements); 537 verifyFormat("if (true) {\n" 538 " f();\n" 539 "} else {\n" 540 " f();\n" 541 "}", 542 AllowSimpleBracedStatements); 543 544 verifyFormat("struct A2 {\n" 545 " int X;\n" 546 "};", 547 AllowSimpleBracedStatements); 548 verifyFormat("typedef struct A2 {\n" 549 " int X;\n" 550 "} A2_t;", 551 AllowSimpleBracedStatements); 552 verifyFormat("template <int> struct A2 {\n" 553 " struct B {};\n" 554 "};", 555 AllowSimpleBracedStatements); 556 557 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 558 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 559 verifyFormat("if (true) {\n" 560 " f();\n" 561 "}", 562 AllowSimpleBracedStatements); 563 verifyFormat("if (true) {\n" 564 " f();\n" 565 "} else {\n" 566 " f();\n" 567 "}", 568 AllowSimpleBracedStatements); 569 570 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 571 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 572 verifyFormat("while (true) {\n" 573 " f();\n" 574 "}", 575 AllowSimpleBracedStatements); 576 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 577 verifyFormat("for (;;) {\n" 578 " f();\n" 579 "}", 580 AllowSimpleBracedStatements); 581 582 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 583 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 584 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true; 585 586 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 587 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 588 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 589 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 590 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 591 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 592 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 593 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 594 verifyFormat("if (true)\n" 595 "{\n" 596 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 597 "}", 598 AllowSimpleBracedStatements); 599 verifyFormat("if (true)\n" 600 "{ //\n" 601 " f();\n" 602 "}", 603 AllowSimpleBracedStatements); 604 verifyFormat("if (true)\n" 605 "{\n" 606 " f();\n" 607 " f();\n" 608 "}", 609 AllowSimpleBracedStatements); 610 verifyFormat("if (true)\n" 611 "{\n" 612 " f();\n" 613 "} else\n" 614 "{\n" 615 " f();\n" 616 "}", 617 AllowSimpleBracedStatements); 618 619 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 620 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 621 verifyFormat("if (true)\n" 622 "{\n" 623 " f();\n" 624 "}", 625 AllowSimpleBracedStatements); 626 verifyFormat("if (true)\n" 627 "{\n" 628 " f();\n" 629 "} else\n" 630 "{\n" 631 " f();\n" 632 "}", 633 AllowSimpleBracedStatements); 634 635 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 636 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 637 verifyFormat("while (true)\n" 638 "{\n" 639 " f();\n" 640 "}", 641 AllowSimpleBracedStatements); 642 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 643 verifyFormat("for (;;)\n" 644 "{\n" 645 " f();\n" 646 "}", 647 AllowSimpleBracedStatements); 648 } 649 650 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 651 FormatStyle Style = getLLVMStyleWithColumns(60); 652 Style.AllowShortBlocksOnASingleLine = true; 653 Style.AllowShortIfStatementsOnASingleLine = true; 654 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 655 EXPECT_EQ("#define A \\\n" 656 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 657 " { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n" 658 "X;", 659 format("#define A \\\n" 660 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 661 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 662 " }\n" 663 "X;", 664 Style)); 665 } 666 667 TEST_F(FormatTest, ParseIfElse) { 668 verifyFormat("if (true)\n" 669 " if (true)\n" 670 " if (true)\n" 671 " f();\n" 672 " else\n" 673 " g();\n" 674 " else\n" 675 " h();\n" 676 "else\n" 677 " i();"); 678 verifyFormat("if (true)\n" 679 " if (true)\n" 680 " if (true) {\n" 681 " if (true)\n" 682 " f();\n" 683 " } else {\n" 684 " g();\n" 685 " }\n" 686 " else\n" 687 " h();\n" 688 "else {\n" 689 " i();\n" 690 "}"); 691 verifyFormat("if (true)\n" 692 " if constexpr (true)\n" 693 " if (true) {\n" 694 " if constexpr (true)\n" 695 " f();\n" 696 " } else {\n" 697 " g();\n" 698 " }\n" 699 " else\n" 700 " h();\n" 701 "else {\n" 702 " i();\n" 703 "}"); 704 verifyFormat("void f() {\n" 705 " if (a) {\n" 706 " } else {\n" 707 " }\n" 708 "}"); 709 } 710 711 TEST_F(FormatTest, ElseIf) { 712 verifyFormat("if (a) {\n} else if (b) {\n}"); 713 verifyFormat("if (a)\n" 714 " f();\n" 715 "else if (b)\n" 716 " g();\n" 717 "else\n" 718 " h();"); 719 verifyFormat("if constexpr (a)\n" 720 " f();\n" 721 "else if constexpr (b)\n" 722 " g();\n" 723 "else\n" 724 " h();"); 725 verifyFormat("if (a) {\n" 726 " f();\n" 727 "}\n" 728 "// or else ..\n" 729 "else {\n" 730 " g()\n" 731 "}"); 732 733 verifyFormat("if (a) {\n" 734 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 735 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 736 "}"); 737 verifyFormat("if (a) {\n" 738 "} else if (\n" 739 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 740 "}", 741 getLLVMStyleWithColumns(62)); 742 verifyFormat("if (a) {\n" 743 "} else if constexpr (\n" 744 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 745 "}", 746 getLLVMStyleWithColumns(62)); 747 } 748 749 TEST_F(FormatTest, FormatsForLoop) { 750 verifyFormat( 751 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 752 " ++VeryVeryLongLoopVariable)\n" 753 " ;"); 754 verifyFormat("for (;;)\n" 755 " f();"); 756 verifyFormat("for (;;) {\n}"); 757 verifyFormat("for (;;) {\n" 758 " f();\n" 759 "}"); 760 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 761 762 verifyFormat( 763 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 764 " E = UnwrappedLines.end();\n" 765 " I != E; ++I) {\n}"); 766 767 verifyFormat( 768 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 769 " ++IIIII) {\n}"); 770 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 771 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 772 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 773 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 774 " I = FD->getDeclsInPrototypeScope().begin(),\n" 775 " E = FD->getDeclsInPrototypeScope().end();\n" 776 " I != E; ++I) {\n}"); 777 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 778 " I = Container.begin(),\n" 779 " E = Container.end();\n" 780 " I != E; ++I) {\n}", 781 getLLVMStyleWithColumns(76)); 782 783 verifyFormat( 784 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 785 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 786 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 787 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 788 " ++aaaaaaaaaaa) {\n}"); 789 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 790 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 791 " ++i) {\n}"); 792 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 793 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 794 "}"); 795 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 796 " aaaaaaaaaa);\n" 797 " iter; ++iter) {\n" 798 "}"); 799 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 801 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 802 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 803 804 // These should not be formatted as Objective-C for-in loops. 805 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 806 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 807 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 808 verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 809 810 FormatStyle NoBinPacking = getLLVMStyle(); 811 NoBinPacking.BinPackParameters = false; 812 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 813 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 814 " aaaaaaaaaaaaaaaa,\n" 815 " aaaaaaaaaaaaaaaa,\n" 816 " aaaaaaaaaaaaaaaa);\n" 817 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 818 "}", 819 NoBinPacking); 820 verifyFormat( 821 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 822 " E = UnwrappedLines.end();\n" 823 " I != E;\n" 824 " ++I) {\n}", 825 NoBinPacking); 826 827 FormatStyle AlignLeft = getLLVMStyle(); 828 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 829 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 830 } 831 832 TEST_F(FormatTest, RangeBasedForLoops) { 833 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 834 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 835 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 836 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 837 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 838 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 839 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 840 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 841 } 842 843 TEST_F(FormatTest, ForEachLoops) { 844 verifyFormat("void f() {\n" 845 " foreach (Item *item, itemlist) {}\n" 846 " Q_FOREACH (Item *item, itemlist) {}\n" 847 " BOOST_FOREACH (Item *item, itemlist) {}\n" 848 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 849 "}"); 850 851 // As function-like macros. 852 verifyFormat("#define foreach(x, y)\n" 853 "#define Q_FOREACH(x, y)\n" 854 "#define BOOST_FOREACH(x, y)\n" 855 "#define UNKNOWN_FOREACH(x, y)\n"); 856 857 // Not as function-like macros. 858 verifyFormat("#define foreach (x, y)\n" 859 "#define Q_FOREACH (x, y)\n" 860 "#define BOOST_FOREACH (x, y)\n" 861 "#define UNKNOWN_FOREACH (x, y)\n"); 862 } 863 864 TEST_F(FormatTest, FormatsWhileLoop) { 865 verifyFormat("while (true) {\n}"); 866 verifyFormat("while (true)\n" 867 " f();"); 868 verifyFormat("while () {\n}"); 869 verifyFormat("while () {\n" 870 " f();\n" 871 "}"); 872 } 873 874 TEST_F(FormatTest, FormatsDoWhile) { 875 verifyFormat("do {\n" 876 " do_something();\n" 877 "} while (something());"); 878 verifyFormat("do\n" 879 " do_something();\n" 880 "while (something());"); 881 } 882 883 TEST_F(FormatTest, FormatsSwitchStatement) { 884 verifyFormat("switch (x) {\n" 885 "case 1:\n" 886 " f();\n" 887 " break;\n" 888 "case kFoo:\n" 889 "case ns::kBar:\n" 890 "case kBaz:\n" 891 " break;\n" 892 "default:\n" 893 " g();\n" 894 " break;\n" 895 "}"); 896 verifyFormat("switch (x) {\n" 897 "case 1: {\n" 898 " f();\n" 899 " break;\n" 900 "}\n" 901 "case 2: {\n" 902 " break;\n" 903 "}\n" 904 "}"); 905 verifyFormat("switch (x) {\n" 906 "case 1: {\n" 907 " f();\n" 908 " {\n" 909 " g();\n" 910 " h();\n" 911 " }\n" 912 " break;\n" 913 "}\n" 914 "}"); 915 verifyFormat("switch (x) {\n" 916 "case 1: {\n" 917 " f();\n" 918 " if (foo) {\n" 919 " g();\n" 920 " h();\n" 921 " }\n" 922 " break;\n" 923 "}\n" 924 "}"); 925 verifyFormat("switch (x) {\n" 926 "case 1: {\n" 927 " f();\n" 928 " g();\n" 929 "} break;\n" 930 "}"); 931 verifyFormat("switch (test)\n" 932 " ;"); 933 verifyFormat("switch (x) {\n" 934 "default: {\n" 935 " // Do nothing.\n" 936 "}\n" 937 "}"); 938 verifyFormat("switch (x) {\n" 939 "// comment\n" 940 "// if 1, do f()\n" 941 "case 1:\n" 942 " f();\n" 943 "}"); 944 verifyFormat("switch (x) {\n" 945 "case 1:\n" 946 " // Do amazing stuff\n" 947 " {\n" 948 " f();\n" 949 " g();\n" 950 " }\n" 951 " break;\n" 952 "}"); 953 verifyFormat("#define A \\\n" 954 " switch (x) { \\\n" 955 " case a: \\\n" 956 " foo = b; \\\n" 957 " }", 958 getLLVMStyleWithColumns(20)); 959 verifyFormat("#define OPERATION_CASE(name) \\\n" 960 " case OP_name: \\\n" 961 " return operations::Operation##name\n", 962 getLLVMStyleWithColumns(40)); 963 verifyFormat("switch (x) {\n" 964 "case 1:;\n" 965 "default:;\n" 966 " int i;\n" 967 "}"); 968 969 verifyGoogleFormat("switch (x) {\n" 970 " case 1:\n" 971 " f();\n" 972 " break;\n" 973 " case kFoo:\n" 974 " case ns::kBar:\n" 975 " case kBaz:\n" 976 " break;\n" 977 " default:\n" 978 " g();\n" 979 " break;\n" 980 "}"); 981 verifyGoogleFormat("switch (x) {\n" 982 " case 1: {\n" 983 " f();\n" 984 " break;\n" 985 " }\n" 986 "}"); 987 verifyGoogleFormat("switch (test)\n" 988 " ;"); 989 990 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 991 " case OP_name: \\\n" 992 " return operations::Operation##name\n"); 993 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 994 " // Get the correction operation class.\n" 995 " switch (OpCode) {\n" 996 " CASE(Add);\n" 997 " CASE(Subtract);\n" 998 " default:\n" 999 " return operations::Unknown;\n" 1000 " }\n" 1001 "#undef OPERATION_CASE\n" 1002 "}"); 1003 verifyFormat("DEBUG({\n" 1004 " switch (x) {\n" 1005 " case A:\n" 1006 " f();\n" 1007 " break;\n" 1008 " // fallthrough\n" 1009 " case B:\n" 1010 " g();\n" 1011 " break;\n" 1012 " }\n" 1013 "});"); 1014 EXPECT_EQ("DEBUG({\n" 1015 " switch (x) {\n" 1016 " case A:\n" 1017 " f();\n" 1018 " break;\n" 1019 " // On B:\n" 1020 " case B:\n" 1021 " g();\n" 1022 " break;\n" 1023 " }\n" 1024 "});", 1025 format("DEBUG({\n" 1026 " switch (x) {\n" 1027 " case A:\n" 1028 " f();\n" 1029 " break;\n" 1030 " // On B:\n" 1031 " case B:\n" 1032 " g();\n" 1033 " break;\n" 1034 " }\n" 1035 "});", 1036 getLLVMStyle())); 1037 EXPECT_EQ("switch (n) {\n" 1038 "case 0: {\n" 1039 " return false;\n" 1040 "}\n" 1041 "default: {\n" 1042 " return true;\n" 1043 "}\n" 1044 "}", 1045 format("switch (n)\n" 1046 "{\n" 1047 "case 0: {\n" 1048 " return false;\n" 1049 "}\n" 1050 "default: {\n" 1051 " return true;\n" 1052 "}\n" 1053 "}", 1054 getLLVMStyle())); 1055 verifyFormat("switch (a) {\n" 1056 "case (b):\n" 1057 " return;\n" 1058 "}"); 1059 1060 verifyFormat("switch (a) {\n" 1061 "case some_namespace::\n" 1062 " some_constant:\n" 1063 " return;\n" 1064 "}", 1065 getLLVMStyleWithColumns(34)); 1066 1067 FormatStyle Style = getLLVMStyle(); 1068 Style.IndentCaseLabels = true; 1069 Style.AllowShortBlocksOnASingleLine = false; 1070 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1071 Style.BraceWrapping.AfterControlStatement = true; 1072 EXPECT_EQ("switch (n)\n" 1073 "{\n" 1074 " case 0:\n" 1075 " {\n" 1076 " return false;\n" 1077 " }\n" 1078 " default:\n" 1079 " {\n" 1080 " return true;\n" 1081 " }\n" 1082 "}", 1083 format("switch (n) {\n" 1084 " case 0: {\n" 1085 " return false;\n" 1086 " }\n" 1087 " default: {\n" 1088 " return true;\n" 1089 " }\n" 1090 "}", 1091 Style)); 1092 } 1093 1094 TEST_F(FormatTest, CaseRanges) { 1095 verifyFormat("switch (x) {\n" 1096 "case 'A' ... 'Z':\n" 1097 "case 1 ... 5:\n" 1098 "case a ... b:\n" 1099 " break;\n" 1100 "}"); 1101 } 1102 1103 TEST_F(FormatTest, ShortCaseLabels) { 1104 FormatStyle Style = getLLVMStyle(); 1105 Style.AllowShortCaseLabelsOnASingleLine = true; 1106 verifyFormat("switch (a) {\n" 1107 "case 1: x = 1; break;\n" 1108 "case 2: return;\n" 1109 "case 3:\n" 1110 "case 4:\n" 1111 "case 5: return;\n" 1112 "case 6: // comment\n" 1113 " return;\n" 1114 "case 7:\n" 1115 " // comment\n" 1116 " return;\n" 1117 "case 8:\n" 1118 " x = 8; // comment\n" 1119 " break;\n" 1120 "default: y = 1; break;\n" 1121 "}", 1122 Style); 1123 verifyFormat("switch (a) {\n" 1124 "case 0: return; // comment\n" 1125 "case 1: break; // comment\n" 1126 "case 2: return;\n" 1127 "// comment\n" 1128 "case 3: return;\n" 1129 "// comment 1\n" 1130 "// comment 2\n" 1131 "// comment 3\n" 1132 "case 4: break; /* comment */\n" 1133 "case 5:\n" 1134 " // comment\n" 1135 " break;\n" 1136 "case 6: /* comment */ x = 1; break;\n" 1137 "case 7: x = /* comment */ 1; break;\n" 1138 "case 8:\n" 1139 " x = 1; /* comment */\n" 1140 " break;\n" 1141 "case 9:\n" 1142 " break; // comment line 1\n" 1143 " // comment line 2\n" 1144 "}", 1145 Style); 1146 EXPECT_EQ("switch (a) {\n" 1147 "case 1:\n" 1148 " x = 8;\n" 1149 " // fall through\n" 1150 "case 2: x = 8;\n" 1151 "// comment\n" 1152 "case 3:\n" 1153 " return; /* comment line 1\n" 1154 " * comment line 2 */\n" 1155 "case 4: i = 8;\n" 1156 "// something else\n" 1157 "#if FOO\n" 1158 "case 5: break;\n" 1159 "#endif\n" 1160 "}", 1161 format("switch (a) {\n" 1162 "case 1: x = 8;\n" 1163 " // fall through\n" 1164 "case 2:\n" 1165 " x = 8;\n" 1166 "// comment\n" 1167 "case 3:\n" 1168 " return; /* comment line 1\n" 1169 " * comment line 2 */\n" 1170 "case 4:\n" 1171 " i = 8;\n" 1172 "// something else\n" 1173 "#if FOO\n" 1174 "case 5: break;\n" 1175 "#endif\n" 1176 "}", 1177 Style)); 1178 EXPECT_EQ("switch (a) {\n" "case 0:\n" 1179 " return; // long long long long long long long long long long long long comment\n" 1180 " // line\n" "}", 1181 format("switch (a) {\n" 1182 "case 0: return; // long long long long long long long long long long long long comment line\n" 1183 "}", 1184 Style)); 1185 EXPECT_EQ("switch (a) {\n" 1186 "case 0:\n" 1187 " return; /* long long long long long long long long long long long long comment\n" 1188 " line */\n" 1189 "}", 1190 format("switch (a) {\n" 1191 "case 0: return; /* long long long long long long long long long long long long comment line */\n" 1192 "}", 1193 Style)); 1194 verifyFormat("switch (a) {\n" 1195 "#if FOO\n" 1196 "case 0: return 0;\n" 1197 "#endif\n" 1198 "}", 1199 Style); 1200 verifyFormat("switch (a) {\n" 1201 "case 1: {\n" 1202 "}\n" 1203 "case 2: {\n" 1204 " return;\n" 1205 "}\n" 1206 "case 3: {\n" 1207 " x = 1;\n" 1208 " return;\n" 1209 "}\n" 1210 "case 4:\n" 1211 " if (x)\n" 1212 " return;\n" 1213 "}", 1214 Style); 1215 Style.ColumnLimit = 21; 1216 verifyFormat("switch (a) {\n" 1217 "case 1: x = 1; break;\n" 1218 "case 2: return;\n" 1219 "case 3:\n" 1220 "case 4:\n" 1221 "case 5: return;\n" 1222 "default:\n" 1223 " y = 1;\n" 1224 " break;\n" 1225 "}", 1226 Style); 1227 Style.ColumnLimit = 80; 1228 Style.AllowShortCaseLabelsOnASingleLine = false; 1229 Style.IndentCaseLabels = true; 1230 EXPECT_EQ("switch (n) {\n" 1231 " default /*comments*/:\n" 1232 " return true;\n" 1233 " case 0:\n" 1234 " return false;\n" 1235 "}", 1236 format("switch (n) {\n" 1237 "default/*comments*/:\n" 1238 " return true;\n" 1239 "case 0:\n" 1240 " return false;\n" 1241 "}", 1242 Style)); 1243 Style.AllowShortCaseLabelsOnASingleLine = true; 1244 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1245 Style.BraceWrapping.AfterControlStatement = true; 1246 EXPECT_EQ("switch (n)\n" 1247 "{\n" 1248 " case 0:\n" 1249 " {\n" 1250 " return false;\n" 1251 " }\n" 1252 " default:\n" 1253 " {\n" 1254 " return true;\n" 1255 " }\n" 1256 "}", 1257 format("switch (n) {\n" 1258 " case 0: {\n" 1259 " return false;\n" 1260 " }\n" 1261 " default:\n" 1262 " {\n" 1263 " return true;\n" 1264 " }\n" 1265 "}", 1266 Style)); 1267 } 1268 1269 TEST_F(FormatTest, FormatsLabels) { 1270 verifyFormat("void f() {\n" 1271 " some_code();\n" 1272 "test_label:\n" 1273 " some_other_code();\n" 1274 " {\n" 1275 " some_more_code();\n" 1276 " another_label:\n" 1277 " some_more_code();\n" 1278 " }\n" 1279 "}"); 1280 verifyFormat("{\n" 1281 " some_code();\n" 1282 "test_label:\n" 1283 " some_other_code();\n" 1284 "}"); 1285 verifyFormat("{\n" 1286 " some_code();\n" 1287 "test_label:;\n" 1288 " int i = 0;\n" 1289 "}"); 1290 } 1291 1292 //===----------------------------------------------------------------------===// 1293 // Tests for classes, namespaces, etc. 1294 //===----------------------------------------------------------------------===// 1295 1296 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1297 verifyFormat("class A {};"); 1298 } 1299 1300 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1301 verifyFormat("class A {\n" 1302 "public:\n" 1303 "public: // comment\n" 1304 "protected:\n" 1305 "private:\n" 1306 " void f() {}\n" 1307 "};"); 1308 verifyFormat("export class A {\n" 1309 "public:\n" 1310 "public: // comment\n" 1311 "protected:\n" 1312 "private:\n" 1313 " void f() {}\n" 1314 "};"); 1315 verifyGoogleFormat("class A {\n" 1316 " public:\n" 1317 " protected:\n" 1318 " private:\n" 1319 " void f() {}\n" 1320 "};"); 1321 verifyGoogleFormat("export class A {\n" 1322 " public:\n" 1323 " protected:\n" 1324 " private:\n" 1325 " void f() {}\n" 1326 "};"); 1327 verifyFormat("class A {\n" 1328 "public slots:\n" 1329 " void f1() {}\n" 1330 "public Q_SLOTS:\n" 1331 " void f2() {}\n" 1332 "protected slots:\n" 1333 " void f3() {}\n" 1334 "protected Q_SLOTS:\n" 1335 " void f4() {}\n" 1336 "private slots:\n" 1337 " void f5() {}\n" 1338 "private Q_SLOTS:\n" 1339 " void f6() {}\n" 1340 "signals:\n" 1341 " void g1();\n" 1342 "Q_SIGNALS:\n" 1343 " void g2();\n" 1344 "};"); 1345 1346 // Don't interpret 'signals' the wrong way. 1347 verifyFormat("signals.set();"); 1348 verifyFormat("for (Signals signals : f()) {\n}"); 1349 verifyFormat("{\n" 1350 " signals.set(); // This needs indentation.\n" 1351 "}"); 1352 verifyFormat("void f() {\n" 1353 "label:\n" 1354 " signals.baz();\n" 1355 "}"); 1356 } 1357 1358 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1359 EXPECT_EQ("class A {\n" 1360 "public:\n" 1361 " void f();\n" 1362 "\n" 1363 "private:\n" 1364 " void g() {}\n" 1365 " // test\n" 1366 "protected:\n" 1367 " int h;\n" 1368 "};", 1369 format("class A {\n" 1370 "public:\n" 1371 "void f();\n" 1372 "private:\n" 1373 "void g() {}\n" 1374 "// test\n" 1375 "protected:\n" 1376 "int h;\n" 1377 "};")); 1378 EXPECT_EQ("class A {\n" 1379 "protected:\n" 1380 "public:\n" 1381 " void f();\n" 1382 "};", 1383 format("class A {\n" 1384 "protected:\n" 1385 "\n" 1386 "public:\n" 1387 "\n" 1388 " void f();\n" 1389 "};")); 1390 1391 // Even ensure proper spacing inside macros. 1392 EXPECT_EQ("#define B \\\n" 1393 " class A { \\\n" 1394 " protected: \\\n" 1395 " public: \\\n" 1396 " void f(); \\\n" 1397 " };", 1398 format("#define B \\\n" 1399 " class A { \\\n" 1400 " protected: \\\n" 1401 " \\\n" 1402 " public: \\\n" 1403 " \\\n" 1404 " void f(); \\\n" 1405 " };", 1406 getGoogleStyle())); 1407 // But don't remove empty lines after macros ending in access specifiers. 1408 EXPECT_EQ("#define A private:\n" 1409 "\n" 1410 "int i;", 1411 format("#define A private:\n" 1412 "\n" 1413 "int i;")); 1414 } 1415 1416 TEST_F(FormatTest, FormatsClasses) { 1417 verifyFormat("class A : public B {};"); 1418 verifyFormat("class A : public ::B {};"); 1419 1420 verifyFormat( 1421 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1422 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1423 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1424 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1425 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1426 verifyFormat( 1427 "class A : public B, public C, public D, public E, public F {};"); 1428 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1429 " public C,\n" 1430 " public D,\n" 1431 " public E,\n" 1432 " public F,\n" 1433 " public G {};"); 1434 1435 verifyFormat("class\n" 1436 " ReallyReallyLongClassName {\n" 1437 " int i;\n" 1438 "};", 1439 getLLVMStyleWithColumns(32)); 1440 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1441 " aaaaaaaaaaaaaaaa> {};"); 1442 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1443 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1444 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1445 verifyFormat("template <class R, class C>\n" 1446 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1447 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1448 verifyFormat("class ::A::B {};"); 1449 } 1450 1451 TEST_F(FormatTest, BreakInheritanceStyle) { 1452 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 1453 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 1454 FormatStyle::BILS_BeforeComma; 1455 verifyFormat("class MyClass : public X {};", 1456 StyleWithInheritanceBreakBeforeComma); 1457 verifyFormat("class MyClass\n" 1458 " : public X\n" 1459 " , public Y {};", 1460 StyleWithInheritanceBreakBeforeComma); 1461 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 1462 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 1463 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 1464 StyleWithInheritanceBreakBeforeComma); 1465 verifyFormat("struct aaaaaaaaaaaaa\n" 1466 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 1467 " aaaaaaaaaaaaaaaa> {};", 1468 StyleWithInheritanceBreakBeforeComma); 1469 1470 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 1471 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 1472 FormatStyle::BILS_AfterColon; 1473 verifyFormat("class MyClass : public X {};", 1474 StyleWithInheritanceBreakAfterColon); 1475 verifyFormat("class MyClass : public X, public Y {};", 1476 StyleWithInheritanceBreakAfterColon); 1477 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 1478 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1479 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 1480 StyleWithInheritanceBreakAfterColon); 1481 verifyFormat("struct aaaaaaaaaaaaa :\n" 1482 " public aaaaaaaaaaaaaaaaaaa< // break\n" 1483 " aaaaaaaaaaaaaaaa> {};", 1484 StyleWithInheritanceBreakAfterColon); 1485 } 1486 1487 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1488 verifyFormat("class A {\n} a, b;"); 1489 verifyFormat("struct A {\n} a, b;"); 1490 verifyFormat("union A {\n} a;"); 1491 } 1492 1493 TEST_F(FormatTest, FormatsEnum) { 1494 verifyFormat("enum {\n" 1495 " Zero,\n" 1496 " One = 1,\n" 1497 " Two = One + 1,\n" 1498 " Three = (One + Two),\n" 1499 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1500 " Five = (One, Two, Three, Four, 5)\n" 1501 "};"); 1502 verifyGoogleFormat("enum {\n" 1503 " Zero,\n" 1504 " One = 1,\n" 1505 " Two = One + 1,\n" 1506 " Three = (One + Two),\n" 1507 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1508 " Five = (One, Two, Three, Four, 5)\n" 1509 "};"); 1510 verifyFormat("enum Enum {};"); 1511 verifyFormat("enum {};"); 1512 verifyFormat("enum X E {} d;"); 1513 verifyFormat("enum __attribute__((...)) E {} d;"); 1514 verifyFormat("enum __declspec__((...)) E {} d;"); 1515 verifyFormat("enum {\n" 1516 " Bar = Foo<int, int>::value\n" 1517 "};", 1518 getLLVMStyleWithColumns(30)); 1519 1520 verifyFormat("enum ShortEnum { A, B, C };"); 1521 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1522 1523 EXPECT_EQ("enum KeepEmptyLines {\n" 1524 " ONE,\n" 1525 "\n" 1526 " TWO,\n" 1527 "\n" 1528 " THREE\n" 1529 "}", 1530 format("enum KeepEmptyLines {\n" 1531 " ONE,\n" 1532 "\n" 1533 " TWO,\n" 1534 "\n" 1535 "\n" 1536 " THREE\n" 1537 "}")); 1538 verifyFormat("enum E { // comment\n" 1539 " ONE,\n" 1540 " TWO\n" 1541 "};\n" 1542 "int i;"); 1543 // Not enums. 1544 verifyFormat("enum X f() {\n" 1545 " a();\n" 1546 " return 42;\n" 1547 "}"); 1548 verifyFormat("enum X Type::f() {\n" 1549 " a();\n" 1550 " return 42;\n" 1551 "}"); 1552 verifyFormat("enum ::X f() {\n" 1553 " a();\n" 1554 " return 42;\n" 1555 "}"); 1556 verifyFormat("enum ns::X f() {\n" 1557 " a();\n" 1558 " return 42;\n" 1559 "}"); 1560 } 1561 1562 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1563 verifyFormat("enum Type {\n" 1564 " One = 0; // These semicolons should be commas.\n" 1565 " Two = 1;\n" 1566 "};"); 1567 verifyFormat("namespace n {\n" 1568 "enum Type {\n" 1569 " One,\n" 1570 " Two, // missing };\n" 1571 " int i;\n" 1572 "}\n" 1573 "void g() {}"); 1574 } 1575 1576 TEST_F(FormatTest, FormatsEnumStruct) { 1577 verifyFormat("enum struct {\n" 1578 " Zero,\n" 1579 " One = 1,\n" 1580 " Two = One + 1,\n" 1581 " Three = (One + Two),\n" 1582 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1583 " Five = (One, Two, Three, Four, 5)\n" 1584 "};"); 1585 verifyFormat("enum struct Enum {};"); 1586 verifyFormat("enum struct {};"); 1587 verifyFormat("enum struct X E {} d;"); 1588 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1589 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1590 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1591 } 1592 1593 TEST_F(FormatTest, FormatsEnumClass) { 1594 verifyFormat("enum class {\n" 1595 " Zero,\n" 1596 " One = 1,\n" 1597 " Two = One + 1,\n" 1598 " Three = (One + Two),\n" 1599 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1600 " Five = (One, Two, Three, Four, 5)\n" 1601 "};"); 1602 verifyFormat("enum class Enum {};"); 1603 verifyFormat("enum class {};"); 1604 verifyFormat("enum class X E {} d;"); 1605 verifyFormat("enum class __attribute__((...)) E {} d;"); 1606 verifyFormat("enum class __declspec__((...)) E {} d;"); 1607 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1608 } 1609 1610 TEST_F(FormatTest, FormatsEnumTypes) { 1611 verifyFormat("enum X : int {\n" 1612 " A, // Force multiple lines.\n" 1613 " B\n" 1614 "};"); 1615 verifyFormat("enum X : int { A, B };"); 1616 verifyFormat("enum X : std::uint32_t { A, B };"); 1617 } 1618 1619 TEST_F(FormatTest, FormatsTypedefEnum) { 1620 FormatStyle Style = getLLVMStyle(); 1621 Style.ColumnLimit = 40; 1622 verifyFormat("typedef enum {} EmptyEnum;"); 1623 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1624 verifyFormat("typedef enum {\n" 1625 " ZERO = 0,\n" 1626 " ONE = 1,\n" 1627 " TWO = 2,\n" 1628 " THREE = 3\n" 1629 "} LongEnum;", 1630 Style); 1631 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1632 Style.BraceWrapping.AfterEnum = true; 1633 verifyFormat("typedef enum {} EmptyEnum;"); 1634 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1635 verifyFormat("typedef enum\n" 1636 "{\n" 1637 " ZERO = 0,\n" 1638 " ONE = 1,\n" 1639 " TWO = 2,\n" 1640 " THREE = 3\n" 1641 "} LongEnum;", 1642 Style); 1643 } 1644 1645 TEST_F(FormatTest, FormatsNSEnums) { 1646 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1647 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1648 " // Information about someDecentlyLongValue.\n" 1649 " someDecentlyLongValue,\n" 1650 " // Information about anotherDecentlyLongValue.\n" 1651 " anotherDecentlyLongValue,\n" 1652 " // Information about aThirdDecentlyLongValue.\n" 1653 " aThirdDecentlyLongValue\n" 1654 "};"); 1655 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1656 " a = 1,\n" 1657 " b = 2,\n" 1658 " c = 3,\n" 1659 "};"); 1660 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1661 " a = 1,\n" 1662 " b = 2,\n" 1663 " c = 3,\n" 1664 "};"); 1665 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1666 " a = 1,\n" 1667 " b = 2,\n" 1668 " c = 3,\n" 1669 "};"); 1670 } 1671 1672 TEST_F(FormatTest, FormatsBitfields) { 1673 verifyFormat("struct Bitfields {\n" 1674 " unsigned sClass : 8;\n" 1675 " unsigned ValueKind : 2;\n" 1676 "};"); 1677 verifyFormat("struct A {\n" 1678 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1679 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1680 "};"); 1681 verifyFormat("struct MyStruct {\n" 1682 " uchar data;\n" 1683 " uchar : 8;\n" 1684 " uchar : 8;\n" 1685 " uchar other;\n" 1686 "};"); 1687 } 1688 1689 TEST_F(FormatTest, FormatsNamespaces) { 1690 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1691 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1692 1693 verifyFormat("namespace some_namespace {\n" 1694 "class A {};\n" 1695 "void f() { f(); }\n" 1696 "}", 1697 LLVMWithNoNamespaceFix); 1698 verifyFormat("/* something */ namespace some_namespace {\n" 1699 "class A {};\n" 1700 "void f() { f(); }\n" 1701 "}", 1702 LLVMWithNoNamespaceFix); 1703 verifyFormat("namespace {\n" 1704 "class A {};\n" 1705 "void f() { f(); }\n" 1706 "}", 1707 LLVMWithNoNamespaceFix); 1708 verifyFormat("/* something */ namespace {\n" 1709 "class A {};\n" 1710 "void f() { f(); }\n" 1711 "}", 1712 LLVMWithNoNamespaceFix); 1713 verifyFormat("inline namespace X {\n" 1714 "class A {};\n" 1715 "void f() { f(); }\n" 1716 "}", 1717 LLVMWithNoNamespaceFix); 1718 verifyFormat("/* something */ inline namespace X {\n" 1719 "class A {};\n" 1720 "void f() { f(); }\n" 1721 "}", 1722 LLVMWithNoNamespaceFix); 1723 verifyFormat("export namespace X {\n" 1724 "class A {};\n" 1725 "void f() { f(); }\n" 1726 "}", 1727 LLVMWithNoNamespaceFix); 1728 verifyFormat("using namespace some_namespace;\n" 1729 "class A {};\n" 1730 "void f() { f(); }", 1731 LLVMWithNoNamespaceFix); 1732 1733 // This code is more common than we thought; if we 1734 // layout this correctly the semicolon will go into 1735 // its own line, which is undesirable. 1736 verifyFormat("namespace {};", 1737 LLVMWithNoNamespaceFix); 1738 verifyFormat("namespace {\n" 1739 "class A {};\n" 1740 "};", 1741 LLVMWithNoNamespaceFix); 1742 1743 verifyFormat("namespace {\n" 1744 "int SomeVariable = 0; // comment\n" 1745 "} // namespace", 1746 LLVMWithNoNamespaceFix); 1747 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1748 "#define HEADER_GUARD\n" 1749 "namespace my_namespace {\n" 1750 "int i;\n" 1751 "} // my_namespace\n" 1752 "#endif // HEADER_GUARD", 1753 format("#ifndef HEADER_GUARD\n" 1754 " #define HEADER_GUARD\n" 1755 " namespace my_namespace {\n" 1756 "int i;\n" 1757 "} // my_namespace\n" 1758 "#endif // HEADER_GUARD", 1759 LLVMWithNoNamespaceFix)); 1760 1761 EXPECT_EQ("namespace A::B {\n" 1762 "class C {};\n" 1763 "}", 1764 format("namespace A::B {\n" 1765 "class C {};\n" 1766 "}", 1767 LLVMWithNoNamespaceFix)); 1768 1769 FormatStyle Style = getLLVMStyle(); 1770 Style.NamespaceIndentation = FormatStyle::NI_All; 1771 EXPECT_EQ("namespace out {\n" 1772 " int i;\n" 1773 " namespace in {\n" 1774 " int i;\n" 1775 " } // namespace in\n" 1776 "} // namespace out", 1777 format("namespace out {\n" 1778 "int i;\n" 1779 "namespace in {\n" 1780 "int i;\n" 1781 "} // namespace in\n" 1782 "} // namespace out", 1783 Style)); 1784 1785 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1786 EXPECT_EQ("namespace out {\n" 1787 "int i;\n" 1788 "namespace in {\n" 1789 " int i;\n" 1790 "} // namespace in\n" 1791 "} // namespace out", 1792 format("namespace out {\n" 1793 "int i;\n" 1794 "namespace in {\n" 1795 "int i;\n" 1796 "} // namespace in\n" 1797 "} // namespace out", 1798 Style)); 1799 } 1800 1801 TEST_F(FormatTest, FormatsCompactNamespaces) { 1802 FormatStyle Style = getLLVMStyle(); 1803 Style.CompactNamespaces = true; 1804 1805 verifyFormat("namespace A { namespace B {\n" 1806 "}} // namespace A::B", 1807 Style); 1808 1809 EXPECT_EQ("namespace out { namespace in {\n" 1810 "}} // namespace out::in", 1811 format("namespace out {\n" 1812 "namespace in {\n" 1813 "} // namespace in\n" 1814 "} // namespace out", 1815 Style)); 1816 1817 // Only namespaces which have both consecutive opening and end get compacted 1818 EXPECT_EQ("namespace out {\n" 1819 "namespace in1 {\n" 1820 "} // namespace in1\n" 1821 "namespace in2 {\n" 1822 "} // namespace in2\n" 1823 "} // namespace out", 1824 format("namespace out {\n" 1825 "namespace in1 {\n" 1826 "} // namespace in1\n" 1827 "namespace in2 {\n" 1828 "} // namespace in2\n" 1829 "} // namespace out", 1830 Style)); 1831 1832 EXPECT_EQ("namespace out {\n" 1833 "int i;\n" 1834 "namespace in {\n" 1835 "int j;\n" 1836 "} // namespace in\n" 1837 "int k;\n" 1838 "} // namespace out", 1839 format("namespace out { int i;\n" 1840 "namespace in { int j; } // namespace in\n" 1841 "int k; } // namespace out", 1842 Style)); 1843 1844 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 1845 "}}} // namespace A::B::C\n", 1846 format("namespace A { namespace B {\n" 1847 "namespace C {\n" 1848 "}} // namespace B::C\n" 1849 "} // namespace A\n", 1850 Style)); 1851 1852 Style.ColumnLimit = 40; 1853 EXPECT_EQ("namespace aaaaaaaaaa {\n" 1854 "namespace bbbbbbbbbb {\n" 1855 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 1856 format("namespace aaaaaaaaaa {\n" 1857 "namespace bbbbbbbbbb {\n" 1858 "} // namespace bbbbbbbbbb\n" 1859 "} // namespace aaaaaaaaaa", 1860 Style)); 1861 1862 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 1863 "namespace cccccc {\n" 1864 "}}} // namespace aaaaaa::bbbbbb::cccccc", 1865 format("namespace aaaaaa {\n" 1866 "namespace bbbbbb {\n" 1867 "namespace cccccc {\n" 1868 "} // namespace cccccc\n" 1869 "} // namespace bbbbbb\n" 1870 "} // namespace aaaaaa", 1871 Style)); 1872 Style.ColumnLimit = 80; 1873 1874 // Extra semicolon after 'inner' closing brace prevents merging 1875 EXPECT_EQ("namespace out { namespace in {\n" 1876 "}; } // namespace out::in", 1877 format("namespace out {\n" 1878 "namespace in {\n" 1879 "}; // namespace in\n" 1880 "} // namespace out", 1881 Style)); 1882 1883 // Extra semicolon after 'outer' closing brace is conserved 1884 EXPECT_EQ("namespace out { namespace in {\n" 1885 "}}; // namespace out::in", 1886 format("namespace out {\n" 1887 "namespace in {\n" 1888 "} // namespace in\n" 1889 "}; // namespace out", 1890 Style)); 1891 1892 Style.NamespaceIndentation = FormatStyle::NI_All; 1893 EXPECT_EQ("namespace out { namespace in {\n" 1894 " int i;\n" 1895 "}} // namespace out::in", 1896 format("namespace out {\n" 1897 "namespace in {\n" 1898 "int i;\n" 1899 "} // namespace in\n" 1900 "} // namespace out", 1901 Style)); 1902 EXPECT_EQ("namespace out { namespace mid {\n" 1903 " namespace in {\n" 1904 " int j;\n" 1905 " } // namespace in\n" 1906 " int k;\n" 1907 "}} // namespace out::mid", 1908 format("namespace out { namespace mid {\n" 1909 "namespace in { int j; } // namespace in\n" 1910 "int k; }} // namespace out::mid", 1911 Style)); 1912 1913 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1914 EXPECT_EQ("namespace out { namespace in {\n" 1915 " int i;\n" 1916 "}} // namespace out::in", 1917 format("namespace out {\n" 1918 "namespace in {\n" 1919 "int i;\n" 1920 "} // namespace in\n" 1921 "} // namespace out", 1922 Style)); 1923 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 1924 " int i;\n" 1925 "}}} // namespace out::mid::in", 1926 format("namespace out {\n" 1927 "namespace mid {\n" 1928 "namespace in {\n" 1929 "int i;\n" 1930 "} // namespace in\n" 1931 "} // namespace mid\n" 1932 "} // namespace out", 1933 Style)); 1934 } 1935 1936 TEST_F(FormatTest, FormatsExternC) { 1937 verifyFormat("extern \"C\" {\nint a;"); 1938 verifyFormat("extern \"C\" {}"); 1939 verifyFormat("extern \"C\" {\n" 1940 "int foo();\n" 1941 "}"); 1942 verifyFormat("extern \"C\" int foo() {}"); 1943 verifyFormat("extern \"C\" int foo();"); 1944 verifyFormat("extern \"C\" int foo() {\n" 1945 " int i = 42;\n" 1946 " return i;\n" 1947 "}"); 1948 1949 FormatStyle Style = getLLVMStyle(); 1950 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1951 Style.BraceWrapping.AfterFunction = true; 1952 verifyFormat("extern \"C\" int foo() {}", Style); 1953 verifyFormat("extern \"C\" int foo();", Style); 1954 verifyFormat("extern \"C\" int foo()\n" 1955 "{\n" 1956 " int i = 42;\n" 1957 " return i;\n" 1958 "}", 1959 Style); 1960 1961 Style.BraceWrapping.AfterExternBlock = true; 1962 Style.BraceWrapping.SplitEmptyRecord = false; 1963 verifyFormat("extern \"C\"\n" 1964 "{}", 1965 Style); 1966 verifyFormat("extern \"C\"\n" 1967 "{\n" 1968 " int foo();\n" 1969 "}", 1970 Style); 1971 } 1972 1973 TEST_F(FormatTest, FormatsInlineASM) { 1974 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 1975 verifyFormat("asm(\"nop\" ::: \"memory\");"); 1976 verifyFormat( 1977 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 1978 " \"cpuid\\n\\t\"\n" 1979 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 1980 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 1981 " : \"a\"(value));"); 1982 EXPECT_EQ( 1983 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1984 " __asm {\n" 1985 " mov edx,[that] // vtable in edx\n" 1986 " mov eax,methodIndex\n" 1987 " call [edx][eax*4] // stdcall\n" 1988 " }\n" 1989 "}", 1990 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1991 " __asm {\n" 1992 " mov edx,[that] // vtable in edx\n" 1993 " mov eax,methodIndex\n" 1994 " call [edx][eax*4] // stdcall\n" 1995 " }\n" 1996 "}")); 1997 EXPECT_EQ("_asm {\n" 1998 " xor eax, eax;\n" 1999 " cpuid;\n" 2000 "}", 2001 format("_asm {\n" 2002 " xor eax, eax;\n" 2003 " cpuid;\n" 2004 "}")); 2005 verifyFormat("void function() {\n" 2006 " // comment\n" 2007 " asm(\"\");\n" 2008 "}"); 2009 EXPECT_EQ("__asm {\n" 2010 "}\n" 2011 "int i;", 2012 format("__asm {\n" 2013 "}\n" 2014 "int i;")); 2015 } 2016 2017 TEST_F(FormatTest, FormatTryCatch) { 2018 verifyFormat("try {\n" 2019 " throw a * b;\n" 2020 "} catch (int a) {\n" 2021 " // Do nothing.\n" 2022 "} catch (...) {\n" 2023 " exit(42);\n" 2024 "}"); 2025 2026 // Function-level try statements. 2027 verifyFormat("int f() try { return 4; } catch (...) {\n" 2028 " return 5;\n" 2029 "}"); 2030 verifyFormat("class A {\n" 2031 " int a;\n" 2032 " A() try : a(0) {\n" 2033 " } catch (...) {\n" 2034 " throw;\n" 2035 " }\n" 2036 "};\n"); 2037 2038 // Incomplete try-catch blocks. 2039 verifyIncompleteFormat("try {} catch ("); 2040 } 2041 2042 TEST_F(FormatTest, FormatSEHTryCatch) { 2043 verifyFormat("__try {\n" 2044 " int a = b * c;\n" 2045 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2046 " // Do nothing.\n" 2047 "}"); 2048 2049 verifyFormat("__try {\n" 2050 " int a = b * c;\n" 2051 "} __finally {\n" 2052 " // Do nothing.\n" 2053 "}"); 2054 2055 verifyFormat("DEBUG({\n" 2056 " __try {\n" 2057 " } __finally {\n" 2058 " }\n" 2059 "});\n"); 2060 } 2061 2062 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2063 verifyFormat("try {\n" 2064 " f();\n" 2065 "} catch {\n" 2066 " g();\n" 2067 "}"); 2068 verifyFormat("try {\n" 2069 " f();\n" 2070 "} catch (A a) MACRO(x) {\n" 2071 " g();\n" 2072 "} catch (B b) MACRO(x) {\n" 2073 " g();\n" 2074 "}"); 2075 } 2076 2077 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2078 FormatStyle Style = getLLVMStyle(); 2079 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 2080 FormatStyle::BS_WebKit}) { 2081 Style.BreakBeforeBraces = BraceStyle; 2082 verifyFormat("try {\n" 2083 " // something\n" 2084 "} catch (...) {\n" 2085 " // something\n" 2086 "}", 2087 Style); 2088 } 2089 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2090 verifyFormat("try {\n" 2091 " // something\n" 2092 "}\n" 2093 "catch (...) {\n" 2094 " // something\n" 2095 "}", 2096 Style); 2097 verifyFormat("__try {\n" 2098 " // something\n" 2099 "}\n" 2100 "__finally {\n" 2101 " // something\n" 2102 "}", 2103 Style); 2104 verifyFormat("@try {\n" 2105 " // something\n" 2106 "}\n" 2107 "@finally {\n" 2108 " // something\n" 2109 "}", 2110 Style); 2111 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2112 verifyFormat("try\n" 2113 "{\n" 2114 " // something\n" 2115 "}\n" 2116 "catch (...)\n" 2117 "{\n" 2118 " // something\n" 2119 "}", 2120 Style); 2121 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2122 verifyFormat("try\n" 2123 " {\n" 2124 " // something\n" 2125 " }\n" 2126 "catch (...)\n" 2127 " {\n" 2128 " // something\n" 2129 " }", 2130 Style); 2131 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2132 Style.BraceWrapping.BeforeCatch = true; 2133 verifyFormat("try {\n" 2134 " // something\n" 2135 "}\n" 2136 "catch (...) {\n" 2137 " // something\n" 2138 "}", 2139 Style); 2140 } 2141 2142 TEST_F(FormatTest, StaticInitializers) { 2143 verifyFormat("static SomeClass SC = {1, 'a'};"); 2144 2145 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2146 " 100000000, " 2147 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2148 2149 // Here, everything other than the "}" would fit on a line. 2150 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2151 " 10000000000000000000000000};"); 2152 EXPECT_EQ("S s = {a,\n" 2153 "\n" 2154 " b};", 2155 format("S s = {\n" 2156 " a,\n" 2157 "\n" 2158 " b\n" 2159 "};")); 2160 2161 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2162 // line. However, the formatting looks a bit off and this probably doesn't 2163 // happen often in practice. 2164 verifyFormat("static int Variable[1] = {\n" 2165 " {1000000000000000000000000000000000000}};", 2166 getLLVMStyleWithColumns(40)); 2167 } 2168 2169 TEST_F(FormatTest, DesignatedInitializers) { 2170 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2171 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2172 " .bbbbbbbbbb = 2,\n" 2173 " .cccccccccc = 3,\n" 2174 " .dddddddddd = 4,\n" 2175 " .eeeeeeeeee = 5};"); 2176 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2177 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2178 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2179 " .ccccccccccccccccccccccccccc = 3,\n" 2180 " .ddddddddddddddddddddddddddd = 4,\n" 2181 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2182 2183 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2184 2185 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 2186 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 2187 " [2] = bbbbbbbbbb,\n" 2188 " [3] = cccccccccc,\n" 2189 " [4] = dddddddddd,\n" 2190 " [5] = eeeeeeeeee};"); 2191 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2192 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2193 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2194 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 2195 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 2196 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 2197 } 2198 2199 TEST_F(FormatTest, NestedStaticInitializers) { 2200 verifyFormat("static A x = {{{}}};\n"); 2201 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2202 " {init1, init2, init3, init4}}};", 2203 getLLVMStyleWithColumns(50)); 2204 2205 verifyFormat("somes Status::global_reps[3] = {\n" 2206 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2207 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2208 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2209 getLLVMStyleWithColumns(60)); 2210 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2211 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2212 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2213 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2214 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2215 " {rect.fRight - rect.fLeft, rect.fBottom - " 2216 "rect.fTop}};"); 2217 2218 verifyFormat( 2219 "SomeArrayOfSomeType a = {\n" 2220 " {{1, 2, 3},\n" 2221 " {1, 2, 3},\n" 2222 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2223 " 333333333333333333333333333333},\n" 2224 " {1, 2, 3},\n" 2225 " {1, 2, 3}}};"); 2226 verifyFormat( 2227 "SomeArrayOfSomeType a = {\n" 2228 " {{1, 2, 3}},\n" 2229 " {{1, 2, 3}},\n" 2230 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2231 " 333333333333333333333333333333}},\n" 2232 " {{1, 2, 3}},\n" 2233 " {{1, 2, 3}}};"); 2234 2235 verifyFormat("struct {\n" 2236 " unsigned bit;\n" 2237 " const char *const name;\n" 2238 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2239 " {kOsWin, \"Windows\"},\n" 2240 " {kOsLinux, \"Linux\"},\n" 2241 " {kOsCrOS, \"Chrome OS\"}};"); 2242 verifyFormat("struct {\n" 2243 " unsigned bit;\n" 2244 " const char *const name;\n" 2245 "} kBitsToOs[] = {\n" 2246 " {kOsMac, \"Mac\"},\n" 2247 " {kOsWin, \"Windows\"},\n" 2248 " {kOsLinux, \"Linux\"},\n" 2249 " {kOsCrOS, \"Chrome OS\"},\n" 2250 "};"); 2251 } 2252 2253 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2254 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2255 " \\\n" 2256 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2257 } 2258 2259 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2260 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2261 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2262 2263 // Do break defaulted and deleted functions. 2264 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2265 " default;", 2266 getLLVMStyleWithColumns(40)); 2267 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2268 " delete;", 2269 getLLVMStyleWithColumns(40)); 2270 } 2271 2272 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2273 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2274 getLLVMStyleWithColumns(40)); 2275 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2276 getLLVMStyleWithColumns(40)); 2277 EXPECT_EQ("#define Q \\\n" 2278 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2279 " \"aaaaaaaa.cpp\"", 2280 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2281 getLLVMStyleWithColumns(40))); 2282 } 2283 2284 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2285 EXPECT_EQ("# 123 \"A string literal\"", 2286 format(" # 123 \"A string literal\"")); 2287 } 2288 2289 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2290 EXPECT_EQ("#;", format("#;")); 2291 verifyFormat("#\n;\n;\n;"); 2292 } 2293 2294 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2295 EXPECT_EQ("#line 42 \"test\"\n", 2296 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2297 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2298 getLLVMStyleWithColumns(12))); 2299 } 2300 2301 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2302 EXPECT_EQ("#line 42 \"test\"", 2303 format("# \\\n line \\\n 42 \\\n \"test\"")); 2304 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2305 } 2306 2307 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2308 verifyFormat("#define A \\x20"); 2309 verifyFormat("#define A \\ x20"); 2310 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2311 verifyFormat("#define A ''"); 2312 verifyFormat("#define A ''qqq"); 2313 verifyFormat("#define A `qqq"); 2314 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2315 EXPECT_EQ("const char *c = STRINGIFY(\n" 2316 "\\na : b);", 2317 format("const char * c = STRINGIFY(\n" 2318 "\\na : b);")); 2319 2320 verifyFormat("a\r\\"); 2321 verifyFormat("a\v\\"); 2322 verifyFormat("a\f\\"); 2323 } 2324 2325 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2326 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2327 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2328 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2329 // FIXME: We never break before the macro name. 2330 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2331 2332 verifyFormat("#define A A\n#define A A"); 2333 verifyFormat("#define A(X) A\n#define A A"); 2334 2335 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2336 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2337 } 2338 2339 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2340 EXPECT_EQ("// somecomment\n" 2341 "#include \"a.h\"\n" 2342 "#define A( \\\n" 2343 " A, B)\n" 2344 "#include \"b.h\"\n" 2345 "// somecomment\n", 2346 format(" // somecomment\n" 2347 " #include \"a.h\"\n" 2348 "#define A(A,\\\n" 2349 " B)\n" 2350 " #include \"b.h\"\n" 2351 " // somecomment\n", 2352 getLLVMStyleWithColumns(13))); 2353 } 2354 2355 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2356 2357 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2358 EXPECT_EQ("#define A \\\n" 2359 " c; \\\n" 2360 " e;\n" 2361 "f;", 2362 format("#define A c; e;\n" 2363 "f;", 2364 getLLVMStyleWithColumns(14))); 2365 } 2366 2367 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2368 2369 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2370 EXPECT_EQ("int x,\n" 2371 "#define A\n" 2372 " y;", 2373 format("int x,\n#define A\ny;")); 2374 } 2375 2376 TEST_F(FormatTest, HashInMacroDefinition) { 2377 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2378 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2379 verifyFormat("#define A \\\n" 2380 " { \\\n" 2381 " f(#c); \\\n" 2382 " }", 2383 getLLVMStyleWithColumns(11)); 2384 2385 verifyFormat("#define A(X) \\\n" 2386 " void function##X()", 2387 getLLVMStyleWithColumns(22)); 2388 2389 verifyFormat("#define A(a, b, c) \\\n" 2390 " void a##b##c()", 2391 getLLVMStyleWithColumns(22)); 2392 2393 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2394 } 2395 2396 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2397 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2398 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2399 } 2400 2401 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2402 EXPECT_EQ("#define A b;", format("#define A \\\n" 2403 " \\\n" 2404 " b;", 2405 getLLVMStyleWithColumns(25))); 2406 EXPECT_EQ("#define A \\\n" 2407 " \\\n" 2408 " a; \\\n" 2409 " b;", 2410 format("#define A \\\n" 2411 " \\\n" 2412 " a; \\\n" 2413 " b;", 2414 getLLVMStyleWithColumns(11))); 2415 EXPECT_EQ("#define A \\\n" 2416 " a; \\\n" 2417 " \\\n" 2418 " b;", 2419 format("#define A \\\n" 2420 " a; \\\n" 2421 " \\\n" 2422 " b;", 2423 getLLVMStyleWithColumns(11))); 2424 } 2425 2426 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2427 verifyIncompleteFormat("#define A :"); 2428 verifyFormat("#define SOMECASES \\\n" 2429 " case 1: \\\n" 2430 " case 2\n", 2431 getLLVMStyleWithColumns(20)); 2432 verifyFormat("#define MACRO(a) \\\n" 2433 " if (a) \\\n" 2434 " f(); \\\n" 2435 " else \\\n" 2436 " g()", 2437 getLLVMStyleWithColumns(18)); 2438 verifyFormat("#define A template <typename T>"); 2439 verifyIncompleteFormat("#define STR(x) #x\n" 2440 "f(STR(this_is_a_string_literal{));"); 2441 verifyFormat("#pragma omp threadprivate( \\\n" 2442 " y)), // expected-warning", 2443 getLLVMStyleWithColumns(28)); 2444 verifyFormat("#d, = };"); 2445 verifyFormat("#if \"a"); 2446 verifyIncompleteFormat("({\n" 2447 "#define b \\\n" 2448 " } \\\n" 2449 " a\n" 2450 "a", 2451 getLLVMStyleWithColumns(15)); 2452 verifyFormat("#define A \\\n" 2453 " { \\\n" 2454 " {\n" 2455 "#define B \\\n" 2456 " } \\\n" 2457 " }", 2458 getLLVMStyleWithColumns(15)); 2459 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2460 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2461 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2462 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2463 } 2464 2465 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2466 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2467 EXPECT_EQ("class A : public QObject {\n" 2468 " Q_OBJECT\n" 2469 "\n" 2470 " A() {}\n" 2471 "};", 2472 format("class A : public QObject {\n" 2473 " Q_OBJECT\n" 2474 "\n" 2475 " A() {\n}\n" 2476 "} ;")); 2477 EXPECT_EQ("MACRO\n" 2478 "/*static*/ int i;", 2479 format("MACRO\n" 2480 " /*static*/ int i;")); 2481 EXPECT_EQ("SOME_MACRO\n" 2482 "namespace {\n" 2483 "void f();\n" 2484 "} // namespace", 2485 format("SOME_MACRO\n" 2486 " namespace {\n" 2487 "void f( );\n" 2488 "} // namespace")); 2489 // Only if the identifier contains at least 5 characters. 2490 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2491 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2492 // Only if everything is upper case. 2493 EXPECT_EQ("class A : public QObject {\n" 2494 " Q_Object A() {}\n" 2495 "};", 2496 format("class A : public QObject {\n" 2497 " Q_Object\n" 2498 " A() {\n}\n" 2499 "} ;")); 2500 2501 // Only if the next line can actually start an unwrapped line. 2502 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2503 format("SOME_WEIRD_LOG_MACRO\n" 2504 "<< SomeThing;")); 2505 2506 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2507 "(n, buffers))\n", 2508 getChromiumStyle(FormatStyle::LK_Cpp)); 2509 } 2510 2511 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2512 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2513 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2514 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2515 "class X {};\n" 2516 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2517 "int *createScopDetectionPass() { return 0; }", 2518 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2519 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2520 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2521 " class X {};\n" 2522 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2523 " int *createScopDetectionPass() { return 0; }")); 2524 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2525 // braces, so that inner block is indented one level more. 2526 EXPECT_EQ("int q() {\n" 2527 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2528 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2529 " IPC_END_MESSAGE_MAP()\n" 2530 "}", 2531 format("int q() {\n" 2532 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2533 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2534 " IPC_END_MESSAGE_MAP()\n" 2535 "}")); 2536 2537 // Same inside macros. 2538 EXPECT_EQ("#define LIST(L) \\\n" 2539 " L(A) \\\n" 2540 " L(B) \\\n" 2541 " L(C)", 2542 format("#define LIST(L) \\\n" 2543 " L(A) \\\n" 2544 " L(B) \\\n" 2545 " L(C)", 2546 getGoogleStyle())); 2547 2548 // These must not be recognized as macros. 2549 EXPECT_EQ("int q() {\n" 2550 " f(x);\n" 2551 " f(x) {}\n" 2552 " f(x)->g();\n" 2553 " f(x)->*g();\n" 2554 " f(x).g();\n" 2555 " f(x) = x;\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)[y].z();\n" 2567 " LOG(INFO) << x;\n" 2568 " ifstream(x) >> x;\n" 2569 "}\n", 2570 format("int q() {\n" 2571 " f(x)\n;\n" 2572 " f(x)\n {}\n" 2573 " f(x)\n->g();\n" 2574 " f(x)\n->*g();\n" 2575 " f(x)\n.g();\n" 2576 " f(x)\n = x;\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[y].z();\n" 2588 " LOG(INFO)\n << x;\n" 2589 " ifstream(x)\n >> x;\n" 2590 "}\n")); 2591 EXPECT_EQ("int q() {\n" 2592 " F(x)\n" 2593 " if (1) {\n" 2594 " }\n" 2595 " F(x)\n" 2596 " while (1) {\n" 2597 " }\n" 2598 " F(x)\n" 2599 " G(x);\n" 2600 " F(x)\n" 2601 " try {\n" 2602 " Q();\n" 2603 " } catch (...) {\n" 2604 " }\n" 2605 "}\n", 2606 format("int q() {\n" 2607 "F(x)\n" 2608 "if (1) {}\n" 2609 "F(x)\n" 2610 "while (1) {}\n" 2611 "F(x)\n" 2612 "G(x);\n" 2613 "F(x)\n" 2614 "try { Q(); } catch (...) {}\n" 2615 "}\n")); 2616 EXPECT_EQ("class A {\n" 2617 " A() : t(0) {}\n" 2618 " A(int i) noexcept() : {}\n" 2619 " A(X x)\n" // FIXME: function-level try blocks are broken. 2620 " try : t(0) {\n" 2621 " } catch (...) {\n" 2622 " }\n" 2623 "};", 2624 format("class A {\n" 2625 " A()\n : t(0) {}\n" 2626 " A(int i)\n noexcept() : {}\n" 2627 " A(X x)\n" 2628 " try : t(0) {} catch (...) {}\n" 2629 "};")); 2630 FormatStyle Style = getLLVMStyle(); 2631 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2632 Style.BraceWrapping.AfterControlStatement = true; 2633 Style.BraceWrapping.AfterFunction = true; 2634 EXPECT_EQ("void f()\n" 2635 "try\n" 2636 "{\n" 2637 "}", 2638 format("void f() try {\n" 2639 "}", Style)); 2640 EXPECT_EQ("class SomeClass {\n" 2641 "public:\n" 2642 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2643 "};", 2644 format("class SomeClass {\n" 2645 "public:\n" 2646 " SomeClass()\n" 2647 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2648 "};")); 2649 EXPECT_EQ("class SomeClass {\n" 2650 "public:\n" 2651 " SomeClass()\n" 2652 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2653 "};", 2654 format("class SomeClass {\n" 2655 "public:\n" 2656 " SomeClass()\n" 2657 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2658 "};", 2659 getLLVMStyleWithColumns(40))); 2660 2661 verifyFormat("MACRO(>)"); 2662 2663 // Some macros contain an implicit semicolon. 2664 Style = getLLVMStyle(); 2665 Style.StatementMacros.push_back("FOO"); 2666 verifyFormat("FOO(a) int b = 0;"); 2667 verifyFormat("FOO(a)\n" 2668 "int b = 0;", 2669 Style); 2670 verifyFormat("FOO(a);\n" 2671 "int b = 0;", 2672 Style); 2673 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 2674 "int b = 0;", 2675 Style); 2676 verifyFormat("FOO()\n" 2677 "int b = 0;", 2678 Style); 2679 verifyFormat("FOO\n" 2680 "int b = 0;", 2681 Style); 2682 verifyFormat("void f() {\n" 2683 " FOO(a)\n" 2684 " return a;\n" 2685 "}", 2686 Style); 2687 verifyFormat("FOO(a)\n" 2688 "FOO(b)", 2689 Style); 2690 verifyFormat("int a = 0;\n" 2691 "FOO(b)\n" 2692 "int c = 0;", 2693 Style); 2694 verifyFormat("int a = 0;\n" 2695 "int x = FOO(a)\n" 2696 "int b = 0;", 2697 Style); 2698 verifyFormat("void foo(int a) { FOO(a) }\n" 2699 "uint32_t bar() {}", 2700 Style); 2701 } 2702 2703 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2704 verifyFormat("#define A \\\n" 2705 " f({ \\\n" 2706 " g(); \\\n" 2707 " });", 2708 getLLVMStyleWithColumns(11)); 2709 } 2710 2711 TEST_F(FormatTest, IndentPreprocessorDirectives) { 2712 FormatStyle Style = getLLVMStyle(); 2713 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 2714 Style.ColumnLimit = 40; 2715 verifyFormat("#ifdef _WIN32\n" 2716 "#define A 0\n" 2717 "#ifdef VAR2\n" 2718 "#define B 1\n" 2719 "#include <someheader.h>\n" 2720 "#define MACRO \\\n" 2721 " some_very_long_func_aaaaaaaaaa();\n" 2722 "#endif\n" 2723 "#else\n" 2724 "#define A 1\n" 2725 "#endif", 2726 Style); 2727 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 2728 verifyFormat("#ifdef _WIN32\n" 2729 "# define A 0\n" 2730 "# ifdef VAR2\n" 2731 "# define B 1\n" 2732 "# include <someheader.h>\n" 2733 "# define MACRO \\\n" 2734 " some_very_long_func_aaaaaaaaaa();\n" 2735 "# endif\n" 2736 "#else\n" 2737 "# define A 1\n" 2738 "#endif", 2739 Style); 2740 verifyFormat("#if A\n" 2741 "# define MACRO \\\n" 2742 " void a(int x) { \\\n" 2743 " b(); \\\n" 2744 " c(); \\\n" 2745 " d(); \\\n" 2746 " e(); \\\n" 2747 " f(); \\\n" 2748 " }\n" 2749 "#endif", 2750 Style); 2751 // Comments before include guard. 2752 verifyFormat("// file comment\n" 2753 "// file comment\n" 2754 "#ifndef HEADER_H\n" 2755 "#define HEADER_H\n" 2756 "code();\n" 2757 "#endif", 2758 Style); 2759 // Test with include guards. 2760 verifyFormat("#ifndef HEADER_H\n" 2761 "#define HEADER_H\n" 2762 "code();\n" 2763 "#endif", 2764 Style); 2765 // Include guards must have a #define with the same variable immediately 2766 // after #ifndef. 2767 verifyFormat("#ifndef NOT_GUARD\n" 2768 "# define FOO\n" 2769 "code();\n" 2770 "#endif", 2771 Style); 2772 2773 // Include guards must cover the entire file. 2774 verifyFormat("code();\n" 2775 "code();\n" 2776 "#ifndef NOT_GUARD\n" 2777 "# define NOT_GUARD\n" 2778 "code();\n" 2779 "#endif", 2780 Style); 2781 verifyFormat("#ifndef NOT_GUARD\n" 2782 "# define NOT_GUARD\n" 2783 "code();\n" 2784 "#endif\n" 2785 "code();", 2786 Style); 2787 // Test with trailing blank lines. 2788 verifyFormat("#ifndef HEADER_H\n" 2789 "#define HEADER_H\n" 2790 "code();\n" 2791 "#endif\n", 2792 Style); 2793 // Include guards don't have #else. 2794 verifyFormat("#ifndef NOT_GUARD\n" 2795 "# define NOT_GUARD\n" 2796 "code();\n" 2797 "#else\n" 2798 "#endif", 2799 Style); 2800 verifyFormat("#ifndef NOT_GUARD\n" 2801 "# define NOT_GUARD\n" 2802 "code();\n" 2803 "#elif FOO\n" 2804 "#endif", 2805 Style); 2806 // Non-identifier #define after potential include guard. 2807 verifyFormat("#ifndef FOO\n" 2808 "# define 1\n" 2809 "#endif\n", 2810 Style); 2811 // #if closes past last non-preprocessor line. 2812 verifyFormat("#ifndef FOO\n" 2813 "#define FOO\n" 2814 "#if 1\n" 2815 "int i;\n" 2816 "# define A 0\n" 2817 "#endif\n" 2818 "#endif\n", 2819 Style); 2820 // FIXME: This doesn't handle the case where there's code between the 2821 // #ifndef and #define but all other conditions hold. This is because when 2822 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 2823 // previous code line yet, so we can't detect it. 2824 EXPECT_EQ("#ifndef NOT_GUARD\n" 2825 "code();\n" 2826 "#define NOT_GUARD\n" 2827 "code();\n" 2828 "#endif", 2829 format("#ifndef NOT_GUARD\n" 2830 "code();\n" 2831 "# define NOT_GUARD\n" 2832 "code();\n" 2833 "#endif", 2834 Style)); 2835 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 2836 // be outside an include guard. Examples are #pragma once and 2837 // #pragma GCC diagnostic, or anything else that does not change the meaning 2838 // of the file if it's included multiple times. 2839 EXPECT_EQ("#ifdef WIN32\n" 2840 "# pragma once\n" 2841 "#endif\n" 2842 "#ifndef HEADER_H\n" 2843 "# define HEADER_H\n" 2844 "code();\n" 2845 "#endif", 2846 format("#ifdef WIN32\n" 2847 "# pragma once\n" 2848 "#endif\n" 2849 "#ifndef HEADER_H\n" 2850 "#define HEADER_H\n" 2851 "code();\n" 2852 "#endif", 2853 Style)); 2854 // FIXME: This does not detect when there is a single non-preprocessor line 2855 // in front of an include-guard-like structure where other conditions hold 2856 // because ScopedLineState hides the line. 2857 EXPECT_EQ("code();\n" 2858 "#ifndef HEADER_H\n" 2859 "#define HEADER_H\n" 2860 "code();\n" 2861 "#endif", 2862 format("code();\n" 2863 "#ifndef HEADER_H\n" 2864 "# define HEADER_H\n" 2865 "code();\n" 2866 "#endif", 2867 Style)); 2868 // Keep comments aligned with #, otherwise indent comments normally. These 2869 // tests cannot use verifyFormat because messUp manipulates leading 2870 // whitespace. 2871 { 2872 const char *Expected = "" 2873 "void f() {\n" 2874 "#if 1\n" 2875 "// Preprocessor aligned.\n" 2876 "# define A 0\n" 2877 " // Code. Separated by blank line.\n" 2878 "\n" 2879 "# define B 0\n" 2880 " // Code. Not aligned with #\n" 2881 "# define C 0\n" 2882 "#endif"; 2883 const char *ToFormat = "" 2884 "void f() {\n" 2885 "#if 1\n" 2886 "// Preprocessor aligned.\n" 2887 "# define A 0\n" 2888 "// Code. Separated by blank line.\n" 2889 "\n" 2890 "# define B 0\n" 2891 " // Code. Not aligned with #\n" 2892 "# define C 0\n" 2893 "#endif"; 2894 EXPECT_EQ(Expected, format(ToFormat, Style)); 2895 EXPECT_EQ(Expected, format(Expected, Style)); 2896 } 2897 // Keep block quotes aligned. 2898 { 2899 const char *Expected = "" 2900 "void f() {\n" 2901 "#if 1\n" 2902 "/* Preprocessor aligned. */\n" 2903 "# define A 0\n" 2904 " /* Code. Separated by blank line. */\n" 2905 "\n" 2906 "# define B 0\n" 2907 " /* Code. Not aligned with # */\n" 2908 "# define C 0\n" 2909 "#endif"; 2910 const char *ToFormat = "" 2911 "void f() {\n" 2912 "#if 1\n" 2913 "/* Preprocessor aligned. */\n" 2914 "# define A 0\n" 2915 "/* Code. Separated by blank line. */\n" 2916 "\n" 2917 "# define B 0\n" 2918 " /* Code. Not aligned with # */\n" 2919 "# define C 0\n" 2920 "#endif"; 2921 EXPECT_EQ(Expected, format(ToFormat, Style)); 2922 EXPECT_EQ(Expected, format(Expected, Style)); 2923 } 2924 // Keep comments aligned with un-indented directives. 2925 { 2926 const char *Expected = "" 2927 "void f() {\n" 2928 "// Preprocessor aligned.\n" 2929 "#define A 0\n" 2930 " // Code. Separated by blank line.\n" 2931 "\n" 2932 "#define B 0\n" 2933 " // Code. Not aligned with #\n" 2934 "#define C 0\n"; 2935 const char *ToFormat = "" 2936 "void f() {\n" 2937 "// Preprocessor aligned.\n" 2938 "#define A 0\n" 2939 "// Code. Separated by blank line.\n" 2940 "\n" 2941 "#define B 0\n" 2942 " // Code. Not aligned with #\n" 2943 "#define C 0\n"; 2944 EXPECT_EQ(Expected, format(ToFormat, Style)); 2945 EXPECT_EQ(Expected, format(Expected, Style)); 2946 } 2947 // Test with tabs. 2948 Style.UseTab = FormatStyle::UT_Always; 2949 Style.IndentWidth = 8; 2950 Style.TabWidth = 8; 2951 verifyFormat("#ifdef _WIN32\n" 2952 "#\tdefine A 0\n" 2953 "#\tifdef VAR2\n" 2954 "#\t\tdefine B 1\n" 2955 "#\t\tinclude <someheader.h>\n" 2956 "#\t\tdefine MACRO \\\n" 2957 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 2958 "#\tendif\n" 2959 "#else\n" 2960 "#\tdefine A 1\n" 2961 "#endif", 2962 Style); 2963 2964 // Regression test: Multiline-macro inside include guards. 2965 verifyFormat("#ifndef HEADER_H\n" 2966 "#define HEADER_H\n" 2967 "#define A() \\\n" 2968 " int i; \\\n" 2969 " int j;\n" 2970 "#endif // HEADER_H", 2971 getLLVMStyleWithColumns(20)); 2972 } 2973 2974 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2975 verifyFormat("{\n { a #c; }\n}"); 2976 } 2977 2978 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2979 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2980 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2981 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2982 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2983 } 2984 2985 TEST_F(FormatTest, EscapedNewlines) { 2986 FormatStyle Narrow = getLLVMStyleWithColumns(11); 2987 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 2988 format("#define A \\\nint i;\\\n int j;", Narrow)); 2989 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2990 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2991 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 2992 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2993 2994 FormatStyle AlignLeft = getLLVMStyle(); 2995 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 2996 EXPECT_EQ("#define MACRO(x) \\\n" 2997 "private: \\\n" 2998 " int x(int a);\n", 2999 format("#define MACRO(x) \\\n" 3000 "private: \\\n" 3001 " int x(int a);\n", 3002 AlignLeft)); 3003 3004 // CRLF line endings 3005 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 3006 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 3007 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 3008 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3009 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 3010 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 3011 EXPECT_EQ("#define MACRO(x) \\\r\n" 3012 "private: \\\r\n" 3013 " int x(int a);\r\n", 3014 format("#define MACRO(x) \\\r\n" 3015 "private: \\\r\n" 3016 " int x(int a);\r\n", 3017 AlignLeft)); 3018 3019 FormatStyle DontAlign = getLLVMStyle(); 3020 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 3021 DontAlign.MaxEmptyLinesToKeep = 3; 3022 // FIXME: can't use verifyFormat here because the newline before 3023 // "public:" is not inserted the first time it's reformatted 3024 EXPECT_EQ("#define A \\\n" 3025 " class Foo { \\\n" 3026 " void bar(); \\\n" 3027 "\\\n" 3028 "\\\n" 3029 "\\\n" 3030 " public: \\\n" 3031 " void baz(); \\\n" 3032 " };", 3033 format("#define A \\\n" 3034 " class Foo { \\\n" 3035 " void bar(); \\\n" 3036 "\\\n" 3037 "\\\n" 3038 "\\\n" 3039 " public: \\\n" 3040 " void baz(); \\\n" 3041 " };", 3042 DontAlign)); 3043 } 3044 3045 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3046 verifyFormat("#define A \\\n" 3047 " int v( \\\n" 3048 " a); \\\n" 3049 " int i;", 3050 getLLVMStyleWithColumns(11)); 3051 } 3052 3053 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3054 EXPECT_EQ( 3055 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3056 " \\\n" 3057 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3058 "\n" 3059 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3060 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3061 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3062 "\\\n" 3063 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3064 " \n" 3065 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3066 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3067 } 3068 3069 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3070 EXPECT_EQ("int\n" 3071 "#define A\n" 3072 " a;", 3073 format("int\n#define A\na;")); 3074 verifyFormat("functionCallTo(\n" 3075 " someOtherFunction(\n" 3076 " withSomeParameters, whichInSequence,\n" 3077 " areLongerThanALine(andAnotherCall,\n" 3078 "#define A B\n" 3079 " withMoreParamters,\n" 3080 " whichStronglyInfluenceTheLayout),\n" 3081 " andMoreParameters),\n" 3082 " trailing);", 3083 getLLVMStyleWithColumns(69)); 3084 verifyFormat("Foo::Foo()\n" 3085 "#ifdef BAR\n" 3086 " : baz(0)\n" 3087 "#endif\n" 3088 "{\n" 3089 "}"); 3090 verifyFormat("void f() {\n" 3091 " if (true)\n" 3092 "#ifdef A\n" 3093 " f(42);\n" 3094 " x();\n" 3095 "#else\n" 3096 " g();\n" 3097 " x();\n" 3098 "#endif\n" 3099 "}"); 3100 verifyFormat("void f(param1, param2,\n" 3101 " param3,\n" 3102 "#ifdef A\n" 3103 " param4(param5,\n" 3104 "#ifdef A1\n" 3105 " param6,\n" 3106 "#ifdef A2\n" 3107 " param7),\n" 3108 "#else\n" 3109 " param8),\n" 3110 " param9,\n" 3111 "#endif\n" 3112 " param10,\n" 3113 "#endif\n" 3114 " param11)\n" 3115 "#else\n" 3116 " param12)\n" 3117 "#endif\n" 3118 "{\n" 3119 " x();\n" 3120 "}", 3121 getLLVMStyleWithColumns(28)); 3122 verifyFormat("#if 1\n" 3123 "int i;"); 3124 verifyFormat("#if 1\n" 3125 "#endif\n" 3126 "#if 1\n" 3127 "#else\n" 3128 "#endif\n"); 3129 verifyFormat("DEBUG({\n" 3130 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3131 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3132 "});\n" 3133 "#if a\n" 3134 "#else\n" 3135 "#endif"); 3136 3137 verifyIncompleteFormat("void f(\n" 3138 "#if A\n" 3139 ");\n" 3140 "#else\n" 3141 "#endif"); 3142 } 3143 3144 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3145 verifyFormat("#endif\n" 3146 "#if B"); 3147 } 3148 3149 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3150 FormatStyle SingleLine = getLLVMStyle(); 3151 SingleLine.AllowShortIfStatementsOnASingleLine = true; 3152 verifyFormat("#if 0\n" 3153 "#elif 1\n" 3154 "#endif\n" 3155 "void foo() {\n" 3156 " if (test) foo2();\n" 3157 "}", 3158 SingleLine); 3159 } 3160 3161 TEST_F(FormatTest, LayoutBlockInsideParens) { 3162 verifyFormat("functionCall({ int i; });"); 3163 verifyFormat("functionCall({\n" 3164 " int i;\n" 3165 " int j;\n" 3166 "});"); 3167 verifyFormat("functionCall(\n" 3168 " {\n" 3169 " int i;\n" 3170 " int j;\n" 3171 " },\n" 3172 " aaaa, bbbb, cccc);"); 3173 verifyFormat("functionA(functionB({\n" 3174 " int i;\n" 3175 " int j;\n" 3176 " }),\n" 3177 " aaaa, bbbb, cccc);"); 3178 verifyFormat("functionCall(\n" 3179 " {\n" 3180 " int i;\n" 3181 " int j;\n" 3182 " },\n" 3183 " aaaa, bbbb, // comment\n" 3184 " cccc);"); 3185 verifyFormat("functionA(functionB({\n" 3186 " int i;\n" 3187 " int j;\n" 3188 " }),\n" 3189 " aaaa, bbbb, // comment\n" 3190 " cccc);"); 3191 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3192 verifyFormat("functionCall(aaaa, bbbb, {\n" 3193 " int i;\n" 3194 " int j;\n" 3195 "});"); 3196 verifyFormat( 3197 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3198 " {\n" 3199 " int i; // break\n" 3200 " },\n" 3201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3202 " ccccccccccccccccc));"); 3203 verifyFormat("DEBUG({\n" 3204 " if (a)\n" 3205 " f();\n" 3206 "});"); 3207 } 3208 3209 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3210 EXPECT_EQ("SOME_MACRO { int i; }\n" 3211 "int i;", 3212 format(" SOME_MACRO {int i;} int i;")); 3213 } 3214 3215 TEST_F(FormatTest, LayoutNestedBlocks) { 3216 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3217 " struct s {\n" 3218 " int i;\n" 3219 " };\n" 3220 " s kBitsToOs[] = {{10}};\n" 3221 " for (int i = 0; i < 10; ++i)\n" 3222 " return;\n" 3223 "}"); 3224 verifyFormat("call(parameter, {\n" 3225 " something();\n" 3226 " // Comment using all columns.\n" 3227 " somethingelse();\n" 3228 "});", 3229 getLLVMStyleWithColumns(40)); 3230 verifyFormat("DEBUG( //\n" 3231 " { f(); }, a);"); 3232 verifyFormat("DEBUG( //\n" 3233 " {\n" 3234 " f(); //\n" 3235 " },\n" 3236 " a);"); 3237 3238 EXPECT_EQ("call(parameter, {\n" 3239 " something();\n" 3240 " // Comment too\n" 3241 " // looooooooooong.\n" 3242 " somethingElse();\n" 3243 "});", 3244 format("call(parameter, {\n" 3245 " something();\n" 3246 " // Comment too looooooooooong.\n" 3247 " somethingElse();\n" 3248 "});", 3249 getLLVMStyleWithColumns(29))); 3250 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3251 EXPECT_EQ("DEBUG({ // comment\n" 3252 " int i;\n" 3253 "});", 3254 format("DEBUG({ // comment\n" 3255 "int i;\n" 3256 "});")); 3257 EXPECT_EQ("DEBUG({\n" 3258 " int i;\n" 3259 "\n" 3260 " // comment\n" 3261 " int j;\n" 3262 "});", 3263 format("DEBUG({\n" 3264 " int i;\n" 3265 "\n" 3266 " // comment\n" 3267 " int j;\n" 3268 "});")); 3269 3270 verifyFormat("DEBUG({\n" 3271 " if (a)\n" 3272 " return;\n" 3273 "});"); 3274 verifyGoogleFormat("DEBUG({\n" 3275 " if (a) return;\n" 3276 "});"); 3277 FormatStyle Style = getGoogleStyle(); 3278 Style.ColumnLimit = 45; 3279 verifyFormat("Debug(\n" 3280 " 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, DontBreakBeforeQualifiedOperator) { 4187 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 4188 // Prefer keeping `::` followed by `operator` together. 4189 EXPECT_EQ("const aaaa::bbbbbbb &\n" 4190 "ccccccccc::operator++() {\n" 4191 " stuff();\n" 4192 "}", 4193 format("const aaaa::bbbbbbb\n" 4194 "&ccccccccc::operator++() { stuff(); }", 4195 getLLVMStyleWithColumns(40))); 4196 } 4197 4198 TEST_F(FormatTest, TrailingReturnType) { 4199 verifyFormat("auto foo() -> int;\n"); 4200 verifyFormat("struct S {\n" 4201 " auto bar() const -> int;\n" 4202 "};"); 4203 verifyFormat("template <size_t Order, typename T>\n" 4204 "auto load_img(const std::string &filename)\n" 4205 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 4206 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 4207 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 4208 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 4209 verifyFormat("template <typename T>\n" 4210 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 4211 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 4212 4213 // Not trailing return types. 4214 verifyFormat("void f() { auto a = b->c(); }"); 4215 } 4216 4217 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 4218 // Avoid breaking before trailing 'const' or other trailing annotations, if 4219 // they are not function-like. 4220 FormatStyle Style = getGoogleStyle(); 4221 Style.ColumnLimit = 47; 4222 verifyFormat("void someLongFunction(\n" 4223 " int someLoooooooooooooongParameter) const {\n}", 4224 getLLVMStyleWithColumns(47)); 4225 verifyFormat("LoooooongReturnType\n" 4226 "someLoooooooongFunction() const {}", 4227 getLLVMStyleWithColumns(47)); 4228 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 4229 " const {}", 4230 Style); 4231 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4232 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 4233 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4234 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 4235 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4236 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 4237 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 4238 " aaaaaaaaaaa aaaaa) const override;"); 4239 verifyGoogleFormat( 4240 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4241 " const override;"); 4242 4243 // Even if the first parameter has to be wrapped. 4244 verifyFormat("void someLongFunction(\n" 4245 " int someLongParameter) const {}", 4246 getLLVMStyleWithColumns(46)); 4247 verifyFormat("void someLongFunction(\n" 4248 " int someLongParameter) const {}", 4249 Style); 4250 verifyFormat("void someLongFunction(\n" 4251 " int someLongParameter) override {}", 4252 Style); 4253 verifyFormat("void someLongFunction(\n" 4254 " int someLongParameter) OVERRIDE {}", 4255 Style); 4256 verifyFormat("void someLongFunction(\n" 4257 " int someLongParameter) final {}", 4258 Style); 4259 verifyFormat("void someLongFunction(\n" 4260 " int someLongParameter) FINAL {}", 4261 Style); 4262 verifyFormat("void someLongFunction(\n" 4263 " int parameter) const override {}", 4264 Style); 4265 4266 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4267 verifyFormat("void someLongFunction(\n" 4268 " int someLongParameter) const\n" 4269 "{\n" 4270 "}", 4271 Style); 4272 4273 // Unless these are unknown annotations. 4274 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 4275 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4276 " LONG_AND_UGLY_ANNOTATION;"); 4277 4278 // Breaking before function-like trailing annotations is fine to keep them 4279 // close to their arguments. 4280 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4281 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4282 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4283 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4284 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4285 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 4286 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 4287 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 4288 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 4289 4290 verifyFormat( 4291 "void aaaaaaaaaaaaaaaaaa()\n" 4292 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 4293 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 4294 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4295 " __attribute__((unused));"); 4296 verifyGoogleFormat( 4297 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4298 " GUARDED_BY(aaaaaaaaaaaa);"); 4299 verifyGoogleFormat( 4300 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4301 " GUARDED_BY(aaaaaaaaaaaa);"); 4302 verifyGoogleFormat( 4303 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4304 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4305 verifyGoogleFormat( 4306 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4307 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4308 } 4309 4310 TEST_F(FormatTest, FunctionAnnotations) { 4311 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4312 "int OldFunction(const string ¶meter) {}"); 4313 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4314 "string OldFunction(const string ¶meter) {}"); 4315 verifyFormat("template <typename T>\n" 4316 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4317 "string OldFunction(const string ¶meter) {}"); 4318 4319 // Not function annotations. 4320 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4321 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4322 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4323 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4324 verifyFormat("MACRO(abc).function() // wrap\n" 4325 " << abc;"); 4326 verifyFormat("MACRO(abc)->function() // wrap\n" 4327 " << abc;"); 4328 verifyFormat("MACRO(abc)::function() // wrap\n" 4329 " << abc;"); 4330 } 4331 4332 TEST_F(FormatTest, BreaksDesireably) { 4333 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4334 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4335 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4336 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4338 "}"); 4339 4340 verifyFormat( 4341 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4343 4344 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4345 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4346 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4347 4348 verifyFormat( 4349 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4350 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4351 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4352 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4353 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4354 4355 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4356 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4357 4358 verifyFormat( 4359 "void f() {\n" 4360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4362 "}"); 4363 verifyFormat( 4364 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4365 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4366 verifyFormat( 4367 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4368 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4369 verifyFormat( 4370 "aaaaaa(aaa,\n" 4371 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4372 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4373 " aaaa);"); 4374 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4375 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4376 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4377 4378 // Indent consistently independent of call expression and unary operator. 4379 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4380 " dddddddddddddddddddddddddddddd));"); 4381 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4382 " dddddddddddddddddddddddddddddd));"); 4383 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4384 " dddddddddddddddddddddddddddddd));"); 4385 4386 // This test case breaks on an incorrect memoization, i.e. an optimization not 4387 // taking into account the StopAt value. 4388 verifyFormat( 4389 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4390 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4391 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4392 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4393 4394 verifyFormat("{\n {\n {\n" 4395 " Annotation.SpaceRequiredBefore =\n" 4396 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4397 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4398 " }\n }\n}"); 4399 4400 // Break on an outer level if there was a break on an inner level. 4401 EXPECT_EQ("f(g(h(a, // comment\n" 4402 " b, c),\n" 4403 " d, e),\n" 4404 " x, y);", 4405 format("f(g(h(a, // comment\n" 4406 " b, c), d, e), x, y);")); 4407 4408 // Prefer breaking similar line breaks. 4409 verifyFormat( 4410 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4411 " NSTrackingMouseEnteredAndExited |\n" 4412 " NSTrackingActiveAlways;"); 4413 } 4414 4415 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4416 FormatStyle NoBinPacking = getGoogleStyle(); 4417 NoBinPacking.BinPackParameters = false; 4418 NoBinPacking.BinPackArguments = true; 4419 verifyFormat("void f() {\n" 4420 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4422 "}", 4423 NoBinPacking); 4424 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4425 " int aaaaaaaaaaaaaaaaaaaa,\n" 4426 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4427 NoBinPacking); 4428 4429 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4430 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4431 " vector<int> bbbbbbbbbbbbbbb);", 4432 NoBinPacking); 4433 // FIXME: This behavior difference is probably not wanted. However, currently 4434 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4435 // template arguments from BreakBeforeParameter being set because of the 4436 // one-per-line formatting. 4437 verifyFormat( 4438 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4439 " aaaaaaaaaa> aaaaaaaaaa);", 4440 NoBinPacking); 4441 verifyFormat( 4442 "void fffffffffff(\n" 4443 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4444 " aaaaaaaaaa);"); 4445 } 4446 4447 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4448 FormatStyle NoBinPacking = getGoogleStyle(); 4449 NoBinPacking.BinPackParameters = false; 4450 NoBinPacking.BinPackArguments = false; 4451 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4452 " aaaaaaaaaaaaaaaaaaaa,\n" 4453 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4454 NoBinPacking); 4455 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4456 " aaaaaaaaaaaaa,\n" 4457 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4458 NoBinPacking); 4459 verifyFormat( 4460 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4461 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4463 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4464 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4465 NoBinPacking); 4466 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4467 " .aaaaaaaaaaaaaaaaaa();", 4468 NoBinPacking); 4469 verifyFormat("void f() {\n" 4470 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4471 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4472 "}", 4473 NoBinPacking); 4474 4475 verifyFormat( 4476 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4477 " aaaaaaaaaaaa,\n" 4478 " aaaaaaaaaaaa);", 4479 NoBinPacking); 4480 verifyFormat( 4481 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4482 " ddddddddddddddddddddddddddddd),\n" 4483 " test);", 4484 NoBinPacking); 4485 4486 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4487 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4488 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4489 " aaaaaaaaaaaaaaaaaa;", 4490 NoBinPacking); 4491 verifyFormat("a(\"a\"\n" 4492 " \"a\",\n" 4493 " a);"); 4494 4495 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4496 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4497 " aaaaaaaaa,\n" 4498 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4499 NoBinPacking); 4500 verifyFormat( 4501 "void f() {\n" 4502 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4503 " .aaaaaaa();\n" 4504 "}", 4505 NoBinPacking); 4506 verifyFormat( 4507 "template <class SomeType, class SomeOtherType>\n" 4508 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4509 NoBinPacking); 4510 } 4511 4512 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4513 FormatStyle Style = getLLVMStyleWithColumns(15); 4514 Style.ExperimentalAutoDetectBinPacking = true; 4515 EXPECT_EQ("aaa(aaaa,\n" 4516 " aaaa,\n" 4517 " aaaa);\n" 4518 "aaa(aaaa,\n" 4519 " aaaa,\n" 4520 " aaaa);", 4521 format("aaa(aaaa,\n" // one-per-line 4522 " aaaa,\n" 4523 " aaaa );\n" 4524 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4525 Style)); 4526 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4527 " aaaa);\n" 4528 "aaa(aaaa, aaaa,\n" 4529 " aaaa);", 4530 format("aaa(aaaa, aaaa,\n" // bin-packed 4531 " aaaa );\n" 4532 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4533 Style)); 4534 } 4535 4536 TEST_F(FormatTest, FormatsBuilderPattern) { 4537 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4538 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4539 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4540 " .StartsWith(\".init\", ORDER_INIT)\n" 4541 " .StartsWith(\".fini\", ORDER_FINI)\n" 4542 " .StartsWith(\".hash\", ORDER_HASH)\n" 4543 " .Default(ORDER_TEXT);\n"); 4544 4545 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4546 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4547 verifyFormat( 4548 "aaaaaaa->aaaaaaa\n" 4549 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4550 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4551 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4552 verifyFormat( 4553 "aaaaaaa->aaaaaaa\n" 4554 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4555 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4556 verifyFormat( 4557 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4558 " aaaaaaaaaaaaaa);"); 4559 verifyFormat( 4560 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4561 " aaaaaa->aaaaaaaaaaaa()\n" 4562 " ->aaaaaaaaaaaaaaaa(\n" 4563 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4564 " ->aaaaaaaaaaaaaaaaa();"); 4565 verifyGoogleFormat( 4566 "void f() {\n" 4567 " someo->Add((new util::filetools::Handler(dir))\n" 4568 " ->OnEvent1(NewPermanentCallback(\n" 4569 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4570 " ->OnEvent2(NewPermanentCallback(\n" 4571 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4572 " ->OnEvent3(NewPermanentCallback(\n" 4573 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4574 " ->OnEvent5(NewPermanentCallback(\n" 4575 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4576 " ->OnEvent6(NewPermanentCallback(\n" 4577 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4578 "}"); 4579 4580 verifyFormat( 4581 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4582 verifyFormat("aaaaaaaaaaaaaaa()\n" 4583 " .aaaaaaaaaaaaaaa()\n" 4584 " .aaaaaaaaaaaaaaa()\n" 4585 " .aaaaaaaaaaaaaaa()\n" 4586 " .aaaaaaaaaaaaaaa();"); 4587 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4588 " .aaaaaaaaaaaaaaa()\n" 4589 " .aaaaaaaaaaaaaaa()\n" 4590 " .aaaaaaaaaaaaaaa();"); 4591 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4592 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4593 " .aaaaaaaaaaaaaaa();"); 4594 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4595 " ->aaaaaaaaaaaaaae(0)\n" 4596 " ->aaaaaaaaaaaaaaa();"); 4597 4598 // Don't linewrap after very short segments. 4599 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4600 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4601 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4602 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4603 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4604 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4605 verifyFormat("aaa()\n" 4606 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4607 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4608 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4609 4610 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4611 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4612 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4613 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4614 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4615 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4616 4617 // Prefer not to break after empty parentheses. 4618 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4619 " First->LastNewlineOffset);"); 4620 4621 // Prefer not to create "hanging" indents. 4622 verifyFormat( 4623 "return !soooooooooooooome_map\n" 4624 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4625 " .second;"); 4626 verifyFormat( 4627 "return aaaaaaaaaaaaaaaa\n" 4628 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4629 " .aaaa(aaaaaaaaaaaaaa);"); 4630 // No hanging indent here. 4631 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4633 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4634 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4635 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4636 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4637 getLLVMStyleWithColumns(60)); 4638 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4639 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4640 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4641 getLLVMStyleWithColumns(59)); 4642 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4643 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4644 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4645 4646 // Dont break if only closing statements before member call 4647 verifyFormat("test() {\n" 4648 " ([]() -> {\n" 4649 " int b = 32;\n" 4650 " return 3;\n" 4651 " }).foo();\n" 4652 "}"); 4653 verifyFormat("test() {\n" 4654 " (\n" 4655 " []() -> {\n" 4656 " int b = 32;\n" 4657 " return 3;\n" 4658 " },\n" 4659 " foo, bar)\n" 4660 " .foo();\n" 4661 "}"); 4662 verifyFormat("test() {\n" 4663 " ([]() -> {\n" 4664 " int b = 32;\n" 4665 " return 3;\n" 4666 " })\n" 4667 " .foo()\n" 4668 " .bar();\n" 4669 "}"); 4670 verifyFormat("test() {\n" 4671 " ([]() -> {\n" 4672 " int b = 32;\n" 4673 " return 3;\n" 4674 " })\n" 4675 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 4676 " \"bbbb\");\n" 4677 "}", 4678 getLLVMStyleWithColumns(30)); 4679 } 4680 4681 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4682 verifyFormat( 4683 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4684 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4685 verifyFormat( 4686 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4687 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4688 4689 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4690 " ccccccccccccccccccccccccc) {\n}"); 4691 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4692 " ccccccccccccccccccccccccc) {\n}"); 4693 4694 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4695 " ccccccccccccccccccccccccc) {\n}"); 4696 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4697 " ccccccccccccccccccccccccc) {\n}"); 4698 4699 verifyFormat( 4700 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4701 " ccccccccccccccccccccccccc) {\n}"); 4702 verifyFormat( 4703 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4704 " ccccccccccccccccccccccccc) {\n}"); 4705 4706 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4707 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4708 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4709 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4710 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4711 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4712 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4713 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4714 4715 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4716 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4717 " aaaaaaaaaaaaaaa != aa) {\n}"); 4718 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4719 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4720 " aaaaaaaaaaaaaaa != aa) {\n}"); 4721 } 4722 4723 TEST_F(FormatTest, BreaksAfterAssignments) { 4724 verifyFormat( 4725 "unsigned Cost =\n" 4726 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4727 " SI->getPointerAddressSpaceee());\n"); 4728 verifyFormat( 4729 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4730 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4731 4732 verifyFormat( 4733 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4734 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4735 verifyFormat("unsigned OriginalStartColumn =\n" 4736 " SourceMgr.getSpellingColumnNumber(\n" 4737 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4738 " 1;"); 4739 } 4740 4741 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 4742 FormatStyle Style = getLLVMStyle(); 4743 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4744 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 4745 Style); 4746 4747 Style.PenaltyBreakAssignment = 20; 4748 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 4749 " cccccccccccccccccccccccccc;", 4750 Style); 4751 } 4752 4753 TEST_F(FormatTest, AlignsAfterAssignments) { 4754 verifyFormat( 4755 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4756 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4757 verifyFormat( 4758 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4759 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4760 verifyFormat( 4761 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4762 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4763 verifyFormat( 4764 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4765 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4766 verifyFormat( 4767 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4768 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4769 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4770 } 4771 4772 TEST_F(FormatTest, AlignsAfterReturn) { 4773 verifyFormat( 4774 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4775 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4776 verifyFormat( 4777 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4778 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4779 verifyFormat( 4780 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4781 " aaaaaaaaaaaaaaaaaaaaaa();"); 4782 verifyFormat( 4783 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4784 " aaaaaaaaaaaaaaaaaaaaaa());"); 4785 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4786 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4787 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4788 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4789 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4790 verifyFormat("return\n" 4791 " // true if code is one of a or b.\n" 4792 " code == a || code == b;"); 4793 } 4794 4795 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4796 verifyFormat( 4797 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4798 " aaaaaaaaa aaaaaaa) {}"); 4799 verifyFormat( 4800 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4801 " aaaaaaaaaaa aaaaaaaaa);"); 4802 verifyFormat( 4803 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4804 " aaaaaaaaaaaaaaaaaaaaa));"); 4805 FormatStyle Style = getLLVMStyle(); 4806 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4807 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4808 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4809 Style); 4810 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4811 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4812 Style); 4813 verifyFormat("SomeLongVariableName->someFunction(\n" 4814 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4815 Style); 4816 verifyFormat( 4817 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4818 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4819 Style); 4820 verifyFormat( 4821 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4822 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4823 Style); 4824 verifyFormat( 4825 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4826 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4827 Style); 4828 4829 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4830 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4831 " b));", 4832 Style); 4833 4834 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4835 Style.BinPackArguments = false; 4836 Style.BinPackParameters = false; 4837 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4838 " aaaaaaaaaaa aaaaaaaa,\n" 4839 " aaaaaaaaa aaaaaaa,\n" 4840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4841 Style); 4842 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4843 " aaaaaaaaaaa aaaaaaaaa,\n" 4844 " aaaaaaaaaaa aaaaaaaaa,\n" 4845 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4846 Style); 4847 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4848 " aaaaaaaaaaaaaaa,\n" 4849 " aaaaaaaaaaaaaaaaaaaaa,\n" 4850 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4851 Style); 4852 verifyFormat( 4853 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4854 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4855 Style); 4856 verifyFormat( 4857 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4858 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4859 Style); 4860 verifyFormat( 4861 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4862 " aaaaaaaaaaaaaaaaaaaaa(\n" 4863 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4864 " aaaaaaaaaaaaaaaa);", 4865 Style); 4866 verifyFormat( 4867 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4868 " aaaaaaaaaaaaaaaaaaaaa(\n" 4869 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4870 " aaaaaaaaaaaaaaaa);", 4871 Style); 4872 } 4873 4874 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4875 FormatStyle Style = getLLVMStyleWithColumns(40); 4876 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4877 " bbbbbbbbbbbbbbbbbbbbbb);", 4878 Style); 4879 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4880 Style.AlignOperands = false; 4881 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4882 " bbbbbbbbbbbbbbbbbbbbbb);", 4883 Style); 4884 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4885 Style.AlignOperands = true; 4886 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4887 " bbbbbbbbbbbbbbbbbbbbbb);", 4888 Style); 4889 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4890 Style.AlignOperands = false; 4891 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4892 " bbbbbbbbbbbbbbbbbbbbbb);", 4893 Style); 4894 } 4895 4896 TEST_F(FormatTest, BreaksConditionalExpressions) { 4897 verifyFormat( 4898 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4899 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4900 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4901 verifyFormat( 4902 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4903 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4904 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4905 verifyFormat( 4906 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4907 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4908 verifyFormat( 4909 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 4910 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4911 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4912 verifyFormat( 4913 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4914 " : aaaaaaaaaaaaa);"); 4915 verifyFormat( 4916 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4917 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4918 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4919 " aaaaaaaaaaaaa);"); 4920 verifyFormat( 4921 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4922 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4923 " aaaaaaaaaaaaa);"); 4924 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4925 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4926 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4927 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4928 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4929 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4930 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4931 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4932 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4933 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4934 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4935 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4936 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4938 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4939 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4940 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4941 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4942 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4943 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4944 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4945 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4946 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4947 " : aaaaaaaaaaaaaaaa;"); 4948 verifyFormat( 4949 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4950 " ? aaaaaaaaaaaaaaa\n" 4951 " : aaaaaaaaaaaaaaa;"); 4952 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4953 " aaaaaaaaa\n" 4954 " ? b\n" 4955 " : c);"); 4956 verifyFormat("return aaaa == bbbb\n" 4957 " // comment\n" 4958 " ? aaaa\n" 4959 " : bbbb;"); 4960 verifyFormat("unsigned Indent =\n" 4961 " format(TheLine.First,\n" 4962 " IndentForLevel[TheLine.Level] >= 0\n" 4963 " ? IndentForLevel[TheLine.Level]\n" 4964 " : TheLine * 2,\n" 4965 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4966 getLLVMStyleWithColumns(60)); 4967 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4968 " ? aaaaaaaaaaaaaaa\n" 4969 " : bbbbbbbbbbbbbbb //\n" 4970 " ? ccccccccccccccc\n" 4971 " : ddddddddddddddd;"); 4972 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4973 " ? aaaaaaaaaaaaaaa\n" 4974 " : (bbbbbbbbbbbbbbb //\n" 4975 " ? ccccccccccccccc\n" 4976 " : ddddddddddddddd);"); 4977 verifyFormat( 4978 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4979 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4980 " aaaaaaaaaaaaaaaaaaaaa +\n" 4981 " aaaaaaaaaaaaaaaaaaaaa\n" 4982 " : aaaaaaaaaa;"); 4983 verifyFormat( 4984 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4985 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4986 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4987 4988 FormatStyle NoBinPacking = getLLVMStyle(); 4989 NoBinPacking.BinPackArguments = false; 4990 verifyFormat( 4991 "void f() {\n" 4992 " g(aaa,\n" 4993 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4994 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4995 " ? aaaaaaaaaaaaaaa\n" 4996 " : aaaaaaaaaaaaaaa);\n" 4997 "}", 4998 NoBinPacking); 4999 verifyFormat( 5000 "void f() {\n" 5001 " g(aaa,\n" 5002 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 5003 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5004 " ?: aaaaaaaaaaaaaaa);\n" 5005 "}", 5006 NoBinPacking); 5007 5008 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 5009 " // comment.\n" 5010 " ccccccccccccccccccccccccccccccccccccccc\n" 5011 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5012 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 5013 5014 // Assignments in conditional expressions. Apparently not uncommon :-(. 5015 verifyFormat("return a != b\n" 5016 " // comment\n" 5017 " ? a = b\n" 5018 " : a = b;"); 5019 verifyFormat("return a != b\n" 5020 " // comment\n" 5021 " ? a = a != b\n" 5022 " // comment\n" 5023 " ? a = b\n" 5024 " : a\n" 5025 " : a;\n"); 5026 verifyFormat("return a != b\n" 5027 " // comment\n" 5028 " ? a\n" 5029 " : a = a != b\n" 5030 " // comment\n" 5031 " ? a = b\n" 5032 " : a;"); 5033 } 5034 5035 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 5036 FormatStyle Style = getLLVMStyle(); 5037 Style.BreakBeforeTernaryOperators = false; 5038 Style.ColumnLimit = 70; 5039 verifyFormat( 5040 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5041 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5042 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5043 Style); 5044 verifyFormat( 5045 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 5046 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5048 Style); 5049 verifyFormat( 5050 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5051 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5052 Style); 5053 verifyFormat( 5054 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 5055 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5057 Style); 5058 verifyFormat( 5059 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 5060 " aaaaaaaaaaaaa);", 5061 Style); 5062 verifyFormat( 5063 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5064 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5066 " aaaaaaaaaaaaa);", 5067 Style); 5068 verifyFormat( 5069 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5070 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5071 " aaaaaaaaaaaaa);", 5072 Style); 5073 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5075 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5077 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5078 Style); 5079 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5080 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5081 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5082 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5083 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5084 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5085 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5086 Style); 5087 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5088 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 5089 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5091 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5092 Style); 5093 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5094 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5095 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5096 Style); 5097 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 5098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5099 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5100 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5101 Style); 5102 verifyFormat( 5103 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5104 " aaaaaaaaaaaaaaa :\n" 5105 " aaaaaaaaaaaaaaa;", 5106 Style); 5107 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 5108 " aaaaaaaaa ?\n" 5109 " b :\n" 5110 " c);", 5111 Style); 5112 verifyFormat("unsigned Indent =\n" 5113 " format(TheLine.First,\n" 5114 " IndentForLevel[TheLine.Level] >= 0 ?\n" 5115 " IndentForLevel[TheLine.Level] :\n" 5116 " TheLine * 2,\n" 5117 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 5118 Style); 5119 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 5120 " aaaaaaaaaaaaaaa :\n" 5121 " bbbbbbbbbbbbbbb ? //\n" 5122 " ccccccccccccccc :\n" 5123 " ddddddddddddddd;", 5124 Style); 5125 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 5126 " aaaaaaaaaaaaaaa :\n" 5127 " (bbbbbbbbbbbbbbb ? //\n" 5128 " ccccccccccccccc :\n" 5129 " ddddddddddddddd);", 5130 Style); 5131 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5132 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 5133 " ccccccccccccccccccccccccccc;", 5134 Style); 5135 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5136 " aaaaa :\n" 5137 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 5138 Style); 5139 } 5140 5141 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 5142 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 5143 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 5144 verifyFormat("bool a = true, b = false;"); 5145 5146 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5147 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 5148 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 5149 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 5150 verifyFormat( 5151 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5152 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 5153 " d = e && f;"); 5154 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 5155 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 5156 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 5157 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 5158 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 5159 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 5160 5161 FormatStyle Style = getGoogleStyle(); 5162 Style.PointerAlignment = FormatStyle::PAS_Left; 5163 Style.DerivePointerAlignment = false; 5164 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5165 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 5166 " *b = bbbbbbbbbbbbbbbbbbb;", 5167 Style); 5168 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 5169 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 5170 Style); 5171 verifyFormat("vector<int*> a, b;", Style); 5172 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 5173 } 5174 5175 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 5176 verifyFormat("arr[foo ? bar : baz];"); 5177 verifyFormat("f()[foo ? bar : baz];"); 5178 verifyFormat("(a + b)[foo ? bar : baz];"); 5179 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 5180 } 5181 5182 TEST_F(FormatTest, AlignsStringLiterals) { 5183 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 5184 " \"short literal\");"); 5185 verifyFormat( 5186 "looooooooooooooooooooooooongFunction(\n" 5187 " \"short literal\"\n" 5188 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 5189 verifyFormat("someFunction(\"Always break between multi-line\"\n" 5190 " \" string literals\",\n" 5191 " and, other, parameters);"); 5192 EXPECT_EQ("fun + \"1243\" /* comment */\n" 5193 " \"5678\";", 5194 format("fun + \"1243\" /* comment */\n" 5195 " \"5678\";", 5196 getLLVMStyleWithColumns(28))); 5197 EXPECT_EQ( 5198 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 5199 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 5200 " \"aaaaaaaaaaaaaaaa\";", 5201 format("aaaaaa =" 5202 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 5203 "aaaaaaaaaaaaaaaaaaaaa\" " 5204 "\"aaaaaaaaaaaaaaaa\";")); 5205 verifyFormat("a = a + \"a\"\n" 5206 " \"a\"\n" 5207 " \"a\";"); 5208 verifyFormat("f(\"a\", \"b\"\n" 5209 " \"c\");"); 5210 5211 verifyFormat( 5212 "#define LL_FORMAT \"ll\"\n" 5213 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 5214 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 5215 5216 verifyFormat("#define A(X) \\\n" 5217 " \"aaaaa\" #X \"bbbbbb\" \\\n" 5218 " \"ccccc\"", 5219 getLLVMStyleWithColumns(23)); 5220 verifyFormat("#define A \"def\"\n" 5221 "f(\"abc\" A \"ghi\"\n" 5222 " \"jkl\");"); 5223 5224 verifyFormat("f(L\"a\"\n" 5225 " L\"b\");"); 5226 verifyFormat("#define A(X) \\\n" 5227 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 5228 " L\"ccccc\"", 5229 getLLVMStyleWithColumns(25)); 5230 5231 verifyFormat("f(@\"a\"\n" 5232 " @\"b\");"); 5233 verifyFormat("NSString s = @\"a\"\n" 5234 " @\"b\"\n" 5235 " @\"c\";"); 5236 verifyFormat("NSString s = @\"a\"\n" 5237 " \"b\"\n" 5238 " \"c\";"); 5239 } 5240 5241 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 5242 FormatStyle Style = getLLVMStyle(); 5243 // No declarations or definitions should be moved to own line. 5244 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 5245 verifyFormat("class A {\n" 5246 " int f() { return 1; }\n" 5247 " int g();\n" 5248 "};\n" 5249 "int f() { return 1; }\n" 5250 "int g();\n", 5251 Style); 5252 5253 // All declarations and definitions should have the return type moved to its 5254 // own 5255 // line. 5256 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 5257 verifyFormat("class E {\n" 5258 " int\n" 5259 " f() {\n" 5260 " return 1;\n" 5261 " }\n" 5262 " int\n" 5263 " g();\n" 5264 "};\n" 5265 "int\n" 5266 "f() {\n" 5267 " return 1;\n" 5268 "}\n" 5269 "int\n" 5270 "g();\n", 5271 Style); 5272 5273 // Top-level definitions, and no kinds of declarations should have the 5274 // return type moved to its own line. 5275 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 5276 verifyFormat("class B {\n" 5277 " int f() { return 1; }\n" 5278 " int g();\n" 5279 "};\n" 5280 "int\n" 5281 "f() {\n" 5282 " return 1;\n" 5283 "}\n" 5284 "int g();\n", 5285 Style); 5286 5287 // Top-level definitions and declarations should have the return type moved 5288 // to its own line. 5289 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 5290 verifyFormat("class C {\n" 5291 " int f() { return 1; }\n" 5292 " int g();\n" 5293 "};\n" 5294 "int\n" 5295 "f() {\n" 5296 " return 1;\n" 5297 "}\n" 5298 "int\n" 5299 "g();\n", 5300 Style); 5301 5302 // All definitions should have the return type moved to its own line, but no 5303 // kinds of declarations. 5304 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5305 verifyFormat("class D {\n" 5306 " int\n" 5307 " f() {\n" 5308 " return 1;\n" 5309 " }\n" 5310 " int g();\n" 5311 "};\n" 5312 "int\n" 5313 "f() {\n" 5314 " return 1;\n" 5315 "}\n" 5316 "int g();\n", 5317 Style); 5318 verifyFormat("const char *\n" 5319 "f(void) {\n" // Break here. 5320 " return \"\";\n" 5321 "}\n" 5322 "const char *bar(void);\n", // No break here. 5323 Style); 5324 verifyFormat("template <class T>\n" 5325 "T *\n" 5326 "f(T &c) {\n" // Break here. 5327 " return NULL;\n" 5328 "}\n" 5329 "template <class T> T *f(T &c);\n", // No break here. 5330 Style); 5331 verifyFormat("class C {\n" 5332 " int\n" 5333 " operator+() {\n" 5334 " return 1;\n" 5335 " }\n" 5336 " int\n" 5337 " operator()() {\n" 5338 " return 1;\n" 5339 " }\n" 5340 "};\n", 5341 Style); 5342 verifyFormat("void\n" 5343 "A::operator()() {}\n" 5344 "void\n" 5345 "A::operator>>() {}\n" 5346 "void\n" 5347 "A::operator+() {}\n", 5348 Style); 5349 verifyFormat("void *operator new(std::size_t s);", // No break here. 5350 Style); 5351 verifyFormat("void *\n" 5352 "operator new(std::size_t s) {}", 5353 Style); 5354 verifyFormat("void *\n" 5355 "operator delete[](void *ptr) {}", 5356 Style); 5357 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 5358 verifyFormat("const char *\n" 5359 "f(void)\n" // Break here. 5360 "{\n" 5361 " return \"\";\n" 5362 "}\n" 5363 "const char *bar(void);\n", // No break here. 5364 Style); 5365 verifyFormat("template <class T>\n" 5366 "T *\n" // Problem here: no line break 5367 "f(T &c)\n" // Break here. 5368 "{\n" 5369 " return NULL;\n" 5370 "}\n" 5371 "template <class T> T *f(T &c);\n", // No break here. 5372 Style); 5373 } 5374 5375 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 5376 FormatStyle NoBreak = getLLVMStyle(); 5377 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 5378 FormatStyle Break = getLLVMStyle(); 5379 Break.AlwaysBreakBeforeMultilineStrings = true; 5380 verifyFormat("aaaa = \"bbbb\"\n" 5381 " \"cccc\";", 5382 NoBreak); 5383 verifyFormat("aaaa =\n" 5384 " \"bbbb\"\n" 5385 " \"cccc\";", 5386 Break); 5387 verifyFormat("aaaa(\"bbbb\"\n" 5388 " \"cccc\");", 5389 NoBreak); 5390 verifyFormat("aaaa(\n" 5391 " \"bbbb\"\n" 5392 " \"cccc\");", 5393 Break); 5394 verifyFormat("aaaa(qqq, \"bbbb\"\n" 5395 " \"cccc\");", 5396 NoBreak); 5397 verifyFormat("aaaa(qqq,\n" 5398 " \"bbbb\"\n" 5399 " \"cccc\");", 5400 Break); 5401 verifyFormat("aaaa(qqq,\n" 5402 " L\"bbbb\"\n" 5403 " L\"cccc\");", 5404 Break); 5405 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 5406 " \"bbbb\"));", 5407 Break); 5408 verifyFormat("string s = someFunction(\n" 5409 " \"abc\"\n" 5410 " \"abc\");", 5411 Break); 5412 5413 // As we break before unary operators, breaking right after them is bad. 5414 verifyFormat("string foo = abc ? \"x\"\n" 5415 " \"blah blah blah blah blah blah\"\n" 5416 " : \"y\";", 5417 Break); 5418 5419 // Don't break if there is no column gain. 5420 verifyFormat("f(\"aaaa\"\n" 5421 " \"bbbb\");", 5422 Break); 5423 5424 // Treat literals with escaped newlines like multi-line string literals. 5425 EXPECT_EQ("x = \"a\\\n" 5426 "b\\\n" 5427 "c\";", 5428 format("x = \"a\\\n" 5429 "b\\\n" 5430 "c\";", 5431 NoBreak)); 5432 EXPECT_EQ("xxxx =\n" 5433 " \"a\\\n" 5434 "b\\\n" 5435 "c\";", 5436 format("xxxx = \"a\\\n" 5437 "b\\\n" 5438 "c\";", 5439 Break)); 5440 5441 EXPECT_EQ("NSString *const kString =\n" 5442 " @\"aaaa\"\n" 5443 " @\"bbbb\";", 5444 format("NSString *const kString = @\"aaaa\"\n" 5445 "@\"bbbb\";", 5446 Break)); 5447 5448 Break.ColumnLimit = 0; 5449 verifyFormat("const char *hello = \"hello llvm\";", Break); 5450 } 5451 5452 TEST_F(FormatTest, AlignsPipes) { 5453 verifyFormat( 5454 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5455 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5456 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5457 verifyFormat( 5458 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5459 " << aaaaaaaaaaaaaaaaaaaa;"); 5460 verifyFormat( 5461 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5462 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5463 verifyFormat( 5464 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5465 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5466 verifyFormat( 5467 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5468 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5469 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5470 verifyFormat( 5471 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5472 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5473 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5474 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5475 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5476 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5477 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5478 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5479 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5480 verifyFormat( 5481 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5482 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5483 verifyFormat( 5484 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 5485 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5486 5487 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5488 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5489 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5490 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5491 " aaaaaaaaaaaaaaaaaaaaa)\n" 5492 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5493 verifyFormat("LOG_IF(aaa == //\n" 5494 " bbb)\n" 5495 " << a << b;"); 5496 5497 // But sometimes, breaking before the first "<<" is desirable. 5498 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5499 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5500 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5501 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5502 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5503 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5504 " << BEF << IsTemplate << Description << E->getType();"); 5505 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5506 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5507 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5508 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5509 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5510 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5511 " << aaa;"); 5512 5513 verifyFormat( 5514 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5515 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5516 5517 // Incomplete string literal. 5518 EXPECT_EQ("llvm::errs() << \"\n" 5519 " << a;", 5520 format("llvm::errs() << \"\n<<a;")); 5521 5522 verifyFormat("void f() {\n" 5523 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5524 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5525 "}"); 5526 5527 // Handle 'endl'. 5528 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5529 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5530 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5531 5532 // Handle '\n'. 5533 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5534 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5535 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5536 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5537 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5538 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5539 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5540 } 5541 5542 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 5543 verifyFormat("return out << \"somepacket = {\\n\"\n" 5544 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5545 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5546 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5547 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5548 " << \"}\";"); 5549 5550 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5551 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5552 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5553 verifyFormat( 5554 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5555 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5556 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5557 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5558 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5559 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5560 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5561 verifyFormat( 5562 "void f() {\n" 5563 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5564 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5565 "}"); 5566 5567 // Breaking before the first "<<" is generally not desirable. 5568 verifyFormat( 5569 "llvm::errs()\n" 5570 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5571 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5572 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5573 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5574 getLLVMStyleWithColumns(70)); 5575 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5576 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5577 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5578 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5579 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5580 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5581 getLLVMStyleWithColumns(70)); 5582 5583 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5584 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5585 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 5586 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5587 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5588 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 5589 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 5590 " (aaaa + aaaa);", 5591 getLLVMStyleWithColumns(40)); 5592 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 5593 " (aaaaaaa + aaaaa));", 5594 getLLVMStyleWithColumns(40)); 5595 verifyFormat( 5596 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 5597 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 5598 " bbbbbbbbbbbbbbbbbbbbbbb);"); 5599 } 5600 5601 TEST_F(FormatTest, UnderstandsEquals) { 5602 verifyFormat( 5603 "aaaaaaaaaaaaaaaaa =\n" 5604 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5605 verifyFormat( 5606 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5607 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5608 verifyFormat( 5609 "if (a) {\n" 5610 " f();\n" 5611 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5612 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5613 "}"); 5614 5615 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5616 " 100000000 + 10000000) {\n}"); 5617 } 5618 5619 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5620 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5621 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5622 5623 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5624 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5625 5626 verifyFormat( 5627 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5628 " Parameter2);"); 5629 5630 verifyFormat( 5631 "ShortObject->shortFunction(\n" 5632 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5633 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5634 5635 verifyFormat("loooooooooooooongFunction(\n" 5636 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5637 5638 verifyFormat( 5639 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5640 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5641 5642 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5643 " .WillRepeatedly(Return(SomeValue));"); 5644 verifyFormat("void f() {\n" 5645 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5646 " .Times(2)\n" 5647 " .WillRepeatedly(Return(SomeValue));\n" 5648 "}"); 5649 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5650 " ccccccccccccccccccccccc);"); 5651 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5652 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5653 " .aaaaa(aaaaa),\n" 5654 " aaaaaaaaaaaaaaaaaaaaa);"); 5655 verifyFormat("void f() {\n" 5656 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5657 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5658 "}"); 5659 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5660 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5661 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5662 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5663 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5664 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5665 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5666 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5667 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5668 "}"); 5669 5670 // Here, it is not necessary to wrap at "." or "->". 5671 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5672 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5673 verifyFormat( 5674 "aaaaaaaaaaa->aaaaaaaaa(\n" 5675 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5676 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5677 5678 verifyFormat( 5679 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5681 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5682 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5683 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5684 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5685 5686 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5687 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5688 " .a();"); 5689 5690 FormatStyle NoBinPacking = getLLVMStyle(); 5691 NoBinPacking.BinPackParameters = false; 5692 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5693 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5694 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5695 " aaaaaaaaaaaaaaaaaaa,\n" 5696 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5697 NoBinPacking); 5698 5699 // If there is a subsequent call, change to hanging indentation. 5700 verifyFormat( 5701 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5702 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5703 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5704 verifyFormat( 5705 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5706 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5707 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5708 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5709 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5710 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5711 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5712 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5713 } 5714 5715 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5716 verifyFormat("template <typename T>\n" 5717 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5718 verifyFormat("template <typename T>\n" 5719 "// T should be one of {A, B}.\n" 5720 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5721 verifyFormat( 5722 "template <typename T>\n" 5723 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5724 verifyFormat("template <typename T>\n" 5725 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5726 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5727 verifyFormat( 5728 "template <typename T>\n" 5729 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5730 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5731 verifyFormat( 5732 "template <typename T>\n" 5733 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5734 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5735 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5736 verifyFormat("template <typename T>\n" 5737 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5738 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5739 verifyFormat( 5740 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5741 " typename T4 = char>\n" 5742 "void f();"); 5743 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5744 " template <typename> class cccccccccccccccccccccc,\n" 5745 " typename ddddddddddddd>\n" 5746 "class C {};"); 5747 verifyFormat( 5748 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5750 5751 verifyFormat("void f() {\n" 5752 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5753 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5754 "}"); 5755 5756 verifyFormat("template <typename T> class C {};"); 5757 verifyFormat("template <typename T> void f();"); 5758 verifyFormat("template <typename T> void f() {}"); 5759 verifyFormat( 5760 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5761 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5762 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5763 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5764 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5766 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5767 getLLVMStyleWithColumns(72)); 5768 EXPECT_EQ("static_cast<A< //\n" 5769 " B> *>(\n" 5770 "\n" 5771 ");", 5772 format("static_cast<A<//\n" 5773 " B>*>(\n" 5774 "\n" 5775 " );")); 5776 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5777 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5778 5779 FormatStyle AlwaysBreak = getLLVMStyle(); 5780 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 5781 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5782 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5783 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5784 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5785 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5786 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5787 verifyFormat("template <template <typename> class Fooooooo,\n" 5788 " template <typename> class Baaaaaaar>\n" 5789 "struct C {};", 5790 AlwaysBreak); 5791 verifyFormat("template <typename T> // T can be A, B or C.\n" 5792 "struct C {};", 5793 AlwaysBreak); 5794 verifyFormat("template <enum E> class A {\n" 5795 "public:\n" 5796 " E *f();\n" 5797 "};"); 5798 5799 FormatStyle NeverBreak = getLLVMStyle(); 5800 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 5801 verifyFormat("template <typename T> class C {};", NeverBreak); 5802 verifyFormat("template <typename T> void f();", NeverBreak); 5803 verifyFormat("template <typename T> void f() {}", NeverBreak); 5804 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}", 5805 NeverBreak); 5806 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5807 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5808 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 5809 NeverBreak); 5810 verifyFormat("template <template <typename> class Fooooooo,\n" 5811 " template <typename> class Baaaaaaar>\n" 5812 "struct C {};", 5813 NeverBreak); 5814 verifyFormat("template <typename T> // T can be A, B or C.\n" 5815 "struct C {};", 5816 NeverBreak); 5817 verifyFormat("template <enum E> class A {\n" 5818 "public:\n" 5819 " E *f();\n" 5820 "};", NeverBreak); 5821 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 5822 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}", 5823 NeverBreak); 5824 } 5825 5826 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 5827 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 5828 Style.ColumnLimit = 60; 5829 EXPECT_EQ("// Baseline - no comments.\n" 5830 "template <\n" 5831 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 5832 "void f() {}", 5833 format("// Baseline - no comments.\n" 5834 "template <\n" 5835 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 5836 "void f() {}", 5837 Style)); 5838 5839 EXPECT_EQ("template <\n" 5840 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 5841 "void f() {}", 5842 format("template <\n" 5843 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 5844 "void f() {}", 5845 Style)); 5846 5847 EXPECT_EQ( 5848 "template <\n" 5849 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 5850 "void f() {}", 5851 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 5852 "void f() {}", 5853 Style)); 5854 5855 EXPECT_EQ( 5856 "template <\n" 5857 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 5858 " // multiline\n" 5859 "void f() {}", 5860 format("template <\n" 5861 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 5862 " // multiline\n" 5863 "void f() {}", 5864 Style)); 5865 5866 EXPECT_EQ( 5867 "template <typename aaaaaaaaaa<\n" 5868 " bbbbbbbbbbbb>::value> // trailing loooong\n" 5869 "void f() {}", 5870 format( 5871 "template <\n" 5872 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 5873 "void f() {}", 5874 Style)); 5875 } 5876 5877 TEST_F(FormatTest, WrapsTemplateParameters) { 5878 FormatStyle Style = getLLVMStyle(); 5879 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5880 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5881 verifyFormat( 5882 "template <typename... a> struct q {};\n" 5883 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5884 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5885 " y;", 5886 Style); 5887 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5888 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5889 verifyFormat( 5890 "template <typename... a> struct r {};\n" 5891 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5892 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5893 " y;", 5894 Style); 5895 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5896 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5897 verifyFormat( 5898 "template <typename... a> struct s {};\n" 5899 "extern s<\n" 5900 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5901 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5902 " y;", 5903 Style); 5904 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5905 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5906 verifyFormat( 5907 "template <typename... a> struct t {};\n" 5908 "extern t<\n" 5909 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5910 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5911 " y;", 5912 Style); 5913 } 5914 5915 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5916 verifyFormat( 5917 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5918 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5919 verifyFormat( 5920 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5922 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5923 5924 // FIXME: Should we have the extra indent after the second break? 5925 verifyFormat( 5926 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5927 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5928 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5929 5930 verifyFormat( 5931 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5932 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5933 5934 // Breaking at nested name specifiers is generally not desirable. 5935 verifyFormat( 5936 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5937 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5938 5939 verifyFormat( 5940 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 5941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5942 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5943 " aaaaaaaaaaaaaaaaaaaaa);", 5944 getLLVMStyleWithColumns(74)); 5945 5946 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5947 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5948 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5949 } 5950 5951 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5952 verifyFormat("A<int> a;"); 5953 verifyFormat("A<A<A<int>>> a;"); 5954 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5955 verifyFormat("bool x = a < 1 || 2 > a;"); 5956 verifyFormat("bool x = 5 < f<int>();"); 5957 verifyFormat("bool x = f<int>() > 5;"); 5958 verifyFormat("bool x = 5 < a<int>::x;"); 5959 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5960 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5961 5962 verifyGoogleFormat("A<A<int>> a;"); 5963 verifyGoogleFormat("A<A<A<int>>> a;"); 5964 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5965 verifyGoogleFormat("A<A<int> > a;"); 5966 verifyGoogleFormat("A<A<A<int> > > a;"); 5967 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5968 verifyGoogleFormat("A<::A<int>> a;"); 5969 verifyGoogleFormat("A<::A> a;"); 5970 verifyGoogleFormat("A< ::A> a;"); 5971 verifyGoogleFormat("A< ::A<int> > a;"); 5972 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5973 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5974 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5975 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5976 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5977 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5978 5979 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5980 5981 verifyFormat("test >> a >> b;"); 5982 verifyFormat("test << a >> b;"); 5983 5984 verifyFormat("f<int>();"); 5985 verifyFormat("template <typename T> void f() {}"); 5986 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5987 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5988 "sizeof(char)>::type>;"); 5989 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5990 verifyFormat("f(a.operator()<A>());"); 5991 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5992 " .template operator()<A>());", 5993 getLLVMStyleWithColumns(35)); 5994 5995 // Not template parameters. 5996 verifyFormat("return a < b && c > d;"); 5997 verifyFormat("void f() {\n" 5998 " while (a < b && c > d) {\n" 5999 " }\n" 6000 "}"); 6001 verifyFormat("template <typename... Types>\n" 6002 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 6003 6004 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6005 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 6006 getLLVMStyleWithColumns(60)); 6007 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 6008 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 6009 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 6010 } 6011 6012 TEST_F(FormatTest, BitshiftOperatorWidth) { 6013 EXPECT_EQ("int a = 1 << 2; /* foo\n" 6014 " bar */", 6015 format("int a=1<<2; /* foo\n" 6016 " bar */")); 6017 6018 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 6019 " bar */", 6020 format("int b =256>>1 ; /* foo\n" 6021 " bar */")); 6022 } 6023 6024 TEST_F(FormatTest, UnderstandsBinaryOperators) { 6025 verifyFormat("COMPARE(a, ==, b);"); 6026 verifyFormat("auto s = sizeof...(Ts) - 1;"); 6027 } 6028 6029 TEST_F(FormatTest, UnderstandsPointersToMembers) { 6030 verifyFormat("int A::*x;"); 6031 verifyFormat("int (S::*func)(void *);"); 6032 verifyFormat("void f() { int (S::*func)(void *); }"); 6033 verifyFormat("typedef bool *(Class::*Member)() const;"); 6034 verifyFormat("void f() {\n" 6035 " (a->*f)();\n" 6036 " a->*x;\n" 6037 " (a.*f)();\n" 6038 " ((*a).*f)();\n" 6039 " a.*x;\n" 6040 "}"); 6041 verifyFormat("void f() {\n" 6042 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6043 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 6044 "}"); 6045 verifyFormat( 6046 "(aaaaaaaaaa->*bbbbbbb)(\n" 6047 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6048 FormatStyle Style = getLLVMStyle(); 6049 Style.PointerAlignment = FormatStyle::PAS_Left; 6050 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 6051 } 6052 6053 TEST_F(FormatTest, UnderstandsUnaryOperators) { 6054 verifyFormat("int a = -2;"); 6055 verifyFormat("f(-1, -2, -3);"); 6056 verifyFormat("a[-1] = 5;"); 6057 verifyFormat("int a = 5 + -2;"); 6058 verifyFormat("if (i == -1) {\n}"); 6059 verifyFormat("if (i != -1) {\n}"); 6060 verifyFormat("if (i > -1) {\n}"); 6061 verifyFormat("if (i < -1) {\n}"); 6062 verifyFormat("++(a->f());"); 6063 verifyFormat("--(a->f());"); 6064 verifyFormat("(a->f())++;"); 6065 verifyFormat("a[42]++;"); 6066 verifyFormat("if (!(a->f())) {\n}"); 6067 verifyFormat("if (!+i) {\n}"); 6068 verifyFormat("~&a;"); 6069 6070 verifyFormat("a-- > b;"); 6071 verifyFormat("b ? -a : c;"); 6072 verifyFormat("n * sizeof char16;"); 6073 verifyFormat("n * alignof char16;", getGoogleStyle()); 6074 verifyFormat("sizeof(char);"); 6075 verifyFormat("alignof(char);", getGoogleStyle()); 6076 6077 verifyFormat("return -1;"); 6078 verifyFormat("switch (a) {\n" 6079 "case -1:\n" 6080 " break;\n" 6081 "}"); 6082 verifyFormat("#define X -1"); 6083 verifyFormat("#define X -kConstant"); 6084 6085 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 6086 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 6087 6088 verifyFormat("int a = /* confusing comment */ -1;"); 6089 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 6090 verifyFormat("int a = i /* confusing comment */++;"); 6091 } 6092 6093 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 6094 verifyFormat("if (!aaaaaaaaaa( // break\n" 6095 " aaaaa)) {\n" 6096 "}"); 6097 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 6098 " aaaaa));"); 6099 verifyFormat("*aaa = aaaaaaa( // break\n" 6100 " bbbbbb);"); 6101 } 6102 6103 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 6104 verifyFormat("bool operator<();"); 6105 verifyFormat("bool operator>();"); 6106 verifyFormat("bool operator=();"); 6107 verifyFormat("bool operator==();"); 6108 verifyFormat("bool operator!=();"); 6109 verifyFormat("int operator+();"); 6110 verifyFormat("int operator++();"); 6111 verifyFormat("int operator++(int) volatile noexcept;"); 6112 verifyFormat("bool operator,();"); 6113 verifyFormat("bool operator();"); 6114 verifyFormat("bool operator()();"); 6115 verifyFormat("bool operator[]();"); 6116 verifyFormat("operator bool();"); 6117 verifyFormat("operator int();"); 6118 verifyFormat("operator void *();"); 6119 verifyFormat("operator SomeType<int>();"); 6120 verifyFormat("operator SomeType<int, int>();"); 6121 verifyFormat("operator SomeType<SomeType<int>>();"); 6122 verifyFormat("void *operator new(std::size_t size);"); 6123 verifyFormat("void *operator new[](std::size_t size);"); 6124 verifyFormat("void operator delete(void *ptr);"); 6125 verifyFormat("void operator delete[](void *ptr);"); 6126 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 6127 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 6128 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 6129 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 6130 6131 verifyFormat( 6132 "ostream &operator<<(ostream &OutputStream,\n" 6133 " SomeReallyLongType WithSomeReallyLongValue);"); 6134 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 6135 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 6136 " return left.group < right.group;\n" 6137 "}"); 6138 verifyFormat("SomeType &operator=(const SomeType &S);"); 6139 verifyFormat("f.template operator()<int>();"); 6140 6141 verifyGoogleFormat("operator void*();"); 6142 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 6143 verifyGoogleFormat("operator ::A();"); 6144 6145 verifyFormat("using A::operator+;"); 6146 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 6147 "int i;"); 6148 } 6149 6150 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 6151 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 6152 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 6153 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 6154 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 6155 verifyFormat("Deleted &operator=(const Deleted &) &;"); 6156 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 6157 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 6158 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 6159 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 6160 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 6161 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 6162 verifyFormat("void Fn(T const &) const &;"); 6163 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 6164 verifyFormat("template <typename T>\n" 6165 "void F(T) && = delete;", 6166 getGoogleStyle()); 6167 6168 FormatStyle AlignLeft = getLLVMStyle(); 6169 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 6170 verifyFormat("void A::b() && {}", AlignLeft); 6171 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 6172 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 6173 AlignLeft); 6174 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 6175 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 6176 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 6177 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 6178 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 6179 verifyFormat("auto Function(T) & -> void;", AlignLeft); 6180 verifyFormat("void Fn(T const&) const&;", AlignLeft); 6181 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 6182 6183 FormatStyle Spaces = getLLVMStyle(); 6184 Spaces.SpacesInCStyleCastParentheses = true; 6185 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 6186 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 6187 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 6188 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 6189 6190 Spaces.SpacesInCStyleCastParentheses = false; 6191 Spaces.SpacesInParentheses = true; 6192 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 6193 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 6194 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 6195 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 6196 } 6197 6198 TEST_F(FormatTest, UnderstandsNewAndDelete) { 6199 verifyFormat("void f() {\n" 6200 " A *a = new A;\n" 6201 " A *a = new (placement) A;\n" 6202 " delete a;\n" 6203 " delete (A *)a;\n" 6204 "}"); 6205 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 6206 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 6207 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6208 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 6209 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 6210 verifyFormat("delete[] h->p;"); 6211 } 6212 6213 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 6214 verifyFormat("int *f(int *a) {}"); 6215 verifyFormat("int main(int argc, char **argv) {}"); 6216 verifyFormat("Test::Test(int b) : a(b * b) {}"); 6217 verifyIndependentOfContext("f(a, *a);"); 6218 verifyFormat("void g() { f(*a); }"); 6219 verifyIndependentOfContext("int a = b * 10;"); 6220 verifyIndependentOfContext("int a = 10 * b;"); 6221 verifyIndependentOfContext("int a = b * c;"); 6222 verifyIndependentOfContext("int a += b * c;"); 6223 verifyIndependentOfContext("int a -= b * c;"); 6224 verifyIndependentOfContext("int a *= b * c;"); 6225 verifyIndependentOfContext("int a /= b * c;"); 6226 verifyIndependentOfContext("int a = *b;"); 6227 verifyIndependentOfContext("int a = *b * c;"); 6228 verifyIndependentOfContext("int a = b * *c;"); 6229 verifyIndependentOfContext("int a = b * (10);"); 6230 verifyIndependentOfContext("S << b * (10);"); 6231 verifyIndependentOfContext("return 10 * b;"); 6232 verifyIndependentOfContext("return *b * *c;"); 6233 verifyIndependentOfContext("return a & ~b;"); 6234 verifyIndependentOfContext("f(b ? *c : *d);"); 6235 verifyIndependentOfContext("int a = b ? *c : *d;"); 6236 verifyIndependentOfContext("*b = a;"); 6237 verifyIndependentOfContext("a * ~b;"); 6238 verifyIndependentOfContext("a * !b;"); 6239 verifyIndependentOfContext("a * +b;"); 6240 verifyIndependentOfContext("a * -b;"); 6241 verifyIndependentOfContext("a * ++b;"); 6242 verifyIndependentOfContext("a * --b;"); 6243 verifyIndependentOfContext("a[4] * b;"); 6244 verifyIndependentOfContext("a[a * a] = 1;"); 6245 verifyIndependentOfContext("f() * b;"); 6246 verifyIndependentOfContext("a * [self dostuff];"); 6247 verifyIndependentOfContext("int x = a * (a + b);"); 6248 verifyIndependentOfContext("(a *)(a + b);"); 6249 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 6250 verifyIndependentOfContext("int *pa = (int *)&a;"); 6251 verifyIndependentOfContext("return sizeof(int **);"); 6252 verifyIndependentOfContext("return sizeof(int ******);"); 6253 verifyIndependentOfContext("return (int **&)a;"); 6254 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 6255 verifyFormat("void f(Type (*parameter)[10]) {}"); 6256 verifyFormat("void f(Type (¶meter)[10]) {}"); 6257 verifyGoogleFormat("return sizeof(int**);"); 6258 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 6259 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 6260 verifyFormat("auto a = [](int **&, int ***) {};"); 6261 verifyFormat("auto PointerBinding = [](const char *S) {};"); 6262 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 6263 verifyFormat("[](const decltype(*a) &value) {}"); 6264 verifyFormat("decltype(a * b) F();"); 6265 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 6266 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 6267 verifyIndependentOfContext("typedef void (*f)(int *a);"); 6268 verifyIndependentOfContext("int i{a * b};"); 6269 verifyIndependentOfContext("aaa && aaa->f();"); 6270 verifyIndependentOfContext("int x = ~*p;"); 6271 verifyFormat("Constructor() : a(a), area(width * height) {}"); 6272 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 6273 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 6274 verifyFormat("void f() { f(a, c * d); }"); 6275 verifyFormat("void f() { f(new a(), c * d); }"); 6276 verifyFormat("void f(const MyOverride &override);"); 6277 verifyFormat("void f(const MyFinal &final);"); 6278 verifyIndependentOfContext("bool a = f() && override.f();"); 6279 verifyIndependentOfContext("bool a = f() && final.f();"); 6280 6281 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 6282 6283 verifyIndependentOfContext("A<int *> a;"); 6284 verifyIndependentOfContext("A<int **> a;"); 6285 verifyIndependentOfContext("A<int *, int *> a;"); 6286 verifyIndependentOfContext("A<int *[]> a;"); 6287 verifyIndependentOfContext( 6288 "const char *const p = reinterpret_cast<const char *const>(q);"); 6289 verifyIndependentOfContext("A<int **, int **> a;"); 6290 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 6291 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 6292 verifyFormat("for (; a && b;) {\n}"); 6293 verifyFormat("bool foo = true && [] { return false; }();"); 6294 6295 verifyFormat( 6296 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6297 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6298 6299 verifyGoogleFormat("int const* a = &b;"); 6300 verifyGoogleFormat("**outparam = 1;"); 6301 verifyGoogleFormat("*outparam = a * b;"); 6302 verifyGoogleFormat("int main(int argc, char** argv) {}"); 6303 verifyGoogleFormat("A<int*> a;"); 6304 verifyGoogleFormat("A<int**> a;"); 6305 verifyGoogleFormat("A<int*, int*> a;"); 6306 verifyGoogleFormat("A<int**, int**> a;"); 6307 verifyGoogleFormat("f(b ? *c : *d);"); 6308 verifyGoogleFormat("int a = b ? *c : *d;"); 6309 verifyGoogleFormat("Type* t = **x;"); 6310 verifyGoogleFormat("Type* t = *++*x;"); 6311 verifyGoogleFormat("*++*x;"); 6312 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 6313 verifyGoogleFormat("Type* t = x++ * y;"); 6314 verifyGoogleFormat( 6315 "const char* const p = reinterpret_cast<const char* const>(q);"); 6316 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 6317 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 6318 verifyGoogleFormat("template <typename T>\n" 6319 "void f(int i = 0, SomeType** temps = NULL);"); 6320 6321 FormatStyle Left = getLLVMStyle(); 6322 Left.PointerAlignment = FormatStyle::PAS_Left; 6323 verifyFormat("x = *a(x) = *a(y);", Left); 6324 verifyFormat("for (;; *a = b) {\n}", Left); 6325 verifyFormat("return *this += 1;", Left); 6326 verifyFormat("throw *x;", Left); 6327 verifyFormat("delete *x;", Left); 6328 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 6329 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 6330 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 6331 6332 verifyIndependentOfContext("a = *(x + y);"); 6333 verifyIndependentOfContext("a = &(x + y);"); 6334 verifyIndependentOfContext("*(x + y).call();"); 6335 verifyIndependentOfContext("&(x + y)->call();"); 6336 verifyFormat("void f() { &(*I).first; }"); 6337 6338 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 6339 verifyFormat( 6340 "int *MyValues = {\n" 6341 " *A, // Operator detection might be confused by the '{'\n" 6342 " *BB // Operator detection might be confused by previous comment\n" 6343 "};"); 6344 6345 verifyIndependentOfContext("if (int *a = &b)"); 6346 verifyIndependentOfContext("if (int &a = *b)"); 6347 verifyIndependentOfContext("if (a & b[i])"); 6348 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 6349 verifyIndependentOfContext("if (*b[i])"); 6350 verifyIndependentOfContext("if (int *a = (&b))"); 6351 verifyIndependentOfContext("while (int *a = &b)"); 6352 verifyIndependentOfContext("size = sizeof *a;"); 6353 verifyIndependentOfContext("if (a && (b = c))"); 6354 verifyFormat("void f() {\n" 6355 " for (const int &v : Values) {\n" 6356 " }\n" 6357 "}"); 6358 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 6359 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 6360 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 6361 6362 verifyFormat("#define A (!a * b)"); 6363 verifyFormat("#define MACRO \\\n" 6364 " int *i = a * b; \\\n" 6365 " void f(a *b);", 6366 getLLVMStyleWithColumns(19)); 6367 6368 verifyIndependentOfContext("A = new SomeType *[Length];"); 6369 verifyIndependentOfContext("A = new SomeType *[Length]();"); 6370 verifyIndependentOfContext("T **t = new T *;"); 6371 verifyIndependentOfContext("T **t = new T *();"); 6372 verifyGoogleFormat("A = new SomeType*[Length]();"); 6373 verifyGoogleFormat("A = new SomeType*[Length];"); 6374 verifyGoogleFormat("T** t = new T*;"); 6375 verifyGoogleFormat("T** t = new T*();"); 6376 6377 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 6378 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 6379 verifyFormat("template <bool a, bool b> " 6380 "typename t::if<x && y>::type f() {}"); 6381 verifyFormat("template <int *y> f() {}"); 6382 verifyFormat("vector<int *> v;"); 6383 verifyFormat("vector<int *const> v;"); 6384 verifyFormat("vector<int *const **const *> v;"); 6385 verifyFormat("vector<int *volatile> v;"); 6386 verifyFormat("vector<a * b> v;"); 6387 verifyFormat("foo<b && false>();"); 6388 verifyFormat("foo<b & 1>();"); 6389 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 6390 verifyFormat( 6391 "template <class T, class = typename std::enable_if<\n" 6392 " std::is_integral<T>::value &&\n" 6393 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 6394 "void F();", 6395 getLLVMStyleWithColumns(70)); 6396 verifyFormat( 6397 "template <class T,\n" 6398 " class = typename std::enable_if<\n" 6399 " std::is_integral<T>::value &&\n" 6400 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 6401 " class U>\n" 6402 "void F();", 6403 getLLVMStyleWithColumns(70)); 6404 verifyFormat( 6405 "template <class T,\n" 6406 " class = typename ::std::enable_if<\n" 6407 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 6408 "void F();", 6409 getGoogleStyleWithColumns(68)); 6410 6411 verifyIndependentOfContext("MACRO(int *i);"); 6412 verifyIndependentOfContext("MACRO(auto *a);"); 6413 verifyIndependentOfContext("MACRO(const A *a);"); 6414 verifyIndependentOfContext("MACRO(A *const a);"); 6415 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 6416 verifyFormat("void f() { f(float{1}, a * a); }"); 6417 // FIXME: Is there a way to make this work? 6418 // verifyIndependentOfContext("MACRO(A *a);"); 6419 6420 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 6421 verifyFormat("return options != nullptr && operator==(*options);"); 6422 6423 EXPECT_EQ("#define OP(x) \\\n" 6424 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6425 " return s << a.DebugString(); \\\n" 6426 " }", 6427 format("#define OP(x) \\\n" 6428 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6429 " return s << a.DebugString(); \\\n" 6430 " }", 6431 getLLVMStyleWithColumns(50))); 6432 6433 // FIXME: We cannot handle this case yet; we might be able to figure out that 6434 // foo<x> d > v; doesn't make sense. 6435 verifyFormat("foo<a<b && c> d> v;"); 6436 6437 FormatStyle PointerMiddle = getLLVMStyle(); 6438 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 6439 verifyFormat("delete *x;", PointerMiddle); 6440 verifyFormat("int * x;", PointerMiddle); 6441 verifyFormat("int *[] x;", PointerMiddle); 6442 verifyFormat("template <int * y> f() {}", PointerMiddle); 6443 verifyFormat("int * f(int * a) {}", PointerMiddle); 6444 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 6445 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 6446 verifyFormat("A<int *> a;", PointerMiddle); 6447 verifyFormat("A<int **> a;", PointerMiddle); 6448 verifyFormat("A<int *, int *> a;", PointerMiddle); 6449 verifyFormat("A<int *[]> a;", PointerMiddle); 6450 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 6451 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 6452 verifyFormat("T ** t = new T *;", PointerMiddle); 6453 6454 // Member function reference qualifiers aren't binary operators. 6455 verifyFormat("string // break\n" 6456 "operator()() & {}"); 6457 verifyFormat("string // break\n" 6458 "operator()() && {}"); 6459 verifyGoogleFormat("template <typename T>\n" 6460 "auto x() & -> int {}"); 6461 } 6462 6463 TEST_F(FormatTest, UnderstandsAttributes) { 6464 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 6465 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 6466 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 6467 FormatStyle AfterType = getLLVMStyle(); 6468 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 6469 verifyFormat("__attribute__((nodebug)) void\n" 6470 "foo() {}\n", 6471 AfterType); 6472 } 6473 6474 TEST_F(FormatTest, UnderstandsSquareAttributes) { 6475 verifyFormat("SomeType s [[unused]] (InitValue);"); 6476 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 6477 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 6478 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 6479 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 6480 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6481 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 6482 6483 // Make sure we do not mistake attributes for array subscripts. 6484 verifyFormat("int a() {}\n" 6485 "[[unused]] int b() {}\n"); 6486 verifyFormat("NSArray *arr;\n" 6487 "arr[[Foo() bar]];"); 6488 6489 // On the other hand, we still need to correctly find array subscripts. 6490 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 6491 6492 // Make sure we do not parse attributes as lambda introducers. 6493 FormatStyle MultiLineFunctions = getLLVMStyle(); 6494 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6495 verifyFormat("[[unused]] int b() {\n" 6496 " return 42;\n" 6497 "}\n", 6498 MultiLineFunctions); 6499 } 6500 6501 TEST_F(FormatTest, UnderstandsEllipsis) { 6502 verifyFormat("int printf(const char *fmt, ...);"); 6503 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 6504 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 6505 6506 FormatStyle PointersLeft = getLLVMStyle(); 6507 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 6508 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 6509 } 6510 6511 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 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("int* a;\n" 6520 "int* a;\n" 6521 "int* a;", 6522 format("int* a;\n" 6523 "int* a;\n" 6524 "int *a;", 6525 getGoogleStyle())); 6526 EXPECT_EQ("int *a;\n" 6527 "int *a;\n" 6528 "int *a;", 6529 format("int *a;\n" 6530 "int * a;\n" 6531 "int * a;", 6532 getGoogleStyle())); 6533 EXPECT_EQ("auto x = [] {\n" 6534 " int *a;\n" 6535 " int *a;\n" 6536 " int *a;\n" 6537 "};", 6538 format("auto x=[]{int *a;\n" 6539 "int * a;\n" 6540 "int * a;};", 6541 getGoogleStyle())); 6542 } 6543 6544 TEST_F(FormatTest, UnderstandsRvalueReferences) { 6545 verifyFormat("int f(int &&a) {}"); 6546 verifyFormat("int f(int a, char &&b) {}"); 6547 verifyFormat("void f() { int &&a = b; }"); 6548 verifyGoogleFormat("int f(int a, char&& b) {}"); 6549 verifyGoogleFormat("void f() { int&& a = b; }"); 6550 6551 verifyIndependentOfContext("A<int &&> a;"); 6552 verifyIndependentOfContext("A<int &&, int &&> a;"); 6553 verifyGoogleFormat("A<int&&> a;"); 6554 verifyGoogleFormat("A<int&&, int&&> a;"); 6555 6556 // Not rvalue references: 6557 verifyFormat("template <bool B, bool C> class A {\n" 6558 " static_assert(B && C, \"Something is wrong\");\n" 6559 "};"); 6560 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 6561 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 6562 verifyFormat("#define A(a, b) (a && b)"); 6563 } 6564 6565 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 6566 verifyFormat("void f() {\n" 6567 " x[aaaaaaaaa -\n" 6568 " b] = 23;\n" 6569 "}", 6570 getLLVMStyleWithColumns(15)); 6571 } 6572 6573 TEST_F(FormatTest, FormatsCasts) { 6574 verifyFormat("Type *A = static_cast<Type *>(P);"); 6575 verifyFormat("Type *A = (Type *)P;"); 6576 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6577 verifyFormat("int a = (int)(2.0f);"); 6578 verifyFormat("int a = (int)2.0f;"); 6579 verifyFormat("x[(int32)y];"); 6580 verifyFormat("x = (int32)y;"); 6581 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6582 verifyFormat("int a = (int)*b;"); 6583 verifyFormat("int a = (int)2.0f;"); 6584 verifyFormat("int a = (int)~0;"); 6585 verifyFormat("int a = (int)++a;"); 6586 verifyFormat("int a = (int)sizeof(int);"); 6587 verifyFormat("int a = (int)+2;"); 6588 verifyFormat("my_int a = (my_int)2.0f;"); 6589 verifyFormat("my_int a = (my_int)sizeof(int);"); 6590 verifyFormat("return (my_int)aaa;"); 6591 verifyFormat("#define x ((int)-1)"); 6592 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6593 verifyFormat("#define p(q) ((int *)&q)"); 6594 verifyFormat("fn(a)(b) + 1;"); 6595 6596 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6597 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6598 verifyFormat("my_int a = (my_int)~0;"); 6599 verifyFormat("my_int a = (my_int)++a;"); 6600 verifyFormat("my_int a = (my_int)-2;"); 6601 verifyFormat("my_int a = (my_int)1;"); 6602 verifyFormat("my_int a = (my_int *)1;"); 6603 verifyFormat("my_int a = (const my_int)-1;"); 6604 verifyFormat("my_int a = (const my_int *)-1;"); 6605 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6606 verifyFormat("my_int a = (ns::my_int)-2;"); 6607 verifyFormat("case (my_int)ONE:"); 6608 verifyFormat("auto x = (X)this;"); 6609 6610 // FIXME: single value wrapped with paren will be treated as cast. 6611 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6612 6613 verifyFormat("{ (void)F; }"); 6614 6615 // Don't break after a cast's 6616 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6617 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6618 " bbbbbbbbbbbbbbbbbbbbbb);"); 6619 6620 // These are not casts. 6621 verifyFormat("void f(int *) {}"); 6622 verifyFormat("f(foo)->b;"); 6623 verifyFormat("f(foo).b;"); 6624 verifyFormat("f(foo)(b);"); 6625 verifyFormat("f(foo)[b];"); 6626 verifyFormat("[](foo) { return 4; }(bar);"); 6627 verifyFormat("(*funptr)(foo)[4];"); 6628 verifyFormat("funptrs[4](foo)[4];"); 6629 verifyFormat("void f(int *);"); 6630 verifyFormat("void f(int *) = 0;"); 6631 verifyFormat("void f(SmallVector<int>) {}"); 6632 verifyFormat("void f(SmallVector<int>);"); 6633 verifyFormat("void f(SmallVector<int>) = 0;"); 6634 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6635 verifyFormat("int a = sizeof(int) * b;"); 6636 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6637 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6638 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6639 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6640 6641 // These are not casts, but at some point were confused with casts. 6642 verifyFormat("virtual void foo(int *) override;"); 6643 verifyFormat("virtual void foo(char &) const;"); 6644 verifyFormat("virtual void foo(int *a, char *) const;"); 6645 verifyFormat("int a = sizeof(int *) + b;"); 6646 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6647 verifyFormat("bool b = f(g<int>) && c;"); 6648 verifyFormat("typedef void (*f)(int i) func;"); 6649 6650 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6651 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6652 // FIXME: The indentation here is not ideal. 6653 verifyFormat( 6654 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6655 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6656 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6657 } 6658 6659 TEST_F(FormatTest, FormatsFunctionTypes) { 6660 verifyFormat("A<bool()> a;"); 6661 verifyFormat("A<SomeType()> a;"); 6662 verifyFormat("A<void (*)(int, std::string)> a;"); 6663 verifyFormat("A<void *(int)>;"); 6664 verifyFormat("void *(*a)(int *, SomeType *);"); 6665 verifyFormat("int (*func)(void *);"); 6666 verifyFormat("void f() { int (*func)(void *); }"); 6667 verifyFormat("template <class CallbackClass>\n" 6668 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6669 6670 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6671 verifyGoogleFormat("void* (*a)(int);"); 6672 verifyGoogleFormat( 6673 "template <class CallbackClass>\n" 6674 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6675 6676 // Other constructs can look somewhat like function types: 6677 verifyFormat("A<sizeof(*x)> a;"); 6678 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6679 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6680 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6681 verifyFormat("int x = f(&h)();"); 6682 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6683 verifyFormat("std::function<\n" 6684 " LooooooooooongTemplatedType<\n" 6685 " SomeType>*(\n" 6686 " LooooooooooooooooongType type)>\n" 6687 " function;", 6688 getGoogleStyleWithColumns(40)); 6689 } 6690 6691 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6692 verifyFormat("A (*foo_)[6];"); 6693 verifyFormat("vector<int> (*foo_)[6];"); 6694 } 6695 6696 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6697 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6698 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6699 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6700 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6701 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6702 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6703 6704 // Different ways of ()-initializiation. 6705 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6706 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6707 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6708 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6709 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6710 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6711 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6712 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6713 6714 // Lambdas should not confuse the variable declaration heuristic. 6715 verifyFormat("LooooooooooooooooongType\n" 6716 " variable(nullptr, [](A *a) {});", 6717 getLLVMStyleWithColumns(40)); 6718 } 6719 6720 TEST_F(FormatTest, BreaksLongDeclarations) { 6721 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6722 " AnotherNameForTheLongType;"); 6723 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6724 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6725 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6726 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6727 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6728 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6729 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6730 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6731 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6732 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6733 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6734 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6735 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6736 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6737 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6738 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6739 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6740 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6741 FormatStyle Indented = getLLVMStyle(); 6742 Indented.IndentWrappedFunctionNames = true; 6743 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6744 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6745 Indented); 6746 verifyFormat( 6747 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6748 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6749 Indented); 6750 verifyFormat( 6751 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6752 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6753 Indented); 6754 verifyFormat( 6755 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6756 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6757 Indented); 6758 6759 // FIXME: Without the comment, this breaks after "(". 6760 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6761 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6762 getGoogleStyle()); 6763 6764 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6765 " int LoooooooooooooooooooongParam2) {}"); 6766 verifyFormat( 6767 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6768 " SourceLocation L, IdentifierIn *II,\n" 6769 " Type *T) {}"); 6770 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6771 "ReallyReaaallyLongFunctionName(\n" 6772 " const std::string &SomeParameter,\n" 6773 " const SomeType<string, SomeOtherTemplateParameter>\n" 6774 " &ReallyReallyLongParameterName,\n" 6775 " const SomeType<string, SomeOtherTemplateParameter>\n" 6776 " &AnotherLongParameterName) {}"); 6777 verifyFormat("template <typename A>\n" 6778 "SomeLoooooooooooooooooooooongType<\n" 6779 " typename some_namespace::SomeOtherType<A>::Type>\n" 6780 "Function() {}"); 6781 6782 verifyGoogleFormat( 6783 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6784 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6785 verifyGoogleFormat( 6786 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6787 " SourceLocation L) {}"); 6788 verifyGoogleFormat( 6789 "some_namespace::LongReturnType\n" 6790 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6791 " int first_long_parameter, int second_parameter) {}"); 6792 6793 verifyGoogleFormat("template <typename T>\n" 6794 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6795 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6796 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6797 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6798 6799 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6800 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6801 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6802 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6803 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6804 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6805 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6806 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6807 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6808 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6809 6810 verifyFormat("template <typename T> // Templates on own line.\n" 6811 "static int // Some comment.\n" 6812 "MyFunction(int a);", 6813 getLLVMStyle()); 6814 } 6815 6816 TEST_F(FormatTest, FormatsArrays) { 6817 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6818 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6819 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6820 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6821 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6822 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6823 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6824 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6825 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6826 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6827 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6828 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6829 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6830 verifyFormat( 6831 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6832 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6833 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6834 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6835 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6836 6837 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6838 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6839 verifyFormat( 6840 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6841 " .aaaaaaa[0]\n" 6842 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6843 verifyFormat("a[::b::c];"); 6844 6845 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6846 6847 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6848 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6849 } 6850 6851 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6852 verifyFormat("(a)->b();"); 6853 verifyFormat("--a;"); 6854 } 6855 6856 TEST_F(FormatTest, HandlesIncludeDirectives) { 6857 verifyFormat("#include <string>\n" 6858 "#include <a/b/c.h>\n" 6859 "#include \"a/b/string\"\n" 6860 "#include \"string.h\"\n" 6861 "#include \"string.h\"\n" 6862 "#include <a-a>\n" 6863 "#include < path with space >\n" 6864 "#include_next <test.h>" 6865 "#include \"abc.h\" // this is included for ABC\n" 6866 "#include \"some long include\" // with a comment\n" 6867 "#include \"some very long include path\"\n" 6868 "#include <some/very/long/include/path>\n", 6869 getLLVMStyleWithColumns(35)); 6870 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6871 EXPECT_EQ("#include <a>", format("#include<a>")); 6872 6873 verifyFormat("#import <string>"); 6874 verifyFormat("#import <a/b/c.h>"); 6875 verifyFormat("#import \"a/b/string\""); 6876 verifyFormat("#import \"string.h\""); 6877 verifyFormat("#import \"string.h\""); 6878 verifyFormat("#if __has_include(<strstream>)\n" 6879 "#include <strstream>\n" 6880 "#endif"); 6881 6882 verifyFormat("#define MY_IMPORT <a/b>"); 6883 6884 verifyFormat("#if __has_include(<a/b>)"); 6885 verifyFormat("#if __has_include_next(<a/b>)"); 6886 verifyFormat("#define F __has_include(<a/b>)"); 6887 verifyFormat("#define F __has_include_next(<a/b>)"); 6888 6889 // Protocol buffer definition or missing "#". 6890 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6891 getLLVMStyleWithColumns(30)); 6892 6893 FormatStyle Style = getLLVMStyle(); 6894 Style.AlwaysBreakBeforeMultilineStrings = true; 6895 Style.ColumnLimit = 0; 6896 verifyFormat("#import \"abc.h\"", Style); 6897 6898 // But 'import' might also be a regular C++ namespace. 6899 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6900 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6901 } 6902 6903 //===----------------------------------------------------------------------===// 6904 // Error recovery tests. 6905 //===----------------------------------------------------------------------===// 6906 6907 TEST_F(FormatTest, IncompleteParameterLists) { 6908 FormatStyle NoBinPacking = getLLVMStyle(); 6909 NoBinPacking.BinPackParameters = false; 6910 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6911 " double *min_x,\n" 6912 " double *max_x,\n" 6913 " double *min_y,\n" 6914 " double *max_y,\n" 6915 " double *min_z,\n" 6916 " double *max_z, ) {}", 6917 NoBinPacking); 6918 } 6919 6920 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6921 verifyFormat("void f() { return; }\n42"); 6922 verifyFormat("void f() {\n" 6923 " if (0)\n" 6924 " return;\n" 6925 "}\n" 6926 "42"); 6927 verifyFormat("void f() { return }\n42"); 6928 verifyFormat("void f() {\n" 6929 " if (0)\n" 6930 " return\n" 6931 "}\n" 6932 "42"); 6933 } 6934 6935 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6936 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6937 EXPECT_EQ("void f() {\n" 6938 " if (a)\n" 6939 " return\n" 6940 "}", 6941 format("void f ( ) { if ( a ) return }")); 6942 EXPECT_EQ("namespace N {\n" 6943 "void f()\n" 6944 "}", 6945 format("namespace N { void f() }")); 6946 EXPECT_EQ("namespace N {\n" 6947 "void f() {}\n" 6948 "void g()\n" 6949 "} // namespace N", 6950 format("namespace N { void f( ) { } void g( ) }")); 6951 } 6952 6953 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6954 verifyFormat("int aaaaaaaa =\n" 6955 " // Overlylongcomment\n" 6956 " b;", 6957 getLLVMStyleWithColumns(20)); 6958 verifyFormat("function(\n" 6959 " ShortArgument,\n" 6960 " LoooooooooooongArgument);\n", 6961 getLLVMStyleWithColumns(20)); 6962 } 6963 6964 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6965 verifyFormat("public:"); 6966 verifyFormat("class A {\n" 6967 "public\n" 6968 " void f() {}\n" 6969 "};"); 6970 verifyFormat("public\n" 6971 "int qwerty;"); 6972 verifyFormat("public\n" 6973 "B {}"); 6974 verifyFormat("public\n" 6975 "{}"); 6976 verifyFormat("public\n" 6977 "B { int x; }"); 6978 } 6979 6980 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6981 verifyFormat("{"); 6982 verifyFormat("#})"); 6983 verifyNoCrash("(/**/[:!] ?[)."); 6984 } 6985 6986 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 6987 // Found by oss-fuzz: 6988 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 6989 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 6990 Style.ColumnLimit = 60; 6991 verifyNoCrash( 6992 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 6993 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 6994 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 6995 Style); 6996 } 6997 6998 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6999 verifyFormat("do {\n}"); 7000 verifyFormat("do {\n}\n" 7001 "f();"); 7002 verifyFormat("do {\n}\n" 7003 "wheeee(fun);"); 7004 verifyFormat("do {\n" 7005 " f();\n" 7006 "}"); 7007 } 7008 7009 TEST_F(FormatTest, IncorrectCodeMissingParens) { 7010 verifyFormat("if {\n foo;\n foo();\n}"); 7011 verifyFormat("switch {\n foo;\n foo();\n}"); 7012 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 7013 verifyFormat("while {\n foo;\n foo();\n}"); 7014 verifyFormat("do {\n foo;\n foo();\n} while;"); 7015 } 7016 7017 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 7018 verifyIncompleteFormat("namespace {\n" 7019 "class Foo { Foo (\n" 7020 "};\n" 7021 "} // namespace"); 7022 } 7023 7024 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 7025 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 7026 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 7027 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 7028 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 7029 7030 EXPECT_EQ("{\n" 7031 " {\n" 7032 " breakme(\n" 7033 " qwe);\n" 7034 " }\n", 7035 format("{\n" 7036 " {\n" 7037 " breakme(qwe);\n" 7038 "}\n", 7039 getLLVMStyleWithColumns(10))); 7040 } 7041 7042 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 7043 verifyFormat("int x = {\n" 7044 " avariable,\n" 7045 " b(alongervariable)};", 7046 getLLVMStyleWithColumns(25)); 7047 } 7048 7049 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 7050 verifyFormat("return (a)(b){1, 2, 3};"); 7051 } 7052 7053 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 7054 verifyFormat("vector<int> x{1, 2, 3, 4};"); 7055 verifyFormat("vector<int> x{\n" 7056 " 1,\n" 7057 " 2,\n" 7058 " 3,\n" 7059 " 4,\n" 7060 "};"); 7061 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 7062 verifyFormat("f({1, 2});"); 7063 verifyFormat("auto v = Foo{-1};"); 7064 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 7065 verifyFormat("Class::Class : member{1, 2, 3} {}"); 7066 verifyFormat("new vector<int>{1, 2, 3};"); 7067 verifyFormat("new int[3]{1, 2, 3};"); 7068 verifyFormat("new int{1};"); 7069 verifyFormat("return {arg1, arg2};"); 7070 verifyFormat("return {arg1, SomeType{parameter}};"); 7071 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 7072 verifyFormat("new T{arg1, arg2};"); 7073 verifyFormat("f(MyMap[{composite, key}]);"); 7074 verifyFormat("class Class {\n" 7075 " T member = {arg1, arg2};\n" 7076 "};"); 7077 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 7078 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 7079 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 7080 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 7081 verifyFormat("int a = std::is_integral<int>{} + 0;"); 7082 7083 verifyFormat("int foo(int i) { return fo1{}(i); }"); 7084 verifyFormat("int foo(int i) { return fo1{}(i); }"); 7085 verifyFormat("auto i = decltype(x){};"); 7086 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 7087 verifyFormat("Node n{1, Node{1000}, //\n" 7088 " 2};"); 7089 verifyFormat("Aaaa aaaaaaa{\n" 7090 " {\n" 7091 " aaaa,\n" 7092 " },\n" 7093 "};"); 7094 verifyFormat("class C : public D {\n" 7095 " SomeClass SC{2};\n" 7096 "};"); 7097 verifyFormat("class C : public A {\n" 7098 " class D : public B {\n" 7099 " void f() { int i{2}; }\n" 7100 " };\n" 7101 "};"); 7102 verifyFormat("#define A {a, a},"); 7103 7104 // Avoid breaking between equal sign and opening brace 7105 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 7106 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 7107 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 7108 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 7109 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 7110 " {\"ccccccccccccccccccccc\", 2}};", 7111 AvoidBreakingFirstArgument); 7112 7113 // Binpacking only if there is no trailing comma 7114 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 7115 " cccccccccc, dddddddddd};", 7116 getLLVMStyleWithColumns(50)); 7117 verifyFormat("const Aaaaaa aaaaa = {\n" 7118 " aaaaaaaaaaa,\n" 7119 " bbbbbbbbbbb,\n" 7120 " ccccccccccc,\n" 7121 " ddddddddddd,\n" 7122 "};", getLLVMStyleWithColumns(50)); 7123 7124 // Cases where distinguising braced lists and blocks is hard. 7125 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 7126 verifyFormat("void f() {\n" 7127 " return; // comment\n" 7128 "}\n" 7129 "SomeType t;"); 7130 verifyFormat("void f() {\n" 7131 " if (a) {\n" 7132 " f();\n" 7133 " }\n" 7134 "}\n" 7135 "SomeType t;"); 7136 7137 // In combination with BinPackArguments = false. 7138 FormatStyle NoBinPacking = getLLVMStyle(); 7139 NoBinPacking.BinPackArguments = false; 7140 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 7141 " bbbbb,\n" 7142 " ccccc,\n" 7143 " ddddd,\n" 7144 " eeeee,\n" 7145 " ffffff,\n" 7146 " ggggg,\n" 7147 " hhhhhh,\n" 7148 " iiiiii,\n" 7149 " jjjjjj,\n" 7150 " kkkkkk};", 7151 NoBinPacking); 7152 verifyFormat("const Aaaaaa aaaaa = {\n" 7153 " aaaaa,\n" 7154 " bbbbb,\n" 7155 " ccccc,\n" 7156 " ddddd,\n" 7157 " eeeee,\n" 7158 " ffffff,\n" 7159 " ggggg,\n" 7160 " hhhhhh,\n" 7161 " iiiiii,\n" 7162 " jjjjjj,\n" 7163 " kkkkkk,\n" 7164 "};", 7165 NoBinPacking); 7166 verifyFormat( 7167 "const Aaaaaa aaaaa = {\n" 7168 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 7169 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 7170 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 7171 "};", 7172 NoBinPacking); 7173 7174 // FIXME: The alignment of these trailing comments might be bad. Then again, 7175 // this might be utterly useless in real code. 7176 verifyFormat("Constructor::Constructor()\n" 7177 " : some_value{ //\n" 7178 " aaaaaaa, //\n" 7179 " bbbbbbb} {}"); 7180 7181 // In braced lists, the first comment is always assumed to belong to the 7182 // first element. Thus, it can be moved to the next or previous line as 7183 // appropriate. 7184 EXPECT_EQ("function({// First element:\n" 7185 " 1,\n" 7186 " // Second element:\n" 7187 " 2});", 7188 format("function({\n" 7189 " // First element:\n" 7190 " 1,\n" 7191 " // Second element:\n" 7192 " 2});")); 7193 EXPECT_EQ("std::vector<int> MyNumbers{\n" 7194 " // First element:\n" 7195 " 1,\n" 7196 " // Second element:\n" 7197 " 2};", 7198 format("std::vector<int> MyNumbers{// First element:\n" 7199 " 1,\n" 7200 " // Second element:\n" 7201 " 2};", 7202 getLLVMStyleWithColumns(30))); 7203 // A trailing comma should still lead to an enforced line break and no 7204 // binpacking. 7205 EXPECT_EQ("vector<int> SomeVector = {\n" 7206 " // aaa\n" 7207 " 1,\n" 7208 " 2,\n" 7209 "};", 7210 format("vector<int> SomeVector = { // aaa\n" 7211 " 1, 2, };")); 7212 7213 FormatStyle ExtraSpaces = getLLVMStyle(); 7214 ExtraSpaces.Cpp11BracedListStyle = false; 7215 ExtraSpaces.ColumnLimit = 75; 7216 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 7217 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 7218 verifyFormat("f({ 1, 2 });", ExtraSpaces); 7219 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 7220 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 7221 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 7222 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 7223 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 7224 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 7225 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 7226 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 7227 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 7228 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 7229 verifyFormat("class Class {\n" 7230 " T member = { arg1, arg2 };\n" 7231 "};", 7232 ExtraSpaces); 7233 verifyFormat( 7234 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7235 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 7236 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 7237 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 7238 ExtraSpaces); 7239 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 7240 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 7241 ExtraSpaces); 7242 verifyFormat( 7243 "someFunction(OtherParam,\n" 7244 " BracedList{ // comment 1 (Forcing interesting break)\n" 7245 " param1, param2,\n" 7246 " // comment 2\n" 7247 " param3, param4 });", 7248 ExtraSpaces); 7249 verifyFormat( 7250 "std::this_thread::sleep_for(\n" 7251 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 7252 ExtraSpaces); 7253 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 7254 " aaaaaaa,\n" 7255 " aaaaaaaaaa,\n" 7256 " aaaaa,\n" 7257 " aaaaaaaaaaaaaaa,\n" 7258 " aaa,\n" 7259 " aaaaaaaaaa,\n" 7260 " a,\n" 7261 " aaaaaaaaaaaaaaaaaaaaa,\n" 7262 " aaaaaaaaaaaa,\n" 7263 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 7264 " aaaaaaa,\n" 7265 " a};"); 7266 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 7267 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 7268 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 7269 7270 // Avoid breaking between initializer/equal sign and opening brace 7271 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 7272 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 7273 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 7274 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 7275 " { \"ccccccccccccccccccccc\", 2 }\n" 7276 "};", 7277 ExtraSpaces); 7278 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 7279 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 7280 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 7281 " { \"ccccccccccccccccccccc\", 2 }\n" 7282 "};", 7283 ExtraSpaces); 7284 7285 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 7286 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 7287 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 7288 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 7289 } 7290 7291 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 7292 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7293 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7294 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7295 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7296 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7297 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 7298 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 7299 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7300 " 1, 22, 333, 4444, 55555, //\n" 7301 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7302 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 7303 verifyFormat( 7304 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7305 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7306 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 7307 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7308 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7309 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7310 " 7777777};"); 7311 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7312 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7313 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7314 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7315 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7316 " // Separating comment.\n" 7317 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7318 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7319 " // Leading comment\n" 7320 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7321 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7322 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7323 " 1, 1, 1, 1};", 7324 getLLVMStyleWithColumns(39)); 7325 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7326 " 1, 1, 1, 1};", 7327 getLLVMStyleWithColumns(38)); 7328 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 7329 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 7330 getLLVMStyleWithColumns(43)); 7331 verifyFormat( 7332 "static unsigned SomeValues[10][3] = {\n" 7333 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 7334 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 7335 verifyFormat("static auto fields = new vector<string>{\n" 7336 " \"aaaaaaaaaaaaa\",\n" 7337 " \"aaaaaaaaaaaaa\",\n" 7338 " \"aaaaaaaaaaaa\",\n" 7339 " \"aaaaaaaaaaaaaa\",\n" 7340 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 7341 " \"aaaaaaaaaaaa\",\n" 7342 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 7343 "};"); 7344 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 7345 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 7346 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 7347 " 3, cccccccccccccccccccccc};", 7348 getLLVMStyleWithColumns(60)); 7349 7350 // Trailing commas. 7351 verifyFormat("vector<int> x = {\n" 7352 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 7353 "};", 7354 getLLVMStyleWithColumns(39)); 7355 verifyFormat("vector<int> x = {\n" 7356 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 7357 "};", 7358 getLLVMStyleWithColumns(39)); 7359 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7360 " 1, 1, 1, 1,\n" 7361 " /**/ /**/};", 7362 getLLVMStyleWithColumns(39)); 7363 7364 // Trailing comment in the first line. 7365 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 7366 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 7367 " 111111111, 222222222, 3333333333, 444444444, //\n" 7368 " 11111111, 22222222, 333333333, 44444444};"); 7369 // Trailing comment in the last line. 7370 verifyFormat("int aaaaa[] = {\n" 7371 " 1, 2, 3, // comment\n" 7372 " 4, 5, 6 // comment\n" 7373 "};"); 7374 7375 // With nested lists, we should either format one item per line or all nested 7376 // lists one on line. 7377 // FIXME: For some nested lists, we can do better. 7378 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 7379 " {aaaaaaaaaaaaaaaaaaa},\n" 7380 " {aaaaaaaaaaaaaaaaaaaaa},\n" 7381 " {aaaaaaaaaaaaaaaaa}};", 7382 getLLVMStyleWithColumns(60)); 7383 verifyFormat( 7384 "SomeStruct my_struct_array = {\n" 7385 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 7386 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 7387 " {aaa, aaa},\n" 7388 " {aaa, aaa},\n" 7389 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 7390 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 7391 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 7392 7393 // No column layout should be used here. 7394 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 7395 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 7396 7397 verifyNoCrash("a<,"); 7398 7399 // No braced initializer here. 7400 verifyFormat("void f() {\n" 7401 " struct Dummy {};\n" 7402 " f(v);\n" 7403 "}"); 7404 7405 // Long lists should be formatted in columns even if they are nested. 7406 verifyFormat( 7407 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7408 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7409 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7410 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7411 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7412 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 7413 7414 // Allow "single-column" layout even if that violates the column limit. There 7415 // isn't going to be a better way. 7416 verifyFormat("std::vector<int> a = {\n" 7417 " aaaaaaaa,\n" 7418 " aaaaaaaa,\n" 7419 " aaaaaaaa,\n" 7420 " aaaaaaaa,\n" 7421 " aaaaaaaaaa,\n" 7422 " aaaaaaaa,\n" 7423 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 7424 getLLVMStyleWithColumns(30)); 7425 verifyFormat("vector<int> aaaa = {\n" 7426 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7427 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7428 " aaaaaa.aaaaaaa,\n" 7429 " aaaaaa.aaaaaaa,\n" 7430 " aaaaaa.aaaaaaa,\n" 7431 " aaaaaa.aaaaaaa,\n" 7432 "};"); 7433 7434 // Don't create hanging lists. 7435 verifyFormat("someFunction(Param, {List1, List2,\n" 7436 " List3});", 7437 getLLVMStyleWithColumns(35)); 7438 verifyFormat("someFunction(Param, Param,\n" 7439 " {List1, List2,\n" 7440 " List3});", 7441 getLLVMStyleWithColumns(35)); 7442 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 7443 " aaaaaaaaaaaaaaaaaaaaaaa);"); 7444 } 7445 7446 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 7447 FormatStyle DoNotMerge = getLLVMStyle(); 7448 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7449 7450 verifyFormat("void f() { return 42; }"); 7451 verifyFormat("void f() {\n" 7452 " return 42;\n" 7453 "}", 7454 DoNotMerge); 7455 verifyFormat("void f() {\n" 7456 " // Comment\n" 7457 "}"); 7458 verifyFormat("{\n" 7459 "#error {\n" 7460 " int a;\n" 7461 "}"); 7462 verifyFormat("{\n" 7463 " int a;\n" 7464 "#error {\n" 7465 "}"); 7466 verifyFormat("void f() {} // comment"); 7467 verifyFormat("void f() { int a; } // comment"); 7468 verifyFormat("void f() {\n" 7469 "} // comment", 7470 DoNotMerge); 7471 verifyFormat("void f() {\n" 7472 " int a;\n" 7473 "} // comment", 7474 DoNotMerge); 7475 verifyFormat("void f() {\n" 7476 "} // comment", 7477 getLLVMStyleWithColumns(15)); 7478 7479 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 7480 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 7481 7482 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 7483 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 7484 verifyFormat("class C {\n" 7485 " C()\n" 7486 " : iiiiiiii(nullptr),\n" 7487 " kkkkkkk(nullptr),\n" 7488 " mmmmmmm(nullptr),\n" 7489 " nnnnnnn(nullptr) {}\n" 7490 "};", 7491 getGoogleStyle()); 7492 7493 FormatStyle NoColumnLimit = getLLVMStyle(); 7494 NoColumnLimit.ColumnLimit = 0; 7495 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 7496 EXPECT_EQ("class C {\n" 7497 " A() : b(0) {}\n" 7498 "};", 7499 format("class C{A():b(0){}};", NoColumnLimit)); 7500 EXPECT_EQ("A()\n" 7501 " : b(0) {\n" 7502 "}", 7503 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 7504 7505 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 7506 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 7507 FormatStyle::SFS_None; 7508 EXPECT_EQ("A()\n" 7509 " : b(0) {\n" 7510 "}", 7511 format("A():b(0){}", DoNotMergeNoColumnLimit)); 7512 EXPECT_EQ("A()\n" 7513 " : b(0) {\n" 7514 "}", 7515 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 7516 7517 verifyFormat("#define A \\\n" 7518 " void f() { \\\n" 7519 " int i; \\\n" 7520 " }", 7521 getLLVMStyleWithColumns(20)); 7522 verifyFormat("#define A \\\n" 7523 " void f() { int i; }", 7524 getLLVMStyleWithColumns(21)); 7525 verifyFormat("#define A \\\n" 7526 " void f() { \\\n" 7527 " int i; \\\n" 7528 " } \\\n" 7529 " int j;", 7530 getLLVMStyleWithColumns(22)); 7531 verifyFormat("#define A \\\n" 7532 " void f() { int i; } \\\n" 7533 " int j;", 7534 getLLVMStyleWithColumns(23)); 7535 } 7536 7537 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 7538 FormatStyle MergeEmptyOnly = getLLVMStyle(); 7539 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7540 verifyFormat("class C {\n" 7541 " int f() {}\n" 7542 "};", 7543 MergeEmptyOnly); 7544 verifyFormat("class C {\n" 7545 " int f() {\n" 7546 " return 42;\n" 7547 " }\n" 7548 "};", 7549 MergeEmptyOnly); 7550 verifyFormat("int f() {}", MergeEmptyOnly); 7551 verifyFormat("int f() {\n" 7552 " return 42;\n" 7553 "}", 7554 MergeEmptyOnly); 7555 7556 // Also verify behavior when BraceWrapping.AfterFunction = true 7557 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7558 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 7559 verifyFormat("int f() {}", MergeEmptyOnly); 7560 verifyFormat("class C {\n" 7561 " int f() {}\n" 7562 "};", 7563 MergeEmptyOnly); 7564 } 7565 7566 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 7567 FormatStyle MergeInlineOnly = getLLVMStyle(); 7568 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7569 verifyFormat("class C {\n" 7570 " int f() { return 42; }\n" 7571 "};", 7572 MergeInlineOnly); 7573 verifyFormat("int f() {\n" 7574 " return 42;\n" 7575 "}", 7576 MergeInlineOnly); 7577 7578 // SFS_Inline implies SFS_Empty 7579 verifyFormat("class C {\n" 7580 " int f() {}\n" 7581 "};", 7582 MergeInlineOnly); 7583 verifyFormat("int f() {}", MergeInlineOnly); 7584 7585 // Also verify behavior when BraceWrapping.AfterFunction = true 7586 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7587 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7588 verifyFormat("class C {\n" 7589 " int f() { return 42; }\n" 7590 "};", 7591 MergeInlineOnly); 7592 verifyFormat("int f()\n" 7593 "{\n" 7594 " return 42;\n" 7595 "}", 7596 MergeInlineOnly); 7597 7598 // SFS_Inline implies SFS_Empty 7599 verifyFormat("int f() {}", MergeInlineOnly); 7600 verifyFormat("class C {\n" 7601 " int f() {}\n" 7602 "};", 7603 MergeInlineOnly); 7604 } 7605 7606 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 7607 FormatStyle MergeInlineOnly = getLLVMStyle(); 7608 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 7609 FormatStyle::SFS_InlineOnly; 7610 verifyFormat("class C {\n" 7611 " int f() { return 42; }\n" 7612 "};", 7613 MergeInlineOnly); 7614 verifyFormat("int f() {\n" 7615 " return 42;\n" 7616 "}", 7617 MergeInlineOnly); 7618 7619 // SFS_InlineOnly does not imply SFS_Empty 7620 verifyFormat("class C {\n" 7621 " int f() {}\n" 7622 "};", 7623 MergeInlineOnly); 7624 verifyFormat("int f() {\n" 7625 "}", 7626 MergeInlineOnly); 7627 7628 // Also verify behavior when BraceWrapping.AfterFunction = true 7629 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7630 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7631 verifyFormat("class C {\n" 7632 " int f() { return 42; }\n" 7633 "};", 7634 MergeInlineOnly); 7635 verifyFormat("int f()\n" 7636 "{\n" 7637 " return 42;\n" 7638 "}", 7639 MergeInlineOnly); 7640 7641 // SFS_InlineOnly does not imply SFS_Empty 7642 verifyFormat("int f()\n" 7643 "{\n" 7644 "}", 7645 MergeInlineOnly); 7646 verifyFormat("class C {\n" 7647 " int f() {}\n" 7648 "};", 7649 MergeInlineOnly); 7650 } 7651 7652 TEST_F(FormatTest, SplitEmptyFunction) { 7653 FormatStyle Style = getLLVMStyle(); 7654 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7655 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7656 Style.BraceWrapping.AfterFunction = true; 7657 Style.BraceWrapping.SplitEmptyFunction = false; 7658 Style.ColumnLimit = 40; 7659 7660 verifyFormat("int f()\n" 7661 "{}", 7662 Style); 7663 verifyFormat("int f()\n" 7664 "{\n" 7665 " return 42;\n" 7666 "}", 7667 Style); 7668 verifyFormat("int f()\n" 7669 "{\n" 7670 " // some comment\n" 7671 "}", 7672 Style); 7673 7674 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7675 verifyFormat("int f() {}", Style); 7676 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7677 "{}", 7678 Style); 7679 verifyFormat("int f()\n" 7680 "{\n" 7681 " return 0;\n" 7682 "}", 7683 Style); 7684 7685 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7686 verifyFormat("class Foo {\n" 7687 " int f() {}\n" 7688 "};\n", 7689 Style); 7690 verifyFormat("class Foo {\n" 7691 " int f() { return 0; }\n" 7692 "};\n", 7693 Style); 7694 verifyFormat("class Foo {\n" 7695 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7696 " {}\n" 7697 "};\n", 7698 Style); 7699 verifyFormat("class Foo {\n" 7700 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7701 " {\n" 7702 " return 0;\n" 7703 " }\n" 7704 "};\n", 7705 Style); 7706 7707 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7708 verifyFormat("int f() {}", Style); 7709 verifyFormat("int f() { return 0; }", Style); 7710 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7711 "{}", 7712 Style); 7713 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7714 "{\n" 7715 " return 0;\n" 7716 "}", 7717 Style); 7718 } 7719 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 7720 FormatStyle Style = getLLVMStyle(); 7721 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7722 verifyFormat("#ifdef A\n" 7723 "int f() {}\n" 7724 "#else\n" 7725 "int g() {}\n" 7726 "#endif", 7727 Style); 7728 } 7729 7730 TEST_F(FormatTest, SplitEmptyClass) { 7731 FormatStyle Style = getLLVMStyle(); 7732 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7733 Style.BraceWrapping.AfterClass = true; 7734 Style.BraceWrapping.SplitEmptyRecord = false; 7735 7736 verifyFormat("class Foo\n" 7737 "{};", 7738 Style); 7739 verifyFormat("/* something */ class Foo\n" 7740 "{};", 7741 Style); 7742 verifyFormat("template <typename X> class Foo\n" 7743 "{};", 7744 Style); 7745 verifyFormat("class Foo\n" 7746 "{\n" 7747 " Foo();\n" 7748 "};", 7749 Style); 7750 verifyFormat("typedef class Foo\n" 7751 "{\n" 7752 "} Foo_t;", 7753 Style); 7754 } 7755 7756 TEST_F(FormatTest, SplitEmptyStruct) { 7757 FormatStyle Style = getLLVMStyle(); 7758 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7759 Style.BraceWrapping.AfterStruct = true; 7760 Style.BraceWrapping.SplitEmptyRecord = false; 7761 7762 verifyFormat("struct Foo\n" 7763 "{};", 7764 Style); 7765 verifyFormat("/* something */ struct Foo\n" 7766 "{};", 7767 Style); 7768 verifyFormat("template <typename X> struct Foo\n" 7769 "{};", 7770 Style); 7771 verifyFormat("struct Foo\n" 7772 "{\n" 7773 " Foo();\n" 7774 "};", 7775 Style); 7776 verifyFormat("typedef struct Foo\n" 7777 "{\n" 7778 "} Foo_t;", 7779 Style); 7780 //typedef struct Bar {} Bar_t; 7781 } 7782 7783 TEST_F(FormatTest, SplitEmptyUnion) { 7784 FormatStyle Style = getLLVMStyle(); 7785 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7786 Style.BraceWrapping.AfterUnion = true; 7787 Style.BraceWrapping.SplitEmptyRecord = false; 7788 7789 verifyFormat("union Foo\n" 7790 "{};", 7791 Style); 7792 verifyFormat("/* something */ union Foo\n" 7793 "{};", 7794 Style); 7795 verifyFormat("union Foo\n" 7796 "{\n" 7797 " A,\n" 7798 "};", 7799 Style); 7800 verifyFormat("typedef union Foo\n" 7801 "{\n" 7802 "} Foo_t;", 7803 Style); 7804 } 7805 7806 TEST_F(FormatTest, SplitEmptyNamespace) { 7807 FormatStyle Style = getLLVMStyle(); 7808 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7809 Style.BraceWrapping.AfterNamespace = true; 7810 Style.BraceWrapping.SplitEmptyNamespace = false; 7811 7812 verifyFormat("namespace Foo\n" 7813 "{};", 7814 Style); 7815 verifyFormat("/* something */ namespace Foo\n" 7816 "{};", 7817 Style); 7818 verifyFormat("inline namespace Foo\n" 7819 "{};", 7820 Style); 7821 verifyFormat("/* something */ inline namespace Foo\n" 7822 "{};", 7823 Style); 7824 verifyFormat("export namespace Foo\n" 7825 "{};", 7826 Style); 7827 verifyFormat("namespace Foo\n" 7828 "{\n" 7829 "void Bar();\n" 7830 "};", 7831 Style); 7832 } 7833 7834 TEST_F(FormatTest, NeverMergeShortRecords) { 7835 FormatStyle Style = getLLVMStyle(); 7836 7837 verifyFormat("class Foo {\n" 7838 " Foo();\n" 7839 "};", 7840 Style); 7841 verifyFormat("typedef class Foo {\n" 7842 " Foo();\n" 7843 "} Foo_t;", 7844 Style); 7845 verifyFormat("struct Foo {\n" 7846 " Foo();\n" 7847 "};", 7848 Style); 7849 verifyFormat("typedef struct Foo {\n" 7850 " Foo();\n" 7851 "} Foo_t;", 7852 Style); 7853 verifyFormat("union Foo {\n" 7854 " A,\n" 7855 "};", 7856 Style); 7857 verifyFormat("typedef union Foo {\n" 7858 " A,\n" 7859 "} Foo_t;", 7860 Style); 7861 verifyFormat("namespace Foo {\n" 7862 "void Bar();\n" 7863 "};", 7864 Style); 7865 7866 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7867 Style.BraceWrapping.AfterClass = true; 7868 Style.BraceWrapping.AfterStruct = true; 7869 Style.BraceWrapping.AfterUnion = true; 7870 Style.BraceWrapping.AfterNamespace = true; 7871 verifyFormat("class Foo\n" 7872 "{\n" 7873 " Foo();\n" 7874 "};", 7875 Style); 7876 verifyFormat("typedef class Foo\n" 7877 "{\n" 7878 " Foo();\n" 7879 "} Foo_t;", 7880 Style); 7881 verifyFormat("struct Foo\n" 7882 "{\n" 7883 " Foo();\n" 7884 "};", 7885 Style); 7886 verifyFormat("typedef struct Foo\n" 7887 "{\n" 7888 " Foo();\n" 7889 "} Foo_t;", 7890 Style); 7891 verifyFormat("union Foo\n" 7892 "{\n" 7893 " A,\n" 7894 "};", 7895 Style); 7896 verifyFormat("typedef union Foo\n" 7897 "{\n" 7898 " A,\n" 7899 "} Foo_t;", 7900 Style); 7901 verifyFormat("namespace Foo\n" 7902 "{\n" 7903 "void Bar();\n" 7904 "};", 7905 Style); 7906 } 7907 7908 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 7909 // Elaborate type variable declarations. 7910 verifyFormat("struct foo a = {bar};\nint n;"); 7911 verifyFormat("class foo a = {bar};\nint n;"); 7912 verifyFormat("union foo a = {bar};\nint n;"); 7913 7914 // Elaborate types inside function definitions. 7915 verifyFormat("struct foo f() {}\nint n;"); 7916 verifyFormat("class foo f() {}\nint n;"); 7917 verifyFormat("union foo f() {}\nint n;"); 7918 7919 // Templates. 7920 verifyFormat("template <class X> void f() {}\nint n;"); 7921 verifyFormat("template <struct X> void f() {}\nint n;"); 7922 verifyFormat("template <union X> void f() {}\nint n;"); 7923 7924 // Actual definitions... 7925 verifyFormat("struct {\n} n;"); 7926 verifyFormat( 7927 "template <template <class T, class Y>, class Z> class X {\n} n;"); 7928 verifyFormat("union Z {\n int n;\n} x;"); 7929 verifyFormat("class MACRO Z {\n} n;"); 7930 verifyFormat("class MACRO(X) Z {\n} n;"); 7931 verifyFormat("class __attribute__(X) Z {\n} n;"); 7932 verifyFormat("class __declspec(X) Z {\n} n;"); 7933 verifyFormat("class A##B##C {\n} n;"); 7934 verifyFormat("class alignas(16) Z {\n} n;"); 7935 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 7936 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 7937 7938 // Redefinition from nested context: 7939 verifyFormat("class A::B::C {\n} n;"); 7940 7941 // Template definitions. 7942 verifyFormat( 7943 "template <typename F>\n" 7944 "Matcher(const Matcher<F> &Other,\n" 7945 " typename enable_if_c<is_base_of<F, T>::value &&\n" 7946 " !is_same<F, T>::value>::type * = 0)\n" 7947 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 7948 7949 // FIXME: This is still incorrectly handled at the formatter side. 7950 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 7951 verifyFormat("int i = SomeFunction(a<b, a> b);"); 7952 7953 // FIXME: 7954 // This now gets parsed incorrectly as class definition. 7955 // verifyFormat("class A<int> f() {\n}\nint n;"); 7956 7957 // Elaborate types where incorrectly parsing the structural element would 7958 // break the indent. 7959 verifyFormat("if (true)\n" 7960 " class X x;\n" 7961 "else\n" 7962 " f();\n"); 7963 7964 // This is simply incomplete. Formatting is not important, but must not crash. 7965 verifyFormat("class A:"); 7966 } 7967 7968 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 7969 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 7970 format("#error Leave all white!!!!! space* alone!\n")); 7971 EXPECT_EQ( 7972 "#warning Leave all white!!!!! space* alone!\n", 7973 format("#warning Leave all white!!!!! space* alone!\n")); 7974 EXPECT_EQ("#error 1", format(" # error 1")); 7975 EXPECT_EQ("#warning 1", format(" # warning 1")); 7976 } 7977 7978 TEST_F(FormatTest, FormatHashIfExpressions) { 7979 verifyFormat("#if AAAA && BBBB"); 7980 verifyFormat("#if (AAAA && BBBB)"); 7981 verifyFormat("#elif (AAAA && BBBB)"); 7982 // FIXME: Come up with a better indentation for #elif. 7983 verifyFormat( 7984 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 7985 " defined(BBBBBBBB)\n" 7986 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 7987 " defined(BBBBBBBB)\n" 7988 "#endif", 7989 getLLVMStyleWithColumns(65)); 7990 } 7991 7992 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 7993 FormatStyle AllowsMergedIf = getGoogleStyle(); 7994 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 7995 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 7996 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 7997 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 7998 EXPECT_EQ("if (true) return 42;", 7999 format("if (true)\nreturn 42;", AllowsMergedIf)); 8000 FormatStyle ShortMergedIf = AllowsMergedIf; 8001 ShortMergedIf.ColumnLimit = 25; 8002 verifyFormat("#define A \\\n" 8003 " if (true) return 42;", 8004 ShortMergedIf); 8005 verifyFormat("#define A \\\n" 8006 " f(); \\\n" 8007 " if (true)\n" 8008 "#define B", 8009 ShortMergedIf); 8010 verifyFormat("#define A \\\n" 8011 " f(); \\\n" 8012 " if (true)\n" 8013 "g();", 8014 ShortMergedIf); 8015 verifyFormat("{\n" 8016 "#ifdef A\n" 8017 " // Comment\n" 8018 " if (true) continue;\n" 8019 "#endif\n" 8020 " // Comment\n" 8021 " if (true) continue;\n" 8022 "}", 8023 ShortMergedIf); 8024 ShortMergedIf.ColumnLimit = 33; 8025 verifyFormat("#define A \\\n" 8026 " if constexpr (true) return 42;", 8027 ShortMergedIf); 8028 ShortMergedIf.ColumnLimit = 29; 8029 verifyFormat("#define A \\\n" 8030 " if (aaaaaaaaaa) return 1; \\\n" 8031 " return 2;", 8032 ShortMergedIf); 8033 ShortMergedIf.ColumnLimit = 28; 8034 verifyFormat("#define A \\\n" 8035 " if (aaaaaaaaaa) \\\n" 8036 " return 1; \\\n" 8037 " return 2;", 8038 ShortMergedIf); 8039 verifyFormat("#define A \\\n" 8040 " if constexpr (aaaaaaa) \\\n" 8041 " return 1; \\\n" 8042 " return 2;", 8043 ShortMergedIf); 8044 } 8045 8046 TEST_F(FormatTest, FormatStarDependingOnContext) { 8047 verifyFormat("void f(int *a);"); 8048 verifyFormat("void f() { f(fint * b); }"); 8049 verifyFormat("class A {\n void f(int *a);\n};"); 8050 verifyFormat("class A {\n int *a;\n};"); 8051 verifyFormat("namespace a {\n" 8052 "namespace b {\n" 8053 "class A {\n" 8054 " void f() {}\n" 8055 " int *a;\n" 8056 "};\n" 8057 "} // namespace b\n" 8058 "} // namespace a"); 8059 } 8060 8061 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 8062 verifyFormat("while"); 8063 verifyFormat("operator"); 8064 } 8065 8066 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 8067 // This code would be painfully slow to format if we didn't skip it. 8068 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 8069 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8070 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8071 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8072 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8073 "A(1, 1)\n" 8074 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 8075 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8076 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8077 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8078 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8079 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8080 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8081 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8082 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8083 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 8084 // Deeply nested part is untouched, rest is formatted. 8085 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 8086 format(std::string("int i;\n") + Code + "int j;\n", 8087 getLLVMStyle(), SC_ExpectIncomplete)); 8088 } 8089 8090 //===----------------------------------------------------------------------===// 8091 // Objective-C tests. 8092 //===----------------------------------------------------------------------===// 8093 8094 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 8095 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 8096 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 8097 format("-(NSUInteger)indexOfObject:(id)anObject;")); 8098 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 8099 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 8100 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 8101 format("-(NSInteger)Method3:(id)anObject;")); 8102 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 8103 format("-(NSInteger)Method4:(id)anObject;")); 8104 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 8105 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 8106 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 8107 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 8108 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 8109 "forAllCells:(BOOL)flag;", 8110 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 8111 "forAllCells:(BOOL)flag;")); 8112 8113 // Very long objectiveC method declaration. 8114 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 8115 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 8116 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 8117 " inRange:(NSRange)range\n" 8118 " outRange:(NSRange)out_range\n" 8119 " outRange1:(NSRange)out_range1\n" 8120 " outRange2:(NSRange)out_range2\n" 8121 " outRange3:(NSRange)out_range3\n" 8122 " outRange4:(NSRange)out_range4\n" 8123 " outRange5:(NSRange)out_range5\n" 8124 " outRange6:(NSRange)out_range6\n" 8125 " outRange7:(NSRange)out_range7\n" 8126 " outRange8:(NSRange)out_range8\n" 8127 " outRange9:(NSRange)out_range9;"); 8128 8129 // When the function name has to be wrapped. 8130 FormatStyle Style = getLLVMStyle(); 8131 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 8132 // and always indents instead. 8133 Style.IndentWrappedFunctionNames = false; 8134 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 8135 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 8136 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 8137 "}", 8138 Style); 8139 Style.IndentWrappedFunctionNames = true; 8140 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 8141 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 8142 " anotherName:(NSString)dddddddddddddd {\n" 8143 "}", 8144 Style); 8145 8146 verifyFormat("- (int)sum:(vector<int>)numbers;"); 8147 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 8148 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 8149 // protocol lists (but not for template classes): 8150 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 8151 8152 verifyFormat("- (int (*)())foo:(int (*)())f;"); 8153 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 8154 8155 // If there's no return type (very rare in practice!), LLVM and Google style 8156 // agree. 8157 verifyFormat("- foo;"); 8158 verifyFormat("- foo:(int)f;"); 8159 verifyGoogleFormat("- foo:(int)foo;"); 8160 } 8161 8162 8163 TEST_F(FormatTest, BreaksStringLiterals) { 8164 EXPECT_EQ("\"some text \"\n" 8165 "\"other\";", 8166 format("\"some text other\";", getLLVMStyleWithColumns(12))); 8167 EXPECT_EQ("\"some text \"\n" 8168 "\"other\";", 8169 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 8170 EXPECT_EQ( 8171 "#define A \\\n" 8172 " \"some \" \\\n" 8173 " \"text \" \\\n" 8174 " \"other\";", 8175 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 8176 EXPECT_EQ( 8177 "#define A \\\n" 8178 " \"so \" \\\n" 8179 " \"text \" \\\n" 8180 " \"other\";", 8181 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 8182 8183 EXPECT_EQ("\"some text\"", 8184 format("\"some text\"", getLLVMStyleWithColumns(1))); 8185 EXPECT_EQ("\"some text\"", 8186 format("\"some text\"", getLLVMStyleWithColumns(11))); 8187 EXPECT_EQ("\"some \"\n" 8188 "\"text\"", 8189 format("\"some text\"", getLLVMStyleWithColumns(10))); 8190 EXPECT_EQ("\"some \"\n" 8191 "\"text\"", 8192 format("\"some text\"", getLLVMStyleWithColumns(7))); 8193 EXPECT_EQ("\"some\"\n" 8194 "\" tex\"\n" 8195 "\"t\"", 8196 format("\"some text\"", getLLVMStyleWithColumns(6))); 8197 EXPECT_EQ("\"some\"\n" 8198 "\" tex\"\n" 8199 "\" and\"", 8200 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 8201 EXPECT_EQ("\"some\"\n" 8202 "\"/tex\"\n" 8203 "\"/and\"", 8204 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 8205 8206 EXPECT_EQ("variable =\n" 8207 " \"long string \"\n" 8208 " \"literal\";", 8209 format("variable = \"long string literal\";", 8210 getLLVMStyleWithColumns(20))); 8211 8212 EXPECT_EQ("variable = f(\n" 8213 " \"long string \"\n" 8214 " \"literal\",\n" 8215 " short,\n" 8216 " loooooooooooooooooooong);", 8217 format("variable = f(\"long string literal\", short, " 8218 "loooooooooooooooooooong);", 8219 getLLVMStyleWithColumns(20))); 8220 8221 EXPECT_EQ( 8222 "f(g(\"long string \"\n" 8223 " \"literal\"),\n" 8224 " b);", 8225 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 8226 EXPECT_EQ("f(g(\"long string \"\n" 8227 " \"literal\",\n" 8228 " a),\n" 8229 " b);", 8230 format("f(g(\"long string literal\", a), b);", 8231 getLLVMStyleWithColumns(20))); 8232 EXPECT_EQ( 8233 "f(\"one two\".split(\n" 8234 " variable));", 8235 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 8236 EXPECT_EQ("f(\"one two three four five six \"\n" 8237 " \"seven\".split(\n" 8238 " really_looooong_variable));", 8239 format("f(\"one two three four five six seven\"." 8240 "split(really_looooong_variable));", 8241 getLLVMStyleWithColumns(33))); 8242 8243 EXPECT_EQ("f(\"some \"\n" 8244 " \"text\",\n" 8245 " other);", 8246 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 8247 8248 // Only break as a last resort. 8249 verifyFormat( 8250 "aaaaaaaaaaaaaaaaaaaa(\n" 8251 " aaaaaaaaaaaaaaaaaaaa,\n" 8252 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 8253 8254 EXPECT_EQ("\"splitmea\"\n" 8255 "\"trandomp\"\n" 8256 "\"oint\"", 8257 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 8258 8259 EXPECT_EQ("\"split/\"\n" 8260 "\"pathat/\"\n" 8261 "\"slashes\"", 8262 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8263 8264 EXPECT_EQ("\"split/\"\n" 8265 "\"pathat/\"\n" 8266 "\"slashes\"", 8267 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8268 EXPECT_EQ("\"split at \"\n" 8269 "\"spaces/at/\"\n" 8270 "\"slashes.at.any$\"\n" 8271 "\"non-alphanumeric%\"\n" 8272 "\"1111111111characte\"\n" 8273 "\"rs\"", 8274 format("\"split at " 8275 "spaces/at/" 8276 "slashes.at." 8277 "any$non-" 8278 "alphanumeric%" 8279 "1111111111characte" 8280 "rs\"", 8281 getLLVMStyleWithColumns(20))); 8282 8283 // Verify that splitting the strings understands 8284 // Style::AlwaysBreakBeforeMultilineStrings. 8285 EXPECT_EQ( 8286 "aaaaaaaaaaaa(\n" 8287 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 8288 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 8289 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 8290 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8291 "aaaaaaaaaaaaaaaaaaaaaa\");", 8292 getGoogleStyle())); 8293 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8294 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 8295 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 8296 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8297 "aaaaaaaaaaaaaaaaaaaaaa\";", 8298 getGoogleStyle())); 8299 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8300 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8301 format("llvm::outs() << " 8302 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 8303 "aaaaaaaaaaaaaaaaaaa\";")); 8304 EXPECT_EQ("ffff(\n" 8305 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8306 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8307 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8308 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8309 getGoogleStyle())); 8310 8311 FormatStyle Style = getLLVMStyleWithColumns(12); 8312 Style.BreakStringLiterals = false; 8313 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 8314 8315 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 8316 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8317 EXPECT_EQ("#define A \\\n" 8318 " \"some \" \\\n" 8319 " \"text \" \\\n" 8320 " \"other\";", 8321 format("#define A \"some text other\";", AlignLeft)); 8322 } 8323 8324 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 8325 EXPECT_EQ("C a = \"some more \"\n" 8326 " \"text\";", 8327 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 8328 } 8329 8330 TEST_F(FormatTest, FullyRemoveEmptyLines) { 8331 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 8332 NoEmptyLines.MaxEmptyLinesToKeep = 0; 8333 EXPECT_EQ("int i = a(b());", 8334 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 8335 } 8336 8337 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 8338 EXPECT_EQ( 8339 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8340 "(\n" 8341 " \"x\t\");", 8342 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8343 "aaaaaaa(" 8344 "\"x\t\");")); 8345 } 8346 8347 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 8348 EXPECT_EQ( 8349 "u8\"utf8 string \"\n" 8350 "u8\"literal\";", 8351 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 8352 EXPECT_EQ( 8353 "u\"utf16 string \"\n" 8354 "u\"literal\";", 8355 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 8356 EXPECT_EQ( 8357 "U\"utf32 string \"\n" 8358 "U\"literal\";", 8359 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 8360 EXPECT_EQ("L\"wide string \"\n" 8361 "L\"literal\";", 8362 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 8363 EXPECT_EQ("@\"NSString \"\n" 8364 "@\"literal\";", 8365 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 8366 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 8367 8368 // This input makes clang-format try to split the incomplete unicode escape 8369 // sequence, which used to lead to a crasher. 8370 verifyNoCrash( 8371 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 8372 getLLVMStyleWithColumns(60)); 8373 } 8374 8375 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 8376 FormatStyle Style = getGoogleStyleWithColumns(15); 8377 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 8378 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 8379 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 8380 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 8381 EXPECT_EQ("u8R\"x(raw literal)x\";", 8382 format("u8R\"x(raw literal)x\";", Style)); 8383 } 8384 8385 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 8386 FormatStyle Style = getLLVMStyleWithColumns(20); 8387 EXPECT_EQ( 8388 "_T(\"aaaaaaaaaaaaaa\")\n" 8389 "_T(\"aaaaaaaaaaaaaa\")\n" 8390 "_T(\"aaaaaaaaaaaa\")", 8391 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 8392 EXPECT_EQ("f(x,\n" 8393 " _T(\"aaaaaaaaaaaa\")\n" 8394 " _T(\"aaa\"),\n" 8395 " z);", 8396 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 8397 8398 // FIXME: Handle embedded spaces in one iteration. 8399 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 8400 // "_T(\"aaaaaaaaaaaaa\")\n" 8401 // "_T(\"aaaaaaaaaaaaa\")\n" 8402 // "_T(\"a\")", 8403 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8404 // getLLVMStyleWithColumns(20))); 8405 EXPECT_EQ( 8406 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 8407 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 8408 EXPECT_EQ("f(\n" 8409 "#if !TEST\n" 8410 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8411 "#endif\n" 8412 ");", 8413 format("f(\n" 8414 "#if !TEST\n" 8415 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 8416 "#endif\n" 8417 ");")); 8418 EXPECT_EQ("f(\n" 8419 "\n" 8420 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 8421 format("f(\n" 8422 "\n" 8423 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 8424 } 8425 8426 TEST_F(FormatTest, BreaksStringLiteralOperands) { 8427 // In a function call with two operands, the second can be broken with no line 8428 // break before it. 8429 EXPECT_EQ("func(a, \"long long \"\n" 8430 " \"long long\");", 8431 format("func(a, \"long long long long\");", 8432 getLLVMStyleWithColumns(24))); 8433 // In a function call with three operands, the second must be broken with a 8434 // line break before it. 8435 EXPECT_EQ("func(a,\n" 8436 " \"long long long \"\n" 8437 " \"long\",\n" 8438 " c);", 8439 format("func(a, \"long long long long\", c);", 8440 getLLVMStyleWithColumns(24))); 8441 // In a function call with three operands, the third must be broken with a 8442 // line break before it. 8443 EXPECT_EQ("func(a, b,\n" 8444 " \"long long long \"\n" 8445 " \"long\");", 8446 format("func(a, b, \"long long long long\");", 8447 getLLVMStyleWithColumns(24))); 8448 // In a function call with three operands, both the second and the third must 8449 // be broken with a line break before them. 8450 EXPECT_EQ("func(a,\n" 8451 " \"long long long \"\n" 8452 " \"long\",\n" 8453 " \"long long long \"\n" 8454 " \"long\");", 8455 format("func(a, \"long long long long\", \"long long long long\");", 8456 getLLVMStyleWithColumns(24))); 8457 // In a chain of << with two operands, the second can be broken with no line 8458 // break before it. 8459 EXPECT_EQ("a << \"line line \"\n" 8460 " \"line\";", 8461 format("a << \"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. 8465 EXPECT_EQ("abcde << \"line \"\n" 8466 " \"line line\"\n" 8467 " << c;", 8468 format("abcde << \"line line line\" << c;", 8469 getLLVMStyleWithColumns(20))); 8470 // In a chain of << with three operands, the third must be broken with a line 8471 // break before it. 8472 EXPECT_EQ("a << b\n" 8473 " << \"line line \"\n" 8474 " \"line\";", 8475 format("a << b << \"line line line\";", 8476 getLLVMStyleWithColumns(20))); 8477 // In a chain of << with three operands, the second can be broken with no line 8478 // break before it and the third must be broken with a line break before it. 8479 EXPECT_EQ("abcd << \"line line \"\n" 8480 " \"line\"\n" 8481 " << \"line line \"\n" 8482 " \"line\";", 8483 format("abcd << \"line line line\" << \"line line line\";", 8484 getLLVMStyleWithColumns(20))); 8485 // In a chain of binary operators with two operands, the second can be broken 8486 // with no line break before it. 8487 EXPECT_EQ("abcd + \"line line \"\n" 8488 " \"line line\";", 8489 format("abcd + \"line line line line\";", 8490 getLLVMStyleWithColumns(20))); 8491 // In a chain of binary operators with three operands, the second must be 8492 // broken with a line break before it. 8493 EXPECT_EQ("abcd +\n" 8494 " \"line line \"\n" 8495 " \"line line\" +\n" 8496 " e;", 8497 format("abcd + \"line line line line\" + e;", 8498 getLLVMStyleWithColumns(20))); 8499 // In a function call with two operands, with AlignAfterOpenBracket enabled, 8500 // the first must be broken with a line break before it. 8501 FormatStyle Style = getLLVMStyleWithColumns(25); 8502 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8503 EXPECT_EQ("someFunction(\n" 8504 " \"long long long \"\n" 8505 " \"long\",\n" 8506 " a);", 8507 format("someFunction(\"long long long long\", a);", Style)); 8508 } 8509 8510 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8511 EXPECT_EQ( 8512 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8514 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8515 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8516 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8517 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8518 } 8519 8520 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8521 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8522 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8523 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8524 "multiline raw string literal xxxxxxxxxxxxxx\n" 8525 ")x\",\n" 8526 " a),\n" 8527 " b);", 8528 format("fffffffffff(g(R\"x(\n" 8529 "multiline raw string literal xxxxxxxxxxxxxx\n" 8530 ")x\", a), b);", 8531 getGoogleStyleWithColumns(20))); 8532 EXPECT_EQ("fffffffffff(\n" 8533 " g(R\"x(qqq\n" 8534 "multiline raw string literal xxxxxxxxxxxxxx\n" 8535 ")x\",\n" 8536 " a),\n" 8537 " b);", 8538 format("fffffffffff(g(R\"x(qqq\n" 8539 "multiline raw string literal xxxxxxxxxxxxxx\n" 8540 ")x\", a), b);", 8541 getGoogleStyleWithColumns(20))); 8542 8543 EXPECT_EQ("fffffffffff(R\"x(\n" 8544 "multiline raw string literal xxxxxxxxxxxxxx\n" 8545 ")x\");", 8546 format("fffffffffff(R\"x(\n" 8547 "multiline raw string literal xxxxxxxxxxxxxx\n" 8548 ")x\");", 8549 getGoogleStyleWithColumns(20))); 8550 EXPECT_EQ("fffffffffff(R\"x(\n" 8551 "multiline raw string literal xxxxxxxxxxxxxx\n" 8552 ")x\" + bbbbbb);", 8553 format("fffffffffff(R\"x(\n" 8554 "multiline raw string literal xxxxxxxxxxxxxx\n" 8555 ")x\" + bbbbbb);", 8556 getGoogleStyleWithColumns(20))); 8557 EXPECT_EQ("fffffffffff(\n" 8558 " R\"x(\n" 8559 "multiline raw string literal xxxxxxxxxxxxxx\n" 8560 ")x\" +\n" 8561 " bbbbbb);", 8562 format("fffffffffff(\n" 8563 " R\"x(\n" 8564 "multiline raw string literal xxxxxxxxxxxxxx\n" 8565 ")x\" + bbbbbb);", 8566 getGoogleStyleWithColumns(20))); 8567 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 8568 format("fffffffffff(\n" 8569 " R\"(single line raw string)\" + bbbbbb);")); 8570 } 8571 8572 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8573 verifyFormat("string a = \"unterminated;"); 8574 EXPECT_EQ("function(\"unterminated,\n" 8575 " OtherParameter);", 8576 format("function( \"unterminated,\n" 8577 " OtherParameter);")); 8578 } 8579 8580 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8581 FormatStyle Style = getLLVMStyle(); 8582 Style.Standard = FormatStyle::LS_Cpp03; 8583 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8584 format("#define x(_a) printf(\"foo\"_a);", Style)); 8585 } 8586 8587 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8588 8589 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8590 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8591 " \"ddeeefff\");", 8592 format("someFunction(\"aaabbbcccdddeeefff\");", 8593 getLLVMStyleWithColumns(25))); 8594 EXPECT_EQ("someFunction1234567890(\n" 8595 " \"aaabbbcccdddeeefff\");", 8596 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8597 getLLVMStyleWithColumns(26))); 8598 EXPECT_EQ("someFunction1234567890(\n" 8599 " \"aaabbbcccdddeeeff\"\n" 8600 " \"f\");", 8601 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8602 getLLVMStyleWithColumns(25))); 8603 EXPECT_EQ("someFunction1234567890(\n" 8604 " \"aaabbbcccdddeeeff\"\n" 8605 " \"f\");", 8606 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8607 getLLVMStyleWithColumns(24))); 8608 EXPECT_EQ("someFunction(\n" 8609 " \"aaabbbcc ddde \"\n" 8610 " \"efff\");", 8611 format("someFunction(\"aaabbbcc ddde efff\");", 8612 getLLVMStyleWithColumns(25))); 8613 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8614 " \"ddeeefff\");", 8615 format("someFunction(\"aaabbbccc ddeeefff\");", 8616 getLLVMStyleWithColumns(25))); 8617 EXPECT_EQ("someFunction1234567890(\n" 8618 " \"aaabb \"\n" 8619 " \"cccdddeeefff\");", 8620 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8621 getLLVMStyleWithColumns(25))); 8622 EXPECT_EQ("#define A \\\n" 8623 " string s = \\\n" 8624 " \"123456789\" \\\n" 8625 " \"0\"; \\\n" 8626 " int i;", 8627 format("#define A string s = \"1234567890\"; int i;", 8628 getLLVMStyleWithColumns(20))); 8629 EXPECT_EQ("someFunction(\n" 8630 " \"aaabbbcc \"\n" 8631 " \"dddeeefff\");", 8632 format("someFunction(\"aaabbbcc dddeeefff\");", 8633 getLLVMStyleWithColumns(25))); 8634 } 8635 8636 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8637 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8638 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8639 EXPECT_EQ("\"test\"\n" 8640 "\"\\n\"", 8641 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8642 EXPECT_EQ("\"tes\\\\\"\n" 8643 "\"n\"", 8644 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8645 EXPECT_EQ("\"\\\\\\\\\"\n" 8646 "\"\\n\"", 8647 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8648 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8649 EXPECT_EQ("\"\\uff01\"\n" 8650 "\"test\"", 8651 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8652 EXPECT_EQ("\"\\Uff01ff02\"", 8653 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8654 EXPECT_EQ("\"\\x000000000001\"\n" 8655 "\"next\"", 8656 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8657 EXPECT_EQ("\"\\x000000000001next\"", 8658 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8659 EXPECT_EQ("\"\\x000000000001\"", 8660 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8661 EXPECT_EQ("\"test\"\n" 8662 "\"\\000000\"\n" 8663 "\"000001\"", 8664 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8665 EXPECT_EQ("\"test\\000\"\n" 8666 "\"00000000\"\n" 8667 "\"1\"", 8668 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8669 } 8670 8671 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8672 verifyFormat("void f() {\n" 8673 " return g() {}\n" 8674 " void h() {}"); 8675 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8676 "g();\n" 8677 "}"); 8678 } 8679 8680 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8681 verifyFormat( 8682 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8683 } 8684 8685 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8686 verifyFormat("class X {\n" 8687 " void f() {\n" 8688 " }\n" 8689 "};", 8690 getLLVMStyleWithColumns(12)); 8691 } 8692 8693 TEST_F(FormatTest, ConfigurableIndentWidth) { 8694 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8695 EightIndent.IndentWidth = 8; 8696 EightIndent.ContinuationIndentWidth = 8; 8697 verifyFormat("void f() {\n" 8698 " someFunction();\n" 8699 " if (true) {\n" 8700 " f();\n" 8701 " }\n" 8702 "}", 8703 EightIndent); 8704 verifyFormat("class X {\n" 8705 " void f() {\n" 8706 " }\n" 8707 "};", 8708 EightIndent); 8709 verifyFormat("int x[] = {\n" 8710 " call(),\n" 8711 " call()};", 8712 EightIndent); 8713 } 8714 8715 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8716 verifyFormat("double\n" 8717 "f();", 8718 getLLVMStyleWithColumns(8)); 8719 } 8720 8721 TEST_F(FormatTest, ConfigurableUseOfTab) { 8722 FormatStyle Tab = getLLVMStyleWithColumns(42); 8723 Tab.IndentWidth = 8; 8724 Tab.UseTab = FormatStyle::UT_Always; 8725 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8726 8727 EXPECT_EQ("if (aaaaaaaa && // q\n" 8728 " bb)\t\t// w\n" 8729 "\t;", 8730 format("if (aaaaaaaa &&// q\n" 8731 "bb)// w\n" 8732 ";", 8733 Tab)); 8734 EXPECT_EQ("if (aaa && bbb) // w\n" 8735 "\t;", 8736 format("if(aaa&&bbb)// w\n" 8737 ";", 8738 Tab)); 8739 8740 verifyFormat("class X {\n" 8741 "\tvoid f() {\n" 8742 "\t\tsomeFunction(parameter1,\n" 8743 "\t\t\t parameter2);\n" 8744 "\t}\n" 8745 "};", 8746 Tab); 8747 verifyFormat("#define A \\\n" 8748 "\tvoid f() { \\\n" 8749 "\t\tsomeFunction( \\\n" 8750 "\t\t parameter1, \\\n" 8751 "\t\t parameter2); \\\n" 8752 "\t}", 8753 Tab); 8754 verifyFormat("int a;\t // x\n" 8755 "int bbbbbbbb; // x\n", 8756 Tab); 8757 8758 Tab.TabWidth = 4; 8759 Tab.IndentWidth = 8; 8760 verifyFormat("class TabWidth4Indent8 {\n" 8761 "\t\tvoid f() {\n" 8762 "\t\t\t\tsomeFunction(parameter1,\n" 8763 "\t\t\t\t\t\t\t parameter2);\n" 8764 "\t\t}\n" 8765 "};", 8766 Tab); 8767 8768 Tab.TabWidth = 4; 8769 Tab.IndentWidth = 4; 8770 verifyFormat("class TabWidth4Indent4 {\n" 8771 "\tvoid f() {\n" 8772 "\t\tsomeFunction(parameter1,\n" 8773 "\t\t\t\t\t parameter2);\n" 8774 "\t}\n" 8775 "};", 8776 Tab); 8777 8778 Tab.TabWidth = 8; 8779 Tab.IndentWidth = 4; 8780 verifyFormat("class TabWidth8Indent4 {\n" 8781 " void f() {\n" 8782 "\tsomeFunction(parameter1,\n" 8783 "\t\t parameter2);\n" 8784 " }\n" 8785 "};", 8786 Tab); 8787 8788 Tab.TabWidth = 8; 8789 Tab.IndentWidth = 8; 8790 EXPECT_EQ("/*\n" 8791 "\t a\t\tcomment\n" 8792 "\t in multiple lines\n" 8793 " */", 8794 format(" /*\t \t \n" 8795 " \t \t a\t\tcomment\t \t\n" 8796 " \t \t in multiple lines\t\n" 8797 " \t */", 8798 Tab)); 8799 8800 Tab.UseTab = FormatStyle::UT_ForIndentation; 8801 verifyFormat("{\n" 8802 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8803 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8804 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8805 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8806 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8807 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8808 "};", 8809 Tab); 8810 verifyFormat("enum AA {\n" 8811 "\ta1, // Force multiple lines\n" 8812 "\ta2,\n" 8813 "\ta3\n" 8814 "};", 8815 Tab); 8816 EXPECT_EQ("if (aaaaaaaa && // q\n" 8817 " bb) // w\n" 8818 "\t;", 8819 format("if (aaaaaaaa &&// q\n" 8820 "bb)// w\n" 8821 ";", 8822 Tab)); 8823 verifyFormat("class X {\n" 8824 "\tvoid f() {\n" 8825 "\t\tsomeFunction(parameter1,\n" 8826 "\t\t parameter2);\n" 8827 "\t}\n" 8828 "};", 8829 Tab); 8830 verifyFormat("{\n" 8831 "\tQ(\n" 8832 "\t {\n" 8833 "\t\t int a;\n" 8834 "\t\t someFunction(aaaaaaaa,\n" 8835 "\t\t bbbbbbb);\n" 8836 "\t },\n" 8837 "\t p);\n" 8838 "}", 8839 Tab); 8840 EXPECT_EQ("{\n" 8841 "\t/* aaaa\n" 8842 "\t bbbb */\n" 8843 "}", 8844 format("{\n" 8845 "/* aaaa\n" 8846 " bbbb */\n" 8847 "}", 8848 Tab)); 8849 EXPECT_EQ("{\n" 8850 "\t/*\n" 8851 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8852 "\t bbbbbbbbbbbbb\n" 8853 "\t*/\n" 8854 "}", 8855 format("{\n" 8856 "/*\n" 8857 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8858 "*/\n" 8859 "}", 8860 Tab)); 8861 EXPECT_EQ("{\n" 8862 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8863 "\t// bbbbbbbbbbbbb\n" 8864 "}", 8865 format("{\n" 8866 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8867 "}", 8868 Tab)); 8869 EXPECT_EQ("{\n" 8870 "\t/*\n" 8871 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8872 "\t bbbbbbbbbbbbb\n" 8873 "\t*/\n" 8874 "}", 8875 format("{\n" 8876 "\t/*\n" 8877 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8878 "\t*/\n" 8879 "}", 8880 Tab)); 8881 EXPECT_EQ("{\n" 8882 "\t/*\n" 8883 "\n" 8884 "\t*/\n" 8885 "}", 8886 format("{\n" 8887 "\t/*\n" 8888 "\n" 8889 "\t*/\n" 8890 "}", 8891 Tab)); 8892 EXPECT_EQ("{\n" 8893 "\t/*\n" 8894 " asdf\n" 8895 "\t*/\n" 8896 "}", 8897 format("{\n" 8898 "\t/*\n" 8899 " asdf\n" 8900 "\t*/\n" 8901 "}", 8902 Tab)); 8903 8904 Tab.UseTab = FormatStyle::UT_Never; 8905 EXPECT_EQ("/*\n" 8906 " a\t\tcomment\n" 8907 " in multiple lines\n" 8908 " */", 8909 format(" /*\t \t \n" 8910 " \t \t a\t\tcomment\t \t\n" 8911 " \t \t in multiple lines\t\n" 8912 " \t */", 8913 Tab)); 8914 EXPECT_EQ("/* some\n" 8915 " comment */", 8916 format(" \t \t /* some\n" 8917 " \t \t comment */", 8918 Tab)); 8919 EXPECT_EQ("int a; /* some\n" 8920 " comment */", 8921 format(" \t \t int a; /* some\n" 8922 " \t \t comment */", 8923 Tab)); 8924 8925 EXPECT_EQ("int a; /* some\n" 8926 "comment */", 8927 format(" \t \t int\ta; /* some\n" 8928 " \t \t comment */", 8929 Tab)); 8930 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8931 " comment */", 8932 format(" \t \t f(\"\t\t\"); /* some\n" 8933 " \t \t comment */", 8934 Tab)); 8935 EXPECT_EQ("{\n" 8936 " /*\n" 8937 " * Comment\n" 8938 " */\n" 8939 " int i;\n" 8940 "}", 8941 format("{\n" 8942 "\t/*\n" 8943 "\t * Comment\n" 8944 "\t */\n" 8945 "\t int i;\n" 8946 "}")); 8947 8948 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8949 Tab.TabWidth = 8; 8950 Tab.IndentWidth = 8; 8951 EXPECT_EQ("if (aaaaaaaa && // q\n" 8952 " bb) // w\n" 8953 "\t;", 8954 format("if (aaaaaaaa &&// q\n" 8955 "bb)// w\n" 8956 ";", 8957 Tab)); 8958 EXPECT_EQ("if (aaa && bbb) // w\n" 8959 "\t;", 8960 format("if(aaa&&bbb)// w\n" 8961 ";", 8962 Tab)); 8963 verifyFormat("class X {\n" 8964 "\tvoid f() {\n" 8965 "\t\tsomeFunction(parameter1,\n" 8966 "\t\t\t parameter2);\n" 8967 "\t}\n" 8968 "};", 8969 Tab); 8970 verifyFormat("#define A \\\n" 8971 "\tvoid f() { \\\n" 8972 "\t\tsomeFunction( \\\n" 8973 "\t\t parameter1, \\\n" 8974 "\t\t parameter2); \\\n" 8975 "\t}", 8976 Tab); 8977 Tab.TabWidth = 4; 8978 Tab.IndentWidth = 8; 8979 verifyFormat("class TabWidth4Indent8 {\n" 8980 "\t\tvoid f() {\n" 8981 "\t\t\t\tsomeFunction(parameter1,\n" 8982 "\t\t\t\t\t\t\t parameter2);\n" 8983 "\t\t}\n" 8984 "};", 8985 Tab); 8986 Tab.TabWidth = 4; 8987 Tab.IndentWidth = 4; 8988 verifyFormat("class TabWidth4Indent4 {\n" 8989 "\tvoid f() {\n" 8990 "\t\tsomeFunction(parameter1,\n" 8991 "\t\t\t\t\t parameter2);\n" 8992 "\t}\n" 8993 "};", 8994 Tab); 8995 Tab.TabWidth = 8; 8996 Tab.IndentWidth = 4; 8997 verifyFormat("class TabWidth8Indent4 {\n" 8998 " void f() {\n" 8999 "\tsomeFunction(parameter1,\n" 9000 "\t\t parameter2);\n" 9001 " }\n" 9002 "};", 9003 Tab); 9004 Tab.TabWidth = 8; 9005 Tab.IndentWidth = 8; 9006 EXPECT_EQ("/*\n" 9007 "\t a\t\tcomment\n" 9008 "\t in multiple lines\n" 9009 " */", 9010 format(" /*\t \t \n" 9011 " \t \t a\t\tcomment\t \t\n" 9012 " \t \t in multiple lines\t\n" 9013 " \t */", 9014 Tab)); 9015 verifyFormat("{\n" 9016 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9017 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9018 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9019 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9020 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9021 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9022 "};", 9023 Tab); 9024 verifyFormat("enum AA {\n" 9025 "\ta1, // Force multiple lines\n" 9026 "\ta2,\n" 9027 "\ta3\n" 9028 "};", 9029 Tab); 9030 EXPECT_EQ("if (aaaaaaaa && // q\n" 9031 " bb) // w\n" 9032 "\t;", 9033 format("if (aaaaaaaa &&// q\n" 9034 "bb)// w\n" 9035 ";", 9036 Tab)); 9037 verifyFormat("class X {\n" 9038 "\tvoid f() {\n" 9039 "\t\tsomeFunction(parameter1,\n" 9040 "\t\t\t parameter2);\n" 9041 "\t}\n" 9042 "};", 9043 Tab); 9044 verifyFormat("{\n" 9045 "\tQ(\n" 9046 "\t {\n" 9047 "\t\t int a;\n" 9048 "\t\t someFunction(aaaaaaaa,\n" 9049 "\t\t\t\t bbbbbbb);\n" 9050 "\t },\n" 9051 "\t p);\n" 9052 "}", 9053 Tab); 9054 EXPECT_EQ("{\n" 9055 "\t/* aaaa\n" 9056 "\t bbbb */\n" 9057 "}", 9058 format("{\n" 9059 "/* aaaa\n" 9060 " bbbb */\n" 9061 "}", 9062 Tab)); 9063 EXPECT_EQ("{\n" 9064 "\t/*\n" 9065 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9066 "\t bbbbbbbbbbbbb\n" 9067 "\t*/\n" 9068 "}", 9069 format("{\n" 9070 "/*\n" 9071 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9072 "*/\n" 9073 "}", 9074 Tab)); 9075 EXPECT_EQ("{\n" 9076 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9077 "\t// bbbbbbbbbbbbb\n" 9078 "}", 9079 format("{\n" 9080 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9081 "}", 9082 Tab)); 9083 EXPECT_EQ("{\n" 9084 "\t/*\n" 9085 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9086 "\t bbbbbbbbbbbbb\n" 9087 "\t*/\n" 9088 "}", 9089 format("{\n" 9090 "\t/*\n" 9091 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9092 "\t*/\n" 9093 "}", 9094 Tab)); 9095 EXPECT_EQ("{\n" 9096 "\t/*\n" 9097 "\n" 9098 "\t*/\n" 9099 "}", 9100 format("{\n" 9101 "\t/*\n" 9102 "\n" 9103 "\t*/\n" 9104 "}", 9105 Tab)); 9106 EXPECT_EQ("{\n" 9107 "\t/*\n" 9108 " asdf\n" 9109 "\t*/\n" 9110 "}", 9111 format("{\n" 9112 "\t/*\n" 9113 " asdf\n" 9114 "\t*/\n" 9115 "}", 9116 Tab)); 9117 EXPECT_EQ("/*\n" 9118 "\t a\t\tcomment\n" 9119 "\t in multiple lines\n" 9120 " */", 9121 format(" /*\t \t \n" 9122 " \t \t a\t\tcomment\t \t\n" 9123 " \t \t in multiple lines\t\n" 9124 " \t */", 9125 Tab)); 9126 EXPECT_EQ("/* some\n" 9127 " comment */", 9128 format(" \t \t /* some\n" 9129 " \t \t comment */", 9130 Tab)); 9131 EXPECT_EQ("int a; /* some\n" 9132 " comment */", 9133 format(" \t \t int a; /* some\n" 9134 " \t \t comment */", 9135 Tab)); 9136 EXPECT_EQ("int a; /* some\n" 9137 "comment */", 9138 format(" \t \t int\ta; /* some\n" 9139 " \t \t comment */", 9140 Tab)); 9141 EXPECT_EQ("f(\"\t\t\"); /* some\n" 9142 " comment */", 9143 format(" \t \t f(\"\t\t\"); /* some\n" 9144 " \t \t comment */", 9145 Tab)); 9146 EXPECT_EQ("{\n" 9147 " /*\n" 9148 " * Comment\n" 9149 " */\n" 9150 " int i;\n" 9151 "}", 9152 format("{\n" 9153 "\t/*\n" 9154 "\t * Comment\n" 9155 "\t */\n" 9156 "\t int i;\n" 9157 "}")); 9158 Tab.AlignConsecutiveAssignments = true; 9159 Tab.AlignConsecutiveDeclarations = true; 9160 Tab.TabWidth = 4; 9161 Tab.IndentWidth = 4; 9162 verifyFormat("class Assign {\n" 9163 "\tvoid f() {\n" 9164 "\t\tint x = 123;\n" 9165 "\t\tint random = 4;\n" 9166 "\t\tstd::string alphabet =\n" 9167 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 9168 "\t}\n" 9169 "};", 9170 Tab); 9171 } 9172 9173 TEST_F(FormatTest, CalculatesOriginalColumn) { 9174 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9175 "q\"; /* some\n" 9176 " comment */", 9177 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9178 "q\"; /* some\n" 9179 " comment */", 9180 getLLVMStyle())); 9181 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 9182 "/* some\n" 9183 " comment */", 9184 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 9185 " /* some\n" 9186 " comment */", 9187 getLLVMStyle())); 9188 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9189 "qqq\n" 9190 "/* some\n" 9191 " comment */", 9192 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9193 "qqq\n" 9194 " /* some\n" 9195 " comment */", 9196 getLLVMStyle())); 9197 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9198 "wwww; /* some\n" 9199 " comment */", 9200 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9201 "wwww; /* some\n" 9202 " comment */", 9203 getLLVMStyle())); 9204 } 9205 9206 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 9207 FormatStyle NoSpace = getLLVMStyle(); 9208 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 9209 9210 verifyFormat("while(true)\n" 9211 " continue;", 9212 NoSpace); 9213 verifyFormat("for(;;)\n" 9214 " continue;", 9215 NoSpace); 9216 verifyFormat("if(true)\n" 9217 " f();\n" 9218 "else if(true)\n" 9219 " f();", 9220 NoSpace); 9221 verifyFormat("do {\n" 9222 " do_something();\n" 9223 "} while(something());", 9224 NoSpace); 9225 verifyFormat("switch(x) {\n" 9226 "default:\n" 9227 " break;\n" 9228 "}", 9229 NoSpace); 9230 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 9231 verifyFormat("size_t x = sizeof(x);", NoSpace); 9232 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 9233 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 9234 verifyFormat("alignas(128) char a[128];", NoSpace); 9235 verifyFormat("size_t x = alignof(MyType);", NoSpace); 9236 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 9237 verifyFormat("int f() throw(Deprecated);", NoSpace); 9238 verifyFormat("typedef void (*cb)(int);", NoSpace); 9239 verifyFormat("T A::operator()();", NoSpace); 9240 verifyFormat("X A::operator++(T);", NoSpace); 9241 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 9242 9243 FormatStyle Space = getLLVMStyle(); 9244 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 9245 9246 verifyFormat("int f ();", Space); 9247 verifyFormat("void f (int a, T b) {\n" 9248 " while (true)\n" 9249 " continue;\n" 9250 "}", 9251 Space); 9252 verifyFormat("if (true)\n" 9253 " f ();\n" 9254 "else if (true)\n" 9255 " f ();", 9256 Space); 9257 verifyFormat("do {\n" 9258 " do_something ();\n" 9259 "} while (something ());", 9260 Space); 9261 verifyFormat("switch (x) {\n" 9262 "default:\n" 9263 " break;\n" 9264 "}", 9265 Space); 9266 verifyFormat("A::A () : a (1) {}", Space); 9267 verifyFormat("void f () __attribute__ ((asdf));", Space); 9268 verifyFormat("*(&a + 1);\n" 9269 "&((&a)[1]);\n" 9270 "a[(b + c) * d];\n" 9271 "(((a + 1) * 2) + 3) * 4;", 9272 Space); 9273 verifyFormat("#define A(x) x", Space); 9274 verifyFormat("#define A (x) x", Space); 9275 verifyFormat("#if defined(x)\n" 9276 "#endif", 9277 Space); 9278 verifyFormat("auto i = std::make_unique<int> (5);", Space); 9279 verifyFormat("size_t x = sizeof (x);", Space); 9280 verifyFormat("auto f (int x) -> decltype (x);", Space); 9281 verifyFormat("int f (T x) noexcept (x.create ());", Space); 9282 verifyFormat("alignas (128) char a[128];", Space); 9283 verifyFormat("size_t x = alignof (MyType);", Space); 9284 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 9285 verifyFormat("int f () throw (Deprecated);", Space); 9286 verifyFormat("typedef void (*cb) (int);", Space); 9287 verifyFormat("T A::operator() ();", Space); 9288 verifyFormat("X A::operator++ (T);", Space); 9289 verifyFormat("auto lambda = [] () { return 0; };", Space); 9290 } 9291 9292 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 9293 FormatStyle Spaces = getLLVMStyle(); 9294 9295 Spaces.SpacesInParentheses = true; 9296 verifyFormat("do_something( ::globalVar );", Spaces); 9297 verifyFormat("call( x, y, z );", Spaces); 9298 verifyFormat("call();", Spaces); 9299 verifyFormat("std::function<void( int, int )> callback;", Spaces); 9300 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 9301 Spaces); 9302 verifyFormat("while ( (bool)1 )\n" 9303 " continue;", 9304 Spaces); 9305 verifyFormat("for ( ;; )\n" 9306 " continue;", 9307 Spaces); 9308 verifyFormat("if ( true )\n" 9309 " f();\n" 9310 "else if ( true )\n" 9311 " f();", 9312 Spaces); 9313 verifyFormat("do {\n" 9314 " do_something( (int)i );\n" 9315 "} while ( something() );", 9316 Spaces); 9317 verifyFormat("switch ( x ) {\n" 9318 "default:\n" 9319 " break;\n" 9320 "}", 9321 Spaces); 9322 9323 Spaces.SpacesInParentheses = false; 9324 Spaces.SpacesInCStyleCastParentheses = true; 9325 verifyFormat("Type *A = ( Type * )P;", Spaces); 9326 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 9327 verifyFormat("x = ( int32 )y;", Spaces); 9328 verifyFormat("int a = ( int )(2.0f);", Spaces); 9329 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 9330 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 9331 verifyFormat("#define x (( int )-1)", Spaces); 9332 9333 // Run the first set of tests again with: 9334 Spaces.SpacesInParentheses = false; 9335 Spaces.SpaceInEmptyParentheses = true; 9336 Spaces.SpacesInCStyleCastParentheses = true; 9337 verifyFormat("call(x, y, z);", Spaces); 9338 verifyFormat("call( );", Spaces); 9339 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9340 verifyFormat("while (( bool )1)\n" 9341 " continue;", 9342 Spaces); 9343 verifyFormat("for (;;)\n" 9344 " continue;", 9345 Spaces); 9346 verifyFormat("if (true)\n" 9347 " f( );\n" 9348 "else if (true)\n" 9349 " f( );", 9350 Spaces); 9351 verifyFormat("do {\n" 9352 " do_something(( int )i);\n" 9353 "} while (something( ));", 9354 Spaces); 9355 verifyFormat("switch (x) {\n" 9356 "default:\n" 9357 " break;\n" 9358 "}", 9359 Spaces); 9360 9361 // Run the first set of tests again with: 9362 Spaces.SpaceAfterCStyleCast = true; 9363 verifyFormat("call(x, y, z);", Spaces); 9364 verifyFormat("call( );", Spaces); 9365 verifyFormat("std::function<void(int, int)> callback;", Spaces); 9366 verifyFormat("while (( bool ) 1)\n" 9367 " continue;", 9368 Spaces); 9369 verifyFormat("for (;;)\n" 9370 " continue;", 9371 Spaces); 9372 verifyFormat("if (true)\n" 9373 " f( );\n" 9374 "else if (true)\n" 9375 " f( );", 9376 Spaces); 9377 verifyFormat("do {\n" 9378 " do_something(( int ) i);\n" 9379 "} while (something( ));", 9380 Spaces); 9381 verifyFormat("switch (x) {\n" 9382 "default:\n" 9383 " break;\n" 9384 "}", 9385 Spaces); 9386 9387 // Run subset of tests again with: 9388 Spaces.SpacesInCStyleCastParentheses = false; 9389 Spaces.SpaceAfterCStyleCast = true; 9390 verifyFormat("while ((bool) 1)\n" 9391 " continue;", 9392 Spaces); 9393 verifyFormat("do {\n" 9394 " do_something((int) i);\n" 9395 "} while (something( ));", 9396 Spaces); 9397 } 9398 9399 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 9400 verifyFormat("int a[5];"); 9401 verifyFormat("a[3] += 42;"); 9402 9403 FormatStyle Spaces = getLLVMStyle(); 9404 Spaces.SpacesInSquareBrackets = true; 9405 // Lambdas unchanged. 9406 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 9407 verifyFormat("return [i, args...] {};", Spaces); 9408 9409 // Not lambdas. 9410 verifyFormat("int a[ 5 ];", Spaces); 9411 verifyFormat("a[ 3 ] += 42;", Spaces); 9412 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 9413 verifyFormat("double &operator[](int i) { return 0; }\n" 9414 "int i;", 9415 Spaces); 9416 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 9417 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 9418 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 9419 } 9420 9421 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 9422 verifyFormat("int a = 5;"); 9423 verifyFormat("a += 42;"); 9424 verifyFormat("a or_eq 8;"); 9425 9426 FormatStyle Spaces = getLLVMStyle(); 9427 Spaces.SpaceBeforeAssignmentOperators = false; 9428 verifyFormat("int a= 5;", Spaces); 9429 verifyFormat("a+= 42;", Spaces); 9430 verifyFormat("a or_eq 8;", Spaces); 9431 } 9432 9433 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 9434 verifyFormat("class Foo : public Bar {};"); 9435 verifyFormat("Foo::Foo() : foo(1) {}"); 9436 verifyFormat("for (auto a : b) {\n}"); 9437 verifyFormat("int x = a ? b : c;"); 9438 verifyFormat("{\n" 9439 "label0:\n" 9440 " int x = 0;\n" 9441 "}"); 9442 verifyFormat("switch (x) {\n" 9443 "case 1:\n" 9444 "default:\n" 9445 "}"); 9446 9447 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 9448 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 9449 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 9450 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 9451 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 9452 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 9453 verifyFormat("{\n" 9454 "label1:\n" 9455 " int x = 0;\n" 9456 "}", 9457 CtorInitializerStyle); 9458 verifyFormat("switch (x) {\n" 9459 "case 1:\n" 9460 "default:\n" 9461 "}", 9462 CtorInitializerStyle); 9463 CtorInitializerStyle.BreakConstructorInitializers = 9464 FormatStyle::BCIS_AfterColon; 9465 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 9466 " aaaaaaaaaaaaaaaa(1),\n" 9467 " bbbbbbbbbbbbbbbb(2) {}", 9468 CtorInitializerStyle); 9469 CtorInitializerStyle.BreakConstructorInitializers = 9470 FormatStyle::BCIS_BeforeComma; 9471 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9472 " : aaaaaaaaaaaaaaaa(1)\n" 9473 " , bbbbbbbbbbbbbbbb(2) {}", 9474 CtorInitializerStyle); 9475 CtorInitializerStyle.BreakConstructorInitializers = 9476 FormatStyle::BCIS_BeforeColon; 9477 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9478 " : aaaaaaaaaaaaaaaa(1),\n" 9479 " bbbbbbbbbbbbbbbb(2) {}", 9480 CtorInitializerStyle); 9481 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 9482 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 9483 ": aaaaaaaaaaaaaaaa(1),\n" 9484 " bbbbbbbbbbbbbbbb(2) {}", 9485 CtorInitializerStyle); 9486 9487 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 9488 InheritanceStyle.SpaceBeforeInheritanceColon = false; 9489 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 9490 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 9491 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 9492 verifyFormat("int x = a ? b : c;", InheritanceStyle); 9493 verifyFormat("{\n" 9494 "label2:\n" 9495 " int x = 0;\n" 9496 "}", 9497 InheritanceStyle); 9498 verifyFormat("switch (x) {\n" 9499 "case 1:\n" 9500 "default:\n" 9501 "}", 9502 InheritanceStyle); 9503 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 9504 verifyFormat("class Foooooooooooooooooooooo:\n" 9505 " public aaaaaaaaaaaaaaaaaa,\n" 9506 " public bbbbbbbbbbbbbbbbbb {\n" 9507 "}", 9508 InheritanceStyle); 9509 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 9510 verifyFormat("class Foooooooooooooooooooooo\n" 9511 " : public aaaaaaaaaaaaaaaaaa\n" 9512 " , public bbbbbbbbbbbbbbbbbb {\n" 9513 "}", 9514 InheritanceStyle); 9515 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 9516 verifyFormat("class Foooooooooooooooooooooo\n" 9517 " : public aaaaaaaaaaaaaaaaaa,\n" 9518 " public bbbbbbbbbbbbbbbbbb {\n" 9519 "}", 9520 InheritanceStyle); 9521 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 9522 verifyFormat("class Foooooooooooooooooooooo\n" 9523 ": public aaaaaaaaaaaaaaaaaa,\n" 9524 " public bbbbbbbbbbbbbbbbbb {}", 9525 InheritanceStyle); 9526 9527 FormatStyle ForLoopStyle = getLLVMStyle(); 9528 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 9529 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 9530 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 9531 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 9532 verifyFormat("int x = a ? b : c;", ForLoopStyle); 9533 verifyFormat("{\n" 9534 "label2:\n" 9535 " int x = 0;\n" 9536 "}", 9537 ForLoopStyle); 9538 verifyFormat("switch (x) {\n" 9539 "case 1:\n" 9540 "default:\n" 9541 "}", 9542 ForLoopStyle); 9543 9544 FormatStyle NoSpaceStyle = getLLVMStyle(); 9545 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 9546 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 9547 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 9548 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 9549 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 9550 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 9551 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 9552 verifyFormat("{\n" 9553 "label3:\n" 9554 " int x = 0;\n" 9555 "}", 9556 NoSpaceStyle); 9557 verifyFormat("switch (x) {\n" 9558 "case 1:\n" 9559 "default:\n" 9560 "}", 9561 NoSpaceStyle); 9562 } 9563 9564 TEST_F(FormatTest, AlignConsecutiveAssignments) { 9565 FormatStyle Alignment = getLLVMStyle(); 9566 Alignment.AlignConsecutiveAssignments = false; 9567 verifyFormat("int a = 5;\n" 9568 "int oneTwoThree = 123;", 9569 Alignment); 9570 verifyFormat("int a = 5;\n" 9571 "int oneTwoThree = 123;", 9572 Alignment); 9573 9574 Alignment.AlignConsecutiveAssignments = true; 9575 verifyFormat("int a = 5;\n" 9576 "int oneTwoThree = 123;", 9577 Alignment); 9578 verifyFormat("int a = method();\n" 9579 "int oneTwoThree = 133;", 9580 Alignment); 9581 verifyFormat("a &= 5;\n" 9582 "bcd *= 5;\n" 9583 "ghtyf += 5;\n" 9584 "dvfvdb -= 5;\n" 9585 "a /= 5;\n" 9586 "vdsvsv %= 5;\n" 9587 "sfdbddfbdfbb ^= 5;\n" 9588 "dvsdsv |= 5;\n" 9589 "int dsvvdvsdvvv = 123;", 9590 Alignment); 9591 verifyFormat("int i = 1, j = 10;\n" 9592 "something = 2000;", 9593 Alignment); 9594 verifyFormat("something = 2000;\n" 9595 "int i = 1, j = 10;\n", 9596 Alignment); 9597 verifyFormat("something = 2000;\n" 9598 "another = 911;\n" 9599 "int i = 1, j = 10;\n" 9600 "oneMore = 1;\n" 9601 "i = 2;", 9602 Alignment); 9603 verifyFormat("int a = 5;\n" 9604 "int one = 1;\n" 9605 "method();\n" 9606 "int oneTwoThree = 123;\n" 9607 "int oneTwo = 12;", 9608 Alignment); 9609 verifyFormat("int oneTwoThree = 123;\n" 9610 "int oneTwo = 12;\n" 9611 "method();\n", 9612 Alignment); 9613 verifyFormat("int oneTwoThree = 123; // comment\n" 9614 "int oneTwo = 12; // comment", 9615 Alignment); 9616 EXPECT_EQ("int a = 5;\n" 9617 "\n" 9618 "int oneTwoThree = 123;", 9619 format("int a = 5;\n" 9620 "\n" 9621 "int oneTwoThree= 123;", 9622 Alignment)); 9623 EXPECT_EQ("int a = 5;\n" 9624 "int one = 1;\n" 9625 "\n" 9626 "int oneTwoThree = 123;", 9627 format("int a = 5;\n" 9628 "int one = 1;\n" 9629 "\n" 9630 "int oneTwoThree = 123;", 9631 Alignment)); 9632 EXPECT_EQ("int a = 5;\n" 9633 "int one = 1;\n" 9634 "\n" 9635 "int oneTwoThree = 123;\n" 9636 "int oneTwo = 12;", 9637 format("int a = 5;\n" 9638 "int one = 1;\n" 9639 "\n" 9640 "int oneTwoThree = 123;\n" 9641 "int oneTwo = 12;", 9642 Alignment)); 9643 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9644 verifyFormat("#define A \\\n" 9645 " int aaaa = 12; \\\n" 9646 " int b = 23; \\\n" 9647 " int ccc = 234; \\\n" 9648 " int dddddddddd = 2345;", 9649 Alignment); 9650 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9651 verifyFormat("#define A \\\n" 9652 " int aaaa = 12; \\\n" 9653 " int b = 23; \\\n" 9654 " int ccc = 234; \\\n" 9655 " int dddddddddd = 2345;", 9656 Alignment); 9657 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9658 verifyFormat("#define A " 9659 " \\\n" 9660 " int aaaa = 12; " 9661 " \\\n" 9662 " int b = 23; " 9663 " \\\n" 9664 " int ccc = 234; " 9665 " \\\n" 9666 " int dddddddddd = 2345;", 9667 Alignment); 9668 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9669 "k = 4, int l = 5,\n" 9670 " int m = 6) {\n" 9671 " int j = 10;\n" 9672 " otherThing = 1;\n" 9673 "}", 9674 Alignment); 9675 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9676 " int i = 1;\n" 9677 " int j = 2;\n" 9678 " int big = 10000;\n" 9679 "}", 9680 Alignment); 9681 verifyFormat("class C {\n" 9682 "public:\n" 9683 " int i = 1;\n" 9684 " virtual void f() = 0;\n" 9685 "};", 9686 Alignment); 9687 verifyFormat("int i = 1;\n" 9688 "if (SomeType t = getSomething()) {\n" 9689 "}\n" 9690 "int j = 2;\n" 9691 "int big = 10000;", 9692 Alignment); 9693 verifyFormat("int j = 7;\n" 9694 "for (int k = 0; k < N; ++k) {\n" 9695 "}\n" 9696 "int j = 2;\n" 9697 "int big = 10000;\n" 9698 "}", 9699 Alignment); 9700 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9701 verifyFormat("int i = 1;\n" 9702 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9703 " = someLooooooooooooooooongFunction();\n" 9704 "int j = 2;", 9705 Alignment); 9706 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9707 verifyFormat("int i = 1;\n" 9708 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9709 " someLooooooooooooooooongFunction();\n" 9710 "int j = 2;", 9711 Alignment); 9712 9713 verifyFormat("auto lambda = []() {\n" 9714 " auto i = 0;\n" 9715 " return 0;\n" 9716 "};\n" 9717 "int i = 0;\n" 9718 "auto v = type{\n" 9719 " i = 1, //\n" 9720 " (i = 2), //\n" 9721 " i = 3 //\n" 9722 "};", 9723 Alignment); 9724 9725 verifyFormat( 9726 "int i = 1;\n" 9727 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9728 " loooooooooooooooooooooongParameterB);\n" 9729 "int j = 2;", 9730 Alignment); 9731 9732 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 9733 " typename B = very_long_type_name_1,\n" 9734 " typename T_2 = very_long_type_name_2>\n" 9735 "auto foo() {}\n", 9736 Alignment); 9737 verifyFormat("int a, b = 1;\n" 9738 "int c = 2;\n" 9739 "int dd = 3;\n", 9740 Alignment); 9741 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9742 "float b[1][] = {{3.f}};\n", 9743 Alignment); 9744 verifyFormat("for (int i = 0; i < 1; i++)\n" 9745 " int x = 1;\n", 9746 Alignment); 9747 verifyFormat("for (i = 0; i < 1; i++)\n" 9748 " x = 1;\n" 9749 "y = 1;\n", 9750 Alignment); 9751 } 9752 9753 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 9754 FormatStyle Alignment = getLLVMStyle(); 9755 Alignment.AlignConsecutiveDeclarations = false; 9756 verifyFormat("float const a = 5;\n" 9757 "int oneTwoThree = 123;", 9758 Alignment); 9759 verifyFormat("int a = 5;\n" 9760 "float const oneTwoThree = 123;", 9761 Alignment); 9762 9763 Alignment.AlignConsecutiveDeclarations = true; 9764 verifyFormat("float const a = 5;\n" 9765 "int oneTwoThree = 123;", 9766 Alignment); 9767 verifyFormat("int a = method();\n" 9768 "float const oneTwoThree = 133;", 9769 Alignment); 9770 verifyFormat("int i = 1, j = 10;\n" 9771 "something = 2000;", 9772 Alignment); 9773 verifyFormat("something = 2000;\n" 9774 "int i = 1, j = 10;\n", 9775 Alignment); 9776 verifyFormat("float something = 2000;\n" 9777 "double another = 911;\n" 9778 "int i = 1, j = 10;\n" 9779 "const int *oneMore = 1;\n" 9780 "unsigned i = 2;", 9781 Alignment); 9782 verifyFormat("float a = 5;\n" 9783 "int one = 1;\n" 9784 "method();\n" 9785 "const double oneTwoThree = 123;\n" 9786 "const unsigned int oneTwo = 12;", 9787 Alignment); 9788 verifyFormat("int oneTwoThree{0}; // comment\n" 9789 "unsigned oneTwo; // comment", 9790 Alignment); 9791 EXPECT_EQ("float const a = 5;\n" 9792 "\n" 9793 "int oneTwoThree = 123;", 9794 format("float const a = 5;\n" 9795 "\n" 9796 "int oneTwoThree= 123;", 9797 Alignment)); 9798 EXPECT_EQ("float a = 5;\n" 9799 "int one = 1;\n" 9800 "\n" 9801 "unsigned oneTwoThree = 123;", 9802 format("float a = 5;\n" 9803 "int one = 1;\n" 9804 "\n" 9805 "unsigned oneTwoThree = 123;", 9806 Alignment)); 9807 EXPECT_EQ("float a = 5;\n" 9808 "int one = 1;\n" 9809 "\n" 9810 "unsigned oneTwoThree = 123;\n" 9811 "int oneTwo = 12;", 9812 format("float a = 5;\n" 9813 "int one = 1;\n" 9814 "\n" 9815 "unsigned oneTwoThree = 123;\n" 9816 "int oneTwo = 12;", 9817 Alignment)); 9818 // Function prototype alignment 9819 verifyFormat("int a();\n" 9820 "double b();", 9821 Alignment); 9822 verifyFormat("int a(int x);\n" 9823 "double b();", 9824 Alignment); 9825 unsigned OldColumnLimit = Alignment.ColumnLimit; 9826 // We need to set ColumnLimit to zero, in order to stress nested alignments, 9827 // otherwise the function parameters will be re-flowed onto a single line. 9828 Alignment.ColumnLimit = 0; 9829 EXPECT_EQ("int a(int x,\n" 9830 " float y);\n" 9831 "double b(int x,\n" 9832 " double y);", 9833 format("int a(int x,\n" 9834 " float y);\n" 9835 "double b(int x,\n" 9836 " double y);", 9837 Alignment)); 9838 // This ensures that function parameters of function declarations are 9839 // correctly indented when their owning functions are indented. 9840 // The failure case here is for 'double y' to not be indented enough. 9841 EXPECT_EQ("double a(int x);\n" 9842 "int b(int y,\n" 9843 " double z);", 9844 format("double a(int x);\n" 9845 "int b(int y,\n" 9846 " double z);", 9847 Alignment)); 9848 // Set ColumnLimit low so that we induce wrapping immediately after 9849 // the function name and opening paren. 9850 Alignment.ColumnLimit = 13; 9851 verifyFormat("int function(\n" 9852 " int x,\n" 9853 " bool y);", 9854 Alignment); 9855 Alignment.ColumnLimit = OldColumnLimit; 9856 // Ensure function pointers don't screw up recursive alignment 9857 verifyFormat("int a(int x, void (*fp)(int y));\n" 9858 "double b();", 9859 Alignment); 9860 Alignment.AlignConsecutiveAssignments = true; 9861 // Ensure recursive alignment is broken by function braces, so that the 9862 // "a = 1" does not align with subsequent assignments inside the function 9863 // body. 9864 verifyFormat("int func(int a = 1) {\n" 9865 " int b = 2;\n" 9866 " int cc = 3;\n" 9867 "}", 9868 Alignment); 9869 verifyFormat("float something = 2000;\n" 9870 "double another = 911;\n" 9871 "int i = 1, j = 10;\n" 9872 "const int *oneMore = 1;\n" 9873 "unsigned i = 2;", 9874 Alignment); 9875 verifyFormat("int oneTwoThree = {0}; // comment\n" 9876 "unsigned oneTwo = 0; // comment", 9877 Alignment); 9878 // Make sure that scope is correctly tracked, in the absence of braces 9879 verifyFormat("for (int i = 0; i < n; i++)\n" 9880 " j = i;\n" 9881 "double x = 1;\n", 9882 Alignment); 9883 verifyFormat("if (int i = 0)\n" 9884 " j = i;\n" 9885 "double x = 1;\n", 9886 Alignment); 9887 // Ensure operator[] and operator() are comprehended 9888 verifyFormat("struct test {\n" 9889 " long long int foo();\n" 9890 " int operator[](int a);\n" 9891 " double bar();\n" 9892 "};\n", 9893 Alignment); 9894 verifyFormat("struct test {\n" 9895 " long long int foo();\n" 9896 " int operator()(int a);\n" 9897 " double bar();\n" 9898 "};\n", 9899 Alignment); 9900 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9901 " int const i = 1;\n" 9902 " int * j = 2;\n" 9903 " int big = 10000;\n" 9904 "\n" 9905 " unsigned oneTwoThree = 123;\n" 9906 " int oneTwo = 12;\n" 9907 " method();\n" 9908 " float k = 2;\n" 9909 " int ll = 10000;\n" 9910 "}", 9911 format("void SomeFunction(int parameter= 0) {\n" 9912 " int const i= 1;\n" 9913 " int *j=2;\n" 9914 " int big = 10000;\n" 9915 "\n" 9916 "unsigned oneTwoThree =123;\n" 9917 "int oneTwo = 12;\n" 9918 " method();\n" 9919 "float k= 2;\n" 9920 "int ll=10000;\n" 9921 "}", 9922 Alignment)); 9923 Alignment.AlignConsecutiveAssignments = false; 9924 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9925 verifyFormat("#define A \\\n" 9926 " int aaaa = 12; \\\n" 9927 " float b = 23; \\\n" 9928 " const int ccc = 234; \\\n" 9929 " unsigned dddddddddd = 2345;", 9930 Alignment); 9931 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9932 verifyFormat("#define A \\\n" 9933 " int aaaa = 12; \\\n" 9934 " float b = 23; \\\n" 9935 " const int ccc = 234; \\\n" 9936 " unsigned dddddddddd = 2345;", 9937 Alignment); 9938 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9939 Alignment.ColumnLimit = 30; 9940 verifyFormat("#define A \\\n" 9941 " int aaaa = 12; \\\n" 9942 " float b = 23; \\\n" 9943 " const int ccc = 234; \\\n" 9944 " int dddddddddd = 2345;", 9945 Alignment); 9946 Alignment.ColumnLimit = 80; 9947 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9948 "k = 4, int l = 5,\n" 9949 " int m = 6) {\n" 9950 " const int j = 10;\n" 9951 " otherThing = 1;\n" 9952 "}", 9953 Alignment); 9954 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9955 " int const i = 1;\n" 9956 " int * j = 2;\n" 9957 " int big = 10000;\n" 9958 "}", 9959 Alignment); 9960 verifyFormat("class C {\n" 9961 "public:\n" 9962 " int i = 1;\n" 9963 " virtual void f() = 0;\n" 9964 "};", 9965 Alignment); 9966 verifyFormat("float i = 1;\n" 9967 "if (SomeType t = getSomething()) {\n" 9968 "}\n" 9969 "const unsigned j = 2;\n" 9970 "int big = 10000;", 9971 Alignment); 9972 verifyFormat("float j = 7;\n" 9973 "for (int k = 0; k < N; ++k) {\n" 9974 "}\n" 9975 "unsigned j = 2;\n" 9976 "int big = 10000;\n" 9977 "}", 9978 Alignment); 9979 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9980 verifyFormat("float i = 1;\n" 9981 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9982 " = someLooooooooooooooooongFunction();\n" 9983 "int j = 2;", 9984 Alignment); 9985 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9986 verifyFormat("int i = 1;\n" 9987 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9988 " someLooooooooooooooooongFunction();\n" 9989 "int j = 2;", 9990 Alignment); 9991 9992 Alignment.AlignConsecutiveAssignments = true; 9993 verifyFormat("auto lambda = []() {\n" 9994 " auto ii = 0;\n" 9995 " float j = 0;\n" 9996 " return 0;\n" 9997 "};\n" 9998 "int i = 0;\n" 9999 "float i2 = 0;\n" 10000 "auto v = type{\n" 10001 " i = 1, //\n" 10002 " (i = 2), //\n" 10003 " i = 3 //\n" 10004 "};", 10005 Alignment); 10006 Alignment.AlignConsecutiveAssignments = false; 10007 10008 verifyFormat( 10009 "int i = 1;\n" 10010 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 10011 " loooooooooooooooooooooongParameterB);\n" 10012 "int j = 2;", 10013 Alignment); 10014 10015 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 10016 // We expect declarations and assignments to align, as long as it doesn't 10017 // exceed the column limit, starting a new alignment sequence whenever it 10018 // happens. 10019 Alignment.AlignConsecutiveAssignments = true; 10020 Alignment.ColumnLimit = 30; 10021 verifyFormat("float ii = 1;\n" 10022 "unsigned j = 2;\n" 10023 "int someVerylongVariable = 1;\n" 10024 "AnotherLongType ll = 123456;\n" 10025 "VeryVeryLongType k = 2;\n" 10026 "int myvar = 1;", 10027 Alignment); 10028 Alignment.ColumnLimit = 80; 10029 Alignment.AlignConsecutiveAssignments = false; 10030 10031 verifyFormat( 10032 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 10033 " typename LongType, typename B>\n" 10034 "auto foo() {}\n", 10035 Alignment); 10036 verifyFormat("float a, b = 1;\n" 10037 "int c = 2;\n" 10038 "int dd = 3;\n", 10039 Alignment); 10040 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 10041 "float b[1][] = {{3.f}};\n", 10042 Alignment); 10043 Alignment.AlignConsecutiveAssignments = true; 10044 verifyFormat("float a, b = 1;\n" 10045 "int c = 2;\n" 10046 "int dd = 3;\n", 10047 Alignment); 10048 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 10049 "float b[1][] = {{3.f}};\n", 10050 Alignment); 10051 Alignment.AlignConsecutiveAssignments = false; 10052 10053 Alignment.ColumnLimit = 30; 10054 Alignment.BinPackParameters = false; 10055 verifyFormat("void foo(float a,\n" 10056 " float b,\n" 10057 " int c,\n" 10058 " uint32_t *d) {\n" 10059 " int * e = 0;\n" 10060 " float f = 0;\n" 10061 " double g = 0;\n" 10062 "}\n" 10063 "void bar(ino_t a,\n" 10064 " int b,\n" 10065 " uint32_t *c,\n" 10066 " bool d) {}\n", 10067 Alignment); 10068 Alignment.BinPackParameters = true; 10069 Alignment.ColumnLimit = 80; 10070 10071 // Bug 33507 10072 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 10073 verifyFormat( 10074 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 10075 " static const Version verVs2017;\n" 10076 " return true;\n" 10077 "});\n", 10078 Alignment); 10079 Alignment.PointerAlignment = FormatStyle::PAS_Right; 10080 10081 // See llvm.org/PR35641 10082 Alignment.AlignConsecutiveDeclarations = true; 10083 verifyFormat("int func() { //\n" 10084 " int b;\n" 10085 " unsigned c;\n" 10086 "}", 10087 Alignment); 10088 } 10089 10090 TEST_F(FormatTest, LinuxBraceBreaking) { 10091 FormatStyle LinuxBraceStyle = getLLVMStyle(); 10092 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 10093 verifyFormat("namespace a\n" 10094 "{\n" 10095 "class A\n" 10096 "{\n" 10097 " void f()\n" 10098 " {\n" 10099 " if (true) {\n" 10100 " a();\n" 10101 " b();\n" 10102 " } else {\n" 10103 " a();\n" 10104 " }\n" 10105 " }\n" 10106 " void g() { return; }\n" 10107 "};\n" 10108 "struct B {\n" 10109 " int x;\n" 10110 "};\n" 10111 "} // namespace a\n", 10112 LinuxBraceStyle); 10113 verifyFormat("enum X {\n" 10114 " Y = 0,\n" 10115 "}\n", 10116 LinuxBraceStyle); 10117 verifyFormat("struct S {\n" 10118 " int Type;\n" 10119 " union {\n" 10120 " int x;\n" 10121 " double y;\n" 10122 " } Value;\n" 10123 " class C\n" 10124 " {\n" 10125 " MyFavoriteType Value;\n" 10126 " } Class;\n" 10127 "}\n", 10128 LinuxBraceStyle); 10129 } 10130 10131 TEST_F(FormatTest, MozillaBraceBreaking) { 10132 FormatStyle MozillaBraceStyle = getLLVMStyle(); 10133 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 10134 MozillaBraceStyle.FixNamespaceComments = false; 10135 verifyFormat("namespace a {\n" 10136 "class A\n" 10137 "{\n" 10138 " void f()\n" 10139 " {\n" 10140 " if (true) {\n" 10141 " a();\n" 10142 " b();\n" 10143 " }\n" 10144 " }\n" 10145 " void g() { return; }\n" 10146 "};\n" 10147 "enum E\n" 10148 "{\n" 10149 " A,\n" 10150 " // foo\n" 10151 " B,\n" 10152 " C\n" 10153 "};\n" 10154 "struct B\n" 10155 "{\n" 10156 " int x;\n" 10157 "};\n" 10158 "}\n", 10159 MozillaBraceStyle); 10160 verifyFormat("struct S\n" 10161 "{\n" 10162 " int Type;\n" 10163 " union\n" 10164 " {\n" 10165 " int x;\n" 10166 " double y;\n" 10167 " } Value;\n" 10168 " class C\n" 10169 " {\n" 10170 " MyFavoriteType Value;\n" 10171 " } Class;\n" 10172 "}\n", 10173 MozillaBraceStyle); 10174 } 10175 10176 TEST_F(FormatTest, StroustrupBraceBreaking) { 10177 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 10178 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10179 verifyFormat("namespace a {\n" 10180 "class A {\n" 10181 " void f()\n" 10182 " {\n" 10183 " if (true) {\n" 10184 " a();\n" 10185 " b();\n" 10186 " }\n" 10187 " }\n" 10188 " void g() { return; }\n" 10189 "};\n" 10190 "struct B {\n" 10191 " int x;\n" 10192 "};\n" 10193 "} // namespace a\n", 10194 StroustrupBraceStyle); 10195 10196 verifyFormat("void foo()\n" 10197 "{\n" 10198 " if (a) {\n" 10199 " a();\n" 10200 " }\n" 10201 " else {\n" 10202 " b();\n" 10203 " }\n" 10204 "}\n", 10205 StroustrupBraceStyle); 10206 10207 verifyFormat("#ifdef _DEBUG\n" 10208 "int foo(int i = 0)\n" 10209 "#else\n" 10210 "int foo(int i = 5)\n" 10211 "#endif\n" 10212 "{\n" 10213 " return i;\n" 10214 "}", 10215 StroustrupBraceStyle); 10216 10217 verifyFormat("void foo() {}\n" 10218 "void bar()\n" 10219 "#ifdef _DEBUG\n" 10220 "{\n" 10221 " foo();\n" 10222 "}\n" 10223 "#else\n" 10224 "{\n" 10225 "}\n" 10226 "#endif", 10227 StroustrupBraceStyle); 10228 10229 verifyFormat("void foobar() { int i = 5; }\n" 10230 "#ifdef _DEBUG\n" 10231 "void bar() {}\n" 10232 "#else\n" 10233 "void bar() { foobar(); }\n" 10234 "#endif", 10235 StroustrupBraceStyle); 10236 } 10237 10238 TEST_F(FormatTest, AllmanBraceBreaking) { 10239 FormatStyle AllmanBraceStyle = getLLVMStyle(); 10240 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 10241 10242 EXPECT_EQ("namespace a\n" 10243 "{\n" 10244 "void f();\n" 10245 "void g();\n" 10246 "} // namespace a\n", 10247 format("namespace a\n" 10248 "{\n" 10249 "void f();\n" 10250 "void g();\n" 10251 "}\n", 10252 AllmanBraceStyle)); 10253 10254 verifyFormat("namespace a\n" 10255 "{\n" 10256 "class A\n" 10257 "{\n" 10258 " void f()\n" 10259 " {\n" 10260 " if (true)\n" 10261 " {\n" 10262 " a();\n" 10263 " b();\n" 10264 " }\n" 10265 " }\n" 10266 " void g() { return; }\n" 10267 "};\n" 10268 "struct B\n" 10269 "{\n" 10270 " int x;\n" 10271 "};\n" 10272 "} // namespace a", 10273 AllmanBraceStyle); 10274 10275 verifyFormat("void f()\n" 10276 "{\n" 10277 " if (true)\n" 10278 " {\n" 10279 " a();\n" 10280 " }\n" 10281 " else if (false)\n" 10282 " {\n" 10283 " b();\n" 10284 " }\n" 10285 " else\n" 10286 " {\n" 10287 " c();\n" 10288 " }\n" 10289 "}\n", 10290 AllmanBraceStyle); 10291 10292 verifyFormat("void f()\n" 10293 "{\n" 10294 " for (int i = 0; i < 10; ++i)\n" 10295 " {\n" 10296 " a();\n" 10297 " }\n" 10298 " while (false)\n" 10299 " {\n" 10300 " b();\n" 10301 " }\n" 10302 " do\n" 10303 " {\n" 10304 " c();\n" 10305 " } while (false)\n" 10306 "}\n", 10307 AllmanBraceStyle); 10308 10309 verifyFormat("void f(int a)\n" 10310 "{\n" 10311 " switch (a)\n" 10312 " {\n" 10313 " case 0:\n" 10314 " break;\n" 10315 " case 1:\n" 10316 " {\n" 10317 " break;\n" 10318 " }\n" 10319 " case 2:\n" 10320 " {\n" 10321 " }\n" 10322 " break;\n" 10323 " default:\n" 10324 " break;\n" 10325 " }\n" 10326 "}\n", 10327 AllmanBraceStyle); 10328 10329 verifyFormat("enum X\n" 10330 "{\n" 10331 " Y = 0,\n" 10332 "}\n", 10333 AllmanBraceStyle); 10334 verifyFormat("enum X\n" 10335 "{\n" 10336 " Y = 0\n" 10337 "}\n", 10338 AllmanBraceStyle); 10339 10340 verifyFormat("@interface BSApplicationController ()\n" 10341 "{\n" 10342 "@private\n" 10343 " id _extraIvar;\n" 10344 "}\n" 10345 "@end\n", 10346 AllmanBraceStyle); 10347 10348 verifyFormat("#ifdef _DEBUG\n" 10349 "int foo(int i = 0)\n" 10350 "#else\n" 10351 "int foo(int i = 5)\n" 10352 "#endif\n" 10353 "{\n" 10354 " return i;\n" 10355 "}", 10356 AllmanBraceStyle); 10357 10358 verifyFormat("void foo() {}\n" 10359 "void bar()\n" 10360 "#ifdef _DEBUG\n" 10361 "{\n" 10362 " foo();\n" 10363 "}\n" 10364 "#else\n" 10365 "{\n" 10366 "}\n" 10367 "#endif", 10368 AllmanBraceStyle); 10369 10370 verifyFormat("void foobar() { int i = 5; }\n" 10371 "#ifdef _DEBUG\n" 10372 "void bar() {}\n" 10373 "#else\n" 10374 "void bar() { foobar(); }\n" 10375 "#endif", 10376 AllmanBraceStyle); 10377 10378 // This shouldn't affect ObjC blocks.. 10379 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 10380 " // ...\n" 10381 " int i;\n" 10382 "}];", 10383 AllmanBraceStyle); 10384 verifyFormat("void (^block)(void) = ^{\n" 10385 " // ...\n" 10386 " int i;\n" 10387 "};", 10388 AllmanBraceStyle); 10389 // .. or dict literals. 10390 verifyFormat("void f()\n" 10391 "{\n" 10392 " // ...\n" 10393 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 10394 "}", 10395 AllmanBraceStyle); 10396 verifyFormat("void f()\n" 10397 "{\n" 10398 " // ...\n" 10399 " [object someMethod:@{a : @\"b\"}];\n" 10400 "}", 10401 AllmanBraceStyle); 10402 verifyFormat("int f()\n" 10403 "{ // comment\n" 10404 " return 42;\n" 10405 "}", 10406 AllmanBraceStyle); 10407 10408 AllmanBraceStyle.ColumnLimit = 19; 10409 verifyFormat("void f() { int i; }", AllmanBraceStyle); 10410 AllmanBraceStyle.ColumnLimit = 18; 10411 verifyFormat("void f()\n" 10412 "{\n" 10413 " int i;\n" 10414 "}", 10415 AllmanBraceStyle); 10416 AllmanBraceStyle.ColumnLimit = 80; 10417 10418 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 10419 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 10420 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 10421 verifyFormat("void f(bool b)\n" 10422 "{\n" 10423 " if (b)\n" 10424 " {\n" 10425 " return;\n" 10426 " }\n" 10427 "}\n", 10428 BreakBeforeBraceShortIfs); 10429 verifyFormat("void f(bool b)\n" 10430 "{\n" 10431 " if constexpr (b)\n" 10432 " {\n" 10433 " return;\n" 10434 " }\n" 10435 "}\n", 10436 BreakBeforeBraceShortIfs); 10437 verifyFormat("void f(bool b)\n" 10438 "{\n" 10439 " if (b) return;\n" 10440 "}\n", 10441 BreakBeforeBraceShortIfs); 10442 verifyFormat("void f(bool b)\n" 10443 "{\n" 10444 " if constexpr (b) return;\n" 10445 "}\n", 10446 BreakBeforeBraceShortIfs); 10447 verifyFormat("void f(bool b)\n" 10448 "{\n" 10449 " while (b)\n" 10450 " {\n" 10451 " return;\n" 10452 " }\n" 10453 "}\n", 10454 BreakBeforeBraceShortIfs); 10455 } 10456 10457 TEST_F(FormatTest, GNUBraceBreaking) { 10458 FormatStyle GNUBraceStyle = getLLVMStyle(); 10459 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 10460 verifyFormat("namespace a\n" 10461 "{\n" 10462 "class A\n" 10463 "{\n" 10464 " void f()\n" 10465 " {\n" 10466 " int a;\n" 10467 " {\n" 10468 " int b;\n" 10469 " }\n" 10470 " if (true)\n" 10471 " {\n" 10472 " a();\n" 10473 " b();\n" 10474 " }\n" 10475 " }\n" 10476 " void g() { return; }\n" 10477 "}\n" 10478 "} // namespace a", 10479 GNUBraceStyle); 10480 10481 verifyFormat("void f()\n" 10482 "{\n" 10483 " if (true)\n" 10484 " {\n" 10485 " a();\n" 10486 " }\n" 10487 " else if (false)\n" 10488 " {\n" 10489 " b();\n" 10490 " }\n" 10491 " else\n" 10492 " {\n" 10493 " c();\n" 10494 " }\n" 10495 "}\n", 10496 GNUBraceStyle); 10497 10498 verifyFormat("void f()\n" 10499 "{\n" 10500 " for (int i = 0; i < 10; ++i)\n" 10501 " {\n" 10502 " a();\n" 10503 " }\n" 10504 " while (false)\n" 10505 " {\n" 10506 " b();\n" 10507 " }\n" 10508 " do\n" 10509 " {\n" 10510 " c();\n" 10511 " }\n" 10512 " while (false);\n" 10513 "}\n", 10514 GNUBraceStyle); 10515 10516 verifyFormat("void f(int a)\n" 10517 "{\n" 10518 " switch (a)\n" 10519 " {\n" 10520 " case 0:\n" 10521 " break;\n" 10522 " case 1:\n" 10523 " {\n" 10524 " break;\n" 10525 " }\n" 10526 " case 2:\n" 10527 " {\n" 10528 " }\n" 10529 " break;\n" 10530 " default:\n" 10531 " break;\n" 10532 " }\n" 10533 "}\n", 10534 GNUBraceStyle); 10535 10536 verifyFormat("enum X\n" 10537 "{\n" 10538 " Y = 0,\n" 10539 "}\n", 10540 GNUBraceStyle); 10541 10542 verifyFormat("@interface BSApplicationController ()\n" 10543 "{\n" 10544 "@private\n" 10545 " id _extraIvar;\n" 10546 "}\n" 10547 "@end\n", 10548 GNUBraceStyle); 10549 10550 verifyFormat("#ifdef _DEBUG\n" 10551 "int foo(int i = 0)\n" 10552 "#else\n" 10553 "int foo(int i = 5)\n" 10554 "#endif\n" 10555 "{\n" 10556 " return i;\n" 10557 "}", 10558 GNUBraceStyle); 10559 10560 verifyFormat("void foo() {}\n" 10561 "void bar()\n" 10562 "#ifdef _DEBUG\n" 10563 "{\n" 10564 " foo();\n" 10565 "}\n" 10566 "#else\n" 10567 "{\n" 10568 "}\n" 10569 "#endif", 10570 GNUBraceStyle); 10571 10572 verifyFormat("void foobar() { int i = 5; }\n" 10573 "#ifdef _DEBUG\n" 10574 "void bar() {}\n" 10575 "#else\n" 10576 "void bar() { foobar(); }\n" 10577 "#endif", 10578 GNUBraceStyle); 10579 } 10580 10581 TEST_F(FormatTest, WebKitBraceBreaking) { 10582 FormatStyle WebKitBraceStyle = getLLVMStyle(); 10583 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 10584 WebKitBraceStyle.FixNamespaceComments = false; 10585 verifyFormat("namespace a {\n" 10586 "class A {\n" 10587 " void f()\n" 10588 " {\n" 10589 " if (true) {\n" 10590 " a();\n" 10591 " b();\n" 10592 " }\n" 10593 " }\n" 10594 " void g() { return; }\n" 10595 "};\n" 10596 "enum E {\n" 10597 " A,\n" 10598 " // foo\n" 10599 " B,\n" 10600 " C\n" 10601 "};\n" 10602 "struct B {\n" 10603 " int x;\n" 10604 "};\n" 10605 "}\n", 10606 WebKitBraceStyle); 10607 verifyFormat("struct S {\n" 10608 " int Type;\n" 10609 " union {\n" 10610 " int x;\n" 10611 " double y;\n" 10612 " } Value;\n" 10613 " class C {\n" 10614 " MyFavoriteType Value;\n" 10615 " } Class;\n" 10616 "};\n", 10617 WebKitBraceStyle); 10618 } 10619 10620 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 10621 verifyFormat("void f() {\n" 10622 " try {\n" 10623 " } catch (const Exception &e) {\n" 10624 " }\n" 10625 "}\n", 10626 getLLVMStyle()); 10627 } 10628 10629 TEST_F(FormatTest, UnderstandsPragmas) { 10630 verifyFormat("#pragma omp reduction(| : var)"); 10631 verifyFormat("#pragma omp reduction(+ : var)"); 10632 10633 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 10634 "(including parentheses).", 10635 format("#pragma mark Any non-hyphenated or hyphenated string " 10636 "(including parentheses).")); 10637 } 10638 10639 TEST_F(FormatTest, UnderstandPragmaOption) { 10640 verifyFormat("#pragma option -C -A"); 10641 10642 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 10643 } 10644 10645 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 10646 FormatStyle Style = getLLVMStyle(); 10647 Style.ColumnLimit = 20; 10648 10649 verifyFormat("int a; // the\n" 10650 " // comment", Style); 10651 EXPECT_EQ("int a; /* first line\n" 10652 " * second\n" 10653 " * line third\n" 10654 " * line\n" 10655 " */", 10656 format("int a; /* first line\n" 10657 " * second\n" 10658 " * line third\n" 10659 " * line\n" 10660 " */", 10661 Style)); 10662 EXPECT_EQ("int a; // first line\n" 10663 " // second\n" 10664 " // line third\n" 10665 " // line", 10666 format("int a; // first line\n" 10667 " // second line\n" 10668 " // third line", 10669 Style)); 10670 10671 Style.PenaltyExcessCharacter = 90; 10672 verifyFormat("int a; // the comment", Style); 10673 EXPECT_EQ("int a; // the comment\n" 10674 " // aaa", 10675 format("int a; // the comment aaa", Style)); 10676 EXPECT_EQ("int a; /* first line\n" 10677 " * second line\n" 10678 " * third line\n" 10679 " */", 10680 format("int a; /* first line\n" 10681 " * second line\n" 10682 " * third line\n" 10683 " */", 10684 Style)); 10685 EXPECT_EQ("int a; // first line\n" 10686 " // second line\n" 10687 " // third line", 10688 format("int a; // first line\n" 10689 " // second line\n" 10690 " // third line", 10691 Style)); 10692 // FIXME: Investigate why this is not getting the same layout as the test 10693 // above. 10694 EXPECT_EQ("int a; /* first line\n" 10695 " * second line\n" 10696 " * third line\n" 10697 " */", 10698 format("int a; /* first line second line third line" 10699 "\n*/", 10700 Style)); 10701 10702 EXPECT_EQ("// foo bar baz bazfoo\n" 10703 "// foo bar foo bar\n", 10704 format("// foo bar baz bazfoo\n" 10705 "// foo bar foo bar\n", 10706 Style)); 10707 EXPECT_EQ("// foo bar baz bazfoo\n" 10708 "// foo bar foo bar\n", 10709 format("// foo bar baz bazfoo\n" 10710 "// foo bar foo bar\n", 10711 Style)); 10712 10713 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 10714 // next one. 10715 EXPECT_EQ("// foo bar baz bazfoo\n" 10716 "// bar foo bar\n", 10717 format("// foo bar baz bazfoo bar\n" 10718 "// foo bar\n", 10719 Style)); 10720 10721 EXPECT_EQ("// foo bar baz bazfoo\n" 10722 "// foo bar baz bazfoo\n" 10723 "// bar foo bar\n", 10724 format("// foo bar baz bazfoo\n" 10725 "// foo bar baz bazfoo bar\n" 10726 "// foo bar\n", 10727 Style)); 10728 10729 EXPECT_EQ("// foo bar baz bazfoo\n" 10730 "// foo bar baz bazfoo\n" 10731 "// bar foo bar\n", 10732 format("// foo bar baz bazfoo\n" 10733 "// foo bar baz bazfoo bar\n" 10734 "// foo bar\n", 10735 Style)); 10736 10737 // Make sure we do not keep protruding characters if strict mode reflow is 10738 // cheaper than keeping protruding characters. 10739 Style.ColumnLimit = 21; 10740 EXPECT_EQ("// foo foo foo foo\n" 10741 "// foo foo foo foo\n" 10742 "// foo foo foo foo\n", 10743 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", 10744 Style)); 10745 10746 EXPECT_EQ("int a = /* long block\n" 10747 " comment */\n" 10748 " 42;", 10749 format("int a = /* long block comment */ 42;", Style)); 10750 } 10751 10752 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 10753 for (size_t i = 1; i < Styles.size(); ++i) \ 10754 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 10755 << " differs from Style #0" 10756 10757 TEST_F(FormatTest, GetsPredefinedStyleByName) { 10758 SmallVector<FormatStyle, 3> Styles; 10759 Styles.resize(3); 10760 10761 Styles[0] = getLLVMStyle(); 10762 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 10763 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 10764 EXPECT_ALL_STYLES_EQUAL(Styles); 10765 10766 Styles[0] = getGoogleStyle(); 10767 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 10768 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 10769 EXPECT_ALL_STYLES_EQUAL(Styles); 10770 10771 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10772 EXPECT_TRUE( 10773 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 10774 EXPECT_TRUE( 10775 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 10776 EXPECT_ALL_STYLES_EQUAL(Styles); 10777 10778 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 10779 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 10780 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 10781 EXPECT_ALL_STYLES_EQUAL(Styles); 10782 10783 Styles[0] = getMozillaStyle(); 10784 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 10785 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 10786 EXPECT_ALL_STYLES_EQUAL(Styles); 10787 10788 Styles[0] = getWebKitStyle(); 10789 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 10790 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 10791 EXPECT_ALL_STYLES_EQUAL(Styles); 10792 10793 Styles[0] = getGNUStyle(); 10794 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 10795 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 10796 EXPECT_ALL_STYLES_EQUAL(Styles); 10797 10798 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 10799 } 10800 10801 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 10802 SmallVector<FormatStyle, 8> Styles; 10803 Styles.resize(2); 10804 10805 Styles[0] = getGoogleStyle(); 10806 Styles[1] = getLLVMStyle(); 10807 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10808 EXPECT_ALL_STYLES_EQUAL(Styles); 10809 10810 Styles.resize(5); 10811 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10812 Styles[1] = getLLVMStyle(); 10813 Styles[1].Language = FormatStyle::LK_JavaScript; 10814 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10815 10816 Styles[2] = getLLVMStyle(); 10817 Styles[2].Language = FormatStyle::LK_JavaScript; 10818 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 10819 "BasedOnStyle: Google", 10820 &Styles[2]) 10821 .value()); 10822 10823 Styles[3] = getLLVMStyle(); 10824 Styles[3].Language = FormatStyle::LK_JavaScript; 10825 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 10826 "Language: JavaScript", 10827 &Styles[3]) 10828 .value()); 10829 10830 Styles[4] = getLLVMStyle(); 10831 Styles[4].Language = FormatStyle::LK_JavaScript; 10832 EXPECT_EQ(0, parseConfiguration("---\n" 10833 "BasedOnStyle: LLVM\n" 10834 "IndentWidth: 123\n" 10835 "---\n" 10836 "BasedOnStyle: Google\n" 10837 "Language: JavaScript", 10838 &Styles[4]) 10839 .value()); 10840 EXPECT_ALL_STYLES_EQUAL(Styles); 10841 } 10842 10843 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 10844 Style.FIELD = false; \ 10845 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 10846 EXPECT_TRUE(Style.FIELD); \ 10847 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 10848 EXPECT_FALSE(Style.FIELD); 10849 10850 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 10851 10852 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 10853 Style.STRUCT.FIELD = false; \ 10854 EXPECT_EQ(0, \ 10855 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 10856 .value()); \ 10857 EXPECT_TRUE(Style.STRUCT.FIELD); \ 10858 EXPECT_EQ(0, \ 10859 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 10860 .value()); \ 10861 EXPECT_FALSE(Style.STRUCT.FIELD); 10862 10863 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 10864 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 10865 10866 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 10867 EXPECT_NE(VALUE, Style.FIELD); \ 10868 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 10869 EXPECT_EQ(VALUE, Style.FIELD) 10870 10871 TEST_F(FormatTest, ParsesConfigurationBools) { 10872 FormatStyle Style = {}; 10873 Style.Language = FormatStyle::LK_Cpp; 10874 CHECK_PARSE_BOOL(AlignOperands); 10875 CHECK_PARSE_BOOL(AlignTrailingComments); 10876 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 10877 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 10878 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 10879 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 10880 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 10881 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 10882 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 10883 CHECK_PARSE_BOOL(BinPackArguments); 10884 CHECK_PARSE_BOOL(BinPackParameters); 10885 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 10886 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 10887 CHECK_PARSE_BOOL(BreakStringLiterals); 10888 CHECK_PARSE_BOOL(CompactNamespaces); 10889 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 10890 CHECK_PARSE_BOOL(DerivePointerAlignment); 10891 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 10892 CHECK_PARSE_BOOL(DisableFormat); 10893 CHECK_PARSE_BOOL(IndentCaseLabels); 10894 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 10895 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 10896 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 10897 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 10898 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 10899 CHECK_PARSE_BOOL(ReflowComments); 10900 CHECK_PARSE_BOOL(SortIncludes); 10901 CHECK_PARSE_BOOL(SortUsingDeclarations); 10902 CHECK_PARSE_BOOL(SpacesInParentheses); 10903 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 10904 CHECK_PARSE_BOOL(SpacesInAngles); 10905 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 10906 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 10907 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 10908 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 10909 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 10910 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 10911 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 10912 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 10913 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 10914 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 10915 10916 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 10917 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 10918 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 10919 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 10920 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 10921 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 10922 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 10923 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 10924 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 10925 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 10926 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 10927 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 10928 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 10929 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 10930 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 10931 } 10932 10933 #undef CHECK_PARSE_BOOL 10934 10935 TEST_F(FormatTest, ParsesConfiguration) { 10936 FormatStyle Style = {}; 10937 Style.Language = FormatStyle::LK_Cpp; 10938 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 10939 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 10940 ConstructorInitializerIndentWidth, 1234u); 10941 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 10942 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 10943 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 10944 CHECK_PARSE("PenaltyBreakAssignment: 1234", 10945 PenaltyBreakAssignment, 1234u); 10946 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 10947 PenaltyBreakBeforeFirstCallParameter, 1234u); 10948 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 10949 PenaltyBreakTemplateDeclaration, 1234u); 10950 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 10951 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 10952 PenaltyReturnTypeOnItsOwnLine, 1234u); 10953 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 10954 SpacesBeforeTrailingComments, 1234u); 10955 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 10956 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 10957 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 10958 10959 Style.PointerAlignment = FormatStyle::PAS_Middle; 10960 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 10961 FormatStyle::PAS_Left); 10962 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 10963 FormatStyle::PAS_Right); 10964 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 10965 FormatStyle::PAS_Middle); 10966 // For backward compatibility: 10967 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 10968 FormatStyle::PAS_Left); 10969 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 10970 FormatStyle::PAS_Right); 10971 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 10972 FormatStyle::PAS_Middle); 10973 10974 Style.Standard = FormatStyle::LS_Auto; 10975 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 10976 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 10977 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 10978 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 10979 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 10980 10981 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10982 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 10983 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 10984 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 10985 FormatStyle::BOS_None); 10986 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 10987 FormatStyle::BOS_All); 10988 // For backward compatibility: 10989 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 10990 FormatStyle::BOS_None); 10991 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 10992 FormatStyle::BOS_All); 10993 10994 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 10995 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 10996 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10997 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 10998 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 10999 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 11000 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 11001 // For backward compatibility: 11002 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 11003 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 11004 11005 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 11006 CHECK_PARSE("BreakInheritanceList: BeforeComma", 11007 BreakInheritanceList, FormatStyle::BILS_BeforeComma); 11008 CHECK_PARSE("BreakInheritanceList: AfterColon", 11009 BreakInheritanceList, FormatStyle::BILS_AfterColon); 11010 CHECK_PARSE("BreakInheritanceList: BeforeColon", 11011 BreakInheritanceList, FormatStyle::BILS_BeforeColon); 11012 // For backward compatibility: 11013 CHECK_PARSE("BreakBeforeInheritanceComma: true", 11014 BreakInheritanceList, FormatStyle::BILS_BeforeComma); 11015 11016 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11017 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 11018 FormatStyle::BAS_Align); 11019 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 11020 FormatStyle::BAS_DontAlign); 11021 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 11022 FormatStyle::BAS_AlwaysBreak); 11023 // For backward compatibility: 11024 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 11025 FormatStyle::BAS_DontAlign); 11026 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 11027 FormatStyle::BAS_Align); 11028 11029 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 11030 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 11031 FormatStyle::ENAS_DontAlign); 11032 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 11033 FormatStyle::ENAS_Left); 11034 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 11035 FormatStyle::ENAS_Right); 11036 // For backward compatibility: 11037 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 11038 FormatStyle::ENAS_Left); 11039 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 11040 FormatStyle::ENAS_Right); 11041 11042 Style.UseTab = FormatStyle::UT_ForIndentation; 11043 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 11044 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 11045 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 11046 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 11047 FormatStyle::UT_ForContinuationAndIndentation); 11048 // For backward compatibility: 11049 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 11050 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 11051 11052 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11053 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 11054 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 11055 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 11056 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 11057 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 11058 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 11059 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 11060 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 11061 // For backward compatibility: 11062 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 11063 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 11064 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 11065 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 11066 11067 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 11068 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 11069 FormatStyle::SBPO_Never); 11070 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 11071 FormatStyle::SBPO_Always); 11072 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 11073 FormatStyle::SBPO_ControlStatements); 11074 // For backward compatibility: 11075 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 11076 FormatStyle::SBPO_Never); 11077 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 11078 FormatStyle::SBPO_ControlStatements); 11079 11080 Style.ColumnLimit = 123; 11081 FormatStyle BaseStyle = getLLVMStyle(); 11082 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 11083 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 11084 11085 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 11086 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 11087 FormatStyle::BS_Attach); 11088 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 11089 FormatStyle::BS_Linux); 11090 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 11091 FormatStyle::BS_Mozilla); 11092 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 11093 FormatStyle::BS_Stroustrup); 11094 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 11095 FormatStyle::BS_Allman); 11096 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 11097 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 11098 FormatStyle::BS_WebKit); 11099 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 11100 FormatStyle::BS_Custom); 11101 11102 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 11103 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 11104 FormatStyle::RTBS_None); 11105 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 11106 FormatStyle::RTBS_All); 11107 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 11108 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 11109 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 11110 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 11111 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 11112 AlwaysBreakAfterReturnType, 11113 FormatStyle::RTBS_TopLevelDefinitions); 11114 11115 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 11116 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", AlwaysBreakTemplateDeclarations, 11117 FormatStyle::BTDS_No); 11118 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", AlwaysBreakTemplateDeclarations, 11119 FormatStyle::BTDS_MultiLine); 11120 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", AlwaysBreakTemplateDeclarations, 11121 FormatStyle::BTDS_Yes); 11122 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", AlwaysBreakTemplateDeclarations, 11123 FormatStyle::BTDS_MultiLine); 11124 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", AlwaysBreakTemplateDeclarations, 11125 FormatStyle::BTDS_Yes); 11126 11127 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 11128 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 11129 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 11130 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 11131 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 11132 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 11133 AlwaysBreakAfterDefinitionReturnType, 11134 FormatStyle::DRTBS_TopLevel); 11135 11136 Style.NamespaceIndentation = FormatStyle::NI_All; 11137 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 11138 FormatStyle::NI_None); 11139 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 11140 FormatStyle::NI_Inner); 11141 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 11142 FormatStyle::NI_All); 11143 11144 // FIXME: This is required because parsing a configuration simply overwrites 11145 // the first N elements of the list instead of resetting it. 11146 Style.ForEachMacros.clear(); 11147 std::vector<std::string> BoostForeach; 11148 BoostForeach.push_back("BOOST_FOREACH"); 11149 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 11150 std::vector<std::string> BoostAndQForeach; 11151 BoostAndQForeach.push_back("BOOST_FOREACH"); 11152 BoostAndQForeach.push_back("Q_FOREACH"); 11153 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 11154 BoostAndQForeach); 11155 11156 Style.StatementMacros.clear(); 11157 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 11158 std::vector<std::string>{"QUNUSED"}); 11159 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 11160 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 11161 11162 Style.IncludeStyle.IncludeCategories.clear(); 11163 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 11164 {"abc/.*", 2}, {".*", 1}}; 11165 CHECK_PARSE("IncludeCategories:\n" 11166 " - Regex: abc/.*\n" 11167 " Priority: 2\n" 11168 " - Regex: .*\n" 11169 " Priority: 1", 11170 IncludeStyle.IncludeCategories, ExpectedCategories); 11171 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 11172 "abc$"); 11173 11174 Style.RawStringFormats.clear(); 11175 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 11176 { 11177 FormatStyle::LK_TextProto, 11178 {"pb", "proto"}, 11179 {"PARSE_TEXT_PROTO"}, 11180 /*CanonicalDelimiter=*/"", 11181 "llvm", 11182 }, 11183 { 11184 FormatStyle::LK_Cpp, 11185 {"cc", "cpp"}, 11186 {"C_CODEBLOCK", "CPPEVAL"}, 11187 /*CanonicalDelimiter=*/"cc", 11188 /*BasedOnStyle=*/"", 11189 }, 11190 }; 11191 11192 CHECK_PARSE("RawStringFormats:\n" 11193 " - Language: TextProto\n" 11194 " Delimiters:\n" 11195 " - 'pb'\n" 11196 " - 'proto'\n" 11197 " EnclosingFunctions:\n" 11198 " - 'PARSE_TEXT_PROTO'\n" 11199 " BasedOnStyle: llvm\n" 11200 " - Language: Cpp\n" 11201 " Delimiters:\n" 11202 " - 'cc'\n" 11203 " - 'cpp'\n" 11204 " EnclosingFunctions:\n" 11205 " - 'C_CODEBLOCK'\n" 11206 " - 'CPPEVAL'\n" 11207 " CanonicalDelimiter: 'cc'", 11208 RawStringFormats, ExpectedRawStringFormats); 11209 } 11210 11211 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 11212 FormatStyle Style = {}; 11213 Style.Language = FormatStyle::LK_Cpp; 11214 CHECK_PARSE("Language: Cpp\n" 11215 "IndentWidth: 12", 11216 IndentWidth, 12u); 11217 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 11218 "IndentWidth: 34", 11219 &Style), 11220 ParseError::Unsuitable); 11221 EXPECT_EQ(12u, Style.IndentWidth); 11222 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 11223 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 11224 11225 Style.Language = FormatStyle::LK_JavaScript; 11226 CHECK_PARSE("Language: JavaScript\n" 11227 "IndentWidth: 12", 11228 IndentWidth, 12u); 11229 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 11230 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 11231 "IndentWidth: 34", 11232 &Style), 11233 ParseError::Unsuitable); 11234 EXPECT_EQ(23u, Style.IndentWidth); 11235 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 11236 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 11237 11238 CHECK_PARSE("BasedOnStyle: LLVM\n" 11239 "IndentWidth: 67", 11240 IndentWidth, 67u); 11241 11242 CHECK_PARSE("---\n" 11243 "Language: JavaScript\n" 11244 "IndentWidth: 12\n" 11245 "---\n" 11246 "Language: Cpp\n" 11247 "IndentWidth: 34\n" 11248 "...\n", 11249 IndentWidth, 12u); 11250 11251 Style.Language = FormatStyle::LK_Cpp; 11252 CHECK_PARSE("---\n" 11253 "Language: JavaScript\n" 11254 "IndentWidth: 12\n" 11255 "---\n" 11256 "Language: Cpp\n" 11257 "IndentWidth: 34\n" 11258 "...\n", 11259 IndentWidth, 34u); 11260 CHECK_PARSE("---\n" 11261 "IndentWidth: 78\n" 11262 "---\n" 11263 "Language: JavaScript\n" 11264 "IndentWidth: 56\n" 11265 "...\n", 11266 IndentWidth, 78u); 11267 11268 Style.ColumnLimit = 123; 11269 Style.IndentWidth = 234; 11270 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 11271 Style.TabWidth = 345; 11272 EXPECT_FALSE(parseConfiguration("---\n" 11273 "IndentWidth: 456\n" 11274 "BreakBeforeBraces: Allman\n" 11275 "---\n" 11276 "Language: JavaScript\n" 11277 "IndentWidth: 111\n" 11278 "TabWidth: 111\n" 11279 "---\n" 11280 "Language: Cpp\n" 11281 "BreakBeforeBraces: Stroustrup\n" 11282 "TabWidth: 789\n" 11283 "...\n", 11284 &Style)); 11285 EXPECT_EQ(123u, Style.ColumnLimit); 11286 EXPECT_EQ(456u, Style.IndentWidth); 11287 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 11288 EXPECT_EQ(789u, Style.TabWidth); 11289 11290 EXPECT_EQ(parseConfiguration("---\n" 11291 "Language: JavaScript\n" 11292 "IndentWidth: 56\n" 11293 "---\n" 11294 "IndentWidth: 78\n" 11295 "...\n", 11296 &Style), 11297 ParseError::Error); 11298 EXPECT_EQ(parseConfiguration("---\n" 11299 "Language: JavaScript\n" 11300 "IndentWidth: 56\n" 11301 "---\n" 11302 "Language: JavaScript\n" 11303 "IndentWidth: 78\n" 11304 "...\n", 11305 &Style), 11306 ParseError::Error); 11307 11308 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 11309 } 11310 11311 #undef CHECK_PARSE 11312 11313 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 11314 FormatStyle Style = {}; 11315 Style.Language = FormatStyle::LK_JavaScript; 11316 Style.BreakBeforeTernaryOperators = true; 11317 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 11318 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 11319 11320 Style.BreakBeforeTernaryOperators = true; 11321 EXPECT_EQ(0, parseConfiguration("---\n" 11322 "BasedOnStyle: Google\n" 11323 "---\n" 11324 "Language: JavaScript\n" 11325 "IndentWidth: 76\n" 11326 "...\n", 11327 &Style) 11328 .value()); 11329 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 11330 EXPECT_EQ(76u, Style.IndentWidth); 11331 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 11332 } 11333 11334 TEST_F(FormatTest, ConfigurationRoundTripTest) { 11335 FormatStyle Style = getLLVMStyle(); 11336 std::string YAML = configurationAsText(Style); 11337 FormatStyle ParsedStyle = {}; 11338 ParsedStyle.Language = FormatStyle::LK_Cpp; 11339 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 11340 EXPECT_EQ(Style, ParsedStyle); 11341 } 11342 11343 TEST_F(FormatTest, WorksFor8bitEncodings) { 11344 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 11345 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 11346 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 11347 "\"\xef\xee\xf0\xf3...\"", 11348 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 11349 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 11350 "\xef\xee\xf0\xf3...\"", 11351 getLLVMStyleWithColumns(12))); 11352 } 11353 11354 TEST_F(FormatTest, HandlesUTF8BOM) { 11355 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 11356 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 11357 format("\xef\xbb\xbf#include <iostream>")); 11358 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 11359 format("\xef\xbb\xbf\n#include <iostream>")); 11360 } 11361 11362 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 11363 #if !defined(_MSC_VER) 11364 11365 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 11366 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 11367 getLLVMStyleWithColumns(35)); 11368 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 11369 getLLVMStyleWithColumns(31)); 11370 verifyFormat("// Однажды в студёную зимнюю пору...", 11371 getLLVMStyleWithColumns(36)); 11372 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 11373 verifyFormat("/* Однажды в студёную зимнюю пору... */", 11374 getLLVMStyleWithColumns(39)); 11375 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 11376 getLLVMStyleWithColumns(35)); 11377 } 11378 11379 TEST_F(FormatTest, SplitsUTF8Strings) { 11380 // Non-printable characters' width is currently considered to be the length in 11381 // bytes in UTF8. The characters can be displayed in very different manner 11382 // (zero-width, single width with a substitution glyph, expanded to their code 11383 // (e.g. "<8d>"), so there's no single correct way to handle them. 11384 EXPECT_EQ("\"aaaaÄ\"\n" 11385 "\"\xc2\x8d\";", 11386 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 11387 EXPECT_EQ("\"aaaaaaaÄ\"\n" 11388 "\"\xc2\x8d\";", 11389 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 11390 EXPECT_EQ("\"Однажды, в \"\n" 11391 "\"студёную \"\n" 11392 "\"зимнюю \"\n" 11393 "\"пору,\"", 11394 format("\"Однажды, в студёную зимнюю пору,\"", 11395 getLLVMStyleWithColumns(13))); 11396 EXPECT_EQ( 11397 "\"一 二 三 \"\n" 11398 "\"四 五六 \"\n" 11399 "\"七 八 九 \"\n" 11400 "\"十\"", 11401 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 11402 EXPECT_EQ("\"一\t\"\n" 11403 "\"二 \t\"\n" 11404 "\"三 四 \"\n" 11405 "\"五\t\"\n" 11406 "\"六 \t\"\n" 11407 "\"七 \"\n" 11408 "\"八九十\tqq\"", 11409 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 11410 getLLVMStyleWithColumns(11))); 11411 11412 // UTF8 character in an escape sequence. 11413 EXPECT_EQ("\"aaaaaa\"\n" 11414 "\"\\\xC2\x8D\"", 11415 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 11416 } 11417 11418 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 11419 EXPECT_EQ("const char *sssss =\n" 11420 " \"一二三四五六七八\\\n" 11421 " 九 十\";", 11422 format("const char *sssss = \"一二三四五六七八\\\n" 11423 " 九 十\";", 11424 getLLVMStyleWithColumns(30))); 11425 } 11426 11427 TEST_F(FormatTest, SplitsUTF8LineComments) { 11428 EXPECT_EQ("// aaaaÄ\xc2\x8d", 11429 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 11430 EXPECT_EQ("// Я из лесу\n" 11431 "// вышел; был\n" 11432 "// сильный\n" 11433 "// мороз.", 11434 format("// Я из лесу вышел; был сильный мороз.", 11435 getLLVMStyleWithColumns(13))); 11436 EXPECT_EQ("// 一二三\n" 11437 "// 四五六七\n" 11438 "// 八 九\n" 11439 "// 十", 11440 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 11441 } 11442 11443 TEST_F(FormatTest, SplitsUTF8BlockComments) { 11444 EXPECT_EQ("/* Гляжу,\n" 11445 " * поднимается\n" 11446 " * медленно в\n" 11447 " * гору\n" 11448 " * Лошадка,\n" 11449 " * везущая\n" 11450 " * хворосту\n" 11451 " * воз. */", 11452 format("/* Гляжу, поднимается медленно в гору\n" 11453 " * Лошадка, везущая хворосту воз. */", 11454 getLLVMStyleWithColumns(13))); 11455 EXPECT_EQ( 11456 "/* 一二三\n" 11457 " * 四五六七\n" 11458 " * 八 九\n" 11459 " * 十 */", 11460 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 11461 EXPECT_EQ("/* \n" 11462 " * \n" 11463 " * - */", 11464 format("/* - */", getLLVMStyleWithColumns(12))); 11465 } 11466 11467 #endif // _MSC_VER 11468 11469 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 11470 FormatStyle Style = getLLVMStyle(); 11471 11472 Style.ConstructorInitializerIndentWidth = 4; 11473 verifyFormat( 11474 "SomeClass::Constructor()\n" 11475 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 11476 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 11477 Style); 11478 11479 Style.ConstructorInitializerIndentWidth = 2; 11480 verifyFormat( 11481 "SomeClass::Constructor()\n" 11482 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 11483 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 11484 Style); 11485 11486 Style.ConstructorInitializerIndentWidth = 0; 11487 verifyFormat( 11488 "SomeClass::Constructor()\n" 11489 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 11490 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 11491 Style); 11492 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11493 verifyFormat( 11494 "SomeLongTemplateVariableName<\n" 11495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 11496 Style); 11497 verifyFormat( 11498 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 11499 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 11500 Style); 11501 } 11502 11503 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 11504 FormatStyle Style = getLLVMStyle(); 11505 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 11506 Style.ConstructorInitializerIndentWidth = 4; 11507 verifyFormat("SomeClass::Constructor()\n" 11508 " : a(a)\n" 11509 " , b(b)\n" 11510 " , c(c) {}", 11511 Style); 11512 verifyFormat("SomeClass::Constructor()\n" 11513 " : a(a) {}", 11514 Style); 11515 11516 Style.ColumnLimit = 0; 11517 verifyFormat("SomeClass::Constructor()\n" 11518 " : a(a) {}", 11519 Style); 11520 verifyFormat("SomeClass::Constructor() noexcept\n" 11521 " : a(a) {}", 11522 Style); 11523 verifyFormat("SomeClass::Constructor()\n" 11524 " : a(a)\n" 11525 " , b(b)\n" 11526 " , c(c) {}", 11527 Style); 11528 verifyFormat("SomeClass::Constructor()\n" 11529 " : a(a) {\n" 11530 " foo();\n" 11531 " bar();\n" 11532 "}", 11533 Style); 11534 11535 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11536 verifyFormat("SomeClass::Constructor()\n" 11537 " : a(a)\n" 11538 " , b(b)\n" 11539 " , c(c) {\n}", 11540 Style); 11541 verifyFormat("SomeClass::Constructor()\n" 11542 " : a(a) {\n}", 11543 Style); 11544 11545 Style.ColumnLimit = 80; 11546 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11547 Style.ConstructorInitializerIndentWidth = 2; 11548 verifyFormat("SomeClass::Constructor()\n" 11549 " : a(a)\n" 11550 " , b(b)\n" 11551 " , c(c) {}", 11552 Style); 11553 11554 Style.ConstructorInitializerIndentWidth = 0; 11555 verifyFormat("SomeClass::Constructor()\n" 11556 ": a(a)\n" 11557 ", b(b)\n" 11558 ", c(c) {}", 11559 Style); 11560 11561 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 11562 Style.ConstructorInitializerIndentWidth = 4; 11563 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 11564 verifyFormat( 11565 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 11566 Style); 11567 verifyFormat( 11568 "SomeClass::Constructor()\n" 11569 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 11570 Style); 11571 Style.ConstructorInitializerIndentWidth = 4; 11572 Style.ColumnLimit = 60; 11573 verifyFormat("SomeClass::Constructor()\n" 11574 " : aaaaaaaa(aaaaaaaa)\n" 11575 " , aaaaaaaa(aaaaaaaa)\n" 11576 " , aaaaaaaa(aaaaaaaa) {}", 11577 Style); 11578 } 11579 11580 TEST_F(FormatTest, Destructors) { 11581 verifyFormat("void F(int &i) { i.~int(); }"); 11582 verifyFormat("void F(int &i) { i->~int(); }"); 11583 } 11584 11585 TEST_F(FormatTest, FormatsWithWebKitStyle) { 11586 FormatStyle Style = getWebKitStyle(); 11587 11588 // Don't indent in outer namespaces. 11589 verifyFormat("namespace outer {\n" 11590 "int i;\n" 11591 "namespace inner {\n" 11592 " int i;\n" 11593 "} // namespace inner\n" 11594 "} // namespace outer\n" 11595 "namespace other_outer {\n" 11596 "int i;\n" 11597 "}", 11598 Style); 11599 11600 // Don't indent case labels. 11601 verifyFormat("switch (variable) {\n" 11602 "case 1:\n" 11603 "case 2:\n" 11604 " doSomething();\n" 11605 " break;\n" 11606 "default:\n" 11607 " ++variable;\n" 11608 "}", 11609 Style); 11610 11611 // Wrap before binary operators. 11612 EXPECT_EQ("void f()\n" 11613 "{\n" 11614 " if (aaaaaaaaaaaaaaaa\n" 11615 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 11616 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 11617 " return;\n" 11618 "}", 11619 format("void f() {\n" 11620 "if (aaaaaaaaaaaaaaaa\n" 11621 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 11622 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 11623 "return;\n" 11624 "}", 11625 Style)); 11626 11627 // Allow functions on a single line. 11628 verifyFormat("void f() { return; }", Style); 11629 11630 // Constructor initializers are formatted one per line with the "," on the 11631 // new line. 11632 verifyFormat("Constructor()\n" 11633 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 11634 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 11635 " aaaaaaaaaaaaaa)\n" 11636 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 11637 "{\n" 11638 "}", 11639 Style); 11640 verifyFormat("SomeClass::Constructor()\n" 11641 " : a(a)\n" 11642 "{\n" 11643 "}", 11644 Style); 11645 EXPECT_EQ("SomeClass::Constructor()\n" 11646 " : a(a)\n" 11647 "{\n" 11648 "}", 11649 format("SomeClass::Constructor():a(a){}", Style)); 11650 verifyFormat("SomeClass::Constructor()\n" 11651 " : a(a)\n" 11652 " , b(b)\n" 11653 " , c(c)\n" 11654 "{\n" 11655 "}", 11656 Style); 11657 verifyFormat("SomeClass::Constructor()\n" 11658 " : a(a)\n" 11659 "{\n" 11660 " foo();\n" 11661 " bar();\n" 11662 "}", 11663 Style); 11664 11665 // Access specifiers should be aligned left. 11666 verifyFormat("class C {\n" 11667 "public:\n" 11668 " int i;\n" 11669 "};", 11670 Style); 11671 11672 // Do not align comments. 11673 verifyFormat("int a; // Do not\n" 11674 "double b; // align comments.", 11675 Style); 11676 11677 // Do not align operands. 11678 EXPECT_EQ("ASSERT(aaaa\n" 11679 " || bbbb);", 11680 format("ASSERT ( aaaa\n||bbbb);", Style)); 11681 11682 // Accept input's line breaks. 11683 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 11684 " || bbbbbbbbbbbbbbb) {\n" 11685 " i++;\n" 11686 "}", 11687 format("if (aaaaaaaaaaaaaaa\n" 11688 "|| bbbbbbbbbbbbbbb) { i++; }", 11689 Style)); 11690 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 11691 " i++;\n" 11692 "}", 11693 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 11694 11695 // Don't automatically break all macro definitions (llvm.org/PR17842). 11696 verifyFormat("#define aNumber 10", Style); 11697 // However, generally keep the line breaks that the user authored. 11698 EXPECT_EQ("#define aNumber \\\n" 11699 " 10", 11700 format("#define aNumber \\\n" 11701 " 10", 11702 Style)); 11703 11704 // Keep empty and one-element array literals on a single line. 11705 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 11706 " copyItems:YES];", 11707 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 11708 "copyItems:YES];", 11709 Style)); 11710 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 11711 " copyItems:YES];", 11712 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 11713 " copyItems:YES];", 11714 Style)); 11715 // FIXME: This does not seem right, there should be more indentation before 11716 // the array literal's entries. Nested blocks have the same problem. 11717 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11718 " @\"a\",\n" 11719 " @\"a\"\n" 11720 "]\n" 11721 " copyItems:YES];", 11722 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11723 " @\"a\",\n" 11724 " @\"a\"\n" 11725 " ]\n" 11726 " copyItems:YES];", 11727 Style)); 11728 EXPECT_EQ( 11729 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11730 " copyItems:YES];", 11731 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11732 " copyItems:YES];", 11733 Style)); 11734 11735 verifyFormat("[self.a b:c c:d];", Style); 11736 EXPECT_EQ("[self.a b:c\n" 11737 " c:d];", 11738 format("[self.a b:c\n" 11739 "c:d];", 11740 Style)); 11741 } 11742 11743 TEST_F(FormatTest, FormatsLambdas) { 11744 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 11745 verifyFormat( 11746 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 11747 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 11748 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 11749 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 11750 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 11751 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 11752 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 11753 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 11754 verifyFormat("int x = f(*+[] {});"); 11755 verifyFormat("void f() {\n" 11756 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 11757 "}\n"); 11758 verifyFormat("void f() {\n" 11759 " other(x.begin(), //\n" 11760 " x.end(), //\n" 11761 " [&](int, int) { return 1; });\n" 11762 "}\n"); 11763 verifyFormat("void f() {\n" 11764 " other.other.other.other.other(\n" 11765 " x.begin(), x.end(),\n" 11766 " [something, rather](int, int, int, int, int, int, int) { return 1; });\n" 11767 "}\n"); 11768 verifyFormat("void f() {\n" 11769 " other.other.other.other.other(\n" 11770 " x.begin(), x.end(),\n" 11771 " [something, rather](int, int, int, int, int, int, int) {\n" 11772 " //\n" 11773 " });\n" 11774 "}\n"); 11775 verifyFormat("SomeFunction([]() { // A cool function...\n" 11776 " return 43;\n" 11777 "});"); 11778 EXPECT_EQ("SomeFunction([]() {\n" 11779 "#define A a\n" 11780 " return 43;\n" 11781 "});", 11782 format("SomeFunction([](){\n" 11783 "#define A a\n" 11784 "return 43;\n" 11785 "});")); 11786 verifyFormat("void f() {\n" 11787 " SomeFunction([](decltype(x), A *a) {});\n" 11788 "}"); 11789 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11790 " [](const aaaaaaaaaa &a) { return a; });"); 11791 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 11792 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 11793 "});"); 11794 verifyFormat("Constructor()\n" 11795 " : Field([] { // comment\n" 11796 " int i;\n" 11797 " }) {}"); 11798 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 11799 " return some_parameter.size();\n" 11800 "};"); 11801 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 11802 " [](const string &s) { return s; };"); 11803 verifyFormat("int i = aaaaaa ? 1 //\n" 11804 " : [] {\n" 11805 " return 2; //\n" 11806 " }();"); 11807 verifyFormat("llvm::errs() << \"number of twos is \"\n" 11808 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 11809 " return x == 2; // force break\n" 11810 " });"); 11811 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11812 " [=](int iiiiiiiiiiii) {\n" 11813 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 11814 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 11815 " });", 11816 getLLVMStyleWithColumns(60)); 11817 verifyFormat("SomeFunction({[&] {\n" 11818 " // comment\n" 11819 " },\n" 11820 " [&] {\n" 11821 " // comment\n" 11822 " }});"); 11823 verifyFormat("SomeFunction({[&] {\n" 11824 " // comment\n" 11825 "}});"); 11826 verifyFormat("virtual aaaaaaaaaaaaaaaa(\n" 11827 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 11828 " aaaaa aaaaaaaaa);"); 11829 11830 // Lambdas with return types. 11831 verifyFormat("int c = []() -> int { return 2; }();\n"); 11832 verifyFormat("int c = []() -> int * { return 2; }();\n"); 11833 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 11834 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 11835 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 11836 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 11837 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 11838 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 11839 verifyFormat("[a, a]() -> a<1> {};"); 11840 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 11841 " int j) -> int {\n" 11842 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 11843 "};"); 11844 verifyFormat( 11845 "aaaaaaaaaaaaaaaaaaaaaa(\n" 11846 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 11847 " return aaaaaaaaaaaaaaaaa;\n" 11848 " });", 11849 getLLVMStyleWithColumns(70)); 11850 verifyFormat("[]() //\n" 11851 " -> int {\n" 11852 " return 1; //\n" 11853 "};"); 11854 11855 // Multiple lambdas in the same parentheses change indentation rules. These 11856 // lambdas are forced to start on new lines. 11857 verifyFormat("SomeFunction(\n" 11858 " []() {\n" 11859 " //\n" 11860 " },\n" 11861 " []() {\n" 11862 " //\n" 11863 " });"); 11864 11865 // A lambda passed as arg0 is always pushed to the next line. 11866 verifyFormat("SomeFunction(\n" 11867 " [this] {\n" 11868 " //\n" 11869 " },\n" 11870 " 1);\n"); 11871 11872 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like the arg0 11873 // case above. 11874 auto Style = getGoogleStyle(); 11875 Style.BinPackArguments = false; 11876 verifyFormat("SomeFunction(\n" 11877 " a,\n" 11878 " [this] {\n" 11879 " //\n" 11880 " },\n" 11881 " b);\n", 11882 Style); 11883 verifyFormat("SomeFunction(\n" 11884 " a,\n" 11885 " [this] {\n" 11886 " //\n" 11887 " },\n" 11888 " b);\n"); 11889 11890 // A lambda with a very long line forces arg0 to be pushed out irrespective of 11891 // the BinPackArguments value (as long as the code is wide enough). 11892 verifyFormat("something->SomeFunction(\n" 11893 " a,\n" 11894 " [this] {\n" 11895 " D0000000000000000000000000000000000000000000000000000000000001();\n" 11896 " },\n" 11897 " b);\n"); 11898 11899 // A multi-line lambda is pulled up as long as the introducer fits on the previous 11900 // line and there are no further args. 11901 verifyFormat("function(1, [this, that] {\n" 11902 " //\n" 11903 "});\n"); 11904 verifyFormat("function([this, that] {\n" 11905 " //\n" 11906 "});\n"); 11907 // FIXME: this format is not ideal and we should consider forcing the first arg 11908 // onto its own line. 11909 verifyFormat("function(a, b, c, //\n" 11910 " d, [this, that] {\n" 11911 " //\n" 11912 " });\n"); 11913 11914 // Multiple lambdas are treated correctly even when there is a short arg0. 11915 verifyFormat("SomeFunction(\n" 11916 " 1,\n" 11917 " [this] {\n" 11918 " //\n" 11919 " },\n" 11920 " [this] {\n" 11921 " //\n" 11922 " },\n" 11923 " 1);\n"); 11924 11925 // More complex introducers. 11926 verifyFormat("return [i, args...] {};"); 11927 11928 // Not lambdas. 11929 verifyFormat("constexpr char hello[]{\"hello\"};"); 11930 verifyFormat("double &operator[](int i) { return 0; }\n" 11931 "int i;"); 11932 verifyFormat("std::unique_ptr<int[]> foo() {}"); 11933 verifyFormat("int i = a[a][a]->f();"); 11934 verifyFormat("int i = (*b)[a]->f();"); 11935 11936 // Other corner cases. 11937 verifyFormat("void f() {\n" 11938 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 11939 " );\n" 11940 "}"); 11941 11942 // Lambdas created through weird macros. 11943 verifyFormat("void f() {\n" 11944 " MACRO((const AA &a) { return 1; });\n" 11945 " MACRO((AA &a) { return 1; });\n" 11946 "}"); 11947 11948 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 11949 " doo_dah();\n" 11950 " doo_dah();\n" 11951 " })) {\n" 11952 "}"); 11953 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 11954 " doo_dah();\n" 11955 " doo_dah();\n" 11956 " })) {\n" 11957 "}"); 11958 verifyFormat("auto lambda = []() {\n" 11959 " int a = 2\n" 11960 "#if A\n" 11961 " + 2\n" 11962 "#endif\n" 11963 " ;\n" 11964 "};"); 11965 11966 // Lambdas with complex multiline introducers. 11967 verifyFormat( 11968 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11969 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 11970 " -> ::std::unordered_set<\n" 11971 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 11972 " //\n" 11973 " });"); 11974 } 11975 11976 TEST_F(FormatTest, EmptyLinesInLambdas) { 11977 verifyFormat("auto lambda = []() {\n" 11978 " x(); //\n" 11979 "};", 11980 "auto lambda = []() {\n" 11981 "\n" 11982 " x(); //\n" 11983 "\n" 11984 "};"); 11985 } 11986 11987 TEST_F(FormatTest, FormatsBlocks) { 11988 FormatStyle ShortBlocks = getLLVMStyle(); 11989 ShortBlocks.AllowShortBlocksOnASingleLine = true; 11990 verifyFormat("int (^Block)(int, int);", ShortBlocks); 11991 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 11992 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 11993 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 11994 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 11995 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 11996 11997 verifyFormat("foo(^{ bar(); });", ShortBlocks); 11998 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 11999 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 12000 12001 verifyFormat("[operation setCompletionBlock:^{\n" 12002 " [self onOperationDone];\n" 12003 "}];"); 12004 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 12005 " [self onOperationDone];\n" 12006 "}]};"); 12007 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 12008 " f();\n" 12009 "}];"); 12010 verifyFormat("int a = [operation block:^int(int *i) {\n" 12011 " return 1;\n" 12012 "}];"); 12013 verifyFormat("[myObject doSomethingWith:arg1\n" 12014 " aaa:^int(int *a) {\n" 12015 " return 1;\n" 12016 " }\n" 12017 " bbb:f(a * bbbbbbbb)];"); 12018 12019 verifyFormat("[operation setCompletionBlock:^{\n" 12020 " [self.delegate newDataAvailable];\n" 12021 "}];", 12022 getLLVMStyleWithColumns(60)); 12023 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 12024 " NSString *path = [self sessionFilePath];\n" 12025 " if (path) {\n" 12026 " // ...\n" 12027 " }\n" 12028 "});"); 12029 verifyFormat("[[SessionService sharedService]\n" 12030 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12031 " if (window) {\n" 12032 " [self windowDidLoad:window];\n" 12033 " } else {\n" 12034 " [self errorLoadingWindow];\n" 12035 " }\n" 12036 " }];"); 12037 verifyFormat("void (^largeBlock)(void) = ^{\n" 12038 " // ...\n" 12039 "};\n", 12040 getLLVMStyleWithColumns(40)); 12041 verifyFormat("[[SessionService sharedService]\n" 12042 " loadWindowWithCompletionBlock: //\n" 12043 " ^(SessionWindow *window) {\n" 12044 " if (window) {\n" 12045 " [self windowDidLoad:window];\n" 12046 " } else {\n" 12047 " [self errorLoadingWindow];\n" 12048 " }\n" 12049 " }];", 12050 getLLVMStyleWithColumns(60)); 12051 verifyFormat("[myObject doSomethingWith:arg1\n" 12052 " firstBlock:^(Foo *a) {\n" 12053 " // ...\n" 12054 " int i;\n" 12055 " }\n" 12056 " secondBlock:^(Bar *b) {\n" 12057 " // ...\n" 12058 " int i;\n" 12059 " }\n" 12060 " thirdBlock:^Foo(Bar *b) {\n" 12061 " // ...\n" 12062 " int i;\n" 12063 " }];"); 12064 verifyFormat("[myObject doSomethingWith:arg1\n" 12065 " firstBlock:-1\n" 12066 " secondBlock:^(Bar *b) {\n" 12067 " // ...\n" 12068 " int i;\n" 12069 " }];"); 12070 12071 verifyFormat("f(^{\n" 12072 " @autoreleasepool {\n" 12073 " if (a) {\n" 12074 " g();\n" 12075 " }\n" 12076 " }\n" 12077 "});"); 12078 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 12079 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 12080 "};"); 12081 12082 FormatStyle FourIndent = getLLVMStyle(); 12083 FourIndent.ObjCBlockIndentWidth = 4; 12084 verifyFormat("[operation setCompletionBlock:^{\n" 12085 " [self onOperationDone];\n" 12086 "}];", 12087 FourIndent); 12088 } 12089 12090 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 12091 FormatStyle ZeroColumn = getLLVMStyle(); 12092 ZeroColumn.ColumnLimit = 0; 12093 12094 verifyFormat("[[SessionService sharedService] " 12095 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12096 " if (window) {\n" 12097 " [self windowDidLoad:window];\n" 12098 " } else {\n" 12099 " [self errorLoadingWindow];\n" 12100 " }\n" 12101 "}];", 12102 ZeroColumn); 12103 EXPECT_EQ("[[SessionService sharedService]\n" 12104 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12105 " if (window) {\n" 12106 " [self windowDidLoad:window];\n" 12107 " } else {\n" 12108 " [self errorLoadingWindow];\n" 12109 " }\n" 12110 " }];", 12111 format("[[SessionService sharedService]\n" 12112 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 12113 " if (window) {\n" 12114 " [self windowDidLoad:window];\n" 12115 " } else {\n" 12116 " [self errorLoadingWindow];\n" 12117 " }\n" 12118 "}];", 12119 ZeroColumn)); 12120 verifyFormat("[myObject doSomethingWith:arg1\n" 12121 " firstBlock:^(Foo *a) {\n" 12122 " // ...\n" 12123 " int i;\n" 12124 " }\n" 12125 " secondBlock:^(Bar *b) {\n" 12126 " // ...\n" 12127 " int i;\n" 12128 " }\n" 12129 " thirdBlock:^Foo(Bar *b) {\n" 12130 " // ...\n" 12131 " int i;\n" 12132 " }];", 12133 ZeroColumn); 12134 verifyFormat("f(^{\n" 12135 " @autoreleasepool {\n" 12136 " if (a) {\n" 12137 " g();\n" 12138 " }\n" 12139 " }\n" 12140 "});", 12141 ZeroColumn); 12142 verifyFormat("void (^largeBlock)(void) = ^{\n" 12143 " // ...\n" 12144 "};", 12145 ZeroColumn); 12146 12147 ZeroColumn.AllowShortBlocksOnASingleLine = true; 12148 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 12149 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 12150 ZeroColumn.AllowShortBlocksOnASingleLine = false; 12151 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 12152 " int i;\n" 12153 "};", 12154 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 12155 } 12156 12157 TEST_F(FormatTest, SupportsCRLF) { 12158 EXPECT_EQ("int a;\r\n" 12159 "int b;\r\n" 12160 "int c;\r\n", 12161 format("int a;\r\n" 12162 " int b;\r\n" 12163 " int c;\r\n", 12164 getLLVMStyle())); 12165 EXPECT_EQ("int a;\r\n" 12166 "int b;\r\n" 12167 "int c;\r\n", 12168 format("int a;\r\n" 12169 " int b;\n" 12170 " int c;\r\n", 12171 getLLVMStyle())); 12172 EXPECT_EQ("int a;\n" 12173 "int b;\n" 12174 "int c;\n", 12175 format("int a;\r\n" 12176 " int b;\n" 12177 " int c;\n", 12178 getLLVMStyle())); 12179 EXPECT_EQ("\"aaaaaaa \"\r\n" 12180 "\"bbbbbbb\";\r\n", 12181 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 12182 EXPECT_EQ("#define A \\\r\n" 12183 " b; \\\r\n" 12184 " c; \\\r\n" 12185 " d;\r\n", 12186 format("#define A \\\r\n" 12187 " b; \\\r\n" 12188 " c; d; \r\n", 12189 getGoogleStyle())); 12190 12191 EXPECT_EQ("/*\r\n" 12192 "multi line block comments\r\n" 12193 "should not introduce\r\n" 12194 "an extra carriage return\r\n" 12195 "*/\r\n", 12196 format("/*\r\n" 12197 "multi line block comments\r\n" 12198 "should not introduce\r\n" 12199 "an extra carriage return\r\n" 12200 "*/\r\n")); 12201 } 12202 12203 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 12204 verifyFormat("MY_CLASS(C) {\n" 12205 " int i;\n" 12206 " int j;\n" 12207 "};"); 12208 } 12209 12210 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 12211 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 12212 TwoIndent.ContinuationIndentWidth = 2; 12213 12214 EXPECT_EQ("int i =\n" 12215 " longFunction(\n" 12216 " arg);", 12217 format("int i = longFunction(arg);", TwoIndent)); 12218 12219 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 12220 SixIndent.ContinuationIndentWidth = 6; 12221 12222 EXPECT_EQ("int i =\n" 12223 " longFunction(\n" 12224 " arg);", 12225 format("int i = longFunction(arg);", SixIndent)); 12226 } 12227 12228 TEST_F(FormatTest, SpacesInAngles) { 12229 FormatStyle Spaces = getLLVMStyle(); 12230 Spaces.SpacesInAngles = true; 12231 12232 verifyFormat("static_cast< int >(arg);", Spaces); 12233 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 12234 verifyFormat("f< int, float >();", Spaces); 12235 verifyFormat("template <> g() {}", Spaces); 12236 verifyFormat("template < std::vector< int > > f() {}", Spaces); 12237 verifyFormat("std::function< void(int, int) > fct;", Spaces); 12238 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 12239 Spaces); 12240 12241 Spaces.Standard = FormatStyle::LS_Cpp03; 12242 Spaces.SpacesInAngles = true; 12243 verifyFormat("A< A< int > >();", Spaces); 12244 12245 Spaces.SpacesInAngles = false; 12246 verifyFormat("A<A<int> >();", Spaces); 12247 12248 Spaces.Standard = FormatStyle::LS_Cpp11; 12249 Spaces.SpacesInAngles = true; 12250 verifyFormat("A< A< int > >();", Spaces); 12251 12252 Spaces.SpacesInAngles = false; 12253 verifyFormat("A<A<int>>();", Spaces); 12254 } 12255 12256 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 12257 FormatStyle Style = getLLVMStyle(); 12258 Style.SpaceAfterTemplateKeyword = false; 12259 verifyFormat("template<int> void foo();", Style); 12260 } 12261 12262 TEST_F(FormatTest, TripleAngleBrackets) { 12263 verifyFormat("f<<<1, 1>>>();"); 12264 verifyFormat("f<<<1, 1, 1, s>>>();"); 12265 verifyFormat("f<<<a, b, c, d>>>();"); 12266 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 12267 verifyFormat("f<param><<<1, 1>>>();"); 12268 verifyFormat("f<1><<<1, 1>>>();"); 12269 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 12270 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12271 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 12272 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 12273 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 12274 } 12275 12276 TEST_F(FormatTest, MergeLessLessAtEnd) { 12277 verifyFormat("<<"); 12278 EXPECT_EQ("< < <", format("\\\n<<<")); 12279 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12280 "aaallvm::outs() <<"); 12281 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12282 "aaaallvm::outs()\n <<"); 12283 } 12284 12285 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 12286 std::string code = "#if A\n" 12287 "#if B\n" 12288 "a.\n" 12289 "#endif\n" 12290 " a = 1;\n" 12291 "#else\n" 12292 "#endif\n" 12293 "#if C\n" 12294 "#else\n" 12295 "#endif\n"; 12296 EXPECT_EQ(code, format(code)); 12297 } 12298 12299 TEST_F(FormatTest, HandleConflictMarkers) { 12300 // Git/SVN conflict markers. 12301 EXPECT_EQ("int a;\n" 12302 "void f() {\n" 12303 " callme(some(parameter1,\n" 12304 "<<<<<<< text by the vcs\n" 12305 " parameter2),\n" 12306 "||||||| text by the vcs\n" 12307 " parameter2),\n" 12308 " parameter3,\n" 12309 "======= text by the vcs\n" 12310 " parameter2, parameter3),\n" 12311 ">>>>>>> text by the vcs\n" 12312 " otherparameter);\n", 12313 format("int a;\n" 12314 "void f() {\n" 12315 " callme(some(parameter1,\n" 12316 "<<<<<<< text by the vcs\n" 12317 " parameter2),\n" 12318 "||||||| text by the vcs\n" 12319 " parameter2),\n" 12320 " parameter3,\n" 12321 "======= text by the vcs\n" 12322 " parameter2,\n" 12323 " parameter3),\n" 12324 ">>>>>>> text by the vcs\n" 12325 " otherparameter);\n")); 12326 12327 // Perforce markers. 12328 EXPECT_EQ("void f() {\n" 12329 " function(\n" 12330 ">>>> text by the vcs\n" 12331 " parameter,\n" 12332 "==== text by the vcs\n" 12333 " parameter,\n" 12334 "==== text by the vcs\n" 12335 " parameter,\n" 12336 "<<<< text by the vcs\n" 12337 " parameter);\n", 12338 format("void f() {\n" 12339 " function(\n" 12340 ">>>> text by the vcs\n" 12341 " parameter,\n" 12342 "==== text by the vcs\n" 12343 " parameter,\n" 12344 "==== text by the vcs\n" 12345 " parameter,\n" 12346 "<<<< text by the vcs\n" 12347 " parameter);\n")); 12348 12349 EXPECT_EQ("<<<<<<<\n" 12350 "|||||||\n" 12351 "=======\n" 12352 ">>>>>>>", 12353 format("<<<<<<<\n" 12354 "|||||||\n" 12355 "=======\n" 12356 ">>>>>>>")); 12357 12358 EXPECT_EQ("<<<<<<<\n" 12359 "|||||||\n" 12360 "int i;\n" 12361 "=======\n" 12362 ">>>>>>>", 12363 format("<<<<<<<\n" 12364 "|||||||\n" 12365 "int i;\n" 12366 "=======\n" 12367 ">>>>>>>")); 12368 12369 // FIXME: Handle parsing of macros around conflict markers correctly: 12370 EXPECT_EQ("#define Macro \\\n" 12371 "<<<<<<<\n" 12372 "Something \\\n" 12373 "|||||||\n" 12374 "Else \\\n" 12375 "=======\n" 12376 "Other \\\n" 12377 ">>>>>>>\n" 12378 " End int i;\n", 12379 format("#define Macro \\\n" 12380 "<<<<<<<\n" 12381 " Something \\\n" 12382 "|||||||\n" 12383 " Else \\\n" 12384 "=======\n" 12385 " Other \\\n" 12386 ">>>>>>>\n" 12387 " End\n" 12388 "int i;\n")); 12389 } 12390 12391 TEST_F(FormatTest, DisableRegions) { 12392 EXPECT_EQ("int i;\n" 12393 "// clang-format off\n" 12394 " int j;\n" 12395 "// clang-format on\n" 12396 "int k;", 12397 format(" int i;\n" 12398 " // clang-format off\n" 12399 " int j;\n" 12400 " // clang-format on\n" 12401 " int k;")); 12402 EXPECT_EQ("int i;\n" 12403 "/* clang-format off */\n" 12404 " int j;\n" 12405 "/* clang-format on */\n" 12406 "int k;", 12407 format(" int i;\n" 12408 " /* clang-format off */\n" 12409 " int j;\n" 12410 " /* clang-format on */\n" 12411 " int k;")); 12412 12413 // Don't reflow comments within disabled regions. 12414 EXPECT_EQ( 12415 "// clang-format off\n" 12416 "// long long long long long long line\n" 12417 "/* clang-format on */\n" 12418 "/* long long long\n" 12419 " * long long long\n" 12420 " * line */\n" 12421 "int i;\n" 12422 "/* clang-format off */\n" 12423 "/* long long long long long long line */\n", 12424 format("// clang-format off\n" 12425 "// long long long long long long line\n" 12426 "/* clang-format on */\n" 12427 "/* long long long long long long line */\n" 12428 "int i;\n" 12429 "/* clang-format off */\n" 12430 "/* long long long long long long line */\n", 12431 getLLVMStyleWithColumns(20))); 12432 } 12433 12434 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 12435 format("? ) ="); 12436 verifyNoCrash("#define a\\\n /**/}"); 12437 } 12438 12439 TEST_F(FormatTest, FormatsTableGenCode) { 12440 FormatStyle Style = getLLVMStyle(); 12441 Style.Language = FormatStyle::LK_TableGen; 12442 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 12443 } 12444 12445 TEST_F(FormatTest, ArrayOfTemplates) { 12446 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 12447 format("auto a = new unique_ptr<int > [ 10];")); 12448 12449 FormatStyle Spaces = getLLVMStyle(); 12450 Spaces.SpacesInSquareBrackets = true; 12451 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 12452 format("auto a = new unique_ptr<int > [10];", Spaces)); 12453 } 12454 12455 TEST_F(FormatTest, ArrayAsTemplateType) { 12456 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 12457 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 12458 12459 FormatStyle Spaces = getLLVMStyle(); 12460 Spaces.SpacesInSquareBrackets = true; 12461 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 12462 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 12463 } 12464 12465 TEST_F(FormatTest, NoSpaceAfterSuper) { 12466 verifyFormat("__super::FooBar();"); 12467 } 12468 12469 TEST(FormatStyle, GetStyleWithEmptyFileName) { 12470 llvm::vfs::InMemoryFileSystem FS; 12471 auto Style1 = getStyle("file", "", "Google", "", &FS); 12472 ASSERT_TRUE((bool)Style1); 12473 ASSERT_EQ(*Style1, getGoogleStyle()); 12474 } 12475 12476 TEST(FormatStyle, GetStyleOfFile) { 12477 llvm::vfs::InMemoryFileSystem FS; 12478 // Test 1: format file in the same directory. 12479 ASSERT_TRUE( 12480 FS.addFile("/a/.clang-format", 0, 12481 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 12482 ASSERT_TRUE( 12483 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 12484 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 12485 ASSERT_TRUE((bool)Style1); 12486 ASSERT_EQ(*Style1, getLLVMStyle()); 12487 12488 // Test 2.1: fallback to default. 12489 ASSERT_TRUE( 12490 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 12491 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 12492 ASSERT_TRUE((bool)Style2); 12493 ASSERT_EQ(*Style2, getMozillaStyle()); 12494 12495 // Test 2.2: no format on 'none' fallback style. 12496 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 12497 ASSERT_TRUE((bool)Style2); 12498 ASSERT_EQ(*Style2, getNoStyle()); 12499 12500 // Test 2.3: format if config is found with no based style while fallback is 12501 // 'none'. 12502 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 12503 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 12504 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 12505 ASSERT_TRUE((bool)Style2); 12506 ASSERT_EQ(*Style2, getLLVMStyle()); 12507 12508 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 12509 Style2 = getStyle("{}", "a.h", "none", "", &FS); 12510 ASSERT_TRUE((bool)Style2); 12511 ASSERT_EQ(*Style2, getLLVMStyle()); 12512 12513 // Test 3: format file in parent directory. 12514 ASSERT_TRUE( 12515 FS.addFile("/c/.clang-format", 0, 12516 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 12517 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 12518 llvm::MemoryBuffer::getMemBuffer("int i;"))); 12519 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 12520 ASSERT_TRUE((bool)Style3); 12521 ASSERT_EQ(*Style3, getGoogleStyle()); 12522 12523 // Test 4: error on invalid fallback style 12524 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 12525 ASSERT_FALSE((bool)Style4); 12526 llvm::consumeError(Style4.takeError()); 12527 12528 // Test 5: error on invalid yaml on command line 12529 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 12530 ASSERT_FALSE((bool)Style5); 12531 llvm::consumeError(Style5.takeError()); 12532 12533 // Test 6: error on invalid style 12534 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 12535 ASSERT_FALSE((bool)Style6); 12536 llvm::consumeError(Style6.takeError()); 12537 12538 // Test 7: found config file, error on parsing it 12539 ASSERT_TRUE( 12540 FS.addFile("/d/.clang-format", 0, 12541 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 12542 "InvalidKey: InvalidValue"))); 12543 ASSERT_TRUE( 12544 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 12545 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 12546 ASSERT_FALSE((bool)Style7); 12547 llvm::consumeError(Style7.takeError()); 12548 } 12549 12550 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 12551 // Column limit is 20. 12552 std::string Code = "Type *a =\n" 12553 " new Type();\n" 12554 "g(iiiii, 0, jjjjj,\n" 12555 " 0, kkkkk, 0, mm);\n" 12556 "int bad = format ;"; 12557 std::string Expected = "auto a = new Type();\n" 12558 "g(iiiii, nullptr,\n" 12559 " jjjjj, nullptr,\n" 12560 " kkkkk, nullptr,\n" 12561 " mm);\n" 12562 "int bad = format ;"; 12563 FileID ID = Context.createInMemoryFile("format.cpp", Code); 12564 tooling::Replacements Replaces = toReplacements( 12565 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 12566 "auto "), 12567 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 12568 "nullptr"), 12569 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 12570 "nullptr"), 12571 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 12572 "nullptr")}); 12573 12574 format::FormatStyle Style = format::getLLVMStyle(); 12575 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 12576 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 12577 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 12578 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 12579 auto Result = applyAllReplacements(Code, *FormattedReplaces); 12580 EXPECT_TRUE(static_cast<bool>(Result)); 12581 EXPECT_EQ(Expected, *Result); 12582 } 12583 12584 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 12585 std::string Code = "#include \"a.h\"\n" 12586 "#include \"c.h\"\n" 12587 "\n" 12588 "int main() {\n" 12589 " return 0;\n" 12590 "}"; 12591 std::string Expected = "#include \"a.h\"\n" 12592 "#include \"b.h\"\n" 12593 "#include \"c.h\"\n" 12594 "\n" 12595 "int main() {\n" 12596 " return 0;\n" 12597 "}"; 12598 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 12599 tooling::Replacements Replaces = toReplacements( 12600 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 12601 "#include \"b.h\"\n")}); 12602 12603 format::FormatStyle Style = format::getLLVMStyle(); 12604 Style.SortIncludes = true; 12605 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 12606 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 12607 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 12608 auto Result = applyAllReplacements(Code, *FormattedReplaces); 12609 EXPECT_TRUE(static_cast<bool>(Result)); 12610 EXPECT_EQ(Expected, *Result); 12611 } 12612 12613 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 12614 EXPECT_EQ("using std::cin;\n" 12615 "using std::cout;", 12616 format("using std::cout;\n" 12617 "using std::cin;", getGoogleStyle())); 12618 } 12619 12620 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 12621 format::FormatStyle Style = format::getLLVMStyle(); 12622 Style.Standard = FormatStyle::LS_Cpp03; 12623 // cpp03 recognize this string as identifier u8 and literal character 'a' 12624 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 12625 } 12626 12627 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 12628 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 12629 // all modes, including C++11, C++14 and C++17 12630 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 12631 } 12632 12633 TEST_F(FormatTest, DoNotFormatLikelyXml) { 12634 EXPECT_EQ("<!-- ;> -->", 12635 format("<!-- ;> -->", getGoogleStyle())); 12636 EXPECT_EQ(" <!-- >; -->", 12637 format(" <!-- >; -->", getGoogleStyle())); 12638 } 12639 12640 TEST_F(FormatTest, StructuredBindings) { 12641 // Structured bindings is a C++17 feature. 12642 // all modes, including C++11, C++14 and C++17 12643 verifyFormat("auto [a, b] = f();"); 12644 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 12645 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 12646 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 12647 EXPECT_EQ("auto const volatile [a, b] = f();", 12648 format("auto const volatile[a, b] = f();")); 12649 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 12650 EXPECT_EQ("auto &[a, b, c] = f();", 12651 format("auto &[ a , b,c ] = f();")); 12652 EXPECT_EQ("auto &&[a, b, c] = f();", 12653 format("auto &&[ a , b,c ] = f();")); 12654 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 12655 EXPECT_EQ("auto const volatile &&[a, b] = f();", 12656 format("auto const volatile &&[a, b] = f();")); 12657 EXPECT_EQ("auto const &&[a, b] = f();", format("auto const && [a, b] = f();")); 12658 EXPECT_EQ("const auto &[a, b] = f();", format("const auto & [a, b] = f();")); 12659 EXPECT_EQ("const auto volatile &&[a, b] = f();", 12660 format("const auto volatile &&[a, b] = f();")); 12661 EXPECT_EQ("volatile const auto &&[a, b] = f();", 12662 format("volatile const auto &&[a, b] = f();")); 12663 EXPECT_EQ("const auto &&[a, b] = f();", format("const auto && [a, b] = f();")); 12664 12665 // Make sure we don't mistake structured bindings for lambdas. 12666 FormatStyle PointerMiddle = getLLVMStyle(); 12667 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 12668 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 12669 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 12670 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 12671 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 12672 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 12673 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 12674 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 12675 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 12676 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 12677 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 12678 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 12679 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 12680 12681 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 12682 format("for (const auto && [a, b] : some_range) {\n}")); 12683 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 12684 format("for (const auto & [a, b] : some_range) {\n}")); 12685 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 12686 format("for (const auto[a, b] : some_range) {\n}")); 12687 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 12688 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 12689 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 12690 EXPECT_EQ("auto const &[x, y](expr);", format("auto const & [x,y] (expr);")); 12691 EXPECT_EQ("auto const &&[x, y](expr);", format("auto const && [x,y] (expr);")); 12692 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 12693 EXPECT_EQ("auto const &[x, y]{expr};", format("auto const & [x,y] {expr};")); 12694 EXPECT_EQ("auto const &&[x, y]{expr};", format("auto const && [x,y] {expr};")); 12695 12696 format::FormatStyle Spaces = format::getLLVMStyle(); 12697 Spaces.SpacesInSquareBrackets = true; 12698 verifyFormat("auto [ a, b ] = f();", Spaces); 12699 verifyFormat("auto &&[ a, b ] = f();", Spaces); 12700 verifyFormat("auto &[ a, b ] = f();", Spaces); 12701 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 12702 verifyFormat("auto const &[ a, b ] = f();", Spaces); 12703 } 12704 12705 TEST_F(FormatTest, FileAndCode) { 12706 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 12707 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 12708 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 12709 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 12710 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n")); 12711 EXPECT_EQ( 12712 FormatStyle::LK_ObjC, 12713 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 12714 EXPECT_EQ(FormatStyle::LK_ObjC, 12715 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 12716 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 12717 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 12718 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n")); 12719 EXPECT_EQ(FormatStyle::LK_ObjC, 12720 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 12721 EXPECT_EQ( 12722 FormatStyle::LK_ObjC, 12723 guessLanguage("foo.h", 12724 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 12725 EXPECT_EQ( 12726 FormatStyle::LK_Cpp, 12727 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 12728 } 12729 12730 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 12731 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 12732 EXPECT_EQ(FormatStyle::LK_ObjC, 12733 guessLanguage("foo.h", "array[[calculator getIndex]];")); 12734 EXPECT_EQ(FormatStyle::LK_Cpp, 12735 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 12736 EXPECT_EQ( 12737 FormatStyle::LK_Cpp, 12738 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 12739 EXPECT_EQ(FormatStyle::LK_ObjC, 12740 guessLanguage("foo.h", "[[noreturn foo] bar];")); 12741 EXPECT_EQ(FormatStyle::LK_Cpp, 12742 guessLanguage("foo.h", "[[clang::fallthrough]];")); 12743 EXPECT_EQ(FormatStyle::LK_ObjC, 12744 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 12745 EXPECT_EQ(FormatStyle::LK_Cpp, 12746 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 12747 EXPECT_EQ(FormatStyle::LK_Cpp, 12748 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 12749 EXPECT_EQ(FormatStyle::LK_ObjC, 12750 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 12751 EXPECT_EQ(FormatStyle::LK_Cpp, 12752 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 12753 EXPECT_EQ( 12754 FormatStyle::LK_Cpp, 12755 guessLanguage("foo.h", 12756 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 12757 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 12758 } 12759 12760 TEST_F(FormatTest, GuessLanguageWithCaret) { 12761 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 12762 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 12763 EXPECT_EQ(FormatStyle::LK_ObjC, 12764 guessLanguage("foo.h", "int(^)(char, float);")); 12765 EXPECT_EQ(FormatStyle::LK_ObjC, 12766 guessLanguage("foo.h", "int(^foo)(char, float);")); 12767 EXPECT_EQ(FormatStyle::LK_ObjC, 12768 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 12769 EXPECT_EQ(FormatStyle::LK_ObjC, 12770 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 12771 EXPECT_EQ( 12772 FormatStyle::LK_ObjC, 12773 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 12774 } 12775 12776 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 12777 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 12778 "void f() {\n" 12779 " asm (\"mov %[e], %[d]\"\n" 12780 " : [d] \"=rm\" (d)\n" 12781 " [e] \"rm\" (*e));\n" 12782 "}")); 12783 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 12784 "void f() {\n" 12785 " _asm (\"mov %[e], %[d]\"\n" 12786 " : [d] \"=rm\" (d)\n" 12787 " [e] \"rm\" (*e));\n" 12788 "}")); 12789 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 12790 "void f() {\n" 12791 " __asm (\"mov %[e], %[d]\"\n" 12792 " : [d] \"=rm\" (d)\n" 12793 " [e] \"rm\" (*e));\n" 12794 "}")); 12795 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 12796 "void f() {\n" 12797 " __asm__ (\"mov %[e], %[d]\"\n" 12798 " : [d] \"=rm\" (d)\n" 12799 " [e] \"rm\" (*e));\n" 12800 "}")); 12801 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 12802 "void f() {\n" 12803 " asm (\"mov %[e], %[d]\"\n" 12804 " : [d] \"=rm\" (d),\n" 12805 " [e] \"rm\" (*e));\n" 12806 "}")); 12807 EXPECT_EQ(FormatStyle::LK_Cpp, 12808 guessLanguage("foo.h", "void f() {\n" 12809 " asm volatile (\"mov %[e], %[d]\"\n" 12810 " : [d] \"=rm\" (d)\n" 12811 " [e] \"rm\" (*e));\n" 12812 "}")); 12813 } 12814 12815 TEST_F(FormatTest, GuessLanguageWithChildLines) { 12816 EXPECT_EQ(FormatStyle::LK_Cpp, 12817 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 12818 EXPECT_EQ(FormatStyle::LK_ObjC, 12819 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 12820 EXPECT_EQ( 12821 FormatStyle::LK_Cpp, 12822 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 12823 EXPECT_EQ( 12824 FormatStyle::LK_ObjC, 12825 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 12826 } 12827 12828 } // end namespace 12829 } // end namespace format 12830 } // end namespace clang 12831