r180173 - Fix comment alignment behavior.

Daniel Jasper djasper at google.com
Tue Apr 23 23:33:59 PDT 2013


Author: djasper
Date: Wed Apr 24 01:33:59 2013
New Revision: 180173

URL: http://llvm.org/viewvc/llvm-project?rev=180173&view=rev
Log:
Fix comment alignment behavior.

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.

Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/lib/Format/WhitespaceManager.h
    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=180173&r1=180172&r2=180173&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Wed Apr 24 01:33:59 2013
@@ -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;
     }

Modified: cfe/trunk/lib/Format/WhitespaceManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/WhitespaceManager.h?rev=180173&r1=180172&r2=180173&view=diff
==============================================================================
--- cfe/trunk/lib/Format/WhitespaceManager.h (original)
+++ cfe/trunk/lib/Format/WhitespaceManager.h Wed Apr 24 01:33:59 2013
@@ -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);
 

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=180173&r1=180172&r2=180173&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Apr 24 01:33:59 2013
@@ -621,6 +621,13 @@ TEST_F(FormatTest, CanFormatCommentsLoca
                    "            // 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) {





More information about the cfe-commits mailing list