Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,18 +753,10 @@ bool Preprocessor::hasErrors(const simplecpp::Output &output)
return false;
}

bool Preprocessor::hasErrors(const simplecpp::OutputList &outputList)
{
const auto it = std::find_if(outputList.cbegin(), outputList.cend(), [](const simplecpp::Output &output) {
return hasErrors(output);
});
return it != outputList.cend();
}

void Preprocessor::handleErrors(const simplecpp::OutputList& outputList, bool throwError)
bool Preprocessor::handleErrors(const simplecpp::OutputList& outputList, bool throwError)
{
const bool showerror = (!mSettings.userDefines.empty() && !mSettings.force);
reportOutput(outputList, showerror);
const bool hasError = reportOutput(outputList, showerror);
if (throwError) {
const auto it = std::find_if(outputList.cbegin(), outputList.cend(), [](const simplecpp::Output &output){
return hasErrors(output);
Expand All @@ -773,6 +765,7 @@ void Preprocessor::handleErrors(const simplecpp::OutputList& outputList, bool th
throw *it;
}
}
return hasError;
}

bool Preprocessor::loadFiles(std::vector<std::string> &files)
Expand All @@ -781,8 +774,7 @@ bool Preprocessor::loadFiles(std::vector<std::string> &files)

simplecpp::OutputList outputList;
mFileCache = simplecpp::load(mTokens, files, dui, &outputList);
handleErrors(outputList, false);
return !hasErrors(outputList);
return !handleErrors(outputList, false);
}

void Preprocessor::removeComments()
Expand Down Expand Up @@ -825,7 +817,7 @@ simplecpp::TokenList Preprocessor::preprocess(const std::string &cfg, std::vecto
mMacroUsage = std::move(macroUsage);
mIfCond = std::move(ifCond);

handleErrors(outputList, throwError);
(void)handleErrors(outputList, throwError);

tokens2.removeComments();

Expand Down Expand Up @@ -859,18 +851,22 @@ std::string Preprocessor::getcode(const std::string &cfg, std::vector<std::strin
return ret.str();
}

void Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool showerror)
bool Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool showerror)
{
bool hasError = false;

for (const simplecpp::Output &out : outputList) {
switch (out.type) {
case simplecpp::Output::ERROR:
hasError = true;
if (!startsWith(out.msg,"#error") || showerror)
error(out.location.file(), out.location.line, out.msg);
break;
case simplecpp::Output::WARNING:
case simplecpp::Output::PORTABILITY_BACKSLASH:
break;
case simplecpp::Output::MISSING_HEADER: {
// not considered an "error"
const std::string::size_type pos1 = out.msg.find_first_of("<\"");
const std::string::size_type pos2 = out.msg.find_first_of(">\"", pos1 + 1U);
if (pos1 < pos2 && pos2 != std::string::npos)
Expand All @@ -880,15 +876,19 @@ void Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool sh
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
case simplecpp::Output::SYNTAX_ERROR:
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
hasError = true;
error(out.location.file(), out.location.line, out.msg);
break;
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
case simplecpp::Output::FILE_NOT_FOUND:
case simplecpp::Output::DUI_ERROR:
hasError = true;
error("", 0, out.msg);
break;
}
}

return hasError;
}

void Preprocessor::error(const std::string &filename, unsigned int linenr, const std::string &msg)
Expand Down
6 changes: 2 additions & 4 deletions lib/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,10 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
static bool hasErrors(const simplecpp::Output &output);

protected:
void reportOutput(const simplecpp::OutputList &outputList, bool showerror);

static bool hasErrors(const simplecpp::OutputList &outputList);
bool reportOutput(const simplecpp::OutputList &outputList, bool showerror);

private:
void handleErrors(const simplecpp::OutputList &outputList, bool throwError);
bool handleErrors(const simplecpp::OutputList &outputList, bool throwError);

static void simplifyPragmaAsmPrivate(simplecpp::TokenList &tokenList);

Expand Down
12 changes: 6 additions & 6 deletions test/testpreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class TestPreprocessor : public TestFixture {
simplecpp::TokenList tokens1 = simplecpp::TokenList(code, files, "file.cpp", &outputList);
PreprocessorTest p(tokens1, settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false));
simplecpp::TokenList tokens2 = p.preprocess("", files, true);
p.reportOutput(outputList, true);
(void)p.reportOutput(outputList, true);
return tokens2.stringify();
}

Expand Down Expand Up @@ -129,16 +129,16 @@ class TestPreprocessor : public TestFixture {
simplecpp::TokenList tokens(code, size, files, Path::simplifyPath(filename), &outputList);
// TODO: we should be using the actual Preprocessor implementation
PreprocessorTest preprocessor(tokens, settings, errorlogger, Path::identify(tokens.getFiles()[0], false));

// TODO: should be possible without a Preprocessor instance
if (preprocessor.reportOutput(outputList, true))
return {};

if (inlineSuppression)
preprocessor.inlineSuppressions(*inlineSuppression);
preprocessor.removeComments();
preprocessor.simplifyPragmaAsm();

preprocessor.reportOutput(outputList, true);

if (PreprocessorTest::hasErrors(outputList))
return {};

std::map<std::string, std::string> cfgcode;
if (cfgs.empty())
cfgs = preprocessor.getConfigs();
Expand Down