]> err.no Git - linux-2.6/blobdiff - scripts/checkpatch.pl
[NETNS][UDP]: Register /proc/net/udp in a namespace.
[linux-2.6] / scripts / checkpatch.pl
index 545471a99eea1ff5e9feb9bfe28445fcf6575be8..2a7cef9726e4892b3454e7198dca4c529aeed891 100755 (executable)
@@ -9,7 +9,7 @@ use strict;
 my $P = $0;
 $P =~ s@.*/@@g;
 
-my $V = '0.13';
+my $V = '0.15';
 
 use Getopt::Long qw(:config no_auto_abbrev);
 
@@ -24,6 +24,7 @@ my $file = 0;
 my $check = 0;
 my $summary = 1;
 my $mailback = 0;
+my $summary_file = 0;
 my $root;
 my %debug;
 GetOptions(
@@ -31,7 +32,6 @@ GetOptions(
        'tree!'         => \$tree,
        'signoff!'      => \$chk_signoff,
        'patch!'        => \$chk_patch,
-       'test-type!'    => \$tst_type,
        'emacs!'        => \$emacs,
        'terse!'        => \$terse,
        'file!'         => \$file,
@@ -40,7 +40,10 @@ GetOptions(
        'root=s'        => \$root,
        'summary!'      => \$summary,
        'mailback!'     => \$mailback,
+       'summary-file!' => \$summary_file,
+
        'debug=s'       => \%debug,
+       'test-type!'    => \$tst_type,
 ) or exit;
 
 my $exit = 0;
@@ -48,13 +51,15 @@ my $exit = 0;
 if ($#ARGV < 0) {
        print "usage: $P [options] patchfile\n";
        print "version: $V\n";
-       print "options: -q           => quiet\n";
-       print "         --no-tree    => run without a kernel tree\n";
-       print "         --terse      => one line per report\n";
-       print "         --emacs      => emacs compile window format\n";
-       print "         --file       => check a source file\n";
-       print "         --strict     => enable more subjective tests\n";
-       print "         --root       => path to the kernel tree root\n";
+       print "options: -q               => quiet\n";
+       print "         --no-tree        => run without a kernel tree\n";
+       print "         --terse          => one line per report\n";
+       print "         --emacs          => emacs compile window format\n";
+       print "         --file           => check a source file\n";
+       print "         --strict         => enable more subjective tests\n";
+       print "         --root           => path to the kernel tree root\n";
+       print "         --no-summary     => suppress the per-file summary\n";
+       print "         --summary-file   => include the filename in summary\n";
        exit(1);
 }
 
@@ -100,8 +105,7 @@ our $Sparse = qr{
                        __iomem|
                        __must_check|
                        __init_refok|
-                       __kprobes|
-                       fastcall
+                       __kprobes
                }x;
 our $Attribute = qr{
                        const|
@@ -153,7 +157,10 @@ sub build_types {
                        \b
                        (?:const\s+)?
                        (?:unsigned\s+)?
-                       $all
+                       (?:
+                               $all|
+                               (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)
+                       )
                        (?:\s+$Sparse|\s+const)*
                        \b
                  }x;
@@ -213,6 +220,7 @@ for my $filename (@ARGV) {
                $exit = 1;
        }
        @rawlines = ();
+       @lines = ();
 }
 
 exit($exit);
@@ -321,14 +329,14 @@ sub sanitise_line {
        }
 
        # Clear out the comments.
-       while ($res =~ m@(/\*.*?\*/)@) {
-               substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]);
+       while ($res =~ m@(/\*.*?\*/)@g) {
+               substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
        }
        if ($res =~ m@(/\*.*)@) {
-               substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]);
+               substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
        }
        if ($res =~ m@^.(.*\*/)@) {
-               substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]);
+               substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
        }
 
        # The pathname on a #include may be surrounded by '<' and '>'.
