diff mbox

[ovs-dev,6/6] checkpatch: Print file line numbers

Message ID 1475871477-11608-7-git-send-email-aconole@redhat.com
State Changes Requested
Headers show

Commit Message

Aaron Conole Oct. 7, 2016, 8:17 p.m. UTC
The line numbers being printed were the line numbers for the patchfile.
This is sometimes okay to fix simple things (trailing or leading
whitespace, missing signoffs, etc).  But more complicated fixes, or
those fixes which require a bit more care, aren't helped by this.  So,
we use the implied file line number.

This can be useful with future work to 'mock' apply and build a real
contextual scanner for checking multi-line changes.

Signed-off-by: Aaron Conole <aconole@redhat.com>
---
 utilities/checkpatch.py | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
diff mbox

Patch

diff --git a/utilities/checkpatch.py b/utilities/checkpatch.py
index c4361f0..26ecc9e 100755
--- a/utilities/checkpatch.py
+++ b/utilities/checkpatch.py
@@ -52,6 +52,7 @@  def print_warning(message, lineno=None):
 
 
 __regex_added_line = re.compile(r'^\+{1,2}[^\+][\w\W]*')
+__regex_subtracted_line = re.compile(r'^\-{1,2}[^\-][\w\W]*')
 __regex_leading_with_whitespace_at_all = re.compile(r'^\s+')
 __regex_leading_with_spaces = re.compile(r'^ +[\S]+')
 __regex_trailing_whitespace = re.compile(r'[^\S]+$')
@@ -77,6 +78,10 @@  line_length_blacklist = ['.am', '.at', 'etc', '.in', '.m4', '.mk', '.patch',
                          '.py']
 
 
+def is_subtracted_line(line):
+    """Returns TRUE if the line in question has been removed."""
+    return __regex_subtracted_line.search(line) is not None
+
 def is_added_line(line):
     """Returns TRUE if the line in question is an added line.
     """
@@ -148,6 +153,8 @@  def ovs_checkpatch_parse(text):
     previous_file = ''
     scissors = re.compile(r'^[\w]*---[\w]*')
     hunks = re.compile('^(---|\+\+\+) (\S+)')
+    hunk_differences = re.compile(
+        r'^@@ ([1-9-+]+),([1-9-+]+) ([1-9-+]+),([1-9-+]+) @@')
     is_signature = re.compile(r'((\s*Signed-off-by: )(.*))$',
                               re.I | re.M | re.S)
     is_co_author = re.compile(r'(\s*(Co-authored-by: )(.*))$',
@@ -196,6 +203,14 @@  def ovs_checkpatch_parse(text):
                 current_file = newfile.group(2)
                 print_file_name = current_file
                 continue
+            reset_line_number = hunk_differences.match(line)
+            if reset_line_number:
+                lineno = int(reset_line_number.group(3))
+                if lineno < 0:
+                    lineno = -1 * lineno
+                lineno -= 1
+            if is_subtracted_line(line):
+                lineno -= 1
             if not is_added_line(line):
                 continue
             # Skip files which have /datapath in them, since they are