diff options
Diffstat (limited to 'clang/unittests/Format')
| -rw-r--r-- | clang/unittests/Format/FormatTestComments.cpp | 6627 | ||||
| -rw-r--r-- | clang/unittests/Format/TokenAnnotatorTest.cpp | 9 |
2 files changed, 3216 insertions, 3420 deletions
diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp index 6b433bb..399f835 100644 --- a/clang/unittests/Format/FormatTestComments.cpp +++ b/clang/unittests/Format/FormatTestComments.cpp @@ -29,13 +29,13 @@ TEST_F(FormatTestComments, UnderstandsSingleLineComments) { "// line 2\n" "void f() {}"); - EXPECT_EQ("// comment", format("//comment")); - EXPECT_EQ("// #comment", format("//#comment")); + verifyFormat("// comment", "//comment"); + verifyFormat("// #comment", "//#comment"); - EXPECT_EQ("// comment\n" - "// clang-format on", - format("//comment\n" - "// clang-format on")); + verifyFormat("// comment\n" + "// clang-format on", + "//comment\n" + "// clang-format on"); verifyFormat("void f() {\n" " // Doesn't do anything\n" @@ -84,11 +84,11 @@ TEST_F(FormatTestComments, UnderstandsSingleLineComments) { "#include \"a/b/c\" // comment"); verifyFormat("#include <a> // comment\n" "#include <a/b/c> // comment"); - EXPECT_EQ("#include \"a\" // comment\n" - "#include \"a/b/c\" // comment", - format("#include \\\n" - " \"a\" // comment\n" - "#include \"a/b/c\" // comment")); + verifyFormat("#include \"a\" // comment\n" + "#include \"a/b/c\" // comment", + "#include \\\n" + " \"a\" // comment\n" + "#include \"a/b/c\" // comment"); verifyFormat("enum E {\n" " // comment\n" @@ -96,63 +96,65 @@ TEST_F(FormatTestComments, UnderstandsSingleLineComments) { " VAL_B\n" "};"); - EXPECT_EQ("enum A {\n" - " // line a\n" - " a,\n" - " b, // line b\n" - "\n" - " // line c\n" - " c\n" - "};", - format("enum A {\n" - " // line a\n" - " a,\n" - " b, // line b\n" - "\n" - " // line c\n" - " c\n" - "};", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("enum A {\n" - " a, // line 1\n" - " // line 2\n" - "};", - format("enum A {\n" - " a, // line 1\n" - " // line 2\n" - "};", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("enum A {\n" - " a, // line 1\n" - " // line 2\n" - "};", - format("enum A {\n" - " a, // line 1\n" - " // line 2\n" - "};", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("enum A {\n" - " a, // line 1\n" - " // line 2\n" - " b\n" - "};", - format("enum A {\n" - " a, // line 1\n" - " // line 2\n" - " b\n" - "};", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("enum A {\n" - " a, // line 1\n" - " // line 2\n" - " b\n" - "};", - format("enum A {\n" - " a, // line 1\n" - " // line 2\n" - " b\n" - "};", - getLLVMStyleWithColumns(20))); + const auto Style20 = getLLVMStyleWithColumns(20); + + verifyFormat("enum A {\n" + " // line a\n" + " a,\n" + " b, // line b\n" + "\n" + " // line c\n" + " c\n" + "};", + "enum A {\n" + " // line a\n" + " a,\n" + " b, // line b\n" + "\n" + " // line c\n" + " c\n" + "};", + Style20); + verifyFormat("enum A {\n" + " a, // line 1\n" + " // line 2\n" + "};", + "enum A {\n" + " a, // line 1\n" + " // line 2\n" + "};", + Style20); + verifyFormat("enum A {\n" + " a, // line 1\n" + " // line 2\n" + "};", + "enum A {\n" + " a, // line 1\n" + " // line 2\n" + "};", + Style20); + verifyFormat("enum A {\n" + " a, // line 1\n" + " // line 2\n" + " b\n" + "};", + "enum A {\n" + " a, // line 1\n" + " // line 2\n" + " b\n" + "};", + Style20); + verifyFormat("enum A {\n" + " a, // line 1\n" + " // line 2\n" + " b\n" + "};", + "enum A {\n" + " a, // line 1\n" + " // line 2\n" + " b\n" + "};", + Style20); verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); @@ -172,28 +174,28 @@ TEST_F(FormatTestComments, UnderstandsSingleLineComments) { verifyFormat("int aaaa; // aaaaa\n" "int aa; // aaaaaaa", - getLLVMStyleWithColumns(20)); + Style20); + + verifyFormat("void f() { // This does something ..\n" + "}\n" + "int a; // This is unrelated", + "void f() { // This does something ..\n" + " }\n" + "int a; // This is unrelated"); + verifyFormat("class C {\n" + " void f() { // This does something ..\n" + " } // awesome..\n" + "\n" + " int a; // This is unrelated\n" + "};", + "class C{void f() { // This does something ..\n" + " } // awesome..\n" + " \n" + "int a; // This is unrelated\n" + "};"); - EXPECT_EQ("void f() { // This does something ..\n" - "}\n" - "int a; // This is unrelated", - format("void f() { // This does something ..\n" - " }\n" - "int a; // This is unrelated")); - EXPECT_EQ("class C {\n" - " void f() { // This does something ..\n" - " } // awesome..\n" - "\n" - " int a; // This is unrelated\n" - "};", - format("class C{void f() { // This does something ..\n" - " } // awesome..\n" - " \n" - "int a; // This is unrelated\n" - "};")); - - EXPECT_EQ("int i; // single line trailing comment", - format("int i;\\\n// single line trailing comment")); + verifyFormat("int i; // single line trailing comment", + "int i;\\\n// single line trailing comment"); verifyGoogleFormat("int a; // Trailing comment."); @@ -210,99 +212,99 @@ TEST_F(FormatTestComments, UnderstandsSingleLineComments) { verifyGoogleFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); - EXPECT_EQ("D(a, {\n" - " // test\n" - " int a;\n" - "});", - format("D(a, {\n" - "// test\n" - "int a;\n" - "});")); - - EXPECT_EQ("lineWith(); // comment\n" - "// at start\n" - "otherLine();", - format("lineWith(); // comment\n" - "// at start\n" - "otherLine();")); - EXPECT_EQ("lineWith(); // comment\n" - "/*\n" - " * at start */\n" - "otherLine();", - format("lineWith(); // comment\n" - "/*\n" - " * at start */\n" - "otherLine();")); - EXPECT_EQ("lineWith(); // comment\n" - " // at start\n" - "otherLine();", - format("lineWith(); // comment\n" - " // at start\n" - "otherLine();")); - - EXPECT_EQ("lineWith(); // comment\n" - "// at start\n" - "otherLine(); // comment", - format("lineWith(); // comment\n" - "// at start\n" - "otherLine(); // comment")); - EXPECT_EQ("lineWith();\n" - "// at start\n" - "otherLine(); // comment", - format("lineWith();\n" - " // at start\n" - "otherLine(); // comment")); - EXPECT_EQ("// first\n" - "// at start\n" - "otherLine(); // comment", - format("// first\n" - " // at start\n" - "otherLine(); // comment")); - EXPECT_EQ("f();\n" - "// first\n" - "// at start\n" - "otherLine(); // comment", - format("f();\n" - "// first\n" - " // at start\n" - "otherLine(); // comment")); + verifyFormat("D(a, {\n" + " // test\n" + " int a;\n" + "});", + "D(a, {\n" + "// test\n" + "int a;\n" + "});"); + + verifyFormat("lineWith(); // comment\n" + "// at start\n" + "otherLine();", + "lineWith(); // comment\n" + "// at start\n" + "otherLine();"); + verifyFormat("lineWith(); // comment\n" + "/*\n" + " * at start */\n" + "otherLine();", + "lineWith(); // comment\n" + "/*\n" + " * at start */\n" + "otherLine();"); + verifyFormat("lineWith(); // comment\n" + " // at start\n" + "otherLine();", + "lineWith(); // comment\n" + " // at start\n" + "otherLine();"); + + verifyFormat("lineWith(); // comment\n" + "// at start\n" + "otherLine(); // comment", + "lineWith(); // comment\n" + "// at start\n" + "otherLine(); // comment"); + verifyFormat("lineWith();\n" + "// at start\n" + "otherLine(); // comment", + "lineWith();\n" + " // at start\n" + "otherLine(); // comment"); + verifyFormat("// first\n" + "// at start\n" + "otherLine(); // comment", + "// first\n" + " // at start\n" + "otherLine(); // comment"); + verifyFormat("f();\n" + "// first\n" + "// at start\n" + "otherLine(); // comment", + "f();\n" + "// first\n" + " // at start\n" + "otherLine(); // comment"); verifyFormat("f(); // comment\n" "// first\n" "// at start\n" "otherLine();"); - EXPECT_EQ("f(); // comment\n" - "// first\n" - "// at start\n" - "otherLine();", - format("f(); // comment\n" - "// first\n" - " // at start\n" - "otherLine();")); - EXPECT_EQ("f(); // comment\n" - " // first\n" - "// at start\n" - "otherLine();", - format("f(); // comment\n" - " // first\n" - "// at start\n" - "otherLine();")); - EXPECT_EQ("void f() {\n" - " lineWith(); // comment\n" - " // at start\n" - "}", - format("void f() {\n" - " lineWith(); // comment\n" - " // at start\n" - "}")); - EXPECT_EQ("int xy; // a\n" - "int z; // b", - format("int xy; // a\n" - "int z; //b")); - EXPECT_EQ("int xy; // a\n" - "int z; // bb", - format("int xy; // a\n" - "int z; //bb", - getLLVMStyleWithColumns(12))); + verifyFormat("f(); // comment\n" + "// first\n" + "// at start\n" + "otherLine();", + "f(); // comment\n" + "// first\n" + " // at start\n" + "otherLine();"); + verifyFormat("f(); // comment\n" + " // first\n" + "// at start\n" + "otherLine();", + "f(); // comment\n" + " // first\n" + "// at start\n" + "otherLine();"); + verifyFormat("void f() {\n" + " lineWith(); // comment\n" + " // at start\n" + "}", + "void f() {\n" + " lineWith(); // comment\n" + " // at start\n" + "}"); + verifyFormat("int xy; // a\n" + "int z; // b", + "int xy; // a\n" + "int z; //b"); + verifyFormat("int xy; // a\n" + "int z; // bb", + "int xy; // a\n" + "int z; //bb", + getLLVMStyleWithColumns(12)); verifyFormat("#define A \\\n" " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n" @@ -317,14 +319,14 @@ TEST_F(FormatTestComments, UnderstandsSingleLineComments) { verifyFormat("if ( // This is some comment\n" " x + 3) {\n" "}"); - EXPECT_EQ("if ( // This is some comment\n" - " // spanning two lines\n" - " x + 3) {\n" - "}", - format("if( // This is some comment\n" - " // spanning two lines\n" - " x + 3) {\n" - "}")); + verifyFormat("if ( // This is some comment\n" + " // spanning two lines\n" + " x + 3) {\n" + "}", + "if( // This is some comment\n" + " // spanning two lines\n" + " x + 3) {\n" + "}"); verifyNoCrash("/\\\n/"); verifyNoCrash("/\\\n* */"); @@ -333,35 +335,35 @@ TEST_F(FormatTestComments, UnderstandsSingleLineComments) { } TEST_F(FormatTestComments, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { - EXPECT_EQ("SomeFunction(a,\n" - " b, // comment\n" - " c);", - format("SomeFunction(a,\n" - " b, // comment\n" - " c);")); - EXPECT_EQ("SomeFunction(a, b,\n" - " // comment\n" - " c);", - format("SomeFunction(a,\n" - " b,\n" - " // comment\n" - " c);")); - EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n" - " c);", - format("SomeFunction(a, b, // comment (unclear relation)\n" - " c);")); - EXPECT_EQ("SomeFunction(a, // comment\n" - " b,\n" - " c); // comment", - format("SomeFunction(a, // comment\n" - " b,\n" - " c); // comment")); - EXPECT_EQ("aaaaaaaaaa(aaaa(aaaa,\n" - " aaaa), //\n" - " aaaa, bbbbb);", - format("aaaaaaaaaa(aaaa(aaaa,\n" - "aaaa), //\n" - "aaaa, bbbbb);")); + verifyFormat("SomeFunction(a,\n" + " b, // comment\n" + " c);", + "SomeFunction(a,\n" + " b, // comment\n" + " c);"); + verifyFormat("SomeFunction(a, b,\n" + " // comment\n" + " c);", + "SomeFunction(a,\n" + " b,\n" + " // comment\n" + " c);"); + verifyFormat("SomeFunction(a, b, // comment (unclear relation)\n" + " c);", + "SomeFunction(a, b, // comment (unclear relation)\n" + " c);"); + verifyFormat("SomeFunction(a, // comment\n" + " b,\n" + " c); // comment", + "SomeFunction(a, // comment\n" + " b,\n" + " c); // comment"); + verifyFormat("aaaaaaaaaa(aaaa(aaaa,\n" + " aaaa), //\n" + " aaaa, bbbbb);", + "aaaaaaaaaa(aaaa(aaaa,\n" + "aaaa), //\n" + "aaaa, bbbbb);"); FormatStyle BreakAlways = getLLVMStyle(); BreakAlways.BinPackParameters = FormatStyle::BPPS_AlwaysOnePerLine; @@ -378,12 +380,12 @@ TEST_F(FormatTestComments, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { } TEST_F(FormatTestComments, RemovesTrailingWhitespaceOfComments) { - EXPECT_EQ("// comment", format("// comment ")); - EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", - format("int aaaaaaa, bbbbbbb; // comment ", - getLLVMStyleWithColumns(33))); - EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f ")); - EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f ")); + verifyFormat("// comment", "// comment "); + verifyFormat("int aaaaaaa, bbbbbbb; // comment", + "int aaaaaaa, bbbbbbb; // comment ", + getLLVMStyleWithColumns(33)); + verifyFormat("// comment\\\n", "// comment\\\n \t \v \f "); + verifyFormat("// comment \\\n", "// comment \\\n \t \v \f "); } TEST_F(FormatTestComments, UnderstandsBlockComments) { @@ -393,16 +395,15 @@ TEST_F(FormatTestComments, UnderstandsBlockComments) { " /*qq_=*/move(q), [this, b](bar<void(uint32_t)> b) {},\n" " c);", getLLVMStyleWithColumns(60)); - EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" - " bbbbbbbbbbbbbbbbbbbbbbbbb);", - format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" - "/* Trailing comment for aa... */\n" - " bbbbbbbbbbbbbbbbbbbbbbbbb);")); - EXPECT_EQ( - "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", - format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" - "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); + verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" + " bbbbbbbbbbbbbbbbbbbbbbbbb);", + "f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" + "/* Trailing comment for aa... */\n" + " bbbbbbbbbbbbbbbbbbbbbbbbb);"); + verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", + "f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" + "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"); verifyFormat( "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" @@ -445,77 +446,77 @@ TEST_F(FormatTestComments, UnderstandsBlockComments) { } TEST_F(FormatTestComments, AlignsBlockComments) { - EXPECT_EQ("/*\n" - " * Really multi-line\n" - " * comment.\n" - " */\n" - "void f() {}", - format(" /*\n" - " * Really multi-line\n" - " * comment.\n" - " */\n" - " void f() {}")); - EXPECT_EQ("class C {\n" - " /*\n" - " * Another multi-line\n" - " * comment.\n" - " */\n" - " void f() {}\n" - "};", - format("class C {\n" - "/*\n" - " * Another multi-line\n" - " * comment.\n" - " */\n" - "void f() {}\n" - "};")); - EXPECT_EQ("/*\n" - " 1. This is a comment with non-trivial formatting.\n" - " 1.1. We have to indent/outdent all lines equally\n" - " 1.1.1. to keep the formatting.\n" - " */", - format(" /*\n" - " 1. This is a comment with non-trivial formatting.\n" - " 1.1. We have to indent/outdent all lines equally\n" - " 1.1.1. to keep the formatting.\n" - " */")); - EXPECT_EQ("/*\n" - "Don't try to outdent if there's not enough indentation.\n" - "*/", - format(" /*\n" - " Don't try to outdent if there's not enough indentation.\n" - " */")); - - EXPECT_EQ("int i; /* Comment with empty...\n" - " *\n" - " * line. */", - format("int i; /* Comment with empty...\n" - " *\n" - " * line. */")); - EXPECT_EQ("int foobar = 0; /* comment */\n" - "int bar = 0; /* multiline\n" - " comment 1 */\n" - "int baz = 0; /* multiline\n" - " comment 2 */\n" - "int bzz = 0; /* multiline\n" - " comment 3 */", - format("int foobar = 0; /* comment */\n" - "int bar = 0; /* multiline\n" - " comment 1 */\n" - "int baz = 0; /* multiline\n" - " comment 2 */\n" - "int bzz = 0; /* multiline\n" - " comment 3 */")); - EXPECT_EQ("int foobar = 0; /* comment */\n" - "int bar = 0; /* multiline\n" - " comment */\n" - "int baz = 0; /* multiline\n" - "comment */", - format("int foobar = 0; /* comment */\n" - "int bar = 0; /* multiline\n" - "comment */\n" - "int baz = 0; /* multiline\n" - "comment */")); + verifyFormat("/*\n" + " * Really multi-line\n" + " * comment.\n" + " */\n" + "void f() {}", + " /*\n" + " * Really multi-line\n" + " * comment.\n" + " */\n" + " void f() {}"); + verifyFormat("class C {\n" + " /*\n" + " * Another multi-line\n" + " * comment.\n" + " */\n" + " void f() {}\n" + "};", + "class C {\n" + "/*\n" + " * Another multi-line\n" + " * comment.\n" + " */\n" + "void f() {}\n" + "};"); + verifyFormat("/*\n" + " 1. This is a comment with non-trivial formatting.\n" + " 1.1. We have to indent/outdent all lines equally\n" + " 1.1.1. to keep the formatting.\n" + " */", + " /*\n" + " 1. This is a comment with non-trivial formatting.\n" + " 1.1. We have to indent/outdent all lines equally\n" + " 1.1.1. to keep the formatting.\n" + " */"); + verifyFormat("/*\n" + "Don't try to outdent if there's not enough indentation.\n" + "*/", + " /*\n" + " Don't try to outdent if there's not enough indentation.\n" + " */"); + + verifyFormat("int i; /* Comment with empty...\n" + " *\n" + " * line. */", + "int i; /* Comment with empty...\n" + " *\n" + " * line. */"); + verifyFormat("int foobar = 0; /* comment */\n" + "int bar = 0; /* multiline\n" + " comment 1 */\n" + "int baz = 0; /* multiline\n" + " comment 2 */\n" + "int bzz = 0; /* multiline\n" + " comment 3 */", + "int foobar = 0; /* comment */\n" + "int bar = 0; /* multiline\n" + " comment 1 */\n" + "int baz = 0; /* multiline\n" + " comment 2 */\n" + "int bzz = 0; /* multiline\n" + " comment 3 */"); + verifyFormat("int foobar = 0; /* comment */\n" + "int bar = 0; /* multiline\n" + " comment */\n" + "int baz = 0; /* multiline\n" + "comment */", + "int foobar = 0; /* comment */\n" + "int bar = 0; /* multiline\n" + "comment */\n" + "int baz = 0; /* multiline\n" + "comment */"); } TEST_F(FormatTestComments, CommentReflowingCanBeTurnedOff) { @@ -553,11 +554,11 @@ TEST_F(FormatTestComments, CommentReflowingCanApplyOnlyToIndents) { } TEST_F(FormatTestComments, CorrectlyHandlesLengthOfBlockComments) { - EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", - format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */")); - EXPECT_EQ( + verifyFormat("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */", + "double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"); + verifyFormat( "void ffffffffffff(\n" " int aaaaaaaa, int bbbbbbbb,\n" " int cccccccccccc) { /*\n" @@ -567,150 +568,146 @@ TEST_F(FormatTestComments, CorrectlyHandlesLengthOfBlockComments) { " bbbbbbbbbb\n" " */\n" "}", - format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" - "{ /*\n" - " aaaaaaaaaa aaaaaaaaaaaaa\n" - " bbbbbbbbbbbbbb bbbbbbbbbb\n" - " */\n" - "}", - getLLVMStyleWithColumns(40))); + "void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n" + "{ /*\n" + " aaaaaaaaaa aaaaaaaaaaaaa\n" + " bbbbbbbbbbbbbb bbbbbbbbbb\n" + " */\n" + "}", + getLLVMStyleWithColumns(40)); } TEST_F(FormatTestComments, DontBreakNonTrailingBlockComments) { - EXPECT_EQ("void ffffffffff(\n" - " int aaaaa /* test */);", - format("void ffffffffff(int aaaaa /* test */);", - getLLVMStyleWithColumns(35))); + verifyFormat("void ffffffffff(\n" + " int aaaaa /* test */);", + "void ffffffffff(int aaaaa /* test */);", + getLLVMStyleWithColumns(35)); } TEST_F(FormatTestComments, SplitsLongCxxComments) { - EXPECT_EQ("// A comment that\n" - "// doesn't fit on\n" - "// one line", - format("// A comment that doesn't fit on one line", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("/// A comment that\n" - "/// doesn't fit on\n" - "/// one line", - format("/// A comment that doesn't fit on one line", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("//! A comment that\n" - "//! doesn't fit on\n" - "//! one line", - format("//! A comment that doesn't fit on one line", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("// a b c d\n" - "// e f g\n" - "// h i j k", - format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); - EXPECT_EQ( - "// a b c d\n" - "// e f g\n" - "// h i j k", - format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10))); - EXPECT_EQ("if (true) // A comment that\n" - " // doesn't fit on\n" - " // one line", - format("if (true) // A comment that doesn't fit on one line ", - getLLVMStyleWithColumns(30))); - verifyNoChange("// Don't_touch_leading_whitespace", - getLLVMStyleWithColumns(20)); - EXPECT_EQ("// Add leading\n" - "// whitespace", - format("//Add leading whitespace", getLLVMStyleWithColumns(20))); - EXPECT_EQ("/// Add leading\n" - "/// whitespace", - format("///Add leading whitespace", getLLVMStyleWithColumns(20))); - EXPECT_EQ("//! Add leading\n" - "//! whitespace", - format("//!Add leading whitespace", getLLVMStyleWithColumns(20))); - EXPECT_EQ("// whitespace", format("//whitespace")); - EXPECT_EQ("// Even if it makes the line exceed the column\n" - "// limit", - format("//Even if it makes the line exceed the column limit", - getLLVMStyleWithColumns(51))); + const auto Style10 = getLLVMStyleWithColumns(10); + const auto Style20 = getLLVMStyleWithColumns(20); + const auto Style22 = getLLVMStyleWithColumns(22); + const auto Style30 = getLLVMStyleWithColumns(30); + + verifyFormat("// A comment that\n" + "// doesn't fit on\n" + "// one line", + "// A comment that doesn't fit on one line", Style20); + verifyFormat("/// A comment that\n" + "/// doesn't fit on\n" + "/// one line", + "/// A comment that doesn't fit on one line", Style20); + verifyFormat("//! A comment that\n" + "//! doesn't fit on\n" + "//! one line", + "//! A comment that doesn't fit on one line", Style20); + verifyFormat("// a b c d\n" + "// e f g\n" + "// h i j k", + "// a b c d e f g h i j k", Style10); + verifyFormat("// a b c d\n" + "// e f g\n" + "// h i j k", + "\\\n// a b c d e f g h i j k", Style10); + verifyFormat("if (true) // A comment that\n" + " // doesn't fit on\n" + " // one line", + "if (true) // A comment that doesn't fit on one line ", + Style30); + verifyNoChange("// Don't_touch_leading_whitespace", Style20); + verifyFormat("// Add leading\n" + "// whitespace", + "//Add leading whitespace", Style20); + verifyFormat("/// Add leading\n" + "/// whitespace", + "///Add leading whitespace", Style20); + verifyFormat("//! Add leading\n" + "//! whitespace", + "//!Add leading whitespace", Style20); + verifyFormat("// whitespace", "//whitespace"); + verifyFormat("// Even if it makes the line exceed the column\n" + "// limit", + "//Even if it makes the line exceed the column limit", + getLLVMStyleWithColumns(51)); verifyFormat("//--But not here"); - EXPECT_EQ("/// line 1\n" - "// add leading whitespace", - format("/// line 1\n" - "//add leading whitespace", - getLLVMStyleWithColumns(30))); - EXPECT_EQ("/// line 1\n" - "/// line 2\n" - "//! line 3\n" - "//! line 4\n" - "//! line 5\n" - "// line 6\n" - "// line 7", - format("///line 1\n" - "///line 2\n" - "//! line 3\n" - "//!line 4\n" - "//!line 5\n" - "// line 6\n" - "//line 7", - getLLVMStyleWithColumns(20))); - - EXPECT_EQ("// aa bb cc dd", - format("// aa bb cc dd ", - getLLVMStyleWithColumns(15))); - - EXPECT_EQ("// A comment before\n" - "// a macro\n" - "// definition\n" - "#define a b", - format("// A comment before a macro definition\n" - "#define a b", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("void ffffff(\n" - " int aaaaaaaaa, // wwww\n" - " int bbbbbbbbbb, // xxxxxxx\n" - " // yyyyyyyyyy\n" - " int c, int d, int e) {}", - format("void ffffff(\n" - " int aaaaaaaaa, // wwww\n" - " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" - " int c, int d, int e) {}", - getLLVMStyleWithColumns(40))); - verifyFormat("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", - getLLVMStyleWithColumns(20)); - EXPECT_EQ( - "#define XXX // a b c d\n" - " // e f g h", - format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22))); - EXPECT_EQ( - "#define XXX // q w e r\n" - " // t y u i", - format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22))); - EXPECT_EQ("{\n" - " //\n" - " //\\\n" - " // long 1 2 3 4 5\n" - "}", - format("{\n" - " //\n" - " //\\\n" - " // long 1 2 3 4 5\n" - "}", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("{\n" - " //\n" - " //\\\n" - " // long 1 2 3 4 5\n" - " // 6\n" - "}", - format("{\n" - " //\n" - " //\\\n" - " // long 1 2 3 4 5 6\n" - "}", - getLLVMStyleWithColumns(20))); - - EXPECT_EQ("//: A comment that\n" - "//: doesn't fit on\n" - "//: one line", - format("//: A comment that doesn't fit on one line", - getLLVMStyleWithColumns(20))); + verifyFormat("/// line 1\n" + "// add leading whitespace", + "/// line 1\n" + "//add leading whitespace", + Style30); + verifyFormat("/// line 1\n" + "/// line 2\n" + "//! line 3\n" + "//! line 4\n" + "//! line 5\n" + "// line 6\n" + "// line 7", + "///line 1\n" + "///line 2\n" + "//! line 3\n" + "//!line 4\n" + "//!line 5\n" + "// line 6\n" + "//line 7", + Style20); + + verifyFormat("// aa bb cc dd", + "// aa bb cc dd ", + getLLVMStyleWithColumns(15)); + + verifyFormat("// A comment before\n" + "// a macro\n" + "// definition\n" + "#define a b", + "// A comment before a macro definition\n" + "#define a b", + Style20); + verifyFormat("void ffffff(\n" + " int aaaaaaaaa, // wwww\n" + " int bbbbbbbbbb, // xxxxxxx\n" + " // yyyyyyyyyy\n" + " int c, int d, int e) {}", + "void ffffff(\n" + " int aaaaaaaaa, // wwww\n" + " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" + " int c, int d, int e) {}", + getLLVMStyleWithColumns(40)); + verifyFormat("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", Style20); + verifyFormat("#define XXX // a b c d\n" + " // e f g h", + "#define XXX // a b c d e f g h", Style22); + verifyFormat("#define XXX // q w e r\n" + " // t y u i", + "#define XXX //q w e r t y u i", Style22); + verifyFormat("{\n" + " //\n" + " //\\\n" + " // long 1 2 3 4 5\n" + "}", + "{\n" + " //\n" + " //\\\n" + " // long 1 2 3 4 5\n" + "}", + Style20); + verifyFormat("{\n" + " //\n" + " //\\\n" + " // long 1 2 3 4 5\n" + " // 6\n" + "}", + "{\n" + " //\n" + " //\\\n" + " // long 1 2 3 4 5 6\n" + "}", + Style20); + + verifyFormat("//: A comment that\n" + "//: doesn't fit on\n" + "//: one line", + "//: A comment that doesn't fit on one line", Style20); verifyFormat( "//\t\t\t\tofMap(message.velocity, 0, 127, 0, ofGetWidth()\n" @@ -719,34 +716,33 @@ TEST_F(FormatTestComments, SplitsLongCxxComments) { } TEST_F(FormatTestComments, PreservesHangingIndentInCxxComments) { - EXPECT_EQ("// A comment\n" - "// that doesn't\n" - "// fit on one\n" - "// line", - format("// A comment that doesn't fit on one line", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("/// A comment\n" - "/// that doesn't\n" - "/// fit on one\n" - "/// line", - format("/// A comment that doesn't fit on one line", - getLLVMStyleWithColumns(20))); + const auto Style20 = getLLVMStyleWithColumns(20); + verifyFormat("// A comment\n" + "// that doesn't\n" + "// fit on one\n" + "// line", + "// A comment that doesn't fit on one line", Style20); + verifyFormat("/// A comment\n" + "/// that doesn't\n" + "/// fit on one\n" + "/// line", + "/// A comment that doesn't fit on one line", Style20); } TEST_F(FormatTestComments, DontSplitLineCommentsWithEscapedNewlines) { - EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" - "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" - "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", - format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" - "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" - "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); - EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" - " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" - " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", - format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" - " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" - " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", - getLLVMStyleWithColumns(50))); + verifyFormat("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" + "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" + "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" + "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" + "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + verifyFormat("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" + " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" + " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", + "int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" + " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" + " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", + getLLVMStyleWithColumns(50)); verifyFormat("double\n" " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n" @@ -759,84 +755,83 @@ TEST_F(FormatTestComments, DontSplitLineCommentsWithEscapedNewlines) { TEST_F(FormatTestComments, DontIntroduceMultilineComments) { // Avoid introducing a multiline comment by breaking after `\`. + auto Style = getLLVMStyle(); for (int ColumnLimit = 15; ColumnLimit <= 17; ++ColumnLimit) { - EXPECT_EQ( - "// aaaaaaaaaa\n" - "// \\ bb", - format("// aaaaaaaaaa \\ bb", getLLVMStyleWithColumns(ColumnLimit))); - EXPECT_EQ( - "// aaaaaaaaa\n" - "// \\ bb", - format("// aaaaaaaaa \\ bb", getLLVMStyleWithColumns(ColumnLimit))); - EXPECT_EQ( - "// aaaaaaaaa\n" - "// \\ \\ bb", - format("// aaaaaaaaa \\ \\ bb", getLLVMStyleWithColumns(ColumnLimit))); + Style.ColumnLimit = ColumnLimit; + verifyFormat("// aaaaaaaaaa\n" + "// \\ bb", + "// aaaaaaaaaa \\ bb", Style); + verifyFormat("// aaaaaaaaa\n" + "// \\ bb", + "// aaaaaaaaa \\ bb", Style); + verifyFormat("// aaaaaaaaa\n" + "// \\ \\ bb", + "// aaaaaaaaa \\ \\ bb", Style); } } TEST_F(FormatTestComments, DontSplitLineCommentsWithPragmas) { FormatStyle Pragmas = getLLVMStyleWithColumns(30); Pragmas.CommentPragmas = "^ IWYU pragma:"; - EXPECT_EQ( - "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", - format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas)); - EXPECT_EQ( - "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", - format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas)); + verifyFormat("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", + "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas); + verifyFormat("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", + "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas); } TEST_F(FormatTestComments, PriorityOfCommentBreaking) { - EXPECT_EQ("if (xxx ==\n" - " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" - " zzz)\n" - " q();", - format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" - " zzz) q();", - getLLVMStyleWithColumns(40))); - EXPECT_EQ("if (xxxxxxxxxx ==\n" - " yyy && // aaaaaa bbbbbbbb cccc\n" - " zzz)\n" - " q();", - format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" - " zzz) q();", - getLLVMStyleWithColumns(40))); - EXPECT_EQ("if (xxxxxxxxxx &&\n" - " yyy || // aaaaaa bbbbbbbb cccc\n" - " zzz)\n" - " q();", - format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" - " zzz) q();", - getLLVMStyleWithColumns(40))); - EXPECT_EQ("fffffffff(\n" - " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" - " zzz);", - format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" - " zzz);", - getLLVMStyleWithColumns(40))); + const auto Style40 = getLLVMStyleWithColumns(40); + verifyFormat("if (xxx ==\n" + " yyy && // aaaaaaaaaaaa bbbbbbbbb\n" + " zzz)\n" + " q();", + "if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n" + " zzz) q();", + Style40); + verifyFormat("if (xxxxxxxxxx ==\n" + " yyy && // aaaaaa bbbbbbbb cccc\n" + " zzz)\n" + " q();", + "if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n" + " zzz) q();", + Style40); + verifyFormat("if (xxxxxxxxxx &&\n" + " yyy || // aaaaaa bbbbbbbb cccc\n" + " zzz)\n" + " q();", + "if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n" + " zzz) q();", + Style40); + verifyFormat("fffffffff(\n" + " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" + " zzz);", + "fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n" + " zzz);", + Style40); } TEST_F(FormatTestComments, MultiLineCommentsInDefines) { - EXPECT_EQ("#define A(x) /* \\\n" - " a comment \\\n" - " inside */ \\\n" - " f();", - format("#define A(x) /* \\\n" - " a comment \\\n" - " inside */ \\\n" - " f();", - getLLVMStyleWithColumns(17))); - EXPECT_EQ("#define A( \\\n" - " x) /* \\\n" - " a comment \\\n" - " inside */ \\\n" - " f();", - format("#define A( \\\n" - " x) /* \\\n" - " a comment \\\n" - " inside */ \\\n" - " f();", - getLLVMStyleWithColumns(17))); + const auto Style17 = getLLVMStyleWithColumns(17); + verifyFormat("#define A(x) /* \\\n" + " a comment \\\n" + " inside */ \\\n" + " f();", + "#define A(x) /* \\\n" + " a comment \\\n" + " inside */ \\\n" + " f();", + Style17); + verifyFormat("#define A( \\\n" + " x) /* \\\n" + " a comment \\\n" + " inside */ \\\n" + " f();", + "#define A( \\\n" + " x) /* \\\n" + " a comment \\\n" + " inside */ \\\n" + " f();", + Style17); } TEST_F(FormatTestComments, LineCommentsInMacrosDoNotGetEscapedNewlines) { @@ -859,285 +854,285 @@ TEST_F(FormatTestComments, LineCommentsInMacrosDoNotGetEscapedNewlines) { } TEST_F(FormatTestComments, ParsesCommentsAdjacentToPPDirectives) { - EXPECT_EQ("namespace {}\n// Test\n#define A", - format("namespace {}\n // Test\n#define A")); - EXPECT_EQ("namespace {}\n/* Test */\n#define A", - format("namespace {}\n /* Test */\n#define A")); - EXPECT_EQ("namespace {}\n/* Test */ #define A", - format("namespace {}\n /* Test */ #define A")); + verifyFormat("namespace {}\n// Test\n#define A", + "namespace {}\n // Test\n#define A"); + verifyFormat("namespace {}\n/* Test */\n#define A", + "namespace {}\n /* Test */\n#define A"); + verifyFormat("namespace {}\n/* Test */ #define A", + "namespace {}\n /* Test */ #define A"); } TEST_F(FormatTestComments, KeepsLevelOfCommentBeforePPDirective) { // Keep the current level if the comment was originally not aligned with // the preprocessor directive. - EXPECT_EQ("void f() {\n" - " int i;\n" - " /* comment */\n" - "#ifdef A\n" - " int j;\n" - "}", - format("void f() {\n" - " int i;\n" - " /* comment */\n" - "#ifdef A\n" - " int j;\n" - "}")); - - EXPECT_EQ("void f() {\n" - " int i;\n" - " /* comment */\n" - "\n" - "#ifdef A\n" - " int j;\n" - "}", - format("void f() {\n" - " int i;\n" - " /* comment */\n" - "\n" - "#ifdef A\n" - " int j;\n" - "}")); - - EXPECT_EQ("int f(int i) {\n" - " if (true) {\n" - " ++i;\n" - " }\n" - " // comment\n" - "#ifdef A\n" - " int j;\n" - "#endif\n" - "}", - format("int f(int i) {\n" - " if (true) {\n" - " ++i;\n" - " }\n" - " // comment\n" - "#ifdef A\n" - "int j;\n" - "#endif\n" - "}")); - - EXPECT_EQ("int f(int i) {\n" - " if (true) {\n" - " i++;\n" - " } else {\n" - " // comment in else\n" - "#ifdef A\n" - " j++;\n" - "#endif\n" - " }\n" - "}", - format("int f(int i) {\n" - " if (true) {\n" - " i++;\n" - " } else {\n" - " // comment in else\n" - "#ifdef A\n" - " j++;\n" - "#endif\n" - " }\n" - "}")); - - EXPECT_EQ("int f(int i) {\n" - " if (true) {\n" - " i++;\n" - " } else {\n" - " /* comment in else */\n" - "#ifdef A\n" - " j++;\n" - "#endif\n" - " }\n" - "}", - format("int f(int i) {\n" - " if (true) {\n" - " i++;\n" - " } else {\n" - " /* comment in else */\n" - "#ifdef A\n" - " j++;\n" - "#endif\n" - " }\n" - "}")); + verifyFormat("void f() {\n" + " int i;\n" + " /* comment */\n" + "#ifdef A\n" + " int j;\n" + "}", + "void f() {\n" + " int i;\n" + " /* comment */\n" + "#ifdef A\n" + " int j;\n" + "}"); + + verifyFormat("void f() {\n" + " int i;\n" + " /* comment */\n" + "\n" + "#ifdef A\n" + " int j;\n" + "}", + "void f() {\n" + " int i;\n" + " /* comment */\n" + "\n" + "#ifdef A\n" + " int j;\n" + "}"); + + verifyFormat("int f(int i) {\n" + " if (true) {\n" + " ++i;\n" + " }\n" + " // comment\n" + "#ifdef A\n" + " int j;\n" + "#endif\n" + "}", + "int f(int i) {\n" + " if (true) {\n" + " ++i;\n" + " }\n" + " // comment\n" + "#ifdef A\n" + "int j;\n" + "#endif\n" + "}"); + + verifyFormat("int f(int i) {\n" + " if (true) {\n" + " i++;\n" + " } else {\n" + " // comment in else\n" + "#ifdef A\n" + " j++;\n" + "#endif\n" + " }\n" + "}", + "int f(int i) {\n" + " if (true) {\n" + " i++;\n" + " } else {\n" + " // comment in else\n" + "#ifdef A\n" + " j++;\n" + "#endif\n" + " }\n" + "}"); + + verifyFormat("int f(int i) {\n" + " if (true) {\n" + " i++;\n" + " } else {\n" + " /* comment in else */\n" + "#ifdef A\n" + " j++;\n" + "#endif\n" + " }\n" + "}", + "int f(int i) {\n" + " if (true) {\n" + " i++;\n" + " } else {\n" + " /* comment in else */\n" + "#ifdef A\n" + " j++;\n" + "#endif\n" + " }\n" + "}"); // Keep the current level if there is an empty line between the comment and // the preprocessor directive. - EXPECT_EQ("void f() {\n" - " int i;\n" - " /* comment */\n" - "\n" - "#ifdef A\n" - " int j;\n" - "}", - format("void f() {\n" - " int i;\n" - "/* comment */\n" - "\n" - "#ifdef A\n" - " int j;\n" - "}")); - - EXPECT_EQ("void f() {\n" - " int i;\n" - " return i;\n" - "}\n" - "// comment\n" - "\n" - "#ifdef A\n" - "int i;\n" - "#endif // A", - format("void f() {\n" - " int i;\n" - " return i;\n" - "}\n" - "// comment\n" - "\n" - "#ifdef A\n" - "int i;\n" - "#endif // A")); - - EXPECT_EQ("int f(int i) {\n" - " if (true) {\n" - " ++i;\n" - " }\n" - " // comment\n" - "\n" - "#ifdef A\n" - " int j;\n" - "#endif\n" - "}", - format("int f(int i) {\n" - " if (true) {\n" - " ++i;\n" - " }\n" - " // comment\n" - "\n" - "#ifdef A\n" - " int j;\n" - "#endif\n" - "}")); - - EXPECT_EQ("int f(int i) {\n" - " if (true) {\n" - " i++;\n" - " } else {\n" - " // comment in else\n" - "\n" - "#ifdef A\n" - " j++;\n" - "#endif\n" - " }\n" - "}", - format("int f(int i) {\n" - " if (true) {\n" - " i++;\n" - " } else {\n" - "// comment in else\n" - "\n" - "#ifdef A\n" - " j++;\n" - "#endif\n" - " }\n" - "}")); - - EXPECT_EQ("int f(int i) {\n" - " if (true) {\n" - " i++;\n" - " } else {\n" - " /* comment in else */\n" - "\n" - "#ifdef A\n" - " j++;\n" - "#endif\n" - " }\n" - "}", - format("int f(int i) {\n" - " if (true) {\n" - " i++;\n" - " } else {\n" - "/* comment in else */\n" - "\n" - "#ifdef A\n" - " j++;\n" - "#endif\n" - " }\n" - "}")); + verifyFormat("void f() {\n" + " int i;\n" + " /* comment */\n" + "\n" + "#ifdef A\n" + " int j;\n" + "}", + "void f() {\n" + " int i;\n" + "/* comment */\n" + "\n" + "#ifdef A\n" + " int j;\n" + "}"); + + verifyFormat("void f() {\n" + " int i;\n" + " return i;\n" + "}\n" + "// comment\n" + "\n" + "#ifdef A\n" + "int i;\n" + "#endif // A", + "void f() {\n" + " int i;\n" + " return i;\n" + "}\n" + "// comment\n" + "\n" + "#ifdef A\n" + "int i;\n" + "#endif // A"); + + verifyFormat("int f(int i) {\n" + " if (true) {\n" + " ++i;\n" + " }\n" + " // comment\n" + "\n" + "#ifdef A\n" + " int j;\n" + "#endif\n" + "}", + "int f(int i) {\n" + " if (true) {\n" + " ++i;\n" + " }\n" + " // comment\n" + "\n" + "#ifdef A\n" + " int j;\n" + "#endif\n" + "}"); + + verifyFormat("int f(int i) {\n" + " if (true) {\n" + " i++;\n" + " } else {\n" + " // comment in else\n" + "\n" + "#ifdef A\n" + " j++;\n" + "#endif\n" + " }\n" + "}", + "int f(int i) {\n" + " if (true) {\n" + " i++;\n" + " } else {\n" + "// comment in else\n" + "\n" + "#ifdef A\n" + " j++;\n" + "#endif\n" + " }\n" + "}"); + + verifyFormat("int f(int i) {\n" + " if (true) {\n" + " i++;\n" + " } else {\n" + " /* comment in else */\n" + "\n" + "#ifdef A\n" + " j++;\n" + "#endif\n" + " }\n" + "}", + "int f(int i) {\n" + " if (true) {\n" + " i++;\n" + " } else {\n" + "/* comment in else */\n" + "\n" + "#ifdef A\n" + " j++;\n" + "#endif\n" + " }\n" + "}"); // Align with the preprocessor directive if the comment was originally aligned // with the preprocessor directive and there is no newline between the comment // and the preprocessor directive. - EXPECT_EQ("void f() {\n" - " int i;\n" - "/* comment */\n" - "#ifdef A\n" - " int j;\n" - "}", - format("void f() {\n" - " int i;\n" - "/* comment */\n" - "#ifdef A\n" - " int j;\n" - "}")); - - EXPECT_EQ("int f(int i) {\n" - " if (true) {\n" - " ++i;\n" - " }\n" - "// comment\n" - "#ifdef A\n" - " int j;\n" - "#endif\n" - "}", - format("int f(int i) {\n" - " if (true) {\n" - " ++i;\n" - " }\n" - "// comment\n" - "#ifdef A\n" - " int j;\n" - "#endif\n" - "}")); - - EXPECT_EQ("int f(int i) {\n" - " if (true) {\n" - " i++;\n" - " } else {\n" - "// comment in else\n" - "#ifdef A\n" - " j++;\n" - "#endif\n" - " }\n" - "}", - format("int f(int i) {\n" - " if (true) {\n" - " i++;\n" - " } else {\n" - " // comment in else\n" - " #ifdef A\n" - " j++;\n" - "#endif\n" - " }\n" - "}")); - - EXPECT_EQ("int f(int i) {\n" - " if (true) {\n" - " i++;\n" - " } else {\n" - "/* comment in else */\n" - "#ifdef A\n" - " j++;\n" - "#endif\n" - " }\n" - "}", - format("int f(int i) {\n" - " if (true) {\n" - " i++;\n" - " } else {\n" - " /* comment in else */\n" - " #ifdef A\n" - " j++;\n" - "#endif\n" - " }\n" - "}")); + verifyFormat("void f() {\n" + " int i;\n" + "/* comment */\n" + "#ifdef A\n" + " int j;\n" + "}", + "void f() {\n" + " int i;\n" + "/* comment */\n" + "#ifdef A\n" + " int j;\n" + "}"); + + verifyFormat("int f(int i) {\n" + " if (true) {\n" + " ++i;\n" + " }\n" + "// comment\n" + "#ifdef A\n" + " int j;\n" + "#endif\n" + "}", + "int f(int i) {\n" + " if (true) {\n" + " ++i;\n" + " }\n" + "// comment\n" + "#ifdef A\n" + " int j;\n" + "#endif\n" + "}"); + + verifyFormat("int f(int i) {\n" + " if (true) {\n" + " i++;\n" + " } else {\n" + "// comment in else\n" + "#ifdef A\n" + " j++;\n" + "#endif\n" + " }\n" + "}", + "int f(int i) {\n" + " if (true) {\n" + " i++;\n" + " } else {\n" + " // comment in else\n" + " #ifdef A\n" + " j++;\n" + "#endif\n" + " }\n" + "}"); + + verifyFormat("int f(int i) {\n" + " if (true) {\n" + " i++;\n" + " } else {\n" + "/* comment in else */\n" + "#ifdef A\n" + " j++;\n" + "#endif\n" + " }\n" + "}", + "int f(int i) {\n" + " if (true) {\n" + " i++;\n" + " } else {\n" + " /* comment in else */\n" + " #ifdef A\n" + " j++;\n" + "#endif\n" + " }\n" + "}"); constexpr StringRef Code("void func() {\n" " // clang-format off\n" @@ -1189,245 +1184,242 @@ TEST_F(FormatTestComments, CommentsBetweenUnbracedBodyAndPPDirective) { } TEST_F(FormatTestComments, SplitsLongLinesInComments) { + const auto Style10 = getLLVMStyleWithColumns(10); + const auto Style15 = getLLVMStyleWithColumns(15); + const auto Style20 = getLLVMStyleWithColumns(20); + // FIXME: Do we need to fix up the " */" at the end? // It doesn't look like any of our current logic triggers this. - EXPECT_EQ("/* This is a long\n" - " * comment that\n" - " * doesn't fit on\n" - " * one line. */", - format("/* " - "This is a long " - "comment that " - "doesn't " - "fit on one line. */", - getLLVMStyleWithColumns(20))); - EXPECT_EQ( - "/* a b c d\n" - " * e f g\n" - " * h i j k\n" - " */", - format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); - EXPECT_EQ( - "/* a b c d\n" - " * e f g\n" - " * h i j k\n" - " */", - format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10))); - EXPECT_EQ("/*\n" - "This is a long\n" - "comment that doesn't\n" - "fit on one line.\n" - "*/", - format("/*\n" - "This is a long " - "comment that doesn't " - "fit on one line. \n" - "*/", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("/*\n" - " * This is a long\n" - " * comment that\n" - " * doesn't fit on\n" - " * one line.\n" - " */", - format("/* \n" - " * This is a long " - " comment that " - " doesn't fit on " - " one line. \n" - " */", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("/*\n" - " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" - " * so_it_should_be_broken\n" - " * wherever_a_space_occurs\n" - " */", - format("/*\n" - " * This_is_a_comment_with_words_that_dont_fit_on_one_line " - " so_it_should_be_broken " - " wherever_a_space_occurs \n" - " */", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("/*\n" - " * This_comment_can_not_be_broken_into_lines\n" - " */", - format("/*\n" - " * This_comment_can_not_be_broken_into_lines\n" - " */", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("{\n" - " /*\n" - " This is another\n" - " long comment that\n" - " doesn't fit on one\n" - " line 1234567890\n" - " */\n" - "}", - format("{\n" - "/*\n" - "This is another " - " long comment that " - " doesn't fit on one" - " line 1234567890\n" - "*/\n" - "}", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("{\n" - " /*\n" - " * This i s\n" - " * another comment\n" - " * t hat doesn' t\n" - " * fit on one l i\n" - " * n e\n" - " */\n" - "}", - format("{\n" - "/*\n" - " * This i s" - " another comment" - " t hat doesn' t" - " fit on one l i" - " n e\n" - " */\n" - "}", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("/*\n" - " * This is a long\n" - " * comment that\n" - " * doesn't fit on\n" - " * one line\n" - " */", - format(" /*\n" - " * This is a long comment that doesn't fit on one line\n" - " */", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("{\n" - " if (something) /* This is a\n" - " long\n" - " comment */\n" - " ;\n" - "}", - format("{\n" - " if (something) /* This is a long comment */\n" - " ;\n" - "}", - getLLVMStyleWithColumns(30))); - - EXPECT_EQ("/* A comment before\n" - " * a macro\n" - " * definition */\n" - "#define a b", - format("/* A comment before a macro definition */\n" - "#define a b", - getLLVMStyleWithColumns(20))); - - EXPECT_EQ("/* some comment\n" - " * a comment that\n" - " * we break another\n" - " * comment we have\n" - " * to break a left\n" - " * comment\n" - " */", - format(" /* some comment\n" - " * a comment that we break\n" - " * another comment we have to break\n" - "* a left comment\n" - " */", - getLLVMStyleWithColumns(20))); - - EXPECT_EQ("/**\n" - " * multiline block\n" - " * comment\n" - " *\n" - " */", - format("/**\n" - " * multiline block comment\n" - " *\n" - " */", - getLLVMStyleWithColumns(20))); + verifyFormat("/* This is a long\n" + " * comment that\n" + " * doesn't fit on\n" + " * one line. */", + "/* " + "This is a long " + "comment that " + "doesn't " + "fit on one line. */", + Style20); + verifyFormat("/* a b c d\n" + " * e f g\n" + " * h i j k\n" + " */", + "/* a b c d e f g h i j k */", Style10); + verifyFormat("/* a b c d\n" + " * e f g\n" + " * h i j k\n" + " */", + "\\\n/* a b c d e f g h i j k */", Style10); + verifyFormat("/*\n" + "This is a long\n" + "comment that doesn't\n" + "fit on one line.\n" + "*/", + "/*\n" + "This is a long " + "comment that doesn't " + "fit on one line. \n" + "*/", + Style20); + verifyFormat("/*\n" + " * This is a long\n" + " * comment that\n" + " * doesn't fit on\n" + " * one line.\n" + " */", + "/* \n" + " * This is a long " + " comment that " + " doesn't fit on " + " one line. \n" + " */", + Style20); + verifyFormat("/*\n" + " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" + " * so_it_should_be_broken\n" + " * wherever_a_space_occurs\n" + " */", + "/*\n" + " * This_is_a_comment_with_words_that_dont_fit_on_one_line " + " so_it_should_be_broken " + " wherever_a_space_occurs \n" + " */", + Style20); + verifyFormat("/*\n" + " * This_comment_can_not_be_broken_into_lines\n" + " */", + "/*\n" + " * This_comment_can_not_be_broken_into_lines\n" + " */", + Style20); + verifyFormat("{\n" + " /*\n" + " This is another\n" + " long comment that\n" + " doesn't fit on one\n" + " line 1234567890\n" + " */\n" + "}", + "{\n" + "/*\n" + "This is another " + " long comment that " + " doesn't fit on one" + " line 1234567890\n" + "*/\n" + "}", + Style20); + verifyFormat("{\n" + " /*\n" + " * This i s\n" + " * another comment\n" + " * t hat doesn' t\n" + " * fit on one l i\n" + " * n e\n" + " */\n" + "}", + "{\n" + "/*\n" + " * This i s" + " another comment" + " t hat doesn' t" + " fit on one l i" + " n e\n" + " */\n" + "}", + Style20); + verifyFormat("/*\n" + " * This is a long\n" + " * comment that\n" + " * doesn't fit on\n" + " * one line\n" + " */", + " /*\n" + " * This is a long comment that doesn't fit on one line\n" + " */", + Style20); + verifyFormat("{\n" + " if (something) /* This is a\n" + " long\n" + " comment */\n" + " ;\n" + "}", + "{\n" + " if (something) /* This is a long comment */\n" + " ;\n" + "}", + getLLVMStyleWithColumns(30)); + + verifyFormat("/* A comment before\n" + " * a macro\n" + " * definition */\n" + "#define a b", + "/* A comment before a macro definition */\n" + "#define a b", + Style20); + + verifyFormat("/* some comment\n" + " * a comment that\n" + " * we break another\n" + " * comment we have\n" + " * to break a left\n" + " * comment\n" + " */", + " /* some comment\n" + " * a comment that we break\n" + " * another comment we have to break\n" + "* a left comment\n" + " */", + Style20); + + verifyFormat("/**\n" + " * multiline block\n" + " * comment\n" + " *\n" + " */", + "/**\n" + " * multiline block comment\n" + " *\n" + " */", + Style20); // This reproduces a crashing bug where both adaptStartOfLine and // getCommentSplit were trying to wrap after the "/**". - verifyFormat("/** multilineblockcommentwithnowrapopportunity */", - getLLVMStyleWithColumns(20)); + verifyFormat("/** multilineblockcommentwithnowrapopportunity */", Style20); - EXPECT_EQ("/*\n" - "\n" - "\n" - " */", - format(" /* \n" - " \n" - " \n" - " */")); - - EXPECT_EQ("/* a a */", - format("/* a a */", getLLVMStyleWithColumns(15))); - EXPECT_EQ("/* a a bc */", - format("/* a a bc */", getLLVMStyleWithColumns(15))); - EXPECT_EQ("/* aaa aaa\n" - " * aaaaa */", - format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); - EXPECT_EQ("/* aaa aaa\n" - " * aaaaa */", - format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15))); + verifyFormat("/*\n" + "\n" + "\n" + " */", + " /* \n" + " \n" + " \n" + " */"); + + verifyFormat("/* a a */", "/* a a */", Style15); + verifyFormat("/* a a bc */", "/* a a bc */", Style15); + verifyFormat("/* aaa aaa\n" + " * aaaaa */", + "/* aaa aaa aaaaa */", Style15); + verifyFormat("/* aaa aaa\n" + " * aaaaa */", + "/* aaa aaa aaaaa */", Style15); } TEST_F(FormatTestComments, SplitsLongLinesInCommentsInPreprocessor) { - EXPECT_EQ("#define X \\\n" - " /* \\\n" - " Test \\\n" - " Macro comment \\\n" - " with a long \\\n" - " line \\\n" - " */ \\\n" - " A + B", - format("#define X \\\n" - " /*\n" - " Test\n" - " Macro comment with a long line\n" - " */ \\\n" - " A + B", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("#define X \\\n" - " /* Macro comment \\\n" - " with a long \\\n" - " line */ \\\n" - " A + B", - format("#define X \\\n" - " /* Macro comment with a long\n" - " line */ \\\n" - " A + B", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("#define X \\\n" - " /* Macro comment \\\n" - " * with a long \\\n" - " * line */ \\\n" - " A + B", - format("#define X \\\n" - " /* Macro comment with a long line */ \\\n" - " A + B", - getLLVMStyleWithColumns(20))); + const auto Style20 = getLLVMStyleWithColumns(20); + verifyFormat("#define X \\\n" + " /* \\\n" + " Test \\\n" + " Macro comment \\\n" + " with a long \\\n" + " line \\\n" + " */ \\\n" + " A + B", + "#define X \\\n" + " /*\n" + " Test\n" + " Macro comment with a long line\n" + " */ \\\n" + " A + B", + Style20); + verifyFormat("#define X \\\n" + " /* Macro comment \\\n" + " with a long \\\n" + " line */ \\\n" + " A + B", + "#define X \\\n" + " /* Macro comment with a long\n" + " line */ \\\n" + " A + B", + Style20); + verifyFormat("#define X \\\n" + " /* Macro comment \\\n" + " * with a long \\\n" + " * line */ \\\n" + " A + B", + "#define X \\\n" + " /* Macro comment with a long line */ \\\n" + " A + B", + Style20); } TEST_F(FormatTestComments, KeepsTrailingPPCommentsAndSectionCommentsSeparate) { verifyFormat("#ifdef A // line about A\n" "// section comment\n" - "#endif", - getLLVMStyleWithColumns(80)); + "#endif"); + verifyFormat("#ifdef A // line 1 about A\n" + " // line 2 about A\n" + "// section comment\n" + "#endif"); verifyFormat("#ifdef A // line 1 about A\n" " // line 2 about A\n" "// section comment\n" "#endif", - getLLVMStyleWithColumns(80)); - EXPECT_EQ("#ifdef A // line 1 about A\n" - " // line 2 about A\n" - "// section comment\n" - "#endif", - format("#ifdef A // line 1 about A\n" - " // line 2 about A\n" - "// section comment\n" - "#endif", - getLLVMStyleWithColumns(80))); + "#ifdef A // line 1 about A\n" + " // line 2 about A\n" + "// section comment\n" + "#endif"); verifyFormat("int f() {\n" " int i;\n" "#ifdef A // comment about A\n" @@ -1438,46 +1430,46 @@ TEST_F(FormatTestComments, KeepsTrailingPPCommentsAndSectionCommentsSeparate) { " // section comment 3\n" " i = 4;\n" "#endif\n" - "}", - getLLVMStyleWithColumns(80)); + "}"); } TEST_F(FormatTestComments, AlignsPPElseEndifComments) { + const auto Style20 = getLLVMStyleWithColumns(20); verifyFormat("#if A\n" "#else // A\n" "int iiii;\n" "#endif // B", - getLLVMStyleWithColumns(20)); + Style20); verifyFormat("#if A\n" "#else // A\n" "int iiii; // CC\n" "#endif // B", - getLLVMStyleWithColumns(20)); - EXPECT_EQ("#if A\n" - "#else // A1\n" - " // A2\n" - "int ii;\n" - "#endif // B", - format("#if A\n" - "#else // A1\n" - " // A2\n" - "int ii;\n" - "#endif // B", - getLLVMStyleWithColumns(20))); + Style20); + verifyFormat("#if A\n" + "#else // A1\n" + " // A2\n" + "int ii;\n" + "#endif // B", + "#if A\n" + "#else // A1\n" + " // A2\n" + "int ii;\n" + "#endif // B", + Style20); } TEST_F(FormatTestComments, CommentsInStaticInitializers) { - EXPECT_EQ( + verifyFormat( "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaa, // comment\n" " aaaaaaaaaaaaaaaaaaaa};", - format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" - " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" - " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" - " aaaaaaaaaaaaaaaaaaaa , // comment\n" - " aaaaaaaaaaaaaaaaaaaa };")); + "static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" + " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" + " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" + " aaaaaaaaaaaaaaaaaaaa , // comment\n" + " aaaaaaaaaaaaaaaaaaaa };"); verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" " bbbbbbbbbbb, ccccccccccc};"); verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" @@ -1500,32 +1492,32 @@ TEST_F(FormatTestComments, CommentsInStaticInitializers) { " {// Group #3\n" " g, h, i}};"); - EXPECT_EQ("S s = {\n" - " // Some comment\n" - " a,\n" - "\n" - " // Comment after empty line\n" - " b}", - format("S s = {\n" - " // Some comment\n" - " a,\n" - " \n" - " // Comment after empty line\n" - " b\n" - "}")); - EXPECT_EQ("S s = {\n" - " /* Some comment */\n" - " a,\n" - "\n" - " /* Comment after empty line */\n" - " b}", - format("S s = {\n" - " /* Some comment */\n" - " a,\n" - " \n" - " /* Comment after empty line */\n" - " b\n" - "}")); + verifyFormat("S s = {\n" + " // Some comment\n" + " a,\n" + "\n" + " // Comment after empty line\n" + " b}", + "S s = {\n" + " // Some comment\n" + " a,\n" + " \n" + " // Comment after empty line\n" + " b\n" + "}"); + verifyFormat("S s = {\n" + " /* Some comment */\n" + " a,\n" + "\n" + " /* Comment after empty line */\n" + " b}", + "S s = {\n" + " /* Some comment */\n" + " a,\n" + " \n" + " /* Comment after empty line */\n" + " b\n" + "}"); verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" @@ -1533,486 +1525,485 @@ TEST_F(FormatTestComments, CommentsInStaticInitializers) { } TEST_F(FormatTestComments, LineCommentsAfterRightBrace) { - EXPECT_EQ("if (true) { // comment about branch\n" - " // comment about f\n" - " f();\n" - "}", - format("if (true) { // comment about branch\n" - " // comment about f\n" - " f();\n" - "}", - getLLVMStyleWithColumns(80))); - EXPECT_EQ("if (1) { // if line 1\n" - " // if line 2\n" - " // if line 3\n" - " // f line 1\n" - " // f line 2\n" - " f();\n" - "} else { // else line 1\n" - " // else line 2\n" - " // else line 3\n" - " // g line 1\n" - " g();\n" - "}", - format("if (1) { // if line 1\n" - " // if line 2\n" - " // if line 3\n" - " // f line 1\n" - " // f line 2\n" - " f();\n" - "} else { // else line 1\n" - " // else line 2\n" - " // else line 3\n" - " // g line 1\n" - " g();\n" - "}")); - EXPECT_EQ("do { // line 1\n" - " // line 2\n" - " // line 3\n" - " f();\n" - "} while (true);", - format("do { // line 1\n" - " // line 2\n" - " // line 3\n" - " f();\n" - "} while (true);", - getLLVMStyleWithColumns(80))); - EXPECT_EQ("while (a < b) { // line 1\n" - " // line 2\n" - " // line 3\n" - " f();\n" - "}", - format("while (a < b) {// line 1\n" - " // line 2\n" - " // line 3\n" - " f();\n" - "}", - getLLVMStyleWithColumns(80))); + verifyFormat("if (true) { // comment about branch\n" + " // comment about f\n" + " f();\n" + "}", + "if (true) { // comment about branch\n" + " // comment about f\n" + " f();\n" + "}"); + verifyFormat("if (1) { // if line 1\n" + " // if line 2\n" + " // if line 3\n" + " // f line 1\n" + " // f line 2\n" + " f();\n" + "} else { // else line 1\n" + " // else line 2\n" + " // else line 3\n" + " // g line 1\n" + " g();\n" + "}", + "if (1) { // if line 1\n" + " // if line 2\n" + " // if line 3\n" + " // f line 1\n" + " // f line 2\n" + " f();\n" + "} else { // else line 1\n" + " // else line 2\n" + " // else line 3\n" + " // g line 1\n" + " g();\n" + "}"); + verifyFormat("do { // line 1\n" + " // line 2\n" + " // line 3\n" + " f();\n" + "} while (true);", + "do { // line 1\n" + " // line 2\n" + " // line 3\n" + " f();\n" + "} while (true);"); + verifyFormat("while (a < b) { // line 1\n" + " // line 2\n" + " // line 3\n" + " f();\n" + "}", + "while (a < b) {// line 1\n" + " // line 2\n" + " // line 3\n" + " f();\n" + "}"); } TEST_F(FormatTestComments, ReflowsComments) { + const auto Style20 = getLLVMStyleWithColumns(20); + const auto Style22 = getLLVMStyleWithColumns(22); // Break a long line and reflow with the full next line. - EXPECT_EQ("// long long long\n" - "// long long", - format("// long long long long\n" - "// long", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long long", + "// long long long long\n" + "// long", + Style20); // Keep the trailing newline while reflowing. - EXPECT_EQ("// long long long\n" - "// long long", - format("// long long long long\n" - "// long", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long long", + "// long long long long\n" + "// long", + Style20); // Break a long line and reflow with a part of the next line. - EXPECT_EQ("// long long long\n" - "// long long\n" - "// long_long", - format("// long long long long\n" - "// long long_long", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long long\n" + "// long_long", + "// long long long long\n" + "// long long_long", + Style20); // Break but do not reflow if the first word from the next line is too long. - EXPECT_EQ("// long long long\n" - "// long\n" - "// long_long_long", - format("// long long long long\n" - "// long_long_long", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long\n" + "// long_long_long", + "// long long long long\n" + "// long_long_long", + Style20); // Don't break or reflow short lines. verifyFormat("// long\n" "// long long long lo\n" "// long long long lo\n" "// long", - getLLVMStyleWithColumns(20)); + Style20); // Keep prefixes and decorations while reflowing. - EXPECT_EQ("/// long long long\n" - "/// long long", - format("/// long long long long\n" - "/// long", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("//! long long long\n" - "//! long long", - format("//! long long long long\n" - "//! long", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("/* long long long\n" - " * long long */", - format("/* long long long long\n" - " * long */", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("///< long long long\n" - "///< long long", - format("///< long long long long\n" - "///< long", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("//!< long long long\n" - "//!< long long", - format("//!< long long long long\n" - "//!< long", - getLLVMStyleWithColumns(20))); + verifyFormat("/// long long long\n" + "/// long long", + "/// long long long long\n" + "/// long", + Style20); + verifyFormat("//! long long long\n" + "//! long long", + "//! long long long long\n" + "//! long", + Style20); + verifyFormat("/* long long long\n" + " * long long */", + "/* long long long long\n" + " * long */", + Style20); + verifyFormat("///< long long long\n" + "///< long long", + "///< long long long long\n" + "///< long", + Style20); + verifyFormat("//!< long long long\n" + "//!< long long", + "//!< long long long long\n" + "//!< long", + Style20); // Don't bring leading whitespace up while reflowing. - EXPECT_EQ("/* long long long\n" - " * long long long\n" - " */", - format("/* long long long long\n" - " * long long\n" - " */", - getLLVMStyleWithColumns(20))); + verifyFormat("/* long long long\n" + " * long long long\n" + " */", + "/* long long long long\n" + " * long long\n" + " */", + Style20); // Reflow the last line of a block comment with its trailing '*/'. - EXPECT_EQ("/* long long long\n" - " long long */", - format("/* long long long long\n" - " long */", - getLLVMStyleWithColumns(20))); + verifyFormat("/* long long long\n" + " long long */", + "/* long long long long\n" + " long */", + Style20); // Reflow two short lines; keep the postfix of the last one. - EXPECT_EQ("/* long long long\n" - " * long long long */", - format("/* long long long long\n" - " * long\n" - " * long */", - getLLVMStyleWithColumns(20))); + verifyFormat("/* long long long\n" + " * long long long */", + "/* long long long long\n" + " * long\n" + " * long */", + Style20); // Put the postfix of the last short reflow line on a newline if it doesn't // fit. - EXPECT_EQ("/* long long long\n" - " * long long longg\n" - " */", - format("/* long long long long\n" - " * long\n" - " * longg */", - getLLVMStyleWithColumns(20))); + verifyFormat("/* long long long\n" + " * long long longg\n" + " */", + "/* long long long long\n" + " * long\n" + " * longg */", + Style20); // Reflow lines with leading whitespace. - EXPECT_EQ("{\n" - " /*\n" - " * long long long\n" - " * long long long\n" - " * long long long\n" - " */\n" - "}", - format("{\n" - "/*\n" - " * long long long long\n" - " * long\n" - " * long long long long\n" - " */\n" - "}", - getLLVMStyleWithColumns(20))); + verifyFormat("{\n" + " /*\n" + " * long long long\n" + " * long long long\n" + " * long long long\n" + " */\n" + "}", + "{\n" + "/*\n" + " * long long long long\n" + " * long\n" + " * long long long long\n" + " */\n" + "}", + Style20); // Break single line block comments that are first in the line with ' *' // decoration. - EXPECT_EQ("/* long long long\n" - " * long */", - format("/* long long long long */", getLLVMStyleWithColumns(20))); + verifyFormat("/* long long long\n" + " * long */", + "/* long long long long */", Style20); // Break single line block comment that are not first in the line with ' ' // decoration. - EXPECT_EQ("int i; /* long long\n" - " long */", - format("int i; /* long long long */", getLLVMStyleWithColumns(20))); + verifyFormat("int i; /* long long\n" + " long */", + "int i; /* long long long */", Style20); // Reflow a line that goes just over the column limit. - EXPECT_EQ("// long long long\n" - "// lon long", - format("// long long long lon\n" - "// long", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// lon long", + "// long long long lon\n" + "// long", + Style20); // Stop reflowing if the next line has a different indentation than the // previous line. - EXPECT_EQ("// long long long\n" - "// long\n" - "// long long\n" - "// long", - format("// long long long long\n" - "// long long\n" - "// long", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long\n" + "// long long\n" + "// long", + "// long long long long\n" + "// long long\n" + "// long", + Style20); // Reflow into the last part of a really long line that has been broken into // multiple lines. - EXPECT_EQ("// long long long\n" - "// long long long\n" - "// long long long", - format("// long long long long long long long long\n" - "// long", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long long long\n" + "// long long long", + "// long long long long long long long long\n" + "// long", + Style20); // Break the first line, then reflow the beginning of the second and third // line up. - EXPECT_EQ("// long long long\n" - "// lon1 lon2 lon2\n" - "// lon2 lon3 lon3", - format("// long long long lon1\n" - "// lon2 lon2 lon2\n" - "// lon3 lon3", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// lon1 lon2 lon2\n" + "// lon2 lon3 lon3", + "// long long long lon1\n" + "// lon2 lon2 lon2\n" + "// lon3 lon3", + Style20); // Reflow the beginning of the second line, then break the rest. - EXPECT_EQ("// long long long\n" - "// lon1 lon2 lon2\n" - "// lon2 lon2 lon2\n" - "// lon3", - format("// long long long lon1\n" - "// lon2 lon2 lon2 lon2 lon2 lon3", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// lon1 lon2 lon2\n" + "// lon2 lon2 lon2\n" + "// lon3", + "// long long long lon1\n" + "// lon2 lon2 lon2 lon2 lon2 lon3", + Style20); // Shrink the first line, then reflow the second line up. - EXPECT_EQ("// long long long", format("// long long\n" - "// long", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long", + "// long long\n" + "// long", + Style20); // Don't shrink leading whitespace. - verifyNoChange("int i; /// a", getLLVMStyleWithColumns(20)); + verifyNoChange("int i; /// a", Style20); // Shrink trailing whitespace if there is no postfix and reflow. - EXPECT_EQ("// long long long\n" - "// long long", - format("// long long long long \n" - "// long", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long long", + "// long long long long \n" + "// long", + Style20); // Shrink trailing whitespace to a single one if there is postfix. - EXPECT_EQ("/* long long long */", - format("/* long long long */", getLLVMStyleWithColumns(20))); + verifyFormat("/* long long long */", "/* long long long */", Style20); // Break a block comment postfix if exceeding the line limit. - EXPECT_EQ("/* long\n" - " */", - format("/* long */", getLLVMStyleWithColumns(20))); + verifyFormat("/* long\n" + " */", + "/* long */", Style20); // Reflow indented comments. - EXPECT_EQ("{\n" - " // long long long\n" - " // long long\n" - " int i; /* long lon\n" - " g long\n" - " */\n" - "}", - format("{\n" - " // long long long long\n" - " // long\n" - " int i; /* long lon g\n" - " long */\n" - "}", - getLLVMStyleWithColumns(20))); + verifyFormat("{\n" + " // long long long\n" + " // long long\n" + " int i; /* long lon\n" + " g long\n" + " */\n" + "}", + "{\n" + " // long long long long\n" + " // long\n" + " int i; /* long lon g\n" + " long */\n" + "}", + Style20); // Don't realign trailing comments after reflow has happened. - EXPECT_EQ("// long long long\n" - "// long long\n" - "long i; // long", - format("// long long long long\n" - "// long\n" - "long i; // long", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("// long long long\n" - "// longng long long\n" - "// long lo", - format("// long long long longng\n" - "// long long long\n" - "// lo", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long long\n" + "long i; // long", + "// long long long long\n" + "// long\n" + "long i; // long", + Style20); + verifyFormat("// long long long\n" + "// longng long long\n" + "// long lo", + "// long long long longng\n" + "// long long long\n" + "// lo", + Style20); // Reflow lines after a broken line. - EXPECT_EQ("int a; // Trailing\n" - " // comment on\n" - " // 2 or 3\n" - " // lines.", - format("int a; // Trailing comment\n" - " // on 2\n" - " // or 3\n" - " // lines.", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("/// This long line\n" - "/// gets reflown.", - format("/// This long line gets\n" - "/// reflown.", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("//! This long line\n" - "//! gets reflown.", - format(" //! This long line gets\n" - " //! reflown.", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("/* This long line\n" - " * gets reflown.\n" - " */", - format("/* This long line gets\n" - " * reflown.\n" - " */", - getLLVMStyleWithColumns(20))); + verifyFormat("int a; // Trailing\n" + " // comment on\n" + " // 2 or 3\n" + " // lines.", + "int a; // Trailing comment\n" + " // on 2\n" + " // or 3\n" + " // lines.", + Style20); + verifyFormat("/// This long line\n" + "/// gets reflown.", + "/// This long line gets\n" + "/// reflown.", + Style20); + verifyFormat("//! This long line\n" + "//! gets reflown.", + " //! This long line gets\n" + " //! reflown.", + Style20); + verifyFormat("/* This long line\n" + " * gets reflown.\n" + " */", + "/* This long line gets\n" + " * reflown.\n" + " */", + Style20); // Reflow after indentation makes a line too long. - EXPECT_EQ("{\n" - " // long long long\n" - " // lo long\n" - "}", - format("{\n" - "// long long long lo\n" - "// long\n" - "}", - getLLVMStyleWithColumns(20))); + verifyFormat("{\n" + " // long long long\n" + " // lo long\n" + "}", + "{\n" + "// long long long lo\n" + "// long\n" + "}", + Style20); // Break and reflow multiple lines. - EXPECT_EQ("/*\n" - " * Reflow the end of\n" - " * line by 11 22 33\n" - " * 4.\n" - " */", - format("/*\n" - " * Reflow the end of line\n" - " * by\n" - " * 11\n" - " * 22\n" - " * 33\n" - " * 4.\n" - " */", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("/// First line gets\n" - "/// broken. Second\n" - "/// line gets\n" - "/// reflown and\n" - "/// broken. Third\n" - "/// gets reflown.", - format("/// First line gets broken.\n" - "/// Second line gets reflown and broken.\n" - "/// Third gets reflown.", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("int i; // first long\n" - " // long snd\n" - " // long.", - format("int i; // first long long\n" - " // snd long.", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("{\n" - " // first long line\n" - " // line second\n" - " // long line line\n" - " // third long line\n" - " // line\n" - "}", - format("{\n" - " // first long line line\n" - " // second long line line\n" - " // third long line line\n" - "}", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("int i; /* first line\n" - " * second\n" - " * line third\n" - " * line\n" - " */", - format("int i; /* first line\n" - " * second line\n" - " * third line\n" - " */", - getLLVMStyleWithColumns(20))); + verifyFormat("/*\n" + " * Reflow the end of\n" + " * line by 11 22 33\n" + " * 4.\n" + " */", + "/*\n" + " * Reflow the end of line\n" + " * by\n" + " * 11\n" + " * 22\n" + " * 33\n" + " * 4.\n" + " */", + Style20); + verifyFormat("/// First line gets\n" + "/// broken. Second\n" + "/// line gets\n" + "/// reflown and\n" + "/// broken. Third\n" + "/// gets reflown.", + "/// First line gets broken.\n" + "/// Second line gets reflown and broken.\n" + "/// Third gets reflown.", + Style20); + verifyFormat("int i; // first long\n" + " // long snd\n" + " // long.", + "int i; // first long long\n" + " // snd long.", + Style20); + verifyFormat("{\n" + " // first long line\n" + " // line second\n" + " // long line line\n" + " // third long line\n" + " // line\n" + "}", + "{\n" + " // first long line line\n" + " // second long line line\n" + " // third long line line\n" + "}", + Style20); + verifyFormat("int i; /* first line\n" + " * second\n" + " * line third\n" + " * line\n" + " */", + "int i; /* first line\n" + " * second line\n" + " * third line\n" + " */", + Style20); // Reflow the last two lines of a section that starts with a line having // different indentation. - EXPECT_EQ("// long\n" - "// long long long\n" - "// long long", - format("// long\n" - "// long long long long\n" - "// long", - getLLVMStyleWithColumns(20))); + verifyFormat("// long\n" + "// long long long\n" + "// long long", + "// long\n" + "// long long long long\n" + "// long", + Style20); // Keep the block comment endling '*/' while reflowing. - EXPECT_EQ("/* Long long long\n" - " * line short */", - format("/* Long long long line\n" - " * short */", - getLLVMStyleWithColumns(20))); + verifyFormat("/* Long long long\n" + " * line short */", + "/* Long long long line\n" + " * short */", + Style20); // Don't reflow between separate blocks of comments. - EXPECT_EQ("/* First comment\n" - " * block will */\n" - "/* Snd\n" - " */", - format("/* First comment block\n" - " * will */\n" - "/* Snd\n" - " */", - getLLVMStyleWithColumns(20))); + verifyFormat("/* First comment\n" + " * block will */\n" + "/* Snd\n" + " */", + "/* First comment block\n" + " * will */\n" + "/* Snd\n" + " */", + Style20); // Don't reflow across blank comment lines. - EXPECT_EQ("int i; // This long\n" - " // line gets\n" - " // broken.\n" - " //\n" - " // keep.", - format("int i; // This long line gets broken.\n" - " // \n" - " // keep.", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("{\n" - " /// long long long\n" - " /// long long\n" - " ///\n" - " /// long\n" - "}", - format("{\n" - " /// long long long long\n" - " /// long\n" - " ///\n" - " /// long\n" - "}", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("//! long long long\n" - "//! long\n" - "\n" - "//! long", - format("//! long long long long\n" - "\n" - "//! long", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("/* long long long\n" - " long\n" - "\n" - " long */", - format("/* long long long long\n" - "\n" - " long */", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("/* long long long\n" - " * long\n" - " *\n" - " * long */", - format("/* long long long long\n" - " *\n" - " * long */", - getLLVMStyleWithColumns(20))); + verifyFormat("int i; // This long\n" + " // line gets\n" + " // broken.\n" + " //\n" + " // keep.", + "int i; // This long line gets broken.\n" + " // \n" + " // keep.", + Style20); + verifyFormat("{\n" + " /// long long long\n" + " /// long long\n" + " ///\n" + " /// long\n" + "}", + "{\n" + " /// long long long long\n" + " /// long\n" + " ///\n" + " /// long\n" + "}", + Style20); + verifyFormat("//! long long long\n" + "//! long\n" + "\n" + "//! long", + "//! long long long long\n" + "\n" + "//! long", + Style20); + verifyFormat("/* long long long\n" + " long\n" + "\n" + " long */", + "/* long long long long\n" + "\n" + " long */", + Style20); + verifyFormat("/* long long long\n" + " * long\n" + " *\n" + " * long */", + "/* long long long long\n" + " *\n" + " * long */", + Style20); // Don't reflow lines having content that is a single character. - EXPECT_EQ("// long long long\n" - "// long\n" - "// l", - format("// long long long long\n" - "// l", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long\n" + "// l", + "// long long long long\n" + "// l", + Style20); // Don't reflow lines starting with two punctuation characters. - EXPECT_EQ("// long long long\n" - "// long\n" - "// ... --- ...", - format("// long long long long\n" - "// ... --- ...", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long\n" + "// ... --- ...", + "// long long long long\n" + "// ... --- ...", + Style20); // Don't reflow lines starting with '@'. - EXPECT_EQ("// long long long\n" - "// long\n" - "// @param arg", - format("// long long long long\n" - "// @param arg", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long\n" + "// @param arg", + "// long long long long\n" + "// @param arg", + Style20); // Don't reflow lines starting with '\'. verifyFormat("// long long long\n" @@ -2020,433 +2011,435 @@ TEST_F(FormatTestComments, ReflowsComments) { "// \\param arg", "// long long long long\n" "// \\param arg", - getLLVMStyleWithColumns(20)); + Style20); // Don't reflow lines starting with 'TODO'. - EXPECT_EQ("// long long long\n" - "// long\n" - "// TODO: long", - format("// long long long long\n" - "// TODO: long", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long\n" + "// TODO: long", + "// long long long long\n" + "// TODO: long", + Style20); // Don't reflow lines starting with 'FIXME'. - EXPECT_EQ("// long long long\n" - "// long\n" - "// FIXME: long", - format("// long long long long\n" - "// FIXME: long", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long\n" + "// FIXME: long", + "// long long long long\n" + "// FIXME: long", + Style20); // Don't reflow lines starting with 'XXX'. - EXPECT_EQ("// long long long\n" - "// long\n" - "// XXX: long", - format("// long long long long\n" - "// XXX: long", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long\n" + "// XXX: long", + "// long long long long\n" + "// XXX: long", + Style20); // Don't reflow comment pragmas. - EXPECT_EQ("// long long long\n" - "// long\n" - "// IWYU pragma:", - format("// long long long long\n" - "// IWYU pragma:", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("/* long long long\n" - " * long\n" - " * IWYU pragma:\n" - " */", - format("/* long long long long\n" - " * IWYU pragma:\n" - " */", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long\n" + "// IWYU pragma:", + "// long long long long\n" + "// IWYU pragma:", + Style20); + verifyFormat("/* long long long\n" + " * long\n" + " * IWYU pragma:\n" + " */", + "/* long long long long\n" + " * IWYU pragma:\n" + " */", + Style20); // Reflow lines that have a non-punctuation character among their first 2 // characters. - EXPECT_EQ("// long long long\n" - "// long 'long'", - format("// long long long long\n" - "// 'long'", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long 'long'", + "// long long long long\n" + "// 'long'", + Style20); // Don't reflow between separate blocks of comments. - EXPECT_EQ("/* First comment\n" - " * block will */\n" - "/* Snd\n" - " */", - format("/* First comment block\n" - " * will */\n" - "/* Snd\n" - " */", - getLLVMStyleWithColumns(20))); + verifyFormat("/* First comment\n" + " * block will */\n" + "/* Snd\n" + " */", + "/* First comment block\n" + " * will */\n" + "/* Snd\n" + " */", + Style20); // Don't reflow lines having different indentation. - EXPECT_EQ("// long long long\n" - "// long\n" - "// long", - format("// long long long long\n" - "// long", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long\n" + "// long", + "// long long long long\n" + "// long", + Style20); // Don't reflow separate bullets in list - EXPECT_EQ("// - long long long\n" - "// long\n" - "// - long", - format("// - long long long long\n" - "// - long", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("// * long long long\n" - "// long\n" - "// * long", - format("// * long long long long\n" - "// * long", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("// + long long long\n" - "// long\n" - "// + long", - format("// + long long long long\n" - "// + long", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("// 1. long long long\n" - "// long\n" - "// 2. long", - format("// 1. long long long long\n" - "// 2. long", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("// -# long long long\n" - "// long\n" - "// -# long", - format("// -# long long long long\n" - "// -# long", - getLLVMStyleWithColumns(20))); - - EXPECT_EQ("// - long long long\n" - "// long long long\n" - "// - long", - format("// - long long long long\n" - "// long long\n" - "// - long", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("// - long long long\n" - "// long long long\n" - "// long\n" - "// - long", - format("// - long long long long\n" - "// long long long\n" - "// - long", - getLLVMStyleWithColumns(20))); + verifyFormat("// - long long long\n" + "// long\n" + "// - long", + "// - long long long long\n" + "// - long", + Style20); + verifyFormat("// * long long long\n" + "// long\n" + "// * long", + "// * long long long long\n" + "// * long", + Style20); + verifyFormat("// + long long long\n" + "// long\n" + "// + long", + "// + long long long long\n" + "// + long", + Style20); + verifyFormat("// 1. long long long\n" + "// long\n" + "// 2. long", + "// 1. long long long long\n" + "// 2. long", + Style20); + verifyFormat("// -# long long long\n" + "// long\n" + "// -# long", + "// -# long long long long\n" + "// -# long", + Style20); + + verifyFormat("// - long long long\n" + "// long long long\n" + "// - long", + "// - long long long long\n" + "// long long\n" + "// - long", + Style20); + verifyFormat("// - long long long\n" + "// long long long\n" + "// long\n" + "// - long", + "// - long long long long\n" + "// long long long\n" + "// - long", + Style20); // Large number (>2 digits) are not list items - EXPECT_EQ("// long long long\n" - "// long 1024. long.", - format("// long long long long\n" - "// 1024. long.", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long long\n" + "// long 1024. long.", + "// long long long long\n" + "// 1024. long.", + Style20); // Do not break before number, to avoid introducing a non-reflowable doxygen // list item. - EXPECT_EQ("// long long\n" - "// long 10. long.", - format("// long long long 10.\n" - "// long.", - getLLVMStyleWithColumns(20))); + verifyFormat("// long long\n" + "// long 10. long.", + "// long long long 10.\n" + "// long.", + Style20); // Don't break or reflow after implicit string literals. verifyFormat("#include <t> // l l l\n" " // l", - getLLVMStyleWithColumns(20)); + Style20); // Don't break or reflow comments on import lines. - EXPECT_EQ("#include \"t\" /* l l l\n" - " * l */", - format("#include \"t\" /* l l l\n" - " * l */", - getLLVMStyleWithColumns(20))); + verifyFormat("#include \"t\" /* l l l\n" + " * l */", + "#include \"t\" /* l l l\n" + " * l */", + Style20); // Don't reflow between different trailing comment sections. - EXPECT_EQ("int i; // long long\n" - " // long\n" - "int j; // long long\n" - " // long", - format("int i; // long long long\n" - "int j; // long long long", - getLLVMStyleWithColumns(20))); + verifyFormat("int i; // long long\n" + " // long\n" + "int j; // long long\n" + " // long", + "int i; // long long long\n" + "int j; // long long long", + Style20); // Don't reflow if the first word on the next line is longer than the // available space at current line. - EXPECT_EQ("int i; // trigger\n" - " // reflow\n" - " // longsec", - format("int i; // trigger reflow\n" - " // longsec", - getLLVMStyleWithColumns(20))); + verifyFormat("int i; // trigger\n" + " // reflow\n" + " // longsec", + "int i; // trigger reflow\n" + " // longsec", + Style20); // Simple case that correctly handles reflow in parameter lists. - EXPECT_EQ("a = f(/* looooooooong\n" - " * long long\n" - " */\n" - " a);", - format("a = f(/* looooooooong long\n* long\n*/ a);", - getLLVMStyleWithColumns(22))); + verifyFormat("a = f(/* looooooooong\n" + " * long long\n" + " */\n" + " a);", + "a = f(/* looooooooong long\n* long\n*/ a);", Style22); // Tricky case that has fewer lines if we reflow the comment, ending up with // fewer lines. - EXPECT_EQ("a = f(/* loooooong\n" - " * long long\n" - " */\n" - " a);", - format("a = f(/* loooooong long\n* long\n*/ a);", - getLLVMStyleWithColumns(22))); + verifyFormat("a = f(/* loooooong\n" + " * long long\n" + " */\n" + " a);", + "a = f(/* loooooong long\n* long\n*/ a);", Style22); // Keep empty comment lines. - EXPECT_EQ("/**/", format(" /**/", getLLVMStyleWithColumns(20))); - EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20))); - EXPECT_EQ("/* */", format(" /* */", getLLVMStyleWithColumns(20))); - EXPECT_EQ("//", format(" // ", getLLVMStyleWithColumns(20))); - EXPECT_EQ("///", format(" /// ", getLLVMStyleWithColumns(20))); + verifyFormat("/**/", " /**/", Style20); + verifyFormat("/* */", " /* */", Style20); + verifyFormat("/* */", " /* */", Style20); + verifyFormat("//", " // ", Style20); + verifyFormat("///", " /// ", Style20); } TEST_F(FormatTestComments, ReflowsCommentsPrecise) { + auto Style = getLLVMStyleWithColumns(20); + // FIXME: This assumes we do not continue compressing whitespace once we are // in reflow mode. Consider compressing whitespace. // Test that we stop reflowing precisely at the column limit. // After reflowing, "// reflows into foo" does not fit the column limit, // so we compress the whitespace. - EXPECT_EQ("// some text that\n" - "// reflows into foo", - format("// some text that reflows\n" - "// into foo", - getLLVMStyleWithColumns(20))); + verifyFormat("// some text that\n" + "// reflows into foo", + "// some text that reflows\n" + "// into foo", + Style); + Style.ColumnLimit = 21; // Given one more column, "// reflows into foo" does fit the limit, so we // do not compress the whitespace. - EXPECT_EQ("// some text that\n" - "// reflows into foo", - format("// some text that reflows\n" - "// into foo", - getLLVMStyleWithColumns(21))); + verifyFormat("// some text that\n" + "// reflows into foo", + "// some text that reflows\n" + "// into foo", + Style); // Make sure that we correctly account for the space added in the reflow case // when making the reflowing decision. // First, when the next line ends precisely one column over the limit, do not // reflow. - EXPECT_EQ("// some text that\n" - "// reflows\n" - "// into1234567", - format("// some text that reflows\n" - "// into1234567", - getLLVMStyleWithColumns(21))); + verifyFormat("// some text that\n" + "// reflows\n" + "// into1234567", + "// some text that reflows\n" + "// into1234567", + Style); // Secondly, when the next line ends later, but the first word in that line // is precisely one column over the limit, do not reflow. - EXPECT_EQ("// some text that\n" - "// reflows\n" - "// into1234567 f", - format("// some text that reflows\n" - "// into1234567 f", - getLLVMStyleWithColumns(21))); + verifyFormat("// some text that\n" + "// reflows\n" + "// into1234567 f", + "// some text that reflows\n" + "// into1234567 f", + Style); } TEST_F(FormatTestComments, ReflowsCommentsWithExtraWhitespace) { + const auto Style16 = getLLVMStyleWithColumns(16); // Baseline. - EXPECT_EQ("// some text\n" - "// that re flows", - format("// some text that\n" - "// re flows", - getLLVMStyleWithColumns(16))); - EXPECT_EQ("// some text\n" - "// that re flows", - format("// some text that\n" - "// re flows", - getLLVMStyleWithColumns(16))); - EXPECT_EQ("/* some text\n" - " * that re flows\n" - " */", - format("/* some text that\n" - "* re flows\n" - "*/", - getLLVMStyleWithColumns(16))); + verifyFormat("// some text\n" + "// that re flows", + "// some text that\n" + "// re flows", + Style16); + verifyFormat("// some text\n" + "// that re flows", + "// some text that\n" + "// re flows", + Style16); + verifyFormat("/* some text\n" + " * that re flows\n" + " */", + "/* some text that\n" + "* re flows\n" + "*/", + Style16); // FIXME: We do not reflow if the indent of two subsequent lines differs; // given that this is different behavior from block comments, do we want // to keep this? - EXPECT_EQ("// some text\n" - "// that\n" - "// re flows", - format("// some text that\n" - "// re flows", - getLLVMStyleWithColumns(16))); + verifyFormat("// some text\n" + "// that\n" + "// re flows", + "// some text that\n" + "// re flows", + Style16); // Space within parts of a line that fit. // FIXME: Use the earliest possible split while reflowing to compress the // whitespace within the line. - EXPECT_EQ("// some text that\n" - "// does re flow\n" - "// more here", - format("// some text that does\n" - "// re flow more here", - getLLVMStyleWithColumns(21))); + verifyFormat("// some text that\n" + "// does re flow\n" + "// more here", + "// some text that does\n" + "// re flow more here", + getLLVMStyleWithColumns(21)); } TEST_F(FormatTestComments, IgnoresIf0Contents) { - EXPECT_EQ("#if 0\n" - "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" - "#endif\n" - "void f() {}", - format("#if 0\n" - "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" - "#endif\n" - "void f( ) { }")); - EXPECT_EQ("#if false\n" - "void f( ) { }\n" - "#endif\n" - "void g() {}", - format("#if false\n" - "void f( ) { }\n" - "#endif\n" - "void g( ) { }")); - EXPECT_EQ("enum E {\n" - " One,\n" - " Two,\n" - "#if 0\n" - "Three,\n" - " Four,\n" - "#endif\n" - " Five\n" - "};", - format("enum E {\n" - " One,Two,\n" - "#if 0\n" - "Three,\n" - " Four,\n" - "#endif\n" - " Five};")); - EXPECT_EQ("enum F {\n" - " One,\n" - "#if 1\n" - " Two,\n" - "#if 0\n" - "Three,\n" - " Four,\n" - "#endif\n" - " Five\n" - "#endif\n" - "};", - format("enum F {\n" - "One,\n" - "#if 1\n" - "Two,\n" - "#if 0\n" - "Three,\n" - " Four,\n" - "#endif\n" - "Five\n" - "#endif\n" - "};")); - EXPECT_EQ("enum G {\n" - " One,\n" - "#if 0\n" - "Two,\n" - "#else\n" - " Three,\n" - "#endif\n" - " Four\n" - "};", - format("enum G {\n" - "One,\n" - "#if 0\n" - "Two,\n" - "#else\n" - "Three,\n" - "#endif\n" - "Four\n" - "};")); - EXPECT_EQ("enum H {\n" - " One,\n" - "#if 0\n" - "#ifdef Q\n" - "Two,\n" - "#else\n" - "Three,\n" - "#endif\n" - "#endif\n" - " Four\n" - "};", - format("enum H {\n" - "One,\n" - "#if 0\n" - "#ifdef Q\n" - "Two,\n" - "#else\n" - "Three,\n" - "#endif\n" - "#endif\n" - "Four\n" - "};")); - EXPECT_EQ("enum I {\n" - " One,\n" - "#if /* test */ 0 || 1\n" - "Two,\n" - "Three,\n" - "#endif\n" - " Four\n" - "};", - format("enum I {\n" - "One,\n" - "#if /* test */ 0 || 1\n" - "Two,\n" - "Three,\n" - "#endif\n" - "Four\n" - "};")); - EXPECT_EQ("enum J {\n" - " One,\n" - "#if 0\n" - "#if 0\n" - "Two,\n" - "#else\n" - "Three,\n" - "#endif\n" - "Four,\n" - "#endif\n" - " Five\n" - "};", - format("enum J {\n" - "One,\n" - "#if 0\n" - "#if 0\n" - "Two,\n" - "#else\n" - "Three,\n" - "#endif\n" - "Four,\n" - "#endif\n" - "Five\n" - "};")); + verifyFormat("#if 0\n" + "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" + "#endif\n" + "void f() {}", + "#if 0\n" + "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" + "#endif\n" + "void f( ) { }"); + verifyFormat("#if false\n" + "void f( ) { }\n" + "#endif\n" + "void g() {}", + "#if false\n" + "void f( ) { }\n" + "#endif\n" + "void g( ) { }"); + verifyFormat("enum E {\n" + " One,\n" + " Two,\n" + "#if 0\n" + "Three,\n" + " Four,\n" + "#endif\n" + " Five\n" + "};", + "enum E {\n" + " One,Two,\n" + "#if 0\n" + "Three,\n" + " Four,\n" + "#endif\n" + " Five};"); + verifyFormat("enum F {\n" + " One,\n" + "#if 1\n" + " Two,\n" + "#if 0\n" + "Three,\n" + " Four,\n" + "#endif\n" + " Five\n" + "#endif\n" + "};", + "enum F {\n" + "One,\n" + "#if 1\n" + "Two,\n" + "#if 0\n" + "Three,\n" + " Four,\n" + "#endif\n" + "Five\n" + "#endif\n" + "};"); + verifyFormat("enum G {\n" + " One,\n" + "#if 0\n" + "Two,\n" + "#else\n" + " Three,\n" + "#endif\n" + " Four\n" + "};", + "enum G {\n" + "One,\n" + "#if 0\n" + "Two,\n" + "#else\n" + "Three,\n" + "#endif\n" + "Four\n" + "};"); + verifyFormat("enum H {\n" + " One,\n" + "#if 0\n" + "#ifdef Q\n" + "Two,\n" + "#else\n" + "Three,\n" + "#endif\n" + "#endif\n" + " Four\n" + "};", + "enum H {\n" + "One,\n" + "#if 0\n" + "#ifdef Q\n" + "Two,\n" + "#else\n" + "Three,\n" + "#endif\n" + "#endif\n" + "Four\n" + "};"); + verifyFormat("enum I {\n" + " One,\n" + "#if /* test */ 0 || 1\n" + "Two,\n" + "Three,\n" + "#endif\n" + " Four\n" + "};", + "enum I {\n" + "One,\n" + "#if /* test */ 0 || 1\n" + "Two,\n" + "Three,\n" + "#endif\n" + "Four\n" + "};"); + verifyFormat("enum J {\n" + " One,\n" + "#if 0\n" + "#if 0\n" + "Two,\n" + "#else\n" + "Three,\n" + "#endif\n" + "Four,\n" + "#endif\n" + " Five\n" + "};", + "enum J {\n" + "One,\n" + "#if 0\n" + "#if 0\n" + "Two,\n" + "#else\n" + "Three,\n" + "#endif\n" + "Four,\n" + "#endif\n" + "Five\n" + "};"); // Ignore stuff in SWIG-blocks. - EXPECT_EQ("#ifdef SWIG\n" - "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" - "#endif\n" - "void f() {}", - format("#ifdef SWIG\n" - "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" - "#endif\n" - "void f( ) { }")); - EXPECT_EQ("#ifndef SWIG\n" - "void f() {}\n" - "#endif", - format("#ifndef SWIG\n" - "void f( ) { }\n" - "#endif")); + verifyFormat("#ifdef SWIG\n" + "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" + "#endif\n" + "void f() {}", + "#ifdef SWIG\n" + "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n" + "#endif\n" + "void f( ) { }"); + verifyFormat("#ifndef SWIG\n" + "void f() {}\n" + "#endif", + "#ifndef SWIG\n" + "void f( ) { }\n" + "#endif"); } TEST_F(FormatTestComments, DontCrashOnBlockComments) { - EXPECT_EQ( + verifyFormat( "int xxxxxxxxx; /* " "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n" "zzzzzz\n" "0*/", - format("int xxxxxxxxx; /* " - "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" - "0*/")); + "int xxxxxxxxx; /* " + "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n" + "0*/"); } TEST_F(FormatTestComments, BlockCommentsInControlLoops) { @@ -2470,225 +2463,229 @@ TEST_F(FormatTestComments, BlockCommentsInControlLoops) { } TEST_F(FormatTestComments, BlockComments) { - EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", - format("/* *//* */ /* */\n/* *//* */ /* */")); - EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); - EXPECT_EQ("#define A /*123*/ \\\n" - " b\n" - "/* */\n" - "someCall(\n" - " parameter);", - format("#define A /*123*/ b\n" - "/* */\n" - "someCall(parameter);", - getLLVMStyleWithColumns(15))); - - EXPECT_EQ("#define A\n" - "/* */ someCall(\n" - " parameter);", - format("#define A\n" - "/* */someCall(parameter);", - getLLVMStyleWithColumns(15))); + const auto Style10 = getLLVMStyleWithColumns(10); + const auto Style15 = getLLVMStyleWithColumns(15); + + verifyFormat("/* */ /* */ /* */\n/* */ /* */ /* */", + "/* *//* */ /* */\n/* *//* */ /* */"); + verifyFormat("/* */ a /* */ b;", " /* */ a/* */ b;"); + verifyFormat("#define A /*123*/ \\\n" + " b\n" + "/* */\n" + "someCall(\n" + " parameter);", + "#define A /*123*/ b\n" + "/* */\n" + "someCall(parameter);", + Style15); + + verifyFormat("#define A\n" + "/* */ someCall(\n" + " parameter);", + "#define A\n" + "/* */someCall(parameter);", + Style15); verifyNoChange("/*\n**\n*/"); - EXPECT_EQ("/*\n" - " *\n" - " * aaaaaa\n" - " * aaaaaa\n" - " */", - format("/*\n" - "*\n" - " * aaaaaa aaaaaa\n" - "*/", - getLLVMStyleWithColumns(10))); - EXPECT_EQ("/*\n" - "**\n" - "* aaaaaa\n" - "* aaaaaa\n" - "*/", - format("/*\n" - "**\n" - "* aaaaaa aaaaaa\n" - "*/", - getLLVMStyleWithColumns(10))); - EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" - " /* line 1\n" - " bbbbbbbbbbbb */\n" - " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", - format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" - " /* line 1\n" - " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", - getLLVMStyleWithColumns(50))); + verifyFormat("/*\n" + " *\n" + " * aaaaaa\n" + " * aaaaaa\n" + " */", + "/*\n" + "*\n" + " * aaaaaa aaaaaa\n" + "*/", + Style10); + verifyFormat("/*\n" + "**\n" + "* aaaaaa\n" + "* aaaaaa\n" + "*/", + "/*\n" + "**\n" + "* aaaaaa aaaaaa\n" + "*/", + Style10); + verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" + " /* line 1\n" + " bbbbbbbbbbbb */\n" + " bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", + "int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" + " /* line 1\n" + " bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;", + getLLVMStyleWithColumns(50)); FormatStyle NoBinPacking = getLLVMStyle(); NoBinPacking.BinPackParameters = FormatStyle::BPPS_OnePerLine; - EXPECT_EQ("someFunction(1, /* comment 1 */\n" - " 2, /* comment 2 */\n" - " 3, /* comment 3 */\n" - " aaaa,\n" - " bbbb);", - format("someFunction (1, /* comment 1 */\n" - " 2, /* comment 2 */ \n" - " 3, /* comment 3 */\n" - "aaaa, bbbb );", - NoBinPacking)); + verifyFormat("someFunction(1, /* comment 1 */\n" + " 2, /* comment 2 */\n" + " 3, /* comment 3 */\n" + " aaaa,\n" + " bbbb);", + "someFunction (1, /* comment 1 */\n" + " 2, /* comment 2 */ \n" + " 3, /* comment 3 */\n" + "aaaa, bbbb );", + NoBinPacking); verifyFormat( "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); - EXPECT_EQ( + verifyFormat( "bool aaaaaaaaaaaaa = /* trailing comment */\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", - format( - "bool aaaaaaaaaaaaa = /* trailing comment */\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); - EXPECT_EQ( - "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" - "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" - "int cccccccccccccccccccccccccccccc; /* comment */", - format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" - "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" - "int cccccccccccccccccccccccccccccc; /* comment */")); + "bool aaaaaaaaaaaaa = /* trailing comment */\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"); + verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" + "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" + "int cccccccccccccccccccccccccccccc; /* comment */", + "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" + "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" + "int cccccccccccccccccccccccccccccc; /* comment */"); verifyFormat("void f(int * /* unused */) {}"); - EXPECT_EQ("/*\n" - " **\n" - " */", - format("/*\n" - " **\n" - " */")); - EXPECT_EQ("/*\n" - " *q\n" - " */", - format("/*\n" - " *q\n" - " */")); - EXPECT_EQ("/*\n" - " * q\n" - " */", - format("/*\n" - " * q\n" - " */")); - EXPECT_EQ("/*\n" - " **/", - format("/*\n" - " **/")); - EXPECT_EQ("/*\n" - " ***/", - format("/*\n" - " ***/")); + verifyFormat("/*\n" + " **\n" + " */", + "/*\n" + " **\n" + " */"); + verifyFormat("/*\n" + " *q\n" + " */", + "/*\n" + " *q\n" + " */"); + verifyFormat("/*\n" + " * q\n" + " */", + "/*\n" + " * q\n" + " */"); + verifyFormat("/*\n" + " **/", + "/*\n" + " **/"); + verifyFormat("/*\n" + " ***/", + "/*\n" + " ***/"); } TEST_F(FormatTestComments, BlockCommentsInMacros) { - EXPECT_EQ("#define A \\\n" - " { \\\n" - " /* one line */ \\\n" - " someCall();", - format("#define A { \\\n" - " /* one line */ \\\n" - " someCall();", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("#define A \\\n" - " { \\\n" - " /* previous */ \\\n" - " /* one line */ \\\n" - " someCall();", - format("#define A { \\\n" - " /* previous */ \\\n" - " /* one line */ \\\n" - " someCall();", - getLLVMStyleWithColumns(20))); + const auto Style20 = getLLVMStyleWithColumns(20); + + verifyFormat("#define A \\\n" + " { \\\n" + " /* one line */ \\\n" + " someCall();", + "#define A { \\\n" + " /* one line */ \\\n" + " someCall();", + Style20); + verifyFormat("#define A \\\n" + " { \\\n" + " /* previous */ \\\n" + " /* one line */ \\\n" + " someCall();", + "#define A { \\\n" + " /* previous */ \\\n" + " /* one line */ \\\n" + " someCall();", + Style20); } TEST_F(FormatTestComments, BlockCommentsAtEndOfLine) { - EXPECT_EQ("a = {\n" - " 1111 /* */\n" - "};", - format("a = {1111 /* */\n" - "};", - getLLVMStyleWithColumns(15))); - EXPECT_EQ("a = {\n" - " 1111 /* */\n" - "};", - format("a = {1111 /* */\n" - "};", - getLLVMStyleWithColumns(15))); - EXPECT_EQ("a = {\n" - " 1111 /* a\n" - " */\n" - "};", - format("a = {1111 /* a */\n" - "};", - getLLVMStyleWithColumns(15))); + const auto Style15 = getLLVMStyleWithColumns(15); + + verifyFormat("a = {\n" + " 1111 /* */\n" + "};", + "a = {1111 /* */\n" + "};", + Style15); + verifyFormat("a = {\n" + " 1111 /* */\n" + "};", + "a = {1111 /* */\n" + "};", + Style15); + verifyFormat("a = {\n" + " 1111 /* a\n" + " */\n" + "};", + "a = {1111 /* a */\n" + "};", + Style15); } TEST_F(FormatTestComments, BreaksAfterMultilineBlockCommentsInParamLists) { - EXPECT_EQ("a = f(/* long\n" - " long */\n" - " a);", - format("a = f(/* long long */ a);", getLLVMStyleWithColumns(16))); - EXPECT_EQ("a = f(\n" - " /* long\n" - " long */\n" - " a);", - format("a = f(/* long long */ a);", getLLVMStyleWithColumns(15))); - - EXPECT_EQ("a = f(/* long\n" - " long\n" - " */\n" - " a);", - format("a = f(/* long\n" - " long\n" - " */a);", - getLLVMStyleWithColumns(16))); - - EXPECT_EQ("a = f(/* long\n" - " long\n" - " */\n" - " a);", - format("a = f(/* long\n" - " long\n" - " */ a);", - getLLVMStyleWithColumns(16))); - - EXPECT_EQ("a = f(/* long\n" - " long\n" - " */\n" - " (1 + 1));", - format("a = f(/* long\n" - " long\n" - " */ (1 + 1));", - getLLVMStyleWithColumns(16))); - - EXPECT_EQ( - "a = f(a,\n" - " /* long\n" - " long */\n" - " b);", - format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(16))); - - EXPECT_EQ( - "a = f(\n" - " a,\n" - " /* long\n" - " long */\n" - " b);", - format("a = f(a, /* long long */ b);", getLLVMStyleWithColumns(15))); - - EXPECT_EQ("a = f(a,\n" - " /* long\n" - " long */\n" - " (1 + 1));", - format("a = f(a, /* long long */ (1 + 1));", - getLLVMStyleWithColumns(16))); - EXPECT_EQ("a = f(\n" - " a,\n" - " /* long\n" - " long */\n" - " (1 + 1));", - format("a = f(a, /* long long */ (1 + 1));", - getLLVMStyleWithColumns(15))); + const auto Style15 = getLLVMStyleWithColumns(15); + const auto Style16 = getLLVMStyleWithColumns(16); + + verifyFormat("a = f(/* long\n" + " long */\n" + " a);", + "a = f(/* long long */ a);", Style16); + verifyFormat("a = f(\n" + " /* long\n" + " long */\n" + " a);", + "a = f(/* long long */ a);", Style15); + + verifyFormat("a = f(/* long\n" + " long\n" + " */\n" + " a);", + "a = f(/* long\n" + " long\n" + " */a);", + Style16); + + verifyFormat("a = f(/* long\n" + " long\n" + " */\n" + " a);", + "a = f(/* long\n" + " long\n" + " */ a);", + Style16); + + verifyFormat("a = f(/* long\n" + " long\n" + " */\n" + " (1 + 1));", + "a = f(/* long\n" + " long\n" + " */ (1 + 1));", + Style16); + + verifyFormat("a = f(a,\n" + " /* long\n" + " long */\n" + " b);", + "a = f(a, /* long long */ b);", Style16); + + verifyFormat("a = f(\n" + " a,\n" + " /* long\n" + " long */\n" + " b);", + "a = f(a, /* long long */ b);", Style15); + + verifyFormat("a = f(a,\n" + " /* long\n" + " long */\n" + " (1 + 1));", + "a = f(a, /* long long */ (1 + 1));", Style16); + verifyFormat("a = f(\n" + " a,\n" + " /* long\n" + " long */\n" + " (1 + 1));", + "a = f(a, /* long long */ (1 + 1));", Style15); } TEST_F(FormatTestComments, IndentLineCommentsInStartOfBlockAtEndOfFile) { @@ -2698,229 +2695,224 @@ TEST_F(FormatTestComments, IndentLineCommentsInStartOfBlockAtEndOfFile) { } TEST_F(FormatTestComments, AlignTrailingComments) { - EXPECT_EQ("#define MACRO(V) \\\n" - " V(Rt2) /* one more char */ \\\n" - " V(Rs) /* than here */ \\\n" - "/* comment 3 */\n", - format("#define MACRO(V)\\\n" - "V(Rt2) /* one more char */ \\\n" - "V(Rs) /* than here */ \\\n" - "/* comment 3 */\n", - getLLVMStyleWithColumns(40))); - EXPECT_EQ("int i = f(abc, // line 1\n" - " d, // line 2\n" - " // line 3\n" - " b);", - format("int i = f(abc, // line 1\n" - " d, // line 2\n" - " // line 3\n" - " b);", - getLLVMStyleWithColumns(40))); + const auto Style15 = getLLVMStyleWithColumns(15); + const auto Style40 = getLLVMStyleWithColumns(40); + + verifyFormat("#define MACRO(V) \\\n" + " V(Rt2) /* one more char */ \\\n" + " V(Rs) /* than here */ \\\n" + "/* comment 3 */\n", + "#define MACRO(V)\\\n" + "V(Rt2) /* one more char */ \\\n" + "V(Rs) /* than here */ \\\n" + "/* comment 3 */\n", + Style40); + verifyFormat("int i = f(abc, // line 1\n" + " d, // line 2\n" + " // line 3\n" + " b);", + "int i = f(abc, // line 1\n" + " d, // line 2\n" + " // line 3\n" + " b);", + Style40); // Align newly broken trailing comments. - EXPECT_EQ("int ab; // line\n" - "int a; // long\n" - " // long", - format("int ab; // line\n" - "int a; // long long", - getLLVMStyleWithColumns(15))); - EXPECT_EQ("int ab; // line\n" - "int a; // long\n" - " // long\n" - " // long", - format("int ab; // line\n" - "int a; // long long\n" - " // long", - getLLVMStyleWithColumns(15))); - EXPECT_EQ("int ab; // line\n" - "int a; // long\n" - " // long\n" - "pt c; // long", - format("int ab; // line\n" - "int a; // long long\n" - "pt c; // long", - getLLVMStyleWithColumns(15))); - EXPECT_EQ("int ab; // line\n" - "int a; // long\n" - " // long\n" - "\n" - "// long", - format("int ab; // line\n" - "int a; // long long\n" - "\n" - "// long", - getLLVMStyleWithColumns(15))); + verifyFormat("int ab; // line\n" + "int a; // long\n" + " // long", + "int ab; // line\n" + "int a; // long long", + Style15); + verifyFormat("int ab; // line\n" + "int a; // long\n" + " // long\n" + " // long", + "int ab; // line\n" + "int a; // long long\n" + " // long", + Style15); + verifyFormat("int ab; // line\n" + "int a; // long\n" + " // long\n" + "pt c; // long", + "int ab; // line\n" + "int a; // long long\n" + "pt c; // long", + Style15); + verifyFormat("int ab; // line\n" + "int a; // long\n" + " // long\n" + "\n" + "// long", + "int ab; // line\n" + "int a; // long long\n" + "\n" + "// long", + Style15); // Don't align newly broken trailing comments if that would put them over the // column limit. - EXPECT_EQ("int i, j; // line 1\n" - "int k; // line longg\n" - " // long", - format("int i, j; // line 1\n" - "int k; // line longg long", - getLLVMStyleWithColumns(20))); + verifyFormat("int i, j; // line 1\n" + "int k; // line longg\n" + " // long", + "int i, j; // line 1\n" + "int k; // line longg long", + getLLVMStyleWithColumns(20)); // Always align if ColumnLimit = 0 - EXPECT_EQ("int i, j; // line 1\n" - "int k; // line longg long", - format("int i, j; // line 1\n" - "int k; // line longg long", - getLLVMStyleWithColumns(0))); + verifyFormat("int i, j; // line 1\n" + "int k; // line longg long", + "int i, j; // line 1\n" + "int k; // line longg long", + getLLVMStyleWithColumns(0)); // Align comment line sections aligned with the next token with the next // token. - EXPECT_EQ("class A {\n" - "public: // public comment\n" - " // comment about a\n" - " int a;\n" - "};", - format("class A {\n" - "public: // public comment\n" - " // comment about a\n" - " int a;\n" - "};", - getLLVMStyleWithColumns(40))); - EXPECT_EQ("class A {\n" - "public: // public comment 1\n" - " // public comment 2\n" - " // comment 1 about a\n" - " // comment 2 about a\n" - " int a;\n" - "};", - format("class A {\n" - "public: // public comment 1\n" - " // public comment 2\n" - " // comment 1 about a\n" - " // comment 2 about a\n" - " int a;\n" - "};", - getLLVMStyleWithColumns(40))); - EXPECT_EQ("int f(int n) { // comment line 1 on f\n" - " // comment line 2 on f\n" - " // comment line 1 before return\n" - " // comment line 2 before return\n" - " return n; // comment line 1 on return\n" - " // comment line 2 on return\n" - " // comment line 1 after return\n" - "}", - format("int f(int n) { // comment line 1 on f\n" - " // comment line 2 on f\n" - " // comment line 1 before return\n" - " // comment line 2 before return\n" - " return n; // comment line 1 on return\n" - " // comment line 2 on return\n" - " // comment line 1 after return\n" - "}", - getLLVMStyleWithColumns(40))); - EXPECT_EQ("int f(int n) {\n" - " switch (n) { // comment line 1 on switch\n" - " // comment line 2 on switch\n" - " // comment line 1 before case 1\n" - " // comment line 2 before case 1\n" - " case 1: // comment line 1 on case 1\n" - " // comment line 2 on case 1\n" - " // comment line 1 before return 1\n" - " // comment line 2 before return 1\n" - " return 1; // comment line 1 on return 1\n" - " // comment line 2 on return 1\n" - " // comment line 1 before default\n" - " // comment line 2 before default\n" - " default: // comment line 1 on default\n" - " // comment line 2 on default\n" - " // comment line 1 before return 2\n" - " return 2 * f(n - 1); // comment line 1 on return 2\n" - " // comment line 2 on return 2\n" - " // comment line 1 after return\n" - " // comment line 2 after return\n" - " }\n" - "}", - format("int f(int n) {\n" - " switch (n) { // comment line 1 on switch\n" - " // comment line 2 on switch\n" - " // comment line 1 before case 1\n" - " // comment line 2 before case 1\n" - " case 1: // comment line 1 on case 1\n" - " // comment line 2 on case 1\n" - " // comment line 1 before return 1\n" - " // comment line 2 before return 1\n" - " return 1; // comment line 1 on return 1\n" - " // comment line 2 on return 1\n" - " // comment line 1 before default\n" - " // comment line 2 before default\n" - " default: // comment line 1 on default\n" - " // comment line 2 on default\n" - " // comment line 1 before return 2\n" - " return 2 * f(n - 1); // comment line 1 on return 2\n" - " // comment line 2 on return 2\n" - " // comment line 1 after return\n" - " // comment line 2 after return\n" - " }\n" - "}", - getLLVMStyleWithColumns(80))); + verifyFormat("class A {\n" + "public: // public comment\n" + " // comment about a\n" + " int a;\n" + "};", + "class A {\n" + "public: // public comment\n" + " // comment about a\n" + " int a;\n" + "};", + Style40); + verifyFormat("class A {\n" + "public: // public comment 1\n" + " // public comment 2\n" + " // comment 1 about a\n" + " // comment 2 about a\n" + " int a;\n" + "};", + "class A {\n" + "public: // public comment 1\n" + " // public comment 2\n" + " // comment 1 about a\n" + " // comment 2 about a\n" + " int a;\n" + "};", + Style40); + verifyFormat("int f(int n) { // comment line 1 on f\n" + " // comment line 2 on f\n" + " // comment line 1 before return\n" + " // comment line 2 before return\n" + " return n; // comment line 1 on return\n" + " // comment line 2 on return\n" + " // comment line 1 after return\n" + "}", + "int f(int n) { // comment line 1 on f\n" + " // comment line 2 on f\n" + " // comment line 1 before return\n" + " // comment line 2 before return\n" + " return n; // comment line 1 on return\n" + " // comment line 2 on return\n" + " // comment line 1 after return\n" + "}", + Style40); + verifyFormat("int f(int n) {\n" + " switch (n) { // comment line 1 on switch\n" + " // comment line 2 on switch\n" + " // comment line 1 before case 1\n" + " // comment line 2 before case 1\n" + " case 1: // comment line 1 on case 1\n" + " // comment line 2 on case 1\n" + " // comment line 1 before return 1\n" + " // comment line 2 before return 1\n" + " return 1; // comment line 1 on return 1\n" + " // comment line 2 on return 1\n" + " // comment line 1 before default\n" + " // comment line 2 before default\n" + " default: // comment line 1 on default\n" + " // comment line 2 on default\n" + " // comment line 1 before return 2\n" + " return 2 * f(n - 1); // comment line 1 on return 2\n" + " // comment line 2 on return 2\n" + " // comment line 1 after return\n" + " // comment line 2 after return\n" + " }\n" + "}", + "int f(int n) {\n" + " switch (n) { // comment line 1 on switch\n" + " // comment line 2 on switch\n" + " // comment line 1 before case 1\n" + " // comment line 2 before case 1\n" + " case 1: // comment line 1 on case 1\n" + " // comment line 2 on case 1\n" + " // comment line 1 before return 1\n" + " // comment line 2 before return 1\n" + " return 1; // comment line 1 on return 1\n" + " // comment line 2 on return 1\n" + " // comment line 1 before default\n" + " // comment line 2 before default\n" + " default: // comment line 1 on default\n" + " // comment line 2 on default\n" + " // comment line 1 before return 2\n" + " return 2 * f(n - 1); // comment line 1 on return 2\n" + " // comment line 2 on return 2\n" + " // comment line 1 after return\n" + " // comment line 2 after return\n" + " }\n" + "}"); // If all the lines in a sequence of line comments are aligned with the next // token, the first line belongs to the previous token and the other lines // belong to the next token. - EXPECT_EQ("int a; // line about a\n" - "long b;", - format("int a; // line about a\n" - " long b;", - getLLVMStyleWithColumns(80))); - EXPECT_EQ("int a; // line about a\n" - "// line about b\n" - "long b;", - format("int a; // line about a\n" - " // line about b\n" - " long b;", - getLLVMStyleWithColumns(80))); - EXPECT_EQ("int a; // line about a\n" - "// line 1 about b\n" - "// line 2 about b\n" - "long b;", - format("int a; // line about a\n" - " // line 1 about b\n" - " // line 2 about b\n" - " long b;", - getLLVMStyleWithColumns(80))); + verifyFormat("int a; // line about a\n" + "long b;", + "int a; // line about a\n" + " long b;"); + verifyFormat("int a; // line about a\n" + "// line about b\n" + "long b;", + "int a; // line about a\n" + " // line about b\n" + " long b;"); + verifyFormat("int a; // line about a\n" + "// line 1 about b\n" + "// line 2 about b\n" + "long b;", + "int a; // line about a\n" + " // line 1 about b\n" + " // line 2 about b\n" + " long b;"); // Checks an edge case in preprocessor handling. // These comments should *not* be aligned - EXPECT_EQ( - "#if FOO\n" - "#else\n" - "long a; // Line about a\n" - "#endif\n" - "#if BAR\n" - "#else\n" - "long b_long_name; // Line about b\n" - "#endif", - format("#if FOO\n" - "#else\n" - "long a; // Line about a\n" // Previous (bad) behavior - "#endif\n" - "#if BAR\n" - "#else\n" - "long b_long_name; // Line about b\n" - "#endif", - getLLVMStyleWithColumns(80))); + verifyFormat("#if FOO\n" + "#else\n" + "long a; // Line about a\n" + "#endif\n" + "#if BAR\n" + "#else\n" + "long b_long_name; // Line about b\n" + "#endif", + "#if FOO\n" + "#else\n" + "long a; // Line about a\n" // Previous (bad) behavior + "#endif\n" + "#if BAR\n" + "#else\n" + "long b_long_name; // Line about b\n" + "#endif"); // bug 47589 - EXPECT_EQ( - "namespace m {\n\n" - "#define FOO_GLOBAL 0 // Global scope.\n" - "#define FOO_LINKLOCAL 1 // Link-local scope.\n" - "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n" - "#define FOO_UNIQUELOCAL 3 // Unique local\n" - "#define FOO_NODELOCAL 4 // Loopback\n\n" - "} // namespace m", - format("namespace m {\n\n" - "#define FOO_GLOBAL 0 // Global scope.\n" - "#define FOO_LINKLOCAL 1 // Link-local scope.\n" - "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n" - "#define FOO_UNIQUELOCAL 3 // Unique local\n" - "#define FOO_NODELOCAL 4 // Loopback\n\n" - "} // namespace m", - getLLVMStyleWithColumns(80))); + verifyFormat("namespace m {\n\n" + "#define FOO_GLOBAL 0 // Global scope.\n" + "#define FOO_LINKLOCAL 1 // Link-local scope.\n" + "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n" + "#define FOO_UNIQUELOCAL 3 // Unique local\n" + "#define FOO_NODELOCAL 4 // Loopback\n\n" + "} // namespace m", + "namespace m {\n\n" + "#define FOO_GLOBAL 0 // Global scope.\n" + "#define FOO_LINKLOCAL 1 // Link-local scope.\n" + "#define FOO_SITELOCAL 2 // Site-local scope (deprecated).\n" + "#define FOO_UNIQUELOCAL 3 // Unique local\n" + "#define FOO_NODELOCAL 4 // Loopback\n\n" + "} // namespace m"); // https://llvm.org/PR53441 verifyFormat("/* */ //\n" @@ -2980,193 +2972,193 @@ TEST_F(FormatTestComments, AlignTrailingCommentsAcrossEmptyLines) { Style.AlignTrailingComments.OverEmptyLines = 2; // Cannot use verifyFormat here // test::messUp removes all new lines which changes the logic - EXPECT_EQ("#include \"a.h\" // comment\n" - "\n" - "\n" - "\n" - "#include \"ab.h\" // comment\n" - "\n" - "\n" - "#include \"abcdefg.h\" // comment", - format("#include \"a.h\" // comment\n" - "\n" - "\n" - "\n" - "#include \"ab.h\" // comment\n" - "\n" - "\n" - "#include \"abcdefg.h\" // comment", - Style)); + verifyFormat("#include \"a.h\" // comment\n" + "\n" + "\n" + "\n" + "#include \"ab.h\" // comment\n" + "\n" + "\n" + "#include \"abcdefg.h\" // comment", + "#include \"a.h\" // comment\n" + "\n" + "\n" + "\n" + "#include \"ab.h\" // comment\n" + "\n" + "\n" + "#include \"abcdefg.h\" // comment", + Style); Style.MaxEmptyLinesToKeep = 1; Style.AlignTrailingComments.OverEmptyLines = 1; // End of testing OverEmptyLines Style.ColumnLimit = 15; - EXPECT_EQ("int ab; // line\n" - "int a; // long\n" - " // long\n" - "\n" - " // long", - format("int ab; // line\n" - "int a; // long long\n" - "\n" - "// long", - Style)); + verifyFormat("int ab; // line\n" + "int a; // long\n" + " // long\n" + "\n" + " // long", + "int ab; // line\n" + "int a; // long long\n" + "\n" + "// long", + Style); Style.ColumnLimit = 15; - EXPECT_EQ("int ab; // line\n" - "\n" - "int a; // long\n" - " // long", - format("int ab; // line\n" - "\n" - "int a; // long long", - Style)); + verifyFormat("int ab; // line\n" + "\n" + "int a; // long\n" + " // long", + "int ab; // line\n" + "\n" + "int a; // long long", + Style); Style.ColumnLimit = 30; - EXPECT_EQ("int foo = 12345; // comment\n" - "int bar =\n" - " 1234; // This is a very\n" - " // long comment\n" - " // which is wrapped\n" - " // arround.\n" - "\n" - "int x = 2; // Is this still\n" - " // aligned?", - format("int foo = 12345; // comment\n" - "int bar = 1234; // This is a very long comment\n" - " // which is wrapped arround.\n" - "\n" - "int x = 2; // Is this still aligned?", - Style)); + verifyFormat("int foo = 12345; // comment\n" + "int bar =\n" + " 1234; // This is a very\n" + " // long comment\n" + " // which is wrapped\n" + " // arround.\n" + "\n" + "int x = 2; // Is this still\n" + " // aligned?", + "int foo = 12345; // comment\n" + "int bar = 1234; // This is a very long comment\n" + " // which is wrapped arround.\n" + "\n" + "int x = 2; // Is this still aligned?", + Style); Style.ColumnLimit = 35; - EXPECT_EQ("int foo = 12345; // comment\n" - "int bar =\n" - " 1234; // This is a very long\n" - " // comment which is\n" - " // wrapped arround.\n" - "\n" - "int x =\n" - " 2; // Is this still aligned?", - format("int foo = 12345; // comment\n" - "int bar = 1234; // This is a very long comment\n" - " // which is wrapped arround.\n" - "\n" - "int x = 2; // Is this still aligned?", - Style)); + verifyFormat("int foo = 12345; // comment\n" + "int bar =\n" + " 1234; // This is a very long\n" + " // comment which is\n" + " // wrapped arround.\n" + "\n" + "int x =\n" + " 2; // Is this still aligned?", + "int foo = 12345; // comment\n" + "int bar = 1234; // This is a very long comment\n" + " // which is wrapped arround.\n" + "\n" + "int x = 2; // Is this still aligned?", + Style); Style.ColumnLimit = 40; - EXPECT_EQ("int foo = 12345; // comment\n" - "int bar =\n" - " 1234; // This is a very long comment\n" - " // which is wrapped arround.\n" - "\n" - "int x = 2; // Is this still aligned?", - format("int foo = 12345; // comment\n" - "int bar = 1234; // This is a very long comment\n" - " // which is wrapped arround.\n" - "\n" - "int x = 2; // Is this still aligned?", - Style)); + verifyFormat("int foo = 12345; // comment\n" + "int bar =\n" + " 1234; // This is a very long comment\n" + " // which is wrapped arround.\n" + "\n" + "int x = 2; // Is this still aligned?", + "int foo = 12345; // comment\n" + "int bar = 1234; // This is a very long comment\n" + " // which is wrapped arround.\n" + "\n" + "int x = 2; // Is this still aligned?", + Style); Style.ColumnLimit = 45; - EXPECT_EQ("int foo = 12345; // comment\n" - "int bar =\n" - " 1234; // This is a very long comment\n" - " // which is wrapped arround.\n" - "\n" - "int x = 2; // Is this still aligned?", - format("int foo = 12345; // comment\n" - "int bar = 1234; // This is a very long comment\n" - " // which is wrapped arround.\n" - "\n" - "int x = 2; // Is this still aligned?", - Style)); + verifyFormat("int foo = 12345; // comment\n" + "int bar =\n" + " 1234; // This is a very long comment\n" + " // which is wrapped arround.\n" + "\n" + "int x = 2; // Is this still aligned?", + "int foo = 12345; // comment\n" + "int bar = 1234; // This is a very long comment\n" + " // which is wrapped arround.\n" + "\n" + "int x = 2; // Is this still aligned?", + Style); Style.ColumnLimit = 80; - EXPECT_EQ("int a; // line about a\n" - "\n" - "// line about b\n" - "long b;", - format("int a; // line about a\n" - "\n" - " // line about b\n" - " long b;", - Style)); + verifyFormat("int a; // line about a\n" + "\n" + "// line about b\n" + "long b;", + "int a; // line about a\n" + "\n" + " // line about b\n" + " long b;", + Style); Style.ColumnLimit = 80; - EXPECT_EQ("int a; // line about a\n" - "\n" - "// line 1 about b\n" - "// line 2 about b\n" - "long b;", - format("int a; // line about a\n" - "\n" - " // line 1 about b\n" - " // line 2 about b\n" - " long b;", - Style)); + verifyFormat("int a; // line about a\n" + "\n" + "// line 1 about b\n" + "// line 2 about b\n" + "long b;", + "int a; // line about a\n" + "\n" + " // line 1 about b\n" + " // line 2 about b\n" + " long b;", + Style); } TEST_F(FormatTestComments, AlignTrailingCommentsLeave) { FormatStyle Style = getLLVMStyle(); Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Leave; - EXPECT_EQ("int a;// do not touch\n" - "int b; // any comments\n" - "int c; // comment\n" - "int d; // comment", - format("int a;// do not touch\n" - "int b; // any comments\n" - "int c; // comment\n" - "int d; // comment", - Style)); - - EXPECT_EQ("int a; // do not touch\n" - "int b; // any comments\n" - "int c; // comment\n" - "int d;// comment", - format("int a; // do not touch\n" - "int b; // any comments\n" - "int c; // comment\n" - "int d;// comment", - Style)); - - EXPECT_EQ("// do not touch\n" - "int a; // any comments\n" - "\n" - " // comment\n" - "// comment\n" - "\n" - "// comment", - format("// do not touch\n" - "int a; // any comments\n" - "\n" - " // comment\n" - "// comment\n" - "\n" - "// comment", - Style)); - - EXPECT_EQ("// do not touch\n" - "int a; // any comments\n" - "\n" - " // comment\n" - "// comment\n" - "\n" - "// comment", - format("// do not touch\n" - "int a; // any comments\n" - "\n" - "\n" - " // comment\n" - "// comment\n" - "\n" - "\n" - "// comment", - Style)); + verifyFormat("int a;// do not touch\n" + "int b; // any comments\n" + "int c; // comment\n" + "int d; // comment", + "int a;// do not touch\n" + "int b; // any comments\n" + "int c; // comment\n" + "int d; // comment", + Style); + + verifyFormat("int a; // do not touch\n" + "int b; // any comments\n" + "int c; // comment\n" + "int d;// comment", + "int a; // do not touch\n" + "int b; // any comments\n" + "int c; // comment\n" + "int d;// comment", + Style); + + verifyFormat("// do not touch\n" + "int a; // any comments\n" + "\n" + " // comment\n" + "// comment\n" + "\n" + "// comment", + "// do not touch\n" + "int a; // any comments\n" + "\n" + " // comment\n" + "// comment\n" + "\n" + "// comment", + Style); + + verifyFormat("// do not touch\n" + "int a; // any comments\n" + "\n" + " // comment\n" + "// comment\n" + "\n" + "// comment", + "// do not touch\n" + "int a; // any comments\n" + "\n" + "\n" + " // comment\n" + "// comment\n" + "\n" + "\n" + "// comment", + Style); verifyFormat("namespace ns {\n" "int i;\n" @@ -3186,36 +3178,36 @@ TEST_F(FormatTestComments, AlignTrailingCommentsLeave) { // Allow to keep 2 empty lines Style.MaxEmptyLinesToKeep = 2; - EXPECT_EQ("// do not touch\n" - "int a; // any comments\n" - "\n" - "\n" - " // comment\n" - "// comment\n" - "\n" - "// comment", - format("// do not touch\n" - "int a; // any comments\n" - "\n" - "\n" - " // comment\n" - "// comment\n" - "\n" - "// comment", - Style)); + verifyFormat("// do not touch\n" + "int a; // any comments\n" + "\n" + "\n" + " // comment\n" + "// comment\n" + "\n" + "// comment", + "// do not touch\n" + "int a; // any comments\n" + "\n" + "\n" + " // comment\n" + "// comment\n" + "\n" + "// comment", + Style); Style.MaxEmptyLinesToKeep = 1; // Just format comments normally when leaving exceeds the column limit Style.ColumnLimit = 35; - EXPECT_EQ("int foo = 12345; // comment\n" - "int bar =\n" - " 1234; // This is a very long\n" - " // comment which is\n" - " // wrapped arround.", - format("int foo = 12345; // comment\n" - "int bar = 1234; // This is a very long comment\n" - " // which is wrapped arround.", - Style)); + verifyFormat("int foo = 12345; // comment\n" + "int bar =\n" + " 1234; // This is a very long\n" + " // comment which is\n" + " // wrapped arround.", + "int foo = 12345; // comment\n" + "int bar = 1234; // This is a very long comment\n" + " // which is wrapped arround.", + Style); Style = getLLVMStyle(); Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Leave; @@ -3502,171 +3494,171 @@ TEST_F(FormatTestComments, DontAlignOverScope) { } TEST_F(FormatTestComments, AlignsBlockCommentDecorations) { - EXPECT_EQ("/*\n" - " */", - format("/*\n" - "*/")); - EXPECT_EQ("/*\n" - " */", - format("/*\n" - " */")); - EXPECT_EQ("/*\n" - " */", - format("/*\n" - " */")); + verifyFormat("/*\n" + " */", + "/*\n" + "*/"); + verifyFormat("/*\n" + " */", + "/*\n" + " */"); + verifyFormat("/*\n" + " */", + "/*\n" + " */"); // Align a single line. - EXPECT_EQ("/*\n" - " * line */", - format("/*\n" - "* line */")); - EXPECT_EQ("/*\n" - " * line */", - format("/*\n" - " * line */")); - EXPECT_EQ("/*\n" - " * line */", - format("/*\n" - " * line */")); - EXPECT_EQ("/*\n" - " * line */", - format("/*\n" - " * line */")); - EXPECT_EQ("/**\n" - " * line */", - format("/**\n" - "* line */")); - EXPECT_EQ("/**\n" - " * line */", - format("/**\n" - " * line */")); - EXPECT_EQ("/**\n" - " * line */", - format("/**\n" - " * line */")); - EXPECT_EQ("/**\n" - " * line */", - format("/**\n" - " * line */")); - EXPECT_EQ("/**\n" - " * line */", - format("/**\n" - " * line */")); + verifyFormat("/*\n" + " * line */", + "/*\n" + "* line */"); + verifyFormat("/*\n" + " * line */", + "/*\n" + " * line */"); + verifyFormat("/*\n" + " * line */", + "/*\n" + " * line */"); + verifyFormat("/*\n" + " * line */", + "/*\n" + " * line */"); + verifyFormat("/**\n" + " * line */", + "/**\n" + "* line */"); + verifyFormat("/**\n" + " * line */", + "/**\n" + " * line */"); + verifyFormat("/**\n" + " * line */", + "/**\n" + " * line */"); + verifyFormat("/**\n" + " * line */", + "/**\n" + " * line */"); + verifyFormat("/**\n" + " * line */", + "/**\n" + " * line */"); // Align the end '*/' after a line. - EXPECT_EQ("/*\n" - " * line\n" - " */", - format("/*\n" - "* line\n" - "*/")); - EXPECT_EQ("/*\n" - " * line\n" - " */", - format("/*\n" - " * line\n" - " */")); - EXPECT_EQ("/*\n" - " * line\n" - " */", - format("/*\n" - " * line\n" - " */")); + verifyFormat("/*\n" + " * line\n" + " */", + "/*\n" + "* line\n" + "*/"); + verifyFormat("/*\n" + " * line\n" + " */", + "/*\n" + " * line\n" + " */"); + verifyFormat("/*\n" + " * line\n" + " */", + "/*\n" + " * line\n" + " */"); // Align two lines. - EXPECT_EQ("/* line 1\n" - " * line 2 */", - format("/* line 1\n" - " * line 2 */")); - EXPECT_EQ("/* line 1\n" - " * line 2 */", - format("/* line 1\n" - "* line 2 */")); - EXPECT_EQ("/* line 1\n" - " * line 2 */", - format("/* line 1\n" - " * line 2 */")); - EXPECT_EQ("/* line 1\n" - " * line 2 */", - format("/* line 1\n" - " * line 2 */")); - EXPECT_EQ("/* line 1\n" - " * line 2 */", - format("/* line 1\n" - " * line 2 */")); - EXPECT_EQ("int i; /* line 1\n" - " * line 2 */", - format("int i; /* line 1\n" - "* line 2 */")); - EXPECT_EQ("int i; /* line 1\n" - " * line 2 */", - format("int i; /* line 1\n" - " * line 2 */")); - EXPECT_EQ("int i; /* line 1\n" - " * line 2 */", - format("int i; /* line 1\n" - " * line 2 */")); + verifyFormat("/* line 1\n" + " * line 2 */", + "/* line 1\n" + " * line 2 */"); + verifyFormat("/* line 1\n" + " * line 2 */", + "/* line 1\n" + "* line 2 */"); + verifyFormat("/* line 1\n" + " * line 2 */", + "/* line 1\n" + " * line 2 */"); + verifyFormat("/* line 1\n" + " * line 2 */", + "/* line 1\n" + " * line 2 */"); + verifyFormat("/* line 1\n" + " * line 2 */", + "/* line 1\n" + " * line 2 */"); + verifyFormat("int i; /* line 1\n" + " * line 2 */", + "int i; /* line 1\n" + "* line 2 */"); + verifyFormat("int i; /* line 1\n" + " * line 2 */", + "int i; /* line 1\n" + " * line 2 */"); + verifyFormat("int i; /* line 1\n" + " * line 2 */", + "int i; /* line 1\n" + " * line 2 */"); // Align several lines. - EXPECT_EQ("/* line 1\n" - " * line 2\n" - " * line 3 */", - format("/* line 1\n" - " * line 2\n" - "* line 3 */")); - EXPECT_EQ("/* line 1\n" - " * line 2\n" - " * line 3 */", - format("/* line 1\n" - " * line 2\n" - "* line 3 */")); - EXPECT_EQ("/*\n" - "** line 1\n" - "** line 2\n" - "*/", - format("/*\n" - "** line 1\n" - " ** line 2\n" - "*/")); + verifyFormat("/* line 1\n" + " * line 2\n" + " * line 3 */", + "/* line 1\n" + " * line 2\n" + "* line 3 */"); + verifyFormat("/* line 1\n" + " * line 2\n" + " * line 3 */", + "/* line 1\n" + " * line 2\n" + "* line 3 */"); + verifyFormat("/*\n" + "** line 1\n" + "** line 2\n" + "*/", + "/*\n" + "** line 1\n" + " ** line 2\n" + "*/"); // Align with different indent after the decorations. - EXPECT_EQ("/*\n" - " * line 1\n" - " * line 2\n" - " * line 3\n" - " * line 4\n" - " */", - format("/*\n" - "* line 1\n" - " * line 2\n" - " * line 3\n" - "* line 4\n" - "*/")); + verifyFormat("/*\n" + " * line 1\n" + " * line 2\n" + " * line 3\n" + " * line 4\n" + " */", + "/*\n" + "* line 1\n" + " * line 2\n" + " * line 3\n" + "* line 4\n" + "*/"); // Align empty or blank lines. - EXPECT_EQ("/**\n" - " *\n" - " *\n" - " *\n" - " */", - format("/**\n" - "* \n" - " * \n" - " *\n" - "*/")); + verifyFormat("/**\n" + " *\n" + " *\n" + " *\n" + " */", + "/**\n" + "* \n" + " * \n" + " *\n" + "*/"); // Align while breaking and reflowing. - EXPECT_EQ("/*\n" - " * long long long\n" - " * long long\n" - " *\n" - " * long */", - format("/*\n" - " * long long long long\n" - " * long\n" - " *\n" - "* long */", - getLLVMStyleWithColumns(20))); + verifyFormat("/*\n" + " * long long long\n" + " * long long\n" + " *\n" + " * long */", + "/*\n" + " * long long long long\n" + " * long\n" + " *\n" + "* long */", + getLLVMStyleWithColumns(20)); } TEST_F(FormatTestComments, NoCrash_Bug34236) { @@ -3674,110 +3666,110 @@ TEST_F(FormatTestComments, NoCrash_Bug34236) { // https://bugs.llvm.org/show_bug.cgi?id=34236 // Temporarily disable formatting for readability. // clang-format off - EXPECT_EQ( + verifyFormat( "/* */ /*\n" " * a\n" " * b c d*/", - format( "/* */ /*\n" " * a b\n" -" * c d*/", - getLLVMStyleWithColumns(80))); +" * c d*/"); // clang-format on } TEST_F(FormatTestComments, NonTrailingBlockComments) { - verifyFormat("const /** comment comment */ A = B;", - getLLVMStyleWithColumns(40)); + const auto Style40 = getLLVMStyleWithColumns(40); + + verifyFormat("const /** comment comment */ A = B;", Style40); verifyFormat("const /** comment comment comment */ A =\n" " B;", - getLLVMStyleWithColumns(40)); - - EXPECT_EQ("const /** comment comment comment\n" - " comment */\n" - " A = B;", - format("const /** comment comment comment comment */\n" - " A = B;", - getLLVMStyleWithColumns(40))); + Style40); + + verifyFormat("const /** comment comment comment\n" + " comment */\n" + " A = B;", + "const /** comment comment comment comment */\n" + " A = B;", + Style40); } TEST_F(FormatTestComments, PythonStyleComments) { + const auto ProtoStyle20 = getTextProtoStyleWithColumns(20); // Keeps a space after '#'. - EXPECT_EQ("# comment\n" - "key: value", - format("#comment\n" - "key:value", - getTextProtoStyleWithColumns(20))); - EXPECT_EQ("# comment\n" - "key: value", - format("# comment\n" - "key:value", - getTextProtoStyleWithColumns(20))); + verifyFormat("# comment\n" + "key: value", + "#comment\n" + "key:value", + ProtoStyle20); + verifyFormat("# comment\n" + "key: value", + "# comment\n" + "key:value", + ProtoStyle20); // Breaks long comment. - EXPECT_EQ("# comment comment\n" - "# comment\n" - "key: value", - format("# comment comment comment\n" - "key:value", - getTextProtoStyleWithColumns(20))); + verifyFormat("# comment comment\n" + "# comment\n" + "key: value", + "# comment comment comment\n" + "key:value", + ProtoStyle20); // Indents comments. - EXPECT_EQ("data {\n" - " # comment comment\n" - " # comment\n" - " key: value\n" - "}", - format("data {\n" - "# comment comment comment\n" - "key: value}", - getTextProtoStyleWithColumns(20))); - EXPECT_EQ("data {\n" - " # comment comment\n" - " # comment\n" - " key: value\n" - "}", - format("data {# comment comment comment\n" - "key: value}", - getTextProtoStyleWithColumns(20))); + verifyFormat("data {\n" + " # comment comment\n" + " # comment\n" + " key: value\n" + "}", + "data {\n" + "# comment comment comment\n" + "key: value}", + ProtoStyle20); + verifyFormat("data {\n" + " # comment comment\n" + " # comment\n" + " key: value\n" + "}", + "data {# comment comment comment\n" + "key: value}", + ProtoStyle20); // Reflows long comments. - EXPECT_EQ("# comment comment\n" - "# comment comment\n" - "key: value", - format("# comment comment comment\n" - "# comment\n" - "key:value", - getTextProtoStyleWithColumns(20))); + verifyFormat("# comment comment\n" + "# comment comment\n" + "key: value", + "# comment comment comment\n" + "# comment\n" + "key:value", + ProtoStyle20); // Breaks trailing comments. - EXPECT_EQ("k: val # comment\n" - " # comment\n" - "a: 1", - format("k:val#comment comment\n" - "a:1", - getTextProtoStyleWithColumns(20))); - EXPECT_EQ("id {\n" - " k: val # comment\n" - " # comment\n" - " # line line\n" - " a: 1\n" - "}", - format("id {k:val#comment comment\n" - "# line line\n" - "a:1}", - getTextProtoStyleWithColumns(20))); + verifyFormat("k: val # comment\n" + " # comment\n" + "a: 1", + "k:val#comment comment\n" + "a:1", + ProtoStyle20); + verifyFormat("id {\n" + " k: val # comment\n" + " # comment\n" + " # line line\n" + " a: 1\n" + "}", + "id {k:val#comment comment\n" + "# line line\n" + "a:1}", + ProtoStyle20); // Aligns trailing comments. - EXPECT_EQ("k: val # commen1\n" - " # commen2\n" - " # commen3\n" - "# commen4\n" - "a: 1 # commen5\n" - " # commen6\n" - " # commen7", - format("k:val#commen1 commen2\n" - " #commen3\n" - "# commen4\n" - "a:1#commen5 commen6\n" - " #commen7", - getTextProtoStyleWithColumns(20))); + verifyFormat("k: val # commen1\n" + " # commen2\n" + " # commen3\n" + "# commen4\n" + "a: 1 # commen5\n" + " # commen6\n" + " # commen7", + "k:val#commen1 commen2\n" + " #commen3\n" + "# commen4\n" + "a:1#commen5 commen6\n" + " #commen7", + ProtoStyle20); } TEST_F(FormatTestComments, BreaksBeforeTrailingUnbreakableSequence) { @@ -3791,16 +3783,15 @@ TEST_F(FormatTestComments, BreaksBeforeTrailingUnbreakableSequence) { TEST_F(FormatTestComments, ReflowBackslashCrash) { // clang-format off - EXPECT_EQ( + verifyFormat( "// How to run:\n" "// bbbbb run \\\n" "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\n" "// \\ <log_file> -- --output_directory=\"<output_directory>\"", - format( "// How to run:\n" "// bbbbb run \\\n" "// rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr \\\n" -"// <log_file> -- --output_directory=\"<output_directory>\"")); +"// <log_file> -- --output_directory=\"<output_directory>\""); // clang-format on } @@ -3809,136 +3800,135 @@ TEST_F(FormatTestComments, IndentsLongJavadocAnnotatedLines) { Style.ColumnLimit = 60; FormatStyle Style20 = getGoogleStyle(FormatStyle::LK_Java); Style20.ColumnLimit = 20; - EXPECT_EQ( - "/**\n" - " * @param x long long long long long long long long long\n" - " * long\n" - " */", - format("/**\n" - " * @param x long long long long long long long long long long\n" - " */", - Style)); - EXPECT_EQ("/**\n" - " * @param x long long long long long long long long long\n" - " * long long long long long long long long long long\n" - " */", - format("/**\n" - " * @param x long long long long long long long long long " - "long long long long long long long long long long\n" - " */", - Style)); - EXPECT_EQ("/**\n" - " * @param x long long long long long long long long long\n" - " * long long long long long long long long long long\n" - " * long\n" - " */", - format("/**\n" - " * @param x long long long long long long long long long " - "long long long long long long long long long long long\n" - " */", - Style)); - EXPECT_EQ("/**\n" - " * Sentence that\n" - " * should be broken.\n" - " * @param short\n" - " * keep indentation\n" - " */", - format("/**\n" - " * Sentence that should be broken.\n" - " * @param short\n" - " * keep indentation\n" - " */", - Style20)); - - EXPECT_EQ("/**\n" - " * @param l1 long1\n" - " * to break\n" - " * @param l2 long2\n" - " * to break\n" - " */", - format("/**\n" - " * @param l1 long1 to break\n" - " * @param l2 long2 to break\n" - " */", - Style20)); - - EXPECT_EQ("/**\n" - " * @param xx to\n" - " * break\n" - " * no reflow\n" - " */", - format("/**\n" - " * @param xx to break\n" - " * no reflow\n" - " */", - Style20)); - - EXPECT_EQ("/**\n" - " * @param xx to\n" - " * break yes\n" - " * reflow\n" - " */", - format("/**\n" - " * @param xx to break\n" - " * yes reflow\n" - " */", - Style20)); + verifyFormat("/**\n" + " * @param x long long long long long long long long long\n" + " * long\n" + " */", + "/**\n" + " * @param x long long long long long long long long long long\n" + " */", + Style); + verifyFormat("/**\n" + " * @param x long long long long long long long long long\n" + " * long long long long long long long long long long\n" + " */", + "/**\n" + " * @param x long long long long long long long long long " + "long long long long long long long long long long\n" + " */", + Style); + verifyFormat("/**\n" + " * @param x long long long long long long long long long\n" + " * long long long long long long long long long long\n" + " * long\n" + " */", + "/**\n" + " * @param x long long long long long long long long long " + "long long long long long long long long long long long\n" + " */", + Style); + verifyFormat("/**\n" + " * Sentence that\n" + " * should be broken.\n" + " * @param short\n" + " * keep indentation\n" + " */", + "/**\n" + " * Sentence that should be broken.\n" + " * @param short\n" + " * keep indentation\n" + " */", + Style20); + + verifyFormat("/**\n" + " * @param l1 long1\n" + " * to break\n" + " * @param l2 long2\n" + " * to break\n" + " */", + "/**\n" + " * @param l1 long1 to break\n" + " * @param l2 long2 to break\n" + " */", + Style20); + + verifyFormat("/**\n" + " * @param xx to\n" + " * break\n" + " * no reflow\n" + " */", + "/**\n" + " * @param xx to break\n" + " * no reflow\n" + " */", + Style20); + + verifyFormat("/**\n" + " * @param xx to\n" + " * break yes\n" + " * reflow\n" + " */", + "/**\n" + " * @param xx to break\n" + " * yes reflow\n" + " */", + Style20); FormatStyle JSStyle20 = getGoogleStyle(FormatStyle::LK_JavaScript); JSStyle20.ColumnLimit = 20; - EXPECT_EQ("/**\n" - " * @param l1 long1\n" - " * to break\n" - " */", - format("/**\n" - " * @param l1 long1 to break\n" - " */", - JSStyle20)); - EXPECT_EQ("/**\n" - " * @param {l1 long1\n" - " * to break}\n" - " */", - format("/**\n" - " * @param {l1 long1 to break}\n" - " */", - JSStyle20)); + verifyFormat("/**\n" + " * @param l1 long1\n" + " * to break\n" + " */", + "/**\n" + " * @param l1 long1 to break\n" + " */", + JSStyle20); + verifyFormat("/**\n" + " * @param {l1 long1\n" + " * to break}\n" + " */", + "/**\n" + " * @param {l1 long1 to break}\n" + " */", + JSStyle20); } TEST_F(FormatTestComments, SpaceAtLineCommentBegin) { FormatStyle Style = getLLVMStyle(); - StringRef NoTextInComment = " // \n" - "\n" - "void foo() {// \n" - "// \n" - "}"; - - EXPECT_EQ("//\n" - "\n" - "void foo() { //\n" - " //\n" - "}", - format(NoTextInComment, Style)); + constexpr StringRef NoTextInComment(" // \n" + "\n" + "void foo() {// \n" + "// \n" + "}"); + + verifyFormat("//\n" + "\n" + "void foo() { //\n" + " //\n" + "}", + NoTextInComment, Style); Style.SpacesInLineCommentPrefix.Minimum = 0; verifyFormat("//#comment", Style); - EXPECT_EQ("//\n" - "\n" - "void foo() { //\n" - " //\n" - "}", - format(NoTextInComment, Style)); + verifyFormat("//\n" + "\n" + "void foo() { //\n" + " //\n" + "}", + NoTextInComment, Style); Style.SpacesInLineCommentPrefix.Minimum = 5; - EXPECT_EQ("// #comment", format("//#comment", Style)); - EXPECT_EQ("//\n" - "\n" - "void foo() { //\n" - " //\n" - "}", - format(NoTextInComment, Style)); + verifyFormat("// #comment", "//#comment", Style); + verifyFormat("//\n" + "\n" + "void foo() { //\n" + " //\n" + "}", + NoTextInComment, Style); Style = getLLVMStyle(); - StringRef Code = + constexpr StringRef Code( "//Free comment without space\n" "\n" "// Free comment with 3 spaces\n" @@ -4008,226 +3998,232 @@ TEST_F(FormatTestComments, SpaceAtLineCommentBegin) { "//} will not move\n" "\n" "//vv will only move\n" - "//} if the line above does"; - - EXPECT_EQ("// Free comment without space\n" - "\n" - "// Free comment with 3 spaces\n" - "\n" - "/// Free Doxygen without space\n" - "\n" - "/// Free Doxygen with 3 spaces\n" - "\n" - "// 🐉 A nice dragon\n" - "\n" - "//\t abccba\n" - "\n" - "//\\t deffed\n" - "\n" - "// 🐉 Another nice dragon\n" - "\n" - "// \t Three leading spaces following tab\n" - "\n" - "// \\t Three leading spaces following backslash\n" - "\n" - "/// A Doxygen Comment with a nested list:\n" - "/// - Foo\n" - "/// - Bar\n" - "/// - Baz\n" - "/// - End\n" - "/// of the inner list\n" - "/// .\n" - "/// .\n" - "\n" - "namespace Foo {\n" - "bool bar(bool b) {\n" - " bool ret1 = true; ///< Doxygenstyle without space\n" - " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n" - " if (b) {\n" - " // Foo\n" - "\n" - " // In function comment\n" - " ret2 = false;\n" - " } // End of if\n" - "\n" - " // if (ret1) {\n" - " // return ret2;\n" - " // }\n" - "\n" - " // if (ret1) {\n" - " // return ret2;\n" - " // }\n" - "\n" - " return ret1 && ret2;\n" - "}\n" - "} // namespace Foo\n" - "\n" - "namespace Bar {\n" - "int foo();\n" - "} // namespace Bar\n" - "//@Nothing added because of the non ascii char\n" - "\n" - "//@ Nothing removed because of the non ascii char\n" - "\n" - "// Comment to move to the left\n" - "// But not this?\n" - "// @but this\n" - "\n" - "// Comment to move to the right\n" - "//@ this stays\n" - "\n" - "//} will not move\n" - "\n" - "// vv will only move\n" - "// } if the line above does", - format(Code, Style)); + "//} if the line above does"); + + constexpr StringRef Code2( + "// Free comment without space\n" + "\n" + "// Free comment with 3 spaces\n" + "\n" + "/// Free Doxygen without space\n" + "\n" + "/// Free Doxygen with 3 spaces\n" + "\n" + "// 🐉 A nice dragon\n" + "\n" + "//\t abccba\n" + "\n" + "//\\t deffed\n" + "\n" + "// 🐉 Another nice dragon\n" + "\n" + "// \t Three leading spaces following tab\n" + "\n" + "// \\t Three leading spaces following backslash\n" + "\n" + "/// A Doxygen Comment with a nested list:\n" + "/// - Foo\n" + "/// - Bar\n" + "/// - Baz\n" + "/// - End\n" + "/// of the inner list\n" + "/// .\n" + "/// .\n" + "\n" + "namespace Foo {\n" + "bool bar(bool b) {\n" + " bool ret1 = true; ///< Doxygenstyle without space\n" + " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n" + " if (b) {\n" + " // Foo\n" + "\n" + " // In function comment\n" + " ret2 = false;\n" + " } // End of if\n" + "\n" + " // if (ret1) {\n" + " // return ret2;\n" + " // }\n" + "\n" + " // if (ret1) {\n" + " // return ret2;\n" + " // }\n" + "\n" + " return ret1 && ret2;\n" + "}\n" + "} // namespace Foo\n" + "\n" + "namespace Bar {\n" + "int foo();\n" + "} // namespace Bar\n" + "//@Nothing added because of the non ascii char\n" + "\n" + "//@ Nothing removed because of the non ascii char\n" + "\n" + "// Comment to move to the left\n" + "// But not this?\n" + "// @but this\n" + "\n" + "// Comment to move to the right\n" + "//@ this stays\n" + "\n" + "//} will not move\n" + "\n" + "// vv will only move\n" + "// } if the line above does"); + + constexpr StringRef Code3( + "//Free comment without space\n" + "\n" + "//Free comment with 3 spaces\n" + "\n" + "///Free Doxygen without space\n" + "\n" + "///Free Doxygen with 3 spaces\n" + "\n" + "//🐉 A nice dragon\n" + "\n" + "//\t abccba\n" + "\n" + "//\\t deffed\n" + "\n" + "//🐉 Another nice dragon\n" + "\n" + "//\t Three leading spaces following tab\n" + "\n" + "//\\t Three leading spaces following backslash\n" + "\n" + "///A Doxygen Comment with a nested list:\n" + "///- Foo\n" + "///- Bar\n" + "/// - Baz\n" // Here we keep the relative indentation + "/// - End\n" + "/// of the inner list\n" + "/// .\n" + "///.\n" + "\n" + "namespace Foo {\n" + "bool bar(bool b) {\n" + " bool ret1 = true; ///<Doxygenstyle without space\n" + " bool ret2 = true; ///<Doxygenstyle with 3 spaces\n" + " if (b) {\n" + " //Foo\n" + "\n" + " //In function comment\n" + " ret2 = false;\n" + " } //End of if\n" + "\n" + " //if (ret1) {\n" + " // return ret2;\n" + " //}\n" + "\n" + " //if (ret1) {\n" + " // return ret2;\n" + " //}\n" + "\n" + " return ret1 && ret2;\n" + "}\n" + "} //namespace Foo\n" + "\n" + "namespace Bar {\n" + "int foo();\n" + "} //namespace Bar\n" + "//@Nothing added because of the non ascii char\n" + "\n" + "//@ Nothing removed because of the non ascii char\n" + "\n" + "//Comment to move to the left\n" + "//But not this?\n" + "//@but this\n" + "\n" + "//Comment to move to the right\n" + "//@ this stays\n" + "\n" + "//} will not move\n" + "\n" + "//vv will only move\n" + "//} if the line above does"); + + constexpr StringRef Code4( + "// Free comment without space\n" + "\n" + "// Free comment with 3 spaces\n" + "\n" + "/// Free Doxygen without space\n" + "\n" + "/// Free Doxygen with 3 spaces\n" + "\n" + "// 🐉 A nice dragon\n" + "\n" + "//\t abccba\n" + "\n" + "//\\t deffed\n" + "\n" + "// 🐉 Another nice dragon\n" + "\n" + "// \t Three leading spaces following tab\n" + "\n" + "// \\t Three leading spaces following backslash\n" + "\n" + "/// A Doxygen Comment with a nested list:\n" + "/// - Foo\n" + "/// - Bar\n" + "/// - Baz\n" + "/// - End\n" + "/// of the inner list\n" + "/// .\n" + "/// .\n" + "\n" + "namespace Foo {\n" + "bool bar(bool b) {\n" + " bool ret1 = true; ///< Doxygenstyle without space\n" + " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n" + " if (b) {\n" + " // Foo\n" + "\n" + " // In function comment\n" + " ret2 = false;\n" + " } // End of if\n" + "\n" + " // if (ret1) {\n" + " // return ret2;\n" + " // }\n" + "\n" + " // if (ret1) {\n" + " // return ret2;\n" + " // }\n" + "\n" + " return ret1 && ret2;\n" + "}\n" + "} // namespace Foo\n" + "\n" + "namespace Bar {\n" + "int foo();\n" + "} // namespace Bar\n" + "//@Nothing added because of the non ascii char\n" + "\n" + "//@ Nothing removed because of the non ascii char\n" + "\n" + "// Comment to move to the left\n" + "// But not this?\n" + "// @but this\n" + "\n" + "// Comment to move to the right\n" + "//@ this stays\n" + "\n" + "//} will not move\n" + "\n" + "// vv will only move\n" + "// } if the line above does"); + + verifyFormat(Code2, Code, Style); Style.SpacesInLineCommentPrefix = {0, 0}; - EXPECT_EQ("//#comment", format("// #comment", Style)); - EXPECT_EQ("//Free comment without space\n" - "\n" - "//Free comment with 3 spaces\n" - "\n" - "///Free Doxygen without space\n" - "\n" - "///Free Doxygen with 3 spaces\n" - "\n" - "//🐉 A nice dragon\n" - "\n" - "//\t abccba\n" - "\n" - "//\\t deffed\n" - "\n" - "//🐉 Another nice dragon\n" - "\n" - "//\t Three leading spaces following tab\n" - "\n" - "//\\t Three leading spaces following backslash\n" - "\n" - "///A Doxygen Comment with a nested list:\n" - "///- Foo\n" - "///- Bar\n" - "/// - Baz\n" // Here we keep the relative indentation - "/// - End\n" - "/// of the inner list\n" - "/// .\n" - "///.\n" - "\n" - "namespace Foo {\n" - "bool bar(bool b) {\n" - " bool ret1 = true; ///<Doxygenstyle without space\n" - " bool ret2 = true; ///<Doxygenstyle with 3 spaces\n" - " if (b) {\n" - " //Foo\n" - "\n" - " //In function comment\n" - " ret2 = false;\n" - " } //End of if\n" - "\n" - " //if (ret1) {\n" - " // return ret2;\n" - " //}\n" - "\n" - " //if (ret1) {\n" - " // return ret2;\n" - " //}\n" - "\n" - " return ret1 && ret2;\n" - "}\n" - "} //namespace Foo\n" - "\n" - "namespace Bar {\n" - "int foo();\n" - "} //namespace Bar\n" - "//@Nothing added because of the non ascii char\n" - "\n" - "//@ Nothing removed because of the non ascii char\n" - "\n" - "//Comment to move to the left\n" - "//But not this?\n" - "//@but this\n" - "\n" - "//Comment to move to the right\n" - "//@ this stays\n" - "\n" - "//} will not move\n" - "\n" - "//vv will only move\n" - "//} if the line above does", - format(Code, Style)); + verifyFormat("//#comment", "// #comment", Style); + verifyFormat(Code3, Code, Style); Style.SpacesInLineCommentPrefix = {2, -1u}; - EXPECT_EQ("// Free comment without space\n" - "\n" - "// Free comment with 3 spaces\n" - "\n" - "/// Free Doxygen without space\n" - "\n" - "/// Free Doxygen with 3 spaces\n" - "\n" - "// 🐉 A nice dragon\n" - "\n" - "//\t abccba\n" - "\n" - "//\\t deffed\n" - "\n" - "// 🐉 Another nice dragon\n" - "\n" - "// \t Three leading spaces following tab\n" - "\n" - "// \\t Three leading spaces following backslash\n" - "\n" - "/// A Doxygen Comment with a nested list:\n" - "/// - Foo\n" - "/// - Bar\n" - "/// - Baz\n" - "/// - End\n" - "/// of the inner list\n" - "/// .\n" - "/// .\n" - "\n" - "namespace Foo {\n" - "bool bar(bool b) {\n" - " bool ret1 = true; ///< Doxygenstyle without space\n" - " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n" - " if (b) {\n" - " // Foo\n" - "\n" - " // In function comment\n" - " ret2 = false;\n" - " } // End of if\n" - "\n" - " // if (ret1) {\n" - " // return ret2;\n" - " // }\n" - "\n" - " // if (ret1) {\n" - " // return ret2;\n" - " // }\n" - "\n" - " return ret1 && ret2;\n" - "}\n" - "} // namespace Foo\n" - "\n" - "namespace Bar {\n" - "int foo();\n" - "} // namespace Bar\n" - "//@Nothing added because of the non ascii char\n" - "\n" - "//@ Nothing removed because of the non ascii char\n" - "\n" - "// Comment to move to the left\n" - "// But not this?\n" - "// @but this\n" - "\n" - "// Comment to move to the right\n" - "//@ this stays\n" - "\n" - "//} will not move\n" - "\n" - "// vv will only move\n" - "// } if the line above does", - format(Code, Style)); + verifyFormat(Code4, Code, Style); Style = getLLVMStyleWithColumns(20); StringRef WrapCode = "//Lorem ipsum dolor sit amet\n" @@ -4237,485 +4233,276 @@ TEST_F(FormatTestComments, SpaceAtLineCommentBegin) { "void f() {//Hello World\n" "}"; - EXPECT_EQ("// Lorem ipsum dolor\n" - "// sit amet\n" - "\n" - "// Lorem ipsum\n" - "// dolor sit amet\n" - "\n" - "void f() { // Hello\n" - " // World\n" - "}", - format(WrapCode, Style)); + verifyFormat("// Lorem ipsum dolor\n" + "// sit amet\n" + "\n" + "// Lorem ipsum\n" + "// dolor sit amet\n" + "\n" + "void f() { // Hello\n" + " // World\n" + "}", + WrapCode, Style); Style.SpacesInLineCommentPrefix = {0, 0}; - EXPECT_EQ("//Lorem ipsum dolor\n" - "//sit amet\n" - "\n" - "//Lorem ipsum\n" - "//dolor sit amet\n" - "\n" - "void f() { //Hello\n" - " //World\n" - "}", - format(WrapCode, Style)); + verifyFormat("//Lorem ipsum dolor\n" + "//sit amet\n" + "\n" + "//Lorem ipsum\n" + "//dolor sit amet\n" + "\n" + "void f() { //Hello\n" + " //World\n" + "}", + WrapCode, Style); Style.SpacesInLineCommentPrefix = {1, 1}; - EXPECT_EQ("// Lorem ipsum dolor\n" - "// sit amet\n" - "\n" - "// Lorem ipsum\n" - "// dolor sit amet\n" - "\n" - "void f() { // Hello\n" - " // World\n" - "}", - format(WrapCode, Style)); - EXPECT_EQ("// x\n" - "// y", - format("// x\n" - "// y", - Style)); - EXPECT_EQ( + verifyFormat("// Lorem ipsum dolor\n" + "// sit amet\n" + "\n" + "// Lorem ipsum\n" + "// dolor sit amet\n" + "\n" + "void f() { // Hello\n" + " // World\n" + "}", + WrapCode, Style); + verifyFormat("// x\n" + "// y", + "// x\n" + "// y", + Style); + verifyFormat( "// loooooooooooooooooooooooooooooong\n" "// commentcomments\n" "// normal comments", - format("// loooooooooooooooooooooooooooooong commentcomments\n" - "// normal comments", - Style)); + "// loooooooooooooooooooooooooooooong commentcomments\n" + "// normal comments", + Style); Style.SpacesInLineCommentPrefix = {3, 3}; - EXPECT_EQ("// Lorem ipsum\n" - "// dolor sit amet\n" - "\n" - "// Lorem ipsum\n" - "// dolor sit\n" - "// amet\n" - "\n" - "void f() { // Hello\n" - " // World\n" - "}", - format(WrapCode, Style)); + verifyFormat("// Lorem ipsum\n" + "// dolor sit amet\n" + "\n" + "// Lorem ipsum\n" + "// dolor sit\n" + "// amet\n" + "\n" + "void f() { // Hello\n" + " // World\n" + "}", + WrapCode, Style); Style = getLLVMStyleWithColumns(20); - StringRef LotsOfSpaces = "// This are more spaces " - "than the ColumnLimit, what now?\n" - "\n" - "// Comment\n" - "\n" - "// This is a text to split in multiple " - "lines, please. Thank you very much!\n" - "\n" - "// A comment with\n" - "// some indentation that has to be split.\n" - "// And now without"; - EXPECT_EQ("// This are more spaces " - "than the ColumnLimit, what now?\n" - "\n" - "// Comment\n" - "\n" - "// This is a text to\n" - "// split in multiple\n" - "// lines, please.\n" - "// Thank you very\n" - "// much!\n" - "\n" - "// A comment with\n" - "// some\n" - "// indentation\n" - "// that has to be\n" - "// split.\n" - "// And now without", - format(LotsOfSpaces, Style)); + constexpr StringRef LotsOfSpaces( + "// This are more spaces " + "than the ColumnLimit, what now?\n" + "\n" + "// Comment\n" + "\n" + "// This is a text to split in multiple " + "lines, please. Thank you very much!\n" + "\n" + "// A comment with\n" + "// some indentation that has to be split.\n" + "// And now without"); + verifyFormat("// This are more spaces " + "than the ColumnLimit, what now?\n" + "\n" + "// Comment\n" + "\n" + "// This is a text to\n" + "// split in multiple\n" + "// lines, please.\n" + "// Thank you very\n" + "// much!\n" + "\n" + "// A comment with\n" + "// some\n" + "// indentation\n" + "// that has to be\n" + "// split.\n" + "// And now without", + LotsOfSpaces, Style); Style.SpacesInLineCommentPrefix = {0, 0}; - EXPECT_EQ("//This are more\n" - "//spaces than the\n" - "//ColumnLimit, what\n" - "//now?\n" - "\n" - "//Comment\n" - "\n" - "//This is a text to\n" - "//split in multiple\n" - "//lines, please.\n" - "//Thank you very\n" - "//much!\n" - "\n" - "//A comment with\n" - "// some indentation\n" - "// that has to be\n" - "// split.\n" - "//And now without", - format(LotsOfSpaces, Style)); + verifyFormat("//This are more\n" + "//spaces than the\n" + "//ColumnLimit, what\n" + "//now?\n" + "\n" + "//Comment\n" + "\n" + "//This is a text to\n" + "//split in multiple\n" + "//lines, please.\n" + "//Thank you very\n" + "//much!\n" + "\n" + "//A comment with\n" + "// some indentation\n" + "// that has to be\n" + "// split.\n" + "//And now without", + LotsOfSpaces, Style); Style.SpacesInLineCommentPrefix = {3, 3}; - EXPECT_EQ("// This are more\n" - "// spaces than the\n" - "// ColumnLimit,\n" - "// what now?\n" - "\n" - "// Comment\n" - "\n" - "// This is a text\n" - "// to split in\n" - "// multiple lines,\n" - "// please. Thank\n" - "// you very much!\n" - "\n" - "// A comment with\n" - "// some\n" - "// indentation\n" - "// that has to\n" - "// be split.\n" - "// And now without", - format(LotsOfSpaces, Style)); + verifyFormat("// This are more\n" + "// spaces than the\n" + "// ColumnLimit,\n" + "// what now?\n" + "\n" + "// Comment\n" + "\n" + "// This is a text\n" + "// to split in\n" + "// multiple lines,\n" + "// please. Thank\n" + "// you very much!\n" + "\n" + "// A comment with\n" + "// some\n" + "// indentation\n" + "// that has to\n" + "// be split.\n" + "// And now without", + LotsOfSpaces, Style); Style.SpacesInLineCommentPrefix = {30, -1u}; - EXPECT_EQ("// This are more spaces than the " - "ColumnLimit, what now?\n" - "\n" - "// Comment\n" - "\n" - "// This is a text to split in " - "multiple lines, please. Thank you very much!\n" - "\n" - "// A comment with\n" - "// some indentation that has to be " - "split.\n" - "// And now without", - format(LotsOfSpaces, Style)); + verifyFormat( + "// This are more spaces than the " + "ColumnLimit, what now?\n" + "\n" + "// Comment\n" + "\n" + "// This is a text to split in " + "multiple lines, please. Thank you very much!\n" + "\n" + "// A comment with\n" + "// some indentation that has to be " + "split.\n" + "// And now without", + LotsOfSpaces, Style); Style.SpacesInLineCommentPrefix = {2, 4}; - EXPECT_EQ("// A Comment to be\n" - "// moved\n" - "// with indent\n" - "\n" - "// A Comment to be\n" - "// moved\n" - "// with indent\n" - "\n" - "// A Comment to be\n" - "// moved\n" - "// with indent\n" - "\n" - "// A Comment to be\n" - "// moved\n" - "// with indent\n" - "\n" - "// A Comment to\n" - "// be moved\n" - "// with indent\n" - "\n" - "// A Comment to\n" - "// be moved\n" - "// with indent\n" - "\n" - "// A Comment to\n" - "// be moved\n" - "// with indent", - format("//A Comment to be moved\n" - "// with indent\n" - "\n" - "// A Comment to be moved\n" - "// with indent\n" - "\n" - "// A Comment to be moved\n" - "// with indent\n" - "\n" - "// A Comment to be moved\n" - "// with indent\n" - "\n" - "// A Comment to be moved\n" - "// with indent\n" - "\n" - "// A Comment to be moved\n" - "// with indent\n" - "\n" - "// A Comment to be moved\n" - "// with indent", - Style)); + verifyFormat("// A Comment to be\n" + "// moved\n" + "// with indent\n" + "\n" + "// A Comment to be\n" + "// moved\n" + "// with indent\n" + "\n" + "// A Comment to be\n" + "// moved\n" + "// with indent\n" + "\n" + "// A Comment to be\n" + "// moved\n" + "// with indent\n" + "\n" + "// A Comment to\n" + "// be moved\n" + "// with indent\n" + "\n" + "// A Comment to\n" + "// be moved\n" + "// with indent\n" + "\n" + "// A Comment to\n" + "// be moved\n" + "// with indent", + "//A Comment to be moved\n" + "// with indent\n" + "\n" + "// A Comment to be moved\n" + "// with indent\n" + "\n" + "// A Comment to be moved\n" + "// with indent\n" + "\n" + "// A Comment to be moved\n" + "// with indent\n" + "\n" + "// A Comment to be moved\n" + "// with indent\n" + "\n" + "// A Comment to be moved\n" + "// with indent\n" + "\n" + "// A Comment to be moved\n" + "// with indent", + Style); Style.ColumnLimit = 30; - EXPECT_EQ("int i; // A Comment to be\n" - " // moved\n" - " // with indent\n" - "\n" - "int i; // A Comment to be\n" - " // moved\n" - " // with indent\n" - "\n" - "int i; // A Comment to be\n" - " // moved\n" - " // with indent\n" - "\n" - "int i; // A Comment to be\n" - " // moved\n" - " // with indent\n" - "\n" - "int i; // A Comment to be\n" - " // moved\n" - " // with indent\n" - "\n" - "int i; // A Comment to be\n" - " // moved\n" - " // with indent\n" - "\n" - "int i; // A Comment to be\n" - " // moved\n" - " // with indent", - format("int i;//A Comment to be moved\n" - " // with indent\n" - "\n" - "int i;// A Comment to be moved\n" - " // with indent\n" - "\n" - "int i;// A Comment to be moved\n" - " // with indent\n" - "\n" - "int i;// A Comment to be moved\n" - " // with indent\n" - "\n" - "int i;// A Comment to be moved\n" - " // with indent\n" - "\n" - "int i;// A Comment to be moved\n" - " // with indent\n" - "\n" - "int i;// A Comment to be moved\n" - " // with indent", - Style)); + verifyFormat("int i; // A Comment to be\n" + " // moved\n" + " // with indent\n" + "\n" + "int i; // A Comment to be\n" + " // moved\n" + " // with indent\n" + "\n" + "int i; // A Comment to be\n" + " // moved\n" + " // with indent\n" + "\n" + "int i; // A Comment to be\n" + " // moved\n" + " // with indent\n" + "\n" + "int i; // A Comment to be\n" + " // moved\n" + " // with indent\n" + "\n" + "int i; // A Comment to be\n" + " // moved\n" + " // with indent\n" + "\n" + "int i; // A Comment to be\n" + " // moved\n" + " // with indent", + "int i;//A Comment to be moved\n" + " // with indent\n" + "\n" + "int i;// A Comment to be moved\n" + " // with indent\n" + "\n" + "int i;// A Comment to be moved\n" + " // with indent\n" + "\n" + "int i;// A Comment to be moved\n" + " // with indent\n" + "\n" + "int i;// A Comment to be moved\n" + " // with indent\n" + "\n" + "int i;// A Comment to be moved\n" + " // with indent\n" + "\n" + "int i;// A Comment to be moved\n" + " // with indent", + Style); Style = getLLVMStyleWithColumns(0); - EXPECT_EQ("// Free comment without space\n" - "\n" - "// Free comment with 3 spaces\n" - "\n" - "/// Free Doxygen without space\n" - "\n" - "/// Free Doxygen with 3 spaces\n" - "\n" - "// 🐉 A nice dragon\n" - "\n" - "//\t abccba\n" - "\n" - "//\\t deffed\n" - "\n" - "// 🐉 Another nice dragon\n" - "\n" - "// \t Three leading spaces following tab\n" - "\n" - "// \\t Three leading spaces following backslash\n" - "\n" - "/// A Doxygen Comment with a nested list:\n" - "/// - Foo\n" - "/// - Bar\n" - "/// - Baz\n" - "/// - End\n" - "/// of the inner list\n" - "/// .\n" - "/// .\n" - "\n" - "namespace Foo {\n" - "bool bar(bool b) {\n" - " bool ret1 = true; ///< Doxygenstyle without space\n" - " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n" - " if (b) {\n" - " // Foo\n" - "\n" - " // In function comment\n" - " ret2 = false;\n" - " } // End of if\n" - "\n" - " // if (ret1) {\n" - " // return ret2;\n" - " // }\n" - "\n" - " // if (ret1) {\n" - " // return ret2;\n" - " // }\n" - "\n" - " return ret1 && ret2;\n" - "}\n" - "} // namespace Foo\n" - "\n" - "namespace Bar {\n" - "int foo();\n" - "} // namespace Bar\n" - "//@Nothing added because of the non ascii char\n" - "\n" - "//@ Nothing removed because of the non ascii char\n" - "\n" - "// Comment to move to the left\n" - "// But not this?\n" - "// @but this\n" - "\n" - "// Comment to move to the right\n" - "//@ this stays\n" - "\n" - "//} will not move\n" - "\n" - "// vv will only move\n" - "// } if the line above does", - format(Code, Style)); + verifyFormat(Code2, Code, Style); Style.SpacesInLineCommentPrefix = {0, 0}; - EXPECT_EQ("//Free comment without space\n" - "\n" - "//Free comment with 3 spaces\n" - "\n" - "///Free Doxygen without space\n" - "\n" - "///Free Doxygen with 3 spaces\n" - "\n" - "//🐉 A nice dragon\n" - "\n" - "//\t abccba\n" - "\n" - "//\\t deffed\n" - "\n" - "//🐉 Another nice dragon\n" - "\n" - "//\t Three leading spaces following tab\n" - "\n" - "//\\t Three leading spaces following backslash\n" - "\n" - "///A Doxygen Comment with a nested list:\n" - "///- Foo\n" - "///- Bar\n" - "/// - Baz\n" // Here we keep the relative indentation - "/// - End\n" - "/// of the inner list\n" - "/// .\n" - "///.\n" - "\n" - "namespace Foo {\n" - "bool bar(bool b) {\n" - " bool ret1 = true; ///<Doxygenstyle without space\n" - " bool ret2 = true; ///<Doxygenstyle with 3 spaces\n" - " if (b) {\n" - " //Foo\n" - "\n" - " //In function comment\n" - " ret2 = false;\n" - " } //End of if\n" - "\n" - " //if (ret1) {\n" - " // return ret2;\n" - " //}\n" - "\n" - " //if (ret1) {\n" - " // return ret2;\n" - " //}\n" - "\n" - " return ret1 && ret2;\n" - "}\n" - "} //namespace Foo\n" - "\n" - "namespace Bar {\n" - "int foo();\n" - "} //namespace Bar\n" - "//@Nothing added because of the non ascii char\n" - "\n" - "//@ Nothing removed because of the non ascii char\n" - "\n" - "//Comment to move to the left\n" - "//But not this?\n" - "//@but this\n" - "\n" - "//Comment to move to the right\n" - "//@ this stays\n" - "\n" - "//} will not move\n" - "\n" - "//vv will only move\n" - "//} if the line above does", - format(Code, Style)); + verifyFormat(Code3, Code, Style); Style.SpacesInLineCommentPrefix = {2, -1u}; - EXPECT_EQ("// Free comment without space\n" - "\n" - "// Free comment with 3 spaces\n" - "\n" - "/// Free Doxygen without space\n" - "\n" - "/// Free Doxygen with 3 spaces\n" - "\n" - "// 🐉 A nice dragon\n" - "\n" - "//\t abccba\n" - "\n" - "//\\t deffed\n" - "\n" - "// 🐉 Another nice dragon\n" - "\n" - "// \t Three leading spaces following tab\n" - "\n" - "// \\t Three leading spaces following backslash\n" - "\n" - "/// A Doxygen Comment with a nested list:\n" - "/// - Foo\n" - "/// - Bar\n" - "/// - Baz\n" - "/// - End\n" - "/// of the inner list\n" - "/// .\n" - "/// .\n" - "\n" - "namespace Foo {\n" - "bool bar(bool b) {\n" - " bool ret1 = true; ///< Doxygenstyle without space\n" - " bool ret2 = true; ///< Doxygenstyle with 3 spaces\n" - " if (b) {\n" - " // Foo\n" - "\n" - " // In function comment\n" - " ret2 = false;\n" - " } // End of if\n" - "\n" - " // if (ret1) {\n" - " // return ret2;\n" - " // }\n" - "\n" - " // if (ret1) {\n" - " // return ret2;\n" - " // }\n" - "\n" - " return ret1 && ret2;\n" - "}\n" - "} // namespace Foo\n" - "\n" - "namespace Bar {\n" - "int foo();\n" - "} // namespace Bar\n" - "//@Nothing added because of the non ascii char\n" - "\n" - "//@ Nothing removed because of the non ascii char\n" - "\n" - "// Comment to move to the left\n" - "// But not this?\n" - "// @but this\n" - "\n" - "// Comment to move to the right\n" - "//@ this stays\n" - "\n" - "//} will not move\n" - "\n" - "// vv will only move\n" - "// } if the line above does", - format(Code, Style)); + verifyFormat(Code4, Code, Style); } TEST_F(FormatTestComments, SplitCommentIntroducers) { - EXPECT_EQ(R"(// -/\ -/ -)", - format(R"(// -/\ -/ - )", - getLLVMStyleWithColumns(10))); + verifyFormat("//\n" + "/\\\n" + "/\n", + "//\n" + "/\\\n" + "/ \n" + " ", + getLLVMStyleWithColumns(10)); } TEST_F(FormatTestComments, LineCommentsOnStartOfFunctionCall) { diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index ca99940..c046142 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -1119,6 +1119,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsOverloadedOperators) { EXPECT_TOKEN(Tokens[8], tok::amp, TT_PointerOrReference); EXPECT_TOKEN(Tokens[12], tok::amp, TT_PointerOrReference); + Tokens = annotate("::foo::bar& ::foo::bar::operator=(::foo::bar& other);"); + ASSERT_EQ(Tokens.size(), 22u) << Tokens; + EXPECT_TOKEN(Tokens[6], tok::identifier, TT_FunctionDeclarationName); + EXPECT_TOKEN(Tokens[17], tok::amp, TT_PointerOrReference); + Tokens = annotate("SomeLoooooooooooooooooType::Awaitable\n" "SomeLoooooooooooooooooType::operator co_await();"); ASSERT_EQ(Tokens.size(), 11u) << Tokens; @@ -3484,6 +3489,10 @@ TEST_F(TokenAnnotatorTest, StartOfName) { ASSERT_EQ(Tokens.size(), 8u) << Tokens; EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown); // Not StartOfName + Tokens = annotate("int* ::foo::bar;"); + ASSERT_EQ(Tokens.size(), 8u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::identifier, TT_StartOfName); + auto Style = getLLVMStyle(); Style.StatementAttributeLikeMacros.push_back("emit"); Tokens = annotate("emit foo = 0;", Style); |
