Skip to content

Commit 0b9d80c

Browse files
committed
Refactoring CheckUnusedFunctions so it uses new infrastructure for multifile analysis
1 parent cf3f8c2 commit 0b9d80c

10 files changed

+37
-42
lines changed

cli/cppcheckexecutor.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,6 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
790790
c++;
791791
}
792792
}
793-
794-
cppcheck.checkFunctionUsage();
795793
cppcheck.analyseWholeProgram();
796794
} else if (!ThreadExecutor::isEnabled()) {
797795
std::cout << "No thread support yet implemented for this platform." << std::endl;

lib/check.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ class CPPCHECKLIB Check {
9191
virtual ~FileInfo() {}
9292
};
9393

94-
virtual FileInfo * getFileInfo(const Tokenizer *tokenizer) const {
94+
virtual FileInfo * getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const {
9595
(void)tokenizer;
96+
(void)settings;
9697
return nullptr;
9798
}
9899

lib/checkbufferoverrun.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1805,8 +1805,10 @@ void CheckBufferOverrun::writeOutsideBufferSizeError(const Token *tok, const std
18051805
" Please check the second and the third parameter of the function '"+strFunctionName+"'.");
18061806
}
18071807

1808-
Check::FileInfo* CheckBufferOverrun::getFileInfo(const Tokenizer *tokenizer) const
1808+
Check::FileInfo* CheckBufferOverrun::getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const
18091809
{
1810+
(void)settings;
1811+
18101812
MyFileInfo *fileInfo = new MyFileInfo;
18111813

18121814
// Array usage..

lib/checkbufferoverrun.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,10 @@ class CPPCHECKLIB CheckBufferOverrun : public Check {
220220
};
221221

222222
/** @brief Parse current TU and extract file info */
223-
Check::FileInfo *getFileInfo(const Tokenizer *tokenizer) const;
223+
Check::FileInfo *getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const;
224224

225225
/** @brief Analyse all file infos for all TU */
226-
virtual void analyseWholeProgram(const std::list<Check::FileInfo*> &fileInfo, ErrorLogger &errorLogger);
226+
void analyseWholeProgram(const std::list<Check::FileInfo*> &fileInfo, ErrorLogger &errorLogger);
227227

228228
private:
229229

lib/checkuninitvar.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,8 +1013,9 @@ class UninitVar : public ExecutionPath {
10131013
/// @}
10141014

10151015

1016-
Check::FileInfo *CheckUninitVar::getFileInfo(const Tokenizer *tokenizer) const
1016+
Check::FileInfo *CheckUninitVar::getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const
10171017
{
1018+
(void)settings;
10181019
MyFileInfo * mfi = new MyFileInfo;
10191020
analyseFunctions(tokenizer, mfi->uvarFunctions);
10201021
// TODO: add suspicious function calls

lib/checkuninitvar.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ class CPPCHECKLIB CheckUninitVar : public Check {
8080
};
8181

8282
/** @brief Parse current TU and extract file info */
83-
Check::FileInfo *getFileInfo(const Tokenizer *tokenizer) const;
83+
Check::FileInfo *getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const;
8484

8585
/** @brief Analyse all file infos for all TU */
86-
virtual void analyseWholeProgram(const std::list<Check::FileInfo*> &fileInfo, ErrorLogger &errorLogger);
86+
void analyseWholeProgram(const std::list<Check::FileInfo*> &fileInfo, ErrorLogger &errorLogger);
8787

8888
void analyseFunctions(const Tokenizer *tokenizer, std::set<std::string> &f) const;
8989

lib/checkunusedfunctions.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,17 @@ void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger,
263263
else
264264
reportError(errmsg);
265265
}
266+
267+
Check::FileInfo *CheckUnusedFunctions::getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const
268+
{
269+
if (settings->isEnabled("unusedFunction") && settings->_jobs == 1)
270+
instance.parseTokens(*tokenizer, tokenizer->list.getFiles().front().c_str(), settings);
271+
return nullptr;
272+
273+
}
274+
275+
void CheckUnusedFunctions::analyseWholeProgram(const std::list<Check::FileInfo*> &fileInfo, ErrorLogger &errorLogger)
276+
{
277+
(void)fileInfo;
278+
instance.check(&errorLogger);
279+
}

lib/checkunusedfunctions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ class CPPCHECKLIB CheckUnusedFunctions : public Check {
4747

4848
void check(ErrorLogger * const errorLogger);
4949

50+
/** @brief Parse current TU and extract file info */
51+
Check::FileInfo *getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const;
52+
53+
/** @brief Analyse all file infos for all TU */
54+
void analyseWholeProgram(const std::list<Check::FileInfo*> &fileInfo, ErrorLogger &errorLogger);
55+
5056
static CheckUnusedFunctions instance;
5157

5258
private:

lib/cppcheck.cpp

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#include "preprocessor.h" // Preprocessor
2121
#include "tokenize.h" // Tokenizer
22-
#include "checkunusedfunctions.h"
2322

2423
#include "check.h"
2524
#include "path.h"
@@ -278,23 +277,6 @@ void CppCheck::internalError(const std::string &filename, const std::string &msg
278277
}
279278
}
280279

281-
282-
void CppCheck::checkFunctionUsage()
283-
{
284-
// This generates false positives - especially for libraries
285-
if (_settings.isEnabled("unusedFunction") && _settings._jobs == 1) {
286-
const bool verbose_orig = _settings._verbose;
287-
_settings._verbose = false;
288-
289-
if (_settings._errorsOnly == false)
290-
_errorLogger.reportOut("Checking usage of global functions..");
291-
292-
CheckUnusedFunctions::instance.check(this);
293-
294-
_settings._verbose = verbose_orig;
295-
}
296-
}
297-
298280
void CppCheck::analyseFile(std::istream &fin, const std::string &filename)
299281
{
300282
// Preprocess file..
@@ -379,8 +361,12 @@ bool CppCheck::checkFile(const std::string &code, const char FileName[], std::se
379361
(*it)->runChecks(&_tokenizer, &_settings, this);
380362
}
381363

382-
if (_settings.isEnabled("unusedFunction") && _settings._jobs == 1)
383-
CheckUnusedFunctions::instance.parseTokens(_tokenizer, FileName, &_settings);
364+
// Analyse the tokens..
365+
for (std::list<Check *>::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) {
366+
Check::FileInfo *fi = (*it)->getFileInfo(&_tokenizer, &_settings);
367+
if (fi != nullptr)
368+
fileInfo.push_back(fi);
369+
}
384370

385371
executeRules("normal", _tokenizer);
386372

@@ -402,13 +388,6 @@ bool CppCheck::checkFile(const std::string &code, const char FileName[], std::se
402388
(*it)->runSimplifiedChecks(&_tokenizer, &_settings, this);
403389
}
404390

405-
// Analyse the tokens..
406-
for (std::list<Check *>::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) {
407-
Check::FileInfo *fi = (*it)->getFileInfo(&_tokenizer);
408-
if (fi != nullptr)
409-
fileInfo.push_back(fi);
410-
}
411-
412391
if (_settings.terminated())
413392
return true;
414393

lib/cppcheck.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
8282
*/
8383
unsigned int check(const std::string &path, const std::string &content);
8484

85-
/**
86-
* @brief Check function usage.
87-
* @note Call this after all files has been checked
88-
*/
89-
void checkFunctionUsage();
90-
9185
/**
9286
* @brief Get reference to current settings.
9387
* @return a reference to current settings

0 commit comments

Comments
 (0)