Skip to content

Commit

Permalink
always provide settings for TokenList in tests (#6055)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Feb 28, 2024
1 parent 4113ea5 commit 5da25c1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 29 deletions.
1 change: 1 addition & 0 deletions lib/tokenlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct TokensFrontBack {

class CPPCHECKLIB TokenList {
public:
// TODO: pass settings as reference
explicit TokenList(const Settings* settings);
~TokenList();

Expand Down
32 changes: 16 additions & 16 deletions test/testlibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class TestLibrary : public TestFixture {
" </function>\n"
"</def>";

TokenList tokenList(nullptr);
TokenList tokenList(&settings);
std::istringstream istr("foo();");
tokenList.createTokens(istr, Standards::Language::CPP);
tokenList.front()->next()->astOperand1(tokenList.front());
Expand All @@ -140,14 +140,14 @@ class TestLibrary : public TestFixture {
Library library;
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));
{
TokenList tokenList(nullptr);
TokenList tokenList(&settings);
std::istringstream istr("fred.foo(123);"); // <- wrong scope, not library function
tokenList.createTokens(istr, Standards::Language::CPP);

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

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

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

{
TokenList tokenList(nullptr);
TokenList tokenList(&settings);
std::istringstream istr("foo();"); // <- too few arguments, not library function
tokenList.createTokens(istr, Standards::Language::CPP);
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
Expand All @@ -196,7 +196,7 @@ class TestLibrary : public TestFixture {
ASSERT(library.isNotLibraryFunction(tokenList.front()));
}
{
TokenList tokenList(nullptr);
TokenList tokenList(&settings);
std::istringstream istr("foo(a);"); // <- library function
tokenList.createTokens(istr, Standards::Language::CPP);
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
Expand All @@ -205,7 +205,7 @@ class TestLibrary : public TestFixture {
ASSERT(!library.isNotLibraryFunction(tokenList.front()));
}
{
TokenList tokenList(nullptr);
TokenList tokenList(&settings);
std::istringstream istr("foo(a, b);"); // <- library function
tokenList.createTokens(istr, Standards::Language::CPP);
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
Expand All @@ -214,7 +214,7 @@ class TestLibrary : public TestFixture {
ASSERT(!library.isNotLibraryFunction(tokenList.front()));
}
{
TokenList tokenList(nullptr);
TokenList tokenList(&settings);
std::istringstream istr("foo(a, b, c);"); // <- too much arguments, not library function
tokenList.createTokens(istr, Standards::Language::CPP);
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
Expand All @@ -232,7 +232,7 @@ class TestLibrary : public TestFixture {
" </function>\n"
"</def>";

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

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

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

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

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

{
TokenList tokenList(nullptr);
TokenList tokenList(&settings);
std::istringstream istr("Foo::foo();");
tokenList.createTokens(istr, Standards::Language::CPP);
ASSERT(library.isnotnoreturn(tokenList.front()->tokAt(2)));
}

{
TokenList tokenList(nullptr);
TokenList tokenList(&settings);
std::istringstream istr("bar();");
tokenList.createTokens(istr, Standards::Language::CPP);
ASSERT(library.isnotnoreturn(tokenList.front()));
Expand Down Expand Up @@ -635,7 +635,7 @@ class TestLibrary : public TestFixture {
Library library;
ASSERT(loadxmldata(library, xmldata, sizeof(xmldata)));

TokenList tokenList(nullptr);
TokenList tokenList(&settings);
std::istringstream istr("a(); b();");
tokenList.createTokens(istr, Standards::Language::CPP);

Expand Down
5 changes: 2 additions & 3 deletions test/testtoken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class TestToken : public TestFixture {
}

private:
/*const*/ TokenList list{nullptr};
const Settings settings;
/*const*/ TokenList list{&settings};

std::vector<std::string> arithmeticalOps;
std::vector<std::string> logicalOps;
Expand Down Expand Up @@ -141,7 +142,6 @@ class TestToken : public TestFixture {

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

tok.str("\"\"");
ASSERT_EQUALS(sizeof(""), Token::getStrSize(&tok, settings));
Expand Down
8 changes: 4 additions & 4 deletions test/testtokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ class TestTokenList : public TestFixture {

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

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

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

{
const char code2[] = "co_return"; // C++20 keyword
TokenList tokenlist(nullptr); // no settings use latest standard
TokenList tokenlist(&settings); // default settings use latest standard
std::istringstream istr(code2);
tokenlist.createTokens(istr, "a.c");
ASSERT_EQUALS(false, tokenlist.front()->isKeyword());
Expand Down
13 changes: 7 additions & 6 deletions test/testtokenrange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class TestTokenRange : public TestFixture {
TestTokenRange() : TestFixture("TestTokenRange") {}

private:
const Settings settings;

void run() override {
TEST_CASE(enumerationToEnd);
TEST_CASE(untilHelperToEnd);
Expand Down Expand Up @@ -70,21 +72,21 @@ class TestTokenRange : public TestFixture {

void enumerationToEnd() const {
std::istringstream istr("void a(){} void main(){ if(true){a();} }");
TokenList tokenList(nullptr);
TokenList tokenList(&settings);
tokenList.createTokens(istr, "test.cpp");
ASSERT_EQUALS("", testTokenRange(ConstTokenRange{ tokenList.front(), nullptr }, tokenList.front(), nullptr));
}

void untilHelperToEnd() const {
std::istringstream istr("void a(){} void main(){ if(true){a();} }");
TokenList tokenList(nullptr);
TokenList tokenList(&settings);
tokenList.createTokens(istr, "test.cpp");
ASSERT_EQUALS("", testTokenRange(tokenList.front()->until(nullptr), tokenList.front(), nullptr));
}

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

void partialEnumeration() const {
std::istringstream istr("void a(){} void main(){ if(true){a();} }");
TokenList tokenList(nullptr);
TokenList tokenList(&settings);
tokenList.createTokens(istr, "test.cpp");
const Token* start = tokenList.front()->tokAt(4);
const Token* end = tokenList.front()->tokAt(10);
ASSERT_EQUALS("", testTokenRange(ConstTokenRange{ start, end }, start, end));
}

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

void exampleAlgorithms() const {
std::istringstream istr("void a(){} void main(){ if(true){a();} }");
TokenList tokenList(nullptr);
TokenList tokenList(&settings);
tokenList.createTokens(istr, "test.cpp");
ConstTokenRange range{ tokenList.front(), nullptr };
ASSERT_EQUALS(true, std::all_of(range.begin(), range.end(), [](const Token*) {
Expand Down

0 comments on commit 5da25c1

Please sign in to comment.