r182577 - Stop aligning trailing comments which are aligned with the next line.

Manuel Klimek klimek at google.com
Thu May 23 04:42:52 PDT 2013


Author: klimek
Date: Thu May 23 06:42:52 2013
New Revision: 182577

URL: http://llvm.org/viewvc/llvm-project?rev=182577&view=rev
Log:
Stop aligning trailing comments which are aligned with the next line.

Previously we would align:
f(); // comment
     // other comment
g();

Even if // other comment was at the start of the line. Now we do not
align trailing comments if they have been already aligned correctly
with the next line.

Thus,
f(); // comment
// other comment
g();
will not be changed, while:
f(); // comment
  // other commment
g();
will lead to the two trailing comments being aligned.

Modified:
    cfe/trunk/lib/Format/WhitespaceManager.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/WhitespaceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/WhitespaceManager.cpp?rev=182577&r1=182576&r2=182577&view=diff
==============================================================================
--- cfe/trunk/lib/Format/WhitespaceManager.cpp (original)
+++ cfe/trunk/lib/Format/WhitespaceManager.cpp Thu May 23 06:42:52 2013
@@ -142,12 +142,26 @@ void WhitespaceManager::alignTrailingCom
     unsigned ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
     Newlines += Changes[i].NewlinesBefore;
     if (Changes[i].IsTrailingComment) {
+      bool WasAlignedWithStartOfNextLine =
+          // A comment on its own line.
+          Changes[i].NewlinesBefore == 1 &&
+          // Not the last line.
+          i + 1 != e &&
+          // The start of the next token was previously aligned with
+          // the start of this comment.
+          (SourceMgr.getSpellingColumnNumber(
+               Changes[i].OriginalWhitespaceRange.getEnd()) ==
+           SourceMgr.getSpellingColumnNumber(
+               Changes[i + 1].OriginalWhitespaceRange.getEnd())) &&
+          // Which is not a comment itself.
+          Changes[i + 1].Kind != tok::comment;
       if (BreakBeforeNext || Newlines > 1 ||
           (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
           // Break the comment sequence if the previous line did not end
           // in a trailing comment.
           (Changes[i].NewlinesBefore == 1 && i > 0 &&
-           !Changes[i - 1].IsTrailingComment)) {
+           !Changes[i - 1].IsTrailingComment) ||
+          WasAlignedWithStartOfNextLine) {
         alignTrailingComments(StartOfSequence, i, MinColumn);
         MinColumn = ChangeMinColumn;
         MaxColumn = ChangeMaxColumn;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=182577&r1=182576&r2=182577&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu May 23 06:42:52 2013
@@ -637,6 +637,19 @@ TEST_F(FormatTest, UnderstandsSingleLine
                    "// test\n"
                    "int a;\n"
                    "});"));
+
+  EXPECT_EQ("lineWith(); // comment\n"
+            "// at start\n"
+            "otherLine();",
+            format("lineWith();   // comment\n"
+                   "// at start\n"
+                   "otherLine();"));
+  EXPECT_EQ("lineWith(); // comment\n"
+            "            // at start\n"
+            "otherLine();",
+            format("lineWith();   // comment\n"
+                   " // at start\n"
+                   "otherLine();"));
 }
 
 TEST_F(FormatTest, CanFormatCommentsLocally) {





More information about the cfe-commits mailing list