/* -*- 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: */ pick'>feature/5-1-pick LibreOffice 核心代码仓库文档基金会
summaryrefslogtreecommitdiff
AgeCommit message (Expand)Author
2024-02-01tdf#140004 Toggle comment in the Basic IDERafael Lima
2024-01-08tdf#156925 Add Color Scheme selector to the Basic IDERafael Lima
2023-02-23tdf#146518 Implement commands to toggle Watch/Stack windowsRafael Lima
2019-09-23Resolves tdf#127400 - Allow to show tip of the day againHeiko Tietze
2018-10-09tdf#120264 - Access to "Get Involved"heiko tietze
2018-06-20menubar basic: update menubar according swriterandreas kainz
2018-06-14menubar: update basic/macro menubarandreas kainz
2017-08-10Reorganize the main menu Basic IDEtagezi
2017-07-04tdf#105831 Add Donate entry on Help menuOlivier
2017-05-23The menu items are moved according to a general design concepttagezi
2017-02-05.uno:ScriptOrganizer should be menu:menuitemMaxim Monastirsky
2016-10-19tdf#96015 (part) link to Doc'tation in Help menuOlivier Hallot
2016-10-10safemode: Add uno command and menu entrySamuel Mehrbrodt
2016-08-11tdf#96015 (part) links to Askbot in the Help menuOlivier Hallot
2015-09-25tdf#94497 UI: Object catalog not available in menu > View in BasicIDECor Nouws
2013-09-02GSOC work, Tool/Options/Basic IDE Options createdGergo Mocsi
2013-09-02GSOC work, renaming for consistencyGergo Mocsi
2013-09-02GSOC work, ModalDialog instead of menu entryGergo Mocsi
2013-09-02GSOC work menu entry + code fixGergo Mocsi
2012-12-20Templates Manager: removed the old dialogs and menu entriesCédric Bosdonnat
2012-10-31CMIS: added cancelCheckOut and checkIn implementations and menu itemsCédric Bosdonnat
2012-10-31CMIS: added CheckOut in the File menuCédric Bosdonnat
2012-07-03Remove duplication of License/Credits informationStefan Knorr (astron)
2012-06-19Revert "Remove License/Credits menu items"Petr Mladek
2012-06-18Remove License/Credits menu itemsStefan Knorr (astron)
2012-06-12re-base on ALv2 code.Michael Meeks
2012-01-23add the "Send Feedback..." help menu itemIvan Timofeev
2011-12-04String change for Line Number functionality in Basic IDEStefan Knorr (astron)
2011-11-17Preparing to add option for line numbers in the basic ideAugust Sodora
2011-11-16add new 'Goto Line' menu item to Basic IDENoel Power
2011-09-12remove Repeat from Basic IDE menuAndras Timar
2011-03-03findbar: Add the findbar to the menus above the Search dialog.Jan Holesovsky
2010-11-25Put show credits entry into all app ui configsThorsten Behrens
2010-09-28Replace the "Help Support" menu item by the "License Information" oneFridrich Štrba
2010-09-15default-no-registration-menu.diff: Remove registration from menu.Michael Meeks
2006-08-04INTEGRATION: CWS onlineupdate3 (1.10.34); FILE MERGEDIvo Hinkelmann
2006-04-26INTEGRATION: CWS menucleanup (1.8.140); FILE MERGEDKurt Zenker
2006-04-07INTEGRATION: CWS onlineupdate1 (1.8.138); FILE MERGEDVladimir Glazounov
2004-11-16INTEGRATION: CWS docking4 (1.7.10); FILE MERGEDOliver Bolte
2004-08-31INTEGRATION: CWS xmlsec05 (1.4.4); FILE MERGEDKurt Zenker
2004-08-11INTEGRATION: CWS pbhelp02 (1.3.2); FILE MERGEDOliver Bolte
2004-08-02INTEGRATION: CWS os35 (1.3.22); FILE MERGEDKurt Zenker