@@ -352,10 +360,15 @@ sub ctx_statement_block {
        my $soff = $off;
        my $coff = $off - 1;
 
+       my $loff = 0;
+
        my $type = '';
        my $level = 0;
+       my $p;
        my $c;
        my $len = 0;
+
+       my $remainder;
        while (1) {
                #warn "CSB: blk<$blk>\n";
                # If we are about to drop off the end, pull in more
@@ -364,6 +377,7 @@ sub ctx_statement_block {
                        for (; $remain > 0; $line++) {
                                next if ($lines[$line] =~ /^-/);
                                $remain--;
+                               $loff = $len;
                                $blk .= $lines[$line] . "\n";
                                $len = length($blk);
                                $line++;
@@ -371,11 +385,13 @@ sub ctx_statement_block {
                        }
                        # Bail if there is no further context.
                        #warn "CSB: blk<$blk> off<$off> len<$len>\n";
-                       if ($off == $len) {
+                       if ($off >= $len) {
                                last;
                        }
                }
+               $p = $c;
                $c = substr($blk, $off, 1);
+               $remainder = substr($blk, $off);
 
                #warn "CSB: c<$c> type<$type> level<$level>\n";
                # Statement ends at the ';' or a close '}' at the
@@ -384,6 +400,13 @@ sub ctx_statement_block {
                        last;
                }
 
+               # An else is really a conditional as long as its not else if
+               if ($level == 0 && (!defined($p) || $p =~ /(?:\s|\})/) &&
+                               $remainder =~ /(else)(?:\s|{)/ &&
+                               $remainder !~ /else\s+if\b/) {
+                       $coff = $off + length($1);
+               }
+
                if (($type eq '' || $type eq '(') && $c eq '(') {
                        $level++;
                        $type = '(';
@@ -410,6 +433,10 @@ sub ctx_statement_block {
                }
                $off++;
        }
+       if ($off == $len) {
+               $line++;
+               $remain--;
+       }
 
        my $statement = substr($blk, $soff, $off - $soff + 1);
        my $condition = substr($blk, $soff, $coff - $soff + 1);
@@ -417,7 +444,82 @@ sub ctx_statement_block {
        #warn "STATEMENT<$statement>\n";
        #warn "CONDITION<$condition>\n";
 
-       return ($statement, $condition);
+       #print "off<$off> loff<$loff>\n";
+
+       return ($statement, $condition,
+                       $line, $remain + 1, $off - $loff + 1, $level);
+}
+
+sub statement_lines {
+       my ($stmt) = @_;
+
+       # Strip the diff line prefixes and rip blank lines at start and end.
+       $stmt =~ s/(^|\n)./$1/g;
+       $stmt =~ s/^\s*//;
+       $stmt =~ s/\s*$//;
+
+       my @stmt_lines = ($stmt =~ /\n/g);
+
+       return $#stmt_lines + 2;
+}
+
+sub statement_rawlines {
+       my ($stmt) = @_;
+
+       my @stmt_lines = ($stmt =~ /\n/g);
+
+       return $#stmt_lines + 2;
+}
+
+sub statement_block_size {
+       my ($stmt) = @_;
+
+       $stmt =~ s/(^|\n)./$1/g;
+       $stmt =~ s/^\s*{//;
+       $stmt =~ s/}\s*$//;
+       $stmt =~ s/^\s*//;
+       $stmt =~ s/\s*$//;
+
+       my @stmt_lines = ($stmt =~ /\n/g);
+       my @stmt_statements = ($stmt =~ /;/g);
+
+       my $stmt_lines = $#stmt_lines + 2;
+       my $stmt_statements = $#stmt_statements + 1;
+
+       if ($stmt_lines > $stmt_statements) {
+               return $stmt_lines;
+       } else {
+               return $stmt_statements;
+       }
+}
+
+sub ctx_statement_full {
+       my ($linenr, $remain, $off) = @_;
+       my ($statement, $condition, $level);
+
+       my (@chunks);
+
+       # Grab the first conditional/block pair.
+       ($statement, $condition, $linenr, $remain, $off, $level) =
+                               ctx_statement_block($linenr, $remain, $off);
+       #print "F: c<$condition> s<$statement>\n";
+       push(@chunks, [ $condition, $statement ]);
+       if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
+               return ($level, $linenr, @chunks);
+       }
+
+       # Pull in the following conditional/block pairs and see if they
+       # could continue the statement.
+       for (;;) {
+               ($statement, $condition, $linenr, $remain, $off, $level) =
+                               ctx_statement_block($linenr, $remain, $off);
+               #print "C: c<$condition> s<$statement> remain<$remain>\n";
+               last if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:else|do)\b/s));
+               #print "C: push\n";
+               push(@chunks, [ $condition, $statement ]);
+       }
+
+       return ($level, $linenr, @chunks);
 }
 
 sub ctx_block_get {
@@ -548,13 +650,13 @@ sub cat_vet {
 }
 
 my $av_preprocessor = 0;
-my $av_paren = 0;
+my $av_pending;
 my @av_paren_type;
 
 sub annotate_reset {
        $av_preprocessor = 0;
-       $av_paren = 0;
-       @av_paren_type = ();
+       $av_pending = '_';
+       @av_paren_type = ('E');
 }
 
 sub annotate_values {
@@ -566,12 +668,13 @@ sub annotate_values {
        print "$stream\n" if ($dbg_values > 1);
 
        while (length($cur)) {
-               print " <$type> " if ($dbg_values > 1);
+               print " <" . join('', @av_paren_type) .
+                                       "> <$type> " if ($dbg_values > 1);
                if ($cur =~ /^(\s+)/o) {
                        print "WS($1)\n" if ($dbg_values > 1);
                        if ($1 =~ /\n/ && $av_preprocessor) {
+                               $type = pop(@av_paren_type);
                                $av_preprocessor = 0;
-                               $type = 'N';
                        }
 
                } elsif ($cur =~ /^($Type)/) {
@@ -581,11 +684,33 @@ sub annotate_values {
                } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
                        print "DEFINE($1)\n" if ($dbg_values > 1);
                        $av_preprocessor = 1;
-                       $av_paren_type[$av_paren] = 'N';
+                       $av_pending = 'N';
 
-               } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|elif|endif))/o) {
-                       print "PRE($1)\n" if ($dbg_values > 1);
+               } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
+                       print "PRE_START($1)\n" if ($dbg_values > 1);
                        $av_preprocessor = 1;
+
+                       push(@av_paren_type, $type);
+                       push(@av_paren_type, $type);
+                       $type = 'N';
+
+               } elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
+                       print "PRE_RESTART($1)\n" if ($dbg_values > 1);
+                       $av_preprocessor = 1;
+
+                       push(@av_paren_type, $av_paren_type[$#av_paren_type]);
+
+                       $type = 'N';
+
+               } elsif ($cur =~ /^(#\s*(?:endif))/o) {
+                       print "PRE_END($1)\n" if ($dbg_values > 1);
+
+                       $av_preprocessor = 1;
+
+                       # Assume all arms of the conditional end as this
+                       # one does, and continue as if the #endif was not here.
+                       pop(@av_paren_type);
+                       push(@av_paren_type, $type);
                        $type = 'N';
 
                } elsif ($cur =~ /^(\\\n)/o) {
@@ -594,13 +719,13 @@ sub annotate_values {
                } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
                        print "SIZEOF($1)\n" if ($dbg_values > 1);
                        if (defined $2) {
-                               $av_paren_type[$av_paren] = 'V';
+                               $av_pending = 'V';
                        }
                        $type = 'N';
 
-               } elsif ($cur =~ /^(if|while|typeof|for)\b/o) {
+               } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
                        print "COND($1)\n" if ($dbg_values > 1);
-                       $av_paren_type[$av_paren] = 'N';
+                       $av_pending = 'N';
                        $type = 'N';
 
                } elsif ($cur =~/^(return|case|else)/o) {
@@ -609,14 +734,14 @@ sub annotate_values {
 
                } elsif ($cur =~ /^(\()/o) {
                        print "PAREN('$1')\n" if ($dbg_values > 1);
-                       $av_paren++;
+                       push(@av_paren_type, $av_pending);
+                       $av_pending = '_';
                        $type = 'N';
 
                } elsif ($cur =~ /^(\))/o) {
-                       $av_paren-- if ($av_paren > 0);
-                       if (defined $av_paren_type[$av_paren]) {
-                               $type = $av_paren_type[$av_paren];
-                               undef $av_paren_type[$av_paren];
+                       my $new_type = pop(@av_paren_type);
+                       if ($new_type ne '_') {
+                               $type = $new_type;
                                print "PAREN('$1') -> $type\n"
                                                        if ($dbg_values > 1);
                        } else {
@@ -625,7 +750,7 @@ sub annotate_values {
 
                } elsif ($cur =~ /^($Ident)\(/o) {
                        print "FUNC($1)\n" if ($dbg_values > 1);
-                       $av_paren_type[$av_paren] = 'V';
+                       $av_pending = 'V';
 
                } elsif ($cur =~ /^($Ident|$Constant)/o) {
                        print "IDENT($1)\n" if ($dbg_values > 1);
@@ -635,8 +760,12 @@ sub annotate_values {
                        print "ASSIGN($1)\n" if ($dbg_values > 1);
                        $type = 'N';
 
-               } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) {
+               } elsif ($cur =~/^(;|{|})/) {
                        print "END($1)\n" if ($dbg_values > 1);
+                       $type = 'E';
+
+               } elsif ($cur =~ /^(;|\?|:|\[)/o) {
+                       print "CLOSE($1)\n" if ($dbg_values > 1);
                        $type = 'N';
 
                } elsif ($cur =~ /^($Operators)/o) {
@@ -658,7 +787,7 @@ sub annotate_values {
 }
 
 sub possible {
-       my ($possible) = @_;
+       my ($possible, $line) = @_;
 
        #print "CHECK<$possible>\n";
        if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
@@ -666,7 +795,7 @@ sub possible {
            $possible ne 'struct' && $possible ne 'enum' &&
            $possible ne 'case' && $possible ne 'else' &&
            $possible ne 'typedef') {
-               warn "POSSIBLE: $possible\n" if ($dbg_possible);
+               warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
                push(@typeList, $possible);
                build_types();
        }
@@ -674,16 +803,15 @@ sub possible {
 
 my $prefix = '';
 
-my @report = ();
 sub report {
        my $line = $prefix . $_[0];
 
        $line = (split('\n', $line))[0] . "\n" if ($terse);
 
-       push(@report, $line);
+       push(our @report, $line);
 }
 sub report_dump {
-       @report;
+       our @report;
 }
 sub ERROR {
        report("ERROR: $_[0]\n");
@@ -721,6 +849,7 @@ sub process {
        my $signoff = 0;
        my $is_patch = 0;
 
+       our @report = ();
        our $cnt_lines = 0;
        our $cnt_error = 0;
        our $cnt_warn = 0;
@@ -735,7 +864,10 @@ sub process {
        my $comment_edge = 0;
        my $first_line = 0;
 
-       my $prev_values = 'N';
+       my $prev_values = 'E';
+
+       # suppression flags
+       my $suppress_ifbraces = 0;
 
        # Pre-scan the patch sanitizing the lines.
        # Pre-scan the patch looking for any __setup documentation.
@@ -791,7 +923,9 @@ sub process {
                                $realcnt=1+1;
                        }
                        annotate_reset();
-                       $prev_values = 'N';
+                       $prev_values = 'E';
+
+                       $suppress_ifbraces = $linenr - 1;
                        next;
                }
 
@@ -934,7 +1068,7 @@ sub process {
                }
 
 # check for RCS/CVS revision markers
-               if ($rawline =~ /\$(Revision|Log|Id)(?:\$|)/) {
+               if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
                        WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
                }
 
@@ -945,41 +1079,44 @@ sub process {
 
 # Check for potential 'bare' types
                if ($realcnt) {
+                       my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
+                       $s =~ s/\n./ /g;
+                       $s =~ s/{.*$//;
+
                        # Ignore goto labels.
-                       if ($line =~ /$Ident:\*$/) {
+                       if ($s =~ /$Ident:\*$/) {
 
                        # Ignore functions being called
-                       } elsif ($line =~ /^.\s*$Ident\s*\(/) {
+                       } elsif ($s =~ /^.\s*$Ident\s*\(/) {
 
                        # definitions in global scope can only start with types
-                       } elsif ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
-                               possible($1);
+                       } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
+                               possible($1, $s);
 
                        # declarations always start with types
-                       } elsif ($prev_values eq 'N' && $line =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=)/) {
-                               possible($1);
+                       } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) {
+                               possible($1, $s);
                        }
 
                        # any (foo ... *) is a pointer cast, and foo is a type
-                       while ($line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
-                               possible($1);
+                       while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
+                               possible($1, $s);
                        }
 
                        # Check for any sort of function declaration.
                        # int foo(something bar, other baz);
                        # void (*store_gdt)(x86_descr_ptr *);
-                       if ($prev_values eq 'N' && $line =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
+                       if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
                                my ($name_len) = length($1);
-                               my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, $name_len);
-                               my $ctx = join("\n", @ctx);
 
-                               $ctx =~ s/\n.//;
+                               my $ctx = $s;
                                substr($ctx, 0, $name_len + 1) = '';
                                $ctx =~ s/\)[^\)]*$//;
+
                                for my $arg (split(/\s*,\s*/, $ctx)) {
                                        if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
 
-                                               possible($1);
+                                               possible($1, $s);
                                        }
                                }
                        }
@@ -1046,8 +1183,8 @@ sub process {
                $curr_values = $prev_values . $curr_values;
                if ($dbg_values) {
                        my $outline = $opline; $outline =~ s/\t/ /g;
-                       warn "--> .$outline\n";
-                       warn "--> $curr_values\n";
+                       print "$linenr > .$outline\n";
+                       print "$linenr > $curr_values\n";
                }
                $prev_values = substr($curr_values, -1);
 
@@ -1094,7 +1231,9 @@ sub process {
                        if (($prevline !~ /^}/) &&
                           ($prevline !~ /^\+}/) &&
                           ($prevline !~ /^ }/) &&
-                          ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) {
+                          ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
+                          ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
+                          ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
                                WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
                        }
                }
@@ -1189,7 +1328,7 @@ sub process {
                        my $ctx = substr($line, 0, $-[1]);
 
                        # Ignore those directives where spaces _are_ permitted.
-                       if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case)$/) {
+                       if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case|__asm__)$/) {
 
                        # cpp #define statements have non-optional spaces, ie
                        # if there is a space between the name and the open
@@ -1223,6 +1362,7 @@ sub process {
                                my $a = '';
                                $a = 'V' if ($elements[$n] ne '');
                                $a = 'W' if ($elements[$n] =~ /\s$/);
+                               $a = 'C' if ($elements[$n] =~ /$;$/);
                                $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
                                $a = 'O' if ($elements[$n] eq '');
                                $a = 'E' if ($elements[$n] eq '' && $n == 0);
@@ -1233,6 +1373,7 @@ sub process {
                                if (defined $elements[$n + 2]) {
                                        $c = 'V' if ($elements[$n + 2] ne '');
                                        $c = 'W' if ($elements[$n + 2] =~ /^\s/);
+                                       $c = 'C' if ($elements[$n + 2] =~ /^$;/);
                                        $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
                                        $c = 'O' if ($elements[$n + 2] eq '');
                                        $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
@@ -1272,10 +1413,17 @@ sub process {
                                #       print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
                                #}
 
+                               # Ignore operators passed as parameters.
+                               if ($op_type ne 'V' &&
+                                   $ca =~ /\s$/ && $cc =~ /^\s*,/) {
+
+#                              # Ignore comments
+#                              } elsif ($op =~ /^$;+$/) {
+
                                # ; should have either the end of line or a space or \ after it
-                               if ($op eq ';') {
-                                       if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
-                                           $cc !~ /^;/) {
+                               } elsif ($op eq ';') {
+                                       if ($ctx !~ /.x[WEBC]/ &&
+                                           $cc !~ /^\\/ && $cc !~ /^;/) {
                                                ERROR("need space after that '$op' $at\n" . $hereptr);
                                        }
 
@@ -1290,7 +1438,7 @@ sub process {
 
                                # , must have a space on the right.
                                } elsif ($op eq ',') {
-                                       if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
+                                       if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
                                                ERROR("need space after that '$op' $at\n" . $hereptr);
                                        }
 
@@ -1303,7 +1451,7 @@ sub process {
                                # unary operator, or a cast
                                } elsif ($op eq '!' || $op eq '~' ||
                                         ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
-                                       if ($ctx !~ /[WEB]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
+                                       if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
                                                ERROR("need space before that '$op' $at\n" . $hereptr);
                                        }
                                        if ($ctx =~ /.xW/) {
@@ -1312,10 +1460,10 @@ sub process {
 
                                # unary ++ and unary -- are allowed no space on one side.
                                } elsif ($op eq '++' or $op eq '--') {
-                                       if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
+                                       if ($ctx !~ /[WOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
                                                ERROR("need space one side of that '$op' $at\n" . $hereptr);
                                        }
-                                       if ($ctx =~ /Wx./ && $cc =~ /^;/) {
+                                       if ($ctx =~ /WxB/ || ($ctx =~ /Wx./ && $cc =~ /^;/)) {
                                                ERROR("no space before that '$op' $at\n" . $hereptr);
                                        }
 
@@ -1326,13 +1474,13 @@ sub process {
                                         $op eq '*' or $op eq '/' or
                                         $op eq '%')
                                {
-                                       if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) {
+                                       if ($ctx !~ /VxV|WxW|VxE|WxE|VxO|Cx.|.xC/) {
                                                ERROR("need consistent spacing around '$op' $at\n" .
                                                        $hereptr);
                                        }
 
                                # All the others need spaces both sides.
-                               } elsif ($ctx !~ /[EW]x[WE]/) {
+                               } elsif ($ctx !~ /[EWC]x[CWE]/) {
                                        # Ignore email addresses <foo@bar>
                                        if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
                                            !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
@@ -1388,7 +1536,7 @@ sub process {
                    $line !~ /for\s*\(\s+;/) {
                        ERROR("no space after that open parenthesis '('\n" . $herecurr);
                }
-               if ($line =~ /\s\)/ && $line !~ /^.\s*\)/ &&
+               if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
                    $line !~ /for\s*\(.*;\s+\)/) {
                        ERROR("no space before that close parenthesis ')'\n" . $herecurr);
                }
@@ -1416,16 +1564,34 @@ sub process {
                        # conditional.
                        substr($s, 0, length($c)) = '';
                        $s =~ s/\n.*//g;
-
-                       if (length($c) && $s !~ /^\s*({|;|\/\*.*\*\/)?\s*\\*\s*$/) {
+                       $s =~ s/$;//g;  # Remove any comments
+                       if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/) {
                                ERROR("trailing statements should be on next line\n" . $herecurr);
                        }
                }
 
+# Check for bitwise tests written as boolean
+               if ($line =~ /
+                       (?:
+                               (?:\[|\(|\&\&|\|\|)
+                               \s*0[xX][0-9]+\s*
+                               (?:\&\&|\|\|)
+                       |
+                               (?:\&\&|\|\|)
+                               \s*0[xX][0-9]+\s*
+                               (?:\&\&|\|\||\)|\])
+                       )/x)
+               {
+                       WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
+               }
+
 # if and else should not have general statements after it
-               if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
-                   $1 !~ /^\s*(?:\sif|{|\\|$)/) {
-                       ERROR("trailing statements should be on next line\n" . $herecurr);
+               if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
+                       my $s = $1;
+                       $s =~ s/$;//g;  # Remove any comments
+                       if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
+                               ERROR("trailing statements should be on next line\n" . $herecurr);
+                       }
                }
 
                # Check for }<nl>else {, these must be at the same
@@ -1472,7 +1638,7 @@ sub process {
 
 # multi-statement macros should be enclosed in a do while loop, grab the
 # first statement and ensure its the whole macro if its not enclosed
-# in a known goot container
+# in a known good container
                if ($prevline =~ /\#define.*\\/ &&
                   $prevline !~/(?:do\s+{|\(\{|\{)/ &&
                   $line !~ /(?:do\s+{|\(\{|\{)/ &&
@@ -1518,45 +1684,97 @@ sub process {
                }
 
 # check for redundant bracing round if etc
-               if ($line =~ /\b(if|while|for|else)\b/) {
-                       # Locate the end of the opening statement.
-                       my @control = ctx_statement($linenr, $realcnt, 0);
-                       my $nr = $linenr + (scalar(@control) - 1);
-                       my $cnt = $realcnt - (scalar(@control) - 1);
-
-                       my $off = $realcnt - $cnt;
-                       #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n";
-
-                       # If this is is a braced statement group check it
-                       if ($lines[$nr - 1] =~ /{\s*$/) {
-                               my ($lvl, @block) = ctx_block_level($nr, $cnt);
-
-                               my $stmt = join("\n", @block);
-                               # Drop the diff line leader.
-                               $stmt =~ s/\n./\n/g;
-                               # Drop the code outside the block.
-                               $stmt =~ s/(^[^{]*){\s*//;
-                               my $before = $1;
-                               $stmt =~ s/\s*}([^}]*$)//;
-                               my $after = $1;
-
-                               #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n";
-                               #print "stmt<$stmt>\n\n";
-
-                               # Count the newlines, if there is only one
-                               # then the block should not have {}'s.
-                               my @lines = ($stmt =~ /\n/g);
-                               my @statements = ($stmt =~ /;/g);
-                               #print "lines<" . scalar(@lines) . ">\n";
-                               #print "statements<" . scalar(@statements) . ">\n";
-                               if ($lvl == 0 && scalar(@lines) == 0 &&
-                                   scalar(@statements) < 2 &&
-                                   $stmt !~ /{/ && $stmt !~ /\bif\b/ &&
-                                   $before !~ /}/ && $after !~ /{/) {
-                                       my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n";
-                                       shift(@block);
-                                       WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
+               if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
+                       my ($level, $endln, @chunks) =
+                               ctx_statement_full($linenr, $realcnt, 1);
+                       #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
+                       #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
+                       if ($#chunks > 0 && $level == 0) {
+                               my $allowed = 0;
+                               my $seen = 0;
+                               my $herectx = $here . "\n";;
+                               my $ln = $linenr - 1;
+                               for my $chunk (@chunks) {
+                                       my ($cond, $block) = @{$chunk};
+
+                                       $herectx .= "$rawlines[$ln]\n[...]\n";
+                                       $ln += statement_rawlines($block) - 1;
+
+                                       substr($block, 0, length($cond)) = '';
+
+                                       $seen++ if ($block =~ /^\s*{/);
+
+                                       #print "cond<$cond> block<$block> allowed<$allowed>\n";
+                                       if (statement_lines($cond) > 1) {
+                                               #print "APW: ALLOWED: cond<$cond>\n";
+                                               $allowed = 1;
+                                       }
+                                       if ($block =~/\b(?:if|for|while)\b/) {
+                                               #print "APW: ALLOWED: block<$block>\n";
+                                               $allowed = 1;
+                                       }
+                                       if (statement_block_size($block) > 1) {
+                                               #print "APW: ALLOWED: lines block<$block>\n";
+                                               $allowed = 1;
+                                       }
+                               }
+                               if ($seen && !$allowed) {
+                                       WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
                                }
+                               # Either way we have looked over this whole
+                               # statement and said what needs to be said.
+                               $suppress_ifbraces = $endln;
+                       }
+               }
+               if ($linenr > $suppress_ifbraces &&
+                                       $line =~ /\b(if|while|for|else)\b/) {
+                       my ($level, $endln, @chunks) =
+                               ctx_statement_full($linenr, $realcnt, $-[0]);
+
+                       my $allowed = 0;
+
+                       # Check the pre-context.
+                       if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
+                               #print "APW: ALLOWED: pre<$1>\n";
+                               $allowed = 1;
+                       }
+                       # Check the condition.
+                       my ($cond, $block) = @{$chunks[0]};
+                       if (defined $cond) {
+                               substr($block, 0, length($cond)) = '';
+                       }
+                       if (statement_lines($cond) > 1) {
+                               #print "APW: ALLOWED: cond<$cond>\n";
+                               $allowed = 1;
+                       }
+                       if ($block =~/\b(?:if|for|while)\b/) {
+                               #print "APW: ALLOWED: block<$block>\n";
+                               $allowed = 1;
+                       }
+                       if (statement_block_size($block) > 1) {
+                               #print "APW: ALLOWED: lines block<$block>\n";
+                               $allowed = 1;
+                       }
+                       # Check the post-context.
+                       if (defined $chunks[1]) {
+                               my ($cond, $block) = @{$chunks[1]};
+                               if (defined $cond) {
+                                       substr($block, 0, length($cond)) = '';
+                               }
+                               if ($block =~ /^\s*\{/) {
+                                       #print "APW: ALLOWED: chunk-1 block<$block>\n";
+                                       $allowed = 1;
+                               }
+                       }
+                       if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
+                               my $herectx = $here . "\n";;
+                               my $end = $linenr + statement_rawlines($block) - 1;
+
+                               for (my $ln = $linenr - 1; $ln < $end; $ln++) {
+                                       $herectx .= $rawlines[$ln] . "\n";;
+                               }
+
+                               WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
                        }
                }
 
@@ -1659,6 +1877,17 @@ sub process {
                if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
                        WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
                }
+
+# check for gcc specific __FUNCTION__
+               if ($line =~ /__FUNCTION__/) {
+                       WARN("__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
+               }
+       }
+
+       # If we have no input at all, then there is nothing to report on
+       # so just keep quiet.
+       if ($#rawlines == -1) {
+               exit(0);
        }
 
        # In mailback mode only produce a report in the negative, for
@@ -1681,7 +1910,8 @@ sub process {
        }
 
        print report_dump();
-       if ($summary) {
+       if ($summary && !($clean == 1 && $quiet == 1)) {
+               print "$filename " if ($summary_file);
                print "total: $cnt_error errors, $cnt_warn warnings, " .
                        (($check)? "$cnt_chk checks, " : "") .
                        "$cnt_lines lines checked\n";
@@ -1696,5 +1926,6 @@ sub process {
                print "are false positives report them to the maintainer, see\n";
                print "CHECKPATCH in MAINTAINERS.\n";
        }
+
        return $clean;
 }