r177739 - Align comments to surrounding unformatted comments.

Daniel Jasper djasper at google.com
Fri Mar 22 09:25:51 PDT 2013


Author: djasper
Date: Fri Mar 22 11:25:51 2013
New Revision: 177739

URL: http://llvm.org/viewvc/llvm-project?rev=177739&view=rev
Log:
Align comments to surrounding unformatted comments.

Before:
int a; // not formatted
// formatting this line only

After:
int a; // not formatted
       // formatting this line only

This makes clang-format stable independent of whether the whole
file or single lines are formatted in most cases.

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=177739&r1=177738&r2=177739&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Fri Mar 22 11:25:51 2013
@@ -141,6 +141,7 @@ public:
           Comment.MinColumn =
               NewLines > 0 ? Spaces : WhitespaceStartColumn + Spaces;
           Comment.MaxColumn = Style.ColumnLimit - Tok.FormatTok.TokenLength;
+          Comment.Untouchable = false;
           Comments.push_back(Comment);
           return;
         }
@@ -199,6 +200,14 @@ public:
     return Replaces;
   }
 
+  void addUntouchableComment(unsigned Column) {
+    StoredComment Comment;
+    Comment.MinColumn = Column;
+    Comment.MaxColumn = Column;
+    Comment.Untouchable = true;
+    Comments.push_back(Comment);
+  }
+
 private:
   /// \brief Finds a common prefix of lines of a block comment to properly
   /// indent (and possibly decorate with '*'s) added lines.
@@ -350,6 +359,7 @@ private:
     unsigned MaxColumn;
     unsigned NewLines;
     unsigned Spaces;
+    bool Untouchable;
   };
   SmallVector<StoredComment, 16> Comments;
   typedef SmallVector<StoredComment, 16>::iterator comment_iterator;
@@ -377,9 +387,11 @@ private:
   /// \brief Put all the comments between \p I and \p E into \p Column.
   void alignComments(comment_iterator I, comment_iterator E, unsigned Column) {
     while (I != E) {
-      unsigned Spaces = I->Spaces + Column - I->MinColumn;
-      storeReplacement(
-          I->Tok, std::string(I->NewLines, '\n') + std::string(Spaces, ' '));
+      if (!I->Untouchable) {
+        unsigned Spaces = I->Spaces + Column - I->MinColumn;
+        storeReplacement(
+            I->Tok, std::string(I->NewLines, '\n') + std::string(Spaces, ' '));
+      }
       ++I;
     }
   }
@@ -1345,6 +1357,9 @@ public:
             SourceMgr.getSpellingColumnNumber(LastLoc) +
             Lex.MeasureTokenLength(LastLoc, SourceMgr, Lex.getLangOpts()) - 1;
         PreviousLineWasTouched = false;
+        if (TheLine.Last->is(tok::comment))
+          Whitespaces.addUntouchableComment(SourceMgr.getSpellingColumnNumber(
+              TheLine.Last->FormatTok.Tok.getLocation()) - 1);
       }
     }
     return Whitespaces.generateReplacements();

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=177739&r1=177738&r2=177739&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Mar 22 11:25:51 2013
@@ -594,6 +594,21 @@ TEST_F(FormatTest, UnderstandsSingleLine
       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81 cols with this comment");
 }
 
+TEST_F(FormatTest, CanFormatCommentsLocally) {
+  EXPECT_EQ("int a;    // comment\n"
+            "int    b; // comment",
+            format("int   a; // comment\n"
+                   "int    b; // comment",
+                   0, 0, getLLVMStyle()));
+  EXPECT_EQ("int   a; // comment\n"
+            "         // line 2\n"
+            "int b;",
+            format("int   a; // comment\n"
+                   "            // line 2\n"
+                   "int b;",
+                   28, 0, getLLVMStyle()));
+}
+
 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
   EXPECT_EQ("// comment", format("// comment  "));
   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",





More information about the cfe-commits mailing list