Skip to content

Commit 5da25c1

Browse files
authored
always provide settings for TokenList in tests (cppcheck-opensource#6055)
1 parent 4113ea5 commit 5da25c1

5 files changed

Lines changed: 30 additions & 29 deletions

File tree

lib/tokenlist.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ struct TokensFrontBack {
5252

5353
class CPPCHECKLIB TokenList {
5454
public:
55+
// TODO: pass settings as reference
5556
explicit TokenList(const Settings* settings);
5657
~TokenList();
5758

test/testlibrary.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class TestLibrary : public TestFixture {
117117
" </function>\n"
118118
"</def>";
119119

120-
TokenList tokenList(nullptr);
120+
TokenList tokenList(&settings);
121121
std::istringstream istr("foo();");
122122
tokenList.createTokens(istr, Standards::Language::CPP);
123123
tokenList.front()->next()->astOperand1(tokenList.front());
@@ -140,14 +140,14 @@ class TestLibrary : public TestFixture {
140140
Library library;
141141
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
142142
{
143-
TokenList tokenList(nullptr);
143+
TokenList tokenList(&settings);
144144
std::istringstream istr("fred.foo(123);"); // <- wrong scope, not library function
145145
tokenList.createTokens(istr, Standards::Language::CPP);
146146

147147
ASSERT(library.isNotLibraryFunction(tokenList.front()->tokAt(2)));
148148
}
149149
{
150-
TokenList tokenList(nullptr);
150+
TokenList tokenList(&settings);
151151
std::istringstream istr("Fred::foo(123);"); // <- wrong scope, not library function
152152
tokenList.createTokens(istr, Standards::Language::CPP);
153153

@@ -163,7 +163,7 @@ class TestLibrary : public TestFixture {
163163
" </function>\n"
164164
"</def>";
165165

166-
TokenList tokenList(nullptr);
166+
TokenList tokenList(&settings);
167167
std::istringstream istr("foo();"); // <- too few arguments, not library function
168168
tokenList.createTokens(istr, Standards::Language::CPP);
169169
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
@@ -187,7 +187,7 @@ class TestLibrary : public TestFixture {
187187
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
188188

189189
{
190-
TokenList tokenList(nullptr);
190+
TokenList tokenList(&settings);
191191
std::istringstream istr("foo();"); // <- too few arguments, not library function
192192
tokenList.createTokens(istr, Standards::Language::CPP);
193193
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
@@ -196,7 +196,7 @@ class TestLibrary : public TestFixture {
196196
ASSERT(library.isNotLibraryFunction(tokenList.front()));
197197
}
198198
{
199-
TokenList tokenList(nullptr);
199+
TokenList tokenList(&settings);
200200
std::istringstream istr("foo(a);"); // <- library function
201201
tokenList.createTokens(istr, Standards::Language::CPP);
202202
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
@@ -205,7 +205,7 @@ class TestLibrary : public TestFixture {
205205
ASSERT(!library.isNotLibraryFunction(tokenList.front()));
206206
}
207207
{
208-
TokenList tokenList(nullptr);
208+
TokenList tokenList(&settings);
209209
std::istringstream istr("foo(a, b);"); // <- library function
210210
tokenList.createTokens(istr, Standards::Language::CPP);
211211
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
@@ -214,7 +214,7 @@ class TestLibrary : public TestFixture {
214214
ASSERT(!library.isNotLibraryFunction(tokenList.front()));
215215
}
216216
{
217-
TokenList tokenList(nullptr);
217+
TokenList tokenList(&settings);
218218
std::istringstream istr("foo(a, b, c);"); // <- too much arguments, not library function
219219
tokenList.createTokens(istr, Standards::Language::CPP);
220220
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
@@ -232,7 +232,7 @@ class TestLibrary : public TestFixture {
232232
" </function>\n"
233233
"</def>";
234234

235-
TokenList tokenList(nullptr);
235+
TokenList tokenList(&settings);
236236
std::istringstream istr("Fred foo(123);"); // <- Variable declaration, not library function
237237
tokenList.createTokens(istr, Standards::Language::CPP);
238238
tokenList.front()->next()->astOperand1(tokenList.front());
@@ -292,7 +292,7 @@ class TestLibrary : public TestFixture {
292292
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
293293
ASSERT_EQUALS(0, library.functions["foo"].argumentChecks[-1].notuninit);
294294

295-
TokenList tokenList(nullptr);
295+
TokenList tokenList(&settings);
296296
std::istringstream istr("foo(a,b,c,d,e);");
297297
tokenList.createTokens(istr, Standards::Language::CPP);
298298
tokenList.front()->next()->astOperand1(tokenList.front());
@@ -317,7 +317,7 @@ class TestLibrary : public TestFixture {
317317
Library library;
318318
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
319319

320-
TokenList tokenList(nullptr);
320+
TokenList tokenList(&settings);
321321
std::istringstream istr("foo(a,b,c,d);");
322322
tokenList.createTokens(istr, Standards::Language::CPP);
323323
tokenList.front()->next()->astOperand1(tokenList.front());
@@ -349,7 +349,7 @@ class TestLibrary : public TestFixture {
349349
Library library;
350350
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
351351

352-
TokenList tokenList(nullptr);
352+
TokenList tokenList(&settings);
353353
std::istringstream istr("foo(a,b,c,d,e,f,g,h,i,j,k);");
354354
tokenList.createTokens(istr, Standards::Language::CPP);
355355
tokenList.front()->next()->astOperand1(tokenList.front());
@@ -491,7 +491,7 @@ class TestLibrary : public TestFixture {
491491
Library library;
492492
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
493493

494-
TokenList tokenList(nullptr);
494+
TokenList tokenList(&settings);
495495
std::istringstream istr("foo(a,b,c,d,e);");
496496
tokenList.createTokens(istr, Standards::Language::CPP);
497497
tokenList.front()->next()->astOperand1(tokenList.front());
@@ -554,14 +554,14 @@ class TestLibrary : public TestFixture {
554554
ASSERT(library.functions.at("bar").argumentChecks.empty());
555555

556556
{
557-
TokenList tokenList(nullptr);
557+
TokenList tokenList(&settings);
558558
std::istringstream istr("Foo::foo();");
559559
tokenList.createTokens(istr, Standards::Language::CPP);
560560
ASSERT(library.isnotnoreturn(tokenList.front()->tokAt(2)));
561561
}
562562

563563
{
564-
TokenList tokenList(nullptr);
564+
TokenList tokenList(&settings);
565565
std::istringstream istr("bar();");
566566
tokenList.createTokens(istr, Standards::Language::CPP);
567567
ASSERT(library.isnotnoreturn(tokenList.front()));
@@ -635,7 +635,7 @@ class TestLibrary : public TestFixture {
635635
Library library;
636636
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
637637

638-
TokenList tokenList(nullptr);
638+
TokenList tokenList(&settings);
639639
std::istringstream istr("a(); b();");
640640
tokenList.createTokens(istr, Standards::Language::CPP);
641641

test/testtoken.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class TestToken : public TestFixture {
3838
}
3939

4040
private:
41-
/*const*/ TokenList list{nullptr};
41+
const Settings settings;
42+
/*const*/ TokenList list{&settings};
4243

4344
std::vector<std::string> arithmeticalOps;
4445
std::vector<std::string> logicalOps;
@@ -141,7 +142,6 @@ class TestToken : public TestFixture {
141142

142143
#define MatchCheck(...) MatchCheck_(__FILE__, __LINE__, __VA_ARGS__)
143144
bool MatchCheck_(const char* file, int line, const std::string& code, const std::string& pattern, unsigned int varid = 0) {
144-
const Settings settings;
145145
Tokenizer tokenizer(settings, this);
146146
std::istringstream istr(";" + code + ";");
147147
try {
@@ -437,7 +437,6 @@ class TestToken : public TestFixture {
437437
void getStrSize() const {
438438
TokensFrontBack tokensFrontBack(list);
439439
Token tok(tokensFrontBack);
440-
const Settings settings;
441440

442441
tok.str("\"\"");
443442
ASSERT_EQUALS(sizeof(""), Token::getStrSize(&tok, settings));

test/testtokenlist.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ class TestTokenList : public TestFixture {
113113

114114
{
115115
const char code2[] = "_Generic"; // C11 keyword
116-
TokenList tokenlist(nullptr); // no settings use latest standard
116+
TokenList tokenlist(&settings); // default settings use latest standard
117117
std::istringstream istr(code2);
118118
tokenlist.createTokens(istr, "a.cpp");
119119
ASSERT_EQUALS(false, tokenlist.front()->isKeyword());
120120
}
121121

122122
{
123123
const char code2[] = "_Generic"; // C11 keyword
124-
TokenList tokenlist(nullptr); // no settings use latest standard
124+
TokenList tokenlist(&settings); // default settings use latest standard
125125
std::istringstream istr(code2);
126126
tokenlist.createTokens(istr, "a.c");
127127
ASSERT_EQUALS(true, tokenlist.front()->isKeyword());
@@ -138,15 +138,15 @@ class TestTokenList : public TestFixture {
138138

139139
{
140140
const char code2[] = "co_return"; // C++20 keyword
141-
TokenList tokenlist(nullptr); // no settings use latest standard
141+
TokenList tokenlist(&settings); // default settings use latest standard
142142
std::istringstream istr(code2);
143143
tokenlist.createTokens(istr, "a.cpp");
144144
ASSERT_EQUALS(true, tokenlist.front()->isKeyword());
145145
}
146146

147147
{
148148
const char code2[] = "co_return"; // C++20 keyword
149-
TokenList tokenlist(nullptr); // no settings use latest standard
149+
TokenList tokenlist(&settings); // default settings use latest standard
150150
std::istringstream istr(code2);
151151
tokenlist.createTokens(istr, "a.c");
152152
ASSERT_EQUALS(false, tokenlist.front()->isKeyword());

test/testtokenrange.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class TestTokenRange : public TestFixture {
3636
TestTokenRange() : TestFixture("TestTokenRange") {}
3737

3838
private:
39+
const Settings settings;
40+
3941
void run() override {
4042
TEST_CASE(enumerationToEnd);
4143
TEST_CASE(untilHelperToEnd);
@@ -70,21 +72,21 @@ class TestTokenRange : public TestFixture {
7072

7173
void enumerationToEnd() const {
7274
std::istringstream istr("void a(){} void main(){ if(true){a();} }");
73-
TokenList tokenList(nullptr);
75+
TokenList tokenList(&settings);
7476
tokenList.createTokens(istr, "test.cpp");
7577
ASSERT_EQUALS("", testTokenRange(ConstTokenRange{ tokenList.front(), nullptr }, tokenList.front(), nullptr));
7678
}
7779

7880
void untilHelperToEnd() const {
7981
std::istringstream istr("void a(){} void main(){ if(true){a();} }");
80-
TokenList tokenList(nullptr);
82+
TokenList tokenList(&settings);
8183
tokenList.createTokens(istr, "test.cpp");
8284
ASSERT_EQUALS("", testTokenRange(tokenList.front()->until(nullptr), tokenList.front(), nullptr));
8385
}
8486

8587
void untilHelperPartWay() const {
8688
std::istringstream istr("void a(){} void main(){ if(true){a();} }");
87-
TokenList tokenList(nullptr);
89+
TokenList tokenList(&settings);
8890
tokenList.createTokens(istr, "test.cpp");
8991
const Token* start = tokenList.front()->tokAt(4);
9092
const Token* end = start->tokAt(8);
@@ -93,15 +95,14 @@ class TestTokenRange : public TestFixture {
9395

9496
void partialEnumeration() const {
9597
std::istringstream istr("void a(){} void main(){ if(true){a();} }");
96-
TokenList tokenList(nullptr);
98+
TokenList tokenList(&settings);
9799
tokenList.createTokens(istr, "test.cpp");
98100
const Token* start = tokenList.front()->tokAt(4);
99101
const Token* end = tokenList.front()->tokAt(10);
100102
ASSERT_EQUALS("", testTokenRange(ConstTokenRange{ start, end }, start, end));
101103
}
102104

103105
void scopeExample() const {
104-
const Settings settings;
105106
Tokenizer tokenizer(settings);
106107
std::istringstream sample("void a(){} void main(){ if(true){a();} }");
107108
ASSERT(tokenizer.tokenize(sample, "test.cpp"));
@@ -118,7 +119,7 @@ class TestTokenRange : public TestFixture {
118119

119120
void exampleAlgorithms() const {
120121
std::istringstream istr("void a(){} void main(){ if(true){a();} }");
121-
TokenList tokenList(nullptr);
122+
TokenList tokenList(&settings);
122123
tokenList.createTokens(istr, "test.cpp");
123124
ConstTokenRange range{ tokenList.front(), nullptr };
124125
ASSERT_EQUALS(true, std::all_of(range.begin(), range.end(), [](const Token*) {

0 commit comments

Comments
 (0)