diff options
Diffstat (limited to 'scripts/checkpatch.pl')
| -rwxr-xr-x | scripts/checkpatch.pl | 236 |
1 files changed, 138 insertions, 98 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 8fda3b3f7be8..e3bfcbe8a520 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
| @@ -227,7 +227,7 @@ our $Inline = qr{inline|__always_inline|noinline}; | |||
| 227 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; | 227 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
| 228 | our $Lval = qr{$Ident(?:$Member)*}; | 228 | our $Lval = qr{$Ident(?:$Member)*}; |
| 229 | 229 | ||
| 230 | our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; | 230 | our $Constant = qr{(?i:(?:[0-9]+|0x[0-9a-f]+)[ul]*)}; |
| 231 | our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; | 231 | our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; |
| 232 | our $Compare = qr{<=|>=|==|!=|<|>}; | 232 | our $Compare = qr{<=|>=|==|!=|<|>}; |
| 233 | our $Operators = qr{ | 233 | our $Operators = qr{ |
| @@ -315,7 +315,7 @@ sub build_types { | |||
| 315 | $NonptrType = qr{ | 315 | $NonptrType = qr{ |
| 316 | (?:$Modifier\s+|const\s+)* | 316 | (?:$Modifier\s+|const\s+)* |
| 317 | (?: | 317 | (?: |
| 318 | (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| | 318 | (?:typeof|__typeof__)\s*\([^\)]*\)| |
| 319 | (?:$typeTypedefs\b)| | 319 | (?:$typeTypedefs\b)| |
| 320 | (?:${all}\b) | 320 | (?:${all}\b) |
| 321 | ) | 321 | ) |
| @@ -334,6 +334,7 @@ our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/; | |||
| 334 | 334 | ||
| 335 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; | 335 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; |
| 336 | our $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*}; | 336 | our $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*}; |
| 337 | our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)}; | ||
| 337 | 338 | ||
| 338 | sub deparenthesize { | 339 | sub deparenthesize { |
| 339 | my ($string) = @_; | 340 | my ($string) = @_; |
| @@ -676,6 +677,10 @@ sub ctx_statement_block { | |||
| 676 | if ($off >= $len) { | 677 | if ($off >= $len) { |
| 677 | last; | 678 | last; |
| 678 | } | 679 | } |
| 680 | if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { | ||
| 681 | $level++; | ||
| 682 | $type = '#'; | ||
| 683 | } | ||
| 679 | } | 684 | } |
| 680 | $p = $c; | 685 | $p = $c; |
| 681 | $c = substr($blk, $off, 1); | 686 | $c = substr($blk, $off, 1); |
| @@ -738,6 +743,13 @@ sub ctx_statement_block { | |||
| 738 | last; | 743 | last; |
| 739 | } | 744 | } |
| 740 | } | 745 | } |
| 746 | # Preprocessor commands end at the newline unless escaped. | ||
| 747 | if ($type eq '#' && $c eq "\n" && $p ne "\\") { | ||
| 748 | $level--; | ||
| 749 | $type = ''; | ||
| 750 | $off++; | ||
| 751 | last; | ||
| 752 | } | ||
| 741 | $off++; | 753 | $off++; |
| 742 | } | 754 | } |
| 743 | # We are truly at the end, so shuffle to the next line. | 755 | # We are truly at the end, so shuffle to the next line. |
| @@ -1020,7 +1032,7 @@ sub annotate_values { | |||
| 1020 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { | 1032 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { |
| 1021 | print "CAST($1)\n" if ($dbg_values > 1); | 1033 | print "CAST($1)\n" if ($dbg_values > 1); |
| 1022 | push(@av_paren_type, $type); | 1034 | push(@av_paren_type, $type); |
| 1023 | $type = 'C'; | 1035 | $type = 'c'; |
| 1024 | 1036 | ||
| 1025 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { | 1037 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { |
| 1026 | print "DECLARE($1)\n" if ($dbg_values > 1); | 1038 | print "DECLARE($1)\n" if ($dbg_values > 1); |
| @@ -1212,7 +1224,9 @@ sub possible { | |||
| 1212 | case| | 1224 | case| |
| 1213 | else| | 1225 | else| |
| 1214 | asm|__asm__| | 1226 | asm|__asm__| |
| 1215 | do | 1227 | do| |
| 1228 | \#| | ||
| 1229 | \#\#| | ||
| 1216 | )(?:\s|$)| | 1230 | )(?:\s|$)| |
| 1217 | ^(?:typedef|struct|enum)\b | 1231 | ^(?:typedef|struct|enum)\b |
| 1218 | )}x; | 1232 | )}x; |
| @@ -1359,6 +1373,7 @@ sub process { | |||
| 1359 | my %suppress_ifbraces; | 1373 | my %suppress_ifbraces; |
| 1360 | my %suppress_whiletrailers; | 1374 | my %suppress_whiletrailers; |
| 1361 | my %suppress_export; | 1375 | my %suppress_export; |
| 1376 | my $suppress_statement = 0; | ||
| 1362 | 1377 | ||
| 1363 | # Pre-scan the patch sanitizing the lines. | 1378 | # Pre-scan the patch sanitizing the lines. |
| 1364 | # Pre-scan the patch looking for any __setup documentation. | 1379 | # Pre-scan the patch looking for any __setup documentation. |
| @@ -1468,6 +1483,7 @@ sub process { | |||
| 1468 | %suppress_ifbraces = (); | 1483 | %suppress_ifbraces = (); |
| 1469 | %suppress_whiletrailers = (); | 1484 | %suppress_whiletrailers = (); |
| 1470 | %suppress_export = (); | 1485 | %suppress_export = (); |
| 1486 | $suppress_statement = 0; | ||
| 1471 | next; | 1487 | next; |
| 1472 | 1488 | ||
| 1473 | # track the line number as we move through the hunk, note that | 1489 | # track the line number as we move through the hunk, note that |
| @@ -1504,9 +1520,11 @@ sub process { | |||
| 1504 | if ($line =~ /^diff --git.*?(\S+)$/) { | 1520 | if ($line =~ /^diff --git.*?(\S+)$/) { |
| 1505 | $realfile = $1; | 1521 | $realfile = $1; |
| 1506 | $realfile =~ s@^([^/]*)/@@; | 1522 | $realfile =~ s@^([^/]*)/@@; |
| 1523 | $in_commit_log = 0; | ||
| 1507 | } elsif ($line =~ /^\+\+\+\s+(\S+)/) { | 1524 | } elsif ($line =~ /^\+\+\+\s+(\S+)/) { |
| 1508 | $realfile = $1; | 1525 | $realfile = $1; |
| 1509 | $realfile =~ s@^([^/]*)/@@; | 1526 | $realfile =~ s@^([^/]*)/@@; |
| 1527 | $in_commit_log = 0; | ||
| 1510 | 1528 | ||
| 1511 | $p1_prefix = $1; | 1529 | $p1_prefix = $1; |
| 1512 | if (!$file && $tree && $p1_prefix ne '' && | 1530 | if (!$file && $tree && $p1_prefix ne '' && |
| @@ -1546,7 +1564,8 @@ sub process { | |||
| 1546 | } | 1564 | } |
| 1547 | 1565 | ||
| 1548 | # Check signature styles | 1566 | # Check signature styles |
| 1549 | if ($line =~ /^(\s*)($signature_tags)(\s*)(.*)/) { | 1567 | if (!$in_header_lines && |
| 1568 | $line =~ /^(\s*)($signature_tags)(\s*)(.*)/) { | ||
| 1550 | my $space_before = $1; | 1569 | my $space_before = $1; |
| 1551 | my $sign_off = $2; | 1570 | my $sign_off = $2; |
| 1552 | my $space_after = $3; | 1571 | my $space_after = $3; |
| @@ -1623,7 +1642,7 @@ sub process { | |||
| 1623 | # Check if it's the start of a commit log | 1642 | # Check if it's the start of a commit log |
| 1624 | # (not a header line and we haven't seen the patch filename) | 1643 | # (not a header line and we haven't seen the patch filename) |
| 1625 | if ($in_header_lines && $realfile =~ /^$/ && | 1644 | if ($in_header_lines && $realfile =~ /^$/ && |
| 1626 | $rawline !~ /^(commit\b|from\b|\w+:).+$/i) { | 1645 | $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) { |
| 1627 | $in_header_lines = 0; | 1646 | $in_header_lines = 0; |
| 1628 | $in_commit_log = 1; | 1647 | $in_commit_log = 1; |
| 1629 | } | 1648 | } |
| @@ -1655,19 +1674,26 @@ sub process { | |||
| 1655 | # Only applies when adding the entry originally, after that we do not have | 1674 | # Only applies when adding the entry originally, after that we do not have |
| 1656 | # sufficient context to determine whether it is indeed long enough. | 1675 | # sufficient context to determine whether it is indeed long enough. |
| 1657 | if ($realfile =~ /Kconfig/ && | 1676 | if ($realfile =~ /Kconfig/ && |
| 1658 | $line =~ /\+\s*(?:---)?help(?:---)?$/) { | 1677 | $line =~ /.\s*config\s+/) { |
| 1659 | my $length = 0; | 1678 | my $length = 0; |
| 1660 | my $cnt = $realcnt; | 1679 | my $cnt = $realcnt; |
| 1661 | my $ln = $linenr + 1; | 1680 | my $ln = $linenr + 1; |
| 1662 | my $f; | 1681 | my $f; |
| 1682 | my $is_start = 0; | ||
| 1663 | my $is_end = 0; | 1683 | my $is_end = 0; |
| 1664 | while ($cnt > 0 && defined $lines[$ln - 1]) { | 1684 | for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { |
| 1665 | $f = $lines[$ln - 1]; | 1685 | $f = $lines[$ln - 1]; |
| 1666 | $cnt-- if ($lines[$ln - 1] !~ /^-/); | 1686 | $cnt-- if ($lines[$ln - 1] !~ /^-/); |
| 1667 | $is_end = $lines[$ln - 1] =~ /^\+/; | 1687 | $is_end = $lines[$ln - 1] =~ /^\+/; |
| 1668 | $ln++; | ||
| 1669 | 1688 | ||
| 1670 | next if ($f =~ /^-/); | 1689 | next if ($f =~ /^-/); |
| 1690 | |||
| 1691 | if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) { | ||
| 1692 | $is_start = 1; | ||
| 1693 | } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) { | ||
| 1694 | $length = -1; | ||
| 1695 | } | ||
| 1696 | |||
| 1671 | $f =~ s/^.//; | 1697 | $f =~ s/^.//; |
| 1672 | $f =~ s/#.*//; | 1698 | $f =~ s/#.*//; |
| 1673 | $f =~ s/^\s+//; | 1699 | $f =~ s/^\s+//; |
| @@ -1679,8 +1705,8 @@ sub process { | |||
| 1679 | $length++; | 1705 | $length++; |
| 1680 | } | 1706 | } |
| 1681 | WARN("CONFIG_DESCRIPTION", | 1707 | WARN("CONFIG_DESCRIPTION", |
| 1682 | "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_end && $length < 4); | 1708 | "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4); |
| 1683 | #print "is_end<$is_end> length<$length>\n"; | 1709 | #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; |
| 1684 | } | 1710 | } |
| 1685 | 1711 | ||
| 1686 | if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && | 1712 | if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && |
| @@ -1792,12 +1818,24 @@ sub process { | |||
| 1792 | # Check for potential 'bare' types | 1818 | # Check for potential 'bare' types |
| 1793 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, | 1819 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, |
| 1794 | $realline_next); | 1820 | $realline_next); |
| 1795 | if ($realcnt && $line =~ /.\s*\S/) { | 1821 | #print "LINE<$line>\n"; |
| 1822 | if ($linenr >= $suppress_statement && | ||
| 1823 | $realcnt && $line =~ /.\s*\S/) { | ||
| 1796 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = | 1824 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = |
| 1797 | ctx_statement_block($linenr, $realcnt, 0); | 1825 | ctx_statement_block($linenr, $realcnt, 0); |
| 1798 | $stat =~ s/\n./\n /g; | 1826 | $stat =~ s/\n./\n /g; |
| 1799 | $cond =~ s/\n./\n /g; | 1827 | $cond =~ s/\n./\n /g; |
| 1800 | 1828 | ||
| 1829 | #print "linenr<$linenr> <$stat>\n"; | ||
| 1830 | # If this statement has no statement boundaries within | ||
| 1831 | # it there is no point in retrying a statement scan | ||
| 1832 | # until we hit end of it. | ||
| 1833 | my $frag = $stat; $frag =~ s/;+\s*$//; | ||
| 1834 | if ($frag !~ /(?:{|;)/) { | ||
| 1835 | #print "skip<$line_nr_next>\n"; | ||
| 1836 | $suppress_statement = $line_nr_next; | ||
| 1837 | } | ||
| 1838 | |||
| 1801 | # Find the real next line. | 1839 | # Find the real next line. |
| 1802 | $realline_next = $line_nr_next; | 1840 | $realline_next = $line_nr_next; |
| 1803 | if (defined $realline_next && | 1841 | if (defined $realline_next && |
| @@ -1923,6 +1961,9 @@ sub process { | |||
| 1923 | 1961 | ||
| 1924 | # Check relative indent for conditionals and blocks. | 1962 | # Check relative indent for conditionals and blocks. |
| 1925 | if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { | 1963 | if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { |
| 1964 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = | ||
| 1965 | ctx_statement_block($linenr, $realcnt, 0) | ||
| 1966 | if (!defined $stat); | ||
| 1926 | my ($s, $c) = ($stat, $cond); | 1967 | my ($s, $c) = ($stat, $cond); |
| 1927 | 1968 | ||
| 1928 | substr($s, 0, length($c), ''); | 1969 | substr($s, 0, length($c), ''); |
| @@ -2090,7 +2131,7 @@ sub process { | |||
| 2090 | # XXX(foo); | 2131 | # XXX(foo); |
| 2091 | # EXPORT_SYMBOL(something_foo); | 2132 | # EXPORT_SYMBOL(something_foo); |
| 2092 | my $name = $1; | 2133 | my $name = $1; |
| 2093 | if ($stat =~ /^.([A-Z_]+)\s*\(\s*($Ident)/ && | 2134 | if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && |
| 2094 | $name =~ /^${Ident}_$2/) { | 2135 | $name =~ /^${Ident}_$2/) { |
| 2095 | #print "FOO C name<$name>\n"; | 2136 | #print "FOO C name<$name>\n"; |
| 2096 | $suppress_export{$realline_next} = 1; | 2137 | $suppress_export{$realline_next} = 1; |
| @@ -2168,8 +2209,9 @@ sub process { | |||
| 2168 | 2209 | ||
| 2169 | # * goes on variable not on type | 2210 | # * goes on variable not on type |
| 2170 | # (char*[ const]) | 2211 | # (char*[ const]) |
| 2171 | if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) { | 2212 | while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { |
| 2172 | my ($from, $to) = ($1, $1); | 2213 | #print "AA<$1>\n"; |
| 2214 | my ($from, $to) = ($2, $2); | ||
| 2173 | 2215 | ||
| 2174 | # Should start with a space. | 2216 | # Should start with a space. |
| 2175 | $to =~ s/^(\S)/ $1/; | 2217 | $to =~ s/^(\S)/ $1/; |
| @@ -2184,8 +2226,10 @@ sub process { | |||
| 2184 | ERROR("POINTER_LOCATION", | 2226 | ERROR("POINTER_LOCATION", |
| 2185 | "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); | 2227 | "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); |
| 2186 | } | 2228 | } |
| 2187 | } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) { | 2229 | } |
| 2188 | my ($from, $to, $ident) = ($1, $1, $2); | 2230 | while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { |
| 2231 | #print "BB<$1>\n"; | ||
| 2232 | my ($from, $to, $ident) = ($2, $2, $3); | ||
| 2189 | 2233 | ||
| 2190 | # Should start with a space. | 2234 | # Should start with a space. |
| 2191 | $to =~ s/^(\S)/ $1/; | 2235 | $to =~ s/^(\S)/ $1/; |
| @@ -2568,7 +2612,7 @@ sub process { | |||
| 2568 | # Flatten any parentheses | 2612 | # Flatten any parentheses |
| 2569 | $value =~ s/\(/ \(/g; | 2613 | $value =~ s/\(/ \(/g; |
| 2570 | $value =~ s/\)/\) /g; | 2614 | $value =~ s/\)/\) /g; |
| 2571 | while ($value =~ s/\[[^\{\}]*\]/1/ || | 2615 | while ($value =~ s/\[[^\[\]]*\]/1/ || |
| 2572 | $value !~ /(?:$Ident|-?$Constant)\s* | 2616 | $value !~ /(?:$Ident|-?$Constant)\s* |
| 2573 | $Compare\s* | 2617 | $Compare\s* |
| 2574 | (?:$Ident|-?$Constant)/x && | 2618 | (?:$Ident|-?$Constant)/x && |
| @@ -2593,28 +2637,6 @@ sub process { | |||
| 2593 | } | 2637 | } |
| 2594 | } | 2638 | } |
| 2595 | 2639 | ||
| 2596 | # typecasts on min/max could be min_t/max_t | ||
| 2597 | if ($line =~ /^\+(?:.*?)\b(min|max)\s*\($Typecast{0,1}($LvalOrFunc)\s*,\s*$Typecast{0,1}($LvalOrFunc)\s*\)/) { | ||
| 2598 | if (defined $2 || defined $8) { | ||
| 2599 | my $call = $1; | ||
| 2600 | my $cast1 = deparenthesize($2); | ||
| 2601 | my $arg1 = $3; | ||
| 2602 | my $cast2 = deparenthesize($8); | ||
| 2603 | my $arg2 = $9; | ||
| 2604 | my $cast; | ||
| 2605 | |||
| 2606 | if ($cast1 ne "" && $cast2 ne "") { | ||
| 2607 | $cast = "$cast1 or $cast2"; | ||
| 2608 | } elsif ($cast1 ne "") { | ||
| 2609 | $cast = $cast1; | ||
| 2610 | } else { | ||
| 2611 | $cast = $cast2; | ||
| 2612 | } | ||
| 2613 | WARN("MINMAX", | ||
| 2614 | "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . $herecurr); | ||
| 2615 | } | ||
| 2616 | } | ||
| 2617 | |||
| 2618 | # Need a space before open parenthesis after if, while etc | 2640 | # Need a space before open parenthesis after if, while etc |
| 2619 | if ($line=~/\b(if|while|for|switch)\(/) { | 2641 | if ($line=~/\b(if|while|for|switch)\(/) { |
| 2620 | ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr); | 2642 | ERROR("SPACING", "space required before the open parenthesis '('\n" . $herecurr); |
| @@ -2623,6 +2645,9 @@ sub process { | |||
| 2623 | # Check for illegal assignment in if conditional -- and check for trailing | 2645 | # Check for illegal assignment in if conditional -- and check for trailing |
| 2624 | # statements after the conditional. | 2646 | # statements after the conditional. |
| 2625 | if ($line =~ /do\s*(?!{)/) { | 2647 | if ($line =~ /do\s*(?!{)/) { |
| 2648 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = | ||
| 2649 | ctx_statement_block($linenr, $realcnt, 0) | ||
| 2650 | if (!defined $stat); | ||
| 2626 | my ($stat_next) = ctx_statement_block($line_nr_next, | 2651 | my ($stat_next) = ctx_statement_block($line_nr_next, |
| 2627 | $remain_next, $off_next); | 2652 | $remain_next, $off_next); |
| 2628 | $stat_next =~ s/\n./\n /g; | 2653 | $stat_next =~ s/\n./\n /g; |
| @@ -2778,47 +2803,13 @@ sub process { | |||
| 2778 | my $cnt = $realcnt; | 2803 | my $cnt = $realcnt; |
| 2779 | my ($off, $dstat, $dcond, $rest); | 2804 | my ($off, $dstat, $dcond, $rest); |
| 2780 | my $ctx = ''; | 2805 | my $ctx = ''; |
| 2781 | |||
| 2782 | my $args = defined($1); | ||
| 2783 | |||
| 2784 | # Find the end of the macro and limit our statement | ||
| 2785 | # search to that. | ||
| 2786 | while ($cnt > 0 && defined $lines[$ln - 1] && | ||
| 2787 | $lines[$ln - 1] =~ /^(?:-|..*\\$)/) | ||
| 2788 | { | ||
| 2789 | $ctx .= $rawlines[$ln - 1] . "\n"; | ||
| 2790 | $cnt-- if ($lines[$ln - 1] !~ /^-/); | ||
| 2791 | $ln++; | ||
| 2792 | } | ||
| 2793 | $ctx .= $rawlines[$ln - 1]; | ||
| 2794 | |||
| 2795 | ($dstat, $dcond, $ln, $cnt, $off) = | 2806 | ($dstat, $dcond, $ln, $cnt, $off) = |
| 2796 | ctx_statement_block($linenr, $ln - $linenr + 1, 0); | 2807 | ctx_statement_block($linenr, $realcnt, 0); |
| 2808 | $ctx = $dstat; | ||
| 2797 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; | 2809 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; |
| 2798 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; | 2810 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; |
| 2799 | 2811 | ||
| 2800 | # Extract the remainder of the define (if any) and | 2812 | $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; |
| 2801 | # rip off surrounding spaces, and trailing \'s. | ||
| 2802 | $rest = ''; | ||
| 2803 | while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) { | ||
| 2804 | #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n"; | ||
| 2805 | if ($off != 0 || $lines[$ln - 1] !~ /^-/) { | ||
| 2806 | $rest .= substr($lines[$ln - 1], $off) . "\n"; | ||
| 2807 | $cnt--; | ||
| 2808 | } | ||
| 2809 | $ln++; | ||
| 2810 | $off = 0; | ||
| 2811 | } | ||
| 2812 | $rest =~ s/\\\n.//g; | ||
| 2813 | $rest =~ s/^\s*//s; | ||
| 2814 | $rest =~ s/\s*$//s; | ||
| 2815 | |||
| 2816 | # Clean up the original statement. | ||
| 2817 | if ($args) { | ||
| 2818 | substr($dstat, 0, length($dcond), ''); | ||
| 2819 | } else { | ||
| 2820 | $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//; | ||
| 2821 | } | ||
| 2822 | $dstat =~ s/$;//g; | 2813 | $dstat =~ s/$;//g; |
| 2823 | $dstat =~ s/\\\n.//g; | 2814 | $dstat =~ s/\\\n.//g; |
| 2824 | $dstat =~ s/^\s*//s; | 2815 | $dstat =~ s/^\s*//s; |
| @@ -2827,7 +2818,7 @@ sub process { | |||
| 2827 | # Flatten any parentheses and braces | 2818 | # Flatten any parentheses and braces |
| 2828 | while ($dstat =~ s/\([^\(\)]*\)/1/ || | 2819 | while ($dstat =~ s/\([^\(\)]*\)/1/ || |
| 2829 | $dstat =~ s/\{[^\{\}]*\}/1/ || | 2820 | $dstat =~ s/\{[^\{\}]*\}/1/ || |
| 2830 | $dstat =~ s/\[[^\{\}]*\]/1/) | 2821 | $dstat =~ s/\[[^\[\]]*\]/1/) |
| 2831 | { | 2822 | { |
| 2832 | } | 2823 | } |
| 2833 | 2824 | ||
| @@ -2844,23 +2835,32 @@ sub process { | |||
| 2844 | ^\"|\"$ | 2835 | ^\"|\"$ |
| 2845 | }x; | 2836 | }x; |
| 2846 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; | 2837 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; |
| 2847 | if ($rest ne '' && $rest ne ',') { | 2838 | if ($dstat ne '' && |
| 2848 | if ($rest !~ /while\s*\(/ && | 2839 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), |
| 2849 | $dstat !~ /$exceptions/) | 2840 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); |
| 2850 | { | 2841 | $dstat !~ /^(?:$Ident|-?$Constant)$/ && # 10 // foo() |
| 2851 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", | 2842 | $dstat !~ /$exceptions/ && |
| 2852 | "Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); | 2843 | $dstat !~ /^\.$Ident\s*=/ && # .foo = |
| 2844 | $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) | ||
| 2845 | $dstat !~ /^for\s*$Constant$/ && # for (...) | ||
| 2846 | $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() | ||
| 2847 | $dstat !~ /^do\s*{/ && # do {... | ||
| 2848 | $dstat !~ /^\({/) # ({... | ||
| 2849 | { | ||
| 2850 | $ctx =~ s/\n*$//; | ||
| 2851 | my $herectx = $here . "\n"; | ||
| 2852 | my $cnt = statement_rawlines($ctx); | ||
| 2853 | |||
| 2854 | for (my $n = 0; $n < $cnt; $n++) { | ||
| 2855 | $herectx .= raw_line($linenr, $n) . "\n"; | ||
| 2853 | } | 2856 | } |
| 2854 | 2857 | ||
| 2855 | } elsif ($ctx !~ /;/) { | 2858 | if ($dstat =~ /;/) { |
| 2856 | if ($dstat ne '' && | 2859 | ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", |
| 2857 | $dstat !~ /^(?:$Ident|-?$Constant)$/ && | 2860 | "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); |
| 2858 | $dstat !~ /$exceptions/ && | 2861 | } else { |
| 2859 | $dstat !~ /^\.$Ident\s*=/ && | ||
| 2860 | $dstat =~ /$Operators/) | ||
| 2861 | { | ||
| 2862 | ERROR("COMPLEX_MACRO", | 2862 | ERROR("COMPLEX_MACRO", |
| 2863 | "Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); | 2863 | "Macros with complex values should be enclosed in parenthesis\n" . "$herectx"); |
| 2864 | } | 2864 | } |
| 2865 | } | 2865 | } |
| 2866 | } | 2866 | } |
| @@ -3111,6 +3111,12 @@ sub process { | |||
| 3111 | "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); | 3111 | "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); |
| 3112 | } | 3112 | } |
| 3113 | 3113 | ||
| 3114 | # Check for __attribute__ format(printf, prefer __printf | ||
| 3115 | if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { | ||
| 3116 | WARN("PREFER_PRINTF", | ||
| 3117 | "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr); | ||
| 3118 | } | ||
| 3119 | |||
| 3114 | # check for sizeof(&) | 3120 | # check for sizeof(&) |
| 3115 | if ($line =~ /\bsizeof\s*\(\s*\&/) { | 3121 | if ($line =~ /\bsizeof\s*\(\s*\&/) { |
| 3116 | WARN("SIZEOF_ADDRESS", | 3122 | WARN("SIZEOF_ADDRESS", |
| @@ -3123,6 +3129,46 @@ sub process { | |||
| 3123 | "Avoid line continuations in quoted strings\n" . $herecurr); | 3129 | "Avoid line continuations in quoted strings\n" . $herecurr); |
| 3124 | } | 3130 | } |
| 3125 | 3131 | ||
| 3132 | # Check for misused memsets | ||
| 3133 | if (defined $stat && | ||
| 3134 | $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { | ||
| 3135 | |||
| 3136 | my $ms_addr = $2; | ||
| 3137 | my $ms_val = $8; | ||
| 3138 | my $ms_size = $14; | ||
| 3139 | |||
| 3140 | if ($ms_size =~ /^(0x|)0$/i) { | ||
| 3141 | ERROR("MEMSET", | ||
| 3142 | "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); | ||
| 3143 | } elsif ($ms_size =~ /^(0x|)1$/i) { | ||
| 3144 | WARN("MEMSET", | ||
| 3145 | "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); | ||
| 3146 | } | ||
| 3147 | } | ||
| 3148 | |||
| 3149 | # typecasts on min/max could be min_t/max_t | ||
| 3150 | if (defined $stat && | ||
| 3151 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { | ||
| 3152 | if (defined $2 || defined $8) { | ||
| 3153 | my $call = $1; | ||
| 3154 | my $cast1 = deparenthesize($2); | ||
| 3155 | my $arg1 = $3; | ||
| 3156 | my $cast2 = deparenthesize($8); | ||
| 3157 | my $arg2 = $9; | ||
| 3158 | my $cast; | ||
| 3159 | |||
| 3160 | if ($cast1 ne "" && $cast2 ne "") { | ||
| 3161 | $cast = "$cast1 or $cast2"; | ||
| 3162 | } elsif ($cast1 ne "") { | ||
| 3163 | $cast = $cast1; | ||
| 3164 | } else { | ||
| 3165 | $cast = $cast2; | ||
| 3166 | } | ||
| 3167 | WARN("MINMAX", | ||
| 3168 | "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); | ||
| 3169 | } | ||
| 3170 | } | ||
| 3171 | |||
| 3126 | # check for new externs in .c files. | 3172 | # check for new externs in .c files. |
| 3127 | if ($realfile =~ /\.c$/ && defined $stat && | 3173 | if ($realfile =~ /\.c$/ && defined $stat && |
| 3128 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) | 3174 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) |
| @@ -3294,12 +3340,6 @@ sub process { | |||
| 3294 | WARN("EXPORTED_WORLD_WRITABLE", | 3340 | WARN("EXPORTED_WORLD_WRITABLE", |
| 3295 | "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); | 3341 | "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); |
| 3296 | } | 3342 | } |
| 3297 | |||
| 3298 | # Check for memset with swapped arguments | ||
| 3299 | if ($line =~ /memset.*\,(\ |)(0x|)0(\ |0|)\);/) { | ||
| 3300 | ERROR("MEMSET", | ||
| 3301 | "memset size is 3rd argument, not the second.\n" . $herecurr); | ||
| 3302 | } | ||
| 3303 | } | 3343 | } |
| 3304 | 3344 | ||
| 3305 | # If we have no input at all, then there is nothing to report on | 3345 | # If we have no input at all, then there is nothing to report on |
