Skip to content
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 @@ -667,7 +667,7 @@ cli/stacktrace.o: cli/stacktrace.cpp cli/stacktrace.h lib/config.h lib/utils.h
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
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/threadexecutor.cpp

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

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
Expand Down
12 changes: 12 additions & 0 deletions test/fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <sstream>
#include <string>

#include <tinyxml2.h>

std::ostringstream errout;
std::ostringstream output;

Expand Down Expand Up @@ -421,3 +423,13 @@ TestFixture::SettingsBuilder& TestFixture::SettingsBuilder::platform(cppcheck::P
throw std::runtime_error("platform '" + platformStr + "' not found");
return *this;
}

TestFixture::SettingsBuilder& TestFixture::SettingsBuilder::libraryxml(const char xmldata[], std::size_t len)
{
tinyxml2::XMLDocument doc;
if (tinyxml2::XML_SUCCESS != doc.Parse(xmldata, len))
throw std::runtime_error("loading XML data failed");
if (settings.library.load(doc).errorcode != Library::ErrorCode::OK)
throw std::runtime_error("loading library XML failed");
return *this;
}
2 changes: 2 additions & 0 deletions test/fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ class TestFixture : public ErrorLogger {

SettingsBuilder& library(const char lib[]);

SettingsBuilder& libraryxml(const char xmldata[], std::size_t len);

SettingsBuilder& platform(cppcheck::Platform::Type type);

SettingsBuilder& checkConfiguration() {
Expand Down
16 changes: 5 additions & 11 deletions test/testbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3532,7 +3532,6 @@ class TestBufferOverrun : public TestFixture {
}

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

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

void minsize_argvalue() {
Settings settings;
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"mymemset\">\n"
Expand All @@ -4097,8 +4095,7 @@ class TestBufferOverrun : public TestFixture {
" <arg nr=\"3\"/>\n"
" </function>\n"
"</def>";
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
settings.severity.enable(Severity::warning);
Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).severity(Severity::warning).build();
settings.platform.sizeof_wchar_t = 4;

check("void f() {\n"
Expand Down Expand Up @@ -4224,7 +4221,6 @@ class TestBufferOverrun : public TestFixture {
}

void minsize_sizeof() {
Settings settings;
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"mystrncpy\">\n"
Expand All @@ -4237,7 +4233,7 @@ class TestBufferOverrun : public TestFixture {
" <arg nr=\"3\"/>\n"
" </function>\n"
"</def>";
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();

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

void minsize_strlen() {
Settings settings;
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"mysprintf\">\n"
Expand All @@ -4299,7 +4294,7 @@ class TestBufferOverrun : public TestFixture {
" </arg>\n"
" </function>\n"
"</def>";
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();

// formatstr..
check("void f() {\n"
Expand Down Expand Up @@ -4399,7 +4394,6 @@ class TestBufferOverrun : public TestFixture {
}

void minsize_mul() {
Settings settings;
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <function name=\"myfread\">\n"
Expand All @@ -4411,7 +4405,7 @@ class TestBufferOverrun : public TestFixture {
" <arg nr=\"4\"/>\n"
" </function>\n"
"</def>";
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();

check("void f() {\n"
" char c[5];\n"
Expand Down
3 changes: 1 addition & 2 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3397,13 +3397,12 @@ class TestClass : public TestFixture {
}

void memsetOnStdPodType() { // Ticket #5901
Settings settings;
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def>\n"
" <podtype name=\"std::uint8_t\" sign=\"u\" size=\"1\"/>\n"
" <podtype name=\"std::atomic_bool\"/>\n"
"</def>";
ASSERT(settings.library.loadxmldata(xmldata, sizeof(xmldata)));
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();

checkNoMemset("class A {\n"
" std::array<int, 10> ints;\n"
Expand Down
80 changes: 43 additions & 37 deletions test/testconstructors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TestConstructors : public TestFixture {
TestConstructors() : TestFixture("TestConstructors") {}

private:
Settings settings = settingsBuilder().severity(Severity::style).severity(Severity::warning).build();
const Settings settings = settingsBuilder().severity(Severity::style).severity(Severity::warning).build();

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

void initvar_private_constructor() {
const Settings settingsOld = settings;
settings.standards.cpp = Standards::CPP11;
check("class Fred\n"
"{\n"
"private:\n"
" int var;\n"
" Fred();\n"
"};\n"
"Fred::Fred()\n"
"{ }");
ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable 'Fred::var' is not initialized in the constructor.\n", errout.str());

settings.standards.cpp = Standards::CPP03;
check("class Fred\n"
"{\n"
"private:\n"
" int var;\n"
" Fred();\n"
"};\n"
"Fred::Fred()\n"
"{ }");
ASSERT_EQUALS("", errout.str());
settings = settingsOld;
{
const Settings s = settingsBuilder(settings).cpp( Standards::CPP11).build();
check("class Fred\n"
"{\n"
"private:\n"
" int var;\n"
" Fred();\n"
"};\n"
"Fred::Fred()\n"
"{ }", s);
ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable 'Fred::var' is not initialized in the constructor.\n", errout.str());
}

{
const Settings s = settingsBuilder(settings).cpp(Standards::CPP03).build();
check("class Fred\n"
"{\n"
"private:\n"
" int var;\n"
" Fred();\n"
"};\n"
"Fred::Fred()\n"
"{ }", s);
ASSERT_EQUALS("", errout.str());
}
}

void initvar_copy_constructor() { // ticket #1611
Expand Down Expand Up @@ -3540,19 +3542,23 @@ class TestConstructors : public TestFixture {
}

void privateCtor1() {
settings.standards.cpp = Standards::CPP03;
check("class Foo {\n"
" int foo;\n"
" Foo() { }\n"
"};");
ASSERT_EQUALS("", errout.str());

settings.standards.cpp = Standards::CPP11;
check("class Foo {\n"
" int foo;\n"
" Foo() { }\n"
"};");
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Foo::foo' is not initialized in the constructor.\n", errout.str());
{
const Settings s = settingsBuilder(settings).cpp(Standards::CPP03).build();
check("class Foo {\n"
" int foo;\n"
" Foo() { }\n"
"};", s);
ASSERT_EQUALS("", errout.str());
}

{
const Settings s = settingsBuilder(settings).cpp(Standards::CPP11).build();
check("class Foo {\n"
" int foo;\n"
" Foo() { }\n"
"};", s);
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'Foo::foo' is not initialized in the constructor.\n", errout.str());
}
}

void privateCtor2() {
Expand Down
16 changes: 6 additions & 10 deletions test/testexceptionsafety.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ class TestExceptionSafety : public TestFixture {
}

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

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

// Tokenize..
Tokenizer tokenizer(&settings, this);
Tokenizer tokenizer(&settings1, this);
std::istringstream istr(code);
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);

// Check char variable usage..
runChecks<CheckExceptionSafety>(&tokenizer, &settings, this);
runChecks<CheckExceptionSafety>(&tokenizer, &settings1, this);
}

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

Settings settingsOld = settings;
LOAD_LIB_2(settings.library, "gnu.cfg");

check(code, true);
const Settings s = settingsBuilder(settings).library("gnu.cfg").build();
check(code, true, &s);
ASSERT_EQUALS("", errout.str());

settings = settingsOld;
}

void nothrowAttributeThrow() {
Expand Down
Loading