Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
| 2 | # (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit) |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 3 | # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit) |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 4 | # (c) 2007, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite, etc) |
| 5 | # Licensed under the terms of the GNU GPL License version 2 |
| 6 | |
| 7 | use strict; |
| 8 | |
| 9 | my $P = $0; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 10 | $P =~ s@.*/@@g; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 11 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 12 | my $V = '0.08'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 13 | |
| 14 | use Getopt::Long qw(:config no_auto_abbrev); |
| 15 | |
| 16 | my $quiet = 0; |
| 17 | my $tree = 1; |
| 18 | my $chk_signoff = 1; |
| 19 | my $chk_patch = 1; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 20 | my $tst_type = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 21 | GetOptions( |
| 22 | 'q|quiet' => \$quiet, |
| 23 | 'tree!' => \$tree, |
| 24 | 'signoff!' => \$chk_signoff, |
| 25 | 'patch!' => \$chk_patch, |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 26 | 'test-type!' => \$tst_type, |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 27 | ) or exit; |
| 28 | |
| 29 | my $exit = 0; |
| 30 | |
| 31 | if ($#ARGV < 0) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 32 | print "usage: $P [options] patchfile\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 33 | print "version: $V\n"; |
| 34 | print "options: -q => quiet\n"; |
| 35 | print " --no-tree => run without a kernel tree\n"; |
| 36 | exit(1); |
| 37 | } |
| 38 | |
| 39 | if ($tree && !top_of_kernel_tree()) { |
| 40 | print "Must be run from the top-level dir. of a kernel tree\n"; |
| 41 | exit(2); |
| 42 | } |
| 43 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 44 | my @dep_includes = (); |
| 45 | my @dep_functions = (); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 46 | my $removal = 'Documentation/feature-removal-schedule.txt'; |
| 47 | if ($tree && -f $removal) { |
| 48 | open(REMOVE, "<$removal") || die "$P: $removal: open failed - $!\n"; |
| 49 | while (<REMOVE>) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 50 | if (/^Check:\s+(.*\S)/) { |
| 51 | for my $entry (split(/[, ]+/, $1)) { |
| 52 | if ($entry =~ m@include/(.*)@) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 53 | push(@dep_includes, $1); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 54 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 55 | } elsif ($entry !~ m@/@) { |
| 56 | push(@dep_functions, $entry); |
| 57 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 58 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 63 | my @rawlines = (); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 64 | while (<>) { |
| 65 | chomp; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 66 | push(@rawlines, $_); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 67 | if (eof(ARGV)) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 68 | if (!process($ARGV, @rawlines)) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 69 | $exit = 1; |
| 70 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 71 | @rawlines = (); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | exit($exit); |
| 76 | |
| 77 | sub top_of_kernel_tree { |
| 78 | if ((-f "COPYING") && (-f "CREDITS") && (-f "Kbuild") && |
| 79 | (-f "MAINTAINERS") && (-f "Makefile") && (-f "README") && |
| 80 | (-d "Documentation") && (-d "arch") && (-d "include") && |
| 81 | (-d "drivers") && (-d "fs") && (-d "init") && (-d "ipc") && |
| 82 | (-d "kernel") && (-d "lib") && (-d "scripts")) { |
| 83 | return 1; |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | sub expand_tabs { |
| 89 | my ($str) = @_; |
| 90 | |
| 91 | my $res = ''; |
| 92 | my $n = 0; |
| 93 | for my $c (split(//, $str)) { |
| 94 | if ($c eq "\t") { |
| 95 | $res .= ' '; |
| 96 | $n++; |
| 97 | for (; ($n % 8) != 0; $n++) { |
| 98 | $res .= ' '; |
| 99 | } |
| 100 | next; |
| 101 | } |
| 102 | $res .= $c; |
| 103 | $n++; |
| 104 | } |
| 105 | |
| 106 | return $res; |
| 107 | } |
| 108 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 109 | sub line_stats { |
| 110 | my ($line) = @_; |
| 111 | |
| 112 | # Drop the diff line leader and expand tabs |
| 113 | $line =~ s/^.//; |
| 114 | $line = expand_tabs($line); |
| 115 | |
| 116 | # Pick the indent from the front of the line. |
| 117 | my ($white) = ($line =~ /^(\s*)/); |
| 118 | |
| 119 | return (length($line), length($white)); |
| 120 | } |
| 121 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 122 | sub sanitise_line { |
| 123 | my ($line) = @_; |
| 124 | |
| 125 | my $res = ''; |
| 126 | my $l = ''; |
| 127 | |
| 128 | my $quote = ''; |
| 129 | |
| 130 | foreach my $c (split(//, $line)) { |
| 131 | if ($l ne "\\" && ($c eq "'" || $c eq '"')) { |
| 132 | if ($quote eq '') { |
| 133 | $quote = $c; |
| 134 | $res .= $c; |
| 135 | $l = $c; |
| 136 | next; |
| 137 | } elsif ($quote eq $c) { |
| 138 | $quote = ''; |
| 139 | } |
| 140 | } |
| 141 | if ($quote && $c ne "\t") { |
| 142 | $res .= "X"; |
| 143 | } else { |
| 144 | $res .= $c; |
| 145 | } |
| 146 | |
| 147 | $l = $c; |
| 148 | } |
| 149 | |
| 150 | return $res; |
| 151 | } |
| 152 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 153 | sub ctx_block_get { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 154 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 155 | my $line; |
| 156 | my $start = $linenr - 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 157 | my $blk = ''; |
| 158 | my @o; |
| 159 | my @c; |
| 160 | my @res = (); |
| 161 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 162 | my $level = 0; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 163 | for ($line = $start; $remain > 0; $line++) { |
| 164 | next if ($rawlines[$line] =~ /^-/); |
| 165 | $remain--; |
| 166 | |
| 167 | $blk .= $rawlines[$line]; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 168 | foreach my $c (split(//, $rawlines[$line])) { |
| 169 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
| 170 | if ($off > 0) { |
| 171 | $off--; |
| 172 | next; |
| 173 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 174 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 175 | if ($c eq $close && $level > 0) { |
| 176 | $level--; |
| 177 | last if ($level == 0); |
| 178 | } elsif ($c eq $open) { |
| 179 | $level++; |
| 180 | } |
| 181 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 182 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 183 | if (!$outer || $level <= 1) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 184 | push(@res, $rawlines[$line]); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 187 | last if ($level == 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 190 | return ($level, @res); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 191 | } |
| 192 | sub ctx_block_outer { |
| 193 | my ($linenr, $remain) = @_; |
| 194 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 195 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
| 196 | return @r; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 197 | } |
| 198 | sub ctx_block { |
| 199 | my ($linenr, $remain) = @_; |
| 200 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 201 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 202 | return @r; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 203 | } |
| 204 | sub ctx_statement { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 205 | my ($linenr, $remain, $off) = @_; |
| 206 | |
| 207 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 208 | return @r; |
| 209 | } |
| 210 | sub ctx_block_level { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 211 | my ($linenr, $remain) = @_; |
| 212 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 213 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | sub ctx_locate_comment { |
| 217 | my ($first_line, $end_line) = @_; |
| 218 | |
| 219 | # Catch a comment on the end of the line itself. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 220 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 221 | return $current_comment if (defined $current_comment); |
| 222 | |
| 223 | # Look through the context and try and figure out if there is a |
| 224 | # comment. |
| 225 | my $in_comment = 0; |
| 226 | $current_comment = ''; |
| 227 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 228 | my $line = $rawlines[$linenr - 1]; |
| 229 | #warn " $line\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 230 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 231 | $in_comment = 1; |
| 232 | } |
| 233 | if ($line =~ m@/\*@) { |
| 234 | $in_comment = 1; |
| 235 | } |
| 236 | if (!$in_comment && $current_comment ne '') { |
| 237 | $current_comment = ''; |
| 238 | } |
| 239 | $current_comment .= $line . "\n" if ($in_comment); |
| 240 | if ($line =~ m@\*/@) { |
| 241 | $in_comment = 0; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | chomp($current_comment); |
| 246 | return($current_comment); |
| 247 | } |
| 248 | sub ctx_has_comment { |
| 249 | my ($first_line, $end_line) = @_; |
| 250 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 251 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 252 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 253 | ##print "CMMT: $cmt\n"; |
| 254 | |
| 255 | return ($cmt ne ''); |
| 256 | } |
| 257 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 258 | sub cat_vet { |
| 259 | my ($vet) = @_; |
| 260 | |
| 261 | $vet =~ s/\t/^I/; |
| 262 | $vet =~ s/$/\$/; |
| 263 | |
| 264 | return $vet; |
| 265 | } |
| 266 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 267 | my @report = (); |
| 268 | sub report { |
| 269 | push(@report, $_[0]); |
| 270 | } |
| 271 | sub report_dump { |
| 272 | @report; |
| 273 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 274 | sub ERROR { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 275 | report("ERROR: $_[0]\n"); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 276 | our $clean = 0; |
| 277 | } |
| 278 | sub WARN { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 279 | report("WARNING: $_[0]\n"); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 280 | our $clean = 0; |
| 281 | } |
| 282 | sub CHK { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 283 | report("CHECK: $_[0]\n"); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 284 | our $clean = 0; |
| 285 | } |
| 286 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 287 | sub process { |
| 288 | my $filename = shift; |
| 289 | my @lines = @_; |
| 290 | |
| 291 | my $linenr=0; |
| 292 | my $prevline=""; |
| 293 | my $stashline=""; |
| 294 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 295 | my $length; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 296 | my $indent; |
| 297 | my $previndent=0; |
| 298 | my $stashindent=0; |
| 299 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 300 | our $clean = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 301 | my $signoff = 0; |
| 302 | my $is_patch = 0; |
| 303 | |
| 304 | # Trace the real file/line as we go. |
| 305 | my $realfile = ''; |
| 306 | my $realline = 0; |
| 307 | my $realcnt = 0; |
| 308 | my $here = ''; |
| 309 | my $in_comment = 0; |
| 310 | my $first_line = 0; |
| 311 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 312 | my $Ident = qr{[A-Za-z\d_]+}; |
| 313 | my $Storage = qr{extern|static}; |
| 314 | my $Sparse = qr{__user|__kernel|__force|__iomem}; |
| 315 | my $NonptrType = qr{ |
| 316 | \b |
| 317 | (?:const\s+)? |
| 318 | (?:unsigned\s+)? |
| 319 | (?: |
| 320 | void| |
| 321 | char| |
| 322 | short| |
| 323 | int| |
| 324 | long| |
| 325 | unsigned| |
| 326 | float| |
| 327 | double| |
| 328 | long\s+int| |
| 329 | long\s+long| |
| 330 | long\s+long\s+int| |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 331 | u8|u16|u32|u64| |
| 332 | s8|s16|s32|s64| |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 333 | struct\s+$Ident| |
| 334 | union\s+$Ident| |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 335 | enum\s+$Ident| |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 336 | ${Ident}_t |
| 337 | ) |
| 338 | (?:\s+$Sparse)* |
| 339 | \b |
| 340 | }x; |
| 341 | my $Type = qr{ |
| 342 | \b$NonptrType\b |
| 343 | (?:\s*\*+\s*const|\s*\*+)? |
| 344 | }x; |
| 345 | my $Declare = qr{(?:$Storage\s+)?$Type}; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 346 | my $Attribute = qr{const|__read_mostly|__init|__initdata|__meminit}; |
| 347 | |
| 348 | my $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
| 349 | my $Lval = qr{$Ident(?:$Member)*}; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 350 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 351 | # Pre-scan the patch looking for any __setup documentation. |
| 352 | my @setup_docs = (); |
| 353 | my $setup_docs = 0; |
| 354 | foreach my $line (@lines) { |
| 355 | if ($line=~/^\+\+\+\s+(\S+)/) { |
| 356 | $setup_docs = 0; |
| 357 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { |
| 358 | $setup_docs = 1; |
| 359 | } |
| 360 | next; |
| 361 | } |
| 362 | |
| 363 | if ($setup_docs && $line =~ /^\+/) { |
| 364 | push(@setup_docs, $line); |
| 365 | } |
| 366 | } |
| 367 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 368 | foreach my $line (@lines) { |
| 369 | $linenr++; |
| 370 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 371 | my $rawline = $line; |
| 372 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 373 | #extract the filename as it passes |
| 374 | if ($line=~/^\+\+\+\s+(\S+)/) { |
| 375 | $realfile=$1; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 376 | $realfile =~ s@^[^/]*/@@; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 377 | $in_comment = 0; |
| 378 | next; |
| 379 | } |
| 380 | #extract the line range in the file after the patch is applied |
| 381 | if ($line=~/^\@\@ -\d+,\d+ \+(\d+)(,(\d+))? \@\@/) { |
| 382 | $is_patch = 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 383 | $first_line = $linenr + 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 384 | $in_comment = 0; |
| 385 | $realline=$1-1; |
| 386 | if (defined $2) { |
| 387 | $realcnt=$3+1; |
| 388 | } else { |
| 389 | $realcnt=1+1; |
| 390 | } |
| 391 | next; |
| 392 | } |
| 393 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 394 | # track the line number as we move through the hunk, note that |
| 395 | # new versions of GNU diff omit the leading space on completely |
| 396 | # blank context lines so we need to count that too. |
| 397 | if ($line =~ /^( |\+|$)/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 398 | $realline++; |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 399 | $realcnt-- if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 400 | |
| 401 | # track any sort of multi-line comment. Obviously if |
| 402 | # the added text or context do not include the whole |
| 403 | # comment we will not see it. Such is life. |
| 404 | # |
| 405 | # Guestimate if this is a continuing comment. If this |
| 406 | # is the start of a diff block and this line starts |
| 407 | # ' *' then it is very likely a comment. |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 408 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 409 | $in_comment = 1; |
| 410 | } |
| 411 | if ($line =~ m@/\*@) { |
| 412 | $in_comment = 1; |
| 413 | } |
| 414 | if ($line =~ m@\*/@) { |
| 415 | $in_comment = 0; |
| 416 | } |
| 417 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 418 | # Measure the line length and indent. |
| 419 | ($length, $indent) = line_stats($line); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 420 | |
| 421 | # Track the previous line. |
| 422 | ($prevline, $stashline) = ($stashline, $line); |
| 423 | ($previndent, $stashindent) = ($stashindent, $indent); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 424 | } elsif ($realcnt == 1) { |
| 425 | $realcnt--; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | #make up the handle for any error we report on this line |
Randy Dunlap | 389834b | 2007-06-08 13:47:03 -0700 | [diff] [blame] | 429 | $here = "#$linenr: "; |
| 430 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 431 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 432 | my $hereline = "$here\n$line\n"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 433 | my $herecurr = "$here\n$line\n"; |
| 434 | my $hereprev = "$here\n$prevline\n$line\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 435 | |
| 436 | #check the patch for a signoff: |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 437 | if ($line =~ /^\s*signed-off-by:/i) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 438 | # This is a signoff, if ugly, so do not double report. |
| 439 | $signoff++; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 440 | if (!($line =~ /^\s*Signed-off-by:/)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 441 | WARN("Signed-off-by: is the preferred form\n" . |
| 442 | $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 443 | } |
| 444 | if ($line =~ /^\s*signed-off-by:\S/i) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 445 | WARN("need space after Signed-off-by:\n" . |
| 446 | $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 450 | # Check for wrappage within a valid hunk of the file |
| 451 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 452 | ERROR("patch seems to be corrupt (line wrapped?)\n" . |
| 453 | $herecurr); |
| 454 | } |
| 455 | |
| 456 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
| 457 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
| 458 | !($line =~ m/^( |
| 459 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 460 | | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
| 461 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
| 462 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
| 463 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
| 464 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 465 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 466 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 467 | )*$/x )) { |
| 468 | ERROR("Invalid UTF-8\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 469 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 470 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 471 | #ignore lines being removed |
| 472 | if ($line=~/^-/) {next;} |
| 473 | |
| 474 | # check we are in a valid source file if not then ignore this hunk |
| 475 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 476 | |
| 477 | #trailing whitespace |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 478 | if ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 479 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
| 480 | ERROR("trailing whitespace\n" . $herevet); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 481 | } |
| 482 | #80 column limit |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 483 | if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 484 | WARN("line over 80 characters\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | # check we are in a valid source file *.[hc] if not then ignore this hunk |
| 488 | next if ($realfile !~ /\.[hc]$/); |
| 489 | |
| 490 | # at the beginning of a line any tabs must come first and anything |
| 491 | # more than 8 must use tabs. |
| 492 | if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 493 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
| 494 | ERROR("use tabs not spaces\n" . $herevet); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | # |
| 498 | # The rest of our checks refer specifically to C style |
| 499 | # only apply those _outside_ comments. |
| 500 | # |
| 501 | next if ($in_comment); |
| 502 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 503 | # Remove comments from the line before processing. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 504 | $line =~ s@/\*.*\*/@@g; |
| 505 | $line =~ s@/\*.*@@; |
| 506 | $line =~ s@.*\*/@@; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 507 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 508 | # Standardise the strings and chars within the input to simplify matching. |
| 509 | $line = sanitise_line($line); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 510 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 511 | # |
| 512 | # Checks which may be anchored in the context. |
| 513 | # |
| 514 | |
| 515 | # Check for switch () and associated case and default |
| 516 | # statements should be at the same indent. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 517 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 518 | my $err = ''; |
| 519 | my $sep = ''; |
| 520 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 521 | shift(@ctx); |
| 522 | for my $ctx (@ctx) { |
| 523 | my ($clen, $cindent) = line_stats($ctx); |
| 524 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 525 | $indent != $cindent) { |
| 526 | $err .= "$sep$ctx\n"; |
| 527 | $sep = ''; |
| 528 | } else { |
| 529 | $sep = "[...]\n"; |
| 530 | } |
| 531 | } |
| 532 | if ($err ne '') { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 533 | ERROR("switch and case should be at the same indent\n$hereline\n$err\n"); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 538 | # or if that brace on the next line is for something else |
| 539 | if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 540 | my @ctx = ctx_statement($linenr, $realcnt, 0); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 541 | my $ctx_ln = $linenr + $#ctx + 1; |
| 542 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 543 | my $ctx = join("\n", @ctx); |
| 544 | |
| 545 | while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) { |
| 546 | $ctx_ln++; |
| 547 | $ctx_cnt--; |
| 548 | } |
| 549 | ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>"; |
| 550 | |
| 551 | if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 552 | ERROR("That open brace { should be on the previous line\n" . |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 553 | "$here\n$ctx\n$lines[$ctx_ln - 1]"); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | #ignore lines not being added |
| 558 | if ($line=~/^[^\+]/) {next;} |
| 559 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 560 | # TEST: allow direct testing of the type matcher. |
| 561 | if ($tst_type && $line =~ /^.$Declare$/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 562 | ERROR("TEST: is type $Declare\n" . $herecurr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 563 | next; |
| 564 | } |
| 565 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 566 | # check for initialisation to aggregates open brace on the next line |
| 567 | if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ && |
| 568 | $line =~ /^.\s*{/) { |
| 569 | ERROR("That open brace { should be on the previous line\n" . $hereprev); |
| 570 | } |
| 571 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 572 | # |
| 573 | # Checks which are anchored on the added line. |
| 574 | # |
| 575 | |
| 576 | # check for malformed paths in #include statements (uses RAW line) |
| 577 | if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) { |
| 578 | my $path = $1; |
| 579 | if ($path =~ m{//}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 580 | ERROR("malformed #include filename\n" . |
| 581 | $herecurr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 582 | } |
| 583 | # Sanitise this special form of string. |
| 584 | $path = 'X' x length($path); |
| 585 | $line =~ s{\<.*\>}{<$path>}; |
| 586 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 587 | |
| 588 | # no C99 // comments |
| 589 | if ($line =~ m{//}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 590 | ERROR("do not use C99 // comments\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 591 | } |
| 592 | # Remove C99 comments. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 593 | $line =~ s@//.*@@; |
| 594 | |
| 595 | #EXPORT_SYMBOL should immediately follow its function closing }. |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 596 | if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || |
| 597 | ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
| 598 | my $name = $1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 599 | if (($prevline !~ /^}/) && |
| 600 | ($prevline !~ /^\+}/) && |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 601 | ($prevline !~ /^ }/) && |
| 602 | ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 603 | WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 607 | # check for external initialisers. |
| 608 | if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) { |
| 609 | ERROR("do not initialise externals to 0 or NULL\n" . |
| 610 | $herecurr); |
| 611 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 612 | # check for static initialisers. |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 613 | if ($line =~ /\s*static\s.*=\s*(0|NULL);/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 614 | ERROR("do not initialise statics to 0 or NULL\n" . |
| 615 | $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 618 | # check for new typedefs, only function parameters and sparse annotations |
| 619 | # make sense. |
| 620 | if ($line =~ /\btypedef\s/ && |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 621 | $line !~ /\btypedef\s+$Type\s+\(\s*\*$Ident\s*\)\s*\(/ && |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 622 | $line !~ /\b__bitwise(?:__|)\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 623 | WARN("do not add new typedefs\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | # * goes on variable not on type |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 627 | if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 628 | ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" . |
| 629 | $herecurr); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 630 | |
| 631 | } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 632 | ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" . |
| 633 | $herecurr); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 634 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 635 | } elsif ($line =~ m{$NonptrType(\*+)(?:\s+$Attribute)?\s+[A-Za-z\d_]+}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 636 | ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" . |
| 637 | $herecurr); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 638 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 639 | } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+$Attribute)\s+[A-Za-z\d_]+}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 640 | ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" . |
| 641 | $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | # # no BUG() or BUG_ON() |
| 645 | # if ($line =~ /\b(BUG|BUG_ON)\b/) { |
| 646 | # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; |
| 647 | # print "$herecurr"; |
| 648 | # $clean = 0; |
| 649 | # } |
| 650 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 651 | # printk should use KERN_* levels. Note that follow on printk's on the |
| 652 | # same line do not need a level, so we use the current block context |
| 653 | # to try and find and validate the current printk. In summary the current |
| 654 | # printk includes all preceeding printk's which have no newline on the end. |
| 655 | # we assume the first bad printk is the one to report. |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 656 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 657 | my $ok = 0; |
| 658 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { |
| 659 | #print "CHECK<$lines[$ln - 1]\n"; |
| 660 | # we have a preceeding printk if it ends |
| 661 | # with "\n" ignore it, else it is to blame |
| 662 | if ($lines[$ln - 1] =~ m{\bprintk\(}) { |
| 663 | if ($rawlines[$ln - 1] !~ m{\\n"}) { |
| 664 | $ok = 1; |
| 665 | } |
| 666 | last; |
| 667 | } |
| 668 | } |
| 669 | if ($ok == 0) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 670 | WARN("printk() should include KERN_ facility level\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 671 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 674 | # function brace can't be on same line, except for #defines of do while, |
| 675 | # or if closed on same line |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 676 | if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 677 | !($line=~/\#define.*do\s{/) and !($line=~/}/)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 678 | ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 679 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 680 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 681 | # check for spaces between functions and their parentheses. |
| 682 | if ($line =~ /($Ident)\s+\(/ && |
| 683 | $1 !~ /^(?:if|for|while|switch|return|volatile)$/ && |
| 684 | $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) { |
| 685 | ERROR("no space between function name and open parenthesis '('\n" . $herecurr); |
| 686 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 687 | # Check operator spacing. |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 688 | # Note we expand the line with the leading + as the real |
| 689 | # line will be displayed with the leading + and the tabs |
| 690 | # will therefore also expand that way. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 691 | my $opline = $line; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 692 | $opline = expand_tabs($opline); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 693 | $opline =~ s/^./ /; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 694 | if (!($line=~/\#\s*include/)) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 695 | my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|=>|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 696 | my $off = 0; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 697 | for (my $n = 0; $n < $#elements; $n += 2) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 698 | $off += length($elements[$n]); |
| 699 | |
| 700 | my $a = ''; |
| 701 | $a = 'V' if ($elements[$n] ne ''); |
| 702 | $a = 'W' if ($elements[$n] =~ /\s$/); |
| 703 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 704 | $a = 'O' if ($elements[$n] eq ''); |
| 705 | $a = 'E' if ($elements[$n] eq '' && $n == 0); |
| 706 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 707 | my $op = $elements[$n + 1]; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 708 | |
| 709 | my $c = ''; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 710 | if (defined $elements[$n + 2]) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 711 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 712 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
| 713 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 714 | $c = 'O' if ($elements[$n + 2] eq ''); |
| 715 | } else { |
| 716 | $c = 'E'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 719 | # Pick up the preceeding and succeeding characters. |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 720 | my $ca = substr($opline, 0, $off); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 721 | my $cc = ''; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 722 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 723 | $cc = substr($opline, $off + length($elements[$n + 1])); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 724 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 725 | my $cb = "$ca$;$cc"; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 726 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 727 | my $ctx = "${a}x${c}"; |
| 728 | |
| 729 | my $at = "(ctx:$ctx)"; |
| 730 | |
| 731 | my $ptr = (" " x $off) . "^"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 732 | my $hereptr = "$hereline$ptr\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 733 | |
| 734 | ##print "<$s1:$op:$s2> <$elements[$n]:$elements[$n + 1]:$elements[$n + 2]>\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 735 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 736 | # ; should have either the end of line or a space or \ after it |
| 737 | if ($op eq ';') { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 738 | if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ && |
| 739 | $cc !~ /^;/) { |
| 740 | ERROR("need space after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | # // is a comment |
| 744 | } elsif ($op eq '//') { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 745 | |
| 746 | # -> should have no spaces |
| 747 | } elsif ($op eq '->') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 748 | if ($ctx =~ /Wx.|.xW/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 749 | ERROR("no spaces around that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | # , must have a space on the right. |
| 753 | } elsif ($op eq ',') { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 754 | if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 755 | ERROR("need space after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | # unary ! and unary ~ are allowed no space on the right |
| 759 | } elsif ($op eq '!' or $op eq '~') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 760 | if ($ctx !~ /[WOEB]x./) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 761 | ERROR("need space before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 762 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 763 | if ($ctx =~ /.xW/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 764 | ERROR("no space after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | # unary ++ and unary -- are allowed no space on one side. |
| 768 | } elsif ($op eq '++' or $op eq '--') { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 769 | if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 770 | ERROR("need space one side of that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 771 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 772 | if ($ctx =~ /Wx./ && $cc =~ /^;/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 773 | ERROR("no space before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 774 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 775 | |
| 776 | # & is both unary and binary |
| 777 | # unary: |
| 778 | # a &b |
| 779 | # binary (consistent spacing): |
| 780 | # a&b OK |
| 781 | # a & b OK |
| 782 | # |
| 783 | # boiling down to: if there is a space on the right then there |
| 784 | # should be one on the left. |
| 785 | # |
| 786 | # - is the same |
| 787 | # |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 788 | } elsif ($op eq '&' or $op eq '-') { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 789 | if ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 790 | ERROR("need space before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 793 | # * is the same as & only adding: |
| 794 | # type: |
| 795 | # (foo *) |
| 796 | # (foo **) |
| 797 | # |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 798 | } elsif ($op eq '*') { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 799 | if ($ca !~ /$Type$/ && $cb !~ /(\*$;|$;\*)/ && |
| 800 | $ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) { |
| 801 | ERROR("need space before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | # << and >> may either have or not have spaces both sides |
| 805 | } elsif ($op eq '<<' or $op eq '>>' or $op eq '+' or $op eq '/' or |
| 806 | $op eq '^' or $op eq '|') |
| 807 | { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 808 | if ($ctx !~ /VxV|WxW|VxE|WxE/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 809 | ERROR("need consistent spacing around '$op' $at\n" . |
| 810 | $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | # All the others need spaces both sides. |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 814 | } elsif ($ctx !~ /[EW]x[WE]/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 815 | ERROR("need spaces around that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 816 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 817 | $off += length($elements[$n + 1]); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 821 | # check for multiple assignments |
| 822 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { |
| 823 | WARN("multiple assignments should be avoided\n" . $herecurr); |
| 824 | } |
| 825 | |
| 826 | # check for multiple declarations, allowing for a function declaration |
| 827 | # continuation. |
| 828 | if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 829 | $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 830 | WARN("declaring multiple variables together should be avoided\n" . $herecurr); |
| 831 | } |
| 832 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 833 | #need space before brace following if, while, etc |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 834 | if ($line =~ /\(.*\){/ || $line =~ /do{/) { |
| 835 | ERROR("need a space before the open brace '{'\n" . $herecurr); |
| 836 | } |
| 837 | |
| 838 | # closing brace should have a space following it when it has anything |
| 839 | # on the line |
| 840 | if ($line =~ /}(?!(?:,|;|\)))\S/) { |
| 841 | ERROR("need a space after that close brace '}'\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | #goto labels aren't indented, allow a single space however |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 845 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 846 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 847 | WARN("labels should not be indented\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | # Need a space before open parenthesis after if, while etc |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 851 | if ($line=~/\b(if|while|for|switch)\(/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 852 | ERROR("need a space before the open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | # Check for illegal assignment in if conditional. |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 856 | if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 857 | #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 858 | ERROR("do not use assignment in if condition\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | # Check for }<nl>else {, these must be at the same |
| 862 | # indent level to be relevant to each other. |
| 863 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and |
| 864 | $previndent == $indent) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 865 | ERROR("else should follow close brace '}'\n" . $hereprev); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 868 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new |
| 869 | # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { |
| 870 | # print "No studly caps, use _\n"; |
| 871 | # print "$herecurr"; |
| 872 | # $clean = 0; |
| 873 | # } |
| 874 | |
| 875 | #no spaces allowed after \ in define |
| 876 | if ($line=~/\#define.*\\\s$/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 877 | WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 880 | #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) |
| 881 | if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 882 | my $checkfile = "include/linux/$1.h"; |
| 883 | if (-f $checkfile) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 884 | CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" . |
| 885 | $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 889 | # if and else should not have general statements after it |
| 890 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ && |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 891 | $1 !~ /^\s*(?:\sif|{|\\|$)/) { |
| 892 | ERROR("trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 895 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 896 | # first statement and ensure its the whole macro if its not enclosed |
| 897 | # in a known goot container |
| 898 | if (($prevline=~/\#define.*\\/) and |
| 899 | !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and |
| 900 | !($line=~/do.*{/) and !($line=~/\(\{/) and |
| 901 | !($line=~/^.\s*$Declare\s/)) { |
| 902 | # Grab the first statement, if that is the entire macro |
| 903 | # its ok. This may start either on the #define line |
| 904 | # or the one below. |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 905 | my $ln = $linenr; |
| 906 | my $cnt = $realcnt; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 907 | my $off = 0; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 908 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 909 | # If the macro starts on the define line start |
| 910 | # grabbing the statement after the identifier |
| 911 | $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$}; |
| 912 | ##print "1<$1> 2<$2>\n"; |
| 913 | if ($2 ne '') { |
| 914 | $off = length($1); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 915 | $ln--; |
| 916 | $cnt++; |
| 917 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 918 | my @ctx = ctx_statement($ln, $cnt, $off); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 919 | my $ctx_ln = $ln + $#ctx + 1; |
| 920 | my $ctx = join("\n", @ctx); |
| 921 | |
| 922 | # Pull in any empty extension lines. |
| 923 | while ($ctx =~ /\\$/ && |
| 924 | $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) { |
| 925 | $ctx .= $lines[$ctx_ln - 1]; |
| 926 | $ctx_ln++; |
| 927 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 928 | |
| 929 | if ($ctx =~ /\\$/) { |
| 930 | if ($ctx =~ /;/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 931 | ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 932 | } else { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 933 | ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 934 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 935 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 938 | # check for redundant bracing round if etc |
| 939 | if ($line =~ /\b(if|while|for|else)\b/) { |
| 940 | # Locate the end of the opening statement. |
| 941 | my @control = ctx_statement($linenr, $realcnt, 0); |
| 942 | my $nr = $linenr + (scalar(@control) - 1); |
| 943 | my $cnt = $realcnt - (scalar(@control) - 1); |
| 944 | |
| 945 | my $off = $realcnt - $cnt; |
| 946 | #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n"; |
| 947 | |
| 948 | # If this is is a braced statement group check it |
| 949 | if ($lines[$nr - 1] =~ /{\s*$/) { |
| 950 | my ($lvl, @block) = ctx_block_level($nr, $cnt); |
| 951 | |
| 952 | my $stmt = join(' ', @block); |
| 953 | $stmt =~ s/^[^{]*{//; |
| 954 | $stmt =~ s/}[^}]*$//; |
| 955 | |
| 956 | #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n"; |
| 957 | #print "stmt<$stmt>\n\n"; |
| 958 | |
| 959 | # Count the ;'s if there is fewer than two |
| 960 | # then there can only be one statement, |
| 961 | # if there is a brace inside we cannot |
| 962 | # trivially detect if its one statement. |
| 963 | # Also nested if's often require braces to |
| 964 | # disambiguate the else binding so shhh there. |
| 965 | my @semi = ($stmt =~ /;/g); |
| 966 | ##print "semi<" . scalar(@semi) . ">\n"; |
| 967 | if ($lvl == 0 && scalar(@semi) < 2 && |
| 968 | $stmt !~ /{/ && $stmt !~ /\bif\b/) { |
| 969 | my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n"; |
| 970 | shift(@block); |
| 971 | ERROR("braces {} are not necessary for single statement blocks\n" . $herectx); |
| 972 | } |
| 973 | } |
| 974 | } |
| 975 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 976 | # don't include deprecated include files (uses RAW line) |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 977 | for my $inc (@dep_includes) { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 978 | if ($rawline =~ m@\#\s*include\s*\<$inc>@) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 979 | ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 983 | # don't use deprecated functions |
| 984 | for my $func (@dep_functions) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 985 | if ($line =~ /\b$func\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 986 | ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
| 990 | # no volatiles please |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 991 | if ($line =~ /\bvolatile\b/ && $line !~ /\basm\s+volatile\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 992 | WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 995 | # warn about #if 0 |
| 996 | if ($line =~ /^.#\s*if\s+0\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 997 | CHK("if this code is redundant consider removing it\n" . |
| 998 | $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1001 | # check for needless kfree() checks |
| 1002 | if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { |
| 1003 | my $expr = $1; |
| 1004 | if ($line =~ /\bkfree\(\Q$expr\E\);/) { |
| 1005 | WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev); |
| 1006 | } |
| 1007 | } |
| 1008 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1009 | # warn about #ifdefs in C files |
| 1010 | # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
| 1011 | # print "#ifdef in C files should be avoided\n"; |
| 1012 | # print "$herecurr"; |
| 1013 | # $clean = 0; |
| 1014 | # } |
| 1015 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1016 | # check for spinlock_t definitions without a comment. |
| 1017 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { |
| 1018 | my $which = $1; |
| 1019 | if (!ctx_has_comment($first_line, $linenr)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1020 | CHK("$1 definition without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | # check for memory barriers without a comment. |
| 1024 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { |
| 1025 | if (!ctx_has_comment($first_line, $linenr)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1026 | CHK("memory barrier without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | # check of hardware specific defines |
| 1030 | if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1031 | CHK("architecture specific defines should be avoided\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1032 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1033 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1034 | # check the location of the inline attribute, that it is between |
| 1035 | # storage class and type. |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1036 | if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ || |
| 1037 | $line =~ /\b(?:inline|always_inline)\s+$Storage/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1038 | ERROR("inline keyword should sit between storage class and type\n" . $herecurr); |
| 1039 | } |
| 1040 | |
| 1041 | # check for new externs in .c files. |
| 1042 | if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) { |
| 1043 | WARN("externs should be avoided in .c files\n" . $herecurr); |
| 1044 | } |
| 1045 | |
| 1046 | # checks for new __setup's |
| 1047 | if ($rawline =~ /\b__setup\("([^"]*)"/) { |
| 1048 | my $name = $1; |
| 1049 | |
| 1050 | if (!grep(/$name/, @setup_docs)) { |
| 1051 | CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); |
| 1052 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1053 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | if ($chk_patch && !$is_patch) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1057 | ERROR("Does not appear to be a unified-diff format patch\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1058 | } |
| 1059 | if ($is_patch && $chk_signoff && $signoff == 0) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1060 | ERROR("Missing Signed-off-by: line(s)\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1063 | if ($clean == 0 && ($chk_patch || $is_patch)) { |
| 1064 | print report_dump(); |
| 1065 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1066 | if ($clean == 1 && $quiet == 0) { |
| 1067 | print "Your patch has no obvious style problems and is ready for submission.\n" |
| 1068 | } |
| 1069 | if ($clean == 0 && $quiet == 0) { |
| 1070 | print "Your patch has style problems, please review. If any of these errors\n"; |
| 1071 | print "are false positives report them to the maintainer, see\n"; |
| 1072 | print "CHECKPATCH in MAINTAINERS.\n"; |
| 1073 | } |
| 1074 | return $clean; |
| 1075 | } |