summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/plugin.cxx
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2017-05-18 14:18:58 +0200
committerStephan Bergmann <sbergman@redhat.com>2017-05-18 14:18:58 +0200
commit8584b7f57c87b111526d5cc75cc1c45646ebf278 (patch)
treeb0fb94e76456a6bd7107ae89fcf54e7e265e90e9 /compilerplugins/clang/plugin.cxx
parent5477f7274e4df1210298c0f503a54eabc0f06bfc (diff)
Refuse to rewrite in workdir
...instead of merely getting a warning from PluginHandler::HandleTranslationUnit after the fact. Such automatic rewriting should probably never be what one wants. Change-Id: I3829007224a197ebb4d55d24323b375cbbdf815c
Diffstat (limited to 'compilerplugins/clang/plugin.cxx')
-rw-r--r--compilerplugins/clang/plugin.cxx26
1 files changed, 26 insertions, 0 deletions
diff --git a/compilerplugins/clang/plugin.cxx b/compilerplugins/clang/plugin.cxx
index 9c308b1f8509..3e1111c612ef 100644
--- a/compilerplugins/clang/plugin.cxx
+++ b/compilerplugins/clang/plugin.cxx
@@ -274,6 +274,8 @@ RewritePlugin::RewritePlugin( const InstantiationData& data )
bool RewritePlugin::insertText( SourceLocation Loc, StringRef Str, bool InsertAfter, bool indentNewLines )
{
assert( rewriter );
+ if (wouldRewriteWorkdir(Loc))
+ return false;
if( rewriter->InsertText( Loc, Str, InsertAfter, indentNewLines ))
return reportEditFailure( Loc );
return true;
@@ -282,6 +284,8 @@ bool RewritePlugin::insertText( SourceLocation Loc, StringRef Str, bool InsertAf
bool RewritePlugin::insertTextAfter( SourceLocation Loc, StringRef Str )
{
assert( rewriter );
+ if (wouldRewriteWorkdir(Loc))
+ return false;
if( rewriter->InsertTextAfter( Loc, Str ))
return reportEditFailure( Loc );
return true;
@@ -290,6 +294,8 @@ bool RewritePlugin::insertTextAfter( SourceLocation Loc, StringRef Str )
bool RewritePlugin::insertTextAfterToken( SourceLocation Loc, StringRef Str )
{
assert( rewriter );
+ if (wouldRewriteWorkdir(Loc))
+ return false;
if( rewriter->InsertTextAfterToken( Loc, Str ))
return reportEditFailure( Loc );
return true;
@@ -298,6 +304,8 @@ bool RewritePlugin::insertTextAfterToken( SourceLocation Loc, StringRef Str )
bool RewritePlugin::insertTextBefore( SourceLocation Loc, StringRef Str )
{
assert( rewriter );
+ if (wouldRewriteWorkdir(Loc))
+ return false;
if( rewriter->InsertTextBefore( Loc, Str ))
return reportEditFailure( Loc );
return true;
@@ -317,6 +325,8 @@ bool RewritePlugin::removeText( SourceRange range, RewriteOptions opts )
bool RewritePlugin::removeText( CharSourceRange range, RewriteOptions opts )
{
assert( rewriter );
+ if (wouldRewriteWorkdir(range.getBegin()))
+ return false;
if( rewriter->getRangeSize( range, opts ) == -1 )
return reportEditFailure( range.getBegin());
if( !handler.addRemoval( range.getBegin() ) )
@@ -378,6 +388,8 @@ bool RewritePlugin::adjustRangeForOptions( CharSourceRange* range, RewriteOption
bool RewritePlugin::replaceText( SourceLocation Start, unsigned OrigLength, StringRef NewStr )
{
assert( rewriter );
+ if (wouldRewriteWorkdir(Start))
+ return false;
if( OrigLength != 0 && !handler.addRemoval( Start ) )
{
report( DiagnosticsEngine::Warning, "double code replacement, possible plugin error", Start );
@@ -391,6 +403,8 @@ bool RewritePlugin::replaceText( SourceLocation Start, unsigned OrigLength, Stri
bool RewritePlugin::replaceText( SourceRange range, StringRef NewStr )
{
assert( rewriter );
+ if (wouldRewriteWorkdir(range.getBegin()))
+ return false;
if( rewriter->getRangeSize( range ) == -1 )
return reportEditFailure( range.getBegin());
if( !handler.addRemoval( range.getBegin() ) )
@@ -406,6 +420,8 @@ bool RewritePlugin::replaceText( SourceRange range, StringRef NewStr )
bool RewritePlugin::replaceText( SourceRange range, SourceRange replacementRange )
{
assert( rewriter );
+ if (wouldRewriteWorkdir(range.getBegin()))
+ return false;
if( rewriter->getRangeSize( range ) == -1 )
return reportEditFailure( range.getBegin());
if( !handler.addRemoval( range.getBegin() ) )
@@ -418,6 +434,16 @@ bool RewritePlugin::replaceText( SourceRange range, SourceRange replacementRange
return true;
}
+bool RewritePlugin::wouldRewriteWorkdir(SourceLocation loc) {
+ if (loc.isInvalid() || loc.isMacroID()) {
+ return false;
+ }
+ return
+ compiler.getSourceManager().getFilename(
+ compiler.getSourceManager().getSpellingLoc(loc))
+ .startswith(WORKDIR "/");
+}
+
bool RewritePlugin::reportEditFailure( SourceLocation loc )
{
report( DiagnosticsEngine::Warning, "cannot perform source modification (macro expansion involved?)", loc );