History

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.

Joe Perches committed 13 years ago

Commit e970b881 parent 52ea8501 file · +34 −2Browse files

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

Unified · Split

scripts/checkpatch.pl+34−2
@@ -241,8 +241,11 @@ our $Sparse = qr{ · 240 unmodified lines
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 { · 3510 unmodified lines
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