Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 82 additions & 78 deletions Makefile

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "addoninfo.h"
#include "check.h"
#include "checks.h"
#include "checkers.h"
#include "color.h"
#include "config.h"
Expand Down Expand Up @@ -359,9 +360,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
if (std::strcmp(argv[i], "--doc") == 0) {
std::ostringstream doc;
// Get documentation..
for (const Check * it : Check::instances()) {
const std::string& name(it->name());
const std::string info(it->classInfo());
for (auto *const c : CheckInstances::get()) {
const std::string& name(c->name());
const std::string info(c->classInfo());
if (!name.empty() && !info.empty())
doc << "## " << name << " ##\n"
<< info << "\n";
Expand Down
38 changes: 3 additions & 35 deletions lib/check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,15 @@
#include "tokenize.h"
#include "vfvalue.h"

#include <algorithm>
#include <cctype>
#include <iostream>
#include <stdexcept>
#include <utility>

//---------------------------------------------------------------------------

Check::Check(const std::string &aname)
: mName(aname)
{
{
const auto it = std::find_if(instances().begin(), instances().end(), [&](const Check *i) {
return i->name() == aname;
});
if (it != instances().end())
throw std::runtime_error("'" + aname + "' instance already exists");
}

// make sure the instances are sorted
const auto it = std::find_if(instances().begin(), instances().end(), [&](const Check* i) {
return i->name() > aname;
});
if (it == instances().end())
instances().push_back(this);
else
instances().insert(it, this);
}
Check::Check(std::string aname)
: mName(std::move(aname))
{}

void Check::writeToErrorList(const ErrorMessage &errmsg)
{
Expand Down Expand Up @@ -88,19 +69,6 @@ bool Check::wrongData(const Token *tok, const char *str)
return true;
}

std::list<Check *> &Check::instances()
{
#ifdef __SVR4
// Under Solaris, destructors are called in wrong order which causes a segmentation fault.
// This fix ensures pointer remains valid and reachable until program terminates.
static std::list<Check *> *_instances= new std::list<Check *>;
return *_instances;
#else
static std::list<Check *> _instances;
return _instances;
#endif
}

std::string Check::getMessageId(const ValueFlow::Value &value, const char id[])
{
if (value.condition != nullptr)
Expand Down
10 changes: 2 additions & 8 deletions lib/check.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,19 @@ class Tokenizer;
class CPPCHECKLIB Check {
public:
/** This constructor is used when registering the CheckClass */
explicit Check(const std::string &aname);
explicit Check(std::string aname);

protected:
/** This constructor is used when running checks. */
Check(std::string aname, const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: mTokenizer(tokenizer), mSettings(settings), mErrorLogger(errorLogger), mName(std::move(aname)) {}

public:
virtual ~Check() {
if (!mTokenizer)
instances().remove(this);
}
virtual ~Check() = default;

Check(const Check &) = delete;
Check& operator=(const Check &) = delete;

/** List of registered check classes. This is used by Cppcheck to run checks and generate documentation */
static std::list<Check *> &instances();
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As another positive side effect this no longer needs to return a mutable reference.


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

Expand Down
5 changes: 0 additions & 5 deletions lib/check64bit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
// CWE ids used
static const CWE CWE758(758U); // Reliance on Undefined, Unspecified, or Implementation-Defined Behavior

// Register this check class (by creating a static instance of it)
namespace {
Check64BitPortability instance;
}

static bool is32BitIntegerReturn(const Function* func, const Settings* settings)
{
if (settings->platform.sizeof_pointer != 8)
Expand Down
5 changes: 0 additions & 5 deletions lib/checkassert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@
// CWE ids used
static const CWE CWE398(398U); // Indicator of Poor Code Quality

// Register this check class (by creating a static instance of it)
namespace {
CheckAssert instance;
}

void CheckAssert::assertWithSideEffects()
{
if (!mSettings->severity.isEnabled(Severity::warning))
Expand Down
6 changes: 0 additions & 6 deletions lib/checkautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@

//---------------------------------------------------------------------------


// Register this check class into cppcheck by creating a static instance of it..
namespace {
CheckAutoVariables instance;
}

static const CWE CWE398(398U); // Indicator of Poor Code Quality
static const CWE CWE562(562U); // Return of Stack Variable Address
static const CWE CWE590(590U); // Free of Memory not on the Heap
Expand Down
5 changes: 0 additions & 5 deletions lib/checkbool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@
#include <vector>
//---------------------------------------------------------------------------

// Register this check class (by creating a static instance of it)
namespace {
CheckBool instance;
}

static const CWE CWE398(398U); // Indicator of Poor Code Quality
static const CWE CWE571(571U); // Expression is Always True
static const CWE CWE587(587U); // Assignment of a Fixed Address to a Pointer
Expand Down
7 changes: 0 additions & 7 deletions lib/checkbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@

//---------------------------------------------------------------------------

// Register this check class (by creating a static instance of it)
namespace {
CheckBufferOverrun instance;
}

//---------------------------------------------------------------------------

// CWE ids used:
static const CWE CWE131(131U); // Incorrect Calculation of Buffer Size
static const CWE CWE170(170U); // Improper Null Termination
Expand Down
5 changes: 0 additions & 5 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@

//---------------------------------------------------------------------------

// Register CheckClass..
namespace {
CheckClass instance;
}

static const CWE CWE398(398U); // Indicator of Poor Code Quality
static const CWE CWE404(404U); // Improper Resource Shutdown or Release
static const CWE CWE665(665U); // Improper Initialization
Expand Down
5 changes: 0 additions & 5 deletions lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ static const CWE CWE571(571U); // Expression is Always True

//---------------------------------------------------------------------------

// Register this check class (by creating a static instance of it)
namespace {
CheckCondition instance;
}

bool CheckCondition::diag(const Token* tok, bool insert)
{
if (!tok)
Expand Down
5 changes: 0 additions & 5 deletions lib/checkexceptionsafety.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@

//---------------------------------------------------------------------------

// Register CheckExceptionSafety..
namespace {
CheckExceptionSafety instance;
}

static const CWE CWE398(398U); // Indicator of Poor Code Quality
static const CWE CWE703(703U); // Improper Check or Handling of Exceptional Conditions
static const CWE CWE480(480U); // Use of Incorrect Operator
Expand Down
6 changes: 0 additions & 6 deletions lib/checkfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@

//---------------------------------------------------------------------------


// Register this check class (by creating a static instance of it)
namespace {
CheckFunctions instance;
}

static const CWE CWE252(252U); // Unchecked Return Value
static const CWE CWE477(477U); // Use of Obsolete Functions
static const CWE CWE758(758U); // Reliance on Undefined, Unspecified, or Implementation-Defined Behavior
Expand Down
6 changes: 0 additions & 6 deletions lib/checkinternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@
#include <set>
#include <vector>

// Register this check class (by creating a static instance of it).
// Disabled in release builds
namespace {
CheckInternal instance;
}

void CheckInternal::checkTokenMatchPatterns()
{
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
Expand Down
5 changes: 5 additions & 0 deletions lib/checkinternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#define checkinternalH
//---------------------------------------------------------------------------

#ifdef CHECK_INTERNAL

#include "check.h"
#include "config.h"

Expand Down Expand Up @@ -93,4 +95,7 @@ class CPPCHECKLIB CheckInternal : public Check {
};
/// @}
//---------------------------------------------------------------------------

#endif // CHECK_INTERNAL

#endif // checkinternalH
5 changes: 0 additions & 5 deletions lib/checkio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@

//---------------------------------------------------------------------------

// Register CheckIO..
namespace {
CheckIO instance;
}

// CVE ID used:
static const CWE CWE119(119U); // Improper Restriction of Operations within the Bounds of a Memory Buffer
static const CWE CWE398(398U); // Indicator of Poor Code Quality
Expand Down
5 changes: 0 additions & 5 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@

//---------------------------------------------------------------------------

// Register this check class (by creating a static instance of it)
namespace {
CheckLeakAutoVar instance;
}

static const CWE CWE672(672U);
static const CWE CWE415(415U);

Expand Down
8 changes: 0 additions & 8 deletions lib/checkmemoryleak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@

//---------------------------------------------------------------------------

// Register this check class (by creating a static instance of it)
namespace {
CheckMemoryLeakInFunction instance1;
CheckMemoryLeakInClass instance2;
CheckMemoryLeakStructMember instance3;
CheckMemoryLeakNoVar instance4;
}

// CWE ID used:
static const CWE CWE398(398U); // Indicator of Poor Code Quality
static const CWE CWE401(401U); // Improper Release of Memory Before Removing Last Reference ('Memory Leak')
Expand Down
5 changes: 0 additions & 5 deletions lib/checknullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@
static const CWE CWE_NULL_POINTER_DEREFERENCE(476U);
static const CWE CWE_INCORRECT_CALCULATION(682U);

// Register this check class (by creating a static instance of it)
namespace {
CheckNullPointer instance;
}

//---------------------------------------------------------------------------

static bool checkNullpointerFunctionCallPlausibility(const Function* func, unsigned int arg)
Expand Down
5 changes: 0 additions & 5 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@

//---------------------------------------------------------------------------

// Register this check class (by creating a static instance of it)
namespace {
CheckOther instance;
}

static const CWE CWE128(128U); // Wrap-around Error
static const CWE CWE131(131U); // Incorrect Calculation of Buffer Size
static const CWE CWE197(197U); // Numeric Truncation Error
Expand Down
6 changes: 0 additions & 6 deletions lib/checkpostfixoperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@
//---------------------------------------------------------------------------


// Register this check class (by creating a static instance of it)
namespace {
CheckPostfixOperator instance;
}


// CWE ids used
static const CWE CWE398(398U); // Indicator of Poor Code Quality

Expand Down
Loading
Loading