Skip to content

Commit 7a05f00

Browse files
authored
Fix #14051 (color codes should not be stored in output file) (#7759)
1 parent 90ee464 commit 7a05f00

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

cli/cmdlineparser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,8 +1554,8 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
15541554
mSettings.templateLocation = "{bold}{file}:{line}:{column}: {dim}note:{reset} {info}\\n{code}";
15551555
}
15561556
// replace static parts of the templates
1557-
substituteTemplateFormatStatic(mSettings.templateFormat);
1558-
substituteTemplateLocationStatic(mSettings.templateLocation);
1557+
substituteTemplateFormatStatic(mSettings.templateFormat, !mSettings.outputFile.empty());
1558+
substituteTemplateLocationStatic(mSettings.templateLocation, !mSettings.outputFile.empty());
15591559

15601560
if (mSettings.force || maxconfigs)
15611561
mSettings.checkAllConfigurations = true;

lib/errorlogger.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,9 @@ static void replace(std::string& source, const std::unordered_map<std::string, s
594594
}
595595
}
596596

597-
static void replaceColors(std::string& source) {
597+
static void replaceColors(std::string& source, bool erase) {
598598
// TODO: colors are not applied when either stdout or stderr is not a TTY because we resolve them before the stream usage
599-
static const std::unordered_map<std::string, std::string> substitutionMap =
599+
static const std::unordered_map<std::string, std::string> substitutionMapReplace =
600600
{
601601
{"{reset}", ::toString(Color::Reset)},
602602
{"{bold}", ::toString(Color::Bold)},
@@ -607,7 +607,21 @@ static void replaceColors(std::string& source) {
607607
{"{magenta}", ::toString(Color::FgMagenta)},
608608
{"{default}", ::toString(Color::FgDefault)},
609609
};
610-
replace(source, substitutionMap);
610+
static const std::unordered_map<std::string, std::string> substitutionMapErase =
611+
{
612+
{"{reset}", ""},
613+
{"{bold}", ""},
614+
{"{dim}", ""},
615+
{"{red}", ""},
616+
{"{green}", ""},
617+
{"{blue}", ""},
618+
{"{magenta}", ""},
619+
{"{default}", ""},
620+
};
621+
if (!erase)
622+
replace(source, substitutionMapReplace);
623+
else
624+
replace(source, substitutionMapErase);
611625
}
612626

613627
std::string ErrorMessage::toString(bool verbose, const std::string &templateFormat, const std::string &templateLocation) const
@@ -916,16 +930,16 @@ std::string replaceStr(std::string s, const std::string &from, const std::string
916930
return s;
917931
}
918932

919-
void substituteTemplateFormatStatic(std::string& templateFormat)
933+
void substituteTemplateFormatStatic(std::string& templateFormat, bool eraseColors)
920934
{
921935
replaceSpecialChars(templateFormat);
922-
replaceColors(templateFormat);
936+
replaceColors(templateFormat, eraseColors);
923937
}
924938

925-
void substituteTemplateLocationStatic(std::string& templateLocation)
939+
void substituteTemplateLocationStatic(std::string& templateLocation, bool eraseColors)
926940
{
927941
replaceSpecialChars(templateLocation);
928-
replaceColors(templateLocation);
942+
replaceColors(templateLocation, eraseColors);
929943
}
930944

931945
std::string getClassification(const std::string &guideline, ReportType reportType) {

lib/errorlogger.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,10 @@ class CPPCHECKLIB ErrorLogger {
288288
std::string replaceStr(std::string s, const std::string &from, const std::string &to);
289289

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

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

296296
/** Get a classification string from the given guideline and reporttype */
297297
CPPCHECKLIB std::string getClassification(const std::string &guideline, ReportType reportType);

test/cli/other_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,6 +2426,41 @@ def test_xml_output(tmp_path): # #13391 / #13485
24262426
'''.format(version_str, test_file_exp, test_file_exp, test_file_exp))
24272427

24282428

2429+
def test_outputfile(tmp_path): # #14051
2430+
test_file = tmp_path / 'test.cpp'
2431+
out_file = tmp_path / 'out.txt'
2432+
with open(test_file, 'wt') as f:
2433+
f.write(
2434+
"""
2435+
int main()
2436+
{
2437+
int x = 1 / 0;
2438+
}
2439+
""")
2440+
2441+
args = [
2442+
'-q',
2443+
'--output-file={}'.format(out_file),
2444+
str(test_file)
2445+
]
2446+
2447+
out_exp = [
2448+
'{}:4:15: error: Division by zero. [zerodiv]'.format(test_file),
2449+
' int x = 1 / 0;',
2450+
' ^',
2451+
]
2452+
2453+
exitcode, stdout, stderr = cppcheck(args)
2454+
assert exitcode == 0, stdout
2455+
assert stdout == ''
2456+
assert stderr == ''
2457+
2458+
with open(out_file, 'rt') as f:
2459+
out_text = f.read()
2460+
2461+
assert out_text.splitlines() == out_exp
2462+
2463+
24292464
def test_internal_error_loc_int(tmp_path):
24302465
test_file = tmp_path / 'test.c'
24312466
with open(test_file, 'wt') as f:

0 commit comments

Comments
 (0)