[cfe-commits] r57037 - in /cfe/trunk: include/clang/Rewrite/Rewriter.h lib/Rewrite/Rewriter.cpp

Chris Lattner sabre at nondot.org
Fri Oct 3 16:31:17 PDT 2008


Author: lattner
Date: Fri Oct  3 18:31:16 2008
New Revision: 57037

URL: http://llvm.org/viewvc/llvm-project?rev=57037&view=rev
Log:
add a new Rewriter::getRewritenText method that returns the text for a range
that includes any edits in the range.

Modified:
    cfe/trunk/include/clang/Rewrite/Rewriter.h
    cfe/trunk/lib/Rewrite/Rewriter.cpp

Modified: cfe/trunk/include/clang/Rewrite/Rewriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Rewrite/Rewriter.h?rev=57037&r1=57036&r2=57037&view=diff

==============================================================================
--- cfe/trunk/include/clang/Rewrite/Rewriter.h (original)
+++ cfe/trunk/include/clang/Rewrite/Rewriter.h Fri Oct  3 18:31:16 2008
@@ -136,6 +136,11 @@
   /// are in the same file.  If not, this returns -1.
   int getRangeSize(SourceRange Range) const;
   
+  /// getRewritenText - Return the rewritten form of the text in the specified
+  /// range.  If the start or end of the range was unrewritable or if they are
+  /// in different buffers, this returns an empty string. 
+  std::string getRewritenText(SourceRange Range) const;
+  
   /// InsertText - Insert the specified string at the specified location in the
   /// original buffer.  This method returns true (and does nothing) if the input
   /// location was not rewritable, false otherwise.

Modified: cfe/trunk/lib/Rewrite/Rewriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/Rewriter.cpp?rev=57037&r1=57036&r2=57037&view=diff

==============================================================================
--- cfe/trunk/lib/Rewrite/Rewriter.cpp (original)
+++ cfe/trunk/lib/Rewrite/Rewriter.cpp Fri Oct  3 18:31:16 2008
@@ -97,6 +97,55 @@
   return EndOff-StartOff;
 }
 
+/// getRewritenText - Return the rewritten form of the text in the specified
+/// range.  If the start or end of the range was unrewritable or if they are
+/// in different buffers, this returns an empty string. 
+///
+/// Note that this method is not particularly efficient.
+///
+std::string Rewriter::getRewritenText(SourceRange Range) const {
+  if (!isRewritable(Range.getBegin()) ||
+      !isRewritable(Range.getEnd()))
+    return "";
+  
+  unsigned StartOff, StartFileID;
+  unsigned EndOff  , EndFileID;
+  StartOff = getLocationOffsetAndFileID(Range.getBegin(), StartFileID);
+  EndOff   = getLocationOffsetAndFileID(Range.getEnd(), EndFileID);
+  
+  if (StartFileID != EndFileID)
+    return ""; // Start and end in different buffers.
+  
+  // If edits have been made to this buffer, the delta between the range may
+  // have changed.
+  std::map<unsigned, RewriteBuffer>::const_iterator I =
+    RewriteBuffers.find(StartFileID);
+  if (I == RewriteBuffers.end()) {
+    // If the buffer hasn't been rewritten, just return the text from the input.
+    const char *Ptr = SourceMgr->getCharacterData(Range.getBegin());
+    
+    // Adjust the end offset to the end of the last token, instead of being the
+    // start of the last token.
+    EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr);
+    return std::string(Ptr, Ptr+EndOff-StartOff);
+  }
+  
+  const RewriteBuffer &RB = I->second;
+  EndOff = RB.getMappedOffset(EndOff, true);
+  StartOff = RB.getMappedOffset(StartOff);
+  
+  // Adjust the end offset to the end of the last token, instead of being the
+  // start of the last token.
+  EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr);
+
+  // Advance the iterators to the right spot, yay for linear time algorithms.
+  RewriteBuffer::iterator Start = RB.begin();
+  std::advance(Start, StartOff);
+  RewriteBuffer::iterator End = Start;
+  std::advance(End, EndOff-StartOff);
+  
+  return std::string(Start, End);
+}
 
 unsigned Rewriter::getLocationOffsetAndFileID(SourceLocation Loc,
                                               unsigned &FileID) const {





More information about the cfe-commits mailing list