Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testrunner: cleaned up tests by using objects to pass the options #7189

Merged
merged 1 commit into from
Jan 23, 2025
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
4 changes: 2 additions & 2 deletions test/test64bit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class Test64BitPortability : public TestFixture {
TEST_CASE(assignment);
}

#define check(code) check_(code, __FILE__, __LINE__)
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
void check_(const char (&code)[size], const char* file, int line) {
void check_(const char* file, int line, const char (&code)[size]) {
// Tokenize..
SimpleTokenizer tokenizer(settings, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);
Expand Down
35 changes: 17 additions & 18 deletions test/testastutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ class TestAstUtils : public TestFixture {
ASSERT_EQUALS(true, findLambdaEndToken("int i = 5 * []{ return 7; }();", "[", /*checkNext*/ false));
}

#define findLambdaStartToken(code) findLambdaStartToken_(code, __FILE__, __LINE__)
#define findLambdaStartToken(...) findLambdaStartToken_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
bool findLambdaStartToken_(const char (&code)[size], const char* file, int line) {
bool findLambdaStartToken_(const char* file, int line, const char (&code)[size]) {
SimpleTokenizer tokenizer(settingsDefault, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);
const Token * const tokStart = (::findLambdaStartToken)(tokenizer.list.back());
Expand Down Expand Up @@ -120,9 +120,9 @@ class TestAstUtils : public TestFixture {
ASSERT_EQUALS(true, findLambdaStartToken("[](void) constexpr -> const * const* int { return x; }"));
}

#define isNullOperand(code) isNullOperand_(code, __FILE__, __LINE__)
#define isNullOperand(...) isNullOperand_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
bool isNullOperand_(const char (&code)[size], const char* file, int line) {
bool isNullOperand_(const char* file, int line, const char (&code)[size]) {
SimpleTokenizer tokenizer(settingsDefault, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);
return (::isNullOperand)(tokenizer.tokens());
Expand All @@ -141,9 +141,9 @@ class TestAstUtils : public TestFixture {
ASSERT_EQUALS(false, isNullOperand("(void*)1;"));
}

#define isReturnScope(code, offset) isReturnScope_(code, offset, __FILE__, __LINE__)
#define isReturnScope(...) isReturnScope_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
bool isReturnScope_(const char (&code)[size], int offset, const char* file, int line) {
bool isReturnScope_(const char* file, int line, const char (&code)[size], int offset) {
SimpleTokenizer tokenizer(settingsDefault, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);
const Token * const tok = (offset < 0)
Expand Down Expand Up @@ -219,9 +219,9 @@ class TestAstUtils : public TestFixture {
isSameExpressionTestInternal(false);
}

#define isVariableChanged(code, startPattern, endPattern) isVariableChanged_(code, startPattern, endPattern, __FILE__, __LINE__)
#define isVariableChanged(...) isVariableChanged_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
bool isVariableChanged_(const char (&code)[size], const char startPattern[], const char endPattern[], const char* file, int line) {
bool isVariableChanged_(const char* file, int line, const char (&code)[size], const char startPattern[], const char endPattern[]) {
SimpleTokenizer tokenizer(settingsDefault, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);
const Token * const tok1 = Token::findsimplematch(tokenizer.tokens(), startPattern, strlen(startPattern));
Expand Down Expand Up @@ -252,9 +252,9 @@ class TestAstUtils : public TestFixture {
ASSERT_EQUALS(false, isVariableChanged("const int A[] = { 1, 2, 3 };", "[", "]"));
}

#define isVariableChangedByFunctionCall(code, pattern, inconclusive) isVariableChangedByFunctionCall_(code, pattern, inconclusive, __FILE__, __LINE__)
#define isVariableChangedByFunctionCall(...) isVariableChangedByFunctionCall_( __FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
bool isVariableChangedByFunctionCall_(const char (&code)[size], const char pattern[], bool *inconclusive, const char* file, int line) {
bool isVariableChangedByFunctionCall_(const char* file, int line, const char (&code)[size], const char pattern[], bool *inconclusive) {
SimpleTokenizer tokenizer(settingsDefault, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);
const Token * const argtok = Token::findmatch(tokenizer.tokens(), pattern);
Expand Down Expand Up @@ -412,15 +412,14 @@ class TestAstUtils : public TestFixture {
}
}

#define isExpressionChanged(code, var, startPattern, endPattern) \
isExpressionChanged_(code, var, startPattern, endPattern, __FILE__, __LINE__)
#define isExpressionChanged(...) isExpressionChanged_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
bool isExpressionChanged_(const char (&code)[size],
bool isExpressionChanged_(const char* file,
int line,
const char (&code)[size],
const char var[],
const char startPattern[],
const char endPattern[],
const char* file,
int line)
const char endPattern[])
{
const Settings settings = settingsBuilder().library("std.cfg").build();
SimpleTokenizer tokenizer(settings, *this);
Expand Down Expand Up @@ -451,9 +450,9 @@ class TestAstUtils : public TestFixture {
"}"));
}

#define nextAfterAstRightmostLeaf(code, parentPattern, rightPattern) nextAfterAstRightmostLeaf_(code, parentPattern, rightPattern, __FILE__, __LINE__)
#define nextAfterAstRightmostLeaf(...) nextAfterAstRightmostLeaf_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
bool nextAfterAstRightmostLeaf_(const char (&code)[size], const char parentPattern[], const char rightPattern[], const char* file, int line) {
bool nextAfterAstRightmostLeaf_(const char* file, int line, const char (&code)[size], const char parentPattern[], const char rightPattern[]) {
SimpleTokenizer tokenizer(settingsDefault, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);
const Token * tok = Token::findsimplematch(tokenizer.tokens(), parentPattern, strlen(parentPattern));
Expand Down
73 changes: 34 additions & 39 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,21 @@ class TestAutoVariables : public TestFixture {
private:
const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::style).library("std.cfg").library("qt.cfg").build();

struct CheckOptions
{
CheckOptions() = default;
bool inconclusive = true;
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], bool inconclusive = true, bool cpp = true) {
const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, inconclusive).build();
void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) {
const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, options.inconclusive).build();

// Tokenize..
SimpleTokenizer tokenizer(settings1, *this);
ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line);
ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line);

runChecks<CheckAutoVariables>(tokenizer, this);
}
Expand Down Expand Up @@ -248,14 +255,14 @@ class TestAutoVariables : public TestFixture {
"{\n"
" char a[10];\n"
" x->str = a;\n"
"}", false);
"}", dinit(CheckOptions, $.inconclusive = false));
ASSERT_EQUALS("", errout_str());

check("void foo(struct X *x)\n"
"{\n"
" char a[10];\n"
" x->str = a;\n"
"}", true);
"}");
ASSERT_EQUALS("[test.cpp:4]: (error, inconclusive) Address of local auto-variable assigned to a function parameter.\n", errout_str());
}

Expand All @@ -265,20 +272,20 @@ class TestAutoVariables : public TestFixture {
" struct txt_scrollpane_s * scrollpane;\n"
" target->parent = &scrollpane->widget;\n"
" return scrollpane;\n"
"}", false);
"}", dinit(CheckOptions, $.inconclusive = false));
ASSERT_EQUALS("", errout_str());
}

void testautovar8() {
check("void foo(int*& p) {\n"
" int i = 0;\n"
" p = &i;\n"
"}", false);
"}", dinit(CheckOptions, $.inconclusive = false));
ASSERT_EQUALS("[test.cpp:3]: (error) Address of local auto-variable assigned to a function parameter.\n", errout_str());

check("void foo(std::string& s) {\n"
" s = foo;\n"
"}", false);
"}", dinit(CheckOptions, $.inconclusive = false));
ASSERT_EQUALS("", errout_str());
}

