From 8cc3e0b2ff1d33dc5adc24f8620aebcaae859b1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Sun, 8 Jan 2023 19:31:54 +0100 Subject: [PATCH] errortypes.h: use `enum class` instead of wrapping types into classes (#4693) --- lib/check.cpp | 4 ++-- lib/check.h | 6 +++--- lib/checkbufferoverrun.cpp | 2 +- lib/checkbufferoverrun.h | 2 +- lib/errorlogger.cpp | 10 +++++----- lib/errorlogger.h | 12 ++++++------ lib/errortypes.h | 14 ++++---------- lib/settings.h | 4 ++-- lib/suppressions.h | 2 +- test/testerrorlogger.cpp | 4 ++-- test/testsuppressions.cpp | 2 +- 11 files changed, 28 insertions(+), 34 deletions(-) diff --git a/lib/check.cpp b/lib/check.cpp index de5a06cb2..792a12ede 100644 --- a/lib/check.cpp +++ b/lib/check.cpp @@ -50,7 +50,7 @@ void Check::reportError(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::CertaintyLevel certainty) +void Check::reportError(const std::list &callstack, Severity::SeverityType 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) @@ -59,7 +59,7 @@ void Check::reportError(const std::list &callstack, Severity::Sev reportError(errmsg); } -void Check::reportError(const ErrorPath &errorPath, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty) +void Check::reportError(const ErrorPath &errorPath, Severity::SeverityType 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 6278a2caf..7bc0892c4 100644 --- a/lib/check.h +++ b/lib/check.h @@ -140,7 +140,7 @@ protected: } /** report an error */ - void reportError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty) { + void reportError(const Token *tok, const Severity::SeverityType 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); } @@ -151,9 +151,9 @@ protected: } /** report an error */ - void reportError(const std::list &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty); + 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 ErrorPath &errorPath, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, Certainty::CertaintyLevel certainty); + void reportError(const ErrorPath &errorPath, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty); ErrorPath getErrorPath(const Token* errtok, const ValueFlow::Value* value, std::string bug) const; diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 3d6a1b297..2153d1357 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -678,7 +678,7 @@ void CheckBufferOverrun::bufferOverflow() } } -void CheckBufferOverrun::bufferOverflowError(const Token *tok, const ValueFlow::Value *value, const Certainty::CertaintyLevel &certainty) +void CheckBufferOverrun::bufferOverflowError(const Token *tok, const ValueFlow::Value *value, Certainty certainty) { reportError(getErrorPath(tok, value, "Buffer overrun"), Severity::error, "bufferAccessOutOfBounds", "Buffer is accessed out of bounds: " + (tok ? tok->expressionString() : "buf"), CWE_BUFFER_OVERRUN, certainty); } diff --git a/lib/checkbufferoverrun.h b/lib/checkbufferoverrun.h index 9c1b7d155..49692f015 100644 --- a/lib/checkbufferoverrun.h +++ b/lib/checkbufferoverrun.h @@ -111,7 +111,7 @@ private: void pointerArithmeticError(const Token *tok, const Token *indexToken, const ValueFlow::Value *indexValue); void bufferOverflow(); - void bufferOverflowError(const Token *tok, const ValueFlow::Value *value, const Certainty::CertaintyLevel& certainty); + void bufferOverflowError(const Token *tok, const ValueFlow::Value *value, Certainty certainty); void arrayIndexThenCheck(); void arrayIndexThenCheckError(const Token *tok, const std::string &indexName); diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 081f4816f..b726ffae8 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -43,7 +43,7 @@ ErrorMessage::ErrorMessage() : severity(Severity::none), cwe(0U), certainty(Certainty::normal), hash(0) {} -ErrorMessage::ErrorMessage(std::list callStack, std::string file1, Severity::SeverityType severity, const std::string &msg, std::string id, Certainty::CertaintyLevel certainty) : +ErrorMessage::ErrorMessage(std::list callStack, std::string file1, Severity::SeverityType 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)), @@ -58,7 +58,7 @@ ErrorMessage::ErrorMessage(std::list callStack, std::string file1, -ErrorMessage::ErrorMessage(std::list callStack, std::string file1, Severity::SeverityType severity, const std::string &msg, std::string id, const CWE &cwe, Certainty::CertaintyLevel certainty) : +ErrorMessage::ErrorMessage(std::list callStack, std::string file1, Severity::SeverityType 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)), @@ -71,7 +71,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::CertaintyLevel certainty) +ErrorMessage::ErrorMessage(const std::list& callstack, const TokenList* list, Severity::SeverityType severity, std::string id, const std::string& msg, Certainty certainty) : id(std::move(id)), severity(severity), cwe(0U), certainty(certainty), hash(0) { // Format callstack @@ -90,7 +90,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::CertaintyLevel certainty) +ErrorMessage::ErrorMessage(const std::list& callstack, const TokenList* list, Severity::SeverityType 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 @@ -110,7 +110,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::CertaintyLevel certainty) +ErrorMessage::ErrorMessage(const ErrorPath &errorPath, const TokenList *tokenList, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty) : id(id), severity(severity), cwe(cwe.id), certainty(certainty) { // Format callstack diff --git a/lib/errorlogger.h b/lib/errorlogger.h index e351ca5b6..dba7ec4f7 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -112,34 +112,34 @@ public: std::string file1, Severity::SeverityType severity, const std::string &msg, - std::string id, Certainty::CertaintyLevel certainty); + std::string id, Certainty certainty); ErrorMessage(std::list callStack, std::string file1, Severity::SeverityType severity, const std::string &msg, std::string id, const CWE &cwe, - Certainty::CertaintyLevel certainty); + Certainty certainty); ErrorMessage(const std::list& callstack, const TokenList* list, Severity::SeverityType severity, std::string id, const std::string& msg, - Certainty::CertaintyLevel certainty); + Certainty certainty); ErrorMessage(const std::list& callstack, const TokenList* list, Severity::SeverityType severity, std::string id, const std::string& msg, const CWE &cwe, - Certainty::CertaintyLevel certainty); + Certainty certainty); ErrorMessage(const ErrorPath &errorPath, const TokenList *tokenList, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, - Certainty::CertaintyLevel certainty); + Certainty certainty); ErrorMessage(); explicit ErrorMessage(const tinyxml2::XMLElement * const errmsg); @@ -175,7 +175,7 @@ public: Severity::SeverityType severity; CWE cwe; - Certainty::CertaintyLevel certainty; + Certainty certainty; /** Warning hash */ std::size_t hash; diff --git a/lib/errortypes.h b/lib/errortypes.h index c3807a56c..dcf5b38f2 100644 --- a/lib/errortypes.h +++ b/lib/errortypes.h @@ -47,18 +47,12 @@ public: TerminateException() : std::runtime_error("terminate") {} }; -class CPPCHECKLIB Certainty { -public: - enum CertaintyLevel { - normal, inconclusive, safe, experimental - }; +enum class Certainty { + normal, inconclusive, safe, experimental }; -class CPPCHECKLIB Checks { -public: - enum CheckList { - unusedFunction, missingInclude, internalCheck - }; +enum class Checks { + unusedFunction, missingInclude, internalCheck }; /** @brief enum class for severity. Used when reporting errors. */ diff --git a/lib/settings.h b/lib/settings.h index afd92bb7c..77b7bff77 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -328,8 +328,8 @@ public: SafeChecks safeChecks; SimpleEnableGroup severity; - SimpleEnableGroup certainty; - SimpleEnableGroup checks; + SimpleEnableGroup certainty; + SimpleEnableGroup checks; /** @brief show timing information (--showtime=file|summary|top5) */ SHOWTIME_MODES showtime; diff --git a/lib/suppressions.h b/lib/suppressions.h index 7fc293ae5..feb816323 100644 --- a/lib/suppressions.h +++ b/lib/suppressions.h @@ -47,7 +47,7 @@ public: return mFileName; } int lineNumber; - Certainty::CertaintyLevel certainty; + Certainty certainty; std::string symbolNames; private: std::string mFileName; diff --git a/test/testerrorlogger.cpp b/test/testerrorlogger.cpp index c7ad96f6f..6de58405b 100644 --- a/test/testerrorlogger.cpp +++ b/test/testerrorlogger.cpp @@ -254,7 +254,7 @@ private: ASSERT_EQUALS("errorId", msg.id); ASSERT_EQUALS(Severity::error, msg.severity); ASSERT_EQUALS(123u, msg.cwe.id); - ASSERT_EQUALS(Certainty::inconclusive, msg.certainty); + ASSERT_EQUALS(static_cast(Certainty::inconclusive), static_cast(msg.certainty)); ASSERT_EQUALS("Programming error.", msg.shortMessage()); ASSERT_EQUALS("Verbose error", msg.verboseMessage()); ASSERT_EQUALS(456u, msg.hash); @@ -303,7 +303,7 @@ private: ASSERT_EQUALS("errorId", msg2.id); ASSERT_EQUALS(Severity::error, msg2.severity); ASSERT_EQUALS("test.cpp", msg2.file0); - ASSERT_EQUALS(Certainty::inconclusive, msg2.certainty); + ASSERT_EQUALS(static_cast(Certainty::inconclusive), static_cast(msg2.certainty)); ASSERT_EQUALS("Programming error", msg2.shortMessage()); ASSERT_EQUALS("Programming error", msg2.verboseMessage()); } diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index f9eef2b82..30d5a96a3 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -98,7 +98,7 @@ private: ret.errorId = errorId; ret.hash = 0; ret.lineNumber = 0; - ret.certainty = Certainty::CertaintyLevel::normal; + ret.certainty = Certainty::normal; return ret; }