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