Merge tag 'riscv-for-linus-5.10-mw1' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / scripts / checkpatch.pl
index 504d2e431c60412dd3720ddfe4df9a0c6ae2e338..4223a9ac7059bf393a897a05948d5fcd49c46879 100755 (executable)
@@ -65,6 +65,7 @@ my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANC
 # git output parsing needs US English output, so first set backtick child process LANGUAGE
 my $git_command ='export LANGUAGE=en_US.UTF-8; git';
 my $tabsize = 8;
+my ${CONFIG_} = "CONFIG_";
 
 sub help {
        my ($exitcode) = @_;
@@ -127,6 +128,8 @@ Options:
   --typedefsfile             Read additional types from this file
   --color[=WHEN]             Use colors 'always', 'never', or only when output
                              is a terminal ('auto'). Default is 'auto'.
+  --kconfig-prefix=WORD      use WORD as a prefix for Kconfig symbols (default
+                             ${CONFIG_})
   -h, --help, --version      display this help and exit
 
 When FILE is - read standard input.
@@ -235,6 +238,7 @@ GetOptions(
        'color=s'       => \$color,
        'no-color'      => \$color,     #keep old behaviors of -nocolor
        'nocolor'       => \$color,     #keep old behaviors of -nocolor
+       'kconfig-prefix=s'      => \${CONFIG_},
        'h|help'        => \$help,
        'version'       => \$help
 ) or help(1);
@@ -970,6 +974,16 @@ sub seed_camelcase_includes {
        }
 }
 
