arm64: mte: add in-kernel MTE helpers
[linux-2.6-microblaze.git] / scripts / checkpatch.pl
index fdfd5ec..0008530 100755 (executable)
@@ -506,6 +506,64 @@ our $signature_tags = qr{(?xi:
        Cc:
 )};
 
+sub edit_distance_min {
+       my (@arr) = @_;
+       my $len = scalar @arr;
+       if ((scalar @arr) < 1) {
+               # if underflow, return
+               return;
+       }
+       my $min = $arr[0];
+       for my $i (0 .. ($len-1)) {
+               if ($arr[$i] < $min) {
+                       $min = $arr[$i];
+               }
+       }
+       return $min;
+}
+
+sub get_edit_distance {
+       my ($str1, $str2) = @_;
+       $str1 = lc($str1);
+       $str2 = lc($str2);
+       $str1 =~ s/-//g;
+       $str2 =~ s/-//g;
+       my $len1 = length($str1);
+       my $len2 = length($str2);
+       # two dimensional array storing minimum edit distance
+       my @distance;
+       for my $i (0 .. $len1) {
+               for my $j (0 .. $len2) {
+                       if ($i == 0) {
+                               $distance[$i][$j] = $j;
+                       } elsif ($j == 0) {
+                               $distance[$i][$j] = $i;
+                       } elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
+                               $distance[$i][$j] = $distance[$i - 1][$j - 1];
+                       } else {
+                               my $dist1 = $distance[$i][$j - 1]; #insert distance
+                               my $dist2 = $distance[$i - 1][$j]; # remove
+                               my $dist3 = $distance[$i - 1][$j - 1]; #replace
+                               $distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
+                       }
+               }
+       }
+       return $distance[$len1][$len2];
+}
+
+sub find_standard_signature {
+       my ($sign_off) = @_;
+       my @standard_signature_tags = (
+               'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
+               'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
+       );
+       foreach my $signature (@standard_signature_tags) {
+               return $signature if (get_edit_distance($sign_off, $signature) <= 2);
+       }
+
+       return "";
+}
+
 our @typeListMisordered = (
        qr{char\s+(?:un)?signed},
        qr{int\s+(?:(?:un)?signed\s+)?short\s},
@@ -2773,8 +2831,17 @@ sub process {
                        my $ucfirst_sign_off = ucfirst(lc($sign_off));
 
                        if ($sign_off !~ /$signature_tags/) {
-                               WARN("BAD_SIGN_OFF",
-                                    "Non-standard signature: $sign_off\n" . $herecurr);
+                               my $suggested_signature = find_standard_signature($sign_off);
+                               if ($suggested_signature eq "") {
+                                       WARN("BAD_SIGN_OFF",
+                                            "Non-standard signature: $sign_off\n" . $herecurr);
+                               } else {
+                                       if (WARN("BAD_SIGN_OFF",
+                                                "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
+                                           $fix) {
+                                               $fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
+                                       }
+                               }
                        }
                        if (defined $space_before && $space_before ne "") {
                                if (WARN("BAD_SIGN_OFF",
@@ -2966,6 +3033,15 @@ sub process {
                        $commit_log_possible_stack_dump = 0;
                }
 
+# Check for lines starting with a #
+               if ($in_commit_log && $line =~ /^#/) {
+                       if (WARN("COMMIT_COMMENT_SYMBOL",
+                                "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
+                           $fix) {
+                               $fixed[$fixlinenr] =~ s/^/ /;
+                       }
+               }
+
 # Check for git id commit length and improperly formed commit descriptions
                if ($in_commit_log && !$commit_log_possible_stack_dump &&
                    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
@@ -3106,15 +3182,18 @@ sub process {
 # Check for various typo / spelling mistakes
                if (defined($misspellings) &&
                    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
-                       while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
+                       while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
                                my $typo = $1;
+                               my $blank = copy_spacing($rawline);
+                               my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
+                               my $hereptr = "$hereline$ptr\n";
                                my $typo_fix = $spelling_fix{lc($typo)};
                                $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
                                $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
                                my $msg_level = \&WARN;
                                $msg_level = \&CHK if ($file);
                                if (&{$msg_level}("TYPO_SPELLING",
-                                                 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
+                                                 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
                                    $fix) {
                                        $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
                                }
@@ -3533,14 +3612,28 @@ sub process {
 
 # check for assignments on the start of a line
                if ($sline =~ /^\+\s+($Assignment)[^=]/) {
-                       CHK("ASSIGNMENT_CONTINUATIONS",
-                           "Assignment operator '$1' should be on the previous line\n" . $hereprev);
+                       my $operator = $1;
+                       if (CHK("ASSIGNMENT_CONTINUATIONS",
+                               "Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
+                           $fix && $prevrawline =~ /^\+/) {
+                               # add assignment operator to the previous line, remove from current line
+                               $fixed[$fixlinenr - 1] .= " $operator";
+                               $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
+                       }
                }
 
 # check for && or || at the start of a line
                if ($rawline =~ /^\+\s*(&&|\|\|)/) {
-                       CHK("LOGICAL_CONTINUATIONS",
-                           "Logical continuations should be on the previous line\n" . $hereprev);
+                       my $operator = $1;
+                       if (CHK("LOGICAL_CONTINUATIONS",
+                               "Logical continuations should be on the previous line\n" . $hereprev) &&
+                           $fix && $prevrawline =~ /^\+/) {
+                               # insert logical operator at last non-comment, non-whitepsace char on previous line
+                               $prevline =~ /[\s$;]*$/;
+                               my $line_end = substr($prevrawline, $-[0]);
+                               $fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
+                               $fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
+                       }
                }
 
 # check indentation starts on a tab stop
@@ -4450,16 +4543,23 @@ sub process {
                             "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
                }
 
-               if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
-                       my $orig = $1;
+# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
+               if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
+                       my $printk = $1;
+                       my $modifier = $2;
+                       my $orig = $3;
+                       $modifier = "" if (!defined($modifier));
                        my $level = lc($orig);
                        $level = "warn" if ($level eq "warning");
                        my $level2 = $level;
                        $level2 = "dbg" if ($level eq "debug");
+                       $level .= $modifier;
+                       $level2 .= $modifier;
                        WARN("PREFER_PR_LEVEL",
-                            "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
+                            "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to $printk(KERN_$orig ...\n" . $herecurr);
                }
 
+# prefer dev_<level> to dev_printk(KERN_<LEVEL>
                if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
                        my $orig = $1;
                        my $level = lc($orig);
@@ -6021,6 +6121,28 @@ sub process {
                             "Avoid logging continuation uses where feasible\n" . $herecurr);
                }
 
+# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
+               if (defined $stat &&
+                   $line =~ /\b$logFunctions\s*\(/ &&
+                   index($stat, '"') >= 0) {
+                       my $lc = $stat =~ tr@\n@@;
+                       $lc = $lc + $linenr;
+                       my $stat_real = get_stat_real($linenr, $lc);
+                       pos($stat_real) = index($stat_real, '"');
+                       while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
+                               my $pspec = $1;
+                               my $h = $2;
+                               my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
+                               if (WARN("UNNECESSARY_MODIFIER",
+                                        "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
+                                   $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
+                                       my $nspec = $pspec;
+                                       $nspec =~ s/h//g;
+                                       $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
+                               }
+                       }
+               }
+
 # check for mask then right shift without a parentheses
                if ($perl_version_ok &&
                    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&