summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel@peralex.com>2016-07-25 13:32:04 +0200
committerNoel Grandin <noelgrandin@gmail.com>2016-08-25 06:37:42 +0000
commitb8064bdf7fabdbfd2546830f6fb8f0de6b8d4329 (patch)
treecb8c8db34a096467cf81c6ae0649a00032bb4a7f /compilerplugins
parent1e84b23839d96068c862e746c9162db79d2c8c62 (diff)
new loplugin: countusersofdefaultparams
Change-Id: I79e2c690f3e664c14af12cf763dd5a8ac20d6b04 Reviewed-on: https://gerrit.libreoffice.org/28353 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Noel Grandin <noelgrandin@gmail.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/countusersofdefaultparams.cxx234
-rwxr-xr-xcompilerplugins/clang/countusersofdefaultparams.py80
-rwxr-xr-xcompilerplugins/clang/unuseddefaultparams.py14
3 files changed, 319 insertions, 9 deletions
diff --git a/compilerplugins/clang/countusersofdefaultparams.cxx b/compilerplugins/clang/countusersofdefaultparams.cxx
new file mode 100644
index 000000000000..de8d8e52bb24
--- /dev/null
+++ b/compilerplugins/clang/countusersofdefaultparams.cxx
@@ -0,0 +1,234 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <string>
+#include <set>
+#include <iostream>
+#include <fstream>
+
+#include "clang/AST/Attr.h"
+
+#include "plugin.hxx"
+#include "compat.hxx"
+
+/*
+ Count call sites that are actually using the defaulted values on params on methods that declare such.
+
+ The process goes something like this:
+ $ make check
+ $ make FORCE_COMPILE_ALL=1 COMPILER_PLUGIN_TOOL='countusersofdefaultparams' check
+ $ ./compilerplugins/clang/countusersofdefaultparams.py
+*/
+
+namespace {
+
+struct MyFuncInfo
+{
+ std::string access;
+ std::string returnType;
+ std::string nameAndParams;
+ std::string sourceLocation;
+};
+bool operator < (const MyFuncInfo &lhs, const MyFuncInfo &rhs)
+{
+ return std::tie(lhs.returnType, lhs.nameAndParams)
+ < std::tie(rhs.returnType, rhs.nameAndParams);
+}
+struct MyCallInfo : public MyFuncInfo
+{
+ std::string sourceLocationOfCall;
+};
+bool operator < (const MyCallInfo &lhs, const MyCallInfo &rhs)
+{
+ return std::tie(lhs.returnType, lhs.nameAndParams, lhs.sourceLocationOfCall)
+ < std::tie(rhs.returnType, rhs.nameAndParams, rhs.sourceLocationOfCall);
+}
+
+
+// try to limit the voluminous output a little
+static std::set<MyCallInfo> callSet;
+static std::set<MyFuncInfo> definitionSet;
+
+class CountUsersOfDefaultParams:
+ public RecursiveASTVisitor<CountUsersOfDefaultParams>, public loplugin::Plugin
+{
+public:
+ explicit CountUsersOfDefaultParams(InstantiationData const & data): Plugin(data) {}
+
+ virtual void run() override
+ {
+ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
+
+ // dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes
+ // writing to the same logfile
+
+ std::string output;
+ for (const MyFuncInfo & s : definitionSet)
+ output += "defn:\t" + s.access + "\t" + s.returnType + "\t" + s.nameAndParams + "\t" + s.sourceLocation + "\n";
+ for (const MyCallInfo & s : callSet)
+ output += "call:\t" + s.returnType + "\t" + s.nameAndParams + "\t" + s.sourceLocationOfCall + "\n";
+ ofstream myfile;
+ myfile.open( SRCDIR "/loplugin.countusersofdefaultparams.log", ios::app | ios::out);
+ myfile << output;
+ myfile.close();
+ }
+
+ bool shouldVisitTemplateInstantiations () const { return true; }
+
+ bool VisitCallExpr(CallExpr * callExpr);
+ bool VisitFunctionDecl( const FunctionDecl* functionDecl );
+private:
+ void niceName(const FunctionDecl* functionDecl, MyFuncInfo&);
+ std::string locationToString(const SourceLocation&);
+};
+
+void CountUsersOfDefaultParams::niceName(const FunctionDecl* functionDecl, MyFuncInfo& aInfo)
+{
+ if (functionDecl->getInstantiatedFromMemberFunction())
+ functionDecl = functionDecl->getInstantiatedFromMemberFunction();
+ else if (functionDecl->getClassScopeSpecializationPattern())
+ functionDecl = functionDecl->getClassScopeSpecializationPattern();
+// workaround clang-3.5 issue
+#if CLANG_VERSION >= 30600
+ else if (functionDecl->getTemplateInstantiationPattern())
+ functionDecl = functionDecl->getTemplateInstantiationPattern();
+#endif
+
+ switch (functionDecl->getAccess())
+ {
+ case AS_public: aInfo.access = "public"; break;
+ case AS_private: aInfo.access = "private"; break;
+ case AS_protected: aInfo.access = "protected"; break;
+ default: aInfo.access = "unknown"; break;
+ }
+ aInfo.returnType = compat::getReturnType(*functionDecl).getCanonicalType().getAsString();
+
+ if (isa<CXXMethodDecl>(functionDecl)) {
+ const CXXRecordDecl* recordDecl = dyn_cast<CXXMethodDecl>(functionDecl)->getParent();
+ aInfo.nameAndParams += recordDecl->getQualifiedNameAsString();
+ aInfo.nameAndParams += "::";
+ }
+ aInfo.nameAndParams += functionDecl->getNameAsString() + "(";
+ bool bFirst = true;
+ for (const ParmVarDecl *pParmVarDecl : compat::parameters(*functionDecl)) {
+ if (bFirst)
+ bFirst = false;
+ else
+ aInfo.nameAndParams += ",";
+ aInfo.nameAndParams += pParmVarDecl->getType().getCanonicalType().getAsString();
+ }
+ aInfo.nameAndParams += ")";
+ if (isa<CXXMethodDecl>(functionDecl) && dyn_cast<CXXMethodDecl>(functionDecl)->isConst()) {
+ aInfo.nameAndParams += " const";
+ }
+
+ aInfo.sourceLocation = locationToString(functionDecl->getLocation());
+}
+
+bool CountUsersOfDefaultParams::VisitCallExpr(CallExpr * callExpr) {
+ if (ignoreLocation(callExpr)) {
+ return true;
+ }
+ const FunctionDecl* functionDecl;
+ if (isa<CXXMemberCallExpr>(callExpr)) {
+ functionDecl = dyn_cast<CXXMemberCallExpr>(callExpr)->getMethodDecl();
+ }
+ else {
+ functionDecl = callExpr->getDirectCallee();
+ }
+ if (functionDecl == nullptr) {
+ return true;
+ }
+ functionDecl = functionDecl->getCanonicalDecl();
+ // work our way up to the root method
+ while (isa<CXXMethodDecl>(functionDecl)) {
+ const CXXMethodDecl* methodDecl = dyn_cast<CXXMethodDecl>(functionDecl);
+ if (methodDecl->size_overridden_methods()==0)
+ break;
+ functionDecl = *methodDecl->begin_overridden_methods();
+ }
+ // work our way back to the root definition for template methods
+ if (functionDecl->getInstantiatedFromMemberFunction())
+ functionDecl = functionDecl->getInstantiatedFromMemberFunction();
+ else if (functionDecl->getClassScopeSpecializationPattern())
+ functionDecl = functionDecl->getClassScopeSpecializationPattern();
+// workaround clang-3.5 issue
+#if CLANG_VERSION >= 30600
+ else if (functionDecl->getTemplateInstantiationPattern())
+ functionDecl = functionDecl->getTemplateInstantiationPattern();
+#endif
+ int n = functionDecl->getNumParams() - 1;
+ if (n < 0 || !functionDecl->getParamDecl(n)->hasDefaultArg()) {
+ return true;
+ }
+ while (n > 0 && functionDecl->getParamDecl(n-1)->hasDefaultArg()) {
+ --n;
+ }
+ // look for callsites that are actually using the defaulted values
+ if ( n < (int)callExpr->getNumArgs() && callExpr->getArg(n)->isDefaultArgument()) {
+ MyCallInfo callInfo;
+ niceName(functionDecl, callInfo);
+ callInfo.sourceLocationOfCall = locationToString(callExpr->getLocStart());
+ callSet.insert(callInfo);
+ }
+ return true;
+}
+
+std::string CountUsersOfDefaultParams::locationToString(const SourceLocation& sourceLoc)
+{
+ SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( sourceLoc );
+ StringRef name = compiler.getSourceManager().getFilename(expansionLoc);
+ return std::string(name.substr(strlen(SRCDIR)+1)) + ":" + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc));
+}
+
+bool CountUsersOfDefaultParams::VisitFunctionDecl( const FunctionDecl* functionDecl )
+{
+ functionDecl = functionDecl->getCanonicalDecl();
+ if( !functionDecl || !functionDecl->getLocation().isValid() || ignoreLocation( functionDecl )) {
+ return true;
+ }
+ const CXXMethodDecl* methodDecl = dyn_cast_or_null<CXXMethodDecl>(functionDecl);
+
+ // ignore method overrides, since the call will show up as being directed to the root method
+ if (methodDecl && (methodDecl->size_overridden_methods() != 0 || methodDecl->hasAttr<OverrideAttr>())) {
+ return true;
+ }
+ // ignore stuff that forms part of the stable URE interface
+ if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(
+ functionDecl->getNameInfo().getLoc()))) {
+ return true;
+ }
+ if (isa<CXXDestructorDecl>(functionDecl)) {
+ return true;
+ }
+ if (isa<CXXConstructorDecl>(functionDecl)) {
+ return true;
+ }
+ if (functionDecl->isDeleted()) {
+ return true;
+ }
+ auto n = functionDecl->getNumParams();
+ if (n == 0 || !functionDecl->getParamDecl(n - 1)->hasDefaultArg()) {
+ return true;
+ }
+
+ if( functionDecl->getLocation().isValid() )
+ {
+ MyFuncInfo funcInfo;
+ niceName(functionDecl, funcInfo);
+ definitionSet.insert(funcInfo);
+ }
+ return true;
+}
+
+loplugin::Plugin::Registration< CountUsersOfDefaultParams > X("countusersofdefaultparams", false);
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/compilerplugins/clang/countusersofdefaultparams.py b/compilerplugins/clang/countusersofdefaultparams.py
new file mode 100755
index 000000000000..cfb271142a3f
--- /dev/null
+++ b/compilerplugins/clang/countusersofdefaultparams.py
@@ -0,0 +1,80 @@
+#!/usr/bin/python
+
+import sys
+import re
+import io
+
+definitionToSourceLocationMap = dict()
+callDict = dict()
+
+# clang does not always use exactly the same numbers in the type-parameter vars it generates
+# so I need to substitute them to ensure we can match correctly.
+normalizeTypeParamsRegex = re.compile(r"type-parameter-\d+-\d+")
+def normalizeTypeParams( line ):
+ return normalizeTypeParamsRegex.sub("type-parameter-?-?", line)
+
+# The parsing here is designed to avoid grabbing stuff which is mixed in from gbuild.
+# I have not yet found a way of suppressing the gbuild output.
+with io.open("loplugin.countusersofdefaultparams.log", "rb", buffering=1024*1024) as txt:
+ for line in txt:
+ if line.startswith("defn:\t"):
+ tokens = line.strip().split("\t")
+ access = tokens[1]
+ returnType = tokens[2]
+ nameAndParams = tokens[3]
+ sourceLocation = tokens[4]
+ funcInfo = normalizeTypeParams(returnType) + " " + normalizeTypeParams(nameAndParams)
+ definitionToSourceLocationMap[funcInfo] = sourceLocation
+ if not funcInfo in callDict:
+ callDict[funcInfo] = set()
+ elif line.startswith("call:\t"):
+ tokens = line.strip().split("\t")
+ returnType = tokens[1]
+ nameAndParams = tokens[2]
+ sourceLocationOfCall = tokens[3]
+ funcInfo = normalizeTypeParams(returnType) + " " + normalizeTypeParams(nameAndParams)
+ if not funcInfo in callDict:
+ callDict[funcInfo] = set()
+ callDict[funcInfo].add(sourceLocationOfCall)
+
+# Invert the definitionToSourceLocationMap.
+sourceLocationToDefinitionMap = {}
+for k, v in definitionToSourceLocationMap.iteritems():
+ sourceLocationToDefinitionMap[v] = sourceLocationToDefinitionMap.get(v, [])
+ sourceLocationToDefinitionMap[v].append(k)
+
+
+tmp1list = list()
+for k,v in callDict.iteritems():
+ if len(v) >= 1:
+ continue
+ # created by macros
+ if k.endswith("::RegisterInterface(class SfxModule *)"):
+ continue
+ if k.endswith("::RegisterChildWindow(_Bool,class SfxModule *,enum SfxChildWindowFlags)"):
+ continue
+ if k.endswith("::RegisterControl(unsigned short,class SfxModule *)"):
+ continue
+ if k.endswith("::RegisterFactory(unsigned short)"):
+ continue
+ # windows-only stuff
+ if "ShutdownIcon::OpenURL" in k:
+ continue
+ if k in definitionToSourceLocationMap:
+ tmp1list.append((k, definitionToSourceLocationMap[k]))
+
+# sort the results using a "natural order" so sequences like [item1,item2,item10] sort nicely
+def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
+ return [int(text) if text.isdigit() else text.lower()
+ for text in re.split(_nsre, s)]
+
+# sort results by name and line number
+tmp1list.sort(key=lambda v: natural_sort_key(v[1]))
+
+# print out the results
+with open("loplugin.countusersofdefaultparams.report", "wt") as f:
+ for t in tmp1list:
+ f.write(t[1] + "\n")
+ f.write(" " + t[0] + "\n")
+
+
diff --git a/compilerplugins/clang/unuseddefaultparams.py b/compilerplugins/clang/unuseddefaultparams.py
index c0c6c613857b..45ef27e47669 100755
--- a/compilerplugins/clang/unuseddefaultparams.py
+++ b/compilerplugins/clang/unuseddefaultparams.py
@@ -8,12 +8,6 @@ definitionSet = set()
definitionToSourceLocationMap = dict()
callSet = set()
-# things we need to exclude for reasons like :
-# - it's a weird template thingy that confuses the plugin
-exclusionSet = set([
- "class boost::intrusive_ptr<class FontCharMap> FontCharMap::GetDefaultMap(_Bool)"
-])
-
# clang does not always use exactly the same numbers in the type-parameter vars it generates
# so I need to substitute them to ensure we can match correctly.
normalizeTypeParamsRegex = re.compile(r"type-parameter-\d+-\d+")
@@ -56,12 +50,14 @@ for k, definitions in sourceLocationToDefinitionMap.iteritems():
tmp1set = set()
for d in definitionSet:
- clazz = d[0] + " " + d[1]
- if clazz in exclusionSet:
+ methodSignature = d[0] + " " + d[1]
+ if (methodSignature == "class boost::intrusive_ptr<class FontCharMap> FontCharMap::GetDefaultMap(_Bool)"
+ # bunch of methods in here all with the same signature
+ or "SwAttrSet::" in d[1] or "SwFormat::" in d[1]):
continue
if d in callSet:
continue
- tmp1set.add((clazz, definitionToSourceLocationMap[d]))
+ tmp1set.add((methodSignature, definitionToSourceLocationMap[d]))
# sort the results using a "natural order" so sequences like [item1,item2,item10] sort nicely
def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):