Kaydet (Commit) 5af68a26 authored tarafından Noel Grandin's avatar Noel Grandin

rename inlineablemethods plugin to expandablemethods

and add support for nested functions

Change-Id: I63daee5b3047fa1ed5de0e5ddaf998f8b17bc780
üst 40fd53a2
...@@ -21,7 +21,8 @@ ...@@ -21,7 +21,8 @@
#include "compat.hxx" #include "compat.hxx"
/** /**
Methods that are only called from inside their own class, and are only called from one spot Find methods that are only called from inside their own class, and are only called from one spot.
They are candidates to be removed and have their code inlined into the call site.
TODO if a method has only one call-site, and that call site is inside a constructor TODO if a method has only one call-site, and that call site is inside a constructor
...@@ -54,11 +55,11 @@ static std::set<MyFuncInfo> largeFunctionSet; ...@@ -54,11 +55,11 @@ static std::set<MyFuncInfo> largeFunctionSet;
static std::set<MyFuncInfo> addressOfSet; static std::set<MyFuncInfo> addressOfSet;
class InlineableMethods: class ExpandableMethods:
public RecursiveASTVisitor<InlineableMethods>, public loplugin::Plugin public RecursiveASTVisitor<ExpandableMethods>, public loplugin::Plugin
{ {
public: public:
explicit InlineableMethods(InstantiationData const & data): Plugin(data) {} explicit ExpandableMethods(InstantiationData const & data): Plugin(data) {}
virtual void run() override virtual void run() override
{ {
...@@ -80,7 +81,7 @@ public: ...@@ -80,7 +81,7 @@ public:
for (const MyFuncInfo & s : addressOfSet) for (const MyFuncInfo & s : addressOfSet)
output += "addrof:\t" + s.returnType + "\t" + s.nameAndParams + "\n"; output += "addrof:\t" + s.returnType + "\t" + s.nameAndParams + "\n";
ofstream myfile; ofstream myfile;
myfile.open( SRCDIR "/loplugin.inlineablemethods.log", ios::app | ios::out); myfile.open( SRCDIR "/loplugin.expandablemethods.log", ios::app | ios::out);
myfile << output; myfile << output;
myfile.close(); myfile.close();
} }
...@@ -105,11 +106,10 @@ private: ...@@ -105,11 +106,10 @@ private:
bool isCalleeFunctionInteresting( const FunctionDecl* ); bool isCalleeFunctionInteresting( const FunctionDecl* );
// I use traverse and a member variable because I cannot find a reliable way of walking back up the AST tree using the parentStmt() stuff // I use traverse and a member variable because I cannot find a reliable way of walking back up the AST tree using the parentStmt() stuff
// TODO doesn't cope with nested functions std::vector<const FunctionDecl*> maTraversingFunctions;
const FunctionDecl* mpTraversingFunction = nullptr;
}; };
MyFuncInfo InlineableMethods::niceName(const FunctionDecl* functionDecl) MyFuncInfo ExpandableMethods::niceName(const FunctionDecl* functionDecl)
{ {
if (functionDecl->getInstantiatedFromMemberFunction()) if (functionDecl->getInstantiatedFromMemberFunction())
functionDecl = functionDecl->getInstantiatedFromMemberFunction(); functionDecl = functionDecl->getInstantiatedFromMemberFunction();
...@@ -159,7 +159,7 @@ MyFuncInfo InlineableMethods::niceName(const FunctionDecl* functionDecl) ...@@ -159,7 +159,7 @@ MyFuncInfo InlineableMethods::niceName(const FunctionDecl* functionDecl)
return aInfo; return aInfo;
} }
std::string InlineableMethods::toString(SourceLocation loc) std::string ExpandableMethods::toString(SourceLocation loc)
{ {
SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( loc ); SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( loc );
StringRef name = compiler.getSourceManager().getFilename(expansionLoc); StringRef name = compiler.getSourceManager().getFilename(expansionLoc);
...@@ -168,7 +168,7 @@ std::string InlineableMethods::toString(SourceLocation loc) ...@@ -168,7 +168,7 @@ std::string InlineableMethods::toString(SourceLocation loc)
return sourceLocation; return sourceLocation;
} }
bool InlineableMethods::VisitFunctionDecl( const FunctionDecl* functionDecl ) bool ExpandableMethods::VisitFunctionDecl( const FunctionDecl* functionDecl )
{ {
const FunctionDecl* canonicalFunctionDecl = functionDecl->getCanonicalDecl(); const FunctionDecl* canonicalFunctionDecl = functionDecl->getCanonicalDecl();
if (!isCalleeFunctionInteresting(canonicalFunctionDecl)) { if (!isCalleeFunctionInteresting(canonicalFunctionDecl)) {
...@@ -209,43 +209,43 @@ bool InlineableMethods::VisitFunctionDecl( const FunctionDecl* functionDecl ) ...@@ -209,43 +209,43 @@ bool InlineableMethods::VisitFunctionDecl( const FunctionDecl* functionDecl )
return true; return true;
} }
bool InlineableMethods::TraverseFunctionDecl( FunctionDecl* p ) bool ExpandableMethods::TraverseFunctionDecl( FunctionDecl* p )
{ {
mpTraversingFunction = p; maTraversingFunctions.push_back(p);
bool ret = RecursiveASTVisitor::TraverseFunctionDecl(p); bool ret = RecursiveASTVisitor::TraverseFunctionDecl(p);
mpTraversingFunction = nullptr; maTraversingFunctions.pop_back();
return ret; return ret;
} }
bool InlineableMethods::TraverseCXXMethodDecl( CXXMethodDecl* p ) bool ExpandableMethods::TraverseCXXMethodDecl( CXXMethodDecl* p )
{ {
mpTraversingFunction = p; maTraversingFunctions.push_back(p);
bool ret = RecursiveASTVisitor::TraverseCXXMethodDecl(p); bool ret = RecursiveASTVisitor::TraverseCXXMethodDecl(p);
mpTraversingFunction = nullptr; maTraversingFunctions.pop_back();
return ret; return ret;
} }
bool InlineableMethods::TraverseCXXConstructorDecl( CXXConstructorDecl* p ) bool ExpandableMethods::TraverseCXXConstructorDecl( CXXConstructorDecl* p )
{ {
mpTraversingFunction = p; maTraversingFunctions.push_back(p);
bool ret = RecursiveASTVisitor::TraverseCXXConstructorDecl(p); bool ret = RecursiveASTVisitor::TraverseCXXConstructorDecl(p);
mpTraversingFunction = nullptr; maTraversingFunctions.pop_back();
return ret; return ret;
} }
bool InlineableMethods::TraverseCXXConversionDecl( CXXConversionDecl* p ) bool ExpandableMethods::TraverseCXXConversionDecl( CXXConversionDecl* p )
{ {
mpTraversingFunction = p; maTraversingFunctions.push_back(p);
bool ret = RecursiveASTVisitor::TraverseCXXConversionDecl(p); bool ret = RecursiveASTVisitor::TraverseCXXConversionDecl(p);
mpTraversingFunction = nullptr; maTraversingFunctions.pop_back();
return ret; return ret;
} }
bool InlineableMethods::TraverseCXXDestructorDecl( CXXDestructorDecl* p ) bool ExpandableMethods::TraverseCXXDestructorDecl( CXXDestructorDecl* p )
{ {
mpTraversingFunction = p; maTraversingFunctions.push_back(p);
bool ret = RecursiveASTVisitor::TraverseCXXDestructorDecl(p); bool ret = RecursiveASTVisitor::TraverseCXXDestructorDecl(p);
mpTraversingFunction = nullptr; maTraversingFunctions.pop_back();
return ret; return ret;
} }
bool InlineableMethods::VisitMemberExpr( const MemberExpr* memberExpr ) bool ExpandableMethods::VisitMemberExpr( const MemberExpr* memberExpr )
{ {
const FunctionDecl* functionDecl = dyn_cast<FunctionDecl>(memberExpr->getMemberDecl()); const FunctionDecl* functionDecl = dyn_cast<FunctionDecl>(memberExpr->getMemberDecl());
if (functionDecl) { if (functionDecl) {
...@@ -254,7 +254,7 @@ bool InlineableMethods::VisitMemberExpr( const MemberExpr* memberExpr ) ...@@ -254,7 +254,7 @@ bool InlineableMethods::VisitMemberExpr( const MemberExpr* memberExpr )
return true; return true;
} }
bool InlineableMethods::VisitDeclRefExpr( const DeclRefExpr* declRefExpr ) bool ExpandableMethods::VisitDeclRefExpr( const DeclRefExpr* declRefExpr )
{ {
const FunctionDecl* functionDecl = dyn_cast<FunctionDecl>(declRefExpr->getDecl()); const FunctionDecl* functionDecl = dyn_cast<FunctionDecl>(declRefExpr->getDecl());
if (functionDecl) { if (functionDecl) {
...@@ -263,9 +263,9 @@ bool InlineableMethods::VisitDeclRefExpr( const DeclRefExpr* declRefExpr ) ...@@ -263,9 +263,9 @@ bool InlineableMethods::VisitDeclRefExpr( const DeclRefExpr* declRefExpr )
return true; return true;
} }
void InlineableMethods::functionTouchedFromExpr( const FunctionDecl* calleeFunctionDecl, const Expr* expr ) void ExpandableMethods::functionTouchedFromExpr( const FunctionDecl* calleeFunctionDecl, const Expr* expr )
{ {
if (!mpTraversingFunction) { if (maTraversingFunctions.empty()) {
return; return;
} }
const FunctionDecl* canonicalFunctionDecl = calleeFunctionDecl->getCanonicalDecl(); const FunctionDecl* canonicalFunctionDecl = calleeFunctionDecl->getCanonicalDecl();
...@@ -282,7 +282,7 @@ void InlineableMethods::functionTouchedFromExpr( const FunctionDecl* calleeFunct ...@@ -282,7 +282,7 @@ void InlineableMethods::functionTouchedFromExpr( const FunctionDecl* calleeFunct
} }
const CXXMethodDecl* calleeMethodDecl = dyn_cast<CXXMethodDecl>(calleeFunctionDecl); const CXXMethodDecl* calleeMethodDecl = dyn_cast<CXXMethodDecl>(calleeFunctionDecl);
const CXXMethodDecl* callsiteParentMethodDecl = dyn_cast<CXXMethodDecl>(mpTraversingFunction); const CXXMethodDecl* callsiteParentMethodDecl = dyn_cast<CXXMethodDecl>(maTraversingFunctions.back());
if (!callsiteParentMethodDecl if (!callsiteParentMethodDecl
|| calleeMethodDecl->getParent() != callsiteParentMethodDecl->getParent()) || calleeMethodDecl->getParent() != callsiteParentMethodDecl->getParent())
{ {
...@@ -290,7 +290,7 @@ void InlineableMethods::functionTouchedFromExpr( const FunctionDecl* calleeFunct ...@@ -290,7 +290,7 @@ void InlineableMethods::functionTouchedFromExpr( const FunctionDecl* calleeFunct
} }
} }
bool InlineableMethods::isCalleeFunctionInteresting(const FunctionDecl* functionDecl) bool ExpandableMethods::isCalleeFunctionInteresting(const FunctionDecl* functionDecl)
{ {
// ignore stuff that forms part of the stable URE interface // ignore stuff that forms part of the stable URE interface
if (isInUnoIncludeFile(functionDecl)) { if (isInUnoIncludeFile(functionDecl)) {
...@@ -315,7 +315,7 @@ bool InlineableMethods::isCalleeFunctionInteresting(const FunctionDecl* function ...@@ -315,7 +315,7 @@ bool InlineableMethods::isCalleeFunctionInteresting(const FunctionDecl* function
return true; return true;
} }
loplugin::Plugin::Registration< InlineableMethods > X("inlineablemethods", false); loplugin::Plugin::Registration< ExpandableMethods > X("expandablemethods", false);
} }
......
...@@ -27,7 +27,7 @@ def normalizeTypeParams( line ): ...@@ -27,7 +27,7 @@ def normalizeTypeParams( line ):
# The parsing here is designed to avoid grabbing stuff which is mixed in from gbuild. # 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. # I have not yet found a way of suppressing the gbuild output.
with io.open("loplugin.inlineablemethods.log", "rb", buffering=1024*1024) as txt: with io.open("loplugin.expandablemethods.log", "rb", buffering=1024*1024) as txt:
for line in txt: for line in txt:
tokens = line.strip().split("\t") tokens = line.strip().split("\t")
if tokens[0] == "definition:": if tokens[0] == "definition:":
...@@ -142,7 +142,7 @@ for d in definitionSet: ...@@ -142,7 +142,7 @@ for d in definitionSet:
tmp4set.add((method, definitionToSourceLocationMap[d])) tmp4set.add((method, definitionToSourceLocationMap[d]))
# print output, sorted by name and line number # print output, sorted by name and line number
with open("loplugin.inlineablemethods.report", "wt") as f: with open("loplugin.expandablemethods.report", "wt") as f:
for t in sort_set_by_natural_key(tmp4set): for t in sort_set_by_natural_key(tmp4set):
f.write(t[1] + "\n") f.write(t[1] + "\n")
f.write(" " + t[0] + "\n") f.write(" " + t[0] + "\n")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment