Remove trailing whitespace of line comments.
authorDaniel Jasper <djasper@google.com>
Fri, 1 Mar 2013 16:45:59 +0000 (16:45 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 1 Mar 2013 16:45:59 +0000 (16:45 +0000)
This fixed llvm.org/PR15378.

llvm-svn: 176351

clang/lib/Format/Format.cpp
clang/unittests/Format/FormatTest.cpp

index 01813ef6c85957f8363d15cd504eeae4d1575cbb..5fd49157bd4c3f2fdcbb0daef2a5abb787cba097 100644 (file)
@@ -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;
   }
 
index 8b1f69a76e79934ed678526db175949fa66bc1b4..052c852a63f8184cac7f3c543684b5198f299214 100644 (file)
@@ -537,6 +537,13 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
       "    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(