Skip to content

Commit e72b502

Browse files
committed
TestPreprocessor: got rid of std::istringstream usage [skip ci]
1 parent 339a647 commit e72b502

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

test/testpreprocessor.cpp

Lines changed: 19 additions & 14 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>
@@ -92,28 +91,34 @@ class TestPreprocessor : public TestFixture {
9291
return preprocessor.getRemarkComments(tokens1);
9392
}
9493

95-
// TODO: we should be using the actual Preprocessor implementation
96-
static std::string getcodeforcfg(const Settings& settings, ErrorLogger& errorlogger, const char code[], const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression = nullptr)
94+
static std::string getcodeforcfg(const Settings& settings, ErrorLogger& errorlogger, const char* code, std::size_t size, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression = nullptr)
9795
{
98-
std::map<std::string, std::string> cfgcode = getcode(settings, errorlogger, code, std::set<std::string>{cfg}, filename, inlineSuppression);
96+
std::map<std::string, std::string> cfgcode = getcode(settings, errorlogger, code, size, std::set<std::string>{cfg}, filename, inlineSuppression);
9997
const auto it = cfgcode.find(cfg);
10098
if (it == cfgcode.end())
10199
return "";
102100
return it->second;
103101
}
104102

105-
static std::map<std::string, std::string> getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], const std::string &filename = "file.c")
103+
template<size_t size>
104+
static std::string getcodeforcfg(const Settings& settings, ErrorLogger& errorlogger, const char (&code)[size], const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression = nullptr)
105+
{
106+
return getcodeforcfg(settings, errorlogger, code, size-1, cfg, filename, inlineSuppression);
107+
}
108+
109+
template<size_t size>
110+
static std::map<std::string, std::string> getcode(const Settings& settings, ErrorLogger& errorlogger, const char (&code)[size], const std::string &filename = "file.c")
106111
{
107-
return getcode(settings, errorlogger, code, {}, filename, nullptr);
112+
return getcode(settings, errorlogger, code, size-1, {}, filename, nullptr);
108113
}
109114

110-
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, SuppressionList *inlineSuppression)
115+
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, SuppressionList *inlineSuppression)
111116
{
112117
simplecpp::OutputList outputList;
113118
std::vector<std::string> files;
114119

115-
std::istringstream istr(code);
116-
simplecpp::TokenList tokens(istr, files, Path::simplifyPath(filename), &outputList);
120+
simplecpp::TokenList tokens(code, size, files, Path::simplifyPath(filename), &outputList);
121+
// TODO: we should be using the actual Preprocessor implementation
117122
Preprocessor preprocessor(settings, errorlogger, Path::identify(tokens.getFiles()[0], false));
118123
if (inlineSuppression)
119124
preprocessor.inlineSuppressions(tokens, *inlineSuppression);
@@ -2456,7 +2461,7 @@ class TestPreprocessor : public TestFixture {
24562461
ScopedFile header("header.h", "", Path::getCurrentPath());
24572462

24582463
std::string code("#include \"" + header.path() + "\"");
2459-
(void)getcodeforcfg(settings, *this, code.c_str(), "", "test.c");
2464+
(void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "test.c");
24602465

24612466
ASSERT_EQUALS("", errout_str());
24622467
}
@@ -2472,7 +2477,7 @@ class TestPreprocessor : public TestFixture {
24722477
const std::string header = Path::join(Path::getCurrentPath(), "header.h");
24732478

24742479
std::string code("#include \"" + header + "\"");
2475-
(void)getcodeforcfg(settings, *this, code.c_str(), "", "test.c");
2480+
(void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "test.c");
24762481

24772482
ASSERT_EQUALS("test.c:1:0: information: Include file: \"" + header + "\" not found. [missingInclude]\n", errout_str());
24782483
}
@@ -2536,7 +2541,7 @@ class TestPreprocessor : public TestFixture {
25362541
ScopedFile header("header.h", "", Path::getCurrentPath());
25372542

25382543
std::string code("#include <" + header.path() + ">");
2539-
(void)getcodeforcfg(settings, *this, code.c_str(), "", "test.c");
2544+
(void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "test.c");
25402545

25412546
ASSERT_EQUALS("", errout_str());
25422547
}
@@ -2552,7 +2557,7 @@ class TestPreprocessor : public TestFixture {
25522557
const std::string header = Path::join(Path::getCurrentPath(), "header.h");
25532558

25542559
std::string code("#include <" + header + ">");
2555-
(void)getcodeforcfg(settings, *this, code.c_str(), "", "test.c");
2560+
(void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "test.c");
25562561

25572562
ASSERT_EQUALS("test.c:1:0: information: Include file: <" + header + "> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n", errout_str());
25582563
}
@@ -2608,7 +2613,7 @@ class TestPreprocessor : public TestFixture {
26082613
"#include \"" + missing3 + "\"\n"
26092614
"#include <" + header6.path() + ">\n"
26102615
"#include <" + missing4 + ">\n");
2611-
(void)getcodeforcfg(settings, *this, code.c_str(), "", "test.c");
2616+
(void)getcodeforcfg(settings, *this, code.data(), code.size(), "", "test.c");
26122617

26132618
ASSERT_EQUALS("test.c:1:0: information: Include file: \"missing.h\" not found. [missingInclude]\n"
26142619
"test.c:2:0: information: Include file: <header.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n"

0 commit comments

Comments
 (0)