Skip to content

Commit

Permalink
refactor: move and rename code in order to support a CMAKE_UNITY_BUILD
Browse files Browse the repository at this point in the history
  • Loading branch information
tnixeu committed Mar 9, 2024
1 parent f97bab8 commit c198f21
Show file tree
Hide file tree
Showing 33 changed files with 220 additions and 208 deletions.
4 changes: 1 addition & 3 deletions lib/check64bit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@

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

// 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;
Check64BitPortability instance64BitPortability;
}

void Check64BitPortability::pointerassignment()
Expand Down
4 changes: 1 addition & 3 deletions lib/checkassert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@

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

// 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;
CheckAssert instanceAssert;
}

void CheckAssert::assertWithSideEffects()
Expand Down
5 changes: 1 addition & 4 deletions lib/checkautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@

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

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

static bool isPtrArg(const Token *tok)
{
Expand Down
6 changes: 1 addition & 5 deletions lib/checkbool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,9 @@

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

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
static const CWE CWE704(704U); // Incorrect Type Conversion or Cast

static bool isBool(const Variable* var)
{
Expand Down
3 changes: 1 addition & 2 deletions lib/checkboost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@

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

static const CWE CWE664(664);

void CheckBoost::checkBoostForeachModification()
{
Expand Down
21 changes: 6 additions & 15 deletions lib/checkbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,11 @@

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

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

// CWE ids used:
static const CWE CWE131(131U); // Incorrect Calculation of Buffer Size
static const CWE CWE170(170U); // Improper Null Termination
static const CWE CWE_ARGUMENT_SIZE(398U); // Indicator of Poor Code Quality
static const CWE CWE_ARRAY_INDEX_THEN_CHECK(398U); // Indicator of Poor Code Quality
static const CWE CWE758(758U); // Reliance on Undefined, Unspecified, or Implementation-Defined Behavior
static const CWE CWE_POINTER_ARITHMETIC_OVERFLOW(758U); // Reliance on Undefined, Unspecified, or Implementation-Defined Behavior
static const CWE CWE_BUFFER_UNDERRUN(786U); // Access of Memory Location Before Start of Buffer
static const CWE CWE_BUFFER_OVERRUN(788U); // Access of Memory Location After End of Buffer

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

Expand Down Expand Up @@ -885,7 +876,7 @@ void CheckBufferOverrun::argumentSizeError(const Token *tok, const std::string &
// CTU..
//---------------------------------------------------------------------------

// a Clang-built executable will crash when using the anonymous MyFileInfo later on - so put it in a unique namespace for now
// a Clang-built executable will crash when using the anonymous MyFileInfoBufferOverrun later on - so put it in a unique namespace for now
// see https://trac.cppcheck.net/ticket/12108 for more details
#ifdef __clang__
inline namespace CheckBufferOverrun_internal
Expand All @@ -894,7 +885,7 @@ namespace
#endif
{
/** data for multifile checking */
class MyFileInfo : public Check::FileInfo {
class MyFileInfoBufferOverrun : public Check::FileInfo {
public:
/** unsafe array index usage */
std::list<CTU::FileInfo::UnsafeUsage> unsafeArrayIndex;
Expand Down Expand Up @@ -954,7 +945,7 @@ Check::FileInfo *CheckBufferOverrun::getFileInfo(const Tokenizer *tokenizer, con
if (unsafeArrayIndex.empty() && unsafePointerArith.empty()) {
return nullptr;
}
auto *fileInfo = new MyFileInfo;
auto *fileInfo = new MyFileInfoBufferOverrun;
fileInfo->unsafeArrayIndex = unsafeArrayIndex;
fileInfo->unsafePointerArith = unsafePointerArith;
return fileInfo;
Expand All @@ -966,7 +957,7 @@ Check::FileInfo * CheckBufferOverrun::loadFileInfoFromXml(const tinyxml2::XMLEle
const std::string arrayIndex("array-index");
const std::string pointerArith("pointer-arith");

auto *fileInfo = new MyFileInfo;
auto *fileInfo = new MyFileInfoBufferOverrun;
for (const tinyxml2::XMLElement *e = xmlElement->FirstChildElement(); e; e = e->NextSiblingElement()) {
if (e->Name() == arrayIndex)
fileInfo->unsafeArrayIndex = CTU::loadUnsafeUsageListFromXml(e);
Expand Down Expand Up @@ -997,7 +988,7 @@ bool CheckBufferOverrun::analyseWholeProgram(const CTU::FileInfo *ctu, const std
const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> callsMap = ctu->getCallsMap();

for (const Check::FileInfo* fi1 : fileInfo) {
const MyFileInfo *fi = dynamic_cast<const MyFileInfo*>(fi1);
const MyFileInfoBufferOverrun *fi = dynamic_cast<const MyFileInfoBufferOverrun*>(fi1);
if (!fi)
continue;
for (const CTU::FileInfo::UnsafeUsage &unsafeUsage : fi->unsafeArrayIndex)
Expand Down
33 changes: 13 additions & 20 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,9 @@ namespace CTU {

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

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
static const CWE CWE758(758U); // Reliance on Undefined, Unspecified, or Implementation-Defined Behavior
static const CWE CWE762(762U); // Mismatched Memory Management Routines

static const CWE CWE_ONE_DEFINITION_RULE(758U);

static const char * getFunctionTypeName(Function::Type type)
{
Expand Down Expand Up @@ -2632,7 +2625,7 @@ void CheckClass::checkConstError2(const Token *tok1, const Token *tok2, const st
// ClassCheck: Check that initializer list is in declared order.
//---------------------------------------------------------------------------

namespace { // avoid one-definition-rule violation
namespace internal { // avoid one-definition-rule violation
struct VarInfo {
VarInfo(const Variable *_var, const Token *_tok)
: var(_var), tok(_tok) {}
Expand Down Expand Up @@ -2666,7 +2659,7 @@ void CheckClass::initializerListOrder()
const Token *tok = func->arg->link()->next();

if (tok->str() == ":") {
std::vector<VarInfo> vars;
std::vector<internal::VarInfo> vars;
tok = tok->next();

// find all variable initializations in list
Expand Down Expand Up @@ -3436,7 +3429,7 @@ void CheckClass::unsafeClassRefMemberError(const Token *tok, const std::string &
CWE(0), Certainty::normal);
}

// a Clang-built executable will crash when using the anonymous MyFileInfo later on - so put it in a unique namespace for now
// a Clang-built executable will crash when using the anonymous MyFileInfoClass later on - so put it in a unique namespace for now
// see https://trac.cppcheck.net/ticket/12108 for more details
#ifdef __clang__
inline namespace CheckClass_internal
Expand All @@ -3445,7 +3438,7 @@ namespace
#endif
{
/* multifile checking; one definition rule violations */
class MyFileInfo : public Check::FileInfo {
class MyFileInfoClass : public Check::FileInfo {
public:
struct NameLoc {
std::string className;
Expand Down Expand Up @@ -3485,7 +3478,7 @@ Check::FileInfo *CheckClass::getFileInfo(const Tokenizer *tokenizer, const Setti
return nullptr;
(void)settings;
// One definition rule
std::vector<MyFileInfo::NameLoc> classDefinitions;
std::vector<MyFileInfoClass::NameLoc> classDefinitions;
for (const Scope * classScope : tokenizer->getSymbolDatabase()->classAndStructScopes) {
if (classScope->isAnonymous())
continue;
Expand Down Expand Up @@ -3519,7 +3512,7 @@ Check::FileInfo *CheckClass::getFileInfo(const Tokenizer *tokenizer, const Setti
if (scope->type != Scope::ScopeType::eGlobal)
continue;

MyFileInfo::NameLoc nameLoc;
MyFileInfoClass::NameLoc nameLoc;
nameLoc.className = std::move(name);
nameLoc.fileName = tokenizer->list.file(classScope->classDef);
nameLoc.lineNumber = classScope->classDef->linenr();
Expand All @@ -3543,14 +3536,14 @@ Check::FileInfo *CheckClass::getFileInfo(const Tokenizer *tokenizer, const Setti
if (classDefinitions.empty())
return nullptr;

auto *fileInfo = new MyFileInfo;
auto *fileInfo = new MyFileInfoClass;
fileInfo->classDefinitions.swap(classDefinitions);
return fileInfo;
}

Check::FileInfo * CheckClass::loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const
{
auto *fileInfo = new MyFileInfo;
auto *fileInfo = new MyFileInfoClass;
for (const tinyxml2::XMLElement *e = xmlElement->FirstChildElement(); e; e = e->NextSiblingElement()) {
if (std::strcmp(e->Name(), "class") != 0)
continue;
Expand All @@ -3560,7 +3553,7 @@ Check::FileInfo * CheckClass::loadFileInfoFromXml(const tinyxml2::XMLElement *xm
const char *col = e->Attribute("col");
const char *hash = e->Attribute("hash");
if (name && file && line && col && hash) {
MyFileInfo::NameLoc nameLoc;
MyFileInfoClass::NameLoc nameLoc;
nameLoc.className = name;
nameLoc.fileName = file;
nameLoc.lineNumber = strToInt<int>(line);
Expand All @@ -3582,17 +3575,17 @@ bool CheckClass::analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<C
(void)ctu; // This argument is unused
(void)settings; // This argument is unused

std::unordered_map<std::string, MyFileInfo::NameLoc> all;
std::unordered_map<std::string, MyFileInfoClass::NameLoc> all;

CheckClass dummy(nullptr, &settings, &errorLogger);
dummy.
logChecker("CheckClass::analyseWholeProgram");

for (const Check::FileInfo* fi1 : fileInfo) {
const MyFileInfo *fi = dynamic_cast<const MyFileInfo*>(fi1);
const MyFileInfoClass *fi = dynamic_cast<const MyFileInfoClass*>(fi1);
if (!fi)
continue;
for (const MyFileInfo::NameLoc &nameLoc : fi->classDefinitions) {
for (const MyFileInfoClass::NameLoc &nameLoc : fi->classDefinitions) {
auto it = all.find(nameLoc.className);
if (it == all.end()) {
all[nameLoc.className] = nameLoc;
Expand Down
7 changes: 1 addition & 6 deletions lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,12 @@
#include <utility>
#include <vector>

// CWE ids used
static const CWE uncheckedErrorConditionCWE(391U);
static const CWE CWE398(398U); // Indicator of Poor Code Quality
static const CWE CWE570(570U); // Expression is Always False
static const CWE CWE571(571U); // Expression is Always True

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

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

bool CheckCondition::diag(const Token* tok, bool insert)
Expand Down
5 changes: 1 addition & 4 deletions lib/checkexceptionsafety.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@

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

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
9 changes: 1 addition & 8 deletions lib/checkfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,9 @@

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

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
static const CWE CWE628(628U); // Function Call with Incorrectly Specified Arguments
static const CWE CWE686(686U); // Function Call With Incorrect Argument Type
static const CWE CWE687(687U); // Function Call With Incorrectly Specified Argument Value
static const CWE CWE688(688U); // Function Call With Incorrect Variable or Reference as Argument

void CheckFunctions::checkProhibitedFunctions()
{
Expand Down
2 changes: 1 addition & 1 deletion lib/checkinternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// Register this check class (by creating a static instance of it).
// Disabled in release builds
namespace {
CheckInternal instance;
CheckInternal instanceInternal;
}

void CheckInternal::checkTokenMatchPatterns()
Expand Down
11 changes: 1 addition & 10 deletions lib/checkio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,9 @@

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

// 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
static const CWE CWE664(664U); // Improper Control of a Resource Through its Lifetime
static const CWE CWE685(685U); // Function Call With Incorrect Number of Arguments
static const CWE CWE686(686U); // Function Call With Incorrect Argument Type
static const CWE CWE687(687U); // Function Call With Incorrectly Specified Argument Value
static const CWE CWE704(704U); // Incorrect Type Conversion or Cast
static const CWE CWE910(910U); // Use of Expired File Descriptor

//---------------------------------------------------------------------------
// std::cout << std::cout;
Expand Down
Loading

0 comments on commit c198f21

Please sign in to comment.