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
113 changes: 57 additions & 56 deletions test/testbufferoverrun.cpp

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class TestClass : public TestFixture {

private:
const Settings settings0 = settingsBuilder().severity(Severity::style).library("std.cfg").build();
const Settings settings0_i = settingsBuilder(settings0).certainty(Certainty::inconclusive).build();
const Settings settings1 = settingsBuilder().severity(Severity::warning).library("std.cfg").build();
const Settings settings2 = settingsBuilder().severity(Severity::style).library("std.cfg").certainty(Certainty::inconclusive).build();
const Settings settings3 = settingsBuilder().severity(Severity::style).library("std.cfg").severity(Severity::warning).build();
Expand Down Expand Up @@ -3655,16 +3656,19 @@ class TestClass : public TestFixture {

struct CheckConstOptions
{
const Settings *s = nullptr;
bool inconclusive = true;
};

#define checkConst(...) checkConst_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
void checkConst_(const char* file, int line, const char (&code)[size], const CheckConstOptions& options = make_default_obj()) {
const Settings settings = settingsBuilder(options.s ? *options.s : settings0).certainty(Certainty::inconclusive, options.inconclusive).build();
const Settings& settings = options.inconclusive ? settings0_i : settings0;

// Tokenize..
checkConst_(file, line, code, settings);
}

template<size_t size>
void checkConst_(const char* file, int line, const char (&code)[size], const Settings& settings) {
SimpleTokenizer tokenizer(settings, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);

Expand Down Expand Up @@ -7747,15 +7751,15 @@ class TestClass : public TestFixture {
}

void qualifiedNameMember() { // #10872
const Settings s = settingsBuilder().severity(Severity::style).debugwarnings().library("std.cfg").build();
const Settings s = settingsBuilder().severity(Severity::style).debugwarnings().library("std.cfg").certainty(Certainty::inconclusive).build();
checkConst("struct data {};\n"
" struct S {\n"
" std::vector<data> std;\n"
" void f();\n"
"};\n"
"void S::f() {\n"
" std::vector<data>::const_iterator end = std.end();\n"
"}\n", dinit(CheckConstOptions, $.s = &s));
"}\n", s);
ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:6:9]: (style, inconclusive) Technically the member function 'S::f' can be const. [functionConst]\n", errout_str());
}

Expand Down
68 changes: 35 additions & 33 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,18 @@ class TestCondition : public TestFixture {

struct CheckOptions
{
const Settings* s = nullptr;
bool cpp = true;
};

#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) {
const Settings& settings = options.s ? *options.s : settings0;
check_(file, line, code, settings0, options.cpp);
}

SimpleTokenizer2 tokenizer(settings, *this, code, options.cpp ? "test.cpp" : "test.c");
template<size_t size>
void check_(const char* file, int line, const char (&code)[size], const Settings& settings, bool cpp = true) {
SimpleTokenizer2 tokenizer(settings, *this, code, cpp ? "test.cpp" : "test.c");

// Tokenizer..
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);
Expand Down Expand Up @@ -515,7 +517,16 @@ class TestCondition : public TestFixture {
errout_str());
}

#define checkPureFunction(code) checkPureFunction_(code, __FILE__, __LINE__)
#define checkPureFunction(...) checkPureFunction_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
void checkPureFunction_(const char* file, int line, const char (&code)[size]) {
// Tokenize..
SimpleTokenizer tokenizer(settings1, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);

runChecks<CheckCondition>(tokenizer, this);
}

