Skip to content

Commit cb2981d

Browse files
committed
testrunner: removed more std::istringstream usage
1 parent a09c467 commit cb2981d

16 files changed

+87
-64
lines changed

test/helpers.cpp

Lines changed: 11 additions & 14 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);
140139
if (inlineSuppression)
141140
preprocessor.inlineSuppressions(tokens, *inlineSuppression);
@@ -163,15 +162,14 @@ std::map<std::string, std::string> PreprocessorHelper::getcode(const Settings& s
163162
return cfgcode;
164163
}
165164

166-
void PreprocessorHelper::preprocess(const char code[], std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger)
165+
void PreprocessorHelper::preprocess(const char code[], std::size_t size, std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger)
167166
{
168-
preprocess(code, files, tokenizer, errorlogger, simplecpp::DUI());
167+
preprocess(code, size, files, tokenizer, errorlogger, simplecpp::DUI());
169168
}
170169

171-
void PreprocessorHelper::preprocess(const char code[], std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger, const simplecpp::DUI& dui)
170+
void PreprocessorHelper::preprocess(const char code[], std::size_t size, std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger, const simplecpp::DUI& dui)
172171
{
173-
std::istringstream istr(code);
174-
const simplecpp::TokenList tokens1(istr, files, files[0]);
172+
const simplecpp::TokenList tokens1(code, size, files, files[0]);
175173

176174
// Preprocess..
177175
simplecpp::TokenList tokens2(files);
@@ -186,11 +184,10 @@ void PreprocessorHelper::preprocess(const char code[], std::vector<std::string>
186184
tokenizer.setDirectives(std::move(directives));
187185
}
188186

189-
std::vector<RemarkComment> PreprocessorHelper::getRemarkComments(const char code[], ErrorLogger& errorLogger)
187+
std::vector<RemarkComment> PreprocessorHelper::getRemarkComments(const char code[], std::size_t size, ErrorLogger& errorLogger)
190188
{
191189
std::vector<std::string> files{"test.cpp"};
192-
std::istringstream istr(code);
193-
const simplecpp::TokenList tokens1(istr, files, files[0]);
190+
const simplecpp::TokenList tokens1(code, size, files, files[0]);
194191

195192
const Settings settings;
196193

test/helpers.h

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,38 @@ class PreprocessorHelper
163163
* @param inlineSuppression the inline suppressions
164164
*/
165165
static std::string getcode(const Settings& settings, ErrorLogger& errorlogger, const std::string &filedata, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression = nullptr);
166-
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);
166+
template<size_t size>
167+
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)
168+
{
169+
return getcode(settings, errorlogger, code, size-1, filename, inlineSuppression);
170+
}
167171

168-
static void preprocess(const char code[], std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger);
169-
static void preprocess(const char code[], std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger, const simplecpp::DUI& dui);
172+
template<size_t size>
173+
static void preprocess(const char (&code)[size], std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger)
174+
{
175+
preprocess(code, size-1, files, tokenizer, errorlogger);
176+
}
177+
template<size_t size>
178+
static void preprocess(const char (&code)[size], std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger, const simplecpp::DUI& dui)
179+
{
180+
preprocess(code, size-1, files, tokenizer, errorlogger, dui);
181+
}
170182

171183
/** get remark comments */
172-
static std::vector<RemarkComment> getRemarkComments(const char code[], ErrorLogger& errorLogger);
184+
template<size_t size>
185+
static std::vector<RemarkComment> getRemarkComments(const char (&code)[size], ErrorLogger& errorLogger)
186+
{
187+
return getRemarkComments(code, size-1, errorLogger);
188+
}
173189

174190
private:
175-
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);
191+
static void preprocess(const char code[], std::size_t size, std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger);
192+
static void preprocess(const char code[], std::size_t size, std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger, const simplecpp::DUI& dui);
193+
194+
static std::vector<RemarkComment> getRemarkComments(const char code[], std::size_t size, ErrorLogger& errorLogger);
195+
196+
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);
197+
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);
176198
};
177199

