1 //===- unittest/Format/FormatTestComments.cpp - Formatting unit tests -----===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/Format/Format.h" 10 11 #include "../Tooling/ReplacementTest.h" 12 #include "FormatTestUtils.h" 13 14 #include "clang/Frontend/TextDiagnosticPrinter.h" 15 #include "llvm/Support/Debug.h" 16 #include "llvm/Support/MemoryBuffer.h" 17 #include "gtest/gtest.h" 18 19 #define DEBUG_TYPE "format-test" 20 21 using clang::tooling::ReplacementTest; 22 23 namespace clang { 24 namespace format { 25 namespace { 26 27 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 28 29 class FormatTestComments : public ::testing::Test { 30 protected: 31 enum StatusCheck { 32 SC_ExpectComplete, 33 SC_ExpectIncomplete, 34 SC_DoNotCheck 35 }; 36 37 std::string format(llvm::StringRef Code, 38 const FormatStyle &Style = getLLVMStyle(), 39 StatusCheck CheckComplete = SC_ExpectComplete) { 40 LLVM_DEBUG(llvm::errs() << "---\n"); 41 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 42 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 43 FormattingAttemptStatus Status; 44 tooling::Replacements Replaces = 45 reformat(Style, Code, Ranges, "<stdin>", &Status); 46 if (CheckComplete != SC_DoNotCheck) { 47 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 48 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 49 << Code << "\n\n"; 50 } 51 ReplacementCount = Replaces.size(); 52 auto Result = applyAllReplacements(Code, Replaces); 53 EXPECT_TRUE(static_cast<bool>(Result)); 54 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 55 return *Result; 56 } 57 58 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 59 FormatStyle Style = getLLVMStyle(); 60 Style.ColumnLimit = ColumnLimit; 61 return Style; 62 } 63 64 FormatStyle getTextProtoStyleWithColumns(unsigned ColumnLimit) { 65 FormatStyle Style = getGoogleStyle(FormatStyle::FormatStyle::LK_TextProto); 66 Style.ColumnLimit = ColumnLimit; 67 return Style; 68 } 69 70 void verifyFormat(llvm::StringRef Code, 71 const FormatStyle &Style = getLLVMStyle()) { 72 EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable"; 73 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 74 } 75 76 void verifyGoogleFormat(llvm::StringRef Code) { 77 verifyFormat(Code, getGoogleStyle()); 78 } 79 80 /// \brief Verify that clang-format does not crash on the given input. 81 void verifyNoCrash(llvm::StringRef Code, 82 const FormatStyle &Style = getLLVMStyle()) { 83 format(Code, Style, SC_DoNotCheck); 84 } 85 86 int ReplacementCount; 87 }; 88 89 //===----------------------------------------------------------------------===// 90 // Tests for comments. 91 //===----------------------------------------------------------------------===// 92 93 TEST_F(FormatTestComments, UnderstandsSingleLineComments) { 94 verifyFormat("//* */"); 95 verifyFormat("// line 1\n" 96 "// line 2\n" 97 "void f() {}\n"); 98 99 verifyFormat("void f() {\n" 100 " // Doesn't do anything\n" 101 "}"); 102 verifyFormat("SomeObject\n" 103 " // Calling someFunction on SomeObject\n" 104 " .someFunction();"); 105 verifyFormat("auto result = SomeObject\n" 106 " // Calling someFunction on SomeObject\n" 107 " .someFunction();"); 108 verifyFormat("void f(int i, // some comment (probably for i)\n" 109 " int j, // some comment (probably for j)\n" 110 " int k); // some comment (probably for k)"); 111 verifyFormat("void f(int i,\n" 112 " // some comment (probably for j)\n" 113 " int j,\n" 114 " // some comment (probably for k)\n" 115 " int k);"); 116 117 verifyFormat("int i // This is a fancy variable\n" 118 " = 5; // with nicely aligned comment."); 119 120 verifyFormat("// Leading comment.\n" 121 "int a; // Trailing comment."); 122 verifyFormat("int a; // Trailing comment\n" 123 " // on 2\n" 124 " // or 3 lines.\n" 125 "int b;"); 126 verifyFormat("int a; // Trailing comment\n" 127 "\n" 128 "// Leading comment.\n" 129 "int b;"); 130 verifyFormat("int a; // Comment.\n" 131 " // More details.\n" 132 "int bbbb; // Another comment."); 133 verifyFormat( 134 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 135 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" 136 "int cccccccccccccccccccccccccccccc; // comment\n" 137 "int ddd; // looooooooooooooooooooooooong comment\n" 138 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" 139 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" 140 "int ccccccccccccccccccc; // comment"); 141 142 verifyFormat("#include \"a\" // comment\n" 143 "#include \"a/b/c\" // comment"); 144 verifyFormat("#include <a> // comment\n" 145 "#include <a/b/c> // comment"); 146 EXPECT_EQ("#include \"a\" // comment\n" 147 "#include \"a/b/c\" // comment", 148 format("#include \\\n" 149 " \"a\" // comment\n" 150 "#include \"a/b/c\" // comment")); 151 152 verifyFormat("enum E {\n" 153 " // comment\n" 154 " VAL_A, // comment\n" 155 " VAL_B\n" 156 "};"); 157 158 EXPECT_EQ("enum A {\n" 159 " // line a\n" 160 " a,\n" 161 " b, // line b\n" 162 "\n" 163 " // line c\n" 164 " c\n" 165 "};", 166 format("enum A {\n" 167 " // line a\n" 168 " a,\n" 169 " b, // line b\n" 170 "\n" 171 " // line c\n" 172 " c\n" 173 "};", 174 getLLVMStyleWithColumns(20))); 175 EXPECT_EQ("enum A {\n" 176 " a, // line 1\n" 177 " // line 2\n" 178 "};", 179 format("enum A {\n" 180 " a, // line 1\n" 181 " // line 2\n" 182 "};", 183 getLLVMStyleWithColumns(20))); 184 EXPECT_EQ("enum A {\n" 185 " a, // line 1\n" 186 " // line 2\n" 187 "};", 188 format("enum A {\n" 189 " a, // line 1\n" 190 " // line 2\n" 191 "};", 192 getLLVMStyleWithColumns(20))); 193 EXPECT_EQ("enum A {\n" 194 " a, // line 1\n" 195 " // line 2\n" 196 " b\n" 197 "};", 198 format("enum A {\n" 199 " a, // line 1\n" 200 " // line 2\n" 201 " b\n" 202 "};", 203 getLLVMStyleWithColumns(20))); 204 EXPECT_EQ("enum A {\n" 205 " a, // line 1\n" 206 " // line 2\n" 207 " b\n" 208 "};", 209 format("enum A {\n" 210 " a, // line 1\n" 211 " // line 2\n" 212 " b\n" 213 "};", 214 getLLVMStyleWithColumns(20))); 215 verifyFormat( 216 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 217 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); 218 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 219 " // Comment inside a statement.\n" 220 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 221 verifyFormat("SomeFunction(a,\n" 222 " // comment\n" 223 " b + x);"); 224 verifyFormat("SomeFunction(a, a,\n" 225 " // comment\n" 226 " b + x);"); 227 verifyFormat( 228 "bool aaaaaaaaaaaaa = // comment\n" 229 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 230 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 231 232 verifyFormat("int aaaa; // aaaaa\n" 233 "int aa; // aaaaaaa", 234 getLLVMStyleWithColumns(20)); 235 236 EXPECT_EQ("void f() { // This does something ..\n" 237 "}\n" 238 "int a; // This is unrelated", 239 format("void f() { // This does something ..\n" 240 " }\n" 241 "int a; // This is unrelated")); 242 EXPECT_EQ("class C {\n" 243 " void f() { // This does something ..\n" 244 " } // awesome..\n" 245 "\n" 246 " int a; // This is unrelated\n" 247 "};", 248 format("class C{void f() { // This does something ..\n" 249 " } // awesome..\n" 250 " \n" 251 "int a; // This is unrelated\n" 252 "};")); 253 254 EXPECT_EQ("int i; // single line trailing comment", 255 format("int i;\\\n// single line trailing comment")); 256 257 verifyGoogleFormat("int a; // Trailing comment."); 258 259 verifyFormat("someFunction(anotherFunction( // Force break.\n" 260 " parameter));"); 261 262 verifyGoogleFormat("#endif // HEADER_GUARD"); 263 264 verifyFormat("const char *test[] = {\n" 265 " // A\n" 266 " \"aaaa\",\n" 267 " // B\n" 268 " \"aaaaa\"};"); 269 verifyGoogleFormat( 270 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 271 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); 272 EXPECT_EQ("D(a, {\n" 273 " // test\n" 274 " int a;\n" 275 "});", 276 format("D(a, {\n" 277 "// test\n" 278 "int a;\n" 279 "});")); 280 281 EXPECT_EQ("lineWith(); // comment\n" 282 "// at start\n" 283 "otherLine();", 284 format("lineWith(); // comment\n" 285 "// at start\n" 286 "otherLine();")); 287 EXPECT_EQ("lineWith(); // comment\n" 288 "/*\n" 289 " * at start */\n" 290 "otherLine();", 291 format("lineWith(); // comment\n" 292 "/*\n" 293 " * at start */\n" 294 "otherLine();")); 295 EXPECT_EQ("lineWith(); // comment\n" 296 " // at start\n" 297 "otherLine();", 298 format("lineWith(); // comment\n" 299 " // at start\n" 300 "otherLine();")); 301 302 EXPECT_EQ("lineWith(); // comment\n" 303 "// at start\n" 304 "otherLine(); // comment", 305 format("lineWith(); // comment\n" 306 "// at start\n" 307 "otherLine(); // comment")); 308 EXPECT_EQ("lineWith();\n" 309 "// at start\n" 310 "otherLine(); // comment", 311 format("lineWith();\n" 312 " // at start\n" 313 "otherLine(); // comment")); 314 EXPECT_EQ("// first\n" 315 "// at start\n" 316 "otherLine(); // comment", 317 format("// first\n" 318 " // at start\n" 319 "otherLine(); // comment")); 320 EXPECT_EQ("f();\n" 321 "// first\n" 322 "// at start\n" 323 "otherLine(); // comment", 324 format("f();\n" 325 "// first\n" 326 " // at start\n" 327 "otherLine(); // comment")); 328 verifyFormat("f(); // comment\n" 329 "// first\n" 330 "// at start\n" 331 "otherLine();"); 332 EXPECT_EQ("f(); // comment\n" 333 "// first\n" 334 "// at start\n" 335 "otherLine();", 336 format("f(); // comment\n" 337 "// first\n" 338 " // at start\n" 339 "otherLine();")); 340 EXPECT_EQ("f(); // comment\n" 341 " // first\n" 342 "// at start\n" 343 "otherLine();", 344 format("f(); // comment\n" 345 " // first\n" 346 "// at start\n" 347 "otherLine();")); 348 EXPECT_EQ("void f() {\n" 349 " lineWith(); // comment\n" 350 " // at start\n" 351 "}", 352 format("void f() {\n" 353 " lineWith(); // comment\n" 354 " // at start\n" 355 "}")); 356 EXPECT_EQ("int xy; // a\n" 357 "int z; // b", 358 format("int xy; // a\n" 359 "int z; //b")); 360 EXPECT_EQ("int xy; // a\n" 361 "int z; // bb", 362 format("int xy; // a\n" 363 "int z; //bb", 364 getLLVMStyleWithColumns(12))); 365 366 verifyFormat("#define A \\\n" 367 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 368 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 369 getLLVMStyleWithColumns(60)); 370 verifyFormat( 371 "#define A \\\n" 372 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" 373 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */", 374 getLLVMStyleWithColumns(61)); 375 376 verifyFormat("if ( // This is some comment\n" 377 " x + 3) {\n" 378 "}"); 379 EXPECT_EQ("if ( // This is some comment\n" 380 " // spanning two lines\n" 381 " x + 3) {\n" 382 "}", 383 format("if( // This is some comment\n" 384 " // spanning two lines\n" 385 " x + 3) {\n" 386 "}")); 387 388 verifyNoCrash("/\\\n/"); 389 verifyNoCrash("/\\\n* */"); 390 // The 0-character somehow makes the lexer return a proper comment. 391 verifyNoCrash(StringRef("/*\\\0\n/", 6)); 392 } 393 394 TEST_F(FormatTestComments, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { 395 EXPECT_EQ("SomeFunction(a,\n" 396 " b, // comment\n" 397 " c);", 398 format("SomeFunction(a,\n" 399 " b, // comment\n" 400 " c);")); 401 EXPECT_EQ("SomeFunction(a, b,\n" 402 " // comment\n" 403 " c);", 404 format("SomeFunction(a,\n" 405 " b,\n" 406 " // comment\n" 407 " c);")); 408 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" 409 " c);", 410 format("SomeFunction(a, b, // comment (unclear relation)\n" 411 " c);")); 412 EXPECT_EQ("SomeFunction(a, // comment\n" 413 " b,\n" 414 " c); // comment", 415 format("SomeFunction(a, // comment\n" 416 " b,\n" 417 " c); // comment")); 418 EXPECT_EQ("aaaaaaaaaa(aaaa(aaaa,\n" 419 " aaaa), //\n" 420 " aaaa, bbbbb);", 421 format("aaaaaaaaaa(aaaa(aaaa,\n" 422 "aaaa), //\n" 423 "aaaa, bbbbb);")); 424 } 425 426 TEST_F(FormatTestComments, RemovesTrailingWhitespaceOfComments) { 427 EXPECT_EQ("// comment", format("// comment ")); 428 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", 429 format("int aaaaaaa, bbbbbbb; // comment ", 430 getLLVMStyleWithColumns(33))); 431 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); 432 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); 433 } 434 435 TEST_F(FormatTestComments, UnderstandsBlockComments) { 436 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); 437 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }"); 438 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" 439 " bbbbbbbbbbbbbbbbbbbbbbbbb);", 440 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" 441 "/* Trailing comment for aa... */\n" 442 " bbbbbbbbbbbbbbbbbbbbbbbbb);")); 443 EXPECT_EQ( 444 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 445 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", 446 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" 447 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); 448 EXPECT_EQ( 449 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 450 " aaaaaaaaaaaaaaaaaa,\n" 451 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 452 "}", 453 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 454 " aaaaaaaaaaaaaaaaaa ,\n" 455 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n" 456 "}")); 457 verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n" 458 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 459 460 FormatStyle NoBinPacking = getLLVMStyle(); 461 NoBinPacking.BinPackParameters = false; 462 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" 463 " /* parameter 2 */ aaaaaa,\n" 464 " /* parameter 3 */ aaaaaa,\n" 465 " /* parameter 4 */ aaaaaa);", 466 NoBinPacking); 467 468 // Aligning block comments in macros. 469 verifyGoogleFormat("#define A \\\n" 470 " int i; /*a*/ \\\n" 471 " int jjj; /*b*/"); 472 } 473 474 TEST_F(FormatTestComments, AlignsBlockComments) { 475 EXPECT_EQ("/*\n" 476 " * Really multi-line\n" 477 " * comment.\n" 478 " */\n" 479 "void f() {}", 480 format(" /*\n" 481 " * Really multi-line\n" 482 " * comment.\n" 483 " */\n" 484 " void f() {}")); 485 EXPECT_EQ("class C {\n" 486 " /*\n" 487 " * Another multi-line\n" 488 " * comment.\n" 489 " */\n" 490 " void f() {}\n" 491 "};", 492 format("class C {\n" 493 "/*\n" 494 " * Another multi-line\n" 495 " * comment.\n" 496 " */\n" 497 "void f() {}\n" 498 "};")); 499 EXPECT_EQ("/*\n" 500 " 1. This is a comment with non-trivial formatting.\n" 501 " 1.1. We have to indent/outdent all lines equally\n" 502 " 1.1.1. to keep the formatting.\n" 503 " */", 504 format(" /*\n" 505 " 1. This is a comment with non-trivial formatting.\n" 506 " 1.1. We have to indent/outdent all lines equally\n" 507 " 1.1.1. to keep the formatting.\n" 508 " */")); 509 EXPECT_EQ("/*\n" 510 "Don't try to outdent if there's not enough indentation.\n" 511 "*/", 512 format(" /*\n" 513 " Don't try to outdent if there's not enough indentation.\n" 514 " */")); 515 516 EXPECT_EQ("int i; /* Comment with empty...\n" 517 " *\n" 518 " * line. */", 519 format("int i; /* Comment with empty...\n" 520 " *\n" 521 " * line. */")); 522 EXPECT_EQ("int foobar = 0; /* comment */\n" 523 "int bar = 0; /* multiline\n" 524 " comment 1 */\n" 525 "int baz = 0; /* multiline\n" 526 " comment 2 */\n" 527 "int bzz = 0; /* multiline\n" 528 " comment 3 */", 529 format("int foobar = 0; /* comment */\n" 530 "int bar = 0; /* multiline\n" 531 " comment 1 */\n" 532 "int baz = 0; /* multiline\n" 533 " comment 2 */\n" 534 "int bzz = 0; /* multiline\n" 535 " comment 3 */")); 536 EXPECT_EQ("int foobar = 0; /* comment */\n" 537 "int bar = 0; /* multiline\n" 538 " comment */\n" 539 "int baz = 0; /* multiline\n" 540 "comment */", 541 format("int foobar = 0; /* comment */\n" 542 "int bar = 0; /* multiline\n" 543 "comment */\n" 544 "int baz = 0; /* multiline\n" 545 "comment */")); 546 } 547 548 TEST_F(FormatTestComments, CommentReflowingCanBeTurnedOff) { 549 FormatStyle Style = getLLVMStyleWithColumns(20); 550 Style.ReflowComments = false; 551 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style); 552 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style); 553 } 554 555 TEST_F(FormatTestComments, CorrectlyHandlesLengthOfBlockComments) { 556 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 557 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", 558 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 559 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); 560 EXPECT_EQ( 561 "void ffffffffffff(\n" 562 " int aaaaaaaa, int bbbbbbbb,\n" 563 " int cccccccccccc) { /*\n" 564 " aaaaaaaaaa\n" 565 " aaaaaaaaaaaaa\n" 566 " bbbbbbbbbbbbbb\n" 567 " bbbbbbbbbb\n" 568 " */\n" 569 "}", 570 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" 571 "{ /*\n" 572 " aaaaaaaaaa aaaaaaaaaaaaa\n" 573 " bbbbbbbbbbbbbb bbbbbbbbbb\n" 574 " */\n" 575 "}", 576 getLLVMStyleWithColumns(40))); 577 } 578 579 TEST_F(FormatTestComments, DontBreakNonTrailingBlockComments) { 580 EXPECT_EQ("void ffffffffff(\n" 581 " int aaaaa /* test */);", 582 format("void ffffffffff(int aaaaa /* test */);", 583 getLLVMStyleWithColumns(35))); 584 } 585 586 TEST_F(FormatTestComments, SplitsLongCxxComments) { 587 EXPECT_EQ("// A comment that\n" 588 "// doesn't fit on\n" 589 "// one line", 590 format("// A comment that doesn't fit on one line", 591 getLLVMStyleWithColumns(20))); 592 EXPECT_EQ("/// A comment that\n" 593 "/// doesn't fit on\n" 594 "/// one line", 595 format("/// A comment that doesn't fit on one line", 596 getLLVMStyleWithColumns(20))); 597 EXPECT_EQ("//! A comment that\n" 598 "//! doesn't fit on\n" 599 "//! one line", 600 format("//! A comment that doesn't fit on one line", 601 getLLVMStyleWithColumns(20))); 602 EXPECT_EQ("// a b c d\n" 603 "// e f g\n" 604 "// h i j k", 605 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 606 EXPECT_EQ( 607 "// a b c d\n" 608 "// e f g\n" 609 "// h i j k", 610 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); 611 EXPECT_EQ("if (true) // A comment that\n" 612 " // doesn't fit on\n" 613 " // one line", 614 format("if (true) // A comment that doesn't fit on one line ", 615 getLLVMStyleWithColumns(30))); 616 EXPECT_EQ("// Don't_touch_leading_whitespace", 617 format("// Don't_touch_leading_whitespace", 618 getLLVMStyleWithColumns(20))); 619 EXPECT_EQ("// Add leading\n" 620 "// whitespace", 621 format("//Add leading whitespace", getLLVMStyleWithColumns(20))); 622 EXPECT_EQ("/// Add leading\n" 623 "/// whitespace", 624 format("///Add leading whitespace", getLLVMStyleWithColumns(20))); 625 EXPECT_EQ("//! Add leading\n" 626 "//! whitespace", 627 format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); 628 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle())); 629 EXPECT_EQ("// Even if it makes the line exceed the column\n" 630 "// limit", 631 format("//Even if it makes the line exceed the column limit", 632 getLLVMStyleWithColumns(51))); 633 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); 634 EXPECT_EQ("/// line 1\n" 635 "// add leading whitespace", 636 format("/// line 1\n" 637 "//add leading whitespace", 638 getLLVMStyleWithColumns(30))); 639 EXPECT_EQ("/// line 1\n" 640 "/// line 2\n" 641 "//! line 3\n" 642 "//! line 4\n" 643 "//! line 5\n" 644 "// line 6\n" 645 "// line 7", 646 format("///line 1\n" 647 "///line 2\n" 648 "//! line 3\n" 649 "//!line 4\n" 650 "//!line 5\n" 651 "// line 6\n" 652 "//line 7", getLLVMStyleWithColumns(20))); 653 654 EXPECT_EQ("// aa bb cc dd", 655 format("// aa bb cc dd ", 656 getLLVMStyleWithColumns(15))); 657 658 EXPECT_EQ("// A comment before\n" 659 "// a macro\n" 660 "// definition\n" 661 "#define a b", 662 format("// A comment before a macro definition\n" 663 "#define a b", 664 getLLVMStyleWithColumns(20))); 665 EXPECT_EQ("void ffffff(\n" 666 " int aaaaaaaaa, // wwww\n" 667 " int bbbbbbbbbb, // xxxxxxx\n" 668 " // yyyyyyyyyy\n" 669 " int c, int d, int e) {}", 670 format("void ffffff(\n" 671 " int aaaaaaaaa, // wwww\n" 672 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" 673 " int c, int d, int e) {}", 674 getLLVMStyleWithColumns(40))); 675 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 676 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 677 getLLVMStyleWithColumns(20))); 678 EXPECT_EQ( 679 "#define XXX // a b c d\n" 680 " // e f g h", 681 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); 682 EXPECT_EQ( 683 "#define XXX // q w e r\n" 684 " // t y u i", 685 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); 686 EXPECT_EQ("{\n" 687 " //\n" 688 " //\\\n" 689 " // long 1 2 3 4 5\n" 690 "}", 691 format("{\n" 692 " //\n" 693 " //\\\n" 694 " // long 1 2 3 4 5\n" 695 "}", 696 getLLVMStyleWithColumns(20))); 697 EXPECT_EQ("{\n" 698 " //\n" 699 " //\\\n" 700 " // long 1 2 3 4 5\n" 701 " // 6\n" 702 "}", 703 format("{\n" 704 " //\n" 705 " //\\\n" 706 " // long 1 2 3 4 5 6\n" 707 "}", 708 getLLVMStyleWithColumns(20))); 709 } 710 711 TEST_F(FormatTestComments, PreservesHangingIndentInCxxComments) { 712 EXPECT_EQ("// A comment\n" 713 "// that doesn't\n" 714 "// fit on one\n" 715 "// line", 716 format("// A comment that doesn't fit on one line", 717 getLLVMStyleWithColumns(20))); 718 EXPECT_EQ("/// A comment\n" 719 "/// that doesn't\n" 720 "/// fit on one\n" 721 "/// line", 722 format("/// A comment that doesn't fit on one line", 723 getLLVMStyleWithColumns(20))); 724 } 725 726 TEST_F(FormatTestComments, DontSplitLineCommentsWithEscapedNewlines) { 727 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 728 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 729 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 730 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 731 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 732 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); 733 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 734 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 735 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 736 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 737 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 738 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 739 getLLVMStyleWithColumns(50))); 740 // FIXME: One day we might want to implement adjustment of leading whitespace 741 // of the consecutive lines in this kind of comment: 742 EXPECT_EQ("double\n" 743 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 744 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 745 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 746 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 747 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" 748 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 749 getLLVMStyleWithColumns(49))); 750 } 751 752 TEST_F(FormatTestComments, DontSplitLineCommentsWithPragmas) { 753 FormatStyle Pragmas = getLLVMStyleWithColumns(30); 754 Pragmas.CommentPragmas = "^ IWYU pragma:"; 755 EXPECT_EQ( 756 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", 757 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); 758 EXPECT_EQ( 759 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", 760 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); 761 } 762 763 TEST_F(FormatTestComments, PriorityOfCommentBreaking) { 764 EXPECT_EQ("if (xxx ==\n" 765 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 766 " zzz)\n" 767 " q();", 768 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" 769 " zzz) q();", 770 getLLVMStyleWithColumns(40))); 771 EXPECT_EQ("if (xxxxxxxxxx ==\n" 772 " yyy && // aaaaaa bbbbbbbb cccc\n" 773 " zzz)\n" 774 " q();", 775 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" 776 " zzz) q();", 777 getLLVMStyleWithColumns(40))); 778 EXPECT_EQ("if (xxxxxxxxxx &&\n" 779 " yyy || // aaaaaa bbbbbbbb cccc\n" 780 " zzz)\n" 781 " q();", 782 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" 783 " zzz) q();", 784 getLLVMStyleWithColumns(40))); 785 EXPECT_EQ("fffffffff(\n" 786 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 787 " zzz);", 788 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" 789 " zzz);", 790 getLLVMStyleWithColumns(40))); 791 } 792 793 TEST_F(FormatTestComments, MultiLineCommentsInDefines) { 794 EXPECT_EQ("#define A(x) /* \\\n" 795 " a comment \\\n" 796 " inside */ \\\n" 797 " f();", 798 format("#define A(x) /* \\\n" 799 " a comment \\\n" 800 " inside */ \\\n" 801 " f();", 802 getLLVMStyleWithColumns(17))); 803 EXPECT_EQ("#define A( \\\n" 804 " x) /* \\\n" 805 " a comment \\\n" 806 " inside */ \\\n" 807 " f();", 808 format("#define A( \\\n" 809 " x) /* \\\n" 810 " a comment \\\n" 811 " inside */ \\\n" 812 " f();", 813 getLLVMStyleWithColumns(17))); 814 } 815 816 TEST_F(FormatTestComments, ParsesCommentsAdjacentToPPDirectives) { 817 EXPECT_EQ("namespace {}\n// Test\n#define A", 818 format("namespace {}\n // Test\n#define A")); 819 EXPECT_EQ("namespace {}\n/* Test */\n#define A", 820 format("namespace {}\n /* Test */\n#define A")); 821 EXPECT_EQ("namespace {}\n/* Test */ #define A", 822 format("namespace {}\n /* Test */ #define A")); 823 } 824 825 TEST_F(FormatTestComments, KeepsLevelOfCommentBeforePPDirective) { 826 // Keep the current level if the comment was originally not aligned with 827 // the preprocessor directive. 828 EXPECT_EQ("void f() {\n" 829 " int i;\n" 830 " /* comment */\n" 831 "#ifdef A\n" 832 " int j;\n" 833 "}", 834 format("void f() {\n" 835 " int i;\n" 836 " /* comment */\n" 837 "#ifdef A\n" 838 " int j;\n" 839 "}")); 840 841 EXPECT_EQ("void f() {\n" 842 " int i;\n" 843 " /* comment */\n" 844 "\n" 845 "#ifdef A\n" 846 " int j;\n" 847 "}", 848 format("void f() {\n" 849 " int i;\n" 850 " /* comment */\n" 851 "\n" 852 "#ifdef A\n" 853 " int j;\n" 854 "}")); 855 856 EXPECT_EQ("int f(int i) {\n" 857 " if (true) {\n" 858 " ++i;\n" 859 " }\n" 860 " // comment\n" 861 "#ifdef A\n" 862 " int j;\n" 863 "#endif\n" 864 "}", 865 format("int f(int i) {\n" 866 " if (true) {\n" 867 " ++i;\n" 868 " }\n" 869 " // comment\n" 870 "#ifdef A\n" 871 "int j;\n" 872 "#endif\n" 873 "}")); 874 875 EXPECT_EQ("int f(int i) {\n" 876 " if (true) {\n" 877 " i++;\n" 878 " } else {\n" 879 " // comment in else\n" 880 "#ifdef A\n" 881 " j++;\n" 882 "#endif\n" 883 " }\n" 884 "}", 885 format("int f(int i) {\n" 886 " if (true) {\n" 887 " i++;\n" 888 " } else {\n" 889 " // comment in else\n" 890 "#ifdef A\n" 891 " j++;\n" 892 "#endif\n" 893 " }\n" 894 "}")); 895 896 EXPECT_EQ("int f(int i) {\n" 897 " if (true) {\n" 898 " i++;\n" 899 " } else {\n" 900 " /* comment in else */\n" 901 "#ifdef A\n" 902 " j++;\n" 903 "#endif\n" 904 " }\n" 905 "}", 906 format("int f(int i) {\n" 907 " if (true) {\n" 908 " i++;\n" 909 " } else {\n" 910 " /* comment in else */\n" 911 "#ifdef A\n" 912 " j++;\n" 913 "#endif\n" 914 " }\n" 915 "}")); 916 917 // Keep the current level if there is an empty line between the comment and 918 // the preprocessor directive. 919 EXPECT_EQ("void f() {\n" 920 " int i;\n" 921 " /* comment */\n" 922 "\n" 923 "#ifdef A\n" 924 " int j;\n" 925 "}", 926 format("void f() {\n" 927 " int i;\n" 928 "/* comment */\n" 929 "\n" 930 "#ifdef A\n" 931 " int j;\n" 932 "}")); 933 934 EXPECT_EQ("void f() {\n" 935 " int i;\n" 936 " return i;\n" 937 "}\n" 938 "// comment\n" 939 "\n" 940 "#ifdef A\n" 941 "int i;\n" 942 "#endif // A", 943 format("void f() {\n" 944 " int i;\n" 945 " return i;\n" 946 "}\n" 947 "// comment\n" 948 "\n" 949 "#ifdef A\n" 950 "int i;\n" 951 "#endif // A")); 952 953 EXPECT_EQ("int f(int i) {\n" 954 " if (true) {\n" 955 " ++i;\n" 956 " }\n" 957 " // comment\n" 958 "\n" 959 "#ifdef A\n" 960 " int j;\n" 961 "#endif\n" 962 "}", 963 format("int f(int i) {\n" 964 " if (true) {\n" 965 " ++i;\n" 966 " }\n" 967 " // comment\n" 968 "\n" 969 "#ifdef A\n" 970 " int j;\n" 971 "#endif\n" 972 "}")); 973 974 EXPECT_EQ("int f(int i) {\n" 975 " if (true) {\n" 976 " i++;\n" 977 " } else {\n" 978 " // comment in else\n" 979 "\n" 980 "#ifdef A\n" 981 " j++;\n" 982 "#endif\n" 983 " }\n" 984 "}", 985 format("int f(int i) {\n" 986 " if (true) {\n" 987 " i++;\n" 988 " } else {\n" 989 "// comment in else\n" 990 "\n" 991 "#ifdef A\n" 992 " j++;\n" 993 "#endif\n" 994 " }\n" 995 "}")); 996 997 EXPECT_EQ("int f(int i) {\n" 998 " if (true) {\n" 999 " i++;\n" 1000 " } else {\n" 1001 " /* comment in else */\n" 1002 "\n" 1003 "#ifdef A\n" 1004 " j++;\n" 1005 "#endif\n" 1006 " }\n" 1007 "}", 1008 format("int f(int i) {\n" 1009 " if (true) {\n" 1010 " i++;\n" 1011 " } else {\n" 1012 "/* comment in else */\n" 1013 "\n" 1014 "#ifdef A\n" 1015 " j++;\n" 1016 "#endif\n" 1017 " }\n" 1018 "}")); 1019 1020 // Align with the preprocessor directive if the comment was originally aligned 1021 // with the preprocessor directive and there is no newline between the comment 1022 // and the preprocessor directive. 1023 EXPECT_EQ("void f() {\n" 1024 " int i;\n" 1025 "/* comment */\n" 1026 "#ifdef A\n" 1027 " int j;\n" 1028 "}", 1029 format("void f() {\n" 1030 " int i;\n" 1031 "/* comment */\n" 1032 "#ifdef A\n" 1033 " int j;\n" 1034 "}")); 1035 1036 EXPECT_EQ("int f(int i) {\n" 1037 " if (true) {\n" 1038 " ++i;\n" 1039 " }\n" 1040 "// comment\n" 1041 "#ifdef A\n" 1042 " int j;\n" 1043 "#endif\n" 1044 "}", 1045 format("int f(int i) {\n" 1046 " if (true) {\n" 1047 " ++i;\n" 1048 " }\n" 1049 "// comment\n" 1050 "#ifdef A\n" 1051 " int j;\n" 1052 "#endif\n" 1053 "}")); 1054 1055 EXPECT_EQ("int f(int i) {\n" 1056 " if (true) {\n" 1057 " i++;\n" 1058 " } else {\n" 1059 "// comment in else\n" 1060 "#ifdef A\n" 1061 " j++;\n" 1062 "#endif\n" 1063 " }\n" 1064 "}", 1065 format("int f(int i) {\n" 1066 " if (true) {\n" 1067 " i++;\n" 1068 " } else {\n" 1069 " // comment in else\n" 1070 " #ifdef A\n" 1071 " j++;\n" 1072 "#endif\n" 1073 " }\n" 1074 "}")); 1075 1076 EXPECT_EQ("int f(int i) {\n" 1077 " if (true) {\n" 1078 " i++;\n" 1079 " } else {\n" 1080 "/* comment in else */\n" 1081 "#ifdef A\n" 1082 " j++;\n" 1083 "#endif\n" 1084 " }\n" 1085 "}", 1086 format("int f(int i) {\n" 1087 " if (true) {\n" 1088 " i++;\n" 1089 " } else {\n" 1090 " /* comment in else */\n" 1091 " #ifdef A\n" 1092 " j++;\n" 1093 "#endif\n" 1094 " }\n" 1095 "}")); 1096 } 1097 1098 TEST_F(FormatTestComments, SplitsLongLinesInComments) { 1099 // FIXME: Do we need to fix up the " */" at the end? 1100 // It doesn't look like any of our current logic triggers this. 1101 EXPECT_EQ("/* This is a long\n" 1102 " * comment that\n" 1103 " * doesn't fit on\n" 1104 " * one line. */", 1105 format("/* " 1106 "This is a long " 1107 "comment that " 1108 "doesn't " 1109 "fit on one line. */", 1110 getLLVMStyleWithColumns(20))); 1111 EXPECT_EQ( 1112 "/* a b c d\n" 1113 " * e f g\n" 1114 " * h i j k\n" 1115 " */", 1116 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1117 EXPECT_EQ( 1118 "/* a b c d\n" 1119 " * e f g\n" 1120 " * h i j k\n" 1121 " */", 1122 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); 1123 EXPECT_EQ("/*\n" 1124 "This is a long\n" 1125 "comment that doesn't\n" 1126 "fit on one line.\n" 1127 "*/", 1128 format("/*\n" 1129 "This is a long " 1130 "comment that doesn't " 1131 "fit on one line. \n" 1132 "*/", 1133 getLLVMStyleWithColumns(20))); 1134 EXPECT_EQ("/*\n" 1135 " * This is a long\n" 1136 " * comment that\n" 1137 " * doesn't fit on\n" 1138 " * one line.\n" 1139 " */", 1140 format("/* \n" 1141 " * This is a long " 1142 " comment that " 1143 " doesn't fit on " 1144 " one line. \n" 1145 " */", 1146 getLLVMStyleWithColumns(20))); 1147 EXPECT_EQ("/*\n" 1148 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" 1149 " * so_it_should_be_broken\n" 1150 " * wherever_a_space_occurs\n" 1151 " */", 1152 format("/*\n" 1153 " * This_is_a_comment_with_words_that_dont_fit_on_one_line " 1154 " so_it_should_be_broken " 1155 " wherever_a_space_occurs \n" 1156 " */", 1157 getLLVMStyleWithColumns(20))); 1158 EXPECT_EQ("/*\n" 1159 " * This_comment_can_not_be_broken_into_lines\n" 1160 " */", 1161 format("/*\n" 1162 " * This_comment_can_not_be_broken_into_lines\n" 1163 " */", 1164 getLLVMStyleWithColumns(20))); 1165 EXPECT_EQ("{\n" 1166 " /*\n" 1167 " This is another\n" 1168 " long comment that\n" 1169 " doesn't fit on one\n" 1170 " line 1234567890\n" 1171 " */\n" 1172 "}", 1173 format("{\n" 1174 "/*\n" 1175 "This is another " 1176 " long comment that " 1177 " doesn't fit on one" 1178 " line 1234567890\n" 1179 "*/\n" 1180 "}", 1181 getLLVMStyleWithColumns(20))); 1182 EXPECT_EQ("{\n" 1183 " /*\n" 1184 " * This i s\n" 1185 " * another comment\n" 1186 " * t hat doesn' t\n" 1187 " * fit on one l i\n" 1188 " * n e\n" 1189 " */\n" 1190 "}", 1191 format("{\n" 1192 "/*\n" 1193 " * This i s" 1194 " another comment" 1195 " t hat doesn' t" 1196 " fit on one l i" 1197 " n e\n" 1198 " */\n" 1199 "}", 1200 getLLVMStyleWithColumns(20))); 1201 EXPECT_EQ("/*\n" 1202 " * This is a long\n" 1203 " * comment that\n" 1204 " * doesn't fit on\n" 1205 " * one line\n" 1206 " */", 1207 format(" /*\n" 1208 " * This is a long comment that doesn't fit on one line\n" 1209 " */", 1210 getLLVMStyleWithColumns(20))); 1211 EXPECT_EQ("{\n" 1212 " if (something) /* This is a\n" 1213 " long\n" 1214 " comment */\n" 1215 " ;\n" 1216 "}", 1217 format("{\n" 1218 " if (something) /* This is a long comment */\n" 1219 " ;\n" 1220 "}", 1221 getLLVMStyleWithColumns(30))); 1222 1223 EXPECT_EQ("/* A comment before\n" 1224 " * a macro\n" 1225 " * definition */\n" 1226 "#define a b", 1227 format("/* A comment before a macro definition */\n" 1228 "#define a b", 1229 getLLVMStyleWithColumns(20))); 1230 1231 EXPECT_EQ("/* some comment\n" 1232 " * a comment that\n" 1233 " * we break another\n" 1234 " * comment we have\n" 1235 " * to break a left\n" 1236 " * comment\n" 1237 " */", 1238 format(" /* some comment\n" 1239 " * a comment that we break\n" 1240 " * another comment we have to break\n" 1241 "* a left comment\n" 1242 " */", 1243 getLLVMStyleWithColumns(20))); 1244 1245 EXPECT_EQ("/**\n" 1246 " * multiline block\n" 1247 " * comment\n" 1248 " *\n" 1249 " */", 1250 format("/**\n" 1251 " * multiline block comment\n" 1252 " *\n" 1253 " */", 1254 getLLVMStyleWithColumns(20))); 1255 1256 // This reproduces a crashing bug where both adaptStartOfLine and 1257 // getCommentSplit were trying to wrap after the "/**". 1258 EXPECT_EQ("/** multilineblockcommentwithnowrapopportunity */", 1259 format("/** multilineblockcommentwithnowrapopportunity */", 1260 getLLVMStyleWithColumns(20))); 1261 1262 EXPECT_EQ("/*\n" 1263 "\n" 1264 "\n" 1265 " */\n", 1266 format(" /* \n" 1267 " \n" 1268 " \n" 1269 " */\n")); 1270 1271 EXPECT_EQ("/* a a */", 1272 format("/* a a */", getLLVMStyleWithColumns(15))); 1273 EXPECT_EQ("/* a a bc */", 1274 format("/* a a bc */", getLLVMStyleWithColumns(15))); 1275 EXPECT_EQ("/* aaa aaa\n" 1276 " * aaaaa */", 1277 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1278 EXPECT_EQ("/* aaa aaa\n" 1279 " * aaaaa */", 1280 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); 1281 } 1282 1283 TEST_F(FormatTestComments, SplitsLongLinesInCommentsInPreprocessor) { 1284 EXPECT_EQ("#define X \\\n" 1285 " /* \\\n" 1286 " Test \\\n" 1287 " Macro comment \\\n" 1288 " with a long \\\n" 1289 " line \\\n" 1290 " */ \\\n" 1291 " A + B", 1292 format("#define X \\\n" 1293 " /*\n" 1294 " Test\n" 1295 " Macro comment with a long line\n" 1296 " */ \\\n" 1297 " A + B", 1298 getLLVMStyleWithColumns(20))); 1299 EXPECT_EQ("#define X \\\n" 1300 " /* Macro comment \\\n" 1301 " with a long \\\n" 1302 " line */ \\\n" 1303 " A + B", 1304 format("#define X \\\n" 1305 " /* Macro comment with a long\n" 1306 " line */ \\\n" 1307 " A + B", 1308 getLLVMStyleWithColumns(20))); 1309 EXPECT_EQ("#define X \\\n" 1310 " /* Macro comment \\\n" 1311 " * with a long \\\n" 1312 " * line */ \\\n" 1313 " A + B", 1314 format("#define X \\\n" 1315 " /* Macro comment with a long line */ \\\n" 1316 " A + B", 1317 getLLVMStyleWithColumns(20))); 1318 } 1319 1320 TEST_F(FormatTestComments, KeepsTrailingPPCommentsAndSectionCommentsSeparate) { 1321 verifyFormat("#ifdef A // line about A\n" 1322 "// section comment\n" 1323 "#endif", 1324 getLLVMStyleWithColumns(80)); 1325 verifyFormat("#ifdef A // line 1 about A\n" 1326 " // line 2 about A\n" 1327 "// section comment\n" 1328 "#endif", 1329 getLLVMStyleWithColumns(80)); 1330 EXPECT_EQ("#ifdef A // line 1 about A\n" 1331 " // line 2 about A\n" 1332 "// section comment\n" 1333 "#endif", 1334 format("#ifdef A // line 1 about A\n" 1335 " // line 2 about A\n" 1336 "// section comment\n" 1337 "#endif", 1338 getLLVMStyleWithColumns(80))); 1339 verifyFormat("int f() {\n" 1340 " int i;\n" 1341 "#ifdef A // comment about A\n" 1342 " // section comment 1\n" 1343 " // section comment 2\n" 1344 " i = 2;\n" 1345 "#else // comment about #else\n" 1346 " // section comment 3\n" 1347 " i = 4;\n" 1348 "#endif\n" 1349 "}", getLLVMStyleWithColumns(80)); 1350 } 1351 1352 TEST_F(FormatTestComments, AlignsPPElseEndifComments) { 1353 verifyFormat("#if A\n" 1354 "#else // A\n" 1355 "int iiii;\n" 1356 "#endif // B", 1357 getLLVMStyleWithColumns(20)); 1358 verifyFormat("#if A\n" 1359 "#else // A\n" 1360 "int iiii; // CC\n" 1361 "#endif // B", 1362 getLLVMStyleWithColumns(20)); 1363 EXPECT_EQ("#if A\n" 1364 "#else // A1\n" 1365 " // A2\n" 1366 "int ii;\n" 1367 "#endif // B", 1368 format("#if A\n" 1369 "#else // A1\n" 1370 " // A2\n" 1371 "int ii;\n" 1372 "#endif // B", 1373 getLLVMStyleWithColumns(20))); 1374 } 1375 1376 TEST_F(FormatTestComments, CommentsInStaticInitializers) { 1377 EXPECT_EQ( 1378 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" 1379 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" 1380 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" 1381 " aaaaaaaaaaaaaaaaaaaa, // comment\n" 1382 " aaaaaaaaaaaaaaaaaaaa};", 1383 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" 1384 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" 1385 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" 1386 " aaaaaaaaaaaaaaaaaaaa , // comment\n" 1387 " aaaaaaaaaaaaaaaaaaaa };")); 1388 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1389 " bbbbbbbbbbb, ccccccccccc};"); 1390 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" 1391 " // comment for bb....\n" 1392 " bbbbbbbbbbb, ccccccccccc};"); 1393 verifyGoogleFormat( 1394 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" 1395 " bbbbbbbbbbb, ccccccccccc};"); 1396 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n" 1397 " // comment for bb....\n" 1398 " bbbbbbbbbbb, ccccccccccc};"); 1399 1400 verifyFormat("S s = {{a, b, c}, // Group #1\n" 1401 " {d, e, f}, // Group #2\n" 1402 " {g, h, i}}; // Group #3"); 1403 verifyFormat("S s = {{// Group #1\n" 1404 " a, b, c},\n" 1405 " {// Group #2\n" 1406 " d, e, f},\n" 1407 " {// Group #3\n" 1408 " g, h, i}};"); 1409 1410 EXPECT_EQ("S s = {\n" 1411 " // Some comment\n" 1412 " a,\n" 1413 "\n" 1414 " // Comment after empty line\n" 1415 " b}", 1416 format("S s = {\n" 1417 " // Some comment\n" 1418 " a,\n" 1419 " \n" 1420 " // Comment after empty line\n" 1421 " b\n" 1422 "}")); 1423 EXPECT_EQ("S s = {\n" 1424 " /* Some comment */\n" 1425 " a,\n" 1426 "\n" 1427 " /* Comment after empty line */\n" 1428 " b}", 1429 format("S s = {\n" 1430 " /* Some comment */\n" 1431 " a,\n" 1432 " \n" 1433 " /* Comment after empty line */\n" 1434 " b\n" 1435 "}")); 1436 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" 1437 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1438 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" 1439 " 0x00, 0x00, 0x00, 0x00}; // comment\n"); 1440 } 1441 1442 TEST_F(FormatTestComments, LineCommentsAfterRightBrace) { 1443 EXPECT_EQ("if (true) { // comment about branch\n" 1444 " // comment about f\n" 1445 " f();\n" 1446 "}", 1447 format("if (true) { // comment about branch\n" 1448 " // comment about f\n" 1449 " f();\n" 1450 "}", 1451 getLLVMStyleWithColumns(80))); 1452 EXPECT_EQ("if (1) { // if line 1\n" 1453 " // if line 2\n" 1454 " // if line 3\n" 1455 " // f line 1\n" 1456 " // f line 2\n" 1457 " f();\n" 1458 "} else { // else line 1\n" 1459 " // else line 2\n" 1460 " // else line 3\n" 1461 " // g line 1\n" 1462 " g();\n" 1463 "}", 1464 format("if (1) { // if line 1\n" 1465 " // if line 2\n" 1466 " // if line 3\n" 1467 " // f line 1\n" 1468 " // f line 2\n" 1469 " f();\n" 1470 "} else { // else line 1\n" 1471 " // else line 2\n" 1472 " // else line 3\n" 1473 " // g line 1\n" 1474 " g();\n" 1475 "}")); 1476 EXPECT_EQ("do { // line 1\n" 1477 " // line 2\n" 1478 " // line 3\n" 1479 " f();\n" 1480 "} while (true);", 1481 format("do { // line 1\n" 1482 " // line 2\n" 1483 " // line 3\n" 1484 " f();\n" 1485 "} while (true);", 1486 getLLVMStyleWithColumns(80))); 1487 EXPECT_EQ("while (a < b) { // line 1\n" 1488 " // line 2\n" 1489 " // line 3\n" 1490 " f();\n" 1491 "}", 1492 format("while (a < b) {// line 1\n" 1493 " // line 2\n" 1494 " // line 3\n" 1495 " f();\n" 1496 "}", 1497 getLLVMStyleWithColumns(80))); 1498 } 1499 1500 TEST_F(FormatTestComments, ReflowsComments) { 1501 // Break a long line and reflow with the full next line. 1502 EXPECT_EQ("// long long long\n" 1503 "// long long", 1504 format("// long long long long\n" 1505 "// long", 1506 getLLVMStyleWithColumns(20))); 1507 1508 // Keep the trailing newline while reflowing. 1509 EXPECT_EQ("// long long long\n" 1510 "// long long\n", 1511 format("// long long long long\n" 1512 "// long\n", 1513 getLLVMStyleWithColumns(20))); 1514 1515 // Break a long line and reflow with a part of the next line. 1516 EXPECT_EQ("// long long long\n" 1517 "// long long\n" 1518 "// long_long", 1519 format("// long long long long\n" 1520 "// long long_long", 1521 getLLVMStyleWithColumns(20))); 1522 1523 // Break but do not reflow if the first word from the next line is too long. 1524 EXPECT_EQ("// long long long\n" 1525 "// long\n" 1526 "// long_long_long\n", 1527 format("// long long long long\n" 1528 "// long_long_long\n", 1529 getLLVMStyleWithColumns(20))); 1530 1531 // Don't break or reflow short lines. 1532 verifyFormat("// long\n" 1533 "// long long long lo\n" 1534 "// long long long lo\n" 1535 "// long", 1536 getLLVMStyleWithColumns(20)); 1537 1538 // Keep prefixes and decorations while reflowing. 1539 EXPECT_EQ("/// long long long\n" 1540 "/// long long\n", 1541 format("/// long long long long\n" 1542 "/// long\n", 1543 getLLVMStyleWithColumns(20))); 1544 EXPECT_EQ("//! long long long\n" 1545 "//! long long\n", 1546 format("//! long long long long\n" 1547 "//! long\n", 1548 getLLVMStyleWithColumns(20))); 1549 EXPECT_EQ("/* long long long\n" 1550 " * long long */", 1551 format("/* long long long long\n" 1552 " * long */", 1553 getLLVMStyleWithColumns(20))); 1554 EXPECT_EQ("///< long long long\n" 1555 "///< long long\n", 1556 format("///< long long long long\n" 1557 "///< long\n", 1558 getLLVMStyleWithColumns(20))); 1559 EXPECT_EQ("//!< long long long\n" 1560 "//!< long long\n", 1561 format("//!< long long long long\n" 1562 "//!< long\n", 1563 getLLVMStyleWithColumns(20))); 1564 1565 // Don't bring leading whitespace up while reflowing. 1566 EXPECT_EQ("/* long long long\n" 1567 " * long long long\n" 1568 " */", 1569 format("/* long long long long\n" 1570 " * long long\n" 1571 " */", 1572 getLLVMStyleWithColumns(20))); 1573 1574 // Reflow the last line of a block comment with its trailing '*/'. 1575 EXPECT_EQ("/* long long long\n" 1576 " long long */", 1577 format("/* long long long long\n" 1578 " long */", 1579 getLLVMStyleWithColumns(20))); 1580 1581 // Reflow two short lines; keep the postfix of the last one. 1582 EXPECT_EQ("/* long long long\n" 1583 " * long long long */", 1584 format("/* long long long long\n" 1585 " * long\n" 1586 " * long */", 1587 getLLVMStyleWithColumns(20))); 1588 1589 // Put the postfix of the last short reflow line on a newline if it doesn't 1590 // fit. 1591 EXPECT_EQ("/* long long long\n" 1592 " * long long longg\n" 1593 " */", 1594 format("/* long long long long\n" 1595 " * long\n" 1596 " * longg */", 1597 getLLVMStyleWithColumns(20))); 1598 1599 // Reflow lines with leading whitespace. 1600 EXPECT_EQ("{\n" 1601 " /*\n" 1602 " * long long long\n" 1603 " * long long long\n" 1604 " * long long long\n" 1605 " */\n" 1606 "}", 1607 format("{\n" 1608 "/*\n" 1609 " * long long long long\n" 1610 " * long\n" 1611 " * long long long long\n" 1612 " */\n" 1613 "}", 1614 getLLVMStyleWithColumns(20))); 1615 1616 // Break single line block comments that are first in the line with ' *' 1617 // decoration. 1618 EXPECT_EQ("/* long long long\n" 1619 " * long */", 1620 format("/* long long long long */", getLLVMStyleWithColumns(20))); 1621 1622 // Break single line block comment that are not first in the line with ' ' 1623 // decoration. 1624 EXPECT_EQ("int i; /* long long\n" 1625 " long */", 1626 format("int i; /* long long long */", getLLVMStyleWithColumns(20))); 1627 1628 // Reflow a line that goes just over the column limit. 1629 EXPECT_EQ("// long long long\n" 1630 "// lon long", 1631 format("// long long long lon\n" 1632 "// long", 1633 getLLVMStyleWithColumns(20))); 1634 1635 // Stop reflowing if the next line has a different indentation than the 1636 // previous line. 1637 EXPECT_EQ("// long long long\n" 1638 "// long\n" 1639 "// long long\n" 1640 "// long", 1641 format("// long long long long\n" 1642 "// long long\n" 1643 "// long", 1644 getLLVMStyleWithColumns(20))); 1645 1646 // Reflow into the last part of a really long line that has been broken into 1647 // multiple lines. 1648 EXPECT_EQ("// long long long\n" 1649 "// long long long\n" 1650 "// long long long\n", 1651 format("// long long long long long long long long\n" 1652 "// long\n", 1653 getLLVMStyleWithColumns(20))); 1654 1655 // Break the first line, then reflow the beginning of the second and third 1656 // line up. 1657 EXPECT_EQ("// long long long\n" 1658 "// lon1 lon2 lon2\n" 1659 "// lon2 lon3 lon3", 1660 format("// long long long lon1\n" 1661 "// lon2 lon2 lon2\n" 1662 "// lon3 lon3", 1663 getLLVMStyleWithColumns(20))); 1664 1665 // Reflow the beginning of the second line, then break the rest. 1666 EXPECT_EQ("// long long long\n" 1667 "// lon1 lon2 lon2\n" 1668 "// lon2 lon2 lon2\n" 1669 "// lon3", 1670 format("// long long long lon1\n" 1671 "// lon2 lon2 lon2 lon2 lon2 lon3", 1672 getLLVMStyleWithColumns(20))); 1673 1674 // Shrink the first line, then reflow the second line up. 1675 EXPECT_EQ("// long long long", format("// long long\n" 1676 "// long", 1677 getLLVMStyleWithColumns(20))); 1678 1679 // Don't shrink leading whitespace. 1680 EXPECT_EQ("int i; /// a", 1681 format("int i; /// a", getLLVMStyleWithColumns(20))); 1682 1683 // Shrink trailing whitespace if there is no postfix and reflow. 1684 EXPECT_EQ("// long long long\n" 1685 "// long long", 1686 format("// long long long long \n" 1687 "// long", 1688 getLLVMStyleWithColumns(20))); 1689 1690 // Shrink trailing whitespace to a single one if there is postfix. 1691 EXPECT_EQ("/* long long long */", 1692 format("/* long long long */", getLLVMStyleWithColumns(20))); 1693 1694 // Break a block comment postfix if exceeding the line limit. 1695 EXPECT_EQ("/* long\n" 1696 " */", 1697 format("/* long */", getLLVMStyleWithColumns(20))); 1698 1699 // Reflow indented comments. 1700 EXPECT_EQ("{\n" 1701 " // long long long\n" 1702 " // long long\n" 1703 " int i; /* long lon\n" 1704 " g long\n" 1705 " */\n" 1706 "}", 1707 format("{\n" 1708 " // long long long long\n" 1709 " // long\n" 1710 " int i; /* long lon g\n" 1711 " long */\n" 1712 "}", 1713 getLLVMStyleWithColumns(20))); 1714 1715 // Don't realign trailing comments after reflow has happened. 1716 EXPECT_EQ("// long long long\n" 1717 "// long long\n" 1718 "long i; // long", 1719 format("// long long long long\n" 1720 "// long\n" 1721 "long i; // long", 1722 getLLVMStyleWithColumns(20))); 1723 EXPECT_EQ("// long long long\n" 1724 "// longng long long\n" 1725 "// long lo", 1726 format("// long long long longng\n" 1727 "// long long long\n" 1728 "// lo", 1729 getLLVMStyleWithColumns(20))); 1730 1731 // Reflow lines after a broken line. 1732 EXPECT_EQ("int a; // Trailing\n" 1733 " // comment on\n" 1734 " // 2 or 3\n" 1735 " // lines.\n", 1736 format("int a; // Trailing comment\n" 1737 " // on 2\n" 1738 " // or 3\n" 1739 " // lines.\n", 1740 getLLVMStyleWithColumns(20))); 1741 EXPECT_EQ("/// This long line\n" 1742 "/// gets reflown.\n", 1743 format("/// This long line gets\n" 1744 "/// reflown.\n", 1745 getLLVMStyleWithColumns(20))); 1746 EXPECT_EQ("//! This long line\n" 1747 "//! gets reflown.\n", 1748 format(" //! This long line gets\n" 1749 " //! reflown.\n", 1750 getLLVMStyleWithColumns(20))); 1751 EXPECT_EQ("/* This long line\n" 1752 " * gets reflown.\n" 1753 " */\n", 1754 format("/* This long line gets\n" 1755 " * reflown.\n" 1756 " */\n", 1757 getLLVMStyleWithColumns(20))); 1758 1759 // Reflow after indentation makes a line too long. 1760 EXPECT_EQ("{\n" 1761 " // long long long\n" 1762 " // lo long\n" 1763 "}\n", 1764 format("{\n" 1765 "// long long long lo\n" 1766 "// long\n" 1767 "}\n", 1768 getLLVMStyleWithColumns(20))); 1769 1770 // Break and reflow multiple lines. 1771 EXPECT_EQ("/*\n" 1772 " * Reflow the end of\n" 1773 " * line by 11 22 33\n" 1774 " * 4.\n" 1775 " */\n", 1776 format("/*\n" 1777 " * Reflow the end of line\n" 1778 " * by\n" 1779 " * 11\n" 1780 " * 22\n" 1781 " * 33\n" 1782 " * 4.\n" 1783 " */\n", 1784 getLLVMStyleWithColumns(20))); 1785 EXPECT_EQ("/// First line gets\n" 1786 "/// broken. Second\n" 1787 "/// line gets\n" 1788 "/// reflown and\n" 1789 "/// broken. Third\n" 1790 "/// gets reflown.\n", 1791 format("/// First line gets broken.\n" 1792 "/// Second line gets reflown and broken.\n" 1793 "/// Third gets reflown.\n", 1794 getLLVMStyleWithColumns(20))); 1795 EXPECT_EQ("int i; // first long\n" 1796 " // long snd\n" 1797 " // long.\n", 1798 format("int i; // first long long\n" 1799 " // snd long.\n", 1800 getLLVMStyleWithColumns(20))); 1801 EXPECT_EQ("{\n" 1802 " // first long line\n" 1803 " // line second\n" 1804 " // long line line\n" 1805 " // third long line\n" 1806 " // line\n" 1807 "}\n", 1808 format("{\n" 1809 " // first long line line\n" 1810 " // second long line line\n" 1811 " // third long line line\n" 1812 "}\n", 1813 getLLVMStyleWithColumns(20))); 1814 EXPECT_EQ("int i; /* first line\n" 1815 " * second\n" 1816 " * line third\n" 1817 " * line\n" 1818 " */", 1819 format("int i; /* first line\n" 1820 " * second line\n" 1821 " * third line\n" 1822 " */", 1823 getLLVMStyleWithColumns(20))); 1824 1825 // Reflow the last two lines of a section that starts with a line having 1826 // different indentation. 1827 EXPECT_EQ( 1828 "// long\n" 1829 "// long long long\n" 1830 "// long long", 1831 format("// long\n" 1832 "// long long long long\n" 1833 "// long", 1834 getLLVMStyleWithColumns(20))); 1835 1836 // Keep the block comment endling '*/' while reflowing. 1837 EXPECT_EQ("/* Long long long\n" 1838 " * line short */\n", 1839 format("/* Long long long line\n" 1840 " * short */\n", 1841 getLLVMStyleWithColumns(20))); 1842 1843 // Don't reflow between separate blocks of comments. 1844 EXPECT_EQ("/* First comment\n" 1845 " * block will */\n" 1846 "/* Snd\n" 1847 " */\n", 1848 format("/* First comment block\n" 1849 " * will */\n" 1850 "/* Snd\n" 1851 " */\n", 1852 getLLVMStyleWithColumns(20))); 1853 1854 // Don't reflow across blank comment lines. 1855 EXPECT_EQ("int i; // This long\n" 1856 " // line gets\n" 1857 " // broken.\n" 1858 " //\n" 1859 " // keep.\n", 1860 format("int i; // This long line gets broken.\n" 1861 " // \n" 1862 " // keep.\n", 1863 getLLVMStyleWithColumns(20))); 1864 EXPECT_EQ("{\n" 1865 " /// long long long\n" 1866 " /// long long\n" 1867 " ///\n" 1868 " /// long\n" 1869 "}", 1870 format("{\n" 1871 " /// long long long long\n" 1872 " /// long\n" 1873 " ///\n" 1874 " /// long\n" 1875 "}", 1876 getLLVMStyleWithColumns(20))); 1877 EXPECT_EQ("//! long long long\n" 1878 "//! long\n" 1879 "\n" 1880 "//! long", 1881 format("//! long long long long\n" 1882 "\n" 1883 "//! long", 1884 getLLVMStyleWithColumns(20))); 1885 EXPECT_EQ("/* long long long\n" 1886 " long\n" 1887 "\n" 1888 " long */", 1889 format("/* long long long long\n" 1890 "\n" 1891 " long */", 1892 getLLVMStyleWithColumns(20))); 1893 EXPECT_EQ("/* long long long\n" 1894 " * long\n" 1895 " *\n" 1896 " * long */", 1897 format("/* long long long long\n" 1898 " *\n" 1899 " * long */", 1900 getLLVMStyleWithColumns(20))); 1901 1902 // Don't reflow lines having content that is a single character. 1903 EXPECT_EQ("// long long long\n" 1904 "// long\n" 1905 "// l", 1906 format("// long long long long\n" 1907 "// l", 1908 getLLVMStyleWithColumns(20))); 1909 1910 // Don't reflow lines starting with two punctuation characters. 1911 EXPECT_EQ("// long long long\n" 1912 "// long\n" 1913 "// ... --- ...", 1914 format( 1915 "// long long long long\n" 1916 "// ... --- ...", 1917 getLLVMStyleWithColumns(20))); 1918 1919 // Don't reflow lines starting with '@'. 1920 EXPECT_EQ("// long long long\n" 1921 "// long\n" 1922 "// @param arg", 1923 format("// long long long long\n" 1924 "// @param arg", 1925 getLLVMStyleWithColumns(20))); 1926 1927 // Don't reflow lines starting with 'TODO'. 1928 EXPECT_EQ("// long long long\n" 1929 "// long\n" 1930 "// TODO: long", 1931 format("// long long long long\n" 1932 "// TODO: long", 1933 getLLVMStyleWithColumns(20))); 1934 1935 // Don't reflow lines starting with 'FIXME'. 1936 EXPECT_EQ("// long long long\n" 1937 "// long\n" 1938 "// FIXME: long", 1939 format("// long long long long\n" 1940 "// FIXME: long", 1941 getLLVMStyleWithColumns(20))); 1942 1943 // Don't reflow lines starting with 'XXX'. 1944 EXPECT_EQ("// long long long\n" 1945 "// long\n" 1946 "// XXX: long", 1947 format("// long long long long\n" 1948 "// XXX: long", 1949 getLLVMStyleWithColumns(20))); 1950 1951 // Don't reflow comment pragmas. 1952 EXPECT_EQ("// long long long\n" 1953 "// long\n" 1954 "// IWYU pragma:", 1955 format("// long long long long\n" 1956 "// IWYU pragma:", 1957 getLLVMStyleWithColumns(20))); 1958 EXPECT_EQ("/* long long long\n" 1959 " * long\n" 1960 " * IWYU pragma:\n" 1961 " */", 1962 format("/* long long long long\n" 1963 " * IWYU pragma:\n" 1964 " */", 1965 getLLVMStyleWithColumns(20))); 1966 1967 // Reflow lines that have a non-punctuation character among their first 2 1968 // characters. 1969 EXPECT_EQ("// long long long\n" 1970 "// long 'long'", 1971 format( 1972 "// long long long long\n" 1973 "// 'long'", 1974 getLLVMStyleWithColumns(20))); 1975 1976 // Don't reflow between separate blocks of comments. 1977 EXPECT_EQ("/* First comment\n" 1978 " * block will */\n" 1979 "/* Snd\n" 1980 " */\n", 1981 format("/* First comment block\n" 1982 " * will */\n" 1983 "/* Snd\n" 1984 " */\n", 1985 getLLVMStyleWithColumns(20))); 1986 1987 // Don't reflow lines having different indentation. 1988 EXPECT_EQ("// long long long\n" 1989 "// long\n" 1990 "// long", 1991 format("// long long long long\n" 1992 "// long", 1993 getLLVMStyleWithColumns(20))); 1994 1995 // Don't reflow separate bullets in list 1996 EXPECT_EQ("// - long long long\n" 1997 "// long\n" 1998 "// - long", 1999 format("// - long long long long\n" 2000 "// - long", 2001 getLLVMStyleWithColumns(20))); 2002 EXPECT_EQ("// * long long long\n" 2003 "// long\n" 2004 "// * long", 2005 format("// * long long long long\n" 2006 "// * long", 2007 getLLVMStyleWithColumns(20))); 2008 EXPECT_EQ("// + long long long\n" 2009 "// long\n" 2010 "// + long", 2011 format("// + long long long long\n" 2012 "// + long", 2013 getLLVMStyleWithColumns(20))); 2014 EXPECT_EQ("// 1. long long long\n" 2015 "// long\n" 2016 "// 2. long", 2017 format("// 1. long long long long\n" 2018 "// 2. long", 2019 getLLVMStyleWithColumns(20))); 2020 EXPECT_EQ("// -# long long long\n" 2021 "// long\n" 2022 "// -# long", 2023 format("// -# long long long long\n" 2024 "// -# long", 2025 getLLVMStyleWithColumns(20))); 2026 2027 EXPECT_EQ("// - long long long\n" 2028 "// long long long\n" 2029 "// - long", 2030 format("// - long long long long\n" 2031 "// long long\n" 2032 "// - long", 2033 getLLVMStyleWithColumns(20))); 2034 EXPECT_EQ("// - long long long\n" 2035 "// long long long\n" 2036 "// long\n" 2037 "// - long", 2038 format("// - long long long long\n" 2039 "// long long long\n" 2040 "// - long", 2041 getLLVMStyleWithColumns(20))); 2042 2043 // Large number (>2 digits) are not list items 2044 EXPECT_EQ("// long long long\n" 2045 "// long 1024. long.", 2046 format("// long long long long\n" 2047 "// 1024. long.", 2048 getLLVMStyleWithColumns(20))); 2049 2050 // Do not break before number, to avoid introducing a non-reflowable doxygen 2051 // list item. 2052 EXPECT_EQ("// long long\n" 2053 "// long 10. long.", 2054 format("// long long long 10.\n" 2055 "// long.", 2056 getLLVMStyleWithColumns(20))); 2057 2058 // Don't break or reflow after implicit string literals. 2059 verifyFormat("#include <t> // l l l\n" 2060 " // l", 2061 getLLVMStyleWithColumns(20)); 2062 2063 // Don't break or reflow comments on import lines. 2064 EXPECT_EQ("#include \"t\" /* l l l\n" 2065 " * l */", 2066 format("#include \"t\" /* l l l\n" 2067 " * l */", 2068 getLLVMStyleWithColumns(20))); 2069 2070 // Don't reflow between different trailing comment sections. 2071 EXPECT_EQ("int i; // long long\n" 2072 " // long\n" 2073 "int j; // long long\n" 2074 " // long\n", 2075 format("int i; // long long long\n" 2076 "int j; // long long long\n", 2077 getLLVMStyleWithColumns(20))); 2078 2079 // Don't reflow if the first word on the next line is longer than the 2080 // available space at current line. 2081 EXPECT_EQ("int i; // trigger\n" 2082 " // reflow\n" 2083 " // longsec\n", 2084 format("int i; // trigger reflow\n" 2085 " // longsec\n", 2086 getLLVMStyleWithColumns(20))); 2087 2088 // Simple case that correctly handles reflow in parameter lists. 2089 EXPECT_EQ("a = f(/* looooooooong\n" 2090 " * long long\n" 2091 " */\n" 2092 " a);", 2093 format("a = f(/* looooooooong long\n* long\n*/ a);", 2094 getLLVMStyleWithColumns(22))); 2095 // Tricky case that has fewer lines if we reflow the comment, ending up with 2096 // fewer lines. 2097 EXPECT_EQ("a = f(/* loooooong\n" 2098 " * long long\n" 2099 " */\n" 2100 " a);", 2101 format("a = f(/* loooooong long\n* long\n*/ a);", 2102 getLLVMStyleWithColumns(22))); 2103 2104 // Keep empty comment lines. 2105 EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20))); 2106 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20))); 2107 EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20))); 2108 EXPECT_EQ("//", format(" // ", getLLVMStyleWithColumns(20))); 2109 EXPECT_EQ("///", format(" /// ", getLLVMStyleWithColumns(20))); 2110 } 2111 2112 TEST_F(FormatTestComments, ReflowsCommentsPrecise) { 2113 // FIXME: This assumes we do not continue compressing whitespace once we are 2114 // in reflow mode. Consider compressing whitespace. 2115 2116 // Test that we stop reflowing precisely at the column limit. 2117 // After reflowing, "// reflows into foo" does not fit the column limit, 2118 // so we compress the whitespace. 2119 EXPECT_EQ("// some text that\n" 2120 "// reflows into foo\n", 2121 format("// some text that reflows\n" 2122 "// into foo\n", 2123 getLLVMStyleWithColumns(20))); 2124 // Given one more column, "// reflows into foo" does fit the limit, so we 2125 // do not compress the whitespace. 2126 EXPECT_EQ("// some text that\n" 2127 "// reflows into foo\n", 2128 format("// some text that reflows\n" 2129 "// into foo\n", 2130 getLLVMStyleWithColumns(21))); 2131 2132 // Make sure that we correctly account for the space added in the reflow case 2133 // when making the reflowing decision. 2134 // First, when the next line ends precisely one column over the limit, do not 2135 // reflow. 2136 EXPECT_EQ("// some text that\n" 2137 "// reflows\n" 2138 "// into1234567\n", 2139 format("// some text that reflows\n" 2140 "// into1234567\n", 2141 getLLVMStyleWithColumns(21))); 2142 // Secondly, when the next line ends later, but the first word in that line 2143 // is precisely one column over the limit, do not reflow. 2144 EXPECT_EQ("// some text that\n" 2145 "// reflows\n" 2146 "// into1234567 f\n", 2147 format("// some text that reflows\n" 2148 "// into1234567 f\n", 2149 getLLVMStyleWithColumns(21))); 2150 } 2151 2152 TEST_F(FormatTestComments, ReflowsCommentsWithExtraWhitespace) { 2153 // Baseline. 2154 EXPECT_EQ("// some text\n" 2155 "// that re flows\n", 2156 format("// some text that\n" 2157 "// re flows\n", 2158 getLLVMStyleWithColumns(16))); 2159 EXPECT_EQ("// some text\n" 2160 "// that re flows\n", 2161 format("// some text that\n" 2162 "// re flows\n", 2163 getLLVMStyleWithColumns(16))); 2164 EXPECT_EQ("/* some text\n" 2165 " * that re flows\n" 2166 " */\n", 2167 format("/* some text that\n" 2168 "* re flows\n" 2169 "*/\n", 2170 getLLVMStyleWithColumns(16))); 2171 // FIXME: We do not reflow if the indent of two subsequent lines differs; 2172 // given that this is different behavior from block comments, do we want 2173 // to keep this? 2174 EXPECT_EQ("// some text\n" 2175 "// that\n" 2176 "// re flows\n", 2177 format("// some text that\n" 2178 "// re flows\n", 2179 getLLVMStyleWithColumns(16))); 2180 // Space within parts of a line that fit. 2181 // FIXME: Use the earliest possible split while reflowing to compress the 2182 // whitespace within the line. 2183 EXPECT_EQ("// some text that\n" 2184 "// does re flow\n" 2185 "// more here\n", 2186 format("// some text that does\n" 2187 "// re flow more here\n", 2188 getLLVMStyleWithColumns(21))); 2189 } 2190 2191 TEST_F(FormatTestComments, IgnoresIf0Contents) { 2192 EXPECT_EQ("#if 0\n" 2193 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 2194 "#endif\n" 2195 "void f() {}", 2196 format("#if 0\n" 2197 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 2198 "#endif\n" 2199 "void f( ) { }")); 2200 EXPECT_EQ("#if false\n" 2201 "void f( ) { }\n" 2202 "#endif\n" 2203 "void g() {}\n", 2204 format("#if false\n" 2205 "void f( ) { }\n" 2206 "#endif\n" 2207 "void g( ) { }\n")); 2208 EXPECT_EQ("enum E {\n" 2209 " One,\n" 2210 " Two,\n" 2211 "#if 0\n" 2212 "Three,\n" 2213 " Four,\n" 2214 "#endif\n" 2215 " Five\n" 2216 "};", 2217 format("enum E {\n" 2218 " One,Two,\n" 2219 "#if 0\n" 2220 "Three,\n" 2221 " Four,\n" 2222 "#endif\n" 2223 " Five};")); 2224 EXPECT_EQ("enum F {\n" 2225 " One,\n" 2226 "#if 1\n" 2227 " Two,\n" 2228 "#if 0\n" 2229 "Three,\n" 2230 " Four,\n" 2231 "#endif\n" 2232 " Five\n" 2233 "#endif\n" 2234 "};", 2235 format("enum F {\n" 2236 "One,\n" 2237 "#if 1\n" 2238 "Two,\n" 2239 "#if 0\n" 2240 "Three,\n" 2241 " Four,\n" 2242 "#endif\n" 2243 "Five\n" 2244 "#endif\n" 2245 "};")); 2246 EXPECT_EQ("enum G {\n" 2247 " One,\n" 2248 "#if 0\n" 2249 "Two,\n" 2250 "#else\n" 2251 " Three,\n" 2252 "#endif\n" 2253 " Four\n" 2254 "};", 2255 format("enum G {\n" 2256 "One,\n" 2257 "#if 0\n" 2258 "Two,\n" 2259 "#else\n" 2260 "Three,\n" 2261 "#endif\n" 2262 "Four\n" 2263 "};")); 2264 EXPECT_EQ("enum H {\n" 2265 " One,\n" 2266 "#if 0\n" 2267 "#ifdef Q\n" 2268 "Two,\n" 2269 "#else\n" 2270 "Three,\n" 2271 "#endif\n" 2272 "#endif\n" 2273 " Four\n" 2274 "};", 2275 format("enum H {\n" 2276 "One,\n" 2277 "#if 0\n" 2278 "#ifdef Q\n" 2279 "Two,\n" 2280 "#else\n" 2281 "Three,\n" 2282 "#endif\n" 2283 "#endif\n" 2284 "Four\n" 2285 "};")); 2286 EXPECT_EQ("enum I {\n" 2287 " One,\n" 2288 "#if /* test */ 0 || 1\n" 2289 "Two,\n" 2290 "Three,\n" 2291 "#endif\n" 2292 " Four\n" 2293 "};", 2294 format("enum I {\n" 2295 "One,\n" 2296 "#if /* test */ 0 || 1\n" 2297 "Two,\n" 2298 "Three,\n" 2299 "#endif\n" 2300 "Four\n" 2301 "};")); 2302 EXPECT_EQ("enum J {\n" 2303 " One,\n" 2304 "#if 0\n" 2305 "#if 0\n" 2306 "Two,\n" 2307 "#else\n" 2308 "Three,\n" 2309 "#endif\n" 2310 "Four,\n" 2311 "#endif\n" 2312 " Five\n" 2313 "};", 2314 format("enum J {\n" 2315 "One,\n" 2316 "#if 0\n" 2317 "#if 0\n" 2318 "Two,\n" 2319 "#else\n" 2320 "Three,\n" 2321 "#endif\n" 2322 "Four,\n" 2323 "#endif\n" 2324 "Five\n" 2325 "};")); 2326 2327 // Ignore stuff in SWIG-blocks. 2328 EXPECT_EQ("#ifdef SWIG\n" 2329 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 2330 "#endif\n" 2331 "void f() {}", 2332 format("#ifdef SWIG\n" 2333 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" 2334 "#endif\n" 2335 "void f( ) { }")); 2336 EXPECT_EQ("#ifndef SWIG\n" 2337 "void f() {}\n" 2338 "#endif", 2339 format("#ifndef SWIG\n" 2340 "void f( ) { }\n" 2341 "#endif")); 2342 } 2343 2344 TEST_F(FormatTestComments, DontCrashOnBlockComments) { 2345 EXPECT_EQ( 2346 "int xxxxxxxxx; /* " 2347 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" 2348 "zzzzzz\n" 2349 "0*/", 2350 format("int xxxxxxxxx; /* " 2351 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" 2352 "0*/")); 2353 } 2354 2355 TEST_F(FormatTestComments, BlockCommentsInControlLoops) { 2356 verifyFormat("if (0) /* a comment in a strange place */ {\n" 2357 " f();\n" 2358 "}"); 2359 verifyFormat("if (0) /* a comment in a strange place */ {\n" 2360 " f();\n" 2361 "} /* another comment */ else /* comment #3 */ {\n" 2362 " g();\n" 2363 "}"); 2364 verifyFormat("while (0) /* a comment in a strange place */ {\n" 2365 " f();\n" 2366 "}"); 2367 verifyFormat("for (;;) /* a comment in a strange place */ {\n" 2368 " f();\n" 2369 "}"); 2370 verifyFormat("do /* a comment in a strange place */ {\n" 2371 " f();\n" 2372 "} /* another comment */ while (0);"); 2373 } 2374 2375 TEST_F(FormatTestComments, BlockComments) { 2376 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", 2377 format("/* *//* */ /* */\n/* *//* */ /* */")); 2378 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); 2379 EXPECT_EQ("#define A /*123*/ \\\n" 2380 " b\n" 2381 "/* */\n" 2382 "someCall(\n" 2383 " parameter);", 2384 format("#define A /*123*/ b\n" 2385 "/* */\n" 2386 "someCall(parameter);", 2387 getLLVMStyleWithColumns(15))); 2388 2389 EXPECT_EQ("#define A\n" 2390 "/* */ someCall(\n" 2391 " parameter);", 2392 format("#define A\n" 2393 "/* */someCall(parameter);", 2394 getLLVMStyleWithColumns(15))); 2395 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); 2396 EXPECT_EQ("/*\n" 2397 " *\n" 2398 " * aaaaaa\n" 2399 " * aaaaaa\n" 2400 " */", 2401 format("/*\n" 2402 "*\n" 2403 " * aaaaaa aaaaaa\n" 2404 "*/", 2405 getLLVMStyleWithColumns(10))); 2406 EXPECT_EQ("/*\n" 2407 "**\n" 2408 "* aaaaaa\n" 2409 "*aaaaaa\n" 2410 "*/", 2411 format("/*\n" 2412 "**\n" 2413 "* aaaaaa aaaaaa\n" 2414 "*/", 2415 getLLVMStyleWithColumns(10))); 2416 EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2417 " /* line 1\n" 2418 " bbbbbbbbbbbb */\n" 2419 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 2420 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 2421 " /* line 1\n" 2422 " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 2423 getLLVMStyleWithColumns(50))); 2424 2425 FormatStyle NoBinPacking = getLLVMStyle(); 2426 NoBinPacking.BinPackParameters = false; 2427 EXPECT_EQ("someFunction(1, /* comment 1 */\n" 2428 " 2, /* comment 2 */\n" 2429 " 3, /* comment 3 */\n" 2430 " aaaa,\n" 2431 " bbbb);", 2432 format("someFunction (1, /* comment 1 */\n" 2433 " 2, /* comment 2 */ \n" 2434 " 3, /* comment 3 */\n" 2435 "aaaa, bbbb );", 2436 NoBinPacking)); 2437 verifyFormat( 2438 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 2440 EXPECT_EQ( 2441 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 2442 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2443 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", 2444 format( 2445 "bool aaaaaaaaaaaaa = /* trailing comment */\n" 2446 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); 2448 EXPECT_EQ( 2449 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 2450 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 2451 "int cccccccccccccccccccccccccccccc; /* comment */\n", 2452 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" 2453 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" 2454 "int cccccccccccccccccccccccccccccc; /* comment */\n")); 2455 2456 verifyFormat("void f(int * /* unused */) {}"); 2457 2458 EXPECT_EQ("/*\n" 2459 " **\n" 2460 " */", 2461 format("/*\n" 2462 " **\n" 2463 " */")); 2464 EXPECT_EQ("/*\n" 2465 " *q\n" 2466 " */", 2467 format("/*\n" 2468 " *q\n" 2469 " */")); 2470 EXPECT_EQ("/*\n" 2471 " * q\n" 2472 " */", 2473 format("/*\n" 2474 " * q\n" 2475 " */")); 2476 EXPECT_EQ("/*\n" 2477 " **/", 2478 format("/*\n" 2479 " **/")); 2480 EXPECT_EQ("/*\n" 2481 " ***/", 2482 format("/*\n" 2483 " ***/")); 2484 } 2485 2486 TEST_F(FormatTestComments, BlockCommentsInMacros) { 2487 EXPECT_EQ("#define A \\\n" 2488 " { \\\n" 2489 " /* one line */ \\\n" 2490 " someCall();", 2491 format("#define A { \\\n" 2492 " /* one line */ \\\n" 2493 " someCall();", 2494 getLLVMStyleWithColumns(20))); 2495 EXPECT_EQ("#define A \\\n" 2496 " { \\\n" 2497 " /* previous */ \\\n" 2498 " /* one line */ \\\n" 2499 " someCall();", 2500 format("#define A { \\\n" 2501 " /* previous */ \\\n" 2502 " /* one line */ \\\n" 2503 " someCall();", 2504 getLLVMStyleWithColumns(20))); 2505 } 2506 2507 TEST_F(FormatTestComments, BlockCommentsAtEndOfLine) { 2508 EXPECT_EQ("a = {\n" 2509 " 1111 /* */\n" 2510 "};", 2511 format("a = {1111 /* */\n" 2512 "};", 2513 getLLVMStyleWithColumns(15))); 2514 EXPECT_EQ("a = {\n" 2515 " 1111 /* */\n" 2516 "};", 2517 format("a = {1111 /* */\n" 2518 "};", 2519 getLLVMStyleWithColumns(15))); 2520 EXPECT_EQ("a = {\n" 2521 " 1111 /* a\n" 2522 " */\n" 2523 "};", 2524 format("a = {1111 /* a */\n" 2525 "};", 2526 getLLVMStyleWithColumns(15))); 2527 } 2528 2529 TEST_F(FormatTestComments, BreaksAfterMultilineBlockCommentsInParamLists) { 2530 EXPECT_EQ("a = f(/* long\n" 2531 " long */\n" 2532 " a);", 2533 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(16))); 2534 EXPECT_EQ("a = f(\n" 2535 " /* long\n" 2536 " long */\n" 2537 " a);", 2538 format("a = f(/* long long */ a);", getLLVMStyleWithColumns(15))); 2539 2540 EXPECT_EQ("a = f(/* long\n" 2541 " long\n" 2542 " */\n" 2543 " a);", 2544 format("a = f(/* long\n" 2545 " long\n" 2546 " */a);", 2547 getLLVMStyleWithColumns(16))); 2548 2549 EXPECT_EQ("a = f(/* long\n" 2550 " long\n" 2551 " */\n" 2552 " a);", 2553 format("a = f(/* long\n" 2554 " long\n" 2555 " */ a);", 2556 getLLVMStyleWithColumns(16))); 2557 2558 EXPECT_EQ("a = f(/* long\n" 2559 " long\n" 2560 " */\n" 2561 " (1 + 1));", 2562 format("a = f(/* long\n" 2563 " long\n" 2564 " */ (1 + 1));", 2565 getLLVMStyleWithColumns(16))); 2566 2567 EXPECT_EQ( 2568 "a = f(a,\n" 2569 " /* long\n" 2570 " long */\n" 2571 " b);", 2572 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(16))); 2573 2574 EXPECT_EQ( 2575 "a = f(\n" 2576 " a,\n" 2577 " /* long\n" 2578 " long */\n" 2579 " b);", 2580 format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(15))); 2581 2582 EXPECT_EQ("a = f(a,\n" 2583 " /* long\n" 2584 " long */\n" 2585 " (1 + 1));", 2586 format("a = f(a, /* long long */ (1 + 1));", 2587 getLLVMStyleWithColumns(16))); 2588 EXPECT_EQ("a = f(\n" 2589 " a,\n" 2590 " /* long\n" 2591 " long */\n" 2592 " (1 + 1));", 2593 format("a = f(a, /* long long */ (1 + 1));", 2594 getLLVMStyleWithColumns(15))); 2595 } 2596 2597 TEST_F(FormatTestComments, IndentLineCommentsInStartOfBlockAtEndOfFile) { 2598 verifyFormat("{\n" 2599 " // a\n" 2600 " // b"); 2601 } 2602 2603 TEST_F(FormatTestComments, AlignTrailingComments) { 2604 EXPECT_EQ("#define MACRO(V) \\\n" 2605 " V(Rt2) /* one more char */ \\\n" 2606 " V(Rs) /* than here */ \\\n" 2607 "/* comment 3 */\n", 2608 format("#define MACRO(V)\\\n" 2609 "V(Rt2) /* one more char */ \\\n" 2610 "V(Rs) /* than here */ \\\n" 2611 "/* comment 3 */\n", 2612 getLLVMStyleWithColumns(40))); 2613 EXPECT_EQ("int i = f(abc, // line 1\n" 2614 " d, // line 2\n" 2615 " // line 3\n" 2616 " b);", 2617 format("int i = f(abc, // line 1\n" 2618 " d, // line 2\n" 2619 " // line 3\n" 2620 " b);", 2621 getLLVMStyleWithColumns(40))); 2622 2623 // Align newly broken trailing comments. 2624 EXPECT_EQ("int ab; // line\n" 2625 "int a; // long\n" 2626 " // long\n", 2627 format("int ab; // line\n" 2628 "int a; // long long\n", 2629 getLLVMStyleWithColumns(15))); 2630 EXPECT_EQ("int ab; // line\n" 2631 "int a; // long\n" 2632 " // long\n" 2633 " // long", 2634 format("int ab; // line\n" 2635 "int a; // long long\n" 2636 " // long", 2637 getLLVMStyleWithColumns(15))); 2638 EXPECT_EQ("int ab; // line\n" 2639 "int a; // long\n" 2640 " // long\n" 2641 "pt c; // long", 2642 format("int ab; // line\n" 2643 "int a; // long long\n" 2644 "pt c; // long", 2645 getLLVMStyleWithColumns(15))); 2646 EXPECT_EQ("int ab; // line\n" 2647 "int a; // long\n" 2648 " // long\n" 2649 "\n" 2650 "// long", 2651 format("int ab; // line\n" 2652 "int a; // long long\n" 2653 "\n" 2654 "// long", 2655 getLLVMStyleWithColumns(15))); 2656 2657 // Don't align newly broken trailing comments if that would put them over the 2658 // column limit. 2659 EXPECT_EQ("int i, j; // line 1\n" 2660 "int k; // line longg\n" 2661 " // long", 2662 format("int i, j; // line 1\n" 2663 "int k; // line longg long", 2664 getLLVMStyleWithColumns(20))); 2665 2666 // Always align if ColumnLimit = 0 2667 EXPECT_EQ("int i, j; // line 1\n" 2668 "int k; // line longg long", 2669 format("int i, j; // line 1\n" 2670 "int k; // line longg long", 2671 getLLVMStyleWithColumns(0))); 2672 2673 // Align comment line sections aligned with the next token with the next 2674 // token. 2675 EXPECT_EQ("class A {\n" 2676 "public: // public comment\n" 2677 " // comment about a\n" 2678 " int a;\n" 2679 "};", 2680 format("class A {\n" 2681 "public: // public comment\n" 2682 " // comment about a\n" 2683 " int a;\n" 2684 "};", 2685 getLLVMStyleWithColumns(40))); 2686 EXPECT_EQ("class A {\n" 2687 "public: // public comment 1\n" 2688 " // public comment 2\n" 2689 " // comment 1 about a\n" 2690 " // comment 2 about a\n" 2691 " int a;\n" 2692 "};", 2693 format("class A {\n" 2694 "public: // public comment 1\n" 2695 " // public comment 2\n" 2696 " // comment 1 about a\n" 2697 " // comment 2 about a\n" 2698 " int a;\n" 2699 "};", 2700 getLLVMStyleWithColumns(40))); 2701 EXPECT_EQ("int f(int n) { // comment line 1 on f\n" 2702 " // comment line 2 on f\n" 2703 " // comment line 1 before return\n" 2704 " // comment line 2 before return\n" 2705 " return n; // comment line 1 on return\n" 2706 " // comment line 2 on return\n" 2707 " // comment line 1 after return\n" 2708 "}", 2709 format("int f(int n) { // comment line 1 on f\n" 2710 " // comment line 2 on f\n" 2711 " // comment line 1 before return\n" 2712 " // comment line 2 before return\n" 2713 " return n; // comment line 1 on return\n" 2714 " // comment line 2 on return\n" 2715 " // comment line 1 after return\n" 2716 "}", 2717 getLLVMStyleWithColumns(40))); 2718 EXPECT_EQ("int f(int n) {\n" 2719 " switch (n) { // comment line 1 on switch\n" 2720 " // comment line 2 on switch\n" 2721 " // comment line 1 before case 1\n" 2722 " // comment line 2 before case 1\n" 2723 " case 1: // comment line 1 on case 1\n" 2724 " // comment line 2 on case 1\n" 2725 " // comment line 1 before return 1\n" 2726 " // comment line 2 before return 1\n" 2727 " return 1; // comment line 1 on return 1\n" 2728 " // comment line 2 on return 1\n" 2729 " // comment line 1 before default\n" 2730 " // comment line 2 before default\n" 2731 " default: // comment line 1 on default\n" 2732 " // comment line 2 on default\n" 2733 " // comment line 1 before return 2\n" 2734 " return 2 * f(n - 1); // comment line 1 on return 2\n" 2735 " // comment line 2 on return 2\n" 2736 " // comment line 1 after return\n" 2737 " // comment line 2 after return\n" 2738 " }\n" 2739 "}", 2740 format("int f(int n) {\n" 2741 " switch (n) { // comment line 1 on switch\n" 2742 " // comment line 2 on switch\n" 2743 " // comment line 1 before case 1\n" 2744 " // comment line 2 before case 1\n" 2745 " case 1: // comment line 1 on case 1\n" 2746 " // comment line 2 on case 1\n" 2747 " // comment line 1 before return 1\n" 2748 " // comment line 2 before return 1\n" 2749 " return 1; // comment line 1 on return 1\n" 2750 " // comment line 2 on return 1\n" 2751 " // comment line 1 before default\n" 2752 " // comment line 2 before default\n" 2753 " default: // comment line 1 on default\n" 2754 " // comment line 2 on default\n" 2755 " // comment line 1 before return 2\n" 2756 " return 2 * f(n - 1); // comment line 1 on return 2\n" 2757 " // comment line 2 on return 2\n" 2758 " // comment line 1 after return\n" 2759 " // comment line 2 after return\n" 2760 " }\n" 2761 "}", 2762 getLLVMStyleWithColumns(80))); 2763 2764 // If all the lines in a sequence of line comments are aligned with the next 2765 // token, the first line belongs to the previous token and the other lines 2766 // belong to the next token. 2767 EXPECT_EQ("int a; // line about a\n" 2768 "long b;", 2769 format("int a; // line about a\n" 2770 " long b;", 2771 getLLVMStyleWithColumns(80))); 2772 EXPECT_EQ("int a; // line about a\n" 2773 "// line about b\n" 2774 "long b;", 2775 format("int a; // line about a\n" 2776 " // line about b\n" 2777 " long b;", 2778 getLLVMStyleWithColumns(80))); 2779 EXPECT_EQ("int a; // line about a\n" 2780 "// line 1 about b\n" 2781 "// line 2 about b\n" 2782 "long b;", 2783 format("int a; // line about a\n" 2784 " // line 1 about b\n" 2785 " // line 2 about b\n" 2786 " long b;", 2787 getLLVMStyleWithColumns(80))); 2788 } 2789 2790 TEST_F(FormatTestComments, AlignsBlockCommentDecorations) { 2791 EXPECT_EQ("/*\n" 2792 " */", 2793 format("/*\n" 2794 "*/", getLLVMStyle())); 2795 EXPECT_EQ("/*\n" 2796 " */", 2797 format("/*\n" 2798 " */", getLLVMStyle())); 2799 EXPECT_EQ("/*\n" 2800 " */", 2801 format("/*\n" 2802 " */", getLLVMStyle())); 2803 2804 // Align a single line. 2805 EXPECT_EQ("/*\n" 2806 " * line */", 2807 format("/*\n" 2808 "* line */", 2809 getLLVMStyle())); 2810 EXPECT_EQ("/*\n" 2811 " * line */", 2812 format("/*\n" 2813 " * line */", 2814 getLLVMStyle())); 2815 EXPECT_EQ("/*\n" 2816 " * line */", 2817 format("/*\n" 2818 " * line */", 2819 getLLVMStyle())); 2820 EXPECT_EQ("/*\n" 2821 " * line */", 2822 format("/*\n" 2823 " * line */", 2824 getLLVMStyle())); 2825 EXPECT_EQ("/**\n" 2826 " * line */", 2827 format("/**\n" 2828 "* line */", 2829 getLLVMStyle())); 2830 EXPECT_EQ("/**\n" 2831 " * line */", 2832 format("/**\n" 2833 " * line */", 2834 getLLVMStyle())); 2835 EXPECT_EQ("/**\n" 2836 " * line */", 2837 format("/**\n" 2838 " * line */", 2839 getLLVMStyle())); 2840 EXPECT_EQ("/**\n" 2841 " * line */", 2842 format("/**\n" 2843 " * line */", 2844 getLLVMStyle())); 2845 EXPECT_EQ("/**\n" 2846 " * line */", 2847 format("/**\n" 2848 " * line */", 2849 getLLVMStyle())); 2850 2851 // Align the end '*/' after a line. 2852 EXPECT_EQ("/*\n" 2853 " * line\n" 2854 " */", 2855 format("/*\n" 2856 "* line\n" 2857 "*/", getLLVMStyle())); 2858 EXPECT_EQ("/*\n" 2859 " * line\n" 2860 " */", 2861 format("/*\n" 2862 " * line\n" 2863 " */", getLLVMStyle())); 2864 EXPECT_EQ("/*\n" 2865 " * line\n" 2866 " */", 2867 format("/*\n" 2868 " * line\n" 2869 " */", getLLVMStyle())); 2870 2871 // Align two lines. 2872 EXPECT_EQ("/* line 1\n" 2873 " * line 2 */", 2874 format("/* line 1\n" 2875 " * line 2 */", 2876 getLLVMStyle())); 2877 EXPECT_EQ("/* line 1\n" 2878 " * line 2 */", 2879 format("/* line 1\n" 2880 "* line 2 */", 2881 getLLVMStyle())); 2882 EXPECT_EQ("/* line 1\n" 2883 " * line 2 */", 2884 format("/* line 1\n" 2885 " * line 2 */", 2886 getLLVMStyle())); 2887 EXPECT_EQ("/* line 1\n" 2888 " * line 2 */", 2889 format("/* line 1\n" 2890 " * line 2 */", 2891 getLLVMStyle())); 2892 EXPECT_EQ("/* line 1\n" 2893 " * line 2 */", 2894 format("/* line 1\n" 2895 " * line 2 */", 2896 getLLVMStyle())); 2897 EXPECT_EQ("int i; /* line 1\n" 2898 " * line 2 */", 2899 format("int i; /* line 1\n" 2900 "* line 2 */", 2901 getLLVMStyle())); 2902 EXPECT_EQ("int i; /* line 1\n" 2903 " * line 2 */", 2904 format("int i; /* line 1\n" 2905 " * line 2 */", 2906 getLLVMStyle())); 2907 EXPECT_EQ("int i; /* line 1\n" 2908 " * line 2 */", 2909 format("int i; /* line 1\n" 2910 " * line 2 */", 2911 getLLVMStyle())); 2912 2913 // Align several lines. 2914 EXPECT_EQ("/* line 1\n" 2915 " * line 2\n" 2916 " * line 3 */", 2917 format("/* line 1\n" 2918 " * line 2\n" 2919 "* line 3 */", 2920 getLLVMStyle())); 2921 EXPECT_EQ("/* line 1\n" 2922 " * line 2\n" 2923 " * line 3 */", 2924 format("/* line 1\n" 2925 " * line 2\n" 2926 "* line 3 */", 2927 getLLVMStyle())); 2928 EXPECT_EQ("/*\n" 2929 "** line 1\n" 2930 "** line 2\n" 2931 "*/", 2932 format("/*\n" 2933 "** line 1\n" 2934 " ** line 2\n" 2935 "*/", 2936 getLLVMStyle())); 2937 2938 // Align with different indent after the decorations. 2939 EXPECT_EQ("/*\n" 2940 " * line 1\n" 2941 " * line 2\n" 2942 " * line 3\n" 2943 " * line 4\n" 2944 " */", 2945 format("/*\n" 2946 "* line 1\n" 2947 " * line 2\n" 2948 " * line 3\n" 2949 "* line 4\n" 2950 "*/", getLLVMStyle())); 2951 2952 // Align empty or blank lines. 2953 EXPECT_EQ("/**\n" 2954 " *\n" 2955 " *\n" 2956 " *\n" 2957 " */", 2958 format("/**\n" 2959 "* \n" 2960 " * \n" 2961 " *\n" 2962 "*/", getLLVMStyle())); 2963 2964 // Align while breaking and reflowing. 2965 EXPECT_EQ("/*\n" 2966 " * long long long\n" 2967 " * long long\n" 2968 " *\n" 2969 " * long */", 2970 format("/*\n" 2971 " * long long long long\n" 2972 " * long\n" 2973 " *\n" 2974 "* long */", 2975 getLLVMStyleWithColumns(20))); 2976 } 2977 2978 TEST_F(FormatTestComments, NoCrash_Bug34236) { 2979 // This is a test case from a crasher reported in: 2980 // https://bugs.llvm.org/show_bug.cgi?id=34236 2981 // Temporarily disable formatting for readability. 2982 // clang-format off 2983 EXPECT_EQ( 2984 "/* */ /*\n" 2985 " * a\n" 2986 " * b c d*/", 2987 format( 2988 "/* */ /*\n" 2989 " * a b\n" 2990 " * c d*/", 2991 getLLVMStyleWithColumns(80))); 2992 // clang-format on 2993 } 2994 2995 TEST_F(FormatTestComments, NonTrailingBlockComments) { 2996 verifyFormat("const /** comment comment */ A = B;", 2997 getLLVMStyleWithColumns(40)); 2998 2999 verifyFormat("const /** comment comment comment */ A =\n" 3000 " B;", 3001 getLLVMStyleWithColumns(40)); 3002 3003 EXPECT_EQ("const /** comment comment comment\n" 3004 " comment */\n" 3005 " A = B;", 3006 format("const /** comment comment comment comment */\n" 3007 " A = B;", 3008 getLLVMStyleWithColumns(40))); 3009 } 3010 3011 TEST_F(FormatTestComments, PythonStyleComments) { 3012 // Keeps a space after '#'. 3013 EXPECT_EQ("# comment\n" 3014 "key: value", 3015 format("#comment\n" 3016 "key:value", 3017 getTextProtoStyleWithColumns(20))); 3018 EXPECT_EQ("# comment\n" 3019 "key: value", 3020 format("# comment\n" 3021 "key:value", 3022 getTextProtoStyleWithColumns(20))); 3023 // Breaks long comment. 3024 EXPECT_EQ("# comment comment\n" 3025 "# comment\n" 3026 "key: value", 3027 format("# comment comment comment\n" 3028 "key:value", 3029 getTextProtoStyleWithColumns(20))); 3030 // Indents comments. 3031 EXPECT_EQ("data {\n" 3032 " # comment comment\n" 3033 " # comment\n" 3034 " key: value\n" 3035 "}", 3036 format("data {\n" 3037 "# comment comment comment\n" 3038 "key: value}", 3039 getTextProtoStyleWithColumns(20))); 3040 EXPECT_EQ("data {\n" 3041 " # comment comment\n" 3042 " # comment\n" 3043 " key: value\n" 3044 "}", 3045 format("data {# comment comment comment\n" 3046 "key: value}", 3047 getTextProtoStyleWithColumns(20))); 3048 // Reflows long comments. 3049 EXPECT_EQ("# comment comment\n" 3050 "# comment comment\n" 3051 "key: value", 3052 format("# comment comment comment\n" 3053 "# comment\n" 3054 "key:value", 3055 getTextProtoStyleWithColumns(20))); 3056 // Breaks trailing comments. 3057 EXPECT_EQ("k: val # comment\n" 3058 " # comment\n" 3059 "a: 1", 3060 format("k:val#comment comment\n" 3061 "a:1", 3062 getTextProtoStyleWithColumns(20))); 3063 EXPECT_EQ("id {\n" 3064 " k: val # comment\n" 3065 " # comment\n" 3066 " # line line\n" 3067 " a: 1\n" 3068 "}", 3069 format("id {k:val#comment comment\n" 3070 "# line line\n" 3071 "a:1}", 3072 getTextProtoStyleWithColumns(20))); 3073 // Aligns trailing comments. 3074 EXPECT_EQ("k: val # commen1\n" 3075 " # commen2\n" 3076 " # commen3\n" 3077 "# commen4\n" 3078 "a: 1 # commen5\n" 3079 " # commen6\n" 3080 " # commen7", 3081 format("k:val#commen1 commen2\n" 3082 " # commen3\n" 3083 "# commen4\n" 3084 "a:1#commen5 commen6\n" 3085 " #commen7", 3086 getTextProtoStyleWithColumns(20))); 3087 } 3088 3089 TEST_F(FormatTestComments, BreaksBeforeTrailingUnbreakableSequence) { 3090 // The end of /* trail */ is exactly at 80 columns, but the unbreakable 3091 // trailing sequence ); after it exceeds the column limit. Make sure we 3092 // correctly break the line in that case. 3093 verifyFormat("int a =\n" 3094 " foo(/* trail */);", 3095 getLLVMStyleWithColumns(23)); 3096 } 3097 3098 TEST_F(FormatTestComments, ReflowBackslashCrash) { 3099 // clang-format off 3100 EXPECT_EQ( 3101 "// How to run:\n" 3102 "// bbbbb run \\\n" 3103 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\n" 3104 "// \\ <log_file> -- --output_directory=\"<output_directory>\"", 3105 format( 3106 "// How to run:\n" 3107 "// bbbbb run \\\n" 3108 "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr \\\n" 3109 "// <log_file> -- --output_directory=\"<output_directory>\"")); 3110 // clang-format on 3111 } 3112 3113 TEST_F(FormatTestComments, IndentsLongJavadocAnnotatedLines) { 3114 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java); 3115 Style.ColumnLimit = 60; 3116 FormatStyle Style20 = getGoogleStyle(FormatStyle::LK_Java); 3117 Style20.ColumnLimit = 20; 3118 EXPECT_EQ( 3119 "/**\n" 3120 " * @param x long long long long long long long long long\n" 3121 " * long\n" 3122 " */\n", 3123 format("/**\n" 3124 " * @param x long long long long long long long long long long\n" 3125 " */\n", 3126 Style)); 3127 EXPECT_EQ("/**\n" 3128 " * @param x long long long long long long long long long\n" 3129 " * long long long long long long long long long long\n" 3130 " */\n", 3131 format("/**\n" 3132 " * @param x long long long long long long long long long " 3133 "long long long long long long long long long long\n" 3134 " */\n", 3135 Style)); 3136 EXPECT_EQ("/**\n" 3137 " * @param x long long long long long long long long long\n" 3138 " * long long long long long long long long long long\n" 3139 " * long\n" 3140 " */\n", 3141 format("/**\n" 3142 " * @param x long long long long long long long long long " 3143 "long long long long long long long long long long long\n" 3144 " */\n", 3145 Style)); 3146 EXPECT_EQ( 3147 "/**\n" 3148 " * Sentence that\n" 3149 " * should be broken.\n" 3150 " * @param short\n" 3151 " * keep indentation\n" 3152 " */\n", format( 3153 "/**\n" 3154 " * Sentence that should be broken.\n" 3155 " * @param short\n" 3156 " * keep indentation\n" 3157 " */\n", Style20)); 3158 3159 EXPECT_EQ("/**\n" 3160 " * @param l1 long1\n" 3161 " * to break\n" 3162 " * @param l2 long2\n" 3163 " * to break\n" 3164 " */\n", 3165 format("/**\n" 3166 " * @param l1 long1 to break\n" 3167 " * @param l2 long2 to break\n" 3168 " */\n", 3169 Style20)); 3170 3171 EXPECT_EQ("/**\n" 3172 " * @param xx to\n" 3173 " * break\n" 3174 " * no reflow\n" 3175 " */\n", 3176 format("/**\n" 3177 " * @param xx to break\n" 3178 " * no reflow\n" 3179 " */\n", 3180 Style20)); 3181 3182 EXPECT_EQ("/**\n" 3183 " * @param xx to\n" 3184 " * break yes\n" 3185 " * reflow\n" 3186 " */\n", 3187 format("/**\n" 3188 " * @param xx to break\n" 3189 " * yes reflow\n" 3190 " */\n", 3191 Style20)); 3192 3193 FormatStyle JSStyle20 = getGoogleStyle(FormatStyle::LK_JavaScript); 3194 JSStyle20.ColumnLimit = 20; 3195 EXPECT_EQ("/**\n" 3196 " * @param l1 long1\n" 3197 " * to break\n" 3198 " */\n", 3199 format("/**\n" 3200 " * @param l1 long1 to break\n" 3201 " */\n", 3202 JSStyle20)); 3203 EXPECT_EQ("/**\n" 3204 " * @param {l1 long1\n" 3205 " * to break}\n" 3206 " */\n", 3207 format("/**\n" 3208 " * @param {l1 long1 to break}\n" 3209 " */\n", 3210 JSStyle20)); 3211 } 3212 3213 } // end namespace 3214 } // end namespace format 3215 } // end namespace clang 3216