avoid redundant `simplecpp::Output::type` switch blocks (#5005)

* avoid redundant `simplecpp::Output::type` switch blocks

* fixed `useStlAlgorithm` warnings
This commit is contained in:
Oliver Stöneberg 2023-04-28 15:37:59 +02:00 committed by GitHub
parent 9239549598
commit c249cc9098
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 61 deletions

View File

@ -666,40 +666,26 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
simplecpp::TokenList tokens1(fileStream, files, filename, &outputList);
// If there is a syntax error, report it and stop
for (const simplecpp::Output &output : outputList) {
bool err;
switch (output.type) {
case simplecpp::Output::ERROR:
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
case simplecpp::Output::SYNTAX_ERROR:
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
err = true;
break;
case simplecpp::Output::WARNING:
case simplecpp::Output::MISSING_HEADER:
case simplecpp::Output::PORTABILITY_BACKSLASH:
err = false;
break;
}
const auto output_it = std::find_if(outputList.cbegin(), outputList.cend(), [](const simplecpp::Output &output){
return Preprocessor::hasErrors(output);
});
if (output_it != outputList.cend()) {
const simplecpp::Output &output = *output_it;
std::string file = Path::fromNativeSeparators(output.location.file());
if (mSettings.relativePaths)
file = Path::getRelativePath(file, mSettings.basePaths);
if (err) {
std::string file = Path::fromNativeSeparators(output.location.file());
if (mSettings.relativePaths)
file = Path::getRelativePath(file, mSettings.basePaths);
const ErrorMessage::FileLocation loc1(file, output.location.line, output.location.col);
std::list<ErrorMessage::FileLocation> callstack(1, loc1);
const ErrorMessage::FileLocation loc1(file, output.location.line, output.location.col);
std::list<ErrorMessage::FileLocation> callstack(1, loc1);
ErrorMessage errmsg(callstack,
"",
Severity::error,
output.msg,
"syntaxError",
Certainty::normal);
reportErr(errmsg);
return mExitCode;
}
ErrorMessage errmsg(callstack,
"",
Severity::error,
output.msg,
"syntaxError",
Certainty::normal);
reportErr(errmsg);
return mExitCode;
}
if (!preprocessor.loadFiles(tokens1, files))
@ -797,10 +783,10 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
}
// Run define rules on raw code
const auto it = std::find_if(mSettings.rules.cbegin(), mSettings.rules.cend(), [](const Settings::Rule& rule) {
const auto rules_it = std::find_if(mSettings.rules.cbegin(), mSettings.rules.cend(), [](const Settings::Rule& rule) {
return rule.tokenlist == "define";
});
if (it != mSettings.rules.cend()) {
if (rules_it != mSettings.rules.cend()) {
std::string code;
const std::list<Directive> &directives = preprocessor.getDirectives();
for (const Directive &dir : directives) {

View File

@ -627,43 +627,41 @@ static simplecpp::DUI createDUI(const Settings &mSettings, const std::string &cf
return dui;
}
bool Preprocessor::hasErrors(const simplecpp::OutputList &outputList)
bool Preprocessor::hasErrors(const simplecpp::Output &output)
{
for (simplecpp::OutputList::const_iterator it = outputList.cbegin(); it != outputList.cend(); ++it) {
switch (it->type) {
case simplecpp::Output::ERROR:
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
case simplecpp::Output::SYNTAX_ERROR:
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
return true;
case simplecpp::Output::WARNING:
case simplecpp::Output::MISSING_HEADER:
case simplecpp::Output::PORTABILITY_BACKSLASH:
break;
}
switch (output.type) {
case simplecpp::Output::ERROR:
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
case simplecpp::Output::SYNTAX_ERROR:
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
return true;
case simplecpp::Output::WARNING:
case simplecpp::Output::MISSING_HEADER:
case simplecpp::Output::PORTABILITY_BACKSLASH:
break;
}
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)
{
const bool showerror = (!mSettings.userDefines.empty() && !mSettings.force);
reportOutput(outputList, showerror);
if (throwError) {
for (const simplecpp::Output& output : outputList) {
switch (output.type) {
case simplecpp::Output::ERROR:
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
case simplecpp::Output::SYNTAX_ERROR:
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
throw output;
case simplecpp::Output::WARNING:
case simplecpp::Output::MISSING_HEADER:
case simplecpp::Output::PORTABILITY_BACKSLASH:
break;
}
const auto it = std::find_if(outputList.cbegin(), outputList.cend(), [](const simplecpp::Output &output){
return hasErrors(output);
});
if (it != outputList.cend()) {
throw *it;
}
}
}

View File

@ -180,6 +180,8 @@ public:
void reportOutput(const simplecpp::OutputList &outputList, bool showerror);
static bool hasErrors(const simplecpp::Output &output);
private:
void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType);
void error(const std::string &filename, unsigned int linenr, const std::string &msg);