Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1417eba
ASSERT() on calls to Tokenizer::tokenize() in test code
chrchr-github Oct 11, 2021
fecec17
Fix return type
chrchr-github Oct 11, 2021
2d3dfeb
Fix return type
chrchr-github Oct 11, 2021
4a625c0
Merge branch 'main' into chr_AssertTokenize
chrchr-github Oct 11, 2021
dbedbd4
Missing assert
chrchr-github Oct 11, 2021
233dbeb
Merge branch 'danmar:main' into chr_AssertTokenize
chrchr-github Oct 11, 2021
2f90454
Merge
chrchr-github Oct 26, 2021
7c913aa
Merge branch 'chr_AssertTokenize' of https://github.com/chrchr-github…
chrchr-github Oct 26, 2021
e6c47bd
Add ASSERT_LOC() to capture file/line info
chrchr-github Oct 26, 2021
b28dbb5
Fix test case
Oct 26, 2021
f8174f4
Add args to ASSERT_LOC(), no return
chrchr-github Oct 27, 2021
9ea857b
Add macros to capture file/line
chrchr-github Oct 28, 2021
fa69d3c
Fix test case
chrchr-github Oct 28, 2021
a7e8184
More ASSERT_LOC
chrchr-github Oct 29, 2021
1a66371
Unused parameters
chrchr-github Oct 29, 2021
1f5fccb
Merge branch 'main' into chr_AssertTokenize
chrchr-github Oct 29, 2021
6d476c2
More ASSERT_LOC
chrchr-github Nov 2, 2021
4996985
Merge branch 'main' into chr_AssertTokenize
chrchr-github Nov 2, 2021
639f45e
ASSERT_LOC
chrchr-github Nov 3, 2021
4d87089
Merge branch 'chr_AssertTokenize' of https://github.com/chrchr-github…
chrchr-github Nov 3, 2021
17b6484
format
chrchr-github Nov 3, 2021
96a0512
format
chrchr-github Nov 3, 2021
dbf0e71
More ASSERT_LOC()
chrchr-github Nov 8, 2021
a8f2d11
More ASSERT_LOC, ASSERT
chrchr-github Nov 9, 2021
5277004
Merge branch 'main' into chr_AssertTokenize
chrchr-github Nov 17, 2021
6d204b0
More ASSERT_LOC
chrchr-github Nov 17, 2021
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
5 changes: 3 additions & 2 deletions test/test64bit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ class Test64BitPortability : public TestFixture {
TEST_CASE(assignment);
}