void multicompare() {
check("void foo(int x)\n"
"{\n"
Expand Down Expand Up @@ -574,15 +585,6 @@ class TestCondition : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

template<size_t size>
void checkPureFunction_(const char (&code)[size], const char* file, int line) {
// Tokenize..
SimpleTokenizer tokenizer(settings1, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);

runChecks<CheckCondition>(tokenizer, this);
}

void overlappingElseIfCondition() {
check("void f(int a, int &b) {\n"
" if (a) { b = 1; }\n"
Expand Down Expand Up @@ -1309,27 +1311,27 @@ class TestCondition : public TestFixture {
const Settings s = settingsBuilder(settings0).certainty(Certainty::inconclusive).build();
check("void f(char x) {\n"
" if (x == '1' || x == '2') {}\n"
"}", dinit(CheckOptions, $.s = &s));
"}", s);
ASSERT_EQUALS("", errout_str());

check("void f(char x) {\n"
" if (x == '1' && x == '2') {}\n"
"}", dinit(CheckOptions, $.s = &s));
"}", s);
ASSERT_EQUALS("[test.cpp:2:16]: (warning) Logical conjunction always evaluates to false: x == '1' && x == '2'. [incorrectLogicOperator]\n", errout_str());

check("int f(char c) {\n"
" return (c >= 'a' && c <= 'z');\n"
"}", dinit(CheckOptions, $.s = &s));
"}", s);
ASSERT_EQUALS("", errout_str());

check("int f(char c) {\n"
" return (c <= 'a' && c >= 'z');\n"
"}", dinit(CheckOptions, $.s = &s));
"}", s);
ASSERT_EQUALS("[test.cpp:2:20]: (warning, inconclusive) Logical conjunction always evaluates to false: c <= 'a' && c >= 'z'. [incorrectLogicOperator]\n", errout_str());

check("int f(char c) {\n"
" return (c <= 'a' && c >= 'z');\n"
"}");
"}"); // TODO: use s?
ASSERT_EQUALS("[test.cpp:2:13] -> [test.cpp:2:25]: (style) Return value 'c>='z'' is always false [knownConditionTrueFalse]\n", errout_str());
}

Expand Down Expand Up @@ -6142,65 +6144,65 @@ class TestCondition : public TestFixture {

check("void f(unsigned char c) {\n"
" if (c == 256) {}\n"
"}", dinit(CheckOptions, $.s = &settingsUnix64));
"}", settingsUnix64);
ASSERT_EQUALS("[test.cpp:2:12]: (style) Comparing expression of type 'unsigned char' against value 256. Condition is always false. [compareValueOutOfTypeRangeError]\n", errout_str());

check("void f(unsigned char* b, int i) {\n" // #6372
" if (b[i] == 256) {}\n"
"}", dinit(CheckOptions, $.s = &settingsUnix64));
"}", settingsUnix64);
ASSERT_EQUALS("[test.cpp:2:15]: (style) Comparing expression of type 'unsigned char' against value 256. Condition is always false. [compareValueOutOfTypeRangeError]\n", errout_str());

check("void f(unsigned char c) {\n"
" if (c == 255) {}\n"
"}", dinit(CheckOptions, $.s = &settingsUnix64));
"}", settingsUnix64);
ASSERT_EQUALS("", errout_str());

check("void f(bool b) {\n"
" if (b == true) {}\n"
"}", dinit(CheckOptions, $.s = &settingsUnix64));
"}", settingsUnix64);
ASSERT_EQUALS("", errout_str());

// #10372
check("void f(signed char x) {\n"
" if (x == 0xff) {}\n"
"}", dinit(CheckOptions, $.s = &settingsUnix64));
"}", settingsUnix64);
ASSERT_EQUALS("[test.cpp:2:12]: (style) Comparing expression of type 'signed char' against value 255. Condition is always false. [compareValueOutOfTypeRangeError]\n", errout_str());

check("void f(short x) {\n"
" if (x == 0xffff) {}\n"
"}", dinit(CheckOptions, $.s = &settingsUnix64));
"}", settingsUnix64);
ASSERT_EQUALS("[test.cpp:2:12]: (style) Comparing expression of type 'signed short' against value 65535. Condition is always false. [compareValueOutOfTypeRangeError]\n", errout_str());

check("void f(int x) {\n"
" if (x == 0xffffffff) {}\n"
"}", dinit(CheckOptions, $.s = &settingsUnix64));
"}", settingsUnix64);
ASSERT_EQUALS("", errout_str());

check("void f(long x) {\n"
" if (x == ~0L) {}\n"
"}", dinit(CheckOptions, $.s = &settingsUnix64));
"}", settingsUnix64);
ASSERT_EQUALS("", errout_str());

