Skip to content

Commit e970b88

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: add rules to check init attribute and const defects
People get this regularly wrong and it breaks the LTO builds, as it causes a section attribute conflict. Add --fix capability too. Based on a patch from Andi Kleen. Signed-off-by: Joe Perches <joe@perches.com> Cc: Andi Kleen <ak@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 52ea850 commit e970b88

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

‎scripts/checkpatch.pl‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,11 @@ sub hash_show_words {
241241
__ref|
242242
__rcu
243243
}x;
244-
245-
our $InitAttribute = qr{__(?:mem|cpu|dev|net_|)(?:initdata|initconst|init\b)};
244+
our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
245+
our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
246+
our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
247+
our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
248+
our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
246249

247250
# Notes to $Attribute:
248251
# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
@@ -3759,6 +3762,35 @@ sub process {
37593762
}
37603763
}
37613764

3765+
# check for $InitAttributeData (ie: __initdata) with const
3766+
if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
3767+
my $attr = $1;
3768+
$attr =~ /($InitAttributePrefix)(.*)/;
3769+
my $attr_prefix = $1;
3770+
my $attr_type = $2;
3771+
if (ERROR("INIT_ATTRIBUTE",
3772+
"Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
3773+
$fix) {
3774+
$fixed[$linenr - 1] =~
3775+
s/$InitAttributeData/${attr_prefix}initconst/;
3776+
}
3777+
}
3778+
3779+
# check for $InitAttributeConst (ie: __initconst) without const
3780+
if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
3781+
my $attr = $1;
3782+
if (ERROR("INIT_ATTRIBUTE",
3783+
"Use of $attr requires a separate use of const\n" . $herecurr) &&
3784+
$fix) {
3785+
my $lead = $fixed[$linenr - 1] =~
3786+
/(^\+\s*(?:static\s+))/;
3787+
$lead = rtrim($1);
3788+
$lead = "$lead " if ($lead !~ /^\+$/);
3789+
$lead = "${lead}const ";
3790+
$fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/;
3791+
}
3792+
}
3793+
37623794
# prefer usleep_range over udelay
37633795
if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
37643796
# ignore udelay's < 10, however

0 commit comments

Comments
 (0)