From bfda0dff356a398c4339cdc6b3820271c4368f47 Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 16 Jan 2024 02:06:56 +0100 Subject: [PATCH] pass references into `CheckUnusedFunctions` --- Makefile | 2 +- lib/checkunusedfunctions.cpp | 48 +++++++++++++++++------------------- lib/checkunusedfunctions.h | 12 ++++----- lib/cppcheck.cpp | 10 ++++---- test/testunusedfunctions.cpp | 8 +++--- 5 files changed, 38 insertions(+), 42 deletions(-) diff --git a/Makefile b/Makefile index 0354edde8947..ead92a228974 100644 --- a/Makefile +++ b/Makefile @@ -551,7 +551,7 @@ $(libcppdir)/checktype.o: lib/checktype.cpp lib/addoninfo.h lib/check.h lib/chec $(libcppdir)/checkuninitvar.o: lib/checkuninitvar.cpp lib/addoninfo.h lib/astutils.h lib/check.h lib/checknullpointer.h lib/checkuninitvar.h lib/color.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkuninitvar.cpp -$(libcppdir)/checkunusedfunctions.o: lib/checkunusedfunctions.cpp externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/astutils.h lib/check.h lib/checkunusedfunctions.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h +$(libcppdir)/checkunusedfunctions.o: lib/checkunusedfunctions.cpp externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/astutils.h lib/checkunusedfunctions.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkunusedfunctions.cpp $(libcppdir)/checkunusedvar.o: lib/checkunusedvar.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/astutils.h lib/check.h lib/checkunusedvar.h lib/config.h lib/errortypes.h lib/fwdanalysis.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/vfvalue.h diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 0d6c94ecab5d..1926d0db2110 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -21,7 +21,6 @@ #include "checkunusedfunctions.h" #include "astutils.h" -#include "check.h" #include "errorlogger.h" #include "errortypes.h" #include "library.h" @@ -75,9 +74,9 @@ void CheckUnusedFunctions::clear() instance.mFunctionCalls.clear(); } -void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings *settings) +void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings &settings) { - const bool doMarkup = settings->library.markupFile(FileName); + const bool doMarkup = settings.library.markupFile(FileName); // Function declarations.. if (!doMarkup) { @@ -126,8 +125,8 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi lambdaEndToken = findLambdaEndToken(tok); // parsing of library code to find called functions - if (settings->library.isexecutableblock(FileName, tok->str())) { - const Token * markupVarToken = tok->tokAt(settings->library.blockstartoffset(FileName)); + if (settings.library.isexecutableblock(FileName, tok->str())) { + const Token * markupVarToken = tok->tokAt(settings.library.blockstartoffset(FileName)); // not found if (!markupVarToken) continue; @@ -135,12 +134,12 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi bool start = true; // find all function calls in library code (starts with '(', not if or while etc) while ((scope || start) && markupVarToken) { - if (markupVarToken->str() == settings->library.blockstart(FileName)) { + if (markupVarToken->str() == settings.library.blockstart(FileName)) { scope++; start = false; - } else if (markupVarToken->str() == settings->library.blockend(FileName)) + } else if (markupVarToken->str() == settings.library.blockend(FileName)) scope--; - else if (!settings->library.iskeyword(FileName, markupVarToken->str())) { + else if (!settings.library.iskeyword(FileName, markupVarToken->str())) { mFunctionCalls.insert(markupVarToken->str()); if (mFunctions.find(markupVarToken->str()) != mFunctions.end()) mFunctions[markupVarToken->str()].usedOtherFile = true; @@ -158,10 +157,10 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi } if (!doMarkup // only check source files - && settings->library.isexporter(tok->str()) && tok->next() != nullptr) { + && settings.library.isexporter(tok->str()) && tok->next() != nullptr) { const Token * propToken = tok->next(); while (propToken && propToken->str() != ")") { - if (settings->library.isexportedprefix(tok->str(), propToken->str())) { + if (settings.library.isexportedprefix(tok->str(), propToken->str())) { const Token* nextPropToken = propToken->next(); const std::string& value = nextPropToken->str(); if (mFunctions.find(value) != mFunctions.end()) { @@ -169,7 +168,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi } mFunctionCalls.insert(value); } - if (settings->library.isexportedsuffix(tok->str(), propToken->str())) { + if (settings.library.isexportedsuffix(tok->str(), propToken->str())) { const Token* prevPropToken = propToken->previous(); const std::string& value = prevPropToken->str(); if (value != ")" && mFunctions.find(value) != mFunctions.end()) { @@ -181,7 +180,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi } } - if (doMarkup && settings->library.isimporter(FileName, tok->str()) && tok->next()) { + if (doMarkup && settings.library.isimporter(FileName, tok->str()) && tok->next()) { const Token * propToken = tok->next(); if (propToken->next()) { propToken = propToken->next(); @@ -197,8 +196,8 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi } } - if (settings->library.isreflection(tok->str())) { - const int argIndex = settings->library.reflectionArgument(tok->str()); + if (settings.library.isreflection(tok->str())) { + const int argIndex = settings.library.reflectionArgument(tok->str()); if (argIndex >= 0) { const Token * funcToken = tok->next(); int index = 0; @@ -320,7 +319,7 @@ static bool isOperatorFunction(const std::string & funcName) return std::find(additionalOperators.cbegin(), additionalOperators.cend(), funcName.substr(operatorPrefix.length())) != additionalOperators.cend(); } -bool CheckUnusedFunctions::check(ErrorLogger * const errorLogger, const Settings& settings) const +bool CheckUnusedFunctions::check(ErrorLogger& errorLogger, const Settings& settings) const { using ErrorParams = std::tuple; std::vector errors; // ensure well-defined order @@ -354,7 +353,7 @@ bool CheckUnusedFunctions::check(ErrorLogger * const errorLogger, const Settings return !errors.empty(); } -void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger, +void CheckUnusedFunctions::unusedFunctionError(ErrorLogger& errorLogger, const std::string &filename, unsigned int fileIndex, unsigned int lineNumber, const std::string &funcname) { @@ -365,18 +364,15 @@ void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger, } const ErrorMessage errmsg(locationList, emptyString, Severity::style, "$symbol:" + funcname + "\nThe function '$symbol' is never used.", "unusedFunction", CWE561, Certainty::normal); - if (errorLogger) - errorLogger->reportErr(errmsg); - else - Check::writeToErrorList(errmsg); + errorLogger.reportErr(errmsg); } -void CheckUnusedFunctions::parseTokens(const Tokenizer *tokenizer, const Settings *settings) +void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const Settings &settings) { - if (!settings->checks.isEnabled(Checks::unusedFunction)) + if (!settings.checks.isEnabled(Checks::unusedFunction)) return; - if (settings->useSingleJob() && settings->buildDir.empty()) - instance.parseTokens(*tokenizer, tokenizer->list.getFiles().front().c_str(), settings); + if (settings.useSingleJob() && settings.buildDir.empty()) + instance.parseTokens(tokenizer, tokenizer.list.getFiles().front().c_str(), settings); } bool CheckUnusedFunctions::check(const Settings& settings, ErrorLogger &errorLogger) @@ -384,7 +380,7 @@ bool CheckUnusedFunctions::check(const Settings& settings, ErrorLogger &errorLog // TODO //CheckUnusedFunctions dummy(nullptr, &settings, &errorLogger); //dummy.logChecker("CheckUnusedFunctions::analyseWholeProgram"); - return instance.check(&errorLogger, settings); + return instance.check(errorLogger, settings); } CheckUnusedFunctions::FunctionDecl::FunctionDecl(const Function *f) @@ -414,7 +410,7 @@ namespace { }; } -void CheckUnusedFunctions::analyseWholeProgram(const Settings &settings, ErrorLogger * const errorLogger, const std::string &buildDir) +void CheckUnusedFunctions::analyseWholeProgram(const Settings &settings, ErrorLogger &errorLogger, const std::string &buildDir) { std::map decls; std::set calls; diff --git a/lib/checkunusedfunctions.h b/lib/checkunusedfunctions.h index 0b700012fccb..b628aebef051 100644 --- a/lib/checkunusedfunctions.h +++ b/lib/checkunusedfunctions.h @@ -48,15 +48,15 @@ class CPPCHECKLIB CheckUnusedFunctions { // Parse current tokens and determine.. // * Check what functions are used // * What functions are declared - void parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings *settings); + void parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings &settings); - static void parseTokens(const Tokenizer *tokenizer, const Settings *settings); + static void parseTokens(const Tokenizer &tokenizer, const Settings &settings); std::string analyzerInfo() const; - static void analyseWholeProgram(const Settings &settings, ErrorLogger * const errorLogger, const std::string &buildDir); + static void analyseWholeProgram(const Settings &settings, ErrorLogger& errorLogger, const std::string &buildDir); - static void getErrorMessages(ErrorLogger *errorLogger) { + static void getErrorMessages(ErrorLogger &errorLogger) { unusedFunctionError(errorLogger, emptyString, 0, 0, "funcName"); } @@ -64,9 +64,9 @@ class CPPCHECKLIB CheckUnusedFunctions { private: // Return true if an error is reported. - bool check(ErrorLogger * const errorLogger, const Settings& settings) const; + bool check(ErrorLogger& errorLogger, const Settings& settings) const; - static void unusedFunctionError(ErrorLogger * const errorLogger, + static void unusedFunctionError(ErrorLogger& errorLogger, const std::string &filename, unsigned int fileIndex, unsigned int lineNumber, const std::string &funcname); diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index fecd3d596b30..a466d80d66e5 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -665,7 +665,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string if (mSettings.library.markupFile(filename)) { Tokenizer tokenizer(mSettings, this, &preprocessor); tokenizer.createTokens(std::move(tokens1)); - CheckUnusedFunctions::parseTokens(&tokenizer, &mSettings); + CheckUnusedFunctions::parseTokens(tokenizer, mSettings); return EXIT_SUCCESS; } @@ -928,7 +928,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string // Analyze info.. if (!mSettings.buildDir.empty()) - checkUnusedFunctions.parseTokens(tokenizer, filename.c_str(), &mSettings); + checkUnusedFunctions.parseTokens(tokenizer, filename.c_str(), mSettings); #ifdef HAVE_RULES // handling of "simple" rules has been removed. @@ -1130,7 +1130,7 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer) } } - CheckUnusedFunctions::parseTokens(&tokenizer, &mSettings); + CheckUnusedFunctions::parseTokens(tokenizer, mSettings); } #ifdef HAVE_RULES @@ -1678,7 +1678,7 @@ void CppCheck::getErrorMessages(ErrorLogger &errorlogger) for (std::list::const_iterator it = Check::instances().cbegin(); it != Check::instances().cend(); ++it) (*it)->getErrorMessages(&errorlogger, &s); - CheckUnusedFunctions::getErrorMessages(&errorlogger); + CheckUnusedFunctions::getErrorMessages(errorlogger); Preprocessor::getErrorMessages(&errorlogger, s); } @@ -1791,7 +1791,7 @@ void CppCheck::analyseWholeProgram(const std::string &buildDir, const std::list< return; } if (mSettings.checks.isEnabled(Checks::unusedFunction)) - CheckUnusedFunctions::analyseWholeProgram(mSettings, this, buildDir); + CheckUnusedFunctions::analyseWholeProgram(mSettings, *this, buildDir); std::list fileInfoList; CTU::FileInfo ctuFileInfo; diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index a619216a821d..d5235a7be93d 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -96,9 +96,9 @@ class TestUnusedFunctions : public TestFixture { // Check for unused functions.. CheckUnusedFunctions checkUnusedFunctions; - checkUnusedFunctions.parseTokens(tokenizer, "someFile.c", &settings1); + checkUnusedFunctions.parseTokens(tokenizer, "someFile.c", settings1); // check() returns error if and only if errout is not empty. - if ((checkUnusedFunctions.check)(this, settings1)) { + if ((checkUnusedFunctions.check)(*this, settings1)) { ASSERT(!errout.str().empty()); } else { ASSERT_EQUALS("", errout.str()); @@ -556,11 +556,11 @@ class TestUnusedFunctions : public TestFixture { std::istringstream istr(code); ASSERT(tokenizer2.tokenize(istr, fname.str().c_str())); - c.parseTokens(tokenizer2, "someFile.c", &settings); + c.parseTokens(tokenizer2, "someFile.c", settings); } // Check for unused functions.. - (c.check)(this, settings); + (c.check)(*this, settings); ASSERT_EQUALS("[test1.cpp:1]: (style) The function 'f' is never used.\n", errout.str()); }