Expand All @@ -289,7 +296,7 @@ class TestAutoVariables : public TestFixture {
" FN fn;\n"
" FP fp;\n"
" p = &fn.i;\n"
"}", false);
"}", dinit(CheckOptions, $.inconclusive = false));
ASSERT_EQUALS("[test.cpp:6]: (error) Address of local auto-variable assigned to a function parameter.\n", errout_str());

check("struct FN {int i;};\n"
Expand All @@ -298,7 +305,7 @@ class TestAutoVariables : public TestFixture {
" FN fn;\n"
" FP fp;\n"
" p = &p_fp->i;\n"
"}", false);
"}", dinit(CheckOptions, $.inconclusive = false));
ASSERT_EQUALS("", errout_str());

check("struct FN {int i;};\n"
Expand All @@ -307,7 +314,7 @@ class TestAutoVariables : public TestFixture {
" FN fn;\n"
" FP fp;\n"
" p = &fp.f->i;\n"
"}", false);
"}", dinit(CheckOptions, $.inconclusive = false));
ASSERT_EQUALS("", errout_str());
}

Expand Down Expand Up @@ -385,7 +392,7 @@ class TestAutoVariables : public TestFixture {
" int i = d;\n"
" d = i;\n"
" return d;"
"}",false);
"}",dinit(CheckOptions, $.inconclusive = false));
ASSERT_EQUALS("", errout_str());

