diff options
author | Joe Perches <joe@perches.com> | 2015-04-16 21:44:36 +0200 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2015-04-17 15:03:58 +0200 |
commit | b598b67060f14eb8c34d0cef1db4727dab56ef91 (patch) | |
tree | eae35ba4c7ad7e538a0c273a504169f540b50b15 /scripts/checkpatch.pl | |
parent | checkpatch: add uart_ops to normally const structs (diff) | |
download | linux-b598b67060f14eb8c34d0cef1db4727dab56ef91.tar.xz linux-b598b67060f14eb8c34d0cef1db4727dab56ef91.zip |
checkpatch: add 'Prefer ARRAY_SIZE" test
Add a test for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo).
Signed-off-by: Joe Perches <joe@perches.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-x | scripts/checkpatch.pl | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index ea628ebfa20e..6c11cb61e9f4 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -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", |