[Libreoffice-commits] core.git: compilerplugins/clang

Stephan Bergmann sbergman at redhat.com
Thu May 18 12:21:16 UTC 2017


 compilerplugins/clang/plugin.cxx |   26 ++++++++++++++++++++++++++
 compilerplugins/clang/plugin.hxx |    1 +
 2 files changed, 27 insertions(+)

New commits:
commit 8584b7f57c87b111526d5cc75cc1c45646ebf278
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Thu May 18 14:18:58 2017 +0200

    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

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 );
diff --git a/compilerplugins/clang/plugin.hxx b/compilerplugins/clang/plugin.hxx
index 7bb79598e854..413b2ab7cefb 100644
--- a/compilerplugins/clang/plugin.hxx
+++ b/compilerplugins/clang/plugin.hxx
@@ -141,6 +141,7 @@ class RewritePlugin
     private:
         template< typename T > friend class Plugin::Registration;
         enum { isRewriter = true };
+        bool wouldRewriteWorkdir(SourceLocation loc);
         bool reportEditFailure( SourceLocation loc );
         bool adjustRangeForOptions( CharSourceRange* range, RewriteOptions options );
     };


More information about the Libreoffice-commits mailing list