checkpatch: try to avoid mask and shift errors
[deliverable/linux.git] / scripts / checkpatch.pl
index 374abf44363615004537cb895d3b55bc410ae99b..24d6702a95c28d73bf6b20bc83d25cd35017ee25 100755 (executable)
@@ -7,10 +7,11 @@
 
 use strict;
 use POSIX;
+use File::Basename;
+use Cwd 'abs_path';
 
 my $P = $0;
-$P =~ s@(.*)/@@g;
-my $D = $1;
+my $D = dirname(abs_path($P));
 
 my $V = '0.32';
 
@@ -438,26 +439,29 @@ our $allowed_asm_includes = qr{(?x:
 
 # Load common spelling mistakes and build regular expression list.
 my $misspellings;
-my @spelling_list;
 my %spelling_fix;
-open(my $spelling, '<', $spelling_file)
-    or die "$P: Can't open $spelling_file for reading: $!\n";
-while (<$spelling>) {
-       my $line = $_;
 
-       $line =~ s/\s*\n?$//g;
-       $line =~ s/^\s*//g;
+if (open(my $spelling, '<', $spelling_file)) {
+       my @spelling_list;
+       while (<$spelling>) {
+               my $line = $_;
 
-       next if ($line =~ m/^\s*#/);
-       next if ($line =~ m/^\s*$/);
+               $line =~ s/\s*\n?$//g;
+               $line =~ s/^\s*//g;
 
-       my ($suspect, $fix) = split(/\|\|/, $line);
+               next if ($line =~ m/^\s*#/);
+               next if ($line =~ m/^\s*$/);
+
+               my ($suspect, $fix) = split(/\|\|/, $line);
 
-       push(@spelling_list, $suspect);
-       $spelling_fix{$suspect} = $fix;
+               push(@spelling_list, $suspect);
+               $spelling_fix{$suspect} = $fix;
+       }
+       close($spelling);
+       $misspellings = join("|", @spelling_list);
+} else {
+       warn "No typos will be found - file '$spelling_file': $!\n";
 }
-close($spelling);
-$misspellings = join("|", @spelling_list);
 
 sub build_types {
        my $mods = "(?x:  \n" . join("|\n  ", @modifierList) . "\n)";
@@ -2246,7 +2250,7 @@ sub process {
                }
 
 # Check for various typo / spelling mistakes
-               if ($in_commit_log || $line =~ /^\+/) {
+               if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) {
                        while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
                                my $typo = $1;
                                my $typo_fix = $spelling_fix{lc($typo)};
@@ -2515,7 +2519,8 @@ sub process {
                        }
                }
 
-               if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|{)/) {
+               if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;\({\[\<\>])/ &&
+                   (!defined($1) || $1 !~ /sizeof\s*/)) {
                        if (CHK("SPACING",
                                "No space is necessary after a cast\n" . $herecurr) &&
                            $fix) {
@@ -3563,14 +3568,33 @@ sub process {
                                                }
                                        }
 
-                               # , must have a space on the right.
+                               # , must not have a space before and must have a space on the right.
                                } elsif ($op eq ',') {
+                                       my $rtrim_before = 0;
+                                       my $space_after = 0;
+                                       if ($ctx =~ /Wx./) {
+                                               if (ERROR("SPACING",
+                                                         "space prohibited before that '$op' $at\n" . $hereptr)) {
+                                                       $line_fixed = 1;
+                                                       $rtrim_before = 1;
+                                               }
+                                       }
                                        if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
                                                if (ERROR("SPACING",
                                                          "space required after that '$op' $at\n" . $hereptr)) {
-                                                       $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
                                                        $line_fixed = 1;
                                                        $last_after = $n;
+                                                       $space_after = 1;
+                                               }
+                                       }
+                                       if ($rtrim_before || $space_after) {
+                                               if ($rtrim_before) {
+                                                       $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
+                                               } else {
+                                                       $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
+                                               }
+                                               if ($space_after) {
+                                                       $good .= " ";
                                                }
                                        }
 
@@ -4426,7 +4450,7 @@ sub process {
                        my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
                        if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
                                WARN('NEEDLESS_IF',
-                                    "$1(NULL) is safe this check is probably not required\n" . $hereprev);
+                                    "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
                        }
                }
 
@@ -4458,6 +4482,14 @@ sub process {
                        }
                }
 
+# check for mask then right shift without a parentheses
+               if ($^V && $^V ge 5.10.0 &&
+                   $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
+                   $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
+                       WARN("MASK_THEN_SHIFT",
+                            "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
+               }
+
 # check for bad placement of section $InitAttribute (e.g.: __initdata)
                if ($line =~ /(\b$InitAttribute\b)/) {
                        my $attr = $1;
@@ -4652,6 +4684,15 @@ sub process {
                        }
                }
 
+# Check for __attribute__ weak, or __weak declarations (may have link issues)
+               if ($^V && $^V ge 5.10.0 &&
+                   $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
+                   ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
+                    $line =~ /\b__weak\b/)) {
+                       ERROR("WEAK_DECLARATION",
+                             "Using weak declarations can have unintended link defects\n" . $herecurr);
+               }
+
 # check for sizeof(&)
                if ($line =~ /\bsizeof\s*\(\s*\&/) {
                        WARN("SIZEOF_ADDRESS",
This page took 0.030768 seconds and 5 git commands to generate.