Skip to content
Snippets Groups Projects
Commit e35ec2b1 authored by Daniel Jasper's avatar Daniel Jasper
Browse files

Make the format scrambler understand line comments.

This allows for writing tests including line comments easier and more readable.
We will need more of those tests in the future and also line comments are
useful to force line breaks in tests.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@170446 91177308-0d34-0410-b5e6-96231b3b80d8
parent a34173e6
No related branches found
No related tags found
No related merge requests found
...@@ -40,22 +40,35 @@ protected: ...@@ -40,22 +40,35 @@ protected:
return format(Code, 0, Code.size(), Style); return format(Code, 0, Code.size(), Style);
} }
void verifyFormat(llvm::StringRef Code) { std::string messUp(llvm::StringRef Code) {
std::string WithoutFormat(Code.str()); std::string MessedUp(Code.str());
for (unsigned i = 0, e = WithoutFormat.size(); i != e; ++i) { bool InComment = false;
if (WithoutFormat[i] == '\n') bool JustReplacedNewline = false;
WithoutFormat[i] = ' '; for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
if (JustReplacedNewline)
MessedUp[i - 1] = '\n';
InComment = true;
} else if (MessedUp[i] != ' ') {
JustReplacedNewline = false;
} else if (MessedUp[i] == '\n') {
if (InComment) {
InComment = false;
} else {
JustReplacedNewline = true;
MessedUp[i] = ' ';
}
}
} }
EXPECT_EQ(Code.str(), format(WithoutFormat)); return MessedUp;
}
void verifyFormat(llvm::StringRef Code) {
EXPECT_EQ(Code.str(), format(messUp(Code)));
} }
void verifyGoogleFormat(llvm::StringRef Code) { void verifyGoogleFormat(llvm::StringRef Code) {
std::string WithoutFormat(Code.str()); EXPECT_EQ(Code.str(), format(messUp(Code), getGoogleStyle()));
for (unsigned i = 0, e = WithoutFormat.size(); i != e; ++i) {
if (WithoutFormat[i] == '\n')
WithoutFormat[i] = ' ';
}
EXPECT_EQ(Code.str(), format(WithoutFormat, getGoogleStyle()));
} }
}; };
...@@ -96,7 +109,9 @@ TEST_F(FormatTest, FormatIfWithoutCompountStatement) { ...@@ -96,7 +109,9 @@ TEST_F(FormatTest, FormatIfWithoutCompountStatement) {
verifyFormat("if (true)\n f();\ng();"); verifyFormat("if (true)\n f();\ng();");
verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
EXPECT_EQ("if (a)\n // comment\n f();", format("if(a)\n// comment\nf();")); verifyFormat("if (a)\n"
" // comment\n"
" f();");
} }
TEST_F(FormatTest, ParseIfElse) { TEST_F(FormatTest, ParseIfElse) {
...@@ -236,21 +251,22 @@ TEST_F(FormatTest, FormatsLabels) { ...@@ -236,21 +251,22 @@ TEST_F(FormatTest, FormatsLabels) {
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
TEST_F(FormatTest, UnderstandsSingleLineComments) { TEST_F(FormatTest, UnderstandsSingleLineComments) {
EXPECT_EQ("// line 1\n// line 2\nvoid f() {\n}\n", verifyFormat("// line 1\n"
format("// line 1\n// line 2\nvoid f() {}\n")); "// line 2\n"
"void f() {\n}\n");
EXPECT_EQ("void f() {\n // Doesn't do anything\n}", verifyFormat("void f() {\n"
format("void f() {\n// Doesn't do anything\n}")); " // Doesn't do anything\n"
"}");
EXPECT_EQ("int i // This is a fancy variable\n = 5;", verifyFormat("int i // This is a fancy variable\n"
format("int i // This is a fancy variable\n= 5;")); " = 5;");
EXPECT_EQ("enum E {\n" verifyFormat("enum E {\n"
" // comment\n" " // comment\n"
" VAL_A, // comment\n" " VAL_A, // comment\n"
" VAL_B\n" " VAL_B\n"
"};", "};");
format("enum E{\n// comment\nVAL_A,// comment\nVAL_B};"));
verifyFormat( verifyFormat(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment