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

View File

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

View File

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