diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 7010e5c3f5e..bbd88ac0c5c 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -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; diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 183d130025a..afc5a02ca5e 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -594,9 +594,9 @@ static void replace(std::string& source, const std::unordered_map substitutionMap = + static const std::unordered_map substitutionMapReplace = { {"{reset}", ::toString(Color::Reset)}, {"{bold}", ::toString(Color::Bold)}, @@ -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 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 @@ -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) { diff --git a/lib/errorlogger.h b/lib/errorlogger.h index 44e3b733cdc..5101f765fad 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -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); diff --git a/test/cli/other_test.py b/test/cli/other_test.py index f50f8d1f74c..ec73fef11e3 100644 --- a/test/cli/other_test.py +++ b/test/cli/other_test.py @@ -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: