From 6f16cfb5a661c0a4b13ed18c9246d8830ca118b5 Mon Sep 17 00:00:00 2001 From: Luboš Luňák Date: Fri, 3 May 2013 14:08:48 +0200 Subject: Fix handling range in removeText(). Turns out removeText( SourceRange ) treats it as a token range, so it's not always character-exact if used for removal of only several characters from a token (e.g. an identifier). Change-Id: I0223d52da90f9535d9ef1d48b0f56d69131536c8 --- compilerplugins/clang/plugin.cxx | 15 ++++++++++++++- compilerplugins/clang/plugin.hxx | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/compilerplugins/clang/plugin.cxx b/compilerplugins/clang/plugin.cxx index 75d55782009c..85bd69625b09 100644 --- a/compilerplugins/clang/plugin.cxx +++ b/compilerplugins/clang/plugin.cxx @@ -105,9 +105,22 @@ bool RewritePlugin::insertTextBefore( SourceLocation Loc, StringRef Str ) return true; } +// These two removeText() overloads should not be merged into one, as the SourceRange +// one uses a token range (which counts token length for some reason), so exact length +// given to this overload would not match afterwards. bool RewritePlugin::removeText( SourceLocation Start, unsigned Length, RewriteOptions opts ) { - return removeText( SourceRange( Start, Start.getLocWithOffset( Length )), opts ); + if( opts.RemoveWholeStatement ) + { + SourceRange range( Start, Start.getLocWithOffset( Length - 1 )); + if( !adjustForWholeStatement( &range )) + return reportEditFailure( Start ); + Start = range.getBegin(); + Length = range.getEnd().getRawEncoding() - range.getBegin().getRawEncoding(); + } + if( rewriter.RemoveText( Start, Length, opts )) + return reportEditFailure( Start ); + return true; } bool RewritePlugin::removeText( SourceRange range, RewriteOptions opts ) diff --git a/compilerplugins/clang/plugin.hxx b/compilerplugins/clang/plugin.hxx index f564ca4700b3..da336818cccb 100644 --- a/compilerplugins/clang/plugin.hxx +++ b/compilerplugins/clang/plugin.hxx @@ -101,7 +101,7 @@ class RewritePlugin bool insertTextAfterToken( SourceLocation Loc, StringRef Str ); bool insertTextBefore( SourceLocation Loc, StringRef Str ); bool removeText( SourceLocation Start, unsigned Length, RewriteOptions opts = RewriteOptions()); - // CharSourceRange not supported, unless really needed, as it makes RemoveSemicolon more complicated + // CharSourceRange not supported, unless really needed, as it needs handling for RemoveWholeStatement. //bool removeText( CharSourceRange range, RewriteOptions opts = RewriteOptions()); bool removeText( SourceRange range, RewriteOptions opts = RewriteOptions()); bool replaceText( SourceLocation Start, unsigned OrigLength, StringRef NewStr ); -- cgit v1.2.3