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 "FormatTestUtils.h" 11 #include "clang/Format/Format.h" 12 #include "llvm/Support/Debug.h" 13 #include "gtest/gtest.h" 14 15 #define DEBUG_TYPE "format-test" 16 17 namespace clang { 18 namespace format { 19 20 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 21 22 class FormatTest : public ::testing::Test { 23 protected: 24 enum IncompleteCheck { 25 IC_ExpectComplete, 26 IC_ExpectIncomplete, 27 IC_DoNotCheck 28 }; 29 30 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, 31 const FormatStyle &Style, 32 IncompleteCheck CheckIncomplete = IC_ExpectComplete) { 33 DEBUG(llvm::errs() << "---\n"); 34 DEBUG(llvm::errs() << Code << "\n\n"); 35 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length)); 36 bool IncompleteFormat = false; 37 tooling::Replacements Replaces = 38 reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat); 39 if (CheckIncomplete != IC_DoNotCheck) { 40 bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete; 41 EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n"; 42 } 43 ReplacementCount = Replaces.size(); 44 std::string Result = applyAllReplacements(Code, Replaces); 45 EXPECT_NE("", Result); 46 DEBUG(llvm::errs() << "\n" << Result << "\n\n"); 47 return Result; 48 } 49 50 std::string format(llvm::StringRef Code, 51 const FormatStyle &Style = getLLVMStyle(), 52 IncompleteCheck CheckIncomplete = IC_ExpectComplete) { 53 return format(Code, 0, Code.size(), Style, CheckIncomplete); 54 } 55 56 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 57 FormatStyle Style = getLLVMStyle(); 58 Style.ColumnLimit = ColumnLimit; 59 return Style; 60 } 61 62 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 63 FormatStyle Style = getGoogleStyle(); 64 Style.ColumnLimit = ColumnLimit; 65 return Style; 66 } 67 68 void verifyFormat(llvm::StringRef Code, 69 const FormatStyle &Style = getLLVMStyle()) { 70 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 71 } 72 73 void verifyIncompleteFormat(llvm::StringRef Code, 74 const FormatStyle &Style = getLLVMStyle()) { 75 EXPECT_EQ(Code.str(), 76 format(test::messUp(Code), Style, IC_ExpectIncomplete)); 77 } 78 79 void verifyGoogleFormat(llvm::StringRef Code) { 80 verifyFormat(Code, getGoogleStyle()); 81 } 82 83 void verifyIndependentOfContext(llvm::StringRef text) { 84 verifyFormat(text); 85 verifyFormat(llvm::Twine("void f() { " + text + " }").str()); 86 } 87 88 /// \brief Verify that clang-format does not crash on the given input. 89 void verifyNoCrash(llvm::StringRef Code, 90 const FormatStyle &Style = getLLVMStyle()) { 91 format(Code, Style, IC_DoNotCheck); 92 } 93 94 int ReplacementCount; 95 }; 96 97 TEST_F(FormatTest, MessUp) { 98 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 99 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 100 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 101 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 102 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 103 } 104 105 //===----------------------------------------------------------------------===// 106 // Basic function tests. 107 //===----------------------------------------------------------------------===// 108 109 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 110 EXPECT_EQ(";", format(";")); 111 } 112 113 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 114 EXPECT_EQ("int i;", format(" int i;")); 115 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 116 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 117 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 118 } 119 120 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 121 EXPECT_EQ("int i;", format("int\ni;")); 122 } 123 124 TEST_F(FormatTest, FormatsNestedBlockStatements) { 125 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 126 } 127 128 TEST_F(FormatTest, FormatsNestedCall) { 129 verifyFormat("Method(f1, f2(f3));"); 130 verifyFormat("Method(f1(f2, f3()));"); 131 verifyFormat("Method(f1(f2, (f3())));"); 132 } 133 134 TEST_F(FormatTest, NestedNameSpecifiers) { 135 verifyFormat("vector<::Type> v;"); 136 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 137 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 138 verifyFormat("bool a = 2 < ::SomeFunction();"); 139 } 140 141 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 142 EXPECT_EQ("if (a) {\n" 143 " f();\n" 144 "}", 145 format("if(a){f();}")); 146 EXPECT_EQ(4, ReplacementCount); 147 EXPECT_EQ("if (a) {\n" 148 " f();\n" 149 "}", 150 format("if (a) {\n" 151 " f();\n" 152 "}")); 153 EXPECT_EQ(0, ReplacementCount); 154 } 155 156 TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) { 157 EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle())); 158 EXPECT_EQ("int a;", format("int a; ")); 159 EXPECT_EQ("int a;\n", format("int a; \n \n \n ")); 160 EXPECT_EQ("int a;\nint b; ", 161 format("int a; \nint b; ", 0, 0, getLLVMStyle())); 162 } 163 164 TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) { 165 EXPECT_EQ("int b;\nint a;", 166 format("int b;\n int a;", 7, 0, getLLVMStyle())); 167 EXPECT_EQ("int b;\n int a;", 168 format("int b;\n int a;", 6, 0, getLLVMStyle())); 169 170 EXPECT_EQ("#define A \\\n" 171 " int a; \\\n" 172 " int b;", 173 format("#define A \\\n" 174 " int a; \\\n" 175 " int b;", 176 26, 0, getLLVMStyleWithColumns(12))); 177 EXPECT_EQ("#define A \\\n" 178 " int a; \\\n" 179 " int b;", 180 format("#define A \\\n" 181 " int a; \\\n" 182 " int b;", 183 25, 0, getLLVMStyleWithColumns(12))); 184 } 185 186 TEST_F(FormatTest, FormatLineWhenInvokedOnTrailingNewline) { 187 EXPECT_EQ("int b;\n\nint a;", 188 format("int b;\n\nint a;", 8, 0, getLLVMStyle())); 189 EXPECT_EQ("int b;\n\nint a;", 190 format("int b;\n\nint a;", 7, 0, getLLVMStyle())); 191 192 // This might not strictly be correct, but is likely good in all practical 193 // cases. 194 EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0, getLLVMStyle())); 195 } 196 197 TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) { 198 EXPECT_EQ("int a;\n\n int b;", 199 format("int a;\n \n\n int b;", 8, 0, getLLVMStyle())); 200 EXPECT_EQ("int a;\n\n int b;", 201 format("int a;\n \n\n int b;", 9, 0, getLLVMStyle())); 202 } 203 204 TEST_F(FormatTest, RemovesEmptyLines) { 205 EXPECT_EQ("class C {\n" 206 " int i;\n" 207 "};", 208 format("class C {\n" 209 " int i;\n" 210 "\n" 211 "};")); 212 213 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 214 EXPECT_EQ("namespace N {\n" 215 "\n" 216 "int i;\n" 217 "}", 218 format("namespace N {\n" 219 "\n" 220 "int i;\n" 221 "}", 222 getGoogleStyle())); 223 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 224 "\n" 225 "int i;\n" 226 "}", 227 format("extern /**/ \"C\" /**/ {\n" 228 "\n" 229 "int i;\n" 230 "}", 231 getGoogleStyle())); 232 233 // ...but do keep inlining and removing empty lines for non-block extern "C" 234 // functions. 235 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 236 EXPECT_EQ("extern \"C\" int f() {\n" 237 " int i = 42;\n" 238 " return i;\n" 239 "}", 240 format("extern \"C\" int f() {\n" 241 "\n" 242 " int i = 42;\n" 243 " return i;\n" 244 "}", 245 getGoogleStyle())); 246 247 // Remove empty lines at the beginning and end of blocks. 248 EXPECT_EQ("void f() {\n" 249 "\n" 250 " if (a) {\n" 251 "\n" 252 " f();\n" 253 " }\n" 254 "}", 255 format("void f() {\n" 256 "\n" 257 " if (a) {\n" 258 "\n" 259 " f();\n" 260 "\n" 261 " }\n" 262 "\n" 263 "}", 264 getLLVMStyle())); 265 EXPECT_EQ("void f() {\n" 266 " if (a) {\n" 267 " f();\n" 268 " }\n" 269 "}", 270 format("void f() {\n" 271 "\n" 272 " if (a) {\n" 273 "\n" 274 " f();\n" 275 "\n" 276 " }\n" 277 "\n" 278 "}", 279 getGoogleStyle())); 280 281 // Don't remove empty lines in more complex control statements. 282 EXPECT_EQ("void f() {\n" 283 " if (a) {\n" 284 " f();\n" 285 "\n" 286 " } else if (b) {\n" 287 " f();\n" 288 " }\n" 289 "}", 290 format("void f() {\n" 291 " if (a) {\n" 292 " f();\n" 293 "\n" 294 " } else if (b) {\n" 295 " f();\n" 296 "\n" 297 " }\n" 298 "\n" 299 "}")); 300 301 // FIXME: This is slightly inconsistent. 302 EXPECT_EQ("namespace {\n" 303 "int i;\n" 304 "}", 305 format("namespace {\n" 306 "int i;\n" 307 "\n" 308 "}")); 309 EXPECT_EQ("namespace {\n" 310 "int i;\n" 311 "\n" 312 "} // namespace", 313 format("namespace {\n" 314 "int i;\n" 315 "\n" 316 "} // namespace")); 317 } 318 319 TEST_F(FormatTest, ReformatsMovedLines) { 320 EXPECT_EQ( 321 "template <typename T> T *getFETokenInfo() const {\n" 322 " return static_cast<T *>(FETokenInfo);\n" 323 "}\n" 324 " int a; // <- Should not be formatted", 325 format( 326 "template<typename T>\n" 327 "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n" 328 " int a; // <- Should not be formatted", 329 9, 5, getLLVMStyle())); 330 } 331 332 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 333 verifyFormat("x = (a) and (b);"); 334 verifyFormat("x = (a) or (b);"); 335 verifyFormat("x = (a) bitand (b);"); 336 verifyFormat("x = (a) bitor (b);"); 337 verifyFormat("x = (a) not_eq (b);"); 338 verifyFormat("x = (a) and_eq (b);"); 339 verifyFormat("x = (a) or_eq (b);"); 340 verifyFormat("x = (a) xor (b);"); 341 } 342 343 //===----------------------------------------------------------------------===// 344 // Tests for control statements. 345 //===----------------------------------------------------------------------===// 346 347 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 348 verifyFormat("if (true)\n f();\ng();"); 349 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 350 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 351 352 FormatStyle AllowsMergedIf = getLLVMStyle(); 353 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 354 verifyFormat("if (a)\n" 355 " // comment\n" 356 " f();", 357 AllowsMergedIf); 358 verifyFormat("if (a)\n" 359 " ;", 360 AllowsMergedIf); 361 verifyFormat("if (a)\n" 362 " if (b) return;", 363 AllowsMergedIf); 364 365 verifyFormat("if (a) // Can't merge this\n" 366 " f();\n", 367 AllowsMergedIf); 368 verifyFormat("if (a) /* still don't merge */\n" 369 " f();", 370 AllowsMergedIf); 371 verifyFormat("if (a) { // Never merge this\n" 372 " f();\n" 373 "}", 374 AllowsMergedIf); 375 verifyFormat("if (a) {/* Never merge this */\n" 376 " f();\n" 377 "}", 378 AllowsMergedIf); 379 380 EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf)); 381 EXPECT_EQ("if (a) return; // comment", 382 format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf)); 383 384 AllowsMergedIf.ColumnLimit = 14; 385 verifyFormat("if (a) return;", AllowsMergedIf); 386 verifyFormat("if (aaaaaaaaa)\n" 387 " return;", 388 AllowsMergedIf); 389 390 AllowsMergedIf.ColumnLimit = 13; 391 verifyFormat("if (a)\n return;", AllowsMergedIf); 392 } 393 394 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 395 FormatStyle AllowsMergedLoops = getLLVMStyle(); 396 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 397 verifyFormat("while (true) continue;", AllowsMergedLoops); 398 verifyFormat("for (;;) continue;", AllowsMergedLoops); 399 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 400 verifyFormat("while (true)\n" 401 " ;", 402 AllowsMergedLoops); 403 verifyFormat("for (;;)\n" 404 " ;", 405 AllowsMergedLoops); 406 verifyFormat("for (;;)\n" 407 " for (;;) continue;", 408 AllowsMergedLoops); 409 verifyFormat("for (;;) // Can't merge this\n" 410 " continue;", 411 AllowsMergedLoops); 412 verifyFormat("for (;;) /* still don't merge */\n" 413 " continue;", 414 AllowsMergedLoops); 415 } 416 417 TEST_F(FormatTest, FormatShortBracedStatements) { 418 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 419 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; 420 421 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true; 422 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 423 424 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 425 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 426 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 427 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 428 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 429 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 430 verifyFormat("if (true) { //\n" 431 " f();\n" 432 "}", 433 AllowSimpleBracedStatements); 434 verifyFormat("if (true) {\n" 435 " f();\n" 436 " f();\n" 437 "}", 438 AllowSimpleBracedStatements); 439 verifyFormat("if (true) {\n" 440 " f();\n" 441 "} else {\n" 442 " f();\n" 443 "}", 444 AllowSimpleBracedStatements); 445 446 verifyFormat("template <int> struct A2 {\n" 447 " struct B {};\n" 448 "};", 449 AllowSimpleBracedStatements); 450 451 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false; 452 verifyFormat("if (true) {\n" 453 " f();\n" 454 "}", 455 AllowSimpleBracedStatements); 456 verifyFormat("if (true) {\n" 457 " f();\n" 458 "} else {\n" 459 " f();\n" 460 "}", 461 AllowSimpleBracedStatements); 462 463 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 464 verifyFormat("while (true) {\n" 465 " f();\n" 466 "}", 467 AllowSimpleBracedStatements); 468 verifyFormat("for (;;) {\n" 469 " f();\n" 470 "}", 471 AllowSimpleBracedStatements); 472 } 473 474 TEST_F(FormatTest, ParseIfElse) { 475 verifyFormat("if (true)\n" 476 " if (true)\n" 477 " if (true)\n" 478 " f();\n" 479 " else\n" 480 " g();\n" 481 " else\n" 482 " h();\n" 483 "else\n" 484 " i();"); 485 verifyFormat("if (true)\n" 486 " if (true)\n" 487 " if (true) {\n" 488 " if (true)\n" 489 " f();\n" 490 " } else {\n" 491 " g();\n" 492 " }\n" 493 " else\n" 494 " h();\n" 495 "else {\n" 496 " i();\n" 497 "}"); 498 verifyFormat("void f() {\n" 499 " if (a) {\n" 500 " } else {\n" 501 " }\n" 502 "}"); 503 } 504 505 TEST_F(FormatTest, ElseIf) { 506 verifyFormat("if (a) {\n} else if (b) {\n}"); 507 verifyFormat("if (a)\n" 508 " f();\n" 509 "else if (b)\n" 510 " g();\n" 511 "else\n" 512 " h();"); 513 verifyFormat("if (a) {\n" 514 " f();\n" 515 "}\n" 516 "// or else ..\n" 517 "else {\n" 518 " g()\n" 519 "}"); 520 521 verifyFormat("if (a) {\n" 522 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 523 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 524 "}"); 525 verifyFormat("if (a) {\n" 526 "} else if (\n" 527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 528 "}", 529 getLLVMStyleWithColumns(62)); 530 } 531 532 TEST_F(FormatTest, FormatsForLoop) { 533 verifyFormat( 534 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 535 " ++VeryVeryLongLoopVariable)\n" 536 " ;"); 537 verifyFormat("for (;;)\n" 538 " f();"); 539 verifyFormat("for (;;) {\n}"); 540 verifyFormat("for (;;) {\n" 541 " f();\n" 542 "}"); 543 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 544 545 verifyFormat( 546 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 547 " E = UnwrappedLines.end();\n" 548 " I != E; ++I) {\n}"); 549 550 verifyFormat( 551 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 552 " ++IIIII) {\n}"); 553 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 554 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 555 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 556 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 557 " I = FD->getDeclsInPrototypeScope().begin(),\n" 558 " E = FD->getDeclsInPrototypeScope().end();\n" 559 " I != E; ++I) {\n}"); 560 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 561 " I = Container.begin(),\n" 562 " E = Container.end();\n" 563 " I != E; ++I) {\n}", 564 getLLVMStyleWithColumns(76)); 565 566 verifyFormat( 567 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 568 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 570 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 571 " ++aaaaaaaaaaa) {\n}"); 572 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 573 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 574 " ++i) {\n}"); 575 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 576 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 577 "}"); 578 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 579 " aaaaaaaaaa);\n" 580 " iter; ++iter) {\n" 581 "}"); 582 583 FormatStyle NoBinPacking = getLLVMStyle(); 584 NoBinPacking.BinPackParameters = false; 585 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 586 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 587 " aaaaaaaaaaaaaaaa,\n" 588 " aaaaaaaaaaaaaaaa,\n" 589 " aaaaaaaaaaaaaaaa);\n" 590 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 591 "}", 592 NoBinPacking); 593 verifyFormat( 594 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 595 " E = UnwrappedLines.end();\n" 596 " I != E;\n" 597 " ++I) {\n}", 598 NoBinPacking); 599 } 600 601 TEST_F(FormatTest, RangeBasedForLoops) { 602 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 603 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 604 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 605 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 606 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 607 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 608 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 609 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 610 } 611 612 TEST_F(FormatTest, ForEachLoops) { 613 verifyFormat("void f() {\n" 614 " foreach (Item *item, itemlist) {}\n" 615 " Q_FOREACH (Item *item, itemlist) {}\n" 616 " BOOST_FOREACH (Item *item, itemlist) {}\n" 617 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 618 "}"); 619 620 // As function-like macros. 621 verifyFormat("#define foreach(x, y)\n" 622 "#define Q_FOREACH(x, y)\n" 623 "#define BOOST_FOREACH(x, y)\n" 624 "#define UNKNOWN_FOREACH(x, y)\n"); 625 626 // Not as function-like macros. 627 verifyFormat("#define foreach (x, y)\n" 628 "#define Q_FOREACH (x, y)\n" 629 "#define BOOST_FOREACH (x, y)\n" 630 "#define UNKNOWN_FOREACH (x, y)\n"); 631 } 632 633 TEST_F(FormatTest, FormatsWhileLoop) { 634 verifyFormat("while (true) {\n}"); 635 verifyFormat("while (true)\n" 636 " f();"); 637 verifyFormat("while () {\n}"); 638 verifyFormat("while () {\n" 639 " f();\n" 640 "}"); 641 } 642 643 TEST_F(FormatTest, FormatsDoWhile) { 644 verifyFormat("do {\n" 645 " do_something();\n" 646 "} while (something());"); 647 verifyFormat("do\n" 648 " do_something();\n" 649 "while (something());"); 650 } 651 652 TEST_F(FormatTest, FormatsSwitchStatement) { 653 verifyFormat("switch (x) {\n" 654 "case 1:\n" 655 " f();\n" 656 " break;\n" 657 "case kFoo:\n" 658 "case ns::kBar:\n" 659 "case kBaz:\n" 660 " break;\n" 661 "default:\n" 662 " g();\n" 663 " break;\n" 664 "}"); 665 verifyFormat("switch (x) {\n" 666 "case 1: {\n" 667 " f();\n" 668 " break;\n" 669 "}\n" 670 "case 2: {\n" 671 " break;\n" 672 "}\n" 673 "}"); 674 verifyFormat("switch (x) {\n" 675 "case 1: {\n" 676 " f();\n" 677 " {\n" 678 " g();\n" 679 " h();\n" 680 " }\n" 681 " break;\n" 682 "}\n" 683 "}"); 684 verifyFormat("switch (x) {\n" 685 "case 1: {\n" 686 " f();\n" 687 " if (foo) {\n" 688 " g();\n" 689 " h();\n" 690 " }\n" 691 " break;\n" 692 "}\n" 693 "}"); 694 verifyFormat("switch (x) {\n" 695 "case 1: {\n" 696 " f();\n" 697 " g();\n" 698 "} break;\n" 699 "}"); 700 verifyFormat("switch (test)\n" 701 " ;"); 702 verifyFormat("switch (x) {\n" 703 "default: {\n" 704 " // Do nothing.\n" 705 "}\n" 706 "}"); 707 verifyFormat("switch (x) {\n" 708 "// comment\n" 709 "// if 1, do f()\n" 710 "case 1:\n" 711 " f();\n" 712 "}"); 713 verifyFormat("switch (x) {\n" 714 "case 1:\n" 715 " // Do amazing stuff\n" 716 " {\n" 717 " f();\n" 718 " g();\n" 719 " }\n" 720 " break;\n" 721 "}"); 722 verifyFormat("#define A \\\n" 723 " switch (x) { \\\n" 724 " case a: \\\n" 725 " foo = b; \\\n" 726 " }", 727 getLLVMStyleWithColumns(20)); 728 verifyFormat("#define OPERATION_CASE(name) \\\n" 729 " case OP_name: \\\n" 730 " return operations::Operation##name\n", 731 getLLVMStyleWithColumns(40)); 732 verifyFormat("switch (x) {\n" 733 "case 1:;\n" 734 "default:;\n" 735 " int i;\n" 736 "}"); 737 738 verifyGoogleFormat("switch (x) {\n" 739 " case 1:\n" 740 " f();\n" 741 " break;\n" 742 " case kFoo:\n" 743 " case ns::kBar:\n" 744 " case kBaz:\n" 745 " break;\n" 746 " default:\n" 747 " g();\n" 748 " break;\n" 749 "}"); 750 verifyGoogleFormat("switch (x) {\n" 751 " case 1: {\n" 752 " f();\n" 753 " break;\n" 754 " }\n" 755 "}"); 756 verifyGoogleFormat("switch (test)\n" 757 " ;"); 758 759 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 760 " case OP_name: \\\n" 761 " return operations::Operation##name\n"); 762 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 763 " // Get the correction operation class.\n" 764 " switch (OpCode) {\n" 765 " CASE(Add);\n" 766 " CASE(Subtract);\n" 767 " default:\n" 768 " return operations::Unknown;\n" 769 " }\n" 770 "#undef OPERATION_CASE\n" 771 "}"); 772 verifyFormat("DEBUG({\n" 773 " switch (x) {\n" 774 " case A:\n" 775 " f();\n" 776 " break;\n" 777 " // On B:\n" 778 " case B:\n" 779 " g();\n" 780 " break;\n" 781 " }\n" 782 "});"); 783 verifyFormat("switch (a) {\n" 784 "case (b):\n" 785 " return;\n" 786 "}"); 787 788 verifyFormat("switch (a) {\n" 789 "case some_namespace::\n" 790 " some_constant:\n" 791 " return;\n" 792 "}", 793 getLLVMStyleWithColumns(34)); 794 } 795 796 TEST_F(FormatTest, CaseRanges) { 797 verifyFormat("switch (x) {\n" 798 "case 'A' ... 'Z':\n" 799 "case 1 ... 5:\n" 800 " break;\n" 801 "}"); 802 } 803 804 TEST_F(FormatTest, ShortCaseLabels) { 805 FormatStyle Style = getLLVMStyle(); 806 Style.AllowShortCaseLabelsOnASingleLine = true; 807 verifyFormat("switch (a) {\n" 808 "case 1: x = 1; break;\n" 809 "case 2: return;\n" 810 "case 3:\n" 811 "case 4:\n" 812 "case 5: return;\n" 813 "case 6: // comment\n" 814 " return;\n" 815 "case 7:\n" 816 " // comment\n" 817 " return;\n" 818 "default: y = 1; break;\n" 819 "}", 820 Style); 821 verifyFormat("switch (a) {\n" 822 "#if FOO\n" 823 "case 0: return 0;\n" 824 "#endif\n" 825 "}", 826 Style); 827 verifyFormat("switch (a) {\n" 828 "case 1: {\n" 829 "}\n" 830 "case 2: {\n" 831 " return;\n" 832 "}\n" 833 "case 3: {\n" 834 " x = 1;\n" 835 " return;\n" 836 "}\n" 837 "case 4:\n" 838 " if (x)\n" 839 " return;\n" 840 "}", 841 Style); 842 Style.ColumnLimit = 21; 843 verifyFormat("switch (a) {\n" 844 "case 1: x = 1; break;\n" 845 "case 2: return;\n" 846 "case 3:\n" 847 "case 4:\n" 848 "case 5: return;\n" 849 "default:\n" 850 " y = 1;\n" 851 " break;\n" 852 "}", 853 Style); 854 } 855 856 TEST_F(FormatTest, FormatsLabels) { 857 verifyFormat("void f() {\n" 858 " some_code();\n" 859 "test_label:\n" 860 " some_other_code();\n" 861 " {\n" 862 " some_more_code();\n" 863 " another_label:\n" 864 " some_more_code();\n" 865 " }\n" 866 "}"); 867 verifyFormat("{\n" 868 " some_code();\n" 869 "test_label:\n" 870 " some_other_code();\n" 871 "}"); 872 verifyFormat("{\n" 873 " some_code();\n" 874 "test_label:;\n" 875 " int i = 0;\n" 876 "}"); 877 } 878 879 //===----------------------------------------------------------------------===// 880 // Tests for comments. 881 //===----------------------------------------------------------------------===// 882 883 TEST_F(FormatTest, UnderstandsSingleLineComments) { 884 verifyFormat("//* */"); 885 verifyFormat("// line 1\n" 886 "// line 2\n" 887 "void f() {}\n"); 888 889 verifyFormat("void f() {\n" 890 " // Doesn't do anything\n" 891 "}"); 892 verifyFormat("SomeObject\n" 893 " // Calling someFunction on SomeObject\n" 894 " .someFunction();"); 895 verifyFormat("auto result = SomeObject\n" 896 " // Calling someFunction on SomeObject\n" 897 " .someFunction();"); 898 verifyFormat("void f(int i, // some comment (probably for i)\n" 899 " int j, // some comment (probably for j)\n" 900 " int k); // some comment (probably for k)"); 901 verifyFormat("void f(int i,\n" 902 " // some comment (probably for j)\n" 903 " int j,\n" 904 " // some comment (probably for k)\n" 905 " int k);"); 906 907 verifyFormat("int i // This is a fancy variable\n" 908 " = 5; // with nicely aligned comment."); 909 910 verifyFormat("// Leading comment.\n" 911 "int a; // Trailing comment."); 912 verifyFormat("int a; // Trailing comment\n" 913 " // on 2\n" 914 " // or 3 lines.\n" 915 "int b;"); 916 verifyFormat("int a; // Trailing comment\n" 917 "\n" 918 "// Leading comment.\n" 919 "int b;"); 920 verifyFormat("int a; // Comment.\n" 921 " // More details.\n" 922 "int bbbb; // Another comment."); 923 verifyFormat( 924 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 925 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 926 "int cccccccccccccccccccccccccccccc; // comment\n" 927 "int ddd; // looooooooooooooooooooooooong comment\n" 928 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 929 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 930 "int ccccccccccccccccccc; // comment"); 931 932 verifyFormat("#include \"a\" // comment\n" 933 "#include \"a/b/c\" // comment"); 934 verifyFormat("#include <a> // comment\n" 935 "#include <a/b/c> // comment"); 936 EXPECT_EQ("#include \"a\" // comment\n" 937 "#include \"a/b/c\" // comment", 938 format("#include \\\n" 939 " \"a\" // comment\n" 940 "#include \"a/b/c\" // comment")); 941 942 verifyFormat("enum E {\n" 943 " // comment\n" 944 " VAL_A, // comment\n" 945 " VAL_B\n" 946 "};"); 947 948 verifyFormat( 949 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 950 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 951 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 952 " // Comment inside a statement.\n" 953 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 954 verifyFormat("SomeFunction(a,\n" 955 " // comment\n" 956 " b + x);"); 957 verifyFormat("SomeFunction(a, a,\n" 958 " // comment\n" 959 " b + x);"); 960 verifyFormat( 961 "bool aaaaaaaaaaaaa = // comment\n" 962 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 963 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 964 965 verifyFormat("int aaaa; // aaaaa\n" 966 "int aa; // aaaaaaa", 967 getLLVMStyleWithColumns(20)); 968 969 EXPECT_EQ("void f() { // This does something ..\n" 970 "}\n" 971 "int a; // This is unrelated", 972 format("void f() { // This does something ..\n" 973 " }\n" 974 "int a; // This is unrelated")); 975 EXPECT_EQ("class C {\n" 976 " void f() { // This does something ..\n" 977 " } // awesome..\n" 978 "\n" 979 " int a; // This is unrelated\n" 980 "};", 981 format("class C{void f() { // This does something ..\n" 982 " } // awesome..\n" 983 " \n" 984 "int a; // This is unrelated\n" 985 "};")); 986 987 EXPECT_EQ("int i; // single line trailing comment", 988 format("int i;\\\n// single line trailing comment")); 989 990 verifyGoogleFormat("int a; // Trailing comment."); 991 992 verifyFormat("someFunction(anotherFunction( // Force break.\n" 993 " parameter));"); 994 995 verifyGoogleFormat("#endif // HEADER_GUARD"); 996 997 verifyFormat("const char *test[] = {\n" 998 " // A\n" 999 " \"aaaa\",\n" 1000 " // B\n" 1001 " \"aaaaa\"};"); 1002 verifyGoogleFormat( 1003 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1004 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); 1005 EXPECT_EQ("D(a, {\n" 1006 " // test\n" 1007 " int a;\n" 1008 "});", 1009 format("D(a, {\n" 1010 "// test\n" 1011 "int a;\n" 1012 "});")); 1013 1014 EXPECT_EQ("lineWith(); // comment\n" 1015 "// at start\n" 1016 "otherLine();", 1017 format("lineWith(); // comment\n" 1018 "// at start\n" 1019 "otherLine();")); 1020 EXPECT_EQ("lineWith(); // comment\n" 1021 " // at start\n" 1022 "otherLine();", 1023 format("lineWith(); // comment\n" 1024 " // at start\n" 1025 "otherLine();")); 1026 1027 EXPECT_EQ("lineWith(); // comment\n" 1028 "// at start\n" 1029 "otherLine(); // comment", 1030 format("lineWith(); // comment\n" 1031 "// at start\n" 1032 "otherLine(); // comment")); 1033 EXPECT_EQ("lineWith();\n" 1034 "// at start\n" 1035 "otherLine(); // comment", 1036 format("lineWith();\n" 1037 " // at start\n" 1038 "otherLine(); // comment")); 1039 EXPECT_EQ("// first\n" 1040 "// at start\n" 1041 "otherLine(); // comment", 1042 format("// first\n" 1043 " // at start\n" 1044 "otherLine(); // comment")); 1045 EXPECT_EQ("f();\n" 1046 "// first\n" 1047 "// at start\n" 1048 "otherLine(); // comment", 1049 format("f();\n" 1050 "// first\n" 1051 " // at start\n" 1052 "otherLine(); // comment")); 1053 verifyFormat("f(); // comment\n" 1054 "// first\n" 1055 "// at start\n" 1056 "otherLine();"); 1057 EXPECT_EQ("f(); // comment\n" 1058 "// first\n" 1059 "// at start\n" 1060 "otherLine();", 1061 format("f(); // comment\n" 1062 "// first\n" 1063 " // at start\n" 1064 "otherLine();")); 1065 EXPECT_EQ("f(); // comment\n" 1066 " // first\n" 1067 "// at start\n" 1068 "otherLine();", 1069 format("f(); // comment\n" 1070 " // first\n" 1071 "// at start\n" 1072 "otherLine();")); 1073 EXPECT_EQ("void f() {\n" 1074 " lineWith(); // comment\n" 1075 " // at start\n" 1076 "}", 1077 format("void f() {\n" 1078 " lineWith(); // comment\n" 1079 " // at start\n" 1080 "}")); 1081 1082 verifyFormat("#define A \\\n" 1083 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1084 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1085 getLLVMStyleWithColumns(60)); 1086 verifyFormat( 1087 "#define A \\\n" 1088 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 1089 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 1090 getLLVMStyleWithColumns(61)); 1091 1092 verifyFormat("if ( // This is some comment\n" 1093 " x + 3) {\n" 1094 "}"); 1095 EXPECT_EQ("if ( // This is some comment\n" 1096 " // spanning two lines\n" 1097 " x + 3) {\n" 1098 "}", 1099 format("if( // This is some comment\n" 1100 " // spanning two lines\n" 1101 " x + 3) {\n" 1102 "}")); 1103 1104 verifyNoCrash("/\\\n/"); 1105 verifyNoCrash("/\\\n* */"); 1106 // The 0-character somehow makes the lexer return a proper comment. 1107 verifyNoCrash(StringRef("/*\\\0\n/", 6)); 1108 } 1109 1110 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 1111 EXPECT_EQ("SomeFunction(a,\n" 1112 " b, // comment\n" 1113 " c);", 1114 format("SomeFunction(a,\n" 1115 " b, // comment\n" 1116 " c);")); 1117 EXPECT_EQ("SomeFunction(a, b,\n" 1118 " // comment\n" 1119 " c);", 1120 format("SomeFunction(a,\n" 1121 " b,\n" 1122 " // comment\n" 1123 " c);")); 1124 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 1125 " c);", 1126 format("SomeFunction(a, b, // comment (unclear relation)\n" 1127 " c);")); 1128 EXPECT_EQ("SomeFunction(a, // comment\n" 1129 " b,\n" 1130 " c); // comment", 1131 format("SomeFunction(a, // comment\n" 1132 " b,\n" 1133 " c); // comment")); 1134 } 1135 1136 TEST_F(FormatTest, CanFormatCommentsLocally) { 1137 EXPECT_EQ("int a; // comment\n" 1138 "int b; // comment", 1139 format("int a; // comment\n" 1140 "int b; // comment", 1141 0, 0, getLLVMStyle())); 1142 EXPECT_EQ("int a; // comment\n" 1143 " // line 2\n" 1144 "int b;", 1145 format("int a; // comment\n" 1146 " // line 2\n" 1147 "int b;", 1148 28, 0, getLLVMStyle())); 1149 EXPECT_EQ("int aaaaaa; // comment\n" 1150 "int b;\n" 1151 "int c; // unrelated comment", 1152 format("int aaaaaa; // comment\n" 1153 "int b;\n" 1154 "int c; // unrelated comment", 1155 31, 0, getLLVMStyle())); 1156 1157 EXPECT_EQ("int a; // This\n" 1158 " // is\n" 1159 " // a", 1160 format("int a; // This\n" 1161 " // is\n" 1162 " // a", 1163 0, 0, getLLVMStyle())); 1164 EXPECT_EQ("int a; // This\n" 1165 " // is\n" 1166 " // a\n" 1167 "// This is b\n" 1168 "int b;", 1169 format("int a; // This\n" 1170 " // is\n" 1171 " // a\n" 1172 "// This is b\n" 1173 "int b;", 1174 0, 0, getLLVMStyle())); 1175 EXPECT_EQ("int a; // This\n" 1176 " // is\n" 1177 " // a\n" 1178 "\n" 1179 " // This is unrelated", 1180 format("int a; // This\n" 1181 " // is\n" 1182 " // a\n" 1183 "\n" 1184 " // This is unrelated", 1185 0, 0, getLLVMStyle())); 1186 EXPECT_EQ("int a;\n" 1187 "// This is\n" 1188 "// not formatted. ", 1189 format("int a;\n" 1190 "// This is\n" 1191 "// not formatted. ", 1192 0, 0, getLLVMStyle())); 1193 } 1194 1195 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { 1196 EXPECT_EQ("// comment", format("// comment ")); 1197 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 1198 format("int aaaaaaa, bbbbbbb; // comment ", 1199 getLLVMStyleWithColumns(33))); 1200 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 1201 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 1202 } 1203 1204 TEST_F(FormatTest, UnderstandsBlockComments) { 1205 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 1206 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }"); 1207 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 1208 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 1209 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 1210 "/* Trailing comment for aa... */\n" 1211 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1212 EXPECT_EQ( 1213 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1214 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 1215 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 1216 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 1217 EXPECT_EQ( 1218 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1219 " aaaaaaaaaaaaaaaaaa,\n" 1220 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1221 "}", 1222 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1223 " aaaaaaaaaaaaaaaaaa ,\n" 1224 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 1225 "}")); 1226 1227 FormatStyle NoBinPacking = getLLVMStyle(); 1228 NoBinPacking.BinPackParameters = false; 1229 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 1230 " /* parameter 2 */ aaaaaa,\n" 1231 " /* parameter 3 */ aaaaaa,\n" 1232 " /* parameter 4 */ aaaaaa);", 1233 NoBinPacking); 1234 1235 // Aligning block comments in macros. 1236 verifyGoogleFormat("#define A \\\n" 1237 " int i; /*a*/ \\\n" 1238 " int jjj; /*b*/"); 1239 } 1240 1241 TEST_F(FormatTest, AlignsBlockComments) { 1242 EXPECT_EQ("/*\n" 1243 " * Really multi-line\n" 1244 " * comment.\n" 1245 " */\n" 1246 "void f() {}", 1247 format(" /*\n" 1248 " * Really multi-line\n" 1249 " * comment.\n" 1250 " */\n" 1251 " void f() {}")); 1252 EXPECT_EQ("class C {\n" 1253 " /*\n" 1254 " * Another multi-line\n" 1255 " * comment.\n" 1256 " */\n" 1257 " void f() {}\n" 1258 "};", 1259 format("class C {\n" 1260 "/*\n" 1261 " * Another multi-line\n" 1262 " * comment.\n" 1263 " */\n" 1264 "void f() {}\n" 1265 "};")); 1266 EXPECT_EQ("/*\n" 1267 " 1. This is a comment with non-trivial formatting.\n" 1268 " 1.1. We have to indent/outdent all lines equally\n" 1269 " 1.1.1. to keep the formatting.\n" 1270 " */", 1271 format(" /*\n" 1272 " 1. This is a comment with non-trivial formatting.\n" 1273 " 1.1. We have to indent/outdent all lines equally\n" 1274 " 1.1.1. to keep the formatting.\n" 1275 " */")); 1276 EXPECT_EQ("/*\n" 1277 "Don't try to outdent if there's not enough indentation.\n" 1278 "*/", 1279 format(" /*\n" 1280 " Don't try to outdent if there's not enough indentation.\n" 1281 " */")); 1282 1283 EXPECT_EQ("int i; /* Comment with empty...\n" 1284 " *\n" 1285 " * line. */", 1286 format("int i; /* Comment with empty...\n" 1287 " *\n" 1288 " * line. */")); 1289 EXPECT_EQ("int foobar = 0; /* comment */\n" 1290 "int bar = 0; /* multiline\n" 1291 " comment 1 */\n" 1292 "int baz = 0; /* multiline\n" 1293 " comment 2 */\n" 1294 "int bzz = 0; /* multiline\n" 1295 " comment 3 */", 1296 format("int foobar = 0; /* comment */\n" 1297 "int bar = 0; /* multiline\n" 1298 " comment 1 */\n" 1299 "int baz = 0; /* multiline\n" 1300 " comment 2 */\n" 1301 "int bzz = 0; /* multiline\n" 1302 " comment 3 */")); 1303 EXPECT_EQ("int foobar = 0; /* comment */\n" 1304 "int bar = 0; /* multiline\n" 1305 " comment */\n" 1306 "int baz = 0; /* multiline\n" 1307 "comment */", 1308 format("int foobar = 0; /* comment */\n" 1309 "int bar = 0; /* multiline\n" 1310 "comment */\n" 1311 "int baz = 0; /* multiline\n" 1312 "comment */")); 1313 } 1314 1315 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) { 1316 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1317 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 1318 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 1319 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 1320 EXPECT_EQ( 1321 "void ffffffffffff(\n" 1322 " int aaaaaaaa, int bbbbbbbb,\n" 1323 " int cccccccccccc) { /*\n" 1324 " aaaaaaaaaa\n" 1325 " aaaaaaaaaaaaa\n" 1326 " bbbbbbbbbbbbbb\n" 1327 " bbbbbbbbbb\n" 1328 " */\n" 1329 "}", 1330 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 1331 "{ /*\n" 1332 " aaaaaaaaaa aaaaaaaaaaaaa\n" 1333 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 1334 " */\n" 1335 "}", 1336 getLLVMStyleWithColumns(40))); 1337 } 1338 1339 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) { 1340 EXPECT_EQ("void ffffffffff(\n" 1341 " int aaaaa /* test */);", 1342 format("void ffffffffff(int aaaaa /* test */);", 1343 getLLVMStyleWithColumns(35))); 1344 } 1345 1346 TEST_F(FormatTest, SplitsLongCxxComments) { 1347 EXPECT_EQ("// A comment that\n" 1348 "// doesn't fit on\n" 1349 "// one line", 1350 format("// A comment that doesn't fit on one line", 1351 getLLVMStyleWithColumns(20))); 1352 EXPECT_EQ("/// A comment that\n" 1353 "/// doesn't fit on\n" 1354 "/// one line", 1355 format("/// A comment that doesn't fit on one line", 1356 getLLVMStyleWithColumns(20))); 1357 EXPECT_EQ("//! A comment that\n" 1358 "//! doesn't fit on\n" 1359 "//! one line", 1360 format("//! A comment that doesn't fit on one line", 1361 getLLVMStyleWithColumns(20))); 1362 EXPECT_EQ("// a b c d\n" 1363 "// e f g\n" 1364 "// h i j k", 1365 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1366 EXPECT_EQ( 1367 "// a b c d\n" 1368 "// e f g\n" 1369 "// h i j k", 1370 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 1371 EXPECT_EQ("if (true) // A comment that\n" 1372 " // doesn't fit on\n" 1373 " // one line", 1374 format("if (true) // A comment that doesn't fit on one line ", 1375 getLLVMStyleWithColumns(30))); 1376 EXPECT_EQ("// Don't_touch_leading_whitespace", 1377 format("// Don't_touch_leading_whitespace", 1378 getLLVMStyleWithColumns(20))); 1379 EXPECT_EQ("// Add leading\n" 1380 "// whitespace", 1381 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 1382 EXPECT_EQ("/// Add leading\n" 1383 "/// whitespace", 1384 format("///Add leading whitespace", getLLVMStyleWithColumns(20))); 1385 EXPECT_EQ("//! Add leading\n" 1386 "//! whitespace", 1387 format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); 1388 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 1389 EXPECT_EQ("// Even if it makes the line exceed the column\n" 1390 "// limit", 1391 format("//Even if it makes the line exceed the column limit", 1392 getLLVMStyleWithColumns(51))); 1393 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 1394 1395 EXPECT_EQ("// aa bb cc dd", 1396 format("// aa bb cc dd ", 1397 getLLVMStyleWithColumns(15))); 1398 1399 EXPECT_EQ("// A comment before\n" 1400 "// a macro\n" 1401 "// definition\n" 1402 "#define a b", 1403 format("// A comment before a macro definition\n" 1404 "#define a b", 1405 getLLVMStyleWithColumns(20))); 1406 EXPECT_EQ("void ffffff(\n" 1407 " int aaaaaaaaa, // wwww\n" 1408 " int bbbbbbbbbb, // xxxxxxx\n" 1409 " // yyyyyyyyyy\n" 1410 " int c, int d, int e) {}", 1411 format("void ffffff(\n" 1412 " int aaaaaaaaa, // wwww\n" 1413 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 1414 " int c, int d, int e) {}", 1415 getLLVMStyleWithColumns(40))); 1416 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1417 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1418 getLLVMStyleWithColumns(20))); 1419 EXPECT_EQ( 1420 "#define XXX // a b c d\n" 1421 " // e f g h", 1422 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 1423 EXPECT_EQ( 1424 "#define XXX // q w e r\n" 1425 " // t y u i", 1426 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 1427 } 1428 1429 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) { 1430 EXPECT_EQ("// A comment\n" 1431 "// that doesn't\n" 1432 "// fit on one\n" 1433 "// line", 1434 format("// A comment that doesn't fit on one line", 1435 getLLVMStyleWithColumns(20))); 1436 EXPECT_EQ("/// A comment\n" 1437 "/// that doesn't\n" 1438 "/// fit on one\n" 1439 "/// line", 1440 format("/// A comment that doesn't fit on one line", 1441 getLLVMStyleWithColumns(20))); 1442 } 1443 1444 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) { 1445 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1446 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1447 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1448 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1449 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 1450 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 1451 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1452 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1453 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1454 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1455 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1456 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1457 getLLVMStyleWithColumns(50))); 1458 // FIXME: One day we might want to implement adjustment of leading whitespace 1459 // of the consecutive lines in this kind of comment: 1460 EXPECT_EQ("double\n" 1461 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1462 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1463 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1464 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1465 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 1466 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 1467 getLLVMStyleWithColumns(49))); 1468 } 1469 1470 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) { 1471 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 1472 Pragmas.CommentPragmas = "^ IWYU pragma:"; 1473 EXPECT_EQ( 1474 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 1475 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 1476 EXPECT_EQ( 1477 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 1478 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 1479 } 1480 1481 TEST_F(FormatTest, PriorityOfCommentBreaking) { 1482 EXPECT_EQ("if (xxx ==\n" 1483 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1484 " zzz)\n" 1485 " q();", 1486 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 1487 " zzz) q();", 1488 getLLVMStyleWithColumns(40))); 1489 EXPECT_EQ("if (xxxxxxxxxx ==\n" 1490 " yyy && // aaaaaa bbbbbbbb cccc\n" 1491 " zzz)\n" 1492 " q();", 1493 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 1494 " zzz) q();", 1495 getLLVMStyleWithColumns(40))); 1496 EXPECT_EQ("if (xxxxxxxxxx &&\n" 1497 " yyy || // aaaaaa bbbbbbbb cccc\n" 1498 " zzz)\n" 1499 " q();", 1500 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 1501 " zzz) q();", 1502 getLLVMStyleWithColumns(40))); 1503 EXPECT_EQ("fffffffff(\n" 1504 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1505 " zzz);", 1506 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 1507 " zzz);", 1508 getLLVMStyleWithColumns(40))); 1509 } 1510 1511 TEST_F(FormatTest, MultiLineCommentsInDefines) { 1512 EXPECT_EQ("#define A(x) /* \\\n" 1513 " a comment \\\n" 1514 " inside */ \\\n" 1515 " f();", 1516 format("#define A(x) /* \\\n" 1517 " a comment \\\n" 1518 " inside */ \\\n" 1519 " f();", 1520 getLLVMStyleWithColumns(17))); 1521 EXPECT_EQ("#define A( \\\n" 1522 " x) /* \\\n" 1523 " a comment \\\n" 1524 " inside */ \\\n" 1525 " f();", 1526 format("#define A( \\\n" 1527 " x) /* \\\n" 1528 " a comment \\\n" 1529 " inside */ \\\n" 1530 " f();", 1531 getLLVMStyleWithColumns(17))); 1532 } 1533 1534 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { 1535 EXPECT_EQ("namespace {}\n// Test\n#define A", 1536 format("namespace {}\n // Test\n#define A")); 1537 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 1538 format("namespace {}\n /* Test */\n#define A")); 1539 EXPECT_EQ("namespace {}\n/* Test */ #define A", 1540 format("namespace {}\n /* Test */ #define A")); 1541 } 1542 1543 TEST_F(FormatTest, SplitsLongLinesInComments) { 1544 EXPECT_EQ("/* This is a long\n" 1545 " * comment that\n" 1546 " * doesn't\n" 1547 " * fit on one line.\n" 1548 " */", 1549 format("/* " 1550 "This is a long " 1551 "comment that " 1552 "doesn't " 1553 "fit on one line. */", 1554 getLLVMStyleWithColumns(20))); 1555 EXPECT_EQ( 1556 "/* a b c d\n" 1557 " * e f g\n" 1558 " * h i j k\n" 1559 " */", 1560 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1561 EXPECT_EQ( 1562 "/* a b c d\n" 1563 " * e f g\n" 1564 " * h i j k\n" 1565 " */", 1566 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1567 EXPECT_EQ("/*\n" 1568 "This is a long\n" 1569 "comment that doesn't\n" 1570 "fit on one line.\n" 1571 "*/", 1572 format("/*\n" 1573 "This is a long " 1574 "comment that doesn't " 1575 "fit on one line. \n" 1576 "*/", 1577 getLLVMStyleWithColumns(20))); 1578 EXPECT_EQ("/*\n" 1579 " * This is a long\n" 1580 " * comment that\n" 1581 " * doesn't fit on\n" 1582 " * one line.\n" 1583 " */", 1584 format("/* \n" 1585 " * This is a long " 1586 " comment that " 1587 " doesn't fit on " 1588 " one line. \n" 1589 " */", 1590 getLLVMStyleWithColumns(20))); 1591 EXPECT_EQ("/*\n" 1592 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1593 " * so_it_should_be_broken\n" 1594 " * wherever_a_space_occurs\n" 1595 " */", 1596 format("/*\n" 1597 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1598 " so_it_should_be_broken " 1599 " wherever_a_space_occurs \n" 1600 " */", 1601 getLLVMStyleWithColumns(20))); 1602 EXPECT_EQ("/*\n" 1603 " * This_comment_can_not_be_broken_into_lines\n" 1604 " */", 1605 format("/*\n" 1606 " * This_comment_can_not_be_broken_into_lines\n" 1607 " */", 1608 getLLVMStyleWithColumns(20))); 1609 EXPECT_EQ("{\n" 1610 " /*\n" 1611 " This is another\n" 1612 " long comment that\n" 1613 " doesn't fit on one\n" 1614 " line 1234567890\n" 1615 " */\n" 1616 "}", 1617 format("{\n" 1618 "/*\n" 1619 "This is another " 1620 " long comment that " 1621 " doesn't fit on one" 1622 " line 1234567890\n" 1623 "*/\n" 1624 "}", 1625 getLLVMStyleWithColumns(20))); 1626 EXPECT_EQ("{\n" 1627 " /*\n" 1628 " * This i s\n" 1629 " * another comment\n" 1630 " * t hat doesn' t\n" 1631 " * fit on one l i\n" 1632 " * n e\n" 1633 " */\n" 1634 "}", 1635 format("{\n" 1636 "/*\n" 1637 " * This i s" 1638 " another comment" 1639 " t hat doesn' t" 1640 " fit on one l i" 1641 " n e\n" 1642 " */\n" 1643 "}", 1644 getLLVMStyleWithColumns(20))); 1645 EXPECT_EQ("/*\n" 1646 " * This is a long\n" 1647 " * comment that\n" 1648 " * doesn't fit on\n" 1649 " * one line\n" 1650 " */", 1651 format(" /*\n" 1652 " * This is a long comment that doesn't fit on one line\n" 1653 " */", 1654 getLLVMStyleWithColumns(20))); 1655 EXPECT_EQ("{\n" 1656 " if (something) /* This is a\n" 1657 " long\n" 1658 " comment */\n" 1659 " ;\n" 1660 "}", 1661 format("{\n" 1662 " if (something) /* This is a long comment */\n" 1663 " ;\n" 1664 "}", 1665 getLLVMStyleWithColumns(30))); 1666 1667 EXPECT_EQ("/* A comment before\n" 1668 " * a macro\n" 1669 " * definition */\n" 1670 "#define a b", 1671 format("/* A comment before a macro definition */\n" 1672 "#define a b", 1673 getLLVMStyleWithColumns(20))); 1674 1675 EXPECT_EQ("/* some comment\n" 1676 " * a comment\n" 1677 "* that we break\n" 1678 " * another comment\n" 1679 "* we have to break\n" 1680 "* a left comment\n" 1681 " */", 1682 format(" /* some comment\n" 1683 " * a comment that we break\n" 1684 " * another comment we have to break\n" 1685 "* a left comment\n" 1686 " */", 1687 getLLVMStyleWithColumns(20))); 1688 1689 EXPECT_EQ("/**\n" 1690 " * multiline block\n" 1691 " * comment\n" 1692 " *\n" 1693 " */", 1694 format("/**\n" 1695 " * multiline block comment\n" 1696 " *\n" 1697 " */", 1698 getLLVMStyleWithColumns(20))); 1699 1700 EXPECT_EQ("/*\n" 1701 "\n" 1702 "\n" 1703 " */\n", 1704 format(" /* \n" 1705 " \n" 1706 " \n" 1707 " */\n")); 1708 1709 EXPECT_EQ("/* a a */", 1710 format("/* a a */", getLLVMStyleWithColumns(15))); 1711 EXPECT_EQ("/* a a bc */", 1712 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1713 EXPECT_EQ("/* aaa aaa\n" 1714 " * aaaaa */", 1715 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1716 EXPECT_EQ("/* aaa aaa\n" 1717 " * aaaaa */", 1718 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1719 } 1720 1721 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { 1722 EXPECT_EQ("#define X \\\n" 1723 " /* \\\n" 1724 " Test \\\n" 1725 " Macro comment \\\n" 1726 " with a long \\\n" 1727 " line \\\n" 1728 " */ \\\n" 1729 " A + B", 1730 format("#define X \\\n" 1731 " /*\n" 1732 " Test\n" 1733 " Macro comment with a long line\n" 1734 " */ \\\n" 1735 " A + B", 1736 getLLVMStyleWithColumns(20))); 1737 EXPECT_EQ("#define X \\\n" 1738 " /* Macro comment \\\n" 1739 " with a long \\\n" 1740 " line */ \\\n" 1741 " A + B", 1742 format("#define X \\\n" 1743 " /* Macro comment with a long\n" 1744 " line */ \\\n" 1745 " A + B", 1746 getLLVMStyleWithColumns(20))); 1747 EXPECT_EQ("#define X \\\n" 1748 " /* Macro comment \\\n" 1749 " * with a long \\\n" 1750 " * line */ \\\n" 1751 " A + B", 1752 format("#define X \\\n" 1753 " /* Macro comment with a long line */ \\\n" 1754 " A + B", 1755 getLLVMStyleWithColumns(20))); 1756 } 1757 1758 TEST_F(FormatTest, CommentsInStaticInitializers) { 1759 EXPECT_EQ( 1760 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1761 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1762 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1763 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1764 " aaaaaaaaaaaaaaaaaaaa};", 1765 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1766 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1767 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1768 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1769 " aaaaaaaaaaaaaaaaaaaa };")); 1770 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1771 " bbbbbbbbbbb, ccccccccccc};"); 1772 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1773 " // comment for bb....\n" 1774 " bbbbbbbbbbb, ccccccccccc};"); 1775 verifyGoogleFormat( 1776 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1777 " bbbbbbbbbbb, ccccccccccc};"); 1778 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1779 " // comment for bb....\n" 1780 " bbbbbbbbbbb, ccccccccccc};"); 1781 1782 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1783 " {d, e, f}, // Group #2\n" 1784 " {g, h, i}}; // Group #3"); 1785 verifyFormat("S s = {{// Group #1\n" 1786 " a, b, c},\n" 1787 " {// Group #2\n" 1788 " d, e, f},\n" 1789 " {// Group #3\n" 1790 " g, h, i}};"); 1791 1792 EXPECT_EQ("S s = {\n" 1793 " // Some comment\n" 1794 " a,\n" 1795 "\n" 1796 " // Comment after empty line\n" 1797 " b}", 1798 format("S s = {\n" 1799 " // Some comment\n" 1800 " a,\n" 1801 " \n" 1802 " // Comment after empty line\n" 1803 " b\n" 1804 "}")); 1805 EXPECT_EQ("S s = {\n" 1806 " /* Some comment */\n" 1807 " a,\n" 1808 "\n" 1809 " /* Comment after empty line */\n" 1810 " b}", 1811 format("S s = {\n" 1812 " /* Some comment */\n" 1813 " a,\n" 1814 " \n" 1815 " /* Comment after empty line */\n" 1816 " b\n" 1817 "}")); 1818 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1819 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1820 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1821 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1822 } 1823 1824 TEST_F(FormatTest, IgnoresIf0Contents) { 1825 EXPECT_EQ("#if 0\n" 1826 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1827 "#endif\n" 1828 "void f() {}", 1829 format("#if 0\n" 1830 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 1831 "#endif\n" 1832 "void f( ) { }")); 1833 EXPECT_EQ("#if false\n" 1834 "void f( ) { }\n" 1835 "#endif\n" 1836 "void g() {}\n", 1837 format("#if false\n" 1838 "void f( ) { }\n" 1839 "#endif\n" 1840 "void g( ) { }\n")); 1841 EXPECT_EQ("enum E {\n" 1842 " One,\n" 1843 " Two,\n" 1844 "#if 0\n" 1845 "Three,\n" 1846 " Four,\n" 1847 "#endif\n" 1848 " Five\n" 1849 "};", 1850 format("enum E {\n" 1851 " One,Two,\n" 1852 "#if 0\n" 1853 "Three,\n" 1854 " Four,\n" 1855 "#endif\n" 1856 " Five};")); 1857 EXPECT_EQ("enum F {\n" 1858 " One,\n" 1859 "#if 1\n" 1860 " Two,\n" 1861 "#if 0\n" 1862 "Three,\n" 1863 " Four,\n" 1864 "#endif\n" 1865 " Five\n" 1866 "#endif\n" 1867 "};", 1868 format("enum F {\n" 1869 "One,\n" 1870 "#if 1\n" 1871 "Two,\n" 1872 "#if 0\n" 1873 "Three,\n" 1874 " Four,\n" 1875 "#endif\n" 1876 "Five\n" 1877 "#endif\n" 1878 "};")); 1879 EXPECT_EQ("enum G {\n" 1880 " One,\n" 1881 "#if 0\n" 1882 "Two,\n" 1883 "#else\n" 1884 " Three,\n" 1885 "#endif\n" 1886 " Four\n" 1887 "};", 1888 format("enum G {\n" 1889 "One,\n" 1890 "#if 0\n" 1891 "Two,\n" 1892 "#else\n" 1893 "Three,\n" 1894 "#endif\n" 1895 "Four\n" 1896 "};")); 1897 EXPECT_EQ("enum H {\n" 1898 " One,\n" 1899 "#if 0\n" 1900 "#ifdef Q\n" 1901 "Two,\n" 1902 "#else\n" 1903 "Three,\n" 1904 "#endif\n" 1905 "#endif\n" 1906 " Four\n" 1907 "};", 1908 format("enum H {\n" 1909 "One,\n" 1910 "#if 0\n" 1911 "#ifdef Q\n" 1912 "Two,\n" 1913 "#else\n" 1914 "Three,\n" 1915 "#endif\n" 1916 "#endif\n" 1917 "Four\n" 1918 "};")); 1919 EXPECT_EQ("enum I {\n" 1920 " One,\n" 1921 "#if /* test */ 0 || 1\n" 1922 "Two,\n" 1923 "Three,\n" 1924 "#endif\n" 1925 " Four\n" 1926 "};", 1927 format("enum I {\n" 1928 "One,\n" 1929 "#if /* test */ 0 || 1\n" 1930 "Two,\n" 1931 "Three,\n" 1932 "#endif\n" 1933 "Four\n" 1934 "};")); 1935 EXPECT_EQ("enum J {\n" 1936 " One,\n" 1937 "#if 0\n" 1938 "#if 0\n" 1939 "Two,\n" 1940 "#else\n" 1941 "Three,\n" 1942 "#endif\n" 1943 "Four,\n" 1944 "#endif\n" 1945 " Five\n" 1946 "};", 1947 format("enum J {\n" 1948 "One,\n" 1949 "#if 0\n" 1950 "#if 0\n" 1951 "Two,\n" 1952 "#else\n" 1953 "Three,\n" 1954 "#endif\n" 1955 "Four,\n" 1956 "#endif\n" 1957 "Five\n" 1958 "};")); 1959 } 1960 1961 //===----------------------------------------------------------------------===// 1962 // Tests for classes, namespaces, etc. 1963 //===----------------------------------------------------------------------===// 1964 1965 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 1966 verifyFormat("class A {};"); 1967 } 1968 1969 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 1970 verifyFormat("class A {\n" 1971 "public:\n" 1972 "public: // comment\n" 1973 "protected:\n" 1974 "private:\n" 1975 " void f() {}\n" 1976 "};"); 1977 verifyGoogleFormat("class A {\n" 1978 " public:\n" 1979 " protected:\n" 1980 " private:\n" 1981 " void f() {}\n" 1982 "};"); 1983 verifyFormat("class A {\n" 1984 "public slots:\n" 1985 " void f() {}\n" 1986 "public Q_SLOTS:\n" 1987 " void f() {}\n" 1988 "signals:\n" 1989 " void g();\n" 1990 "};"); 1991 1992 // Don't interpret 'signals' the wrong way. 1993 verifyFormat("signals.set();"); 1994 verifyFormat("for (Signals signals : f()) {\n}"); 1995 verifyFormat("{\n" 1996 " signals.set(); // This needs indentation.\n" 1997 "}"); 1998 } 1999 2000 TEST_F(FormatTest, SeparatesLogicalBlocks) { 2001 EXPECT_EQ("class A {\n" 2002 "public:\n" 2003 " void f();\n" 2004 "\n" 2005 "private:\n" 2006 " void g() {}\n" 2007 " // test\n" 2008 "protected:\n" 2009 " int h;\n" 2010 "};", 2011 format("class A {\n" 2012 "public:\n" 2013 "void f();\n" 2014 "private:\n" 2015 "void g() {}\n" 2016 "// test\n" 2017 "protected:\n" 2018 "int h;\n" 2019 "};")); 2020 EXPECT_EQ("class A {\n" 2021 "protected:\n" 2022 "public:\n" 2023 " void f();\n" 2024 "};", 2025 format("class A {\n" 2026 "protected:\n" 2027 "\n" 2028 "public:\n" 2029 "\n" 2030 " void f();\n" 2031 "};")); 2032 2033 // Even ensure proper spacing inside macros. 2034 EXPECT_EQ("#define B \\\n" 2035 " class A { \\\n" 2036 " protected: \\\n" 2037 " public: \\\n" 2038 " void f(); \\\n" 2039 " };", 2040 format("#define B \\\n" 2041 " class A { \\\n" 2042 " protected: \\\n" 2043 " \\\n" 2044 " public: \\\n" 2045 " \\\n" 2046 " void f(); \\\n" 2047 " };", 2048 getGoogleStyle())); 2049 // But don't remove empty lines after macros ending in access specifiers. 2050 EXPECT_EQ("#define A private:\n" 2051 "\n" 2052 "int i;", 2053 format("#define A private:\n" 2054 "\n" 2055 "int i;")); 2056 } 2057 2058 TEST_F(FormatTest, FormatsClasses) { 2059 verifyFormat("class A : public B {};"); 2060 verifyFormat("class A : public ::B {};"); 2061 2062 verifyFormat( 2063 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2064 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2065 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 2066 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2067 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2068 verifyFormat( 2069 "class A : public B, public C, public D, public E, public F {};"); 2070 verifyFormat("class AAAAAAAAAAAA : public B,\n" 2071 " public C,\n" 2072 " public D,\n" 2073 " public E,\n" 2074 " public F,\n" 2075 " public G {};"); 2076 2077 verifyFormat("class\n" 2078 " ReallyReallyLongClassName {\n" 2079 " int i;\n" 2080 "};", 2081 getLLVMStyleWithColumns(32)); 2082 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2083 " aaaaaaaaaaaaaaaa> {};"); 2084 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 2085 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 2086 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 2087 verifyFormat("template <class R, class C>\n" 2088 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 2089 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 2090 verifyFormat("class ::A::B {};"); 2091 } 2092 2093 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 2094 verifyFormat("class A {\n} a, b;"); 2095 verifyFormat("struct A {\n} a, b;"); 2096 verifyFormat("union A {\n} a;"); 2097 } 2098 2099 TEST_F(FormatTest, FormatsEnum) { 2100 verifyFormat("enum {\n" 2101 " Zero,\n" 2102 " One = 1,\n" 2103 " Two = One + 1,\n" 2104 " Three = (One + Two),\n" 2105 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2106 " Five = (One, Two, Three, Four, 5)\n" 2107 "};"); 2108 verifyGoogleFormat("enum {\n" 2109 " Zero,\n" 2110 " One = 1,\n" 2111 " Two = One + 1,\n" 2112 " Three = (One + Two),\n" 2113 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2114 " Five = (One, Two, Three, Four, 5)\n" 2115 "};"); 2116 verifyFormat("enum Enum {};"); 2117 verifyFormat("enum {};"); 2118 verifyFormat("enum X E {} d;"); 2119 verifyFormat("enum __attribute__((...)) E {} d;"); 2120 verifyFormat("enum __declspec__((...)) E {} d;"); 2121 verifyFormat("enum X f() {\n a();\n return 42;\n}"); 2122 verifyFormat("enum {\n" 2123 " Bar = Foo<int, int>::value\n" 2124 "};", 2125 getLLVMStyleWithColumns(30)); 2126 2127 verifyFormat("enum ShortEnum { A, B, C };"); 2128 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 2129 2130 EXPECT_EQ("enum KeepEmptyLines {\n" 2131 " ONE,\n" 2132 "\n" 2133 " TWO,\n" 2134 "\n" 2135 " THREE\n" 2136 "}", 2137 format("enum KeepEmptyLines {\n" 2138 " ONE,\n" 2139 "\n" 2140 " TWO,\n" 2141 "\n" 2142 "\n" 2143 " THREE\n" 2144 "}")); 2145 verifyFormat("enum E { // comment\n" 2146 " ONE,\n" 2147 " TWO\n" 2148 "};\n" 2149 "int i;"); 2150 } 2151 2152 TEST_F(FormatTest, FormatsEnumsWithErrors) { 2153 verifyFormat("enum Type {\n" 2154 " One = 0; // These semicolons should be commas.\n" 2155 " Two = 1;\n" 2156 "};"); 2157 verifyFormat("namespace n {\n" 2158 "enum Type {\n" 2159 " One,\n" 2160 " Two, // missing };\n" 2161 " int i;\n" 2162 "}\n" 2163 "void g() {}"); 2164 } 2165 2166 TEST_F(FormatTest, FormatsEnumStruct) { 2167 verifyFormat("enum struct {\n" 2168 " Zero,\n" 2169 " One = 1,\n" 2170 " Two = One + 1,\n" 2171 " Three = (One + Two),\n" 2172 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2173 " Five = (One, Two, Three, Four, 5)\n" 2174 "};"); 2175 verifyFormat("enum struct Enum {};"); 2176 verifyFormat("enum struct {};"); 2177 verifyFormat("enum struct X E {} d;"); 2178 verifyFormat("enum struct __attribute__((...)) E {} d;"); 2179 verifyFormat("enum struct __declspec__((...)) E {} d;"); 2180 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 2181 } 2182 2183 TEST_F(FormatTest, FormatsEnumClass) { 2184 verifyFormat("enum class {\n" 2185 " Zero,\n" 2186 " One = 1,\n" 2187 " Two = One + 1,\n" 2188 " Three = (One + Two),\n" 2189 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2190 " Five = (One, Two, Three, Four, 5)\n" 2191 "};"); 2192 verifyFormat("enum class Enum {};"); 2193 verifyFormat("enum class {};"); 2194 verifyFormat("enum class X E {} d;"); 2195 verifyFormat("enum class __attribute__((...)) E {} d;"); 2196 verifyFormat("enum class __declspec__((...)) E {} d;"); 2197 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 2198 } 2199 2200 TEST_F(FormatTest, FormatsEnumTypes) { 2201 verifyFormat("enum X : int {\n" 2202 " A, // Force multiple lines.\n" 2203 " B\n" 2204 "};"); 2205 verifyFormat("enum X : int { A, B };"); 2206 verifyFormat("enum X : std::uint32_t { A, B };"); 2207 } 2208 2209 TEST_F(FormatTest, FormatsNSEnums) { 2210 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2211 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 2212 " // Information about someDecentlyLongValue.\n" 2213 " someDecentlyLongValue,\n" 2214 " // Information about anotherDecentlyLongValue.\n" 2215 " anotherDecentlyLongValue,\n" 2216 " // Information about aThirdDecentlyLongValue.\n" 2217 " aThirdDecentlyLongValue\n" 2218 "};"); 2219 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 2220 " a = 1,\n" 2221 " b = 2,\n" 2222 " c = 3,\n" 2223 "};"); 2224 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 2225 " a = 1,\n" 2226 " b = 2,\n" 2227 " c = 3,\n" 2228 "};"); 2229 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 2230 " a = 1,\n" 2231 " b = 2,\n" 2232 " c = 3,\n" 2233 "};"); 2234 } 2235 2236 TEST_F(FormatTest, FormatsBitfields) { 2237 verifyFormat("struct Bitfields {\n" 2238 " unsigned sClass : 8;\n" 2239 " unsigned ValueKind : 2;\n" 2240 "};"); 2241 verifyFormat("struct A {\n" 2242 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 2243 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 2244 "};"); 2245 verifyFormat("struct MyStruct {\n" 2246 " uchar data;\n" 2247 " uchar : 8;\n" 2248 " uchar : 8;\n" 2249 " uchar other;\n" 2250 "};"); 2251 } 2252 2253 TEST_F(FormatTest, FormatsNamespaces) { 2254 verifyFormat("namespace some_namespace {\n" 2255 "class A {};\n" 2256 "void f() { f(); }\n" 2257 "}"); 2258 verifyFormat("namespace {\n" 2259 "class A {};\n" 2260 "void f() { f(); }\n" 2261 "}"); 2262 verifyFormat("inline namespace X {\n" 2263 "class A {};\n" 2264 "void f() { f(); }\n" 2265 "}"); 2266 verifyFormat("using namespace some_namespace;\n" 2267 "class A {};\n" 2268 "void f() { f(); }"); 2269 2270 // This code is more common than we thought; if we 2271 // layout this correctly the semicolon will go into 2272 // its own line, which is undesirable. 2273 verifyFormat("namespace {};"); 2274 verifyFormat("namespace {\n" 2275 "class A {};\n" 2276 "};"); 2277 2278 verifyFormat("namespace {\n" 2279 "int SomeVariable = 0; // comment\n" 2280 "} // namespace"); 2281 EXPECT_EQ("#ifndef HEADER_GUARD\n" 2282 "#define HEADER_GUARD\n" 2283 "namespace my_namespace {\n" 2284 "int i;\n" 2285 "} // my_namespace\n" 2286 "#endif // HEADER_GUARD", 2287 format("#ifndef HEADER_GUARD\n" 2288 " #define HEADER_GUARD\n" 2289 " namespace my_namespace {\n" 2290 "int i;\n" 2291 "} // my_namespace\n" 2292 "#endif // HEADER_GUARD")); 2293 2294 FormatStyle Style = getLLVMStyle(); 2295 Style.NamespaceIndentation = FormatStyle::NI_All; 2296 EXPECT_EQ("namespace out {\n" 2297 " int i;\n" 2298 " namespace in {\n" 2299 " int i;\n" 2300 " } // namespace\n" 2301 "} // namespace", 2302 format("namespace out {\n" 2303 "int i;\n" 2304 "namespace in {\n" 2305 "int i;\n" 2306 "} // namespace\n" 2307 "} // namespace", 2308 Style)); 2309 2310 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2311 EXPECT_EQ("namespace out {\n" 2312 "int i;\n" 2313 "namespace in {\n" 2314 " int i;\n" 2315 "} // namespace\n" 2316 "} // namespace", 2317 format("namespace out {\n" 2318 "int i;\n" 2319 "namespace in {\n" 2320 "int i;\n" 2321 "} // namespace\n" 2322 "} // namespace", 2323 Style)); 2324 } 2325 2326 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } 2327 2328 TEST_F(FormatTest, FormatsInlineASM) { 2329 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 2330 verifyFormat("asm(\"nop\" ::: \"memory\");"); 2331 verifyFormat( 2332 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 2333 " \"cpuid\\n\\t\"\n" 2334 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 2335 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 2336 " : \"a\"(value));"); 2337 EXPECT_EQ( 2338 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2339 " __asm {\n" 2340 " mov edx,[that] // vtable in edx\n" 2341 " mov eax,methodIndex\n" 2342 " call [edx][eax*4] // stdcall\n" 2343 " }\n" 2344 "}", 2345 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 2346 " __asm {\n" 2347 " mov edx,[that] // vtable in edx\n" 2348 " mov eax,methodIndex\n" 2349 " call [edx][eax*4] // stdcall\n" 2350 " }\n" 2351 "}")); 2352 EXPECT_EQ("_asm {\n" 2353 " xor eax, eax;\n" 2354 " cpuid;\n" 2355 "}", 2356 format("_asm {\n" 2357 " xor eax, eax;\n" 2358 " cpuid;\n" 2359 "}")); 2360 verifyFormat("void function() {\n" 2361 " // comment\n" 2362 " asm(\"\");\n" 2363 "}"); 2364 EXPECT_EQ("__asm {\n" 2365 "}\n" 2366 "int i;", 2367 format("__asm {\n" 2368 "}\n" 2369 "int i;")); 2370 } 2371 2372 TEST_F(FormatTest, FormatTryCatch) { 2373 verifyFormat("try {\n" 2374 " throw a * b;\n" 2375 "} catch (int a) {\n" 2376 " // Do nothing.\n" 2377 "} catch (...) {\n" 2378 " exit(42);\n" 2379 "}"); 2380 2381 // Function-level try statements. 2382 verifyFormat("int f() try { return 4; } catch (...) {\n" 2383 " return 5;\n" 2384 "}"); 2385 verifyFormat("class A {\n" 2386 " int a;\n" 2387 " A() try : a(0) {\n" 2388 " } catch (...) {\n" 2389 " throw;\n" 2390 " }\n" 2391 "};\n"); 2392 2393 // Incomplete try-catch blocks. 2394 verifyIncompleteFormat("try {} catch ("); 2395 } 2396 2397 TEST_F(FormatTest, FormatSEHTryCatch) { 2398 verifyFormat("__try {\n" 2399 " int a = b * c;\n" 2400 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 2401 " // Do nothing.\n" 2402 "}"); 2403 2404 verifyFormat("__try {\n" 2405 " int a = b * c;\n" 2406 "} __finally {\n" 2407 " // Do nothing.\n" 2408 "}"); 2409 2410 verifyFormat("DEBUG({\n" 2411 " __try {\n" 2412 " } __finally {\n" 2413 " }\n" 2414 "});\n"); 2415 } 2416 2417 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 2418 verifyFormat("try {\n" 2419 " f();\n" 2420 "} catch {\n" 2421 " g();\n" 2422 "}"); 2423 verifyFormat("try {\n" 2424 " f();\n" 2425 "} catch (A a) MACRO(x) {\n" 2426 " g();\n" 2427 "} catch (B b) MACRO(x) {\n" 2428 " g();\n" 2429 "}"); 2430 } 2431 2432 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 2433 FormatStyle Style = getLLVMStyle(); 2434 Style.BreakBeforeBraces = FormatStyle::BS_Attach; 2435 verifyFormat("try {\n" 2436 " // something\n" 2437 "} catch (...) {\n" 2438 " // something\n" 2439 "}", 2440 Style); 2441 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 2442 verifyFormat("try {\n" 2443 " // something\n" 2444 "}\n" 2445 "catch (...) {\n" 2446 " // something\n" 2447 "}", 2448 Style); 2449 verifyFormat("__try {\n" 2450 " // something\n" 2451 "}\n" 2452 "__finally {\n" 2453 " // something\n" 2454 "}", 2455 Style); 2456 verifyFormat("@try {\n" 2457 " // something\n" 2458 "}\n" 2459 "@finally {\n" 2460 " // something\n" 2461 "}", 2462 Style); 2463 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 2464 verifyFormat("try\n" 2465 "{\n" 2466 " // something\n" 2467 "}\n" 2468 "catch (...)\n" 2469 "{\n" 2470 " // something\n" 2471 "}", 2472 Style); 2473 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 2474 verifyFormat("try\n" 2475 " {\n" 2476 " // something\n" 2477 " }\n" 2478 "catch (...)\n" 2479 " {\n" 2480 " // something\n" 2481 " }", 2482 Style); 2483 } 2484 2485 TEST_F(FormatTest, FormatObjCTryCatch) { 2486 verifyFormat("@try {\n" 2487 " f();\n" 2488 "} @catch (NSException e) {\n" 2489 " @throw;\n" 2490 "} @finally {\n" 2491 " exit(42);\n" 2492 "}"); 2493 verifyFormat("DEBUG({\n" 2494 " @try {\n" 2495 " } @finally {\n" 2496 " }\n" 2497 "});\n"); 2498 } 2499 2500 TEST_F(FormatTest, StaticInitializers) { 2501 verifyFormat("static SomeClass SC = {1, 'a'};"); 2502 2503 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 2504 " 100000000, " 2505 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 2506 2507 // Here, everything other than the "}" would fit on a line. 2508 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 2509 " 10000000000000000000000000};"); 2510 EXPECT_EQ("S s = {a,\n" 2511 "\n" 2512 " b};", 2513 format("S s = {\n" 2514 " a,\n" 2515 "\n" 2516 " b\n" 2517 "};")); 2518 2519 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 2520 // line. However, the formatting looks a bit off and this probably doesn't 2521 // happen often in practice. 2522 verifyFormat("static int Variable[1] = {\n" 2523 " {1000000000000000000000000000000000000}};", 2524 getLLVMStyleWithColumns(40)); 2525 } 2526 2527 TEST_F(FormatTest, DesignatedInitializers) { 2528 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 2529 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 2530 " .bbbbbbbbbb = 2,\n" 2531 " .cccccccccc = 3,\n" 2532 " .dddddddddd = 4,\n" 2533 " .eeeeeeeeee = 5};"); 2534 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 2535 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 2536 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 2537 " .ccccccccccccccccccccccccccc = 3,\n" 2538 " .ddddddddddddddddddddddddddd = 4,\n" 2539 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 2540 2541 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 2542 } 2543 2544 TEST_F(FormatTest, NestedStaticInitializers) { 2545 verifyFormat("static A x = {{{}}};\n"); 2546 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 2547 " {init1, init2, init3, init4}}};", 2548 getLLVMStyleWithColumns(50)); 2549 2550 verifyFormat("somes Status::global_reps[3] = {\n" 2551 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2552 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2553 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 2554 getLLVMStyleWithColumns(60)); 2555 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 2556 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 2557 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 2558 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 2559 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 2560 " {rect.fRight - rect.fLeft, rect.fBottom - " 2561 "rect.fTop}};"); 2562 2563 verifyFormat( 2564 "SomeArrayOfSomeType a = {\n" 2565 " {{1, 2, 3},\n" 2566 " {1, 2, 3},\n" 2567 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 2568 " 333333333333333333333333333333},\n" 2569 " {1, 2, 3},\n" 2570 " {1, 2, 3}}};"); 2571 verifyFormat( 2572 "SomeArrayOfSomeType a = {\n" 2573 " {{1, 2, 3}},\n" 2574 " {{1, 2, 3}},\n" 2575 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 2576 " 333333333333333333333333333333}},\n" 2577 " {{1, 2, 3}},\n" 2578 " {{1, 2, 3}}};"); 2579 2580 verifyFormat("struct {\n" 2581 " unsigned bit;\n" 2582 " const char *const name;\n" 2583 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 2584 " {kOsWin, \"Windows\"},\n" 2585 " {kOsLinux, \"Linux\"},\n" 2586 " {kOsCrOS, \"Chrome OS\"}};"); 2587 verifyFormat("struct {\n" 2588 " unsigned bit;\n" 2589 " const char *const name;\n" 2590 "} kBitsToOs[] = {\n" 2591 " {kOsMac, \"Mac\"},\n" 2592 " {kOsWin, \"Windows\"},\n" 2593 " {kOsLinux, \"Linux\"},\n" 2594 " {kOsCrOS, \"Chrome OS\"},\n" 2595 "};"); 2596 } 2597 2598 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 2599 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 2600 " \\\n" 2601 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 2602 } 2603 2604 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 2605 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 2606 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 2607 2608 // Do break defaulted and deleted functions. 2609 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2610 " default;", 2611 getLLVMStyleWithColumns(40)); 2612 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 2613 " delete;", 2614 getLLVMStyleWithColumns(40)); 2615 } 2616 2617 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 2618 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 2619 getLLVMStyleWithColumns(40)); 2620 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2621 getLLVMStyleWithColumns(40)); 2622 EXPECT_EQ("#define Q \\\n" 2623 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 2624 " \"aaaaaaaa.cpp\"", 2625 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 2626 getLLVMStyleWithColumns(40))); 2627 } 2628 2629 TEST_F(FormatTest, UnderstandsLinePPDirective) { 2630 EXPECT_EQ("# 123 \"A string literal\"", 2631 format(" # 123 \"A string literal\"")); 2632 } 2633 2634 TEST_F(FormatTest, LayoutUnknownPPDirective) { 2635 EXPECT_EQ("#;", format("#;")); 2636 verifyFormat("#\n;\n;\n;"); 2637 } 2638 2639 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 2640 EXPECT_EQ("#line 42 \"test\"\n", 2641 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 2642 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 2643 getLLVMStyleWithColumns(12))); 2644 } 2645 2646 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 2647 EXPECT_EQ("#line 42 \"test\"", 2648 format("# \\\n line \\\n 42 \\\n \"test\"")); 2649 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 2650 } 2651 2652 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 2653 verifyFormat("#define A \\x20"); 2654 verifyFormat("#define A \\ x20"); 2655 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 2656 verifyFormat("#define A ''"); 2657 verifyFormat("#define A ''qqq"); 2658 verifyFormat("#define A `qqq"); 2659 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 2660 EXPECT_EQ("const char *c = STRINGIFY(\n" 2661 "\\na : b);", 2662 format("const char * c = STRINGIFY(\n" 2663 "\\na : b);")); 2664 2665 verifyFormat("a\r\\"); 2666 verifyFormat("a\v\\"); 2667 verifyFormat("a\f\\"); 2668 } 2669 2670 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 2671 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 2672 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 2673 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 2674 // FIXME: We never break before the macro name. 2675 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 2676 2677 verifyFormat("#define A A\n#define A A"); 2678 verifyFormat("#define A(X) A\n#define A A"); 2679 2680 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 2681 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 2682 } 2683 2684 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 2685 EXPECT_EQ("// somecomment\n" 2686 "#include \"a.h\"\n" 2687 "#define A( \\\n" 2688 " A, B)\n" 2689 "#include \"b.h\"\n" 2690 "// somecomment\n", 2691 format(" // somecomment\n" 2692 " #include \"a.h\"\n" 2693 "#define A(A,\\\n" 2694 " B)\n" 2695 " #include \"b.h\"\n" 2696 " // somecomment\n", 2697 getLLVMStyleWithColumns(13))); 2698 } 2699 2700 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 2701 2702 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 2703 EXPECT_EQ("#define A \\\n" 2704 " c; \\\n" 2705 " e;\n" 2706 "f;", 2707 format("#define A c; e;\n" 2708 "f;", 2709 getLLVMStyleWithColumns(14))); 2710 } 2711 2712 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 2713 2714 TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) { 2715 EXPECT_EQ("int i;\n" 2716 "#define A \\\n" 2717 " int i; \\\n" 2718 " int j\n" 2719 "int k;", 2720 format("int i;\n" 2721 "#define A \\\n" 2722 " int i ; \\\n" 2723 " int j\n" 2724 "int k;", 2725 8, 0, getGoogleStyle())); // 8: position of "#define". 2726 EXPECT_EQ("int i;\n" 2727 "#define A \\\n" 2728 " int i; \\\n" 2729 " int j\n" 2730 "int k;", 2731 format("int i;\n" 2732 "#define A \\\n" 2733 " int i ; \\\n" 2734 " int j\n" 2735 "int k;", 2736 45, 0, getGoogleStyle())); // 45: position of "j". 2737 } 2738 2739 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 2740 EXPECT_EQ("int x,\n" 2741 "#define A\n" 2742 " y;", 2743 format("int x,\n#define A\ny;")); 2744 } 2745 2746 TEST_F(FormatTest, HashInMacroDefinition) { 2747 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 2748 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 2749 verifyFormat("#define A \\\n" 2750 " { \\\n" 2751 " f(#c); \\\n" 2752 " }", 2753 getLLVMStyleWithColumns(11)); 2754 2755 verifyFormat("#define A(X) \\\n" 2756 " void function##X()", 2757 getLLVMStyleWithColumns(22)); 2758 2759 verifyFormat("#define A(a, b, c) \\\n" 2760 " void a##b##c()", 2761 getLLVMStyleWithColumns(22)); 2762 2763 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 2764 } 2765 2766 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 2767 EXPECT_EQ("#define A (x)", format("#define A (x)")); 2768 EXPECT_EQ("#define A(x)", format("#define A(x)")); 2769 } 2770 2771 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 2772 EXPECT_EQ("#define A b;", format("#define A \\\n" 2773 " \\\n" 2774 " b;", 2775 getLLVMStyleWithColumns(25))); 2776 EXPECT_EQ("#define A \\\n" 2777 " \\\n" 2778 " a; \\\n" 2779 " b;", 2780 format("#define A \\\n" 2781 " \\\n" 2782 " a; \\\n" 2783 " b;", 2784 getLLVMStyleWithColumns(11))); 2785 EXPECT_EQ("#define A \\\n" 2786 " a; \\\n" 2787 " \\\n" 2788 " b;", 2789 format("#define A \\\n" 2790 " a; \\\n" 2791 " \\\n" 2792 " b;", 2793 getLLVMStyleWithColumns(11))); 2794 } 2795 2796 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 2797 verifyIncompleteFormat("#define A :"); 2798 verifyFormat("#define SOMECASES \\\n" 2799 " case 1: \\\n" 2800 " case 2\n", 2801 getLLVMStyleWithColumns(20)); 2802 verifyFormat("#define A template <typename T>"); 2803 verifyIncompleteFormat("#define STR(x) #x\n" 2804 "f(STR(this_is_a_string_literal{));"); 2805 verifyFormat("#pragma omp threadprivate( \\\n" 2806 " y)), // expected-warning", 2807 getLLVMStyleWithColumns(28)); 2808 verifyFormat("#d, = };"); 2809 verifyFormat("#if \"a"); 2810 verifyIncompleteFormat("({\n" 2811 "#define b \\\n" 2812 " } \\\n" 2813 " a\n" 2814 "a", getLLVMStyleWithColumns(15)); 2815 verifyFormat("#define A \\\n" 2816 " { \\\n" 2817 " {\n" 2818 "#define B \\\n" 2819 " } \\\n" 2820 " }", 2821 getLLVMStyleWithColumns(15)); 2822 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 2823 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 2824 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 2825 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 2826 } 2827 2828 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 2829 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 2830 EXPECT_EQ("class A : public QObject {\n" 2831 " Q_OBJECT\n" 2832 "\n" 2833 " A() {}\n" 2834 "};", 2835 format("class A : public QObject {\n" 2836 " Q_OBJECT\n" 2837 "\n" 2838 " A() {\n}\n" 2839 "} ;")); 2840 EXPECT_EQ("MACRO\n" 2841 "/*static*/ int i;", 2842 format("MACRO\n" 2843 " /*static*/ int i;")); 2844 EXPECT_EQ("SOME_MACRO\n" 2845 "namespace {\n" 2846 "void f();\n" 2847 "}", 2848 format("SOME_MACRO\n" 2849 " namespace {\n" 2850 "void f( );\n" 2851 "}")); 2852 // Only if the identifier contains at least 5 characters. 2853 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 2854 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 2855 // Only if everything is upper case. 2856 EXPECT_EQ("class A : public QObject {\n" 2857 " Q_Object A() {}\n" 2858 "};", 2859 format("class A : public QObject {\n" 2860 " Q_Object\n" 2861 " A() {\n}\n" 2862 "} ;")); 2863 2864 // Only if the next line can actually start an unwrapped line. 2865 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 2866 format("SOME_WEIRD_LOG_MACRO\n" 2867 "<< SomeThing;")); 2868 2869 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 2870 "(n, buffers))\n", 2871 getChromiumStyle(FormatStyle::LK_Cpp)); 2872 } 2873 2874 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 2875 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2876 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2877 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2878 "class X {};\n" 2879 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2880 "int *createScopDetectionPass() { return 0; }", 2881 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 2882 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 2883 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 2884 " class X {};\n" 2885 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 2886 " int *createScopDetectionPass() { return 0; }")); 2887 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 2888 // braces, so that inner block is indented one level more. 2889 EXPECT_EQ("int q() {\n" 2890 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2891 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2892 " IPC_END_MESSAGE_MAP()\n" 2893 "}", 2894 format("int q() {\n" 2895 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 2896 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 2897 " IPC_END_MESSAGE_MAP()\n" 2898 "}")); 2899 2900 // Same inside macros. 2901 EXPECT_EQ("#define LIST(L) \\\n" 2902 " L(A) \\\n" 2903 " L(B) \\\n" 2904 " L(C)", 2905 format("#define LIST(L) \\\n" 2906 " L(A) \\\n" 2907 " L(B) \\\n" 2908 " L(C)", 2909 getGoogleStyle())); 2910 2911 // These must not be recognized as macros. 2912 EXPECT_EQ("int q() {\n" 2913 " f(x);\n" 2914 " f(x) {}\n" 2915 " f(x)->g();\n" 2916 " f(x)->*g();\n" 2917 " f(x).g();\n" 2918 " f(x) = x;\n" 2919 " f(x) += x;\n" 2920 " f(x) -= x;\n" 2921 " f(x) *= x;\n" 2922 " f(x) /= x;\n" 2923 " f(x) %= x;\n" 2924 " f(x) &= x;\n" 2925 " f(x) |= x;\n" 2926 " f(x) ^= x;\n" 2927 " f(x) >>= x;\n" 2928 " f(x) <<= x;\n" 2929 " f(x)[y].z();\n" 2930 " LOG(INFO) << x;\n" 2931 " ifstream(x) >> x;\n" 2932 "}\n", 2933 format("int q() {\n" 2934 " f(x)\n;\n" 2935 " f(x)\n {}\n" 2936 " f(x)\n->g();\n" 2937 " f(x)\n->*g();\n" 2938 " f(x)\n.g();\n" 2939 " f(x)\n = x;\n" 2940 " f(x)\n += x;\n" 2941 " f(x)\n -= x;\n" 2942 " f(x)\n *= x;\n" 2943 " f(x)\n /= x;\n" 2944 " f(x)\n %= x;\n" 2945 " f(x)\n &= x;\n" 2946 " f(x)\n |= x;\n" 2947 " f(x)\n ^= x;\n" 2948 " f(x)\n >>= x;\n" 2949 " f(x)\n <<= x;\n" 2950 " f(x)\n[y].z();\n" 2951 " LOG(INFO)\n << x;\n" 2952 " ifstream(x)\n >> x;\n" 2953 "}\n")); 2954 EXPECT_EQ("int q() {\n" 2955 " F(x)\n" 2956 " if (1) {\n" 2957 " }\n" 2958 " F(x)\n" 2959 " while (1) {\n" 2960 " }\n" 2961 " F(x)\n" 2962 " G(x);\n" 2963 " F(x)\n" 2964 " try {\n" 2965 " Q();\n" 2966 " } catch (...) {\n" 2967 " }\n" 2968 "}\n", 2969 format("int q() {\n" 2970 "F(x)\n" 2971 "if (1) {}\n" 2972 "F(x)\n" 2973 "while (1) {}\n" 2974 "F(x)\n" 2975 "G(x);\n" 2976 "F(x)\n" 2977 "try { Q(); } catch (...) {}\n" 2978 "}\n")); 2979 EXPECT_EQ("class A {\n" 2980 " A() : t(0) {}\n" 2981 " A(int i) noexcept() : {}\n" 2982 " A(X x)\n" // FIXME: function-level try blocks are broken. 2983 " try : t(0) {\n" 2984 " } catch (...) {\n" 2985 " }\n" 2986 "};", 2987 format("class A {\n" 2988 " A()\n : t(0) {}\n" 2989 " A(int i)\n noexcept() : {}\n" 2990 " A(X x)\n" 2991 " try : t(0) {} catch (...) {}\n" 2992 "};")); 2993 EXPECT_EQ("class SomeClass {\n" 2994 "public:\n" 2995 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 2996 "};", 2997 format("class SomeClass {\n" 2998 "public:\n" 2999 " SomeClass()\n" 3000 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3001 "};")); 3002 EXPECT_EQ("class SomeClass {\n" 3003 "public:\n" 3004 " SomeClass()\n" 3005 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3006 "};", 3007 format("class SomeClass {\n" 3008 "public:\n" 3009 " SomeClass()\n" 3010 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3011 "};", 3012 getLLVMStyleWithColumns(40))); 3013 } 3014 3015 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 3016 verifyFormat("#define A \\\n" 3017 " f({ \\\n" 3018 " g(); \\\n" 3019 " });", 3020 getLLVMStyleWithColumns(11)); 3021 } 3022 3023 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { 3024 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); 3025 } 3026 3027 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 3028 verifyFormat("{\n { a #c; }\n}"); 3029 } 3030 3031 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 3032 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 3033 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 3034 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 3035 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 3036 } 3037 3038 TEST_F(FormatTest, EscapedNewlines) { 3039 EXPECT_EQ( 3040 "#define A \\\n int i; \\\n int j;", 3041 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); 3042 EXPECT_EQ( 3043 "#define A\n\nint i;", format("#define A \\\n\n int i;")); 3044 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 3045 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/")); 3046 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 3047 } 3048 3049 TEST_F(FormatTest, DontCrashOnBlockComments) { 3050 EXPECT_EQ( 3051 "int xxxxxxxxx; /* " 3052 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 3053 "zzzzzz\n" 3054 "0*/", 3055 format("int xxxxxxxxx; /* " 3056 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 3057 "0*/")); 3058 } 3059 3060 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 3061 verifyFormat("#define A \\\n" 3062 " int v( \\\n" 3063 " a); \\\n" 3064 " int i;", 3065 getLLVMStyleWithColumns(11)); 3066 } 3067 3068 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 3069 EXPECT_EQ( 3070 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3071 " \\\n" 3072 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3073 "\n" 3074 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3075 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 3076 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3077 "\\\n" 3078 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 3079 " \n" 3080 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 3081 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 3082 } 3083 3084 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 3085 EXPECT_EQ("int\n" 3086 "#define A\n" 3087 " a;", 3088 format("int\n#define A\na;")); 3089 verifyFormat("functionCallTo(\n" 3090 " someOtherFunction(\n" 3091 " withSomeParameters, whichInSequence,\n" 3092 " areLongerThanALine(andAnotherCall,\n" 3093 "#define A B\n" 3094 " withMoreParamters,\n" 3095 " whichStronglyInfluenceTheLayout),\n" 3096 " andMoreParameters),\n" 3097 " trailing);", 3098 getLLVMStyleWithColumns(69)); 3099 verifyFormat("Foo::Foo()\n" 3100 "#ifdef BAR\n" 3101 " : baz(0)\n" 3102 "#endif\n" 3103 "{\n" 3104 "}"); 3105 verifyFormat("void f() {\n" 3106 " if (true)\n" 3107 "#ifdef A\n" 3108 " f(42);\n" 3109 " x();\n" 3110 "#else\n" 3111 " g();\n" 3112 " x();\n" 3113 "#endif\n" 3114 "}"); 3115 verifyFormat("void f(param1, param2,\n" 3116 " param3,\n" 3117 "#ifdef A\n" 3118 " param4(param5,\n" 3119 "#ifdef A1\n" 3120 " param6,\n" 3121 "#ifdef A2\n" 3122 " param7),\n" 3123 "#else\n" 3124 " param8),\n" 3125 " param9,\n" 3126 "#endif\n" 3127 " param10,\n" 3128 "#endif\n" 3129 " param11)\n" 3130 "#else\n" 3131 " param12)\n" 3132 "#endif\n" 3133 "{\n" 3134 " x();\n" 3135 "}", 3136 getLLVMStyleWithColumns(28)); 3137 verifyFormat("#if 1\n" 3138 "int i;"); 3139 verifyFormat("#if 1\n" 3140 "#endif\n" 3141 "#if 1\n" 3142 "#else\n" 3143 "#endif\n"); 3144 verifyFormat("DEBUG({\n" 3145 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 3147 "});\n" 3148 "#if a\n" 3149 "#else\n" 3150 "#endif"); 3151 3152 verifyIncompleteFormat("void f(\n" 3153 "#if A\n" 3154 " );\n" 3155 "#else\n" 3156 "#endif"); 3157 } 3158 3159 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 3160 verifyFormat("#endif\n" 3161 "#if B"); 3162 } 3163 3164 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 3165 FormatStyle SingleLine = getLLVMStyle(); 3166 SingleLine.AllowShortIfStatementsOnASingleLine = true; 3167 verifyFormat("#if 0\n" 3168 "#elif 1\n" 3169 "#endif\n" 3170 "void foo() {\n" 3171 " if (test) foo2();\n" 3172 "}", 3173 SingleLine); 3174 } 3175 3176 TEST_F(FormatTest, LayoutBlockInsideParens) { 3177 verifyFormat("functionCall({ int i; });"); 3178 verifyFormat("functionCall({\n" 3179 " int i;\n" 3180 " int j;\n" 3181 "});"); 3182 verifyFormat("functionCall({\n" 3183 " int i;\n" 3184 " int j;\n" 3185 "}, aaaa, bbbb, cccc);"); 3186 verifyFormat("functionA(functionB({\n" 3187 " int i;\n" 3188 " int j;\n" 3189 " }),\n" 3190 " aaaa, bbbb, cccc);"); 3191 verifyFormat("functionCall(\n" 3192 " {\n" 3193 " int i;\n" 3194 " int j;\n" 3195 " },\n" 3196 " aaaa, bbbb, // comment\n" 3197 " cccc);"); 3198 verifyFormat("functionA(functionB({\n" 3199 " int i;\n" 3200 " int j;\n" 3201 " }),\n" 3202 " aaaa, bbbb, // comment\n" 3203 " cccc);"); 3204 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 3205 verifyFormat("functionCall(aaaa, bbbb, {\n" 3206 " int i;\n" 3207 " int j;\n" 3208 "});"); 3209 verifyFormat( 3210 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 3211 " {\n" 3212 " int i; // break\n" 3213 " },\n" 3214 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3215 " ccccccccccccccccc));"); 3216 verifyFormat("DEBUG({\n" 3217 " if (a)\n" 3218 " f();\n" 3219 "});"); 3220 EXPECT_EQ("int longlongname; // comment\n" 3221 "int x = f({\n" 3222 " int x; // comment\n" 3223 " int y; // comment\n" 3224 "});", 3225 format("int longlongname; // comment\n" 3226 "int x = f({\n" 3227 " int x; // comment\n" 3228 " int y; // comment\n" 3229 "});", 3230 65, 0, getLLVMStyle())); 3231 EXPECT_EQ("int s = f({\n" 3232 " class X {\n" 3233 " public:\n" 3234 " void f();\n" 3235 " };\n" 3236 "});", 3237 format("int s = f({\n" 3238 " class X {\n" 3239 " public:\n" 3240 " void f();\n" 3241 " };\n" 3242 "});", 3243 0, 0, getLLVMStyle())); 3244 } 3245 3246 TEST_F(FormatTest, LayoutBlockInsideStatement) { 3247 EXPECT_EQ("SOME_MACRO { int i; }\n" 3248 "int i;", 3249 format(" SOME_MACRO {int i;} int i;")); 3250 } 3251 3252 TEST_F(FormatTest, LayoutNestedBlocks) { 3253 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 3254 " struct s {\n" 3255 " int i;\n" 3256 " };\n" 3257 " s kBitsToOs[] = {{10}};\n" 3258 " for (int i = 0; i < 10; ++i)\n" 3259 " return;\n" 3260 "}"); 3261 verifyFormat("call(parameter, {\n" 3262 " something();\n" 3263 " // Comment using all columns.\n" 3264 " somethingelse();\n" 3265 "});", 3266 getLLVMStyleWithColumns(40)); 3267 verifyFormat("DEBUG( //\n" 3268 " { f(); }, a);"); 3269 verifyFormat("DEBUG( //\n" 3270 " {\n" 3271 " f(); //\n" 3272 " },\n" 3273 " a);"); 3274 3275 EXPECT_EQ("call(parameter, {\n" 3276 " something();\n" 3277 " // Comment too\n" 3278 " // looooooooooong.\n" 3279 " somethingElse();\n" 3280 "});", 3281 format("call(parameter, {\n" 3282 " something();\n" 3283 " // Comment too looooooooooong.\n" 3284 " somethingElse();\n" 3285 "});", 3286 getLLVMStyleWithColumns(29))); 3287 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 3288 EXPECT_EQ("DEBUG({ // comment\n" 3289 " int i;\n" 3290 "});", 3291 format("DEBUG({ // comment\n" 3292 "int i;\n" 3293 "});")); 3294 EXPECT_EQ("DEBUG({\n" 3295 " int i;\n" 3296 "\n" 3297 " // comment\n" 3298 " int j;\n" 3299 "});", 3300 format("DEBUG({\n" 3301 " int i;\n" 3302 "\n" 3303 " // comment\n" 3304 " int j;\n" 3305 "});")); 3306 3307 verifyFormat("DEBUG({\n" 3308 " if (a)\n" 3309 " return;\n" 3310 "});"); 3311 verifyGoogleFormat("DEBUG({\n" 3312 " if (a) return;\n" 3313 "});"); 3314 FormatStyle Style = getGoogleStyle(); 3315 Style.ColumnLimit = 45; 3316 verifyFormat("Debug(aaaaa, {\n" 3317 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 3318 "}, a);", 3319 Style); 3320 3321 verifyNoCrash("^{v^{a}}"); 3322 } 3323 3324 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 3325 EXPECT_EQ("#define MACRO() \\\n" 3326 " Debug(aaa, /* force line break */ \\\n" 3327 " { \\\n" 3328 " int i; \\\n" 3329 " int j; \\\n" 3330 " })", 3331 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 3332 " { int i; int j; })", 3333 getGoogleStyle())); 3334 3335 EXPECT_EQ("#define A \\\n" 3336 " [] { \\\n" 3337 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3338 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 3339 " }", 3340 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 3341 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 3342 getGoogleStyle())); 3343 } 3344 3345 TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) { 3346 EXPECT_EQ("DEBUG({\n" 3347 " int i;\n" 3348 " int j;\n" 3349 "});", 3350 format("DEBUG( {\n" 3351 " int i;\n" 3352 " int j;\n" 3353 "} ) ;", 3354 20, 1, getLLVMStyle())); 3355 EXPECT_EQ("DEBUG( {\n" 3356 " int i;\n" 3357 " int j;\n" 3358 "} ) ;", 3359 format("DEBUG( {\n" 3360 " int i;\n" 3361 " int j;\n" 3362 "} ) ;", 3363 41, 1, getLLVMStyle())); 3364 EXPECT_EQ("DEBUG( {\n" 3365 " int i;\n" 3366 " int j;\n" 3367 "} ) ;", 3368 format("DEBUG( {\n" 3369 " int i;\n" 3370 " int j;\n" 3371 "} ) ;", 3372 41, 1, getLLVMStyle())); 3373 EXPECT_EQ("DEBUG({\n" 3374 " int i;\n" 3375 " int j;\n" 3376 "});", 3377 format("DEBUG( {\n" 3378 " int i;\n" 3379 " int j;\n" 3380 "} ) ;", 3381 20, 1, getLLVMStyle())); 3382 3383 EXPECT_EQ("Debug({\n" 3384 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" 3385 " return;\n" 3386 " },\n" 3387 " a);", 3388 format("Debug({\n" 3389 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" 3390 " return;\n" 3391 " },\n" 3392 " a);", 3393 50, 1, getLLVMStyle())); 3394 EXPECT_EQ("DEBUG({\n" 3395 " DEBUG({\n" 3396 " int a;\n" 3397 " int b;\n" 3398 " }) ;\n" 3399 "});", 3400 format("DEBUG({\n" 3401 " DEBUG({\n" 3402 " int a;\n" 3403 " int b;\n" // Format this line only. 3404 " }) ;\n" // Don't touch this line. 3405 "});", 3406 35, 0, getLLVMStyle())); 3407 EXPECT_EQ("DEBUG({\n" 3408 " int a; //\n" 3409 "});", 3410 format("DEBUG({\n" 3411 " int a; //\n" 3412 "});", 3413 0, 0, getLLVMStyle())); 3414 EXPECT_EQ("someFunction(\n" 3415 " [] {\n" 3416 " // Only with this comment.\n" 3417 " int i; // invoke formatting here.\n" 3418 " }, // force line break\n" 3419 " aaa);", 3420 format("someFunction(\n" 3421 " [] {\n" 3422 " // Only with this comment.\n" 3423 " int i; // invoke formatting here.\n" 3424 " }, // force line break\n" 3425 " aaa);", 3426 63, 1, getLLVMStyle())); 3427 } 3428 3429 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 3430 EXPECT_EQ("{}", format("{}")); 3431 verifyFormat("enum E {};"); 3432 verifyFormat("enum E {}"); 3433 } 3434 3435 //===----------------------------------------------------------------------===// 3436 // Line break tests. 3437 //===----------------------------------------------------------------------===// 3438 3439 TEST_F(FormatTest, PreventConfusingIndents) { 3440 verifyFormat( 3441 "void f() {\n" 3442 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 3443 " parameter, parameter, parameter)),\n" 3444 " SecondLongCall(parameter));\n" 3445 "}"); 3446 verifyFormat( 3447 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3448 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 3449 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3450 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 3451 verifyFormat( 3452 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3453 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 3454 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 3455 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 3456 verifyFormat( 3457 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 3458 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 3459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 3460 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 3461 verifyFormat("int a = bbbb && ccc && fffff(\n" 3462 "#define A Just forcing a new line\n" 3463 " ddd);"); 3464 } 3465 3466 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 3467 verifyFormat( 3468 "bool aaaaaaa =\n" 3469 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 3470 " bbbbbbbb();"); 3471 verifyFormat( 3472 "bool aaaaaaa =\n" 3473 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 3474 " bbbbbbbb();"); 3475 3476 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3477 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 3478 " ccccccccc == ddddddddddd;"); 3479 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 3480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 3481 " ccccccccc == ddddddddddd;"); 3482 verifyFormat( 3483 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 3484 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 3485 " ccccccccc == ddddddddddd;"); 3486 3487 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3488 " aaaaaa) &&\n" 3489 " bbbbbb && cccccc;"); 3490 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 3491 " aaaaaa) >>\n" 3492 " bbbbbb;"); 3493 verifyFormat("Whitespaces.addUntouchableComment(\n" 3494 " SourceMgr.getSpellingColumnNumber(\n" 3495 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 3496 " 1);"); 3497 3498 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3499 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 3500 " cccccc) {\n}"); 3501 verifyFormat("b = a &&\n" 3502 " // Comment\n" 3503 " b.c && d;"); 3504 3505 // If the LHS of a comparison is not a binary expression itself, the 3506 // additional linebreak confuses many people. 3507 verifyFormat( 3508 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 3510 "}"); 3511 verifyFormat( 3512 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3514 "}"); 3515 verifyFormat( 3516 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 3517 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3518 "}"); 3519 // Even explicit parentheses stress the precedence enough to make the 3520 // additional break unnecessary. 3521 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3522 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 3523 "}"); 3524 // This cases is borderline, but with the indentation it is still readable. 3525 verifyFormat( 3526 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3527 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3528 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 3529 "}", 3530 getLLVMStyleWithColumns(75)); 3531 3532 // If the LHS is a binary expression, we should still use the additional break 3533 // as otherwise the formatting hides the operator precedence. 3534 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3536 " 5) {\n" 3537 "}"); 3538 3539 FormatStyle OnePerLine = getLLVMStyle(); 3540 OnePerLine.BinPackParameters = false; 3541 verifyFormat( 3542 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3543 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 3544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 3545 OnePerLine); 3546 } 3547 3548 TEST_F(FormatTest, ExpressionIndentation) { 3549 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3550 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3553 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 3554 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 3555 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 3557 " ccccccccccccccccccccccccccccccccccccccccc;"); 3558 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3559 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3560 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3561 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3562 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3563 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3564 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3565 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3566 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 3567 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 3568 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 3569 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 3570 verifyFormat("if () {\n" 3571 "} else if (aaaaa &&\n" 3572 " bbbbb > // break\n" 3573 " ccccc) {\n" 3574 "}"); 3575 3576 // Presence of a trailing comment used to change indentation of b. 3577 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 3578 " b;\n" 3579 "return aaaaaaaaaaaaaaaaaaa +\n" 3580 " b; //", 3581 getLLVMStyleWithColumns(30)); 3582 } 3583 3584 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 3585 // Not sure what the best system is here. Like this, the LHS can be found 3586 // immediately above an operator (everything with the same or a higher 3587 // indent). The RHS is aligned right of the operator and so compasses 3588 // everything until something with the same indent as the operator is found. 3589 // FIXME: Is this a good system? 3590 FormatStyle Style = getLLVMStyle(); 3591 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 3592 verifyFormat( 3593 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3594 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3595 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3596 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3597 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3598 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3599 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3600 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3601 " > ccccccccccccccccccccccccccccccccccccccccc;", 3602 Style); 3603 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3604 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3605 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3606 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3607 Style); 3608 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3609 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3610 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3611 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3612 Style); 3613 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3614 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3615 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3616 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 3617 Style); 3618 verifyFormat("if () {\n" 3619 "} else if (aaaaa\n" 3620 " && bbbbb // break\n" 3621 " > ccccc) {\n" 3622 "}", 3623 Style); 3624 verifyFormat("return (a)\n" 3625 " // comment\n" 3626 " + b;", 3627 Style); 3628 verifyFormat( 3629 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3630 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3631 " + cc;", 3632 Style); 3633 3634 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3635 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 3636 Style); 3637 3638 // Forced by comments. 3639 verifyFormat( 3640 "unsigned ContentSize =\n" 3641 " sizeof(int16_t) // DWARF ARange version number\n" 3642 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 3643 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 3644 " + sizeof(int8_t); // Segment Size (in bytes)"); 3645 3646 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 3647 " == boost::fusion::at_c<1>(iiii).second;", 3648 Style); 3649 3650 Style.ColumnLimit = 60; 3651 verifyFormat("zzzzzzzzzz\n" 3652 " = bbbbbbbbbbbbbbbbb\n" 3653 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 3654 Style); 3655 } 3656 3657 TEST_F(FormatTest, NoOperandAlignment) { 3658 FormatStyle Style = getLLVMStyle(); 3659 Style.AlignOperands = false; 3660 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3661 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3662 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3663 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3664 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3665 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3666 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3667 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3668 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3669 " > ccccccccccccccccccccccccccccccccccccccccc;", 3670 Style); 3671 3672 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3673 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3674 " + cc;", 3675 Style); 3676 verifyFormat("int a = aa\n" 3677 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 3678 " * cccccccccccccccccccccccccccccccccccc;", 3679 Style); 3680 3681 Style.AlignAfterOpenBracket = false; 3682 verifyFormat("return (a > b\n" 3683 " // comment1\n" 3684 " // comment2\n" 3685 " || c);", 3686 Style); 3687 } 3688 3689 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 3690 FormatStyle Style = getLLVMStyle(); 3691 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 3692 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 3693 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3694 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 3695 Style); 3696 } 3697 3698 TEST_F(FormatTest, ConstructorInitializers) { 3699 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 3700 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 3701 getLLVMStyleWithColumns(45)); 3702 verifyFormat("Constructor()\n" 3703 " : Inttializer(FitsOnTheLine) {}", 3704 getLLVMStyleWithColumns(44)); 3705 verifyFormat("Constructor()\n" 3706 " : Inttializer(FitsOnTheLine) {}", 3707 getLLVMStyleWithColumns(43)); 3708 3709 verifyFormat( 3710 "SomeClass::Constructor()\n" 3711 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3712 3713 verifyFormat( 3714 "SomeClass::Constructor()\n" 3715 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3716 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 3717 verifyFormat( 3718 "SomeClass::Constructor()\n" 3719 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3720 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 3721 3722 verifyFormat("Constructor()\n" 3723 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3724 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3725 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 3726 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 3727 3728 verifyFormat("Constructor()\n" 3729 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3730 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3731 3732 verifyFormat("Constructor(int Parameter = 0)\n" 3733 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 3734 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 3735 verifyFormat("Constructor()\n" 3736 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 3737 "}", 3738 getLLVMStyleWithColumns(60)); 3739 verifyFormat("Constructor()\n" 3740 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3741 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 3742 3743 // Here a line could be saved by splitting the second initializer onto two 3744 // lines, but that is not desirable. 3745 verifyFormat("Constructor()\n" 3746 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 3747 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 3748 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 3749 3750 FormatStyle OnePerLine = getLLVMStyle(); 3751 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3752 verifyFormat("SomeClass::Constructor()\n" 3753 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3754 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3755 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3756 OnePerLine); 3757 verifyFormat("SomeClass::Constructor()\n" 3758 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 3759 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 3760 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 3761 OnePerLine); 3762 verifyFormat("MyClass::MyClass(int var)\n" 3763 " : some_var_(var), // 4 space indent\n" 3764 " some_other_var_(var + 1) { // lined up\n" 3765 "}", 3766 OnePerLine); 3767 verifyFormat("Constructor()\n" 3768 " : aaaaa(aaaaaa),\n" 3769 " aaaaa(aaaaaa),\n" 3770 " aaaaa(aaaaaa),\n" 3771 " aaaaa(aaaaaa),\n" 3772 " aaaaa(aaaaaa) {}", 3773 OnePerLine); 3774 verifyFormat("Constructor()\n" 3775 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 3776 " aaaaaaaaaaaaaaaaaaaaaa) {}", 3777 OnePerLine); 3778 3779 EXPECT_EQ("Constructor()\n" 3780 " : // Comment forcing unwanted break.\n" 3781 " aaaa(aaaa) {}", 3782 format("Constructor() :\n" 3783 " // Comment forcing unwanted break.\n" 3784 " aaaa(aaaa) {}")); 3785 } 3786 3787 TEST_F(FormatTest, MemoizationTests) { 3788 // This breaks if the memoization lookup does not take \c Indent and 3789 // \c LastSpace into account. 3790 verifyFormat( 3791 "extern CFRunLoopTimerRef\n" 3792 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 3793 " CFTimeInterval interval, CFOptionFlags flags,\n" 3794 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 3795 " CFRunLoopTimerContext *context) {}"); 3796 3797 // Deep nesting somewhat works around our memoization. 3798 verifyFormat( 3799 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3800 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3801 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3802 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 3803 " aaaaa())))))))))))))))))))))))))))))))))))))));", 3804 getLLVMStyleWithColumns(65)); 3805 verifyFormat( 3806 "aaaaa(\n" 3807 " aaaaa,\n" 3808 " aaaaa(\n" 3809 " aaaaa,\n" 3810 " aaaaa(\n" 3811 " aaaaa,\n" 3812 " aaaaa(\n" 3813 " aaaaa,\n" 3814 " aaaaa(\n" 3815 " aaaaa,\n" 3816 " aaaaa(\n" 3817 " aaaaa,\n" 3818 " aaaaa(\n" 3819 " aaaaa,\n" 3820 " aaaaa(\n" 3821 " aaaaa,\n" 3822 " aaaaa(\n" 3823 " aaaaa,\n" 3824 " aaaaa(\n" 3825 " aaaaa,\n" 3826 " aaaaa(\n" 3827 " aaaaa,\n" 3828 " aaaaa(\n" 3829 " aaaaa,\n" 3830 " aaaaa))))))))))));", 3831 getLLVMStyleWithColumns(65)); 3832 verifyFormat( 3833 "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" 3834 " a),\n" 3835 " a),\n" 3836 " a),\n" 3837 " a),\n" 3838 " a),\n" 3839 " a),\n" 3840 " a),\n" 3841 " a),\n" 3842 " a),\n" 3843 " a),\n" 3844 " a),\n" 3845 " a),\n" 3846 " a),\n" 3847 " a),\n" 3848 " a),\n" 3849 " a),\n" 3850 " a)", 3851 getLLVMStyleWithColumns(65)); 3852 3853 // This test takes VERY long when memoization is broken. 3854 FormatStyle OnePerLine = getLLVMStyle(); 3855 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 3856 OnePerLine.BinPackParameters = false; 3857 std::string input = "Constructor()\n" 3858 " : aaaa(a,\n"; 3859 for (unsigned i = 0, e = 80; i != e; ++i) { 3860 input += " a,\n"; 3861 } 3862 input += " a) {}"; 3863 verifyFormat(input, OnePerLine); 3864 } 3865 3866 TEST_F(FormatTest, BreaksAsHighAsPossible) { 3867 verifyFormat( 3868 "void f() {\n" 3869 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 3870 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 3871 " f();\n" 3872 "}"); 3873 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 3874 " Intervals[i - 1].getRange().getLast()) {\n}"); 3875 } 3876 3877 TEST_F(FormatTest, BreaksFunctionDeclarations) { 3878 // Principially, we break function declarations in a certain order: 3879 // 1) break amongst arguments. 3880 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 3881 " Cccccccccccccc cccccccccccccc);"); 3882 verifyFormat("template <class TemplateIt>\n" 3883 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 3884 " TemplateIt *stop) {}"); 3885 3886 // 2) break after return type. 3887 verifyFormat( 3888 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3889 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 3890 getGoogleStyle()); 3891 3892 // 3) break after (. 3893 verifyFormat( 3894 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 3895 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 3896 getGoogleStyle()); 3897 3898 // 4) break before after nested name specifiers. 3899 verifyFormat( 3900 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3901 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 3902 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 3903 getGoogleStyle()); 3904 3905 // However, there are exceptions, if a sufficient amount of lines can be 3906 // saved. 3907 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 3908 // more adjusting. 3909 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3910 " Cccccccccccccc cccccccccc,\n" 3911 " Cccccccccccccc cccccccccc,\n" 3912 " Cccccccccccccc cccccccccc,\n" 3913 " Cccccccccccccc cccccccccc);"); 3914 verifyFormat( 3915 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3916 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3917 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3918 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 3919 getGoogleStyle()); 3920 verifyFormat( 3921 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 3922 " Cccccccccccccc cccccccccc,\n" 3923 " Cccccccccccccc cccccccccc,\n" 3924 " Cccccccccccccc cccccccccc,\n" 3925 " Cccccccccccccc cccccccccc,\n" 3926 " Cccccccccccccc cccccccccc,\n" 3927 " Cccccccccccccc cccccccccc);"); 3928 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 3929 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3930 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3931 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 3932 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 3933 3934 // Break after multi-line parameters. 3935 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3936 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 3937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3938 " bbbb bbbb);"); 3939 verifyFormat("void SomeLoooooooooooongFunction(\n" 3940 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 3941 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3942 " int bbbbbbbbbbbbb);"); 3943 3944 // Treat overloaded operators like other functions. 3945 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3946 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 3947 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3948 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 3949 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 3950 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 3951 verifyGoogleFormat( 3952 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 3953 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3954 verifyGoogleFormat( 3955 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 3956 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 3957 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3958 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3959 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 3960 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 3961 verifyGoogleFormat( 3962 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 3963 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3964 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 3965 3966 FormatStyle Style = getLLVMStyle(); 3967 Style.PointerAlignment = FormatStyle::PAS_Left; 3968 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 3969 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 3970 Style); 3971 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 3972 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 3973 Style); 3974 } 3975 3976 TEST_F(FormatTest, TrailingReturnType) { 3977 verifyFormat("auto foo() -> int;\n"); 3978 verifyFormat("struct S {\n" 3979 " auto bar() const -> int;\n" 3980 "};"); 3981 verifyFormat("template <size_t Order, typename T>\n" 3982 "auto load_img(const std::string &filename)\n" 3983 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 3984 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 3985 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 3986 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 3987 verifyFormat("template <typename T>\n" 3988 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 3989 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 3990 3991 // Not trailing return types. 3992 verifyFormat("void f() { auto a = b->c(); }"); 3993 } 3994 3995 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 3996 // Avoid breaking before trailing 'const' or other trailing annotations, if 3997 // they are not function-like. 3998 FormatStyle Style = getGoogleStyle(); 3999 Style.ColumnLimit = 47; 4000 verifyFormat("void someLongFunction(\n" 4001 " int someLoooooooooooooongParameter) const {\n}", 4002 getLLVMStyleWithColumns(47)); 4003 verifyFormat("LoooooongReturnType\n" 4004 "someLoooooooongFunction() const {}", 4005 getLLVMStyleWithColumns(47)); 4006 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 4007 " const {}", 4008 Style); 4009 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4010 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 4011 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4012 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 4013 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 4014 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 4015 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 4016 " aaaaaaaaaaa aaaaa) const override;"); 4017 verifyGoogleFormat( 4018 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4019 " const override;"); 4020 4021 // Even if the first parameter has to be wrapped. 4022 verifyFormat("void someLongFunction(\n" 4023 " int someLongParameter) const {}", 4024 getLLVMStyleWithColumns(46)); 4025 verifyFormat("void someLongFunction(\n" 4026 " int someLongParameter) const {}", 4027 Style); 4028 verifyFormat("void someLongFunction(\n" 4029 " int someLongParameter) override {}", 4030 Style); 4031 verifyFormat("void someLongFunction(\n" 4032 " int someLongParameter) OVERRIDE {}", 4033 Style); 4034 verifyFormat("void someLongFunction(\n" 4035 " int someLongParameter) final {}", 4036 Style); 4037 verifyFormat("void someLongFunction(\n" 4038 " int someLongParameter) FINAL {}", 4039 Style); 4040 verifyFormat("void someLongFunction(\n" 4041 " int parameter) const override {}", 4042 Style); 4043 4044 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4045 verifyFormat("void someLongFunction(\n" 4046 " int someLongParameter) const\n" 4047 "{\n" 4048 "}", 4049 Style); 4050 4051 // Unless these are unknown annotations. 4052 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 4053 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4054 " LONG_AND_UGLY_ANNOTATION;"); 4055 4056 // Breaking before function-like trailing annotations is fine to keep them 4057 // close to their arguments. 4058 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4059 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4060 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4061 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 4062 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 4063 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 4064 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 4065 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 4066 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 4067 4068 verifyFormat( 4069 "void aaaaaaaaaaaaaaaaaa()\n" 4070 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 4071 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 4072 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4073 " __attribute__((unused));"); 4074 verifyGoogleFormat( 4075 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4076 " GUARDED_BY(aaaaaaaaaaaa);"); 4077 verifyGoogleFormat( 4078 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4079 " GUARDED_BY(aaaaaaaaaaaa);"); 4080 verifyGoogleFormat( 4081 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4082 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4083 verifyGoogleFormat( 4084 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 4085 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4086 } 4087 4088 TEST_F(FormatTest, FunctionAnnotations) { 4089 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4090 "string OldFunction(const string ¶meter) {}"); 4091 verifyFormat("template <typename T>\n" 4092 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 4093 "string OldFunction(const string ¶meter) {}"); 4094 4095 // Not function annotations. 4096 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4097 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 4098 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 4099 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 4100 } 4101 4102 TEST_F(FormatTest, BreaksDesireably) { 4103 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4104 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 4105 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 4106 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4107 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 4108 "}"); 4109 4110 verifyFormat( 4111 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 4113 4114 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4115 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4116 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4117 4118 verifyFormat( 4119 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4120 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4121 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4122 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 4123 4124 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4125 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4126 4127 verifyFormat( 4128 "void f() {\n" 4129 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 4130 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4131 "}"); 4132 verifyFormat( 4133 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4135 verifyFormat( 4136 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4137 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 4138 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4139 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4140 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4141 4142 // Indent consistently independent of call expression. 4143 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 4144 " dddddddddddddddddddddddddddddd));\n" 4145 "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 4146 " dddddddddddddddddddddddddddddd));"); 4147 4148 // This test case breaks on an incorrect memoization, i.e. an optimization not 4149 // taking into account the StopAt value. 4150 verifyFormat( 4151 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4152 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4153 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 4154 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4155 4156 verifyFormat("{\n {\n {\n" 4157 " Annotation.SpaceRequiredBefore =\n" 4158 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 4159 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 4160 " }\n }\n}"); 4161 4162 // Break on an outer level if there was a break on an inner level. 4163 EXPECT_EQ("f(g(h(a, // comment\n" 4164 " b, c),\n" 4165 " d, e),\n" 4166 " x, y);", 4167 format("f(g(h(a, // comment\n" 4168 " b, c), d, e), x, y);")); 4169 4170 // Prefer breaking similar line breaks. 4171 verifyFormat( 4172 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 4173 " NSTrackingMouseEnteredAndExited |\n" 4174 " NSTrackingActiveAlways;"); 4175 } 4176 4177 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 4178 FormatStyle NoBinPacking = getGoogleStyle(); 4179 NoBinPacking.BinPackParameters = false; 4180 NoBinPacking.BinPackArguments = true; 4181 verifyFormat("void f() {\n" 4182 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 4183 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4184 "}", 4185 NoBinPacking); 4186 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 4187 " int aaaaaaaaaaaaaaaaaaaa,\n" 4188 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4189 NoBinPacking); 4190 } 4191 4192 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 4193 FormatStyle NoBinPacking = getGoogleStyle(); 4194 NoBinPacking.BinPackParameters = false; 4195 NoBinPacking.BinPackArguments = false; 4196 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 4197 " aaaaaaaaaaaaaaaaaaaa,\n" 4198 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 4199 NoBinPacking); 4200 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 4201 " aaaaaaaaaaaaa,\n" 4202 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 4203 NoBinPacking); 4204 verifyFormat( 4205 "aaaaaaaa(aaaaaaaaaaaaa,\n" 4206 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4207 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 4208 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 4210 NoBinPacking); 4211 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4212 " .aaaaaaaaaaaaaaaaaa();", 4213 NoBinPacking); 4214 verifyFormat("void f() {\n" 4215 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4216 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 4217 "}", 4218 NoBinPacking); 4219 4220 verifyFormat( 4221 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4222 " aaaaaaaaaaaa,\n" 4223 " aaaaaaaaaaaa);", 4224 NoBinPacking); 4225 verifyFormat( 4226 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 4227 " ddddddddddddddddddddddddddddd),\n" 4228 " test);", 4229 NoBinPacking); 4230 4231 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 4232 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 4233 " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;", 4234 NoBinPacking); 4235 verifyFormat("a(\"a\"\n" 4236 " \"a\",\n" 4237 " a);"); 4238 4239 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 4240 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 4241 " aaaaaaaaa,\n" 4242 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4243 NoBinPacking); 4244 verifyFormat( 4245 "void f() {\n" 4246 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 4247 " .aaaaaaa();\n" 4248 "}", 4249 NoBinPacking); 4250 verifyFormat( 4251 "template <class SomeType, class SomeOtherType>\n" 4252 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 4253 NoBinPacking); 4254 } 4255 4256 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 4257 FormatStyle Style = getLLVMStyleWithColumns(15); 4258 Style.ExperimentalAutoDetectBinPacking = true; 4259 EXPECT_EQ("aaa(aaaa,\n" 4260 " aaaa,\n" 4261 " aaaa);\n" 4262 "aaa(aaaa,\n" 4263 " aaaa,\n" 4264 " aaaa);", 4265 format("aaa(aaaa,\n" // one-per-line 4266 " aaaa,\n" 4267 " aaaa );\n" 4268 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4269 Style)); 4270 EXPECT_EQ("aaa(aaaa, aaaa,\n" 4271 " aaaa);\n" 4272 "aaa(aaaa, aaaa,\n" 4273 " aaaa);", 4274 format("aaa(aaaa, aaaa,\n" // bin-packed 4275 " aaaa );\n" 4276 "aaa(aaaa, aaaa, aaaa);", // inconclusive 4277 Style)); 4278 } 4279 4280 TEST_F(FormatTest, FormatsBuilderPattern) { 4281 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 4282 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 4283 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 4284 " .StartsWith(\".init\", ORDER_INIT)\n" 4285 " .StartsWith(\".fini\", ORDER_FINI)\n" 4286 " .StartsWith(\".hash\", ORDER_HASH)\n" 4287 " .Default(ORDER_TEXT);\n"); 4288 4289 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 4290 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 4291 verifyFormat( 4292 "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n" 4293 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4294 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 4295 verifyFormat( 4296 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 4297 " aaaaaaaaaaaaaa);"); 4298 verifyFormat( 4299 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 4300 " aaaaaa->aaaaaaaaaaaa()\n" 4301 " ->aaaaaaaaaaaaaaaa(\n" 4302 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4303 " ->aaaaaaaaaaaaaaaaa();"); 4304 verifyGoogleFormat( 4305 "void f() {\n" 4306 " someo->Add((new util::filetools::Handler(dir))\n" 4307 " ->OnEvent1(NewPermanentCallback(\n" 4308 " this, &HandlerHolderClass::EventHandlerCBA))\n" 4309 " ->OnEvent2(NewPermanentCallback(\n" 4310 " this, &HandlerHolderClass::EventHandlerCBB))\n" 4311 " ->OnEvent3(NewPermanentCallback(\n" 4312 " this, &HandlerHolderClass::EventHandlerCBC))\n" 4313 " ->OnEvent5(NewPermanentCallback(\n" 4314 " this, &HandlerHolderClass::EventHandlerCBD))\n" 4315 " ->OnEvent6(NewPermanentCallback(\n" 4316 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 4317 "}"); 4318 4319 verifyFormat( 4320 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 4321 verifyFormat("aaaaaaaaaaaaaaa()\n" 4322 " .aaaaaaaaaaaaaaa()\n" 4323 " .aaaaaaaaaaaaaaa()\n" 4324 " .aaaaaaaaaaaaaaa()\n" 4325 " .aaaaaaaaaaaaaaa();"); 4326 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4327 " .aaaaaaaaaaaaaaa()\n" 4328 " .aaaaaaaaaaaaaaa()\n" 4329 " .aaaaaaaaaaaaaaa();"); 4330 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4331 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 4332 " .aaaaaaaaaaaaaaa();"); 4333 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 4334 " ->aaaaaaaaaaaaaae(0)\n" 4335 " ->aaaaaaaaaaaaaaa();"); 4336 4337 // Don't linewrap after very short segments. 4338 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4339 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4340 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4341 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4342 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4343 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4344 verifyFormat("aaa()\n" 4345 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4346 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4347 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 4348 4349 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4350 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 4351 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 4352 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 4353 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4354 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 4355 4356 // Prefer not to break after empty parentheses. 4357 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 4358 " First->LastNewlineOffset);"); 4359 } 4360 4361 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 4362 verifyFormat( 4363 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4364 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 4365 verifyFormat( 4366 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 4367 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 4368 4369 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4370 " ccccccccccccccccccccccccc) {\n}"); 4371 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 4372 " ccccccccccccccccccccccccc) {\n}"); 4373 4374 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 4375 " ccccccccccccccccccccccccc) {\n}"); 4376 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 4377 " ccccccccccccccccccccccccc) {\n}"); 4378 4379 verifyFormat( 4380 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 4381 " ccccccccccccccccccccccccc) {\n}"); 4382 verifyFormat( 4383 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 4384 " ccccccccccccccccccccccccc) {\n}"); 4385 4386 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 4387 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 4388 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 4389 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4390 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 4391 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 4392 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 4393 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 4394 4395 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 4396 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 4397 " aaaaaaaaaaaaaaa != aa) {\n}"); 4398 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 4399 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 4400 " aaaaaaaaaaaaaaa != aa) {\n}"); 4401 } 4402 4403 TEST_F(FormatTest, BreaksAfterAssignments) { 4404 verifyFormat( 4405 "unsigned Cost =\n" 4406 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 4407 " SI->getPointerAddressSpaceee());\n"); 4408 verifyFormat( 4409 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 4410 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 4411 4412 verifyFormat( 4413 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 4414 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 4415 verifyFormat("unsigned OriginalStartColumn =\n" 4416 " SourceMgr.getSpellingColumnNumber(\n" 4417 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 4418 " 1;"); 4419 } 4420 4421 TEST_F(FormatTest, AlignsAfterAssignments) { 4422 verifyFormat( 4423 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4424 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4425 verifyFormat( 4426 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4427 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4428 verifyFormat( 4429 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4430 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4431 verifyFormat( 4432 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4433 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4434 verifyFormat( 4435 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4436 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 4437 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 4438 } 4439 4440 TEST_F(FormatTest, AlignsAfterReturn) { 4441 verifyFormat( 4442 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4443 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 4444 verifyFormat( 4445 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4446 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 4447 verifyFormat( 4448 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4449 " aaaaaaaaaaaaaaaaaaaaaa();"); 4450 verifyFormat( 4451 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 4452 " aaaaaaaaaaaaaaaaaaaaaa());"); 4453 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4455 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4456 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 4457 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4458 verifyFormat("return\n" 4459 " // true if code is one of a or b.\n" 4460 " code == a || code == b;"); 4461 } 4462 4463 TEST_F(FormatTest, AlignsAfterOpenBracket) { 4464 verifyFormat( 4465 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4466 " aaaaaaaaa aaaaaaa) {}"); 4467 verifyFormat( 4468 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4469 " aaaaaaaaaaa aaaaaaaaa);"); 4470 verifyFormat( 4471 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4472 " aaaaaaaaaaaaaaaaaaaaa));"); 4473 FormatStyle Style = getLLVMStyle(); 4474 Style.AlignAfterOpenBracket = false; 4475 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4476 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 4477 Style); 4478 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 4479 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 4480 Style); 4481 verifyFormat("SomeLongVariableName->someFunction(\n" 4482 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 4483 Style); 4484 verifyFormat( 4485 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 4486 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 4487 Style); 4488 verifyFormat( 4489 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 4490 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4491 Style); 4492 verifyFormat( 4493 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 4494 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 4495 Style); 4496 } 4497 4498 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 4499 FormatStyle Style = getLLVMStyleWithColumns(40); 4500 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4501 " bbbbbbbbbbbbbbbbbbbbbb);", 4502 Style); 4503 Style.AlignAfterOpenBracket = true; 4504 Style.AlignOperands = false; 4505 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4506 " bbbbbbbbbbbbbbbbbbbbbb);", 4507 Style); 4508 Style.AlignAfterOpenBracket = false; 4509 Style.AlignOperands = true; 4510 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4511 " bbbbbbbbbbbbbbbbbbbbbb);", 4512 Style); 4513 Style.AlignAfterOpenBracket = false; 4514 Style.AlignOperands = false; 4515 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 4516 " bbbbbbbbbbbbbbbbbbbbbb);", 4517 Style); 4518 } 4519 4520 TEST_F(FormatTest, BreaksConditionalExpressions) { 4521 verifyFormat( 4522 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4523 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4524 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4525 verifyFormat( 4526 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4527 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4528 verifyFormat( 4529 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 4530 " : aaaaaaaaaaaaa);"); 4531 verifyFormat( 4532 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4533 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4534 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4535 " aaaaaaaaaaaaa);"); 4536 verifyFormat( 4537 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4538 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4539 " aaaaaaaaaaaaa);"); 4540 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4541 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4542 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4543 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4545 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4546 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4547 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4549 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4550 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4551 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4552 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4553 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4554 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4555 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4556 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4557 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4558 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4559 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4560 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4561 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4562 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4563 " : aaaaaaaaaaaaaaaa;"); 4564 verifyFormat( 4565 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4566 " ? aaaaaaaaaaaaaaa\n" 4567 " : aaaaaaaaaaaaaaa;"); 4568 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4569 " aaaaaaaaa\n" 4570 " ? b\n" 4571 " : c);"); 4572 verifyFormat("return aaaa == bbbb\n" 4573 " // comment\n" 4574 " ? aaaa\n" 4575 " : bbbb;"); 4576 verifyFormat("unsigned Indent =\n" 4577 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" 4578 " ? IndentForLevel[TheLine.Level]\n" 4579 " : TheLine * 2,\n" 4580 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4581 getLLVMStyleWithColumns(70)); 4582 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4583 " ? aaaaaaaaaaaaaaa\n" 4584 " : bbbbbbbbbbbbbbb //\n" 4585 " ? ccccccccccccccc\n" 4586 " : ddddddddddddddd;"); 4587 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 4588 " ? aaaaaaaaaaaaaaa\n" 4589 " : (bbbbbbbbbbbbbbb //\n" 4590 " ? ccccccccccccccc\n" 4591 " : ddddddddddddddd);"); 4592 verifyFormat( 4593 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4594 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4595 " aaaaaaaaaaaaaaaaaaaaa +\n" 4596 " aaaaaaaaaaaaaaaaaaaaa\n" 4597 " : aaaaaaaaaa;"); 4598 verifyFormat( 4599 "aaaaaa = aaaaaaaaaaaa\n" 4600 " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4601 " : aaaaaaaaaaaaaaaaaaaaaa\n" 4602 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4603 4604 FormatStyle NoBinPacking = getLLVMStyle(); 4605 NoBinPacking.BinPackArguments = false; 4606 verifyFormat( 4607 "void f() {\n" 4608 " g(aaa,\n" 4609 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4610 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4611 " ? aaaaaaaaaaaaaaa\n" 4612 " : aaaaaaaaaaaaaaa);\n" 4613 "}", 4614 NoBinPacking); 4615 verifyFormat( 4616 "void f() {\n" 4617 " g(aaa,\n" 4618 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 4619 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4620 " ?: aaaaaaaaaaaaaaa);\n" 4621 "}", 4622 NoBinPacking); 4623 4624 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 4625 " // comment.\n" 4626 " ccccccccccccccccccccccccccccccccccccccc\n" 4627 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4628 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 4629 4630 // Assignments in conditional expressions. Apparently not uncommon :-(. 4631 verifyFormat("return a != b\n" 4632 " // comment\n" 4633 " ? a = b\n" 4634 " : a = b;"); 4635 verifyFormat("return a != b\n" 4636 " // comment\n" 4637 " ? a = a != b\n" 4638 " // comment\n" 4639 " ? a = b\n" 4640 " : a\n" 4641 " : a;\n"); 4642 verifyFormat("return a != b\n" 4643 " // comment\n" 4644 " ? a\n" 4645 " : a = a != b\n" 4646 " // comment\n" 4647 " ? a = b\n" 4648 " : a;"); 4649 } 4650 4651 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 4652 FormatStyle Style = getLLVMStyle(); 4653 Style.BreakBeforeTernaryOperators = false; 4654 Style.ColumnLimit = 70; 4655 verifyFormat( 4656 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4657 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4658 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4659 Style); 4660 verifyFormat( 4661 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4662 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4663 Style); 4664 verifyFormat( 4665 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 4666 " aaaaaaaaaaaaa);", 4667 Style); 4668 verifyFormat( 4669 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4670 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4671 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4672 " aaaaaaaaaaaaa);", 4673 Style); 4674 verifyFormat( 4675 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4676 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4677 " aaaaaaaaaaaaa);", 4678 Style); 4679 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4681 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4682 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4683 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4684 Style); 4685 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4686 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4687 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4688 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 4689 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4690 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4691 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4692 Style); 4693 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4694 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 4695 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4696 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4697 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 4698 Style); 4699 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4700 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4701 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4702 Style); 4703 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 4704 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4705 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 4706 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4707 Style); 4708 verifyFormat( 4709 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 4710 " aaaaaaaaaaaaaaa :\n" 4711 " aaaaaaaaaaaaaaa;", 4712 Style); 4713 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 4714 " aaaaaaaaa ?\n" 4715 " b :\n" 4716 " c);", 4717 Style); 4718 verifyFormat( 4719 "unsigned Indent =\n" 4720 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n" 4721 " IndentForLevel[TheLine.Level] :\n" 4722 " TheLine * 2,\n" 4723 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 4724 Style); 4725 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4726 " aaaaaaaaaaaaaaa :\n" 4727 " bbbbbbbbbbbbbbb ? //\n" 4728 " ccccccccccccccc :\n" 4729 " ddddddddddddddd;", 4730 Style); 4731 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 4732 " aaaaaaaaaaaaaaa :\n" 4733 " (bbbbbbbbbbbbbbb ? //\n" 4734 " ccccccccccccccc :\n" 4735 " ddddddddddddddd);", 4736 Style); 4737 } 4738 4739 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 4740 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 4741 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 4742 verifyFormat("bool a = true, b = false;"); 4743 4744 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 4745 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 4746 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 4747 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 4748 verifyFormat( 4749 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4750 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 4751 " d = e && f;"); 4752 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 4753 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 4754 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4755 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 4756 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 4757 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 4758 4759 FormatStyle Style = getGoogleStyle(); 4760 Style.PointerAlignment = FormatStyle::PAS_Left; 4761 Style.DerivePointerAlignment = false; 4762 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4763 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 4764 " *b = bbbbbbbbbbbbbbbbbbb;", 4765 Style); 4766 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 4767 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 4768 Style); 4769 } 4770 4771 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 4772 verifyFormat("arr[foo ? bar : baz];"); 4773 verifyFormat("f()[foo ? bar : baz];"); 4774 verifyFormat("(a + b)[foo ? bar : baz];"); 4775 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 4776 } 4777 4778 TEST_F(FormatTest, AlignsStringLiterals) { 4779 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 4780 " \"short literal\");"); 4781 verifyFormat( 4782 "looooooooooooooooooooooooongFunction(\n" 4783 " \"short literal\"\n" 4784 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 4785 verifyFormat("someFunction(\"Always break between multi-line\"\n" 4786 " \" string literals\",\n" 4787 " and, other, parameters);"); 4788 EXPECT_EQ("fun + \"1243\" /* comment */\n" 4789 " \"5678\";", 4790 format("fun + \"1243\" /* comment */\n" 4791 " \"5678\";", 4792 getLLVMStyleWithColumns(28))); 4793 EXPECT_EQ( 4794 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 4795 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 4796 " \"aaaaaaaaaaaaaaaa\";", 4797 format("aaaaaa =" 4798 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 4799 "aaaaaaaaaaaaaaaaaaaaa\" " 4800 "\"aaaaaaaaaaaaaaaa\";")); 4801 verifyFormat("a = a + \"a\"\n" 4802 " \"a\"\n" 4803 " \"a\";"); 4804 verifyFormat("f(\"a\", \"b\"\n" 4805 " \"c\");"); 4806 4807 verifyFormat( 4808 "#define LL_FORMAT \"ll\"\n" 4809 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 4810 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 4811 4812 verifyFormat("#define A(X) \\\n" 4813 " \"aaaaa\" #X \"bbbbbb\" \\\n" 4814 " \"ccccc\"", 4815 getLLVMStyleWithColumns(23)); 4816 verifyFormat("#define A \"def\"\n" 4817 "f(\"abc\" A \"ghi\"\n" 4818 " \"jkl\");"); 4819 4820 verifyFormat("f(L\"a\"\n" 4821 " L\"b\");"); 4822 verifyFormat("#define A(X) \\\n" 4823 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 4824 " L\"ccccc\"", 4825 getLLVMStyleWithColumns(25)); 4826 4827 verifyFormat("f(@\"a\"\n" 4828 " @\"b\");"); 4829 verifyFormat("NSString s = @\"a\"\n" 4830 " @\"b\"\n" 4831 " @\"c\";"); 4832 verifyFormat("NSString s = @\"a\"\n" 4833 " \"b\"\n" 4834 " \"c\";"); 4835 } 4836 4837 TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) { 4838 FormatStyle AfterType = getLLVMStyle(); 4839 AfterType.AlwaysBreakAfterDefinitionReturnType = true; 4840 verifyFormat("const char *\n" 4841 "f(void) {\n" // Break here. 4842 " return \"\";\n" 4843 "}\n" 4844 "const char *bar(void);\n", // No break here. 4845 AfterType); 4846 verifyFormat("template <class T>\n" 4847 "T *\n" 4848 "f(T &c) {\n" // Break here. 4849 " return NULL;\n" 4850 "}\n" 4851 "template <class T> T *f(T &c);\n", // No break here. 4852 AfterType); 4853 AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4854 verifyFormat("const char *\n" 4855 "f(void)\n" // Break here. 4856 "{\n" 4857 " return \"\";\n" 4858 "}\n" 4859 "const char *bar(void);\n", // No break here. 4860 AfterType); 4861 verifyFormat("template <class T>\n" 4862 "T *\n" // Problem here: no line break 4863 "f(T &c)\n" // Break here. 4864 "{\n" 4865 " return NULL;\n" 4866 "}\n" 4867 "template <class T> T *f(T &c);\n", // No break here. 4868 AfterType); 4869 } 4870 4871 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 4872 FormatStyle NoBreak = getLLVMStyle(); 4873 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 4874 FormatStyle Break = getLLVMStyle(); 4875 Break.AlwaysBreakBeforeMultilineStrings = true; 4876 verifyFormat("aaaa = \"bbbb\"\n" 4877 " \"cccc\";", 4878 NoBreak); 4879 verifyFormat("aaaa =\n" 4880 " \"bbbb\"\n" 4881 " \"cccc\";", 4882 Break); 4883 verifyFormat("aaaa(\"bbbb\"\n" 4884 " \"cccc\");", 4885 NoBreak); 4886 verifyFormat("aaaa(\n" 4887 " \"bbbb\"\n" 4888 " \"cccc\");", 4889 Break); 4890 verifyFormat("aaaa(qqq, \"bbbb\"\n" 4891 " \"cccc\");", 4892 NoBreak); 4893 verifyFormat("aaaa(qqq,\n" 4894 " \"bbbb\"\n" 4895 " \"cccc\");", 4896 Break); 4897 verifyFormat("aaaa(qqq,\n" 4898 " L\"bbbb\"\n" 4899 " L\"cccc\");", 4900 Break); 4901 4902 // As we break before unary operators, breaking right after them is bad. 4903 verifyFormat("string foo = abc ? \"x\"\n" 4904 " \"blah blah blah blah blah blah\"\n" 4905 " : \"y\";", 4906 Break); 4907 4908 // Don't break if there is no column gain. 4909 verifyFormat("f(\"aaaa\"\n" 4910 " \"bbbb\");", 4911 Break); 4912 4913 // Treat literals with escaped newlines like multi-line string literals. 4914 EXPECT_EQ("x = \"a\\\n" 4915 "b\\\n" 4916 "c\";", 4917 format("x = \"a\\\n" 4918 "b\\\n" 4919 "c\";", 4920 NoBreak)); 4921 EXPECT_EQ("x =\n" 4922 " \"a\\\n" 4923 "b\\\n" 4924 "c\";", 4925 format("x = \"a\\\n" 4926 "b\\\n" 4927 "c\";", 4928 Break)); 4929 4930 // Exempt ObjC strings for now. 4931 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n" 4932 " @\"bbbb\";", 4933 format("NSString *const kString = @\"aaaa\"\n" 4934 "@\"bbbb\";", 4935 Break)); 4936 4937 Break.ColumnLimit = 0; 4938 verifyFormat("const char *hello = \"hello llvm\";", Break); 4939 } 4940 4941 TEST_F(FormatTest, AlignsPipes) { 4942 verifyFormat( 4943 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4944 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4945 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4946 verifyFormat( 4947 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 4948 " << aaaaaaaaaaaaaaaaaaaa;"); 4949 verifyFormat( 4950 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4951 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4952 verifyFormat( 4953 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 4954 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 4955 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 4956 verifyFormat( 4957 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4958 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4959 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4960 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4961 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4962 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 4963 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4964 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 4965 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 4966 verifyFormat( 4967 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4968 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 4969 4970 verifyFormat("return out << \"somepacket = {\\n\"\n" 4971 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 4972 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 4973 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 4974 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 4975 " << \"}\";"); 4976 4977 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4978 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 4979 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 4980 verifyFormat( 4981 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 4982 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 4983 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 4984 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 4985 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 4986 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 4987 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 4988 verifyFormat( 4989 "void f() {\n" 4990 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 4991 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 4992 "}"); 4993 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 4994 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 4995 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4996 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4997 " aaaaaaaaaaaaaaaaaaaaa)\n" 4998 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 4999 verifyFormat("LOG_IF(aaa == //\n" 5000 " bbb)\n" 5001 " << a << b;"); 5002 5003 // Breaking before the first "<<" is generally not desirable. 5004 verifyFormat( 5005 "llvm::errs()\n" 5006 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5007 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5008 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5009 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5010 getLLVMStyleWithColumns(70)); 5011 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5012 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5013 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5014 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5015 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 5016 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5017 getLLVMStyleWithColumns(70)); 5018 5019 // But sometimes, breaking before the first "<<" is desirable. 5020 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 5021 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 5022 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 5023 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5024 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5025 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 5026 " << BEF << IsTemplate << Description << E->getType();"); 5027 5028 verifyFormat( 5029 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5030 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5031 5032 // Incomplete string literal. 5033 EXPECT_EQ("llvm::errs() << \"\n" 5034 " << a;", 5035 format("llvm::errs() << \"\n<<a;")); 5036 5037 verifyFormat("void f() {\n" 5038 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 5039 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 5040 "}"); 5041 5042 // Handle 'endl'. 5043 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 5044 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5045 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 5046 } 5047 5048 TEST_F(FormatTest, UnderstandsEquals) { 5049 verifyFormat( 5050 "aaaaaaaaaaaaaaaaa =\n" 5051 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5052 verifyFormat( 5053 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5055 verifyFormat( 5056 "if (a) {\n" 5057 " f();\n" 5058 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5059 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5060 "}"); 5061 5062 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5063 " 100000000 + 10000000) {\n}"); 5064 } 5065 5066 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 5067 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5068 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 5069 5070 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 5071 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 5072 5073 verifyFormat( 5074 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 5075 " Parameter2);"); 5076 5077 verifyFormat( 5078 "ShortObject->shortFunction(\n" 5079 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 5080 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 5081 5082 verifyFormat("loooooooooooooongFunction(\n" 5083 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 5084 5085 verifyFormat( 5086 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 5087 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 5088 5089 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5090 " .WillRepeatedly(Return(SomeValue));"); 5091 verifyFormat("void f() {\n" 5092 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 5093 " .Times(2)\n" 5094 " .WillRepeatedly(Return(SomeValue));\n" 5095 "}"); 5096 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 5097 " ccccccccccccccccccccccc);"); 5098 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5099 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5100 " .aaaaa(aaaaa),\n" 5101 " aaaaaaaaaaaaaaaaaaaaa);"); 5102 verifyFormat("void f() {\n" 5103 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5104 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 5105 "}"); 5106 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5107 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5108 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5109 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5110 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5111 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5112 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5113 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5114 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 5115 "}"); 5116 5117 // Here, it is not necessary to wrap at "." or "->". 5118 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 5119 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 5120 verifyFormat( 5121 "aaaaaaaaaaa->aaaaaaaaa(\n" 5122 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5123 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 5124 5125 verifyFormat( 5126 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 5128 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 5129 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5130 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 5131 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 5132 5133 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5135 " .a();"); 5136 5137 FormatStyle NoBinPacking = getLLVMStyle(); 5138 NoBinPacking.BinPackParameters = false; 5139 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5140 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 5141 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 5142 " aaaaaaaaaaaaaaaaaaa,\n" 5143 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5144 NoBinPacking); 5145 5146 // If there is a subsequent call, change to hanging indentation. 5147 verifyFormat( 5148 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5149 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 5150 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5151 verifyFormat( 5152 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5153 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 5154 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5155 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5156 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5157 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5158 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5159 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5160 } 5161 5162 TEST_F(FormatTest, WrapsTemplateDeclarations) { 5163 verifyFormat("template <typename T>\n" 5164 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5165 verifyFormat("template <typename T>\n" 5166 "// T should be one of {A, B}.\n" 5167 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 5168 verifyFormat( 5169 "template <typename T>\n" 5170 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 5171 verifyFormat("template <typename T>\n" 5172 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 5173 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 5174 verifyFormat( 5175 "template <typename T>\n" 5176 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 5177 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 5178 verifyFormat( 5179 "template <typename T>\n" 5180 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 5181 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 5182 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5183 verifyFormat("template <typename T>\n" 5184 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5185 " int aaaaaaaaaaaaaaaaaaaaaa);"); 5186 verifyFormat( 5187 "template <typename T1, typename T2 = char, typename T3 = char,\n" 5188 " typename T4 = char>\n" 5189 "void f();"); 5190 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 5191 " template <typename> class cccccccccccccccccccccc,\n" 5192 " typename ddddddddddddd>\n" 5193 "class C {};"); 5194 verifyFormat( 5195 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 5196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5197 5198 verifyFormat("void f() {\n" 5199 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 5200 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 5201 "}"); 5202 5203 verifyFormat("template <typename T> class C {};"); 5204 verifyFormat("template <typename T> void f();"); 5205 verifyFormat("template <typename T> void f() {}"); 5206 verifyFormat( 5207 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5208 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 5210 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 5211 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5212 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 5213 " bbbbbbbbbbbbbbbbbbbbbbbb);", 5214 getLLVMStyleWithColumns(72)); 5215 EXPECT_EQ("static_cast<A< //\n" 5216 " B> *>(\n" 5217 "\n" 5218 " );", 5219 format("static_cast<A<//\n" 5220 " B>*>(\n" 5221 "\n" 5222 " );")); 5223 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5224 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 5225 5226 FormatStyle AlwaysBreak = getLLVMStyle(); 5227 AlwaysBreak.AlwaysBreakTemplateDeclarations = true; 5228 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 5229 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 5230 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 5231 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5232 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 5233 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 5234 verifyFormat("template <template <typename> class Fooooooo,\n" 5235 " template <typename> class Baaaaaaar>\n" 5236 "struct C {};", 5237 AlwaysBreak); 5238 verifyFormat("template <typename T> // T can be A, B or C.\n" 5239 "struct C {};", 5240 AlwaysBreak); 5241 } 5242 5243 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 5244 verifyFormat( 5245 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5246 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5247 verifyFormat( 5248 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5249 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5250 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 5251 5252 // FIXME: Should we have the extra indent after the second break? 5253 verifyFormat( 5254 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5255 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5256 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5257 5258 verifyFormat( 5259 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 5260 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 5261 5262 // Breaking at nested name specifiers is generally not desirable. 5263 verifyFormat( 5264 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5265 " aaaaaaaaaaaaaaaaaaaaaaa);"); 5266 5267 verifyFormat( 5268 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5270 " aaaaaaaaaaaaaaaaaaaaa);", 5271 getLLVMStyleWithColumns(74)); 5272 5273 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 5274 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5275 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 5276 } 5277 5278 TEST_F(FormatTest, UnderstandsTemplateParameters) { 5279 verifyFormat("A<int> a;"); 5280 verifyFormat("A<A<A<int>>> a;"); 5281 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 5282 verifyFormat("bool x = a < 1 || 2 > a;"); 5283 verifyFormat("bool x = 5 < f<int>();"); 5284 verifyFormat("bool x = f<int>() > 5;"); 5285 verifyFormat("bool x = 5 < a<int>::x;"); 5286 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 5287 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 5288 5289 verifyGoogleFormat("A<A<int>> a;"); 5290 verifyGoogleFormat("A<A<A<int>>> a;"); 5291 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 5292 verifyGoogleFormat("A<A<int> > a;"); 5293 verifyGoogleFormat("A<A<A<int> > > a;"); 5294 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 5295 verifyGoogleFormat("A<::A<int>> a;"); 5296 verifyGoogleFormat("A<::A> a;"); 5297 verifyGoogleFormat("A< ::A> a;"); 5298 verifyGoogleFormat("A< ::A<int> > a;"); 5299 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 5300 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 5301 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 5302 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 5303 5304 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 5305 5306 verifyFormat("test >> a >> b;"); 5307 verifyFormat("test << a >> b;"); 5308 5309 verifyFormat("f<int>();"); 5310 verifyFormat("template <typename T> void f() {}"); 5311 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 5312 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 5313 "sizeof(char)>::type>;"); 5314 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 5315 5316 // Not template parameters. 5317 verifyFormat("return a < b && c > d;"); 5318 verifyFormat("void f() {\n" 5319 " while (a < b && c > d) {\n" 5320 " }\n" 5321 "}"); 5322 verifyFormat("template <typename... Types>\n" 5323 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 5324 5325 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5326 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 5327 getLLVMStyleWithColumns(60)); 5328 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 5329 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 5330 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 5331 } 5332 5333 TEST_F(FormatTest, UnderstandsBinaryOperators) { 5334 verifyFormat("COMPARE(a, ==, b);"); 5335 } 5336 5337 TEST_F(FormatTest, UnderstandsPointersToMembers) { 5338 verifyFormat("int A::*x;"); 5339 verifyFormat("int (S::*func)(void *);"); 5340 verifyFormat("void f() { int (S::*func)(void *); }"); 5341 verifyFormat("typedef bool *(Class::*Member)() const;"); 5342 verifyFormat("void f() {\n" 5343 " (a->*f)();\n" 5344 " a->*x;\n" 5345 " (a.*f)();\n" 5346 " ((*a).*f)();\n" 5347 " a.*x;\n" 5348 "}"); 5349 verifyFormat("void f() {\n" 5350 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5351 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 5352 "}"); 5353 verifyFormat( 5354 "(aaaaaaaaaa->*bbbbbbb)(\n" 5355 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 5356 FormatStyle Style = getLLVMStyle(); 5357 Style.PointerAlignment = FormatStyle::PAS_Left; 5358 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 5359 } 5360 5361 TEST_F(FormatTest, UnderstandsUnaryOperators) { 5362 verifyFormat("int a = -2;"); 5363 verifyFormat("f(-1, -2, -3);"); 5364 verifyFormat("a[-1] = 5;"); 5365 verifyFormat("int a = 5 + -2;"); 5366 verifyFormat("if (i == -1) {\n}"); 5367 verifyFormat("if (i != -1) {\n}"); 5368 verifyFormat("if (i > -1) {\n}"); 5369 verifyFormat("if (i < -1) {\n}"); 5370 verifyFormat("++(a->f());"); 5371 verifyFormat("--(a->f());"); 5372 verifyFormat("(a->f())++;"); 5373 verifyFormat("a[42]++;"); 5374 verifyFormat("if (!(a->f())) {\n}"); 5375 5376 verifyFormat("a-- > b;"); 5377 verifyFormat("b ? -a : c;"); 5378 verifyFormat("n * sizeof char16;"); 5379 verifyFormat("n * alignof char16;", getGoogleStyle()); 5380 verifyFormat("sizeof(char);"); 5381 verifyFormat("alignof(char);", getGoogleStyle()); 5382 5383 verifyFormat("return -1;"); 5384 verifyFormat("switch (a) {\n" 5385 "case -1:\n" 5386 " break;\n" 5387 "}"); 5388 verifyFormat("#define X -1"); 5389 verifyFormat("#define X -kConstant"); 5390 5391 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 5392 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 5393 5394 verifyFormat("int a = /* confusing comment */ -1;"); 5395 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 5396 verifyFormat("int a = i /* confusing comment */++;"); 5397 } 5398 5399 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 5400 verifyFormat("if (!aaaaaaaaaa( // break\n" 5401 " aaaaa)) {\n" 5402 "}"); 5403 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 5404 " aaaaa));"); 5405 verifyFormat("*aaa = aaaaaaa( // break\n" 5406 " bbbbbb);"); 5407 } 5408 5409 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 5410 verifyFormat("bool operator<();"); 5411 verifyFormat("bool operator>();"); 5412 verifyFormat("bool operator=();"); 5413 verifyFormat("bool operator==();"); 5414 verifyFormat("bool operator!=();"); 5415 verifyFormat("int operator+();"); 5416 verifyFormat("int operator++();"); 5417 verifyFormat("bool operator();"); 5418 verifyFormat("bool operator()();"); 5419 verifyFormat("bool operator[]();"); 5420 verifyFormat("operator bool();"); 5421 verifyFormat("operator int();"); 5422 verifyFormat("operator void *();"); 5423 verifyFormat("operator SomeType<int>();"); 5424 verifyFormat("operator SomeType<int, int>();"); 5425 verifyFormat("operator SomeType<SomeType<int>>();"); 5426 verifyFormat("void *operator new(std::size_t size);"); 5427 verifyFormat("void *operator new[](std::size_t size);"); 5428 verifyFormat("void operator delete(void *ptr);"); 5429 verifyFormat("void operator delete[](void *ptr);"); 5430 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 5431 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 5432 5433 verifyFormat( 5434 "ostream &operator<<(ostream &OutputStream,\n" 5435 " SomeReallyLongType WithSomeReallyLongValue);"); 5436 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 5437 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 5438 " return left.group < right.group;\n" 5439 "}"); 5440 verifyFormat("SomeType &operator=(const SomeType &S);"); 5441 verifyFormat("f.template operator()<int>();"); 5442 5443 verifyGoogleFormat("operator void*();"); 5444 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 5445 verifyGoogleFormat("operator ::A();"); 5446 5447 verifyFormat("using A::operator+;"); 5448 5449 verifyFormat("string // break\n" 5450 "operator()() & {}"); 5451 verifyFormat("string // break\n" 5452 "operator()() && {}"); 5453 } 5454 5455 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 5456 verifyFormat("Deleted &operator=(const Deleted &)& = default;"); 5457 verifyFormat("Deleted &operator=(const Deleted &)&& = delete;"); 5458 verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;"); 5459 verifyFormat("SomeType MemberFunction(const Deleted &)&& = delete;"); 5460 verifyFormat("Deleted &operator=(const Deleted &)&;"); 5461 verifyFormat("Deleted &operator=(const Deleted &)&&;"); 5462 verifyFormat("SomeType MemberFunction(const Deleted &)&;"); 5463 verifyFormat("SomeType MemberFunction(const Deleted &)&&;"); 5464 5465 verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;"); 5466 verifyGoogleFormat("SomeType MemberFunction(const Deleted&)& = delete;"); 5467 verifyGoogleFormat("Deleted& operator=(const Deleted&)&;"); 5468 verifyGoogleFormat("SomeType MemberFunction(const Deleted&)&;"); 5469 5470 FormatStyle Spaces = getLLVMStyle(); 5471 Spaces.SpacesInCStyleCastParentheses = true; 5472 verifyFormat("Deleted &operator=(const Deleted &)& = default;", Spaces); 5473 verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;", Spaces); 5474 verifyFormat("Deleted &operator=(const Deleted &)&;", Spaces); 5475 verifyFormat("SomeType MemberFunction(const Deleted &)&;", Spaces); 5476 5477 Spaces.SpacesInCStyleCastParentheses = false; 5478 Spaces.SpacesInParentheses = true; 5479 verifyFormat("Deleted &operator=( const Deleted & )& = default;", Spaces); 5480 verifyFormat("SomeType MemberFunction( const Deleted & )& = delete;", Spaces); 5481 verifyFormat("Deleted &operator=( const Deleted & )&;", Spaces); 5482 verifyFormat("SomeType MemberFunction( const Deleted & )&;", Spaces); 5483 } 5484 5485 TEST_F(FormatTest, UnderstandsNewAndDelete) { 5486 verifyFormat("void f() {\n" 5487 " A *a = new A;\n" 5488 " A *a = new (placement) A;\n" 5489 " delete a;\n" 5490 " delete (A *)a;\n" 5491 "}"); 5492 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5493 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5494 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5495 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 5496 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 5497 verifyFormat("delete[] h->p;"); 5498 } 5499 5500 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 5501 verifyFormat("int *f(int *a) {}"); 5502 verifyFormat("int main(int argc, char **argv) {}"); 5503 verifyFormat("Test::Test(int b) : a(b * b) {}"); 5504 verifyIndependentOfContext("f(a, *a);"); 5505 verifyFormat("void g() { f(*a); }"); 5506 verifyIndependentOfContext("int a = b * 10;"); 5507 verifyIndependentOfContext("int a = 10 * b;"); 5508 verifyIndependentOfContext("int a = b * c;"); 5509 verifyIndependentOfContext("int a += b * c;"); 5510 verifyIndependentOfContext("int a -= b * c;"); 5511 verifyIndependentOfContext("int a *= b * c;"); 5512 verifyIndependentOfContext("int a /= b * c;"); 5513 verifyIndependentOfContext("int a = *b;"); 5514 verifyIndependentOfContext("int a = *b * c;"); 5515 verifyIndependentOfContext("int a = b * *c;"); 5516 verifyIndependentOfContext("int a = b * (10);"); 5517 verifyIndependentOfContext("S << b * (10);"); 5518 verifyIndependentOfContext("return 10 * b;"); 5519 verifyIndependentOfContext("return *b * *c;"); 5520 verifyIndependentOfContext("return a & ~b;"); 5521 verifyIndependentOfContext("f(b ? *c : *d);"); 5522 verifyIndependentOfContext("int a = b ? *c : *d;"); 5523 verifyIndependentOfContext("*b = a;"); 5524 verifyIndependentOfContext("a * ~b;"); 5525 verifyIndependentOfContext("a * !b;"); 5526 verifyIndependentOfContext("a * +b;"); 5527 verifyIndependentOfContext("a * -b;"); 5528 verifyIndependentOfContext("a * ++b;"); 5529 verifyIndependentOfContext("a * --b;"); 5530 verifyIndependentOfContext("a[4] * b;"); 5531 verifyIndependentOfContext("a[a * a] = 1;"); 5532 verifyIndependentOfContext("f() * b;"); 5533 verifyIndependentOfContext("a * [self dostuff];"); 5534 verifyIndependentOfContext("int x = a * (a + b);"); 5535 verifyIndependentOfContext("(a *)(a + b);"); 5536 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 5537 verifyIndependentOfContext("int *pa = (int *)&a;"); 5538 verifyIndependentOfContext("return sizeof(int **);"); 5539 verifyIndependentOfContext("return sizeof(int ******);"); 5540 verifyIndependentOfContext("return (int **&)a;"); 5541 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 5542 verifyFormat("void f(Type (*parameter)[10]) {}"); 5543 verifyGoogleFormat("return sizeof(int**);"); 5544 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 5545 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 5546 verifyFormat("auto a = [](int **&, int ***) {};"); 5547 verifyFormat("auto PointerBinding = [](const char *S) {};"); 5548 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 5549 verifyFormat("[](const decltype(*a) &value) {}"); 5550 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 5551 verifyIndependentOfContext("typedef void (*f)(int *a);"); 5552 verifyIndependentOfContext("int i{a * b};"); 5553 verifyIndependentOfContext("aaa && aaa->f();"); 5554 verifyIndependentOfContext("int x = ~*p;"); 5555 verifyFormat("Constructor() : a(a), area(width * height) {}"); 5556 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 5557 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 5558 verifyFormat("void f() { f(a, c * d); }"); 5559 verifyFormat("void f() { f(new a(), c * d); }"); 5560 5561 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 5562 5563 verifyIndependentOfContext("A<int *> a;"); 5564 verifyIndependentOfContext("A<int **> a;"); 5565 verifyIndependentOfContext("A<int *, int *> a;"); 5566 verifyIndependentOfContext("A<int *[]> a;"); 5567 verifyIndependentOfContext( 5568 "const char *const p = reinterpret_cast<const char *const>(q);"); 5569 verifyIndependentOfContext("A<int **, int **> a;"); 5570 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 5571 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 5572 verifyFormat("for (; a && b;) {\n}"); 5573 verifyFormat("bool foo = true && [] { return false; }();"); 5574 5575 verifyFormat( 5576 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5577 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5578 5579 verifyGoogleFormat("**outparam = 1;"); 5580 verifyGoogleFormat("*outparam = a * b;"); 5581 verifyGoogleFormat("int main(int argc, char** argv) {}"); 5582 verifyGoogleFormat("A<int*> a;"); 5583 verifyGoogleFormat("A<int**> a;"); 5584 verifyGoogleFormat("A<int*, int*> a;"); 5585 verifyGoogleFormat("A<int**, int**> a;"); 5586 verifyGoogleFormat("f(b ? *c : *d);"); 5587 verifyGoogleFormat("int a = b ? *c : *d;"); 5588 verifyGoogleFormat("Type* t = **x;"); 5589 verifyGoogleFormat("Type* t = *++*x;"); 5590 verifyGoogleFormat("*++*x;"); 5591 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 5592 verifyGoogleFormat("Type* t = x++ * y;"); 5593 verifyGoogleFormat( 5594 "const char* const p = reinterpret_cast<const char* const>(q);"); 5595 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 5596 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 5597 verifyGoogleFormat("template <typename T>\n" 5598 "void f(int i = 0, SomeType** temps = NULL);"); 5599 5600 FormatStyle Left = getLLVMStyle(); 5601 Left.PointerAlignment = FormatStyle::PAS_Left; 5602 verifyFormat("x = *a(x) = *a(y);", Left); 5603 5604 verifyIndependentOfContext("a = *(x + y);"); 5605 verifyIndependentOfContext("a = &(x + y);"); 5606 verifyIndependentOfContext("*(x + y).call();"); 5607 verifyIndependentOfContext("&(x + y)->call();"); 5608 verifyFormat("void f() { &(*I).first; }"); 5609 5610 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 5611 verifyFormat( 5612 "int *MyValues = {\n" 5613 " *A, // Operator detection might be confused by the '{'\n" 5614 " *BB // Operator detection might be confused by previous comment\n" 5615 "};"); 5616 5617 verifyIndependentOfContext("if (int *a = &b)"); 5618 verifyIndependentOfContext("if (int &a = *b)"); 5619 verifyIndependentOfContext("if (a & b[i])"); 5620 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 5621 verifyIndependentOfContext("if (*b[i])"); 5622 verifyIndependentOfContext("if (int *a = (&b))"); 5623 verifyIndependentOfContext("while (int *a = &b)"); 5624 verifyIndependentOfContext("size = sizeof *a;"); 5625 verifyIndependentOfContext("if (a && (b = c))"); 5626 verifyFormat("void f() {\n" 5627 " for (const int &v : Values) {\n" 5628 " }\n" 5629 "}"); 5630 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 5631 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 5632 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 5633 5634 verifyFormat("#define A (!a * b)"); 5635 verifyFormat("#define MACRO \\\n" 5636 " int *i = a * b; \\\n" 5637 " void f(a *b);", 5638 getLLVMStyleWithColumns(19)); 5639 5640 verifyIndependentOfContext("A = new SomeType *[Length];"); 5641 verifyIndependentOfContext("A = new SomeType *[Length]();"); 5642 verifyIndependentOfContext("T **t = new T *;"); 5643 verifyIndependentOfContext("T **t = new T *();"); 5644 verifyGoogleFormat("A = new SomeType*[Length]();"); 5645 verifyGoogleFormat("A = new SomeType*[Length];"); 5646 verifyGoogleFormat("T** t = new T*;"); 5647 verifyGoogleFormat("T** t = new T*();"); 5648 5649 FormatStyle PointerLeft = getLLVMStyle(); 5650 PointerLeft.PointerAlignment = FormatStyle::PAS_Left; 5651 verifyFormat("delete *x;", PointerLeft); 5652 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 5653 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 5654 verifyFormat("template <bool a, bool b> " 5655 "typename t::if<x && y>::type f() {}"); 5656 verifyFormat("template <int *y> f() {}"); 5657 verifyFormat("vector<int *> v;"); 5658 verifyFormat("vector<int *const> v;"); 5659 verifyFormat("vector<int *const **const *> v;"); 5660 verifyFormat("vector<int *volatile> v;"); 5661 verifyFormat("vector<a * b> v;"); 5662 verifyFormat("foo<b && false>();"); 5663 verifyFormat("foo<b & 1>();"); 5664 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 5665 verifyFormat( 5666 "template <class T, class = typename std::enable_if<\n" 5667 " std::is_integral<T>::value &&\n" 5668 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 5669 "void F();", 5670 getLLVMStyleWithColumns(76)); 5671 verifyFormat( 5672 "template <class T,\n" 5673 " class = typename ::std::enable_if<\n" 5674 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 5675 "void F();", 5676 getGoogleStyleWithColumns(68)); 5677 5678 verifyIndependentOfContext("MACRO(int *i);"); 5679 verifyIndependentOfContext("MACRO(auto *a);"); 5680 verifyIndependentOfContext("MACRO(const A *a);"); 5681 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 5682 // FIXME: Is there a way to make this work? 5683 // verifyIndependentOfContext("MACRO(A *a);"); 5684 5685 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 5686 5687 EXPECT_EQ("#define OP(x) \\\n" 5688 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5689 " return s << a.DebugString(); \\\n" 5690 " }", 5691 format("#define OP(x) \\\n" 5692 " ostream &operator<<(ostream &s, const A &a) { \\\n" 5693 " return s << a.DebugString(); \\\n" 5694 " }", 5695 getLLVMStyleWithColumns(50))); 5696 5697 // FIXME: We cannot handle this case yet; we might be able to figure out that 5698 // foo<x> d > v; doesn't make sense. 5699 verifyFormat("foo<a<b && c> d> v;"); 5700 5701 FormatStyle PointerMiddle = getLLVMStyle(); 5702 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 5703 verifyFormat("delete *x;", PointerMiddle); 5704 verifyFormat("int * x;", PointerMiddle); 5705 verifyFormat("template <int * y> f() {}", PointerMiddle); 5706 verifyFormat("int * f(int * a) {}", PointerMiddle); 5707 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 5708 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 5709 verifyFormat("A<int *> a;", PointerMiddle); 5710 verifyFormat("A<int **> a;", PointerMiddle); 5711 verifyFormat("A<int *, int *> a;", PointerMiddle); 5712 verifyFormat("A<int * []> a;", PointerMiddle); 5713 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 5714 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 5715 verifyFormat("T ** t = new T *;", PointerMiddle); 5716 } 5717 5718 TEST_F(FormatTest, UnderstandsAttributes) { 5719 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 5720 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 5721 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 5722 } 5723 5724 TEST_F(FormatTest, UnderstandsEllipsis) { 5725 verifyFormat("int printf(const char *fmt, ...);"); 5726 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 5727 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}"); 5728 5729 FormatStyle PointersLeft = getLLVMStyle(); 5730 PointersLeft.PointerAlignment = FormatStyle::PAS_Left; 5731 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft); 5732 } 5733 5734 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 5735 EXPECT_EQ("int *a;\n" 5736 "int *a;\n" 5737 "int *a;", 5738 format("int *a;\n" 5739 "int* a;\n" 5740 "int *a;", 5741 getGoogleStyle())); 5742 EXPECT_EQ("int* a;\n" 5743 "int* a;\n" 5744 "int* a;", 5745 format("int* a;\n" 5746 "int* a;\n" 5747 "int *a;", 5748 getGoogleStyle())); 5749 EXPECT_EQ("int *a;\n" 5750 "int *a;\n" 5751 "int *a;", 5752 format("int *a;\n" 5753 "int * a;\n" 5754 "int * a;", 5755 getGoogleStyle())); 5756 } 5757 5758 TEST_F(FormatTest, UnderstandsRvalueReferences) { 5759 verifyFormat("int f(int &&a) {}"); 5760 verifyFormat("int f(int a, char &&b) {}"); 5761 verifyFormat("void f() { int &&a = b; }"); 5762 verifyGoogleFormat("int f(int a, char&& b) {}"); 5763 verifyGoogleFormat("void f() { int&& a = b; }"); 5764 5765 verifyIndependentOfContext("A<int &&> a;"); 5766 verifyIndependentOfContext("A<int &&, int &&> a;"); 5767 verifyGoogleFormat("A<int&&> a;"); 5768 verifyGoogleFormat("A<int&&, int&&> a;"); 5769 5770 // Not rvalue references: 5771 verifyFormat("template <bool B, bool C> class A {\n" 5772 " static_assert(B && C, \"Something is wrong\");\n" 5773 "};"); 5774 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 5775 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 5776 verifyFormat("#define A(a, b) (a && b)"); 5777 } 5778 5779 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 5780 verifyFormat("void f() {\n" 5781 " x[aaaaaaaaa -\n" 5782 " b] = 23;\n" 5783 "}", 5784 getLLVMStyleWithColumns(15)); 5785 } 5786 5787 TEST_F(FormatTest, FormatsCasts) { 5788 verifyFormat("Type *A = static_cast<Type *>(P);"); 5789 verifyFormat("Type *A = (Type *)P;"); 5790 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 5791 verifyFormat("int a = (int)(2.0f);"); 5792 verifyFormat("int a = (int)2.0f;"); 5793 verifyFormat("x[(int32)y];"); 5794 verifyFormat("x = (int32)y;"); 5795 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 5796 verifyFormat("int a = (int)*b;"); 5797 verifyFormat("int a = (int)2.0f;"); 5798 verifyFormat("int a = (int)~0;"); 5799 verifyFormat("int a = (int)++a;"); 5800 verifyFormat("int a = (int)sizeof(int);"); 5801 verifyFormat("int a = (int)+2;"); 5802 verifyFormat("my_int a = (my_int)2.0f;"); 5803 verifyFormat("my_int a = (my_int)sizeof(int);"); 5804 verifyFormat("return (my_int)aaa;"); 5805 verifyFormat("#define x ((int)-1)"); 5806 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 5807 verifyFormat("#define p(q) ((int *)&q)"); 5808 verifyFormat("fn(a)(b) + 1;"); 5809 5810 verifyFormat("void f() { my_int a = (my_int)*b; }"); 5811 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 5812 verifyFormat("my_int a = (my_int)~0;"); 5813 verifyFormat("my_int a = (my_int)++a;"); 5814 verifyFormat("my_int a = (my_int)-2;"); 5815 verifyFormat("my_int a = (my_int)1;"); 5816 verifyFormat("my_int a = (my_int *)1;"); 5817 verifyFormat("my_int a = (const my_int)-1;"); 5818 verifyFormat("my_int a = (const my_int *)-1;"); 5819 verifyFormat("my_int a = (my_int)(my_int)-1;"); 5820 verifyFormat("my_int a = (ns::my_int)-2;"); 5821 verifyFormat("case (my_int)ONE:"); 5822 5823 // FIXME: single value wrapped with paren will be treated as cast. 5824 verifyFormat("void f(int i = (kValue)*kMask) {}"); 5825 5826 verifyFormat("{ (void)F; }"); 5827 5828 // Don't break after a cast's 5829 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5830 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 5831 " bbbbbbbbbbbbbbbbbbbbbb);"); 5832 5833 // These are not casts. 5834 verifyFormat("void f(int *) {}"); 5835 verifyFormat("f(foo)->b;"); 5836 verifyFormat("f(foo).b;"); 5837 verifyFormat("f(foo)(b);"); 5838 verifyFormat("f(foo)[b];"); 5839 verifyFormat("[](foo) { return 4; }(bar);"); 5840 verifyFormat("(*funptr)(foo)[4];"); 5841 verifyFormat("funptrs[4](foo)[4];"); 5842 verifyFormat("void f(int *);"); 5843 verifyFormat("void f(int *) = 0;"); 5844 verifyFormat("void f(SmallVector<int>) {}"); 5845 verifyFormat("void f(SmallVector<int>);"); 5846 verifyFormat("void f(SmallVector<int>) = 0;"); 5847 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 5848 verifyFormat("int a = sizeof(int) * b;"); 5849 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 5850 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 5851 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 5852 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 5853 5854 // These are not casts, but at some point were confused with casts. 5855 verifyFormat("virtual void foo(int *) override;"); 5856 verifyFormat("virtual void foo(char &) const;"); 5857 verifyFormat("virtual void foo(int *a, char *) const;"); 5858 verifyFormat("int a = sizeof(int *) + b;"); 5859 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 5860 5861 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 5862 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 5863 // FIXME: The indentation here is not ideal. 5864 verifyFormat( 5865 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5866 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 5867 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 5868 } 5869 5870 TEST_F(FormatTest, FormatsFunctionTypes) { 5871 verifyFormat("A<bool()> a;"); 5872 verifyFormat("A<SomeType()> a;"); 5873 verifyFormat("A<void (*)(int, std::string)> a;"); 5874 verifyFormat("A<void *(int)>;"); 5875 verifyFormat("void *(*a)(int *, SomeType *);"); 5876 verifyFormat("int (*func)(void *);"); 5877 verifyFormat("void f() { int (*func)(void *); }"); 5878 verifyFormat("template <class CallbackClass>\n" 5879 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 5880 5881 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 5882 verifyGoogleFormat("void* (*a)(int);"); 5883 verifyGoogleFormat( 5884 "template <class CallbackClass>\n" 5885 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 5886 5887 // Other constructs can look somewhat like function types: 5888 verifyFormat("A<sizeof(*x)> a;"); 5889 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 5890 verifyFormat("some_var = function(*some_pointer_var)[0];"); 5891 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 5892 } 5893 5894 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 5895 verifyFormat("A (*foo_)[6];"); 5896 verifyFormat("vector<int> (*foo_)[6];"); 5897 } 5898 5899 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 5900 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5901 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5902 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 5903 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5904 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5905 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 5906 5907 // Different ways of ()-initializiation. 5908 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5909 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 5910 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5911 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 5912 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5913 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 5914 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 5915 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 5916 } 5917 5918 TEST_F(FormatTest, BreaksLongDeclarations) { 5919 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 5920 " AnotherNameForTheLongType;"); 5921 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 5922 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 5923 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5924 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5925 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 5926 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 5927 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5928 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5929 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 5930 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5931 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5932 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5933 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5934 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 5935 FormatStyle Indented = getLLVMStyle(); 5936 Indented.IndentWrappedFunctionNames = true; 5937 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5938 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 5939 Indented); 5940 verifyFormat( 5941 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 5942 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5943 Indented); 5944 verifyFormat( 5945 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 5946 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5947 Indented); 5948 verifyFormat( 5949 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 5950 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 5951 Indented); 5952 5953 // FIXME: Without the comment, this breaks after "(". 5954 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 5955 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 5956 getGoogleStyle()); 5957 5958 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 5959 " int LoooooooooooooooooooongParam2) {}"); 5960 verifyFormat( 5961 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 5962 " SourceLocation L, IdentifierIn *II,\n" 5963 " Type *T) {}"); 5964 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 5965 "ReallyReaaallyLongFunctionName(\n" 5966 " const std::string &SomeParameter,\n" 5967 " const SomeType<string, SomeOtherTemplateParameter>\n" 5968 " &ReallyReallyLongParameterName,\n" 5969 " const SomeType<string, SomeOtherTemplateParameter>\n" 5970 " &AnotherLongParameterName) {}"); 5971 verifyFormat("template <typename A>\n" 5972 "SomeLoooooooooooooooooooooongType<\n" 5973 " typename some_namespace::SomeOtherType<A>::Type>\n" 5974 "Function() {}"); 5975 5976 verifyGoogleFormat( 5977 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 5978 " aaaaaaaaaaaaaaaaaaaaaaa;"); 5979 verifyGoogleFormat( 5980 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 5981 " SourceLocation L) {}"); 5982 verifyGoogleFormat( 5983 "some_namespace::LongReturnType\n" 5984 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 5985 " int first_long_parameter, int second_parameter) {}"); 5986 5987 verifyGoogleFormat("template <typename T>\n" 5988 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5989 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 5990 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5991 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 5992 5993 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 5994 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5995 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 5996 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5997 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5998 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5999 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6000 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6001 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 6002 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6003 } 6004 6005 TEST_F(FormatTest, FormatsArrays) { 6006 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6007 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 6008 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6009 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6010 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6011 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 6012 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6013 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6014 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 6015 verifyFormat( 6016 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 6017 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 6018 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 6019 6020 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 6021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 6022 verifyFormat( 6023 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 6024 " .aaaaaaa[0]\n" 6025 " .aaaaaaaaaaaaaaaaaaaaaa();"); 6026 6027 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 6028 } 6029 6030 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 6031 verifyFormat("(a)->b();"); 6032 verifyFormat("--a;"); 6033 } 6034 6035 TEST_F(FormatTest, HandlesIncludeDirectives) { 6036 verifyFormat("#include <string>\n" 6037 "#include <a/b/c.h>\n" 6038 "#include \"a/b/string\"\n" 6039 "#include \"string.h\"\n" 6040 "#include \"string.h\"\n" 6041 "#include <a-a>\n" 6042 "#include < path with space >\n" 6043 "#include_next <test.h>" 6044 "#include \"abc.h\" // this is included for ABC\n" 6045 "#include \"some long include\" // with a comment\n" 6046 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", 6047 getLLVMStyleWithColumns(35)); 6048 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 6049 EXPECT_EQ("#include <a>", format("#include<a>")); 6050 6051 verifyFormat("#import <string>"); 6052 verifyFormat("#import <a/b/c.h>"); 6053 verifyFormat("#import \"a/b/string\""); 6054 verifyFormat("#import \"string.h\""); 6055 verifyFormat("#import \"string.h\""); 6056 verifyFormat("#if __has_include(<strstream>)\n" 6057 "#include <strstream>\n" 6058 "#endif"); 6059 6060 verifyFormat("#define MY_IMPORT <a/b>"); 6061 6062 // Protocol buffer definition or missing "#". 6063 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 6064 getLLVMStyleWithColumns(30)); 6065 6066 FormatStyle Style = getLLVMStyle(); 6067 Style.AlwaysBreakBeforeMultilineStrings = true; 6068 Style.ColumnLimit = 0; 6069 verifyFormat("#import \"abc.h\"", Style); 6070 6071 // But 'import' might also be a regular C++ namespace. 6072 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6073 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6074 } 6075 6076 //===----------------------------------------------------------------------===// 6077 // Error recovery tests. 6078 //===----------------------------------------------------------------------===// 6079 6080 TEST_F(FormatTest, IncompleteParameterLists) { 6081 FormatStyle NoBinPacking = getLLVMStyle(); 6082 NoBinPacking.BinPackParameters = false; 6083 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 6084 " double *min_x,\n" 6085 " double *max_x,\n" 6086 " double *min_y,\n" 6087 " double *max_y,\n" 6088 " double *min_z,\n" 6089 " double *max_z, ) {}", 6090 NoBinPacking); 6091 } 6092 6093 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 6094 verifyFormat("void f() { return; }\n42"); 6095 verifyFormat("void f() {\n" 6096 " if (0)\n" 6097 " return;\n" 6098 "}\n" 6099 "42"); 6100 verifyFormat("void f() { return }\n42"); 6101 verifyFormat("void f() {\n" 6102 " if (0)\n" 6103 " return\n" 6104 "}\n" 6105 "42"); 6106 } 6107 6108 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 6109 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 6110 EXPECT_EQ("void f() {\n" 6111 " if (a)\n" 6112 " return\n" 6113 "}", 6114 format("void f ( ) { if ( a ) return }")); 6115 EXPECT_EQ("namespace N {\n" 6116 "void f()\n" 6117 "}", 6118 format("namespace N { void f() }")); 6119 EXPECT_EQ("namespace N {\n" 6120 "void f() {}\n" 6121 "void g()\n" 6122 "}", 6123 format("namespace N { void f( ) { } void g( ) }")); 6124 } 6125 6126 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 6127 verifyFormat("int aaaaaaaa =\n" 6128 " // Overlylongcomment\n" 6129 " b;", 6130 getLLVMStyleWithColumns(20)); 6131 verifyFormat("function(\n" 6132 " ShortArgument,\n" 6133 " LoooooooooooongArgument);\n", 6134 getLLVMStyleWithColumns(20)); 6135 } 6136 6137 TEST_F(FormatTest, IncorrectAccessSpecifier) { 6138 verifyFormat("public:"); 6139 verifyFormat("class A {\n" 6140 "public\n" 6141 " void f() {}\n" 6142 "};"); 6143 verifyFormat("public\n" 6144 "int qwerty;"); 6145 verifyFormat("public\n" 6146 "B {}"); 6147 verifyFormat("public\n" 6148 "{}"); 6149 verifyFormat("public\n" 6150 "B { int x; }"); 6151 } 6152 6153 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 6154 verifyFormat("{"); 6155 verifyFormat("#})"); 6156 verifyNoCrash("(/**/[:!] ?[)."); 6157 } 6158 6159 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 6160 verifyFormat("do {\n}"); 6161 verifyFormat("do {\n}\n" 6162 "f();"); 6163 verifyFormat("do {\n}\n" 6164 "wheeee(fun);"); 6165 verifyFormat("do {\n" 6166 " f();\n" 6167 "}"); 6168 } 6169 6170 TEST_F(FormatTest, IncorrectCodeMissingParens) { 6171 verifyFormat("if {\n foo;\n foo();\n}"); 6172 verifyFormat("switch {\n foo;\n foo();\n}"); 6173 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 6174 verifyFormat("while {\n foo;\n foo();\n}"); 6175 verifyFormat("do {\n foo;\n foo();\n} while;"); 6176 } 6177 6178 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 6179 verifyIncompleteFormat("namespace {\n" 6180 "class Foo { Foo (\n" 6181 "};\n" 6182 "} // comment"); 6183 } 6184 6185 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 6186 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 6187 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 6188 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 6189 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 6190 6191 EXPECT_EQ("{\n" 6192 " {\n" 6193 " breakme(\n" 6194 " qwe);\n" 6195 " }\n", 6196 format("{\n" 6197 " {\n" 6198 " breakme(qwe);\n" 6199 "}\n", 6200 getLLVMStyleWithColumns(10))); 6201 } 6202 6203 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 6204 verifyFormat("int x = {\n" 6205 " avariable,\n" 6206 " b(alongervariable)};", 6207 getLLVMStyleWithColumns(25)); 6208 } 6209 6210 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 6211 verifyFormat("return (a)(b){1, 2, 3};"); 6212 } 6213 6214 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 6215 verifyFormat("vector<int> x{1, 2, 3, 4};"); 6216 verifyFormat("vector<int> x{\n" 6217 " 1, 2, 3, 4,\n" 6218 "};"); 6219 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 6220 verifyFormat("f({1, 2});"); 6221 verifyFormat("auto v = Foo{-1};"); 6222 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 6223 verifyFormat("Class::Class : member{1, 2, 3} {}"); 6224 verifyFormat("new vector<int>{1, 2, 3};"); 6225 verifyFormat("new int[3]{1, 2, 3};"); 6226 verifyFormat("new int{1};"); 6227 verifyFormat("return {arg1, arg2};"); 6228 verifyFormat("return {arg1, SomeType{parameter}};"); 6229 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 6230 verifyFormat("new T{arg1, arg2};"); 6231 verifyFormat("f(MyMap[{composite, key}]);"); 6232 verifyFormat("class Class {\n" 6233 " T member = {arg1, arg2};\n" 6234 "};"); 6235 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 6236 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 6237 verifyFormat("int a = std::is_integral<int>{} + 0;"); 6238 6239 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6240 verifyFormat("int foo(int i) { return fo1{}(i); }"); 6241 verifyFormat("auto i = decltype(x){};"); 6242 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 6243 verifyFormat("Node n{1, Node{1000}, //\n" 6244 " 2};"); 6245 verifyFormat("Aaaa aaaaaaa{\n" 6246 " {\n" 6247 " aaaa,\n" 6248 " },\n" 6249 "};"); 6250 verifyFormat("class C : public D {\n" 6251 " SomeClass SC{2};\n" 6252 "};"); 6253 verifyFormat("class C : public A {\n" 6254 " class D : public B {\n" 6255 " void f() { int i{2}; }\n" 6256 " };\n" 6257 "};"); 6258 6259 // In combination with BinPackArguments = false. 6260 FormatStyle NoBinPacking = getLLVMStyle(); 6261 NoBinPacking.BinPackArguments = false; 6262 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 6263 " bbbbb,\n" 6264 " ccccc,\n" 6265 " ddddd,\n" 6266 " eeeee,\n" 6267 " ffffff,\n" 6268 " ggggg,\n" 6269 " hhhhhh,\n" 6270 " iiiiii,\n" 6271 " jjjjjj,\n" 6272 " kkkkkk};", 6273 NoBinPacking); 6274 verifyFormat("const Aaaaaa aaaaa = {\n" 6275 " aaaaa,\n" 6276 " bbbbb,\n" 6277 " ccccc,\n" 6278 " ddddd,\n" 6279 " eeeee,\n" 6280 " ffffff,\n" 6281 " ggggg,\n" 6282 " hhhhhh,\n" 6283 " iiiiii,\n" 6284 " jjjjjj,\n" 6285 " kkkkkk,\n" 6286 "};", 6287 NoBinPacking); 6288 verifyFormat( 6289 "const Aaaaaa aaaaa = {\n" 6290 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 6291 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 6292 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 6293 "};", 6294 NoBinPacking); 6295 6296 // FIXME: The alignment of these trailing comments might be bad. Then again, 6297 // this might be utterly useless in real code. 6298 verifyFormat("Constructor::Constructor()\n" 6299 " : some_value{ //\n" 6300 " aaaaaaa, //\n" 6301 " bbbbbbb} {}"); 6302 6303 // In braced lists, the first comment is always assumed to belong to the 6304 // first element. Thus, it can be moved to the next or previous line as 6305 // appropriate. 6306 EXPECT_EQ("function({// First element:\n" 6307 " 1,\n" 6308 " // Second element:\n" 6309 " 2});", 6310 format("function({\n" 6311 " // First element:\n" 6312 " 1,\n" 6313 " // Second element:\n" 6314 " 2});")); 6315 EXPECT_EQ("std::vector<int> MyNumbers{\n" 6316 " // First element:\n" 6317 " 1,\n" 6318 " // Second element:\n" 6319 " 2};", 6320 format("std::vector<int> MyNumbers{// First element:\n" 6321 " 1,\n" 6322 " // Second element:\n" 6323 " 2};", 6324 getLLVMStyleWithColumns(30))); 6325 // A trailing comma should still lead to an enforced line break. 6326 EXPECT_EQ("vector<int> SomeVector = {\n" 6327 " // aaa\n" 6328 " 1, 2,\n" 6329 "};", 6330 format("vector<int> SomeVector = { // aaa\n" 6331 " 1, 2, };")); 6332 6333 FormatStyle ExtraSpaces = getLLVMStyle(); 6334 ExtraSpaces.Cpp11BracedListStyle = false; 6335 ExtraSpaces.ColumnLimit = 75; 6336 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 6337 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 6338 verifyFormat("f({ 1, 2 });", ExtraSpaces); 6339 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 6340 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 6341 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 6342 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 6343 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 6344 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 6345 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 6346 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 6347 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 6348 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 6349 verifyFormat("class Class {\n" 6350 " T member = { arg1, arg2 };\n" 6351 "};", 6352 ExtraSpaces); 6353 verifyFormat( 6354 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6355 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 6356 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 6357 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 6358 ExtraSpaces); 6359 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 6360 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 6361 ExtraSpaces); 6362 verifyFormat( 6363 "someFunction(OtherParam,\n" 6364 " BracedList{ // comment 1 (Forcing interesting break)\n" 6365 " param1, param2,\n" 6366 " // comment 2\n" 6367 " param3, param4 });", 6368 ExtraSpaces); 6369 verifyFormat( 6370 "std::this_thread::sleep_for(\n" 6371 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 6372 ExtraSpaces); 6373 verifyFormat( 6374 "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n" 6375 " aaaaaaa,\n" 6376 " aaaaaaaaaa,\n" 6377 " aaaaa,\n" 6378 " aaaaaaaaaaaaaaa,\n" 6379 " aaa,\n" 6380 " aaaaaaaaaa,\n" 6381 " a,\n" 6382 " aaaaaaaaaaaaaaaaaaaaa,\n" 6383 " aaaaaaaaaaaa,\n" 6384 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 6385 " aaaaaaa,\n" 6386 " a};"); 6387 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 6388 } 6389 6390 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 6391 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6392 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6393 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6394 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6395 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6396 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6397 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 6398 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6399 " 1, 22, 333, 4444, 55555, //\n" 6400 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6401 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 6402 verifyFormat( 6403 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6404 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 6405 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 6406 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6407 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6408 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 6409 " 7777777};"); 6410 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6411 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6412 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6413 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6414 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6415 " // Separating comment.\n" 6416 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6417 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 6418 " // Leading comment\n" 6419 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 6420 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 6421 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6422 " 1, 1, 1, 1};", 6423 getLLVMStyleWithColumns(39)); 6424 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6425 " 1, 1, 1, 1};", 6426 getLLVMStyleWithColumns(38)); 6427 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 6428 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 6429 getLLVMStyleWithColumns(43)); 6430 verifyFormat( 6431 "static unsigned SomeValues[10][3] = {\n" 6432 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 6433 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 6434 verifyFormat("static auto fields = new vector<string>{\n" 6435 " \"aaaaaaaaaaaaa\",\n" 6436 " \"aaaaaaaaaaaaa\",\n" 6437 " \"aaaaaaaaaaaa\",\n" 6438 " \"aaaaaaaaaaaaaa\",\n" 6439 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6440 " \"aaaaaaaaaaaa\",\n" 6441 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 6442 "};"); 6443 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 6444 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 6445 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 6446 " 3, cccccccccccccccccccccc};", 6447 getLLVMStyleWithColumns(60)); 6448 6449 // Trailing commas. 6450 verifyFormat("vector<int> x = {\n" 6451 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 6452 "};", 6453 getLLVMStyleWithColumns(39)); 6454 verifyFormat("vector<int> x = {\n" 6455 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 6456 "};", 6457 getLLVMStyleWithColumns(39)); 6458 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 6459 " 1, 1, 1, 1,\n" 6460 " /**/ /**/};", 6461 getLLVMStyleWithColumns(39)); 6462 6463 // Trailing comment in the first line. 6464 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 6465 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 6466 " 111111111, 222222222, 3333333333, 444444444, //\n" 6467 " 11111111, 22222222, 333333333, 44444444};"); 6468 6469 // With nested lists, we should either format one item per line or all nested 6470 // lists one on line. 6471 // FIXME: For some nested lists, we can do better. 6472 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 6473 " {aaaaaaaaaaaaaaaaaaa},\n" 6474 " {aaaaaaaaaaaaaaaaaaaaa},\n" 6475 " {aaaaaaaaaaaaaaaaa}};", 6476 getLLVMStyleWithColumns(60)); 6477 verifyFormat( 6478 "SomeStruct my_struct_array = {\n" 6479 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 6480 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 6481 " {aaa, aaa},\n" 6482 " {aaa, aaa},\n" 6483 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 6484 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6485 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 6486 6487 // No column layout should be used here. 6488 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 6489 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 6490 6491 verifyNoCrash("a<,"); 6492 } 6493 6494 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 6495 FormatStyle DoNotMerge = getLLVMStyle(); 6496 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6497 6498 verifyFormat("void f() { return 42; }"); 6499 verifyFormat("void f() {\n" 6500 " return 42;\n" 6501 "}", 6502 DoNotMerge); 6503 verifyFormat("void f() {\n" 6504 " // Comment\n" 6505 "}"); 6506 verifyFormat("{\n" 6507 "#error {\n" 6508 " int a;\n" 6509 "}"); 6510 verifyFormat("{\n" 6511 " int a;\n" 6512 "#error {\n" 6513 "}"); 6514 verifyFormat("void f() {} // comment"); 6515 verifyFormat("void f() { int a; } // comment"); 6516 verifyFormat("void f() {\n" 6517 "} // comment", 6518 DoNotMerge); 6519 verifyFormat("void f() {\n" 6520 " int a;\n" 6521 "} // comment", 6522 DoNotMerge); 6523 verifyFormat("void f() {\n" 6524 "} // comment", 6525 getLLVMStyleWithColumns(15)); 6526 6527 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 6528 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 6529 6530 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 6531 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 6532 verifyFormat("class C {\n" 6533 " C()\n" 6534 " : iiiiiiii(nullptr),\n" 6535 " kkkkkkk(nullptr),\n" 6536 " mmmmmmm(nullptr),\n" 6537 " nnnnnnn(nullptr) {}\n" 6538 "};", 6539 getGoogleStyle()); 6540 6541 FormatStyle NoColumnLimit = getLLVMStyle(); 6542 NoColumnLimit.ColumnLimit = 0; 6543 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 6544 EXPECT_EQ("class C {\n" 6545 " A() : b(0) {}\n" 6546 "};", 6547 format("class C{A():b(0){}};", NoColumnLimit)); 6548 EXPECT_EQ("A()\n" 6549 " : b(0) {\n" 6550 "}", 6551 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 6552 6553 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 6554 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 6555 FormatStyle::SFS_None; 6556 EXPECT_EQ("A()\n" 6557 " : b(0) {\n" 6558 "}", 6559 format("A():b(0){}", DoNotMergeNoColumnLimit)); 6560 EXPECT_EQ("A()\n" 6561 " : b(0) {\n" 6562 "}", 6563 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 6564 6565 verifyFormat("#define A \\\n" 6566 " void f() { \\\n" 6567 " int i; \\\n" 6568 " }", 6569 getLLVMStyleWithColumns(20)); 6570 verifyFormat("#define A \\\n" 6571 " void f() { int i; }", 6572 getLLVMStyleWithColumns(21)); 6573 verifyFormat("#define A \\\n" 6574 " void f() { \\\n" 6575 " int i; \\\n" 6576 " } \\\n" 6577 " int j;", 6578 getLLVMStyleWithColumns(22)); 6579 verifyFormat("#define A \\\n" 6580 " void f() { int i; } \\\n" 6581 " int j;", 6582 getLLVMStyleWithColumns(23)); 6583 } 6584 6585 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 6586 FormatStyle MergeInlineOnly = getLLVMStyle(); 6587 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 6588 verifyFormat("class C {\n" 6589 " int f() { return 42; }\n" 6590 "};", 6591 MergeInlineOnly); 6592 verifyFormat("int f() {\n" 6593 " return 42;\n" 6594 "}", 6595 MergeInlineOnly); 6596 } 6597 6598 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 6599 // Elaborate type variable declarations. 6600 verifyFormat("struct foo a = {bar};\nint n;"); 6601 verifyFormat("class foo a = {bar};\nint n;"); 6602 verifyFormat("union foo a = {bar};\nint n;"); 6603 6604 // Elaborate types inside function definitions. 6605 verifyFormat("struct foo f() {}\nint n;"); 6606 verifyFormat("class foo f() {}\nint n;"); 6607 verifyFormat("union foo f() {}\nint n;"); 6608 6609 // Templates. 6610 verifyFormat("template <class X> void f() {}\nint n;"); 6611 verifyFormat("template <struct X> void f() {}\nint n;"); 6612 verifyFormat("template <union X> void f() {}\nint n;"); 6613 6614 // Actual definitions... 6615 verifyFormat("struct {\n} n;"); 6616 verifyFormat( 6617 "template <template <class T, class Y>, class Z> class X {\n} n;"); 6618 verifyFormat("union Z {\n int n;\n} x;"); 6619 verifyFormat("class MACRO Z {\n} n;"); 6620 verifyFormat("class MACRO(X) Z {\n} n;"); 6621 verifyFormat("class __attribute__(X) Z {\n} n;"); 6622 verifyFormat("class __declspec(X) Z {\n} n;"); 6623 verifyFormat("class A##B##C {\n} n;"); 6624 verifyFormat("class alignas(16) Z {\n} n;"); 6625 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 6626 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 6627 6628 // Redefinition from nested context: 6629 verifyFormat("class A::B::C {\n} n;"); 6630 6631 // Template definitions. 6632 verifyFormat( 6633 "template <typename F>\n" 6634 "Matcher(const Matcher<F> &Other,\n" 6635 " typename enable_if_c<is_base_of<F, T>::value &&\n" 6636 " !is_same<F, T>::value>::type * = 0)\n" 6637 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 6638 6639 // FIXME: This is still incorrectly handled at the formatter side. 6640 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 6641 verifyFormat("int i = SomeFunction(a<b, a> b);"); 6642 6643 // FIXME: 6644 // This now gets parsed incorrectly as class definition. 6645 // verifyFormat("class A<int> f() {\n}\nint n;"); 6646 6647 // Elaborate types where incorrectly parsing the structural element would 6648 // break the indent. 6649 verifyFormat("if (true)\n" 6650 " class X x;\n" 6651 "else\n" 6652 " f();\n"); 6653 6654 // This is simply incomplete. Formatting is not important, but must not crash. 6655 verifyFormat("class A:"); 6656 } 6657 6658 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 6659 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 6660 format("#error Leave all white!!!!! space* alone!\n")); 6661 EXPECT_EQ( 6662 "#warning Leave all white!!!!! space* alone!\n", 6663 format("#warning Leave all white!!!!! space* alone!\n")); 6664 EXPECT_EQ("#error 1", format(" # error 1")); 6665 EXPECT_EQ("#warning 1", format(" # warning 1")); 6666 } 6667 6668 TEST_F(FormatTest, FormatHashIfExpressions) { 6669 verifyFormat("#if AAAA && BBBB"); 6670 // FIXME: Come up with a better indentation for #elif. 6671 verifyFormat( 6672 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 6673 " defined(BBBBBBBB)\n" 6674 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 6675 " defined(BBBBBBBB)\n" 6676 "#endif", 6677 getLLVMStyleWithColumns(65)); 6678 } 6679 6680 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 6681 FormatStyle AllowsMergedIf = getGoogleStyle(); 6682 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; 6683 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 6684 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 6685 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 6686 EXPECT_EQ("if (true) return 42;", 6687 format("if (true)\nreturn 42;", AllowsMergedIf)); 6688 FormatStyle ShortMergedIf = AllowsMergedIf; 6689 ShortMergedIf.ColumnLimit = 25; 6690 verifyFormat("#define A \\\n" 6691 " if (true) return 42;", 6692 ShortMergedIf); 6693 verifyFormat("#define A \\\n" 6694 " f(); \\\n" 6695 " if (true)\n" 6696 "#define B", 6697 ShortMergedIf); 6698 verifyFormat("#define A \\\n" 6699 " f(); \\\n" 6700 " if (true)\n" 6701 "g();", 6702 ShortMergedIf); 6703 verifyFormat("{\n" 6704 "#ifdef A\n" 6705 " // Comment\n" 6706 " if (true) continue;\n" 6707 "#endif\n" 6708 " // Comment\n" 6709 " if (true) continue;\n" 6710 "}", 6711 ShortMergedIf); 6712 ShortMergedIf.ColumnLimit = 29; 6713 verifyFormat("#define A \\\n" 6714 " if (aaaaaaaaaa) return 1; \\\n" 6715 " return 2;", 6716 ShortMergedIf); 6717 ShortMergedIf.ColumnLimit = 28; 6718 verifyFormat("#define A \\\n" 6719 " if (aaaaaaaaaa) \\\n" 6720 " return 1; \\\n" 6721 " return 2;", 6722 ShortMergedIf); 6723 } 6724 6725 TEST_F(FormatTest, BlockCommentsInControlLoops) { 6726 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6727 " f();\n" 6728 "}"); 6729 verifyFormat("if (0) /* a comment in a strange place */ {\n" 6730 " f();\n" 6731 "} /* another comment */ else /* comment #3 */ {\n" 6732 " g();\n" 6733 "}"); 6734 verifyFormat("while (0) /* a comment in a strange place */ {\n" 6735 " f();\n" 6736 "}"); 6737 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 6738 " f();\n" 6739 "}"); 6740 verifyFormat("do /* a comment in a strange place */ {\n" 6741 " f();\n" 6742 "} /* another comment */ while (0);"); 6743 } 6744 6745 TEST_F(FormatTest, BlockComments) { 6746 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 6747 format("/* *//* */ /* */\n/* *//* */ /* */")); 6748 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 6749 EXPECT_EQ("#define A /*123*/ \\\n" 6750 " b\n" 6751 "/* */\n" 6752 "someCall(\n" 6753 " parameter);", 6754 format("#define A /*123*/ b\n" 6755 "/* */\n" 6756 "someCall(parameter);", 6757 getLLVMStyleWithColumns(15))); 6758 6759 EXPECT_EQ("#define A\n" 6760 "/* */ someCall(\n" 6761 " parameter);", 6762 format("#define A\n" 6763 "/* */someCall(parameter);", 6764 getLLVMStyleWithColumns(15))); 6765 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 6766 EXPECT_EQ("/*\n" 6767 "*\n" 6768 " * aaaaaa\n" 6769 " * aaaaaa\n" 6770 "*/", 6771 format("/*\n" 6772 "*\n" 6773 " * aaaaaa aaaaaa\n" 6774 "*/", 6775 getLLVMStyleWithColumns(10))); 6776 EXPECT_EQ("/*\n" 6777 "**\n" 6778 "* aaaaaa\n" 6779 "*aaaaaa\n" 6780 "*/", 6781 format("/*\n" 6782 "**\n" 6783 "* aaaaaa aaaaaa\n" 6784 "*/", 6785 getLLVMStyleWithColumns(10))); 6786 6787 FormatStyle NoBinPacking = getLLVMStyle(); 6788 NoBinPacking.BinPackParameters = false; 6789 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 6790 " 2, /* comment 2 */\n" 6791 " 3, /* comment 3 */\n" 6792 " aaaa,\n" 6793 " bbbb);", 6794 format("someFunction (1, /* comment 1 */\n" 6795 " 2, /* comment 2 */ \n" 6796 " 3, /* comment 3 */\n" 6797 "aaaa, bbbb );", 6798 NoBinPacking)); 6799 verifyFormat( 6800 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6801 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6802 EXPECT_EQ( 6803 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 6804 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6805 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 6806 format( 6807 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 6808 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6809 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 6810 EXPECT_EQ( 6811 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 6812 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 6813 "int cccccccccccccccccccccccccccccc; /* comment */\n", 6814 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 6815 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 6816 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 6817 6818 verifyFormat("void f(int * /* unused */) {}"); 6819 6820 EXPECT_EQ("/*\n" 6821 " **\n" 6822 " */", 6823 format("/*\n" 6824 " **\n" 6825 " */")); 6826 EXPECT_EQ("/*\n" 6827 " *q\n" 6828 " */", 6829 format("/*\n" 6830 " *q\n" 6831 " */")); 6832 EXPECT_EQ("/*\n" 6833 " * q\n" 6834 " */", 6835 format("/*\n" 6836 " * q\n" 6837 " */")); 6838 EXPECT_EQ("/*\n" 6839 " **/", 6840 format("/*\n" 6841 " **/")); 6842 EXPECT_EQ("/*\n" 6843 " ***/", 6844 format("/*\n" 6845 " ***/")); 6846 } 6847 6848 TEST_F(FormatTest, BlockCommentsInMacros) { 6849 EXPECT_EQ("#define A \\\n" 6850 " { \\\n" 6851 " /* one line */ \\\n" 6852 " someCall();", 6853 format("#define A { \\\n" 6854 " /* one line */ \\\n" 6855 " someCall();", 6856 getLLVMStyleWithColumns(20))); 6857 EXPECT_EQ("#define A \\\n" 6858 " { \\\n" 6859 " /* previous */ \\\n" 6860 " /* one line */ \\\n" 6861 " someCall();", 6862 format("#define A { \\\n" 6863 " /* previous */ \\\n" 6864 " /* one line */ \\\n" 6865 " someCall();", 6866 getLLVMStyleWithColumns(20))); 6867 } 6868 6869 TEST_F(FormatTest, BlockCommentsAtEndOfLine) { 6870 EXPECT_EQ("a = {\n" 6871 " 1111 /* */\n" 6872 "};", 6873 format("a = {1111 /* */\n" 6874 "};", 6875 getLLVMStyleWithColumns(15))); 6876 EXPECT_EQ("a = {\n" 6877 " 1111 /* */\n" 6878 "};", 6879 format("a = {1111 /* */\n" 6880 "};", 6881 getLLVMStyleWithColumns(15))); 6882 6883 // FIXME: The formatting is still wrong here. 6884 EXPECT_EQ("a = {\n" 6885 " 1111 /* a\n" 6886 " */\n" 6887 "};", 6888 format("a = {1111 /* a */\n" 6889 "};", 6890 getLLVMStyleWithColumns(15))); 6891 } 6892 6893 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { 6894 // FIXME: This is not what we want... 6895 verifyFormat("{\n" 6896 "// a" 6897 "// b"); 6898 } 6899 6900 TEST_F(FormatTest, FormatStarDependingOnContext) { 6901 verifyFormat("void f(int *a);"); 6902 verifyFormat("void f() { f(fint * b); }"); 6903 verifyFormat("class A {\n void f(int *a);\n};"); 6904 verifyFormat("class A {\n int *a;\n};"); 6905 verifyFormat("namespace a {\n" 6906 "namespace b {\n" 6907 "class A {\n" 6908 " void f() {}\n" 6909 " int *a;\n" 6910 "};\n" 6911 "}\n" 6912 "}"); 6913 } 6914 6915 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 6916 verifyFormat("while"); 6917 verifyFormat("operator"); 6918 } 6919 6920 //===----------------------------------------------------------------------===// 6921 // Objective-C tests. 6922 //===----------------------------------------------------------------------===// 6923 6924 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 6925 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 6926 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 6927 format("-(NSUInteger)indexOfObject:(id)anObject;")); 6928 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 6929 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 6930 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 6931 format("-(NSInteger)Method3:(id)anObject;")); 6932 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 6933 format("-(NSInteger)Method4:(id)anObject;")); 6934 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 6935 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 6936 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 6937 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 6938 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 6939 "forAllCells:(BOOL)flag;", 6940 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 6941 "forAllCells:(BOOL)flag;")); 6942 6943 // Very long objectiveC method declaration. 6944 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 6945 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 6946 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 6947 " inRange:(NSRange)range\n" 6948 " outRange:(NSRange)out_range\n" 6949 " outRange1:(NSRange)out_range1\n" 6950 " outRange2:(NSRange)out_range2\n" 6951 " outRange3:(NSRange)out_range3\n" 6952 " outRange4:(NSRange)out_range4\n" 6953 " outRange5:(NSRange)out_range5\n" 6954 " outRange6:(NSRange)out_range6\n" 6955 " outRange7:(NSRange)out_range7\n" 6956 " outRange8:(NSRange)out_range8\n" 6957 " outRange9:(NSRange)out_range9;"); 6958 6959 // When the function name has to be wrapped. 6960 FormatStyle Style = getLLVMStyle(); 6961 Style.IndentWrappedFunctionNames = false; 6962 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 6963 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 6964 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 6965 "}", 6966 Style); 6967 Style.IndentWrappedFunctionNames = true; 6968 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 6969 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 6970 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 6971 "}", 6972 Style); 6973 6974 verifyFormat("- (int)sum:(vector<int>)numbers;"); 6975 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 6976 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 6977 // protocol lists (but not for template classes): 6978 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 6979 6980 verifyFormat("- (int (*)())foo:(int (*)())f;"); 6981 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 6982 6983 // If there's no return type (very rare in practice!), LLVM and Google style 6984 // agree. 6985 verifyFormat("- foo;"); 6986 verifyFormat("- foo:(int)f;"); 6987 verifyGoogleFormat("- foo:(int)foo;"); 6988 } 6989 6990 TEST_F(FormatTest, FormatObjCInterface) { 6991 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 6992 "@public\n" 6993 " int field1;\n" 6994 "@protected\n" 6995 " int field2;\n" 6996 "@private\n" 6997 " int field3;\n" 6998 "@package\n" 6999 " int field4;\n" 7000 "}\n" 7001 "+ (id)init;\n" 7002 "@end"); 7003 7004 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" 7005 " @public\n" 7006 " int field1;\n" 7007 " @protected\n" 7008 " int field2;\n" 7009 " @private\n" 7010 " int field3;\n" 7011 " @package\n" 7012 " int field4;\n" 7013 "}\n" 7014 "+ (id)init;\n" 7015 "@end"); 7016 7017 verifyFormat("@interface /* wait for it */ Foo\n" 7018 "+ (id)init;\n" 7019 "// Look, a comment!\n" 7020 "- (int)answerWith:(int)i;\n" 7021 "@end"); 7022 7023 verifyFormat("@interface Foo\n" 7024 "@end\n" 7025 "@interface Bar\n" 7026 "@end"); 7027 7028 verifyFormat("@interface Foo : Bar\n" 7029 "+ (id)init;\n" 7030 "@end"); 7031 7032 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 7033 "+ (id)init;\n" 7034 "@end"); 7035 7036 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" 7037 "+ (id)init;\n" 7038 "@end"); 7039 7040 verifyFormat("@interface Foo (HackStuff)\n" 7041 "+ (id)init;\n" 7042 "@end"); 7043 7044 verifyFormat("@interface Foo ()\n" 7045 "+ (id)init;\n" 7046 "@end"); 7047 7048 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 7049 "+ (id)init;\n" 7050 "@end"); 7051 7052 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" 7053 "+ (id)init;\n" 7054 "@end"); 7055 7056 verifyFormat("@interface Foo {\n" 7057 " int _i;\n" 7058 "}\n" 7059 "+ (id)init;\n" 7060 "@end"); 7061 7062 verifyFormat("@interface Foo : Bar {\n" 7063 " int _i;\n" 7064 "}\n" 7065 "+ (id)init;\n" 7066 "@end"); 7067 7068 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 7069 " int _i;\n" 7070 "}\n" 7071 "+ (id)init;\n" 7072 "@end"); 7073 7074 verifyFormat("@interface Foo (HackStuff) {\n" 7075 " int _i;\n" 7076 "}\n" 7077 "+ (id)init;\n" 7078 "@end"); 7079 7080 verifyFormat("@interface Foo () {\n" 7081 " int _i;\n" 7082 "}\n" 7083 "+ (id)init;\n" 7084 "@end"); 7085 7086 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 7087 " int _i;\n" 7088 "}\n" 7089 "+ (id)init;\n" 7090 "@end"); 7091 7092 FormatStyle OnePerLine = getGoogleStyle(); 7093 OnePerLine.BinPackParameters = false; 7094 verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n" 7095 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7096 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7097 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 7099 "}", 7100 OnePerLine); 7101 } 7102 7103 TEST_F(FormatTest, FormatObjCImplementation) { 7104 verifyFormat("@implementation Foo : NSObject {\n" 7105 "@public\n" 7106 " int field1;\n" 7107 "@protected\n" 7108 " int field2;\n" 7109 "@private\n" 7110 " int field3;\n" 7111 "@package\n" 7112 " int field4;\n" 7113 "}\n" 7114 "+ (id)init {\n}\n" 7115 "@end"); 7116 7117 verifyGoogleFormat("@implementation Foo : NSObject {\n" 7118 " @public\n" 7119 " int field1;\n" 7120 " @protected\n" 7121 " int field2;\n" 7122 " @private\n" 7123 " int field3;\n" 7124 " @package\n" 7125 " int field4;\n" 7126 "}\n" 7127 "+ (id)init {\n}\n" 7128 "@end"); 7129 7130 verifyFormat("@implementation Foo\n" 7131 "+ (id)init {\n" 7132 " if (true)\n" 7133 " return nil;\n" 7134 "}\n" 7135 "// Look, a comment!\n" 7136 "- (int)answerWith:(int)i {\n" 7137 " return i;\n" 7138 "}\n" 7139 "+ (int)answerWith:(int)i {\n" 7140 " return i;\n" 7141 "}\n" 7142 "@end"); 7143 7144 verifyFormat("@implementation Foo\n" 7145 "@end\n" 7146 "@implementation Bar\n" 7147 "@end"); 7148 7149 EXPECT_EQ("@implementation Foo : Bar\n" 7150 "+ (id)init {\n}\n" 7151 "- (void)foo {\n}\n" 7152 "@end", 7153 format("@implementation Foo : Bar\n" 7154 "+(id)init{}\n" 7155 "-(void)foo{}\n" 7156 "@end")); 7157 7158 verifyFormat("@implementation Foo {\n" 7159 " int _i;\n" 7160 "}\n" 7161 "+ (id)init {\n}\n" 7162 "@end"); 7163 7164 verifyFormat("@implementation Foo : Bar {\n" 7165 " int _i;\n" 7166 "}\n" 7167 "+ (id)init {\n}\n" 7168 "@end"); 7169 7170 verifyFormat("@implementation Foo (HackStuff)\n" 7171 "+ (id)init {\n}\n" 7172 "@end"); 7173 verifyFormat("@implementation ObjcClass\n" 7174 "- (void)method;\n" 7175 "{}\n" 7176 "@end"); 7177 } 7178 7179 TEST_F(FormatTest, FormatObjCProtocol) { 7180 verifyFormat("@protocol Foo\n" 7181 "@property(weak) id delegate;\n" 7182 "- (NSUInteger)numberOfThings;\n" 7183 "@end"); 7184 7185 verifyFormat("@protocol MyProtocol <NSObject>\n" 7186 "- (NSUInteger)numberOfThings;\n" 7187 "@end"); 7188 7189 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" 7190 "- (NSUInteger)numberOfThings;\n" 7191 "@end"); 7192 7193 verifyFormat("@protocol Foo;\n" 7194 "@protocol Bar;\n"); 7195 7196 verifyFormat("@protocol Foo\n" 7197 "@end\n" 7198 "@protocol Bar\n" 7199 "@end"); 7200 7201 verifyFormat("@protocol myProtocol\n" 7202 "- (void)mandatoryWithInt:(int)i;\n" 7203 "@optional\n" 7204 "- (void)optional;\n" 7205 "@required\n" 7206 "- (void)required;\n" 7207 "@optional\n" 7208 "@property(assign) int madProp;\n" 7209 "@end\n"); 7210 7211 verifyFormat("@property(nonatomic, assign, readonly)\n" 7212 " int *looooooooooooooooooooooooooooongNumber;\n" 7213 "@property(nonatomic, assign, readonly)\n" 7214 " NSString *looooooooooooooooooooooooooooongName;"); 7215 7216 verifyFormat("@implementation PR18406\n" 7217 "}\n" 7218 "@end"); 7219 } 7220 7221 TEST_F(FormatTest, FormatObjCMethodDeclarations) { 7222 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 7223 " rect:(NSRect)theRect\n" 7224 " interval:(float)theInterval {\n" 7225 "}"); 7226 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 7227 " longKeyword:(NSRect)theRect\n" 7228 " evenLongerKeyword:(float)theInterval\n" 7229 " error:(NSError **)theError {\n" 7230 "}"); 7231 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n" 7232 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n" 7233 " NS_DESIGNATED_INITIALIZER;", 7234 getLLVMStyleWithColumns(60)); 7235 } 7236 7237 TEST_F(FormatTest, FormatObjCMethodExpr) { 7238 verifyFormat("[foo bar:baz];"); 7239 verifyFormat("return [foo bar:baz];"); 7240 verifyFormat("return (a)[foo bar:baz];"); 7241 verifyFormat("f([foo bar:baz]);"); 7242 verifyFormat("f(2, [foo bar:baz]);"); 7243 verifyFormat("f(2, a ? b : c);"); 7244 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 7245 7246 // Unary operators. 7247 verifyFormat("int a = +[foo bar:baz];"); 7248 verifyFormat("int a = -[foo bar:baz];"); 7249 verifyFormat("int a = ![foo bar:baz];"); 7250 verifyFormat("int a = ~[foo bar:baz];"); 7251 verifyFormat("int a = ++[foo bar:baz];"); 7252 verifyFormat("int a = --[foo bar:baz];"); 7253 verifyFormat("int a = sizeof [foo bar:baz];"); 7254 verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle()); 7255 verifyFormat("int a = &[foo bar:baz];"); 7256 verifyFormat("int a = *[foo bar:baz];"); 7257 // FIXME: Make casts work, without breaking f()[4]. 7258 // verifyFormat("int a = (int)[foo bar:baz];"); 7259 // verifyFormat("return (int)[foo bar:baz];"); 7260 // verifyFormat("(void)[foo bar:baz];"); 7261 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 7262 7263 // Binary operators. 7264 verifyFormat("[foo bar:baz], [foo bar:baz];"); 7265 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 7266 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 7267 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 7268 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 7269 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 7270 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 7271 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 7272 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 7273 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 7274 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 7275 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 7276 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 7277 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 7278 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 7279 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 7280 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 7281 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 7282 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 7283 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 7284 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 7285 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 7286 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 7287 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 7288 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 7289 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 7290 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 7291 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 7292 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 7293 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 7294 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 7295 // Whew! 7296 7297 verifyFormat("return in[42];"); 7298 verifyFormat("for (auto v : in[1]) {\n}"); 7299 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}"); 7300 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}"); 7301 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}"); 7302 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}"); 7303 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}"); 7304 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 7305 "}"); 7306 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];"); 7307 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];"); 7308 verifyFormat("[self aaaaa:(Type)a bbbbb:3];"); 7309 7310 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 7311 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 7312 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 7313 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 7314 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 7315 verifyFormat("[button setAction:@selector(zoomOut:)];"); 7316 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 7317 7318 verifyFormat("arr[[self indexForFoo:a]];"); 7319 verifyFormat("throw [self errorFor:a];"); 7320 verifyFormat("@throw [self errorFor:a];"); 7321 7322 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];"); 7323 verifyFormat("[(id)foo bar:(id) ? baz : quux];"); 7324 verifyFormat("4 > 4 ? (id)a : (id)baz;"); 7325 7326 // This tests that the formatter doesn't break after "backing" but before ":", 7327 // which would be at 80 columns. 7328 verifyFormat( 7329 "void f() {\n" 7330 " if ((self = [super initWithContentRect:contentRect\n" 7331 " styleMask:styleMask ?: otherMask\n" 7332 " backing:NSBackingStoreBuffered\n" 7333 " defer:YES]))"); 7334 7335 verifyFormat( 7336 "[foo checkThatBreakingAfterColonWorksOk:\n" 7337 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 7338 7339 verifyFormat("[myObj short:arg1 // Force line break\n" 7340 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" 7341 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" 7342 " error:arg4];"); 7343 verifyFormat( 7344 "void f() {\n" 7345 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7346 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7347 " pos.width(), pos.height())\n" 7348 " styleMask:NSBorderlessWindowMask\n" 7349 " backing:NSBackingStoreBuffered\n" 7350 " defer:NO]);\n" 7351 "}"); 7352 verifyFormat( 7353 "void f() {\n" 7354 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n" 7355 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n" 7356 " pos.width(), pos.height())\n" 7357 " syeMask:NSBorderlessWindowMask\n" 7358 " bking:NSBackingStoreBuffered\n" 7359 " der:NO]);\n" 7360 "}", 7361 getLLVMStyleWithColumns(70)); 7362 verifyFormat( 7363 "void f() {\n" 7364 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 7365 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 7366 " pos.width(), pos.height())\n" 7367 " styleMask:NSBorderlessWindowMask\n" 7368 " backing:NSBackingStoreBuffered\n" 7369 " defer:NO]);\n" 7370 "}", 7371 getChromiumStyle(FormatStyle::LK_Cpp)); 7372 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 7373 " with:contentsNativeView];"); 7374 7375 verifyFormat( 7376 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 7377 " owner:nillllll];"); 7378 7379 verifyFormat( 7380 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 7381 " forType:kBookmarkButtonDragType];"); 7382 7383 verifyFormat("[defaultCenter addObserver:self\n" 7384 " selector:@selector(willEnterFullscreen)\n" 7385 " name:kWillEnterFullscreenNotification\n" 7386 " object:nil];"); 7387 verifyFormat("[image_rep drawInRect:drawRect\n" 7388 " fromRect:NSZeroRect\n" 7389 " operation:NSCompositeCopy\n" 7390 " fraction:1.0\n" 7391 " respectFlipped:NO\n" 7392 " hints:nil];"); 7393 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7394 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7395 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7396 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 7397 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n" 7398 " aaaaaaaaaaaaaaaaaaaaaa];"); 7399 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" 7400 " .aaaaaaaa];", // FIXME: Indentation seems off. 7401 getLLVMStyleWithColumns(60)); 7402 7403 verifyFormat( 7404 "scoped_nsobject<NSTextField> message(\n" 7405 " // The frame will be fixed up when |-setMessageText:| is called.\n" 7406 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 7407 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" 7408 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" 7409 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" 7410 " aaaa:bbb];"); 7411 verifyFormat("[self param:function( //\n" 7412 " parameter)]"); 7413 verifyFormat( 7414 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7415 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 7416 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); 7417 7418 // FIXME: This violates the column limit. 7419 verifyFormat( 7420 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7421 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n" 7422 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];", 7423 getLLVMStyleWithColumns(60)); 7424 7425 // Variadic parameters. 7426 verifyFormat( 7427 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); 7428 verifyFormat( 7429 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7430 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 7431 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];"); 7432 verifyFormat("[self // break\n" 7433 " a:a\n" 7434 " aaa:aaa];"); 7435 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n" 7436 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);"); 7437 } 7438 7439 TEST_F(FormatTest, ObjCAt) { 7440 verifyFormat("@autoreleasepool"); 7441 verifyFormat("@catch"); 7442 verifyFormat("@class"); 7443 verifyFormat("@compatibility_alias"); 7444 verifyFormat("@defs"); 7445 verifyFormat("@dynamic"); 7446 verifyFormat("@encode"); 7447 verifyFormat("@end"); 7448 verifyFormat("@finally"); 7449 verifyFormat("@implementation"); 7450 verifyFormat("@import"); 7451 verifyFormat("@interface"); 7452 verifyFormat("@optional"); 7453 verifyFormat("@package"); 7454 verifyFormat("@private"); 7455 verifyFormat("@property"); 7456 verifyFormat("@protected"); 7457 verifyFormat("@protocol"); 7458 verifyFormat("@public"); 7459 verifyFormat("@required"); 7460 verifyFormat("@selector"); 7461 verifyFormat("@synchronized"); 7462 verifyFormat("@synthesize"); 7463 verifyFormat("@throw"); 7464 verifyFormat("@try"); 7465 7466 EXPECT_EQ("@interface", format("@ interface")); 7467 7468 // The precise formatting of this doesn't matter, nobody writes code like 7469 // this. 7470 verifyFormat("@ /*foo*/ interface"); 7471 } 7472 7473 TEST_F(FormatTest, ObjCSnippets) { 7474 verifyFormat("@autoreleasepool {\n" 7475 " foo();\n" 7476 "}"); 7477 verifyFormat("@class Foo, Bar;"); 7478 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 7479 verifyFormat("@dynamic textColor;"); 7480 verifyFormat("char *buf1 = @encode(int *);"); 7481 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 7482 verifyFormat("char *buf1 = @encode(int **);"); 7483 verifyFormat("Protocol *proto = @protocol(p1);"); 7484 verifyFormat("SEL s = @selector(foo:);"); 7485 verifyFormat("@synchronized(self) {\n" 7486 " f();\n" 7487 "}"); 7488 7489 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7490 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 7491 7492 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 7493 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 7494 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); 7495 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7496 getMozillaStyle()); 7497 verifyFormat("@property BOOL editable;", getMozillaStyle()); 7498 verifyFormat("@property (assign, getter=isEditable) BOOL editable;", 7499 getWebKitStyle()); 7500 verifyFormat("@property BOOL editable;", getWebKitStyle()); 7501 7502 verifyFormat("@import foo.bar;\n" 7503 "@import baz;"); 7504 } 7505 7506 TEST_F(FormatTest, ObjCLiterals) { 7507 verifyFormat("@\"String\""); 7508 verifyFormat("@1"); 7509 verifyFormat("@+4.8"); 7510 verifyFormat("@-4"); 7511 verifyFormat("@1LL"); 7512 verifyFormat("@.5"); 7513 verifyFormat("@'c'"); 7514 verifyFormat("@true"); 7515 7516 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 7517 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 7518 verifyFormat("NSNumber *favoriteColor = @(Green);"); 7519 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 7520 7521 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];"); 7522 } 7523 7524 TEST_F(FormatTest, ObjCDictLiterals) { 7525 verifyFormat("@{"); 7526 verifyFormat("@{}"); 7527 verifyFormat("@{@\"one\" : @1}"); 7528 verifyFormat("return @{@\"one\" : @1;"); 7529 verifyFormat("@{@\"one\" : @1}"); 7530 7531 verifyFormat("@{@\"one\" : @{@2 : @1}}"); 7532 verifyFormat("@{\n" 7533 " @\"one\" : @{@2 : @1},\n" 7534 "}"); 7535 7536 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); 7537 verifyIncompleteFormat("[self setDict:@{}"); 7538 verifyIncompleteFormat("[self setDict:@{@1 : @2}"); 7539 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); 7540 verifyFormat( 7541 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); 7542 verifyFormat( 7543 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); 7544 7545 verifyFormat("NSDictionary *d = @{\n" 7546 " @\"nam\" : NSUserNam(),\n" 7547 " @\"dte\" : [NSDate date],\n" 7548 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7549 "};"); 7550 verifyFormat( 7551 "@{\n" 7552 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7553 "regularFont,\n" 7554 "};"); 7555 verifyGoogleFormat( 7556 "@{\n" 7557 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 7558 "regularFont,\n" 7559 "};"); 7560 verifyFormat( 7561 "@{\n" 7562 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 7563 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n" 7564 "};"); 7565 7566 // We should try to be robust in case someone forgets the "@". 7567 verifyFormat("NSDictionary *d = {\n" 7568 " @\"nam\" : NSUserNam(),\n" 7569 " @\"dte\" : [NSDate date],\n" 7570 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 7571 "};"); 7572 verifyFormat("NSMutableDictionary *dictionary =\n" 7573 " [NSMutableDictionary dictionaryWithDictionary:@{\n" 7574 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" 7575 " bbbbbbbbbbbbbbbbbb : bbbbb,\n" 7576 " cccccccccccccccc : ccccccccccccccc\n" 7577 " }];"); 7578 } 7579 7580 TEST_F(FormatTest, ObjCArrayLiterals) { 7581 verifyIncompleteFormat("@["); 7582 verifyFormat("@[]"); 7583 verifyFormat( 7584 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 7585 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 7586 verifyFormat("NSArray *array = @[ [foo description] ];"); 7587 7588 verifyFormat( 7589 "NSArray *some_variable = @[\n" 7590 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 7591 " @\"aaaaaaaaaaaaaaaaa\",\n" 7592 " @\"aaaaaaaaaaaaaaaaa\",\n" 7593 " @\"aaaaaaaaaaaaaaaaa\"\n" 7594 "];"); 7595 verifyFormat("NSArray *some_variable = @[\n" 7596 " @\"aaaaaaaaaaaaaaaaa\",\n" 7597 " @\"aaaaaaaaaaaaaaaaa\",\n" 7598 " @\"aaaaaaaaaaaaaaaaa\",\n" 7599 " @\"aaaaaaaaaaaaaaaaa\",\n" 7600 "];"); 7601 verifyGoogleFormat("NSArray *some_variable = @[\n" 7602 " @\"aaaaaaaaaaaaaaaaa\",\n" 7603 " @\"aaaaaaaaaaaaaaaaa\",\n" 7604 " @\"aaaaaaaaaaaaaaaaa\",\n" 7605 " @\"aaaaaaaaaaaaaaaaa\"\n" 7606 "];"); 7607 verifyFormat("NSArray *array = @[\n" 7608 " @\"a\",\n" 7609 " @\"a\",\n" // Trailing comma -> one per line. 7610 "];"); 7611 7612 // We should try to be robust in case someone forgets the "@". 7613 verifyFormat("NSArray *some_variable = [\n" 7614 " @\"aaaaaaaaaaaaaaaaa\",\n" 7615 " @\"aaaaaaaaaaaaaaaaa\",\n" 7616 " @\"aaaaaaaaaaaaaaaaa\",\n" 7617 " @\"aaaaaaaaaaaaaaaaa\",\n" 7618 "];"); 7619 verifyFormat( 7620 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n" 7621 " index:(NSUInteger)index\n" 7622 " nonDigitAttributes:\n" 7623 " (NSDictionary *)noDigitAttributes;"); 7624 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n" 7625 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n" 7626 "]];"); 7627 } 7628 7629 TEST_F(FormatTest, ReformatRegionAdjustsIndent) { 7630 EXPECT_EQ("{\n" 7631 "{\n" 7632 "a;\n" 7633 "b;\n" 7634 "}\n" 7635 "}", 7636 format("{\n" 7637 "{\n" 7638 "a;\n" 7639 " b;\n" 7640 "}\n" 7641 "}", 7642 13, 2, getLLVMStyle())); 7643 EXPECT_EQ("{\n" 7644 "{\n" 7645 " a;\n" 7646 "b;\n" 7647 "}\n" 7648 "}", 7649 format("{\n" 7650 "{\n" 7651 " a;\n" 7652 "b;\n" 7653 "}\n" 7654 "}", 7655 9, 2, getLLVMStyle())); 7656 EXPECT_EQ("{\n" 7657 "{\n" 7658 "public:\n" 7659 " b;\n" 7660 "}\n" 7661 "}", 7662 format("{\n" 7663 "{\n" 7664 "public:\n" 7665 " b;\n" 7666 "}\n" 7667 "}", 7668 17, 2, getLLVMStyle())); 7669 EXPECT_EQ("{\n" 7670 "{\n" 7671 "a;\n" 7672 "}\n" 7673 "{\n" 7674 " b; //\n" 7675 "}\n" 7676 "}", 7677 format("{\n" 7678 "{\n" 7679 "a;\n" 7680 "}\n" 7681 "{\n" 7682 " b; //\n" 7683 "}\n" 7684 "}", 7685 22, 2, getLLVMStyle())); 7686 EXPECT_EQ(" {\n" 7687 " a; //\n" 7688 " }", 7689 format(" {\n" 7690 "a; //\n" 7691 " }", 7692 4, 2, getLLVMStyle())); 7693 EXPECT_EQ("void f() {}\n" 7694 "void g() {}", 7695 format("void f() {}\n" 7696 "void g() {}", 7697 13, 0, getLLVMStyle())); 7698 EXPECT_EQ("int a; // comment\n" 7699 " // line 2\n" 7700 "int b;", 7701 format("int a; // comment\n" 7702 " // line 2\n" 7703 " int b;", 7704 35, 0, getLLVMStyle())); 7705 EXPECT_EQ(" int a;\n" 7706 " void\n" 7707 " ffffff() {\n" 7708 " }", 7709 format(" int a;\n" 7710 "void ffffff() {}", 7711 11, 0, getLLVMStyleWithColumns(11))); 7712 7713 EXPECT_EQ(" void f() {\n" 7714 "#define A 1\n" 7715 " }", 7716 format(" void f() {\n" 7717 " #define A 1\n" // Format this line. 7718 " }", 7719 20, 0, getLLVMStyle())); 7720 EXPECT_EQ(" void f() {\n" 7721 " int i;\n" 7722 "#define A \\\n" 7723 " int i; \\\n" 7724 " int j;\n" 7725 " int k;\n" 7726 " }", 7727 format(" void f() {\n" 7728 " int i;\n" 7729 "#define A \\\n" 7730 " int i; \\\n" 7731 " int j;\n" 7732 " int k;\n" // Format this line. 7733 " }", 7734 67, 0, getLLVMStyle())); 7735 } 7736 7737 TEST_F(FormatTest, BreaksStringLiterals) { 7738 EXPECT_EQ("\"some text \"\n" 7739 "\"other\";", 7740 format("\"some text other\";", getLLVMStyleWithColumns(12))); 7741 EXPECT_EQ("\"some text \"\n" 7742 "\"other\";", 7743 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 7744 EXPECT_EQ( 7745 "#define A \\\n" 7746 " \"some \" \\\n" 7747 " \"text \" \\\n" 7748 " \"other\";", 7749 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 7750 EXPECT_EQ( 7751 "#define A \\\n" 7752 " \"so \" \\\n" 7753 " \"text \" \\\n" 7754 " \"other\";", 7755 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 7756 7757 EXPECT_EQ("\"some text\"", 7758 format("\"some text\"", getLLVMStyleWithColumns(1))); 7759 EXPECT_EQ("\"some text\"", 7760 format("\"some text\"", getLLVMStyleWithColumns(11))); 7761 EXPECT_EQ("\"some \"\n" 7762 "\"text\"", 7763 format("\"some text\"", getLLVMStyleWithColumns(10))); 7764 EXPECT_EQ("\"some \"\n" 7765 "\"text\"", 7766 format("\"some text\"", getLLVMStyleWithColumns(7))); 7767 EXPECT_EQ("\"some\"\n" 7768 "\" tex\"\n" 7769 "\"t\"", 7770 format("\"some text\"", getLLVMStyleWithColumns(6))); 7771 EXPECT_EQ("\"some\"\n" 7772 "\" tex\"\n" 7773 "\" and\"", 7774 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 7775 EXPECT_EQ("\"some\"\n" 7776 "\"/tex\"\n" 7777 "\"/and\"", 7778 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 7779 7780 EXPECT_EQ("variable =\n" 7781 " \"long string \"\n" 7782 " \"literal\";", 7783 format("variable = \"long string literal\";", 7784 getLLVMStyleWithColumns(20))); 7785 7786 EXPECT_EQ("variable = f(\n" 7787 " \"long string \"\n" 7788 " \"literal\",\n" 7789 " short,\n" 7790 " loooooooooooooooooooong);", 7791 format("variable = f(\"long string literal\", short, " 7792 "loooooooooooooooooooong);", 7793 getLLVMStyleWithColumns(20))); 7794 7795 EXPECT_EQ( 7796 "f(g(\"long string \"\n" 7797 " \"literal\"),\n" 7798 " b);", 7799 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 7800 EXPECT_EQ("f(g(\"long string \"\n" 7801 " \"literal\",\n" 7802 " a),\n" 7803 " b);", 7804 format("f(g(\"long string literal\", a), b);", 7805 getLLVMStyleWithColumns(20))); 7806 EXPECT_EQ( 7807 "f(\"one two\".split(\n" 7808 " variable));", 7809 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 7810 EXPECT_EQ("f(\"one two three four five six \"\n" 7811 " \"seven\".split(\n" 7812 " really_looooong_variable));", 7813 format("f(\"one two three four five six seven\"." 7814 "split(really_looooong_variable));", 7815 getLLVMStyleWithColumns(33))); 7816 7817 EXPECT_EQ("f(\"some \"\n" 7818 " \"text\",\n" 7819 " other);", 7820 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 7821 7822 // Only break as a last resort. 7823 verifyFormat( 7824 "aaaaaaaaaaaaaaaaaaaa(\n" 7825 " aaaaaaaaaaaaaaaaaaaa,\n" 7826 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 7827 7828 EXPECT_EQ("\"splitmea\"\n" 7829 "\"trandomp\"\n" 7830 "\"oint\"", 7831 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 7832 7833 EXPECT_EQ("\"split/\"\n" 7834 "\"pathat/\"\n" 7835 "\"slashes\"", 7836 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7837 7838 EXPECT_EQ("\"split/\"\n" 7839 "\"pathat/\"\n" 7840 "\"slashes\"", 7841 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 7842 EXPECT_EQ("\"split at \"\n" 7843 "\"spaces/at/\"\n" 7844 "\"slashes.at.any$\"\n" 7845 "\"non-alphanumeric%\"\n" 7846 "\"1111111111characte\"\n" 7847 "\"rs\"", 7848 format("\"split at " 7849 "spaces/at/" 7850 "slashes.at." 7851 "any$non-" 7852 "alphanumeric%" 7853 "1111111111characte" 7854 "rs\"", 7855 getLLVMStyleWithColumns(20))); 7856 7857 // Verify that splitting the strings understands 7858 // Style::AlwaysBreakBeforeMultilineStrings. 7859 EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n" 7860 " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 7861 " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 7862 format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa " 7863 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa " 7864 "aaaaaaaaaaaaaaaaaaaaaa\");", 7865 getGoogleStyle())); 7866 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7867 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 7868 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 7869 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 7870 "aaaaaaaaaaaaaaaaaaaaaa\";", 7871 getGoogleStyle())); 7872 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7873 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7874 format("llvm::outs() << " 7875 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 7876 "aaaaaaaaaaaaaaaaaaa\";")); 7877 EXPECT_EQ("ffff(\n" 7878 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7879 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7880 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7881 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 7882 getGoogleStyle())); 7883 7884 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 7885 AlignLeft.AlignEscapedNewlinesLeft = true; 7886 EXPECT_EQ("#define A \\\n" 7887 " \"some \" \\\n" 7888 " \"text \" \\\n" 7889 " \"other\";", 7890 format("#define A \"some text other\";", AlignLeft)); 7891 } 7892 7893 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 7894 EXPECT_EQ( 7895 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7896 "(\n" 7897 " \"x\t\");", 7898 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 7899 "aaaaaaa(" 7900 "\"x\t\");")); 7901 } 7902 7903 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 7904 EXPECT_EQ( 7905 "u8\"utf8 string \"\n" 7906 "u8\"literal\";", 7907 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 7908 EXPECT_EQ( 7909 "u\"utf16 string \"\n" 7910 "u\"literal\";", 7911 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 7912 EXPECT_EQ( 7913 "U\"utf32 string \"\n" 7914 "U\"literal\";", 7915 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 7916 EXPECT_EQ("L\"wide string \"\n" 7917 "L\"literal\";", 7918 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 7919 EXPECT_EQ("@\"NSString \"\n" 7920 "@\"literal\";", 7921 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 7922 7923 // This input makes clang-format try to split the incomplete unicode escape 7924 // sequence, which used to lead to a crasher. 7925 verifyNoCrash( 7926 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 7927 getLLVMStyleWithColumns(60)); 7928 } 7929 7930 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 7931 FormatStyle Style = getGoogleStyleWithColumns(15); 7932 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 7933 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 7934 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 7935 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 7936 EXPECT_EQ("u8R\"x(raw literal)x\";", 7937 format("u8R\"x(raw literal)x\";", Style)); 7938 } 7939 7940 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 7941 FormatStyle Style = getLLVMStyleWithColumns(20); 7942 EXPECT_EQ( 7943 "_T(\"aaaaaaaaaaaaaa\")\n" 7944 "_T(\"aaaaaaaaaaaaaa\")\n" 7945 "_T(\"aaaaaaaaaaaa\")", 7946 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 7947 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n" 7948 " _T(\"aaaaaa\"),\n" 7949 " z);", 7950 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 7951 7952 // FIXME: Handle embedded spaces in one iteration. 7953 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 7954 // "_T(\"aaaaaaaaaaaaa\")\n" 7955 // "_T(\"aaaaaaaaaaaaa\")\n" 7956 // "_T(\"a\")", 7957 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7958 // getLLVMStyleWithColumns(20))); 7959 EXPECT_EQ( 7960 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 7961 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 7962 EXPECT_EQ("f(\n" 7963 "#if !TEST\n" 7964 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7965 "#endif\n" 7966 " );", 7967 format("f(\n" 7968 "#if !TEST\n" 7969 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 7970 "#endif\n" 7971 ");")); 7972 EXPECT_EQ("f(\n" 7973 "\n" 7974 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 7975 format("f(\n" 7976 "\n" 7977 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 7978 } 7979 7980 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 7981 EXPECT_EQ( 7982 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7983 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7984 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 7985 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7986 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 7987 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 7988 } 7989 7990 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 7991 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 7992 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 7993 EXPECT_EQ("fffffffffff(g(R\"x(\n" 7994 "multiline raw string literal xxxxxxxxxxxxxx\n" 7995 ")x\",\n" 7996 " a),\n" 7997 " b);", 7998 format("fffffffffff(g(R\"x(\n" 7999 "multiline raw string literal xxxxxxxxxxxxxx\n" 8000 ")x\", a), b);", 8001 getGoogleStyleWithColumns(20))); 8002 EXPECT_EQ("fffffffffff(\n" 8003 " g(R\"x(qqq\n" 8004 "multiline raw string literal xxxxxxxxxxxxxx\n" 8005 ")x\",\n" 8006 " a),\n" 8007 " b);", 8008 format("fffffffffff(g(R\"x(qqq\n" 8009 "multiline raw string literal xxxxxxxxxxxxxx\n" 8010 ")x\", a), b);", 8011 getGoogleStyleWithColumns(20))); 8012 8013 EXPECT_EQ("fffffffffff(R\"x(\n" 8014 "multiline raw string literal xxxxxxxxxxxxxx\n" 8015 ")x\");", 8016 format("fffffffffff(R\"x(\n" 8017 "multiline raw string literal xxxxxxxxxxxxxx\n" 8018 ")x\");", 8019 getGoogleStyleWithColumns(20))); 8020 EXPECT_EQ("fffffffffff(R\"x(\n" 8021 "multiline raw string literal xxxxxxxxxxxxxx\n" 8022 ")x\" + bbbbbb);", 8023 format("fffffffffff(R\"x(\n" 8024 "multiline raw string literal xxxxxxxxxxxxxx\n" 8025 ")x\" + bbbbbb);", 8026 getGoogleStyleWithColumns(20))); 8027 EXPECT_EQ("fffffffffff(\n" 8028 " R\"x(\n" 8029 "multiline raw string literal xxxxxxxxxxxxxx\n" 8030 ")x\" +\n" 8031 " bbbbbb);", 8032 format("fffffffffff(\n" 8033 " R\"x(\n" 8034 "multiline raw string literal xxxxxxxxxxxxxx\n" 8035 ")x\" + bbbbbb);", 8036 getGoogleStyleWithColumns(20))); 8037 } 8038 8039 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 8040 verifyFormat("string a = \"unterminated;"); 8041 EXPECT_EQ("function(\"unterminated,\n" 8042 " OtherParameter);", 8043 format("function( \"unterminated,\n" 8044 " OtherParameter);")); 8045 } 8046 8047 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 8048 FormatStyle Style = getLLVMStyle(); 8049 Style.Standard = FormatStyle::LS_Cpp03; 8050 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 8051 format("#define x(_a) printf(\"foo\"_a);", Style)); 8052 } 8053 8054 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 8055 8056 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 8057 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 8058 " \"ddeeefff\");", 8059 format("someFunction(\"aaabbbcccdddeeefff\");", 8060 getLLVMStyleWithColumns(25))); 8061 EXPECT_EQ("someFunction1234567890(\n" 8062 " \"aaabbbcccdddeeefff\");", 8063 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8064 getLLVMStyleWithColumns(26))); 8065 EXPECT_EQ("someFunction1234567890(\n" 8066 " \"aaabbbcccdddeeeff\"\n" 8067 " \"f\");", 8068 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8069 getLLVMStyleWithColumns(25))); 8070 EXPECT_EQ("someFunction1234567890(\n" 8071 " \"aaabbbcccdddeeeff\"\n" 8072 " \"f\");", 8073 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 8074 getLLVMStyleWithColumns(24))); 8075 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8076 " \"ddde \"\n" 8077 " \"efff\");", 8078 format("someFunction(\"aaabbbcc ddde efff\");", 8079 getLLVMStyleWithColumns(25))); 8080 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 8081 " \"ddeeefff\");", 8082 format("someFunction(\"aaabbbccc ddeeefff\");", 8083 getLLVMStyleWithColumns(25))); 8084 EXPECT_EQ("someFunction1234567890(\n" 8085 " \"aaabb \"\n" 8086 " \"cccdddeeefff\");", 8087 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 8088 getLLVMStyleWithColumns(25))); 8089 EXPECT_EQ("#define A \\\n" 8090 " string s = \\\n" 8091 " \"123456789\" \\\n" 8092 " \"0\"; \\\n" 8093 " int i;", 8094 format("#define A string s = \"1234567890\"; int i;", 8095 getLLVMStyleWithColumns(20))); 8096 // FIXME: Put additional penalties on breaking at non-whitespace locations. 8097 EXPECT_EQ("someFunction(\"aaabbbcc \"\n" 8098 " \"dddeeeff\"\n" 8099 " \"f\");", 8100 format("someFunction(\"aaabbbcc dddeeefff\");", 8101 getLLVMStyleWithColumns(25))); 8102 } 8103 8104 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 8105 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 8106 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 8107 EXPECT_EQ("\"test\"\n" 8108 "\"\\n\"", 8109 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 8110 EXPECT_EQ("\"tes\\\\\"\n" 8111 "\"n\"", 8112 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 8113 EXPECT_EQ("\"\\\\\\\\\"\n" 8114 "\"\\n\"", 8115 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 8116 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 8117 EXPECT_EQ("\"\\uff01\"\n" 8118 "\"test\"", 8119 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 8120 EXPECT_EQ("\"\\Uff01ff02\"", 8121 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 8122 EXPECT_EQ("\"\\x000000000001\"\n" 8123 "\"next\"", 8124 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 8125 EXPECT_EQ("\"\\x000000000001next\"", 8126 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 8127 EXPECT_EQ("\"\\x000000000001\"", 8128 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 8129 EXPECT_EQ("\"test\"\n" 8130 "\"\\000000\"\n" 8131 "\"000001\"", 8132 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 8133 EXPECT_EQ("\"test\\000\"\n" 8134 "\"00000000\"\n" 8135 "\"1\"", 8136 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 8137 } 8138 8139 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 8140 verifyFormat("void f() {\n" 8141 " return g() {}\n" 8142 " void h() {}"); 8143 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 8144 "g();\n" 8145 "}"); 8146 } 8147 8148 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 8149 verifyFormat( 8150 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 8151 } 8152 8153 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 8154 verifyFormat("class X {\n" 8155 " void f() {\n" 8156 " }\n" 8157 "};", 8158 getLLVMStyleWithColumns(12)); 8159 } 8160 8161 TEST_F(FormatTest, ConfigurableIndentWidth) { 8162 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 8163 EightIndent.IndentWidth = 8; 8164 EightIndent.ContinuationIndentWidth = 8; 8165 verifyFormat("void f() {\n" 8166 " someFunction();\n" 8167 " if (true) {\n" 8168 " f();\n" 8169 " }\n" 8170 "}", 8171 EightIndent); 8172 verifyFormat("class X {\n" 8173 " void f() {\n" 8174 " }\n" 8175 "};", 8176 EightIndent); 8177 verifyFormat("int x[] = {\n" 8178 " call(),\n" 8179 " call()};", 8180 EightIndent); 8181 } 8182 8183 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 8184 verifyFormat("double\n" 8185 "f();", 8186 getLLVMStyleWithColumns(8)); 8187 } 8188 8189 TEST_F(FormatTest, ConfigurableUseOfTab) { 8190 FormatStyle Tab = getLLVMStyleWithColumns(42); 8191 Tab.IndentWidth = 8; 8192 Tab.UseTab = FormatStyle::UT_Always; 8193 Tab.AlignEscapedNewlinesLeft = true; 8194 8195 EXPECT_EQ("if (aaaaaaaa && // q\n" 8196 " bb)\t\t// w\n" 8197 "\t;", 8198 format("if (aaaaaaaa &&// q\n" 8199 "bb)// w\n" 8200 ";", 8201 Tab)); 8202 EXPECT_EQ("if (aaa && bbb) // w\n" 8203 "\t;", 8204 format("if(aaa&&bbb)// w\n" 8205 ";", 8206 Tab)); 8207 8208 verifyFormat("class X {\n" 8209 "\tvoid f() {\n" 8210 "\t\tsomeFunction(parameter1,\n" 8211 "\t\t\t parameter2);\n" 8212 "\t}\n" 8213 "};", 8214 Tab); 8215 verifyFormat("#define A \\\n" 8216 "\tvoid f() { \\\n" 8217 "\t\tsomeFunction( \\\n" 8218 "\t\t parameter1, \\\n" 8219 "\t\t parameter2); \\\n" 8220 "\t}", 8221 Tab); 8222 EXPECT_EQ("void f() {\n" 8223 "\tf();\n" 8224 "\tg();\n" 8225 "}", 8226 format("void f() {\n" 8227 "\tf();\n" 8228 "\tg();\n" 8229 "}", 8230 0, 0, Tab)); 8231 EXPECT_EQ("void f() {\n" 8232 "\tf();\n" 8233 "\tg();\n" 8234 "}", 8235 format("void f() {\n" 8236 "\tf();\n" 8237 "\tg();\n" 8238 "}", 8239 16, 0, Tab)); 8240 EXPECT_EQ("void f() {\n" 8241 " \tf();\n" 8242 "\tg();\n" 8243 "}", 8244 format("void f() {\n" 8245 " \tf();\n" 8246 " \tg();\n" 8247 "}", 8248 21, 0, Tab)); 8249 8250 Tab.TabWidth = 4; 8251 Tab.IndentWidth = 8; 8252 verifyFormat("class TabWidth4Indent8 {\n" 8253 "\t\tvoid f() {\n" 8254 "\t\t\t\tsomeFunction(parameter1,\n" 8255 "\t\t\t\t\t\t\t parameter2);\n" 8256 "\t\t}\n" 8257 "};", 8258 Tab); 8259 8260 Tab.TabWidth = 4; 8261 Tab.IndentWidth = 4; 8262 verifyFormat("class TabWidth4Indent4 {\n" 8263 "\tvoid f() {\n" 8264 "\t\tsomeFunction(parameter1,\n" 8265 "\t\t\t\t\t parameter2);\n" 8266 "\t}\n" 8267 "};", 8268 Tab); 8269 8270 Tab.TabWidth = 8; 8271 Tab.IndentWidth = 4; 8272 verifyFormat("class TabWidth8Indent4 {\n" 8273 " void f() {\n" 8274 "\tsomeFunction(parameter1,\n" 8275 "\t\t parameter2);\n" 8276 " }\n" 8277 "};", 8278 Tab); 8279 8280 Tab.TabWidth = 8; 8281 Tab.IndentWidth = 8; 8282 EXPECT_EQ("/*\n" 8283 "\t a\t\tcomment\n" 8284 "\t in multiple lines\n" 8285 " */", 8286 format(" /*\t \t \n" 8287 " \t \t a\t\tcomment\t \t\n" 8288 " \t \t in multiple lines\t\n" 8289 " \t */", 8290 Tab)); 8291 8292 Tab.UseTab = FormatStyle::UT_ForIndentation; 8293 verifyFormat("{\n" 8294 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8295 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8296 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8297 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8298 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8299 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 8300 "};", 8301 Tab); 8302 verifyFormat("enum A {\n" 8303 "\ta1, // Force multiple lines\n" 8304 "\ta2,\n" 8305 "\ta3\n" 8306 "};", 8307 Tab); 8308 EXPECT_EQ("if (aaaaaaaa && // q\n" 8309 " bb) // w\n" 8310 "\t;", 8311 format("if (aaaaaaaa &&// q\n" 8312 "bb)// w\n" 8313 ";", 8314 Tab)); 8315 verifyFormat("class X {\n" 8316 "\tvoid f() {\n" 8317 "\t\tsomeFunction(parameter1,\n" 8318 "\t\t parameter2);\n" 8319 "\t}\n" 8320 "};", 8321 Tab); 8322 verifyFormat("{\n" 8323 "\tQ({\n" 8324 "\t\tint a;\n" 8325 "\t\tsomeFunction(aaaaaaaa,\n" 8326 "\t\t bbbbbbb);\n" 8327 "\t}, p);\n" 8328 "}", 8329 Tab); 8330 EXPECT_EQ("{\n" 8331 "\t/* aaaa\n" 8332 "\t bbbb */\n" 8333 "}", 8334 format("{\n" 8335 "/* aaaa\n" 8336 " bbbb */\n" 8337 "}", 8338 Tab)); 8339 EXPECT_EQ("{\n" 8340 "\t/*\n" 8341 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8342 "\t bbbbbbbbbbbbb\n" 8343 "\t*/\n" 8344 "}", 8345 format("{\n" 8346 "/*\n" 8347 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8348 "*/\n" 8349 "}", 8350 Tab)); 8351 EXPECT_EQ("{\n" 8352 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8353 "\t// bbbbbbbbbbbbb\n" 8354 "}", 8355 format("{\n" 8356 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8357 "}", 8358 Tab)); 8359 EXPECT_EQ("{\n" 8360 "\t/*\n" 8361 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8362 "\t bbbbbbbbbbbbb\n" 8363 "\t*/\n" 8364 "}", 8365 format("{\n" 8366 "\t/*\n" 8367 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 8368 "\t*/\n" 8369 "}", 8370 Tab)); 8371 EXPECT_EQ("{\n" 8372 "\t/*\n" 8373 "\n" 8374 "\t*/\n" 8375 "}", 8376 format("{\n" 8377 "\t/*\n" 8378 "\n" 8379 "\t*/\n" 8380 "}", 8381 Tab)); 8382 EXPECT_EQ("{\n" 8383 "\t/*\n" 8384 " asdf\n" 8385 "\t*/\n" 8386 "}", 8387 format("{\n" 8388 "\t/*\n" 8389 " asdf\n" 8390 "\t*/\n" 8391 "}", 8392 Tab)); 8393 8394 Tab.UseTab = FormatStyle::UT_Never; 8395 EXPECT_EQ("/*\n" 8396 " a\t\tcomment\n" 8397 " in multiple lines\n" 8398 " */", 8399 format(" /*\t \t \n" 8400 " \t \t a\t\tcomment\t \t\n" 8401 " \t \t in multiple lines\t\n" 8402 " \t */", 8403 Tab)); 8404 EXPECT_EQ("/* some\n" 8405 " comment */", 8406 format(" \t \t /* some\n" 8407 " \t \t comment */", 8408 Tab)); 8409 EXPECT_EQ("int a; /* some\n" 8410 " comment */", 8411 format(" \t \t int a; /* some\n" 8412 " \t \t comment */", 8413 Tab)); 8414 8415 EXPECT_EQ("int a; /* some\n" 8416 "comment */", 8417 format(" \t \t int\ta; /* some\n" 8418 " \t \t comment */", 8419 Tab)); 8420 EXPECT_EQ("f(\"\t\t\"); /* some\n" 8421 " comment */", 8422 format(" \t \t f(\"\t\t\"); /* some\n" 8423 " \t \t comment */", 8424 Tab)); 8425 EXPECT_EQ("{\n" 8426 " /*\n" 8427 " * Comment\n" 8428 " */\n" 8429 " int i;\n" 8430 "}", 8431 format("{\n" 8432 "\t/*\n" 8433 "\t * Comment\n" 8434 "\t */\n" 8435 "\t int i;\n" 8436 "}")); 8437 } 8438 8439 TEST_F(FormatTest, CalculatesOriginalColumn) { 8440 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8441 "q\"; /* some\n" 8442 " comment */", 8443 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8444 "q\"; /* some\n" 8445 " comment */", 8446 getLLVMStyle())); 8447 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8448 "/* some\n" 8449 " comment */", 8450 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 8451 " /* some\n" 8452 " comment */", 8453 getLLVMStyle())); 8454 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8455 "qqq\n" 8456 "/* some\n" 8457 " comment */", 8458 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8459 "qqq\n" 8460 " /* some\n" 8461 " comment */", 8462 getLLVMStyle())); 8463 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8464 "wwww; /* some\n" 8465 " comment */", 8466 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 8467 "wwww; /* some\n" 8468 " comment */", 8469 getLLVMStyle())); 8470 } 8471 8472 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 8473 FormatStyle NoSpace = getLLVMStyle(); 8474 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 8475 8476 verifyFormat("while(true)\n" 8477 " continue;", 8478 NoSpace); 8479 verifyFormat("for(;;)\n" 8480 " continue;", 8481 NoSpace); 8482 verifyFormat("if(true)\n" 8483 " f();\n" 8484 "else if(true)\n" 8485 " f();", 8486 NoSpace); 8487 verifyFormat("do {\n" 8488 " do_something();\n" 8489 "} while(something());", 8490 NoSpace); 8491 verifyFormat("switch(x) {\n" 8492 "default:\n" 8493 " break;\n" 8494 "}", 8495 NoSpace); 8496 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 8497 verifyFormat("size_t x = sizeof(x);", NoSpace); 8498 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 8499 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 8500 verifyFormat("alignas(128) char a[128];", NoSpace); 8501 verifyFormat("size_t x = alignof(MyType);", NoSpace); 8502 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 8503 verifyFormat("int f() throw(Deprecated);", NoSpace); 8504 verifyFormat("typedef void (*cb)(int);", NoSpace); 8505 8506 FormatStyle Space = getLLVMStyle(); 8507 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 8508 8509 verifyFormat("int f ();", Space); 8510 verifyFormat("void f (int a, T b) {\n" 8511 " while (true)\n" 8512 " continue;\n" 8513 "}", 8514 Space); 8515 verifyFormat("if (true)\n" 8516 " f ();\n" 8517 "else if (true)\n" 8518 " f ();", 8519 Space); 8520 verifyFormat("do {\n" 8521 " do_something ();\n" 8522 "} while (something ());", 8523 Space); 8524 verifyFormat("switch (x) {\n" 8525 "default:\n" 8526 " break;\n" 8527 "}", 8528 Space); 8529 verifyFormat("A::A () : a (1) {}", Space); 8530 verifyFormat("void f () __attribute__ ((asdf));", Space); 8531 verifyFormat("*(&a + 1);\n" 8532 "&((&a)[1]);\n" 8533 "a[(b + c) * d];\n" 8534 "(((a + 1) * 2) + 3) * 4;", 8535 Space); 8536 verifyFormat("#define A(x) x", Space); 8537 verifyFormat("#define A (x) x", Space); 8538 verifyFormat("#if defined(x)\n" 8539 "#endif", 8540 Space); 8541 verifyFormat("auto i = std::make_unique<int> (5);", Space); 8542 verifyFormat("size_t x = sizeof (x);", Space); 8543 verifyFormat("auto f (int x) -> decltype (x);", Space); 8544 verifyFormat("int f (T x) noexcept (x.create ());", Space); 8545 verifyFormat("alignas (128) char a[128];", Space); 8546 verifyFormat("size_t x = alignof (MyType);", Space); 8547 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 8548 verifyFormat("int f () throw (Deprecated);", Space); 8549 verifyFormat("typedef void (*cb) (int);", Space); 8550 } 8551 8552 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 8553 FormatStyle Spaces = getLLVMStyle(); 8554 8555 Spaces.SpacesInParentheses = true; 8556 verifyFormat("call( x, y, z );", Spaces); 8557 verifyFormat("call();", Spaces); 8558 verifyFormat("std::function<void( int, int )> callback;", Spaces); 8559 verifyFormat("while ( (bool)1 )\n" 8560 " continue;", 8561 Spaces); 8562 verifyFormat("for ( ;; )\n" 8563 " continue;", 8564 Spaces); 8565 verifyFormat("if ( true )\n" 8566 " f();\n" 8567 "else if ( true )\n" 8568 " f();", 8569 Spaces); 8570 verifyFormat("do {\n" 8571 " do_something( (int)i );\n" 8572 "} while ( something() );", 8573 Spaces); 8574 verifyFormat("switch ( x ) {\n" 8575 "default:\n" 8576 " break;\n" 8577 "}", 8578 Spaces); 8579 8580 Spaces.SpacesInParentheses = false; 8581 Spaces.SpacesInCStyleCastParentheses = true; 8582 verifyFormat("Type *A = ( Type * )P;", Spaces); 8583 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 8584 verifyFormat("x = ( int32 )y;", Spaces); 8585 verifyFormat("int a = ( int )(2.0f);", Spaces); 8586 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 8587 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 8588 verifyFormat("#define x (( int )-1)", Spaces); 8589 8590 // Run the first set of tests again with: 8591 Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true; 8592 Spaces.SpacesInCStyleCastParentheses = true; 8593 verifyFormat("call(x, y, z);", Spaces); 8594 verifyFormat("call( );", Spaces); 8595 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8596 verifyFormat("while (( bool )1)\n" 8597 " continue;", 8598 Spaces); 8599 verifyFormat("for (;;)\n" 8600 " continue;", 8601 Spaces); 8602 verifyFormat("if (true)\n" 8603 " f( );\n" 8604 "else if (true)\n" 8605 " f( );", 8606 Spaces); 8607 verifyFormat("do {\n" 8608 " do_something(( int )i);\n" 8609 "} while (something( ));", 8610 Spaces); 8611 verifyFormat("switch (x) {\n" 8612 "default:\n" 8613 " break;\n" 8614 "}", 8615 Spaces); 8616 8617 // Run the first set of tests again with: 8618 Spaces.SpaceAfterCStyleCast = true; 8619 verifyFormat("call(x, y, z);", Spaces); 8620 verifyFormat("call( );", Spaces); 8621 verifyFormat("std::function<void(int, int)> callback;", Spaces); 8622 verifyFormat("while (( bool ) 1)\n" 8623 " continue;", 8624 Spaces); 8625 verifyFormat("for (;;)\n" 8626 " continue;", 8627 Spaces); 8628 verifyFormat("if (true)\n" 8629 " f( );\n" 8630 "else if (true)\n" 8631 " f( );", 8632 Spaces); 8633 verifyFormat("do {\n" 8634 " do_something(( int ) i);\n" 8635 "} while (something( ));", 8636 Spaces); 8637 verifyFormat("switch (x) {\n" 8638 "default:\n" 8639 " break;\n" 8640 "}", 8641 Spaces); 8642 8643 // Run subset of tests again with: 8644 Spaces.SpacesInCStyleCastParentheses = false; 8645 Spaces.SpaceAfterCStyleCast = true; 8646 verifyFormat("while ((bool) 1)\n" 8647 " continue;", 8648 Spaces); 8649 verifyFormat("do {\n" 8650 " do_something((int) i);\n" 8651 "} while (something( ));", 8652 Spaces); 8653 } 8654 8655 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 8656 verifyFormat("int a[5];"); 8657 verifyFormat("a[3] += 42;"); 8658 8659 FormatStyle Spaces = getLLVMStyle(); 8660 Spaces.SpacesInSquareBrackets = true; 8661 // Lambdas unchanged. 8662 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 8663 verifyFormat("return [i, args...] {};", Spaces); 8664 8665 // Not lambdas. 8666 verifyFormat("int a[ 5 ];", Spaces); 8667 verifyFormat("a[ 3 ] += 42;", Spaces); 8668 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 8669 verifyFormat("double &operator[](int i) { return 0; }\n" 8670 "int i;", 8671 Spaces); 8672 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 8673 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 8674 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 8675 } 8676 8677 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 8678 verifyFormat("int a = 5;"); 8679 verifyFormat("a += 42;"); 8680 verifyFormat("a or_eq 8;"); 8681 8682 FormatStyle Spaces = getLLVMStyle(); 8683 Spaces.SpaceBeforeAssignmentOperators = false; 8684 verifyFormat("int a= 5;", Spaces); 8685 verifyFormat("a+= 42;", Spaces); 8686 verifyFormat("a or_eq 8;", Spaces); 8687 } 8688 8689 TEST_F(FormatTest, AlignConsecutiveAssignments) { 8690 FormatStyle Alignment = getLLVMStyle(); 8691 Alignment.AlignConsecutiveAssignments = false; 8692 verifyFormat("int a = 5;\n" 8693 "int oneTwoThree = 123;", 8694 Alignment); 8695 verifyFormat("int a = 5;\n" 8696 "int oneTwoThree = 123;", 8697 Alignment); 8698 8699 Alignment.AlignConsecutiveAssignments = true; 8700 verifyFormat("int a = 5;\n" 8701 "int oneTwoThree = 123;", 8702 Alignment); 8703 verifyFormat("int a = method();\n" 8704 "int oneTwoThree = 133;", 8705 Alignment); 8706 verifyFormat("a &= 5;\n" 8707 "bcd *= 5;\n" 8708 "ghtyf += 5;\n" 8709 "dvfvdb -= 5;\n" 8710 "a /= 5;\n" 8711 "vdsvsv %= 5;\n" 8712 "sfdbddfbdfbb ^= 5;\n" 8713 "dvsdsv |= 5;\n" 8714 "int dsvvdvsdvvv = 123;", 8715 Alignment); 8716 verifyFormat("int i = 1, j = 10;\n" 8717 "something = 2000;", 8718 Alignment); 8719 verifyFormat("something = 2000;\n" 8720 "int i = 1, j = 10;\n", 8721 Alignment); 8722 verifyFormat("something = 2000;\n" 8723 "another = 911;\n" 8724 "int i = 1, j = 10;\n" 8725 "oneMore = 1;\n" 8726 "i = 2;", 8727 Alignment); 8728 verifyFormat("int a = 5;\n" 8729 "int one = 1;\n" 8730 "method();\n" 8731 "int oneTwoThree = 123;\n" 8732 "int oneTwo = 12;", 8733 Alignment); 8734 verifyFormat("int oneTwoThree = 123; // comment\n" 8735 "int oneTwo = 12; // comment", 8736 Alignment); 8737 EXPECT_EQ("int a = 5;\n" 8738 "\n" 8739 "int oneTwoThree = 123;", 8740 format("int a = 5;\n" 8741 "\n" 8742 "int oneTwoThree= 123;", 8743 Alignment)); 8744 EXPECT_EQ("int a = 5;\n" 8745 "int one = 1;\n" 8746 "\n" 8747 "int oneTwoThree = 123;", 8748 format("int a = 5;\n" 8749 "int one = 1;\n" 8750 "\n" 8751 "int oneTwoThree = 123;", 8752 Alignment)); 8753 EXPECT_EQ("int a = 5;\n" 8754 "int one = 1;\n" 8755 "\n" 8756 "int oneTwoThree = 123;\n" 8757 "int oneTwo = 12;", 8758 format("int a = 5;\n" 8759 "int one = 1;\n" 8760 "\n" 8761 "int oneTwoThree = 123;\n" 8762 "int oneTwo = 12;", 8763 Alignment)); 8764 Alignment.AlignEscapedNewlinesLeft = true; 8765 verifyFormat("#define A \\\n" 8766 " int aaaa = 12; \\\n" 8767 " int b = 23; \\\n" 8768 " int ccc = 234; \\\n" 8769 " int dddddddddd = 2345;", 8770 Alignment); 8771 Alignment.AlignEscapedNewlinesLeft = false; 8772 verifyFormat("#define A " 8773 " \\\n" 8774 " int aaaa = 12; " 8775 " \\\n" 8776 " int b = 23; " 8777 " \\\n" 8778 " int ccc = 234; " 8779 " \\\n" 8780 " int dddddddddd = 2345;", 8781 Alignment); 8782 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 8783 "k = 4, int l = 5,\n" 8784 " int m = 6) {\n" 8785 " int j = 10;\n" 8786 " otherThing = 1;\n" 8787 "}", 8788 Alignment); 8789 verifyFormat("void SomeFunction(int parameter = 0) {\n" 8790 " int i = 1;\n" 8791 " int j = 2;\n" 8792 " int big = 10000;\n" 8793 "}", 8794 Alignment); 8795 verifyFormat("class C {\n" 8796 "public:\n" 8797 " int i = 1;\n" 8798 " virtual void f() = 0;\n" 8799 "};", 8800 Alignment); 8801 verifyFormat("int i = 1;\n" 8802 "if (SomeType t = getSomething()) {\n" 8803 "}\n" 8804 "int j = 2;\n" 8805 "int big = 10000;", 8806 Alignment); 8807 verifyFormat("int j = 7;\n" 8808 "for (int k = 0; k < N; ++k) {\n" 8809 "}\n" 8810 "int j = 2;\n" 8811 "int big = 10000;\n" 8812 "}", 8813 Alignment); 8814 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8815 verifyFormat("int i = 1;\n" 8816 "LooooooooooongType loooooooooooooooooooooongVariable\n" 8817 " = someLooooooooooooooooongFunction();\n" 8818 "int j = 2;", 8819 Alignment); 8820 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8821 verifyFormat("int i = 1;\n" 8822 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 8823 " someLooooooooooooooooongFunction();\n" 8824 "int j = 2;", 8825 Alignment); 8826 // FIXME: Should align all three assignments 8827 verifyFormat( 8828 "int i = 1;\n" 8829 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 8830 " loooooooooooooooooooooongParameterB);\n" 8831 "int j = 2;", 8832 Alignment); 8833 } 8834 8835 TEST_F(FormatTest, LinuxBraceBreaking) { 8836 FormatStyle LinuxBraceStyle = getLLVMStyle(); 8837 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 8838 verifyFormat("namespace a\n" 8839 "{\n" 8840 "class A\n" 8841 "{\n" 8842 " void f()\n" 8843 " {\n" 8844 " if (true) {\n" 8845 " a();\n" 8846 " b();\n" 8847 " }\n" 8848 " }\n" 8849 " void g() { return; }\n" 8850 "};\n" 8851 "struct B {\n" 8852 " int x;\n" 8853 "};\n" 8854 "}\n", 8855 LinuxBraceStyle); 8856 verifyFormat("enum X {\n" 8857 " Y = 0,\n" 8858 "}\n", 8859 LinuxBraceStyle); 8860 verifyFormat("struct S {\n" 8861 " int Type;\n" 8862 " union {\n" 8863 " int x;\n" 8864 " double y;\n" 8865 " } Value;\n" 8866 " class C\n" 8867 " {\n" 8868 " MyFavoriteType Value;\n" 8869 " } Class;\n" 8870 "}\n", 8871 LinuxBraceStyle); 8872 } 8873 8874 TEST_F(FormatTest, StroustrupBraceBreaking) { 8875 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 8876 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8877 verifyFormat("namespace a {\n" 8878 "class A {\n" 8879 " void f()\n" 8880 " {\n" 8881 " if (true) {\n" 8882 " a();\n" 8883 " b();\n" 8884 " }\n" 8885 " }\n" 8886 " void g() { return; }\n" 8887 "};\n" 8888 "struct B {\n" 8889 " int x;\n" 8890 "};\n" 8891 "}\n", 8892 StroustrupBraceStyle); 8893 8894 verifyFormat("void foo()\n" 8895 "{\n" 8896 " if (a) {\n" 8897 " a();\n" 8898 " }\n" 8899 " else {\n" 8900 " b();\n" 8901 " }\n" 8902 "}\n", 8903 StroustrupBraceStyle); 8904 8905 verifyFormat("#ifdef _DEBUG\n" 8906 "int foo(int i = 0)\n" 8907 "#else\n" 8908 "int foo(int i = 5)\n" 8909 "#endif\n" 8910 "{\n" 8911 " return i;\n" 8912 "}", 8913 StroustrupBraceStyle); 8914 8915 verifyFormat("void foo() {}\n" 8916 "void bar()\n" 8917 "#ifdef _DEBUG\n" 8918 "{\n" 8919 " foo();\n" 8920 "}\n" 8921 "#else\n" 8922 "{\n" 8923 "}\n" 8924 "#endif", 8925 StroustrupBraceStyle); 8926 8927 verifyFormat("void foobar() { int i = 5; }\n" 8928 "#ifdef _DEBUG\n" 8929 "void bar() {}\n" 8930 "#else\n" 8931 "void bar() { foobar(); }\n" 8932 "#endif", 8933 StroustrupBraceStyle); 8934 } 8935 8936 TEST_F(FormatTest, AllmanBraceBreaking) { 8937 FormatStyle AllmanBraceStyle = getLLVMStyle(); 8938 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 8939 verifyFormat("namespace a\n" 8940 "{\n" 8941 "class A\n" 8942 "{\n" 8943 " void f()\n" 8944 " {\n" 8945 " if (true)\n" 8946 " {\n" 8947 " a();\n" 8948 " b();\n" 8949 " }\n" 8950 " }\n" 8951 " void g() { return; }\n" 8952 "};\n" 8953 "struct B\n" 8954 "{\n" 8955 " int x;\n" 8956 "};\n" 8957 "}", 8958 AllmanBraceStyle); 8959 8960 verifyFormat("void f()\n" 8961 "{\n" 8962 " if (true)\n" 8963 " {\n" 8964 " a();\n" 8965 " }\n" 8966 " else if (false)\n" 8967 " {\n" 8968 " b();\n" 8969 " }\n" 8970 " else\n" 8971 " {\n" 8972 " c();\n" 8973 " }\n" 8974 "}\n", 8975 AllmanBraceStyle); 8976 8977 verifyFormat("void f()\n" 8978 "{\n" 8979 " for (int i = 0; i < 10; ++i)\n" 8980 " {\n" 8981 " a();\n" 8982 " }\n" 8983 " while (false)\n" 8984 " {\n" 8985 " b();\n" 8986 " }\n" 8987 " do\n" 8988 " {\n" 8989 " c();\n" 8990 " } while (false)\n" 8991 "}\n", 8992 AllmanBraceStyle); 8993 8994 verifyFormat("void f(int a)\n" 8995 "{\n" 8996 " switch (a)\n" 8997 " {\n" 8998 " case 0:\n" 8999 " break;\n" 9000 " case 1:\n" 9001 " {\n" 9002 " break;\n" 9003 " }\n" 9004 " case 2:\n" 9005 " {\n" 9006 " }\n" 9007 " break;\n" 9008 " default:\n" 9009 " break;\n" 9010 " }\n" 9011 "}\n", 9012 AllmanBraceStyle); 9013 9014 verifyFormat("enum X\n" 9015 "{\n" 9016 " Y = 0,\n" 9017 "}\n", 9018 AllmanBraceStyle); 9019 verifyFormat("enum X\n" 9020 "{\n" 9021 " Y = 0\n" 9022 "}\n", 9023 AllmanBraceStyle); 9024 9025 verifyFormat("@interface BSApplicationController ()\n" 9026 "{\n" 9027 "@private\n" 9028 " id _extraIvar;\n" 9029 "}\n" 9030 "@end\n", 9031 AllmanBraceStyle); 9032 9033 verifyFormat("#ifdef _DEBUG\n" 9034 "int foo(int i = 0)\n" 9035 "#else\n" 9036 "int foo(int i = 5)\n" 9037 "#endif\n" 9038 "{\n" 9039 " return i;\n" 9040 "}", 9041 AllmanBraceStyle); 9042 9043 verifyFormat("void foo() {}\n" 9044 "void bar()\n" 9045 "#ifdef _DEBUG\n" 9046 "{\n" 9047 " foo();\n" 9048 "}\n" 9049 "#else\n" 9050 "{\n" 9051 "}\n" 9052 "#endif", 9053 AllmanBraceStyle); 9054 9055 verifyFormat("void foobar() { int i = 5; }\n" 9056 "#ifdef _DEBUG\n" 9057 "void bar() {}\n" 9058 "#else\n" 9059 "void bar() { foobar(); }\n" 9060 "#endif", 9061 AllmanBraceStyle); 9062 9063 // This shouldn't affect ObjC blocks.. 9064 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 9065 " // ...\n" 9066 " int i;\n" 9067 "}];", 9068 AllmanBraceStyle); 9069 verifyFormat("void (^block)(void) = ^{\n" 9070 " // ...\n" 9071 " int i;\n" 9072 "};", 9073 AllmanBraceStyle); 9074 // .. or dict literals. 9075 verifyFormat("void f()\n" 9076 "{\n" 9077 " [object someMethod:@{ @\"a\" : @\"b\" }];\n" 9078 "}", 9079 AllmanBraceStyle); 9080 verifyFormat("int f()\n" 9081 "{ // comment\n" 9082 " return 42;\n" 9083 "}", 9084 AllmanBraceStyle); 9085 9086 AllmanBraceStyle.ColumnLimit = 19; 9087 verifyFormat("void f() { int i; }", AllmanBraceStyle); 9088 AllmanBraceStyle.ColumnLimit = 18; 9089 verifyFormat("void f()\n" 9090 "{\n" 9091 " int i;\n" 9092 "}", 9093 AllmanBraceStyle); 9094 AllmanBraceStyle.ColumnLimit = 80; 9095 9096 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 9097 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true; 9098 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 9099 verifyFormat("void f(bool b)\n" 9100 "{\n" 9101 " if (b)\n" 9102 " {\n" 9103 " return;\n" 9104 " }\n" 9105 "}\n", 9106 BreakBeforeBraceShortIfs); 9107 verifyFormat("void f(bool b)\n" 9108 "{\n" 9109 " if (b) return;\n" 9110 "}\n", 9111 BreakBeforeBraceShortIfs); 9112 verifyFormat("void f(bool b)\n" 9113 "{\n" 9114 " while (b)\n" 9115 " {\n" 9116 " return;\n" 9117 " }\n" 9118 "}\n", 9119 BreakBeforeBraceShortIfs); 9120 } 9121 9122 TEST_F(FormatTest, GNUBraceBreaking) { 9123 FormatStyle GNUBraceStyle = getLLVMStyle(); 9124 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 9125 verifyFormat("namespace a\n" 9126 "{\n" 9127 "class A\n" 9128 "{\n" 9129 " void f()\n" 9130 " {\n" 9131 " int a;\n" 9132 " {\n" 9133 " int b;\n" 9134 " }\n" 9135 " if (true)\n" 9136 " {\n" 9137 " a();\n" 9138 " b();\n" 9139 " }\n" 9140 " }\n" 9141 " void g() { return; }\n" 9142 "}\n" 9143 "}", 9144 GNUBraceStyle); 9145 9146 verifyFormat("void f()\n" 9147 "{\n" 9148 " if (true)\n" 9149 " {\n" 9150 " a();\n" 9151 " }\n" 9152 " else if (false)\n" 9153 " {\n" 9154 " b();\n" 9155 " }\n" 9156 " else\n" 9157 " {\n" 9158 " c();\n" 9159 " }\n" 9160 "}\n", 9161 GNUBraceStyle); 9162 9163 verifyFormat("void f()\n" 9164 "{\n" 9165 " for (int i = 0; i < 10; ++i)\n" 9166 " {\n" 9167 " a();\n" 9168 " }\n" 9169 " while (false)\n" 9170 " {\n" 9171 " b();\n" 9172 " }\n" 9173 " do\n" 9174 " {\n" 9175 " c();\n" 9176 " }\n" 9177 " while (false);\n" 9178 "}\n", 9179 GNUBraceStyle); 9180 9181 verifyFormat("void f(int a)\n" 9182 "{\n" 9183 " switch (a)\n" 9184 " {\n" 9185 " case 0:\n" 9186 " break;\n" 9187 " case 1:\n" 9188 " {\n" 9189 " break;\n" 9190 " }\n" 9191 " case 2:\n" 9192 " {\n" 9193 " }\n" 9194 " break;\n" 9195 " default:\n" 9196 " break;\n" 9197 " }\n" 9198 "}\n", 9199 GNUBraceStyle); 9200 9201 verifyFormat("enum X\n" 9202 "{\n" 9203 " Y = 0,\n" 9204 "}\n", 9205 GNUBraceStyle); 9206 9207 verifyFormat("@interface BSApplicationController ()\n" 9208 "{\n" 9209 "@private\n" 9210 " id _extraIvar;\n" 9211 "}\n" 9212 "@end\n", 9213 GNUBraceStyle); 9214 9215 verifyFormat("#ifdef _DEBUG\n" 9216 "int foo(int i = 0)\n" 9217 "#else\n" 9218 "int foo(int i = 5)\n" 9219 "#endif\n" 9220 "{\n" 9221 " return i;\n" 9222 "}", 9223 GNUBraceStyle); 9224 9225 verifyFormat("void foo() {}\n" 9226 "void bar()\n" 9227 "#ifdef _DEBUG\n" 9228 "{\n" 9229 " foo();\n" 9230 "}\n" 9231 "#else\n" 9232 "{\n" 9233 "}\n" 9234 "#endif", 9235 GNUBraceStyle); 9236 9237 verifyFormat("void foobar() { int i = 5; }\n" 9238 "#ifdef _DEBUG\n" 9239 "void bar() {}\n" 9240 "#else\n" 9241 "void bar() { foobar(); }\n" 9242 "#endif", 9243 GNUBraceStyle); 9244 } 9245 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 9246 verifyFormat("void f() {\n" 9247 " try {\n" 9248 " } catch (const Exception &e) {\n" 9249 " }\n" 9250 "}\n", 9251 getLLVMStyle()); 9252 } 9253 9254 TEST_F(FormatTest, UnderstandsPragmas) { 9255 verifyFormat("#pragma omp reduction(| : var)"); 9256 verifyFormat("#pragma omp reduction(+ : var)"); 9257 9258 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 9259 "(including parentheses).", 9260 format("#pragma mark Any non-hyphenated or hyphenated string " 9261 "(including parentheses).")); 9262 } 9263 9264 TEST_F(FormatTest, UnderstandPragmaOption) { 9265 verifyFormat("#pragma option -C -A"); 9266 9267 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 9268 } 9269 9270 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 9271 for (size_t i = 1; i < Styles.size(); ++i) \ 9272 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \ 9273 << " differs from Style #0" 9274 9275 TEST_F(FormatTest, GetsPredefinedStyleByName) { 9276 SmallVector<FormatStyle, 3> Styles; 9277 Styles.resize(3); 9278 9279 Styles[0] = getLLVMStyle(); 9280 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 9281 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 9282 EXPECT_ALL_STYLES_EQUAL(Styles); 9283 9284 Styles[0] = getGoogleStyle(); 9285 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 9286 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 9287 EXPECT_ALL_STYLES_EQUAL(Styles); 9288 9289 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9290 EXPECT_TRUE( 9291 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 9292 EXPECT_TRUE( 9293 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 9294 EXPECT_ALL_STYLES_EQUAL(Styles); 9295 9296 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 9297 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 9298 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 9299 EXPECT_ALL_STYLES_EQUAL(Styles); 9300 9301 Styles[0] = getMozillaStyle(); 9302 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 9303 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 9304 EXPECT_ALL_STYLES_EQUAL(Styles); 9305 9306 Styles[0] = getWebKitStyle(); 9307 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 9308 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 9309 EXPECT_ALL_STYLES_EQUAL(Styles); 9310 9311 Styles[0] = getGNUStyle(); 9312 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 9313 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 9314 EXPECT_ALL_STYLES_EQUAL(Styles); 9315 9316 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 9317 } 9318 9319 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 9320 SmallVector<FormatStyle, 8> Styles; 9321 Styles.resize(2); 9322 9323 Styles[0] = getGoogleStyle(); 9324 Styles[1] = getLLVMStyle(); 9325 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9326 EXPECT_ALL_STYLES_EQUAL(Styles); 9327 9328 Styles.resize(5); 9329 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 9330 Styles[1] = getLLVMStyle(); 9331 Styles[1].Language = FormatStyle::LK_JavaScript; 9332 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 9333 9334 Styles[2] = getLLVMStyle(); 9335 Styles[2].Language = FormatStyle::LK_JavaScript; 9336 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 9337 "BasedOnStyle: Google", 9338 &Styles[2]).value()); 9339 9340 Styles[3] = getLLVMStyle(); 9341 Styles[3].Language = FormatStyle::LK_JavaScript; 9342 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 9343 "Language: JavaScript", 9344 &Styles[3]).value()); 9345 9346 Styles[4] = getLLVMStyle(); 9347 Styles[4].Language = FormatStyle::LK_JavaScript; 9348 EXPECT_EQ(0, parseConfiguration("---\n" 9349 "BasedOnStyle: LLVM\n" 9350 "IndentWidth: 123\n" 9351 "---\n" 9352 "BasedOnStyle: Google\n" 9353 "Language: JavaScript", 9354 &Styles[4]).value()); 9355 EXPECT_ALL_STYLES_EQUAL(Styles); 9356 } 9357 9358 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 9359 Style.FIELD = false; \ 9360 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 9361 EXPECT_TRUE(Style.FIELD); \ 9362 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 9363 EXPECT_FALSE(Style.FIELD); 9364 9365 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 9366 9367 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 9368 EXPECT_NE(VALUE, Style.FIELD); \ 9369 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 9370 EXPECT_EQ(VALUE, Style.FIELD) 9371 9372 TEST_F(FormatTest, ParsesConfigurationBools) { 9373 FormatStyle Style = {}; 9374 Style.Language = FormatStyle::LK_Cpp; 9375 CHECK_PARSE_BOOL(AlignAfterOpenBracket); 9376 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); 9377 CHECK_PARSE_BOOL(AlignOperands); 9378 CHECK_PARSE_BOOL(AlignTrailingComments); 9379 CHECK_PARSE_BOOL(AlignConsecutiveAssignments); 9380 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 9381 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); 9382 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 9383 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine); 9384 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 9385 CHECK_PARSE_BOOL(AlwaysBreakAfterDefinitionReturnType); 9386 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations); 9387 CHECK_PARSE_BOOL(BinPackParameters); 9388 CHECK_PARSE_BOOL(BinPackArguments); 9389 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 9390 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma); 9391 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 9392 CHECK_PARSE_BOOL(DerivePointerAlignment); 9393 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 9394 CHECK_PARSE_BOOL(IndentCaseLabels); 9395 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 9396 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 9397 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 9398 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 9399 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 9400 CHECK_PARSE_BOOL(SpacesInParentheses); 9401 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 9402 CHECK_PARSE_BOOL(SpacesInAngles); 9403 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 9404 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 9405 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 9406 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 9407 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 9408 } 9409 9410 #undef CHECK_PARSE_BOOL 9411 9412 TEST_F(FormatTest, ParsesConfiguration) { 9413 FormatStyle Style = {}; 9414 Style.Language = FormatStyle::LK_Cpp; 9415 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 9416 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 9417 ConstructorInitializerIndentWidth, 1234u); 9418 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 9419 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 9420 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 9421 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 9422 PenaltyBreakBeforeFirstCallParameter, 1234u); 9423 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 9424 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 9425 PenaltyReturnTypeOnItsOwnLine, 1234u); 9426 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 9427 SpacesBeforeTrailingComments, 1234u); 9428 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 9429 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 9430 9431 Style.PointerAlignment = FormatStyle::PAS_Middle; 9432 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 9433 FormatStyle::PAS_Left); 9434 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 9435 FormatStyle::PAS_Right); 9436 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 9437 FormatStyle::PAS_Middle); 9438 // For backward compatibility: 9439 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 9440 FormatStyle::PAS_Left); 9441 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 9442 FormatStyle::PAS_Right); 9443 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 9444 FormatStyle::PAS_Middle); 9445 9446 Style.Standard = FormatStyle::LS_Auto; 9447 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 9448 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11); 9449 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 9450 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 9451 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 9452 9453 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9454 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 9455 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 9456 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 9457 FormatStyle::BOS_None); 9458 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 9459 FormatStyle::BOS_All); 9460 // For backward compatibility: 9461 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 9462 FormatStyle::BOS_None); 9463 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 9464 FormatStyle::BOS_All); 9465 9466 Style.UseTab = FormatStyle::UT_ForIndentation; 9467 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 9468 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 9469 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 9470 // For backward compatibility: 9471 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 9472 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 9473 9474 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 9475 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 9476 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9477 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 9478 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 9479 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 9480 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 9481 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 9482 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9483 // For backward compatibility: 9484 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 9485 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 9486 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 9487 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 9488 9489 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 9490 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 9491 FormatStyle::SBPO_Never); 9492 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 9493 FormatStyle::SBPO_Always); 9494 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 9495 FormatStyle::SBPO_ControlStatements); 9496 // For backward compatibility: 9497 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 9498 FormatStyle::SBPO_Never); 9499 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 9500 FormatStyle::SBPO_ControlStatements); 9501 9502 Style.ColumnLimit = 123; 9503 FormatStyle BaseStyle = getLLVMStyle(); 9504 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 9505 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 9506 9507 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 9508 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 9509 FormatStyle::BS_Attach); 9510 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 9511 FormatStyle::BS_Linux); 9512 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 9513 FormatStyle::BS_Stroustrup); 9514 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 9515 FormatStyle::BS_Allman); 9516 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 9517 9518 Style.NamespaceIndentation = FormatStyle::NI_All; 9519 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 9520 FormatStyle::NI_None); 9521 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 9522 FormatStyle::NI_Inner); 9523 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 9524 FormatStyle::NI_All); 9525 9526 Style.ForEachMacros.clear(); 9527 std::vector<std::string> BoostForeach; 9528 BoostForeach.push_back("BOOST_FOREACH"); 9529 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 9530 std::vector<std::string> BoostAndQForeach; 9531 BoostAndQForeach.push_back("BOOST_FOREACH"); 9532 BoostAndQForeach.push_back("Q_FOREACH"); 9533 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 9534 BoostAndQForeach); 9535 } 9536 9537 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 9538 FormatStyle Style = {}; 9539 Style.Language = FormatStyle::LK_Cpp; 9540 CHECK_PARSE("Language: Cpp\n" 9541 "IndentWidth: 12", 9542 IndentWidth, 12u); 9543 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 9544 "IndentWidth: 34", 9545 &Style), 9546 ParseError::Unsuitable); 9547 EXPECT_EQ(12u, Style.IndentWidth); 9548 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9549 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9550 9551 Style.Language = FormatStyle::LK_JavaScript; 9552 CHECK_PARSE("Language: JavaScript\n" 9553 "IndentWidth: 12", 9554 IndentWidth, 12u); 9555 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 9556 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 9557 "IndentWidth: 34", 9558 &Style), 9559 ParseError::Unsuitable); 9560 EXPECT_EQ(23u, Style.IndentWidth); 9561 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 9562 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9563 9564 CHECK_PARSE("BasedOnStyle: LLVM\n" 9565 "IndentWidth: 67", 9566 IndentWidth, 67u); 9567 9568 CHECK_PARSE("---\n" 9569 "Language: JavaScript\n" 9570 "IndentWidth: 12\n" 9571 "---\n" 9572 "Language: Cpp\n" 9573 "IndentWidth: 34\n" 9574 "...\n", 9575 IndentWidth, 12u); 9576 9577 Style.Language = FormatStyle::LK_Cpp; 9578 CHECK_PARSE("---\n" 9579 "Language: JavaScript\n" 9580 "IndentWidth: 12\n" 9581 "---\n" 9582 "Language: Cpp\n" 9583 "IndentWidth: 34\n" 9584 "...\n", 9585 IndentWidth, 34u); 9586 CHECK_PARSE("---\n" 9587 "IndentWidth: 78\n" 9588 "---\n" 9589 "Language: JavaScript\n" 9590 "IndentWidth: 56\n" 9591 "...\n", 9592 IndentWidth, 78u); 9593 9594 Style.ColumnLimit = 123; 9595 Style.IndentWidth = 234; 9596 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 9597 Style.TabWidth = 345; 9598 EXPECT_FALSE(parseConfiguration("---\n" 9599 "IndentWidth: 456\n" 9600 "BreakBeforeBraces: Allman\n" 9601 "---\n" 9602 "Language: JavaScript\n" 9603 "IndentWidth: 111\n" 9604 "TabWidth: 111\n" 9605 "---\n" 9606 "Language: Cpp\n" 9607 "BreakBeforeBraces: Stroustrup\n" 9608 "TabWidth: 789\n" 9609 "...\n", 9610 &Style)); 9611 EXPECT_EQ(123u, Style.ColumnLimit); 9612 EXPECT_EQ(456u, Style.IndentWidth); 9613 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 9614 EXPECT_EQ(789u, Style.TabWidth); 9615 9616 EXPECT_EQ(parseConfiguration("---\n" 9617 "Language: JavaScript\n" 9618 "IndentWidth: 56\n" 9619 "---\n" 9620 "IndentWidth: 78\n" 9621 "...\n", 9622 &Style), 9623 ParseError::Error); 9624 EXPECT_EQ(parseConfiguration("---\n" 9625 "Language: JavaScript\n" 9626 "IndentWidth: 56\n" 9627 "---\n" 9628 "Language: JavaScript\n" 9629 "IndentWidth: 78\n" 9630 "...\n", 9631 &Style), 9632 ParseError::Error); 9633 9634 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 9635 } 9636 9637 #undef CHECK_PARSE 9638 9639 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 9640 FormatStyle Style = {}; 9641 Style.Language = FormatStyle::LK_JavaScript; 9642 Style.BreakBeforeTernaryOperators = true; 9643 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 9644 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9645 9646 Style.BreakBeforeTernaryOperators = true; 9647 EXPECT_EQ(0, parseConfiguration("---\n" 9648 "BasedOnStyle: Google\n" 9649 "---\n" 9650 "Language: JavaScript\n" 9651 "IndentWidth: 76\n" 9652 "...\n", 9653 &Style).value()); 9654 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 9655 EXPECT_EQ(76u, Style.IndentWidth); 9656 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 9657 } 9658 9659 TEST_F(FormatTest, ConfigurationRoundTripTest) { 9660 FormatStyle Style = getLLVMStyle(); 9661 std::string YAML = configurationAsText(Style); 9662 FormatStyle ParsedStyle = {}; 9663 ParsedStyle.Language = FormatStyle::LK_Cpp; 9664 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 9665 EXPECT_EQ(Style, ParsedStyle); 9666 } 9667 9668 TEST_F(FormatTest, WorksFor8bitEncodings) { 9669 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 9670 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 9671 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 9672 "\"\xef\xee\xf0\xf3...\"", 9673 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 9674 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 9675 "\xef\xee\xf0\xf3...\"", 9676 getLLVMStyleWithColumns(12))); 9677 } 9678 9679 TEST_F(FormatTest, HandlesUTF8BOM) { 9680 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 9681 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 9682 format("\xef\xbb\xbf#include <iostream>")); 9683 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 9684 format("\xef\xbb\xbf\n#include <iostream>")); 9685 } 9686 9687 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 9688 #if !defined(_MSC_VER) 9689 9690 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 9691 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 9692 getLLVMStyleWithColumns(35)); 9693 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 9694 getLLVMStyleWithColumns(31)); 9695 verifyFormat("// Однажды в студёную зимнюю пору...", 9696 getLLVMStyleWithColumns(36)); 9697 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 9698 verifyFormat("/* Однажды в студёную зимнюю пору... */", 9699 getLLVMStyleWithColumns(39)); 9700 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 9701 getLLVMStyleWithColumns(35)); 9702 } 9703 9704 TEST_F(FormatTest, SplitsUTF8Strings) { 9705 // Non-printable characters' width is currently considered to be the length in 9706 // bytes in UTF8. The characters can be displayed in very different manner 9707 // (zero-width, single width with a substitution glyph, expanded to their code 9708 // (e.g. "<8d>"), so there's no single correct way to handle them. 9709 EXPECT_EQ("\"aaaaÄ\"\n" 9710 "\"\xc2\x8d\";", 9711 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9712 EXPECT_EQ("\"aaaaaaaÄ\"\n" 9713 "\"\xc2\x8d\";", 9714 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 9715 EXPECT_EQ("\"Однажды, в \"\n" 9716 "\"студёную \"\n" 9717 "\"зимнюю \"\n" 9718 "\"пору,\"", 9719 format("\"Однажды, в студёную зимнюю пору,\"", 9720 getLLVMStyleWithColumns(13))); 9721 EXPECT_EQ( 9722 "\"一 二 三 \"\n" 9723 "\"四 五六 \"\n" 9724 "\"七 八 九 \"\n" 9725 "\"十\"", 9726 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 9727 EXPECT_EQ("\"一\t二 \"\n" 9728 "\"\t三 \"\n" 9729 "\"四 五\t六 \"\n" 9730 "\"\t七 \"\n" 9731 "\"八九十\tqq\"", 9732 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 9733 getLLVMStyleWithColumns(11))); 9734 } 9735 9736 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 9737 EXPECT_EQ("const char *sssss =\n" 9738 " \"一二三四五六七八\\\n" 9739 " 九 十\";", 9740 format("const char *sssss = \"一二三四五六七八\\\n" 9741 " 九 十\";", 9742 getLLVMStyleWithColumns(30))); 9743 } 9744 9745 TEST_F(FormatTest, SplitsUTF8LineComments) { 9746 EXPECT_EQ("// aaaaÄ\xc2\x8d", 9747 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 9748 EXPECT_EQ("// Я из лесу\n" 9749 "// вышел; был\n" 9750 "// сильный\n" 9751 "// мороз.", 9752 format("// Я из лесу вышел; был сильный мороз.", 9753 getLLVMStyleWithColumns(13))); 9754 EXPECT_EQ("// 一二三\n" 9755 "// 四五六七\n" 9756 "// 八 九\n" 9757 "// 十", 9758 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 9759 } 9760 9761 TEST_F(FormatTest, SplitsUTF8BlockComments) { 9762 EXPECT_EQ("/* Гляжу,\n" 9763 " * поднимается\n" 9764 " * медленно в\n" 9765 " * гору\n" 9766 " * Лошадка,\n" 9767 " * везущая\n" 9768 " * хворосту\n" 9769 " * воз. */", 9770 format("/* Гляжу, поднимается медленно в гору\n" 9771 " * Лошадка, везущая хворосту воз. */", 9772 getLLVMStyleWithColumns(13))); 9773 EXPECT_EQ( 9774 "/* 一二三\n" 9775 " * 四五六七\n" 9776 " * 八 九\n" 9777 " * 十 */", 9778 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 9779 EXPECT_EQ("/* \n" 9780 " * \n" 9781 " * - */", 9782 format("/* - */", getLLVMStyleWithColumns(12))); 9783 } 9784 9785 #endif // _MSC_VER 9786 9787 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 9788 FormatStyle Style = getLLVMStyle(); 9789 9790 Style.ConstructorInitializerIndentWidth = 4; 9791 verifyFormat( 9792 "SomeClass::Constructor()\n" 9793 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 9794 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 9795 Style); 9796 9797 Style.ConstructorInitializerIndentWidth = 2; 9798 verifyFormat( 9799 "SomeClass::Constructor()\n" 9800 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 9801 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 9802 Style); 9803 9804 Style.ConstructorInitializerIndentWidth = 0; 9805 verifyFormat( 9806 "SomeClass::Constructor()\n" 9807 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 9808 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 9809 Style); 9810 } 9811 9812 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 9813 FormatStyle Style = getLLVMStyle(); 9814 Style.BreakConstructorInitializersBeforeComma = true; 9815 Style.ConstructorInitializerIndentWidth = 4; 9816 verifyFormat("SomeClass::Constructor()\n" 9817 " : a(a)\n" 9818 " , b(b)\n" 9819 " , c(c) {}", 9820 Style); 9821 verifyFormat("SomeClass::Constructor()\n" 9822 " : a(a) {}", 9823 Style); 9824 9825 Style.ColumnLimit = 0; 9826 verifyFormat("SomeClass::Constructor()\n" 9827 " : a(a) {}", 9828 Style); 9829 verifyFormat("SomeClass::Constructor()\n" 9830 " : a(a)\n" 9831 " , b(b)\n" 9832 " , c(c) {}", 9833 Style); 9834 verifyFormat("SomeClass::Constructor()\n" 9835 " : a(a) {\n" 9836 " foo();\n" 9837 " bar();\n" 9838 "}", 9839 Style); 9840 9841 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9842 verifyFormat("SomeClass::Constructor()\n" 9843 " : a(a)\n" 9844 " , b(b)\n" 9845 " , c(c) {\n}", 9846 Style); 9847 verifyFormat("SomeClass::Constructor()\n" 9848 " : a(a) {\n}", 9849 Style); 9850 9851 Style.ColumnLimit = 80; 9852 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 9853 Style.ConstructorInitializerIndentWidth = 2; 9854 verifyFormat("SomeClass::Constructor()\n" 9855 " : a(a)\n" 9856 " , b(b)\n" 9857 " , c(c) {}", 9858 Style); 9859 9860 Style.ConstructorInitializerIndentWidth = 0; 9861 verifyFormat("SomeClass::Constructor()\n" 9862 ": a(a)\n" 9863 ", b(b)\n" 9864 ", c(c) {}", 9865 Style); 9866 9867 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 9868 Style.ConstructorInitializerIndentWidth = 4; 9869 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 9870 verifyFormat( 9871 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 9872 Style); 9873 verifyFormat( 9874 "SomeClass::Constructor()\n" 9875 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 9876 Style); 9877 Style.ConstructorInitializerIndentWidth = 4; 9878 Style.ColumnLimit = 60; 9879 verifyFormat("SomeClass::Constructor()\n" 9880 " : aaaaaaaa(aaaaaaaa)\n" 9881 " , aaaaaaaa(aaaaaaaa)\n" 9882 " , aaaaaaaa(aaaaaaaa) {}", 9883 Style); 9884 } 9885 9886 TEST_F(FormatTest, Destructors) { 9887 verifyFormat("void F(int &i) { i.~int(); }"); 9888 verifyFormat("void F(int &i) { i->~int(); }"); 9889 } 9890 9891 TEST_F(FormatTest, FormatsWithWebKitStyle) { 9892 FormatStyle Style = getWebKitStyle(); 9893 9894 // Don't indent in outer namespaces. 9895 verifyFormat("namespace outer {\n" 9896 "int i;\n" 9897 "namespace inner {\n" 9898 " int i;\n" 9899 "} // namespace inner\n" 9900 "} // namespace outer\n" 9901 "namespace other_outer {\n" 9902 "int i;\n" 9903 "}", 9904 Style); 9905 9906 // Don't indent case labels. 9907 verifyFormat("switch (variable) {\n" 9908 "case 1:\n" 9909 "case 2:\n" 9910 " doSomething();\n" 9911 " break;\n" 9912 "default:\n" 9913 " ++variable;\n" 9914 "}", 9915 Style); 9916 9917 // Wrap before binary operators. 9918 EXPECT_EQ("void f()\n" 9919 "{\n" 9920 " if (aaaaaaaaaaaaaaaa\n" 9921 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 9922 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 9923 " return;\n" 9924 "}", 9925 format("void f() {\n" 9926 "if (aaaaaaaaaaaaaaaa\n" 9927 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 9928 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 9929 "return;\n" 9930 "}", 9931 Style)); 9932 9933 // Allow functions on a single line. 9934 verifyFormat("void f() { return; }", Style); 9935 9936 // Constructor initializers are formatted one per line with the "," on the 9937 // new line. 9938 verifyFormat("Constructor()\n" 9939 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9940 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 9941 " aaaaaaaaaaaaaa)\n" 9942 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 9943 "{\n" 9944 "}", 9945 Style); 9946 verifyFormat("SomeClass::Constructor()\n" 9947 " : a(a)\n" 9948 "{\n" 9949 "}", 9950 Style); 9951 EXPECT_EQ("SomeClass::Constructor()\n" 9952 " : a(a)\n" 9953 "{\n" 9954 "}", 9955 format("SomeClass::Constructor():a(a){}", Style)); 9956 verifyFormat("SomeClass::Constructor()\n" 9957 " : a(a)\n" 9958 " , b(b)\n" 9959 " , c(c)\n" 9960 "{\n" 9961 "}", 9962 Style); 9963 verifyFormat("SomeClass::Constructor()\n" 9964 " : a(a)\n" 9965 "{\n" 9966 " foo();\n" 9967 " bar();\n" 9968 "}", 9969 Style); 9970 9971 // Access specifiers should be aligned left. 9972 verifyFormat("class C {\n" 9973 "public:\n" 9974 " int i;\n" 9975 "};", 9976 Style); 9977 9978 // Do not align comments. 9979 verifyFormat("int a; // Do not\n" 9980 "double b; // align comments.", 9981 Style); 9982 9983 // Do not align operands. 9984 EXPECT_EQ("ASSERT(aaaa\n" 9985 " || bbbb);", 9986 format("ASSERT ( aaaa\n||bbbb);", Style)); 9987 9988 // Accept input's line breaks. 9989 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 9990 " || bbbbbbbbbbbbbbb) {\n" 9991 " i++;\n" 9992 "}", 9993 format("if (aaaaaaaaaaaaaaa\n" 9994 "|| bbbbbbbbbbbbbbb) { i++; }", 9995 Style)); 9996 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 9997 " i++;\n" 9998 "}", 9999 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 10000 10001 // Don't automatically break all macro definitions (llvm.org/PR17842). 10002 verifyFormat("#define aNumber 10", Style); 10003 // However, generally keep the line breaks that the user authored. 10004 EXPECT_EQ("#define aNumber \\\n" 10005 " 10", 10006 format("#define aNumber \\\n" 10007 " 10", 10008 Style)); 10009 10010 // Keep empty and one-element array literals on a single line. 10011 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 10012 " copyItems:YES];", 10013 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 10014 "copyItems:YES];", 10015 Style)); 10016 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 10017 " copyItems:YES];", 10018 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 10019 " copyItems:YES];", 10020 Style)); 10021 // FIXME: This does not seem right, there should be more indentation before 10022 // the array literal's entries. Nested blocks have the same problem. 10023 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10024 " @\"a\",\n" 10025 " @\"a\"\n" 10026 "]\n" 10027 " copyItems:YES];", 10028 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 10029 " @\"a\",\n" 10030 " @\"a\"\n" 10031 " ]\n" 10032 " copyItems:YES];", 10033 Style)); 10034 EXPECT_EQ( 10035 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10036 " copyItems:YES];", 10037 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 10038 " copyItems:YES];", 10039 Style)); 10040 10041 verifyFormat("[self.a b:c c:d];", Style); 10042 EXPECT_EQ("[self.a b:c\n" 10043 " c:d];", 10044 format("[self.a b:c\n" 10045 "c:d];", 10046 Style)); 10047 } 10048 10049 TEST_F(FormatTest, FormatsLambdas) { 10050 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 10051 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 10052 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 10053 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 10054 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 10055 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 10056 verifyFormat("void f() {\n" 10057 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 10058 "}\n"); 10059 verifyFormat("void f() {\n" 10060 " other(x.begin(), //\n" 10061 " x.end(), //\n" 10062 " [&](int, int) { return 1; });\n" 10063 "}\n"); 10064 verifyFormat("SomeFunction([]() { // A cool function...\n" 10065 " return 43;\n" 10066 "});"); 10067 EXPECT_EQ("SomeFunction([]() {\n" 10068 "#define A a\n" 10069 " return 43;\n" 10070 "});", 10071 format("SomeFunction([](){\n" 10072 "#define A a\n" 10073 "return 43;\n" 10074 "});")); 10075 verifyFormat("void f() {\n" 10076 " SomeFunction([](decltype(x), A *a) {});\n" 10077 "}"); 10078 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10079 " [](const aaaaaaaaaa &a) { return a; });"); 10080 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 10081 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 10082 "});"); 10083 verifyFormat("Constructor()\n" 10084 " : Field([] { // comment\n" 10085 " int i;\n" 10086 " }) {}"); 10087 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 10088 " return some_parameter.size();\n" 10089 "};"); 10090 verifyFormat("int i = aaaaaa ? 1 //\n" 10091 " : [] {\n" 10092 " return 2; //\n" 10093 " }();"); 10094 verifyFormat("llvm::errs() << \"number of twos is \"\n" 10095 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 10096 " return x == 2; // force break\n" 10097 " });"); 10098 10099 // Lambdas with return types. 10100 verifyFormat("int c = []() -> int { return 2; }();\n"); 10101 verifyFormat("int c = []() -> int * { return 2; }();\n"); 10102 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 10103 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 10104 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 10105 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 10106 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 10107 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 10108 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 10109 " int j) -> int {\n" 10110 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 10111 "};"); 10112 verifyFormat( 10113 "aaaaaaaaaaaaaaaaaaaaaa(\n" 10114 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 10115 " return aaaaaaaaaaaaaaaaa;\n" 10116 " });", 10117 getLLVMStyleWithColumns(70)); 10118 10119 // Multiple lambdas in the same parentheses change indentation rules. 10120 verifyFormat("SomeFunction(\n" 10121 " []() {\n" 10122 " int i = 42;\n" 10123 " return i;\n" 10124 " },\n" 10125 " []() {\n" 10126 " int j = 43;\n" 10127 " return j;\n" 10128 " });"); 10129 10130 // More complex introducers. 10131 verifyFormat("return [i, args...] {};"); 10132 10133 // Not lambdas. 10134 verifyFormat("constexpr char hello[]{\"hello\"};"); 10135 verifyFormat("double &operator[](int i) { return 0; }\n" 10136 "int i;"); 10137 verifyFormat("std::unique_ptr<int[]> foo() {}"); 10138 verifyFormat("int i = a[a][a]->f();"); 10139 verifyFormat("int i = (*b)[a]->f();"); 10140 10141 // Other corner cases. 10142 verifyFormat("void f() {\n" 10143 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 10144 " );\n" 10145 "}"); 10146 10147 // Lambdas created through weird macros. 10148 verifyFormat("void f() {\n" 10149 " MACRO((const AA &a) { return 1; });\n" 10150 "}"); 10151 10152 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 10153 " doo_dah();\n" 10154 " doo_dah();\n" 10155 " })) {\n" 10156 "}"); 10157 verifyFormat("auto lambda = []() {\n" 10158 " int a = 2\n" 10159 "#if A\n" 10160 " + 2\n" 10161 "#endif\n" 10162 " ;\n" 10163 "};"); 10164 } 10165 10166 TEST_F(FormatTest, FormatsBlocks) { 10167 FormatStyle ShortBlocks = getLLVMStyle(); 10168 ShortBlocks.AllowShortBlocksOnASingleLine = true; 10169 verifyFormat("int (^Block)(int, int);", ShortBlocks); 10170 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 10171 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 10172 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 10173 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 10174 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 10175 10176 verifyFormat("foo(^{ bar(); });", ShortBlocks); 10177 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 10178 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 10179 10180 verifyFormat("[operation setCompletionBlock:^{\n" 10181 " [self onOperationDone];\n" 10182 "}];"); 10183 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 10184 " [self onOperationDone];\n" 10185 "}]};"); 10186 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 10187 " f();\n" 10188 "}];"); 10189 verifyFormat("int a = [operation block:^int(int *i) {\n" 10190 " return 1;\n" 10191 "}];"); 10192 verifyFormat("[myObject doSomethingWith:arg1\n" 10193 " aaa:^int(int *a) {\n" 10194 " return 1;\n" 10195 " }\n" 10196 " bbb:f(a * bbbbbbbb)];"); 10197 10198 verifyFormat("[operation setCompletionBlock:^{\n" 10199 " [self.delegate newDataAvailable];\n" 10200 "}];", 10201 getLLVMStyleWithColumns(60)); 10202 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 10203 " NSString *path = [self sessionFilePath];\n" 10204 " if (path) {\n" 10205 " // ...\n" 10206 " }\n" 10207 "});"); 10208 verifyFormat("[[SessionService sharedService]\n" 10209 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10210 " if (window) {\n" 10211 " [self windowDidLoad:window];\n" 10212 " } else {\n" 10213 " [self errorLoadingWindow];\n" 10214 " }\n" 10215 " }];"); 10216 verifyFormat("void (^largeBlock)(void) = ^{\n" 10217 " // ...\n" 10218 "};\n", 10219 getLLVMStyleWithColumns(40)); 10220 verifyFormat("[[SessionService sharedService]\n" 10221 " loadWindowWithCompletionBlock: //\n" 10222 " ^(SessionWindow *window) {\n" 10223 " if (window) {\n" 10224 " [self windowDidLoad:window];\n" 10225 " } else {\n" 10226 " [self errorLoadingWindow];\n" 10227 " }\n" 10228 " }];", 10229 getLLVMStyleWithColumns(60)); 10230 verifyFormat("[myObject doSomethingWith:arg1\n" 10231 " firstBlock:^(Foo *a) {\n" 10232 " // ...\n" 10233 " int i;\n" 10234 " }\n" 10235 " secondBlock:^(Bar *b) {\n" 10236 " // ...\n" 10237 " int i;\n" 10238 " }\n" 10239 " thirdBlock:^Foo(Bar *b) {\n" 10240 " // ...\n" 10241 " int i;\n" 10242 " }];"); 10243 verifyFormat("[myObject doSomethingWith:arg1\n" 10244 " firstBlock:-1\n" 10245 " secondBlock:^(Bar *b) {\n" 10246 " // ...\n" 10247 " int i;\n" 10248 " }];"); 10249 10250 verifyFormat("f(^{\n" 10251 " @autoreleasepool {\n" 10252 " if (a) {\n" 10253 " g();\n" 10254 " }\n" 10255 " }\n" 10256 "});"); 10257 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 10258 10259 FormatStyle FourIndent = getLLVMStyle(); 10260 FourIndent.ObjCBlockIndentWidth = 4; 10261 verifyFormat("[operation setCompletionBlock:^{\n" 10262 " [self onOperationDone];\n" 10263 "}];", 10264 FourIndent); 10265 } 10266 10267 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 10268 FormatStyle ZeroColumn = getLLVMStyle(); 10269 ZeroColumn.ColumnLimit = 0; 10270 10271 verifyFormat("[[SessionService sharedService] " 10272 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10273 " if (window) {\n" 10274 " [self windowDidLoad:window];\n" 10275 " } else {\n" 10276 " [self errorLoadingWindow];\n" 10277 " }\n" 10278 "}];", 10279 ZeroColumn); 10280 EXPECT_EQ("[[SessionService sharedService]\n" 10281 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10282 " if (window) {\n" 10283 " [self windowDidLoad:window];\n" 10284 " } else {\n" 10285 " [self errorLoadingWindow];\n" 10286 " }\n" 10287 " }];", 10288 format("[[SessionService sharedService]\n" 10289 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 10290 " if (window) {\n" 10291 " [self windowDidLoad:window];\n" 10292 " } else {\n" 10293 " [self errorLoadingWindow];\n" 10294 " }\n" 10295 "}];", 10296 ZeroColumn)); 10297 verifyFormat("[myObject doSomethingWith:arg1\n" 10298 " firstBlock:^(Foo *a) {\n" 10299 " // ...\n" 10300 " int i;\n" 10301 " }\n" 10302 " secondBlock:^(Bar *b) {\n" 10303 " // ...\n" 10304 " int i;\n" 10305 " }\n" 10306 " thirdBlock:^Foo(Bar *b) {\n" 10307 " // ...\n" 10308 " int i;\n" 10309 " }];", 10310 ZeroColumn); 10311 verifyFormat("f(^{\n" 10312 " @autoreleasepool {\n" 10313 " if (a) {\n" 10314 " g();\n" 10315 " }\n" 10316 " }\n" 10317 "});", 10318 ZeroColumn); 10319 verifyFormat("void (^largeBlock)(void) = ^{\n" 10320 " // ...\n" 10321 "};", 10322 ZeroColumn); 10323 10324 ZeroColumn.AllowShortBlocksOnASingleLine = true; 10325 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 10326 format("void (^largeBlock)(void) = ^{ int i; };", 10327 ZeroColumn)); 10328 ZeroColumn.AllowShortBlocksOnASingleLine = false; 10329 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 10330 " int i;\n" 10331 "};", 10332 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 10333 } 10334 10335 TEST_F(FormatTest, SupportsCRLF) { 10336 EXPECT_EQ("int a;\r\n" 10337 "int b;\r\n" 10338 "int c;\r\n", 10339 format("int a;\r\n" 10340 " int b;\r\n" 10341 " int c;\r\n", 10342 getLLVMStyle())); 10343 EXPECT_EQ("int a;\r\n" 10344 "int b;\r\n" 10345 "int c;\r\n", 10346 format("int a;\r\n" 10347 " int b;\n" 10348 " int c;\r\n", 10349 getLLVMStyle())); 10350 EXPECT_EQ("int a;\n" 10351 "int b;\n" 10352 "int c;\n", 10353 format("int a;\r\n" 10354 " int b;\n" 10355 " int c;\n", 10356 getLLVMStyle())); 10357 EXPECT_EQ("\"aaaaaaa \"\r\n" 10358 "\"bbbbbbb\";\r\n", 10359 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 10360 EXPECT_EQ("#define A \\\r\n" 10361 " b; \\\r\n" 10362 " c; \\\r\n" 10363 " d;\r\n", 10364 format("#define A \\\r\n" 10365 " b; \\\r\n" 10366 " c; d; \r\n", 10367 getGoogleStyle())); 10368 10369 EXPECT_EQ("/*\r\n" 10370 "multi line block comments\r\n" 10371 "should not introduce\r\n" 10372 "an extra carriage return\r\n" 10373 "*/\r\n", 10374 format("/*\r\n" 10375 "multi line block comments\r\n" 10376 "should not introduce\r\n" 10377 "an extra carriage return\r\n" 10378 "*/\r\n")); 10379 } 10380 10381 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 10382 verifyFormat("MY_CLASS(C) {\n" 10383 " int i;\n" 10384 " int j;\n" 10385 "};"); 10386 } 10387 10388 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 10389 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 10390 TwoIndent.ContinuationIndentWidth = 2; 10391 10392 EXPECT_EQ("int i =\n" 10393 " longFunction(\n" 10394 " arg);", 10395 format("int i = longFunction(arg);", TwoIndent)); 10396 10397 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 10398 SixIndent.ContinuationIndentWidth = 6; 10399 10400 EXPECT_EQ("int i =\n" 10401 " longFunction(\n" 10402 " arg);", 10403 format("int i = longFunction(arg);", SixIndent)); 10404 } 10405 10406 TEST_F(FormatTest, SpacesInAngles) { 10407 FormatStyle Spaces = getLLVMStyle(); 10408 Spaces.SpacesInAngles = true; 10409 10410 verifyFormat("static_cast< int >(arg);", Spaces); 10411 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 10412 verifyFormat("f< int, float >();", Spaces); 10413 verifyFormat("template <> g() {}", Spaces); 10414 verifyFormat("template < std::vector< int > > f() {}", Spaces); 10415 10416 Spaces.Standard = FormatStyle::LS_Cpp03; 10417 Spaces.SpacesInAngles = true; 10418 verifyFormat("A< A< int > >();", Spaces); 10419 10420 Spaces.SpacesInAngles = false; 10421 verifyFormat("A<A<int> >();", Spaces); 10422 10423 Spaces.Standard = FormatStyle::LS_Cpp11; 10424 Spaces.SpacesInAngles = true; 10425 verifyFormat("A< A< int > >();", Spaces); 10426 10427 Spaces.SpacesInAngles = false; 10428 verifyFormat("A<A<int>>();", Spaces); 10429 } 10430 10431 TEST_F(FormatTest, TripleAngleBrackets) { 10432 verifyFormat("f<<<1, 1>>>();"); 10433 verifyFormat("f<<<1, 1, 1, s>>>();"); 10434 verifyFormat("f<<<a, b, c, d>>>();"); 10435 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 10436 verifyFormat("f<param><<<1, 1>>>();"); 10437 verifyFormat("f<1><<<1, 1>>>();"); 10438 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 10439 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10440 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 10441 } 10442 10443 TEST_F(FormatTest, MergeLessLessAtEnd) { 10444 verifyFormat("<<"); 10445 EXPECT_EQ("< < <", format("\\\n<<<")); 10446 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10447 "aaallvm::outs() <<"); 10448 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 10449 "aaaallvm::outs()\n <<"); 10450 } 10451 10452 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 10453 std::string code = "#if A\n" 10454 "#if B\n" 10455 "a.\n" 10456 "#endif\n" 10457 " a = 1;\n" 10458 "#else\n" 10459 "#endif\n" 10460 "#if C\n" 10461 "#else\n" 10462 "#endif\n"; 10463 EXPECT_EQ(code, format(code)); 10464 } 10465 10466 TEST_F(FormatTest, HandleConflictMarkers) { 10467 // Git/SVN conflict markers. 10468 EXPECT_EQ("int a;\n" 10469 "void f() {\n" 10470 " callme(some(parameter1,\n" 10471 "<<<<<<< text by the vcs\n" 10472 " parameter2),\n" 10473 "||||||| text by the vcs\n" 10474 " parameter2),\n" 10475 " parameter3,\n" 10476 "======= text by the vcs\n" 10477 " parameter2, parameter3),\n" 10478 ">>>>>>> text by the vcs\n" 10479 " otherparameter);\n", 10480 format("int a;\n" 10481 "void f() {\n" 10482 " callme(some(parameter1,\n" 10483 "<<<<<<< text by the vcs\n" 10484 " parameter2),\n" 10485 "||||||| text by the vcs\n" 10486 " parameter2),\n" 10487 " parameter3,\n" 10488 "======= text by the vcs\n" 10489 " parameter2,\n" 10490 " parameter3),\n" 10491 ">>>>>>> text by the vcs\n" 10492 " otherparameter);\n")); 10493 10494 // Perforce markers. 10495 EXPECT_EQ("void f() {\n" 10496 " function(\n" 10497 ">>>> text by the vcs\n" 10498 " parameter,\n" 10499 "==== text by the vcs\n" 10500 " parameter,\n" 10501 "==== text by the vcs\n" 10502 " parameter,\n" 10503 "<<<< text by the vcs\n" 10504 " parameter);\n", 10505 format("void f() {\n" 10506 " function(\n" 10507 ">>>> text by the vcs\n" 10508 " parameter,\n" 10509 "==== text by the vcs\n" 10510 " parameter,\n" 10511 "==== text by the vcs\n" 10512 " parameter,\n" 10513 "<<<< text by the vcs\n" 10514 " parameter);\n")); 10515 10516 EXPECT_EQ("<<<<<<<\n" 10517 "|||||||\n" 10518 "=======\n" 10519 ">>>>>>>", 10520 format("<<<<<<<\n" 10521 "|||||||\n" 10522 "=======\n" 10523 ">>>>>>>")); 10524 10525 EXPECT_EQ("<<<<<<<\n" 10526 "|||||||\n" 10527 "int i;\n" 10528 "=======\n" 10529 ">>>>>>>", 10530 format("<<<<<<<\n" 10531 "|||||||\n" 10532 "int i;\n" 10533 "=======\n" 10534 ">>>>>>>")); 10535 10536 // FIXME: Handle parsing of macros around conflict markers correctly: 10537 EXPECT_EQ("#define Macro \\\n" 10538 "<<<<<<<\n" 10539 "Something \\\n" 10540 "|||||||\n" 10541 "Else \\\n" 10542 "=======\n" 10543 "Other \\\n" 10544 ">>>>>>>\n" 10545 " End int i;\n", 10546 format("#define Macro \\\n" 10547 "<<<<<<<\n" 10548 " Something \\\n" 10549 "|||||||\n" 10550 " Else \\\n" 10551 "=======\n" 10552 " Other \\\n" 10553 ">>>>>>>\n" 10554 " End\n" 10555 "int i;\n")); 10556 } 10557 10558 TEST_F(FormatTest, DisableRegions) { 10559 EXPECT_EQ("int i;\n" 10560 "// clang-format off\n" 10561 " int j;\n" 10562 "// clang-format on\n" 10563 "int k;", 10564 format(" int i;\n" 10565 " // clang-format off\n" 10566 " int j;\n" 10567 " // clang-format on\n" 10568 " int k;")); 10569 EXPECT_EQ("int i;\n" 10570 "/* clang-format off */\n" 10571 " int j;\n" 10572 "/* clang-format on */\n" 10573 "int k;", 10574 format(" int i;\n" 10575 " /* clang-format off */\n" 10576 " int j;\n" 10577 " /* clang-format on */\n" 10578 " int k;")); 10579 } 10580 10581 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 10582 format("? ) ="); 10583 verifyNoCrash("#define a\\\n /**/}"); 10584 } 10585 10586 } // end namespace tooling 10587 } // end namespace clang 10588