Skip to content

Commit 4aac30c

Browse files
committed
pass a char buffer to simplecpp instead of a stream
1 parent d59e664 commit 4aac30c

11 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
@@ -819,9 +819,8 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
819819
code += "#line " + std::to_string(dir.linenr) + " \"" + dir.file + "\"\n" + dir.str + '\n';
820820
}
821821
TokenList tokenlist(&mSettings);
822-
std::istringstream istr2(code);
823822
// TODO: asserts when file has unknown extension
824-
tokenlist.createTokens(istr2, Path::identify(*files.begin(), false)); // TODO: check result?
823+
tokenlist.createTokens(code.data(), code.size(), Path::identify(*files.begin(), false)); // TODO: check result?
825824
executeRules("define", tokenlist);
826825
}
827826
#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
@@ -1772,8 +1772,7 @@ static std::shared_ptr<Token> createTokenFromExpression(const std::string& retur
17721772
std::shared_ptr<TokenList> tokenList = std::make_shared<TokenList>(&settings);
17731773
{
17741774
const std::string code = "return " + returnValue + ";";
1775-
std::istringstream istr(code);
1776-
if (!tokenList->createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C))
1775+
if (!tokenList->createTokens(code.data(), code.size(), cpp ? Standards::Language::CPP : Standards::Language::C))
17771776
return nullptr;
17781777
}
17791778

lib/symboldatabase.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7562,8 +7562,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
75627562
if (!typestr.empty()) {
75637563
ValueType valuetype;
75647564
TokenList tokenList(&mSettings);
7565-
std::istringstream istr(typestr+";");
7566-
tokenList.createTokens(istr, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
7565+
const std::string str(typestr+";");
7566+
tokenList.createTokens(str.data(), str.size(), tok->isCpp() ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
75677567
tokenList.simplifyStdType();
75687568
if (parsedecl(tokenList.front(), &valuetype, mDefaultSignedness, mSettings)) {
75697569
valuetype.originalTypeName = typestr;
@@ -7652,8 +7652,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
76527652
continue;
76537653
}
76547654
TokenList tokenList(&mSettings);
7655-
std::istringstream istr(typestr+";");
7656-
if (tokenList.createTokens(istr, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C)) {
7655+
const std::string str(typestr+";");
7656+
if (tokenList.createTokens(str.data(), str.size(), tok->isCpp() ? Standards::Language::CPP : Standards::Language::C)) {
76577657
ValueType vt;
76587658
tokenList.simplifyPlatformTypes();
76597659
tokenList.simplifyStdType();

lib/valueflow.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@
123123
#include <memory>
124124
#include <numeric>
125125
#include <set>
126-
#include <sstream>
127-
#include <stdexcept>
128126
#include <string>
129127
#include <type_traits>
130128
#include <unordered_map>
@@ -2911,8 +2909,7 @@ static bool isNotEqual(std::pair<const Token*, const Token*> x, std::pair<const
29112909
static bool isNotEqual(std::pair<const Token*, const Token*> x, const std::string& y, bool cpp)
29122910
{
29132911
TokenList tokenList(nullptr);
2914-
std::istringstream istr(y);
2915-
tokenList.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
2912+
tokenList.createTokens(y.data(), y.size(), cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
29162913
return isNotEqual(x, std::make_pair(tokenList.front(), tokenList.back()));
29172914
}
29182915
static bool isNotEqual(std::pair<const Token*, const Token*> x, const ValueType* y, bool cpp)
@@ -8258,8 +8255,8 @@ static void valueFlowDynamicBufferSize(const TokenList& tokenlist, const SymbolD
82588255
static bool getMinMaxValues(const std::string &typestr, const Settings &settings, bool cpp, MathLib::bigint &minvalue, MathLib::bigint &maxvalue)
82598256
{
82608257
TokenList typeTokens(&settings);
8261-
std::istringstream istr(typestr+";");
8262-
if (!typeTokens.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C))
8258+
const std::string str(typestr+";");
8259+
if (!typeTokens.createTokens(str.data(), str.size(), cpp ? Standards::Language::CPP : Standards::Language::C))
82638260
return false;
82648261
typeTokens.simplifyPlatformTypes();
82658262
typeTokens.simplifyStdType();

test/helpers.h

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

test/testlibrary.cpp

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

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

@@ -176,35 +176,35 @@ class TestLibrary : public TestFixture {
176176

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

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

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

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

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
@@ -253,11 +253,11 @@ class TestSimplifyTypedef : public TestFixture {
253253
return tokenizer.tokens()->stringifyList(nullptr, !simplify);
254254
}
255255

256-
std::string simplifyTypedef(const char code[]) {
256+
template<size_t size>
257+
std::string simplifyTypedef(const char (&data)[size]) {
257258
Tokenizer tokenizer(settings1, *this);
258259

259-
std::istringstream istr(code);
260-
if (!tokenizer.list.createTokens(istr, Standards::Language::CPP))
260+
if (!tokenizer.list.createTokens(data, size-1, Standards::Language::CPP))
261261
return "";
262262
tokenizer.createLinks();
263263
tokenizer.simplifyTypedef();
@@ -289,11 +289,11 @@ class TestSimplifyTypedef : public TestFixture {
289289
}
290290

291291

292-
std::string simplifyTypedefC(const char code[]) {
292+
template<size_t size>
293+
std::string simplifyTypedefC(const char (&data)[size]) {
293294
Tokenizer tokenizer(settings1, *this);
294295

295-
std::istringstream istr(code);
296-
if (!tokenizer.list.createTokens(istr, "file.c"))
296+
if (!tokenizer.list.createTokens(data, size-1, "file.c"))
297297
return "";
298298
tokenizer.createLinks();
299299
tokenizer.simplifyTypedef();
@@ -476,17 +476,16 @@ class TestSimplifyTypedef : public TestFixture {
476476
}
477477

478478
void carray3() {
479-
const char* code{};
480-
code = "typedef int a[256];\n" // #11689
481-
"typedef a b[256];\n"
482-
"b* p;\n";
479+
const char code[] = "typedef int a[256];\n" // #11689
480+
"typedef a b[256];\n"
481+
"b* p;\n";
483482
ASSERT_EQUALS("int ( * p ) [ 256 ] [ 256 ] ;", simplifyTypedef(code));
484483

485-
code = "typedef int a[1];\n"
486-
"typedef a b[2];\n"
487-
"typedef b c[3];\n"
488-
"c* p;\n";
489-
ASSERT_EQUALS("int ( * p ) [ 3 ] [ 2 ] [ 1 ] ;", simplifyTypedef(code));
484+
const char code1[] = "typedef int a[1];\n"
485+
"typedef a b[2];\n"
486+
"typedef b c[3];\n"
487+
"c* p;\n";
488+
ASSERT_EQUALS("int ( * p ) [ 3 ] [ 2 ] [ 1 ] ;", simplifyTypedef(code1));
490489
}
491490

492491
void carray4() {
@@ -4268,8 +4267,7 @@ class TestSimplifyTypedef : public TestFixture {
42684267
"void test(rFunctionPointer_fp functionPointer);";
42694268

42704269
Tokenizer tokenizer(settings1, *this);
4271-
std::istringstream istr(code);
4272-
ASSERT(tokenizer.list.createTokens(istr, "file.c"));
4270+
ASSERT(tokenizer.list.createTokens(code, "file.c"));
42734271
tokenizer.createLinks();
42744272
tokenizer.simplifyTypedef();
42754273

test/testtokenize.cpp

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

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

59935993
tokenizer.combineStringAndCharLiterals();

0 commit comments

Comments
 (0)