Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a

// Default template format..
if (mSettings.templateFormat.empty()) {
mSettings.templateFormat = "{bold}{file}:{line}:{column}: {red}{inconclusive:{magenta}}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes the downgrade of red to magenta for inconclusive findings.

mSettings.templateFormat = "{bold}{file}:{line}:{column}: {inconclusive:}{severity}:{inconclusive: inconclusive:}{default} {message} [{id}]{reset}\\n{code}";
if (mSettings.templateLocation.empty())
mSettings.templateLocation = "{bold}{file}:{line}:{column}: {dim}note:{reset} {info}\\n{code}";
}
Expand Down
3 changes: 2 additions & 1 deletion lib/errorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
// replace id with guideline if present
// replace severity with classification if present
const std::string idStr = guideline.empty() ? id : guideline;
const std::string severityStr = classification.empty() ? severityToString(severity) : classification;
std::string severityStr = classification.empty() ? coloredSeverityToString(severity) : classification;

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

Expand All @@ -656,6 +656,7 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
findAndReplace(result, replaceFrom, replaceWith);
pos1 = result.find("{inconclusive:", pos1);
}
replaceColors(severityStr, !templateFormat.empty());
findAndReplace(result, "{severity}", severityStr);
findAndReplace(result, "{cwe}", std::to_string(cwe.id));
findAndReplace(result, "{message}", verbose ? mVerboseMessage : mShortMessage);
Expand Down
25 changes: 25 additions & 0 deletions lib/errortypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,31 @@ std::string severityToString(Severity severity)
cppcheck::unreachable();
}

std::string coloredSeverityToString(Severity severity)
{
switch (severity) {
case Severity::none:
return "";
case Severity::error:
return "{red}error";
case Severity::warning:
return "{magenta}warning";
case Severity::style:
return "{bold}style";
case Severity::performance:
return "{bold}performance";
case Severity::portability:
return "{bold}portability";
case Severity::information:
return "{green}information";
case Severity::debug:
return "debug";
case Severity::internal:
return "internal";
}
throw InternalError(nullptr, "Unknown severity");
}

// TODO: bail out on invalid severity
Severity severityFromString(const std::string& severity)
{
Expand Down
1 change: 1 addition & 0 deletions lib/errortypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ enum class Severity : std::uint8_t {
};

CPPCHECKLIB std::string severityToString(Severity severity);
CPPCHECKLIB std::string coloredSeverityToString(Severity severity);
CPPCHECKLIB Severity severityFromString(const std::string &severity);

struct CWE {
Expand Down
29 changes: 29 additions & 0 deletions test/testerrorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class TestErrorLogger : public TestFixture {
TEST_CASE(ErrorMessageVerbose);
TEST_CASE(ErrorMessageVerboseLocations);
TEST_CASE(ErrorMessageFromInternalError);
TEST_CASE(ErrorMessageColorized);
TEST_CASE(CustomFormat);
TEST_CASE(CustomFormat2);
TEST_CASE(CustomFormatLocations);
Expand Down Expand Up @@ -341,6 +342,34 @@ class TestErrorLogger : public TestFixture {
testReportType(ReportType::certC, Severity::error, "resourceLeak", "L3", "FIO42-C");
}

void ErrorMessageColorized() const {
const bool oDisableColors = gDisableColors;
gDisableColors = false;
setenv("CLICOLOR_FORCE", "1", 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Since it requires you to modify the environment this should be implemented as a Python test instead. This should probably live next to the ones added when CLICOLOR_FORCE was introduced.

std::list<ErrorMessage::FileLocation> locs = { };
{
ErrorMessage msg(std::move(locs), "", Severity::error, "Programming error.\nVerbose error", "errorId",
Certainty::normal);
ASSERT_EQUALS("{bold} \x1b[31merror: Programming error.", msg.toString(false, "{bold} {severity}: {message}", ""));
}
{
ErrorMessage msg(std::move(locs), "", Severity::warning, "Programming warning.\nVerbose warning", "errorId",
Certainty::normal);
ASSERT_EQUALS("{bold} \x1b[35mwarning: Programming warning.", msg.toString(false, "{bold} {severity}: {message}", ""));
}
{
ErrorMessage msg(std::move(locs), "", Severity::style, "Style.\nVerbose style", "errorId", Certainty::normal);
ASSERT_EQUALS("{bold} \x1b[1mstyle: Style.", msg.toString(false, "{bold} {severity}: {message}", ""));
}
{
ErrorMessage msg(std::move(locs), "", Severity::information, "Programming information.\nProgramming information",
"errorId", Certainty::normal);
ASSERT_EQUALS("{bold} \x1b[32minformation: Programming information.", msg.toString(false, "{bold} {severity}: {message}", ""));
}
setenv("CLICOLOR_FORCE", "", 1);
gDisableColors = oDisableColors;
}

void CustomFormat() const {
std::list<ErrorMessage::FileLocation> locs(1, fooCpp5);
ErrorMessage msg(std::move(locs), "", Severity::error, "Programming error.\nVerbose error", "errorId", Certainty::normal);
Expand Down