Skip to content

Commit 992d8bb

Browse files
committed
stop pretending CheckUnusedFunctions is a real Check
1 parent 51744e6 commit 992d8bb

4 files changed

Lines changed: 49 additions & 75 deletions

File tree

lib/checkunusedfunctions.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "checkunusedfunctions.h"
2222

2323
#include "astutils.h"
24+
#include "check.h"
2425
#include "errorlogger.h"
2526
#include "errortypes.h"
2627
#include "library.h"
@@ -380,8 +381,9 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer *tokenizer, const Setting
380381

381382
bool CheckUnusedFunctions::check(const Settings& settings, ErrorLogger &errorLogger)
382383
{
383-
CheckUnusedFunctions dummy(nullptr, &settings, &errorLogger);
384-
dummy.logChecker("CheckUnusedFunctions::analyseWholeProgram");
384+
// TODO
385+
//CheckUnusedFunctions dummy(nullptr, &settings, &errorLogger);
386+
//dummy.logChecker("CheckUnusedFunctions::analyseWholeProgram");
385387
return instance.check(&errorLogger, settings);
386388
}
387389

@@ -412,7 +414,7 @@ namespace {
412414
};
413415
}
414416

415-
void CheckUnusedFunctions::analyseWholeProgram2(const Settings &settings, ErrorLogger * const errorLogger, const std::string &buildDir)
417+
void CheckUnusedFunctions::analyseWholeProgram(const Settings &settings, ErrorLogger * const errorLogger, const std::string &buildDir)
416418
{
417419
std::map<std::string, Location> decls;
418420
std::set<std::string> calls;

lib/checkunusedfunctions.h

Lines changed: 3 additions & 30 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,24 +34,14 @@ 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 TestUnusedFunctions;
4842

4943
public:
50-
/** @brief This constructor is used when registering the CheckUnusedFunctions */
51-
CheckUnusedFunctions() : Check(myName()) {}
52-
53-
/** @brief This constructor is used when running checks. */
54-
CheckUnusedFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
55-
: Check(myName(), tokenizer, settings, errorLogger) {}
44+
CheckUnusedFunctions() = default;
5645

5746
static void clear();
5847

@@ -65,8 +54,7 @@ class CPPCHECKLIB CheckUnusedFunctions : public Check {
6554

6655
std::string analyzerInfo() const;
6756

68-
/** @brief Combine and analyze all analyzerInfos for all TUs */
69-
static void analyseWholeProgram2(const Settings &settings, ErrorLogger * const errorLogger, const std::string &buildDir);
57+
static void analyseWholeProgram(const Settings &settings, ErrorLogger * const errorLogger, const std::string &buildDir);
7058

7159
static void getErrorMessages(ErrorLogger *errorLogger) {
7260
unusedFunctionError(errorLogger, emptyString, 0, 0, "funcName");
@@ -78,25 +66,10 @@ class CPPCHECKLIB CheckUnusedFunctions : public Check {
7866
// Return true if an error is reported.
7967
bool check(ErrorLogger * const errorLogger, const Settings& settings) const;
8068

81-
void getErrorMessages(ErrorLogger */*errorLogger*/, const Settings * /*settings*/) const override {}
82-
83-
void runChecks(const Tokenizer & /*tokenizer*/, ErrorLogger * /*errorLogger*/) override {}
84-
85-
/**
86-
* Dummy implementation, just to provide error for --errorlist
87-
*/
8869
static void unusedFunctionError(ErrorLogger * const errorLogger,
8970
const std::string &filename, unsigned int fileIndex, unsigned int lineNumber,
9071
const std::string &funcname);
9172

92-
static std::string myName() {
93-
return "Unused functions";
94-
}
95-
96-
std::string classInfo() const override {
97-
return "Check for functions that are never called\n";
98-
}
99-
10073
struct CPPCHECKLIB FunctionUsage {
10174
std::string filename;
10275
unsigned int lineNumber{};

lib/cppcheck.cpp

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
629629
mPlistFile.close();
630630
}
631631

632-
CheckUnusedFunctions checkUnusedFunctions(nullptr, nullptr, nullptr);
632+
CheckUnusedFunctions checkUnusedFunctions;
633633

634634
try {
635635
Preprocessor preprocessor(mSettings, this);
@@ -1069,39 +1069,39 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
10691069
const char* unusedFunctionOnly = std::getenv("UNUSEDFUNCTION_ONLY");
10701070
const bool doUnusedFunctionOnly = unusedFunctionOnly && (std::strcmp(unusedFunctionOnly, "1") == 0);
10711071

1072-
const std::time_t maxTime = mSettings.checksMaxTime > 0 ? std::time(nullptr) + mSettings.checksMaxTime : 0;
1072+
if (!doUnusedFunctionOnly) {
1073+
const std::time_t maxTime = mSettings.checksMaxTime > 0 ? std::time(nullptr) + mSettings.checksMaxTime : 0;
10731074

1074-
// call all "runChecks" in all registered Check classes
1075-
// cppcheck-suppress shadowFunction - TODO: fix this
1076-
for (Check *check : Check::instances()) {
1077-
if (Settings::terminated())
1078-
return;
1079-
1080-
if (maxTime > 0 && std::time(nullptr) > maxTime) {
1081-
if (mSettings.debugwarnings) {
1082-
ErrorMessage::FileLocation loc;
1083-
loc.setfile(tokenizer.list.getFiles()[0]);
1084-
ErrorMessage errmsg({std::move(loc)},
1085-
emptyString,
1086-
Severity::debug,
1087-
"Checks maximum time exceeded",
1088-
"checksMaxTime",
1089-
Certainty::normal);
1090-
reportErr(errmsg);
1075+
// call all "runChecks" in all registered Check classes
1076+
// cppcheck-suppress shadowFunction - TODO: fix this
1077+
for (Check *check : Check::instances()) {
1078+
if (Settings::terminated())
1079+
return;
1080+
1081+
if (maxTime > 0 && std::time(nullptr) > maxTime) {
1082+
if (mSettings.debugwarnings) {
1083+
ErrorMessage::FileLocation loc;
1084+
loc.setfile(tokenizer.list.getFiles()[0]);
1085+
ErrorMessage errmsg({std::move(loc)},
1086+
emptyString,
1087+
Severity::debug,
1088+
"Checks maximum time exceeded",
1089+
"checksMaxTime",
1090+
Certainty::normal);
1091+
reportErr(errmsg);
1092+
}
1093+
return;
10911094
}
1092-
return;
1093-
}
10941095

1095-
if (doUnusedFunctionOnly && dynamic_cast<CheckUnusedFunctions*>(check) == nullptr)
1096-
continue;
1097-
1098-
Timer timerRunChecks(check->name() + "::runChecks", mSettings.showtime, &s_timerResults);
1099-
check->runChecks(tokenizer, this);
1096+
Timer timerRunChecks(check->name() + "::runChecks", mSettings.showtime, &s_timerResults);
1097+
check->runChecks(tokenizer, this);
1098+
}
11001099
}
11011100

1102-
if (mSettings.clang)
1101+
if (mSettings.clang) {
11031102
// TODO: Use CTU for Clang analysis
11041103
return;
1104+
}
11051105

11061106

11071107
if (mSettings.useSingleJob() || !mSettings.buildDir.empty()) {
@@ -1116,18 +1116,17 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
11161116
delete fi1;
11171117
}
11181118

1119-
// cppcheck-suppress shadowFunction - TODO: fix this
1120-
for (const Check *check : Check::instances()) {
1121-
if (doUnusedFunctionOnly && dynamic_cast<const CheckUnusedFunctions*>(check) == nullptr)
1122-
continue;
1123-
1124-
if (Check::FileInfo * const fi = check->getFileInfo(&tokenizer, &mSettings)) {
1125-
if (!mSettings.buildDir.empty())
1126-
mAnalyzerInformation.setFileInfo(check->name(), fi->toString());
1127-
if (mSettings.useSingleJob())
1128-
mFileInfo.push_back(fi);
1129-
else
1130-
delete fi;
1119+
if (!doUnusedFunctionOnly) {
1120+
// cppcheck-suppress shadowFunction - TODO: fix this
1121+
for (const Check *check : Check::instances()) {
1122+
if (Check::FileInfo * const fi = check->getFileInfo(&tokenizer, &mSettings)) {
1123+
if (!mSettings.buildDir.empty())
1124+
mAnalyzerInformation.setFileInfo(check->name(), fi->toString());
1125+
if (mSettings.useSingleJob())
1126+
mFileInfo.push_back(fi);
1127+
else
1128+
delete fi;
1129+
}
11311130
}
11321131
}
11331132

@@ -1792,7 +1791,7 @@ void CppCheck::analyseWholeProgram(const std::string &buildDir, const std::list<
17921791
return;
17931792
}
17941793
if (mSettings.checks.isEnabled(Checks::unusedFunction))
1795-
CheckUnusedFunctions::analyseWholeProgram2(mSettings, this, buildDir);
1794+
CheckUnusedFunctions::analyseWholeProgram(mSettings, this, buildDir);
17961795
std::list<Check::FileInfo*> fileInfoList;
17971796
CTU::FileInfo ctuFileInfo;
17981797

test/testunusedfunctions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class TestUnusedFunctions : public TestFixture {
9595
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
9696

9797
// Check for unused functions..
98-
CheckUnusedFunctions checkUnusedFunctions(&tokenizer, &settings1, this);
98+
CheckUnusedFunctions checkUnusedFunctions;
9999
checkUnusedFunctions.parseTokens(tokenizer, "someFile.c", &settings1);
100100
// check() returns error if and only if errout is not empty.
101101
if ((checkUnusedFunctions.check)(this, settings1)) {
@@ -538,7 +538,7 @@ class TestUnusedFunctions : public TestFixture {
538538

539539
void multipleFiles() {
540540
Tokenizer tokenizer(settings, this);
541-
CheckUnusedFunctions c(&tokenizer, &settings, nullptr);
541+
CheckUnusedFunctions c;
542542

543543
// Clear the error buffer..
544544
errout.str("");

0 commit comments

Comments
 (0)