checkpatch: add test for char * arrays that could be static const
authorJoe Perches <joe@perches.com>
Thu, 3 Apr 2014 21:49:18 +0000 (14:49 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 3 Apr 2014 23:21:13 +0000 (16:21 -0700)
static const char* arrays create smaller text as each function call does
not have to populate the array.

Emit a warning when char *arrays aren't static const and the array is
not apparently global by being declared in the first column.

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>
scripts/checkpatch.pl

index 646295c..91308be 100755 (executable)
@@ -2675,6 +2675,13 @@ sub process {
                                $herecurr);
                }
 
+# check for non-global char *foo[] = {"bar", ...} declarations.
+               if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
+                       WARN("STATIC_CONST_CHAR_ARRAY",
+                            "char * array declaration might be better as static const\n" .
+                               $herecurr);
+               }
+
 # check for function declarations without arguments like "int foo()"
                if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
                        if (ERROR("FUNCTION_WITHOUT_ARGS",