Skip to content

Commit 8df610d

Browse files
committed
fix severity colorization
esp. information as red was disturbing.
1 parent 8ec2a65 commit 8df610d

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
@@ -1394,7 +1394,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
13941394

13951395
// Default template format..
13961396
if (mSettings.templateFormat.empty()) {
1397-
mSettings.templateFormat = "{bold}{file}:{line}:{column}: {red}{inconclusive:{magenta}}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}";
1397+
mSettings.templateFormat = "{bold}{file}:{line}:{column}: {inconclusive:}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}";
13981398
if (mSettings.templateLocation.empty())
13991399
mSettings.templateLocation = "{bold}{file}:{line}:{column}: {dim}note:{reset} {info}\\n{code}";
14001400
}

lib/errorlogger.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,9 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
637637
findAndReplace(result, replaceFrom, replaceWith);
638638
pos1 = result.find("{inconclusive:", pos1);
639639
}
640-
findAndReplace(result, "{severity}", severityToString(severity));
640+
std::string sseverity = coloredSeverityToString(severity);
641+
replaceColors(sseverity);
642+
findAndReplace(result, "{severity}", sseverity);
641643
findAndReplace(result, "{cwe}", std::to_string(cwe.id));
642644
findAndReplace(result, "{message}", verbose ? mVerboseMessage : mShortMessage);
643645
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
@@ -46,6 +46,7 @@ class TestErrorLogger : public TestFixture {
4646
TEST_CASE(ErrorMessageVerbose);
4747
TEST_CASE(ErrorMessageVerboseLocations);
4848
TEST_CASE(ErrorMessageFromInternalError);
49+
TEST_CASE(ErrorMessageColorized);
4950
TEST_CASE(CustomFormat);
5051
TEST_CASE(CustomFormat2);
5152
TEST_CASE(CustomFormatLocations);
@@ -193,6 +194,34 @@ class TestErrorLogger : public TestFixture {
193194
}
194195
}
195196

197+
void ErrorMessageColorized() const {
198+
const bool oDisableColors = gDisableColors;
199+
gDisableColors = false;
200+
setenv("CLICOLOR_FORCE", "1", 1);
201+
std::list<ErrorMessage::FileLocation> locs = { };
202+
{
203+
ErrorMessage msg(std::move(locs), emptyString, Severity::error, "Programming error.\nVerbose error", "errorId",
204+
Certainty::normal);
205+
ASSERT_EQUALS("{bold} \x1b[31merror: Programming error.", msg.toString(false, "{bold} {severity}: {message}"));
206+
}
207+
{
208+
ErrorMessage msg(std::move(locs), emptyString, Severity::warning, "Programming warning.\nVerbose warning", "errorId",
209+
Certainty::normal);
210+
ASSERT_EQUALS("{bold} \x1b[35mwarning: Programming warning.", msg.toString(false, "{bold} {severity}: {message}"));
211+
}
212+
{
213+
ErrorMessage msg(std::move(locs), emptyString, Severity::style, "Style.\nVerbose style", "errorId", Certainty::normal);
214+
ASSERT_EQUALS("{bold} \x1b[1mstyle: Style.", msg.toString(false, "{bold} {severity}: {message}"));
215+
}
216+
{
217+
ErrorMessage msg(std::move(locs), emptyString, Severity::information, "Programming information.\nProgramming information",
218+
"errorId", Certainty::normal);
219+
ASSERT_EQUALS("{bold} \x1b[32minformation: Programming information.", msg.toString(false, "{bold} {severity}: {message}"));
220+
}
221+
setenv("CLICOLOR_FORCE", "", 1);
222+
gDisableColors = oDisableColors;
223+
}
224+
196225
void CustomFormat() const {
197226
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
198227
ErrorMessage msg(std::move(locs), emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", Certainty::normal);

0 commit comments

Comments
 (0)