Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library: made all members private and moved implementations to source file #6408

Merged
merged 2 commits into from
May 23, 2024
Merged
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ test/testmathlib.o: test/testmathlib.cpp lib/addoninfo.h lib/check.h lib/color.h
test/testmemleak.o: test/testmemleak.cpp lib/addoninfo.h lib/check.h lib/checkmemoryleak.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.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 test/fixture.h test/helpers.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testmemleak.cpp

test/testnullpointer.o: test/testnullpointer.cpp lib/addoninfo.h lib/check.h lib/checknullpointer.h lib/color.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
test/testnullpointer.o: test/testnullpointer.cpp externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/check.h lib/checknullpointer.h lib/color.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h test/fixture.h test/helpers.h
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testnullpointer.cpp

test/testoptions.o: test/testoptions.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h test/fixture.h test/options.h
Expand Down
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2188,7 +2188,7 @@ static bool hasNoreturnFunction(const Token* tok, const Library& library, const
} else if (Token::Match(ftok, "exit|abort")) {
return true;
}
if (unknownFunc && !function && library.functions.count(library.getFunctionName(ftok)) == 0)
if (unknownFunc && !function && library.functions().count(library.getFunctionName(ftok)) == 0)
*unknownFunc = ftok;
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/checkfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ void CheckFunctions::checkLibraryMatchFunctions()
if (functionName.empty())
continue;

if (mSettings->library.functions.find(functionName) != mSettings->library.functions.end())
if (mSettings->library.functions().find(functionName) != mSettings->library.functions().end())
continue;

if (mSettings->library.podtype(tok->expressionString()))
Expand Down
181 changes: 139 additions & 42 deletions lib/library.cpp

Large diffs are not rendered by default.