+sub git_is_single_file {
+       my ($filename) = @_;
+
+       return 0 if ((which("git") eq "") || !(-e "$gitroot"));
+
+       my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
+       my $count = $output =~ tr/\n//;
+       return $count eq 1 && $output =~ m{^${filename}$};
+}
+
 sub git_commit_info {
        my ($commit, $id, $desc) = @_;
 
@@ -1043,6 +1057,9 @@ my $vname;
 $allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
 for my $filename (@ARGV) {
        my $FILE;
+       my $is_git_file = git_is_single_file($filename);
+       my $oldfile = $file;
+       $file = 1 if ($is_git_file);
        if ($git) {
                open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
                        die "$P: $filename: git format-patch failed - $!\n";
@@ -1087,6 +1104,7 @@ for my $filename (@ARGV) {
        @modifierListFile = ();
        @typeListFile = ();
        build_types();
+       $file = $oldfile if ($is_git_file);
 }
 
 if (!$quiet) {
@@ -1163,10 +1181,10 @@ sub parse_email {
                }
        }
 
+       $comment = trim($comment);
        $name = trim($name);
        $name =~ s/^\"|\"$//g;
-       $name =~ s/(\s*\([^\)]+\))\s*//;
-       if (defined($1)) {
+       if ($name =~ s/(\s*\([^\)]+\))\s*//) {
                $name_comment = trim($1);
        }
        $address = trim($address);
@@ -1181,10 +1199,12 @@ sub parse_email {
 }
 
 sub format_email {
-       my ($name, $address) = @_;
+       my ($name, $name_comment, $address, $comment) = @_;
 
        my $formatted_email;
 
+       $name_comment = trim($name_comment);
+       $comment = trim($comment);
        $name = trim($name);
        $name =~ s/^\"|\"$//g;
        $address = trim($address);
@@ -1197,9 +1217,9 @@ sub format_email {
        if ("$name" eq "") {
                $formatted_email = "$address";
        } else {
-               $formatted_email = "$name <$address>";
+               $formatted_email = "$name$name_comment <$address>";
        }
-
+       $formatted_email .= "$comment";
        return $formatted_email;
 }
 
@@ -1207,17 +1227,23 @@ sub reformat_email {
        my ($email) = @_;
 
        my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
-       return format_email($email_name, $email_address);
+       return format_email($email_name, $name_comment, $email_address, $comment);
 }
 
 sub same_email_addresses {
-       my ($email1, $email2) = @_;
+       my ($email1, $email2, $match_comment) = @_;
 
        my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
        my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
 
+       if ($match_comment != 1) {
+               return $email1_name eq $email2_name &&
+                      $email1_address eq $email2_address;
+       }
        return $email1_name eq $email2_name &&
-              $email1_address eq $email2_address;
+              $email1_address eq $email2_address &&
+              $name1_comment eq $name2_comment &&
+              $comment1 eq $comment2;
 }
 
 sub which {
@@ -2347,6 +2373,7 @@ sub process {
        my $signoff = 0;
        my $author = '';
        my $authorsignoff = 0;
+       my $author_sob = '';
        my $is_patch = 0;
        my $is_binding_patch = -1;
        my $in_header_lines = $file ? 0 : 1;
@@ -2661,6 +2688,10 @@ sub process {
 # Check the patch for a From:
                if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
                        $author = $1;
+                       my $curline = $linenr;
+                       while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
+                               $author .= $1;
+                       }
                        $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
                        $author =~ s/"//g;
                        $author = reformat_email($author);
@@ -2670,9 +2701,37 @@ sub process {
                if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
                        $signoff++;
                        $in_commit_log = 0;
-                       if ($author ne '') {
-                               if (same_email_addresses($1, $author)) {
+                       if ($author ne ''  && $authorsignoff != 1) {
+                               if (same_email_addresses($1, $author, 1)) {
                                        $authorsignoff = 1;
+                               } else {
+                                       my $ctx = $1;
+                                       my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
+                                       my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
+
+                                       if ($email_address eq $author_address && $email_name eq $author_name) {
+                                               $author_sob = $ctx;
+                                               $authorsignoff = 2;
+                                       } elsif ($email_address eq $author_address) {
+                                               $author_sob = $ctx;
+                                               $authorsignoff = 3;
+                                       } elsif ($email_name eq $author_name) {
+                                               $author_sob = $ctx;
+                                               $authorsignoff = 4;
+
+                                               my $address1 = $email_address;
+                                               my $address2 = $author_address;
+
+                                               if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
+                                                       $address1 = "$1$2";
+                                               }
+                                               if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
+                                                       $address2 = "$1$2";
+                                               }
+                                               if ($address1 eq $address2) {
+                                                       $authorsignoff = 5;
+                                               }
+                                       }
                                }
                        }
                }
@@ -2729,7 +2788,7 @@ sub process {
                        }
 
                        my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
-                       my $suggested_email = format_email(($email_name, $email_address));
+                       my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
                        if ($suggested_email eq "") {
                                ERROR("BAD_SIGN_OFF",
                                      "Unrecognized email address: '$email'\n" . $herecurr);
@@ -2739,9 +2798,9 @@ sub process {
                                $dequoted =~ s/" </ </;
                                # Don't force email to have quotes
                                # Allow just an angle bracketed address
-                               if (!same_email_addresses($email, $suggested_email)) {
+                               if (!same_email_addresses($email, $suggested_email, 0)) {
                                        WARN("BAD_SIGN_OFF",
-                                            "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
+                                            "email address '$email' might be better as '$suggested_email'\n" . $herecurr);
                                }
                        }
 
@@ -2987,6 +3046,42 @@ sub process {
                        }
                }
 
+# check for repeated words separated by a single space
+               if ($rawline =~ /^\+/ || $in_commit_log) {
+                       while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
+
+                               my $first = $1;
+                               my $second = $2;
+
+                               if ($first =~ /(?:struct|union|enum)/) {
+                                       pos($rawline) += length($first) + length($second) + 1;
+                                       next;
+                               }
+
+                               next if ($first ne $second);
+                               next if ($first eq 'long');
+
+                               if (WARN("REPEATED_WORD",
+                                        "Possible repeated word: '$first'\n" . $herecurr) &&
+                                   $fix) {
+                                       $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
+                               }
+                       }
+
+                       # if it's a repeated word on consecutive lines in a comment block
+                       if ($prevline =~ /$;+\s*$/ &&
+                           $prevrawline =~ /($word_pattern)\s*$/) {
+                               my $last_word = $1;
+                               if ($rawline =~ /^\+\s*\*\s*$last_word /) {
+                                       if (WARN("REPEATED_WORD",
+                                                "Possible repeated word: '$last_word'\n" . $hereprev) &&
+                                           $fix) {
+                                               $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
+                                       }
+                               }
+                       }
+               }
+
 # ignore non-hunk lines and lines being removed
                next if (!$hunk_line || $line =~ /^-/);
 
@@ -3213,6 +3308,12 @@ sub process {
                        }
                }
 
+# check for embedded filenames
+               if ($rawline =~ /^\+.*\Q$realfile\E/) {
+                       WARN("EMBEDDED_FILENAME",
+                            "It's generally not useful to have the filename in the file\n" . $herecurr);
+               }
+
 # check we are in a valid source file if not then ignore this hunk
                next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
 
@@ -3310,42 +3411,6 @@ sub process {
                        }
                }
 
-# check for repeated words separated by a single space
-               if ($rawline =~ /^\+/) {
-                       while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
-
-                               my $first = $1;
-                               my $second = $2;
-
-                               if ($first =~ /(?:struct|union|enum)/) {
-                                       pos($rawline) += length($first) + length($second) + 1;
-                                       next;
-                               }
-
-                               next if ($first ne $second);
-                               next if ($first eq 'long');
-
-                               if (WARN("REPEATED_WORD",
-                                        "Possible repeated word: '$first'\n" . $herecurr) &&
-                                   $fix) {
-                                       $fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
-                               }
-                       }
-
-                       # if it's a repeated word on consecutive lines in a comment block
-                       if ($prevline =~ /$;+\s*$/ &&
-                           $prevrawline =~ /($word_pattern)\s*$/) {
-                               my $last_word = $1;
-                               if ($rawline =~ /^\+\s*\*\s*$last_word /) {
-                                       if (WARN("REPEATED_WORD",
-                                                "Possible repeated word: '$last_word'\n" . $hereprev) &&
-                                           $fix) {
-                                               $fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
-                                       }
-                               }
-                       }
-               }
-
 # check for space before tabs.
                if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
                        my $herevet = "$here\n" . cat_vet($rawline) . "\n";
@@ -3436,7 +3501,7 @@ sub process {
                if ($realfile =~ m@^(drivers/net/|net/)@ &&
                    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
                    $rawline =~ /^\+[ \t]*\*/ &&
-                   $realline > 2) {
+                   $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
                        WARN("NETWORKING_BLOCK_COMMENT_STYLE",
                             "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
                }
@@ -3895,6 +3960,17 @@ sub process {
 #ignore lines not being added
                next if ($line =~ /^[^\+]/);
 
+# check for self assignments used to avoid compiler warnings
+# e.g.:        int foo = foo, *bar = NULL;
+#      struct foo bar = *(&(bar));
+               if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
+                       my $var = $1;
+                       if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
+                               WARN("SELF_ASSIGNMENT",
+                                    "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
+                       }
+               }
+
 # check for dereferences that span multiple lines
                if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
                    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
@@ -4270,6 +4346,12 @@ sub process {
                             "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
                }
 
+# trace_printk should not be used in production code.
+               if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
+                       WARN("TRACE_PRINTK",
+                            "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
+               }
+
 # ENOSYS means "bad syscall nr" and nothing else.  This will have a small
 # number of false positives, but assembly files are not checked, so at
 # least the arch entry code will not trigger this warning.
@@ -4936,6 +5018,17 @@ sub process {
                        }
                }
 
+# check if a statement with a comma should be two statements like:
+#      foo = bar(),    /* comma should be semicolon */
+#      bar = baz();
+               if (defined($stat) &&
+                   $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
+                       my $cnt = statement_rawlines($stat);
+                       my $herectx = get_stat_here($linenr, $cnt, $here);
+                       WARN("SUSPECT_COMMA_SEMICOLON",
+                            "Possible comma where semicolon could be used\n" . $herectx);
+               }
+
 # return is not a function
                if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
                        my $spacing = $1;
@@ -5295,9 +5388,9 @@ sub process {
                        $dstat =~ s/\s*$//s;
 
                        # Flatten any parentheses and braces
-                       while ($dstat =~ s/\([^\(\)]*\)/1/ ||
-                              $dstat =~ s/\{[^\{\}]*\}/1/ ||
-                              $dstat =~ s/.\[[^\[\]]*\]/1/)
+                       while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
+                              $dstat =~ s/\{[^\{\}]*\}/1u/ ||
+                              $dstat =~ s/.\[[^\[\]]*\]/1u/)
                        {
                        }
 
@@ -5338,6 +5431,7 @@ sub process {
                            $dstat !~ /^\.$Ident\s*=/ &&                                # .foo =
                            $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&          # stringification #foo
                            $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&       # do {...} while (...); // do {...} while (...)
+                           $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&           # while (...) {...}
                            $dstat !~ /^for\s*$Constant$/ &&                            # for (...)
                            $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&   # for (...) bar()
                            $dstat !~ /^do\s*{/ &&                                      # do {...
@@ -6524,16 +6618,16 @@ sub process {
                }
 
 # check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
-               if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^CONFIG_/) {
+               if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
                        WARN("IS_ENABLED_CONFIG",
-                            "IS_ENABLED($1) is normally used as IS_ENABLED(CONFIG_$1)\n" . $herecurr);
+                            "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
                }
 
 # check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
-               if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
+               if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
                        my $config = $1;
                        if (WARN("PREFER_IS_ENABLED",
-                                "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
+                                "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
                            $fix) {
                                $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
                        }
@@ -6886,9 +6980,33 @@ sub process {
                if ($signoff == 0) {
                        ERROR("MISSING_SIGN_OFF",
                              "Missing Signed-off-by: line(s)\n");
-               } elsif (!$authorsignoff) {
-                       WARN("NO_AUTHOR_SIGN_OFF",
-                            "Missing Signed-off-by: line by nominal patch author '$author'\n");
+               } elsif ($authorsignoff != 1) {
+                       # authorsignoff values:
+                       # 0 -> missing sign off
+                       # 1 -> sign off identical
+                       # 2 -> names and addresses match, comments mismatch
+                       # 3 -> addresses match, names different
+                       # 4 -> names match, addresses different
+                       # 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
+
+                       my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
+
+                       if ($authorsignoff == 0) {
+                               ERROR("NO_AUTHOR_SIGN_OFF",
+                                     "Missing Signed-off-by: line by nominal patch author '$author'\n");
+                       } elsif ($authorsignoff == 2) {
+                               CHK("FROM_SIGN_OFF_MISMATCH",
+                                   "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
+                       } elsif ($authorsignoff == 3) {
+                               WARN("FROM_SIGN_OFF_MISMATCH",
+                                    "From:/Signed-off-by: email name mismatch: $sob_msg\n");
+                       } elsif ($authorsignoff == 4) {
+                               WARN("FROM_SIGN_OFF_MISMATCH",
+                                    "From:/Signed-off-by: email address mismatch: $sob_msg\n");
+                       } elsif ($authorsignoff == 5) {
+                               WARN("FROM_SIGN_OFF_MISMATCH",
+                                    "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
+                       }
                }
        }