Skip to contentSkip to conversation

diff-report bugfix

PATCH In review Luca Fancellu 2 participants 5 messages Jul 18, 2023 – Jul 20, 2023

lore
Share & tools
lore
#0 lore raw

cover diff-report bugfix

This serie has some bugfix for some issues found on the diff-report.py tool during usage of its module or by luck by code inspection.

Luca Fancellu (2):
xen/misra: diff-report.py: Fix UnifiedFormatParser change line
registration
xen/misra: diff-report.py: fix function invocation

 xen/scripts/diff-report.py                                | 8 ++++++--
 .../xen_analysis/diff_tool/unified_format_parser.py       | 7 ++++++-
 2 files changed, 12 insertions(+), 3 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 xen/misra: diff-report.py: Fix UnifiedFormatParser change line registration

Fix the line number on the registration of a 'remove' change type when consecutive 'remove' changes are registered.

Currently the algorithm registers consecutive 'remove' changes at the same line it encounter the first one, 'add' changes type are not affected by the bug.

Fixes 1d7c45f895b6 ("xen/misra: diff-report.py: add report patching feature")
---
 .../xen_analysis/diff_tool/unified_format_parser.py        | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
xen/scripts/xen_analysis/diff_tool/unified_format_parser.py +6 -1
index 8b3fbc318df7..6c506caeafce 100644
@@ -144,6 +144,7 @@ class UnifiedFormatParser(object):
         file_linenum = 0
         hunk_a_linemax = 0
         hunk_b_linemax = 0
+        consecutive_remove = 0
         diff_elem = None
         parse_state = ParserState.FIND_DIFF_HEADER
         ChangeMode = ChangeSet.ChangeMode
@@ -210,14 +211,18 @@ class UnifiedFormatParser(object):
                 if (hunk_b_linemax > 0) and line.startswith("+"):
                     diff_elem.add_change(file_linenum, ChangeType.ADD)
                     hunk_b_linemax -= 1
+                    consecutive_remove = 0
                 elif (hunk_a_linemax > 0) and line.startswith("-"):
-                    diff_elem.add_change(file_linenum, ChangeType.REMOVE)
+                    diff_elem.add_change(file_linenum + consecutive_remove,
+                                         ChangeType.REMOVE)
                     hunk_a_linemax -= 1
                     file_linenum -= 1
+                    consecutive_remove += 1
                 elif ((hunk_a_linemax + hunk_b_linemax) > 0) and \
                         line.startswith(" "):
                     hunk_a_linemax -= 1 if (hunk_a_linemax > 0) else 0
                     hunk_b_linemax -= 1 if (hunk_b_linemax > 0) else 0
+                    consecutive_remove = 0
 
                 if (hunk_a_linemax + hunk_b_linemax) <= 0:
                     parse_state = ParserState.FIND_HUNK_OR_DIFF_HEADER
Signature · 1 line
2.34.1
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] xen/misra: diff-report.py: Fix UnifiedFormatParser change line registration

Quoted 9 lines · Luca Fancellu

Fix the line number on the registration of a 'remove' change type when consecutive 'remove' changes are registered.

Currently the algorithm registers consecutive 'remove' changes at the same line it encounter the first one, 'add' changes type are not affected by the bug.

Fixes 1d7c45f895b6 ("xen/misra: diff-report.py: add report patching feature")
Quoted 39 lines
---
 .../xen_analysis/diff_tool/unified_format_parser.py        | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
xen/scripts/xen_analysis/diff_tool/unified_format_parser.py +6 -1
index 8b3fbc318df7..6c506caeafce 100644
@@ -144,6 +144,7 @@ class UnifiedFormatParser(object):
         file_linenum = 0
         hunk_a_linemax = 0
         hunk_b_linemax = 0
+        consecutive_remove = 0
         diff_elem = None
         parse_state = ParserState.FIND_DIFF_HEADER
         ChangeMode = ChangeSet.ChangeMode
