2015-07-02 09:18:31 +02:00
|
|
|
/* -*- 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>
|
2015-07-06 10:44:18 +02:00
|
|
|
#include <fstream>
|
2015-07-02 09:18:31 +02:00
|
|
|
#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.
|
|
|
|
|
2015-07-06 10:44:18 +02:00
|
|
|
Be warned that it produces around 2.4G of log file.
|
2015-07-02 09:18:31 +02:00
|
|
|
|
|
|
|
The process goes something like this:
|
|
|
|
$ make check
|
2015-07-06 10:44:18 +02:00
|
|
|
$ make FORCE_COMPILE_ALL=1 COMPILER_PLUGIN_TOOL='unusedmethods' check
|
|
|
|
$ ./compilerplugins/clang/unusedmethods.py unusedmethods.log > result.txt
|
2015-07-02 09:18:31 +02:00
|
|
|
|
|
|
|
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 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
|
2015-07-13 09:55:03 +02:00
|
|
|
TODO track instantiation of overridden methods when a template class is instantiated
|
2015-07-02 09:18:31 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-07-06 10:44:18 +02:00
|
|
|
// try to limit the volumninous output a little
|
|
|
|
static std::set<std::string> callSet;
|
|
|
|
static std::set<std::string> definitionSet;
|
|
|
|
|
|
|
|
|
2015-07-02 09:18:31 +02:00
|
|
|
class UnusedMethods:
|
|
|
|
public RecursiveASTVisitor<UnusedMethods>, public loplugin::Plugin
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit UnusedMethods(InstantiationData const & data): Plugin(data) {}
|
|
|
|
|
2015-07-06 10:44:18 +02:00
|
|
|
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 std::string & s : callSet)
|
|
|
|
output += "call:\t" + s + "\t\n";
|
|
|
|
for (const std::string & s : definitionSet)
|
|
|
|
output += "definition:\t" + s + "\t\n";
|
|
|
|
ofstream myfile;
|
|
|
|
myfile.open("/home/noel/libo4/unusedmethods.log", ios::app | ios::out);
|
|
|
|
myfile << output;
|
|
|
|
myfile.close();
|
|
|
|
}
|
2015-07-02 09:18:31 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2015-07-06 10:44:18 +02:00
|
|
|
decl = decl->getCanonicalDecl();
|
2015-07-02 09:18:31 +02:00
|
|
|
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);
|
2015-07-06 10:44:18 +02:00
|
|
|
callSet.insert(s);
|
2015-07-02 09:18:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)::");
|
|
|
|
}
|
|
|
|
|
2015-07-07 13:58:41 +02:00
|
|
|
// prevent recursive templates from blowing up the stack
|
2015-07-13 13:31:13 +02:00
|
|
|
static std::set<std::string> traversedFunctionSet;
|
2015-07-07 13:58:41 +02:00
|
|
|
|
2015-07-02 09:18:31 +02:00
|
|
|
bool UnusedMethods::VisitCallExpr(CallExpr* expr)
|
|
|
|
{
|
|
|
|
if (ignoreLocation(expr)) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-07 10:12:55 +02:00
|
|
|
FunctionDecl* calleeFunctionDecl = expr->getDirectCallee();
|
2015-07-07 13:58:41 +02:00
|
|
|
if (calleeFunctionDecl == nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-13 13:31:13 +02:00
|
|
|
// if we see a call to a function, it may effectively create new code,
|
|
|
|
// if the function is templated. However, if we are inside a template function,
|
|
|
|
// calling another function on the same template, the same problem occurs.
|
|
|
|
// Rather than tracking all of that, just traverse anything we have not already traversed.
|
|
|
|
if (traversedFunctionSet.insert(calleeFunctionDecl->getQualifiedNameAsString()).second)
|
|
|
|
TraverseFunctionDecl(calleeFunctionDecl);
|
2015-07-07 10:12:55 +02:00
|
|
|
|
|
|
|
CXXMethodDecl* calleeMethodDecl = dyn_cast_or_null<CXXMethodDecl>(calleeFunctionDecl);
|
|
|
|
if (calleeMethodDecl == nullptr) {
|
|
|
|
return true;
|
2015-07-02 09:18:31 +02:00
|
|
|
}
|
2015-07-07 10:12:55 +02:00
|
|
|
logCallToRootMethods(calleeMethodDecl);
|
2015-07-02 09:18:31 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-06 10:44:18 +02:00
|
|
|
definitionSet.insert(niceName(functionDecl));
|
2015-07-02 09:18:31 +02:00
|
|
|
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: */
|