Skip to content
Merged
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
4 changes: 2 additions & 2 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1554,8 +1554,8 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
mSettings.templateLocation = "{bold}{file}:{line}:{column}: {dim}note:{reset} {info}\\n{code}";
}
// replace static parts of the templates
substituteTemplateFormatStatic(mSettings.templateFormat);
substituteTemplateLocationStatic(mSettings.templateLocation);
substituteTemplateFormatStatic(mSettings.templateFormat, !mSettings.outputFile.empty());
substituteTemplateLocationStatic(mSettings.templateLocation, !mSettings.outputFile.empty());

if (mSettings.force || maxconfigs)
mSettings.checkAllConfigurations = true;
Expand Down
28 changes: 21 additions & 7 deletions lib/errorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,9 @@ static void replace(std::string& source, const std::unordered_map<std::string, s
}
}

static void replaceColors(std::string& source) {
static void replaceColors(std::string& source, bool erase) {
// TODO: colors are not applied when either stdout or stderr is not a TTY because we resolve them before the stream usage
static const std::unordered_map<std::string, std::string> substitutionMap =
static const std::unordered_map<std::string, std::string> substitutionMapReplace =
{
{"{reset}", ::toString(Color::Reset)},
{"{bold}", ::toString(Color::Bold)},
Expand All @@ -607,7 +607,21 @@ static void replaceColors(std::string& source) {
{"{magenta}", ::toString(Color::FgMagenta)},
{"{default}", ::toString(Color::FgDefault)},
};
replace(source, substitutionMap);
static const std::unordered_map<std::string, std::string> substitutionMapErase =
{
{"{reset}", ""},
{"{bold}", ""},
{"{dim}", ""},
{"{red}", ""},
{"{green}", ""},
{"{blue}", ""},
{"{magenta}", ""},
{"{default}", ""},
};
if (!erase)
replace(source, substitutionMapReplace);
else
replace(source, substitutionMapErase);
}

std::string ErrorMessage::toString(bool verbose, const std::string &templateFormat, const std::string &templateLocation) const
Expand Down Expand Up @@ -913,16 +927,16 @@ std::string replaceStr(std::string s, const std::string &from, const std::string
return s;
}

void substituteTemplateFormatStatic(std::string& templateFormat)
void substituteTemplateFormatStatic(std::string& templateFormat, bool eraseColors)
{
replaceSpecialChars(templateFormat);
replaceColors(templateFormat);
replaceColors(templateFormat, eraseColors);
}

void substituteTemplateLocationStatic(std::string& templateLocation)
void substituteTemplateLocationStatic(std::string& templateLocation, bool eraseColors)
{
replaceSpecialChars(templateLocation);
replaceColors(templateLocation);
replaceColors(templateLocation, eraseColors);
}

std::string getClassification(const std::string &guideline, ReportType reportType) {
Expand Down
4 changes: 2 additions & 2 deletions lib/errorlogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,10 @@ class CPPCHECKLIB ErrorLogger {
std::string replaceStr(std::string s, const std::string &from, const std::string &to);

/** replaces the static parts of the location template **/
CPPCHECKLIB void substituteTemplateFormatStatic(std::string& templateFormat);
CPPCHECKLIB void substituteTemplateFormatStatic(std::string& templateFormat, bool eraseColors = false);

/** replaces the static parts of the location template **/
CPPCHECKLIB void substituteTemplateLocationStatic(std::string& templateLocation);
CPPCHECKLIB void substituteTemplateLocationStatic(std::string& templateLocation, bool eraseColors = false);

/** Get a classification string from the given guideline and reporttype */
CPPCHECKLIB std::string getClassification(const std::string &guideline, ReportType reportType);
Expand Down
35 changes: 35 additions & 0 deletions test/cli/other_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2426,6 +2426,41 @@ def test_xml_output(tmp_path): # #13391 / #13485
'''.format(version_str, test_file_exp, test_file_exp, test_file_exp))


def test_outputfile(tmp_path): # #14051
test_file = tmp_path / 'test.cpp'
out_file = tmp_path / 'out.txt'
with open(test_file, 'wt') as f:
f.write(
"""
int main()
{
int x = 1 / 0;
}
""")

args = [
'-q',
'--output-file={}'.format(out_file),
str(test_file)
]

out_exp = [
'{}:4:15: error: Division by zero. [zerodiv]'.format(test_file),
' int x = 1 / 0;',
' ^',
]

exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0, stdout
assert stdout == ''
assert stderr == ''

with open(out_file, 'rt') as f:
out_text = f.read()

assert out_text.splitlines() == out_exp


def test_internal_error_loc_int(tmp_path):
test_file = tmp_path / 'test.c'
with open(test_file, 'wt') as f:
Expand Down