Skip to content

Commit ab69847

Browse files
authored
Library: made all members private and moved implementations to source file (#6408)
This ensures that we do not modify the library directly. And the implementations are moved in preparation of moving all the data into an implementation class.
1 parent 06f5689 commit ab69847

8 files changed

Lines changed: 216 additions & 140 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ test/testmathlib.o: test/testmathlib.cpp lib/addoninfo.h lib/check.h lib/color.h
787787
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
788788
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testmemleak.cpp
789789

790-
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
790+
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
791791
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testnullpointer.cpp
792792

793793
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

lib/astutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2188,7 +2188,7 @@ static bool hasNoreturnFunction(const Token* tok, const Library& library, const
21882188
} else if (Token::Match(ftok, "exit|abort")) {
21892189
return true;
21902190
}
2191-
if (unknownFunc && !function && library.functions.count(library.getFunctionName(ftok)) == 0)
2191+
if (unknownFunc && !function && library.functions().count(library.getFunctionName(ftok)) == 0)
21922192
*unknownFunc = ftok;
21932193
return false;
21942194
}

lib/checkfunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ void CheckFunctions::checkLibraryMatchFunctions()
655655
if (functionName.empty())
656656
continue;
657657

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

661661
if (mSettings->library.podtype(tok->expressionString()))

lib/library.cpp

Lines changed: 139 additions & 42 deletions
Large diffs are not rendered by default.

lib/library.h

