]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - scripts/checkpatch.pl
checkpatch: add 'Prefer ARRAY_SIZE" test
[karo-tx-linux.git] / scripts / checkpatch.pl
index ea628ebfa20e7a3b8960a369c1c88c12aa5d353d..6c11cb61e9f48dd2bc801b26271c245c86868225 100755 (executable)
@@ -3227,6 +3227,19 @@ sub process {
                                $herecurr);
                }
 
+# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
+               if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
+                       my $array = $1;
+                       if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
+                               my $array_div = $1;
+                               if (WARN("ARRAY_SIZE",
+                                        "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
+                                   $fix) {
+                                       $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
+                               }
+                       }
+               }
+
 # check for function declarations without arguments like "int foo()"
                if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
                        if (ERROR("FUNCTION_WITHOUT_ARGS",