aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl345
1 files changed, 259 insertions, 86 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 2ee9eb750560..47016c304c84 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -31,12 +31,16 @@ my $show_types = 0;
31my $fix = 0; 31my $fix = 0;
32my $root; 32my $root;
33my %debug; 33my %debug;
34my %ignore_type = ();
35my %camelcase = (); 34my %camelcase = ();
35my %use_type = ();
36my @use = ();
37my %ignore_type = ();
36my @ignore = (); 38my @ignore = ();
37my $help = 0; 39my $help = 0;
38my $configuration_file = ".checkpatch.conf"; 40my $configuration_file = ".checkpatch.conf";
39my $max_line_length = 80; 41my $max_line_length = 80;
42my $ignore_perl_version = 0;
43my $minimum_perl_version = 5.10.0;
40 44
41sub help { 45sub help {
42 my ($exitcode) = @_; 46 my ($exitcode) = @_;
@@ -54,6 +58,7 @@ Options:
54 --terse one line per report 58 --terse one line per report
55 -f, --file treat FILE as regular source file 59 -f, --file treat FILE as regular source file
56 --subjective, --strict enable more subjective tests 60 --subjective, --strict enable more subjective tests
61 --types TYPE(,TYPE2...) show only these comma separated message types
57 --ignore TYPE(,TYPE2...) ignore various comma separated message types 62 --ignore TYPE(,TYPE2...) ignore various comma separated message types
58 --max-line-length=n set the maximum line length, if exceeded, warn 63 --max-line-length=n set the maximum line length, if exceeded, warn
59 --show-types show the message "types" in the output 64 --show-types show the message "types" in the output
@@ -71,6 +76,8 @@ Options:
71 "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 76 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
72 with potential errors corrected to the preferred 77 with potential errors corrected to the preferred
73 checkpatch style 78 checkpatch style
79 --ignore-perl-version override checking of perl version. expect
80 runtime errors.
74 -h, --help, --version display this help and exit 81 -h, --help, --version display this help and exit
75 82
76When FILE is - read standard input. 83When FILE is - read standard input.
@@ -116,6 +123,7 @@ GetOptions(
116 'subjective!' => \$check, 123 'subjective!' => \$check,
117 'strict!' => \$check, 124 'strict!' => \$check,
118 'ignore=s' => \@ignore, 125 'ignore=s' => \@ignore,
126 'types=s' => \@use,
119 'show-types!' => \$show_types, 127 'show-types!' => \$show_types,
120 'max-line-length=i' => \$max_line_length, 128 'max-line-length=i' => \$max_line_length,
121 'root=s' => \$root, 129 'root=s' => \$root,
@@ -123,6 +131,7 @@ GetOptions(
123 'mailback!' => \$mailback, 131 'mailback!' => \$mailback,
124 'summary-file!' => \$summary_file, 132 'summary-file!' => \$summary_file,
125 'fix!' => \$fix, 133 'fix!' => \$fix,
134 'ignore-perl-version!' => \$ignore_perl_version,
126 'debug=s' => \%debug, 135 'debug=s' => \%debug,
127 'test-only=s' => \$tst_only, 136 'test-only=s' => \$tst_only,
128 'h|help' => \$help, 137 'h|help' => \$help,
@@ -133,24 +142,50 @@ help(0) if ($help);
133 142
134my $exit = 0; 143my $exit = 0;
135 144
145if ($^V && $^V lt $minimum_perl_version) {
146 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
147 if (!$ignore_perl_version) {
148 exit(1);
149 }
150}
151
136if ($#ARGV < 0) { 152if ($#ARGV < 0) {
137 print "$P: no input files\n"; 153 print "$P: no input files\n";
138 exit(1); 154 exit(1);
139} 155}
140 156
141@ignore = split(/,/, join(',',@ignore)); 157sub hash_save_array_words {
142foreach my $word (@ignore) { 158 my ($hashRef, $arrayRef) = @_;
143 $word =~ s/\s*\n?$//g; 159
144 $word =~ s/^\s*//g; 160 my @array = split(/,/, join(',', @$arrayRef));
145 $word =~ s/\s+/ /g; 161 foreach my $word (@array) {
146 $word =~ tr/[a-z]/[A-Z]/; 162 $word =~ s/\s*\n?$//g;
163 $word =~ s/^\s*//g;
164 $word =~ s/\s+/ /g;
165 $word =~ tr/[a-z]/[A-Z]/;
166
167 next if ($word =~ m/^\s*#/);
168 next if ($word =~ m/^\s*$/);
147 169
148 next if ($word =~ m/^\s*#/); 170 $hashRef->{$word}++;
149 next if ($word =~ m/^\s*$/); 171 }
172}
150 173
151 $ignore_type{$word}++; 174sub hash_show_words {
175 my ($hashRef, $prefix) = @_;
176
177 if ($quiet == 0 && keys %$hashRef) {
178 print "NOTE: $prefix message types:";
179 foreach my $word (sort keys %$hashRef) {
180 print " $word";
181 }
182 print "\n\n";
183 }
152} 184}
153 185
186hash_save_array_words(\%ignore_type, \@ignore);
187hash_save_array_words(\%use_type, \@use);
188
154my $dbg_values = 0; 189my $dbg_values = 0;
155my $dbg_possible = 0; 190my $dbg_possible = 0;
156my $dbg_type = 0; 191my $dbg_type = 0;
@@ -207,6 +242,8 @@ our $Sparse = qr{
207 __rcu 242 __rcu
208 }x; 243 }x;
209 244
245our $InitAttribute = qr{__(?:mem|cpu|dev|net_|)(?:initdata|initconst|init\b)};
246
210# Notes to $Attribute: 247# Notes to $Attribute:
211# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 248# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
212our $Attribute = qr{ 249our $Attribute = qr{
@@ -227,7 +264,7 @@ our $Attribute = qr{
227 __deprecated| 264 __deprecated|
228 __read_mostly| 265 __read_mostly|
229 __kprobes| 266 __kprobes|
230 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)| 267 $InitAttribute|
231 ____cacheline_aligned| 268 ____cacheline_aligned|
232 ____cacheline_aligned_in_smp| 269 ____cacheline_aligned_in_smp|
233 ____cacheline_internodealigned_in_smp| 270 ____cacheline_internodealigned_in_smp|
@@ -257,6 +294,7 @@ our $Operators = qr{
257 }x; 294 }x;
258 295
259our $NonptrType; 296our $NonptrType;
297our $NonptrTypeWithAttr;
260our $Type; 298our $Type;
261our $Declare; 299our $Declare;
262 300
@@ -319,6 +357,12 @@ our @typeList = (
319 qr{${Ident}_handler}, 357 qr{${Ident}_handler},
320 qr{${Ident}_handler_fn}, 358 qr{${Ident}_handler_fn},
321); 359);
360our @typeListWithAttr = (
361 @typeList,
362 qr{struct\s+$InitAttribute\s+$Ident},
363 qr{union\s+$InitAttribute\s+$Ident},
364);
365
322our @modifierList = ( 366our @modifierList = (
323 qr{fastcall}, 367 qr{fastcall},
324); 368);
@@ -332,6 +376,7 @@ our $allowed_asm_includes = qr{(?x:
332sub build_types { 376sub build_types {
333 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 377 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
334 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 378 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
379 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
335 $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 380 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
336 $NonptrType = qr{ 381 $NonptrType = qr{
337 (?:$Modifier\s+|const\s+)* 382 (?:$Modifier\s+|const\s+)*
@@ -342,6 +387,15 @@ sub build_types {
342 ) 387 )
343 (?:\s+$Modifier|\s+const)* 388 (?:\s+$Modifier|\s+const)*
344 }x; 389 }x;
390 $NonptrTypeWithAttr = qr{
391 (?:$Modifier\s+|const\s+)*
392 (?:
393 (?:typeof|__typeof__)\s*\([^\)]*\)|
394 (?:$typeTypedefs\b)|
395 (?:${allWithAttr}\b)
396 )
397 (?:\s+$Modifier|\s+const)*
398 }x;
345 $Type = qr{ 399 $Type = qr{
346 $NonptrType 400 $NonptrType
347 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)? 401 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
@@ -1355,7 +1409,9 @@ sub possible {
1355my $prefix = ''; 1409my $prefix = '';
1356 1410
1357sub show_type { 1411sub show_type {
1358 return !defined $ignore_type{$_[0]}; 1412 return defined $use_type{$_[0]} if (scalar keys %use_type > 0);
1413
1414 return !defined $ignore_type{$_[0]};
1359} 1415}
1360 1416
1361sub report { 1417sub report {
@@ -1435,7 +1491,23 @@ sub check_absolute_file {
1435sub trim { 1491sub trim {
1436 my ($string) = @_; 1492 my ($string) = @_;
1437 1493
1438 $string =~ s/(^\s+|\s+$)//g; 1494 $string =~ s/^\s+|\s+$//g;
1495
1496 return $string;
1497}
1498
1499sub ltrim {
1500 my ($string) = @_;
1501
1502 $string =~ s/^\s+//;
1503
1504 return $string;
1505}
1506
1507sub rtrim {
1508 my ($string) = @_;
1509
1510 $string =~ s/\s+$//;
1439 1511
1440 return $string; 1512 return $string;
1441} 1513}
@@ -1532,6 +1604,7 @@ sub process {
1532 my %suppress_export; 1604 my %suppress_export;
1533 my $suppress_statement = 0; 1605 my $suppress_statement = 0;
1534 1606
1607 my %signatures = ();
1535 1608
1536 # Pre-scan the patch sanitizing the lines. 1609 # Pre-scan the patch sanitizing the lines.
1537 # Pre-scan the patch looking for any __setup documentation. 1610 # Pre-scan the patch looking for any __setup documentation.
@@ -1624,6 +1697,8 @@ sub process {
1624 $linenr = 0; 1697 $linenr = 0;
1625 foreach my $line (@lines) { 1698 foreach my $line (@lines) {
1626 $linenr++; 1699 $linenr++;
1700 my $sline = $line; #copy of $line
1701 $sline =~ s/$;/ /g; #with comments as spaces
1627 1702
1628 my $rawline = $rawlines[$linenr - 1]; 1703 my $rawline = $rawlines[$linenr - 1];
1629 1704
@@ -1781,6 +1856,17 @@ sub process {
1781 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 1856 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
1782 } 1857 }
1783 } 1858 }
1859
1860# Check for duplicate signatures
1861 my $sig_nospace = $line;
1862 $sig_nospace =~ s/\s//g;
1863 $sig_nospace = lc($sig_nospace);
1864 if (defined $signatures{$sig_nospace}) {
1865 WARN("BAD_SIGN_OFF",
1866 "Duplicate signature\n" . $herecurr);
1867 } else {
1868 $signatures{$sig_nospace} = 1;
1869 }
1784 } 1870 }
1785 1871
1786# Check for wrappage within a valid hunk of the file 1872# Check for wrappage within a valid hunk of the file
@@ -1845,15 +1931,17 @@ sub process {
1845#trailing whitespace 1931#trailing whitespace
1846 if ($line =~ /^\+.*\015/) { 1932 if ($line =~ /^\+.*\015/) {
1847 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1933 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1848 ERROR("DOS_LINE_ENDINGS", 1934 if (ERROR("DOS_LINE_ENDINGS",
1849 "DOS line endings\n" . $herevet); 1935 "DOS line endings\n" . $herevet) &&
1850 1936 $fix) {
1937 $fixed[$linenr - 1] =~ s/[\s\015]+$//;
1938 }
1851 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1939 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1852 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1940 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1853 if (ERROR("TRAILING_WHITESPACE", 1941 if (ERROR("TRAILING_WHITESPACE",
1854 "trailing whitespace\n" . $herevet) && 1942 "trailing whitespace\n" . $herevet) &&
1855 $fix) { 1943 $fix) {
1856 $fixed[$linenr - 1] =~ s/^(\+.*?)\s+$/$1/; 1944 $fixed[$linenr - 1] =~ s/\s+$//;
1857 } 1945 }
1858 1946
1859 $rpt_cleaners = 1; 1947 $rpt_cleaners = 1;
@@ -2060,6 +2148,7 @@ sub process {
2060 if ($realfile =~ m@^(drivers/net/|net/)@ && 2148 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2061 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /* 2149 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2062 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 2150 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2151 $rawline =~ /^\+/ && #line is new
2063 $rawline !~ /^\+[ \t]*\*/) { #no leading * 2152 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2064 WARN("NETWORKING_BLOCK_COMMENT_STYLE", 2153 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2065 "networking block comments start with * on subsequent lines\n" . $hereprev); 2154 "networking block comments start with * on subsequent lines\n" . $hereprev);
@@ -2126,7 +2215,7 @@ sub process {
2126 $realline_next); 2215 $realline_next);
2127#print "LINE<$line>\n"; 2216#print "LINE<$line>\n";
2128 if ($linenr >= $suppress_statement && 2217 if ($linenr >= $suppress_statement &&
2129 $realcnt && $line =~ /.\s*\S/) { 2218 $realcnt && $sline =~ /.\s*\S/) {
2130 ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 2219 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2131 ctx_statement_block($linenr, $realcnt, 0); 2220 ctx_statement_block($linenr, $realcnt, 0);
2132 $stat =~ s/\n./\n /g; 2221 $stat =~ s/\n./\n /g;
@@ -2486,16 +2575,22 @@ sub process {
2486 } 2575 }
2487 2576
2488# check for global initialisers. 2577# check for global initialisers.
2489 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { 2578 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
2490 ERROR("GLOBAL_INITIALISERS", 2579 if (ERROR("GLOBAL_INITIALISERS",
2491 "do not initialise globals to 0 or NULL\n" . 2580 "do not initialise globals to 0 or NULL\n" .
2492 $herecurr); 2581 $herecurr) &&
2582 $fix) {
2583 $fixed[$linenr - 1] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
2584 }
2493 } 2585 }
2494# check for static initialisers. 2586# check for static initialisers.
2495 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 2587 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
2496 ERROR("INITIALISED_STATIC", 2588 if (ERROR("INITIALISED_STATIC",
2497 "do not initialise statics to 0 or NULL\n" . 2589 "do not initialise statics to 0 or NULL\n" .
2498 $herecurr); 2590 $herecurr) &&
2591 $fix) {
2592 $fixed[$linenr - 1] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
2593 }
2499 } 2594 }
2500 2595
2501# check for static const char * arrays. 2596# check for static const char * arrays.
@@ -2638,8 +2733,12 @@ sub process {
2638 } 2733 }
2639 2734
2640 if ($line =~ /\bpr_warning\s*\(/) { 2735 if ($line =~ /\bpr_warning\s*\(/) {
2641 WARN("PREFER_PR_LEVEL", 2736 if (WARN("PREFER_PR_LEVEL",
2642 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr); 2737 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
2738 $fix) {
2739 $fixed[$linenr - 1] =~
2740 s/\bpr_warning\b/pr_warn/;
2741 }
2643 } 2742 }
2644 2743
2645 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 2744 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
@@ -2759,6 +2858,7 @@ sub process {
2759 $off = 0; 2858 $off = 0;
2760 2859
2761 my $blank = copy_spacing($opline); 2860 my $blank = copy_spacing($opline);
2861 my $last_after = -1;
2762 2862
2763 for (my $n = 0; $n < $#elements; $n += 2) { 2863 for (my $n = 0; $n < $#elements; $n += 2) {
2764 2864
@@ -2824,7 +2924,7 @@ sub process {
2824 $cc !~ /^\\/ && $cc !~ /^;/) { 2924 $cc !~ /^\\/ && $cc !~ /^;/) {
2825 if (ERROR("SPACING", 2925 if (ERROR("SPACING",
2826 "space required after that '$op' $at\n" . $hereptr)) { 2926 "space required after that '$op' $at\n" . $hereptr)) {
2827 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 2927 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
2828 $line_fixed = 1; 2928 $line_fixed = 1;
2829 } 2929 }
2830 } 2930 }
@@ -2839,11 +2939,11 @@ sub process {
2839 if ($ctx =~ /Wx.|.xW/) { 2939 if ($ctx =~ /Wx.|.xW/) {
2840 if (ERROR("SPACING", 2940 if (ERROR("SPACING",
2841 "spaces prohibited around that '$op' $at\n" . $hereptr)) { 2941 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
2842 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 2942 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2843 $line_fixed = 1;
2844 if (defined $fix_elements[$n + 2]) { 2943 if (defined $fix_elements[$n + 2]) {
2845 $fix_elements[$n + 2] =~ s/^\s+//; 2944 $fix_elements[$n + 2] =~ s/^\s+//;
2846 } 2945 }
2946 $line_fixed = 1;
2847 } 2947 }
2848 } 2948 }
2849 2949
@@ -2852,8 +2952,9 @@ sub process {
2852 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 2952 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
2853 if (ERROR("SPACING", 2953 if (ERROR("SPACING",
2854 "space required after that '$op' $at\n" . $hereptr)) { 2954 "space required after that '$op' $at\n" . $hereptr)) {
2855 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " "; 2955 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
2856 $line_fixed = 1; 2956 $line_fixed = 1;
2957 $last_after = $n;
2857 } 2958 }
2858 } 2959 }
2859 2960
@@ -2870,8 +2971,10 @@ sub process {
2870 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 2971 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
2871 if (ERROR("SPACING", 2972 if (ERROR("SPACING",
2872 "space required before that '$op' $at\n" . $hereptr)) { 2973 "space required before that '$op' $at\n" . $hereptr)) {
2873 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 2974 if ($n != $last_after + 2) {
2874 $line_fixed = 1; 2975 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
2976 $line_fixed = 1;
2977 }
2875 } 2978 }
2876 } 2979 }
2877 if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 2980 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
@@ -2880,12 +2983,11 @@ sub process {
2880 } elsif ($ctx =~ /.xW/) { 2983 } elsif ($ctx =~ /.xW/) {
2881 if (ERROR("SPACING", 2984 if (ERROR("SPACING",
2882 "space prohibited after that '$op' $at\n" . $hereptr)) { 2985 "space prohibited after that '$op' $at\n" . $hereptr)) {
2883 $fixed_line =~ s/\s+$//; 2986 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
2884 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2885 $line_fixed = 1;
2886 if (defined $fix_elements[$n + 2]) { 2987 if (defined $fix_elements[$n + 2]) {
2887 $fix_elements[$n + 2] =~ s/^\s+//; 2988 $fix_elements[$n + 2] =~ s/^\s+//;
2888 } 2989 }
2990 $line_fixed = 1;
2889 } 2991 }
2890 } 2992 }
2891 2993
@@ -2894,8 +2996,7 @@ sub process {
2894 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 2996 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2895 if (ERROR("SPACING", 2997 if (ERROR("SPACING",
2896 "space required one side of that '$op' $at\n" . $hereptr)) { 2998 "space required one side of that '$op' $at\n" . $hereptr)) {
2897 $fixed_line =~ s/\s+$//; 2999 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
2898 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2899 $line_fixed = 1; 3000 $line_fixed = 1;
2900 } 3001 }
2901 } 3002 }
@@ -2903,20 +3004,18 @@ sub process {
2903 ($ctx =~ /Wx./ && $cc =~ /^;/)) { 3004 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2904 if (ERROR("SPACING", 3005 if (ERROR("SPACING",
2905 "space prohibited before that '$op' $at\n" . $hereptr)) { 3006 "space prohibited before that '$op' $at\n" . $hereptr)) {
2906 $fixed_line =~ s/\s+$//; 3007 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2907 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2908 $line_fixed = 1; 3008 $line_fixed = 1;
2909 } 3009 }
2910 } 3010 }
2911 if ($ctx =~ /ExW/) { 3011 if ($ctx =~ /ExW/) {
2912 if (ERROR("SPACING", 3012 if (ERROR("SPACING",
2913 "space prohibited after that '$op' $at\n" . $hereptr)) { 3013 "space prohibited after that '$op' $at\n" . $hereptr)) {
2914 $fixed_line =~ s/\s+$//; 3014 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
2915 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2916 $line_fixed = 1;
2917 if (defined $fix_elements[$n + 2]) { 3015 if (defined $fix_elements[$n + 2]) {
2918 $fix_elements[$n + 2] =~ s/^\s+//; 3016 $fix_elements[$n + 2] =~ s/^\s+//;
2919 } 3017 }
3018 $line_fixed = 1;
2920 } 3019 }
2921 } 3020 }
2922 3021
@@ -2930,8 +3029,10 @@ sub process {
2930 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 3029 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
2931 if (ERROR("SPACING", 3030 if (ERROR("SPACING",
2932 "need consistent spacing around '$op' $at\n" . $hereptr)) { 3031 "need consistent spacing around '$op' $at\n" . $hereptr)) {
2933 $fixed_line =~ s/\s+$//; 3032 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2934 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3033 if (defined $fix_elements[$n + 2]) {
3034 $fix_elements[$n + 2] =~ s/^\s+//;
3035 }
2935 $line_fixed = 1; 3036 $line_fixed = 1;
2936 } 3037 }
2937 } 3038 }
@@ -2942,7 +3043,7 @@ sub process {
2942 if ($ctx =~ /Wx./) { 3043 if ($ctx =~ /Wx./) {
2943 if (ERROR("SPACING", 3044 if (ERROR("SPACING",
2944 "space prohibited before that '$op' $at\n" . $hereptr)) { 3045 "space prohibited before that '$op' $at\n" . $hereptr)) {
2945 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 3046 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2946 $line_fixed = 1; 3047 $line_fixed = 1;
2947 } 3048 }
2948 } 3049 }
@@ -2969,8 +3070,10 @@ sub process {
2969 if ($ok == 0) { 3070 if ($ok == 0) {
2970 if (ERROR("SPACING", 3071 if (ERROR("SPACING",
2971 "spaces required around that '$op' $at\n" . $hereptr)) { 3072 "spaces required around that '$op' $at\n" . $hereptr)) {
2972 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3073 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2973 $good = $fix_elements[$n] . " " . trim($fix_elements[$n + 1]) . " "; 3074 if (defined $fix_elements[$n + 2]) {
3075 $fix_elements[$n + 2] =~ s/^\s+//;
3076 }
2974 $line_fixed = 1; 3077 $line_fixed = 1;
2975 } 3078 }
2976 } 3079 }
@@ -3031,8 +3134,7 @@ sub process {
3031 if (ERROR("SPACING", 3134 if (ERROR("SPACING",
3032 "space required before the open brace '{'\n" . $herecurr) && 3135 "space required before the open brace '{'\n" . $herecurr) &&
3033 $fix) { 3136 $fix) {
3034 $fixed[$linenr - 1] =~ 3137 $fixed[$linenr - 1] =~ s/^(\+.*(?:do|\))){/$1 {/;
3035 s/^(\+.*(?:do|\))){/$1 {/;
3036 } 3138 }
3037 } 3139 }
3038 3140
@@ -3047,8 +3149,12 @@ sub process {
3047# closing brace should have a space following it when it has anything 3149# closing brace should have a space following it when it has anything
3048# on the line 3150# on the line
3049 if ($line =~ /}(?!(?:,|;|\)))\S/) { 3151 if ($line =~ /}(?!(?:,|;|\)))\S/) {
3050 ERROR("SPACING", 3152 if (ERROR("SPACING",
3051 "space required after that close brace '}'\n" . $herecurr); 3153 "space required after that close brace '}'\n" . $herecurr) &&
3154 $fix) {
3155 $fixed[$linenr - 1] =~
3156 s/}((?!(?:,|;|\)))\S)/} $1/;
3157 }
3052 } 3158 }
3053 3159
3054# check spacing on square brackets 3160# check spacing on square brackets
@@ -3271,8 +3377,13 @@ sub process {
3271 3377
3272#gcc binary extension 3378#gcc binary extension
3273 if ($var =~ /^$Binary$/) { 3379 if ($var =~ /^$Binary$/) {
3274 WARN("GCC_BINARY_CONSTANT", 3380 if (WARN("GCC_BINARY_CONSTANT",
3275 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr); 3381 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
3382 $fix) {
3383 my $hexval = sprintf("0x%x", oct($var));
3384 $fixed[$linenr - 1] =~
3385 s/\b$var\b/$hexval/;
3386 }
3276 } 3387 }
3277 3388
3278#CamelCase 3389#CamelCase
@@ -3282,19 +3393,26 @@ sub process {
3282 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 3393 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
3283#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) 3394#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
3284 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) { 3395 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) {
3285 seed_camelcase_includes() if ($check); 3396 while ($var =~ m{($Ident)}g) {
3286 if (!defined $camelcase{$var}) { 3397 my $word = $1;
3287 $camelcase{$var} = 1; 3398 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3288 CHK("CAMELCASE", 3399 seed_camelcase_includes() if ($check);
3289 "Avoid CamelCase: <$var>\n" . $herecurr); 3400 if (!defined $camelcase{$word}) {
3401 $camelcase{$word} = 1;
3402 CHK("CAMELCASE",
3403 "Avoid CamelCase: <$word>\n" . $herecurr);
3404 }
3290 } 3405 }
3291 } 3406 }
3292 } 3407 }
3293 3408
3294#no spaces allowed after \ in define 3409#no spaces allowed after \ in define
3295 if ($line=~/\#\s*define.*\\\s$/) { 3410 if ($line =~ /\#\s*define.*\\\s+$/) {
3296 WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 3411 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3297 "Whitepspace after \\ makes next lines useless\n" . $herecurr); 3412 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
3413 $fix) {
3414 $fixed[$linenr - 1] =~ s/\s+$//;
3415 }
3298 } 3416 }
3299 3417
3300#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 3418#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
@@ -3374,7 +3492,8 @@ sub process {
3374 $dstat !~ /^for\s*$Constant$/ && # for (...) 3492 $dstat !~ /^for\s*$Constant$/ && # for (...)
3375 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 3493 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3376 $dstat !~ /^do\s*{/ && # do {... 3494 $dstat !~ /^do\s*{/ && # do {...
3377 $dstat !~ /^\({/) # ({... 3495 $dstat !~ /^\({/ && # ({...
3496 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
3378 { 3497 {
3379 $ctx =~ s/\n*$//; 3498 $ctx =~ s/\n*$//;
3380 my $herectx = $here . "\n"; 3499 my $herectx = $here . "\n";
@@ -3606,6 +3725,32 @@ sub process {
3606 } 3725 }
3607 } 3726 }
3608 3727
3728sub string_find_replace {
3729 my ($string, $find, $replace) = @_;
3730
3731 $string =~ s/$find/$replace/g;
3732
3733 return $string;
3734}
3735
3736# check for bad placement of section $InitAttribute (e.g.: __initdata)
3737 if ($line =~ /(\b$InitAttribute\b)/) {
3738 my $attr = $1;
3739 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
3740 my $ptr = $1;
3741 my $var = $2;
3742 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
3743 ERROR("MISPLACED_INIT",
3744 "$attr should be placed after $var\n" . $herecurr)) ||
3745 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
3746 WARN("MISPLACED_INIT",
3747 "$attr should be placed after $var\n" . $herecurr))) &&
3748 $fix) {
3749 $fixed[$linenr - 1] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e;
3750 }
3751 }
3752 }
3753
3609# prefer usleep_range over udelay 3754# prefer usleep_range over udelay
3610 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 3755 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
3611 # ignore udelay's < 10, however 3756 # ignore udelay's < 10, however
@@ -3691,8 +3836,12 @@ sub process {
3691 3836
3692# Check for __inline__ and __inline, prefer inline 3837# Check for __inline__ and __inline, prefer inline
3693 if ($line =~ /\b(__inline__|__inline)\b/) { 3838 if ($line =~ /\b(__inline__|__inline)\b/) {
3694 WARN("INLINE", 3839 if (WARN("INLINE",
3695 "plain inline is preferred over $1\n" . $herecurr); 3840 "plain inline is preferred over $1\n" . $herecurr) &&
3841 $fix) {
3842 $fixed[$linenr - 1] =~ s/\b(__inline__|__inline)\b/inline/;
3843
3844 }
3696 } 3845 }
3697 3846
3698# Check for __attribute__ packed, prefer __packed 3847# Check for __attribute__ packed, prefer __packed
@@ -3709,14 +3858,21 @@ sub process {
3709 3858
3710# Check for __attribute__ format(printf, prefer __printf 3859# Check for __attribute__ format(printf, prefer __printf
3711 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 3860 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3712 WARN("PREFER_PRINTF", 3861 if (WARN("PREFER_PRINTF",
3713 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr); 3862 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3863 $fix) {
3864 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
3865
3866 }
3714 } 3867 }
3715 3868
3716# Check for __attribute__ format(scanf, prefer __scanf 3869# Check for __attribute__ format(scanf, prefer __scanf
3717 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 3870 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3718 WARN("PREFER_SCANF", 3871 if (WARN("PREFER_SCANF",
3719 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr); 3872 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3873 $fix) {
3874 $fixed[$linenr - 1] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
3875 }
3720 } 3876 }
3721 3877
3722# check for sizeof(&) 3878# check for sizeof(&)
@@ -3727,8 +3883,11 @@ sub process {
3727 3883
3728# check for sizeof without parenthesis 3884# check for sizeof without parenthesis
3729 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 3885 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3730 WARN("SIZEOF_PARENTHESIS", 3886 if (WARN("SIZEOF_PARENTHESIS",
3731 "sizeof $1 should be sizeof($1)\n" . $herecurr); 3887 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
3888 $fix) {
3889 $fixed[$linenr - 1] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
3890 }
3732 } 3891 }
3733 3892
3734# check for line continuations in quoted strings with odd counts of " 3893# check for line continuations in quoted strings with odd counts of "
@@ -3747,8 +3906,11 @@ sub process {
3747 if ($line =~ /\bseq_printf\s*\(/) { 3906 if ($line =~ /\bseq_printf\s*\(/) {
3748 my $fmt = get_quoted_string($line, $rawline); 3907 my $fmt = get_quoted_string($line, $rawline);
3749 if ($fmt !~ /[^\\]\%/) { 3908 if ($fmt !~ /[^\\]\%/) {
3750 WARN("PREFER_SEQ_PUTS", 3909 if (WARN("PREFER_SEQ_PUTS",
3751 "Prefer seq_puts to seq_printf\n" . $herecurr); 3910 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3911 $fix) {
3912 $fixed[$linenr - 1] =~ s/\bseq_printf\b/seq_puts/;
3913 }
3752 } 3914 }
3753 } 3915 }
3754 3916
@@ -3810,6 +3972,16 @@ sub process {
3810 } 3972 }
3811 } 3973 }
3812 3974
3975# check for new externs in .h files.
3976 if ($realfile =~ /\.h$/ &&
3977 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
3978 if (WARN("AVOID_EXTERNS",
3979 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
3980 $fix) {
3981 $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
3982 }
3983 }
3984
3813# check for new externs in .c files. 3985# check for new externs in .c files.
3814 if ($realfile =~ /\.c$/ && defined $stat && 3986 if ($realfile =~ /\.c$/ && defined $stat &&
3815 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 3987 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
@@ -3879,8 +4051,11 @@ sub process {
3879 4051
3880# check for multiple semicolons 4052# check for multiple semicolons
3881 if ($line =~ /;\s*;\s*$/) { 4053 if ($line =~ /;\s*;\s*$/) {
3882 WARN("ONE_SEMICOLON", 4054 if (WARN("ONE_SEMICOLON",
3883 "Statements terminations use 1 semicolon\n" . $herecurr); 4055 "Statements terminations use 1 semicolon\n" . $herecurr) &&
4056 $fix) {
4057 $fixed[$linenr - 1] =~ s/(\s*;\s*){2,}$/;/g;
4058 }
3884 } 4059 }
3885 4060
3886# check for switch/default statements without a break; 4061# check for switch/default statements without a break;
@@ -3898,9 +4073,12 @@ sub process {
3898 } 4073 }
3899 4074
3900# check for gcc specific __FUNCTION__ 4075# check for gcc specific __FUNCTION__
3901 if ($line =~ /__FUNCTION__/) { 4076 if ($line =~ /\b__FUNCTION__\b/) {
3902 WARN("USE_FUNC", 4077 if (WARN("USE_FUNC",
3903 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 4078 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
4079 $fix) {
4080 $fixed[$linenr - 1] =~ s/\b__FUNCTION__\b/__func__/g;
4081 }
3904 } 4082 }
3905 4083
3906# check for use of yield() 4084# check for use of yield()
@@ -4105,13 +4283,8 @@ sub process {
4105 } 4283 }
4106 } 4284 }
4107 4285
4108 if ($quiet == 0 && keys %ignore_type) { 4286 hash_show_words(\%use_type, "Used");
4109 print "NOTE: Ignored message types:"; 4287 hash_show_words(\%ignore_type, "Ignored");
4110 foreach my $ignore (sort keys %ignore_type) {
4111 print " $ignore";
4112 }
4113 print "\n\n";
4114 }
4115 4288
4116 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") { 4289 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4117 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes"; 4290 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";