Lines changed: 19 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -109,30 +109,19 @@ class CPPCHECKLIB Library {
109109

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

116114
// TODO: get rid of this
117115
/** get deallocation info for function by name (deprecated, use other alloc) */
118-
const AllocFunc* getDeallocFuncInfo(const char name[]) const {
119-
return getAllocDealloc(mDealloc, name);
120-
}
116+
const AllocFunc* getDeallocFuncInfo(const char name[]) const;
121117

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

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

137126
static bool isCompliantValidationExpression(const char* p);
138127

@@ -267,7 +256,7 @@ class CPPCHECKLIB Library {
267256
static Yield yieldFrom(const std::string& yieldName);
268257
static Action actionFrom(const std::string& actionName);
269258
};
270-
std::unordered_map<std::string, Container> containers;
259+
const std::unordered_map<std::string, Container>& containers() const;
271260
const Container* detectContainer(const Token* typeStart) const;
272261
const Container* detectIterator(const Token* typeStart) const;
273262
const Container* detectContainerOrIterator(const Token* typeStart, bool* isIterator = nullptr, bool withoutStd = false) const;
@@ -327,7 +316,7 @@ class CPPCHECKLIB Library {
327316
};
328317

329318
const Function *getFunction(const Token *ftok) const;
330-
std::unordered_map<std::string, Function> functions;
319+
const std::unordered_map<std::string, Function>& functions() const;
331320
bool isUse(const std::string& functionName) const;
332321
bool isLeakIgnore(const std::string& functionName) const;
333322
bool isFunctionConst(const std::string& functionName, bool pure) const;
@@ -377,9 +366,7 @@ class CPPCHECKLIB Library {
377366

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

380-
const std::set<std::string> &markupExtensions() const {
381-
return mMarkupExtensions;
382-
}
369+
const std::set<std::string> &markupExtensions() const;
383370

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

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

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

397-
bool isexporter(const std::string &prefix) const {
398-
return mExporters.find(prefix) != mExporters.end();
399-
}
384+
bool isexporter(const std::string &prefix) const;
400385

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

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

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

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

419-
bool isreflection(const std::string &token) const {
420-
return mReflection.find(token) != mReflection.end();
421-
}
398+
bool isreflection(const std::string &token) const;
422399

423-
int reflectionArgument(const std::string &token) const {
424-
const std::map<std::string, int>::const_iterator it = mReflection.find(token);
425-
if (it != mReflection.end())
426-
return it->second;
427-
return -1;
428-
}
400+
int reflectionArgument(const std::string &token) const;
429401

430-
bool isentrypoint(const std::string &func) const {
431-
return func == "main" || mEntrypoints.find(func) != mEntrypoints.end();
432-
}
402+
bool isentrypoint(const std::string &func) const;
433403

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

@@ -438,7 +408,7 @@ class CPPCHECKLIB Library {
438408
bool unique = false;
439409
};
440410

441-
std::unordered_map<std::string, SmartPointer> smartPointers;
411+
const std::unordered_map<std::string, SmartPointer>& smartPointers() const;
442412
bool isSmartPointer(const Token *tok) const;
443413
const SmartPointer* detectSmartPointer(const Token* tok, bool withoutStd = false) const;
444414

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

455422
struct PlatformType {
456423
bool operator == (const PlatformType & type) const {
@@ -482,17 +449,7 @@ class CPPCHECKLIB Library {
482449
std::map<std::string, PlatformType> mPlatformTypes;
483450
};
484451

485-
const PlatformType *platform_type(const std::string &name, const std::string & platform) const {
486-
const std::map<std::string, Platform>::const_iterator it = mPlatforms.find(platform);
487-
if (it != mPlatforms.end()) {
488-
const PlatformType * const type = it->second.platform_type(name);
489-
if (type)
490-
return type;
491-
}
492-
493-
const std::map<std::string, PlatformType>::const_iterator it2 = mPlatformTypes.find(name);
494-
return (it2 != mPlatformTypes.end()) ? &(it2->second) : nullptr;
495-
}
452+
const PlatformType *platform_type(const std::string &name, const std::string & platform) const;
496453

497454
/**
498455
* Get function name for function call
@@ -566,6 +523,9 @@ class CPPCHECKLIB Library {
566523
int mOffset{};
567524
std::set<std::string> mBlocks;
568525
};
526+
std::unordered_map<std::string, Container> mContainers;
527+
std::unordered_map<std::string, Function> mFunctions;
528+
std::unordered_map<std::string, SmartPointer> mSmartPointers;
569529
enum class FalseTrueMaybe { False, True, Maybe };
570530
int mAllocId{};
571531
std::set<std::string> mFiles;

lib/symboldatabase.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,7 @@ void SymbolDatabase::createSymbolDatabaseIncompleteVars()
14941494
fstr.insert(0, ftok->previous()->str() + "::");
14951495
ftok = ftok->tokAt(-2);
14961496
}
1497-
if (mSettings.library.functions.find(fstr) != mSettings.library.functions.end())
1497+
if (mSettings.library.functions().find(fstr) != mSettings.library.functions().end())
14981498
continue;
14991499
if (tok->isCpp()) {
15001500
const Token* parent = tok->astParent();
@@ -7536,10 +7536,10 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
75367536
vt.smartPointerType = vt.typeScope->definedType;
75377537
vt.typeScope = nullptr;
75387538
}
7539-
if (e == "std::make_shared" && mSettings.library.smartPointers.count("std::shared_ptr") > 0)
7540-
vt.smartPointer = &mSettings.library.smartPointers.at("std::shared_ptr");
7541-
if (e == "std::make_unique" && mSettings.library.smartPointers.count("std::unique_ptr") > 0)
7542-
vt.smartPointer = &mSettings.library.smartPointers.at("std::unique_ptr");
7539+
if (e == "std::make_shared" && mSettings.library.smartPointers().count("std::shared_ptr") > 0)
7540+
vt.smartPointer = &mSettings.library.smartPointers().at("std::shared_ptr");
7541+
if (e == "std::make_unique" && mSettings.library.smartPointers().count("std::unique_ptr") > 0)
7542+
vt.smartPointer = &mSettings.library.smartPointers().at("std::unique_ptr");
75437543
vt.type = ValueType::Type::SMART_POINTER;
75447544
vt.smartPointerTypeToken = tok->astOperand1()->tokAt(3);
75457545
setValueType(tok, vt);

test/testlibrary.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class TestLibrary : public TestFixture {
105105
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n<def/>";
106106
Library library;
107107
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
108-
ASSERT(library.functions.empty());
108+
ASSERT(library.functions().empty());
109109
}
110110

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

123123
Library library;
124124
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
125-
ASSERT_EQUALS(library.functions.size(), 1U);
126-
ASSERT(library.functions.at("foo").argumentChecks.empty());
125+
ASSERT_EQUALS(library.functions().size(), 1U);
126+
ASSERT(library.functions().at("foo").argumentChecks.empty());
127127
ASSERT(library.isnotnoreturn(tokenList.front()));
128128
}
129129

@@ -252,13 +252,13 @@ class TestLibrary : public TestFixture {
252252

253253
Library library;
254254
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
255-
ASSERT_EQUALS(0, library.functions["foo"].argumentChecks[1].notuninit);
256-
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[2].notnull);
257-
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[3].formatstr);
258-
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[4].strz);
259-
ASSERT_EQUALS(false, library.functions["foo"].argumentChecks[4].optional);
260-
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[5].notbool);
261-
ASSERT_EQUALS(true, library.functions["foo"].argumentChecks[5].optional);
255+
ASSERT_EQUALS(0, library.functions().at("foo").argumentChecks.at(1).notuninit);
256+
ASSERT_EQUALS(true, library.functions().at("foo").argumentChecks.at(2).notnull);
257+
ASSERT_EQUALS(true, library.functions().at("foo").argumentChecks.at(3).formatstr);
258+
ASSERT_EQUALS(true, library.functions().at("foo").argumentChecks.at(4).strz);
259+
ASSERT_EQUALS(false, library.functions().at("foo").argumentChecks.at(4).optional);
260+
ASSERT_EQUALS(true, library.functions().at("foo").argumentChecks.at(5).notbool);
261+
ASSERT_EQUALS(true, library.functions().at("foo").argumentChecks.at(5).optional);
262262
}
263263

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

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

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

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

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

541541
Library library;
542542
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
543-
ASSERT_EQUALS(library.functions.size(), 2U);
544-
ASSERT(library.functions.at("Foo::foo").argumentChecks.empty());
545-
ASSERT(library.functions.at("bar").argumentChecks.empty());
543+
ASSERT_EQUALS(library.functions().size(), 2U);
544+
ASSERT(library.functions().at("Foo::foo").argumentChecks.empty());
545+
ASSERT(library.functions().at("bar").argumentChecks.empty());
546546

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

568568
Library library;
569569
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
570-
ASSERT_EQUALS(library.functions.size(), 1U);
570+
ASSERT_EQUALS(library.functions().size(), 1U);
571571

572572
{
573573
SimpleTokenizer tokenizer(settings, *this);
@@ -656,7 +656,7 @@ class TestLibrary : public TestFixture {
656656

657657
Library library;
658658
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
659-
ASSERT(library.functions.empty());
659+
ASSERT(library.functions().empty());
660660

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

700700
Library library;
701701
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
702-
ASSERT(library.functions.empty());
702+
ASSERT(library.functions().empty());
703703

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

719719
Library library;
720720
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
721-
ASSERT(library.functions.empty());
721+
ASSERT(library.functions().empty());
722722

723723
ASSERT(Library::isresource(library.allocId("CreateX")));
724724
ASSERT_EQUALS(library.allocId("CreateX"), library.deallocId("DeleteX"));
@@ -815,9 +815,9 @@ class TestLibrary : public TestFixture {
815815
Library library;
816816
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
817817

818-
const Library::Container& A = library.containers["A"];
819-
const Library::Container& B = library.containers["B"];
820-
const Library::Container& C = library.containers["C"];
818+
const Library::Container& A = library.containers().at("A");
819+
const Library::Container& B = library.containers().at("B");
820+
const Library::Container& C = library.containers().at("C");
821821

822822
ASSERT_EQUALS(A.type_templateArgNo, 1);
823823
ASSERT_EQUALS(A.size_templateArgNo, 4);

test/testnullpointer.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include <unordered_map>
3535
#include <vector>
3636

37+
#include "xml.h"
38+
3739
class TestNullPointer : public TestFixture {
3840
public:
3941
TestNullPointer() : TestFixture("TestNullPointer") {}
@@ -176,6 +178,12 @@ class TestNullPointer : public TestFixture {
176178
TEST_CASE(ctuTest);
177179
}
178180

181+
static bool loadxmldata(Library &lib, const char xmldata[], std::size_t len)
182+
{
183+
tinyxml2::XMLDocument doc;
184+
return (tinyxml2::XML_SUCCESS == doc.Parse(xmldata, len)) && (lib.load(doc).errorcode == Library::ErrorCode::OK);
185+
}
186+
179187
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
180188
template<size_t size>
181189
void check_(const char* file, int line, const char (&code)[size], bool inconclusive = false, bool cpp = true) {
@@ -4149,11 +4157,17 @@ class TestNullPointer : public TestFixture {
41494157

41504158
// nothing bad..
41514159
{
4160+
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
4161+
"<def>\n"
4162+
" <function name=\"x\">\n"
4163+
" <arg nr=\"1\"></arg>\n"
4164+
" <arg nr=\"2\"></arg>\n"
4165+
" <arg nr=\"3\"></arg>\n"
4166+
" </function>\n"
4167+
"</def>";
4168+
41524169
Library library;
4153-
Library::ArgumentChecks arg;
4154-
library.functions["x"].argumentChecks[1] = arg;
4155-
library.functions["x"].argumentChecks[2] = arg;
4156-
library.functions["x"].argumentChecks[3] = arg;
4170+
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
41574171

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

41634177
// for 1st parameter null pointer is not ok..
41644178
{
4179+
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
4180+
"<def>\n"
4181+
" <function name=\"x\">\n"
4182+
" <arg nr=\"1\"><not-null/></arg>\n"
4183+
" <arg nr=\"2\"></arg>\n"
4184+
" <arg nr=\"3\"></arg>\n"
4185+
" </function>\n"
4186+
"</def>";
4187+
41654188
Library library;
4166-
Library::ArgumentChecks arg;
4167-
library.functions["x"].argumentChecks[1] = arg;
4168-
library.functions["x"].argumentChecks[2] = arg;
4169-
library.functions["x"].argumentChecks[3] = arg;
4170-
library.functions["x"].argumentChecks[1].notnull = true;
4189+
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
41714190

41724191
std::list<const Token *> null;
41734192
CheckNullPointer::parseFunctionCall(*xtok, null, library);

0 commit comments

Comments
 (0)