Skip to content

Commit

Permalink
pass references into CheckUnusedFunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Jan 17, 2024
1 parent 1e8e988 commit bfda0df
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 22 additions & 26 deletions lib/checkunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "checkunusedfunctions.h"

#include "astutils.h"
#include "check.h"
#include "errorlogger.h"
#include "errortypes.h"
#include "library.h"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -126,21 +125,21 @@ 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;
int scope = 0;
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;
Expand All @@ -158,18 +157,18 @@ 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()) {
mFunctions[value].usedOtherFile = true;
}
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()) {
Expand All @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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::string, unsigned int, unsigned int, std::string>;
std::vector<ErrorParams> errors; // ensure well-defined order
Expand Down Expand Up @@ -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)
{
Expand All @@ -365,26 +364,23 @@ 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)
{
// 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)
Expand Down Expand Up @@ -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<std::string, Location> decls;
std::set<std::string> calls;
Expand Down
12 changes: 6 additions & 6 deletions lib/checkunusedfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,25 @@ 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");
}

static bool check(const Settings& settings, ErrorLogger &errorLogger);

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);

Expand Down
10 changes: 5 additions & 5 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -1130,7 +1130,7 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
}
}

CheckUnusedFunctions::parseTokens(&tokenizer, &mSettings);
CheckUnusedFunctions::parseTokens(tokenizer, mSettings);
}

#ifdef HAVE_RULES
Expand Down Expand Up @@ -1678,7 +1678,7 @@ void CppCheck::getErrorMessages(ErrorLogger &errorlogger)
for (std::list<Check *>::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);
}

Expand Down Expand Up @@ -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<Check::FileInfo*> fileInfoList;
CTU::FileInfo ctuFileInfo;

Expand Down
8 changes: 4 additions & 4 deletions test/testunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit bfda0df

Please sign in to comment.