diff-report.py tool
v3 cover diff-report.py tool
--------------------------------------------------------------------------------
This serie is dependent on this patch, in case cppcheck report are generated using xen-analysis.py that calls the makefile with in-tree build, because this tool (in particular the patching feature) needs the path from the repository instead of the path from /xen/xen:
https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/
--------------------------------------------------------------------------------
Now that we have a tool (xen-analysis.py) that wraps cppcheck to generates reports, we have a generic overview of how many static analysis issues and non compliances to MISRA C we have for a certain revision of the codebase.
This is great and eventually the work to be done should be just having less and less findings in the report until we reach zero.
This is just an ideal trend, because in practice we might have issues that comes from existing code (macro for example) that are not going to be fixed soon for any reason, but we would like to check and see how many issues are introduced by the commits added (ideally zero, but if any is added and the fault resides outside the changed code, maintainers might decide to include it anyway).
So the idea is to check the difference between the reports of the codebase: one called "baseline" which is basically the current codebase, the other one called "new report" that is the codebase after the changes.
To check if any new finding is added, we need to have a look on every finding in the "new report" that is not listed in the "baseline".
It seems very simple, but what can happen to existing findings in the code after a commit is applied?
Basically existing findings can be shifted in position due to changes to unrelated lines, or they can be also deleted or fixed for changes involving the findings line (Michal was the first one to point that out).
So comparing the two report now seems quite difficult, because if we compare them we will have all the new findings plus all the findings that changed position due to the changes applied.
To overcome this, the diff-tool.py is created and it allows to "patch" the "baseline" report, looking into the changes applied to the baseline codebase, described by git diff.
This serie is organised in two patch, I've tried to split the amount of code and to leave a meaning, so in the first patch you have everything you need to import cppcheck reports and do a "raw" diff between reports, this gives you an hint about new findings + old findings that has changed place.
The second patch is adding the "patching" system, having a class that parses the git diff output and later "patching" the baseline before doing the comparison. This last option is activated only when passing the git diff changes to the tool, but everything is described (I hope) in the help.
Some consideration needs to be made, this tool can translate in coordinates (file, line) the findings from the "baseline" to the "new report" using the git diff output as, let me say, a translation matrix.
This doesn't mean it can understand the meaning of the findings and recognise them in the new codebase, so for example, a finding related to a line that is moved to another part of the file won't be recognised as "old finding" and will be just removed from the "baseline patched report", however we will find it in the new report unless it contains a fix for the reported issue.
This means that the tool is not really suited to be a gatekeeper for the merge action, it is more suitable to help the maintainer to understand when a change is introducing new issues without having to check and compare manually two reports of (nowadays) hundreds of finding.
Eventually we could run it in the CI and make the CI reply to the patchwork thread with its output!
The tool has also a debug argument that when applied, generates extra files that can be checked against the original files, for example the reports are imported in the tool, and afterwards the debug code will generate them back from the imported data and they should be the same (if everything works). Another debug check is to export the representation of the parsed git diff output, so that the developer can check how and if the parser interpreted correctly the data.
Future works for this tool might be to parse also Coverity reports and eventually (don't know if it is possible) also eclair text report.
Luca Fancellu (3):
xen/misra: add diff-report.py tool
xen/misra: diff-report.py: add report patching feature
maintainers: Add Xen MISRA Analysis Tools section
MAINTAINERS | 10 + xen/scripts/diff-report.py | 130 ++++++++++ .../xen_analysis/diff_tool/__init__.py | 0 .../xen_analysis/diff_tool/cppcheck_report.py | 44 ++++ xen/scripts/xen_analysis/diff_tool/debug.py | 61 +++++ xen/scripts/xen_analysis/diff_tool/report.py | 187 ++++++++++++++ .../diff_tool/unified_format_parser.py | 232 ++++++++++++++++++ 7 files changed, 664 insertions(+) create mode 100755 xen/scripts/diff-report.py create mode 100644 xen/scripts/xen_analysis/diff_tool/__init__.py create mode 100644 xen/scripts/xen_analysis/diff_tool/cppcheck_report.py create mode 100644 xen/scripts/xen_analysis/diff_tool/debug.py create mode 100644 xen/scripts/xen_analysis/diff_tool/report.py create mode 100644 xen/scripts/xen_analysis/diff_tool/unified_format_parser.py
Signature · 1 line
2.34.1
PATCH v3 1/3 xen/misra: add diff-report.py tool
Add a new tool, diff-report.py that can be used to make diff between reports generated by xen-analysis.py tool.
Currently this tool supports the Xen cppcheck text report format in its operations.
The tool prints every finding that is in the report passed with -r (check report) which is not in the report passed with -b (baseline).
---
Changes from v2:
- Add A-by, T-by Stefano
Changes from v1:
- removed 2 method from class ReportEntry that landed there by a mistake on rebase.
- Made the script compatible also with python2
--- xen/scripts/diff-report.py | 80 ++++++++++++++ .../xen_analysis/diff_tool/__init__.py | 0 .../xen_analysis/diff_tool/cppcheck_report.py | 44 ++++++++ xen/scripts/xen_analysis/diff_tool/debug.py | 40 +++++++ xen/scripts/xen_analysis/diff_tool/report.py | 100 ++++++++++++++++++ 5 files changed, 264 insertions(+) create mode 100755 xen/scripts/diff-report.py create mode 100644 xen/scripts/xen_analysis/diff_tool/__init__.py create mode 100644 xen/scripts/xen_analysis/diff_tool/cppcheck_report.py create mode 100644 xen/scripts/xen_analysis/diff_tool/debug.py create mode 100644 xen/scripts/xen_analysis/diff_tool/report.py
xen/scripts/diff-report.py new file +80 -0
@@ -0,0 +1,80 @@ +#!/usr/bin/env python3 + +from __future__ import print_function +import os +import sys +from argparse import ArgumentParser +from xen_analysis.diff_tool.cppcheck_report import CppcheckReport +from xen_analysis.diff_tool.debug import Debug +from xen_analysis.diff_tool.report import ReportError + + +def log_info(text, end='\n'): + # type: (str, str) -> None + global args + global file_out + + if (args.verbose): + print(text, end=end, file=file_out) + + +def main(argv): + # type: (list) -> None + global args + global file_out + + parser = ArgumentParser(prog="diff-report.py") + parser.add_argument("-b", "--baseline", required=True, type=str, + help="Path to the baseline report.") + parser.add_argument("--debug", action='store_true', + help="Produce intermediate reports during operations.") + parser.add_argument("-o", "--out", default="stdout", type=str, + help="Where to print the tool output. Default is " + "stdout") + parser.add_argument("-r", "--report", required=True, type=str, + help="Path to the 'check report', the one checked " + "against the baseline.") + parser.add_argument("-v", "--verbose", action='store_true', + help="Print more informations during the run.") + + args = parser.parse_args() + + if args.out == "stdout": + file_out = sys.stdout + else: + try: + file_out = open(args.out, "wt") + except OSError as e: + print("ERROR: Issue opening file {}: {}".format(args.out, e)) + sys.exit(1) + + debug = Debug(args) + + try: + baseline_path = os.path.realpath(args.baseline) + log_info("Loading baseline report {}".format(baseline_path), "") + baseline = CppcheckReport(baseline_path) + baseline.parse() + debug.debug_print_parsed_report(baseline) + log_info(" [OK]") + new_rep_path = os.path.realpath(args.report) + log_info("Loading check report {}".format(new_rep_path), "") + new_rep = CppcheckReport(new_rep_path) + new_rep.parse() + debug.debug_print_parsed_report(new_rep) + log_info(" [OK]") + except ReportError as e: + print("ERROR: {}".format(e)) + sys.exit(1) + + output = new_rep - baseline + print(output, end="", file=file_out) + + if len(output) > 0: + sys.exit(1) + + sys.exit(0) + + +if __name__ == "__main__": + main(sys.argv[1:])
xen/scripts/xen_analysis/diff_tool/__init__.py new file +0 -0
xen/scripts/xen_analysis/diff_tool/cppcheck_report.py new file +44 -0
44 lines not on this page · raw message
xen/scripts/xen_analysis/diff_tool/debug.py new file +40 -0
@@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +from __future__ import print_function +import os +from .report import Report + + +class Debug: + def __init__(self, args): + self.args = args + + def __get_debug_out_filename(self, path, type): + # type: (str, str) -> str + # Take basename + file_name = os.path.basename(path) + # Split in name and extension + file_name = os.path.splitext(file_name) + if self.args.out != "stdout": + out_folder = os.path.dirname(self.args.out) + else: + out_folder = "./" + dbg_report_path = out_folder + file_name[0] + type + file_name[1] + + return dbg_report_path + + def __debug_print_report(self, report, type): + # type: (Report, str) -> None + report_name = self.__get_debug_out_filename(report.get_report_path(), + type) + try: + with open(report_name, "wt") as outfile: + print(report, end="", file=outfile) + except OSError as e: + print("ERROR: Issue opening file {}: {}".format(report_name, e)) + + def debug_print_parsed_report(self, report): + # type: (Report) -> None + if not self.args.debug: + return + self.__debug_print_report(report, ".parsed")
xen/scripts/xen_analysis/diff_tool/report.py new file +100 -0
100 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 2/3 xen/misra: diff-report.py: add report patching feature
In reply to Luca FancelluAdd a feature to the diff-report.py script that improves the comparison between two analysis report, one from a baseline codebase and the other from the changes applied to the baseline.
The comparison between reports of different codebase is an issue because entries in the baseline could have been moved in position due to addition or deletion of unrelated lines or can disappear because of deletion of the interested line, making the comparison between two revisions of the code harder.
Having a baseline report, a report of the codebase with the changes called "new report" and a git diff format file that describes the changes happened to the code from the baseline, this feature can understand which entries from the baseline report are deleted or shifted in position due to changes to unrelated lines and can modify them as they will appear in the "new report".
Having the "patched baseline" and the "new report", now it's simple to make the diff between them and print only the entry that are new.
---
Changes from v2:
- Fix --baseline-rev/--report-rev git command
- Add A-by, T-by Stefano
Changes from v1:
- Made the script compatible with python2
--- xen/scripts/diff-report.py | 54 +++- xen/scripts/xen_analysis/diff_tool/debug.py | 21 ++ xen/scripts/xen_analysis/diff_tool/report.py | 87 +++++++ .../diff_tool/unified_format_parser.py | 232 ++++++++++++++++++ 4 files changed, 392 insertions(+), 2 deletions(-) create mode 100644 xen/scripts/xen_analysis/diff_tool/unified_format_parser.py
xen/scripts/diff-report.py +52 -2
@@ -7,6 +7,10 @@ from argparse import ArgumentParser from xen_analysis.diff_tool.cppcheck_report import CppcheckReport from xen_analysis.diff_tool.debug import Debug from xen_analysis.diff_tool.report import ReportError +from xen_analysis.diff_tool.unified_format_parser import \ + (UnifiedFormatParser, UnifiedFormatParseError) +from xen_analysis.settings import repo_dir +from xen_analysis.utils import invoke_command def log_info(text, end='\n'):
@@ -36,9 +40,32 @@ def main(argv): "against the baseline.") parser.add_argument("-v", "--verbose", action='store_true', help="Print more informations during the run.") + parser.add_argument("--patch", type=str, + help="The patch file containing the changes to the " + "code, from the baseline analysis result to the " + "'check report' analysis result.\n" + "Do not use with --baseline-rev/--report-rev") + parser.add_argument("--baseline-rev", type=str, + help="Revision or SHA of the codebase analysed to " + "create the baseline report.\n" + "Use together with --report-rev") + parser.add_argument("--report-rev", type=str, + help="Revision or SHA of the codebase analysed to " + "create the 'check report'.\n" + "Use together with --baseline-rev") args = parser.parse_args() + if args.patch and (args.baseline_rev or args.report_rev): + print("ERROR: '--patch' argument can't be used with '--baseline-rev'" + " or '--report-rev'.") + sys.exit(1) + + if bool(args.baseline_rev) != bool(args.report_rev): + print("ERROR: '--baseline-rev' must be used together with " + "'--report-rev'.") + sys.exit(1) + if args.out == "stdout": file_out = sys.stdout else:
@@ -63,11 +90,34 @@ def main(argv): new_rep.parse() debug.debug_print_parsed_report(new_rep) log_info(" [OK]") - except ReportError as e: + diff_source = None + if args.patch: + diff_source = os.path.realpath(args.patch) + elif args.baseline_rev: + git_diff = invoke_command( + "git --git-dir={}/.git diff -C -C {}..{}" + .format(repo_dir, args.baseline_rev, args.report_rev), + True, "Error occured invoking:\n{}\n\n{}" + ) + diff_source = git_diff.splitlines(keepends=True) + if diff_source: + log_info("Parsing changes...", "") + diffs = UnifiedFormatParser(diff_source) + debug.debug_print_parsed_diff(diffs) + log_info(" [OK]") + except (ReportError, UnifiedFormatParseError) as e: print("ERROR: {}".format(e)) sys.exit(1) - output = new_rep - baseline + if args.patch or args.baseline_rev: + log_info("Patching baseline...", "") + baseline_patched = baseline.patch(diffs) + debug.debug_print_patched_report(baseline_patched) + log_info(" [OK]") + output = new_rep - baseline_patched + else: + output = new_rep - baseline + print(output, end="", file=file_out) if len(output) > 0:
xen/scripts/xen_analysis/diff_tool/debug.py +21 -0
@@ -3,6 +3,7 @@ from __future__ import print_function import os from .report import Report +from .unified_format_parser import UnifiedFormatParser class Debug:
@@ -38,3 +39,23 @@ class Debug: if not self.args.debug: return self.__debug_print_report(report, ".parsed") + + def debug_print_patched_report(self, report): + # type: (Report) -> None + if not self.args.debug: + return + # The patched report contains already .patched in its name + self.__debug_print_report(report, "") + + def debug_print_parsed_diff(self, diff): + # type: (UnifiedFormatParser) -> None + if not self.args.debug: + return + diff_filename = diff.get_diff_path() + out_pathname = self.__get_debug_out_filename(diff_filename, ".parsed") + try: + with open(out_pathname, "wt") as outfile: + for change_obj in diff.get_change_sets().values(): + print(change_obj, end="", file=outfile) + except OSError as e: + print("ERROR: Issue opening file {}: {}".format(out_pathname, e))
xen/scripts/xen_analysis/diff_tool/report.py +87 -0
99 lines not on this page · raw message
xen/scripts/xen_analysis/diff_tool/unified_format_parser.py new file +232 -0
232 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 3/3 maintainers: Add Xen MISRA Analysis Tools section
In reply to Luca FancelluAdd a section for the Xen MISRA Analysis Tools.
---
Changes from v2:
- New patch, suggested by Stefano:
https://lore.kernel.org/all/alpine.DEB.2.22.394.2305171232440.128889@ubuntu-linux-20-04-desktop/
--- MAINTAINERS | 10 ++++++++++ 1 file changed, 10 insertions(+)
MAINTAINERS +10 -0
@@ -667,6 +667,16 @@ F: tools/xentrace/ F: xen/common/trace.c F: xen/include/xen/trace.h +XEN MISRA ANALYSIS TOOLS +M: Luca Fancellu <[email protected]> +S: Supported +F: xen/scripts/xen_analysis/ +F: xen/scripts/xen-analysis.py +F: xen/scripts/diff-report.py +F: xen/tools/cppcheck-plat/ +F: xen/tools/convert_misra_doc.py +F: xen/tools/cppcheck-cc.sh + XSM/FLASK M: Daniel P. Smith <[email protected]> S: Supported
Signature · 1 line
2.34.1
Re: [PATCH v3 3/3] maintainers: Add Xen MISRA Analysis Tools section
Quoted 3 lines · Luca Fancellu
Add a section for the Xen MISRA Analysis Tools.
Signed-off-by Luca Fancellu <[email protected]>
Quoted 32 lines
---
Changes from v2:
- New patch, suggested by Stefano:https://lore.kernel.org/all/alpine.DEB.2.22.394.2305171232440.128889@ubuntu-linux-20-04-desktop/ --- MAINTAINERS | 10 ++++++++++ 1 file changed, 10 insertions(+)MAINTAINERS +10
-0@@ -667,6 +667,16 @@ F: tools/xentrace/ F: xen/common/trace.c F: xen/include/xen/trace.h +XEN MISRA ANALYSIS TOOLS +M: Luca Fancellu <[email protected]> +S: Supported +F: xen/scripts/xen_analysis/ +F: xen/scripts/xen-analysis.py +F: xen/scripts/diff-report.py +F: xen/tools/cppcheck-plat/ +F: xen/tools/convert_misra_doc.py +F: xen/tools/cppcheck-cc.sh + XSM/FLASK M: Daniel P. Smith <[email protected]> S: SupportedSignature · 1 line
2.34.1
Quoted 96 lines · Luca Fancellu
--------------------------------------------------------------------------------
This serie is dependent on this patch, in case cppcheck report are generated usingxen-analysis.pythat calls the makefile with in-tree build, because this tool (in particular the patching feature) needs the path from the repository instead of the path from /xen/xen:
https://patchwork.kernel.org/project/xen-devel/patch/[email protected]/
--------------------------------------------------------------------------------Now that we have a tool (
xen-analysis.py) that wraps cppcheck to generates reports, we have a generic overview of how many static analysis issues and non compliances to MISRA C we have for a certain revision of the codebase.This is great and eventually the work to be done should be just having less and less findings in the report until we reach zero.
This is just an ideal trend, because in practice we might have issues that comes from existing code (macro for example) that are not going to be fixed soon for any reason, but we would like to check and see how many issues are introduced by the commits added (ideally zero, but if any is added and the fault resides outside the changed code, maintainers might decide to include it anyway).
So the idea is to check the difference between the reports of the codebase: one called "baseline" which is basically the current codebase, the other one called "new report" that is the codebase after the changes.
To check if any new finding is added, we need to have a look on every finding in the "new report" that is not listed in the "baseline".It seems very simple, but what can happen to existing findings in the code after a commit is applied?
Basically existing findings can be shifted in position due to changes to unrelated lines, or they can be also deleted or fixed for changes involving the findings line (Michal was the first one to point that out).So comparing the two report now seems quite difficult, because if we compare them we will have all the new findings plus all the findings that changed position due to the changes applied.
To overcome this, the
diff-tool.pyis created and it allows to "patch" the "baseline" report, looking into the changes applied to the baseline codebase, described by git diff.This serie is organised in two patch, I've tried to split the amount of code and to leave a meaning, so in the first patch you have everything you need to import cppcheck reports and do a "raw" diff between reports, this gives you an hint about new findings + old findings that has changed place.
The second patch is adding the "patching" system, having a class that parses the git diff output and later "patching" the baseline before doing the comparison. This last option is activated only when passing the git diff changes to the tool, but everything is described (I hope) in the help.
Some consideration needs to be made, this tool can translate in coordinates (file, line) the findings from the "baseline" to the "new report" using the git diff output as, let me say, a translation matrix.
This doesn't mean it can understand the meaning of the findings and recognise them in the new codebase, so for example, a finding related to a line that is moved to another part of the file won't be recognised as "old finding" and will be just removed from the "baseline patched report", however we will find it in the new report unless it contains a fix for the reported issue.This means that the tool is not really suited to be a gatekeeper for the merge action, it is more suitable to help the maintainer to understand when a change is introducing new issues without having to check and compare manually two reports of (nowadays) hundreds of finding.
Eventually we could run it in the CI and make the CI reply to the patchwork thread with its output!The tool has also a debug argument that when applied, generates extra files that can be checked against the original files, for example the reports are imported in the tool, and afterwards the debug code will generate them back from the imported data and they should be the same (if everything works). Another debug check is to export the representation of the parsed git diff output, so that the developer can check how and if the parser interpreted correctly the data.
Future works for this tool might be to parse also Coverity reports and eventually (don't know if it is possible) also eclair text report.
Luca Fancellu (3):
xen/misra: adddiff-report.pytool
xen/misra:diff-report.py: add report patching feature
maintainers: Add Xen MISRA Analysis Tools sectionMAINTAINERS | 10 + xen/scripts/diff-report.py | 130 ++++++++++ .../xen_analysis/diff_tool/__init__.py | 0 .../xen_analysis/diff_tool/cppcheck_report.py | 44 ++++ xen/scripts/xen_analysis/diff_tool/debug.py | 61 +++++ xen/scripts/xen_analysis/diff_tool/report.py | 187 ++++++++++++++ .../diff_tool/unified_format_parser.py | 232 ++++++++++++++++++ 7 files changed, 664 insertions(+)
Quoted 11 lines
create mode 100755
xen/scripts/diff-report.py
create mode 100644xen/scripts/xen_analysis/diff_tool/__init__.pycreate mode 100644xen/scripts/xen_analysis/diff_tool/cppcheck_report.pycreate mode 100644xen/scripts/xen_analysis/diff_tool/debug.pycreate mode 100644xen/scripts/xen_analysis/diff_tool/report.pycreate mode 100644xen/scripts/xen_analysis/diff_tool/unified_format_parser.pySignature · 1 line
2.34.1
Hi All,
Is it possible to commit this serie? I think it has T-by and A-by Stefano for all the patches and I didn’t receive so far comments against it
Thank you, please read this mail as a gentle ping :)