diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 52fb9bf1cff1..ce85191c743f 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -802,7 +802,7 @@ unsigned int CppCheck::checkInternal(const std::string& filename, const std::str TokenList tokenlist(&mSettings); std::istringstream istr2(code); // TODO: asserts when file has unknown extension - tokenlist.createTokens(istr2, Path::identify(*files.begin())); // TODO: check result? + tokenlist.createTokens(code.data(), code.size(), Path::identify(*files.begin())); // TODO: check result? executeRules("define", tokenlist); } #endif diff --git a/lib/library.cpp b/lib/library.cpp index ddd2a13ed90b..a4a5b1223bcd 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -35,7 +35,6 @@ #include #include #include -#include #include #include #include @@ -55,8 +54,8 @@ static std::vector getnames(const char *names) static void gettokenlistfromvalid(const std::string& valid, bool cpp, TokenList& tokenList) { - std::istringstream istr(valid + ','); - tokenList.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result? + const std::string str(valid + ','); + tokenList.createTokens(str.data(), str.size(), cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result? for (Token *tok = tokenList.front(); tok; tok = tok->next()) { if (Token::Match(tok,"- %num%")) { tok->str("-" + tok->strAt(1)); @@ -1769,8 +1768,7 @@ std::shared_ptr createTokenFromExpression(const std::string& returnValue, std::shared_ptr tokenList = std::make_shared(&settings); { const std::string code = "return " + returnValue + ";"; - std::istringstream istr(code); - if (!tokenList->createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C)) + if (!tokenList->createTokens(code.data(), code.size(), cpp ? Standards::Language::CPP : Standards::Language::C)) return nullptr; } diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index df786b608eb8..ff41411f23a6 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -7558,8 +7558,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to if (!typestr.empty()) { ValueType valuetype; TokenList tokenList(&mSettings); - std::istringstream istr(typestr+";"); - tokenList.createTokens(istr, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C); // TODO: check result? + const std::string str(typestr+";"); + tokenList.createTokens(str.data(), str.size(), tok->isCpp() ? Standards::Language::CPP : Standards::Language::C); // TODO: check result? tokenList.simplifyStdType(); if (parsedecl(tokenList.front(), &valuetype, mDefaultSignedness, mSettings)) { valuetype.originalTypeName = typestr; @@ -7648,8 +7648,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to continue; } TokenList tokenList(&mSettings); - std::istringstream istr(typestr+";"); - if (tokenList.createTokens(istr, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C)) { + const std::string str(typestr+";"); + if (tokenList.createTokens(str.data(), str.size(), tok->isCpp() ? Standards::Language::CPP : Standards::Language::C)) { ValueType vt; tokenList.simplifyPlatformTypes(); tokenList.simplifyStdType(); diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index bbec433c3ecb..776128794402 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -121,7 +121,6 @@ #include #include #include -#include #include #include #include @@ -3871,8 +3870,7 @@ static bool isNotEqual(std::pair x, std::pair x, const std::string& y, bool cpp) { TokenList tokenList(nullptr); - std::istringstream istr(y); - tokenList.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result? + tokenList.createTokens(y.data(), y.size(), cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result? return isNotEqual(x, std::make_pair(tokenList.front(), tokenList.back())); } static bool isNotEqual(std::pair x, const ValueType* y, bool cpp) @@ -9250,8 +9248,8 @@ static bool getMinMaxValues(const ValueType *vt, const Platform &platform, MathL static bool getMinMaxValues(const std::string &typestr, const Settings &settings, bool cpp, MathLib::bigint &minvalue, MathLib::bigint &maxvalue) { TokenList typeTokens(&settings); - std::istringstream istr(typestr+";"); - if (!typeTokens.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C)) + const std::string str(typestr+";"); + if (!typeTokens.createTokens(str.data(), str.size(), cpp ? Standards::Language::CPP : Standards::Language::C)) return false; typeTokens.simplifyPlatformTypes(); typeTokens.simplifyStdType(); diff --git a/test/helpers.h b/test/helpers.h index 107db754069e..928c3a768387 100644 --- a/test/helpers.h +++ b/test/helpers.h @@ -90,11 +90,10 @@ class SimpleTokenizer : public Tokenizer { class SimpleTokenList { public: - - explicit SimpleTokenList(const char code[], Standards::Language lang = Standards::Language::CPP) + template + SimpleTokenList(const char (&data)[size], Standards::Language lang = Standards::Language::CPP) { - std::istringstream iss(code); - if (!list.createTokens(iss, lang)) + if (!list.createTokens(data, size-1, lang)) throw std::runtime_error("creating tokens failed"); } diff --git a/test/testlibrary.cpp b/test/testlibrary.cpp index e6cf6f2c4ed0..56d1203ac1aa 100644 --- a/test/testlibrary.cpp +++ b/test/testlibrary.cpp @@ -160,8 +160,8 @@ class TestLibrary : public TestFixture { ""; TokenList tokenList(&settings); - std::istringstream istr("foo();"); // <- too few arguments, not library function - ASSERT(tokenList.createTokens(istr, Standards::Language::CPP)); + const char code[] = "foo();"; // <- too few arguments, not library function + ASSERT(tokenList.createTokens(code, Standards::Language::CPP)); Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous()); tokenList.createAst(); @@ -184,8 +184,8 @@ class TestLibrary : public TestFixture { { TokenList tokenList(&settings); - std::istringstream istr("foo();"); // <- too few arguments, not library function - ASSERT(tokenList.createTokens(istr, Standards::Language::CPP)); + const char code[] = "foo();"; // <- too few arguments, not library function + ASSERT(tokenList.createTokens(code, Standards::Language::CPP)); Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous()); tokenList.createAst(); @@ -193,8 +193,8 @@ class TestLibrary : public TestFixture { } { TokenList tokenList(&settings); - std::istringstream istr("foo(a);"); // <- library function - ASSERT(tokenList.createTokens(istr, Standards::Language::CPP)); + const char code[] = "foo(a);"; // <- library function + ASSERT(tokenList.createTokens(code, Standards::Language::CPP)); Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous()); tokenList.createAst(); @@ -202,8 +202,8 @@ class TestLibrary : public TestFixture { } { TokenList tokenList(&settings); - std::istringstream istr("foo(a, b);"); // <- library function - ASSERT(tokenList.createTokens(istr, Standards::Language::CPP)); + const char code[] = "foo(a, b);"; // <- library function + ASSERT(tokenList.createTokens(code, Standards::Language::CPP)); Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous()); tokenList.createAst(); @@ -211,8 +211,8 @@ class TestLibrary : public TestFixture { } { TokenList tokenList(&settings); - std::istringstream istr("foo(a, b, c);"); // <- too much arguments, not library function - ASSERT(tokenList.createTokens(istr, Standards::Language::CPP)); + const char code[] = "foo(a, b, c);"; // <- too much arguments, not library function + ASSERT(tokenList.createTokens(code, Standards::Language::CPP)); Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous()); tokenList.createAst(); diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 01f5e31c274e..c1f3175457b8 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -5301,11 +5301,11 @@ class TestSimplifyTemplate : public TestFixture { "C> y;")); } - unsigned int templateParameters(const char code[]) { + template + unsigned int templateParameters(const char (&data)[size]) { Tokenizer tokenizer(settings, *this); - std::istringstream istr(code); - if (!tokenizer.list.createTokens(istr, "test.cpp")) + if (!tokenizer.list.createTokens(data, size-1, "test.cpp")) return false; tokenizer.createLinks(); tokenizer.splitTemplateRightAngleBrackets(false); @@ -5369,11 +5369,11 @@ class TestSimplifyTemplate : public TestFixture { } // Helper function to unit test TemplateSimplifier::getTemplateNamePosition - int templateNamePositionHelper(const char code[], unsigned offset = 0) { + template + int templateNamePositionHelper(const char (&data)[size], unsigned offset = 0) { Tokenizer tokenizer(settings, *this); - std::istringstream istr(code); - if (!tokenizer.list.createTokens(istr, "test.cpp")) + if (!tokenizer.list.createTokens(data, size-1, "test.cpp")) return false; tokenizer.createLinks(); tokenizer.splitTemplateRightAngleBrackets(false); @@ -5440,11 +5440,11 @@ class TestSimplifyTemplate : public TestFixture { } // Helper function to unit test TemplateSimplifier::findTemplateDeclarationEnd - bool findTemplateDeclarationEndHelper(const char code[], const char pattern[], unsigned offset = 0) { + template + bool findTemplateDeclarationEndHelper(const char (&data)[size], const char pattern[], unsigned offset = 0) { Tokenizer tokenizer(settings, *this); - std::istringstream istr(code); - if (!tokenizer.list.createTokens(istr, "test.cpp")) + if (!tokenizer.list.createTokens(data, size-1, "test.cpp")) return false; tokenizer.createLinks(); tokenizer.splitTemplateRightAngleBrackets(false); @@ -5470,11 +5470,11 @@ class TestSimplifyTemplate : public TestFixture { } // Helper function to unit test TemplateSimplifier::getTemplateParametersInDeclaration - bool getTemplateParametersInDeclarationHelper(const char code[], const std::vector & params) { + template + bool getTemplateParametersInDeclarationHelper(const char (&data)[size], const std::vector & params) { Tokenizer tokenizer(settings, *this); - std::istringstream istr(code); - if (!tokenizer.list.createTokens(istr, "test.cpp")) + if (!tokenizer.list.createTokens(data, size-1, "test.cpp")) return false; tokenizer.createLinks(); tokenizer.splitTemplateRightAngleBrackets(false); diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 44abd19da3c8..c780412251d6 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -249,11 +249,11 @@ class TestSimplifyTypedef : public TestFixture { return tokenizer.tokens()->stringifyList(nullptr, !simplify); } - std::string simplifyTypedef(const char code[]) { + template + std::string simplifyTypedef(const char (&data)[size]) { Tokenizer tokenizer(settings1, *this); - std::istringstream istr(code); - if (!tokenizer.list.createTokens(istr, Standards::Language::CPP)) + if (!tokenizer.list.createTokens(data, size-1, Standards::Language::CPP)) return ""; tokenizer.createLinks(); tokenizer.simplifyTypedef(); @@ -284,11 +284,11 @@ class TestSimplifyTypedef : public TestFixture { } - std::string simplifyTypedefC(const char code[]) { + template + std::string simplifyTypedefC(const char (&data)[size]) { Tokenizer tokenizer(settings1, *this); - std::istringstream istr(code); - if (!tokenizer.list.createTokens(istr, "file.c")) + if (!tokenizer.list.createTokens(data, size-1, "file.c")) return ""; tokenizer.createLinks(); tokenizer.simplifyTypedef(); @@ -465,23 +465,21 @@ class TestSimplifyTypedef : public TestFixture { } void carray3() { - const char* code{}; - code = "typedef int a[256];\n" // #11689 - "typedef a b[256];\n" - "b* p;\n"; + const char code[] = "typedef int a[256];\n" // #11689 + "typedef a b[256];\n" + "b* p;\n"; ASSERT_EQUALS("int ( * p ) [ 256 ] [ 256 ] ;", simplifyTypedef(code)); - code = "typedef int a[1];\n" - "typedef a b[2];\n" - "typedef b c[3];\n" - "c* p;\n"; - ASSERT_EQUALS("int ( * p ) [ 3 ] [ 2 ] [ 1 ] ;", simplifyTypedef(code)); + const char code1[] = "typedef int a[1];\n" + "typedef a b[2];\n" + "typedef b c[3];\n" + "c* p;\n"; + ASSERT_EQUALS("int ( * p ) [ 3 ] [ 2 ] [ 1 ] ;", simplifyTypedef(code1)); } void carray4() { - const char* code{}; - code = "typedef int arr[12];\n" // #12019 - "void foo() { arr temp = {0}; }\n"; + const char code[] = "typedef int arr[12];\n" // #12019 + "void foo() { arr temp = {0}; }\n"; ASSERT_EQUALS("void foo ( ) { int temp [ 12 ] = { 0 } ; }", tok(code)); } @@ -4222,8 +4220,7 @@ class TestSimplifyTypedef : public TestFixture { "void test(rFunctionPointer_fp functionPointer);"; Tokenizer tokenizer(settings1, *this); - std::istringstream istr(code); - ASSERT(tokenizer.list.createTokens(istr, "file.c")); + ASSERT(tokenizer.list.createTokens(code, "file.c")); tokenizer.createLinks(); tokenizer.simplifyTypedef(); diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index 5e31520ce965..265724027969 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -1186,7 +1186,7 @@ class TestSuppressions : public TestFixture { settings.exitCode = 1; const char code[] = "int f() { int a; return a; }"; - ASSERT_EQUALS(0, cppCheck.check("test.c", code)); // <- no unsuppressed error is seen + ASSERT_EQUALS(0, cppCheck.check("test.c", reinterpret_cast(code), sizeof(code))); // <- no unsuppressed error is seen ASSERT_EQUALS("[test.c:1]: (error) Uninitialized variable: a\n", errout_str()); // <- report error so ThreadExecutor can suppress it and make sure the global suppression is matched. } @@ -1226,7 +1226,7 @@ class TestSuppressions : public TestFixture { " // cppcheck-suppress unusedStructMember\n" " int y;\n" "};"; - ASSERT_EQUALS(0, cppCheck.check("/somewhere/test.cpp", code)); + ASSERT_EQUALS(0, cppCheck.check("/somewhere/test.cpp", reinterpret_cast(code), sizeof(code))); ASSERT_EQUALS("",errout_str()); } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index b07114d7f79b..7524506ba4bb 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -5925,11 +5925,11 @@ class TestTokenizer : public TestFixture { Z3 }; - std::string testAst(const char code[], AstStyle style = AstStyle::Simple) { + template + std::string testAst(const char (&data)[size], AstStyle style = AstStyle::Simple) { // tokenize given code.. Tokenizer tokenizer(settings0, *this); - std::istringstream istr(code); - if (!tokenizer.list.createTokens(istr,"test.cpp")) + if (!tokenizer.list.createTokens(data, size-1,"test.cpp")) return "ERROR"; tokenizer.combineStringAndCharLiterals(); diff --git a/test/testtokenlist.cpp b/test/testtokenlist.cpp index 4b11c5c37dbf..2c9d5b5b25fa 100644 --- a/test/testtokenlist.cpp +++ b/test/testtokenlist.cpp @@ -25,7 +25,6 @@ #include "token.h" #include "tokenlist.h" -#include #include #include @@ -122,8 +121,7 @@ class TestTokenList : public TestFixture { const char code2[] = "_Generic"; // C11 keyword const Settings s = settingsBuilder().c(Standards::C89).build(); TokenList tokenlist(&s); - std::istringstream istr(code2); - ASSERT(tokenlist.createTokens(istr, "a.c")); + ASSERT(tokenlist.createTokens(code2, "a.c")); ASSERT_EQUALS(false, tokenlist.front()->isKeyword()); } @@ -143,8 +141,7 @@ class TestTokenList : public TestFixture { const char code2[] = "noexcept"; // C++11 keyword const Settings s = settingsBuilder().cpp(Standards::CPP03).build(); TokenList tokenlist(&s); - std::istringstream istr(code2); - ASSERT(tokenlist.createTokens(istr, "a.cpp")); + ASSERT(tokenlist.createTokens(code2, "a.cpp")); ASSERT_EQUALS(false, tokenlist.front()->isKeyword()); } }