r176351 - Remove trailing whitespace of line comments.
Daniel Jasper
djasper at google.com
Fri Mar 1 08:46:00 PST 2013
Author: djasper
Date: Fri Mar 1 10:45:59 2013
New Revision: 176351
URL: http://llvm.org/viewvc/llvm-project?rev=176351&view=rev
Log:
Remove trailing whitespace of line comments.
This fixed llvm.org/PR15378.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=176351&r1=176350&r2=176351&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Fri Mar 1 10:45:59 2013
@@ -116,20 +116,30 @@ public:
// Align line comments if they are trailing or if they continue other
// trailing comments.
- if (isTrailingComment(Tok) && (Tok.Parent != NULL || !Comments.empty())) {
- if (Style.ColumnLimit >=
- Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) {
- Comments.push_back(StoredComment());
- Comments.back().Tok = Tok.FormatTok;
- Comments.back().Spaces = Spaces;
- Comments.back().NewLines = NewLines;
- if (NewLines == 0)
- Comments.back().MinColumn = WhitespaceStartColumn + Spaces;
- else
- Comments.back().MinColumn = Spaces;
- Comments.back().MaxColumn =
- Style.ColumnLimit - Tok.FormatTok.TokenLength;
- return;
+ if (isTrailingComment(Tok)) {
+ // Remove the comment's trailing whitespace.
+ if (Tok.FormatTok.Tok.getLength() != Tok.FormatTok.TokenLength)
+ Replaces.insert(tooling::Replacement(
+ SourceMgr, Tok.FormatTok.Tok.getLocation().getLocWithOffset(
+ Tok.FormatTok.TokenLength),
+ Tok.FormatTok.Tok.getLength() - Tok.FormatTok.TokenLength, ""));
+
+ // Align comment with other comments.
+ if (Tok.Parent != NULL || !Comments.empty()) {
+ if (Style.ColumnLimit >=
+ Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) {
+ Comments.push_back(StoredComment());
+ Comments.back().Tok = Tok.FormatTok;
+ Comments.back().Spaces = Spaces;
+ Comments.back().NewLines = NewLines;
+ if (NewLines == 0)
+ Comments.back().MinColumn = WhitespaceStartColumn + Spaces;
+ else
+ Comments.back().MinColumn = Spaces;
+ Comments.back().MaxColumn =
+ Style.ColumnLimit - Tok.FormatTok.TokenLength;
+ return;
+ }
}
}
@@ -1017,6 +1027,11 @@ public:
GreaterStashed = true;
}
+ // If we reformat comments, we remove trailing whitespace. Update the length
+ // accordingly.
+ if (FormatTok.Tok.is(tok::comment))
+ FormatTok.TokenLength = Text.rtrim().size();
+
return FormatTok;
}
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=176351&r1=176350&r2=176351&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Mar 1 10:45:59 2013
@@ -537,6 +537,13 @@ TEST_F(FormatTest, UnderstandsSingleLine
" aaaaaaaaaaaaaaaaaaaaaa); // 81 cols with this comment");
}
+TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
+ EXPECT_EQ("// comment", format("// comment "));
+ EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
+ format("int aaaaaaa, bbbbbbb; // comment ",
+ getLLVMStyleWithColumns(33)));
+}
+
TEST_F(FormatTest, UnderstandsMultiLineComments) {
verifyFormat("f(/*test=*/ true);");
EXPECT_EQ(
More information about the cfe-commits
mailing list