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

Manuel Klimek klimek at google.com
Thu Jan 10 11:49:59 PST 2013


Author: klimek
Date: Thu Jan 10 13:49:59 2013
New Revision: 172098

URL: http://llvm.org/viewvc/llvm-project?rev=172098&view=rev
Log:
Refactoring the outermost structure of the formatter.

This is the last step of pure shuffling stuff around, the next step will
be the actual feature.

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=172098&r1=172097&r2=172098&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Jan 10 13:49:59 2013
@@ -1220,55 +1220,63 @@
     unsigned PreviousEndOfLineColumn = 0;
     for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),
                                               E = UnwrappedLines.end();
-         I != E; ++I)
-      PreviousEndOfLineColumn = formatUnwrappedLine(*I,
-                                                    PreviousEndOfLineColumn);
+         I != E; ++I) {
+      const UnwrappedLine &TheLine = *I;
+      if (touchesRanges(TheLine)) {
+        TokenAnnotator Annotator(TheLine, Style, SourceMgr, Lex);
+        if (!Annotator.annotate())
+          break;
+        unsigned Indent = formatFirstToken(Annotator.getRootToken(),
+                                           TheLine.Level, TheLine.InPPDirective,
+                                           PreviousEndOfLineColumn);
+        bool FitsOnALine = fitsOnALine(Annotator.getRootToken(), Indent,
+                                       TheLine.InPPDirective);
+        UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
+                                         FitsOnALine, Annotator.getLineType(),
+                                         Annotator.getRootToken(), Replaces,
+                                         StructuralError);
+        PreviousEndOfLineColumn = Formatter.format();
+      } else {
+        // If we did not reformat this unwrapped line, the column at the end of
+        // the last token is unchanged - thus, we can calculate the end of the
+        // last token, and return the result.
+        const FormatToken *Last = getLastLine(TheLine);
+        PreviousEndOfLineColumn =
+            SourceMgr.getSpellingColumnNumber(Last->Tok.getLocation()) +
+            Lex.MeasureTokenLength(Last->Tok.getLocation(), SourceMgr,
+                                   Lex.getLangOpts()) -
+            1;
+      }
+    }
     return Replaces;
   }
 
 private:
-  virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
-    UnwrappedLines.push_back(TheLine);
+  const FormatToken *getLastLine(const UnwrappedLine &TheLine) {
+    const FormatToken *Last = &TheLine.RootToken;
+    while (!Last->Children.empty())
+      Last = &Last->Children.back();
+    return Last;
   }
 
-  unsigned formatUnwrappedLine(const UnwrappedLine &TheLine,
-                               unsigned PreviousEndOfLineColumn) {
+  bool touchesRanges(const UnwrappedLine &TheLine) {
     const FormatToken *First = &TheLine.RootToken;
-    const FormatToken *Last = First;
-    while (!Last->Children.empty())
-      Last = &Last->Children.back();
+    const FormatToken *Last = getLastLine(TheLine);
     CharSourceRange LineRange = CharSourceRange::getTokenRange(
                                     First->Tok.getLocation(),
                                     Last->Tok.getLocation());
-
     for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
-      if (SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(),
-                                              Ranges[i].getBegin()) ||
-          SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
-                                              LineRange.getBegin()))
-        continue;
-
-      TokenAnnotator Annotator(TheLine, Style, SourceMgr, Lex);
-      if (!Annotator.annotate())
-        break;
-      unsigned Indent = formatFirstToken(Annotator.getRootToken(),
-                                         TheLine.Level, TheLine.InPPDirective,
-                                         PreviousEndOfLineColumn);
-      bool FitsOnALine = fitsOnALine(Annotator.getRootToken(), Indent,
-                                     TheLine.InPPDirective);
-      UnwrappedLineFormatter Formatter(
-          Style, SourceMgr, TheLine, Indent, FitsOnALine,
-          Annotator.getLineType(), Annotator.getRootToken(), Replaces,
-          StructuralError);
-      return Formatter.format();
+      if (!SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(),
+                                               Ranges[i].getBegin()) &&
+          !SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
+                                               LineRange.getBegin()))
+        return true;
     }
-    // If we did not reformat this unwrapped line, the column at the end of the
-    // last token is unchanged - thus, we can calculate the end of the last
-    // token, and return the result.
-    return SourceMgr.getSpellingColumnNumber(Last->Tok.getLocation()) +
-           Lex.MeasureTokenLength(Last->Tok.getLocation(), SourceMgr,
-                                  Lex.getLangOpts()) -
-           1;
+    return false;
+  }
+
+  virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
+    UnwrappedLines.push_back(TheLine);
   }
 
   bool fitsOnALine(const AnnotatedToken &RootToken, unsigned Indent,





More information about the cfe-commits mailing list