diff mbox

[ovs-dev,1/3] checkpatch: Check for infix operator whitespace.

Message ID 20170809203752.16886-1-joe@ovn.org
State Accepted
Headers show

Commit Message

Joe Stringer Aug. 9, 2017, 8:37 p.m. UTC
The 'Expressions' section of the coding style specifies that one space
should be on either side of infix binary and ternary operators. This
adds a check to checkpatch.py for most of these.

The regex won't match if there are speech marks on the line, because
the style should not apply to the contents of strings.

This check is left at warning level because there isn't a good way to
determine whether a line is within a multiline comment or string, so it
will occasionally flag such lines which contain hyphenated words.

Signed-off-by: Joe Stringer <joe@ovn.org>
---
 utilities/checkpatch.py | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

Comments

Ben Pfaff Aug. 9, 2017, 8:58 p.m. UTC | #1
On Wed, Aug 09, 2017 at 01:37:50PM -0700, Joe Stringer wrote:
> The 'Expressions' section of the coding style specifies that one space
> should be on either side of infix binary and ternary operators. This
> adds a check to checkpatch.py for most of these.
> 
> The regex won't match if there are speech marks on the line, because
> the style should not apply to the contents of strings.
> 
> This check is left at warning level because there isn't a good way to
> determine whether a line is within a multiline comment or string, so it
> will occasionally flag such lines which contain hyphenated words.
> 
> Signed-off-by: Joe Stringer <joe@ovn.org>

Acked-by: Ben Pfaff <blp@ovn.org>
Joe Stringer Aug. 9, 2017, 11:57 p.m. UTC | #2
On 9 August 2017 at 13:58, Ben Pfaff <blp@ovn.org> wrote:
> On Wed, Aug 09, 2017 at 01:37:50PM -0700, Joe Stringer wrote:
>> The 'Expressions' section of the coding style specifies that one space
>> should be on either side of infix binary and ternary operators. This
>> adds a check to checkpatch.py for most of these.
>>
>> The regex won't match if there are speech marks on the line, because
>> the style should not apply to the contents of strings.
>>
>> This check is left at warning level because there isn't a good way to
>> determine whether a line is within a multiline comment or string, so it
>> will occasionally flag such lines which contain hyphenated words.
>>
>> Signed-off-by: Joe Stringer <joe@ovn.org>
>
> Acked-by: Ben Pfaff <blp@ovn.org>

Thanks, applied to master.
diff mbox

Patch

diff --git a/utilities/checkpatch.py b/utilities/checkpatch.py
index 684c8b201d22..8b9ca0f504d3 100755
--- a/utilities/checkpatch.py
+++ b/utilities/checkpatch.py
@@ -275,6 +275,25 @@  checks += [
     for (function_name, description) in std_functions]
 
 
+def regex_operator_factory(operator):
+    regex = re.compile(r'^[^#][^"\']*[^ "]%s[^ "\'][^"]*' % operator)
+    return lambda x: regex.search(x) is not None
+
+
+infix_operators = \
+    [re.escape(op) for op in ['/', '%', '<<', '>>', '<=', '>=', '==', '!=',
+            '^', '|', '&&', '||', '?:', '=', '+=', '-=', '*=', '/=', '%=',
+            '&=', '^=', '|=', '<<=', '>>=']] \
+    + ['[^<" ]<[^=" ]', '[^->" ]>[^=" ]', '[^ !()/"]\*[^/]', '[^ !&()"]&',
+       '[^" +(]\+[^"+;]', '[^" -(]-[^"->;]', '[^" <>=!^|+\-*/%&]=[^"=]']
+checks += [
+    {'regex': '(\.c|\.h)(\.in)?$', 'match_name': None,
+     'prereq': lambda x: not is_comment_line(x),
+     'check': regex_operator_factory(operator),
+     'print': lambda: print_warning("Line lacks whitespace around operator")}
+    for operator in infix_operators]
+
+
 def get_file_type_checks(filename):
     """Returns the list of checks for a file based on matching the filename
        against regex."""