check("void f(long long x) {\n"
" if (x == ~0LL) {}\n"
"}", dinit(CheckOptions, $.s = &settingsUnix64));
"}", settingsUnix64);
ASSERT_EQUALS("", errout_str());

check("int f(int x) {\n"
" const int i = 0xFFFFFFFF;\n"
" if (x == i) {}\n"
"}", dinit(CheckOptions, $.s = &settingsUnix64));
"}", settingsUnix64);
ASSERT_EQUALS("", errout_str());

check("void f() {\n"
" char c;\n"
" if ((c = foo()) != -1) {}\n"
"}", dinit(CheckOptions, $.s = &settingsUnix64));
"}", settingsUnix64);
ASSERT_EQUALS("", errout_str());

check("void f(int x) {\n"
" if (x < 3000000000) {}\n"
"}", dinit(CheckOptions, $.s = &settingsUnix64));
"}", settingsUnix64);
ASSERT_EQUALS("[test.cpp:2:11]: (style) Comparing expression of type 'signed int' against value 3000000000. Condition is always true. [compareValueOutOfTypeRangeError]\n", errout_str());

check("void f(const signed char i) {\n" // #8545
Expand All @@ -6210,7 +6212,7 @@ class TestCondition : public TestFixture {
" if (i < +128) {}\n" // warn
" if (i <= +127) {}\n" // warn
" if (i <= +126) {}\n"
"}\n", dinit(CheckOptions, $.s = &settingsUnix64));
"}\n", settingsUnix64);
ASSERT_EQUALS("[test.cpp:2:15]: (style) Comparing expression of type 'const signed char' against value -129. Condition is always true. [compareValueOutOfTypeRangeError]\n"
"[test.cpp:3:15]: (style) Comparing expression of type 'const signed char' against value -128. Condition is always true. [compareValueOutOfTypeRangeError]\n"
"[test.cpp:5:15]: (style) Comparing expression of type 'const signed char' against value 128. Condition is always true. [compareValueOutOfTypeRangeError]\n"
Expand All @@ -6234,7 +6236,7 @@ class TestCondition : public TestFixture {
" if (255 > u) {}\n"
" if (255 <= u) {}\n"
" if (255 >= u) {}\n" // warn
"}\n", dinit(CheckOptions, $.s = &settingsUnix64));
"}\n", settingsUnix64);
ASSERT_EQUALS("[test.cpp:3:14]: (style) Comparing expression of type 'const unsigned char' against value 0. Condition is always false. [compareValueOutOfTypeRangeError]\n"
"[test.cpp:4:14]: (style) Comparing expression of type 'const unsigned char' against value 0. Condition is always true. [compareValueOutOfTypeRangeError]\n"
"[test.cpp:6:14]: (style) Comparing expression of type 'const unsigned char' against value 255. Condition is always false. [compareValueOutOfTypeRangeError]\n"
Expand All @@ -6247,7 +6249,7 @@ class TestCondition : public TestFixture {

check("void f(bool b) {\n" // #14037
" if (b != 2) {}\n"
"}\n", dinit(CheckOptions, $.s = &settingsUnix64));
"}\n", settingsUnix64);
ASSERT_EQUALS("[test.cpp:2:14]: (style) Comparing expression of type 'bool' against value 2. Condition is always true. [compareValueOutOfTypeRangeError]\n",
errout_str());
}
Expand Down
28 changes: 16 additions & 12 deletions test/testconstructors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,23 @@ class TestConstructors : public TestFixture {

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

struct CheckOptions
{
bool inconclusive = false;
const Settings* s = nullptr;
};

#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) {
const Settings settings1 = settingsBuilder(options.s ? *options.s : settings).certainty(Certainty::inconclusive, options.inconclusive).build();
const Settings settings1 = options.inconclusive ? settings_i : settings;

// Tokenize..
check_(file, line, code, settings1);
}

