summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2021-05-06 19:27:50 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2021-05-06 22:17:16 +0200
commitd6375dcd1db6ce7e1bae8297949801f7d4249e52 (patch)
treebc4df363f8a0d569ba942ad15dcca906d5242832 /compilerplugins
parent48969e9f578c16ff4142b2911746212044cbce61 (diff)
loplugin:unusedmethods
plugin code needed some updating because it was interacting badly with PCH code in pluginhandler::ignoreLocation Change-Id: I228f94a4e285747bd1d5b8536010f8617118cafa Reviewed-on: https://gerrit.libreoffice.org/c/core/+/115212 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/unusedmethods.cxx58
1 files changed, 49 insertions, 9 deletions
diff --git a/compilerplugins/clang/unusedmethods.cxx b/compilerplugins/clang/unusedmethods.cxx
index b854238d2688..818c0b645a48 100644
--- a/compilerplugins/clang/unusedmethods.cxx
+++ b/compilerplugins/clang/unusedmethods.cxx
@@ -82,6 +82,11 @@ public:
virtual void run() override
{
+ StringRef fn(handler.getMainFileName());
+ // ignore external code, makes this run faster
+ if (fn.contains("UnpackedTarball"))
+ return;
+
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
// dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes
@@ -90,10 +95,8 @@ public:
std::string output;
for (const MyFuncInfo & s : definitionSet)
{
- // ignore external code
- if (s.sourceLocation.rfind("external/", 0) != 0)
- output += "definition:\t" + s.access + "\t" + s.returnType + "\t" + s.nameAndParams
- + "\t" + s.sourceLocation + "\t" + s.virtualness + "\n";
+ output += "definition:\t" + s.access + "\t" + s.returnType + "\t" + s.nameAndParams
+ + "\t" + s.sourceLocation + "\t" + s.virtualness + "\n";
}
// for the "unused method" analysis
for (const MyFuncInfo & s : callSet)
@@ -128,6 +131,9 @@ private:
MyFuncInfo niceName(const FunctionDecl* functionDecl);
std::string toString(SourceLocation loc);
void functionTouchedFromExpr( const FunctionDecl* calleeFunctionDecl, const Expr* expr );
+ bool ignoreLocation(SourceLocation loc);
+ bool checkIgnoreLocation(SourceLocation loc);
+
CXXRecordDecl const * currentCxxRecordDecl = nullptr;
FunctionDecl const * currentFunctionDecl = nullptr;
};
@@ -193,6 +199,40 @@ MyFuncInfo UnusedMethods::niceName(const FunctionDecl* functionDecl)
return aInfo;
}
+/**
+ * Our need to see everything conflicts with the PCH code in pluginhandler::ignoreLocation,
+ * so we have to do this ourselves.
+ */
+bool UnusedMethods::ignoreLocation(SourceLocation loc)
+{
+ static std::unordered_map<SourceLocation, bool> checkedMap;
+ auto it = checkedMap.find(loc);
+ if (it != checkedMap.end())
+ return it->second;
+ bool ignore = checkIgnoreLocation(loc);
+ checkedMap.emplace(loc, ignore);
+ return ignore;
+}
+
+bool UnusedMethods::checkIgnoreLocation(SourceLocation loc)
+{
+ // simplified form of the code in PluginHandler::checkIgnoreLocation
+ SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( loc );
+ if( compiler.getSourceManager().isInSystemHeader( expansionLoc ))
+ return true;
+ PresumedLoc presumedLoc = compiler.getSourceManager().getPresumedLoc( expansionLoc );
+ if( presumedLoc.isInvalid())
+ return true;
+ const char* bufferName = presumedLoc.getFilename();
+ if (bufferName == NULL
+ || loplugin::hasPathnamePrefix(bufferName, SRCDIR "/external/"))
+ return true;
+ if( loplugin::hasPathnamePrefix(bufferName, BUILDDIR "/")
+ || loplugin::hasPathnamePrefix(bufferName, SRCDIR "/") )
+ return false; // ok
+ return true;
+}
+
std::string UnusedMethods::toString(SourceLocation loc)
{
SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( loc );
@@ -221,7 +261,7 @@ void UnusedMethods::logCallToRootMethods(const FunctionDecl* functionDecl, std::
{
while (functionDecl->getTemplateInstantiationPattern())
functionDecl = functionDecl->getTemplateInstantiationPattern();
- if (functionDecl->getLocation().isValid() && !ignoreLocation( functionDecl )
+ if (functionDecl->getLocation().isValid() && !ignoreLocation( compat::getBeginLoc(functionDecl) )
&& !functionDecl->isExternC())
funcSet.insert(niceName(functionDecl));
}
@@ -266,7 +306,7 @@ gotfunc:
{
const FunctionDecl* parentFunctionOfCallSite = getParentFunctionDecl(expr);
if (parentFunctionOfCallSite != calleeFunctionDecl) {
- if (!parentFunctionOfCallSite || !ignoreLocation(parentFunctionOfCallSite)) {
+ if (!parentFunctionOfCallSite || !ignoreLocation(compat::getBeginLoc(parentFunctionOfCallSite))) {
calledFromOutsideSet.insert(niceName(calleeFunctionDecl));
}
}
@@ -306,7 +346,7 @@ bool UnusedMethods::VisitCXXConstructExpr( const CXXConstructExpr* constructExpr
const CXXConstructorDecl* constructorDecl = constructExpr->getConstructor();
constructorDecl = constructorDecl->getCanonicalDecl();
- if (!constructorDecl->getLocation().isValid() || ignoreLocation(constructorDecl)) {
+ if (!constructorDecl->getLocation().isValid() || ignoreLocation(compat::getBeginLoc(constructorDecl))) {
return true;
}
@@ -337,7 +377,7 @@ bool UnusedMethods::VisitFunctionDecl( const FunctionDecl* functionDecl )
{
return true;
}
- if (!canonicalFunctionDecl->getLocation().isValid() || ignoreLocation(canonicalFunctionDecl)) {
+ if (!canonicalFunctionDecl->getLocation().isValid() || ignoreLocation(compat::getBeginLoc(canonicalFunctionDecl))) {
return true;
}
// ignore method overrides, since the call will show up as being directed to the root method
@@ -367,7 +407,7 @@ bool UnusedMethods::VisitDeclRefExpr( const DeclRefExpr* declRefExpr )
{
const FunctionDecl* parentFunctionOfCallSite = getParentFunctionDecl(declRefExpr);
if (parentFunctionOfCallSite != functionDecl) {
- if (!parentFunctionOfCallSite || !ignoreLocation(parentFunctionOfCallSite)) {
+ if (!parentFunctionOfCallSite || !ignoreLocation(compat::getBeginLoc(parentFunctionOfCallSite))) {
calledFromOutsideSet.insert(niceName(functionDecl));
}
}