Skip to content

Commit b3784d3

Browse files
committed
pass a char buffer to simplecpp instead of a stream
1 parent 0fed4b2 commit b3784d3

12 files changed

Lines changed: 54 additions & 66 deletions

lib/cppcheck.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,9 +821,8 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
821821
code += "#line " + std::to_string(dir.linenr) + " \"" + dir.file + "\"\n" + dir.str + '\n';
822822
}
823823
TokenList tokenlist(&mSettings);
824-
std::istringstream istr2(code);
825824
// TODO: asserts when file has unknown extension
826-
tokenlist.createTokens(istr2, Path::identify(*files.begin(), false)); // TODO: check result?
825+
tokenlist.createTokens(code.data(), code.size(), Path::identify(*files.begin(), false)); // TODO: check result?
827826
executeRules("define", tokenlist);
828827
}
829828
#endif

lib/library.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <iostream>
3636
#include <list>
3737
#include <memory>
38-
#include <sstream>
3938
#include <stdexcept>
4039
#include <string>
4140
#include <unordered_set>
@@ -172,8 +171,8 @@ static std::vector<std::string> getnames(const char *names)
172171

173172
static void gettokenlistfromvalid(const std::string& valid, bool cpp, TokenList& tokenList)
174173
{
175-
std::istringstream istr(valid + ',');
176-
tokenList.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
174+
const std::string str(valid + ',');
175+
tokenList.createTokens(str.data(), str.size(), cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
177176
for (Token *tok = tokenList.front(); tok; tok = tok->next()) {
178177
if (Token::Match(tok,"- %num%")) {
179178
tok->str("-" + tok->strAt(1));

lib/programmemory.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,8 +1791,7 @@ static std::shared_ptr<Token> createTokenFromExpression(const std::string& retur
17911791
std::shared_ptr<TokenList> tokenList = std::make_shared<TokenList>(&settings);
17921792
{
17931793
const std::string code = "return " + returnValue + ";";
1794-
std::istringstream istr(code);
1795-
if (!tokenList->createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C))
1794+
if (!tokenList->createTokens(code.data(), code.size(), cpp ? Standards::Language::CPP : Standards::Language::C))
17961795
return nullptr;
17971796
}
17981797

lib/symboldatabase.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7629,8 +7629,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
76297629
if (!typestr.empty()) {
76307630
ValueType valuetype;
76317631
TokenList tokenList(&mSettings);
7632-
std::istringstream istr(typestr+";");
7633-
tokenList.createTokens(istr, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
7632+
const std::string str(typestr+";");
7633+
tokenList.createTokens(str.data(), str.size(), tok->isCpp() ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
76347634
tokenList.simplifyStdType();
76357635
if (parsedecl(tokenList.front(), &valuetype, mDefaultSignedness, mSettings)) {
76367636
valuetype.originalTypeName = typestr;
@@ -7719,8 +7719,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
77197719
continue;
77207720
}
77217721
TokenList tokenList(&mSettings);
7722-
std::istringstream istr(typestr+";");
7723-
if (tokenList.createTokens(istr, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C)) {
7722+
const std::string str(typestr+";");
7723+
if (tokenList.createTokens(str.data(), str.size(), tok->isCpp() ? Standards::Language::CPP : Standards::Language::C)) {
77247724
ValueType vt;
77257725
tokenList.simplifyPlatformTypes();
77267726
tokenList.simplifyStdType();

lib/valueflow.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@
123123
#include <memory>
124124
#include <numeric>
125125
#include <set>
126-
#include <sstream>
127126
#include <string>
128127
#include <unordered_map>
129128
#include <unordered_set>
@@ -1047,8 +1046,7 @@ static bool isNotEqual(std::pair<const Token*, const Token*> x, std::pair<const
10471046
static bool isNotEqual(std::pair<const Token*, const Token*> x, const std::string& y, bool cpp)
10481047
{
10491048
TokenList tokenList(nullptr);
1050-
std::istringstream istr(y);
1051-
tokenList.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
1049+
tokenList.createTokens(y.data(), y.size(), cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
10521050
return isNotEqual(x, std::make_pair(tokenList.front(), tokenList.back()));
10531051
}
10541052
static bool isNotEqual(std::pair<const Token*, const Token*> x, const ValueType* y, bool cpp)

lib/vf_common.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <cstddef>
3636
#include <exception>
3737
#include <limits>
38-
#include <sstream>
3938
#include <utility>
4039
#include <vector>
4140

@@ -99,8 +98,8 @@ namespace ValueFlow
9998
bool getMinMaxValues(const std::string &typestr, const Settings &settings, bool cpp, MathLib::bigint &minvalue, MathLib::bigint &maxvalue)
10099
{
101100
TokenList typeTokens(&settings);
102-
std::istringstream istr(typestr+";");
103-
if (!typeTokens.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C))
101+
const std::string str(typestr+";");
102+
if (!typeTokens.createTokens(str.data(), str.size(), cpp ? Standards::Language::CPP : Standards::Language::C))
104103
return false;
105104
typeTokens.simplifyPlatformTypes();
106105
typeTokens.simplifyStdType();

test/helpers.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ class SimpleTokenList
112112
template<size_t size>
113113
explicit SimpleTokenList(const char (&code)[size], Standards::Language lang = Standards::Language::CPP)
114114
{
115-
std::istringstream iss(code);
116-
if (!list.createTokens(iss, lang))
115+
if (!list.createTokens(code, size-1, lang))
117116
throw std::runtime_error("creating tokens failed");
118117
}
119118

test/testlibrary.cpp

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

155155
TokenList tokenList(&settings);
156-
std::istringstream istr("foo();"); // <- too few arguments, not library function
157-
ASSERT(tokenList.createTokens(istr, Standards::Language::CPP));
156+
const char code[] = "foo();"; // <- too few arguments, not library function
157+
ASSERT(tokenList.createTokens(code, Standards::Language::CPP));
158158
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
159159
tokenList.createAst();
160160

@@ -177,17 +177,17 @@ class TestLibrary : public TestFixture {
177177

178178
{
179179
TokenList tokenList(&settings);
180-
std::istringstream istr("foo();"); // <- too few arguments, not library function
181-
ASSERT(tokenList.createTokens(istr, Standards::Language::CPP));
180+
const char code[] = "foo();"; // <- too few arguments, not library function
181+
ASSERT(tokenList.createTokens(code, Standards::Language::CPP));
182182
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
183183
tokenList.createAst();
184184

185185
ASSERT(library.isNotLibraryFunction(tokenList.front()));
186186
}
187187
{
188188
TokenList tokenList(&settings);
189-
std::istringstream istr("foo(a);"); // <- library function
190-
ASSERT(tokenList.createTokens(istr, Standards::Language::CPP));
189+
const char code[] = "foo(a);"; // <- library function
190+
ASSERT(tokenList.createTokens(code, Standards::Language::CPP));
191191
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
192192
tokenList.createAst();
193193

@@ -197,8 +197,8 @@ class TestLibrary : public TestFixture {
197197
}
198198
{
199199
TokenList tokenList(&settings);
200-
std::istringstream istr("foo(a, b);"); // <- library function
201-
ASSERT(tokenList.createTokens(istr, Standards::Language::CPP));
200+
const char code[] = "foo(a, b);"; // <- library function
201+
ASSERT(tokenList.createTokens(code, Standards::Language::CPP));
202202
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
203203
tokenList.createAst();
204204

@@ -208,8 +208,8 @@ class TestLibrary : public TestFixture {
208208
}
209209
{
210210
TokenList tokenList(&settings);
211-
std::istringstream istr("foo(a, b, c);"); // <- too much arguments, not library function
212-
ASSERT(tokenList.createTokens(istr, Standards::Language::CPP));
211+
const char code[] = "foo(a, b, c);"; // <- too much arguments, not library function
212+
ASSERT(tokenList.createTokens(code, Standards::Language::CPP));
213213
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
214214
tokenList.createAst();
215215

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
@@ -260,11 +260,11 @@ class TestSimplifyTypedef : public TestFixture {
260260
return tokenizer.tokens()->stringifyList(nullptr, !simplify);
261261
}
262262

263-
std::string simplifyTypedef(const char code[]) {
263+
template<size_t size>
264+
std::string simplifyTypedef(const char (&data)[size]) {
264265
Tokenizer tokenizer(settings1, *this);
265266

266-
std::istringstream istr(code);
267-
if (!tokenizer.list.createTokens(istr, Standards::Language::CPP))
267+
if (!tokenizer.list.createTokens(data, size-1, Standards::Language::CPP))
268268
return "";
269269
tokenizer.createLinks();
270270
tokenizer.simplifyTypedef();
@@ -296,11 +296,11 @@ class TestSimplifyTypedef : public TestFixture {
296296
}
297297

298298

299-
std::string simplifyTypedefC(const char code[]) {
299+
template<size_t size>
300+
std::string simplifyTypedefC(const char (&data)[size]) {
300301
Tokenizer tokenizer(settings1, *this);
301302

302-
std::istringstream istr(code);
303-
if (!tokenizer.list.createTokens(istr, "file.c"))
303+
if (!tokenizer.list.createTokens(data, size-1, "file.c"))
304304
return "";
305305
tokenizer.createLinks();
306306
tokenizer.simplifyTypedef();
@@ -499,17 +499,16 @@ class TestSimplifyTypedef : public TestFixture {
499499
}
500500

501501
void carray3() {
502-
const char* code{};
503-
code = "typedef int a[256];\n" // #11689
504-
"typedef a b[256];\n"
505-
"b* p;\n";
502+
const char code[] = "typedef int a[256];\n" // #11689
503+
"typedef a b[256];\n"
504+
"b* p;\n";
506505
ASSERT_EQUALS("int ( * p ) [ 256 ] [ 256 ] ;", simplifyTypedef(code));
507506

508-
code = "typedef int a[1];\n"
509-
"typedef a b[2];\n"
510-
"typedef b c[3];\n"
511-
"c* p;\n";
512-
ASSERT_EQUALS("int ( * p ) [ 3 ] [ 2 ] [ 1 ] ;", simplifyTypedef(code));
507+
const char code1[] = "typedef int a[1];\n"
508+
"typedef a b[2];\n"
509+
"typedef b c[3];\n"
510+
"c* p;\n";
511+
ASSERT_EQUALS("int ( * p ) [ 3 ] [ 2 ] [ 1 ] ;", simplifyTypedef(code1));
513512
}
514513

515514
void carray4() {
@@ -4404,8 +4403,7 @@ class TestSimplifyTypedef : public TestFixture {
44044403
"void test(rFunctionPointer_fp functionPointer);";
44054404

44064405
Tokenizer tokenizer(settings1, *this);
4407-
std::istringstream istr(code);
4408-
ASSERT(tokenizer.list.createTokens(istr, "file.c"));
4406+
ASSERT(tokenizer.list.createTokens(code, "file.c"));
44094407
tokenizer.createLinks();
44104408
tokenizer.simplifyTypedef();
44114409

0 commit comments

Comments
 (0)