aboutsummaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorAndy Whitcroft <apw@shadowen.org>2008-07-24 00:29:03 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2008-07-24 13:47:26 -0400
commitf5fe35dd95549b1b419cdeb2ec3fe61fda94fa93 (patch)
treedbb0d8a8951f5b376900e0c26a392d9ccaafef27 /scripts
parent53210168feeff9a3c780bd42f69936d4c12381d5 (diff)
checkpatch: condition/loop indent checks
Check to see if the block/statement which a condition or loop introduces is indented correctly. Signed-off-by: Andy Whitcroft <apw@shadowen.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/checkpatch.pl59
1 files changed, 55 insertions, 4 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 8616baee0aeb..13d7a330b5dd 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1167,10 +1167,10 @@ sub process {
1167 } 1167 }
1168 1168
1169# Check for potential 'bare' types 1169# Check for potential 'bare' types
1170 my ($stat, $cond); 1170 my ($stat, $cond, $line_nr_next, $remain_next);
1171 if ($realcnt && $line =~ /.\s*\S/) { 1171 if ($realcnt && $line =~ /.\s*\S/) {
1172 ($stat, $cond) = ctx_statement_block($linenr, 1172 ($stat, $cond, $line_nr_next, $remain_next) =
1173 $realcnt, 0); 1173 ctx_statement_block($linenr, $realcnt, 0);
1174 $stat =~ s/\n./\n /g; 1174 $stat =~ s/\n./\n /g;
1175 $cond =~ s/\n./\n /g; 1175 $cond =~ s/\n./\n /g;
1176 1176
@@ -1712,7 +1712,8 @@ sub process {
1712 ERROR("space required before the open parenthesis '('\n" . $herecurr); 1712 ERROR("space required before the open parenthesis '('\n" . $herecurr);
1713 } 1713 }
1714 1714
1715# Check for illegal assignment in if conditional. 1715# Check for illegal assignment in if conditional -- and check for trailing
1716# statements after the conditional.
1716 if ($line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 1717 if ($line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
1717 my ($s, $c) = ($stat, $cond); 1718 my ($s, $c) = ($stat, $cond);
1718 1719
@@ -1732,6 +1733,56 @@ sub process {
1732 } 1733 }
1733 } 1734 }
1734 1735
1736# Check relative indent for conditionals and blocks.
1737 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1738 my ($s, $c) = ($stat, $cond);
1739
1740 substr($s, 0, length($c), '');
1741
1742 # Make sure we remove the line prefixes as we have
1743 # none on the first line, and are going to readd them
1744 # where necessary.
1745 $s =~ s/\n./\n/gs;
1746
1747 # We want to check the first line inside the block
1748 # starting at the end of the conditional, so remove:
1749 # 1) any blank line termination
1750 # 2) any opening brace { on end of the line
1751 # 3) any do (...) {
1752 my $continuation = 0;
1753 my $check = 0;
1754 $s =~ s/^.*\bdo\b//;
1755 $s =~ s/^\s*{//;
1756 if ($s =~ s/^\s*\\//) {
1757 $continuation = 1;
1758 }
1759 if ($s =~ s/^\s*\n//) {
1760 $check = 1;
1761 }
1762
1763 # Also ignore a loop construct at the end of a
1764 # preprocessor statement.
1765 if (($prevline =~ /^.\s*#\s*define\s/ ||
1766 $prevline =~ /\\\s*$/) && $continuation == 0) {
1767 $check = 0;
1768 }
1769
1770 # Ignore the current line if its is a preprocessor
1771 # line.
1772 if ($s =~ /^\s*#\s*/) {
1773 $check = 0;
1774 }
1775
1776 my (undef, $sindent) = line_stats("+" . $s);
1777
1778 ##print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s>\n";
1779
1780 if ($check && (($sindent % 8) != 0 ||
1781 ($sindent <= $indent && $s ne ''))) {
1782 WARN("suspect code indent for conditional statements\n" . $herecurr);
1783 }
1784 }
1785
1735# Check for bitwise tests written as boolean 1786# Check for bitwise tests written as boolean
1736 if ($line =~ / 1787 if ($line =~ /
1737 (?: 1788 (?: