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