Skip to content

Commit

Permalink
unused
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Feb 12, 2024
1 parent 83e3cca commit a3ebea2
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 68 deletions.
40 changes: 11 additions & 29 deletions lib/checkunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ namespace CTU {

//---------------------------------------------------------------------------

// Register this check class
namespace {
CheckUnusedFunctions instance;
}

static const CWE CWE561(561U); // Dead Code

static std::string stripTemplateParameters(const std::string& funcName) {
Expand All @@ -68,14 +63,10 @@ static std::string stripTemplateParameters(const std::string& funcName) {
// FUNCTION USAGE - Check for unused functions etc
//---------------------------------------------------------------------------

void CheckUnusedFunctions::clear()
void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const Settings &settings)
{
instance.mFunctions.clear();
instance.mFunctionCalls.clear();
}
const char * const FileName = tokenizer.list.getFiles().front().c_str();

void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings &settings)
{
const bool doMarkup = settings.library.markupFile(FileName);

// Function declarations..
Expand Down Expand Up @@ -319,8 +310,16 @@ static bool isOperatorFunction(const std::string & funcName)
return std::find(additionalOperators.cbegin(), additionalOperators.cend(), funcName.substr(operatorPrefix.length())) != additionalOperators.cend();
}

bool CheckUnusedFunctions::check(ErrorLogger& errorLogger, const Settings& settings) const
#define logChecker(id) \
do { \
const ErrorMessage errmsg({}, nullptr, Severity::internal, "logChecker", (id), CWE(0U), Certainty::normal); \
errorLogger.reportErr(errmsg); \
} while (false)

bool CheckUnusedFunctions::check(const Settings& settings, ErrorLogger& errorLogger) const
{
logChecker("CheckUnusedFunctions::check"); // unusedFunction

using ErrorParams = std::tuple<std::string, unsigned int, unsigned int, std::string>;
std::vector<ErrorParams> errors; // ensure well-defined order

Expand Down Expand Up @@ -367,23 +366,6 @@ void CheckUnusedFunctions::unusedFunctionError(ErrorLogger& errorLogger,
errorLogger.reportErr(errmsg);
}

void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const Settings &settings)
{
instance.parseTokens(tokenizer, tokenizer.list.getFiles().front().c_str(), settings);
}

#define logChecker(id) \
do { \
const ErrorMessage errmsg({}, nullptr, Severity::internal, "logChecker", (id), CWE(0U), Certainty::normal); \
errorLogger.reportErr(errmsg); \
} while (false)

bool CheckUnusedFunctions::check(const Settings& settings, ErrorLogger &errorLogger)
{
logChecker("CheckUnusedFunctions::check"); // unusedFunction
return instance.check(errorLogger, settings);
}

CheckUnusedFunctions::FunctionDecl::FunctionDecl(const Function *f)
: functionName(f->name()), lineNumber(f->token->linenr())
{}
Expand Down
12 changes: 3 additions & 9 deletions lib/checkunusedfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ 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);

static void parseTokens(const Tokenizer &tokenizer, const Settings &settings);
void parseTokens(const Tokenizer &tokenizer, const Settings &settings);

std::string analyzerInfo() const;

Expand All @@ -62,14 +60,10 @@ class CPPCHECKLIB CheckUnusedFunctions {
unusedFunctionError(errorLogger, emptyString, 0, 0, "funcName");
}

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

private:
static void clear();

// Return true if an error is reported.
bool check(ErrorLogger& errorLogger, const Settings& settings) const;
bool check(const Settings& settings, ErrorLogger& errorLogger) const;

private:
static void unusedFunctionError(ErrorLogger& errorLogger,
const std::string &filename, unsigned int fileIndex, unsigned int lineNumber,
const std::string &funcname);
Expand Down
22 changes: 12 additions & 10 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ static bool reportClangErrors(std::istream &is, const std::function<void(const E
return false;
}

unsigned int CppCheck::checkClang(const std::string &path)
unsigned int CppCheck::checkClang(const std::string &path, CheckUnusedFunctions * const unusedFunctionsCheckerGlobal)
{
if (!mSettings.quiet)
mErrorLogger.reportOut(std::string("Checking ") + path + " ...", Color::FgGreen);
Expand Down Expand Up @@ -507,7 +507,7 @@ unsigned int CppCheck::checkClang(const std::string &path)
&s_timerResults);
if (mSettings.debugnormal)
tokenizer.printDebugOutput(1);
checkNormalTokens(tokenizer);
checkNormalTokens(tokenizer, unusedFunctionsCheckerGlobal);

// create dumpfile
std::ofstream fdump;
Expand Down Expand Up @@ -544,7 +544,7 @@ unsigned int CppCheck::checkClang(const std::string &path)
return mExitCode;
}

