]> granicus.if.org Git - clang/commitdiff
Stop aligning trailing comments which are aligned with the next line.
authorManuel Klimek <klimek@google.com>
Thu, 23 May 2013 11:42:52 +0000 (11:42 +0000)
committerManuel Klimek <klimek@google.com>
Thu, 23 May 2013 11:42:52 +0000 (11:42 +0000)
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.

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

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

index a75514ffe7541776cdebd840286bb43dbc74ace1..d4be86e586134daf683bc7553d1b47b7eea8a7e2 100644 (file)
@@ -142,12 +142,26 @@ void WhitespaceManager::alignTrailingComments() {
     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;
index 4919a142a83e813517d20caccde79da16ec5e698..e51a8fe8cd3f7a4df3adfbb74b027ee077a274a9 100644 (file)
@@ -637,6 +637,19 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
                    "// 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) {