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