Skip to content

Commit 34c1e49

Browse files
committed
testrunner: removed more std::istringstream usage
1 parent c2f81a8 commit 34c1e49

14 files changed

Lines changed: 91 additions & 81 deletions

test/helpers.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,24 @@ ScopedFile::~ScopedFile() {
117117
// TODO: we should be using the actual Preprocessor implementation
118118
std::string PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const std::string &filedata, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression)
119119
{
120-
std::map<std::string, std::string> cfgcode = getcode(settings, errorlogger, filedata.c_str(), std::set<std::string>{cfg}, filename, inlineSuppression);
120+
std::map<std::string, std::string> cfgcode = getcode(settings, errorlogger, filedata.c_str(), filedata.size(), std::set<std::string>{cfg}, filename, inlineSuppression);
121121
const auto it = cfgcode.find(cfg);
122122
if (it == cfgcode.end())
123123
return "";
124124
return it->second;
125125
}
126126

127-
std::map<std::string, std::string> PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], const std::string &filename, SuppressionList *inlineSuppression)
127+
std::map<std::string, std::string> PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], std::size_t size, const std::string &filename, SuppressionList *inlineSuppression)
128128
{
129-
return getcode(settings, errorlogger, code, {}, filename, inlineSuppression);
129+
return getcode(settings, errorlogger, code, size, {}, filename, inlineSuppression);
130130
}
131131

132-
std::map<std::string, std::string> PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], std::set<std::string> cfgs, const std::string &filename, SuppressionList *inlineSuppression)
132+
std::map<std::string, std::string> PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], std::size_t size, std::set<std::string> cfgs, const std::string &filename, SuppressionList *inlineSuppression)
133133
{
134134
simplecpp::OutputList outputList;
135135
std::vector<std::string> files;
136136

137-
std::istringstream istr(code);
138-
simplecpp::TokenList tokens(istr, files, Path::simplifyPath(filename), &outputList);
137+
simplecpp::TokenList tokens(code, size, files, Path::simplifyPath(filename), &outputList);
139138
Preprocessor preprocessor(settings, errorlogger, Path::identify(tokens.getFiles()[0], false));
140139
if (inlineSuppression)
141140
preprocessor.inlineSuppressions(tokens, *inlineSuppression);
@@ -162,11 +161,9 @@ std::map<std::string, std::string> PreprocessorHelper::getcode(const Settings& s
162161
return cfgcode;
163162
}
164163

165-
void SimpleTokenizer2::preprocess(const char code[], std::vector<std::string> &files, const std::string& file0, Tokenizer& tokenizer, ErrorLogger& errorlogger)
164+
void SimpleTokenizer2::preprocess(const char* code, std::size_t size, std::vector<std::string> &files, const std::string& file0, Tokenizer& tokenizer, ErrorLogger& errorlogger)
166165
{
167-
// TODO: get rid of stream
168-
std::istringstream istr(code);
169-
const simplecpp::TokenList tokens1(istr, files, file0);
166+
const simplecpp::TokenList tokens1(code, size, files, file0);
170167

171168
Preprocessor preprocessor(tokenizer.getSettings(), errorlogger, Path::identify(tokens1.getFiles()[0], false));
172169
simplecpp::TokenList tokens2 = preprocessor.preprocess(tokens1, "", files, true);

test/helpers.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,15 @@ class PreprocessorHelper
177177
* @param inlineSuppression the inline suppressions
178178
*/
179179
static std::string getcode(const Settings& settings, ErrorLogger& errorlogger, const std::string &filedata, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression = nullptr);
180-
static std::map<std::string, std::string> getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr);
180+
template<size_t size>
181+
static std::map<std::string, std::string> getcode(const Settings& settings, ErrorLogger& errorlogger, const char (&code)[size], const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr)
182+
{
183+
return getcode(settings, errorlogger, code, size-1, filename, inlineSuppression);
184+
}
181185

182186
private:
183-
static std::map<std::string, std::string> getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], std::set<std::string> cfgs, const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr);
187+
static std::map<std::string, std::string> getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], std::size_t size, const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr);
188+
static std::map<std::string, std::string> getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], std::size_t size, std::set<std::string> cfgs, const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr);
184189
};
185190

