Skip to content

Commit 3ec4da0

Browse files
authored
improved library loading in tests (#4806)
1 parent e99d7c6 commit 3ec4da0

18 files changed

+73
-99
lines changed

lib/checkclass.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,6 @@ static bool isVariableCopyNeeded(const Variable &var, Function::Type type)
102102
(var.valueType() && var.valueType()->type >= ValueType::Type::CHAR));
103103
}
104104

105-
static bool isVcl(const Settings *settings)
106-
{
107-
return std::any_of(settings->libraries.cbegin(), settings->libraries.cend(), [](const std::string& library) {
108-
return library == "vcl";
109-
});
110-
}
111-
112105
static bool isVclTypeInit(const Type *type)
113106
{
114107
if (!type)
@@ -141,7 +134,7 @@ void CheckClass::constructors()
141134

142135
const bool printInconclusive = mSettings->certainty.isEnabled(Certainty::inconclusive);
143136
for (const Scope * scope : mSymbolDatabase->classAndStructScopes) {
144-
if (isVcl(mSettings) && isVclTypeInit(scope->definedType))
137+
if (mSettings->hasLib("vcl") && isVclTypeInit(scope->definedType))
145138
continue;
146139

147140
const bool unusedTemplate = Token::simpleMatch(scope->classDef->previous(), ">");

lib/checkmemoryleak.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
116116
return New;
117117
}
118118

119-
if (mSettings_->posix()) {
119+
if (mSettings_->hasLib("posix")) {
120120
if (Token::Match(tok2, "open|openat|creat|mkstemp|mkostemp|socket (")) {
121121
// simple sanity check of function parameters..
122122
// TODO: Make such check for all these functions
@@ -237,7 +237,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok
237237
if (tok->str() == "realloc" && Token::simpleMatch(vartok->next(), ", 0 )"))
238238
return Malloc;
239239

240-
if (mSettings_->posix()) {
240+
if (mSettings_->hasLib("posix")) {
241241
if (tok->str() == "close")
242242
return Fd;
243243
if (tok->str() == "pclose")
@@ -276,7 +276,7 @@ bool CheckMemoryLeak::isReopenStandardStream(const Token *tok) const
276276

277277
bool CheckMemoryLeak::isOpenDevNull(const Token *tok) const
278278
{
279-
if (mSettings_->posix() && tok->str() == "open" && numberOfArguments(tok) == 2) {
279+
if (mSettings_->hasLib("posix") && tok->str() == "open" && numberOfArguments(tok) == 2) {
280280
const Token* arg = getArguments(tok).at(0);
281281
if (Token::simpleMatch(arg, "\"/dev/null\""))
282282
return true;

lib/settings.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ class CPPCHECKLIB Settings : public cppcheck::Platform {
417417
*/
418418
bool isEnabled(const ValueFlow::Value *value, bool inconclusiveCheck=false) const;
419419

420-
/** Is posix library specified? */
421-
bool posix() const {
422-
return std::find(libraries.cbegin(), libraries.cend(), "posix") != libraries.cend();
420+
/** Is library specified? */
421+
bool hasLib(const std::string &lib) const {
422+
return std::find(libraries.cbegin(), libraries.cend(), lib) != libraries.cend();
423423
}
424424

425425
/** @brief Request termination of checking */

test/testautovariables.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class TestAutoVariables : public TestFixture {
5252
settings.severity.enable(Severity::style);
5353
LOAD_LIB_2(settings.library, "std.cfg");
5454
LOAD_LIB_2(settings.library, "qt.cfg");
55+
settings.libraries.emplace_back("qt");
5556

5657
TEST_CASE(testautovar1);
5758
TEST_CASE(testautovar2);

test/testbufferoverrun.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3528,9 +3528,7 @@ class TestBufferOverrun : public TestFixture {
35283528
" <arg nr=\"2\"/>\n"
35293529
" </function>\n"
35303530
"</def>";
3531-
tinyxml2::XMLDocument doc;
3532-
doc.Parse(xmldata, sizeof(xmldata));
3533-
settings.library.load(doc);
3531+
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
35343532

35353533
// Attempt to get size from Cfg files, no false positives if size is not specified
35363534
check("void f() {\n"
@@ -4083,9 +4081,7 @@ class TestBufferOverrun : public TestFixture {
40834081
" <arg nr=\"3\"/>\n"
40844082
" </function>\n"
40854083
"</def>";
4086-
tinyxml2::XMLDocument doc;
4087-
doc.Parse(xmldata, sizeof(xmldata));
4088-
settings.library.load(doc);
4084+
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
40894085
settings.severity.enable(Severity::warning);
40904086
settings.sizeof_wchar_t = 4;
40914087

@@ -4225,9 +4221,7 @@ class TestBufferOverrun : public TestFixture {
42254221
" <arg nr=\"3\"/>\n"
42264222
" </function>\n"
42274223
"</def>";
4228-
tinyxml2::XMLDocument doc;
4229-
doc.Parse(xmldata, sizeof(xmldata));
4230-
settings.library.load(doc);
4224+
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
42314225

42324226
check("void f() {\n"
42334227
" char c[7];\n"
@@ -4289,9 +4283,7 @@ class TestBufferOverrun : public TestFixture {
42894283
" </arg>\n"
42904284
" </function>\n"
42914285
"</def>";
4292-
tinyxml2::XMLDocument doc;
4293-
doc.Parse(xmldata, sizeof(xmldata));
4294-
settings.library.load(doc);
4286+
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
42954287

42964288
// formatstr..
42974289
check("void f() {\n"
@@ -4403,9 +4395,7 @@ class TestBufferOverrun : public TestFixture {
44034395
" <arg nr=\"4\"/>\n"
44044396
" </function>\n"
44054397
"</def>";
4406-
tinyxml2::XMLDocument doc;
4407-
doc.Parse(xmldata, sizeof(xmldata));
4408-
settings.library.load(doc);
4398+
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
44094399

44104400
check("void f() {\n"
44114401
" char c[5];\n"
@@ -5498,6 +5488,7 @@ class TestBufferOverrun : public TestFixture {
54985488

54995489
Settings settings;
55005490
LOAD_LIB_2(settings.library, "posix.cfg");
5491+
settings.libraries.emplace_back("posix");
55015492

55025493
check("void f(){\n"
55035494
"int pipefd[1];\n" // <-- array of two integers is needed

test/testclass.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ class TestClass : public TestFixture {
6060
" </access>\n"
6161
" </container>\n"
6262
"</def>";
63-
tinyxml2::XMLDocument doc;
64-
doc.Parse(xmldata, sizeof(xmldata));
65-
settings0.library.load(doc);
66-
settings1.library.load(doc);
63+
ASSERT(settings0.library.loadxmldata(xmldata, sizeof(xmldata)));
64+
ASSERT(settings1.library.loadxmldata(xmldata, sizeof(xmldata)));
6765
}
6866

6967

@@ -3390,9 +3388,7 @@ class TestClass : public TestFixture {
33903388
" <podtype name=\"std::uint8_t\" sign=\"u\" size=\"1\"/>\n"
33913389
" <podtype name=\"std::atomic_bool\"/>\n"
33923390
"</def>";
3393-
tinyxml2::XMLDocument doc;
3394-
doc.Parse(xmldata, sizeof(xmldata));
3395-
settings.library.load(doc);
3391+
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
33963392

33973393
checkNoMemset("class A {\n"
33983394
" std::array<int, 10> ints;\n"

test/testcondition.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class TestCondition : public TestFixture {
4949
PLATFORM(settings1, cppcheck::Platform::Native);
5050

5151
LOAD_LIB_2(settings0.library, "qt.cfg");
52+
settings0.libraries.emplace_back("qt");
5253
LOAD_LIB_2(settings0.library, "std.cfg");
5354

5455
settings0.severity.enable(Severity::style);
@@ -58,11 +59,9 @@ class TestCondition : public TestFixture {
5859
"<def>\n"
5960
" <function name=\"bar\"> <pure/> </function>\n"
6061
"</def>";
61-
tinyxml2::XMLDocument xmldoc;
62-
xmldoc.Parse(cfg, sizeof(cfg));
62+
ASSERT(settings1.library.loadxmldata(cfg, sizeof(cfg)));
6363
settings1.severity.enable(Severity::style);
6464
settings1.severity.enable(Severity::warning);
65-
settings1.library.load(xmldoc);
6665

6766
TEST_CASE(assignAndCompare); // assignment and comparison don't match
6867
TEST_CASE(mismatchingBitAnd); // overlapping bitmasks

test/testconstructors.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,7 +1952,8 @@ class TestConstructors : public TestFixture {
19521952

19531953
void initvar_smartptr() { // #10237
19541954
Settings s;
1955-
s.libraries.emplace_back("std");
1955+
// TODO: test shuld probably not pass without library
1956+
//LOAD_LIB_2(s.library, "std.cfg");
19561957
check("struct S {\n"
19571958
" explicit S(const std::shared_ptr<S>& sp) {\n"
19581959
" set(*sp);\n"
@@ -3589,7 +3590,9 @@ class TestConstructors : public TestFixture {
35893590

35903591
void uninitVarInheritClassInit() {
35913592
Settings s;
3592-
s.libraries.emplace_back("vcl");
3593+
// TODO: test should probably not pass without library
3594+
//LOAD_LIB_2(s.library, "vcl.cfg");
3595+
//s.libraries.emplace_back("vcl");
35933596

35943597
check("class Fred: public TObject\n"
35953598
"{\n"

test/testerrorlogger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ class TestErrorLogger : public TestFixture {
249249
" <location file=\"foo.cpp\" line=\"5\" column=\"2\"/>\n"
250250
"</error>";
251251
tinyxml2::XMLDocument doc;
252-
doc.Parse(xmldata, sizeof(xmldata));
252+
ASSERT(doc.Parse(xmldata, sizeof(xmldata)) == tinyxml2::XML_SUCCESS);
253253
ErrorMessage msg(doc.FirstChildElement());
254254
ASSERT_EQUALS("errorId", msg.id);
255255
ASSERT_EQUALS(Severity::error, msg.severity);

test/testfunctions.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ class TestFunctions : public TestFixture {
4343
settings.severity.enable(Severity::performance);
4444
settings.severity.enable(Severity::portability);
4545
settings.certainty.enable(Certainty::inconclusive);
46-
settings.libraries.emplace_back("posix");
4746
settings.standards.c = Standards::C11;
4847
settings.standards.cpp = Standards::CPP11;
4948
LOAD_LIB_2(settings.library, "std.cfg");
5049
LOAD_LIB_2(settings.library, "posix.cfg");
50+
settings.libraries.emplace_back("posix");
5151

5252
// Prohibited functions
5353
TEST_CASE(prohibitedFunctions_posix);
@@ -1313,9 +1313,7 @@ class TestFunctions : public TestFixture {
13131313
" <arg nr=\"2\"/>\n"
13141314
" </function>\n"
13151315
"</def>";
1316-
tinyxml2::XMLDocument doc;
1317-
doc.Parse(xmldata, sizeof(xmldata));
1318-
settings2.library.load(doc);
1316+
ASSERT(settings2.library.loadxmldata(xmldata, sizeof(xmldata)));
13191317

13201318
check("void foo() {\n"
13211319
" mystrcmp(a, b);\n"
@@ -1468,9 +1466,7 @@ class TestFunctions : public TestFixture {
14681466
" <arg nr=\"2\"/>\n"
14691467
" </function>\n"
14701468
"</def>";
1471-
tinyxml2::XMLDocument doc;
1472-
doc.Parse(xmldata, sizeof(xmldata));
1473-
settings2.library.load(doc);
1469+
ASSERT(settings2.library.loadxmldata(xmldata, sizeof(xmldata)));
14741470

14751471
check("void foo() {\n"
14761472
" mystrcmp(a, b);\n"

0 commit comments

Comments
 (0)