From 8c0d43d928f306608f9bf48ace05527353cfb1e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Thu, 12 Oct 2023 11:58:39 +0200 Subject: [PATCH] removed unnecessary encapsulation of severity enum and made it an `enum class` (#5541) --- cli/cmdlineparser.cpp | 2 +- gui/checkthread.cpp | 12 ++--- gui/erroritem.h | 12 ++--- gui/resultstree.cpp | 6 +-- gui/resultstree.h | 4 +- gui/showtypes.cpp | 6 +-- gui/showtypes.h | 6 +-- lib/check.cpp | 4 +- lib/check.h | 10 ++-- lib/checkio.cpp | 22 ++++---- lib/checkio.h | 2 +- lib/checkmemoryleak.cpp | 4 +- lib/checkmemoryleak.h | 4 +- lib/checktype.cpp | 2 +- lib/cppcheck.cpp | 12 ++--- lib/errorlogger.cpp | 24 ++++----- lib/errorlogger.h | 12 ++--- lib/errortypes.cpp | 41 +++++++-------- lib/errortypes.h | 108 ++++++++++++++++++--------------------- lib/library.cpp | 2 +- lib/library.h | 2 +- lib/settings.cpp | 10 ++-- lib/settings.h | 6 +-- lib/tokenize.cpp | 4 +- lib/tokenize.h | 4 +- test/fixture.h | 2 +- test/testerrorlogger.cpp | 6 +-- test/testlibrary.cpp | 4 +- 28 files changed, 164 insertions(+), 169 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index eb330b82f84..1e18eac06c5 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -791,7 +791,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) if (message) { const tinyxml2::XMLElement *severity = message->FirstChildElement("severity"); if (severity) - rule.severity = Severity::fromString(severity->GetText()); + rule.severity = severityFromString(severity->GetText()); const tinyxml2::XMLElement *id = message->FirstChildElement("id"); if (id) diff --git a/gui/checkthread.cpp b/gui/checkthread.cpp index 2f79d1b7b31..988a2ab4b86 100644 --- a/gui/checkthread.cpp +++ b/gui/checkthread.cpp @@ -347,9 +347,9 @@ void CheckThread::parseClangErrors(const QString &tool, const QString &file0, QS errorItem.errorPath.last().line = r1MatchRes.captured(2).toInt(); errorItem.errorPath.last().column = r1MatchRes.captured(3).toInt(); if (r1MatchRes.captured(4) == "warning") - errorItem.severity = Severity::SeverityType::warning; + errorItem.severity = Severity::warning; else if (r1MatchRes.captured(4) == "error" || r1MatchRes.captured(4) == "fatal error") - errorItem.severity = Severity::SeverityType::error; + errorItem.severity = Severity::error; QString message,id; const QRegularExpressionMatch r2MatchRes = r2.match(r1MatchRes.captured(5)); @@ -362,13 +362,13 @@ void CheckThread::parseClangErrors(const QString &tool, const QString &file0, QS id = tool + '-' + r2MatchRes.captured(2); if (tool == CLANG_TIDY) { if (id1.startsWith("performance")) - errorItem.severity = Severity::SeverityType::performance; + errorItem.severity = Severity::performance; else if (id1.startsWith("portability")) - errorItem.severity = Severity::SeverityType::portability; + errorItem.severity = Severity::portability; else if (id1.startsWith("misc") && !id1.contains("unused")) - errorItem.severity = Severity::SeverityType::warning; + errorItem.severity = Severity::warning; else - errorItem.severity = Severity::SeverityType::style; + errorItem.severity = Severity::style; } } else { message = r1MatchRes.captured(5); diff --git a/gui/erroritem.h b/gui/erroritem.h index 5605f7ea6cb..507d7286df5 100644 --- a/gui/erroritem.h +++ b/gui/erroritem.h @@ -37,12 +37,12 @@ */ class GuiSeverity { public: - static QString toString(Severity::SeverityType severity) { - return QString::fromStdString(Severity::toString(severity)); + static QString toString(Severity severity) { + return QString::fromStdString(severityToString(severity)); } - static Severity::SeverityType fromString(const QString &severity) { - return Severity::fromString(severity.toStdString()); + static Severity fromString(const QString &severity) { + return severityFromString(severity.toStdString()); } }; @@ -83,7 +83,7 @@ class ErrorItem { QString file0; QString errorId; - Severity::SeverityType severity; + Severity severity; bool inconclusive; QString summary; QString message; @@ -117,7 +117,7 @@ class ErrorLine { int cwe; unsigned long long hash; bool inconclusive; - Severity::SeverityType severity; + Severity severity; QString summary; QString message; QString sinceDate; diff --git a/gui/resultstree.cpp b/gui/resultstree.cpp index ca4b6ec0bc1..b7ac15602b8 100644 --- a/gui/resultstree.cpp +++ b/gui/resultstree.cpp @@ -319,7 +319,7 @@ QStandardItem *ResultsTree::addBacktraceFiles(QStandardItem *parent, return list[0]; } -QString ResultsTree::severityToTranslatedString(Severity::SeverityType severity) +QString ResultsTree::severityToTranslatedString(Severity severity) { switch (severity) { case Severity::style: @@ -901,7 +901,7 @@ void ResultsTree::copy() QString inconclusive = data[INCONCLUSIVE].toBool() ? ",inconclusive" : ""; text += '[' + data[FILENAME].toString() + ':' + QString::number(data[LINE].toInt()) + "] (" - + QString::fromStdString(Severity::toString(ShowTypes::ShowTypeToSeverity((ShowTypes::ShowType)data[SEVERITY].toInt()))) + inconclusive + + QString::fromStdString(severityToString(ShowTypes::ShowTypeToSeverity((ShowTypes::ShowType)data[SEVERITY].toInt()))) + inconclusive + ") " + data[MESSAGE].toString() + " [" @@ -1155,7 +1155,7 @@ QString ResultsTree::getFilePath(const QStandardItem *target, bool fullPath) return QString(); } -QString ResultsTree::severityToIcon(Severity::SeverityType severity) +QString ResultsTree::severityToIcon(Severity severity) { switch (severity) { case Severity::error: diff --git a/gui/resultstree.h b/gui/resultstree.h index 23f7053b33a..53ba0906754 100644 --- a/gui/resultstree.h +++ b/gui/resultstree.h @@ -335,7 +335,7 @@ protected slots: * * @param severity Severity */ - static QString severityToIcon(Severity::SeverityType severity); + static QString severityToIcon(Severity severity); /** * @brief Helper function to open an error within target with application* @@ -382,7 +382,7 @@ protected slots: * @param severity Severity to convert * @return Severity as translated string */ - static QString severityToTranslatedString(Severity::SeverityType severity); + static QString severityToTranslatedString(Severity severity); /** * @brief Load all settings diff --git a/gui/showtypes.cpp b/gui/showtypes.cpp index 03d61c47ebf..d59b708438f 100644 --- a/gui/showtypes.cpp +++ b/gui/showtypes.cpp @@ -32,7 +32,7 @@ ShowTypes::~ShowTypes() save(); } -ShowTypes::ShowType ShowTypes::SeverityToShowType(Severity::SeverityType severity) +ShowTypes::ShowType ShowTypes::SeverityToShowType(Severity severity) { switch (severity) { case Severity::none: @@ -54,7 +54,7 @@ ShowTypes::ShowType ShowTypes::SeverityToShowType(Severity::SeverityType severit } } -Severity::SeverityType ShowTypes::ShowTypeToSeverity(ShowTypes::ShowType type) +Severity ShowTypes::ShowTypeToSeverity(ShowTypes::ShowType type) { switch (type) { case ShowTypes::ShowStyle: @@ -117,7 +117,7 @@ bool ShowTypes::isShown(ShowTypes::ShowType category) const return mVisible[category]; } -bool ShowTypes::isShown(Severity::SeverityType severity) const +bool ShowTypes::isShown(Severity severity) const { return isShown(ShowTypes::SeverityToShowType(severity)); } diff --git a/gui/showtypes.h b/gui/showtypes.h index d1e98336152..3cff501cf3f 100644 --- a/gui/showtypes.h +++ b/gui/showtypes.h @@ -85,7 +85,7 @@ class ShowTypes { * @param severity severity to check. * @return true if the severity is visible. */ - bool isShown(Severity::SeverityType severity) const; + bool isShown(Severity severity) const; /** * @brief Show/hide the showtype. @@ -99,14 +99,14 @@ class ShowTypes { * @param severity Error severity * @return Severity converted to ShowTypes value */ - static ShowTypes::ShowType SeverityToShowType(Severity::SeverityType severity); + static ShowTypes::ShowType SeverityToShowType(Severity severity); /** * @brief Convert ShowType to severity string * @param type ShowType to convert * @return ShowType converted to severity */ - static Severity::SeverityType ShowTypeToSeverity(ShowTypes::ShowType type); + static Severity ShowTypeToSeverity(ShowTypes::ShowType type); /** * @brief Convert QVariant (that contains an int) to Showtypes value diff --git a/lib/check.cpp b/lib/check.cpp index 114ca8d71cc..53b4e5fed34 100644 --- a/lib/check.cpp +++ b/lib/check.cpp @@ -61,7 +61,7 @@ void Check::writeToErrorList(const ErrorMessage &errmsg) } -void Check::reportError(const std::list &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty) +void Check::reportError(const std::list &callstack, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty) { const ErrorMessage errmsg(callstack, mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty); if (mErrorLogger) @@ -70,7 +70,7 @@ void Check::reportError(const std::list &callstack, Severity::Sev writeToErrorList(errmsg); } -void Check::reportError(const ErrorPath &errorPath, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty) +void Check::reportError(const ErrorPath &errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty) { const ErrorMessage errmsg(errorPath, mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty); if (mErrorLogger) diff --git a/lib/check.h b/lib/check.h index e98d1c7e31d..91930a37450 100644 --- a/lib/check.h +++ b/lib/check.h @@ -137,25 +137,25 @@ class CPPCHECKLIB Check { ErrorLogger* const mErrorLogger{}; /** report an error */ - void reportError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg) { + void reportError(const Token *tok, const Severity severity, const std::string &id, const std::string &msg) { reportError(tok, severity, id, msg, CWE(0U), Certainty::normal); } /** report an error */ - void reportError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty) { + void reportError(const Token *tok, const Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty) { const std::list callstack(1, tok); reportError(callstack, severity, id, msg, cwe, certainty); } /** report an error */ - void reportError(const std::list &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg) { + void reportError(const std::list &callstack, Severity severity, const std::string &id, const std::string &msg) { reportError(callstack, severity, id, msg, CWE(0U), Certainty::normal); } /** report an error */ - void reportError(const std::list &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty); + void reportError(const std::list &callstack, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty); - void reportError(const ErrorPath &errorPath, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty); + void reportError(const ErrorPath &errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty); /** log checker */ void logChecker(const char id[]); diff --git a/lib/checkio.cpp b/lib/checkio.cpp index e52f19871b4..583bbc23e0b 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -1715,7 +1715,7 @@ void CheckIO::wrongPrintfScanfArgumentsError(const Token* tok, nonneg int numFormat, nonneg int numFunction) { - const Severity::SeverityType severity = numFormat > numFunction ? Severity::error : Severity::warning; + const Severity severity = numFormat > numFunction ? Severity::error : Severity::warning; if (severity != Severity::error && !mSettings->severity.isEnabled(Severity::warning)) return; @@ -1749,7 +1749,7 @@ void CheckIO::wrongPrintfScanfPosixParameterPositionError(const Token* tok, cons void CheckIO::invalidScanfArgTypeError_s(const Token* tok, nonneg int numFormat, const std::string& specifier, const ArgumentInfo* argInfo) { - const Severity::SeverityType severity = getSeverity(argInfo); + const Severity severity = getSeverity(argInfo); if (!mSettings->severity.isEnabled(severity)) return; std::ostringstream errmsg; @@ -1765,7 +1765,7 @@ void CheckIO::invalidScanfArgTypeError_s(const Token* tok, nonneg int numFormat, } void CheckIO::invalidScanfArgTypeError_int(const Token* tok, nonneg int numFormat, const std::string& specifier, const ArgumentInfo* argInfo, bool isUnsigned) { - const Severity::SeverityType severity = getSeverity(argInfo); + const Severity severity = getSeverity(argInfo); if (!mSettings->severity.isEnabled(severity)) return; std::ostringstream errmsg; @@ -1810,7 +1810,7 @@ void CheckIO::invalidScanfArgTypeError_int(const Token* tok, nonneg int numForma } void CheckIO::invalidScanfArgTypeError_float(const Token* tok, nonneg int numFormat, const std::string& specifier, const ArgumentInfo* argInfo) { - const Severity::SeverityType severity = getSeverity(argInfo); + const Severity severity = getSeverity(argInfo); if (!mSettings->severity.isEnabled(severity)) return; std::ostringstream errmsg; @@ -1829,7 +1829,7 @@ void CheckIO::invalidScanfArgTypeError_float(const Token* tok, nonneg int numFor void CheckIO::invalidPrintfArgTypeError_s(const Token* tok, nonneg int numFormat, const ArgumentInfo* argInfo) { - const Severity::SeverityType severity = getSeverity(argInfo); + const Severity severity = getSeverity(argInfo); if (!mSettings->severity.isEnabled(severity)) return; std::ostringstream errmsg; @@ -1840,7 +1840,7 @@ void CheckIO::invalidPrintfArgTypeError_s(const Token* tok, nonneg int numFormat } void CheckIO::invalidPrintfArgTypeError_n(const Token* tok, nonneg int numFormat, const ArgumentInfo* argInfo) { - const Severity::SeverityType severity = getSeverity(argInfo); + const Severity severity = getSeverity(argInfo); if (!mSettings->severity.isEnabled(severity)) return; std::ostringstream errmsg; @@ -1851,7 +1851,7 @@ void CheckIO::invalidPrintfArgTypeError_n(const Token* tok, nonneg int numFormat } void CheckIO::invalidPrintfArgTypeError_p(const Token* tok, nonneg int numFormat, const ArgumentInfo* argInfo) { - const Severity::SeverityType severity = getSeverity(argInfo); + const Severity severity = getSeverity(argInfo); if (!mSettings->severity.isEnabled(severity)) return; std::ostringstream errmsg; @@ -1901,7 +1901,7 @@ static void printfFormatType(std::ostream& os, const std::string& specifier, boo void CheckIO::invalidPrintfArgTypeError_uint(const Token* tok, nonneg int numFormat, const std::string& specifier, const ArgumentInfo* argInfo) { - const Severity::SeverityType severity = getSeverity(argInfo); + const Severity severity = getSeverity(argInfo); if (!mSettings->severity.isEnabled(severity)) return; std::ostringstream errmsg; @@ -1915,7 +1915,7 @@ void CheckIO::invalidPrintfArgTypeError_uint(const Token* tok, nonneg int numFor void CheckIO::invalidPrintfArgTypeError_sint(const Token* tok, nonneg int numFormat, const std::string& specifier, const ArgumentInfo* argInfo) { - const Severity::SeverityType severity = getSeverity(argInfo); + const Severity severity = getSeverity(argInfo); if (!mSettings->severity.isEnabled(severity)) return; std::ostringstream errmsg; @@ -1928,7 +1928,7 @@ void CheckIO::invalidPrintfArgTypeError_sint(const Token* tok, nonneg int numFor } void CheckIO::invalidPrintfArgTypeError_float(const Token* tok, nonneg int numFormat, const std::string& specifier, const ArgumentInfo* argInfo) { - const Severity::SeverityType severity = getSeverity(argInfo); + const Severity severity = getSeverity(argInfo); if (!mSettings->severity.isEnabled(severity)) return; std::ostringstream errmsg; @@ -1941,7 +1941,7 @@ void CheckIO::invalidPrintfArgTypeError_float(const Token* tok, nonneg int numFo reportError(tok, severity, "invalidPrintfArgType_float", errmsg.str(), CWE686, Certainty::normal); } -Severity::SeverityType CheckIO::getSeverity(const CheckIO::ArgumentInfo *argInfo) +Severity CheckIO::getSeverity(const CheckIO::ArgumentInfo *argInfo) { return (argInfo && argInfo->typeToken && !argInfo->typeToken->originalName().empty()) ? Severity::portability : Severity::warning; } diff --git a/lib/checkio.h b/lib/checkio.h index 8fb109283b3..3e9d15bb972 100644 --- a/lib/checkio.h +++ b/lib/checkio.h @@ -132,7 +132,7 @@ class CPPCHECKLIB CheckIO : public Check { void invalidLengthModifierError(const Token* tok, nonneg int numFormat, const std::string& modifier); void invalidScanfFormatWidthError(const Token* tok, nonneg int numFormat, int width, const Variable *var, const std::string& specifier); static void argumentType(std::ostream & os, const ArgumentInfo * argInfo); - static Severity::SeverityType getSeverity(const ArgumentInfo *argInfo); + static Severity getSeverity(const ArgumentInfo *argInfo); void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override { CheckIO c(nullptr, settings, errorLogger); diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 553e91bc13a..57aacdd2faa 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -279,7 +279,7 @@ void CheckMemoryLeak::memoryLeak(const Token *tok, const std::string &varname, A } //--------------------------------------------------------------------------- -void CheckMemoryLeak::reportErr(const Token *tok, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe) const +void CheckMemoryLeak::reportErr(const Token *tok, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe) const { std::list callstack; @@ -289,7 +289,7 @@ void CheckMemoryLeak::reportErr(const Token *tok, Severity::SeverityType severit reportErr(callstack, severity, id, msg, cwe); } -void CheckMemoryLeak::reportErr(const std::list &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe) const +void CheckMemoryLeak::reportErr(const std::list &callstack, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe) const { const ErrorMessage errmsg(callstack, mTokenizer_ ? &mTokenizer_->list : nullptr, severity, id, msg, cwe, Certainty::normal); if (mErrorLogger_) diff --git a/lib/checkmemoryleak.h b/lib/checkmemoryleak.h index c406ffccfff..ef8e0564c66 100644 --- a/lib/checkmemoryleak.h +++ b/lib/checkmemoryleak.h @@ -70,7 +70,7 @@ class CPPCHECKLIB CheckMemoryLeak { * @param msg text * @param cwe cwe number */ - void reportErr(const Token *tok, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe) const; + void reportErr(const Token *tok, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe) const; /** * Report error. Similar with the function Check::reportError @@ -80,7 +80,7 @@ class CPPCHECKLIB CheckMemoryLeak { * @param msg text * @param cwe cwe number */ - void reportErr(const std::list &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe) const; + void reportErr(const std::list &callstack, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe) const; public: CheckMemoryLeak() = delete; diff --git a/lib/checktype.cpp b/lib/checktype.cpp index 5ed81eb3213..20338d19dc8 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -144,7 +144,7 @@ void CheckType::tooBigSignedBitwiseShiftError(const Token *tok, int lhsbits, con } - Severity::SeverityType severity = rhsbits.errorSeverity() ? Severity::error : Severity::warning; + Severity severity = rhsbits.errorSeverity() ? Severity::error : Severity::warning; if (cpp14) severity = Severity::portability; diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 35fa7363dd3..5a9e5fe2200 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -1429,8 +1429,8 @@ void CppCheck::executeAddons(const std::vector& files) const std::string text = obj["message"].get(); errmsg.setmsg(text); const std::string severity = obj["severity"].get(); - errmsg.severity = Severity::fromString(severity); - if (errmsg.severity == Severity::SeverityType::none) { + errmsg.severity = severityFromString(severity); + if (errmsg.severity == Severity::none) { if (!endsWith(errmsg.id, "-logChecker")) continue; } @@ -1672,13 +1672,13 @@ void CppCheck::analyseClangTidy(const ImportProject::FileSettings &fileSettings) errmsg.id = "clang-tidy-" + errorString.substr(1, errorString.length() - 2); if (errmsg.id.find("performance") != std::string::npos) - errmsg.severity = Severity::SeverityType::performance; + errmsg.severity = Severity::performance; else if (errmsg.id.find("portability") != std::string::npos) - errmsg.severity = Severity::SeverityType::portability; + errmsg.severity = Severity::portability; else if (errmsg.id.find("cert") != std::string::npos || errmsg.id.find("misc") != std::string::npos || errmsg.id.find("unused") != std::string::npos) - errmsg.severity = Severity::SeverityType::warning; + errmsg.severity = Severity::warning; else - errmsg.severity = Severity::SeverityType::style; + errmsg.severity = Severity::style; errmsg.file0 = fixedpath; errmsg.setmsg(messageString); diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 91be7c24dc1..ccaf478074b 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -58,7 +58,7 @@ ErrorMessage::ErrorMessage() {} // TODO: id and msg are swapped compared to other calls -ErrorMessage::ErrorMessage(std::list callStack, std::string file1, Severity::SeverityType severity, const std::string &msg, std::string id, Certainty certainty) : +ErrorMessage::ErrorMessage(std::list callStack, std::string file1, Severity severity, const std::string &msg, std::string id, Certainty certainty) : callStack(std::move(callStack)), // locations for this error message id(std::move(id)), // set the message id file0(std::move(file1)), @@ -73,7 +73,7 @@ ErrorMessage::ErrorMessage(std::list callStack, std::string file1, // TODO: id and msg are swapped compared to other calls -ErrorMessage::ErrorMessage(std::list callStack, std::string file1, Severity::SeverityType severity, const std::string &msg, std::string id, const CWE &cwe, Certainty certainty) : +ErrorMessage::ErrorMessage(std::list callStack, std::string file1, Severity severity, const std::string &msg, std::string id, const CWE &cwe, Certainty certainty) : callStack(std::move(callStack)), // locations for this error message id(std::move(id)), // set the message id file0(std::move(file1)), @@ -86,7 +86,7 @@ ErrorMessage::ErrorMessage(std::list callStack, std::string file1, setmsg(msg); } -ErrorMessage::ErrorMessage(const std::list& callstack, const TokenList* list, Severity::SeverityType severity, std::string id, const std::string& msg, Certainty certainty) +ErrorMessage::ErrorMessage(const std::list& callstack, const TokenList* list, Severity severity, std::string id, const std::string& msg, Certainty certainty) : id(std::move(id)), severity(severity), cwe(0U), certainty(certainty), hash(0) { // Format callstack @@ -105,7 +105,7 @@ ErrorMessage::ErrorMessage(const std::list& callstack, const Token } -ErrorMessage::ErrorMessage(const std::list& callstack, const TokenList* list, Severity::SeverityType severity, std::string id, const std::string& msg, const CWE &cwe, Certainty certainty) +ErrorMessage::ErrorMessage(const std::list& callstack, const TokenList* list, Severity severity, std::string id, const std::string& msg, const CWE &cwe, Certainty certainty) : id(std::move(id)), severity(severity), cwe(cwe.id), certainty(certainty) { // Format callstack @@ -125,7 +125,7 @@ ErrorMessage::ErrorMessage(const std::list& callstack, const Token hash = 0; // calculateWarningHash(list, hashWarning.str()); } -ErrorMessage::ErrorMessage(const ErrorPath &errorPath, const TokenList *tokenList, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty) +ErrorMessage::ErrorMessage(const ErrorPath &errorPath, const TokenList *tokenList, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty) : id(id), severity(severity), cwe(cwe.id), certainty(certainty) { // Format callstack @@ -165,7 +165,7 @@ ErrorMessage::ErrorMessage(const tinyxml2::XMLElement * const errmsg) id = attr ? attr : unknown; attr = errmsg->Attribute("severity"); - severity = attr ? Severity::fromString(attr) : Severity::none; + severity = attr ? severityFromString(attr) : Severity::none; attr = errmsg->Attribute("cwe"); cwe.id = attr ? strToInt(attr) : 0; @@ -267,7 +267,7 @@ std::string ErrorMessage::serialize() const // Serialize this message into a simple string std::string oss; serializeString(oss, id); - serializeString(oss, Severity::toString(severity)); + serializeString(oss, severityToString(severity)); serializeString(oss, std::to_string(cwe.id)); serializeString(oss, std::to_string(hash)); serializeString(oss, file0); @@ -345,7 +345,7 @@ void ErrorMessage::deserialize(const std::string &data) throw InternalError(nullptr, "Internal Error: Deserialization of error message failed - insufficient elements"); id = std::move(results[0]); - severity = Severity::fromString(results[1]); + severity = severityFromString(results[1]); cwe.id = 0; if (!results[2].empty()) { std::string err; @@ -483,7 +483,7 @@ std::string ErrorMessage::toXML() const tinyxml2::XMLPrinter printer(nullptr, false, 2); printer.OpenElement("error", false); printer.PushAttribute("id", id.c_str()); - printer.PushAttribute("severity", Severity::toString(severity).c_str()); + printer.PushAttribute("severity", severityToString(severity).c_str()); printer.PushAttribute("msg", fixInvalidChars(mShortMessage).c_str()); printer.PushAttribute("verbose", fixInvalidChars(mVerboseMessage).c_str()); if (cwe.id) @@ -629,7 +629,7 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm } if (severity != Severity::none) { text += '('; - text += Severity::toString(severity); + text += severityToString(severity); if (certainty == Certainty::inconclusive) text += ", inconclusive"; text += ") "; @@ -651,7 +651,7 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm findAndReplace(result, replaceFrom, replaceWith); pos1 = result.find("{inconclusive:", pos1); } - findAndReplace(result, "{severity}", Severity::toString(severity)); + findAndReplace(result, "{severity}", severityToString(severity)); findAndReplace(result, "{cwe}", std::to_string(cwe.id)); findAndReplace(result, "{message}", verbose ? mVerboseMessage : mShortMessage); if (!callStack.empty()) { @@ -882,7 +882,7 @@ std::string ErrorLogger::plistData(const ErrorMessage &msg) plist << " \r\n" << " description" << ErrorLogger::toxml(msg.shortMessage()) << "\r\n" - << " category" << Severity::toString(msg.severity) << "\r\n" + << " category" << severityToString(msg.severity) << "\r\n" << " type" << ErrorLogger::toxml(msg.shortMessage()) << "\r\n" << " check_name" << msg.id << "\r\n" << " \r\n" diff --git a/lib/errorlogger.h b/lib/errorlogger.h index ccc569c8c38..44d9f3b8c62 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -110,32 +110,32 @@ class CPPCHECKLIB ErrorMessage { ErrorMessage(std::list callStack, std::string file1, - Severity::SeverityType severity, + Severity severity, const std::string &msg, std::string id, Certainty certainty); ErrorMessage(std::list callStack, std::string file1, - Severity::SeverityType severity, + Severity severity, const std::string &msg, std::string id, const CWE &cwe, Certainty certainty); ErrorMessage(const std::list& callstack, const TokenList* list, - Severity::SeverityType severity, + Severity severity, std::string id, const std::string& msg, Certainty certainty); ErrorMessage(const std::list& callstack, const TokenList* list, - Severity::SeverityType severity, + Severity severity, std::string id, const std::string& msg, const CWE &cwe, Certainty certainty); ErrorMessage(const ErrorPath &errorPath, const TokenList *tokenList, - Severity::SeverityType severity, + Severity severity, const char id[], const std::string &msg, const CWE &cwe, @@ -173,7 +173,7 @@ class CPPCHECKLIB ErrorMessage { /** For GUI rechecking; source file (not header) */ std::string file0; - Severity::SeverityType severity; + Severity severity; CWE cwe; Certainty certainty; diff --git a/lib/errortypes.cpp b/lib/errortypes.cpp index 1653df844bd..a7028d89be6 100644 --- a/lib/errortypes.cpp +++ b/lib/errortypes.cpp @@ -47,47 +47,48 @@ InternalError::InternalError(const Token *tok, std::string errorMsg, std::string token(tok), errorMessage(std::move(errorMsg)), details(std::move(details)), type(type), id(typeToString(type)) {} -std::string Severity::toString(Severity::SeverityType severity) +std::string severityToString(Severity severity) { switch (severity) { - case none: + case Severity::none: return ""; - case error: + case Severity::error: return "error"; - case warning: + case Severity::warning: return "warning"; - case style: + case Severity::style: return "style"; - case performance: + case Severity::performance: return "performance"; - case portability: + case Severity::portability: return "portability"; - case information: + case Severity::information: return "information"; - case debug: + case Severity::debug: return "debug"; } throw InternalError(nullptr, "Unknown severity"); } -Severity::SeverityType Severity::fromString(const std::string& severity) + +Severity severityFromString(const std::string& severity) { if (severity.empty()) - return none; + return Severity::none; if (severity == "none") - return none; + return Severity::none; if (severity == "error") - return error; + return Severity::error; if (severity == "warning") - return warning; + return Severity::warning; if (severity == "style") - return style; + return Severity::style; if (severity == "performance") - return performance; + return Severity::performance; if (severity == "portability") - return portability; + return Severity::portability; if (severity == "information") - return information; + return Severity::information; if (severity == "debug") - return debug; - return none; + return Severity::debug; + return Severity::none; } diff --git a/lib/errortypes.h b/lib/errortypes.h index 2bb3a32c5f8..ff83d6a4744 100644 --- a/lib/errortypes.h +++ b/lib/errortypes.h @@ -60,67 +60,61 @@ enum class Checks { }; /** @brief enum class for severity. Used when reporting errors. */ -class CPPCHECKLIB Severity { -public: +enum class Severity { /** - * Message severities. + * No severity (default value). */ - enum SeverityType { - /** - * No severity (default value). - */ - none, - /** - * Programming error. - * This indicates severe error like memory leak etc. - * The error is certain. - */ - error, - /** - * Warning. - * Used for dangerous coding style that can cause severe runtime errors. - * For example: forgetting to initialize a member variable in a constructor. - */ - warning, - /** - * Style warning. - * Used for general code cleanup recommendations. Fixing these - * will not fix any bugs but will make the code easier to maintain. - * For example: redundant code, unreachable code, etc. - */ - style, - /** - * Performance warning. - * Not an error as is but suboptimal code and fixing it probably leads - * to faster performance of the compiled code. - */ - performance, - /** - * Portability warning. - * This warning indicates the code is not properly portable for - * different platforms and bitnesses (32/64 bit). If the code is meant - * to compile in different platforms and bitnesses these warnings - * should be fixed. - */ - portability, - /** - * Checking information. - * Information message about the checking (process) itself. These - * messages inform about header files not found etc issues that are - * not errors in the code but something user needs to know. - */ - information, - /** - * Debug message. - * Debug-mode message useful for the developers. - */ - debug - }; - - static std::string toString(SeverityType severity); - static SeverityType fromString(const std::string &severity); + none, + /** + * Programming error. + * This indicates severe error like memory leak etc. + * The error is certain. + */ + error, + /** + * Warning. + * Used for dangerous coding style that can cause severe runtime errors. + * For example: forgetting to initialize a member variable in a constructor. + */ + warning, + /** + * Style warning. + * Used for general code cleanup recommendations. Fixing these + * will not fix any bugs but will make the code easier to maintain. + * For example: redundant code, unreachable code, etc. + */ + style, + /** + * Performance warning. + * Not an error as is but suboptimal code and fixing it probably leads + * to faster performance of the compiled code. + */ + performance, + /** + * Portability warning. + * This warning indicates the code is not properly portable for + * different platforms and bitnesses (32/64 bit). If the code is meant + * to compile in different platforms and bitnesses these warnings + * should be fixed. + */ + portability, + /** + * Checking information. + * Information message about the checking (process) itself. These + * messages inform about header files not found etc issues that are + * not errors in the code but something user needs to know. + */ + information, + /** + * Debug message. + * Debug-mode message useful for the developers. + */ + debug }; +CPPCHECKLIB std::string severityToString(Severity severity); +CPPCHECKLIB Severity severityFromString(const std::string &severity); + struct CWE { explicit CWE(unsigned short cweId) : id(cweId) {} unsigned short id; diff --git a/lib/library.cpp b/lib/library.cpp index b5699ff41d7..1bd06904d5a 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -837,7 +837,7 @@ Library::Error Library::loadFunction(const tinyxml2::XMLElement * const node, co const char* const severity = functionnode->Attribute("severity"); if (severity == nullptr) return Error(ErrorCode::MISSING_ATTRIBUTE, "severity"); - wi.severity = Severity::fromString(severity); + wi.severity = severityFromString(severity); const char* const cstd = functionnode->Attribute("cstd"); if (cstd) { diff --git a/lib/library.h b/lib/library.h index 7cd68a8f679..e45683a56f6 100644 --- a/lib/library.h +++ b/lib/library.h @@ -164,7 +164,7 @@ class CPPCHECKLIB Library { struct WarnInfo { std::string message; Standards standards; - Severity::SeverityType severity; + Severity severity; }; std::map functionwarn; diff --git a/lib/settings.cpp b/lib/settings.cpp index 18519bad216..c0cf5da27a3 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -75,7 +75,7 @@ void Settings::loadCppcheckCfg() } } -std::string Settings::parseEnabled(const std::string &str, std::tuple, SimpleEnableGroup> &groups) +std::string Settings::parseEnabled(const std::string &str, std::tuple, SimpleEnableGroup> &groups) { // Enable parameters may be comma separated... if (str.find(',') != std::string::npos) { @@ -100,9 +100,9 @@ std::string Settings::parseEnabled(const std::string &str, std::tuple newSeverity; + SimpleEnableGroup newSeverity; newSeverity.fill(); - newSeverity.disable(Severity::SeverityType::error); + newSeverity.disable(Severity::error); severity.enable(newSeverity); checks.enable(Checks::missingInclude); checks.enable(Checks::unusedFunction); @@ -148,7 +148,7 @@ std::string Settings::removeEnabled(const std::string &str) std::string Settings::applyEnabled(const std::string &str, bool enable) { - std::tuple, SimpleEnableGroup> groups; + std::tuple, SimpleEnableGroup> groups; std::string errmsg = parseEnabled(str, groups); if (!errmsg.empty()) return (enable ? "--enable" : "--disable") + errmsg; @@ -164,7 +164,7 @@ std::string Settings::applyEnabled(const std::string &str, bool enable) checks.disable(c); } // FIXME: hack to make sure "error" is always enabled - severity.enable(Severity::SeverityType::error); + severity.enable(Severity::error); return errmsg; } diff --git a/lib/settings.h b/lib/settings.h index 900fd61cf98..f5bf7a5d374 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -282,7 +282,7 @@ class CPPCHECKLIB WARN_UNUSED Settings { std::string pattern; std::string id = "rule"; // default id std::string summary; - Severity::SeverityType severity = Severity::style; // default severity + Severity severity = Severity::style; // default severity }; #ifdef HAVE_RULES @@ -335,7 +335,7 @@ class CPPCHECKLIB WARN_UNUSED Settings { SafeChecks safeChecks; - SimpleEnableGroup severity; + SimpleEnableGroup severity; SimpleEnableGroup certainty; SimpleEnableGroup checks; @@ -439,7 +439,7 @@ class CPPCHECKLIB WARN_UNUSED Settings { void setCheckLevelNormal(); private: - static std::string parseEnabled(const std::string &str, std::tuple, SimpleEnableGroup> &groups); + static std::string parseEnabled(const std::string &str, std::tuple, SimpleEnableGroup> &groups); std::string applyEnabled(const std::string &str, bool enable); }; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 7e319e4de15..4766f4add9f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -10274,13 +10274,13 @@ void Tokenizer::prepareTernaryOpForAST() } } -void Tokenizer::reportError(const Token* tok, const Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive) const +void Tokenizer::reportError(const Token* tok, const Severity severity, const std::string& id, const std::string& msg, bool inconclusive) const { const std::list callstack(1, tok); reportError(callstack, severity, id, msg, inconclusive); } -void Tokenizer::reportError(const std::list& callstack, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive) const +void Tokenizer::reportError(const std::list& callstack, Severity severity, const std::string& id, const std::string& msg, bool inconclusive) const { const ErrorMessage errmsg(callstack, &list, severity, id, msg, inconclusive ? Certainty::inconclusive : Certainty::normal); if (mErrorLogger) diff --git a/lib/tokenize.h b/lib/tokenize.h index 9ce946ff7cd..07b526ca08c 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -572,8 +572,8 @@ class CPPCHECKLIB Tokenizer { /** * report error message */ - void reportError(const Token* tok, const Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive = false) const; - void reportError(const std::list& callstack, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive = false) const; + void reportError(const Token* tok, const Severity severity, const std::string& id, const std::string& msg, bool inconclusive = false) const; + void reportError(const std::list& callstack, Severity severity, const std::string& id, const std::string& msg, bool inconclusive = false) const; bool duplicateTypedef(Token **tokPtr, const Token *name, const Token *typeDef) const; diff --git a/test/fixture.h b/test/fixture.h index 4e5f7262d47..039a4a2c097 100644 --- a/test/fixture.h +++ b/test/fixture.h @@ -138,7 +138,7 @@ class TestFixture : public ErrorLogger { explicit SettingsBuilder(const TestFixture &fixture) : fixture(fixture) {} SettingsBuilder(const TestFixture &fixture, Settings settings) : fixture(fixture), settings(std::move(settings)) {} - SettingsBuilder& severity(Severity::SeverityType sev, bool b = true) { + SettingsBuilder& severity(Severity sev, bool b = true) { if (REDUNDANT_CHECK && settings.severity.isEnabled(sev) == b) throw std::runtime_error("redundant setting: severity"); settings.severity.setEnabled(sev, b); diff --git a/test/testerrorlogger.cpp b/test/testerrorlogger.cpp index 78a7ae47425..52ef7e7033b 100644 --- a/test/testerrorlogger.cpp +++ b/test/testerrorlogger.cpp @@ -281,7 +281,7 @@ class TestErrorLogger : public TestFixture { ASSERT(doc.Parse(xmldata, sizeof(xmldata)) == tinyxml2::XML_SUCCESS); ErrorMessage msg(doc.FirstChildElement()); ASSERT_EQUALS("errorId", msg.id); - ASSERT_EQUALS(Severity::error, msg.severity); + ASSERT_EQUALS(static_cast(Severity::error), static_cast(msg.severity)); ASSERT_EQUALS(123u, msg.cwe.id); ASSERT_EQUALS(static_cast(Certainty::inconclusive), static_cast(msg.certainty)); ASSERT_EQUALS("Programming error.", msg.shortMessage()); @@ -330,7 +330,7 @@ class TestErrorLogger : public TestFixture { ErrorMessage msg2; ASSERT_NO_THROW(msg2.deserialize(msg_str)); ASSERT_EQUALS("errorId", msg2.id); - ASSERT_EQUALS(Severity::error, msg2.severity); + ASSERT_EQUALS(static_cast(Severity::error), static_cast(msg2.severity)); ASSERT_EQUALS("test.cpp", msg2.file0); ASSERT_EQUALS(static_cast(Certainty::inconclusive), static_cast(msg2.certainty)); ASSERT_EQUALS("Programming error", msg2.shortMessage()); @@ -418,7 +418,7 @@ class TestErrorLogger : public TestFixture { ErrorMessage msg2; ASSERT_NO_THROW(msg2.deserialize(msg_str)); ASSERT_EQUALS("errorId", msg2.id); - ASSERT_EQUALS(Severity::error, msg2.severity); + ASSERT_EQUALS(static_cast(Severity::error), static_cast(msg2.severity)); ASSERT_EQUALS("1.c", msg2.file0); ASSERT_EQUALS("Illegal character in \"foo\\001bar\"", msg2.shortMessage()); ASSERT_EQUALS("Illegal character in \"foo\\001bar\"", msg2.verboseMessage()); diff --git a/test/testlibrary.cpp b/test/testlibrary.cpp index f4bb1cab956..508ee938d80 100644 --- a/test/testlibrary.cpp +++ b/test/testlibrary.cpp @@ -645,12 +645,12 @@ class TestLibrary : public TestFixture { ASSERT(a && b); if (a && b) { ASSERT_EQUALS("Message", a->message); - ASSERT_EQUALS(Severity::style, a->severity); + ASSERT_EQUALS(static_cast(Severity::style), static_cast(a->severity)); ASSERT_EQUALS(Standards::C99, a->standards.c); ASSERT_EQUALS(Standards::CPP03, a->standards.cpp); ASSERT_EQUALS("Obsolescent function 'b' called. It is recommended to use 'c', 'd' or 'e' instead.", b->message); - ASSERT_EQUALS(Severity::performance, b->severity); + ASSERT_EQUALS(static_cast(Severity::performance), static_cast(b->severity)); ASSERT_EQUALS(Standards::C89, b->standards.c); ASSERT_EQUALS(Standards::CPP11, b->standards.cpp); }