diff options
-rw-r--r-- | compilerplugins/clang/unusedmethods.cxx | 198 | ||||
-rwxr-xr-x | compilerplugins/clang/unusedmethods.py | 77 | ||||
-rw-r--r-- | compilerplugins/clang/unusedmethodsremove.cxx | 145 |
3 files changed, 420 insertions, 0 deletions
diff --git a/compilerplugins/clang/unusedmethods.cxx b/compilerplugins/clang/unusedmethods.cxx new file mode 100644 index 000000000000..e45ee7fb7fd3 --- /dev/null +++ b/compilerplugins/clang/unusedmethods.cxx @@ -0,0 +1,198 @@ +/* -*- 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 <cassert> +#include <string> +#include <iostream> +#include <set> +#include "plugin.hxx" +#include "compat.hxx" + +/** +Dump a list of calls to methods, and a list of method definitions. +Then we will post-process the 2 lists and find the set of unused methods. + +Be warned that it produces around 3G of log file. + +The process goes something like this: + $ make check + $ make FORCE_COMPILE_ALL=1 COMPILER_PLUGIN_TOOL='unusedmethods' check > log.txt + $ grep -P '(call:)|(definition:)' log.txt | sort -u > log2.txt + $ ./compilerplugins/clang/unusedmethods.py log2.txt > result.txt + +and then + $ for dir in *; do make FORCE_COMPILE_ALL=1 UPDATE_FILES=$dir COMPILER_PLUGIN_TOOL='unusedmethodsremove' $dir; done +to auto-remove the method declarations + +Note that the actual process may involve a fair amount of undoing, hand editing, and general messing around +to get it to work :-) + +TODO ignore calls from a method to itself, so we can eliminate unused recursive methods +TODO deal with calls to superclass/member constructors from other constructors, so + we can find unused constructors +TODO need to handle places where the code takes the address of a method, that needs to count + as a use-site. +TODO deal with free functions and static methods +TODO track instantiations of template class constructor methods +*/ + +namespace { + +class UnusedMethods: + public RecursiveASTVisitor<UnusedMethods>, public loplugin::Plugin +{ +public: + explicit UnusedMethods(InstantiationData const & data): Plugin(data) {} + + virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool VisitCallExpr(CallExpr* ); + bool VisitCXXMethodDecl( const CXXMethodDecl* decl ); + bool VisitDeclRefExpr( const DeclRefExpr* ); + bool TraverseCXXMethodDecl(CXXMethodDecl * decl) { return RecursiveASTVisitor::TraverseCXXMethodDecl(decl); } +}; + +static std::string niceName(const CXXMethodDecl* functionDecl) +{ + std::string s = + compat::getReturnType(*functionDecl).getCanonicalType().getAsString() + + " " + functionDecl->getParent()->getQualifiedNameAsString() + + "::" + functionDecl->getNameAsString() + + "("; + bool bFirst = true; + for (const ParmVarDecl *pParmVarDecl : functionDecl->params()) { + if (bFirst) + bFirst = false; + else + s += ","; + s += pParmVarDecl->getType().getCanonicalType().getAsString(); + } + s += ")"; + if (functionDecl->isConst()) { + s += " const"; + } + return s; +} + +// try to limit the volumninous output a little +static std::set<std::string> alreadySeenCallSet; + +static void logCallToRootMethods(const CXXMethodDecl* decl) +{ + // For virtual/overriding methods, we need to pretend we called the root method(s), + // so that they get marked as used. + bool bPrinted = false; + for(CXXMethodDecl::method_iterator it = decl->begin_overridden_methods(); + it != decl->end_overridden_methods(); ++it) + { + logCallToRootMethods(*it); + bPrinted = true; + } + if (!bPrinted) + { + std::string s = niceName(decl); + if (alreadySeenCallSet.insert(s).second) + cout << "call:\t" << niceName(decl) << endl; + } +} + +static bool startsWith(const std::string& s, const char* other) +{ + return s.compare(0, strlen(other), other) == 0; +} + +static bool isStandardStuff(const std::string& s) +{ + // ignore UNO interface definitions, cannot change those + return startsWith(s, "com::sun::star::") + // ignore stuff in the C++ stdlib and boost + || startsWith(s, "std::") || startsWith(s, "boost::") || startsWith(s, "class boost::") || startsWith(s, "__gnu_debug::") + // can't change our rtl layer + || startsWith(s, "rtl::") + // ignore anonymous namespace stuff, it is compilation-unit-local and the compiler will detect any + // unused code there + || startsWith(s, "(anonymous namespace)::"); +} + +bool UnusedMethods::VisitCallExpr(CallExpr* expr) +{ + if (ignoreLocation(expr)) { + return true; + } + CXXMethodDecl* decl = dyn_cast_or_null<CXXMethodDecl>( + expr->getDirectCallee()); + if (decl == nullptr) { + return true; + } + logCallToRootMethods(decl); + // if we see a call to a templated method, it effectively instantiates a new method, + // so we need to examine it's interior to see if it in turn calls anything else + if (decl->getTemplatedKind() != clang::FunctionDecl::TemplatedKind::TK_NonTemplate + || decl->isFunctionTemplateSpecialization()) + { + TraverseCXXMethodDecl(decl); + } + return true; +} + +bool UnusedMethods::VisitCXXMethodDecl( const CXXMethodDecl* functionDecl ) +{ + if (ignoreLocation(functionDecl)) { + return true; + } + functionDecl = functionDecl->getCanonicalDecl(); + // ignore method overrides, since the call will show up as being directed to the root method + if (functionDecl->size_overridden_methods() != 0 || functionDecl->hasAttr<OverrideAttr>()) { + return true; + } + // ignore static's for now. Would require generalising this plugin a little + if (functionDecl->isStatic()) { + return true; + } + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc( + functionDecl->getNameInfo().getLoc()))) { + return true; + } + if (isStandardStuff(functionDecl->getParent()->getQualifiedNameAsString())) { + return true; + } + if (isa<CXXDestructorDecl>(functionDecl)) { + return true; + } + if (isa<CXXConstructorDecl>(functionDecl)) { + return true; + } + if (functionDecl->isDeleted()) { + return true; + } + + cout << "definition:\t" << niceName(functionDecl) << endl; + return true; +} + +// this catches places that take the address of a method +bool UnusedMethods::VisitDeclRefExpr( const DeclRefExpr* declRefExpr ) +{ + if (ignoreLocation(declRefExpr)) { + return true; + } + const Decl* functionDecl = declRefExpr->getDecl(); + if (!isa<CXXMethodDecl>(functionDecl)) { + return true; + } + logCallToRootMethods(dyn_cast<CXXMethodDecl>(functionDecl)); + return true; +} + +loplugin::Plugin::Registration< UnusedMethods > X("unusedmethods", false); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/unusedmethods.py b/compilerplugins/clang/unusedmethods.py new file mode 100755 index 000000000000..4b17f95a01d4 --- /dev/null +++ b/compilerplugins/clang/unusedmethods.py @@ -0,0 +1,77 @@ +#!/usr/bin/python + +import sys + +definitionSet = set() +callSet = set() +# things we need to exclude for reasons like : +# - it's a weird template thingy that confuses the plugin +exclusionSet = set([ + "double basegfx::DoubleTraits::maxVal()", + "double basegfx::DoubleTraits::minVal()", + "double basegfx::DoubleTraits::neutral()", + "int basegfx::Int32Traits::maxVal()", + "int basegfx::Int32Traits::minVal()", + "int basegfx::Int32Traits::neutral()", + "unsigned long UniqueIndexImpl::Insert(void *)", + # used from a yacc lexer + "class rtl::OUString unoidl::detail::SourceProviderType::getName() const", + "_Bool unoidl::detail::SourceProviderType::equals(const struct unoidl::detail::SourceProviderType &) const", + "_Bool unoidl::detail::SourceProviderEntityPad::isPublished() const", + "_Bool unoidl::detail::SourceProviderInterfaceTypeEntityPad::checkMemberClashes(int,void *,struct unoidl::detail::SourceProviderScannerData *,const class rtl::OUString &,const class rtl::OUString &,_Bool) const", + "_Bool unoidl::detail::SourceProviderInterfaceTypeEntityPad::checkBaseClashes(int,void *,struct unoidl::detail::SourceProviderScannerData *,const class rtl::OUString &,const class rtl::Reference<class unoidl::InterfaceTypeEntity> &,_Bool,_Bool,_Bool,class std::__debug::set<class rtl::OUString, struct std::less<class rtl::OUString>, class std::allocator<class rtl::OUString> > *) const", + "_Bool unoidl::detail::SourceProviderInterfaceTypeEntityPad::addDirectBase(int,void *,struct unoidl::detail::SourceProviderScannerData *,const struct unoidl::detail::SourceProviderInterfaceTypeEntityPad::DirectBase &,_Bool)", + "_Bool unoidl::detail::SourceProviderInterfaceTypeEntityPad::addBase(int,void *,struct unoidl::detail::SourceProviderScannerData *,const class rtl::OUString &,const class rtl::OUString &,const class rtl::Reference<class unoidl::InterfaceTypeEntity> &,_Bool,_Bool)", + "_Bool unoidl::detail::SourceProviderInterfaceTypeEntityPad::addDirectMember(int,void *,struct unoidl::detail::SourceProviderScannerData *,const class rtl::OUString &)", + "_Bool unoidl::detail::SourceProviderInterfaceTypeEntityPad::addOptionalBaseMembers(int,void *,struct unoidl::detail::SourceProviderScannerData *,const class rtl::OUString &,const class rtl::Reference<class unoidl::InterfaceTypeEntity> &)", + "void unoidl::detail::SourceProviderScannerData::setSource(const void *,unsigned long)", + # TODO track instantiations of template class constructors + "void comphelper::IEventProcessor::release()", + # used by Windows build + "_Bool basegfx::B2ITuple::equalZero() const", + "class basegfx::B2DPolyPolygon basegfx::unotools::UnoPolyPolygon::getPolyPolygonUnsafe() const", + ]) + + +with open(sys.argv[1]) as txt: + for line in txt: + if line.startswith("definition:\t"): + idx1 = line.find("\t") + clazzName = line[idx1+1 : len(line)-1] + definitionSet.add(clazzName) + elif line.startswith("call:\t"): + idx1 = line.find("\t") + clazzName = line[idx1+1 : len(line)-1] + callSet.add(clazzName) + +for clazz in sorted(definitionSet - callSet - exclusionSet): + # ignore operators, they are normally called from inside STL code + if (clazz.find("::operator") != -1): + continue + # ignore the custom RTTI stuff + if ( (clazz.find("::CreateType()") != -1) + or (clazz.find("::IsA(") != -1) + or (clazz.find("::Type()") != -1)): + continue + # if this method is const, and there is a non-const variant of it, and the non-const variant is in use, then leave it alone + if (clazz.endswith(" const") + and clazz[6:len(clazz)-6] in definitionSet + and clazz[6:len(clazz)-6] in callSet): + continue + # if this method is non-const, and there is a const variant of it, and the const variant is in use, then leave it alone + if ((not clazz.endswith(" const")) + and ("const " + clazz + " const") in definitionSet + and ("const " + clazz + " const") in callSet): + continue + # There is lots of macro magic going on in /home/noel/libo4/include/sax/fshelper.hxx that should be using C++11 varag templates + if clazz.startswith("void sax_fastparser::FastSerializerHelper::"): + continue + # used by Windows build + if clazz.find("DdeTopic::") != -1 or clazz.find("DdeData::") != -1 or clazz.find("DdeService::") != -1: + continue + print clazz + +# add an empty line at the end to make it easier for the unusedmethodsremove plugin to mmap() the output file +print + + diff --git a/compilerplugins/clang/unusedmethodsremove.cxx b/compilerplugins/clang/unusedmethodsremove.cxx new file mode 100644 index 000000000000..908773316ac2 --- /dev/null +++ b/compilerplugins/clang/unusedmethodsremove.cxx @@ -0,0 +1,145 @@ +/* -*- 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 <cassert> +#include <string> +#include <iostream> +#include "plugin.hxx" +#include "compat.hxx" +#include <sys/mman.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/stat.h> +#include <assert.h> +#include <cstring> + +/** + This is intended to be run as the second stage of the "unusedmethods" clang plugin. +*/ + +namespace { + +class UnusedMethodsRemove: + public RecursiveASTVisitor<UnusedMethodsRemove>, public loplugin::RewritePlugin +{ +public: + explicit UnusedMethodsRemove(InstantiationData const & data); + ~UnusedMethodsRemove(); + + virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool VisitCXXMethodDecl( const CXXMethodDecl* var ); +private: + // I use a brute-force approach - mmap the results file and do a linear search on it + // It works surprisingly well, because the file is small enough to fit into L2 cache on modern CPU's + size_t mmapFilesize; + int mmapFD; + char* mmappedData; +}; + +static size_t getFilesize(const char* filename) +{ + struct stat st; + stat(filename, &st); + return st.st_size; +} + +UnusedMethodsRemove::UnusedMethodsRemove(InstantiationData const & data): RewritePlugin(data) +{ + static const char sInputFile[] = "/home/noel/libo4/result.txt"; + mmapFilesize = getFilesize(sInputFile); + //Open file + mmapFD = open(sInputFile, O_RDONLY, 0); + assert(mmapFD != -1); + //Execute mmap + mmappedData = static_cast<char*>(mmap(NULL, mmapFilesize, PROT_READ, MAP_PRIVATE | MAP_POPULATE, mmapFD, 0)); + assert(mmappedData != NULL); +} + +UnusedMethodsRemove::~UnusedMethodsRemove() +{ + //Cleanup + int rc = munmap(mmappedData, mmapFilesize); + assert(rc == 0); + close(mmapFD); +} + +static std::string niceName(const CXXMethodDecl* functionDecl) +{ + std::string s = + compat::getReturnType(*functionDecl).getCanonicalType().getAsString() + + " " + functionDecl->getParent()->getQualifiedNameAsString() + + "::" + functionDecl->getNameAsString() + + "("; + bool bFirst = true; + for (const ParmVarDecl *pParmVarDecl : functionDecl->params()) { + if (bFirst) + bFirst = false; + else + s += ","; + s += pParmVarDecl->getType().getCanonicalType().getAsString(); + } + s += ")"; + if (functionDecl->isConst()) { + s += " const"; + } + return s; +} + +bool UnusedMethodsRemove::VisitCXXMethodDecl( const CXXMethodDecl* functionDecl ) +{ + if (rewriter == nullptr) { + return true; + } + if (ignoreLocation(functionDecl)) { + return true; + } + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc( + functionDecl->getCanonicalDecl()->getNameInfo().getLoc()))) { + return true; + } + + // don't mess with templates + if (functionDecl->getParent()->getDescribedClassTemplate() != nullptr) { + return true; + } + if (functionDecl->getTemplatedKind() != FunctionDecl::TK_NonTemplate) { + return true; + } + + std::string aNiceName = "\n" + niceName(functionDecl) + "\n"; + const char *aNiceNameStr = aNiceName.c_str(); + char* found = std::search(mmappedData, mmappedData + mmapFilesize, aNiceNameStr, aNiceNameStr + strlen(aNiceNameStr)); + if(!(found < mmappedData + mmapFilesize)) { + return true; + } + // sometimes the declaration has a semicolon just after it, and it's much neater to remove that too. + SourceRange replaceRange(functionDecl->getSourceRange()); + if (rewriter->getRewrittenText(SourceRange(replaceRange.getEnd(), replaceRange.getEnd().getLocWithOffset(1))) == ";") { + replaceRange.setEnd(replaceRange.getEnd().getLocWithOffset(1)); + } + if (!replaceText(replaceRange, "")) { + report( + DiagnosticsEngine::Warning, + "Could not remove unused method (" + niceName(functionDecl) + ")", + functionDecl->getLocStart()) + << functionDecl->getSourceRange(); + } + return true; +} + + + +loplugin::Plugin::Registration< UnusedMethodsRemove > X("unusedmethodsremove", false); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |