Skip to content

Commit 40552e2

Browse files
authored
stop pretending CheckUnusedFunctions is a real Check (#5884)
1 parent 0afe8a1 commit 40552e2

5 files changed

Lines changed: 102 additions & 122 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ $(libcppdir)/checktype.o: lib/checktype.cpp lib/addoninfo.h lib/check.h lib/chec
551551
$(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
552552
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkuninitvar.cpp
553553

554-
$(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
554+
$(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
555555
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkunusedfunctions.cpp
556556

557557
$(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

lib/checkunusedfunctions.cpp

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ void CheckUnusedFunctions::clear()
7474
instance.mFunctionCalls.clear();
7575
}
7676

77-
void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings *settings)
77+
void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings &settings)
7878
{
79-
const bool doMarkup = settings->library.markupFile(FileName);
79+
const bool doMarkup = settings.library.markupFile(FileName);
8080

8181
// Function declarations..
8282
if (!doMarkup) {
@@ -125,21 +125,21 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
125125
lambdaEndToken = findLambdaEndToken(tok);
126126

127127
// parsing of library code to find called functions
128-
if (settings->library.isexecutableblock(FileName, tok->str())) {
129-
const Token * markupVarToken = tok->tokAt(settings->library.blockstartoffset(FileName));
128+
if (settings.library.isexecutableblock(FileName, tok->str())) {
129+
const Token * markupVarToken = tok->tokAt(settings.library.blockstartoffset(FileName));
130130
// not found
131131
if (!markupVarToken)
132132
continue;
133133
int scope = 0;
134134
bool start = true;
135135
// find all function calls in library code (starts with '(', not if or while etc)
136136
while ((scope || start) && markupVarToken) {
137-
if (markupVarToken->str() == settings->library.blockstart(FileName)) {
137+
if (markupVarToken->str() == settings.library.blockstart(FileName)) {
138138
scope++;
139139
start = false;
140-
} else if (markupVarToken->str() == settings->library.blockend(FileName))
140+
} else if (markupVarToken->str() == settings.library.blockend(FileName))
141141
scope--;
142-
else if (!settings->library.iskeyword(FileName, markupVarToken->str())) {
142+
else if (!settings.library.iskeyword(FileName, markupVarToken->str())) {
143143
mFunctionCalls.insert(markupVarToken->str());
144144
if (mFunctions.find(markupVarToken->str()) != mFunctions.end())
145145
mFunctions[markupVarToken->str()].usedOtherFile = true;
@@ -157,18 +157,18 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
157157
}
158158

159159
if (!doMarkup // only check source files
160-
&& settings->library.isexporter(tok->str()) && tok->next() != nullptr) {
160+
&& settings.library.isexporter(tok->str()) && tok->next() != nullptr) {
161161
const Token * propToken = tok->next();
162162
while (propToken && propToken->str() != ")") {
163-
if (settings->library.isexportedprefix(tok->str(), propToken->str())) {
163+
if (settings.library.isexportedprefix(tok->str(), propToken->str())) {
164164
const Token* nextPropToken = propToken->next();
165165
const std::string& value = nextPropToken->str();
166166
if (mFunctions.find(value) != mFunctions.end()) {
167167
mFunctions[value].usedOtherFile = true;
168168
}
169169
mFunctionCalls.insert(value);
170170
}
171-
if (settings->library.isexportedsuffix(tok->str(), propToken->str())) {
171+
if (settings.library.isexportedsuffix(tok->str(), propToken->str())) {
172172
const Token* prevPropToken = propToken->previous();
173173
const std::string& value = prevPropToken->str();
174174
if (value != ")" && mFunctions.find(value) != mFunctions.end()) {
@@ -180,7 +180,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
180180
}
181181
}
182182

183-
if (doMarkup && settings->library.isimporter(FileName, tok->str()) && tok->next()) {
183+
if (doMarkup && settings.library.isimporter(FileName, tok->str()) && tok->next()) {
184184
const Token * propToken = tok->next();
185185
if (propToken->next()) {
186186
propToken = propToken->next();
@@ -196,8 +196,8 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
196196
}
197197
}
198198

199-
if (settings->library.isreflection(tok->str())) {
200-
const int argIndex = settings->library.reflectionArgument(tok->str());
199+
if (settings.library.isreflection(tok->str())) {
200+
const int argIndex = settings.library.reflectionArgument(tok->str());
201201
if (argIndex >= 0) {
202202
const Token * funcToken = tok->next();
203203
int index = 0;
@@ -319,7 +319,7 @@ static bool isOperatorFunction(const std::string & funcName)
319319
return std::find(additionalOperators.cbegin(), additionalOperators.cend(), funcName.substr(operatorPrefix.length())) != additionalOperators.cend();
320320
}
321321

322-
bool CheckUnusedFunctions::check(ErrorLogger * const errorLogger, const Settings& settings) const
322+
bool CheckUnusedFunctions::check(ErrorLogger& errorLogger, const Settings& settings) const
323323
{
324324
using ErrorParams = std::tuple<std::string, unsigned int, unsigned int, std::string>;
325325
std::vector<ErrorParams> errors; // ensure well-defined order
@@ -353,7 +353,7 @@ bool CheckUnusedFunctions::check(ErrorLogger * const errorLogger, const Settings
353353
return !errors.empty();
354354
}
355355

356-
void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger,
356+
void CheckUnusedFunctions::unusedFunctionError(ErrorLogger& errorLogger,
357357
const std::string &filename, unsigned int fileIndex, unsigned int lineNumber,
358358
const std::string &funcname)
359359
{
@@ -364,29 +364,27 @@ void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger,
364364
}
365365

366366
const ErrorMessage errmsg(std::move(locationList), emptyString, Severity::style, "$symbol:" + funcname + "\nThe function '$symbol' is never used.", "unusedFunction", CWE561, Certainty::normal);
367-
if (errorLogger)
368-
errorLogger->reportErr(errmsg);
369-
else
370-
Check::writeToErrorList(errmsg);
367+
errorLogger.reportErr(errmsg);
371368
}
372369

373-
Check::FileInfo *CheckUnusedFunctions::getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const
370+
void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const Settings &settings)
374371
{
375-
if (!settings->checks.isEnabled(Checks::unusedFunction))
376-
return nullptr;
377-
if (settings->useSingleJob() && settings->buildDir.empty())
378-
instance.parseTokens(*tokenizer, tokenizer->list.getFiles().front().c_str(), settings);
379-
return nullptr;
372+
if (!settings.checks.isEnabled(Checks::unusedFunction))
373+
return;
374+
if (settings.useSingleJob() && settings.buildDir.empty())
375+
instance.parseTokens(tokenizer, tokenizer.list.getFiles().front().c_str(), settings);
380376
}
381377

382-
bool CheckUnusedFunctions::analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<Check::FileInfo*> &fileInfo, const Settings& settings, ErrorLogger &errorLogger)
378+
#define logChecker(id) \
379+
do { \
380+
const ErrorMessage errmsg({}, nullptr, Severity::internal, "logChecker", (id), CWE(0U), Certainty::normal); \
381+
errorLogger.reportErr(errmsg); \
382+
} while (false)
383+
384+
bool CheckUnusedFunctions::check(const Settings& settings, ErrorLogger &errorLogger)
383385
{
384-
(void)ctu;
385-
(void)fileInfo;
386-
CheckUnusedFunctions dummy(nullptr, &settings, &errorLogger);
387-
dummy.
388-
logChecker("CheckUnusedFunctions::analyseWholeProgram"); // unusedFunctions
389-
return check(&errorLogger, settings);
386+
logChecker("CheckUnusedFunctions::check"); // unusedFunction
387+
return instance.check(errorLogger, settings);
390388
}
391389

392390
CheckUnusedFunctions::FunctionDecl::FunctionDecl(const Function *f)
@@ -416,7 +414,7 @@ namespace {
416414
};
417415
}
418416

419-
void CheckUnusedFunctions::analyseWholeProgram(const Settings &settings, ErrorLogger * const errorLogger, const std::string &buildDir)
417+
void CheckUnusedFunctions::analyseWholeProgram(const Settings &settings, ErrorLogger &errorLogger, const std::string &buildDir)
420418
{
421419
std::map<std::string, Location> decls;
422420
std::set<std::string> calls;

lib/checkunusedfunctions.h

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#define checkunusedfunctionsH
2323
//---------------------------------------------------------------------------
2424

25-
#include "check.h"
2625
#include "config.h"
2726

2827
#include <list>
@@ -35,71 +34,46 @@ class Function;
3534
class Settings;
3635
class Tokenizer;
3736

38-
namespace CTU {
39-
class FileInfo;
40-
}
41-
42-
/// @addtogroup Checks
4337
/** @brief Check for functions never called */
4438
/// @{
4539

46-
class CPPCHECKLIB CheckUnusedFunctions : public Check {
40+
class CPPCHECKLIB CheckUnusedFunctions {
4741
friend class TestSuppressions;
4842
friend class TestSingleExecutorBase;
4943
friend class TestProcessExecutorBase;
5044
friend class TestThreadExecutorBase;
45+
friend class TestUnusedFunctions;
5146

5247
public:
53-
/** @brief This constructor is used when registering the CheckUnusedFunctions */
54-
CheckUnusedFunctions() : Check(myName()) {}
55-
56-
/** @brief This constructor is used when running checks. */
57-
CheckUnusedFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
58-
: Check(myName(), tokenizer, settings, errorLogger) {}
48+
CheckUnusedFunctions() = default;
5949

6050
// Parse current tokens and determine..
6151
// * Check what functions are used
6252
// * What functions are declared
63-
void parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings *settings);
53+
void parseTokens(const Tokenizer &tokenizer, const char FileName[], const Settings &settings);
6454

65-
// Return true if an error is reported.
66-
bool check(ErrorLogger * const errorLogger, const Settings& settings) const;
55+
static void parseTokens(const Tokenizer &tokenizer, const Settings &settings);
6756

68-
/** @brief Parse current TU and extract file info */
69-
Check::FileInfo *getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const override;
57+
std::string analyzerInfo() const;
7058

71-
/** @brief Analyse all file infos for all TU */
72-
bool analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<Check::FileInfo*> &fileInfo, const Settings& settings, ErrorLogger &errorLogger) override;
59+
static void analyseWholeProgram(const Settings &settings, ErrorLogger& errorLogger, const std::string &buildDir);
7360

74-
std::string analyzerInfo() const;
61+
static void getErrorMessages(ErrorLogger &errorLogger) {
62+
unusedFunctionError(errorLogger, emptyString, 0, 0, "funcName");
63+
}
7564

76-
/** @brief Combine and analyze all analyzerInfos for all TUs */
77-
static void analyseWholeProgram(const Settings &settings, ErrorLogger * const errorLogger, const std::string &buildDir);
65+
static bool check(const Settings& settings, ErrorLogger &errorLogger);
7866

7967
private:
8068
static void clear();
8169

82-
void getErrorMessages(ErrorLogger *errorLogger, const Settings * /*settings*/) const override {
83-
CheckUnusedFunctions::unusedFunctionError(errorLogger, emptyString, 0, 0, "funcName");
84-
}
85-
86-
void runChecks(const Tokenizer & /*tokenizer*/, ErrorLogger * /*errorLogger*/) override {}
70+
// Return true if an error is reported.
71+
bool check(ErrorLogger& errorLogger, const Settings& settings) const;
8772

88-
/**
89-
* Dummy implementation, just to provide error for --errorlist
90-
*/
91-
static void unusedFunctionError(ErrorLogger * const errorLogger,
73+
static void unusedFunctionError(ErrorLogger& errorLogger,
9274
const std::string &filename, unsigned int fileIndex, unsigned int lineNumber,
9375
const std::string &funcname);
9476

95-
static std::string myName() {
96-
return "Unused functions";
97-
}
98-
99-
std::string classInfo() const override {
100-
return "Check for functions that are never called\n";
101-
}
102-
10377
struct CPPCHECKLIB FunctionUsage {
10478
std::string filename;
10579
unsigned int lineNumber{};

0 commit comments

Comments
 (0)