check("void foo(int* ptr) {\n" // #4793
Expand Down Expand Up @@ -482,7 +489,7 @@ class TestAutoVariables : public TestFixture {
" if (lumdiff > 5.0f)\n"
" return &darkOutline;\n"
" return 0;\n"
"}", false);
"}", dinit(CheckOptions, $.inconclusive = false));
ASSERT_EQUALS("", errout_str());
}

Expand Down Expand Up @@ -917,7 +924,7 @@ class TestAutoVariables : public TestFixture {
check("void svn_repos_dir_delta2() {\n"
" struct context c;\n"
" SVN_ERR(delete(&c, root_baton, src_entry, pool));\n"
"}\n", false, /* cpp= */ false);
"}\n", dinit(CheckOptions, $.inconclusive = false, $.cpp = false));
ASSERT_EQUALS("", errout_str());
}

Expand Down Expand Up @@ -1323,7 +1330,7 @@ class TestAutoVariables : public TestFixture {
" double ret = getValue();\n"
" rd = ret;\n"
" return rd;\n"
"}", false);
"}", dinit(CheckOptions, $.inconclusive = false));
ASSERT_EQUALS("", errout_str());
}

Expand Down Expand Up @@ -1799,8 +1806,7 @@ class TestAutoVariables : public TestFixture {
"const int& bar(const std::unordered_map<int, int>& m, int k) {\n"
" auto x = 0;\n"
" return get_default(m, k, x);\n"
"}\n",
true);
"}\n");
ASSERT_EQUALS(
"[test.cpp:2] -> [test.cpp:4] -> [test.cpp:9] -> [test.cpp:9]: (error, inconclusive) Reference to local variable returned.\n",
errout_str());
Expand All @@ -1813,8 +1819,7 @@ class TestAutoVariables : public TestFixture {
"}\n"
"const int& bar(const std::unordered_map<int, int>& m, int k) {\n"
" return get_default(m, k, 0);\n"
"}\n",
true);
"}\n");
ASSERT_EQUALS(
"[test.cpp:2] -> [test.cpp:4] -> [test.cpp:8] -> [test.cpp:8]: (error, inconclusive) Reference to temporary returned.\n",
errout_str());
Expand Down Expand Up @@ -2564,8 +2569,7 @@ class TestAutoVariables : public TestFixture {
"const int* bar(const std::unordered_map<int, int>& m, int k) {\n"
" auto x = 0;\n"
" return get_default(m, k, &x);\n"
"}\n",
true);
"}\n");
ASSERT_EQUALS(
"[test.cpp:9] -> [test.cpp:9] -> [test.cpp:8] -> [test.cpp:9]: (error, inconclusive) Returning pointer to local variable 'x' that will be invalid when returning.\n",
errout_str());
Expand Down Expand Up @@ -2798,15 +2802,13 @@ class TestAutoVariables : public TestFixture {

check("std::string f(std::string Str, int first, int last) {\n"
" return { Str.begin() + first, Str.begin() + last + 1 };\n"
"}\n",
true);
"}\n");
ASSERT_EQUALS("", errout_str());

