Skip to content

Commit ca311eb

Browse files
ASSERT() on calls to Tokenizer::tokenize() in test code (danmar#3501)
1 parent 853a1f6 commit ca311eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+485
-384
lines changed

test/test64bit.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,16 @@ class Test64BitPortability : public TestFixture {
4242
TEST_CASE(assignment);
4343
}
4444

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

4950
// Tokenize..
5051
Tokenizer tokenizer(&settings, this);
5152
LOAD_LIB_2(settings.library, "std.cfg");
5253
std::istringstream istr(code);
53-
tokenizer.tokenize(istr, "test.cpp");
54+
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
5455

5556
// Check char variable usage..
5657
Check64BitPortability check64BitPortability(&tokenizer, &settings, this);

test/testassert.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ class TestAssert : public TestFixture {
3030
private:
3131
Settings settings;
3232

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

3738
// Tokenize..
3839
Tokenizer tokenizer(&settings, this);
3940
std::istringstream istr(code);
40-
tokenizer.tokenize(istr, filename);
41+
ASSERT_LOC(tokenizer.tokenize(istr, filename), file, line);
4142

4243
// Check..
4344
CheckAssert checkAssert;

test/testastutils.cpp

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,30 @@ class TestAstUtils : public TestFixture {
3333
private:
3434

3535
void run() OVERRIDE {
36-
TEST_CASE(findLambdaEndToken);
37-
TEST_CASE(findLambdaStartToken);
38-
TEST_CASE(isNullOperand);
39-
TEST_CASE(isReturnScope);
40-
TEST_CASE(isSameExpression);
41-
TEST_CASE(isVariableChanged);
42-
TEST_CASE(isVariableChangedByFunctionCall);
43-
TEST_CASE(nextAfterAstRightmostLeaf);
36+
TEST_CASE(findLambdaEndTokenTest);
37+
TEST_CASE(findLambdaStartTokenTest);
38+
TEST_CASE(isNullOperandTest);
39+
TEST_CASE(isReturnScopeTest);
40+
TEST_CASE(isSameExpressionTest);
41+
TEST_CASE(isVariableChangedTest);
42+
TEST_CASE(isVariableChangedByFunctionCallTest);
43+
TEST_CASE(nextAfterAstRightmostLeafTest);
4444
TEST_CASE(isUsedAsBool);
4545
}
4646

47-
bool findLambdaEndToken(const char code[]) {
47+
#define findLambdaEndToken(code) findLambdaEndToken_(code, __FILE__, __LINE__)
48+
bool findLambdaEndToken_(const char code[], const char* file, int line) {
4849
Settings settings;
4950
Tokenizer tokenizer(&settings, this);
5051
std::istringstream istr(code);
51-
tokenizer.tokenize(istr, "test.cpp");
52-
const Token * const tokEnd = ::findLambdaEndToken(tokenizer.tokens());
52+
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
53+
const Token * const tokEnd = (::findLambdaEndToken)(tokenizer.tokens());
5354
return tokEnd && tokEnd->next() == nullptr;
5455
}
5556

56-
void findLambdaEndToken() {
57+
void findLambdaEndTokenTest() {
5758
const Token* nullTok = nullptr;
58-
ASSERT(nullptr == ::findLambdaEndToken(nullTok));
59+
ASSERT(nullptr == (::findLambdaEndToken)(nullTok));
5960
ASSERT_EQUALS(false, findLambdaEndToken("void f() { }"));
6061
ASSERT_EQUALS(true, findLambdaEndToken("[]{ }"));
6162
ASSERT_EQUALS(true, findLambdaEndToken("[]{ return 0; }"));
@@ -77,17 +78,18 @@ class TestAstUtils : public TestFixture {
7778
ASSERT_EQUALS(true, findLambdaEndToken("[](void) constexpr -> const * const* int { return x; }"));
7879
}
7980

80-
bool findLambdaStartToken(const char code[]) {
81+
#define findLambdaStartToken(code) findLambdaStartToken_(code, __FILE__, __LINE__)
82+
bool findLambdaStartToken_(const char code[], const char* file, int line) {
8183
Settings settings;
8284
Tokenizer tokenizer(&settings, this);
8385
std::istringstream istr(code);
84-
tokenizer.tokenize(istr, "test.cpp");
85-
const Token * const tokStart = ::findLambdaStartToken(tokenizer.list.back());
86+
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
87+
const Token * const tokStart = (::findLambdaStartToken)(tokenizer.list.back());
8688
return tokStart && tokStart == tokenizer.list.front();
8789
}
8890

89-
void findLambdaStartToken() {
90-
ASSERT(nullptr == ::findLambdaStartToken(nullptr));
91+
void findLambdaStartTokenTest() {
92+
ASSERT(nullptr == (::findLambdaStartToken)(nullptr));
9193
ASSERT_EQUALS(false, findLambdaStartToken("void f() { }"));
9294
ASSERT_EQUALS(true, findLambdaStartToken("[]{ }"));
9395
ASSERT_EQUALS(true, findLambdaStartToken("[]{ return 0; }"));
@@ -109,15 +111,16 @@ class TestAstUtils : public TestFixture {
109111
ASSERT_EQUALS(true, findLambdaStartToken("[](void) constexpr -> const * const* int { return x; }"));
110112
}
111113

112-
bool isNullOperand(const char code[]) {
114+
#define isNullOperand(code) isNullOperand_(code, __FILE__, __LINE__)
115+
bool isNullOperand_(const char code[], const char* file, int line) {
113116
Settings settings;
114117
Tokenizer tokenizer(&settings, this);
115118
std::istringstream istr(code);
116-
tokenizer.tokenize(istr, "test.cpp");
117-
return ::isNullOperand(tokenizer.tokens());
119+
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
120+
return (::isNullOperand)(tokenizer.tokens());
118121
}
119122

120-
void isNullOperand() {
123+
void isNullOperandTest() {
121124
ASSERT_EQUALS(true, isNullOperand("(void*)0;"));
122125
ASSERT_EQUALS(true, isNullOperand("(void*)0U;"));
123126
ASSERT_EQUALS(true, isNullOperand("(void*)0x0LL;"));
@@ -130,18 +133,19 @@ class TestAstUtils : public TestFixture {
130133
ASSERT_EQUALS(false, isNullOperand("(void*)1;"));
131134
}
132135

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

144-
void isReturnScope() {
148+
void isReturnScopeTest() {
145149
ASSERT_EQUALS(true, isReturnScope("void f() { if (a) { return; } }", -2));
146150
ASSERT_EQUALS(true, isReturnScope("int f() { if (a) { return {}; } }", -2)); // #8891
147151
ASSERT_EQUALS(true, isReturnScope("std::string f() { if (a) { return std::string{}; } }", -2)); // #8891
@@ -160,19 +164,20 @@ class TestAstUtils : public TestFixture {
160164
ASSERT_EQUALS(true, isReturnScope("void positiveTokenOffset() { return; }", 7));
161165
}
162166

163-
bool isSameExpression(const char code[], const char tokStr1[], const char tokStr2[]) {
167+
#define isSameExpression(code, tokStr1, tokStr2) isSameExpression_(code, tokStr1, tokStr2, __FILE__, __LINE__)
168+
bool isSameExpression_(const char code[], const char tokStr1[], const char tokStr2[], const char* file, int line) {
164169
Settings settings;
165170
Library library;
166171
Tokenizer tokenizer(&settings, this);
167172
std::istringstream istr(code);
168-
tokenizer.tokenize(istr, "test.cpp");
173+
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
169174
tokenizer.simplifyTokens1("");
170175
const Token * const tok1 = Token::findsimplematch(tokenizer.tokens(), tokStr1, strlen(tokStr1));
171176
const Token * const tok2 = Token::findsimplematch(tok1->next(), tokStr2, strlen(tokStr2));
172-
return ::isSameExpression(false, false, tok1, tok2, library, false, true, nullptr);
177+
return (::isSameExpression)(false, false, tok1, tok2, library, false, true, nullptr);
173178
}
174179

175-
void isSameExpression() {
180+
void isSameExpressionTest() {
176181
ASSERT_EQUALS(true, isSameExpression("x = 1 + 1;", "1", "1"));
177182
ASSERT_EQUALS(false, isSameExpression("x = 1 + 1u;", "1", "1u"));
178183
ASSERT_EQUALS(true, isSameExpression("x = 1.0 + 1.0;", "1.0", "1.0"));
@@ -199,17 +204,18 @@ class TestAstUtils : public TestFixture {
199204
ASSERT_EQUALS(true, true);
200205
}
201206

202-
bool isVariableChanged(const char code[], const char startPattern[], const char endPattern[]) {
207+
#define isVariableChanged(code, startPattern, endPattern) isVariableChanged_(code, startPattern, endPattern, __FILE__, __LINE__)
208+
bool isVariableChanged_(const char code[], const char startPattern[], const char endPattern[], const char* file, int line) {
203209
Settings settings;
204210
Tokenizer tokenizer(&settings, this);
205211
std::istringstream istr(code);
206-
tokenizer.tokenize(istr, "test.cpp");
212+
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
207213
const Token * const tok1 = Token::findsimplematch(tokenizer.tokens(), startPattern, strlen(startPattern));
208214
const Token * const tok2 = Token::findsimplematch(tokenizer.tokens(), endPattern, strlen(endPattern));
209-
return ::isVariableChanged(tok1,tok2,1,false,&settings,true);
215+
return (::isVariableChanged)(tok1, tok2, 1, false, &settings, true);
210216
}
211217

212-
void isVariableChanged() {
218+
void isVariableChangedTest() {
213219
// #8211 - no lhs for >> , do not crash
214220
isVariableChanged("void f() {\n"
215221
" int b;\n"
@@ -221,16 +227,17 @@ class TestAstUtils : public TestFixture {
221227
"}\n", "= a", "}"));
222228
}
223229

224-
bool isVariableChangedByFunctionCall(const char code[], const char pattern[], bool *inconclusive) {
230+
#define isVariableChangedByFunctionCall(code, pattern, inconclusive) isVariableChangedByFunctionCall_(code, pattern, inconclusive, __FILE__, __LINE__)
231+
bool isVariableChangedByFunctionCall_(const char code[], const char pattern[], bool *inconclusive, const char* file, int line) {
225232
Settings settings;
226233
Tokenizer tokenizer(&settings, this);
227234
std::istringstream istr(code);
228-
tokenizer.tokenize(istr, "test.cpp");
235+
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
229236
const Token * const argtok = Token::findmatch(tokenizer.tokens(), pattern);
230-
return ::isVariableChangedByFunctionCall(argtok, 0, &settings, inconclusive);
237+
return (::isVariableChangedByFunctionCall)(argtok, 0, &settings, inconclusive);
231238
}
232239

233-
void isVariableChangedByFunctionCall() {
240+
void isVariableChangedByFunctionCallTest() {
234241
const char *code;
235242
bool inconclusive;
236243

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

252-
bool nextAfterAstRightmostLeaf(const char code[], const char parentPattern[], const char rightPattern[]) {
259+
#define nextAfterAstRightmostLeaf(code, parentPattern, rightPattern) nextAfterAstRightmostLeaf_(code, parentPattern, rightPattern, __FILE__, __LINE__)
260+
bool nextAfterAstRightmostLeaf_(const char code[], const char parentPattern[], const char rightPattern[], const char* file, int line) {
253261
Settings settings;
254262
Tokenizer tokenizer(&settings, this);
255263
std::istringstream istr(code);
256-
tokenizer.tokenize(istr, "test.cpp");
264+
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
257265
const Token * tok = Token::findsimplematch(tokenizer.tokens(), parentPattern, strlen(parentPattern));
258-
return Token::simpleMatch(::nextAfterAstRightmostLeaf(tok), rightPattern, strlen(rightPattern));
266+
return Token::simpleMatch((::nextAfterAstRightmostLeaf)(tok), rightPattern, strlen(rightPattern));
259267
}
260268

261-
void nextAfterAstRightmostLeaf() {
269+
void nextAfterAstRightmostLeafTest() {
262270
ASSERT_EQUALS(true, nextAfterAstRightmostLeaf("void f(int a, int b) { int x = a + b; }", "=", "; }"));
263271
ASSERT_EQUALS(true, nextAfterAstRightmostLeaf("int * g(int); void f(int a, int b) { int x = g(a); }", "=", "; }"));
264272
ASSERT_EQUALS(true, nextAfterAstRightmostLeaf("int * g(int); void f(int a, int b) { int x = g(a)[b]; }", "=", "; }"));

test/testautovariables.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class TestAutoVariables : public TestFixture {
3030
private:
3131
Settings settings;
3232

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

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

4445
CheckAutoVariables checkAutoVariables;
4546
checkAutoVariables.runChecks(&tokenizer, &settings, this);

test/testbool.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class TestBool : public TestFixture {
7575
TEST_CASE(returnNonBoolClass);
7676
}
7777

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

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

8990
// Check...
9091
CheckBool checkBool(&tokenizer, &settings, this);

test/testboost.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ class TestBoost : public TestFixture {
3737
TEST_CASE(BoostForeachContainerModification);
3838
}
3939

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

4445
// Tokenize..
4546
Tokenizer tokenizer(&settings, this);
4647
std::istringstream istr(code);
47-
tokenizer.tokenize(istr, "test.cpp");
48+
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
4849

4950
// Check..
5051
CheckBoost checkBoost;

test/testbufferoverrun.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class TestBufferOverrun : public TestFixture {
3939
private:
4040
Settings settings0;
4141

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

@@ -48,17 +49,17 @@ class TestBufferOverrun : public TestFixture {
4849
// Tokenize..
4950
Tokenizer tokenizer(&settings0, this);
5051
std::istringstream istr(code);
51-
tokenizer.tokenize(istr, filename);
52+
ASSERT_LOC(tokenizer.tokenize(istr, filename), file, line);
5253

5354
// Check for buffer overruns..
5455
CheckBufferOverrun checkBufferOverrun;
5556
checkBufferOverrun.runChecks(&tokenizer, &settings0, this);
5657
}
5758

58-
void check(const char code[], const Settings &settings, const char filename[] = "test.cpp") {
59+
void check_(const char* file, int line, const char code[], const Settings &settings, const char filename[] = "test.cpp") {
5960
Tokenizer tokenizer(&settings, this);
6061
std::istringstream istr(code);
61-
tokenizer.tokenize(istr, filename);
62+
ASSERT_LOC(tokenizer.tokenize(istr, filename), file, line);
6263

6364
// Clear the error buffer..
6465
errout.str("");
@@ -4539,22 +4540,23 @@ class TestBufferOverrun : public TestFixture {
45394540
ASSERT_EQUALS("[test.cpp:3]: (portability) Undefined behaviour, pointer arithmetic 'arr+20' is out of bounds.\n", errout.str());
45404541
}
45414542

4542-
void ctu(const char code[]) {
4543+
#define ctu(code) ctu_(code, __FILE__, __LINE__)
4544+
void ctu_(const char code[], const char* file, int line) {
45434545
// Clear the error buffer..
45444546
errout.str("");
45454547

45464548
// Tokenize..
45474549
Tokenizer tokenizer(&settings0, this);
45484550
std::istringstream istr(code);
4549-
tokenizer.tokenize(istr, "test.cpp");
4551+
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
45504552

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

45534555
// Check code..
45544556
std::list<Check::FileInfo*> fileInfo;
4555-
CheckBufferOverrun check(&tokenizer, &settings0, this);
4556-
fileInfo.push_back(check.getFileInfo(&tokenizer, &settings0));
4557-
check.analyseWholeProgram(ctu, fileInfo, settings0, *this);
4557+
CheckBufferOverrun checkBO(&tokenizer, &settings0, this);
4558+
fileInfo.push_back(checkBO.getFileInfo(&tokenizer, &settings0));
4559+
checkBO.analyseWholeProgram(ctu, fileInfo, settings0, *this);
45584560
while (!fileInfo.empty()) {
45594561
delete fileInfo.back();
45604562
fileInfo.pop_back();

test/testcharvar.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ class TestCharVar : public TestFixture {
4141
TEST_CASE(bitop);
4242
}
4343

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

4849
// Tokenize..
4950
Tokenizer tokenizer(&settings, this);
5051
std::istringstream istr(code);
51-
tokenizer.tokenize(istr, "test.cpp");
52+
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
5253

5354
// Check char variable usage..
5455
CheckOther checkOther(&tokenizer, &settings, this);

0 commit comments

Comments
 (0)