r193051 - clang-format: Fix formatting of nested blocks after comment.

Daniel Jasper djasper at google.com
Sun Oct 20 10:28:33 PDT 2013


Author: djasper
Date: Sun Oct 20 12:28:32 2013
New Revision: 193051

URL: http://llvm.org/viewvc/llvm-project?rev=193051&view=rev
Log:
clang-format: Fix formatting of nested blocks after comment.

Before:
  DEBUG({ // Comment that used to confuse clang-format.
  fdafas();
  });
Before:
  DEBUG({ // Comments are now fine.
    fdafas();
  });

This fixed llvm.org/PR17619.

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

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=193051&r1=193050&r2=193051&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Sun Oct 20 12:28:32 2013
@@ -541,9 +541,10 @@ private:
   /// break or don't break.
   bool formatChildren(LineState &State, bool NewLine, bool DryRun,
                       unsigned &Penalty) {
-    const FormatToken &LBrace = *State.NextToken->Previous;
-    if (LBrace.isNot(tok::l_brace) || LBrace.BlockKind != BK_Block ||
-        LBrace.Children.size() == 0)
+    const FormatToken &Previous = *State.NextToken->Previous;
+    const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
+    if (!LBrace || LBrace->isNot(tok::l_brace) ||
+        LBrace->BlockKind != BK_Block || Previous.Children.size() == 0)
       // The previous token does not open a block. Nothing to do. We don't
       // assert so that we can simply call this function for all tokens.
       return true;
@@ -551,8 +552,8 @@ private:
     if (NewLine) {
       unsigned ParentIndent = State.Stack.back().Indent;
       for (SmallVector<AnnotatedLine *, 1>::const_iterator
-               I = LBrace.Children.begin(),
-               E = LBrace.Children.end();
+               I = Previous.Children.begin(),
+               E = Previous.Children.end();
            I != E; ++I) {
         unsigned Indent =
             ParentIndent + ((*I)->Level - Line.Level - 1) * Style.IndentWidth;
@@ -570,24 +571,24 @@ private:
       return true;
     }
 
-    if (LBrace.Children.size() > 1)
+    if (Previous.Children.size() > 1)
       return false; // Cannot merge multiple statements into a single line.
 
     // We can't put the closing "}" on a line with a trailing comment.
-    if (LBrace.Children[0]->Last->isTrailingComment())
+    if (Previous.Children[0]->Last->isTrailingComment())
       return false;
 
     if (!DryRun) {
       Whitespaces->replaceWhitespace(
-          *LBrace.Children[0]->First,
+          *Previous.Children[0]->First,
           /*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1,
           /*StartOfTokenColumn=*/State.Column, State.Line->InPPDirective);
       UnwrappedLineFormatter Formatter(Indenter, Whitespaces, Style,
-                                       *LBrace.Children[0]);
+                                       *Previous.Children[0]);
       Penalty += Formatter.format(State.Column + 1, DryRun);
     }
 
-    State.Column += 1 + LBrace.Children[0]->Last->TotalLength;
+    State.Column += 1 + Previous.Children[0]->Last->TotalLength;
     return true;
   }
 

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=193051&r1=193050&r2=193051&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sun Oct 20 12:28:32 2013
@@ -2397,6 +2397,12 @@ TEST_F(FormatTest, LayoutNestedBlocks) {
                    "});",
                    getLLVMStyleWithColumns(29)));
   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
+  EXPECT_EQ("DEBUG({ // comment\n"
+            "  int i;\n"
+            "});",
+            format("DEBUG({ // comment\n"
+                   "int  i;\n"
+                   "});"));
   EXPECT_EQ("DEBUG({\n"
             "  int i;\n"
             "\n"





More information about the cfe-commits mailing list