From 277e96fd05760d55a6107f938eaa0a561b42f833 Mon Sep 17 00:00:00 2001 From: firewave Date: Mon, 12 Feb 2024 16:47:57 +0100 Subject: [PATCH] always provide settings for `TokenList` in tests --- lib/tokenlist.h | 1 + test/testlibrary.cpp | 32 ++++++++++++++++---------------- test/testtoken.cpp | 5 ++--- test/testtokenlist.cpp | 8 ++++---- test/testtokenrange.cpp | 13 +++++++------ 5 files changed, 30 insertions(+), 29 deletions(-) diff --git a/lib/tokenlist.h b/lib/tokenlist.h index 9c621a7eeac..9aeba9ad172 100644 --- a/lib/tokenlist.h +++ b/lib/tokenlist.h @@ -52,6 +52,7 @@ struct TokensFrontBack { class CPPCHECKLIB TokenList { public: + // TODO: pass settings as reference explicit TokenList(const Settings* settings); ~TokenList(); diff --git a/test/testlibrary.cpp b/test/testlibrary.cpp index 51c35d71baa..d7fc3ca68d5 100644 --- a/test/testlibrary.cpp +++ b/test/testlibrary.cpp @@ -117,7 +117,7 @@ class TestLibrary : public TestFixture { " \n" ""; - TokenList tokenList(nullptr); + TokenList tokenList(&settings); std::istringstream istr("foo();"); tokenList.createTokens(istr, Standards::Language::CPP); tokenList.front()->next()->astOperand1(tokenList.front()); @@ -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); @@ -163,7 +163,7 @@ class TestLibrary : public TestFixture { " \n" ""; - 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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -232,7 +232,7 @@ class TestLibrary : public TestFixture { " \n" ""; - 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()); @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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())); @@ -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); diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 8bb1dd3df5c..a93a8dda529 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -38,7 +38,8 @@ class TestToken : public TestFixture { } private: - /*const*/ TokenList list{nullptr}; + const Settings settings; + /*const*/ TokenList list{&settings}; std::vector arithmeticalOps; std::vector logicalOps; @@ -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 { @@ -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)); diff --git a/test/testtokenlist.cpp b/test/testtokenlist.cpp index a3542b6ee56..ed3729c4926 100644 --- a/test/testtokenlist.cpp +++ b/test/testtokenlist.cpp @@ -113,7 +113,7 @@ 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()); @@ -121,7 +121,7 @@ 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.c"); ASSERT_EQUALS(true, tokenlist.front()->isKeyword()); @@ -138,7 +138,7 @@ 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()); @@ -146,7 +146,7 @@ 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.c"); ASSERT_EQUALS(false, tokenlist.front()->isKeyword()); diff --git a/test/testtokenrange.cpp b/test/testtokenrange.cpp index 0e640b7844b..d1d51d8fb4b 100644 --- a/test/testtokenrange.cpp +++ b/test/testtokenrange.cpp @@ -36,6 +36,8 @@ class TestTokenRange : public TestFixture { TestTokenRange() : TestFixture("TestTokenRange") {} private: + const Settings settings; + void run() override { TEST_CASE(enumerationToEnd); TEST_CASE(untilHelperToEnd); @@ -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); @@ -93,7 +95,7 @@ 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); @@ -101,7 +103,6 @@ class TestTokenRange : public TestFixture { } 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")); @@ -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*) {