History

checkpatch: prefer static const declarations

There are about 100,000 uses of 'static const <type>' but about 400 uses of 'static <type> const' in the kernel where type is not a pointer. The kernel almost always uses "static const" over "const static" as there is a compiler warning for that declaration style. But there is no compiler warning for "static <type> const". So add a checkpatch warning for the atypical declaration uses of. const static <type> <foo> and static <type> const <foo> For example: $ ./scripts/checkpatch.pl -f --emacs --quiet --nosummary -types=static_const arch/arm/crypto/aes-ce-glue.c arch/arm/crypto/aes-ce-glue.c:75: WARNING: Move const after static - use 'static const u8' #75: FILE: arch/arm/crypto/aes-ce-glue.c:75: + static u8 const rcon[] = {

Joe Perches committed 5 years ago

Commit 73169761 parent 4104a201 file · +12 −0Browse files

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

Unified · Split

scripts/checkpatch.pl+12−0
@@ -4232,6 +4232,18 @@ sub process { · 4231 unmodified lines
42324232 }
42334233 }
42344234
4235+# check for const static or static <non ptr type> const declarations
4236+# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
4237+ if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
4238+ $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
4239+ if (WARN("STATIC_CONST",
4240+ "Move const after static - use 'static const $1'\n" . $herecurr) &&
4241+ $fix) {
4242+ $fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
4243+ $fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
4244+ }
4245+ }
4246+
42354247 # check for non-global char *foo[] = {"bar", ...} declarations.
42364248 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
42374249 WARN("STATIC_CONST_CHAR_ARRAY",