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
241 __ref|241 __ref|
242 __rcu242 __rcu
243 }x;243 }x;
244 244our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
245our $InitAttribute = qr{__(?:mem|cpu|dev|net_|)(?:initdata|initconst|init\b)};245our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
246our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
247our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
248our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
246 249
247# Notes to $Attribute:250# Notes to $Attribute:
248# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check251# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
@@ -3759,6 +3762,35 @@ sub process { · 3510 unmodified lines
3759 }3762 }
3760 }3763 }
3761 3764
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
3762# prefer usleep_range over udelay3794# prefer usleep_range over udelay
3763 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {3795 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
3764 # ignore udelay's < 10, however3796 # ignore udelay's < 10, however