Skip to content

Commit 960e8bb

Browse files
authored
testrunner: got rid of some redundant preprocessing code / added some missing asserts (#5521)
This consolidates the stray invocations of preprocessing.
1 parent a22c181 commit 960e8bb

19 files changed

+145
-225
lines changed

Makefile

Lines changed: 14 additions & 14 deletions
Large diffs are not rendered by default.

test/helpers.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,34 @@ std::string PreprocessorHelper::getcode(Preprocessor &preprocessor, const std::s
137137

138138
return ret;
139139
}
140+
141+
void PreprocessorHelper::preprocess(const char code[], std::vector<std::string> &files, Tokenizer& tokenizer)
142+
{
143+
// Raw Tokens..
144+
std::istringstream istr(code);
145+
const simplecpp::TokenList tokens1(istr, files, files[0]);
146+
147+
// Preprocess..
148+
simplecpp::TokenList tokens2(files);
149+
std::map<std::string, simplecpp::TokenList*> filedata;
150+
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
151+
152+
// Tokenizer..
153+
tokenizer.createTokens(std::move(tokens2));
154+
}
155+
156+
void PreprocessorHelper::preprocess(Preprocessor &preprocessor, const char code[], std::vector<std::string> &files, Tokenizer& tokenizer)
157+
{
158+
std::istringstream istr(code);
159+
const simplecpp::TokenList tokens1(istr, files, files[0]);
160+
161+
// Preprocess..
162+
simplecpp::TokenList tokens2(files);
163+
std::map<std::string, simplecpp::TokenList*> filedata;
164+
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
165+
166+
// Tokenizer..
167+
tokenizer.createTokens(std::move(tokens2));
168+
169+
preprocessor.setDirectives(tokens1);
170+
}

test/helpers.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class PreprocessorHelper
9090
* @param inlineSuppression the inline suppressions
9191
*/
9292
static std::string getcode(Preprocessor &preprocessor, const std::string &filedata, const std::string &cfg, const std::string &filename, Suppressions *inlineSuppression = nullptr);
93+
94+
static void preprocess(const char code[], std::vector<std::string> &files, Tokenizer& tokenizer);
95+
static void preprocess(Preprocessor &preprocessor, const char code[], std::vector<std::string> &files, Tokenizer& tokenizer);
9396
};
9497

