Skip to content

Commit dff2828

Browse files
committed
testrunner: removed more std::istringstream usage [skip ci]
1 parent e1e6914 commit dff2828

16 files changed

+112
-89
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
@@ -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);
@@ -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,47 +767,47 @@ class TestPreprocessor : public TestFixture {
769767
}
770768

771769
void if_macro_eq_macro() {
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";
770+
const char code[] = "#define A B\n"
771+
"#define B 1\n"
772+
"#define C 1\n"
773+
"#if A == C\n"
774+
"Wilma\n"
775+
"#else\n"
776+
"Betty\n"
777+
"#endif\n";
780778
ASSERT_EQUALS("\n", getConfigsStr(code));
781779
}
782780

783781
void ticket_3675() {
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";
782+
const char code[] = "#ifdef YYSTACKSIZE\n"
783+
"#define YYMAXDEPTH YYSTACKSIZE\n"
784+
"#else\n"
785+
"#define YYSTACKSIZE YYMAXDEPTH\n"
786+
"#endif\n"
787+
"#if YYDEBUG\n"
788+
"#endif\n";
791789
(void)PreprocessorHelper::getcode(settings0, *this, code);
792790

793791
// There's nothing to assert. It just needs to not hang.
794792
}
795793

796794
void ticket_3699() {
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";
795+
const char code[] = "#define INLINE __forceinline\n"
796+
"#define inline __forceinline\n"
797+
"#define __forceinline inline\n"
798+
"#if !defined(_WIN32)\n"
799+
"#endif\n"
800+
"INLINE inline __forceinline\n";
803801
const std::map<std::string, std::string> actual = PreprocessorHelper::getcode(settings0, *this, code);
804802

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

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

@@ -2229,12 +2227,12 @@ class TestPreprocessor : public TestFixture {
22292227
}
22302228

22312229
void if_sizeof() { // #4071
2232-
static 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";
2230+
const char code[] = "#if sizeof(unsigned short) == 2\n"
2231+
"Fred & Wilma\n"
2232+
"#elif sizeof(unsigned short) == 4\n"
2233+
"Fred & Wilma\n"
2234+
"#else\n"
2235+
"#endif";
22382236

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

test/testsimplifytypedef.cpp

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

274-
275-
std::string simplifyTypedefP(const char code[]) {
274+
template<size_t size>
275+
std::string simplifyTypedefP(const char (&code)[size]) {
276276
std::vector<std::string> files(1, "test.cpp");
277277
Tokenizer tokenizer(settings0, *this);
278278
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

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

0 commit comments

Comments
 (0)