From fadc93f99b33f31b083f3429df56433da875212e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Mon, 11 Mar 2024 00:35:26 +0100 Subject: [PATCH] testrunner: refactored `givenACodeSampleToTokenize` into `SimpleTokenizer` and `SimpleTokenList` / several related cleanups (#6084) --- Makefile | 4 +- test/helpers.h | 48 +++-- test/testlibrary.cpp | 83 ++++---- test/testsimplifytemplate.cpp | 12 +- test/testsimplifytypedef.cpp | 8 +- test/testsymboldatabase.cpp | 2 +- test/testtoken.cpp | 358 +++++++++++++++++----------------- test/testtokenlist.cpp | 35 +--- test/testtokenrange.cpp | 26 ++- 9 files changed, 289 insertions(+), 287 deletions(-) diff --git a/Makefile b/Makefile index 314bf5c4e66..23a15c49e54 100644 --- a/Makefile +++ b/Makefile @@ -856,10 +856,10 @@ test/testtoken.o: test/testtoken.cpp lib/addoninfo.h lib/check.h lib/color.h lib test/testtokenize.o: test/testtokenize.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testtokenize.cpp -test/testtokenlist.o: test/testtokenlist.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h +test/testtokenlist.o: test/testtokenlist.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testtokenlist.cpp -test/testtokenrange.o: test/testtokenrange.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/tokenrange.h lib/utils.h lib/vfvalue.h test/fixture.h +test/testtokenrange.o: test/testtokenrange.cpp lib/addoninfo.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/tokenrange.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testtokenrange.cpp test/testtype.o: test/testtype.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/check.h lib/checktype.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h diff --git a/test/helpers.h b/test/helpers.h index 9326b7255eb..d134b6121d1 100644 --- a/test/helpers.h +++ b/test/helpers.h @@ -20,10 +20,12 @@ #define helpersH #include "settings.h" +#include "standards.h" #include "tokenize.h" #include "tokenlist.h" #include +#include #include #include #include @@ -31,23 +33,19 @@ class Token; class Preprocessor; class SuppressionList; +class ErrorLogger; namespace simplecpp { struct DUI; } -class givenACodeSampleToTokenize { -private: - const Settings settings; - Tokenizer tokenizer; - +class SimpleTokenizer { public: - explicit givenACodeSampleToTokenize(const char sample[], bool createOnly = false, bool cpp = true) - : tokenizer(settings, nullptr) { + SimpleTokenizer(ErrorLogger& errorlogger, const char sample[], bool cpp = true) + : tokenizer{settings, &errorlogger} + { std::istringstream iss(sample); - if (createOnly) - tokenizer.list.createTokens(iss, cpp ? "test.cpp" : "test.c"); - else - tokenizer.tokenize(iss, cpp ? "test.cpp" : "test.c"); + if (!tokenizer.tokenize(iss, cpp ? "test.cpp" : "test.c")) + throw std::runtime_error("creating tokens failed"); } Token* tokens() { @@ -57,6 +55,34 @@ class givenACodeSampleToTokenize { const Token* tokens() const { return tokenizer.tokens(); } + +private: + const Settings settings; + Tokenizer tokenizer; +}; + +class SimpleTokenList +{ +public: + + explicit SimpleTokenList(const char code[], Standards::Language lang = Standards::Language::CPP) + { + std::istringstream iss(code); + if (!list.createTokens(iss, lang)) + throw std::runtime_error("creating tokens failed"); + } + + Token* front() { + return list.front(); + } + + const Token* front() const { + return list.front(); + } + +private: + const Settings settings; + TokenList list{&settings}; }; diff --git a/test/testlibrary.cpp b/test/testlibrary.cpp index ff444a7a57c..09f2a3a5e53 100644 --- a/test/testlibrary.cpp +++ b/test/testlibrary.cpp @@ -117,9 +117,8 @@ class TestLibrary : public TestFixture { " \n" ""; - TokenList tokenList(&settings); - std::istringstream istr("foo();"); - tokenList.createTokens(istr, Standards::Language::CPP); + const char code[] = "foo();"; + SimpleTokenList tokenList(code); tokenList.front()->next()->astOperand1(tokenList.front()); Library library; @@ -140,16 +139,14 @@ class TestLibrary : public TestFixture { Library library; ASSERT(loadxmldata(library, xmldata, sizeof(xmldata))); { - TokenList tokenList(&settings); - std::istringstream istr("fred.foo(123);"); // <- wrong scope, not library function - tokenList.createTokens(istr, Standards::Language::CPP); + const char code[] = "fred.foo(123);"; // <- wrong scope, not library function + const SimpleTokenList tokenList(code); ASSERT(library.isNotLibraryFunction(tokenList.front()->tokAt(2))); } { - TokenList tokenList(&settings); - std::istringstream istr("Fred::foo(123);"); // <- wrong scope, not library function - tokenList.createTokens(istr, Standards::Language::CPP); + const char code[] = "Fred::foo(123);"; // <- wrong scope, not library function + const SimpleTokenList tokenList(code); ASSERT(library.isNotLibraryFunction(tokenList.front()->tokAt(2))); } @@ -165,7 +162,7 @@ class TestLibrary : public TestFixture { TokenList tokenList(&settings); std::istringstream istr("foo();"); // <- too few arguments, not library function - tokenList.createTokens(istr, Standards::Language::CPP); + ASSERT(tokenList.createTokens(istr, Standards::Language::CPP)); Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous()); tokenList.createAst(); @@ -189,7 +186,7 @@ class TestLibrary : public TestFixture { { TokenList tokenList(&settings); std::istringstream istr("foo();"); // <- too few arguments, not library function - tokenList.createTokens(istr, Standards::Language::CPP); + ASSERT(tokenList.createTokens(istr, Standards::Language::CPP)); Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous()); tokenList.createAst(); @@ -198,7 +195,7 @@ class TestLibrary : public TestFixture { { TokenList tokenList(&settings); std::istringstream istr("foo(a);"); // <- library function - tokenList.createTokens(istr, Standards::Language::CPP); + ASSERT(tokenList.createTokens(istr, Standards::Language::CPP)); Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous()); tokenList.createAst(); @@ -207,7 +204,7 @@ class TestLibrary : public TestFixture { { TokenList tokenList(&settings); std::istringstream istr("foo(a, b);"); // <- library function - tokenList.createTokens(istr, Standards::Language::CPP); + ASSERT(tokenList.createTokens(istr, Standards::Language::CPP)); Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous()); tokenList.createAst(); @@ -216,7 +213,7 @@ class TestLibrary : public TestFixture { { TokenList tokenList(&settings); std::istringstream istr("foo(a, b, c);"); // <- too much arguments, not library function - tokenList.createTokens(istr, Standards::Language::CPP); + ASSERT(tokenList.createTokens(istr, Standards::Language::CPP)); Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous()); tokenList.createAst(); @@ -232,9 +229,8 @@ class TestLibrary : public TestFixture { " \n" ""; - TokenList tokenList(&settings); - std::istringstream istr("Fred foo(123);"); // <- Variable declaration, not library function - tokenList.createTokens(istr, Standards::Language::CPP); + const char code[] = "Fred foo(123);"; // <- Variable declaration, not library function + SimpleTokenList tokenList(code); tokenList.front()->next()->astOperand1(tokenList.front()); tokenList.front()->next()->varId(1); @@ -292,9 +288,8 @@ class TestLibrary : public TestFixture { ASSERT(loadxmldata(library, xmldata, sizeof(xmldata))); ASSERT_EQUALS(0, library.functions["foo"].argumentChecks[-1].notuninit); - TokenList tokenList(&settings); - std::istringstream istr("foo(a,b,c,d,e);"); - tokenList.createTokens(istr, Standards::Language::CPP); + const char code[] = "foo(a,b,c,d,e);"; + SimpleTokenList tokenList(code); tokenList.front()->next()->astOperand1(tokenList.front()); ASSERT_EQUALS(false, library.isuninitargbad(tokenList.front(), 1)); @@ -317,9 +312,8 @@ class TestLibrary : public TestFixture { Library library; ASSERT(loadxmldata(library, xmldata, sizeof(xmldata))); - TokenList tokenList(&settings); - std::istringstream istr("foo(a,b,c,d);"); - tokenList.createTokens(istr, Standards::Language::CPP); + const char code[] = "foo(a,b,c,d);"; + SimpleTokenList tokenList(code); tokenList.front()->next()->astOperand1(tokenList.front()); ASSERT(Library::ArgumentChecks::Direction::DIR_IN == library.getArgDirection(tokenList.front(), 1)); @@ -349,9 +343,8 @@ class TestLibrary : public TestFixture { Library library; ASSERT(loadxmldata(library, xmldata, sizeof(xmldata))); - TokenList tokenList(&settings); - std::istringstream istr("foo(a,b,c,d,e,f,g,h,i,j,k);"); - tokenList.createTokens(istr, Standards::Language::CPP); + const char code[] = "foo(a,b,c,d,e,f,g,h,i,j,k);"; + SimpleTokenList tokenList(code); tokenList.front()->next()->astOperand1(tokenList.front()); // 1- @@ -491,9 +484,8 @@ class TestLibrary : public TestFixture { Library library; ASSERT(loadxmldata(library, xmldata, sizeof(xmldata))); - TokenList tokenList(&settings); - std::istringstream istr("foo(a,b,c,d,e);"); - tokenList.createTokens(istr, Standards::Language::CPP); + const char code[] = "foo(a,b,c,d,e);"; + SimpleTokenList tokenList(code); tokenList.front()->next()->astOperand1(tokenList.front()); // arg1: type=strlen arg2 @@ -554,16 +546,14 @@ class TestLibrary : public TestFixture { ASSERT(library.functions.at("bar").argumentChecks.empty()); { - TokenList tokenList(&settings); - std::istringstream istr("Foo::foo();"); - tokenList.createTokens(istr, Standards::Language::CPP); + const char code[] = "Foo::foo();"; + const SimpleTokenList tokenList(code); ASSERT(library.isnotnoreturn(tokenList.front()->tokAt(2))); } { - TokenList tokenList(&settings); - std::istringstream istr("bar();"); - tokenList.createTokens(istr, Standards::Language::CPP); + const char code[] = "bar();"; + const SimpleTokenList tokenList(code); ASSERT(library.isnotnoreturn(tokenList.front())); } } @@ -635,9 +625,8 @@ class TestLibrary : public TestFixture { Library library; ASSERT(loadxmldata(library, xmldata, sizeof(xmldata))); - TokenList tokenList(&settings); - std::istringstream istr("a(); b();"); - tokenList.createTokens(istr, Standards::Language::CPP); + const char code[] = "a(); b();"; + const SimpleTokenList tokenList(code); const Library::WarnInfo* a = library.getWarnInfo(tokenList.front()); const Library::WarnInfo* b = library.getWarnInfo(tokenList.front()->tokAt(4)); @@ -791,7 +780,7 @@ class TestLibrary : public TestFixture { } } - void container() const { + void container() { constexpr char xmldata[] = "\n" "\n" " \n" @@ -871,7 +860,7 @@ class TestLibrary : public TestFixture { ASSERT_EQUALS(C.arrayLike_indexOp, true); { - givenACodeSampleToTokenize var("std::A a;"); + const SimpleTokenizer var(*this, "std::A a;"); ASSERT_EQUALS(&A, library.detectContainer(var.tokens())); ASSERT(!library.detectIterator(var.tokens())); bool isIterator; @@ -880,14 +869,14 @@ class TestLibrary : public TestFixture { } { - givenACodeSampleToTokenize var("std::A::size_type a_s;"); + const SimpleTokenizer var(*this, "std::A::size_type a_s;"); ASSERT(!library.detectContainer(var.tokens())); ASSERT(!library.detectIterator(var.tokens())); ASSERT(!library.detectContainerOrIterator(var.tokens())); } { - givenACodeSampleToTokenize var("std::A::iterator a_it;"); + const SimpleTokenizer var(*this, "std::A::iterator a_it;"); ASSERT(!library.detectContainer(var.tokens())); ASSERT_EQUALS(&A, library.detectIterator(var.tokens())); bool isIterator; @@ -896,7 +885,7 @@ class TestLibrary : public TestFixture { } { - givenACodeSampleToTokenize var("std::B b;"); + const SimpleTokenizer var(*this, "std::B b;"); ASSERT_EQUALS(&B, library.detectContainer(var.tokens())); ASSERT(!library.detectIterator(var.tokens())); bool isIterator; @@ -905,14 +894,14 @@ class TestLibrary : public TestFixture { } { - givenACodeSampleToTokenize var("std::B::size_type b_s;"); + const SimpleTokenizer var(*this, "std::B::size_type b_s;"); ASSERT(!library.detectContainer(var.tokens())); ASSERT(!library.detectIterator(var.tokens())); ASSERT(!library.detectContainerOrIterator(var.tokens())); } { - givenACodeSampleToTokenize var("std::B::iterator b_it;"); + const SimpleTokenizer var(*this, "std::B::iterator b_it;"); ASSERT(!library.detectContainer(var.tokens())); ASSERT_EQUALS(&B, library.detectIterator(var.tokens())); bool isIterator; @@ -921,14 +910,14 @@ class TestLibrary : public TestFixture { } { - givenACodeSampleToTokenize var("C c;"); + const SimpleTokenizer var(*this, "C c;"); ASSERT(!library.detectContainer(var.tokens())); ASSERT(!library.detectIterator(var.tokens())); ASSERT(!library.detectContainerOrIterator(var.tokens())); } { - givenACodeSampleToTokenize var("D d;"); + const SimpleTokenizer var(*this, "D d;"); ASSERT(!library.detectContainer(var.tokens())); ASSERT(!library.detectIterator(var.tokens())); ASSERT(!library.detectContainerOrIterator(var.tokens())); diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index fe35d5791bb..6a0415cc7dc 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -5290,7 +5290,8 @@ class TestSimplifyTemplate : public TestFixture { Tokenizer tokenizer(settings, this); std::istringstream istr(code); - tokenizer.list.createTokens(istr, "test.cpp"); + if (!tokenizer.list.createTokens(istr, "test.cpp")) + return false; tokenizer.createLinks(); tokenizer.splitTemplateRightAngleBrackets(false); @@ -5357,7 +5358,8 @@ class TestSimplifyTemplate : public TestFixture { Tokenizer tokenizer(settings, this); std::istringstream istr(code); - tokenizer.list.createTokens(istr, "test.cpp"); + if (!tokenizer.list.createTokens(istr, "test.cpp")) + return false; tokenizer.createLinks(); tokenizer.splitTemplateRightAngleBrackets(false); @@ -5427,7 +5429,8 @@ class TestSimplifyTemplate : public TestFixture { Tokenizer tokenizer(settings, this); std::istringstream istr(code); - tokenizer.list.createTokens(istr, "test.cpp"); + if (!tokenizer.list.createTokens(istr, "test.cpp")) + return false; tokenizer.createLinks(); tokenizer.splitTemplateRightAngleBrackets(false); @@ -5456,7 +5459,8 @@ class TestSimplifyTemplate : public TestFixture { Tokenizer tokenizer(settings, this); std::istringstream istr(code); - tokenizer.list.createTokens(istr, "test.cpp"); + if (!tokenizer.list.createTokens(istr, "test.cpp")) + return false; tokenizer.createLinks(); tokenizer.splitTemplateRightAngleBrackets(false); diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 37c76de4eaa..88109a1816d 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -255,7 +255,8 @@ class TestSimplifyTypedef : public TestFixture { Tokenizer tokenizer(settings1, this); std::istringstream istr(code); - tokenizer.list.createTokens(istr, Standards::Language::CPP); + if (!tokenizer.list.createTokens(istr, Standards::Language::CPP)) + return ""; tokenizer.createLinks(); tokenizer.simplifyTypedef(); @@ -296,7 +297,8 @@ class TestSimplifyTypedef : public TestFixture { Tokenizer tokenizer(settings1, this); std::istringstream istr(code); - tokenizer.list.createTokens(istr, "file.c"); + if (!tokenizer.list.createTokens(istr, "file.c")) + return ""; tokenizer.createLinks(); tokenizer.simplifyTypedef(); try { @@ -4157,7 +4159,7 @@ class TestSimplifyTypedef : public TestFixture { Tokenizer tokenizer(settings1, this); std::istringstream istr(code); - tokenizer.list.createTokens(istr, "file.c"); + ASSERT(tokenizer.list.createTokens(istr, "file.c")); tokenizer.createLinks(); tokenizer.simplifyTypedef(); diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 68fd0d0a227..1328945d325 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -817,7 +817,7 @@ class TestSymbolDatabase : public TestFixture { } { reset(); - givenACodeSampleToTokenize constpointer("const int* p;"); + const SimpleTokenizer constpointer(*this, "const int* p;"); Variable v2(constpointer.tokens()->tokAt(3), constpointer.tokens()->next(), constpointer.tokens()->tokAt(2), 0, AccessControl::Public, nullptr, nullptr, &settings1); ASSERT(false == v2.isArray()); ASSERT(true == v2.isPointer()); diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 7972d27a190..95126e4048c 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -233,8 +233,8 @@ class TestToken : public TestFixture { void multiCompare2() const { // #3294 // Original pattern that failed: [[,(=<>+-*|&^] %num% [+-*/] %num% ]|,|)|;|=|%op% - givenACodeSampleToTokenize toks("a == 1", true); - ASSERT_EQUALS(true, Token::Match(toks.tokens(), "a =|%op%")); + const SimpleTokenList toks("a == 1"); + ASSERT_EQUALS(true, Token::Match(toks.front(), "a =|%op%")); } void multiCompare3() const { @@ -242,55 +242,55 @@ class TestToken : public TestFixture { // Code snippet that failed: "return lv@86 |= rv@87 ;" // Note: Also test "reverse" alternative pattern, two different code paths to handle it - givenACodeSampleToTokenize toks("return a |= b ;", true); - ASSERT_EQUALS(false, Token::Match(toks.tokens(), "return %name% xyz|%or% %name% ;")); - ASSERT_EQUALS(false, Token::Match(toks.tokens(), "return %name% %or%|xyz %name% ;")); + const SimpleTokenList toks("return a |= b ;"); + ASSERT_EQUALS(false, Token::Match(toks.front(), "return %name% xyz|%or% %name% ;")); + ASSERT_EQUALS(false, Token::Match(toks.front(), "return %name% %or%|xyz %name% ;")); - givenACodeSampleToTokenize toks2("return a | b ;", true); - ASSERT_EQUALS(true, Token::Match(toks2.tokens(), "return %name% xyz|%or% %name% ;")); - ASSERT_EQUALS(true, Token::Match(toks2.tokens(), "return %name% %or%|xyz %name% ;")); + const SimpleTokenList toks2("return a | b ;"); + ASSERT_EQUALS(true, Token::Match(toks2.front(), "return %name% xyz|%or% %name% ;")); + ASSERT_EQUALS(true, Token::Match(toks2.front(), "return %name% %or%|xyz %name% ;")); - givenACodeSampleToTokenize toks3("return a || b ;", true); - ASSERT_EQUALS(false, Token::Match(toks3.tokens(), "return %name% xyz|%or% %name% ;")); - ASSERT_EQUALS(false, Token::Match(toks3.tokens(), "return %name% %or%|xyz %name% ;")); + const SimpleTokenList toks3("return a || b ;"); + ASSERT_EQUALS(false, Token::Match(toks3.front(), "return %name% xyz|%or% %name% ;")); + ASSERT_EQUALS(false, Token::Match(toks3.front(), "return %name% %or%|xyz %name% ;")); - ASSERT_EQUALS(true, Token::Match(toks3.tokens(), "return %name% xyz|%oror% %name% ;")); - ASSERT_EQUALS(true, Token::Match(toks3.tokens(), "return %name% %oror%|xyz %name% ;")); + ASSERT_EQUALS(true, Token::Match(toks3.front(), "return %name% xyz|%oror% %name% ;")); + ASSERT_EQUALS(true, Token::Match(toks3.front(), "return %name% %oror%|xyz %name% ;")); - givenACodeSampleToTokenize toks4("a % b ;", true); - ASSERT_EQUALS(true, Token::Match(toks4.tokens(), "%name% >>|<<|&|%or%|^|% %name% ;")); - ASSERT_EQUALS(true, Token::Match(toks4.tokens(), "%name% %|>>|<<|&|%or%|^ %name% ;")); - ASSERT_EQUALS(true, Token::Match(toks4.tokens(), "%name% >>|<<|&|%or%|%|^ %name% ;")); + const SimpleTokenList toks4("a % b ;"); + ASSERT_EQUALS(true, Token::Match(toks4.front(), "%name% >>|<<|&|%or%|^|% %name% ;")); + ASSERT_EQUALS(true, Token::Match(toks4.front(), "%name% %|>>|<<|&|%or%|^ %name% ;")); + ASSERT_EQUALS(true, Token::Match(toks4.front(), "%name% >>|<<|&|%or%|%|^ %name% ;")); //%name%|%num% support - givenACodeSampleToTokenize num("100", true); - ASSERT_EQUALS(true, Token::Match(num.tokens(), "%num%|%name%")); - ASSERT_EQUALS(true, Token::Match(num.tokens(), "%name%|%num%")); - ASSERT_EQUALS(true, Token::Match(num.tokens(), "%name%|%num%|%bool%")); - ASSERT_EQUALS(true, Token::Match(num.tokens(), "%name%|%bool%|%num%")); - ASSERT_EQUALS(true, Token::Match(num.tokens(), "%name%|%bool%|%str%|%num%")); - ASSERT_EQUALS(false, Token::Match(num.tokens(), "%bool%|%name%")); - ASSERT_EQUALS(false, Token::Match(num.tokens(), "%type%|%bool%|%char%")); - ASSERT_EQUALS(true, Token::Match(num.tokens(), "%type%|%bool%|100")); - - givenACodeSampleToTokenize numparen("( 100 )", true); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %num%|%name% )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %name%|%num% )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %name%|%num%|%bool% )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %name%|%bool%|%num% )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| %name%|%bool%|%str%|%num% )|")); - ASSERT_EQUALS(false, Token::Match(numparen.tokens(), "(| %bool%|%name% )|")); - - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %num%|%name%| )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %name%|%num%| )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %name%|%num%|%bool%| )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %name%|%bool%|%num%| )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %name%|%bool%|%str%|%num%| )|")); - ASSERT_EQUALS(true, Token::Match(numparen.tokens(), "(| 100 %bool%|%name%| )|")); + const SimpleTokenList num("100"); + ASSERT_EQUALS(true, Token::Match(num.front(), "%num%|%name%")); + ASSERT_EQUALS(true, Token::Match(num.front(), "%name%|%num%")); + ASSERT_EQUALS(true, Token::Match(num.front(), "%name%|%num%|%bool%")); + ASSERT_EQUALS(true, Token::Match(num.front(), "%name%|%bool%|%num%")); + ASSERT_EQUALS(true, Token::Match(num.front(), "%name%|%bool%|%str%|%num%")); + ASSERT_EQUALS(false, Token::Match(num.front(), "%bool%|%name%")); + ASSERT_EQUALS(false, Token::Match(num.front(), "%type%|%bool%|%char%")); + ASSERT_EQUALS(true, Token::Match(num.front(), "%type%|%bool%|100")); + + const SimpleTokenList numparen("( 100 )"); + ASSERT_EQUALS(true, Token::Match(numparen.front(), "(| %num%|%name% )|")); + ASSERT_EQUALS(true, Token::Match(numparen.front(), "(| %name%|%num% )|")); + ASSERT_EQUALS(true, Token::Match(numparen.front(), "(| %name%|%num%|%bool% )|")); + ASSERT_EQUALS(true, Token::Match(numparen.front(), "(| %name%|%bool%|%num% )|")); + ASSERT_EQUALS(true, Token::Match(numparen.front(), "(| %name%|%bool%|%str%|%num% )|")); + ASSERT_EQUALS(false, Token::Match(numparen.front(), "(| %bool%|%name% )|")); + + ASSERT_EQUALS(true, Token::Match(numparen.front(), "(| 100 %num%|%name%| )|")); + ASSERT_EQUALS(true, Token::Match(numparen.front(), "(| 100 %name%|%num%| )|")); + ASSERT_EQUALS(true, Token::Match(numparen.front(), "(| 100 %name%|%num%|%bool%| )|")); + ASSERT_EQUALS(true, Token::Match(numparen.front(), "(| 100 %name%|%bool%|%num%| )|")); + ASSERT_EQUALS(true, Token::Match(numparen.front(), "(| 100 %name%|%bool%|%str%|%num%| )|")); + ASSERT_EQUALS(true, Token::Match(numparen.front(), "(| 100 %bool%|%name%| )|")); } - void multiCompare4() const { - givenACodeSampleToTokenize var("std :: queue < int > foo ;"); + void multiCompare4() { + const SimpleTokenizer var(*this, "std :: queue < int > foo ;"); ASSERT_EQUALS(Token::eBracket, var.tokens()->tokAt(3)->tokType()); ASSERT_EQUALS(Token::eBracket, var.tokens()->tokAt(5)->tokType()); @@ -545,40 +545,40 @@ class TestToken : public TestFixture { ASSERT_EQUALS(true, *tokensFront == &tok); } - void nextArgument() const { - givenACodeSampleToTokenize example1("foo(1, 2, 3, 4);"); + void nextArgument() { + const SimpleTokenizer example1(*this, "foo(1, 2, 3, 4);"); ASSERT_EQUALS(true, Token::simpleMatch(example1.tokens()->tokAt(2)->nextArgument(), "2 , 3")); ASSERT_EQUALS(true, Token::simpleMatch(example1.tokens()->tokAt(4)->nextArgument(), "3 , 4")); - givenACodeSampleToTokenize example2("foo();"); + const SimpleTokenizer example2(*this, "foo();"); ASSERT_EQUALS(true, example2.tokens()->tokAt(2)->nextArgument() == nullptr); - givenACodeSampleToTokenize example3("foo(bar(a, b), 2, 3);"); + const SimpleTokenizer example3(*this, "foo(bar(a, b), 2, 3);"); ASSERT_EQUALS(true, Token::simpleMatch(example3.tokens()->tokAt(2)->nextArgument(), "2 , 3")); - givenACodeSampleToTokenize example4("foo(x.i[1], \"\", 3);"); + const SimpleTokenizer example4(*this, "foo(x.i[1], \"\", 3);"); ASSERT_EQUALS(true, Token::simpleMatch(example4.tokens()->tokAt(2)->nextArgument(), "\"\" , 3")); } void eraseTokens() const { - givenACodeSampleToTokenize code("begin ; { this code will be removed } end", true); - Token::eraseTokens(code.tokens()->next(), code.tokens()->tokAt(9)); - ASSERT_EQUALS("begin ; end", code.tokens()->stringifyList(nullptr, false)); + SimpleTokenList code("begin ; { this code will be removed } end", Standards::Language::C); + Token::eraseTokens(code.front()->next(), code.front()->tokAt(9)); + ASSERT_EQUALS("begin ; end", code.front()->stringifyList(nullptr, false)); } void matchAny() const { - givenACodeSampleToTokenize varBitOrVar("abc|def", true); - ASSERT_EQUALS(true, Token::Match(varBitOrVar.tokens(), "%name% %or% %name%")); + const SimpleTokenList varBitOrVar("abc|def"); + ASSERT_EQUALS(true, Token::Match(varBitOrVar.front(), "%name% %or% %name%")); - givenACodeSampleToTokenize varLogOrVar("abc||def", true); - ASSERT_EQUALS(true, Token::Match(varLogOrVar.tokens(), "%name% %oror% %name%")); + const SimpleTokenList varLogOrVar("abc||def"); + ASSERT_EQUALS(true, Token::Match(varLogOrVar.front(), "%name% %oror% %name%")); } void matchSingleChar() const { - givenACodeSampleToTokenize singleChar("a", true); - ASSERT_EQUALS(true, Token::Match(singleChar.tokens(), "[a|bc]")); - ASSERT_EQUALS(false, Token::Match(singleChar.tokens(), "[d|ef]")); + const SimpleTokenList singleChar("a"); + ASSERT_EQUALS(true, Token::Match(singleChar.front(), "[a|bc]")); + ASSERT_EQUALS(false, Token::Match(singleChar.front(), "[d|ef]")); TokensFrontBack tokensFrontBack(list); Token multiChar(tokensFrontBack); @@ -587,81 +587,81 @@ class TestToken : public TestFixture { } void matchNothingOrAnyNotElse() const { - givenACodeSampleToTokenize empty_String("", true); - ASSERT_EQUALS(true, Token::Match(empty_String.tokens(), "!!else")); - ASSERT_EQUALS(false, Token::Match(empty_String.tokens(), "!!else something")); + const SimpleTokenList empty_String(""); + ASSERT_EQUALS(true, Token::Match(empty_String.front(), "!!else")); + ASSERT_EQUALS(false, Token::Match(empty_String.front(), "!!else something")); - givenACodeSampleToTokenize ifSemicolon("if ;", true); - ASSERT_EQUALS(true, Token::Match(ifSemicolon.tokens(), "if ; !!else")); + const SimpleTokenList ifSemicolon("if ;"); + ASSERT_EQUALS(true, Token::Match(ifSemicolon.front(), "if ; !!else")); - givenACodeSampleToTokenize ifSemicolonSomething("if ; something", true); - ASSERT_EQUALS(true, Token::Match(ifSemicolonSomething.tokens(), "if ; !!else")); + const SimpleTokenList ifSemicolonSomething("if ; something"); + ASSERT_EQUALS(true, Token::Match(ifSemicolonSomething.front(), "if ; !!else")); - givenACodeSampleToTokenize justElse("else", true); - ASSERT_EQUALS(false, Token::Match(justElse.tokens(), "!!else")); + const SimpleTokenList justElse("else"); + ASSERT_EQUALS(false, Token::Match(justElse.front(), "!!else")); - givenACodeSampleToTokenize ifSemicolonElse("if ; else", true); - ASSERT_EQUALS(false, Token::Match(ifSemicolonElse.tokens(), "if ; !!else")); + const SimpleTokenList ifSemicolonElse("if ; else"); + ASSERT_EQUALS(false, Token::Match(ifSemicolonElse.front(), "if ; !!else")); } - void matchType() const { - givenACodeSampleToTokenize type("abc", true); - ASSERT_EQUALS(true, Token::Match(type.tokens(), "%type%")); + void matchType() { + const SimpleTokenList type("abc"); + ASSERT_EQUALS(true, Token::Match(type.front(), "%type%")); - givenACodeSampleToTokenize isVar("int a = 3 ;"); + const SimpleTokenizer isVar(*this, "int a = 3 ;"); ASSERT_EQUALS(true, Token::Match(isVar.tokens(), "%type%")); ASSERT_EQUALS(true, Token::Match(isVar.tokens(), "%type% %name%")); ASSERT_EQUALS(false, Token::Match(isVar.tokens(), "%type% %type%")); - givenACodeSampleToTokenize noType1_cpp("delete", true, true); - ASSERT_EQUALS(false, Token::Match(noType1_cpp.tokens(), "%type%")); + const SimpleTokenList noType1_cpp("delete"); + ASSERT_EQUALS(false, Token::Match(noType1_cpp.front(), "%type%")); - givenACodeSampleToTokenize noType1_c("delete", true, false); - ASSERT_EQUALS(true, Token::Match(noType1_c.tokens(), "%type%")); + const SimpleTokenList noType1_c("delete", Standards::Language::C); + ASSERT_EQUALS(true, Token::Match(noType1_c.front(), "%type%")); - givenACodeSampleToTokenize noType2("void delete", true); - ASSERT_EQUALS(false, Token::Match(noType2.tokens(), "!!foo %type%")); + const SimpleTokenList noType2("void delete"); + ASSERT_EQUALS(false, Token::Match(noType2.front(), "!!foo %type%")); } void matchChar() const { - givenACodeSampleToTokenize chr1("'a'", true); - ASSERT_EQUALS(true, Token::Match(chr1.tokens(), "%char%")); + const SimpleTokenList chr1("'a'"); + ASSERT_EQUALS(true, Token::Match(chr1.front(), "%char%")); - givenACodeSampleToTokenize chr2("'1'", true); - ASSERT_EQUALS(true, Token::Match(chr2.tokens(), "%char%")); + const SimpleTokenList chr2("'1'"); + ASSERT_EQUALS(true, Token::Match(chr2.front(), "%char%")); - givenACodeSampleToTokenize noChr("\"10\"", true); - ASSERT_EQUALS(false, Token::Match(noChr.tokens(), "%char%")); + const SimpleTokenList noChr("\"10\""); + ASSERT_EQUALS(false, Token::Match(noChr.front(), "%char%")); } void matchCompOp() const { - givenACodeSampleToTokenize comp1("<=", true); - ASSERT_EQUALS(true, Token::Match(comp1.tokens(), "%comp%")); + const SimpleTokenList comp1("<="); + ASSERT_EQUALS(true, Token::Match(comp1.front(), "%comp%")); - givenACodeSampleToTokenize comp2(">", true); - ASSERT_EQUALS(true, Token::Match(comp2.tokens(), "%comp%")); + const SimpleTokenList comp2(">"); + ASSERT_EQUALS(true, Token::Match(comp2.front(), "%comp%")); - givenACodeSampleToTokenize noComp("=", true); - ASSERT_EQUALS(false, Token::Match(noComp.tokens(), "%comp%")); + const SimpleTokenList noComp("="); + ASSERT_EQUALS(false, Token::Match(noComp.front(), "%comp%")); } void matchStr() const { - givenACodeSampleToTokenize noStr1("abc", true); - ASSERT_EQUALS(false, Token::Match(noStr1.tokens(), "%str%")); + const SimpleTokenList noStr1("abc"); + ASSERT_EQUALS(false, Token::Match(noStr1.front(), "%str%")); - givenACodeSampleToTokenize noStr2("'a'", true); - ASSERT_EQUALS(false, Token::Match(noStr2.tokens(), "%str%")); + const SimpleTokenList noStr2("'a'"); + ASSERT_EQUALS(false, Token::Match(noStr2.front(), "%str%")); - givenACodeSampleToTokenize str("\"abc\"", true); - ASSERT_EQUALS(true, Token::Match(str.tokens(), "%str%")); + const SimpleTokenList str("\"abc\""); + ASSERT_EQUALS(true, Token::Match(str.front(), "%str%")); // Empty string - givenACodeSampleToTokenize emptyStr("\"\"", true); - ASSERT_EQUALS(true, Token::Match(emptyStr.tokens(), "%str%")); + const SimpleTokenList emptyStr("\"\""); + ASSERT_EQUALS(true, Token::Match(emptyStr.front(), "%str%")); } - void matchVarid() const { - givenACodeSampleToTokenize var("int a ; int b ;"); + void matchVarid() { + const SimpleTokenizer var(*this, "int a ; int b ;"); // Varid == 0 should throw exception ASSERT_THROW((void)Token::Match(var.tokens(), "%type% %varid% ; %type% %name%", 0),InternalError); @@ -678,98 +678,98 @@ class TestToken : public TestFixture { } void matchNumeric() const { - givenACodeSampleToTokenize nonNumeric("abc", true); - ASSERT_EQUALS(false, Token::Match(nonNumeric.tokens(), "%num%")); + const SimpleTokenList nonNumeric("abc"); + ASSERT_EQUALS(false, Token::Match(nonNumeric.front(), "%num%")); - givenACodeSampleToTokenize msLiteral("5ms", true); // #11438 - ASSERT_EQUALS(false, Token::Match(msLiteral.tokens(), "%num%")); + const SimpleTokenList msLiteral("5ms"); // #11438 + ASSERT_EQUALS(false, Token::Match(msLiteral.front(), "%num%")); - givenACodeSampleToTokenize sLiteral("3s", true); - ASSERT_EQUALS(false, Token::Match(sLiteral.tokens(), "%num%")); + const SimpleTokenList sLiteral("3s"); + ASSERT_EQUALS(false, Token::Match(sLiteral.front(), "%num%")); - givenACodeSampleToTokenize octal("0123", true); - ASSERT_EQUALS(true, Token::Match(octal.tokens(), "%num%")); + const SimpleTokenList octal("0123"); + ASSERT_EQUALS(true, Token::Match(octal.front(), "%num%")); - givenACodeSampleToTokenize decimal("4567", true); - ASSERT_EQUALS(true, Token::Match(decimal.tokens(), "%num%")); + const SimpleTokenList decimal("4567"); + ASSERT_EQUALS(true, Token::Match(decimal.front(), "%num%")); - givenACodeSampleToTokenize hexadecimal("0xDEADBEEF", true); - ASSERT_EQUALS(true, Token::Match(hexadecimal.tokens(), "%num%")); + const SimpleTokenList hexadecimal("0xDEADBEEF"); + ASSERT_EQUALS(true, Token::Match(hexadecimal.front(), "%num%")); - givenACodeSampleToTokenize floatingPoint("0.0f", true); - ASSERT_EQUALS(true, Token::Match(floatingPoint.tokens(), "%num%")); + const SimpleTokenList floatingPoint("0.0f"); + ASSERT_EQUALS(true, Token::Match(floatingPoint.front(), "%num%")); - givenACodeSampleToTokenize signedLong("0L", true); - ASSERT_EQUALS(true, Token::Match(signedLong.tokens(), "%num%")); + const SimpleTokenList signedLong("0L"); + ASSERT_EQUALS(true, Token::Match(signedLong.front(), "%num%")); - givenACodeSampleToTokenize negativeSignedLong("-0L", true); - ASSERT_EQUALS(true, Token::Match(negativeSignedLong.tokens(), "- %num%")); + const SimpleTokenList negativeSignedLong("-0L"); + ASSERT_EQUALS(true, Token::Match(negativeSignedLong.front(), "- %num%")); - givenACodeSampleToTokenize positiveSignedLong("+0L", true); - ASSERT_EQUALS(true, Token::Match(positiveSignedLong.tokens(), "+ %num%")); + const SimpleTokenList positiveSignedLong("+0L"); + ASSERT_EQUALS(true, Token::Match(positiveSignedLong.front(), "+ %num%")); - givenACodeSampleToTokenize unsignedInt("0U", true); - ASSERT_EQUALS(true, Token::Match(unsignedInt.tokens(), "%num%")); + const SimpleTokenList unsignedInt("0U"); + ASSERT_EQUALS(true, Token::Match(unsignedInt.front(), "%num%")); - givenACodeSampleToTokenize unsignedLong("0UL", true); - ASSERT_EQUALS(true, Token::Match(unsignedLong.tokens(), "%num%")); + const SimpleTokenList unsignedLong("0UL"); + ASSERT_EQUALS(true, Token::Match(unsignedLong.front(), "%num%")); - givenACodeSampleToTokenize unsignedLongLong("0ULL", true); - ASSERT_EQUALS(true, Token::Match(unsignedLongLong.tokens(), "%num%")); + const SimpleTokenList unsignedLongLong("0ULL"); + ASSERT_EQUALS(true, Token::Match(unsignedLongLong.front(), "%num%")); - givenACodeSampleToTokenize positive("+666", true); - ASSERT_EQUALS(true, Token::Match(positive.tokens(), "+ %num%")); + const SimpleTokenList positive("+666"); + ASSERT_EQUALS(true, Token::Match(positive.front(), "+ %num%")); - givenACodeSampleToTokenize negative("-42", true); - ASSERT_EQUALS(true, Token::Match(negative.tokens(), "- %num%")); + const SimpleTokenList negative("-42"); + ASSERT_EQUALS(true, Token::Match(negative.front(), "- %num%")); - givenACodeSampleToTokenize negativeNull("-.0", true); - ASSERT_EQUALS(true, Token::Match(negativeNull.tokens(), "- %num%")); + const SimpleTokenList negativeNull("-.0"); + ASSERT_EQUALS(true, Token::Match(negativeNull.front(), "- %num%")); - givenACodeSampleToTokenize positiveNull("+.0", true); - ASSERT_EQUALS(true, Token::Match(positiveNull.tokens(), "+ %num%")); + const SimpleTokenList positiveNull("+.0"); + ASSERT_EQUALS(true, Token::Match(positiveNull.front(), "+ %num%")); } void matchBoolean() const { - givenACodeSampleToTokenize yes("YES", true); - ASSERT_EQUALS(false, Token::Match(yes.tokens(), "%bool%")); + const SimpleTokenList yes("YES"); + ASSERT_EQUALS(false, Token::Match(yes.front(), "%bool%")); - givenACodeSampleToTokenize positive("true", true); - ASSERT_EQUALS(true, Token::Match(positive.tokens(), "%bool%")); + const SimpleTokenList positive("true"); + ASSERT_EQUALS(true, Token::Match(positive.front(), "%bool%")); - givenACodeSampleToTokenize negative("false", true); - ASSERT_EQUALS(true, Token::Match(negative.tokens(), "%bool%")); + const SimpleTokenList negative("false"); + ASSERT_EQUALS(true, Token::Match(negative.front(), "%bool%")); } - void matchOr() const { - givenACodeSampleToTokenize bitwiseOr(";|;", true); + void matchOr() { + const SimpleTokenList bitwiseOr(";|;"); // cppcheck-suppress simplePatternError - this is intentional - ASSERT_EQUALS(true, Token::Match(bitwiseOr.tokens(), "; %or%")); - ASSERT_EQUALS(true, Token::Match(bitwiseOr.tokens(), "; %op%")); + ASSERT_EQUALS(true, Token::Match(bitwiseOr.front(), "; %or%")); + ASSERT_EQUALS(true, Token::Match(bitwiseOr.front(), "; %op%")); // cppcheck-suppress simplePatternError - this is intentional - ASSERT_EQUALS(false, Token::Match(bitwiseOr.tokens(), "; %oror%")); + ASSERT_EQUALS(false, Token::Match(bitwiseOr.front(), "; %oror%")); - givenACodeSampleToTokenize bitwiseOrAssignment(";|=;"); + const SimpleTokenizer bitwiseOrAssignment(*this, ";|=;"); // cppcheck-suppress simplePatternError - this is intentional ASSERT_EQUALS(false, Token::Match(bitwiseOrAssignment.tokens(), "; %or%")); ASSERT_EQUALS(true, Token::Match(bitwiseOrAssignment.tokens(), "; %op%")); // cppcheck-suppress simplePatternError - this is intentional ASSERT_EQUALS(false, Token::Match(bitwiseOrAssignment.tokens(), "; %oror%")); - givenACodeSampleToTokenize logicalOr(";||;", true); + const SimpleTokenList logicalOr(";||;"); // cppcheck-suppress simplePatternError - this is intentional - ASSERT_EQUALS(false, Token::Match(logicalOr.tokens(), "; %or%")); - ASSERT_EQUALS(true, Token::Match(logicalOr.tokens(), "; %op%")); + ASSERT_EQUALS(false, Token::Match(logicalOr.front(), "; %or%")); + ASSERT_EQUALS(true, Token::Match(logicalOr.front(), "; %op%")); // cppcheck-suppress simplePatternError - this is intentional - ASSERT_EQUALS(true, Token::Match(logicalOr.tokens(), "; %oror%")); - ASSERT_EQUALS(true, Token::Match(logicalOr.tokens(), "; &&|%oror%")); - ASSERT_EQUALS(true, Token::Match(logicalOr.tokens(), "; %oror%|&&")); - - givenACodeSampleToTokenize logicalAnd(";&&;", true); - ASSERT_EQUALS(true, Token::simpleMatch(logicalAnd.tokens(), "; &&")); - ASSERT_EQUALS(true, Token::Match(logicalAnd.tokens(), "; &&|%oror%")); - ASSERT_EQUALS(true, Token::Match(logicalAnd.tokens(), "; %oror%|&&")); + ASSERT_EQUALS(true, Token::Match(logicalOr.front(), "; %oror%")); + ASSERT_EQUALS(true, Token::Match(logicalOr.front(), "; &&|%oror%")); + ASSERT_EQUALS(true, Token::Match(logicalOr.front(), "; %oror%|&&")); + + const SimpleTokenList logicalAnd(";&&;"); + ASSERT_EQUALS(true, Token::simpleMatch(logicalAnd.front(), "; &&")); + ASSERT_EQUALS(true, Token::Match(logicalAnd.front(), "; &&|%oror%")); + ASSERT_EQUALS(true, Token::Match(logicalAnd.front(), "; %oror%|&&")); } static void append_vector(std::vector &dest, const std::vector &src) { @@ -1105,39 +1105,39 @@ class TestToken : public TestFixture { ASSERT_EQUALS(true, tok.isName()); } - void canFindMatchingBracketsNeedsOpen() const { - givenACodeSampleToTokenize var("std::deque > intsets;"); + void canFindMatchingBracketsNeedsOpen() { + const SimpleTokenizer var(*this, "std::deque > intsets;"); const Token* const t = var.tokens()->findClosingBracket(); ASSERT(t == nullptr); } - void canFindMatchingBracketsInnerPair() const { - givenACodeSampleToTokenize var("std::deque > intsets;"); + void canFindMatchingBracketsInnerPair() { + const SimpleTokenizer var(*this, "std::deque > intsets;"); const Token * const t = var.tokens()->tokAt(7)->findClosingBracket(); ASSERT_EQUALS(">", t->str()); ASSERT(var.tokens()->tokAt(9) == t); } - void canFindMatchingBracketsOuterPair() const { - givenACodeSampleToTokenize var("std::deque > intsets;"); + void canFindMatchingBracketsOuterPair() { + const SimpleTokenizer var(*this, "std::deque > intsets;"); const Token* const t = var.tokens()->tokAt(3)->findClosingBracket(); ASSERT_EQUALS(">", t->str()); ASSERT(var.tokens()->tokAt(10) == t); } - void canFindMatchingBracketsWithTooManyClosing() const { - givenACodeSampleToTokenize var("X< 1>2 > x1;"); + void canFindMatchingBracketsWithTooManyClosing() { + const SimpleTokenizer var(*this, "X< 1>2 > x1;"); const Token* const t = var.tokens()->next()->findClosingBracket(); ASSERT_EQUALS(">", t->str()); ASSERT(var.tokens()->tokAt(3) == t); } - void canFindMatchingBracketsWithTooManyOpening() const { - givenACodeSampleToTokenize var("X < (2 < 1) > x1;"); + void canFindMatchingBracketsWithTooManyOpening() { + const SimpleTokenizer var(*this, "X < (2 < 1) > x1;"); const Token* t = var.tokens()->next()->findClosingBracket(); ASSERT(t != nullptr && t->str() == ">"); @@ -1146,39 +1146,39 @@ class TestToken : public TestFixture { ASSERT(t == nullptr); } - void findClosingBracket() const { - givenACodeSampleToTokenize var("template struct S : public Fred> {}"); + void findClosingBracket() { + const SimpleTokenizer var(*this, "template struct S : public Fred> {}"); const Token* const t = var.tokens()->next()->findClosingBracket(); ASSERT(Token::simpleMatch(t, "> struct")); } - void findClosingBracket2() const { - givenACodeSampleToTokenize var("const auto g = []() {};\n"); // #11275 + void findClosingBracket2() { + const SimpleTokenizer var(*this, "const auto g = []() {};\n"); // #11275 const Token* const t = Token::findsimplematch(var.tokens(), "<"); ASSERT(t && Token::simpleMatch(t->findClosingBracket(), ">")); } - void expressionString() const { - givenACodeSampleToTokenize var1("void f() { *((unsigned long long *)x) = 0; }"); + void expressionString() { + const SimpleTokenizer var1(*this, "void f() { *((unsigned long long *)x) = 0; }"); const Token *const tok1 = Token::findsimplematch(var1.tokens(), "*"); ASSERT_EQUALS("*((unsigned long long*)x)", tok1->expressionString()); - givenACodeSampleToTokenize var2("typedef unsigned long long u64; void f() { *((u64 *)x) = 0; }"); + const SimpleTokenizer var2(*this, "typedef unsigned long long u64; void f() { *((u64 *)x) = 0; }"); const Token *const tok2 = Token::findsimplematch(var2.tokens(), "*"); ASSERT_EQUALS("*((unsigned long long*)x)", tok2->expressionString()); - givenACodeSampleToTokenize data3("void f() { return (t){1,2}; }"); + const SimpleTokenizer data3(*this, "void f() { return (t){1,2}; }"); ASSERT_EQUALS("return(t){1,2}", data3.tokens()->tokAt(5)->expressionString()); - givenACodeSampleToTokenize data4("void f() { return L\"a\"; }"); + const SimpleTokenizer data4(*this, "void f() { return L\"a\"; }"); ASSERT_EQUALS("returnL\"a\"", data4.tokens()->tokAt(5)->expressionString()); - givenACodeSampleToTokenize data5("void f() { return U\"a\"; }"); + const SimpleTokenizer data5(*this, "void f() { return U\"a\"; }"); ASSERT_EQUALS("returnU\"a\"", data5.tokens()->tokAt(5)->expressionString()); - givenACodeSampleToTokenize data6("x = \"\\0\\x1\\x2\\x3\\x4\\x5\\x6\\x7\";"); + const SimpleTokenizer data6(*this, "x = \"\\0\\x1\\x2\\x3\\x4\\x5\\x6\\x7\";"); ASSERT_EQUALS("x=\"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\"", data6.tokens()->next()->expressionString()); } diff --git a/test/testtokenlist.cpp b/test/testtokenlist.cpp index e58dd7b7c6d..9e92f2b70e7 100644 --- a/test/testtokenlist.cpp +++ b/test/testtokenlist.cpp @@ -18,6 +18,7 @@ #include "settings.h" #include "fixture.h" +#include "helpers.h" #include "platform.h" #include "standards.h" #include "token.h" @@ -62,11 +63,7 @@ class TestTokenList : public TestFixture { errout.str(""); - // tokenize.. - TokenList tokenlist(&settings); - std::istringstream istr(code); - tokenlist.createTokens(istr, "a.cpp"); - + const SimpleTokenList tokenlist(code); ASSERT(Token::simpleMatch(tokenlist.front(), "a + + 1 ; 1 + + b ;")); } @@ -75,9 +72,7 @@ class TestTokenList : public TestFixture { const char code[] = "for a int delete true"; { - TokenList tokenlist(&settings); - std::istringstream istr(code); - tokenlist.createTokens(istr, "a.c"); + const SimpleTokenList tokenlist(code, Standards::Language::C); ASSERT_EQUALS(true, tokenlist.front()->isKeyword()); ASSERT_EQUALS(true, tokenlist.front()->isControlFlowKeyword()); @@ -93,9 +88,7 @@ class TestTokenList : public TestFixture { ASSERT_EQUALS(false, tokenlist.front()->tokAt(4)->isControlFlowKeyword()); } { - TokenList tokenlist(&settings); - std::istringstream istr(code); - tokenlist.createTokens(istr, "a.cpp"); + const SimpleTokenList tokenlist(code); ASSERT_EQUALS(true, tokenlist.front()->isKeyword()); ASSERT_EQUALS(true, tokenlist.front()->isControlFlowKeyword()); @@ -113,17 +106,13 @@ class TestTokenList : public TestFixture { { const char code2[] = "_Generic"; // C11 keyword - TokenList tokenlist(&settings); // default settings use latest standard - std::istringstream istr(code2); - tokenlist.createTokens(istr, "a.cpp"); + const SimpleTokenList tokenlist(code2); // default settings use latest standard ASSERT_EQUALS(false, tokenlist.front()->isKeyword()); } { const char code2[] = "_Generic"; // C11 keyword - TokenList tokenlist(&settings); // default settings use latest standard - std::istringstream istr(code2); - tokenlist.createTokens(istr, "a.c"); + const SimpleTokenList tokenlist(code2, Standards::Language::C); // default settings use latest standard ASSERT_EQUALS(true, tokenlist.front()->isKeyword()); } @@ -132,23 +121,19 @@ class TestTokenList : public TestFixture { const Settings s = settingsBuilder().c(Standards::C89).build(); TokenList tokenlist(&s); std::istringstream istr(code2); - tokenlist.createTokens(istr, "a.c"); + ASSERT(tokenlist.createTokens(istr, "a.c")); ASSERT_EQUALS(false, tokenlist.front()->isKeyword()); } { const char code2[] = "co_return"; // C++20 keyword - TokenList tokenlist(&settings); // default settings use latest standard - std::istringstream istr(code2); - tokenlist.createTokens(istr, "a.cpp"); + const SimpleTokenList tokenlist(code2); // default settings use latest standard ASSERT_EQUALS(true, tokenlist.front()->isKeyword()); } { const char code2[] = "co_return"; // C++20 keyword - TokenList tokenlist(&settings); // default settings use latest standard - std::istringstream istr(code2); - tokenlist.createTokens(istr, "a.c"); + const SimpleTokenList tokenlist(code2, Standards::Language::C); // default settings use latest standard ASSERT_EQUALS(false, tokenlist.front()->isKeyword()); } @@ -157,7 +142,7 @@ class TestTokenList : public TestFixture { const Settings s = settingsBuilder().cpp(Standards::CPP03).build(); TokenList tokenlist(&s); std::istringstream istr(code2); - tokenlist.createTokens(istr, "a.cpp"); + ASSERT(tokenlist.createTokens(istr, "a.cpp")); ASSERT_EQUALS(false, tokenlist.front()->isKeyword()); } } diff --git a/test/testtokenrange.cpp b/test/testtokenrange.cpp index dee15b918c6..4843591654b 100644 --- a/test/testtokenrange.cpp +++ b/test/testtokenrange.cpp @@ -18,6 +18,7 @@ #include "settings.h" #include "fixture.h" +#include "helpers.h" #include "token.h" #include "tokenize.h" #include "tokenlist.h" @@ -69,32 +70,28 @@ class TestTokenRange : public TestFixture { } void enumerationToEnd() const { - std::istringstream istr("void a(){} void main(){ if(true){a();} }"); - TokenList tokenList(&settingsDefault); - tokenList.createTokens(istr, "test.cpp"); + const char code[] = "void a(){} void main(){ if(true){a();} }"; + const SimpleTokenList tokenList(code); 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(&settingsDefault); - tokenList.createTokens(istr, "test.cpp"); + const char code[] = "void a(){} void main(){ if(true){a();} }"; + const SimpleTokenList tokenList(code); 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(&settingsDefault); - tokenList.createTokens(istr, "test.cpp"); + const char code[] = "void a(){} void main(){ if(true){a();} }"; + const SimpleTokenList tokenList(code); const Token* start = tokenList.front()->tokAt(4); const Token* end = start->tokAt(8); ASSERT_EQUALS("", testTokenRange(start->until(end), start, end)); } void partialEnumeration() const { - std::istringstream istr("void a(){} void main(){ if(true){a();} }"); - TokenList tokenList(&settingsDefault); - tokenList.createTokens(istr, "test.cpp"); + const char code[] = "void a(){} void main(){ if(true){a();} }"; + const SimpleTokenList tokenList(code); const Token* start = tokenList.front()->tokAt(4); const Token* end = tokenList.front()->tokAt(10); ASSERT_EQUALS("", testTokenRange(ConstTokenRange{ start, end }, start, end)); @@ -116,9 +113,8 @@ class TestTokenRange : public TestFixture { } void exampleAlgorithms() const { - std::istringstream istr("void a(){} void main(){ if(true){a();} }"); - TokenList tokenList(&settingsDefault); - tokenList.createTokens(istr, "test.cpp"); + const char code[] = "void a(){} void main(){ if(true){a();} }"; + const SimpleTokenList tokenList(code); ConstTokenRange range{ tokenList.front(), nullptr }; ASSERT_EQUALS(true, std::all_of(range.begin(), range.end(), [](const Token*) { return true;