Skip to content

Commit

Permalink
testrunner: removed more std::istringstream usage
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Jul 15, 2024
1 parent 4664ac4 commit 10eaa80
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 89 deletions.
25 changes: 11 additions & 14 deletions test/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,24 @@ ScopedFile::~ScopedFile() {
// TODO: we should be using the actual Preprocessor implementation
std::string PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const std::string &filedata, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression)
{
std::map<std::string, std::string> cfgcode = getcode(settings, errorlogger, filedata.c_str(), std::set<std::string>{cfg}, filename, inlineSuppression);
std::map<std::string, std::string> cfgcode = getcode(settings, errorlogger, filedata.c_str(), filedata.size(), std::set<std::string>{cfg}, filename, inlineSuppression);
const auto it = cfgcode.find(cfg);
if (it == cfgcode.end())
return "";
return it->second;
}

std::map<std::string, std::string> PreprocessorHelper::getcode(const Settings& settings, ErrorLogger& errorlogger, const char code[], const std::string &filename, SuppressionList *inlineSuppression)
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)
{
return getcode(settings, errorlogger, code, {}, filename, inlineSuppression);
return getcode(settings, errorlogger, code, size, {}, filename, inlineSuppression);
}

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)
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)
{
simplecpp::OutputList outputList;
std::vector<std::string> files;

std::istringstream istr(code);
simplecpp::TokenList tokens(istr, files, Path::simplifyPath(filename), &outputList);
simplecpp::TokenList tokens(code, size, files, Path::simplifyPath(filename), &outputList);
Preprocessor preprocessor(settings, errorlogger);
if (inlineSuppression)
preprocessor.inlineSuppressions(tokens, *inlineSuppression);
Expand Down Expand Up @@ -163,15 +162,14 @@ std::map<std::string, std::string> PreprocessorHelper::getcode(const Settings& s
return cfgcode;
}

void PreprocessorHelper::preprocess(const char code[], std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger)
void PreprocessorHelper::preprocess(const char code[], std::size_t size, std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger)
{
preprocess(code, files, tokenizer, errorlogger, simplecpp::DUI());
preprocess(code, size, files, tokenizer, errorlogger, simplecpp::DUI());
}

void PreprocessorHelper::preprocess(const char code[], std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger, const simplecpp::DUI& dui)
void PreprocessorHelper::preprocess(const char code[], std::size_t size, std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger, const simplecpp::DUI& dui)
{
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);
const simplecpp::TokenList tokens1(code, size, files, files[0]);

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

std::vector<RemarkComment> PreprocessorHelper::getRemarkComments(const char code[], ErrorLogger& errorLogger)
std::vector<RemarkComment> PreprocessorHelper::getRemarkComments(const char code[], std::size_t size, ErrorLogger& errorLogger)
{
std::vector<std::string> files{"test.cpp"};
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);
const simplecpp::TokenList tokens1(code, size, files, files[0]);

const Settings settings;

Expand Down
32 changes: 27 additions & 5 deletions test/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,38 @@ class PreprocessorHelper
* @param inlineSuppression the inline suppressions
*/
static std::string getcode(const Settings& settings, ErrorLogger& errorlogger, const std::string &filedata, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression = nullptr);
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);
template<size_t size>
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)
{
return getcode(settings, errorlogger, code, size-1, filename, inlineSuppression);
}

static void preprocess(const char code[], std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger);
static void preprocess(const char code[], std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger, const simplecpp::DUI& dui);
template<size_t size>
static void preprocess(const char (&code)[size], std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger)
{
preprocess(code, size-1, files, tokenizer, errorlogger);
}
template<size_t size>
static void preprocess(const char (&code)[size], std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger, const simplecpp::DUI& dui)
{
preprocess(code, size-1, files, tokenizer, errorlogger, dui);
}

/** get remark comments */
static std::vector<RemarkComment> getRemarkComments(const char code[], ErrorLogger& errorLogger);
template<size_t size>
static std::vector<RemarkComment> getRemarkComments(const char (&code)[size], ErrorLogger& errorLogger)
{
return getRemarkComments(code, size-1, errorLogger);
}

private:
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);
static void preprocess(const char code[], std::size_t size, std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger);
static void preprocess(const char code[], std::size_t size, std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger, const simplecpp::DUI& dui);

static std::vector<RemarkComment> getRemarkComments(const char code[], std::size_t size, ErrorLogger& errorLogger);

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);
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);
};