check("std::string f(std::string s) {\n"
" std::string r = { s.begin(), s.end() };\n"
" return r;\n"
"}\n",
true);
"}\n");
ASSERT_EQUALS("", errout_str());

check("struct A {\n"
Expand Down Expand Up @@ -3550,8 +3552,7 @@ class TestAutoVariables : public TestFixture {
" int i = 0;\n"
" A a{i};\n"
" return a;\n"
"}\n",
true);
"}\n");
ASSERT_EQUALS(
"[test.cpp:7] -> [test.cpp:6] -> [test.cpp:8]: (error, inconclusive) Returning object that points to local variable 'i' that will be invalid when returning.\n",
errout_str());
Expand All @@ -3564,8 +3565,7 @@ class TestAutoVariables : public TestFixture {
" int i = 0;\n"
" A a{i};\n"
" return a;\n"
"}\n",
true);
"}\n");
ASSERT_EQUALS("", errout_str());

check("struct A {\n"
Expand Down Expand Up @@ -3718,8 +3718,7 @@ class TestAutoVariables : public TestFixture {
"S f() {\n"
" std::string m(\"abc\");\n"
" return S(m);\n"
"}\n",
true);
"}\n");
ASSERT_EQUALS("", errout_str());

check("struct S {\n"
Expand All @@ -3729,8 +3728,7 @@ class TestAutoVariables : public TestFixture {
"S f() {\n"
" std::string s(\"abc\");\n"
" return S(s.c_str());\n"
"}\n",
true);
"}\n");
ASSERT_EQUALS("", errout_str());

check("struct S {\n"
Expand All @@ -3742,8 +3740,7 @@ class TestAutoVariables : public TestFixture {
"void f(const std::stringstream& buffer) {\n"
" S s(buffer.str().c_str());\n"
" s.g();\n"
"}\n",
true);
"}\n");
ASSERT_EQUALS("", errout_str());
}

Expand Down Expand Up @@ -4101,16 +4098,14 @@ class TestAutoVariables : public TestFixture {
"void T::f() {\n"
" U u(p->g().c_str());\n"
" if (u.h()) {}\n"
"}\n",
true);
"}\n");
ASSERT_EQUALS("", errout_str());

// #11442
check("const std::string& f(const P< std::string >& value) {\n"
" static const std::string empty;\n"
" return value.get() == nullptr ? empty : *value;\n"
"}\n",
true);
"}\n");
ASSERT_EQUALS("", errout_str());

// #11472
Expand Down
14 changes: 10 additions & 4 deletions test/testbool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,18 @@ class TestBool : public TestFixture {
TEST_CASE(returnNonBoolClass);
}

struct CheckOptions
{
CheckOptions() = default;
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], bool cpp = true) {
void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) {
// Tokenize..
SimpleTokenizer tokenizer(settings, *this);
ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line);
ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line);

// Check...
runChecks<CheckBool>(tokenizer, this);
Expand Down Expand Up @@ -146,7 +152,7 @@ class TestBool : public TestFixture {
" const int *rmat = n < 4 ? " /* OK */
" ctx->q_intra_matrix :"
" ctx->q_chroma_intra_matrix;\n"
"}", false);
"}", dinit(CheckOptions, $.cpp = false));
ASSERT_EQUALS("[test.c:3]: (error) Boolean value assigned to pointer.\n", errout_str());

// ticket #6588 (c++ mode)
Expand All @@ -165,7 +171,7 @@ class TestBool : public TestFixture {
" char* m1 = compare(a, b) < 0\n"
" ? (compare(b, c) < 0 ? b : (compare(a, c) < 0 ? c : a))\n"
" : (compare(a, c) < 0 ? a : (compare(b, c) < 0 ? c : b));\n"
"}", false);
"}", dinit(CheckOptions, $.cpp = false));
ASSERT_EQUALS("", errout_str());

// #7381
Expand Down
Loading
Loading