template<size_t size>
void check_(const char* file, int line, const char (&code)[size], const Settings& settings1) {
SimpleTokenizer tokenizer(settings1, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);

Expand Down Expand Up @@ -1593,7 +1597,7 @@ class TestConstructors : public TestFixture {
" Fred();\n"
"};\n"
"Fred::Fred()\n"
"{ }", dinit(CheckOptions, $.s = &s));
"{ }", s);
ASSERT_EQUALS("[test.cpp:7:7]: (warning) Member variable 'Fred::var' is not initialized in the constructor. [uninitMemberVarPrivate]\n", errout_str());
}

Expand All @@ -1606,7 +1610,7 @@ class TestConstructors : public TestFixture {
" Fred();\n"
"};\n"
"Fred::Fred()\n"
"{ }", dinit(CheckOptions, $.s = &s));
"{ }", s);
ASSERT_EQUALS("", errout_str());
}
}
Expand Down Expand Up @@ -2064,7 +2068,7 @@ class TestConstructors : public TestFixture {
" d = rhs.get();\n"
" }\n"
" double d;\n"
"};", dinit(CheckOptions, $.s = &s));
"};", s);
ASSERT_EQUALS("", errout_str());

check("struct S {\n" // #8485
Expand Down Expand Up @@ -2098,7 +2102,7 @@ class TestConstructors : public TestFixture {
check("struct S {\n"
" S& operator=(const S& s) { return *this; }\n"
" std::mutex m;\n"
"};\n", dinit(CheckOptions, $.s = &s));
"};\n", s);
ASSERT_EQUALS("", errout_str());
}

Expand Down Expand Up @@ -3046,7 +3050,7 @@ class TestConstructors : public TestFixture {
check("struct C {\n" // #13989
" C() = default;\n"
" std::list<int>::const_iterator it;\n"
"};\n", dinit(CheckOptions, $.s = &s));
"};\n", s);
ASSERT_EQUALS("[test.cpp:2:5]: (warning) Member variable 'C::it' is not initialized in the constructor. [uninitMemberVar]\n", errout_str());
}

Expand Down Expand Up @@ -3257,7 +3261,7 @@ class TestConstructors : public TestFixture {
" std::array<std::string, 2> e;\n"
" std::array<U, 2> f;\n"
"S() {}\n"
"};\n", dinit(CheckOptions, $.s = &s));
"};\n", s);

ASSERT_EQUALS("[test.cpp:10:1]: (warning) Member variable 'S::a' is not initialized in the constructor. [uninitMemberVar]\n"
"[test.cpp:10:1]: (warning) Member variable 'S::b' is not initialized in the constructor. [uninitMemberVar]\n"
Expand Down Expand Up @@ -3689,7 +3693,7 @@ class TestConstructors : public TestFixture {
check("class Foo {\n"
" int foo;\n"
" Foo() { }\n"
"};", dinit(CheckOptions, $.s = &s));
"};", s);
ASSERT_EQUALS("", errout_str());
}

Expand All @@ -3698,7 +3702,7 @@ class TestConstructors : public TestFixture {
check("class Foo {\n"
" int foo;\n"
" Foo() { }\n"
"};", dinit(CheckOptions, $.s = &s));
"};", s);
ASSERT_EQUALS("[test.cpp:3:5]: (warning) Member variable 'Foo::foo' is not initialized in the constructor. [uninitMemberVarPrivate]\n", errout_str());
}
}
Expand Down Expand Up @@ -3760,7 +3764,7 @@ class TestConstructors : public TestFixture {
" Fred() { }\n"
"private:\n"
" int x;\n"
"};", dinit(CheckOptions, $.s = &s));
"};", s);
ASSERT_EQUALS("", errout_str());
}

Expand Down
3 changes: 1 addition & 2 deletions test/testerrorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,7 @@ class TestErrorLogger : public TestFixture {
}
}

#define testReportType(reportType, severity, errorId, expectedClassification, expectedGuideline) \
testReportType_(__FILE__, __LINE__, reportType, severity, errorId, expectedClassification, expectedGuideline)
#define testReportType(...) testReportType_(__FILE__, __LINE__, __VA_ARGS__)
void testReportType_(const char *file, int line, ReportType reportType, Severity severity, const std::string &errorId,
const std::string &expectedClassification, const std::string &expectedGuideline) const
{
Expand Down
Loading