checkpatch: attempt to find missing switch/case break;
[platform/adaptation/renesas_rcar/renesas_kernel.git] / scripts / checkpatch.pl
index 9c98100..260b324 100755 (executable)
@@ -2049,16 +2049,12 @@ sub process {
                }
 
 # Check for user-visible strings broken across lines, which breaks the ability
-# to grep for the string.  Limited to strings used as parameters (those
-# following an open parenthesis), which almost completely eliminates false
-# positives, as well as warning only once per parameter rather than once per
-# line of the string.  Make an exception when the previous string ends in a
-# newline (multiple lines in one string constant) or \n\t (common in inline
-# assembly to indent the instruction on the following line).
+# to grep for the string.  Make exceptions when the previous string ends in a
+# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
+# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
                if ($line =~ /^\+\s*"/ &&
                    $prevline =~ /"\s*$/ &&
-                   $prevline =~ /\(/ &&
-                   $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
+                   $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
                        WARN("SPLIT_STRING",
                             "quoted string split across lines\n" . $hereprev);
                }
@@ -2634,10 +2630,13 @@ sub process {
                                $herecurr);
                }
 
-# check for declarations of struct pci_device_id
-               if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
-                       WARN("DEFINE_PCI_DEVICE_TABLE",
-                            "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
+# check for uses of DEFINE_PCI_DEVICE_TABLE
+               if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
+                       if (WARN("DEFINE_PCI_DEVICE_TABLE",
+                                "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
+                           $fix) {
+                               $fixed[$linenr - 1] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
+                       }
                }
 
 # check for new typedefs, only function parameters and sparse annotations
@@ -3122,7 +3121,7 @@ sub process {
                }
 
 # check for whitespace before a non-naked semicolon
-               if ($line =~ /^\+.*\S\s+;/) {
+               if ($line =~ /^\+.*\S\s+;\s*$/) {
                        if (WARN("SPACING",
                                 "space prohibited before semicolon\n" . $herecurr) &&
                            $fix) {
@@ -4114,6 +4113,12 @@ sub process {
                             "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
                }
 
+# check for GFP_NOWAIT use
+               if ($line =~ /\b__GFP_NOFAIL\b/) {
+                       WARN("__GFP_NOFAIL",
+                            "Use of __GFP_NOFAIL is deprecated, no new users should be added\n" . $herecurr);
+               }
+
 # check for multiple semicolons
                if ($line =~ /;\s*;\s*$/) {
                        if (WARN("ONE_SEMICOLON",
@@ -4123,6 +4128,31 @@ sub process {
                        }
                }
 
+# check for case / default statements not preceeded by break/fallthrough/switch
+               if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
+                       my $has_break = 0;
+                       my $has_statement = 0;
+                       my $count = 0;
+                       my $prevline = $linenr;
+                       while ($prevline > 1 && $count < 3 && !$has_break) {
+                               $prevline--;
+                               my $rline = $rawlines[$prevline - 1];
+                               my $fline = $lines[$prevline - 1];
+                               last if ($fline =~ /^\@\@/);
+                               next if ($fline =~ /^\-/);
+                               next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
+                               $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
+                               next if ($fline =~ /^.[\s$;]*$/);
+                               $has_statement = 1;
+                               $count++;
+                               $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
+                       }
+                       if (!$has_break && $has_statement) {
+                               WARN("MISSING_BREAK",
+                                    "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
+                       }
+               }
+
 # check for switch/default statements without a break;
                if ($^V && $^V ge 5.10.0 &&
                    defined $stat &&