All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] checkpatch: Add 'Prefer ARRAY_SIZE" test
@ 2015-04-07 15:45 Joe Perches
  0 siblings, 0 replies; only message in thread
From: Joe Perches @ 2015-04-07 15:45 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Andy Whitcroft, LKML

Add a test for sizeof(foo)/sizeof(foo[0]) that could
be ARRAY_SIZE(foo).

Signed-off-by: Joe Perches <joe@perches.com>
---
 scripts/checkpatch.pl | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 9a8b2bd..d2e9752 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -3226,6 +3226,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",



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2015-04-07 15:45 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-07 15:45 [PATCH] checkpatch: Add 'Prefer ARRAY_SIZE" test Joe Perches

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.