Skip to content

Commit 1459984

Browse files
rurbanReini Urban
authored andcommitted
fix severity colorization
esp. information as red was disturbing.
1 parent 55e2dc6 commit 1459984

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

15241524
// Default template format..
15251525
if (mSettings.templateFormat.empty()) {
1526-
mSettings.templateFormat = "{bold}{file}:{line}:{column}: {red}{inconclusive:{magenta}}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}";
1526+
mSettings.templateFormat = "{bold}{file}:{line}:{column}: {inconclusive:}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}";
15271527
if (mSettings.templateLocation.empty())
15281528
mSettings.templateLocation = "{bold}{file}:{line}:{column}: {dim}note:{reset} {info}\\n{code}";
15291529
}

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
@@ -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);
@@ -342,6 +343,34 @@ class TestErrorLogger : public TestFixture {
342343
testReportType(ReportType::certC, Severity::error, "resourceLeak", "L3", "FIO42-C");
343344
}
344345

346+
void ErrorMessageColorized() const {
347+
const bool oDisableColors = gDisableColors;
348+
gDisableColors = false;
349+
setenv("CLICOLOR_FORCE", "1", 1);
350+
std::list<ErrorMessage::FileLocation> locs = { };
351+
{
352+
ErrorMessage msg(std::move(locs), emptyString, Severity::error, "Programming error.\nVerbose error", "errorId",
353+
Certainty::normal);
354+
ASSERT_EQUALS("{bold} \x1b[31merror: Programming error.", msg.toString(false, "{bold} {severity}: {message}", ""));
355+
}
356+
{
357+
ErrorMessage msg(std::move(locs), emptyString, Severity::warning, "Programming warning.\nVerbose warning", "errorId",
358+
Certainty::normal);
359+
ASSERT_EQUALS("{bold} \x1b[35mwarning: Programming warning.", msg.toString(false, "{bold} {severity}: {message}", ""));
360+
}
361+
{
362+
ErrorMessage msg(std::move(locs), emptyString, Severity::style, "Style.\nVerbose style", "errorId", Certainty::normal);
363+
ASSERT_EQUALS("{bold} \x1b[1mstyle: Style.", msg.toString(false, "{bold} {severity}: {message}", ""));
364+
}
365+
{
366+
ErrorMessage msg(std::move(locs), emptyString, Severity::information, "Programming information.\nProgramming information",
367+
"errorId", Certainty::normal);
368+
ASSERT_EQUALS("{bold} \x1b[32minformation: Programming information.", msg.toString(false, "{bold} {severity}: {message}", ""));
369+
}
370+
setenv("CLICOLOR_FORCE", "", 1);
371+
gDisableColors = oDisableColors;
372+
}
373+
345374
void CustomFormat() const {
346375
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
347376
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
@@ -390,12 +390,12 @@ class TestMathLib : public TestFixture {
390390
SUPPRESS_WARNING_CLANG_PUSH("-Wimplicitly-unsigned-literal")
391391
SUPPRESS_WARNING_GCC_PUSH("-Woverflow")
392392
{
393-
constexpr MathLib::bigint i = 18446744073709551615;
393+
constexpr MathLib::bigint i = 18446744073709551615ULL;
394394
ASSERT_EQUALS(i, MathLib::toBigNumber(std::to_string(i)));
395395
ASSERT_EQUALS(i, MathLib::toBigNumber("18446744073709551615"));
396396
}
397397
{
398-
constexpr MathLib::bigint i = -18446744073709551615;
398+
constexpr MathLib::bigint i = -18446744073709551615ULL;
399399
ASSERT_EQUALS(i, MathLib::toBigNumber(std::to_string(i)));
400400
ASSERT_EQUALS(i, MathLib::toBigNumber("-18446744073709551615"));
401401
}
@@ -566,12 +566,12 @@ class TestMathLib : public TestFixture {
566566
SUPPRESS_WARNING_CLANG_PUSH("-Wimplicitly-unsigned-literal")
567567
SUPPRESS_WARNING_GCC_PUSH("-Woverflow")
568568
{
569-
constexpr MathLib::biguint u = 18446744073709551615;
569+
constexpr MathLib::biguint u = 18446744073709551615ULL;
570570
ASSERT_EQUALS(u, MathLib::toBigUNumber(std::to_string(u)));
571571
ASSERT_EQUALS(u, MathLib::toBigUNumber("18446744073709551615"));
572572
}
573573
{
574-
constexpr MathLib::biguint u = -18446744073709551615;
574+
constexpr MathLib::biguint u = -18446744073709551615ULL;
575575
ASSERT_EQUALS(u, MathLib::toBigUNumber(std::to_string(u)));
576576
ASSERT_EQUALS(u, MathLib::toBigUNumber("-18446744073709551615"));
577577
}

0 commit comments

Comments
 (0)