Skip to content

Commit

Permalink
moved MyFileInfo definitions into source files (danmar#4697)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Oct 21, 2023
1 parent 26ba29c commit 09785a4
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 99 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ validateRules:

###### Build

$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.h lib/calculate.h lib/check.h lib/checkuninitvar.h lib/color.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/findtoken.h lib/forwardanalyzer.h lib/importproject.h lib/infer.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/programmemory.h lib/reverseanalyzer.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vfvalue.h
$(libcppdir)/valueflow.o: lib/valueflow.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.h lib/calculate.h lib/check.h lib/checkuninitvar.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/findtoken.h lib/forwardanalyzer.h lib/importproject.h lib/infer.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/programmemory.h lib/reverseanalyzer.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/valueflow.cpp

$(libcppdir)/tokenize.o: lib/tokenize.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/summaries.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/vfvalue.h
Expand Down Expand Up @@ -521,7 +521,7 @@ $(libcppdir)/checkinternal.o: lib/checkinternal.cpp lib/addoninfo.h lib/astutils
$(libcppdir)/checkio.o: lib/checkio.cpp lib/addoninfo.h lib/check.h lib/checkio.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkio.cpp

$(libcppdir)/checkleakautovar.o: lib/checkleakautovar.cpp lib/addoninfo.h lib/astutils.h lib/check.h lib/checkleakautovar.h lib/checkmemoryleak.h lib/checknullpointer.h lib/color.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
$(libcppdir)/checkleakautovar.o: lib/checkleakautovar.cpp lib/addoninfo.h lib/astutils.h lib/check.h lib/checkleakautovar.h lib/checkmemoryleak.h lib/checknullpointer.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkleakautovar.cpp

$(libcppdir)/checkmemoryleak.o: lib/checkmemoryleak.cpp lib/addoninfo.h lib/astutils.h lib/check.h lib/checkmemoryleak.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
Expand All @@ -539,7 +539,7 @@ $(libcppdir)/checkpostfixoperator.o: lib/checkpostfixoperator.cpp lib/addoninfo.
$(libcppdir)/checksizeof.o: lib/checksizeof.cpp lib/addoninfo.h lib/check.h lib/checksizeof.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checksizeof.cpp

$(libcppdir)/checkstl.o: lib/checkstl.cpp lib/addoninfo.h lib/astutils.h lib/check.h lib/checknullpointer.h lib/checkstl.h lib/color.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/pathanalysis.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/vfvalue.h
$(libcppdir)/checkstl.o: lib/checkstl.cpp lib/addoninfo.h lib/astutils.h lib/check.h lib/checknullpointer.h lib/checkstl.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/pathanalysis.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkstl.cpp

$(libcppdir)/checkstring.o: lib/checkstring.cpp lib/addoninfo.h lib/astutils.h lib/check.h lib/checkstring.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
Expand Down
29 changes: 21 additions & 8 deletions lib/checkbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,14 +883,27 @@ void CheckBufferOverrun::argumentSizeError(const Token *tok, const std::string &
// CTU..
//---------------------------------------------------------------------------

std::string CheckBufferOverrun::MyFileInfo::toString() const
{
std::string xml;
if (!unsafeArrayIndex.empty())
xml = " <array-index>\n" + CTU::toString(unsafeArrayIndex) + " </array-index>\n";
if (!unsafePointerArith.empty())
xml += " <pointer-arith>\n" + CTU::toString(unsafePointerArith) + " </pointer-arith>\n";
return xml;
namespace {
/** data for multifile checking */
class MyFileInfo : public Check::FileInfo {
public:
/** unsafe array index usage */
std::list<CTU::FileInfo::UnsafeUsage> unsafeArrayIndex;

/** unsafe pointer arithmetics */
std::list<CTU::FileInfo::UnsafeUsage> unsafePointerArith;

/** Convert data into xml string */
std::string toString() const override
{
std::string xml;
if (!unsafeArrayIndex.empty())
xml = " <array-index>\n" + CTU::toString(unsafeArrayIndex) + " </array-index>\n";
if (!unsafePointerArith.empty())
xml += " <pointer-arith>\n" + CTU::toString(unsafePointerArith) + " </pointer-arith>\n";
return xml;
}
};
}

bool CheckBufferOverrun::isCtuUnsafeBufferUsage(const Check *check, const Token *argtok, MathLib::bigint *offset, int type)
Expand Down
14 changes: 0 additions & 14 deletions lib/checkbufferoverrun.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,6 @@ class CPPCHECKLIB CheckBufferOverrun : public Check {
ValueFlow::Value getBufferSize(const Token *bufTok) const;

// CTU

/** data for multifile checking */
class MyFileInfo : public Check::FileInfo {
public:
/** unsafe array index usage */
std::list<CTU::FileInfo::UnsafeUsage> unsafeArrayIndex;

/** unsafe pointer arithmetic */
std::list<CTU::FileInfo::UnsafeUsage> unsafePointerArith;

/** Convert MyFileInfo data into xml string */
std::string toString() const override;
};

static bool isCtuUnsafeBufferUsage(const Check *check, const Token *argtok, MathLib::bigint *offset, int type);
static bool isCtuUnsafeArrayIndex(const Check *check, const Token *argtok, MathLib::bigint *offset);
static bool isCtuUnsafePointerArith(const Check *check, const Token *argtok, MathLib::bigint *offset);
Expand Down
51 changes: 37 additions & 14 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include <algorithm>
#include <cctype>
#include <cstddef>
#include <cstring>
#include <iterator>
#include <utility>
Expand Down Expand Up @@ -3390,6 +3391,42 @@ void CheckClass::unsafeClassRefMemberError(const Token *tok, const std::string &
CWE(0), Certainty::normal);
}

namespace {
/* multifile checking; one definition rule violations */
class MyFileInfo : public Check::FileInfo {
public:
struct NameLoc {
std::string className;
std::string fileName;
int lineNumber;
int column;
std::size_t hash;

bool isSameLocation(const NameLoc& other) const {
return fileName == other.fileName &&
lineNumber == other.lineNumber &&
column == other.column;
}
};
std::vector<NameLoc> classDefinitions;

/** Convert data into xml string */
std::string toString() const override
{
std::string ret;
for (const NameLoc &nameLoc: classDefinitions) {
ret += "<class name=\"" + ErrorLogger::toxml(nameLoc.className) +
"\" file=\"" + ErrorLogger::toxml(nameLoc.fileName) +
"\" line=\"" + std::to_string(nameLoc.lineNumber) +
"\" col=\"" + std::to_string(nameLoc.column) +
"\" hash=\"" + std::to_string(nameLoc.hash) +
"\"/>\n";
}
return ret;
}
};
}

Check::FileInfo *CheckClass::getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const
{
if (!tokenizer->isCPP())
Expand Down Expand Up @@ -3459,20 +3496,6 @@ Check::FileInfo *CheckClass::getFileInfo(const Tokenizer *tokenizer, const Setti
return fileInfo;
}

std::string CheckClass::MyFileInfo::toString() const
{
std::string ret;
for (const MyFileInfo::NameLoc &nameLoc: classDefinitions) {
ret += "<class name=\"" + ErrorLogger::toxml(nameLoc.className) +
"\" file=\"" + ErrorLogger::toxml(nameLoc.fileName) +
"\" line=\"" + std::to_string(nameLoc.lineNumber) +
"\" col=\"" + std::to_string(nameLoc.column) +
"\" hash=\"" + std::to_string(nameLoc.hash) +
"\"/>\n";
}
return ret;
}

Check::FileInfo * CheckClass::loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const
{
MyFileInfo *fileInfo = new MyFileInfo;
Expand Down
28 changes: 0 additions & 28 deletions lib/checkclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "tokenize.h"
#include "symboldatabase.h"

#include <cstddef>
#include <list>
#include <map>
#include <set>
Expand Down Expand Up @@ -164,33 +163,6 @@ class CPPCHECKLIB CheckClass : public Check {
/** @brief Unsafe class check - const reference member */
void checkUnsafeClassRefMember();


/* multifile checking; one definition rule violations */
class MyFileInfo : public Check::FileInfo {
public:
struct NameLoc {
std::string className;
std::string fileName;
int lineNumber;
int column;
std::size_t hash;

bool operator==(const NameLoc& other) const {
return isSameLocation(other) && hash == other.hash;
}

bool isSameLocation(const NameLoc& other) const {
return fileName == other.fileName &&
lineNumber == other.lineNumber &&
column == other.column;
}
};
std::vector<NameLoc> classDefinitions;

/** Convert MyFileInfo data into xml string */
std::string toString() const override;
};

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

Expand Down
22 changes: 17 additions & 5 deletions lib/checknullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "checknullpointer.h"

#include "astutils.h"
#include "ctu.h"
#include "errorlogger.h"
#include "errortypes.h"
#include "library.h"
Expand Down Expand Up @@ -547,11 +548,6 @@ void CheckNullPointer::redundantConditionWarning(const Token* tok, const ValueFl
inconclusive ? Certainty::inconclusive : Certainty::normal);
}

std::string CheckNullPointer::MyFileInfo::toString() const
{
return CTU::toString(unsafeUsage);
}

// NOLINTNEXTLINE(readability-non-const-parameter) - used as callback so we need to preserve the signature
static bool isUnsafeUsage(const Check *check, const Token *vartok, MathLib::bigint *value)
{
Expand All @@ -561,6 +557,22 @@ static bool isUnsafeUsage(const Check *check, const Token *vartok, MathLib::bigi
return checkNullPointer && checkNullPointer->isPointerDeRef(vartok, unknown);
}

namespace {
/* data for multifile checking */
class MyFileInfo : public Check::FileInfo {
public:
/** function arguments that are dereferenced without checking if they are null */
std::list<CTU::FileInfo::UnsafeUsage> unsafeUsage;

/** Convert data into xml string */
std::string toString() const override
{
return CTU::toString(unsafeUsage);
}
};
}


Check::FileInfo *CheckNullPointer::getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const
{
CheckNullPointer check(tokenizer, settings, nullptr);
Expand Down
15 changes: 4 additions & 11 deletions lib/checknullpointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

#include "check.h"
#include "config.h"
#include "ctu.h"
#include "tokenize.h"
#include "vfvalue.h"

Expand All @@ -36,6 +35,10 @@ class Library;
class Settings;
class Token;

namespace CTU {
class FileInfo;
}

namespace tinyxml2 {
class XMLElement;
}
Expand Down Expand Up @@ -102,16 +105,6 @@ class CPPCHECKLIB CheckNullPointer : public Check {
}
void nullPointerError(const Token *tok, const std::string &varname, const ValueFlow::Value* value, bool inconclusive);

/* data for multifile checking */
class MyFileInfo : public Check::FileInfo {
public:
/** function arguments that are dereferenced without checking if they are null */
std::list<CTU::FileInfo::UnsafeUsage> unsafeUsage;

/** Convert MyFileInfo data into xml string */
std::string toString() const override;
};

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

Expand Down
21 changes: 16 additions & 5 deletions lib/checkuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "checkuninitvar.h"

#include "astutils.h"
#include "ctu.h"
#include "errorlogger.h"
#include "library.h"
#include "mathlib.h"
Expand Down Expand Up @@ -1680,11 +1681,6 @@ void CheckUninitVar::valueFlowUninit()
}
}

std::string CheckUninitVar::MyFileInfo::toString() const
{
return CTU::toString(unsafeUsage);
}

Check::FileInfo *CheckUninitVar::getFileInfo(const Tokenizer *tokenizer, const Settings *settings) const
{
const CheckUninitVar checker(tokenizer, settings, nullptr);
Expand All @@ -1699,6 +1695,21 @@ static bool isVariableUsage(const Check *check, const Token *vartok, MathLib::bi
return c && c->isVariableUsage(vartok, true, CheckUninitVar::Alloc::ARRAY);
}

namespace {
/* data for multifile checking */
class MyFileInfo : public Check::FileInfo {
public:
/** function arguments that data are unconditionally read */
std::list<CTU::FileInfo::UnsafeUsage> unsafeUsage;

/** Convert data into xml string */
std::string toString() const override
{
return CTU::toString(unsafeUsage);
}
};
}

Check::FileInfo *CheckUninitVar::getFileInfo() const
{
const std::list<CTU::FileInfo::UnsafeUsage> &unsafeUsage = CTU::getUnsafeUsage(mTokenizer, mSettings, this, ::isVariableUsage);
Expand Down
15 changes: 4 additions & 11 deletions lib/checkuninitvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

#include "check.h"
#include "config.h"
#include "ctu.h"
#include "mathlib.h"
#include "errortypes.h"
#include "tokenize.h"
Expand All @@ -42,6 +41,10 @@ class ErrorLogger;
class Settings;
class Library;

namespace CTU {
class FileInfo;
}

namespace tinyxml2 {
class XMLElement;
}
Expand Down Expand Up @@ -102,16 +105,6 @@ class CPPCHECKLIB CheckUninitVar : public Check {
/** ValueFlow-based checking for uninitialized variables */
void valueFlowUninit();

/* data for multifile checking */
class MyFileInfo : public Check::FileInfo {
public:
/** function arguments that data are unconditionally read */
std::list<CTU::FileInfo::UnsafeUsage> unsafeUsage;

/** Convert MyFileInfo data into xml string */
std::string toString() const override;
};

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

Expand Down
1 change: 1 addition & 0 deletions lib/cppcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>

class Tokenizer;
Expand Down

0 comments on commit 09785a4

Please sign in to comment.