summaryrefslogtreecommitdiffstats
path: root/clang/lib/Rewrite
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2008-10-03 23:31:16 +0000
committerChris Lattner <sabre@nondot.org>2008-10-03 23:31:16 +0000
commit7c306a70b971cd0d7ac4c7845fc2b9991ad143b7 (patch)
tree39194d3568cfc99634903a9a4e724dc59cb65a7e /clang/lib/Rewrite
parent3d5a11c62254780f42993d4d7d4ac0247d1e308d (diff)
downloadbcm5719-llvm-7c306a70b971cd0d7ac4c7845fc2b9991ad143b7.tar.gz
bcm5719-llvm-7c306a70b971cd0d7ac4c7845fc2b9991ad143b7.zip
add a new Rewriter::getRewritenText method that returns the text for a range
that includes any edits in the range. llvm-svn: 57037
Diffstat (limited to 'clang/lib/Rewrite')
-rw-r--r--clang/lib/Rewrite/Rewriter.cpp49
1 files changed, 49 insertions, 0 deletions
diff --git a/clang/lib/Rewrite/Rewriter.cpp b/clang/lib/Rewrite/Rewriter.cpp
index f414e1c5f22..9235a91bef5 100644
--- a/clang/lib/Rewrite/Rewriter.cpp
+++ b/clang/lib/Rewrite/Rewriter.cpp
@@ -97,6 +97,55 @@ int Rewriter::getRangeSize(SourceRange Range) const {
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 {
OpenPOWER on IntegriCloud