Skip to content

Commit c4441cf

Browse files
committed
fix severity colorization
esp. information as red was disturbing.
1 parent f837929 commit c4441cf

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

cli/cmdlineparser.cpp

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

15701570
// Default template format..
15711571
if (mSettings.templateFormat.empty()) {
1572-
mSettings.templateFormat = "{bold}{file}:{line}:{column}: {red}{inconclusive:{magenta}}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}";
1572+
mSettings.templateFormat = "{bold}{file}:{line}:{column}: {inconclusive:}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}";
15731573
if (mSettings.templateLocation.empty())
15741574
mSettings.templateLocation = "{bold}{file}:{line}:{column}: {dim}note:{reset} {info}\\n{code}";
15751575
}

lib/errorlogger.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,9 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
632632
findAndReplace(result, replaceFrom, replaceWith);
633633
pos1 = result.find("{inconclusive:", pos1);
634634
}
635-
findAndReplace(result, "{severity}", severityStr);
635+
std::string sseverity = coloredSeverityToString(severity);
636+
replaceColors(sseverity);
637+
findAndReplace(result, "{severity}", sseverity);
636638
findAndReplace(result, "{cwe}", std::to_string(cwe.id));
637639
findAndReplace(result, "{message}", verbose ? mVerboseMessage : mShortMessage);
638640
findAndReplace(result, "{remark}", remark);

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
throw InternalError(nullptr, "Unknown severity");
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
@@ -50,6 +50,7 @@ class TestErrorLogger : public TestFixture {
5050
TEST_CASE(ErrorMessageVerbose);
5151
TEST_CASE(ErrorMessageVerboseLocations);
5252
TEST_CASE(ErrorMessageFromInternalError);
53+
TEST_CASE(ErrorMessageColorized);
5354
TEST_CASE(CustomFormat);
5455
TEST_CASE(CustomFormat2);
5556
TEST_CASE(CustomFormatLocations);
@@ -243,6 +244,34 @@ class TestErrorLogger : public TestFixture {
243244
ASSERT_EQUALS("L3 FIO42-C", msg.toString(true, format, ""));
244245
}
245246

247+
void ErrorMessageColorized() const {
248+
const bool oDisableColors = gDisableColors;
249+
gDisableColors = false;
250+
setenv("CLICOLOR_FORCE", "1", 1);
251+
std::list<ErrorMessage::FileLocation> locs = { };
252+
{
253+
ErrorMessage msg(std::move(locs), emptyString, Severity::error, "Programming error.\nVerbose error", "errorId",
254+
Certainty::normal);
255+
ASSERT_EQUALS("{bold} \x1b[31merror: Programming error.", msg.toString(false, "{bold} {severity}: {message}"));
256+
}
257+
{
258+
ErrorMessage msg(std::move(locs), emptyString, Severity::warning, "Programming warning.\nVerbose warning", "errorId",
259+
Certainty::normal);
260+
ASSERT_EQUALS("{bold} \x1b[35mwarning: Programming warning.", msg.toString(false, "{bold} {severity}: {message}"));
261+
}
262+
{
263+
ErrorMessage msg(std::move(locs), emptyString, Severity::style, "Style.\nVerbose style", "errorId", Certainty::normal);
264+
ASSERT_EQUALS("{bold} \x1b[1mstyle: Style.", msg.toString(false, "{bold} {severity}: {message}"));
265+
}
266+
{
267+
ErrorMessage msg(std::move(locs), emptyString, Severity::information, "Programming information.\nProgramming information",
268+
"errorId", Certainty::normal);
269+
ASSERT_EQUALS("{bold} \x1b[32minformation: Programming information.", msg.toString(false, "{bold} {severity}: {message}"));
270+
}
271+
setenv("CLICOLOR_FORCE", "", 1);
272+
gDisableColors = oDisableColors;
273+
}
274+
246275
void CustomFormat() const {
247276
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
248277
ErrorMessage msg(std::move(locs), "", Severity::error, "Programming error.\nVerbose error", "errorId", Certainty::normal);

0 commit comments

Comments
 (0)