Skip to content

Commit 2e85422

Browse files
committed
testrunner: removed more std::istringstream usage [skip ci]
1 parent da820c6 commit 2e85422

16 files changed

+115
-93
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,17 +162,16 @@ 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
{
173172
// TODO: make sure the given Tokenizer has not been used yet
174173

175-
std::istringstream istr(code);
176-
const simplecpp::TokenList tokens1(istr, files, files[0]);
174+
const simplecpp::TokenList tokens1(code, size, files, files[0]);
177175

178176
// Preprocess..
179177
simplecpp::TokenList tokens2(files);
@@ -189,11 +187,10 @@ void PreprocessorHelper::preprocess(const char code[], std::vector<std::string>
189187
tokenizer.setDirectives(std::move(directives));
190188
}
191189

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

198195
const Settings settings;
199196

test/helpers.h

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,38 @@ class PreprocessorHelper
167167
* @param inlineSuppression the inline suppressions
168168
*/
169169
static std::string getcode(const Settings& settings, ErrorLogger& errorlogger, const std::string &filedata, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression = nullptr);
170-
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);
170+
template<size_t size>
171+
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)
172+
{
173+
return getcode(settings, errorlogger, code, size-1, filename, inlineSuppression);
174+
}
171175

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

175187
/** get remark comments */
176-
static std::vector<RemarkComment> getRemarkComments(const char code[], ErrorLogger& errorLogger);
188+
template<size_t size>
189+
static std::vector<RemarkComment> getRemarkComments(const char (&code)[size], ErrorLogger& errorLogger)
190+
{
191+
return getRemarkComments(code, size-1, errorLogger);
192+
}
177193

178194
private:
179-
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);
195+
static void preprocess(const char code[], std::size_t size, std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger);
196+
static void preprocess(const char code[], std::size_t size, std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger, const simplecpp::DUI& dui);
197+
198+
static std::vector<RemarkComment> getRemarkComments(const char code[], std::size_t size, ErrorLogger& errorLogger);
199+
200+
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);
201+
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);
180202
};
181203

