[cfe-commits] r173100 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp

Daniel Jasper djasper at google.com
Mon Jan 21 14:49:21 PST 2013


Author: djasper
Date: Mon Jan 21 16:49:20 2013
New Revision: 173100

URL: http://llvm.org/viewvc/llvm-project?rev=173100&view=rev
Log:
Remove "incorrect" aligning of trailing comments.

We used to align trailing comments belong to different things.
Before:
void f() { // some function..
}
int a;     // some variable..

After:
void f() { // some function..
}
int a; // some variable..

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=173100&r1=173099&r2=173100&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Jan 21 16:49:20 2013
@@ -202,7 +202,13 @@
   void replaceWhitespace(const AnnotatedToken &Tok, unsigned NewLines,
                          unsigned Spaces, unsigned WhitespaceStartColumn,
                          const FormatStyle &Style) {
-    if (Tok.Type == TT_LineComment && NewLines < 2 &&
+    // 2+ newlines mean an empty line separating logic scopes.
+    if (NewLines >= 2)
+      alignComments();
+
+    // Align line comments if they are trailing or if they continue other
+    // trailing comments.
+    if (Tok.Type == TT_LineComment &&
         (Tok.Parent != NULL || !Comments.empty())) {
       if (Style.ColumnLimit >=
           Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) {
@@ -215,10 +221,11 @@
                                     Spaces - Tok.FormatTok.TokenLength;
         return;
       }
-    } else if (NewLines == 0 && Tok.Children.empty() &&
-               Tok.Type != TT_LineComment) {
-      alignComments();
     }
+
+    // If this line does not have a trailing comment, align the stored comments.
+    if (Tok.Children.empty() && Tok.Type != TT_LineComment)
+      alignComments();
     storeReplacement(Tok.FormatTok,
                      std::string(NewLines, '\n') + std::string(Spaces, ' '));
   }

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=173100&r1=173099&r2=173100&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jan 21 16:49:20 2013
@@ -404,6 +404,21 @@
                "    // Comment inside a statement.\n"
                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
 
+  EXPECT_EQ("void f() { // This does something ..\n"
+            "}\n"
+            "int a; // This is unrelated",
+            format("void f()    {     // This does something ..\n"
+                   "  }\n"
+                   "int   a;     // This is unrelated"));
+  EXPECT_EQ("void f() { // This does something ..\n"
+            "}          // awesome..\n"
+            "\n"
+            "int a; // This is unrelated",
+            format("void f()    { // This does something ..\n"
+                   "      } // awesome..\n"
+                   " \n"
+                   "int a;    // This is unrelated"));
+
   EXPECT_EQ("int i; // single line trailing comment",
             format("int i;\\\n// single line trailing comment"));
 





More information about the cfe-commits mailing list