Skip to content

Commit 27667c1

Browse files
committed
pass a char buffer to simplecpp instead of a stream
1 parent 56f096b commit 27667c1

11 files changed

Lines changed: 56 additions & 67 deletions

lib/cppcheck.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,9 +801,8 @@ unsigned int CppCheck::checkInternal(const std::string& filename, const std::str
801801
code += "#line " + std::to_string(dir.linenr) + " \"" + dir.file + "\"\n" + dir.str + '\n';
802802
}
803803
TokenList tokenlist(&mSettings);
804-
std::istringstream istr2(code);
805804
// TODO: asserts when file has unknown extension
806-
tokenlist.createTokens(istr2, Path::identify(*files.begin(), false)); // TODO: check result?
805+
tokenlist.createTokens(code.data(), code.size(), Path::identify(*files.begin(), false)); // TODO: check result?
807806
executeRules("define", tokenlist);
808807
}
809808
#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));
@@ -1784,8 +1783,7 @@ std::shared_ptr<Token> createTokenFromExpression(const std::string& returnValue,
17841783
std::shared_ptr<TokenList> tokenList = std::make_shared<TokenList>(&settings);
17851784
{
17861785
const std::string code = "return " + returnValue + ";";
1787-
std::istringstream istr(code);
1788-
if (!tokenList->createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C))
1786+
if (!tokenList->createTokens(code.data(), code.size(), cpp ? Standards::Language::CPP : Standards::Language::C))
17891787
return nullptr;
17901788
}
17911789

lib/symboldatabase.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7557,8 +7557,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
75577557
if (!typestr.empty()) {
75587558
ValueType valuetype;
75597559
TokenList tokenList(&mSettings);
7560-
std::istringstream istr(typestr+";");
7561-
tokenList.createTokens(istr, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
7560+
const std::string str(typestr+";");
7561+
tokenList.createTokens(str.data(), str.size(), tok->isCpp() ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
75627562
tokenList.simplifyStdType();
75637563
if (parsedecl(tokenList.front(), &valuetype, mDefaultSignedness, mSettings)) {
75647564
valuetype.originalTypeName = typestr;
@@ -7647,8 +7647,8 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
76477647
continue;
76487648
}
76497649
TokenList tokenList(&mSettings);
7650-
std::istringstream istr(typestr+";");
7651-
if (tokenList.createTokens(istr, tok->isCpp() ? Standards::Language::CPP : Standards::Language::C)) {
7650+
const std::string str(typestr+";");
7651+
if (tokenList.createTokens(str.data(), str.size(), tok->isCpp() ? Standards::Language::CPP : Standards::Language::C)) {
76527652
ValueType vt;
76537653
tokenList.simplifyPlatformTypes();
76547654
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>
@@ -3885,8 +3884,7 @@ static bool isNotEqual(std::pair<const Token*, const Token*> x, std::pair<const
38853884
static bool isNotEqual(std::pair<const Token*, const Token*> x, const std::string& y, bool cpp)
38863885
{
38873886
TokenList tokenList(nullptr);
3888-
std::istringstream istr(y);
3889-
tokenList.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
3887+
tokenList.createTokens(y.data(), y.size(), cpp ? Standards::Language::CPP : Standards::Language::C); // TODO: check result?
38903888
return isNotEqual(x, std::make_pair(tokenList.front(), tokenList.back()));
38913889
}
38923890
static bool isNotEqual(std::pair<const Token*, const Token*> x, const ValueType* y, bool cpp)
@@ -9278,8 +9276,8 @@ static bool getMinMaxValues(const ValueType *vt, const Platform &platform, MathL
92789276
static bool getMinMaxValues(const std::string &typestr, const Settings &settings, bool cpp, MathLib::bigint &minvalue, MathLib::bigint &maxvalue)
92799277
{
92809278
TokenList typeTokens(&settings);
9281-
std::istringstream istr(typestr+";");
9282-
if (!typeTokens.createTokens(istr, cpp ? Standards::Language::CPP : Standards::Language::C))
9279+
const std::string str(typestr+";");
9280+
if (!typeTokens.createTokens(str.data(), str.size(), cpp ? Standards::Language::CPP : Standards::Language::C))
92839281
return false;
92849282
typeTokens.simplifyPlatformTypes();
92859283
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
@@ -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
@@ -251,11 +251,11 @@ class TestSimplifyTypedef : public TestFixture {
251251
return tokenizer.tokens()->stringifyList(nullptr, !simplify);
252252
}
253253

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

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

289289

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

293-
std::istringstream istr(code);
294-
if (!tokenizer.list.createTokens(istr, "file.c"))
294+
if (!tokenizer.list.createTokens(data, size-1, "file.c"))
295295
return "";
296296
tokenizer.createLinks();
297297
tokenizer.simplifyTypedef();
@@ -474,17 +474,16 @@ class TestSimplifyTypedef : public TestFixture {
474474
}
475475

476476
void carray3() {
477-
const char* code{};
478-
code = "typedef int a[256];\n" // #11689
479-
"typedef a b[256];\n"
480-
"b* p;\n";
477+
const char code[] = "typedef int a[256];\n" // #11689
478+
"typedef a b[256];\n"
479+
"b* p;\n";
481480
ASSERT_EQUALS("int ( * p ) [ 256 ] [ 256 ] ;", simplifyTypedef(code));
482481

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

490489
void carray4() {
@@ -4249,8 +4248,7 @@ class TestSimplifyTypedef : public TestFixture {
42494248
"void test(rFunctionPointer_fp functionPointer);";
42504249

42514250
Tokenizer tokenizer(settings1, *this);
4252-
std::istringstream istr(code);
4253-
ASSERT(tokenizer.list.createTokens(istr, "file.c"));
4251+
ASSERT(tokenizer.list.createTokens(code, "file.c"));
42544252
tokenizer.createLinks();
42554253
tokenizer.simplifyTypedef();
42564254

test/testsuppressions.cpp

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

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

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

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)