diff --git a/addons/misra.py b/addons/misra.py index 7abc99f365a..5cca5f701dd 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -24,6 +24,7 @@ import argparse import string import copy +import junit_xml try: from itertools import izip as zip @@ -1552,6 +1553,9 @@ def __init__(self, settings, stdversion="c89"): self.ruleTexts = {} self.ruleText_filename = None + self.junit_testcases = [] + self.junit_filehandle = None + # Dictionary of dictionaries for rules to suppress # Dict1 is keyed by rule number in the hundreds format of # Major * 100 + minor. ie Rule 5.2 = (5*100) + 2 @@ -4540,6 +4544,11 @@ def reportError(self, location, num1, num2): if this_violation not in self.existing_violations: self.existing_violations.add(this_violation) cppcheckdata.reportError(location, cppcheck_severity, errmsg, 'misra', errorId, misra_severity) + if self.junit_filehandle: + loc = '[%s:%i]' % (location.file, location.linenr) + tc = junit_xml.TestCase(loc, 'misra', 1.0) + tc.add_error_info(errmsg, loc, errorId) + self.junit_testcases.append(tc) if misra_severity not in self.violations: self.violations[misra_severity] = [] @@ -5075,6 +5084,7 @@ def get_args_parser(): parser.add_argument("-generate-table", help=argparse.SUPPRESS, action="store_true") parser.add_argument("-verify", help=argparse.SUPPRESS, action="store_true") parser.add_argument("--severity", type=str, help="Set a custom severity string, for example 'error' or 'warning'. ") + parser.add_argument("--junit-file-export", type=str, help="Filename to export report as JUnit XML") return parser @@ -5104,6 +5114,16 @@ def main(): print('Fatal error: file is not found: ' + filename) sys.exit(1) + if args.junit_file_export: + filename = os.path.expanduser(args.junit_file_export) + filename = os.path.normpath(filename) + if os.path.isfile(filename): + print('JUnit file already exists and will be overwritten') + try: + checker.junit_filehandle = open(filename, 'w+') + except FileNotFoundError as e: + print(f'An error occurred: {e}') + if args.verify_rule_texts and not args.rule_texts: print("Error: Please specify rule texts file with --rule-texts=") @@ -5181,6 +5201,10 @@ def main(): if args.show_suppressed_rules: checker.showSuppressedRules() + if args.junit_file_export: + ts = junit_xml.TestSuite("MISRA2012_TS", checker.junit_testcases) + junit_xml.TestSuite.to_file(checker.junit_filehandle, [ts], prettyprint=True) + checker.junit_filehandle.close() if __name__ == '__main__': diff --git a/requirements.txt b/requirements.txt index 2ecb430828a..c0ae10ce189 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ pcre,pfultz2/pcre@8.45 -H sha256:d6f7182602a775a7d500a0cedca6449af0400c6493951513046d17615ed0bf11 +junit-xml \ No newline at end of file