aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl306
1 files changed, 275 insertions, 31 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 3257d3d96767..4c0383da1c9a 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -2,7 +2,7 @@
2# (c) 2001, Dave Jones. (the file handling bit) 2# (c) 2001, Dave Jones. (the file handling bit)
3# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit) 3# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite) 4# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5# (c) 2008,2009, Andy Whitcroft <apw@canonical.com> 5# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6# Licensed under the terms of the GNU GPL License version 2 6# Licensed under the terms of the GNU GPL License version 2
7 7
8use strict; 8use strict;
@@ -10,7 +10,7 @@ use strict;
10my $P = $0; 10my $P = $0;
11$P =~ s@.*/@@g; 11$P =~ s@.*/@@g;
12 12
13my $V = '0.30'; 13my $V = '0.31';
14 14
15use Getopt::Long qw(:config no_auto_abbrev); 15use Getopt::Long qw(:config no_auto_abbrev);
16 16
@@ -103,6 +103,8 @@ for my $key (keys %debug) {
103 die "$@" if ($@); 103 die "$@" if ($@);
104} 104}
105 105
106my $rpt_cleaners = 0;
107
106if ($terse) { 108if ($terse) {
107 $emacs = 1; 109 $emacs = 1;
108 $quiet++; 110 $quiet++;
@@ -145,11 +147,28 @@ our $Sparse = qr{
145 __kprobes| 147 __kprobes|
146 __ref 148 __ref
147 }x; 149 }x;
150
151# Notes to $Attribute:
152# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
148our $Attribute = qr{ 153our $Attribute = qr{
149 const| 154 const|
155 __percpu|
156 __nocast|
157 __safe|
158 __bitwise__|
159 __packed__|
160 __packed2__|
161 __naked|
162 __maybe_unused|
163 __always_unused|
164 __noreturn|
165 __used|
166 __cold|
167 __noclone|
168 __deprecated|
150 __read_mostly| 169 __read_mostly|
151 __kprobes| 170 __kprobes|
152 __(?:mem|cpu|dev|)(?:initdata|init)| 171 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
153 ____cacheline_aligned| 172 ____cacheline_aligned|
154 ____cacheline_aligned_in_smp| 173 ____cacheline_aligned_in_smp|
155 ____cacheline_internodealigned_in_smp| 174 ____cacheline_internodealigned_in_smp|
@@ -189,6 +208,14 @@ our $typeTypedefs = qr{(?x:
189 atomic_t 208 atomic_t
190)}; 209)};
191 210
211our $logFunctions = qr{(?x:
212 printk|
213 pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
214 (dev|netdev|netif)_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
215 WARN|
216 panic
217)};
218
192our @typeList = ( 219our @typeList = (
193 qr{void}, 220 qr{void},
194 qr{(?:unsigned\s+)?char}, 221 qr{(?:unsigned\s+)?char},
@@ -213,6 +240,12 @@ our @modifierList = (
213 qr{fastcall}, 240 qr{fastcall},
214); 241);
215 242
243our $allowed_asm_includes = qr{(?x:
244 irq|
245 memory
246)};
247# memory.h: ARM has a custom one
248
216sub build_types { 249sub build_types {
217 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 250 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
218 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 251 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
@@ -541,6 +574,9 @@ sub ctx_statement_block {
541 $type = ($level != 0)? '{' : ''; 574 $type = ($level != 0)? '{' : '';
542 575
543 if ($level == 0) { 576 if ($level == 0) {
577 if (substr($blk, $off + 1, 1) eq ';') {
578 $off++;
579 }
544 last; 580 last;
545 } 581 }
546 } 582 }
@@ -655,15 +691,15 @@ sub ctx_block_get {
655 $blk .= $rawlines[$line]; 691 $blk .= $rawlines[$line];
656 692
657 # Handle nested #if/#else. 693 # Handle nested #if/#else.
658 if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 694 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
659 push(@stack, $level); 695 push(@stack, $level);
660 } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 696 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
661 $level = $stack[$#stack - 1]; 697 $level = $stack[$#stack - 1];
662 } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) { 698 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
663 $level = pop(@stack); 699 $level = pop(@stack);
664 } 700 }
665 701
666 foreach my $c (split(//, $rawlines[$line])) { 702 foreach my $c (split(//, $lines[$line])) {
667 ##print "C<$c>L<$level><$open$close>O<$off>\n"; 703 ##print "C<$c>L<$level><$open$close>O<$off>\n";
668 if ($off > 0) { 704 if ($off > 0) {
669 $off--; 705 $off--;
@@ -823,7 +859,12 @@ sub annotate_values {
823 $av_preprocessor = 0; 859 $av_preprocessor = 0;
824 } 860 }
825 861
826 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) { 862 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
863 print "CAST($1)\n" if ($dbg_values > 1);
864 push(@av_paren_type, $type);
865 $type = 'C';
866
867 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
827 print "DECLARE($1)\n" if ($dbg_values > 1); 868 print "DECLARE($1)\n" if ($dbg_values > 1);
828 $type = 'T'; 869 $type = 'T';
829 870
@@ -1288,7 +1329,11 @@ sub process {
1288 $here = "#$realline: " if ($file); 1329 $here = "#$realline: " if ($file);
1289 1330
1290 # extract the filename as it passes 1331 # extract the filename as it passes
1291 if ($line=~/^\+\+\+\s+(\S+)/) { 1332 if ($line =~ /^diff --git.*?(\S+)$/) {
1333 $realfile = $1;
1334 $realfile =~ s@^([^/]*)/@@;
1335
1336 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1292 $realfile = $1; 1337 $realfile = $1;
1293 $realfile =~ s@^([^/]*)/@@; 1338 $realfile =~ s@^([^/]*)/@@;
1294 1339
@@ -1312,6 +1357,14 @@ sub process {
1312 1357
1313 $cnt_lines++ if ($realcnt != 0); 1358 $cnt_lines++ if ($realcnt != 0);
1314 1359
1360# Check for incorrect file permissions
1361 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1362 my $permhere = $here . "FILE: $realfile\n";
1363 if ($realfile =~ /(Makefile|Kconfig|\.c|\.h|\.S|\.tmpl)$/) {
1364 ERROR("do not set execute permissions for source files\n" . $permhere);
1365 }
1366 }
1367
1315#check the patch for a signoff: 1368#check the patch for a signoff:
1316 if ($line =~ /^\s*signed-off-by:/i) { 1369 if ($line =~ /^\s*signed-off-by:/i) {
1317 # This is a signoff, if ugly, so do not double report. 1370 # This is a signoff, if ugly, so do not double report.
@@ -1369,6 +1422,38 @@ sub process {
1369 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 1422 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1370 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1423 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1371 ERROR("trailing whitespace\n" . $herevet); 1424 ERROR("trailing whitespace\n" . $herevet);
1425 $rpt_cleaners = 1;
1426 }
1427
1428# check for Kconfig help text having a real description
1429# Only applies when adding the entry originally, after that we do not have
1430# sufficient context to determine whether it is indeed long enough.
1431 if ($realfile =~ /Kconfig/ &&
1432 $line =~ /\+\s*(?:---)?help(?:---)?$/) {
1433 my $length = 0;
1434 my $cnt = $realcnt;
1435 my $ln = $linenr + 1;
1436 my $f;
1437 my $is_end = 0;
1438 while ($cnt > 0 && defined $lines[$ln - 1]) {
1439 $f = $lines[$ln - 1];
1440 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1441 $is_end = $lines[$ln - 1] =~ /^\+/;
1442 $ln++;
1443
1444 next if ($f =~ /^-/);
1445 $f =~ s/^.//;
1446 $f =~ s/#.*//;
1447 $f =~ s/^\s+//;
1448 next if ($f =~ /^$/);
1449 if ($f =~ /^\s*config\s/) {
1450 $is_end = 1;
1451 last;
1452 }
1453 $length++;
1454 }
1455 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
1456 #print "is_end<$is_end> length<$length>\n";
1372 } 1457 }
1373 1458
1374# check we are in a valid source file if not then ignore this hunk 1459# check we are in a valid source file if not then ignore this hunk
@@ -1377,12 +1462,18 @@ sub process {
1377#80 column limit 1462#80 column limit
1378 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 1463 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1379 $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 1464 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1380 $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ && 1465 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ ||
1466 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1381 $length > 80) 1467 $length > 80)
1382 { 1468 {
1383 WARN("line over 80 characters\n" . $herecurr); 1469 WARN("line over 80 characters\n" . $herecurr);
1384 } 1470 }
1385 1471
1472# check for spaces before a quoted newline
1473 if ($rawline =~ /^.*\".*\s\\n/) {
1474 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1475 }
1476
1386# check for adding lines without a newline. 1477# check for adding lines without a newline.
1387 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 1478 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1388 WARN("adding a line without newline at end of file\n" . $herecurr); 1479 WARN("adding a line without newline at end of file\n" . $herecurr);
@@ -1409,6 +1500,23 @@ sub process {
1409 $rawline =~ /^\+\s* \s*/) { 1500 $rawline =~ /^\+\s* \s*/) {
1410 my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 1501 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1411 ERROR("code indent should use tabs where possible\n" . $herevet); 1502 ERROR("code indent should use tabs where possible\n" . $herevet);
1503 $rpt_cleaners = 1;
1504 }
1505
1506# check for space before tabs.
1507 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1508 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1509 WARN("please, no space before tabs\n" . $herevet);
1510 }
1511
1512# check for spaces at the beginning of a line.
1513# Exceptions:
1514# 1) within comments
1515# 2) indented preprocessor commands
1516# 3) hanging labels
1517 if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) {
1518 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1519 WARN("please, no spaces at the start of a line\n" . $herevet);
1412 } 1520 }
1413 1521
1414# check we are in a valid C source file if not then ignore this hunk 1522# check we are in a valid C source file if not then ignore this hunk
@@ -1544,7 +1652,7 @@ sub process {
1544 1652
1545 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 1653 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1546 ERROR("that open brace { should be on the previous line\n" . 1654 ERROR("that open brace { should be on the previous line\n" .
1547 "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); 1655 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1548 } 1656 }
1549 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 1657 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1550 $ctx =~ /\)\s*\;\s*$/ && 1658 $ctx =~ /\)\s*\;\s*$/ &&
@@ -1553,7 +1661,7 @@ sub process {
1553 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 1661 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1554 if ($nindent > $indent) { 1662 if ($nindent > $indent) {
1555 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" . 1663 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1556 "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); 1664 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1557 } 1665 }
1558 } 1666 }
1559 } 1667 }
@@ -1714,8 +1822,17 @@ sub process {
1714 !defined $suppress_export{$realline_next} && 1822 !defined $suppress_export{$realline_next} &&
1715 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 1823 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1716 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 1824 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1825 # Handle definitions which produce identifiers with
1826 # a prefix:
1827 # XXX(foo);
1828 # EXPORT_SYMBOL(something_foo);
1717 my $name = $1; 1829 my $name = $1;
1718 if ($stat !~ /(?: 1830 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
1831 $name =~ /^${Ident}_$2/) {
1832#print "FOO C name<$name>\n";
1833 $suppress_export{$realline_next} = 1;
1834
1835 } elsif ($stat !~ /(?:
1719 \n.}\s*$| 1836 \n.}\s*$|
1720 ^.DEFINE_$Ident\(\Q$name\E\)| 1837 ^.DEFINE_$Ident\(\Q$name\E\)|
1721 ^.DECLARE_$Ident\(\Q$name\E\)| 1838 ^.DECLARE_$Ident\(\Q$name\E\)|
@@ -1741,9 +1858,9 @@ sub process {
1741 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 1858 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1742 } 1859 }
1743 1860
1744# check for external initialisers. 1861# check for global initialisers.
1745 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { 1862 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1746 ERROR("do not initialise externals to 0 or NULL\n" . 1863 ERROR("do not initialise globals to 0 or NULL\n" .
1747 $herecurr); 1864 $herecurr);
1748 } 1865 }
1749# check for static initialisers. 1866# check for static initialisers.
@@ -1752,6 +1869,23 @@ sub process {
1752 $herecurr); 1869 $herecurr);
1753 } 1870 }
1754 1871
1872# check for static const char * arrays.
1873 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
1874 WARN("static const char * array should probably be static const char * const\n" .
1875 $herecurr);
1876 }
1877
1878# check for static char foo[] = "bar" declarations.
1879 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
1880 WARN("static char array declaration should probably be static const char\n" .
1881 $herecurr);
1882 }
1883
1884# check for declarations of struct pci_device_id
1885 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
1886 WARN("Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
1887 }
1888
1755# check for new typedefs, only function parameters and sparse annotations 1889# check for new typedefs, only function parameters and sparse annotations
1756# make sense. 1890# make sense.
1757 if ($line =~ /\btypedef\s/ && 1891 if ($line =~ /\btypedef\s/ &&
@@ -1845,6 +1979,11 @@ sub process {
1845 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); 1979 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1846 } 1980 }
1847 1981
1982# missing space after union, struct or enum definition
1983 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
1984 WARN("missing space after $1 definition\n" . $herecurr);
1985 }
1986
1848# check for spacing round square brackets; allowed: 1987# check for spacing round square brackets; allowed:
1849# 1. with a type on the left -- int [] a; 1988# 1. with a type on the left -- int [] a;
1850# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 1989# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
@@ -2122,21 +2261,29 @@ sub process {
2122 my $value = $2; 2261 my $value = $2;
2123 2262
2124 # Flatten any parentheses 2263 # Flatten any parentheses
2125 $value =~ s/\)\(/\) \(/g; 2264 $value =~ s/\(/ \(/g;
2265 $value =~ s/\)/\) /g;
2126 while ($value =~ s/\[[^\{\}]*\]/1/ || 2266 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2127 $value !~ /(?:$Ident|-?$Constant)\s* 2267 $value !~ /(?:$Ident|-?$Constant)\s*
2128 $Compare\s* 2268 $Compare\s*
2129 (?:$Ident|-?$Constant)/x && 2269 (?:$Ident|-?$Constant)/x &&
2130 $value =~ s/\([^\(\)]*\)/1/) { 2270 $value =~ s/\([^\(\)]*\)/1/) {
2131 } 2271 }
2132 2272#print "value<$value>\n";
2133 if ($value =~ /^(?:$Ident|-?$Constant)$/) { 2273 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2134 ERROR("return is not a function, parentheses are not required\n" . $herecurr); 2274 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2135 2275
2136 } elsif ($spacing !~ /\s+/) { 2276 } elsif ($spacing !~ /\s+/) {
2137 ERROR("space required before the open parenthesis '('\n" . $herecurr); 2277 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2138 } 2278 }
2139 } 2279 }
2280# Return of what appears to be an errno should normally be -'ve
2281 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2282 my $name = $1;
2283 if ($name ne 'EOF' && $name ne 'ERROR') {
2284 WARN("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
2285 }
2286 }
2140 2287
2141# Need a space before open parenthesis after if, while etc 2288# Need a space before open parenthesis after if, while etc
2142 if ($line=~/\b(if|while|for|switch)\(/) { 2289 if ($line=~/\b(if|while|for|switch)\(/) {
@@ -2182,8 +2329,10 @@ sub process {
2182 # Find out how long the conditional actually is. 2329 # Find out how long the conditional actually is.
2183 my @newlines = ($c =~ /\n/gs); 2330 my @newlines = ($c =~ /\n/gs);
2184 my $cond_lines = 1 + $#newlines; 2331 my $cond_lines = 1 + $#newlines;
2332 my $stat_real = '';
2185 2333
2186 my $stat_real = raw_line($linenr, $cond_lines); 2334 $stat_real = raw_line($linenr, $cond_lines)
2335 . "\n" if ($cond_lines);
2187 if (defined($stat_real) && $cond_lines > 1) { 2336 if (defined($stat_real) && $cond_lines > 1) {
2188 $stat_real = "[...]\n$stat_real"; 2337 $stat_real = "[...]\n$stat_real";
2189 } 2338 }
@@ -2269,7 +2418,7 @@ sub process {
2269 my $checkfile = "include/linux/$file"; 2418 my $checkfile = "include/linux/$file";
2270 if (-f "$root/$checkfile" && 2419 if (-f "$root/$checkfile" &&
2271 $realfile ne $checkfile && 2420 $realfile ne $checkfile &&
2272 $1 ne 'irq') 2421 $1 !~ /$allowed_asm_includes/)
2273 { 2422 {
2274 if ($realfile =~ m{^arch/}) { 2423 if ($realfile =~ m{^arch/}) {
2275 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 2424 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
@@ -2348,11 +2497,13 @@ sub process {
2348 DECLARE_PER_CPU| 2497 DECLARE_PER_CPU|
2349 DEFINE_PER_CPU| 2498 DEFINE_PER_CPU|
2350 __typeof__\(| 2499 __typeof__\(|
2500 union|
2501 struct|
2351 \.$Ident\s*=\s*| 2502 \.$Ident\s*=\s*|
2352 ^\"|\"$ 2503 ^\"|\"$
2353 }x; 2504 }x;
2354 #print "REST<$rest> dstat<$dstat>\n"; 2505 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2355 if ($rest ne '') { 2506 if ($rest ne '' && $rest ne ',') {
2356 if ($rest !~ /while\s*\(/ && 2507 if ($rest !~ /while\s*\(/ &&
2357 $dstat !~ /$exceptions/) 2508 $dstat !~ /$exceptions/)
2358 { 2509 {
@@ -2529,6 +2680,21 @@ sub process {
2529 } 2680 }
2530 } 2681 }
2531 2682
2683# prefer usleep_range over udelay
2684 if ($line =~ /\budelay\s*\(\s*(\w+)\s*\)/) {
2685 # ignore udelay's < 10, however
2686 if (! (($1 =~ /(\d+)/) && ($1 < 10)) ) {
2687 CHK("usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
2688 }
2689 }
2690
2691# warn about unexpectedly long msleep's
2692 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
2693 if ($1 < 20) {
2694 WARN("msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
2695 }
2696 }
2697
2532# warn about #ifdefs in C files 2698# warn about #ifdefs in C files
2533# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 2699# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2534# print "#ifdef in C files should be avoided\n"; 2700# print "#ifdef in C files should be avoided\n";
@@ -2560,6 +2726,11 @@ sub process {
2560 CHK("architecture specific defines should be avoided\n" . $herecurr); 2726 CHK("architecture specific defines should be avoided\n" . $herecurr);
2561 } 2727 }
2562 2728
2729# Check that the storage class is at the beginning of a declaration
2730 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2731 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2732 }
2733
2563# check the location of the inline attribute, that it is between 2734# check the location of the inline attribute, that it is between
2564# storage class and type. 2735# storage class and type.
2565 if ($line =~ /\b$Type\s+$Inline\b/ || 2736 if ($line =~ /\b$Type\s+$Inline\b/ ||
@@ -2572,6 +2743,16 @@ sub process {
2572 WARN("plain inline is preferred over $1\n" . $herecurr); 2743 WARN("plain inline is preferred over $1\n" . $herecurr);
2573 } 2744 }
2574 2745
2746# Check for __attribute__ packed, prefer __packed
2747 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
2748 WARN("__packed is preferred over __attribute__((packed))\n" . $herecurr);
2749 }
2750
2751# check for sizeof(&)
2752 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2753 WARN("sizeof(& should be avoided\n" . $herecurr);
2754 }
2755
2575# check for new externs in .c files. 2756# check for new externs in .c files.
2576 if ($realfile =~ /\.c$/ && defined $stat && 2757 if ($realfile =~ /\.c$/ && defined $stat &&
2577 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 2758 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
@@ -2609,22 +2790,24 @@ sub process {
2609 } 2790 }
2610 2791
2611# check for pointless casting of kmalloc return 2792# check for pointless casting of kmalloc return
2612 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { 2793 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
2613 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 2794 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2614 } 2795 }
2615 2796
2797# check for multiple semicolons
2798 if ($line =~ /;\s*;\s*$/) {
2799 WARN("Statements terminations use 1 semicolon\n" . $herecurr);
2800 }
2801
2616# check for gcc specific __FUNCTION__ 2802# check for gcc specific __FUNCTION__
2617 if ($line =~ /__FUNCTION__/) { 2803 if ($line =~ /__FUNCTION__/) {
2618 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 2804 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2619 } 2805 }
2620 2806
2621# check for semaphores used as mutexes 2807# check for semaphores initialized locked
2622 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) { 2808 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
2623 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2624 }
2625# check for semaphores used as mutexes
2626 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2627 WARN("consider using a completion\n" . $herecurr); 2809 WARN("consider using a completion\n" . $herecurr);
2810
2628 } 2811 }
2629# recommend strict_strto* over simple_strto* 2812# recommend strict_strto* over simple_strto*
2630 if ($line =~ /\bsimple_(strto.*?)\s*\(/) { 2813 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
@@ -2634,9 +2817,46 @@ sub process {
2634 if ($line =~ /^.\s*__initcall\s*\(/) { 2817 if ($line =~ /^.\s*__initcall\s*\(/) {
2635 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr); 2818 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2636 } 2819 }
2637# check for struct file_operations, ensure they are const. 2820# check for various ops structs, ensure they are const.
2821 my $struct_ops = qr{acpi_dock_ops|
2822 address_space_operations|
2823 backlight_ops|
2824 block_device_operations|
2825 dentry_operations|
2826 dev_pm_ops|
2827 dma_map_ops|
2828 extent_io_ops|
2829 file_lock_operations|
2830 file_operations|
2831 hv_ops|
2832 ide_dma_ops|
2833 intel_dvo_dev_ops|
2834 item_operations|
2835 iwl_ops|
2836 kgdb_arch|
2837 kgdb_io|
2838 kset_uevent_ops|
2839 lock_manager_operations|
2840 microcode_ops|
2841 mtrr_ops|
2842 neigh_ops|
2843 nlmsvc_binding|
2844 pci_raw_ops|
2845 pipe_buf_operations|
2846 platform_hibernation_ops|
2847 platform_suspend_ops|
2848 proto_ops|
2849 rpc_pipe_ops|
2850 seq_operations|
2851 snd_ac97_build_ops|
2852 soc_pcmcia_socket_ops|
2853 stacktrace_ops|
2854 sysfs_ops|
2855 tty_operations|
2856 usb_mon_operations|
2857 wd_ops}x;
2638 if ($line !~ /\bconst\b/ && 2858 if ($line !~ /\bconst\b/ &&
2639 $line =~ /\bstruct\s+(file_operations|seq_operations)\b/) { 2859 $line =~ /\bstruct\s+($struct_ops)\b/) {
2640 WARN("struct $1 should normally be const\n" . 2860 WARN("struct $1 should normally be const\n" .
2641 $herecurr); 2861 $herecurr);
2642 } 2862 }
@@ -2672,6 +2892,21 @@ sub process {
2672 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 2892 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2673 } 2893 }
2674 } 2894 }
2895
2896# check for lockdep_set_novalidate_class
2897 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2898 $line =~ /__lockdep_no_validate__\s*\)/ ) {
2899 if ($realfile !~ m@^kernel/lockdep@ &&
2900 $realfile !~ m@^include/linux/lockdep@ &&
2901 $realfile !~ m@^drivers/base/core@) {
2902 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2903 }
2904 }
2905
2906 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
2907 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
2908 WARN("Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
2909 }
2675 } 2910 }
2676 2911
2677 # If we have no input at all, then there is nothing to report on 2912 # If we have no input at all, then there is nothing to report on
@@ -2708,6 +2943,15 @@ sub process {
2708 print "\n" if ($quiet == 0); 2943 print "\n" if ($quiet == 0);
2709 } 2944 }
2710 2945
2946 if ($quiet == 0) {
2947 # If there were whitespace errors which cleanpatch can fix
2948 # then suggest that.
2949 if ($rpt_cleaners) {
2950 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2951 print " scripts/cleanfile\n\n";
2952 }
2953 }
2954
2711 if ($clean == 1 && $quiet == 0) { 2955 if ($clean == 1 && $quiet == 0) {
2712 print "$vname has no obvious style problems and is ready for submission.\n" 2956 print "$vname has no obvious style problems and is ready for submission.\n"
2713 } 2957 }