aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl718
1 files changed, 185 insertions, 533 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 4d2c7dfdaab..3dfc47134e5 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -33,7 +33,6 @@ my %ignore_type = ();
33my @ignore = (); 33my @ignore = ();
34my $help = 0; 34my $help = 0;
35my $configuration_file = ".checkpatch.conf"; 35my $configuration_file = ".checkpatch.conf";
36my $max_line_length = 80;
37 36
38sub help { 37sub help {
39 my ($exitcode) = @_; 38 my ($exitcode) = @_;
@@ -52,7 +51,6 @@ Options:
52 -f, --file treat FILE as regular source file 51 -f, --file treat FILE as regular source file
53 --subjective, --strict enable more subjective tests 52 --subjective, --strict enable more subjective tests
54 --ignore TYPE(,TYPE2...) ignore various comma separated message types 53 --ignore TYPE(,TYPE2...) ignore various comma separated message types
55 --max-line-length=n set the maximum line length, if exceeded, warn
56 --show-types show the message "types" in the output 54 --show-types show the message "types" in the output
57 --root=PATH PATH to the kernel tree root 55 --root=PATH PATH to the kernel tree root
58 --no-summary suppress the per-file summary 56 --no-summary suppress the per-file summary
@@ -109,7 +107,6 @@ GetOptions(
109 'strict!' => \$check, 107 'strict!' => \$check,
110 'ignore=s' => \@ignore, 108 'ignore=s' => \@ignore,
111 'show-types!' => \$show_types, 109 'show-types!' => \$show_types,
112 'max-line-length=i' => \$max_line_length,
113 'root=s' => \$root, 110 'root=s' => \$root,
114 'summary!' => \$summary, 111 'summary!' => \$summary,
115 'mailback!' => \$mailback, 112 'mailback!' => \$mailback,
@@ -230,11 +227,7 @@ our $Inline = qr{inline|__always_inline|noinline};
230our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 227our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
231our $Lval = qr{$Ident(?:$Member)*}; 228our $Lval = qr{$Ident(?:$Member)*};
232 229
233our $Float_hex = qr{(?i:0x[0-9a-f]+p-?[0-9]+[fl]?)}; 230our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
234our $Float_dec = qr{(?i:((?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?))};
235our $Float_int = qr{(?i:[0-9]+e-?[0-9]+[fl]?)};
236our $Float = qr{$Float_hex|$Float_dec|$Float_int};
237our $Constant = qr{(?:$Float|(?i:(?:0x[0-9a-f]+|[0-9]+)[ul]*))};
238our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; 231our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
239our $Compare = qr{<=|>=|==|!=|<|>}; 232our $Compare = qr{<=|>=|==|!=|<|>};
240our $Operators = qr{ 233our $Operators = qr{
@@ -247,8 +240,9 @@ our $NonptrType;
247our $Type; 240our $Type;
248our $Declare; 241our $Declare;
249 242
250our $NON_ASCII_UTF8 = qr{ 243our $UTF8 = qr {
251 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 244 [\x09\x0A\x0D\x20-\x7E] # ASCII
245 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
252 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 246 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
253 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 247 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
254 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 248 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
@@ -257,11 +251,6 @@ our $NON_ASCII_UTF8 = qr{
257 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 251 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
258}x; 252}x;
259 253
260our $UTF8 = qr{
261 [\x09\x0A\x0D\x20-\x7E] # ASCII
262 | $NON_ASCII_UTF8
263}x;
264
265our $typeTypedefs = qr{(?x: 254our $typeTypedefs = qr{(?x:
266 (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 255 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
267 atomic_t 256 atomic_t
@@ -322,7 +311,7 @@ sub build_types {
322 $NonptrType = qr{ 311 $NonptrType = qr{
323 (?:$Modifier\s+|const\s+)* 312 (?:$Modifier\s+|const\s+)*
324 (?: 313 (?:
325 (?:typeof|__typeof__)\s*\([^\)]*\)| 314 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
326 (?:$typeTypedefs\b)| 315 (?:$typeTypedefs\b)|
327 (?:${all}\b) 316 (?:${all}\b)
328 ) 317 )
@@ -330,23 +319,17 @@ sub build_types {
330 }x; 319 }x;
331 $Type = qr{ 320 $Type = qr{
332 $NonptrType 321 $NonptrType
333 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)? 322 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
334 (?:\s+$Inline|\s+$Modifier)* 323 (?:\s+$Inline|\s+$Modifier)*
335 }x; 324 }x;
336 $Declare = qr{(?:$Storage\s+)?$Type}; 325 $Declare = qr{(?:$Storage\s+)?$Type};
337} 326}
338build_types(); 327build_types();
339 328
329our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
340 330
341our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 331our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
342 332our $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
343# Using $balanced_parens, $LvalOrFunc, or $FuncArg
344# requires at least perl version v5.10.0
345# Any use must be runtime checked with $^V
346
347our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
348our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
349our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
350 333
351sub deparenthesize { 334sub deparenthesize {
352 my ($string) = @_; 335 my ($string) = @_;
@@ -359,6 +342,27 @@ sub deparenthesize {
359 342
360$chk_signoff = 0 if ($file); 343$chk_signoff = 0 if ($file);
361 344
345my @dep_includes = ();
346my @dep_functions = ();
347my $removal = "Documentation/feature-removal-schedule.txt";
348if ($tree && -f "$root/$removal") {
349 open(my $REMOVE, '<', "$root/$removal") ||
350 die "$P: $removal: open failed - $!\n";
351 while (<$REMOVE>) {
352 if (/^Check:\s+(.*\S)/) {
353 for my $entry (split(/[, ]+/, $1)) {
354 if ($entry =~ m@include/(.*)@) {
355 push(@dep_includes, $1);
356
357 } elsif ($entry !~ m@/@) {
358 push(@dep_functions, $entry);
359 }
360 }
361 }
362 }
363 close($REMOVE);
364}
365
362my @rawlines = (); 366my @rawlines = ();
363my @lines = (); 367my @lines = ();
364my $vname; 368my $vname;
@@ -407,7 +411,7 @@ sub top_of_kernel_tree {
407 } 411 }
408 } 412 }
409 return 1; 413 return 1;
410} 414 }
411 415
412sub parse_email { 416sub parse_email {
413 my ($formatted_email) = @_; 417 my ($formatted_email) = @_;
@@ -668,10 +672,6 @@ sub ctx_statement_block {
668 if ($off >= $len) { 672 if ($off >= $len) {
669 last; 673 last;
670 } 674 }
671 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
672 $level++;
673 $type = '#';
674 }
675 } 675 }
676 $p = $c; 676 $p = $c;
677 $c = substr($blk, $off, 1); 677 $c = substr($blk, $off, 1);
@@ -734,13 +734,6 @@ sub ctx_statement_block {
734 last; 734 last;
735 } 735 }
736 } 736 }
737 # Preprocessor commands end at the newline unless escaped.
738 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
739 $level--;
740 $type = '';
741 $off++;
742 last;
743 }
744 $off++; 737 $off++;
745 } 738 }
746 # We are truly at the end, so shuffle to the next line. 739 # We are truly at the end, so shuffle to the next line.
@@ -1023,7 +1016,7 @@ sub annotate_values {
1023 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 1016 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1024 print "CAST($1)\n" if ($dbg_values > 1); 1017 print "CAST($1)\n" if ($dbg_values > 1);
1025 push(@av_paren_type, $type); 1018 push(@av_paren_type, $type);
1026 $type = 'c'; 1019 $type = 'C';
1027 1020
1028 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1021 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1029 print "DECLARE($1)\n" if ($dbg_values > 1); 1022 print "DECLARE($1)\n" if ($dbg_values > 1);
@@ -1215,9 +1208,7 @@ sub possible {
1215 case| 1208 case|
1216 else| 1209 else|
1217 asm|__asm__| 1210 asm|__asm__|
1218 do| 1211 do
1219 \#|
1220 \#\#|
1221 )(?:\s|$)| 1212 )(?:\s|$)|
1222 ^(?:typedef|struct|enum)\b 1213 ^(?:typedef|struct|enum)\b
1223 )}x; 1214 )}x;
@@ -1321,36 +1312,6 @@ sub check_absolute_file {
1321 } 1312 }
1322} 1313}
1323 1314
1324sub pos_last_openparen {
1325 my ($line) = @_;
1326
1327 my $pos = 0;
1328
1329 my $opens = $line =~ tr/\(/\(/;
1330 my $closes = $line =~ tr/\)/\)/;
1331
1332 my $last_openparen = 0;
1333
1334 if (($opens == 0) || ($closes >= $opens)) {
1335 return -1;
1336 }
1337
1338 my $len = length($line);
1339
1340 for ($pos = 0; $pos < $len; $pos++) {
1341 my $string = substr($line, $pos);
1342 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1343 $pos += length($1) - 1;
1344 } elsif (substr($line, $pos, 1) eq '(') {
1345 $last_openparen = $pos;
1346 } elsif (index($string, '(') == -1) {
1347 last;
1348 }
1349 }
1350
1351 return $last_openparen + 1;
1352}
1353
1354sub process { 1315sub process {
1355 my $filename = shift; 1316 my $filename = shift;
1356 1317
@@ -1369,11 +1330,6 @@ sub process {
1369 my $signoff = 0; 1330 my $signoff = 0;
1370 my $is_patch = 0; 1331 my $is_patch = 0;
1371 1332
1372 my $in_header_lines = 1;
1373 my $in_commit_log = 0; #Scanning lines before patch
1374
1375 my $non_utf8_charset = 0;
1376
1377 our @report = (); 1333 our @report = ();
1378 our $cnt_lines = 0; 1334 our $cnt_lines = 0;
1379 our $cnt_error = 0; 1335 our $cnt_error = 0;
@@ -1396,9 +1352,6 @@ sub process {
1396 my %suppress_ifbraces; 1352 my %suppress_ifbraces;
1397 my %suppress_whiletrailers; 1353 my %suppress_whiletrailers;
1398 my %suppress_export; 1354 my %suppress_export;
1399 my $suppress_statement = 0;
1400
1401 my %camelcase = ();
1402 1355
1403 # Pre-scan the patch sanitizing the lines. 1356 # Pre-scan the patch sanitizing the lines.
1404 # Pre-scan the patch looking for any __setup documentation. 1357 # Pre-scan the patch looking for any __setup documentation.
@@ -1508,7 +1461,6 @@ sub process {
1508 %suppress_ifbraces = (); 1461 %suppress_ifbraces = ();
1509 %suppress_whiletrailers = (); 1462 %suppress_whiletrailers = ();
1510 %suppress_export = (); 1463 %suppress_export = ();
1511 $suppress_statement = 0;
1512 next; 1464 next;
1513 1465
1514# track the line number as we move through the hunk, note that 1466# track the line number as we move through the hunk, note that
@@ -1545,11 +1497,10 @@ sub process {
1545 if ($line =~ /^diff --git.*?(\S+)$/) { 1497 if ($line =~ /^diff --git.*?(\S+)$/) {
1546 $realfile = $1; 1498 $realfile = $1;
1547 $realfile =~ s@^([^/]*)/@@; 1499 $realfile =~ s@^([^/]*)/@@;
1548 $in_commit_log = 0; 1500
1549 } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 1501 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1550 $realfile = $1; 1502 $realfile = $1;
1551 $realfile =~ s@^([^/]*)/@@; 1503 $realfile =~ s@^([^/]*)/@@;
1552 $in_commit_log = 0;
1553 1504
1554 $p1_prefix = $1; 1505 $p1_prefix = $1;
1555 if (!$file && $tree && $p1_prefix ne '' && 1506 if (!$file && $tree && $p1_prefix ne '' &&
@@ -1585,22 +1536,16 @@ sub process {
1585# Check the patch for a signoff: 1536# Check the patch for a signoff:
1586 if ($line =~ /^\s*signed-off-by:/i) { 1537 if ($line =~ /^\s*signed-off-by:/i) {
1587 $signoff++; 1538 $signoff++;
1588 $in_commit_log = 0;
1589 } 1539 }
1590 1540
1591# Check signature styles 1541# Check signature styles
1592 if (!$in_header_lines && 1542 if ($line =~ /^(\s*)($signature_tags)(\s*)(.*)/) {
1593 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
1594 my $space_before = $1; 1543 my $space_before = $1;
1595 my $sign_off = $2; 1544 my $sign_off = $2;
1596 my $space_after = $3; 1545 my $space_after = $3;
1597 my $email = $4; 1546 my $email = $4;
1598 my $ucfirst_sign_off = ucfirst(lc($sign_off)); 1547 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1599 1548
1600 if ($sign_off !~ /$signature_tags/) {
1601 WARN("BAD_SIGN_OFF",
1602 "Non-standard signature: $sign_off\n" . $herecurr);
1603 }
1604 if (defined $space_before && $space_before ne "") { 1549 if (defined $space_before && $space_before ne "") {
1605 WARN("BAD_SIGN_OFF", 1550 WARN("BAD_SIGN_OFF",
1606 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr); 1551 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr);
@@ -1668,28 +1613,6 @@ sub process {
1668 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 1613 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1669 } 1614 }
1670 1615
1671# Check if it's the start of a commit log
1672# (not a header line and we haven't seen the patch filename)
1673 if ($in_header_lines && $realfile =~ /^$/ &&
1674 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
1675 $in_header_lines = 0;
1676 $in_commit_log = 1;
1677 }
1678
1679# Check if there is UTF-8 in a commit log when a mail header has explicitly
1680# declined it, i.e defined some charset where it is missing.
1681 if ($in_header_lines &&
1682 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1683 $1 !~ /utf-8/i) {
1684 $non_utf8_charset = 1;
1685 }
1686
1687 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
1688 $rawline =~ /$NON_ASCII_UTF8/) {
1689 WARN("UTF8_BEFORE_PATCH",
1690 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1691 }
1692
1693# ignore non-hunk lines and lines being removed 1616# ignore non-hunk lines and lines being removed
1694 next if (!$hunk_line || $line =~ /^-/); 1617 next if (!$hunk_line || $line =~ /^-/);
1695 1618
@@ -1710,26 +1633,19 @@ sub process {
1710# Only applies when adding the entry originally, after that we do not have 1633# Only applies when adding the entry originally, after that we do not have
1711# sufficient context to determine whether it is indeed long enough. 1634# sufficient context to determine whether it is indeed long enough.
1712 if ($realfile =~ /Kconfig/ && 1635 if ($realfile =~ /Kconfig/ &&
1713 $line =~ /.\s*config\s+/) { 1636 $line =~ /\+\s*(?:---)?help(?:---)?$/) {
1714 my $length = 0; 1637 my $length = 0;
1715 my $cnt = $realcnt; 1638 my $cnt = $realcnt;
1716 my $ln = $linenr + 1; 1639 my $ln = $linenr + 1;
1717 my $f; 1640 my $f;
1718 my $is_start = 0;
1719 my $is_end = 0; 1641 my $is_end = 0;
1720 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 1642 while ($cnt > 0 && defined $lines[$ln - 1]) {
1721 $f = $lines[$ln - 1]; 1643 $f = $lines[$ln - 1];
1722 $cnt-- if ($lines[$ln - 1] !~ /^-/); 1644 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1723 $is_end = $lines[$ln - 1] =~ /^\+/; 1645 $is_end = $lines[$ln - 1] =~ /^\+/;
1646 $ln++;
1724 1647
1725 next if ($f =~ /^-/); 1648 next if ($f =~ /^-/);
1726
1727 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1728 $is_start = 1;
1729 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1730 $length = -1;
1731 }
1732
1733 $f =~ s/^.//; 1649 $f =~ s/^.//;
1734 $f =~ s/#.*//; 1650 $f =~ s/#.*//;
1735 $f =~ s/^\s+//; 1651 $f =~ s/^\s+//;
@@ -1741,58 +1657,22 @@ sub process {
1741 $length++; 1657 $length++;
1742 } 1658 }
1743 WARN("CONFIG_DESCRIPTION", 1659 WARN("CONFIG_DESCRIPTION",
1744 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4); 1660 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4);
1745 #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 1661 #print "is_end<$is_end> length<$length>\n";
1746 }
1747
1748# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1749 if ($realfile =~ /Kconfig/ &&
1750 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1751 WARN("CONFIG_EXPERIMENTAL",
1752 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1753 }
1754
1755 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1756 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1757 my $flag = $1;
1758 my $replacement = {
1759 'EXTRA_AFLAGS' => 'asflags-y',
1760 'EXTRA_CFLAGS' => 'ccflags-y',
1761 'EXTRA_CPPFLAGS' => 'cppflags-y',
1762 'EXTRA_LDFLAGS' => 'ldflags-y',
1763 };
1764
1765 WARN("DEPRECATED_VARIABLE",
1766 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1767 } 1662 }
1768 1663
1769# check we are in a valid source file if not then ignore this hunk 1664# check we are in a valid source file if not then ignore this hunk
1770 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 1665 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1771 1666
1772#line length limit 1667#80 column limit
1773 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 1668 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1774 $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 1669 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1775 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ || 1670 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
1776 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && 1671 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1777 $length > $max_line_length) 1672 $length > 80)
1778 { 1673 {
1779 WARN("LONG_LINE", 1674 WARN("LONG_LINE",
1780 "line over $max_line_length characters\n" . $herecurr); 1675 "line over 80 characters\n" . $herecurr);
1781 }
1782
1783# Check for user-visible strings broken across lines, which breaks the ability
1784# to grep for the string. Limited to strings used as parameters (those
1785# following an open parenthesis), which almost completely eliminates false
1786# positives, as well as warning only once per parameter rather than once per
1787# line of the string. Make an exception when the previous string ends in a
1788# newline (multiple lines in one string constant) or \n\t (common in inline
1789# assembly to indent the instruction on the following line).
1790 if ($line =~ /^\+\s*"/ &&
1791 $prevline =~ /"\s*$/ &&
1792 $prevline =~ /\(/ &&
1793 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1794 WARN("SPLIT_STRING",
1795 "quoted string split across lines\n" . $hereprev);
1796 } 1676 }
1797 1677
1798# check for spaces before a quoted newline 1678# check for spaces before a quoted newline
@@ -1841,58 +1721,6 @@ sub process {
1841 "please, no space before tabs\n" . $herevet); 1721 "please, no space before tabs\n" . $herevet);
1842 } 1722 }
1843 1723
1844# check for && or || at the start of a line
1845 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1846 CHK("LOGICAL_CONTINUATIONS",
1847 "Logical continuations should be on the previous line\n" . $hereprev);
1848 }
1849
1850# check multi-line statement indentation matches previous line
1851 if ($^V && $^V ge 5.10.0 &&
1852 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1853 $prevline =~ /^\+(\t*)(.*)$/;
1854 my $oldindent = $1;
1855 my $rest = $2;
1856
1857 my $pos = pos_last_openparen($rest);
1858 if ($pos >= 0) {
1859 $line =~ /^(\+| )([ \t]*)/;
1860 my $newindent = $2;
1861
1862 my $goodtabindent = $oldindent .
1863 "\t" x ($pos / 8) .
1864 " " x ($pos % 8);
1865 my $goodspaceindent = $oldindent . " " x $pos;
1866
1867 if ($newindent ne $goodtabindent &&
1868 $newindent ne $goodspaceindent) {
1869 CHK("PARENTHESIS_ALIGNMENT",
1870 "Alignment should match open parenthesis\n" . $hereprev);
1871 }
1872 }
1873 }
1874
1875 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) {
1876 CHK("SPACING",
1877 "No space is necessary after a cast\n" . $hereprev);
1878 }
1879
1880 if ($realfile =~ m@^(drivers/net/|net/)@ &&
1881 $rawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
1882 $prevrawline =~ /^\+[ \t]*$/) {
1883 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1884 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
1885 }
1886
1887 if ($realfile =~ m@^(drivers/net/|net/)@ &&
1888 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1889 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1890 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
1891 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
1892 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1893 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
1894 }
1895
1896# check for spaces at the beginning of a line. 1724# check for spaces at the beginning of a line.
1897# Exceptions: 1725# Exceptions:
1898# 1) within comments 1726# 1) within comments
@@ -1907,12 +1735,6 @@ sub process {
1907# check we are in a valid C source file if not then ignore this hunk 1735# check we are in a valid C source file if not then ignore this hunk
1908 next if ($realfile !~ /\.(h|c)$/); 1736 next if ($realfile !~ /\.(h|c)$/);
1909 1737
1910# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
1911 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
1912 WARN("CONFIG_EXPERIMENTAL",
1913 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1914 }
1915
1916# check for RCS/CVS revision markers 1738# check for RCS/CVS revision markers
1917 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 1739 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1918 WARN("CVS_KEYWORD", 1740 WARN("CVS_KEYWORD",
@@ -1934,24 +1756,12 @@ sub process {
1934# Check for potential 'bare' types 1756# Check for potential 'bare' types
1935 my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 1757 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1936 $realline_next); 1758 $realline_next);
1937#print "LINE<$line>\n"; 1759 if ($realcnt && $line =~ /.\s*\S/) {
1938 if ($linenr >= $suppress_statement &&
1939 $realcnt && $line =~ /.\s*\S/) {
1940 ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 1760 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1941 ctx_statement_block($linenr, $realcnt, 0); 1761 ctx_statement_block($linenr, $realcnt, 0);
1942 $stat =~ s/\n./\n /g; 1762 $stat =~ s/\n./\n /g;
1943 $cond =~ s/\n./\n /g; 1763 $cond =~ s/\n./\n /g;
1944 1764
1945#print "linenr<$linenr> <$stat>\n";
1946 # If this statement has no statement boundaries within
1947 # it there is no point in retrying a statement scan
1948 # until we hit end of it.
1949 my $frag = $stat; $frag =~ s/;+\s*$//;
1950 if ($frag !~ /(?:{|;)/) {
1951#print "skip<$line_nr_next>\n";
1952 $suppress_statement = $line_nr_next;
1953 }
1954
1955 # Find the real next line. 1765 # Find the real next line.
1956 $realline_next = $line_nr_next; 1766 $realline_next = $line_nr_next;
1957 if (defined $realline_next && 1767 if (defined $realline_next &&
@@ -2040,12 +1850,6 @@ sub process {
2040 my $pre_ctx = "$1$2"; 1850 my $pre_ctx = "$1$2";
2041 1851
2042 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 1852 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
2043
2044 if ($line =~ /^\+\t{6,}/) {
2045 WARN("DEEP_INDENTATION",
2046 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2047 }
2048
2049 my $ctx_cnt = $realcnt - $#ctx - 1; 1853 my $ctx_cnt = $realcnt - $#ctx - 1;
2050 my $ctx = join("\n", @ctx); 1854 my $ctx = join("\n", @ctx);
2051 1855
@@ -2083,9 +1887,6 @@ sub process {
2083 1887
2084# Check relative indent for conditionals and blocks. 1888# Check relative indent for conditionals and blocks.
2085 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 1889 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2086 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2087 ctx_statement_block($linenr, $realcnt, 0)
2088 if (!defined $stat);
2089 my ($s, $c) = ($stat, $cond); 1890 my ($s, $c) = ($stat, $cond);
2090 1891
2091 substr($s, 0, length($c), ''); 1892 substr($s, 0, length($c), '');
@@ -2226,11 +2027,8 @@ sub process {
2226 my $path = $1; 2027 my $path = $1;
2227 if ($path =~ m{//}) { 2028 if ($path =~ m{//}) {
2228 ERROR("MALFORMED_INCLUDE", 2029 ERROR("MALFORMED_INCLUDE",
2229 "malformed #include filename\n" . $herecurr); 2030 "malformed #include filename\n" .
2230 } 2031 $herecurr);
2231 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2232 ERROR("UAPI_INCLUDE",
2233 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2234 } 2032 }
2235 } 2033 }
2236 2034
@@ -2256,7 +2054,7 @@ sub process {
2256 # XXX(foo); 2054 # XXX(foo);
2257 # EXPORT_SYMBOL(something_foo); 2055 # EXPORT_SYMBOL(something_foo);
2258 my $name = $1; 2056 my $name = $1;
2259 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 2057 if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ &&
2260 $name =~ /^${Ident}_$2/) { 2058 $name =~ /^${Ident}_$2/) {
2261#print "FOO C name<$name>\n"; 2059#print "FOO C name<$name>\n";
2262 $suppress_export{$realline_next} = 1; 2060 $suppress_export{$realline_next} = 1;
@@ -2334,9 +2132,8 @@ sub process {
2334 2132
2335# * goes on variable not on type 2133# * goes on variable not on type
2336 # (char*[ const]) 2134 # (char*[ const])
2337 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 2135 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
2338 #print "AA<$1>\n"; 2136 my ($from, $to) = ($1, $1);
2339 my ($from, $to) = ($2, $2);
2340 2137
2341 # Should start with a space. 2138 # Should start with a space.
2342 $to =~ s/^(\S)/ $1/; 2139 $to =~ s/^(\S)/ $1/;
@@ -2351,10 +2148,8 @@ sub process {
2351 ERROR("POINTER_LOCATION", 2148 ERROR("POINTER_LOCATION",
2352 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); 2149 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
2353 } 2150 }
2354 } 2151 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
2355 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 2152 my ($from, $to, $ident) = ($1, $1, $2);
2356 #print "BB<$1>\n";
2357 my ($from, $to, $ident) = ($2, $2, $3);
2358 2153
2359 # Should start with a space. 2154 # Should start with a space.
2360 $to =~ s/^(\S)/ $1/; 2155 $to =~ s/^(\S)/ $1/;
@@ -2415,21 +2210,6 @@ sub process {
2415 } 2210 }
2416 } 2211 }
2417 2212
2418 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2419 my $orig = $1;
2420 my $level = lc($orig);
2421 $level = "warn" if ($level eq "warning");
2422 my $level2 = $level;
2423 $level2 = "dbg" if ($level eq "debug");
2424 WARN("PREFER_PR_LEVEL",
2425 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
2426 }
2427
2428 if ($line =~ /\bpr_warning\s*\(/) {
2429 WARN("PREFER_PR_LEVEL",
2430 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr);
2431 }
2432
2433# function brace can't be on same line, except for #defines of do while, 2213# function brace can't be on same line, except for #defines of do while,
2434# or if closed on same line 2214# or if closed on same line
2435 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and 2215 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
@@ -2459,7 +2239,7 @@ sub process {
2459 my ($where, $prefix) = ($-[1], $1); 2239 my ($where, $prefix) = ($-[1], $1);
2460 if ($prefix !~ /$Type\s+$/ && 2240 if ($prefix !~ /$Type\s+$/ &&
2461 ($where != 0 || $prefix !~ /^.\s+$/) && 2241 ($where != 0 || $prefix !~ /^.\s+$/) &&
2462 $prefix !~ /[{,]\s+$/) { 2242 $prefix !~ /{\s+$/) {
2463 ERROR("BRACKET_SPACE", 2243 ERROR("BRACKET_SPACE",
2464 "space prohibited before open square bracket '['\n" . $herecurr); 2244 "space prohibited before open square bracket '['\n" . $herecurr);
2465 } 2245 }
@@ -2496,13 +2276,6 @@ sub process {
2496 "space prohibited between function name and open parenthesis '('\n" . $herecurr); 2276 "space prohibited between function name and open parenthesis '('\n" . $herecurr);
2497 } 2277 }
2498 } 2278 }
2499
2500# check for whitespace before a non-naked semicolon
2501 if ($line =~ /^\+.*\S\s+;/) {
2502 CHK("SPACING",
2503 "space prohibited before semicolon\n" . $herecurr);
2504 }
2505
2506# Check operator spacing. 2279# Check operator spacing.
2507 if (!($line=~/\#\s*include/)) { 2280 if (!($line=~/\#\s*include/)) {
2508 my $ops = qr{ 2281 my $ops = qr{
@@ -2759,7 +2532,7 @@ sub process {
2759 # Flatten any parentheses 2532 # Flatten any parentheses
2760 $value =~ s/\(/ \(/g; 2533 $value =~ s/\(/ \(/g;
2761 $value =~ s/\)/\) /g; 2534 $value =~ s/\)/\) /g;
2762 while ($value =~ s/\[[^\[\]]*\]/1/ || 2535 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2763 $value !~ /(?:$Ident|-?$Constant)\s* 2536 $value !~ /(?:$Ident|-?$Constant)\s*
2764 $Compare\s* 2537 $Compare\s*
2765 (?:$Ident|-?$Constant)/x && 2538 (?:$Ident|-?$Constant)/x &&
@@ -2784,6 +2557,28 @@ sub process {
2784 } 2557 }
2785 } 2558 }
2786 2559
2560# typecasts on min/max could be min_t/max_t
2561 if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) {
2562 if (defined $2 || defined $8) {
2563 my $call = $1;
2564 my $cast1 = deparenthesize($2);
2565 my $arg1 = $3;
2566 my $cast2 = deparenthesize($8);
2567 my $arg2 = $9;
2568 my $cast;
2569
2570 if ($cast1 ne "" && $cast2 ne "") {
2571 $cast = "$cast1 or $cast2";
2572 } elsif ($cast1 ne "") {
2573 $cast = $cast1;
2574 } else {
2575 $cast = $cast2;
2576 }
2577 WARN("MINMAX",
2578 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr);
2579 }
2580 }
2581
2787# Need a space before open parenthesis after if, while etc 2582# Need a space before open parenthesis after if, while etc
2788 if ($line=~/\b(if|while|for|switch)\(/) { 2583 if ($line=~/\b(if|while|for|switch)\(/) {
2789 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr); 2584 ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr);
@@ -2792,9 +2587,6 @@ sub process {
2792# Check for illegal assignment in if conditional -- and check for trailing 2587# Check for illegal assignment in if conditional -- and check for trailing
2793# statements after the conditional. 2588# statements after the conditional.
2794 if ($line =~ /do\s*(?!{)/) { 2589 if ($line =~ /do\s*(?!{)/) {
2795 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2796 ctx_statement_block($linenr, $realcnt, 0)
2797 if (!defined $stat);
2798 my ($stat_next) = ctx_statement_block($line_nr_next, 2590 my ($stat_next) = ctx_statement_block($line_nr_next,
2799 $remain_next, $off_next); 2591 $remain_next, $off_next);
2800 $stat_next =~ s/\n./\n /g; 2592 $stat_next =~ s/\n./\n /g;
@@ -2910,17 +2702,12 @@ sub process {
2910 } 2702 }
2911 } 2703 }
2912 2704
2913#CamelCase 2705#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2914 while ($line =~ m{($Constant|$Lval)}g) { 2706# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2915 my $var = $1; 2707# print "No studly caps, use _\n";
2916 if ($var !~ /$Constant/ && 2708# print "$herecurr";
2917 $var =~ /[A-Z]\w*[a-z]|[a-z]\w*[A-Z]/ && 2709# $clean = 0;
2918 !defined $camelcase{$var}) { 2710# }
2919 $camelcase{$var} = 1;
2920 WARN("CAMELCASE",
2921 "Avoid CamelCase: <$var>\n" . $herecurr);
2922 }
2923 }
2924 2711
2925#no spaces allowed after \ in define 2712#no spaces allowed after \ in define
2926 if ($line=~/\#\s*define.*\\\s$/) { 2713 if ($line=~/\#\s*define.*\\\s$/) {
@@ -2955,13 +2742,47 @@ sub process {
2955 my $cnt = $realcnt; 2742 my $cnt = $realcnt;
2956 my ($off, $dstat, $dcond, $rest); 2743 my ($off, $dstat, $dcond, $rest);
2957 my $ctx = ''; 2744 my $ctx = '';
2745
2746 my $args = defined($1);
2747
2748 # Find the end of the macro and limit our statement
2749 # search to that.
2750 while ($cnt > 0 && defined $lines[$ln - 1] &&
2751 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2752 {
2753 $ctx .= $rawlines[$ln - 1] . "\n";
2754 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2755 $ln++;
2756 }
2757 $ctx .= $rawlines[$ln - 1];
2758
2958 ($dstat, $dcond, $ln, $cnt, $off) = 2759 ($dstat, $dcond, $ln, $cnt, $off) =
2959 ctx_statement_block($linenr, $realcnt, 0); 2760 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2960 $ctx = $dstat;
2961 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 2761 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2962 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 2762 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2963 2763
2964 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; 2764 # Extract the remainder of the define (if any) and
2765 # rip off surrounding spaces, and trailing \'s.
2766 $rest = '';
2767 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2768 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2769 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2770 $rest .= substr($lines[$ln - 1], $off) . "\n";
2771 $cnt--;
2772 }
2773 $ln++;
2774 $off = 0;
2775 }
2776 $rest =~ s/\\\n.//g;
2777 $rest =~ s/^\s*//s;
2778 $rest =~ s/\s*$//s;
2779
2780 # Clean up the original statement.
2781 if ($args) {
2782 substr($dstat, 0, length($dcond), '');
2783 } else {
2784 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2785 }
2965 $dstat =~ s/$;//g; 2786 $dstat =~ s/$;//g;
2966 $dstat =~ s/\\\n.//g; 2787 $dstat =~ s/\\\n.//g;
2967 $dstat =~ s/^\s*//s; 2788 $dstat =~ s/^\s*//s;
@@ -2970,20 +2791,14 @@ sub process {
2970 # Flatten any parentheses and braces 2791 # Flatten any parentheses and braces
2971 while ($dstat =~ s/\([^\(\)]*\)/1/ || 2792 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2972 $dstat =~ s/\{[^\{\}]*\}/1/ || 2793 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2973 $dstat =~ s/\[[^\[\]]*\]/1/) 2794 $dstat =~ s/\[[^\{\}]*\]/1/)
2974 {
2975 }
2976
2977 # Flatten any obvious string concatentation.
2978 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
2979 $dstat =~ s/$Ident\s*("X*")/$1/)
2980 { 2795 {
2981 } 2796 }
2982 2797
2983 my $exceptions = qr{ 2798 my $exceptions = qr{
2984 $Declare| 2799 $Declare|
2985 module_param_named| 2800 module_param_named|
2986 MODULE_PARM_DESC| 2801 MODULE_PARAM_DESC|
2987 DECLARE_PER_CPU| 2802 DECLARE_PER_CPU|
2988 DEFINE_PER_CPU| 2803 DEFINE_PER_CPU|
2989 __typeof__\(| 2804 __typeof__\(|
@@ -2993,84 +2808,23 @@ sub process {
2993 ^\"|\"$ 2808 ^\"|\"$
2994 }x; 2809 }x;
2995 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 2810 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2996 if ($dstat ne '' && 2811 if ($rest ne '' && $rest ne ',') {
2997 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 2812 if ($rest !~ /while\s*\(/ &&
2998 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 2813 $dstat !~ /$exceptions/)
2999 $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo 2814 {
3000 $dstat !~ /^'X'$/ && # character constants
3001 $dstat !~ /$exceptions/ &&
3002 $dstat !~ /^\.$Ident\s*=/ && # .foo =
3003 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
3004 $dstat !~ /^for\s*$Constant$/ && # for (...)
3005 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3006 $dstat !~ /^do\s*{/ && # do {...
3007 $dstat !~ /^\({/) # ({...
3008 {
3009 $ctx =~ s/\n*$//;
3010 my $herectx = $here . "\n";
3011 my $cnt = statement_rawlines($ctx);
3012
3013 for (my $n = 0; $n < $cnt; $n++) {
3014 $herectx .= raw_line($linenr, $n) . "\n";
3015 }
3016
3017 if ($dstat =~ /;/) {
3018 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 2815 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3019 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 2816 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
3020 } else {
3021 ERROR("COMPLEX_MACRO",
3022 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3023 } 2817 }
3024 }
3025
3026# check for line continuations outside of #defines, preprocessor #, and asm
3027
3028 } else {
3029 if ($prevline !~ /^..*\\$/ &&
3030 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3031 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
3032 $line =~ /^\+.*\\$/) {
3033 WARN("LINE_CONTINUATIONS",
3034 "Avoid unnecessary line continuations\n" . $herecurr);
3035 }
3036 }
3037
3038# do {} while (0) macro tests:
3039# single-statement macros do not need to be enclosed in do while (0) loop,
3040# macro should not end with a semicolon
3041 if ($^V && $^V ge 5.10.0 &&
3042 $realfile !~ m@/vmlinux.lds.h$@ &&
3043 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3044 my $ln = $linenr;
3045 my $cnt = $realcnt;
3046 my ($off, $dstat, $dcond, $rest);
3047 my $ctx = '';
3048 ($dstat, $dcond, $ln, $cnt, $off) =
3049 ctx_statement_block($linenr, $realcnt, 0);
3050 $ctx = $dstat;
3051 2818
3052 $dstat =~ s/\\\n.//g; 2819 } elsif ($ctx !~ /;/) {
3053 2820 if ($dstat ne '' &&
3054 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 2821 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
3055 my $stmts = $2; 2822 $dstat !~ /$exceptions/ &&
3056 my $semis = $3; 2823 $dstat !~ /^\.$Ident\s*=/ &&
3057 2824 $dstat =~ /$Operators/)
3058 $ctx =~ s/\n*$//; 2825 {
3059 my $cnt = statement_rawlines($ctx); 2826 ERROR("COMPLEX_MACRO",
3060 my $herectx = $here . "\n"; 2827 "Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
3061
3062 for (my $n = 0; $n < $cnt; $n++) {
3063 $herectx .= raw_line($linenr, $n) . "\n";
3064 }
3065
3066 if (($stmts =~ tr/;/;/) == 1 &&
3067 $stmts !~ /^\s*(if|while|for|switch)\b/) {
3068 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3069 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3070 }
3071 if (defined $semis && $semis ne "") {
3072 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3073 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3074 } 2828 }
3075 } 2829 }
3076 } 2830 }
@@ -3092,8 +2846,7 @@ sub process {
3092 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 2846 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
3093 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 2847 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3094 if ($#chunks > 0 && $level == 0) { 2848 if ($#chunks > 0 && $level == 0) {
3095 my @allowed = (); 2849 my $allowed = 0;
3096 my $allow = 0;
3097 my $seen = 0; 2850 my $seen = 0;
3098 my $herectx = $here . "\n"; 2851 my $herectx = $here . "\n";
3099 my $ln = $linenr - 1; 2852 my $ln = $linenr - 1;
@@ -3104,7 +2857,6 @@ sub process {
3104 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 2857 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3105 my $offset = statement_rawlines($whitespace) - 1; 2858 my $offset = statement_rawlines($whitespace) - 1;
3106 2859
3107 $allowed[$allow] = 0;
3108 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 2860 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3109 2861
3110 # We have looked at and allowed this specific line. 2862 # We have looked at and allowed this specific line.
@@ -3117,34 +2869,23 @@ sub process {
3117 2869
3118 $seen++ if ($block =~ /^\s*{/); 2870 $seen++ if ($block =~ /^\s*{/);
3119 2871
3120 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 2872 #print "cond<$cond> block<$block> allowed<$allowed>\n";
3121 if (statement_lines($cond) > 1) { 2873 if (statement_lines($cond) > 1) {
3122 #print "APW: ALLOWED: cond<$cond>\n"; 2874 #print "APW: ALLOWED: cond<$cond>\n";
3123 $allowed[$allow] = 1; 2875 $allowed = 1;
3124 } 2876 }
3125 if ($block =~/\b(?:if|for|while)\b/) { 2877 if ($block =~/\b(?:if|for|while)\b/) {
3126 #print "APW: ALLOWED: block<$block>\n"; 2878 #print "APW: ALLOWED: block<$block>\n";
3127 $allowed[$allow] = 1; 2879 $allowed = 1;
3128 } 2880 }
3129 if (statement_block_size($block) > 1) { 2881 if (statement_block_size($block) > 1) {
3130 #print "APW: ALLOWED: lines block<$block>\n"; 2882 #print "APW: ALLOWED: lines block<$block>\n";
3131 $allowed[$allow] = 1; 2883 $allowed = 1;
3132 } 2884 }
3133 $allow++;
3134 } 2885 }
3135 if ($seen) { 2886 if ($seen && !$allowed) {
3136 my $sum_allowed = 0; 2887 WARN("BRACES",
3137 foreach (@allowed) { 2888 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3138 $sum_allowed += $_;
3139 }
3140 if ($sum_allowed == 0) {
3141 WARN("BRACES",
3142 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3143 } elsif ($sum_allowed != $allow &&
3144 $seen != $allow) {
3145 CHK("BRACES",
3146 "braces {} should be used on all arms of this statement\n" . $herectx);
3147 }
3148 } 2889 }
3149 } 2890 }
3150 } 2891 }
@@ -3191,11 +2932,11 @@ sub process {
3191 } 2932 }
3192 } 2933 }
3193 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 2934 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
3194 my $herectx = $here . "\n"; 2935 my $herectx = $here . "\n";;
3195 my $cnt = statement_rawlines($block); 2936 my $cnt = statement_rawlines($block);
3196 2937
3197 for (my $n = 0; $n < $cnt; $n++) { 2938 for (my $n = 0; $n < $cnt; $n++) {
3198 $herectx .= raw_line($linenr, $n) . "\n"; 2939 $herectx .= raw_line($linenr, $n) . "\n";;
3199 } 2940 }
3200 2941
3201 WARN("BRACES", 2942 WARN("BRACES",
@@ -3203,14 +2944,20 @@ sub process {
3203 } 2944 }
3204 } 2945 }
3205 2946
3206# check for unnecessary blank lines around braces 2947# don't include deprecated include files (uses RAW line)
3207 if (($line =~ /^..*}\s*$/ && $prevline =~ /^.\s*$/)) { 2948 for my $inc (@dep_includes) {
3208 CHK("BRACES", 2949 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
3209 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev); 2950 ERROR("DEPRECATED_INCLUDE",
2951 "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2952 }
3210 } 2953 }
3211 if (($line =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 2954
3212 CHK("BRACES", 2955# don't use deprecated functions
3213 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev); 2956 for my $func (@dep_functions) {
2957 if ($line =~ /\b$func\b/) {
2958 ERROR("DEPRECATED_FUNCTION",
2959 "Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2960 }
3214 } 2961 }
3215 2962
3216# no volatiles please 2963# no volatiles please
@@ -3227,12 +2974,20 @@ sub process {
3227 $herecurr); 2974 $herecurr);
3228 } 2975 }
3229 2976
3230# check for needless "if (<foo>) fn(<foo>)" uses 2977# check for needless kfree() checks
3231 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 2978 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3232 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;'; 2979 my $expr = $1;
3233 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) { 2980 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
3234 WARN('NEEDLESS_IF', 2981 WARN("NEEDLESS_KFREE",
3235 "$1(NULL) is safe this check is probably not required\n" . $hereprev); 2982 "kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2983 }
2984 }
2985# check for needless usb_free_urb() checks
2986 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2987 my $expr = $1;
2988 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2989 WARN("NEEDLESS_USB_FREE_URB",
2990 "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
3236 } 2991 }
3237 } 2992 }
3238 2993
@@ -3320,100 +3075,18 @@ sub process {
3320 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 3075 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
3321 } 3076 }
3322 3077
3323# Check for __attribute__ format(printf, prefer __printf
3324 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3325 WARN("PREFER_PRINTF",
3326 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3327 }
3328
3329# Check for __attribute__ format(scanf, prefer __scanf
3330 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3331 WARN("PREFER_SCANF",
3332 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3333 }
3334
3335# check for sizeof(&) 3078# check for sizeof(&)
3336 if ($line =~ /\bsizeof\s*\(\s*\&/) { 3079 if ($line =~ /\bsizeof\s*\(\s*\&/) {
3337 WARN("SIZEOF_ADDRESS", 3080 WARN("SIZEOF_ADDRESS",
3338 "sizeof(& should be avoided\n" . $herecurr); 3081 "sizeof(& should be avoided\n" . $herecurr);
3339 } 3082 }
3340 3083
3341# check for sizeof without parenthesis
3342 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3343 WARN("SIZEOF_PARENTHESIS",
3344 "sizeof $1 should be sizeof($1)\n" . $herecurr);
3345 }
3346
3347# check for line continuations in quoted strings with odd counts of " 3084# check for line continuations in quoted strings with odd counts of "
3348 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { 3085 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
3349 WARN("LINE_CONTINUATIONS", 3086 WARN("LINE_CONTINUATIONS",
3350 "Avoid line continuations in quoted strings\n" . $herecurr); 3087 "Avoid line continuations in quoted strings\n" . $herecurr);
3351 } 3088 }
3352 3089
3353# check for struct spinlock declarations
3354 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3355 WARN("USE_SPINLOCK_T",
3356 "struct spinlock should be spinlock_t\n" . $herecurr);
3357 }
3358
3359# Check for misused memsets
3360 if ($^V && $^V ge 5.10.0 &&
3361 defined $stat &&
3362 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3363
3364 my $ms_addr = $2;
3365 my $ms_val = $7;
3366 my $ms_size = $12;
3367
3368 if ($ms_size =~ /^(0x|)0$/i) {
3369 ERROR("MEMSET",
3370 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
3371 } elsif ($ms_size =~ /^(0x|)1$/i) {
3372 WARN("MEMSET",
3373 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3374 }
3375 }
3376
3377# typecasts on min/max could be min_t/max_t
3378 if ($^V && $^V ge 5.10.0 &&
3379 defined $stat &&
3380 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3381 if (defined $2 || defined $7) {
3382 my $call = $1;
3383 my $cast1 = deparenthesize($2);
3384 my $arg1 = $3;
3385 my $cast2 = deparenthesize($7);
3386 my $arg2 = $8;
3387 my $cast;
3388
3389 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
3390 $cast = "$cast1 or $cast2";
3391 } elsif ($cast1 ne "") {
3392 $cast = $cast1;
3393 } else {
3394 $cast = $cast2;
3395 }
3396 WARN("MINMAX",
3397 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
3398 }
3399 }
3400
3401# check usleep_range arguments
3402 if ($^V && $^V ge 5.10.0 &&
3403 defined $stat &&
3404 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3405 my $min = $1;
3406 my $max = $7;
3407 if ($min eq $max) {
3408 WARN("USLEEP_RANGE",
3409 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3410 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3411 $min > $max) {
3412 WARN("USLEEP_RANGE",
3413 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3414 }
3415 }
3416
3417# check for new externs in .c files. 3090# check for new externs in .c files.
3418 if ($realfile =~ /\.c$/ && defined $stat && 3091 if ($realfile =~ /\.c$/ && defined $stat &&
3419 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 3092 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
@@ -3462,22 +3135,8 @@ sub process {
3462 3135
3463# check for multiple semicolons 3136# check for multiple semicolons
3464 if ($line =~ /;\s*;\s*$/) { 3137 if ($line =~ /;\s*;\s*$/) {
3465 WARN("ONE_SEMICOLON", 3138 WARN("ONE_SEMICOLON",
3466 "Statements terminations use 1 semicolon\n" . $herecurr); 3139 "Statements terminations use 1 semicolon\n" . $herecurr);
3467 }
3468
3469# check for switch/default statements without a break;
3470 if ($^V && $^V ge 5.10.0 &&
3471 defined $stat &&
3472 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3473 my $ctx = '';
3474 my $herectx = $here . "\n";
3475 my $cnt = statement_rawlines($stat);
3476 for (my $n = 0; $n < $cnt; $n++) {
3477 $herectx .= raw_line($linenr, $n) . "\n";
3478 }
3479 WARN("DEFAULT_NO_BREAK",
3480 "switch default: should use break\n" . $herectx);
3481 } 3140 }
3482 3141
3483# check for gcc specific __FUNCTION__ 3142# check for gcc specific __FUNCTION__
@@ -3486,30 +3145,22 @@ sub process {
3486 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); 3145 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
3487 } 3146 }
3488 3147
3489# check for use of yield()
3490 if ($line =~ /\byield\s*\(\s*\)/) {
3491 WARN("YIELD",
3492 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3493 }
3494
3495# check for semaphores initialized locked 3148# check for semaphores initialized locked
3496 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 3149 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
3497 WARN("CONSIDER_COMPLETION", 3150 WARN("CONSIDER_COMPLETION",
3498 "consider using a completion\n" . $herecurr); 3151 "consider using a completion\n" . $herecurr);
3499 }
3500 3152
3501# recommend kstrto* over simple_strto* and strict_strto* 3153 }
3502 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 3154# recommend kstrto* over simple_strto*
3155 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
3503 WARN("CONSIDER_KSTRTO", 3156 WARN("CONSIDER_KSTRTO",
3504 "$1 is obsolete, use k$3 instead\n" . $herecurr); 3157 "consider using kstrto* in preference to simple_$1\n" . $herecurr);
3505 } 3158 }
3506
3507# check for __initcall(), use device_initcall() explicitly please 3159# check for __initcall(), use device_initcall() explicitly please
3508 if ($line =~ /^.\s*__initcall\s*\(/) { 3160 if ($line =~ /^.\s*__initcall\s*\(/) {
3509 WARN("USE_DEVICE_INITCALL", 3161 WARN("USE_DEVICE_INITCALL",
3510 "please use device_initcall() instead of __initcall()\n" . $herecurr); 3162 "please use device_initcall() instead of __initcall()\n" . $herecurr);
3511 } 3163 }
3512
3513# check for various ops structs, ensure they are const. 3164# check for various ops structs, ensure they are const.
3514 my $struct_ops = qr{acpi_dock_ops| 3165 my $struct_ops = qr{acpi_dock_ops|
3515 address_space_operations| 3166 address_space_operations|
@@ -3607,6 +3258,12 @@ sub process {
3607 WARN("EXPORTED_WORLD_WRITABLE", 3258 WARN("EXPORTED_WORLD_WRITABLE",
3608 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 3259 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
3609 } 3260 }
3261
3262 # Check for memset with swapped arguments
3263 if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) {
3264 ERROR("MEMSET",
3265 "memset size is 3rd argument, not the second.\n" . $herecurr);
3266 }
3610 } 3267 }
3611 3268
3612 # If we have no input at all, then there is nothing to report on 3269 # If we have no input at all, then there is nothing to report on
@@ -3646,12 +3303,6 @@ sub process {
3646 } 3303 }
3647 3304
3648 if ($quiet == 0) { 3305 if ($quiet == 0) {
3649
3650 if ($^V lt 5.10.0) {
3651 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
3652 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
3653 }
3654
3655 # If there were whitespace errors which cleanpatch can fix 3306 # If there were whitespace errors which cleanpatch can fix
3656 # then suggest that. 3307 # then suggest that.
3657 if ($rpt_cleaners) { 3308 if ($rpt_cleaners) {
@@ -3661,12 +3312,13 @@ sub process {
3661 } 3312 }
3662 } 3313 }
3663 3314
3664 if ($quiet == 0 && keys %ignore_type) { 3315 if (keys %ignore_type) {
3665 print "NOTE: Ignored message types:"; 3316 print "NOTE: Ignored message types:";
3666 foreach my $ignore (sort keys %ignore_type) { 3317 foreach my $ignore (sort keys %ignore_type) {
3667 print " $ignore"; 3318 print " $ignore";
3668 } 3319 }
3669 print "\n\n"; 3320 print "\n";
3321 print "\n" if ($quiet == 0);
3670 } 3322 }
3671 3323
3672 if ($clean == 1 && $quiet == 0) { 3324 if ($clean == 1 && $quiet == 0) {