From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- .../clang/countusersofdefaultparams.cxx | 250 +++++++++++++++++++++ 1 file changed, 250 insertions(+) create mode 100644 compilerplugins/clang/countusersofdefaultparams.cxx (limited to 'compilerplugins/clang/countusersofdefaultparams.cxx') diff --git a/compilerplugins/clang/countusersofdefaultparams.cxx b/compilerplugins/clang/countusersofdefaultparams.cxx new file mode 100644 index 000000000..c336509b3 --- /dev/null +++ b/compilerplugins/clang/countusersofdefaultparams.cxx @@ -0,0 +1,250 @@ +/* -*- 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 +#include +#include +#include + +#include "clang/AST/Attr.h" + +#include "config_clang.h" + +#include "plugin.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 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 callSet; +static std::set definitionSet; + +class CountUsersOfDefaultParams: + public RecursiveASTVisitor, public loplugin::Plugin +{ +public: + explicit CountUsersOfDefaultParams(loplugin::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"; + std::ofstream myfile; + myfile.open( WORKDIR "/loplugin.countusersofdefaultparams.log", std::ios::app | std::ios::out); + myfile << output; + myfile.close(); + } + + bool shouldVisitTemplateInstantiations () const { return true; } + + bool VisitCallExpr( const CallExpr * ); + bool VisitFunctionDecl( const FunctionDecl* ); + bool VisitCXXConstructExpr( const CXXConstructExpr * ); +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->getTemplateInstantiationPattern()) + functionDecl = functionDecl->getTemplateInstantiationPattern(); + + 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 = functionDecl->getReturnType().getCanonicalType().getAsString(); + + if (isa(functionDecl)) { + const CXXRecordDecl* recordDecl = dyn_cast(functionDecl)->getParent(); + aInfo.nameAndParams += recordDecl->getQualifiedNameAsString(); + aInfo.nameAndParams += "::"; + } + aInfo.nameAndParams += functionDecl->getNameAsString() + "("; + bool bFirst = true; + for (const ParmVarDecl *pParmVarDecl : functionDecl->parameters()) { + if (bFirst) + bFirst = false; + else + aInfo.nameAndParams += ","; + aInfo.nameAndParams += pParmVarDecl->getType().getCanonicalType().getAsString(); + } + aInfo.nameAndParams += ")"; + if (isa(functionDecl) && dyn_cast(functionDecl)->isConst()) { + aInfo.nameAndParams += " const"; + } + + aInfo.sourceLocation = locationToString(functionDecl->getLocation()); + loplugin::normalizeDotDotInFilePath(aInfo.sourceLocation); +} + +bool CountUsersOfDefaultParams::VisitCallExpr(const CallExpr * callExpr) { + if (ignoreLocation(callExpr)) { + return true; + } + const FunctionDecl* functionDecl; + if (isa(callExpr)) { + functionDecl = dyn_cast(callExpr)->getMethodDecl(); + } + else { + functionDecl = callExpr->getDirectCallee(); + } + if (functionDecl == nullptr) { + return true; + } + functionDecl = functionDecl->getCanonicalDecl(); + // work our way up to the root method + while (isa(functionDecl)) { + const CXXMethodDecl* methodDecl = dyn_cast(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->getTemplateInstantiationPattern()) + functionDecl = functionDecl->getTemplateInstantiationPattern(); + 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->getBeginLoc()); + callSet.insert(callInfo); + } + return true; +} + +bool CountUsersOfDefaultParams::VisitCXXConstructExpr(const CXXConstructExpr * constructExpr) { + if (ignoreLocation(constructExpr)) { + return true; + } + const CXXConstructorDecl* constructorDecl = constructExpr->getConstructor()->getCanonicalDecl(); + // work our way back to the root definition for template methods + if (constructorDecl->getInstantiatedFromMemberFunction()) + constructorDecl = dyn_cast(constructorDecl->getInstantiatedFromMemberFunction()); + else if (constructorDecl->getTemplateInstantiationPattern()) + constructorDecl = dyn_cast(constructorDecl->getTemplateInstantiationPattern()); + int n = constructorDecl->getNumParams() - 1; + if (n < 0 || !constructorDecl->getParamDecl(n)->hasDefaultArg()) { + return true; + } + while (n > 0 && constructorDecl->getParamDecl(n-1)->hasDefaultArg()) { + --n; + } + // look for callsites that are actually using the defaulted values + if ( n < (int)constructExpr->getNumArgs() && constructExpr->getArg(n)->isDefaultArgument()) { + MyCallInfo callInfo; + niceName(constructorDecl, callInfo); + callInfo.sourceLocationOfCall = locationToString(constructExpr->getBeginLoc()); + callSet.insert(callInfo); + } + return true; +} + +std::string CountUsersOfDefaultParams::locationToString(const SourceLocation& sourceLoc) +{ + SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( sourceLoc ); + StringRef name = getFilenameOfLocation(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(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())) { + return true; + } + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(functionDecl)) { + return true; + } + if (isa(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: */ -- cgit v1.2.3