Skip to content

Commit 7c0d543

Browse files
committed
pass a char buffer to simplecpp instead of a stream
1 parent 6bcecd7 commit 7c0d543

11 files changed

Lines changed: 67 additions & 74 deletions

lib/cppcheck.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,8 +1088,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
10881088
code += "#line " + std::to_string(dir.linenr) + " \"" + dir.file + "\"\n" + dir.str + '\n';
10891089
}
10901090
TokenList tokenlist(mSettings, file.lang());
1091-
std::istringstream istr2(code);
1092-
tokenlist.createTokens(istr2); // TODO: check result?
1091+
tokenlist.createTokens(code.data(), code.size()); // TODO: check result?
10931092
executeRules("define", tokenlist);
10941093
}
10951094
#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>
@@ -178,8 +177,8 @@ static std::vector<std::string> getnames(const char *names)
178177

179178
static void gettokenlistfromvalid(const std::string& valid, TokenList& tokenList)
180179
{
181-
std::istringstream istr(valid + ',');
182-
tokenList.createTokens(istr); // TODO: check result?
180+
const std::string str(valid + ',');
181+
tokenList.createTokens(str.data(), str.size()); // TODO: check result?
183182
for (Token *tok = tokenList.front(); tok; tok = tok->next()) {
184183
if (Token::Match(tok,"- %num%")) {
185184
tok->str("-" + tok->strAt(1));

lib/programmemory.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,8 +1815,7 @@ static std::shared_ptr<Token> createTokenFromExpression(const std::string& retur
18151815
std::shared_ptr<TokenList> tokenList = std::make_shared<TokenList>(settings, cpp ? Standards::Language::CPP : Standards::Language::C);
18161816
{
18171817
const std::string code = "return " + returnValue + ";";
1818-
std::istringstream istr(code);
1819-
if (!tokenList->createTokens(istr))
1818+
if (!tokenList->createTokens(code.data(), code.size()))
18201819
return nullptr;
18211820
}
18221821

lib/symboldatabase.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7750,8 +7750,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
77507750
if (!typestr.empty()) {
77517751
ValueType valuetype;
77527752
TokenList tokenList(mSettings, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C);
7753-
std::istringstream istr(typestr+";");
7754-
tokenList.createTokens(istr); // TODO: check result?
7753+
const std::string str(typestr+";");
7754+
tokenList.createTokens(str.data(), str.size()); // TODO: check result?
77557755
tokenList.simplifyStdType();
77567756
if (parsedecl(tokenList.front(), &valuetype, mDefaultSignedness, mSettings)) {
77577757
valuetype.originalTypeName = typestr;
@@ -7840,8 +7840,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
78407840
continue;
78417841
}
78427842
TokenList tokenList(mSettings, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C);
7843-
std::istringstream istr(typestr+";");
7844-
if (tokenList.createTokens(istr)) {
7843+
const std::string str(typestr+";");
7844+
if (tokenList.createTokens(str.data(), str.size())) {
78457845
ValueType vt;
78467846
tokenList.simplifyPlatformTypes();
78477847
tokenList.simplifyStdType();

lib/valueflow.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@
124124
#include <memory>
125125
#include <numeric>
126126
#include <set>
127-
#include <sstream>
128127
#include <string>
129128
#include <unordered_map>
130129
#include <unordered_set>
@@ -1992,8 +1991,7 @@ static bool isNotEqual(std::pair<const Token*, const Token*> x, std::pair<const
19921991
static bool isNotEqual(std::pair<const Token*, const Token*> x, const std::string& y, bool cpp, const Settings& settings)
19931992
{
19941993
TokenList tokenList(settings, cpp ? Standards::Language::CPP : Standards::Language::C);
1995-
std::istringstream istr(y);
1996-
tokenList.createTokens(istr); // TODO: check result?
1994+
tokenList.createTokens(y.data(), y.size()); // TODO: check result?
19971995
return isNotEqual(x, std::make_pair(tokenList.front(), tokenList.back()));
19981996
}
19991997
static bool isNotEqual(std::pair<const Token*, const Token*> x, const ValueType* y, bool cpp, const Settings& settings)
@@ -7108,8 +7106,8 @@ static bool getMinMaxValues(const std::string& typestr,
71087106
MathLib::bigint& maxvalue)
71097107
{
71107108
TokenList typeTokens(settings, cpp ? Standards::Language::CPP : Standards::Language::C);
7111-
std::istringstream istr(typestr + ";");
7112-
if (!typeTokens.createTokens(istr))
7109+
const std::string str(typestr + ";");
7110+
if (!typeTokens.createTokens(str.data(), str.size()))
71137111
return false;
71147112
typeTokens.simplifyPlatformTypes();
71157113
typeTokens.simplifyStdType();

test/helpers.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ class SimpleTokenList
110110
explicit SimpleTokenList(const char (&code)[size], const std::string& file0, Standards::Language lang = Standards::Language::CPP)
111111
: list{settings, lang}
112112
{
113-
std::istringstream iss(code);
114113
list.appendFileIfNew(file0);
115-
if (!list.createTokens(iss))
114+
if (!list.createTokens(code, size-1))
116115
throw std::runtime_error("creating tokens failed");
117116
}
118117

@@ -268,12 +267,18 @@ class SimpleTokenizer2 : public Tokenizer {
268267

269268
struct TokenListHelper
270269
{
271-
static bool createTokens(TokenList& tokenlist, std::istream& istr, const std::string& file)
270+
template<size_t size>
271+
static bool createTokens(TokenList& tokenlist, const char (&code)[size], const std::string& file)
272+
{
273+
return createTokens(tokenlist, code, size-1, file);
274+
}
275+
276+
static bool createTokens(TokenList& tokenlist, const char* data, size_t size, const std::string& file)
272277
{
273278
if (tokenlist.front())
274279
throw std::runtime_error("token list is not empty");
275280
tokenlist.appendFileIfNew(file);
276-
return tokenlist.createTokens(istr);
281+
return tokenlist.createTokens(data, size);
277282
}
278283
};
279284

test/testlibrary.cpp

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

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

@@ -178,17 +178,17 @@ class TestLibrary : public TestFixture {
178178

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

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

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

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

test/testsimplifytemplate.cpp

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

5442-
unsigned int templateParameters(const char code[]) {
5442+
template<size_t size>
5443+
unsigned int templateParameters(const char (&data)[size]) {
54435444
TokenList tokenlist{settings, Standards::Language::CPP};
5444-
std::istringstream istr(code);
54455445
tokenlist.appendFileIfNew("test.cpp");
5446-
if (!tokenlist.createTokens(istr))
5446+
if (!tokenlist.createTokens(data, size-1))
54475447
return false;
54485448
Tokenizer tokenizer(std::move(tokenlist), *this);
54495449
tokenizer.createLinks();
@@ -5508,12 +5508,12 @@ class TestSimplifyTemplate : public TestFixture {
55085508
}
55095509

55105510
// Helper function to unit test TemplateSimplifier::getTemplateNamePosition
5511-
int templateNamePositionHelper(const char code[], unsigned offset = 0) {
5511+
template<size_t size>
5512+
int templateNamePositionHelper(const char (&data)[size], unsigned offset = 0) {
55125513
TokenList tokenlist{settings, Standards::Language::CPP};
55135514

5514-
std::istringstream istr(code);
55155515
tokenlist.appendFileIfNew("test.cpp");
5516-
if (!tokenlist.createTokens(istr))
5516+
if (!tokenlist.createTokens(data, size-1))
55175517
return false;
55185518
Tokenizer tokenizer(std::move(tokenlist), *this);
55195519
tokenizer.createLinks();
@@ -5581,10 +5581,10 @@ class TestSimplifyTemplate : public TestFixture {
55815581
}
55825582

55835583
// Helper function to unit test TemplateSimplifier::findTemplateDeclarationEnd
5584-
bool findTemplateDeclarationEndHelper(const char code[], const char pattern[], unsigned offset = 0) {
5584+
template<size_t size>
5585+
bool findTemplateDeclarationEndHelper(const char (&data)[size], const char pattern[], unsigned offset = 0) {
55855586
TokenList tokenlist{settings, Standards::Language::CPP};
5586-
std::istringstream istr(code);
5587-
if (!TokenListHelper::createTokens(tokenlist, istr, "test.cpp"))
5587+
if (!TokenListHelper::createTokens(tokenlist, data, size-1, "test.cpp"))
55885588
return false;
55895589
Tokenizer tokenizer(std::move(tokenlist), *this);
55905590
tokenizer.createLinks();
@@ -5611,11 +5611,11 @@ class TestSimplifyTemplate : public TestFixture {
56115611
}
56125612

56135613
// Helper function to unit test TemplateSimplifier::getTemplateParametersInDeclaration
5614-
bool getTemplateParametersInDeclarationHelper(const char code[], const std::vector<std::string> & params) {
5614+
template<size_t size>
5615+
bool getTemplateParametersInDeclarationHelper(const char (&data)[size], const std::vector<std::string> & params) {
56155616
TokenList tokenlist{settings, Standards::Language::CPP};
56165617

5617-
std::istringstream istr(code);
5618-
if (!TokenListHelper::createTokens(tokenlist, istr, "test.cpp"))
5618+
if (!TokenListHelper::createTokens(tokenlist, data, size-1, "test.cpp"))
56195619
return false;
56205620
Tokenizer tokenizer(std::move(tokenlist), *this);
56215621
tokenizer.createLinks();

test/testsimplifytypedef.cpp

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,10 @@ class TestSimplifyTypedef : public TestFixture {
277277
return tokenizer.tokens()->stringifyList(nullptr, !options.simplify);
278278
}
279279

280-
std::string simplifyTypedef(const char code[]) {
280+
template<size_t size>
281+
std::string simplifyTypedef(const char (&data)[size]) {
281282
TokenList tokenlist{settings1, Standards::Language::CPP};
282-
std::istringstream istr(code);
283-
if (!tokenlist.createTokens(istr))
283+
if (!tokenlist.createTokens(data, size-1))
284284
return "";
285285
Tokenizer tokenizer(std::move(tokenlist), *this);
286286
tokenizer.createLinks();
@@ -311,11 +311,11 @@ class TestSimplifyTypedef : public TestFixture {
311311
}
312312

313313

314-
std::string simplifyTypedefC(const char code[]) {
314+
template<size_t size>
315+
std::string simplifyTypedefC(const char (&data)[size]) {
315316
TokenList tokenlist{settings1, Standards::Language::C};
316317

317-
std::istringstream istr(code);
318-
if (!TokenListHelper::createTokens(tokenlist, istr, "file.c"))
318+
if (!TokenListHelper::createTokens(tokenlist, data, size-1, "file.c"))
319319
return "";
320320
Tokenizer tokenizer(std::move(tokenlist), *this);
321321
tokenizer.createLinks();
@@ -328,10 +328,10 @@ class TestSimplifyTypedef : public TestFixture {
328328
return tokenizer.tokens()->stringifyList(nullptr, false);
329329
}
330330

331-
std::string dumpTypedefInfo(const char code[]) {
331+
template<size_t size>
332+
std::string dumpTypedefInfo(const char (&code)[size]) {
332333
TokenList tokenlist{settings1, Standards::Language::C};
333-
std::istringstream istr(code);
334-
if (!TokenListHelper::createTokens(tokenlist, istr, "file.c"))
334+
if (!TokenListHelper::createTokens(tokenlist, code, "file.c"))
335335
return {};
336336
Tokenizer tokenizer(std::move(tokenlist), *this);
337337
tokenizer.createLinks();
@@ -515,17 +515,16 @@ class TestSimplifyTypedef : public TestFixture {
515515
}
516516

517517
void carray3() {
518-
const char* code{};
519-
code = "typedef int a[256];\n" // #11689
520-
"typedef a b[256];\n"
521-
"b* p;\n";
518+
const char code[] = "typedef int a[256];\n" // #11689
519+
"typedef a b[256];\n"
520+
"b* p;\n";
522521
ASSERT_EQUALS("int ( * p ) [ 256 ] [ 256 ] ;", simplifyTypedef(code));
523522

524-
code = "typedef int a[1];\n"
525-
"typedef a b[2];\n"
526-
"typedef b c[3];\n"
527-
"c* p;\n";
528-
ASSERT_EQUALS("int ( * p ) [ 3 ] [ 2 ] [ 1 ] ;", simplifyTypedef(code));
523+
const char code1[] = "typedef int a[1];\n"
524+
"typedef a b[2];\n"
525+
"typedef b c[3];\n"
526+
"c* p;\n";
527+
ASSERT_EQUALS("int ( * p ) [ 3 ] [ 2 ] [ 1 ] ;", simplifyTypedef(code1));
529528
}
530529

531530
void carray4() {
@@ -4459,8 +4458,7 @@ class TestSimplifyTypedef : public TestFixture {
44594458
"void test(rFunctionPointer_fp functionPointer);";
44604459

44614460
TokenList tokenlist{settings1, Standards::Language::C};
4462-
std::istringstream istr(code);
4463-
ASSERT(TokenListHelper::createTokens(tokenlist, istr, "file.c"));
4461+
ASSERT(TokenListHelper::createTokens(tokenlist, code, "file.c"));
44644462
Tokenizer tokenizer(std::move(tokenlist), *this);
44654463
tokenizer.createLinks();
44664464
tokenizer.simplifyTypedef();
@@ -4502,8 +4500,7 @@ class TestSimplifyTypedef : public TestFixture {
45024500
"}";
45034501

45044502
TokenList tokenlist{settings1, Standards::Language::C};
4505-
std::istringstream istr(code);
4506-
ASSERT(TokenListHelper::createTokens(tokenlist, istr, "file.c"));
4503+
ASSERT(TokenListHelper::createTokens(tokenlist, code, "file.c"));
45074504
Tokenizer tokenizer(std::move(tokenlist), *this);
45084505
tokenizer.createLinks();
45094506
tokenizer.simplifyTypedef();
@@ -4521,8 +4518,7 @@ class TestSimplifyTypedef : public TestFixture {
45214518
"}";
45224519

45234520
TokenList tokenlist{settings1, Standards::Language::C};
4524-
std::istringstream istr(code);
4525-
ASSERT(TokenListHelper::createTokens(tokenlist, istr, "file.c"));
4521+
ASSERT(TokenListHelper::createTokens(tokenlist, code, "file.c"));
45264522
Tokenizer tokenizer(std::move(tokenlist), *this);
45274523
tokenizer.createLinks();
45284524
tokenizer.simplifyTypedef();

test/testtokenize.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6204,12 +6204,12 @@ class TestTokenizer : public TestFixture {
62046204
Z3
62056205
};
62066206

6207-
std::string testAst(const char code[], AstStyle style = AstStyle::Simple) {
6207+
template<size_t size>
6208+
std::string testAst(const char (&data)[size], AstStyle style = AstStyle::Simple) {
62086209
// tokenize given code..
62096210
TokenList tokenlist{settings0, Standards::Language::CPP};
6210-
std::istringstream istr(code);
62116211
tokenlist.appendFileIfNew("test.cpp");
6212-
if (!tokenlist.createTokens(istr))
6212+
if (!tokenlist.createTokens(data, size-1))
62136213
return "ERROR";
62146214

62156215
Tokenizer tokenizer(std::move(tokenlist), *this);

0 commit comments

Comments
 (0)