clang-format: fix erroneous replacement in lines entirely made of spaces inside C style comments with \r\n end of line
Mathieu Champlon
m.champlon at free.fr
Mon Jun 15 22:03:10 PDT 2015
Hi,
Attached patch adds a test and a fix for clang-format.
The formatting works as expected but replacements get generated every
time which needlessly touch files when formatting code.
I hope the patch is fine, I created it with git...
Thanks,
MAT.
-------------- next part --------------
diff --git lib/Format/BreakableToken.cpp lib/Format/BreakableToken.cpp
index 66e935a..64e9a7d 100644
--- lib/Format/BreakableToken.cpp
+++ lib/Format/BreakableToken.cpp
@@ -342,8 +342,13 @@ void BreakableBlockComment::adjustWhitespace(unsigned LineIndex,
++EndOfPreviousLine;
// Calculate the start of the non-whitespace text in the current line.
size_t StartOfLine = Lines[LineIndex].find_first_not_of(Blanks);
- if (StartOfLine == StringRef::npos)
+ if (StartOfLine == StringRef::npos) {
StartOfLine = Lines[LineIndex].size();
+ // When a line is entirely made of spaces, remove the trailing \r
+ // from the character count.
+ if (Lines[LineIndex].endswith("\r"))
+ --StartOfLine;
+ }
StringRef Whitespace = Lines[LineIndex].substr(0, StartOfLine);
// Adjust Lines to only contain relevant text.
diff --git unittests/Format/FormatTest.cpp unittests/Format/FormatTest.cpp
index 4c58e21..942f023 100644
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -151,6 +151,13 @@ TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
" f();\n"
"}"));
EXPECT_EQ(0, ReplacementCount);
+ EXPECT_EQ("/*\r\n"
+ "\r\n"
+ "*/\r\n",
+ format("/*\r\n"
+ "\r\n"
+ "*/\r\n"));
+ EXPECT_EQ(0, ReplacementCount);
}
TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
More information about the cfe-commits
mailing list