]> granicus.if.org Git - clang/commitdiff
Fix comment alignment behavior.
authorDaniel Jasper <djasper@google.com>
Wed, 24 Apr 2013 06:33:59 +0000 (06:33 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 24 Apr 2013 06:33:59 +0000 (06:33 +0000)
In the following snippet, clang-format incorrectly aligned the
trailing comment, when only the last line was formatted:

  int aaaaaa; // comment
  int b;
  int c; // Formatting only this line moved this comment.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@180173 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
lib/Format/WhitespaceManager.h
unittests/Format/FormatTest.cpp

index 2538f33f5070dce16a12ebb2375de4c97985641f..0c3f85d124773eb68b1210f6f1251c2f8fc3e333 100644 (file)
@@ -1069,6 +1069,8 @@ public:
         if (TheLine.Last->is(tok::comment))
           Whitespaces.addUntouchableComment(SourceMgr.getSpellingColumnNumber(
               TheLine.Last->FormatTok.Tok.getLocation()) - 1);
+        else
+          Whitespaces.alignComments();
       }
       PreviousLineLastToken = I->Last;
     }
index 252997f6d8d4aef7504e7346e80e7358a1412529..2833e249c426ec065041b10dd70d97f220e3617e 100644 (file)
@@ -66,6 +66,9 @@ public:
 
   void addUntouchableComment(unsigned Column);
 
+  /// \brief Try to align all stashed comments.
+  void alignComments();
+
 private:
   std::string getNewLineText(unsigned NewLines, unsigned Spaces);
 
@@ -84,9 +87,6 @@ private:
   SmallVector<StoredComment, 16> Comments;
   typedef SmallVector<StoredComment, 16>::iterator comment_iterator;
 
-  /// \brief Try to align all stashed comments.
-  void alignComments();
-
   /// \brief Put all the comments between \p I and \p E into \p Column.
   void alignComments(comment_iterator I, comment_iterator E, unsigned Column);
 
index 0095d58c6b73657e54a083b27bdecaa2cfe7e25d..97539d91a61ed673da8d218b42d88e002375ccb2 100644 (file)
@@ -621,6 +621,13 @@ TEST_F(FormatTest, CanFormatCommentsLocally) {
                    "            // line 2\n"
                    "int b;",
                    28, 0, getLLVMStyle()));
+  EXPECT_EQ("int aaaaaa; // comment\n"
+            "int b;\n"
+            "int c; // unrelated comment",
+            format("int aaaaaa; // comment\n"
+                   "int b;\n"
+                   "int   c; // unrelated comment",
+                   31, 0, getLLVMStyle()));
 }
 
 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {