r190042 - Address post-commit review comments from r190038.

Daniel Jasper djasper at google.com
Thu Sep 5 03:48:50 PDT 2013


Author: djasper
Date: Thu Sep  5 05:48:50 2013
New Revision: 190042

URL: http://llvm.org/viewvc/llvm-project?rev=190042&view=rev
Log:
Address post-commit review comments from r190038.

Mostly additional comments :-).

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/lib/Format/TokenAnnotator.h

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=190042&r1=190041&r2=190042&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Thu Sep  5 05:48:50 2013
@@ -511,6 +511,21 @@ unsigned ContinuationIndenter::moveState
     bool AvoidBinPacking;
     if (Current.is(tok::l_brace)) {
       if (Current.MatchingParen && Current.BlockKind == BK_Block) {
+        // If this is an l_brace starting a nested block, we pretend (wrt. to
+        // indentation) that we already consumed the corresponding r_brace.
+        // Thus, we remove all ParenStates caused bake fake parentheses that end
+        // at the r_brace. The net effect of this is that we don't indent
+        // relative to the l_brace, if the nested block is the last parameter of
+        // a function. For example, this formats:
+        //
+        //   SomeFunction(a, [] {
+        //     f();  // break
+        //   });
+        //
+        // instead of:
+        //   SomeFunction(a, [] {
+        //                        f();  // break
+        //                      });
         for (unsigned i = 0; i != Current.MatchingParen->FakeRParens; ++i)
           State.Stack.pop_back();
         NewIndent = State.Stack.back().LastSpace;
@@ -565,6 +580,8 @@ unsigned ContinuationIndenter::moveState
   // Remove scopes created by fake parenthesis.
   if (Current.isNot(tok::r_brace) ||
       (Current.MatchingParen && Current.MatchingParen->BlockKind != BK_Block)) {
+    // Don't remove FakeRParens attached to r_braces that surround nested blocks
+    // as they will have been removed early (see above).
     for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
       unsigned VariablePos = State.Stack.back().VariablePos;
       State.Stack.pop_back();

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=190042&r1=190041&r2=190042&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Sep  5 05:48:50 2013
@@ -489,8 +489,8 @@ private:
     ++Count;
   }
 
-  /// \brief Format all children of \p Tok assuming the parent is indented to
-  /// \p ParentIndent.
+  /// \brief If the \p State's next token is an r_brace closing a nested block,
+  /// format the nested block before it.
   ///
   /// Returns \c true if all children could be placed successfully and adapts
   /// \p Penalty as well as \p State. If \p DryRun is false, also directly
@@ -514,7 +514,9 @@ private:
     const FormatToken &LBrace = *State.NextToken->Previous;
     if (LBrace.isNot(tok::l_brace) || LBrace.BlockKind != BK_Block ||
         LBrace.Children.size() == 0)
-      return true; // The previous token does not open a block. Nothing to do.
+      // 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; 
 
     if (NewLine) {
       unsigned ParentIndent = State.Stack.back().Indent;
@@ -734,7 +736,6 @@ public:
     for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
       delete AnnotatedLines[i];
     }
-    AnnotatedLines.clear();
   }
 
   tooling::Replacements format() {

Modified: cfe/trunk/lib/Format/TokenAnnotator.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.h?rev=190042&r1=190041&r2=190042&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.h (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.h Thu Sep  5 05:48:50 2013
@@ -66,7 +66,6 @@ public:
     for (unsigned i = 0, e = Children.size(); i != e; ++i) {
       delete Children[i];
     }
-    Children.clear();
   }
 
   FormatToken *First;





More information about the cfe-commits mailing list