namespace cppcheck {
Expand Down
3 changes: 2 additions & 1 deletion test/testbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class TestBufferOverrun : public TestFixture {
}

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

Expand Down
3 changes: 2 additions & 1 deletion test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8554,7 +8554,8 @@ class TestClass : public TestFixture {
}

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

std::vector<std::string> files(1, "test.cpp");
Expand Down
6 changes: 4 additions & 2 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ class TestCondition : public TestFixture {
}

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

void check_(const char* file, int line, const char code[], const char* filename = "test.cpp", bool inconclusive = false) {
template<size_t size>
void check_(const char* file, int line, const char (&code)[size], const char* filename = "test.cpp", bool inconclusive = false) {
const Settings settings = settingsBuilder(settings0).certainty(Certainty::inconclusive, inconclusive).build();
check_(file, line, code, settings, filename);
}
Expand Down
3 changes: 2 additions & 1 deletion test/testincompletestatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class TestIncompleteStatement : public TestFixture {
const Settings settings = settingsBuilder().severity(Severity::warning).build();

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

std::vector<std::string> files(1, cpp ? "test.cpp" : "test.c");
Expand Down
3 changes: 2 additions & 1 deletion test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3103,7 +3103,8 @@ class TestLeakAutoVarRecursiveCountLimit : public TestFixture {
const Settings settings = settingsBuilder().library("std.cfg").checkLibrary().build();

#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
void checkP_(const char* file, int line, const char code[], bool cpp = false) {
template<size_t size>
void checkP_(const char* file, int line, const char (&code)[size], bool cpp = false) {
std::vector<std::string> files(1, cpp?"test.cpp":"test.c");
Tokenizer tokenizer(settings, *this);
PreprocessorHelper::preprocess(code, files, tokenizer, *this);
Expand Down
82 changes: 40 additions & 42 deletions test/testpreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

Expand All @@ -48,11 +47,11 @@ class TestPreprocessor : public TestFixture {
class OurPreprocessor : public Preprocessor {
public:

static std::string expandMacros(const char code[], ErrorLogger *errorLogger = nullptr) {
std::istringstream istr(code);
template<size_t size>
static std::string expandMacros(const char (&code)[size], ErrorLogger *errorLogger = nullptr) {
simplecpp::OutputList outputList;
std::vector<std::string> files;
const simplecpp::TokenList tokens1 = simplecpp::TokenList(istr, files, "file.cpp", &outputList);
const simplecpp::TokenList tokens1 = simplecpp::TokenList(code, size-1, files, "file.cpp", &outputList);
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::TokenList tokens2(files);
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI(), &outputList);
Expand Down Expand Up @@ -268,16 +267,16 @@ class TestPreprocessor : public TestFixture {
TEST_CASE(hashCalculation);
}

std::string getConfigsStr(const char filedata[], const char *arg = nullptr) {
template<size_t size>
std::string getConfigsStr(const char (&code)[size], const char *arg = nullptr) {
Settings settings;
if (arg && std::strncmp(arg,"-D",2)==0)
settings.userDefines = arg + 2;
if (arg && std::strncmp(arg,"-U",2)==0)
settings.userUndefs.insert(arg+2);
Preprocessor preprocessor(settings, *this);
std::vector<std::string> files;
std::istringstream istr(filedata);
simplecpp::TokenList tokens(istr,files);
simplecpp::TokenList tokens(code,size-1,files);
tokens.removeComments();
const std::set<std::string> configs = preprocessor.getConfigs(tokens);
std::string ret;
Expand All @@ -286,12 +285,12 @@ class TestPreprocessor : public TestFixture {
return ret;
}

std::size_t getHash(const char filedata[]) {
template<size_t size>
std::size_t getHash(const char (&code)[size]) {
Settings settings;
Preprocessor preprocessor(settings, *this);
std::vector<std::string> files;
std::istringstream istr(filedata);
simplecpp::TokenList tokens(istr,files);
simplecpp::TokenList tokens(code,size-1,files);
tokens.removeComments();
return preprocessor.calculateHash(tokens, "");
}
Expand Down Expand Up @@ -444,9 +443,8 @@ class TestPreprocessor : public TestFixture {
"#else\n"
"2\n"
"#endif\n";
std::istringstream istr(filedata);
std::vector<std::string> files;
simplecpp::TokenList tokens(istr, files, "test.c");
simplecpp::TokenList tokens(filedata, sizeof(filedata), files, "test.c");

// preprocess code with unix32 platform..
{
Expand Down Expand Up @@ -769,47 +767,47 @@ class TestPreprocessor : public TestFixture {
}

void if_macro_eq_macro() {
const char *code = "#define A B\n"
"#define B 1\n"
"#define C 1\n"
"#if A == C\n"
"Wilma\n"
"#else\n"
"Betty\n"
"#endif\n";
const char code[] = "#define A B\n"
"#define B 1\n"
"#define C 1\n"
"#if A == C\n"
"Wilma\n"
"#else\n"
"Betty\n"
"#endif\n";
ASSERT_EQUALS("\n", getConfigsStr(code));
}

void ticket_3675() {
const char* code = "#ifdef YYSTACKSIZE\n"
"#define YYMAXDEPTH YYSTACKSIZE\n"
"#else\n"
"#define YYSTACKSIZE YYMAXDEPTH\n"
"#endif\n"
"#if YYDEBUG\n"
"#endif\n";
const char code[] = "#ifdef YYSTACKSIZE\n"
"#define YYMAXDEPTH YYSTACKSIZE\n"
"#else\n"
"#define YYSTACKSIZE YYMAXDEPTH\n"
"#endif\n"
"#if YYDEBUG\n"
"#endif\n";
(void)PreprocessorHelper::getcode(settings0, *this, code);

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

void ticket_3699() {
const char* code = "#define INLINE __forceinline\n"
"#define inline __forceinline\n"
"#define __forceinline inline\n"
"#if !defined(_WIN32)\n"
"#endif\n"
"INLINE inline __forceinline\n";
const char code[] = "#define INLINE __forceinline\n"
"#define inline __forceinline\n"
"#define __forceinline inline\n"
"#if !defined(_WIN32)\n"
"#endif\n"
"INLINE inline __forceinline\n";
const std::map<std::string, std::string> actual = PreprocessorHelper::getcode(settings0, *this, code);

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

void ticket_4922() { // #4922
const char* code = "__asm__ \n"
"{ int extern __value) 0; (double return (\"\" } extern\n"
"__typeof __finite (__finite) __finite __inline \"__GI___finite\");";
const char code[] = "__asm__ \n"
"{ int extern __value) 0; (double return (\"\" } extern\n"
"__typeof __finite (__finite) __finite __inline \"__GI___finite\");";
(void)PreprocessorHelper::getcode(settings0, *this, code);
}

Expand Down Expand Up @@ -2229,12 +2227,12 @@ class TestPreprocessor : public TestFixture {
}

void if_sizeof() { // #4071
static const char* code = "#if sizeof(unsigned short) == 2\n"
"Fred & Wilma\n"
"#elif sizeof(unsigned short) == 4\n"
"Fred & Wilma\n"
"#else\n"
"#endif";
const char code[] = "#if sizeof(unsigned short) == 2\n"
"Fred & Wilma\n"
"#elif sizeof(unsigned short) == 4\n"
"Fred & Wilma\n"
"#else\n"
"#endif";

const std::map<std::string, std::string> actual = PreprocessorHelper::getcode(settings0, *this, code);
ASSERT_EQUALS("\nFred & Wilma", actual.at(""));
Expand Down
4 changes: 2 additions & 2 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ class TestSimplifyTypedef : public TestFixture {
return tokenizer.tokens()->stringifyList(nullptr, false);
}


std::string simplifyTypedefP(const char code[]) {
template<size_t size>
std::string simplifyTypedefP(const char (&code)[size]) {
std::vector<std::string> files(1, "test.cpp");
Tokenizer tokenizer(settings0, *this);
PreprocessorHelper::preprocess(code, files, tokenizer, *this);
Expand Down
4 changes: 1 addition & 3 deletions test/testsimplifyusing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "utils.h"

#include <cstddef>
#include <sstream>
#include <string>
#include <vector>

Expand Down Expand Up @@ -104,8 +103,7 @@ class TestSimplifyUsing : public TestFixture {
Tokenizer tokenizer(settings, *this);
std::vector<std::string> files(1, "test.cpp");
PreprocessorHelper::preprocess(code, files, tokenizer, *this);
std::istringstream istr(code);
ASSERT_LOC(tokenizer.list.createTokens(istr, "test.cpp"), file, line); // TODO: this creates the tokens a second time
ASSERT_LOC(tokenizer.list.createTokens(code, size-1, "test.cpp"), file, line); // TODO: this creates the tokens a second time
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);
return tokenizer.tokens()->stringifyList(nullptr);
}
Expand Down
3 changes: 2 additions & 1 deletion test/teststring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class TestString : public TestFixture {
}

#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
void check_(const char* file, int line, const char code[], const char filename[] = "test.cpp") {
template<size_t size>
void check_(const char* file, int line, const char (&code)[size], const char filename[] = "test.cpp") {
std::vector<std::string> files(1, filename);
Tokenizer tokenizer(settings, *this);
PreprocessorHelper::preprocess(code, files, tokenizer, *this);
Expand Down
Loading

0 comments on commit 10eaa80

Please sign in to comment.