[ovs-dev] [PATCH] utilities/checkpatch.py: Check for appropriate bracing

Aaron Conole aconole at redhat.com
Fri May 20 15:52:59 UTC 2016


Teach checkpatch.py to understand that if/for/while blocks should always
end with braces on the same line (if possible). This does not address
multi-line if/for/while blocks, but provides a point where such blocks
could be added.

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

diff --git a/utilities/checkpatch.py b/utilities/checkpatch.py
index b641560..754059a 100755
--- a/utilities/checkpatch.py
+++ b/utilities/checkpatch.py
@@ -51,6 +51,10 @@ __regex_single_line_feed = re.compile(r'^\f$')
 __regex_for_if_missing_whitespace = re.compile(r'(if|for|while)[\(]')
 __regex_for_if_too_much_whitespace = re.compile(r'(if|for|while)  +[\(]')
 __regex_for_if_parens_whitespace = re.compile(r'(if|for|while) \( +[\s\S]+\)')
+__regex_is_for_if_single_line_bracket = \
+    re.compile(r'^ +(if|for|while) \(.*\)')
+
+__regex_ends_with_bracket = re.compile(r'[^\s]\) {$')
 
 skip_leading_whitespace_check = False
 skip_trailing_whitespace_check = False
@@ -104,6 +108,28 @@ def if_and_for_whitespace_checks(line):
     return True
 
 
+def if_and_for_end_with_bracket_check(line):
+    """Return TRUE if there is not a bracket at the end of an if, for, while
+       block which fits on a single line ie: 'if (foo)'"""
+
+    def balanced_parens(line):
+        """This is a rather naive counter - it won't deal with quotes"""
+        balance = 0
+        for letter in line:
+            if letter is '(':
+                balance += 1
+            elif letter is ')':
+                balance -= 1
+        return balance is 0
+
+    if __regex_is_for_if_single_line_bracket.search(line) is not None:
+        if not balanced_parens(line):
+            return True
+        if __regex_ends_with_bracket.search(line) is None:
+            return False
+    return True
+
+
 def ovs_checkpatch_parse(text):
     lineno = 0
     signatures = []
@@ -182,6 +208,10 @@ def ovs_checkpatch_parse(text):
                 print_line = True
                 print_warning("Improper whitespace around control block",
                               lineno)
+            if not if_and_for_end_with_bracket_check(line[1:]):
+                print_line = True
+                print_warning("Inappropriate bracing around statement",
+                              lineno)
             if print_line:
                 print(line)
     if __errors or __warnings:
-- 
2.5.5




More information about the dev mailing list