Skip to content

Commit 83b450a

Browse files
committed
pass a char buffer to simplecpp instead of a stream
1 parent ac5cf8a commit 83b450a

File tree

11 files changed

+56
-66
lines changed

11 files changed

+56
-66
lines changed

lib/cppcheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ unsigned int CppCheck::checkInternal(const std::string& filename, const std::str
802802
TokenList tokenlist(&mSettings);
803803
std::istringstream istr2(code);
804804
// TODO: asserts when file has unknown extension
805-
tokenlist.createTokens(istr2, Path::identify(*files.begin())); // TODO: check result?
805+
tokenlist.createTokens(code.data(), code.size(), Path::identify(*files.begin())); // TODO: check result?
806806
executeRules("define", tokenlist);
807807
}
808808
#endif

lib/library.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <cstring>
3636
#include <list>
3737
#include <memory>
38-
#include <sstream>
3938
#include <stack>
4039
#include <stdexcept>
4140
#include <string>
@@ -55,8 +54,8 @@ static std::vector<std::string> getnames(const char *names)
5554

5655
static void gettokenlistfromvalid(const std::string& valid, bool cpp, TokenList& tokenList)
5756
{
58-
std::istringstream istr(valid + ',');
59-
tokenList.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
57+
const std::string str(valid + ',');
58+
tokenList.createTokens(str.data(), str.size(), cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
6059
for (Token *tok = tokenList.front(); tok; tok = tok->next()) {
6160
if (Token::Match(tok,"- %num%")) {
6261
tok->str("-" + tok->strAt(1));
@@ -1769,8 +1768,7 @@ std::shared_ptr<Token> createTokenFromExpression(const std::string& returnValue,
17691768
std::shared_ptr<TokenList> tokenList = std::make_shared<TokenList>(&settings);
17701769
{
17711770
const std::string code = "return " + returnValue + ";";
1772-
std::istringstream istr(code);
1773-
if (!tokenList->createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C))
1771+
if (!tokenList->createTokens(code.data(), code.size(), cpp ? Standards::Language::CPP : Standards::Language::C))
17741772
return nullptr;
17751773
}
17761774

lib/symboldatabase.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7558,8 +7558,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
75587558
if (!typestr.empty()) {
75597559
ValueType valuetype;
75607560
TokenList tokenList(&mSettings);
7561-
std::istringstream istr(typestr+";");
7562-
tokenList.createTokens(istr, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
7561+
const std::string str(typestr+";");
7562+
tokenList.createTokens(str.data(), str.size(), tok->isCpp() ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
75637563
tokenList.simplifyStdType();
75647564
if (parsedecl(tokenList.front(), &valuetype, mDefaultSignedness, mSettings)) {
75657565
valuetype.originalTypeName = typestr;
@@ -7648,8 +7648,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
76487648
continue;
76497649
}
76507650
TokenList tokenList(&mSettings);
7651-
std::istringstream istr(typestr+";");
7652-
if (tokenList.createTokens(istr, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C)) {
7651+
const std::string str(typestr+";");
7652+
if (tokenList.createTokens(str.data(), str.size(), tok->isCpp() ? Standards::Language::CPP : Standards::Language::C)) {
76537653
ValueType vt;
76547654
tokenList.simplifyPlatformTypes();
76557655
tokenList.simplifyStdType();

lib/valueflow.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@
121121
#include <memory>
122122
#include <numeric>
123123
#include <set>
124-
#include <sstream>
125124
#include <string>
126125
#include <type_traits>
127126
#include <unordered_map>
@@ -3871,8 +3870,7 @@ static bool isNotEqual(std::pair<const Token*, const Token*> x, std::pair<const
38713870
static bool isNotEqual(std::pair<const Token*, const Token*> x, const std::string& y, bool cpp)
38723871
{
38733872
TokenList tokenList(nullptr);
3874-
std::istringstream istr(y);
3875-
tokenList.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
3873+
tokenList.createTokens(y.data(), y.size(), cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
38763874
return isNotEqual(x, std::make_pair(tokenList.front(), tokenList.back()));
38773875
}
38783876
static bool isNotEqual(std::pair<const Token*, const Token*> x, const ValueType* y, bool cpp)
@@ -9250,8 +9248,8 @@ static bool getMinMaxValues(const ValueType *vt, const Platform &platform, MathL
92509248
static bool getMinMaxValues(const std::string &typestr, const Settings &settings, bool cpp, MathLib::bigint &minvalue, MathLib::bigint &maxvalue)
92519249
{
92529250
TokenList typeTokens(&settings);
9253-
std::istringstream istr(typestr+";");
9254-
if (!typeTokens.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C))
9251+
const std::string str(typestr+";");
9252+
if (!typeTokens.createTokens(str.data(), str.size(), cpp ? Standards::Language::CPP : Standards::Language::C))
92559253
return false;
92569254
typeTokens.simplifyPlatformTypes();
92579255
typeTokens.simplifyStdType();

test/helpers.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ class SimpleTokenList
107107
template<size_t size>
108108
explicit SimpleTokenList(const char (&code)[size], Standards::Language lang = Standards::Language::CPP)
109109
{
110-
std::istringstream iss(code);
111-
if (!list.createTokens(iss, lang))
110+
if (!list.createTokens(code, size-1, lang))
112111
throw std::runtime_error("creating tokens failed");
113112
}
114113

test/testlibrary.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ class TestLibrary : public TestFixture {
160160
"</def>";
161161

162162
TokenList tokenList(&settings);
163-
std::istringstream istr("foo();"); // <- too few arguments, not library function
164-
ASSERT(tokenList.createTokens(istr, Standards::Language::CPP));
163+
const char code[] = "foo();"; // <- too few arguments, not library function
164+
ASSERT(tokenList.createTokens(code, Standards::Language::CPP));
165165
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
166166
tokenList.createAst();
167167

@@ -184,35 +184,35 @@ class TestLibrary : public TestFixture {
184184

185185
{
186186
TokenList tokenList(&settings);
187-
std::istringstream istr("foo();"); // <- too few arguments, not library function
188-
ASSERT(tokenList.createTokens(istr, Standards::Language::CPP));
187+
const char code[] = "foo();"; // <- too few arguments, not library function
188+
ASSERT(tokenList.createTokens(code, Standards::Language::CPP));
189189
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
190190
tokenList.createAst();
191191

192192
ASSERT(library.isNotLibraryFunction(tokenList.front()));
193193
}
194194
{
195195
TokenList tokenList(&settings);
196-
std::istringstream istr("foo(a);"); // <- library function
197-
ASSERT(tokenList.createTokens(istr, Standards::Language::CPP));
196+
const char code[] = "foo(a);"; // <- library function
197+
ASSERT(tokenList.createTokens(code, Standards::Language::CPP));
198198
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
199199
tokenList.createAst();
200200

201201
ASSERT(!library.isNotLibraryFunction(tokenList.front()));
202202
}
203203
{
204204
TokenList tokenList(&settings);
205-
std::istringstream istr("foo(a, b);"); // <- library function
206-
ASSERT(tokenList.createTokens(istr, Standards::Language::CPP));
205+
const char code[] = "foo(a, b);"; // <- library function
206+
ASSERT(tokenList.createTokens(code, Standards::Language::CPP));
207207
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
208208
tokenList.createAst();
209209

210210
ASSERT(!library.isNotLibraryFunction(tokenList.front()));
211211
}
212212
{
213213
TokenList tokenList(&settings);
214-
std::istringstream istr("foo(a, b, c);"); // <- too much arguments, not library function
215-
ASSERT(tokenList.createTokens(istr, Standards::Language::CPP));
214+
const char code[] = "foo(a, b, c);"; // <- too much arguments, not library function
215+
ASSERT(tokenList.createTokens(code, Standards::Language::CPP));
216216
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
217217
tokenList.createAst();
218218

test/testsimplifytemplate.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5302,11 +5302,11 @@ class TestSimplifyTemplate : public TestFixture {
53025302
"C<B<int>> y;"));
53035303
}
53045304

5305-
unsigned int templateParameters(const char code[]) {
5305+
template<size_t size>
5306+
unsigned int templateParameters(const char (&data)[size]) {
53065307
Tokenizer tokenizer(settings, *this);
53075308

5308-
std::istringstream istr(code);
5309-
if (!tokenizer.list.createTokens(istr, "test.cpp"))
5309+
if (!tokenizer.list.createTokens(data, size-1, "test.cpp"))
53105310
return false;
53115311
tokenizer.createLinks();
53125312
tokenizer.splitTemplateRightAngleBrackets(false);
@@ -5370,11 +5370,11 @@ class TestSimplifyTemplate : public TestFixture {
53705370
}
53715371

53725372
// Helper function to unit test TemplateSimplifier::getTemplateNamePosition
5373-
int templateNamePositionHelper(const char code[], unsigned offset = 0) {
5373+
template<size_t size>
5374+
int templateNamePositionHelper(const char (&data)[size], unsigned offset = 0) {
53745375
Tokenizer tokenizer(settings, *this);
53755376

5376-
std::istringstream istr(code);
5377-
if (!tokenizer.list.createTokens(istr, "test.cpp"))
5377+
if (!tokenizer.list.createTokens(data, size-1, "test.cpp"))
53785378
return false;
53795379
tokenizer.createLinks();
53805380
tokenizer.splitTemplateRightAngleBrackets(false);
@@ -5441,11 +5441,11 @@ class TestSimplifyTemplate : public TestFixture {
54415441
}
54425442

54435443
// Helper function to unit test TemplateSimplifier::findTemplateDeclarationEnd
5444-
bool findTemplateDeclarationEndHelper(const char code[], const char pattern[], unsigned offset = 0) {
5444+
template<size_t size>
5445+
bool findTemplateDeclarationEndHelper(const char (&data)[size], const char pattern[], unsigned offset = 0) {
54455446
Tokenizer tokenizer(settings, *this);
54465447

5447-
std::istringstream istr(code);
5448-
if (!tokenizer.list.createTokens(istr, "test.cpp"))
5448+
if (!tokenizer.list.createTokens(data, size-1, "test.cpp"))
54495449
return false;
54505450
tokenizer.createLinks();
54515451
tokenizer.splitTemplateRightAngleBrackets(false);
@@ -5471,11 +5471,11 @@ class TestSimplifyTemplate : public TestFixture {
54715471
}
54725472

54735473
// Helper function to unit test TemplateSimplifier::getTemplateParametersInDeclaration
5474-
bool getTemplateParametersInDeclarationHelper(const char code[], const std::vector<std::string> & params) {
5474+
template<size_t size>
5475+
bool getTemplateParametersInDeclarationHelper(const char (&data)[size], const std::vector<std::string> & params) {
54755476
Tokenizer tokenizer(settings, *this);
54765477

5477-
std::istringstream istr(code);
5478-
if (!tokenizer.list.createTokens(istr, "test.cpp"))
5478+
if (!tokenizer.list.createTokens(data, size-1, "test.cpp"))
54795479
return false;
54805480
tokenizer.createLinks();
54815481
tokenizer.splitTemplateRightAngleBrackets(false);

test/testsimplifytypedef.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,11 @@ class TestSimplifyTypedef : public TestFixture {
250250
return tokenizer.tokens()->stringifyList(nullptr, !simplify);
251251
}
252252

253-
std::string simplifyTypedef(const char code[]) {
253+
template<size_t size>
254+
std::string simplifyTypedef(const char (&data)[size]) {
254255
Tokenizer tokenizer(settings1, *this);
255256

256-
std::istringstream istr(code);
257-
if (!tokenizer.list.createTokens(istr, Standards::Language::CPP))
257+
if (!tokenizer.list.createTokens(data, size-1, Standards::Language::CPP))
258258
return "";
259259
tokenizer.createLinks();
260260
tokenizer.simplifyTypedef();
@@ -286,11 +286,11 @@ class TestSimplifyTypedef : public TestFixture {
286286
}
287287

288288

289-
std::string simplifyTypedefC(const char code[]) {
289+
template<size_t size>
290+
std::string simplifyTypedefC(const char (&data)[size]) {
290291
Tokenizer tokenizer(settings1, *this);
291292

292-
std::istringstream istr(code);
293-
if (!tokenizer.list.createTokens(istr, "file.c"))
293+
if (!tokenizer.list.createTokens(data, size-1, "file.c"))
294294
return "";
295295
tokenizer.createLinks();
296296
tokenizer.simplifyTypedef();
@@ -467,17 +467,16 @@ class TestSimplifyTypedef : public TestFixture {
467467
}
468468

469469
void carray3() {
470-
const char* code{};
471-
code = "typedef int a[256];\n" // #11689
472-
"typedef a b[256];\n"
473-
"b* p;\n";
470+
const char code[] = "typedef int a[256];\n" // #11689
471+
"typedef a b[256];\n"
472+
"b* p;\n";
474473
ASSERT_EQUALS("int ( * p ) [ 256 ] [ 256 ] ;", simplifyTypedef(code));
475474

476-
code = "typedef int a[1];\n"
477-
"typedef a b[2];\n"
478-
"typedef b c[3];\n"
479-
"c* p;\n";
480-
ASSERT_EQUALS("int ( * p ) [ 3 ] [ 2 ] [ 1 ] ;", simplifyTypedef(code));
475+
const char code1[] = "typedef int a[1];\n"
476+
"typedef a b[2];\n"
477+
"typedef b c[3];\n"
478+
"c* p;\n";
479+
ASSERT_EQUALS("int ( * p ) [ 3 ] [ 2 ] [ 1 ] ;", simplifyTypedef(code1));
481480
}
482481

483482
void carray4() {
@@ -4242,8 +4241,7 @@ class TestSimplifyTypedef : public TestFixture {
42424241
"void test(rFunctionPointer_fp functionPointer);";
42434242

42444243
Tokenizer tokenizer(settings1, *this);
4245-
std::istringstream istr(code);
4246-
ASSERT(tokenizer.list.createTokens(istr, "file.c"));
4244+
ASSERT(tokenizer.list.createTokens(code, "file.c"));
42474245
tokenizer.createLinks();
42484246
tokenizer.simplifyTypedef();
42494247

test/testsuppressions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ class TestSuppressions : public TestFixture {
11861186
settings.exitCode = 1;
11871187

11881188
const char code[] = "int f() { int a; return a; }";
1189-
ASSERT_EQUALS(0, cppCheck.check("test.c", code)); // <- no unsuppressed error is seen
1189+
ASSERT_EQUALS(0, cppCheck.check("test.c", reinterpret_cast<const uint8_t*>(code), sizeof(code))); // <- no unsuppressed error is seen
11901190
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.
11911191
}
11921192

@@ -1226,7 +1226,7 @@ class TestSuppressions : public TestFixture {
12261226
" // cppcheck-suppress unusedStructMember\n"
12271227
" int y;\n"
12281228
"};";
1229-
ASSERT_EQUALS(0, cppCheck.check("/somewhere/test.cpp", code));
1229+
ASSERT_EQUALS(0, cppCheck.check("/somewhere/test.cpp", reinterpret_cast<const uint8_t*>(code), sizeof(code)));
12301230
ASSERT_EQUALS("",errout_str());
12311231
}
12321232

test/testtokenize.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5982,11 +5982,11 @@ class TestTokenizer : public TestFixture {
59825982
Z3
59835983
};
59845984

5985-
std::string testAst(const char code[], AstStyle style = AstStyle::Simple) {
5985+
template<size_t size>
5986+
std::string testAst(const char (&data)[size], AstStyle style = AstStyle::Simple) {
59865987
// tokenize given code..
59875988
Tokenizer tokenizer(settings0, *this);
5988-
std::istringstream istr(code);
5989-
if (!tokenizer.list.createTokens(istr,"test.cpp"))
5989+
if (!tokenizer.list.createTokens(data, size-1,"test.cpp"))
59905990
return "ERROR";
59915991

59925992
tokenizer.combineStringAndCharLiterals();

0 commit comments

Comments
 (0)