r225352 - clang-format: Understand single-line comments at the end of blocks.

Daniel Jasper djasper at google.com
Wed Jan 7 06:00:11 PST 2015


Author: djasper
Date: Wed Jan  7 08:00:11 2015
New Revision: 225352

URL: http://llvm.org/viewvc/llvm-project?rev=225352&view=rev
Log:
clang-format: Understand single-line comments at the end of blocks.

This prevents clang-format from moving/aligning the comment in the
snippet:
  void f() {
    int i; // some comment
    // some unrelated comment
  }

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=225352&r1=225351&r2=225352&view=diff
==============================================================================
--- cfe/trunk/lib/Format/WhitespaceManager.cpp (original)
+++ cfe/trunk/lib/Format/WhitespaceManager.cpp Wed Jan  7 08:00:11 2015
@@ -163,15 +163,17 @@ void WhitespaceManager::alignTrailingCom
                                   Changes[i - 1].StartOfTokenColumn == 0;
     bool WasAlignedWithStartOfNextLine = false;
     if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
+      unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
+          Changes[i].OriginalWhitespaceRange.getEnd());
       for (unsigned j = i + 1; j != e; ++j) {
         if (Changes[j].Kind != tok::comment) { // Skip over comments.
+          unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
+              Changes[j].OriginalWhitespaceRange.getEnd());
           // The start of the next token was previously aligned with the
           // start of this comment.
           WasAlignedWithStartOfNextLine =
-              (SourceMgr.getSpellingColumnNumber(
-                   Changes[i].OriginalWhitespaceRange.getEnd()) ==
-               SourceMgr.getSpellingColumnNumber(
-                   Changes[j].OriginalWhitespaceRange.getEnd()));
+              CommentColumn == NextColumn ||
+              CommentColumn == NextColumn + Style.IndentWidth;
           break;
         }
       }

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=225352&r1=225351&r2=225352&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Jan  7 08:00:11 2015
@@ -998,6 +998,14 @@ TEST_F(FormatTest, UnderstandsSingleLine
                    " // first\n"
                    "// at start\n"
                    "otherLine();"));
+  EXPECT_EQ("void f() {\n"
+            "  lineWith(); // comment\n"
+            "  // at start\n"
+            "}",
+            format("void              f() {\n"
+                   "  lineWith(); // comment\n"
+                   "  // at start\n"
+                   "}"));
 
   verifyFormat(
       "#define A                                                  \\\n"





More information about the cfe-commits mailing list