Collect the options applicable to the Rewriter methods into a RewriterOptions struct.

llvm-svn: 129430
This commit is contained in:
Argyrios Kyrtzidis 2011-04-13 07:15:11 +00:00
parent 8001850ee8
commit 71c58f3d59
2 changed files with 37 additions and 34 deletions

View File

@ -130,6 +130,23 @@ class Rewriter {
const LangOptions *LangOpts; const LangOptions *LangOpts;
std::map<FileID, RewriteBuffer> RewriteBuffers; std::map<FileID, RewriteBuffer> RewriteBuffers;
public: public:
struct RewriteOptions {
/// \brief Given a source range, true to include previous inserts at the
/// beginning of the range as part of the range itself (true by default).
bool IncludeInsertsAtBeginOfRange;
/// \brief Given a source range, true to include previous inserts at the
/// end of the range as part of the range itself (true by default).
bool IncludeInsertsAtEndOfRange;
/// \brief If true and removing some text leaves a blank line
/// also remove the empty line (false by default).
bool RemoveLineIfEmpty;
RewriteOptions()
: IncludeInsertsAtBeginOfRange(true),
IncludeInsertsAtEndOfRange(true),
RemoveLineIfEmpty(false) { }
};
typedef std::map<FileID, RewriteBuffer>::iterator buffer_iterator; typedef std::map<FileID, RewriteBuffer>::iterator buffer_iterator;
explicit Rewriter(SourceManager &SM, const LangOptions &LO) explicit Rewriter(SourceManager &SM, const LangOptions &LO)
@ -151,11 +168,10 @@ public:
/// getRangeSize - Return the size in bytes of the specified range if they /// getRangeSize - Return the size in bytes of the specified range if they
/// are in the same file. If not, this returns -1. /// are in the same file. If not, this returns -1.
/// If AfterInserts is true and if the beginning of range indicates a position int getRangeSize(SourceRange Range,
/// where text is inserted, the beginning of range will be after any inserted RewriteOptions opts = RewriteOptions()) const;
/// text at the position. int getRangeSize(const CharSourceRange &Range,
int getRangeSize(SourceRange Range, bool AfterInserts = false) const; RewriteOptions opts = RewriteOptions()) const;
int getRangeSize(const CharSourceRange &Range, bool AfterInserts=false) const;
/// getRewrittenText - Return the rewritten form of the text in the specified /// getRewrittenText - 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 /// range. If the start or end of the range was unrewritable or if they are
@ -195,32 +211,17 @@ public:
/// RemoveText - Remove the specified text region. /// RemoveText - Remove the specified text region.
bool RemoveText(SourceLocation Start, unsigned Length, bool RemoveText(SourceLocation Start, unsigned Length,
bool removeLineIfEmpty = false); RewriteOptions opts = RewriteOptions());
/// \brief Remove the specified text region. /// \brief Remove the specified text region.
/// bool RemoveText(CharSourceRange range,
/// \param afterInserts if true the beginning of removal will be after any RewriteOptions opts = RewriteOptions()) {
/// inserted text at the position. return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
///
/// \param removeLineIfEmpty if true and removing the text leaves a blank line
/// also remove the empty line.
bool RemoveText(CharSourceRange range, bool afterInserts = false,
bool removeLineIfEmpty = false) {
return RemoveText(range.getBegin(), getRangeSize(range, afterInserts),
removeLineIfEmpty);
} }
/// \brief Remove the specified text region. /// \brief Remove the specified text region.
/// bool RemoveText(SourceRange range, RewriteOptions opts = RewriteOptions()) {
/// \param afterInserts if true the beginning of removal will be after any return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
/// inserted text at the position.
///
/// \param removeLineIfEmpty if true and removing the text leaves a blank line
/// also remove the empty line.
bool RemoveText(SourceRange range, bool afterInserts = false,
bool removeLineIfEmpty = false) {
return RemoveText(range.getBegin(), getRangeSize(range, afterInserts),
removeLineIfEmpty);
} }
/// ReplaceText - This method replaces a range of characters in the input /// ReplaceText - This method replaces a range of characters in the input

View File

@ -117,7 +117,7 @@ void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength,
/// getRangeSize - Return the size in bytes of the specified range if they /// getRangeSize - Return the size in bytes of the specified range if they
/// are in the same file. If not, this returns -1. /// are in the same file. If not, this returns -1.
int Rewriter::getRangeSize(const CharSourceRange &Range, int Rewriter::getRangeSize(const CharSourceRange &Range,
bool AfterInserts) const { RewriteOptions opts) const {
if (!isRewritable(Range.getBegin()) || if (!isRewritable(Range.getBegin()) ||
!isRewritable(Range.getEnd())) return -1; !isRewritable(Range.getEnd())) return -1;
@ -136,8 +136,8 @@ int Rewriter::getRangeSize(const CharSourceRange &Range,
RewriteBuffers.find(StartFileID); RewriteBuffers.find(StartFileID);
if (I != RewriteBuffers.end()) { if (I != RewriteBuffers.end()) {
const RewriteBuffer &RB = I->second; const RewriteBuffer &RB = I->second;
EndOff = RB.getMappedOffset(EndOff, true); EndOff = RB.getMappedOffset(EndOff, opts.IncludeInsertsAtEndOfRange);
StartOff = RB.getMappedOffset(StartOff, AfterInserts); StartOff = RB.getMappedOffset(StartOff, !opts.IncludeInsertsAtBeginOfRange);
} }
@ -149,8 +149,8 @@ int Rewriter::getRangeSize(const CharSourceRange &Range,
return EndOff-StartOff; return EndOff-StartOff;
} }
int Rewriter::getRangeSize(SourceRange Range, bool AfterInserts) const { int Rewriter::getRangeSize(SourceRange Range, RewriteOptions opts) const {
return getRangeSize(CharSourceRange::getTokenRange(Range), AfterInserts); return getRangeSize(CharSourceRange::getTokenRange(Range), opts);
} }
@ -243,18 +243,20 @@ bool Rewriter::InsertTextAfterToken(SourceLocation Loc, llvm::StringRef Str) {
if (!isRewritable(Loc)) return true; if (!isRewritable(Loc)) return true;
FileID FID; FileID FID;
unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID); unsigned StartOffs = getLocationOffsetAndFileID(Loc, FID);
StartOffs += getRangeSize(SourceRange(Loc, Loc), /*AfterInserts*/true); RewriteOptions rangeOpts;
rangeOpts.IncludeInsertsAtBeginOfRange = false;
StartOffs += getRangeSize(SourceRange(Loc, Loc), rangeOpts);
getEditBuffer(FID).InsertText(StartOffs, Str, /*InsertAfter*/true); getEditBuffer(FID).InsertText(StartOffs, Str, /*InsertAfter*/true);
return false; return false;
} }
/// RemoveText - Remove the specified text region. /// RemoveText - Remove the specified text region.
bool Rewriter::RemoveText(SourceLocation Start, unsigned Length, bool Rewriter::RemoveText(SourceLocation Start, unsigned Length,
bool removeLineIfEmpty) { RewriteOptions opts) {
if (!isRewritable(Start)) return true; if (!isRewritable(Start)) return true;
FileID FID; FileID FID;
unsigned StartOffs = getLocationOffsetAndFileID(Start, FID); unsigned StartOffs = getLocationOffsetAndFileID(Start, FID);
getEditBuffer(FID).RemoveText(StartOffs, Length, removeLineIfEmpty); getEditBuffer(FID).RemoveText(StartOffs, Length, opts.RemoveLineIfEmpty);
return false; return false;
} }