178200
namespace cppcheck {

test/testbufferoverrun.cpp

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

7777
#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
78-
void checkP_(const char* file, int line, const char code[], const char* filename = "test.cpp")
78+
template<size_t size>
79+
void checkP_(const char* file, int line, const char (&code)[size], const char* filename = "test.cpp")
7980
{
8081
const Settings settings = settingsBuilder(settings0).severity(Severity::performance).certainty(Certainty::inconclusive).build();
8182

test/testclass.cpp

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

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

85498550
std::vector<std::string> files(1, "test.cpp");

test/testcondition.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ class TestCondition : public TestFixture {
126126
}
127127

128128
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
129-
void check_(const char* file, int line, const char code[], const Settings &settings, const char* filename = "test.cpp") {
129+
template<size_t size>
130+
void check_(const char* file, int line, const char (&code)[size], const Settings &settings, const char* filename = "test.cpp") {
130131
std::vector<std::string> files(1, filename);
131132
Tokenizer tokenizer(settings, *this);
132133
PreprocessorHelper::preprocess(code, files, tokenizer, *this);
@@ -138,7 +139,8 @@ class TestCondition : public TestFixture {
138139
runChecks<CheckCondition>(tokenizer, this);
139140
}
140141

141-
void check_(const char* file, int line, const char code[], const char* filename = "test.cpp", bool inconclusive = false) {
142+
template<size_t size>
143+
void check_(const char* file, int line, const char (&code)[size], const char* filename = "test.cpp", bool inconclusive = false) {
142144
const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive, inconclusive).build();
143145
check_(file, line, code, settings, filename);
144146
}

test/testincompletestatement.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class TestIncompleteStatement : public TestFixture {
3434
const Settings settings = settingsBuilder().severity(Severity::warning).build();
3535

3636
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
37-
void check_(const char* file, int line, const char code[], bool inconclusive = false, bool cpp = true) {
37+
template<size_t size>
38+
void check_(const char* file, int line, const char (&code)[size], bool inconclusive = false, bool cpp = true) {
3839
const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, inconclusive).build();
3940

4041
std::vector<std::string> files(1, cpp ? "test.cpp" : "test.c");

test/testleakautovar.cpp

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

30913091
#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
3092-
void checkP_(const char* file, int line, const char code[], bool cpp = false) {
3092+
template<size_t size>
3093+
void checkP_(const char* file, int line, const char (&code)[size], bool cpp = false) {
30933094
std::vector<std::string> files(1, cpp?"test.cpp":"test.c");
30943095
Tokenizer tokenizer(settings, *this);
30953096
PreprocessorHelper::preprocess(code, files, tokenizer, *this);

test/testpreprocessor.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include <list>
3434
#include <map>
3535
#include <set>
36-
#include <sstream>
3736
#include <string>
3837
#include <vector>
3938

@@ -48,11 +47,11 @@ class TestPreprocessor : public TestFixture {
4847
class OurPreprocessor : public Preprocessor {
4948
public:
5049

51-
static std::string expandMacros(const char code[], ErrorLogger *errorLogger = nullptr) {
52-
std::istringstream istr(code);
50+
template<size_t size>
51+
static std::string expandMacros(const char (&code)[size], ErrorLogger *errorLogger = nullptr) {
5352
simplecpp::OutputList outputList;
5453
std::vector<std::string> files;
55-
const simplecpp::TokenList tokens1 = simplecpp::TokenList(istr, files, "file.cpp", &outputList);
54+
const simplecpp::TokenList tokens1 = simplecpp::TokenList(code, size-1, files, "file.cpp", &outputList);
5655
std::map<std::string, simplecpp::TokenList*> filedata;
5756
simplecpp::TokenList tokens2(files);
5857
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI(), &outputList);
@@ -268,16 +267,16 @@ class TestPreprocessor : public TestFixture {
268267
TEST_CASE(hashCalculation);
269268
}
270269

271-
std::string getConfigsStr(const char filedata[], const char *arg = nullptr) {
270+
template<size_t size>
271+
std::string getConfigsStr(const char (&code)[size], const char *arg = nullptr) {
272272
Settings settings;
273273
if (arg && std::strncmp(arg,"-D",2)==0)
274274
settings.userDefines = arg + 2;
275275
if (arg && std::strncmp(arg,"-U",2)==0)
276276
settings.userUndefs.insert(arg+2);
277277
Preprocessor preprocessor(settings, *this);
278278
std::vector<std::string> files;
279-
std::istringstream istr(filedata);
280-
simplecpp::TokenList tokens(istr,files);
279+
simplecpp::TokenList tokens(code,size-1,files);
281280
tokens.removeComments();
282281
const std::set<std::string> configs = preprocessor.getConfigs(tokens);
283282
std::string ret;
@@ -286,12 +285,12 @@ class TestPreprocessor : public TestFixture {
286285
return ret;
287286
}
288287

289-
std::size_t getHash(const char filedata[]) {
288+
template<size_t size>
289+
std::size_t getHash(const char (&code)[size]) {
290290
Settings settings;
291291
Preprocessor preprocessor(settings, *this);
292292
std::vector<std::string> files;
293-
std::istringstream istr(filedata);
294-
simplecpp::TokenList tokens(istr,files);
293+
simplecpp::TokenList tokens(code,size-1,files);
295294
tokens.removeComments();
296295
return preprocessor.calculateHash(tokens, "");
297296
}
@@ -444,9 +443,8 @@ class TestPreprocessor : public TestFixture {
444443
"#else\n"
445444
"2\n"
446445
"#endif\n";
447-
std::istringstream istr(filedata);
448446
std::vector<std::string> files;
449-
simplecpp::TokenList tokens(istr, files, "test.c");
447+
simplecpp::TokenList tokens(filedata, sizeof(filedata), files, "test.c");
450448

451449
// preprocess code with unix32 platform..
452450
{
@@ -769,7 +767,7 @@ class TestPreprocessor : public TestFixture {
769767
}
770768

771769
void if_macro_eq_macro() {
772-
const char *code = "#define A B\n"
770+
const char code[] = "#define A B\n"
773771
"#define B 1\n"
774772
"#define C 1\n"
775773
"#if A == C\n"
@@ -781,7 +779,7 @@ class TestPreprocessor : public TestFixture {
781779
}
782780

783781
void ticket_3675() {
784-
const char* code = "#ifdef YYSTACKSIZE\n"
782+
const char code[] = "#ifdef YYSTACKSIZE\n"
785783
"#define YYMAXDEPTH YYSTACKSIZE\n"
786784
"#else\n"
787785
"#define YYSTACKSIZE YYMAXDEPTH\n"
@@ -794,7 +792,7 @@ class TestPreprocessor : public TestFixture {
794792
}
795793

796794
void ticket_3699() {
797-
const char* code = "#define INLINE __forceinline\n"
795+
const char code[] = "#define INLINE __forceinline\n"
798796
"#define inline __forceinline\n"
799797
"#define __forceinline inline\n"
800798
"#if !defined(_WIN32)\n"
@@ -807,7 +805,7 @@ class TestPreprocessor : public TestFixture {
807805
}
808806

809807
void ticket_4922() { // #4922
810-
const char* code = "__asm__ \n"
808+
const char code[] = "__asm__ \n"
811809
"{ int extern __value) 0; (double return (\"\" } extern\n"
812810
"__typeof __finite (__finite) __finite __inline \"__GI___finite\");";
813811
(void)PreprocessorHelper::getcode(settings0, *this, code);
@@ -2229,7 +2227,7 @@ class TestPreprocessor : public TestFixture {
22292227
}
22302228

22312229
void if_sizeof() { // #4071
2232-
static const char* code = "#if sizeof(unsigned short) == 2\n"
2230+
const char code[] = "#if sizeof(unsigned short) == 2\n"
22332231
"Fred & Wilma\n"
22342232
"#elif sizeof(unsigned short) == 4\n"
22352233
"Fred & Wilma\n"

test/testsimplifytypedef.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ class TestSimplifyTypedef : public TestFixture {
265265
return tokenizer.tokens()->stringifyList(nullptr, false);
266266
}
267267

268-
269-
std::string simplifyTypedefP(const char code[]) {
268+
template<size_t size>
269+
std::string simplifyTypedefP(const char (&code)[size]) {
270270
std::vector<std::string> files(1, "test.cpp");
271271
Tokenizer tokenizer(settings0, *this);
272272
PreprocessorHelper::preprocess(code, files, tokenizer, *this);

test/testsimplifyusing.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "utils.h"
2828

2929
#include <cstddef>
30-
#include <sstream>
3130
#include <string>
3231
#include <vector>
3332

@@ -104,8 +103,7 @@ class TestSimplifyUsing : public TestFixture {
104103
Tokenizer tokenizer(settings, *this);
105104
std::vector<std::string> files(1, "test.cpp");
106105
PreprocessorHelper::preprocess(code, files, tokenizer, *this);
107-
std::istringstream istr(code);
108-
ASSERT_LOC(tokenizer.list.createTokens(istr, "test.cpp"), file, line); // TODO: this creates the tokens a second time
106+
ASSERT_LOC(tokenizer.list.createTokens(code, size-1, "test.cpp"), file, line); // TODO: this creates the tokens a second time
109107
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);
110108
return tokenizer.tokens()->stringifyList(nullptr);
111109
}

0 commit comments

Comments
 (0)