aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Format/FormatTestComments.cpp
diff options
context:
space:
mode:
authorBjörn Schäpers <bjoern@hazardy.de>2021-01-29 22:01:42 +0100
committerBjörn Schäpers <bjoern@hazardy.de>2021-02-01 22:48:50 +0100
commit772eb24e00629faaae0244aa0d6d6204542c579b (patch)
tree5f7eb43bf20d5a37662cfa650d7fd57d2c47cdb7 /clang/unittests/Format/FormatTestComments.cpp
parent35f746c17fd0315b235050c0a350fc521f944f20 (diff)
downloadllvm-772eb24e00629faaae0244aa0d6d6204542c579b.zip
llvm-772eb24e00629faaae0244aa0d6d6204542c579b.tar.gz
llvm-772eb24e00629faaae0244aa0d6d6204542c579b.tar.bz2
[clang-format] Add option to control the spaces in a line comment
Differential Revision: https://reviews.llvm.org/D92257
Diffstat (limited to 'clang/unittests/Format/FormatTestComments.cpp')
-rw-r--r--clang/unittests/Format/FormatTestComments.cpp522
1 files changed, 521 insertions, 1 deletions
diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp
index 457e732..108028e 100644
--- a/clang/unittests/Format/FormatTestComments.cpp
+++ b/clang/unittests/Format/FormatTestComments.cpp
@@ -3144,7 +3144,7 @@ TEST_F(FormatTestComments, PythonStyleComments) {
" # commen6\n"
" # commen7",
format("k:val#commen1 commen2\n"
- " # commen3\n"
+ " #commen3\n"
"# commen4\n"
"a:1#commen5 commen6\n"
" #commen7",
@@ -3275,6 +3275,526 @@ TEST_F(FormatTestComments, IndentsLongJavadocAnnotatedLines) {
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));
+
+ Style.SpacesInLineCommentPrefix.Minimum = 0;
+ EXPECT_EQ("//\n"
+ "\n"
+ "void foo() { //\n"
+ " //\n"
+ "}",
+ format(NoTextInComment, Style));
+
+ Style.SpacesInLineCommentPrefix.Minimum = 5;
+ EXPECT_EQ("//\n"
+ "\n"
+ "void foo() { //\n"
+ " //\n"
+ "}",
+ format(NoTextInComment, Style));
+
+ Style = getLLVMStyle();
+ StringRef Code =
+ "//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 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" // Commented out at the beginning of the line
+ "// return ret2;\n"
+ "// }\n"
+ "\n"
+ " //if (ret1) {\n" // Commtented out at the beginning of the content
+ " // return ret2;\n"
+ " //}\n"
+ "\n"
+ " return ret1 && ret2;\n"
+ "}\n"
+ "}\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\n";
+
+ 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 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\n",
+ format(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 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\n",
+ format(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 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\n",
+ format(Code, Style));
+
+ Style = getLLVMStyleWithColumns(20);
+ StringRef WrapCode = "//Lorem ipsum dolor sit amet\n"
+ "\n"
+ "// Lorem ipsum dolor sit amet\n"
+ "\n"
+ "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));
+
+ 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));
+
+ 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));
+
+ 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));
+
+ Style = getLLVMStyleWithColumns(20);
+ StringRef AShitloadOfSpaces = "// 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(AShitloadOfSpaces, 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(AShitloadOfSpaces, 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(AShitloadOfSpaces, 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(AShitloadOfSpaces, 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\n",
+ 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\n",
+ 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\n",
+ 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\n",
+ Style));
+}
+
} // end namespace
} // end namespace format
} // end namespace clang