Skip to content

Commit 60ab00d

Browse files
committed
refs #14498 - do not create static instances of the checks
1 parent eed1daf commit 60ab00d

35 files changed

Lines changed: 222 additions & 274 deletions

Makefile

Lines changed: 82 additions & 78 deletions
Large diffs are not rendered by default.

cli/cmdlineparser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "addoninfo.h"
2222
#include "check.h"
23+
#include "checks.h"
2324
#include "checkers.h"
2425
#include "color.h"
2526
#include "config.h"
@@ -359,7 +360,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
359360
if (std::strcmp(argv[i], "--doc") == 0) {
360361
std::ostringstream doc;
361362
// Get documentation..
362-
for (const Check * it : Check::instances()) {
363+
for (const Check * it : CheckInstances::get()) {
363364
const std::string& name(it->name());
364365
const std::string info(it->classInfo());
365366
if (!name.empty() && !info.empty())

lib/check.cpp

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,15 @@
2626
#include "tokenize.h"
2727
#include "vfvalue.h"
2828

29-
#include <algorithm>
3029
#include <cctype>
3130
#include <iostream>
32-
#include <stdexcept>
3331
#include <utility>
3432

3533
//---------------------------------------------------------------------------
3634

37-
Check::Check(const std::string &aname)
38-
: mName(aname)
39-
{
40-
{
41-
const auto it = std::find_if(instances().begin(), instances().end(), [&](const Check *i) {
42-
return i->name() == aname;
43-
});
44-
if (it != instances().end())
45-
throw std::runtime_error("'" + aname + "' instance already exists");
46-
}
47-
48-
// make sure the instances are sorted
49-
const auto it = std::find_if(instances().begin(), instances().end(), [&](const Check* i) {
50-
return i->name() > aname;
51-
});
52-
if (it == instances().end())
53-
instances().push_back(this);
54-
else
55-
instances().insert(it, this);
56-
}
35+
Check::Check(std::string aname)
36+
: mName(std::move(aname))
37+
{}
5738

5839
void Check::writeToErrorList(const ErrorMessage &errmsg)
5940
{
@@ -88,19 +69,6 @@ bool Check::wrongData(const Token *tok, const char *str)
8869
return true;
8970
}
9071

91-
std::list<Check *> &Check::instances()
92-
{
93-
#ifdef __SVR4
94-
// Under Solaris, destructors are called in wrong order which causes a segmentation fault.
95-
// This fix ensures pointer remains valid and reachable until program terminates.
96-
static std::list<Check *> *_instances= new std::list<Check *>;
97-
return *_instances;
98-
#else
99-
static std::list<Check *> _instances;
100-
return _instances;
101-
#endif
102-
}
103-
10472
std::string Check::getMessageId(const ValueFlow::Value &value, const char id[])
10573
{
10674
if (value.condition != nullptr)

lib/check.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,19 @@ class Tokenizer;
5959
class CPPCHECKLIB Check {
6060
public:
6161
/** This constructor is used when registering the CheckClass */
62-
explicit Check(const std::string &aname);
62+
explicit Check(std::string aname);
6363

6464
protected:
6565
/** This constructor is used when running checks. */
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

6969
public:
70-
virtual ~Check() {
71-
if (!mTokenizer)
72-
instances().remove(this);
73-
}
70+
virtual ~Check() = default;
7471

7572
Check(const Check &) = delete;
7673
Check& operator=(const Check &) = delete;
7774

78-
/** List of registered check classes. This is used by Cppcheck to run checks and generate documentation */
79-
static std::list<Check *> &instances();
80-
8175
/** run checks, the token list is not simplified */
8276
virtual void runChecks(const Tokenizer &, ErrorLogger *) = 0;
8377

lib/check64bit.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
// CWE ids used
3737
static const CWE CWE758(758U); // Reliance on Undefined, Unspecified, or Implementation-Defined Behavior
3838

39-
// Register this check class (by creating a static instance of it)
40-
namespace {
41-
Check64BitPortability instance;
42-
}
43-
4439
static bool is32BitIntegerReturn(const Function* func, const Settings* settings)
4540
{
4641
if (settings->platform.sizeof_pointer != 8)

lib/checkassert.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@
3939
// CWE ids used
4040
static const CWE CWE398(398U); // Indicator of Poor Code Quality
4141

42-
// Register this check class (by creating a static instance of it)
43-
namespace {
44-
CheckAssert instance;
45-
}
46-
4742
void CheckAssert::assertWithSideEffects()
4843
{
4944
if (!mSettings->severity.isEnabled(Severity::warning))

lib/checkautovariables.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@
3939

4040
//---------------------------------------------------------------------------
4141

42-
43-
// Register this check class into cppcheck by creating a static instance of it..
44-
namespace {
45-
CheckAutoVariables instance;
46-
}
47-
4842
static const CWE CWE398(398U); // Indicator of Poor Code Quality
4943
static const CWE CWE562(562U); // Return of Stack Variable Address
5044
static const CWE CWE590(590U); // Free of Memory not on the Heap

lib/checkbool.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@
3232
#include <vector>
3333
//---------------------------------------------------------------------------
3434

35-
// Register this check class (by creating a static instance of it)
36-
namespace {
37-
CheckBool instance;
38-
}
39-
4035
static const CWE CWE398(398U); // Indicator of Poor Code Quality
4136
static const CWE CWE571(571U); // Expression is Always True
4237
static const CWE CWE587(587U); // Assignment of a Fixed Address to a Pointer

lib/checkbufferoverrun.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,6 @@
5050

5151
//---------------------------------------------------------------------------
5252

53-
// Register this check class (by creating a static instance of it)
54-
namespace {
55-
CheckBufferOverrun instance;
56-
}
57-
58-
//---------------------------------------------------------------------------
59-
6053
// CWE ids used:
6154
static const CWE CWE131(131U); // Incorrect Calculation of Buffer Size
6255
static const CWE CWE170(170U); // Improper Null Termination

lib/checkclass.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@
4343

4444
//---------------------------------------------------------------------------
4545

46-
// Register CheckClass..
47-
namespace {
48-
CheckClass instance;
49-
}
50-
5146
static const CWE CWE398(398U); // Indicator of Poor Code Quality
5247
static const CWE CWE404(404U); // Improper Resource Shutdown or Release
5348
static const CWE CWE665(665U); // Improper Initialization

0 commit comments

Comments
 (0)