1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "clang/Format/Format.h" 11 12 #include "../Tooling/ReplacementTest.h" 13 #include "FormatTestUtils.h" 14 15 #include "clang/Frontend/TextDiagnosticPrinter.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "gtest/gtest.h" 19 20 #define DEBUG_TYPE "format-test" 21 22 using clang::tooling::ReplacementTest; 23 using clang::tooling::toReplacements; 24 25 namespace clang { 26 namespace format { 27 namespace { 28 29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 30 31 class FormatTest : public ::testing::Test { 32 protected: 33 enum StatusCheck { 34 SC_ExpectComplete, 35 SC_ExpectIncomplete, 36 SC_DoNotCheck 37 }; 38 39 std::string format(llvm::StringRef Code, 40 const FormatStyle &Style = getLLVMStyle(), 41 StatusCheck CheckComplete = SC_ExpectComplete) { 42 DEBUG(llvm::errs() << "---\n"); 43 DEBUG(llvm::errs() << Code << "\n\n"); 44 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 45 FormattingAttemptStatus Status; 46 tooling::Replacements Replaces = 47 reformat(Style, Code, Ranges, "<stdin>", &Status); 48 if (CheckComplete != SC_DoNotCheck) { 49 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 50 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 51 << Code << "\n\n"; 52 } 53 ReplacementCount = Replaces.size(); 54 auto Result = applyAllReplacements(Code, Replaces); 55 EXPECT_TRUE(static_cast<bool>(Result)); 56 DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 57 return *Result; 58 } 59 60 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 61 Style.ColumnLimit = ColumnLimit; 62 return Style; 63 } 64 65 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 66 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 67 } 68 69 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 70 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 71 } 72 73 void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code, 74 const FormatStyle &Style = getLLVMStyle()) { 75 EXPECT_EQ(Expected.str(), format(Code, Style)); 76 if (Style.Language == FormatStyle::LK_Cpp) { 77 // Objective-C++ is a superset of C++, so everything checked for C++ 78 // needs to be checked for Objective-C++ as well. 79 FormatStyle ObjCStyle = Style; 80 ObjCStyle.Language = FormatStyle::LK_ObjC; 81 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 82 } 83 } 84 85 void verifyFormat(llvm::StringRef Code, 86 const FormatStyle &Style = getLLVMStyle()) { 87 verifyFormat(Code, test::messUp(Code), Style); 88 } 89 90 void verifyIncompleteFormat(llvm::StringRef Code, 91 const FormatStyle &Style = getLLVMStyle()) { 92 EXPECT_EQ(Code.str(), 93 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 94 } 95 96 void verifyGoogleFormat(llvm::StringRef Code) { 97 verifyFormat(Code, getGoogleStyle()); 98 } 99 100 void verifyIndependentOfContext(llvm::StringRef text) { 101 verifyFormat(text); 102 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 103 } 104 105 /// \brief Verify that clang-format does not crash on the given input. 106 void verifyNoCrash(llvm::StringRef Code, 107 const FormatStyle &Style = getLLVMStyle()) { 108 format(Code, Style, SC_DoNotCheck); 109 } 110 111 int ReplacementCount; 112 }; 113 114 TEST_F(FormatTest, MessUp) { 115 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 116 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 117 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 118 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 119 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 120 } 121 122 //===----------------------------------------------------------------------===// 123 // Basic function tests. 124 //===----------------------------------------------------------------------===// 125 126 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 127 EXPECT_EQ(";", format(";")); 128 } 129 130 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 131 EXPECT_EQ("int i;", format(" int i;")); 132 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 133 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 134 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 135 } 136 137 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 138 EXPECT_EQ("int i;", format("int\ni;")); 139 } 140 141 TEST_F(FormatTest, FormatsNestedBlockStatements) { 142 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 143 } 144 145 TEST_F(FormatTest, FormatsNestedCall) { 146 verifyFormat("Method(f1, f2(f3));"); 147 verifyFormat("Method(f1(f2, f3()));"); 148 verifyFormat("Method(f1(f2, (f3())));"); 149 } 150 151 TEST_F(FormatTest, NestedNameSpecifiers) { 152 verifyFormat("vector<::Type> v;"); 153 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 154 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 155 verifyFormat("bool a = 2 < ::SomeFunction();"); 156 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 157 verifyFormat("some::string getName();"); 158 } 159 160 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 161 EXPECT_EQ("if (a) {\n" 162 " f();\n" 163 "}", 164 format("if(a){f();}")); 165 EXPECT_EQ(4, ReplacementCount); 166 EXPECT_EQ("if (a) {\n" 167 " f();\n" 168 "}", 169 format("if (a) {\n" 170 " f();\n" 171 "}")); 172 EXPECT_EQ(0, ReplacementCount); 173 EXPECT_EQ("/*\r\n" 174 "\r\n" 175 "*/\r\n", 176 format("/*\r\n" 177 "\r\n" 178 "*/\r\n")); 179 EXPECT_EQ(0, ReplacementCount); 180 } 181 182 TEST_F(FormatTest, RemovesEmptyLines) { 183 EXPECT_EQ("class C {\n" 184 " int i;\n" 185 "};", 186 format("class C {\n" 187 " int i;\n" 188 "\n" 189 "};")); 190 191 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 192 EXPECT_EQ("namespace N {\n" 193 "\n" 194 "int i;\n" 195 "}", 196 format("namespace N {\n" 197 "\n" 198 "int i;\n" 199 "}", 200 getGoogleStyle())); 201 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 202 "\n" 203 "int i;\n" 204 "}", 205 format("extern /**/ \"C\" /**/ {\n" 206 "\n" 207 "int i;\n" 208 "}", 209 getGoogleStyle())); 210 211 // ...but do keep inlining and removing empty lines for non-block extern "C" 212 // functions. 213 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 214 EXPECT_EQ("extern \"C\" int f() {\n" 215 " int i = 42;\n" 216 " return i;\n" 217 "}", 218 format("extern \"C\" int f() {\n" 219 "\n" 220 " int i = 42;\n" 221 " return i;\n" 222 "}", 223 getGoogleStyle())); 224 225 // Remove empty lines at the beginning and end of blocks. 226 EXPECT_EQ("void f() {\n" 227 "\n" 228 " if (a) {\n" 229 "\n" 230 " f();\n" 231 " }\n" 232 "}", 233 format("void f() {\n" 234 "\n" 235 " if (a) {\n" 236 "\n" 237 " f();\n" 238 "\n" 239 " }\n" 240 "\n" 241 "}", 242 getLLVMStyle())); 243 EXPECT_EQ("void f() {\n" 244 " if (a) {\n" 245 " f();\n" 246 " }\n" 247 "}", 248 format("void f() {\n" 249 "\n" 250 " if (a) {\n" 251 "\n" 252 " f();\n" 253 "\n" 254 " }\n" 255 "\n" 256 "}", 257 getGoogleStyle())); 258 259 // Don't remove empty lines in more complex control statements. 260 EXPECT_EQ("void f() {\n" 261 " if (a) {\n" 262 " f();\n" 263 "\n" 264 " } else if (b) {\n" 265 " f();\n" 266 " }\n" 267 "}", 268 format("void f() {\n" 269 " if (a) {\n" 270 " f();\n" 271 "\n" 272 " } else if (b) {\n" 273 " f();\n" 274 "\n" 275 " }\n" 276 "\n" 277 "}")); 278 279 // FIXME: This is slightly inconsistent. 280 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 281 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 282 EXPECT_EQ("namespace {\n" 283 "int i;\n" 284 "}", 285 format("namespace {\n" 286 "int i;\n" 287 "\n" 288 "}", LLVMWithNoNamespaceFix)); 289 EXPECT_EQ("namespace {\n" 290 "int i;\n" 291 "}", 292 format("namespace {\n" 293 "int i;\n" 294 "\n" 295 "}")); 296 EXPECT_EQ("namespace {\n" 297 "int i;\n" 298 "\n" 299 "} // namespace", 300 format("namespace {\n" 301 "int i;\n" 302 "\n" 303 "} // namespace")); 304 305 FormatStyle Style = getLLVMStyle(); 306 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 307 Style.MaxEmptyLinesToKeep = 2; 308 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 309 Style.BraceWrapping.AfterClass = true; 310 Style.BraceWrapping.AfterFunction = true; 311 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 312 313 EXPECT_EQ("class Foo\n" 314 "{\n" 315 " Foo() {}\n" 316 "\n" 317 " void funk() {}\n" 318 "};", 319 format("class Foo\n" 320 "{\n" 321 " Foo()\n" 322 " {\n" 323 " }\n" 324 "\n" 325 " void funk() {}\n" 326 "};", 327 Style)); 328 } 329 330 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 331 verifyFormat("x = (a) and (b);"); 332 verifyFormat("x = (a) or (b);"); 333 verifyFormat("x = (a) bitand (b);"); 334 verifyFormat("x = (a) bitor (b);"); 335 verifyFormat("x = (a) not_eq (b);"); 336 verifyFormat("x = (a) and_eq (b);"); 337 verifyFormat("x = (a) or_eq (b);"); 338 verifyFormat("x = (a) xor (b);"); 339 } 340 341 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 342 verifyFormat("x = compl(a);"); 343 verifyFormat("x = not(a);"); 344 verifyFormat("x = bitand(a);"); 345 // Unary operator must not be merged with the next identifier 346 verifyFormat("x = compl a;"); 347 verifyFormat("x = not a;"); 348 verifyFormat("x = bitand a;"); 349 } 350 351 //===----------------------------------------------------------------------===// 352 // Tests for control statements. 353 //===----------------------------------------------------------------------===// 354 355 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 356 verifyFormat("if (true)\n f();\ng();"); 357 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 358 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 359 verifyFormat("if constexpr (true)\n" 360 " f();\ng();"); 361 verifyFormat("if constexpr (a)\n" 362 " if constexpr (b)\n" 363 " if constexpr (c)\n" 364 " g();\n" 365 "h();"); 366 verifyFormat("if constexpr (a)\n" 367 " if constexpr (b) {\n" 368 " f();\n" 369 " }\n" 370 "g();"); 371 372 FormatStyle AllowsMergedIf = getLLVMStyle(); 373 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 374 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 375 verifyFormat("if (a)\n" 376 " // comment\n" 377 " f();", 378 AllowsMergedIf); 379 verifyFormat("{\n" 380 " if (a)\n" 381 " label:\n" 382 " f();\n" 383 "}", 384 AllowsMergedIf); 385 verifyFormat("#define A \\\n" 386 " if (a) \\\n" 387 " label: \\\n" 388 " f()", 389 AllowsMergedIf); 390 verifyFormat("if (a)\n" 391 " ;", 392 AllowsMergedIf); 393 verifyFormat("if (a)\n" 394 " if (b) return;", 395 AllowsMergedIf); 396 397 verifyFormat("if (a) // Can't merge this\n" 398 " f();\n", 399 AllowsMergedIf); 400 verifyFormat("if (a) /* still don't merge */\n" 401 " f();", 402 AllowsMergedIf); 403 verifyFormat("if (a) { // Never merge this\n" 404 " f();\n" 405 "}", 406 AllowsMergedIf); 407 verifyFormat("if (a) { /* Never merge this */\n" 408 " f();\n" 409 "}", 410 AllowsMergedIf); 411 412 AllowsMergedIf.ColumnLimit = 14; 413 verifyFormat("if (a) return;", AllowsMergedIf); 414 verifyFormat("if (aaaaaaaaa)\n" 415 " return;", 416 AllowsMergedIf); 417 418 AllowsMergedIf.ColumnLimit = 13; 419 verifyFormat("if (a)\n return;", AllowsMergedIf); 420 } 421 422 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 423 FormatStyle AllowsMergedLoops = getLLVMStyle(); 424 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 425 verifyFormat("while (true) continue;", AllowsMergedLoops); 426 verifyFormat("for (;;) continue;", AllowsMergedLoops); 427 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 428 verifyFormat("while (true)\n" 429 " ;", 430 AllowsMergedLoops); 431 verifyFormat("for (;;)\n" 432 " ;", 433 AllowsMergedLoops); 434 verifyFormat("for (;;)\n" 435 " for (;;) continue;", 436 AllowsMergedLoops); 437 verifyFormat("for (;;) // Can't merge this\n" 438 " continue;", 439 AllowsMergedLoops); 440 verifyFormat("for (;;) /* still don't merge */\n" 441 " continue;", 442 AllowsMergedLoops); 443 } 444 445 TEST_F(FormatTest, FormatShortBracedStatements) { 446 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 447 AllowSimpleBracedStatements.ColumnLimit = 40; 448 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 449 450 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 451 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 452 453 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 454 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 455 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 456 457 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 458 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 459 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 460 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 461 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 462 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 463 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 464 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 465 verifyFormat("if (true) {\n" 466 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 467 "}", 468 AllowSimpleBracedStatements); 469 verifyFormat("if (true) { //\n" 470 " f();\n" 471 "}", 472 AllowSimpleBracedStatements); 473 verifyFormat("if (true) {\n" 474 " f();\n" 475 " f();\n" 476 "}", 477 AllowSimpleBracedStatements); 478 verifyFormat("if (true) {\n" 479 " f();\n" 480 "} else {\n" 481 " f();\n" 482 "}", 483 AllowSimpleBracedStatements); 484 485 verifyFormat("struct A2 {\n" 486 " int X;\n" 487 "};", 488 AllowSimpleBracedStatements); 489 verifyFormat("typedef struct A2 {\n" 490 " int X;\n" 491 "} A2_t;", 492 AllowSimpleBracedStatements); 493 verifyFormat("template <int> struct A2 {\n" 494 " struct B {};\n" 495 "};", 496 AllowSimpleBracedStatements); 497 498 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 499 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 500 verifyFormat("if (true) {\n" 501 " f();\n" 502 "}", 503 AllowSimpleBracedStatements); 504 verifyFormat("if (true) {\n" 505 " f();\n" 506 "} else {\n" 507 " f();\n" 508 "}", 509 AllowSimpleBracedStatements); 510 511 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 512 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 513 verifyFormat("while (true) {\n" 514 " f();\n" 515 "}", 516 AllowSimpleBracedStatements); 517 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 518 verifyFormat("for (;;) {\n" 519 " f();\n" 520 "}", 521 AllowSimpleBracedStatements); 522 523 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 524 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 525 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true; 526 527 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 528 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 529 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 530 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 531 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 532 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 533 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 534 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 535 verifyFormat("if (true)\n" 536 "{\n" 537 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 538 "}", 539 AllowSimpleBracedStatements); 540 verifyFormat("if (true)\n" 541 "{ //\n" 542 " f();\n" 543 "}", 544 AllowSimpleBracedStatements); 545 verifyFormat("if (true)\n" 546 "{\n" 547 " f();\n" 548 " f();\n" 549 "}", 550 AllowSimpleBracedStatements); 551 verifyFormat("if (true)\n" 552 "{\n" 553 " f();\n" 554 "} else\n" 555 "{\n" 556 " f();\n" 557 "}", 558 AllowSimpleBracedStatements); 559 560 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 561 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 562 verifyFormat("if (true)\n" 563 "{\n" 564 " f();\n" 565 "}", 566 AllowSimpleBracedStatements); 567 verifyFormat("if (true)\n" 568 "{\n" 569 " f();\n" 570 "} else\n" 571 "{\n" 572 " f();\n" 573 "}", 574 AllowSimpleBracedStatements); 575 576 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 577 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 578 verifyFormat("while (true)\n" 579 "{\n" 580 " f();\n" 581 "}", 582 AllowSimpleBracedStatements); 583 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 584 verifyFormat("for (;;)\n" 585 "{\n" 586 " f();\n" 587 "}", 588 AllowSimpleBracedStatements); 589 } 590 591 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 592 FormatStyle Style = getLLVMStyleWithColumns(60); 593 Style.AllowShortBlocksOnASingleLine = true; 594 Style.AllowShortIfStatementsOnASingleLine = true; 595 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 596 EXPECT_EQ("#define A \\\n" 597 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 598 " { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n" 599 "X;", 600 format("#define A \\\n" 601 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 602 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 603 " }\n" 604 "X;", 605 Style)); 606 } 607 608 TEST_F(FormatTest, ParseIfElse) { 609 verifyFormat("if (true)\n" 610 " if (true)\n" 611 " if (true)\n" 612 " f();\n" 613 " else\n" 614 " g();\n" 615 " else\n" 616 " h();\n" 617 "else\n" 618 " i();"); 619 verifyFormat("if (true)\n" 620 " if (true)\n" 621 " if (true) {\n" 622 " if (true)\n" 623 " f();\n" 624 " } else {\n" 625 " g();\n" 626 " }\n" 627 " else\n" 628 " h();\n" 629 "else {\n" 630 " i();\n" 631 "}"); 632 verifyFormat("if (true)\n" 633 " if constexpr (true)\n" 634 " if (true) {\n" 635 " if constexpr (true)\n" 636 " f();\n" 637 " } else {\n" 638 " g();\n" 639 " }\n" 640 " else\n" 641 " h();\n" 642 "else {\n" 643 " i();\n" 644 "}"); 645 verifyFormat("void f() {\n" 646 " if (a) {\n" 647 " } else {\n" 648 " }\n" 649 "}"); 650 } 651 652 TEST_F(FormatTest, ElseIf) { 653 verifyFormat("if (a) {\n} else if (b) {\n}"); 654 verifyFormat("if (a)\n" 655 " f();\n" 656 "else if (b)\n" 657 " g();\n" 658 "else\n" 659 " h();"); 660 verifyFormat("if constexpr (a)\n" 661 " f();\n" 662 "else if constexpr (b)\n" 663 " g();\n" 664 "else\n" 665 " h();"); 666 verifyFormat("if (a) {\n" 667 " f();\n" 668 "}\n" 669 "// or else ..\n" 670 "else {\n" 671 " g()\n" 672 "}"); 673 674 verifyFormat("if (a) {\n" 675 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 676 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 677 "}"); 678 verifyFormat("if (a) {\n" 679 "} else if (\n" 680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 681 "}", 682 getLLVMStyleWithColumns(62)); 683 verifyFormat("if (a) {\n" 684 "} else if constexpr (\n" 685 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 686 "}", 687 getLLVMStyleWithColumns(62)); 688 } 689 690 TEST_F(FormatTest, FormatsForLoop) { 691 verifyFormat( 692 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 693 " ++VeryVeryLongLoopVariable)\n" 694 " ;"); 695 verifyFormat("for (;;)\n" 696 " f();"); 697 verifyFormat("for (;;) {\n}"); 698 verifyFormat("for (;;) {\n" 699 " f();\n" 700 "}"); 701 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 702 703 verifyFormat( 704 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 705 " E = UnwrappedLines.end();\n" 706 " I != E; ++I) {\n}"); 707 708 verifyFormat( 709 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 710 " ++IIIII) {\n}"); 711 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 712 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 713 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 714 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 715 " I = FD->getDeclsInPrototypeScope().begin(),\n" 716 " E = FD->getDeclsInPrototypeScope().end();\n" 717 " I != E; ++I) {\n}"); 718 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 719 " I = Container.begin(),\n" 720 " E = Container.end();\n" 721 " I != E; ++I) {\n}", 722 getLLVMStyleWithColumns(76)); 723 724 verifyFormat( 725 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 726 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 729 " ++aaaaaaaaaaa) {\n}"); 730 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 731 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 732 " ++i) {\n}"); 733 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 734 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 735 "}"); 736 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 737 " aaaaaaaaaa);\n" 738 " iter; ++iter) {\n" 739 "}"); 740 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 741 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 742 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 743 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 744 745 FormatStyle NoBinPacking = getLLVMStyle(); 746 NoBinPacking.BinPackParameters = false; 747 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 748 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 749 " aaaaaaaaaaaaaaaa,\n" 750 " aaaaaaaaaaaaaaaa,\n" 751 " aaaaaaaaaaaaaaaa);\n" 752 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 753 "}", 754 NoBinPacking); 755 verifyFormat( 756 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 757 " E = UnwrappedLines.end();\n" 758 " I != E;\n" 759 " ++I) {\n}", 760 NoBinPacking); 761 762 FormatStyle AlignLeft = getLLVMStyle(); 763 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 764 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 765 } 766 767 TEST_F(FormatTest, RangeBasedForLoops) { 768 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 769 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 770 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 771 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 772 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 773 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 774 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 775 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 776 } 777 778 TEST_F(FormatTest, ForEachLoops) { 779 verifyFormat("void f() {\n" 780 " foreach (Item *item, itemlist) {}\n" 781 " Q_FOREACH (Item *item, itemlist) {}\n" 782 " BOOST_FOREACH (Item *item, itemlist) {}\n" 783 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 784 "}"); 785 786 // As function-like macros. 787 verifyFormat("#define foreach(x, y)\n" 788 "#define Q_FOREACH(x, y)\n" 789 "#define BOOST_FOREACH(x, y)\n" 790 "#define UNKNOWN_FOREACH(x, y)\n"); 791 792 // Not as function-like macros. 793 verifyFormat("#define foreach (x, y)\n" 794 "#define Q_FOREACH (x, y)\n" 795 "#define BOOST_FOREACH (x, y)\n" 796 "#define UNKNOWN_FOREACH (x, y)\n"); 797 } 798 799 TEST_F(FormatTest, FormatsWhileLoop) { 800 verifyFormat("while (true) {\n}"); 801 verifyFormat("while (true)\n" 802 " f();"); 803 verifyFormat("while () {\n}"); 804 verifyFormat("while () {\n" 805 " f();\n" 806 "}"); 807 } 808 809 TEST_F(FormatTest, FormatsDoWhile) { 810 verifyFormat("do {\n" 811 " do_something();\n" 812 "} while (something());"); 813 verifyFormat("do\n" 814 " do_something();\n" 815 "while (something());"); 816 } 817 818 TEST_F(FormatTest, FormatsSwitchStatement) { 819 verifyFormat("switch (x) {\n" 820 "case 1:\n" 821 " f();\n" 822 " break;\n" 823 "case kFoo:\n" 824 "case ns::kBar:\n" 825 "case kBaz:\n" 826 " break;\n" 827 "default:\n" 828 " g();\n" 829 " break;\n" 830 "}"); 831 verifyFormat("switch (x) {\n" 832 "case 1: {\n" 833 " f();\n" 834 " break;\n" 835 "}\n" 836 "case 2: {\n" 837 " break;\n" 838 "}\n" 839 "}"); 840 verifyFormat("switch (x) {\n" 841 "case 1: {\n" 842 " f();\n" 843 " {\n" 844 " g();\n" 845 " h();\n" 846 " }\n" 847 " break;\n" 848 "}\n" 849 "}"); 850 verifyFormat("switch (x) {\n" 851 "case 1: {\n" 852 " f();\n" 853 " if (foo) {\n" 854 " g();\n" 855 " h();\n" 856 " }\n" 857 " break;\n" 858 "}\n" 859 "}"); 860 verifyFormat("switch (x) {\n" 861 "case 1: {\n" 862 " f();\n" 863 " g();\n" 864 "} break;\n" 865 "}"); 866 verifyFormat("switch (test)\n" 867 " ;"); 868 verifyFormat("switch (x) {\n" 869 "default: {\n" 870 " // Do nothing.\n" 871 "}\n" 872 "}"); 873 verifyFormat("switch (x) {\n" 874 "// comment\n" 875 "// if 1, do f()\n" 876 "case 1:\n" 877 " f();\n" 878 "}"); 879 verifyFormat("switch (x) {\n" 880 "case 1:\n" 881 " // Do amazing stuff\n" 882 " {\n" 883 " f();\n" 884 " g();\n" 885 " }\n" 886 " break;\n" 887 "}"); 888 verifyFormat("#define A \\\n" 889 " switch (x) { \\\n" 890 " case a: \\\n" 891 " foo = b; \\\n" 892 " }", 893 getLLVMStyleWithColumns(20)); 894 verifyFormat("#define OPERATION_CASE(name) \\\n" 895 " case OP_name: \\\n" 896 " return operations::Operation##name\n", 897 getLLVMStyleWithColumns(40)); 898 verifyFormat("switch (x) {\n" 899 "case 1:;\n" 900 "default:;\n" 901 " int i;\n" 902 "}"); 903 904 verifyGoogleFormat("switch (x) {\n" 905 " case 1:\n" 906 " f();\n" 907 " break;\n" 908 " case kFoo:\n" 909 " case ns::kBar:\n" 910 " case kBaz:\n" 911 " break;\n" 912 " default:\n" 913 " g();\n" 914 " break;\n" 915 "}"); 916 verifyGoogleFormat("switch (x) {\n" 917 " case 1: {\n" 918 " f();\n" 919 " break;\n" 920 " }\n" 921 "}"); 922 verifyGoogleFormat("switch (test)\n" 923 " ;"); 924 925 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 926 " case OP_name: \\\n" 927 " return operations::Operation##name\n"); 928 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 929 " // Get the correction operation class.\n" 930 " switch (OpCode) {\n" 931 " CASE(Add);\n" 932 " CASE(Subtract);\n" 933 " default:\n" 934 " return operations::Unknown;\n" 935 " }\n" 936 "#undef OPERATION_CASE\n" 937 "}"); 938 verifyFormat("DEBUG({\n" 939 " switch (x) {\n" 940 " case A:\n" 941 " f();\n" 942 " break;\n" 943 " // fallthrough\n" 944 " case B:\n" 945 " g();\n" 946 " break;\n" 947 " }\n" 948 "});"); 949 EXPECT_EQ("DEBUG({\n" 950 " switch (x) {\n" 951 " case A:\n" 952 " f();\n" 953 " break;\n" 954 " // On B:\n" 955 " case B:\n" 956 " g();\n" 957 " break;\n" 958 " }\n" 959 "});", 960 format("DEBUG({\n" 961 " switch (x) {\n" 962 " case A:\n" 963 " f();\n" 964 " break;\n" 965 " // On B:\n" 966 " case B:\n" 967 " g();\n" 968 " break;\n" 969 " }\n" 970 "});", 971 getLLVMStyle())); 972 verifyFormat("switch (a) {\n" 973 "case (b):\n" 974 " return;\n" 975 "}"); 976 977 verifyFormat("switch (a) {\n" 978 "case some_namespace::\n" 979 " some_constant:\n" 980 " return;\n" 981 "}", 982 getLLVMStyleWithColumns(34)); 983 } 984 985 TEST_F(FormatTest, CaseRanges) { 986 verifyFormat("switch (x) {\n" 987 "case 'A' ... 'Z':\n" 988 "case 1 ... 5:\n" 989 "case a ... b:\n" 990 " break;\n" 991 "}"); 992 } 993 994 TEST_F(FormatTest, ShortCaseLabels) { 995 FormatStyle Style = getLLVMStyle(); 996 Style.AllowShortCaseLabelsOnASingleLine = true; 997 verifyFormat("switch (a) {\n" 998 "case 1: x = 1; break;\n" 999 "case 2: return;\n" 1000 "case 3:\n" 1001 "case 4:\n" 1002 "case 5: return;\n" 1003 "case 6: // comment\n" 1004 " return;\n" 1005 "case 7:\n" 1006 " // comment\n" 1007 " return;\n" 1008 "case 8:\n" 1009 " x = 8; // comment\n" 1010 " break;\n" 1011 "default: y = 1; break;\n" 1012 "}", 1013 Style); 1014 verifyFormat("switch (a) {\n" 1015 "case 0: return; // comment\n" 1016 "case 1: break; // comment\n" 1017 "case 2: return;\n" 1018 "// comment\n" 1019 "case 3: return;\n" 1020 "// comment 1\n" 1021 "// comment 2\n" 1022 "// comment 3\n" 1023 "case 4: break; /* comment */\n" 1024 "case 5:\n" 1025 " // comment\n" 1026 " break;\n" 1027 "case 6: /* comment */ x = 1; break;\n" 1028 "case 7: x = /* comment */ 1; break;\n" 1029 "case 8:\n" 1030 " x = 1; /* comment */\n" 1031 " break;\n" 1032 "case 9:\n" 1033 " break; // comment line 1\n" 1034 " // comment line 2\n" 1035 "}", 1036 Style); 1037 EXPECT_EQ("switch (a) {\n" 1038 "case 1:\n" 1039 " x = 8;\n" 1040 " // fall through\n" 1041 "case 2: x = 8;\n" 1042 "// comment\n" 1043 "case 3:\n" 1044 " return; /* comment line 1\n" 1045 " * comment line 2 */\n" 1046 "case 4: i = 8;\n" 1047 "// something else\n" 1048 "#if FOO\n" 1049 "case 5: break;\n" 1050 "#endif\n" 1051 "}", 1052 format("switch (a) {\n" 1053 "case 1: x = 8;\n" 1054 " // fall through\n" 1055 "case 2:\n" 1056 " x = 8;\n" 1057 "// comment\n" 1058 "case 3:\n" 1059 " return; /* comment line 1\n" 1060 " * comment line 2 */\n" 1061 "case 4:\n" 1062 " i = 8;\n" 1063 "// something else\n" 1064 "#if FOO\n" 1065 "case 5: break;\n" 1066 "#endif\n" 1067 "}", 1068 Style)); 1069 EXPECT_EQ("switch (a) {\n" "case 0:\n" 1070 " return; // long long long long long long long long long long long long comment\n" 1071 " // line\n" "}", 1072 format("switch (a) {\n" 1073 "case 0: return; // long long long long long long long long long long long long comment line\n" 1074 "}", 1075 Style)); 1076 EXPECT_EQ("switch (a) {\n" 1077 "case 0:\n" 1078 " return; /* long long long long long long long long long long long long comment\n" 1079 " line */\n" 1080 "}", 1081 format("switch (a) {\n" 1082 "case 0: return; /* long long long long long long long long long long long long comment line */\n" 1083 "}", 1084 Style)); 1085 verifyFormat("switch (a) {\n" 1086 "#if FOO\n" 1087 "case 0: return 0;\n" 1088 "#endif\n" 1089 "}", 1090 Style); 1091 verifyFormat("switch (a) {\n" 1092 "case 1: {\n" 1093 "}\n" 1094 "case 2: {\n" 1095 " return;\n" 1096 "}\n" 1097 "case 3: {\n" 1098 " x = 1;\n" 1099 " return;\n" 1100 "}\n" 1101 "case 4:\n" 1102 " if (x)\n" 1103 " return;\n" 1104 "}", 1105 Style); 1106 Style.ColumnLimit = 21; 1107 verifyFormat("switch (a) {\n" 1108 "case 1: x = 1; break;\n" 1109 "case 2: return;\n" 1110 "case 3:\n" 1111 "case 4:\n" 1112 "case 5: return;\n" 1113 "default:\n" 1114 " y = 1;\n" 1115 " break;\n" 1116 "}", 1117 Style); 1118 } 1119 1120 TEST_F(FormatTest, FormatsLabels) { 1121 verifyFormat("void f() {\n" 1122 " some_code();\n" 1123 "test_label:\n" 1124 " some_other_code();\n" 1125 " {\n" 1126 " some_more_code();\n" 1127 " another_label:\n" 1128 " some_more_code();\n" 1129 " }\n" 1130 "}"); 1131 verifyFormat("{\n" 1132 " some_code();\n" 1133 "test_label:\n" 1134 " some_other_code();\n" 1135 "}"); 1136 verifyFormat("{\n" 1137 " some_code();\n" 1138 "test_label:;\n" 1139 " int i = 0;\n" 1140 "}"); 1141 } 1142 1143 //===----------------------------------------------------------------------===// 1144 // Tests for classes, namespaces, etc. 1145 //===----------------------------------------------------------------------===// 1146 1147 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1148 verifyFormat("class A {};"); 1149 } 1150 1151 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1152 verifyFormat("class A {\n" 1153 "public:\n" 1154 "public: // comment\n" 1155 "protected:\n" 1156 "private:\n" 1157 " void f() {}\n" 1158 "};"); 1159 verifyGoogleFormat("class A {\n" 1160 " public:\n" 1161 " protected:\n" 1162 " private:\n" 1163 " void f() {}\n" 1164 "};"); 1165 verifyFormat("class A {\n" 1166 "public slots:\n" 1167 " void f1() {}\n" 1168 "public Q_SLOTS:\n" 1169 " void f2() {}\n" 1170 "protected slots:\n" 1171 " void f3() {}\n" 1172 "protected Q_SLOTS:\n" 1173 " void f4() {}\n" 1174 "private slots:\n" 1175 " void f5() {}\n" 1176 "private Q_SLOTS:\n" 1177 " void f6() {}\n" 1178 "signals:\n" 1179 " void g1();\n" 1180 "Q_SIGNALS:\n" 1181 " void g2();\n" 1182 "};"); 1183 1184 // Don't interpret 'signals' the wrong way. 1185 verifyFormat("signals.set();"); 1186 verifyFormat("for (Signals signals : f()) {\n}"); 1187 verifyFormat("{\n" 1188 " signals.set(); // This needs indentation.\n" 1189 "}"); 1190 verifyFormat("void f() {\n" 1191 "label:\n" 1192 " signals.baz();\n" 1193 "}"); 1194 } 1195 1196 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1197 EXPECT_EQ("class A {\n" 1198 "public:\n" 1199 " void f();\n" 1200 "\n" 1201 "private:\n" 1202 " void g() {}\n" 1203 " // test\n" 1204 "protected:\n" 1205 " int h;\n" 1206 "};", 1207 format("class A {\n" 1208 "public:\n" 1209 "void f();\n" 1210 "private:\n" 1211 "void g() {}\n" 1212 "// test\n" 1213 "protected:\n" 1214 "int h;\n" 1215 "};")); 1216 EXPECT_EQ("class A {\n" 1217 "protected:\n" 1218 "public:\n" 1219 " void f();\n" 1220 "};", 1221 format("class A {\n" 1222 "protected:\n" 1223 "\n" 1224 "public:\n" 1225 "\n" 1226 " void f();\n" 1227 "};")); 1228 1229 // Even ensure proper spacing inside macros. 1230 EXPECT_EQ("#define B \\\n" 1231 " class A { \\\n" 1232 " protected: \\\n" 1233 " public: \\\n" 1234 " void f(); \\\n" 1235 " };", 1236 format("#define B \\\n" 1237 " class A { \\\n" 1238 " protected: \\\n" 1239 " \\\n" 1240 " public: \\\n" 1241 " \\\n" 1242 " void f(); \\\n" 1243 " };", 1244 getGoogleStyle())); 1245 // But don't remove empty lines after macros ending in access specifiers. 1246 EXPECT_EQ("#define A private:\n" 1247 "\n" 1248 "int i;", 1249 format("#define A private:\n" 1250 "\n" 1251 "int i;")); 1252 } 1253 1254 TEST_F(FormatTest, FormatsClasses) { 1255 verifyFormat("class A : public B {};"); 1256 verifyFormat("class A : public ::B {};"); 1257 1258 verifyFormat( 1259 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1260 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1261 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1262 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1263 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1264 verifyFormat( 1265 "class A : public B, public C, public D, public E, public F {};"); 1266 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1267 " public C,\n" 1268 " public D,\n" 1269 " public E,\n" 1270 " public F,\n" 1271 " public G {};"); 1272 1273 verifyFormat("class\n" 1274 " ReallyReallyLongClassName {\n" 1275 " int i;\n" 1276 "};", 1277 getLLVMStyleWithColumns(32)); 1278 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1279 " aaaaaaaaaaaaaaaa> {};"); 1280 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1281 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1282 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1283 verifyFormat("template <class R, class C>\n" 1284 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1285 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1286 verifyFormat("class ::A::B {};"); 1287 } 1288 1289 TEST_F(FormatTest, BreakBeforeInheritanceComma) { 1290 FormatStyle StyleWithInheritanceBreak = getLLVMStyle(); 1291 StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true; 1292 1293 verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak); 1294 verifyFormat("class MyClass\n" 1295 " : public X\n" 1296 " , public Y {};", 1297 StyleWithInheritanceBreak); 1298 } 1299 1300 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1301 verifyFormat("class A {\n} a, b;"); 1302 verifyFormat("struct A {\n} a, b;"); 1303 verifyFormat("union A {\n} a;"); 1304 } 1305 1306 TEST_F(FormatTest, FormatsEnum) { 1307 verifyFormat("enum {\n" 1308 " Zero,\n" 1309 " One = 1,\n" 1310 " Two = One + 1,\n" 1311 " Three = (One + Two),\n" 1312 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1313 " Five = (One, Two, Three, Four, 5)\n" 1314 "};"); 1315 verifyGoogleFormat("enum {\n" 1316 " Zero,\n" 1317 " One = 1,\n" 1318 " Two = One + 1,\n" 1319 " Three = (One + Two),\n" 1320 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1321 " Five = (One, Two, Three, Four, 5)\n" 1322 "};"); 1323 verifyFormat("enum Enum {};"); 1324 verifyFormat("enum {};"); 1325 verifyFormat("enum X E {} d;"); 1326 verifyFormat("enum __attribute__((...)) E {} d;"); 1327 verifyFormat("enum __declspec__((...)) E {} d;"); 1328 verifyFormat("enum {\n" 1329 " Bar = Foo<int, int>::value\n" 1330 "};", 1331 getLLVMStyleWithColumns(30)); 1332 1333 verifyFormat("enum ShortEnum { A, B, C };"); 1334 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1335 1336 EXPECT_EQ("enum KeepEmptyLines {\n" 1337 " ONE,\n" 1338 "\n" 1339 " TWO,\n" 1340 "\n" 1341 " THREE\n" 1342 "}", 1343 format("enum KeepEmptyLines {\n" 1344 " ONE,\n" 1345 "\n" 1346 " TWO,\n" 1347 "\n" 1348 "\n" 1349 " THREE\n" 1350 "}")); 1351 verifyFormat("enum E { // comment\n" 1352 " ONE,\n" 1353 " TWO\n" 1354 "};\n" 1355 "int i;"); 1356 // Not enums. 1357 verifyFormat("enum X f() {\n" 1358 " a();\n" 1359 " return 42;\n" 1360 "}"); 1361 verifyFormat("enum X Type::f() {\n" 1362 " a();\n" 1363 " return 42;\n" 1364 "}"); 1365 verifyFormat("enum ::X f() {\n" 1366 " a();\n" 1367 " return 42;\n" 1368 "}"); 1369 verifyFormat("enum ns::X f() {\n" 1370 " a();\n" 1371 " return 42;\n" 1372 "}"); 1373 } 1374 1375 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1376 verifyFormat("enum Type {\n" 1377 " One = 0; // These semicolons should be commas.\n" 1378 " Two = 1;\n" 1379 "};"); 1380 verifyFormat("namespace n {\n" 1381 "enum Type {\n" 1382 " One,\n" 1383 " Two, // missing };\n" 1384 " int i;\n" 1385 "}\n" 1386 "void g() {}"); 1387 } 1388 1389 TEST_F(FormatTest, FormatsEnumStruct) { 1390 verifyFormat("enum struct {\n" 1391 " Zero,\n" 1392 " One = 1,\n" 1393 " Two = One + 1,\n" 1394 " Three = (One + Two),\n" 1395 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1396 " Five = (One, Two, Three, Four, 5)\n" 1397 "};"); 1398 verifyFormat("enum struct Enum {};"); 1399 verifyFormat("enum struct {};"); 1400 verifyFormat("enum struct X E {} d;"); 1401 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1402 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1403 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1404 } 1405 1406 TEST_F(FormatTest, FormatsEnumClass) { 1407 verifyFormat("enum class {\n" 1408 " Zero,\n" 1409 " One = 1,\n" 1410 " Two = One + 1,\n" 1411 " Three = (One + Two),\n" 1412 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1413 " Five = (One, Two, Three, Four, 5)\n" 1414 "};"); 1415 verifyFormat("enum class Enum {};"); 1416 verifyFormat("enum class {};"); 1417 verifyFormat("enum class X E {} d;"); 1418 verifyFormat("enum class __attribute__((...)) E {} d;"); 1419 verifyFormat("enum class __declspec__((...)) E {} d;"); 1420 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1421 } 1422 1423 TEST_F(FormatTest, FormatsEnumTypes) { 1424 verifyFormat("enum X : int {\n" 1425 " A, // Force multiple lines.\n" 1426 " B\n" 1427 "};"); 1428 verifyFormat("enum X : int { A, B };"); 1429 verifyFormat("enum X : std::uint32_t { A, B };"); 1430 } 1431 1432 TEST_F(FormatTest, FormatsTypedefEnum) { 1433 FormatStyle Style = getLLVMStyle(); 1434 Style.ColumnLimit = 40; 1435 verifyFormat("typedef enum {} EmptyEnum;"); 1436 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1437 verifyFormat("typedef enum {\n" 1438 " ZERO = 0,\n" 1439 " ONE = 1,\n" 1440 " TWO = 2,\n" 1441 " THREE = 3\n" 1442 "} LongEnum;", 1443 Style); 1444 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1445 Style.BraceWrapping.AfterEnum = true; 1446 verifyFormat("typedef enum {} EmptyEnum;"); 1447 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 1448 verifyFormat("typedef enum\n" 1449 "{\n" 1450 " ZERO = 0,\n" 1451 " ONE = 1,\n" 1452 " TWO = 2,\n" 1453 " THREE = 3\n" 1454 "} LongEnum;", 1455 Style); 1456 } 1457 1458 TEST_F(FormatTest, FormatsNSEnums) { 1459 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1460 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1461 " // Information about someDecentlyLongValue.\n" 1462 " someDecentlyLongValue,\n" 1463 " // Information about anotherDecentlyLongValue.\n" 1464 " anotherDecentlyLongValue,\n" 1465 " // Information about aThirdDecentlyLongValue.\n" 1466 " aThirdDecentlyLongValue\n" 1467 "};"); 1468 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1469 " a = 1,\n" 1470 " b = 2,\n" 1471 " c = 3,\n" 1472 "};"); 1473 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1474 " a = 1,\n" 1475 " b = 2,\n" 1476 " c = 3,\n" 1477 "};"); 1478 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1479 " a = 1,\n" 1480 " b = 2,\n" 1481 " c = 3,\n" 1482 "};"); 1483 } 1484 1485 TEST_F(FormatTest, FormatsBitfields) { 1486 verifyFormat("struct Bitfields {\n" 1487 " unsigned sClass : 8;\n" 1488 " unsigned ValueKind : 2;\n" 1489 "};"); 1490 verifyFormat("struct A {\n" 1491 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1492 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1493 "};"); 1494 verifyFormat("struct MyStruct {\n" 1495 " uchar data;\n" 1496 " uchar : 8;\n" 1497 " uchar : 8;\n" 1498 " uchar other;\n" 1499 "};"); 1500 } 1501 1502 TEST_F(FormatTest, FormatsNamespaces) { 1503 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1504 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1505 1506 verifyFormat("namespace some_namespace {\n" 1507 "class A {};\n" 1508 "void f() { f(); }\n" 1509 "}", 1510 LLVMWithNoNamespaceFix); 1511 verifyFormat("namespace {\n" 1512 "class A {};\n" 1513 "void f() { f(); }\n" 1514 "}", 1515 LLVMWithNoNamespaceFix); 1516 verifyFormat("inline namespace X {\n" 1517 "class A {};\n" 1518 "void f() { f(); }\n" 1519 "}", 1520 LLVMWithNoNamespaceFix); 1521 verifyFormat("using namespace some_namespace;\n" 1522 "class A {};\n" 1523 "void f() { f(); }", 1524 LLVMWithNoNamespaceFix); 1525 1526 // This code is more common than we thought; if we 1527 // layout this correctly the semicolon will go into 1528 // its own line, which is undesirable. 1529 verifyFormat("namespace {};", 1530 LLVMWithNoNamespaceFix); 1531 verifyFormat("namespace {\n" 1532 "class A {};\n" 1533 "};", 1534 LLVMWithNoNamespaceFix); 1535 1536 verifyFormat("namespace {\n" 1537 "int SomeVariable = 0; // comment\n" 1538 "} // namespace", 1539 LLVMWithNoNamespaceFix); 1540 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1541 "#define HEADER_GUARD\n" 1542 "namespace my_namespace {\n" 1543 "int i;\n" 1544 "} // my_namespace\n" 1545 "#endif // HEADER_GUARD", 1546 format("#ifndef HEADER_GUARD\n" 1547 " #define HEADER_GUARD\n" 1548 " namespace my_namespace {\n" 1549 "int i;\n" 1550 "} // my_namespace\n" 1551 "#endif // HEADER_GUARD", 1552 LLVMWithNoNamespaceFix)); 1553 1554 EXPECT_EQ("namespace A::B {\n" 1555 "class C {};\n" 1556 "}", 1557 format("namespace A::B {\n" 1558 "class C {};\n" 1559 "}", 1560 LLVMWithNoNamespaceFix)); 1561 1562 FormatStyle Style = getLLVMStyle(); 1563 Style.NamespaceIndentation = FormatStyle::NI_All; 1564 EXPECT_EQ("namespace out {\n" 1565 " int i;\n" 1566 " namespace in {\n" 1567 " int i;\n" 1568 " } // namespace in\n" 1569 "} // namespace out", 1570 format("namespace out {\n" 1571 "int i;\n" 1572 "namespace in {\n" 1573 "int i;\n" 1574 "} // namespace in\n" 1575 "} // namespace out", 1576 Style)); 1577 1578 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1579 EXPECT_EQ("namespace out {\n" 1580 "int i;\n" 1581 "namespace in {\n" 1582 " int i;\n" 1583 "} // namespace in\n" 1584 "} // namespace out", 1585 format("namespace out {\n" 1586 "int i;\n" 1587 "namespace in {\n" 1588 "int i;\n" 1589 "} // namespace in\n" 1590 "} // namespace out", 1591 Style)); 1592 } 1593 1594 TEST_F(FormatTest, FormatsCompactNamespaces) { 1595 FormatStyle Style = getLLVMStyle(); 1596 Style.CompactNamespaces = true; 1597 1598 verifyFormat("namespace A { namespace B {\n" 1599 "}} // namespace A::B", 1600 Style); 1601 1602 EXPECT_EQ("namespace out { namespace in {\n" 1603 "}} // namespace out::in", 1604 format("namespace out {\n" 1605 "namespace in {\n" 1606 "} // namespace in\n" 1607 "} // namespace out", 1608 Style)); 1609 1610 // Only namespaces which have both consecutive opening and end get compacted 1611 EXPECT_EQ("namespace out {\n" 1612 "namespace in1 {\n" 1613 "} // namespace in1\n" 1614 "namespace in2 {\n" 1615 "} // namespace in2\n" 1616 "} // namespace out", 1617 format("namespace out {\n" 1618 "namespace in1 {\n" 1619 "} // namespace in1\n" 1620 "namespace in2 {\n" 1621 "} // namespace in2\n" 1622 "} // namespace out", 1623 Style)); 1624 1625 EXPECT_EQ("namespace out {\n" 1626 "int i;\n" 1627 "namespace in {\n" 1628 "int j;\n" 1629 "} // namespace in\n" 1630 "int k;\n" 1631 "} // namespace out", 1632 format("namespace out { int i;\n" 1633 "namespace in { int j; } // namespace in\n" 1634 "int k; } // namespace out", 1635 Style)); 1636 1637 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 1638 "}}} // namespace A::B::C\n", 1639 format("namespace A { namespace B {\n" 1640 "namespace C {\n" 1641 "}} // namespace B::C\n" 1642 "} // namespace A\n", 1643 Style)); 1644 1645 Style.ColumnLimit = 40; 1646 EXPECT_EQ("namespace aaaaaaaaaa {\n" 1647 "namespace bbbbbbbbbb {\n" 1648 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 1649 format("namespace aaaaaaaaaa {\n" 1650 "namespace bbbbbbbbbb {\n" 1651 "} // namespace bbbbbbbbbb\n" 1652 "} // namespace aaaaaaaaaa", 1653 Style)); 1654 1655 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 1656 "namespace cccccc {\n" 1657 "}}} // namespace aaaaaa::bbbbbb::cccccc", 1658 format("namespace aaaaaa {\n" 1659 "namespace bbbbbb {\n" 1660 "namespace cccccc {\n" 1661 "} // namespace cccccc\n" 1662 "} // namespace bbbbbb\n" 1663 "} // namespace aaaaaa", 1664 Style)); 1665 Style.ColumnLimit = 80; 1666 1667 // Extra semicolon after 'inner' closing brace prevents merging 1668 EXPECT_EQ("namespace out { namespace in {\n" 1669 "}; } // namespace out::in", 1670 format("namespace out {\n" 1671 "namespace in {\n" 1672 "}; // namespace in\n" 1673 "} // namespace out", 1674 Style)); 1675 1676 // Extra semicolon after 'outer' closing brace is conserved 1677 EXPECT_EQ("namespace out { namespace in {\n" 1678 "}}; // namespace out::in", 1679 format("namespace out {\n" 1680 "namespace in {\n" 1681 "} // namespace in\n" 1682 "}; // namespace out", 1683 Style)); 1684 1685 Style.NamespaceIndentation = FormatStyle::NI_All; 1686 EXPECT_EQ("namespace out { namespace in {\n" 1687 " int i;\n" 1688 "}} // namespace out::in", 1689 format("namespace out {\n" 1690 "namespace in {\n" 1691 "int i;\n" 1692 "} // namespace in\n" 1693 "} // namespace out", 1694 Style)); 1695 EXPECT_EQ("namespace out { namespace mid {\n" 1696 " namespace in {\n" 1697 " int j;\n" 1698 " } // namespace in\n" 1699 " int k;\n" 1700 "}} // namespace out::mid", 1701 format("namespace out { namespace mid {\n" 1702 "namespace in { int j; } // namespace in\n" 1703 "int k; }} // namespace out::mid", 1704 Style)); 1705 1706 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1707 EXPECT_EQ("namespace out { namespace in {\n" 1708 " int i;\n" 1709 "}} // namespace out::in", 1710 format("namespace out {\n" 1711 "namespace in {\n" 1712 "int i;\n" 1713 "} // namespace in\n" 1714 "} // namespace out", 1715 Style)); 1716 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 1717 " int i;\n" 1718 "}}} // namespace out::mid::in", 1719 format("namespace out {\n" 1720 "namespace mid {\n" 1721 "namespace in {\n" 1722 "int i;\n" 1723 "} // namespace in\n" 1724 "} // namespace mid\n" 1725 "} // namespace out", 1726 Style)); 1727 } 1728 1729 TEST_F(FormatTest, FormatsExternC) { 1730 verifyFormat("extern \"C\" {\nint a;"); 1731 verifyFormat("extern \"C\" {}"); 1732 verifyFormat("extern \"C\" {\n" 1733 "int foo();\n" 1734 "}"); 1735 verifyFormat("extern \"C\" int foo() {}"); 1736 verifyFormat("extern \"C\" int foo();"); 1737 verifyFormat("extern \"C\" int foo() {\n" 1738 " int i = 42;\n" 1739 " return i;\n" 1740 "}"); 1741 1742 FormatStyle Style = getLLVMStyle(); 1743 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1744 Style.BraceWrapping.AfterFunction = true; 1745 verifyFormat("extern \"C\" int foo() {}", Style); 1746 verifyFormat("extern \"C\" int foo();", Style); 1747 verifyFormat("extern \"C\" int foo()\n" 1748 "{\n" 1749 " int i = 42;\n" 1750 " return i;\n" 1751 "}", 1752 Style); 1753 1754 Style.BraceWrapping.AfterExternBlock = true; 1755 Style.BraceWrapping.SplitEmptyRecord = false; 1756 verifyFormat("extern \"C\"\n" 1757 "{}", 1758 Style); 1759 verifyFormat("extern \"C\"\n" 1760 "{\n" 1761 " int foo();\n" 1762 "}", 1763 Style); 1764 } 1765 1766 TEST_F(FormatTest, FormatsInlineASM) { 1767 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 1768 verifyFormat("asm(\"nop\" ::: \"memory\");"); 1769 verifyFormat( 1770 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 1771 " \"cpuid\\n\\t\"\n" 1772 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 1773 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 1774 " : \"a\"(value));"); 1775 EXPECT_EQ( 1776 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1777 " __asm {\n" 1778 " mov edx,[that] // vtable in edx\n" 1779 " mov eax,methodIndex\n" 1780 " call [edx][eax*4] // stdcall\n" 1781 " }\n" 1782 "}", 1783 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1784 " __asm {\n" 1785 " mov edx,[that] // vtable in edx\n" 1786 " mov eax,methodIndex\n" 1787 " call [edx][eax*4] // stdcall\n" 1788 " }\n" 1789 "}")); 1790 EXPECT_EQ("_asm {\n" 1791 " xor eax, eax;\n" 1792 " cpuid;\n" 1793 "}", 1794 format("_asm {\n" 1795 " xor eax, eax;\n" 1796 " cpuid;\n" 1797 "}")); 1798 verifyFormat("void function() {\n" 1799 " // comment\n" 1800 " asm(\"\");\n" 1801 "}"); 1802 EXPECT_EQ("__asm {\n" 1803 "}\n" 1804 "int i;", 1805 format("__asm {\n" 1806 "}\n" 1807 "int i;")); 1808 } 1809 1810 TEST_F(FormatTest, FormatTryCatch) { 1811 verifyFormat("try {\n" 1812 " throw a * b;\n" 1813 "} catch (int a) {\n" 1814 " // Do nothing.\n" 1815 "} catch (...) {\n" 1816 " exit(42);\n" 1817 "}"); 1818 1819 // Function-level try statements. 1820 verifyFormat("int f() try { return 4; } catch (...) {\n" 1821 " return 5;\n" 1822 "}"); 1823 verifyFormat("class A {\n" 1824 " int a;\n" 1825 " A() try : a(0) {\n" 1826 " } catch (...) {\n" 1827 " throw;\n" 1828 " }\n" 1829 "};\n"); 1830 1831 // Incomplete try-catch blocks. 1832 verifyIncompleteFormat("try {} catch ("); 1833 } 1834 1835 TEST_F(FormatTest, FormatSEHTryCatch) { 1836 verifyFormat("__try {\n" 1837 " int a = b * c;\n" 1838 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 1839 " // Do nothing.\n" 1840 "}"); 1841 1842 verifyFormat("__try {\n" 1843 " int a = b * c;\n" 1844 "} __finally {\n" 1845 " // Do nothing.\n" 1846 "}"); 1847 1848 verifyFormat("DEBUG({\n" 1849 " __try {\n" 1850 " } __finally {\n" 1851 " }\n" 1852 "});\n"); 1853 } 1854 1855 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 1856 verifyFormat("try {\n" 1857 " f();\n" 1858 "} catch {\n" 1859 " g();\n" 1860 "}"); 1861 verifyFormat("try {\n" 1862 " f();\n" 1863 "} catch (A a) MACRO(x) {\n" 1864 " g();\n" 1865 "} catch (B b) MACRO(x) {\n" 1866 " g();\n" 1867 "}"); 1868 } 1869 1870 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 1871 FormatStyle Style = getLLVMStyle(); 1872 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 1873 FormatStyle::BS_WebKit}) { 1874 Style.BreakBeforeBraces = BraceStyle; 1875 verifyFormat("try {\n" 1876 " // something\n" 1877 "} catch (...) {\n" 1878 " // something\n" 1879 "}", 1880 Style); 1881 } 1882 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 1883 verifyFormat("try {\n" 1884 " // something\n" 1885 "}\n" 1886 "catch (...) {\n" 1887 " // something\n" 1888 "}", 1889 Style); 1890 verifyFormat("__try {\n" 1891 " // something\n" 1892 "}\n" 1893 "__finally {\n" 1894 " // something\n" 1895 "}", 1896 Style); 1897 verifyFormat("@try {\n" 1898 " // something\n" 1899 "}\n" 1900 "@finally {\n" 1901 " // something\n" 1902 "}", 1903 Style); 1904 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1905 verifyFormat("try\n" 1906 "{\n" 1907 " // something\n" 1908 "}\n" 1909 "catch (...)\n" 1910 "{\n" 1911 " // something\n" 1912 "}", 1913 Style); 1914 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 1915 verifyFormat("try\n" 1916 " {\n" 1917 " // something\n" 1918 " }\n" 1919 "catch (...)\n" 1920 " {\n" 1921 " // something\n" 1922 " }", 1923 Style); 1924 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1925 Style.BraceWrapping.BeforeCatch = true; 1926 verifyFormat("try {\n" 1927 " // something\n" 1928 "}\n" 1929 "catch (...) {\n" 1930 " // something\n" 1931 "}", 1932 Style); 1933 } 1934 1935 TEST_F(FormatTest, StaticInitializers) { 1936 verifyFormat("static SomeClass SC = {1, 'a'};"); 1937 1938 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 1939 " 100000000, " 1940 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 1941 1942 // Here, everything other than the "}" would fit on a line. 1943 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 1944 " 10000000000000000000000000};"); 1945 EXPECT_EQ("S s = {a,\n" 1946 "\n" 1947 " b};", 1948 format("S s = {\n" 1949 " a,\n" 1950 "\n" 1951 " b\n" 1952 "};")); 1953 1954 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 1955 // line. However, the formatting looks a bit off and this probably doesn't 1956 // happen often in practice. 1957 verifyFormat("static int Variable[1] = {\n" 1958 " {1000000000000000000000000000000000000}};", 1959 getLLVMStyleWithColumns(40)); 1960 } 1961 1962 TEST_F(FormatTest, DesignatedInitializers) { 1963 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 1964 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 1965 " .bbbbbbbbbb = 2,\n" 1966 " .cccccccccc = 3,\n" 1967 " .dddddddddd = 4,\n" 1968 " .eeeeeeeeee = 5};"); 1969 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1970 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 1971 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 1972 " .ccccccccccccccccccccccccccc = 3,\n" 1973 " .ddddddddddddddddddddddddddd = 4,\n" 1974 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 1975 1976 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 1977 1978 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 1979 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 1980 " [2] = bbbbbbbbbb,\n" 1981 " [3] = cccccccccc,\n" 1982 " [4] = dddddddddd,\n" 1983 " [5] = eeeeeeeeee};"); 1984 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1985 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1986 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 1987 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 1988 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 1989 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 1990 } 1991 1992 TEST_F(FormatTest, NestedStaticInitializers) { 1993 verifyFormat("static A x = {{{}}};\n"); 1994 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 1995 " {init1, init2, init3, init4}}};", 1996 getLLVMStyleWithColumns(50)); 1997 1998 verifyFormat("somes Status::global_reps[3] = {\n" 1999 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2000 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2001 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2002 getLLVMStyleWithColumns(60)); 2003 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2004 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2005 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2006 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2007 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2008 " {rect.fRight - rect.fLeft, rect.fBottom - " 2009 "rect.fTop}};"); 2010 2011 verifyFormat( 2012 "SomeArrayOfSomeType a = {\n" 2013 " {{1, 2, 3},\n" 2014 " {1, 2, 3},\n" 2015 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2016 " 333333333333333333333333333333},\n" 2017 " {1, 2, 3},\n" 2018 " {1, 2, 3}}};"); 2019 verifyFormat( 2020 "SomeArrayOfSomeType a = {\n" 2021 " {{1, 2, 3}},\n" 2022 " {{1, 2, 3}},\n" 2023 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2024 " 333333333333333333333333333333}},\n" 2025 " {{1, 2, 3}},\n" 2026 " {{1, 2, 3}}};"); 2027 2028 verifyFormat("struct {\n" 2029 " unsigned bit;\n" 2030 " const char *const name;\n" 2031 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2032 " {kOsWin, \"Windows\"},\n" 2033 " {kOsLinux, \"Linux\"},\n" 2034 " {kOsCrOS, \"Chrome OS\"}};"); 2035 verifyFormat("struct {\n" 2036 " unsigned bit;\n" 2037 " const char *const name;\n" 2038 "} kBitsToOs[] = {\n" 2039 " {kOsMac, \"Mac\"},\n" 2040 " {kOsWin, \"Windows\"},\n" 2041 " {kOsLinux, \"Linux\"},\n" 2042 " {kOsCrOS, \"Chrome OS\"},\n" 2043 "};"); 2044 } 2045 2046 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2047 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2048 " \\\n" 2049 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2050 } 2051 2052 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2053 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2054 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2055 2056 // Do break defaulted and deleted functions. 2057 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2058 " default;", 2059 getLLVMStyleWithColumns(40)); 2060 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2061 " delete;", 2062 getLLVMStyleWithColumns(40)); 2063 } 2064 2065 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2066 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2067 getLLVMStyleWithColumns(40)); 2068 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2069 getLLVMStyleWithColumns(40)); 2070 EXPECT_EQ("#define Q \\\n" 2071 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2072 " \"aaaaaaaa.cpp\"", 2073 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2074 getLLVMStyleWithColumns(40))); 2075 } 2076 2077 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2078 EXPECT_EQ("# 123 \"A string literal\"", 2079 format(" # 123 \"A string literal\"")); 2080 } 2081 2082 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2083 EXPECT_EQ("#;", format("#;")); 2084 verifyFormat("#\n;\n;\n;"); 2085 } 2086 2087 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2088 EXPECT_EQ("#line 42 \"test\"\n", 2089 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2090 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2091 getLLVMStyleWithColumns(12))); 2092 } 2093 2094 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2095 EXPECT_EQ("#line 42 \"test\"", 2096 format("# \\\n line \\\n 42 \\\n \"test\"")); 2097 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2098 } 2099 2100 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2101 verifyFormat("#define A \\x20"); 2102 verifyFormat("#define A \\ x20"); 2103 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2104 verifyFormat("#define A ''"); 2105 verifyFormat("#define A ''qqq"); 2106 verifyFormat("#define A `qqq"); 2107 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2108 EXPECT_EQ("const char *c = STRINGIFY(\n" 2109 "\\na : b);", 2110 format("const char * c = STRINGIFY(\n" 2111 "\\na : b);")); 2112 2113 verifyFormat("a\r\\"); 2114 verifyFormat("a\v\\"); 2115 verifyFormat("a\f\\"); 2116 } 2117 2118 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2119 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2120 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2121 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2122 // FIXME: We never break before the macro name. 2123 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2124 2125 verifyFormat("#define A A\n#define A A"); 2126 verifyFormat("#define A(X) A\n#define A A"); 2127 2128 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2129 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2130 } 2131 2132 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2133 EXPECT_EQ("// somecomment\n" 2134 "#include \"a.h\"\n" 2135 "#define A( \\\n" 2136 " A, B)\n" 2137 "#include \"b.h\"\n" 2138 "// somecomment\n", 2139 format(" // somecomment\n" 2140 " #include \"a.h\"\n" 2141 "#define A(A,\\\n" 2142 " B)\n" 2143 " #include \"b.h\"\n" 2144 " // somecomment\n", 2145 getLLVMStyleWithColumns(13))); 2146 } 2147 2148 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2149 2150 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2151 EXPECT_EQ("#define A \\\n" 2152 " c; \\\n" 2153 " e;\n" 2154 "f;", 2155 format("#define A c; e;\n" 2156 "f;", 2157 getLLVMStyleWithColumns(14))); 2158 } 2159 2160 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2161 2162 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2163 EXPECT_EQ("int x,\n" 2164 "#define A\n" 2165 " y;", 2166 format("int x,\n#define A\ny;")); 2167 } 2168 2169 TEST_F(FormatTest, HashInMacroDefinition) { 2170 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2171 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2172 verifyFormat("#define A \\\n" 2173 " { \\\n" 2174 " f(#c); \\\n" 2175 " }", 2176 getLLVMStyleWithColumns(11)); 2177 2178 verifyFormat("#define A(X) \\\n" 2179 " void function##X()", 2180 getLLVMStyleWithColumns(22)); 2181 2182 verifyFormat("#define A(a, b, c) \\\n" 2183 " void a##b##c()", 2184 getLLVMStyleWithColumns(22)); 2185 2186 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2187 } 2188 2189 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2190 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2191 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2192 } 2193 2194 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2195 EXPECT_EQ("#define A b;", format("#define A \\\n" 2196 " \\\n" 2197 " b;", 2198 getLLVMStyleWithColumns(25))); 2199 EXPECT_EQ("#define A \\\n" 2200 " \\\n" 2201 " a; \\\n" 2202 " b;", 2203 format("#define A \\\n" 2204 " \\\n" 2205 " a; \\\n" 2206 " b;", 2207 getLLVMStyleWithColumns(11))); 2208 EXPECT_EQ("#define A \\\n" 2209 " a; \\\n" 2210 " \\\n" 2211 " b;", 2212 format("#define A \\\n" 2213 " a; \\\n" 2214 " \\\n" 2215 " b;", 2216 getLLVMStyleWithColumns(11))); 2217 } 2218 2219 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2220 verifyIncompleteFormat("#define A :"); 2221 verifyFormat("#define SOMECASES \\\n" 2222 " case 1: \\\n" 2223 " case 2\n", 2224 getLLVMStyleWithColumns(20)); 2225 verifyFormat("#define MACRO(a) \\\n" 2226 " if (a) \\\n" 2227 " f(); \\\n" 2228 " else \\\n" 2229 " g()", 2230 getLLVMStyleWithColumns(18)); 2231 verifyFormat("#define A template <typename T>"); 2232 verifyIncompleteFormat("#define STR(x) #x\n" 2233 "f(STR(this_is_a_string_literal{));"); 2234 verifyFormat("#pragma omp threadprivate( \\\n" 2235 " y)), // expected-warning", 2236 getLLVMStyleWithColumns(28)); 2237 verifyFormat("#d, = };"); 2238 verifyFormat("#if \"a"); 2239 verifyIncompleteFormat("({\n" 2240 "#define b \\\n" 2241 " } \\\n" 2242 " a\n" 2243 "a", 2244 getLLVMStyleWithColumns(15)); 2245 verifyFormat("#define A \\\n" 2246 " { \\\n" 2247 " {\n" 2248 "#define B \\\n" 2249 " } \\\n" 2250 " }", 2251 getLLVMStyleWithColumns(15)); 2252 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2253 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2254 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2255 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2256 } 2257 2258 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2259 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2260 EXPECT_EQ("class A : public QObject {\n" 2261 " Q_OBJECT\n" 2262 "\n" 2263 " A() {}\n" 2264 "};", 2265 format("class A : public QObject {\n" 2266 " Q_OBJECT\n" 2267 "\n" 2268 " A() {\n}\n" 2269 "} ;")); 2270 EXPECT_EQ("MACRO\n" 2271 "/*static*/ int i;", 2272 format("MACRO\n" 2273 " /*static*/ int i;")); 2274 EXPECT_EQ("SOME_MACRO\n" 2275 "namespace {\n" 2276 "void f();\n" 2277 "} // namespace", 2278 format("SOME_MACRO\n" 2279 " namespace {\n" 2280 "void f( );\n" 2281 "} // namespace")); 2282 // Only if the identifier contains at least 5 characters. 2283 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2284 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2285 // Only if everything is upper case. 2286 EXPECT_EQ("class A : public QObject {\n" 2287 " Q_Object A() {}\n" 2288 "};", 2289 format("class A : public QObject {\n" 2290 " Q_Object\n" 2291 " A() {\n}\n" 2292 "} ;")); 2293 2294 // Only if the next line can actually start an unwrapped line. 2295 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2296 format("SOME_WEIRD_LOG_MACRO\n" 2297 "<< SomeThing;")); 2298 2299 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2300 "(n, buffers))\n", 2301 getChromiumStyle(FormatStyle::LK_Cpp)); 2302 } 2303 2304 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2305 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2306 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2307 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2308 "class X {};\n" 2309 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2310 "int *createScopDetectionPass() { return 0; }", 2311 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2312 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2313 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2314 " class X {};\n" 2315 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2316 " int *createScopDetectionPass() { return 0; }")); 2317 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2318 // braces, so that inner block is indented one level more. 2319 EXPECT_EQ("int q() {\n" 2320 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2321 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2322 " IPC_END_MESSAGE_MAP()\n" 2323 "}", 2324 format("int q() {\n" 2325 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2326 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2327 " IPC_END_MESSAGE_MAP()\n" 2328 "}")); 2329 2330 // Same inside macros. 2331 EXPECT_EQ("#define LIST(L) \\\n" 2332 " L(A) \\\n" 2333 " L(B) \\\n" 2334 " L(C)", 2335 format("#define LIST(L) \\\n" 2336 " L(A) \\\n" 2337 " L(B) \\\n" 2338 " L(C)", 2339 getGoogleStyle())); 2340 2341 // These must not be recognized as macros. 2342 EXPECT_EQ("int q() {\n" 2343 " f(x);\n" 2344 " f(x) {}\n" 2345 " f(x)->g();\n" 2346 " f(x)->*g();\n" 2347 " f(x).g();\n" 2348 " f(x) = x;\n" 2349 " f(x) += x;\n" 2350 " f(x) -= x;\n" 2351 " f(x) *= x;\n" 2352 " f(x) /= x;\n" 2353 " f(x) %= x;\n" 2354 " f(x) &= x;\n" 2355 " f(x) |= x;\n" 2356 " f(x) ^= x;\n" 2357 " f(x) >>= x;\n" 2358 " f(x) <<= x;\n" 2359 " f(x)[y].z();\n" 2360 " LOG(INFO) << x;\n" 2361 " ifstream(x) >> x;\n" 2362 "}\n", 2363 format("int q() {\n" 2364 " f(x)\n;\n" 2365 " f(x)\n {}\n" 2366 " f(x)\n->g();\n" 2367 " f(x)\n->*g();\n" 2368 " f(x)\n.g();\n" 2369 " f(x)\n = x;\n" 2370 " f(x)\n += x;\n" 2371 " f(x)\n -= x;\n" 2372 " f(x)\n *= x;\n" 2373 " f(x)\n /= x;\n" 2374 " f(x)\n %= x;\n" 2375 " f(x)\n &= x;\n" 2376 " f(x)\n |= x;\n" 2377 " f(x)\n ^= x;\n" 2378 " f(x)\n >>= x;\n" 2379 " f(x)\n <<= x;\n" 2380 " f(x)\n[y].z();\n" 2381 " LOG(INFO)\n << x;\n" 2382 " ifstream(x)\n >> x;\n" 2383 "}\n")); 2384 EXPECT_EQ("int q() {\n" 2385 " F(x)\n" 2386 " if (1) {\n" 2387 " }\n" 2388 " F(x)\n" 2389 " while (1) {\n" 2390 " }\n" 2391 " F(x)\n" 2392 " G(x);\n" 2393 " F(x)\n" 2394 " try {\n" 2395 " Q();\n" 2396 " } catch (...) {\n" 2397 " }\n" 2398 "}\n", 2399 format("int q() {\n" 2400 "F(x)\n" 2401 "if (1) {}\n" 2402 "F(x)\n" 2403 "while (1) {}\n" 2404 "F(x)\n" 2405 "G(x);\n" 2406 "F(x)\n" 2407 "try { Q(); } catch (...) {}\n" 2408 "}\n")); 2409 EXPECT_EQ("class A {\n" 2410 " A() : t(0) {}\n" 2411 " A(int i) noexcept() : {}\n" 2412 " A(X x)\n" // FIXME: function-level try blocks are broken. 2413 " try : t(0) {\n" 2414 " } catch (...) {\n" 2415 " }\n" 2416 "};", 2417 format("class A {\n" 2418 " A()\n : t(0) {}\n" 2419 " A(int i)\n noexcept() : {}\n" 2420 " A(X x)\n" 2421 " try : t(0) {} catch (...) {}\n" 2422 "};")); 2423 EXPECT_EQ("class SomeClass {\n" 2424 "public:\n" 2425 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2426 "};", 2427 format("class SomeClass {\n" 2428 "public:\n" 2429 " SomeClass()\n" 2430 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2431 "};")); 2432 EXPECT_EQ("class SomeClass {\n" 2433 "public:\n" 2434 " SomeClass()\n" 2435 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2436 "};", 2437 format("class SomeClass {\n" 2438 "public:\n" 2439 " SomeClass()\n" 2440 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2441 "};", 2442 getLLVMStyleWithColumns(40))); 2443 2444 verifyFormat("MACRO(>)"); 2445 } 2446 2447 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2448 verifyFormat("#define A \\\n" 2449 " f({ \\\n" 2450 " g(); \\\n" 2451 " });", 2452 getLLVMStyleWithColumns(11)); 2453 } 2454 2455 TEST_F(FormatTest, IndentPreprocessorDirectives) { 2456 FormatStyle Style = getLLVMStyle(); 2457 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 2458 Style.ColumnLimit = 40; 2459 verifyFormat("#ifdef _WIN32\n" 2460 "#define A 0\n" 2461 "#ifdef VAR2\n" 2462 "#define B 1\n" 2463 "#include <someheader.h>\n" 2464 "#define MACRO \\\n" 2465 " some_very_long_func_aaaaaaaaaa();\n" 2466 "#endif\n" 2467 "#else\n" 2468 "#define A 1\n" 2469 "#endif", 2470 Style); 2471 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 2472 verifyFormat("#ifdef _WIN32\n" 2473 "# define A 0\n" 2474 "# ifdef VAR2\n" 2475 "# define B 1\n" 2476 "# include <someheader.h>\n" 2477 "# define MACRO \\\n" 2478 " some_very_long_func_aaaaaaaaaa();\n" 2479 "# endif\n" 2480 "#else\n" 2481 "# define A 1\n" 2482 "#endif", 2483 Style); 2484 verifyFormat("#if A\n" 2485 "# define MACRO \\\n" 2486 " void a(int x) { \\\n" 2487 " b(); \\\n" 2488 " c(); \\\n" 2489 " d(); \\\n" 2490 " e(); \\\n" 2491 " f(); \\\n" 2492 " }\n" 2493 "#endif", 2494 Style); 2495 // Comments before include guard. 2496 verifyFormat("// file comment\n" 2497 "// file comment\n" 2498 "#ifndef HEADER_H\n" 2499 "#define HEADER_H\n" 2500 "code();\n" 2501 "#endif", 2502 Style); 2503 // Test with include guards. 2504 verifyFormat("#ifndef HEADER_H\n" 2505 "#define HEADER_H\n" 2506 "code();\n" 2507 "#endif", 2508 Style); 2509 // Include guards must have a #define with the same variable immediately 2510 // after #ifndef. 2511 verifyFormat("#ifndef NOT_GUARD\n" 2512 "# define FOO\n" 2513 "code();\n" 2514 "#endif", 2515 Style); 2516 2517 // Include guards must cover the entire file. 2518 verifyFormat("code();\n" 2519 "code();\n" 2520 "#ifndef NOT_GUARD\n" 2521 "# define NOT_GUARD\n" 2522 "code();\n" 2523 "#endif", 2524 Style); 2525 verifyFormat("#ifndef NOT_GUARD\n" 2526 "# define NOT_GUARD\n" 2527 "code();\n" 2528 "#endif\n" 2529 "code();", 2530 Style); 2531 // Test with trailing blank lines. 2532 verifyFormat("#ifndef HEADER_H\n" 2533 "#define HEADER_H\n" 2534 "code();\n" 2535 "#endif\n", 2536 Style); 2537 // Include guards don't have #else. 2538 verifyFormat("#ifndef NOT_GUARD\n" 2539 "# define NOT_GUARD\n" 2540 "code();\n" 2541 "#else\n" 2542 "#endif", 2543 Style); 2544 verifyFormat("#ifndef NOT_GUARD\n" 2545 "# define NOT_GUARD\n" 2546 "code();\n" 2547 "#elif FOO\n" 2548 "#endif", 2549 Style); 2550 // Non-identifier #define after potential include guard. 2551 verifyFormat("#ifndef FOO\n" 2552 "# define 1\n" 2553 "#endif\n", 2554 Style); 2555 // #if closes past last non-preprocessor line. 2556 verifyFormat("#ifndef FOO\n" 2557 "#define FOO\n" 2558 "#if 1\n" 2559 "int i;\n" 2560 "# define A 0\n" 2561 "#endif\n" 2562 "#endif\n", 2563 Style); 2564 // FIXME: This doesn't handle the case where there's code between the 2565 // #ifndef and #define but all other conditions hold. This is because when 2566 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 2567 // previous code line yet, so we can't detect it. 2568 EXPECT_EQ("#ifndef NOT_GUARD\n" 2569 "code();\n" 2570 "#define NOT_GUARD\n" 2571 "code();\n" 2572 "#endif", 2573 format("#ifndef NOT_GUARD\n" 2574 "code();\n" 2575 "# define NOT_GUARD\n" 2576 "code();\n" 2577 "#endif", 2578 Style)); 2579 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 2580 // be outside an include guard. Examples are #pragma once and 2581 // #pragma GCC diagnostic, or anything else that does not change the meaning 2582 // of the file if it's included multiple times. 2583 EXPECT_EQ("#ifdef WIN32\n" 2584 "# pragma once\n" 2585 "#endif\n" 2586 "#ifndef HEADER_H\n" 2587 "# define HEADER_H\n" 2588 "code();\n" 2589 "#endif", 2590 format("#ifdef WIN32\n" 2591 "# pragma once\n" 2592 "#endif\n" 2593 "#ifndef HEADER_H\n" 2594 "#define HEADER_H\n" 2595 "code();\n" 2596 "#endif", 2597 Style)); 2598 // FIXME: This does not detect when there is a single non-preprocessor line 2599 // in front of an include-guard-like structure where other conditions hold 2600 // because ScopedLineState hides the line. 2601 EXPECT_EQ("code();\n" 2602 "#ifndef HEADER_H\n" 2603 "#define HEADER_H\n" 2604 "code();\n" 2605 "#endif", 2606 format("code();\n" 2607 "#ifndef HEADER_H\n" 2608 "# define HEADER_H\n" 2609 "code();\n" 2610 "#endif", 2611 Style)); 2612 // Keep comments aligned with #, otherwise indent comments normally. These 2613 // tests cannot use verifyFormat because messUp manipulates leading 2614 // whitespace. 2615 { 2616 const char *Expected = "" 2617 "void f() {\n" 2618 "#if 1\n" 2619 "// Preprocessor aligned.\n" 2620 "# define A 0\n" 2621 " // Code. Separated by blank line.\n" 2622 "\n" 2623 "# define B 0\n" 2624 " // Code. Not aligned with #\n" 2625 "# define C 0\n" 2626 "#endif"; 2627 const char *ToFormat = "" 2628 "void f() {\n" 2629 "#if 1\n" 2630 "// Preprocessor aligned.\n" 2631 "# define A 0\n" 2632 "// Code. Separated by blank line.\n" 2633 "\n" 2634 "# define B 0\n" 2635 " // Code. Not aligned with #\n" 2636 "# define C 0\n" 2637 "#endif"; 2638 EXPECT_EQ(Expected, format(ToFormat, Style)); 2639 EXPECT_EQ(Expected, format(Expected, Style)); 2640 } 2641 // Keep block quotes aligned. 2642 { 2643 const char *Expected = "" 2644 "void f() {\n" 2645 "#if 1\n" 2646 "/* Preprocessor aligned. */\n" 2647 "# define A 0\n" 2648 " /* Code. Separated by blank line. */\n" 2649 "\n" 2650 "# define B 0\n" 2651 " /* Code. Not aligned with # */\n" 2652 "# define C 0\n" 2653 "#endif"; 2654 const char *ToFormat = "" 2655 "void f() {\n" 2656 "#if 1\n" 2657 "/* Preprocessor aligned. */\n" 2658 "# define A 0\n" 2659 "/* Code. Separated by blank line. */\n" 2660 "\n" 2661 "# define B 0\n" 2662 " /* Code. Not aligned with # */\n" 2663 "# define C 0\n" 2664 "#endif"; 2665 EXPECT_EQ(Expected, format(ToFormat, Style)); 2666 EXPECT_EQ(Expected, format(Expected, Style)); 2667 } 2668 // Keep comments aligned with un-indented directives. 2669 { 2670 const char *Expected = "" 2671 "void f() {\n" 2672 "// Preprocessor aligned.\n" 2673 "#define A 0\n" 2674 " // Code. Separated by blank line.\n" 2675 "\n" 2676 "#define B 0\n" 2677 " // Code. Not aligned with #\n" 2678 "#define C 0\n"; 2679 const char *ToFormat = "" 2680 "void f() {\n" 2681 "// Preprocessor aligned.\n" 2682 "#define A 0\n" 2683 "// Code. Separated by blank line.\n" 2684 "\n" 2685 "#define B 0\n" 2686 " // Code. Not aligned with #\n" 2687 "#define C 0\n"; 2688 EXPECT_EQ(Expected, format(ToFormat, Style)); 2689 EXPECT_EQ(Expected, format(Expected, Style)); 2690 } 2691 // Test with tabs. 2692 Style.UseTab = FormatStyle::UT_Always; 2693 Style.IndentWidth = 8; 2694 Style.TabWidth = 8; 2695 verifyFormat("#ifdef _WIN32\n" 2696 "#\tdefine A 0\n" 2697 "#\tifdef VAR2\n" 2698 "#\t\tdefine B 1\n" 2699 "#\t\tinclude <someheader.h>\n" 2700 "#\t\tdefine MACRO \\\n" 2701 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 2702 "#\tendif\n" 2703 "#else\n" 2704 "#\tdefine A 1\n" 2705 "#endif", 2706 Style); 2707 2708 // Regression test: Multiline-macro inside include guards. 2709 verifyFormat("#ifndef HEADER_H\n" 2710 "#define HEADER_H\n" 2711 "#define A() \\\n" 2712 " int i; \\\n" 2713 " int j;\n" 2714 "#endif // HEADER_H", 2715 getLLVMStyleWithColumns(20)); 2716 } 2717 2718 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2719 verifyFormat("{\n { a #c; }\n}"); 2720 } 2721 2722 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2723 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2724 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2725 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2726 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2727 } 2728 2729 TEST_F(FormatTest, EscapedNewlines) { 2730 FormatStyle Narrow = getLLVMStyleWithColumns(11); 2731 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 2732 format("#define A \\\nint i;\\\n int j;", Narrow)); 2733 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2734 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2735 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 2736 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2737 2738 FormatStyle AlignLeft = getLLVMStyle(); 2739 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 2740 EXPECT_EQ("#define MACRO(x) \\\n" 2741 "private: \\\n" 2742 " int x(int a);\n", 2743 format("#define MACRO(x) \\\n" 2744 "private: \\\n" 2745 " int x(int a);\n", 2746 AlignLeft)); 2747 2748 // CRLF line endings 2749 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 2750 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 2751 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 2752 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2753 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 2754 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 2755 EXPECT_EQ("#define MACRO(x) \\\r\n" 2756 "private: \\\r\n" 2757 " int x(int a);\r\n", 2758 format("#define MACRO(x) \\\r\n" 2759 "private: \\\r\n" 2760 " int x(int a);\r\n", 2761 AlignLeft)); 2762 2763 FormatStyle DontAlign = getLLVMStyle(); 2764 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 2765 DontAlign.MaxEmptyLinesToKeep = 3; 2766 // FIXME: can't use verifyFormat here because the newline before 2767 // "public:" is not inserted the first time it's reformatted 2768 EXPECT_EQ("#define A \\\n" 2769 " class Foo { \\\n" 2770 " void bar(); \\\n" 2771 "\\\n" 2772 "\\\n" 2773 "\\\n" 2774 " public: \\\n" 2775 " void baz(); \\\n" 2776 " };", 2777 format("#define A \\\n" 2778 " class Foo { \\\n" 2779 " void bar(); \\\n" 2780 "\\\n" 2781 "\\\n" 2782 "\\\n" 2783 " public: \\\n" 2784 " void baz(); \\\n" 2785 " };", 2786 DontAlign)); 2787 } 2788 2789 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 2790 verifyFormat("#define A \\\n" 2791 " int v( \\\n" 2792 " a); \\\n" 2793 " int i;", 2794 getLLVMStyleWithColumns(11)); 2795 } 2796 2797 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 2798 EXPECT_EQ( 2799 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2800 " \\\n" 2801 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2802 "\n" 2803 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2804 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 2805 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2806 "\\\n" 2807 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2808 " \n" 2809 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2810 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 2811 } 2812 2813 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 2814 EXPECT_EQ("int\n" 2815 "#define A\n" 2816 " a;", 2817 format("int\n#define A\na;")); 2818 verifyFormat("functionCallTo(\n" 2819 " someOtherFunction(\n" 2820 " withSomeParameters, whichInSequence,\n" 2821 " areLongerThanALine(andAnotherCall,\n" 2822 "#define A B\n" 2823 " withMoreParamters,\n" 2824 " whichStronglyInfluenceTheLayout),\n" 2825 " andMoreParameters),\n" 2826 " trailing);", 2827 getLLVMStyleWithColumns(69)); 2828 verifyFormat("Foo::Foo()\n" 2829 "#ifdef BAR\n" 2830 " : baz(0)\n" 2831 "#endif\n" 2832 "{\n" 2833 "}"); 2834 verifyFormat("void f() {\n" 2835 " if (true)\n" 2836 "#ifdef A\n" 2837 " f(42);\n" 2838 " x();\n" 2839 "#else\n" 2840 " g();\n" 2841 " x();\n" 2842 "#endif\n" 2843 "}"); 2844 verifyFormat("void f(param1, param2,\n" 2845 " param3,\n" 2846 "#ifdef A\n" 2847 " param4(param5,\n" 2848 "#ifdef A1\n" 2849 " param6,\n" 2850 "#ifdef A2\n" 2851 " param7),\n" 2852 "#else\n" 2853 " param8),\n" 2854 " param9,\n" 2855 "#endif\n" 2856 " param10,\n" 2857 "#endif\n" 2858 " param11)\n" 2859 "#else\n" 2860 " param12)\n" 2861 "#endif\n" 2862 "{\n" 2863 " x();\n" 2864 "}", 2865 getLLVMStyleWithColumns(28)); 2866 verifyFormat("#if 1\n" 2867 "int i;"); 2868 verifyFormat("#if 1\n" 2869 "#endif\n" 2870 "#if 1\n" 2871 "#else\n" 2872 "#endif\n"); 2873 verifyFormat("DEBUG({\n" 2874 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2875 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2876 "});\n" 2877 "#if a\n" 2878 "#else\n" 2879 "#endif"); 2880 2881 verifyIncompleteFormat("void f(\n" 2882 "#if A\n" 2883 ");\n" 2884 "#else\n" 2885 "#endif"); 2886 } 2887 2888 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 2889 verifyFormat("#endif\n" 2890 "#if B"); 2891 } 2892 2893 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 2894 FormatStyle SingleLine = getLLVMStyle(); 2895 SingleLine.AllowShortIfStatementsOnASingleLine = true; 2896 verifyFormat("#if 0\n" 2897 "#elif 1\n" 2898 "#endif\n" 2899 "void foo() {\n" 2900 " if (test) foo2();\n" 2901 "}", 2902 SingleLine); 2903 } 2904 2905 TEST_F(FormatTest, LayoutBlockInsideParens) { 2906 verifyFormat("functionCall({ int i; });"); 2907 verifyFormat("functionCall({\n" 2908 " int i;\n" 2909 " int j;\n" 2910 "});"); 2911 verifyFormat("functionCall(\n" 2912 " {\n" 2913 " int i;\n" 2914 " int j;\n" 2915 " },\n" 2916 " aaaa, bbbb, cccc);"); 2917 verifyFormat("functionA(functionB({\n" 2918 " int i;\n" 2919 " int j;\n" 2920 " }),\n" 2921 " aaaa, bbbb, cccc);"); 2922 verifyFormat("functionCall(\n" 2923 " {\n" 2924 " int i;\n" 2925 " int j;\n" 2926 " },\n" 2927 " aaaa, bbbb, // comment\n" 2928 " cccc);"); 2929 verifyFormat("functionA(functionB({\n" 2930 " int i;\n" 2931 " int j;\n" 2932 " }),\n" 2933 " aaaa, bbbb, // comment\n" 2934 " cccc);"); 2935 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 2936 verifyFormat("functionCall(aaaa, bbbb, {\n" 2937 " int i;\n" 2938 " int j;\n" 2939 "});"); 2940 verifyFormat( 2941 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 2942 " {\n" 2943 " int i; // break\n" 2944 " },\n" 2945 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2946 " ccccccccccccccccc));"); 2947 verifyFormat("DEBUG({\n" 2948 " if (a)\n" 2949 " f();\n" 2950 "});"); 2951 } 2952 2953 TEST_F(FormatTest, LayoutBlockInsideStatement) { 2954 EXPECT_EQ("SOME_MACRO { int i; }\n" 2955 "int i;", 2956 format(" SOME_MACRO {int i;} int i;")); 2957 } 2958 2959 TEST_F(FormatTest, LayoutNestedBlocks) { 2960 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 2961 " struct s {\n" 2962 " int i;\n" 2963 " };\n" 2964 " s kBitsToOs[] = {{10}};\n" 2965 " for (int i = 0; i < 10; ++i)\n" 2966 " return;\n" 2967 "}"); 2968 verifyFormat("call(parameter, {\n" 2969 " something();\n" 2970 " // Comment using all columns.\n" 2971 " somethingelse();\n" 2972 "});", 2973 getLLVMStyleWithColumns(40)); 2974 verifyFormat("DEBUG( //\n" 2975 " { f(); }, a);"); 2976 verifyFormat("DEBUG( //\n" 2977 " {\n" 2978 " f(); //\n" 2979 " },\n" 2980 " a);"); 2981 2982 EXPECT_EQ("call(parameter, {\n" 2983 " something();\n" 2984 " // Comment too\n" 2985 " // looooooooooong.\n" 2986 " somethingElse();\n" 2987 "});", 2988 format("call(parameter, {\n" 2989 " something();\n" 2990 " // Comment too looooooooooong.\n" 2991 " somethingElse();\n" 2992 "});", 2993 getLLVMStyleWithColumns(29))); 2994 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 2995 EXPECT_EQ("DEBUG({ // comment\n" 2996 " int i;\n" 2997 "});", 2998 format("DEBUG({ // comment\n" 2999 "int i;\n" 3000 "});")); 3001 EXPECT_EQ("DEBUG({\n" 3002 " int i;\n" 3003 "\n" 3004 " // comment\n" 3005 " int j;\n" 3006 "});", 3007 format("DEBUG({\n" 3008 " int i;\n" 3009 "\n" 3010 " // comment\n" 3011 " int j;\n" 3012 "});")); 3013 3014 verifyFormat("DEBUG({\n" 3015 " if (a)\n" 3016 " return;\n" 3017 "});"); 3018 verifyGoogleFormat("DEBUG({\n" 3019 " if (a) return;\n" 3020 "});"); 3021 FormatStyle Style = getGoogleStyle(); 3022 Style.ColumnLimit = 45; 3023 verifyFormat("Debug(aaaaa,\n" 3024 " {\n" 3025 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3026 " },\n" 3027 " a);", 3028 Style); 3029 3030 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 3031 3032 verifyNoCrash("^{v^{a}}"); 3033 } 3034 3035 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3036 EXPECT_EQ("#define MACRO() \\\n" 3037 " Debug(aaa, /* force line break */ \\\n" 3038 " { \\\n" 3039 " int i; \\\n" 3040 " int j; \\\n" 3041 " })", 3042 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3043 " { int i; int j; })", 3044 getGoogleStyle())); 3045 3046 EXPECT_EQ("#define A \\\n" 3047 " [] { \\\n" 3048 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3049 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3050 " }", 3051 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3052 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3053 getGoogleStyle())); 3054 } 3055 3056 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3057 EXPECT_EQ("{}", format("{}")); 3058 verifyFormat("enum E {};"); 3059 verifyFormat("enum E {}"); 3060 } 3061 3062 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 3063 FormatStyle Style = getLLVMStyle(); 3064 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 3065 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 3066 verifyFormat("FOO_BEGIN\n" 3067 " FOO_ENTRY\n" 3068 "FOO_END", Style); 3069 verifyFormat("FOO_BEGIN\n" 3070 " NESTED_FOO_BEGIN\n" 3071 " NESTED_FOO_ENTRY\n" 3072 " NESTED_FOO_END\n" 3073 "FOO_END", Style); 3074 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 3075 " int x;\n" 3076 " x = 1;\n" 3077 "FOO_END(Baz)", Style); 3078 } 3079 3080 //===----------------------------------------------------------------------===// 3081 // Line break tests. 3082 //===----------------------------------------------------------------------===// 3083 3084 TEST_F(FormatTest, PreventConfusingIndents) { 3085 verifyFormat( 3086 "void f() {\n" 3087 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3088 " parameter, parameter, parameter)),\n" 3089 " SecondLongCall(parameter));\n" 3090 "}"); 3091 verifyFormat( 3092 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3093 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3094 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3095 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3096 verifyFormat( 3097 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3098 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3099 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3100 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3101 verifyFormat( 3102 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3103 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3104 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3105 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3106 verifyFormat("int a = bbbb && ccc &&\n" 3107 " fffff(\n" 3108 "#define A Just forcing a new line\n" 3109 " ddd);"); 3110 } 3111 3112 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3113 verifyFormat( 3114 "bool aaaaaaa =\n" 3115 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3116 " bbbbbbbb();"); 3117 verifyFormat( 3118 "bool aaaaaaa =\n" 3119 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3120 " bbbbbbbb();"); 3121 3122 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3123 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3124 " ccccccccc == ddddddddddd;"); 3125 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3126 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3127 " ccccccccc == ddddddddddd;"); 3128 verifyFormat( 3129 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3130 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3131 " ccccccccc == ddddddddddd;"); 3132 3133 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3134 " aaaaaa) &&\n" 3135 " bbbbbb && cccccc;"); 3136 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3137 " aaaaaa) >>\n" 3138 " bbbbbb;"); 3139 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 3140 " SourceMgr.getSpellingColumnNumber(\n" 3141 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3142 " 1);"); 3143 3144 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3145 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3146 " cccccc) {\n}"); 3147 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3148 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 3149 " cccccc) {\n}"); 3150 verifyFormat("b = a &&\n" 3151 " // Comment\n" 3152 " b.c && d;"); 3153 3154 // If the LHS of a comparison is not a binary expression itself, the 3155 // additional linebreak confuses many people. 3156 verifyFormat( 3157 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3158 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3159 "}"); 3160 verifyFormat( 3161 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3162 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3163 "}"); 3164 verifyFormat( 3165 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3167 "}"); 3168 verifyFormat( 3169 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 3171 "}"); 3172 // Even explicit parentheses stress the precedence enough to make the 3173 // additional break unnecessary. 3174 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3175 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3176 "}"); 3177 // This cases is borderline, but with the indentation it is still readable. 3178 verifyFormat( 3179 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3180 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3181 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3182 "}", 3183 getLLVMStyleWithColumns(75)); 3184 3185 // If the LHS is a binary expression, we should still use the additional break 3186 // as otherwise the formatting hides the operator precedence. 3187 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3188 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3189 " 5) {\n" 3190 "}"); 3191 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3192 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 3193 " 5) {\n" 3194 "}"); 3195 3196 FormatStyle OnePerLine = getLLVMStyle(); 3197 OnePerLine.BinPackParameters = false; 3198 verifyFormat( 3199 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3200 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3202 OnePerLine); 3203 3204 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 3205 " .aaa(aaaaaaaaaaaaa) *\n" 3206 " aaaaaaa +\n" 3207 " aaaaaaa;", 3208 getLLVMStyleWithColumns(40)); 3209 } 3210 3211 TEST_F(FormatTest, ExpressionIndentation) { 3212 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3214 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3215 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3216 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3217 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3218 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3219 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3220 " ccccccccccccccccccccccccccccccccccccccccc;"); 3221 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3222 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3223 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3224 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3225 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3227 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3228 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3229 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3230 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3231 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3232 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3233 verifyFormat("if () {\n" 3234 "} else if (aaaaa && bbbbb > // break\n" 3235 " ccccc) {\n" 3236 "}"); 3237 verifyFormat("if () {\n" 3238 "} else if (aaaaa &&\n" 3239 " bbbbb > // break\n" 3240 " ccccc &&\n" 3241 " ddddd) {\n" 3242 "}"); 3243 3244 // Presence of a trailing comment used to change indentation of b. 3245 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3246 " b;\n" 3247 "return aaaaaaaaaaaaaaaaaaa +\n" 3248 " b; //", 3249 getLLVMStyleWithColumns(30)); 3250 } 3251 3252 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3253 // Not sure what the best system is here. Like this, the LHS can be found 3254 // immediately above an operator (everything with the same or a higher 3255 // indent). The RHS is aligned right of the operator and so compasses 3256 // everything until something with the same indent as the operator is found. 3257 // FIXME: Is this a good system? 3258 FormatStyle Style = getLLVMStyle(); 3259 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3260 verifyFormat( 3261 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3262 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3263 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3264 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3265 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3266 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3267 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3268 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3269 " > ccccccccccccccccccccccccccccccccccccccccc;", 3270 Style); 3271 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3272 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3273 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3274 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3275 Style); 3276 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3277 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3278 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3279 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3280 Style); 3281 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3282 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3283 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3284 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3285 Style); 3286 verifyFormat("if () {\n" 3287 "} else if (aaaaa\n" 3288 " && bbbbb // break\n" 3289 " > ccccc) {\n" 3290 "}", 3291 Style); 3292 verifyFormat("return (a)\n" 3293 " // comment\n" 3294 " + b;", 3295 Style); 3296 verifyFormat( 3297 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3298 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3299 " + cc;", 3300 Style); 3301 3302 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3303 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3304 Style); 3305 3306 // Forced by comments. 3307 verifyFormat( 3308 "unsigned ContentSize =\n" 3309 " sizeof(int16_t) // DWARF ARange version number\n" 3310 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3311 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3312 " + sizeof(int8_t); // Segment Size (in bytes)"); 3313 3314 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3315 " == boost::fusion::at_c<1>(iiii).second;", 3316 Style); 3317 3318 Style.ColumnLimit = 60; 3319 verifyFormat("zzzzzzzzzz\n" 3320 " = bbbbbbbbbbbbbbbbb\n" 3321 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3322 Style); 3323 } 3324 3325 TEST_F(FormatTest, EnforcedOperatorWraps) { 3326 // Here we'd like to wrap after the || operators, but a comment is forcing an 3327 // earlier wrap. 3328 verifyFormat("bool x = aaaaa //\n" 3329 " || bbbbb\n" 3330 " //\n" 3331 " || cccc;"); 3332 } 3333 3334 TEST_F(FormatTest, NoOperandAlignment) { 3335 FormatStyle Style = getLLVMStyle(); 3336 Style.AlignOperands = false; 3337 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 3338 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3339 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3340 Style); 3341 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3342 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3343 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3344 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3345 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3346 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3347 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3348 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3349 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3350 " > ccccccccccccccccccccccccccccccccccccccccc;", 3351 Style); 3352 3353 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3354 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3355 " + cc;", 3356 Style); 3357 verifyFormat("int a = aa\n" 3358 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3359 " * cccccccccccccccccccccccccccccccccccc;\n", 3360 Style); 3361 3362 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 3363 verifyFormat("return (a > b\n" 3364 " // comment1\n" 3365 " // comment2\n" 3366 " || c);", 3367 Style); 3368 } 3369 3370 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3371 FormatStyle Style = getLLVMStyle(); 3372 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3373 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3374 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3375 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3376 Style); 3377 } 3378 3379 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 3380 FormatStyle Style = getLLVMStyle(); 3381 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3382 Style.BinPackArguments = false; 3383 Style.ColumnLimit = 40; 3384 verifyFormat("void test() {\n" 3385 " someFunction(\n" 3386 " this + argument + is + quite\n" 3387 " + long + so + it + gets + wrapped\n" 3388 " + but + remains + bin - packed);\n" 3389 "}", 3390 Style); 3391 verifyFormat("void test() {\n" 3392 " someFunction(arg1,\n" 3393 " this + argument + is\n" 3394 " + quite + long + so\n" 3395 " + it + gets + wrapped\n" 3396 " + but + remains + bin\n" 3397 " - packed,\n" 3398 " arg3);\n" 3399 "}", 3400 Style); 3401 verifyFormat("void test() {\n" 3402 " someFunction(\n" 3403 " arg1,\n" 3404 " this + argument + has\n" 3405 " + anotherFunc(nested,\n" 3406 " calls + whose\n" 3407 " + arguments\n" 3408 " + are + also\n" 3409 " + wrapped,\n" 3410 " in + addition)\n" 3411 " + to + being + bin - packed,\n" 3412 " arg3);\n" 3413 "}", 3414 Style); 3415 3416 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 3417 verifyFormat("void test() {\n" 3418 " someFunction(\n" 3419 " arg1,\n" 3420 " this + argument + has +\n" 3421 " anotherFunc(nested,\n" 3422 " calls + whose +\n" 3423 " arguments +\n" 3424 " are + also +\n" 3425 " wrapped,\n" 3426 " in + addition) +\n" 3427 " to + being + bin - packed,\n" 3428 " arg3);\n" 3429 "}", 3430 Style); 3431 } 3432 3433 TEST_F(FormatTest, ConstructorInitializers) { 3434 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3435 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3436 getLLVMStyleWithColumns(45)); 3437 verifyFormat("Constructor()\n" 3438 " : Inttializer(FitsOnTheLine) {}", 3439 getLLVMStyleWithColumns(44)); 3440 verifyFormat("Constructor()\n" 3441 " : Inttializer(FitsOnTheLine) {}", 3442 getLLVMStyleWithColumns(43)); 3443 3444 verifyFormat("template <typename T>\n" 3445 "Constructor() : Initializer(FitsOnTheLine) {}", 3446 getLLVMStyleWithColumns(45)); 3447 3448 verifyFormat( 3449 "SomeClass::Constructor()\n" 3450 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3451 3452 verifyFormat( 3453 "SomeClass::Constructor()\n" 3454 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3455 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3456 verifyFormat( 3457 "SomeClass::Constructor()\n" 3458 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3459 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3460 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3461 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3462 " : aaaaaaaaaa(aaaaaa) {}"); 3463 3464 verifyFormat("Constructor()\n" 3465 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3466 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3467 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3468 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3469 3470 verifyFormat("Constructor()\n" 3471 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3472 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3473 3474 verifyFormat("Constructor(int Parameter = 0)\n" 3475 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3476 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3477 verifyFormat("Constructor()\n" 3478 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3479 "}", 3480 getLLVMStyleWithColumns(60)); 3481 verifyFormat("Constructor()\n" 3482 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3483 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3484 3485 // Here a line could be saved by splitting the second initializer onto two 3486 // lines, but that is not desirable. 3487 verifyFormat("Constructor()\n" 3488 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3489 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3490 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3491 3492 FormatStyle OnePerLine = getLLVMStyle(); 3493 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3494 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3495 verifyFormat("SomeClass::Constructor()\n" 3496 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3497 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3498 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3499 OnePerLine); 3500 verifyFormat("SomeClass::Constructor()\n" 3501 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3502 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3503 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3504 OnePerLine); 3505 verifyFormat("MyClass::MyClass(int var)\n" 3506 " : some_var_(var), // 4 space indent\n" 3507 " some_other_var_(var + 1) { // lined up\n" 3508 "}", 3509 OnePerLine); 3510 verifyFormat("Constructor()\n" 3511 " : aaaaa(aaaaaa),\n" 3512 " aaaaa(aaaaaa),\n" 3513 " aaaaa(aaaaaa),\n" 3514 " aaaaa(aaaaaa),\n" 3515 " aaaaa(aaaaaa) {}", 3516 OnePerLine); 3517 verifyFormat("Constructor()\n" 3518 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3519 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3520 OnePerLine); 3521 OnePerLine.BinPackParameters = false; 3522 verifyFormat( 3523 "Constructor()\n" 3524 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3525 " aaaaaaaaaaa().aaa(),\n" 3526 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3527 OnePerLine); 3528 OnePerLine.ColumnLimit = 60; 3529 verifyFormat("Constructor()\n" 3530 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3531 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3532 OnePerLine); 3533 3534 EXPECT_EQ("Constructor()\n" 3535 " : // Comment forcing unwanted break.\n" 3536 " aaaa(aaaa) {}", 3537 format("Constructor() :\n" 3538 " // Comment forcing unwanted break.\n" 3539 " aaaa(aaaa) {}")); 3540 } 3541 3542 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 3543 FormatStyle Style = getLLVMStyle(); 3544 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 3545 3546 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3547 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 3548 getStyleWithColumns(Style, 45)); 3549 verifyFormat("Constructor() :\n" 3550 " Initializer(FitsOnTheLine) {}", 3551 getStyleWithColumns(Style, 44)); 3552 verifyFormat("Constructor() :\n" 3553 " Initializer(FitsOnTheLine) {}", 3554 getStyleWithColumns(Style, 43)); 3555 3556 verifyFormat("template <typename T>\n" 3557 "Constructor() : Initializer(FitsOnTheLine) {}", 3558 getStyleWithColumns(Style, 50)); 3559 3560 verifyFormat( 3561 "SomeClass::Constructor() :\n" 3562 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3563 Style); 3564 3565 verifyFormat( 3566 "SomeClass::Constructor() :\n" 3567 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3568 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3569 Style); 3570 verifyFormat( 3571 "SomeClass::Constructor() :\n" 3572 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3573 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3574 Style); 3575 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3576 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 3577 " aaaaaaaaaa(aaaaaa) {}", 3578 Style); 3579 3580 verifyFormat("Constructor() :\n" 3581 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3582 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3583 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3584 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 3585 Style); 3586 3587 verifyFormat("Constructor() :\n" 3588 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3589 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3590 Style); 3591 3592 verifyFormat("Constructor(int Parameter = 0) :\n" 3593 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3594 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 3595 Style); 3596 verifyFormat("Constructor() :\n" 3597 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3598 "}", 3599 getStyleWithColumns(Style, 60)); 3600 verifyFormat("Constructor() :\n" 3601 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3602 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 3603 Style); 3604 3605 // Here a line could be saved by splitting the second initializer onto two 3606 // lines, but that is not desirable. 3607 verifyFormat("Constructor() :\n" 3608 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3609 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3610 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3611 Style); 3612 3613 FormatStyle OnePerLine = Style; 3614 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3615 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3616 verifyFormat("SomeClass::Constructor() :\n" 3617 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3618 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3619 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3620 OnePerLine); 3621 verifyFormat("SomeClass::Constructor() :\n" 3622 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3623 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3624 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3625 OnePerLine); 3626 verifyFormat("MyClass::MyClass(int var) :\n" 3627 " some_var_(var), // 4 space indent\n" 3628 " some_other_var_(var + 1) { // lined up\n" 3629 "}", 3630 OnePerLine); 3631 verifyFormat("Constructor() :\n" 3632 " aaaaa(aaaaaa),\n" 3633 " aaaaa(aaaaaa),\n" 3634 " aaaaa(aaaaaa),\n" 3635 " aaaaa(aaaaaa),\n" 3636 " aaaaa(aaaaaa) {}", 3637 OnePerLine); 3638 verifyFormat("Constructor() :\n" 3639 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3640 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3641 OnePerLine); 3642 OnePerLine.BinPackParameters = false; 3643 verifyFormat( 3644 "Constructor() :\n" 3645 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3646 " aaaaaaaaaaa().aaa(),\n" 3647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3648 OnePerLine); 3649 OnePerLine.ColumnLimit = 60; 3650 verifyFormat("Constructor() :\n" 3651 " aaaaaaaaaaaaaaaaaaaa(a),\n" 3652 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3653 OnePerLine); 3654 3655 EXPECT_EQ("Constructor() :\n" 3656 " // Comment forcing unwanted break.\n" 3657 " aaaa(aaaa) {}", 3658 format("Constructor() :\n" 3659 " // Comment forcing unwanted break.\n" 3660 " aaaa(aaaa) {}", 3661 Style)); 3662 3663 Style.ColumnLimit = 0; 3664 verifyFormat("SomeClass::Constructor() :\n" 3665 " a(a) {}", 3666 Style); 3667 verifyFormat("SomeClass::Constructor() noexcept :\n" 3668 " a(a) {}", 3669 Style); 3670 verifyFormat("SomeClass::Constructor() :\n" 3671 " a(a), b(b), c(c) {}", 3672 Style); 3673 verifyFormat("SomeClass::Constructor() :\n" 3674 " a(a) {\n" 3675 " foo();\n" 3676 " bar();\n" 3677 "}", 3678 Style); 3679 3680 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3681 verifyFormat("SomeClass::Constructor() :\n" 3682 " a(a), b(b), c(c) {\n" 3683 "}", 3684 Style); 3685 verifyFormat("SomeClass::Constructor() :\n" 3686 " a(a) {\n" 3687 "}", 3688 Style); 3689 3690 Style.ColumnLimit = 80; 3691 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3692 Style.ConstructorInitializerIndentWidth = 2; 3693 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 3694 Style); 3695 verifyFormat("SomeClass::Constructor() :\n" 3696 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3697 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 3698 Style); 3699 } 3700 3701 #ifndef EXPENSIVE_CHECKS 3702 // Expensive checks enables libstdc++ checking which includes validating the 3703 // state of ranges used in std::priority_queue - this blows out the 3704 // runtime/scalability of the function and makes this test unacceptably slow. 3705 TEST_F(FormatTest, MemoizationTests) { 3706 // This breaks if the memoization lookup does not take \c Indent and 3707 // \c LastSpace into account. 3708 verifyFormat( 3709 "extern CFRunLoopTimerRef\n" 3710 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3711 " CFTimeInterval interval, CFOptionFlags flags,\n" 3712 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3713 " CFRunLoopTimerContext *context) {}"); 3714 3715 // Deep nesting somewhat works around our memoization. 3716 verifyFormat( 3717 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3718 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3719 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3720 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3721 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3722 getLLVMStyleWithColumns(65)); 3723 verifyFormat( 3724 "aaaaa(\n" 3725 " aaaaa,\n" 3726 " aaaaa(\n" 3727 " aaaaa,\n" 3728 " aaaaa(\n" 3729 " aaaaa,\n" 3730 " aaaaa(\n" 3731 " aaaaa,\n" 3732 " aaaaa(\n" 3733 " aaaaa,\n" 3734 " aaaaa(\n" 3735 " aaaaa,\n" 3736 " aaaaa(\n" 3737 " aaaaa,\n" 3738 " aaaaa(\n" 3739 " aaaaa,\n" 3740 " aaaaa(\n" 3741 " aaaaa,\n" 3742 " aaaaa(\n" 3743 " aaaaa,\n" 3744 " aaaaa(\n" 3745 " aaaaa,\n" 3746 " aaaaa(\n" 3747 " aaaaa,\n" 3748 " aaaaa))))))))))));", 3749 getLLVMStyleWithColumns(65)); 3750 verifyFormat( 3751 "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" 3752 " a),\n" 3753 " a),\n" 3754 " a),\n" 3755 " a),\n" 3756 " a),\n" 3757 " a),\n" 3758 " a),\n" 3759 " a),\n" 3760 " a),\n" 3761 " a),\n" 3762 " a),\n" 3763 " a),\n" 3764 " a),\n" 3765 " a),\n" 3766 " a),\n" 3767 " a),\n" 3768 " a)", 3769 getLLVMStyleWithColumns(65)); 3770 3771 // This test takes VERY long when memoization is broken. 3772 FormatStyle OnePerLine = getLLVMStyle(); 3773 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3774 OnePerLine.BinPackParameters = false; 3775 std::string input = "Constructor()\n" 3776 " : aaaa(a,\n"; 3777 for (unsigned i = 0, e = 80; i != e; ++i) { 3778 input += " a,\n"; 3779 } 3780 input += " a) {}"; 3781 verifyFormat(input, OnePerLine); 3782 } 3783 #endif 3784 3785 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3786 verifyFormat( 3787 "void f() {\n" 3788 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3789 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3790 " f();\n" 3791 "}"); 3792 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3793 " Intervals[i - 1].getRange().getLast()) {\n}"); 3794 } 3795 3796 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3797 // Principially, we break function declarations in a certain order: 3798 // 1) break amongst arguments. 3799 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3800 " Cccccccccccccc cccccccccccccc);"); 3801 verifyFormat("template <class TemplateIt>\n" 3802 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3803 " TemplateIt *stop) {}"); 3804 3805 // 2) break after return type. 3806 verifyFormat( 3807 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3808 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3809 getGoogleStyle()); 3810 3811 // 3) break after (. 3812 verifyFormat( 3813 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3814 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3815 getGoogleStyle()); 3816 3817 // 4) break before after nested name specifiers. 3818 verifyFormat( 3819 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3820 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3821 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3822 getGoogleStyle()); 3823 3824 // However, there are exceptions, if a sufficient amount of lines can be 3825 // saved. 3826 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3827 // more adjusting. 3828 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3829 " Cccccccccccccc cccccccccc,\n" 3830 " Cccccccccccccc cccccccccc,\n" 3831 " Cccccccccccccc cccccccccc,\n" 3832 " Cccccccccccccc cccccccccc);"); 3833 verifyFormat( 3834 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3835 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3836 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3837 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3838 getGoogleStyle()); 3839 verifyFormat( 3840 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3841 " Cccccccccccccc cccccccccc,\n" 3842 " Cccccccccccccc cccccccccc,\n" 3843 " Cccccccccccccc cccccccccc,\n" 3844 " Cccccccccccccc cccccccccc,\n" 3845 " Cccccccccccccc cccccccccc,\n" 3846 " Cccccccccccccc cccccccccc);"); 3847 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3848 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3849 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3850 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3851 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3852 3853 // Break after multi-line parameters. 3854 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3855 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3856 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3857 " bbbb bbbb);"); 3858 verifyFormat("void SomeLoooooooooooongFunction(\n" 3859 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3860 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3861 " int bbbbbbbbbbbbb);"); 3862 3863 // Treat overloaded operators like other functions. 3864 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3865 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3866 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3867 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3868 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3869 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3870 verifyGoogleFormat( 3871 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3872 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3873 verifyGoogleFormat( 3874 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3875 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3876 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3877 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3878 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3879 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3880 verifyGoogleFormat( 3881 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3882 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3883 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3884 verifyGoogleFormat( 3885 "template <typename T>\n" 3886 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3887 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3888 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3889 3890 FormatStyle Style = getLLVMStyle(); 3891 Style.PointerAlignment = FormatStyle::PAS_Left; 3892 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3893 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3894 Style); 3895 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3897 Style); 3898 } 3899 3900 TEST_F(FormatTest, TrailingReturnType) { 3901 verifyFormat("auto foo() -> int;\n"); 3902 verifyFormat("struct S {\n" 3903 " auto bar() const -> int;\n" 3904 "};"); 3905 verifyFormat("template <size_t Order, typename T>\n" 3906 "auto load_img(const std::string &filename)\n" 3907 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3908 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3909 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3910 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3911 verifyFormat("template <typename T>\n" 3912 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3913 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3914 3915 // Not trailing return types. 3916 verifyFormat("void f() { auto a = b->c(); }"); 3917 } 3918 3919 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3920 // Avoid breaking before trailing 'const' or other trailing annotations, if 3921 // they are not function-like. 3922 FormatStyle Style = getGoogleStyle(); 3923 Style.ColumnLimit = 47; 3924 verifyFormat("void someLongFunction(\n" 3925 " int someLoooooooooooooongParameter) const {\n}", 3926 getLLVMStyleWithColumns(47)); 3927 verifyFormat("LoooooongReturnType\n" 3928 "someLoooooooongFunction() const {}", 3929 getLLVMStyleWithColumns(47)); 3930 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3931 " const {}", 3932 Style); 3933 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3934 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3935 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3936 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3937 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3938 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3939 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3940 " aaaaaaaaaaa aaaaa) const override;"); 3941 verifyGoogleFormat( 3942 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3943 " const override;"); 3944 3945 // Even if the first parameter has to be wrapped. 3946 verifyFormat("void someLongFunction(\n" 3947 " int someLongParameter) const {}", 3948 getLLVMStyleWithColumns(46)); 3949 verifyFormat("void someLongFunction(\n" 3950 " int someLongParameter) const {}", 3951 Style); 3952 verifyFormat("void someLongFunction(\n" 3953 " int someLongParameter) override {}", 3954 Style); 3955 verifyFormat("void someLongFunction(\n" 3956 " int someLongParameter) OVERRIDE {}", 3957 Style); 3958 verifyFormat("void someLongFunction(\n" 3959 " int someLongParameter) final {}", 3960 Style); 3961 verifyFormat("void someLongFunction(\n" 3962 " int someLongParameter) FINAL {}", 3963 Style); 3964 verifyFormat("void someLongFunction(\n" 3965 " int parameter) const override {}", 3966 Style); 3967 3968 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3969 verifyFormat("void someLongFunction(\n" 3970 " int someLongParameter) const\n" 3971 "{\n" 3972 "}", 3973 Style); 3974 3975 // Unless these are unknown annotations. 3976 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3977 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3978 " LONG_AND_UGLY_ANNOTATION;"); 3979 3980 // Breaking before function-like trailing annotations is fine to keep them 3981 // close to their arguments. 3982 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3983 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3984 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3985 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3986 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3987 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3988 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3989 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3990 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3991 3992 verifyFormat( 3993 "void aaaaaaaaaaaaaaaaaa()\n" 3994 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3995 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3996 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3997 " __attribute__((unused));"); 3998 verifyGoogleFormat( 3999 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4000 " GUARDED_BY(aaaaaaaaaaaa);"); 4001 verifyGoogleFormat( 4002 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4003 " GUARDED_BY(aaaaaaaaaaaa);"); 4004 verifyGoogleFormat( 4005 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4006 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4007 verifyGoogleFormat( 4008 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4009 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4010 } 4011 4012 TEST_F(FormatTest, FunctionAnnotations) { 4013 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4014 "int OldFunction(const string ¶meter) {}"); 4015 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4016 "string OldFunction(const string ¶meter) {}"); 4017 verifyFormat("template <typename T>\n" 4018 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4019 "string OldFunction(const string ¶meter) {}"); 4020 4021 // Not function annotations. 4022 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4023 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4024 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4025 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4026 verifyFormat("MACRO(abc).function() // wrap\n" 4027 " << abc;"); 4028 verifyFormat("MACRO(abc)->function() // wrap\n" 4029 " << abc;"); 4030 verifyFormat("MACRO(abc)::function() // wrap\n" 4031 " << abc;"); 4032 } 4033 4034 TEST_F(FormatTest, BreaksDesireably) { 4035 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4036 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4037 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4038 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4040 "}"); 4041 4042 verifyFormat( 4043 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4045 4046 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4048 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4049 4050 verifyFormat( 4051 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4052 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4053 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4054 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4055 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4056 4057 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4058 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4059 4060 verifyFormat( 4061 "void f() {\n" 4062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4064 "}"); 4065 verifyFormat( 4066 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4067 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4068 verifyFormat( 4069 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4071 verifyFormat( 4072 "aaaaaa(aaa,\n" 4073 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4075 " aaaa);"); 4076 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4077 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4079 4080 // Indent consistently independent of call expression and unary operator. 4081 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4082 " dddddddddddddddddddddddddddddd));"); 4083 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4084 " dddddddddddddddddddddddddddddd));"); 4085 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4086 " dddddddddddddddddddddddddddddd));"); 4087 4088 // This test case breaks on an incorrect memoization, i.e. an optimization not 4089 // taking into account the StopAt value. 4090 verifyFormat( 4091 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4092 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4093 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4094 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4095 4096 verifyFormat("{\n {\n {\n" 4097 " Annotation.SpaceRequiredBefore =\n" 4098 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4099 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4100 " }\n }\n}"); 4101 4102 // Break on an outer level if there was a break on an inner level. 4103 EXPECT_EQ("f(g(h(a, // comment\n" 4104 " b, c),\n" 4105 " d, e),\n" 4106 " x, y);", 4107 format("f(g(h(a, // comment\n" 4108 " b, c), d, e), x, y);")); 4109 4110 // Prefer breaking similar line breaks. 4111 verifyFormat( 4112 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4113 " NSTrackingMouseEnteredAndExited |\n" 4114 " NSTrackingActiveAlways;"); 4115 } 4116 4117 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4118 FormatStyle NoBinPacking = getGoogleStyle(); 4119 NoBinPacking.BinPackParameters = false; 4120 NoBinPacking.BinPackArguments = true; 4121 verifyFormat("void f() {\n" 4122 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4123 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4124 "}", 4125 NoBinPacking); 4126 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4127 " int aaaaaaaaaaaaaaaaaaaa,\n" 4128 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4129 NoBinPacking); 4130 4131 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4132 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4133 " vector<int> bbbbbbbbbbbbbbb);", 4134 NoBinPacking); 4135 // FIXME: This behavior difference is probably not wanted. However, currently 4136 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 4137 // template arguments from BreakBeforeParameter being set because of the 4138 // one-per-line formatting. 4139 verifyFormat( 4140 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4141 " aaaaaaaaaa> aaaaaaaaaa);", 4142 NoBinPacking); 4143 verifyFormat( 4144 "void fffffffffff(\n" 4145 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 4146 " aaaaaaaaaa);"); 4147 } 4148 4149 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4150 FormatStyle NoBinPacking = getGoogleStyle(); 4151 NoBinPacking.BinPackParameters = false; 4152 NoBinPacking.BinPackArguments = false; 4153 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4154 " aaaaaaaaaaaaaaaaaaaa,\n" 4155 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4156 NoBinPacking); 4157 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4158 " aaaaaaaaaaaaa,\n" 4159 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4160 NoBinPacking); 4161 verifyFormat( 4162 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4163 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4164 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4165 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4167 NoBinPacking); 4168 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4169 " .aaaaaaaaaaaaaaaaaa();", 4170 NoBinPacking); 4171 verifyFormat("void f() {\n" 4172 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4173 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4174 "}", 4175 NoBinPacking); 4176 4177 verifyFormat( 4178 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4179 " aaaaaaaaaaaa,\n" 4180 " aaaaaaaaaaaa);", 4181 NoBinPacking); 4182 verifyFormat( 4183 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4184 " ddddddddddddddddddddddddddddd),\n" 4185 " test);", 4186 NoBinPacking); 4187 4188 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4189 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4190 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 4191 " aaaaaaaaaaaaaaaaaa;", 4192 NoBinPacking); 4193 verifyFormat("a(\"a\"\n" 4194 " \"a\",\n" 4195 " a);"); 4196 4197 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4198 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4199 " aaaaaaaaa,\n" 4200 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4201 NoBinPacking); 4202 verifyFormat( 4203 "void f() {\n" 4204 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4205 " .aaaaaaa();\n" 4206 "}", 4207 NoBinPacking); 4208 verifyFormat( 4209 "template <class SomeType, class SomeOtherType>\n" 4210 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4211 NoBinPacking); 4212 } 4213 4214 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4215 FormatStyle Style = getLLVMStyleWithColumns(15); 4216 Style.ExperimentalAutoDetectBinPacking = true; 4217 EXPECT_EQ("aaa(aaaa,\n" 4218 " aaaa,\n" 4219 " aaaa);\n" 4220 "aaa(aaaa,\n" 4221 " aaaa,\n" 4222 " aaaa);", 4223 format("aaa(aaaa,\n" // one-per-line 4224 " aaaa,\n" 4225 " aaaa );\n" 4226 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4227 Style)); 4228 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4229 " aaaa);\n" 4230 "aaa(aaaa, aaaa,\n" 4231 " aaaa);", 4232 format("aaa(aaaa, aaaa,\n" // bin-packed 4233 " aaaa );\n" 4234 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4235 Style)); 4236 } 4237 4238 TEST_F(FormatTest, FormatsBuilderPattern) { 4239 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4240 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4241 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4242 " .StartsWith(\".init\", ORDER_INIT)\n" 4243 " .StartsWith(\".fini\", ORDER_FINI)\n" 4244 " .StartsWith(\".hash\", ORDER_HASH)\n" 4245 " .Default(ORDER_TEXT);\n"); 4246 4247 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4248 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4249 verifyFormat( 4250 "aaaaaaa->aaaaaaa\n" 4251 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4252 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4253 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4254 verifyFormat( 4255 "aaaaaaa->aaaaaaa\n" 4256 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4257 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4258 verifyFormat( 4259 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4260 " aaaaaaaaaaaaaa);"); 4261 verifyFormat( 4262 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4263 " aaaaaa->aaaaaaaaaaaa()\n" 4264 " ->aaaaaaaaaaaaaaaa(\n" 4265 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4266 " ->aaaaaaaaaaaaaaaaa();"); 4267 verifyGoogleFormat( 4268 "void f() {\n" 4269 " someo->Add((new util::filetools::Handler(dir))\n" 4270 " ->OnEvent1(NewPermanentCallback(\n" 4271 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4272 " ->OnEvent2(NewPermanentCallback(\n" 4273 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4274 " ->OnEvent3(NewPermanentCallback(\n" 4275 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4276 " ->OnEvent5(NewPermanentCallback(\n" 4277 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4278 " ->OnEvent6(NewPermanentCallback(\n" 4279 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4280 "}"); 4281 4282 verifyFormat( 4283 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4284 verifyFormat("aaaaaaaaaaaaaaa()\n" 4285 " .aaaaaaaaaaaaaaa()\n" 4286 " .aaaaaaaaaaaaaaa()\n" 4287 " .aaaaaaaaaaaaaaa()\n" 4288 " .aaaaaaaaaaaaaaa();"); 4289 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4290 " .aaaaaaaaaaaaaaa()\n" 4291 " .aaaaaaaaaaaaaaa()\n" 4292 " .aaaaaaaaaaaaaaa();"); 4293 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4294 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4295 " .aaaaaaaaaaaaaaa();"); 4296 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4297 " ->aaaaaaaaaaaaaae(0)\n" 4298 " ->aaaaaaaaaaaaaaa();"); 4299 4300 // Don't linewrap after very short segments. 4301 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4302 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4303 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4304 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4305 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4306 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4307 verifyFormat("aaa()\n" 4308 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4309 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4310 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4311 4312 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4313 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4314 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4315 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4316 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4317 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4318 4319 // Prefer not to break after empty parentheses. 4320 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4321 " First->LastNewlineOffset);"); 4322 4323 // Prefer not to create "hanging" indents. 4324 verifyFormat( 4325 "return !soooooooooooooome_map\n" 4326 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4327 " .second;"); 4328 verifyFormat( 4329 "return aaaaaaaaaaaaaaaa\n" 4330 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 4331 " .aaaa(aaaaaaaaaaaaaa);"); 4332 // No hanging indent here. 4333 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 4334 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4335 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 4336 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4337 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4338 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4339 getLLVMStyleWithColumns(60)); 4340 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 4341 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 4342 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4343 getLLVMStyleWithColumns(59)); 4344 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4345 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4346 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4347 } 4348 4349 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4350 verifyFormat( 4351 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4352 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4353 verifyFormat( 4354 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4355 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4356 4357 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4358 " ccccccccccccccccccccccccc) {\n}"); 4359 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4360 " ccccccccccccccccccccccccc) {\n}"); 4361 4362 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4363 " ccccccccccccccccccccccccc) {\n}"); 4364 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4365 " ccccccccccccccccccccccccc) {\n}"); 4366 4367 verifyFormat( 4368 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4369 " ccccccccccccccccccccccccc) {\n}"); 4370 verifyFormat( 4371 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4372 " ccccccccccccccccccccccccc) {\n}"); 4373 4374 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4375 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4376 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4377 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4378 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4379 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4380 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4381 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4382 4383 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4384 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4385 " aaaaaaaaaaaaaaa != aa) {\n}"); 4386 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4387 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4388 " aaaaaaaaaaaaaaa != aa) {\n}"); 4389 } 4390 4391 TEST_F(FormatTest, BreaksAfterAssignments) { 4392 verifyFormat( 4393 "unsigned Cost =\n" 4394 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4395 " SI->getPointerAddressSpaceee());\n"); 4396 verifyFormat( 4397 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4398 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4399 4400 verifyFormat( 4401 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4402 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4403 verifyFormat("unsigned OriginalStartColumn =\n" 4404 " SourceMgr.getSpellingColumnNumber(\n" 4405 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4406 " 1;"); 4407 } 4408 4409 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 4410 FormatStyle Style = getLLVMStyle(); 4411 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4412 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 4413 Style); 4414 4415 Style.PenaltyBreakAssignment = 20; 4416 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 4417 " cccccccccccccccccccccccccc;", 4418 Style); 4419 } 4420 4421 TEST_F(FormatTest, AlignsAfterAssignments) { 4422 verifyFormat( 4423 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4424 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4425 verifyFormat( 4426 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4427 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4428 verifyFormat( 4429 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4430 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4431 verifyFormat( 4432 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4433 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4434 verifyFormat( 4435 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4436 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4437 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4438 } 4439 4440 TEST_F(FormatTest, AlignsAfterReturn) { 4441 verifyFormat( 4442 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4443 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4444 verifyFormat( 4445 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4446 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4447 verifyFormat( 4448 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4449 " aaaaaaaaaaaaaaaaaaaaaa();"); 4450 verifyFormat( 4451 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4452 " aaaaaaaaaaaaaaaaaaaaaa());"); 4453 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4455 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4456 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4457 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4458 verifyFormat("return\n" 4459 " // true if code is one of a or b.\n" 4460 " code == a || code == b;"); 4461 } 4462 4463 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4464 verifyFormat( 4465 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4466 " aaaaaaaaa aaaaaaa) {}"); 4467 verifyFormat( 4468 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4469 " aaaaaaaaaaa aaaaaaaaa);"); 4470 verifyFormat( 4471 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4472 " aaaaaaaaaaaaaaaaaaaaa));"); 4473 FormatStyle Style = getLLVMStyle(); 4474 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4475 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4476 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4477 Style); 4478 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4479 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4480 Style); 4481 verifyFormat("SomeLongVariableName->someFunction(\n" 4482 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4483 Style); 4484 verifyFormat( 4485 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4486 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4487 Style); 4488 verifyFormat( 4489 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4490 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4491 Style); 4492 verifyFormat( 4493 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4494 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4495 Style); 4496 4497 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4498 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4499 " b));", 4500 Style); 4501 4502 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4503 Style.BinPackArguments = false; 4504 Style.BinPackParameters = false; 4505 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4506 " aaaaaaaaaaa aaaaaaaa,\n" 4507 " aaaaaaaaa aaaaaaa,\n" 4508 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4509 Style); 4510 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4511 " aaaaaaaaaaa aaaaaaaaa,\n" 4512 " aaaaaaaaaaa aaaaaaaaa,\n" 4513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4514 Style); 4515 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4516 " aaaaaaaaaaaaaaa,\n" 4517 " aaaaaaaaaaaaaaaaaaaaa,\n" 4518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4519 Style); 4520 verifyFormat( 4521 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4522 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4523 Style); 4524 verifyFormat( 4525 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4526 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4527 Style); 4528 verifyFormat( 4529 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4530 " aaaaaaaaaaaaaaaaaaaaa(\n" 4531 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4532 " aaaaaaaaaaaaaaaa);", 4533 Style); 4534 verifyFormat( 4535 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4536 " aaaaaaaaaaaaaaaaaaaaa(\n" 4537 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4538 " aaaaaaaaaaaaaaaa);", 4539 Style); 4540 } 4541 4542 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4543 FormatStyle Style = getLLVMStyleWithColumns(40); 4544 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4545 " bbbbbbbbbbbbbbbbbbbbbb);", 4546 Style); 4547 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4548 Style.AlignOperands = false; 4549 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4550 " bbbbbbbbbbbbbbbbbbbbbb);", 4551 Style); 4552 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4553 Style.AlignOperands = true; 4554 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4555 " bbbbbbbbbbbbbbbbbbbbbb);", 4556 Style); 4557 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4558 Style.AlignOperands = false; 4559 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4560 " bbbbbbbbbbbbbbbbbbbbbb);", 4561 Style); 4562 } 4563 4564 TEST_F(FormatTest, BreaksConditionalExpressions) { 4565 verifyFormat( 4566 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4567 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4568 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4569 verifyFormat( 4570 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4571 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4572 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4573 verifyFormat( 4574 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4575 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4576 verifyFormat( 4577 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 4578 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4579 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4580 verifyFormat( 4581 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4582 " : aaaaaaaaaaaaa);"); 4583 verifyFormat( 4584 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4585 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4586 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4587 " aaaaaaaaaaaaa);"); 4588 verifyFormat( 4589 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4590 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4591 " aaaaaaaaaaaaa);"); 4592 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4593 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4594 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4595 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4596 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4597 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4598 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4599 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4600 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4601 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4602 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4603 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4604 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4605 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4606 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4607 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4608 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4609 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4610 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4611 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4612 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4613 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4614 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4615 " : aaaaaaaaaaaaaaaa;"); 4616 verifyFormat( 4617 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4618 " ? aaaaaaaaaaaaaaa\n" 4619 " : aaaaaaaaaaaaaaa;"); 4620 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4621 " aaaaaaaaa\n" 4622 " ? b\n" 4623 " : c);"); 4624 verifyFormat("return aaaa == bbbb\n" 4625 " // comment\n" 4626 " ? aaaa\n" 4627 " : bbbb;"); 4628 verifyFormat("unsigned Indent =\n" 4629 " format(TheLine.First,\n" 4630 " IndentForLevel[TheLine.Level] >= 0\n" 4631 " ? IndentForLevel[TheLine.Level]\n" 4632 " : TheLine * 2,\n" 4633 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4634 getLLVMStyleWithColumns(60)); 4635 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4636 " ? aaaaaaaaaaaaaaa\n" 4637 " : bbbbbbbbbbbbbbb //\n" 4638 " ? ccccccccccccccc\n" 4639 " : ddddddddddddddd;"); 4640 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4641 " ? aaaaaaaaaaaaaaa\n" 4642 " : (bbbbbbbbbbbbbbb //\n" 4643 " ? ccccccccccccccc\n" 4644 " : ddddddddddddddd);"); 4645 verifyFormat( 4646 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4647 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4648 " aaaaaaaaaaaaaaaaaaaaa +\n" 4649 " aaaaaaaaaaaaaaaaaaaaa\n" 4650 " : aaaaaaaaaa;"); 4651 verifyFormat( 4652 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4653 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4654 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4655 4656 FormatStyle NoBinPacking = getLLVMStyle(); 4657 NoBinPacking.BinPackArguments = false; 4658 verifyFormat( 4659 "void f() {\n" 4660 " g(aaa,\n" 4661 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4662 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4663 " ? aaaaaaaaaaaaaaa\n" 4664 " : aaaaaaaaaaaaaaa);\n" 4665 "}", 4666 NoBinPacking); 4667 verifyFormat( 4668 "void f() {\n" 4669 " g(aaa,\n" 4670 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4671 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4672 " ?: aaaaaaaaaaaaaaa);\n" 4673 "}", 4674 NoBinPacking); 4675 4676 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4677 " // comment.\n" 4678 " ccccccccccccccccccccccccccccccccccccccc\n" 4679 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4680 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4681 4682 // Assignments in conditional expressions. Apparently not uncommon :-(. 4683 verifyFormat("return a != b\n" 4684 " // comment\n" 4685 " ? a = b\n" 4686 " : a = b;"); 4687 verifyFormat("return a != b\n" 4688 " // comment\n" 4689 " ? a = a != b\n" 4690 " // comment\n" 4691 " ? a = b\n" 4692 " : a\n" 4693 " : a;\n"); 4694 verifyFormat("return a != b\n" 4695 " // comment\n" 4696 " ? a\n" 4697 " : a = a != b\n" 4698 " // comment\n" 4699 " ? a = b\n" 4700 " : a;"); 4701 } 4702 4703 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4704 FormatStyle Style = getLLVMStyle(); 4705 Style.BreakBeforeTernaryOperators = false; 4706 Style.ColumnLimit = 70; 4707 verifyFormat( 4708 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4711 Style); 4712 verifyFormat( 4713 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4714 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4715 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4716 Style); 4717 verifyFormat( 4718 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4720 Style); 4721 verifyFormat( 4722 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 4723 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4724 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4725 Style); 4726 verifyFormat( 4727 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4728 " aaaaaaaaaaaaa);", 4729 Style); 4730 verifyFormat( 4731 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4732 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4733 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4734 " aaaaaaaaaaaaa);", 4735 Style); 4736 verifyFormat( 4737 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4738 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4739 " aaaaaaaaaaaaa);", 4740 Style); 4741 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4742 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4743 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4744 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4745 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4746 Style); 4747 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4748 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4749 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4750 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4752 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4753 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4754 Style); 4755 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4756 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4757 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4758 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4759 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4760 Style); 4761 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4762 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4763 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4764 Style); 4765 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4766 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4767 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4768 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4769 Style); 4770 verifyFormat( 4771 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4772 " aaaaaaaaaaaaaaa :\n" 4773 " aaaaaaaaaaaaaaa;", 4774 Style); 4775 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4776 " aaaaaaaaa ?\n" 4777 " b :\n" 4778 " c);", 4779 Style); 4780 verifyFormat("unsigned Indent =\n" 4781 " format(TheLine.First,\n" 4782 " IndentForLevel[TheLine.Level] >= 0 ?\n" 4783 " IndentForLevel[TheLine.Level] :\n" 4784 " TheLine * 2,\n" 4785 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4786 Style); 4787 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4788 " aaaaaaaaaaaaaaa :\n" 4789 " bbbbbbbbbbbbbbb ? //\n" 4790 " ccccccccccccccc :\n" 4791 " ddddddddddddddd;", 4792 Style); 4793 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4794 " aaaaaaaaaaaaaaa :\n" 4795 " (bbbbbbbbbbbbbbb ? //\n" 4796 " ccccccccccccccc :\n" 4797 " ddddddddddddddd);", 4798 Style); 4799 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4800 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4801 " ccccccccccccccccccccccccccc;", 4802 Style); 4803 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4804 " aaaaa :\n" 4805 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4806 Style); 4807 } 4808 4809 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4810 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4811 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4812 verifyFormat("bool a = true, b = false;"); 4813 4814 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4815 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4816 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4817 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4818 verifyFormat( 4819 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4820 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4821 " d = e && f;"); 4822 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4823 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4824 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4825 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4826 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4827 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4828 4829 FormatStyle Style = getGoogleStyle(); 4830 Style.PointerAlignment = FormatStyle::PAS_Left; 4831 Style.DerivePointerAlignment = false; 4832 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4833 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4834 " *b = bbbbbbbbbbbbbbbbbbb;", 4835 Style); 4836 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4837 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4838 Style); 4839 verifyFormat("vector<int*> a, b;", Style); 4840 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 4841 } 4842 4843 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4844 verifyFormat("arr[foo ? bar : baz];"); 4845 verifyFormat("f()[foo ? bar : baz];"); 4846 verifyFormat("(a + b)[foo ? bar : baz];"); 4847 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4848 } 4849 4850 TEST_F(FormatTest, AlignsStringLiterals) { 4851 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4852 " \"short literal\");"); 4853 verifyFormat( 4854 "looooooooooooooooooooooooongFunction(\n" 4855 " \"short literal\"\n" 4856 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4857 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4858 " \" string literals\",\n" 4859 " and, other, parameters);"); 4860 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4861 " \"5678\";", 4862 format("fun + \"1243\" /* comment */\n" 4863 " \"5678\";", 4864 getLLVMStyleWithColumns(28))); 4865 EXPECT_EQ( 4866 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4867 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4868 " \"aaaaaaaaaaaaaaaa\";", 4869 format("aaaaaa =" 4870 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4871 "aaaaaaaaaaaaaaaaaaaaa\" " 4872 "\"aaaaaaaaaaaaaaaa\";")); 4873 verifyFormat("a = a + \"a\"\n" 4874 " \"a\"\n" 4875 " \"a\";"); 4876 verifyFormat("f(\"a\", \"b\"\n" 4877 " \"c\");"); 4878 4879 verifyFormat( 4880 "#define LL_FORMAT \"ll\"\n" 4881 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4882 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4883 4884 verifyFormat("#define A(X) \\\n" 4885 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4886 " \"ccccc\"", 4887 getLLVMStyleWithColumns(23)); 4888 verifyFormat("#define A \"def\"\n" 4889 "f(\"abc\" A \"ghi\"\n" 4890 " \"jkl\");"); 4891 4892 verifyFormat("f(L\"a\"\n" 4893 " L\"b\");"); 4894 verifyFormat("#define A(X) \\\n" 4895 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4896 " L\"ccccc\"", 4897 getLLVMStyleWithColumns(25)); 4898 4899 verifyFormat("f(@\"a\"\n" 4900 " @\"b\");"); 4901 verifyFormat("NSString s = @\"a\"\n" 4902 " @\"b\"\n" 4903 " @\"c\";"); 4904 verifyFormat("NSString s = @\"a\"\n" 4905 " \"b\"\n" 4906 " \"c\";"); 4907 } 4908 4909 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4910 FormatStyle Style = getLLVMStyle(); 4911 // No declarations or definitions should be moved to own line. 4912 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4913 verifyFormat("class A {\n" 4914 " int f() { return 1; }\n" 4915 " int g();\n" 4916 "};\n" 4917 "int f() { return 1; }\n" 4918 "int g();\n", 4919 Style); 4920 4921 // All declarations and definitions should have the return type moved to its 4922 // own 4923 // line. 4924 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4925 verifyFormat("class E {\n" 4926 " int\n" 4927 " f() {\n" 4928 " return 1;\n" 4929 " }\n" 4930 " int\n" 4931 " g();\n" 4932 "};\n" 4933 "int\n" 4934 "f() {\n" 4935 " return 1;\n" 4936 "}\n" 4937 "int\n" 4938 "g();\n", 4939 Style); 4940 4941 // Top-level definitions, and no kinds of declarations should have the 4942 // return type moved to its own line. 4943 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4944 verifyFormat("class B {\n" 4945 " int f() { return 1; }\n" 4946 " int g();\n" 4947 "};\n" 4948 "int\n" 4949 "f() {\n" 4950 " return 1;\n" 4951 "}\n" 4952 "int g();\n", 4953 Style); 4954 4955 // Top-level definitions and declarations should have the return type moved 4956 // to its own line. 4957 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4958 verifyFormat("class C {\n" 4959 " int f() { return 1; }\n" 4960 " int g();\n" 4961 "};\n" 4962 "int\n" 4963 "f() {\n" 4964 " return 1;\n" 4965 "}\n" 4966 "int\n" 4967 "g();\n", 4968 Style); 4969 4970 // All definitions should have the return type moved to its own line, but no 4971 // kinds of declarations. 4972 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4973 verifyFormat("class D {\n" 4974 " int\n" 4975 " f() {\n" 4976 " return 1;\n" 4977 " }\n" 4978 " int g();\n" 4979 "};\n" 4980 "int\n" 4981 "f() {\n" 4982 " return 1;\n" 4983 "}\n" 4984 "int g();\n", 4985 Style); 4986 verifyFormat("const char *\n" 4987 "f(void) {\n" // Break here. 4988 " return \"\";\n" 4989 "}\n" 4990 "const char *bar(void);\n", // No break here. 4991 Style); 4992 verifyFormat("template <class T>\n" 4993 "T *\n" 4994 "f(T &c) {\n" // Break here. 4995 " return NULL;\n" 4996 "}\n" 4997 "template <class T> T *f(T &c);\n", // No break here. 4998 Style); 4999 verifyFormat("class C {\n" 5000 " int\n" 5001 " operator+() {\n" 5002 " return 1;\n" 5003 " }\n" 5004 " int\n" 5005 " operator()() {\n" 5006 " return 1;\n" 5007 " }\n" 5008 "};\n", 5009 Style); 5010 verifyFormat("void\n" 5011 "A::operator()() {}\n" 5012 "void\n" 5013 "A::operator>>() {}\n" 5014 "void\n" 5015 "A::operator+() {}\n", 5016 Style); 5017 verifyFormat("void *operator new(std::size_t s);", // No break here. 5018 Style); 5019 verifyFormat("void *\n" 5020 "operator new(std::size_t s) {}", 5021 Style); 5022 verifyFormat("void *\n" 5023 "operator delete[](void *ptr) {}", 5024 Style); 5025 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 5026 verifyFormat("const char *\n" 5027 "f(void)\n" // Break here. 5028 "{\n" 5029 " return \"\";\n" 5030 "}\n" 5031 "const char *bar(void);\n", // No break here. 5032 Style); 5033 verifyFormat("template <class T>\n" 5034 "T *\n" // Problem here: no line break 5035 "f(T &c)\n" // Break here. 5036 "{\n" 5037 " return NULL;\n" 5038 "}\n" 5039 "template <class T> T *f(T &c);\n", // No break here. 5040 Style); 5041 } 5042 5043 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 5044 FormatStyle NoBreak = getLLVMStyle(); 5045 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 5046 FormatStyle Break = getLLVMStyle(); 5047 Break.AlwaysBreakBeforeMultilineStrings = true; 5048 verifyFormat("aaaa = \"bbbb\"\n" 5049 " \"cccc\";", 5050 NoBreak); 5051 verifyFormat("aaaa =\n" 5052 " \"bbbb\"\n" 5053 " \"cccc\";", 5054 Break); 5055 verifyFormat("aaaa(\"bbbb\"\n" 5056 " \"cccc\");", 5057 NoBreak); 5058 verifyFormat("aaaa(\n" 5059 " \"bbbb\"\n" 5060 " \"cccc\");", 5061 Break); 5062 verifyFormat("aaaa(qqq, \"bbbb\"\n" 5063 " \"cccc\");", 5064 NoBreak); 5065 verifyFormat("aaaa(qqq,\n" 5066 " \"bbbb\"\n" 5067 " \"cccc\");", 5068 Break); 5069 verifyFormat("aaaa(qqq,\n" 5070 " L\"bbbb\"\n" 5071 " L\"cccc\");", 5072 Break); 5073 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 5074 " \"bbbb\"));", 5075 Break); 5076 verifyFormat("string s = someFunction(\n" 5077 " \"abc\"\n" 5078 " \"abc\");", 5079 Break); 5080 5081 // As we break before unary operators, breaking right after them is bad. 5082 verifyFormat("string foo = abc ? \"x\"\n" 5083 " \"blah blah blah blah blah blah\"\n" 5084 " : \"y\";", 5085 Break); 5086 5087 // Don't break if there is no column gain. 5088 verifyFormat("f(\"aaaa\"\n" 5089 " \"bbbb\");", 5090 Break); 5091 5092 // Treat literals with escaped newlines like multi-line string literals. 5093 EXPECT_EQ("x = \"a\\\n" 5094 "b\\\n" 5095 "c\";", 5096 format("x = \"a\\\n" 5097 "b\\\n" 5098 "c\";", 5099 NoBreak)); 5100 EXPECT_EQ("xxxx =\n" 5101 " \"a\\\n" 5102 "b\\\n" 5103 "c\";", 5104 format("xxxx = \"a\\\n" 5105 "b\\\n" 5106 "c\";", 5107 Break)); 5108 5109 EXPECT_EQ("NSString *const kString =\n" 5110 " @\"aaaa\"\n" 5111 " @\"bbbb\";", 5112 format("NSString *const kString = @\"aaaa\"\n" 5113 "@\"bbbb\";", 5114 Break)); 5115 5116 Break.ColumnLimit = 0; 5117 verifyFormat("const char *hello = \"hello llvm\";", Break); 5118 } 5119 5120 TEST_F(FormatTest, AlignsPipes) { 5121 verifyFormat( 5122 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5123 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5124 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5125 verifyFormat( 5126 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 5127 " << aaaaaaaaaaaaaaaaaaaa;"); 5128 verifyFormat( 5129 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5130 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5131 verifyFormat( 5132 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5133 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5134 verifyFormat( 5135 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 5136 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 5137 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 5138 verifyFormat( 5139 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5140 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5141 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5142 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5143 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5144 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5145 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5146 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 5147 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 5148 verifyFormat( 5149 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5151 verifyFormat( 5152 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 5153 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5154 5155 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 5156 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 5157 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5158 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5159 " aaaaaaaaaaaaaaaaaaaaa)\n" 5160 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5161 verifyFormat("LOG_IF(aaa == //\n" 5162 " bbb)\n" 5163 " << a << b;"); 5164 5165 // But sometimes, breaking before the first "<<" is desirable. 5166 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5167 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5168 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5169 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5170 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5171 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5172 " << BEF << IsTemplate << Description << E->getType();"); 5173 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5174 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5175 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5176 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5177 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5179 " << aaa;"); 5180 5181 verifyFormat( 5182 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5183 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5184 5185 // Incomplete string literal. 5186 EXPECT_EQ("llvm::errs() << \"\n" 5187 " << a;", 5188 format("llvm::errs() << \"\n<<a;")); 5189 5190 verifyFormat("void f() {\n" 5191 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5192 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5193 "}"); 5194 5195 // Handle 'endl'. 5196 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5197 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5198 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5199 5200 // Handle '\n'. 5201 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 5202 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5203 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 5204 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 5205 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 5206 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 5207 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 5208 } 5209 5210 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 5211 verifyFormat("return out << \"somepacket = {\\n\"\n" 5212 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 5213 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 5214 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 5215 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 5216 " << \"}\";"); 5217 5218 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5219 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 5220 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 5221 verifyFormat( 5222 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 5223 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 5224 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 5225 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 5226 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 5227 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 5228 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5229 verifyFormat( 5230 "void f() {\n" 5231 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 5232 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 5233 "}"); 5234 5235 // Breaking before the first "<<" is generally not desirable. 5236 verifyFormat( 5237 "llvm::errs()\n" 5238 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5239 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5240 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5241 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5242 getLLVMStyleWithColumns(70)); 5243 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5244 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5245 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5246 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5247 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5248 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5249 getLLVMStyleWithColumns(70)); 5250 5251 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5252 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 5253 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 5254 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5255 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 5256 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 5257 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 5258 " (aaaa + aaaa);", 5259 getLLVMStyleWithColumns(40)); 5260 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 5261 " (aaaaaaa + aaaaa));", 5262 getLLVMStyleWithColumns(40)); 5263 verifyFormat( 5264 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 5265 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 5266 " bbbbbbbbbbbbbbbbbbbbbbb);"); 5267 } 5268 5269 TEST_F(FormatTest, UnderstandsEquals) { 5270 verifyFormat( 5271 "aaaaaaaaaaaaaaaaa =\n" 5272 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5273 verifyFormat( 5274 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5275 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5276 verifyFormat( 5277 "if (a) {\n" 5278 " f();\n" 5279 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5280 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5281 "}"); 5282 5283 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5284 " 100000000 + 10000000) {\n}"); 5285 } 5286 5287 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5288 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5289 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5290 5291 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5292 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5293 5294 verifyFormat( 5295 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5296 " Parameter2);"); 5297 5298 verifyFormat( 5299 "ShortObject->shortFunction(\n" 5300 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5301 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5302 5303 verifyFormat("loooooooooooooongFunction(\n" 5304 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5305 5306 verifyFormat( 5307 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5308 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5309 5310 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5311 " .WillRepeatedly(Return(SomeValue));"); 5312 verifyFormat("void f() {\n" 5313 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5314 " .Times(2)\n" 5315 " .WillRepeatedly(Return(SomeValue));\n" 5316 "}"); 5317 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5318 " ccccccccccccccccccccccc);"); 5319 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5320 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5321 " .aaaaa(aaaaa),\n" 5322 " aaaaaaaaaaaaaaaaaaaaa);"); 5323 verifyFormat("void f() {\n" 5324 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5325 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5326 "}"); 5327 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5328 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5329 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5330 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5331 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5332 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5333 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5334 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5335 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5336 "}"); 5337 5338 // Here, it is not necessary to wrap at "." or "->". 5339 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5340 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5341 verifyFormat( 5342 "aaaaaaaaaaa->aaaaaaaaa(\n" 5343 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5344 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5345 5346 verifyFormat( 5347 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5348 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5349 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5350 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5351 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5352 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5353 5354 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5355 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5356 " .a();"); 5357 5358 FormatStyle NoBinPacking = getLLVMStyle(); 5359 NoBinPacking.BinPackParameters = false; 5360 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5361 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5362 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5363 " aaaaaaaaaaaaaaaaaaa,\n" 5364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5365 NoBinPacking); 5366 5367 // If there is a subsequent call, change to hanging indentation. 5368 verifyFormat( 5369 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5370 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5371 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5372 verifyFormat( 5373 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5374 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5375 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5376 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5377 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5378 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5379 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5380 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5381 } 5382 5383 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5384 verifyFormat("template <typename T>\n" 5385 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5386 verifyFormat("template <typename T>\n" 5387 "// T should be one of {A, B}.\n" 5388 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5389 verifyFormat( 5390 "template <typename T>\n" 5391 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5392 verifyFormat("template <typename T>\n" 5393 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5394 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5395 verifyFormat( 5396 "template <typename T>\n" 5397 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5398 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5399 verifyFormat( 5400 "template <typename T>\n" 5401 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5402 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5403 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5404 verifyFormat("template <typename T>\n" 5405 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5406 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5407 verifyFormat( 5408 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5409 " typename T4 = char>\n" 5410 "void f();"); 5411 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5412 " template <typename> class cccccccccccccccccccccc,\n" 5413 " typename ddddddddddddd>\n" 5414 "class C {};"); 5415 verifyFormat( 5416 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5417 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5418 5419 verifyFormat("void f() {\n" 5420 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5421 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5422 "}"); 5423 5424 verifyFormat("template <typename T> class C {};"); 5425 verifyFormat("template <typename T> void f();"); 5426 verifyFormat("template <typename T> void f() {}"); 5427 verifyFormat( 5428 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5430 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5431 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5434 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5435 getLLVMStyleWithColumns(72)); 5436 EXPECT_EQ("static_cast<A< //\n" 5437 " B> *>(\n" 5438 "\n" 5439 ");", 5440 format("static_cast<A<//\n" 5441 " B>*>(\n" 5442 "\n" 5443 " );")); 5444 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5445 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5446 5447 FormatStyle AlwaysBreak = getLLVMStyle(); 5448 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5449 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5450 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5451 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5452 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5453 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5454 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5455 verifyFormat("template <template <typename> class Fooooooo,\n" 5456 " template <typename> class Baaaaaaar>\n" 5457 "struct C {};", 5458 AlwaysBreak); 5459 verifyFormat("template <typename T> // T can be A, B or C.\n" 5460 "struct C {};", 5461 AlwaysBreak); 5462 verifyFormat("template <enum E> class A {\n" 5463 "public:\n" 5464 " E *f();\n" 5465 "};"); 5466 } 5467 5468 TEST_F(FormatTest, WrapsTemplateParameters) { 5469 FormatStyle Style = getLLVMStyle(); 5470 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5471 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5472 verifyFormat( 5473 "template <typename... a> struct q {};\n" 5474 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5475 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5476 " y;", 5477 Style); 5478 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5479 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5480 verifyFormat( 5481 "template <typename... a> struct r {};\n" 5482 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5483 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5484 " y;", 5485 Style); 5486 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5487 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5488 verifyFormat( 5489 "template <typename... a> struct s {};\n" 5490 "extern s<\n" 5491 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5492 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5493 " y;", 5494 Style); 5495 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5496 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5497 verifyFormat( 5498 "template <typename... a> struct t {};\n" 5499 "extern t<\n" 5500 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5501 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5502 " y;", 5503 Style); 5504 } 5505 5506 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5507 verifyFormat( 5508 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5510 verifyFormat( 5511 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5512 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5514 5515 // FIXME: Should we have the extra indent after the second break? 5516 verifyFormat( 5517 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5519 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5520 5521 verifyFormat( 5522 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5523 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5524 5525 // Breaking at nested name specifiers is generally not desirable. 5526 verifyFormat( 5527 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5528 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5529 5530 verifyFormat( 5531 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 5532 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5533 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5534 " aaaaaaaaaaaaaaaaaaaaa);", 5535 getLLVMStyleWithColumns(74)); 5536 5537 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5538 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5539 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5540 } 5541 5542 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5543 verifyFormat("A<int> a;"); 5544 verifyFormat("A<A<A<int>>> a;"); 5545 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5546 verifyFormat("bool x = a < 1 || 2 > a;"); 5547 verifyFormat("bool x = 5 < f<int>();"); 5548 verifyFormat("bool x = f<int>() > 5;"); 5549 verifyFormat("bool x = 5 < a<int>::x;"); 5550 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5551 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5552 5553 verifyGoogleFormat("A<A<int>> a;"); 5554 verifyGoogleFormat("A<A<A<int>>> a;"); 5555 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5556 verifyGoogleFormat("A<A<int> > a;"); 5557 verifyGoogleFormat("A<A<A<int> > > a;"); 5558 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5559 verifyGoogleFormat("A<::A<int>> a;"); 5560 verifyGoogleFormat("A<::A> a;"); 5561 verifyGoogleFormat("A< ::A> a;"); 5562 verifyGoogleFormat("A< ::A<int> > a;"); 5563 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5564 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5565 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5566 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5567 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5568 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5569 5570 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5571 5572 verifyFormat("test >> a >> b;"); 5573 verifyFormat("test << a >> b;"); 5574 5575 verifyFormat("f<int>();"); 5576 verifyFormat("template <typename T> void f() {}"); 5577 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5578 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5579 "sizeof(char)>::type>;"); 5580 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5581 verifyFormat("f(a.operator()<A>());"); 5582 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5583 " .template operator()<A>());", 5584 getLLVMStyleWithColumns(35)); 5585 5586 // Not template parameters. 5587 verifyFormat("return a < b && c > d;"); 5588 verifyFormat("void f() {\n" 5589 " while (a < b && c > d) {\n" 5590 " }\n" 5591 "}"); 5592 verifyFormat("template <typename... Types>\n" 5593 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5594 5595 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5596 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5597 getLLVMStyleWithColumns(60)); 5598 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5599 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5600 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5601 } 5602 5603 TEST_F(FormatTest, BitshiftOperatorWidth) { 5604 EXPECT_EQ("int a = 1 << 2; /* foo\n" 5605 " bar */", 5606 format("int a=1<<2; /* foo\n" 5607 " bar */")); 5608 5609 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 5610 " bar */", 5611 format("int b =256>>1 ; /* foo\n" 5612 " bar */")); 5613 } 5614 5615 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5616 verifyFormat("COMPARE(a, ==, b);"); 5617 verifyFormat("auto s = sizeof...(Ts) - 1;"); 5618 } 5619 5620 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5621 verifyFormat("int A::*x;"); 5622 verifyFormat("int (S::*func)(void *);"); 5623 verifyFormat("void f() { int (S::*func)(void *); }"); 5624 verifyFormat("typedef bool *(Class::*Member)() const;"); 5625 verifyFormat("void f() {\n" 5626 " (a->*f)();\n" 5627 " a->*x;\n" 5628 " (a.*f)();\n" 5629 " ((*a).*f)();\n" 5630 " a.*x;\n" 5631 "}"); 5632 verifyFormat("void f() {\n" 5633 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5634 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5635 "}"); 5636 verifyFormat( 5637 "(aaaaaaaaaa->*bbbbbbb)(\n" 5638 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5639 FormatStyle Style = getLLVMStyle(); 5640 Style.PointerAlignment = FormatStyle::PAS_Left; 5641 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5642 } 5643 5644 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5645 verifyFormat("int a = -2;"); 5646 verifyFormat("f(-1, -2, -3);"); 5647 verifyFormat("a[-1] = 5;"); 5648 verifyFormat("int a = 5 + -2;"); 5649 verifyFormat("if (i == -1) {\n}"); 5650 verifyFormat("if (i != -1) {\n}"); 5651 verifyFormat("if (i > -1) {\n}"); 5652 verifyFormat("if (i < -1) {\n}"); 5653 verifyFormat("++(a->f());"); 5654 verifyFormat("--(a->f());"); 5655 verifyFormat("(a->f())++;"); 5656 verifyFormat("a[42]++;"); 5657 verifyFormat("if (!(a->f())) {\n}"); 5658 5659 verifyFormat("a-- > b;"); 5660 verifyFormat("b ? -a : c;"); 5661 verifyFormat("n * sizeof char16;"); 5662 verifyFormat("n * alignof char16;", getGoogleStyle()); 5663 verifyFormat("sizeof(char);"); 5664 verifyFormat("alignof(char);", getGoogleStyle()); 5665 5666 verifyFormat("return -1;"); 5667 verifyFormat("switch (a) {\n" 5668 "case -1:\n" 5669 " break;\n" 5670 "}"); 5671 verifyFormat("#define X -1"); 5672 verifyFormat("#define X -kConstant"); 5673 5674 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5675 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5676 5677 verifyFormat("int a = /* confusing comment */ -1;"); 5678 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5679 verifyFormat("int a = i /* confusing comment */++;"); 5680 } 5681 5682 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5683 verifyFormat("if (!aaaaaaaaaa( // break\n" 5684 " aaaaa)) {\n" 5685 "}"); 5686 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5687 " aaaaa));"); 5688 verifyFormat("*aaa = aaaaaaa( // break\n" 5689 " bbbbbb);"); 5690 } 5691 5692 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5693 verifyFormat("bool operator<();"); 5694 verifyFormat("bool operator>();"); 5695 verifyFormat("bool operator=();"); 5696 verifyFormat("bool operator==();"); 5697 verifyFormat("bool operator!=();"); 5698 verifyFormat("int operator+();"); 5699 verifyFormat("int operator++();"); 5700 verifyFormat("int operator++(int) volatile noexcept;"); 5701 verifyFormat("bool operator,();"); 5702 verifyFormat("bool operator();"); 5703 verifyFormat("bool operator()();"); 5704 verifyFormat("bool operator[]();"); 5705 verifyFormat("operator bool();"); 5706 verifyFormat("operator int();"); 5707 verifyFormat("operator void *();"); 5708 verifyFormat("operator SomeType<int>();"); 5709 verifyFormat("operator SomeType<int, int>();"); 5710 verifyFormat("operator SomeType<SomeType<int>>();"); 5711 verifyFormat("void *operator new(std::size_t size);"); 5712 verifyFormat("void *operator new[](std::size_t size);"); 5713 verifyFormat("void operator delete(void *ptr);"); 5714 verifyFormat("void operator delete[](void *ptr);"); 5715 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5716 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5717 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5718 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5719 5720 verifyFormat( 5721 "ostream &operator<<(ostream &OutputStream,\n" 5722 " SomeReallyLongType WithSomeReallyLongValue);"); 5723 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5724 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5725 " return left.group < right.group;\n" 5726 "}"); 5727 verifyFormat("SomeType &operator=(const SomeType &S);"); 5728 verifyFormat("f.template operator()<int>();"); 5729 5730 verifyGoogleFormat("operator void*();"); 5731 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5732 verifyGoogleFormat("operator ::A();"); 5733 5734 verifyFormat("using A::operator+;"); 5735 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5736 "int i;"); 5737 } 5738 5739 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5740 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5741 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5742 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5743 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5744 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5745 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5746 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5747 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5748 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5749 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5750 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5751 verifyFormat("void Fn(T const &) const &;"); 5752 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 5753 verifyFormat("template <typename T>\n" 5754 "void F(T) && = delete;", 5755 getGoogleStyle()); 5756 5757 FormatStyle AlignLeft = getLLVMStyle(); 5758 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5759 verifyFormat("void A::b() && {}", AlignLeft); 5760 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5761 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5762 AlignLeft); 5763 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5764 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5765 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5766 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5767 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5768 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5769 verifyFormat("void Fn(T const&) const&;", AlignLeft); 5770 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 5771 5772 FormatStyle Spaces = getLLVMStyle(); 5773 Spaces.SpacesInCStyleCastParentheses = true; 5774 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5775 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5776 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5777 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5778 5779 Spaces.SpacesInCStyleCastParentheses = false; 5780 Spaces.SpacesInParentheses = true; 5781 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5782 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5783 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5784 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5785 } 5786 5787 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5788 verifyFormat("void f() {\n" 5789 " A *a = new A;\n" 5790 " A *a = new (placement) A;\n" 5791 " delete a;\n" 5792 " delete (A *)a;\n" 5793 "}"); 5794 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5795 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5796 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5797 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5798 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5799 verifyFormat("delete[] h->p;"); 5800 } 5801 5802 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5803 verifyFormat("int *f(int *a) {}"); 5804 verifyFormat("int main(int argc, char **argv) {}"); 5805 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5806 verifyIndependentOfContext("f(a, *a);"); 5807 verifyFormat("void g() { f(*a); }"); 5808 verifyIndependentOfContext("int a = b * 10;"); 5809 verifyIndependentOfContext("int a = 10 * b;"); 5810 verifyIndependentOfContext("int a = b * c;"); 5811 verifyIndependentOfContext("int a += b * c;"); 5812 verifyIndependentOfContext("int a -= b * c;"); 5813 verifyIndependentOfContext("int a *= b * c;"); 5814 verifyIndependentOfContext("int a /= b * c;"); 5815 verifyIndependentOfContext("int a = *b;"); 5816 verifyIndependentOfContext("int a = *b * c;"); 5817 verifyIndependentOfContext("int a = b * *c;"); 5818 verifyIndependentOfContext("int a = b * (10);"); 5819 verifyIndependentOfContext("S << b * (10);"); 5820 verifyIndependentOfContext("return 10 * b;"); 5821 verifyIndependentOfContext("return *b * *c;"); 5822 verifyIndependentOfContext("return a & ~b;"); 5823 verifyIndependentOfContext("f(b ? *c : *d);"); 5824 verifyIndependentOfContext("int a = b ? *c : *d;"); 5825 verifyIndependentOfContext("*b = a;"); 5826 verifyIndependentOfContext("a * ~b;"); 5827 verifyIndependentOfContext("a * !b;"); 5828 verifyIndependentOfContext("a * +b;"); 5829 verifyIndependentOfContext("a * -b;"); 5830 verifyIndependentOfContext("a * ++b;"); 5831 verifyIndependentOfContext("a * --b;"); 5832 verifyIndependentOfContext("a[4] * b;"); 5833 verifyIndependentOfContext("a[a * a] = 1;"); 5834 verifyIndependentOfContext("f() * b;"); 5835 verifyIndependentOfContext("a * [self dostuff];"); 5836 verifyIndependentOfContext("int x = a * (a + b);"); 5837 verifyIndependentOfContext("(a *)(a + b);"); 5838 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5839 verifyIndependentOfContext("int *pa = (int *)&a;"); 5840 verifyIndependentOfContext("return sizeof(int **);"); 5841 verifyIndependentOfContext("return sizeof(int ******);"); 5842 verifyIndependentOfContext("return (int **&)a;"); 5843 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5844 verifyFormat("void f(Type (*parameter)[10]) {}"); 5845 verifyFormat("void f(Type (¶meter)[10]) {}"); 5846 verifyGoogleFormat("return sizeof(int**);"); 5847 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5848 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5849 verifyFormat("auto a = [](int **&, int ***) {};"); 5850 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5851 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5852 verifyFormat("[](const decltype(*a) &value) {}"); 5853 verifyFormat("decltype(a * b) F();"); 5854 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5855 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5856 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5857 verifyIndependentOfContext("int i{a * b};"); 5858 verifyIndependentOfContext("aaa && aaa->f();"); 5859 verifyIndependentOfContext("int x = ~*p;"); 5860 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5861 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5862 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5863 verifyFormat("void f() { f(a, c * d); }"); 5864 verifyFormat("void f() { f(new a(), c * d); }"); 5865 verifyFormat("void f(const MyOverride &override);"); 5866 verifyFormat("void f(const MyFinal &final);"); 5867 verifyIndependentOfContext("bool a = f() && override.f();"); 5868 verifyIndependentOfContext("bool a = f() && final.f();"); 5869 5870 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5871 5872 verifyIndependentOfContext("A<int *> a;"); 5873 verifyIndependentOfContext("A<int **> a;"); 5874 verifyIndependentOfContext("A<int *, int *> a;"); 5875 verifyIndependentOfContext("A<int *[]> a;"); 5876 verifyIndependentOfContext( 5877 "const char *const p = reinterpret_cast<const char *const>(q);"); 5878 verifyIndependentOfContext("A<int **, int **> a;"); 5879 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5880 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5881 verifyFormat("for (; a && b;) {\n}"); 5882 verifyFormat("bool foo = true && [] { return false; }();"); 5883 5884 verifyFormat( 5885 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5886 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5887 5888 verifyGoogleFormat("int const* a = &b;"); 5889 verifyGoogleFormat("**outparam = 1;"); 5890 verifyGoogleFormat("*outparam = a * b;"); 5891 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5892 verifyGoogleFormat("A<int*> a;"); 5893 verifyGoogleFormat("A<int**> a;"); 5894 verifyGoogleFormat("A<int*, int*> a;"); 5895 verifyGoogleFormat("A<int**, int**> a;"); 5896 verifyGoogleFormat("f(b ? *c : *d);"); 5897 verifyGoogleFormat("int a = b ? *c : *d;"); 5898 verifyGoogleFormat("Type* t = **x;"); 5899 verifyGoogleFormat("Type* t = *++*x;"); 5900 verifyGoogleFormat("*++*x;"); 5901 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5902 verifyGoogleFormat("Type* t = x++ * y;"); 5903 verifyGoogleFormat( 5904 "const char* const p = reinterpret_cast<const char* const>(q);"); 5905 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5906 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5907 verifyGoogleFormat("template <typename T>\n" 5908 "void f(int i = 0, SomeType** temps = NULL);"); 5909 5910 FormatStyle Left = getLLVMStyle(); 5911 Left.PointerAlignment = FormatStyle::PAS_Left; 5912 verifyFormat("x = *a(x) = *a(y);", Left); 5913 verifyFormat("for (;; *a = b) {\n}", Left); 5914 verifyFormat("return *this += 1;", Left); 5915 verifyFormat("throw *x;", Left); 5916 verifyFormat("delete *x;", Left); 5917 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 5918 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 5919 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 5920 5921 verifyIndependentOfContext("a = *(x + y);"); 5922 verifyIndependentOfContext("a = &(x + y);"); 5923 verifyIndependentOfContext("*(x + y).call();"); 5924 verifyIndependentOfContext("&(x + y)->call();"); 5925 verifyFormat("void f() { &(*I).first; }"); 5926 5927 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5928 verifyFormat( 5929 "int *MyValues = {\n" 5930 " *A, // Operator detection might be confused by the '{'\n" 5931 " *BB // Operator detection might be confused by previous comment\n" 5932 "};"); 5933 5934 verifyIndependentOfContext("if (int *a = &b)"); 5935 verifyIndependentOfContext("if (int &a = *b)"); 5936 verifyIndependentOfContext("if (a & b[i])"); 5937 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5938 verifyIndependentOfContext("if (*b[i])"); 5939 verifyIndependentOfContext("if (int *a = (&b))"); 5940 verifyIndependentOfContext("while (int *a = &b)"); 5941 verifyIndependentOfContext("size = sizeof *a;"); 5942 verifyIndependentOfContext("if (a && (b = c))"); 5943 verifyFormat("void f() {\n" 5944 " for (const int &v : Values) {\n" 5945 " }\n" 5946 "}"); 5947 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5948 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5949 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5950 5951 verifyFormat("#define A (!a * b)"); 5952 verifyFormat("#define MACRO \\\n" 5953 " int *i = a * b; \\\n" 5954 " void f(a *b);", 5955 getLLVMStyleWithColumns(19)); 5956 5957 verifyIndependentOfContext("A = new SomeType *[Length];"); 5958 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5959 verifyIndependentOfContext("T **t = new T *;"); 5960 verifyIndependentOfContext("T **t = new T *();"); 5961 verifyGoogleFormat("A = new SomeType*[Length]();"); 5962 verifyGoogleFormat("A = new SomeType*[Length];"); 5963 verifyGoogleFormat("T** t = new T*;"); 5964 verifyGoogleFormat("T** t = new T*();"); 5965 5966 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5967 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5968 verifyFormat("template <bool a, bool b> " 5969 "typename t::if<x && y>::type f() {}"); 5970 verifyFormat("template <int *y> f() {}"); 5971 verifyFormat("vector<int *> v;"); 5972 verifyFormat("vector<int *const> v;"); 5973 verifyFormat("vector<int *const **const *> v;"); 5974 verifyFormat("vector<int *volatile> v;"); 5975 verifyFormat("vector<a * b> v;"); 5976 verifyFormat("foo<b && false>();"); 5977 verifyFormat("foo<b & 1>();"); 5978 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5979 verifyFormat( 5980 "template <class T, class = typename std::enable_if<\n" 5981 " std::is_integral<T>::value &&\n" 5982 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5983 "void F();", 5984 getLLVMStyleWithColumns(70)); 5985 verifyFormat( 5986 "template <class T,\n" 5987 " class = typename std::enable_if<\n" 5988 " std::is_integral<T>::value &&\n" 5989 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 5990 " class U>\n" 5991 "void F();", 5992 getLLVMStyleWithColumns(70)); 5993 verifyFormat( 5994 "template <class T,\n" 5995 " class = typename ::std::enable_if<\n" 5996 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5997 "void F();", 5998 getGoogleStyleWithColumns(68)); 5999 6000 verifyIndependentOfContext("MACRO(int *i);"); 6001 verifyIndependentOfContext("MACRO(auto *a);"); 6002 verifyIndependentOfContext("MACRO(const A *a);"); 6003 verifyIndependentOfContext("MACRO(A *const a);"); 6004 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 6005 verifyFormat("void f() { f(float{1}, a * a); }"); 6006 // FIXME: Is there a way to make this work? 6007 // verifyIndependentOfContext("MACRO(A *a);"); 6008 6009 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 6010 verifyFormat("return options != nullptr && operator==(*options);"); 6011 6012 EXPECT_EQ("#define OP(x) \\\n" 6013 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6014 " return s << a.DebugString(); \\\n" 6015 " }", 6016 format("#define OP(x) \\\n" 6017 " ostream &operator<<(ostream &s, const A &a) { \\\n" 6018 " return s << a.DebugString(); \\\n" 6019 " }", 6020 getLLVMStyleWithColumns(50))); 6021 6022 // FIXME: We cannot handle this case yet; we might be able to figure out that 6023 // foo<x> d > v; doesn't make sense. 6024 verifyFormat("foo<a<b && c> d> v;"); 6025 6026 FormatStyle PointerMiddle = getLLVMStyle(); 6027 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 6028 verifyFormat("delete *x;", PointerMiddle); 6029 verifyFormat("int * x;", PointerMiddle); 6030 verifyFormat("template <int * y> f() {}", PointerMiddle); 6031 verifyFormat("int * f(int * a) {}", PointerMiddle); 6032 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 6033 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 6034 verifyFormat("A<int *> a;", PointerMiddle); 6035 verifyFormat("A<int **> a;", PointerMiddle); 6036 verifyFormat("A<int *, int *> a;", PointerMiddle); 6037 verifyFormat("A<int * []> a;", PointerMiddle); 6038 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 6039 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 6040 verifyFormat("T ** t = new T *;", PointerMiddle); 6041 6042 // Member function reference qualifiers aren't binary operators. 6043 verifyFormat("string // break\n" 6044 "operator()() & {}"); 6045 verifyFormat("string // break\n" 6046 "operator()() && {}"); 6047 verifyGoogleFormat("template <typename T>\n" 6048 "auto x() & -> int {}"); 6049 } 6050 6051 TEST_F(FormatTest, UnderstandsAttributes) { 6052 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 6053 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 6054 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 6055 FormatStyle AfterType = getLLVMStyle(); 6056 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 6057 verifyFormat("__attribute__((nodebug)) void\n" 6058 "foo() {}\n", 6059 AfterType); 6060 } 6061 6062 TEST_F(FormatTest, UnderstandsEllipsis) { 6063 verifyFormat("int printf(const char *fmt, ...);"); 6064 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 6065 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 6066 6067 FormatStyle PointersLeft = getLLVMStyle(); 6068 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 6069 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 6070 } 6071 6072 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 6073 EXPECT_EQ("int *a;\n" 6074 "int *a;\n" 6075 "int *a;", 6076 format("int *a;\n" 6077 "int* a;\n" 6078 "int *a;", 6079 getGoogleStyle())); 6080 EXPECT_EQ("int* a;\n" 6081 "int* a;\n" 6082 "int* a;", 6083 format("int* a;\n" 6084 "int* a;\n" 6085 "int *a;", 6086 getGoogleStyle())); 6087 EXPECT_EQ("int *a;\n" 6088 "int *a;\n" 6089 "int *a;", 6090 format("int *a;\n" 6091 "int * a;\n" 6092 "int * a;", 6093 getGoogleStyle())); 6094 EXPECT_EQ("auto x = [] {\n" 6095 " int *a;\n" 6096 " int *a;\n" 6097 " int *a;\n" 6098 "};", 6099 format("auto x=[]{int *a;\n" 6100 "int * a;\n" 6101 "int * a;};", 6102 getGoogleStyle())); 6103 } 6104 6105 TEST_F(FormatTest, UnderstandsRvalueReferences) { 6106 verifyFormat("int f(int &&a) {}"); 6107 verifyFormat("int f(int a, char &&b) {}"); 6108 verifyFormat("void f() { int &&a = b; }"); 6109 verifyGoogleFormat("int f(int a, char&& b) {}"); 6110 verifyGoogleFormat("void f() { int&& a = b; }"); 6111 6112 verifyIndependentOfContext("A<int &&> a;"); 6113 verifyIndependentOfContext("A<int &&, int &&> a;"); 6114 verifyGoogleFormat("A<int&&> a;"); 6115 verifyGoogleFormat("A<int&&, int&&> a;"); 6116 6117 // Not rvalue references: 6118 verifyFormat("template <bool B, bool C> class A {\n" 6119 " static_assert(B && C, \"Something is wrong\");\n" 6120 "};"); 6121 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 6122 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 6123 verifyFormat("#define A(a, b) (a && b)"); 6124 } 6125 6126 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 6127 verifyFormat("void f() {\n" 6128 " x[aaaaaaaaa -\n" 6129 " b] = 23;\n" 6130 "}", 6131 getLLVMStyleWithColumns(15)); 6132 } 6133 6134 TEST_F(FormatTest, FormatsCasts) { 6135 verifyFormat("Type *A = static_cast<Type *>(P);"); 6136 verifyFormat("Type *A = (Type *)P;"); 6137 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 6138 verifyFormat("int a = (int)(2.0f);"); 6139 verifyFormat("int a = (int)2.0f;"); 6140 verifyFormat("x[(int32)y];"); 6141 verifyFormat("x = (int32)y;"); 6142 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 6143 verifyFormat("int a = (int)*b;"); 6144 verifyFormat("int a = (int)2.0f;"); 6145 verifyFormat("int a = (int)~0;"); 6146 verifyFormat("int a = (int)++a;"); 6147 verifyFormat("int a = (int)sizeof(int);"); 6148 verifyFormat("int a = (int)+2;"); 6149 verifyFormat("my_int a = (my_int)2.0f;"); 6150 verifyFormat("my_int a = (my_int)sizeof(int);"); 6151 verifyFormat("return (my_int)aaa;"); 6152 verifyFormat("#define x ((int)-1)"); 6153 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 6154 verifyFormat("#define p(q) ((int *)&q)"); 6155 verifyFormat("fn(a)(b) + 1;"); 6156 6157 verifyFormat("void f() { my_int a = (my_int)*b; }"); 6158 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 6159 verifyFormat("my_int a = (my_int)~0;"); 6160 verifyFormat("my_int a = (my_int)++a;"); 6161 verifyFormat("my_int a = (my_int)-2;"); 6162 verifyFormat("my_int a = (my_int)1;"); 6163 verifyFormat("my_int a = (my_int *)1;"); 6164 verifyFormat("my_int a = (const my_int)-1;"); 6165 verifyFormat("my_int a = (const my_int *)-1;"); 6166 verifyFormat("my_int a = (my_int)(my_int)-1;"); 6167 verifyFormat("my_int a = (ns::my_int)-2;"); 6168 verifyFormat("case (my_int)ONE:"); 6169 verifyFormat("auto x = (X)this;"); 6170 6171 // FIXME: single value wrapped with paren will be treated as cast. 6172 verifyFormat("void f(int i = (kValue)*kMask) {}"); 6173 6174 verifyFormat("{ (void)F; }"); 6175 6176 // Don't break after a cast's 6177 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6178 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 6179 " bbbbbbbbbbbbbbbbbbbbbb);"); 6180 6181 // These are not casts. 6182 verifyFormat("void f(int *) {}"); 6183 verifyFormat("f(foo)->b;"); 6184 verifyFormat("f(foo).b;"); 6185 verifyFormat("f(foo)(b);"); 6186 verifyFormat("f(foo)[b];"); 6187 verifyFormat("[](foo) { return 4; }(bar);"); 6188 verifyFormat("(*funptr)(foo)[4];"); 6189 verifyFormat("funptrs[4](foo)[4];"); 6190 verifyFormat("void f(int *);"); 6191 verifyFormat("void f(int *) = 0;"); 6192 verifyFormat("void f(SmallVector<int>) {}"); 6193 verifyFormat("void f(SmallVector<int>);"); 6194 verifyFormat("void f(SmallVector<int>) = 0;"); 6195 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 6196 verifyFormat("int a = sizeof(int) * b;"); 6197 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 6198 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 6199 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 6200 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 6201 6202 // These are not casts, but at some point were confused with casts. 6203 verifyFormat("virtual void foo(int *) override;"); 6204 verifyFormat("virtual void foo(char &) const;"); 6205 verifyFormat("virtual void foo(int *a, char *) const;"); 6206 verifyFormat("int a = sizeof(int *) + b;"); 6207 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 6208 verifyFormat("bool b = f(g<int>) && c;"); 6209 verifyFormat("typedef void (*f)(int i) func;"); 6210 6211 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 6212 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 6213 // FIXME: The indentation here is not ideal. 6214 verifyFormat( 6215 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6216 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 6217 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 6218 } 6219 6220 TEST_F(FormatTest, FormatsFunctionTypes) { 6221 verifyFormat("A<bool()> a;"); 6222 verifyFormat("A<SomeType()> a;"); 6223 verifyFormat("A<void (*)(int, std::string)> a;"); 6224 verifyFormat("A<void *(int)>;"); 6225 verifyFormat("void *(*a)(int *, SomeType *);"); 6226 verifyFormat("int (*func)(void *);"); 6227 verifyFormat("void f() { int (*func)(void *); }"); 6228 verifyFormat("template <class CallbackClass>\n" 6229 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 6230 6231 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 6232 verifyGoogleFormat("void* (*a)(int);"); 6233 verifyGoogleFormat( 6234 "template <class CallbackClass>\n" 6235 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 6236 6237 // Other constructs can look somewhat like function types: 6238 verifyFormat("A<sizeof(*x)> a;"); 6239 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 6240 verifyFormat("some_var = function(*some_pointer_var)[0];"); 6241 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 6242 verifyFormat("int x = f(&h)();"); 6243 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 6244 verifyFormat("std::function<\n" 6245 " LooooooooooongTemplatedType<\n" 6246 " SomeType>*(\n" 6247 " LooooooooooooooooongType type)>\n" 6248 " function;", 6249 getGoogleStyleWithColumns(40)); 6250 } 6251 6252 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 6253 verifyFormat("A (*foo_)[6];"); 6254 verifyFormat("vector<int> (*foo_)[6];"); 6255 } 6256 6257 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 6258 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6259 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6260 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 6261 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6262 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6263 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 6264 6265 // Different ways of ()-initializiation. 6266 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6267 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 6268 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6269 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 6270 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6271 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 6272 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 6273 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 6274 6275 // Lambdas should not confuse the variable declaration heuristic. 6276 verifyFormat("LooooooooooooooooongType\n" 6277 " variable(nullptr, [](A *a) {});", 6278 getLLVMStyleWithColumns(40)); 6279 } 6280 6281 TEST_F(FormatTest, BreaksLongDeclarations) { 6282 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 6283 " AnotherNameForTheLongType;"); 6284 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 6285 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6286 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6287 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6288 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 6289 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 6290 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6291 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6292 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 6293 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6294 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6295 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6296 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6297 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 6298 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6299 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 6300 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6301 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 6302 FormatStyle Indented = getLLVMStyle(); 6303 Indented.IndentWrappedFunctionNames = true; 6304 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6305 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 6306 Indented); 6307 verifyFormat( 6308 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 6309 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6310 Indented); 6311 verifyFormat( 6312 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 6313 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6314 Indented); 6315 verifyFormat( 6316 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 6317 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 6318 Indented); 6319 6320 // FIXME: Without the comment, this breaks after "(". 6321 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 6322 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 6323 getGoogleStyle()); 6324 6325 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 6326 " int LoooooooooooooooooooongParam2) {}"); 6327 verifyFormat( 6328 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 6329 " SourceLocation L, IdentifierIn *II,\n" 6330 " Type *T) {}"); 6331 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 6332 "ReallyReaaallyLongFunctionName(\n" 6333 " const std::string &SomeParameter,\n" 6334 " const SomeType<string, SomeOtherTemplateParameter>\n" 6335 " &ReallyReallyLongParameterName,\n" 6336 " const SomeType<string, SomeOtherTemplateParameter>\n" 6337 " &AnotherLongParameterName) {}"); 6338 verifyFormat("template <typename A>\n" 6339 "SomeLoooooooooooooooooooooongType<\n" 6340 " typename some_namespace::SomeOtherType<A>::Type>\n" 6341 "Function() {}"); 6342 6343 verifyGoogleFormat( 6344 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 6345 " aaaaaaaaaaaaaaaaaaaaaaa;"); 6346 verifyGoogleFormat( 6347 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 6348 " SourceLocation L) {}"); 6349 verifyGoogleFormat( 6350 "some_namespace::LongReturnType\n" 6351 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 6352 " int first_long_parameter, int second_parameter) {}"); 6353 6354 verifyGoogleFormat("template <typename T>\n" 6355 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6356 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 6357 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6358 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 6359 6360 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 6361 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6362 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6363 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6364 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6365 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 6366 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6367 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6368 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6369 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6370 6371 verifyFormat("template <typename T> // Templates on own line.\n" 6372 "static int // Some comment.\n" 6373 "MyFunction(int a);", 6374 getLLVMStyle()); 6375 } 6376 6377 TEST_F(FormatTest, FormatsArrays) { 6378 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6379 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6380 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 6381 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 6382 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6383 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 6384 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6385 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6386 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6387 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6388 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6389 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6390 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6391 verifyFormat( 6392 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6393 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6394 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6395 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 6396 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6397 6398 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6399 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6400 verifyFormat( 6401 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6402 " .aaaaaaa[0]\n" 6403 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6404 verifyFormat("a[::b::c];"); 6405 6406 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6407 6408 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 6409 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 6410 } 6411 6412 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6413 verifyFormat("(a)->b();"); 6414 verifyFormat("--a;"); 6415 } 6416 6417 TEST_F(FormatTest, HandlesIncludeDirectives) { 6418 verifyFormat("#include <string>\n" 6419 "#include <a/b/c.h>\n" 6420 "#include \"a/b/string\"\n" 6421 "#include \"string.h\"\n" 6422 "#include \"string.h\"\n" 6423 "#include <a-a>\n" 6424 "#include < path with space >\n" 6425 "#include_next <test.h>" 6426 "#include \"abc.h\" // this is included for ABC\n" 6427 "#include \"some long include\" // with a comment\n" 6428 "#include \"some very long include path\"\n" 6429 "#include <some/very/long/include/path>\n", 6430 getLLVMStyleWithColumns(35)); 6431 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6432 EXPECT_EQ("#include <a>", format("#include<a>")); 6433 6434 verifyFormat("#import <string>"); 6435 verifyFormat("#import <a/b/c.h>"); 6436 verifyFormat("#import \"a/b/string\""); 6437 verifyFormat("#import \"string.h\""); 6438 verifyFormat("#import \"string.h\""); 6439 verifyFormat("#if __has_include(<strstream>)\n" 6440 "#include <strstream>\n" 6441 "#endif"); 6442 6443 verifyFormat("#define MY_IMPORT <a/b>"); 6444 6445 verifyFormat("#if __has_include(<a/b>)"); 6446 verifyFormat("#if __has_include_next(<a/b>)"); 6447 verifyFormat("#define F __has_include(<a/b>)"); 6448 verifyFormat("#define F __has_include_next(<a/b>)"); 6449 6450 // Protocol buffer definition or missing "#". 6451 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6452 getLLVMStyleWithColumns(30)); 6453 6454 FormatStyle Style = getLLVMStyle(); 6455 Style.AlwaysBreakBeforeMultilineStrings = true; 6456 Style.ColumnLimit = 0; 6457 verifyFormat("#import \"abc.h\"", Style); 6458 6459 // But 'import' might also be a regular C++ namespace. 6460 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6461 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6462 } 6463 6464 //===----------------------------------------------------------------------===// 6465 // Error recovery tests. 6466 //===----------------------------------------------------------------------===// 6467 6468 TEST_F(FormatTest, IncompleteParameterLists) { 6469 FormatStyle NoBinPacking = getLLVMStyle(); 6470 NoBinPacking.BinPackParameters = false; 6471 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6472 " double *min_x,\n" 6473 " double *max_x,\n" 6474 " double *min_y,\n" 6475 " double *max_y,\n" 6476 " double *min_z,\n" 6477 " double *max_z, ) {}", 6478 NoBinPacking); 6479 } 6480 6481 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6482 verifyFormat("void f() { return; }\n42"); 6483 verifyFormat("void f() {\n" 6484 " if (0)\n" 6485 " return;\n" 6486 "}\n" 6487 "42"); 6488 verifyFormat("void f() { return }\n42"); 6489 verifyFormat("void f() {\n" 6490 " if (0)\n" 6491 " return\n" 6492 "}\n" 6493 "42"); 6494 } 6495 6496 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6497 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6498 EXPECT_EQ("void f() {\n" 6499 " if (a)\n" 6500 " return\n" 6501 "}", 6502 format("void f ( ) { if ( a ) return }")); 6503 EXPECT_EQ("namespace N {\n" 6504 "void f()\n" 6505 "}", 6506 format("namespace N { void f() }")); 6507 EXPECT_EQ("namespace N {\n" 6508 "void f() {}\n" 6509 "void g()\n" 6510 "} // namespace N", 6511 format("namespace N { void f( ) { } void g( ) }")); 6512 } 6513 6514 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6515 verifyFormat("int aaaaaaaa =\n" 6516 " // Overlylongcomment\n" 6517 " b;", 6518 getLLVMStyleWithColumns(20)); 6519 verifyFormat("function(\n" 6520 " ShortArgument,\n" 6521 " LoooooooooooongArgument);\n", 6522 getLLVMStyleWithColumns(20)); 6523 } 6524 6525 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6526 verifyFormat("public:"); 6527 verifyFormat("class A {\n" 6528 "public\n" 6529 " void f() {}\n" 6530 "};"); 6531 verifyFormat("public\n" 6532 "int qwerty;"); 6533 verifyFormat("public\n" 6534 "B {}"); 6535 verifyFormat("public\n" 6536 "{}"); 6537 verifyFormat("public\n" 6538 "B { int x; }"); 6539 } 6540 6541 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6542 verifyFormat("{"); 6543 verifyFormat("#})"); 6544 verifyNoCrash("(/**/[:!] ?[)."); 6545 } 6546 6547 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6548 verifyFormat("do {\n}"); 6549 verifyFormat("do {\n}\n" 6550 "f();"); 6551 verifyFormat("do {\n}\n" 6552 "wheeee(fun);"); 6553 verifyFormat("do {\n" 6554 " f();\n" 6555 "}"); 6556 } 6557 6558 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6559 verifyFormat("if {\n foo;\n foo();\n}"); 6560 verifyFormat("switch {\n foo;\n foo();\n}"); 6561 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6562 verifyFormat("while {\n foo;\n foo();\n}"); 6563 verifyFormat("do {\n foo;\n foo();\n} while;"); 6564 } 6565 6566 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6567 verifyIncompleteFormat("namespace {\n" 6568 "class Foo { Foo (\n" 6569 "};\n" 6570 "} // namespace"); 6571 } 6572 6573 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6574 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6575 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6576 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6577 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6578 6579 EXPECT_EQ("{\n" 6580 " {\n" 6581 " breakme(\n" 6582 " qwe);\n" 6583 " }\n", 6584 format("{\n" 6585 " {\n" 6586 " breakme(qwe);\n" 6587 "}\n", 6588 getLLVMStyleWithColumns(10))); 6589 } 6590 6591 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6592 verifyFormat("int x = {\n" 6593 " avariable,\n" 6594 " b(alongervariable)};", 6595 getLLVMStyleWithColumns(25)); 6596 } 6597 6598 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6599 verifyFormat("return (a)(b){1, 2, 3};"); 6600 } 6601 6602 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6603 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6604 verifyFormat("vector<int> x{\n" 6605 " 1,\n" 6606 " 2,\n" 6607 " 3,\n" 6608 " 4,\n" 6609 "};"); 6610 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6611 verifyFormat("f({1, 2});"); 6612 verifyFormat("auto v = Foo{-1};"); 6613 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6614 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6615 verifyFormat("new vector<int>{1, 2, 3};"); 6616 verifyFormat("new int[3]{1, 2, 3};"); 6617 verifyFormat("new int{1};"); 6618 verifyFormat("return {arg1, arg2};"); 6619 verifyFormat("return {arg1, SomeType{parameter}};"); 6620 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6621 verifyFormat("new T{arg1, arg2};"); 6622 verifyFormat("f(MyMap[{composite, key}]);"); 6623 verifyFormat("class Class {\n" 6624 " T member = {arg1, arg2};\n" 6625 "};"); 6626 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6627 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 6628 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 6629 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6630 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6631 6632 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6633 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6634 verifyFormat("auto i = decltype(x){};"); 6635 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6636 verifyFormat("Node n{1, Node{1000}, //\n" 6637 " 2};"); 6638 verifyFormat("Aaaa aaaaaaa{\n" 6639 " {\n" 6640 " aaaa,\n" 6641 " },\n" 6642 "};"); 6643 verifyFormat("class C : public D {\n" 6644 " SomeClass SC{2};\n" 6645 "};"); 6646 verifyFormat("class C : public A {\n" 6647 " class D : public B {\n" 6648 " void f() { int i{2}; }\n" 6649 " };\n" 6650 "};"); 6651 verifyFormat("#define A {a, a},"); 6652 6653 // Binpacking only if there is no trailing comma 6654 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 6655 " cccccccccc, dddddddddd};", 6656 getLLVMStyleWithColumns(50)); 6657 verifyFormat("const Aaaaaa aaaaa = {\n" 6658 " aaaaaaaaaaa,\n" 6659 " bbbbbbbbbbb,\n" 6660 " ccccccccccc,\n" 6661 " ddddddddddd,\n" 6662 "};", getLLVMStyleWithColumns(50)); 6663 6664 // Cases where distinguising braced lists and blocks is hard. 6665 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 6666 verifyFormat("void f() {\n" 6667 " return; // comment\n" 6668 "}\n" 6669 "SomeType t;"); 6670 verifyFormat("void f() {\n" 6671 " if (a) {\n" 6672 " f();\n" 6673 " }\n" 6674 "}\n" 6675 "SomeType t;"); 6676 6677 // In combination with BinPackArguments = false. 6678 FormatStyle NoBinPacking = getLLVMStyle(); 6679 NoBinPacking.BinPackArguments = false; 6680 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6681 " bbbbb,\n" 6682 " ccccc,\n" 6683 " ddddd,\n" 6684 " eeeee,\n" 6685 " ffffff,\n" 6686 " ggggg,\n" 6687 " hhhhhh,\n" 6688 " iiiiii,\n" 6689 " jjjjjj,\n" 6690 " kkkkkk};", 6691 NoBinPacking); 6692 verifyFormat("const Aaaaaa aaaaa = {\n" 6693 " aaaaa,\n" 6694 " bbbbb,\n" 6695 " ccccc,\n" 6696 " ddddd,\n" 6697 " eeeee,\n" 6698 " ffffff,\n" 6699 " ggggg,\n" 6700 " hhhhhh,\n" 6701 " iiiiii,\n" 6702 " jjjjjj,\n" 6703 " kkkkkk,\n" 6704 "};", 6705 NoBinPacking); 6706 verifyFormat( 6707 "const Aaaaaa aaaaa = {\n" 6708 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6709 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6710 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6711 "};", 6712 NoBinPacking); 6713 6714 // FIXME: The alignment of these trailing comments might be bad. Then again, 6715 // this might be utterly useless in real code. 6716 verifyFormat("Constructor::Constructor()\n" 6717 " : some_value{ //\n" 6718 " aaaaaaa, //\n" 6719 " bbbbbbb} {}"); 6720 6721 // In braced lists, the first comment is always assumed to belong to the 6722 // first element. Thus, it can be moved to the next or previous line as 6723 // appropriate. 6724 EXPECT_EQ("function({// First element:\n" 6725 " 1,\n" 6726 " // Second element:\n" 6727 " 2});", 6728 format("function({\n" 6729 " // First element:\n" 6730 " 1,\n" 6731 " // Second element:\n" 6732 " 2});")); 6733 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6734 " // First element:\n" 6735 " 1,\n" 6736 " // Second element:\n" 6737 " 2};", 6738 format("std::vector<int> MyNumbers{// First element:\n" 6739 " 1,\n" 6740 " // Second element:\n" 6741 " 2};", 6742 getLLVMStyleWithColumns(30))); 6743 // A trailing comma should still lead to an enforced line break and no 6744 // binpacking. 6745 EXPECT_EQ("vector<int> SomeVector = {\n" 6746 " // aaa\n" 6747 " 1,\n" 6748 " 2,\n" 6749 "};", 6750 format("vector<int> SomeVector = { // aaa\n" 6751 " 1, 2, };")); 6752 6753 FormatStyle ExtraSpaces = getLLVMStyle(); 6754 ExtraSpaces.Cpp11BracedListStyle = false; 6755 ExtraSpaces.ColumnLimit = 75; 6756 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6757 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6758 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6759 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6760 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6761 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6762 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6763 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6764 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6765 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6766 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6767 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6768 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6769 verifyFormat("class Class {\n" 6770 " T member = { arg1, arg2 };\n" 6771 "};", 6772 ExtraSpaces); 6773 verifyFormat( 6774 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6775 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6776 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6777 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6778 ExtraSpaces); 6779 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6780 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6781 ExtraSpaces); 6782 verifyFormat( 6783 "someFunction(OtherParam,\n" 6784 " BracedList{ // comment 1 (Forcing interesting break)\n" 6785 " param1, param2,\n" 6786 " // comment 2\n" 6787 " param3, param4 });", 6788 ExtraSpaces); 6789 verifyFormat( 6790 "std::this_thread::sleep_for(\n" 6791 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6792 ExtraSpaces); 6793 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 6794 " aaaaaaa,\n" 6795 " aaaaaaaaaa,\n" 6796 " aaaaa,\n" 6797 " aaaaaaaaaaaaaaa,\n" 6798 " aaa,\n" 6799 " aaaaaaaaaa,\n" 6800 " a,\n" 6801 " aaaaaaaaaaaaaaaaaaaaa,\n" 6802 " aaaaaaaaaaaa,\n" 6803 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6804 " aaaaaaa,\n" 6805 " a};"); 6806 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6807 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 6808 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 6809 } 6810 6811 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6812 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6813 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6814 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6815 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6816 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6817 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6818 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6819 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6820 " 1, 22, 333, 4444, 55555, //\n" 6821 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6822 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6823 verifyFormat( 6824 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6825 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6826 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6827 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6828 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6829 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6830 " 7777777};"); 6831 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6832 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6833 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6834 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6835 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6836 " // Separating comment.\n" 6837 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6838 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6839 " // Leading comment\n" 6840 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6841 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6842 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6843 " 1, 1, 1, 1};", 6844 getLLVMStyleWithColumns(39)); 6845 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6846 " 1, 1, 1, 1};", 6847 getLLVMStyleWithColumns(38)); 6848 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6849 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6850 getLLVMStyleWithColumns(43)); 6851 verifyFormat( 6852 "static unsigned SomeValues[10][3] = {\n" 6853 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6854 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6855 verifyFormat("static auto fields = new vector<string>{\n" 6856 " \"aaaaaaaaaaaaa\",\n" 6857 " \"aaaaaaaaaaaaa\",\n" 6858 " \"aaaaaaaaaaaa\",\n" 6859 " \"aaaaaaaaaaaaaa\",\n" 6860 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6861 " \"aaaaaaaaaaaa\",\n" 6862 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6863 "};"); 6864 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6865 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6866 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6867 " 3, cccccccccccccccccccccc};", 6868 getLLVMStyleWithColumns(60)); 6869 6870 // Trailing commas. 6871 verifyFormat("vector<int> x = {\n" 6872 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6873 "};", 6874 getLLVMStyleWithColumns(39)); 6875 verifyFormat("vector<int> x = {\n" 6876 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6877 "};", 6878 getLLVMStyleWithColumns(39)); 6879 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6880 " 1, 1, 1, 1,\n" 6881 " /**/ /**/};", 6882 getLLVMStyleWithColumns(39)); 6883 6884 // Trailing comment in the first line. 6885 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6886 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6887 " 111111111, 222222222, 3333333333, 444444444, //\n" 6888 " 11111111, 22222222, 333333333, 44444444};"); 6889 // Trailing comment in the last line. 6890 verifyFormat("int aaaaa[] = {\n" 6891 " 1, 2, 3, // comment\n" 6892 " 4, 5, 6 // comment\n" 6893 "};"); 6894 6895 // With nested lists, we should either format one item per line or all nested 6896 // lists one on line. 6897 // FIXME: For some nested lists, we can do better. 6898 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6899 " {aaaaaaaaaaaaaaaaaaa},\n" 6900 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6901 " {aaaaaaaaaaaaaaaaa}};", 6902 getLLVMStyleWithColumns(60)); 6903 verifyFormat( 6904 "SomeStruct my_struct_array = {\n" 6905 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6906 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6907 " {aaa, aaa},\n" 6908 " {aaa, aaa},\n" 6909 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6910 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6911 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6912 6913 // No column layout should be used here. 6914 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6915 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6916 6917 verifyNoCrash("a<,"); 6918 6919 // No braced initializer here. 6920 verifyFormat("void f() {\n" 6921 " struct Dummy {};\n" 6922 " f(v);\n" 6923 "}"); 6924 6925 // Long lists should be formatted in columns even if they are nested. 6926 verifyFormat( 6927 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6928 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6929 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6930 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6931 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6932 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6933 6934 // Allow "single-column" layout even if that violates the column limit. There 6935 // isn't going to be a better way. 6936 verifyFormat("std::vector<int> a = {\n" 6937 " aaaaaaaa,\n" 6938 " aaaaaaaa,\n" 6939 " aaaaaaaa,\n" 6940 " aaaaaaaa,\n" 6941 " aaaaaaaaaa,\n" 6942 " aaaaaaaa,\n" 6943 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 6944 getLLVMStyleWithColumns(30)); 6945 verifyFormat("vector<int> aaaa = {\n" 6946 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6947 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6948 " aaaaaa.aaaaaaa,\n" 6949 " aaaaaa.aaaaaaa,\n" 6950 " aaaaaa.aaaaaaa,\n" 6951 " aaaaaa.aaaaaaa,\n" 6952 "};"); 6953 6954 // Don't create hanging lists. 6955 verifyFormat("someFunction(Param, {List1, List2,\n" 6956 " List3});", 6957 getLLVMStyleWithColumns(35)); 6958 verifyFormat("someFunction(Param, Param,\n" 6959 " {List1, List2,\n" 6960 " List3});", 6961 getLLVMStyleWithColumns(35)); 6962 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 6963 " aaaaaaaaaaaaaaaaaaaaaaa);"); 6964 } 6965 6966 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6967 FormatStyle DoNotMerge = getLLVMStyle(); 6968 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6969 6970 verifyFormat("void f() { return 42; }"); 6971 verifyFormat("void f() {\n" 6972 " return 42;\n" 6973 "}", 6974 DoNotMerge); 6975 verifyFormat("void f() {\n" 6976 " // Comment\n" 6977 "}"); 6978 verifyFormat("{\n" 6979 "#error {\n" 6980 " int a;\n" 6981 "}"); 6982 verifyFormat("{\n" 6983 " int a;\n" 6984 "#error {\n" 6985 "}"); 6986 verifyFormat("void f() {} // comment"); 6987 verifyFormat("void f() { int a; } // comment"); 6988 verifyFormat("void f() {\n" 6989 "} // comment", 6990 DoNotMerge); 6991 verifyFormat("void f() {\n" 6992 " int a;\n" 6993 "} // comment", 6994 DoNotMerge); 6995 verifyFormat("void f() {\n" 6996 "} // comment", 6997 getLLVMStyleWithColumns(15)); 6998 6999 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 7000 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 7001 7002 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 7003 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 7004 verifyFormat("class C {\n" 7005 " C()\n" 7006 " : iiiiiiii(nullptr),\n" 7007 " kkkkkkk(nullptr),\n" 7008 " mmmmmmm(nullptr),\n" 7009 " nnnnnnn(nullptr) {}\n" 7010 "};", 7011 getGoogleStyle()); 7012 7013 FormatStyle NoColumnLimit = getLLVMStyle(); 7014 NoColumnLimit.ColumnLimit = 0; 7015 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 7016 EXPECT_EQ("class C {\n" 7017 " A() : b(0) {}\n" 7018 "};", 7019 format("class C{A():b(0){}};", NoColumnLimit)); 7020 EXPECT_EQ("A()\n" 7021 " : b(0) {\n" 7022 "}", 7023 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 7024 7025 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 7026 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 7027 FormatStyle::SFS_None; 7028 EXPECT_EQ("A()\n" 7029 " : b(0) {\n" 7030 "}", 7031 format("A():b(0){}", DoNotMergeNoColumnLimit)); 7032 EXPECT_EQ("A()\n" 7033 " : b(0) {\n" 7034 "}", 7035 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 7036 7037 verifyFormat("#define A \\\n" 7038 " void f() { \\\n" 7039 " int i; \\\n" 7040 " }", 7041 getLLVMStyleWithColumns(20)); 7042 verifyFormat("#define A \\\n" 7043 " void f() { int i; }", 7044 getLLVMStyleWithColumns(21)); 7045 verifyFormat("#define A \\\n" 7046 " void f() { \\\n" 7047 " int i; \\\n" 7048 " } \\\n" 7049 " int j;", 7050 getLLVMStyleWithColumns(22)); 7051 verifyFormat("#define A \\\n" 7052 " void f() { int i; } \\\n" 7053 " int j;", 7054 getLLVMStyleWithColumns(23)); 7055 } 7056 7057 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 7058 FormatStyle MergeEmptyOnly = getLLVMStyle(); 7059 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7060 verifyFormat("class C {\n" 7061 " int f() {}\n" 7062 "};", 7063 MergeEmptyOnly); 7064 verifyFormat("class C {\n" 7065 " int f() {\n" 7066 " return 42;\n" 7067 " }\n" 7068 "};", 7069 MergeEmptyOnly); 7070 verifyFormat("int f() {}", MergeEmptyOnly); 7071 verifyFormat("int f() {\n" 7072 " return 42;\n" 7073 "}", 7074 MergeEmptyOnly); 7075 7076 // Also verify behavior when BraceWrapping.AfterFunction = true 7077 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7078 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 7079 verifyFormat("int f() {}", MergeEmptyOnly); 7080 verifyFormat("class C {\n" 7081 " int f() {}\n" 7082 "};", 7083 MergeEmptyOnly); 7084 } 7085 7086 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 7087 FormatStyle MergeInlineOnly = getLLVMStyle(); 7088 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7089 verifyFormat("class C {\n" 7090 " int f() { return 42; }\n" 7091 "};", 7092 MergeInlineOnly); 7093 verifyFormat("int f() {\n" 7094 " return 42;\n" 7095 "}", 7096 MergeInlineOnly); 7097 7098 // SFS_Inline implies SFS_Empty 7099 verifyFormat("class C {\n" 7100 " int f() {}\n" 7101 "};", 7102 MergeInlineOnly); 7103 verifyFormat("int f() {}", MergeInlineOnly); 7104 7105 // Also verify behavior when BraceWrapping.AfterFunction = true 7106 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7107 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7108 verifyFormat("class C {\n" 7109 " int f() { return 42; }\n" 7110 "};", 7111 MergeInlineOnly); 7112 verifyFormat("int f()\n" 7113 "{\n" 7114 " return 42;\n" 7115 "}", 7116 MergeInlineOnly); 7117 7118 // SFS_Inline implies SFS_Empty 7119 verifyFormat("int f() {}", MergeInlineOnly); 7120 verifyFormat("class C {\n" 7121 " int f() {}\n" 7122 "};", 7123 MergeInlineOnly); 7124 } 7125 7126 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 7127 FormatStyle MergeInlineOnly = getLLVMStyle(); 7128 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 7129 FormatStyle::SFS_InlineOnly; 7130 verifyFormat("class C {\n" 7131 " int f() { return 42; }\n" 7132 "};", 7133 MergeInlineOnly); 7134 verifyFormat("int f() {\n" 7135 " return 42;\n" 7136 "}", 7137 MergeInlineOnly); 7138 7139 // SFS_InlineOnly does not imply SFS_Empty 7140 verifyFormat("class C {\n" 7141 " int f() {}\n" 7142 "};", 7143 MergeInlineOnly); 7144 verifyFormat("int f() {\n" 7145 "}", 7146 MergeInlineOnly); 7147 7148 // Also verify behavior when BraceWrapping.AfterFunction = true 7149 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 7150 MergeInlineOnly.BraceWrapping.AfterFunction = true; 7151 verifyFormat("class C {\n" 7152 " int f() { return 42; }\n" 7153 "};", 7154 MergeInlineOnly); 7155 verifyFormat("int f()\n" 7156 "{\n" 7157 " return 42;\n" 7158 "}", 7159 MergeInlineOnly); 7160 7161 // SFS_InlineOnly does not imply SFS_Empty 7162 verifyFormat("int f()\n" 7163 "{\n" 7164 "}", 7165 MergeInlineOnly); 7166 verifyFormat("class C {\n" 7167 " int f() {}\n" 7168 "};", 7169 MergeInlineOnly); 7170 } 7171 7172 TEST_F(FormatTest, SplitEmptyFunction) { 7173 FormatStyle Style = getLLVMStyle(); 7174 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 7175 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7176 Style.BraceWrapping.AfterFunction = true; 7177 Style.BraceWrapping.SplitEmptyFunction = false; 7178 Style.ColumnLimit = 40; 7179 7180 verifyFormat("int f()\n" 7181 "{}", 7182 Style); 7183 verifyFormat("int f()\n" 7184 "{\n" 7185 " return 42;\n" 7186 "}", 7187 Style); 7188 verifyFormat("int f()\n" 7189 "{\n" 7190 " // some comment\n" 7191 "}", 7192 Style); 7193 7194 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 7195 verifyFormat("int f() {}", Style); 7196 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7197 "{}", 7198 Style); 7199 verifyFormat("int f()\n" 7200 "{\n" 7201 " return 0;\n" 7202 "}", 7203 Style); 7204 7205 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 7206 verifyFormat("class Foo {\n" 7207 " int f() {}\n" 7208 "};\n", 7209 Style); 7210 verifyFormat("class Foo {\n" 7211 " int f() { return 0; }\n" 7212 "};\n", 7213 Style); 7214 verifyFormat("class Foo {\n" 7215 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7216 " {}\n" 7217 "};\n", 7218 Style); 7219 verifyFormat("class Foo {\n" 7220 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7221 " {\n" 7222 " return 0;\n" 7223 " }\n" 7224 "};\n", 7225 Style); 7226 7227 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7228 verifyFormat("int f() {}", Style); 7229 verifyFormat("int f() { return 0; }", Style); 7230 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7231 "{}", 7232 Style); 7233 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 7234 "{\n" 7235 " return 0;\n" 7236 "}", 7237 Style); 7238 } 7239 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 7240 FormatStyle Style = getLLVMStyle(); 7241 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 7242 verifyFormat("#ifdef A\n" 7243 "int f() {}\n" 7244 "#else\n" 7245 "int g() {}\n" 7246 "#endif", 7247 Style); 7248 } 7249 7250 TEST_F(FormatTest, SplitEmptyClass) { 7251 FormatStyle Style = getLLVMStyle(); 7252 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7253 Style.BraceWrapping.AfterClass = true; 7254 Style.BraceWrapping.SplitEmptyRecord = false; 7255 7256 verifyFormat("class Foo\n" 7257 "{};", 7258 Style); 7259 verifyFormat("/* something */ class Foo\n" 7260 "{};", 7261 Style); 7262 verifyFormat("template <typename X> class Foo\n" 7263 "{};", 7264 Style); 7265 verifyFormat("class Foo\n" 7266 "{\n" 7267 " Foo();\n" 7268 "};", 7269 Style); 7270 verifyFormat("typedef class Foo\n" 7271 "{\n" 7272 "} Foo_t;", 7273 Style); 7274 } 7275 7276 TEST_F(FormatTest, SplitEmptyStruct) { 7277 FormatStyle Style = getLLVMStyle(); 7278 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7279 Style.BraceWrapping.AfterStruct = true; 7280 Style.BraceWrapping.SplitEmptyRecord = false; 7281 7282 verifyFormat("struct Foo\n" 7283 "{};", 7284 Style); 7285 verifyFormat("/* something */ struct Foo\n" 7286 "{};", 7287 Style); 7288 verifyFormat("template <typename X> struct Foo\n" 7289 "{};", 7290 Style); 7291 verifyFormat("struct Foo\n" 7292 "{\n" 7293 " Foo();\n" 7294 "};", 7295 Style); 7296 verifyFormat("typedef struct Foo\n" 7297 "{\n" 7298 "} Foo_t;", 7299 Style); 7300 //typedef struct Bar {} Bar_t; 7301 } 7302 7303 TEST_F(FormatTest, SplitEmptyUnion) { 7304 FormatStyle Style = getLLVMStyle(); 7305 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7306 Style.BraceWrapping.AfterUnion = true; 7307 Style.BraceWrapping.SplitEmptyRecord = false; 7308 7309 verifyFormat("union Foo\n" 7310 "{};", 7311 Style); 7312 verifyFormat("/* something */ union Foo\n" 7313 "{};", 7314 Style); 7315 verifyFormat("union Foo\n" 7316 "{\n" 7317 " A,\n" 7318 "};", 7319 Style); 7320 verifyFormat("typedef union Foo\n" 7321 "{\n" 7322 "} Foo_t;", 7323 Style); 7324 } 7325 7326 TEST_F(FormatTest, SplitEmptyNamespace) { 7327 FormatStyle Style = getLLVMStyle(); 7328 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7329 Style.BraceWrapping.AfterNamespace = true; 7330 Style.BraceWrapping.SplitEmptyNamespace = false; 7331 7332 verifyFormat("namespace Foo\n" 7333 "{};", 7334 Style); 7335 verifyFormat("/* something */ namespace Foo\n" 7336 "{};", 7337 Style); 7338 verifyFormat("inline namespace Foo\n" 7339 "{};", 7340 Style); 7341 verifyFormat("namespace Foo\n" 7342 "{\n" 7343 "void Bar();\n" 7344 "};", 7345 Style); 7346 } 7347 7348 TEST_F(FormatTest, NeverMergeShortRecords) { 7349 FormatStyle Style = getLLVMStyle(); 7350 7351 verifyFormat("class Foo {\n" 7352 " Foo();\n" 7353 "};", 7354 Style); 7355 verifyFormat("typedef class Foo {\n" 7356 " Foo();\n" 7357 "} Foo_t;", 7358 Style); 7359 verifyFormat("struct Foo {\n" 7360 " Foo();\n" 7361 "};", 7362 Style); 7363 verifyFormat("typedef struct Foo {\n" 7364 " Foo();\n" 7365 "} Foo_t;", 7366 Style); 7367 verifyFormat("union Foo {\n" 7368 " A,\n" 7369 "};", 7370 Style); 7371 verifyFormat("typedef union Foo {\n" 7372 " A,\n" 7373 "} Foo_t;", 7374 Style); 7375 verifyFormat("namespace Foo {\n" 7376 "void Bar();\n" 7377 "};", 7378 Style); 7379 7380 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 7381 Style.BraceWrapping.AfterClass = true; 7382 Style.BraceWrapping.AfterStruct = true; 7383 Style.BraceWrapping.AfterUnion = true; 7384 Style.BraceWrapping.AfterNamespace = true; 7385 verifyFormat("class Foo\n" 7386 "{\n" 7387 " Foo();\n" 7388 "};", 7389 Style); 7390 verifyFormat("typedef class Foo\n" 7391 "{\n" 7392 " Foo();\n" 7393 "} Foo_t;", 7394 Style); 7395 verifyFormat("struct Foo\n" 7396 "{\n" 7397 " Foo();\n" 7398 "};", 7399 Style); 7400 verifyFormat("typedef struct Foo\n" 7401 "{\n" 7402 " Foo();\n" 7403 "} Foo_t;", 7404 Style); 7405 verifyFormat("union Foo\n" 7406 "{\n" 7407 " A,\n" 7408 "};", 7409 Style); 7410 verifyFormat("typedef union Foo\n" 7411 "{\n" 7412 " A,\n" 7413 "} Foo_t;", 7414 Style); 7415 verifyFormat("namespace Foo\n" 7416 "{\n" 7417 "void Bar();\n" 7418 "};", 7419 Style); 7420 } 7421 7422 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 7423 // Elaborate type variable declarations. 7424 verifyFormat("struct foo a = {bar};\nint n;"); 7425 verifyFormat("class foo a = {bar};\nint n;"); 7426 verifyFormat("union foo a = {bar};\nint n;"); 7427 7428 // Elaborate types inside function definitions. 7429 verifyFormat("struct foo f() {}\nint n;"); 7430 verifyFormat("class foo f() {}\nint n;"); 7431 verifyFormat("union foo f() {}\nint n;"); 7432 7433 // Templates. 7434 verifyFormat("template <class X> void f() {}\nint n;"); 7435 verifyFormat("template <struct X> void f() {}\nint n;"); 7436 verifyFormat("template <union X> void f() {}\nint n;"); 7437 7438 // Actual definitions... 7439 verifyFormat("struct {\n} n;"); 7440 verifyFormat( 7441 "template <template <class T, class Y>, class Z> class X {\n} n;"); 7442 verifyFormat("union Z {\n int n;\n} x;"); 7443 verifyFormat("class MACRO Z {\n} n;"); 7444 verifyFormat("class MACRO(X) Z {\n} n;"); 7445 verifyFormat("class __attribute__(X) Z {\n} n;"); 7446 verifyFormat("class __declspec(X) Z {\n} n;"); 7447 verifyFormat("class A##B##C {\n} n;"); 7448 verifyFormat("class alignas(16) Z {\n} n;"); 7449 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 7450 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 7451 7452 // Redefinition from nested context: 7453 verifyFormat("class A::B::C {\n} n;"); 7454 7455 // Template definitions. 7456 verifyFormat( 7457 "template <typename F>\n" 7458 "Matcher(const Matcher<F> &Other,\n" 7459 " typename enable_if_c<is_base_of<F, T>::value &&\n" 7460 " !is_same<F, T>::value>::type * = 0)\n" 7461 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 7462 7463 // FIXME: This is still incorrectly handled at the formatter side. 7464 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 7465 verifyFormat("int i = SomeFunction(a<b, a> b);"); 7466 7467 // FIXME: 7468 // This now gets parsed incorrectly as class definition. 7469 // verifyFormat("class A<int> f() {\n}\nint n;"); 7470 7471 // Elaborate types where incorrectly parsing the structural element would 7472 // break the indent. 7473 verifyFormat("if (true)\n" 7474 " class X x;\n" 7475 "else\n" 7476 " f();\n"); 7477 7478 // This is simply incomplete. Formatting is not important, but must not crash. 7479 verifyFormat("class A:"); 7480 } 7481 7482 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 7483 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 7484 format("#error Leave all white!!!!! space* alone!\n")); 7485 EXPECT_EQ( 7486 "#warning Leave all white!!!!! space* alone!\n", 7487 format("#warning Leave all white!!!!! space* alone!\n")); 7488 EXPECT_EQ("#error 1", format(" # error 1")); 7489 EXPECT_EQ("#warning 1", format(" # warning 1")); 7490 } 7491 7492 TEST_F(FormatTest, FormatHashIfExpressions) { 7493 verifyFormat("#if AAAA && BBBB"); 7494 verifyFormat("#if (AAAA && BBBB)"); 7495 verifyFormat("#elif (AAAA && BBBB)"); 7496 // FIXME: Come up with a better indentation for #elif. 7497 verifyFormat( 7498 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 7499 " defined(BBBBBBBB)\n" 7500 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 7501 " defined(BBBBBBBB)\n" 7502 "#endif", 7503 getLLVMStyleWithColumns(65)); 7504 } 7505 7506 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 7507 FormatStyle AllowsMergedIf = getGoogleStyle(); 7508 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 7509 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 7510 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 7511 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 7512 EXPECT_EQ("if (true) return 42;", 7513 format("if (true)\nreturn 42;", AllowsMergedIf)); 7514 FormatStyle ShortMergedIf = AllowsMergedIf; 7515 ShortMergedIf.ColumnLimit = 25; 7516 verifyFormat("#define A \\\n" 7517 " if (true) return 42;", 7518 ShortMergedIf); 7519 verifyFormat("#define A \\\n" 7520 " f(); \\\n" 7521 " if (true)\n" 7522 "#define B", 7523 ShortMergedIf); 7524 verifyFormat("#define A \\\n" 7525 " f(); \\\n" 7526 " if (true)\n" 7527 "g();", 7528 ShortMergedIf); 7529 verifyFormat("{\n" 7530 "#ifdef A\n" 7531 " // Comment\n" 7532 " if (true) continue;\n" 7533 "#endif\n" 7534 " // Comment\n" 7535 " if (true) continue;\n" 7536 "}", 7537 ShortMergedIf); 7538 ShortMergedIf.ColumnLimit = 33; 7539 verifyFormat("#define A \\\n" 7540 " if constexpr (true) return 42;", 7541 ShortMergedIf); 7542 ShortMergedIf.ColumnLimit = 29; 7543 verifyFormat("#define A \\\n" 7544 " if (aaaaaaaaaa) return 1; \\\n" 7545 " return 2;", 7546 ShortMergedIf); 7547 ShortMergedIf.ColumnLimit = 28; 7548 verifyFormat("#define A \\\n" 7549 " if (aaaaaaaaaa) \\\n" 7550 " return 1; \\\n" 7551 " return 2;", 7552 ShortMergedIf); 7553 verifyFormat("#define A \\\n" 7554 " if constexpr (aaaaaaa) \\\n" 7555 " return 1; \\\n" 7556 " return 2;", 7557 ShortMergedIf); 7558 } 7559 7560 TEST_F(FormatTest, FormatStarDependingOnContext) { 7561 verifyFormat("void f(int *a);"); 7562 verifyFormat("void f() { f(fint * b); }"); 7563 verifyFormat("class A {\n void f(int *a);\n};"); 7564 verifyFormat("class A {\n int *a;\n};"); 7565 verifyFormat("namespace a {\n" 7566 "namespace b {\n" 7567 "class A {\n" 7568 " void f() {}\n" 7569 " int *a;\n" 7570 "};\n" 7571 "} // namespace b\n" 7572 "} // namespace a"); 7573 } 7574 7575 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7576 verifyFormat("while"); 7577 verifyFormat("operator"); 7578 } 7579 7580 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 7581 // This code would be painfully slow to format if we didn't skip it. 7582 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 7583 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7584 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7585 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7586 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7587 "A(1, 1)\n" 7588 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 7589 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7590 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7591 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7592 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7593 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7594 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7595 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7596 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7597 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 7598 // Deeply nested part is untouched, rest is formatted. 7599 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 7600 format(std::string("int i;\n") + Code + "int j;\n", 7601 getLLVMStyle(), SC_ExpectIncomplete)); 7602 } 7603 7604 //===----------------------------------------------------------------------===// 7605 // Objective-C tests. 7606 //===----------------------------------------------------------------------===// 7607 7608 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7609 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7610 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7611 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7612 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7613 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7614 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7615 format("-(NSInteger)Method3:(id)anObject;")); 7616 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7617 format("-(NSInteger)Method4:(id)anObject;")); 7618 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7619 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7620 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7621 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7622 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7623 "forAllCells:(BOOL)flag;", 7624 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7625 "forAllCells:(BOOL)flag;")); 7626 7627 // Very long objectiveC method declaration. 7628 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7629 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7630 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7631 " inRange:(NSRange)range\n" 7632 " outRange:(NSRange)out_range\n" 7633 " outRange1:(NSRange)out_range1\n" 7634 " outRange2:(NSRange)out_range2\n" 7635 " outRange3:(NSRange)out_range3\n" 7636 " outRange4:(NSRange)out_range4\n" 7637 " outRange5:(NSRange)out_range5\n" 7638 " outRange6:(NSRange)out_range6\n" 7639 " outRange7:(NSRange)out_range7\n" 7640 " outRange8:(NSRange)out_range8\n" 7641 " outRange9:(NSRange)out_range9;"); 7642 7643 // When the function name has to be wrapped. 7644 FormatStyle Style = getLLVMStyle(); 7645 Style.IndentWrappedFunctionNames = false; 7646 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7647 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7648 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7649 "}", 7650 Style); 7651 Style.IndentWrappedFunctionNames = true; 7652 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7653 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7654 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7655 "}", 7656 Style); 7657 7658 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7659 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7660 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7661 // protocol lists (but not for template classes): 7662 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7663 7664 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7665 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7666 7667 // If there's no return type (very rare in practice!), LLVM and Google style 7668 // agree. 7669 verifyFormat("- foo;"); 7670 verifyFormat("- foo:(int)f;"); 7671 verifyGoogleFormat("- foo:(int)foo;"); 7672 } 7673 7674 7675 TEST_F(FormatTest, BreaksStringLiterals) { 7676 EXPECT_EQ("\"some text \"\n" 7677 "\"other\";", 7678 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7679 EXPECT_EQ("\"some text \"\n" 7680 "\"other\";", 7681 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7682 EXPECT_EQ( 7683 "#define A \\\n" 7684 " \"some \" \\\n" 7685 " \"text \" \\\n" 7686 " \"other\";", 7687 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7688 EXPECT_EQ( 7689 "#define A \\\n" 7690 " \"so \" \\\n" 7691 " \"text \" \\\n" 7692 " \"other\";", 7693 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7694 7695 EXPECT_EQ("\"some text\"", 7696 format("\"some text\"", getLLVMStyleWithColumns(1))); 7697 EXPECT_EQ("\"some text\"", 7698 format("\"some text\"", getLLVMStyleWithColumns(11))); 7699 EXPECT_EQ("\"some \"\n" 7700 "\"text\"", 7701 format("\"some text\"", getLLVMStyleWithColumns(10))); 7702 EXPECT_EQ("\"some \"\n" 7703 "\"text\"", 7704 format("\"some text\"", getLLVMStyleWithColumns(7))); 7705 EXPECT_EQ("\"some\"\n" 7706 "\" tex\"\n" 7707 "\"t\"", 7708 format("\"some text\"", getLLVMStyleWithColumns(6))); 7709 EXPECT_EQ("\"some\"\n" 7710 "\" tex\"\n" 7711 "\" and\"", 7712 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7713 EXPECT_EQ("\"some\"\n" 7714 "\"/tex\"\n" 7715 "\"/and\"", 7716 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7717 7718 EXPECT_EQ("variable =\n" 7719 " \"long string \"\n" 7720 " \"literal\";", 7721 format("variable = \"long string literal\";", 7722 getLLVMStyleWithColumns(20))); 7723 7724 EXPECT_EQ("variable = f(\n" 7725 " \"long string \"\n" 7726 " \"literal\",\n" 7727 " short,\n" 7728 " loooooooooooooooooooong);", 7729 format("variable = f(\"long string literal\", short, " 7730 "loooooooooooooooooooong);", 7731 getLLVMStyleWithColumns(20))); 7732 7733 EXPECT_EQ( 7734 "f(g(\"long string \"\n" 7735 " \"literal\"),\n" 7736 " b);", 7737 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7738 EXPECT_EQ("f(g(\"long string \"\n" 7739 " \"literal\",\n" 7740 " a),\n" 7741 " b);", 7742 format("f(g(\"long string literal\", a), b);", 7743 getLLVMStyleWithColumns(20))); 7744 EXPECT_EQ( 7745 "f(\"one two\".split(\n" 7746 " variable));", 7747 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7748 EXPECT_EQ("f(\"one two three four five six \"\n" 7749 " \"seven\".split(\n" 7750 " really_looooong_variable));", 7751 format("f(\"one two three four five six seven\"." 7752 "split(really_looooong_variable));", 7753 getLLVMStyleWithColumns(33))); 7754 7755 EXPECT_EQ("f(\"some \"\n" 7756 " \"text\",\n" 7757 " other);", 7758 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7759 7760 // Only break as a last resort. 7761 verifyFormat( 7762 "aaaaaaaaaaaaaaaaaaaa(\n" 7763 " aaaaaaaaaaaaaaaaaaaa,\n" 7764 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7765 7766 EXPECT_EQ("\"splitmea\"\n" 7767 "\"trandomp\"\n" 7768 "\"oint\"", 7769 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7770 7771 EXPECT_EQ("\"split/\"\n" 7772 "\"pathat/\"\n" 7773 "\"slashes\"", 7774 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7775 7776 EXPECT_EQ("\"split/\"\n" 7777 "\"pathat/\"\n" 7778 "\"slashes\"", 7779 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7780 EXPECT_EQ("\"split at \"\n" 7781 "\"spaces/at/\"\n" 7782 "\"slashes.at.any$\"\n" 7783 "\"non-alphanumeric%\"\n" 7784 "\"1111111111characte\"\n" 7785 "\"rs\"", 7786 format("\"split at " 7787 "spaces/at/" 7788 "slashes.at." 7789 "any$non-" 7790 "alphanumeric%" 7791 "1111111111characte" 7792 "rs\"", 7793 getLLVMStyleWithColumns(20))); 7794 7795 // Verify that splitting the strings understands 7796 // Style::AlwaysBreakBeforeMultilineStrings. 7797 EXPECT_EQ( 7798 "aaaaaaaaaaaa(\n" 7799 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7800 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7801 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7802 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7803 "aaaaaaaaaaaaaaaaaaaaaa\");", 7804 getGoogleStyle())); 7805 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7806 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7807 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7808 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7809 "aaaaaaaaaaaaaaaaaaaaaa\";", 7810 getGoogleStyle())); 7811 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7812 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7813 format("llvm::outs() << " 7814 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7815 "aaaaaaaaaaaaaaaaaaa\";")); 7816 EXPECT_EQ("ffff(\n" 7817 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7818 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7819 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7820 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7821 getGoogleStyle())); 7822 7823 FormatStyle Style = getLLVMStyleWithColumns(12); 7824 Style.BreakStringLiterals = false; 7825 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 7826 7827 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7828 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 7829 EXPECT_EQ("#define A \\\n" 7830 " \"some \" \\\n" 7831 " \"text \" \\\n" 7832 " \"other\";", 7833 format("#define A \"some text other\";", AlignLeft)); 7834 } 7835 7836 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 7837 EXPECT_EQ("C a = \"some more \"\n" 7838 " \"text\";", 7839 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 7840 } 7841 7842 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7843 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7844 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7845 EXPECT_EQ("int i = a(b());", 7846 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7847 } 7848 7849 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7850 EXPECT_EQ( 7851 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7852 "(\n" 7853 " \"x\t\");", 7854 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7855 "aaaaaaa(" 7856 "\"x\t\");")); 7857 } 7858 7859 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7860 EXPECT_EQ( 7861 "u8\"utf8 string \"\n" 7862 "u8\"literal\";", 7863 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7864 EXPECT_EQ( 7865 "u\"utf16 string \"\n" 7866 "u\"literal\";", 7867 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7868 EXPECT_EQ( 7869 "U\"utf32 string \"\n" 7870 "U\"literal\";", 7871 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7872 EXPECT_EQ("L\"wide string \"\n" 7873 "L\"literal\";", 7874 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7875 EXPECT_EQ("@\"NSString \"\n" 7876 "@\"literal\";", 7877 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7878 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 7879 7880 // This input makes clang-format try to split the incomplete unicode escape 7881 // sequence, which used to lead to a crasher. 7882 verifyNoCrash( 7883 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7884 getLLVMStyleWithColumns(60)); 7885 } 7886 7887 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7888 FormatStyle Style = getGoogleStyleWithColumns(15); 7889 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7890 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7891 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7892 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7893 EXPECT_EQ("u8R\"x(raw literal)x\";", 7894 format("u8R\"x(raw literal)x\";", Style)); 7895 } 7896 7897 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7898 FormatStyle Style = getLLVMStyleWithColumns(20); 7899 EXPECT_EQ( 7900 "_T(\"aaaaaaaaaaaaaa\")\n" 7901 "_T(\"aaaaaaaaaaaaaa\")\n" 7902 "_T(\"aaaaaaaaaaaa\")", 7903 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7904 EXPECT_EQ("f(x,\n" 7905 " _T(\"aaaaaaaaaaaa\")\n" 7906 " _T(\"aaa\"),\n" 7907 " z);", 7908 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7909 7910 // FIXME: Handle embedded spaces in one iteration. 7911 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7912 // "_T(\"aaaaaaaaaaaaa\")\n" 7913 // "_T(\"aaaaaaaaaaaaa\")\n" 7914 // "_T(\"a\")", 7915 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7916 // getLLVMStyleWithColumns(20))); 7917 EXPECT_EQ( 7918 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7919 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7920 EXPECT_EQ("f(\n" 7921 "#if !TEST\n" 7922 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7923 "#endif\n" 7924 ");", 7925 format("f(\n" 7926 "#if !TEST\n" 7927 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7928 "#endif\n" 7929 ");")); 7930 EXPECT_EQ("f(\n" 7931 "\n" 7932 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7933 format("f(\n" 7934 "\n" 7935 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7936 } 7937 7938 TEST_F(FormatTest, BreaksStringLiteralOperands) { 7939 // In a function call with two operands, the second can be broken with no line 7940 // break before it. 7941 EXPECT_EQ("func(a, \"long long \"\n" 7942 " \"long long\");", 7943 format("func(a, \"long long long long\");", 7944 getLLVMStyleWithColumns(24))); 7945 // In a function call with three operands, the second must be broken with a 7946 // line break before it. 7947 EXPECT_EQ("func(a,\n" 7948 " \"long long long \"\n" 7949 " \"long\",\n" 7950 " c);", 7951 format("func(a, \"long long long long\", c);", 7952 getLLVMStyleWithColumns(24))); 7953 // In a function call with three operands, the third must be broken with a 7954 // line break before it. 7955 EXPECT_EQ("func(a, b,\n" 7956 " \"long long long \"\n" 7957 " \"long\");", 7958 format("func(a, b, \"long long long long\");", 7959 getLLVMStyleWithColumns(24))); 7960 // In a function call with three operands, both the second and the third must 7961 // be broken with a line break before them. 7962 EXPECT_EQ("func(a,\n" 7963 " \"long long long \"\n" 7964 " \"long\",\n" 7965 " \"long long long \"\n" 7966 " \"long\");", 7967 format("func(a, \"long long long long\", \"long long long long\");", 7968 getLLVMStyleWithColumns(24))); 7969 // In a chain of << with two operands, the second can be broken with no line 7970 // break before it. 7971 EXPECT_EQ("a << \"line line \"\n" 7972 " \"line\";", 7973 format("a << \"line line line\";", 7974 getLLVMStyleWithColumns(20))); 7975 // In a chain of << with three operands, the second can be broken with no line 7976 // break before it. 7977 EXPECT_EQ("abcde << \"line \"\n" 7978 " \"line line\"\n" 7979 " << c;", 7980 format("abcde << \"line line line\" << c;", 7981 getLLVMStyleWithColumns(20))); 7982 // In a chain of << with three operands, the third must be broken with a line 7983 // break before it. 7984 EXPECT_EQ("a << b\n" 7985 " << \"line line \"\n" 7986 " \"line\";", 7987 format("a << b << \"line line line\";", 7988 getLLVMStyleWithColumns(20))); 7989 // In a chain of << with three operands, the second can be broken with no line 7990 // break before it and the third must be broken with a line break before it. 7991 EXPECT_EQ("abcd << \"line line \"\n" 7992 " \"line\"\n" 7993 " << \"line line \"\n" 7994 " \"line\";", 7995 format("abcd << \"line line line\" << \"line line line\";", 7996 getLLVMStyleWithColumns(20))); 7997 // In a chain of binary operators with two operands, the second can be broken 7998 // with no line break before it. 7999 EXPECT_EQ("abcd + \"line line \"\n" 8000 " \"line line\";", 8001 format("abcd + \"line line line line\";", 8002 getLLVMStyleWithColumns(20))); 8003 // In a chain of binary operators with three operands, the second must be 8004 // broken with a line break before it. 8005 EXPECT_EQ("abcd +\n" 8006 " \"line line \"\n" 8007 " \"line line\" +\n" 8008 " e;", 8009 format("abcd + \"line line line line\" + e;", 8010 getLLVMStyleWithColumns(20))); 8011 // In a function call with two operands, with AlignAfterOpenBracket enabled, 8012 // the first must be broken with a line break before it. 8013 FormatStyle Style = getLLVMStyleWithColumns(25); 8014 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8015 EXPECT_EQ("someFunction(\n" 8016 " \"long long long \"\n" 8017 " \"long\",\n" 8018 " a);", 8019 format("someFunction(\"long long long long\", a);", Style)); 8020 } 8021 8022 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 8023 EXPECT_EQ( 8024 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8025 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8026 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 8027 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8028 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 8029 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 8030 } 8031 8032 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 8033 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 8034 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 8035 EXPECT_EQ("fffffffffff(g(R\"x(\n" 8036 "multiline raw string literal xxxxxxxxxxxxxx\n" 8037 ")x\",\n" 8038 " a),\n" 8039 " b);", 8040 format("fffffffffff(g(R\"x(\n" 8041 "multiline raw string literal xxxxxxxxxxxxxx\n" 8042 ")x\", a), b);", 8043 getGoogleStyleWithColumns(20))); 8044 EXPECT_EQ("fffffffffff(\n" 8045 " g(R\"x(qqq\n" 8046 "multiline raw string literal xxxxxxxxxxxxxx\n" 8047 ")x\",\n" 8048 " a),\n" 8049 " b);", 8050 format("fffffffffff(g(R\"x(qqq\n" 8051 "multiline raw string literal xxxxxxxxxxxxxx\n" 8052 ")x\", a), b);", 8053 getGoogleStyleWithColumns(20))); 8054 8055 EXPECT_EQ("fffffffffff(R\"x(\n" 8056 "multiline raw string literal xxxxxxxxxxxxxx\n" 8057 ")x\");", 8058 format("fffffffffff(R\"x(\n" 8059 "multiline raw string literal xxxxxxxxxxxxxx\n" 8060 ")x\");", 8061 getGoogleStyleWithColumns(20))); 8062 EXPECT_EQ("fffffffffff(R\"x(\n" 8063 "multiline raw string literal xxxxxxxxxxxxxx\n" 8064 ")x\" + bbbbbb);", 8065 format("fffffffffff(R\"x(\n" 8066 "multiline raw string literal xxxxxxxxxxxxxx\n" 8067 ")x\" + bbbbbb);", 8068 getGoogleStyleWithColumns(20))); 8069 EXPECT_EQ("fffffffffff(\n" 8070 " R\"x(\n" 8071 "multiline raw string literal xxxxxxxxxxxxxx\n" 8072 ")x\" +\n" 8073 " bbbbbb);", 8074 format("fffffffffff(\n" 8075 " R\"x(\n" 8076 "multiline raw string literal xxxxxxxxxxxxxx\n" 8077 ")x\" + bbbbbb);", 8078 getGoogleStyleWithColumns(20))); 8079 } 8080 8081 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8082 verifyFormat("string a = \"unterminated;"); 8083 EXPECT_EQ("function(\"unterminated,\n" 8084 " OtherParameter);", 8085 format("function( \"unterminated,\n" 8086 " OtherParameter);")); 8087 } 8088 8089 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8090 FormatStyle Style = getLLVMStyle(); 8091 Style.Standard = FormatStyle::LS_Cpp03; 8092 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8093 format("#define x(_a) printf(\"foo\"_a);", Style)); 8094 } 8095 8096 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8097 8098 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8099 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8100 " \"ddeeefff\");", 8101 format("someFunction(\"aaabbbcccdddeeefff\");", 8102 getLLVMStyleWithColumns(25))); 8103 EXPECT_EQ("someFunction1234567890(\n" 8104 " \"aaabbbcccdddeeefff\");", 8105 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8106 getLLVMStyleWithColumns(26))); 8107 EXPECT_EQ("someFunction1234567890(\n" 8108 " \"aaabbbcccdddeeeff\"\n" 8109 " \"f\");", 8110 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8111 getLLVMStyleWithColumns(25))); 8112 EXPECT_EQ("someFunction1234567890(\n" 8113 " \"aaabbbcccdddeeeff\"\n" 8114 " \"f\");", 8115 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8116 getLLVMStyleWithColumns(24))); 8117 EXPECT_EQ("someFunction(\n" 8118 " \"aaabbbcc ddde \"\n" 8119 " \"efff\");", 8120 format("someFunction(\"aaabbbcc ddde efff\");", 8121 getLLVMStyleWithColumns(25))); 8122 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8123 " \"ddeeefff\");", 8124 format("someFunction(\"aaabbbccc ddeeefff\");", 8125 getLLVMStyleWithColumns(25))); 8126 EXPECT_EQ("someFunction1234567890(\n" 8127 " \"aaabb \"\n" 8128 " \"cccdddeeefff\");", 8129 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8130 getLLVMStyleWithColumns(25))); 8131 EXPECT_EQ("#define A \\\n" 8132 " string s = \\\n" 8133 " \"123456789\" \\\n" 8134 " \"0\"; \\\n" 8135 " int i;", 8136 format("#define A string s = \"1234567890\"; int i;", 8137 getLLVMStyleWithColumns(20))); 8138 EXPECT_EQ("someFunction(\n" 8139 " \"aaabbbcc \"\n" 8140 " \"dddeeefff\");", 8141 format("someFunction(\"aaabbbcc dddeeefff\");", 8142 getLLVMStyleWithColumns(25))); 8143 } 8144 8145 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8146 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8147 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8148 EXPECT_EQ("\"test\"\n" 8149 "\"\\n\"", 8150 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8151 EXPECT_EQ("\"tes\\\\\"\n" 8152 "\"n\"", 8153 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8154 EXPECT_EQ("\"\\\\\\\\\"\n" 8155 "\"\\n\"", 8156 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8157 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8158 EXPECT_EQ("\"\\uff01\"\n" 8159 "\"test\"", 8160 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8161 EXPECT_EQ("\"\\Uff01ff02\"", 8162 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8163 EXPECT_EQ("\"\\x000000000001\"\n" 8164 "\"next\"", 8165 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8166 EXPECT_EQ("\"\\x000000000001next\"", 8167 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8168 EXPECT_EQ("\"\\x000000000001\"", 8169 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8170 EXPECT_EQ("\"test\"\n" 8171 "\"\\000000\"\n" 8172 "\"000001\"", 8173 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8174 EXPECT_EQ("\"test\\000\"\n" 8175 "\"00000000\"\n" 8176 "\"1\"", 8177 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8178 } 8179 8180 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8181 verifyFormat("void f() {\n" 8182 " return g() {}\n" 8183 " void h() {}"); 8184 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8185 "g();\n" 8186 "}"); 8187 } 8188 8189 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8190 verifyFormat( 8191 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8192 } 8193 8194 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8195 verifyFormat("class X {\n" 8196 " void f() {\n" 8197 " }\n" 8198 "};", 8199 getLLVMStyleWithColumns(12)); 8200 } 8201 8202 TEST_F(FormatTest, ConfigurableIndentWidth) { 8203 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8204 EightIndent.IndentWidth = 8; 8205 EightIndent.ContinuationIndentWidth = 8; 8206 verifyFormat("void f() {\n" 8207 " someFunction();\n" 8208 " if (true) {\n" 8209 " f();\n" 8210 " }\n" 8211 "}", 8212 EightIndent); 8213 verifyFormat("class X {\n" 8214 " void f() {\n" 8215 " }\n" 8216 "};", 8217 EightIndent); 8218 verifyFormat("int x[] = {\n" 8219 " call(),\n" 8220 " call()};", 8221 EightIndent); 8222 } 8223 8224 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8225 verifyFormat("double\n" 8226 "f();", 8227 getLLVMStyleWithColumns(8)); 8228 } 8229 8230 TEST_F(FormatTest, ConfigurableUseOfTab) { 8231 FormatStyle Tab = getLLVMStyleWithColumns(42); 8232 Tab.IndentWidth = 8; 8233 Tab.UseTab = FormatStyle::UT_Always; 8234 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8235 8236 EXPECT_EQ("if (aaaaaaaa && // q\n" 8237 " bb)\t\t// w\n" 8238 "\t;", 8239 format("if (aaaaaaaa &&// q\n" 8240 "bb)// w\n" 8241 ";", 8242 Tab)); 8243 EXPECT_EQ("if (aaa && bbb) // w\n" 8244 "\t;", 8245 format("if(aaa&&bbb)// w\n" 8246 ";", 8247 Tab)); 8248 8249 verifyFormat("class X {\n" 8250 "\tvoid f() {\n" 8251 "\t\tsomeFunction(parameter1,\n" 8252 "\t\t\t parameter2);\n" 8253 "\t}\n" 8254 "};", 8255 Tab); 8256 verifyFormat("#define A \\\n" 8257 "\tvoid f() { \\\n" 8258 "\t\tsomeFunction( \\\n" 8259 "\t\t parameter1, \\\n" 8260 "\t\t parameter2); \\\n" 8261 "\t}", 8262 Tab); 8263 8264 Tab.TabWidth = 4; 8265 Tab.IndentWidth = 8; 8266 verifyFormat("class TabWidth4Indent8 {\n" 8267 "\t\tvoid f() {\n" 8268 "\t\t\t\tsomeFunction(parameter1,\n" 8269 "\t\t\t\t\t\t\t parameter2);\n" 8270 "\t\t}\n" 8271 "};", 8272 Tab); 8273 8274 Tab.TabWidth = 4; 8275 Tab.IndentWidth = 4; 8276 verifyFormat("class TabWidth4Indent4 {\n" 8277 "\tvoid f() {\n" 8278 "\t\tsomeFunction(parameter1,\n" 8279 "\t\t\t\t\t parameter2);\n" 8280 "\t}\n" 8281 "};", 8282 Tab); 8283 8284 Tab.TabWidth = 8; 8285 Tab.IndentWidth = 4; 8286 verifyFormat("class TabWidth8Indent4 {\n" 8287 " void f() {\n" 8288 "\tsomeFunction(parameter1,\n" 8289 "\t\t parameter2);\n" 8290 " }\n" 8291 "};", 8292 Tab); 8293 8294 Tab.TabWidth = 8; 8295 Tab.IndentWidth = 8; 8296 EXPECT_EQ("/*\n" 8297 "\t a\t\tcomment\n" 8298 "\t in multiple lines\n" 8299 " */", 8300 format(" /*\t \t \n" 8301 " \t \t a\t\tcomment\t \t\n" 8302 " \t \t in multiple lines\t\n" 8303 " \t */", 8304 Tab)); 8305 8306 Tab.UseTab = FormatStyle::UT_ForIndentation; 8307 verifyFormat("{\n" 8308 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8309 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8310 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8311 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8312 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8313 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8314 "};", 8315 Tab); 8316 verifyFormat("enum AA {\n" 8317 "\ta1, // Force multiple lines\n" 8318 "\ta2,\n" 8319 "\ta3\n" 8320 "};", 8321 Tab); 8322 EXPECT_EQ("if (aaaaaaaa && // q\n" 8323 " bb) // w\n" 8324 "\t;", 8325 format("if (aaaaaaaa &&// q\n" 8326 "bb)// w\n" 8327 ";", 8328 Tab)); 8329 verifyFormat("class X {\n" 8330 "\tvoid f() {\n" 8331 "\t\tsomeFunction(parameter1,\n" 8332 "\t\t parameter2);\n" 8333 "\t}\n" 8334 "};", 8335 Tab); 8336 verifyFormat("{\n" 8337 "\tQ(\n" 8338 "\t {\n" 8339 "\t\t int a;\n" 8340 "\t\t someFunction(aaaaaaaa,\n" 8341 "\t\t bbbbbbb);\n" 8342 "\t },\n" 8343 "\t p);\n" 8344 "}", 8345 Tab); 8346 EXPECT_EQ("{\n" 8347 "\t/* aaaa\n" 8348 "\t bbbb */\n" 8349 "}", 8350 format("{\n" 8351 "/* aaaa\n" 8352 " bbbb */\n" 8353 "}", 8354 Tab)); 8355 EXPECT_EQ("{\n" 8356 "\t/*\n" 8357 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8358 "\t bbbbbbbbbbbbb\n" 8359 "\t*/\n" 8360 "}", 8361 format("{\n" 8362 "/*\n" 8363 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8364 "*/\n" 8365 "}", 8366 Tab)); 8367 EXPECT_EQ("{\n" 8368 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8369 "\t// bbbbbbbbbbbbb\n" 8370 "}", 8371 format("{\n" 8372 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8373 "}", 8374 Tab)); 8375 EXPECT_EQ("{\n" 8376 "\t/*\n" 8377 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8378 "\t bbbbbbbbbbbbb\n" 8379 "\t*/\n" 8380 "}", 8381 format("{\n" 8382 "\t/*\n" 8383 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8384 "\t*/\n" 8385 "}", 8386 Tab)); 8387 EXPECT_EQ("{\n" 8388 "\t/*\n" 8389 "\n" 8390 "\t*/\n" 8391 "}", 8392 format("{\n" 8393 "\t/*\n" 8394 "\n" 8395 "\t*/\n" 8396 "}", 8397 Tab)); 8398 EXPECT_EQ("{\n" 8399 "\t/*\n" 8400 " asdf\n" 8401 "\t*/\n" 8402 "}", 8403 format("{\n" 8404 "\t/*\n" 8405 " asdf\n" 8406 "\t*/\n" 8407 "}", 8408 Tab)); 8409 8410 Tab.UseTab = FormatStyle::UT_Never; 8411 EXPECT_EQ("/*\n" 8412 " a\t\tcomment\n" 8413 " in multiple lines\n" 8414 " */", 8415 format(" /*\t \t \n" 8416 " \t \t a\t\tcomment\t \t\n" 8417 " \t \t in multiple lines\t\n" 8418 " \t */", 8419 Tab)); 8420 EXPECT_EQ("/* some\n" 8421 " comment */", 8422 format(" \t \t /* some\n" 8423 " \t \t comment */", 8424 Tab)); 8425 EXPECT_EQ("int a; /* some\n" 8426 " comment */", 8427 format(" \t \t int a; /* some\n" 8428 " \t \t comment */", 8429 Tab)); 8430 8431 EXPECT_EQ("int a; /* some\n" 8432 "comment */", 8433 format(" \t \t int\ta; /* some\n" 8434 " \t \t comment */", 8435 Tab)); 8436 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8437 " comment */", 8438 format(" \t \t f(\"\t\t\"); /* some\n" 8439 " \t \t comment */", 8440 Tab)); 8441 EXPECT_EQ("{\n" 8442 " /*\n" 8443 " * Comment\n" 8444 " */\n" 8445 " int i;\n" 8446 "}", 8447 format("{\n" 8448 "\t/*\n" 8449 "\t * Comment\n" 8450 "\t */\n" 8451 "\t int i;\n" 8452 "}")); 8453 8454 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 8455 Tab.TabWidth = 8; 8456 Tab.IndentWidth = 8; 8457 EXPECT_EQ("if (aaaaaaaa && // q\n" 8458 " bb) // w\n" 8459 "\t;", 8460 format("if (aaaaaaaa &&// q\n" 8461 "bb)// w\n" 8462 ";", 8463 Tab)); 8464 EXPECT_EQ("if (aaa && bbb) // w\n" 8465 "\t;", 8466 format("if(aaa&&bbb)// w\n" 8467 ";", 8468 Tab)); 8469 verifyFormat("class X {\n" 8470 "\tvoid f() {\n" 8471 "\t\tsomeFunction(parameter1,\n" 8472 "\t\t\t parameter2);\n" 8473 "\t}\n" 8474 "};", 8475 Tab); 8476 verifyFormat("#define A \\\n" 8477 "\tvoid f() { \\\n" 8478 "\t\tsomeFunction( \\\n" 8479 "\t\t parameter1, \\\n" 8480 "\t\t parameter2); \\\n" 8481 "\t}", 8482 Tab); 8483 Tab.TabWidth = 4; 8484 Tab.IndentWidth = 8; 8485 verifyFormat("class TabWidth4Indent8 {\n" 8486 "\t\tvoid f() {\n" 8487 "\t\t\t\tsomeFunction(parameter1,\n" 8488 "\t\t\t\t\t\t\t parameter2);\n" 8489 "\t\t}\n" 8490 "};", 8491 Tab); 8492 Tab.TabWidth = 4; 8493 Tab.IndentWidth = 4; 8494 verifyFormat("class TabWidth4Indent4 {\n" 8495 "\tvoid f() {\n" 8496 "\t\tsomeFunction(parameter1,\n" 8497 "\t\t\t\t\t parameter2);\n" 8498 "\t}\n" 8499 "};", 8500 Tab); 8501 Tab.TabWidth = 8; 8502 Tab.IndentWidth = 4; 8503 verifyFormat("class TabWidth8Indent4 {\n" 8504 " void f() {\n" 8505 "\tsomeFunction(parameter1,\n" 8506 "\t\t parameter2);\n" 8507 " }\n" 8508 "};", 8509 Tab); 8510 Tab.TabWidth = 8; 8511 Tab.IndentWidth = 8; 8512 EXPECT_EQ("/*\n" 8513 "\t a\t\tcomment\n" 8514 "\t in multiple lines\n" 8515 " */", 8516 format(" /*\t \t \n" 8517 " \t \t a\t\tcomment\t \t\n" 8518 " \t \t in multiple lines\t\n" 8519 " \t */", 8520 Tab)); 8521 verifyFormat("{\n" 8522 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8523 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8524 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8525 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8526 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8527 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8528 "};", 8529 Tab); 8530 verifyFormat("enum AA {\n" 8531 "\ta1, // Force multiple lines\n" 8532 "\ta2,\n" 8533 "\ta3\n" 8534 "};", 8535 Tab); 8536 EXPECT_EQ("if (aaaaaaaa && // q\n" 8537 " bb) // w\n" 8538 "\t;", 8539 format("if (aaaaaaaa &&// q\n" 8540 "bb)// w\n" 8541 ";", 8542 Tab)); 8543 verifyFormat("class X {\n" 8544 "\tvoid f() {\n" 8545 "\t\tsomeFunction(parameter1,\n" 8546 "\t\t\t parameter2);\n" 8547 "\t}\n" 8548 "};", 8549 Tab); 8550 verifyFormat("{\n" 8551 "\tQ(\n" 8552 "\t {\n" 8553 "\t\t int a;\n" 8554 "\t\t someFunction(aaaaaaaa,\n" 8555 "\t\t\t\t bbbbbbb);\n" 8556 "\t },\n" 8557 "\t p);\n" 8558 "}", 8559 Tab); 8560 EXPECT_EQ("{\n" 8561 "\t/* aaaa\n" 8562 "\t bbbb */\n" 8563 "}", 8564 format("{\n" 8565 "/* aaaa\n" 8566 " bbbb */\n" 8567 "}", 8568 Tab)); 8569 EXPECT_EQ("{\n" 8570 "\t/*\n" 8571 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8572 "\t bbbbbbbbbbbbb\n" 8573 "\t*/\n" 8574 "}", 8575 format("{\n" 8576 "/*\n" 8577 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8578 "*/\n" 8579 "}", 8580 Tab)); 8581 EXPECT_EQ("{\n" 8582 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8583 "\t// bbbbbbbbbbbbb\n" 8584 "}", 8585 format("{\n" 8586 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8587 "}", 8588 Tab)); 8589 EXPECT_EQ("{\n" 8590 "\t/*\n" 8591 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8592 "\t bbbbbbbbbbbbb\n" 8593 "\t*/\n" 8594 "}", 8595 format("{\n" 8596 "\t/*\n" 8597 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8598 "\t*/\n" 8599 "}", 8600 Tab)); 8601 EXPECT_EQ("{\n" 8602 "\t/*\n" 8603 "\n" 8604 "\t*/\n" 8605 "}", 8606 format("{\n" 8607 "\t/*\n" 8608 "\n" 8609 "\t*/\n" 8610 "}", 8611 Tab)); 8612 EXPECT_EQ("{\n" 8613 "\t/*\n" 8614 " asdf\n" 8615 "\t*/\n" 8616 "}", 8617 format("{\n" 8618 "\t/*\n" 8619 " asdf\n" 8620 "\t*/\n" 8621 "}", 8622 Tab)); 8623 EXPECT_EQ("/*\n" 8624 "\t a\t\tcomment\n" 8625 "\t in multiple lines\n" 8626 " */", 8627 format(" /*\t \t \n" 8628 " \t \t a\t\tcomment\t \t\n" 8629 " \t \t in multiple lines\t\n" 8630 " \t */", 8631 Tab)); 8632 EXPECT_EQ("/* some\n" 8633 " comment */", 8634 format(" \t \t /* some\n" 8635 " \t \t comment */", 8636 Tab)); 8637 EXPECT_EQ("int a; /* some\n" 8638 " comment */", 8639 format(" \t \t int a; /* some\n" 8640 " \t \t comment */", 8641 Tab)); 8642 EXPECT_EQ("int a; /* some\n" 8643 "comment */", 8644 format(" \t \t int\ta; /* some\n" 8645 " \t \t comment */", 8646 Tab)); 8647 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8648 " comment */", 8649 format(" \t \t f(\"\t\t\"); /* some\n" 8650 " \t \t comment */", 8651 Tab)); 8652 EXPECT_EQ("{\n" 8653 " /*\n" 8654 " * Comment\n" 8655 " */\n" 8656 " int i;\n" 8657 "}", 8658 format("{\n" 8659 "\t/*\n" 8660 "\t * Comment\n" 8661 "\t */\n" 8662 "\t int i;\n" 8663 "}")); 8664 Tab.AlignConsecutiveAssignments = true; 8665 Tab.AlignConsecutiveDeclarations = true; 8666 Tab.TabWidth = 4; 8667 Tab.IndentWidth = 4; 8668 verifyFormat("class Assign {\n" 8669 "\tvoid f() {\n" 8670 "\t\tint x = 123;\n" 8671 "\t\tint random = 4;\n" 8672 "\t\tstd::string alphabet =\n" 8673 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8674 "\t}\n" 8675 "};", 8676 Tab); 8677 } 8678 8679 TEST_F(FormatTest, CalculatesOriginalColumn) { 8680 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8681 "q\"; /* some\n" 8682 " comment */", 8683 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8684 "q\"; /* some\n" 8685 " comment */", 8686 getLLVMStyle())); 8687 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8688 "/* some\n" 8689 " comment */", 8690 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8691 " /* some\n" 8692 " comment */", 8693 getLLVMStyle())); 8694 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8695 "qqq\n" 8696 "/* some\n" 8697 " comment */", 8698 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8699 "qqq\n" 8700 " /* some\n" 8701 " comment */", 8702 getLLVMStyle())); 8703 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8704 "wwww; /* some\n" 8705 " comment */", 8706 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8707 "wwww; /* some\n" 8708 " comment */", 8709 getLLVMStyle())); 8710 } 8711 8712 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8713 FormatStyle NoSpace = getLLVMStyle(); 8714 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8715 8716 verifyFormat("while(true)\n" 8717 " continue;", 8718 NoSpace); 8719 verifyFormat("for(;;)\n" 8720 " continue;", 8721 NoSpace); 8722 verifyFormat("if(true)\n" 8723 " f();\n" 8724 "else if(true)\n" 8725 " f();", 8726 NoSpace); 8727 verifyFormat("do {\n" 8728 " do_something();\n" 8729 "} while(something());", 8730 NoSpace); 8731 verifyFormat("switch(x) {\n" 8732 "default:\n" 8733 " break;\n" 8734 "}", 8735 NoSpace); 8736 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8737 verifyFormat("size_t x = sizeof(x);", NoSpace); 8738 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8739 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8740 verifyFormat("alignas(128) char a[128];", NoSpace); 8741 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8742 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8743 verifyFormat("int f() throw(Deprecated);", NoSpace); 8744 verifyFormat("typedef void (*cb)(int);", NoSpace); 8745 verifyFormat("T A::operator()();", NoSpace); 8746 verifyFormat("X A::operator++(T);", NoSpace); 8747 8748 FormatStyle Space = getLLVMStyle(); 8749 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8750 8751 verifyFormat("int f ();", Space); 8752 verifyFormat("void f (int a, T b) {\n" 8753 " while (true)\n" 8754 " continue;\n" 8755 "}", 8756 Space); 8757 verifyFormat("if (true)\n" 8758 " f ();\n" 8759 "else if (true)\n" 8760 " f ();", 8761 Space); 8762 verifyFormat("do {\n" 8763 " do_something ();\n" 8764 "} while (something ());", 8765 Space); 8766 verifyFormat("switch (x) {\n" 8767 "default:\n" 8768 " break;\n" 8769 "}", 8770 Space); 8771 verifyFormat("A::A () : a (1) {}", Space); 8772 verifyFormat("void f () __attribute__ ((asdf));", Space); 8773 verifyFormat("*(&a + 1);\n" 8774 "&((&a)[1]);\n" 8775 "a[(b + c) * d];\n" 8776 "(((a + 1) * 2) + 3) * 4;", 8777 Space); 8778 verifyFormat("#define A(x) x", Space); 8779 verifyFormat("#define A (x) x", Space); 8780 verifyFormat("#if defined(x)\n" 8781 "#endif", 8782 Space); 8783 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8784 verifyFormat("size_t x = sizeof (x);", Space); 8785 verifyFormat("auto f (int x) -> decltype (x);", Space); 8786 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8787 verifyFormat("alignas (128) char a[128];", Space); 8788 verifyFormat("size_t x = alignof (MyType);", Space); 8789 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8790 verifyFormat("int f () throw (Deprecated);", Space); 8791 verifyFormat("typedef void (*cb) (int);", Space); 8792 verifyFormat("T A::operator() ();", Space); 8793 verifyFormat("X A::operator++ (T);", Space); 8794 } 8795 8796 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8797 FormatStyle Spaces = getLLVMStyle(); 8798 8799 Spaces.SpacesInParentheses = true; 8800 verifyFormat("call( x, y, z );", Spaces); 8801 verifyFormat("call();", Spaces); 8802 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8803 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8804 Spaces); 8805 verifyFormat("while ( (bool)1 )\n" 8806 " continue;", 8807 Spaces); 8808 verifyFormat("for ( ;; )\n" 8809 " continue;", 8810 Spaces); 8811 verifyFormat("if ( true )\n" 8812 " f();\n" 8813 "else if ( true )\n" 8814 " f();", 8815 Spaces); 8816 verifyFormat("do {\n" 8817 " do_something( (int)i );\n" 8818 "} while ( something() );", 8819 Spaces); 8820 verifyFormat("switch ( x ) {\n" 8821 "default:\n" 8822 " break;\n" 8823 "}", 8824 Spaces); 8825 8826 Spaces.SpacesInParentheses = false; 8827 Spaces.SpacesInCStyleCastParentheses = true; 8828 verifyFormat("Type *A = ( Type * )P;", Spaces); 8829 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8830 verifyFormat("x = ( int32 )y;", Spaces); 8831 verifyFormat("int a = ( int )(2.0f);", Spaces); 8832 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8833 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8834 verifyFormat("#define x (( int )-1)", Spaces); 8835 8836 // Run the first set of tests again with: 8837 Spaces.SpacesInParentheses = false; 8838 Spaces.SpaceInEmptyParentheses = true; 8839 Spaces.SpacesInCStyleCastParentheses = true; 8840 verifyFormat("call(x, y, z);", Spaces); 8841 verifyFormat("call( );", Spaces); 8842 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8843 verifyFormat("while (( bool )1)\n" 8844 " continue;", 8845 Spaces); 8846 verifyFormat("for (;;)\n" 8847 " continue;", 8848 Spaces); 8849 verifyFormat("if (true)\n" 8850 " f( );\n" 8851 "else if (true)\n" 8852 " f( );", 8853 Spaces); 8854 verifyFormat("do {\n" 8855 " do_something(( int )i);\n" 8856 "} while (something( ));", 8857 Spaces); 8858 verifyFormat("switch (x) {\n" 8859 "default:\n" 8860 " break;\n" 8861 "}", 8862 Spaces); 8863 8864 // Run the first set of tests again with: 8865 Spaces.SpaceAfterCStyleCast = true; 8866 verifyFormat("call(x, y, z);", Spaces); 8867 verifyFormat("call( );", Spaces); 8868 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8869 verifyFormat("while (( bool ) 1)\n" 8870 " continue;", 8871 Spaces); 8872 verifyFormat("for (;;)\n" 8873 " continue;", 8874 Spaces); 8875 verifyFormat("if (true)\n" 8876 " f( );\n" 8877 "else if (true)\n" 8878 " f( );", 8879 Spaces); 8880 verifyFormat("do {\n" 8881 " do_something(( int ) i);\n" 8882 "} while (something( ));", 8883 Spaces); 8884 verifyFormat("switch (x) {\n" 8885 "default:\n" 8886 " break;\n" 8887 "}", 8888 Spaces); 8889 8890 // Run subset of tests again with: 8891 Spaces.SpacesInCStyleCastParentheses = false; 8892 Spaces.SpaceAfterCStyleCast = true; 8893 verifyFormat("while ((bool) 1)\n" 8894 " continue;", 8895 Spaces); 8896 verifyFormat("do {\n" 8897 " do_something((int) i);\n" 8898 "} while (something( ));", 8899 Spaces); 8900 } 8901 8902 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8903 verifyFormat("int a[5];"); 8904 verifyFormat("a[3] += 42;"); 8905 8906 FormatStyle Spaces = getLLVMStyle(); 8907 Spaces.SpacesInSquareBrackets = true; 8908 // Lambdas unchanged. 8909 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8910 verifyFormat("return [i, args...] {};", Spaces); 8911 8912 // Not lambdas. 8913 verifyFormat("int a[ 5 ];", Spaces); 8914 verifyFormat("a[ 3 ] += 42;", Spaces); 8915 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8916 verifyFormat("double &operator[](int i) { return 0; }\n" 8917 "int i;", 8918 Spaces); 8919 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8920 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8921 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8922 } 8923 8924 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8925 verifyFormat("int a = 5;"); 8926 verifyFormat("a += 42;"); 8927 verifyFormat("a or_eq 8;"); 8928 8929 FormatStyle Spaces = getLLVMStyle(); 8930 Spaces.SpaceBeforeAssignmentOperators = false; 8931 verifyFormat("int a= 5;", Spaces); 8932 verifyFormat("a+= 42;", Spaces); 8933 verifyFormat("a or_eq 8;", Spaces); 8934 } 8935 8936 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8937 FormatStyle Alignment = getLLVMStyle(); 8938 Alignment.AlignConsecutiveAssignments = false; 8939 verifyFormat("int a = 5;\n" 8940 "int oneTwoThree = 123;", 8941 Alignment); 8942 verifyFormat("int a = 5;\n" 8943 "int oneTwoThree = 123;", 8944 Alignment); 8945 8946 Alignment.AlignConsecutiveAssignments = true; 8947 verifyFormat("int a = 5;\n" 8948 "int oneTwoThree = 123;", 8949 Alignment); 8950 verifyFormat("int a = method();\n" 8951 "int oneTwoThree = 133;", 8952 Alignment); 8953 verifyFormat("a &= 5;\n" 8954 "bcd *= 5;\n" 8955 "ghtyf += 5;\n" 8956 "dvfvdb -= 5;\n" 8957 "a /= 5;\n" 8958 "vdsvsv %= 5;\n" 8959 "sfdbddfbdfbb ^= 5;\n" 8960 "dvsdsv |= 5;\n" 8961 "int dsvvdvsdvvv = 123;", 8962 Alignment); 8963 verifyFormat("int i = 1, j = 10;\n" 8964 "something = 2000;", 8965 Alignment); 8966 verifyFormat("something = 2000;\n" 8967 "int i = 1, j = 10;\n", 8968 Alignment); 8969 verifyFormat("something = 2000;\n" 8970 "another = 911;\n" 8971 "int i = 1, j = 10;\n" 8972 "oneMore = 1;\n" 8973 "i = 2;", 8974 Alignment); 8975 verifyFormat("int a = 5;\n" 8976 "int one = 1;\n" 8977 "method();\n" 8978 "int oneTwoThree = 123;\n" 8979 "int oneTwo = 12;", 8980 Alignment); 8981 verifyFormat("int oneTwoThree = 123;\n" 8982 "int oneTwo = 12;\n" 8983 "method();\n", 8984 Alignment); 8985 verifyFormat("int oneTwoThree = 123; // comment\n" 8986 "int oneTwo = 12; // comment", 8987 Alignment); 8988 EXPECT_EQ("int a = 5;\n" 8989 "\n" 8990 "int oneTwoThree = 123;", 8991 format("int a = 5;\n" 8992 "\n" 8993 "int oneTwoThree= 123;", 8994 Alignment)); 8995 EXPECT_EQ("int a = 5;\n" 8996 "int one = 1;\n" 8997 "\n" 8998 "int oneTwoThree = 123;", 8999 format("int a = 5;\n" 9000 "int one = 1;\n" 9001 "\n" 9002 "int oneTwoThree = 123;", 9003 Alignment)); 9004 EXPECT_EQ("int a = 5;\n" 9005 "int one = 1;\n" 9006 "\n" 9007 "int oneTwoThree = 123;\n" 9008 "int oneTwo = 12;", 9009 format("int a = 5;\n" 9010 "int one = 1;\n" 9011 "\n" 9012 "int oneTwoThree = 123;\n" 9013 "int oneTwo = 12;", 9014 Alignment)); 9015 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9016 verifyFormat("#define A \\\n" 9017 " int aaaa = 12; \\\n" 9018 " int b = 23; \\\n" 9019 " int ccc = 234; \\\n" 9020 " int dddddddddd = 2345;", 9021 Alignment); 9022 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9023 verifyFormat("#define A \\\n" 9024 " int aaaa = 12; \\\n" 9025 " int b = 23; \\\n" 9026 " int ccc = 234; \\\n" 9027 " int dddddddddd = 2345;", 9028 Alignment); 9029 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9030 verifyFormat("#define A " 9031 " \\\n" 9032 " int aaaa = 12; " 9033 " \\\n" 9034 " int b = 23; " 9035 " \\\n" 9036 " int ccc = 234; " 9037 " \\\n" 9038 " int dddddddddd = 2345;", 9039 Alignment); 9040 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9041 "k = 4, int l = 5,\n" 9042 " int m = 6) {\n" 9043 " int j = 10;\n" 9044 " otherThing = 1;\n" 9045 "}", 9046 Alignment); 9047 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9048 " int i = 1;\n" 9049 " int j = 2;\n" 9050 " int big = 10000;\n" 9051 "}", 9052 Alignment); 9053 verifyFormat("class C {\n" 9054 "public:\n" 9055 " int i = 1;\n" 9056 " virtual void f() = 0;\n" 9057 "};", 9058 Alignment); 9059 verifyFormat("int i = 1;\n" 9060 "if (SomeType t = getSomething()) {\n" 9061 "}\n" 9062 "int j = 2;\n" 9063 "int big = 10000;", 9064 Alignment); 9065 verifyFormat("int j = 7;\n" 9066 "for (int k = 0; k < N; ++k) {\n" 9067 "}\n" 9068 "int j = 2;\n" 9069 "int big = 10000;\n" 9070 "}", 9071 Alignment); 9072 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9073 verifyFormat("int i = 1;\n" 9074 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9075 " = someLooooooooooooooooongFunction();\n" 9076 "int j = 2;", 9077 Alignment); 9078 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9079 verifyFormat("int i = 1;\n" 9080 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9081 " someLooooooooooooooooongFunction();\n" 9082 "int j = 2;", 9083 Alignment); 9084 9085 verifyFormat("auto lambda = []() {\n" 9086 " auto i = 0;\n" 9087 " return 0;\n" 9088 "};\n" 9089 "int i = 0;\n" 9090 "auto v = type{\n" 9091 " i = 1, //\n" 9092 " (i = 2), //\n" 9093 " i = 3 //\n" 9094 "};", 9095 Alignment); 9096 9097 verifyFormat( 9098 "int i = 1;\n" 9099 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9100 " loooooooooooooooooooooongParameterB);\n" 9101 "int j = 2;", 9102 Alignment); 9103 9104 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 9105 " typename B = very_long_type_name_1,\n" 9106 " typename T_2 = very_long_type_name_2>\n" 9107 "auto foo() {}\n", 9108 Alignment); 9109 verifyFormat("int a, b = 1;\n" 9110 "int c = 2;\n" 9111 "int dd = 3;\n", 9112 Alignment); 9113 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9114 "float b[1][] = {{3.f}};\n", 9115 Alignment); 9116 verifyFormat("for (int i = 0; i < 1; i++)\n" 9117 " int x = 1;\n", 9118 Alignment); 9119 verifyFormat("for (i = 0; i < 1; i++)\n" 9120 " x = 1;\n" 9121 "y = 1;\n", 9122 Alignment); 9123 } 9124 9125 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 9126 FormatStyle Alignment = getLLVMStyle(); 9127 Alignment.AlignConsecutiveDeclarations = false; 9128 verifyFormat("float const a = 5;\n" 9129 "int oneTwoThree = 123;", 9130 Alignment); 9131 verifyFormat("int a = 5;\n" 9132 "float const oneTwoThree = 123;", 9133 Alignment); 9134 9135 Alignment.AlignConsecutiveDeclarations = true; 9136 verifyFormat("float const a = 5;\n" 9137 "int oneTwoThree = 123;", 9138 Alignment); 9139 verifyFormat("int a = method();\n" 9140 "float const oneTwoThree = 133;", 9141 Alignment); 9142 verifyFormat("int i = 1, j = 10;\n" 9143 "something = 2000;", 9144 Alignment); 9145 verifyFormat("something = 2000;\n" 9146 "int i = 1, j = 10;\n", 9147 Alignment); 9148 verifyFormat("float something = 2000;\n" 9149 "double another = 911;\n" 9150 "int i = 1, j = 10;\n" 9151 "const int *oneMore = 1;\n" 9152 "unsigned i = 2;", 9153 Alignment); 9154 verifyFormat("float a = 5;\n" 9155 "int one = 1;\n" 9156 "method();\n" 9157 "const double oneTwoThree = 123;\n" 9158 "const unsigned int oneTwo = 12;", 9159 Alignment); 9160 verifyFormat("int oneTwoThree{0}; // comment\n" 9161 "unsigned oneTwo; // comment", 9162 Alignment); 9163 EXPECT_EQ("float const a = 5;\n" 9164 "\n" 9165 "int oneTwoThree = 123;", 9166 format("float const a = 5;\n" 9167 "\n" 9168 "int oneTwoThree= 123;", 9169 Alignment)); 9170 EXPECT_EQ("float a = 5;\n" 9171 "int one = 1;\n" 9172 "\n" 9173 "unsigned oneTwoThree = 123;", 9174 format("float a = 5;\n" 9175 "int one = 1;\n" 9176 "\n" 9177 "unsigned oneTwoThree = 123;", 9178 Alignment)); 9179 EXPECT_EQ("float a = 5;\n" 9180 "int one = 1;\n" 9181 "\n" 9182 "unsigned oneTwoThree = 123;\n" 9183 "int oneTwo = 12;", 9184 format("float a = 5;\n" 9185 "int one = 1;\n" 9186 "\n" 9187 "unsigned oneTwoThree = 123;\n" 9188 "int oneTwo = 12;", 9189 Alignment)); 9190 // Function prototype alignment 9191 verifyFormat("int a();\n" 9192 "double b();", 9193 Alignment); 9194 verifyFormat("int a(int x);\n" 9195 "double b();", 9196 Alignment); 9197 unsigned OldColumnLimit = Alignment.ColumnLimit; 9198 // We need to set ColumnLimit to zero, in order to stress nested alignments, 9199 // otherwise the function parameters will be re-flowed onto a single line. 9200 Alignment.ColumnLimit = 0; 9201 EXPECT_EQ("int a(int x,\n" 9202 " float y);\n" 9203 "double b(int x,\n" 9204 " double y);", 9205 format("int a(int x,\n" 9206 " float y);\n" 9207 "double b(int x,\n" 9208 " double y);", 9209 Alignment)); 9210 // This ensures that function parameters of function declarations are 9211 // correctly indented when their owning functions are indented. 9212 // The failure case here is for 'double y' to not be indented enough. 9213 EXPECT_EQ("double a(int x);\n" 9214 "int b(int y,\n" 9215 " double z);", 9216 format("double a(int x);\n" 9217 "int b(int y,\n" 9218 " double z);", 9219 Alignment)); 9220 // Set ColumnLimit low so that we induce wrapping immediately after 9221 // the function name and opening paren. 9222 Alignment.ColumnLimit = 13; 9223 verifyFormat("int function(\n" 9224 " int x,\n" 9225 " bool y);", 9226 Alignment); 9227 Alignment.ColumnLimit = OldColumnLimit; 9228 // Ensure function pointers don't screw up recursive alignment 9229 verifyFormat("int a(int x, void (*fp)(int y));\n" 9230 "double b();", 9231 Alignment); 9232 Alignment.AlignConsecutiveAssignments = true; 9233 // Ensure recursive alignment is broken by function braces, so that the 9234 // "a = 1" does not align with subsequent assignments inside the function 9235 // body. 9236 verifyFormat("int func(int a = 1) {\n" 9237 " int b = 2;\n" 9238 " int cc = 3;\n" 9239 "}", 9240 Alignment); 9241 verifyFormat("float something = 2000;\n" 9242 "double another = 911;\n" 9243 "int i = 1, j = 10;\n" 9244 "const int *oneMore = 1;\n" 9245 "unsigned i = 2;", 9246 Alignment); 9247 verifyFormat("int oneTwoThree = {0}; // comment\n" 9248 "unsigned oneTwo = 0; // comment", 9249 Alignment); 9250 // Make sure that scope is correctly tracked, in the absence of braces 9251 verifyFormat("for (int i = 0; i < n; i++)\n" 9252 " j = i;\n" 9253 "double x = 1;\n", 9254 Alignment); 9255 verifyFormat("if (int i = 0)\n" 9256 " j = i;\n" 9257 "double x = 1;\n", 9258 Alignment); 9259 // Ensure operator[] and operator() are comprehended 9260 verifyFormat("struct test {\n" 9261 " long long int foo();\n" 9262 " int operator[](int a);\n" 9263 " double bar();\n" 9264 "};\n", 9265 Alignment); 9266 verifyFormat("struct test {\n" 9267 " long long int foo();\n" 9268 " int operator()(int a);\n" 9269 " double bar();\n" 9270 "};\n", 9271 Alignment); 9272 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 9273 " int const i = 1;\n" 9274 " int * j = 2;\n" 9275 " int big = 10000;\n" 9276 "\n" 9277 " unsigned oneTwoThree = 123;\n" 9278 " int oneTwo = 12;\n" 9279 " method();\n" 9280 " float k = 2;\n" 9281 " int ll = 10000;\n" 9282 "}", 9283 format("void SomeFunction(int parameter= 0) {\n" 9284 " int const i= 1;\n" 9285 " int *j=2;\n" 9286 " int big = 10000;\n" 9287 "\n" 9288 "unsigned oneTwoThree =123;\n" 9289 "int oneTwo = 12;\n" 9290 " method();\n" 9291 "float k= 2;\n" 9292 "int ll=10000;\n" 9293 "}", 9294 Alignment)); 9295 Alignment.AlignConsecutiveAssignments = false; 9296 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 9297 verifyFormat("#define A \\\n" 9298 " int aaaa = 12; \\\n" 9299 " float b = 23; \\\n" 9300 " const int ccc = 234; \\\n" 9301 " unsigned dddddddddd = 2345;", 9302 Alignment); 9303 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9304 verifyFormat("#define A \\\n" 9305 " int aaaa = 12; \\\n" 9306 " float b = 23; \\\n" 9307 " const int ccc = 234; \\\n" 9308 " unsigned dddddddddd = 2345;", 9309 Alignment); 9310 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 9311 Alignment.ColumnLimit = 30; 9312 verifyFormat("#define A \\\n" 9313 " int aaaa = 12; \\\n" 9314 " float b = 23; \\\n" 9315 " const int ccc = 234; \\\n" 9316 " int dddddddddd = 2345;", 9317 Alignment); 9318 Alignment.ColumnLimit = 80; 9319 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 9320 "k = 4, int l = 5,\n" 9321 " int m = 6) {\n" 9322 " const int j = 10;\n" 9323 " otherThing = 1;\n" 9324 "}", 9325 Alignment); 9326 verifyFormat("void SomeFunction(int parameter = 0) {\n" 9327 " int const i = 1;\n" 9328 " int * j = 2;\n" 9329 " int big = 10000;\n" 9330 "}", 9331 Alignment); 9332 verifyFormat("class C {\n" 9333 "public:\n" 9334 " int i = 1;\n" 9335 " virtual void f() = 0;\n" 9336 "};", 9337 Alignment); 9338 verifyFormat("float i = 1;\n" 9339 "if (SomeType t = getSomething()) {\n" 9340 "}\n" 9341 "const unsigned j = 2;\n" 9342 "int big = 10000;", 9343 Alignment); 9344 verifyFormat("float j = 7;\n" 9345 "for (int k = 0; k < N; ++k) {\n" 9346 "}\n" 9347 "unsigned j = 2;\n" 9348 "int big = 10000;\n" 9349 "}", 9350 Alignment); 9351 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9352 verifyFormat("float i = 1;\n" 9353 "LooooooooooongType loooooooooooooooooooooongVariable\n" 9354 " = someLooooooooooooooooongFunction();\n" 9355 "int j = 2;", 9356 Alignment); 9357 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9358 verifyFormat("int i = 1;\n" 9359 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 9360 " someLooooooooooooooooongFunction();\n" 9361 "int j = 2;", 9362 Alignment); 9363 9364 Alignment.AlignConsecutiveAssignments = true; 9365 verifyFormat("auto lambda = []() {\n" 9366 " auto ii = 0;\n" 9367 " float j = 0;\n" 9368 " return 0;\n" 9369 "};\n" 9370 "int i = 0;\n" 9371 "float i2 = 0;\n" 9372 "auto v = type{\n" 9373 " i = 1, //\n" 9374 " (i = 2), //\n" 9375 " i = 3 //\n" 9376 "};", 9377 Alignment); 9378 Alignment.AlignConsecutiveAssignments = false; 9379 9380 verifyFormat( 9381 "int i = 1;\n" 9382 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 9383 " loooooooooooooooooooooongParameterB);\n" 9384 "int j = 2;", 9385 Alignment); 9386 9387 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 9388 // We expect declarations and assignments to align, as long as it doesn't 9389 // exceed the column limit, starting a new alignment sequence whenever it 9390 // happens. 9391 Alignment.AlignConsecutiveAssignments = true; 9392 Alignment.ColumnLimit = 30; 9393 verifyFormat("float ii = 1;\n" 9394 "unsigned j = 2;\n" 9395 "int someVerylongVariable = 1;\n" 9396 "AnotherLongType ll = 123456;\n" 9397 "VeryVeryLongType k = 2;\n" 9398 "int myvar = 1;", 9399 Alignment); 9400 Alignment.ColumnLimit = 80; 9401 Alignment.AlignConsecutiveAssignments = false; 9402 9403 verifyFormat( 9404 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 9405 " typename LongType, typename B>\n" 9406 "auto foo() {}\n", 9407 Alignment); 9408 verifyFormat("float a, b = 1;\n" 9409 "int c = 2;\n" 9410 "int dd = 3;\n", 9411 Alignment); 9412 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9413 "float b[1][] = {{3.f}};\n", 9414 Alignment); 9415 Alignment.AlignConsecutiveAssignments = true; 9416 verifyFormat("float a, b = 1;\n" 9417 "int c = 2;\n" 9418 "int dd = 3;\n", 9419 Alignment); 9420 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 9421 "float b[1][] = {{3.f}};\n", 9422 Alignment); 9423 Alignment.AlignConsecutiveAssignments = false; 9424 9425 Alignment.ColumnLimit = 30; 9426 Alignment.BinPackParameters = false; 9427 verifyFormat("void foo(float a,\n" 9428 " float b,\n" 9429 " int c,\n" 9430 " uint32_t *d) {\n" 9431 " int * e = 0;\n" 9432 " float f = 0;\n" 9433 " double g = 0;\n" 9434 "}\n" 9435 "void bar(ino_t a,\n" 9436 " int b,\n" 9437 " uint32_t *c,\n" 9438 " bool d) {}\n", 9439 Alignment); 9440 Alignment.BinPackParameters = true; 9441 Alignment.ColumnLimit = 80; 9442 9443 // Bug 33507 9444 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 9445 verifyFormat( 9446 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 9447 " static const Version verVs2017;\n" 9448 " return true;\n" 9449 "});\n", 9450 Alignment); 9451 Alignment.PointerAlignment = FormatStyle::PAS_Right; 9452 } 9453 9454 TEST_F(FormatTest, LinuxBraceBreaking) { 9455 FormatStyle LinuxBraceStyle = getLLVMStyle(); 9456 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 9457 verifyFormat("namespace a\n" 9458 "{\n" 9459 "class A\n" 9460 "{\n" 9461 " void f()\n" 9462 " {\n" 9463 " if (true) {\n" 9464 " a();\n" 9465 " b();\n" 9466 " } else {\n" 9467 " a();\n" 9468 " }\n" 9469 " }\n" 9470 " void g() { return; }\n" 9471 "};\n" 9472 "struct B {\n" 9473 " int x;\n" 9474 "};\n" 9475 "} // namespace a\n", 9476 LinuxBraceStyle); 9477 verifyFormat("enum X {\n" 9478 " Y = 0,\n" 9479 "}\n", 9480 LinuxBraceStyle); 9481 verifyFormat("struct S {\n" 9482 " int Type;\n" 9483 " union {\n" 9484 " int x;\n" 9485 " double y;\n" 9486 " } Value;\n" 9487 " class C\n" 9488 " {\n" 9489 " MyFavoriteType Value;\n" 9490 " } Class;\n" 9491 "}\n", 9492 LinuxBraceStyle); 9493 } 9494 9495 TEST_F(FormatTest, MozillaBraceBreaking) { 9496 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9497 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9498 MozillaBraceStyle.FixNamespaceComments = false; 9499 verifyFormat("namespace a {\n" 9500 "class A\n" 9501 "{\n" 9502 " void f()\n" 9503 " {\n" 9504 " if (true) {\n" 9505 " a();\n" 9506 " b();\n" 9507 " }\n" 9508 " }\n" 9509 " void g() { return; }\n" 9510 "};\n" 9511 "enum E\n" 9512 "{\n" 9513 " A,\n" 9514 " // foo\n" 9515 " B,\n" 9516 " C\n" 9517 "};\n" 9518 "struct B\n" 9519 "{\n" 9520 " int x;\n" 9521 "};\n" 9522 "}\n", 9523 MozillaBraceStyle); 9524 verifyFormat("struct S\n" 9525 "{\n" 9526 " int Type;\n" 9527 " union\n" 9528 " {\n" 9529 " int x;\n" 9530 " double y;\n" 9531 " } Value;\n" 9532 " class C\n" 9533 " {\n" 9534 " MyFavoriteType Value;\n" 9535 " } Class;\n" 9536 "}\n", 9537 MozillaBraceStyle); 9538 } 9539 9540 TEST_F(FormatTest, StroustrupBraceBreaking) { 9541 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9542 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9543 verifyFormat("namespace a {\n" 9544 "class A {\n" 9545 " void f()\n" 9546 " {\n" 9547 " if (true) {\n" 9548 " a();\n" 9549 " b();\n" 9550 " }\n" 9551 " }\n" 9552 " void g() { return; }\n" 9553 "};\n" 9554 "struct B {\n" 9555 " int x;\n" 9556 "};\n" 9557 "} // namespace a\n", 9558 StroustrupBraceStyle); 9559 9560 verifyFormat("void foo()\n" 9561 "{\n" 9562 " if (a) {\n" 9563 " a();\n" 9564 " }\n" 9565 " else {\n" 9566 " b();\n" 9567 " }\n" 9568 "}\n", 9569 StroustrupBraceStyle); 9570 9571 verifyFormat("#ifdef _DEBUG\n" 9572 "int foo(int i = 0)\n" 9573 "#else\n" 9574 "int foo(int i = 5)\n" 9575 "#endif\n" 9576 "{\n" 9577 " return i;\n" 9578 "}", 9579 StroustrupBraceStyle); 9580 9581 verifyFormat("void foo() {}\n" 9582 "void bar()\n" 9583 "#ifdef _DEBUG\n" 9584 "{\n" 9585 " foo();\n" 9586 "}\n" 9587 "#else\n" 9588 "{\n" 9589 "}\n" 9590 "#endif", 9591 StroustrupBraceStyle); 9592 9593 verifyFormat("void foobar() { int i = 5; }\n" 9594 "#ifdef _DEBUG\n" 9595 "void bar() {}\n" 9596 "#else\n" 9597 "void bar() { foobar(); }\n" 9598 "#endif", 9599 StroustrupBraceStyle); 9600 } 9601 9602 TEST_F(FormatTest, AllmanBraceBreaking) { 9603 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9604 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9605 9606 EXPECT_EQ("namespace a\n" 9607 "{\n" 9608 "void f();\n" 9609 "void g();\n" 9610 "} // namespace a\n", 9611 format("namespace a\n" 9612 "{\n" 9613 "void f();\n" 9614 "void g();\n" 9615 "}\n", 9616 AllmanBraceStyle)); 9617 9618 verifyFormat("namespace a\n" 9619 "{\n" 9620 "class A\n" 9621 "{\n" 9622 " void f()\n" 9623 " {\n" 9624 " if (true)\n" 9625 " {\n" 9626 " a();\n" 9627 " b();\n" 9628 " }\n" 9629 " }\n" 9630 " void g() { return; }\n" 9631 "};\n" 9632 "struct B\n" 9633 "{\n" 9634 " int x;\n" 9635 "};\n" 9636 "} // namespace a", 9637 AllmanBraceStyle); 9638 9639 verifyFormat("void f()\n" 9640 "{\n" 9641 " if (true)\n" 9642 " {\n" 9643 " a();\n" 9644 " }\n" 9645 " else if (false)\n" 9646 " {\n" 9647 " b();\n" 9648 " }\n" 9649 " else\n" 9650 " {\n" 9651 " c();\n" 9652 " }\n" 9653 "}\n", 9654 AllmanBraceStyle); 9655 9656 verifyFormat("void f()\n" 9657 "{\n" 9658 " for (int i = 0; i < 10; ++i)\n" 9659 " {\n" 9660 " a();\n" 9661 " }\n" 9662 " while (false)\n" 9663 " {\n" 9664 " b();\n" 9665 " }\n" 9666 " do\n" 9667 " {\n" 9668 " c();\n" 9669 " } while (false)\n" 9670 "}\n", 9671 AllmanBraceStyle); 9672 9673 verifyFormat("void f(int a)\n" 9674 "{\n" 9675 " switch (a)\n" 9676 " {\n" 9677 " case 0:\n" 9678 " break;\n" 9679 " case 1:\n" 9680 " {\n" 9681 " break;\n" 9682 " }\n" 9683 " case 2:\n" 9684 " {\n" 9685 " }\n" 9686 " break;\n" 9687 " default:\n" 9688 " break;\n" 9689 " }\n" 9690 "}\n", 9691 AllmanBraceStyle); 9692 9693 verifyFormat("enum X\n" 9694 "{\n" 9695 " Y = 0,\n" 9696 "}\n", 9697 AllmanBraceStyle); 9698 verifyFormat("enum X\n" 9699 "{\n" 9700 " Y = 0\n" 9701 "}\n", 9702 AllmanBraceStyle); 9703 9704 verifyFormat("@interface BSApplicationController ()\n" 9705 "{\n" 9706 "@private\n" 9707 " id _extraIvar;\n" 9708 "}\n" 9709 "@end\n", 9710 AllmanBraceStyle); 9711 9712 verifyFormat("#ifdef _DEBUG\n" 9713 "int foo(int i = 0)\n" 9714 "#else\n" 9715 "int foo(int i = 5)\n" 9716 "#endif\n" 9717 "{\n" 9718 " return i;\n" 9719 "}", 9720 AllmanBraceStyle); 9721 9722 verifyFormat("void foo() {}\n" 9723 "void bar()\n" 9724 "#ifdef _DEBUG\n" 9725 "{\n" 9726 " foo();\n" 9727 "}\n" 9728 "#else\n" 9729 "{\n" 9730 "}\n" 9731 "#endif", 9732 AllmanBraceStyle); 9733 9734 verifyFormat("void foobar() { int i = 5; }\n" 9735 "#ifdef _DEBUG\n" 9736 "void bar() {}\n" 9737 "#else\n" 9738 "void bar() { foobar(); }\n" 9739 "#endif", 9740 AllmanBraceStyle); 9741 9742 // This shouldn't affect ObjC blocks.. 9743 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9744 " // ...\n" 9745 " int i;\n" 9746 "}];", 9747 AllmanBraceStyle); 9748 verifyFormat("void (^block)(void) = ^{\n" 9749 " // ...\n" 9750 " int i;\n" 9751 "};", 9752 AllmanBraceStyle); 9753 // .. or dict literals. 9754 verifyFormat("void f()\n" 9755 "{\n" 9756 " // ...\n" 9757 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 9758 "}", 9759 AllmanBraceStyle); 9760 verifyFormat("void f()\n" 9761 "{\n" 9762 " // ...\n" 9763 " [object someMethod:@{a : @\"b\"}];\n" 9764 "}", 9765 AllmanBraceStyle); 9766 verifyFormat("int f()\n" 9767 "{ // comment\n" 9768 " return 42;\n" 9769 "}", 9770 AllmanBraceStyle); 9771 9772 AllmanBraceStyle.ColumnLimit = 19; 9773 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9774 AllmanBraceStyle.ColumnLimit = 18; 9775 verifyFormat("void f()\n" 9776 "{\n" 9777 " int i;\n" 9778 "}", 9779 AllmanBraceStyle); 9780 AllmanBraceStyle.ColumnLimit = 80; 9781 9782 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9783 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9784 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9785 verifyFormat("void f(bool b)\n" 9786 "{\n" 9787 " if (b)\n" 9788 " {\n" 9789 " return;\n" 9790 " }\n" 9791 "}\n", 9792 BreakBeforeBraceShortIfs); 9793 verifyFormat("void f(bool b)\n" 9794 "{\n" 9795 " if constexpr (b)\n" 9796 " {\n" 9797 " return;\n" 9798 " }\n" 9799 "}\n", 9800 BreakBeforeBraceShortIfs); 9801 verifyFormat("void f(bool b)\n" 9802 "{\n" 9803 " if (b) return;\n" 9804 "}\n", 9805 BreakBeforeBraceShortIfs); 9806 verifyFormat("void f(bool b)\n" 9807 "{\n" 9808 " if constexpr (b) return;\n" 9809 "}\n", 9810 BreakBeforeBraceShortIfs); 9811 verifyFormat("void f(bool b)\n" 9812 "{\n" 9813 " while (b)\n" 9814 " {\n" 9815 " return;\n" 9816 " }\n" 9817 "}\n", 9818 BreakBeforeBraceShortIfs); 9819 } 9820 9821 TEST_F(FormatTest, GNUBraceBreaking) { 9822 FormatStyle GNUBraceStyle = getLLVMStyle(); 9823 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9824 verifyFormat("namespace a\n" 9825 "{\n" 9826 "class A\n" 9827 "{\n" 9828 " void f()\n" 9829 " {\n" 9830 " int a;\n" 9831 " {\n" 9832 " int b;\n" 9833 " }\n" 9834 " if (true)\n" 9835 " {\n" 9836 " a();\n" 9837 " b();\n" 9838 " }\n" 9839 " }\n" 9840 " void g() { return; }\n" 9841 "}\n" 9842 "} // namespace a", 9843 GNUBraceStyle); 9844 9845 verifyFormat("void f()\n" 9846 "{\n" 9847 " if (true)\n" 9848 " {\n" 9849 " a();\n" 9850 " }\n" 9851 " else if (false)\n" 9852 " {\n" 9853 " b();\n" 9854 " }\n" 9855 " else\n" 9856 " {\n" 9857 " c();\n" 9858 " }\n" 9859 "}\n", 9860 GNUBraceStyle); 9861 9862 verifyFormat("void f()\n" 9863 "{\n" 9864 " for (int i = 0; i < 10; ++i)\n" 9865 " {\n" 9866 " a();\n" 9867 " }\n" 9868 " while (false)\n" 9869 " {\n" 9870 " b();\n" 9871 " }\n" 9872 " do\n" 9873 " {\n" 9874 " c();\n" 9875 " }\n" 9876 " while (false);\n" 9877 "}\n", 9878 GNUBraceStyle); 9879 9880 verifyFormat("void f(int a)\n" 9881 "{\n" 9882 " switch (a)\n" 9883 " {\n" 9884 " case 0:\n" 9885 " break;\n" 9886 " case 1:\n" 9887 " {\n" 9888 " break;\n" 9889 " }\n" 9890 " case 2:\n" 9891 " {\n" 9892 " }\n" 9893 " break;\n" 9894 " default:\n" 9895 " break;\n" 9896 " }\n" 9897 "}\n", 9898 GNUBraceStyle); 9899 9900 verifyFormat("enum X\n" 9901 "{\n" 9902 " Y = 0,\n" 9903 "}\n", 9904 GNUBraceStyle); 9905 9906 verifyFormat("@interface BSApplicationController ()\n" 9907 "{\n" 9908 "@private\n" 9909 " id _extraIvar;\n" 9910 "}\n" 9911 "@end\n", 9912 GNUBraceStyle); 9913 9914 verifyFormat("#ifdef _DEBUG\n" 9915 "int foo(int i = 0)\n" 9916 "#else\n" 9917 "int foo(int i = 5)\n" 9918 "#endif\n" 9919 "{\n" 9920 " return i;\n" 9921 "}", 9922 GNUBraceStyle); 9923 9924 verifyFormat("void foo() {}\n" 9925 "void bar()\n" 9926 "#ifdef _DEBUG\n" 9927 "{\n" 9928 " foo();\n" 9929 "}\n" 9930 "#else\n" 9931 "{\n" 9932 "}\n" 9933 "#endif", 9934 GNUBraceStyle); 9935 9936 verifyFormat("void foobar() { int i = 5; }\n" 9937 "#ifdef _DEBUG\n" 9938 "void bar() {}\n" 9939 "#else\n" 9940 "void bar() { foobar(); }\n" 9941 "#endif", 9942 GNUBraceStyle); 9943 } 9944 9945 TEST_F(FormatTest, WebKitBraceBreaking) { 9946 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9947 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9948 WebKitBraceStyle.FixNamespaceComments = false; 9949 verifyFormat("namespace a {\n" 9950 "class A {\n" 9951 " void f()\n" 9952 " {\n" 9953 " if (true) {\n" 9954 " a();\n" 9955 " b();\n" 9956 " }\n" 9957 " }\n" 9958 " void g() { return; }\n" 9959 "};\n" 9960 "enum E {\n" 9961 " A,\n" 9962 " // foo\n" 9963 " B,\n" 9964 " C\n" 9965 "};\n" 9966 "struct B {\n" 9967 " int x;\n" 9968 "};\n" 9969 "}\n", 9970 WebKitBraceStyle); 9971 verifyFormat("struct S {\n" 9972 " int Type;\n" 9973 " union {\n" 9974 " int x;\n" 9975 " double y;\n" 9976 " } Value;\n" 9977 " class C {\n" 9978 " MyFavoriteType Value;\n" 9979 " } Class;\n" 9980 "};\n", 9981 WebKitBraceStyle); 9982 } 9983 9984 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9985 verifyFormat("void f() {\n" 9986 " try {\n" 9987 " } catch (const Exception &e) {\n" 9988 " }\n" 9989 "}\n", 9990 getLLVMStyle()); 9991 } 9992 9993 TEST_F(FormatTest, UnderstandsPragmas) { 9994 verifyFormat("#pragma omp reduction(| : var)"); 9995 verifyFormat("#pragma omp reduction(+ : var)"); 9996 9997 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9998 "(including parentheses).", 9999 format("#pragma mark Any non-hyphenated or hyphenated string " 10000 "(including parentheses).")); 10001 } 10002 10003 TEST_F(FormatTest, UnderstandPragmaOption) { 10004 verifyFormat("#pragma option -C -A"); 10005 10006 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 10007 } 10008 10009 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 10010 FormatStyle Style = getLLVMStyle(); 10011 Style.ColumnLimit = 20; 10012 10013 verifyFormat("int a; // the\n" 10014 " // comment", Style); 10015 EXPECT_EQ("int a; /* first line\n" 10016 " * second\n" 10017 " * line third\n" 10018 " * line\n" 10019 " */", 10020 format("int a; /* first line\n" 10021 " * second\n" 10022 " * line third\n" 10023 " * line\n" 10024 " */", 10025 Style)); 10026 EXPECT_EQ("int a; // first line\n" 10027 " // second\n" 10028 " // line third\n" 10029 " // line", 10030 format("int a; // first line\n" 10031 " // second line\n" 10032 " // third line", 10033 Style)); 10034 10035 Style.PenaltyExcessCharacter = 90; 10036 verifyFormat("int a; // the comment", Style); 10037 EXPECT_EQ("int a; // the comment\n" 10038 " // aaa", 10039 format("int a; // the comment aaa", Style)); 10040 EXPECT_EQ("int a; /* first line\n" 10041 " * second line\n" 10042 " * third line\n" 10043 " */", 10044 format("int a; /* first line\n" 10045 " * second line\n" 10046 " * third line\n" 10047 " */", 10048 Style)); 10049 EXPECT_EQ("int a; // first line\n" 10050 " // second line\n" 10051 " // third line", 10052 format("int a; // first line\n" 10053 " // second line\n" 10054 " // third line", 10055 Style)); 10056 // FIXME: Investigate why this is not getting the same layout as the test 10057 // above. 10058 EXPECT_EQ("int a; /* first line\n" 10059 " * second line\n" 10060 " * third line\n" 10061 " */", 10062 format("int a; /* first line second line third line" 10063 "\n*/", 10064 Style)); 10065 10066 EXPECT_EQ("// foo bar baz bazfoo\n" 10067 "// foo bar foo bar\n", 10068 format("// foo bar baz bazfoo\n" 10069 "// foo bar foo bar\n", 10070 Style)); 10071 EXPECT_EQ("// foo bar baz bazfoo\n" 10072 "// foo bar foo bar\n", 10073 format("// foo bar baz bazfoo\n" 10074 "// foo bar foo bar\n", 10075 Style)); 10076 10077 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 10078 // next one. 10079 EXPECT_EQ("// foo bar baz bazfoo\n" 10080 "// bar foo bar\n", 10081 format("// foo bar baz bazfoo bar\n" 10082 "// foo bar\n", 10083 Style)); 10084 10085 EXPECT_EQ("// foo bar baz bazfoo\n" 10086 "// foo bar baz bazfoo\n" 10087 "// bar foo bar\n", 10088 format("// foo bar baz bazfoo\n" 10089 "// foo bar baz bazfoo bar\n" 10090 "// foo bar\n", 10091 Style)); 10092 10093 EXPECT_EQ("// foo bar baz bazfoo\n" 10094 "// foo bar baz bazfoo\n" 10095 "// bar foo bar\n", 10096 format("// foo bar baz bazfoo\n" 10097 "// foo bar baz bazfoo bar\n" 10098 "// foo bar\n", 10099 Style)); 10100 10101 // Make sure we do not keep protruding characters if strict mode reflow is 10102 // cheaper than keeping protruding characters. 10103 Style.ColumnLimit = 21; 10104 EXPECT_EQ("// foo foo foo foo\n" 10105 "// foo foo foo foo\n" 10106 "// foo foo foo foo\n", 10107 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", 10108 Style)); 10109 10110 EXPECT_EQ("int a = /* long block\n" 10111 " comment */\n" 10112 " 42;", 10113 format("int a = /* long block comment */ 42;", Style)); 10114 } 10115 10116 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 10117 for (size_t i = 1; i < Styles.size(); ++i) \ 10118 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 10119 << " differs from Style #0" 10120 10121 TEST_F(FormatTest, GetsPredefinedStyleByName) { 10122 SmallVector<FormatStyle, 3> Styles; 10123 Styles.resize(3); 10124 10125 Styles[0] = getLLVMStyle(); 10126 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 10127 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 10128 EXPECT_ALL_STYLES_EQUAL(Styles); 10129 10130 Styles[0] = getGoogleStyle(); 10131 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 10132 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 10133 EXPECT_ALL_STYLES_EQUAL(Styles); 10134 10135 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10136 EXPECT_TRUE( 10137 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 10138 EXPECT_TRUE( 10139 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 10140 EXPECT_ALL_STYLES_EQUAL(Styles); 10141 10142 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 10143 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 10144 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 10145 EXPECT_ALL_STYLES_EQUAL(Styles); 10146 10147 Styles[0] = getMozillaStyle(); 10148 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 10149 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 10150 EXPECT_ALL_STYLES_EQUAL(Styles); 10151 10152 Styles[0] = getWebKitStyle(); 10153 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 10154 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 10155 EXPECT_ALL_STYLES_EQUAL(Styles); 10156 10157 Styles[0] = getGNUStyle(); 10158 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 10159 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 10160 EXPECT_ALL_STYLES_EQUAL(Styles); 10161 10162 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 10163 } 10164 10165 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 10166 SmallVector<FormatStyle, 8> Styles; 10167 Styles.resize(2); 10168 10169 Styles[0] = getGoogleStyle(); 10170 Styles[1] = getLLVMStyle(); 10171 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10172 EXPECT_ALL_STYLES_EQUAL(Styles); 10173 10174 Styles.resize(5); 10175 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 10176 Styles[1] = getLLVMStyle(); 10177 Styles[1].Language = FormatStyle::LK_JavaScript; 10178 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 10179 10180 Styles[2] = getLLVMStyle(); 10181 Styles[2].Language = FormatStyle::LK_JavaScript; 10182 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 10183 "BasedOnStyle: Google", 10184 &Styles[2]) 10185 .value()); 10186 10187 Styles[3] = getLLVMStyle(); 10188 Styles[3].Language = FormatStyle::LK_JavaScript; 10189 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 10190 "Language: JavaScript", 10191 &Styles[3]) 10192 .value()); 10193 10194 Styles[4] = getLLVMStyle(); 10195 Styles[4].Language = FormatStyle::LK_JavaScript; 10196 EXPECT_EQ(0, parseConfiguration("---\n" 10197 "BasedOnStyle: LLVM\n" 10198 "IndentWidth: 123\n" 10199 "---\n" 10200 "BasedOnStyle: Google\n" 10201 "Language: JavaScript", 10202 &Styles[4]) 10203 .value()); 10204 EXPECT_ALL_STYLES_EQUAL(Styles); 10205 } 10206 10207 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 10208 Style.FIELD = false; \ 10209 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 10210 EXPECT_TRUE(Style.FIELD); \ 10211 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 10212 EXPECT_FALSE(Style.FIELD); 10213 10214 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 10215 10216 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 10217 Style.STRUCT.FIELD = false; \ 10218 EXPECT_EQ(0, \ 10219 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 10220 .value()); \ 10221 EXPECT_TRUE(Style.STRUCT.FIELD); \ 10222 EXPECT_EQ(0, \ 10223 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 10224 .value()); \ 10225 EXPECT_FALSE(Style.STRUCT.FIELD); 10226 10227 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 10228 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 10229 10230 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 10231 EXPECT_NE(VALUE, Style.FIELD); \ 10232 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 10233 EXPECT_EQ(VALUE, Style.FIELD) 10234 10235 TEST_F(FormatTest, ParsesConfigurationBools) { 10236 FormatStyle Style = {}; 10237 Style.Language = FormatStyle::LK_Cpp; 10238 CHECK_PARSE_BOOL(AlignOperands); 10239 CHECK_PARSE_BOOL(AlignTrailingComments); 10240 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 10241 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 10242 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 10243 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 10244 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 10245 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 10246 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 10247 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 10248 CHECK_PARSE_BOOL(BinPackArguments); 10249 CHECK_PARSE_BOOL(BinPackParameters); 10250 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 10251 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 10252 CHECK_PARSE_BOOL(BreakStringLiterals); 10253 CHECK_PARSE_BOOL(BreakBeforeInheritanceComma) 10254 CHECK_PARSE_BOOL(CompactNamespaces); 10255 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 10256 CHECK_PARSE_BOOL(DerivePointerAlignment); 10257 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 10258 CHECK_PARSE_BOOL(DisableFormat); 10259 CHECK_PARSE_BOOL(IndentCaseLabels); 10260 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 10261 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 10262 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 10263 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 10264 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 10265 CHECK_PARSE_BOOL(ReflowComments); 10266 CHECK_PARSE_BOOL(SortIncludes); 10267 CHECK_PARSE_BOOL(SortUsingDeclarations); 10268 CHECK_PARSE_BOOL(SpacesInParentheses); 10269 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 10270 CHECK_PARSE_BOOL(SpacesInAngles); 10271 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 10272 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 10273 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 10274 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 10275 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 10276 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 10277 10278 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 10279 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 10280 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 10281 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 10282 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 10283 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 10284 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 10285 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 10286 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 10287 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 10288 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 10289 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 10290 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 10291 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 10292 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 10293 } 10294 10295 #undef CHECK_PARSE_BOOL 10296 10297 TEST_F(FormatTest, ParsesConfiguration) { 10298 FormatStyle Style = {}; 10299 Style.Language = FormatStyle::LK_Cpp; 10300 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 10301 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 10302 ConstructorInitializerIndentWidth, 1234u); 10303 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 10304 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 10305 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 10306 CHECK_PARSE("PenaltyBreakAssignment: 1234", 10307 PenaltyBreakAssignment, 1234u); 10308 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 10309 PenaltyBreakBeforeFirstCallParameter, 1234u); 10310 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 10311 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 10312 PenaltyReturnTypeOnItsOwnLine, 1234u); 10313 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 10314 SpacesBeforeTrailingComments, 1234u); 10315 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 10316 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 10317 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 10318 10319 Style.PointerAlignment = FormatStyle::PAS_Middle; 10320 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 10321 FormatStyle::PAS_Left); 10322 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 10323 FormatStyle::PAS_Right); 10324 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 10325 FormatStyle::PAS_Middle); 10326 // For backward compatibility: 10327 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 10328 FormatStyle::PAS_Left); 10329 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 10330 FormatStyle::PAS_Right); 10331 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 10332 FormatStyle::PAS_Middle); 10333 10334 Style.Standard = FormatStyle::LS_Auto; 10335 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 10336 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 10337 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 10338 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 10339 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 10340 10341 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 10342 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 10343 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 10344 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 10345 FormatStyle::BOS_None); 10346 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 10347 FormatStyle::BOS_All); 10348 // For backward compatibility: 10349 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 10350 FormatStyle::BOS_None); 10351 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 10352 FormatStyle::BOS_All); 10353 10354 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 10355 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 10356 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10357 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 10358 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 10359 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 10360 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 10361 // For backward compatibility: 10362 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 10363 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 10364 10365 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10366 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 10367 FormatStyle::BAS_Align); 10368 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 10369 FormatStyle::BAS_DontAlign); 10370 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 10371 FormatStyle::BAS_AlwaysBreak); 10372 // For backward compatibility: 10373 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 10374 FormatStyle::BAS_DontAlign); 10375 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 10376 FormatStyle::BAS_Align); 10377 10378 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 10379 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 10380 FormatStyle::ENAS_DontAlign); 10381 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 10382 FormatStyle::ENAS_Left); 10383 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 10384 FormatStyle::ENAS_Right); 10385 // For backward compatibility: 10386 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 10387 FormatStyle::ENAS_Left); 10388 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 10389 FormatStyle::ENAS_Right); 10390 10391 Style.UseTab = FormatStyle::UT_ForIndentation; 10392 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 10393 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 10394 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 10395 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 10396 FormatStyle::UT_ForContinuationAndIndentation); 10397 // For backward compatibility: 10398 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 10399 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 10400 10401 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 10402 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 10403 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10404 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 10405 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 10406 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 10407 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 10408 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 10409 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10410 // For backward compatibility: 10411 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 10412 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 10413 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 10414 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 10415 10416 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 10417 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 10418 FormatStyle::SBPO_Never); 10419 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 10420 FormatStyle::SBPO_Always); 10421 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 10422 FormatStyle::SBPO_ControlStatements); 10423 // For backward compatibility: 10424 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 10425 FormatStyle::SBPO_Never); 10426 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 10427 FormatStyle::SBPO_ControlStatements); 10428 10429 Style.ColumnLimit = 123; 10430 FormatStyle BaseStyle = getLLVMStyle(); 10431 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 10432 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 10433 10434 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 10435 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 10436 FormatStyle::BS_Attach); 10437 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 10438 FormatStyle::BS_Linux); 10439 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 10440 FormatStyle::BS_Mozilla); 10441 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 10442 FormatStyle::BS_Stroustrup); 10443 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 10444 FormatStyle::BS_Allman); 10445 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 10446 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 10447 FormatStyle::BS_WebKit); 10448 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 10449 FormatStyle::BS_Custom); 10450 10451 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10452 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 10453 FormatStyle::RTBS_None); 10454 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 10455 FormatStyle::RTBS_All); 10456 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 10457 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 10458 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 10459 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 10460 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 10461 AlwaysBreakAfterReturnType, 10462 FormatStyle::RTBS_TopLevelDefinitions); 10463 10464 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 10465 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 10466 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 10467 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 10468 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 10469 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 10470 AlwaysBreakAfterDefinitionReturnType, 10471 FormatStyle::DRTBS_TopLevel); 10472 10473 Style.NamespaceIndentation = FormatStyle::NI_All; 10474 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 10475 FormatStyle::NI_None); 10476 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 10477 FormatStyle::NI_Inner); 10478 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 10479 FormatStyle::NI_All); 10480 10481 // FIXME: This is required because parsing a configuration simply overwrites 10482 // the first N elements of the list instead of resetting it. 10483 Style.ForEachMacros.clear(); 10484 std::vector<std::string> BoostForeach; 10485 BoostForeach.push_back("BOOST_FOREACH"); 10486 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 10487 std::vector<std::string> BoostAndQForeach; 10488 BoostAndQForeach.push_back("BOOST_FOREACH"); 10489 BoostAndQForeach.push_back("Q_FOREACH"); 10490 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 10491 BoostAndQForeach); 10492 10493 Style.IncludeCategories.clear(); 10494 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 10495 {".*", 1}}; 10496 CHECK_PARSE("IncludeCategories:\n" 10497 " - Regex: abc/.*\n" 10498 " Priority: 2\n" 10499 " - Regex: .*\n" 10500 " Priority: 1", 10501 IncludeCategories, ExpectedCategories); 10502 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 10503 10504 Style.RawStringFormats.clear(); 10505 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 10506 { 10507 FormatStyle::LK_TextProto, 10508 {"pb", "proto"}, 10509 {"PARSE_TEXT_PROTO"}, 10510 /*CanonicalDelimiter=*/"", 10511 "llvm", 10512 }, 10513 { 10514 FormatStyle::LK_Cpp, 10515 {"cc", "cpp"}, 10516 {"C_CODEBLOCK", "CPPEVAL"}, 10517 /*CanonicalDelimiter=*/"cc", 10518 /*BasedOnStyle=*/"", 10519 }, 10520 }; 10521 10522 CHECK_PARSE("RawStringFormats:\n" 10523 " - Language: TextProto\n" 10524 " Delimiters:\n" 10525 " - 'pb'\n" 10526 " - 'proto'\n" 10527 " EnclosingFunctions:\n" 10528 " - 'PARSE_TEXT_PROTO'\n" 10529 " BasedOnStyle: llvm\n" 10530 " - Language: Cpp\n" 10531 " Delimiters:\n" 10532 " - 'cc'\n" 10533 " - 'cpp'\n" 10534 " EnclosingFunctions:\n" 10535 " - 'C_CODEBLOCK'\n" 10536 " - 'CPPEVAL'\n" 10537 " CanonicalDelimiter: 'cc'", 10538 RawStringFormats, ExpectedRawStringFormats); 10539 } 10540 10541 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 10542 FormatStyle Style = {}; 10543 Style.Language = FormatStyle::LK_Cpp; 10544 CHECK_PARSE("Language: Cpp\n" 10545 "IndentWidth: 12", 10546 IndentWidth, 12u); 10547 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 10548 "IndentWidth: 34", 10549 &Style), 10550 ParseError::Unsuitable); 10551 EXPECT_EQ(12u, Style.IndentWidth); 10552 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10553 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10554 10555 Style.Language = FormatStyle::LK_JavaScript; 10556 CHECK_PARSE("Language: JavaScript\n" 10557 "IndentWidth: 12", 10558 IndentWidth, 12u); 10559 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 10560 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 10561 "IndentWidth: 34", 10562 &Style), 10563 ParseError::Unsuitable); 10564 EXPECT_EQ(23u, Style.IndentWidth); 10565 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 10566 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10567 10568 CHECK_PARSE("BasedOnStyle: LLVM\n" 10569 "IndentWidth: 67", 10570 IndentWidth, 67u); 10571 10572 CHECK_PARSE("---\n" 10573 "Language: JavaScript\n" 10574 "IndentWidth: 12\n" 10575 "---\n" 10576 "Language: Cpp\n" 10577 "IndentWidth: 34\n" 10578 "...\n", 10579 IndentWidth, 12u); 10580 10581 Style.Language = FormatStyle::LK_Cpp; 10582 CHECK_PARSE("---\n" 10583 "Language: JavaScript\n" 10584 "IndentWidth: 12\n" 10585 "---\n" 10586 "Language: Cpp\n" 10587 "IndentWidth: 34\n" 10588 "...\n", 10589 IndentWidth, 34u); 10590 CHECK_PARSE("---\n" 10591 "IndentWidth: 78\n" 10592 "---\n" 10593 "Language: JavaScript\n" 10594 "IndentWidth: 56\n" 10595 "...\n", 10596 IndentWidth, 78u); 10597 10598 Style.ColumnLimit = 123; 10599 Style.IndentWidth = 234; 10600 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 10601 Style.TabWidth = 345; 10602 EXPECT_FALSE(parseConfiguration("---\n" 10603 "IndentWidth: 456\n" 10604 "BreakBeforeBraces: Allman\n" 10605 "---\n" 10606 "Language: JavaScript\n" 10607 "IndentWidth: 111\n" 10608 "TabWidth: 111\n" 10609 "---\n" 10610 "Language: Cpp\n" 10611 "BreakBeforeBraces: Stroustrup\n" 10612 "TabWidth: 789\n" 10613 "...\n", 10614 &Style)); 10615 EXPECT_EQ(123u, Style.ColumnLimit); 10616 EXPECT_EQ(456u, Style.IndentWidth); 10617 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 10618 EXPECT_EQ(789u, Style.TabWidth); 10619 10620 EXPECT_EQ(parseConfiguration("---\n" 10621 "Language: JavaScript\n" 10622 "IndentWidth: 56\n" 10623 "---\n" 10624 "IndentWidth: 78\n" 10625 "...\n", 10626 &Style), 10627 ParseError::Error); 10628 EXPECT_EQ(parseConfiguration("---\n" 10629 "Language: JavaScript\n" 10630 "IndentWidth: 56\n" 10631 "---\n" 10632 "Language: JavaScript\n" 10633 "IndentWidth: 78\n" 10634 "...\n", 10635 &Style), 10636 ParseError::Error); 10637 10638 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 10639 } 10640 10641 #undef CHECK_PARSE 10642 10643 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 10644 FormatStyle Style = {}; 10645 Style.Language = FormatStyle::LK_JavaScript; 10646 Style.BreakBeforeTernaryOperators = true; 10647 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 10648 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10649 10650 Style.BreakBeforeTernaryOperators = true; 10651 EXPECT_EQ(0, parseConfiguration("---\n" 10652 "BasedOnStyle: Google\n" 10653 "---\n" 10654 "Language: JavaScript\n" 10655 "IndentWidth: 76\n" 10656 "...\n", 10657 &Style) 10658 .value()); 10659 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10660 EXPECT_EQ(76u, Style.IndentWidth); 10661 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10662 } 10663 10664 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10665 FormatStyle Style = getLLVMStyle(); 10666 std::string YAML = configurationAsText(Style); 10667 FormatStyle ParsedStyle = {}; 10668 ParsedStyle.Language = FormatStyle::LK_Cpp; 10669 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10670 EXPECT_EQ(Style, ParsedStyle); 10671 } 10672 10673 TEST_F(FormatTest, WorksFor8bitEncodings) { 10674 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10675 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10676 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10677 "\"\xef\xee\xf0\xf3...\"", 10678 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10679 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10680 "\xef\xee\xf0\xf3...\"", 10681 getLLVMStyleWithColumns(12))); 10682 } 10683 10684 TEST_F(FormatTest, HandlesUTF8BOM) { 10685 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10686 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10687 format("\xef\xbb\xbf#include <iostream>")); 10688 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10689 format("\xef\xbb\xbf\n#include <iostream>")); 10690 } 10691 10692 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10693 #if !defined(_MSC_VER) 10694 10695 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10696 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10697 getLLVMStyleWithColumns(35)); 10698 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10699 getLLVMStyleWithColumns(31)); 10700 verifyFormat("// Однажды в студёную зимнюю пору...", 10701 getLLVMStyleWithColumns(36)); 10702 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10703 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10704 getLLVMStyleWithColumns(39)); 10705 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10706 getLLVMStyleWithColumns(35)); 10707 } 10708 10709 TEST_F(FormatTest, SplitsUTF8Strings) { 10710 // Non-printable characters' width is currently considered to be the length in 10711 // bytes in UTF8. The characters can be displayed in very different manner 10712 // (zero-width, single width with a substitution glyph, expanded to their code 10713 // (e.g. "<8d>"), so there's no single correct way to handle them. 10714 EXPECT_EQ("\"aaaaÄ\"\n" 10715 "\"\xc2\x8d\";", 10716 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10717 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10718 "\"\xc2\x8d\";", 10719 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10720 EXPECT_EQ("\"Однажды, в \"\n" 10721 "\"студёную \"\n" 10722 "\"зимнюю \"\n" 10723 "\"пору,\"", 10724 format("\"Однажды, в студёную зимнюю пору,\"", 10725 getLLVMStyleWithColumns(13))); 10726 EXPECT_EQ( 10727 "\"一 二 三 \"\n" 10728 "\"四 五六 \"\n" 10729 "\"七 八 九 \"\n" 10730 "\"十\"", 10731 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10732 EXPECT_EQ("\"一\t\"\n" 10733 "\"二 \t\"\n" 10734 "\"三 四 \"\n" 10735 "\"五\t\"\n" 10736 "\"六 \t\"\n" 10737 "\"七 \"\n" 10738 "\"八九十\tqq\"", 10739 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10740 getLLVMStyleWithColumns(11))); 10741 10742 // UTF8 character in an escape sequence. 10743 EXPECT_EQ("\"aaaaaa\"\n" 10744 "\"\\\xC2\x8D\"", 10745 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10746 } 10747 10748 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10749 EXPECT_EQ("const char *sssss =\n" 10750 " \"一二三四五六七八\\\n" 10751 " 九 十\";", 10752 format("const char *sssss = \"一二三四五六七八\\\n" 10753 " 九 十\";", 10754 getLLVMStyleWithColumns(30))); 10755 } 10756 10757 TEST_F(FormatTest, SplitsUTF8LineComments) { 10758 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10759 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10760 EXPECT_EQ("// Я из лесу\n" 10761 "// вышел; был\n" 10762 "// сильный\n" 10763 "// мороз.", 10764 format("// Я из лесу вышел; был сильный мороз.", 10765 getLLVMStyleWithColumns(13))); 10766 EXPECT_EQ("// 一二三\n" 10767 "// 四五六七\n" 10768 "// 八 九\n" 10769 "// 十", 10770 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10771 } 10772 10773 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10774 EXPECT_EQ("/* Гляжу,\n" 10775 " * поднимается\n" 10776 " * медленно в\n" 10777 " * гору\n" 10778 " * Лошадка,\n" 10779 " * везущая\n" 10780 " * хворосту\n" 10781 " * воз. */", 10782 format("/* Гляжу, поднимается медленно в гору\n" 10783 " * Лошадка, везущая хворосту воз. */", 10784 getLLVMStyleWithColumns(13))); 10785 EXPECT_EQ( 10786 "/* 一二三\n" 10787 " * 四五六七\n" 10788 " * 八 九\n" 10789 " * 十 */", 10790 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10791 EXPECT_EQ("/* \n" 10792 " * \n" 10793 " * - */", 10794 format("/* - */", getLLVMStyleWithColumns(12))); 10795 } 10796 10797 #endif // _MSC_VER 10798 10799 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10800 FormatStyle Style = getLLVMStyle(); 10801 10802 Style.ConstructorInitializerIndentWidth = 4; 10803 verifyFormat( 10804 "SomeClass::Constructor()\n" 10805 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10806 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10807 Style); 10808 10809 Style.ConstructorInitializerIndentWidth = 2; 10810 verifyFormat( 10811 "SomeClass::Constructor()\n" 10812 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10813 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10814 Style); 10815 10816 Style.ConstructorInitializerIndentWidth = 0; 10817 verifyFormat( 10818 "SomeClass::Constructor()\n" 10819 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10820 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10821 Style); 10822 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10823 verifyFormat( 10824 "SomeLongTemplateVariableName<\n" 10825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10826 Style); 10827 verifyFormat( 10828 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10829 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10830 Style); 10831 } 10832 10833 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10834 FormatStyle Style = getLLVMStyle(); 10835 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 10836 Style.ConstructorInitializerIndentWidth = 4; 10837 verifyFormat("SomeClass::Constructor()\n" 10838 " : a(a)\n" 10839 " , b(b)\n" 10840 " , c(c) {}", 10841 Style); 10842 verifyFormat("SomeClass::Constructor()\n" 10843 " : a(a) {}", 10844 Style); 10845 10846 Style.ColumnLimit = 0; 10847 verifyFormat("SomeClass::Constructor()\n" 10848 " : a(a) {}", 10849 Style); 10850 verifyFormat("SomeClass::Constructor() noexcept\n" 10851 " : a(a) {}", 10852 Style); 10853 verifyFormat("SomeClass::Constructor()\n" 10854 " : a(a)\n" 10855 " , b(b)\n" 10856 " , c(c) {}", 10857 Style); 10858 verifyFormat("SomeClass::Constructor()\n" 10859 " : a(a) {\n" 10860 " foo();\n" 10861 " bar();\n" 10862 "}", 10863 Style); 10864 10865 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10866 verifyFormat("SomeClass::Constructor()\n" 10867 " : a(a)\n" 10868 " , b(b)\n" 10869 " , c(c) {\n}", 10870 Style); 10871 verifyFormat("SomeClass::Constructor()\n" 10872 " : a(a) {\n}", 10873 Style); 10874 10875 Style.ColumnLimit = 80; 10876 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10877 Style.ConstructorInitializerIndentWidth = 2; 10878 verifyFormat("SomeClass::Constructor()\n" 10879 " : a(a)\n" 10880 " , b(b)\n" 10881 " , c(c) {}", 10882 Style); 10883 10884 Style.ConstructorInitializerIndentWidth = 0; 10885 verifyFormat("SomeClass::Constructor()\n" 10886 ": a(a)\n" 10887 ", b(b)\n" 10888 ", c(c) {}", 10889 Style); 10890 10891 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10892 Style.ConstructorInitializerIndentWidth = 4; 10893 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10894 verifyFormat( 10895 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10896 Style); 10897 verifyFormat( 10898 "SomeClass::Constructor()\n" 10899 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10900 Style); 10901 Style.ConstructorInitializerIndentWidth = 4; 10902 Style.ColumnLimit = 60; 10903 verifyFormat("SomeClass::Constructor()\n" 10904 " : aaaaaaaa(aaaaaaaa)\n" 10905 " , aaaaaaaa(aaaaaaaa)\n" 10906 " , aaaaaaaa(aaaaaaaa) {}", 10907 Style); 10908 } 10909 10910 TEST_F(FormatTest, Destructors) { 10911 verifyFormat("void F(int &i) { i.~int(); }"); 10912 verifyFormat("void F(int &i) { i->~int(); }"); 10913 } 10914 10915 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10916 FormatStyle Style = getWebKitStyle(); 10917 10918 // Don't indent in outer namespaces. 10919 verifyFormat("namespace outer {\n" 10920 "int i;\n" 10921 "namespace inner {\n" 10922 " int i;\n" 10923 "} // namespace inner\n" 10924 "} // namespace outer\n" 10925 "namespace other_outer {\n" 10926 "int i;\n" 10927 "}", 10928 Style); 10929 10930 // Don't indent case labels. 10931 verifyFormat("switch (variable) {\n" 10932 "case 1:\n" 10933 "case 2:\n" 10934 " doSomething();\n" 10935 " break;\n" 10936 "default:\n" 10937 " ++variable;\n" 10938 "}", 10939 Style); 10940 10941 // Wrap before binary operators. 10942 EXPECT_EQ("void f()\n" 10943 "{\n" 10944 " if (aaaaaaaaaaaaaaaa\n" 10945 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10946 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10947 " return;\n" 10948 "}", 10949 format("void f() {\n" 10950 "if (aaaaaaaaaaaaaaaa\n" 10951 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10952 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10953 "return;\n" 10954 "}", 10955 Style)); 10956 10957 // Allow functions on a single line. 10958 verifyFormat("void f() { return; }", Style); 10959 10960 // Constructor initializers are formatted one per line with the "," on the 10961 // new line. 10962 verifyFormat("Constructor()\n" 10963 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10964 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10965 " aaaaaaaaaaaaaa)\n" 10966 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10967 "{\n" 10968 "}", 10969 Style); 10970 verifyFormat("SomeClass::Constructor()\n" 10971 " : a(a)\n" 10972 "{\n" 10973 "}", 10974 Style); 10975 EXPECT_EQ("SomeClass::Constructor()\n" 10976 " : a(a)\n" 10977 "{\n" 10978 "}", 10979 format("SomeClass::Constructor():a(a){}", Style)); 10980 verifyFormat("SomeClass::Constructor()\n" 10981 " : a(a)\n" 10982 " , b(b)\n" 10983 " , c(c)\n" 10984 "{\n" 10985 "}", 10986 Style); 10987 verifyFormat("SomeClass::Constructor()\n" 10988 " : a(a)\n" 10989 "{\n" 10990 " foo();\n" 10991 " bar();\n" 10992 "}", 10993 Style); 10994 10995 // Access specifiers should be aligned left. 10996 verifyFormat("class C {\n" 10997 "public:\n" 10998 " int i;\n" 10999 "};", 11000 Style); 11001 11002 // Do not align comments. 11003 verifyFormat("int a; // Do not\n" 11004 "double b; // align comments.", 11005 Style); 11006 11007 // Do not align operands. 11008 EXPECT_EQ("ASSERT(aaaa\n" 11009 " || bbbb);", 11010 format("ASSERT ( aaaa\n||bbbb);", Style)); 11011 11012 // Accept input's line breaks. 11013 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 11014 " || bbbbbbbbbbbbbbb) {\n" 11015 " i++;\n" 11016 "}", 11017 format("if (aaaaaaaaaaaaaaa\n" 11018 "|| bbbbbbbbbbbbbbb) { i++; }", 11019 Style)); 11020 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 11021 " i++;\n" 11022 "}", 11023 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 11024 11025 // Don't automatically break all macro definitions (llvm.org/PR17842). 11026 verifyFormat("#define aNumber 10", Style); 11027 // However, generally keep the line breaks that the user authored. 11028 EXPECT_EQ("#define aNumber \\\n" 11029 " 10", 11030 format("#define aNumber \\\n" 11031 " 10", 11032 Style)); 11033 11034 // Keep empty and one-element array literals on a single line. 11035 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 11036 " copyItems:YES];", 11037 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 11038 "copyItems:YES];", 11039 Style)); 11040 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 11041 " copyItems:YES];", 11042 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 11043 " copyItems:YES];", 11044 Style)); 11045 // FIXME: This does not seem right, there should be more indentation before 11046 // the array literal's entries. Nested blocks have the same problem. 11047 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11048 " @\"a\",\n" 11049 " @\"a\"\n" 11050 "]\n" 11051 " copyItems:YES];", 11052 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 11053 " @\"a\",\n" 11054 " @\"a\"\n" 11055 " ]\n" 11056 " copyItems:YES];", 11057 Style)); 11058 EXPECT_EQ( 11059 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11060 " copyItems:YES];", 11061 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 11062 " copyItems:YES];", 11063 Style)); 11064 11065 verifyFormat("[self.a b:c c:d];", Style); 11066 EXPECT_EQ("[self.a b:c\n" 11067 " c:d];", 11068 format("[self.a b:c\n" 11069 "c:d];", 11070 Style)); 11071 } 11072 11073 TEST_F(FormatTest, FormatsLambdas) { 11074 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 11075 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 11076 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 11077 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 11078 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 11079 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 11080 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 11081 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 11082 verifyFormat("int x = f(*+[] {});"); 11083 verifyFormat("void f() {\n" 11084 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 11085 "}\n"); 11086 verifyFormat("void f() {\n" 11087 " other(x.begin(), //\n" 11088 " x.end(), //\n" 11089 " [&](int, int) { return 1; });\n" 11090 "}\n"); 11091 verifyFormat("SomeFunction([]() { // A cool function...\n" 11092 " return 43;\n" 11093 "});"); 11094 EXPECT_EQ("SomeFunction([]() {\n" 11095 "#define A a\n" 11096 " return 43;\n" 11097 "});", 11098 format("SomeFunction([](){\n" 11099 "#define A a\n" 11100 "return 43;\n" 11101 "});")); 11102 verifyFormat("void f() {\n" 11103 " SomeFunction([](decltype(x), A *a) {});\n" 11104 "}"); 11105 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11106 " [](const aaaaaaaaaa &a) { return a; });"); 11107 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 11108 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 11109 "});"); 11110 verifyFormat("Constructor()\n" 11111 " : Field([] { // comment\n" 11112 " int i;\n" 11113 " }) {}"); 11114 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 11115 " return some_parameter.size();\n" 11116 "};"); 11117 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 11118 " [](const string &s) { return s; };"); 11119 verifyFormat("int i = aaaaaa ? 1 //\n" 11120 " : [] {\n" 11121 " return 2; //\n" 11122 " }();"); 11123 verifyFormat("llvm::errs() << \"number of twos is \"\n" 11124 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 11125 " return x == 2; // force break\n" 11126 " });"); 11127 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11128 " [=](int iiiiiiiiiiii) {\n" 11129 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 11130 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 11131 " });", 11132 getLLVMStyleWithColumns(60)); 11133 verifyFormat("SomeFunction({[&] {\n" 11134 " // comment\n" 11135 " },\n" 11136 " [&] {\n" 11137 " // comment\n" 11138 " }});"); 11139 verifyFormat("SomeFunction({[&] {\n" 11140 " // comment\n" 11141 "}});"); 11142 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 11143 " [&]() { return true; },\n" 11144 " aaaaa aaaaaaaaa);"); 11145 11146 // Lambdas with return types. 11147 verifyFormat("int c = []() -> int { return 2; }();\n"); 11148 verifyFormat("int c = []() -> int * { return 2; }();\n"); 11149 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 11150 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 11151 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 11152 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 11153 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 11154 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 11155 verifyFormat("[a, a]() -> a<1> {};"); 11156 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 11157 " int j) -> int {\n" 11158 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 11159 "};"); 11160 verifyFormat( 11161 "aaaaaaaaaaaaaaaaaaaaaa(\n" 11162 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 11163 " return aaaaaaaaaaaaaaaaa;\n" 11164 " });", 11165 getLLVMStyleWithColumns(70)); 11166 verifyFormat("[]() //\n" 11167 " -> int {\n" 11168 " return 1; //\n" 11169 "};"); 11170 11171 // Multiple lambdas in the same parentheses change indentation rules. 11172 verifyFormat("SomeFunction(\n" 11173 " []() {\n" 11174 " int i = 42;\n" 11175 " return i;\n" 11176 " },\n" 11177 " []() {\n" 11178 " int j = 43;\n" 11179 " return j;\n" 11180 " });"); 11181 11182 // More complex introducers. 11183 verifyFormat("return [i, args...] {};"); 11184 11185 // Not lambdas. 11186 verifyFormat("constexpr char hello[]{\"hello\"};"); 11187 verifyFormat("double &operator[](int i) { return 0; }\n" 11188 "int i;"); 11189 verifyFormat("std::unique_ptr<int[]> foo() {}"); 11190 verifyFormat("int i = a[a][a]->f();"); 11191 verifyFormat("int i = (*b)[a]->f();"); 11192 11193 // Other corner cases. 11194 verifyFormat("void f() {\n" 11195 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 11196 " );\n" 11197 "}"); 11198 11199 // Lambdas created through weird macros. 11200 verifyFormat("void f() {\n" 11201 " MACRO((const AA &a) { return 1; });\n" 11202 " MACRO((AA &a) { return 1; });\n" 11203 "}"); 11204 11205 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 11206 " doo_dah();\n" 11207 " doo_dah();\n" 11208 " })) {\n" 11209 "}"); 11210 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 11211 " doo_dah();\n" 11212 " doo_dah();\n" 11213 " })) {\n" 11214 "}"); 11215 verifyFormat("auto lambda = []() {\n" 11216 " int a = 2\n" 11217 "#if A\n" 11218 " + 2\n" 11219 "#endif\n" 11220 " ;\n" 11221 "};"); 11222 11223 // Lambdas with complex multiline introducers. 11224 verifyFormat( 11225 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 11226 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 11227 " -> ::std::unordered_set<\n" 11228 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 11229 " //\n" 11230 " });"); 11231 } 11232 11233 TEST_F(FormatTest, EmptyLinesInLambdas) { 11234 verifyFormat("auto lambda = []() {\n" 11235 " x(); //\n" 11236 "};", 11237 "auto lambda = []() {\n" 11238 "\n" 11239 " x(); //\n" 11240 "\n" 11241 "};"); 11242 } 11243 11244 TEST_F(FormatTest, FormatsBlocks) { 11245 FormatStyle ShortBlocks = getLLVMStyle(); 11246 ShortBlocks.AllowShortBlocksOnASingleLine = true; 11247 verifyFormat("int (^Block)(int, int);", ShortBlocks); 11248 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 11249 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 11250 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 11251 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 11252 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 11253 11254 verifyFormat("foo(^{ bar(); });", ShortBlocks); 11255 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 11256 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 11257 11258 verifyFormat("[operation setCompletionBlock:^{\n" 11259 " [self onOperationDone];\n" 11260 "}];"); 11261 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 11262 " [self onOperationDone];\n" 11263 "}]};"); 11264 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 11265 " f();\n" 11266 "}];"); 11267 verifyFormat("int a = [operation block:^int(int *i) {\n" 11268 " return 1;\n" 11269 "}];"); 11270 verifyFormat("[myObject doSomethingWith:arg1\n" 11271 " aaa:^int(int *a) {\n" 11272 " return 1;\n" 11273 " }\n" 11274 " bbb:f(a * bbbbbbbb)];"); 11275 11276 verifyFormat("[operation setCompletionBlock:^{\n" 11277 " [self.delegate newDataAvailable];\n" 11278 "}];", 11279 getLLVMStyleWithColumns(60)); 11280 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 11281 " NSString *path = [self sessionFilePath];\n" 11282 " if (path) {\n" 11283 " // ...\n" 11284 " }\n" 11285 "});"); 11286 verifyFormat("[[SessionService sharedService]\n" 11287 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11288 " if (window) {\n" 11289 " [self windowDidLoad:window];\n" 11290 " } else {\n" 11291 " [self errorLoadingWindow];\n" 11292 " }\n" 11293 " }];"); 11294 verifyFormat("void (^largeBlock)(void) = ^{\n" 11295 " // ...\n" 11296 "};\n", 11297 getLLVMStyleWithColumns(40)); 11298 verifyFormat("[[SessionService sharedService]\n" 11299 " loadWindowWithCompletionBlock: //\n" 11300 " ^(SessionWindow *window) {\n" 11301 " if (window) {\n" 11302 " [self windowDidLoad:window];\n" 11303 " } else {\n" 11304 " [self errorLoadingWindow];\n" 11305 " }\n" 11306 " }];", 11307 getLLVMStyleWithColumns(60)); 11308 verifyFormat("[myObject doSomethingWith:arg1\n" 11309 " firstBlock:^(Foo *a) {\n" 11310 " // ...\n" 11311 " int i;\n" 11312 " }\n" 11313 " secondBlock:^(Bar *b) {\n" 11314 " // ...\n" 11315 " int i;\n" 11316 " }\n" 11317 " thirdBlock:^Foo(Bar *b) {\n" 11318 " // ...\n" 11319 " int i;\n" 11320 " }];"); 11321 verifyFormat("[myObject doSomethingWith:arg1\n" 11322 " firstBlock:-1\n" 11323 " secondBlock:^(Bar *b) {\n" 11324 " // ...\n" 11325 " int i;\n" 11326 " }];"); 11327 11328 verifyFormat("f(^{\n" 11329 " @autoreleasepool {\n" 11330 " if (a) {\n" 11331 " g();\n" 11332 " }\n" 11333 " }\n" 11334 "});"); 11335 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 11336 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 11337 "};"); 11338 11339 FormatStyle FourIndent = getLLVMStyle(); 11340 FourIndent.ObjCBlockIndentWidth = 4; 11341 verifyFormat("[operation setCompletionBlock:^{\n" 11342 " [self onOperationDone];\n" 11343 "}];", 11344 FourIndent); 11345 } 11346 11347 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 11348 FormatStyle ZeroColumn = getLLVMStyle(); 11349 ZeroColumn.ColumnLimit = 0; 11350 11351 verifyFormat("[[SessionService sharedService] " 11352 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11353 " if (window) {\n" 11354 " [self windowDidLoad:window];\n" 11355 " } else {\n" 11356 " [self errorLoadingWindow];\n" 11357 " }\n" 11358 "}];", 11359 ZeroColumn); 11360 EXPECT_EQ("[[SessionService sharedService]\n" 11361 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11362 " if (window) {\n" 11363 " [self windowDidLoad:window];\n" 11364 " } else {\n" 11365 " [self errorLoadingWindow];\n" 11366 " }\n" 11367 " }];", 11368 format("[[SessionService sharedService]\n" 11369 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 11370 " if (window) {\n" 11371 " [self windowDidLoad:window];\n" 11372 " } else {\n" 11373 " [self errorLoadingWindow];\n" 11374 " }\n" 11375 "}];", 11376 ZeroColumn)); 11377 verifyFormat("[myObject doSomethingWith:arg1\n" 11378 " firstBlock:^(Foo *a) {\n" 11379 " // ...\n" 11380 " int i;\n" 11381 " }\n" 11382 " secondBlock:^(Bar *b) {\n" 11383 " // ...\n" 11384 " int i;\n" 11385 " }\n" 11386 " thirdBlock:^Foo(Bar *b) {\n" 11387 " // ...\n" 11388 " int i;\n" 11389 " }];", 11390 ZeroColumn); 11391 verifyFormat("f(^{\n" 11392 " @autoreleasepool {\n" 11393 " if (a) {\n" 11394 " g();\n" 11395 " }\n" 11396 " }\n" 11397 "});", 11398 ZeroColumn); 11399 verifyFormat("void (^largeBlock)(void) = ^{\n" 11400 " // ...\n" 11401 "};", 11402 ZeroColumn); 11403 11404 ZeroColumn.AllowShortBlocksOnASingleLine = true; 11405 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 11406 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11407 ZeroColumn.AllowShortBlocksOnASingleLine = false; 11408 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 11409 " int i;\n" 11410 "};", 11411 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 11412 } 11413 11414 TEST_F(FormatTest, SupportsCRLF) { 11415 EXPECT_EQ("int a;\r\n" 11416 "int b;\r\n" 11417 "int c;\r\n", 11418 format("int a;\r\n" 11419 " int b;\r\n" 11420 " int c;\r\n", 11421 getLLVMStyle())); 11422 EXPECT_EQ("int a;\r\n" 11423 "int b;\r\n" 11424 "int c;\r\n", 11425 format("int a;\r\n" 11426 " int b;\n" 11427 " int c;\r\n", 11428 getLLVMStyle())); 11429 EXPECT_EQ("int a;\n" 11430 "int b;\n" 11431 "int c;\n", 11432 format("int a;\r\n" 11433 " int b;\n" 11434 " int c;\n", 11435 getLLVMStyle())); 11436 EXPECT_EQ("\"aaaaaaa \"\r\n" 11437 "\"bbbbbbb\";\r\n", 11438 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 11439 EXPECT_EQ("#define A \\\r\n" 11440 " b; \\\r\n" 11441 " c; \\\r\n" 11442 " d;\r\n", 11443 format("#define A \\\r\n" 11444 " b; \\\r\n" 11445 " c; d; \r\n", 11446 getGoogleStyle())); 11447 11448 EXPECT_EQ("/*\r\n" 11449 "multi line block comments\r\n" 11450 "should not introduce\r\n" 11451 "an extra carriage return\r\n" 11452 "*/\r\n", 11453 format("/*\r\n" 11454 "multi line block comments\r\n" 11455 "should not introduce\r\n" 11456 "an extra carriage return\r\n" 11457 "*/\r\n")); 11458 } 11459 11460 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 11461 verifyFormat("MY_CLASS(C) {\n" 11462 " int i;\n" 11463 " int j;\n" 11464 "};"); 11465 } 11466 11467 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 11468 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 11469 TwoIndent.ContinuationIndentWidth = 2; 11470 11471 EXPECT_EQ("int i =\n" 11472 " longFunction(\n" 11473 " arg);", 11474 format("int i = longFunction(arg);", TwoIndent)); 11475 11476 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 11477 SixIndent.ContinuationIndentWidth = 6; 11478 11479 EXPECT_EQ("int i =\n" 11480 " longFunction(\n" 11481 " arg);", 11482 format("int i = longFunction(arg);", SixIndent)); 11483 } 11484 11485 TEST_F(FormatTest, SpacesInAngles) { 11486 FormatStyle Spaces = getLLVMStyle(); 11487 Spaces.SpacesInAngles = true; 11488 11489 verifyFormat("static_cast< int >(arg);", Spaces); 11490 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 11491 verifyFormat("f< int, float >();", Spaces); 11492 verifyFormat("template <> g() {}", Spaces); 11493 verifyFormat("template < std::vector< int > > f() {}", Spaces); 11494 verifyFormat("std::function< void(int, int) > fct;", Spaces); 11495 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 11496 Spaces); 11497 11498 Spaces.Standard = FormatStyle::LS_Cpp03; 11499 Spaces.SpacesInAngles = true; 11500 verifyFormat("A< A< int > >();", Spaces); 11501 11502 Spaces.SpacesInAngles = false; 11503 verifyFormat("A<A<int> >();", Spaces); 11504 11505 Spaces.Standard = FormatStyle::LS_Cpp11; 11506 Spaces.SpacesInAngles = true; 11507 verifyFormat("A< A< int > >();", Spaces); 11508 11509 Spaces.SpacesInAngles = false; 11510 verifyFormat("A<A<int>>();", Spaces); 11511 } 11512 11513 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 11514 FormatStyle Style = getLLVMStyle(); 11515 Style.SpaceAfterTemplateKeyword = false; 11516 verifyFormat("template<int> void foo();", Style); 11517 } 11518 11519 TEST_F(FormatTest, TripleAngleBrackets) { 11520 verifyFormat("f<<<1, 1>>>();"); 11521 verifyFormat("f<<<1, 1, 1, s>>>();"); 11522 verifyFormat("f<<<a, b, c, d>>>();"); 11523 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 11524 verifyFormat("f<param><<<1, 1>>>();"); 11525 verifyFormat("f<1><<<1, 1>>>();"); 11526 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 11527 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11528 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 11529 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 11530 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 11531 } 11532 11533 TEST_F(FormatTest, MergeLessLessAtEnd) { 11534 verifyFormat("<<"); 11535 EXPECT_EQ("< < <", format("\\\n<<<")); 11536 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11537 "aaallvm::outs() <<"); 11538 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11539 "aaaallvm::outs()\n <<"); 11540 } 11541 11542 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 11543 std::string code = "#if A\n" 11544 "#if B\n" 11545 "a.\n" 11546 "#endif\n" 11547 " a = 1;\n" 11548 "#else\n" 11549 "#endif\n" 11550 "#if C\n" 11551 "#else\n" 11552 "#endif\n"; 11553 EXPECT_EQ(code, format(code)); 11554 } 11555 11556 TEST_F(FormatTest, HandleConflictMarkers) { 11557 // Git/SVN conflict markers. 11558 EXPECT_EQ("int a;\n" 11559 "void f() {\n" 11560 " callme(some(parameter1,\n" 11561 "<<<<<<< text by the vcs\n" 11562 " parameter2),\n" 11563 "||||||| text by the vcs\n" 11564 " parameter2),\n" 11565 " parameter3,\n" 11566 "======= text by the vcs\n" 11567 " parameter2, parameter3),\n" 11568 ">>>>>>> text by the vcs\n" 11569 " otherparameter);\n", 11570 format("int a;\n" 11571 "void f() {\n" 11572 " callme(some(parameter1,\n" 11573 "<<<<<<< text by the vcs\n" 11574 " parameter2),\n" 11575 "||||||| text by the vcs\n" 11576 " parameter2),\n" 11577 " parameter3,\n" 11578 "======= text by the vcs\n" 11579 " parameter2,\n" 11580 " parameter3),\n" 11581 ">>>>>>> text by the vcs\n" 11582 " otherparameter);\n")); 11583 11584 // Perforce markers. 11585 EXPECT_EQ("void f() {\n" 11586 " function(\n" 11587 ">>>> text by the vcs\n" 11588 " parameter,\n" 11589 "==== text by the vcs\n" 11590 " parameter,\n" 11591 "==== text by the vcs\n" 11592 " parameter,\n" 11593 "<<<< text by the vcs\n" 11594 " parameter);\n", 11595 format("void f() {\n" 11596 " function(\n" 11597 ">>>> text by the vcs\n" 11598 " parameter,\n" 11599 "==== text by the vcs\n" 11600 " parameter,\n" 11601 "==== text by the vcs\n" 11602 " parameter,\n" 11603 "<<<< text by the vcs\n" 11604 " parameter);\n")); 11605 11606 EXPECT_EQ("<<<<<<<\n" 11607 "|||||||\n" 11608 "=======\n" 11609 ">>>>>>>", 11610 format("<<<<<<<\n" 11611 "|||||||\n" 11612 "=======\n" 11613 ">>>>>>>")); 11614 11615 EXPECT_EQ("<<<<<<<\n" 11616 "|||||||\n" 11617 "int i;\n" 11618 "=======\n" 11619 ">>>>>>>", 11620 format("<<<<<<<\n" 11621 "|||||||\n" 11622 "int i;\n" 11623 "=======\n" 11624 ">>>>>>>")); 11625 11626 // FIXME: Handle parsing of macros around conflict markers correctly: 11627 EXPECT_EQ("#define Macro \\\n" 11628 "<<<<<<<\n" 11629 "Something \\\n" 11630 "|||||||\n" 11631 "Else \\\n" 11632 "=======\n" 11633 "Other \\\n" 11634 ">>>>>>>\n" 11635 " End int i;\n", 11636 format("#define Macro \\\n" 11637 "<<<<<<<\n" 11638 " Something \\\n" 11639 "|||||||\n" 11640 " Else \\\n" 11641 "=======\n" 11642 " Other \\\n" 11643 ">>>>>>>\n" 11644 " End\n" 11645 "int i;\n")); 11646 } 11647 11648 TEST_F(FormatTest, DisableRegions) { 11649 EXPECT_EQ("int i;\n" 11650 "// clang-format off\n" 11651 " int j;\n" 11652 "// clang-format on\n" 11653 "int k;", 11654 format(" int i;\n" 11655 " // clang-format off\n" 11656 " int j;\n" 11657 " // clang-format on\n" 11658 " int k;")); 11659 EXPECT_EQ("int i;\n" 11660 "/* clang-format off */\n" 11661 " int j;\n" 11662 "/* clang-format on */\n" 11663 "int k;", 11664 format(" int i;\n" 11665 " /* clang-format off */\n" 11666 " int j;\n" 11667 " /* clang-format on */\n" 11668 " int k;")); 11669 11670 // Don't reflow comments within disabled regions. 11671 EXPECT_EQ( 11672 "// clang-format off\n" 11673 "// long long long long long long line\n" 11674 "/* clang-format on */\n" 11675 "/* long long long\n" 11676 " * long long long\n" 11677 " * line */\n" 11678 "int i;\n" 11679 "/* clang-format off */\n" 11680 "/* long long long long long long line */\n", 11681 format("// clang-format off\n" 11682 "// long long long long long long line\n" 11683 "/* clang-format on */\n" 11684 "/* long long long long long long line */\n" 11685 "int i;\n" 11686 "/* clang-format off */\n" 11687 "/* long long long long long long line */\n", 11688 getLLVMStyleWithColumns(20))); 11689 } 11690 11691 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11692 format("? ) ="); 11693 verifyNoCrash("#define a\\\n /**/}"); 11694 } 11695 11696 TEST_F(FormatTest, FormatsTableGenCode) { 11697 FormatStyle Style = getLLVMStyle(); 11698 Style.Language = FormatStyle::LK_TableGen; 11699 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11700 } 11701 11702 TEST_F(FormatTest, ArrayOfTemplates) { 11703 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 11704 format("auto a = new unique_ptr<int > [ 10];")); 11705 11706 FormatStyle Spaces = getLLVMStyle(); 11707 Spaces.SpacesInSquareBrackets = true; 11708 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 11709 format("auto a = new unique_ptr<int > [10];", Spaces)); 11710 } 11711 11712 TEST_F(FormatTest, ArrayAsTemplateType) { 11713 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 11714 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 11715 11716 FormatStyle Spaces = getLLVMStyle(); 11717 Spaces.SpacesInSquareBrackets = true; 11718 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 11719 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 11720 } 11721 11722 TEST_F(FormatTest, NoSpaceAfterSuper) { 11723 verifyFormat("__super::FooBar();"); 11724 } 11725 11726 TEST(FormatStyle, GetStyleOfFile) { 11727 vfs::InMemoryFileSystem FS; 11728 // Test 1: format file in the same directory. 11729 ASSERT_TRUE( 11730 FS.addFile("/a/.clang-format", 0, 11731 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 11732 ASSERT_TRUE( 11733 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11734 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 11735 ASSERT_TRUE((bool)Style1); 11736 ASSERT_EQ(*Style1, getLLVMStyle()); 11737 11738 // Test 2.1: fallback to default. 11739 ASSERT_TRUE( 11740 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11741 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 11742 ASSERT_TRUE((bool)Style2); 11743 ASSERT_EQ(*Style2, getMozillaStyle()); 11744 11745 // Test 2.2: no format on 'none' fallback style. 11746 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11747 ASSERT_TRUE((bool)Style2); 11748 ASSERT_EQ(*Style2, getNoStyle()); 11749 11750 // Test 2.3: format if config is found with no based style while fallback is 11751 // 'none'. 11752 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 11753 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 11754 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11755 ASSERT_TRUE((bool)Style2); 11756 ASSERT_EQ(*Style2, getLLVMStyle()); 11757 11758 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 11759 Style2 = getStyle("{}", "a.h", "none", "", &FS); 11760 ASSERT_TRUE((bool)Style2); 11761 ASSERT_EQ(*Style2, getLLVMStyle()); 11762 11763 // Test 3: format file in parent directory. 11764 ASSERT_TRUE( 11765 FS.addFile("/c/.clang-format", 0, 11766 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 11767 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 11768 llvm::MemoryBuffer::getMemBuffer("int i;"))); 11769 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 11770 ASSERT_TRUE((bool)Style3); 11771 ASSERT_EQ(*Style3, getGoogleStyle()); 11772 11773 // Test 4: error on invalid fallback style 11774 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 11775 ASSERT_FALSE((bool)Style4); 11776 llvm::consumeError(Style4.takeError()); 11777 11778 // Test 5: error on invalid yaml on command line 11779 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 11780 ASSERT_FALSE((bool)Style5); 11781 llvm::consumeError(Style5.takeError()); 11782 11783 // Test 6: error on invalid style 11784 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 11785 ASSERT_FALSE((bool)Style6); 11786 llvm::consumeError(Style6.takeError()); 11787 11788 // Test 7: found config file, error on parsing it 11789 ASSERT_TRUE( 11790 FS.addFile("/d/.clang-format", 0, 11791 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 11792 "InvalidKey: InvalidValue"))); 11793 ASSERT_TRUE( 11794 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11795 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 11796 ASSERT_FALSE((bool)Style7); 11797 llvm::consumeError(Style7.takeError()); 11798 } 11799 11800 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11801 // Column limit is 20. 11802 std::string Code = "Type *a =\n" 11803 " new Type();\n" 11804 "g(iiiii, 0, jjjjj,\n" 11805 " 0, kkkkk, 0, mm);\n" 11806 "int bad = format ;"; 11807 std::string Expected = "auto a = new Type();\n" 11808 "g(iiiii, nullptr,\n" 11809 " jjjjj, nullptr,\n" 11810 " kkkkk, nullptr,\n" 11811 " mm);\n" 11812 "int bad = format ;"; 11813 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11814 tooling::Replacements Replaces = toReplacements( 11815 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 11816 "auto "), 11817 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 11818 "nullptr"), 11819 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 11820 "nullptr"), 11821 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 11822 "nullptr")}); 11823 11824 format::FormatStyle Style = format::getLLVMStyle(); 11825 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11826 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11827 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11828 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11829 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11830 EXPECT_TRUE(static_cast<bool>(Result)); 11831 EXPECT_EQ(Expected, *Result); 11832 } 11833 11834 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11835 std::string Code = "#include \"a.h\"\n" 11836 "#include \"c.h\"\n" 11837 "\n" 11838 "int main() {\n" 11839 " return 0;\n" 11840 "}"; 11841 std::string Expected = "#include \"a.h\"\n" 11842 "#include \"b.h\"\n" 11843 "#include \"c.h\"\n" 11844 "\n" 11845 "int main() {\n" 11846 " return 0;\n" 11847 "}"; 11848 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 11849 tooling::Replacements Replaces = toReplacements( 11850 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 11851 "#include \"b.h\"\n")}); 11852 11853 format::FormatStyle Style = format::getLLVMStyle(); 11854 Style.SortIncludes = true; 11855 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11856 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11857 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11858 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11859 EXPECT_TRUE(static_cast<bool>(Result)); 11860 EXPECT_EQ(Expected, *Result); 11861 } 11862 11863 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 11864 EXPECT_EQ("using std::cin;\n" 11865 "using std::cout;", 11866 format("using std::cout;\n" 11867 "using std::cin;", getGoogleStyle())); 11868 } 11869 11870 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 11871 format::FormatStyle Style = format::getLLVMStyle(); 11872 Style.Standard = FormatStyle::LS_Cpp03; 11873 // cpp03 recognize this string as identifier u8 and literal character 'a' 11874 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 11875 } 11876 11877 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 11878 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 11879 // all modes, including C++11, C++14 and C++17 11880 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 11881 } 11882 11883 TEST_F(FormatTest, DoNotFormatLikelyXml) { 11884 EXPECT_EQ("<!-- ;> -->", 11885 format("<!-- ;> -->", getGoogleStyle())); 11886 EXPECT_EQ(" <!-- >; -->", 11887 format(" <!-- >; -->", getGoogleStyle())); 11888 } 11889 11890 TEST_F(FormatTest, StructuredBindings) { 11891 // Structured bindings is a C++17 feature. 11892 // all modes, including C++11, C++14 and C++17 11893 verifyFormat("auto [a, b] = f();"); 11894 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 11895 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 11896 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 11897 EXPECT_EQ("auto const volatile [a, b] = f();", 11898 format("auto const volatile[a, b] = f();")); 11899 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 11900 EXPECT_EQ("auto &[a, b, c] = f();", 11901 format("auto &[ a , b,c ] = f();")); 11902 EXPECT_EQ("auto &&[a, b, c] = f();", 11903 format("auto &&[ a , b,c ] = f();")); 11904 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 11905 EXPECT_EQ("auto const volatile &&[a, b] = f();", 11906 format("auto const volatile &&[a, b] = f();")); 11907 EXPECT_EQ("auto const &&[a, b] = f();", format("auto const && [a, b] = f();")); 11908 EXPECT_EQ("const auto &[a, b] = f();", format("const auto & [a, b] = f();")); 11909 EXPECT_EQ("const auto volatile &&[a, b] = f();", 11910 format("const auto volatile &&[a, b] = f();")); 11911 EXPECT_EQ("volatile const auto &&[a, b] = f();", 11912 format("volatile const auto &&[a, b] = f();")); 11913 EXPECT_EQ("const auto &&[a, b] = f();", format("const auto && [a, b] = f();")); 11914 11915 // Make sure we don't mistake structured bindings for lambdas. 11916 FormatStyle PointerMiddle = getLLVMStyle(); 11917 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 11918 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 11919 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 11920 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 11921 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 11922 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 11923 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 11924 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 11925 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 11926 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 11927 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 11928 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 11929 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 11930 11931 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 11932 format("for (const auto && [a, b] : some_range) {\n}")); 11933 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 11934 format("for (const auto & [a, b] : some_range) {\n}")); 11935 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 11936 format("for (const auto[a, b] : some_range) {\n}")); 11937 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 11938 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 11939 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 11940 EXPECT_EQ("auto const &[x, y](expr);", format("auto const & [x,y] (expr);")); 11941 EXPECT_EQ("auto const &&[x, y](expr);", format("auto const && [x,y] (expr);")); 11942 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 11943 EXPECT_EQ("auto const &[x, y]{expr};", format("auto const & [x,y] {expr};")); 11944 EXPECT_EQ("auto const &&[x, y]{expr};", format("auto const && [x,y] {expr};")); 11945 11946 format::FormatStyle Spaces = format::getLLVMStyle(); 11947 Spaces.SpacesInSquareBrackets = true; 11948 verifyFormat("auto [ a, b ] = f();", Spaces); 11949 verifyFormat("auto &&[ a, b ] = f();", Spaces); 11950 verifyFormat("auto &[ a, b ] = f();", Spaces); 11951 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 11952 verifyFormat("auto const &[ a, b ] = f();", Spaces); 11953 } 11954 11955 } // end namespace 11956 } // end namespace format 11957 } // end namespace clang 11958