util/lint/checkpatch: Update 'Check for compiler attributes'

This reduce difference with linux v5.18.

Signed-off-by: Elyes Haouas <ehaouas@noos.fr>
Change-Id: I817630321587dec515cd94aa7b73a17819526190
Reviewed-on: https://review.coreboot.org/c/coreboot/+/64604
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Martin L Roth <gaumless@tutanota.com>
This commit is contained in:
Elyes Haouas
2022-05-23 18:38:19 +02:00
committed by Martin L Roth
parent a7b0d38964
commit 069dfe33a3

View File

@@ -6054,38 +6054,68 @@ sub process {
} }
} }
# Check for __attribute__ packed, prefer __packed # Check for compiler attributes
if ($realfile !~ m@\binclude/uapi/@ && if ($realfile !~ m@\binclude/uapi/@ &&
$line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
WARN("PREFER_PACKED", my $attr = $1;
"__packed is preferred over __attribute__((packed))\n" . $herecurr); $attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
}
# Check for __attribute__ aligned, prefer __aligned my %attr_list = (
if ($realfile !~ m@\binclude/uapi/@ && "alias" => "__alias",
$line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { "aligned" => "__aligned",
WARN("PREFER_ALIGNED", "always_inline" => "__always_inline",
"__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); "assume_aligned" => "__assume_aligned",
} "cold" => "__cold",
"const" => "__attribute_const__",
# Check for __attribute__ format(printf, prefer __printf "copy" => "__copy",
if ($realfile !~ m@\binclude/uapi/@ && "designated_init" => "__designated_init",
$line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { "externally_visible" => "__visible",
if (WARN("PREFER_PRINTF", "format" => "printf|scanf",
"__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && "gnu_inline" => "__gnu_inline",
$fix) { "malloc" => "__malloc",
$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; "mode" => "__mode",
"no_caller_saved_registers" => "__no_caller_saved_registers",
"noclone" => "__noclone",
"noinline" => "noinline",
"nonstring" => "__nonstring",
"noreturn" => "__noreturn",
"packed" => "__packed",
"pure" => "__pure",
"section" => "__section",
"used" => "__used",
"weak" => "__weak"
);
while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
my $orig_attr = $1;
my $params = '';
$params = $2 if defined($2);
my $curr_attr = $orig_attr;
$curr_attr =~ s/^[\s_]+|[\s_]+$//g;
if (exists($attr_list{$curr_attr})) {
my $new = $attr_list{$curr_attr};
if ($curr_attr eq "format" && $params) {
$params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
$new = "__$1\($2";
} else {
$new = "$new$params";
}
if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
"Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
$fix) {
my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
$fixed[$fixlinenr] =~ s/$remove//;
$fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
$fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
$fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
}
}
} }
}
# Check for __attribute__ format(scanf, prefer __scanf # Check for __attribute__ unused, prefer __always_unused or __maybe_unused
if ($realfile !~ m@\binclude/uapi/@ && if ($attr =~ /^_*unused/) {
$line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
if (WARN("PREFER_SCANF", "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
"__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
$fix) {
$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
} }
} }