Skip to content

Commit c249cc9

Browse files
authored
avoid redundant simplecpp::Output::type switch blocks (#5005)
* avoid redundant `simplecpp::Output::type` switch blocks * fixed `useStlAlgorithm` warnings
1 parent 9239549 commit c249cc9

File tree

3 files changed

+50
-64
lines changed

3 files changed

+50
-64
lines changed

lib/cppcheck.cpp

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -666,40 +666,26 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
666666
simplecpp::TokenList tokens1(fileStream, files, filename, &outputList);
667667

668668
// If there is a syntax error, report it and stop
669-
for (const simplecpp::Output &output : outputList) {
670-
bool err;
671-
switch (output.type) {
672-
case simplecpp::Output::ERROR:
673-
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
674-
case simplecpp::Output::SYNTAX_ERROR:
675-
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
676-
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
677-
err = true;
678-
break;
679-
case simplecpp::Output::WARNING:
680-
case simplecpp::Output::MISSING_HEADER:
681-
case simplecpp::Output::PORTABILITY_BACKSLASH:
682-
err = false;
683-
break;
684-
}
685-
686-
if (err) {
687-
std::string file = Path::fromNativeSeparators(output.location.file());
688-
if (mSettings.relativePaths)
689-
file = Path::getRelativePath(file, mSettings.basePaths);
690-
691-
const ErrorMessage::FileLocation loc1(file, output.location.line, output.location.col);
692-
std::list<ErrorMessage::FileLocation> callstack(1, loc1);
693-
694-
ErrorMessage errmsg(callstack,
695-
"",
696-
Severity::error,
697-
output.msg,
698-
"syntaxError",
699-
Certainty::normal);
700-
reportErr(errmsg);
701-
return mExitCode;
702-
}
669+
const auto output_it = std::find_if(outputList.cbegin(), outputList.cend(), [](const simplecpp::Output &output){
670+
return Preprocessor::hasErrors(output);
671+
});
672+
if (output_it != outputList.cend()) {
673+
const simplecpp::Output &output = *output_it;
674+
std::string file = Path::fromNativeSeparators(output.location.file());
675+
if (mSettings.relativePaths)
676+
file = Path::getRelativePath(file, mSettings.basePaths);
677+
678+
const ErrorMessage::FileLocation loc1(file, output.location.line, output.location.col);
679+
std::list<ErrorMessage::FileLocation> callstack(1, loc1);
680+
681+
ErrorMessage errmsg(callstack,
682+
"",
683+
Severity::error,
684+
output.msg,
685+
"syntaxError",
686+
Certainty::normal);
687+
reportErr(errmsg);
688+
return mExitCode;
703689
}
704690

705691
if (!preprocessor.loadFiles(tokens1, files))
@@ -797,10 +783,10 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
797783
}
798784

799785
// Run define rules on raw code
800-
const auto it = std::find_if(mSettings.rules.cbegin(), mSettings.rules.cend(), [](const Settings::Rule& rule) {
786+
const auto rules_it = std::find_if(mSettings.rules.cbegin(), mSettings.rules.cend(), [](const Settings::Rule& rule) {
801787
return rule.tokenlist == "define";
802788
});
803-
if (it != mSettings.rules.cend()) {
789+
if (rules_it != mSettings.rules.cend()) {
804790
std::string code;
805791
const std::list<Directive> &directives = preprocessor.getDirectives();
806792
for (const Directive &dir : directives) {

lib/preprocessor.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -627,43 +627,41 @@ static simplecpp::DUI createDUI(const Settings &mSettings, const std::string &cf
627627
return dui;
628628
}
629629

630-
bool Preprocessor::hasErrors(const simplecpp::OutputList &outputList)
631-
{
632-
for (simplecpp::OutputList::const_iterator it = outputList.cbegin(); it != outputList.cend(); ++it) {
633-
switch (it->type) {
634-
case simplecpp::Output::ERROR:
635-
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
636-
case simplecpp::Output::SYNTAX_ERROR:
637-
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
638-
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
639-
return true;
640-
case simplecpp::Output::WARNING:
641-
case simplecpp::Output::MISSING_HEADER:
642-
case simplecpp::Output::PORTABILITY_BACKSLASH:
643-
break;
644-
}
630+
bool Preprocessor::hasErrors(const simplecpp::Output &output)
631+
{
632+
switch (output.type) {
633+
case simplecpp::Output::ERROR:
634+
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
635+
case simplecpp::Output::SYNTAX_ERROR:
636+
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
637+
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
638+
return true;
639+
case simplecpp::Output::WARNING:
640+
case simplecpp::Output::MISSING_HEADER:
641+
case simplecpp::Output::PORTABILITY_BACKSLASH:
642+
break;
645643
}
646644
return false;
647645
}
648646

647+
bool Preprocessor::hasErrors(const simplecpp::OutputList &outputList)
648+
{
649+
const auto it = std::find_if(outputList.cbegin(), outputList.cend(), [](const simplecpp::Output &output) {
650+
return hasErrors(output);
651+
});
652+
return it != outputList.cend();
653+
}
654+
649655
void Preprocessor::handleErrors(const simplecpp::OutputList& outputList, bool throwError)
650656
{
651657
const bool showerror = (!mSettings.userDefines.empty() && !mSettings.force);
652658
reportOutput(outputList, showerror);
653659
if (throwError) {
654-
for (const simplecpp::Output& output : outputList) {
655-
switch (output.type) {
656-
case simplecpp::Output::ERROR:
657-
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
658-
case simplecpp::Output::SYNTAX_ERROR:
659-
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
660-
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
661-
throw output;
662-
case simplecpp::Output::WARNING:
663-
case simplecpp::Output::MISSING_HEADER:
664-
case simplecpp::Output::PORTABILITY_BACKSLASH:
665-
break;
666-
}
660+
const auto it = std::find_if(outputList.cbegin(), outputList.cend(), [](const simplecpp::Output &output){
661+
return hasErrors(output);
662+
});
663+
if (it != outputList.cend()) {
664+
throw *it;
667665
}
668666
}
669667
}

lib/preprocessor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ class CPPCHECKLIB Preprocessor {
180180

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

183+
static bool hasErrors(const simplecpp::Output &output);
184+
183185
private:
184186
void missingInclude(const std::string &filename, unsigned int linenr, const std::string &header, HeaderTypes headerType);
185187
void error(const std::string &filename, unsigned int linenr, const std::string &msg);

0 commit comments

Comments
 (0)