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

Joe Stringer joe at ovn.org
Wed Aug 9 20:37:50 UTC 2017


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 at ovn.org>
---
 utilities/checkpatch.py | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

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."""
-- 
2.13.3



More information about the dev mailing list