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 Code, 74 const FormatStyle &Style = getLLVMStyle()) { 75 EXPECT_EQ(Code.str(), format(test::messUp(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(Code.str(), format(test::messUp(Code), ObjCStyle)); 82 } 83 } 84 85 void verifyIncompleteFormat(llvm::StringRef Code, 86 const FormatStyle &Style = getLLVMStyle()) { 87 EXPECT_EQ(Code.str(), 88 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 89 } 90 91 void verifyGoogleFormat(llvm::StringRef Code) { 92 verifyFormat(Code, getGoogleStyle()); 93 } 94 95 void verifyIndependentOfContext(llvm::StringRef text) { 96 verifyFormat(text); 97 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 98 } 99 100 /// \brief Verify that clang-format does not crash on the given input. 101 void verifyNoCrash(llvm::StringRef Code, 102 const FormatStyle &Style = getLLVMStyle()) { 103 format(Code, Style, SC_DoNotCheck); 104 } 105 106 int ReplacementCount; 107 }; 108 109 TEST_F(FormatTest, MessUp) { 110 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 111 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 112 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 113 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 114 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 115 } 116 117 //===----------------------------------------------------------------------===// 118 // Basic function tests. 119 //===----------------------------------------------------------------------===// 120 121 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 122 EXPECT_EQ(";", format(";")); 123 } 124 125 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 126 EXPECT_EQ("int i;", format(" int i;")); 127 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 128 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 129 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 130 } 131 132 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 133 EXPECT_EQ("int i;", format("int\ni;")); 134 } 135 136 TEST_F(FormatTest, FormatsNestedBlockStatements) { 137 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 138 } 139 140 TEST_F(FormatTest, FormatsNestedCall) { 141 verifyFormat("Method(f1, f2(f3));"); 142 verifyFormat("Method(f1(f2, f3()));"); 143 verifyFormat("Method(f1(f2, (f3())));"); 144 } 145 146 TEST_F(FormatTest, NestedNameSpecifiers) { 147 verifyFormat("vector<::Type> v;"); 148 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 149 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 150 verifyFormat("bool a = 2 < ::SomeFunction();"); 151 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 152 verifyFormat("some::string getName();"); 153 } 154 155 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 156 EXPECT_EQ("if (a) {\n" 157 " f();\n" 158 "}", 159 format("if(a){f();}")); 160 EXPECT_EQ(4, ReplacementCount); 161 EXPECT_EQ("if (a) {\n" 162 " f();\n" 163 "}", 164 format("if (a) {\n" 165 " f();\n" 166 "}")); 167 EXPECT_EQ(0, ReplacementCount); 168 EXPECT_EQ("/*\r\n" 169 "\r\n" 170 "*/\r\n", 171 format("/*\r\n" 172 "\r\n" 173 "*/\r\n")); 174 EXPECT_EQ(0, ReplacementCount); 175 } 176 177 TEST_F(FormatTest, RemovesEmptyLines) { 178 EXPECT_EQ("class C {\n" 179 " int i;\n" 180 "};", 181 format("class C {\n" 182 " int i;\n" 183 "\n" 184 "};")); 185 186 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 187 EXPECT_EQ("namespace N {\n" 188 "\n" 189 "int i;\n" 190 "}", 191 format("namespace N {\n" 192 "\n" 193 "int i;\n" 194 "}", 195 getGoogleStyle())); 196 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 197 "\n" 198 "int i;\n" 199 "}", 200 format("extern /**/ \"C\" /**/ {\n" 201 "\n" 202 "int i;\n" 203 "}", 204 getGoogleStyle())); 205 206 // ...but do keep inlining and removing empty lines for non-block extern "C" 207 // functions. 208 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 209 EXPECT_EQ("extern \"C\" int f() {\n" 210 " int i = 42;\n" 211 " return i;\n" 212 "}", 213 format("extern \"C\" int f() {\n" 214 "\n" 215 " int i = 42;\n" 216 " return i;\n" 217 "}", 218 getGoogleStyle())); 219 220 // Remove empty lines at the beginning and end of blocks. 221 EXPECT_EQ("void f() {\n" 222 "\n" 223 " if (a) {\n" 224 "\n" 225 " f();\n" 226 " }\n" 227 "}", 228 format("void f() {\n" 229 "\n" 230 " if (a) {\n" 231 "\n" 232 " f();\n" 233 "\n" 234 " }\n" 235 "\n" 236 "}", 237 getLLVMStyle())); 238 EXPECT_EQ("void f() {\n" 239 " if (a) {\n" 240 " f();\n" 241 " }\n" 242 "}", 243 format("void f() {\n" 244 "\n" 245 " if (a) {\n" 246 "\n" 247 " f();\n" 248 "\n" 249 " }\n" 250 "\n" 251 "}", 252 getGoogleStyle())); 253 254 // Don't remove empty lines in more complex control statements. 255 EXPECT_EQ("void f() {\n" 256 " if (a) {\n" 257 " f();\n" 258 "\n" 259 " } else if (b) {\n" 260 " f();\n" 261 " }\n" 262 "}", 263 format("void f() {\n" 264 " if (a) {\n" 265 " f();\n" 266 "\n" 267 " } else if (b) {\n" 268 " f();\n" 269 "\n" 270 " }\n" 271 "\n" 272 "}")); 273 274 // FIXME: This is slightly inconsistent. 275 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 276 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 277 EXPECT_EQ("namespace {\n" 278 "int i;\n" 279 "}", 280 format("namespace {\n" 281 "int i;\n" 282 "\n" 283 "}", LLVMWithNoNamespaceFix)); 284 EXPECT_EQ("namespace {\n" 285 "int i;\n" 286 "}", 287 format("namespace {\n" 288 "int i;\n" 289 "\n" 290 "}")); 291 EXPECT_EQ("namespace {\n" 292 "int i;\n" 293 "\n" 294 "} // namespace", 295 format("namespace {\n" 296 "int i;\n" 297 "\n" 298 "} // namespace")); 299 300 FormatStyle Style = getLLVMStyle(); 301 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 302 Style.MaxEmptyLinesToKeep = 2; 303 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 304 Style.BraceWrapping.AfterClass = true; 305 Style.BraceWrapping.AfterFunction = true; 306 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 307 308 EXPECT_EQ("class Foo\n" 309 "{\n" 310 " Foo() {}\n" 311 "\n" 312 " void funk() {}\n" 313 "};", 314 format("class Foo\n" 315 "{\n" 316 " Foo()\n" 317 " {\n" 318 " }\n" 319 "\n" 320 " void funk() {}\n" 321 "};", 322 Style)); 323 } 324 325 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 326 verifyFormat("x = (a) and (b);"); 327 verifyFormat("x = (a) or (b);"); 328 verifyFormat("x = (a) bitand (b);"); 329 verifyFormat("x = (a) bitor (b);"); 330 verifyFormat("x = (a) not_eq (b);"); 331 verifyFormat("x = (a) and_eq (b);"); 332 verifyFormat("x = (a) or_eq (b);"); 333 verifyFormat("x = (a) xor (b);"); 334 } 335 336 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 337 verifyFormat("x = compl(a);"); 338 verifyFormat("x = not(a);"); 339 verifyFormat("x = bitand(a);"); 340 // Unary operator must not be merged with the next identifier 341 verifyFormat("x = compl a;"); 342 verifyFormat("x = not a;"); 343 verifyFormat("x = bitand a;"); 344 } 345 346 //===----------------------------------------------------------------------===// 347 // Tests for control statements. 348 //===----------------------------------------------------------------------===// 349 350 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 351 verifyFormat("if (true)\n f();\ng();"); 352 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 353 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 354 verifyFormat("if constexpr (true)\n" 355 " f();\ng();"); 356 verifyFormat("if constexpr (a)\n" 357 " if constexpr (b)\n" 358 " if constexpr (c)\n" 359 " g();\n" 360 "h();"); 361 verifyFormat("if constexpr (a)\n" 362 " if constexpr (b) {\n" 363 " f();\n" 364 " }\n" 365 "g();"); 366 367 FormatStyle AllowsMergedIf = getLLVMStyle(); 368 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 369 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 370 verifyFormat("if (a)\n" 371 " // comment\n" 372 " f();", 373 AllowsMergedIf); 374 verifyFormat("{\n" 375 " if (a)\n" 376 " label:\n" 377 " f();\n" 378 "}", 379 AllowsMergedIf); 380 verifyFormat("#define A \\\n" 381 " if (a) \\\n" 382 " label: \\\n" 383 " f()", 384 AllowsMergedIf); 385 verifyFormat("if (a)\n" 386 " ;", 387 AllowsMergedIf); 388 verifyFormat("if (a)\n" 389 " if (b) return;", 390 AllowsMergedIf); 391 392 verifyFormat("if (a) // Can't merge this\n" 393 " f();\n", 394 AllowsMergedIf); 395 verifyFormat("if (a) /* still don't merge */\n" 396 " f();", 397 AllowsMergedIf); 398 verifyFormat("if (a) { // Never merge this\n" 399 " f();\n" 400 "}", 401 AllowsMergedIf); 402 verifyFormat("if (a) { /* Never merge this */\n" 403 " f();\n" 404 "}", 405 AllowsMergedIf); 406 407 AllowsMergedIf.ColumnLimit = 14; 408 verifyFormat("if (a) return;", AllowsMergedIf); 409 verifyFormat("if (aaaaaaaaa)\n" 410 " return;", 411 AllowsMergedIf); 412 413 AllowsMergedIf.ColumnLimit = 13; 414 verifyFormat("if (a)\n return;", AllowsMergedIf); 415 } 416 417 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 418 FormatStyle AllowsMergedLoops = getLLVMStyle(); 419 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 420 verifyFormat("while (true) continue;", AllowsMergedLoops); 421 verifyFormat("for (;;) continue;", AllowsMergedLoops); 422 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 423 verifyFormat("while (true)\n" 424 " ;", 425 AllowsMergedLoops); 426 verifyFormat("for (;;)\n" 427 " ;", 428 AllowsMergedLoops); 429 verifyFormat("for (;;)\n" 430 " for (;;) continue;", 431 AllowsMergedLoops); 432 verifyFormat("for (;;) // Can't merge this\n" 433 " continue;", 434 AllowsMergedLoops); 435 verifyFormat("for (;;) /* still don't merge */\n" 436 " continue;", 437 AllowsMergedLoops); 438 } 439 440 TEST_F(FormatTest, FormatShortBracedStatements) { 441 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 442 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 443 444 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 445 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 446 447 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 448 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 449 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 450 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 451 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 452 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 453 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 454 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 455 verifyFormat("if (true) { //\n" 456 " f();\n" 457 "}", 458 AllowSimpleBracedStatements); 459 verifyFormat("if (true) {\n" 460 " f();\n" 461 " f();\n" 462 "}", 463 AllowSimpleBracedStatements); 464 verifyFormat("if (true) {\n" 465 " f();\n" 466 "} else {\n" 467 " f();\n" 468 "}", 469 AllowSimpleBracedStatements); 470 471 verifyFormat("struct A2 {\n" 472 " int X;\n" 473 "};", 474 AllowSimpleBracedStatements); 475 verifyFormat("typedef struct A2 {\n" 476 " int X;\n" 477 "} A2_t;", 478 AllowSimpleBracedStatements); 479 verifyFormat("template <int> struct A2 {\n" 480 " struct B {};\n" 481 "};", 482 AllowSimpleBracedStatements); 483 484 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 485 verifyFormat("if (true) {\n" 486 " f();\n" 487 "}", 488 AllowSimpleBracedStatements); 489 verifyFormat("if (true) {\n" 490 " f();\n" 491 "} else {\n" 492 " f();\n" 493 "}", 494 AllowSimpleBracedStatements); 495 496 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 497 verifyFormat("while (true) {\n" 498 " f();\n" 499 "}", 500 AllowSimpleBracedStatements); 501 verifyFormat("for (;;) {\n" 502 " f();\n" 503 "}", 504 AllowSimpleBracedStatements); 505 } 506 507 TEST_F(FormatTest, ParseIfElse) { 508 verifyFormat("if (true)\n" 509 " if (true)\n" 510 " if (true)\n" 511 " f();\n" 512 " else\n" 513 " g();\n" 514 " else\n" 515 " h();\n" 516 "else\n" 517 " i();"); 518 verifyFormat("if (true)\n" 519 " if (true)\n" 520 " if (true) {\n" 521 " if (true)\n" 522 " f();\n" 523 " } else {\n" 524 " g();\n" 525 " }\n" 526 " else\n" 527 " h();\n" 528 "else {\n" 529 " i();\n" 530 "}"); 531 verifyFormat("if (true)\n" 532 " if constexpr (true)\n" 533 " if (true) {\n" 534 " if constexpr (true)\n" 535 " f();\n" 536 " } else {\n" 537 " g();\n" 538 " }\n" 539 " else\n" 540 " h();\n" 541 "else {\n" 542 " i();\n" 543 "}"); 544 verifyFormat("void f() {\n" 545 " if (a) {\n" 546 " } else {\n" 547 " }\n" 548 "}"); 549 } 550 551 TEST_F(FormatTest, ElseIf) { 552 verifyFormat("if (a) {\n} else if (b) {\n}"); 553 verifyFormat("if (a)\n" 554 " f();\n" 555 "else if (b)\n" 556 " g();\n" 557 "else\n" 558 " h();"); 559 verifyFormat("if constexpr (a)\n" 560 " f();\n" 561 "else if constexpr (b)\n" 562 " g();\n" 563 "else\n" 564 " h();"); 565 verifyFormat("if (a) {\n" 566 " f();\n" 567 "}\n" 568 "// or else ..\n" 569 "else {\n" 570 " g()\n" 571 "}"); 572 573 verifyFormat("if (a) {\n" 574 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 575 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 576 "}"); 577 verifyFormat("if (a) {\n" 578 "} else if (\n" 579 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 580 "}", 581 getLLVMStyleWithColumns(62)); 582 verifyFormat("if (a) {\n" 583 "} else if constexpr (\n" 584 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 585 "}", 586 getLLVMStyleWithColumns(62)); 587 } 588 589 TEST_F(FormatTest, FormatsForLoop) { 590 verifyFormat( 591 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 592 " ++VeryVeryLongLoopVariable)\n" 593 " ;"); 594 verifyFormat("for (;;)\n" 595 " f();"); 596 verifyFormat("for (;;) {\n}"); 597 verifyFormat("for (;;) {\n" 598 " f();\n" 599 "}"); 600 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 601 602 verifyFormat( 603 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 604 " E = UnwrappedLines.end();\n" 605 " I != E; ++I) {\n}"); 606 607 verifyFormat( 608 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 609 " ++IIIII) {\n}"); 610 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 611 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 612 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 613 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 614 " I = FD->getDeclsInPrototypeScope().begin(),\n" 615 " E = FD->getDeclsInPrototypeScope().end();\n" 616 " I != E; ++I) {\n}"); 617 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 618 " I = Container.begin(),\n" 619 " E = Container.end();\n" 620 " I != E; ++I) {\n}", 621 getLLVMStyleWithColumns(76)); 622 623 verifyFormat( 624 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 625 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 626 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 627 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 628 " ++aaaaaaaaaaa) {\n}"); 629 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 630 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 631 " ++i) {\n}"); 632 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 633 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 634 "}"); 635 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 636 " aaaaaaaaaa);\n" 637 " iter; ++iter) {\n" 638 "}"); 639 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 640 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 641 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 642 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 643 644 FormatStyle NoBinPacking = getLLVMStyle(); 645 NoBinPacking.BinPackParameters = false; 646 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 647 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 648 " aaaaaaaaaaaaaaaa,\n" 649 " aaaaaaaaaaaaaaaa,\n" 650 " aaaaaaaaaaaaaaaa);\n" 651 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 652 "}", 653 NoBinPacking); 654 verifyFormat( 655 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 656 " E = UnwrappedLines.end();\n" 657 " I != E;\n" 658 " ++I) {\n}", 659 NoBinPacking); 660 } 661 662 TEST_F(FormatTest, RangeBasedForLoops) { 663 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 664 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 665 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 666 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 667 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 668 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 669 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 670 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 671 } 672 673 TEST_F(FormatTest, ForEachLoops) { 674 verifyFormat("void f() {\n" 675 " foreach (Item *item, itemlist) {}\n" 676 " Q_FOREACH (Item *item, itemlist) {}\n" 677 " BOOST_FOREACH (Item *item, itemlist) {}\n" 678 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 679 "}"); 680 681 // As function-like macros. 682 verifyFormat("#define foreach(x, y)\n" 683 "#define Q_FOREACH(x, y)\n" 684 "#define BOOST_FOREACH(x, y)\n" 685 "#define UNKNOWN_FOREACH(x, y)\n"); 686 687 // Not as function-like macros. 688 verifyFormat("#define foreach (x, y)\n" 689 "#define Q_FOREACH (x, y)\n" 690 "#define BOOST_FOREACH (x, y)\n" 691 "#define UNKNOWN_FOREACH (x, y)\n"); 692 } 693 694 TEST_F(FormatTest, FormatsWhileLoop) { 695 verifyFormat("while (true) {\n}"); 696 verifyFormat("while (true)\n" 697 " f();"); 698 verifyFormat("while () {\n}"); 699 verifyFormat("while () {\n" 700 " f();\n" 701 "}"); 702 } 703 704 TEST_F(FormatTest, FormatsDoWhile) { 705 verifyFormat("do {\n" 706 " do_something();\n" 707 "} while (something());"); 708 verifyFormat("do\n" 709 " do_something();\n" 710 "while (something());"); 711 } 712 713 TEST_F(FormatTest, FormatsSwitchStatement) { 714 verifyFormat("switch (x) {\n" 715 "case 1:\n" 716 " f();\n" 717 " break;\n" 718 "case kFoo:\n" 719 "case ns::kBar:\n" 720 "case kBaz:\n" 721 " break;\n" 722 "default:\n" 723 " g();\n" 724 " break;\n" 725 "}"); 726 verifyFormat("switch (x) {\n" 727 "case 1: {\n" 728 " f();\n" 729 " break;\n" 730 "}\n" 731 "case 2: {\n" 732 " break;\n" 733 "}\n" 734 "}"); 735 verifyFormat("switch (x) {\n" 736 "case 1: {\n" 737 " f();\n" 738 " {\n" 739 " g();\n" 740 " h();\n" 741 " }\n" 742 " break;\n" 743 "}\n" 744 "}"); 745 verifyFormat("switch (x) {\n" 746 "case 1: {\n" 747 " f();\n" 748 " if (foo) {\n" 749 " g();\n" 750 " h();\n" 751 " }\n" 752 " break;\n" 753 "}\n" 754 "}"); 755 verifyFormat("switch (x) {\n" 756 "case 1: {\n" 757 " f();\n" 758 " g();\n" 759 "} break;\n" 760 "}"); 761 verifyFormat("switch (test)\n" 762 " ;"); 763 verifyFormat("switch (x) {\n" 764 "default: {\n" 765 " // Do nothing.\n" 766 "}\n" 767 "}"); 768 verifyFormat("switch (x) {\n" 769 "// comment\n" 770 "// if 1, do f()\n" 771 "case 1:\n" 772 " f();\n" 773 "}"); 774 verifyFormat("switch (x) {\n" 775 "case 1:\n" 776 " // Do amazing stuff\n" 777 " {\n" 778 " f();\n" 779 " g();\n" 780 " }\n" 781 " break;\n" 782 "}"); 783 verifyFormat("#define A \\\n" 784 " switch (x) { \\\n" 785 " case a: \\\n" 786 " foo = b; \\\n" 787 " }", 788 getLLVMStyleWithColumns(20)); 789 verifyFormat("#define OPERATION_CASE(name) \\\n" 790 " case OP_name: \\\n" 791 " return operations::Operation##name\n", 792 getLLVMStyleWithColumns(40)); 793 verifyFormat("switch (x) {\n" 794 "case 1:;\n" 795 "default:;\n" 796 " int i;\n" 797 "}"); 798 799 verifyGoogleFormat("switch (x) {\n" 800 " case 1:\n" 801 " f();\n" 802 " break;\n" 803 " case kFoo:\n" 804 " case ns::kBar:\n" 805 " case kBaz:\n" 806 " break;\n" 807 " default:\n" 808 " g();\n" 809 " break;\n" 810 "}"); 811 verifyGoogleFormat("switch (x) {\n" 812 " case 1: {\n" 813 " f();\n" 814 " break;\n" 815 " }\n" 816 "}"); 817 verifyGoogleFormat("switch (test)\n" 818 " ;"); 819 820 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 821 " case OP_name: \\\n" 822 " return operations::Operation##name\n"); 823 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 824 " // Get the correction operation class.\n" 825 " switch (OpCode) {\n" 826 " CASE(Add);\n" 827 " CASE(Subtract);\n" 828 " default:\n" 829 " return operations::Unknown;\n" 830 " }\n" 831 "#undef OPERATION_CASE\n" 832 "}"); 833 verifyFormat("DEBUG({\n" 834 " switch (x) {\n" 835 " case A:\n" 836 " f();\n" 837 " break;\n" 838 " // fallthrough\n" 839 " case B:\n" 840 " g();\n" 841 " break;\n" 842 " }\n" 843 "});"); 844 EXPECT_EQ("DEBUG({\n" 845 " switch (x) {\n" 846 " case A:\n" 847 " f();\n" 848 " break;\n" 849 " // On B:\n" 850 " case B:\n" 851 " g();\n" 852 " break;\n" 853 " }\n" 854 "});", 855 format("DEBUG({\n" 856 " switch (x) {\n" 857 " case A:\n" 858 " f();\n" 859 " break;\n" 860 " // On B:\n" 861 " case B:\n" 862 " g();\n" 863 " break;\n" 864 " }\n" 865 "});", 866 getLLVMStyle())); 867 verifyFormat("switch (a) {\n" 868 "case (b):\n" 869 " return;\n" 870 "}"); 871 872 verifyFormat("switch (a) {\n" 873 "case some_namespace::\n" 874 " some_constant:\n" 875 " return;\n" 876 "}", 877 getLLVMStyleWithColumns(34)); 878 } 879 880 TEST_F(FormatTest, CaseRanges) { 881 verifyFormat("switch (x) {\n" 882 "case 'A' ... 'Z':\n" 883 "case 1 ... 5:\n" 884 "case a ... b:\n" 885 " break;\n" 886 "}"); 887 } 888 889 TEST_F(FormatTest, ShortCaseLabels) { 890 FormatStyle Style = getLLVMStyle(); 891 Style.AllowShortCaseLabelsOnASingleLine = true; 892 verifyFormat("switch (a) {\n" 893 "case 1: x = 1; break;\n" 894 "case 2: return;\n" 895 "case 3:\n" 896 "case 4:\n" 897 "case 5: return;\n" 898 "case 6: // comment\n" 899 " return;\n" 900 "case 7:\n" 901 " // comment\n" 902 " return;\n" 903 "case 8:\n" 904 " x = 8; // comment\n" 905 " break;\n" 906 "default: y = 1; break;\n" 907 "}", 908 Style); 909 verifyFormat("switch (a) {\n" 910 "case 0: return; // comment\n" 911 "case 1: break; // comment\n" 912 "case 2: return;\n" 913 "// comment\n" 914 "case 3: return;\n" 915 "// comment 1\n" 916 "// comment 2\n" 917 "// comment 3\n" 918 "case 4: break; /* comment */\n" 919 "case 5:\n" 920 " // comment\n" 921 " break;\n" 922 "case 6: /* comment */ x = 1; break;\n" 923 "case 7: x = /* comment */ 1; break;\n" 924 "case 8:\n" 925 " x = 1; /* comment */\n" 926 " break;\n" 927 "case 9:\n" 928 " break; // comment line 1\n" 929 " // comment line 2\n" 930 "}", 931 Style); 932 EXPECT_EQ("switch (a) {\n" 933 "case 1:\n" 934 " x = 8;\n" 935 " // fall through\n" 936 "case 2: x = 8;\n" 937 "// comment\n" 938 "case 3:\n" 939 " return; /* comment line 1\n" 940 " * comment line 2 */\n" 941 "case 4: i = 8;\n" 942 "// something else\n" 943 "#if FOO\n" 944 "case 5: break;\n" 945 "#endif\n" 946 "}", 947 format("switch (a) {\n" 948 "case 1: x = 8;\n" 949 " // fall through\n" 950 "case 2:\n" 951 " x = 8;\n" 952 "// comment\n" 953 "case 3:\n" 954 " return; /* comment line 1\n" 955 " * comment line 2 */\n" 956 "case 4:\n" 957 " i = 8;\n" 958 "// something else\n" 959 "#if FOO\n" 960 "case 5: break;\n" 961 "#endif\n" 962 "}", 963 Style)); 964 EXPECT_EQ("switch (a) {\n" "case 0:\n" 965 " return; // long long long long long long long long long long long long comment\n" 966 " // line\n" "}", 967 format("switch (a) {\n" 968 "case 0: return; // long long long long long long long long long long long long comment line\n" 969 "}", 970 Style)); 971 EXPECT_EQ("switch (a) {\n" 972 "case 0:\n" 973 " return; /* long long long long long long long long long long long long comment\n" 974 " line */\n" 975 "}", 976 format("switch (a) {\n" 977 "case 0: return; /* long long long long long long long long long long long long comment line */\n" 978 "}", 979 Style)); 980 verifyFormat("switch (a) {\n" 981 "#if FOO\n" 982 "case 0: return 0;\n" 983 "#endif\n" 984 "}", 985 Style); 986 verifyFormat("switch (a) {\n" 987 "case 1: {\n" 988 "}\n" 989 "case 2: {\n" 990 " return;\n" 991 "}\n" 992 "case 3: {\n" 993 " x = 1;\n" 994 " return;\n" 995 "}\n" 996 "case 4:\n" 997 " if (x)\n" 998 " return;\n" 999 "}", 1000 Style); 1001 Style.ColumnLimit = 21; 1002 verifyFormat("switch (a) {\n" 1003 "case 1: x = 1; break;\n" 1004 "case 2: return;\n" 1005 "case 3:\n" 1006 "case 4:\n" 1007 "case 5: return;\n" 1008 "default:\n" 1009 " y = 1;\n" 1010 " break;\n" 1011 "}", 1012 Style); 1013 } 1014 1015 TEST_F(FormatTest, FormatsLabels) { 1016 verifyFormat("void f() {\n" 1017 " some_code();\n" 1018 "test_label:\n" 1019 " some_other_code();\n" 1020 " {\n" 1021 " some_more_code();\n" 1022 " another_label:\n" 1023 " some_more_code();\n" 1024 " }\n" 1025 "}"); 1026 verifyFormat("{\n" 1027 " some_code();\n" 1028 "test_label:\n" 1029 " some_other_code();\n" 1030 "}"); 1031 verifyFormat("{\n" 1032 " some_code();\n" 1033 "test_label:;\n" 1034 " int i = 0;\n" 1035 "}"); 1036 } 1037 1038 //===----------------------------------------------------------------------===// 1039 // Tests for classes, namespaces, etc. 1040 //===----------------------------------------------------------------------===// 1041 1042 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1043 verifyFormat("class A {};"); 1044 } 1045 1046 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1047 verifyFormat("class A {\n" 1048 "public:\n" 1049 "public: // comment\n" 1050 "protected:\n" 1051 "private:\n" 1052 " void f() {}\n" 1053 "};"); 1054 verifyGoogleFormat("class A {\n" 1055 " public:\n" 1056 " protected:\n" 1057 " private:\n" 1058 " void f() {}\n" 1059 "};"); 1060 verifyFormat("class A {\n" 1061 "public slots:\n" 1062 " void f1() {}\n" 1063 "public Q_SLOTS:\n" 1064 " void f2() {}\n" 1065 "protected slots:\n" 1066 " void f3() {}\n" 1067 "protected Q_SLOTS:\n" 1068 " void f4() {}\n" 1069 "private slots:\n" 1070 " void f5() {}\n" 1071 "private Q_SLOTS:\n" 1072 " void f6() {}\n" 1073 "signals:\n" 1074 " void g1();\n" 1075 "Q_SIGNALS:\n" 1076 " void g2();\n" 1077 "};"); 1078 1079 // Don't interpret 'signals' the wrong way. 1080 verifyFormat("signals.set();"); 1081 verifyFormat("for (Signals signals : f()) {\n}"); 1082 verifyFormat("{\n" 1083 " signals.set(); // This needs indentation.\n" 1084 "}"); 1085 verifyFormat("void f() {\n" 1086 "label:\n" 1087 " signals.baz();\n" 1088 "}"); 1089 } 1090 1091 TEST_F(FormatTest, SeparatesLogicalBlocks) { 1092 EXPECT_EQ("class A {\n" 1093 "public:\n" 1094 " void f();\n" 1095 "\n" 1096 "private:\n" 1097 " void g() {}\n" 1098 " // test\n" 1099 "protected:\n" 1100 " int h;\n" 1101 "};", 1102 format("class A {\n" 1103 "public:\n" 1104 "void f();\n" 1105 "private:\n" 1106 "void g() {}\n" 1107 "// test\n" 1108 "protected:\n" 1109 "int h;\n" 1110 "};")); 1111 EXPECT_EQ("class A {\n" 1112 "protected:\n" 1113 "public:\n" 1114 " void f();\n" 1115 "};", 1116 format("class A {\n" 1117 "protected:\n" 1118 "\n" 1119 "public:\n" 1120 "\n" 1121 " void f();\n" 1122 "};")); 1123 1124 // Even ensure proper spacing inside macros. 1125 EXPECT_EQ("#define B \\\n" 1126 " class A { \\\n" 1127 " protected: \\\n" 1128 " public: \\\n" 1129 " void f(); \\\n" 1130 " };", 1131 format("#define B \\\n" 1132 " class A { \\\n" 1133 " protected: \\\n" 1134 " \\\n" 1135 " public: \\\n" 1136 " \\\n" 1137 " void f(); \\\n" 1138 " };", 1139 getGoogleStyle())); 1140 // But don't remove empty lines after macros ending in access specifiers. 1141 EXPECT_EQ("#define A private:\n" 1142 "\n" 1143 "int i;", 1144 format("#define A private:\n" 1145 "\n" 1146 "int i;")); 1147 } 1148 1149 TEST_F(FormatTest, FormatsClasses) { 1150 verifyFormat("class A : public B {};"); 1151 verifyFormat("class A : public ::B {};"); 1152 1153 verifyFormat( 1154 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1155 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1156 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 1157 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 1158 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 1159 verifyFormat( 1160 "class A : public B, public C, public D, public E, public F {};"); 1161 verifyFormat("class AAAAAAAAAAAA : public B,\n" 1162 " public C,\n" 1163 " public D,\n" 1164 " public E,\n" 1165 " public F,\n" 1166 " public G {};"); 1167 1168 verifyFormat("class\n" 1169 " ReallyReallyLongClassName {\n" 1170 " int i;\n" 1171 "};", 1172 getLLVMStyleWithColumns(32)); 1173 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 1174 " aaaaaaaaaaaaaaaa> {};"); 1175 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 1176 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 1177 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 1178 verifyFormat("template <class R, class C>\n" 1179 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 1180 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 1181 verifyFormat("class ::A::B {};"); 1182 } 1183 1184 TEST_F(FormatTest, BreakBeforeInheritanceComma) { 1185 FormatStyle StyleWithInheritanceBreak = getLLVMStyle(); 1186 StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true; 1187 1188 verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak); 1189 verifyFormat("class MyClass\n" 1190 " : public X\n" 1191 " , public Y {};", 1192 StyleWithInheritanceBreak); 1193 } 1194 1195 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 1196 verifyFormat("class A {\n} a, b;"); 1197 verifyFormat("struct A {\n} a, b;"); 1198 verifyFormat("union A {\n} a;"); 1199 } 1200 1201 TEST_F(FormatTest, FormatsEnum) { 1202 verifyFormat("enum {\n" 1203 " Zero,\n" 1204 " One = 1,\n" 1205 " Two = One + 1,\n" 1206 " Three = (One + Two),\n" 1207 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1208 " Five = (One, Two, Three, Four, 5)\n" 1209 "};"); 1210 verifyGoogleFormat("enum {\n" 1211 " Zero,\n" 1212 " One = 1,\n" 1213 " Two = One + 1,\n" 1214 " Three = (One + Two),\n" 1215 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1216 " Five = (One, Two, Three, Four, 5)\n" 1217 "};"); 1218 verifyFormat("enum Enum {};"); 1219 verifyFormat("enum {};"); 1220 verifyFormat("enum X E {} d;"); 1221 verifyFormat("enum __attribute__((...)) E {} d;"); 1222 verifyFormat("enum __declspec__((...)) E {} d;"); 1223 verifyFormat("enum {\n" 1224 " Bar = Foo<int, int>::value\n" 1225 "};", 1226 getLLVMStyleWithColumns(30)); 1227 1228 verifyFormat("enum ShortEnum { A, B, C };"); 1229 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 1230 1231 EXPECT_EQ("enum KeepEmptyLines {\n" 1232 " ONE,\n" 1233 "\n" 1234 " TWO,\n" 1235 "\n" 1236 " THREE\n" 1237 "}", 1238 format("enum KeepEmptyLines {\n" 1239 " ONE,\n" 1240 "\n" 1241 " TWO,\n" 1242 "\n" 1243 "\n" 1244 " THREE\n" 1245 "}")); 1246 verifyFormat("enum E { // comment\n" 1247 " ONE,\n" 1248 " TWO\n" 1249 "};\n" 1250 "int i;"); 1251 // Not enums. 1252 verifyFormat("enum X f() {\n" 1253 " a();\n" 1254 " return 42;\n" 1255 "}"); 1256 verifyFormat("enum X Type::f() {\n" 1257 " a();\n" 1258 " return 42;\n" 1259 "}"); 1260 verifyFormat("enum ::X f() {\n" 1261 " a();\n" 1262 " return 42;\n" 1263 "}"); 1264 verifyFormat("enum ns::X f() {\n" 1265 " a();\n" 1266 " return 42;\n" 1267 "}"); 1268 } 1269 1270 TEST_F(FormatTest, FormatsEnumsWithErrors) { 1271 verifyFormat("enum Type {\n" 1272 " One = 0; // These semicolons should be commas.\n" 1273 " Two = 1;\n" 1274 "};"); 1275 verifyFormat("namespace n {\n" 1276 "enum Type {\n" 1277 " One,\n" 1278 " Two, // missing };\n" 1279 " int i;\n" 1280 "}\n" 1281 "void g() {}"); 1282 } 1283 1284 TEST_F(FormatTest, FormatsEnumStruct) { 1285 verifyFormat("enum struct {\n" 1286 " Zero,\n" 1287 " One = 1,\n" 1288 " Two = One + 1,\n" 1289 " Three = (One + Two),\n" 1290 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1291 " Five = (One, Two, Three, Four, 5)\n" 1292 "};"); 1293 verifyFormat("enum struct Enum {};"); 1294 verifyFormat("enum struct {};"); 1295 verifyFormat("enum struct X E {} d;"); 1296 verifyFormat("enum struct __attribute__((...)) E {} d;"); 1297 verifyFormat("enum struct __declspec__((...)) E {} d;"); 1298 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 1299 } 1300 1301 TEST_F(FormatTest, FormatsEnumClass) { 1302 verifyFormat("enum class {\n" 1303 " Zero,\n" 1304 " One = 1,\n" 1305 " Two = One + 1,\n" 1306 " Three = (One + Two),\n" 1307 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 1308 " Five = (One, Two, Three, Four, 5)\n" 1309 "};"); 1310 verifyFormat("enum class Enum {};"); 1311 verifyFormat("enum class {};"); 1312 verifyFormat("enum class X E {} d;"); 1313 verifyFormat("enum class __attribute__((...)) E {} d;"); 1314 verifyFormat("enum class __declspec__((...)) E {} d;"); 1315 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 1316 } 1317 1318 TEST_F(FormatTest, FormatsEnumTypes) { 1319 verifyFormat("enum X : int {\n" 1320 " A, // Force multiple lines.\n" 1321 " B\n" 1322 "};"); 1323 verifyFormat("enum X : int { A, B };"); 1324 verifyFormat("enum X : std::uint32_t { A, B };"); 1325 } 1326 1327 TEST_F(FormatTest, FormatsNSEnums) { 1328 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 1329 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 1330 " // Information about someDecentlyLongValue.\n" 1331 " someDecentlyLongValue,\n" 1332 " // Information about anotherDecentlyLongValue.\n" 1333 " anotherDecentlyLongValue,\n" 1334 " // Information about aThirdDecentlyLongValue.\n" 1335 " aThirdDecentlyLongValue\n" 1336 "};"); 1337 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 1338 " a = 1,\n" 1339 " b = 2,\n" 1340 " c = 3,\n" 1341 "};"); 1342 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 1343 " a = 1,\n" 1344 " b = 2,\n" 1345 " c = 3,\n" 1346 "};"); 1347 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 1348 " a = 1,\n" 1349 " b = 2,\n" 1350 " c = 3,\n" 1351 "};"); 1352 } 1353 1354 TEST_F(FormatTest, FormatsBitfields) { 1355 verifyFormat("struct Bitfields {\n" 1356 " unsigned sClass : 8;\n" 1357 " unsigned ValueKind : 2;\n" 1358 "};"); 1359 verifyFormat("struct A {\n" 1360 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 1361 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 1362 "};"); 1363 verifyFormat("struct MyStruct {\n" 1364 " uchar data;\n" 1365 " uchar : 8;\n" 1366 " uchar : 8;\n" 1367 " uchar other;\n" 1368 "};"); 1369 } 1370 1371 TEST_F(FormatTest, FormatsNamespaces) { 1372 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 1373 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 1374 1375 verifyFormat("namespace some_namespace {\n" 1376 "class A {};\n" 1377 "void f() { f(); }\n" 1378 "}", 1379 LLVMWithNoNamespaceFix); 1380 verifyFormat("namespace {\n" 1381 "class A {};\n" 1382 "void f() { f(); }\n" 1383 "}", 1384 LLVMWithNoNamespaceFix); 1385 verifyFormat("inline namespace X {\n" 1386 "class A {};\n" 1387 "void f() { f(); }\n" 1388 "}", 1389 LLVMWithNoNamespaceFix); 1390 verifyFormat("using namespace some_namespace;\n" 1391 "class A {};\n" 1392 "void f() { f(); }", 1393 LLVMWithNoNamespaceFix); 1394 1395 // This code is more common than we thought; if we 1396 // layout this correctly the semicolon will go into 1397 // its own line, which is undesirable. 1398 verifyFormat("namespace {};", 1399 LLVMWithNoNamespaceFix); 1400 verifyFormat("namespace {\n" 1401 "class A {};\n" 1402 "};", 1403 LLVMWithNoNamespaceFix); 1404 1405 verifyFormat("namespace {\n" 1406 "int SomeVariable = 0; // comment\n" 1407 "} // namespace", 1408 LLVMWithNoNamespaceFix); 1409 EXPECT_EQ("#ifndef HEADER_GUARD\n" 1410 "#define HEADER_GUARD\n" 1411 "namespace my_namespace {\n" 1412 "int i;\n" 1413 "} // my_namespace\n" 1414 "#endif // HEADER_GUARD", 1415 format("#ifndef HEADER_GUARD\n" 1416 " #define HEADER_GUARD\n" 1417 " namespace my_namespace {\n" 1418 "int i;\n" 1419 "} // my_namespace\n" 1420 "#endif // HEADER_GUARD", 1421 LLVMWithNoNamespaceFix)); 1422 1423 EXPECT_EQ("namespace A::B {\n" 1424 "class C {};\n" 1425 "}", 1426 format("namespace A::B {\n" 1427 "class C {};\n" 1428 "}", 1429 LLVMWithNoNamespaceFix)); 1430 1431 FormatStyle Style = getLLVMStyle(); 1432 Style.NamespaceIndentation = FormatStyle::NI_All; 1433 EXPECT_EQ("namespace out {\n" 1434 " int i;\n" 1435 " namespace in {\n" 1436 " int i;\n" 1437 " } // namespace in\n" 1438 "} // namespace out", 1439 format("namespace out {\n" 1440 "int i;\n" 1441 "namespace in {\n" 1442 "int i;\n" 1443 "} // namespace in\n" 1444 "} // namespace out", 1445 Style)); 1446 1447 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1448 EXPECT_EQ("namespace out {\n" 1449 "int i;\n" 1450 "namespace in {\n" 1451 " int i;\n" 1452 "} // namespace in\n" 1453 "} // namespace out", 1454 format("namespace out {\n" 1455 "int i;\n" 1456 "namespace in {\n" 1457 "int i;\n" 1458 "} // namespace in\n" 1459 "} // namespace out", 1460 Style)); 1461 } 1462 1463 TEST_F(FormatTest, FormatsCompactNamespaces) { 1464 FormatStyle Style = getLLVMStyle(); 1465 Style.CompactNamespaces = true; 1466 1467 verifyFormat("namespace A { namespace B {\n" 1468 "}} // namespace A::B", 1469 Style); 1470 1471 EXPECT_EQ("namespace out { namespace in {\n" 1472 "}} // namespace out::in", 1473 format("namespace out {\n" 1474 "namespace in {\n" 1475 "} // namespace in\n" 1476 "} // namespace out", 1477 Style)); 1478 1479 // Only namespaces which have both consecutive opening and end get compacted 1480 EXPECT_EQ("namespace out {\n" 1481 "namespace in1 {\n" 1482 "} // namespace in1\n" 1483 "namespace in2 {\n" 1484 "} // namespace in2\n" 1485 "} // namespace out", 1486 format("namespace out {\n" 1487 "namespace in1 {\n" 1488 "} // namespace in1\n" 1489 "namespace in2 {\n" 1490 "} // namespace in2\n" 1491 "} // namespace out", 1492 Style)); 1493 1494 EXPECT_EQ("namespace out {\n" 1495 "int i;\n" 1496 "namespace in {\n" 1497 "int j;\n" 1498 "} // namespace in\n" 1499 "int k;\n" 1500 "} // namespace out", 1501 format("namespace out { int i;\n" 1502 "namespace in { int j; } // namespace in\n" 1503 "int k; } // namespace out", 1504 Style)); 1505 1506 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 1507 "}}} // namespace A::B::C\n", 1508 format("namespace A { namespace B {\n" 1509 "namespace C {\n" 1510 "}} // namespace B::C\n" 1511 "} // namespace A\n", 1512 Style)); 1513 1514 Style.ColumnLimit = 40; 1515 EXPECT_EQ("namespace aaaaaaaaaa {\n" 1516 "namespace bbbbbbbbbb {\n" 1517 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 1518 format("namespace aaaaaaaaaa {\n" 1519 "namespace bbbbbbbbbb {\n" 1520 "} // namespace bbbbbbbbbb\n" 1521 "} // namespace aaaaaaaaaa", 1522 Style)); 1523 1524 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 1525 "namespace cccccc {\n" 1526 "}}} // namespace aaaaaa::bbbbbb::cccccc", 1527 format("namespace aaaaaa {\n" 1528 "namespace bbbbbb {\n" 1529 "namespace cccccc {\n" 1530 "} // namespace cccccc\n" 1531 "} // namespace bbbbbb\n" 1532 "} // namespace aaaaaa", 1533 Style)); 1534 Style.ColumnLimit = 80; 1535 1536 // Extra semicolon after 'inner' closing brace prevents merging 1537 EXPECT_EQ("namespace out { namespace in {\n" 1538 "}; } // namespace out::in", 1539 format("namespace out {\n" 1540 "namespace in {\n" 1541 "}; // namespace in\n" 1542 "} // namespace out", 1543 Style)); 1544 1545 // Extra semicolon after 'outer' closing brace is conserved 1546 EXPECT_EQ("namespace out { namespace in {\n" 1547 "}}; // namespace out::in", 1548 format("namespace out {\n" 1549 "namespace in {\n" 1550 "} // namespace in\n" 1551 "}; // namespace out", 1552 Style)); 1553 1554 Style.NamespaceIndentation = FormatStyle::NI_All; 1555 EXPECT_EQ("namespace out { namespace in {\n" 1556 " int i;\n" 1557 "}} // namespace out::in", 1558 format("namespace out {\n" 1559 "namespace in {\n" 1560 "int i;\n" 1561 "} // namespace in\n" 1562 "} // namespace out", 1563 Style)); 1564 EXPECT_EQ("namespace out { namespace mid {\n" 1565 " namespace in {\n" 1566 " int j;\n" 1567 " } // namespace in\n" 1568 " int k;\n" 1569 "}} // namespace out::mid", 1570 format("namespace out { namespace mid {\n" 1571 "namespace in { int j; } // namespace in\n" 1572 "int k; }} // namespace out::mid", 1573 Style)); 1574 1575 Style.NamespaceIndentation = FormatStyle::NI_Inner; 1576 EXPECT_EQ("namespace out { namespace in {\n" 1577 " int i;\n" 1578 "}} // namespace out::in", 1579 format("namespace out {\n" 1580 "namespace in {\n" 1581 "int i;\n" 1582 "} // namespace in\n" 1583 "} // namespace out", 1584 Style)); 1585 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 1586 " int i;\n" 1587 "}}} // namespace out::mid::in", 1588 format("namespace out {\n" 1589 "namespace mid {\n" 1590 "namespace in {\n" 1591 "int i;\n" 1592 "} // namespace in\n" 1593 "} // namespace mid\n" 1594 "} // namespace out", 1595 Style)); 1596 } 1597 1598 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 1599 1600 TEST_F(FormatTest, FormatsInlineASM) { 1601 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 1602 verifyFormat("asm(\"nop\" ::: \"memory\");"); 1603 verifyFormat( 1604 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 1605 " \"cpuid\\n\\t\"\n" 1606 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 1607 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 1608 " : \"a\"(value));"); 1609 EXPECT_EQ( 1610 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1611 " __asm {\n" 1612 " mov edx,[that] // vtable in edx\n" 1613 " mov eax,methodIndex\n" 1614 " call [edx][eax*4] // stdcall\n" 1615 " }\n" 1616 "}", 1617 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 1618 " __asm {\n" 1619 " mov edx,[that] // vtable in edx\n" 1620 " mov eax,methodIndex\n" 1621 " call [edx][eax*4] // stdcall\n" 1622 " }\n" 1623 "}")); 1624 EXPECT_EQ("_asm {\n" 1625 " xor eax, eax;\n" 1626 " cpuid;\n" 1627 "}", 1628 format("_asm {\n" 1629 " xor eax, eax;\n" 1630 " cpuid;\n" 1631 "}")); 1632 verifyFormat("void function() {\n" 1633 " // comment\n" 1634 " asm(\"\");\n" 1635 "}"); 1636 EXPECT_EQ("__asm {\n" 1637 "}\n" 1638 "int i;", 1639 format("__asm {\n" 1640 "}\n" 1641 "int i;")); 1642 } 1643 1644 TEST_F(FormatTest, FormatTryCatch) { 1645 verifyFormat("try {\n" 1646 " throw a * b;\n" 1647 "} catch (int a) {\n" 1648 " // Do nothing.\n" 1649 "} catch (...) {\n" 1650 " exit(42);\n" 1651 "}"); 1652 1653 // Function-level try statements. 1654 verifyFormat("int f() try { return 4; } catch (...) {\n" 1655 " return 5;\n" 1656 "}"); 1657 verifyFormat("class A {\n" 1658 " int a;\n" 1659 " A() try : a(0) {\n" 1660 " } catch (...) {\n" 1661 " throw;\n" 1662 " }\n" 1663 "};\n"); 1664 1665 // Incomplete try-catch blocks. 1666 verifyIncompleteFormat("try {} catch ("); 1667 } 1668 1669 TEST_F(FormatTest, FormatSEHTryCatch) { 1670 verifyFormat("__try {\n" 1671 " int a = b * c;\n" 1672 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 1673 " // Do nothing.\n" 1674 "}"); 1675 1676 verifyFormat("__try {\n" 1677 " int a = b * c;\n" 1678 "} __finally {\n" 1679 " // Do nothing.\n" 1680 "}"); 1681 1682 verifyFormat("DEBUG({\n" 1683 " __try {\n" 1684 " } __finally {\n" 1685 " }\n" 1686 "});\n"); 1687 } 1688 1689 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 1690 verifyFormat("try {\n" 1691 " f();\n" 1692 "} catch {\n" 1693 " g();\n" 1694 "}"); 1695 verifyFormat("try {\n" 1696 " f();\n" 1697 "} catch (A a) MACRO(x) {\n" 1698 " g();\n" 1699 "} catch (B b) MACRO(x) {\n" 1700 " g();\n" 1701 "}"); 1702 } 1703 1704 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 1705 FormatStyle Style = getLLVMStyle(); 1706 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 1707 FormatStyle::BS_WebKit}) { 1708 Style.BreakBeforeBraces = BraceStyle; 1709 verifyFormat("try {\n" 1710 " // something\n" 1711 "} catch (...) {\n" 1712 " // something\n" 1713 "}", 1714 Style); 1715 } 1716 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 1717 verifyFormat("try {\n" 1718 " // something\n" 1719 "}\n" 1720 "catch (...) {\n" 1721 " // something\n" 1722 "}", 1723 Style); 1724 verifyFormat("__try {\n" 1725 " // something\n" 1726 "}\n" 1727 "__finally {\n" 1728 " // something\n" 1729 "}", 1730 Style); 1731 verifyFormat("@try {\n" 1732 " // something\n" 1733 "}\n" 1734 "@finally {\n" 1735 " // something\n" 1736 "}", 1737 Style); 1738 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1739 verifyFormat("try\n" 1740 "{\n" 1741 " // something\n" 1742 "}\n" 1743 "catch (...)\n" 1744 "{\n" 1745 " // something\n" 1746 "}", 1747 Style); 1748 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 1749 verifyFormat("try\n" 1750 " {\n" 1751 " // something\n" 1752 " }\n" 1753 "catch (...)\n" 1754 " {\n" 1755 " // something\n" 1756 " }", 1757 Style); 1758 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1759 Style.BraceWrapping.BeforeCatch = true; 1760 verifyFormat("try {\n" 1761 " // something\n" 1762 "}\n" 1763 "catch (...) {\n" 1764 " // something\n" 1765 "}", 1766 Style); 1767 } 1768 1769 TEST_F(FormatTest, StaticInitializers) { 1770 verifyFormat("static SomeClass SC = {1, 'a'};"); 1771 1772 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 1773 " 100000000, " 1774 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 1775 1776 // Here, everything other than the "}" would fit on a line. 1777 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 1778 " 10000000000000000000000000};"); 1779 EXPECT_EQ("S s = {a,\n" 1780 "\n" 1781 " b};", 1782 format("S s = {\n" 1783 " a,\n" 1784 "\n" 1785 " b\n" 1786 "};")); 1787 1788 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 1789 // line. However, the formatting looks a bit off and this probably doesn't 1790 // happen often in practice. 1791 verifyFormat("static int Variable[1] = {\n" 1792 " {1000000000000000000000000000000000000}};", 1793 getLLVMStyleWithColumns(40)); 1794 } 1795 1796 TEST_F(FormatTest, DesignatedInitializers) { 1797 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 1798 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 1799 " .bbbbbbbbbb = 2,\n" 1800 " .cccccccccc = 3,\n" 1801 " .dddddddddd = 4,\n" 1802 " .eeeeeeeeee = 5};"); 1803 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1804 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 1805 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 1806 " .ccccccccccccccccccccccccccc = 3,\n" 1807 " .ddddddddddddddddddddddddddd = 4,\n" 1808 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 1809 1810 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 1811 1812 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 1813 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 1814 " [2] = bbbbbbbbbb,\n" 1815 " [3] = cccccccccc,\n" 1816 " [4] = dddddddddd,\n" 1817 " [5] = eeeeeeeeee};"); 1818 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 1819 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1820 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 1821 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 1822 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 1823 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 1824 } 1825 1826 TEST_F(FormatTest, NestedStaticInitializers) { 1827 verifyFormat("static A x = {{{}}};\n"); 1828 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 1829 " {init1, init2, init3, init4}}};", 1830 getLLVMStyleWithColumns(50)); 1831 1832 verifyFormat("somes Status::global_reps[3] = {\n" 1833 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 1834 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 1835 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 1836 getLLVMStyleWithColumns(60)); 1837 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 1838 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 1839 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 1840 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 1841 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 1842 " {rect.fRight - rect.fLeft, rect.fBottom - " 1843 "rect.fTop}};"); 1844 1845 verifyFormat( 1846 "SomeArrayOfSomeType a = {\n" 1847 " {{1, 2, 3},\n" 1848 " {1, 2, 3},\n" 1849 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 1850 " 333333333333333333333333333333},\n" 1851 " {1, 2, 3},\n" 1852 " {1, 2, 3}}};"); 1853 verifyFormat( 1854 "SomeArrayOfSomeType a = {\n" 1855 " {{1, 2, 3}},\n" 1856 " {{1, 2, 3}},\n" 1857 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 1858 " 333333333333333333333333333333}},\n" 1859 " {{1, 2, 3}},\n" 1860 " {{1, 2, 3}}};"); 1861 1862 verifyFormat("struct {\n" 1863 " unsigned bit;\n" 1864 " const char *const name;\n" 1865 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 1866 " {kOsWin, \"Windows\"},\n" 1867 " {kOsLinux, \"Linux\"},\n" 1868 " {kOsCrOS, \"Chrome OS\"}};"); 1869 verifyFormat("struct {\n" 1870 " unsigned bit;\n" 1871 " const char *const name;\n" 1872 "} kBitsToOs[] = {\n" 1873 " {kOsMac, \"Mac\"},\n" 1874 " {kOsWin, \"Windows\"},\n" 1875 " {kOsLinux, \"Linux\"},\n" 1876 " {kOsCrOS, \"Chrome OS\"},\n" 1877 "};"); 1878 } 1879 1880 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 1881 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 1882 " \\\n" 1883 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 1884 } 1885 1886 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 1887 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 1888 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 1889 1890 // Do break defaulted and deleted functions. 1891 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 1892 " default;", 1893 getLLVMStyleWithColumns(40)); 1894 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 1895 " delete;", 1896 getLLVMStyleWithColumns(40)); 1897 } 1898 1899 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 1900 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 1901 getLLVMStyleWithColumns(40)); 1902 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 1903 getLLVMStyleWithColumns(40)); 1904 EXPECT_EQ("#define Q \\\n" 1905 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 1906 " \"aaaaaaaa.cpp\"", 1907 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 1908 getLLVMStyleWithColumns(40))); 1909 } 1910 1911 TEST_F(FormatTest, UnderstandsLinePPDirective) { 1912 EXPECT_EQ("# 123 \"A string literal\"", 1913 format(" # 123 \"A string literal\"")); 1914 } 1915 1916 TEST_F(FormatTest, LayoutUnknownPPDirective) { 1917 EXPECT_EQ("#;", format("#;")); 1918 verifyFormat("#\n;\n;\n;"); 1919 } 1920 1921 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 1922 EXPECT_EQ("#line 42 \"test\"\n", 1923 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 1924 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 1925 getLLVMStyleWithColumns(12))); 1926 } 1927 1928 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 1929 EXPECT_EQ("#line 42 \"test\"", 1930 format("# \\\n line \\\n 42 \\\n \"test\"")); 1931 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 1932 } 1933 1934 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 1935 verifyFormat("#define A \\x20"); 1936 verifyFormat("#define A \\ x20"); 1937 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 1938 verifyFormat("#define A ''"); 1939 verifyFormat("#define A ''qqq"); 1940 verifyFormat("#define A `qqq"); 1941 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 1942 EXPECT_EQ("const char *c = STRINGIFY(\n" 1943 "\\na : b);", 1944 format("const char * c = STRINGIFY(\n" 1945 "\\na : b);")); 1946 1947 verifyFormat("a\r\\"); 1948 verifyFormat("a\v\\"); 1949 verifyFormat("a\f\\"); 1950 } 1951 1952 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 1953 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 1954 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 1955 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 1956 // FIXME: We never break before the macro name. 1957 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 1958 1959 verifyFormat("#define A A\n#define A A"); 1960 verifyFormat("#define A(X) A\n#define A A"); 1961 1962 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 1963 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 1964 } 1965 1966 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 1967 EXPECT_EQ("// somecomment\n" 1968 "#include \"a.h\"\n" 1969 "#define A( \\\n" 1970 " A, B)\n" 1971 "#include \"b.h\"\n" 1972 "// somecomment\n", 1973 format(" // somecomment\n" 1974 " #include \"a.h\"\n" 1975 "#define A(A,\\\n" 1976 " B)\n" 1977 " #include \"b.h\"\n" 1978 " // somecomment\n", 1979 getLLVMStyleWithColumns(13))); 1980 } 1981 1982 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 1983 1984 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 1985 EXPECT_EQ("#define A \\\n" 1986 " c; \\\n" 1987 " e;\n" 1988 "f;", 1989 format("#define A c; e;\n" 1990 "f;", 1991 getLLVMStyleWithColumns(14))); 1992 } 1993 1994 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 1995 1996 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 1997 EXPECT_EQ("int x,\n" 1998 "#define A\n" 1999 " y;", 2000 format("int x,\n#define A\ny;")); 2001 } 2002 2003 TEST_F(FormatTest, HashInMacroDefinition) { 2004 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2005 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2006 verifyFormat("#define A \\\n" 2007 " { \\\n" 2008 " f(#c); \\\n" 2009 " }", 2010 getLLVMStyleWithColumns(11)); 2011 2012 verifyFormat("#define A(X) \\\n" 2013 " void function##X()", 2014 getLLVMStyleWithColumns(22)); 2015 2016 verifyFormat("#define A(a, b, c) \\\n" 2017 " void a##b##c()", 2018 getLLVMStyleWithColumns(22)); 2019 2020 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2021 } 2022 2023 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2024 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2025 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2026 } 2027 2028 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2029 EXPECT_EQ("#define A b;", format("#define A \\\n" 2030 " \\\n" 2031 " b;", 2032 getLLVMStyleWithColumns(25))); 2033 EXPECT_EQ("#define A \\\n" 2034 " \\\n" 2035 " a; \\\n" 2036 " b;", 2037 format("#define A \\\n" 2038 " \\\n" 2039 " a; \\\n" 2040 " b;", 2041 getLLVMStyleWithColumns(11))); 2042 EXPECT_EQ("#define A \\\n" 2043 " a; \\\n" 2044 " \\\n" 2045 " b;", 2046 format("#define A \\\n" 2047 " a; \\\n" 2048 " \\\n" 2049 " b;", 2050 getLLVMStyleWithColumns(11))); 2051 } 2052 2053 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2054 verifyIncompleteFormat("#define A :"); 2055 verifyFormat("#define SOMECASES \\\n" 2056 " case 1: \\\n" 2057 " case 2\n", 2058 getLLVMStyleWithColumns(20)); 2059 verifyFormat("#define MACRO(a) \\\n" 2060 " if (a) \\\n" 2061 " f(); \\\n" 2062 " else \\\n" 2063 " g()", 2064 getLLVMStyleWithColumns(18)); 2065 verifyFormat("#define A template <typename T>"); 2066 verifyIncompleteFormat("#define STR(x) #x\n" 2067 "f(STR(this_is_a_string_literal{));"); 2068 verifyFormat("#pragma omp threadprivate( \\\n" 2069 " y)), // expected-warning", 2070 getLLVMStyleWithColumns(28)); 2071 verifyFormat("#d, = };"); 2072 verifyFormat("#if \"a"); 2073 verifyIncompleteFormat("({\n" 2074 "#define b \\\n" 2075 " } \\\n" 2076 " a\n" 2077 "a", 2078 getLLVMStyleWithColumns(15)); 2079 verifyFormat("#define A \\\n" 2080 " { \\\n" 2081 " {\n" 2082 "#define B \\\n" 2083 " } \\\n" 2084 " }", 2085 getLLVMStyleWithColumns(15)); 2086 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2087 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2088 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2089 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2090 } 2091 2092 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2093 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2094 EXPECT_EQ("class A : public QObject {\n" 2095 " Q_OBJECT\n" 2096 "\n" 2097 " A() {}\n" 2098 "};", 2099 format("class A : public QObject {\n" 2100 " Q_OBJECT\n" 2101 "\n" 2102 " A() {\n}\n" 2103 "} ;")); 2104 EXPECT_EQ("MACRO\n" 2105 "/*static*/ int i;", 2106 format("MACRO\n" 2107 " /*static*/ int i;")); 2108 EXPECT_EQ("SOME_MACRO\n" 2109 "namespace {\n" 2110 "void f();\n" 2111 "} // namespace", 2112 format("SOME_MACRO\n" 2113 " namespace {\n" 2114 "void f( );\n" 2115 "} // namespace")); 2116 // Only if the identifier contains at least 5 characters. 2117 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2118 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2119 // Only if everything is upper case. 2120 EXPECT_EQ("class A : public QObject {\n" 2121 " Q_Object A() {}\n" 2122 "};", 2123 format("class A : public QObject {\n" 2124 " Q_Object\n" 2125 " A() {\n}\n" 2126 "} ;")); 2127 2128 // Only if the next line can actually start an unwrapped line. 2129 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2130 format("SOME_WEIRD_LOG_MACRO\n" 2131 "<< SomeThing;")); 2132 2133 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2134 "(n, buffers))\n", 2135 getChromiumStyle(FormatStyle::LK_Cpp)); 2136 } 2137 2138 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2139 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2140 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2141 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2142 "class X {};\n" 2143 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2144 "int *createScopDetectionPass() { return 0; }", 2145 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2146 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2147 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2148 " class X {};\n" 2149 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2150 " int *createScopDetectionPass() { return 0; }")); 2151 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2152 // braces, so that inner block is indented one level more. 2153 EXPECT_EQ("int q() {\n" 2154 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2155 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2156 " IPC_END_MESSAGE_MAP()\n" 2157 "}", 2158 format("int q() {\n" 2159 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2160 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2161 " IPC_END_MESSAGE_MAP()\n" 2162 "}")); 2163 2164 // Same inside macros. 2165 EXPECT_EQ("#define LIST(L) \\\n" 2166 " L(A) \\\n" 2167 " L(B) \\\n" 2168 " L(C)", 2169 format("#define LIST(L) \\\n" 2170 " L(A) \\\n" 2171 " L(B) \\\n" 2172 " L(C)", 2173 getGoogleStyle())); 2174 2175 // These must not be recognized as macros. 2176 EXPECT_EQ("int q() {\n" 2177 " f(x);\n" 2178 " f(x) {}\n" 2179 " f(x)->g();\n" 2180 " f(x)->*g();\n" 2181 " f(x).g();\n" 2182 " f(x) = x;\n" 2183 " f(x) += x;\n" 2184 " f(x) -= x;\n" 2185 " f(x) *= x;\n" 2186 " f(x) /= x;\n" 2187 " f(x) %= x;\n" 2188 " f(x) &= x;\n" 2189 " f(x) |= x;\n" 2190 " f(x) ^= x;\n" 2191 " f(x) >>= x;\n" 2192 " f(x) <<= x;\n" 2193 " f(x)[y].z();\n" 2194 " LOG(INFO) << x;\n" 2195 " ifstream(x) >> x;\n" 2196 "}\n", 2197 format("int q() {\n" 2198 " f(x)\n;\n" 2199 " f(x)\n {}\n" 2200 " f(x)\n->g();\n" 2201 " f(x)\n->*g();\n" 2202 " f(x)\n.g();\n" 2203 " f(x)\n = x;\n" 2204 " f(x)\n += x;\n" 2205 " f(x)\n -= x;\n" 2206 " f(x)\n *= x;\n" 2207 " f(x)\n /= x;\n" 2208 " f(x)\n %= x;\n" 2209 " f(x)\n &= x;\n" 2210 " f(x)\n |= x;\n" 2211 " f(x)\n ^= x;\n" 2212 " f(x)\n >>= x;\n" 2213 " f(x)\n <<= x;\n" 2214 " f(x)\n[y].z();\n" 2215 " LOG(INFO)\n << x;\n" 2216 " ifstream(x)\n >> x;\n" 2217 "}\n")); 2218 EXPECT_EQ("int q() {\n" 2219 " F(x)\n" 2220 " if (1) {\n" 2221 " }\n" 2222 " F(x)\n" 2223 " while (1) {\n" 2224 " }\n" 2225 " F(x)\n" 2226 " G(x);\n" 2227 " F(x)\n" 2228 " try {\n" 2229 " Q();\n" 2230 " } catch (...) {\n" 2231 " }\n" 2232 "}\n", 2233 format("int q() {\n" 2234 "F(x)\n" 2235 "if (1) {}\n" 2236 "F(x)\n" 2237 "while (1) {}\n" 2238 "F(x)\n" 2239 "G(x);\n" 2240 "F(x)\n" 2241 "try { Q(); } catch (...) {}\n" 2242 "}\n")); 2243 EXPECT_EQ("class A {\n" 2244 " A() : t(0) {}\n" 2245 " A(int i) noexcept() : {}\n" 2246 " A(X x)\n" // FIXME: function-level try blocks are broken. 2247 " try : t(0) {\n" 2248 " } catch (...) {\n" 2249 " }\n" 2250 "};", 2251 format("class A {\n" 2252 " A()\n : t(0) {}\n" 2253 " A(int i)\n noexcept() : {}\n" 2254 " A(X x)\n" 2255 " try : t(0) {} catch (...) {}\n" 2256 "};")); 2257 EXPECT_EQ("class SomeClass {\n" 2258 "public:\n" 2259 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2260 "};", 2261 format("class SomeClass {\n" 2262 "public:\n" 2263 " SomeClass()\n" 2264 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2265 "};")); 2266 EXPECT_EQ("class SomeClass {\n" 2267 "public:\n" 2268 " SomeClass()\n" 2269 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2270 "};", 2271 format("class SomeClass {\n" 2272 "public:\n" 2273 " SomeClass()\n" 2274 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2275 "};", 2276 getLLVMStyleWithColumns(40))); 2277 2278 verifyFormat("MACRO(>)"); 2279 } 2280 2281 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 2282 verifyFormat("#define A \\\n" 2283 " f({ \\\n" 2284 " g(); \\\n" 2285 " });", 2286 getLLVMStyleWithColumns(11)); 2287 } 2288 2289 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 2290 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 2291 } 2292 2293 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 2294 verifyFormat("{\n { a #c; }\n}"); 2295 } 2296 2297 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 2298 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 2299 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 2300 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 2301 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 2302 } 2303 2304 TEST_F(FormatTest, EscapedNewlines) { 2305 EXPECT_EQ( 2306 "#define A \\\n int i; \\\n int j;", 2307 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 2308 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 2309 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 2310 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 2311 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 2312 2313 FormatStyle DontAlign = getLLVMStyle(); 2314 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 2315 DontAlign.MaxEmptyLinesToKeep = 3; 2316 // FIXME: can't use verifyFormat here because the newline before 2317 // "public:" is not inserted the first time it's reformatted 2318 EXPECT_EQ("#define A \\\n" 2319 " class Foo { \\\n" 2320 " void bar(); \\\n" 2321 "\\\n" 2322 "\\\n" 2323 "\\\n" 2324 " public: \\\n" 2325 " void baz(); \\\n" 2326 " };", 2327 format("#define A \\\n" 2328 " class Foo { \\\n" 2329 " void bar(); \\\n" 2330 "\\\n" 2331 "\\\n" 2332 "\\\n" 2333 " public: \\\n" 2334 " void baz(); \\\n" 2335 " };", DontAlign)); 2336 } 2337 2338 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 2339 verifyFormat("#define A \\\n" 2340 " int v( \\\n" 2341 " a); \\\n" 2342 " int i;", 2343 getLLVMStyleWithColumns(11)); 2344 } 2345 2346 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 2347 EXPECT_EQ( 2348 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2349 " \\\n" 2350 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2351 "\n" 2352 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2353 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 2354 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2355 "\\\n" 2356 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 2357 " \n" 2358 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 2359 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 2360 } 2361 2362 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 2363 EXPECT_EQ("int\n" 2364 "#define A\n" 2365 " a;", 2366 format("int\n#define A\na;")); 2367 verifyFormat("functionCallTo(\n" 2368 " someOtherFunction(\n" 2369 " withSomeParameters, whichInSequence,\n" 2370 " areLongerThanALine(andAnotherCall,\n" 2371 "#define A B\n" 2372 " withMoreParamters,\n" 2373 " whichStronglyInfluenceTheLayout),\n" 2374 " andMoreParameters),\n" 2375 " trailing);", 2376 getLLVMStyleWithColumns(69)); 2377 verifyFormat("Foo::Foo()\n" 2378 "#ifdef BAR\n" 2379 " : baz(0)\n" 2380 "#endif\n" 2381 "{\n" 2382 "}"); 2383 verifyFormat("void f() {\n" 2384 " if (true)\n" 2385 "#ifdef A\n" 2386 " f(42);\n" 2387 " x();\n" 2388 "#else\n" 2389 " g();\n" 2390 " x();\n" 2391 "#endif\n" 2392 "}"); 2393 verifyFormat("void f(param1, param2,\n" 2394 " param3,\n" 2395 "#ifdef A\n" 2396 " param4(param5,\n" 2397 "#ifdef A1\n" 2398 " param6,\n" 2399 "#ifdef A2\n" 2400 " param7),\n" 2401 "#else\n" 2402 " param8),\n" 2403 " param9,\n" 2404 "#endif\n" 2405 " param10,\n" 2406 "#endif\n" 2407 " param11)\n" 2408 "#else\n" 2409 " param12)\n" 2410 "#endif\n" 2411 "{\n" 2412 " x();\n" 2413 "}", 2414 getLLVMStyleWithColumns(28)); 2415 verifyFormat("#if 1\n" 2416 "int i;"); 2417 verifyFormat("#if 1\n" 2418 "#endif\n" 2419 "#if 1\n" 2420 "#else\n" 2421 "#endif\n"); 2422 verifyFormat("DEBUG({\n" 2423 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2424 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2425 "});\n" 2426 "#if a\n" 2427 "#else\n" 2428 "#endif"); 2429 2430 verifyIncompleteFormat("void f(\n" 2431 "#if A\n" 2432 ");\n" 2433 "#else\n" 2434 "#endif"); 2435 } 2436 2437 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 2438 verifyFormat("#endif\n" 2439 "#if B"); 2440 } 2441 2442 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 2443 FormatStyle SingleLine = getLLVMStyle(); 2444 SingleLine.AllowShortIfStatementsOnASingleLine = true; 2445 verifyFormat("#if 0\n" 2446 "#elif 1\n" 2447 "#endif\n" 2448 "void foo() {\n" 2449 " if (test) foo2();\n" 2450 "}", 2451 SingleLine); 2452 } 2453 2454 TEST_F(FormatTest, LayoutBlockInsideParens) { 2455 verifyFormat("functionCall({ int i; });"); 2456 verifyFormat("functionCall({\n" 2457 " int i;\n" 2458 " int j;\n" 2459 "});"); 2460 verifyFormat("functionCall(\n" 2461 " {\n" 2462 " int i;\n" 2463 " int j;\n" 2464 " },\n" 2465 " aaaa, bbbb, cccc);"); 2466 verifyFormat("functionA(functionB({\n" 2467 " int i;\n" 2468 " int j;\n" 2469 " }),\n" 2470 " aaaa, bbbb, cccc);"); 2471 verifyFormat("functionCall(\n" 2472 " {\n" 2473 " int i;\n" 2474 " int j;\n" 2475 " },\n" 2476 " aaaa, bbbb, // comment\n" 2477 " cccc);"); 2478 verifyFormat("functionA(functionB({\n" 2479 " int i;\n" 2480 " int j;\n" 2481 " }),\n" 2482 " aaaa, bbbb, // comment\n" 2483 " cccc);"); 2484 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 2485 verifyFormat("functionCall(aaaa, bbbb, {\n" 2486 " int i;\n" 2487 " int j;\n" 2488 "});"); 2489 verifyFormat( 2490 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 2491 " {\n" 2492 " int i; // break\n" 2493 " },\n" 2494 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 2495 " ccccccccccccccccc));"); 2496 verifyFormat("DEBUG({\n" 2497 " if (a)\n" 2498 " f();\n" 2499 "});"); 2500 } 2501 2502 TEST_F(FormatTest, LayoutBlockInsideStatement) { 2503 EXPECT_EQ("SOME_MACRO { int i; }\n" 2504 "int i;", 2505 format(" SOME_MACRO {int i;} int i;")); 2506 } 2507 2508 TEST_F(FormatTest, LayoutNestedBlocks) { 2509 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 2510 " struct s {\n" 2511 " int i;\n" 2512 " };\n" 2513 " s kBitsToOs[] = {{10}};\n" 2514 " for (int i = 0; i < 10; ++i)\n" 2515 " return;\n" 2516 "}"); 2517 verifyFormat("call(parameter, {\n" 2518 " something();\n" 2519 " // Comment using all columns.\n" 2520 " somethingelse();\n" 2521 "});", 2522 getLLVMStyleWithColumns(40)); 2523 verifyFormat("DEBUG( //\n" 2524 " { f(); }, a);"); 2525 verifyFormat("DEBUG( //\n" 2526 " {\n" 2527 " f(); //\n" 2528 " },\n" 2529 " a);"); 2530 2531 EXPECT_EQ("call(parameter, {\n" 2532 " something();\n" 2533 " // Comment too\n" 2534 " // looooooooooong.\n" 2535 " somethingElse();\n" 2536 "});", 2537 format("call(parameter, {\n" 2538 " something();\n" 2539 " // Comment too looooooooooong.\n" 2540 " somethingElse();\n" 2541 "});", 2542 getLLVMStyleWithColumns(29))); 2543 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 2544 EXPECT_EQ("DEBUG({ // comment\n" 2545 " int i;\n" 2546 "});", 2547 format("DEBUG({ // comment\n" 2548 "int i;\n" 2549 "});")); 2550 EXPECT_EQ("DEBUG({\n" 2551 " int i;\n" 2552 "\n" 2553 " // comment\n" 2554 " int j;\n" 2555 "});", 2556 format("DEBUG({\n" 2557 " int i;\n" 2558 "\n" 2559 " // comment\n" 2560 " int j;\n" 2561 "});")); 2562 2563 verifyFormat("DEBUG({\n" 2564 " if (a)\n" 2565 " return;\n" 2566 "});"); 2567 verifyGoogleFormat("DEBUG({\n" 2568 " if (a) return;\n" 2569 "});"); 2570 FormatStyle Style = getGoogleStyle(); 2571 Style.ColumnLimit = 45; 2572 verifyFormat("Debug(aaaaa,\n" 2573 " {\n" 2574 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 2575 " },\n" 2576 " a);", 2577 Style); 2578 2579 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 2580 2581 verifyNoCrash("^{v^{a}}"); 2582 } 2583 2584 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 2585 EXPECT_EQ("#define MACRO() \\\n" 2586 " Debug(aaa, /* force line break */ \\\n" 2587 " { \\\n" 2588 " int i; \\\n" 2589 " int j; \\\n" 2590 " })", 2591 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 2592 " { int i; int j; })", 2593 getGoogleStyle())); 2594 2595 EXPECT_EQ("#define A \\\n" 2596 " [] { \\\n" 2597 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2598 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 2599 " }", 2600 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 2601 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 2602 getGoogleStyle())); 2603 } 2604 2605 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 2606 EXPECT_EQ("{}", format("{}")); 2607 verifyFormat("enum E {};"); 2608 verifyFormat("enum E {}"); 2609 } 2610 2611 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 2612 FormatStyle Style = getLLVMStyle(); 2613 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 2614 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 2615 verifyFormat("FOO_BEGIN\n" 2616 " FOO_ENTRY\n" 2617 "FOO_END", Style); 2618 verifyFormat("FOO_BEGIN\n" 2619 " NESTED_FOO_BEGIN\n" 2620 " NESTED_FOO_ENTRY\n" 2621 " NESTED_FOO_END\n" 2622 "FOO_END", Style); 2623 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 2624 " int x;\n" 2625 " x = 1;\n" 2626 "FOO_END(Baz)", Style); 2627 } 2628 2629 //===----------------------------------------------------------------------===// 2630 // Line break tests. 2631 //===----------------------------------------------------------------------===// 2632 2633 TEST_F(FormatTest, PreventConfusingIndents) { 2634 verifyFormat( 2635 "void f() {\n" 2636 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 2637 " parameter, parameter, parameter)),\n" 2638 " SecondLongCall(parameter));\n" 2639 "}"); 2640 verifyFormat( 2641 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2642 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 2643 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 2644 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 2645 verifyFormat( 2646 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2647 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 2648 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 2649 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 2650 verifyFormat( 2651 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 2652 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 2653 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 2654 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 2655 verifyFormat("int a = bbbb && ccc &&\n" 2656 " fffff(\n" 2657 "#define A Just forcing a new line\n" 2658 " ddd);"); 2659 } 2660 2661 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 2662 verifyFormat( 2663 "bool aaaaaaa =\n" 2664 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 2665 " bbbbbbbb();"); 2666 verifyFormat( 2667 "bool aaaaaaa =\n" 2668 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 2669 " bbbbbbbb();"); 2670 2671 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 2672 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 2673 " ccccccccc == ddddddddddd;"); 2674 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 2675 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 2676 " ccccccccc == ddddddddddd;"); 2677 verifyFormat( 2678 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 2679 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 2680 " ccccccccc == ddddddddddd;"); 2681 2682 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 2683 " aaaaaa) &&\n" 2684 " bbbbbb && cccccc;"); 2685 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 2686 " aaaaaa) >>\n" 2687 " bbbbbb;"); 2688 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 2689 " SourceMgr.getSpellingColumnNumber(\n" 2690 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 2691 " 1);"); 2692 2693 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2694 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 2695 " cccccc) {\n}"); 2696 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2697 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 2698 " cccccc) {\n}"); 2699 verifyFormat("b = a &&\n" 2700 " // Comment\n" 2701 " b.c && d;"); 2702 2703 // If the LHS of a comparison is not a binary expression itself, the 2704 // additional linebreak confuses many people. 2705 verifyFormat( 2706 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2707 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 2708 "}"); 2709 verifyFormat( 2710 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2711 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2712 "}"); 2713 verifyFormat( 2714 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 2715 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2716 "}"); 2717 // Even explicit parentheses stress the precedence enough to make the 2718 // additional break unnecessary. 2719 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2720 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 2721 "}"); 2722 // This cases is borderline, but with the indentation it is still readable. 2723 verifyFormat( 2724 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2725 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2726 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 2727 "}", 2728 getLLVMStyleWithColumns(75)); 2729 2730 // If the LHS is a binary expression, we should still use the additional break 2731 // as otherwise the formatting hides the operator precedence. 2732 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2733 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2734 " 5) {\n" 2735 "}"); 2736 2737 FormatStyle OnePerLine = getLLVMStyle(); 2738 OnePerLine.BinPackParameters = false; 2739 verifyFormat( 2740 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2741 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2742 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 2743 OnePerLine); 2744 2745 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 2746 " .aaa(aaaaaaaaaaaaa) *\n" 2747 " aaaaaaa +\n" 2748 " aaaaaaa;", 2749 getLLVMStyleWithColumns(40)); 2750 } 2751 2752 TEST_F(FormatTest, ExpressionIndentation) { 2753 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2754 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2755 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2756 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2757 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 2758 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 2759 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2760 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 2761 " ccccccccccccccccccccccccccccccccccccccccc;"); 2762 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2763 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2764 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2765 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2766 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2767 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2768 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2769 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2770 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 2771 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 2772 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2773 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 2774 verifyFormat("if () {\n" 2775 "} else if (aaaaa && bbbbb > // break\n" 2776 " ccccc) {\n" 2777 "}"); 2778 verifyFormat("if () {\n" 2779 "} else if (aaaaa &&\n" 2780 " bbbbb > // break\n" 2781 " ccccc &&\n" 2782 " ddddd) {\n" 2783 "}"); 2784 2785 // Presence of a trailing comment used to change indentation of b. 2786 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 2787 " b;\n" 2788 "return aaaaaaaaaaaaaaaaaaa +\n" 2789 " b; //", 2790 getLLVMStyleWithColumns(30)); 2791 } 2792 2793 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 2794 // Not sure what the best system is here. Like this, the LHS can be found 2795 // immediately above an operator (everything with the same or a higher 2796 // indent). The RHS is aligned right of the operator and so compasses 2797 // everything until something with the same indent as the operator is found. 2798 // FIXME: Is this a good system? 2799 FormatStyle Style = getLLVMStyle(); 2800 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 2801 verifyFormat( 2802 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2803 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2804 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2805 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2806 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2807 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2808 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2809 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2810 " > ccccccccccccccccccccccccccccccccccccccccc;", 2811 Style); 2812 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2813 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2814 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2815 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2816 Style); 2817 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2818 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2819 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2820 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2821 Style); 2822 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2823 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2824 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2825 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 2826 Style); 2827 verifyFormat("if () {\n" 2828 "} else if (aaaaa\n" 2829 " && bbbbb // break\n" 2830 " > ccccc) {\n" 2831 "}", 2832 Style); 2833 verifyFormat("return (a)\n" 2834 " // comment\n" 2835 " + b;", 2836 Style); 2837 verifyFormat( 2838 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2839 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2840 " + cc;", 2841 Style); 2842 2843 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2844 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 2845 Style); 2846 2847 // Forced by comments. 2848 verifyFormat( 2849 "unsigned ContentSize =\n" 2850 " sizeof(int16_t) // DWARF ARange version number\n" 2851 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 2852 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 2853 " + sizeof(int8_t); // Segment Size (in bytes)"); 2854 2855 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 2856 " == boost::fusion::at_c<1>(iiii).second;", 2857 Style); 2858 2859 Style.ColumnLimit = 60; 2860 verifyFormat("zzzzzzzzzz\n" 2861 " = bbbbbbbbbbbbbbbbb\n" 2862 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 2863 Style); 2864 } 2865 2866 TEST_F(FormatTest, EnforcedOperatorWraps) { 2867 // Here we'd like to wrap after the || operators, but a comment is forcing an 2868 // earlier wrap. 2869 verifyFormat("bool x = aaaaa //\n" 2870 " || bbbbb\n" 2871 " //\n" 2872 " || cccc;"); 2873 } 2874 2875 TEST_F(FormatTest, NoOperandAlignment) { 2876 FormatStyle Style = getLLVMStyle(); 2877 Style.AlignOperands = false; 2878 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 2879 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 2880 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 2881 Style); 2882 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 2883 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2884 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2885 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2886 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2887 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2888 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2889 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2890 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2891 " > ccccccccccccccccccccccccccccccccccccccccc;", 2892 Style); 2893 2894 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2895 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2896 " + cc;", 2897 Style); 2898 verifyFormat("int a = aa\n" 2899 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 2900 " * cccccccccccccccccccccccccccccccccccc;\n", 2901 Style); 2902 2903 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 2904 verifyFormat("return (a > b\n" 2905 " // comment1\n" 2906 " // comment2\n" 2907 " || c);", 2908 Style); 2909 } 2910 2911 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 2912 FormatStyle Style = getLLVMStyle(); 2913 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 2914 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 2916 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 2917 Style); 2918 } 2919 2920 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 2921 FormatStyle Style = getLLVMStyle(); 2922 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 2923 Style.BinPackArguments = false; 2924 Style.ColumnLimit = 40; 2925 verifyFormat("void test() {\n" 2926 " someFunction(\n" 2927 " this + argument + is + quite\n" 2928 " + long + so + it + gets + wrapped\n" 2929 " + but + remains + bin - packed);\n" 2930 "}", 2931 Style); 2932 verifyFormat("void test() {\n" 2933 " someFunction(arg1,\n" 2934 " this + argument + is\n" 2935 " + quite + long + so\n" 2936 " + it + gets + wrapped\n" 2937 " + but + remains + bin\n" 2938 " - packed,\n" 2939 " arg3);\n" 2940 "}", 2941 Style); 2942 verifyFormat("void test() {\n" 2943 " someFunction(\n" 2944 " arg1,\n" 2945 " this + argument + has\n" 2946 " + anotherFunc(nested,\n" 2947 " calls + whose\n" 2948 " + arguments\n" 2949 " + are + also\n" 2950 " + wrapped,\n" 2951 " in + addition)\n" 2952 " + to + being + bin - packed,\n" 2953 " arg3);\n" 2954 "}", 2955 Style); 2956 2957 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 2958 verifyFormat("void test() {\n" 2959 " someFunction(\n" 2960 " arg1,\n" 2961 " this + argument + has +\n" 2962 " anotherFunc(nested,\n" 2963 " calls + whose +\n" 2964 " arguments +\n" 2965 " are + also +\n" 2966 " wrapped,\n" 2967 " in + addition) +\n" 2968 " to + being + bin - packed,\n" 2969 " arg3);\n" 2970 "}", 2971 Style); 2972 } 2973 2974 TEST_F(FormatTest, ConstructorInitializers) { 2975 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 2976 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 2977 getLLVMStyleWithColumns(45)); 2978 verifyFormat("Constructor()\n" 2979 " : Inttializer(FitsOnTheLine) {}", 2980 getLLVMStyleWithColumns(44)); 2981 verifyFormat("Constructor()\n" 2982 " : Inttializer(FitsOnTheLine) {}", 2983 getLLVMStyleWithColumns(43)); 2984 2985 verifyFormat("template <typename T>\n" 2986 "Constructor() : Initializer(FitsOnTheLine) {}", 2987 getLLVMStyleWithColumns(45)); 2988 2989 verifyFormat( 2990 "SomeClass::Constructor()\n" 2991 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 2992 2993 verifyFormat( 2994 "SomeClass::Constructor()\n" 2995 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 2996 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 2997 verifyFormat( 2998 "SomeClass::Constructor()\n" 2999 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3000 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3001 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3002 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3003 " : aaaaaaaaaa(aaaaaa) {}"); 3004 3005 verifyFormat("Constructor()\n" 3006 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3007 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3008 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3009 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3010 3011 verifyFormat("Constructor()\n" 3012 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3013 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3014 3015 verifyFormat("Constructor(int Parameter = 0)\n" 3016 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3017 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3018 verifyFormat("Constructor()\n" 3019 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3020 "}", 3021 getLLVMStyleWithColumns(60)); 3022 verifyFormat("Constructor()\n" 3023 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3024 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3025 3026 // Here a line could be saved by splitting the second initializer onto two 3027 // lines, but that is not desirable. 3028 verifyFormat("Constructor()\n" 3029 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3030 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3031 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3032 3033 FormatStyle OnePerLine = getLLVMStyle(); 3034 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3035 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3036 verifyFormat("SomeClass::Constructor()\n" 3037 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3038 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3039 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3040 OnePerLine); 3041 verifyFormat("SomeClass::Constructor()\n" 3042 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3043 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3044 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3045 OnePerLine); 3046 verifyFormat("MyClass::MyClass(int var)\n" 3047 " : some_var_(var), // 4 space indent\n" 3048 " some_other_var_(var + 1) { // lined up\n" 3049 "}", 3050 OnePerLine); 3051 verifyFormat("Constructor()\n" 3052 " : aaaaa(aaaaaa),\n" 3053 " aaaaa(aaaaaa),\n" 3054 " aaaaa(aaaaaa),\n" 3055 " aaaaa(aaaaaa),\n" 3056 " aaaaa(aaaaaa) {}", 3057 OnePerLine); 3058 verifyFormat("Constructor()\n" 3059 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3060 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3061 OnePerLine); 3062 OnePerLine.BinPackParameters = false; 3063 verifyFormat( 3064 "Constructor()\n" 3065 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3066 " aaaaaaaaaaa().aaa(),\n" 3067 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3068 OnePerLine); 3069 OnePerLine.ColumnLimit = 60; 3070 verifyFormat("Constructor()\n" 3071 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 3072 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3073 OnePerLine); 3074 3075 EXPECT_EQ("Constructor()\n" 3076 " : // Comment forcing unwanted break.\n" 3077 " aaaa(aaaa) {}", 3078 format("Constructor() :\n" 3079 " // Comment forcing unwanted break.\n" 3080 " aaaa(aaaa) {}")); 3081 } 3082 3083 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 3084 FormatStyle Style = getLLVMStyle(); 3085 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 3086 3087 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3088 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 3089 getStyleWithColumns(Style, 45)); 3090 verifyFormat("Constructor() :\n" 3091 " Initializer(FitsOnTheLine) {}", 3092 getStyleWithColumns(Style, 44)); 3093 verifyFormat("Constructor() :\n" 3094 " Initializer(FitsOnTheLine) {}", 3095 getStyleWithColumns(Style, 43)); 3096 3097 verifyFormat("template <typename T>\n" 3098 "Constructor() : Initializer(FitsOnTheLine) {}", 3099 getStyleWithColumns(Style, 50)); 3100 3101 verifyFormat( 3102 "SomeClass::Constructor() :\n" 3103 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3104 Style); 3105 3106 verifyFormat( 3107 "SomeClass::Constructor() :\n" 3108 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3109 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3110 Style); 3111 verifyFormat( 3112 "SomeClass::Constructor() :\n" 3113 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3114 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 3115 Style); 3116 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3117 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 3118 " aaaaaaaaaa(aaaaaa) {}", 3119 Style); 3120 3121 verifyFormat("Constructor() :\n" 3122 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3123 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3124 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3125 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 3126 Style); 3127 3128 verifyFormat("Constructor() :\n" 3129 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3130 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3131 Style); 3132 3133 verifyFormat("Constructor(int Parameter = 0) :\n" 3134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3135 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 3136 Style); 3137 verifyFormat("Constructor() :\n" 3138 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3139 "}", 3140 getStyleWithColumns(Style, 60)); 3141 verifyFormat("Constructor() :\n" 3142 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3143 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 3144 Style); 3145 3146 // Here a line could be saved by splitting the second initializer onto two 3147 // lines, but that is not desirable. 3148 verifyFormat("Constructor() :\n" 3149 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3150 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3151 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3152 Style); 3153 3154 FormatStyle OnePerLine = Style; 3155 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3156 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 3157 verifyFormat("SomeClass::Constructor() :\n" 3158 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3159 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3160 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3161 OnePerLine); 3162 verifyFormat("SomeClass::Constructor() :\n" 3163 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3164 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3165 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3166 OnePerLine); 3167 verifyFormat("MyClass::MyClass(int var) :\n" 3168 " some_var_(var), // 4 space indent\n" 3169 " some_other_var_(var + 1) { // lined up\n" 3170 "}", 3171 OnePerLine); 3172 verifyFormat("Constructor() :\n" 3173 " aaaaa(aaaaaa),\n" 3174 " aaaaa(aaaaaa),\n" 3175 " aaaaa(aaaaaa),\n" 3176 " aaaaa(aaaaaa),\n" 3177 " aaaaa(aaaaaa) {}", 3178 OnePerLine); 3179 verifyFormat("Constructor() :\n" 3180 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3181 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3182 OnePerLine); 3183 OnePerLine.BinPackParameters = false; 3184 verifyFormat( 3185 "Constructor() :\n" 3186 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3187 " aaaaaaaaaaa().aaa(),\n" 3188 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3189 OnePerLine); 3190 OnePerLine.ColumnLimit = 60; 3191 verifyFormat("Constructor() :\n" 3192 " aaaaaaaaaaaaaaaaaaaa(a),\n" 3193 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 3194 OnePerLine); 3195 3196 EXPECT_EQ("Constructor() :\n" 3197 " // Comment forcing unwanted break.\n" 3198 " aaaa(aaaa) {}", 3199 format("Constructor() :\n" 3200 " // Comment forcing unwanted break.\n" 3201 " aaaa(aaaa) {}", 3202 Style)); 3203 3204 Style.ColumnLimit = 0; 3205 verifyFormat("SomeClass::Constructor() :\n" 3206 " a(a) {}", 3207 Style); 3208 verifyFormat("SomeClass::Constructor() noexcept :\n" 3209 " a(a) {}", 3210 Style); 3211 verifyFormat("SomeClass::Constructor() :\n" 3212 " a(a), b(b), c(c) {}", 3213 Style); 3214 verifyFormat("SomeClass::Constructor() :\n" 3215 " a(a) {\n" 3216 " foo();\n" 3217 " bar();\n" 3218 "}", 3219 Style); 3220 3221 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3222 verifyFormat("SomeClass::Constructor() :\n" 3223 " a(a), b(b), c(c) {\n" 3224 "}", 3225 Style); 3226 verifyFormat("SomeClass::Constructor() :\n" 3227 " a(a) {\n" 3228 "}", 3229 Style); 3230 3231 Style.ColumnLimit = 80; 3232 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3233 Style.ConstructorInitializerIndentWidth = 2; 3234 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", 3235 Style); 3236 verifyFormat("SomeClass::Constructor() :\n" 3237 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3238 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 3239 Style); 3240 } 3241 3242 TEST_F(FormatTest, MemoizationTests) { 3243 // This breaks if the memoization lookup does not take \c Indent and 3244 // \c LastSpace into account. 3245 verifyFormat( 3246 "extern CFRunLoopTimerRef\n" 3247 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3248 " CFTimeInterval interval, CFOptionFlags flags,\n" 3249 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3250 " CFRunLoopTimerContext *context) {}"); 3251 3252 // Deep nesting somewhat works around our memoization. 3253 verifyFormat( 3254 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3255 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3256 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3257 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3258 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3259 getLLVMStyleWithColumns(65)); 3260 verifyFormat( 3261 "aaaaa(\n" 3262 " aaaaa,\n" 3263 " aaaaa(\n" 3264 " aaaaa,\n" 3265 " aaaaa(\n" 3266 " aaaaa,\n" 3267 " aaaaa(\n" 3268 " aaaaa,\n" 3269 " aaaaa(\n" 3270 " aaaaa,\n" 3271 " aaaaa(\n" 3272 " aaaaa,\n" 3273 " aaaaa(\n" 3274 " aaaaa,\n" 3275 " aaaaa(\n" 3276 " aaaaa,\n" 3277 " aaaaa(\n" 3278 " aaaaa,\n" 3279 " aaaaa(\n" 3280 " aaaaa,\n" 3281 " aaaaa(\n" 3282 " aaaaa,\n" 3283 " aaaaa(\n" 3284 " aaaaa,\n" 3285 " aaaaa))))))))))));", 3286 getLLVMStyleWithColumns(65)); 3287 verifyFormat( 3288 "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" 3289 " a),\n" 3290 " a),\n" 3291 " a),\n" 3292 " a),\n" 3293 " a),\n" 3294 " a),\n" 3295 " a),\n" 3296 " a),\n" 3297 " a),\n" 3298 " a),\n" 3299 " a),\n" 3300 " a),\n" 3301 " a),\n" 3302 " a),\n" 3303 " a),\n" 3304 " a),\n" 3305 " a)", 3306 getLLVMStyleWithColumns(65)); 3307 3308 // This test takes VERY long when memoization is broken. 3309 FormatStyle OnePerLine = getLLVMStyle(); 3310 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3311 OnePerLine.BinPackParameters = false; 3312 std::string input = "Constructor()\n" 3313 " : aaaa(a,\n"; 3314 for (unsigned i = 0, e = 80; i != e; ++i) { 3315 input += " a,\n"; 3316 } 3317 input += " a) {}"; 3318 verifyFormat(input, OnePerLine); 3319 } 3320 3321 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3322 verifyFormat( 3323 "void f() {\n" 3324 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3325 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3326 " f();\n" 3327 "}"); 3328 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3329 " Intervals[i - 1].getRange().getLast()) {\n}"); 3330 } 3331 3332 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3333 // Principially, we break function declarations in a certain order: 3334 // 1) break amongst arguments. 3335 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3336 " Cccccccccccccc cccccccccccccc);"); 3337 verifyFormat("template <class TemplateIt>\n" 3338 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3339 " TemplateIt *stop) {}"); 3340 3341 // 2) break after return type. 3342 verifyFormat( 3343 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3344 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3345 getGoogleStyle()); 3346 3347 // 3) break after (. 3348 verifyFormat( 3349 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3350 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3351 getGoogleStyle()); 3352 3353 // 4) break before after nested name specifiers. 3354 verifyFormat( 3355 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3356 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3357 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3358 getGoogleStyle()); 3359 3360 // However, there are exceptions, if a sufficient amount of lines can be 3361 // saved. 3362 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3363 // more adjusting. 3364 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3365 " Cccccccccccccc cccccccccc,\n" 3366 " Cccccccccccccc cccccccccc,\n" 3367 " Cccccccccccccc cccccccccc,\n" 3368 " Cccccccccccccc cccccccccc);"); 3369 verifyFormat( 3370 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3371 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3372 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3373 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3374 getGoogleStyle()); 3375 verifyFormat( 3376 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3377 " Cccccccccccccc cccccccccc,\n" 3378 " Cccccccccccccc cccccccccc,\n" 3379 " Cccccccccccccc cccccccccc,\n" 3380 " Cccccccccccccc cccccccccc,\n" 3381 " Cccccccccccccc cccccccccc,\n" 3382 " Cccccccccccccc cccccccccc);"); 3383 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3384 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3385 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3386 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3387 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3388 3389 // Break after multi-line parameters. 3390 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3391 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3392 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3393 " bbbb bbbb);"); 3394 verifyFormat("void SomeLoooooooooooongFunction(\n" 3395 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3396 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3397 " int bbbbbbbbbbbbb);"); 3398 3399 // Treat overloaded operators like other functions. 3400 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3401 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3402 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3403 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3404 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3405 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3406 verifyGoogleFormat( 3407 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3408 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3409 verifyGoogleFormat( 3410 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3411 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3412 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3413 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3414 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3415 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3416 verifyGoogleFormat( 3417 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3418 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3419 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3420 verifyGoogleFormat( 3421 "template <typename T>\n" 3422 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3423 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 3424 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 3425 3426 FormatStyle Style = getLLVMStyle(); 3427 Style.PointerAlignment = FormatStyle::PAS_Left; 3428 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3429 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3430 Style); 3431 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3433 Style); 3434 } 3435 3436 TEST_F(FormatTest, TrailingReturnType) { 3437 verifyFormat("auto foo() -> int;\n"); 3438 verifyFormat("struct S {\n" 3439 " auto bar() const -> int;\n" 3440 "};"); 3441 verifyFormat("template <size_t Order, typename T>\n" 3442 "auto load_img(const std::string &filename)\n" 3443 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3444 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3445 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3446 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3447 verifyFormat("template <typename T>\n" 3448 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3449 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3450 3451 // Not trailing return types. 3452 verifyFormat("void f() { auto a = b->c(); }"); 3453 } 3454 3455 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3456 // Avoid breaking before trailing 'const' or other trailing annotations, if 3457 // they are not function-like. 3458 FormatStyle Style = getGoogleStyle(); 3459 Style.ColumnLimit = 47; 3460 verifyFormat("void someLongFunction(\n" 3461 " int someLoooooooooooooongParameter) const {\n}", 3462 getLLVMStyleWithColumns(47)); 3463 verifyFormat("LoooooongReturnType\n" 3464 "someLoooooooongFunction() const {}", 3465 getLLVMStyleWithColumns(47)); 3466 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 3467 " const {}", 3468 Style); 3469 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3470 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 3471 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3472 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 3473 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 3474 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 3475 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 3476 " aaaaaaaaaaa aaaaa) const override;"); 3477 verifyGoogleFormat( 3478 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3479 " const override;"); 3480 3481 // Even if the first parameter has to be wrapped. 3482 verifyFormat("void someLongFunction(\n" 3483 " int someLongParameter) const {}", 3484 getLLVMStyleWithColumns(46)); 3485 verifyFormat("void someLongFunction(\n" 3486 " int someLongParameter) const {}", 3487 Style); 3488 verifyFormat("void someLongFunction(\n" 3489 " int someLongParameter) override {}", 3490 Style); 3491 verifyFormat("void someLongFunction(\n" 3492 " int someLongParameter) OVERRIDE {}", 3493 Style); 3494 verifyFormat("void someLongFunction(\n" 3495 " int someLongParameter) final {}", 3496 Style); 3497 verifyFormat("void someLongFunction(\n" 3498 " int someLongParameter) FINAL {}", 3499 Style); 3500 verifyFormat("void someLongFunction(\n" 3501 " int parameter) const override {}", 3502 Style); 3503 3504 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3505 verifyFormat("void someLongFunction(\n" 3506 " int someLongParameter) const\n" 3507 "{\n" 3508 "}", 3509 Style); 3510 3511 // Unless these are unknown annotations. 3512 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 3513 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3514 " LONG_AND_UGLY_ANNOTATION;"); 3515 3516 // Breaking before function-like trailing annotations is fine to keep them 3517 // close to their arguments. 3518 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3519 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3520 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3521 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 3522 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 3523 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 3524 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 3525 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 3526 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 3527 3528 verifyFormat( 3529 "void aaaaaaaaaaaaaaaaaa()\n" 3530 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 3531 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 3532 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3533 " __attribute__((unused));"); 3534 verifyGoogleFormat( 3535 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3536 " GUARDED_BY(aaaaaaaaaaaa);"); 3537 verifyGoogleFormat( 3538 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3539 " GUARDED_BY(aaaaaaaaaaaa);"); 3540 verifyGoogleFormat( 3541 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3542 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3543 verifyGoogleFormat( 3544 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 3545 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3546 } 3547 3548 TEST_F(FormatTest, FunctionAnnotations) { 3549 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3550 "int OldFunction(const string ¶meter) {}"); 3551 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3552 "string OldFunction(const string ¶meter) {}"); 3553 verifyFormat("template <typename T>\n" 3554 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 3555 "string OldFunction(const string ¶meter) {}"); 3556 3557 // Not function annotations. 3558 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3559 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 3560 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 3561 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 3562 verifyFormat("MACRO(abc).function() // wrap\n" 3563 " << abc;"); 3564 verifyFormat("MACRO(abc)->function() // wrap\n" 3565 " << abc;"); 3566 verifyFormat("MACRO(abc)::function() // wrap\n" 3567 " << abc;"); 3568 } 3569 3570 TEST_F(FormatTest, BreaksDesireably) { 3571 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3572 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 3573 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 3574 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3575 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 3576 "}"); 3577 3578 verifyFormat( 3579 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3581 3582 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3584 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3585 3586 verifyFormat( 3587 "aaaaaaaa(aaaaaaaaaaaaa,\n" 3588 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3589 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3590 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3591 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 3592 3593 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3594 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3595 3596 verifyFormat( 3597 "void f() {\n" 3598 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 3599 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3600 "}"); 3601 verifyFormat( 3602 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3603 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3604 verifyFormat( 3605 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3606 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 3607 verifyFormat( 3608 "aaaaaa(aaa,\n" 3609 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3610 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3611 " aaaa);"); 3612 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3613 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3614 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3615 3616 // Indent consistently independent of call expression and unary operator. 3617 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3618 " dddddddddddddddddddddddddddddd));"); 3619 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3620 " dddddddddddddddddddddddddddddd));"); 3621 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 3622 " dddddddddddddddddddddddddddddd));"); 3623 3624 // This test case breaks on an incorrect memoization, i.e. an optimization not 3625 // taking into account the StopAt value. 3626 verifyFormat( 3627 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3628 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3629 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 3630 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3631 3632 verifyFormat("{\n {\n {\n" 3633 " Annotation.SpaceRequiredBefore =\n" 3634 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 3635 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 3636 " }\n }\n}"); 3637 3638 // Break on an outer level if there was a break on an inner level. 3639 EXPECT_EQ("f(g(h(a, // comment\n" 3640 " b, c),\n" 3641 " d, e),\n" 3642 " x, y);", 3643 format("f(g(h(a, // comment\n" 3644 " b, c), d, e), x, y);")); 3645 3646 // Prefer breaking similar line breaks. 3647 verifyFormat( 3648 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 3649 " NSTrackingMouseEnteredAndExited |\n" 3650 " NSTrackingActiveAlways;"); 3651 } 3652 3653 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 3654 FormatStyle NoBinPacking = getGoogleStyle(); 3655 NoBinPacking.BinPackParameters = false; 3656 NoBinPacking.BinPackArguments = true; 3657 verifyFormat("void f() {\n" 3658 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 3659 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 3660 "}", 3661 NoBinPacking); 3662 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 3663 " int aaaaaaaaaaaaaaaaaaaa,\n" 3664 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3665 NoBinPacking); 3666 3667 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 3668 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3669 " vector<int> bbbbbbbbbbbbbbb);", 3670 NoBinPacking); 3671 // FIXME: This behavior difference is probably not wanted. However, currently 3672 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 3673 // template arguments from BreakBeforeParameter being set because of the 3674 // one-per-line formatting. 3675 verifyFormat( 3676 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 3677 " aaaaaaaaaa> aaaaaaaaaa);", 3678 NoBinPacking); 3679 verifyFormat( 3680 "void fffffffffff(\n" 3681 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 3682 " aaaaaaaaaa);"); 3683 } 3684 3685 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 3686 FormatStyle NoBinPacking = getGoogleStyle(); 3687 NoBinPacking.BinPackParameters = false; 3688 NoBinPacking.BinPackArguments = false; 3689 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 3690 " aaaaaaaaaaaaaaaaaaaa,\n" 3691 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 3692 NoBinPacking); 3693 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 3694 " aaaaaaaaaaaaa,\n" 3695 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 3696 NoBinPacking); 3697 verifyFormat( 3698 "aaaaaaaa(aaaaaaaaaaaaa,\n" 3699 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3700 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 3701 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3702 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 3703 NoBinPacking); 3704 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 3705 " .aaaaaaaaaaaaaaaaaa();", 3706 NoBinPacking); 3707 verifyFormat("void f() {\n" 3708 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3709 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 3710 "}", 3711 NoBinPacking); 3712 3713 verifyFormat( 3714 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3715 " aaaaaaaaaaaa,\n" 3716 " aaaaaaaaaaaa);", 3717 NoBinPacking); 3718 verifyFormat( 3719 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 3720 " ddddddddddddddddddddddddddddd),\n" 3721 " test);", 3722 NoBinPacking); 3723 3724 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 3725 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 3726 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 3727 " aaaaaaaaaaaaaaaaaa;", 3728 NoBinPacking); 3729 verifyFormat("a(\"a\"\n" 3730 " \"a\",\n" 3731 " a);"); 3732 3733 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 3734 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 3735 " aaaaaaaaa,\n" 3736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3737 NoBinPacking); 3738 verifyFormat( 3739 "void f() {\n" 3740 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 3741 " .aaaaaaa();\n" 3742 "}", 3743 NoBinPacking); 3744 verifyFormat( 3745 "template <class SomeType, class SomeOtherType>\n" 3746 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 3747 NoBinPacking); 3748 } 3749 3750 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 3751 FormatStyle Style = getLLVMStyleWithColumns(15); 3752 Style.ExperimentalAutoDetectBinPacking = true; 3753 EXPECT_EQ("aaa(aaaa,\n" 3754 " aaaa,\n" 3755 " aaaa);\n" 3756 "aaa(aaaa,\n" 3757 " aaaa,\n" 3758 " aaaa);", 3759 format("aaa(aaaa,\n" // one-per-line 3760 " aaaa,\n" 3761 " aaaa );\n" 3762 "aaa(aaaa, aaaa, aaaa);", // inconclusive 3763 Style)); 3764 EXPECT_EQ("aaa(aaaa, aaaa,\n" 3765 " aaaa);\n" 3766 "aaa(aaaa, aaaa,\n" 3767 " aaaa);", 3768 format("aaa(aaaa, aaaa,\n" // bin-packed 3769 " aaaa );\n" 3770 "aaa(aaaa, aaaa, aaaa);", // inconclusive 3771 Style)); 3772 } 3773 3774 TEST_F(FormatTest, FormatsBuilderPattern) { 3775 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 3776 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 3777 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 3778 " .StartsWith(\".init\", ORDER_INIT)\n" 3779 " .StartsWith(\".fini\", ORDER_FINI)\n" 3780 " .StartsWith(\".hash\", ORDER_HASH)\n" 3781 " .Default(ORDER_TEXT);\n"); 3782 3783 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 3784 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 3785 verifyFormat( 3786 "aaaaaaa->aaaaaaa\n" 3787 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3788 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3789 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 3790 verifyFormat( 3791 "aaaaaaa->aaaaaaa\n" 3792 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3793 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 3794 verifyFormat( 3795 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 3796 " aaaaaaaaaaaaaa);"); 3797 verifyFormat( 3798 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 3799 " aaaaaa->aaaaaaaaaaaa()\n" 3800 " ->aaaaaaaaaaaaaaaa(\n" 3801 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3802 " ->aaaaaaaaaaaaaaaaa();"); 3803 verifyGoogleFormat( 3804 "void f() {\n" 3805 " someo->Add((new util::filetools::Handler(dir))\n" 3806 " ->OnEvent1(NewPermanentCallback(\n" 3807 " this, &HandlerHolderClass::EventHandlerCBA))\n" 3808 " ->OnEvent2(NewPermanentCallback(\n" 3809 " this, &HandlerHolderClass::EventHandlerCBB))\n" 3810 " ->OnEvent3(NewPermanentCallback(\n" 3811 " this, &HandlerHolderClass::EventHandlerCBC))\n" 3812 " ->OnEvent5(NewPermanentCallback(\n" 3813 " this, &HandlerHolderClass::EventHandlerCBD))\n" 3814 " ->OnEvent6(NewPermanentCallback(\n" 3815 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 3816 "}"); 3817 3818 verifyFormat( 3819 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 3820 verifyFormat("aaaaaaaaaaaaaaa()\n" 3821 " .aaaaaaaaaaaaaaa()\n" 3822 " .aaaaaaaaaaaaaaa()\n" 3823 " .aaaaaaaaaaaaaaa()\n" 3824 " .aaaaaaaaaaaaaaa();"); 3825 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3826 " .aaaaaaaaaaaaaaa()\n" 3827 " .aaaaaaaaaaaaaaa()\n" 3828 " .aaaaaaaaaaaaaaa();"); 3829 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3830 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 3831 " .aaaaaaaaaaaaaaa();"); 3832 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 3833 " ->aaaaaaaaaaaaaae(0)\n" 3834 " ->aaaaaaaaaaaaaaa();"); 3835 3836 // Don't linewrap after very short segments. 3837 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3838 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3839 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3840 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3841 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3842 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3843 verifyFormat("aaa()\n" 3844 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3845 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3846 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 3847 3848 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 3849 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 3850 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 3851 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 3852 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 3854 3855 // Prefer not to break after empty parentheses. 3856 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 3857 " First->LastNewlineOffset);"); 3858 3859 // Prefer not to create "hanging" indents. 3860 verifyFormat( 3861 "return !soooooooooooooome_map\n" 3862 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3863 " .second;"); 3864 verifyFormat( 3865 "return aaaaaaaaaaaaaaaa\n" 3866 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 3867 " .aaaa(aaaaaaaaaaaaaa);"); 3868 // No hanging indent here. 3869 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 3870 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3871 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 3872 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3873 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 3874 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3875 getLLVMStyleWithColumns(60)); 3876 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 3877 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 3878 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 3879 getLLVMStyleWithColumns(59)); 3880 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3881 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 3882 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3883 } 3884 3885 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 3886 verifyFormat( 3887 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3888 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 3889 verifyFormat( 3890 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 3891 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 3892 3893 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 3894 " ccccccccccccccccccccccccc) {\n}"); 3895 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 3896 " ccccccccccccccccccccccccc) {\n}"); 3897 3898 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 3899 " ccccccccccccccccccccccccc) {\n}"); 3900 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 3901 " ccccccccccccccccccccccccc) {\n}"); 3902 3903 verifyFormat( 3904 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 3905 " ccccccccccccccccccccccccc) {\n}"); 3906 verifyFormat( 3907 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 3908 " ccccccccccccccccccccccccc) {\n}"); 3909 3910 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 3911 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 3912 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 3913 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 3914 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 3915 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 3916 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 3917 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 3918 3919 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 3920 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 3921 " aaaaaaaaaaaaaaa != aa) {\n}"); 3922 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 3923 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 3924 " aaaaaaaaaaaaaaa != aa) {\n}"); 3925 } 3926 3927 TEST_F(FormatTest, BreaksAfterAssignments) { 3928 verifyFormat( 3929 "unsigned Cost =\n" 3930 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 3931 " SI->getPointerAddressSpaceee());\n"); 3932 verifyFormat( 3933 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 3934 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 3935 3936 verifyFormat( 3937 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 3938 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 3939 verifyFormat("unsigned OriginalStartColumn =\n" 3940 " SourceMgr.getSpellingColumnNumber(\n" 3941 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 3942 " 1;"); 3943 } 3944 3945 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 3946 FormatStyle Style = getLLVMStyle(); 3947 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3948 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 3949 Style); 3950 3951 Style.PenaltyBreakAssignment = 20; 3952 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3953 " cccccccccccccccccccccccccc;", 3954 Style); 3955 } 3956 3957 TEST_F(FormatTest, AlignsAfterAssignments) { 3958 verifyFormat( 3959 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3960 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3961 verifyFormat( 3962 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3963 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3964 verifyFormat( 3965 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3966 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3967 verifyFormat( 3968 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3969 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 3970 verifyFormat( 3971 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 3972 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 3973 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 3974 } 3975 3976 TEST_F(FormatTest, AlignsAfterReturn) { 3977 verifyFormat( 3978 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3979 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 3980 verifyFormat( 3981 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3982 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 3983 verifyFormat( 3984 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 3985 " aaaaaaaaaaaaaaaaaaaaaa();"); 3986 verifyFormat( 3987 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 3988 " aaaaaaaaaaaaaaaaaaaaaa());"); 3989 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3990 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 3991 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3992 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 3993 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 3994 verifyFormat("return\n" 3995 " // true if code is one of a or b.\n" 3996 " code == a || code == b;"); 3997 } 3998 3999 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4000 verifyFormat( 4001 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4002 " aaaaaaaaa aaaaaaa) {}"); 4003 verifyFormat( 4004 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4005 " aaaaaaaaaaa aaaaaaaaa);"); 4006 verifyFormat( 4007 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4008 " aaaaaaaaaaaaaaaaaaaaa));"); 4009 FormatStyle Style = getLLVMStyle(); 4010 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4011 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4012 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4013 Style); 4014 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4015 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4016 Style); 4017 verifyFormat("SomeLongVariableName->someFunction(\n" 4018 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4019 Style); 4020 verifyFormat( 4021 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4022 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4023 Style); 4024 verifyFormat( 4025 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4026 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4027 Style); 4028 verifyFormat( 4029 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4030 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4031 Style); 4032 4033 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 4034 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 4035 " b));", 4036 Style); 4037 4038 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 4039 Style.BinPackArguments = false; 4040 Style.BinPackParameters = false; 4041 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4042 " aaaaaaaaaaa aaaaaaaa,\n" 4043 " aaaaaaaaa aaaaaaa,\n" 4044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4045 Style); 4046 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4047 " aaaaaaaaaaa aaaaaaaaa,\n" 4048 " aaaaaaaaaaa aaaaaaaaa,\n" 4049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4050 Style); 4051 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 4052 " aaaaaaaaaaaaaaa,\n" 4053 " aaaaaaaaaaaaaaaaaaaaa,\n" 4054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4055 Style); 4056 verifyFormat( 4057 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 4058 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4059 Style); 4060 verifyFormat( 4061 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 4062 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 4063 Style); 4064 verifyFormat( 4065 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4066 " aaaaaaaaaaaaaaaaaaaaa(\n" 4067 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 4068 " aaaaaaaaaaaaaaaa);", 4069 Style); 4070 verifyFormat( 4071 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4072 " aaaaaaaaaaaaaaaaaaaaa(\n" 4073 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 4074 " aaaaaaaaaaaaaaaa);", 4075 Style); 4076 } 4077 4078 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4079 FormatStyle Style = getLLVMStyleWithColumns(40); 4080 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4081 " bbbbbbbbbbbbbbbbbbbbbb);", 4082 Style); 4083 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 4084 Style.AlignOperands = false; 4085 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4086 " bbbbbbbbbbbbbbbbbbbbbb);", 4087 Style); 4088 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4089 Style.AlignOperands = true; 4090 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4091 " bbbbbbbbbbbbbbbbbbbbbb);", 4092 Style); 4093 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4094 Style.AlignOperands = false; 4095 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4096 " bbbbbbbbbbbbbbbbbbbbbb);", 4097 Style); 4098 } 4099 4100 TEST_F(FormatTest, BreaksConditionalExpressions) { 4101 verifyFormat( 4102 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4103 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4104 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4105 verifyFormat( 4106 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4107 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4108 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4109 verifyFormat( 4110 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4111 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4112 verifyFormat( 4113 "aaaa(aaaaaaaaa, aaaaaaaaa,\n" 4114 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4115 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4116 verifyFormat( 4117 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4118 " : aaaaaaaaaaaaa);"); 4119 verifyFormat( 4120 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4121 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4122 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4123 " aaaaaaaaaaaaa);"); 4124 verifyFormat( 4125 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4126 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4127 " aaaaaaaaaaaaa);"); 4128 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4129 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4130 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4131 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4132 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4133 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4135 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4136 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4137 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4138 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4139 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4140 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4141 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4142 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4143 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4144 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4145 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4146 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4147 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4148 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4149 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4150 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4151 " : aaaaaaaaaaaaaaaa;"); 4152 verifyFormat( 4153 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4154 " ? aaaaaaaaaaaaaaa\n" 4155 " : aaaaaaaaaaaaaaa;"); 4156 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4157 " aaaaaaaaa\n" 4158 " ? b\n" 4159 " : c);"); 4160 verifyFormat("return aaaa == bbbb\n" 4161 " // comment\n" 4162 " ? aaaa\n" 4163 " : bbbb;"); 4164 verifyFormat("unsigned Indent =\n" 4165 " format(TheLine.First,\n" 4166 " IndentForLevel[TheLine.Level] >= 0\n" 4167 " ? IndentForLevel[TheLine.Level]\n" 4168 " : TheLine * 2,\n" 4169 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4170 getLLVMStyleWithColumns(60)); 4171 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4172 " ? aaaaaaaaaaaaaaa\n" 4173 " : bbbbbbbbbbbbbbb //\n" 4174 " ? ccccccccccccccc\n" 4175 " : ddddddddddddddd;"); 4176 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4177 " ? aaaaaaaaaaaaaaa\n" 4178 " : (bbbbbbbbbbbbbbb //\n" 4179 " ? ccccccccccccccc\n" 4180 " : ddddddddddddddd);"); 4181 verifyFormat( 4182 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4183 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4184 " aaaaaaaaaaaaaaaaaaaaa +\n" 4185 " aaaaaaaaaaaaaaaaaaaaa\n" 4186 " : aaaaaaaaaa;"); 4187 verifyFormat( 4188 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4189 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4190 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4191 4192 FormatStyle NoBinPacking = getLLVMStyle(); 4193 NoBinPacking.BinPackArguments = false; 4194 verifyFormat( 4195 "void f() {\n" 4196 " g(aaa,\n" 4197 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4198 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4199 " ? aaaaaaaaaaaaaaa\n" 4200 " : aaaaaaaaaaaaaaa);\n" 4201 "}", 4202 NoBinPacking); 4203 verifyFormat( 4204 "void f() {\n" 4205 " g(aaa,\n" 4206 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4207 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4208 " ?: aaaaaaaaaaaaaaa);\n" 4209 "}", 4210 NoBinPacking); 4211 4212 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4213 " // comment.\n" 4214 " ccccccccccccccccccccccccccccccccccccccc\n" 4215 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4216 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4217 4218 // Assignments in conditional expressions. Apparently not uncommon :-(. 4219 verifyFormat("return a != b\n" 4220 " // comment\n" 4221 " ? a = b\n" 4222 " : a = b;"); 4223 verifyFormat("return a != b\n" 4224 " // comment\n" 4225 " ? a = a != b\n" 4226 " // comment\n" 4227 " ? a = b\n" 4228 " : a\n" 4229 " : a;\n"); 4230 verifyFormat("return a != b\n" 4231 " // comment\n" 4232 " ? a\n" 4233 " : a = a != b\n" 4234 " // comment\n" 4235 " ? a = b\n" 4236 " : a;"); 4237 } 4238 4239 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4240 FormatStyle Style = getLLVMStyle(); 4241 Style.BreakBeforeTernaryOperators = false; 4242 Style.ColumnLimit = 70; 4243 verifyFormat( 4244 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4245 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4246 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4247 Style); 4248 verifyFormat( 4249 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 4250 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4252 Style); 4253 verifyFormat( 4254 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4255 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4256 Style); 4257 verifyFormat( 4258 "aaaa(aaaaaaaa, aaaaaaaaaa,\n" 4259 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4260 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4261 Style); 4262 verifyFormat( 4263 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4264 " aaaaaaaaaaaaa);", 4265 Style); 4266 verifyFormat( 4267 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4268 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4270 " aaaaaaaaaaaaa);", 4271 Style); 4272 verifyFormat( 4273 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4274 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4275 " aaaaaaaaaaaaa);", 4276 Style); 4277 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4278 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4279 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4280 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4281 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4282 Style); 4283 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4284 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4285 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4286 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4287 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4288 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4289 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4290 Style); 4291 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4292 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4293 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4294 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4295 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4296 Style); 4297 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4298 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4299 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4300 Style); 4301 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4302 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4303 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4304 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4305 Style); 4306 verifyFormat( 4307 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4308 " aaaaaaaaaaaaaaa :\n" 4309 " aaaaaaaaaaaaaaa;", 4310 Style); 4311 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4312 " aaaaaaaaa ?\n" 4313 " b :\n" 4314 " c);", 4315 Style); 4316 verifyFormat("unsigned Indent =\n" 4317 " format(TheLine.First,\n" 4318 " IndentForLevel[TheLine.Level] >= 0 ?\n" 4319 " IndentForLevel[TheLine.Level] :\n" 4320 " TheLine * 2,\n" 4321 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4322 Style); 4323 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4324 " aaaaaaaaaaaaaaa :\n" 4325 " bbbbbbbbbbbbbbb ? //\n" 4326 " ccccccccccccccc :\n" 4327 " ddddddddddddddd;", 4328 Style); 4329 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4330 " aaaaaaaaaaaaaaa :\n" 4331 " (bbbbbbbbbbbbbbb ? //\n" 4332 " ccccccccccccccc :\n" 4333 " ddddddddddddddd);", 4334 Style); 4335 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4336 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 4337 " ccccccccccccccccccccccccccc;", 4338 Style); 4339 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4340 " aaaaa :\n" 4341 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 4342 Style); 4343 } 4344 4345 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4346 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4347 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4348 verifyFormat("bool a = true, b = false;"); 4349 4350 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4351 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4352 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4353 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4354 verifyFormat( 4355 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4356 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4357 " d = e && f;"); 4358 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4359 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4360 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4361 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4362 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4363 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4364 4365 FormatStyle Style = getGoogleStyle(); 4366 Style.PointerAlignment = FormatStyle::PAS_Left; 4367 Style.DerivePointerAlignment = false; 4368 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4369 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4370 " *b = bbbbbbbbbbbbbbbbbbb;", 4371 Style); 4372 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4373 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4374 Style); 4375 verifyFormat("vector<int*> a, b;", Style); 4376 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 4377 } 4378 4379 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4380 verifyFormat("arr[foo ? bar : baz];"); 4381 verifyFormat("f()[foo ? bar : baz];"); 4382 verifyFormat("(a + b)[foo ? bar : baz];"); 4383 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4384 } 4385 4386 TEST_F(FormatTest, AlignsStringLiterals) { 4387 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4388 " \"short literal\");"); 4389 verifyFormat( 4390 "looooooooooooooooooooooooongFunction(\n" 4391 " \"short literal\"\n" 4392 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4393 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4394 " \" string literals\",\n" 4395 " and, other, parameters);"); 4396 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4397 " \"5678\";", 4398 format("fun + \"1243\" /* comment */\n" 4399 " \"5678\";", 4400 getLLVMStyleWithColumns(28))); 4401 EXPECT_EQ( 4402 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4403 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4404 " \"aaaaaaaaaaaaaaaa\";", 4405 format("aaaaaa =" 4406 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4407 "aaaaaaaaaaaaaaaaaaaaa\" " 4408 "\"aaaaaaaaaaaaaaaa\";")); 4409 verifyFormat("a = a + \"a\"\n" 4410 " \"a\"\n" 4411 " \"a\";"); 4412 verifyFormat("f(\"a\", \"b\"\n" 4413 " \"c\");"); 4414 4415 verifyFormat( 4416 "#define LL_FORMAT \"ll\"\n" 4417 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4418 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4419 4420 verifyFormat("#define A(X) \\\n" 4421 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4422 " \"ccccc\"", 4423 getLLVMStyleWithColumns(23)); 4424 verifyFormat("#define A \"def\"\n" 4425 "f(\"abc\" A \"ghi\"\n" 4426 " \"jkl\");"); 4427 4428 verifyFormat("f(L\"a\"\n" 4429 " L\"b\");"); 4430 verifyFormat("#define A(X) \\\n" 4431 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4432 " L\"ccccc\"", 4433 getLLVMStyleWithColumns(25)); 4434 4435 verifyFormat("f(@\"a\"\n" 4436 " @\"b\");"); 4437 verifyFormat("NSString s = @\"a\"\n" 4438 " @\"b\"\n" 4439 " @\"c\";"); 4440 verifyFormat("NSString s = @\"a\"\n" 4441 " \"b\"\n" 4442 " \"c\";"); 4443 } 4444 4445 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 4446 FormatStyle Style = getLLVMStyle(); 4447 // No declarations or definitions should be moved to own line. 4448 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 4449 verifyFormat("class A {\n" 4450 " int f() { return 1; }\n" 4451 " int g();\n" 4452 "};\n" 4453 "int f() { return 1; }\n" 4454 "int g();\n", 4455 Style); 4456 4457 // All declarations and definitions should have the return type moved to its 4458 // own 4459 // line. 4460 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 4461 verifyFormat("class E {\n" 4462 " int\n" 4463 " f() {\n" 4464 " return 1;\n" 4465 " }\n" 4466 " int\n" 4467 " g();\n" 4468 "};\n" 4469 "int\n" 4470 "f() {\n" 4471 " return 1;\n" 4472 "}\n" 4473 "int\n" 4474 "g();\n", 4475 Style); 4476 4477 // Top-level definitions, and no kinds of declarations should have the 4478 // return type moved to its own line. 4479 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 4480 verifyFormat("class B {\n" 4481 " int f() { return 1; }\n" 4482 " int g();\n" 4483 "};\n" 4484 "int\n" 4485 "f() {\n" 4486 " return 1;\n" 4487 "}\n" 4488 "int g();\n", 4489 Style); 4490 4491 // Top-level definitions and declarations should have the return type moved 4492 // to its own line. 4493 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 4494 verifyFormat("class C {\n" 4495 " int f() { return 1; }\n" 4496 " int g();\n" 4497 "};\n" 4498 "int\n" 4499 "f() {\n" 4500 " return 1;\n" 4501 "}\n" 4502 "int\n" 4503 "g();\n", 4504 Style); 4505 4506 // All definitions should have the return type moved to its own line, but no 4507 // kinds of declarations. 4508 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 4509 verifyFormat("class D {\n" 4510 " int\n" 4511 " f() {\n" 4512 " return 1;\n" 4513 " }\n" 4514 " int g();\n" 4515 "};\n" 4516 "int\n" 4517 "f() {\n" 4518 " return 1;\n" 4519 "}\n" 4520 "int g();\n", 4521 Style); 4522 verifyFormat("const char *\n" 4523 "f(void) {\n" // Break here. 4524 " return \"\";\n" 4525 "}\n" 4526 "const char *bar(void);\n", // No break here. 4527 Style); 4528 verifyFormat("template <class T>\n" 4529 "T *\n" 4530 "f(T &c) {\n" // Break here. 4531 " return NULL;\n" 4532 "}\n" 4533 "template <class T> T *f(T &c);\n", // No break here. 4534 Style); 4535 verifyFormat("class C {\n" 4536 " int\n" 4537 " operator+() {\n" 4538 " return 1;\n" 4539 " }\n" 4540 " int\n" 4541 " operator()() {\n" 4542 " return 1;\n" 4543 " }\n" 4544 "};\n", 4545 Style); 4546 verifyFormat("void\n" 4547 "A::operator()() {}\n" 4548 "void\n" 4549 "A::operator>>() {}\n" 4550 "void\n" 4551 "A::operator+() {}\n", 4552 Style); 4553 verifyFormat("void *operator new(std::size_t s);", // No break here. 4554 Style); 4555 verifyFormat("void *\n" 4556 "operator new(std::size_t s) {}", 4557 Style); 4558 verifyFormat("void *\n" 4559 "operator delete[](void *ptr) {}", 4560 Style); 4561 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4562 verifyFormat("const char *\n" 4563 "f(void)\n" // Break here. 4564 "{\n" 4565 " return \"\";\n" 4566 "}\n" 4567 "const char *bar(void);\n", // No break here. 4568 Style); 4569 verifyFormat("template <class T>\n" 4570 "T *\n" // Problem here: no line break 4571 "f(T &c)\n" // Break here. 4572 "{\n" 4573 " return NULL;\n" 4574 "}\n" 4575 "template <class T> T *f(T &c);\n", // No break here. 4576 Style); 4577 } 4578 4579 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 4580 FormatStyle NoBreak = getLLVMStyle(); 4581 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 4582 FormatStyle Break = getLLVMStyle(); 4583 Break.AlwaysBreakBeforeMultilineStrings = true; 4584 verifyFormat("aaaa = \"bbbb\"\n" 4585 " \"cccc\";", 4586 NoBreak); 4587 verifyFormat("aaaa =\n" 4588 " \"bbbb\"\n" 4589 " \"cccc\";", 4590 Break); 4591 verifyFormat("aaaa(\"bbbb\"\n" 4592 " \"cccc\");", 4593 NoBreak); 4594 verifyFormat("aaaa(\n" 4595 " \"bbbb\"\n" 4596 " \"cccc\");", 4597 Break); 4598 verifyFormat("aaaa(qqq, \"bbbb\"\n" 4599 " \"cccc\");", 4600 NoBreak); 4601 verifyFormat("aaaa(qqq,\n" 4602 " \"bbbb\"\n" 4603 " \"cccc\");", 4604 Break); 4605 verifyFormat("aaaa(qqq,\n" 4606 " L\"bbbb\"\n" 4607 " L\"cccc\");", 4608 Break); 4609 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 4610 " \"bbbb\"));", 4611 Break); 4612 verifyFormat("string s = someFunction(\n" 4613 " \"abc\"\n" 4614 " \"abc\");", 4615 Break); 4616 4617 // As we break before unary operators, breaking right after them is bad. 4618 verifyFormat("string foo = abc ? \"x\"\n" 4619 " \"blah blah blah blah blah blah\"\n" 4620 " : \"y\";", 4621 Break); 4622 4623 // Don't break if there is no column gain. 4624 verifyFormat("f(\"aaaa\"\n" 4625 " \"bbbb\");", 4626 Break); 4627 4628 // Treat literals with escaped newlines like multi-line string literals. 4629 EXPECT_EQ("x = \"a\\\n" 4630 "b\\\n" 4631 "c\";", 4632 format("x = \"a\\\n" 4633 "b\\\n" 4634 "c\";", 4635 NoBreak)); 4636 EXPECT_EQ("xxxx =\n" 4637 " \"a\\\n" 4638 "b\\\n" 4639 "c\";", 4640 format("xxxx = \"a\\\n" 4641 "b\\\n" 4642 "c\";", 4643 Break)); 4644 4645 EXPECT_EQ("NSString *const kString =\n" 4646 " @\"aaaa\"\n" 4647 " @\"bbbb\";", 4648 format("NSString *const kString = @\"aaaa\"\n" 4649 "@\"bbbb\";", 4650 Break)); 4651 4652 Break.ColumnLimit = 0; 4653 verifyFormat("const char *hello = \"hello llvm\";", Break); 4654 } 4655 4656 TEST_F(FormatTest, AlignsPipes) { 4657 verifyFormat( 4658 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4659 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4660 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4661 verifyFormat( 4662 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 4663 " << aaaaaaaaaaaaaaaaaaaa;"); 4664 verifyFormat( 4665 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4666 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4667 verifyFormat( 4668 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4669 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4670 verifyFormat( 4671 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 4672 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 4673 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 4674 verifyFormat( 4675 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4676 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4677 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4678 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4679 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4681 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4682 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 4683 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 4684 verifyFormat( 4685 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4686 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4687 verifyFormat( 4688 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 4689 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4690 4691 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 4692 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 4693 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4694 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4695 " aaaaaaaaaaaaaaaaaaaaa)\n" 4696 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4697 verifyFormat("LOG_IF(aaa == //\n" 4698 " bbb)\n" 4699 " << a << b;"); 4700 4701 // But sometimes, breaking before the first "<<" is desirable. 4702 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4703 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 4704 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 4705 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4706 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4707 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 4708 " << BEF << IsTemplate << Description << E->getType();"); 4709 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4710 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4711 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4712 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 4713 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4715 " << aaa;"); 4716 4717 verifyFormat( 4718 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4719 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4720 4721 // Incomplete string literal. 4722 EXPECT_EQ("llvm::errs() << \"\n" 4723 " << a;", 4724 format("llvm::errs() << \"\n<<a;")); 4725 4726 verifyFormat("void f() {\n" 4727 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 4728 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 4729 "}"); 4730 4731 // Handle 'endl'. 4732 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 4733 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 4734 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 4735 4736 // Handle '\n'. 4737 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 4738 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 4739 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 4740 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 4741 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 4742 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 4743 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 4744 } 4745 4746 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 4747 verifyFormat("return out << \"somepacket = {\\n\"\n" 4748 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 4749 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 4750 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 4751 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 4752 " << \"}\";"); 4753 4754 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4755 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4756 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 4757 verifyFormat( 4758 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 4759 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 4760 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 4761 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 4762 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 4763 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 4764 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4765 verifyFormat( 4766 "void f() {\n" 4767 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 4768 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4769 "}"); 4770 4771 // Breaking before the first "<<" is generally not desirable. 4772 verifyFormat( 4773 "llvm::errs()\n" 4774 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4775 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4776 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4777 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4778 getLLVMStyleWithColumns(70)); 4779 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4780 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4781 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4782 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4783 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 4784 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4785 getLLVMStyleWithColumns(70)); 4786 4787 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 4788 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 4789 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 4790 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 4791 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 4792 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 4793 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 4794 " (aaaa + aaaa);", 4795 getLLVMStyleWithColumns(40)); 4796 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 4797 " (aaaaaaa + aaaaa));", 4798 getLLVMStyleWithColumns(40)); 4799 verifyFormat( 4800 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 4801 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 4802 " bbbbbbbbbbbbbbbbbbbbbbb);"); 4803 } 4804 4805 TEST_F(FormatTest, UnderstandsEquals) { 4806 verifyFormat( 4807 "aaaaaaaaaaaaaaaaa =\n" 4808 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4809 verifyFormat( 4810 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4811 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 4812 verifyFormat( 4813 "if (a) {\n" 4814 " f();\n" 4815 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4816 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 4817 "}"); 4818 4819 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4820 " 100000000 + 10000000) {\n}"); 4821 } 4822 4823 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 4824 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 4825 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 4826 4827 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 4828 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 4829 4830 verifyFormat( 4831 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 4832 " Parameter2);"); 4833 4834 verifyFormat( 4835 "ShortObject->shortFunction(\n" 4836 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 4837 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 4838 4839 verifyFormat("loooooooooooooongFunction(\n" 4840 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 4841 4842 verifyFormat( 4843 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 4844 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 4845 4846 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 4847 " .WillRepeatedly(Return(SomeValue));"); 4848 verifyFormat("void f() {\n" 4849 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 4850 " .Times(2)\n" 4851 " .WillRepeatedly(Return(SomeValue));\n" 4852 "}"); 4853 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 4854 " ccccccccccccccccccccccc);"); 4855 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4856 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4857 " .aaaaa(aaaaa),\n" 4858 " aaaaaaaaaaaaaaaaaaaaa);"); 4859 verifyFormat("void f() {\n" 4860 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4861 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 4862 "}"); 4863 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4865 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4866 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4867 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4868 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4869 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4870 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4871 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 4872 "}"); 4873 4874 // Here, it is not necessary to wrap at "." or "->". 4875 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 4876 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 4877 verifyFormat( 4878 "aaaaaaaaaaa->aaaaaaaaa(\n" 4879 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4880 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 4881 4882 verifyFormat( 4883 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4884 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 4885 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 4886 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 4887 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 4888 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 4889 4890 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4892 " .a();"); 4893 4894 FormatStyle NoBinPacking = getLLVMStyle(); 4895 NoBinPacking.BinPackParameters = false; 4896 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 4897 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 4898 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 4899 " aaaaaaaaaaaaaaaaaaa,\n" 4900 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4901 NoBinPacking); 4902 4903 // If there is a subsequent call, change to hanging indentation. 4904 verifyFormat( 4905 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4906 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 4907 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4908 verifyFormat( 4909 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4910 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 4911 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4912 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4913 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4914 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4916 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 4917 } 4918 4919 TEST_F(FormatTest, WrapsTemplateDeclarations) { 4920 verifyFormat("template <typename T>\n" 4921 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 4922 verifyFormat("template <typename T>\n" 4923 "// T should be one of {A, B}.\n" 4924 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 4925 verifyFormat( 4926 "template <typename T>\n" 4927 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 4928 verifyFormat("template <typename T>\n" 4929 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 4930 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 4931 verifyFormat( 4932 "template <typename T>\n" 4933 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 4934 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 4935 verifyFormat( 4936 "template <typename T>\n" 4937 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 4938 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 4939 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4940 verifyFormat("template <typename T>\n" 4941 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4942 " int aaaaaaaaaaaaaaaaaaaaaa);"); 4943 verifyFormat( 4944 "template <typename T1, typename T2 = char, typename T3 = char,\n" 4945 " typename T4 = char>\n" 4946 "void f();"); 4947 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 4948 " template <typename> class cccccccccccccccccccccc,\n" 4949 " typename ddddddddddddd>\n" 4950 "class C {};"); 4951 verifyFormat( 4952 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 4953 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4954 4955 verifyFormat("void f() {\n" 4956 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 4957 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 4958 "}"); 4959 4960 verifyFormat("template <typename T> class C {};"); 4961 verifyFormat("template <typename T> void f();"); 4962 verifyFormat("template <typename T> void f() {}"); 4963 verifyFormat( 4964 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 4965 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4966 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 4967 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 4968 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4969 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 4970 " bbbbbbbbbbbbbbbbbbbbbbbb);", 4971 getLLVMStyleWithColumns(72)); 4972 EXPECT_EQ("static_cast<A< //\n" 4973 " B> *>(\n" 4974 "\n" 4975 ");", 4976 format("static_cast<A<//\n" 4977 " B>*>(\n" 4978 "\n" 4979 " );")); 4980 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4981 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 4982 4983 FormatStyle AlwaysBreak = getLLVMStyle(); 4984 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 4985 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 4986 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 4987 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 4988 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4989 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 4990 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 4991 verifyFormat("template <template <typename> class Fooooooo,\n" 4992 " template <typename> class Baaaaaaar>\n" 4993 "struct C {};", 4994 AlwaysBreak); 4995 verifyFormat("template <typename T> // T can be A, B or C.\n" 4996 "struct C {};", 4997 AlwaysBreak); 4998 verifyFormat("template <enum E> class A {\n" 4999 "public:\n" 5000 " E *f();\n" 5001 "};"); 5002 } 5003 5004 TEST_F(FormatTest, WrapsTemplateParameters) { 5005 FormatStyle Style = getLLVMStyle(); 5006 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5007 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5008 verifyFormat( 5009 "template <typename... a> struct q {};\n" 5010 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5011 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5012 " y;", 5013 Style); 5014 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5015 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5016 verifyFormat( 5017 "template <typename... a> struct r {};\n" 5018 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 5019 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 5020 " y;", 5021 Style); 5022 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5023 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5024 verifyFormat( 5025 "template <typename... a> struct s {};\n" 5026 "extern s<\n" 5027 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5028 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5029 " y;", 5030 Style); 5031 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5032 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5033 verifyFormat( 5034 "template <typename... a> struct t {};\n" 5035 "extern t<\n" 5036 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5037 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n" 5038 " y;", 5039 Style); 5040 } 5041 5042 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5043 verifyFormat( 5044 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5045 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5046 verifyFormat( 5047 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5048 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5050 5051 // FIXME: Should we have the extra indent after the second break? 5052 verifyFormat( 5053 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5055 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5056 5057 verifyFormat( 5058 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5059 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5060 5061 // Breaking at nested name specifiers is generally not desirable. 5062 verifyFormat( 5063 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5064 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5065 5066 verifyFormat( 5067 "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 5068 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5069 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5070 " aaaaaaaaaaaaaaaaaaaaa);", 5071 getLLVMStyleWithColumns(74)); 5072 5073 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5075 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5076 } 5077 5078 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5079 verifyFormat("A<int> a;"); 5080 verifyFormat("A<A<A<int>>> a;"); 5081 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5082 verifyFormat("bool x = a < 1 || 2 > a;"); 5083 verifyFormat("bool x = 5 < f<int>();"); 5084 verifyFormat("bool x = f<int>() > 5;"); 5085 verifyFormat("bool x = 5 < a<int>::x;"); 5086 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5087 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5088 5089 verifyGoogleFormat("A<A<int>> a;"); 5090 verifyGoogleFormat("A<A<A<int>>> a;"); 5091 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5092 verifyGoogleFormat("A<A<int> > a;"); 5093 verifyGoogleFormat("A<A<A<int> > > a;"); 5094 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5095 verifyGoogleFormat("A<::A<int>> a;"); 5096 verifyGoogleFormat("A<::A> a;"); 5097 verifyGoogleFormat("A< ::A> a;"); 5098 verifyGoogleFormat("A< ::A<int> > a;"); 5099 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5100 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5101 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5102 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5103 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 5104 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 5105 5106 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5107 5108 verifyFormat("test >> a >> b;"); 5109 verifyFormat("test << a >> b;"); 5110 5111 verifyFormat("f<int>();"); 5112 verifyFormat("template <typename T> void f() {}"); 5113 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5114 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5115 "sizeof(char)>::type>;"); 5116 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5117 verifyFormat("f(a.operator()<A>());"); 5118 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5119 " .template operator()<A>());", 5120 getLLVMStyleWithColumns(35)); 5121 5122 // Not template parameters. 5123 verifyFormat("return a < b && c > d;"); 5124 verifyFormat("void f() {\n" 5125 " while (a < b && c > d) {\n" 5126 " }\n" 5127 "}"); 5128 verifyFormat("template <typename... Types>\n" 5129 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5130 5131 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5132 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5133 getLLVMStyleWithColumns(60)); 5134 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5135 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5136 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5137 } 5138 5139 TEST_F(FormatTest, BitshiftOperatorWidth) { 5140 EXPECT_EQ("int a = 1 << 2; /* foo\n" 5141 " bar */", 5142 format("int a=1<<2; /* foo\n" 5143 " bar */")); 5144 5145 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 5146 " bar */", 5147 format("int b =256>>1 ; /* foo\n" 5148 " bar */")); 5149 } 5150 5151 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5152 verifyFormat("COMPARE(a, ==, b);"); 5153 verifyFormat("auto s = sizeof...(Ts) - 1;"); 5154 } 5155 5156 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5157 verifyFormat("int A::*x;"); 5158 verifyFormat("int (S::*func)(void *);"); 5159 verifyFormat("void f() { int (S::*func)(void *); }"); 5160 verifyFormat("typedef bool *(Class::*Member)() const;"); 5161 verifyFormat("void f() {\n" 5162 " (a->*f)();\n" 5163 " a->*x;\n" 5164 " (a.*f)();\n" 5165 " ((*a).*f)();\n" 5166 " a.*x;\n" 5167 "}"); 5168 verifyFormat("void f() {\n" 5169 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5170 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5171 "}"); 5172 verifyFormat( 5173 "(aaaaaaaaaa->*bbbbbbb)(\n" 5174 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5175 FormatStyle Style = getLLVMStyle(); 5176 Style.PointerAlignment = FormatStyle::PAS_Left; 5177 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5178 } 5179 5180 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5181 verifyFormat("int a = -2;"); 5182 verifyFormat("f(-1, -2, -3);"); 5183 verifyFormat("a[-1] = 5;"); 5184 verifyFormat("int a = 5 + -2;"); 5185 verifyFormat("if (i == -1) {\n}"); 5186 verifyFormat("if (i != -1) {\n}"); 5187 verifyFormat("if (i > -1) {\n}"); 5188 verifyFormat("if (i < -1) {\n}"); 5189 verifyFormat("++(a->f());"); 5190 verifyFormat("--(a->f());"); 5191 verifyFormat("(a->f())++;"); 5192 verifyFormat("a[42]++;"); 5193 verifyFormat("if (!(a->f())) {\n}"); 5194 5195 verifyFormat("a-- > b;"); 5196 verifyFormat("b ? -a : c;"); 5197 verifyFormat("n * sizeof char16;"); 5198 verifyFormat("n * alignof char16;", getGoogleStyle()); 5199 verifyFormat("sizeof(char);"); 5200 verifyFormat("alignof(char);", getGoogleStyle()); 5201 5202 verifyFormat("return -1;"); 5203 verifyFormat("switch (a) {\n" 5204 "case -1:\n" 5205 " break;\n" 5206 "}"); 5207 verifyFormat("#define X -1"); 5208 verifyFormat("#define X -kConstant"); 5209 5210 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5211 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5212 5213 verifyFormat("int a = /* confusing comment */ -1;"); 5214 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5215 verifyFormat("int a = i /* confusing comment */++;"); 5216 } 5217 5218 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5219 verifyFormat("if (!aaaaaaaaaa( // break\n" 5220 " aaaaa)) {\n" 5221 "}"); 5222 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5223 " aaaaa));"); 5224 verifyFormat("*aaa = aaaaaaa( // break\n" 5225 " bbbbbb);"); 5226 } 5227 5228 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5229 verifyFormat("bool operator<();"); 5230 verifyFormat("bool operator>();"); 5231 verifyFormat("bool operator=();"); 5232 verifyFormat("bool operator==();"); 5233 verifyFormat("bool operator!=();"); 5234 verifyFormat("int operator+();"); 5235 verifyFormat("int operator++();"); 5236 verifyFormat("bool operator,();"); 5237 verifyFormat("bool operator();"); 5238 verifyFormat("bool operator()();"); 5239 verifyFormat("bool operator[]();"); 5240 verifyFormat("operator bool();"); 5241 verifyFormat("operator int();"); 5242 verifyFormat("operator void *();"); 5243 verifyFormat("operator SomeType<int>();"); 5244 verifyFormat("operator SomeType<int, int>();"); 5245 verifyFormat("operator SomeType<SomeType<int>>();"); 5246 verifyFormat("void *operator new(std::size_t size);"); 5247 verifyFormat("void *operator new[](std::size_t size);"); 5248 verifyFormat("void operator delete(void *ptr);"); 5249 verifyFormat("void operator delete[](void *ptr);"); 5250 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5251 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5252 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 5253 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 5254 5255 verifyFormat( 5256 "ostream &operator<<(ostream &OutputStream,\n" 5257 " SomeReallyLongType WithSomeReallyLongValue);"); 5258 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5259 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5260 " return left.group < right.group;\n" 5261 "}"); 5262 verifyFormat("SomeType &operator=(const SomeType &S);"); 5263 verifyFormat("f.template operator()<int>();"); 5264 5265 verifyGoogleFormat("operator void*();"); 5266 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5267 verifyGoogleFormat("operator ::A();"); 5268 5269 verifyFormat("using A::operator+;"); 5270 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 5271 "int i;"); 5272 } 5273 5274 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5275 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 5276 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 5277 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 5278 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 5279 verifyFormat("Deleted &operator=(const Deleted &) &;"); 5280 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 5281 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 5282 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 5283 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 5284 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 5285 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 5286 verifyFormat("void Fn(T const &) const &;"); 5287 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 5288 verifyFormat("template <typename T>\n" 5289 "void F(T) && = delete;", 5290 getGoogleStyle()); 5291 5292 FormatStyle AlignLeft = getLLVMStyle(); 5293 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 5294 verifyFormat("void A::b() && {}", AlignLeft); 5295 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 5296 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 5297 AlignLeft); 5298 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 5299 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 5300 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 5301 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 5302 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 5303 verifyFormat("auto Function(T) & -> void;", AlignLeft); 5304 verifyFormat("void Fn(T const&) const&;", AlignLeft); 5305 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 5306 5307 FormatStyle Spaces = getLLVMStyle(); 5308 Spaces.SpacesInCStyleCastParentheses = true; 5309 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 5310 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 5311 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 5312 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 5313 5314 Spaces.SpacesInCStyleCastParentheses = false; 5315 Spaces.SpacesInParentheses = true; 5316 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 5317 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces); 5318 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 5319 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 5320 } 5321 5322 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5323 verifyFormat("void f() {\n" 5324 " A *a = new A;\n" 5325 " A *a = new (placement) A;\n" 5326 " delete a;\n" 5327 " delete (A *)a;\n" 5328 "}"); 5329 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5330 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5331 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5332 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5333 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5334 verifyFormat("delete[] h->p;"); 5335 } 5336 5337 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5338 verifyFormat("int *f(int *a) {}"); 5339 verifyFormat("int main(int argc, char **argv) {}"); 5340 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5341 verifyIndependentOfContext("f(a, *a);"); 5342 verifyFormat("void g() { f(*a); }"); 5343 verifyIndependentOfContext("int a = b * 10;"); 5344 verifyIndependentOfContext("int a = 10 * b;"); 5345 verifyIndependentOfContext("int a = b * c;"); 5346 verifyIndependentOfContext("int a += b * c;"); 5347 verifyIndependentOfContext("int a -= b * c;"); 5348 verifyIndependentOfContext("int a *= b * c;"); 5349 verifyIndependentOfContext("int a /= b * c;"); 5350 verifyIndependentOfContext("int a = *b;"); 5351 verifyIndependentOfContext("int a = *b * c;"); 5352 verifyIndependentOfContext("int a = b * *c;"); 5353 verifyIndependentOfContext("int a = b * (10);"); 5354 verifyIndependentOfContext("S << b * (10);"); 5355 verifyIndependentOfContext("return 10 * b;"); 5356 verifyIndependentOfContext("return *b * *c;"); 5357 verifyIndependentOfContext("return a & ~b;"); 5358 verifyIndependentOfContext("f(b ? *c : *d);"); 5359 verifyIndependentOfContext("int a = b ? *c : *d;"); 5360 verifyIndependentOfContext("*b = a;"); 5361 verifyIndependentOfContext("a * ~b;"); 5362 verifyIndependentOfContext("a * !b;"); 5363 verifyIndependentOfContext("a * +b;"); 5364 verifyIndependentOfContext("a * -b;"); 5365 verifyIndependentOfContext("a * ++b;"); 5366 verifyIndependentOfContext("a * --b;"); 5367 verifyIndependentOfContext("a[4] * b;"); 5368 verifyIndependentOfContext("a[a * a] = 1;"); 5369 verifyIndependentOfContext("f() * b;"); 5370 verifyIndependentOfContext("a * [self dostuff];"); 5371 verifyIndependentOfContext("int x = a * (a + b);"); 5372 verifyIndependentOfContext("(a *)(a + b);"); 5373 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5374 verifyIndependentOfContext("int *pa = (int *)&a;"); 5375 verifyIndependentOfContext("return sizeof(int **);"); 5376 verifyIndependentOfContext("return sizeof(int ******);"); 5377 verifyIndependentOfContext("return (int **&)a;"); 5378 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5379 verifyFormat("void f(Type (*parameter)[10]) {}"); 5380 verifyFormat("void f(Type (¶meter)[10]) {}"); 5381 verifyGoogleFormat("return sizeof(int**);"); 5382 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5383 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5384 verifyFormat("auto a = [](int **&, int ***) {};"); 5385 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5386 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5387 verifyFormat("[](const decltype(*a) &value) {}"); 5388 verifyFormat("decltype(a * b) F();"); 5389 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5390 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 5391 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5392 verifyIndependentOfContext("int i{a * b};"); 5393 verifyIndependentOfContext("aaa && aaa->f();"); 5394 verifyIndependentOfContext("int x = ~*p;"); 5395 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5396 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5397 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5398 verifyFormat("void f() { f(a, c * d); }"); 5399 verifyFormat("void f() { f(new a(), c * d); }"); 5400 verifyFormat("void f(const MyOverride &override);"); 5401 verifyFormat("void f(const MyFinal &final);"); 5402 verifyIndependentOfContext("bool a = f() && override.f();"); 5403 verifyIndependentOfContext("bool a = f() && final.f();"); 5404 5405 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5406 5407 verifyIndependentOfContext("A<int *> a;"); 5408 verifyIndependentOfContext("A<int **> a;"); 5409 verifyIndependentOfContext("A<int *, int *> a;"); 5410 verifyIndependentOfContext("A<int *[]> a;"); 5411 verifyIndependentOfContext( 5412 "const char *const p = reinterpret_cast<const char *const>(q);"); 5413 verifyIndependentOfContext("A<int **, int **> a;"); 5414 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5415 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5416 verifyFormat("for (; a && b;) {\n}"); 5417 verifyFormat("bool foo = true && [] { return false; }();"); 5418 5419 verifyFormat( 5420 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5422 5423 verifyGoogleFormat("int const* a = &b;"); 5424 verifyGoogleFormat("**outparam = 1;"); 5425 verifyGoogleFormat("*outparam = a * b;"); 5426 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5427 verifyGoogleFormat("A<int*> a;"); 5428 verifyGoogleFormat("A<int**> a;"); 5429 verifyGoogleFormat("A<int*, int*> a;"); 5430 verifyGoogleFormat("A<int**, int**> a;"); 5431 verifyGoogleFormat("f(b ? *c : *d);"); 5432 verifyGoogleFormat("int a = b ? *c : *d;"); 5433 verifyGoogleFormat("Type* t = **x;"); 5434 verifyGoogleFormat("Type* t = *++*x;"); 5435 verifyGoogleFormat("*++*x;"); 5436 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5437 verifyGoogleFormat("Type* t = x++ * y;"); 5438 verifyGoogleFormat( 5439 "const char* const p = reinterpret_cast<const char* const>(q);"); 5440 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5441 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5442 verifyGoogleFormat("template <typename T>\n" 5443 "void f(int i = 0, SomeType** temps = NULL);"); 5444 5445 FormatStyle Left = getLLVMStyle(); 5446 Left.PointerAlignment = FormatStyle::PAS_Left; 5447 verifyFormat("x = *a(x) = *a(y);", Left); 5448 verifyFormat("for (;; *a = b) {\n}", Left); 5449 verifyFormat("return *this += 1;", Left); 5450 verifyFormat("throw *x;", Left); 5451 5452 verifyIndependentOfContext("a = *(x + y);"); 5453 verifyIndependentOfContext("a = &(x + y);"); 5454 verifyIndependentOfContext("*(x + y).call();"); 5455 verifyIndependentOfContext("&(x + y)->call();"); 5456 verifyFormat("void f() { &(*I).first; }"); 5457 5458 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5459 verifyFormat( 5460 "int *MyValues = {\n" 5461 " *A, // Operator detection might be confused by the '{'\n" 5462 " *BB // Operator detection might be confused by previous comment\n" 5463 "};"); 5464 5465 verifyIndependentOfContext("if (int *a = &b)"); 5466 verifyIndependentOfContext("if (int &a = *b)"); 5467 verifyIndependentOfContext("if (a & b[i])"); 5468 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5469 verifyIndependentOfContext("if (*b[i])"); 5470 verifyIndependentOfContext("if (int *a = (&b))"); 5471 verifyIndependentOfContext("while (int *a = &b)"); 5472 verifyIndependentOfContext("size = sizeof *a;"); 5473 verifyIndependentOfContext("if (a && (b = c))"); 5474 verifyFormat("void f() {\n" 5475 " for (const int &v : Values) {\n" 5476 " }\n" 5477 "}"); 5478 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5479 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5480 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5481 5482 verifyFormat("#define A (!a * b)"); 5483 verifyFormat("#define MACRO \\\n" 5484 " int *i = a * b; \\\n" 5485 " void f(a *b);", 5486 getLLVMStyleWithColumns(19)); 5487 5488 verifyIndependentOfContext("A = new SomeType *[Length];"); 5489 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5490 verifyIndependentOfContext("T **t = new T *;"); 5491 verifyIndependentOfContext("T **t = new T *();"); 5492 verifyGoogleFormat("A = new SomeType*[Length]();"); 5493 verifyGoogleFormat("A = new SomeType*[Length];"); 5494 verifyGoogleFormat("T** t = new T*;"); 5495 verifyGoogleFormat("T** t = new T*();"); 5496 5497 FormatStyle PointerLeft = getLLVMStyle(); 5498 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5499 verifyFormat("delete *x;", PointerLeft); 5500 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5501 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5502 verifyFormat("template <bool a, bool b> " 5503 "typename t::if<x && y>::type f() {}"); 5504 verifyFormat("template <int *y> f() {}"); 5505 verifyFormat("vector<int *> v;"); 5506 verifyFormat("vector<int *const> v;"); 5507 verifyFormat("vector<int *const **const *> v;"); 5508 verifyFormat("vector<int *volatile> v;"); 5509 verifyFormat("vector<a * b> v;"); 5510 verifyFormat("foo<b && false>();"); 5511 verifyFormat("foo<b & 1>();"); 5512 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5513 verifyFormat( 5514 "template <class T, class = typename std::enable_if<\n" 5515 " std::is_integral<T>::value &&\n" 5516 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5517 "void F();", 5518 getLLVMStyleWithColumns(70)); 5519 verifyFormat( 5520 "template <class T,\n" 5521 " class = typename std::enable_if<\n" 5522 " std::is_integral<T>::value &&\n" 5523 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 5524 " class U>\n" 5525 "void F();", 5526 getLLVMStyleWithColumns(70)); 5527 verifyFormat( 5528 "template <class T,\n" 5529 " class = typename ::std::enable_if<\n" 5530 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5531 "void F();", 5532 getGoogleStyleWithColumns(68)); 5533 5534 verifyIndependentOfContext("MACRO(int *i);"); 5535 verifyIndependentOfContext("MACRO(auto *a);"); 5536 verifyIndependentOfContext("MACRO(const A *a);"); 5537 verifyIndependentOfContext("MACRO(A *const a);"); 5538 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5539 verifyFormat("void f() { f(float{1}, a * a); }"); 5540 // FIXME: Is there a way to make this work? 5541 // verifyIndependentOfContext("MACRO(A *a);"); 5542 5543 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5544 verifyFormat("return options != nullptr && operator==(*options);"); 5545 5546 EXPECT_EQ("#define OP(x) \\\n" 5547 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5548 " return s << a.DebugString(); \\\n" 5549 " }", 5550 format("#define OP(x) \\\n" 5551 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5552 " return s << a.DebugString(); \\\n" 5553 " }", 5554 getLLVMStyleWithColumns(50))); 5555 5556 // FIXME: We cannot handle this case yet; we might be able to figure out that 5557 // foo<x> d > v; doesn't make sense. 5558 verifyFormat("foo<a<b && c> d> v;"); 5559 5560 FormatStyle PointerMiddle = getLLVMStyle(); 5561 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5562 verifyFormat("delete *x;", PointerMiddle); 5563 verifyFormat("int * x;", PointerMiddle); 5564 verifyFormat("template <int * y> f() {}", PointerMiddle); 5565 verifyFormat("int * f(int * a) {}", PointerMiddle); 5566 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5567 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5568 verifyFormat("A<int *> a;", PointerMiddle); 5569 verifyFormat("A<int **> a;", PointerMiddle); 5570 verifyFormat("A<int *, int *> a;", PointerMiddle); 5571 verifyFormat("A<int * []> a;", PointerMiddle); 5572 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5573 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5574 verifyFormat("T ** t = new T *;", PointerMiddle); 5575 5576 // Member function reference qualifiers aren't binary operators. 5577 verifyFormat("string // break\n" 5578 "operator()() & {}"); 5579 verifyFormat("string // break\n" 5580 "operator()() && {}"); 5581 verifyGoogleFormat("template <typename T>\n" 5582 "auto x() & -> int {}"); 5583 } 5584 5585 TEST_F(FormatTest, UnderstandsAttributes) { 5586 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5587 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5588 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5589 FormatStyle AfterType = getLLVMStyle(); 5590 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 5591 verifyFormat("__attribute__((nodebug)) void\n" 5592 "foo() {}\n", 5593 AfterType); 5594 } 5595 5596 TEST_F(FormatTest, UnderstandsEllipsis) { 5597 verifyFormat("int printf(const char *fmt, ...);"); 5598 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5599 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5600 5601 FormatStyle PointersLeft = getLLVMStyle(); 5602 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5603 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5604 } 5605 5606 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5607 EXPECT_EQ("int *a;\n" 5608 "int *a;\n" 5609 "int *a;", 5610 format("int *a;\n" 5611 "int* a;\n" 5612 "int *a;", 5613 getGoogleStyle())); 5614 EXPECT_EQ("int* a;\n" 5615 "int* a;\n" 5616 "int* a;", 5617 format("int* a;\n" 5618 "int* a;\n" 5619 "int *a;", 5620 getGoogleStyle())); 5621 EXPECT_EQ("int *a;\n" 5622 "int *a;\n" 5623 "int *a;", 5624 format("int *a;\n" 5625 "int * a;\n" 5626 "int * a;", 5627 getGoogleStyle())); 5628 EXPECT_EQ("auto x = [] {\n" 5629 " int *a;\n" 5630 " int *a;\n" 5631 " int *a;\n" 5632 "};", 5633 format("auto x=[]{int *a;\n" 5634 "int * a;\n" 5635 "int * a;};", 5636 getGoogleStyle())); 5637 } 5638 5639 TEST_F(FormatTest, UnderstandsRvalueReferences) { 5640 verifyFormat("int f(int &&a) {}"); 5641 verifyFormat("int f(int a, char &&b) {}"); 5642 verifyFormat("void f() { int &&a = b; }"); 5643 verifyGoogleFormat("int f(int a, char&& b) {}"); 5644 verifyGoogleFormat("void f() { int&& a = b; }"); 5645 5646 verifyIndependentOfContext("A<int &&> a;"); 5647 verifyIndependentOfContext("A<int &&, int &&> a;"); 5648 verifyGoogleFormat("A<int&&> a;"); 5649 verifyGoogleFormat("A<int&&, int&&> a;"); 5650 5651 // Not rvalue references: 5652 verifyFormat("template <bool B, bool C> class A {\n" 5653 " static_assert(B && C, \"Something is wrong\");\n" 5654 "};"); 5655 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 5656 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 5657 verifyFormat("#define A(a, b) (a && b)"); 5658 } 5659 5660 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 5661 verifyFormat("void f() {\n" 5662 " x[aaaaaaaaa -\n" 5663 " b] = 23;\n" 5664 "}", 5665 getLLVMStyleWithColumns(15)); 5666 } 5667 5668 TEST_F(FormatTest, FormatsCasts) { 5669 verifyFormat("Type *A = static_cast<Type *>(P);"); 5670 verifyFormat("Type *A = (Type *)P;"); 5671 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 5672 verifyFormat("int a = (int)(2.0f);"); 5673 verifyFormat("int a = (int)2.0f;"); 5674 verifyFormat("x[(int32)y];"); 5675 verifyFormat("x = (int32)y;"); 5676 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 5677 verifyFormat("int a = (int)*b;"); 5678 verifyFormat("int a = (int)2.0f;"); 5679 verifyFormat("int a = (int)~0;"); 5680 verifyFormat("int a = (int)++a;"); 5681 verifyFormat("int a = (int)sizeof(int);"); 5682 verifyFormat("int a = (int)+2;"); 5683 verifyFormat("my_int a = (my_int)2.0f;"); 5684 verifyFormat("my_int a = (my_int)sizeof(int);"); 5685 verifyFormat("return (my_int)aaa;"); 5686 verifyFormat("#define x ((int)-1)"); 5687 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 5688 verifyFormat("#define p(q) ((int *)&q)"); 5689 verifyFormat("fn(a)(b) + 1;"); 5690 5691 verifyFormat("void f() { my_int a = (my_int)*b; }"); 5692 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 5693 verifyFormat("my_int a = (my_int)~0;"); 5694 verifyFormat("my_int a = (my_int)++a;"); 5695 verifyFormat("my_int a = (my_int)-2;"); 5696 verifyFormat("my_int a = (my_int)1;"); 5697 verifyFormat("my_int a = (my_int *)1;"); 5698 verifyFormat("my_int a = (const my_int)-1;"); 5699 verifyFormat("my_int a = (const my_int *)-1;"); 5700 verifyFormat("my_int a = (my_int)(my_int)-1;"); 5701 verifyFormat("my_int a = (ns::my_int)-2;"); 5702 verifyFormat("case (my_int)ONE:"); 5703 verifyFormat("auto x = (X)this;"); 5704 5705 // FIXME: single value wrapped with paren will be treated as cast. 5706 verifyFormat("void f(int i = (kValue)*kMask) {}"); 5707 5708 verifyFormat("{ (void)F; }"); 5709 5710 // Don't break after a cast's 5711 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5712 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 5713 " bbbbbbbbbbbbbbbbbbbbbb);"); 5714 5715 // These are not casts. 5716 verifyFormat("void f(int *) {}"); 5717 verifyFormat("f(foo)->b;"); 5718 verifyFormat("f(foo).b;"); 5719 verifyFormat("f(foo)(b);"); 5720 verifyFormat("f(foo)[b];"); 5721 verifyFormat("[](foo) { return 4; }(bar);"); 5722 verifyFormat("(*funptr)(foo)[4];"); 5723 verifyFormat("funptrs[4](foo)[4];"); 5724 verifyFormat("void f(int *);"); 5725 verifyFormat("void f(int *) = 0;"); 5726 verifyFormat("void f(SmallVector<int>) {}"); 5727 verifyFormat("void f(SmallVector<int>);"); 5728 verifyFormat("void f(SmallVector<int>) = 0;"); 5729 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 5730 verifyFormat("int a = sizeof(int) * b;"); 5731 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 5732 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 5733 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 5734 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 5735 5736 // These are not casts, but at some point were confused with casts. 5737 verifyFormat("virtual void foo(int *) override;"); 5738 verifyFormat("virtual void foo(char &) const;"); 5739 verifyFormat("virtual void foo(int *a, char *) const;"); 5740 verifyFormat("int a = sizeof(int *) + b;"); 5741 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 5742 verifyFormat("bool b = f(g<int>) && c;"); 5743 verifyFormat("typedef void (*f)(int i) func;"); 5744 5745 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 5746 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5747 // FIXME: The indentation here is not ideal. 5748 verifyFormat( 5749 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5750 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 5751 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 5752 } 5753 5754 TEST_F(FormatTest, FormatsFunctionTypes) { 5755 verifyFormat("A<bool()> a;"); 5756 verifyFormat("A<SomeType()> a;"); 5757 verifyFormat("A<void (*)(int, std::string)> a;"); 5758 verifyFormat("A<void *(int)>;"); 5759 verifyFormat("void *(*a)(int *, SomeType *);"); 5760 verifyFormat("int (*func)(void *);"); 5761 verifyFormat("void f() { int (*func)(void *); }"); 5762 verifyFormat("template <class CallbackClass>\n" 5763 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 5764 5765 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 5766 verifyGoogleFormat("void* (*a)(int);"); 5767 verifyGoogleFormat( 5768 "template <class CallbackClass>\n" 5769 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 5770 5771 // Other constructs can look somewhat like function types: 5772 verifyFormat("A<sizeof(*x)> a;"); 5773 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 5774 verifyFormat("some_var = function(*some_pointer_var)[0];"); 5775 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 5776 verifyFormat("int x = f(&h)();"); 5777 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 5778 verifyFormat("std::function<\n" 5779 " LooooooooooongTemplatedType<\n" 5780 " SomeType>*(\n" 5781 " LooooooooooooooooongType type)>\n" 5782 " function;", 5783 getGoogleStyleWithColumns(40)); 5784 } 5785 5786 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 5787 verifyFormat("A (*foo_)[6];"); 5788 verifyFormat("vector<int> (*foo_)[6];"); 5789 } 5790 5791 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 5792 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5793 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5794 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 5795 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5796 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5797 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5798 5799 // Different ways of ()-initializiation. 5800 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5801 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 5802 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5803 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 5804 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5805 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 5806 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5807 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 5808 5809 // Lambdas should not confuse the variable declaration heuristic. 5810 verifyFormat("LooooooooooooooooongType\n" 5811 " variable(nullptr, [](A *a) {});", 5812 getLLVMStyleWithColumns(40)); 5813 } 5814 5815 TEST_F(FormatTest, BreaksLongDeclarations) { 5816 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 5817 " AnotherNameForTheLongType;"); 5818 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 5819 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5820 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5821 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5822 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 5823 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5824 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5825 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5826 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 5827 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5828 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5829 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5830 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5831 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5832 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5833 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 5834 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5835 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 5836 FormatStyle Indented = getLLVMStyle(); 5837 Indented.IndentWrappedFunctionNames = true; 5838 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5839 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 5840 Indented); 5841 verifyFormat( 5842 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5843 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5844 Indented); 5845 verifyFormat( 5846 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5847 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5848 Indented); 5849 verifyFormat( 5850 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5851 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5852 Indented); 5853 5854 // FIXME: Without the comment, this breaks after "(". 5855 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 5856 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 5857 getGoogleStyle()); 5858 5859 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 5860 " int LoooooooooooooooooooongParam2) {}"); 5861 verifyFormat( 5862 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 5863 " SourceLocation L, IdentifierIn *II,\n" 5864 " Type *T) {}"); 5865 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 5866 "ReallyReaaallyLongFunctionName(\n" 5867 " const std::string &SomeParameter,\n" 5868 " const SomeType<string, SomeOtherTemplateParameter>\n" 5869 " &ReallyReallyLongParameterName,\n" 5870 " const SomeType<string, SomeOtherTemplateParameter>\n" 5871 " &AnotherLongParameterName) {}"); 5872 verifyFormat("template <typename A>\n" 5873 "SomeLoooooooooooooooooooooongType<\n" 5874 " typename some_namespace::SomeOtherType<A>::Type>\n" 5875 "Function() {}"); 5876 5877 verifyGoogleFormat( 5878 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 5879 " aaaaaaaaaaaaaaaaaaaaaaa;"); 5880 verifyGoogleFormat( 5881 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 5882 " SourceLocation L) {}"); 5883 verifyGoogleFormat( 5884 "some_namespace::LongReturnType\n" 5885 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 5886 " int first_long_parameter, int second_parameter) {}"); 5887 5888 verifyGoogleFormat("template <typename T>\n" 5889 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5890 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 5891 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5892 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 5893 5894 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5895 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5896 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5897 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5898 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5899 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5900 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5901 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5902 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 5903 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5904 5905 verifyFormat("template <typename T> // Templates on own line.\n" 5906 "static int // Some comment.\n" 5907 "MyFunction(int a);", 5908 getLLVMStyle()); 5909 } 5910 5911 TEST_F(FormatTest, FormatsArrays) { 5912 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5913 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 5914 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 5915 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 5916 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 5917 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 5918 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5919 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 5920 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5921 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 5922 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5923 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5924 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 5925 verifyFormat( 5926 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 5927 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5928 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 5929 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 5930 " .aaaaaaaaaaaaaaaaaaaaaa();"); 5931 5932 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 5933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 5934 verifyFormat( 5935 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 5936 " .aaaaaaa[0]\n" 5937 " .aaaaaaaaaaaaaaaaaaaaaa();"); 5938 verifyFormat("a[::b::c];"); 5939 5940 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 5941 5942 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 5943 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 5944 } 5945 5946 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 5947 verifyFormat("(a)->b();"); 5948 verifyFormat("--a;"); 5949 } 5950 5951 TEST_F(FormatTest, HandlesIncludeDirectives) { 5952 verifyFormat("#include <string>\n" 5953 "#include <a/b/c.h>\n" 5954 "#include \"a/b/string\"\n" 5955 "#include \"string.h\"\n" 5956 "#include \"string.h\"\n" 5957 "#include <a-a>\n" 5958 "#include < path with space >\n" 5959 "#include_next <test.h>" 5960 "#include \"abc.h\" // this is included for ABC\n" 5961 "#include \"some long include\" // with a comment\n" 5962 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 5963 getLLVMStyleWithColumns(35)); 5964 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 5965 EXPECT_EQ("#include <a>", format("#include<a>")); 5966 5967 verifyFormat("#import <string>"); 5968 verifyFormat("#import <a/b/c.h>"); 5969 verifyFormat("#import \"a/b/string\""); 5970 verifyFormat("#import \"string.h\""); 5971 verifyFormat("#import \"string.h\""); 5972 verifyFormat("#if __has_include(<strstream>)\n" 5973 "#include <strstream>\n" 5974 "#endif"); 5975 5976 verifyFormat("#define MY_IMPORT <a/b>"); 5977 5978 verifyFormat("#if __has_include(<a/b>)"); 5979 verifyFormat("#if __has_include_next(<a/b>)"); 5980 verifyFormat("#define F __has_include(<a/b>)"); 5981 verifyFormat("#define F __has_include_next(<a/b>)"); 5982 5983 // Protocol buffer definition or missing "#". 5984 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 5985 getLLVMStyleWithColumns(30)); 5986 5987 FormatStyle Style = getLLVMStyle(); 5988 Style.AlwaysBreakBeforeMultilineStrings = true; 5989 Style.ColumnLimit = 0; 5990 verifyFormat("#import \"abc.h\"", Style); 5991 5992 // But 'import' might also be a regular C++ namespace. 5993 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5994 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5995 } 5996 5997 //===----------------------------------------------------------------------===// 5998 // Error recovery tests. 5999 //===----------------------------------------------------------------------===// 6000 6001 TEST_F(FormatTest, IncompleteParameterLists) { 6002 FormatStyle NoBinPacking = getLLVMStyle(); 6003 NoBinPacking.BinPackParameters = false; 6004 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6005 " double *min_x,\n" 6006 " double *max_x,\n" 6007 " double *min_y,\n" 6008 " double *max_y,\n" 6009 " double *min_z,\n" 6010 " double *max_z, ) {}", 6011 NoBinPacking); 6012 } 6013 6014 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6015 verifyFormat("void f() { return; }\n42"); 6016 verifyFormat("void f() {\n" 6017 " if (0)\n" 6018 " return;\n" 6019 "}\n" 6020 "42"); 6021 verifyFormat("void f() { return }\n42"); 6022 verifyFormat("void f() {\n" 6023 " if (0)\n" 6024 " return\n" 6025 "}\n" 6026 "42"); 6027 } 6028 6029 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6030 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6031 EXPECT_EQ("void f() {\n" 6032 " if (a)\n" 6033 " return\n" 6034 "}", 6035 format("void f ( ) { if ( a ) return }")); 6036 EXPECT_EQ("namespace N {\n" 6037 "void f()\n" 6038 "}", 6039 format("namespace N { void f() }")); 6040 EXPECT_EQ("namespace N {\n" 6041 "void f() {}\n" 6042 "void g()\n" 6043 "} // namespace N", 6044 format("namespace N { void f( ) { } void g( ) }")); 6045 } 6046 6047 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6048 verifyFormat("int aaaaaaaa =\n" 6049 " // Overlylongcomment\n" 6050 " b;", 6051 getLLVMStyleWithColumns(20)); 6052 verifyFormat("function(\n" 6053 " ShortArgument,\n" 6054 " LoooooooooooongArgument);\n", 6055 getLLVMStyleWithColumns(20)); 6056 } 6057 6058 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6059 verifyFormat("public:"); 6060 verifyFormat("class A {\n" 6061 "public\n" 6062 " void f() {}\n" 6063 "};"); 6064 verifyFormat("public\n" 6065 "int qwerty;"); 6066 verifyFormat("public\n" 6067 "B {}"); 6068 verifyFormat("public\n" 6069 "{}"); 6070 verifyFormat("public\n" 6071 "B { int x; }"); 6072 } 6073 6074 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6075 verifyFormat("{"); 6076 verifyFormat("#})"); 6077 verifyNoCrash("(/**/[:!] ?[)."); 6078 } 6079 6080 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6081 verifyFormat("do {\n}"); 6082 verifyFormat("do {\n}\n" 6083 "f();"); 6084 verifyFormat("do {\n}\n" 6085 "wheeee(fun);"); 6086 verifyFormat("do {\n" 6087 " f();\n" 6088 "}"); 6089 } 6090 6091 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6092 verifyFormat("if {\n foo;\n foo();\n}"); 6093 verifyFormat("switch {\n foo;\n foo();\n}"); 6094 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6095 verifyFormat("while {\n foo;\n foo();\n}"); 6096 verifyFormat("do {\n foo;\n foo();\n} while;"); 6097 } 6098 6099 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6100 verifyIncompleteFormat("namespace {\n" 6101 "class Foo { Foo (\n" 6102 "};\n" 6103 "} // namespace"); 6104 } 6105 6106 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6107 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6108 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6109 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6110 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6111 6112 EXPECT_EQ("{\n" 6113 " {\n" 6114 " breakme(\n" 6115 " qwe);\n" 6116 " }\n", 6117 format("{\n" 6118 " {\n" 6119 " breakme(qwe);\n" 6120 "}\n", 6121 getLLVMStyleWithColumns(10))); 6122 } 6123 6124 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6125 verifyFormat("int x = {\n" 6126 " avariable,\n" 6127 " b(alongervariable)};", 6128 getLLVMStyleWithColumns(25)); 6129 } 6130 6131 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6132 verifyFormat("return (a)(b){1, 2, 3};"); 6133 } 6134 6135 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6136 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6137 verifyFormat("vector<int> x{\n" 6138 " 1,\n" 6139 " 2,\n" 6140 " 3,\n" 6141 " 4,\n" 6142 "};"); 6143 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6144 verifyFormat("f({1, 2});"); 6145 verifyFormat("auto v = Foo{-1};"); 6146 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6147 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6148 verifyFormat("new vector<int>{1, 2, 3};"); 6149 verifyFormat("new int[3]{1, 2, 3};"); 6150 verifyFormat("new int{1};"); 6151 verifyFormat("return {arg1, arg2};"); 6152 verifyFormat("return {arg1, SomeType{parameter}};"); 6153 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6154 verifyFormat("new T{arg1, arg2};"); 6155 verifyFormat("f(MyMap[{composite, key}]);"); 6156 verifyFormat("class Class {\n" 6157 " T member = {arg1, arg2};\n" 6158 "};"); 6159 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6160 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 6161 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 6162 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6163 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6164 6165 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6166 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6167 verifyFormat("auto i = decltype(x){};"); 6168 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6169 verifyFormat("Node n{1, Node{1000}, //\n" 6170 " 2};"); 6171 verifyFormat("Aaaa aaaaaaa{\n" 6172 " {\n" 6173 " aaaa,\n" 6174 " },\n" 6175 "};"); 6176 verifyFormat("class C : public D {\n" 6177 " SomeClass SC{2};\n" 6178 "};"); 6179 verifyFormat("class C : public A {\n" 6180 " class D : public B {\n" 6181 " void f() { int i{2}; }\n" 6182 " };\n" 6183 "};"); 6184 verifyFormat("#define A {a, a},"); 6185 6186 // Binpacking only if there is no trailing comma 6187 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 6188 " cccccccccc, dddddddddd};", 6189 getLLVMStyleWithColumns(50)); 6190 verifyFormat("const Aaaaaa aaaaa = {\n" 6191 " aaaaaaaaaaa,\n" 6192 " bbbbbbbbbbb,\n" 6193 " ccccccccccc,\n" 6194 " ddddddddddd,\n" 6195 "};", getLLVMStyleWithColumns(50)); 6196 6197 // Cases where distinguising braced lists and blocks is hard. 6198 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 6199 verifyFormat("void f() {\n" 6200 " return; // comment\n" 6201 "}\n" 6202 "SomeType t;"); 6203 verifyFormat("void f() {\n" 6204 " if (a) {\n" 6205 " f();\n" 6206 " }\n" 6207 "}\n" 6208 "SomeType t;"); 6209 6210 // In combination with BinPackArguments = false. 6211 FormatStyle NoBinPacking = getLLVMStyle(); 6212 NoBinPacking.BinPackArguments = false; 6213 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6214 " bbbbb,\n" 6215 " ccccc,\n" 6216 " ddddd,\n" 6217 " eeeee,\n" 6218 " ffffff,\n" 6219 " ggggg,\n" 6220 " hhhhhh,\n" 6221 " iiiiii,\n" 6222 " jjjjjj,\n" 6223 " kkkkkk};", 6224 NoBinPacking); 6225 verifyFormat("const Aaaaaa aaaaa = {\n" 6226 " aaaaa,\n" 6227 " bbbbb,\n" 6228 " ccccc,\n" 6229 " ddddd,\n" 6230 " eeeee,\n" 6231 " ffffff,\n" 6232 " ggggg,\n" 6233 " hhhhhh,\n" 6234 " iiiiii,\n" 6235 " jjjjjj,\n" 6236 " kkkkkk,\n" 6237 "};", 6238 NoBinPacking); 6239 verifyFormat( 6240 "const Aaaaaa aaaaa = {\n" 6241 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6242 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6243 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6244 "};", 6245 NoBinPacking); 6246 6247 // FIXME: The alignment of these trailing comments might be bad. Then again, 6248 // this might be utterly useless in real code. 6249 verifyFormat("Constructor::Constructor()\n" 6250 " : some_value{ //\n" 6251 " aaaaaaa, //\n" 6252 " bbbbbbb} {}"); 6253 6254 // In braced lists, the first comment is always assumed to belong to the 6255 // first element. Thus, it can be moved to the next or previous line as 6256 // appropriate. 6257 EXPECT_EQ("function({// First element:\n" 6258 " 1,\n" 6259 " // Second element:\n" 6260 " 2});", 6261 format("function({\n" 6262 " // First element:\n" 6263 " 1,\n" 6264 " // Second element:\n" 6265 " 2});")); 6266 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6267 " // First element:\n" 6268 " 1,\n" 6269 " // Second element:\n" 6270 " 2};", 6271 format("std::vector<int> MyNumbers{// First element:\n" 6272 " 1,\n" 6273 " // Second element:\n" 6274 " 2};", 6275 getLLVMStyleWithColumns(30))); 6276 // A trailing comma should still lead to an enforced line break and no 6277 // binpacking. 6278 EXPECT_EQ("vector<int> SomeVector = {\n" 6279 " // aaa\n" 6280 " 1,\n" 6281 " 2,\n" 6282 "};", 6283 format("vector<int> SomeVector = { // aaa\n" 6284 " 1, 2, };")); 6285 6286 FormatStyle ExtraSpaces = getLLVMStyle(); 6287 ExtraSpaces.Cpp11BracedListStyle = false; 6288 ExtraSpaces.ColumnLimit = 75; 6289 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6290 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6291 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6292 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6293 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6294 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6295 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6296 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6297 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6298 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6299 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6300 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6301 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6302 verifyFormat("class Class {\n" 6303 " T member = { arg1, arg2 };\n" 6304 "};", 6305 ExtraSpaces); 6306 verifyFormat( 6307 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6308 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6309 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6310 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6311 ExtraSpaces); 6312 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6313 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6314 ExtraSpaces); 6315 verifyFormat( 6316 "someFunction(OtherParam,\n" 6317 " BracedList{ // comment 1 (Forcing interesting break)\n" 6318 " param1, param2,\n" 6319 " // comment 2\n" 6320 " param3, param4 });", 6321 ExtraSpaces); 6322 verifyFormat( 6323 "std::this_thread::sleep_for(\n" 6324 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6325 ExtraSpaces); 6326 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 6327 " aaaaaaa,\n" 6328 " aaaaaaaaaa,\n" 6329 " aaaaa,\n" 6330 " aaaaaaaaaaaaaaa,\n" 6331 " aaa,\n" 6332 " aaaaaaaaaa,\n" 6333 " a,\n" 6334 " aaaaaaaaaaaaaaaaaaaaa,\n" 6335 " aaaaaaaaaaaa,\n" 6336 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6337 " aaaaaaa,\n" 6338 " a};"); 6339 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6340 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 6341 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 6342 } 6343 6344 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6345 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6346 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6347 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6348 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6349 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6350 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6351 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6352 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6353 " 1, 22, 333, 4444, 55555, //\n" 6354 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6355 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6356 verifyFormat( 6357 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6358 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6359 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6360 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6361 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6362 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6363 " 7777777};"); 6364 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6365 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6366 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6367 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6368 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6369 " // Separating comment.\n" 6370 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6371 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6372 " // Leading comment\n" 6373 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6374 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6375 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6376 " 1, 1, 1, 1};", 6377 getLLVMStyleWithColumns(39)); 6378 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6379 " 1, 1, 1, 1};", 6380 getLLVMStyleWithColumns(38)); 6381 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6382 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6383 getLLVMStyleWithColumns(43)); 6384 verifyFormat( 6385 "static unsigned SomeValues[10][3] = {\n" 6386 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6387 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6388 verifyFormat("static auto fields = new vector<string>{\n" 6389 " \"aaaaaaaaaaaaa\",\n" 6390 " \"aaaaaaaaaaaaa\",\n" 6391 " \"aaaaaaaaaaaa\",\n" 6392 " \"aaaaaaaaaaaaaa\",\n" 6393 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6394 " \"aaaaaaaaaaaa\",\n" 6395 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6396 "};"); 6397 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6398 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6399 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6400 " 3, cccccccccccccccccccccc};", 6401 getLLVMStyleWithColumns(60)); 6402 6403 // Trailing commas. 6404 verifyFormat("vector<int> x = {\n" 6405 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6406 "};", 6407 getLLVMStyleWithColumns(39)); 6408 verifyFormat("vector<int> x = {\n" 6409 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6410 "};", 6411 getLLVMStyleWithColumns(39)); 6412 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6413 " 1, 1, 1, 1,\n" 6414 " /**/ /**/};", 6415 getLLVMStyleWithColumns(39)); 6416 6417 // Trailing comment in the first line. 6418 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6419 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6420 " 111111111, 222222222, 3333333333, 444444444, //\n" 6421 " 11111111, 22222222, 333333333, 44444444};"); 6422 // Trailing comment in the last line. 6423 verifyFormat("int aaaaa[] = {\n" 6424 " 1, 2, 3, // comment\n" 6425 " 4, 5, 6 // comment\n" 6426 "};"); 6427 6428 // With nested lists, we should either format one item per line or all nested 6429 // lists one on line. 6430 // FIXME: For some nested lists, we can do better. 6431 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6432 " {aaaaaaaaaaaaaaaaaaa},\n" 6433 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6434 " {aaaaaaaaaaaaaaaaa}};", 6435 getLLVMStyleWithColumns(60)); 6436 verifyFormat( 6437 "SomeStruct my_struct_array = {\n" 6438 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6439 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6440 " {aaa, aaa},\n" 6441 " {aaa, aaa},\n" 6442 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6443 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6444 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6445 6446 // No column layout should be used here. 6447 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6448 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6449 6450 verifyNoCrash("a<,"); 6451 6452 // No braced initializer here. 6453 verifyFormat("void f() {\n" 6454 " struct Dummy {};\n" 6455 " f(v);\n" 6456 "}"); 6457 6458 // Long lists should be formatted in columns even if they are nested. 6459 verifyFormat( 6460 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6461 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6462 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6463 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6464 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6465 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 6466 6467 // Allow "single-column" layout even if that violates the column limit. There 6468 // isn't going to be a better way. 6469 verifyFormat("std::vector<int> a = {\n" 6470 " aaaaaaaa,\n" 6471 " aaaaaaaa,\n" 6472 " aaaaaaaa,\n" 6473 " aaaaaaaa,\n" 6474 " aaaaaaaaaa,\n" 6475 " aaaaaaaa,\n" 6476 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 6477 getLLVMStyleWithColumns(30)); 6478 verifyFormat("vector<int> aaaa = {\n" 6479 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6480 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6481 " aaaaaa.aaaaaaa,\n" 6482 " aaaaaa.aaaaaaa,\n" 6483 " aaaaaa.aaaaaaa,\n" 6484 " aaaaaa.aaaaaaa,\n" 6485 "};"); 6486 6487 // Don't create hanging lists. 6488 verifyFormat("someFunction(Param, {List1, List2,\n" 6489 " List3});", 6490 getLLVMStyleWithColumns(35)); 6491 verifyFormat("someFunction(Param, Param,\n" 6492 " {List1, List2,\n" 6493 " List3});", 6494 getLLVMStyleWithColumns(35)); 6495 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 6496 " aaaaaaaaaaaaaaaaaaaaaaa);"); 6497 } 6498 6499 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6500 FormatStyle DoNotMerge = getLLVMStyle(); 6501 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6502 6503 verifyFormat("void f() { return 42; }"); 6504 verifyFormat("void f() {\n" 6505 " return 42;\n" 6506 "}", 6507 DoNotMerge); 6508 verifyFormat("void f() {\n" 6509 " // Comment\n" 6510 "}"); 6511 verifyFormat("{\n" 6512 "#error {\n" 6513 " int a;\n" 6514 "}"); 6515 verifyFormat("{\n" 6516 " int a;\n" 6517 "#error {\n" 6518 "}"); 6519 verifyFormat("void f() {} // comment"); 6520 verifyFormat("void f() { int a; } // comment"); 6521 verifyFormat("void f() {\n" 6522 "} // comment", 6523 DoNotMerge); 6524 verifyFormat("void f() {\n" 6525 " int a;\n" 6526 "} // comment", 6527 DoNotMerge); 6528 verifyFormat("void f() {\n" 6529 "} // comment", 6530 getLLVMStyleWithColumns(15)); 6531 6532 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6533 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6534 6535 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6536 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6537 verifyFormat("class C {\n" 6538 " C()\n" 6539 " : iiiiiiii(nullptr),\n" 6540 " kkkkkkk(nullptr),\n" 6541 " mmmmmmm(nullptr),\n" 6542 " nnnnnnn(nullptr) {}\n" 6543 "};", 6544 getGoogleStyle()); 6545 6546 FormatStyle NoColumnLimit = getLLVMStyle(); 6547 NoColumnLimit.ColumnLimit = 0; 6548 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6549 EXPECT_EQ("class C {\n" 6550 " A() : b(0) {}\n" 6551 "};", 6552 format("class C{A():b(0){}};", NoColumnLimit)); 6553 EXPECT_EQ("A()\n" 6554 " : b(0) {\n" 6555 "}", 6556 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6557 6558 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6559 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6560 FormatStyle::SFS_None; 6561 EXPECT_EQ("A()\n" 6562 " : b(0) {\n" 6563 "}", 6564 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6565 EXPECT_EQ("A()\n" 6566 " : b(0) {\n" 6567 "}", 6568 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6569 6570 verifyFormat("#define A \\\n" 6571 " void f() { \\\n" 6572 " int i; \\\n" 6573 " }", 6574 getLLVMStyleWithColumns(20)); 6575 verifyFormat("#define A \\\n" 6576 " void f() { int i; }", 6577 getLLVMStyleWithColumns(21)); 6578 verifyFormat("#define A \\\n" 6579 " void f() { \\\n" 6580 " int i; \\\n" 6581 " } \\\n" 6582 " int j;", 6583 getLLVMStyleWithColumns(22)); 6584 verifyFormat("#define A \\\n" 6585 " void f() { int i; } \\\n" 6586 " int j;", 6587 getLLVMStyleWithColumns(23)); 6588 } 6589 6590 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 6591 FormatStyle MergeEmptyOnly = getLLVMStyle(); 6592 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 6593 verifyFormat("class C {\n" 6594 " int f() {}\n" 6595 "};", 6596 MergeEmptyOnly); 6597 verifyFormat("class C {\n" 6598 " int f() {\n" 6599 " return 42;\n" 6600 " }\n" 6601 "};", 6602 MergeEmptyOnly); 6603 verifyFormat("int f() {}", MergeEmptyOnly); 6604 verifyFormat("int f() {\n" 6605 " return 42;\n" 6606 "}", 6607 MergeEmptyOnly); 6608 6609 // Also verify behavior when BraceWrapping.AfterFunction = true 6610 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 6611 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 6612 verifyFormat("int f() {}", MergeEmptyOnly); 6613 verifyFormat("class C {\n" 6614 " int f() {}\n" 6615 "};", 6616 MergeEmptyOnly); 6617 } 6618 6619 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6620 FormatStyle MergeInlineOnly = getLLVMStyle(); 6621 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6622 verifyFormat("class C {\n" 6623 " int f() { return 42; }\n" 6624 "};", 6625 MergeInlineOnly); 6626 verifyFormat("int f() {\n" 6627 " return 42;\n" 6628 "}", 6629 MergeInlineOnly); 6630 6631 // SFS_Inline implies SFS_Empty 6632 verifyFormat("class C {\n" 6633 " int f() {}\n" 6634 "};", 6635 MergeInlineOnly); 6636 verifyFormat("int f() {}", MergeInlineOnly); 6637 6638 // Also verify behavior when BraceWrapping.AfterFunction = true 6639 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 6640 MergeInlineOnly.BraceWrapping.AfterFunction = true; 6641 verifyFormat("class C {\n" 6642 " int f() { return 42; }\n" 6643 "};", 6644 MergeInlineOnly); 6645 verifyFormat("int f()\n" 6646 "{\n" 6647 " return 42;\n" 6648 "}", 6649 MergeInlineOnly); 6650 6651 // SFS_Inline implies SFS_Empty 6652 verifyFormat("int f() {}", MergeInlineOnly); 6653 verifyFormat("class C {\n" 6654 " int f() {}\n" 6655 "};", 6656 MergeInlineOnly); 6657 } 6658 6659 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 6660 FormatStyle MergeInlineOnly = getLLVMStyle(); 6661 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 6662 FormatStyle::SFS_InlineOnly; 6663 verifyFormat("class C {\n" 6664 " int f() { return 42; }\n" 6665 "};", 6666 MergeInlineOnly); 6667 verifyFormat("int f() {\n" 6668 " return 42;\n" 6669 "}", 6670 MergeInlineOnly); 6671 6672 // SFS_InlineOnly does not imply SFS_Empty 6673 verifyFormat("class C {\n" 6674 " int f() {}\n" 6675 "};", 6676 MergeInlineOnly); 6677 verifyFormat("int f() {\n" 6678 "}", 6679 MergeInlineOnly); 6680 6681 // Also verify behavior when BraceWrapping.AfterFunction = true 6682 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 6683 MergeInlineOnly.BraceWrapping.AfterFunction = true; 6684 verifyFormat("class C {\n" 6685 " int f() { return 42; }\n" 6686 "};", 6687 MergeInlineOnly); 6688 verifyFormat("int f()\n" 6689 "{\n" 6690 " return 42;\n" 6691 "}", 6692 MergeInlineOnly); 6693 6694 // SFS_InlineOnly does not imply SFS_Empty 6695 verifyFormat("int f()\n" 6696 "{\n" 6697 "}", 6698 MergeInlineOnly); 6699 verifyFormat("class C {\n" 6700 " int f() {}\n" 6701 "};", 6702 MergeInlineOnly); 6703 } 6704 6705 TEST_F(FormatTest, SplitEmptyFunction) { 6706 FormatStyle Style = getLLVMStyle(); 6707 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6708 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6709 Style.BraceWrapping.AfterFunction = true; 6710 Style.BraceWrapping.SplitEmptyFunction = false; 6711 Style.ColumnLimit = 40; 6712 6713 verifyFormat("int f()\n" 6714 "{}", 6715 Style); 6716 verifyFormat("int f()\n" 6717 "{\n" 6718 " return 42;\n" 6719 "}", 6720 Style); 6721 verifyFormat("int f()\n" 6722 "{\n" 6723 " // some comment\n" 6724 "}", 6725 Style); 6726 6727 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 6728 verifyFormat("int f() {}", Style); 6729 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6730 "{}", 6731 Style); 6732 verifyFormat("int f()\n" 6733 "{\n" 6734 " return 0;\n" 6735 "}", 6736 Style); 6737 6738 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6739 verifyFormat("class Foo {\n" 6740 " int f() {}\n" 6741 "};\n", 6742 Style); 6743 verifyFormat("class Foo {\n" 6744 " int f() { return 0; }\n" 6745 "};\n", 6746 Style); 6747 verifyFormat("class Foo {\n" 6748 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6749 " {}\n" 6750 "};\n", 6751 Style); 6752 verifyFormat("class Foo {\n" 6753 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6754 " {\n" 6755 " return 0;\n" 6756 " }\n" 6757 "};\n", 6758 Style); 6759 6760 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6761 verifyFormat("int f() {}", Style); 6762 verifyFormat("int f() { return 0; }", Style); 6763 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6764 "{}", 6765 Style); 6766 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 6767 "{\n" 6768 " return 0;\n" 6769 "}", 6770 Style); 6771 } 6772 6773 TEST_F(FormatTest, SplitEmptyClass) { 6774 FormatStyle Style = getLLVMStyle(); 6775 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6776 Style.BraceWrapping.AfterClass = true; 6777 Style.BraceWrapping.SplitEmptyRecord = false; 6778 6779 verifyFormat("class Foo\n" 6780 "{};", 6781 Style); 6782 verifyFormat("/* something */ class Foo\n" 6783 "{};", 6784 Style); 6785 verifyFormat("template <typename X> class Foo\n" 6786 "{};", 6787 Style); 6788 verifyFormat("class Foo\n" 6789 "{\n" 6790 " Foo();\n" 6791 "};", 6792 Style); 6793 verifyFormat("typedef class Foo\n" 6794 "{\n" 6795 "} Foo_t;", 6796 Style); 6797 } 6798 6799 TEST_F(FormatTest, SplitEmptyStruct) { 6800 FormatStyle Style = getLLVMStyle(); 6801 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6802 Style.BraceWrapping.AfterStruct = true; 6803 Style.BraceWrapping.SplitEmptyRecord = false; 6804 6805 verifyFormat("struct Foo\n" 6806 "{};", 6807 Style); 6808 verifyFormat("/* something */ struct Foo\n" 6809 "{};", 6810 Style); 6811 verifyFormat("template <typename X> struct Foo\n" 6812 "{};", 6813 Style); 6814 verifyFormat("struct Foo\n" 6815 "{\n" 6816 " Foo();\n" 6817 "};", 6818 Style); 6819 verifyFormat("typedef struct Foo\n" 6820 "{\n" 6821 "} Foo_t;", 6822 Style); 6823 //typedef struct Bar {} Bar_t; 6824 } 6825 6826 TEST_F(FormatTest, SplitEmptyUnion) { 6827 FormatStyle Style = getLLVMStyle(); 6828 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6829 Style.BraceWrapping.AfterUnion = true; 6830 Style.BraceWrapping.SplitEmptyRecord = false; 6831 6832 verifyFormat("union Foo\n" 6833 "{};", 6834 Style); 6835 verifyFormat("/* something */ union Foo\n" 6836 "{};", 6837 Style); 6838 verifyFormat("union Foo\n" 6839 "{\n" 6840 " A,\n" 6841 "};", 6842 Style); 6843 verifyFormat("typedef union Foo\n" 6844 "{\n" 6845 "} Foo_t;", 6846 Style); 6847 } 6848 6849 TEST_F(FormatTest, SplitEmptyNamespace) { 6850 FormatStyle Style = getLLVMStyle(); 6851 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6852 Style.BraceWrapping.AfterNamespace = true; 6853 Style.BraceWrapping.SplitEmptyNamespace = false; 6854 6855 verifyFormat("namespace Foo\n" 6856 "{};", 6857 Style); 6858 verifyFormat("/* something */ namespace Foo\n" 6859 "{};", 6860 Style); 6861 verifyFormat("inline namespace Foo\n" 6862 "{};", 6863 Style); 6864 verifyFormat("namespace Foo\n" 6865 "{\n" 6866 "void Bar();\n" 6867 "};", 6868 Style); 6869 } 6870 6871 TEST_F(FormatTest, NeverMergeShortRecords) { 6872 FormatStyle Style = getLLVMStyle(); 6873 6874 verifyFormat("class Foo {\n" 6875 " Foo();\n" 6876 "};", 6877 Style); 6878 verifyFormat("typedef class Foo {\n" 6879 " Foo();\n" 6880 "} Foo_t;", 6881 Style); 6882 verifyFormat("struct Foo {\n" 6883 " Foo();\n" 6884 "};", 6885 Style); 6886 verifyFormat("typedef struct Foo {\n" 6887 " Foo();\n" 6888 "} Foo_t;", 6889 Style); 6890 verifyFormat("union Foo {\n" 6891 " A,\n" 6892 "};", 6893 Style); 6894 verifyFormat("typedef union Foo {\n" 6895 " A,\n" 6896 "} Foo_t;", 6897 Style); 6898 verifyFormat("namespace Foo {\n" 6899 "void Bar();\n" 6900 "};", 6901 Style); 6902 6903 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 6904 Style.BraceWrapping.AfterClass = true; 6905 Style.BraceWrapping.AfterStruct = true; 6906 Style.BraceWrapping.AfterUnion = true; 6907 Style.BraceWrapping.AfterNamespace = true; 6908 verifyFormat("class Foo\n" 6909 "{\n" 6910 " Foo();\n" 6911 "};", 6912 Style); 6913 verifyFormat("typedef class Foo\n" 6914 "{\n" 6915 " Foo();\n" 6916 "} Foo_t;", 6917 Style); 6918 verifyFormat("struct Foo\n" 6919 "{\n" 6920 " Foo();\n" 6921 "};", 6922 Style); 6923 verifyFormat("typedef struct Foo\n" 6924 "{\n" 6925 " Foo();\n" 6926 "} Foo_t;", 6927 Style); 6928 verifyFormat("union Foo\n" 6929 "{\n" 6930 " A,\n" 6931 "};", 6932 Style); 6933 verifyFormat("typedef union Foo\n" 6934 "{\n" 6935 " A,\n" 6936 "} Foo_t;", 6937 Style); 6938 verifyFormat("namespace Foo\n" 6939 "{\n" 6940 "void Bar();\n" 6941 "};", 6942 Style); 6943 } 6944 6945 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6946 // Elaborate type variable declarations. 6947 verifyFormat("struct foo a = {bar};\nint n;"); 6948 verifyFormat("class foo a = {bar};\nint n;"); 6949 verifyFormat("union foo a = {bar};\nint n;"); 6950 6951 // Elaborate types inside function definitions. 6952 verifyFormat("struct foo f() {}\nint n;"); 6953 verifyFormat("class foo f() {}\nint n;"); 6954 verifyFormat("union foo f() {}\nint n;"); 6955 6956 // Templates. 6957 verifyFormat("template <class X> void f() {}\nint n;"); 6958 verifyFormat("template <struct X> void f() {}\nint n;"); 6959 verifyFormat("template <union X> void f() {}\nint n;"); 6960 6961 // Actual definitions... 6962 verifyFormat("struct {\n} n;"); 6963 verifyFormat( 6964 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6965 verifyFormat("union Z {\n int n;\n} x;"); 6966 verifyFormat("class MACRO Z {\n} n;"); 6967 verifyFormat("class MACRO(X) Z {\n} n;"); 6968 verifyFormat("class __attribute__(X) Z {\n} n;"); 6969 verifyFormat("class __declspec(X) Z {\n} n;"); 6970 verifyFormat("class A##B##C {\n} n;"); 6971 verifyFormat("class alignas(16) Z {\n} n;"); 6972 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6973 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6974 6975 // Redefinition from nested context: 6976 verifyFormat("class A::B::C {\n} n;"); 6977 6978 // Template definitions. 6979 verifyFormat( 6980 "template <typename F>\n" 6981 "Matcher(const Matcher<F> &Other,\n" 6982 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6983 " !is_same<F, T>::value>::type * = 0)\n" 6984 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6985 6986 // FIXME: This is still incorrectly handled at the formatter side. 6987 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6988 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6989 6990 // FIXME: 6991 // This now gets parsed incorrectly as class definition. 6992 // verifyFormat("class A<int> f() {\n}\nint n;"); 6993 6994 // Elaborate types where incorrectly parsing the structural element would 6995 // break the indent. 6996 verifyFormat("if (true)\n" 6997 " class X x;\n" 6998 "else\n" 6999 " f();\n"); 7000 7001 // This is simply incomplete. Formatting is not important, but must not crash. 7002 verifyFormat("class A:"); 7003 } 7004 7005 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 7006 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 7007 format("#error Leave all white!!!!! space* alone!\n")); 7008 EXPECT_EQ( 7009 "#warning Leave all white!!!!! space* alone!\n", 7010 format("#warning Leave all white!!!!! space* alone!\n")); 7011 EXPECT_EQ("#error 1", format(" # error 1")); 7012 EXPECT_EQ("#warning 1", format(" # warning 1")); 7013 } 7014 7015 TEST_F(FormatTest, FormatHashIfExpressions) { 7016 verifyFormat("#if AAAA && BBBB"); 7017 verifyFormat("#if (AAAA && BBBB)"); 7018 verifyFormat("#elif (AAAA && BBBB)"); 7019 // FIXME: Come up with a better indentation for #elif. 7020 verifyFormat( 7021 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 7022 " defined(BBBBBBBB)\n" 7023 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 7024 " defined(BBBBBBBB)\n" 7025 "#endif", 7026 getLLVMStyleWithColumns(65)); 7027 } 7028 7029 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 7030 FormatStyle AllowsMergedIf = getGoogleStyle(); 7031 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 7032 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 7033 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 7034 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 7035 EXPECT_EQ("if (true) return 42;", 7036 format("if (true)\nreturn 42;", AllowsMergedIf)); 7037 FormatStyle ShortMergedIf = AllowsMergedIf; 7038 ShortMergedIf.ColumnLimit = 25; 7039 verifyFormat("#define A \\\n" 7040 " if (true) return 42;", 7041 ShortMergedIf); 7042 verifyFormat("#define A \\\n" 7043 " f(); \\\n" 7044 " if (true)\n" 7045 "#define B", 7046 ShortMergedIf); 7047 verifyFormat("#define A \\\n" 7048 " f(); \\\n" 7049 " if (true)\n" 7050 "g();", 7051 ShortMergedIf); 7052 verifyFormat("{\n" 7053 "#ifdef A\n" 7054 " // Comment\n" 7055 " if (true) continue;\n" 7056 "#endif\n" 7057 " // Comment\n" 7058 " if (true) continue;\n" 7059 "}", 7060 ShortMergedIf); 7061 ShortMergedIf.ColumnLimit = 33; 7062 verifyFormat("#define A \\\n" 7063 " if constexpr (true) return 42;", 7064 ShortMergedIf); 7065 ShortMergedIf.ColumnLimit = 29; 7066 verifyFormat("#define A \\\n" 7067 " if (aaaaaaaaaa) return 1; \\\n" 7068 " return 2;", 7069 ShortMergedIf); 7070 ShortMergedIf.ColumnLimit = 28; 7071 verifyFormat("#define A \\\n" 7072 " if (aaaaaaaaaa) \\\n" 7073 " return 1; \\\n" 7074 " return 2;", 7075 ShortMergedIf); 7076 verifyFormat("#define A \\\n" 7077 " if constexpr (aaaaaaa) \\\n" 7078 " return 1; \\\n" 7079 " return 2;", 7080 ShortMergedIf); 7081 } 7082 7083 TEST_F(FormatTest, FormatStarDependingOnContext) { 7084 verifyFormat("void f(int *a);"); 7085 verifyFormat("void f() { f(fint * b); }"); 7086 verifyFormat("class A {\n void f(int *a);\n};"); 7087 verifyFormat("class A {\n int *a;\n};"); 7088 verifyFormat("namespace a {\n" 7089 "namespace b {\n" 7090 "class A {\n" 7091 " void f() {}\n" 7092 " int *a;\n" 7093 "};\n" 7094 "} // namespace b\n" 7095 "} // namespace a"); 7096 } 7097 7098 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 7099 verifyFormat("while"); 7100 verifyFormat("operator"); 7101 } 7102 7103 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 7104 // This code would be painfully slow to format if we didn't skip it. 7105 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 7106 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7107 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7108 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7109 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 7110 "A(1, 1)\n" 7111 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 7112 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7113 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7114 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7115 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7116 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7117 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7118 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7119 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 7120 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 7121 // Deeply nested part is untouched, rest is formatted. 7122 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 7123 format(std::string("int i;\n") + Code + "int j;\n", 7124 getLLVMStyle(), SC_ExpectIncomplete)); 7125 } 7126 7127 //===----------------------------------------------------------------------===// 7128 // Objective-C tests. 7129 //===----------------------------------------------------------------------===// 7130 7131 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 7132 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 7133 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 7134 format("-(NSUInteger)indexOfObject:(id)anObject;")); 7135 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 7136 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 7137 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 7138 format("-(NSInteger)Method3:(id)anObject;")); 7139 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 7140 format("-(NSInteger)Method4:(id)anObject;")); 7141 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 7142 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 7143 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 7144 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 7145 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7146 "forAllCells:(BOOL)flag;", 7147 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 7148 "forAllCells:(BOOL)flag;")); 7149 7150 // Very long objectiveC method declaration. 7151 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 7152 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 7153 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 7154 " inRange:(NSRange)range\n" 7155 " outRange:(NSRange)out_range\n" 7156 " outRange1:(NSRange)out_range1\n" 7157 " outRange2:(NSRange)out_range2\n" 7158 " outRange3:(NSRange)out_range3\n" 7159 " outRange4:(NSRange)out_range4\n" 7160 " outRange5:(NSRange)out_range5\n" 7161 " outRange6:(NSRange)out_range6\n" 7162 " outRange7:(NSRange)out_range7\n" 7163 " outRange8:(NSRange)out_range8\n" 7164 " outRange9:(NSRange)out_range9;"); 7165 7166 // When the function name has to be wrapped. 7167 FormatStyle Style = getLLVMStyle(); 7168 Style.IndentWrappedFunctionNames = false; 7169 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7170 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7171 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7172 "}", 7173 Style); 7174 Style.IndentWrappedFunctionNames = true; 7175 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 7176 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 7177 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 7178 "}", 7179 Style); 7180 7181 verifyFormat("- (int)sum:(vector<int>)numbers;"); 7182 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 7183 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 7184 // protocol lists (but not for template classes): 7185 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 7186 7187 verifyFormat("- (int (*)())foo:(int (*)())f;"); 7188 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 7189 7190 // If there's no return type (very rare in practice!), LLVM and Google style 7191 // agree. 7192 verifyFormat("- foo;"); 7193 verifyFormat("- foo:(int)f;"); 7194 verifyGoogleFormat("- foo:(int)foo;"); 7195 } 7196 7197 7198 TEST_F(FormatTest, BreaksStringLiterals) { 7199 EXPECT_EQ("\"some text \"\n" 7200 "\"other\";", 7201 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7202 EXPECT_EQ("\"some text \"\n" 7203 "\"other\";", 7204 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7205 EXPECT_EQ( 7206 "#define A \\\n" 7207 " \"some \" \\\n" 7208 " \"text \" \\\n" 7209 " \"other\";", 7210 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7211 EXPECT_EQ( 7212 "#define A \\\n" 7213 " \"so \" \\\n" 7214 " \"text \" \\\n" 7215 " \"other\";", 7216 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7217 7218 EXPECT_EQ("\"some text\"", 7219 format("\"some text\"", getLLVMStyleWithColumns(1))); 7220 EXPECT_EQ("\"some text\"", 7221 format("\"some text\"", getLLVMStyleWithColumns(11))); 7222 EXPECT_EQ("\"some \"\n" 7223 "\"text\"", 7224 format("\"some text\"", getLLVMStyleWithColumns(10))); 7225 EXPECT_EQ("\"some \"\n" 7226 "\"text\"", 7227 format("\"some text\"", getLLVMStyleWithColumns(7))); 7228 EXPECT_EQ("\"some\"\n" 7229 "\" tex\"\n" 7230 "\"t\"", 7231 format("\"some text\"", getLLVMStyleWithColumns(6))); 7232 EXPECT_EQ("\"some\"\n" 7233 "\" tex\"\n" 7234 "\" and\"", 7235 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7236 EXPECT_EQ("\"some\"\n" 7237 "\"/tex\"\n" 7238 "\"/and\"", 7239 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7240 7241 EXPECT_EQ("variable =\n" 7242 " \"long string \"\n" 7243 " \"literal\";", 7244 format("variable = \"long string literal\";", 7245 getLLVMStyleWithColumns(20))); 7246 7247 EXPECT_EQ("variable = f(\n" 7248 " \"long string \"\n" 7249 " \"literal\",\n" 7250 " short,\n" 7251 " loooooooooooooooooooong);", 7252 format("variable = f(\"long string literal\", short, " 7253 "loooooooooooooooooooong);", 7254 getLLVMStyleWithColumns(20))); 7255 7256 EXPECT_EQ( 7257 "f(g(\"long string \"\n" 7258 " \"literal\"),\n" 7259 " b);", 7260 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7261 EXPECT_EQ("f(g(\"long string \"\n" 7262 " \"literal\",\n" 7263 " a),\n" 7264 " b);", 7265 format("f(g(\"long string literal\", a), b);", 7266 getLLVMStyleWithColumns(20))); 7267 EXPECT_EQ( 7268 "f(\"one two\".split(\n" 7269 " variable));", 7270 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7271 EXPECT_EQ("f(\"one two three four five six \"\n" 7272 " \"seven\".split(\n" 7273 " really_looooong_variable));", 7274 format("f(\"one two three four five six seven\"." 7275 "split(really_looooong_variable));", 7276 getLLVMStyleWithColumns(33))); 7277 7278 EXPECT_EQ("f(\"some \"\n" 7279 " \"text\",\n" 7280 " other);", 7281 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7282 7283 // Only break as a last resort. 7284 verifyFormat( 7285 "aaaaaaaaaaaaaaaaaaaa(\n" 7286 " aaaaaaaaaaaaaaaaaaaa,\n" 7287 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7288 7289 EXPECT_EQ("\"splitmea\"\n" 7290 "\"trandomp\"\n" 7291 "\"oint\"", 7292 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7293 7294 EXPECT_EQ("\"split/\"\n" 7295 "\"pathat/\"\n" 7296 "\"slashes\"", 7297 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7298 7299 EXPECT_EQ("\"split/\"\n" 7300 "\"pathat/\"\n" 7301 "\"slashes\"", 7302 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7303 EXPECT_EQ("\"split at \"\n" 7304 "\"spaces/at/\"\n" 7305 "\"slashes.at.any$\"\n" 7306 "\"non-alphanumeric%\"\n" 7307 "\"1111111111characte\"\n" 7308 "\"rs\"", 7309 format("\"split at " 7310 "spaces/at/" 7311 "slashes.at." 7312 "any$non-" 7313 "alphanumeric%" 7314 "1111111111characte" 7315 "rs\"", 7316 getLLVMStyleWithColumns(20))); 7317 7318 // Verify that splitting the strings understands 7319 // Style::AlwaysBreakBeforeMultilineStrings. 7320 EXPECT_EQ( 7321 "aaaaaaaaaaaa(\n" 7322 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7323 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7324 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 7325 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7326 "aaaaaaaaaaaaaaaaaaaaaa\");", 7327 getGoogleStyle())); 7328 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7329 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7330 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7331 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7332 "aaaaaaaaaaaaaaaaaaaaaa\";", 7333 getGoogleStyle())); 7334 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7335 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7336 format("llvm::outs() << " 7337 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7338 "aaaaaaaaaaaaaaaaaaa\";")); 7339 EXPECT_EQ("ffff(\n" 7340 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7341 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7342 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7343 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7344 getGoogleStyle())); 7345 7346 FormatStyle Style = getLLVMStyleWithColumns(12); 7347 Style.BreakStringLiterals = false; 7348 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 7349 7350 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7351 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 7352 EXPECT_EQ("#define A \\\n" 7353 " \"some \" \\\n" 7354 " \"text \" \\\n" 7355 " \"other\";", 7356 format("#define A \"some text other\";", AlignLeft)); 7357 } 7358 7359 TEST_F(FormatTest, FullyRemoveEmptyLines) { 7360 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 7361 NoEmptyLines.MaxEmptyLinesToKeep = 0; 7362 EXPECT_EQ("int i = a(b());", 7363 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 7364 } 7365 7366 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7367 EXPECT_EQ( 7368 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7369 "(\n" 7370 " \"x\t\");", 7371 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7372 "aaaaaaa(" 7373 "\"x\t\");")); 7374 } 7375 7376 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7377 EXPECT_EQ( 7378 "u8\"utf8 string \"\n" 7379 "u8\"literal\";", 7380 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7381 EXPECT_EQ( 7382 "u\"utf16 string \"\n" 7383 "u\"literal\";", 7384 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7385 EXPECT_EQ( 7386 "U\"utf32 string \"\n" 7387 "U\"literal\";", 7388 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7389 EXPECT_EQ("L\"wide string \"\n" 7390 "L\"literal\";", 7391 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7392 EXPECT_EQ("@\"NSString \"\n" 7393 "@\"literal\";", 7394 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7395 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 7396 7397 // This input makes clang-format try to split the incomplete unicode escape 7398 // sequence, which used to lead to a crasher. 7399 verifyNoCrash( 7400 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7401 getLLVMStyleWithColumns(60)); 7402 } 7403 7404 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7405 FormatStyle Style = getGoogleStyleWithColumns(15); 7406 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7407 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7408 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7409 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7410 EXPECT_EQ("u8R\"x(raw literal)x\";", 7411 format("u8R\"x(raw literal)x\";", Style)); 7412 } 7413 7414 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7415 FormatStyle Style = getLLVMStyleWithColumns(20); 7416 EXPECT_EQ( 7417 "_T(\"aaaaaaaaaaaaaa\")\n" 7418 "_T(\"aaaaaaaaaaaaaa\")\n" 7419 "_T(\"aaaaaaaaaaaa\")", 7420 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7421 EXPECT_EQ("f(x,\n" 7422 " _T(\"aaaaaaaaaaaa\")\n" 7423 " _T(\"aaa\"),\n" 7424 " z);", 7425 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7426 7427 // FIXME: Handle embedded spaces in one iteration. 7428 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7429 // "_T(\"aaaaaaaaaaaaa\")\n" 7430 // "_T(\"aaaaaaaaaaaaa\")\n" 7431 // "_T(\"a\")", 7432 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7433 // getLLVMStyleWithColumns(20))); 7434 EXPECT_EQ( 7435 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7436 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7437 EXPECT_EQ("f(\n" 7438 "#if !TEST\n" 7439 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7440 "#endif\n" 7441 ");", 7442 format("f(\n" 7443 "#if !TEST\n" 7444 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7445 "#endif\n" 7446 ");")); 7447 EXPECT_EQ("f(\n" 7448 "\n" 7449 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7450 format("f(\n" 7451 "\n" 7452 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7453 } 7454 7455 TEST_F(FormatTest, BreaksStringLiteralOperands) { 7456 // In a function call with two operands, the second can be broken with no line 7457 // break before it. 7458 EXPECT_EQ("func(a, \"long long \"\n" 7459 " \"long long\");", 7460 format("func(a, \"long long long long\");", 7461 getLLVMStyleWithColumns(24))); 7462 // In a function call with three operands, the second must be broken with a 7463 // line break before it. 7464 EXPECT_EQ("func(a,\n" 7465 " \"long long long \"\n" 7466 " \"long\",\n" 7467 " c);", 7468 format("func(a, \"long long long long\", c);", 7469 getLLVMStyleWithColumns(24))); 7470 // In a function call with three operands, the third must be broken with a 7471 // line break before it. 7472 EXPECT_EQ("func(a, b,\n" 7473 " \"long long long \"\n" 7474 " \"long\");", 7475 format("func(a, b, \"long long long long\");", 7476 getLLVMStyleWithColumns(24))); 7477 // In a function call with three operands, both the second and the third must 7478 // be broken with a line break before them. 7479 EXPECT_EQ("func(a,\n" 7480 " \"long long long \"\n" 7481 " \"long\",\n" 7482 " \"long long long \"\n" 7483 " \"long\");", 7484 format("func(a, \"long long long long\", \"long long long long\");", 7485 getLLVMStyleWithColumns(24))); 7486 // In a chain of << with two operands, the second can be broken with no line 7487 // break before it. 7488 EXPECT_EQ("a << \"line line \"\n" 7489 " \"line\";", 7490 format("a << \"line line line\";", 7491 getLLVMStyleWithColumns(20))); 7492 // In a chain of << with three operands, the second can be broken with no line 7493 // break before it. 7494 EXPECT_EQ("abcde << \"line \"\n" 7495 " \"line line\"\n" 7496 " << c;", 7497 format("abcde << \"line line line\" << c;", 7498 getLLVMStyleWithColumns(20))); 7499 // In a chain of << with three operands, the third must be broken with a line 7500 // break before it. 7501 EXPECT_EQ("a << b\n" 7502 " << \"line line \"\n" 7503 " \"line\";", 7504 format("a << b << \"line line line\";", 7505 getLLVMStyleWithColumns(20))); 7506 // In a chain of << with three operands, the second can be broken with no line 7507 // break before it and the third must be broken with a line break before it. 7508 EXPECT_EQ("abcd << \"line line \"\n" 7509 " \"line\"\n" 7510 " << \"line line \"\n" 7511 " \"line\";", 7512 format("abcd << \"line line line\" << \"line line line\";", 7513 getLLVMStyleWithColumns(20))); 7514 // In a chain of binary operators with two operands, the second can be broken 7515 // with no line break before it. 7516 EXPECT_EQ("abcd + \"line line \"\n" 7517 " \"line line\";", 7518 format("abcd + \"line line line line\";", 7519 getLLVMStyleWithColumns(20))); 7520 // In a chain of binary operators with three operands, the second must be 7521 // broken with a line break before it. 7522 EXPECT_EQ("abcd +\n" 7523 " \"line line \"\n" 7524 " \"line line\" +\n" 7525 " e;", 7526 format("abcd + \"line line line line\" + e;", 7527 getLLVMStyleWithColumns(20))); 7528 // In a function call with two operands, with AlignAfterOpenBracket enabled, 7529 // the first must be broken with a line break before it. 7530 FormatStyle Style = getLLVMStyleWithColumns(25); 7531 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7532 EXPECT_EQ("someFunction(\n" 7533 " \"long long long \"\n" 7534 " \"long\",\n" 7535 " a);", 7536 format("someFunction(\"long long long long\", a);", Style)); 7537 } 7538 7539 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 7540 EXPECT_EQ( 7541 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7542 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7543 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7544 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7545 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7546 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 7547 } 7548 7549 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 7550 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 7551 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 7552 EXPECT_EQ("fffffffffff(g(R\"x(\n" 7553 "multiline raw string literal xxxxxxxxxxxxxx\n" 7554 ")x\",\n" 7555 " a),\n" 7556 " b);", 7557 format("fffffffffff(g(R\"x(\n" 7558 "multiline raw string literal xxxxxxxxxxxxxx\n" 7559 ")x\", a), b);", 7560 getGoogleStyleWithColumns(20))); 7561 EXPECT_EQ("fffffffffff(\n" 7562 " g(R\"x(qqq\n" 7563 "multiline raw string literal xxxxxxxxxxxxxx\n" 7564 ")x\",\n" 7565 " a),\n" 7566 " b);", 7567 format("fffffffffff(g(R\"x(qqq\n" 7568 "multiline raw string literal xxxxxxxxxxxxxx\n" 7569 ")x\", a), b);", 7570 getGoogleStyleWithColumns(20))); 7571 7572 EXPECT_EQ("fffffffffff(R\"x(\n" 7573 "multiline raw string literal xxxxxxxxxxxxxx\n" 7574 ")x\");", 7575 format("fffffffffff(R\"x(\n" 7576 "multiline raw string literal xxxxxxxxxxxxxx\n" 7577 ")x\");", 7578 getGoogleStyleWithColumns(20))); 7579 EXPECT_EQ("fffffffffff(R\"x(\n" 7580 "multiline raw string literal xxxxxxxxxxxxxx\n" 7581 ")x\" + bbbbbb);", 7582 format("fffffffffff(R\"x(\n" 7583 "multiline raw string literal xxxxxxxxxxxxxx\n" 7584 ")x\" + bbbbbb);", 7585 getGoogleStyleWithColumns(20))); 7586 EXPECT_EQ("fffffffffff(\n" 7587 " R\"x(\n" 7588 "multiline raw string literal xxxxxxxxxxxxxx\n" 7589 ")x\" +\n" 7590 " bbbbbb);", 7591 format("fffffffffff(\n" 7592 " R\"x(\n" 7593 "multiline raw string literal xxxxxxxxxxxxxx\n" 7594 ")x\" + bbbbbb);", 7595 getGoogleStyleWithColumns(20))); 7596 } 7597 7598 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 7599 verifyFormat("string a = \"unterminated;"); 7600 EXPECT_EQ("function(\"unterminated,\n" 7601 " OtherParameter);", 7602 format("function( \"unterminated,\n" 7603 " OtherParameter);")); 7604 } 7605 7606 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 7607 FormatStyle Style = getLLVMStyle(); 7608 Style.Standard = FormatStyle::LS_Cpp03; 7609 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 7610 format("#define x(_a) printf(\"foo\"_a);", Style)); 7611 } 7612 7613 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 7614 7615 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 7616 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 7617 " \"ddeeefff\");", 7618 format("someFunction(\"aaabbbcccdddeeefff\");", 7619 getLLVMStyleWithColumns(25))); 7620 EXPECT_EQ("someFunction1234567890(\n" 7621 " \"aaabbbcccdddeeefff\");", 7622 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7623 getLLVMStyleWithColumns(26))); 7624 EXPECT_EQ("someFunction1234567890(\n" 7625 " \"aaabbbcccdddeeeff\"\n" 7626 " \"f\");", 7627 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7628 getLLVMStyleWithColumns(25))); 7629 EXPECT_EQ("someFunction1234567890(\n" 7630 " \"aaabbbcccdddeeeff\"\n" 7631 " \"f\");", 7632 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 7633 getLLVMStyleWithColumns(24))); 7634 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7635 " \"ddde \"\n" 7636 " \"efff\");", 7637 format("someFunction(\"aaabbbcc ddde efff\");", 7638 getLLVMStyleWithColumns(25))); 7639 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 7640 " \"ddeeefff\");", 7641 format("someFunction(\"aaabbbccc ddeeefff\");", 7642 getLLVMStyleWithColumns(25))); 7643 EXPECT_EQ("someFunction1234567890(\n" 7644 " \"aaabb \"\n" 7645 " \"cccdddeeefff\");", 7646 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 7647 getLLVMStyleWithColumns(25))); 7648 EXPECT_EQ("#define A \\\n" 7649 " string s = \\\n" 7650 " \"123456789\" \\\n" 7651 " \"0\"; \\\n" 7652 " int i;", 7653 format("#define A string s = \"1234567890\"; int i;", 7654 getLLVMStyleWithColumns(20))); 7655 // FIXME: Put additional penalties on breaking at non-whitespace locations. 7656 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 7657 " \"dddeeeff\"\n" 7658 " \"f\");", 7659 format("someFunction(\"aaabbbcc dddeeefff\");", 7660 getLLVMStyleWithColumns(25))); 7661 } 7662 7663 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 7664 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 7665 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 7666 EXPECT_EQ("\"test\"\n" 7667 "\"\\n\"", 7668 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 7669 EXPECT_EQ("\"tes\\\\\"\n" 7670 "\"n\"", 7671 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 7672 EXPECT_EQ("\"\\\\\\\\\"\n" 7673 "\"\\n\"", 7674 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 7675 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 7676 EXPECT_EQ("\"\\uff01\"\n" 7677 "\"test\"", 7678 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 7679 EXPECT_EQ("\"\\Uff01ff02\"", 7680 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 7681 EXPECT_EQ("\"\\x000000000001\"\n" 7682 "\"next\"", 7683 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 7684 EXPECT_EQ("\"\\x000000000001next\"", 7685 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 7686 EXPECT_EQ("\"\\x000000000001\"", 7687 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 7688 EXPECT_EQ("\"test\"\n" 7689 "\"\\000000\"\n" 7690 "\"000001\"", 7691 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 7692 EXPECT_EQ("\"test\\000\"\n" 7693 "\"00000000\"\n" 7694 "\"1\"", 7695 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 7696 } 7697 7698 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 7699 verifyFormat("void f() {\n" 7700 " return g() {}\n" 7701 " void h() {}"); 7702 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 7703 "g();\n" 7704 "}"); 7705 } 7706 7707 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 7708 verifyFormat( 7709 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 7710 } 7711 7712 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 7713 verifyFormat("class X {\n" 7714 " void f() {\n" 7715 " }\n" 7716 "};", 7717 getLLVMStyleWithColumns(12)); 7718 } 7719 7720 TEST_F(FormatTest, ConfigurableIndentWidth) { 7721 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 7722 EightIndent.IndentWidth = 8; 7723 EightIndent.ContinuationIndentWidth = 8; 7724 verifyFormat("void f() {\n" 7725 " someFunction();\n" 7726 " if (true) {\n" 7727 " f();\n" 7728 " }\n" 7729 "}", 7730 EightIndent); 7731 verifyFormat("class X {\n" 7732 " void f() {\n" 7733 " }\n" 7734 "};", 7735 EightIndent); 7736 verifyFormat("int x[] = {\n" 7737 " call(),\n" 7738 " call()};", 7739 EightIndent); 7740 } 7741 7742 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 7743 verifyFormat("double\n" 7744 "f();", 7745 getLLVMStyleWithColumns(8)); 7746 } 7747 7748 TEST_F(FormatTest, ConfigurableUseOfTab) { 7749 FormatStyle Tab = getLLVMStyleWithColumns(42); 7750 Tab.IndentWidth = 8; 7751 Tab.UseTab = FormatStyle::UT_Always; 7752 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 7753 7754 EXPECT_EQ("if (aaaaaaaa && // q\n" 7755 " bb)\t\t// w\n" 7756 "\t;", 7757 format("if (aaaaaaaa &&// q\n" 7758 "bb)// w\n" 7759 ";", 7760 Tab)); 7761 EXPECT_EQ("if (aaa && bbb) // w\n" 7762 "\t;", 7763 format("if(aaa&&bbb)// w\n" 7764 ";", 7765 Tab)); 7766 7767 verifyFormat("class X {\n" 7768 "\tvoid f() {\n" 7769 "\t\tsomeFunction(parameter1,\n" 7770 "\t\t\t parameter2);\n" 7771 "\t}\n" 7772 "};", 7773 Tab); 7774 verifyFormat("#define A \\\n" 7775 "\tvoid f() { \\\n" 7776 "\t\tsomeFunction( \\\n" 7777 "\t\t parameter1, \\\n" 7778 "\t\t parameter2); \\\n" 7779 "\t}", 7780 Tab); 7781 7782 Tab.TabWidth = 4; 7783 Tab.IndentWidth = 8; 7784 verifyFormat("class TabWidth4Indent8 {\n" 7785 "\t\tvoid f() {\n" 7786 "\t\t\t\tsomeFunction(parameter1,\n" 7787 "\t\t\t\t\t\t\t parameter2);\n" 7788 "\t\t}\n" 7789 "};", 7790 Tab); 7791 7792 Tab.TabWidth = 4; 7793 Tab.IndentWidth = 4; 7794 verifyFormat("class TabWidth4Indent4 {\n" 7795 "\tvoid f() {\n" 7796 "\t\tsomeFunction(parameter1,\n" 7797 "\t\t\t\t\t parameter2);\n" 7798 "\t}\n" 7799 "};", 7800 Tab); 7801 7802 Tab.TabWidth = 8; 7803 Tab.IndentWidth = 4; 7804 verifyFormat("class TabWidth8Indent4 {\n" 7805 " void f() {\n" 7806 "\tsomeFunction(parameter1,\n" 7807 "\t\t parameter2);\n" 7808 " }\n" 7809 "};", 7810 Tab); 7811 7812 Tab.TabWidth = 8; 7813 Tab.IndentWidth = 8; 7814 EXPECT_EQ("/*\n" 7815 "\t a\t\tcomment\n" 7816 "\t in multiple lines\n" 7817 " */", 7818 format(" /*\t \t \n" 7819 " \t \t a\t\tcomment\t \t\n" 7820 " \t \t in multiple lines\t\n" 7821 " \t */", 7822 Tab)); 7823 7824 Tab.UseTab = FormatStyle::UT_ForIndentation; 7825 verifyFormat("{\n" 7826 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7827 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7828 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7829 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7830 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7831 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 7832 "};", 7833 Tab); 7834 verifyFormat("enum AA {\n" 7835 "\ta1, // Force multiple lines\n" 7836 "\ta2,\n" 7837 "\ta3\n" 7838 "};", 7839 Tab); 7840 EXPECT_EQ("if (aaaaaaaa && // q\n" 7841 " bb) // w\n" 7842 "\t;", 7843 format("if (aaaaaaaa &&// q\n" 7844 "bb)// w\n" 7845 ";", 7846 Tab)); 7847 verifyFormat("class X {\n" 7848 "\tvoid f() {\n" 7849 "\t\tsomeFunction(parameter1,\n" 7850 "\t\t parameter2);\n" 7851 "\t}\n" 7852 "};", 7853 Tab); 7854 verifyFormat("{\n" 7855 "\tQ(\n" 7856 "\t {\n" 7857 "\t\t int a;\n" 7858 "\t\t someFunction(aaaaaaaa,\n" 7859 "\t\t bbbbbbb);\n" 7860 "\t },\n" 7861 "\t p);\n" 7862 "}", 7863 Tab); 7864 EXPECT_EQ("{\n" 7865 "\t/* aaaa\n" 7866 "\t bbbb */\n" 7867 "}", 7868 format("{\n" 7869 "/* aaaa\n" 7870 " bbbb */\n" 7871 "}", 7872 Tab)); 7873 EXPECT_EQ("{\n" 7874 "\t/*\n" 7875 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7876 "\t bbbbbbbbbbbbb\n" 7877 "\t*/\n" 7878 "}", 7879 format("{\n" 7880 "/*\n" 7881 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7882 "*/\n" 7883 "}", 7884 Tab)); 7885 EXPECT_EQ("{\n" 7886 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7887 "\t// bbbbbbbbbbbbb\n" 7888 "}", 7889 format("{\n" 7890 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7891 "}", 7892 Tab)); 7893 EXPECT_EQ("{\n" 7894 "\t/*\n" 7895 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7896 "\t bbbbbbbbbbbbb\n" 7897 "\t*/\n" 7898 "}", 7899 format("{\n" 7900 "\t/*\n" 7901 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 7902 "\t*/\n" 7903 "}", 7904 Tab)); 7905 EXPECT_EQ("{\n" 7906 "\t/*\n" 7907 "\n" 7908 "\t*/\n" 7909 "}", 7910 format("{\n" 7911 "\t/*\n" 7912 "\n" 7913 "\t*/\n" 7914 "}", 7915 Tab)); 7916 EXPECT_EQ("{\n" 7917 "\t/*\n" 7918 " asdf\n" 7919 "\t*/\n" 7920 "}", 7921 format("{\n" 7922 "\t/*\n" 7923 " asdf\n" 7924 "\t*/\n" 7925 "}", 7926 Tab)); 7927 7928 Tab.UseTab = FormatStyle::UT_Never; 7929 EXPECT_EQ("/*\n" 7930 " a\t\tcomment\n" 7931 " in multiple lines\n" 7932 " */", 7933 format(" /*\t \t \n" 7934 " \t \t a\t\tcomment\t \t\n" 7935 " \t \t in multiple lines\t\n" 7936 " \t */", 7937 Tab)); 7938 EXPECT_EQ("/* some\n" 7939 " comment */", 7940 format(" \t \t /* some\n" 7941 " \t \t comment */", 7942 Tab)); 7943 EXPECT_EQ("int a; /* some\n" 7944 " comment */", 7945 format(" \t \t int a; /* some\n" 7946 " \t \t comment */", 7947 Tab)); 7948 7949 EXPECT_EQ("int a; /* some\n" 7950 "comment */", 7951 format(" \t \t int\ta; /* some\n" 7952 " \t \t comment */", 7953 Tab)); 7954 EXPECT_EQ("f(\"\t\t\"); /* some\n" 7955 " comment */", 7956 format(" \t \t f(\"\t\t\"); /* some\n" 7957 " \t \t comment */", 7958 Tab)); 7959 EXPECT_EQ("{\n" 7960 " /*\n" 7961 " * Comment\n" 7962 " */\n" 7963 " int i;\n" 7964 "}", 7965 format("{\n" 7966 "\t/*\n" 7967 "\t * Comment\n" 7968 "\t */\n" 7969 "\t int i;\n" 7970 "}")); 7971 7972 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 7973 Tab.TabWidth = 8; 7974 Tab.IndentWidth = 8; 7975 EXPECT_EQ("if (aaaaaaaa && // q\n" 7976 " bb) // w\n" 7977 "\t;", 7978 format("if (aaaaaaaa &&// q\n" 7979 "bb)// w\n" 7980 ";", 7981 Tab)); 7982 EXPECT_EQ("if (aaa && bbb) // w\n" 7983 "\t;", 7984 format("if(aaa&&bbb)// w\n" 7985 ";", 7986 Tab)); 7987 verifyFormat("class X {\n" 7988 "\tvoid f() {\n" 7989 "\t\tsomeFunction(parameter1,\n" 7990 "\t\t\t parameter2);\n" 7991 "\t}\n" 7992 "};", 7993 Tab); 7994 verifyFormat("#define A \\\n" 7995 "\tvoid f() { \\\n" 7996 "\t\tsomeFunction( \\\n" 7997 "\t\t parameter1, \\\n" 7998 "\t\t parameter2); \\\n" 7999 "\t}", 8000 Tab); 8001 Tab.TabWidth = 4; 8002 Tab.IndentWidth = 8; 8003 verifyFormat("class TabWidth4Indent8 {\n" 8004 "\t\tvoid f() {\n" 8005 "\t\t\t\tsomeFunction(parameter1,\n" 8006 "\t\t\t\t\t\t\t parameter2);\n" 8007 "\t\t}\n" 8008 "};", 8009 Tab); 8010 Tab.TabWidth = 4; 8011 Tab.IndentWidth = 4; 8012 verifyFormat("class TabWidth4Indent4 {\n" 8013 "\tvoid f() {\n" 8014 "\t\tsomeFunction(parameter1,\n" 8015 "\t\t\t\t\t parameter2);\n" 8016 "\t}\n" 8017 "};", 8018 Tab); 8019 Tab.TabWidth = 8; 8020 Tab.IndentWidth = 4; 8021 verifyFormat("class TabWidth8Indent4 {\n" 8022 " void f() {\n" 8023 "\tsomeFunction(parameter1,\n" 8024 "\t\t parameter2);\n" 8025 " }\n" 8026 "};", 8027 Tab); 8028 Tab.TabWidth = 8; 8029 Tab.IndentWidth = 8; 8030 EXPECT_EQ("/*\n" 8031 "\t a\t\tcomment\n" 8032 "\t in multiple lines\n" 8033 " */", 8034 format(" /*\t \t \n" 8035 " \t \t a\t\tcomment\t \t\n" 8036 " \t \t in multiple lines\t\n" 8037 " \t */", 8038 Tab)); 8039 verifyFormat("{\n" 8040 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8041 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8042 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8043 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8044 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8045 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8046 "};", 8047 Tab); 8048 verifyFormat("enum AA {\n" 8049 "\ta1, // Force multiple lines\n" 8050 "\ta2,\n" 8051 "\ta3\n" 8052 "};", 8053 Tab); 8054 EXPECT_EQ("if (aaaaaaaa && // q\n" 8055 " bb) // w\n" 8056 "\t;", 8057 format("if (aaaaaaaa &&// q\n" 8058 "bb)// w\n" 8059 ";", 8060 Tab)); 8061 verifyFormat("class X {\n" 8062 "\tvoid f() {\n" 8063 "\t\tsomeFunction(parameter1,\n" 8064 "\t\t\t parameter2);\n" 8065 "\t}\n" 8066 "};", 8067 Tab); 8068 verifyFormat("{\n" 8069 "\tQ(\n" 8070 "\t {\n" 8071 "\t\t int a;\n" 8072 "\t\t someFunction(aaaaaaaa,\n" 8073 "\t\t\t\t bbbbbbb);\n" 8074 "\t },\n" 8075 "\t p);\n" 8076 "}", 8077 Tab); 8078 EXPECT_EQ("{\n" 8079 "\t/* aaaa\n" 8080 "\t bbbb */\n" 8081 "}", 8082 format("{\n" 8083 "/* aaaa\n" 8084 " bbbb */\n" 8085 "}", 8086 Tab)); 8087 EXPECT_EQ("{\n" 8088 "\t/*\n" 8089 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8090 "\t bbbbbbbbbbbbb\n" 8091 "\t*/\n" 8092 "}", 8093 format("{\n" 8094 "/*\n" 8095 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8096 "*/\n" 8097 "}", 8098 Tab)); 8099 EXPECT_EQ("{\n" 8100 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8101 "\t// bbbbbbbbbbbbb\n" 8102 "}", 8103 format("{\n" 8104 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8105 "}", 8106 Tab)); 8107 EXPECT_EQ("{\n" 8108 "\t/*\n" 8109 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8110 "\t bbbbbbbbbbbbb\n" 8111 "\t*/\n" 8112 "}", 8113 format("{\n" 8114 "\t/*\n" 8115 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8116 "\t*/\n" 8117 "}", 8118 Tab)); 8119 EXPECT_EQ("{\n" 8120 "\t/*\n" 8121 "\n" 8122 "\t*/\n" 8123 "}", 8124 format("{\n" 8125 "\t/*\n" 8126 "\n" 8127 "\t*/\n" 8128 "}", 8129 Tab)); 8130 EXPECT_EQ("{\n" 8131 "\t/*\n" 8132 " asdf\n" 8133 "\t*/\n" 8134 "}", 8135 format("{\n" 8136 "\t/*\n" 8137 " asdf\n" 8138 "\t*/\n" 8139 "}", 8140 Tab)); 8141 EXPECT_EQ("/*\n" 8142 "\t a\t\tcomment\n" 8143 "\t in multiple lines\n" 8144 " */", 8145 format(" /*\t \t \n" 8146 " \t \t a\t\tcomment\t \t\n" 8147 " \t \t in multiple lines\t\n" 8148 " \t */", 8149 Tab)); 8150 EXPECT_EQ("/* some\n" 8151 " comment */", 8152 format(" \t \t /* some\n" 8153 " \t \t comment */", 8154 Tab)); 8155 EXPECT_EQ("int a; /* some\n" 8156 " comment */", 8157 format(" \t \t int a; /* some\n" 8158 " \t \t comment */", 8159 Tab)); 8160 EXPECT_EQ("int a; /* some\n" 8161 "comment */", 8162 format(" \t \t int\ta; /* some\n" 8163 " \t \t comment */", 8164 Tab)); 8165 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8166 " comment */", 8167 format(" \t \t f(\"\t\t\"); /* some\n" 8168 " \t \t comment */", 8169 Tab)); 8170 EXPECT_EQ("{\n" 8171 " /*\n" 8172 " * Comment\n" 8173 " */\n" 8174 " int i;\n" 8175 "}", 8176 format("{\n" 8177 "\t/*\n" 8178 "\t * Comment\n" 8179 "\t */\n" 8180 "\t int i;\n" 8181 "}")); 8182 Tab.AlignConsecutiveAssignments = true; 8183 Tab.AlignConsecutiveDeclarations = true; 8184 Tab.TabWidth = 4; 8185 Tab.IndentWidth = 4; 8186 verifyFormat("class Assign {\n" 8187 "\tvoid f() {\n" 8188 "\t\tint x = 123;\n" 8189 "\t\tint random = 4;\n" 8190 "\t\tstd::string alphabet =\n" 8191 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 8192 "\t}\n" 8193 "};", 8194 Tab); 8195 } 8196 8197 TEST_F(FormatTest, CalculatesOriginalColumn) { 8198 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8199 "q\"; /* some\n" 8200 " comment */", 8201 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8202 "q\"; /* some\n" 8203 " comment */", 8204 getLLVMStyle())); 8205 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8206 "/* some\n" 8207 " comment */", 8208 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8209 " /* some\n" 8210 " comment */", 8211 getLLVMStyle())); 8212 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8213 "qqq\n" 8214 "/* some\n" 8215 " comment */", 8216 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8217 "qqq\n" 8218 " /* some\n" 8219 " comment */", 8220 getLLVMStyle())); 8221 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8222 "wwww; /* some\n" 8223 " comment */", 8224 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8225 "wwww; /* some\n" 8226 " comment */", 8227 getLLVMStyle())); 8228 } 8229 8230 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8231 FormatStyle NoSpace = getLLVMStyle(); 8232 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8233 8234 verifyFormat("while(true)\n" 8235 " continue;", 8236 NoSpace); 8237 verifyFormat("for(;;)\n" 8238 " continue;", 8239 NoSpace); 8240 verifyFormat("if(true)\n" 8241 " f();\n" 8242 "else if(true)\n" 8243 " f();", 8244 NoSpace); 8245 verifyFormat("do {\n" 8246 " do_something();\n" 8247 "} while(something());", 8248 NoSpace); 8249 verifyFormat("switch(x) {\n" 8250 "default:\n" 8251 " break;\n" 8252 "}", 8253 NoSpace); 8254 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8255 verifyFormat("size_t x = sizeof(x);", NoSpace); 8256 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8257 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8258 verifyFormat("alignas(128) char a[128];", NoSpace); 8259 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8260 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8261 verifyFormat("int f() throw(Deprecated);", NoSpace); 8262 verifyFormat("typedef void (*cb)(int);", NoSpace); 8263 verifyFormat("T A::operator()();", NoSpace); 8264 verifyFormat("X A::operator++(T);", NoSpace); 8265 8266 FormatStyle Space = getLLVMStyle(); 8267 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8268 8269 verifyFormat("int f ();", Space); 8270 verifyFormat("void f (int a, T b) {\n" 8271 " while (true)\n" 8272 " continue;\n" 8273 "}", 8274 Space); 8275 verifyFormat("if (true)\n" 8276 " f ();\n" 8277 "else if (true)\n" 8278 " f ();", 8279 Space); 8280 verifyFormat("do {\n" 8281 " do_something ();\n" 8282 "} while (something ());", 8283 Space); 8284 verifyFormat("switch (x) {\n" 8285 "default:\n" 8286 " break;\n" 8287 "}", 8288 Space); 8289 verifyFormat("A::A () : a (1) {}", Space); 8290 verifyFormat("void f () __attribute__ ((asdf));", Space); 8291 verifyFormat("*(&a + 1);\n" 8292 "&((&a)[1]);\n" 8293 "a[(b + c) * d];\n" 8294 "(((a + 1) * 2) + 3) * 4;", 8295 Space); 8296 verifyFormat("#define A(x) x", Space); 8297 verifyFormat("#define A (x) x", Space); 8298 verifyFormat("#if defined(x)\n" 8299 "#endif", 8300 Space); 8301 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8302 verifyFormat("size_t x = sizeof (x);", Space); 8303 verifyFormat("auto f (int x) -> decltype (x);", Space); 8304 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8305 verifyFormat("alignas (128) char a[128];", Space); 8306 verifyFormat("size_t x = alignof (MyType);", Space); 8307 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8308 verifyFormat("int f () throw (Deprecated);", Space); 8309 verifyFormat("typedef void (*cb) (int);", Space); 8310 verifyFormat("T A::operator() ();", Space); 8311 verifyFormat("X A::operator++ (T);", Space); 8312 } 8313 8314 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8315 FormatStyle Spaces = getLLVMStyle(); 8316 8317 Spaces.SpacesInParentheses = true; 8318 verifyFormat("call( x, y, z );", Spaces); 8319 verifyFormat("call();", Spaces); 8320 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8321 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 8322 Spaces); 8323 verifyFormat("while ( (bool)1 )\n" 8324 " continue;", 8325 Spaces); 8326 verifyFormat("for ( ;; )\n" 8327 " continue;", 8328 Spaces); 8329 verifyFormat("if ( true )\n" 8330 " f();\n" 8331 "else if ( true )\n" 8332 " f();", 8333 Spaces); 8334 verifyFormat("do {\n" 8335 " do_something( (int)i );\n" 8336 "} while ( something() );", 8337 Spaces); 8338 verifyFormat("switch ( x ) {\n" 8339 "default:\n" 8340 " break;\n" 8341 "}", 8342 Spaces); 8343 8344 Spaces.SpacesInParentheses = false; 8345 Spaces.SpacesInCStyleCastParentheses = true; 8346 verifyFormat("Type *A = ( Type * )P;", Spaces); 8347 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8348 verifyFormat("x = ( int32 )y;", Spaces); 8349 verifyFormat("int a = ( int )(2.0f);", Spaces); 8350 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8351 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8352 verifyFormat("#define x (( int )-1)", Spaces); 8353 8354 // Run the first set of tests again with: 8355 Spaces.SpacesInParentheses = false; 8356 Spaces.SpaceInEmptyParentheses = true; 8357 Spaces.SpacesInCStyleCastParentheses = true; 8358 verifyFormat("call(x, y, z);", Spaces); 8359 verifyFormat("call( );", Spaces); 8360 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8361 verifyFormat("while (( bool )1)\n" 8362 " continue;", 8363 Spaces); 8364 verifyFormat("for (;;)\n" 8365 " continue;", 8366 Spaces); 8367 verifyFormat("if (true)\n" 8368 " f( );\n" 8369 "else if (true)\n" 8370 " f( );", 8371 Spaces); 8372 verifyFormat("do {\n" 8373 " do_something(( int )i);\n" 8374 "} while (something( ));", 8375 Spaces); 8376 verifyFormat("switch (x) {\n" 8377 "default:\n" 8378 " break;\n" 8379 "}", 8380 Spaces); 8381 8382 // Run the first set of tests again with: 8383 Spaces.SpaceAfterCStyleCast = true; 8384 verifyFormat("call(x, y, z);", Spaces); 8385 verifyFormat("call( );", Spaces); 8386 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8387 verifyFormat("while (( bool ) 1)\n" 8388 " continue;", 8389 Spaces); 8390 verifyFormat("for (;;)\n" 8391 " continue;", 8392 Spaces); 8393 verifyFormat("if (true)\n" 8394 " f( );\n" 8395 "else if (true)\n" 8396 " f( );", 8397 Spaces); 8398 verifyFormat("do {\n" 8399 " do_something(( int ) i);\n" 8400 "} while (something( ));", 8401 Spaces); 8402 verifyFormat("switch (x) {\n" 8403 "default:\n" 8404 " break;\n" 8405 "}", 8406 Spaces); 8407 8408 // Run subset of tests again with: 8409 Spaces.SpacesInCStyleCastParentheses = false; 8410 Spaces.SpaceAfterCStyleCast = true; 8411 verifyFormat("while ((bool) 1)\n" 8412 " continue;", 8413 Spaces); 8414 verifyFormat("do {\n" 8415 " do_something((int) i);\n" 8416 "} while (something( ));", 8417 Spaces); 8418 } 8419 8420 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8421 verifyFormat("int a[5];"); 8422 verifyFormat("a[3] += 42;"); 8423 8424 FormatStyle Spaces = getLLVMStyle(); 8425 Spaces.SpacesInSquareBrackets = true; 8426 // Lambdas unchanged. 8427 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8428 verifyFormat("return [i, args...] {};", Spaces); 8429 8430 // Not lambdas. 8431 verifyFormat("int a[ 5 ];", Spaces); 8432 verifyFormat("a[ 3 ] += 42;", Spaces); 8433 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8434 verifyFormat("double &operator[](int i) { return 0; }\n" 8435 "int i;", 8436 Spaces); 8437 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8438 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8439 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8440 } 8441 8442 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8443 verifyFormat("int a = 5;"); 8444 verifyFormat("a += 42;"); 8445 verifyFormat("a or_eq 8;"); 8446 8447 FormatStyle Spaces = getLLVMStyle(); 8448 Spaces.SpaceBeforeAssignmentOperators = false; 8449 verifyFormat("int a= 5;", Spaces); 8450 verifyFormat("a+= 42;", Spaces); 8451 verifyFormat("a or_eq 8;", Spaces); 8452 } 8453 8454 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8455 FormatStyle Alignment = getLLVMStyle(); 8456 Alignment.AlignConsecutiveAssignments = false; 8457 verifyFormat("int a = 5;\n" 8458 "int oneTwoThree = 123;", 8459 Alignment); 8460 verifyFormat("int a = 5;\n" 8461 "int oneTwoThree = 123;", 8462 Alignment); 8463 8464 Alignment.AlignConsecutiveAssignments = true; 8465 verifyFormat("int a = 5;\n" 8466 "int oneTwoThree = 123;", 8467 Alignment); 8468 verifyFormat("int a = method();\n" 8469 "int oneTwoThree = 133;", 8470 Alignment); 8471 verifyFormat("a &= 5;\n" 8472 "bcd *= 5;\n" 8473 "ghtyf += 5;\n" 8474 "dvfvdb -= 5;\n" 8475 "a /= 5;\n" 8476 "vdsvsv %= 5;\n" 8477 "sfdbddfbdfbb ^= 5;\n" 8478 "dvsdsv |= 5;\n" 8479 "int dsvvdvsdvvv = 123;", 8480 Alignment); 8481 verifyFormat("int i = 1, j = 10;\n" 8482 "something = 2000;", 8483 Alignment); 8484 verifyFormat("something = 2000;\n" 8485 "int i = 1, j = 10;\n", 8486 Alignment); 8487 verifyFormat("something = 2000;\n" 8488 "another = 911;\n" 8489 "int i = 1, j = 10;\n" 8490 "oneMore = 1;\n" 8491 "i = 2;", 8492 Alignment); 8493 verifyFormat("int a = 5;\n" 8494 "int one = 1;\n" 8495 "method();\n" 8496 "int oneTwoThree = 123;\n" 8497 "int oneTwo = 12;", 8498 Alignment); 8499 verifyFormat("int oneTwoThree = 123;\n" 8500 "int oneTwo = 12;\n" 8501 "method();\n", 8502 Alignment); 8503 verifyFormat("int oneTwoThree = 123; // comment\n" 8504 "int oneTwo = 12; // comment", 8505 Alignment); 8506 EXPECT_EQ("int a = 5;\n" 8507 "\n" 8508 "int oneTwoThree = 123;", 8509 format("int a = 5;\n" 8510 "\n" 8511 "int oneTwoThree= 123;", 8512 Alignment)); 8513 EXPECT_EQ("int a = 5;\n" 8514 "int one = 1;\n" 8515 "\n" 8516 "int oneTwoThree = 123;", 8517 format("int a = 5;\n" 8518 "int one = 1;\n" 8519 "\n" 8520 "int oneTwoThree = 123;", 8521 Alignment)); 8522 EXPECT_EQ("int a = 5;\n" 8523 "int one = 1;\n" 8524 "\n" 8525 "int oneTwoThree = 123;\n" 8526 "int oneTwo = 12;", 8527 format("int a = 5;\n" 8528 "int one = 1;\n" 8529 "\n" 8530 "int oneTwoThree = 123;\n" 8531 "int oneTwo = 12;", 8532 Alignment)); 8533 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 8534 verifyFormat("#define A \\\n" 8535 " int aaaa = 12; \\\n" 8536 " int b = 23; \\\n" 8537 " int ccc = 234; \\\n" 8538 " int dddddddddd = 2345;", 8539 Alignment); 8540 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8541 verifyFormat("#define A \\\n" 8542 " int aaaa = 12; \\\n" 8543 " int b = 23; \\\n" 8544 " int ccc = 234; \\\n" 8545 " int dddddddddd = 2345;", 8546 Alignment); 8547 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 8548 verifyFormat("#define A " 8549 " \\\n" 8550 " int aaaa = 12; " 8551 " \\\n" 8552 " int b = 23; " 8553 " \\\n" 8554 " int ccc = 234; " 8555 " \\\n" 8556 " int dddddddddd = 2345;", 8557 Alignment); 8558 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8559 "k = 4, int l = 5,\n" 8560 " int m = 6) {\n" 8561 " int j = 10;\n" 8562 " otherThing = 1;\n" 8563 "}", 8564 Alignment); 8565 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8566 " int i = 1;\n" 8567 " int j = 2;\n" 8568 " int big = 10000;\n" 8569 "}", 8570 Alignment); 8571 verifyFormat("class C {\n" 8572 "public:\n" 8573 " int i = 1;\n" 8574 " virtual void f() = 0;\n" 8575 "};", 8576 Alignment); 8577 verifyFormat("int i = 1;\n" 8578 "if (SomeType t = getSomething()) {\n" 8579 "}\n" 8580 "int j = 2;\n" 8581 "int big = 10000;", 8582 Alignment); 8583 verifyFormat("int j = 7;\n" 8584 "for (int k = 0; k < N; ++k) {\n" 8585 "}\n" 8586 "int j = 2;\n" 8587 "int big = 10000;\n" 8588 "}", 8589 Alignment); 8590 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8591 verifyFormat("int i = 1;\n" 8592 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8593 " = someLooooooooooooooooongFunction();\n" 8594 "int j = 2;", 8595 Alignment); 8596 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8597 verifyFormat("int i = 1;\n" 8598 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8599 " someLooooooooooooooooongFunction();\n" 8600 "int j = 2;", 8601 Alignment); 8602 8603 verifyFormat("auto lambda = []() {\n" 8604 " auto i = 0;\n" 8605 " return 0;\n" 8606 "};\n" 8607 "int i = 0;\n" 8608 "auto v = type{\n" 8609 " i = 1, //\n" 8610 " (i = 2), //\n" 8611 " i = 3 //\n" 8612 "};", 8613 Alignment); 8614 8615 verifyFormat( 8616 "int i = 1;\n" 8617 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8618 " loooooooooooooooooooooongParameterB);\n" 8619 "int j = 2;", 8620 Alignment); 8621 8622 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 8623 " typename B = very_long_type_name_1,\n" 8624 " typename T_2 = very_long_type_name_2>\n" 8625 "auto foo() {}\n", 8626 Alignment); 8627 verifyFormat("int a, b = 1;\n" 8628 "int c = 2;\n" 8629 "int dd = 3;\n", 8630 Alignment); 8631 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8632 "float b[1][] = {{3.f}};\n", 8633 Alignment); 8634 verifyFormat("for (int i = 0; i < 1; i++)\n" 8635 " int x = 1;\n", 8636 Alignment); 8637 verifyFormat("for (i = 0; i < 1; i++)\n" 8638 " x = 1;\n" 8639 "y = 1;\n", 8640 Alignment); 8641 } 8642 8643 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 8644 FormatStyle Alignment = getLLVMStyle(); 8645 Alignment.AlignConsecutiveDeclarations = false; 8646 verifyFormat("float const a = 5;\n" 8647 "int oneTwoThree = 123;", 8648 Alignment); 8649 verifyFormat("int a = 5;\n" 8650 "float const oneTwoThree = 123;", 8651 Alignment); 8652 8653 Alignment.AlignConsecutiveDeclarations = true; 8654 verifyFormat("float const a = 5;\n" 8655 "int oneTwoThree = 123;", 8656 Alignment); 8657 verifyFormat("int a = method();\n" 8658 "float const oneTwoThree = 133;", 8659 Alignment); 8660 verifyFormat("int i = 1, j = 10;\n" 8661 "something = 2000;", 8662 Alignment); 8663 verifyFormat("something = 2000;\n" 8664 "int i = 1, j = 10;\n", 8665 Alignment); 8666 verifyFormat("float something = 2000;\n" 8667 "double another = 911;\n" 8668 "int i = 1, j = 10;\n" 8669 "const int *oneMore = 1;\n" 8670 "unsigned i = 2;", 8671 Alignment); 8672 verifyFormat("float a = 5;\n" 8673 "int one = 1;\n" 8674 "method();\n" 8675 "const double oneTwoThree = 123;\n" 8676 "const unsigned int oneTwo = 12;", 8677 Alignment); 8678 verifyFormat("int oneTwoThree{0}; // comment\n" 8679 "unsigned oneTwo; // comment", 8680 Alignment); 8681 EXPECT_EQ("float const a = 5;\n" 8682 "\n" 8683 "int oneTwoThree = 123;", 8684 format("float const a = 5;\n" 8685 "\n" 8686 "int oneTwoThree= 123;", 8687 Alignment)); 8688 EXPECT_EQ("float a = 5;\n" 8689 "int one = 1;\n" 8690 "\n" 8691 "unsigned oneTwoThree = 123;", 8692 format("float a = 5;\n" 8693 "int one = 1;\n" 8694 "\n" 8695 "unsigned oneTwoThree = 123;", 8696 Alignment)); 8697 EXPECT_EQ("float a = 5;\n" 8698 "int one = 1;\n" 8699 "\n" 8700 "unsigned oneTwoThree = 123;\n" 8701 "int oneTwo = 12;", 8702 format("float a = 5;\n" 8703 "int one = 1;\n" 8704 "\n" 8705 "unsigned oneTwoThree = 123;\n" 8706 "int oneTwo = 12;", 8707 Alignment)); 8708 // Function prototype alignment 8709 verifyFormat("int a();\n" 8710 "double b();", 8711 Alignment); 8712 verifyFormat("int a(int x);\n" 8713 "double b();", 8714 Alignment); 8715 unsigned OldColumnLimit = Alignment.ColumnLimit; 8716 // We need to set ColumnLimit to zero, in order to stress nested alignments, 8717 // otherwise the function parameters will be re-flowed onto a single line. 8718 Alignment.ColumnLimit = 0; 8719 EXPECT_EQ("int a(int x,\n" 8720 " float y);\n" 8721 "double b(int x,\n" 8722 " double y);", 8723 format("int a(int x,\n" 8724 " float y);\n" 8725 "double b(int x,\n" 8726 " double y);", 8727 Alignment)); 8728 // This ensures that function parameters of function declarations are 8729 // correctly indented when their owning functions are indented. 8730 // The failure case here is for 'double y' to not be indented enough. 8731 EXPECT_EQ("double a(int x);\n" 8732 "int b(int y,\n" 8733 " double z);", 8734 format("double a(int x);\n" 8735 "int b(int y,\n" 8736 " double z);", 8737 Alignment)); 8738 // Set ColumnLimit low so that we induce wrapping immediately after 8739 // the function name and opening paren. 8740 Alignment.ColumnLimit = 13; 8741 verifyFormat("int function(\n" 8742 " int x,\n" 8743 " bool y);", 8744 Alignment); 8745 Alignment.ColumnLimit = OldColumnLimit; 8746 // Ensure function pointers don't screw up recursive alignment 8747 verifyFormat("int a(int x, void (*fp)(int y));\n" 8748 "double b();", 8749 Alignment); 8750 Alignment.AlignConsecutiveAssignments = true; 8751 // Ensure recursive alignment is broken by function braces, so that the 8752 // "a = 1" does not align with subsequent assignments inside the function 8753 // body. 8754 verifyFormat("int func(int a = 1) {\n" 8755 " int b = 2;\n" 8756 " int cc = 3;\n" 8757 "}", 8758 Alignment); 8759 verifyFormat("float something = 2000;\n" 8760 "double another = 911;\n" 8761 "int i = 1, j = 10;\n" 8762 "const int *oneMore = 1;\n" 8763 "unsigned i = 2;", 8764 Alignment); 8765 verifyFormat("int oneTwoThree = {0}; // comment\n" 8766 "unsigned oneTwo = 0; // comment", 8767 Alignment); 8768 // Make sure that scope is correctly tracked, in the absence of braces 8769 verifyFormat("for (int i = 0; i < n; i++)\n" 8770 " j = i;\n" 8771 "double x = 1;\n", 8772 Alignment); 8773 verifyFormat("if (int i = 0)\n" 8774 " j = i;\n" 8775 "double x = 1;\n", 8776 Alignment); 8777 // Ensure operator[] and operator() are comprehended 8778 verifyFormat("struct test {\n" 8779 " long long int foo();\n" 8780 " int operator[](int a);\n" 8781 " double bar();\n" 8782 "};\n", 8783 Alignment); 8784 verifyFormat("struct test {\n" 8785 " long long int foo();\n" 8786 " int operator()(int a);\n" 8787 " double bar();\n" 8788 "};\n", 8789 Alignment); 8790 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 8791 " int const i = 1;\n" 8792 " int * j = 2;\n" 8793 " int big = 10000;\n" 8794 "\n" 8795 " unsigned oneTwoThree = 123;\n" 8796 " int oneTwo = 12;\n" 8797 " method();\n" 8798 " float k = 2;\n" 8799 " int ll = 10000;\n" 8800 "}", 8801 format("void SomeFunction(int parameter= 0) {\n" 8802 " int const i= 1;\n" 8803 " int *j=2;\n" 8804 " int big = 10000;\n" 8805 "\n" 8806 "unsigned oneTwoThree =123;\n" 8807 "int oneTwo = 12;\n" 8808 " method();\n" 8809 "float k= 2;\n" 8810 "int ll=10000;\n" 8811 "}", 8812 Alignment)); 8813 Alignment.AlignConsecutiveAssignments = false; 8814 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 8815 verifyFormat("#define A \\\n" 8816 " int aaaa = 12; \\\n" 8817 " float b = 23; \\\n" 8818 " const int ccc = 234; \\\n" 8819 " unsigned dddddddddd = 2345;", 8820 Alignment); 8821 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 8822 verifyFormat("#define A \\\n" 8823 " int aaaa = 12; \\\n" 8824 " float b = 23; \\\n" 8825 " const int ccc = 234; \\\n" 8826 " unsigned dddddddddd = 2345;", 8827 Alignment); 8828 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 8829 Alignment.ColumnLimit = 30; 8830 verifyFormat("#define A \\\n" 8831 " int aaaa = 12; \\\n" 8832 " float b = 23; \\\n" 8833 " const int ccc = 234; \\\n" 8834 " int dddddddddd = 2345;", 8835 Alignment); 8836 Alignment.ColumnLimit = 80; 8837 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8838 "k = 4, int l = 5,\n" 8839 " int m = 6) {\n" 8840 " const int j = 10;\n" 8841 " otherThing = 1;\n" 8842 "}", 8843 Alignment); 8844 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8845 " int const i = 1;\n" 8846 " int * j = 2;\n" 8847 " int big = 10000;\n" 8848 "}", 8849 Alignment); 8850 verifyFormat("class C {\n" 8851 "public:\n" 8852 " int i = 1;\n" 8853 " virtual void f() = 0;\n" 8854 "};", 8855 Alignment); 8856 verifyFormat("float i = 1;\n" 8857 "if (SomeType t = getSomething()) {\n" 8858 "}\n" 8859 "const unsigned j = 2;\n" 8860 "int big = 10000;", 8861 Alignment); 8862 verifyFormat("float j = 7;\n" 8863 "for (int k = 0; k < N; ++k) {\n" 8864 "}\n" 8865 "unsigned j = 2;\n" 8866 "int big = 10000;\n" 8867 "}", 8868 Alignment); 8869 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8870 verifyFormat("float i = 1;\n" 8871 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8872 " = someLooooooooooooooooongFunction();\n" 8873 "int j = 2;", 8874 Alignment); 8875 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8876 verifyFormat("int i = 1;\n" 8877 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8878 " someLooooooooooooooooongFunction();\n" 8879 "int j = 2;", 8880 Alignment); 8881 8882 Alignment.AlignConsecutiveAssignments = true; 8883 verifyFormat("auto lambda = []() {\n" 8884 " auto ii = 0;\n" 8885 " float j = 0;\n" 8886 " return 0;\n" 8887 "};\n" 8888 "int i = 0;\n" 8889 "float i2 = 0;\n" 8890 "auto v = type{\n" 8891 " i = 1, //\n" 8892 " (i = 2), //\n" 8893 " i = 3 //\n" 8894 "};", 8895 Alignment); 8896 Alignment.AlignConsecutiveAssignments = false; 8897 8898 verifyFormat( 8899 "int i = 1;\n" 8900 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8901 " loooooooooooooooooooooongParameterB);\n" 8902 "int j = 2;", 8903 Alignment); 8904 8905 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 8906 // We expect declarations and assignments to align, as long as it doesn't 8907 // exceed the column limit, starting a new alignment sequence whenever it 8908 // happens. 8909 Alignment.AlignConsecutiveAssignments = true; 8910 Alignment.ColumnLimit = 30; 8911 verifyFormat("float ii = 1;\n" 8912 "unsigned j = 2;\n" 8913 "int someVerylongVariable = 1;\n" 8914 "AnotherLongType ll = 123456;\n" 8915 "VeryVeryLongType k = 2;\n" 8916 "int myvar = 1;", 8917 Alignment); 8918 Alignment.ColumnLimit = 80; 8919 Alignment.AlignConsecutiveAssignments = false; 8920 8921 verifyFormat( 8922 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 8923 " typename LongType, typename B>\n" 8924 "auto foo() {}\n", 8925 Alignment); 8926 verifyFormat("float a, b = 1;\n" 8927 "int c = 2;\n" 8928 "int dd = 3;\n", 8929 Alignment); 8930 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8931 "float b[1][] = {{3.f}};\n", 8932 Alignment); 8933 Alignment.AlignConsecutiveAssignments = true; 8934 verifyFormat("float a, b = 1;\n" 8935 "int c = 2;\n" 8936 "int dd = 3;\n", 8937 Alignment); 8938 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 8939 "float b[1][] = {{3.f}};\n", 8940 Alignment); 8941 Alignment.AlignConsecutiveAssignments = false; 8942 8943 Alignment.ColumnLimit = 30; 8944 Alignment.BinPackParameters = false; 8945 verifyFormat("void foo(float a,\n" 8946 " float b,\n" 8947 " int c,\n" 8948 " uint32_t *d) {\n" 8949 " int * e = 0;\n" 8950 " float f = 0;\n" 8951 " double g = 0;\n" 8952 "}\n" 8953 "void bar(ino_t a,\n" 8954 " int b,\n" 8955 " uint32_t *c,\n" 8956 " bool d) {}\n", 8957 Alignment); 8958 Alignment.BinPackParameters = true; 8959 Alignment.ColumnLimit = 80; 8960 } 8961 8962 TEST_F(FormatTest, LinuxBraceBreaking) { 8963 FormatStyle LinuxBraceStyle = getLLVMStyle(); 8964 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 8965 verifyFormat("namespace a\n" 8966 "{\n" 8967 "class A\n" 8968 "{\n" 8969 " void f()\n" 8970 " {\n" 8971 " if (true) {\n" 8972 " a();\n" 8973 " b();\n" 8974 " } else {\n" 8975 " a();\n" 8976 " }\n" 8977 " }\n" 8978 " void g() { return; }\n" 8979 "};\n" 8980 "struct B {\n" 8981 " int x;\n" 8982 "};\n" 8983 "}\n", 8984 LinuxBraceStyle); 8985 verifyFormat("enum X {\n" 8986 " Y = 0,\n" 8987 "}\n", 8988 LinuxBraceStyle); 8989 verifyFormat("struct S {\n" 8990 " int Type;\n" 8991 " union {\n" 8992 " int x;\n" 8993 " double y;\n" 8994 " } Value;\n" 8995 " class C\n" 8996 " {\n" 8997 " MyFavoriteType Value;\n" 8998 " } Class;\n" 8999 "}\n", 9000 LinuxBraceStyle); 9001 } 9002 9003 TEST_F(FormatTest, MozillaBraceBreaking) { 9004 FormatStyle MozillaBraceStyle = getLLVMStyle(); 9005 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 9006 MozillaBraceStyle.FixNamespaceComments = false; 9007 verifyFormat("namespace a {\n" 9008 "class A\n" 9009 "{\n" 9010 " void f()\n" 9011 " {\n" 9012 " if (true) {\n" 9013 " a();\n" 9014 " b();\n" 9015 " }\n" 9016 " }\n" 9017 " void g() { return; }\n" 9018 "};\n" 9019 "enum E\n" 9020 "{\n" 9021 " A,\n" 9022 " // foo\n" 9023 " B,\n" 9024 " C\n" 9025 "};\n" 9026 "struct B\n" 9027 "{\n" 9028 " int x;\n" 9029 "};\n" 9030 "}\n", 9031 MozillaBraceStyle); 9032 verifyFormat("struct S\n" 9033 "{\n" 9034 " int Type;\n" 9035 " union\n" 9036 " {\n" 9037 " int x;\n" 9038 " double y;\n" 9039 " } Value;\n" 9040 " class C\n" 9041 " {\n" 9042 " MyFavoriteType Value;\n" 9043 " } Class;\n" 9044 "}\n", 9045 MozillaBraceStyle); 9046 } 9047 9048 TEST_F(FormatTest, StroustrupBraceBreaking) { 9049 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 9050 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9051 verifyFormat("namespace a {\n" 9052 "class A {\n" 9053 " void f()\n" 9054 " {\n" 9055 " if (true) {\n" 9056 " a();\n" 9057 " b();\n" 9058 " }\n" 9059 " }\n" 9060 " void g() { return; }\n" 9061 "};\n" 9062 "struct B {\n" 9063 " int x;\n" 9064 "};\n" 9065 "} // namespace a\n", 9066 StroustrupBraceStyle); 9067 9068 verifyFormat("void foo()\n" 9069 "{\n" 9070 " if (a) {\n" 9071 " a();\n" 9072 " }\n" 9073 " else {\n" 9074 " b();\n" 9075 " }\n" 9076 "}\n", 9077 StroustrupBraceStyle); 9078 9079 verifyFormat("#ifdef _DEBUG\n" 9080 "int foo(int i = 0)\n" 9081 "#else\n" 9082 "int foo(int i = 5)\n" 9083 "#endif\n" 9084 "{\n" 9085 " return i;\n" 9086 "}", 9087 StroustrupBraceStyle); 9088 9089 verifyFormat("void foo() {}\n" 9090 "void bar()\n" 9091 "#ifdef _DEBUG\n" 9092 "{\n" 9093 " foo();\n" 9094 "}\n" 9095 "#else\n" 9096 "{\n" 9097 "}\n" 9098 "#endif", 9099 StroustrupBraceStyle); 9100 9101 verifyFormat("void foobar() { int i = 5; }\n" 9102 "#ifdef _DEBUG\n" 9103 "void bar() {}\n" 9104 "#else\n" 9105 "void bar() { foobar(); }\n" 9106 "#endif", 9107 StroustrupBraceStyle); 9108 } 9109 9110 TEST_F(FormatTest, AllmanBraceBreaking) { 9111 FormatStyle AllmanBraceStyle = getLLVMStyle(); 9112 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 9113 verifyFormat("namespace a\n" 9114 "{\n" 9115 "class A\n" 9116 "{\n" 9117 " void f()\n" 9118 " {\n" 9119 " if (true)\n" 9120 " {\n" 9121 " a();\n" 9122 " b();\n" 9123 " }\n" 9124 " }\n" 9125 " void g() { return; }\n" 9126 "};\n" 9127 "struct B\n" 9128 "{\n" 9129 " int x;\n" 9130 "};\n" 9131 "}", 9132 AllmanBraceStyle); 9133 9134 verifyFormat("void f()\n" 9135 "{\n" 9136 " if (true)\n" 9137 " {\n" 9138 " a();\n" 9139 " }\n" 9140 " else if (false)\n" 9141 " {\n" 9142 " b();\n" 9143 " }\n" 9144 " else\n" 9145 " {\n" 9146 " c();\n" 9147 " }\n" 9148 "}\n", 9149 AllmanBraceStyle); 9150 9151 verifyFormat("void f()\n" 9152 "{\n" 9153 " for (int i = 0; i < 10; ++i)\n" 9154 " {\n" 9155 " a();\n" 9156 " }\n" 9157 " while (false)\n" 9158 " {\n" 9159 " b();\n" 9160 " }\n" 9161 " do\n" 9162 " {\n" 9163 " c();\n" 9164 " } while (false)\n" 9165 "}\n", 9166 AllmanBraceStyle); 9167 9168 verifyFormat("void f(int a)\n" 9169 "{\n" 9170 " switch (a)\n" 9171 " {\n" 9172 " case 0:\n" 9173 " break;\n" 9174 " case 1:\n" 9175 " {\n" 9176 " break;\n" 9177 " }\n" 9178 " case 2:\n" 9179 " {\n" 9180 " }\n" 9181 " break;\n" 9182 " default:\n" 9183 " break;\n" 9184 " }\n" 9185 "}\n", 9186 AllmanBraceStyle); 9187 9188 verifyFormat("enum X\n" 9189 "{\n" 9190 " Y = 0,\n" 9191 "}\n", 9192 AllmanBraceStyle); 9193 verifyFormat("enum X\n" 9194 "{\n" 9195 " Y = 0\n" 9196 "}\n", 9197 AllmanBraceStyle); 9198 9199 verifyFormat("@interface BSApplicationController ()\n" 9200 "{\n" 9201 "@private\n" 9202 " id _extraIvar;\n" 9203 "}\n" 9204 "@end\n", 9205 AllmanBraceStyle); 9206 9207 verifyFormat("#ifdef _DEBUG\n" 9208 "int foo(int i = 0)\n" 9209 "#else\n" 9210 "int foo(int i = 5)\n" 9211 "#endif\n" 9212 "{\n" 9213 " return i;\n" 9214 "}", 9215 AllmanBraceStyle); 9216 9217 verifyFormat("void foo() {}\n" 9218 "void bar()\n" 9219 "#ifdef _DEBUG\n" 9220 "{\n" 9221 " foo();\n" 9222 "}\n" 9223 "#else\n" 9224 "{\n" 9225 "}\n" 9226 "#endif", 9227 AllmanBraceStyle); 9228 9229 verifyFormat("void foobar() { int i = 5; }\n" 9230 "#ifdef _DEBUG\n" 9231 "void bar() {}\n" 9232 "#else\n" 9233 "void bar() { foobar(); }\n" 9234 "#endif", 9235 AllmanBraceStyle); 9236 9237 // This shouldn't affect ObjC blocks.. 9238 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9239 " // ...\n" 9240 " int i;\n" 9241 "}];", 9242 AllmanBraceStyle); 9243 verifyFormat("void (^block)(void) = ^{\n" 9244 " // ...\n" 9245 " int i;\n" 9246 "};", 9247 AllmanBraceStyle); 9248 // .. or dict literals. 9249 verifyFormat("void f()\n" 9250 "{\n" 9251 " // ...\n" 9252 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 9253 "}", 9254 AllmanBraceStyle); 9255 verifyFormat("void f()\n" 9256 "{\n" 9257 " // ...\n" 9258 " [object someMethod:@{a : @\"b\"}];\n" 9259 "}", 9260 AllmanBraceStyle); 9261 verifyFormat("int f()\n" 9262 "{ // comment\n" 9263 " return 42;\n" 9264 "}", 9265 AllmanBraceStyle); 9266 9267 AllmanBraceStyle.ColumnLimit = 19; 9268 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9269 AllmanBraceStyle.ColumnLimit = 18; 9270 verifyFormat("void f()\n" 9271 "{\n" 9272 " int i;\n" 9273 "}", 9274 AllmanBraceStyle); 9275 AllmanBraceStyle.ColumnLimit = 80; 9276 9277 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9278 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9279 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9280 verifyFormat("void f(bool b)\n" 9281 "{\n" 9282 " if (b)\n" 9283 " {\n" 9284 " return;\n" 9285 " }\n" 9286 "}\n", 9287 BreakBeforeBraceShortIfs); 9288 verifyFormat("void f(bool b)\n" 9289 "{\n" 9290 " if constexpr (b)\n" 9291 " {\n" 9292 " return;\n" 9293 " }\n" 9294 "}\n", 9295 BreakBeforeBraceShortIfs); 9296 verifyFormat("void f(bool b)\n" 9297 "{\n" 9298 " if (b) return;\n" 9299 "}\n", 9300 BreakBeforeBraceShortIfs); 9301 verifyFormat("void f(bool b)\n" 9302 "{\n" 9303 " if constexpr (b) return;\n" 9304 "}\n", 9305 BreakBeforeBraceShortIfs); 9306 verifyFormat("void f(bool b)\n" 9307 "{\n" 9308 " while (b)\n" 9309 " {\n" 9310 " return;\n" 9311 " }\n" 9312 "}\n", 9313 BreakBeforeBraceShortIfs); 9314 } 9315 9316 TEST_F(FormatTest, GNUBraceBreaking) { 9317 FormatStyle GNUBraceStyle = getLLVMStyle(); 9318 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9319 verifyFormat("namespace a\n" 9320 "{\n" 9321 "class A\n" 9322 "{\n" 9323 " void f()\n" 9324 " {\n" 9325 " int a;\n" 9326 " {\n" 9327 " int b;\n" 9328 " }\n" 9329 " if (true)\n" 9330 " {\n" 9331 " a();\n" 9332 " b();\n" 9333 " }\n" 9334 " }\n" 9335 " void g() { return; }\n" 9336 "}\n" 9337 "}", 9338 GNUBraceStyle); 9339 9340 verifyFormat("void f()\n" 9341 "{\n" 9342 " if (true)\n" 9343 " {\n" 9344 " a();\n" 9345 " }\n" 9346 " else if (false)\n" 9347 " {\n" 9348 " b();\n" 9349 " }\n" 9350 " else\n" 9351 " {\n" 9352 " c();\n" 9353 " }\n" 9354 "}\n", 9355 GNUBraceStyle); 9356 9357 verifyFormat("void f()\n" 9358 "{\n" 9359 " for (int i = 0; i < 10; ++i)\n" 9360 " {\n" 9361 " a();\n" 9362 " }\n" 9363 " while (false)\n" 9364 " {\n" 9365 " b();\n" 9366 " }\n" 9367 " do\n" 9368 " {\n" 9369 " c();\n" 9370 " }\n" 9371 " while (false);\n" 9372 "}\n", 9373 GNUBraceStyle); 9374 9375 verifyFormat("void f(int a)\n" 9376 "{\n" 9377 " switch (a)\n" 9378 " {\n" 9379 " case 0:\n" 9380 " break;\n" 9381 " case 1:\n" 9382 " {\n" 9383 " break;\n" 9384 " }\n" 9385 " case 2:\n" 9386 " {\n" 9387 " }\n" 9388 " break;\n" 9389 " default:\n" 9390 " break;\n" 9391 " }\n" 9392 "}\n", 9393 GNUBraceStyle); 9394 9395 verifyFormat("enum X\n" 9396 "{\n" 9397 " Y = 0,\n" 9398 "}\n", 9399 GNUBraceStyle); 9400 9401 verifyFormat("@interface BSApplicationController ()\n" 9402 "{\n" 9403 "@private\n" 9404 " id _extraIvar;\n" 9405 "}\n" 9406 "@end\n", 9407 GNUBraceStyle); 9408 9409 verifyFormat("#ifdef _DEBUG\n" 9410 "int foo(int i = 0)\n" 9411 "#else\n" 9412 "int foo(int i = 5)\n" 9413 "#endif\n" 9414 "{\n" 9415 " return i;\n" 9416 "}", 9417 GNUBraceStyle); 9418 9419 verifyFormat("void foo() {}\n" 9420 "void bar()\n" 9421 "#ifdef _DEBUG\n" 9422 "{\n" 9423 " foo();\n" 9424 "}\n" 9425 "#else\n" 9426 "{\n" 9427 "}\n" 9428 "#endif", 9429 GNUBraceStyle); 9430 9431 verifyFormat("void foobar() { int i = 5; }\n" 9432 "#ifdef _DEBUG\n" 9433 "void bar() {}\n" 9434 "#else\n" 9435 "void bar() { foobar(); }\n" 9436 "#endif", 9437 GNUBraceStyle); 9438 } 9439 9440 TEST_F(FormatTest, WebKitBraceBreaking) { 9441 FormatStyle WebKitBraceStyle = getLLVMStyle(); 9442 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 9443 WebKitBraceStyle.FixNamespaceComments = false; 9444 verifyFormat("namespace a {\n" 9445 "class A {\n" 9446 " void f()\n" 9447 " {\n" 9448 " if (true) {\n" 9449 " a();\n" 9450 " b();\n" 9451 " }\n" 9452 " }\n" 9453 " void g() { return; }\n" 9454 "};\n" 9455 "enum E {\n" 9456 " A,\n" 9457 " // foo\n" 9458 " B,\n" 9459 " C\n" 9460 "};\n" 9461 "struct B {\n" 9462 " int x;\n" 9463 "};\n" 9464 "}\n", 9465 WebKitBraceStyle); 9466 verifyFormat("struct S {\n" 9467 " int Type;\n" 9468 " union {\n" 9469 " int x;\n" 9470 " double y;\n" 9471 " } Value;\n" 9472 " class C {\n" 9473 " MyFavoriteType Value;\n" 9474 " } Class;\n" 9475 "};\n", 9476 WebKitBraceStyle); 9477 } 9478 9479 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9480 verifyFormat("void f() {\n" 9481 " try {\n" 9482 " } catch (const Exception &e) {\n" 9483 " }\n" 9484 "}\n", 9485 getLLVMStyle()); 9486 } 9487 9488 TEST_F(FormatTest, UnderstandsPragmas) { 9489 verifyFormat("#pragma omp reduction(| : var)"); 9490 verifyFormat("#pragma omp reduction(+ : var)"); 9491 9492 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9493 "(including parentheses).", 9494 format("#pragma mark Any non-hyphenated or hyphenated string " 9495 "(including parentheses).")); 9496 } 9497 9498 TEST_F(FormatTest, UnderstandPragmaOption) { 9499 verifyFormat("#pragma option -C -A"); 9500 9501 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9502 } 9503 9504 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9505 for (size_t i = 1; i < Styles.size(); ++i) \ 9506 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9507 << " differs from Style #0" 9508 9509 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9510 SmallVector<FormatStyle, 3> Styles; 9511 Styles.resize(3); 9512 9513 Styles[0] = getLLVMStyle(); 9514 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9515 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9516 EXPECT_ALL_STYLES_EQUAL(Styles); 9517 9518 Styles[0] = getGoogleStyle(); 9519 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9520 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9521 EXPECT_ALL_STYLES_EQUAL(Styles); 9522 9523 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9524 EXPECT_TRUE( 9525 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9526 EXPECT_TRUE( 9527 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9528 EXPECT_ALL_STYLES_EQUAL(Styles); 9529 9530 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9531 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9532 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9533 EXPECT_ALL_STYLES_EQUAL(Styles); 9534 9535 Styles[0] = getMozillaStyle(); 9536 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9537 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9538 EXPECT_ALL_STYLES_EQUAL(Styles); 9539 9540 Styles[0] = getWebKitStyle(); 9541 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9542 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9543 EXPECT_ALL_STYLES_EQUAL(Styles); 9544 9545 Styles[0] = getGNUStyle(); 9546 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9547 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9548 EXPECT_ALL_STYLES_EQUAL(Styles); 9549 9550 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9551 } 9552 9553 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9554 SmallVector<FormatStyle, 8> Styles; 9555 Styles.resize(2); 9556 9557 Styles[0] = getGoogleStyle(); 9558 Styles[1] = getLLVMStyle(); 9559 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9560 EXPECT_ALL_STYLES_EQUAL(Styles); 9561 9562 Styles.resize(5); 9563 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9564 Styles[1] = getLLVMStyle(); 9565 Styles[1].Language = FormatStyle::LK_JavaScript; 9566 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9567 9568 Styles[2] = getLLVMStyle(); 9569 Styles[2].Language = FormatStyle::LK_JavaScript; 9570 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9571 "BasedOnStyle: Google", 9572 &Styles[2]) 9573 .value()); 9574 9575 Styles[3] = getLLVMStyle(); 9576 Styles[3].Language = FormatStyle::LK_JavaScript; 9577 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9578 "Language: JavaScript", 9579 &Styles[3]) 9580 .value()); 9581 9582 Styles[4] = getLLVMStyle(); 9583 Styles[4].Language = FormatStyle::LK_JavaScript; 9584 EXPECT_EQ(0, parseConfiguration("---\n" 9585 "BasedOnStyle: LLVM\n" 9586 "IndentWidth: 123\n" 9587 "---\n" 9588 "BasedOnStyle: Google\n" 9589 "Language: JavaScript", 9590 &Styles[4]) 9591 .value()); 9592 EXPECT_ALL_STYLES_EQUAL(Styles); 9593 } 9594 9595 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9596 Style.FIELD = false; \ 9597 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9598 EXPECT_TRUE(Style.FIELD); \ 9599 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9600 EXPECT_FALSE(Style.FIELD); 9601 9602 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9603 9604 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 9605 Style.STRUCT.FIELD = false; \ 9606 EXPECT_EQ(0, \ 9607 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 9608 .value()); \ 9609 EXPECT_TRUE(Style.STRUCT.FIELD); \ 9610 EXPECT_EQ(0, \ 9611 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 9612 .value()); \ 9613 EXPECT_FALSE(Style.STRUCT.FIELD); 9614 9615 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 9616 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 9617 9618 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9619 EXPECT_NE(VALUE, Style.FIELD); \ 9620 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9621 EXPECT_EQ(VALUE, Style.FIELD) 9622 9623 TEST_F(FormatTest, ParsesConfigurationBools) { 9624 FormatStyle Style = {}; 9625 Style.Language = FormatStyle::LK_Cpp; 9626 CHECK_PARSE_BOOL(AlignOperands); 9627 CHECK_PARSE_BOOL(AlignTrailingComments); 9628 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9629 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations); 9630 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9631 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9632 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9633 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 9634 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 9635 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 9636 CHECK_PARSE_BOOL(BinPackArguments); 9637 CHECK_PARSE_BOOL(BinPackParameters); 9638 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 9639 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 9640 CHECK_PARSE_BOOL(BreakStringLiterals); 9641 CHECK_PARSE_BOOL(BreakBeforeInheritanceComma) 9642 CHECK_PARSE_BOOL(CompactNamespaces); 9643 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 9644 CHECK_PARSE_BOOL(DerivePointerAlignment); 9645 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 9646 CHECK_PARSE_BOOL(DisableFormat); 9647 CHECK_PARSE_BOOL(IndentCaseLabels); 9648 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 9649 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 9650 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 9651 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 9652 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 9653 CHECK_PARSE_BOOL(ReflowComments); 9654 CHECK_PARSE_BOOL(SortIncludes); 9655 CHECK_PARSE_BOOL(SortUsingDeclarations); 9656 CHECK_PARSE_BOOL(SpacesInParentheses); 9657 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 9658 CHECK_PARSE_BOOL(SpacesInAngles); 9659 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 9660 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 9661 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 9662 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 9663 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 9664 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 9665 9666 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 9667 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); 9668 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 9669 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 9670 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 9671 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 9672 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 9673 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 9674 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 9675 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 9676 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 9677 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 9678 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 9679 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 9680 } 9681 9682 #undef CHECK_PARSE_BOOL 9683 9684 TEST_F(FormatTest, ParsesConfiguration) { 9685 FormatStyle Style = {}; 9686 Style.Language = FormatStyle::LK_Cpp; 9687 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 9688 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 9689 ConstructorInitializerIndentWidth, 1234u); 9690 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 9691 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 9692 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 9693 CHECK_PARSE("PenaltyBreakAssignment: 1234", 9694 PenaltyBreakAssignment, 1234u); 9695 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 9696 PenaltyBreakBeforeFirstCallParameter, 1234u); 9697 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 9698 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 9699 PenaltyReturnTypeOnItsOwnLine, 1234u); 9700 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 9701 SpacesBeforeTrailingComments, 1234u); 9702 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 9703 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 9704 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 9705 9706 Style.PointerAlignment = FormatStyle::PAS_Middle; 9707 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 9708 FormatStyle::PAS_Left); 9709 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 9710 FormatStyle::PAS_Right); 9711 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 9712 FormatStyle::PAS_Middle); 9713 // For backward compatibility: 9714 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 9715 FormatStyle::PAS_Left); 9716 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 9717 FormatStyle::PAS_Right); 9718 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 9719 FormatStyle::PAS_Middle); 9720 9721 Style.Standard = FormatStyle::LS_Auto; 9722 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 9723 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 9724 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 9725 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 9726 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 9727 9728 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9729 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 9730 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 9731 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 9732 FormatStyle::BOS_None); 9733 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 9734 FormatStyle::BOS_All); 9735 // For backward compatibility: 9736 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 9737 FormatStyle::BOS_None); 9738 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 9739 FormatStyle::BOS_All); 9740 9741 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 9742 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 9743 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 9744 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 9745 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 9746 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 9747 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 9748 // For backward compatibility: 9749 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 9750 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 9751 9752 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9753 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 9754 FormatStyle::BAS_Align); 9755 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 9756 FormatStyle::BAS_DontAlign); 9757 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 9758 FormatStyle::BAS_AlwaysBreak); 9759 // For backward compatibility: 9760 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 9761 FormatStyle::BAS_DontAlign); 9762 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 9763 FormatStyle::BAS_Align); 9764 9765 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 9766 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 9767 FormatStyle::ENAS_DontAlign); 9768 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 9769 FormatStyle::ENAS_Left); 9770 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 9771 FormatStyle::ENAS_Right); 9772 // For backward compatibility: 9773 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 9774 FormatStyle::ENAS_Left); 9775 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 9776 FormatStyle::ENAS_Right); 9777 9778 Style.UseTab = FormatStyle::UT_ForIndentation; 9779 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 9780 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 9781 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 9782 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 9783 FormatStyle::UT_ForContinuationAndIndentation); 9784 // For backward compatibility: 9785 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 9786 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 9787 9788 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 9789 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 9790 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9791 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 9792 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 9793 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 9794 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 9795 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 9796 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9797 // For backward compatibility: 9798 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 9799 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9800 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 9801 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9802 9803 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 9804 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 9805 FormatStyle::SBPO_Never); 9806 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 9807 FormatStyle::SBPO_Always); 9808 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 9809 FormatStyle::SBPO_ControlStatements); 9810 // For backward compatibility: 9811 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 9812 FormatStyle::SBPO_Never); 9813 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 9814 FormatStyle::SBPO_ControlStatements); 9815 9816 Style.ColumnLimit = 123; 9817 FormatStyle BaseStyle = getLLVMStyle(); 9818 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 9819 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 9820 9821 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9822 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 9823 FormatStyle::BS_Attach); 9824 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 9825 FormatStyle::BS_Linux); 9826 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 9827 FormatStyle::BS_Mozilla); 9828 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 9829 FormatStyle::BS_Stroustrup); 9830 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 9831 FormatStyle::BS_Allman); 9832 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 9833 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 9834 FormatStyle::BS_WebKit); 9835 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 9836 FormatStyle::BS_Custom); 9837 9838 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9839 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 9840 FormatStyle::RTBS_None); 9841 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 9842 FormatStyle::RTBS_All); 9843 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 9844 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 9845 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 9846 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 9847 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 9848 AlwaysBreakAfterReturnType, 9849 FormatStyle::RTBS_TopLevelDefinitions); 9850 9851 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 9852 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 9853 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 9854 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 9855 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 9856 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 9857 AlwaysBreakAfterDefinitionReturnType, 9858 FormatStyle::DRTBS_TopLevel); 9859 9860 Style.NamespaceIndentation = FormatStyle::NI_All; 9861 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 9862 FormatStyle::NI_None); 9863 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 9864 FormatStyle::NI_Inner); 9865 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 9866 FormatStyle::NI_All); 9867 9868 // FIXME: This is required because parsing a configuration simply overwrites 9869 // the first N elements of the list instead of resetting it. 9870 Style.ForEachMacros.clear(); 9871 std::vector<std::string> BoostForeach; 9872 BoostForeach.push_back("BOOST_FOREACH"); 9873 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 9874 std::vector<std::string> BoostAndQForeach; 9875 BoostAndQForeach.push_back("BOOST_FOREACH"); 9876 BoostAndQForeach.push_back("Q_FOREACH"); 9877 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 9878 BoostAndQForeach); 9879 9880 Style.IncludeCategories.clear(); 9881 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2}, 9882 {".*", 1}}; 9883 CHECK_PARSE("IncludeCategories:\n" 9884 " - Regex: abc/.*\n" 9885 " Priority: 2\n" 9886 " - Regex: .*\n" 9887 " Priority: 1", 9888 IncludeCategories, ExpectedCategories); 9889 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$"); 9890 } 9891 9892 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 9893 FormatStyle Style = {}; 9894 Style.Language = FormatStyle::LK_Cpp; 9895 CHECK_PARSE("Language: Cpp\n" 9896 "IndentWidth: 12", 9897 IndentWidth, 12u); 9898 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 9899 "IndentWidth: 34", 9900 &Style), 9901 ParseError::Unsuitable); 9902 EXPECT_EQ(12u, Style.IndentWidth); 9903 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9904 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9905 9906 Style.Language = FormatStyle::LK_JavaScript; 9907 CHECK_PARSE("Language: JavaScript\n" 9908 "IndentWidth: 12", 9909 IndentWidth, 12u); 9910 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 9911 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 9912 "IndentWidth: 34", 9913 &Style), 9914 ParseError::Unsuitable); 9915 EXPECT_EQ(23u, Style.IndentWidth); 9916 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9917 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9918 9919 CHECK_PARSE("BasedOnStyle: LLVM\n" 9920 "IndentWidth: 67", 9921 IndentWidth, 67u); 9922 9923 CHECK_PARSE("---\n" 9924 "Language: JavaScript\n" 9925 "IndentWidth: 12\n" 9926 "---\n" 9927 "Language: Cpp\n" 9928 "IndentWidth: 34\n" 9929 "...\n", 9930 IndentWidth, 12u); 9931 9932 Style.Language = FormatStyle::LK_Cpp; 9933 CHECK_PARSE("---\n" 9934 "Language: JavaScript\n" 9935 "IndentWidth: 12\n" 9936 "---\n" 9937 "Language: Cpp\n" 9938 "IndentWidth: 34\n" 9939 "...\n", 9940 IndentWidth, 34u); 9941 CHECK_PARSE("---\n" 9942 "IndentWidth: 78\n" 9943 "---\n" 9944 "Language: JavaScript\n" 9945 "IndentWidth: 56\n" 9946 "...\n", 9947 IndentWidth, 78u); 9948 9949 Style.ColumnLimit = 123; 9950 Style.IndentWidth = 234; 9951 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 9952 Style.TabWidth = 345; 9953 EXPECT_FALSE(parseConfiguration("---\n" 9954 "IndentWidth: 456\n" 9955 "BreakBeforeBraces: Allman\n" 9956 "---\n" 9957 "Language: JavaScript\n" 9958 "IndentWidth: 111\n" 9959 "TabWidth: 111\n" 9960 "---\n" 9961 "Language: Cpp\n" 9962 "BreakBeforeBraces: Stroustrup\n" 9963 "TabWidth: 789\n" 9964 "...\n", 9965 &Style)); 9966 EXPECT_EQ(123u, Style.ColumnLimit); 9967 EXPECT_EQ(456u, Style.IndentWidth); 9968 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 9969 EXPECT_EQ(789u, Style.TabWidth); 9970 9971 EXPECT_EQ(parseConfiguration("---\n" 9972 "Language: JavaScript\n" 9973 "IndentWidth: 56\n" 9974 "---\n" 9975 "IndentWidth: 78\n" 9976 "...\n", 9977 &Style), 9978 ParseError::Error); 9979 EXPECT_EQ(parseConfiguration("---\n" 9980 "Language: JavaScript\n" 9981 "IndentWidth: 56\n" 9982 "---\n" 9983 "Language: JavaScript\n" 9984 "IndentWidth: 78\n" 9985 "...\n", 9986 &Style), 9987 ParseError::Error); 9988 9989 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9990 } 9991 9992 #undef CHECK_PARSE 9993 9994 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 9995 FormatStyle Style = {}; 9996 Style.Language = FormatStyle::LK_JavaScript; 9997 Style.BreakBeforeTernaryOperators = true; 9998 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 9999 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10000 10001 Style.BreakBeforeTernaryOperators = true; 10002 EXPECT_EQ(0, parseConfiguration("---\n" 10003 "BasedOnStyle: Google\n" 10004 "---\n" 10005 "Language: JavaScript\n" 10006 "IndentWidth: 76\n" 10007 "...\n", 10008 &Style) 10009 .value()); 10010 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 10011 EXPECT_EQ(76u, Style.IndentWidth); 10012 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 10013 } 10014 10015 TEST_F(FormatTest, ConfigurationRoundTripTest) { 10016 FormatStyle Style = getLLVMStyle(); 10017 std::string YAML = configurationAsText(Style); 10018 FormatStyle ParsedStyle = {}; 10019 ParsedStyle.Language = FormatStyle::LK_Cpp; 10020 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 10021 EXPECT_EQ(Style, ParsedStyle); 10022 } 10023 10024 TEST_F(FormatTest, WorksFor8bitEncodings) { 10025 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 10026 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 10027 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 10028 "\"\xef\xee\xf0\xf3...\"", 10029 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 10030 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 10031 "\xef\xee\xf0\xf3...\"", 10032 getLLVMStyleWithColumns(12))); 10033 } 10034 10035 TEST_F(FormatTest, HandlesUTF8BOM) { 10036 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 10037 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 10038 format("\xef\xbb\xbf#include <iostream>")); 10039 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 10040 format("\xef\xbb\xbf\n#include <iostream>")); 10041 } 10042 10043 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 10044 #if !defined(_MSC_VER) 10045 10046 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 10047 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 10048 getLLVMStyleWithColumns(35)); 10049 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 10050 getLLVMStyleWithColumns(31)); 10051 verifyFormat("// Однажды в студёную зимнюю пору...", 10052 getLLVMStyleWithColumns(36)); 10053 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 10054 verifyFormat("/* Однажды в студёную зимнюю пору... */", 10055 getLLVMStyleWithColumns(39)); 10056 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 10057 getLLVMStyleWithColumns(35)); 10058 } 10059 10060 TEST_F(FormatTest, SplitsUTF8Strings) { 10061 // Non-printable characters' width is currently considered to be the length in 10062 // bytes in UTF8. The characters can be displayed in very different manner 10063 // (zero-width, single width with a substitution glyph, expanded to their code 10064 // (e.g. "<8d>"), so there's no single correct way to handle them. 10065 EXPECT_EQ("\"aaaaÄ\"\n" 10066 "\"\xc2\x8d\";", 10067 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10068 EXPECT_EQ("\"aaaaaaaÄ\"\n" 10069 "\"\xc2\x8d\";", 10070 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 10071 EXPECT_EQ("\"Однажды, в \"\n" 10072 "\"студёную \"\n" 10073 "\"зимнюю \"\n" 10074 "\"пору,\"", 10075 format("\"Однажды, в студёную зимнюю пору,\"", 10076 getLLVMStyleWithColumns(13))); 10077 EXPECT_EQ( 10078 "\"一 二 三 \"\n" 10079 "\"四 五六 \"\n" 10080 "\"七 八 九 \"\n" 10081 "\"十\"", 10082 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 10083 EXPECT_EQ("\"一\t二 \"\n" 10084 "\"\t三 \"\n" 10085 "\"四 五\t六 \"\n" 10086 "\"\t七 \"\n" 10087 "\"八九十\tqq\"", 10088 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 10089 getLLVMStyleWithColumns(11))); 10090 10091 // UTF8 character in an escape sequence. 10092 EXPECT_EQ("\"aaaaaa\"\n" 10093 "\"\\\xC2\x8D\"", 10094 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 10095 } 10096 10097 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 10098 EXPECT_EQ("const char *sssss =\n" 10099 " \"一二三四五六七八\\\n" 10100 " 九 十\";", 10101 format("const char *sssss = \"一二三四五六七八\\\n" 10102 " 九 十\";", 10103 getLLVMStyleWithColumns(30))); 10104 } 10105 10106 TEST_F(FormatTest, SplitsUTF8LineComments) { 10107 EXPECT_EQ("// aaaaÄ\xc2\x8d", 10108 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 10109 EXPECT_EQ("// Я из лесу\n" 10110 "// вышел; был\n" 10111 "// сильный\n" 10112 "// мороз.", 10113 format("// Я из лесу вышел; был сильный мороз.", 10114 getLLVMStyleWithColumns(13))); 10115 EXPECT_EQ("// 一二三\n" 10116 "// 四五六七\n" 10117 "// 八 九\n" 10118 "// 十", 10119 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 10120 } 10121 10122 TEST_F(FormatTest, SplitsUTF8BlockComments) { 10123 EXPECT_EQ("/* Гляжу,\n" 10124 " * поднимается\n" 10125 " * медленно в\n" 10126 " * гору\n" 10127 " * Лошадка,\n" 10128 " * везущая\n" 10129 " * хворосту\n" 10130 " * воз. */", 10131 format("/* Гляжу, поднимается медленно в гору\n" 10132 " * Лошадка, везущая хворосту воз. */", 10133 getLLVMStyleWithColumns(13))); 10134 EXPECT_EQ( 10135 "/* 一二三\n" 10136 " * 四五六七\n" 10137 " * 八 九\n" 10138 " * 十 */", 10139 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 10140 EXPECT_EQ("/* \n" 10141 " * \n" 10142 " * - */", 10143 format("/* - */", getLLVMStyleWithColumns(12))); 10144 } 10145 10146 #endif // _MSC_VER 10147 10148 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 10149 FormatStyle Style = getLLVMStyle(); 10150 10151 Style.ConstructorInitializerIndentWidth = 4; 10152 verifyFormat( 10153 "SomeClass::Constructor()\n" 10154 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10155 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10156 Style); 10157 10158 Style.ConstructorInitializerIndentWidth = 2; 10159 verifyFormat( 10160 "SomeClass::Constructor()\n" 10161 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10162 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10163 Style); 10164 10165 Style.ConstructorInitializerIndentWidth = 0; 10166 verifyFormat( 10167 "SomeClass::Constructor()\n" 10168 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 10169 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 10170 Style); 10171 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10172 verifyFormat( 10173 "SomeLongTemplateVariableName<\n" 10174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 10175 Style); 10176 verifyFormat( 10177 "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 10178 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 10179 Style); 10180 } 10181 10182 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 10183 FormatStyle Style = getLLVMStyle(); 10184 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 10185 Style.ConstructorInitializerIndentWidth = 4; 10186 verifyFormat("SomeClass::Constructor()\n" 10187 " : a(a)\n" 10188 " , b(b)\n" 10189 " , c(c) {}", 10190 Style); 10191 verifyFormat("SomeClass::Constructor()\n" 10192 " : a(a) {}", 10193 Style); 10194 10195 Style.ColumnLimit = 0; 10196 verifyFormat("SomeClass::Constructor()\n" 10197 " : a(a) {}", 10198 Style); 10199 verifyFormat("SomeClass::Constructor() noexcept\n" 10200 " : a(a) {}", 10201 Style); 10202 verifyFormat("SomeClass::Constructor()\n" 10203 " : a(a)\n" 10204 " , b(b)\n" 10205 " , c(c) {}", 10206 Style); 10207 verifyFormat("SomeClass::Constructor()\n" 10208 " : a(a) {\n" 10209 " foo();\n" 10210 " bar();\n" 10211 "}", 10212 Style); 10213 10214 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10215 verifyFormat("SomeClass::Constructor()\n" 10216 " : a(a)\n" 10217 " , b(b)\n" 10218 " , c(c) {\n}", 10219 Style); 10220 verifyFormat("SomeClass::Constructor()\n" 10221 " : a(a) {\n}", 10222 Style); 10223 10224 Style.ColumnLimit = 80; 10225 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 10226 Style.ConstructorInitializerIndentWidth = 2; 10227 verifyFormat("SomeClass::Constructor()\n" 10228 " : a(a)\n" 10229 " , b(b)\n" 10230 " , c(c) {}", 10231 Style); 10232 10233 Style.ConstructorInitializerIndentWidth = 0; 10234 verifyFormat("SomeClass::Constructor()\n" 10235 ": a(a)\n" 10236 ", b(b)\n" 10237 ", c(c) {}", 10238 Style); 10239 10240 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 10241 Style.ConstructorInitializerIndentWidth = 4; 10242 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 10243 verifyFormat( 10244 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 10245 Style); 10246 verifyFormat( 10247 "SomeClass::Constructor()\n" 10248 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 10249 Style); 10250 Style.ConstructorInitializerIndentWidth = 4; 10251 Style.ColumnLimit = 60; 10252 verifyFormat("SomeClass::Constructor()\n" 10253 " : aaaaaaaa(aaaaaaaa)\n" 10254 " , aaaaaaaa(aaaaaaaa)\n" 10255 " , aaaaaaaa(aaaaaaaa) {}", 10256 Style); 10257 } 10258 10259 TEST_F(FormatTest, Destructors) { 10260 verifyFormat("void F(int &i) { i.~int(); }"); 10261 verifyFormat("void F(int &i) { i->~int(); }"); 10262 } 10263 10264 TEST_F(FormatTest, FormatsWithWebKitStyle) { 10265 FormatStyle Style = getWebKitStyle(); 10266 10267 // Don't indent in outer namespaces. 10268 verifyFormat("namespace outer {\n" 10269 "int i;\n" 10270 "namespace inner {\n" 10271 " int i;\n" 10272 "} // namespace inner\n" 10273 "} // namespace outer\n" 10274 "namespace other_outer {\n" 10275 "int i;\n" 10276 "}", 10277 Style); 10278 10279 // Don't indent case labels. 10280 verifyFormat("switch (variable) {\n" 10281 "case 1:\n" 10282 "case 2:\n" 10283 " doSomething();\n" 10284 " break;\n" 10285 "default:\n" 10286 " ++variable;\n" 10287 "}", 10288 Style); 10289 10290 // Wrap before binary operators. 10291 EXPECT_EQ("void f()\n" 10292 "{\n" 10293 " if (aaaaaaaaaaaaaaaa\n" 10294 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 10295 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10296 " return;\n" 10297 "}", 10298 format("void f() {\n" 10299 "if (aaaaaaaaaaaaaaaa\n" 10300 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 10301 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 10302 "return;\n" 10303 "}", 10304 Style)); 10305 10306 // Allow functions on a single line. 10307 verifyFormat("void f() { return; }", Style); 10308 10309 // Constructor initializers are formatted one per line with the "," on the 10310 // new line. 10311 verifyFormat("Constructor()\n" 10312 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 10313 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 10314 " aaaaaaaaaaaaaa)\n" 10315 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 10316 "{\n" 10317 "}", 10318 Style); 10319 verifyFormat("SomeClass::Constructor()\n" 10320 " : a(a)\n" 10321 "{\n" 10322 "}", 10323 Style); 10324 EXPECT_EQ("SomeClass::Constructor()\n" 10325 " : a(a)\n" 10326 "{\n" 10327 "}", 10328 format("SomeClass::Constructor():a(a){}", Style)); 10329 verifyFormat("SomeClass::Constructor()\n" 10330 " : a(a)\n" 10331 " , b(b)\n" 10332 " , c(c)\n" 10333 "{\n" 10334 "}", 10335 Style); 10336 verifyFormat("SomeClass::Constructor()\n" 10337 " : a(a)\n" 10338 "{\n" 10339 " foo();\n" 10340 " bar();\n" 10341 "}", 10342 Style); 10343 10344 // Access specifiers should be aligned left. 10345 verifyFormat("class C {\n" 10346 "public:\n" 10347 " int i;\n" 10348 "};", 10349 Style); 10350 10351 // Do not align comments. 10352 verifyFormat("int a; // Do not\n" 10353 "double b; // align comments.", 10354 Style); 10355 10356 // Do not align operands. 10357 EXPECT_EQ("ASSERT(aaaa\n" 10358 " || bbbb);", 10359 format("ASSERT ( aaaa\n||bbbb);", Style)); 10360 10361 // Accept input's line breaks. 10362 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 10363 " || bbbbbbbbbbbbbbb) {\n" 10364 " i++;\n" 10365 "}", 10366 format("if (aaaaaaaaaaaaaaa\n" 10367 "|| bbbbbbbbbbbbbbb) { i++; }", 10368 Style)); 10369 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 10370 " i++;\n" 10371 "}", 10372 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10373 10374 // Don't automatically break all macro definitions (llvm.org/PR17842). 10375 verifyFormat("#define aNumber 10", Style); 10376 // However, generally keep the line breaks that the user authored. 10377 EXPECT_EQ("#define aNumber \\\n" 10378 " 10", 10379 format("#define aNumber \\\n" 10380 " 10", 10381 Style)); 10382 10383 // Keep empty and one-element array literals on a single line. 10384 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10385 " copyItems:YES];", 10386 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10387 "copyItems:YES];", 10388 Style)); 10389 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10390 " copyItems:YES];", 10391 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10392 " copyItems:YES];", 10393 Style)); 10394 // FIXME: This does not seem right, there should be more indentation before 10395 // the array literal's entries. Nested blocks have the same problem. 10396 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10397 " @\"a\",\n" 10398 " @\"a\"\n" 10399 "]\n" 10400 " copyItems:YES];", 10401 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10402 " @\"a\",\n" 10403 " @\"a\"\n" 10404 " ]\n" 10405 " copyItems:YES];", 10406 Style)); 10407 EXPECT_EQ( 10408 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10409 " copyItems:YES];", 10410 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10411 " copyItems:YES];", 10412 Style)); 10413 10414 verifyFormat("[self.a b:c c:d];", Style); 10415 EXPECT_EQ("[self.a b:c\n" 10416 " c:d];", 10417 format("[self.a b:c\n" 10418 "c:d];", 10419 Style)); 10420 } 10421 10422 TEST_F(FormatTest, FormatsLambdas) { 10423 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10424 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10425 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10426 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10427 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10428 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10429 verifyFormat("int x = f(*+[] {});"); 10430 verifyFormat("void f() {\n" 10431 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10432 "}\n"); 10433 verifyFormat("void f() {\n" 10434 " other(x.begin(), //\n" 10435 " x.end(), //\n" 10436 " [&](int, int) { return 1; });\n" 10437 "}\n"); 10438 verifyFormat("SomeFunction([]() { // A cool function...\n" 10439 " return 43;\n" 10440 "});"); 10441 EXPECT_EQ("SomeFunction([]() {\n" 10442 "#define A a\n" 10443 " return 43;\n" 10444 "});", 10445 format("SomeFunction([](){\n" 10446 "#define A a\n" 10447 "return 43;\n" 10448 "});")); 10449 verifyFormat("void f() {\n" 10450 " SomeFunction([](decltype(x), A *a) {});\n" 10451 "}"); 10452 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10453 " [](const aaaaaaaaaa &a) { return a; });"); 10454 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10455 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10456 "});"); 10457 verifyFormat("Constructor()\n" 10458 " : Field([] { // comment\n" 10459 " int i;\n" 10460 " }) {}"); 10461 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10462 " return some_parameter.size();\n" 10463 "};"); 10464 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 10465 " [](const string &s) { return s; };"); 10466 verifyFormat("int i = aaaaaa ? 1 //\n" 10467 " : [] {\n" 10468 " return 2; //\n" 10469 " }();"); 10470 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10471 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10472 " return x == 2; // force break\n" 10473 " });"); 10474 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10475 " [=](int iiiiiiiiiiii) {\n" 10476 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 10477 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 10478 " });", 10479 getLLVMStyleWithColumns(60)); 10480 verifyFormat("SomeFunction({[&] {\n" 10481 " // comment\n" 10482 " },\n" 10483 " [&] {\n" 10484 " // comment\n" 10485 " }});"); 10486 verifyFormat("SomeFunction({[&] {\n" 10487 " // comment\n" 10488 "}});"); 10489 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n" 10490 " [&]() { return true; },\n" 10491 " aaaaa aaaaaaaaa);"); 10492 10493 // Lambdas with return types. 10494 verifyFormat("int c = []() -> int { return 2; }();\n"); 10495 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10496 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10497 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10498 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10499 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10500 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10501 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10502 verifyFormat("[a, a]() -> a<1> {};"); 10503 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10504 " int j) -> int {\n" 10505 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10506 "};"); 10507 verifyFormat( 10508 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10509 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10510 " return aaaaaaaaaaaaaaaaa;\n" 10511 " });", 10512 getLLVMStyleWithColumns(70)); 10513 verifyFormat("[]() //\n" 10514 " -> int {\n" 10515 " return 1; //\n" 10516 "};"); 10517 10518 // Multiple lambdas in the same parentheses change indentation rules. 10519 verifyFormat("SomeFunction(\n" 10520 " []() {\n" 10521 " int i = 42;\n" 10522 " return i;\n" 10523 " },\n" 10524 " []() {\n" 10525 " int j = 43;\n" 10526 " return j;\n" 10527 " });"); 10528 10529 // More complex introducers. 10530 verifyFormat("return [i, args...] {};"); 10531 10532 // Not lambdas. 10533 verifyFormat("constexpr char hello[]{\"hello\"};"); 10534 verifyFormat("double &operator[](int i) { return 0; }\n" 10535 "int i;"); 10536 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10537 verifyFormat("int i = a[a][a]->f();"); 10538 verifyFormat("int i = (*b)[a]->f();"); 10539 10540 // Other corner cases. 10541 verifyFormat("void f() {\n" 10542 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10543 " );\n" 10544 "}"); 10545 10546 // Lambdas created through weird macros. 10547 verifyFormat("void f() {\n" 10548 " MACRO((const AA &a) { return 1; });\n" 10549 " MACRO((AA &a) { return 1; });\n" 10550 "}"); 10551 10552 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10553 " doo_dah();\n" 10554 " doo_dah();\n" 10555 " })) {\n" 10556 "}"); 10557 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 10558 " doo_dah();\n" 10559 " doo_dah();\n" 10560 " })) {\n" 10561 "}"); 10562 verifyFormat("auto lambda = []() {\n" 10563 " int a = 2\n" 10564 "#if A\n" 10565 " + 2\n" 10566 "#endif\n" 10567 " ;\n" 10568 "};"); 10569 10570 // Lambdas with complex multiline introducers. 10571 verifyFormat( 10572 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10573 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 10574 " -> ::std::unordered_set<\n" 10575 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 10576 " //\n" 10577 " });"); 10578 } 10579 10580 TEST_F(FormatTest, FormatsBlocks) { 10581 FormatStyle ShortBlocks = getLLVMStyle(); 10582 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10583 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10584 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10585 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10586 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10587 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10588 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10589 10590 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10591 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10592 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10593 10594 verifyFormat("[operation setCompletionBlock:^{\n" 10595 " [self onOperationDone];\n" 10596 "}];"); 10597 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10598 " [self onOperationDone];\n" 10599 "}]};"); 10600 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10601 " f();\n" 10602 "}];"); 10603 verifyFormat("int a = [operation block:^int(int *i) {\n" 10604 " return 1;\n" 10605 "}];"); 10606 verifyFormat("[myObject doSomethingWith:arg1\n" 10607 " aaa:^int(int *a) {\n" 10608 " return 1;\n" 10609 " }\n" 10610 " bbb:f(a * bbbbbbbb)];"); 10611 10612 verifyFormat("[operation setCompletionBlock:^{\n" 10613 " [self.delegate newDataAvailable];\n" 10614 "}];", 10615 getLLVMStyleWithColumns(60)); 10616 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10617 " NSString *path = [self sessionFilePath];\n" 10618 " if (path) {\n" 10619 " // ...\n" 10620 " }\n" 10621 "});"); 10622 verifyFormat("[[SessionService sharedService]\n" 10623 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10624 " if (window) {\n" 10625 " [self windowDidLoad:window];\n" 10626 " } else {\n" 10627 " [self errorLoadingWindow];\n" 10628 " }\n" 10629 " }];"); 10630 verifyFormat("void (^largeBlock)(void) = ^{\n" 10631 " // ...\n" 10632 "};\n", 10633 getLLVMStyleWithColumns(40)); 10634 verifyFormat("[[SessionService sharedService]\n" 10635 " loadWindowWithCompletionBlock: //\n" 10636 " ^(SessionWindow *window) {\n" 10637 " if (window) {\n" 10638 " [self windowDidLoad:window];\n" 10639 " } else {\n" 10640 " [self errorLoadingWindow];\n" 10641 " }\n" 10642 " }];", 10643 getLLVMStyleWithColumns(60)); 10644 verifyFormat("[myObject doSomethingWith:arg1\n" 10645 " firstBlock:^(Foo *a) {\n" 10646 " // ...\n" 10647 " int i;\n" 10648 " }\n" 10649 " secondBlock:^(Bar *b) {\n" 10650 " // ...\n" 10651 " int i;\n" 10652 " }\n" 10653 " thirdBlock:^Foo(Bar *b) {\n" 10654 " // ...\n" 10655 " int i;\n" 10656 " }];"); 10657 verifyFormat("[myObject doSomethingWith:arg1\n" 10658 " firstBlock:-1\n" 10659 " secondBlock:^(Bar *b) {\n" 10660 " // ...\n" 10661 " int i;\n" 10662 " }];"); 10663 10664 verifyFormat("f(^{\n" 10665 " @autoreleasepool {\n" 10666 " if (a) {\n" 10667 " g();\n" 10668 " }\n" 10669 " }\n" 10670 "});"); 10671 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 10672 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 10673 "};"); 10674 10675 FormatStyle FourIndent = getLLVMStyle(); 10676 FourIndent.ObjCBlockIndentWidth = 4; 10677 verifyFormat("[operation setCompletionBlock:^{\n" 10678 " [self onOperationDone];\n" 10679 "}];", 10680 FourIndent); 10681 } 10682 10683 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 10684 FormatStyle ZeroColumn = getLLVMStyle(); 10685 ZeroColumn.ColumnLimit = 0; 10686 10687 verifyFormat("[[SessionService sharedService] " 10688 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10689 " if (window) {\n" 10690 " [self windowDidLoad:window];\n" 10691 " } else {\n" 10692 " [self errorLoadingWindow];\n" 10693 " }\n" 10694 "}];", 10695 ZeroColumn); 10696 EXPECT_EQ("[[SessionService sharedService]\n" 10697 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10698 " if (window) {\n" 10699 " [self windowDidLoad:window];\n" 10700 " } else {\n" 10701 " [self errorLoadingWindow];\n" 10702 " }\n" 10703 " }];", 10704 format("[[SessionService sharedService]\n" 10705 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10706 " if (window) {\n" 10707 " [self windowDidLoad:window];\n" 10708 " } else {\n" 10709 " [self errorLoadingWindow];\n" 10710 " }\n" 10711 "}];", 10712 ZeroColumn)); 10713 verifyFormat("[myObject doSomethingWith:arg1\n" 10714 " firstBlock:^(Foo *a) {\n" 10715 " // ...\n" 10716 " int i;\n" 10717 " }\n" 10718 " secondBlock:^(Bar *b) {\n" 10719 " // ...\n" 10720 " int i;\n" 10721 " }\n" 10722 " thirdBlock:^Foo(Bar *b) {\n" 10723 " // ...\n" 10724 " int i;\n" 10725 " }];", 10726 ZeroColumn); 10727 verifyFormat("f(^{\n" 10728 " @autoreleasepool {\n" 10729 " if (a) {\n" 10730 " g();\n" 10731 " }\n" 10732 " }\n" 10733 "});", 10734 ZeroColumn); 10735 verifyFormat("void (^largeBlock)(void) = ^{\n" 10736 " // ...\n" 10737 "};", 10738 ZeroColumn); 10739 10740 ZeroColumn.AllowShortBlocksOnASingleLine = true; 10741 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 10742 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10743 ZeroColumn.AllowShortBlocksOnASingleLine = false; 10744 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 10745 " int i;\n" 10746 "};", 10747 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10748 } 10749 10750 TEST_F(FormatTest, SupportsCRLF) { 10751 EXPECT_EQ("int a;\r\n" 10752 "int b;\r\n" 10753 "int c;\r\n", 10754 format("int a;\r\n" 10755 " int b;\r\n" 10756 " int c;\r\n", 10757 getLLVMStyle())); 10758 EXPECT_EQ("int a;\r\n" 10759 "int b;\r\n" 10760 "int c;\r\n", 10761 format("int a;\r\n" 10762 " int b;\n" 10763 " int c;\r\n", 10764 getLLVMStyle())); 10765 EXPECT_EQ("int a;\n" 10766 "int b;\n" 10767 "int c;\n", 10768 format("int a;\r\n" 10769 " int b;\n" 10770 " int c;\n", 10771 getLLVMStyle())); 10772 EXPECT_EQ("\"aaaaaaa \"\r\n" 10773 "\"bbbbbbb\";\r\n", 10774 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 10775 EXPECT_EQ("#define A \\\r\n" 10776 " b; \\\r\n" 10777 " c; \\\r\n" 10778 " d;\r\n", 10779 format("#define A \\\r\n" 10780 " b; \\\r\n" 10781 " c; d; \r\n", 10782 getGoogleStyle())); 10783 10784 EXPECT_EQ("/*\r\n" 10785 "multi line block comments\r\n" 10786 "should not introduce\r\n" 10787 "an extra carriage return\r\n" 10788 "*/\r\n", 10789 format("/*\r\n" 10790 "multi line block comments\r\n" 10791 "should not introduce\r\n" 10792 "an extra carriage return\r\n" 10793 "*/\r\n")); 10794 } 10795 10796 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 10797 verifyFormat("MY_CLASS(C) {\n" 10798 " int i;\n" 10799 " int j;\n" 10800 "};"); 10801 } 10802 10803 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 10804 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 10805 TwoIndent.ContinuationIndentWidth = 2; 10806 10807 EXPECT_EQ("int i =\n" 10808 " longFunction(\n" 10809 " arg);", 10810 format("int i = longFunction(arg);", TwoIndent)); 10811 10812 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 10813 SixIndent.ContinuationIndentWidth = 6; 10814 10815 EXPECT_EQ("int i =\n" 10816 " longFunction(\n" 10817 " arg);", 10818 format("int i = longFunction(arg);", SixIndent)); 10819 } 10820 10821 TEST_F(FormatTest, SpacesInAngles) { 10822 FormatStyle Spaces = getLLVMStyle(); 10823 Spaces.SpacesInAngles = true; 10824 10825 verifyFormat("static_cast< int >(arg);", Spaces); 10826 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 10827 verifyFormat("f< int, float >();", Spaces); 10828 verifyFormat("template <> g() {}", Spaces); 10829 verifyFormat("template < std::vector< int > > f() {}", Spaces); 10830 verifyFormat("std::function< void(int, int) > fct;", Spaces); 10831 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 10832 Spaces); 10833 10834 Spaces.Standard = FormatStyle::LS_Cpp03; 10835 Spaces.SpacesInAngles = true; 10836 verifyFormat("A< A< int > >();", Spaces); 10837 10838 Spaces.SpacesInAngles = false; 10839 verifyFormat("A<A<int> >();", Spaces); 10840 10841 Spaces.Standard = FormatStyle::LS_Cpp11; 10842 Spaces.SpacesInAngles = true; 10843 verifyFormat("A< A< int > >();", Spaces); 10844 10845 Spaces.SpacesInAngles = false; 10846 verifyFormat("A<A<int>>();", Spaces); 10847 } 10848 10849 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 10850 FormatStyle Style = getLLVMStyle(); 10851 Style.SpaceAfterTemplateKeyword = false; 10852 verifyFormat("template<int> void foo();", Style); 10853 } 10854 10855 TEST_F(FormatTest, TripleAngleBrackets) { 10856 verifyFormat("f<<<1, 1>>>();"); 10857 verifyFormat("f<<<1, 1, 1, s>>>();"); 10858 verifyFormat("f<<<a, b, c, d>>>();"); 10859 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 10860 verifyFormat("f<param><<<1, 1>>>();"); 10861 verifyFormat("f<1><<<1, 1>>>();"); 10862 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 10863 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10864 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 10865 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 10866 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 10867 } 10868 10869 TEST_F(FormatTest, MergeLessLessAtEnd) { 10870 verifyFormat("<<"); 10871 EXPECT_EQ("< < <", format("\\\n<<<")); 10872 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10873 "aaallvm::outs() <<"); 10874 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10875 "aaaallvm::outs()\n <<"); 10876 } 10877 10878 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 10879 std::string code = "#if A\n" 10880 "#if B\n" 10881 "a.\n" 10882 "#endif\n" 10883 " a = 1;\n" 10884 "#else\n" 10885 "#endif\n" 10886 "#if C\n" 10887 "#else\n" 10888 "#endif\n"; 10889 EXPECT_EQ(code, format(code)); 10890 } 10891 10892 TEST_F(FormatTest, HandleConflictMarkers) { 10893 // Git/SVN conflict markers. 10894 EXPECT_EQ("int a;\n" 10895 "void f() {\n" 10896 " callme(some(parameter1,\n" 10897 "<<<<<<< text by the vcs\n" 10898 " parameter2),\n" 10899 "||||||| text by the vcs\n" 10900 " parameter2),\n" 10901 " parameter3,\n" 10902 "======= text by the vcs\n" 10903 " parameter2, parameter3),\n" 10904 ">>>>>>> text by the vcs\n" 10905 " otherparameter);\n", 10906 format("int a;\n" 10907 "void f() {\n" 10908 " callme(some(parameter1,\n" 10909 "<<<<<<< text by the vcs\n" 10910 " parameter2),\n" 10911 "||||||| text by the vcs\n" 10912 " parameter2),\n" 10913 " parameter3,\n" 10914 "======= text by the vcs\n" 10915 " parameter2,\n" 10916 " parameter3),\n" 10917 ">>>>>>> text by the vcs\n" 10918 " otherparameter);\n")); 10919 10920 // Perforce markers. 10921 EXPECT_EQ("void f() {\n" 10922 " function(\n" 10923 ">>>> text by the vcs\n" 10924 " parameter,\n" 10925 "==== text by the vcs\n" 10926 " parameter,\n" 10927 "==== text by the vcs\n" 10928 " parameter,\n" 10929 "<<<< text by the vcs\n" 10930 " parameter);\n", 10931 format("void f() {\n" 10932 " function(\n" 10933 ">>>> text by the vcs\n" 10934 " parameter,\n" 10935 "==== text by the vcs\n" 10936 " parameter,\n" 10937 "==== text by the vcs\n" 10938 " parameter,\n" 10939 "<<<< text by the vcs\n" 10940 " parameter);\n")); 10941 10942 EXPECT_EQ("<<<<<<<\n" 10943 "|||||||\n" 10944 "=======\n" 10945 ">>>>>>>", 10946 format("<<<<<<<\n" 10947 "|||||||\n" 10948 "=======\n" 10949 ">>>>>>>")); 10950 10951 EXPECT_EQ("<<<<<<<\n" 10952 "|||||||\n" 10953 "int i;\n" 10954 "=======\n" 10955 ">>>>>>>", 10956 format("<<<<<<<\n" 10957 "|||||||\n" 10958 "int i;\n" 10959 "=======\n" 10960 ">>>>>>>")); 10961 10962 // FIXME: Handle parsing of macros around conflict markers correctly: 10963 EXPECT_EQ("#define Macro \\\n" 10964 "<<<<<<<\n" 10965 "Something \\\n" 10966 "|||||||\n" 10967 "Else \\\n" 10968 "=======\n" 10969 "Other \\\n" 10970 ">>>>>>>\n" 10971 " End int i;\n", 10972 format("#define Macro \\\n" 10973 "<<<<<<<\n" 10974 " Something \\\n" 10975 "|||||||\n" 10976 " Else \\\n" 10977 "=======\n" 10978 " Other \\\n" 10979 ">>>>>>>\n" 10980 " End\n" 10981 "int i;\n")); 10982 } 10983 10984 TEST_F(FormatTest, DisableRegions) { 10985 EXPECT_EQ("int i;\n" 10986 "// clang-format off\n" 10987 " int j;\n" 10988 "// clang-format on\n" 10989 "int k;", 10990 format(" int i;\n" 10991 " // clang-format off\n" 10992 " int j;\n" 10993 " // clang-format on\n" 10994 " int k;")); 10995 EXPECT_EQ("int i;\n" 10996 "/* clang-format off */\n" 10997 " int j;\n" 10998 "/* clang-format on */\n" 10999 "int k;", 11000 format(" int i;\n" 11001 " /* clang-format off */\n" 11002 " int j;\n" 11003 " /* clang-format on */\n" 11004 " int k;")); 11005 11006 // Don't reflow comments within disabled regions. 11007 EXPECT_EQ( 11008 "// clang-format off\n" 11009 "// long long long long long long line\n" 11010 "/* clang-format on */\n" 11011 "/* long long long\n" 11012 " * long long long\n" 11013 " * line */\n" 11014 "int i;\n" 11015 "/* clang-format off */\n" 11016 "/* long long long long long long line */\n", 11017 format("// clang-format off\n" 11018 "// long long long long long long line\n" 11019 "/* clang-format on */\n" 11020 "/* long long long long long long line */\n" 11021 "int i;\n" 11022 "/* clang-format off */\n" 11023 "/* long long long long long long line */\n", 11024 getLLVMStyleWithColumns(20))); 11025 } 11026 11027 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 11028 format("? ) ="); 11029 verifyNoCrash("#define a\\\n /**/}"); 11030 } 11031 11032 TEST_F(FormatTest, FormatsTableGenCode) { 11033 FormatStyle Style = getLLVMStyle(); 11034 Style.Language = FormatStyle::LK_TableGen; 11035 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 11036 } 11037 11038 TEST_F(FormatTest, ArrayOfTemplates) { 11039 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 11040 format("auto a = new unique_ptr<int > [ 10];")); 11041 11042 FormatStyle Spaces = getLLVMStyle(); 11043 Spaces.SpacesInSquareBrackets = true; 11044 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 11045 format("auto a = new unique_ptr<int > [10];", Spaces)); 11046 } 11047 11048 TEST_F(FormatTest, ArrayAsTemplateType) { 11049 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 11050 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 11051 11052 FormatStyle Spaces = getLLVMStyle(); 11053 Spaces.SpacesInSquareBrackets = true; 11054 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 11055 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 11056 } 11057 11058 TEST_F(FormatTest, NoSpaceAfterSuper) { 11059 verifyFormat("__super::FooBar();"); 11060 } 11061 11062 TEST(FormatStyle, GetStyleOfFile) { 11063 vfs::InMemoryFileSystem FS; 11064 // Test 1: format file in the same directory. 11065 ASSERT_TRUE( 11066 FS.addFile("/a/.clang-format", 0, 11067 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 11068 ASSERT_TRUE( 11069 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11070 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 11071 ASSERT_TRUE((bool)Style1); 11072 ASSERT_EQ(*Style1, getLLVMStyle()); 11073 11074 // Test 2.1: fallback to default. 11075 ASSERT_TRUE( 11076 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11077 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 11078 ASSERT_TRUE((bool)Style2); 11079 ASSERT_EQ(*Style2, getMozillaStyle()); 11080 11081 // Test 2.2: no format on 'none' fallback style. 11082 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11083 ASSERT_TRUE((bool)Style2); 11084 ASSERT_EQ(*Style2, getNoStyle()); 11085 11086 // Test 2.3: format if config is found with no based style while fallback is 11087 // 'none'. 11088 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 11089 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 11090 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 11091 ASSERT_TRUE((bool)Style2); 11092 ASSERT_EQ(*Style2, getLLVMStyle()); 11093 11094 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 11095 Style2 = getStyle("{}", "a.h", "none", "", &FS); 11096 ASSERT_TRUE((bool)Style2); 11097 ASSERT_EQ(*Style2, getLLVMStyle()); 11098 11099 // Test 3: format file in parent directory. 11100 ASSERT_TRUE( 11101 FS.addFile("/c/.clang-format", 0, 11102 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 11103 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 11104 llvm::MemoryBuffer::getMemBuffer("int i;"))); 11105 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 11106 ASSERT_TRUE((bool)Style3); 11107 ASSERT_EQ(*Style3, getGoogleStyle()); 11108 11109 // Test 4: error on invalid fallback style 11110 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 11111 ASSERT_FALSE((bool)Style4); 11112 llvm::consumeError(Style4.takeError()); 11113 11114 // Test 5: error on invalid yaml on command line 11115 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 11116 ASSERT_FALSE((bool)Style5); 11117 llvm::consumeError(Style5.takeError()); 11118 11119 // Test 6: error on invalid style 11120 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 11121 ASSERT_FALSE((bool)Style6); 11122 llvm::consumeError(Style6.takeError()); 11123 11124 // Test 7: found config file, error on parsing it 11125 ASSERT_TRUE( 11126 FS.addFile("/d/.clang-format", 0, 11127 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 11128 "InvalidKey: InvalidValue"))); 11129 ASSERT_TRUE( 11130 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 11131 auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 11132 ASSERT_FALSE((bool)Style7); 11133 llvm::consumeError(Style7.takeError()); 11134 } 11135 11136 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 11137 // Column limit is 20. 11138 std::string Code = "Type *a =\n" 11139 " new Type();\n" 11140 "g(iiiii, 0, jjjjj,\n" 11141 " 0, kkkkk, 0, mm);\n" 11142 "int bad = format ;"; 11143 std::string Expected = "auto a = new Type();\n" 11144 "g(iiiii, nullptr,\n" 11145 " jjjjj, nullptr,\n" 11146 " kkkkk, nullptr,\n" 11147 " mm);\n" 11148 "int bad = format ;"; 11149 FileID ID = Context.createInMemoryFile("format.cpp", Code); 11150 tooling::Replacements Replaces = toReplacements( 11151 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 11152 "auto "), 11153 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 11154 "nullptr"), 11155 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 11156 "nullptr"), 11157 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 11158 "nullptr")}); 11159 11160 format::FormatStyle Style = format::getLLVMStyle(); 11161 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 11162 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11163 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11164 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11165 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11166 EXPECT_TRUE(static_cast<bool>(Result)); 11167 EXPECT_EQ(Expected, *Result); 11168 } 11169 11170 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 11171 std::string Code = "#include \"a.h\"\n" 11172 "#include \"c.h\"\n" 11173 "\n" 11174 "int main() {\n" 11175 " return 0;\n" 11176 "}"; 11177 std::string Expected = "#include \"a.h\"\n" 11178 "#include \"b.h\"\n" 11179 "#include \"c.h\"\n" 11180 "\n" 11181 "int main() {\n" 11182 " return 0;\n" 11183 "}"; 11184 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 11185 tooling::Replacements Replaces = toReplacements( 11186 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 11187 "#include \"b.h\"\n")}); 11188 11189 format::FormatStyle Style = format::getLLVMStyle(); 11190 Style.SortIncludes = true; 11191 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 11192 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 11193 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 11194 auto Result = applyAllReplacements(Code, *FormattedReplaces); 11195 EXPECT_TRUE(static_cast<bool>(Result)); 11196 EXPECT_EQ(Expected, *Result); 11197 } 11198 11199 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 11200 EXPECT_EQ("using std::cin;\n" 11201 "using std::cout;", 11202 format("using std::cout;\n" 11203 "using std::cin;", getGoogleStyle())); 11204 } 11205 11206 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 11207 format::FormatStyle Style = format::getLLVMStyle(); 11208 Style.Standard = FormatStyle::LS_Cpp03; 11209 // cpp03 recognize this string as identifier u8 and literal character 'a' 11210 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 11211 } 11212 11213 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 11214 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 11215 // all modes, including C++11, C++14 and C++17 11216 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 11217 } 11218 11219 } // end namespace 11220 } // end namespace format 11221 } // end namespace clang 11222