Skip to content

Commit d9961f9

Browse files
committed
pass a char buffer to simplecpp instead of a stream
1 parent 28a35a7 commit d9961f9

10 files changed

+54
-66
lines changed

lib/cppcheck.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,9 +804,8 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
804804
code += "#line " + std::to_string(dir.linenr) + " \"" + dir.file + "\"\n" + dir.str + '\n';
805805
}
806806
TokenList tokenlist(&mSettings);
807-
std::istringstream istr2(code);
808807
// TODO: asserts when file has unknown extension
809-
tokenlist.createTokens(istr2, Path::identify(*files.begin(), false)); // TODO: check result?
808+
tokenlist.createTokens(code.data(), code.size(), Path::identify(*files.begin(), false)); // TODO: check result?
810809
executeRules("define", tokenlist);
811810
}
812811
#endif

lib/library.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include <iostream>
3737
#include <list>
3838
#include <memory>
39-
#include <sstream>
4039
#include <stack>
4140
#include <stdexcept>
4241
#include <string>
@@ -56,8 +55,8 @@ static std::vector<std::string> getnames(const char *names)
5655

5756
static void gettokenlistfromvalid(const std::string& valid, bool cpp, TokenList& tokenList)
5857
{
59-
std::istringstream istr(valid + ',');
60-
tokenList.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
58+
const std::string str(valid + ',');
59+
tokenList.createTokens(str.data(), str.size(), cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
6160
for (Token *tok = tokenList.front(); tok; tok = tok->next()) {
6261
if (Token::Match(tok,"- %num%")) {
6362
tok->str("-" + tok->strAt(1));
@@ -1816,8 +1815,7 @@ std::shared_ptr<Token> createTokenFromExpression(const std::string& returnValue,
18161815
std::shared_ptr<TokenList> tokenList = std::make_shared<TokenList>(&settings);
18171816
{
18181817
const std::string code = "return " + returnValue + ";";
1819-
std::istringstream istr(code);
1820-
if (!tokenList->createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C))
1818+
if (!tokenList->createTokens(code.data(), code.size(), cpp ? Standards::Language::CPP : Standards::Language::C))
18211819
return nullptr;
18221820
}
18231821

lib/symboldatabase.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7560,8 +7560,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
75607560
if (!typestr.empty()) {
75617561
ValueType valuetype;
75627562
TokenList tokenList(&mSettings);
7563-
std::istringstream istr(typestr+";");
7564-
tokenList.createTokens(istr, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
7563+
const std::string str(typestr+";");
7564+
tokenList.createTokens(str.data(), str.size(), tok->isCpp() ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
75657565
tokenList.simplifyStdType();
75667566
if (parsedecl(tokenList.front(), &valuetype, mDefaultSignedness, mSettings)) {
75677567
valuetype.originalTypeName = typestr;
@@ -7650,8 +7650,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
76507650
continue;
76517651
}
76527652
TokenList tokenList(&mSettings);
7653-
std::istringstream istr(typestr+";");
7654-
if (tokenList.createTokens(istr, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C)) {
7653+
const std::string str(typestr+";");
7654+
if (tokenList.createTokens(str.data(), str.size(), tok->isCpp() ? Standards::Language::CPP : Standards::Language::C)) {
76557655
ValueType vt;
76567656
tokenList.simplifyPlatformTypes();
76577657
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>
@@ -2926,8 +2924,7 @@ static bool isNotEqual(std::pair<const Token*, const Token*> x, std::pair<const
29262924
static bool isNotEqual(std::pair<const Token*, const Token*> x, const std::string& y, bool cpp)
29272925
{
29282926
TokenList tokenList(nullptr);
2929-
std::istringstream istr(y);
2930-
tokenList.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
2927+
tokenList.createTokens(y.data(), y.size(), cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
29312928
return isNotEqual(x, std::make_pair(tokenList.front(), tokenList.back()));
29322929
}
29332930
static bool isNotEqual(std::pair<const Token*, const Token*> x, const ValueType* y, bool cpp)
@@ -8264,8 +8261,8 @@ static void valueFlowDynamicBufferSize(const TokenList& tokenlist, const SymbolD
82648261
static bool getMinMaxValues(const std::string &typestr, const Settings &settings, bool cpp, MathLib::bigint &minvalue, MathLib::bigint &maxvalue)
82658262
{
82668263
TokenList typeTokens(&settings);
8267-
std::istringstream istr(typestr+";");
8268-
if (!typeTokens.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C))
8264+
const std::string str(typestr+";");
8265+
if (!typeTokens.createTokens(str.data(), str.size(), cpp ? Standards::Language::CPP : Standards::Language::C))
82698266
return false;
82708267
typeTokens.simplifyPlatformTypes();
82718268
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
@@ -5985,11 +5985,11 @@ class TestTokenizer : public TestFixture {
59855985
Z3
59865986
};
59875987

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

59955995
tokenizer.combineStringAndCharLiterals();

test/testtokenlist.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "token.h"
2626
#include "tokenlist.h"
2727

28-
#include <sstream>
2928
#include <stack>
3029
#include <string>
3130
#include <utility>
@@ -126,8 +125,7 @@ class TestTokenList : public TestFixture {
126125
const char code2[] = "_Generic"; // C11 keyword
127126
const Settings s = settingsBuilder().c(Standards::C89).build();
128127
TokenList tokenlist(&s);
129-
std::istringstream istr(code2);
130-
ASSERT(tokenlist.createTokens(istr, "a.c"));
128+
ASSERT(tokenlist.createTokens(code2, "a.c"));
131129
ASSERT_EQUALS(false, tokenlist.front()->isKeyword());
132130
}
133131

@@ -147,8 +145,7 @@ class TestTokenList : public TestFixture {
147145
const char code2[] = "noexcept"; // C++11 keyword
148146
const Settings s = settingsBuilder().cpp(Standards::CPP03).build();
149147
TokenList tokenlist(&s);
150-
std::istringstream istr(code2);
151-
ASSERT(tokenlist.createTokens(istr, "a.cpp"));
148+
ASSERT(tokenlist.createTokens(code2, "a.cpp"));
152149
ASSERT_EQUALS(false, tokenlist.front()->isKeyword());
153150
}
154151
}

0 commit comments

Comments
 (0)