Skip to content

Commit 90ee464

Browse files
authored
TestPreprocessor: removed usage of std::istringstream (#6873)
1 parent 71ee509 commit 90ee464

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

test/testpreprocessor.cpp

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <list>
3636
#include <map>
3737
#include <set>
38-
#include <sstream>
3938
#include <stdexcept>
4039
#include <string>
4140
#include <utility>
@@ -50,27 +49,27 @@ class TestPreprocessor : public TestFixture {
5049
TestPreprocessor() : TestFixture("TestPreprocessor") {}
5150

5251
private:
53-
std::string expandMacros(const char code[], ErrorLogger &errorLogger) const {
54-
std::istringstream istr(code);
52+
template<size_t size>
53+
std::string expandMacros(const char (&code)[size], ErrorLogger &errorLogger) const {
5554
simplecpp::OutputList outputList;
5655
std::vector<std::string> files;
57-
const simplecpp::TokenList tokens1 = simplecpp::TokenList(istr, files, "file.cpp", &outputList);
56+
const simplecpp::TokenList tokens1 = simplecpp::TokenList(code, size-1, files, "file.cpp", &outputList);
5857
Preprocessor p(settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false));
5958
simplecpp::TokenList tokens2 = p.preprocess(tokens1, "", files, true);
6059
p.reportOutput(outputList, true);
6160
return tokens2.stringify();
6261
}
6362

64-
static void preprocess(const char code[], std::vector<std::string> &files, const std::string& file0, TokenList& tokenlist, const simplecpp::DUI& dui)
63+
template<size_t size>
64+
static void preprocess(const char (&code)[size], std::vector<std::string> &files, const std::string& file0, TokenList& tokenlist, const simplecpp::DUI& dui)
6565
{
6666
if (!files.empty())
6767
throw std::runtime_error("file list not empty");
6868

6969
if (tokenlist.front())
7070
throw std::runtime_error("token list not empty");
7171

72-
std::istringstream istr(code);
73-
const simplecpp::TokenList tokens1(istr, files, file0);
72+
const simplecpp::TokenList tokens1(code, size-1, files, file0);
7473

7574
// Preprocess..
7675
simplecpp::TokenList tokens2(files);
@@ -82,11 +81,11 @@ class TestPreprocessor : public TestFixture {
8281
tokenlist.createTokens(std::move(tokens2));
8382
}
8483

85-
std::vector<RemarkComment> getRemarkComments(const char code[], ErrorLogger& errorLogger) const
84+
template<size_t size>
85+
std::vector<RemarkComment> getRemarkComments(const char (&code)[size], ErrorLogger& errorLogger) const
8686
{
8787
std::vector<std::string> files;
88-
std::istringstream istr(code);
89-
const simplecpp::TokenList tokens1(istr, files, "test.cpp");
88+
const simplecpp::TokenList tokens1(code, size-1, files, "test.cpp");
9089

9190
const Preprocessor preprocessor(settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false));
9291
return preprocessor.getRemarkComments(tokens1);
@@ -301,16 +300,16 @@ class TestPreprocessor : public TestFixture {
301300
TEST_CASE(standard);
302301
}
303302

304-
std::string getConfigsStr(const char filedata[], const char *arg = nullptr) {
303+
template<size_t size>
304+
std::string getConfigsStr(const char (&code)[size], const char *arg = nullptr) {
305305
Settings settings;
306306
if (arg && std::strncmp(arg,"-D",2)==0)
307307
settings.userDefines = arg + 2;
308308
if (arg && std::strncmp(arg,"-U",2)==0)
309309
settings.userUndefs.insert(arg+2);
310310
std::vector<std::string> files;
311-
std::istringstream istr(filedata);
312311
// TODO: this adds an empty filename
313-
simplecpp::TokenList tokens(istr,files);
312+
simplecpp::TokenList tokens(code, size-1,files);
314313
tokens.removeComments();
315314
Preprocessor preprocessor(settings, *this, Standards::Language::C); // TODO: do we need to consider #file?
316315
const std::set<std::string> configs = preprocessor.getConfigs(tokens);
@@ -320,11 +319,11 @@ class TestPreprocessor : public TestFixture {
320319
return ret;
321320
}
322321

323-
std::size_t getHash(const char filedata[]) {
322+
template<size_t size>
323+
std::size_t getHash(const char (&code)[size]) {
324324
std::vector<std::string> files;
325-
std::istringstream istr(filedata);
326325
// TODO: this adds an empty filename
327-
simplecpp::TokenList tokens(istr,files);
326+
simplecpp::TokenList tokens(code,size-1,files);
328327
tokens.removeComments();
329328
Preprocessor preprocessor(settingsDefault, *this, Standards::Language::C); // TODO: do we need to consider #file?
330329
return preprocessor.calculateHash(tokens, "");
@@ -478,9 +477,8 @@ class TestPreprocessor : public TestFixture {
478477
"#else\n"
479478
"2\n"
480479
"#endif\n";
481-
std::istringstream istr(filedata);
482480
std::vector<std::string> files;
483-
simplecpp::TokenList tokens(istr, files, "test.c");
481+
simplecpp::TokenList tokens(filedata, sizeof(filedata), files, "test.c");
484482

485483
// preprocess code with unix32 platform..
486484
{
@@ -803,14 +801,14 @@ class TestPreprocessor : public TestFixture {
803801
}
804802

805803
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";
804+
const char code[] = "#define A B\n"
805+
"#define B 1\n"
806+
"#define C 1\n"
807+
"#if A == C\n"
808+
"Wilma\n"
809+
"#else\n"
810+
"Betty\n"
811+
"#endif\n";
814812
ASSERT_EQUALS("\n", getConfigsStr(code));
815813
}
816814

0 commit comments

Comments
 (0)