]> granicus.if.org Git - clang/commitdiff
Remove "incorrect" aligning of trailing comments.
authorDaniel Jasper <djasper@google.com>
Mon, 21 Jan 2013 22:49:20 +0000 (22:49 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 21 Jan 2013 22:49:20 +0000 (22:49 +0000)
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..

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

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

index 6fb1bc2cb751c97c410b44c056f9a91322524f21..05c96bd38da75d6c8bd0a0d0d3b070b59e9371bb 100644 (file)
@@ -202,7 +202,13 @@ public:
   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 @@ public:
                                     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, ' '));
   }
index c9b255ce4145faaf3a1d7ec8aa8a05f1ff31354b..ba6d7519c6c29a556940972d1fc4bb2547d1b41c 100644 (file)
@@ -404,6 +404,21 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
                "    // 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"));