78 changes: 19 additions & 59 deletions lib/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,30 +109,19 @@ class CPPCHECKLIB Library {

// TODO: get rid of this
/** get allocation info for function by name (deprecated, use other alloc) */
const AllocFunc* getAllocFuncInfo(const char name[]) const {
return getAllocDealloc(mAlloc, name);
}
const AllocFunc* getAllocFuncInfo(const char name[]) const;

// TODO: get rid of this
/** get deallocation info for function by name (deprecated, use other alloc) */
const AllocFunc* getDeallocFuncInfo(const char name[]) const {
return getAllocDealloc(mDealloc, name);
}
const AllocFunc* getDeallocFuncInfo(const char name[]) const;

// TODO: get rid of this
/** get allocation id for function by name (deprecated, use other alloc) */
// cppcheck-suppress unusedFunction
int allocId(const char name[]) const {
const AllocFunc* af = getAllocDealloc(mAlloc, name);
return af ? af->groupId : 0;
}
int allocId(const char name[]) const;

// TODO: get rid of this
/** get deallocation id for function by name (deprecated, use other alloc) */
int deallocId(const char name[]) const {
const AllocFunc* af = getAllocDealloc(mDealloc, name);
return af ? af->groupId : 0;
}
int deallocId(const char name[]) const;

static bool isCompliantValidationExpression(const char* p);

Expand Down Expand Up @@ -267,7 +256,7 @@ class CPPCHECKLIB Library {
static Yield yieldFrom(const std::string& yieldName);
static Action actionFrom(const std::string& actionName);
};
std::unordered_map<std::string, Container> containers;
const std::unordered_map<std::string, Container>& containers() const;
const Container* detectContainer(const Token* typeStart) const;
const Container* detectIterator(const Token* typeStart) const;
const Container* detectContainerOrIterator(const Token* typeStart, bool* isIterator = nullptr, bool withoutStd = false) const;
Expand Down Expand Up @@ -327,7 +316,7 @@ class CPPCHECKLIB Library {
};

const Function *getFunction(const Token *ftok) const;
std::unordered_map<std::string, Function> functions;
const std::unordered_map<std::string, Function>& functions() const;
bool isUse(const std::string& functionName) const;
bool isLeakIgnore(const std::string& functionName) const;
bool isFunctionConst(const std::string& functionName, bool pure) const;
Expand Down Expand Up @@ -377,9 +366,7 @@ class CPPCHECKLIB Library {

bool processMarkupAfterCode(const std::string &path) const;

const std::set<std::string> &markupExtensions() const {
return mMarkupExtensions;
}
const std::set<std::string> &markupExtensions() const;

bool reportErrors(const std::string &path) const;

Expand All @@ -394,19 +381,11 @@ class CPPCHECKLIB Library {

bool iskeyword(const std::string &file, const std::string &keyword) const;

bool isexporter(const std::string &prefix) const {
return mExporters.find(prefix) != mExporters.end();
}
bool isexporter(const std::string &prefix) const;

bool isexportedprefix(const std::string &prefix, const std::string &token) const {
const std::map<std::string, ExportedFunctions>::const_iterator it = mExporters.find(prefix);
return (it != mExporters.end() && it->second.isPrefix(token));
}
bool isexportedprefix(const std::string &prefix, const std::string &token) const;

bool isexportedsuffix(const std::string &prefix, const std::string &token) const {
const std::map<std::string, ExportedFunctions>::const_iterator it = mExporters.find(prefix);
return (it != mExporters.end() && it->second.isSuffix(token));
}
bool isexportedsuffix(const std::string &prefix, const std::string &token) const;

bool isimporter(const std::string& file, const std::string &importer) const;

Expand All @@ -416,20 +395,11 @@ class CPPCHECKLIB Library {
static bool isContainerYield(const Token* const cond, Library::Container::Yield y, const std::string& fallback = emptyString);
static Library::Container::Yield getContainerYield(const Token* const cond);

bool isreflection(const std::string &token) const {
return mReflection.find(token) != mReflection.end();
}
bool isreflection(const std::string &token) const;

int reflectionArgument(const std::string &token) const {
const std::map<std::string, int>::const_iterator it = mReflection.find(token);
if (it != mReflection.end())
return it->second;
return -1;
}
int reflectionArgument(const std::string &token) const;

bool isentrypoint(const std::string &func) const {
return func == "main" || mEntrypoints.find(func) != mEntrypoints.end();
}
bool isentrypoint(const std::string &func) const;

std::set<std::string> defines; // to provide some library defines

Expand All @@ -438,7 +408,7 @@ class CPPCHECKLIB Library {
bool unique = false;
};

std::unordered_map<std::string, SmartPointer> smartPointers;
const std::unordered_map<std::string, SmartPointer>& smartPointers() const;
bool isSmartPointer(const Token *tok) const;
const SmartPointer* detectSmartPointer(const Token* tok, bool withoutStd = false) const;

Expand All @@ -447,10 +417,7 @@ class CPPCHECKLIB Library {
char sign;
enum class Type { NO, BOOL, CHAR, SHORT, INT, LONG, LONGLONG } stdtype;
};
const PodType *podtype(const std::string &name) const {
const std::unordered_map<std::string, PodType>::const_iterator it = mPodTypes.find(name);
return (it != mPodTypes.end()) ? &(it->second) : nullptr;
}
const PodType *podtype(const std::string &name) const;

struct PlatformType {
bool operator == (const PlatformType & type) const {
Expand Down Expand Up @@ -482,17 +449,7 @@ class CPPCHECKLIB Library {
std::map<std::string, PlatformType> mPlatformTypes;
};

const PlatformType *platform_type(const std::string &name, const std::string & platform) const {
const std::map<std::string, Platform>::const_iterator it = mPlatforms.find(platform);
if (it != mPlatforms.end()) {
const PlatformType * const type = it->second.platform_type(name);
if (type)
return type;
}

const std::map<std::string, PlatformType>::const_iterator it2 = mPlatformTypes.find(name);
return (it2 != mPlatformTypes.end()) ? &(it2->second) : nullptr;
}
const PlatformType *platform_type(const std::string &name, const std::string & platform) const;

/**
* Get function name for function call
Expand Down Expand Up @@ -566,6 +523,9 @@ class CPPCHECKLIB Library {
int mOffset{};
std::set<std::string> mBlocks;
};
std::unordered_map<std::string, Container> mContainers;
std::unordered_map<std::string, Function> mFunctions;
std::unordered_map<std::string, SmartPointer> mSmartPointers;
enum class FalseTrueMaybe { False, True, Maybe };
int mAllocId{};
std::set<std::string> mFiles;
Expand Down
10 changes: 5 additions & 5 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ void SymbolDatabase::createSymbolDatabaseIncompleteVars()
fstr.insert(0, ftok->previous()->str() + "::");
ftok = ftok->tokAt(-2);
}
if (mSettings.library.functions.find(fstr) != mSettings.library.functions.end())
if (mSettings.library.functions().find(fstr) != mSettings.library.functions().end())
continue;
if (tok->isCpp()) {
const Token* parent = tok->astParent();
Expand Down Expand Up @@ -7539,10 +7539,10 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
vt.smartPointerType = vt.typeScope->definedType;
vt.typeScope = nullptr;
}
if (e == "std::make_shared" && mSettings.library.smartPointers.count("std::shared_ptr") > 0)
vt.smartPointer = &mSettings.library.smartPointers.at("std::shared_ptr");
if (e == "std::make_unique" && mSettings.library.smartPointers.count("std::unique_ptr") > 0)
vt.smartPointer = &mSettings.library.smartPointers.at("std::unique_ptr");
if (e == "std::make_shared" && mSettings.library.smartPointers().count("std::shared_ptr") > 0)
vt.smartPointer = &mSettings.library.smartPointers().at("std::shared_ptr");
if (e == "std::make_unique" && mSettings.library.smartPointers().count("std::unique_ptr") > 0)
vt.smartPointer = &mSettings.library.smartPointers().at("std::unique_ptr");
vt.type = ValueType::Type::SMART_POINTER;
vt.smartPointerTypeToken = tok->astOperand1()->tokAt(3);
setValueType(tok, vt);
Expand Down
44 changes: 22 additions & 22 deletions test/testlibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class TestLibrary : public TestFixture {
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n<def/>";
Library library;
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
ASSERT(library.functions.empty());
ASSERT(library.functions().empty());
}

void function() const {
Expand All @@ -122,8 +122,8 @@ class TestLibrary : public TestFixture {

Library library;
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
ASSERT_EQUALS(library.functions.size(), 1U);
ASSERT(library.functions.at("foo").argumentChecks.empty());
ASSERT_EQUALS(library.functions().size(), 1U);
ASSERT(library.functions().at("foo").argumentChecks.empty());
ASSERT(library.isnotnoreturn(tokenList.front()));
}

Expand Down Expand Up @@ -252,13 +252,13 @@ class TestLibrary : public TestFixture {

Library library;
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
ASSERT_EQUALS(0, library.functions["foo"].argumentChecks[1].notuninit);
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[2].notnull);
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[3].formatstr);
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[4].strz);
ASSERT_EQUALS(false, library.functions["foo"].argumentChecks[4].optional);
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[5].notbool);
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[5].optional);
ASSERT_EQUALS(0, library.functions().at("foo").argumentChecks.at(1).notuninit);
ASSERT_EQUALS(true, library.functions().at("foo").argumentChecks.at(2).notnull);
ASSERT_EQUALS(true, library.functions().at("foo").argumentChecks.at(3).formatstr);
ASSERT_EQUALS(true, library.functions().at("foo").argumentChecks.at(4).strz);
ASSERT_EQUALS(false, library.functions().at("foo").argumentChecks.at(4).optional);
ASSERT_EQUALS(true, library.functions().at("foo").argumentChecks.at(5).notbool);
ASSERT_EQUALS(true, library.functions().at("foo").argumentChecks.at(5).optional);
}

void function_arg_any() const {
Expand All @@ -271,7 +271,7 @@ class TestLibrary : public TestFixture {

Library library;
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
ASSERT_EQUALS(0, library.functions["foo"].argumentChecks[-1].notuninit);
ASSERT_EQUALS(0, library.functions().at("foo").argumentChecks.at(-1).notuninit);
}

void function_arg_variadic() const {
Expand All @@ -285,7 +285,7 @@ class TestLibrary : public TestFixture {

Library library;
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
ASSERT_EQUALS(0, library.functions["foo"].argumentChecks[-1].notuninit);
ASSERT_EQUALS(0, library.functions().at("foo").argumentChecks.at(-1).notuninit);

const char code[] = "foo(a,b,c,d,e);";
SimpleTokenList tokenList(code);
Expand Down Expand Up @@ -540,9 +540,9 @@ class TestLibrary : public TestFixture {

Library library;
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
ASSERT_EQUALS(library.functions.size(), 2U);
ASSERT(library.functions.at("Foo::foo").argumentChecks.empty());
ASSERT(library.functions.at("bar").argumentChecks.empty());
ASSERT_EQUALS(library.functions().size(), 2U);
ASSERT(library.functions().at("Foo::foo").argumentChecks.empty());
ASSERT(library.functions().at("bar").argumentChecks.empty());

{
const char code[] = "Foo::foo();";
Expand All @@ -567,7 +567,7 @@ class TestLibrary : public TestFixture {

Library library;
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
ASSERT_EQUALS(library.functions.size(), 1U);
ASSERT_EQUALS(library.functions().size(), 1U);

{
SimpleTokenizer tokenizer(settings, *this);
Expand Down Expand Up @@ -656,7 +656,7 @@ class TestLibrary : public TestFixture {

Library library;
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
ASSERT(library.functions.empty());
ASSERT(library.functions().empty());

const Library::AllocFunc* af = library.getAllocFuncInfo("CreateX");
ASSERT(af && af->arg == -1);
Expand Down Expand Up @@ -699,7 +699,7 @@ class TestLibrary : public TestFixture {

Library library;
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
ASSERT(library.functions.empty());
ASSERT(library.functions().empty());

const Library::AllocFunc* af = library.getAllocFuncInfo("CreateX");
ASSERT(af && af->arg == 5 && !af->initData);
Expand All @@ -718,7 +718,7 @@ class TestLibrary : public TestFixture {

Library library;
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
ASSERT(library.functions.empty());
ASSERT(library.functions().empty());

ASSERT(Library::isresource(library.allocId("CreateX")));
ASSERT_EQUALS(library.allocId("CreateX"), library.deallocId("DeleteX"));
Expand Down Expand Up @@ -815,9 +815,9 @@ class TestLibrary : public TestFixture {
Library library;
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));

const Library::Container& A = library.containers["A"];
const Library::Container& B = library.containers["B"];
const Library::Container& C = library.containers["C"];
const Library::Container& A = library.containers().at("A");
const Library::Container& B = library.containers().at("B");
const Library::Container& C = library.containers().at("C");

ASSERT_EQUALS(A.type_templateArgNo, 1);
ASSERT_EQUALS(A.size_templateArgNo, 4);
Expand Down
37 changes: 28 additions & 9 deletions test/testnullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include <unordered_map>
#include <vector>

#include "xml.h"

class TestNullPointer : public TestFixture {
public:
TestNullPointer() : TestFixture("TestNullPointer") {}
Expand Down Expand Up @@ -176,6 +178,12 @@ class TestNullPointer : public TestFixture {
TEST_CASE(ctuTest);
}

static bool loadxmldata(Library &lib, const char xmldata[], std::size_t len)
{
tinyxml2::XMLDocument doc;
return (tinyxml2::XML_SUCCESS == doc.Parse(xmldata, len)) && (lib.load(doc).errorcode == Library::ErrorCode::OK);
}

#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
void check_(const char* file, int line, const char (&code)[size], bool inconclusive = false, bool cpp = true) {
Expand Down Expand Up @@ -4149,11 +4157,17 @@ class TestNullPointer : public TestFixture {

// nothing bad..
{
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"x\">\n"
" <arg nr=\"1\"></arg>\n"
" <arg nr=\"2\"></arg>\n"
" <arg nr=\"3\"></arg>\n"
" </function>\n"
"</def>";

Library library;
Library::ArgumentChecks arg;
library.functions["x"].argumentChecks[1] = arg;
library.functions["x"].argumentChecks[2] = arg;
library.functions["x"].argumentChecks[3] = arg;
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));

std::list<const Token *> null;
CheckNullPointer::parseFunctionCall(*xtok, null, library);
Expand All @@ -4162,12 +4176,17 @@ class TestNullPointer : public TestFixture {

// for 1st parameter null pointer is not ok..
{
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"x\">\n"
" <arg nr=\"1\"><not-null/></arg>\n"
" <arg nr=\"2\"></arg>\n"
" <arg nr=\"3\"></arg>\n"
" </function>\n"
"</def>";

Library library;
Library::ArgumentChecks arg;
library.functions["x"].argumentChecks[1] = arg;
library.functions["x"].argumentChecks[2] = arg;
library.functions["x"].argumentChecks[3] = arg;
library.functions["x"].argumentChecks[1].notnull = true;
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));

std::list<const Token *> null;
CheckNullPointer::parseFunctionCall(*xtok, null, library);
Expand Down
Loading