Skip to content

Commit b5431c8

Browse files
rurbanReini Urban
authored andcommitted
fix severity colorization
esp. information as red was disturbing.
1 parent d92c579 commit b5431c8

5 files changed

Lines changed: 58 additions & 2 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
15811581

15821582
// Default template format..
15831583
if (mSettings.templateFormat.empty()) {
1584-
mSettings.templateFormat = "{bold}{file}:{line}:{column}: {red}{inconclusive:{magenta}}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}";
1584+
mSettings.templateFormat = "{bold}{file}:{line}:{column}: {inconclusive:}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}";
15851585
if (mSettings.templateLocation.empty())
15861586
mSettings.templateLocation = "{bold}{file}:{line}:{column}: {dim}note:{reset} {info}\\n{code}";
15871587
}

lib/errorlogger.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
644644
// replace id with guideline if present
645645
// replace severity with classification if present
646646
const std::string idStr = guideline.empty() ? id : guideline;
647-
const std::string severityStr = classification.empty() ? severityToString(severity) : classification;
647+
std::string severityStr = classification.empty() ? coloredSeverityToString(severity) : classification;
648648

649649
findAndReplace(result, "{id}", idStr);
650650

@@ -656,6 +656,7 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
656656
findAndReplace(result, replaceFrom, replaceWith);
657657
pos1 = result.find("{inconclusive:", pos1);
658658
}
659+
replaceColors(severityStr);
659660
findAndReplace(result, "{severity}", severityStr);
660661
findAndReplace(result, "{cwe}", std::to_string(cwe.id));
661662
findAndReplace(result, "{message}", verbose ? mVerboseMessage : mShortMessage);

lib/errortypes.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,31 @@ std::string severityToString(Severity severity)
7272
cppcheck::unreachable();
7373
}
7474

75+
std::string coloredSeverityToString(Severity severity)
76+
{
77+
switch (severity) {
78+
case Severity::none:
79+
return "";
80+
case Severity::error:
81+
return "{red}error";
82+
case Severity::warning:
83+
return "{magenta}warning";
84+
case Severity::style:
85+
return "{bold}style";
86+
case Severity::performance:
87+
return "{bold}performance";
88+
case Severity::portability:
89+
return "{bold}portability";
90+
case Severity::information:
91+
return "{green}information";
92+
case Severity::debug:
93+
return "debug";
94+
case Severity::internal:
95+
return "internal";
96+
}
97+
throw InternalError(nullptr, "Unknown severity");
98+
}
99+
75100
// TODO: bail out on invalid severity
76101
Severity severityFromString(const std::string& severity)
77102
{

lib/errortypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ enum class Severity : std::uint8_t {
120120
};
121121

122122
CPPCHECKLIB std::string severityToString(Severity severity);
123+
CPPCHECKLIB std::string coloredSeverityToString(Severity severity);
123124
CPPCHECKLIB Severity severityFromString(const std::string &severity);
124125

125126
struct CWE {

test/testerrorlogger.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class TestErrorLogger : public TestFixture {
5252
TEST_CASE(ErrorMessageVerbose);
5353
TEST_CASE(ErrorMessageVerboseLocations);
5454
TEST_CASE(ErrorMessageFromInternalError);
55+
TEST_CASE(ErrorMessageColorized);
5556
TEST_CASE(CustomFormat);
5657
TEST_CASE(CustomFormat2);
5758
TEST_CASE(CustomFormatLocations);
@@ -341,6 +342,34 @@ class TestErrorLogger : public TestFixture {
341342
testReportType(ReportType::certC, Severity::error, "resourceLeak", "L3", "FIO42-C");
342343
}
343344

345+
void ErrorMessageColorized() const {
346+
const bool oDisableColors = gDisableColors;
347+
gDisableColors = false;
348+
setenv("CLICOLOR_FORCE", "1", 1);
349+
std::list<ErrorMessage::FileLocation> locs = { };
350+
{
351+
ErrorMessage msg(std::move(locs), emptyString, Severity::error, "Programming error.\nVerbose error", "errorId",
352+
Certainty::normal);
353+
ASSERT_EQUALS("{bold} \x1b[31merror: Programming error.", msg.toString(false, "{bold} {severity}: {message}", ""));
354+
}
355+
{
356+
ErrorMessage msg(std::move(locs), emptyString, Severity::warning, "Programming warning.\nVerbose warning", "errorId",
357+
Certainty::normal);
358+
ASSERT_EQUALS("{bold} \x1b[35mwarning: Programming warning.", msg.toString(false, "{bold} {severity}: {message}", ""));
359+
}
360+
{
361+
ErrorMessage msg(std::move(locs), emptyString, Severity::style, "Style.\nVerbose style", "errorId", Certainty::normal);
362+
ASSERT_EQUALS("{bold} \x1b[1mstyle: Style.", msg.toString(false, "{bold} {severity}: {message}", ""));
363+
}
364+
{
365+
ErrorMessage msg(std::move(locs), emptyString, Severity::information, "Programming information.\nProgramming information",
366+
"errorId", Certainty::normal);
367+
ASSERT_EQUALS("{bold} \x1b[32minformation: Programming information.", msg.toString(false, "{bold} {severity}: {message}", ""));
368+
}
369+
setenv("CLICOLOR_FORCE", "", 1);
370+
gDisableColors = oDisableColors;
371+
}
372+
344373
void CustomFormat() const {
345374
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
346375
ErrorMessage msg(std::move(locs), "", Severity::error, "Programming error.\nVerbose error", "errorId", Certainty::normal);

0 commit comments

Comments
 (0)