186191
namespace cppcheck {
@@ -254,18 +259,18 @@ class SimpleTokenizer2 : public Tokenizer {
254259
SimpleTokenizer2(const Settings &settings, ErrorLogger &errorlogger, const char (&code)[size], const std::string& file0)
255260
: Tokenizer{TokenList{settings, Path::identify(file0, false)}, errorlogger}
256261
{
257-
preprocess(code, mFiles, file0, *this, errorlogger);
262+
preprocess(code, size-1, mFiles, file0, *this, errorlogger);
258263
}
259264

260265
// TODO: get rid of this
261-
SimpleTokenizer2(const Settings &settings, ErrorLogger &errorlogger, const char code[], const std::string& file0)
266+
SimpleTokenizer2(const Settings &settings, ErrorLogger &errorlogger, const char* code, std::size_t size, const std::string& file0)
262267
: Tokenizer{TokenList{settings, Path::identify(file0, false)}, errorlogger}
263268
{
264-
preprocess(code, mFiles, file0, *this, errorlogger);
269+
preprocess(code, size, mFiles, file0, *this, errorlogger);
265270
}
266271

267272
private:
268-
static void preprocess(const char code[], std::vector<std::string> &files, const std::string& file0, Tokenizer& tokenizer, ErrorLogger& errorlogger);
273+
static void preprocess(const char* code, std::size_t size, std::vector<std::string> &files, const std::string& file0, Tokenizer& tokenizer, ErrorLogger& errorlogger);
269274

270275
std::vector<std::string> mFiles;
271276
};

test/testbufferoverrun.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ class TestBufferOverrun : public TestFixture {
7070
}
7171

7272
#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
73-
void checkP_(const char* file, int line, const char code[])
73+
template<size_t size>
74+
void checkP_(const char* file, int line, const char (&code)[size])
7475
{
7576
const Settings settings = settingsBuilder(settings0).severity(Severity::performance).certainty(Certainty::inconclusive).build();
7677

test/testclass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8778,7 +8778,8 @@ class TestClass : public TestFixture {
87788778
}
87798779

87808780
#define checkUselessOverride(...) checkUselessOverride_(__FILE__, __LINE__, __VA_ARGS__)
8781-
void checkUselessOverride_(const char* file, int line, const char code[]) {
8781+
template<size_t size>
8782+
void checkUselessOverride_(const char* file, int line, const char (&code)[size]) {
87828783
const Settings settings = settingsBuilder().severity(Severity::style).build();
87838784

87848785
SimpleTokenizer2 tokenizer(settings, *this, code, "test.cpp");

test/testcondition.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ class TestCondition : public TestFixture {
136136
};
137137

138138
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
139-
void check_(const char* file, int line, const char code[], const CheckOptions& options = make_default_obj()) {
139+
template<size_t size>
140+
void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) {
140141
const Settings settings = settingsBuilder(options.s ? *options.s : settings0).certainty(Certainty::inconclusive, options.inconclusive).build();
141142

142143
SimpleTokenizer2 tokenizer(settings, *this, code, options.cpp ? "test.cpp" : "test.c");
@@ -149,7 +150,8 @@ class TestCondition : public TestFixture {
149150
}
150151

151152
#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
152-
void checkP_(const char* file, int line, const char code[])
153+
template<size_t size>
154+
void checkP_(const char* file, int line, const char (&code)[size])
153155
{
154156
const Settings settings = settingsBuilder(settings0).severity(Severity::performance).certainty(Certainty::inconclusive).build();
155157

test/testincompletestatement.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class TestIncompleteStatement : public TestFixture {
3939
};
4040

4141
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
42-
void check_(const char* file, int line, const char code[], const CheckOptions& options = make_default_obj()) {
42+
template<size_t size>
43+
void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) {
4344
const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, options.inconclusive).build();
4445

4546
SimpleTokenizer2 tokenizer(settings1, *this, code, options.cpp ? "test.cpp" : "test.c");

test/testleakautovar.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3229,7 +3229,8 @@ class TestLeakAutoVarRecursiveCountLimit : public TestFixture {
32293229
const Settings settings = settingsBuilder().library("std.cfg").checkLibrary().build();
32303230

32313231
#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
3232-
void checkP_(const char* file, int line, const char code[], bool cpp = false) {
3232+
template<size_t size>
3233+
void checkP_(const char* file, int line, const char (&code)[size], bool cpp = false) {
32333234
SimpleTokenizer2 tokenizer(settings, *this, code, cpp?"test.cpp":"test.c");
32343235

32353236
// Tokenizer..

test/testpreprocessor.cpp

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ class TestPreprocessor : public TestFixture {
5050
TestPreprocessor() : TestFixture("TestPreprocessor") {}
5151

5252
private:
53-
std::string expandMacros(const char code[], ErrorLogger &errorLogger) const {
54-
std::istringstream istr(code);
53+
template<size_t size>
54+
std::string expandMacros(const char (&code)[size], ErrorLogger &errorLogger) {
5555
simplecpp::OutputList outputList;
5656
std::vector<std::string> files;
57-
const simplecpp::TokenList tokens1 = simplecpp::TokenList(istr, files, "file.cpp", &outputList);
57+
const simplecpp::TokenList tokens1 = simplecpp::TokenList(code, size-1, files, "file.cpp", &outputList);
5858
Preprocessor p(settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false));
5959
simplecpp::TokenList tokens2 = p.preprocess(tokens1, "", files, true);
6060
p.reportOutput(outputList, true);
@@ -301,16 +301,16 @@ class TestPreprocessor : public TestFixture {
301301
TEST_CASE(standard);
302302
}
303303

304-
std::string getConfigsStr(const char filedata[], const char *arg = nullptr) {
304+
template<size_t size>
305+
std::string getConfigsStr(const char (&code)[size], const char *arg = nullptr) {
305306
Settings settings;
306307
if (arg && std::strncmp(arg,"-D",2)==0)
307308
settings.userDefines = arg + 2;
308309
if (arg && std::strncmp(arg,"-U",2)==0)
309310
settings.userUndefs.insert(arg+2);
310311
std::vector<std::string> files;
311-
std::istringstream istr(filedata);
312312
// TODO: this adds an empty filename
313-
simplecpp::TokenList tokens(istr,files);
313+
simplecpp::TokenList tokens(code,size-1,files);
314314
tokens.removeComments();
315315
Preprocessor preprocessor(settings, *this, Standards::Language::C); // TODO: do we need to consider #file?
316316
const std::set<std::string> configs = preprocessor.getConfigs(tokens);
@@ -320,11 +320,11 @@ class TestPreprocessor : public TestFixture {
320320
return ret;
321321
}
322322

323-
std::size_t getHash(const char filedata[]) {
323+
template<size_t size>
324+
std::size_t getHash(const char (&code)[size]) {
324325
std::vector<std::string> files;
325-
std::istringstream istr(filedata);
326326
// TODO: this adds an empty filename
327-
simplecpp::TokenList tokens(istr,files);
327+
simplecpp::TokenList tokens(code,size-1,files);
328328
tokens.removeComments();
329329
Preprocessor preprocessor(settingsDefault, *this, Standards::Language::C); // TODO: do we need to consider #file?
330330
return preprocessor.calculateHash(tokens, "");
@@ -478,9 +478,8 @@ class TestPreprocessor : public TestFixture {
478478
"#else\n"
479479
"2\n"
480480
"#endif\n";
481-
std::istringstream istr(filedata);
482481
std::vector<std::string> files;
483-
simplecpp::TokenList tokens(istr, files, "test.c");
482+
simplecpp::TokenList tokens(filedata, sizeof(filedata), files, "test.c");
484483

485484
// preprocess code with unix32 platform..
486485
{
@@ -803,47 +802,47 @@ class TestPreprocessor : public TestFixture {
803802
}
804803

805804
void if_macro_eq_macro() {
806-
const char *code = "#define A B\n"
807-
"#define B 1\n"
808-
"#define C 1\n"
809-
"#if A == C\n"
810-
"Wilma\n"
811-
"#else\n"
812-
"Betty\n"
813-
"#endif\n";
805+
const char code[] = "#define A B\n"
806+
"#define B 1\n"
807+
"#define C 1\n"
808+
"#if A == C\n"
809+
"Wilma\n"
810+
"#else\n"
811+
"Betty\n"
812+
"#endif\n";
814813
ASSERT_EQUALS("\n", getConfigsStr(code));
815814
}
816815

817816
void ticket_3675() {
818-
const char* code = "#ifdef YYSTACKSIZE\n"
819-
"#define YYMAXDEPTH YYSTACKSIZE\n"
820-
"#else\n"
821-
"#define YYSTACKSIZE YYMAXDEPTH\n"
822-
"#endif\n"
823-
"#if YYDEBUG\n"
824-
"#endif\n";
817+
const char code[] = "#ifdef YYSTACKSIZE\n"
818+
"#define YYMAXDEPTH YYSTACKSIZE\n"
819+
"#else\n"
820+
"#define YYSTACKSIZE YYMAXDEPTH\n"
821+
"#endif\n"
822+
"#if YYDEBUG\n"
823+
"#endif\n";
825824
(void)PreprocessorHelper::getcode(settings0, *this, code);
826825

827826
// There's nothing to assert. It just needs to not hang.
828827
}
829828

830829
void ticket_3699() {
831-
const char* code = "#define INLINE __forceinline\n"
832-
"#define inline __forceinline\n"
833-
"#define __forceinline inline\n"
834-
"#if !defined(_WIN32)\n"
835-
"#endif\n"
836-
"INLINE inline __forceinline\n";
830+
const char code[] = "#define INLINE __forceinline\n"
831+
"#define inline __forceinline\n"
832+
"#define __forceinline inline\n"
833+
"#if !defined(_WIN32)\n"
834+
"#endif\n"
835+
"INLINE inline __forceinline\n";
837836
const std::map<std::string, std::string> actual = PreprocessorHelper::getcode(settings0, *this, code);
838837

839838
// First, it must not hang. Second, inline must becomes inline, and __forceinline must become __forceinline.
840839
ASSERT_EQUALS("\n\n\n\n\n$__forceinline $inline $__forceinline", actual.at(""));
841840
}
842841

843842
void ticket_4922() { // #4922
844-
const char* code = "__asm__ \n"
845-
"{ int extern __value) 0; (double return (\"\" } extern\n"
846-
"__typeof __finite (__finite) __finite __inline \"__GI___finite\");";
843+
const char code[] = "__asm__ \n"
844+
"{ int extern __value) 0; (double return (\"\" } extern\n"
845+
"__typeof __finite (__finite) __finite __inline \"__GI___finite\");";
847846
(void)PreprocessorHelper::getcode(settings0, *this, code);
848847
}
849848

@@ -2286,12 +2285,12 @@ class TestPreprocessor : public TestFixture {
22862285
}
22872286

22882287
void if_sizeof() { // #4071
2289-
static const char* code = "#if sizeof(unsigned short) == 2\n"
2290-
"Fred & Wilma\n"
2291-
"#elif sizeof(unsigned short) == 4\n"
2292-
"Fred & Wilma\n"
2293-
"#else\n"
2294-
"#endif";
2288+
const char code[] = "#if sizeof(unsigned short) == 2\n"
2289+
"Fred & Wilma\n"
2290+
"#elif sizeof(unsigned short) == 4\n"
2291+
"Fred & Wilma\n"
2292+
"#else\n"
2293+
"#endif";
22952294

22962295
const std::map<std::string, std::string> actual = PreprocessorHelper::getcode(settings0, *this, code);
22972296
ASSERT_EQUALS("\nFred & Wilma", actual.at(""));

test/testsimplifytypedef.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "tokenlist.h"
2727

2828
#include <cstddef>
29-
#include <sstream>
3029
#include <string>
3130
#include <utility>
3231

@@ -289,8 +288,8 @@ class TestSimplifyTypedef : public TestFixture {
289288
return tokenizer.tokens()->stringifyList(nullptr, false);
290289
}
291290

292-
293-
std::string simplifyTypedefP(const char code[]) {
291+
template<size_t size>
292+
std::string simplifyTypedefP(const char (&code)[size]) {
294293
SimpleTokenizer2 tokenizer(settings0, *this, code, "test.cpp");
295294

296295
// Tokenize..
@@ -331,7 +330,7 @@ class TestSimplifyTypedef : public TestFixture {
331330
template<size_t size>
332331
std::string dumpTypedefInfo(const char (&code)[size]) {
333332
TokenList tokenlist{settings1, Standards::Language::C};
334-
if (!TokenListHelper::createTokens(tokenlist, code, "file.c"))
333+
if (!TokenListHelper::createTokens(tokenlist, code, size-1, "file.c"))
335334
return {};
336335
Tokenizer tokenizer(std::move(tokenlist), *this);
337336
tokenizer.createLinks();

test/teststring.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ class TestString : public TestFixture {
6767
};
6868

6969
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
70-
void check_(const char* file, int line, const char code[], const CheckOptions& options = make_default_obj()) {
70+
template<size_t size>
71+
void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) {
7172
SimpleTokenizer2 tokenizer(settings, *this, code, options.cpp ? "test.cpp" : "test.c");
7273

7374
// Tokenize..

0 commit comments

Comments
 (0)