Skip to content

Commit

Permalink
testrunner: merged Testpreprocessor::preprocess() with `Preprocesso…
Browse files Browse the repository at this point in the history
…rHelper::getcode()`
  • Loading branch information
firewave committed Apr 11, 2024
1 parent b9c535c commit 1c49488
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 149 deletions.
46 changes: 32 additions & 14 deletions test/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,36 +112,54 @@ ScopedFile::~ScopedFile() {

// TODO: we should be using the actual Preprocessor implementation
std::string PreprocessorHelper::getcode(Preprocessor &preprocessor, const std::string &filedata, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression)
{
std::map<std::string, std::string> cfgcode = getcode(preprocessor, filedata.c_str(), 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(Preprocessor &preprocessor, const char code[], const std::string &filename, SuppressionList *inlineSuppression)
{
return getcode(preprocessor, code, {}, filename, inlineSuppression);
}

std::map<std::string, std::string> PreprocessorHelper::getcode(Preprocessor &preprocessor, const char code[], std::set<std::string> cfgs, const std::string &filename, SuppressionList *inlineSuppression)
{
simplecpp::OutputList outputList;
std::vector<std::string> files;

std::istringstream istr(filedata);
simplecpp::TokenList tokens1(istr, files, Path::simplifyPath(filename), &outputList);
std::istringstream istr(code);
simplecpp::TokenList tokens(istr, files, Path::simplifyPath(filename), &outputList);
if (inlineSuppression)
preprocessor.inlineSuppressions(tokens1, *inlineSuppression);
tokens1.removeComments();
preprocessor.simplifyPragmaAsm(&tokens1);
preprocessor.inlineSuppressions(tokens, *inlineSuppression);
tokens.removeComments();
preprocessor.simplifyPragmaAsm(&tokens);
preprocessor.removeComments();

preprocessor.reportOutput(outputList, true);

if (Preprocessor::hasErrors(outputList))
return "";

std::string ret;
try {
// TODO: also preserve location information when #include exists - enabling that will fail since #line is treated like a regular token
ret = preprocessor.getcode(tokens1, cfg, files, filedata.find("#file") != std::string::npos);
} catch (const simplecpp::Output &) {
ret.clear();
return {};

std::map<std::string, std::string> cfgcode;
if (cfgs.empty())
cfgs = preprocessor.getConfigs(tokens);
for (const std::string & config : cfgs) {
try {
// TODO: also preserve location information when #include exists - enabling that will fail since #line is treated like a regular token
cfgcode[config] = preprocessor.getcode(tokens, config, files, std::string(code).find("#file") != std::string::npos);
} catch (const simplecpp::Output &) {
cfgcode[config] = "";
}
}

// Since "files" is a local variable the tracking info must be cleared..
preprocessor.mMacroUsage.clear();
preprocessor.mIfCond.clear();

return ret;
return cfgcode;
}

void PreprocessorHelper::preprocess(const char code[], std::vector<std::string> &files, Tokenizer& tokenizer, ErrorLogger& errorlogger)
Expand Down
4 changes: 4 additions & 0 deletions test/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,13 @@ class PreprocessorHelper
* @param inlineSuppression the inline suppressions
*/
static std::string getcode(Preprocessor &preprocessor, const std::string &filedata, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression = nullptr);
static std::map<std::string, std::string> getcode(Preprocessor &preprocessor, const char code[], const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr);

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

private:
static std::map<std::string, std::string> getcode(Preprocessor &preprocessor, const char code[], std::set<std::string> cfgs, const std::string &filename = "file.c", SuppressionList *inlineSuppression = nullptr);
};

namespace cppcheck {
Expand Down
Loading

0 comments on commit 1c49488

Please sign in to comment.