diff --git a/clang/include/clang/Rewrite/Rewriter.h b/clang/include/clang/Rewrite/Rewriter.h index e7ebda491b2a..9513e674649b 100644 --- a/clang/include/clang/Rewrite/Rewriter.h +++ b/clang/include/clang/Rewrite/Rewriter.h @@ -130,6 +130,23 @@ class Rewriter { const LangOptions *LangOpts; std::map RewriteBuffers; 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::iterator buffer_iterator; explicit Rewriter(SourceManager &SM, const LangOptions &LO) @@ -151,11 +168,10 @@ public: /// getRangeSize - Return the size in bytes of the specified range if they /// are in the same file. If not, this returns -1. - /// If AfterInserts is true and if the beginning of range indicates a position - /// where text is inserted, the beginning of range will be after any inserted - /// text at the position. - int getRangeSize(SourceRange Range, bool AfterInserts = false) const; - int getRangeSize(const CharSourceRange &Range, bool AfterInserts=false) const; + int getRangeSize(SourceRange Range, + RewriteOptions opts = RewriteOptions()) const; + int getRangeSize(const CharSourceRange &Range, + RewriteOptions opts = RewriteOptions()) const; /// 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 @@ -195,32 +211,17 @@ public: /// RemoveText - Remove the specified text region. bool RemoveText(SourceLocation Start, unsigned Length, - bool removeLineIfEmpty = false); + RewriteOptions opts = RewriteOptions()); /// \brief Remove the specified text region. - /// - /// \param afterInserts if true the beginning of removal will be after any - /// inserted text at the position. - /// - /// \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); + bool RemoveText(CharSourceRange range, + RewriteOptions opts = RewriteOptions()) { + return RemoveText(range.getBegin(), getRangeSize(range, opts), opts); } /// \brief Remove the specified text region. - /// - /// \param afterInserts if true the beginning of removal will be after any - /// 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); + bool RemoveText(SourceRange range, RewriteOptions opts = RewriteOptions()) { + return RemoveText(range.getBegin(), getRangeSize(range, opts), opts); } /// ReplaceText - This method replaces a range of characters in the input diff --git a/clang/lib/Rewrite/Rewriter.cpp b/clang/lib/Rewrite/Rewriter.cpp index 3d396d43e4dc..5ccf0f332cb8 100644 --- a/clang/lib/Rewrite/Rewriter.cpp +++ b/clang/lib/Rewrite/Rewriter.cpp @@ -117,7 +117,7 @@ void RewriteBuffer::ReplaceText(unsigned OrigOffset, unsigned OrigLength, /// getRangeSize - Return the size in bytes of the specified range if they /// are in the same file. If not, this returns -1. int Rewriter::getRangeSize(const CharSourceRange &Range, - bool AfterInserts) const { + RewriteOptions opts) const { if (!isRewritable(Range.getBegin()) || !isRewritable(Range.getEnd())) return -1; @@ -136,8 +136,8 @@ int Rewriter::getRangeSize(const CharSourceRange &Range, RewriteBuffers.find(StartFileID); if (I != RewriteBuffers.end()) { const RewriteBuffer &RB = I->second; - EndOff = RB.getMappedOffset(EndOff, true); - StartOff = RB.getMappedOffset(StartOff, AfterInserts); + EndOff = RB.getMappedOffset(EndOff, opts.IncludeInsertsAtEndOfRange); + StartOff = RB.getMappedOffset(StartOff, !opts.IncludeInsertsAtBeginOfRange); } @@ -149,8 +149,8 @@ int Rewriter::getRangeSize(const CharSourceRange &Range, return EndOff-StartOff; } -int Rewriter::getRangeSize(SourceRange Range, bool AfterInserts) const { - return getRangeSize(CharSourceRange::getTokenRange(Range), AfterInserts); +int Rewriter::getRangeSize(SourceRange Range, RewriteOptions opts) const { + return getRangeSize(CharSourceRange::getTokenRange(Range), opts); } @@ -243,18 +243,20 @@ bool Rewriter::InsertTextAfterToken(SourceLocation Loc, llvm::StringRef Str) { if (!isRewritable(Loc)) return true; FileID 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); return false; } /// RemoveText - Remove the specified text region. bool Rewriter::RemoveText(SourceLocation Start, unsigned Length, - bool removeLineIfEmpty) { + RewriteOptions opts) { if (!isRewritable(Start)) return true; FileID FID; unsigned StartOffs = getLocationOffsetAndFileID(Start, FID); - getEditBuffer(FID).RemoveText(StartOffs, Length, removeLineIfEmpty); + getEditBuffer(FID).RemoveText(StartOffs, Length, opts.RemoveLineIfEmpty); return false; }