[cfe-commits] r172092 - /cfe/trunk/lib/Format/Format.cpp

Manuel Klimek klimek at google.com
Thu Jan 10 11:17:33 PST 2013


Author: klimek
Date: Thu Jan 10 13:17:33 2013
New Revision: 172092

URL: http://llvm.org/viewvc/llvm-project?rev=172092&view=rev
Log:
Pull calculation whether a line fits one level up.

This is the next step towards being able to configure multiple unwrapped
lines into one.

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

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=172092&r1=172091&r2=172092&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Jan 10 13:17:33 2013
@@ -59,9 +59,9 @@
 class AnnotatedToken {
 public:
   AnnotatedToken(const FormatToken &FormatTok)
-      : FormatTok(FormatTok), Type(TT_Unknown),
-        ClosesTemplateDeclaration(false), Parent(NULL) {
-  }
+      : FormatTok(FormatTok), Type(TT_Unknown), SpaceRequiredBefore(false),
+        CanBreakBefore(false), MustBreakBefore(false),
+        ClosesTemplateDeclaration(false), Parent(NULL) {}
 
   bool is(tok::TokenKind Kind) const {
     return FormatTok.Tok.is(Kind);
@@ -160,13 +160,13 @@
 
 class UnwrappedLineFormatter {
 public:
-  UnwrappedLineFormatter(
-      const FormatStyle &Style, SourceManager &SourceMgr,
-      const UnwrappedLine &Line, unsigned FirstIndent,
-      LineType CurrentLineType, const AnnotatedToken &RootToken,
-      tooling::Replacements &Replaces, bool StructuralError)
+  UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
+                         const UnwrappedLine &Line, unsigned FirstIndent,
+                         bool FitsOnALine, LineType CurrentLineType,
+                         const AnnotatedToken &RootToken,
+                         tooling::Replacements &Replaces, bool StructuralError)
       : Style(Style), SourceMgr(SourceMgr), Line(Line),
-        FirstIndent(FirstIndent),
+        FirstIndent(FirstIndent), FitsOnALine(FitsOnALine),
         CurrentLineType(CurrentLineType), RootToken(RootToken),
         Replaces(Replaces) {
     Parameters.PenaltyIndentLevel = 15;
@@ -194,25 +194,6 @@
     // The first token has already been indented and thus consumed.
     moveStateToNextToken(State);
 
-    // Check whether the UnwrappedLine can be put onto a single line. If so,
-    // this is bound to be the optimal solution (by definition) and we don't
-    // need to analyze the entire solution space.
-    unsigned Columns = State.Column;
-    bool FitsOnALine = true;
-    const AnnotatedToken *Tok = State.NextToken;
-    while (Tok != NULL) {
-      Columns += (Tok->SpaceRequiredBefore ? 1 : 0) +
-                 Tok->FormatTok.TokenLength;
-      // A special case for the colon of a constructor initializer as this only
-      // needs to be put on a new line if the line needs to be split.
-      if (Columns > Style.ColumnLimit - (Line.InPPDirective ? 1 : 0) ||
-          (Tok->MustBreakBefore && Tok->Type != TT_CtorInitializerColon)) {
-        FitsOnALine = false;
-        break;
-      }
-      Tok = Tok->Children.empty() ? NULL : &Tok->Children[0];
-    }
-
     // Start iterating at 1 as we have correctly formatted of Token #0 above.
     while (State.NextToken != NULL) {
       if (FitsOnALine) {
@@ -566,6 +547,7 @@
   SourceManager &SourceMgr;
   const UnwrappedLine &Line;
   const unsigned FirstIndent;
+  const bool FitsOnALine;
   const LineType CurrentLineType;
   const AnnotatedToken &RootToken;
   tooling::Replacements &Replaces;
@@ -1256,8 +1238,10 @@
       unsigned Indent = formatFirstToken(Annotator.getRootToken(),
                                          TheLine.Level, TheLine.InPPDirective,
                                          PreviousEndOfLineColumn);
+      bool FitsOnALine = fitsOnALine(Annotator.getRootToken(), Indent,
+                                     TheLine.InPPDirective);
       UnwrappedLineFormatter Formatter(
-          Style, SourceMgr, TheLine, Indent,
+          Style, SourceMgr, TheLine, Indent, FitsOnALine,
           Annotator.getLineType(), Annotator.getRootToken(), Replaces,
           StructuralError);
       return Formatter.format();
@@ -1271,6 +1255,29 @@
            1;
   }
 
+  bool fitsOnALine(const AnnotatedToken &RootToken, unsigned Indent,
+                   bool InPPDirective) {
+    // Check whether the UnwrappedLine can be put onto a single line. If so,
+    // this is bound to be the optimal solution (by definition) and we don't
+    // need to analyze the entire solution space.
+    unsigned Columns = Indent + RootToken.FormatTok.TokenLength;
+    bool FitsOnALine = true;
+    const AnnotatedToken *Tok = &RootToken;
+    while (Tok != NULL) {
+      Columns += (Tok->SpaceRequiredBefore ? 1 : 0) +
+                 Tok->FormatTok.TokenLength;
+      // A special case for the colon of a constructor initializer as this only
+      // needs to be put on a new line if the line needs to be split.
+      if (Columns > Style.ColumnLimit - (InPPDirective ? 1 : 0) ||
+          (Tok->MustBreakBefore && Tok->Type != TT_CtorInitializerColon)) {
+        FitsOnALine = false;
+        break;
+      }
+      Tok = Tok->Children.empty() ? NULL : &Tok->Children[0];
+    }
+    return FitsOnALine;
+  }
+
   /// \brief Add a new line and the required indent before the first Token
   /// of the \c UnwrappedLine if there was no structural parsing error.
   /// Returns the indent level of the \c UnwrappedLine.





More information about the cfe-commits mailing list