9598
namespace cppcheck {

test/testastutils.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,13 @@ class TestAstUtils : public TestFixture {
173173
ASSERT_EQUALS(true, isReturnScope("void positiveTokenOffset() { return; }", 7));
174174
}
175175

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

test/testbufferoverrun.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "checkbufferoverrun.h"
2222
#include "ctu.h"
2323
#include "errortypes.h"
24+
#include "helpers.h"
2425
#include "standards.h"
2526
#include "platform.h"
2627
#include "settings.h"
@@ -71,28 +72,21 @@ class TestBufferOverrun : public TestFixture {
7172
runChecks<CheckBufferOverrun>(tokenizer, this);
7273
}
7374

74-
void checkP(const char code[], const char* filename = "test.cpp")
75+
#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
76+
void checkP_(const char* file, int line, const char code[], const char* filename = "test.cpp")
7577
{
7678
// Clear the error buffer..
7779
errout.str("");
7880

7981
const Settings settings = settingsBuilder(settings0).severity(Severity::performance)
8082
.c(Standards::CLatest).cpp(Standards::CPPLatest).certainty(Certainty::inconclusive).build();
8183

82-
// Raw tokens..
8384
std::vector<std::string> files(1, filename);
84-
std::istringstream istr(code);
85-
const simplecpp::TokenList tokens1(istr, files, files[0]);
86-
87-
// Preprocess..
88-
simplecpp::TokenList tokens2(files);
89-
std::map<std::string, simplecpp::TokenList*> filedata;
90-
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
85+
Tokenizer tokenizer(&settings, this);
86+
PreprocessorHelper::preprocess(code, files, tokenizer);
9187

9288
// Tokenizer..
93-
Tokenizer tokenizer(&settings, this);
94-
tokenizer.createTokens(std::move(tokens2));
95-
tokenizer.simplifyTokens1("");
89+
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);
9690

9791
// Check for buffer overruns..
9892
runChecks<CheckBufferOverrun>(tokenizer, this);

test/testclass.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "check.h"
2020
#include "checkclass.h"
2121
#include "errortypes.h"
22+
#include "helpers.h"
2223
#include "preprocessor.h"
2324
#include "settings.h"
2425
#include "fixture.h"
@@ -8409,26 +8410,17 @@ class TestClass : public TestFixture {
84098410
ASSERT_EQUALS("", errout.str());
84108411
}
84118412

8412-
#define checkUselessOverride(code) checkUselessOverride_(code, __FILE__, __LINE__)
8413-
void checkUselessOverride_(const char code[], const char* file, int line) {
8413+
#define checkUselessOverride(...) checkUselessOverride_(__FILE__, __LINE__, __VA_ARGS__)
8414+
void checkUselessOverride_(const char* file, int line, const char code[]) {
84148415
// Clear the error log
84158416
errout.str("");
84168417

84178418
const Settings settings = settingsBuilder().severity(Severity::style).build();
84188419

8419-
// Raw tokens..
84208420
std::vector<std::string> files(1, "test.cpp");
8421-
std::istringstream istr(code);
8422-
const simplecpp::TokenList tokens1(istr, files, files[0]);
8423-
8424-
// Preprocess..
8425-
simplecpp::TokenList tokens2(files);
8426-
std::map<std::string, simplecpp::TokenList*> filedata;
8427-
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
8428-
8429-
// Tokenize..
84308421
Tokenizer tokenizer(&settings, this);
8431-
tokenizer.createTokens(std::move(tokens2));
8422+
PreprocessorHelper::preprocess(code, files, tokenizer);
8423+
84328424
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);
84338425

84348426
// Check..

test/testcondition.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "checkcondition.h"
2020
#include "errortypes.h"
21+
#include "helpers.h"
2122
#include "library.h"
2223
#include "platform.h"
2324
#include "preprocessor.h"
@@ -127,35 +128,26 @@ class TestCondition : public TestFixture {
127128
TEST_CASE(knownConditionIncrementLoop); // #9808
128129
}
129130

130-
void check(const char code[], const Settings &settings, const char* filename = "test.cpp") {
131+
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
132+
void check_(const char* file, int line, const char code[], const Settings &settings, const char* filename = "test.cpp") {
131133
// Clear the error buffer..
132134
errout.str("");
133135

134-
// Raw tokens..
135-
std::vector<std::string> files(1, filename);
136-
std::istringstream istr(code);
137-
const simplecpp::TokenList tokens1(istr, files, files[0]);
138-
139-
// Preprocess..
140-
simplecpp::TokenList tokens2(files);
141-
std::map<std::string, simplecpp::TokenList*> filedata;
142-
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
143-
144136
Preprocessor preprocessor(settings);
145-
preprocessor.setDirectives(tokens1);
137+
std::vector<std::string> files(1, filename);
138+
Tokenizer tokenizer(&settings, this, &preprocessor);
139+
PreprocessorHelper::preprocess(preprocessor, code, files, tokenizer);
146140

147141
// Tokenizer..
148-
Tokenizer tokenizer(&settings, this, &preprocessor);
149-
tokenizer.createTokens(std::move(tokens2));
150-
tokenizer.simplifyTokens1("");
142+
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);
151143

152144
// Run checks..
153145
runChecks<CheckCondition>(tokenizer, this);
154146
}
155147

156-
void check(const char code[], const char* filename = "test.cpp", bool inconclusive = false) {
148+
void check_(const char* file, int line, const char code[], const char* filename = "test.cpp", bool inconclusive = false) {
157149
const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive, inconclusive).build();
158-
check(code, settings, filename);
150+
check_(file, line, code, settings, filename);
159151
}
160152

161153
void assignAndCompare() {

test/testincompletestatement.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "checkother.h"
2020
#include "errortypes.h"
21+
#include "helpers.h"
2122
#include "settings.h"
2223
#include "fixture.h"
2324
#include "tokenize.h"
@@ -37,26 +38,19 @@ class TestIncompleteStatement : public TestFixture {
3738
private:
3839
const Settings settings = settingsBuilder().severity(Severity::warning).build();
3940

40-
void check(const char code[], bool inconclusive = false) {
41+
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
42+
void check_(const char* file, int line, const char code[], bool inconclusive = false) {
4143
// Clear the error buffer..
4244
errout.str("");
4345

4446
const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, inconclusive).build();
4547

46-
// Raw tokens..
4748
std::vector<std::string> files(1, "test.cpp");
48-
std::istringstream istr(code);
49-
const simplecpp::TokenList tokens1(istr, files, files[0]);
50-
51-
// Preprocess..
52-
simplecpp::TokenList tokens2(files);
53-
std::map<std::string, simplecpp::TokenList*> filedata;
54-
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
49+
Tokenizer tokenizer(&settings1, this);
50+
PreprocessorHelper::preprocess(code, files, tokenizer);
5551

5652
// Tokenize..
57-
Tokenizer tokenizer(&settings1, this);
58-
tokenizer.createTokens(std::move(tokens2));
59-
tokenizer.simplifyTokens1("");
53+
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);
6054

6155
// Check for incomplete statements..
6256
CheckOther checkOther(&tokenizer, &settings1, this);

test/testleakautovar.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "checkleakautovar.h"
2121
#include "errortypes.h"
22+
#include "helpers.h"
2223
#include "library.h"
2324
#include "settings.h"
2425
#include "fixture.h"
@@ -2837,24 +2838,17 @@ class TestLeakAutoVarRecursiveCountLimit : public TestFixture {
28372838
private:
28382839
const Settings settings = settingsBuilder().library("std.cfg").checkLibrary().build();
28392840

2840-
void checkP(const char code[], bool cpp = false) {
2841+
#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
2842+
void checkP_(const char* file, int line, const char code[], bool cpp = false) {
28412843
// Clear the error buffer..
28422844
errout.str("");
28432845

2844-
// Raw tokens..
28452846
std::vector<std::string> files(1, cpp?"test.cpp":"test.c");
2846-
std::istringstream istr(code);
2847-
const simplecpp::TokenList tokens1(istr, files, files[0]);
2848-
2849-
// Preprocess..
2850-
simplecpp::TokenList tokens2(files);
2851-
std::map<std::string, simplecpp::TokenList*> filedata;
2852-
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
2847+
Tokenizer tokenizer(&settings, this);
2848+
PreprocessorHelper::preprocess(code, files, tokenizer);
28532849

28542850
// Tokenizer..
2855-
Tokenizer tokenizer(&settings, this);
2856-
tokenizer.createTokens(std::move(tokens2));
2857-
tokenizer.simplifyTokens1("");
2851+
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);
28582852

28592853
// Check for leaks..
28602854
runChecks<CheckLeakAutoVar>(tokenizer, this);

test/testnullpointer.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "checknullpointer.h"
2121
#include "ctu.h"
2222
#include "errortypes.h"
23+
#include "helpers.h"
2324
#include "library.h"
2425
#include "settings.h"
2526
#include "fixture.h"
@@ -192,26 +193,19 @@ class TestNullPointer : public TestFixture {
192193
runChecks<CheckNullPointer>(tokenizer, this);
193194
}
194195

195-
void checkP(const char code[]) {
196+
#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
197+
void checkP_(const char* file, int line, const char code[]) {
196198
// Clear the error buffer..
197199
errout.str("");
198200

199201
const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, false).build();
200202

201-
// Raw tokens..
202203
std::vector<std::string> files(1, "test.cpp");
203-
std::istringstream istr(code);
204-
const simplecpp::TokenList tokens1(istr, files, files[0]);
205-
206-
// Preprocess..
207-
simplecpp::TokenList tokens2(files);
208-
std::map<std::string, simplecpp::TokenList*> filedata;
209-
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
204+
Tokenizer tokenizer(&settings1, this);
205+
PreprocessorHelper::preprocess(code, files, tokenizer);
210206

211207
// Tokenizer..
212-
Tokenizer tokenizer(&settings1, this);
213-
tokenizer.createTokens(std::move(tokens2));
214-
tokenizer.simplifyTokens1("");
208+
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);
215209

216210
// Check for null pointer dereferences..
217211
runChecks<CheckNullPointer>(tokenizer, this);

test/testother.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "checkother.h"
2020
#include "errortypes.h"
21+
#include "helpers.h"
2122
#include "library.h"
2223
#include "platform.h"
2324
#include "preprocessor.h"
@@ -330,7 +331,8 @@ class TestOther : public TestFixture {
330331
check_(file, line, code, "test.cpp", true, true, false, s);
331332
}
332333

333-
void checkP(const char code[], const char *filename = "test.cpp") {
334+
#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
335+
void checkP_(const char* file, int line, const char code[], const char *filename = "test.cpp") {
334336
// Clear the error buffer..
335337
errout.str("");
336338

@@ -343,23 +345,13 @@ class TestOther : public TestFixture {
343345
settings->standards.cpp = Standards::CPPLatest;
344346
settings->certainty.enable(Certainty::inconclusive);
345347

346-
// Raw tokens..
347-
std::vector<std::string> files(1, filename);
348-
std::istringstream istr(code);
349-
const simplecpp::TokenList tokens1(istr, files, files[0]);
350-
351-
// Preprocess..
352-
simplecpp::TokenList tokens2(files);
353-
std::map<std::string, simplecpp::TokenList*> filedata;
354-
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
355-
356348
Preprocessor preprocessor(*settings);
357-
preprocessor.setDirectives(tokens1);
349+
std::vector<std::string> files(1, filename);
350+
Tokenizer tokenizer(settings, this, &preprocessor);
351+
PreprocessorHelper::preprocess(preprocessor, code, files, tokenizer);
358352

359353
// Tokenizer..
360-
Tokenizer tokenizer(settings, this, &preprocessor);
361-
tokenizer.createTokens(std::move(tokens2));
362-
tokenizer.simplifyTokens1("");
354+
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);
363355

364356
// Check..
365357
runChecks<CheckOther>(tokenizer, this);

test/testsimplifytypedef.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919

2020
#include "errortypes.h"
21+
#include "helpers.h"
2122
#include "platform.h"
2223
#include "settings.h"
2324
#include "fixture.h"
@@ -263,19 +264,11 @@ class TestSimplifyTypedef : public TestFixture {
263264
// Clear the error buffer..
264265
errout.str("");
265266

266-
// Raw tokens..
267267
std::vector<std::string> files(1, "test.cpp");
268-
std::istringstream istr(code);
269-
const simplecpp::TokenList tokens1(istr, files, files[0]);
270-
271-
// Preprocess..
272-
simplecpp::TokenList tokens2(files);
273-
std::map<std::string, simplecpp::TokenList*> filedata;
274-
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
268+
Tokenizer tokenizer(&settings0, this);
269+
PreprocessorHelper::preprocess(code, files, tokenizer);
275270

276271
// Tokenize..
277-
Tokenizer tokenizer(&settings0, this);
278-
tokenizer.createTokens(std::move(tokens2));
279272
tokenizer.createLinks();
280273
tokenizer.simplifyTypedef();
281274

0 commit comments

Comments
 (0)