aboutsummaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorJoe Perches <joe@perches.com>2012-03-23 18:02:19 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-03-23 19:58:37 -0400
commitaad4f61498314d41d047ea2161b7bd7237b72d33 (patch)
tree503836c82abeaed36ad119538fa3cbb7b8d94b24 /scripts
parentb337d8b82f235d0212f7adcaeb431fd4e688bb98 (diff)
checkpatch: add --strict tests for braces, comments and casts
Add some more subjective --strict tests. Add a test for block comments that start with a blank line followed only by a line with just the comment block initiator. Prefer a blank line followed by /* comment... Add a test for unnecessary spaces after a cast. Add a test for symmetric uses of braces in if/else blocks. If one branch needs braces, then all branches should use braces. Signed-off-by: Joe Perches <joe@perches.com> Cc: 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.pl40
1 files changed, 32 insertions, 8 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 07d718415c5c..21486c04b11c 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -1849,6 +1849,17 @@ sub process {
1849 } 1849 }
1850 } 1850 }
1851 1851
1852 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) {
1853 CHK("SPACING",
1854 "No space is necessary after a cast\n" . $hereprev);
1855 }
1856
1857 if ($rawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
1858 $prevrawline =~ /^\+[ \t]*$/) {
1859 CHK("BLOCK_COMMENT_STYLE",
1860 "Don't begin block comments with only a /* line, use /* comment...\n" . $hereprev);
1861 }
1862
1852# check for spaces at the beginning of a line. 1863# check for spaces at the beginning of a line.
1853# Exceptions: 1864# Exceptions:
1854# 1) within comments 1865# 1) within comments
@@ -2961,7 +2972,8 @@ sub process {
2961 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 2972 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2962 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 2973 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2963 if ($#chunks > 0 && $level == 0) { 2974 if ($#chunks > 0 && $level == 0) {
2964 my $allowed = 0; 2975 my @allowed = ();
2976 my $allow = 0;
2965 my $seen = 0; 2977 my $seen = 0;
2966 my $herectx = $here . "\n"; 2978 my $herectx = $here . "\n";
2967 my $ln = $linenr - 1; 2979 my $ln = $linenr - 1;
@@ -2972,6 +2984,7 @@ sub process {
2972 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 2984 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2973 my $offset = statement_rawlines($whitespace) - 1; 2985 my $offset = statement_rawlines($whitespace) - 1;
2974 2986
2987 $allowed[$allow] = 0;
2975 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 2988 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2976 2989
2977 # We have looked at and allowed this specific line. 2990 # We have looked at and allowed this specific line.
@@ -2984,23 +2997,34 @@ sub process {
2984 2997
2985 $seen++ if ($block =~ /^\s*{/); 2998 $seen++ if ($block =~ /^\s*{/);
2986 2999
2987 #print "cond<$cond> block<$block> allowed<$allowed>\n"; 3000 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
2988 if (statement_lines($cond) > 1) { 3001 if (statement_lines($cond) > 1) {
2989 #print "APW: ALLOWED: cond<$cond>\n"; 3002 #print "APW: ALLOWED: cond<$cond>\n";
2990 $allowed = 1; 3003 $allowed[$allow] = 1;
2991 } 3004 }
2992 if ($block =~/\b(?:if|for|while)\b/) { 3005 if ($block =~/\b(?:if|for|while)\b/) {
2993 #print "APW: ALLOWED: block<$block>\n"; 3006 #print "APW: ALLOWED: block<$block>\n";
2994 $allowed = 1; 3007 $allowed[$allow] = 1;
2995 } 3008 }
2996 if (statement_block_size($block) > 1) { 3009 if (statement_block_size($block) > 1) {
2997 #print "APW: ALLOWED: lines block<$block>\n"; 3010 #print "APW: ALLOWED: lines block<$block>\n";
2998 $allowed = 1; 3011 $allowed[$allow] = 1;
2999 } 3012 }
3013 $allow++;
3000 } 3014 }
3001 if ($seen && !$allowed) { 3015 if ($seen) {
3002 WARN("BRACES", 3016 my $sum_allowed = 0;
3003 "braces {} are not necessary for any arm of this statement\n" . $herectx); 3017 foreach (@allowed) {
3018 $sum_allowed += $_;
3019 }
3020 if ($sum_allowed == 0) {
3021 WARN("BRACES",
3022 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3023 } elsif ($sum_allowed != $allow &&
3024 $seen != $allow) {
3025 CHK("BRACES",
3026 "braces {} should be used on all arms of this statement\n" . $herectx);
3027 }
3004 } 3028 }
3005 } 3029 }
3006 } 3030 }