Skip to content

Commit 860f557

Browse files
committed
small Check::instances() usage cleanup
- consistently use range loop - fixed `shadowFunction` selfcheck warnings - return a const reference
1 parent eed1daf commit 860f557

6 files changed

Lines changed: 35 additions & 30 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
359359
if (std::strcmp(argv[i], "--doc") == 0) {
360360
std::ostringstream doc;
361361
// Get documentation..
362-
for (const Check * it : Check::instances()) {
363-
const std::string& name(it->name());
364-
const std::string info(it->classInfo());
362+
for (const Check * const c : Check::instances()) {
363+
const std::string& name(c->name());
364+
const std::string info(c->classInfo());
365365
if (!name.empty() && !info.empty())
366366
doc << "## " << name << " ##\n"
367367
<< info << "\n";

lib/check.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ Check::Check(const std::string &aname)
5050
return i->name() > aname;
5151
});
5252
if (it == instances().end())
53-
instances().push_back(this);
53+
instances_internal().push_back(this);
5454
else
55-
instances().insert(it, this);
55+
instances_internal().insert(it, this);
5656
}
5757

5858
void Check::writeToErrorList(const ErrorMessage &errmsg)
@@ -88,7 +88,7 @@ bool Check::wrongData(const Token *tok, const char *str)
8888
return true;
8989
}
9090

91-
std::list<Check *> &Check::instances()
91+
std::list<Check *> &Check::instances_internal()
9292
{
9393
#ifdef __SVR4
9494
// Under Solaris, destructors are called in wrong order which causes a segmentation fault.
@@ -101,6 +101,11 @@ std::list<Check *> &Check::instances()
101101
#endif
102102
}
103103