unsigned int CppCheck::check(const std::string &path)
unsigned int CppCheck::check(const std::string &path, CheckUnusedFunctions * const unusedFunctionsCheckerGlobal)
{
if (mSettings.clang)
return checkClang(path);
Expand Down Expand Up @@ -631,6 +631,8 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
mPlistFile.close();
}

CheckUnusedFunctions& unusedFunctionsGlobal = CheckUnusedFunctions::global();

try {
if (mSettings.library.markupFile(filename)) {
if (mSettings.checks.isEnabled(Checks::unusedFunction) &&
Expand All @@ -643,7 +645,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
std::ifstream in(filename);
tokenizer.list.createTokens(in, filename);
}
CheckUnusedFunctions::parseTokens(tokenizer, mSettings);
unusedFunctionsGlobal.parseTokens(tokenizer, mSettings);
}
return EXIT_SUCCESS;
}
Expand Down Expand Up @@ -933,7 +935,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
}

// Check normal tokens
checkNormalTokens(tokenizer);
checkNormalTokens(tokenizer, &unusedFunctionsGlobal);

#ifdef HAVE_RULES
// handling of "simple" rules has been removed.
Expand Down Expand Up @@ -1067,7 +1069,7 @@ void CppCheck::checkRawTokens(const Tokenizer &tokenizer)
// CppCheck - A function that checks a normal token list
//---------------------------------------------------------------------------

void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
void CppCheck::checkNormalTokens(const Tokenizer &tokenizer, CheckUnusedFunctions * const unusedFunctionsCheckerGlobal)
{
CheckUnusedFunctions unusedFunctionsChecker;

Expand Down Expand Up @@ -1105,12 +1107,12 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
}

if (mSettings.checks.isEnabled(Checks::unusedFunction) && !mSettings.buildDir.empty()) {
unusedFunctionsChecker.parseTokens(tokenizer, tokenizer.list.getFiles().front().c_str(), mSettings);
unusedFunctionsChecker.parseTokens(tokenizer, mSettings);
}
if (mSettings.checks.isEnabled(Checks::unusedFunction) &&
mSettings.useSingleJob() &&
mSettings.buildDir.empty()) {
CheckUnusedFunctions::parseTokens(tokenizer, mSettings);
unusedFunctionsCheckerGlobal->parseTokens(tokenizer, mSettings);
}

