r257258 - clang-format: Improve selective comment formatting.

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 9 07:56:53 PST 2016


Author: djasper
Date: Sat Jan  9 09:56:53 2016
New Revision: 257258

URL: http://llvm.org/viewvc/llvm-project?rev=257258&view=rev
Log:
clang-format: Improve selective comment formatting.

Starting here:
  int x; // Format this line only.
  int xx; //
  int xxxxx; //

Before:
  int x;     // Format this line only.
  int xx; //
  int xxxxx; //

After:
  int x;  // Format this line only.
  int xx; //
  int xxxxx; //

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

Modified: cfe/trunk/lib/Format/WhitespaceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/WhitespaceManager.cpp?rev=257258&r1=257257&r2=257258&view=diff
==============================================================================
--- cfe/trunk/lib/Format/WhitespaceManager.cpp (original)
+++ cfe/trunk/lib/Format/WhitespaceManager.cpp Sat Jan  9 09:56:53 2016
@@ -55,8 +55,9 @@ void WhitespaceManager::replaceWhitespac
     return;
   Tok.Decision = (Newlines > 0) ? FD_Break : FD_Continue;
   Changes.push_back(
-      Change(true, Tok.WhitespaceRange, IndentLevel, Spaces, StartOfTokenColumn,
-             Newlines, "", "", Tok.Tok.getKind(), InPPDirective && !Tok.IsFirst,
+      Change(/*CreateReplacement=*/true, Tok.WhitespaceRange, IndentLevel,
+             Spaces, StartOfTokenColumn, Newlines, "", "", Tok.Tok.getKind(),
+             InPPDirective && !Tok.IsFirst,
              Tok.is(TT_StartOfName) || Tok.is(TT_FunctionDeclarationName)));
 }
 
@@ -64,11 +65,11 @@ void WhitespaceManager::addUntouchableTo
                                             bool InPPDirective) {
   if (Tok.Finalized)
     return;
-  Changes.push_back(
-      Change(false, Tok.WhitespaceRange, /*IndentLevel=*/0,
-             /*Spaces=*/0, Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
-             Tok.Tok.getKind(), InPPDirective && !Tok.IsFirst,
-             Tok.is(TT_StartOfName) || Tok.is(TT_FunctionDeclarationName)));
+  Changes.push_back(Change(
+      /*CreateReplacement=*/false, Tok.WhitespaceRange, /*IndentLevel=*/0,
+      /*Spaces=*/0, Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
+      Tok.Tok.getKind(), InPPDirective && !Tok.IsFirst,
+      Tok.is(TT_StartOfName) || Tok.is(TT_FunctionDeclarationName)));
 }
 
 void WhitespaceManager::replaceWhitespaceInToken(
@@ -342,6 +343,12 @@ void WhitespaceManager::alignTrailingCom
 
     unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
     unsigned ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
+
+    // If we don't create a replacement for this change, we have to consider
+    // it to be immovable.
+    if (!Changes[i].CreateReplacement)
+      ChangeMaxColumn = ChangeMinColumn;
+
     if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
       ChangeMaxColumn -= 2;
     // If this comment follows an } in column 0, it probably documents the

Modified: cfe/trunk/unittests/Format/FormatTestSelective.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestSelective.cpp?rev=257258&r1=257257&r2=257258&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestSelective.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestSelective.cpp Sat Jan  9 09:56:53 2016
@@ -162,6 +162,13 @@ TEST_F(FormatTestSelective, FormatsComme
                    "// This is\n"
                    "// not formatted.   ",
                    0, 0));
+  EXPECT_EQ("int x;  // Format this line.\n"
+            "int xx; //\n"
+            "int xxxxx; //",
+            format("int x; // Format this line.\n"
+                   "int xx; //\n"
+                   "int xxxxx; //",
+                   0, 0));
 }
 
 TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) {




More information about the cfe-commits mailing list