104+
const std::list<Check *> &Check::instances()
105+
{
106+
return instances_internal();
107+
}
108+
104109
std::string Check::getMessageId(const ValueFlow::Value &value, const char id[])
105110
{
106111
if (value.condition != nullptr)

lib/check.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,20 @@ class CPPCHECKLIB Check {
6666
Check(std::string aname, const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
6767
: mTokenizer(tokenizer), mSettings(settings), mErrorLogger(errorLogger), mName(std::move(aname)) {}
6868

69+
private:
70+
static std::list<Check *> &instances_internal();
71+
6972
public:
7073
virtual ~Check() {
7174
if (!mTokenizer)
72-
instances().remove(this);
75+
instances_internal().remove(this);
7376
}
7477

7578
Check(const Check &) = delete;
7679
Check& operator=(const Check &) = delete;
7780

7881
/** List of registered check classes. This is used by Cppcheck to run checks and generate documentation */
79-
static std::list<Check *> &instances();
82+
static const std::list<Check *> &instances();
8083

8184
/** run checks, the token list is not simplified */
8285
virtual void runChecks(const Tokenizer &, ErrorLogger *) = 0;

lib/cppcheck.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,8 +1338,7 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer, AnalyzerInformation
13381338
const std::time_t maxTime = mSettings.checksMaxTime > 0 ? std::time(nullptr) + mSettings.checksMaxTime : 0;
13391339

13401340
// call all "runChecks" in all registered Check classes
1341-
// cppcheck-suppress shadowFunction - TODO: fix this
1342-
for (Check *check : Check::instances()) {
1341+
for (Check * const c : Check::instances()) {
13431342
if (Settings::terminated())
13441343
return;
13451344

@@ -1357,8 +1356,8 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer, AnalyzerInformation
13571356
return;
13581357
}
13591358

1360-
Timer::run(check->name() + "::runChecks", mTimerResults, [&]() {
1361-
check->runChecks(tokenizer, &mErrorLogger);
1359+
Timer::run(c->name() + "::runChecks", mTimerResults, [&]() {
1360+
c->runChecks(tokenizer, &mErrorLogger);
13621361
});
13631362
}
13641363
}
@@ -1388,11 +1387,10 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer, AnalyzerInformation
13881387
}
13891388

13901389
if (!doUnusedFunctionOnly) {
1391-
// cppcheck-suppress shadowFunction - TODO: fix this
1392-
for (const Check *check : Check::instances()) {
1393-
if (Check::FileInfo * const fi = check->getFileInfo(tokenizer, mSettings, currentConfig)) {
1390+
for (const Check * const c : Check::instances()) {
1391+
if (Check::FileInfo * const fi = c->getFileInfo(tokenizer, mSettings, currentConfig)) {
13941392
if (analyzerInformation)
1395-
analyzerInformation->setFileInfo(check->name(), fi->toString());
1393+
analyzerInformation->setFileInfo(c->name(), fi->toString());
13961394
if (mSettings.useSingleJob())
13971395
mFileInfo.push_back(fi);
13981396
else
@@ -1714,8 +1712,8 @@ void CppCheck::getErrorMessages(ErrorLogger &errorlogger)
17141712
s.addEnabled("all");
17151713

17161714
// call all "getErrorMessages" in all registered Check classes
1717-
for (auto it = Check::instances().cbegin(); it != Check::instances().cend(); ++it)
1718-
(*it)->getErrorMessages(&errorlogger, &s);
1715+
for (const Check * const c : Check::instances())
1716+
c->getErrorMessages(&errorlogger, &s);
17191717

17201718
CheckUnusedFunctions::getErrorMessages(errorlogger);
17211719
Preprocessor::getErrorMessages(errorlogger, s);
@@ -1832,9 +1830,8 @@ bool CppCheck::analyseWholeProgram()
18321830
}
18331831
}
18341832

1835-
// cppcheck-suppress shadowFunction - TODO: fix this
1836-
for (Check *check : Check::instances())
1837-
errors |= check->analyseWholeProgram(ctu, mFileInfo, mSettings, mErrorLogger); // TODO: ctu
1833+
for (Check * const c : Check::instances())
1834+
errors |= c->analyseWholeProgram(ctu, mFileInfo, mSettings, mErrorLogger); // TODO: ctu
18381835
}
18391836

18401837
if (mUnusedFunctionsCheck)
@@ -1881,9 +1878,8 @@ unsigned int CppCheck::analyseWholeProgram(const std::string &buildDir, const st
18811878
}
18821879
else {
18831880
// Analyse the tokens
1884-
// cppcheck-suppress shadowFunction - TODO: fix this
1885-
for (Check *check : Check::instances())
1886-
check->analyseWholeProgram(ctuFileInfo, fileInfoList, mSettings, mErrorLogger);
1881+
for (Check * const c : Check::instances())
1882+
c->analyseWholeProgram(ctuFileInfo, fileInfoList, mSettings, mErrorLogger);
18871883
}
18881884

18891885
for (Check::FileInfo *fi : fileInfoList)

test/testcheck.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,19 @@ class TestCheck : public TestFixture {
3333
}
3434

3535
void instancesSorted() const {
36-
for (auto i = Check::instances().cbegin(); i != Check::instances().cend(); ++i) {
36+
const auto& checks = Check::instances();
37+
for (auto i = checks.cbegin(); i != checks.cend(); ++i) {
3738
auto j = i;
3839
++j;
39-
if (j != Check::instances().cend()) {
40+
if (j != checks.cend()) {
4041
ASSERT_EQUALS(true, (*i)->name() < (*j)->name());
4142
}
4243
}
4344
}
4445

4546
void classInfoFormat() const {
46-
for (auto i = Check::instances().cbegin(); i != Check::instances().cend(); ++i) {
47-
const std::string info = (*i)->classInfo();
47+
for (Check * const c : Check::instances()) {
48+
const std::string info = c->classInfo();
4849
if (!info.empty()) {
4950
ASSERT('\n' != info[0]); // No \n in the beginning
5051
ASSERT('\n' == info.back()); // \n at end

test/testgarbage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ class TestGarbage : public TestFixture {
285285
ASSERT_LOC(tokenizer.tokenize(code), file, line);
286286

287287
// call all "runChecks" in all registered Check classes
288-
for (auto it = Check::instances().cbegin(); it != Check::instances().cend(); ++it) {
289-
(*it)->runChecks(tokenizer, this);
288+
for (Check * const c : Check::instances()) {
289+
c->runChecks(tokenizer, this);
290290
}
291291

292292
return tokenizer.tokens()->stringifyList(false, false, false, true, false, nullptr, nullptr);

0 commit comments

Comments
 (0)