Skip to content

Commit e88e91b

Browse files
rurbanReini Urban
authored andcommitted
fix severity colorization
esp. information as red was disturbing.
1 parent 88ef81e commit e88e91b

6 files changed

Lines changed: 62 additions & 6 deletions

File tree

cli/cmdlineparser.cpp

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

15761576
// Default template format..
15771577
if (mSettings.templateFormat.empty()) {
1578-
mSettings.templateFormat = "{bold}{file}:{line}:{column}: {red}{inconclusive:{magenta}}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}";
1578+
mSettings.templateFormat = "{bold}{file}:{line}:{column}: {inconclusive:}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}";
15791579
if (mSettings.templateLocation.empty())
15801580
mSettings.templateLocation = "{bold}{file}:{line}:{column}: {dim}note:{reset} {info}\\n{code}";
15811581
}

lib/errorlogger.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
620620
// replace id with guideline if present
621621
// replace severity with classification if present
622622
const std::string idStr = guideline.empty() ? id : guideline;
623-
const std::string severityStr = classification.empty() ? severityToString(severity) : classification;
623+
std::string severityStr = classification.empty() ? coloredSeverityToString(severity) : classification;
624624

625625
findAndReplace(result, "{id}", idStr);
626626

@@ -632,6 +632,7 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
632632
findAndReplace(result, replaceFrom, replaceWith);
633633
pos1 = result.find("{inconclusive:", pos1);
634634
}
635+
replaceColors(severityStr);
635636
findAndReplace(result, "{severity}", severityStr);
636637
findAndReplace(result, "{cwe}", std::to_string(cwe.id));
637638
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
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);

test/testmathlib.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,12 @@ class TestMathLib : public TestFixture {
389389
SUPPRESS_WARNING_CLANG_PUSH("-Wimplicitly-unsigned-literal")
390390
SUPPRESS_WARNING_GCC_PUSH("-Woverflow")
391391
{
392-
constexpr MathLib::bigint i = 18446744073709551615;
392+
constexpr MathLib::bigint i = 18446744073709551615ULL;
393393
ASSERT_EQUALS(i, MathLib::toBigNumber(std::to_string(i)));
394394
ASSERT_EQUALS(i, MathLib::toBigNumber("18446744073709551615"));
395395
}
396396
{
397-
constexpr MathLib::bigint i = -18446744073709551615;
397+
constexpr MathLib::bigint i = -18446744073709551615ULL;
398398
ASSERT_EQUALS(i, MathLib::toBigNumber(std::to_string(i)));
399399
ASSERT_EQUALS(i, MathLib::toBigNumber("-18446744073709551615"));
400400
}
@@ -565,12 +565,12 @@ class TestMathLib : public TestFixture {
565565
SUPPRESS_WARNING_CLANG_PUSH("-Wimplicitly-unsigned-literal")
566566
SUPPRESS_WARNING_GCC_PUSH("-Woverflow")
567567
{
568-
constexpr MathLib::biguint u = 18446744073709551615;
568+
constexpr MathLib::biguint u = 18446744073709551615ULL;
569569
ASSERT_EQUALS(u, MathLib::toBigUNumber(std::to_string(u)));
570570
ASSERT_EQUALS(u, MathLib::toBigUNumber("18446744073709551615"));
571571
}
572572
{
573-
constexpr MathLib::biguint u = -18446744073709551615;
573+
constexpr MathLib::biguint u = -18446744073709551615ULL;
574574
ASSERT_EQUALS(u, MathLib::toBigUNumber(std::to_string(u)));
575575
ASSERT_EQUALS(u, MathLib::toBigUNumber("-18446744073709551615"));
576576
}

0 commit comments

Comments
 (0)