182204
namespace cppcheck {

test/testbufferoverrun.cpp

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

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

test/testclass.cpp

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

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

85748575
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
@@ -127,7 +127,8 @@ class TestCondition : public TestFixture {
127127
}
128128

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

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

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
@@ -3096,7 +3096,8 @@ class TestLeakAutoVarRecursiveCountLimit : public TestFixture {
30963096
const Settings settings = settingsBuilder().library("std.cfg").checkLibrary().build();
30973097

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

test/testpreprocessor.cpp

Lines changed: 40 additions & 42 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);
@@ -270,16 +269,16 @@ class TestPreprocessor : public TestFixture {
270269
TEST_CASE(standard);
271270
}
272271

273-
std::string getConfigsStr(const char filedata[], const char *arg = nullptr) {
272+
template<size_t size>
273+
std::string getConfigsStr(const char (&code)[size], const char *arg = nullptr) {
274274
Settings settings;
275275
if (arg && std::strncmp(arg,"-D",2)==0)
276276
settings.userDefines = arg + 2;
277277
if (arg && std::strncmp(arg,"-U",2)==0)
278278
settings.userUndefs.insert(arg+2);
279279
Preprocessor preprocessor(settings, *this);
280280
std::vector<std::string> files;
281-
std::istringstream istr(filedata);
282-
simplecpp::TokenList tokens(istr,files);
281+
simplecpp::TokenList tokens(code,size-1,files);
283282
tokens.removeComments();
284283
const std::set<std::string> configs = preprocessor.getConfigs(tokens);
285284
std::string ret;
@@ -288,12 +287,12 @@ class TestPreprocessor : public TestFixture {
288287
return ret;
289288
}
290289

291-
std::size_t getHash(const char filedata[]) {
290+
template<size_t size>
291+
std::size_t getHash(const char (&code)[size]) {
292292
Settings settings;
293293
Preprocessor preprocessor(settings, *this);
294294
std::vector<std::string> files;
295-
std::istringstream istr(filedata);
296-
simplecpp::TokenList tokens(istr,files);
295+
simplecpp::TokenList tokens(code,size-1,files);
297296
tokens.removeComments();
298297
return preprocessor.calculateHash(tokens, "");
299298
}
@@ -446,9 +445,8 @@ class TestPreprocessor : public TestFixture {
446445
"#else\n"
447446
"2\n"
448447
"#endif\n";
449-
std::istringstream istr(filedata);
450448
std::vector<std::string> files;
451-
simplecpp::TokenList tokens(istr, files, "test.c");
449+
simplecpp::TokenList tokens(filedata, sizeof(filedata), files, "test.c");
452450

453451
// preprocess code with unix32 platform..
454452
{
@@ -771,47 +769,47 @@ class TestPreprocessor : public TestFixture {
771769
}
772770

773771
void if_macro_eq_macro() {
774-
const char *code = "#define A B\n"
775-
"#define B 1\n"
776-
"#define C 1\n"
777-
"#if A == C\n"
778-
"Wilma\n"
779-
"#else\n"
780-
"Betty\n"
781-
"#endif\n";
772+
const char code[] = "#define A B\n"
773+
"#define B 1\n"
774+
"#define C 1\n"
775+
"#if A == C\n"
776+
"Wilma\n"
777+
"#else\n"
778+
"Betty\n"
779+
"#endif\n";
782780
ASSERT_EQUALS("\n", getConfigsStr(code));
783781
}
784782

785783
void ticket_3675() {
786-
const char* code = "#ifdef YYSTACKSIZE\n"
787-
"#define YYMAXDEPTH YYSTACKSIZE\n"
788-
"#else\n"
789-
"#define YYSTACKSIZE YYMAXDEPTH\n"
790-
"#endif\n"
791-
"#if YYDEBUG\n"
792-
"#endif\n";
784+
const char code[] = "#ifdef YYSTACKSIZE\n"
785+
"#define YYMAXDEPTH YYSTACKSIZE\n"
786+
"#else\n"
787+
"#define YYSTACKSIZE YYMAXDEPTH\n"
788+
"#endif\n"
789+
"#if YYDEBUG\n"
790+
"#endif\n";
793791
(void)PreprocessorHelper::getcode(settings0, *this, code);
794792

795793
// There's nothing to assert. It just needs to not hang.
796794
}
797795

798796
void ticket_3699() {
799-
const char* code = "#define INLINE __forceinline\n"
800-
"#define inline __forceinline\n"
801-
"#define __forceinline inline\n"
802-
"#if !defined(_WIN32)\n"
803-
"#endif\n"
804-
"INLINE inline __forceinline\n";
797+
const char code[] = "#define INLINE __forceinline\n"
798+
"#define inline __forceinline\n"
799+
"#define __forceinline inline\n"
800+
"#if !defined(_WIN32)\n"
801+
"#endif\n"
802+
"INLINE inline __forceinline\n";
805803
const std::map<std::string, std::string> actual = PreprocessorHelper::getcode(settings0, *this, code);
806804

807805
// First, it must not hang. Second, inline must becomes inline, and __forceinline must become __forceinline.
808806
ASSERT_EQUALS("\n\n\n\n\n$__forceinline $inline $__forceinline", actual.at(""));
809807
}
810808

811809
void ticket_4922() { // #4922
812-
const char* code = "__asm__ \n"
813-
"{ int extern __value) 0; (double return (\"\" } extern\n"
814-
"__typeof __finite (__finite) __finite __inline \"__GI___finite\");";
810+
const char code[] = "__asm__ \n"
811+
"{ int extern __value) 0; (double return (\"\" } extern\n"
812+
"__typeof __finite (__finite) __finite __inline \"__GI___finite\");";
815813
(void)PreprocessorHelper::getcode(settings0, *this, code);
816814
}
817815

@@ -2231,12 +2229,12 @@ class TestPreprocessor : public TestFixture {
22312229
}
22322230

22332231
void if_sizeof() { // #4071
2234-
static const char* code = "#if sizeof(unsigned short) == 2\n"
2235-
"Fred & Wilma\n"
2236-
"#elif sizeof(unsigned short) == 4\n"
2237-
"Fred & Wilma\n"
2238-
"#else\n"
2239-
"#endif";
2232+
const char code[] = "#if sizeof(unsigned short) == 2\n"
2233+
"Fred & Wilma\n"
2234+
"#elif sizeof(unsigned short) == 4\n"
2235+
"Fred & Wilma\n"
2236+
"#else\n"
2237+
"#endif";
22402238

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

test/testsimplifytypedef.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "tokenlist.h"
2828

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

@@ -272,8 +271,8 @@ class TestSimplifyTypedef : public TestFixture {
272271
return tokenizer.tokens()->stringifyList(nullptr, false);
273272
}
274273

275-
276-
std::string simplifyTypedefP(const char code[]) {
274+
template<size_t size>
275+
std::string simplifyTypedefP(const char (&code)[size]) {
277276
std::vector<std::string> files(1, "test.cpp");
278277
Tokenizer tokenizer(settings0, *this);
279278
PreprocessorHelper::preprocess(code, files, tokenizer, *this);
@@ -312,11 +311,11 @@ class TestSimplifyTypedef : public TestFixture {
312311
return tokenizer.tokens()->stringifyList(nullptr, false);
313312
}
314313

315-
std::string dumpTypedefInfo(const char code[]) {
314+
template<size_t size>
315+
std::string dumpTypedefInfo(const char (&code)[size]) {
316316
Tokenizer tokenizer(settings1, *this);
317317

318-
std::istringstream istr(code);
319-
if (!tokenizer.list.createTokens(istr, "file.c"))
318+
if (!tokenizer.list.createTokens(code, size-1, "file.c"))
320319
return {};
321320
tokenizer.createLinks();
322321
tokenizer.simplifyTypedef();

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

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

0 commit comments

Comments
 (0)