[cfe-commits] r129430 - in /cfe/trunk: include/clang/Rewrite/Rewriter.h lib/Rewrite/Rewriter.cpp
Argyrios Kyrtzidis
akyrtzi at gmail.com
Wed Apr 13 00:15:11 PDT 2011
Author: akirtzidis
Date: Wed Apr 13 02:15:11 2011
New Revision: 129430
URL: http://llvm.org/viewvc/llvm-project?rev=129430&view=rev
Log:
Collect the options applicable to the Rewriter methods into a RewriterOptions struct.
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=129430&r1=129429&r2=129430&view=diff
==============================================================================
--- cfe/trunk/include/clang/Rewrite/Rewriter.h (original)
+++ cfe/trunk/include/clang/Rewrite/Rewriter.h Wed Apr 13 02:15:11 2011
@@ -130,6 +130,23 @@
const LangOptions *LangOpts;
std::map<FileID, RewriteBuffer> 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<FileID, RewriteBuffer>::iterator buffer_iterator;
explicit Rewriter(SourceManager &SM, const LangOptions &LO)
@@ -151,11 +168,10 @@
/// 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 @@
/// 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
Modified: cfe/trunk/lib/Rewrite/Rewriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/Rewriter.cpp?rev=129430&r1=129429&r2=129430&view=diff
==============================================================================
--- cfe/trunk/lib/Rewrite/Rewriter.cpp (original)
+++ cfe/trunk/lib/Rewrite/Rewriter.cpp Wed Apr 13 02:15:11 2011
@@ -117,7 +117,7 @@
/// 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 @@
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 @@
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 @@
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;
}
More information about the cfe-commits
mailing list