void check(const char code[]) {
#define check(code) check_(code, __FILE__, __LINE__)
void check_(const char code[], const char* file, int line) {
// Clear the error buffer..
errout.str("");

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

// Check char variable usage..
Check64BitPortability check64BitPortability(&tokenizer, &settings, this);
Expand Down
5 changes: 3 additions & 2 deletions test/testassert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ class TestAssert : public TestFixture {
private:
Settings settings;

void check(const char code[], const char *filename = "test.cpp") {
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
void check_(const char* file, int line, const char code[], const char *filename = "test.cpp") {
// Clear the error buffer..
errout.str("");

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

// Check..
CheckAssert checkAssert;
Expand Down
92 changes: 50 additions & 42 deletions test/testastutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,30 @@ class TestAstUtils : public TestFixture {
private:

void run() OVERRIDE {
TEST_CASE(findLambdaEndToken);
TEST_CASE(findLambdaStartToken);
TEST_CASE(isNullOperand);
TEST_CASE(isReturnScope);
TEST_CASE(isSameExpression);
TEST_CASE(isVariableChanged);
TEST_CASE(isVariableChangedByFunctionCall);
TEST_CASE(nextAfterAstRightmostLeaf);
TEST_CASE(findLambdaEndTokenTest);
TEST_CASE(findLambdaStartTokenTest);
TEST_CASE(isNullOperandTest);
TEST_CASE(isReturnScopeTest);
TEST_CASE(isSameExpressionTest);
TEST_CASE(isVariableChangedTest);
TEST_CASE(isVariableChangedByFunctionCallTest);
TEST_CASE(nextAfterAstRightmostLeafTest);
TEST_CASE(isUsedAsBool);
}

bool findLambdaEndToken(const char code[]) {
#define findLambdaEndToken(code) findLambdaEndToken_(code, __FILE__, __LINE__)
bool findLambdaEndToken_(const char code[], const char* file, int line) {
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token * const tokEnd = ::findLambdaEndToken(tokenizer.tokens());
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
const Token * const tokEnd = (::findLambdaEndToken)(tokenizer.tokens());
return tokEnd && tokEnd->next() == nullptr;
}

void findLambdaEndToken() {
void findLambdaEndTokenTest() {
const Token* nullTok = nullptr;
ASSERT(nullptr == ::findLambdaEndToken(nullTok));
ASSERT(nullptr == (::findLambdaEndToken)(nullTok));
ASSERT_EQUALS(false, findLambdaEndToken("void f() { }"));
ASSERT_EQUALS(true, findLambdaEndToken("[]{ }"));
ASSERT_EQUALS(true, findLambdaEndToken("[]{ return 0; }"));
Expand All @@ -77,17 +78,18 @@ class TestAstUtils : public TestFixture {
ASSERT_EQUALS(true, findLambdaEndToken("[](void) constexpr -> const * const* int { return x; }"));
}

bool findLambdaStartToken(const char code[]) {
#define findLambdaStartToken(code) findLambdaStartToken_(code, __FILE__, __LINE__)
bool findLambdaStartToken_(const char code[], const char* file, int line) {
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token * const tokStart = ::findLambdaStartToken(tokenizer.list.back());
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
const Token * const tokStart = (::findLambdaStartToken)(tokenizer.list.back());
return tokStart && tokStart == tokenizer.list.front();
}

void findLambdaStartToken() {
ASSERT(nullptr == ::findLambdaStartToken(nullptr));
void findLambdaStartTokenTest() {
ASSERT(nullptr == (::findLambdaStartToken)(nullptr));
ASSERT_EQUALS(false, findLambdaStartToken("void f() { }"));
ASSERT_EQUALS(true, findLambdaStartToken("[]{ }"));
ASSERT_EQUALS(true, findLambdaStartToken("[]{ return 0; }"));
Expand All @@ -109,15 +111,16 @@ class TestAstUtils : public TestFixture {
ASSERT_EQUALS(true, findLambdaStartToken("[](void) constexpr -> const * const* int { return x; }"));
}

bool isNullOperand(const char code[]) {
#define isNullOperand(code) isNullOperand_(code, __FILE__, __LINE__)
bool isNullOperand_(const char code[], const char* file, int line) {
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
return ::isNullOperand(tokenizer.tokens());
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
return (::isNullOperand)(tokenizer.tokens());
}

void isNullOperand() {
void isNullOperandTest() {
ASSERT_EQUALS(true, isNullOperand("(void*)0;"));
ASSERT_EQUALS(true, isNullOperand("(void*)0U;"));
ASSERT_EQUALS(true, isNullOperand("(void*)0x0LL;"));
Expand All @@ -130,18 +133,19 @@ class TestAstUtils : public TestFixture {
ASSERT_EQUALS(false, isNullOperand("(void*)1;"));
}

bool isReturnScope(const char code[], int offset) {
#define isReturnScope(code, offset) isReturnScope_(code, offset, __FILE__, __LINE__)
bool isReturnScope_(const char code[], int offset, const char* file, int line) {
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
const Token * const tok = (offset < 0)
? tokenizer.list.back()->tokAt(1+offset)
: tokenizer.tokens()->tokAt(offset);
return ::isReturnScope(tok);
return (::isReturnScope)(tok);
}

void isReturnScope() {
void isReturnScopeTest() {
ASSERT_EQUALS(true, isReturnScope("void f() { if (a) { return; } }", -2));
ASSERT_EQUALS(true, isReturnScope("int f() { if (a) { return {}; } }", -2)); // #8891
ASSERT_EQUALS(true, isReturnScope("std::string f() { if (a) { return std::string{}; } }", -2)); // #8891
Expand All @@ -160,19 +164,20 @@ class TestAstUtils : public TestFixture {
ASSERT_EQUALS(true, isReturnScope("void positiveTokenOffset() { return; }", 7));
}

bool isSameExpression(const char code[], const char tokStr1[], const char tokStr2[]) {
#define isSameExpression(code, tokStr1, tokStr2) isSameExpression_(code, tokStr1, tokStr2, __FILE__, __LINE__)
bool isSameExpression_(const char code[], const char tokStr1[], const char tokStr2[], const char* file, int line) {
Settings settings;
Library library;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
tokenizer.simplifyTokens1("");
const Token * const tok1 = Token::findsimplematch(tokenizer.tokens(), tokStr1, strlen(tokStr1));
const Token * const tok2 = Token::findsimplematch(tok1->next(), tokStr2, strlen(tokStr2));
return ::isSameExpression(false, false, tok1, tok2, library, false, true, nullptr);
return (::isSameExpression)(false, false, tok1, tok2, library, false, true, nullptr);
}

void isSameExpression() {
void isSameExpressionTest() {
ASSERT_EQUALS(true, isSameExpression("x = 1 + 1;", "1", "1"));
ASSERT_EQUALS(false, isSameExpression("x = 1 + 1u;", "1", "1u"));
ASSERT_EQUALS(true, isSameExpression("x = 1.0 + 1.0;", "1.0", "1.0"));
Expand All @@ -199,17 +204,18 @@ class TestAstUtils : public TestFixture {
ASSERT_EQUALS(true, true);
}

bool isVariableChanged(const char code[], const char startPattern[], const char endPattern[]) {
#define isVariableChanged(code, startPattern, endPattern) isVariableChanged_(code, startPattern, endPattern, __FILE__, __LINE__)
bool isVariableChanged_(const char code[], const char startPattern[], const char endPattern[], const char* file, int line) {
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
const Token * const tok1 = Token::findsimplematch(tokenizer.tokens(), startPattern, strlen(startPattern));
const Token * const tok2 = Token::findsimplematch(tokenizer.tokens(), endPattern, strlen(endPattern));
return ::isVariableChanged(tok1,tok2,1,false,&settings,true);
return (::isVariableChanged)(tok1, tok2, 1, false, &settings, true);
}

void isVariableChanged() {
void isVariableChangedTest() {
// #8211 - no lhs for >> , do not crash
isVariableChanged("void f() {\n"
" int b;\n"
Expand All @@ -221,16 +227,17 @@ class TestAstUtils : public TestFixture {
"}\n", "= a", "}"));
}

bool isVariableChangedByFunctionCall(const char code[], const char pattern[], bool *inconclusive) {
#define isVariableChangedByFunctionCall(code, pattern, inconclusive) isVariableChangedByFunctionCall_(code, pattern, inconclusive, __FILE__, __LINE__)
bool isVariableChangedByFunctionCall_(const char code[], const char pattern[], bool *inconclusive, const char* file, int line) {
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
const Token * const argtok = Token::findmatch(tokenizer.tokens(), pattern);
return ::isVariableChangedByFunctionCall(argtok, 0, &settings, inconclusive);
return (::isVariableChangedByFunctionCall)(argtok, 0, &settings, inconclusive);
}

void isVariableChangedByFunctionCall() {
void isVariableChangedByFunctionCallTest() {
const char *code;
bool inconclusive;

Expand All @@ -249,16 +256,17 @@ class TestAstUtils : public TestFixture {
TODO_ASSERT_EQUALS(false, true, inconclusive);
}

bool nextAfterAstRightmostLeaf(const char code[], const char parentPattern[], const char rightPattern[]) {
#define nextAfterAstRightmostLeaf(code, parentPattern, rightPattern) nextAfterAstRightmostLeaf_(code, parentPattern, rightPattern, __FILE__, __LINE__)
bool nextAfterAstRightmostLeaf_(const char code[], const char parentPattern[], const char rightPattern[], const char* file, int line) {
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
const Token * tok = Token::findsimplematch(tokenizer.tokens(), parentPattern, strlen(parentPattern));
return Token::simpleMatch(::nextAfterAstRightmostLeaf(tok), rightPattern, strlen(rightPattern));
return Token::simpleMatch((::nextAfterAstRightmostLeaf)(tok), rightPattern, strlen(rightPattern));
}

void nextAfterAstRightmostLeaf() {
void nextAfterAstRightmostLeafTest() {
ASSERT_EQUALS(true, nextAfterAstRightmostLeaf("void f(int a, int b) { int x = a + b; }", "=", "; }"));
ASSERT_EQUALS(true, nextAfterAstRightmostLeaf("int * g(int); void f(int a, int b) { int x = g(a); }", "=", "; }"));
ASSERT_EQUALS(true, nextAfterAstRightmostLeaf("int * g(int); void f(int a, int b) { int x = g(a)[b]; }", "=", "; }"));
Expand Down
5 changes: 3 additions & 2 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class TestAutoVariables : public TestFixture {
private:
Settings settings;

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

Expand All @@ -39,7 +40,7 @@ class TestAutoVariables : public TestFixture {
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
ASSERT_LOC(tokenizer.tokenize(istr, filename), file, line);

CheckAutoVariables checkAutoVariables;
checkAutoVariables.runChecks(&tokenizer, &settings, this);
Expand Down
5 changes: 3 additions & 2 deletions test/testbool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class TestBool : public TestFixture {
TEST_CASE(returnNonBoolClass);
}

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

Expand All @@ -84,7 +85,7 @@ class TestBool : public TestFixture {
// Tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
ASSERT_LOC(tokenizer.tokenize(istr, filename), file, line);

// Check...
CheckBool checkBool(&tokenizer, &settings, this);
Expand Down
5 changes: 3 additions & 2 deletions test/testboost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ class TestBoost : public TestFixture {
TEST_CASE(BoostForeachContainerModification);
}

void check(const char code[]) {
#define check(code) check_(code, __FILE__, __LINE__)
void check_(const char code[], const char* file, int line) {
// Clear the error buffer..
errout.str("");

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

// Check..
CheckBoost checkBoost;
Expand Down
20 changes: 11 additions & 9 deletions test/testbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class TestBufferOverrun : public TestFixture {
private:
Settings settings0;

void check(const char code[], const char filename[] = "test.cpp") {
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
void check_(const char* file, int line, const char code[], const char filename[] = "test.cpp") {
// Clear the error buffer..
errout.str("");

Expand All @@ -47,17 +48,17 @@ class TestBufferOverrun : public TestFixture {
// Tokenize..
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
ASSERT_LOC(tokenizer.tokenize(istr, filename), file, line);

// Check for buffer overruns..
CheckBufferOverrun checkBufferOverrun;
checkBufferOverrun.runChecks(&tokenizer, &settings0, this);
}

void check(const char code[], const Settings &settings, const char filename[] = "test.cpp") {
void check_(const char* file, int line, const char code[], const Settings &settings, const char filename[] = "test.cpp") {
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
ASSERT_LOC(tokenizer.tokenize(istr, filename), file, line);

// Clear the error buffer..
errout.str("");
Expand Down Expand Up @@ -4461,22 +4462,23 @@ class TestBufferOverrun : public TestFixture {
ASSERT_EQUALS("[test.cpp:3]: (portability) Undefined behaviour, pointer arithmetic 'arr+20' is out of bounds.\n", errout.str());
}

void ctu(const char code[]) {
#define ctu(code) ctu_(code, __FILE__, __LINE__)
void ctu_(const char code[], const char* file, int line) {
// Clear the error buffer..
errout.str("");

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

CTU::FileInfo *ctu = CTU::getFileInfo(&tokenizer);

// Check code..
std::list<Check::FileInfo*> fileInfo;
CheckBufferOverrun check(&tokenizer, &settings0, this);
fileInfo.push_back(check.getFileInfo(&tokenizer, &settings0));
check.analyseWholeProgram(ctu, fileInfo, settings0, *this);
CheckBufferOverrun checkBO(&tokenizer, &settings0, this);
fileInfo.push_back(checkBO.getFileInfo(&tokenizer, &settings0));
checkBO.analyseWholeProgram(ctu, fileInfo, settings0, *this);
while (!fileInfo.empty()) {
delete fileInfo.back();
fileInfo.pop_back();
Expand Down
5 changes: 3 additions & 2 deletions test/testcharvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ class TestCharVar : public TestFixture {
TEST_CASE(bitop);
}

void check(const char code[]) {
#define check(code) check_(code, __FILE__, __LINE__)
void check_(const char code[], const char* file, int line) {
// Clear the error buffer..
errout.str("");

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

// Check char variable usage..
CheckOther checkOther(&tokenizer, &settings, this);
Expand Down
Loading