Skip to content

Commit

Permalink
checkpatch: add rules to check init attribute and const defects
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Joe Perches authored and Linus Torvalds committed Nov 13, 2013
1 parent 52ea850 commit e970b88
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions scripts/checkpatch.pl
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,11 @@ sub hash_show_words {
__ref|
__rcu
}x;

our $InitAttribute = qr{__(?:mem|cpu|dev|net_|)(?:initdata|initconst|init\b)};
our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};

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

# check for $InitAttributeData (ie: __initdata) with const
if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
my $attr = $1;
$attr =~ /($InitAttributePrefix)(.*)/;
my $attr_prefix = $1;
my $attr_type = $2;
if (ERROR("INIT_ATTRIBUTE",
"Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
$fix) {
$fixed[$linenr - 1] =~
s/$InitAttributeData/${attr_prefix}initconst/;
}
}

# check for $InitAttributeConst (ie: __initconst) without const
if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
my $attr = $1;
if (ERROR("INIT_ATTRIBUTE",
"Use of $attr requires a separate use of const\n" . $herecurr) &&
$fix) {
my $lead = $fixed[$linenr - 1] =~
/(^\+\s*(?:static\s+))/;
$lead = rtrim($1);
$lead = "$lead " if ($lead !~ /^\+$/);
$lead = "${lead}const ";
$fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/;
}
}

# prefer usleep_range over udelay
if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
# ignore udelay's < 10, however
Expand Down

0 comments on commit e970b88

Please sign in to comment.