1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/Format/Format.h" 10 11 #include "../Tooling/ReplacementTest.h" 12 #include "FormatTestUtils.h" 13 14 #include "clang/Frontend/TextDiagnosticPrinter.h" 15 #include "llvm/Support/Debug.h" 16 #include "llvm/Support/MemoryBuffer.h" 17 #include "gtest/gtest.h" 18 19 #define DEBUG_TYPE "format-test" 20 21 using clang::tooling::ReplacementTest; 22 using clang::tooling::toReplacements; 23 24 namespace clang { 25 namespace format { 26 namespace { 27 28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 29 30 class FormatTest : public ::testing::Test { 31 protected: 32 enum StatusCheck { 33 SC_ExpectComplete, 34 SC_ExpectIncomplete, 35 SC_DoNotCheck 36 }; 37 38 std::string format(llvm::StringRef Code, 39 const FormatStyle &Style = getLLVMStyle(), 40 StatusCheck CheckComplete = SC_ExpectComplete) { 41 LLVM_DEBUG(llvm::errs() << "---\n"); 42 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 43 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 44 FormattingAttemptStatus Status; 45 tooling::Replacements Replaces = 46 reformat(Style, Code, Ranges, "<stdin>", &Status); 47 if (CheckComplete != SC_DoNotCheck) { 48 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 49 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 50 << Code << "\n\n"; 51 } 52 ReplacementCount = Replaces.size(); 53 auto Result = applyAllReplacements(Code, Replaces); 54 EXPECT_TRUE(static_cast<bool>(Result)); 55 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 56 return *Result; 57 } 58 59 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 60 Style.ColumnLimit = ColumnLimit; 61 return Style; 62 } 63 64 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 65 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 66 } 67 68 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 69 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 70 } 71 72 void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code, 73 const FormatStyle &Style = getLLVMStyle()) { 74 EXPECT_EQ(Expected.str(), format(Expected, Style)) 75 << "Expected code is not stable"; 76 EXPECT_EQ(Expected.str(), format(Code, Style)); 77 if (Style.Language == FormatStyle::LK_Cpp) { 78 // Objective-C++ is a superset of C++, so everything checked for C++ 79 // needs to be checked for Objective-C++ as well. 80 FormatStyle ObjCStyle = Style; 81 ObjCStyle.Language = FormatStyle::LK_ObjC; 82 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 83 } 84 } 85 86 void verifyFormat(llvm::StringRef Code, 87 const FormatStyle &Style = getLLVMStyle()) { 88 verifyFormat(Code, test::messUp(Code), Style); 89 } 90 91 void verifyIncompleteFormat(llvm::StringRef Code, 92 const FormatStyle &Style = getLLVMStyle()) { 93 EXPECT_EQ(Code.str(), 94 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 95 } 96 97 void verifyGoogleFormat(llvm::StringRef Code) { 98 verifyFormat(Code, getGoogleStyle()); 99 } 100 101 void verifyIndependentOfContext(llvm::StringRef text) { 102 verifyFormat(text); 103 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 104 } 105 106 /// \brief Verify that clang-format does not crash on the given input. 107 void verifyNoCrash(llvm::StringRef Code, 108 const FormatStyle &Style = getLLVMStyle()) { 109 format(Code, Style, SC_DoNotCheck); 110 } 111 112 int ReplacementCount; 113 }; 114 115 TEST_F(FormatTest, MessUp) { 116 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 117 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 118 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 119 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 120 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 121 } 122 123 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { 124 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); 125 } 126 127 TEST_F(FormatTest, LLVMStyleOverride) { 128 EXPECT_EQ(FormatStyle::LK_Proto, 129 getLLVMStyle(FormatStyle::LK_Proto).Language); 130 } 131 132 //===----------------------------------------------------------------------===// 133 // Basic function tests. 134 //===----------------------------------------------------------------------===// 135 136 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 137 EXPECT_EQ(";", format(";")); 138 } 139 140 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 141 EXPECT_EQ("int i;", format(" int i;")); 142 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 143 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 144 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 145 } 146 147 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 148 EXPECT_EQ("int i;", format("int\ni;")); 149 } 150 151 TEST_F(FormatTest, FormatsNestedBlockStatements) { 152 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 153 } 154 155 TEST_F(FormatTest, FormatsNestedCall) { 156 verifyFormat("Method(f1, f2(f3));"); 157 verifyFormat("Method(f1(f2, f3()));"); 158 verifyFormat("Method(f1(f2, (f3())));"); 159 } 160 161 TEST_F(FormatTest, NestedNameSpecifiers) { 162 verifyFormat("vector<::Type> v;"); 163 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 164 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 165 verifyFormat("bool a = 2 < ::SomeFunction();"); 166 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 167 verifyFormat("some::string getName();"); 168 } 169 170 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 171 EXPECT_EQ("if (a) {\n" 172 " f();\n" 173 "}", 174 format("if(a){f();}")); 175 EXPECT_EQ(4, ReplacementCount); 176 EXPECT_EQ("if (a) {\n" 177 " f();\n" 178 "}", 179 format("if (a) {\n" 180 " f();\n" 181 "}")); 182 EXPECT_EQ(0, ReplacementCount); 183 EXPECT_EQ("/*\r\n" 184 "\r\n" 185 "*/\r\n", 186 format("/*\r\n" 187 "\r\n" 188 "*/\r\n")); 189 EXPECT_EQ(0, ReplacementCount); 190 } 191 192 TEST_F(FormatTest, RemovesEmptyLines) { 193 EXPECT_EQ("class C {\n" 194 " int i;\n" 195 "};", 196 format("class C {\n" 197 " int i;\n" 198 "\n" 199 "};")); 200 201 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 202 EXPECT_EQ("namespace N {\n" 203 "\n" 204 "int i;\n" 205 "}", 206 format("namespace N {\n" 207 "\n" 208 "int i;\n" 209 "}", 210 getGoogleStyle())); 211 EXPECT_EQ("/* something */ namespace N {\n" 212 "\n" 213 "int i;\n" 214 "}", 215 format("/* something */ namespace N {\n" 216 "\n" 217 "int i;\n" 218 "}", 219 getGoogleStyle())); 220 EXPECT_EQ("inline namespace N {\n" 221 "\n" 222 "int i;\n" 223 "}", 224 format("inline namespace N {\n" 225 "\n" 226 "int i;\n" 227 "}", 228 getGoogleStyle())); 229 EXPECT_EQ("/* something */ inline namespace N {\n" 230 "\n" 231 "int i;\n" 232 "}", 233 format("/* something */ inline namespace N {\n" 234 "\n" 235 "int i;\n" 236 "}", 237 getGoogleStyle())); 238 EXPECT_EQ("export namespace N {\n" 239 "\n" 240 "int i;\n" 241 "}", 242 format("export namespace N {\n" 243 "\n" 244 "int i;\n" 245 "}", 246 getGoogleStyle())); 247 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 248 "\n" 249 "int i;\n" 250 "}", 251 format("extern /**/ \"C\" /**/ {\n" 252 "\n" 253 "int i;\n" 254 "}", 255 getGoogleStyle())); 256 257 // ...but do keep inlining and removing empty lines for non-block extern "C" 258 // functions. 259 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 260 EXPECT_EQ("extern \"C\" int f() {\n" 261 " int i = 42;\n" 262 " return i;\n" 263 "}", 264 format("extern \"C\" int f() {\n" 265 "\n" 266 " int i = 42;\n" 267 " return i;\n" 268 "}", 269 getGoogleStyle())); 270 271 // Remove empty lines at the beginning and end of blocks. 272 EXPECT_EQ("void f() {\n" 273 "\n" 274 " if (a) {\n" 275 "\n" 276 " f();\n" 277 " }\n" 278 "}", 279 format("void f() {\n" 280 "\n" 281 " if (a) {\n" 282 "\n" 283 " f();\n" 284 "\n" 285 " }\n" 286 "\n" 287 "}", 288 getLLVMStyle())); 289 EXPECT_EQ("void f() {\n" 290 " if (a) {\n" 291 " f();\n" 292 " }\n" 293 "}", 294 format("void f() {\n" 295 "\n" 296 " if (a) {\n" 297 "\n" 298 " f();\n" 299 "\n" 300 " }\n" 301 "\n" 302 "}", 303 getGoogleStyle())); 304 305 // Don't remove empty lines in more complex control statements. 306 EXPECT_EQ("void f() {\n" 307 " if (a) {\n" 308 " f();\n" 309 "\n" 310 " } else if (b) {\n" 311 " f();\n" 312 " }\n" 313 "}", 314 format("void f() {\n" 315 " if (a) {\n" 316 " f();\n" 317 "\n" 318 " } else if (b) {\n" 319 " f();\n" 320 "\n" 321 " }\n" 322 "\n" 323 "}")); 324 325 // Don't remove empty lines before namespace endings. 326 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 327 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 328 EXPECT_EQ("namespace {\n" 329 "int i;\n" 330 "\n" 331 "}", 332 format("namespace {\n" 333 "int i;\n" 334 "\n" 335 "}", LLVMWithNoNamespaceFix)); 336 EXPECT_EQ("namespace {\n" 337 "int i;\n" 338 "}", 339 format("namespace {\n" 340 "int i;\n" 341 "}", LLVMWithNoNamespaceFix)); 342 EXPECT_EQ("namespace {\n" 343 "int i;\n" 344 "\n" 345 "};", 346 format("namespace {\n" 347 "int i;\n" 348 "\n" 349 "};", LLVMWithNoNamespaceFix)); 350 EXPECT_EQ("namespace {\n" 351 "int i;\n" 352 "};", 353 format("namespace {\n" 354 "int i;\n" 355 "};", LLVMWithNoNamespaceFix)); 356 EXPECT_EQ("namespace {\n" 357 "int i;\n" 358 "\n" 359 "}", 360 format("namespace {\n" 361 "int i;\n" 362 "\n" 363 "}")); 364 EXPECT_EQ("namespace {\n" 365 "int i;\n" 366 "\n" 367 "} // namespace", 368 format("namespace {\n" 369 "int i;\n" 370 "\n" 371 "} // namespace")); 372 373 FormatStyle Style = getLLVMStyle(); 374 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 375 Style.MaxEmptyLinesToKeep = 2; 376 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 377 Style.BraceWrapping.AfterClass = true; 378 Style.BraceWrapping.AfterFunction = true; 379 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 380 381 EXPECT_EQ("class Foo\n" 382 "{\n" 383 " Foo() {}\n" 384 "\n" 385 " void funk() {}\n" 386 "};", 387 format("class Foo\n" 388 "{\n" 389 " Foo()\n" 390 " {\n" 391 " }\n" 392 "\n" 393 " void funk() {}\n" 394 "};", 395 Style)); 396 } 397 398 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 399 verifyFormat("x = (a) and (b);"); 400 verifyFormat("x = (a) or (b);"); 401 verifyFormat("x = (a) bitand (b);"); 402 verifyFormat("x = (a) bitor (b);"); 403 verifyFormat("x = (a) not_eq (b);"); 404 verifyFormat("x = (a) and_eq (b);"); 405 verifyFormat("x = (a) or_eq (b);"); 406 verifyFormat("x = (a) xor (b);"); 407 } 408 409 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 410 verifyFormat("x = compl(a);"); 411 verifyFormat("x = not(a);"); 412 verifyFormat("x = bitand(a);"); 413 // Unary operator must not be merged with the next identifier 414 verifyFormat("x = compl a;"); 415 verifyFormat("x = not a;"); 416 verifyFormat("x = bitand a;"); 417 } 418 419 //===----------------------------------------------------------------------===// 420 // Tests for control statements. 421 //===----------------------------------------------------------------------===// 422 423 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 424 verifyFormat("if (true)\n f();\ng();"); 425 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 426 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 427 verifyFormat("if constexpr (true)\n" 428 " f();\ng();"); 429 verifyFormat("if CONSTEXPR (true)\n" 430 " f();\ng();"); 431 verifyFormat("if constexpr (a)\n" 432 " if constexpr (b)\n" 433 " if constexpr (c)\n" 434 " g();\n" 435 "h();"); 436 verifyFormat("if CONSTEXPR (a)\n" 437 " if CONSTEXPR (b)\n" 438 " if CONSTEXPR (c)\n" 439 " g();\n" 440 "h();"); 441 verifyFormat("if constexpr (a)\n" 442 " if constexpr (b) {\n" 443 " f();\n" 444 " }\n" 445 "g();"); 446 verifyFormat("if CONSTEXPR (a)\n" 447 " if CONSTEXPR (b) {\n" 448 " f();\n" 449 " }\n" 450 "g();"); 451 452 FormatStyle AllowsMergedIf = getLLVMStyle(); 453 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 454 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 455 FormatStyle::SIS_WithoutElse; 456 verifyFormat("if (a)\n" 457 " // comment\n" 458 " f();", 459 AllowsMergedIf); 460 verifyFormat("{\n" 461 " if (a)\n" 462 " label:\n" 463 " f();\n" 464 "}", 465 AllowsMergedIf); 466 verifyFormat("#define A \\\n" 467 " if (a) \\\n" 468 " label: \\\n" 469 " f()", 470 AllowsMergedIf); 471 verifyFormat("if (a)\n" 472 " ;", 473 AllowsMergedIf); 474 verifyFormat("if (a)\n" 475 " if (b) return;", 476 AllowsMergedIf); 477 478 verifyFormat("if (a) // Can't merge this\n" 479 " f();\n", 480 AllowsMergedIf); 481 verifyFormat("if (a) /* still don't merge */\n" 482 " f();", 483 AllowsMergedIf); 484 verifyFormat("if (a) { // Never merge this\n" 485 " f();\n" 486 "}", 487 AllowsMergedIf); 488 verifyFormat("if (a) { /* Never merge this */\n" 489 " f();\n" 490 "}", 491 AllowsMergedIf); 492 493 AllowsMergedIf.ColumnLimit = 14; 494 verifyFormat("if (a) return;", AllowsMergedIf); 495 verifyFormat("if (aaaaaaaaa)\n" 496 " return;", 497 AllowsMergedIf); 498 499 AllowsMergedIf.ColumnLimit = 13; 500 verifyFormat("if (a)\n return;", AllowsMergedIf); 501 } 502 503 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 504 FormatStyle AllowsMergedIf = getLLVMStyle(); 505 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 506 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 507 FormatStyle::SIS_WithoutElse; 508 verifyFormat("if (a)\n" 509 " f();\n" 510 "else {\n" 511 " g();\n" 512 "}", 513 AllowsMergedIf); 514 verifyFormat("if (a)\n" 515 " f();\n" 516 "else\n" 517 " g();\n", 518 AllowsMergedIf); 519 520 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always; 521 522 verifyFormat("if (a) f();\n" 523 "else {\n" 524 " g();\n" 525 "}", 526 AllowsMergedIf); 527 verifyFormat("if (a) f();\n" 528 "else {\n" 529 " if (a) f();\n" 530 " else {\n" 531 " g();\n" 532 " }\n" 533 " g();\n" 534 "}", 535 AllowsMergedIf); 536 } 537 538 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 539 FormatStyle AllowsMergedLoops = getLLVMStyle(); 540 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 541 verifyFormat("while (true) continue;", AllowsMergedLoops); 542 verifyFormat("for (;;) continue;", AllowsMergedLoops); 543 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 544 verifyFormat("while (true)\n" 545 " ;", 546 AllowsMergedLoops); 547 verifyFormat("for (;;)\n" 548 " ;", 549 AllowsMergedLoops); 550 verifyFormat("for (;;)\n" 551 " for (;;) continue;", 552 AllowsMergedLoops); 553 verifyFormat("for (;;) // Can't merge this\n" 554 " continue;", 555 AllowsMergedLoops); 556 verifyFormat("for (;;) /* still don't merge */\n" 557 " continue;", 558 AllowsMergedLoops); 559 } 560 561 TEST_F(FormatTest, FormatShortBracedStatements) { 562 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 563 AllowSimpleBracedStatements.ColumnLimit = 40; 564 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 565 566 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 567 FormatStyle::SIS_WithoutElse; 568 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 569 570 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 571 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 572 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 573 574 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 575 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 576 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 577 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 578 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 579 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 580 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 581 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 582 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 583 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 584 verifyFormat("if (true) {\n" 585 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 586 "}", 587 AllowSimpleBracedStatements); 588 verifyFormat("if (true) { //\n" 589 " f();\n" 590 "}", 591 AllowSimpleBracedStatements); 592 verifyFormat("if (true) {\n" 593 " f();\n" 594 " f();\n" 595 "}", 596 AllowSimpleBracedStatements); 597 verifyFormat("if (true) {\n" 598 " f();\n" 599 "} else {\n" 600 " f();\n" 601 "}", 602 AllowSimpleBracedStatements); 603 604 verifyFormat("struct A2 {\n" 605 " int X;\n" 606 "};", 607 AllowSimpleBracedStatements); 608 verifyFormat("typedef struct A2 {\n" 609 " int X;\n" 610 "} A2_t;", 611 AllowSimpleBracedStatements); 612 verifyFormat("template <int> struct A2 {\n" 613 " struct B {};\n" 614 "};", 615 AllowSimpleBracedStatements); 616 617 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 618 FormatStyle::SIS_Never; 619 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 620 verifyFormat("if (true) {\n" 621 " f();\n" 622 "}", 623 AllowSimpleBracedStatements); 624 verifyFormat("if (true) {\n" 625 " f();\n" 626 "} else {\n" 627 " f();\n" 628 "}", 629 AllowSimpleBracedStatements); 630 631 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 632 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 633 verifyFormat("while (true) {\n" 634 " f();\n" 635 "}", 636 AllowSimpleBracedStatements); 637 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 638 verifyFormat("for (;;) {\n" 639 " f();\n" 640 "}", 641 AllowSimpleBracedStatements); 642 643 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 644 FormatStyle::SIS_WithoutElse; 645 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 646 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true; 647 648 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 649 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 650 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 651 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 652 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 653 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 654 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 655 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 656 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 657 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 658 verifyFormat("if (true)\n" 659 "{\n" 660 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 661 "}", 662 AllowSimpleBracedStatements); 663 verifyFormat("if (true)\n" 664 "{ //\n" 665 " f();\n" 666 "}", 667 AllowSimpleBracedStatements); 668 verifyFormat("if (true)\n" 669 "{\n" 670 " f();\n" 671 " f();\n" 672 "}", 673 AllowSimpleBracedStatements); 674 verifyFormat("if (true)\n" 675 "{\n" 676 " f();\n" 677 "} else\n" 678 "{\n" 679 " f();\n" 680 "}", 681 AllowSimpleBracedStatements); 682 683 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 684 FormatStyle::SIS_Never; 685 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 686 verifyFormat("if (true)\n" 687 "{\n" 688 " f();\n" 689 "}", 690 AllowSimpleBracedStatements); 691 verifyFormat("if (true)\n" 692 "{\n" 693 " f();\n" 694 "} else\n" 695 "{\n" 696 " f();\n" 697 "}", 698 AllowSimpleBracedStatements); 699 700 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 701 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 702 verifyFormat("while (true)\n" 703 "{\n" 704 " f();\n" 705 "}", 706 AllowSimpleBracedStatements); 707 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 708 verifyFormat("for (;;)\n" 709 "{\n" 710 " f();\n" 711 "}", 712 AllowSimpleBracedStatements); 713 } 714 715 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 716 FormatStyle Style = getLLVMStyleWithColumns(60); 717 Style.AllowShortBlocksOnASingleLine = true; 718 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 719 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 720 EXPECT_EQ("#define A \\\n" 721 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 722 " { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n" 723 "X;", 724 format("#define A \\\n" 725 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 726 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 727 " }\n" 728 "X;", 729 Style)); 730 } 731 732 TEST_F(FormatTest, ParseIfElse) { 733 verifyFormat("if (true)\n" 734 " if (true)\n" 735 " if (true)\n" 736 " f();\n" 737 " else\n" 738 " g();\n" 739 " else\n" 740 " h();\n" 741 "else\n" 742 " i();"); 743 verifyFormat("if (true)\n" 744 " if (true)\n" 745 " if (true) {\n" 746 " if (true)\n" 747 " f();\n" 748 " } else {\n" 749 " g();\n" 750 " }\n" 751 " else\n" 752 " h();\n" 753 "else {\n" 754 " i();\n" 755 "}"); 756 verifyFormat("if (true)\n" 757 " if constexpr (true)\n" 758 " if (true) {\n" 759 " if constexpr (true)\n" 760 " f();\n" 761 " } else {\n" 762 " g();\n" 763 " }\n" 764 " else\n" 765 " h();\n" 766 "else {\n" 767 " i();\n" 768 "}"); 769 verifyFormat("if (true)\n" 770 " if CONSTEXPR (true)\n" 771 " if (true) {\n" 772 " if CONSTEXPR (true)\n" 773 " f();\n" 774 " } else {\n" 775 " g();\n" 776 " }\n" 777 " else\n" 778 " h();\n" 779 "else {\n" 780 " i();\n" 781 "}"); 782 verifyFormat("void f() {\n" 783 " if (a) {\n" 784 " } else {\n" 785 " }\n" 786 "}"); 787 } 788 789 TEST_F(FormatTest, ElseIf) { 790 verifyFormat("if (a) {\n} else if (b) {\n}"); 791 verifyFormat("if (a)\n" 792 " f();\n" 793 "else if (b)\n" 794 " g();\n" 795 "else\n" 796 " h();"); 797 verifyFormat("if constexpr (a)\n" 798 " f();\n" 799 "else if constexpr (b)\n" 800 " g();\n" 801 "else\n" 802 " h();"); 803 verifyFormat("if CONSTEXPR (a)\n" 804 " f();\n" 805 "else if CONSTEXPR (b)\n" 806 " g();\n" 807 "else\n" 808 " h();"); 809 verifyFormat("if (a) {\n" 810 " f();\n" 811 "}\n" 812 "// or else ..\n" 813 "else {\n" 814 " g()\n" 815 "}"); 816 817 verifyFormat("if (a) {\n" 818 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 819 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 820 "}"); 821 verifyFormat("if (a) {\n" 822 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 823 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 824 "}"); 825 verifyFormat("if (a) {\n" 826 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 827 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 828 "}"); 829 verifyFormat("if (a) {\n" 830 "} else if (\n" 831 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 832 "}", 833 getLLVMStyleWithColumns(62)); 834 verifyFormat("if (a) {\n" 835 "} else if constexpr (\n" 836 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 837 "}", 838 getLLVMStyleWithColumns(62)); 839 verifyFormat("if (a) {\n" 840 "} else if CONSTEXPR (\n" 841 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 842 "}", 843 getLLVMStyleWithColumns(62)); 844 } 845 846 TEST_F(FormatTest, FormatsForLoop) { 847 verifyFormat( 848 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 849 " ++VeryVeryLongLoopVariable)\n" 850 " ;"); 851 verifyFormat("for (;;)\n" 852 " f();"); 853 verifyFormat("for (;;) {\n}"); 854 verifyFormat("for (;;) {\n" 855 " f();\n" 856 "}"); 857 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 858 859 verifyFormat( 860 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 861 " E = UnwrappedLines.end();\n" 862 " I != E; ++I) {\n}"); 863 864 verifyFormat( 865 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 866 " ++IIIII) {\n}"); 867 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 868 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 869 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 870 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 871 " I = FD->getDeclsInPrototypeScope().begin(),\n" 872 " E = FD->getDeclsInPrototypeScope().end();\n" 873 " I != E; ++I) {\n}"); 874 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 875 " I = Container.begin(),\n" 876 " E = Container.end();\n" 877 " I != E; ++I) {\n}", 878 getLLVMStyleWithColumns(76)); 879 880 verifyFormat( 881 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 882 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 883 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 884 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 885 " ++aaaaaaaaaaa) {\n}"); 886 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 887 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 888 " ++i) {\n}"); 889 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 890 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 891 "}"); 892 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 893 " aaaaaaaaaa);\n" 894 " iter; ++iter) {\n" 895 "}"); 896 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 897 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 898 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 899 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 900 901 // These should not be formatted as Objective-C for-in loops. 902 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 903 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 904 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 905 verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 906 907 FormatStyle NoBinPacking = getLLVMStyle(); 908 NoBinPacking.BinPackParameters = false; 909 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 910 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 911 " aaaaaaaaaaaaaaaa,\n" 912 " aaaaaaaaaaaaaaaa,\n" 913 " aaaaaaaaaaaaaaaa);\n" 914 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 915 "}", 916 NoBinPacking); 917 verifyFormat( 918 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 919 " E = UnwrappedLines.end();\n" 920 " I != E;\n" 921 " ++I) {\n}", 922 NoBinPacking); 923 924 FormatStyle AlignLeft = getLLVMStyle(); 925 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 926 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 927 } 928 929 TEST_F(FormatTest, RangeBasedForLoops) { 930 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 931 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 932 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 933 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 934 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 935 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 936 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 937 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 938 } 939 940 TEST_F(FormatTest, ForEachLoops) { 941 verifyFormat("void f() {\n" 942 " foreach (Item *item, itemlist) {}\n" 943 " Q_FOREACH (Item *item, itemlist) {}\n" 944 " BOOST_FOREACH (Item *item, itemlist) {}\n" 945 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 946 "}"); 947 948 // As function-like macros. 949 verifyFormat("#define foreach(x, y)\n" 950 "#define Q_FOREACH(x, y)\n" 951 "#define BOOST_FOREACH(x, y)\n" 952 "#define UNKNOWN_FOREACH(x, y)\n"); 953 954 // Not as function-like macros. 955 verifyFormat("#define foreach (x, y)\n" 956 "#define Q_FOREACH (x, y)\n" 957 "#define BOOST_FOREACH (x, y)\n" 958 "#define UNKNOWN_FOREACH (x, y)\n"); 959 } 960 961 TEST_F(FormatTest, FormatsWhileLoop) { 962 verifyFormat("while (true) {\n}"); 963 verifyFormat("while (true)\n" 964 " f();"); 965 verifyFormat("while () {\n}"); 966 verifyFormat("while () {\n" 967 " f();\n" 968 "}"); 969 } 970 971 TEST_F(FormatTest, FormatsDoWhile) { 972 verifyFormat("do {\n" 973 " do_something();\n" 974 "} while (something());"); 975 verifyFormat("do\n" 976 " do_something();\n" 977 "while (something());"); 978 } 979 980 TEST_F(FormatTest, FormatsSwitchStatement) { 981 verifyFormat("switch (x) {\n" 982 "case 1:\n" 983 " f();\n" 984 " break;\n" 985 "case kFoo:\n" 986 "case ns::kBar:\n" 987 "case kBaz:\n" 988 " break;\n" 989 "default:\n" 990 " g();\n" 991 " break;\n" 992 "}"); 993 verifyFormat("switch (x) {\n" 994 "case 1: {\n" 995 " f();\n" 996 " break;\n" 997 "}\n" 998 "case 2: {\n" 999 " break;\n" 1000 "}\n" 1001 "}"); 1002 verifyFormat("switch (x) {\n" 1003 "case 1: {\n" 1004 " f();\n" 1005 " {\n" 1006 " g();\n" 1007 " h();\n" 1008 " }\n" 1009 " break;\n" 1010 "}\n" 1011 "}"); 1012 verifyFormat("switch (x) {\n" 1013 "case 1: {\n" 1014 " f();\n" 1015 " if (foo) {\n" 1016 " g();\n" 1017 " h();\n" 1018 " }\n" 1019 " break;\n" 1020 "}\n" 1021 "}"); 1022 verifyFormat("switch (x) {\n" 1023 "case 1: {\n" 1024 " f();\n" 1025 " g();\n" 1026 "} break;\n" 1027 "}"); 1028 verifyFormat("switch (test)\n" 1029 " ;"); 1030 verifyFormat("switch (x) {\n" 1031 "default: {\n" 1032 " // Do nothing.\n" 1033 "}\n" 1034 "}"); 1035 verifyFormat("switch (x) {\n" 1036 "// comment\n" 1037 "// if 1, do f()\n" 1038 "case 1:\n" 1039 " f();\n" 1040 "}"); 1041 verifyFormat("switch (x) {\n" 1042 "case 1:\n" 1043 " // Do amazing stuff\n" 1044 " {\n" 1045 " f();\n" 1046 " g();\n" 1047 " }\n" 1048 " break;\n" 1049 "}"); 1050 verifyFormat("#define A \\\n" 1051 " switch (x) { \\\n" 1052 " case a: \\\n" 1053 " foo = b; \\\n" 1054 " }", 1055 getLLVMStyleWithColumns(20)); 1056 verifyFormat("#define OPERATION_CASE(name) \\\n" 1057 " case OP_name: \\\n" 1058 " return operations::Operation##name\n", 1059 getLLVMStyleWithColumns(40)); 1060 verifyFormat("switch (x) {\n" 1061 "case 1:;\n" 1062 "default:;\n" 1063 " int i;\n" 1064 "}"); 1065 1066 verifyGoogleFormat("switch (x) {\n" 1067 " case 1:\n" 1068 " f();\n" 1069 " break;\n" 1070 " case kFoo:\n" 1071 " case ns::kBar:\n" 1072 " case kBaz:\n" 1073 " break;\n" 1074 " default:\n" 1075 " g();\n" 1076 " break;\n" 1077 "}"); 1078 verifyGoogleFormat("switch (x) {\n" 1079 " case 1: {\n" 1080 " f();\n" 1081 " break;\n" 1082 " }\n" 1083 "}"); 1084 verifyGoogleFormat("switch (test)\n" 1085 " ;"); 1086 1087 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 1088 " case OP_name: \\\n" 1089 " return operations::Operation##name\n"); 1090 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 1091 " // Get the correction operation class.\n" 1092 " switch (OpCode) {\n" 1093 " CASE(Add);\n" 1094 " CASE(Subtract);\n" 1095 " default:\n" 1096 " return operations::Unknown;\n" 1097 " }\n" 1098 "#undef OPERATION_CASE\n" 1099 "}"); 1100 verifyFormat("DEBUG({\n" 1101 " switch (x) {\n" 1102 " case A:\n" 1103 " f();\n" 1104 " break;\n" 1105 " // fallthrough\n" 1106 " case B:\n" 1107 " g();\n" 1108 " break;\n" 1109 " }\n" 1110 "});"); 1111 EXPECT_EQ("DEBUG({\n" 1112 " switch (x) {\n" 1113 " case A:\n" 1114 " f();\n" 1115 " break;\n" 1116 " // On B:\n" 1117 " case B:\n" 1118 " g();\n" 1119 " break;\n" 1120 " }\n" 1121 "});", 1122 format("DEBUG({\n" 1123 " switch (x) {\n" 1124 " case A:\n" 1125 " f();\n" 1126 " break;\n" 1127 " // On B:\n" 1128 " case B:\n" 1129 " g();\n" 1130 " break;\n" 1131 " }\n" 1132 "});", 1133 getLLVMStyle())); 1134 EXPECT_EQ("switch (n) {\n" 1135 "case 0: {\n" 1136 " return false;\n" 1137 "}\n" 1138 "default: {\n" 1139 " return true;\n" 1140 "}\n" 1141 "}", 1142 format("switch (n)\n" 1143 "{\n" 1144 "case 0: {\n" 1145 " return false;\n" 1146 "}\n" 1147 "default: {\n" 1148 " return true;\n" 1149 "}\n" 1150 "}", 1151 getLLVMStyle())); 1152 verifyFormat("switch (a) {\n" 1153 "case (b):\n" 1154 " return;\n" 1155 "}"); 1156 1157 verifyFormat("switch (a) {\n" 1158 "case some_namespace::\n" 1159 " some_constant:\n" 1160 " return;\n" 1161 "}", 1162 getLLVMStyleWithColumns(34)); 1163 1164 FormatStyle Style = getLLVMStyle(); 1165 Style.IndentCaseLabels = true; 1166 Style.AllowShortBlocksOnASingleLine = false; 1167 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1168 Style.BraceWrapping.AfterCaseLabel = true; 1169 Style.BraceWrapping.AfterControlStatement = true; 1170 EXPECT_EQ("switch (n)\n" 1171 "{\n" 1172 " case 0:\n" 1173 " {\n" 1174 " return false;\n" 1175 " }\n" 1176 " default:\n" 1177 " {\n" 1178 " return true;\n" 1179 " }\n" 1180 "}", 1181 format("switch (n) {\n" 1182 " case 0: {\n" 1183 " return false;\n" 1184 " }\n" 1185 " default: {\n" 1186 " return true;\n" 1187 " }\n" 1188 "}", 1189 Style)); 1190 Style.BraceWrapping.AfterCaseLabel = false; 1191 EXPECT_EQ("switch (n)\n" 1192 "{\n" 1193 " case 0: {\n" 1194 " return false;\n" 1195 " }\n" 1196 " default: {\n" 1197 " return true;\n" 1198 " }\n" 1199 "}", 1200 format("switch (n) {\n" 1201 " case 0:\n" 1202 " {\n" 1203 " return false;\n" 1204 " }\n" 1205 " default:\n" 1206 " {\n" 1207 " return true;\n" 1208 " }\n" 1209 "}", 1210 Style)); 1211 } 1212 1213 TEST_F(FormatTest, CaseRanges) { 1214 verifyFormat("switch (x) {\n" 1215 "case 'A' ... 'Z':\n" 1216 "case 1 ... 5:\n" 1217 "case a ... b:\n" 1218 " break;\n" 1219 "}"); 1220 } 1221 1222 TEST_F(FormatTest, ShortCaseLabels) { 1223 FormatStyle Style = getLLVMStyle(); 1224 Style.AllowShortCaseLabelsOnASingleLine = true; 1225 verifyFormat("switch (a) {\n" 1226 "case 1: x = 1; break;\n" 1227 "case 2: return;\n" 1228 "case 3:\n" 1229 "case 4:\n" 1230 "case 5: return;\n" 1231 "case 6: // comment\n" 1232 " return;\n" 1233 "case 7:\n" 1234 " // comment\n" 1235 " return;\n" 1236 "case 8:\n" 1237 " x = 8; // comment\n" 1238 " break;\n" 1239 "default: y = 1; break;\n" 1240 "}", 1241 Style); 1242 verifyFormat("switch (a) {\n" 1243 "case 0: return; // comment\n" 1244 "case 1: break; // comment\n" 1245 "case 2: return;\n" 1246 "// comment\n" 1247 "case 3: return;\n" 1248 "// comment 1\n" 1249 "// comment 2\n" 1250 "// comment 3\n" 1251 "case 4: break; /* comment */\n" 1252 "case 5:\n" 1253 " // comment\n" 1254 " break;\n" 1255 "case 6: /* comment */ x = 1; break;\n" 1256 "case 7: x = /* comment */ 1; break;\n" 1257 "case 8:\n" 1258 " x = 1; /* comment */\n" 1259 " break;\n" 1260 "case 9:\n" 1261 " break; // comment line 1\n" 1262 " // comment line 2\n" 1263 "}", 1264 Style); 1265 EXPECT_EQ("switch (a) {\n" 1266 "case 1:\n" 1267 " x = 8;\n" 1268 " // fall through\n" 1269 "case 2: x = 8;\n" 1270 "// comment\n" 1271 "case 3:\n" 1272 " return; /* comment line 1\n" 1273 " * comment line 2 */\n" 1274 "case 4: i = 8;\n" 1275 "// something else\n" 1276 "#if FOO\n" 1277 "case 5: break;\n" 1278 "#endif\n" 1279 "}", 1280 format("switch (a) {\n" 1281 "case 1: x = 8;\n" 1282 " // fall through\n" 1283 "case 2:\n" 1284 " x = 8;\n" 1285 "// comment\n" 1286 "case 3:\n" 1287 " return; /* comment line 1\n" 1288 " * comment line 2 */\n" 1289 "case 4:\n" 1290 " i = 8;\n" 1291 "// something else\n" 1292 "#if FOO\n" 1293 "case 5: break;\n" 1294 "#endif\n" 1295 "}", 1296 Style)); 1297 EXPECT_EQ("switch (a) {\n" "case 0:\n" 1298 " return; // long long long long long long long long long long long long comment\n" 1299 " // line\n" "}", 1300 format("switch (a) {\n" 1301 "case 0: return; // long long long long long long long long long long long long comment line\n" 1302 "}", 1303 Style)); 1304 EXPECT_EQ("switch (a) {\n" 1305 "case 0:\n" 1306 " return; /* long long long long long long long long long long long long comment\n" 1307 " line */\n" 1308 "}", 1309 format("switch (a) {\n" 1310 "case 0: return; /* long long long long long long long long long long long long comment line */\n" 1311 "}", 1312 Style)); 1313 verifyFormat("switch (a) {\n" 1314 "#if FOO\n" 1315 "case 0: return 0;\n" 1316 "#endif\n" 1317 "}", 1318 Style); 1319 verifyFormat("switch (a) {\n" 1320 "case 1: {\n" 1321 "}\n" 1322 "case 2: {\n" 1323 " return;\n" 1324 "}\n" 1325 "case 3: {\n" 1326 " x = 1;\n" 1327 " return;\n" 1328 "}\n" 1329 "case 4:\n" 1330 " if (x)\n" 1331 " return;\n" 1332 "}", 1333 Style); 1334 Style.ColumnLimit = 21; 1335 verifyFormat("switch (a) {\n" 1336 "case 1: x = 1; break;\n" 1337 "case 2: return;\n" 1338 "case 3:\n" 1339 "case 4:\n" 1340 "case 5: return;\n" 1341 "default:\n" 1342 " y = 1;\n" 1343 " break;\n" 1344 "}", 1345 Style); 1346 Style.ColumnLimit = 80; 1347 Style.AllowShortCaseLabelsOnASingleLine = false; 1348 Style.IndentCaseLabels = true; 1349 EXPECT_EQ("switch (n) {\n" 1350 " default /*comments*/:\n" 1351 " return true;\n" 1352 " case 0:\n" 1353 " return false;\n" 1354 "}", 1355 format("switch (n) {\n" 1356 "default/*comments*/:\n" 1357 " return true;\n" 1358 "case 0:\n" 1359 " return false;\n" 1360 "}", 1361 Style)); 1362 Style.AllowShortCaseLabelsOnASingleLine = true; 1363 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1364 Style.BraceWrapping.AfterCaseLabel = true; 1365 Style.BraceWrapping.AfterControlStatement = true; 1366 EXPECT_EQ("switch (n)\n" 1367 "{\n" 1368 " case 0:\n" 1369 " {\n" 1370 " return false;\n" 1371 " }\n" 1372 " default:\n" 1373 " {\n" 1374 " return true;\n" 1375 " }\n" 1376 "}", 1377 format("switch (n) {\n" 1378 " case 0: {\n" 1379 " return false;\n" 1380 " }\n" 1381 " default:\n" 1382 " {\n" 1383 " return true;\n" 1384 " }\n" 1385 "}", 1386 Style)); 1387 } 1388 1389 TEST_F(FormatTest, FormatsLabels) { 1390 verifyFormat("void f() {\n" 1391 " some_code();\n" 1392 "test_label:\n" 1393 " some_other_code();\n" 1394 " {\n" 1395 " some_more_code();\n" 1396 " another_label:\n" 1397 " some_more_code();\n" 1398 " }\n" 1399 "}"); 1400 verifyFormat("{\n" 1401 " some_code();\n" 1402 "test_label:\n" 1403 " some_other_code();\n" 1404 "}"); 1405 verifyFormat("{\n" 1406 " some_code();\n" 1407 "test_label:;\n" 1408 " int i = 0;\n" 1409 "}"); 1410 } 1411 1412 //===----------------------------------------------------------------------===// 1413 // Tests for classes, namespaces, etc. 1414 //===----------------------------------------------------------------------===// 1415 1416 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1417 verifyFormat("class A {};"); 1418 } 1419 1420 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1421 verifyFormat("class A {\n" 1422 "public:\n" 1423 "public: // comment\n" 1424 "protected:\n" 1425 "private:\n" 1426 " void f() {}\n" 1427 "};"); 1428 verifyFormat("export class A {\n" 1429 "public:\n" 1430 "public: // comment\n" 1431 "protected:\n" 1432 "private:\n" 1433 " void f() {}\n" 1434 "};"); 1435 verifyGoogleFormat("class A {\n" 1436 " public:\n" 1437 " protected:\n" 1438 " private:\n" 1439 " void f() {}\n" 1440 "};"); 1441 verifyGoogleFormat("export class A {\n" 1442 " public:\n" 1443 " protected:\n" 1444 " private:\n" 1445 " void f() {}\n" 1446 "};"); 1447 verifyFormat("class A {\n" 1448 "public slots:\n" 1449 " void f1() {}\n" 1450 "public Q_SLOTS:\n" 1451 " void f2() {}\n" 1452 "protected slots:\n" 1453 " void f3() {}\n" 1454 "protected Q_SLOTS:\n" 1455 " void f4() {}\n" 1456 "private slots:\n" 1457 " void f5() {}\n" 1458 "private Q_SLOTS:\n" 1459 " void f6() {}\n" 1460 "signals:\n" 1461 " void g1();\n" 1462 "Q_SIGNALS:\n" 1463 " void g2();\n" 1464 "};"); 1465 1466 // Don't interpret 'signals' the wrong way. 1467 verifyFormat("signals.set();"); 1468 verifyFormat("for (Signals signals : f()) {\n}"); 1469 verifyFormat("{\n" 1470 " signals.set(); // This needs indentation.\n" 1471 "}"); 1472 verifyFormat("void f() {\n" 1473 "label:\n" 1474 " signals.baz();\n" 1475 "}"); 1476 } 1477 1478 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1479 EXPECT_EQ("class A {\n" 1480 "public:\n" 1481 " void f();\n" 1482 "\n" 1483 "private:\n" 1484 " void g() {}\n" 1485 " // test\n" 1486 "protected:\n" 1487 " int h;\n" 1488 "};", 1489 format("class A {\n" 1490 "public:\n" 1491 "void f();\n" 1492 "private:\n" 1493 "void g() {}\n" 1494 "// test\n" 1495 "protected:\n" 1496 "int h;\n" 1497 "};")); 1498 EXPECT_EQ("class A {\n" 1499 "protected:\n" 1500 "public:\n" 1501 " void f();\n" 1502 "};", 1503 format("class A {\n" 1504 "protected:\n" 1505 "\n" 1506 "public:\n" 1507 "\n" 1508 " void f();\n" 1509 "};")); 1510 1511 // Even ensure proper spacing inside macros. 1512 EXPECT_EQ("#define B \\\n" 1513 " class A { \\\n" 1514 " protected: \\\n" 1515 " public: \\\n" 1516 " void f(); \\\n" 1517 " };", 1518 format("#define B \\\n" 1519 " class A { \\\n" 1520 " protected: \\\n" 1521 " \\\n" 1522 " public: \\\n" 1523 " \\\n" 1524 " void f(); \\\n" 1525 " };", 1526 getGoogleStyle())); 1527 // But don't remove empty lines after macros ending in access specifiers. 1528 EXPECT_EQ("#define A private:\n" 1529 "\n" 1530 "int i;", 1531 format("#define A private:\n" 1532 "\n" 1533 "int i;")); 1534 } 1535 1536 TEST_F(FormatTest, FormatsClasses) { 1537 verifyFormat("class A : public B {};"); 1538 verifyFormat("class A : public ::B {};"); 1539 1540 verifyFormat( 1541 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1542 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1543 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1544 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1545 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1546 verifyFormat( 1547 "class A : public B, public C, public D, public E, public F {};"); 1548 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1549 " public C,\n" 1550 " public D,\n" 1551 " public E,\n" 1552 " public F,\n" 1553 " public G {};"); 1554 1555 verifyFormat("class\n" 1556 " ReallyReallyLongClassName {\n" 1557 " int i;\n" 1558 "};", 1559 getLLVMStyleWithColumns(32)); 1560 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1561 " aaaaaaaaaaaaaaaa> {};"); 1562 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1563 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1564 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1565 verifyFormat("template <class R, class C>\n" 1566 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1567 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1568 verifyFormat("class ::A::B {};"); 1569 } 1570 1571 TEST_F(FormatTest, BreakInheritanceStyle) { 1572 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 1573 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 1574 FormatStyle::BILS_BeforeComma; 1575 verifyFormat("class MyClass : public X {};", 1576 StyleWithInheritanceBreakBeforeComma); 1577 verifyFormat("class MyClass\n" 1578 " : public X\n" 1579 " , public Y {};", 1580 StyleWithInheritanceBreakBeforeComma); 1581 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 1582 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 1583 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 1584 StyleWithInheritanceBreakBeforeComma); 1585 verifyFormat("struct aaaaaaaaaaaaa\n" 1586 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 1587 " aaaaaaaaaaaaaaaa> {};", 1588 StyleWithInheritanceBreakBeforeComma); 1589 1590 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 1591 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 1592 FormatStyle::BILS_AfterColon; 1593 verifyFormat("class MyClass : public X {};", 1594 StyleWithInheritanceBreakAfterColon); 1595 verifyFormat("class MyClass : public X, public Y {};", 1596 StyleWithInheritanceBreakAfterColon); 1597 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 1598 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1599 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 1600 StyleWithInheritanceBreakAfterColon); 1601 verifyFormat("struct aaaaaaaaaaaaa :\n" 1602 " public aaaaaaaaaaaaaaaaaaa< // break\n" 1603 " aaaaaaaaaaaaaaaa> {};", 1604 StyleWithInheritanceBreakAfterColon); 1605 } 1606 1607 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1608 verifyFormat("class A {\n} a, b;"); 1609 verifyFormat("struct A {\n} a, b;"); 1610 verifyFormat("union A {\n} a;"); 1611 } 1612 1613 TEST_F(FormatTest, FormatsEnum) { 1614 verifyFormat("enum {\n" 1615 " Zero,\n" 1616 " One = 1,\n" 1617 " Two = One + 1,\n" 1618 " Three = (One + Two),\n" 1619 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1620 " Five = (One, Two, Three, Four, 5)\n" 1621 "};"); 1622 verifyGoogleFormat("enum {\n" 1623 " Zero,\n" 1624 " One = 1,\n" 1625 " Two = One + 1,\n" 1626 " Three = (One + Two),\n" 1627 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1628 " Five = (One, Two, Three, Four, 5)\n" 1629 "};"); 1630 verifyFormat("enum Enum {};"); 1631 verifyFormat("enum {};"); 1632 verifyFormat("enum X E {} d;"); 1633 verifyFormat("enum __attribute__((...)) E {} d;"); 1634 verifyFormat("enum __declspec__((...)) E {} d;"); 1635 verifyFormat("enum {\n" 1636 " Bar = Foo<int, int>::value\n" 1637 "};", 1638 getLLVMStyleWithColumns(30)); 1639 1640 verifyFormat("enum ShortEnum { A, B, C };"); 1641 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1642 1643 EXPECT_EQ("enum KeepEmptyLines {\n" 1644 " ONE,\n" 1645 "\n" 1646 " TWO,\n" 1647 "\n" 1648 " THREE\n" 1649 "}", 1650 format("enum KeepEmptyLines {\n" 1651 " ONE,\n" 1652 "\n" 1653 " TWO,\n" 1654 "\n" 1655 "\n" 1656 " THREE\n" 1657 "}")); 1658 verifyFormat("enum E { // comment\n" 1659 " ONE,\n" 1660 " TWO\n" 1661 "};\n" 1662 "int i;"); 1663 // Not enums. 1664 verifyFormat("enum X f() {\n" 1665 " a();\n" 1666 " return 42;\n" 1667 "}"); 1668 verifyFormat("enum X Type::f() {\n" 1669 " a();\n" 1670 " return 42;\n" 1671 "}"); 1672 verifyFormat("enum ::X f() {\n" 1673 " a();\n" 1674 " return 42;\n" 1675 "}"); 1676 verifyFormat("enum ns::X f() {\n" 1677 " a();\n" 1678 " return 42;\n" 1679 "}"); 1680 } 1681 1682 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1683 verifyFormat("enum Type {\n" 1684 " One = 0; // These semicolons should be commas.\n" 1685 " Two = 1;\n" 1686 "};"); 1687 verifyFormat("namespace n {\n" 1688 "enum Type {\n" 1689 " One,\n" 1690 " Two, // missing };\n" 1691 " int i;\n" 1692 "}\n" 1693 "void g() {}"); 1694 } 1695 1696 TEST_F(FormatTest, FormatsEnumStruct) { 1697 verifyFormat("enum struct {\n" 1698 " Zero,\n" 1699 " One = 1,\n" 1700 " Two = One + 1,\n" 1701 " Three = (One + Two),\n" 1702 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1703 " Five = (One, Two, Three, Four, 5)\n" 1704 "};"); 1705 verifyFormat("enum struct Enum {};"); 1706 verifyFormat("enum struct {};"); 1707 verifyFormat("enum struct X E {} d;"); 1708 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1709 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1710 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1711 } 1712 1713 TEST_F(FormatTest, FormatsEnumClass) { 1714 verifyFormat("enum class {\n" 1715 " Zero,\n" 1716 " One = 1,\n" 1717 " Two = One + 1,\n" 1718 " Three = (One + Two),\n" 1719 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1720 " Five = (One, Two, Three, Four, 5)\n" 1721 "};"); 1722 verifyFormat("enum class Enum {};"); 1723 verifyFormat("enum class {};"); 1724 verifyFormat("enum class X E {} d;"); 1725 verifyFormat("enum class __attribute__((...)) E {} d;"); 1726 verifyFormat("enum class __declspec__((...)) E {} d;"); 1727 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1728 } 1729 1730 TEST_F(FormatTest, FormatsEnumTypes) { 1731 verifyFormat("enum X : int {\n" 1732 " A, // Force multiple lines.\n" 1733 " B\n" 1734 "};"); 1735 verifyFormat("enum X : int { A, B };"); 1736 verifyFormat("enum X : std::uint32_t { A, B };"); 1737 } 1738 1739 TEST_F(FormatTest, FormatsTypedefEnum) { 1740 FormatStyle Style = getLLVMStyle(); 1741 Style.ColumnLimit = 40; 1742 verifyFormat("typedef enum {} EmptyEnum;"); 1743 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1744 verifyFormat("typedef enum {\n" 1745 " ZERO = 0,\n" 1746 " ONE = 1,\n" 1747 " TWO = 2,\n" 1748 " THREE = 3\n" 1749 "} LongEnum;", 1750 Style); 1751 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1752 Style.BraceWrapping.AfterEnum = true; 1753 verifyFormat("typedef enum {} EmptyEnum;"); 1754 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1755 verifyFormat("typedef enum\n" 1756 "{\n" 1757 " ZERO = 0,\n" 1758 " ONE = 1,\n" 1759 " TWO = 2,\n" 1760 " THREE = 3\n" 1761 "} LongEnum;", 1762 Style); 1763 } 1764 1765 TEST_F(FormatTest, FormatsNSEnums) { 1766 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1767 verifyGoogleFormat( 1768 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1769 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1770 " // Information about someDecentlyLongValue.\n" 1771 " someDecentlyLongValue,\n" 1772 " // Information about anotherDecentlyLongValue.\n" 1773 " anotherDecentlyLongValue,\n" 1774 " // Information about aThirdDecentlyLongValue.\n" 1775 " aThirdDecentlyLongValue\n" 1776 "};"); 1777 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 1778 " // Information about someDecentlyLongValue.\n" 1779 " someDecentlyLongValue,\n" 1780 " // Information about anotherDecentlyLongValue.\n" 1781 " anotherDecentlyLongValue,\n" 1782 " // Information about aThirdDecentlyLongValue.\n" 1783 " aThirdDecentlyLongValue\n" 1784 "};"); 1785 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1786 " a = 1,\n" 1787 " b = 2,\n" 1788 " c = 3,\n" 1789 "};"); 1790 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1791 " a = 1,\n" 1792 " b = 2,\n" 1793 " c = 3,\n" 1794 "};"); 1795 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 1796 " a = 1,\n" 1797 " b = 2,\n" 1798 " c = 3,\n" 1799 "};"); 1800 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1801 " a = 1,\n" 1802 " b = 2,\n" 1803 " c = 3,\n" 1804 "};"); 1805 } 1806 1807 TEST_F(FormatTest, FormatsBitfields) { 1808 verifyFormat("struct Bitfields {\n" 1809 " unsigned sClass : 8;\n" 1810 " unsigned ValueKind : 2;\n" 1811 "};"); 1812 verifyFormat("struct A {\n" 1813 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1814 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1815 "};"); 1816 verifyFormat("struct MyStruct {\n" 1817 " uchar data;\n" 1818 " uchar : 8;\n" 1819 " uchar : 8;\n" 1820 " uchar other;\n" 1821 "};"); 1822 } 1823 1824 TEST_F(FormatTest, FormatsNamespaces) { 1825 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1826 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1827 1828 verifyFormat("namespace some_namespace {\n" 1829 "class A {};\n" 1830 "void f() { f(); }\n" 1831 "}", 1832 LLVMWithNoNamespaceFix); 1833 verifyFormat("namespace N::inline D {\n" 1834 "class A {};\n" 1835 "void f() { f(); }\n" 1836 "}", 1837 LLVMWithNoNamespaceFix); 1838 verifyFormat("namespace N::inline D::E {\n" 1839 "class A {};\n" 1840 "void f() { f(); }\n" 1841 "}", 1842 LLVMWithNoNamespaceFix); 1843 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 1844 "class A {};\n" 1845 "void f() { f(); }\n" 1846 "}", 1847 LLVMWithNoNamespaceFix); 1848 verifyFormat("/* something */ namespace some_namespace {\n" 1849 "class A {};\n" 1850 "void f() { f(); }\n" 1851 "}", 1852 LLVMWithNoNamespaceFix); 1853 verifyFormat("namespace {\n" 1854 "class A {};\n" 1855 "void f() { f(); }\n" 1856 "}", 1857 LLVMWithNoNamespaceFix); 1858 verifyFormat("/* something */ namespace {\n" 1859 "class A {};\n" 1860 "void f() { f(); }\n" 1861 "}", 1862 LLVMWithNoNamespaceFix); 1863 verifyFormat("inline namespace X {\n" 1864 "class A {};\n" 1865 "void f() { f(); }\n" 1866 "}", 1867 LLVMWithNoNamespaceFix); 1868 verifyFormat("/* something */ inline namespace X {\n" 1869 "class A {};\n" 1870 "void f() { f(); }\n" 1871 "}", 1872 LLVMWithNoNamespaceFix); 1873 verifyFormat("export namespace X {\n" 1874 "class A {};\n" 1875 "void f() { f(); }\n" 1876 "}", 1877 LLVMWithNoNamespaceFix); 1878 verifyFormat("using namespace some_namespace;\n" 1879 "class A {};\n" 1880 "void f() { f(); }", 1881 LLVMWithNoNamespaceFix); 1882 1883 // This code is more common than we thought; if we 1884 // layout this correctly the semicolon will go into 1885 // its own line, which is undesirable. 1886 verifyFormat("namespace {};", 1887 LLVMWithNoNamespaceFix); 1888 verifyFormat("namespace {\n" 1889 "class A {};\n" 1890 "};", 1891 LLVMWithNoNamespaceFix); 1892 1893 verifyFormat("namespace {\n" 1894 "int SomeVariable = 0; // comment\n" 1895 "} // namespace", 1896 LLVMWithNoNamespaceFix); 1897 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1898 "#define HEADER_GUARD\n" 1899 "namespace my_namespace {\n" 1900 "int i;\n" 1901 "} // my_namespace\n" 1902 "#endif // HEADER_GUARD", 1903 format("#ifndef HEADER_GUARD\n" 1904 " #define HEADER_GUARD\n" 1905 " namespace my_namespace {\n" 1906 "int i;\n" 1907 "} // my_namespace\n" 1908 "#endif // HEADER_GUARD", 1909 LLVMWithNoNamespaceFix)); 1910 1911 EXPECT_EQ("namespace A::B {\n" 1912 "class C {};\n" 1913 "}", 1914 format("namespace A::B {\n" 1915 "class C {};\n" 1916 "}", 1917 LLVMWithNoNamespaceFix)); 1918 1919 FormatStyle Style = getLLVMStyle(); 1920 Style.NamespaceIndentation = FormatStyle::NI_All; 1921 EXPECT_EQ("namespace out {\n" 1922 " int i;\n" 1923 " namespace in {\n" 1924 " int i;\n" 1925 " } // namespace in\n" 1926 "} // namespace out", 1927 format("namespace out {\n" 1928 "int i;\n" 1929 "namespace in {\n" 1930 "int i;\n" 1931 "} // namespace in\n" 1932 "} // namespace out", 1933 Style)); 1934 1935 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1936 EXPECT_EQ("namespace out {\n" 1937 "int i;\n" 1938 "namespace in {\n" 1939 " int i;\n" 1940 "} // namespace in\n" 1941 "} // namespace out", 1942 format("namespace out {\n" 1943 "int i;\n" 1944 "namespace in {\n" 1945 "int i;\n" 1946 "} // namespace in\n" 1947 "} // namespace out", 1948 Style)); 1949 } 1950 1951 TEST_F(FormatTest, NamespaceMacros) { 1952 FormatStyle Style = getLLVMStyle(); 1953 Style.NamespaceMacros.push_back("TESTSUITE"); 1954 1955 verifyFormat("TESTSUITE(A) {\n" 1956 "int foo();\n" 1957 "} // TESTSUITE(A)", 1958 Style); 1959 1960 verifyFormat("TESTSUITE(A, B) {\n" 1961 "int foo();\n" 1962 "} // TESTSUITE(A)", 1963 Style); 1964 1965 // Properly indent according to NamespaceIndentation style 1966 Style.NamespaceIndentation = FormatStyle::NI_All; 1967 verifyFormat("TESTSUITE(A) {\n" 1968 " int foo();\n" 1969 "} // TESTSUITE(A)", 1970 Style); 1971 verifyFormat("TESTSUITE(A) {\n" 1972 " namespace B {\n" 1973 " int foo();\n" 1974 " } // namespace B\n" 1975 "} // TESTSUITE(A)", 1976 Style); 1977 verifyFormat("namespace A {\n" 1978 " TESTSUITE(B) {\n" 1979 " int foo();\n" 1980 " } // TESTSUITE(B)\n" 1981 "} // namespace A", 1982 Style); 1983 1984 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1985 verifyFormat("TESTSUITE(A) {\n" 1986 "TESTSUITE(B) {\n" 1987 " int foo();\n" 1988 "} // TESTSUITE(B)\n" 1989 "} // TESTSUITE(A)", 1990 Style); 1991 verifyFormat("TESTSUITE(A) {\n" 1992 "namespace B {\n" 1993 " int foo();\n" 1994 "} // namespace B\n" 1995 "} // TESTSUITE(A)", 1996 Style); 1997 verifyFormat("namespace A {\n" 1998 "TESTSUITE(B) {\n" 1999 " int foo();\n" 2000 "} // TESTSUITE(B)\n" 2001 "} // namespace A", 2002 Style); 2003 2004 // Properly merge namespace-macros blocks in CompactNamespaces mode 2005 Style.NamespaceIndentation = FormatStyle::NI_None; 2006 Style.CompactNamespaces = true; 2007 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 2008 "}} // TESTSUITE(A::B)", 2009 Style); 2010 2011 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 2012 "}} // TESTSUITE(out::in)", 2013 format("TESTSUITE(out) {\n" 2014 "TESTSUITE(in) {\n" 2015 "} // TESTSUITE(in)\n" 2016 "} // TESTSUITE(out)", 2017 Style)); 2018 2019 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 2020 "}} // TESTSUITE(out::in)", 2021 format("TESTSUITE(out) {\n" 2022 "TESTSUITE(in) {\n" 2023 "} // TESTSUITE(in)\n" 2024 "} // TESTSUITE(out)", 2025 Style)); 2026 2027 // Do not merge different namespaces/macros 2028 EXPECT_EQ("namespace out {\n" 2029 "TESTSUITE(in) {\n" 2030 "} // TESTSUITE(in)\n" 2031 "} // namespace out", 2032 format("namespace out {\n" 2033 "TESTSUITE(in) {\n" 2034 "} // TESTSUITE(in)\n" 2035 "} // namespace out", 2036 Style)); 2037 EXPECT_EQ("TESTSUITE(out) {\n" 2038 "namespace in {\n" 2039 "} // namespace in\n" 2040 "} // TESTSUITE(out)", 2041 format("TESTSUITE(out) {\n" 2042 "namespace in {\n" 2043 "} // namespace in\n" 2044 "} // TESTSUITE(out)", 2045 Style)); 2046 Style.NamespaceMacros.push_back("FOOBAR"); 2047 EXPECT_EQ("TESTSUITE(out) {\n" 2048 "FOOBAR(in) {\n" 2049 "} // FOOBAR(in)\n" 2050 "} // TESTSUITE(out)", 2051 format("TESTSUITE(out) {\n" 2052 "FOOBAR(in) {\n" 2053 "} // FOOBAR(in)\n" 2054 "} // TESTSUITE(out)", 2055 Style)); 2056 } 2057 2058 TEST_F(FormatTest, FormatsCompactNamespaces) { 2059 FormatStyle Style = getLLVMStyle(); 2060 Style.CompactNamespaces = true; 2061 Style.NamespaceMacros.push_back("TESTSUITE"); 2062 2063 verifyFormat("namespace A { namespace B {\n" 2064 "}} // namespace A::B", 2065 Style); 2066 2067 EXPECT_EQ("namespace out { namespace in {\n" 2068 "}} // namespace out::in", 2069 format("namespace out {\n" 2070 "namespace in {\n" 2071 "} // namespace in\n" 2072 "} // namespace out", 2073 Style)); 2074 2075 // Only namespaces which have both consecutive opening and end get compacted 2076 EXPECT_EQ("namespace out {\n" 2077 "namespace in1 {\n" 2078 "} // namespace in1\n" 2079 "namespace in2 {\n" 2080 "} // namespace in2\n" 2081 "} // namespace out", 2082 format("namespace out {\n" 2083 "namespace in1 {\n" 2084 "} // namespace in1\n" 2085 "namespace in2 {\n" 2086 "} // namespace in2\n" 2087 "} // namespace out", 2088 Style)); 2089 2090 EXPECT_EQ("namespace out {\n" 2091 "int i;\n" 2092 "namespace in {\n" 2093 "int j;\n" 2094 "} // namespace in\n" 2095 "int k;\n" 2096 "} // namespace out", 2097 format("namespace out { int i;\n" 2098 "namespace in { int j; } // namespace in\n" 2099 "int k; } // namespace out", 2100 Style)); 2101 2102 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 2103 "}}} // namespace A::B::C\n", 2104 format("namespace A { namespace B {\n" 2105 "namespace C {\n" 2106 "}} // namespace B::C\n" 2107 "} // namespace A\n", 2108 Style)); 2109 2110 Style.ColumnLimit = 40; 2111 EXPECT_EQ("namespace aaaaaaaaaa {\n" 2112 "namespace bbbbbbbbbb {\n" 2113 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 2114 format("namespace aaaaaaaaaa {\n" 2115 "namespace bbbbbbbbbb {\n" 2116 "} // namespace bbbbbbbbbb\n" 2117 "} // namespace aaaaaaaaaa", 2118 Style)); 2119 2120 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 2121 "namespace cccccc {\n" 2122 "}}} // namespace aaaaaa::bbbbbb::cccccc", 2123 format("namespace aaaaaa {\n" 2124 "namespace bbbbbb {\n" 2125 "namespace cccccc {\n" 2126 "} // namespace cccccc\n" 2127 "} // namespace bbbbbb\n" 2128 "} // namespace aaaaaa", 2129 Style)); 2130 Style.ColumnLimit = 80; 2131 2132 // Extra semicolon after 'inner' closing brace prevents merging 2133 EXPECT_EQ("namespace out { namespace in {\n" 2134 "}; } // namespace out::in", 2135 format("namespace out {\n" 2136 "namespace in {\n" 2137 "}; // namespace in\n" 2138 "} // namespace out", 2139 Style)); 2140 2141 // Extra semicolon after 'outer' closing brace is conserved 2142 EXPECT_EQ("namespace out { namespace in {\n" 2143 "}}; // namespace out::in", 2144 format("namespace out {\n" 2145 "namespace in {\n" 2146 "} // namespace in\n" 2147 "}; // namespace out", 2148 Style)); 2149 2150 Style.NamespaceIndentation = FormatStyle::NI_All; 2151 EXPECT_EQ("namespace out { namespace in {\n" 2152 " int i;\n" 2153 "}} // namespace out::in", 2154 format("namespace out {\n" 2155 "namespace in {\n" 2156 "int i;\n" 2157 "} // namespace in\n" 2158 "} // namespace out", 2159 Style)); 2160 EXPECT_EQ("namespace out { namespace mid {\n" 2161 " namespace in {\n" 2162 " int j;\n" 2163 " } // namespace in\n" 2164 " int k;\n" 2165 "}} // namespace out::mid", 2166 format("namespace out { namespace mid {\n" 2167 "namespace in { int j; } // namespace in\n" 2168 "int k; }} // namespace out::mid", 2169 Style)); 2170 2171 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2172 EXPECT_EQ("namespace out { namespace in {\n" 2173 " int i;\n" 2174 "}} // namespace out::in", 2175 format("namespace out {\n" 2176 "namespace in {\n" 2177 "int i;\n" 2178 "} // namespace in\n" 2179 "} // namespace out", 2180 Style)); 2181 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 2182 " int i;\n" 2183 "}}} // namespace out::mid::in", 2184 format("namespace out {\n" 2185 "namespace mid {\n" 2186 "namespace in {\n" 2187 "int i;\n" 2188 "} // namespace in\n" 2189 "} // namespace mid\n" 2190 "} // namespace out", 2191 Style)); 2192 } 2193 2194 TEST_F(FormatTest, FormatsExternC) { 2195 verifyFormat("extern \"C\" {\nint a;"); 2196 verifyFormat("extern \"C\" {}"); 2197 verifyFormat("extern \"C\" {\n" 2198 "int foo();\n" 2199 "}"); 2200 verifyFormat("extern \"C\" int foo() {}"); 2201 verifyFormat("extern \"C\" int foo();"); 2202 verifyFormat("extern \"C\" int foo() {\n" 2203 " int i = 42;\n" 2204 " return i;\n" 2205 "}"); 2206 2207 FormatStyle Style = getLLVMStyle(); 2208 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2209 Style.BraceWrapping.AfterFunction = true; 2210 verifyFormat("extern \"C\" int foo() {}", Style); 2211 verifyFormat("extern \"C\" int foo();", Style); 2212 verifyFormat("extern \"C\" int foo()\n" 2213 "{\n" 2214 " int i = 42;\n" 2215 " return i;\n" 2216 "}", 2217 Style); 2218 2219 Style.BraceWrapping.AfterExternBlock = true; 2220 Style.BraceWrapping.SplitEmptyRecord = false; 2221 verifyFormat("extern \"C\"\n" 2222 "{}", 2223 Style); 2224 verifyFormat("extern \"C\"\n" 2225 "{\n" 2226 " int foo();\n" 2227 "}", 2228 Style); 2229 } 2230 2231 TEST_F(FormatTest, FormatsInlineASM) { 2232 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2233 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2234 verifyFormat( 2235 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 2236 " \"cpuid\\n\\t\"\n" 2237 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 2238 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 2239 " : \"a\"(value));"); 2240 EXPECT_EQ( 2241 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2242 " __asm {\n" 2243 " mov edx,[that] // vtable in edx\n" 2244 " mov eax,methodIndex\n" 2245 " call [edx][eax*4] // stdcall\n" 2246 " }\n" 2247 "}", 2248 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2249 " __asm {\n" 2250 " mov edx,[that] // vtable in edx\n" 2251 " mov eax,methodIndex\n" 2252 " call [edx][eax*4] // stdcall\n" 2253 " }\n" 2254 "}")); 2255 EXPECT_EQ("_asm {\n" 2256 " xor eax, eax;\n" 2257 " cpuid;\n" 2258 "}", 2259 format("_asm {\n" 2260 " xor eax, eax;\n" 2261 " cpuid;\n" 2262 "}")); 2263 verifyFormat("void function() {\n" 2264 " // comment\n" 2265 " asm(\"\");\n" 2266 "}"); 2267 EXPECT_EQ("__asm {\n" 2268 "}\n" 2269 "int i;", 2270 format("__asm {\n" 2271 "}\n" 2272 "int i;")); 2273 } 2274 2275 TEST_F(FormatTest, FormatTryCatch) { 2276 verifyFormat("try {\n" 2277 " throw a * b;\n" 2278 "} catch (int a) {\n" 2279 " // Do nothing.\n" 2280 "} catch (...) {\n" 2281 " exit(42);\n" 2282 "}"); 2283 2284 // Function-level try statements. 2285 verifyFormat("int f() try { return 4; } catch (...) {\n" 2286 " return 5;\n" 2287 "}"); 2288 verifyFormat("class A {\n" 2289 " int a;\n" 2290 " A() try : a(0) {\n" 2291 " } catch (...) {\n" 2292 " throw;\n" 2293 " }\n" 2294 "};\n"); 2295 2296 // Incomplete try-catch blocks. 2297 verifyIncompleteFormat("try {} catch ("); 2298 } 2299 2300 TEST_F(FormatTest, FormatSEHTryCatch) { 2301 verifyFormat("__try {\n" 2302 " int a = b * c;\n" 2303 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2304 " // Do nothing.\n" 2305 "}"); 2306 2307 verifyFormat("__try {\n" 2308 " int a = b * c;\n" 2309 "} __finally {\n" 2310 " // Do nothing.\n" 2311 "}"); 2312 2313 verifyFormat("DEBUG({\n" 2314 " __try {\n" 2315 " } __finally {\n" 2316 " }\n" 2317 "});\n"); 2318 } 2319 2320 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2321 verifyFormat("try {\n" 2322 " f();\n" 2323 "} catch {\n" 2324 " g();\n" 2325 "}"); 2326 verifyFormat("try {\n" 2327 " f();\n" 2328 "} catch (A a) MACRO(x) {\n" 2329 " g();\n" 2330 "} catch (B b) MACRO(x) {\n" 2331 " g();\n" 2332 "}"); 2333 } 2334 2335 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2336 FormatStyle Style = getLLVMStyle(); 2337 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 2338 FormatStyle::BS_WebKit}) { 2339 Style.BreakBeforeBraces = BraceStyle; 2340 verifyFormat("try {\n" 2341 " // something\n" 2342 "} catch (...) {\n" 2343 " // something\n" 2344 "}", 2345 Style); 2346 } 2347 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2348 verifyFormat("try {\n" 2349 " // something\n" 2350 "}\n" 2351 "catch (...) {\n" 2352 " // something\n" 2353 "}", 2354 Style); 2355 verifyFormat("__try {\n" 2356 " // something\n" 2357 "}\n" 2358 "__finally {\n" 2359 " // something\n" 2360 "}", 2361 Style); 2362 verifyFormat("@try {\n" 2363 " // something\n" 2364 "}\n" 2365 "@finally {\n" 2366 " // something\n" 2367 "}", 2368 Style); 2369 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2370 verifyFormat("try\n" 2371 "{\n" 2372 " // something\n" 2373 "}\n" 2374 "catch (...)\n" 2375 "{\n" 2376 " // something\n" 2377 "}", 2378 Style); 2379 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2380 verifyFormat("try\n" 2381 " {\n" 2382 " // something\n" 2383 " }\n" 2384 "catch (...)\n" 2385 " {\n" 2386 " // something\n" 2387 " }", 2388 Style); 2389 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2390 Style.BraceWrapping.BeforeCatch = true; 2391 verifyFormat("try {\n" 2392 " // something\n" 2393 "}\n" 2394 "catch (...) {\n" 2395 " // something\n" 2396 "}", 2397 Style); 2398 } 2399 2400 TEST_F(FormatTest, StaticInitializers) { 2401 verifyFormat("static SomeClass SC = {1, 'a'};"); 2402 2403 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2404 " 100000000, " 2405 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2406 2407 // Here, everything other than the "}" would fit on a line. 2408 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2409 " 10000000000000000000000000};"); 2410 EXPECT_EQ("S s = {a,\n" 2411 "\n" 2412 " b};", 2413 format("S s = {\n" 2414 " a,\n" 2415 "\n" 2416 " b\n" 2417 "};")); 2418 2419 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2420 // line. However, the formatting looks a bit off and this probably doesn't 2421 // happen often in practice. 2422 verifyFormat("static int Variable[1] = {\n" 2423 " {1000000000000000000000000000000000000}};", 2424 getLLVMStyleWithColumns(40)); 2425 } 2426 2427 TEST_F(FormatTest, DesignatedInitializers) { 2428 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2429 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2430 " .bbbbbbbbbb = 2,\n" 2431 " .cccccccccc = 3,\n" 2432 " .dddddddddd = 4,\n" 2433 " .eeeeeeeeee = 5};"); 2434 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2435 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2436 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2437 " .ccccccccccccccccccccccccccc = 3,\n" 2438 " .ddddddddddddddddddddddddddd = 4,\n" 2439 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2440 2441 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2442 2443 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 2444 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 2445 " [2] = bbbbbbbbbb,\n" 2446 " [3] = cccccccccc,\n" 2447 " [4] = dddddddddd,\n" 2448 " [5] = eeeeeeeeee};"); 2449 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2450 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 2451 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2452 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 2453 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 2454 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 2455 } 2456 2457 TEST_F(FormatTest, NestedStaticInitializers) { 2458 verifyFormat("static A x = {{{}}};\n"); 2459 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2460 " {init1, init2, init3, init4}}};", 2461 getLLVMStyleWithColumns(50)); 2462 2463 verifyFormat("somes Status::global_reps[3] = {\n" 2464 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2465 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2466 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2467 getLLVMStyleWithColumns(60)); 2468 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2469 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2470 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2471 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2472 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2473 " {rect.fRight - rect.fLeft, rect.fBottom - " 2474 "rect.fTop}};"); 2475 2476 verifyFormat( 2477 "SomeArrayOfSomeType a = {\n" 2478 " {{1, 2, 3},\n" 2479 " {1, 2, 3},\n" 2480 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2481 " 333333333333333333333333333333},\n" 2482 " {1, 2, 3},\n" 2483 " {1, 2, 3}}};"); 2484 verifyFormat( 2485 "SomeArrayOfSomeType a = {\n" 2486 " {{1, 2, 3}},\n" 2487 " {{1, 2, 3}},\n" 2488 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2489 " 333333333333333333333333333333}},\n" 2490 " {{1, 2, 3}},\n" 2491 " {{1, 2, 3}}};"); 2492 2493 verifyFormat("struct {\n" 2494 " unsigned bit;\n" 2495 " const char *const name;\n" 2496 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2497 " {kOsWin, \"Windows\"},\n" 2498 " {kOsLinux, \"Linux\"},\n" 2499 " {kOsCrOS, \"Chrome OS\"}};"); 2500 verifyFormat("struct {\n" 2501 " unsigned bit;\n" 2502 " const char *const name;\n" 2503 "} kBitsToOs[] = {\n" 2504 " {kOsMac, \"Mac\"},\n" 2505 " {kOsWin, \"Windows\"},\n" 2506 " {kOsLinux, \"Linux\"},\n" 2507 " {kOsCrOS, \"Chrome OS\"},\n" 2508 "};"); 2509 } 2510 2511 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2512 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2513 " \\\n" 2514 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2515 } 2516 2517 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2518 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2519 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2520 2521 // Do break defaulted and deleted functions. 2522 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2523 " default;", 2524 getLLVMStyleWithColumns(40)); 2525 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2526 " delete;", 2527 getLLVMStyleWithColumns(40)); 2528 } 2529 2530 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2531 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2532 getLLVMStyleWithColumns(40)); 2533 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2534 getLLVMStyleWithColumns(40)); 2535 EXPECT_EQ("#define Q \\\n" 2536 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2537 " \"aaaaaaaa.cpp\"", 2538 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2539 getLLVMStyleWithColumns(40))); 2540 } 2541 2542 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2543 EXPECT_EQ("# 123 \"A string literal\"", 2544 format(" # 123 \"A string literal\"")); 2545 } 2546 2547 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2548 EXPECT_EQ("#;", format("#;")); 2549 verifyFormat("#\n;\n;\n;"); 2550 } 2551 2552 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2553 EXPECT_EQ("#line 42 \"test\"\n", 2554 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2555 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2556 getLLVMStyleWithColumns(12))); 2557 } 2558 2559 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2560 EXPECT_EQ("#line 42 \"test\"", 2561 format("# \\\n line \\\n 42 \\\n \"test\"")); 2562 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2563 } 2564 2565 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2566 verifyFormat("#define A \\x20"); 2567 verifyFormat("#define A \\ x20"); 2568 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2569 verifyFormat("#define A ''"); 2570 verifyFormat("#define A ''qqq"); 2571 verifyFormat("#define A `qqq"); 2572 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2573 EXPECT_EQ("const char *c = STRINGIFY(\n" 2574 "\\na : b);", 2575 format("const char * c = STRINGIFY(\n" 2576 "\\na : b);")); 2577 2578 verifyFormat("a\r\\"); 2579 verifyFormat("a\v\\"); 2580 verifyFormat("a\f\\"); 2581 } 2582 2583 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2584 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2585 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2586 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2587 // FIXME: We never break before the macro name. 2588 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2589 2590 verifyFormat("#define A A\n#define A A"); 2591 verifyFormat("#define A(X) A\n#define A A"); 2592 2593 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2594 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2595 } 2596 2597 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2598 EXPECT_EQ("// somecomment\n" 2599 "#include \"a.h\"\n" 2600 "#define A( \\\n" 2601 " A, B)\n" 2602 "#include \"b.h\"\n" 2603 "// somecomment\n", 2604 format(" // somecomment\n" 2605 " #include \"a.h\"\n" 2606 "#define A(A,\\\n" 2607 " B)\n" 2608 " #include \"b.h\"\n" 2609 " // somecomment\n", 2610 getLLVMStyleWithColumns(13))); 2611 } 2612 2613 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2614 2615 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2616 EXPECT_EQ("#define A \\\n" 2617 " c; \\\n" 2618 " e;\n" 2619 "f;", 2620 format("#define A c; e;\n" 2621 "f;", 2622 getLLVMStyleWithColumns(14))); 2623 } 2624 2625 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2626 2627 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2628 EXPECT_EQ("int x,\n" 2629 "#define A\n" 2630 " y;", 2631 format("int x,\n#define A\ny;")); 2632 } 2633 2634 TEST_F(FormatTest, HashInMacroDefinition) { 2635 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2636 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2637 verifyFormat("#define A \\\n" 2638 " { \\\n" 2639 " f(#c); \\\n" 2640 " }", 2641 getLLVMStyleWithColumns(11)); 2642 2643 verifyFormat("#define A(X) \\\n" 2644 " void function##X()", 2645 getLLVMStyleWithColumns(22)); 2646 2647 verifyFormat("#define A(a, b, c) \\\n" 2648 " void a##b##c()", 2649 getLLVMStyleWithColumns(22)); 2650 2651 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2652 } 2653 2654 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2655 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2656 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2657 2658 FormatStyle Style = getLLVMStyle(); 2659 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 2660 verifyFormat("#define true ((foo)1)", Style); 2661 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 2662 verifyFormat("#define false((foo)0)", Style); 2663 } 2664 2665 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2666 EXPECT_EQ("#define A b;", format("#define A \\\n" 2667 " \\\n" 2668 " b;", 2669 getLLVMStyleWithColumns(25))); 2670 EXPECT_EQ("#define A \\\n" 2671 " \\\n" 2672 " a; \\\n" 2673 " b;", 2674 format("#define A \\\n" 2675 " \\\n" 2676 " a; \\\n" 2677 " b;", 2678 getLLVMStyleWithColumns(11))); 2679 EXPECT_EQ("#define A \\\n" 2680 " a; \\\n" 2681 " \\\n" 2682 " b;", 2683 format("#define A \\\n" 2684 " a; \\\n" 2685 " \\\n" 2686 " b;", 2687 getLLVMStyleWithColumns(11))); 2688 } 2689 2690 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2691 verifyIncompleteFormat("#define A :"); 2692 verifyFormat("#define SOMECASES \\\n" 2693 " case 1: \\\n" 2694 " case 2\n", 2695 getLLVMStyleWithColumns(20)); 2696 verifyFormat("#define MACRO(a) \\\n" 2697 " if (a) \\\n" 2698 " f(); \\\n" 2699 " else \\\n" 2700 " g()", 2701 getLLVMStyleWithColumns(18)); 2702 verifyFormat("#define A template <typename T>"); 2703 verifyIncompleteFormat("#define STR(x) #x\n" 2704 "f(STR(this_is_a_string_literal{));"); 2705 verifyFormat("#pragma omp threadprivate( \\\n" 2706 " y)), // expected-warning", 2707 getLLVMStyleWithColumns(28)); 2708 verifyFormat("#d, = };"); 2709 verifyFormat("#if \"a"); 2710 verifyIncompleteFormat("({\n" 2711 "#define b \\\n" 2712 " } \\\n" 2713 " a\n" 2714 "a", 2715 getLLVMStyleWithColumns(15)); 2716 verifyFormat("#define A \\\n" 2717 " { \\\n" 2718 " {\n" 2719 "#define B \\\n" 2720 " } \\\n" 2721 " }", 2722 getLLVMStyleWithColumns(15)); 2723 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2724 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2725 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2726 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2727 } 2728 2729 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2730 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2731 EXPECT_EQ("class A : public QObject {\n" 2732 " Q_OBJECT\n" 2733 "\n" 2734 " A() {}\n" 2735 "};", 2736 format("class A : public QObject {\n" 2737 " Q_OBJECT\n" 2738 "\n" 2739 " A() {\n}\n" 2740 "} ;")); 2741 EXPECT_EQ("MACRO\n" 2742 "/*static*/ int i;", 2743 format("MACRO\n" 2744 " /*static*/ int i;")); 2745 EXPECT_EQ("SOME_MACRO\n" 2746 "namespace {\n" 2747 "void f();\n" 2748 "} // namespace", 2749 format("SOME_MACRO\n" 2750 " namespace {\n" 2751 "void f( );\n" 2752 "} // namespace")); 2753 // Only if the identifier contains at least 5 characters. 2754 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2755 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2756 // Only if everything is upper case. 2757 EXPECT_EQ("class A : public QObject {\n" 2758 " Q_Object A() {}\n" 2759 "};", 2760 format("class A : public QObject {\n" 2761 " Q_Object\n" 2762 " A() {\n}\n" 2763 "} ;")); 2764 2765 // Only if the next line can actually start an unwrapped line. 2766 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2767 format("SOME_WEIRD_LOG_MACRO\n" 2768 "<< SomeThing;")); 2769 2770 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2771 "(n, buffers))\n", 2772 getChromiumStyle(FormatStyle::LK_Cpp)); 2773 2774 // See PR41483 2775 EXPECT_EQ("/**/ FOO(a)\n" 2776 "FOO(b)", 2777 format("/**/ FOO(a)\n" 2778 "FOO(b)")); 2779 } 2780 2781 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2782 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2783 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2784 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2785 "class X {};\n" 2786 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2787 "int *createScopDetectionPass() { return 0; }", 2788 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2789 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2790 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2791 " class X {};\n" 2792 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2793 " int *createScopDetectionPass() { return 0; }")); 2794 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2795 // braces, so that inner block is indented one level more. 2796 EXPECT_EQ("int q() {\n" 2797 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2798 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2799 " IPC_END_MESSAGE_MAP()\n" 2800 "}", 2801 format("int q() {\n" 2802 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2803 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2804 " IPC_END_MESSAGE_MAP()\n" 2805 "}")); 2806 2807 // Same inside macros. 2808 EXPECT_EQ("#define LIST(L) \\\n" 2809 " L(A) \\\n" 2810 " L(B) \\\n" 2811 " L(C)", 2812 format("#define LIST(L) \\\n" 2813 " L(A) \\\n" 2814 " L(B) \\\n" 2815 " L(C)", 2816 getGoogleStyle())); 2817 2818 // These must not be recognized as macros. 2819 EXPECT_EQ("int q() {\n" 2820 " f(x);\n" 2821 " f(x) {}\n" 2822 " f(x)->g();\n" 2823 " f(x)->*g();\n" 2824 " f(x).g();\n" 2825 " f(x) = x;\n" 2826 " f(x) += x;\n" 2827 " f(x) -= x;\n" 2828 " f(x) *= x;\n" 2829 " f(x) /= x;\n" 2830 " f(x) %= x;\n" 2831 " f(x) &= x;\n" 2832 " f(x) |= x;\n" 2833 " f(x) ^= x;\n" 2834 " f(x) >>= x;\n" 2835 " f(x) <<= x;\n" 2836 " f(x)[y].z();\n" 2837 " LOG(INFO) << x;\n" 2838 " ifstream(x) >> x;\n" 2839 "}\n", 2840 format("int q() {\n" 2841 " f(x)\n;\n" 2842 " f(x)\n {}\n" 2843 " f(x)\n->g();\n" 2844 " f(x)\n->*g();\n" 2845 " f(x)\n.g();\n" 2846 " f(x)\n = x;\n" 2847 " f(x)\n += x;\n" 2848 " f(x)\n -= x;\n" 2849 " f(x)\n *= x;\n" 2850 " f(x)\n /= x;\n" 2851 " f(x)\n %= x;\n" 2852 " f(x)\n &= x;\n" 2853 " f(x)\n |= x;\n" 2854 " f(x)\n ^= x;\n" 2855 " f(x)\n >>= x;\n" 2856 " f(x)\n <<= x;\n" 2857 " f(x)\n[y].z();\n" 2858 " LOG(INFO)\n << x;\n" 2859 " ifstream(x)\n >> x;\n" 2860 "}\n")); 2861 EXPECT_EQ("int q() {\n" 2862 " F(x)\n" 2863 " if (1) {\n" 2864 " }\n" 2865 " F(x)\n" 2866 " while (1) {\n" 2867 " }\n" 2868 " F(x)\n" 2869 " G(x);\n" 2870 " F(x)\n" 2871 " try {\n" 2872 " Q();\n" 2873 " } catch (...) {\n" 2874 " }\n" 2875 "}\n", 2876 format("int q() {\n" 2877 "F(x)\n" 2878 "if (1) {}\n" 2879 "F(x)\n" 2880 "while (1) {}\n" 2881 "F(x)\n" 2882 "G(x);\n" 2883 "F(x)\n" 2884 "try { Q(); } catch (...) {}\n" 2885 "}\n")); 2886 EXPECT_EQ("class A {\n" 2887 " A() : t(0) {}\n" 2888 " A(int i) noexcept() : {}\n" 2889 " A(X x)\n" // FIXME: function-level try blocks are broken. 2890 " try : t(0) {\n" 2891 " } catch (...) {\n" 2892 " }\n" 2893 "};", 2894 format("class A {\n" 2895 " A()\n : t(0) {}\n" 2896 " A(int i)\n noexcept() : {}\n" 2897 " A(X x)\n" 2898 " try : t(0) {} catch (...) {}\n" 2899 "};")); 2900 FormatStyle Style = getLLVMStyle(); 2901 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2902 Style.BraceWrapping.AfterControlStatement = true; 2903 Style.BraceWrapping.AfterFunction = true; 2904 EXPECT_EQ("void f()\n" 2905 "try\n" 2906 "{\n" 2907 "}", 2908 format("void f() try {\n" 2909 "}", Style)); 2910 EXPECT_EQ("class SomeClass {\n" 2911 "public:\n" 2912 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2913 "};", 2914 format("class SomeClass {\n" 2915 "public:\n" 2916 " SomeClass()\n" 2917 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2918 "};")); 2919 EXPECT_EQ("class SomeClass {\n" 2920 "public:\n" 2921 " SomeClass()\n" 2922 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2923 "};", 2924 format("class SomeClass {\n" 2925 "public:\n" 2926 " SomeClass()\n" 2927 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2928 "};", 2929 getLLVMStyleWithColumns(40))); 2930 2931 verifyFormat("MACRO(>)"); 2932 2933 // Some macros contain an implicit semicolon. 2934 Style = getLLVMStyle(); 2935 Style.StatementMacros.push_back("FOO"); 2936 verifyFormat("FOO(a) int b = 0;"); 2937 verifyFormat("FOO(a)\n" 2938 "int b = 0;", 2939 Style); 2940 verifyFormat("FOO(a);\n" 2941 "int b = 0;", 2942 Style); 2943 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 2944 "int b = 0;", 2945 Style); 2946 verifyFormat("FOO()\n" 2947 "int b = 0;", 2948 Style); 2949 verifyFormat("FOO\n" 2950 "int b = 0;", 2951 Style); 2952 verifyFormat("void f() {\n" 2953 " FOO(a)\n" 2954 " return a;\n" 2955 "}", 2956 Style); 2957 verifyFormat("FOO(a)\n" 2958 "FOO(b)", 2959 Style); 2960 verifyFormat("int a = 0;\n" 2961 "FOO(b)\n" 2962 "int c = 0;", 2963 Style); 2964 verifyFormat("int a = 0;\n" 2965 "int x = FOO(a)\n" 2966 "int b = 0;", 2967 Style); 2968 verifyFormat("void foo(int a) { FOO(a) }\n" 2969 "uint32_t bar() {}", 2970 Style); 2971 } 2972 2973 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2974 verifyFormat("#define A \\\n" 2975 " f({ \\\n" 2976 " g(); \\\n" 2977 " });", 2978 getLLVMStyleWithColumns(11)); 2979 } 2980 2981 TEST_F(FormatTest, IndentPreprocessorDirectives) { 2982 FormatStyle Style = getLLVMStyle(); 2983 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 2984 Style.ColumnLimit = 40; 2985 verifyFormat("#ifdef _WIN32\n" 2986 "#define A 0\n" 2987 "#ifdef VAR2\n" 2988 "#define B 1\n" 2989 "#include <someheader.h>\n" 2990 "#define MACRO \\\n" 2991 " some_very_long_func_aaaaaaaaaa();\n" 2992 "#endif\n" 2993 "#else\n" 2994 "#define A 1\n" 2995 "#endif", 2996 Style); 2997 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 2998 verifyFormat("#ifdef _WIN32\n" 2999 "# define A 0\n" 3000 "# ifdef VAR2\n" 3001 "# define B 1\n" 3002 "# include <someheader.h>\n" 3003 "# define MACRO \\\n" 3004 " some_very_long_func_aaaaaaaaaa();\n" 3005 "# endif\n" 3006 "#else\n" 3007 "# define A 1\n" 3008 "#endif", 3009 Style); 3010 verifyFormat("#if A\n" 3011 "# define MACRO \\\n" 3012 " void a(int x) { \\\n" 3013 " b(); \\\n" 3014 " c(); \\\n" 3015 " d(); \\\n" 3016 " e(); \\\n" 3017 " f(); \\\n" 3018 " }\n" 3019 "#endif", 3020 Style); 3021 // Comments before include guard. 3022 verifyFormat("// file comment\n" 3023 "// file comment\n" 3024 "#ifndef HEADER_H\n" 3025 "#define HEADER_H\n" 3026 "code();\n" 3027 "#endif", 3028 Style); 3029 // Test with include guards. 3030 verifyFormat("#ifndef HEADER_H\n" 3031 "#define HEADER_H\n" 3032 "code();\n" 3033 "#endif", 3034 Style); 3035 // Include guards must have a #define with the same variable immediately 3036 // after #ifndef. 3037 verifyFormat("#ifndef NOT_GUARD\n" 3038 "# define FOO\n" 3039 "code();\n" 3040 "#endif", 3041 Style); 3042 3043 // Include guards must cover the entire file. 3044 verifyFormat("code();\n" 3045 "code();\n" 3046 "#ifndef NOT_GUARD\n" 3047 "# define NOT_GUARD\n" 3048 "code();\n" 3049 "#endif", 3050 Style); 3051 verifyFormat("#ifndef NOT_GUARD\n" 3052 "# define NOT_GUARD\n" 3053 "code();\n" 3054 "#endif\n" 3055 "code();", 3056 Style); 3057 // Test with trailing blank lines. 3058 verifyFormat("#ifndef HEADER_H\n" 3059 "#define HEADER_H\n" 3060 "code();\n" 3061 "#endif\n", 3062 Style); 3063 // Include guards don't have #else. 3064 verifyFormat("#ifndef NOT_GUARD\n" 3065 "# define NOT_GUARD\n" 3066 "code();\n" 3067 "#else\n" 3068 "#endif", 3069 Style); 3070 verifyFormat("#ifndef NOT_GUARD\n" 3071 "# define NOT_GUARD\n" 3072 "code();\n" 3073 "#elif FOO\n" 3074 "#endif", 3075 Style); 3076 // Non-identifier #define after potential include guard. 3077 verifyFormat("#ifndef FOO\n" 3078 "# define 1\n" 3079 "#endif\n", 3080 Style); 3081 // #if closes past last non-preprocessor line. 3082 verifyFormat("#ifndef FOO\n" 3083 "#define FOO\n" 3084 "#if 1\n" 3085 "int i;\n" 3086 "# define A 0\n" 3087 "#endif\n" 3088 "#endif\n", 3089 Style); 3090 // Don't crash if there is an #elif directive without a condition. 3091 verifyFormat("#if 1\n" 3092 "int x;\n" 3093 "#elif\n" 3094 "int y;\n" 3095 "#else\n" 3096 "int z;\n" 3097 "#endif", 3098 Style); 3099 // FIXME: This doesn't handle the case where there's code between the 3100 // #ifndef and #define but all other conditions hold. This is because when 3101 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 3102 // previous code line yet, so we can't detect it. 3103 EXPECT_EQ("#ifndef NOT_GUARD\n" 3104 "code();\n" 3105 "#define NOT_GUARD\n" 3106 "code();\n" 3107 "#endif", 3108 format("#ifndef NOT_GUARD\n" 3109 "code();\n" 3110 "# define NOT_GUARD\n" 3111 "code();\n" 3112 "#endif", 3113 Style)); 3114 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 3115 // be outside an include guard. Examples are #pragma once and 3116 // #pragma GCC diagnostic, or anything else that does not change the meaning 3117 // of the file if it's included multiple times. 3118 EXPECT_EQ("#ifdef WIN32\n" 3119 "# pragma once\n" 3120 "#endif\n" 3121 "#ifndef HEADER_H\n" 3122 "# define HEADER_H\n" 3123 "code();\n" 3124 "#endif", 3125 format("#ifdef WIN32\n" 3126 "# pragma once\n" 3127 "#endif\n" 3128 "#ifndef HEADER_H\n" 3129 "#define HEADER_H\n" 3130 "code();\n" 3131 "#endif", 3132 Style)); 3133 // FIXME: This does not detect when there is a single non-preprocessor line 3134 // in front of an include-guard-like structure where other conditions hold 3135 // because ScopedLineState hides the line. 3136 EXPECT_EQ("code();\n" 3137 "#ifndef HEADER_H\n" 3138 "#define HEADER_H\n" 3139 "code();\n" 3140 "#endif", 3141 format("code();\n" 3142 "#ifndef HEADER_H\n" 3143 "# define HEADER_H\n" 3144 "code();\n" 3145 "#endif", 3146 Style)); 3147 // Keep comments aligned with #, otherwise indent comments normally. These 3148 // tests cannot use verifyFormat because messUp manipulates leading 3149 // whitespace. 3150 { 3151 const char *Expected = "" 3152 "void f() {\n" 3153 "#if 1\n" 3154 "// Preprocessor aligned.\n" 3155 "# define A 0\n" 3156 " // Code. Separated by blank line.\n" 3157 "\n" 3158 "# define B 0\n" 3159 " // Code. Not aligned with #\n" 3160 "# define C 0\n" 3161 "#endif"; 3162 const char *ToFormat = "" 3163 "void f() {\n" 3164 "#if 1\n" 3165 "// Preprocessor aligned.\n" 3166 "# define A 0\n" 3167 "// Code. Separated by blank line.\n" 3168 "\n" 3169 "# define B 0\n" 3170 " // Code. Not aligned with #\n" 3171 "# define C 0\n" 3172 "#endif"; 3173 EXPECT_EQ(Expected, format(ToFormat, Style)); 3174 EXPECT_EQ(Expected, format(Expected, Style)); 3175 } 3176 // Keep block quotes aligned. 3177 { 3178 const char *Expected = "" 3179 "void f() {\n" 3180 "#if 1\n" 3181 "/* Preprocessor aligned. */\n" 3182 "# define A 0\n" 3183 " /* Code. Separated by blank line. */\n" 3184 "\n" 3185 "# define B 0\n" 3186 " /* Code. Not aligned with # */\n" 3187 "# define C 0\n" 3188 "#endif"; 3189 const char *ToFormat = "" 3190 "void f() {\n" 3191 "#if 1\n" 3192 "/* Preprocessor aligned. */\n" 3193 "# define A 0\n" 3194 "/* Code. Separated by blank line. */\n" 3195 "\n" 3196 "# define B 0\n" 3197 " /* Code. Not aligned with # */\n" 3198 "# define C 0\n" 3199 "#endif"; 3200 EXPECT_EQ(Expected, format(ToFormat, Style)); 3201 EXPECT_EQ(Expected, format(Expected, Style)); 3202 } 3203 // Keep comments aligned with un-indented directives. 3204 { 3205 const char *Expected = "" 3206 "void f() {\n" 3207 "// Preprocessor aligned.\n" 3208 "#define A 0\n" 3209 " // Code. Separated by blank line.\n" 3210 "\n" 3211 "#define B 0\n" 3212 " // Code. Not aligned with #\n" 3213 "#define C 0\n"; 3214 const char *ToFormat = "" 3215 "void f() {\n" 3216 "// Preprocessor aligned.\n" 3217 "#define A 0\n" 3218 "// Code. Separated by blank line.\n" 3219 "\n" 3220 "#define B 0\n" 3221 " // Code. Not aligned with #\n" 3222 "#define C 0\n"; 3223 EXPECT_EQ(Expected, format(ToFormat, Style)); 3224 EXPECT_EQ(Expected, format(Expected, Style)); 3225 } 3226 // Test AfterHash with tabs. 3227 { 3228 FormatStyle Tabbed = Style; 3229 Tabbed.UseTab = FormatStyle::UT_Always; 3230 Tabbed.IndentWidth = 8; 3231 Tabbed.TabWidth = 8; 3232 verifyFormat("#ifdef _WIN32\n" 3233 "#\tdefine A 0\n" 3234 "#\tifdef VAR2\n" 3235 "#\t\tdefine B 1\n" 3236 "#\t\tinclude <someheader.h>\n" 3237 "#\t\tdefine MACRO \\\n" 3238 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 3239 "#\tendif\n" 3240 "#else\n" 3241 "#\tdefine A 1\n" 3242 "#endif", 3243 Tabbed); 3244 } 3245 3246 // Regression test: Multiline-macro inside include guards. 3247 verifyFormat("#ifndef HEADER_H\n" 3248 "#define HEADER_H\n" 3249 "#define A() \\\n" 3250 " int i; \\\n" 3251 " int j;\n" 3252 "#endif // HEADER_H", 3253 getLLVMStyleWithColumns(20)); 3254 3255 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 3256 // Basic before hash indent tests 3257 verifyFormat("#ifdef _WIN32\n" 3258 " #define A 0\n" 3259 " #ifdef VAR2\n" 3260 " #define B 1\n" 3261 " #include <someheader.h>\n" 3262 " #define MACRO \\\n" 3263 " some_very_long_func_aaaaaaaaaa();\n" 3264 " #endif\n" 3265 "#else\n" 3266 " #define A 1\n" 3267 "#endif", 3268 Style); 3269 verifyFormat("#if A\n" 3270 " #define MACRO \\\n" 3271 " void a(int x) { \\\n" 3272 " b(); \\\n" 3273 " c(); \\\n" 3274 " d(); \\\n" 3275 " e(); \\\n" 3276 " f(); \\\n" 3277 " }\n" 3278 "#endif", 3279 Style); 3280 // Keep comments aligned with indented directives. These 3281 // tests cannot use verifyFormat because messUp manipulates leading 3282 // whitespace. 3283 { 3284 const char *Expected = "void f() {\n" 3285 "// Aligned to preprocessor.\n" 3286 "#if 1\n" 3287 " // Aligned to code.\n" 3288 " int a;\n" 3289 " #if 1\n" 3290 " // Aligned to preprocessor.\n" 3291 " #define A 0\n" 3292 " // Aligned to code.\n" 3293 " int b;\n" 3294 " #endif\n" 3295 "#endif\n" 3296 "}"; 3297 const char *ToFormat = "void f() {\n" 3298 "// Aligned to preprocessor.\n" 3299 "#if 1\n" 3300 "// Aligned to code.\n" 3301 "int a;\n" 3302 "#if 1\n" 3303 "// Aligned to preprocessor.\n" 3304 "#define A 0\n" 3305 "// Aligned to code.\n" 3306 "int b;\n" 3307 "#endif\n" 3308 "#endif\n" 3309 "}"; 3310 EXPECT_EQ(Expected, format(ToFormat, Style)); 3311 EXPECT_EQ(Expected, format(Expected, Style)); 3312 } 3313 { 3314 const char *Expected = "void f() {\n" 3315 "/* Aligned to preprocessor. */\n" 3316 "#if 1\n" 3317 " /* Aligned to code. */\n" 3318 " int a;\n" 3319 " #if 1\n" 3320 " /* Aligned to preprocessor. */\n" 3321 " #define A 0\n" 3322 " /* Aligned to code. */\n" 3323 " int b;\n" 3324 " #endif\n" 3325 "#endif\n" 3326 "}"; 3327 const char *ToFormat = "void f() {\n" 3328 "/* Aligned to preprocessor. */\n" 3329 "#if 1\n" 3330 "/* Aligned to code. */\n" 3331 "int a;\n" 3332 "#if 1\n" 3333 "/* Aligned to preprocessor. */\n" 3334 "#define A 0\n" 3335 "/* Aligned to code. */\n" 3336 "int b;\n" 3337 "#endif\n" 3338 "#endif\n" 3339 "}"; 3340 EXPECT_EQ(Expected, format(ToFormat, Style)); 3341 EXPECT_EQ(Expected, format(Expected, Style)); 3342 } 3343 3344 // Test single comment before preprocessor 3345 verifyFormat("// Comment\n" 3346 "\n" 3347 "#if 1\n" 3348 "#endif", 3349 Style); 3350 } 3351 3352 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 3353 verifyFormat("{\n { a #c; }\n}"); 3354 } 3355 3356 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 3357 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 3358 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 3359 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 3360 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 3361 } 3362 3363 TEST_F(FormatTest, EscapedNewlines) { 3364 FormatStyle Narrow = getLLVMStyleWithColumns(11); 3365 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 3366 format("#define A \\\nint i;\\\n int j;", Narrow)); 3367 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 3368 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3369 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 3370 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 3371 3372 FormatStyle AlignLeft = getLLVMStyle(); 3373 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 3374 EXPECT_EQ("#define MACRO(x) \\\n" 3375 "private: \\\n" 3376 " int x(int a);\n", 3377 format("#define MACRO(x) \\\n" 3378 "private: \\\n" 3379 " int x(int a);\n", 3380 AlignLeft)); 3381 3382 // CRLF line endings 3383 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 3384 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 3385 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 3386 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3387 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 3388 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 3389 EXPECT_EQ("#define MACRO(x) \\\r\n" 3390 "private: \\\r\n" 3391 " int x(int a);\r\n", 3392 format("#define MACRO(x) \\\r\n" 3393 "private: \\\r\n" 3394 " int x(int a);\r\n", 3395 AlignLeft)); 3396 3397 FormatStyle DontAlign = getLLVMStyle(); 3398 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 3399 DontAlign.MaxEmptyLinesToKeep = 3; 3400 // FIXME: can't use verifyFormat here because the newline before 3401 // "public:" is not inserted the first time it's reformatted 3402 EXPECT_EQ("#define A \\\n" 3403 " class Foo { \\\n" 3404 " void bar(); \\\n" 3405 "\\\n" 3406 "\\\n" 3407 "\\\n" 3408 " public: \\\n" 3409 " void baz(); \\\n" 3410 " };", 3411 format("#define A \\\n" 3412 " class Foo { \\\n" 3413 " void bar(); \\\n" 3414 "\\\n" 3415 "\\\n" 3416 "\\\n" 3417 " public: \\\n" 3418 " void baz(); \\\n" 3419 " };", 3420 DontAlign)); 3421 } 3422 3423 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3424 verifyFormat("#define A \\\n" 3425 " int v( \\\n" 3426 " a); \\\n" 3427 " int i;", 3428 getLLVMStyleWithColumns(11)); 3429 } 3430 3431 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3432 EXPECT_EQ( 3433 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3434 " \\\n" 3435 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3436 "\n" 3437 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3438 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3439 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3440 "\\\n" 3441 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3442 " \n" 3443 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3444 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3445 } 3446 3447 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3448 EXPECT_EQ("int\n" 3449 "#define A\n" 3450 " a;", 3451 format("int\n#define A\na;")); 3452 verifyFormat("functionCallTo(\n" 3453 " someOtherFunction(\n" 3454 " withSomeParameters, whichInSequence,\n" 3455 " areLongerThanALine(andAnotherCall,\n" 3456 "#define A B\n" 3457 " withMoreParamters,\n" 3458 " whichStronglyInfluenceTheLayout),\n" 3459 " andMoreParameters),\n" 3460 " trailing);", 3461 getLLVMStyleWithColumns(69)); 3462 verifyFormat("Foo::Foo()\n" 3463 "#ifdef BAR\n" 3464 " : baz(0)\n" 3465 "#endif\n" 3466 "{\n" 3467 "}"); 3468 verifyFormat("void f() {\n" 3469 " if (true)\n" 3470 "#ifdef A\n" 3471 " f(42);\n" 3472 " x();\n" 3473 "#else\n" 3474 " g();\n" 3475 " x();\n" 3476 "#endif\n" 3477 "}"); 3478 verifyFormat("void f(param1, param2,\n" 3479 " param3,\n" 3480 "#ifdef A\n" 3481 " param4(param5,\n" 3482 "#ifdef A1\n" 3483 " param6,\n" 3484 "#ifdef A2\n" 3485 " param7),\n" 3486 "#else\n" 3487 " param8),\n" 3488 " param9,\n" 3489 "#endif\n" 3490 " param10,\n" 3491 "#endif\n" 3492 " param11)\n" 3493 "#else\n" 3494 " param12)\n" 3495 "#endif\n" 3496 "{\n" 3497 " x();\n" 3498 "}", 3499 getLLVMStyleWithColumns(28)); 3500 verifyFormat("#if 1\n" 3501 "int i;"); 3502 verifyFormat("#if 1\n" 3503 "#endif\n" 3504 "#if 1\n" 3505 "#else\n" 3506 "#endif\n"); 3507 verifyFormat("DEBUG({\n" 3508 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3510 "});\n" 3511 "#if a\n" 3512 "#else\n" 3513 "#endif"); 3514 3515 verifyIncompleteFormat("void f(\n" 3516 "#if A\n" 3517 ");\n" 3518 "#else\n" 3519 "#endif"); 3520 } 3521 3522 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3523 verifyFormat("#endif\n" 3524 "#if B"); 3525 } 3526 3527 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3528 FormatStyle SingleLine = getLLVMStyle(); 3529 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 3530 verifyFormat("#if 0\n" 3531 "#elif 1\n" 3532 "#endif\n" 3533 "void foo() {\n" 3534 " if (test) foo2();\n" 3535 "}", 3536 SingleLine); 3537 } 3538 3539 TEST_F(FormatTest, LayoutBlockInsideParens) { 3540 verifyFormat("functionCall({ int i; });"); 3541 verifyFormat("functionCall({\n" 3542 " int i;\n" 3543 " int j;\n" 3544 "});"); 3545 verifyFormat("functionCall(\n" 3546 " {\n" 3547 " int i;\n" 3548 " int j;\n" 3549 " },\n" 3550 " aaaa, bbbb, cccc);"); 3551 verifyFormat("functionA(functionB({\n" 3552 " int i;\n" 3553 " int j;\n" 3554 " }),\n" 3555 " aaaa, bbbb, cccc);"); 3556 verifyFormat("functionCall(\n" 3557 " {\n" 3558 " int i;\n" 3559 " int j;\n" 3560 " },\n" 3561 " aaaa, bbbb, // comment\n" 3562 " cccc);"); 3563 verifyFormat("functionA(functionB({\n" 3564 " int i;\n" 3565 " int j;\n" 3566 " }),\n" 3567 " aaaa, bbbb, // comment\n" 3568 " cccc);"); 3569 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3570 verifyFormat("functionCall(aaaa, bbbb, {\n" 3571 " int i;\n" 3572 " int j;\n" 3573 "});"); 3574 verifyFormat( 3575 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3576 " {\n" 3577 " int i; // break\n" 3578 " },\n" 3579 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3580 " ccccccccccccccccc));"); 3581 verifyFormat("DEBUG({\n" 3582 " if (a)\n" 3583 " f();\n" 3584 "});"); 3585 } 3586 3587 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3588 EXPECT_EQ("SOME_MACRO { int i; }\n" 3589 "int i;", 3590 format(" SOME_MACRO {int i;} int i;")); 3591 } 3592 3593 TEST_F(FormatTest, LayoutNestedBlocks) { 3594 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3595 " struct s {\n" 3596 " int i;\n" 3597 " };\n" 3598 " s kBitsToOs[] = {{10}};\n" 3599 " for (int i = 0; i < 10; ++i)\n" 3600 " return;\n" 3601 "}"); 3602 verifyFormat("call(parameter, {\n" 3603 " something();\n" 3604 " // Comment using all columns.\n" 3605 " somethingelse();\n" 3606 "});", 3607 getLLVMStyleWithColumns(40)); 3608 verifyFormat("DEBUG( //\n" 3609 " { f(); }, a);"); 3610 verifyFormat("DEBUG( //\n" 3611 " {\n" 3612 " f(); //\n" 3613 " },\n" 3614 " a);"); 3615 3616 EXPECT_EQ("call(parameter, {\n" 3617 " something();\n" 3618 " // Comment too\n" 3619 " // looooooooooong.\n" 3620 " somethingElse();\n" 3621 "});", 3622 format("call(parameter, {\n" 3623 " something();\n" 3624 " // Comment too looooooooooong.\n" 3625 " somethingElse();\n" 3626 "});", 3627 getLLVMStyleWithColumns(29))); 3628 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3629 EXPECT_EQ("DEBUG({ // comment\n" 3630 " int i;\n" 3631 "});", 3632 format("DEBUG({ // comment\n" 3633 "int i;\n" 3634 "});")); 3635 EXPECT_EQ("DEBUG({\n" 3636 " int i;\n" 3637 "\n" 3638 " // comment\n" 3639 " int j;\n" 3640 "});", 3641 format("DEBUG({\n" 3642 " int i;\n" 3643 "\n" 3644 " // comment\n" 3645 " int j;\n" 3646 "});")); 3647 3648 verifyFormat("DEBUG({\n" 3649 " if (a)\n" 3650 " return;\n" 3651 "});"); 3652 verifyGoogleFormat("DEBUG({\n" 3653 " if (a) return;\n" 3654 "});"); 3655 FormatStyle Style = getGoogleStyle(); 3656 Style.ColumnLimit = 45; 3657 verifyFormat("Debug(\n" 3658 " aaaaa,\n" 3659 " {\n" 3660 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3661 " },\n" 3662 " a);", 3663 Style); 3664 3665 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3666 3667 verifyNoCrash("^{v^{a}}"); 3668 } 3669 3670 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3671 EXPECT_EQ("#define MACRO() \\\n" 3672 " Debug(aaa, /* force line break */ \\\n" 3673 " { \\\n" 3674 " int i; \\\n" 3675 " int j; \\\n" 3676 " })", 3677 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3678 " { int i; int j; })", 3679 getGoogleStyle())); 3680 3681 EXPECT_EQ("#define A \\\n" 3682 " [] { \\\n" 3683 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3684 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3685 " }", 3686 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3687 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3688 getGoogleStyle())); 3689 } 3690 3691 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3692 EXPECT_EQ("{}", format("{}")); 3693 verifyFormat("enum E {};"); 3694 verifyFormat("enum E {}"); 3695 } 3696 3697 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3698 FormatStyle Style = getLLVMStyle(); 3699 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3700 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3701 verifyFormat("FOO_BEGIN\n" 3702 " FOO_ENTRY\n" 3703 "FOO_END", Style); 3704 verifyFormat("FOO_BEGIN\n" 3705 " NESTED_FOO_BEGIN\n" 3706 " NESTED_FOO_ENTRY\n" 3707 " NESTED_FOO_END\n" 3708 "FOO_END", Style); 3709 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3710 " int x;\n" 3711 " x = 1;\n" 3712 "FOO_END(Baz)", Style); 3713 } 3714 3715 //===----------------------------------------------------------------------===// 3716 // Line break tests. 3717 //===----------------------------------------------------------------------===// 3718 3719 TEST_F(FormatTest, PreventConfusingIndents) { 3720 verifyFormat( 3721 "void f() {\n" 3722 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3723 " parameter, parameter, parameter)),\n" 3724 " SecondLongCall(parameter));\n" 3725 "}"); 3726 verifyFormat( 3727 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3728 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3730 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3731 verifyFormat( 3732 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3733 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3734 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3735 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3736 verifyFormat( 3737 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3738 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3739 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3740 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3741 verifyFormat("int a = bbbb && ccc &&\n" 3742 " fffff(\n" 3743 "#define A Just forcing a new line\n" 3744 " ddd);"); 3745 } 3746 3747 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3748 verifyFormat( 3749 "bool aaaaaaa =\n" 3750 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3751 " bbbbbbbb();"); 3752 verifyFormat( 3753 "bool aaaaaaa =\n" 3754 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3755 " bbbbbbbb();"); 3756 3757 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3758 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3759 " ccccccccc == ddddddddddd;"); 3760 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3761 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3762 " ccccccccc == ddddddddddd;"); 3763 verifyFormat( 3764 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3766 " ccccccccc == ddddddddddd;"); 3767 3768 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3769 " aaaaaa) &&\n" 3770 " bbbbbb && cccccc;"); 3771 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3772 " aaaaaa) >>\n" 3773 " bbbbbb;"); 3774 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3775 " SourceMgr.getSpellingColumnNumber(\n" 3776 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3777 " 1);"); 3778 3779 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3780 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3781 " cccccc) {\n}"); 3782 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3783 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 3784 " cccccc) {\n}"); 3785 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3786 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 3787 " cccccc) {\n}"); 3788 verifyFormat("b = a &&\n" 3789 " // Comment\n" 3790 " b.c && d;"); 3791 3792 // If the LHS of a comparison is not a binary expression itself, the 3793 // additional linebreak confuses many people. 3794 verifyFormat( 3795 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3796 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3797 "}"); 3798 verifyFormat( 3799 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3801 "}"); 3802 verifyFormat( 3803 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3804 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3805 "}"); 3806 verifyFormat( 3807 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3808 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 3809 "}"); 3810 // Even explicit parentheses stress the precedence enough to make the 3811 // additional break unnecessary. 3812 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3813 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3814 "}"); 3815 // This cases is borderline, but with the indentation it is still readable. 3816 verifyFormat( 3817 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3818 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3819 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3820 "}", 3821 getLLVMStyleWithColumns(75)); 3822 3823 // If the LHS is a binary expression, we should still use the additional break 3824 // as otherwise the formatting hides the operator precedence. 3825 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3826 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3827 " 5) {\n" 3828 "}"); 3829 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3830 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 3831 " 5) {\n" 3832 "}"); 3833 3834 FormatStyle OnePerLine = getLLVMStyle(); 3835 OnePerLine.BinPackParameters = false; 3836 verifyFormat( 3837 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3838 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3839 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3840 OnePerLine); 3841 3842 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 3843 " .aaa(aaaaaaaaaaaaa) *\n" 3844 " aaaaaaa +\n" 3845 " aaaaaaa;", 3846 getLLVMStyleWithColumns(40)); 3847 } 3848 3849 TEST_F(FormatTest, ExpressionIndentation) { 3850 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3851 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3852 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3854 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3855 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3856 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3858 " ccccccccccccccccccccccccccccccccccccccccc;"); 3859 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3860 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3861 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3862 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3863 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3865 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3866 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3867 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3868 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3869 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3870 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3871 verifyFormat("if () {\n" 3872 "} else if (aaaaa && bbbbb > // break\n" 3873 " ccccc) {\n" 3874 "}"); 3875 verifyFormat("if () {\n" 3876 "} else if constexpr (aaaaa && bbbbb > // break\n" 3877 " ccccc) {\n" 3878 "}"); 3879 verifyFormat("if () {\n" 3880 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 3881 " ccccc) {\n" 3882 "}"); 3883 verifyFormat("if () {\n" 3884 "} else if (aaaaa &&\n" 3885 " bbbbb > // break\n" 3886 " ccccc &&\n" 3887 " ddddd) {\n" 3888 "}"); 3889 3890 // Presence of a trailing comment used to change indentation of b. 3891 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3892 " b;\n" 3893 "return aaaaaaaaaaaaaaaaaaa +\n" 3894 " b; //", 3895 getLLVMStyleWithColumns(30)); 3896 } 3897 3898 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3899 // Not sure what the best system is here. Like this, the LHS can be found 3900 // immediately above an operator (everything with the same or a higher 3901 // indent). The RHS is aligned right of the operator and so compasses 3902 // everything until something with the same indent as the operator is found. 3903 // FIXME: Is this a good system? 3904 FormatStyle Style = getLLVMStyle(); 3905 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3906 verifyFormat( 3907 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3908 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3909 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3910 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3911 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3912 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3913 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3914 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3915 " > ccccccccccccccccccccccccccccccccccccccccc;", 3916 Style); 3917 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3918 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3919 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3920 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3921 Style); 3922 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3923 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3924 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3925 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3926 Style); 3927 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3928 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3929 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3930 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3931 Style); 3932 verifyFormat("if () {\n" 3933 "} else if (aaaaa\n" 3934 " && bbbbb // break\n" 3935 " > ccccc) {\n" 3936 "}", 3937 Style); 3938 verifyFormat("return (a)\n" 3939 " // comment\n" 3940 " + b;", 3941 Style); 3942 verifyFormat( 3943 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3944 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3945 " + cc;", 3946 Style); 3947 3948 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3949 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3950 Style); 3951 3952 // Forced by comments. 3953 verifyFormat( 3954 "unsigned ContentSize =\n" 3955 " sizeof(int16_t) // DWARF ARange version number\n" 3956 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3957 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3958 " + sizeof(int8_t); // Segment Size (in bytes)"); 3959 3960 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3961 " == boost::fusion::at_c<1>(iiii).second;", 3962 Style); 3963 3964 Style.ColumnLimit = 60; 3965 verifyFormat("zzzzzzzzzz\n" 3966 " = bbbbbbbbbbbbbbbbb\n" 3967 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3968 Style); 3969 3970 Style.ColumnLimit = 80; 3971 Style.IndentWidth = 4; 3972 Style.TabWidth = 4; 3973 Style.UseTab = FormatStyle::UT_Always; 3974 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3975 Style.AlignOperands = false; 3976 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 3977 "\t&& (someOtherLongishConditionPart1\n" 3978 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 3979 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && (someOtherLongishConditionPart1 || someOtherEvenLongerNestedConditionPart2);", 3980 Style)); 3981 } 3982 3983 TEST_F(FormatTest, EnforcedOperatorWraps) { 3984 // Here we'd like to wrap after the || operators, but a comment is forcing an 3985 // earlier wrap. 3986 verifyFormat("bool x = aaaaa //\n" 3987 " || bbbbb\n" 3988 " //\n" 3989 " || cccc;"); 3990 } 3991 3992 TEST_F(FormatTest, NoOperandAlignment) { 3993 FormatStyle Style = getLLVMStyle(); 3994 Style.AlignOperands = false; 3995 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 3996 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3997 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3998 Style); 3999 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 4000 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4001 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4002 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4003 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4004 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4005 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4006 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4007 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4008 " > ccccccccccccccccccccccccccccccccccccccccc;", 4009 Style); 4010 4011 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4012 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4013 " + cc;", 4014 Style); 4015 verifyFormat("int a = aa\n" 4016 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4017 " * cccccccccccccccccccccccccccccccccccc;\n", 4018 Style); 4019 4020 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4021 verifyFormat("return (a > b\n" 4022 " // comment1\n" 4023 " // comment2\n" 4024 " || c);", 4025 Style); 4026 } 4027 4028 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 4029 FormatStyle Style = getLLVMStyle(); 4030 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 4031 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4032 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4033 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 4034 Style); 4035 } 4036 4037 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 4038 FormatStyle Style = getLLVMStyle(); 4039 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 4040 Style.BinPackArguments = false; 4041 Style.ColumnLimit = 40; 4042 verifyFormat("void test() {\n" 4043 " someFunction(\n" 4044 " this + argument + is + quite\n" 4045 " + long + so + it + gets + wrapped\n" 4046 " + but + remains + bin - packed);\n" 4047 "}", 4048 Style); 4049 verifyFormat("void test() {\n" 4050 " someFunction(arg1,\n" 4051 " this + argument + is\n" 4052 " + quite + long + so\n" 4053 " + it + gets + wrapped\n" 4054 " + but + remains + bin\n" 4055 " - packed,\n" 4056 " arg3);\n" 4057 "}", 4058 Style); 4059 verifyFormat("void test() {\n" 4060 " someFunction(\n" 4061 " arg1,\n" 4062 " this + argument + has\n" 4063 " + anotherFunc(nested,\n" 4064 " calls + whose\n" 4065 " + arguments\n" 4066 " + are + also\n" 4067 " + wrapped,\n" 4068 " in + addition)\n" 4069 " + to + being + bin - packed,\n" 4070 " arg3);\n" 4071 "}", 4072 Style); 4073 4074 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 4075 verifyFormat("void test() {\n" 4076 " someFunction(\n" 4077 " arg1,\n" 4078 " this + argument + has +\n" 4079 " anotherFunc(nested,\n" 4080 " calls + whose +\n" 4081 " arguments +\n" 4082 " are + also +\n" 4083 " wrapped,\n" 4084 " in + addition) +\n" 4085 " to + being + bin - packed,\n" 4086 " arg3);\n" 4087 "}", 4088 Style); 4089 } 4090 4091 TEST_F(FormatTest, ConstructorInitializers) { 4092 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 4093 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 4094 getLLVMStyleWithColumns(45)); 4095 verifyFormat("Constructor()\n" 4096 " : Inttializer(FitsOnTheLine) {}", 4097 getLLVMStyleWithColumns(44)); 4098 verifyFormat("Constructor()\n" 4099 " : Inttializer(FitsOnTheLine) {}", 4100 getLLVMStyleWithColumns(43)); 4101 4102 verifyFormat("template <typename T>\n" 4103 "Constructor() : Initializer(FitsOnTheLine) {}", 4104 getLLVMStyleWithColumns(45)); 4105 4106 verifyFormat( 4107 "SomeClass::Constructor()\n" 4108 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 4109 4110 verifyFormat( 4111 "SomeClass::Constructor()\n" 4112 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4113 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 4114 verifyFormat( 4115 "SomeClass::Constructor()\n" 4116 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4117 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 4118 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4119 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4120 " : aaaaaaaaaa(aaaaaa) {}"); 4121 4122 verifyFormat("Constructor()\n" 4123 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4124 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4125 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4126 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 4127 4128 verifyFormat("Constructor()\n" 4129 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4130 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4131 4132 verifyFormat("Constructor(int Parameter = 0)\n" 4133 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 4134 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 4135 verifyFormat("Constructor()\n" 4136 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 4137 "}", 4138 getLLVMStyleWithColumns(60)); 4139 verifyFormat("Constructor()\n" 4140 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4141 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 4142 4143 // Here a line could be saved by splitting the second initializer onto two 4144 // lines, but that is not desirable. 4145 verifyFormat("Constructor()\n" 4146 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 4147 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 4148 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4149 4150 FormatStyle OnePerLine = getLLVMStyle(); 4151 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4152 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 4153 verifyFormat("SomeClass::Constructor()\n" 4154 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4155 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4156 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4157 OnePerLine); 4158 verifyFormat("SomeClass::Constructor()\n" 4159 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 4160 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4161 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4162 OnePerLine); 4163 verifyFormat("MyClass::MyClass(int var)\n" 4164 " : some_var_(var), // 4 space indent\n" 4165 " some_other_var_(var + 1) { // lined up\n" 4166 "}", 4167 OnePerLine); 4168 verifyFormat("Constructor()\n" 4169 " : aaaaa(aaaaaa),\n" 4170 " aaaaa(aaaaaa),\n" 4171 " aaaaa(aaaaaa),\n" 4172 " aaaaa(aaaaaa),\n" 4173 " aaaaa(aaaaaa) {}", 4174 OnePerLine); 4175 verifyFormat("Constructor()\n" 4176 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 4177 " aaaaaaaaaaaaaaaaaaaaaa) {}", 4178 OnePerLine); 4179 OnePerLine.BinPackParameters = false; 4180 verifyFormat( 4181 "Constructor()\n" 4182 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4183 " aaaaaaaaaaa().aaa(),\n" 4184 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4185 OnePerLine); 4186 OnePerLine.ColumnLimit = 60; 4187 verifyFormat("Constructor()\n" 4188 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 4189 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 4190 OnePerLine); 4191 4192 EXPECT_EQ("Constructor()\n" 4193 " : // Comment forcing unwanted break.\n" 4194 " aaaa(aaaa) {}", 4195 format("Constructor() :\n" 4196 " // Comment forcing unwanted break.\n" 4197 " aaaa(aaaa) {}")); 4198 } 4199 4200 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 4201 FormatStyle Style = getLLVMStyle(); 4202 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 4203 Style.ColumnLimit = 60; 4204 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4205 Style.AllowAllConstructorInitializersOnNextLine = true; 4206 Style.BinPackParameters = false; 4207 4208 for (int i = 0; i < 4; ++i) { 4209 // Test all combinations of parameters that should not have an effect. 4210 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 4211 Style.AllowAllArgumentsOnNextLine = i & 2; 4212 4213 Style.AllowAllConstructorInitializersOnNextLine = true; 4214 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 4215 verifyFormat("Constructor()\n" 4216 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4217 Style); 4218 verifyFormat("Constructor() : a(a), b(b) {}", Style); 4219 4220 Style.AllowAllConstructorInitializersOnNextLine = false; 4221 verifyFormat("Constructor()\n" 4222 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 4223 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 4224 Style); 4225 verifyFormat("Constructor() : a(a), b(b) {}", Style); 4226 4227 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 4228 Style.AllowAllConstructorInitializersOnNextLine = true; 4229 verifyFormat("Constructor()\n" 4230 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4231 Style); 4232 4233 Style.AllowAllConstructorInitializersOnNextLine = false; 4234 verifyFormat("Constructor()\n" 4235 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 4236 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4237 Style); 4238 4239 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 4240 Style.AllowAllConstructorInitializersOnNextLine = true; 4241 verifyFormat("Constructor() :\n" 4242 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4243 Style); 4244 4245 Style.AllowAllConstructorInitializersOnNextLine = false; 4246 verifyFormat("Constructor() :\n" 4247 " aaaaaaaaaaaaaaaaaa(a),\n" 4248 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4249 Style); 4250 } 4251 4252 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 4253 // AllowAllConstructorInitializersOnNextLine in all 4254 // BreakConstructorInitializers modes 4255 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 4256 Style.AllowAllParametersOfDeclarationOnNextLine = true; 4257 Style.AllowAllConstructorInitializersOnNextLine = false; 4258 verifyFormat("SomeClassWithALongName::Constructor(\n" 4259 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 4260 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 4261 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 4262 Style); 4263 4264 Style.AllowAllConstructorInitializersOnNextLine = true; 4265 verifyFormat("SomeClassWithALongName::Constructor(\n" 4266 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4267 " int bbbbbbbbbbbbb,\n" 4268 " int cccccccccccccccc)\n" 4269 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4270 Style); 4271 4272 Style.AllowAllParametersOfDeclarationOnNextLine = false; 4273 Style.AllowAllConstructorInitializersOnNextLine = false; 4274 verifyFormat("SomeClassWithALongName::Constructor(\n" 4275 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4276 " int bbbbbbbbbbbbb)\n" 4277 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 4278 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 4279 Style); 4280 4281 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 4282 4283 Style.AllowAllParametersOfDeclarationOnNextLine = true; 4284 verifyFormat("SomeClassWithALongName::Constructor(\n" 4285 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 4286 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 4287 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4288 Style); 4289 4290 Style.AllowAllConstructorInitializersOnNextLine = true; 4291 verifyFormat("SomeClassWithALongName::Constructor(\n" 4292 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4293 " int bbbbbbbbbbbbb,\n" 4294 " int cccccccccccccccc)\n" 4295 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4296 Style); 4297 4298 Style.AllowAllParametersOfDeclarationOnNextLine = false; 4299 Style.AllowAllConstructorInitializersOnNextLine = false; 4300 verifyFormat("SomeClassWithALongName::Constructor(\n" 4301 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4302 " int bbbbbbbbbbbbb)\n" 4303 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 4304 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4305 Style); 4306 4307 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 4308 Style.AllowAllParametersOfDeclarationOnNextLine = true; 4309 verifyFormat("SomeClassWithALongName::Constructor(\n" 4310 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 4311 " aaaaaaaaaaaaaaaaaaaa(a),\n" 4312 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4313 Style); 4314 4315 Style.AllowAllConstructorInitializersOnNextLine = true; 4316 verifyFormat("SomeClassWithALongName::Constructor(\n" 4317 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4318 " int bbbbbbbbbbbbb,\n" 4319 " int cccccccccccccccc) :\n" 4320 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 4321 Style); 4322 4323 Style.AllowAllParametersOfDeclarationOnNextLine = false; 4324 Style.AllowAllConstructorInitializersOnNextLine = false; 4325 verifyFormat("SomeClassWithALongName::Constructor(\n" 4326 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 4327 " int bbbbbbbbbbbbb) :\n" 4328 " aaaaaaaaaaaaaaaaaaaa(a),\n" 4329 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 4330 Style); 4331 } 4332 4333 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 4334 FormatStyle Style = getLLVMStyle(); 4335 Style.ColumnLimit = 60; 4336 Style.BinPackArguments = false; 4337 for (int i = 0; i < 4; ++i) { 4338 // Test all combinations of parameters that should not have an effect. 4339 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 4340 Style.AllowAllConstructorInitializersOnNextLine = i & 2; 4341 4342 Style.AllowAllArgumentsOnNextLine = true; 4343 verifyFormat("void foo() {\n" 4344 " FunctionCallWithReallyLongName(\n" 4345 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 4346 "}", 4347 Style); 4348 Style.AllowAllArgumentsOnNextLine = false; 4349 verifyFormat("void foo() {\n" 4350 " FunctionCallWithReallyLongName(\n" 4351 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4352 " bbbbbbbbbbbb);\n" 4353 "}", 4354 Style); 4355 4356 Style.AllowAllArgumentsOnNextLine = true; 4357 verifyFormat("void foo() {\n" 4358 " auto VariableWithReallyLongName = {\n" 4359 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 4360 "}", 4361 Style); 4362 Style.AllowAllArgumentsOnNextLine = false; 4363 verifyFormat("void foo() {\n" 4364 " auto VariableWithReallyLongName = {\n" 4365 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4366 " bbbbbbbbbbbb};\n" 4367 "}", 4368 Style); 4369 } 4370 4371 // This parameter should not affect declarations. 4372 Style.BinPackParameters = false; 4373 Style.AllowAllArgumentsOnNextLine = false; 4374 Style.AllowAllParametersOfDeclarationOnNextLine = true; 4375 verifyFormat("void FunctionCallWithReallyLongName(\n" 4376 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 4377 Style); 4378 Style.AllowAllParametersOfDeclarationOnNextLine = false; 4379 verifyFormat("void FunctionCallWithReallyLongName(\n" 4380 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 4381 " int bbbbbbbbbbbb);", 4382 Style); 4383 } 4384 4385 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 4386 FormatStyle Style = getLLVMStyle(); 4387 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 4388 4389 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 4390 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 4391 getStyleWithColumns(Style, 45)); 4392 verifyFormat("Constructor() :\n" 4393 " Initializer(FitsOnTheLine) {}", 4394 getStyleWithColumns(Style, 44)); 4395 verifyFormat("Constructor() :\n" 4396 " Initializer(FitsOnTheLine) {}", 4397 getStyleWithColumns(Style, 43)); 4398 4399 verifyFormat("template <typename T>\n" 4400 "Constructor() : Initializer(FitsOnTheLine) {}", 4401 getStyleWithColumns(Style, 50)); 4402 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4403 verifyFormat( 4404 "SomeClass::Constructor() :\n" 4405 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 4406 Style); 4407 4408 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false; 4409 verifyFormat( 4410 "SomeClass::Constructor() :\n" 4411 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 4412 Style); 4413 4414 verifyFormat( 4415 "SomeClass::Constructor() :\n" 4416 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4417 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4418 Style); 4419 verifyFormat( 4420 "SomeClass::Constructor() :\n" 4421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4422 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 4423 Style); 4424 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4425 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4426 " aaaaaaaaaa(aaaaaa) {}", 4427 Style); 4428 4429 verifyFormat("Constructor() :\n" 4430 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4431 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4432 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4433 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 4434 Style); 4435 4436 verifyFormat("Constructor() :\n" 4437 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4438 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4439 Style); 4440 4441 verifyFormat("Constructor(int Parameter = 0) :\n" 4442 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 4443 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 4444 Style); 4445 verifyFormat("Constructor() :\n" 4446 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 4447 "}", 4448 getStyleWithColumns(Style, 60)); 4449 verifyFormat("Constructor() :\n" 4450 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4451 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 4452 Style); 4453 4454 // Here a line could be saved by splitting the second initializer onto two 4455 // lines, but that is not desirable. 4456 verifyFormat("Constructor() :\n" 4457 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 4458 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 4459 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4460 Style); 4461 4462 FormatStyle OnePerLine = Style; 4463 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4464 OnePerLine.AllowAllConstructorInitializersOnNextLine = false; 4465 verifyFormat("SomeClass::Constructor() :\n" 4466 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4467 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4468 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4469 OnePerLine); 4470 verifyFormat("SomeClass::Constructor() :\n" 4471 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 4472 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 4473 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 4474 OnePerLine); 4475 verifyFormat("MyClass::MyClass(int var) :\n" 4476 " some_var_(var), // 4 space indent\n" 4477 " some_other_var_(var + 1) { // lined up\n" 4478 "}", 4479 OnePerLine); 4480 verifyFormat("Constructor() :\n" 4481 " aaaaa(aaaaaa),\n" 4482 " aaaaa(aaaaaa),\n" 4483 " aaaaa(aaaaaa),\n" 4484 " aaaaa(aaaaaa),\n" 4485 " aaaaa(aaaaaa) {}", 4486 OnePerLine); 4487 verifyFormat("Constructor() :\n" 4488 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 4489 " aaaaaaaaaaaaaaaaaaaaaa) {}", 4490 OnePerLine); 4491 OnePerLine.BinPackParameters = false; 4492 verifyFormat( 4493 "Constructor() :\n" 4494 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4495 " aaaaaaaaaaa().aaa(),\n" 4496 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4497 OnePerLine); 4498 OnePerLine.ColumnLimit = 60; 4499 verifyFormat("Constructor() :\n" 4500 " aaaaaaaaaaaaaaaaaaaa(a),\n" 4501 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 4502 OnePerLine); 4503 4504 EXPECT_EQ("Constructor() :\n" 4505 " // Comment forcing unwanted break.\n" 4506 " aaaa(aaaa) {}", 4507 format("Constructor() :\n" 4508 " // Comment forcing unwanted break.\n" 4509 " aaaa(aaaa) {}", 4510 Style)); 4511 4512 Style.ColumnLimit = 0; 4513 verifyFormat("SomeClass::Constructor() :\n" 4514 " a(a) {}", 4515 Style); 4516 verifyFormat("SomeClass::Constructor() noexcept :\n" 4517 " a(a) {}", 4518 Style); 4519 verifyFormat("SomeClass::Constructor() :\n" 4520 " a(a), b(b), c(c) {}", 4521 Style); 4522 verifyFormat("SomeClass::Constructor() :\n" 4523 " a(a) {\n" 4524 " foo();\n" 4525 " bar();\n" 4526 "}", 4527 Style); 4528 4529 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 4530 verifyFormat("SomeClass::Constructor() :\n" 4531 " a(a), b(b), c(c) {\n" 4532 "}", 4533 Style); 4534 verifyFormat("SomeClass::Constructor() :\n" 4535 " a(a) {\n" 4536 "}", 4537 Style); 4538 4539 Style.ColumnLimit = 80; 4540 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 4541 Style.ConstructorInitializerIndentWidth = 2; 4542 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 4543 Style); 4544 verifyFormat("SomeClass::Constructor() :\n" 4545 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4546 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 4547 Style); 4548 4549 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as well 4550 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 4551 verifyFormat("class SomeClass\n" 4552 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4553 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 4554 Style); 4555 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 4556 verifyFormat("class SomeClass\n" 4557 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4558 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 4559 Style); 4560 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 4561 verifyFormat("class SomeClass :\n" 4562 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4563 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 4564 Style); 4565 } 4566 4567 #ifndef EXPENSIVE_CHECKS 4568 // Expensive checks enables libstdc++ checking which includes validating the 4569 // state of ranges used in std::priority_queue - this blows out the 4570 // runtime/scalability of the function and makes this test unacceptably slow. 4571 TEST_F(FormatTest, MemoizationTests) { 4572 // This breaks if the memoization lookup does not take \c Indent and 4573 // \c LastSpace into account. 4574 verifyFormat( 4575 "extern CFRunLoopTimerRef\n" 4576 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 4577 " CFTimeInterval interval, CFOptionFlags flags,\n" 4578 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 4579 " CFRunLoopTimerContext *context) {}"); 4580 4581 // Deep nesting somewhat works around our memoization. 4582 verifyFormat( 4583 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4584 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4585 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4586 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 4587 " aaaaa())))))))))))))))))))))))))))))))))))))));", 4588 getLLVMStyleWithColumns(65)); 4589 verifyFormat( 4590 "aaaaa(\n" 4591 " aaaaa,\n" 4592 " aaaaa(\n" 4593 " aaaaa,\n" 4594 " aaaaa(\n" 4595 " aaaaa,\n" 4596 " aaaaa(\n" 4597 " aaaaa,\n" 4598 " aaaaa(\n" 4599 " aaaaa,\n" 4600 " aaaaa(\n" 4601 " aaaaa,\n" 4602 " aaaaa(\n" 4603 " aaaaa,\n" 4604 " aaaaa(\n" 4605 " aaaaa,\n" 4606 " aaaaa(\n" 4607 " aaaaa,\n" 4608 " aaaaa(\n" 4609 " aaaaa,\n" 4610 " aaaaa(\n" 4611 " aaaaa,\n" 4612 " aaaaa(\n" 4613 " aaaaa,\n" 4614 " aaaaa))))))))))));", 4615 getLLVMStyleWithColumns(65)); 4616 verifyFormat( 4617 "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" 4618 " a),\n" 4619 " a),\n" 4620 " a),\n" 4621 " a),\n" 4622 " a),\n" 4623 " a),\n" 4624 " a),\n" 4625 " a),\n" 4626 " a),\n" 4627 " a),\n" 4628 " a),\n" 4629 " a),\n" 4630 " a),\n" 4631 " a),\n" 4632 " a),\n" 4633 " a),\n" 4634 " a)", 4635 getLLVMStyleWithColumns(65)); 4636 4637 // This test takes VERY long when memoization is broken. 4638 FormatStyle OnePerLine = getLLVMStyle(); 4639 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 4640 OnePerLine.BinPackParameters = false; 4641 std::string input = "Constructor()\n" 4642 " : aaaa(a,\n"; 4643 for (unsigned i = 0, e = 80; i != e; ++i) { 4644 input += " a,\n"; 4645 } 4646 input += " a) {}"; 4647 verifyFormat(input, OnePerLine); 4648 } 4649 #endif 4650 4651 TEST_F(FormatTest, BreaksAsHighAsPossible) { 4652 verifyFormat( 4653 "void f() {\n" 4654 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 4655 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 4656 " f();\n" 4657 "}"); 4658 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 4659 " Intervals[i - 1].getRange().getLast()) {\n}"); 4660 } 4661 4662 TEST_F(FormatTest, BreaksFunctionDeclarations) { 4663 // Principially, we break function declarations in a certain order: 4664 // 1) break amongst arguments. 4665 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 4666 " Cccccccccccccc cccccccccccccc);"); 4667 verifyFormat("template <class TemplateIt>\n" 4668 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 4669 " TemplateIt *stop) {}"); 4670 4671 // 2) break after return type. 4672 verifyFormat( 4673 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4674 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 4675 getGoogleStyle()); 4676 4677 // 3) break after (. 4678 verifyFormat( 4679 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 4680 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 4681 getGoogleStyle()); 4682 4683 // 4) break before after nested name specifiers. 4684 verifyFormat( 4685 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4686 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 4687 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 4688 getGoogleStyle()); 4689 4690 // However, there are exceptions, if a sufficient amount of lines can be 4691 // saved. 4692 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 4693 // more adjusting. 4694 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 4695 " Cccccccccccccc cccccccccc,\n" 4696 " Cccccccccccccc cccccccccc,\n" 4697 " Cccccccccccccc cccccccccc,\n" 4698 " Cccccccccccccc cccccccccc);"); 4699 verifyFormat( 4700 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4701 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4702 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4703 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 4704 getGoogleStyle()); 4705 verifyFormat( 4706 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 4707 " Cccccccccccccc cccccccccc,\n" 4708 " Cccccccccccccc cccccccccc,\n" 4709 " Cccccccccccccc cccccccccc,\n" 4710 " Cccccccccccccc cccccccccc,\n" 4711 " Cccccccccccccc cccccccccc,\n" 4712 " Cccccccccccccc cccccccccc);"); 4713 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4714 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4715 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4716 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 4717 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 4718 4719 // Break after multi-line parameters. 4720 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4721 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4723 " bbbb bbbb);"); 4724 verifyFormat("void SomeLoooooooooooongFunction(\n" 4725 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 4726 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4727 " int bbbbbbbbbbbbb);"); 4728 4729 // Treat overloaded operators like other functions. 4730 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4731 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 4732 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4733 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 4734 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 4735 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 4736 verifyGoogleFormat( 4737 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 4738 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 4739 verifyGoogleFormat( 4740 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 4741 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 4742 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4743 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 4744 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 4745 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 4746 verifyGoogleFormat( 4747 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 4748 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4749 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 4750 verifyGoogleFormat( 4751 "template <typename T>\n" 4752 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4753 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 4754 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 4755 4756 FormatStyle Style = getLLVMStyle(); 4757 Style.PointerAlignment = FormatStyle::PAS_Left; 4758 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4759 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 4760 Style); 4761 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 4762 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4763 Style); 4764 } 4765 4766 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 4767 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 4768 // Prefer keeping `::` followed by `operator` together. 4769 EXPECT_EQ("const aaaa::bbbbbbb &\n" 4770 "ccccccccc::operator++() {\n" 4771 " stuff();\n" 4772 "}", 4773 format("const aaaa::bbbbbbb\n" 4774 "&ccccccccc::operator++() { stuff(); }", 4775 getLLVMStyleWithColumns(40))); 4776 } 4777 4778 TEST_F(FormatTest, TrailingReturnType) { 4779 verifyFormat("auto foo() -> int;\n"); 4780 verifyFormat("struct S {\n" 4781 " auto bar() const -> int;\n" 4782 "};"); 4783 verifyFormat("template <size_t Order, typename T>\n" 4784 "auto load_img(const std::string &filename)\n" 4785 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 4786 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 4787 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 4788 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 4789 verifyFormat("template <typename T>\n" 4790 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 4791 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 4792 4793 // Not trailing return types. 4794 verifyFormat("void f() { auto a = b->c(); }"); 4795 } 4796 4797 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 4798 // Avoid breaking before trailing 'const' or other trailing annotations, if 4799 // they are not function-like. 4800 FormatStyle Style = getGoogleStyle(); 4801 Style.ColumnLimit = 47; 4802 verifyFormat("void someLongFunction(\n" 4803 " int someLoooooooooooooongParameter) const {\n}", 4804 getLLVMStyleWithColumns(47)); 4805 verifyFormat("LoooooongReturnType\n" 4806 "someLoooooooongFunction() const {}", 4807 getLLVMStyleWithColumns(47)); 4808 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 4809 " const {}", 4810 Style); 4811 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4812 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 4813 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4814 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 4815 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4816 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 4817 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 4818 " aaaaaaaaaaa aaaaa) const override;"); 4819 verifyGoogleFormat( 4820 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4821 " const override;"); 4822 4823 // Even if the first parameter has to be wrapped. 4824 verifyFormat("void someLongFunction(\n" 4825 " int someLongParameter) const {}", 4826 getLLVMStyleWithColumns(46)); 4827 verifyFormat("void someLongFunction(\n" 4828 " int someLongParameter) const {}", 4829 Style); 4830 verifyFormat("void someLongFunction(\n" 4831 " int someLongParameter) override {}", 4832 Style); 4833 verifyFormat("void someLongFunction(\n" 4834 " int someLongParameter) OVERRIDE {}", 4835 Style); 4836 verifyFormat("void someLongFunction(\n" 4837 " int someLongParameter) final {}", 4838 Style); 4839 verifyFormat("void someLongFunction(\n" 4840 " int someLongParameter) FINAL {}", 4841 Style); 4842 verifyFormat("void someLongFunction(\n" 4843 " int parameter) const override {}", 4844 Style); 4845 4846 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4847 verifyFormat("void someLongFunction(\n" 4848 " int someLongParameter) const\n" 4849 "{\n" 4850 "}", 4851 Style); 4852 4853 // Unless these are unknown annotations. 4854 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 4855 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4856 " LONG_AND_UGLY_ANNOTATION;"); 4857 4858 // Breaking before function-like trailing annotations is fine to keep them 4859 // close to their arguments. 4860 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4861 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4862 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4863 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4864 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4865 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 4866 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 4867 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 4868 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 4869 4870 verifyFormat( 4871 "void aaaaaaaaaaaaaaaaaa()\n" 4872 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 4873 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 4874 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4875 " __attribute__((unused));"); 4876 verifyGoogleFormat( 4877 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4878 " GUARDED_BY(aaaaaaaaaaaa);"); 4879 verifyGoogleFormat( 4880 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4881 " GUARDED_BY(aaaaaaaaaaaa);"); 4882 verifyGoogleFormat( 4883 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4884 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4885 verifyGoogleFormat( 4886 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4887 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4888 } 4889 4890 TEST_F(FormatTest, FunctionAnnotations) { 4891 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4892 "int OldFunction(const string ¶meter) {}"); 4893 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4894 "string OldFunction(const string ¶meter) {}"); 4895 verifyFormat("template <typename T>\n" 4896 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4897 "string OldFunction(const string ¶meter) {}"); 4898 4899 // Not function annotations. 4900 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4901 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4902 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4903 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4904 verifyFormat("MACRO(abc).function() // wrap\n" 4905 " << abc;"); 4906 verifyFormat("MACRO(abc)->function() // wrap\n" 4907 " << abc;"); 4908 verifyFormat("MACRO(abc)::function() // wrap\n" 4909 " << abc;"); 4910 } 4911 4912 TEST_F(FormatTest, BreaksDesireably) { 4913 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4914 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4915 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4916 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4917 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4918 "}"); 4919 4920 verifyFormat( 4921 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4922 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4923 4924 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4925 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4926 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4927 4928 verifyFormat( 4929 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4930 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4931 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4932 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4934 4935 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4936 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4937 4938 verifyFormat( 4939 "void f() {\n" 4940 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4942 "}"); 4943 verifyFormat( 4944 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4945 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4946 verifyFormat( 4947 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4948 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4949 verifyFormat( 4950 "aaaaaa(aaa,\n" 4951 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4952 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4953 " aaaa);"); 4954 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4955 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4956 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4957 4958 // Indent consistently independent of call expression and unary operator. 4959 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4960 " dddddddddddddddddddddddddddddd));"); 4961 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4962 " dddddddddddddddddddddddddddddd));"); 4963 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4964 " dddddddddddddddddddddddddddddd));"); 4965 4966 // This test case breaks on an incorrect memoization, i.e. an optimization not 4967 // taking into account the StopAt value. 4968 verifyFormat( 4969 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4970 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4971 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4972 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4973 4974 verifyFormat("{\n {\n {\n" 4975 " Annotation.SpaceRequiredBefore =\n" 4976 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4977 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4978 " }\n }\n}"); 4979 4980 // Break on an outer level if there was a break on an inner level. 4981 EXPECT_EQ("f(g(h(a, // comment\n" 4982 " b, c),\n" 4983 " d, e),\n" 4984 " x, y);", 4985 format("f(g(h(a, // comment\n" 4986 " b, c), d, e), x, y);")); 4987 4988 // Prefer breaking similar line breaks. 4989 verifyFormat( 4990 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4991 " NSTrackingMouseEnteredAndExited |\n" 4992 " NSTrackingActiveAlways;"); 4993 } 4994 4995 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4996 FormatStyle NoBinPacking = getGoogleStyle(); 4997 NoBinPacking.BinPackParameters = false; 4998 NoBinPacking.BinPackArguments = true; 4999 verifyFormat("void f() {\n" 5000 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 5001 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5002 "}", 5003 NoBinPacking); 5004 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 5005 " int aaaaaaaaaaaaaaaaaaaa,\n" 5006 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5007 NoBinPacking); 5008 5009 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 5010 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5011 " vector<int> bbbbbbbbbbbbbbb);", 5012 NoBinPacking); 5013 // FIXME: This behavior difference is probably not wanted. However, currently 5014 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 5015 // template arguments from BreakBeforeParameter being set because of the 5016 // one-per-line formatting. 5017 verifyFormat( 5018 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 5019 " aaaaaaaaaa> aaaaaaaaaa);", 5020 NoBinPacking); 5021 verifyFormat( 5022 "void fffffffffff(\n" 5023 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 5024 " aaaaaaaaaa);"); 5025 } 5026 5027 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 5028 FormatStyle NoBinPacking = getGoogleStyle(); 5029 NoBinPacking.BinPackParameters = false; 5030 NoBinPacking.BinPackArguments = false; 5031 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 5032 " aaaaaaaaaaaaaaaaaaaa,\n" 5033 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 5034 NoBinPacking); 5035 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 5036 " aaaaaaaaaaaaa,\n" 5037 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 5038 NoBinPacking); 5039 verifyFormat( 5040 "aaaaaaaa(aaaaaaaaaaaaa,\n" 5041 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5042 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 5043 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 5045 NoBinPacking); 5046 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 5047 " .aaaaaaaaaaaaaaaaaa();", 5048 NoBinPacking); 5049 verifyFormat("void f() {\n" 5050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5051 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 5052 "}", 5053 NoBinPacking); 5054 5055 verifyFormat( 5056 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5057 " aaaaaaaaaaaa,\n" 5058 " aaaaaaaaaaaa);", 5059 NoBinPacking); 5060 verifyFormat( 5061 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 5062 " ddddddddddddddddddddddddddddd),\n" 5063 " test);", 5064 NoBinPacking); 5065 5066 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 5067 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 5068 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 5069 " aaaaaaaaaaaaaaaaaa;", 5070 NoBinPacking); 5071 verifyFormat("a(\"a\"\n" 5072 " \"a\",\n" 5073 " a);"); 5074 5075 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 5076 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 5077 " aaaaaaaaa,\n" 5078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5079 NoBinPacking); 5080 verifyFormat( 5081 "void f() {\n" 5082 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 5083 " .aaaaaaa();\n" 5084 "}", 5085 NoBinPacking); 5086 verifyFormat( 5087 "template <class SomeType, class SomeOtherType>\n" 5088 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 5089 NoBinPacking); 5090 } 5091 5092 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 5093 FormatStyle Style = getLLVMStyleWithColumns(15); 5094 Style.ExperimentalAutoDetectBinPacking = true; 5095 EXPECT_EQ("aaa(aaaa,\n" 5096 " aaaa,\n" 5097 " aaaa);\n" 5098 "aaa(aaaa,\n" 5099 " aaaa,\n" 5100 " aaaa);", 5101 format("aaa(aaaa,\n" // one-per-line 5102 " aaaa,\n" 5103 " aaaa );\n" 5104 "aaa(aaaa, aaaa, aaaa);", // inconclusive 5105 Style)); 5106 EXPECT_EQ("aaa(aaaa, aaaa,\n" 5107 " aaaa);\n" 5108 "aaa(aaaa, aaaa,\n" 5109 " aaaa);", 5110 format("aaa(aaaa, aaaa,\n" // bin-packed 5111 " aaaa );\n" 5112 "aaa(aaaa, aaaa, aaaa);", // inconclusive 5113 Style)); 5114 } 5115 5116 TEST_F(FormatTest, FormatsBuilderPattern) { 5117 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 5118 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 5119 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 5120 " .StartsWith(\".init\", ORDER_INIT)\n" 5121 " .StartsWith(\".fini\", ORDER_FINI)\n" 5122 " .StartsWith(\".hash\", ORDER_HASH)\n" 5123 " .Default(ORDER_TEXT);\n"); 5124 5125 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 5126 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 5127 verifyFormat( 5128 "aaaaaaa->aaaaaaa\n" 5129 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5130 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5131 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 5132 verifyFormat( 5133 "aaaaaaa->aaaaaaa\n" 5134 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5135 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 5136 verifyFormat( 5137 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 5138 " aaaaaaaaaaaaaa);"); 5139 verifyFormat( 5140 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 5141 " aaaaaa->aaaaaaaaaaaa()\n" 5142 " ->aaaaaaaaaaaaaaaa(\n" 5143 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5144 " ->aaaaaaaaaaaaaaaaa();"); 5145 verifyGoogleFormat( 5146 "void f() {\n" 5147 " someo->Add((new util::filetools::Handler(dir))\n" 5148 " ->OnEvent1(NewPermanentCallback(\n" 5149 " this, &HandlerHolderClass::EventHandlerCBA))\n" 5150 " ->OnEvent2(NewPermanentCallback(\n" 5151 " this, &HandlerHolderClass::EventHandlerCBB))\n" 5152 " ->OnEvent3(NewPermanentCallback(\n" 5153 " this, &HandlerHolderClass::EventHandlerCBC))\n" 5154 " ->OnEvent5(NewPermanentCallback(\n" 5155 " this, &HandlerHolderClass::EventHandlerCBD))\n" 5156 " ->OnEvent6(NewPermanentCallback(\n" 5157 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 5158 "}"); 5159 5160 verifyFormat( 5161 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 5162 verifyFormat("aaaaaaaaaaaaaaa()\n" 5163 " .aaaaaaaaaaaaaaa()\n" 5164 " .aaaaaaaaaaaaaaa()\n" 5165 " .aaaaaaaaaaaaaaa()\n" 5166 " .aaaaaaaaaaaaaaa();"); 5167 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 5168 " .aaaaaaaaaaaaaaa()\n" 5169 " .aaaaaaaaaaaaaaa()\n" 5170 " .aaaaaaaaaaaaaaa();"); 5171 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 5172 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 5173 " .aaaaaaaaaaaaaaa();"); 5174 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 5175 " ->aaaaaaaaaaaaaae(0)\n" 5176 " ->aaaaaaaaaaaaaaa();"); 5177 5178 // Don't linewrap after very short segments. 5179 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5180 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5181 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5182 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5183 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5184 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5185 verifyFormat("aaa()\n" 5186 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5187 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5188 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5189 5190 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 5191 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5192 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 5193 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 5194 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5195 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 5196 5197 // Prefer not to break after empty parentheses. 5198 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 5199 " First->LastNewlineOffset);"); 5200 5201 // Prefer not to create "hanging" indents. 5202 verifyFormat( 5203 "return !soooooooooooooome_map\n" 5204 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5205 " .second;"); 5206 verifyFormat( 5207 "return aaaaaaaaaaaaaaaa\n" 5208 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 5209 " .aaaa(aaaaaaaaaaaaaa);"); 5210 // No hanging indent here. 5211 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 5212 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5213 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 5214 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5215 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 5216 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5217 getLLVMStyleWithColumns(60)); 5218 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 5219 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 5220 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5221 getLLVMStyleWithColumns(59)); 5222 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5223 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5224 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5225 5226 // Dont break if only closing statements before member call 5227 verifyFormat("test() {\n" 5228 " ([]() -> {\n" 5229 " int b = 32;\n" 5230 " return 3;\n" 5231 " }).foo();\n" 5232 "}"); 5233 verifyFormat("test() {\n" 5234 " (\n" 5235 " []() -> {\n" 5236 " int b = 32;\n" 5237 " return 3;\n" 5238 " },\n" 5239 " foo, bar)\n" 5240 " .foo();\n" 5241 "}"); 5242 verifyFormat("test() {\n" 5243 " ([]() -> {\n" 5244 " int b = 32;\n" 5245 " return 3;\n" 5246 " })\n" 5247 " .foo()\n" 5248 " .bar();\n" 5249 "}"); 5250 verifyFormat("test() {\n" 5251 " ([]() -> {\n" 5252 " int b = 32;\n" 5253 " return 3;\n" 5254 " })\n" 5255 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 5256 " \"bbbb\");\n" 5257 "}", 5258 getLLVMStyleWithColumns(30)); 5259 } 5260 5261 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 5262 verifyFormat( 5263 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5264 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 5265 verifyFormat( 5266 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 5267 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 5268 5269 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 5270 " ccccccccccccccccccccccccc) {\n}"); 5271 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 5272 " ccccccccccccccccccccccccc) {\n}"); 5273 5274 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 5275 " ccccccccccccccccccccccccc) {\n}"); 5276 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 5277 " ccccccccccccccccccccccccc) {\n}"); 5278 5279 verifyFormat( 5280 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 5281 " ccccccccccccccccccccccccc) {\n}"); 5282 verifyFormat( 5283 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 5284 " ccccccccccccccccccccccccc) {\n}"); 5285 5286 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 5287 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 5288 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 5289 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 5290 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 5291 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 5292 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 5293 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 5294 5295 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 5296 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 5297 " aaaaaaaaaaaaaaa != aa) {\n}"); 5298 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 5299 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 5300 " aaaaaaaaaaaaaaa != aa) {\n}"); 5301 } 5302 5303 TEST_F(FormatTest, BreaksAfterAssignments) { 5304 verifyFormat( 5305 "unsigned Cost =\n" 5306 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 5307 " SI->getPointerAddressSpaceee());\n"); 5308 verifyFormat( 5309 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 5310 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 5311 5312 verifyFormat( 5313 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 5314 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 5315 verifyFormat("unsigned OriginalStartColumn =\n" 5316 " SourceMgr.getSpellingColumnNumber(\n" 5317 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 5318 " 1;"); 5319 } 5320 5321 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 5322 FormatStyle Style = getLLVMStyle(); 5323 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5324 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 5325 Style); 5326 5327 Style.PenaltyBreakAssignment = 20; 5328 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 5329 " cccccccccccccccccccccccccc;", 5330 Style); 5331 } 5332 5333 TEST_F(FormatTest, AlignsAfterAssignments) { 5334 verifyFormat( 5335 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5336 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5337 verifyFormat( 5338 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5339 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5340 verifyFormat( 5341 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5342 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5343 verifyFormat( 5344 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5345 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 5346 verifyFormat( 5347 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 5348 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 5349 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 5350 } 5351 5352 TEST_F(FormatTest, AlignsAfterReturn) { 5353 verifyFormat( 5354 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5355 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 5356 verifyFormat( 5357 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5358 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 5359 verifyFormat( 5360 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 5361 " aaaaaaaaaaaaaaaaaaaaaa();"); 5362 verifyFormat( 5363 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 5364 " aaaaaaaaaaaaaaaaaaaaaa());"); 5365 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5366 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5367 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5368 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 5369 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5370 verifyFormat("return\n" 5371 " // true if code is one of a or b.\n" 5372 " code == a || code == b;"); 5373 } 5374 5375 TEST_F(FormatTest, AlignsAfterOpenBracket) { 5376 verifyFormat( 5377 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 5378 " aaaaaaaaa aaaaaaa) {}"); 5379 verifyFormat( 5380 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 5381 " aaaaaaaaaaa aaaaaaaaa);"); 5382 verifyFormat( 5383 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 5384 " aaaaaaaaaaaaaaaaaaaaa));"); 5385 FormatStyle Style = getLLVMStyle(); 5386 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5387 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5388 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 5389 Style); 5390 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 5391 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 5392 Style); 5393 verifyFormat("SomeLongVariableName->someFunction(\n" 5394 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 5395 Style); 5396 verifyFormat( 5397 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 5398 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5399 Style); 5400 verifyFormat( 5401 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 5402 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5403 Style); 5404 verifyFormat( 5405 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 5406 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 5407 Style); 5408 5409 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 5410 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 5411 " b));", 5412 Style); 5413 5414 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5415 Style.BinPackArguments = false; 5416 Style.BinPackParameters = false; 5417 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5418 " aaaaaaaaaaa aaaaaaaa,\n" 5419 " aaaaaaaaa aaaaaaa,\n" 5420 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5421 Style); 5422 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 5423 " aaaaaaaaaaa aaaaaaaaa,\n" 5424 " aaaaaaaaaaa aaaaaaaaa,\n" 5425 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5426 Style); 5427 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 5428 " aaaaaaaaaaaaaaa,\n" 5429 " aaaaaaaaaaaaaaaaaaaaa,\n" 5430 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 5431 Style); 5432 verifyFormat( 5433 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 5434 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 5435 Style); 5436 verifyFormat( 5437 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 5438 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 5439 Style); 5440 verifyFormat( 5441 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5442 " aaaaaaaaaaaaaaaaaaaaa(\n" 5443 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 5444 " aaaaaaaaaaaaaaaa);", 5445 Style); 5446 verifyFormat( 5447 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5448 " aaaaaaaaaaaaaaaaaaaaa(\n" 5449 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 5450 " aaaaaaaaaaaaaaaa);", 5451 Style); 5452 } 5453 5454 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 5455 FormatStyle Style = getLLVMStyleWithColumns(40); 5456 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5457 " bbbbbbbbbbbbbbbbbbbbbb);", 5458 Style); 5459 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 5460 Style.AlignOperands = false; 5461 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5462 " bbbbbbbbbbbbbbbbbbbbbb);", 5463 Style); 5464 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5465 Style.AlignOperands = true; 5466 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5467 " bbbbbbbbbbbbbbbbbbbbbb);", 5468 Style); 5469 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5470 Style.AlignOperands = false; 5471 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 5472 " bbbbbbbbbbbbbbbbbbbbbb);", 5473 Style); 5474 } 5475 5476 TEST_F(FormatTest, BreaksConditionalExpressions) { 5477 verifyFormat( 5478 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5479 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5480 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5481 verifyFormat( 5482 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 5483 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5484 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5485 verifyFormat( 5486 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5487 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5488 verifyFormat( 5489 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 5490 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5491 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5492 verifyFormat( 5493 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 5494 " : aaaaaaaaaaaaa);"); 5495 verifyFormat( 5496 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5497 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5498 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5499 " aaaaaaaaaaaaa);"); 5500 verifyFormat( 5501 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5502 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5503 " aaaaaaaaaaaaa);"); 5504 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5505 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5506 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5507 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5508 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5509 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5510 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5511 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5512 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5513 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5514 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5515 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5516 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5517 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5518 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5519 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5520 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5521 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5522 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5523 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5524 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 5525 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5526 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5527 " : aaaaaaaaaaaaaaaa;"); 5528 verifyFormat( 5529 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5530 " ? aaaaaaaaaaaaaaa\n" 5531 " : aaaaaaaaaaaaaaa;"); 5532 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 5533 " aaaaaaaaa\n" 5534 " ? b\n" 5535 " : c);"); 5536 verifyFormat("return aaaa == bbbb\n" 5537 " // comment\n" 5538 " ? aaaa\n" 5539 " : bbbb;"); 5540 verifyFormat("unsigned Indent =\n" 5541 " format(TheLine.First,\n" 5542 " IndentForLevel[TheLine.Level] >= 0\n" 5543 " ? IndentForLevel[TheLine.Level]\n" 5544 " : TheLine * 2,\n" 5545 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 5546 getLLVMStyleWithColumns(60)); 5547 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 5548 " ? aaaaaaaaaaaaaaa\n" 5549 " : bbbbbbbbbbbbbbb //\n" 5550 " ? ccccccccccccccc\n" 5551 " : ddddddddddddddd;"); 5552 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 5553 " ? aaaaaaaaaaaaaaa\n" 5554 " : (bbbbbbbbbbbbbbb //\n" 5555 " ? ccccccccccccccc\n" 5556 " : ddddddddddddddd);"); 5557 verifyFormat( 5558 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5559 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5560 " aaaaaaaaaaaaaaaaaaaaa +\n" 5561 " aaaaaaaaaaaaaaaaaaaaa\n" 5562 " : aaaaaaaaaa;"); 5563 verifyFormat( 5564 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5565 " : aaaaaaaaaaaaaaaaaaaaaa\n" 5566 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5567 5568 FormatStyle NoBinPacking = getLLVMStyle(); 5569 NoBinPacking.BinPackArguments = false; 5570 verifyFormat( 5571 "void f() {\n" 5572 " g(aaa,\n" 5573 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 5574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5575 " ? aaaaaaaaaaaaaaa\n" 5576 " : aaaaaaaaaaaaaaa);\n" 5577 "}", 5578 NoBinPacking); 5579 verifyFormat( 5580 "void f() {\n" 5581 " g(aaa,\n" 5582 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 5583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5584 " ?: aaaaaaaaaaaaaaa);\n" 5585 "}", 5586 NoBinPacking); 5587 5588 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 5589 " // comment.\n" 5590 " ccccccccccccccccccccccccccccccccccccccc\n" 5591 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5592 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 5593 5594 // Assignments in conditional expressions. Apparently not uncommon :-(. 5595 verifyFormat("return a != b\n" 5596 " // comment\n" 5597 " ? a = b\n" 5598 " : a = b;"); 5599 verifyFormat("return a != b\n" 5600 " // comment\n" 5601 " ? a = a != b\n" 5602 " // comment\n" 5603 " ? a = b\n" 5604 " : a\n" 5605 " : a;\n"); 5606 verifyFormat("return a != b\n" 5607 " // comment\n" 5608 " ? a\n" 5609 " : a = a != b\n" 5610 " // comment\n" 5611 " ? a = b\n" 5612 " : a;"); 5613 } 5614 5615 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 5616 FormatStyle Style = getLLVMStyle(); 5617 Style.BreakBeforeTernaryOperators = false; 5618 Style.ColumnLimit = 70; 5619 verifyFormat( 5620 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5621 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5622 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5623 Style); 5624 verifyFormat( 5625 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 5626 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5627 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5628 Style); 5629 verifyFormat( 5630 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5631 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5632 Style); 5633 verifyFormat( 5634 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 5635 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5636 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5637 Style); 5638 verifyFormat( 5639 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 5640 " aaaaaaaaaaaaa);", 5641 Style); 5642 verifyFormat( 5643 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5644 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5645 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5646 " aaaaaaaaaaaaa);", 5647 Style); 5648 verifyFormat( 5649 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5650 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5651 " aaaaaaaaaaaaa);", 5652 Style); 5653 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5654 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5655 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5656 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5657 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5658 Style); 5659 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5660 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5661 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5662 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5664 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5665 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5666 Style); 5667 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5668 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 5669 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5670 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5671 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5672 Style); 5673 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5674 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5675 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5676 Style); 5677 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 5678 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5679 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 5680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5681 Style); 5682 verifyFormat( 5683 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5684 " aaaaaaaaaaaaaaa :\n" 5685 " aaaaaaaaaaaaaaa;", 5686 Style); 5687 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 5688 " aaaaaaaaa ?\n" 5689 " b :\n" 5690 " c);", 5691 Style); 5692 verifyFormat("unsigned Indent =\n" 5693 " format(TheLine.First,\n" 5694 " IndentForLevel[TheLine.Level] >= 0 ?\n" 5695 " IndentForLevel[TheLine.Level] :\n" 5696 " TheLine * 2,\n" 5697 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 5698 Style); 5699 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 5700 " aaaaaaaaaaaaaaa :\n" 5701 " bbbbbbbbbbbbbbb ? //\n" 5702 " ccccccccccccccc :\n" 5703 " ddddddddddddddd;", 5704 Style); 5705 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 5706 " aaaaaaaaaaaaaaa :\n" 5707 " (bbbbbbbbbbbbbbb ? //\n" 5708 " ccccccccccccccc :\n" 5709 " ddddddddddddddd);", 5710 Style); 5711 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5712 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 5713 " ccccccccccccccccccccccccccc;", 5714 Style); 5715 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 5716 " aaaaa :\n" 5717 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 5718 Style); 5719 } 5720 5721 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 5722 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 5723 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 5724 verifyFormat("bool a = true, b = false;"); 5725 5726 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 5728 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 5729 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 5730 verifyFormat( 5731 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5732 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 5733 " d = e && f;"); 5734 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 5735 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 5736 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 5737 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 5738 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 5739 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 5740 5741 FormatStyle Style = getGoogleStyle(); 5742 Style.PointerAlignment = FormatStyle::PAS_Left; 5743 Style.DerivePointerAlignment = false; 5744 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5745 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 5746 " *b = bbbbbbbbbbbbbbbbbbb;", 5747 Style); 5748 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 5749 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 5750 Style); 5751 verifyFormat("vector<int*> a, b;", Style); 5752 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 5753 } 5754 5755 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 5756 verifyFormat("arr[foo ? bar : baz];"); 5757 verifyFormat("f()[foo ? bar : baz];"); 5758 verifyFormat("(a + b)[foo ? bar : baz];"); 5759 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 5760 } 5761 5762 TEST_F(FormatTest, AlignsStringLiterals) { 5763 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 5764 " \"short literal\");"); 5765 verifyFormat( 5766 "looooooooooooooooooooooooongFunction(\n" 5767 " \"short literal\"\n" 5768 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 5769 verifyFormat("someFunction(\"Always break between multi-line\"\n" 5770 " \" string literals\",\n" 5771 " and, other, parameters);"); 5772 EXPECT_EQ("fun + \"1243\" /* comment */\n" 5773 " \"5678\";", 5774 format("fun + \"1243\" /* comment */\n" 5775 " \"5678\";", 5776 getLLVMStyleWithColumns(28))); 5777 EXPECT_EQ( 5778 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 5779 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 5780 " \"aaaaaaaaaaaaaaaa\";", 5781 format("aaaaaa =" 5782 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 5783 "aaaaaaaaaaaaaaaaaaaaa\" " 5784 "\"aaaaaaaaaaaaaaaa\";")); 5785 verifyFormat("a = a + \"a\"\n" 5786 " \"a\"\n" 5787 " \"a\";"); 5788 verifyFormat("f(\"a\", \"b\"\n" 5789 " \"c\");"); 5790 5791 verifyFormat( 5792 "#define LL_FORMAT \"ll\"\n" 5793 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 5794 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 5795 5796 verifyFormat("#define A(X) \\\n" 5797 " \"aaaaa\" #X \"bbbbbb\" \\\n" 5798 " \"ccccc\"", 5799 getLLVMStyleWithColumns(23)); 5800 verifyFormat("#define A \"def\"\n" 5801 "f(\"abc\" A \"ghi\"\n" 5802 " \"jkl\");"); 5803 5804 verifyFormat("f(L\"a\"\n" 5805 " L\"b\");"); 5806 verifyFormat("#define A(X) \\\n" 5807 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 5808 " L\"ccccc\"", 5809 getLLVMStyleWithColumns(25)); 5810 5811 verifyFormat("f(@\"a\"\n" 5812 " @\"b\");"); 5813 verifyFormat("NSString s = @\"a\"\n" 5814 " @\"b\"\n" 5815 " @\"c\";"); 5816 verifyFormat("NSString s = @\"a\"\n" 5817 " \"b\"\n" 5818 " \"c\";"); 5819 } 5820 5821 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 5822 FormatStyle Style = getLLVMStyle(); 5823 // No declarations or definitions should be moved to own line. 5824 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 5825 verifyFormat("class A {\n" 5826 " int f() { return 1; }\n" 5827 " int g();\n" 5828 "};\n" 5829 "int f() { return 1; }\n" 5830 "int g();\n", 5831 Style); 5832 5833 // All declarations and definitions should have the return type moved to its 5834 // own 5835 // line. 5836 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 5837 verifyFormat("class E {\n" 5838 " int\n" 5839 " f() {\n" 5840 " return 1;\n" 5841 " }\n" 5842 " int\n" 5843 " g();\n" 5844 "};\n" 5845 "int\n" 5846 "f() {\n" 5847 " return 1;\n" 5848 "}\n" 5849 "int\n" 5850 "g();\n", 5851 Style); 5852 5853 // Top-level definitions, and no kinds of declarations should have the 5854 // return type moved to its own line. 5855 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 5856 verifyFormat("class B {\n" 5857 " int f() { return 1; }\n" 5858 " int g();\n" 5859 "};\n" 5860 "int\n" 5861 "f() {\n" 5862 " return 1;\n" 5863 "}\n" 5864 "int g();\n", 5865 Style); 5866 5867 // Top-level definitions and declarations should have the return type moved 5868 // to its own line. 5869 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 5870 verifyFormat("class C {\n" 5871 " int f() { return 1; }\n" 5872 " int g();\n" 5873 "};\n" 5874 "int\n" 5875 "f() {\n" 5876 " return 1;\n" 5877 "}\n" 5878 "int\n" 5879 "g();\n", 5880 Style); 5881 5882 // All definitions should have the return type moved to its own line, but no 5883 // kinds of declarations. 5884 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5885 verifyFormat("class D {\n" 5886 " int\n" 5887 " f() {\n" 5888 " return 1;\n" 5889 " }\n" 5890 " int g();\n" 5891 "};\n" 5892 "int\n" 5893 "f() {\n" 5894 " return 1;\n" 5895 "}\n" 5896 "int g();\n", 5897 Style); 5898 verifyFormat("const char *\n" 5899 "f(void) {\n" // Break here. 5900 " return \"\";\n" 5901 "}\n" 5902 "const char *bar(void);\n", // No break here. 5903 Style); 5904 verifyFormat("template <class T>\n" 5905 "T *\n" 5906 "f(T &c) {\n" // Break here. 5907 " return NULL;\n" 5908 "}\n" 5909 "template <class T> T *f(T &c);\n", // No break here. 5910 Style); 5911 verifyFormat("class C {\n" 5912 " int\n" 5913 " operator+() {\n" 5914 " return 1;\n" 5915 " }\n" 5916 " int\n" 5917 " operator()() {\n" 5918 " return 1;\n" 5919 " }\n" 5920 "};\n", 5921 Style); 5922 verifyFormat("void\n" 5923 "A::operator()() {}\n" 5924 "void\n" 5925 "A::operator>>() {}\n" 5926 "void\n" 5927 "A::operator+() {}\n", 5928 Style); 5929 verifyFormat("void *operator new(std::size_t s);", // No break here. 5930 Style); 5931 verifyFormat("void *\n" 5932 "operator new(std::size_t s) {}", 5933 Style); 5934 verifyFormat("void *\n" 5935 "operator delete[](void *ptr) {}", 5936 Style); 5937 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 5938 verifyFormat("const char *\n" 5939 "f(void)\n" // Break here. 5940 "{\n" 5941 " return \"\";\n" 5942 "}\n" 5943 "const char *bar(void);\n", // No break here. 5944 Style); 5945 verifyFormat("template <class T>\n" 5946 "T *\n" // Problem here: no line break 5947 "f(T &c)\n" // Break here. 5948 "{\n" 5949 " return NULL;\n" 5950 "}\n" 5951 "template <class T> T *f(T &c);\n", // No break here. 5952 Style); 5953 verifyFormat("int\n" 5954 "foo(A<bool> a)\n" 5955 "{\n" 5956 " return a;\n" 5957 "}\n", 5958 Style); 5959 verifyFormat("int\n" 5960 "foo(A<8> a)\n" 5961 "{\n" 5962 " return a;\n" 5963 "}\n", 5964 Style); 5965 verifyFormat("int\n" 5966 "foo(A<B<bool>, 8> a)\n" 5967 "{\n" 5968 " return a;\n" 5969 "}\n", 5970 Style); 5971 verifyFormat("int\n" 5972 "foo(A<B<8>, bool> a)\n" 5973 "{\n" 5974 " return a;\n" 5975 "}\n", 5976 Style); 5977 verifyFormat("int\n" 5978 "foo(A<B<bool>, bool> a)\n" 5979 "{\n" 5980 " return a;\n" 5981 "}\n", 5982 Style); 5983 verifyFormat("int\n" 5984 "foo(A<B<8>, 8> a)\n" 5985 "{\n" 5986 " return a;\n" 5987 "}\n", 5988 Style); 5989 5990 Style = getGNUStyle(); 5991 5992 // Test for comments at the end of function declarations. 5993 verifyFormat("void\n" 5994 "foo (int a, /*abc*/ int b) // def\n" 5995 "{\n" 5996 "}\n", 5997 Style); 5998 5999 verifyFormat("void\n" 6000 "foo (int a, /* abc */ int b) /* def */\n" 6001 "{\n" 6002 "}\n", 6003 Style); 6004 6005 // Definitions that should not break after return type 6006 verifyFormat("void foo (int a, int b); // def\n", Style); 6007 verifyFormat("void foo (int a, int b); /* def */\n", Style); 6008 verifyFormat("void foo (int a, int b);\n", Style); 6009 } 6010 6011 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 6012 FormatStyle NoBreak = getLLVMStyle(); 6013 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 6014 FormatStyle Break = getLLVMStyle(); 6015 Break.AlwaysBreakBeforeMultilineStrings = true; 6016 verifyFormat("aaaa = \"bbbb\"\n" 6017 " \"cccc\";", 6018 NoBreak); 6019 verifyFormat("aaaa =\n" 6020 " \"bbbb\"\n" 6021 " \"cccc\";", 6022 Break); 6023 verifyFormat("aaaa(\"bbbb\"\n" 6024 " \"cccc\");", 6025 NoBreak); 6026 verifyFormat("aaaa(\n" 6027 " \"bbbb\"\n" 6028 " \"cccc\");", 6029 Break); 6030 verifyFormat("aaaa(qqq, \"bbbb\"\n" 6031 " \"cccc\");", 6032 NoBreak); 6033 verifyFormat("aaaa(qqq,\n" 6034 " \"bbbb\"\n" 6035 " \"cccc\");", 6036 Break); 6037 verifyFormat("aaaa(qqq,\n" 6038 " L\"bbbb\"\n" 6039 " L\"cccc\");", 6040 Break); 6041 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 6042 " \"bbbb\"));", 6043 Break); 6044 verifyFormat("string s = someFunction(\n" 6045 " \"abc\"\n" 6046 " \"abc\");", 6047 Break); 6048 6049 // As we break before unary operators, breaking right after them is bad. 6050 verifyFormat("string foo = abc ? \"x\"\n" 6051 " \"blah blah blah blah blah blah\"\n" 6052 " : \"y\";", 6053 Break); 6054 6055 // Don't break if there is no column gain. 6056 verifyFormat("f(\"aaaa\"\n" 6057 " \"bbbb\");", 6058 Break); 6059 6060 // Treat literals with escaped newlines like multi-line string literals. 6061 EXPECT_EQ("x = \"a\\\n" 6062 "b\\\n" 6063 "c\";", 6064 format("x = \"a\\\n" 6065 "b\\\n" 6066 "c\";", 6067 NoBreak)); 6068 EXPECT_EQ("xxxx =\n" 6069 " \"a\\\n" 6070 "b\\\n" 6071 "c\";", 6072 format("xxxx = \"a\\\n" 6073 "b\\\n" 6074 "c\";", 6075 Break)); 6076 6077 EXPECT_EQ("NSString *const kString =\n" 6078 " @\"aaaa\"\n" 6079 " @\"bbbb\";", 6080 format("NSString *const kString = @\"aaaa\"\n" 6081 "@\"bbbb\";", 6082 Break)); 6083 6084 Break.ColumnLimit = 0; 6085 verifyFormat("const char *hello = \"hello llvm\";", Break); 6086 } 6087 6088 TEST_F(FormatTest, AlignsPipes) { 6089 verifyFormat( 6090 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6091 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6092 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6093 verifyFormat( 6094 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 6095 " << aaaaaaaaaaaaaaaaaaaa;"); 6096 verifyFormat( 6097 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6098 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6099 verifyFormat( 6100 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6101 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6102 verifyFormat( 6103 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 6104 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 6105 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 6106 verifyFormat( 6107 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6108 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6109 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6110 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6111 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6113 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6114 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 6115 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 6116 verifyFormat( 6117 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6118 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6119 verifyFormat( 6120 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 6121 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6122 6123 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 6124 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 6125 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6126 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6127 " aaaaaaaaaaaaaaaaaaaaa)\n" 6128 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6129 verifyFormat("LOG_IF(aaa == //\n" 6130 " bbb)\n" 6131 " << a << b;"); 6132 6133 // But sometimes, breaking before the first "<<" is desirable. 6134 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 6135 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 6136 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 6137 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6138 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6139 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 6140 " << BEF << IsTemplate << Description << E->getType();"); 6141 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 6142 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6143 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6144 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 6145 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6147 " << aaa;"); 6148 6149 verifyFormat( 6150 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6151 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6152 6153 // Incomplete string literal. 6154 EXPECT_EQ("llvm::errs() << \"\n" 6155 " << a;", 6156 format("llvm::errs() << \"\n<<a;")); 6157 6158 verifyFormat("void f() {\n" 6159 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 6160 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 6161 "}"); 6162 6163 // Handle 'endl'. 6164 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 6165 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 6166 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 6167 6168 // Handle '\n'. 6169 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 6170 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 6171 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 6172 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 6173 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 6174 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 6175 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 6176 } 6177 6178 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 6179 verifyFormat("return out << \"somepacket = {\\n\"\n" 6180 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 6181 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 6182 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 6183 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 6184 " << \"}\";"); 6185 6186 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 6187 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 6188 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 6189 verifyFormat( 6190 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 6191 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 6192 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 6193 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 6194 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 6195 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 6196 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6197 verifyFormat( 6198 "void f() {\n" 6199 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 6200 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6201 "}"); 6202 6203 // Breaking before the first "<<" is generally not desirable. 6204 verifyFormat( 6205 "llvm::errs()\n" 6206 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6207 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6208 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6209 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6210 getLLVMStyleWithColumns(70)); 6211 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 6212 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6213 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 6214 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6215 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 6216 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6217 getLLVMStyleWithColumns(70)); 6218 6219 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 6220 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 6221 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 6222 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 6223 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 6224 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 6225 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 6226 " (aaaa + aaaa);", 6227 getLLVMStyleWithColumns(40)); 6228 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 6229 " (aaaaaaa + aaaaa));", 6230 getLLVMStyleWithColumns(40)); 6231 verifyFormat( 6232 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 6233 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 6234 " bbbbbbbbbbbbbbbbbbbbbbb);"); 6235 } 6236 6237 TEST_F(FormatTest, UnderstandsEquals) { 6238 verifyFormat( 6239 "aaaaaaaaaaaaaaaaa =\n" 6240 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6241 verifyFormat( 6242 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6243 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 6244 verifyFormat( 6245 "if (a) {\n" 6246 " f();\n" 6247 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6248 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 6249 "}"); 6250 6251 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6252 " 100000000 + 10000000) {\n}"); 6253 } 6254 6255 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 6256 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 6257 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 6258 6259 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 6260 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 6261 6262 verifyFormat( 6263 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 6264 " Parameter2);"); 6265 6266 verifyFormat( 6267 "ShortObject->shortFunction(\n" 6268 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 6269 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 6270 6271 verifyFormat("loooooooooooooongFunction(\n" 6272 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 6273 6274 verifyFormat( 6275 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 6276 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 6277 6278 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 6279 " .WillRepeatedly(Return(SomeValue));"); 6280 verifyFormat("void f() {\n" 6281 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 6282 " .Times(2)\n" 6283 " .WillRepeatedly(Return(SomeValue));\n" 6284 "}"); 6285 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 6286 " ccccccccccccccccccccccc);"); 6287 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6288 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6289 " .aaaaa(aaaaa),\n" 6290 " aaaaaaaaaaaaaaaaaaaaa);"); 6291 verifyFormat("void f() {\n" 6292 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6293 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 6294 "}"); 6295 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6296 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6297 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6298 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6299 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6300 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6301 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6302 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6303 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 6304 "}"); 6305 6306 // Here, it is not necessary to wrap at "." or "->". 6307 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 6308 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 6309 verifyFormat( 6310 "aaaaaaaaaaa->aaaaaaaaa(\n" 6311 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6312 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 6313 6314 verifyFormat( 6315 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6316 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 6317 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 6318 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 6319 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 6320 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 6321 6322 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6323 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6324 " .a();"); 6325 6326 FormatStyle NoBinPacking = getLLVMStyle(); 6327 NoBinPacking.BinPackParameters = false; 6328 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 6329 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 6330 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 6331 " aaaaaaaaaaaaaaaaaaa,\n" 6332 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6333 NoBinPacking); 6334 6335 // If there is a subsequent call, change to hanging indentation. 6336 verifyFormat( 6337 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6338 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 6339 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6340 verifyFormat( 6341 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6342 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 6343 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6345 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6346 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6347 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6348 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 6349 } 6350 6351 TEST_F(FormatTest, WrapsTemplateDeclarations) { 6352 verifyFormat("template <typename T>\n" 6353 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 6354 verifyFormat("template <typename T>\n" 6355 "// T should be one of {A, B}.\n" 6356 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 6357 verifyFormat( 6358 "template <typename T>\n" 6359 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 6360 verifyFormat("template <typename T>\n" 6361 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 6362 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 6363 verifyFormat( 6364 "template <typename T>\n" 6365 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 6366 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 6367 verifyFormat( 6368 "template <typename T>\n" 6369 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 6370 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 6371 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6372 verifyFormat("template <typename T>\n" 6373 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6374 " int aaaaaaaaaaaaaaaaaaaaaa);"); 6375 verifyFormat( 6376 "template <typename T1, typename T2 = char, typename T3 = char,\n" 6377 " typename T4 = char>\n" 6378 "void f();"); 6379 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 6380 " template <typename> class cccccccccccccccccccccc,\n" 6381 " typename ddddddddddddd>\n" 6382 "class C {};"); 6383 verifyFormat( 6384 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 6385 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6386 6387 verifyFormat("void f() {\n" 6388 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 6389 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 6390 "}"); 6391 6392 verifyFormat("template <typename T> class C {};"); 6393 verifyFormat("template <typename T> void f();"); 6394 verifyFormat("template <typename T> void f() {}"); 6395 verifyFormat( 6396 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 6397 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6398 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 6399 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 6400 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6401 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 6402 " bbbbbbbbbbbbbbbbbbbbbbbb);", 6403 getLLVMStyleWithColumns(72)); 6404 EXPECT_EQ("static_cast<A< //\n" 6405 " B> *>(\n" 6406 "\n" 6407 ");", 6408 format("static_cast<A<//\n" 6409 " B>*>(\n" 6410 "\n" 6411 " );")); 6412 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6413 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 6414 6415 FormatStyle AlwaysBreak = getLLVMStyle(); 6416 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 6417 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 6418 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 6419 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 6420 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6421 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 6422 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 6423 verifyFormat("template <template <typename> class Fooooooo,\n" 6424 " template <typename> class Baaaaaaar>\n" 6425 "struct C {};", 6426 AlwaysBreak); 6427 verifyFormat("template <typename T> // T can be A, B or C.\n" 6428 "struct C {};", 6429 AlwaysBreak); 6430 verifyFormat("template <enum E> class A {\n" 6431 "public:\n" 6432 " E *f();\n" 6433 "};"); 6434 6435 FormatStyle NeverBreak = getLLVMStyle(); 6436 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 6437 verifyFormat("template <typename T> class C {};", NeverBreak); 6438 verifyFormat("template <typename T> void f();", NeverBreak); 6439 verifyFormat("template <typename T> void f() {}", NeverBreak); 6440 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}", 6441 NeverBreak); 6442 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6443 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 6444 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 6445 NeverBreak); 6446 verifyFormat("template <template <typename> class Fooooooo,\n" 6447 " template <typename> class Baaaaaaar>\n" 6448 "struct C {};", 6449 NeverBreak); 6450 verifyFormat("template <typename T> // T can be A, B or C.\n" 6451 "struct C {};", 6452 NeverBreak); 6453 verifyFormat("template <enum E> class A {\n" 6454 "public:\n" 6455 " E *f();\n" 6456 "};", NeverBreak); 6457 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 6458 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbb) {}", 6459 NeverBreak); 6460 } 6461 6462 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 6463 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 6464 Style.ColumnLimit = 60; 6465 EXPECT_EQ("// Baseline - no comments.\n" 6466 "template <\n" 6467 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 6468 "void f() {}", 6469 format("// Baseline - no comments.\n" 6470 "template <\n" 6471 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 6472 "void f() {}", 6473 Style)); 6474 6475 EXPECT_EQ("template <\n" 6476 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 6477 "void f() {}", 6478 format("template <\n" 6479 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 6480 "void f() {}", 6481 Style)); 6482 6483 EXPECT_EQ( 6484 "template <\n" 6485 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 6486 "void f() {}", 6487 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 6488 "void f() {}", 6489 Style)); 6490 6491 EXPECT_EQ( 6492 "template <\n" 6493 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 6494 " // multiline\n" 6495 "void f() {}", 6496 format("template <\n" 6497 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 6498 " // multiline\n" 6499 "void f() {}", 6500 Style)); 6501 6502 EXPECT_EQ( 6503 "template <typename aaaaaaaaaa<\n" 6504 " bbbbbbbbbbbb>::value> // trailing loooong\n" 6505 "void f() {}", 6506 format( 6507 "template <\n" 6508 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 6509 "void f() {}", 6510 Style)); 6511 } 6512 6513 TEST_F(FormatTest, WrapsTemplateParameters) { 6514 FormatStyle Style = getLLVMStyle(); 6515 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6516 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6517 verifyFormat( 6518 "template <typename... a> struct q {};\n" 6519 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 6520 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 6521 " y;", 6522 Style); 6523 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6524 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6525 verifyFormat( 6526 "template <typename... a> struct r {};\n" 6527 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 6528 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 6529 " y;", 6530 Style); 6531 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6532 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6533 verifyFormat( 6534 "template <typename... a> struct s {};\n" 6535 "extern s<\n" 6536 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6537 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 6538 " y;", 6539 Style); 6540 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6541 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6542 verifyFormat( 6543 "template <typename... a> struct t {};\n" 6544 "extern t<\n" 6545 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6546 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 6547 " y;", 6548 Style); 6549 } 6550 6551 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 6552 verifyFormat( 6553 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6554 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6555 verifyFormat( 6556 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6557 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 6559 6560 // FIXME: Should we have the extra indent after the second break? 6561 verifyFormat( 6562 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6563 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6564 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6565 6566 verifyFormat( 6567 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 6568 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 6569 6570 // Breaking at nested name specifiers is generally not desirable. 6571 verifyFormat( 6572 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6573 " aaaaaaaaaaaaaaaaaaaaaaa);"); 6574 6575 verifyFormat( 6576 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 6577 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6579 " aaaaaaaaaaaaaaaaaaaaa);", 6580 getLLVMStyleWithColumns(74)); 6581 6582 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 6583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6584 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6585 } 6586 6587 TEST_F(FormatTest, UnderstandsTemplateParameters) { 6588 verifyFormat("A<int> a;"); 6589 verifyFormat("A<A<A<int>>> a;"); 6590 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 6591 verifyFormat("bool x = a < 1 || 2 > a;"); 6592 verifyFormat("bool x = 5 < f<int>();"); 6593 verifyFormat("bool x = f<int>() > 5;"); 6594 verifyFormat("bool x = 5 < a<int>::x;"); 6595 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 6596 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 6597 6598 verifyGoogleFormat("A<A<int>> a;"); 6599 verifyGoogleFormat("A<A<A<int>>> a;"); 6600 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 6601 verifyGoogleFormat("A<A<int> > a;"); 6602 verifyGoogleFormat("A<A<A<int> > > a;"); 6603 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 6604 verifyGoogleFormat("A<::A<int>> a;"); 6605 verifyGoogleFormat("A<::A> a;"); 6606 verifyGoogleFormat("A< ::A> a;"); 6607 verifyGoogleFormat("A< ::A<int> > a;"); 6608 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 6609 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 6610 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 6611 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 6612 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 6613 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 6614 6615 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 6616 6617 verifyFormat("test >> a >> b;"); 6618 verifyFormat("test << a >> b;"); 6619 6620 verifyFormat("f<int>();"); 6621 verifyFormat("template <typename T> void f() {}"); 6622 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 6623 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 6624 "sizeof(char)>::type>;"); 6625 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 6626 verifyFormat("f(a.operator()<A>());"); 6627 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6628 " .template operator()<A>());", 6629 getLLVMStyleWithColumns(35)); 6630 6631 // Not template parameters. 6632 verifyFormat("return a < b && c > d;"); 6633 verifyFormat("void f() {\n" 6634 " while (a < b && c > d) {\n" 6635 " }\n" 6636 "}"); 6637 verifyFormat("template <typename... Types>\n" 6638 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 6639 6640 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6641 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 6642 getLLVMStyleWithColumns(60)); 6643 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 6644 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 6645 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 6646 } 6647 6648 TEST_F(FormatTest, BitshiftOperatorWidth) { 6649 EXPECT_EQ("int a = 1 << 2; /* foo\n" 6650 " bar */", 6651 format("int a=1<<2; /* foo\n" 6652 " bar */")); 6653 6654 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 6655 " bar */", 6656 format("int b =256>>1 ; /* foo\n" 6657 " bar */")); 6658 } 6659 6660 TEST_F(FormatTest, UnderstandsBinaryOperators) { 6661 verifyFormat("COMPARE(a, ==, b);"); 6662 verifyFormat("auto s = sizeof...(Ts) - 1;"); 6663 } 6664 6665 TEST_F(FormatTest, UnderstandsPointersToMembers) { 6666 verifyFormat("int A::*x;"); 6667 verifyFormat("int (S::*func)(void *);"); 6668 verifyFormat("void f() { int (S::*func)(void *); }"); 6669 verifyFormat("typedef bool *(Class::*Member)() const;"); 6670 verifyFormat("void f() {\n" 6671 " (a->*f)();\n" 6672 " a->*x;\n" 6673 " (a.*f)();\n" 6674 " ((*a).*f)();\n" 6675 " a.*x;\n" 6676 "}"); 6677 verifyFormat("void f() {\n" 6678 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6679 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 6680 "}"); 6681 verifyFormat( 6682 "(aaaaaaaaaa->*bbbbbbb)(\n" 6683 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6684 FormatStyle Style = getLLVMStyle(); 6685 Style.PointerAlignment = FormatStyle::PAS_Left; 6686 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 6687 } 6688 6689 TEST_F(FormatTest, UnderstandsUnaryOperators) { 6690 verifyFormat("int a = -2;"); 6691 verifyFormat("f(-1, -2, -3);"); 6692 verifyFormat("a[-1] = 5;"); 6693 verifyFormat("int a = 5 + -2;"); 6694 verifyFormat("if (i == -1) {\n}"); 6695 verifyFormat("if (i != -1) {\n}"); 6696 verifyFormat("if (i > -1) {\n}"); 6697 verifyFormat("if (i < -1) {\n}"); 6698 verifyFormat("++(a->f());"); 6699 verifyFormat("--(a->f());"); 6700 verifyFormat("(a->f())++;"); 6701 verifyFormat("a[42]++;"); 6702 verifyFormat("if (!(a->f())) {\n}"); 6703 verifyFormat("if (!+i) {\n}"); 6704 verifyFormat("~&a;"); 6705 6706 verifyFormat("a-- > b;"); 6707 verifyFormat("b ? -a : c;"); 6708 verifyFormat("n * sizeof char16;"); 6709 verifyFormat("n * alignof char16;", getGoogleStyle()); 6710 verifyFormat("sizeof(char);"); 6711 verifyFormat("alignof(char);", getGoogleStyle()); 6712 6713 verifyFormat("return -1;"); 6714 verifyFormat("switch (a) {\n" 6715 "case -1:\n" 6716 " break;\n" 6717 "}"); 6718 verifyFormat("#define X -1"); 6719 verifyFormat("#define X -kConstant"); 6720 6721 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 6722 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 6723 6724 verifyFormat("int a = /* confusing comment */ -1;"); 6725 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 6726 verifyFormat("int a = i /* confusing comment */++;"); 6727 } 6728 6729 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 6730 verifyFormat("if (!aaaaaaaaaa( // break\n" 6731 " aaaaa)) {\n" 6732 "}"); 6733 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 6734 " aaaaa));"); 6735 verifyFormat("*aaa = aaaaaaa( // break\n" 6736 " bbbbbb);"); 6737 } 6738 6739 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 6740 verifyFormat("bool operator<();"); 6741 verifyFormat("bool operator>();"); 6742 verifyFormat("bool operator=();"); 6743 verifyFormat("bool operator==();"); 6744 verifyFormat("bool operator!=();"); 6745 verifyFormat("int operator+();"); 6746 verifyFormat("int operator++();"); 6747 verifyFormat("int operator++(int) volatile noexcept;"); 6748 verifyFormat("bool operator,();"); 6749 verifyFormat("bool operator();"); 6750 verifyFormat("bool operator()();"); 6751 verifyFormat("bool operator[]();"); 6752 verifyFormat("operator bool();"); 6753 verifyFormat("operator int();"); 6754 verifyFormat("operator void *();"); 6755 verifyFormat("operator SomeType<int>();"); 6756 verifyFormat("operator SomeType<int, int>();"); 6757 verifyFormat("operator SomeType<SomeType<int>>();"); 6758 verifyFormat("void *operator new(std::size_t size);"); 6759 verifyFormat("void *operator new[](std::size_t size);"); 6760 verifyFormat("void operator delete(void *ptr);"); 6761 verifyFormat("void operator delete[](void *ptr);"); 6762 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 6763 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 6764 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 6765 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 6766 6767 verifyFormat( 6768 "ostream &operator<<(ostream &OutputStream,\n" 6769 " SomeReallyLongType WithSomeReallyLongValue);"); 6770 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 6771 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 6772 " return left.group < right.group;\n" 6773 "}"); 6774 verifyFormat("SomeType &operator=(const SomeType &S);"); 6775 verifyFormat("f.template operator()<int>();"); 6776 6777 verifyGoogleFormat("operator void*();"); 6778 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 6779 verifyGoogleFormat("operator ::A();"); 6780 6781 verifyFormat("using A::operator+;"); 6782 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 6783 "int i;"); 6784 } 6785 6786 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 6787 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 6788 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 6789 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 6790 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 6791 verifyFormat("Deleted &operator=(const Deleted &) &;"); 6792 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 6793 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 6794 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 6795 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 6796 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 6797 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 6798 verifyFormat("void Fn(T const &) const &;"); 6799 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 6800 verifyFormat("template <typename T>\n" 6801 "void F(T) && = delete;", 6802 getGoogleStyle()); 6803 6804 FormatStyle AlignLeft = getLLVMStyle(); 6805 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 6806 verifyFormat("void A::b() && {}", AlignLeft); 6807 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 6808 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 6809 AlignLeft); 6810 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 6811 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 6812 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 6813 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 6814 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 6815 verifyFormat("auto Function(T) & -> void;", AlignLeft); 6816 verifyFormat("void Fn(T const&) const&;", AlignLeft); 6817 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 6818 6819 FormatStyle Spaces = getLLVMStyle(); 6820 Spaces.SpacesInCStyleCastParentheses = true; 6821 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 6822 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 6823 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 6824 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 6825 6826 Spaces.SpacesInCStyleCastParentheses = false; 6827 Spaces.SpacesInParentheses = true; 6828 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 6829 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 6830 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 6831 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 6832 } 6833 6834 TEST_F(FormatTest, UnderstandsNewAndDelete) { 6835 verifyFormat("void f() {\n" 6836 " A *a = new A;\n" 6837 " A *a = new (placement) A;\n" 6838 " delete a;\n" 6839 " delete (A *)a;\n" 6840 "}"); 6841 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 6842 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 6843 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6844 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 6845 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 6846 verifyFormat("delete[] h->p;"); 6847 } 6848 6849 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 6850 verifyFormat("int *f(int *a) {}"); 6851 verifyFormat("int main(int argc, char **argv) {}"); 6852 verifyFormat("Test::Test(int b) : a(b * b) {}"); 6853 verifyIndependentOfContext("f(a, *a);"); 6854 verifyFormat("void g() { f(*a); }"); 6855 verifyIndependentOfContext("int a = b * 10;"); 6856 verifyIndependentOfContext("int a = 10 * b;"); 6857 verifyIndependentOfContext("int a = b * c;"); 6858 verifyIndependentOfContext("int a += b * c;"); 6859 verifyIndependentOfContext("int a -= b * c;"); 6860 verifyIndependentOfContext("int a *= b * c;"); 6861 verifyIndependentOfContext("int a /= b * c;"); 6862 verifyIndependentOfContext("int a = *b;"); 6863 verifyIndependentOfContext("int a = *b * c;"); 6864 verifyIndependentOfContext("int a = b * *c;"); 6865 verifyIndependentOfContext("int a = b * (10);"); 6866 verifyIndependentOfContext("S << b * (10);"); 6867 verifyIndependentOfContext("return 10 * b;"); 6868 verifyIndependentOfContext("return *b * *c;"); 6869 verifyIndependentOfContext("return a & ~b;"); 6870 verifyIndependentOfContext("f(b ? *c : *d);"); 6871 verifyIndependentOfContext("int a = b ? *c : *d;"); 6872 verifyIndependentOfContext("*b = a;"); 6873 verifyIndependentOfContext("a * ~b;"); 6874 verifyIndependentOfContext("a * !b;"); 6875 verifyIndependentOfContext("a * +b;"); 6876 verifyIndependentOfContext("a * -b;"); 6877 verifyIndependentOfContext("a * ++b;"); 6878 verifyIndependentOfContext("a * --b;"); 6879 verifyIndependentOfContext("a[4] * b;"); 6880 verifyIndependentOfContext("a[a * a] = 1;"); 6881 verifyIndependentOfContext("f() * b;"); 6882 verifyIndependentOfContext("a * [self dostuff];"); 6883 verifyIndependentOfContext("int x = a * (a + b);"); 6884 verifyIndependentOfContext("(a *)(a + b);"); 6885 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 6886 verifyIndependentOfContext("int *pa = (int *)&a;"); 6887 verifyIndependentOfContext("return sizeof(int **);"); 6888 verifyIndependentOfContext("return sizeof(int ******);"); 6889 verifyIndependentOfContext("return (int **&)a;"); 6890 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 6891 verifyFormat("void f(Type (*parameter)[10]) {}"); 6892 verifyFormat("void f(Type (¶meter)[10]) {}"); 6893 verifyGoogleFormat("return sizeof(int**);"); 6894 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 6895 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 6896 verifyFormat("auto a = [](int **&, int ***) {};"); 6897 verifyFormat("auto PointerBinding = [](const char *S) {};"); 6898 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 6899 verifyFormat("[](const decltype(*a) &value) {}"); 6900 verifyFormat("decltype(a * b) F();"); 6901 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 6902 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 6903 verifyIndependentOfContext("typedef void (*f)(int *a);"); 6904 verifyIndependentOfContext("int i{a * b};"); 6905 verifyIndependentOfContext("aaa && aaa->f();"); 6906 verifyIndependentOfContext("int x = ~*p;"); 6907 verifyFormat("Constructor() : a(a), area(width * height) {}"); 6908 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 6909 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 6910 verifyFormat("void f() { f(a, c * d); }"); 6911 verifyFormat("void f() { f(new a(), c * d); }"); 6912 verifyFormat("void f(const MyOverride &override);"); 6913 verifyFormat("void f(const MyFinal &final);"); 6914 verifyIndependentOfContext("bool a = f() && override.f();"); 6915 verifyIndependentOfContext("bool a = f() && final.f();"); 6916 6917 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 6918 6919 verifyIndependentOfContext("A<int *> a;"); 6920 verifyIndependentOfContext("A<int **> a;"); 6921 verifyIndependentOfContext("A<int *, int *> a;"); 6922 verifyIndependentOfContext("A<int *[]> a;"); 6923 verifyIndependentOfContext( 6924 "const char *const p = reinterpret_cast<const char *const>(q);"); 6925 verifyIndependentOfContext("A<int **, int **> a;"); 6926 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 6927 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 6928 verifyFormat("for (; a && b;) {\n}"); 6929 verifyFormat("bool foo = true && [] { return false; }();"); 6930 6931 verifyFormat( 6932 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6934 6935 verifyGoogleFormat("int const* a = &b;"); 6936 verifyGoogleFormat("**outparam = 1;"); 6937 verifyGoogleFormat("*outparam = a * b;"); 6938 verifyGoogleFormat("int main(int argc, char** argv) {}"); 6939 verifyGoogleFormat("A<int*> a;"); 6940 verifyGoogleFormat("A<int**> a;"); 6941 verifyGoogleFormat("A<int*, int*> a;"); 6942 verifyGoogleFormat("A<int**, int**> a;"); 6943 verifyGoogleFormat("f(b ? *c : *d);"); 6944 verifyGoogleFormat("int a = b ? *c : *d;"); 6945 verifyGoogleFormat("Type* t = **x;"); 6946 verifyGoogleFormat("Type* t = *++*x;"); 6947 verifyGoogleFormat("*++*x;"); 6948 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 6949 verifyGoogleFormat("Type* t = x++ * y;"); 6950 verifyGoogleFormat( 6951 "const char* const p = reinterpret_cast<const char* const>(q);"); 6952 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 6953 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 6954 verifyGoogleFormat("template <typename T>\n" 6955 "void f(int i = 0, SomeType** temps = NULL);"); 6956 6957 FormatStyle Left = getLLVMStyle(); 6958 Left.PointerAlignment = FormatStyle::PAS_Left; 6959 verifyFormat("x = *a(x) = *a(y);", Left); 6960 verifyFormat("for (;; *a = b) {\n}", Left); 6961 verifyFormat("return *this += 1;", Left); 6962 verifyFormat("throw *x;", Left); 6963 verifyFormat("delete *x;", Left); 6964 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 6965 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 6966 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 6967 6968 verifyIndependentOfContext("a = *(x + y);"); 6969 verifyIndependentOfContext("a = &(x + y);"); 6970 verifyIndependentOfContext("*(x + y).call();"); 6971 verifyIndependentOfContext("&(x + y)->call();"); 6972 verifyFormat("void f() { &(*I).first; }"); 6973 6974 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 6975 verifyFormat( 6976 "int *MyValues = {\n" 6977 " *A, // Operator detection might be confused by the '{'\n" 6978 " *BB // Operator detection might be confused by previous comment\n" 6979 "};"); 6980 6981 verifyIndependentOfContext("if (int *a = &b)"); 6982 verifyIndependentOfContext("if (int &a = *b)"); 6983 verifyIndependentOfContext("if (a & b[i])"); 6984 verifyIndependentOfContext("if constexpr (a & b[i])"); 6985 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 6986 verifyIndependentOfContext("if (a * (b * c))"); 6987 verifyIndependentOfContext("if constexpr (a * (b * c))"); 6988 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 6989 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 6990 verifyIndependentOfContext("if (*b[i])"); 6991 verifyIndependentOfContext("if (int *a = (&b))"); 6992 verifyIndependentOfContext("while (int *a = &b)"); 6993 verifyIndependentOfContext("while (a * (b * c))"); 6994 verifyIndependentOfContext("size = sizeof *a;"); 6995 verifyIndependentOfContext("if (a && (b = c))"); 6996 verifyFormat("void f() {\n" 6997 " for (const int &v : Values) {\n" 6998 " }\n" 6999 "}"); 7000 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 7001 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 7002 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 7003 7004 verifyFormat("#define A (!a * b)"); 7005 verifyFormat("#define MACRO \\\n" 7006 " int *i = a * b; \\\n" 7007 " void f(a *b);", 7008 getLLVMStyleWithColumns(19)); 7009 7010 verifyIndependentOfContext("A = new SomeType *[Length];"); 7011 verifyIndependentOfContext("A = new SomeType *[Length]();"); 7012 verifyIndependentOfContext("T **t = new T *;"); 7013 verifyIndependentOfContext("T **t = new T *();"); 7014 verifyGoogleFormat("A = new SomeType*[Length]();"); 7015 verifyGoogleFormat("A = new SomeType*[Length];"); 7016 verifyGoogleFormat("T** t = new T*;"); 7017 verifyGoogleFormat("T** t = new T*();"); 7018 7019 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 7020 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 7021 verifyFormat("template <bool a, bool b> " 7022 "typename t::if<x && y>::type f() {}"); 7023 verifyFormat("template <int *y> f() {}"); 7024 verifyFormat("vector<int *> v;"); 7025 verifyFormat("vector<int *const> v;"); 7026 verifyFormat("vector<int *const **const *> v;"); 7027 verifyFormat("vector<int *volatile> v;"); 7028 verifyFormat("vector<a * b> v;"); 7029 verifyFormat("foo<b && false>();"); 7030 verifyFormat("foo<b & 1>();"); 7031 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 7032 verifyFormat( 7033 "template <class T, class = typename std::enable_if<\n" 7034 " std::is_integral<T>::value &&\n" 7035 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 7036 "void F();", 7037 getLLVMStyleWithColumns(70)); 7038 verifyFormat( 7039 "template <class T,\n" 7040 " class = typename std::enable_if<\n" 7041 " std::is_integral<T>::value &&\n" 7042 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 7043 " class U>\n" 7044 "void F();", 7045 getLLVMStyleWithColumns(70)); 7046 verifyFormat( 7047 "template <class T,\n" 7048 " class = typename ::std::enable_if<\n" 7049 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 7050 "void F();", 7051 getGoogleStyleWithColumns(68)); 7052 7053 verifyIndependentOfContext("MACRO(int *i);"); 7054 verifyIndependentOfContext("MACRO(auto *a);"); 7055 verifyIndependentOfContext("MACRO(const A *a);"); 7056 verifyIndependentOfContext("MACRO(A *const a);"); 7057 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 7058 verifyFormat("void f() { f(float{1}, a * a); }"); 7059 // FIXME: Is there a way to make this work? 7060 // verifyIndependentOfContext("MACRO(A *a);"); 7061 7062 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 7063 verifyFormat("return options != nullptr && operator==(*options);"); 7064 7065 EXPECT_EQ("#define OP(x) \\\n" 7066 " ostream &operator<<(ostream &s, const A &a) { \\\n" 7067 " return s << a.DebugString(); \\\n" 7068 " }", 7069 format("#define OP(x) \\\n" 7070 " ostream &operator<<(ostream &s, const A &a) { \\\n" 7071 " return s << a.DebugString(); \\\n" 7072 " }", 7073 getLLVMStyleWithColumns(50))); 7074 7075 // FIXME: We cannot handle this case yet; we might be able to figure out that 7076 // foo<x> d > v; doesn't make sense. 7077 verifyFormat("foo<a<b && c> d> v;"); 7078 7079 FormatStyle PointerMiddle = getLLVMStyle(); 7080 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 7081 verifyFormat("delete *x;", PointerMiddle); 7082 verifyFormat("int * x;", PointerMiddle); 7083 verifyFormat("int *[] x;", PointerMiddle); 7084 verifyFormat("template <int * y> f() {}", PointerMiddle); 7085 verifyFormat("int * f(int * a) {}", PointerMiddle); 7086 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 7087 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 7088 verifyFormat("A<int *> a;", PointerMiddle); 7089 verifyFormat("A<int **> a;", PointerMiddle); 7090 verifyFormat("A<int *, int *> a;", PointerMiddle); 7091 verifyFormat("A<int *[]> a;", PointerMiddle); 7092 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 7093 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 7094 verifyFormat("T ** t = new T *;", PointerMiddle); 7095 7096 // Member function reference qualifiers aren't binary operators. 7097 verifyFormat("string // break\n" 7098 "operator()() & {}"); 7099 verifyFormat("string // break\n" 7100 "operator()() && {}"); 7101 verifyGoogleFormat("template <typename T>\n" 7102 "auto x() & -> int {}"); 7103 } 7104 7105 TEST_F(FormatTest, UnderstandsAttributes) { 7106 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 7107 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 7108 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 7109 FormatStyle AfterType = getLLVMStyle(); 7110 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 7111 verifyFormat("__attribute__((nodebug)) void\n" 7112 "foo() {}\n", 7113 AfterType); 7114 } 7115 7116 TEST_F(FormatTest, UnderstandsSquareAttributes) { 7117 verifyFormat("SomeType s [[unused]] (InitValue);"); 7118 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 7119 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 7120 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 7121 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 7122 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7123 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 7124 7125 // Make sure we do not mistake attributes for array subscripts. 7126 verifyFormat("int a() {}\n" 7127 "[[unused]] int b() {}\n"); 7128 verifyFormat("NSArray *arr;\n" 7129 "arr[[Foo() bar]];"); 7130 7131 // On the other hand, we still need to correctly find array subscripts. 7132 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 7133 7134 // Make sure that we do not mistake Objective-C method inside array literals 7135 // as attributes, even if those method names are also keywords. 7136 verifyFormat("@[ [foo bar] ];"); 7137 verifyFormat("@[ [NSArray class] ];"); 7138 verifyFormat("@[ [foo enum] ];"); 7139 7140 // Make sure we do not parse attributes as lambda introducers. 7141 FormatStyle MultiLineFunctions = getLLVMStyle(); 7142 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7143 verifyFormat("[[unused]] int b() {\n" 7144 " return 42;\n" 7145 "}\n", 7146 MultiLineFunctions); 7147 } 7148 7149 TEST_F(FormatTest, UnderstandsEllipsis) { 7150 verifyFormat("int printf(const char *fmt, ...);"); 7151 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 7152 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 7153 7154 FormatStyle PointersLeft = getLLVMStyle(); 7155 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 7156 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 7157 } 7158 7159 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 7160 EXPECT_EQ("int *a;\n" 7161 "int *a;\n" 7162 "int *a;", 7163 format("int *a;\n" 7164 "int* a;\n" 7165 "int *a;", 7166 getGoogleStyle())); 7167 EXPECT_EQ("int* a;\n" 7168 "int* a;\n" 7169 "int* a;", 7170 format("int* a;\n" 7171 "int* a;\n" 7172 "int *a;", 7173 getGoogleStyle())); 7174 EXPECT_EQ("int *a;\n" 7175 "int *a;\n" 7176 "int *a;", 7177 format("int *a;\n" 7178 "int * a;\n" 7179 "int * a;", 7180 getGoogleStyle())); 7181 EXPECT_EQ("auto x = [] {\n" 7182 " int *a;\n" 7183 " int *a;\n" 7184 " int *a;\n" 7185 "};", 7186 format("auto x=[]{int *a;\n" 7187 "int * a;\n" 7188 "int * a;};", 7189 getGoogleStyle())); 7190 } 7191 7192 TEST_F(FormatTest, UnderstandsRvalueReferences) { 7193 verifyFormat("int f(int &&a) {}"); 7194 verifyFormat("int f(int a, char &&b) {}"); 7195 verifyFormat("void f() { int &&a = b; }"); 7196 verifyGoogleFormat("int f(int a, char&& b) {}"); 7197 verifyGoogleFormat("void f() { int&& a = b; }"); 7198 7199 verifyIndependentOfContext("A<int &&> a;"); 7200 verifyIndependentOfContext("A<int &&, int &&> a;"); 7201 verifyGoogleFormat("A<int&&> a;"); 7202 verifyGoogleFormat("A<int&&, int&&> a;"); 7203 7204 // Not rvalue references: 7205 verifyFormat("template <bool B, bool C> class A {\n" 7206 " static_assert(B && C, \"Something is wrong\");\n" 7207 "};"); 7208 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 7209 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 7210 verifyFormat("#define A(a, b) (a && b)"); 7211 } 7212 7213 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 7214 verifyFormat("void f() {\n" 7215 " x[aaaaaaaaa -\n" 7216 " b] = 23;\n" 7217 "}", 7218 getLLVMStyleWithColumns(15)); 7219 } 7220 7221 TEST_F(FormatTest, FormatsCasts) { 7222 verifyFormat("Type *A = static_cast<Type *>(P);"); 7223 verifyFormat("Type *A = (Type *)P;"); 7224 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 7225 verifyFormat("int a = (int)(2.0f);"); 7226 verifyFormat("int a = (int)2.0f;"); 7227 verifyFormat("x[(int32)y];"); 7228 verifyFormat("x = (int32)y;"); 7229 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 7230 verifyFormat("int a = (int)*b;"); 7231 verifyFormat("int a = (int)2.0f;"); 7232 verifyFormat("int a = (int)~0;"); 7233 verifyFormat("int a = (int)++a;"); 7234 verifyFormat("int a = (int)sizeof(int);"); 7235 verifyFormat("int a = (int)+2;"); 7236 verifyFormat("my_int a = (my_int)2.0f;"); 7237 verifyFormat("my_int a = (my_int)sizeof(int);"); 7238 verifyFormat("return (my_int)aaa;"); 7239 verifyFormat("#define x ((int)-1)"); 7240 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 7241 verifyFormat("#define p(q) ((int *)&q)"); 7242 verifyFormat("fn(a)(b) + 1;"); 7243 7244 verifyFormat("void f() { my_int a = (my_int)*b; }"); 7245 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 7246 verifyFormat("my_int a = (my_int)~0;"); 7247 verifyFormat("my_int a = (my_int)++a;"); 7248 verifyFormat("my_int a = (my_int)-2;"); 7249 verifyFormat("my_int a = (my_int)1;"); 7250 verifyFormat("my_int a = (my_int *)1;"); 7251 verifyFormat("my_int a = (const my_int)-1;"); 7252 verifyFormat("my_int a = (const my_int *)-1;"); 7253 verifyFormat("my_int a = (my_int)(my_int)-1;"); 7254 verifyFormat("my_int a = (ns::my_int)-2;"); 7255 verifyFormat("case (my_int)ONE:"); 7256 verifyFormat("auto x = (X)this;"); 7257 7258 // FIXME: single value wrapped with paren will be treated as cast. 7259 verifyFormat("void f(int i = (kValue)*kMask) {}"); 7260 7261 verifyFormat("{ (void)F; }"); 7262 7263 // Don't break after a cast's 7264 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7265 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 7266 " bbbbbbbbbbbbbbbbbbbbbb);"); 7267 7268 // These are not casts. 7269 verifyFormat("void f(int *) {}"); 7270 verifyFormat("f(foo)->b;"); 7271 verifyFormat("f(foo).b;"); 7272 verifyFormat("f(foo)(b);"); 7273 verifyFormat("f(foo)[b];"); 7274 verifyFormat("[](foo) { return 4; }(bar);"); 7275 verifyFormat("(*funptr)(foo)[4];"); 7276 verifyFormat("funptrs[4](foo)[4];"); 7277 verifyFormat("void f(int *);"); 7278 verifyFormat("void f(int *) = 0;"); 7279 verifyFormat("void f(SmallVector<int>) {}"); 7280 verifyFormat("void f(SmallVector<int>);"); 7281 verifyFormat("void f(SmallVector<int>) = 0;"); 7282 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 7283 verifyFormat("int a = sizeof(int) * b;"); 7284 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 7285 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 7286 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 7287 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 7288 7289 // These are not casts, but at some point were confused with casts. 7290 verifyFormat("virtual void foo(int *) override;"); 7291 verifyFormat("virtual void foo(char &) const;"); 7292 verifyFormat("virtual void foo(int *a, char *) const;"); 7293 verifyFormat("int a = sizeof(int *) + b;"); 7294 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 7295 verifyFormat("bool b = f(g<int>) && c;"); 7296 verifyFormat("typedef void (*f)(int i) func;"); 7297 7298 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 7299 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 7300 // FIXME: The indentation here is not ideal. 7301 verifyFormat( 7302 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7303 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 7304 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 7305 } 7306 7307 TEST_F(FormatTest, FormatsFunctionTypes) { 7308 verifyFormat("A<bool()> a;"); 7309 verifyFormat("A<SomeType()> a;"); 7310 verifyFormat("A<void (*)(int, std::string)> a;"); 7311 verifyFormat("A<void *(int)>;"); 7312 verifyFormat("void *(*a)(int *, SomeType *);"); 7313 verifyFormat("int (*func)(void *);"); 7314 verifyFormat("void f() { int (*func)(void *); }"); 7315 verifyFormat("template <class CallbackClass>\n" 7316 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 7317 7318 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 7319 verifyGoogleFormat("void* (*a)(int);"); 7320 verifyGoogleFormat( 7321 "template <class CallbackClass>\n" 7322 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 7323 7324 // Other constructs can look somewhat like function types: 7325 verifyFormat("A<sizeof(*x)> a;"); 7326 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 7327 verifyFormat("some_var = function(*some_pointer_var)[0];"); 7328 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 7329 verifyFormat("int x = f(&h)();"); 7330 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 7331 verifyFormat("std::function<\n" 7332 " LooooooooooongTemplatedType<\n" 7333 " SomeType>*(\n" 7334 " LooooooooooooooooongType type)>\n" 7335 " function;", 7336 getGoogleStyleWithColumns(40)); 7337 } 7338 7339 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 7340 verifyFormat("A (*foo_)[6];"); 7341 verifyFormat("vector<int> (*foo_)[6];"); 7342 } 7343 7344 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 7345 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7346 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 7347 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 7348 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 7349 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7350 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 7351 7352 // Different ways of ()-initializiation. 7353 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7354 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 7355 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7356 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 7357 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7358 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 7359 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 7360 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 7361 7362 // Lambdas should not confuse the variable declaration heuristic. 7363 verifyFormat("LooooooooooooooooongType\n" 7364 " variable(nullptr, [](A *a) {});", 7365 getLLVMStyleWithColumns(40)); 7366 } 7367 7368 TEST_F(FormatTest, BreaksLongDeclarations) { 7369 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 7370 " AnotherNameForTheLongType;"); 7371 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 7372 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7373 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7374 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 7375 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 7376 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 7377 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7378 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 7379 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 7380 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 7381 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 7382 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 7383 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 7384 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 7385 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7386 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 7387 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7388 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 7389 FormatStyle Indented = getLLVMStyle(); 7390 Indented.IndentWrappedFunctionNames = true; 7391 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7392 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 7393 Indented); 7394 verifyFormat( 7395 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 7396 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 7397 Indented); 7398 verifyFormat( 7399 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 7400 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 7401 Indented); 7402 verifyFormat( 7403 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 7404 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 7405 Indented); 7406 7407 // FIXME: Without the comment, this breaks after "(". 7408 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 7409 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 7410 getGoogleStyle()); 7411 7412 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 7413 " int LoooooooooooooooooooongParam2) {}"); 7414 verifyFormat( 7415 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 7416 " SourceLocation L, IdentifierIn *II,\n" 7417 " Type *T) {}"); 7418 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 7419 "ReallyReaaallyLongFunctionName(\n" 7420 " const std::string &SomeParameter,\n" 7421 " const SomeType<string, SomeOtherTemplateParameter>\n" 7422 " &ReallyReallyLongParameterName,\n" 7423 " const SomeType<string, SomeOtherTemplateParameter>\n" 7424 " &AnotherLongParameterName) {}"); 7425 verifyFormat("template <typename A>\n" 7426 "SomeLoooooooooooooooooooooongType<\n" 7427 " typename some_namespace::SomeOtherType<A>::Type>\n" 7428 "Function() {}"); 7429 7430 verifyGoogleFormat( 7431 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 7432 " aaaaaaaaaaaaaaaaaaaaaaa;"); 7433 verifyGoogleFormat( 7434 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 7435 " SourceLocation L) {}"); 7436 verifyGoogleFormat( 7437 "some_namespace::LongReturnType\n" 7438 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 7439 " int first_long_parameter, int second_parameter) {}"); 7440 7441 verifyGoogleFormat("template <typename T>\n" 7442 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 7443 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 7444 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7445 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 7446 7447 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 7448 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7449 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7450 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7451 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 7452 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 7453 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7454 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 7456 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7457 7458 verifyFormat("template <typename T> // Templates on own line.\n" 7459 "static int // Some comment.\n" 7460 "MyFunction(int a);", 7461 getLLVMStyle()); 7462 } 7463 7464 TEST_F(FormatTest, FormatsArrays) { 7465 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 7466 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 7467 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 7468 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 7469 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 7470 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 7471 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7472 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 7473 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7474 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 7475 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7476 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 7477 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 7478 verifyFormat( 7479 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 7480 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 7481 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7482 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 7483 " .aaaaaaaaaaaaaaaaaaaaaa();"); 7484 7485 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 7486 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 7487 verifyFormat( 7488 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 7489 " .aaaaaaa[0]\n" 7490 " .aaaaaaaaaaaaaaaaaaaaaa();"); 7491 verifyFormat("a[::b::c];"); 7492 7493 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 7494 7495 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 7496 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 7497 } 7498 7499 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 7500 verifyFormat("(a)->b();"); 7501 verifyFormat("--a;"); 7502 } 7503 7504 TEST_F(FormatTest, HandlesIncludeDirectives) { 7505 verifyFormat("#include <string>\n" 7506 "#include <a/b/c.h>\n" 7507 "#include \"a/b/string\"\n" 7508 "#include \"string.h\"\n" 7509 "#include \"string.h\"\n" 7510 "#include <a-a>\n" 7511 "#include < path with space >\n" 7512 "#include_next <test.h>" 7513 "#include \"abc.h\" // this is included for ABC\n" 7514 "#include \"some long include\" // with a comment\n" 7515 "#include \"some very long include path\"\n" 7516 "#include <some/very/long/include/path>\n", 7517 getLLVMStyleWithColumns(35)); 7518 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 7519 EXPECT_EQ("#include <a>", format("#include<a>")); 7520 7521 verifyFormat("#import <string>"); 7522 verifyFormat("#import <a/b/c.h>"); 7523 verifyFormat("#import \"a/b/string\""); 7524 verifyFormat("#import \"string.h\""); 7525 verifyFormat("#import \"string.h\""); 7526 verifyFormat("#if __has_include(<strstream>)\n" 7527 "#include <strstream>\n" 7528 "#endif"); 7529 7530 verifyFormat("#define MY_IMPORT <a/b>"); 7531 7532 verifyFormat("#if __has_include(<a/b>)"); 7533 verifyFormat("#if __has_include_next(<a/b>)"); 7534 verifyFormat("#define F __has_include(<a/b>)"); 7535 verifyFormat("#define F __has_include_next(<a/b>)"); 7536 7537 // Protocol buffer definition or missing "#". 7538 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 7539 getLLVMStyleWithColumns(30)); 7540 7541 FormatStyle Style = getLLVMStyle(); 7542 Style.AlwaysBreakBeforeMultilineStrings = true; 7543 Style.ColumnLimit = 0; 7544 verifyFormat("#import \"abc.h\"", Style); 7545 7546 // But 'import' might also be a regular C++ namespace. 7547 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7549 } 7550 7551 //===----------------------------------------------------------------------===// 7552 // Error recovery tests. 7553 //===----------------------------------------------------------------------===// 7554 7555 TEST_F(FormatTest, IncompleteParameterLists) { 7556 FormatStyle NoBinPacking = getLLVMStyle(); 7557 NoBinPacking.BinPackParameters = false; 7558 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 7559 " double *min_x,\n" 7560 " double *max_x,\n" 7561 " double *min_y,\n" 7562 " double *max_y,\n" 7563 " double *min_z,\n" 7564 " double *max_z, ) {}", 7565 NoBinPacking); 7566 } 7567 7568 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 7569 verifyFormat("void f() { return; }\n42"); 7570 verifyFormat("void f() {\n" 7571 " if (0)\n" 7572 " return;\n" 7573 "}\n" 7574 "42"); 7575 verifyFormat("void f() { return }\n42"); 7576 verifyFormat("void f() {\n" 7577 " if (0)\n" 7578 " return\n" 7579 "}\n" 7580 "42"); 7581 } 7582 7583 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 7584 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 7585 EXPECT_EQ("void f() {\n" 7586 " if (a)\n" 7587 " return\n" 7588 "}", 7589 format("void f ( ) { if ( a ) return }")); 7590 EXPECT_EQ("namespace N {\n" 7591 "void f()\n" 7592 "}", 7593 format("namespace N { void f() }")); 7594 EXPECT_EQ("namespace N {\n" 7595 "void f() {}\n" 7596 "void g()\n" 7597 "} // namespace N", 7598 format("namespace N { void f( ) { } void g( ) }")); 7599 } 7600 7601 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 7602 verifyFormat("int aaaaaaaa =\n" 7603 " // Overlylongcomment\n" 7604 " b;", 7605 getLLVMStyleWithColumns(20)); 7606 verifyFormat("function(\n" 7607 " ShortArgument,\n" 7608 " LoooooooooooongArgument);\n", 7609 getLLVMStyleWithColumns(20)); 7610 } 7611 7612 TEST_F(FormatTest, IncorrectAccessSpecifier) { 7613 verifyFormat("public:"); 7614 verifyFormat("class A {\n" 7615 "public\n" 7616 " void f() {}\n" 7617 "};"); 7618 verifyFormat("public\n" 7619 "int qwerty;"); 7620 verifyFormat("public\n" 7621 "B {}"); 7622 verifyFormat("public\n" 7623 "{}"); 7624 verifyFormat("public\n" 7625 "B { int x; }"); 7626 } 7627 7628 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 7629 verifyFormat("{"); 7630 verifyFormat("#})"); 7631 verifyNoCrash("(/**/[:!] ?[)."); 7632 } 7633 7634 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 7635 // Found by oss-fuzz: 7636 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 7637 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 7638 Style.ColumnLimit = 60; 7639 verifyNoCrash( 7640 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 7641 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 7642 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 7643 Style); 7644 } 7645 7646 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 7647 verifyFormat("do {\n}"); 7648 verifyFormat("do {\n}\n" 7649 "f();"); 7650 verifyFormat("do {\n}\n" 7651 "wheeee(fun);"); 7652 verifyFormat("do {\n" 7653 " f();\n" 7654 "}"); 7655 } 7656 7657 TEST_F(FormatTest, IncorrectCodeMissingParens) { 7658 verifyFormat("if {\n foo;\n foo();\n}"); 7659 verifyFormat("switch {\n foo;\n foo();\n}"); 7660 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 7661 verifyFormat("while {\n foo;\n foo();\n}"); 7662 verifyFormat("do {\n foo;\n foo();\n} while;"); 7663 } 7664 7665 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 7666 verifyIncompleteFormat("namespace {\n" 7667 "class Foo { Foo (\n" 7668 "};\n" 7669 "} // namespace"); 7670 } 7671 7672 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 7673 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 7674 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 7675 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 7676 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 7677 7678 EXPECT_EQ("{\n" 7679 " {\n" 7680 " breakme(\n" 7681 " qwe);\n" 7682 " }\n", 7683 format("{\n" 7684 " {\n" 7685 " breakme(qwe);\n" 7686 "}\n", 7687 getLLVMStyleWithColumns(10))); 7688 } 7689 7690 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 7691 verifyFormat("int x = {\n" 7692 " avariable,\n" 7693 " b(alongervariable)};", 7694 getLLVMStyleWithColumns(25)); 7695 } 7696 7697 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 7698 verifyFormat("return (a)(b){1, 2, 3};"); 7699 } 7700 7701 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 7702 verifyFormat("vector<int> x{1, 2, 3, 4};"); 7703 verifyFormat("vector<int> x{\n" 7704 " 1,\n" 7705 " 2,\n" 7706 " 3,\n" 7707 " 4,\n" 7708 "};"); 7709 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 7710 verifyFormat("f({1, 2});"); 7711 verifyFormat("auto v = Foo{-1};"); 7712 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 7713 verifyFormat("Class::Class : member{1, 2, 3} {}"); 7714 verifyFormat("new vector<int>{1, 2, 3};"); 7715 verifyFormat("new int[3]{1, 2, 3};"); 7716 verifyFormat("new int{1};"); 7717 verifyFormat("return {arg1, arg2};"); 7718 verifyFormat("return {arg1, SomeType{parameter}};"); 7719 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 7720 verifyFormat("new T{arg1, arg2};"); 7721 verifyFormat("f(MyMap[{composite, key}]);"); 7722 verifyFormat("class Class {\n" 7723 " T member = {arg1, arg2};\n" 7724 "};"); 7725 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 7726 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 7727 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 7728 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 7729 verifyFormat("int a = std::is_integral<int>{} + 0;"); 7730 7731 verifyFormat("int foo(int i) { return fo1{}(i); }"); 7732 verifyFormat("int foo(int i) { return fo1{}(i); }"); 7733 verifyFormat("auto i = decltype(x){};"); 7734 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 7735 verifyFormat("Node n{1, Node{1000}, //\n" 7736 " 2};"); 7737 verifyFormat("Aaaa aaaaaaa{\n" 7738 " {\n" 7739 " aaaa,\n" 7740 " },\n" 7741 "};"); 7742 verifyFormat("class C : public D {\n" 7743 " SomeClass SC{2};\n" 7744 "};"); 7745 verifyFormat("class C : public A {\n" 7746 " class D : public B {\n" 7747 " void f() { int i{2}; }\n" 7748 " };\n" 7749 "};"); 7750 verifyFormat("#define A {a, a},"); 7751 7752 // Avoid breaking between equal sign and opening brace 7753 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 7754 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 7755 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 7756 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 7757 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 7758 " {\"ccccccccccccccccccccc\", 2}};", 7759 AvoidBreakingFirstArgument); 7760 7761 // Binpacking only if there is no trailing comma 7762 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 7763 " cccccccccc, dddddddddd};", 7764 getLLVMStyleWithColumns(50)); 7765 verifyFormat("const Aaaaaa aaaaa = {\n" 7766 " aaaaaaaaaaa,\n" 7767 " bbbbbbbbbbb,\n" 7768 " ccccccccccc,\n" 7769 " ddddddddddd,\n" 7770 "};", getLLVMStyleWithColumns(50)); 7771 7772 // Cases where distinguising braced lists and blocks is hard. 7773 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 7774 verifyFormat("void f() {\n" 7775 " return; // comment\n" 7776 "}\n" 7777 "SomeType t;"); 7778 verifyFormat("void f() {\n" 7779 " if (a) {\n" 7780 " f();\n" 7781 " }\n" 7782 "}\n" 7783 "SomeType t;"); 7784 7785 // In combination with BinPackArguments = false. 7786 FormatStyle NoBinPacking = getLLVMStyle(); 7787 NoBinPacking.BinPackArguments = false; 7788 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 7789 " bbbbb,\n" 7790 " ccccc,\n" 7791 " ddddd,\n" 7792 " eeeee,\n" 7793 " ffffff,\n" 7794 " ggggg,\n" 7795 " hhhhhh,\n" 7796 " iiiiii,\n" 7797 " jjjjjj,\n" 7798 " kkkkkk};", 7799 NoBinPacking); 7800 verifyFormat("const Aaaaaa aaaaa = {\n" 7801 " aaaaa,\n" 7802 " bbbbb,\n" 7803 " ccccc,\n" 7804 " ddddd,\n" 7805 " eeeee,\n" 7806 " ffffff,\n" 7807 " ggggg,\n" 7808 " hhhhhh,\n" 7809 " iiiiii,\n" 7810 " jjjjjj,\n" 7811 " kkkkkk,\n" 7812 "};", 7813 NoBinPacking); 7814 verifyFormat( 7815 "const Aaaaaa aaaaa = {\n" 7816 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 7817 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 7818 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 7819 "};", 7820 NoBinPacking); 7821 7822 // FIXME: The alignment of these trailing comments might be bad. Then again, 7823 // this might be utterly useless in real code. 7824 verifyFormat("Constructor::Constructor()\n" 7825 " : some_value{ //\n" 7826 " aaaaaaa, //\n" 7827 " bbbbbbb} {}"); 7828 7829 // In braced lists, the first comment is always assumed to belong to the 7830 // first element. Thus, it can be moved to the next or previous line as 7831 // appropriate. 7832 EXPECT_EQ("function({// First element:\n" 7833 " 1,\n" 7834 " // Second element:\n" 7835 " 2});", 7836 format("function({\n" 7837 " // First element:\n" 7838 " 1,\n" 7839 " // Second element:\n" 7840 " 2});")); 7841 EXPECT_EQ("std::vector<int> MyNumbers{\n" 7842 " // First element:\n" 7843 " 1,\n" 7844 " // Second element:\n" 7845 " 2};", 7846 format("std::vector<int> MyNumbers{// First element:\n" 7847 " 1,\n" 7848 " // Second element:\n" 7849 " 2};", 7850 getLLVMStyleWithColumns(30))); 7851 // A trailing comma should still lead to an enforced line break and no 7852 // binpacking. 7853 EXPECT_EQ("vector<int> SomeVector = {\n" 7854 " // aaa\n" 7855 " 1,\n" 7856 " 2,\n" 7857 "};", 7858 format("vector<int> SomeVector = { // aaa\n" 7859 " 1, 2, };")); 7860 7861 FormatStyle ExtraSpaces = getLLVMStyle(); 7862 ExtraSpaces.Cpp11BracedListStyle = false; 7863 ExtraSpaces.ColumnLimit = 75; 7864 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 7865 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 7866 verifyFormat("f({ 1, 2 });", ExtraSpaces); 7867 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 7868 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 7869 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 7870 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 7871 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 7872 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 7873 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 7874 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 7875 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 7876 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 7877 verifyFormat("class Class {\n" 7878 " T member = { arg1, arg2 };\n" 7879 "};", 7880 ExtraSpaces); 7881 verifyFormat( 7882 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7883 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 7884 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 7885 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 7886 ExtraSpaces); 7887 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 7888 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 7889 ExtraSpaces); 7890 verifyFormat( 7891 "someFunction(OtherParam,\n" 7892 " BracedList{ // comment 1 (Forcing interesting break)\n" 7893 " param1, param2,\n" 7894 " // comment 2\n" 7895 " param3, param4 });", 7896 ExtraSpaces); 7897 verifyFormat( 7898 "std::this_thread::sleep_for(\n" 7899 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 7900 ExtraSpaces); 7901 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 7902 " aaaaaaa,\n" 7903 " aaaaaaaaaa,\n" 7904 " aaaaa,\n" 7905 " aaaaaaaaaaaaaaa,\n" 7906 " aaa,\n" 7907 " aaaaaaaaaa,\n" 7908 " a,\n" 7909 " aaaaaaaaaaaaaaaaaaaaa,\n" 7910 " aaaaaaaaaaaa,\n" 7911 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 7912 " aaaaaaa,\n" 7913 " a};"); 7914 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 7915 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 7916 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 7917 7918 // Avoid breaking between initializer/equal sign and opening brace 7919 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 7920 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 7921 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 7922 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 7923 " { \"ccccccccccccccccccccc\", 2 }\n" 7924 "};", 7925 ExtraSpaces); 7926 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 7927 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 7928 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 7929 " { \"ccccccccccccccccccccc\", 2 }\n" 7930 "};", 7931 ExtraSpaces); 7932 7933 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 7934 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 7935 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 7936 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 7937 } 7938 7939 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 7940 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7941 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7942 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7943 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7944 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7945 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 7946 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 7947 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7948 " 1, 22, 333, 4444, 55555, //\n" 7949 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7950 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 7951 verifyFormat( 7952 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7953 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 7954 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 7955 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7956 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7957 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 7958 " 7777777};"); 7959 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7960 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7961 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7962 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7963 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7964 " // Separating comment.\n" 7965 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7966 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 7967 " // Leading comment\n" 7968 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 7969 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 7970 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7971 " 1, 1, 1, 1};", 7972 getLLVMStyleWithColumns(39)); 7973 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 7974 " 1, 1, 1, 1};", 7975 getLLVMStyleWithColumns(38)); 7976 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 7977 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 7978 getLLVMStyleWithColumns(43)); 7979 verifyFormat( 7980 "static unsigned SomeValues[10][3] = {\n" 7981 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 7982 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 7983 verifyFormat("static auto fields = new vector<string>{\n" 7984 " \"aaaaaaaaaaaaa\",\n" 7985 " \"aaaaaaaaaaaaa\",\n" 7986 " \"aaaaaaaaaaaa\",\n" 7987 " \"aaaaaaaaaaaaaa\",\n" 7988 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 7989 " \"aaaaaaaaaaaa\",\n" 7990 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 7991 "};"); 7992 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 7993 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 7994 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 7995 " 3, cccccccccccccccccccccc};", 7996 getLLVMStyleWithColumns(60)); 7997 7998 // Trailing commas. 7999 verifyFormat("vector<int> x = {\n" 8000 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 8001 "};", 8002 getLLVMStyleWithColumns(39)); 8003 verifyFormat("vector<int> x = {\n" 8004 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 8005 "};", 8006 getLLVMStyleWithColumns(39)); 8007 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 8008 " 1, 1, 1, 1,\n" 8009 " /**/ /**/};", 8010 getLLVMStyleWithColumns(39)); 8011 8012 // Trailing comment in the first line. 8013 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 8014 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 8015 " 111111111, 222222222, 3333333333, 444444444, //\n" 8016 " 11111111, 22222222, 333333333, 44444444};"); 8017 // Trailing comment in the last line. 8018 verifyFormat("int aaaaa[] = {\n" 8019 " 1, 2, 3, // comment\n" 8020 " 4, 5, 6 // comment\n" 8021 "};"); 8022 8023 // With nested lists, we should either format one item per line or all nested 8024 // lists one on line. 8025 // FIXME: For some nested lists, we can do better. 8026 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 8027 " {aaaaaaaaaaaaaaaaaaa},\n" 8028 " {aaaaaaaaaaaaaaaaaaaaa},\n" 8029 " {aaaaaaaaaaaaaaaaa}};", 8030 getLLVMStyleWithColumns(60)); 8031 verifyFormat( 8032 "SomeStruct my_struct_array = {\n" 8033 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 8034 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 8035 " {aaa, aaa},\n" 8036 " {aaa, aaa},\n" 8037 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 8038 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 8039 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 8040 8041 // No column layout should be used here. 8042 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 8043 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 8044 8045 verifyNoCrash("a<,"); 8046 8047 // No braced initializer here. 8048 verifyFormat("void f() {\n" 8049 " struct Dummy {};\n" 8050 " f(v);\n" 8051 "}"); 8052 8053 // Long lists should be formatted in columns even if they are nested. 8054 verifyFormat( 8055 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 8056 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 8057 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 8058 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 8059 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 8060 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 8061 8062 // Allow "single-column" layout even if that violates the column limit. There 8063 // isn't going to be a better way. 8064 verifyFormat("std::vector<int> a = {\n" 8065 " aaaaaaaa,\n" 8066 " aaaaaaaa,\n" 8067 " aaaaaaaa,\n" 8068 " aaaaaaaa,\n" 8069 " aaaaaaaaaa,\n" 8070 " aaaaaaaa,\n" 8071 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 8072 getLLVMStyleWithColumns(30)); 8073 verifyFormat("vector<int> aaaa = {\n" 8074 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8075 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8076 " aaaaaa.aaaaaaa,\n" 8077 " aaaaaa.aaaaaaa,\n" 8078 " aaaaaa.aaaaaaa,\n" 8079 " aaaaaa.aaaaaaa,\n" 8080 "};"); 8081 8082 // Don't create hanging lists. 8083 verifyFormat("someFunction(Param, {List1, List2,\n" 8084 " List3});", 8085 getLLVMStyleWithColumns(35)); 8086 verifyFormat("someFunction(Param, Param,\n" 8087 " {List1, List2,\n" 8088 " List3});", 8089 getLLVMStyleWithColumns(35)); 8090 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 8091 " aaaaaaaaaaaaaaaaaaaaaaa);"); 8092 } 8093 8094 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 8095 FormatStyle DoNotMerge = getLLVMStyle(); 8096 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 8097 8098 verifyFormat("void f() { return 42; }"); 8099 verifyFormat("void f() {\n" 8100 " return 42;\n" 8101 "}", 8102 DoNotMerge); 8103 verifyFormat("void f() {\n" 8104 " // Comment\n" 8105 "}"); 8106 verifyFormat("{\n" 8107 "#error {\n" 8108 " int a;\n" 8109 "}"); 8110 verifyFormat("{\n" 8111 " int a;\n" 8112 "#error {\n" 8113 "}"); 8114 verifyFormat("void f() {} // comment"); 8115 verifyFormat("void f() { int a; } // comment"); 8116 verifyFormat("void f() {\n" 8117 "} // comment", 8118 DoNotMerge); 8119 verifyFormat("void f() {\n" 8120 " int a;\n" 8121 "} // comment", 8122 DoNotMerge); 8123 verifyFormat("void f() {\n" 8124 "} // comment", 8125 getLLVMStyleWithColumns(15)); 8126 8127 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 8128 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 8129 8130 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 8131 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 8132 verifyFormat("class C {\n" 8133 " C()\n" 8134 " : iiiiiiii(nullptr),\n" 8135 " kkkkkkk(nullptr),\n" 8136 " mmmmmmm(nullptr),\n" 8137 " nnnnnnn(nullptr) {}\n" 8138 "};", 8139 getGoogleStyle()); 8140 8141 FormatStyle NoColumnLimit = getLLVMStyle(); 8142 NoColumnLimit.ColumnLimit = 0; 8143 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 8144 EXPECT_EQ("class C {\n" 8145 " A() : b(0) {}\n" 8146 "};", 8147 format("class C{A():b(0){}};", NoColumnLimit)); 8148 EXPECT_EQ("A()\n" 8149 " : b(0) {\n" 8150 "}", 8151 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 8152 8153 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 8154 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 8155 FormatStyle::SFS_None; 8156 EXPECT_EQ("A()\n" 8157 " : b(0) {\n" 8158 "}", 8159 format("A():b(0){}", DoNotMergeNoColumnLimit)); 8160 EXPECT_EQ("A()\n" 8161 " : b(0) {\n" 8162 "}", 8163 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 8164 8165 verifyFormat("#define A \\\n" 8166 " void f() { \\\n" 8167 " int i; \\\n" 8168 " }", 8169 getLLVMStyleWithColumns(20)); 8170 verifyFormat("#define A \\\n" 8171 " void f() { int i; }", 8172 getLLVMStyleWithColumns(21)); 8173 verifyFormat("#define A \\\n" 8174 " void f() { \\\n" 8175 " int i; \\\n" 8176 " } \\\n" 8177 " int j;", 8178 getLLVMStyleWithColumns(22)); 8179 verifyFormat("#define A \\\n" 8180 " void f() { int i; } \\\n" 8181 " int j;", 8182 getLLVMStyleWithColumns(23)); 8183 } 8184 8185 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 8186 FormatStyle MergeEmptyOnly = getLLVMStyle(); 8187 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 8188 verifyFormat("class C {\n" 8189 " int f() {}\n" 8190 "};", 8191 MergeEmptyOnly); 8192 verifyFormat("class C {\n" 8193 " int f() {\n" 8194 " return 42;\n" 8195 " }\n" 8196 "};", 8197 MergeEmptyOnly); 8198 verifyFormat("int f() {}", MergeEmptyOnly); 8199 verifyFormat("int f() {\n" 8200 " return 42;\n" 8201 "}", 8202 MergeEmptyOnly); 8203 8204 // Also verify behavior when BraceWrapping.AfterFunction = true 8205 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 8206 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 8207 verifyFormat("int f() {}", MergeEmptyOnly); 8208 verifyFormat("class C {\n" 8209 " int f() {}\n" 8210 "};", 8211 MergeEmptyOnly); 8212 } 8213 8214 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 8215 FormatStyle MergeInlineOnly = getLLVMStyle(); 8216 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 8217 verifyFormat("class C {\n" 8218 " int f() { return 42; }\n" 8219 "};", 8220 MergeInlineOnly); 8221 verifyFormat("int f() {\n" 8222 " return 42;\n" 8223 "}", 8224 MergeInlineOnly); 8225 8226 // SFS_Inline implies SFS_Empty 8227 verifyFormat("class C {\n" 8228 " int f() {}\n" 8229 "};", 8230 MergeInlineOnly); 8231 verifyFormat("int f() {}", MergeInlineOnly); 8232 8233 // Also verify behavior when BraceWrapping.AfterFunction = true 8234 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 8235 MergeInlineOnly.BraceWrapping.AfterFunction = true; 8236 verifyFormat("class C {\n" 8237 " int f() { return 42; }\n" 8238 "};", 8239 MergeInlineOnly); 8240 verifyFormat("int f()\n" 8241 "{\n" 8242 " return 42;\n" 8243 "}", 8244 MergeInlineOnly); 8245 8246 // SFS_Inline implies SFS_Empty 8247 verifyFormat("int f() {}", MergeInlineOnly); 8248 verifyFormat("class C {\n" 8249 " int f() {}\n" 8250 "};", 8251 MergeInlineOnly); 8252 } 8253 8254 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 8255 FormatStyle MergeInlineOnly = getLLVMStyle(); 8256 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 8257 FormatStyle::SFS_InlineOnly; 8258 verifyFormat("class C {\n" 8259 " int f() { return 42; }\n" 8260 "};", 8261 MergeInlineOnly); 8262 verifyFormat("int f() {\n" 8263 " return 42;\n" 8264 "}", 8265 MergeInlineOnly); 8266 8267 // SFS_InlineOnly does not imply SFS_Empty 8268 verifyFormat("class C {\n" 8269 " int f() {}\n" 8270 "};", 8271 MergeInlineOnly); 8272 verifyFormat("int f() {\n" 8273 "}", 8274 MergeInlineOnly); 8275 8276 // Also verify behavior when BraceWrapping.AfterFunction = true 8277 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 8278 MergeInlineOnly.BraceWrapping.AfterFunction = true; 8279 verifyFormat("class C {\n" 8280 " int f() { return 42; }\n" 8281 "};", 8282 MergeInlineOnly); 8283 verifyFormat("int f()\n" 8284 "{\n" 8285 " return 42;\n" 8286 "}", 8287 MergeInlineOnly); 8288 8289 // SFS_InlineOnly does not imply SFS_Empty 8290 verifyFormat("int f()\n" 8291 "{\n" 8292 "}", 8293 MergeInlineOnly); 8294 verifyFormat("class C {\n" 8295 " int f() {}\n" 8296 "};", 8297 MergeInlineOnly); 8298 } 8299 8300 TEST_F(FormatTest, SplitEmptyFunction) { 8301 FormatStyle Style = getLLVMStyle(); 8302 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 8303 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8304 Style.BraceWrapping.AfterFunction = true; 8305 Style.BraceWrapping.SplitEmptyFunction = false; 8306 Style.ColumnLimit = 40; 8307 8308 verifyFormat("int f()\n" 8309 "{}", 8310 Style); 8311 verifyFormat("int f()\n" 8312 "{\n" 8313 " return 42;\n" 8314 "}", 8315 Style); 8316 verifyFormat("int f()\n" 8317 "{\n" 8318 " // some comment\n" 8319 "}", 8320 Style); 8321 8322 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 8323 verifyFormat("int f() {}", Style); 8324 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 8325 "{}", 8326 Style); 8327 verifyFormat("int f()\n" 8328 "{\n" 8329 " return 0;\n" 8330 "}", 8331 Style); 8332 8333 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 8334 verifyFormat("class Foo {\n" 8335 " int f() {}\n" 8336 "};\n", 8337 Style); 8338 verifyFormat("class Foo {\n" 8339 " int f() { return 0; }\n" 8340 "};\n", 8341 Style); 8342 verifyFormat("class Foo {\n" 8343 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 8344 " {}\n" 8345 "};\n", 8346 Style); 8347 verifyFormat("class Foo {\n" 8348 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 8349 " {\n" 8350 " return 0;\n" 8351 " }\n" 8352 "};\n", 8353 Style); 8354 8355 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 8356 verifyFormat("int f() {}", Style); 8357 verifyFormat("int f() { return 0; }", Style); 8358 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 8359 "{}", 8360 Style); 8361 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 8362 "{\n" 8363 " return 0;\n" 8364 "}", 8365 Style); 8366 } 8367 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 8368 FormatStyle Style = getLLVMStyle(); 8369 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 8370 verifyFormat("#ifdef A\n" 8371 "int f() {}\n" 8372 "#else\n" 8373 "int g() {}\n" 8374 "#endif", 8375 Style); 8376 } 8377 8378 TEST_F(FormatTest, SplitEmptyClass) { 8379 FormatStyle Style = getLLVMStyle(); 8380 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8381 Style.BraceWrapping.AfterClass = true; 8382 Style.BraceWrapping.SplitEmptyRecord = false; 8383 8384 verifyFormat("class Foo\n" 8385 "{};", 8386 Style); 8387 verifyFormat("/* something */ class Foo\n" 8388 "{};", 8389 Style); 8390 verifyFormat("template <typename X> class Foo\n" 8391 "{};", 8392 Style); 8393 verifyFormat("class Foo\n" 8394 "{\n" 8395 " Foo();\n" 8396 "};", 8397 Style); 8398 verifyFormat("typedef class Foo\n" 8399 "{\n" 8400 "} Foo_t;", 8401 Style); 8402 } 8403 8404 TEST_F(FormatTest, SplitEmptyStruct) { 8405 FormatStyle Style = getLLVMStyle(); 8406 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8407 Style.BraceWrapping.AfterStruct = true; 8408 Style.BraceWrapping.SplitEmptyRecord = false; 8409 8410 verifyFormat("struct Foo\n" 8411 "{};", 8412 Style); 8413 verifyFormat("/* something */ struct Foo\n" 8414 "{};", 8415 Style); 8416 verifyFormat("template <typename X> struct Foo\n" 8417 "{};", 8418 Style); 8419 verifyFormat("struct Foo\n" 8420 "{\n" 8421 " Foo();\n" 8422 "};", 8423 Style); 8424 verifyFormat("typedef struct Foo\n" 8425 "{\n" 8426 "} Foo_t;", 8427 Style); 8428 //typedef struct Bar {} Bar_t; 8429 } 8430 8431 TEST_F(FormatTest, SplitEmptyUnion) { 8432 FormatStyle Style = getLLVMStyle(); 8433 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8434 Style.BraceWrapping.AfterUnion = true; 8435 Style.BraceWrapping.SplitEmptyRecord = false; 8436 8437 verifyFormat("union Foo\n" 8438 "{};", 8439 Style); 8440 verifyFormat("/* something */ union Foo\n" 8441 "{};", 8442 Style); 8443 verifyFormat("union Foo\n" 8444 "{\n" 8445 " A,\n" 8446 "};", 8447 Style); 8448 verifyFormat("typedef union Foo\n" 8449 "{\n" 8450 "} Foo_t;", 8451 Style); 8452 } 8453 8454 TEST_F(FormatTest, SplitEmptyNamespace) { 8455 FormatStyle Style = getLLVMStyle(); 8456 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8457 Style.BraceWrapping.AfterNamespace = true; 8458 Style.BraceWrapping.SplitEmptyNamespace = false; 8459 8460 verifyFormat("namespace Foo\n" 8461 "{};", 8462 Style); 8463 verifyFormat("/* something */ namespace Foo\n" 8464 "{};", 8465 Style); 8466 verifyFormat("inline namespace Foo\n" 8467 "{};", 8468 Style); 8469 verifyFormat("/* something */ inline namespace Foo\n" 8470 "{};", 8471 Style); 8472 verifyFormat("export namespace Foo\n" 8473 "{};", 8474 Style); 8475 verifyFormat("namespace Foo\n" 8476 "{\n" 8477 "void Bar();\n" 8478 "};", 8479 Style); 8480 } 8481 8482 TEST_F(FormatTest, NeverMergeShortRecords) { 8483 FormatStyle Style = getLLVMStyle(); 8484 8485 verifyFormat("class Foo {\n" 8486 " Foo();\n" 8487 "};", 8488 Style); 8489 verifyFormat("typedef class Foo {\n" 8490 " Foo();\n" 8491 "} Foo_t;", 8492 Style); 8493 verifyFormat("struct Foo {\n" 8494 " Foo();\n" 8495 "};", 8496 Style); 8497 verifyFormat("typedef struct Foo {\n" 8498 " Foo();\n" 8499 "} Foo_t;", 8500 Style); 8501 verifyFormat("union Foo {\n" 8502 " A,\n" 8503 "};", 8504 Style); 8505 verifyFormat("typedef union Foo {\n" 8506 " A,\n" 8507 "} Foo_t;", 8508 Style); 8509 verifyFormat("namespace Foo {\n" 8510 "void Bar();\n" 8511 "};", 8512 Style); 8513 8514 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8515 Style.BraceWrapping.AfterClass = true; 8516 Style.BraceWrapping.AfterStruct = true; 8517 Style.BraceWrapping.AfterUnion = true; 8518 Style.BraceWrapping.AfterNamespace = true; 8519 verifyFormat("class Foo\n" 8520 "{\n" 8521 " Foo();\n" 8522 "};", 8523 Style); 8524 verifyFormat("typedef class Foo\n" 8525 "{\n" 8526 " Foo();\n" 8527 "} Foo_t;", 8528 Style); 8529 verifyFormat("struct Foo\n" 8530 "{\n" 8531 " Foo();\n" 8532 "};", 8533 Style); 8534 verifyFormat("typedef struct Foo\n" 8535 "{\n" 8536 " Foo();\n" 8537 "} Foo_t;", 8538 Style); 8539 verifyFormat("union Foo\n" 8540 "{\n" 8541 " A,\n" 8542 "};", 8543 Style); 8544 verifyFormat("typedef union Foo\n" 8545 "{\n" 8546 " A,\n" 8547 "} Foo_t;", 8548 Style); 8549 verifyFormat("namespace Foo\n" 8550 "{\n" 8551 "void Bar();\n" 8552 "};", 8553 Style); 8554 } 8555 8556 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 8557 // Elaborate type variable declarations. 8558 verifyFormat("struct foo a = {bar};\nint n;"); 8559 verifyFormat("class foo a = {bar};\nint n;"); 8560 verifyFormat("union foo a = {bar};\nint n;"); 8561 8562 // Elaborate types inside function definitions. 8563 verifyFormat("struct foo f() {}\nint n;"); 8564 verifyFormat("class foo f() {}\nint n;"); 8565 verifyFormat("union foo f() {}\nint n;"); 8566 8567 // Templates. 8568 verifyFormat("template <class X> void f() {}\nint n;"); 8569 verifyFormat("template <struct X> void f() {}\nint n;"); 8570 verifyFormat("template <union X> void f() {}\nint n;"); 8571 8572 // Actual definitions... 8573 verifyFormat("struct {\n} n;"); 8574 verifyFormat( 8575 "template <template <class T, class Y>, class Z> class X {\n} n;"); 8576 verifyFormat("union Z {\n int n;\n} x;"); 8577 verifyFormat("class MACRO Z {\n} n;"); 8578 verifyFormat("class MACRO(X) Z {\n} n;"); 8579 verifyFormat("class __attribute__(X) Z {\n} n;"); 8580 verifyFormat("class __declspec(X) Z {\n} n;"); 8581 verifyFormat("class A##B##C {\n} n;"); 8582 verifyFormat("class alignas(16) Z {\n} n;"); 8583 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 8584 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 8585 8586 // Redefinition from nested context: 8587 verifyFormat("class A::B::C {\n} n;"); 8588 8589 // Template definitions. 8590 verifyFormat( 8591 "template <typename F>\n" 8592 "Matcher(const Matcher<F> &Other,\n" 8593 " typename enable_if_c<is_base_of<F, T>::value &&\n" 8594 " !is_same<F, T>::value>::type * = 0)\n" 8595 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 8596 8597 // FIXME: This is still incorrectly handled at the formatter side. 8598 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 8599 verifyFormat("int i = SomeFunction(a<b, a> b);"); 8600 8601 // FIXME: 8602 // This now gets parsed incorrectly as class definition. 8603 // verifyFormat("class A<int> f() {\n}\nint n;"); 8604 8605 // Elaborate types where incorrectly parsing the structural element would 8606 // break the indent. 8607 verifyFormat("if (true)\n" 8608 " class X x;\n" 8609 "else\n" 8610 " f();\n"); 8611 8612 // This is simply incomplete. Formatting is not important, but must not crash. 8613 verifyFormat("class A:"); 8614 } 8615 8616 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 8617 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 8618 format("#error Leave all white!!!!! space* alone!\n")); 8619 EXPECT_EQ( 8620 "#warning Leave all white!!!!! space* alone!\n", 8621 format("#warning Leave all white!!!!! space* alone!\n")); 8622 EXPECT_EQ("#error 1", format(" # error 1")); 8623 EXPECT_EQ("#warning 1", format(" # warning 1")); 8624 } 8625 8626 TEST_F(FormatTest, FormatHashIfExpressions) { 8627 verifyFormat("#if AAAA && BBBB"); 8628 verifyFormat("#if (AAAA && BBBB)"); 8629 verifyFormat("#elif (AAAA && BBBB)"); 8630 // FIXME: Come up with a better indentation for #elif. 8631 verifyFormat( 8632 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 8633 " defined(BBBBBBBB)\n" 8634 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 8635 " defined(BBBBBBBB)\n" 8636 "#endif", 8637 getLLVMStyleWithColumns(65)); 8638 } 8639 8640 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 8641 FormatStyle AllowsMergedIf = getGoogleStyle(); 8642 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 8643 FormatStyle::SIS_WithoutElse; 8644 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 8645 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 8646 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 8647 EXPECT_EQ("if (true) return 42;", 8648 format("if (true)\nreturn 42;", AllowsMergedIf)); 8649 FormatStyle ShortMergedIf = AllowsMergedIf; 8650 ShortMergedIf.ColumnLimit = 25; 8651 verifyFormat("#define A \\\n" 8652 " if (true) return 42;", 8653 ShortMergedIf); 8654 verifyFormat("#define A \\\n" 8655 " f(); \\\n" 8656 " if (true)\n" 8657 "#define B", 8658 ShortMergedIf); 8659 verifyFormat("#define A \\\n" 8660 " f(); \\\n" 8661 " if (true)\n" 8662 "g();", 8663 ShortMergedIf); 8664 verifyFormat("{\n" 8665 "#ifdef A\n" 8666 " // Comment\n" 8667 " if (true) continue;\n" 8668 "#endif\n" 8669 " // Comment\n" 8670 " if (true) continue;\n" 8671 "}", 8672 ShortMergedIf); 8673 ShortMergedIf.ColumnLimit = 33; 8674 verifyFormat("#define A \\\n" 8675 " if constexpr (true) return 42;", 8676 ShortMergedIf); 8677 verifyFormat("#define A \\\n" 8678 " if CONSTEXPR (true) return 42;", 8679 ShortMergedIf); 8680 ShortMergedIf.ColumnLimit = 29; 8681 verifyFormat("#define A \\\n" 8682 " if (aaaaaaaaaa) return 1; \\\n" 8683 " return 2;", 8684 ShortMergedIf); 8685 ShortMergedIf.ColumnLimit = 28; 8686 verifyFormat("#define A \\\n" 8687 " if (aaaaaaaaaa) \\\n" 8688 " return 1; \\\n" 8689 " return 2;", 8690 ShortMergedIf); 8691 verifyFormat("#define A \\\n" 8692 " if constexpr (aaaaaaa) \\\n" 8693 " return 1; \\\n" 8694 " return 2;", 8695 ShortMergedIf); 8696 verifyFormat("#define A \\\n" 8697 " if CONSTEXPR (aaaaaaa) \\\n" 8698 " return 1; \\\n" 8699 " return 2;", 8700 ShortMergedIf); 8701 } 8702 8703 TEST_F(FormatTest, FormatStarDependingOnContext) { 8704 verifyFormat("void f(int *a);"); 8705 verifyFormat("void f() { f(fint * b); }"); 8706 verifyFormat("class A {\n void f(int *a);\n};"); 8707 verifyFormat("class A {\n int *a;\n};"); 8708 verifyFormat("namespace a {\n" 8709 "namespace b {\n" 8710 "class A {\n" 8711 " void f() {}\n" 8712 " int *a;\n" 8713 "};\n" 8714 "} // namespace b\n" 8715 "} // namespace a"); 8716 } 8717 8718 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 8719 verifyFormat("while"); 8720 verifyFormat("operator"); 8721 } 8722 8723 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 8724 // This code would be painfully slow to format if we didn't skip it. 8725 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 8726 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8727 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8728 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8729 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 8730 "A(1, 1)\n" 8731 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 8732 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8733 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8734 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8735 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8736 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8737 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8738 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8739 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 8740 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 8741 // Deeply nested part is untouched, rest is formatted. 8742 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 8743 format(std::string("int i;\n") + Code + "int j;\n", 8744 getLLVMStyle(), SC_ExpectIncomplete)); 8745 } 8746 8747 //===----------------------------------------------------------------------===// 8748 // Objective-C tests. 8749 //===----------------------------------------------------------------------===// 8750 8751 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 8752 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 8753 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 8754 format("-(NSUInteger)indexOfObject:(id)anObject;")); 8755 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 8756 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 8757 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 8758 format("-(NSInteger)Method3:(id)anObject;")); 8759 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 8760 format("-(NSInteger)Method4:(id)anObject;")); 8761 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 8762 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 8763 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 8764 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 8765 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 8766 "forAllCells:(BOOL)flag;", 8767 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 8768 "forAllCells:(BOOL)flag;")); 8769 8770 // Very long objectiveC method declaration. 8771 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 8772 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 8773 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 8774 " inRange:(NSRange)range\n" 8775 " outRange:(NSRange)out_range\n" 8776 " outRange1:(NSRange)out_range1\n" 8777 " outRange2:(NSRange)out_range2\n" 8778 " outRange3:(NSRange)out_range3\n" 8779 " outRange4:(NSRange)out_range4\n" 8780 " outRange5:(NSRange)out_range5\n" 8781 " outRange6:(NSRange)out_range6\n" 8782 " outRange7:(NSRange)out_range7\n" 8783 " outRange8:(NSRange)out_range8\n" 8784 " outRange9:(NSRange)out_range9;"); 8785 8786 // When the function name has to be wrapped. 8787 FormatStyle Style = getLLVMStyle(); 8788 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 8789 // and always indents instead. 8790 Style.IndentWrappedFunctionNames = false; 8791 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 8792 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 8793 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 8794 "}", 8795 Style); 8796 Style.IndentWrappedFunctionNames = true; 8797 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 8798 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 8799 " anotherName:(NSString)dddddddddddddd {\n" 8800 "}", 8801 Style); 8802 8803 verifyFormat("- (int)sum:(vector<int>)numbers;"); 8804 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 8805 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 8806 // protocol lists (but not for template classes): 8807 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 8808 8809 verifyFormat("- (int (*)())foo:(int (*)())f;"); 8810 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 8811 8812 // If there's no return type (very rare in practice!), LLVM and Google style 8813 // agree. 8814 verifyFormat("- foo;"); 8815 verifyFormat("- foo:(int)f;"); 8816 verifyGoogleFormat("- foo:(int)foo;"); 8817 } 8818 8819 8820 TEST_F(FormatTest, BreaksStringLiterals) { 8821 EXPECT_EQ("\"some text \"\n" 8822 "\"other\";", 8823 format("\"some text other\";", getLLVMStyleWithColumns(12))); 8824 EXPECT_EQ("\"some text \"\n" 8825 "\"other\";", 8826 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 8827 EXPECT_EQ( 8828 "#define A \\\n" 8829 " \"some \" \\\n" 8830 " \"text \" \\\n" 8831 " \"other\";", 8832 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 8833 EXPECT_EQ( 8834 "#define A \\\n" 8835 " \"so \" \\\n" 8836 " \"text \" \\\n" 8837 " \"other\";", 8838 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 8839 8840 EXPECT_EQ("\"some text\"", 8841 format("\"some text\"", getLLVMStyleWithColumns(1))); 8842 EXPECT_EQ("\"some text\"", 8843 format("\"some text\"", getLLVMStyleWithColumns(11))); 8844 EXPECT_EQ("\"some \"\n" 8845 "\"text\"", 8846 format("\"some text\"", getLLVMStyleWithColumns(10))); 8847 EXPECT_EQ("\"some \"\n" 8848 "\"text\"", 8849 format("\"some text\"", getLLVMStyleWithColumns(7))); 8850 EXPECT_EQ("\"some\"\n" 8851 "\" tex\"\n" 8852 "\"t\"", 8853 format("\"some text\"", getLLVMStyleWithColumns(6))); 8854 EXPECT_EQ("\"some\"\n" 8855 "\" tex\"\n" 8856 "\" and\"", 8857 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 8858 EXPECT_EQ("\"some\"\n" 8859 "\"/tex\"\n" 8860 "\"/and\"", 8861 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 8862 8863 EXPECT_EQ("variable =\n" 8864 " \"long string \"\n" 8865 " \"literal\";", 8866 format("variable = \"long string literal\";", 8867 getLLVMStyleWithColumns(20))); 8868 8869 EXPECT_EQ("variable = f(\n" 8870 " \"long string \"\n" 8871 " \"literal\",\n" 8872 " short,\n" 8873 " loooooooooooooooooooong);", 8874 format("variable = f(\"long string literal\", short, " 8875 "loooooooooooooooooooong);", 8876 getLLVMStyleWithColumns(20))); 8877 8878 EXPECT_EQ( 8879 "f(g(\"long string \"\n" 8880 " \"literal\"),\n" 8881 " b);", 8882 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 8883 EXPECT_EQ("f(g(\"long string \"\n" 8884 " \"literal\",\n" 8885 " a),\n" 8886 " b);", 8887 format("f(g(\"long string literal\", a), b);", 8888 getLLVMStyleWithColumns(20))); 8889 EXPECT_EQ( 8890 "f(\"one two\".split(\n" 8891 " variable));", 8892 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 8893 EXPECT_EQ("f(\"one two three four five six \"\n" 8894 " \"seven\".split(\n" 8895 " really_looooong_variable));", 8896 format("f(\"one two three four five six seven\"." 8897 "split(really_looooong_variable));", 8898 getLLVMStyleWithColumns(33))); 8899 8900 EXPECT_EQ("f(\"some \"\n" 8901 " \"text\",\n" 8902 " other);", 8903 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 8904 8905 // Only break as a last resort. 8906 verifyFormat( 8907 "aaaaaaaaaaaaaaaaaaaa(\n" 8908 " aaaaaaaaaaaaaaaaaaaa,\n" 8909 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 8910 8911 EXPECT_EQ("\"splitmea\"\n" 8912 "\"trandomp\"\n" 8913 "\"oint\"", 8914 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 8915 8916 EXPECT_EQ("\"split/\"\n" 8917 "\"pathat/\"\n" 8918 "\"slashes\"", 8919 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8920 8921 EXPECT_EQ("\"split/\"\n" 8922 "\"pathat/\"\n" 8923 "\"slashes\"", 8924 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 8925 EXPECT_EQ("\"split at \"\n" 8926 "\"spaces/at/\"\n" 8927 "\"slashes.at.any$\"\n" 8928 "\"non-alphanumeric%\"\n" 8929 "\"1111111111characte\"\n" 8930 "\"rs\"", 8931 format("\"split at " 8932 "spaces/at/" 8933 "slashes.at." 8934 "any$non-" 8935 "alphanumeric%" 8936 "1111111111characte" 8937 "rs\"", 8938 getLLVMStyleWithColumns(20))); 8939 8940 // Verify that splitting the strings understands 8941 // Style::AlwaysBreakBeforeMultilineStrings. 8942 EXPECT_EQ( 8943 "aaaaaaaaaaaa(\n" 8944 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 8945 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 8946 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 8947 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8948 "aaaaaaaaaaaaaaaaaaaaaa\");", 8949 getGoogleStyle())); 8950 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8951 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 8952 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 8953 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 8954 "aaaaaaaaaaaaaaaaaaaaaa\";", 8955 getGoogleStyle())); 8956 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8957 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8958 format("llvm::outs() << " 8959 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 8960 "aaaaaaaaaaaaaaaaaaa\";")); 8961 EXPECT_EQ("ffff(\n" 8962 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8963 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8964 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8965 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 8966 getGoogleStyle())); 8967 8968 FormatStyle Style = getLLVMStyleWithColumns(12); 8969 Style.BreakStringLiterals = false; 8970 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 8971 8972 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 8973 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8974 EXPECT_EQ("#define A \\\n" 8975 " \"some \" \\\n" 8976 " \"text \" \\\n" 8977 " \"other\";", 8978 format("#define A \"some text other\";", AlignLeft)); 8979 } 8980 8981 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 8982 EXPECT_EQ("C a = \"some more \"\n" 8983 " \"text\";", 8984 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 8985 } 8986 8987 TEST_F(FormatTest, FullyRemoveEmptyLines) { 8988 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 8989 NoEmptyLines.MaxEmptyLinesToKeep = 0; 8990 EXPECT_EQ("int i = a(b());", 8991 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 8992 } 8993 8994 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 8995 EXPECT_EQ( 8996 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 8997 "(\n" 8998 " \"x\t\");", 8999 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 9000 "aaaaaaa(" 9001 "\"x\t\");")); 9002 } 9003 9004 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 9005 EXPECT_EQ( 9006 "u8\"utf8 string \"\n" 9007 "u8\"literal\";", 9008 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 9009 EXPECT_EQ( 9010 "u\"utf16 string \"\n" 9011 "u\"literal\";", 9012 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 9013 EXPECT_EQ( 9014 "U\"utf32 string \"\n" 9015 "U\"literal\";", 9016 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 9017 EXPECT_EQ("L\"wide string \"\n" 9018 "L\"literal\";", 9019 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 9020 EXPECT_EQ("@\"NSString \"\n" 9021 "@\"literal\";", 9022 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 9023 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 9024 9025 // This input makes clang-format try to split the incomplete unicode escape 9026 // sequence, which used to lead to a crasher. 9027 verifyNoCrash( 9028 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 9029 getLLVMStyleWithColumns(60)); 9030 } 9031 9032 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 9033 FormatStyle Style = getGoogleStyleWithColumns(15); 9034 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 9035 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 9036 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 9037 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 9038 EXPECT_EQ("u8R\"x(raw literal)x\";", 9039 format("u8R\"x(raw literal)x\";", Style)); 9040 } 9041 9042 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 9043 FormatStyle Style = getLLVMStyleWithColumns(20); 9044 EXPECT_EQ( 9045 "_T(\"aaaaaaaaaaaaaa\")\n" 9046 "_T(\"aaaaaaaaaaaaaa\")\n" 9047 "_T(\"aaaaaaaaaaaa\")", 9048 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 9049 EXPECT_EQ("f(x,\n" 9050 " _T(\"aaaaaaaaaaaa\")\n" 9051 " _T(\"aaa\"),\n" 9052 " z);", 9053 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 9054 9055 // FIXME: Handle embedded spaces in one iteration. 9056 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 9057 // "_T(\"aaaaaaaaaaaaa\")\n" 9058 // "_T(\"aaaaaaaaaaaaa\")\n" 9059 // "_T(\"a\")", 9060 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 9061 // getLLVMStyleWithColumns(20))); 9062 EXPECT_EQ( 9063 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 9064 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 9065 EXPECT_EQ("f(\n" 9066 "#if !TEST\n" 9067 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 9068 "#endif\n" 9069 ");", 9070 format("f(\n" 9071 "#if !TEST\n" 9072 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 9073 "#endif\n" 9074 ");")); 9075 EXPECT_EQ("f(\n" 9076 "\n" 9077 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 9078 format("f(\n" 9079 "\n" 9080 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 9081 } 9082 9083 TEST_F(FormatTest, BreaksStringLiteralOperands) { 9084 // In a function call with two operands, the second can be broken with no line 9085 // break before it. 9086 EXPECT_EQ("func(a, \"long long \"\n" 9087 " \"long long\");", 9088 format("func(a, \"long long long long\");", 9089 getLLVMStyleWithColumns(24))); 9090 // In a function call with three operands, the second must be broken with a 9091 // line break before it. 9092 EXPECT_EQ("func(a,\n" 9093 " \"long long long \"\n" 9094 " \"long\",\n" 9095 " c);", 9096 format("func(a, \"long long long long\", c);", 9097 getLLVMStyleWithColumns(24))); 9098 // In a function call with three operands, the third must be broken with a 9099 // line break before it. 9100 EXPECT_EQ("func(a, b,\n" 9101 " \"long long long \"\n" 9102 " \"long\");", 9103 format("func(a, b, \"long long long long\");", 9104 getLLVMStyleWithColumns(24))); 9105 // In a function call with three operands, both the second and the third must 9106 // be broken with a line break before them. 9107 EXPECT_EQ("func(a,\n" 9108 " \"long long long \"\n" 9109 " \"long\",\n" 9110 " \"long long long \"\n" 9111 " \"long\");", 9112 format("func(a, \"long long long long\", \"long long long long\");", 9113 getLLVMStyleWithColumns(24))); 9114 // In a chain of << with two operands, the second can be broken with no line 9115 // break before it. 9116 EXPECT_EQ("a << \"line line \"\n" 9117 " \"line\";", 9118 format("a << \"line line line\";", 9119 getLLVMStyleWithColumns(20))); 9120 // In a chain of << with three operands, the second can be broken with no line 9121 // break before it. 9122 EXPECT_EQ("abcde << \"line \"\n" 9123 " \"line line\"\n" 9124 " << c;", 9125 format("abcde << \"line line line\" << c;", 9126 getLLVMStyleWithColumns(20))); 9127 // In a chain of << with three operands, the third must be broken with a line 9128 // break before it. 9129 EXPECT_EQ("a << b\n" 9130 " << \"line line \"\n" 9131 " \"line\";", 9132 format("a << b << \"line line line\";", 9133 getLLVMStyleWithColumns(20))); 9134 // In a chain of << with three operands, the second can be broken with no line 9135 // break before it and the third must be broken with a line break before it. 9136 EXPECT_EQ("abcd << \"line line \"\n" 9137 " \"line\"\n" 9138 " << \"line line \"\n" 9139 " \"line\";", 9140 format("abcd << \"line line line\" << \"line line line\";", 9141 getLLVMStyleWithColumns(20))); 9142 // In a chain of binary operators with two operands, the second can be broken 9143 // with no line break before it. 9144 EXPECT_EQ("abcd + \"line line \"\n" 9145 " \"line line\";", 9146 format("abcd + \"line line line line\";", 9147 getLLVMStyleWithColumns(20))); 9148 // In a chain of binary operators with three operands, the second must be 9149 // broken with a line break before it. 9150 EXPECT_EQ("abcd +\n" 9151 " \"line line \"\n" 9152 " \"line line\" +\n" 9153 " e;", 9154 format("abcd + \"line line line line\" + e;", 9155 getLLVMStyleWithColumns(20))); 9156 // In a function call with two operands, with AlignAfterOpenBracket enabled, 9157 // the first must be broken with a line break before it. 9158 FormatStyle Style = getLLVMStyleWithColumns(25); 9159 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9160 EXPECT_EQ("someFunction(\n" 9161 " \"long long long \"\n" 9162 " \"long\",\n" 9163 " a);", 9164 format("someFunction(\"long long long long\", a);", Style)); 9165 } 9166 9167 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 9168 EXPECT_EQ( 9169 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 9170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 9171 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 9172 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 9173 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 9174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 9175 } 9176 9177 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 9178 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 9179 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 9180 EXPECT_EQ("fffffffffff(g(R\"x(\n" 9181 "multiline raw string literal xxxxxxxxxxxxxx\n" 9182 ")x\",\n" 9183 " a),\n" 9184 " b);", 9185 format("fffffffffff(g(R\"x(\n" 9186 "multiline raw string literal xxxxxxxxxxxxxx\n" 9187 ")x\", a), b);", 9188 getGoogleStyleWithColumns(20))); 9189 EXPECT_EQ("fffffffffff(\n" 9190 " g(R\"x(qqq\n" 9191 "multiline raw string literal xxxxxxxxxxxxxx\n" 9192 ")x\",\n" 9193 " a),\n" 9194 " b);", 9195 format("fffffffffff(g(R\"x(qqq\n" 9196 "multiline raw string literal xxxxxxxxxxxxxx\n" 9197 ")x\", a), b);", 9198 getGoogleStyleWithColumns(20))); 9199 9200 EXPECT_EQ("fffffffffff(R\"x(\n" 9201 "multiline raw string literal xxxxxxxxxxxxxx\n" 9202 ")x\");", 9203 format("fffffffffff(R\"x(\n" 9204 "multiline raw string literal xxxxxxxxxxxxxx\n" 9205 ")x\");", 9206 getGoogleStyleWithColumns(20))); 9207 EXPECT_EQ("fffffffffff(R\"x(\n" 9208 "multiline raw string literal xxxxxxxxxxxxxx\n" 9209 ")x\" + bbbbbb);", 9210 format("fffffffffff(R\"x(\n" 9211 "multiline raw string literal xxxxxxxxxxxxxx\n" 9212 ")x\" + bbbbbb);", 9213 getGoogleStyleWithColumns(20))); 9214 EXPECT_EQ("fffffffffff(\n" 9215 " R\"x(\n" 9216 "multiline raw string literal xxxxxxxxxxxxxx\n" 9217 ")x\" +\n" 9218 " bbbbbb);", 9219 format("fffffffffff(\n" 9220 " R\"x(\n" 9221 "multiline raw string literal xxxxxxxxxxxxxx\n" 9222 ")x\" + bbbbbb);", 9223 getGoogleStyleWithColumns(20))); 9224 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 9225 format("fffffffffff(\n" 9226 " R\"(single line raw string)\" + bbbbbb);")); 9227 } 9228 9229 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 9230 verifyFormat("string a = \"unterminated;"); 9231 EXPECT_EQ("function(\"unterminated,\n" 9232 " OtherParameter);", 9233 format("function( \"unterminated,\n" 9234 " OtherParameter);")); 9235 } 9236 9237 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 9238 FormatStyle Style = getLLVMStyle(); 9239 Style.Standard = FormatStyle::LS_Cpp03; 9240 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 9241 format("#define x(_a) printf(\"foo\"_a);", Style)); 9242 } 9243 9244 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 9245 9246 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 9247 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 9248 " \"ddeeefff\");", 9249 format("someFunction(\"aaabbbcccdddeeefff\");", 9250 getLLVMStyleWithColumns(25))); 9251 EXPECT_EQ("someFunction1234567890(\n" 9252 " \"aaabbbcccdddeeefff\");", 9253 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 9254 getLLVMStyleWithColumns(26))); 9255 EXPECT_EQ("someFunction1234567890(\n" 9256 " \"aaabbbcccdddeeeff\"\n" 9257 " \"f\");", 9258 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 9259 getLLVMStyleWithColumns(25))); 9260 EXPECT_EQ("someFunction1234567890(\n" 9261 " \"aaabbbcccdddeeeff\"\n" 9262 " \"f\");", 9263 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 9264 getLLVMStyleWithColumns(24))); 9265 EXPECT_EQ("someFunction(\n" 9266 " \"aaabbbcc ddde \"\n" 9267 " \"efff\");", 9268 format("someFunction(\"aaabbbcc ddde efff\");", 9269 getLLVMStyleWithColumns(25))); 9270 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 9271 " \"ddeeefff\");", 9272 format("someFunction(\"aaabbbccc ddeeefff\");", 9273 getLLVMStyleWithColumns(25))); 9274 EXPECT_EQ("someFunction1234567890(\n" 9275 " \"aaabb \"\n" 9276 " \"cccdddeeefff\");", 9277 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 9278 getLLVMStyleWithColumns(25))); 9279 EXPECT_EQ("#define A \\\n" 9280 " string s = \\\n" 9281 " \"123456789\" \\\n" 9282 " \"0\"; \\\n" 9283 " int i;", 9284 format("#define A string s = \"1234567890\"; int i;", 9285 getLLVMStyleWithColumns(20))); 9286 EXPECT_EQ("someFunction(\n" 9287 " \"aaabbbcc \"\n" 9288 " \"dddeeefff\");", 9289 format("someFunction(\"aaabbbcc dddeeefff\");", 9290 getLLVMStyleWithColumns(25))); 9291 } 9292 9293 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 9294 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 9295 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 9296 EXPECT_EQ("\"test\"\n" 9297 "\"\\n\"", 9298 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 9299 EXPECT_EQ("\"tes\\\\\"\n" 9300 "\"n\"", 9301 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 9302 EXPECT_EQ("\"\\\\\\\\\"\n" 9303 "\"\\n\"", 9304 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 9305 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 9306 EXPECT_EQ("\"\\uff01\"\n" 9307 "\"test\"", 9308 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 9309 EXPECT_EQ("\"\\Uff01ff02\"", 9310 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 9311 EXPECT_EQ("\"\\x000000000001\"\n" 9312 "\"next\"", 9313 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 9314 EXPECT_EQ("\"\\x000000000001next\"", 9315 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 9316 EXPECT_EQ("\"\\x000000000001\"", 9317 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 9318 EXPECT_EQ("\"test\"\n" 9319 "\"\\000000\"\n" 9320 "\"000001\"", 9321 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 9322 EXPECT_EQ("\"test\\000\"\n" 9323 "\"00000000\"\n" 9324 "\"1\"", 9325 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 9326 } 9327 9328 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 9329 verifyFormat("void f() {\n" 9330 " return g() {}\n" 9331 " void h() {}"); 9332 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 9333 "g();\n" 9334 "}"); 9335 } 9336 9337 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 9338 verifyFormat( 9339 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 9340 } 9341 9342 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 9343 verifyFormat("class X {\n" 9344 " void f() {\n" 9345 " }\n" 9346 "};", 9347 getLLVMStyleWithColumns(12)); 9348 } 9349 9350 TEST_F(FormatTest, ConfigurableIndentWidth) { 9351 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 9352 EightIndent.IndentWidth = 8; 9353 EightIndent.ContinuationIndentWidth = 8; 9354 verifyFormat("void f() {\n" 9355 " someFunction();\n" 9356 " if (true) {\n" 9357 " f();\n" 9358 " }\n" 9359 "}", 9360 EightIndent); 9361 verifyFormat("class X {\n" 9362 " void f() {\n" 9363 " }\n" 9364 "};", 9365 EightIndent); 9366 verifyFormat("int x[] = {\n" 9367 " call(),\n" 9368 " call()};", 9369 EightIndent); 9370 } 9371 9372 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 9373 verifyFormat("double\n" 9374 "f();", 9375 getLLVMStyleWithColumns(8)); 9376 } 9377 9378 TEST_F(FormatTest, ConfigurableUseOfTab) { 9379 FormatStyle Tab = getLLVMStyleWithColumns(42); 9380 Tab.IndentWidth = 8; 9381 Tab.UseTab = FormatStyle::UT_Always; 9382 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9383 9384 EXPECT_EQ("if (aaaaaaaa && // q\n" 9385 " bb)\t\t// w\n" 9386 "\t;", 9387 format("if (aaaaaaaa &&// q\n" 9388 "bb)// w\n" 9389 ";", 9390 Tab)); 9391 EXPECT_EQ("if (aaa && bbb) // w\n" 9392 "\t;", 9393 format("if(aaa&&bbb)// w\n" 9394 ";", 9395 Tab)); 9396 9397 verifyFormat("class X {\n" 9398 "\tvoid f() {\n" 9399 "\t\tsomeFunction(parameter1,\n" 9400 "\t\t\t parameter2);\n" 9401 "\t}\n" 9402 "};", 9403 Tab); 9404 verifyFormat("#define A \\\n" 9405 "\tvoid f() { \\\n" 9406 "\t\tsomeFunction( \\\n" 9407 "\t\t parameter1, \\\n" 9408 "\t\t parameter2); \\\n" 9409 "\t}", 9410 Tab); 9411 verifyFormat("int a;\t // x\n" 9412 "int bbbbbbbb; // x\n", 9413 Tab); 9414 9415 Tab.TabWidth = 4; 9416 Tab.IndentWidth = 8; 9417 verifyFormat("class TabWidth4Indent8 {\n" 9418 "\t\tvoid f() {\n" 9419 "\t\t\t\tsomeFunction(parameter1,\n" 9420 "\t\t\t\t\t\t\t parameter2);\n" 9421 "\t\t}\n" 9422 "};", 9423 Tab); 9424 9425 Tab.TabWidth = 4; 9426 Tab.IndentWidth = 4; 9427 verifyFormat("class TabWidth4Indent4 {\n" 9428 "\tvoid f() {\n" 9429 "\t\tsomeFunction(parameter1,\n" 9430 "\t\t\t\t\t parameter2);\n" 9431 "\t}\n" 9432 "};", 9433 Tab); 9434 9435 Tab.TabWidth = 8; 9436 Tab.IndentWidth = 4; 9437 verifyFormat("class TabWidth8Indent4 {\n" 9438 " void f() {\n" 9439 "\tsomeFunction(parameter1,\n" 9440 "\t\t parameter2);\n" 9441 " }\n" 9442 "};", 9443 Tab); 9444 9445 Tab.TabWidth = 8; 9446 Tab.IndentWidth = 8; 9447 EXPECT_EQ("/*\n" 9448 "\t a\t\tcomment\n" 9449 "\t in multiple lines\n" 9450 " */", 9451 format(" /*\t \t \n" 9452 " \t \t a\t\tcomment\t \t\n" 9453 " \t \t in multiple lines\t\n" 9454 " \t */", 9455 Tab)); 9456 9457 Tab.UseTab = FormatStyle::UT_ForIndentation; 9458 verifyFormat("{\n" 9459 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9460 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9461 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9462 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9463 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9464 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9465 "};", 9466 Tab); 9467 verifyFormat("enum AA {\n" 9468 "\ta1, // Force multiple lines\n" 9469 "\ta2,\n" 9470 "\ta3\n" 9471 "};", 9472 Tab); 9473 EXPECT_EQ("if (aaaaaaaa && // q\n" 9474 " bb) // w\n" 9475 "\t;", 9476 format("if (aaaaaaaa &&// q\n" 9477 "bb)// w\n" 9478 ";", 9479 Tab)); 9480 verifyFormat("class X {\n" 9481 "\tvoid f() {\n" 9482 "\t\tsomeFunction(parameter1,\n" 9483 "\t\t parameter2);\n" 9484 "\t}\n" 9485 "};", 9486 Tab); 9487 verifyFormat("{\n" 9488 "\tQ(\n" 9489 "\t {\n" 9490 "\t\t int a;\n" 9491 "\t\t someFunction(aaaaaaaa,\n" 9492 "\t\t bbbbbbb);\n" 9493 "\t },\n" 9494 "\t p);\n" 9495 "}", 9496 Tab); 9497 EXPECT_EQ("{\n" 9498 "\t/* aaaa\n" 9499 "\t bbbb */\n" 9500 "}", 9501 format("{\n" 9502 "/* aaaa\n" 9503 " bbbb */\n" 9504 "}", 9505 Tab)); 9506 EXPECT_EQ("{\n" 9507 "\t/*\n" 9508 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9509 "\t bbbbbbbbbbbbb\n" 9510 "\t*/\n" 9511 "}", 9512 format("{\n" 9513 "/*\n" 9514 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9515 "*/\n" 9516 "}", 9517 Tab)); 9518 EXPECT_EQ("{\n" 9519 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9520 "\t// bbbbbbbbbbbbb\n" 9521 "}", 9522 format("{\n" 9523 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9524 "}", 9525 Tab)); 9526 EXPECT_EQ("{\n" 9527 "\t/*\n" 9528 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9529 "\t bbbbbbbbbbbbb\n" 9530 "\t*/\n" 9531 "}", 9532 format("{\n" 9533 "\t/*\n" 9534 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9535 "\t*/\n" 9536 "}", 9537 Tab)); 9538 EXPECT_EQ("{\n" 9539 "\t/*\n" 9540 "\n" 9541 "\t*/\n" 9542 "}", 9543 format("{\n" 9544 "\t/*\n" 9545 "\n" 9546 "\t*/\n" 9547 "}", 9548 Tab)); 9549 EXPECT_EQ("{\n" 9550 "\t/*\n" 9551 " asdf\n" 9552 "\t*/\n" 9553 "}", 9554 format("{\n" 9555 "\t/*\n" 9556 " asdf\n" 9557 "\t*/\n" 9558 "}", 9559 Tab)); 9560 9561 Tab.UseTab = FormatStyle::UT_Never; 9562 EXPECT_EQ("/*\n" 9563 " a\t\tcomment\n" 9564 " in multiple lines\n" 9565 " */", 9566 format(" /*\t \t \n" 9567 " \t \t a\t\tcomment\t \t\n" 9568 " \t \t in multiple lines\t\n" 9569 " \t */", 9570 Tab)); 9571 EXPECT_EQ("/* some\n" 9572 " comment */", 9573 format(" \t \t /* some\n" 9574 " \t \t comment */", 9575 Tab)); 9576 EXPECT_EQ("int a; /* some\n" 9577 " comment */", 9578 format(" \t \t int a; /* some\n" 9579 " \t \t comment */", 9580 Tab)); 9581 9582 EXPECT_EQ("int a; /* some\n" 9583 "comment */", 9584 format(" \t \t int\ta; /* some\n" 9585 " \t \t comment */", 9586 Tab)); 9587 EXPECT_EQ("f(\"\t\t\"); /* some\n" 9588 " comment */", 9589 format(" \t \t f(\"\t\t\"); /* some\n" 9590 " \t \t comment */", 9591 Tab)); 9592 EXPECT_EQ("{\n" 9593 " /*\n" 9594 " * Comment\n" 9595 " */\n" 9596 " int i;\n" 9597 "}", 9598 format("{\n" 9599 "\t/*\n" 9600 "\t * Comment\n" 9601 "\t */\n" 9602 "\t int i;\n" 9603 "}")); 9604 9605 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 9606 Tab.TabWidth = 8; 9607 Tab.IndentWidth = 8; 9608 EXPECT_EQ("if (aaaaaaaa && // q\n" 9609 " bb) // w\n" 9610 "\t;", 9611 format("if (aaaaaaaa &&// q\n" 9612 "bb)// w\n" 9613 ";", 9614 Tab)); 9615 EXPECT_EQ("if (aaa && bbb) // w\n" 9616 "\t;", 9617 format("if(aaa&&bbb)// w\n" 9618 ";", 9619 Tab)); 9620 verifyFormat("class X {\n" 9621 "\tvoid f() {\n" 9622 "\t\tsomeFunction(parameter1,\n" 9623 "\t\t\t parameter2);\n" 9624 "\t}\n" 9625 "};", 9626 Tab); 9627 verifyFormat("#define A \\\n" 9628 "\tvoid f() { \\\n" 9629 "\t\tsomeFunction( \\\n" 9630 "\t\t parameter1, \\\n" 9631 "\t\t parameter2); \\\n" 9632 "\t}", 9633 Tab); 9634 Tab.TabWidth = 4; 9635 Tab.IndentWidth = 8; 9636 verifyFormat("class TabWidth4Indent8 {\n" 9637 "\t\tvoid f() {\n" 9638 "\t\t\t\tsomeFunction(parameter1,\n" 9639 "\t\t\t\t\t\t\t parameter2);\n" 9640 "\t\t}\n" 9641 "};", 9642 Tab); 9643 Tab.TabWidth = 4; 9644 Tab.IndentWidth = 4; 9645 verifyFormat("class TabWidth4Indent4 {\n" 9646 "\tvoid f() {\n" 9647 "\t\tsomeFunction(parameter1,\n" 9648 "\t\t\t\t\t parameter2);\n" 9649 "\t}\n" 9650 "};", 9651 Tab); 9652 Tab.TabWidth = 8; 9653 Tab.IndentWidth = 4; 9654 verifyFormat("class TabWidth8Indent4 {\n" 9655 " void f() {\n" 9656 "\tsomeFunction(parameter1,\n" 9657 "\t\t parameter2);\n" 9658 " }\n" 9659 "};", 9660 Tab); 9661 Tab.TabWidth = 8; 9662 Tab.IndentWidth = 8; 9663 EXPECT_EQ("/*\n" 9664 "\t a\t\tcomment\n" 9665 "\t in multiple lines\n" 9666 " */", 9667 format(" /*\t \t \n" 9668 " \t \t a\t\tcomment\t \t\n" 9669 " \t \t in multiple lines\t\n" 9670 " \t */", 9671 Tab)); 9672 verifyFormat("{\n" 9673 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9674 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9675 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9676 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9677 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9678 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 9679 "};", 9680 Tab); 9681 verifyFormat("enum AA {\n" 9682 "\ta1, // Force multiple lines\n" 9683 "\ta2,\n" 9684 "\ta3\n" 9685 "};", 9686 Tab); 9687 EXPECT_EQ("if (aaaaaaaa && // q\n" 9688 " bb) // w\n" 9689 "\t;", 9690 format("if (aaaaaaaa &&// q\n" 9691 "bb)// w\n" 9692 ";", 9693 Tab)); 9694 verifyFormat("class X {\n" 9695 "\tvoid f() {\n" 9696 "\t\tsomeFunction(parameter1,\n" 9697 "\t\t\t parameter2);\n" 9698 "\t}\n" 9699 "};", 9700 Tab); 9701 verifyFormat("{\n" 9702 "\tQ(\n" 9703 "\t {\n" 9704 "\t\t int a;\n" 9705 "\t\t someFunction(aaaaaaaa,\n" 9706 "\t\t\t\t bbbbbbb);\n" 9707 "\t },\n" 9708 "\t p);\n" 9709 "}", 9710 Tab); 9711 EXPECT_EQ("{\n" 9712 "\t/* aaaa\n" 9713 "\t bbbb */\n" 9714 "}", 9715 format("{\n" 9716 "/* aaaa\n" 9717 " bbbb */\n" 9718 "}", 9719 Tab)); 9720 EXPECT_EQ("{\n" 9721 "\t/*\n" 9722 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9723 "\t bbbbbbbbbbbbb\n" 9724 "\t*/\n" 9725 "}", 9726 format("{\n" 9727 "/*\n" 9728 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9729 "*/\n" 9730 "}", 9731 Tab)); 9732 EXPECT_EQ("{\n" 9733 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9734 "\t// bbbbbbbbbbbbb\n" 9735 "}", 9736 format("{\n" 9737 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9738 "}", 9739 Tab)); 9740 EXPECT_EQ("{\n" 9741 "\t/*\n" 9742 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9743 "\t bbbbbbbbbbbbb\n" 9744 "\t*/\n" 9745 "}", 9746 format("{\n" 9747 "\t/*\n" 9748 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 9749 "\t*/\n" 9750 "}", 9751 Tab)); 9752 EXPECT_EQ("{\n" 9753 "\t/*\n" 9754 "\n" 9755 "\t*/\n" 9756 "}", 9757 format("{\n" 9758 "\t/*\n" 9759 "\n" 9760 "\t*/\n" 9761 "}", 9762 Tab)); 9763 EXPECT_EQ("{\n" 9764 "\t/*\n" 9765 " asdf\n" 9766 "\t*/\n" 9767 "}", 9768 format("{\n" 9769 "\t/*\n" 9770 " asdf\n" 9771 "\t*/\n" 9772 "}", 9773 Tab)); 9774 EXPECT_EQ("/*\n" 9775 "\t a\t\tcomment\n" 9776 "\t in multiple lines\n" 9777 " */", 9778 format(" /*\t \t \n" 9779 " \t \t a\t\tcomment\t \t\n" 9780 " \t \t in multiple lines\t\n" 9781 " \t */", 9782 Tab)); 9783 EXPECT_EQ("/* some\n" 9784 " comment */", 9785 format(" \t \t /* some\n" 9786 " \t \t comment */", 9787 Tab)); 9788 EXPECT_EQ("int a; /* some\n" 9789 " comment */", 9790 format(" \t \t int a; /* some\n" 9791 " \t \t comment */", 9792 Tab)); 9793 EXPECT_EQ("int a; /* some\n" 9794 "comment */", 9795 format(" \t \t int\ta; /* some\n" 9796 " \t \t comment */", 9797 Tab)); 9798 EXPECT_EQ("f(\"\t\t\"); /* some\n" 9799 " comment */", 9800 format(" \t \t f(\"\t\t\"); /* some\n" 9801 " \t \t comment */", 9802 Tab)); 9803 EXPECT_EQ("{\n" 9804 " /*\n" 9805 " * Comment\n" 9806 " */\n" 9807 " int i;\n" 9808 "}", 9809 format("{\n" 9810 "\t/*\n" 9811 "\t * Comment\n" 9812 "\t */\n" 9813 "\t int i;\n" 9814 "}")); 9815 Tab.AlignConsecutiveAssignments = true; 9816 Tab.AlignConsecutiveDeclarations = true; 9817 Tab.TabWidth = 4; 9818 Tab.IndentWidth = 4; 9819 verifyFormat("class Assign {\n" 9820 "\tvoid f() {\n" 9821 "\t\tint x = 123;\n" 9822 "\t\tint random = 4;\n" 9823 "\t\tstd::string alphabet =\n" 9824 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 9825 "\t}\n" 9826 "};", 9827 Tab); 9828 } 9829 9830 TEST_F(FormatTest, CalculatesOriginalColumn) { 9831 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9832 "q\"; /* some\n" 9833 " comment */", 9834 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9835 "q\"; /* some\n" 9836 " comment */", 9837 getLLVMStyle())); 9838 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 9839 "/* some\n" 9840 " comment */", 9841 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 9842 " /* some\n" 9843 " comment */", 9844 getLLVMStyle())); 9845 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9846 "qqq\n" 9847 "/* some\n" 9848 " comment */", 9849 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9850 "qqq\n" 9851 " /* some\n" 9852 " comment */", 9853 getLLVMStyle())); 9854 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9855 "wwww; /* some\n" 9856 " comment */", 9857 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 9858 "wwww; /* some\n" 9859 " comment */", 9860 getLLVMStyle())); 9861 } 9862 9863 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 9864 FormatStyle NoSpace = getLLVMStyle(); 9865 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 9866 9867 verifyFormat("while(true)\n" 9868 " continue;", 9869 NoSpace); 9870 verifyFormat("for(;;)\n" 9871 " continue;", 9872 NoSpace); 9873 verifyFormat("if(true)\n" 9874 " f();\n" 9875 "else if(true)\n" 9876 " f();", 9877 NoSpace); 9878 verifyFormat("do {\n" 9879 " do_something();\n" 9880 "} while(something());", 9881 NoSpace); 9882 verifyFormat("switch(x) {\n" 9883 "default:\n" 9884 " break;\n" 9885 "}", 9886 NoSpace); 9887 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 9888 verifyFormat("size_t x = sizeof(x);", NoSpace); 9889 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 9890 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 9891 verifyFormat("alignas(128) char a[128];", NoSpace); 9892 verifyFormat("size_t x = alignof(MyType);", NoSpace); 9893 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 9894 verifyFormat("int f() throw(Deprecated);", NoSpace); 9895 verifyFormat("typedef void (*cb)(int);", NoSpace); 9896 verifyFormat("T A::operator()();", NoSpace); 9897 verifyFormat("X A::operator++(T);", NoSpace); 9898 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 9899 9900 FormatStyle Space = getLLVMStyle(); 9901 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 9902 9903 verifyFormat("int f ();", Space); 9904 verifyFormat("void f (int a, T b) {\n" 9905 " while (true)\n" 9906 " continue;\n" 9907 "}", 9908 Space); 9909 verifyFormat("if (true)\n" 9910 " f ();\n" 9911 "else if (true)\n" 9912 " f ();", 9913 Space); 9914 verifyFormat("do {\n" 9915 " do_something ();\n" 9916 "} while (something ());", 9917 Space); 9918 verifyFormat("switch (x) {\n" 9919 "default:\n" 9920 " break;\n" 9921 "}", 9922 Space); 9923 verifyFormat("A::A () : a (1) {}", Space); 9924 verifyFormat("void f () __attribute__ ((asdf));", Space); 9925 verifyFormat("*(&a + 1);\n" 9926 "&((&a)[1]);\n" 9927 "a[(b + c) * d];\n" 9928 "(((a + 1) * 2) + 3) * 4;", 9929 Space); 9930 verifyFormat("#define A(x) x", Space); 9931 verifyFormat("#define A (x) x", Space); 9932 verifyFormat("#if defined(x)\n" 9933 "#endif", 9934 Space); 9935 verifyFormat("auto i = std::make_unique<int> (5);", Space); 9936 verifyFormat("size_t x = sizeof (x);", Space); 9937 verifyFormat("auto f (int x) -> decltype (x);", Space); 9938 verifyFormat("int f (T x) noexcept (x.create ());", Space); 9939 verifyFormat("alignas (128) char a[128];", Space); 9940 verifyFormat("size_t x = alignof (MyType);", Space); 9941 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 9942 verifyFormat("int f () throw (Deprecated);", Space); 9943 verifyFormat("typedef void (*cb) (int);", Space); 9944 verifyFormat("T A::operator() ();", Space); 9945 verifyFormat("X A::operator++ (T);", Space); 9946 verifyFormat("auto lambda = [] () { return 0; };", Space); 9947 verifyFormat("int x = int (y);", Space); 9948 9949 FormatStyle SomeSpace = getLLVMStyle(); 9950 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 9951 9952 verifyFormat("[]() -> float {}", SomeSpace); 9953 verifyFormat("[] (auto foo) {}", SomeSpace); 9954 verifyFormat("[foo]() -> int {}", SomeSpace); 9955 verifyFormat("int f();", SomeSpace); 9956 verifyFormat("void f (int a, T b) {\n" 9957 " while (true)\n" 9958 " continue;\n" 9959 "}", 9960 SomeSpace); 9961 verifyFormat("if (true)\n" 9962 " f();\n" 9963 "else if (true)\n" 9964 " f();", 9965 SomeSpace); 9966 verifyFormat("do {\n" 9967 " do_something();\n" 9968 "} while (something());", 9969 SomeSpace); 9970 verifyFormat("switch (x) {\n" 9971 "default:\n" 9972 " break;\n" 9973 "}", 9974 SomeSpace); 9975 verifyFormat("A::A() : a (1) {}", SomeSpace); 9976 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 9977 verifyFormat("*(&a + 1);\n" 9978 "&((&a)[1]);\n" 9979 "a[(b + c) * d];\n" 9980 "(((a + 1) * 2) + 3) * 4;", 9981 SomeSpace); 9982 verifyFormat("#define A(x) x", SomeSpace); 9983 verifyFormat("#define A (x) x", SomeSpace); 9984 verifyFormat("#if defined(x)\n" 9985 "#endif", 9986 SomeSpace); 9987 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 9988 verifyFormat("size_t x = sizeof (x);", SomeSpace); 9989 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 9990 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 9991 verifyFormat("alignas (128) char a[128];", SomeSpace); 9992 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 9993 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 9994 SomeSpace); 9995 verifyFormat("int f() throw (Deprecated);", SomeSpace); 9996 verifyFormat("typedef void (*cb) (int);", SomeSpace); 9997 verifyFormat("T A::operator()();", SomeSpace); 9998 verifyFormat("X A::operator++ (T);", SomeSpace); 9999 verifyFormat("int x = int (y);", SomeSpace); 10000 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 10001 } 10002 10003 TEST_F(FormatTest, SpaceAfterLogicalNot) { 10004 FormatStyle Spaces = getLLVMStyle(); 10005 Spaces.SpaceAfterLogicalNot = true; 10006 10007 verifyFormat("bool x = ! y", Spaces); 10008 verifyFormat("if (! isFailure())", Spaces); 10009 verifyFormat("if (! (a && b))", Spaces); 10010 verifyFormat("\"Error!\"", Spaces); 10011 verifyFormat("! ! x", Spaces); 10012 } 10013 10014 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 10015 FormatStyle Spaces = getLLVMStyle(); 10016 10017 Spaces.SpacesInParentheses = true; 10018 verifyFormat("do_something( ::globalVar );", Spaces); 10019 verifyFormat("call( x, y, z );", Spaces); 10020 verifyFormat("call();", Spaces); 10021 verifyFormat("std::function<void( int, int )> callback;", Spaces); 10022 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 10023 Spaces); 10024 verifyFormat("while ( (bool)1 )\n" 10025 " continue;", 10026 Spaces); 10027 verifyFormat("for ( ;; )\n" 10028 " continue;", 10029 Spaces); 10030 verifyFormat("if ( true )\n" 10031 " f();\n" 10032 "else if ( true )\n" 10033 " f();", 10034 Spaces); 10035 verifyFormat("do {\n" 10036 " do_something( (int)i );\n" 10037 "} while ( something() );", 10038 Spaces); 10039 verifyFormat("switch ( x ) {\n" 10040 "default:\n" 10041 " break;\n" 10042 "}", 10043 Spaces); 10044 10045 Spaces.SpacesInParentheses = false; 10046 Spaces.SpacesInCStyleCastParentheses = true; 10047 verifyFormat("Type *A = ( Type * )P;", Spaces); 10048 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 10049 verifyFormat("x = ( int32 )y;", Spaces); 10050 verifyFormat("int a = ( int )(2.0f);", Spaces); 10051 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 10052 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 10053 verifyFormat("#define x (( int )-1)", Spaces); 10054 10055 // Run the first set of tests again with: 10056 Spaces.SpacesInParentheses = false; 10057 Spaces.SpaceInEmptyParentheses = true; 10058 Spaces.SpacesInCStyleCastParentheses = true; 10059 verifyFormat("call(x, y, z);", Spaces); 10060 verifyFormat("call( );", Spaces); 10061 verifyFormat("std::function<void(int, int)> callback;", Spaces); 10062 verifyFormat("while (( bool )1)\n" 10063 " continue;", 10064 Spaces); 10065 verifyFormat("for (;;)\n" 10066 " continue;", 10067 Spaces); 10068 verifyFormat("if (true)\n" 10069 " f( );\n" 10070 "else if (true)\n" 10071 " f( );", 10072 Spaces); 10073 verifyFormat("do {\n" 10074 " do_something(( int )i);\n" 10075 "} while (something( ));", 10076 Spaces); 10077 verifyFormat("switch (x) {\n" 10078 "default:\n" 10079 " break;\n" 10080 "}", 10081 Spaces); 10082 10083 // Run the first set of tests again with: 10084 Spaces.SpaceAfterCStyleCast = true; 10085 verifyFormat("call(x, y, z);", Spaces); 10086 verifyFormat("call( );", Spaces); 10087 verifyFormat("std::function<void(int, int)> callback;", Spaces); 10088 verifyFormat("while (( bool ) 1)\n" 10089 " continue;", 10090 Spaces); 10091 verifyFormat("for (;;)\n" 10092 " continue;", 10093 Spaces); 10094 verifyFormat("if (true)\n" 10095 " f( );\n" 10096 "else if (true)\n" 10097 " f( );", 10098 Spaces); 10099 verifyFormat("do {\n" 10100 " do_something(( int ) i);\n" 10101 "} while (something( ));", 10102 Spaces); 10103 verifyFormat("switch (x) {\n" 10104 "default:\n" 10105 " break;\n" 10106 "}", 10107 Spaces); 10108 10109 // Run subset of tests again with: 10110 Spaces.SpacesInCStyleCastParentheses = false; 10111 Spaces.SpaceAfterCStyleCast = true; 10112 verifyFormat("while ((bool) 1)\n" 10113 " continue;", 10114 Spaces); 10115 verifyFormat("do {\n" 10116 " do_something((int) i);\n" 10117 "} while (something( ));", 10118 Spaces); 10119 } 10120 10121 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 10122 verifyFormat("int a[5];"); 10123 verifyFormat("a[3] += 42;"); 10124 10125 FormatStyle Spaces = getLLVMStyle(); 10126 Spaces.SpacesInSquareBrackets = true; 10127 // Lambdas unchanged. 10128 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 10129 verifyFormat("return [i, args...] {};", Spaces); 10130 10131 // Not lambdas. 10132 verifyFormat("int a[ 5 ];", Spaces); 10133 verifyFormat("a[ 3 ] += 42;", Spaces); 10134 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 10135 verifyFormat("double &operator[](int i) { return 0; }\n" 10136 "int i;", 10137 Spaces); 10138 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 10139 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 10140 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 10141 } 10142 10143 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 10144 verifyFormat("int a = 5;"); 10145 verifyFormat("a += 42;"); 10146 verifyFormat("a or_eq 8;"); 10147 10148 FormatStyle Spaces = getLLVMStyle(); 10149 Spaces.SpaceBeforeAssignmentOperators = false; 10150 verifyFormat("int a= 5;", Spaces); 10151 verifyFormat("a+= 42;", Spaces); 10152 verifyFormat("a or_eq 8;", Spaces); 10153 } 10154 10155 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 10156 verifyFormat("class Foo : public Bar {};"); 10157 verifyFormat("Foo::Foo() : foo(1) {}"); 10158 verifyFormat("for (auto a : b) {\n}"); 10159 verifyFormat("int x = a ? b : c;"); 10160 verifyFormat("{\n" 10161 "label0:\n" 10162 " int x = 0;\n" 10163 "}"); 10164 verifyFormat("switch (x) {\n" 10165 "case 1:\n" 10166 "default:\n" 10167 "}"); 10168 10169 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 10170 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 10171 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 10172 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 10173 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 10174 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 10175 verifyFormat("{\n" 10176 "label1:\n" 10177 " int x = 0;\n" 10178 "}", 10179 CtorInitializerStyle); 10180 verifyFormat("switch (x) {\n" 10181 "case 1:\n" 10182 "default:\n" 10183 "}", 10184 CtorInitializerStyle); 10185 CtorInitializerStyle.BreakConstructorInitializers = 10186 FormatStyle::BCIS_AfterColon; 10187 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 10188 " aaaaaaaaaaaaaaaa(1),\n" 10189 " bbbbbbbbbbbbbbbb(2) {}", 10190 CtorInitializerStyle); 10191 CtorInitializerStyle.BreakConstructorInitializers = 10192 FormatStyle::BCIS_BeforeComma; 10193 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 10194 " : aaaaaaaaaaaaaaaa(1)\n" 10195 " , bbbbbbbbbbbbbbbb(2) {}", 10196 CtorInitializerStyle); 10197 CtorInitializerStyle.BreakConstructorInitializers = 10198 FormatStyle::BCIS_BeforeColon; 10199 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 10200 " : aaaaaaaaaaaaaaaa(1),\n" 10201 " bbbbbbbbbbbbbbbb(2) {}", 10202 CtorInitializerStyle); 10203 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 10204 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 10205 ": aaaaaaaaaaaaaaaa(1),\n" 10206 " bbbbbbbbbbbbbbbb(2) {}", 10207 CtorInitializerStyle); 10208 10209 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 10210 InheritanceStyle.SpaceBeforeInheritanceColon = false; 10211 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 10212 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 10213 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 10214 verifyFormat("int x = a ? b : c;", InheritanceStyle); 10215 verifyFormat("{\n" 10216 "label2:\n" 10217 " int x = 0;\n" 10218 "}", 10219 InheritanceStyle); 10220 verifyFormat("switch (x) {\n" 10221 "case 1:\n" 10222 "default:\n" 10223 "}", 10224 InheritanceStyle); 10225 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 10226 verifyFormat("class Foooooooooooooooooooooo:\n" 10227 " public aaaaaaaaaaaaaaaaaa,\n" 10228 " public bbbbbbbbbbbbbbbbbb {\n" 10229 "}", 10230 InheritanceStyle); 10231 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 10232 verifyFormat("class Foooooooooooooooooooooo\n" 10233 " : public aaaaaaaaaaaaaaaaaa\n" 10234 " , public bbbbbbbbbbbbbbbbbb {\n" 10235 "}", 10236 InheritanceStyle); 10237 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 10238 verifyFormat("class Foooooooooooooooooooooo\n" 10239 " : public aaaaaaaaaaaaaaaaaa,\n" 10240 " public bbbbbbbbbbbbbbbbbb {\n" 10241 "}", 10242 InheritanceStyle); 10243 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 10244 verifyFormat("class Foooooooooooooooooooooo\n" 10245 ": public aaaaaaaaaaaaaaaaaa,\n" 10246 " public bbbbbbbbbbbbbbbbbb {}", 10247 InheritanceStyle); 10248 10249 FormatStyle ForLoopStyle = getLLVMStyle(); 10250 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 10251 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 10252 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 10253 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 10254 verifyFormat("int x = a ? b : c;", ForLoopStyle); 10255 verifyFormat("{\n" 10256 "label2:\n" 10257 " int x = 0;\n" 10258 "}", 10259 ForLoopStyle); 10260 verifyFormat("switch (x) {\n" 10261 "case 1:\n" 10262 "default:\n" 10263 "}", 10264 ForLoopStyle); 10265 10266 FormatStyle NoSpaceStyle = getLLVMStyle(); 10267 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 10268 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 10269 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 10270 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 10271 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 10272 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 10273 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 10274 verifyFormat("{\n" 10275 "label3:\n" 10276 " int x = 0;\n" 10277 "}", 10278 NoSpaceStyle); 10279 verifyFormat("switch (x) {\n" 10280 "case 1:\n" 10281 "default:\n" 10282 "}", 10283 NoSpaceStyle); 10284 } 10285 10286 TEST_F(FormatTest, AlignConsecutiveMacros) { 10287 FormatStyle Style = getLLVMStyle(); 10288 Style.AlignConsecutiveAssignments = true; 10289 Style.AlignConsecutiveDeclarations = true; 10290 Style.AlignConsecutiveMacros = false; 10291 10292 verifyFormat("#define a 3\n" 10293 "#define bbbb 4\n" 10294 "#define ccc (5)", 10295 Style); 10296 10297 verifyFormat("#define f(x) (x * x)\n" 10298 "#define fff(x, y, z) (x * y + z)\n" 10299 "#define ffff(x, y) (x - y)", 10300 Style); 10301 10302 verifyFormat("#define foo(x, y) (x + y)\n" 10303 "#define bar (5, 6)(2 + 2)", 10304 Style); 10305 10306 verifyFormat("#define a 3\n" 10307 "#define bbbb 4\n" 10308 "#define ccc (5)\n" 10309 "#define f(x) (x * x)\n" 10310 "#define fff(x, y, z) (x * y + z)\n" 10311 "#define ffff(x, y) (x - y)", 10312 Style); 10313 10314 Style.AlignConsecutiveMacros = true; 10315 verifyFormat("#define a 3\n" 10316 "#define bbbb 4\n" 10317 "#define ccc (5)", 10318 Style); 10319 10320 verifyFormat("#define f(x) (x * x)\n" 10321 "#define fff(x, y, z) (x * y + z)\n" 10322 "#define ffff(x, y) (x - y)", 10323 Style); 10324 10325 verifyFormat("#define foo(x, y) (x + y)\n" 10326 "#define bar (5, 6)(2 + 2)", 10327 Style); 10328 10329 verifyFormat("#define a 3\n" 10330 "#define bbbb 4\n" 10331 "#define ccc (5)\n" 10332 "#define f(x) (x * x)\n" 10333 "#define fff(x, y, z) (x * y + z)\n" 10334 "#define ffff(x, y) (x - y)", 10335 Style); 10336 10337 verifyFormat("#define a 5\n" 10338 "#define foo(x, y) (x + y)\n" 10339 "#define CCC (6)\n" 10340 "auto lambda = []() {\n" 10341 " auto ii = 0;\n" 10342 " float j = 0;\n" 10343 " return 0;\n" 10344 "};\n" 10345 "int i = 0;\n" 10346 "float i2 = 0;\n" 10347 "auto v = type{\n" 10348 " i = 1, //\n" 10349 " (i = 2), //\n" 10350 " i = 3 //\n" 10351 "};", 10352 Style); 10353 10354 Style.AlignConsecutiveMacros = false; 10355 Style.ColumnLimit = 20; 10356 10357 verifyFormat("#define a \\\n" 10358 " \"aabbbbbbbbbbbb\"\n" 10359 "#define D \\\n" 10360 " \"aabbbbbbbbbbbb\" \\\n" 10361 " \"ccddeeeeeeeee\"\n" 10362 "#define B \\\n" 10363 " \"QQQQQQQQQQQQQ\" \\\n" 10364 " \"FFFFFFFFFFFFF\" \\\n" 10365 " \"LLLLLLLL\"\n", 10366 Style); 10367 10368 Style.AlignConsecutiveMacros = true; 10369 verifyFormat("#define a \\\n" 10370 " \"aabbbbbbbbbbbb\"\n" 10371 "#define D \\\n" 10372 " \"aabbbbbbbbbbbb\" \\\n" 10373 " \"ccddeeeeeeeee\"\n" 10374 "#define B \\\n" 10375 " \"QQQQQQQQQQQQQ\" \\\n" 10376 " \"FFFFFFFFFFFFF\" \\\n" 10377 " \"LLLLLLLL\"\n", 10378 Style); 10379 } 10380 10381 TEST_F(FormatTest, AlignConsecutiveAssignments) { 10382 FormatStyle Alignment = getLLVMStyle(); 10383 Alignment.AlignConsecutiveMacros = true; 10384 Alignment.AlignConsecutiveAssignments = false; 10385 verifyFormat("int a = 5;\n" 10386 "int oneTwoThree = 123;", 10387 Alignment); 10388 verifyFormat("int a = 5;\n" 10389 "int oneTwoThree = 123;", 10390 Alignment); 10391 10392 Alignment.AlignConsecutiveAssignments = true; 10393 verifyFormat("int a = 5;\n" 10394 "int oneTwoThree = 123;", 10395 Alignment); 10396 verifyFormat("int a = method();\n" 10397 "int oneTwoThree = 133;", 10398 Alignment); 10399 verifyFormat("a &= 5;\n" 10400 "bcd *= 5;\n" 10401 "ghtyf += 5;\n" 10402 "dvfvdb -= 5;\n" 10403 "a /= 5;\n" 10404 "vdsvsv %= 5;\n" 10405 "sfdbddfbdfbb ^= 5;\n" 10406 "dvsdsv |= 5;\n" 10407 "int dsvvdvsdvvv = 123;", 10408 Alignment); 10409 verifyFormat("int i = 1, j = 10;\n" 10410 "something = 2000;", 10411 Alignment); 10412 verifyFormat("something = 2000;\n" 10413 "int i = 1, j = 10;\n", 10414 Alignment); 10415 verifyFormat("something = 2000;\n" 10416 "another = 911;\n" 10417 "int i = 1, j = 10;\n" 10418 "oneMore = 1;\n" 10419 "i = 2;", 10420 Alignment); 10421 verifyFormat("int a = 5;\n" 10422 "int one = 1;\n" 10423 "method();\n" 10424 "int oneTwoThree = 123;\n" 10425 "int oneTwo = 12;", 10426 Alignment); 10427 verifyFormat("int oneTwoThree = 123;\n" 10428 "int oneTwo = 12;\n" 10429 "method();\n", 10430 Alignment); 10431 verifyFormat("int oneTwoThree = 123; // comment\n" 10432 "int oneTwo = 12; // comment", 10433 Alignment); 10434 EXPECT_EQ("int a = 5;\n" 10435 "\n" 10436 "int oneTwoThree = 123;", 10437 format("int a = 5;\n" 10438 "\n" 10439 "int oneTwoThree= 123;", 10440 Alignment)); 10441 EXPECT_EQ("int a = 5;\n" 10442 "int one = 1;\n" 10443 "\n" 10444 "int oneTwoThree = 123;", 10445 format("int a = 5;\n" 10446 "int one = 1;\n" 10447 "\n" 10448 "int oneTwoThree = 123;", 10449 Alignment)); 10450 EXPECT_EQ("int a = 5;\n" 10451 "int one = 1;\n" 10452 "\n" 10453 "int oneTwoThree = 123;\n" 10454 "int oneTwo = 12;", 10455 format("int a = 5;\n" 10456 "int one = 1;\n" 10457 "\n" 10458 "int oneTwoThree = 123;\n" 10459 "int oneTwo = 12;", 10460 Alignment)); 10461 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 10462 verifyFormat("#define A \\\n" 10463 " int aaaa = 12; \\\n" 10464 " int b = 23; \\\n" 10465 " int ccc = 234; \\\n" 10466 " int dddddddddd = 2345;", 10467 Alignment); 10468 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 10469 verifyFormat("#define A \\\n" 10470 " int aaaa = 12; \\\n" 10471 " int b = 23; \\\n" 10472 " int ccc = 234; \\\n" 10473 " int dddddddddd = 2345;", 10474 Alignment); 10475 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 10476 verifyFormat("#define A " 10477 " \\\n" 10478 " int aaaa = 12; " 10479 " \\\n" 10480 " int b = 23; " 10481 " \\\n" 10482 " int ccc = 234; " 10483 " \\\n" 10484 " int dddddddddd = 2345;", 10485 Alignment); 10486 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 10487 "k = 4, int l = 5,\n" 10488 " int m = 6) {\n" 10489 " int j = 10;\n" 10490 " otherThing = 1;\n" 10491 "}", 10492 Alignment); 10493 verifyFormat("void SomeFunction(int parameter = 0) {\n" 10494 " int i = 1;\n" 10495 " int j = 2;\n" 10496 " int big = 10000;\n" 10497 "}", 10498 Alignment); 10499 verifyFormat("class C {\n" 10500 "public:\n" 10501 " int i = 1;\n" 10502 " virtual void f() = 0;\n" 10503 "};", 10504 Alignment); 10505 verifyFormat("int i = 1;\n" 10506 "if (SomeType t = getSomething()) {\n" 10507 "}\n" 10508 "int j = 2;\n" 10509 "int big = 10000;", 10510 Alignment); 10511 verifyFormat("int j = 7;\n" 10512 "for (int k = 0; k < N; ++k) {\n" 10513 "}\n" 10514 "int j = 2;\n" 10515 "int big = 10000;\n" 10516 "}", 10517 Alignment); 10518 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10519 verifyFormat("int i = 1;\n" 10520 "LooooooooooongType loooooooooooooooooooooongVariable\n" 10521 " = someLooooooooooooooooongFunction();\n" 10522 "int j = 2;", 10523 Alignment); 10524 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 10525 verifyFormat("int i = 1;\n" 10526 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 10527 " someLooooooooooooooooongFunction();\n" 10528 "int j = 2;", 10529 Alignment); 10530 10531 verifyFormat("auto lambda = []() {\n" 10532 " auto i = 0;\n" 10533 " return 0;\n" 10534 "};\n" 10535 "int i = 0;\n" 10536 "auto v = type{\n" 10537 " i = 1, //\n" 10538 " (i = 2), //\n" 10539 " i = 3 //\n" 10540 "};", 10541 Alignment); 10542 10543 verifyFormat( 10544 "int i = 1;\n" 10545 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 10546 " loooooooooooooooooooooongParameterB);\n" 10547 "int j = 2;", 10548 Alignment); 10549 10550 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 10551 " typename B = very_long_type_name_1,\n" 10552 " typename T_2 = very_long_type_name_2>\n" 10553 "auto foo() {}\n", 10554 Alignment); 10555 verifyFormat("int a, b = 1;\n" 10556 "int c = 2;\n" 10557 "int dd = 3;\n", 10558 Alignment); 10559 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 10560 "float b[1][] = {{3.f}};\n", 10561 Alignment); 10562 verifyFormat("for (int i = 0; i < 1; i++)\n" 10563 " int x = 1;\n", 10564 Alignment); 10565 verifyFormat("for (i = 0; i < 1; i++)\n" 10566 " x = 1;\n" 10567 "y = 1;\n", 10568 Alignment); 10569 } 10570 10571 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 10572 FormatStyle Alignment = getLLVMStyle(); 10573 Alignment.AlignConsecutiveMacros = true; 10574 Alignment.AlignConsecutiveDeclarations = false; 10575 verifyFormat("float const a = 5;\n" 10576 "int oneTwoThree = 123;", 10577 Alignment); 10578 verifyFormat("int a = 5;\n" 10579 "float const oneTwoThree = 123;", 10580 Alignment); 10581 10582 Alignment.AlignConsecutiveDeclarations = true; 10583 verifyFormat("float const a = 5;\n" 10584 "int oneTwoThree = 123;", 10585 Alignment); 10586 verifyFormat("int a = method();\n" 10587 "float const oneTwoThree = 133;", 10588 Alignment); 10589 verifyFormat("int i = 1, j = 10;\n" 10590 "something = 2000;", 10591 Alignment); 10592 verifyFormat("something = 2000;\n" 10593 "int i = 1, j = 10;\n", 10594 Alignment); 10595 verifyFormat("float something = 2000;\n" 10596 "double another = 911;\n" 10597 "int i = 1, j = 10;\n" 10598 "const int *oneMore = 1;\n" 10599 "unsigned i = 2;", 10600 Alignment); 10601 verifyFormat("float a = 5;\n" 10602 "int one = 1;\n" 10603 "method();\n" 10604 "const double oneTwoThree = 123;\n" 10605 "const unsigned int oneTwo = 12;", 10606 Alignment); 10607 verifyFormat("int oneTwoThree{0}; // comment\n" 10608 "unsigned oneTwo; // comment", 10609 Alignment); 10610 EXPECT_EQ("float const a = 5;\n" 10611 "\n" 10612 "int oneTwoThree = 123;", 10613 format("float const a = 5;\n" 10614 "\n" 10615 "int oneTwoThree= 123;", 10616 Alignment)); 10617 EXPECT_EQ("float a = 5;\n" 10618 "int one = 1;\n" 10619 "\n" 10620 "unsigned oneTwoThree = 123;", 10621 format("float a = 5;\n" 10622 "int one = 1;\n" 10623 "\n" 10624 "unsigned oneTwoThree = 123;", 10625 Alignment)); 10626 EXPECT_EQ("float a = 5;\n" 10627 "int one = 1;\n" 10628 "\n" 10629 "unsigned oneTwoThree = 123;\n" 10630 "int oneTwo = 12;", 10631 format("float a = 5;\n" 10632 "int one = 1;\n" 10633 "\n" 10634 "unsigned oneTwoThree = 123;\n" 10635 "int oneTwo = 12;", 10636 Alignment)); 10637 // Function prototype alignment 10638 verifyFormat("int a();\n" 10639 "double b();", 10640 Alignment); 10641 verifyFormat("int a(int x);\n" 10642 "double b();", 10643 Alignment); 10644 unsigned OldColumnLimit = Alignment.ColumnLimit; 10645 // We need to set ColumnLimit to zero, in order to stress nested alignments, 10646 // otherwise the function parameters will be re-flowed onto a single line. 10647 Alignment.ColumnLimit = 0; 10648 EXPECT_EQ("int a(int x,\n" 10649 " float y);\n" 10650 "double b(int x,\n" 10651 " double y);", 10652 format("int a(int x,\n" 10653 " float y);\n" 10654 "double b(int x,\n" 10655 " double y);", 10656 Alignment)); 10657 // This ensures that function parameters of function declarations are 10658 // correctly indented when their owning functions are indented. 10659 // The failure case here is for 'double y' to not be indented enough. 10660 EXPECT_EQ("double a(int x);\n" 10661 "int b(int y,\n" 10662 " double z);", 10663 format("double a(int x);\n" 10664 "int b(int y,\n" 10665 " double z);", 10666 Alignment)); 10667 // Set ColumnLimit low so that we induce wrapping immediately after 10668 // the function name and opening paren. 10669 Alignment.ColumnLimit = 13; 10670 verifyFormat("int function(\n" 10671 " int x,\n" 10672 " bool y);", 10673 Alignment); 10674 Alignment.ColumnLimit = OldColumnLimit; 10675 // Ensure function pointers don't screw up recursive alignment 10676 verifyFormat("int a(int x, void (*fp)(int y));\n" 10677 "double b();", 10678 Alignment); 10679 Alignment.AlignConsecutiveAssignments = true; 10680 // Ensure recursive alignment is broken by function braces, so that the 10681 // "a = 1" does not align with subsequent assignments inside the function 10682 // body. 10683 verifyFormat("int func(int a = 1) {\n" 10684 " int b = 2;\n" 10685 " int cc = 3;\n" 10686 "}", 10687 Alignment); 10688 verifyFormat("float something = 2000;\n" 10689 "double another = 911;\n" 10690 "int i = 1, j = 10;\n" 10691 "const int *oneMore = 1;\n" 10692 "unsigned i = 2;", 10693 Alignment); 10694 verifyFormat("int oneTwoThree = {0}; // comment\n" 10695 "unsigned oneTwo = 0; // comment", 10696 Alignment); 10697 // Make sure that scope is correctly tracked, in the absence of braces 10698 verifyFormat("for (int i = 0; i < n; i++)\n" 10699 " j = i;\n" 10700 "double x = 1;\n", 10701 Alignment); 10702 verifyFormat("if (int i = 0)\n" 10703 " j = i;\n" 10704 "double x = 1;\n", 10705 Alignment); 10706 // Ensure operator[] and operator() are comprehended 10707 verifyFormat("struct test {\n" 10708 " long long int foo();\n" 10709 " int operator[](int a);\n" 10710 " double bar();\n" 10711 "};\n", 10712 Alignment); 10713 verifyFormat("struct test {\n" 10714 " long long int foo();\n" 10715 " int operator()(int a);\n" 10716 " double bar();\n" 10717 "};\n", 10718 Alignment); 10719 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 10720 " int const i = 1;\n" 10721 " int * j = 2;\n" 10722 " int big = 10000;\n" 10723 "\n" 10724 " unsigned oneTwoThree = 123;\n" 10725 " int oneTwo = 12;\n" 10726 " method();\n" 10727 " float k = 2;\n" 10728 " int ll = 10000;\n" 10729 "}", 10730 format("void SomeFunction(int parameter= 0) {\n" 10731 " int const i= 1;\n" 10732 " int *j=2;\n" 10733 " int big = 10000;\n" 10734 "\n" 10735 "unsigned oneTwoThree =123;\n" 10736 "int oneTwo = 12;\n" 10737 " method();\n" 10738 "float k= 2;\n" 10739 "int ll=10000;\n" 10740 "}", 10741 Alignment)); 10742 Alignment.AlignConsecutiveAssignments = false; 10743 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 10744 verifyFormat("#define A \\\n" 10745 " int aaaa = 12; \\\n" 10746 " float b = 23; \\\n" 10747 " const int ccc = 234; \\\n" 10748 " unsigned dddddddddd = 2345;", 10749 Alignment); 10750 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 10751 verifyFormat("#define A \\\n" 10752 " int aaaa = 12; \\\n" 10753 " float b = 23; \\\n" 10754 " const int ccc = 234; \\\n" 10755 " unsigned dddddddddd = 2345;", 10756 Alignment); 10757 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 10758 Alignment.ColumnLimit = 30; 10759 verifyFormat("#define A \\\n" 10760 " int aaaa = 12; \\\n" 10761 " float b = 23; \\\n" 10762 " const int ccc = 234; \\\n" 10763 " int dddddddddd = 2345;", 10764 Alignment); 10765 Alignment.ColumnLimit = 80; 10766 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 10767 "k = 4, int l = 5,\n" 10768 " int m = 6) {\n" 10769 " const int j = 10;\n" 10770 " otherThing = 1;\n" 10771 "}", 10772 Alignment); 10773 verifyFormat("void SomeFunction(int parameter = 0) {\n" 10774 " int const i = 1;\n" 10775 " int * j = 2;\n" 10776 " int big = 10000;\n" 10777 "}", 10778 Alignment); 10779 verifyFormat("class C {\n" 10780 "public:\n" 10781 " int i = 1;\n" 10782 " virtual void f() = 0;\n" 10783 "};", 10784 Alignment); 10785 verifyFormat("float i = 1;\n" 10786 "if (SomeType t = getSomething()) {\n" 10787 "}\n" 10788 "const unsigned j = 2;\n" 10789 "int big = 10000;", 10790 Alignment); 10791 verifyFormat("float j = 7;\n" 10792 "for (int k = 0; k < N; ++k) {\n" 10793 "}\n" 10794 "unsigned j = 2;\n" 10795 "int big = 10000;\n" 10796 "}", 10797 Alignment); 10798 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10799 verifyFormat("float i = 1;\n" 10800 "LooooooooooongType loooooooooooooooooooooongVariable\n" 10801 " = someLooooooooooooooooongFunction();\n" 10802 "int j = 2;", 10803 Alignment); 10804 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 10805 verifyFormat("int i = 1;\n" 10806 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 10807 " someLooooooooooooooooongFunction();\n" 10808 "int j = 2;", 10809 Alignment); 10810 10811 Alignment.AlignConsecutiveAssignments = true; 10812 verifyFormat("auto lambda = []() {\n" 10813 " auto ii = 0;\n" 10814 " float j = 0;\n" 10815 " return 0;\n" 10816 "};\n" 10817 "int i = 0;\n" 10818 "float i2 = 0;\n" 10819 "auto v = type{\n" 10820 " i = 1, //\n" 10821 " (i = 2), //\n" 10822 " i = 3 //\n" 10823 "};", 10824 Alignment); 10825 Alignment.AlignConsecutiveAssignments = false; 10826 10827 verifyFormat( 10828 "int i = 1;\n" 10829 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 10830 " loooooooooooooooooooooongParameterB);\n" 10831 "int j = 2;", 10832 Alignment); 10833 10834 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 10835 // We expect declarations and assignments to align, as long as it doesn't 10836 // exceed the column limit, starting a new alignment sequence whenever it 10837 // happens. 10838 Alignment.AlignConsecutiveAssignments = true; 10839 Alignment.ColumnLimit = 30; 10840 verifyFormat("float ii = 1;\n" 10841 "unsigned j = 2;\n" 10842 "int someVerylongVariable = 1;\n" 10843 "AnotherLongType ll = 123456;\n" 10844 "VeryVeryLongType k = 2;\n" 10845 "int myvar = 1;", 10846 Alignment); 10847 Alignment.ColumnLimit = 80; 10848 Alignment.AlignConsecutiveAssignments = false; 10849 10850 verifyFormat( 10851 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 10852 " typename LongType, typename B>\n" 10853 "auto foo() {}\n", 10854 Alignment); 10855 verifyFormat("float a, b = 1;\n" 10856 "int c = 2;\n" 10857 "int dd = 3;\n", 10858 Alignment); 10859 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 10860 "float b[1][] = {{3.f}};\n", 10861 Alignment); 10862 Alignment.AlignConsecutiveAssignments = true; 10863 verifyFormat("float a, b = 1;\n" 10864 "int c = 2;\n" 10865 "int dd = 3;\n", 10866 Alignment); 10867 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 10868 "float b[1][] = {{3.f}};\n", 10869 Alignment); 10870 Alignment.AlignConsecutiveAssignments = false; 10871 10872 Alignment.ColumnLimit = 30; 10873 Alignment.BinPackParameters = false; 10874 verifyFormat("void foo(float a,\n" 10875 " float b,\n" 10876 " int c,\n" 10877 " uint32_t *d) {\n" 10878 " int * e = 0;\n" 10879 " float f = 0;\n" 10880 " double g = 0;\n" 10881 "}\n" 10882 "void bar(ino_t a,\n" 10883 " int b,\n" 10884 " uint32_t *c,\n" 10885 " bool d) {}\n", 10886 Alignment); 10887 Alignment.BinPackParameters = true; 10888 Alignment.ColumnLimit = 80; 10889 10890 // Bug 33507 10891 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 10892 verifyFormat( 10893 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 10894 " static const Version verVs2017;\n" 10895 " return true;\n" 10896 "});\n", 10897 Alignment); 10898 Alignment.PointerAlignment = FormatStyle::PAS_Right; 10899 10900 // See llvm.org/PR35641 10901 Alignment.AlignConsecutiveDeclarations = true; 10902 verifyFormat("int func() { //\n" 10903 " int b;\n" 10904 " unsigned c;\n" 10905 "}", 10906 Alignment); 10907 10908 // See PR37175 10909 FormatStyle Style = getMozillaStyle(); 10910 Style.AlignConsecutiveDeclarations = true; 10911 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 10912 "foo(int a);", 10913 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 10914 } 10915 10916 TEST_F(FormatTest, LinuxBraceBreaking) { 10917 FormatStyle LinuxBraceStyle = getLLVMStyle(); 10918 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 10919 verifyFormat("namespace a\n" 10920 "{\n" 10921 "class A\n" 10922 "{\n" 10923 " void f()\n" 10924 " {\n" 10925 " if (true) {\n" 10926 " a();\n" 10927 " b();\n" 10928 " } else {\n" 10929 " a();\n" 10930 " }\n" 10931 " }\n" 10932 " void g() { return; }\n" 10933 "};\n" 10934 "struct B {\n" 10935 " int x;\n" 10936 "};\n" 10937 "} // namespace a\n", 10938 LinuxBraceStyle); 10939 verifyFormat("enum X {\n" 10940 " Y = 0,\n" 10941 "}\n", 10942 LinuxBraceStyle); 10943 verifyFormat("struct S {\n" 10944 " int Type;\n" 10945 " union {\n" 10946 " int x;\n" 10947 " double y;\n" 10948 " } Value;\n" 10949 " class C\n" 10950 " {\n" 10951 " MyFavoriteType Value;\n" 10952 " } Class;\n" 10953 "}\n", 10954 LinuxBraceStyle); 10955 } 10956 10957 TEST_F(FormatTest, MozillaBraceBreaking) { 10958 FormatStyle MozillaBraceStyle = getLLVMStyle(); 10959 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 10960 MozillaBraceStyle.FixNamespaceComments = false; 10961 verifyFormat("namespace a {\n" 10962 "class A\n" 10963 "{\n" 10964 " void f()\n" 10965 " {\n" 10966 " if (true) {\n" 10967 " a();\n" 10968 " b();\n" 10969 " }\n" 10970 " }\n" 10971 " void g() { return; }\n" 10972 "};\n" 10973 "enum E\n" 10974 "{\n" 10975 " A,\n" 10976 " // foo\n" 10977 " B,\n" 10978 " C\n" 10979 "};\n" 10980 "struct B\n" 10981 "{\n" 10982 " int x;\n" 10983 "};\n" 10984 "}\n", 10985 MozillaBraceStyle); 10986 verifyFormat("struct S\n" 10987 "{\n" 10988 " int Type;\n" 10989 " union\n" 10990 " {\n" 10991 " int x;\n" 10992 " double y;\n" 10993 " } Value;\n" 10994 " class C\n" 10995 " {\n" 10996 " MyFavoriteType Value;\n" 10997 " } Class;\n" 10998 "}\n", 10999 MozillaBraceStyle); 11000 } 11001 11002 TEST_F(FormatTest, StroustrupBraceBreaking) { 11003 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 11004 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 11005 verifyFormat("namespace a {\n" 11006 "class A {\n" 11007 " void f()\n" 11008 " {\n" 11009 " if (true) {\n" 11010 " a();\n" 11011 " b();\n" 11012 " }\n" 11013 " }\n" 11014 " void g() { return; }\n" 11015 "};\n" 11016 "struct B {\n" 11017 " int x;\n" 11018 "};\n" 11019 "} // namespace a\n", 11020 StroustrupBraceStyle); 11021 11022 verifyFormat("void foo()\n" 11023 "{\n" 11024 " if (a) {\n" 11025 " a();\n" 11026 " }\n" 11027 " else {\n" 11028 " b();\n" 11029 " }\n" 11030 "}\n", 11031 StroustrupBraceStyle); 11032 11033 verifyFormat("#ifdef _DEBUG\n" 11034 "int foo(int i = 0)\n" 11035 "#else\n" 11036 "int foo(int i = 5)\n" 11037 "#endif\n" 11038 "{\n" 11039 " return i;\n" 11040 "}", 11041 StroustrupBraceStyle); 11042 11043 verifyFormat("void foo() {}\n" 11044 "void bar()\n" 11045 "#ifdef _DEBUG\n" 11046 "{\n" 11047 " foo();\n" 11048 "}\n" 11049 "#else\n" 11050 "{\n" 11051 "}\n" 11052 "#endif", 11053 StroustrupBraceStyle); 11054 11055 verifyFormat("void foobar() { int i = 5; }\n" 11056 "#ifdef _DEBUG\n" 11057 "void bar() {}\n" 11058 "#else\n" 11059 "void bar() { foobar(); }\n" 11060 "#endif", 11061 StroustrupBraceStyle); 11062 } 11063 11064 TEST_F(FormatTest, AllmanBraceBreaking) { 11065 FormatStyle AllmanBraceStyle = getLLVMStyle(); 11066 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 11067 11068 EXPECT_EQ("namespace a\n" 11069 "{\n" 11070 "void f();\n" 11071 "void g();\n" 11072 "} // namespace a\n", 11073 format("namespace a\n" 11074 "{\n" 11075 "void f();\n" 11076 "void g();\n" 11077 "}\n", 11078 AllmanBraceStyle)); 11079 11080 verifyFormat("namespace a\n" 11081 "{\n" 11082 "class A\n" 11083 "{\n" 11084 " void f()\n" 11085 " {\n" 11086 " if (true)\n" 11087 " {\n" 11088 " a();\n" 11089 " b();\n" 11090 " }\n" 11091 " }\n" 11092 " void g() { return; }\n" 11093 "};\n" 11094 "struct B\n" 11095 "{\n" 11096 " int x;\n" 11097 "};\n" 11098 "union C\n" 11099 "{\n" 11100 "};\n" 11101 "} // namespace a", 11102 AllmanBraceStyle); 11103 11104 verifyFormat("void f()\n" 11105 "{\n" 11106 " if (true)\n" 11107 " {\n" 11108 " a();\n" 11109 " }\n" 11110 " else if (false)\n" 11111 " {\n" 11112 " b();\n" 11113 " }\n" 11114 " else\n" 11115 " {\n" 11116 " c();\n" 11117 " }\n" 11118 "}\n", 11119 AllmanBraceStyle); 11120 11121 verifyFormat("void f()\n" 11122 "{\n" 11123 " for (int i = 0; i < 10; ++i)\n" 11124 " {\n" 11125 " a();\n" 11126 " }\n" 11127 " while (false)\n" 11128 " {\n" 11129 " b();\n" 11130 " }\n" 11131 " do\n" 11132 " {\n" 11133 " c();\n" 11134 " } while (false)\n" 11135 "}\n", 11136 AllmanBraceStyle); 11137 11138 verifyFormat("void f(int a)\n" 11139 "{\n" 11140 " switch (a)\n" 11141 " {\n" 11142 " case 0:\n" 11143 " break;\n" 11144 " case 1:\n" 11145 " {\n" 11146 " break;\n" 11147 " }\n" 11148 " case 2:\n" 11149 " {\n" 11150 " }\n" 11151 " break;\n" 11152 " default:\n" 11153 " break;\n" 11154 " }\n" 11155 "}\n", 11156 AllmanBraceStyle); 11157 11158 verifyFormat("enum X\n" 11159 "{\n" 11160 " Y = 0,\n" 11161 "}\n", 11162 AllmanBraceStyle); 11163 verifyFormat("enum X\n" 11164 "{\n" 11165 " Y = 0\n" 11166 "}\n", 11167 AllmanBraceStyle); 11168 11169 verifyFormat("@interface BSApplicationController ()\n" 11170 "{\n" 11171 "@private\n" 11172 " id _extraIvar;\n" 11173 "}\n" 11174 "@end\n", 11175 AllmanBraceStyle); 11176 11177 verifyFormat("#ifdef _DEBUG\n" 11178 "int foo(int i = 0)\n" 11179 "#else\n" 11180 "int foo(int i = 5)\n" 11181 "#endif\n" 11182 "{\n" 11183 " return i;\n" 11184 "}", 11185 AllmanBraceStyle); 11186 11187 verifyFormat("void foo() {}\n" 11188 "void bar()\n" 11189 "#ifdef _DEBUG\n" 11190 "{\n" 11191 " foo();\n" 11192 "}\n" 11193 "#else\n" 11194 "{\n" 11195 "}\n" 11196 "#endif", 11197 AllmanBraceStyle); 11198 11199 verifyFormat("void foobar() { int i = 5; }\n" 11200 "#ifdef _DEBUG\n" 11201 "void bar() {}\n" 11202 "#else\n" 11203 "void bar() { foobar(); }\n" 11204 "#endif", 11205 AllmanBraceStyle); 11206 11207 // This shouldn't affect ObjC blocks.. 11208 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 11209 " // ...\n" 11210 " int i;\n" 11211 "}];", 11212 AllmanBraceStyle); 11213 verifyFormat("void (^block)(void) = ^{\n" 11214 " // ...\n" 11215 " int i;\n" 11216 "};", 11217 AllmanBraceStyle); 11218 // .. or dict literals. 11219 verifyFormat("void f()\n" 11220 "{\n" 11221 " // ...\n" 11222 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 11223 "}", 11224 AllmanBraceStyle); 11225 verifyFormat("void f()\n" 11226 "{\n" 11227 " // ...\n" 11228 " [object someMethod:@{a : @\"b\"}];\n" 11229 "}", 11230 AllmanBraceStyle); 11231 verifyFormat("int f()\n" 11232 "{ // comment\n" 11233 " return 42;\n" 11234 "}", 11235 AllmanBraceStyle); 11236 11237 AllmanBraceStyle.ColumnLimit = 19; 11238 verifyFormat("void f() { int i; }", AllmanBraceStyle); 11239 AllmanBraceStyle.ColumnLimit = 18; 11240 verifyFormat("void f()\n" 11241 "{\n" 11242 " int i;\n" 11243 "}", 11244 AllmanBraceStyle); 11245 AllmanBraceStyle.ColumnLimit = 80; 11246 11247 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 11248 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 11249 FormatStyle::SIS_WithoutElse; 11250 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 11251 verifyFormat("void f(bool b)\n" 11252 "{\n" 11253 " if (b)\n" 11254 " {\n" 11255 " return;\n" 11256 " }\n" 11257 "}\n", 11258 BreakBeforeBraceShortIfs); 11259 verifyFormat("void f(bool b)\n" 11260 "{\n" 11261 " if constexpr (b)\n" 11262 " {\n" 11263 " return;\n" 11264 " }\n" 11265 "}\n", 11266 BreakBeforeBraceShortIfs); 11267 verifyFormat("void f(bool b)\n" 11268 "{\n" 11269 " if CONSTEXPR (b)\n" 11270 " {\n" 11271 " return;\n" 11272 " }\n" 11273 "}\n", 11274 BreakBeforeBraceShortIfs); 11275 verifyFormat("void f(bool b)\n" 11276 "{\n" 11277 " if (b) return;\n" 11278 "}\n", 11279 BreakBeforeBraceShortIfs); 11280 verifyFormat("void f(bool b)\n" 11281 "{\n" 11282 " if constexpr (b) return;\n" 11283 "}\n", 11284 BreakBeforeBraceShortIfs); 11285 verifyFormat("void f(bool b)\n" 11286 "{\n" 11287 " if CONSTEXPR (b) return;\n" 11288 "}\n", 11289 BreakBeforeBraceShortIfs); 11290 verifyFormat("void f(bool b)\n" 11291 "{\n" 11292 " while (b)\n" 11293 " {\n" 11294 " return;\n" 11295 " }\n" 11296 "}\n", 11297 BreakBeforeBraceShortIfs); 11298 } 11299 11300 TEST_F(FormatTest, GNUBraceBreaking) { 11301 FormatStyle GNUBraceStyle = getLLVMStyle(); 11302 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 11303 verifyFormat("namespace a\n" 11304 "{\n" 11305 "class A\n" 11306 "{\n" 11307 " void f()\n" 11308 " {\n" 11309 " int a;\n" 11310 " {\n" 11311 " int b;\n" 11312 " }\n" 11313 " if (true)\n" 11314 " {\n" 11315 " a();\n" 11316 " b();\n" 11317 " }\n" 11318 " }\n" 11319 " void g() { return; }\n" 11320 "}\n" 11321 "} // namespace a", 11322 GNUBraceStyle); 11323 11324 verifyFormat("void f()\n" 11325 "{\n" 11326 " if (true)\n" 11327 " {\n" 11328 " a();\n" 11329 " }\n" 11330 " else if (false)\n" 11331 " {\n" 11332 " b();\n" 11333 " }\n" 11334 " else\n" 11335 " {\n" 11336 " c();\n" 11337 " }\n" 11338 "}\n", 11339 GNUBraceStyle); 11340 11341 verifyFormat("void f()\n" 11342 "{\n" 11343 " for (int i = 0; i < 10; ++i)\n" 11344 " {\n" 11345 " a();\n" 11346 " }\n" 11347 " while (false)\n" 11348 " {\n" 11349 " b();\n" 11350 " }\n" 11351 " do\n" 11352 " {\n" 11353 " c();\n" 11354 " }\n" 11355 " while (false);\n" 11356 "}\n", 11357 GNUBraceStyle); 11358 11359 verifyFormat("void f(int a)\n" 11360 "{\n" 11361 " switch (a)\n" 11362 " {\n" 11363 " case 0:\n" 11364 " break;\n" 11365 " case 1:\n" 11366 " {\n" 11367 " break;\n" 11368 " }\n" 11369 " case 2:\n" 11370 " {\n" 11371 " }\n" 11372 " break;\n" 11373 " default:\n" 11374 " break;\n" 11375 " }\n" 11376 "}\n", 11377 GNUBraceStyle); 11378 11379 verifyFormat("enum X\n" 11380 "{\n" 11381 " Y = 0,\n" 11382 "}\n", 11383 GNUBraceStyle); 11384 11385 verifyFormat("@interface BSApplicationController ()\n" 11386 "{\n" 11387 "@private\n" 11388 " id _extraIvar;\n" 11389 "}\n" 11390 "@end\n", 11391 GNUBraceStyle); 11392 11393 verifyFormat("#ifdef _DEBUG\n" 11394 "int foo(int i = 0)\n" 11395 "#else\n" 11396 "int foo(int i = 5)\n" 11397 "#endif\n" 11398 "{\n" 11399 " return i;\n" 11400 "}", 11401 GNUBraceStyle); 11402 11403 verifyFormat("void foo() {}\n" 11404 "void bar()\n" 11405 "#ifdef _DEBUG\n" 11406 "{\n" 11407 " foo();\n" 11408 "}\n" 11409 "#else\n" 11410 "{\n" 11411 "}\n" 11412 "#endif", 11413 GNUBraceStyle); 11414 11415 verifyFormat("void foobar() { int i = 5; }\n" 11416 "#ifdef _DEBUG\n" 11417 "void bar() {}\n" 11418 "#else\n" 11419 "void bar() { foobar(); }\n" 11420 "#endif", 11421 GNUBraceStyle); 11422 } 11423 11424 TEST_F(FormatTest, WebKitBraceBreaking) { 11425 FormatStyle WebKitBraceStyle = getLLVMStyle(); 11426 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 11427 WebKitBraceStyle.FixNamespaceComments = false; 11428 verifyFormat("namespace a {\n" 11429 "class A {\n" 11430 " void f()\n" 11431 " {\n" 11432 " if (true) {\n" 11433 " a();\n" 11434 " b();\n" 11435 " }\n" 11436 " }\n" 11437 " void g() { return; }\n" 11438 "};\n" 11439 "enum E {\n" 11440 " A,\n" 11441 " // foo\n" 11442 " B,\n" 11443 " C\n" 11444 "};\n" 11445 "struct B {\n" 11446 " int x;\n" 11447 "};\n" 11448 "}\n", 11449 WebKitBraceStyle); 11450 verifyFormat("struct S {\n" 11451 " int Type;\n" 11452 " union {\n" 11453 " int x;\n" 11454 " double y;\n" 11455 " } Value;\n" 11456 " class C {\n" 11457 " MyFavoriteType Value;\n" 11458 " } Class;\n" 11459 "};\n", 11460 WebKitBraceStyle); 11461 } 11462 11463 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 11464 verifyFormat("void f() {\n" 11465 " try {\n" 11466 " } catch (const Exception &e) {\n" 11467 " }\n" 11468 "}\n", 11469 getLLVMStyle()); 11470 } 11471 11472 TEST_F(FormatTest, UnderstandsPragmas) { 11473 verifyFormat("#pragma omp reduction(| : var)"); 11474 verifyFormat("#pragma omp reduction(+ : var)"); 11475 11476 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 11477 "(including parentheses).", 11478 format("#pragma mark Any non-hyphenated or hyphenated string " 11479 "(including parentheses).")); 11480 } 11481 11482 TEST_F(FormatTest, UnderstandPragmaOption) { 11483 verifyFormat("#pragma option -C -A"); 11484 11485 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 11486 } 11487 11488 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 11489 FormatStyle Style = getLLVMStyle(); 11490 Style.ColumnLimit = 20; 11491 11492 // See PR41213 11493 EXPECT_EQ("/*\n" 11494 " *\t9012345\n" 11495 " * /8901\n" 11496 " */", 11497 format("/*\n" 11498 " *\t9012345 /8901\n" 11499 " */", 11500 Style)); 11501 EXPECT_EQ("/*\n" 11502 " *345678\n" 11503 " *\t/8901\n" 11504 " */", 11505 format("/*\n" 11506 " *345678\t/8901\n" 11507 " */", 11508 Style)); 11509 11510 verifyFormat("int a; // the\n" 11511 " // comment", Style); 11512 EXPECT_EQ("int a; /* first line\n" 11513 " * second\n" 11514 " * line third\n" 11515 " * line\n" 11516 " */", 11517 format("int a; /* first line\n" 11518 " * second\n" 11519 " * line third\n" 11520 " * line\n" 11521 " */", 11522 Style)); 11523 EXPECT_EQ("int a; // first line\n" 11524 " // second\n" 11525 " // line third\n" 11526 " // line", 11527 format("int a; // first line\n" 11528 " // second line\n" 11529 " // third line", 11530 Style)); 11531 11532 Style.PenaltyExcessCharacter = 90; 11533 verifyFormat("int a; // the comment", Style); 11534 EXPECT_EQ("int a; // the comment\n" 11535 " // aaa", 11536 format("int a; // the comment aaa", Style)); 11537 EXPECT_EQ("int a; /* first line\n" 11538 " * second line\n" 11539 " * third line\n" 11540 " */", 11541 format("int a; /* first line\n" 11542 " * second line\n" 11543 " * third line\n" 11544 " */", 11545 Style)); 11546 EXPECT_EQ("int a; // first line\n" 11547 " // second line\n" 11548 " // third line", 11549 format("int a; // first line\n" 11550 " // second line\n" 11551 " // third line", 11552 Style)); 11553 // FIXME: Investigate why this is not getting the same layout as the test 11554 // above. 11555 EXPECT_EQ("int a; /* first line\n" 11556 " * second line\n" 11557 " * third line\n" 11558 " */", 11559 format("int a; /* first line second line third line" 11560 "\n*/", 11561 Style)); 11562 11563 EXPECT_EQ("// foo bar baz bazfoo\n" 11564 "// foo bar foo bar\n", 11565 format("// foo bar baz bazfoo\n" 11566 "// foo bar foo bar\n", 11567 Style)); 11568 EXPECT_EQ("// foo bar baz bazfoo\n" 11569 "// foo bar foo bar\n", 11570 format("// foo bar baz bazfoo\n" 11571 "// foo bar foo bar\n", 11572 Style)); 11573 11574 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 11575 // next one. 11576 EXPECT_EQ("// foo bar baz bazfoo\n" 11577 "// bar foo bar\n", 11578 format("// foo bar baz bazfoo bar\n" 11579 "// foo bar\n", 11580 Style)); 11581 11582 EXPECT_EQ("// foo bar baz bazfoo\n" 11583 "// foo bar baz bazfoo\n" 11584 "// bar foo bar\n", 11585 format("// foo bar baz bazfoo\n" 11586 "// foo bar baz bazfoo bar\n" 11587 "// foo bar\n", 11588 Style)); 11589 11590 EXPECT_EQ("// foo bar baz bazfoo\n" 11591 "// foo bar baz bazfoo\n" 11592 "// bar foo bar\n", 11593 format("// foo bar baz bazfoo\n" 11594 "// foo bar baz bazfoo bar\n" 11595 "// foo bar\n", 11596 Style)); 11597 11598 // Make sure we do not keep protruding characters if strict mode reflow is 11599 // cheaper than keeping protruding characters. 11600 Style.ColumnLimit = 21; 11601 EXPECT_EQ("// foo foo foo foo\n" 11602 "// foo foo foo foo\n" 11603 "// foo foo foo foo\n", 11604 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", 11605 Style)); 11606 11607 EXPECT_EQ("int a = /* long block\n" 11608 " comment */\n" 11609 " 42;", 11610 format("int a = /* long block comment */ 42;", Style)); 11611 } 11612 11613 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 11614 for (size_t i = 1; i < Styles.size(); ++i) \ 11615 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 11616 << " differs from Style #0" 11617 11618 TEST_F(FormatTest, GetsPredefinedStyleByName) { 11619 SmallVector<FormatStyle, 3> Styles; 11620 Styles.resize(3); 11621 11622 Styles[0] = getLLVMStyle(); 11623 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 11624 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 11625 EXPECT_ALL_STYLES_EQUAL(Styles); 11626 11627 Styles[0] = getGoogleStyle(); 11628 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 11629 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 11630 EXPECT_ALL_STYLES_EQUAL(Styles); 11631 11632 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 11633 EXPECT_TRUE( 11634 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 11635 EXPECT_TRUE( 11636 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 11637 EXPECT_ALL_STYLES_EQUAL(Styles); 11638 11639 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 11640 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 11641 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 11642 EXPECT_ALL_STYLES_EQUAL(Styles); 11643 11644 Styles[0] = getMozillaStyle(); 11645 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 11646 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 11647 EXPECT_ALL_STYLES_EQUAL(Styles); 11648 11649 Styles[0] = getWebKitStyle(); 11650 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 11651 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 11652 EXPECT_ALL_STYLES_EQUAL(Styles); 11653 11654 Styles[0] = getGNUStyle(); 11655 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 11656 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 11657 EXPECT_ALL_STYLES_EQUAL(Styles); 11658 11659 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 11660 } 11661 11662 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 11663 SmallVector<FormatStyle, 8> Styles; 11664 Styles.resize(2); 11665 11666 Styles[0] = getGoogleStyle(); 11667 Styles[1] = getLLVMStyle(); 11668 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 11669 EXPECT_ALL_STYLES_EQUAL(Styles); 11670 11671 Styles.resize(5); 11672 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 11673 Styles[1] = getLLVMStyle(); 11674 Styles[1].Language = FormatStyle::LK_JavaScript; 11675 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 11676 11677 Styles[2] = getLLVMStyle(); 11678 Styles[2].Language = FormatStyle::LK_JavaScript; 11679 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 11680 "BasedOnStyle: Google", 11681 &Styles[2]) 11682 .value()); 11683 11684 Styles[3] = getLLVMStyle(); 11685 Styles[3].Language = FormatStyle::LK_JavaScript; 11686 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 11687 "Language: JavaScript", 11688 &Styles[3]) 11689 .value()); 11690 11691 Styles[4] = getLLVMStyle(); 11692 Styles[4].Language = FormatStyle::LK_JavaScript; 11693 EXPECT_EQ(0, parseConfiguration("---\n" 11694 "BasedOnStyle: LLVM\n" 11695 "IndentWidth: 123\n" 11696 "---\n" 11697 "BasedOnStyle: Google\n" 11698 "Language: JavaScript", 11699 &Styles[4]) 11700 .value()); 11701 EXPECT_ALL_STYLES_EQUAL(Styles); 11702 } 11703 11704 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 11705 Style.FIELD = false; \ 11706 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 11707 EXPECT_TRUE(Style.FIELD); \ 11708 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 11709 EXPECT_FALSE(Style.FIELD); 11710 11711 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 11712 11713 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 11714 Style.STRUCT.FIELD = false; \ 11715 EXPECT_EQ(0, \ 11716 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 11717 .value()); \ 11718 EXPECT_TRUE(Style.STRUCT.FIELD); \ 11719 EXPECT_EQ(0, \ 11720 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 11721 .value()); \ 11722 EXPECT_FALSE(Style.STRUCT.FIELD); 11723 11724 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 11725 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 11726 11727 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 11728 EXPECT_NE(VALUE, Style.FIELD); \ 11729 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 11730 EXPECT_EQ(VALUE, Style.FIELD) 11731 11732 TEST_F(FormatTest, ParsesConfigurationBools) { 11733 FormatStyle Style = {}; 11734 Style.Language = FormatStyle::LK_Cpp; 11735 CHECK_PARSE_BOOL(AlignOperands); 11736 CHECK_PARSE_BOOL(AlignTrailingComments); 11737 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 11738 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 11739 CHECK_PARSE_BOOL(AlignConsecutiveMacros); 11740 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 11741 CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine); 11742 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 11743 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 11744 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 11745 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 11746 CHECK_PARSE_BOOL(BinPackArguments); 11747 CHECK_PARSE_BOOL(BinPackParameters); 11748 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 11749 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 11750 CHECK_PARSE_BOOL(BreakStringLiterals); 11751 CHECK_PARSE_BOOL(CompactNamespaces); 11752 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 11753 CHECK_PARSE_BOOL(DerivePointerAlignment); 11754 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 11755 CHECK_PARSE_BOOL(DisableFormat); 11756 CHECK_PARSE_BOOL(IndentCaseLabels); 11757 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 11758 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 11759 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 11760 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 11761 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 11762 CHECK_PARSE_BOOL(ReflowComments); 11763 CHECK_PARSE_BOOL(SortIncludes); 11764 CHECK_PARSE_BOOL(SortUsingDeclarations); 11765 CHECK_PARSE_BOOL(SpacesInParentheses); 11766 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 11767 CHECK_PARSE_BOOL(SpacesInAngles); 11768 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 11769 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 11770 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 11771 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 11772 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 11773 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 11774 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 11775 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 11776 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 11777 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 11778 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 11779 11780 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 11781 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 11782 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 11783 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 11784 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 11785 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 11786 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 11787 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 11788 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 11789 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 11790 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 11791 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 11792 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 11793 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 11794 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 11795 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 11796 } 11797 11798 #undef CHECK_PARSE_BOOL 11799 11800 TEST_F(FormatTest, ParsesConfiguration) { 11801 FormatStyle Style = {}; 11802 Style.Language = FormatStyle::LK_Cpp; 11803 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 11804 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 11805 ConstructorInitializerIndentWidth, 1234u); 11806 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 11807 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 11808 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 11809 CHECK_PARSE("PenaltyBreakAssignment: 1234", 11810 PenaltyBreakAssignment, 1234u); 11811 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 11812 PenaltyBreakBeforeFirstCallParameter, 1234u); 11813 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 11814 PenaltyBreakTemplateDeclaration, 1234u); 11815 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 11816 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 11817 PenaltyReturnTypeOnItsOwnLine, 1234u); 11818 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 11819 SpacesBeforeTrailingComments, 1234u); 11820 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 11821 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 11822 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 11823 11824 Style.PointerAlignment = FormatStyle::PAS_Middle; 11825 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 11826 FormatStyle::PAS_Left); 11827 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 11828 FormatStyle::PAS_Right); 11829 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 11830 FormatStyle::PAS_Middle); 11831 // For backward compatibility: 11832 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 11833 FormatStyle::PAS_Left); 11834 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 11835 FormatStyle::PAS_Right); 11836 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 11837 FormatStyle::PAS_Middle); 11838 11839 Style.Standard = FormatStyle::LS_Auto; 11840 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 11841 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 11842 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 11843 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 11844 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 11845 11846 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 11847 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 11848 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 11849 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 11850 FormatStyle::BOS_None); 11851 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 11852 FormatStyle::BOS_All); 11853 // For backward compatibility: 11854 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 11855 FormatStyle::BOS_None); 11856 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 11857 FormatStyle::BOS_All); 11858 11859 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 11860 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 11861 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 11862 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 11863 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 11864 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 11865 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 11866 // For backward compatibility: 11867 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 11868 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 11869 11870 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 11871 CHECK_PARSE("BreakInheritanceList: BeforeComma", 11872 BreakInheritanceList, FormatStyle::BILS_BeforeComma); 11873 CHECK_PARSE("BreakInheritanceList: AfterColon", 11874 BreakInheritanceList, FormatStyle::BILS_AfterColon); 11875 CHECK_PARSE("BreakInheritanceList: BeforeColon", 11876 BreakInheritanceList, FormatStyle::BILS_BeforeColon); 11877 // For backward compatibility: 11878 CHECK_PARSE("BreakBeforeInheritanceComma: true", 11879 BreakInheritanceList, FormatStyle::BILS_BeforeComma); 11880 11881 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11882 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 11883 FormatStyle::BAS_Align); 11884 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 11885 FormatStyle::BAS_DontAlign); 11886 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 11887 FormatStyle::BAS_AlwaysBreak); 11888 // For backward compatibility: 11889 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 11890 FormatStyle::BAS_DontAlign); 11891 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 11892 FormatStyle::BAS_Align); 11893 11894 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 11895 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 11896 FormatStyle::ENAS_DontAlign); 11897 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 11898 FormatStyle::ENAS_Left); 11899 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 11900 FormatStyle::ENAS_Right); 11901 // For backward compatibility: 11902 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 11903 FormatStyle::ENAS_Left); 11904 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 11905 FormatStyle::ENAS_Right); 11906 11907 Style.UseTab = FormatStyle::UT_ForIndentation; 11908 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 11909 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 11910 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 11911 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 11912 FormatStyle::UT_ForContinuationAndIndentation); 11913 // For backward compatibility: 11914 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 11915 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 11916 11917 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11918 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 11919 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 11920 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 11921 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 11922 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 11923 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 11924 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 11925 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 11926 // For backward compatibility: 11927 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 11928 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 11929 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 11930 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 11931 11932 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 11933 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 11934 FormatStyle::SBPO_Never); 11935 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 11936 FormatStyle::SBPO_Always); 11937 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 11938 FormatStyle::SBPO_ControlStatements); 11939 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 11940 FormatStyle::SBPO_NonEmptyParentheses); 11941 // For backward compatibility: 11942 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 11943 FormatStyle::SBPO_Never); 11944 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 11945 FormatStyle::SBPO_ControlStatements); 11946 11947 Style.ColumnLimit = 123; 11948 FormatStyle BaseStyle = getLLVMStyle(); 11949 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 11950 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 11951 11952 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 11953 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 11954 FormatStyle::BS_Attach); 11955 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 11956 FormatStyle::BS_Linux); 11957 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 11958 FormatStyle::BS_Mozilla); 11959 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 11960 FormatStyle::BS_Stroustrup); 11961 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 11962 FormatStyle::BS_Allman); 11963 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 11964 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 11965 FormatStyle::BS_WebKit); 11966 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 11967 FormatStyle::BS_Custom); 11968 11969 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 11970 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 11971 FormatStyle::RTBS_None); 11972 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 11973 FormatStyle::RTBS_All); 11974 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 11975 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 11976 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 11977 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 11978 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 11979 AlwaysBreakAfterReturnType, 11980 FormatStyle::RTBS_TopLevelDefinitions); 11981 11982 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 11983 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", AlwaysBreakTemplateDeclarations, 11984 FormatStyle::BTDS_No); 11985 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", AlwaysBreakTemplateDeclarations, 11986 FormatStyle::BTDS_MultiLine); 11987 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", AlwaysBreakTemplateDeclarations, 11988 FormatStyle::BTDS_Yes); 11989 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", AlwaysBreakTemplateDeclarations, 11990 FormatStyle::BTDS_MultiLine); 11991 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", AlwaysBreakTemplateDeclarations, 11992 FormatStyle::BTDS_Yes); 11993 11994 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 11995 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 11996 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 11997 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 11998 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 11999 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 12000 AlwaysBreakAfterDefinitionReturnType, 12001 FormatStyle::DRTBS_TopLevel); 12002 12003 Style.NamespaceIndentation = FormatStyle::NI_All; 12004 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 12005 FormatStyle::NI_None); 12006 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 12007 FormatStyle::NI_Inner); 12008 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 12009 FormatStyle::NI_All); 12010 12011 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Always; 12012 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 12013 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 12014 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 12015 AllowShortIfStatementsOnASingleLine, 12016 FormatStyle::SIS_WithoutElse); 12017 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 12018 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Always); 12019 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 12020 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 12021 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 12022 AllowShortIfStatementsOnASingleLine, 12023 FormatStyle::SIS_WithoutElse); 12024 12025 // FIXME: This is required because parsing a configuration simply overwrites 12026 // the first N elements of the list instead of resetting it. 12027 Style.ForEachMacros.clear(); 12028 std::vector<std::string> BoostForeach; 12029 BoostForeach.push_back("BOOST_FOREACH"); 12030 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 12031 std::vector<std::string> BoostAndQForeach; 12032 BoostAndQForeach.push_back("BOOST_FOREACH"); 12033 BoostAndQForeach.push_back("Q_FOREACH"); 12034 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 12035 BoostAndQForeach); 12036 12037 Style.StatementMacros.clear(); 12038 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 12039 std::vector<std::string>{"QUNUSED"}); 12040 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 12041 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 12042 12043 Style.NamespaceMacros.clear(); 12044 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 12045 std::vector<std::string>{"TESTSUITE"}); 12046 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 12047 std::vector<std::string>({"TESTSUITE", "SUITE"})); 12048 12049 Style.IncludeStyle.IncludeCategories.clear(); 12050 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 12051 {"abc/.*", 2}, {".*", 1}}; 12052 CHECK_PARSE("IncludeCategories:\n" 12053 " - Regex: abc/.*\n" 12054 " Priority: 2\n" 12055 " - Regex: .*\n" 12056 " Priority: 1", 12057 IncludeStyle.IncludeCategories, ExpectedCategories); 12058 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 12059 "abc$"); 12060 12061 Style.RawStringFormats.clear(); 12062 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 12063 { 12064 FormatStyle::LK_TextProto, 12065 {"pb", "proto"}, 12066 {"PARSE_TEXT_PROTO"}, 12067 /*CanonicalDelimiter=*/"", 12068 "llvm", 12069 }, 12070 { 12071 FormatStyle::LK_Cpp, 12072 {"cc", "cpp"}, 12073 {"C_CODEBLOCK", "CPPEVAL"}, 12074 /*CanonicalDelimiter=*/"cc", 12075 /*BasedOnStyle=*/"", 12076 }, 12077 }; 12078 12079 CHECK_PARSE("RawStringFormats:\n" 12080 " - Language: TextProto\n" 12081 " Delimiters:\n" 12082 " - 'pb'\n" 12083 " - 'proto'\n" 12084 " EnclosingFunctions:\n" 12085 " - 'PARSE_TEXT_PROTO'\n" 12086 " BasedOnStyle: llvm\n" 12087 " - Language: Cpp\n" 12088 " Delimiters:\n" 12089 " - 'cc'\n" 12090 " - 'cpp'\n" 12091 " EnclosingFunctions:\n" 12092 " - 'C_CODEBLOCK'\n" 12093 " - 'CPPEVAL'\n" 12094 " CanonicalDelimiter: 'cc'", 12095 RawStringFormats, ExpectedRawStringFormats); 12096 } 12097 12098 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 12099 FormatStyle Style = {}; 12100 Style.Language = FormatStyle::LK_Cpp; 12101 CHECK_PARSE("Language: Cpp\n" 12102 "IndentWidth: 12", 12103 IndentWidth, 12u); 12104 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 12105 "IndentWidth: 34", 12106 &Style), 12107 ParseError::Unsuitable); 12108 EXPECT_EQ(12u, Style.IndentWidth); 12109 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 12110 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 12111 12112 Style.Language = FormatStyle::LK_JavaScript; 12113 CHECK_PARSE("Language: JavaScript\n" 12114 "IndentWidth: 12", 12115 IndentWidth, 12u); 12116 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 12117 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 12118 "IndentWidth: 34", 12119 &Style), 12120 ParseError::Unsuitable); 12121 EXPECT_EQ(23u, Style.IndentWidth); 12122 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 12123 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 12124 12125 CHECK_PARSE("BasedOnStyle: LLVM\n" 12126 "IndentWidth: 67", 12127 IndentWidth, 67u); 12128 12129 CHECK_PARSE("---\n" 12130 "Language: JavaScript\n" 12131 "IndentWidth: 12\n" 12132 "---\n" 12133 "Language: Cpp\n" 12134 "IndentWidth: 34\n" 12135 "...\n", 12136 IndentWidth, 12u); 12137 12138 Style.Language = FormatStyle::LK_Cpp; 12139 CHECK_PARSE("---\n" 12140 "Language: JavaScript\n" 12141 "IndentWidth: 12\n" 12142 "---\n" 12143 "Language: Cpp\n" 12144 "IndentWidth: 34\n" 12145 "...\n", 12146 IndentWidth, 34u); 12147 CHECK_PARSE("---\n" 12148 "IndentWidth: 78\n" 12149 "---\n" 12150 "Language: JavaScript\n" 12151 "IndentWidth: 56\n" 12152 "...\n", 12153 IndentWidth, 78u); 12154 12155 Style.ColumnLimit = 123; 12156 Style.IndentWidth = 234; 12157 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 12158 Style.TabWidth = 345; 12159 EXPECT_FALSE(parseConfiguration("---\n" 12160 "IndentWidth: 456\n" 12161 "BreakBeforeBraces: Allman\n" 12162 "---\n" 12163 "Language: JavaScript\n" 12164 "IndentWidth: 111\n" 12165 "TabWidth: 111\n" 12166 "---\n" 12167 "Language: Cpp\n" 12168 "BreakBeforeBraces: Stroustrup\n" 12169 "TabWidth: 789\n" 12170 "...\n", 12171 &Style)); 12172 EXPECT_EQ(123u, Style.ColumnLimit); 12173 EXPECT_EQ(456u, Style.IndentWidth); 12174 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 12175 EXPECT_EQ(789u, Style.TabWidth); 12176 12177 EXPECT_EQ(parseConfiguration("---\n" 12178 "Language: JavaScript\n" 12179 "IndentWidth: 56\n" 12180 "---\n" 12181 "IndentWidth: 78\n" 12182 "...\n", 12183 &Style), 12184 ParseError::Error); 12185 EXPECT_EQ(parseConfiguration("---\n" 12186 "Language: JavaScript\n" 12187 "IndentWidth: 56\n" 12188 "---\n" 12189 "Language: JavaScript\n" 12190 "IndentWidth: 78\n" 12191 "...\n", 12192 &Style), 12193 ParseError::Error); 12194 12195 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 12196 } 12197 12198 #undef CHECK_PARSE 12199 12200 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 12201 FormatStyle Style = {}; 12202 Style.Language = FormatStyle::LK_JavaScript; 12203 Style.BreakBeforeTernaryOperators = true; 12204 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 12205 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 12206 12207 Style.BreakBeforeTernaryOperators = true; 12208 EXPECT_EQ(0, parseConfiguration("---\n" 12209 "BasedOnStyle: Google\n" 12210 "---\n" 12211 "Language: JavaScript\n" 12212 "IndentWidth: 76\n" 12213 "...\n", 12214 &Style) 12215 .value()); 12216 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 12217 EXPECT_EQ(76u, Style.IndentWidth); 12218 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 12219 } 12220 12221 TEST_F(FormatTest, ConfigurationRoundTripTest) { 12222 FormatStyle Style = getLLVMStyle(); 12223 std::string YAML = configurationAsText(Style); 12224 FormatStyle ParsedStyle = {}; 12225 ParsedStyle.Language = FormatStyle::LK_Cpp; 12226 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 12227 EXPECT_EQ(Style, ParsedStyle); 12228 } 12229 12230 TEST_F(FormatTest, WorksFor8bitEncodings) { 12231 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 12232 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 12233 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 12234 "\"\xef\xee\xf0\xf3...\"", 12235 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 12236 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 12237 "\xef\xee\xf0\xf3...\"", 12238 getLLVMStyleWithColumns(12))); 12239 } 12240 12241 TEST_F(FormatTest, HandlesUTF8BOM) { 12242 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 12243 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 12244 format("\xef\xbb\xbf#include <iostream>")); 12245 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 12246 format("\xef\xbb\xbf\n#include <iostream>")); 12247 } 12248 12249 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 12250 #if !defined(_MSC_VER) 12251 12252 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 12253 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 12254 getLLVMStyleWithColumns(35)); 12255 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 12256 getLLVMStyleWithColumns(31)); 12257 verifyFormat("// Однажды в студёную зимнюю пору...", 12258 getLLVMStyleWithColumns(36)); 12259 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 12260 verifyFormat("/* Однажды в студёную зимнюю пору... */", 12261 getLLVMStyleWithColumns(39)); 12262 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 12263 getLLVMStyleWithColumns(35)); 12264 } 12265 12266 TEST_F(FormatTest, SplitsUTF8Strings) { 12267 // Non-printable characters' width is currently considered to be the length in 12268 // bytes in UTF8. The characters can be displayed in very different manner 12269 // (zero-width, single width with a substitution glyph, expanded to their code 12270 // (e.g. "<8d>"), so there's no single correct way to handle them. 12271 EXPECT_EQ("\"aaaaÄ\"\n" 12272 "\"\xc2\x8d\";", 12273 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 12274 EXPECT_EQ("\"aaaaaaaÄ\"\n" 12275 "\"\xc2\x8d\";", 12276 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 12277 EXPECT_EQ("\"Однажды, в \"\n" 12278 "\"студёную \"\n" 12279 "\"зимнюю \"\n" 12280 "\"пору,\"", 12281 format("\"Однажды, в студёную зимнюю пору,\"", 12282 getLLVMStyleWithColumns(13))); 12283 EXPECT_EQ( 12284 "\"一 二 三 \"\n" 12285 "\"四 五六 \"\n" 12286 "\"七 八 九 \"\n" 12287 "\"十\"", 12288 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 12289 EXPECT_EQ("\"一\t\"\n" 12290 "\"二 \t\"\n" 12291 "\"三 四 \"\n" 12292 "\"五\t\"\n" 12293 "\"六 \t\"\n" 12294 "\"七 \"\n" 12295 "\"八九十\tqq\"", 12296 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 12297 getLLVMStyleWithColumns(11))); 12298 12299 // UTF8 character in an escape sequence. 12300 EXPECT_EQ("\"aaaaaa\"\n" 12301 "\"\\\xC2\x8D\"", 12302 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 12303 } 12304 12305 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 12306 EXPECT_EQ("const char *sssss =\n" 12307 " \"一二三四五六七八\\\n" 12308 " 九 十\";", 12309 format("const char *sssss = \"一二三四五六七八\\\n" 12310 " 九 十\";", 12311 getLLVMStyleWithColumns(30))); 12312 } 12313 12314 TEST_F(FormatTest, SplitsUTF8LineComments) { 12315 EXPECT_EQ("// aaaaÄ\xc2\x8d", 12316 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 12317 EXPECT_EQ("// Я из лесу\n" 12318 "// вышел; был\n" 12319 "// сильный\n" 12320 "// мороз.", 12321 format("// Я из лесу вышел; был сильный мороз.", 12322 getLLVMStyleWithColumns(13))); 12323 EXPECT_EQ("// 一二三\n" 12324 "// 四五六七\n" 12325 "// 八 九\n" 12326 "// 十", 12327 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 12328 } 12329 12330 TEST_F(FormatTest, SplitsUTF8BlockComments) { 12331 EXPECT_EQ("/* Гляжу,\n" 12332 " * поднимается\n" 12333 " * медленно в\n" 12334 " * гору\n" 12335 " * Лошадка,\n" 12336 " * везущая\n" 12337 " * хворосту\n" 12338 " * воз. */", 12339 format("/* Гляжу, поднимается медленно в гору\n" 12340 " * Лошадка, везущая хворосту воз. */", 12341 getLLVMStyleWithColumns(13))); 12342 EXPECT_EQ( 12343 "/* 一二三\n" 12344 " * 四五六七\n" 12345 " * 八 九\n" 12346 " * 十 */", 12347 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 12348 EXPECT_EQ("/* \n" 12349 " * \n" 12350 " * - */", 12351 format("/* - */", getLLVMStyleWithColumns(12))); 12352 } 12353 12354 #endif // _MSC_VER 12355 12356 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 12357 FormatStyle Style = getLLVMStyle(); 12358 12359 Style.ConstructorInitializerIndentWidth = 4; 12360 verifyFormat( 12361 "SomeClass::Constructor()\n" 12362 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 12363 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 12364 Style); 12365 12366 Style.ConstructorInitializerIndentWidth = 2; 12367 verifyFormat( 12368 "SomeClass::Constructor()\n" 12369 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 12370 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 12371 Style); 12372 12373 Style.ConstructorInitializerIndentWidth = 0; 12374 verifyFormat( 12375 "SomeClass::Constructor()\n" 12376 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 12377 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 12378 Style); 12379 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12380 verifyFormat( 12381 "SomeLongTemplateVariableName<\n" 12382 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 12383 Style); 12384 verifyFormat( 12385 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 12386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 12387 Style); 12388 12389 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 12390 verifyFormat( 12391 "SomeClass::Constructor() :\n" 12392 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 12393 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 12394 Style); 12395 } 12396 12397 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 12398 FormatStyle Style = getLLVMStyle(); 12399 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 12400 Style.ConstructorInitializerIndentWidth = 4; 12401 verifyFormat("SomeClass::Constructor()\n" 12402 " : a(a)\n" 12403 " , b(b)\n" 12404 " , c(c) {}", 12405 Style); 12406 verifyFormat("SomeClass::Constructor()\n" 12407 " : a(a) {}", 12408 Style); 12409 12410 Style.ColumnLimit = 0; 12411 verifyFormat("SomeClass::Constructor()\n" 12412 " : a(a) {}", 12413 Style); 12414 verifyFormat("SomeClass::Constructor() noexcept\n" 12415 " : a(a) {}", 12416 Style); 12417 verifyFormat("SomeClass::Constructor()\n" 12418 " : a(a)\n" 12419 " , b(b)\n" 12420 " , c(c) {}", 12421 Style); 12422 verifyFormat("SomeClass::Constructor()\n" 12423 " : a(a) {\n" 12424 " foo();\n" 12425 " bar();\n" 12426 "}", 12427 Style); 12428 12429 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12430 verifyFormat("SomeClass::Constructor()\n" 12431 " : a(a)\n" 12432 " , b(b)\n" 12433 " , c(c) {\n}", 12434 Style); 12435 verifyFormat("SomeClass::Constructor()\n" 12436 " : a(a) {\n}", 12437 Style); 12438 12439 Style.ColumnLimit = 80; 12440 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12441 Style.ConstructorInitializerIndentWidth = 2; 12442 verifyFormat("SomeClass::Constructor()\n" 12443 " : a(a)\n" 12444 " , b(b)\n" 12445 " , c(c) {}", 12446 Style); 12447 12448 Style.ConstructorInitializerIndentWidth = 0; 12449 verifyFormat("SomeClass::Constructor()\n" 12450 ": a(a)\n" 12451 ", b(b)\n" 12452 ", c(c) {}", 12453 Style); 12454 12455 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 12456 Style.ConstructorInitializerIndentWidth = 4; 12457 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 12458 verifyFormat( 12459 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 12460 Style); 12461 verifyFormat( 12462 "SomeClass::Constructor()\n" 12463 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 12464 Style); 12465 Style.ConstructorInitializerIndentWidth = 4; 12466 Style.ColumnLimit = 60; 12467 verifyFormat("SomeClass::Constructor()\n" 12468 " : aaaaaaaa(aaaaaaaa)\n" 12469 " , aaaaaaaa(aaaaaaaa)\n" 12470 " , aaaaaaaa(aaaaaaaa) {}", 12471 Style); 12472 } 12473 12474 TEST_F(FormatTest, Destructors) { 12475 verifyFormat("void F(int &i) { i.~int(); }"); 12476 verifyFormat("void F(int &i) { i->~int(); }"); 12477 } 12478 12479 TEST_F(FormatTest, FormatsWithWebKitStyle) { 12480 FormatStyle Style = getWebKitStyle(); 12481 12482 // Don't indent in outer namespaces. 12483 verifyFormat("namespace outer {\n" 12484 "int i;\n" 12485 "namespace inner {\n" 12486 " int i;\n" 12487 "} // namespace inner\n" 12488 "} // namespace outer\n" 12489 "namespace other_outer {\n" 12490 "int i;\n" 12491 "}", 12492 Style); 12493 12494 // Don't indent case labels. 12495 verifyFormat("switch (variable) {\n" 12496 "case 1:\n" 12497 "case 2:\n" 12498 " doSomething();\n" 12499 " break;\n" 12500 "default:\n" 12501 " ++variable;\n" 12502 "}", 12503 Style); 12504 12505 // Wrap before binary operators. 12506 EXPECT_EQ("void f()\n" 12507 "{\n" 12508 " if (aaaaaaaaaaaaaaaa\n" 12509 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 12510 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 12511 " return;\n" 12512 "}", 12513 format("void f() {\n" 12514 "if (aaaaaaaaaaaaaaaa\n" 12515 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 12516 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 12517 "return;\n" 12518 "}", 12519 Style)); 12520 12521 // Allow functions on a single line. 12522 verifyFormat("void f() { return; }", Style); 12523 12524 // Constructor initializers are formatted one per line with the "," on the 12525 // new line. 12526 verifyFormat("Constructor()\n" 12527 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 12528 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 12529 " aaaaaaaaaaaaaa)\n" 12530 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 12531 "{\n" 12532 "}", 12533 Style); 12534 verifyFormat("SomeClass::Constructor()\n" 12535 " : a(a)\n" 12536 "{\n" 12537 "}", 12538 Style); 12539 EXPECT_EQ("SomeClass::Constructor()\n" 12540 " : a(a)\n" 12541 "{\n" 12542 "}", 12543 format("SomeClass::Constructor():a(a){}", Style)); 12544 verifyFormat("SomeClass::Constructor()\n" 12545 " : a(a)\n" 12546 " , b(b)\n" 12547 " , c(c)\n" 12548 "{\n" 12549 "}", 12550 Style); 12551 verifyFormat("SomeClass::Constructor()\n" 12552 " : a(a)\n" 12553 "{\n" 12554 " foo();\n" 12555 " bar();\n" 12556 "}", 12557 Style); 12558 12559 // Access specifiers should be aligned left. 12560 verifyFormat("class C {\n" 12561 "public:\n" 12562 " int i;\n" 12563 "};", 12564 Style); 12565 12566 // Do not align comments. 12567 verifyFormat("int a; // Do not\n" 12568 "double b; // align comments.", 12569 Style); 12570 12571 // Do not align operands. 12572 EXPECT_EQ("ASSERT(aaaa\n" 12573 " || bbbb);", 12574 format("ASSERT ( aaaa\n||bbbb);", Style)); 12575 12576 // Accept input's line breaks. 12577 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 12578 " || bbbbbbbbbbbbbbb) {\n" 12579 " i++;\n" 12580 "}", 12581 format("if (aaaaaaaaaaaaaaa\n" 12582 "|| bbbbbbbbbbbbbbb) { i++; }", 12583 Style)); 12584 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 12585 " i++;\n" 12586 "}", 12587 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 12588 12589 // Don't automatically break all macro definitions (llvm.org/PR17842). 12590 verifyFormat("#define aNumber 10", Style); 12591 // However, generally keep the line breaks that the user authored. 12592 EXPECT_EQ("#define aNumber \\\n" 12593 " 10", 12594 format("#define aNumber \\\n" 12595 " 10", 12596 Style)); 12597 12598 // Keep empty and one-element array literals on a single line. 12599 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 12600 " copyItems:YES];", 12601 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 12602 "copyItems:YES];", 12603 Style)); 12604 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 12605 " copyItems:YES];", 12606 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 12607 " copyItems:YES];", 12608 Style)); 12609 // FIXME: This does not seem right, there should be more indentation before 12610 // the array literal's entries. Nested blocks have the same problem. 12611 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 12612 " @\"a\",\n" 12613 " @\"a\"\n" 12614 "]\n" 12615 " copyItems:YES];", 12616 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 12617 " @\"a\",\n" 12618 " @\"a\"\n" 12619 " ]\n" 12620 " copyItems:YES];", 12621 Style)); 12622 EXPECT_EQ( 12623 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 12624 " copyItems:YES];", 12625 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 12626 " copyItems:YES];", 12627 Style)); 12628 12629 verifyFormat("[self.a b:c c:d];", Style); 12630 EXPECT_EQ("[self.a b:c\n" 12631 " c:d];", 12632 format("[self.a b:c\n" 12633 "c:d];", 12634 Style)); 12635 } 12636 12637 TEST_F(FormatTest, FormatsLambdas) { 12638 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 12639 verifyFormat( 12640 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 12641 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 12642 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 12643 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 12644 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 12645 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 12646 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 12647 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 12648 verifyFormat("int x = f(*+[] {});"); 12649 verifyFormat("void f() {\n" 12650 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 12651 "}\n"); 12652 verifyFormat("void f() {\n" 12653 " other(x.begin(), //\n" 12654 " x.end(), //\n" 12655 " [&](int, int) { return 1; });\n" 12656 "}\n"); 12657 verifyFormat("void f() {\n" 12658 " other.other.other.other.other(\n" 12659 " x.begin(), x.end(),\n" 12660 " [something, rather](int, int, int, int, int, int, int) { return 1; });\n" 12661 "}\n"); 12662 verifyFormat("void f() {\n" 12663 " other.other.other.other.other(\n" 12664 " x.begin(), x.end(),\n" 12665 " [something, rather](int, int, int, int, int, int, int) {\n" 12666 " //\n" 12667 " });\n" 12668 "}\n"); 12669 verifyFormat("SomeFunction([]() { // A cool function...\n" 12670 " return 43;\n" 12671 "});"); 12672 EXPECT_EQ("SomeFunction([]() {\n" 12673 "#define A a\n" 12674 " return 43;\n" 12675 "});", 12676 format("SomeFunction([](){\n" 12677 "#define A a\n" 12678 "return 43;\n" 12679 "});")); 12680 verifyFormat("void f() {\n" 12681 " SomeFunction([](decltype(x), A *a) {});\n" 12682 "}"); 12683 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 12684 " [](const aaaaaaaaaa &a) { return a; });"); 12685 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 12686 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 12687 "});"); 12688 verifyFormat("Constructor()\n" 12689 " : Field([] { // comment\n" 12690 " int i;\n" 12691 " }) {}"); 12692 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 12693 " return some_parameter.size();\n" 12694 "};"); 12695 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 12696 " [](const string &s) { return s; };"); 12697 verifyFormat("int i = aaaaaa ? 1 //\n" 12698 " : [] {\n" 12699 " return 2; //\n" 12700 " }();"); 12701 verifyFormat("llvm::errs() << \"number of twos is \"\n" 12702 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 12703 " return x == 2; // force break\n" 12704 " });"); 12705 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 12706 " [=](int iiiiiiiiiiii) {\n" 12707 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 12708 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 12709 " });", 12710 getLLVMStyleWithColumns(60)); 12711 verifyFormat("SomeFunction({[&] {\n" 12712 " // comment\n" 12713 " },\n" 12714 " [&] {\n" 12715 " // comment\n" 12716 " }});"); 12717 verifyFormat("SomeFunction({[&] {\n" 12718 " // comment\n" 12719 "}});"); 12720 verifyFormat("virtual aaaaaaaaaaaaaaaa(\n" 12721 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 12722 " aaaaa aaaaaaaaa);"); 12723 12724 // Lambdas with return types. 12725 verifyFormat("int c = []() -> int { return 2; }();\n"); 12726 verifyFormat("int c = []() -> int * { return 2; }();\n"); 12727 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 12728 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 12729 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 12730 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 12731 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 12732 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 12733 verifyFormat("[a, a]() -> a<1> {};"); 12734 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 12735 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 12736 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 12737 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 12738 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 12739 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 12740 verifyFormat("[]() -> foo<!5> { return {}; };"); 12741 verifyFormat("[]() -> foo<~5> { return {}; };"); 12742 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 12743 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 12744 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 12745 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 12746 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 12747 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 12748 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 12749 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 12750 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 12751 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 12752 verifyFormat("namespace bar {\n" 12753 "// broken:\n" 12754 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 12755 "} // namespace bar"); 12756 verifyFormat("namespace bar {\n" 12757 "// broken:\n" 12758 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 12759 "} // namespace bar"); 12760 verifyFormat("namespace bar {\n" 12761 "// broken:\n" 12762 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 12763 "} // namespace bar"); 12764 verifyFormat("namespace bar {\n" 12765 "// broken:\n" 12766 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 12767 "} // namespace bar"); 12768 verifyFormat("namespace bar {\n" 12769 "// broken:\n" 12770 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 12771 "} // namespace bar"); 12772 verifyFormat("namespace bar {\n" 12773 "// broken:\n" 12774 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 12775 "} // namespace bar"); 12776 verifyFormat("namespace bar {\n" 12777 "// broken:\n" 12778 "auto foo{[]() -> foo<!5> { return {}; }};\n" 12779 "} // namespace bar"); 12780 verifyFormat("namespace bar {\n" 12781 "// broken:\n" 12782 "auto foo{[]() -> foo<~5> { return {}; }};\n" 12783 "} // namespace bar"); 12784 verifyFormat("namespace bar {\n" 12785 "// broken:\n" 12786 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 12787 "} // namespace bar"); 12788 verifyFormat("namespace bar {\n" 12789 "// broken:\n" 12790 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 12791 "} // namespace bar"); 12792 verifyFormat("namespace bar {\n" 12793 "// broken:\n" 12794 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 12795 "} // namespace bar"); 12796 verifyFormat("namespace bar {\n" 12797 "// broken:\n" 12798 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 12799 "} // namespace bar"); 12800 verifyFormat("namespace bar {\n" 12801 "// broken:\n" 12802 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 12803 "} // namespace bar"); 12804 verifyFormat("namespace bar {\n" 12805 "// broken:\n" 12806 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 12807 "} // namespace bar"); 12808 verifyFormat("namespace bar {\n" 12809 "// broken:\n" 12810 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 12811 "} // namespace bar"); 12812 verifyFormat("namespace bar {\n" 12813 "// broken:\n" 12814 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 12815 "} // namespace bar"); 12816 verifyFormat("namespace bar {\n" 12817 "// broken:\n" 12818 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 12819 "} // namespace bar"); 12820 verifyFormat("namespace bar {\n" 12821 "// broken:\n" 12822 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 12823 "} // namespace bar"); 12824 verifyFormat("[]() -> a<1> {};"); 12825 verifyFormat("[]() -> a<1> { ; };"); 12826 verifyFormat("[]() -> a<1> { ; }();"); 12827 verifyFormat("[a, a]() -> a<true> {};"); 12828 verifyFormat("[]() -> a<true> {};"); 12829 verifyFormat("[]() -> a<true> { ; };"); 12830 verifyFormat("[]() -> a<true> { ; }();"); 12831 verifyFormat("[a, a]() -> a<false> {};"); 12832 verifyFormat("[]() -> a<false> {};"); 12833 verifyFormat("[]() -> a<false> { ; };"); 12834 verifyFormat("[]() -> a<false> { ; }();"); 12835 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 12836 verifyFormat("namespace bar {\n" 12837 "auto foo{[]() -> foo<false> { ; }};\n" 12838 "} // namespace bar"); 12839 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 12840 " int j) -> int {\n" 12841 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 12842 "};"); 12843 verifyFormat( 12844 "aaaaaaaaaaaaaaaaaaaaaa(\n" 12845 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 12846 " return aaaaaaaaaaaaaaaaa;\n" 12847 " });", 12848 getLLVMStyleWithColumns(70)); 12849 verifyFormat("[]() //\n" 12850 " -> int {\n" 12851 " return 1; //\n" 12852 "};"); 12853 12854 // Multiple lambdas in the same parentheses change indentation rules. These 12855 // lambdas are forced to start on new lines. 12856 verifyFormat("SomeFunction(\n" 12857 " []() {\n" 12858 " //\n" 12859 " },\n" 12860 " []() {\n" 12861 " //\n" 12862 " });"); 12863 12864 // A lambda passed as arg0 is always pushed to the next line. 12865 verifyFormat("SomeFunction(\n" 12866 " [this] {\n" 12867 " //\n" 12868 " },\n" 12869 " 1);\n"); 12870 12871 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like the arg0 12872 // case above. 12873 auto Style = getGoogleStyle(); 12874 Style.BinPackArguments = false; 12875 verifyFormat("SomeFunction(\n" 12876 " a,\n" 12877 " [this] {\n" 12878 " //\n" 12879 " },\n" 12880 " b);\n", 12881 Style); 12882 verifyFormat("SomeFunction(\n" 12883 " a,\n" 12884 " [this] {\n" 12885 " //\n" 12886 " },\n" 12887 " b);\n"); 12888 12889 // A lambda with a very long line forces arg0 to be pushed out irrespective of 12890 // the BinPackArguments value (as long as the code is wide enough). 12891 verifyFormat("something->SomeFunction(\n" 12892 " a,\n" 12893 " [this] {\n" 12894 " D0000000000000000000000000000000000000000000000000000000000001();\n" 12895 " },\n" 12896 " b);\n"); 12897 12898 // A multi-line lambda is pulled up as long as the introducer fits on the previous 12899 // line and there are no further args. 12900 verifyFormat("function(1, [this, that] {\n" 12901 " //\n" 12902 "});\n"); 12903 verifyFormat("function([this, that] {\n" 12904 " //\n" 12905 "});\n"); 12906 // FIXME: this format is not ideal and we should consider forcing the first arg 12907 // onto its own line. 12908 verifyFormat("function(a, b, c, //\n" 12909 " d, [this, that] {\n" 12910 " //\n" 12911 " });\n"); 12912 12913 // Multiple lambdas are treated correctly even when there is a short arg0. 12914 verifyFormat("SomeFunction(\n" 12915 " 1,\n" 12916 " [this] {\n" 12917 " //\n" 12918 " },\n" 12919 " [this] {\n" 12920 " //\n" 12921 " },\n" 12922 " 1);\n"); 12923 12924 // More complex introducers. 12925 verifyFormat("return [i, args...] {};"); 12926 12927 // Not lambdas. 12928 verifyFormat("constexpr char hello[]{\"hello\"};"); 12929 verifyFormat("double &operator[](int i) { return 0; }\n" 12930 "int i;"); 12931 verifyFormat("std::unique_ptr<int[]> foo() {}"); 12932 verifyFormat("int i = a[a][a]->f();"); 12933 verifyFormat("int i = (*b)[a]->f();"); 12934 12935 // Other corner cases. 12936 verifyFormat("void f() {\n" 12937 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 12938 " );\n" 12939 "}"); 12940 12941 // Lambdas created through weird macros. 12942 verifyFormat("void f() {\n" 12943 " MACRO((const AA &a) { return 1; });\n" 12944 " MACRO((AA &a) { return 1; });\n" 12945 "}"); 12946 12947 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 12948 " doo_dah();\n" 12949 " doo_dah();\n" 12950 " })) {\n" 12951 "}"); 12952 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 12953 " doo_dah();\n" 12954 " doo_dah();\n" 12955 " })) {\n" 12956 "}"); 12957 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 12958 " doo_dah();\n" 12959 " doo_dah();\n" 12960 " })) {\n" 12961 "}"); 12962 verifyFormat("auto lambda = []() {\n" 12963 " int a = 2\n" 12964 "#if A\n" 12965 " + 2\n" 12966 "#endif\n" 12967 " ;\n" 12968 "};"); 12969 12970 // Lambdas with complex multiline introducers. 12971 verifyFormat( 12972 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 12973 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 12974 " -> ::std::unordered_set<\n" 12975 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 12976 " //\n" 12977 " });"); 12978 12979 FormatStyle DoNotMerge = getLLVMStyle(); 12980 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 12981 verifyFormat("auto c = []() {\n" 12982 " return b;\n" 12983 "};", 12984 "auto c = []() { return b; };", DoNotMerge); 12985 verifyFormat("auto c = []() {\n" 12986 "};", 12987 " auto c = []() {};", DoNotMerge); 12988 12989 FormatStyle MergeEmptyOnly = getLLVMStyle(); 12990 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 12991 verifyFormat("auto c = []() {\n" 12992 " return b;\n" 12993 "};", 12994 "auto c = []() {\n" 12995 " return b;\n" 12996 " };", 12997 MergeEmptyOnly); 12998 verifyFormat("auto c = []() {};", 12999 "auto c = []() {\n" 13000 "};", 13001 MergeEmptyOnly); 13002 13003 FormatStyle MergeInline = getLLVMStyle(); 13004 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 13005 verifyFormat("auto c = []() {\n" 13006 " return b;\n" 13007 "};", 13008 "auto c = []() { return b; };", MergeInline); 13009 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 13010 MergeInline); 13011 verifyFormat("function([]() { return b; }, a)", 13012 "function([]() { return b; }, a)", MergeInline); 13013 verifyFormat("function(a, []() { return b; })", 13014 "function(a, []() { return b; })", MergeInline); 13015 } 13016 13017 TEST_F(FormatTest, EmptyLinesInLambdas) { 13018 verifyFormat("auto lambda = []() {\n" 13019 " x(); //\n" 13020 "};", 13021 "auto lambda = []() {\n" 13022 "\n" 13023 " x(); //\n" 13024 "\n" 13025 "};"); 13026 } 13027 13028 TEST_F(FormatTest, FormatsBlocks) { 13029 FormatStyle ShortBlocks = getLLVMStyle(); 13030 ShortBlocks.AllowShortBlocksOnASingleLine = true; 13031 verifyFormat("int (^Block)(int, int);", ShortBlocks); 13032 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 13033 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 13034 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 13035 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 13036 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 13037 13038 verifyFormat("foo(^{ bar(); });", ShortBlocks); 13039 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 13040 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 13041 13042 verifyFormat("[operation setCompletionBlock:^{\n" 13043 " [self onOperationDone];\n" 13044 "}];"); 13045 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 13046 " [self onOperationDone];\n" 13047 "}]};"); 13048 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 13049 " f();\n" 13050 "}];"); 13051 verifyFormat("int a = [operation block:^int(int *i) {\n" 13052 " return 1;\n" 13053 "}];"); 13054 verifyFormat("[myObject doSomethingWith:arg1\n" 13055 " aaa:^int(int *a) {\n" 13056 " return 1;\n" 13057 " }\n" 13058 " bbb:f(a * bbbbbbbb)];"); 13059 13060 verifyFormat("[operation setCompletionBlock:^{\n" 13061 " [self.delegate newDataAvailable];\n" 13062 "}];", 13063 getLLVMStyleWithColumns(60)); 13064 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 13065 " NSString *path = [self sessionFilePath];\n" 13066 " if (path) {\n" 13067 " // ...\n" 13068 " }\n" 13069 "});"); 13070 verifyFormat("[[SessionService sharedService]\n" 13071 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 13072 " if (window) {\n" 13073 " [self windowDidLoad:window];\n" 13074 " } else {\n" 13075 " [self errorLoadingWindow];\n" 13076 " }\n" 13077 " }];"); 13078 verifyFormat("void (^largeBlock)(void) = ^{\n" 13079 " // ...\n" 13080 "};\n", 13081 getLLVMStyleWithColumns(40)); 13082 verifyFormat("[[SessionService sharedService]\n" 13083 " loadWindowWithCompletionBlock: //\n" 13084 " ^(SessionWindow *window) {\n" 13085 " if (window) {\n" 13086 " [self windowDidLoad:window];\n" 13087 " } else {\n" 13088 " [self errorLoadingWindow];\n" 13089 " }\n" 13090 " }];", 13091 getLLVMStyleWithColumns(60)); 13092 verifyFormat("[myObject doSomethingWith:arg1\n" 13093 " firstBlock:^(Foo *a) {\n" 13094 " // ...\n" 13095 " int i;\n" 13096 " }\n" 13097 " secondBlock:^(Bar *b) {\n" 13098 " // ...\n" 13099 " int i;\n" 13100 " }\n" 13101 " thirdBlock:^Foo(Bar *b) {\n" 13102 " // ...\n" 13103 " int i;\n" 13104 " }];"); 13105 verifyFormat("[myObject doSomethingWith:arg1\n" 13106 " firstBlock:-1\n" 13107 " secondBlock:^(Bar *b) {\n" 13108 " // ...\n" 13109 " int i;\n" 13110 " }];"); 13111 13112 verifyFormat("f(^{\n" 13113 " @autoreleasepool {\n" 13114 " if (a) {\n" 13115 " g();\n" 13116 " }\n" 13117 " }\n" 13118 "});"); 13119 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 13120 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 13121 "};"); 13122 13123 FormatStyle FourIndent = getLLVMStyle(); 13124 FourIndent.ObjCBlockIndentWidth = 4; 13125 verifyFormat("[operation setCompletionBlock:^{\n" 13126 " [self onOperationDone];\n" 13127 "}];", 13128 FourIndent); 13129 } 13130 13131 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 13132 FormatStyle ZeroColumn = getLLVMStyle(); 13133 ZeroColumn.ColumnLimit = 0; 13134 13135 verifyFormat("[[SessionService sharedService] " 13136 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 13137 " if (window) {\n" 13138 " [self windowDidLoad:window];\n" 13139 " } else {\n" 13140 " [self errorLoadingWindow];\n" 13141 " }\n" 13142 "}];", 13143 ZeroColumn); 13144 EXPECT_EQ("[[SessionService sharedService]\n" 13145 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 13146 " if (window) {\n" 13147 " [self windowDidLoad:window];\n" 13148 " } else {\n" 13149 " [self errorLoadingWindow];\n" 13150 " }\n" 13151 " }];", 13152 format("[[SessionService sharedService]\n" 13153 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 13154 " if (window) {\n" 13155 " [self windowDidLoad:window];\n" 13156 " } else {\n" 13157 " [self errorLoadingWindow];\n" 13158 " }\n" 13159 "}];", 13160 ZeroColumn)); 13161 verifyFormat("[myObject doSomethingWith:arg1\n" 13162 " firstBlock:^(Foo *a) {\n" 13163 " // ...\n" 13164 " int i;\n" 13165 " }\n" 13166 " secondBlock:^(Bar *b) {\n" 13167 " // ...\n" 13168 " int i;\n" 13169 " }\n" 13170 " thirdBlock:^Foo(Bar *b) {\n" 13171 " // ...\n" 13172 " int i;\n" 13173 " }];", 13174 ZeroColumn); 13175 verifyFormat("f(^{\n" 13176 " @autoreleasepool {\n" 13177 " if (a) {\n" 13178 " g();\n" 13179 " }\n" 13180 " }\n" 13181 "});", 13182 ZeroColumn); 13183 verifyFormat("void (^largeBlock)(void) = ^{\n" 13184 " // ...\n" 13185 "};", 13186 ZeroColumn); 13187 13188 ZeroColumn.AllowShortBlocksOnASingleLine = true; 13189 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 13190 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 13191 ZeroColumn.AllowShortBlocksOnASingleLine = false; 13192 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 13193 " int i;\n" 13194 "};", 13195 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 13196 } 13197 13198 TEST_F(FormatTest, SupportsCRLF) { 13199 EXPECT_EQ("int a;\r\n" 13200 "int b;\r\n" 13201 "int c;\r\n", 13202 format("int a;\r\n" 13203 " int b;\r\n" 13204 " int c;\r\n", 13205 getLLVMStyle())); 13206 EXPECT_EQ("int a;\r\n" 13207 "int b;\r\n" 13208 "int c;\r\n", 13209 format("int a;\r\n" 13210 " int b;\n" 13211 " int c;\r\n", 13212 getLLVMStyle())); 13213 EXPECT_EQ("int a;\n" 13214 "int b;\n" 13215 "int c;\n", 13216 format("int a;\r\n" 13217 " int b;\n" 13218 " int c;\n", 13219 getLLVMStyle())); 13220 EXPECT_EQ("\"aaaaaaa \"\r\n" 13221 "\"bbbbbbb\";\r\n", 13222 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 13223 EXPECT_EQ("#define A \\\r\n" 13224 " b; \\\r\n" 13225 " c; \\\r\n" 13226 " d;\r\n", 13227 format("#define A \\\r\n" 13228 " b; \\\r\n" 13229 " c; d; \r\n", 13230 getGoogleStyle())); 13231 13232 EXPECT_EQ("/*\r\n" 13233 "multi line block comments\r\n" 13234 "should not introduce\r\n" 13235 "an extra carriage return\r\n" 13236 "*/\r\n", 13237 format("/*\r\n" 13238 "multi line block comments\r\n" 13239 "should not introduce\r\n" 13240 "an extra carriage return\r\n" 13241 "*/\r\n")); 13242 EXPECT_EQ("/*\r\n" 13243 "\r\n" 13244 "*/", 13245 format("/*\r\n" 13246 " \r\r\r\n" 13247 "*/")); 13248 } 13249 13250 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 13251 verifyFormat("MY_CLASS(C) {\n" 13252 " int i;\n" 13253 " int j;\n" 13254 "};"); 13255 } 13256 13257 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 13258 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 13259 TwoIndent.ContinuationIndentWidth = 2; 13260 13261 EXPECT_EQ("int i =\n" 13262 " longFunction(\n" 13263 " arg);", 13264 format("int i = longFunction(arg);", TwoIndent)); 13265 13266 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 13267 SixIndent.ContinuationIndentWidth = 6; 13268 13269 EXPECT_EQ("int i =\n" 13270 " longFunction(\n" 13271 " arg);", 13272 format("int i = longFunction(arg);", SixIndent)); 13273 } 13274 13275 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 13276 FormatStyle Style = getLLVMStyle(); 13277 verifyFormat("int Foo::getter(\n" 13278 " //\n" 13279 ") const {\n" 13280 " return foo;\n" 13281 "}", 13282 Style); 13283 verifyFormat("void Foo::setter(\n" 13284 " //\n" 13285 ") {\n" 13286 " foo = 1;\n" 13287 "}", 13288 Style); 13289 } 13290 13291 TEST_F(FormatTest, SpacesInAngles) { 13292 FormatStyle Spaces = getLLVMStyle(); 13293 Spaces.SpacesInAngles = true; 13294 13295 verifyFormat("static_cast< int >(arg);", Spaces); 13296 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 13297 verifyFormat("f< int, float >();", Spaces); 13298 verifyFormat("template <> g() {}", Spaces); 13299 verifyFormat("template < std::vector< int > > f() {}", Spaces); 13300 verifyFormat("std::function< void(int, int) > fct;", Spaces); 13301 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 13302 Spaces); 13303 13304 Spaces.Standard = FormatStyle::LS_Cpp03; 13305 Spaces.SpacesInAngles = true; 13306 verifyFormat("A< A< int > >();", Spaces); 13307 13308 Spaces.SpacesInAngles = false; 13309 verifyFormat("A<A<int> >();", Spaces); 13310 13311 Spaces.Standard = FormatStyle::LS_Cpp11; 13312 Spaces.SpacesInAngles = true; 13313 verifyFormat("A< A< int > >();", Spaces); 13314 13315 Spaces.SpacesInAngles = false; 13316 verifyFormat("A<A<int>>();", Spaces); 13317 } 13318 13319 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 13320 FormatStyle Style = getLLVMStyle(); 13321 Style.SpaceAfterTemplateKeyword = false; 13322 verifyFormat("template<int> void foo();", Style); 13323 } 13324 13325 TEST_F(FormatTest, TripleAngleBrackets) { 13326 verifyFormat("f<<<1, 1>>>();"); 13327 verifyFormat("f<<<1, 1, 1, s>>>();"); 13328 verifyFormat("f<<<a, b, c, d>>>();"); 13329 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 13330 verifyFormat("f<param><<<1, 1>>>();"); 13331 verifyFormat("f<1><<<1, 1>>>();"); 13332 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 13333 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13334 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 13335 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 13336 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 13337 } 13338 13339 TEST_F(FormatTest, MergeLessLessAtEnd) { 13340 verifyFormat("<<"); 13341 EXPECT_EQ("< < <", format("\\\n<<<")); 13342 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13343 "aaallvm::outs() <<"); 13344 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13345 "aaaallvm::outs()\n <<"); 13346 } 13347 13348 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 13349 std::string code = "#if A\n" 13350 "#if B\n" 13351 "a.\n" 13352 "#endif\n" 13353 " a = 1;\n" 13354 "#else\n" 13355 "#endif\n" 13356 "#if C\n" 13357 "#else\n" 13358 "#endif\n"; 13359 EXPECT_EQ(code, format(code)); 13360 } 13361 13362 TEST_F(FormatTest, HandleConflictMarkers) { 13363 // Git/SVN conflict markers. 13364 EXPECT_EQ("int a;\n" 13365 "void f() {\n" 13366 " callme(some(parameter1,\n" 13367 "<<<<<<< text by the vcs\n" 13368 " parameter2),\n" 13369 "||||||| text by the vcs\n" 13370 " parameter2),\n" 13371 " parameter3,\n" 13372 "======= text by the vcs\n" 13373 " parameter2, parameter3),\n" 13374 ">>>>>>> text by the vcs\n" 13375 " otherparameter);\n", 13376 format("int a;\n" 13377 "void f() {\n" 13378 " callme(some(parameter1,\n" 13379 "<<<<<<< text by the vcs\n" 13380 " parameter2),\n" 13381 "||||||| text by the vcs\n" 13382 " parameter2),\n" 13383 " parameter3,\n" 13384 "======= text by the vcs\n" 13385 " parameter2,\n" 13386 " parameter3),\n" 13387 ">>>>>>> text by the vcs\n" 13388 " otherparameter);\n")); 13389 13390 // Perforce markers. 13391 EXPECT_EQ("void f() {\n" 13392 " function(\n" 13393 ">>>> text by the vcs\n" 13394 " parameter,\n" 13395 "==== text by the vcs\n" 13396 " parameter,\n" 13397 "==== text by the vcs\n" 13398 " parameter,\n" 13399 "<<<< text by the vcs\n" 13400 " parameter);\n", 13401 format("void f() {\n" 13402 " function(\n" 13403 ">>>> text by the vcs\n" 13404 " parameter,\n" 13405 "==== text by the vcs\n" 13406 " parameter,\n" 13407 "==== text by the vcs\n" 13408 " parameter,\n" 13409 "<<<< text by the vcs\n" 13410 " parameter);\n")); 13411 13412 EXPECT_EQ("<<<<<<<\n" 13413 "|||||||\n" 13414 "=======\n" 13415 ">>>>>>>", 13416 format("<<<<<<<\n" 13417 "|||||||\n" 13418 "=======\n" 13419 ">>>>>>>")); 13420 13421 EXPECT_EQ("<<<<<<<\n" 13422 "|||||||\n" 13423 "int i;\n" 13424 "=======\n" 13425 ">>>>>>>", 13426 format("<<<<<<<\n" 13427 "|||||||\n" 13428 "int i;\n" 13429 "=======\n" 13430 ">>>>>>>")); 13431 13432 // FIXME: Handle parsing of macros around conflict markers correctly: 13433 EXPECT_EQ("#define Macro \\\n" 13434 "<<<<<<<\n" 13435 "Something \\\n" 13436 "|||||||\n" 13437 "Else \\\n" 13438 "=======\n" 13439 "Other \\\n" 13440 ">>>>>>>\n" 13441 " End int i;\n", 13442 format("#define Macro \\\n" 13443 "<<<<<<<\n" 13444 " Something \\\n" 13445 "|||||||\n" 13446 " Else \\\n" 13447 "=======\n" 13448 " Other \\\n" 13449 ">>>>>>>\n" 13450 " End\n" 13451 "int i;\n")); 13452 } 13453 13454 TEST_F(FormatTest, DisableRegions) { 13455 EXPECT_EQ("int i;\n" 13456 "// clang-format off\n" 13457 " int j;\n" 13458 "// clang-format on\n" 13459 "int k;", 13460 format(" int i;\n" 13461 " // clang-format off\n" 13462 " int j;\n" 13463 " // clang-format on\n" 13464 " int k;")); 13465 EXPECT_EQ("int i;\n" 13466 "/* clang-format off */\n" 13467 " int j;\n" 13468 "/* clang-format on */\n" 13469 "int k;", 13470 format(" int i;\n" 13471 " /* clang-format off */\n" 13472 " int j;\n" 13473 " /* clang-format on */\n" 13474 " int k;")); 13475 13476 // Don't reflow comments within disabled regions. 13477 EXPECT_EQ( 13478 "// clang-format off\n" 13479 "// long long long long long long line\n" 13480 "/* clang-format on */\n" 13481 "/* long long long\n" 13482 " * long long long\n" 13483 " * line */\n" 13484 "int i;\n" 13485 "/* clang-format off */\n" 13486 "/* long long long long long long line */\n", 13487 format("// clang-format off\n" 13488 "// long long long long long long line\n" 13489 "/* clang-format on */\n" 13490 "/* long long long long long long line */\n" 13491 "int i;\n" 13492 "/* clang-format off */\n" 13493 "/* long long long long long long line */\n", 13494 getLLVMStyleWithColumns(20))); 13495 } 13496 13497 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 13498 format("? ) ="); 13499 verifyNoCrash("#define a\\\n /**/}"); 13500 } 13501 13502 TEST_F(FormatTest, FormatsTableGenCode) { 13503 FormatStyle Style = getLLVMStyle(); 13504 Style.Language = FormatStyle::LK_TableGen; 13505 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 13506 } 13507 13508 TEST_F(FormatTest, ArrayOfTemplates) { 13509 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 13510 format("auto a = new unique_ptr<int > [ 10];")); 13511 13512 FormatStyle Spaces = getLLVMStyle(); 13513 Spaces.SpacesInSquareBrackets = true; 13514 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 13515 format("auto a = new unique_ptr<int > [10];", Spaces)); 13516 } 13517 13518 TEST_F(FormatTest, ArrayAsTemplateType) { 13519 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 13520 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 13521 13522 FormatStyle Spaces = getLLVMStyle(); 13523 Spaces.SpacesInSquareBrackets = true; 13524 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 13525 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 13526 } 13527 13528 TEST_F(FormatTest, NoSpaceAfterSuper) { 13529 verifyFormat("__super::FooBar();"); 13530 } 13531 13532 TEST(FormatStyle, GetStyleWithEmptyFileName) { 13533 llvm::vfs::InMemoryFileSystem FS; 13534 auto Style1 = getStyle("file", "", "Google", "", &FS); 13535 ASSERT_TRUE((bool)Style1); 13536 ASSERT_EQ(*Style1, getGoogleStyle()); 13537 } 13538 13539 TEST(FormatStyle, GetStyleOfFile) { 13540 llvm::vfs::InMemoryFileSystem FS; 13541 // Test 1: format file in the same directory. 13542 ASSERT_TRUE( 13543 FS.addFile("/a/.clang-format", 0, 13544 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 13545 ASSERT_TRUE( 13546 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 13547 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 13548 ASSERT_TRUE((bool)Style1); 13549 ASSERT_EQ(*Style1, getLLVMStyle()); 13550 13551 // Test 2.1: fallback to default. 13552 ASSERT_TRUE( 13553 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 13554 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 13555 ASSERT_TRUE((bool)Style2); 13556 ASSERT_EQ(*Style2, getMozillaStyle()); 13557 13558 // Test 2.2: no format on 'none' fallback style. 13559 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 13560 ASSERT_TRUE((bool)Style2); 13561 ASSERT_EQ(*Style2, getNoStyle()); 13562 13563 // Test 2.3: format if config is found with no based style while fallback is 13564 // 'none'. 13565 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 13566 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 13567 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 13568 ASSERT_TRUE((bool)Style2); 13569 ASSERT_EQ(*Style2, getLLVMStyle()); 13570 13571 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 13572 Style2 = getStyle("{}", "a.h", "none", "", &FS); 13573 ASSERT_TRUE((bool)Style2); 13574 ASSERT_EQ(*Style2, getLLVMStyle()); 13575 13576 // Test 3: format file in parent directory. 13577 ASSERT_TRUE( 13578 FS.addFile("/c/.clang-format", 0, 13579 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 13580 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 13581 llvm::MemoryBuffer::getMemBuffer("int i;"))); 13582 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 13583 ASSERT_TRUE((bool)Style3); 13584 ASSERT_EQ(*Style3, getGoogleStyle()); 13585 13586 // Test 4: error on invalid fallback style 13587 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 13588 ASSERT_FALSE((bool)Style4); 13589 llvm::consumeError(Style4.takeError()); 13590 13591 // Test 5: error on invalid yaml on command line 13592 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 13593 ASSERT_FALSE((bool)Style5); 13594 llvm::consumeError(Style5.takeError()); 13595 13596 // Test 6: error on invalid style 13597 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 13598 ASSERT_FALSE((bool)Style6); 13599 llvm::consumeError(Style6.takeError()); 13600 13601 // Test 7: found config file, error on parsing it 13602 ASSERT_TRUE( 13603 FS.addFile("/d/.clang-format", 0, 13604 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 13605 "InvalidKey: InvalidValue"))); 13606 ASSERT_TRUE( 13607 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 13608 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 13609 ASSERT_FALSE((bool)Style7); 13610 llvm::consumeError(Style7.takeError()); 13611 13612 // Test 8: inferred per-language defaults apply. 13613 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 13614 ASSERT_TRUE((bool)StyleTd); 13615 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 13616 } 13617 13618 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 13619 // Column limit is 20. 13620 std::string Code = "Type *a =\n" 13621 " new Type();\n" 13622 "g(iiiii, 0, jjjjj,\n" 13623 " 0, kkkkk, 0, mm);\n" 13624 "int bad = format ;"; 13625 std::string Expected = "auto a = new Type();\n" 13626 "g(iiiii, nullptr,\n" 13627 " jjjjj, nullptr,\n" 13628 " kkkkk, nullptr,\n" 13629 " mm);\n" 13630 "int bad = format ;"; 13631 FileID ID = Context.createInMemoryFile("format.cpp", Code); 13632 tooling::Replacements Replaces = toReplacements( 13633 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 13634 "auto "), 13635 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 13636 "nullptr"), 13637 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 13638 "nullptr"), 13639 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 13640 "nullptr")}); 13641 13642 format::FormatStyle Style = format::getLLVMStyle(); 13643 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 13644 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 13645 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 13646 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 13647 auto Result = applyAllReplacements(Code, *FormattedReplaces); 13648 EXPECT_TRUE(static_cast<bool>(Result)); 13649 EXPECT_EQ(Expected, *Result); 13650 } 13651 13652 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 13653 std::string Code = "#include \"a.h\"\n" 13654 "#include \"c.h\"\n" 13655 "\n" 13656 "int main() {\n" 13657 " return 0;\n" 13658 "}"; 13659 std::string Expected = "#include \"a.h\"\n" 13660 "#include \"b.h\"\n" 13661 "#include \"c.h\"\n" 13662 "\n" 13663 "int main() {\n" 13664 " return 0;\n" 13665 "}"; 13666 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 13667 tooling::Replacements Replaces = toReplacements( 13668 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 13669 "#include \"b.h\"\n")}); 13670 13671 format::FormatStyle Style = format::getLLVMStyle(); 13672 Style.SortIncludes = true; 13673 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 13674 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 13675 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 13676 auto Result = applyAllReplacements(Code, *FormattedReplaces); 13677 EXPECT_TRUE(static_cast<bool>(Result)); 13678 EXPECT_EQ(Expected, *Result); 13679 } 13680 13681 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 13682 EXPECT_EQ("using std::cin;\n" 13683 "using std::cout;", 13684 format("using std::cout;\n" 13685 "using std::cin;", getGoogleStyle())); 13686 } 13687 13688 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 13689 format::FormatStyle Style = format::getLLVMStyle(); 13690 Style.Standard = FormatStyle::LS_Cpp03; 13691 // cpp03 recognize this string as identifier u8 and literal character 'a' 13692 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 13693 } 13694 13695 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 13696 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 13697 // all modes, including C++11, C++14 and C++17 13698 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 13699 } 13700 13701 TEST_F(FormatTest, DoNotFormatLikelyXml) { 13702 EXPECT_EQ("<!-- ;> -->", 13703 format("<!-- ;> -->", getGoogleStyle())); 13704 EXPECT_EQ(" <!-- >; -->", 13705 format(" <!-- >; -->", getGoogleStyle())); 13706 } 13707 13708 TEST_F(FormatTest, StructuredBindings) { 13709 // Structured bindings is a C++17 feature. 13710 // all modes, including C++11, C++14 and C++17 13711 verifyFormat("auto [a, b] = f();"); 13712 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 13713 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 13714 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 13715 EXPECT_EQ("auto const volatile [a, b] = f();", 13716 format("auto const volatile[a, b] = f();")); 13717 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 13718 EXPECT_EQ("auto &[a, b, c] = f();", 13719 format("auto &[ a , b,c ] = f();")); 13720 EXPECT_EQ("auto &&[a, b, c] = f();", 13721 format("auto &&[ a , b,c ] = f();")); 13722 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 13723 EXPECT_EQ("auto const volatile &&[a, b] = f();", 13724 format("auto const volatile &&[a, b] = f();")); 13725 EXPECT_EQ("auto const &&[a, b] = f();", format("auto const && [a, b] = f();")); 13726 EXPECT_EQ("const auto &[a, b] = f();", format("const auto & [a, b] = f();")); 13727 EXPECT_EQ("const auto volatile &&[a, b] = f();", 13728 format("const auto volatile &&[a, b] = f();")); 13729 EXPECT_EQ("volatile const auto &&[a, b] = f();", 13730 format("volatile const auto &&[a, b] = f();")); 13731 EXPECT_EQ("const auto &&[a, b] = f();", format("const auto && [a, b] = f();")); 13732 13733 // Make sure we don't mistake structured bindings for lambdas. 13734 FormatStyle PointerMiddle = getLLVMStyle(); 13735 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 13736 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 13737 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 13738 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 13739 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 13740 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 13741 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 13742 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 13743 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 13744 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 13745 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 13746 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 13747 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 13748 13749 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 13750 format("for (const auto && [a, b] : some_range) {\n}")); 13751 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 13752 format("for (const auto & [a, b] : some_range) {\n}")); 13753 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 13754 format("for (const auto[a, b] : some_range) {\n}")); 13755 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 13756 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 13757 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 13758 EXPECT_EQ("auto const &[x, y](expr);", format("auto const & [x,y] (expr);")); 13759 EXPECT_EQ("auto const &&[x, y](expr);", format("auto const && [x,y] (expr);")); 13760 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 13761 EXPECT_EQ("auto const &[x, y]{expr};", format("auto const & [x,y] {expr};")); 13762 EXPECT_EQ("auto const &&[x, y]{expr};", format("auto const && [x,y] {expr};")); 13763 13764 format::FormatStyle Spaces = format::getLLVMStyle(); 13765 Spaces.SpacesInSquareBrackets = true; 13766 verifyFormat("auto [ a, b ] = f();", Spaces); 13767 verifyFormat("auto &&[ a, b ] = f();", Spaces); 13768 verifyFormat("auto &[ a, b ] = f();", Spaces); 13769 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 13770 verifyFormat("auto const &[ a, b ] = f();", Spaces); 13771 } 13772 13773 TEST_F(FormatTest, FileAndCode) { 13774 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 13775 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 13776 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 13777 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 13778 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n")); 13779 EXPECT_EQ( 13780 FormatStyle::LK_ObjC, 13781 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 13782 EXPECT_EQ(FormatStyle::LK_ObjC, 13783 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 13784 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 13785 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 13786 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n")); 13787 EXPECT_EQ(FormatStyle::LK_ObjC, 13788 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 13789 EXPECT_EQ( 13790 FormatStyle::LK_ObjC, 13791 guessLanguage("foo.h", 13792 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 13793 EXPECT_EQ( 13794 FormatStyle::LK_Cpp, 13795 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 13796 } 13797 13798 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 13799 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 13800 EXPECT_EQ(FormatStyle::LK_ObjC, 13801 guessLanguage("foo.h", "array[[calculator getIndex]];")); 13802 EXPECT_EQ(FormatStyle::LK_Cpp, 13803 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 13804 EXPECT_EQ( 13805 FormatStyle::LK_Cpp, 13806 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 13807 EXPECT_EQ(FormatStyle::LK_ObjC, 13808 guessLanguage("foo.h", "[[noreturn foo] bar];")); 13809 EXPECT_EQ(FormatStyle::LK_Cpp, 13810 guessLanguage("foo.h", "[[clang::fallthrough]];")); 13811 EXPECT_EQ(FormatStyle::LK_ObjC, 13812 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 13813 EXPECT_EQ(FormatStyle::LK_Cpp, 13814 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 13815 EXPECT_EQ(FormatStyle::LK_Cpp, 13816 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 13817 EXPECT_EQ(FormatStyle::LK_ObjC, 13818 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 13819 EXPECT_EQ(FormatStyle::LK_Cpp, 13820 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 13821 EXPECT_EQ( 13822 FormatStyle::LK_Cpp, 13823 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 13824 EXPECT_EQ( 13825 FormatStyle::LK_Cpp, 13826 guessLanguage("foo.h", 13827 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 13828 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 13829 } 13830 13831 TEST_F(FormatTest, GuessLanguageWithCaret) { 13832 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 13833 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 13834 EXPECT_EQ(FormatStyle::LK_ObjC, 13835 guessLanguage("foo.h", "int(^)(char, float);")); 13836 EXPECT_EQ(FormatStyle::LK_ObjC, 13837 guessLanguage("foo.h", "int(^foo)(char, float);")); 13838 EXPECT_EQ(FormatStyle::LK_ObjC, 13839 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 13840 EXPECT_EQ(FormatStyle::LK_ObjC, 13841 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 13842 EXPECT_EQ( 13843 FormatStyle::LK_ObjC, 13844 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 13845 } 13846 13847 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 13848 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 13849 "void f() {\n" 13850 " asm (\"mov %[e], %[d]\"\n" 13851 " : [d] \"=rm\" (d)\n" 13852 " [e] \"rm\" (*e));\n" 13853 "}")); 13854 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 13855 "void f() {\n" 13856 " _asm (\"mov %[e], %[d]\"\n" 13857 " : [d] \"=rm\" (d)\n" 13858 " [e] \"rm\" (*e));\n" 13859 "}")); 13860 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 13861 "void f() {\n" 13862 " __asm (\"mov %[e], %[d]\"\n" 13863 " : [d] \"=rm\" (d)\n" 13864 " [e] \"rm\" (*e));\n" 13865 "}")); 13866 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 13867 "void f() {\n" 13868 " __asm__ (\"mov %[e], %[d]\"\n" 13869 " : [d] \"=rm\" (d)\n" 13870 " [e] \"rm\" (*e));\n" 13871 "}")); 13872 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", 13873 "void f() {\n" 13874 " asm (\"mov %[e], %[d]\"\n" 13875 " : [d] \"=rm\" (d),\n" 13876 " [e] \"rm\" (*e));\n" 13877 "}")); 13878 EXPECT_EQ(FormatStyle::LK_Cpp, 13879 guessLanguage("foo.h", "void f() {\n" 13880 " asm volatile (\"mov %[e], %[d]\"\n" 13881 " : [d] \"=rm\" (d)\n" 13882 " [e] \"rm\" (*e));\n" 13883 "}")); 13884 } 13885 13886 TEST_F(FormatTest, GuessLanguageWithChildLines) { 13887 EXPECT_EQ(FormatStyle::LK_Cpp, 13888 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 13889 EXPECT_EQ(FormatStyle::LK_ObjC, 13890 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 13891 EXPECT_EQ( 13892 FormatStyle::LK_Cpp, 13893 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 13894 EXPECT_EQ( 13895 FormatStyle::LK_ObjC, 13896 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 13897 } 13898 13899 TEST_F(FormatTest, TypenameMacros) { 13900 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 13901 13902 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 13903 FormatStyle Google = getGoogleStyleWithColumns(0); 13904 Google.TypenameMacros = TypenameMacros; 13905 verifyFormat("struct foo {\n" 13906 " int bar;\n" 13907 " TAILQ_ENTRY(a) bleh;\n" 13908 "};", Google); 13909 13910 FormatStyle Macros = getLLVMStyle(); 13911 Macros.TypenameMacros = TypenameMacros; 13912 13913 verifyFormat("STACK_OF(int) a;", Macros); 13914 verifyFormat("STACK_OF(int) *a;", Macros); 13915 verifyFormat("STACK_OF(int const *) *a;", Macros); 13916 verifyFormat("STACK_OF(int *const) *a;", Macros); 13917 verifyFormat("STACK_OF(int, string) a;", Macros); 13918 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 13919 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 13920 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 13921 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 13922 13923 Macros.PointerAlignment = FormatStyle::PAS_Left; 13924 verifyFormat("STACK_OF(int)* a;", Macros); 13925 verifyFormat("STACK_OF(int*)* a;", Macros); 13926 } 13927 13928 } // end namespace 13929 } // end namespace format 13930 } // end namespace clang 13931