Skip to content

Commit 5833fc3

Browse files
authored
testrunner: even more SettingsBuilder usage and const cleanups (danmar#5030)
* moved some of the test-only `Library::loadxmldata()` calls into `test` * testrunner: reduced need for backup/restore of settings
1 parent 9770dd7 commit 5833fc3

17 files changed

+215
-229
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ cli/stacktrace.o: cli/stacktrace.cpp cli/stacktrace.h lib/config.h lib/utils.h
667667
cli/threadexecutor.o: cli/threadexecutor.cpp cli/cppcheckexecutor.h cli/executor.h cli/threadexecutor.h lib/analyzerinfo.h lib/check.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h
668668
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/threadexecutor.cpp
669669

670-
test/fixture.o: test/fixture.cpp 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/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h test/fixture.h test/options.h test/redirect.h
670+
test/fixture.o: test/fixture.cpp externals/tinyxml2/tinyxml2.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/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h test/fixture.h test/options.h test/redirect.h
671671
$(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/fixture.cpp
672672

673673
test/helpers.o: test/helpers.cpp externals/simplecpp/simplecpp.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/preprocessor.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/helpers.h

test/fixture.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include <sstream>
3131
#include <string>
3232

33+
#include <tinyxml2.h>
34+
3335
std::ostringstream errout;
3436
std::ostringstream output;
3537

@@ -421,3 +423,13 @@ TestFixture::SettingsBuilder& TestFixture::SettingsBuilder::platform(cppcheck::P
421423
throw std::runtime_error("platform '" + platformStr + "' not found");
422424
return *this;
423425
}
426+
427+
TestFixture::SettingsBuilder& TestFixture::SettingsBuilder::libraryxml(const char xmldata[], std::size_t len)
428+
{
429+
tinyxml2::XMLDocument doc;
430+
if (tinyxml2::XML_SUCCESS != doc.Parse(xmldata, len))
431+
throw std::runtime_error("loading XML data failed");
432+
if (settings.library.load(doc).errorcode != Library::ErrorCode::OK)
433+
throw std::runtime_error("loading library XML failed");
434+
return *this;
435+
}

test/fixture.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ class TestFixture : public ErrorLogger {
176176

177177
SettingsBuilder& library(const char lib[]);
178178

179+
SettingsBuilder& libraryxml(const char xmldata[], std::size_t len);
180+
179181
SettingsBuilder& platform(cppcheck::Platform::Type type);
180182

181183
SettingsBuilder& checkConfiguration() {

test/testbufferoverrun.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3532,7 +3532,6 @@ class TestBufferOverrun : public TestFixture {
35323532
}
35333533

35343534
void buffer_overrun_readSizeFromCfg() {
3535-
Settings settings;
35363535
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
35373536
"<def>\n"
35383537
" <podtype name=\"u8\" sign=\"u\" size=\"1\"/>\n"
@@ -3544,7 +3543,7 @@ class TestBufferOverrun : public TestFixture {
35443543
" <arg nr=\"2\"/>\n"
35453544
" </function>\n"
35463545
"</def>";
3547-
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
3546+
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
35483547

35493548
// Attempt to get size from Cfg files, no false positives if size is not specified
35503549
check("void f() {\n"
@@ -4085,7 +4084,6 @@ class TestBufferOverrun : public TestFixture {
40854084
// extracttests.disable
40864085

40874086
void minsize_argvalue() {
4088-
Settings settings;
40894087
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
40904088
"<def>\n"
40914089
" <function name=\"mymemset\">\n"
@@ -4097,8 +4095,7 @@ class TestBufferOverrun : public TestFixture {
40974095
" <arg nr=\"3\"/>\n"
40984096
" </function>\n"
40994097
"</def>";
4100-
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
4101-
settings.severity.enable(Severity::warning);
4098+
Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).severity(Severity::warning).build();
41024099
settings.platform.sizeof_wchar_t = 4;
41034100

41044101
check("void f() {\n"
@@ -4224,7 +4221,6 @@ class TestBufferOverrun : public TestFixture {
42244221
}
42254222

42264223
void minsize_sizeof() {
4227-
Settings settings;
42284224
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
42294225
"<def>\n"
42304226
" <function name=\"mystrncpy\">\n"
@@ -4237,7 +4233,7 @@ class TestBufferOverrun : public TestFixture {
42374233
" <arg nr=\"3\"/>\n"
42384234
" </function>\n"
42394235
"</def>";
4240-
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
4236+
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
42414237

42424238
check("void f() {\n"
42434239
" char c[7];\n"
@@ -4285,7 +4281,6 @@ class TestBufferOverrun : public TestFixture {
42854281
}
42864282

42874283
void minsize_strlen() {
4288-
Settings settings;
42894284
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
42904285
"<def>\n"
42914286
" <function name=\"mysprintf\">\n"
@@ -4299,7 +4294,7 @@ class TestBufferOverrun : public TestFixture {
42994294
" </arg>\n"
43004295
" </function>\n"
43014296
"</def>";
4302-
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
4297+
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
43034298

43044299
// formatstr..
43054300
check("void f() {\n"
@@ -4399,7 +4394,6 @@ class TestBufferOverrun : public TestFixture {
43994394
}
44004395

44014396
void minsize_mul() {
4402-
Settings settings;
44034397
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
44044398
"<def>\n"
44054399
" <function name=\"myfread\">\n"
@@ -4411,7 +4405,7 @@ class TestBufferOverrun : public TestFixture {
44114405
" <arg nr=\"4\"/>\n"
44124406
" </function>\n"
44134407
"</def>";
4414-
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
4408+
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
44154409

44164410
check("void f() {\n"
44174411
" char c[5];\n"

test/testclass.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3397,13 +3397,12 @@ class TestClass : public TestFixture {
33973397
}
33983398

33993399
void memsetOnStdPodType() { // Ticket #5901
3400-
Settings settings;
34013400
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
34023401
"<def>\n"
34033402
" <podtype name=\"std::uint8_t\" sign=\"u\" size=\"1\"/>\n"
34043403
" <podtype name=\"std::atomic_bool\"/>\n"
34053404
"</def>";
3406-
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
3405+
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
34073406

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

test/testconstructors.cpp

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class TestConstructors : public TestFixture {
3232
TestConstructors() : TestFixture("TestConstructors") {}
3333

3434
private:
35-
Settings settings = settingsBuilder().severity(Severity::style).severity(Severity::warning).build();
35+
const Settings settings = settingsBuilder().severity(Severity::style).severity(Severity::warning).build();
3636

3737
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
3838
void check_(const char* file, int line, const char code[], bool inconclusive = false) {
@@ -1493,29 +1493,31 @@ class TestConstructors : public TestFixture {
14931493
}
14941494

14951495
void initvar_private_constructor() {
1496-
const Settings settingsOld = settings;
1497-
settings.standards.cpp = Standards::CPP11;
1498-
check("class Fred\n"
1499-
"{\n"
1500-
"private:\n"
1501-
" int var;\n"
1502-
" Fred();\n"
1503-
"};\n"
1504-
"Fred::Fred()\n"
1505-
"{ }");
1506-
ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable 'Fred::var' is not initialized in the constructor.\n", errout.str());
1507-
1508-
settings.standards.cpp = Standards::CPP03;
1509-
check("class Fred\n"
1510-
"{\n"
1511-
"private:\n"
1512-
" int var;\n"
1513-
" Fred();\n"
1514-
"};\n"
1515-
"Fred::Fred()\n"
1516-
"{ }");
1517-
ASSERT_EQUALS("", errout.str());
1518-
settings = settingsOld;
1496+
{
1497+
const Settings s = settingsBuilder(settings).cpp( Standards::CPP11).build();
1498+
check("class Fred\n"
1499+
"{\n"
1500+
"private:\n"
1501+
" int var;\n"
1502+
" Fred();\n"
1503+
"};\n"
1504+
"Fred::Fred()\n"
1505+
"{ }", s);
1506+
ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable 'Fred::var' is not initialized in the constructor.\n", errout.str());
1507+
}
1508+
1509+
{
1510+
const Settings s = settingsBuilder(settings).cpp(Standards::CPP03).build();
1511+
check("class Fred\n"
1512+
"{\n"
1513+
"private:\n"
1514+
" int var;\n"
1515+
" Fred();\n"
1516+
"};\n"
1517+
"Fred::Fred()\n"
1518+
"{ }", s);
1519+
ASSERT_EQUALS("", errout.str());
1520+
}
15191521
}
15201522

15211523
void initvar_copy_constructor() { // ticket #1611
@@ -3540,19 +3542,23 @@ class TestConstructors : public TestFixture {
35403542
}
35413543

35423544
void privateCtor1() {
3543-
settings.standards.cpp = Standards::CPP03;
3544-
check("class Foo {\n"
3545-
" int foo;\n"
3546-
" Foo() { }\n"
3547-
"};");
3548-
ASSERT_EQUALS("", errout.str());
3549-
3550-
settings.standards.cpp = Standards::CPP11;
3551-
check("class Foo {\n"
3552-
" int foo;\n"
3553-
" Foo() { }\n"
3554-
"};");
3555-
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Foo::foo' is not initialized in the constructor.\n", errout.str());
3545+
{
3546+
const Settings s = settingsBuilder(settings).cpp(Standards::CPP03).build();
3547+
check("class Foo {\n"
3548+
" int foo;\n"
3549+
" Foo() { }\n"
3550+
"};", s);
3551+
ASSERT_EQUALS("", errout.str());
3552+
}
3553+
3554+
{
3555+
const Settings s = settingsBuilder(settings).cpp(Standards::CPP11).build();
3556+
check("class Foo {\n"
3557+
" int foo;\n"
3558+
" Foo() { }\n"
3559+
"};", s);
3560+
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Foo::foo' is not initialized in the constructor.\n", errout.str());
3561+
}
35563562
}
35573563

35583564
void privateCtor2() {

test/testexceptionsafety.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,19 @@ class TestExceptionSafety : public TestFixture {
6060
}
6161

6262
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
63-
void check_(const char* file, int line, const char code[], bool inconclusive = false) {
63+
void check_(const char* file, int line, const char code[], bool inconclusive = false, const Settings *s = nullptr) {
6464
// Clear the error buffer..
6565
errout.str("");
6666

67-
settings.certainty.setEnabled(Certainty::inconclusive, inconclusive);
67+
Settings settings1 = settingsBuilder(s ? *s : settings).certainty(Certainty::inconclusive, inconclusive).build();
6868

6969
// Tokenize..
70-
Tokenizer tokenizer(&settings, this);
70+
Tokenizer tokenizer(&settings1, this);
7171
std::istringstream istr(code);
7272
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
7373

7474
// Check char variable usage..
75-
runChecks<CheckExceptionSafety>(&tokenizer, &settings, this);
75+
runChecks<CheckExceptionSafety>(&tokenizer, &settings1, this);
7676
}
7777

7878
void destructors() {
@@ -398,13 +398,9 @@ class TestExceptionSafety : public TestFixture {
398398
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (style, inconclusive) Unhandled exception specification when calling function f().\n"
399399
"[test.cpp:6] -> [test.cpp:1]: (style, inconclusive) Unhandled exception specification when calling function f().\n", errout.str());
400400

401-
Settings settingsOld = settings;
402-
LOAD_LIB_2(settings.library, "gnu.cfg");
403-
404-
check(code, true);
401+
const Settings s = settingsBuilder(settings).library("gnu.cfg").build();
402+
check(code, true, &s);
405403
ASSERT_EQUALS("", errout.str());
406-
407-
settings = settingsOld;
408404
}
409405

410406
void nothrowAttributeThrow() {

0 commit comments

Comments
 (0)