From 8df610df6df41b27480249243dbbe905b8d03d58 Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Wed, 17 Jul 2024 09:28:40 +0200 Subject: [PATCH] fix severity colorization esp. information as red was disturbing. --- cli/cmdlineparser.cpp | 2 +- lib/errorlogger.cpp | 4 +++- lib/errortypes.cpp | 25 +++++++++++++++++++++++++ lib/errortypes.h | 1 + test/testerrorlogger.cpp | 29 +++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 2 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index ae39951d855..c1c9580106b 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -1394,7 +1394,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a // Default template format.. if (mSettings.templateFormat.empty()) { - mSettings.templateFormat = "{bold}{file}:{line}:{column}: {red}{inconclusive:{magenta}}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}"; + mSettings.templateFormat = "{bold}{file}:{line}:{column}: {inconclusive:}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}"; if (mSettings.templateLocation.empty()) mSettings.templateLocation = "{bold}{file}:{line}:{column}: {dim}note:{reset} {info}\\n{code}"; } diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 17c5a82c977..0db71758bc7 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -637,7 +637,9 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm findAndReplace(result, replaceFrom, replaceWith); pos1 = result.find("{inconclusive:", pos1); } - findAndReplace(result, "{severity}", severityToString(severity)); + std::string sseverity = coloredSeverityToString(severity); + replaceColors(sseverity); + findAndReplace(result, "{severity}", sseverity); findAndReplace(result, "{cwe}", std::to_string(cwe.id)); findAndReplace(result, "{message}", verbose ? mVerboseMessage : mShortMessage); findAndReplace(result, "{remark}", remark); diff --git a/lib/errortypes.cpp b/lib/errortypes.cpp index 2c8824c95a6..ffaed8d3028 100644 --- a/lib/errortypes.cpp +++ b/lib/errortypes.cpp @@ -72,6 +72,31 @@ std::string severityToString(Severity severity) throw InternalError(nullptr, "Unknown severity"); } +std::string coloredSeverityToString(Severity severity) +{ + switch (severity) { + case Severity::none: + return ""; + case Severity::error: + return "{red}error"; + case Severity::warning: + return "{magenta}warning"; + case Severity::style: + return "{bold}style"; + case Severity::performance: + return "{bold}performance"; + case Severity::portability: + return "{bold}portability"; + case Severity::information: + return "{green}information"; + case Severity::debug: + return "debug"; + case Severity::internal: + return "internal"; + } + throw InternalError(nullptr, "Unknown severity"); +} + // TODO: bail out on invalid severity Severity severityFromString(const std::string& severity) { diff --git a/lib/errortypes.h b/lib/errortypes.h index 0a13030c9f1..641e883239d 100644 --- a/lib/errortypes.h +++ b/lib/errortypes.h @@ -120,6 +120,7 @@ enum class Severity : std::uint8_t { }; CPPCHECKLIB std::string severityToString(Severity severity); +CPPCHECKLIB std::string coloredSeverityToString(Severity severity); CPPCHECKLIB Severity severityFromString(const std::string &severity); struct CWE { diff --git a/test/testerrorlogger.cpp b/test/testerrorlogger.cpp index 79e1d7575d2..6cc598847e7 100644 --- a/test/testerrorlogger.cpp +++ b/test/testerrorlogger.cpp @@ -46,6 +46,7 @@ class TestErrorLogger : public TestFixture { TEST_CASE(ErrorMessageVerbose); TEST_CASE(ErrorMessageVerboseLocations); TEST_CASE(ErrorMessageFromInternalError); + TEST_CASE(ErrorMessageColorized); TEST_CASE(CustomFormat); TEST_CASE(CustomFormat2); TEST_CASE(CustomFormatLocations); @@ -193,6 +194,34 @@ class TestErrorLogger : public TestFixture { } } + void ErrorMessageColorized() const { + const bool oDisableColors = gDisableColors; + gDisableColors = false; + setenv("CLICOLOR_FORCE", "1", 1); + std::list locs = { }; + { + ErrorMessage msg(std::move(locs), emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", + Certainty::normal); + ASSERT_EQUALS("{bold} \x1b[31merror: Programming error.", msg.toString(false, "{bold} {severity}: {message}")); + } + { + ErrorMessage msg(std::move(locs), emptyString, Severity::warning, "Programming warning.\nVerbose warning", "errorId", + Certainty::normal); + ASSERT_EQUALS("{bold} \x1b[35mwarning: Programming warning.", msg.toString(false, "{bold} {severity}: {message}")); + } + { + ErrorMessage msg(std::move(locs), emptyString, Severity::style, "Style.\nVerbose style", "errorId", Certainty::normal); + ASSERT_EQUALS("{bold} \x1b[1mstyle: Style.", msg.toString(false, "{bold} {severity}: {message}")); + } + { + ErrorMessage msg(std::move(locs), emptyString, Severity::information, "Programming information.\nProgramming information", + "errorId", Certainty::normal); + ASSERT_EQUALS("{bold} \x1b[32minformation: Programming information.", msg.toString(false, "{bold} {severity}: {message}")); + } + setenv("CLICOLOR_FORCE", "", 1); + gDisableColors = oDisableColors; + } + void CustomFormat() const { std::list locs(1, fooCpp5); ErrorMessage msg(std::move(locs), emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", Certainty::normal);