diff mbox

[ovs-dev] checkpatch: Also check switch, HMAP_FOR_EACH, etc.

Message ID 20170526183105.23052-1-blp@ovn.org
State Accepted
Headers show

Commit Message

Ben Pfaff May 26, 2017, 6:31 p.m. UTC
The switch statement and our FOR_EACH macro iteration constructs have the
same rules as if, for, and while.

Signed-off-by: Ben Pfaff <blp@ovn.org>
---
 utilities/checkpatch.py | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

Comments

Aaron Conole May 30, 2017, 8:24 p.m. UTC | #1
Ben Pfaff <blp@ovn.org> writes:

> The switch statement and our FOR_EACH macro iteration constructs have the
> same rules as if, for, and while.
>
> Signed-off-by: Ben Pfaff <blp@ovn.org>
> ---

At first, I was thinking that the FOR_EACH constructs were treated
separately, but actually it looks like there are just places where the
usage is incorrect.

Acked-by: Aaron Conole <aconole@redhat.com>
Ben Pfaff May 31, 2017, 3:55 p.m. UTC | #2
On Tue, May 30, 2017 at 04:24:55PM -0400, Aaron Conole wrote:
> Ben Pfaff <blp@ovn.org> writes:
> 
> > The switch statement and our FOR_EACH macro iteration constructs have the
> > same rules as if, for, and while.
> >
> > Signed-off-by: Ben Pfaff <blp@ovn.org>
> > ---
> 
> At first, I was thinking that the FOR_EACH constructs were treated
> separately, but actually it looks like there are just places where the
> usage is incorrect.

Yes, mistakes tend to creep in--perhaps this will help.

> Acked-by: Aaron Conole <aconole@redhat.com>

Thanks!  I applied this to master.
diff mbox

Patch

diff --git a/utilities/checkpatch.py b/utilities/checkpatch.py
index 295ecd4537bb..ec4b79e599df 100755
--- a/utilities/checkpatch.py
+++ b/utilities/checkpatch.py
@@ -62,18 +62,26 @@  def print_warning(message):
     __warnings = __warnings + 1
 
 
+# These are keywords whose names are normally followed by a space and
+# something in parentheses (usually an expression) then a left curly brace.
+#
+# 'do' almost qualifies but it's also used as "do { ... } while (...);".
+__parenthesized_constructs = 'if|for|while|switch|[_A-Z]+FOR_EACH[_A-Z]*'
+
 __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]+$')
 __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_missing_whitespace = re.compile(r' +(%s)[\(]'
+                                               % __parenthesized_constructs)
+__regex_for_if_too_much_whitespace = re.compile(r' +(%s)  +[\(]'
+                                                % __parenthesized_constructs)
 __regex_for_if_parens_whitespace = \
-    re.compile(r' +(if|for|while) \( +[\s\S]+\)')
+    re.compile(r' +(%s) \( +[\s\S]+\)' % __parenthesized_constructs)
 __regex_is_for_if_single_line_bracket = \
-    re.compile(r'^ +(if|for|while) \(.*\)')
+    re.compile(r'^ +(%s) \(.*\)' % __parenthesized_constructs)
 __regex_ends_with_bracket = \
     re.compile(r'[^\s]\) {(\s+/\*[\s\Sa-zA-Z0-9\.,\?\*/+-]*)?$')
 __regex_ptr_declaration_missing_whitespace = re.compile(r'[a-zA-Z0-9]\*[^*]')