if (mSettings.clang) {
Expand Down Expand Up @@ -1794,7 +1796,7 @@ bool CppCheck::analyseWholeProgram()
errors |= check->analyseWholeProgram(&ctu, mFileInfo, mSettings, *this); // TODO: ctu

if (mSettings.checks.isEnabled(Checks::unusedFunction))
errors |= CheckUnusedFunctions::check(mSettings, *this);
errors |= CheckUnusedFunctions::global().check(mSettings, *this);

return errors && (mExitCode > 0);
}
Expand Down Expand Up @@ -1861,7 +1863,7 @@ void CppCheck::analyseWholeProgram(const std::string &buildDir, const std::list<
check->analyseWholeProgram(&ctuFileInfo, fileInfoList, mSettings, *this);

if (mSettings.checks.isEnabled(Checks::unusedFunction))
CheckUnusedFunctions::check(mSettings, *this);
CheckUnusedFunctions::global().check(mSettings, *this);

for (Check::FileInfo *fi : fileInfoList)
delete fi;
Expand Down
7 changes: 4 additions & 3 deletions lib/cppcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
class Tokenizer;
enum class SHOWTIME_MODES;
struct FileSettings;
class CheckUnusedFunctions;

/// @addtogroup Core
/// @{
Expand Down Expand Up @@ -82,7 +83,7 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
* @note You must set settings before calling this function (by calling
* settings()).
*/
unsigned int check(const std::string &path);
unsigned int check(const std::string &path, CheckUnusedFunctions * const unusedFunctionsCheckerGlobal);
unsigned int check(const FileSettings &fs);

/**
Expand Down Expand Up @@ -182,7 +183,7 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
* @brief Check normal tokens
* @param tokenizer tokenizer instance
*/
void checkNormalTokens(const Tokenizer &tokenizer);
void checkNormalTokens(const Tokenizer &tokenizer, CheckUnusedFunctions * const unusedFunctionsCheckerGlobal);

/**
* Execute addons
Expand All @@ -204,7 +205,7 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
void executeRules(const std::string &tokenlist, const Tokenizer &tokenizer);
#endif

unsigned int checkClang(const std::string &path);
unsigned int checkClang(const std::string &path, CheckUnusedFunctions * const unusedFunctionsCheckerGlobal);

/**
* @brief Errors and warnings are directed here.
Expand Down
2 changes: 1 addition & 1 deletion test/testcppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class TestCppcheck : public TestFixture {

ErrorLogger2 errorLogger;
CppCheck cppcheck(errorLogger, false, {});
ASSERT_EQUALS(1, cppcheck.check(file.path()));
ASSERT_EQUALS(1, cppcheck.check(file.path(), nullptr));
// TODO: how to properly disable these warnings?
errorLogger.ids.erase(std::remove_if(errorLogger.ids.begin(), errorLogger.ids.end(), [](const std::string& id) {
return id == "logChecker";
Expand Down
2 changes: 0 additions & 2 deletions test/testprocessexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ class TestProcessExecutorBase : public TestFixture {
void check(unsigned int jobs, int files, int result, const std::string &data, const CheckOptions& opt = make_default_obj{}) {
errout.str("");

CheckUnusedFunctions::clear();

std::list<FileSettings> fileSettings;

std::list<std::pair<std::string, std::size_t>> filelist;
Expand Down
2 changes: 0 additions & 2 deletions test/testsingleexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ class TestSingleExecutorBase : public TestFixture {
void check(int files, int result, const std::string &data, const CheckOptions& opt = make_default_obj{}) {
errout.str("");

CheckUnusedFunctions::clear();

std::list<FileSettings> fileSettings;

std::list<std::pair<std::string, std::size_t>> filelist;
Expand Down
6 changes: 0 additions & 6 deletions test/testsuppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ class TestSuppressions : public TestFixture {
// Clear the error log
errout.str("");

CheckUnusedFunctions::clear();

std::list<FileSettings> fileSettings;

std::list<std::pair<std::string, std::size_t>> filelist;
Expand Down Expand Up @@ -284,8 +282,6 @@ class TestSuppressions : public TestFixture {
unsigned int _checkSuppressionThreads(const char code[], bool useFS, const std::string &suppression = emptyString) {
errout.str("");

CheckUnusedFunctions::clear();

std::list<FileSettings> fileSettings;

std::list<std::pair<std::string, std::size_t>> filelist;
Expand Down Expand Up @@ -334,8 +330,6 @@ class TestSuppressions : public TestFixture {
unsigned int _checkSuppressionProcesses(const char code[], bool useFS, const std::string &suppression = emptyString) {
errout.str("");

CheckUnusedFunctions::clear();

std::list<FileSettings> fileSettings;

std::list<std::pair<std::string, std::size_t>> filelist;
Expand Down
2 changes: 0 additions & 2 deletions test/testthreadexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ class TestThreadExecutorBase : public TestFixture {
void check(unsigned int jobs, int files, int result, const std::string &data, const CheckOptions& opt = make_default_obj{}) {
errout.str("");

CheckUnusedFunctions::clear();

std::list<FileSettings> fileSettings;

std::list<std::pair<std::string, std::size_t>> filelist;
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, settings1);
// check() returns error if and only if errout is not empty.
if ((checkUnusedFunctions.check)(*this, settings1)) {
if ((checkUnusedFunctions.check)(settings1, *this)) {
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, settings);
}

// Check for unused functions..
(c.check)(*this, settings);
(c.check)(settings, *this);

ASSERT_EQUALS("[test1.cpp:1]: (style) The function 'f' is never used.\n", errout.str());
}
Expand Down

0 comments on commit a3ebea2

Please sign in to comment.