@@ -210,14 +211,18 @@ class UnifiedFormatParser(object):
                 if (hunk_b_linemax > 0) and line.startswith("+"):
                     diff_elem.add_change(file_linenum, ChangeType.ADD)
                     hunk_b_linemax -= 1
+                    consecutive_remove = 0
                 elif (hunk_a_linemax > 0) and line.startswith("-"):
-                    diff_elem.add_change(file_linenum, ChangeType.REMOVE)
+                    diff_elem.add_change(file_linenum + consecutive_remove,
+                                         ChangeType.REMOVE)
                     hunk_a_linemax -= 1
                     file_linenum -= 1
+                    consecutive_remove += 1
                 elif ((hunk_a_linemax + hunk_b_linemax) > 0) and \
                         line.startswith(" "):
                     hunk_a_linemax -= 1 if (hunk_a_linemax > 0) else 0
                     hunk_b_linemax -= 1 if (hunk_b_linemax > 0) else 0
+                    consecutive_remove = 0
 
                 if (hunk_a_linemax + hunk_b_linemax) <= 0:
                     parse_state = ParserState.FIND_HUNK_OR_DIFF_HEADER
Signature · 1 line
2.34.1
#3 lore raw

PATCH 2/2 xen/misra: diff-report.py: fix function invocation

In reply to Luca Fancellu

Fix the invocation of invoke_command() that takes an optional parameter for the exception type, but in the code the error message template was passed instead, so fix it passing a new exception type.

Fixes 1d7c45f895b6 ("xen/misra: diff-report.py: add report patching feature")
---
 xen/scripts/diff-report.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
xen/scripts/diff-report.py +6 -2
index 636f98f5eebe..a1fe6bc2fccd 100755
@@ -13,6 +13,10 @@ from xen_analysis.settings import repo_dir
 from xen_analysis.utils import invoke_command
 
 
+class DiffReportError(Exception):
+    pass
+
+
 def log_info(text, end='\n'):
     # type: (str, str) -> None
     global args
@@ -97,7 +101,7 @@ def main(argv):
             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{}"
+                True, DiffReportError, "Error occured invoking:\n{}\n\n{}"
             )
             diff_source = git_diff.splitlines(keepends=True)
         if diff_source:
@@ -105,7 +109,7 @@ def main(argv):
             diffs = UnifiedFormatParser(diff_source)
             debug.debug_print_parsed_diff(diffs)
             log_info(" [OK]")
-    except (ReportError, UnifiedFormatParseError) as e:
+    except (DiffReportError, ReportError, UnifiedFormatParseError) as e:
         print("ERROR: {}".format(e))
         sys.exit(1)
 
Signature · 1 line
2.34.1
Acked-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] xen/misra: diff-report.py: fix function invocation

Quoted 7 lines · Luca Fancellu

Fix the invocation of invoke_command() that takes an optional parameter for the exception type, but in the code the error message template was passed instead, so fix it passing a new exception type.

Fixes 1d7c45f895b6 ("xen/misra: diff-report.py: add report patching feature")
Quoted 40 lines
---
 xen/scripts/diff-report.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
xen/scripts/diff-report.py +6 -2
index 636f98f5eebe..a1fe6bc2fccd 100755
@@ -13,6 +13,10 @@ from xen_analysis.settings import repo_dir
 from xen_analysis.utils import invoke_command
 
 
+class DiffReportError(Exception):
+    pass
+
+
 def log_info(text, end='\n'):
     # type: (str, str) -> None
     global args
@@ -97,7 +101,7 @@ def main(argv):
             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{}"
+                True, DiffReportError, "Error occured invoking:\n{}\n\n{}"
             )
             diff_source = git_diff.splitlines(keepends=True)
         if diff_source:
@@ -105,7 +109,7 @@ def main(argv):
             diffs = UnifiedFormatParser(diff_source)
             debug.debug_print_parsed_diff(diffs)
             log_info(" [OK]")
-    except (ReportError, UnifiedFormatParseError) as e:
+    except (DiffReportError, ReportError, UnifiedFormatParseError) as e:
         print("ERROR: {}".format(e))
         sys.exit(1)
 
Signature · 1 line
2.34.1