History

checkpatch: warn on const char foo[] = "bar"; declarations

These declarations should generally be static const to avoid poor compilation and runtime performance where compilers tend to initialize the const declaration for every call instead of using .rodata for the string. Miscellanea: - Convert spaces to tabs for indentation in 2 adjacent checks

Joe Perches committed 7 years ago

Commit 77b8c0a1 parent 05391771 file · +11 −2Browse files

Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all

Unified · Split

scripts/checkpatch.pl+11−2
@@ -3890,14 +3890,23 @@ sub process { · 3889 unmodified lines
38903890 WARN("STATIC_CONST_CHAR_ARRAY",
38913891 "static const char * array should probably be static const char * const\n" .
38923892 $herecurr);
3893− }
3893+ }
3894+
3895+# check for initialized const char arrays that should be static const
3896+ if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
3897+ if (WARN("STATIC_CONST_CHAR_ARRAY",
3898+ "const array should probably be static const\n" . $herecurr) &&
3899+ $fix) {
3900+ $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
3901+ }
3902+ }
38943903
38953904 # check for static char foo[] = "bar" declarations.
38963905 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
38973906 WARN("STATIC_CONST_CHAR_ARRAY",
38983907 "static char array declaration should probably be static const char\n" .
38993908 $herecurr);
3900− }
3909+ }
39013910
39023911 # check for const <foo> const where <foo> is not a pointer or array type
39033912 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {