diff options
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-x | scripts/checkpatch.pl | 150 |
1 files changed, 126 insertions, 24 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 2039acdf5122..90b54d4697fd 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 | ||
8 | use strict; | 8 | use strict; |
@@ -10,7 +10,7 @@ use strict; | |||
10 | my $P = $0; | 10 | my $P = $0; |
11 | $P =~ s@.*/@@g; | 11 | $P =~ s@.*/@@g; |
12 | 12 | ||
13 | my $V = '0.30'; | 13 | my $V = '0.31'; |
14 | 14 | ||
15 | use Getopt::Long qw(:config no_auto_abbrev); | 15 | use 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 | ||
106 | my $rpt_cleaners = 0; | ||
107 | |||
106 | if ($terse) { | 108 | if ($terse) { |
107 | $emacs = 1; | 109 | $emacs = 1; |
108 | $quiet++; | 110 | $quiet++; |
@@ -150,6 +152,20 @@ our $Sparse = qr{ | |||
150 | # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check | 152 | # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check |
151 | our $Attribute = qr{ | 153 | our $Attribute = qr{ |
152 | 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| | ||
153 | __read_mostly| | 169 | __read_mostly| |
154 | __kprobes| | 170 | __kprobes| |
155 | __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)| | 171 | __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)| |
@@ -675,15 +691,15 @@ sub ctx_block_get { | |||
675 | $blk .= $rawlines[$line]; | 691 | $blk .= $rawlines[$line]; |
676 | 692 | ||
677 | # Handle nested #if/#else. | 693 | # Handle nested #if/#else. |
678 | if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { | 694 | if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { |
679 | push(@stack, $level); | 695 | push(@stack, $level); |
680 | } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { | 696 | } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { |
681 | $level = $stack[$#stack - 1]; | 697 | $level = $stack[$#stack - 1]; |
682 | } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) { | 698 | } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { |
683 | $level = pop(@stack); | 699 | $level = pop(@stack); |
684 | } | 700 | } |
685 | 701 | ||
686 | foreach my $c (split(//, $rawlines[$line])) { | 702 | foreach my $c (split(//, $lines[$line])) { |
687 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; | 703 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
688 | if ($off > 0) { | 704 | if ($off > 0) { |
689 | $off--; | 705 | $off--; |
@@ -843,7 +859,12 @@ sub annotate_values { | |||
843 | $av_preprocessor = 0; | 859 | $av_preprocessor = 0; |
844 | } | 860 | } |
845 | 861 | ||
846 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) { | 862 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/) { |
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*$)/) { | ||
847 | print "DECLARE($1)\n" if ($dbg_values > 1); | 868 | print "DECLARE($1)\n" if ($dbg_values > 1); |
848 | $type = 'T'; | 869 | $type = 'T'; |
849 | 870 | ||
@@ -1308,7 +1329,11 @@ sub process { | |||
1308 | $here = "#$realline: " if ($file); | 1329 | $here = "#$realline: " if ($file); |
1309 | 1330 | ||
1310 | # extract the filename as it passes | 1331 | # extract the filename as it passes |
1311 | if ($line=~/^\+\+\+\s+(\S+)/) { | 1332 | if ($line =~ /^diff --git.*?(\S+)$/) { |
1333 | $realfile = $1; | ||
1334 | $realfile =~ s@^([^/]*)/@@; | ||
1335 | |||
1336 | } elsif ($line =~ /^\+\+\+\s+(\S+)/) { | ||
1312 | $realfile = $1; | 1337 | $realfile = $1; |
1313 | $realfile =~ s@^([^/]*)/@@; | 1338 | $realfile =~ s@^([^/]*)/@@; |
1314 | 1339 | ||
@@ -1332,6 +1357,14 @@ sub process { | |||
1332 | 1357 | ||
1333 | $cnt_lines++ if ($realcnt != 0); | 1358 | $cnt_lines++ if ($realcnt != 0); |
1334 | 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 | |||
1335 | #check the patch for a signoff: | 1368 | #check the patch for a signoff: |
1336 | if ($line =~ /^\s*signed-off-by:/i) { | 1369 | if ($line =~ /^\s*signed-off-by:/i) { |
1337 | # This is a signoff, if ugly, so do not double report. | 1370 | # This is a signoff, if ugly, so do not double report. |
@@ -1389,21 +1422,38 @@ sub process { | |||
1389 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { | 1422 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
1390 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; | 1423 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
1391 | ERROR("trailing whitespace\n" . $herevet); | 1424 | ERROR("trailing whitespace\n" . $herevet); |
1425 | $rpt_cleaners = 1; | ||
1392 | } | 1426 | } |
1393 | 1427 | ||
1394 | # check for Kconfig help text having a real description | 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. | ||
1395 | if ($realfile =~ /Kconfig/ && | 1431 | if ($realfile =~ /Kconfig/ && |
1396 | $line =~ /\+?\s*(---)?help(---)?$/) { | 1432 | $line =~ /\+\s*(?:---)?help(?:---)?$/) { |
1397 | my $length = 0; | 1433 | my $length = 0; |
1398 | for (my $l = $linenr; defined($lines[$l]); $l++) { | 1434 | my $cnt = $realcnt; |
1399 | my $f = $lines[$l]; | 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/^.//; | ||
1400 | $f =~ s/#.*//; | 1446 | $f =~ s/#.*//; |
1401 | $f =~ s/^\s+//; | 1447 | $f =~ s/^\s+//; |
1402 | next if ($f =~ /^$/); | 1448 | next if ($f =~ /^$/); |
1403 | last if ($f =~ /^\s*config\s/); | 1449 | if ($f =~ /^\s*config\s/) { |
1450 | $is_end = 1; | ||
1451 | last; | ||
1452 | } | ||
1404 | $length++; | 1453 | $length++; |
1405 | } | 1454 | } |
1406 | WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($length < 4); | 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"; | ||
1407 | } | 1457 | } |
1408 | 1458 | ||
1409 | # 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 |
@@ -1450,6 +1500,7 @@ sub process { | |||
1450 | $rawline =~ /^\+\s* \s*/) { | 1500 | $rawline =~ /^\+\s* \s*/) { |
1451 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; | 1501 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
1452 | 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; | ||
1453 | } | 1504 | } |
1454 | 1505 | ||
1455 | # check for space before tabs. | 1506 | # check for space before tabs. |
@@ -1459,10 +1510,13 @@ sub process { | |||
1459 | } | 1510 | } |
1460 | 1511 | ||
1461 | # check for spaces at the beginning of a line. | 1512 | # check for spaces at the beginning of a line. |
1462 | if ($rawline =~ /^\+ / && $rawline !~ /\+ +\*/) { | 1513 | # Exceptions: |
1514 | # 1) within comments | ||
1515 | # 2) indented preprocessor commands | ||
1516 | # 3) hanging labels | ||
1517 | if ($rawline =~ /^\+ / && $line !~ /\+ *(?:$;|#|$Ident:)/) { | ||
1463 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; | 1518 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
1464 | WARN("please, no space for starting a line, \ | 1519 | WARN("please, no spaces at the start of a line\n" . $herevet); |
1465 | excluding comments\n" . $herevet); | ||
1466 | } | 1520 | } |
1467 | 1521 | ||
1468 | # 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 |
@@ -1598,7 +1652,7 @@ sub process { | |||
1598 | 1652 | ||
1599 | 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*{/) { |
1600 | ERROR("that open brace { should be on the previous line\n" . | 1654 | ERROR("that open brace { should be on the previous line\n" . |
1601 | "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); | 1655 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
1602 | } | 1656 | } |
1603 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && | 1657 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && |
1604 | $ctx =~ /\)\s*\;\s*$/ && | 1658 | $ctx =~ /\)\s*\;\s*$/ && |
@@ -1607,7 +1661,7 @@ sub process { | |||
1607 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); | 1661 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
1608 | if ($nindent > $indent) { | 1662 | if ($nindent > $indent) { |
1609 | WARN("trailing semicolon indicates no statements, indent implies otherwise\n" . | 1663 | WARN("trailing semicolon indicates no statements, indent implies otherwise\n" . |
1610 | "$here\n$ctx\n$lines[$ctx_ln - 1]\n"); | 1664 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
1611 | } | 1665 | } |
1612 | } | 1666 | } |
1613 | } | 1667 | } |
@@ -1768,8 +1822,17 @@ sub process { | |||
1768 | !defined $suppress_export{$realline_next} && | 1822 | !defined $suppress_export{$realline_next} && |
1769 | ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || | 1823 | ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || |
1770 | $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); | ||
1771 | my $name = $1; | 1829 | my $name = $1; |
1772 | 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 !~ /(?: | ||
1773 | \n.}\s*$| | 1836 | \n.}\s*$| |
1774 | ^.DEFINE_$Ident\(\Q$name\E\)| | 1837 | ^.DEFINE_$Ident\(\Q$name\E\)| |
1775 | ^.DECLARE_$Ident\(\Q$name\E\)| | 1838 | ^.DECLARE_$Ident\(\Q$name\E\)| |
@@ -1806,6 +1869,23 @@ sub process { | |||
1806 | $herecurr); | 1869 | $herecurr); |
1807 | } | 1870 | } |
1808 | 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 | |||
1809 | # check for new typedefs, only function parameters and sparse annotations | 1889 | # check for new typedefs, only function parameters and sparse annotations |
1810 | # make sense. | 1890 | # make sense. |
1811 | if ($line =~ /\btypedef\s/ && | 1891 | if ($line =~ /\btypedef\s/ && |
@@ -1899,6 +1979,11 @@ sub process { | |||
1899 | 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); |
1900 | } | 1980 | } |
1901 | 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 | |||
1902 | # check for spacing round square brackets; allowed: | 1987 | # check for spacing round square brackets; allowed: |
1903 | # 1. with a type on the left -- int [] a; | 1988 | # 1. with a type on the left -- int [] a; |
1904 | # 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, |
@@ -2176,21 +2261,29 @@ sub process { | |||
2176 | my $value = $2; | 2261 | my $value = $2; |
2177 | 2262 | ||
2178 | # Flatten any parentheses | 2263 | # Flatten any parentheses |
2179 | $value =~ s/\)\(/\) \(/g; | 2264 | $value =~ s/\(/ \(/g; |
2265 | $value =~ s/\)/\) /g; | ||
2180 | while ($value =~ s/\[[^\{\}]*\]/1/ || | 2266 | while ($value =~ s/\[[^\{\}]*\]/1/ || |
2181 | $value !~ /(?:$Ident|-?$Constant)\s* | 2267 | $value !~ /(?:$Ident|-?$Constant)\s* |
2182 | $Compare\s* | 2268 | $Compare\s* |
2183 | (?:$Ident|-?$Constant)/x && | 2269 | (?:$Ident|-?$Constant)/x && |
2184 | $value =~ s/\([^\(\)]*\)/1/) { | 2270 | $value =~ s/\([^\(\)]*\)/1/) { |
2185 | } | 2271 | } |
2186 | 2272 | #print "value<$value>\n"; | |
2187 | if ($value =~ /^(?:$Ident|-?$Constant)$/) { | 2273 | if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) { |
2188 | 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); |
2189 | 2275 | ||
2190 | } elsif ($spacing !~ /\s+/) { | 2276 | } elsif ($spacing !~ /\s+/) { |
2191 | ERROR("space required before the open parenthesis '('\n" . $herecurr); | 2277 | ERROR("space required before the open parenthesis '('\n" . $herecurr); |
2192 | } | 2278 | } |
2193 | } | 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 | } | ||
2194 | 2287 | ||
2195 | # Need a space before open parenthesis after if, while etc | 2288 | # Need a space before open parenthesis after if, while etc |
2196 | if ($line=~/\b(if|while|for|switch)\(/) { | 2289 | if ($line=~/\b(if|while|for|switch)\(/) { |
@@ -2409,8 +2502,8 @@ sub process { | |||
2409 | \.$Ident\s*=\s*| | 2502 | \.$Ident\s*=\s*| |
2410 | ^\"|\"$ | 2503 | ^\"|\"$ |
2411 | }x; | 2504 | }x; |
2412 | #print "REST<$rest> dstat<$dstat>\n"; | 2505 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; |
2413 | if ($rest ne '') { | 2506 | if ($rest ne '' && $rest ne ',') { |
2414 | if ($rest !~ /while\s*\(/ && | 2507 | if ($rest !~ /while\s*\(/ && |
2415 | $dstat !~ /$exceptions/) | 2508 | $dstat !~ /$exceptions/) |
2416 | { | 2509 | { |
@@ -2839,6 +2932,15 @@ sub process { | |||
2839 | print "\n" if ($quiet == 0); | 2932 | print "\n" if ($quiet == 0); |
2840 | } | 2933 | } |
2841 | 2934 | ||
2935 | if ($quiet == 0) { | ||
2936 | # If there were whitespace errors which cleanpatch can fix | ||
2937 | # then suggest that. | ||
2938 | if ($rpt_cleaners) { | ||
2939 | print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; | ||
2940 | print " scripts/cleanfile\n\n"; | ||
2941 | } | ||
2942 | } | ||
2943 | |||
2842 | if ($clean == 1 && $quiet == 0) { | 2944 | if ($clean == 1 && $quiet == 0) { |
2843 | print "$vname has no obvious style problems and is ready for submission.\n" | 2945 | print "$vname has no obvious style problems and is ready for submission.\n" |
2844 | } | 2946 | } |