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

Pranav Kant pranavk at collabora.co.uk
Thu Dec 21 14:37:58 UTC 2017


 compilerplugins/clang/plugin.cxx        |   14 +++++++++++++-
 compilerplugins/clang/plugin.hxx        |    4 ++++
 compilerplugins/clang/pluginhandler.cxx |   16 +++++++++++++++-
 compilerplugins/clang/pluginhandler.hxx |    2 ++
 4 files changed, 34 insertions(+), 2 deletions(-)

New commits:
commit b39e627be45f847554f11fdac040b6f4da4054ba
Author: Pranav Kant <pranavk at collabora.co.uk>
Date:   Sat Dec 16 14:50:30 2017 +0530

    Allow compiler plugins for online
    
    Change-Id: I8e45936ef5675d531be71496e8894b90eaf2f6e2
    Reviewed-on: https://gerrit.libreoffice.org/46769
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: pranavk <pranavk at collabora.co.uk>

diff --git a/compilerplugins/clang/plugin.cxx b/compilerplugins/clang/plugin.cxx
index e43cec4eb998..2e9e390b6714 100644
--- a/compilerplugins/clang/plugin.cxx
+++ b/compilerplugins/clang/plugin.cxx
@@ -18,6 +18,8 @@
 #include <clang/Basic/FileManager.h>
 #include <clang/Lex/Lexer.h>
 
+#include <llvm/Support/Path.h>
+
 #include "pluginhandler.hxx"
 
 /*
@@ -618,10 +620,20 @@ bool hasPathnamePrefix(StringRef pathname, StringRef prefix)
         [](StringRef p, StringRef a) { return p.startswith(a); });
 }
 
+std::string getAbsolutePath(StringRef path)
+{
+    llvm::SmallString<1024> absPath(path);
+    llvm::sys::fs::make_absolute(absPath);
+    llvm::sys::path::remove_dots(absPath, true);
+    return absPath.str().str();
+}
+
 bool isSamePathname(StringRef pathname, StringRef other)
 {
+    std::string absPathname = getAbsolutePath(pathname);
+    std::string absOther = getAbsolutePath(other);
     return checkPathname(
-        pathname, other, [](StringRef p, StringRef a) { return p == a; });
+        absPathname, absOther, [](StringRef p, StringRef a) { return p == a; });
 }
 
 } // namespace
diff --git a/compilerplugins/clang/plugin.hxx b/compilerplugins/clang/plugin.hxx
index 4560157e4afd..c6f1c7cbd6e8 100644
--- a/compilerplugins/clang/plugin.hxx
+++ b/compilerplugins/clang/plugin.hxx
@@ -79,6 +79,7 @@ protected:
     bool isInUnoIncludeFile(const FunctionDecl*) const;
 
     bool isDebugMode() const { return handler.isDebugMode(); }
+    bool isLOOLMode() const { return handler.isLOOLMode(); }
 
     static bool isUnitTestMode();
 
@@ -228,6 +229,9 @@ void normalizeDotDotInFilePath(std::string&);
 // prefix may also contain backslashes:
 bool hasPathnamePrefix(StringRef pathname, StringRef prefix);
 
+// get the absolute path for a given path
+std::string getAbsolutePath(StringRef path);
+
 // Same as pathname == other, except on Windows, where pathname and other may
 // also contain backslashes:
 bool isSamePathname(StringRef pathname, StringRef other);
diff --git a/compilerplugins/clang/pluginhandler.cxx b/compilerplugins/clang/pluginhandler.cxx
index 433bd0a9efad..150bc4d1ef4d 100644
--- a/compilerplugins/clang/pluginhandler.cxx
+++ b/compilerplugins/clang/pluginhandler.cxx
@@ -18,6 +18,7 @@
 #include <clang/Frontend/CompilerInstance.h>
 #include <clang/Frontend/FrontendPluginRegistry.h>
 #include <clang/Lex/PPCallbacks.h>
+
 #include <stdio.h>
 
 #if defined _WIN32
@@ -116,6 +117,8 @@ void PluginHandler::handleOption( const std::string& option )
         unitTestMode = true;
     else if (option == "debug")
         debugMode = true;
+    else if ( option.substr(0, 15) == "lool-base-path=" )
+        loolBasePath = option.substr(15);
     else
         report( DiagnosticsEngine::Fatal, "unknown option %0" ) << option;
 }
@@ -193,7 +196,7 @@ bool PluginHandler::checkIgnoreLocation(SourceLocation loc)
     if( compiler.getSourceManager().isInSystemHeader( expansionLoc ))
         return true;
     const char* bufferName = compiler.getSourceManager().getPresumedLoc( expansionLoc ).getFilename();
-    if (bufferName == NULL
+    if (bufferName == nullptr
         || hasPathnamePrefix(bufferName, SRCDIR "/external/")
         || isSamePathname(bufferName, SRCDIR "/sdext/source/pdfimport/wrapper/keyword_list") )
             // workdir/CustomTarget/sdext/pdfimport/hash.cxx is generated from
@@ -222,6 +225,11 @@ bool PluginHandler::checkIgnoreLocation(SourceLocation loc)
         if (hasPathnamePrefix(s, WORKDIR))
             return true;
     }
+    if ( isLOOLMode() ) {
+        std::string absPath = getAbsolutePath(bufferName);
+        if ( StringRef(absPath).startswith(loolBasePath) )
+            return false;
+    }
     if( hasPathnamePrefix(bufferName, BUILDDIR)
         || hasPathnamePrefix(bufferName, SRCDIR) )
         return false; // ok
@@ -277,6 +285,12 @@ void PluginHandler::HandleTranslationUnit( ASTContext& context )
             pathWarning = "modified source in build dir : %0";
         else if( name.startswith(SRCDIR "/") )
             ; // ok
+        else if ( isLOOLMode() )
+        {
+            std::string absPath = getAbsolutePath(name);
+            if ( !StringRef(absPath).startswith(loolBasePath) )
+                bSkip = true;
+        }
         else
         {
             pathWarning = "modified source in unknown location, not modifying : %0";
diff --git a/compilerplugins/clang/pluginhandler.hxx b/compilerplugins/clang/pluginhandler.hxx
index ea0eb2444506..cb75f9443bb5 100644
--- a/compilerplugins/clang/pluginhandler.hxx
+++ b/compilerplugins/clang/pluginhandler.hxx
@@ -56,6 +56,7 @@ public:
     bool ignoreLocation(SourceLocation loc);
     bool addRemoval( SourceLocation loc );
     bool isDebugMode() const { return debugMode; }
+    bool isLOOLMode() const { return !loolBasePath.empty(); }
     static bool isUnitTestMode();
 private:
     void handleOption( const std::string& option );
@@ -69,6 +70,7 @@ private:
     std::set< SourceLocation > removals;
     std::string scope;
     std::string warningsOnly;
+    std::string loolBasePath;
     bool warningsAsErrors;
     bool debugMode = false;
 };


More information about the Libreoffice-commits mailing list