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 | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 12 | my $V = '0.11'; |
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 | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 21 | my $emacs = 0; |
| 22 | my $file = 0; |
| 23 | my $check = 0; |
| 24 | my $root; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 25 | GetOptions( |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 26 | 'q|quiet+' => \$quiet, |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 27 | 'tree!' => \$tree, |
| 28 | 'signoff!' => \$chk_signoff, |
| 29 | 'patch!' => \$chk_patch, |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 30 | 'test-type!' => \$tst_type, |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 31 | 'emacs!' => \$emacs, |
| 32 | 'file!' => \$file, |
| 33 | 'subjective!' => \$check, |
| 34 | 'strict!' => \$check, |
| 35 | 'root=s' => \$root, |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 36 | ) or exit; |
| 37 | |
| 38 | my $exit = 0; |
| 39 | |
| 40 | if ($#ARGV < 0) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 41 | print "usage: $P [options] patchfile\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 42 | print "version: $V\n"; |
| 43 | print "options: -q => quiet\n"; |
| 44 | print " --no-tree => run without a kernel tree\n"; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 45 | print " --emacs => emacs compile window format\n"; |
| 46 | print " --file => check a source file\n"; |
| 47 | print " --strict => enable more subjective tests\n"; |
| 48 | print " --root => path to the kernel tree root\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 49 | exit(1); |
| 50 | } |
| 51 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 52 | if ($tree) { |
| 53 | if (defined $root) { |
| 54 | if (!top_of_kernel_tree($root)) { |
| 55 | die "$P: $root: --root does not point at a valid tree\n"; |
| 56 | } |
| 57 | } else { |
| 58 | if (top_of_kernel_tree('.')) { |
| 59 | $root = '.'; |
| 60 | } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && |
| 61 | top_of_kernel_tree($1)) { |
| 62 | $root = $1; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if (!defined $root) { |
| 67 | print "Must be run from the top-level dir. of a kernel tree\n"; |
| 68 | exit(2); |
| 69 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 72 | my $emitted_corrupt = 0; |
| 73 | |
| 74 | our $Ident = qr{[A-Za-z_][A-Za-z\d_]*}; |
| 75 | our $Storage = qr{extern|static|asmlinkage}; |
| 76 | our $Sparse = qr{ |
| 77 | __user| |
| 78 | __kernel| |
| 79 | __force| |
| 80 | __iomem| |
| 81 | __must_check| |
| 82 | __init_refok| |
| 83 | __kprobes| |
| 84 | fastcall |
| 85 | }x; |
| 86 | our $Attribute = qr{ |
| 87 | const| |
| 88 | __read_mostly| |
| 89 | __kprobes| |
| 90 | __(?:mem|cpu|dev|)(?:initdata|init) |
| 91 | }x; |
| 92 | our $Inline = qr{inline|__always_inline|noinline}; |
| 93 | our $NonptrType = qr{ |
| 94 | \b |
| 95 | (?:const\s+)? |
| 96 | (?:unsigned\s+)? |
| 97 | (?: |
| 98 | void| |
| 99 | char| |
| 100 | short| |
| 101 | int| |
| 102 | long| |
| 103 | unsigned| |
| 104 | float| |
| 105 | double| |
| 106 | bool| |
| 107 | long\s+int| |
| 108 | long\s+long| |
| 109 | long\s+long\s+int| |
| 110 | (?:__)?(?:u|s|be|le)(?:8|16|32|64)| |
| 111 | struct\s+$Ident| |
| 112 | union\s+$Ident| |
| 113 | enum\s+$Ident| |
| 114 | ${Ident}_t| |
| 115 | ${Ident}_handler| |
| 116 | ${Ident}_handler_fn |
| 117 | ) |
| 118 | (?:\s+$Sparse)* |
| 119 | \b |
| 120 | }x; |
| 121 | |
| 122 | our $Type = qr{ |
| 123 | \b$NonptrType\b |
| 124 | (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? |
| 125 | (?:\s+$Sparse|\s+$Attribute)* |
| 126 | }x; |
| 127 | our $Declare = qr{(?:$Storage\s+)?$Type}; |
| 128 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
| 129 | our $Lval = qr{$Ident(?:$Member)*}; |
| 130 | |
| 131 | our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; |
| 132 | our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; |
| 133 | our $Operators = qr{ |
| 134 | <=|>=|==|!=| |
| 135 | =>|->|<<|>>|<|>|!|~| |
| 136 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/ |
| 137 | }x; |
| 138 | |
| 139 | our $Bare = ''; |
| 140 | |
| 141 | $chk_signoff = 0 if ($file); |
| 142 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 143 | my @dep_includes = (); |
| 144 | my @dep_functions = (); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 145 | my $removal = "Documentation/feature-removal-schedule.txt"; |
| 146 | if ($tree && -f "$root/$removal") { |
| 147 | open(REMOVE, "<$root/$removal") || |
| 148 | die "$P: $removal: open failed - $!\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 149 | while (<REMOVE>) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 150 | if (/^Check:\s+(.*\S)/) { |
| 151 | for my $entry (split(/[, ]+/, $1)) { |
| 152 | if ($entry =~ m@include/(.*)@) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 153 | push(@dep_includes, $1); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 154 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 155 | } elsif ($entry !~ m@/@) { |
| 156 | push(@dep_functions, $entry); |
| 157 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 158 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 163 | my @rawlines = (); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 164 | for my $filename (@ARGV) { |
| 165 | if ($file) { |
| 166 | open(FILE, "diff -u /dev/null $filename|") || |
| 167 | die "$P: $filename: diff failed - $!\n"; |
| 168 | } else { |
| 169 | open(FILE, "<$filename") || |
| 170 | die "$P: $filename: open failed - $!\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 171 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 172 | while (<FILE>) { |
| 173 | chomp; |
| 174 | push(@rawlines, $_); |
| 175 | } |
| 176 | close(FILE); |
| 177 | if (!process($filename, @rawlines)) { |
| 178 | $exit = 1; |
| 179 | } |
| 180 | @rawlines = (); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | exit($exit); |
| 184 | |
| 185 | sub top_of_kernel_tree { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 186 | my ($root) = @_; |
| 187 | |
| 188 | my @tree_check = ( |
| 189 | "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", |
| 190 | "README", "Documentation", "arch", "include", "drivers", |
| 191 | "fs", "init", "ipc", "kernel", "lib", "scripts", |
| 192 | ); |
| 193 | |
| 194 | foreach my $check (@tree_check) { |
| 195 | if (! -e $root . '/' . $check) { |
| 196 | return 0; |
| 197 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 198 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 199 | return 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | sub expand_tabs { |
| 203 | my ($str) = @_; |
| 204 | |
| 205 | my $res = ''; |
| 206 | my $n = 0; |
| 207 | for my $c (split(//, $str)) { |
| 208 | if ($c eq "\t") { |
| 209 | $res .= ' '; |
| 210 | $n++; |
| 211 | for (; ($n % 8) != 0; $n++) { |
| 212 | $res .= ' '; |
| 213 | } |
| 214 | next; |
| 215 | } |
| 216 | $res .= $c; |
| 217 | $n++; |
| 218 | } |
| 219 | |
| 220 | return $res; |
| 221 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 222 | sub copy_spacing { |
| 223 | my ($str) = @_; |
| 224 | |
| 225 | my $res = ''; |
| 226 | for my $c (split(//, $str)) { |
| 227 | if ($c eq "\t") { |
| 228 | $res .= $c; |
| 229 | } else { |
| 230 | $res .= ' '; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return $res; |
| 235 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 236 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 237 | sub line_stats { |
| 238 | my ($line) = @_; |
| 239 | |
| 240 | # Drop the diff line leader and expand tabs |
| 241 | $line =~ s/^.//; |
| 242 | $line = expand_tabs($line); |
| 243 | |
| 244 | # Pick the indent from the front of the line. |
| 245 | my ($white) = ($line =~ /^(\s*)/); |
| 246 | |
| 247 | return (length($line), length($white)); |
| 248 | } |
| 249 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 250 | sub sanitise_line { |
| 251 | my ($line) = @_; |
| 252 | |
| 253 | my $res = ''; |
| 254 | my $l = ''; |
| 255 | |
| 256 | my $quote = ''; |
| 257 | |
| 258 | foreach my $c (split(//, $line)) { |
| 259 | if ($l ne "\\" && ($c eq "'" || $c eq '"')) { |
| 260 | if ($quote eq '') { |
| 261 | $quote = $c; |
| 262 | $res .= $c; |
| 263 | $l = $c; |
| 264 | next; |
| 265 | } elsif ($quote eq $c) { |
| 266 | $quote = ''; |
| 267 | } |
| 268 | } |
| 269 | if ($quote && $c ne "\t") { |
| 270 | $res .= "X"; |
| 271 | } else { |
| 272 | $res .= $c; |
| 273 | } |
| 274 | |
| 275 | $l = $c; |
| 276 | } |
| 277 | |
| 278 | return $res; |
| 279 | } |
| 280 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 281 | sub ctx_block_get { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 282 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 283 | my $line; |
| 284 | my $start = $linenr - 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 285 | my $blk = ''; |
| 286 | my @o; |
| 287 | my @c; |
| 288 | my @res = (); |
| 289 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 290 | my $level = 0; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 291 | for ($line = $start; $remain > 0; $line++) { |
| 292 | next if ($rawlines[$line] =~ /^-/); |
| 293 | $remain--; |
| 294 | |
| 295 | $blk .= $rawlines[$line]; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 296 | foreach my $c (split(//, $rawlines[$line])) { |
| 297 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; |
| 298 | if ($off > 0) { |
| 299 | $off--; |
| 300 | next; |
| 301 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 302 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 303 | if ($c eq $close && $level > 0) { |
| 304 | $level--; |
| 305 | last if ($level == 0); |
| 306 | } elsif ($c eq $open) { |
| 307 | $level++; |
| 308 | } |
| 309 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 310 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 311 | if (!$outer || $level <= 1) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 312 | push(@res, $rawlines[$line]); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 315 | last if ($level == 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 318 | return ($level, @res); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 319 | } |
| 320 | sub ctx_block_outer { |
| 321 | my ($linenr, $remain) = @_; |
| 322 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 323 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); |
| 324 | return @r; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 325 | } |
| 326 | sub ctx_block { |
| 327 | my ($linenr, $remain) = @_; |
| 328 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 329 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
| 330 | return @r; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 331 | } |
| 332 | sub ctx_statement { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 333 | my ($linenr, $remain, $off) = @_; |
| 334 | |
| 335 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 336 | return @r; |
| 337 | } |
| 338 | sub ctx_block_level { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 339 | my ($linenr, $remain) = @_; |
| 340 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 341 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 342 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 343 | sub ctx_statement_level { |
| 344 | my ($linenr, $remain, $off) = @_; |
| 345 | |
| 346 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); |
| 347 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 348 | |
| 349 | sub ctx_locate_comment { |
| 350 | my ($first_line, $end_line) = @_; |
| 351 | |
| 352 | # Catch a comment on the end of the line itself. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 353 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 354 | return $current_comment if (defined $current_comment); |
| 355 | |
| 356 | # Look through the context and try and figure out if there is a |
| 357 | # comment. |
| 358 | my $in_comment = 0; |
| 359 | $current_comment = ''; |
| 360 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 361 | my $line = $rawlines[$linenr - 1]; |
| 362 | #warn " $line\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 363 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
| 364 | $in_comment = 1; |
| 365 | } |
| 366 | if ($line =~ m@/\*@) { |
| 367 | $in_comment = 1; |
| 368 | } |
| 369 | if (!$in_comment && $current_comment ne '') { |
| 370 | $current_comment = ''; |
| 371 | } |
| 372 | $current_comment .= $line . "\n" if ($in_comment); |
| 373 | if ($line =~ m@\*/@) { |
| 374 | $in_comment = 0; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | chomp($current_comment); |
| 379 | return($current_comment); |
| 380 | } |
| 381 | sub ctx_has_comment { |
| 382 | my ($first_line, $end_line) = @_; |
| 383 | my $cmt = ctx_locate_comment($first_line, $end_line); |
| 384 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 385 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 386 | ##print "CMMT: $cmt\n"; |
| 387 | |
| 388 | return ($cmt ne ''); |
| 389 | } |
| 390 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 391 | sub cat_vet { |
| 392 | my ($vet) = @_; |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 393 | my ($res, $coded); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 394 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 395 | $res = ''; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 396 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { |
| 397 | $res .= $1; |
| 398 | if ($2 ne '') { |
| 399 | $coded = sprintf("^%c", unpack('C', $2) + 64); |
| 400 | $res .= $coded; |
| 401 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 402 | } |
| 403 | $res =~ s/$/\$/; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 404 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 405 | return $res; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 408 | sub annotate_values { |
| 409 | my ($stream, $type) = @_; |
| 410 | |
| 411 | my $res; |
| 412 | my $cur = $stream; |
| 413 | |
| 414 | my $debug = 0; |
| 415 | |
| 416 | print "$stream\n" if ($debug); |
| 417 | |
| 418 | ##my $type = 'N'; |
| 419 | my $pos = 0; |
| 420 | my $preprocessor = 0; |
| 421 | my $paren = 0; |
| 422 | my @paren_type; |
| 423 | |
| 424 | # Include any user defined types we may have found as we went. |
| 425 | my $type_match = "(?:$Type$Bare)"; |
| 426 | |
| 427 | while (length($cur)) { |
| 428 | print " <$type> " if ($debug); |
| 429 | if ($cur =~ /^(\s+)/o) { |
| 430 | print "WS($1)\n" if ($debug); |
| 431 | if ($1 =~ /\n/ && $preprocessor) { |
| 432 | $preprocessor = 0; |
| 433 | $type = 'N'; |
| 434 | } |
| 435 | |
| 436 | } elsif ($cur =~ /^($type_match)/) { |
| 437 | print "DECLARE($1)\n" if ($debug); |
| 438 | $type = 'T'; |
| 439 | |
| 440 | } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) { |
| 441 | print "DEFINE($1)\n" if ($debug); |
| 442 | $preprocessor = 1; |
| 443 | $paren_type[$paren] = 'N'; |
| 444 | |
| 445 | } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|endif))/o) { |
| 446 | print "PRE($1)\n" if ($debug); |
| 447 | $preprocessor = 1; |
| 448 | $type = 'N'; |
| 449 | |
| 450 | } elsif ($cur =~ /^(\\\n)/o) { |
| 451 | print "PRECONT($1)\n" if ($debug); |
| 452 | |
| 453 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
| 454 | print "SIZEOF($1)\n" if ($debug); |
| 455 | if (defined $2) { |
| 456 | $paren_type[$paren] = 'V'; |
| 457 | } |
| 458 | $type = 'N'; |
| 459 | |
| 460 | } elsif ($cur =~ /^(if|while|typeof)\b/o) { |
| 461 | print "COND($1)\n" if ($debug); |
| 462 | $paren_type[$paren] = 'N'; |
| 463 | $type = 'N'; |
| 464 | |
| 465 | } elsif ($cur =~/^(return|case|else)/o) { |
| 466 | print "KEYWORD($1)\n" if ($debug); |
| 467 | $type = 'N'; |
| 468 | |
| 469 | } elsif ($cur =~ /^(\()/o) { |
| 470 | print "PAREN('$1')\n" if ($debug); |
| 471 | $paren++; |
| 472 | $type = 'N'; |
| 473 | |
| 474 | } elsif ($cur =~ /^(\))/o) { |
| 475 | $paren-- if ($paren > 0); |
| 476 | if (defined $paren_type[$paren]) { |
| 477 | $type = $paren_type[$paren]; |
| 478 | undef $paren_type[$paren]; |
| 479 | print "PAREN('$1') -> $type\n" if ($debug); |
| 480 | } else { |
| 481 | print "PAREN('$1')\n" if ($debug); |
| 482 | } |
| 483 | |
| 484 | } elsif ($cur =~ /^($Ident)\(/o) { |
| 485 | print "FUNC($1)\n" if ($debug); |
| 486 | $paren_type[$paren] = 'V'; |
| 487 | |
| 488 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
| 489 | print "IDENT($1)\n" if ($debug); |
| 490 | $type = 'V'; |
| 491 | |
| 492 | } elsif ($cur =~ /^($Assignment)/o) { |
| 493 | print "ASSIGN($1)\n" if ($debug); |
| 494 | $type = 'N'; |
| 495 | |
| 496 | } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) { |
| 497 | print "END($1)\n" if ($debug); |
| 498 | $type = 'N'; |
| 499 | |
| 500 | } elsif ($cur =~ /^($Operators)/o) { |
| 501 | print "OP($1)\n" if ($debug); |
| 502 | if ($1 ne '++' && $1 ne '--') { |
| 503 | $type = 'N'; |
| 504 | } |
| 505 | |
| 506 | } elsif ($cur =~ /(^.)/o) { |
| 507 | print "C($1)\n" if ($debug); |
| 508 | } |
| 509 | if (defined $1) { |
| 510 | $cur = substr($cur, length($1)); |
| 511 | $res .= $type x length($1); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | return $res; |
| 516 | } |
| 517 | |
| 518 | my $prefix = ''; |
| 519 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 520 | my @report = (); |
| 521 | sub report { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 522 | push(@report, $prefix . $_[0]); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 523 | } |
| 524 | sub report_dump { |
| 525 | @report; |
| 526 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 527 | sub ERROR { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 528 | report("ERROR: $_[0]\n"); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 529 | our $clean = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 530 | our $cnt_error++; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 531 | } |
| 532 | sub WARN { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 533 | report("WARNING: $_[0]\n"); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 534 | our $clean = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 535 | our $cnt_warn++; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 536 | } |
| 537 | sub CHK { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 538 | if ($check) { |
| 539 | report("CHECK: $_[0]\n"); |
| 540 | our $clean = 0; |
| 541 | our $cnt_chk++; |
| 542 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 545 | sub process { |
| 546 | my $filename = shift; |
| 547 | my @lines = @_; |
| 548 | |
| 549 | my $linenr=0; |
| 550 | my $prevline=""; |
| 551 | my $stashline=""; |
| 552 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 553 | my $length; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 554 | my $indent; |
| 555 | my $previndent=0; |
| 556 | my $stashindent=0; |
| 557 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 558 | our $clean = 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 559 | my $signoff = 0; |
| 560 | my $is_patch = 0; |
| 561 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 562 | our $cnt_lines = 0; |
| 563 | our $cnt_error = 0; |
| 564 | our $cnt_warn = 0; |
| 565 | our $cnt_chk = 0; |
| 566 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 567 | # Trace the real file/line as we go. |
| 568 | my $realfile = ''; |
| 569 | my $realline = 0; |
| 570 | my $realcnt = 0; |
| 571 | my $here = ''; |
| 572 | my $in_comment = 0; |
| 573 | my $first_line = 0; |
| 574 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 575 | my $prev_values = 'N'; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 576 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 577 | # Possible bare types. |
| 578 | my @bare = (); |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 579 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 580 | # Pre-scan the patch looking for any __setup documentation. |
| 581 | my @setup_docs = (); |
| 582 | my $setup_docs = 0; |
| 583 | foreach my $line (@lines) { |
| 584 | if ($line=~/^\+\+\+\s+(\S+)/) { |
| 585 | $setup_docs = 0; |
| 586 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { |
| 587 | $setup_docs = 1; |
| 588 | } |
| 589 | next; |
| 590 | } |
| 591 | |
| 592 | if ($setup_docs && $line =~ /^\+/) { |
| 593 | push(@setup_docs, $line); |
| 594 | } |
| 595 | } |
| 596 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 597 | $prefix = ''; |
| 598 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 599 | foreach my $line (@lines) { |
| 600 | $linenr++; |
| 601 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 602 | my $rawline = $line; |
| 603 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 604 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 605 | #extract the filename as it passes |
| 606 | if ($line=~/^\+\+\+\s+(\S+)/) { |
| 607 | $realfile=$1; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 608 | $realfile =~ s@^[^/]*/@@; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 609 | $in_comment = 0; |
| 610 | next; |
| 611 | } |
| 612 | #extract the line range in the file after the patch is applied |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 613 | if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 614 | $is_patch = 1; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 615 | $first_line = $linenr + 1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 616 | $in_comment = 0; |
| 617 | $realline=$1-1; |
| 618 | if (defined $2) { |
| 619 | $realcnt=$3+1; |
| 620 | } else { |
| 621 | $realcnt=1+1; |
| 622 | } |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 623 | $prev_values = 'N'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 624 | next; |
| 625 | } |
| 626 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 627 | # track the line number as we move through the hunk, note that |
| 628 | # new versions of GNU diff omit the leading space on completely |
| 629 | # blank context lines so we need to count that too. |
| 630 | if ($line =~ /^( |\+|$)/) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 631 | $realline++; |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 632 | $realcnt-- if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 633 | |
| 634 | # track any sort of multi-line comment. Obviously if |
| 635 | # the added text or context do not include the whole |
| 636 | # comment we will not see it. Such is life. |
| 637 | # |
| 638 | # Guestimate if this is a continuing comment. If this |
| 639 | # is the start of a diff block and this line starts |
| 640 | # ' *' then it is very likely a comment. |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 641 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 642 | $in_comment = 1; |
| 643 | } |
| 644 | if ($line =~ m@/\*@) { |
| 645 | $in_comment = 1; |
| 646 | } |
| 647 | if ($line =~ m@\*/@) { |
| 648 | $in_comment = 0; |
| 649 | } |
| 650 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 651 | # Measure the line length and indent. |
| 652 | ($length, $indent) = line_stats($line); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 653 | |
| 654 | # Track the previous line. |
| 655 | ($prevline, $stashline) = ($stashline, $line); |
| 656 | ($previndent, $stashindent) = ($stashindent, $indent); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 657 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 658 | } elsif ($realcnt == 1) { |
| 659 | $realcnt--; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | #make up the handle for any error we report on this line |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 663 | $here = "#$linenr: " if (!$file); |
| 664 | $here = "#$realline: " if ($file); |
Randy Dunlap | 389834b | 2007-06-08 13:47:03 -0700 | [diff] [blame] | 665 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 666 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 667 | my $hereline = "$here\n$line\n"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 668 | my $herecurr = "$here\n$line\n"; |
| 669 | my $hereprev = "$here\n$prevline\n$line\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 670 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 671 | $prefix = "$filename:$realline: " if ($emacs && $file); |
| 672 | $prefix = "$filename:$linenr: " if ($emacs && !$file); |
| 673 | $cnt_lines++ if ($realcnt != 0); |
| 674 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 675 | #check the patch for a signoff: |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 676 | if ($line =~ /^\s*signed-off-by:/i) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 677 | # This is a signoff, if ugly, so do not double report. |
| 678 | $signoff++; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 679 | if (!($line =~ /^\s*Signed-off-by:/)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 680 | WARN("Signed-off-by: is the preferred form\n" . |
| 681 | $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 682 | } |
| 683 | if ($line =~ /^\s*signed-off-by:\S/i) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 684 | WARN("need space after Signed-off-by:\n" . |
| 685 | $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 689 | # Check for wrappage within a valid hunk of the file |
| 690 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |$)}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 691 | ERROR("patch seems to be corrupt (line wrapped?)\n" . |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 692 | $herecurr) if (!$emitted_corrupt++); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
| 696 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
| 697 | !($line =~ m/^( |
| 698 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
| 699 | | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
| 700 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
| 701 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte |
| 702 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates |
| 703 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 |
| 704 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
| 705 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
| 706 | )*$/x )) { |
| 707 | ERROR("Invalid UTF-8\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 708 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 709 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 710 | #ignore lines being removed |
| 711 | if ($line=~/^-/) {next;} |
| 712 | |
| 713 | # check we are in a valid source file if not then ignore this hunk |
| 714 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 715 | |
| 716 | #trailing whitespace |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 717 | if ($line =~ /^\+.*\015/) { |
| 718 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
| 719 | ERROR("DOS line endings\n" . $herevet); |
| 720 | |
| 721 | } elsif ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 722 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
| 723 | ERROR("trailing whitespace\n" . $herevet); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 724 | } |
| 725 | #80 column limit |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 726 | if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 727 | WARN("line over 80 characters\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | # check we are in a valid source file *.[hc] if not then ignore this hunk |
| 731 | next if ($realfile !~ /\.[hc]$/); |
| 732 | |
| 733 | # at the beginning of a line any tabs must come first and anything |
| 734 | # more than 8 must use tabs. |
| 735 | if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 736 | my $herevet = "$here\n" . cat_vet($line) . "\n"; |
| 737 | ERROR("use tabs not spaces\n" . $herevet); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 740 | # Remove comments from the line before processing. |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 741 | my $comment_edge = ($line =~ s@/\*.*\*/@@g) + |
| 742 | ($line =~ s@/\*.*@@) + |
| 743 | ($line =~ s@^(.).*\*/@$1@); |
| 744 | |
| 745 | # The rest of our checks refer specifically to C style |
| 746 | # only apply those _outside_ comments. Only skip |
| 747 | # lines in the middle of comments. |
| 748 | next if (!$comment_edge && $in_comment); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 749 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 750 | # Standardise the strings and chars within the input to simplify matching. |
| 751 | $line = sanitise_line($line); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 752 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 753 | # Check for potential 'bare' types |
| 754 | if ($realcnt && |
| 755 | $line !~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?$Type\b/ && |
| 756 | $line !~ /$Ident:\s*$/ && |
| 757 | $line !~ /^.\s*$Ident\s*\(/ && |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 758 | # definitions in global scope can only start with types |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 759 | ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?($Ident)\b/ || |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 760 | # declarations always start with types |
| 761 | $line =~ /^.\s*(?:$Storage\s+)?($Ident)\b\s*\**\s*$Ident\s*(?:;|=)/) || |
| 762 | # any (foo ... *) is a pointer cast, and foo is a type |
| 763 | $line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/) { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 764 | my $possible = $1; |
| 765 | if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ && |
| 766 | $possible ne 'goto' && $possible ne 'return' && |
| 767 | $possible ne 'struct' && $possible ne 'enum' && |
| 768 | $possible ne 'case' && $possible ne 'else' && |
| 769 | $possible ne 'typedef') { |
| 770 | #print "POSSIBLE<$possible>\n"; |
| 771 | push(@bare, $possible); |
| 772 | my $bare = join("|", @bare); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 773 | $Bare = '|' . qr{ |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 774 | \b(?:$bare)\b |
| 775 | (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? |
| 776 | (?:\s+$Sparse)* |
| 777 | }x; |
| 778 | } |
| 779 | } |
| 780 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 781 | # |
| 782 | # Checks which may be anchored in the context. |
| 783 | # |
| 784 | |
| 785 | # Check for switch () and associated case and default |
| 786 | # statements should be at the same indent. |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 787 | if ($line=~/\bswitch\s*\(.*\)/) { |
| 788 | my $err = ''; |
| 789 | my $sep = ''; |
| 790 | my @ctx = ctx_block_outer($linenr, $realcnt); |
| 791 | shift(@ctx); |
| 792 | for my $ctx (@ctx) { |
| 793 | my ($clen, $cindent) = line_stats($ctx); |
| 794 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && |
| 795 | $indent != $cindent) { |
| 796 | $err .= "$sep$ctx\n"; |
| 797 | $sep = ''; |
| 798 | } else { |
| 799 | $sep = "[...]\n"; |
| 800 | } |
| 801 | } |
| 802 | if ($err ne '') { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 803 | ERROR("switch and case should be at the same indent\n$hereline$err"); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
| 807 | # if/while/etc brace do not go on next line, unless defining a do while loop, |
| 808 | # or if that brace on the next line is for something else |
| 809 | if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 810 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 811 | my $ctx_ln = $linenr + $#ctx + 1; |
| 812 | my $ctx_cnt = $realcnt - $#ctx - 1; |
| 813 | my $ctx = join("\n", @ctx); |
| 814 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 815 | # Skip over any removed lines in the context following statement. |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 816 | while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) { |
| 817 | $ctx_ln++; |
| 818 | $ctx_cnt--; |
| 819 | } |
| 820 | ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>"; |
| 821 | |
| 822 | if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 823 | ERROR("That open brace { should be on the previous line\n" . |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 824 | "$here\n$ctx\n$lines[$ctx_ln - 1]"); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 825 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 826 | if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) { |
| 827 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); |
| 828 | if ($nindent > $indent) { |
| 829 | WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" . |
| 830 | "$here\n$ctx\n$lines[$ctx_ln - 1]"); |
| 831 | } |
| 832 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 835 | # Track the 'values' across context and added lines. |
| 836 | my $opline = $line; $opline =~ s/^./ /; |
| 837 | my $curr_values = annotate_values($opline . "\n", $prev_values); |
| 838 | $curr_values = $prev_values . $curr_values; |
| 839 | #warn "--> $opline\n"; |
| 840 | #warn "--> $curr_values ($prev_values)\n"; |
| 841 | $prev_values = substr($curr_values, -1); |
| 842 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 843 | #ignore lines not being added |
| 844 | if ($line=~/^[^\+]/) {next;} |
| 845 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 846 | # TEST: allow direct testing of the type matcher. |
| 847 | if ($tst_type && $line =~ /^.$Declare$/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 848 | ERROR("TEST: is type $Declare\n" . $herecurr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 849 | next; |
| 850 | } |
| 851 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 852 | # check for initialisation to aggregates open brace on the next line |
| 853 | if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ && |
| 854 | $line =~ /^.\s*{/) { |
| 855 | ERROR("That open brace { should be on the previous line\n" . $hereprev); |
| 856 | } |
| 857 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 858 | # |
| 859 | # Checks which are anchored on the added line. |
| 860 | # |
| 861 | |
| 862 | # check for malformed paths in #include statements (uses RAW line) |
| 863 | if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) { |
| 864 | my $path = $1; |
| 865 | if ($path =~ m{//}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 866 | ERROR("malformed #include filename\n" . |
| 867 | $herecurr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 868 | } |
| 869 | # Sanitise this special form of string. |
| 870 | $path = 'X' x length($path); |
| 871 | $line =~ s{\<.*\>}{<$path>}; |
| 872 | } |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 873 | |
| 874 | # no C99 // comments |
| 875 | if ($line =~ m{//}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 876 | ERROR("do not use C99 // comments\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 877 | } |
| 878 | # Remove C99 comments. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 879 | $line =~ s@//.*@@; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 880 | $opline =~ s@//.*@@; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 881 | |
| 882 | #EXPORT_SYMBOL should immediately follow its function closing }. |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 883 | if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || |
| 884 | ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { |
| 885 | my $name = $1; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 886 | if (($prevline !~ /^}/) && |
| 887 | ($prevline !~ /^\+}/) && |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 888 | ($prevline !~ /^ }/) && |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 889 | ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 890 | WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 894 | # check for external initialisers. |
| 895 | if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) { |
| 896 | ERROR("do not initialise externals to 0 or NULL\n" . |
| 897 | $herecurr); |
| 898 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 899 | # check for static initialisers. |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 900 | if ($line =~ /\s*static\s.*=\s*(0|NULL);/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 901 | ERROR("do not initialise statics to 0 or NULL\n" . |
| 902 | $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 905 | # check for new typedefs, only function parameters and sparse annotations |
| 906 | # make sense. |
| 907 | if ($line =~ /\btypedef\s/ && |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 908 | $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ && |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 909 | $line !~ /\b__bitwise(?:__|)\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 910 | WARN("do not add new typedefs\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | # * goes on variable not on type |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 914 | if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 915 | ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" . |
| 916 | $herecurr); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 917 | |
| 918 | } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 919 | ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" . |
| 920 | $herecurr); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 921 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 922 | } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 923 | ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" . |
| 924 | $herecurr); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 925 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 926 | } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 927 | ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" . |
| 928 | $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | # # no BUG() or BUG_ON() |
| 932 | # if ($line =~ /\b(BUG|BUG_ON)\b/) { |
| 933 | # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; |
| 934 | # print "$herecurr"; |
| 935 | # $clean = 0; |
| 936 | # } |
| 937 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 938 | # printk should use KERN_* levels. Note that follow on printk's on the |
| 939 | # same line do not need a level, so we use the current block context |
| 940 | # to try and find and validate the current printk. In summary the current |
| 941 | # printk includes all preceeding printk's which have no newline on the end. |
| 942 | # we assume the first bad printk is the one to report. |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 943 | if ($line =~ /\bprintk\((?!KERN_)\s*"/) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 944 | my $ok = 0; |
| 945 | for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { |
| 946 | #print "CHECK<$lines[$ln - 1]\n"; |
| 947 | # we have a preceeding printk if it ends |
| 948 | # with "\n" ignore it, else it is to blame |
| 949 | if ($lines[$ln - 1] =~ m{\bprintk\(}) { |
| 950 | if ($rawlines[$ln - 1] !~ m{\\n"}) { |
| 951 | $ok = 1; |
| 952 | } |
| 953 | last; |
| 954 | } |
| 955 | } |
| 956 | if ($ok == 0) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 957 | WARN("printk() should include KERN_ facility level\n" . $herecurr); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 958 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 959 | } |
| 960 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 961 | # function brace can't be on same line, except for #defines of do while, |
| 962 | # or if closed on same line |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 963 | if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 964 | !($line=~/\#define.*do\s{/) and !($line=~/}/)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 965 | 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] | 966 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 967 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 968 | # check for spaces between functions and their parentheses. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 969 | while ($line =~ /($Ident)\s+\(/g) { |
| 970 | if ($1 !~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case)$/ && |
| 971 | $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) { |
| 972 | WARN("no space between function name and open parenthesis '('\n" . $herecurr); |
| 973 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 974 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 975 | # Check operator spacing. |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 976 | if (!($line=~/\#\s*include/)) { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 977 | my $ops = qr{ |
| 978 | <<=|>>=|<=|>=|==|!=| |
| 979 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
| 980 | =>|->|<<|>>|<|>|=|!|~| |
| 981 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/ |
| 982 | }x; |
| 983 | my @elements = split(/($ops|;)/, $opline); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 984 | my $off = 0; |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 985 | |
| 986 | my $blank = copy_spacing($opline); |
| 987 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 988 | for (my $n = 0; $n < $#elements; $n += 2) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 989 | $off += length($elements[$n]); |
| 990 | |
| 991 | my $a = ''; |
| 992 | $a = 'V' if ($elements[$n] ne ''); |
| 993 | $a = 'W' if ($elements[$n] =~ /\s$/); |
| 994 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); |
| 995 | $a = 'O' if ($elements[$n] eq ''); |
| 996 | $a = 'E' if ($elements[$n] eq '' && $n == 0); |
| 997 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 998 | my $op = $elements[$n + 1]; |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 999 | |
| 1000 | my $c = ''; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1001 | if (defined $elements[$n + 2]) { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1002 | $c = 'V' if ($elements[$n + 2] ne ''); |
| 1003 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
| 1004 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 1005 | $c = 'O' if ($elements[$n + 2] eq ''); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1006 | $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1007 | } else { |
| 1008 | $c = 'E'; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1011 | # Pick up the preceeding and succeeding characters. |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1012 | my $ca = substr($opline, 0, $off); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1013 | my $cc = ''; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1014 | if (length($opline) >= ($off + length($elements[$n + 1]))) { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1015 | $cc = substr($opline, $off + length($elements[$n + 1])); |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1016 | } |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1017 | my $cb = "$ca$;$cc"; |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1018 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1019 | my $ctx = "${a}x${c}"; |
| 1020 | |
| 1021 | my $at = "(ctx:$ctx)"; |
| 1022 | |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1023 | my $ptr = substr($blank, 0, $off) . "^"; |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1024 | my $hereptr = "$hereline$ptr\n"; |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1025 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1026 | # Classify operators into binary, unary, or |
| 1027 | # definitions (* only) where they have more |
| 1028 | # than one mode. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1029 | my $op_type = substr($curr_values, $off + 1, 1); |
| 1030 | my $op_left = substr($curr_values, $off, 1); |
| 1031 | my $is_unary; |
| 1032 | if ($op_type eq 'T') { |
| 1033 | $is_unary = 2; |
| 1034 | } elsif ($op_left eq 'V') { |
| 1035 | $is_unary = 0; |
| 1036 | } else { |
| 1037 | $is_unary = 1; |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1038 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1039 | #if ($op eq '-' || $op eq '&' || $op eq '*') { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1040 | # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n"; |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1041 | #} |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1042 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1043 | # ; should have either the end of line or a space or \ after it |
| 1044 | if ($op eq ';') { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1045 | if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ && |
| 1046 | $cc !~ /^;/) { |
| 1047 | ERROR("need space after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | # // is a comment |
| 1051 | } elsif ($op eq '//') { |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1052 | |
| 1053 | # -> should have no spaces |
| 1054 | } elsif ($op eq '->') { |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1055 | if ($ctx =~ /Wx.|.xW/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1056 | ERROR("no spaces around that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | # , must have a space on the right. |
| 1060 | } elsif ($op eq ',') { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1061 | if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1062 | ERROR("need space after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1065 | # '*' as part of a type definition -- reported already. |
| 1066 | } elsif ($op eq '*' && $is_unary == 2) { |
| 1067 | #warn "'*' is part of type\n"; |
| 1068 | |
| 1069 | # unary operators should have a space before and |
| 1070 | # none after. May be left adjacent to another |
| 1071 | # unary operator, or a cast |
| 1072 | } elsif ($op eq '!' || $op eq '~' || |
| 1073 | ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) { |
| 1074 | if ($ctx !~ /[WEB]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1075 | ERROR("need space before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1076 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1077 | if ($ctx =~ /.xW/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1078 | ERROR("no space after that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | # unary ++ and unary -- are allowed no space on one side. |
| 1082 | } elsif ($op eq '++' or $op eq '--') { |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1083 | if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1084 | ERROR("need space one side of that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1085 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1086 | if ($ctx =~ /Wx./ && $cc =~ /^;/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1087 | ERROR("no space before that '$op' $at\n" . $hereptr); |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1088 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1089 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1090 | # << and >> may either have or not have spaces both sides |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1091 | } elsif ($op eq '<<' or $op eq '>>' or |
| 1092 | $op eq '&' or $op eq '^' or $op eq '|' or |
| 1093 | $op eq '+' or $op eq '-' or |
| 1094 | $op eq '*' or $op eq '/') |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1095 | { |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1096 | if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1097 | ERROR("need consistent spacing around '$op' $at\n" . |
| 1098 | $hereptr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | # All the others need spaces both sides. |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1102 | } elsif ($ctx !~ /[EW]x[WE]/) { |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1103 | # Ignore email addresses <foo@bar> |
| 1104 | if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) && |
| 1105 | !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) { |
| 1106 | ERROR("need spaces around that '$op' $at\n" . $hereptr); |
| 1107 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1108 | } |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1109 | $off += length($elements[$n + 1]); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1113 | # check for multiple assignments |
| 1114 | if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1115 | CHK("multiple assignments should be avoided\n" . $herecurr); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1118 | ## # check for multiple declarations, allowing for a function declaration |
| 1119 | ## # continuation. |
| 1120 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 1121 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 1122 | ## |
| 1123 | ## # Remove any bracketed sections to ensure we do not |
| 1124 | ## # falsly report the parameters of functions. |
| 1125 | ## my $ln = $line; |
| 1126 | ## while ($ln =~ s/\([^\(\)]*\)//g) { |
| 1127 | ## } |
| 1128 | ## if ($ln =~ /,/) { |
| 1129 | ## WARN("declaring multiple variables together should be avoided\n" . $herecurr); |
| 1130 | ## } |
| 1131 | ## } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1132 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1133 | #need space before brace following if, while, etc |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1134 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || |
| 1135 | $line =~ /do{/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1136 | ERROR("need a space before the open brace '{'\n" . $herecurr); |
| 1137 | } |
| 1138 | |
| 1139 | # closing brace should have a space following it when it has anything |
| 1140 | # on the line |
| 1141 | if ($line =~ /}(?!(?:,|;|\)))\S/) { |
| 1142 | ERROR("need a space after that close brace '}'\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1145 | # check spacing on square brackets |
| 1146 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { |
| 1147 | ERROR("no space after that open square bracket '['\n" . $herecurr); |
| 1148 | } |
| 1149 | if ($line =~ /\s\]/) { |
| 1150 | ERROR("no space before that close square bracket ']'\n" . $herecurr); |
| 1151 | } |
| 1152 | |
| 1153 | # check spacing on paretheses |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1154 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && |
| 1155 | $line !~ /for\s*\(\s+;/) { |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1156 | ERROR("no space after that open parenthesis '('\n" . $herecurr); |
| 1157 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1158 | if ($line =~ /\s\)/ && $line !~ /^.\s*\)/ && |
| 1159 | $line !~ /for\s*\(.*;\s+\)/) { |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1160 | ERROR("no space before that close parenthesis ')'\n" . $herecurr); |
| 1161 | } |
| 1162 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1163 | #goto labels aren't indented, allow a single space however |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1164 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1165 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1166 | WARN("labels should not be indented\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | # Need a space before open parenthesis after if, while etc |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1170 | if ($line=~/\b(if|while|for|switch)\(/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1171 | ERROR("need a space before the open parenthesis '('\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | # Check for illegal assignment in if conditional. |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1175 | if ($line=~/\bif\s*\(.*[^<>!=]=[^=]/) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1176 | #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1177 | ERROR("do not use assignment in if condition\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | # Check for }<nl>else {, these must be at the same |
| 1181 | # indent level to be relevant to each other. |
| 1182 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and |
| 1183 | $previndent == $indent) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1184 | ERROR("else should follow close brace '}'\n" . $hereprev); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1187 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new |
| 1188 | # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { |
| 1189 | # print "No studly caps, use _\n"; |
| 1190 | # print "$herecurr"; |
| 1191 | # $clean = 0; |
| 1192 | # } |
| 1193 | |
| 1194 | #no spaces allowed after \ in define |
| 1195 | if ($line=~/\#define.*\\\s$/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1196 | WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1199 | #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) |
| 1200 | if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) { |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1201 | my $checkfile = "$root/include/linux/$1.h"; |
| 1202 | if (-f $checkfile && $1 ne 'irq.h') { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1203 | CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" . |
| 1204 | $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1208 | # if and else should not have general statements after it |
| 1209 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ && |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1210 | $1 !~ /^\s*(?:\sif|{|\\|$)/) { |
| 1211 | ERROR("trailing statements should be on next line\n" . $herecurr); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1214 | # multi-statement macros should be enclosed in a do while loop, grab the |
| 1215 | # first statement and ensure its the whole macro if its not enclosed |
| 1216 | # in a known goot container |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1217 | if ($prevline =~ /\#define.*\\/ && |
| 1218 | $prevline !~/(?:do\s+{|\(\{|\{)/ && |
| 1219 | $line !~ /(?:do\s+{|\(\{|\{)/ && |
| 1220 | $line !~ /^.\s*$Declare\s/) { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1221 | # Grab the first statement, if that is the entire macro |
| 1222 | # its ok. This may start either on the #define line |
| 1223 | # or the one below. |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1224 | my $ln = $linenr; |
| 1225 | my $cnt = $realcnt; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1226 | my $off = 0; |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1227 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1228 | # If the macro starts on the define line start |
| 1229 | # grabbing the statement after the identifier |
| 1230 | $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$}; |
| 1231 | ##print "1<$1> 2<$2>\n"; |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1232 | if (defined $2 && $2 ne '') { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1233 | $off = length($1); |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1234 | $ln--; |
| 1235 | $cnt++; |
| 1236 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1237 | my @ctx = ctx_statement($ln, $cnt, $off); |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1238 | my $ctx_ln = $ln + $#ctx + 1; |
| 1239 | my $ctx = join("\n", @ctx); |
| 1240 | |
| 1241 | # Pull in any empty extension lines. |
| 1242 | while ($ctx =~ /\\$/ && |
| 1243 | $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) { |
| 1244 | $ctx .= $lines[$ctx_ln - 1]; |
| 1245 | $ctx_ln++; |
| 1246 | } |
Andy Whitcroft | d8aaf12 | 2007-06-23 17:16:44 -0700 | [diff] [blame] | 1247 | |
| 1248 | if ($ctx =~ /\\$/) { |
| 1249 | if ($ctx =~ /;/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1250 | 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] | 1251 | } else { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1252 | 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] | 1253 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1254 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1257 | # check for redundant bracing round if etc |
| 1258 | if ($line =~ /\b(if|while|for|else)\b/) { |
| 1259 | # Locate the end of the opening statement. |
| 1260 | my @control = ctx_statement($linenr, $realcnt, 0); |
| 1261 | my $nr = $linenr + (scalar(@control) - 1); |
| 1262 | my $cnt = $realcnt - (scalar(@control) - 1); |
| 1263 | |
| 1264 | my $off = $realcnt - $cnt; |
| 1265 | #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n"; |
| 1266 | |
| 1267 | # If this is is a braced statement group check it |
| 1268 | if ($lines[$nr - 1] =~ /{\s*$/) { |
| 1269 | my ($lvl, @block) = ctx_block_level($nr, $cnt); |
| 1270 | |
| 1271 | my $stmt = join(' ', @block); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1272 | $stmt =~ s/(^[^{]*){//; |
| 1273 | my $before = $1; |
| 1274 | $stmt =~ s/}([^}]*$)//; |
| 1275 | my $after = $1; |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1276 | |
| 1277 | #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n"; |
| 1278 | #print "stmt<$stmt>\n\n"; |
| 1279 | |
| 1280 | # Count the ;'s if there is fewer than two |
| 1281 | # then there can only be one statement, |
| 1282 | # if there is a brace inside we cannot |
| 1283 | # trivially detect if its one statement. |
| 1284 | # Also nested if's often require braces to |
| 1285 | # disambiguate the else binding so shhh there. |
| 1286 | my @semi = ($stmt =~ /;/g); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1287 | push(@semi, "/**/") if ($stmt =~ m@/\*@); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1288 | ##print "semi<" . scalar(@semi) . ">\n"; |
| 1289 | if ($lvl == 0 && scalar(@semi) < 2 && |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1290 | $stmt !~ /{/ && $stmt !~ /\bif\b/ && |
| 1291 | $before !~ /}/ && $after !~ /{/) { |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1292 | my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n"; |
| 1293 | shift(@block); |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1294 | WARN("braces {} are not necessary for single statement blocks\n" . $herectx); |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1295 | } |
| 1296 | } |
| 1297 | } |
| 1298 | |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1299 | # don't include deprecated include files (uses RAW line) |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1300 | for my $inc (@dep_includes) { |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1301 | if ($rawline =~ m@\#\s*include\s*\<$inc>@) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1302 | 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] | 1303 | } |
| 1304 | } |
| 1305 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1306 | # don't use deprecated functions |
| 1307 | for my $func (@dep_functions) { |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1308 | if ($line =~ /\b$func\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1309 | 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] | 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | # no volatiles please |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1314 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; |
| 1315 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1316 | 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] | 1317 | } |
| 1318 | |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1319 | # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated |
| 1320 | if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) { |
| 1321 | ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr); |
| 1322 | } |
| 1323 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1324 | # warn about #if 0 |
| 1325 | if ($line =~ /^.#\s*if\s+0\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1326 | CHK("if this code is redundant consider removing it\n" . |
| 1327 | $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1330 | # check for needless kfree() checks |
| 1331 | if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { |
| 1332 | my $expr = $1; |
| 1333 | if ($line =~ /\bkfree\(\Q$expr\E\);/) { |
| 1334 | WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev); |
| 1335 | } |
| 1336 | } |
| 1337 | |
Andy Whitcroft | 00df344 | 2007-06-08 13:47:06 -0700 | [diff] [blame] | 1338 | # warn about #ifdefs in C files |
| 1339 | # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { |
| 1340 | # print "#ifdef in C files should be avoided\n"; |
| 1341 | # print "$herecurr"; |
| 1342 | # $clean = 0; |
| 1343 | # } |
| 1344 | |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1345 | # warn about spacing in #ifdefs |
| 1346 | if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) { |
| 1347 | ERROR("exactly one space required after that #$1\n" . $herecurr); |
| 1348 | } |
| 1349 | |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1350 | # check for spinlock_t definitions without a comment. |
| 1351 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { |
| 1352 | my $which = $1; |
| 1353 | if (!ctx_has_comment($first_line, $linenr)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1354 | CHK("$1 definition without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | # check for memory barriers without a comment. |
| 1358 | if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { |
| 1359 | if (!ctx_has_comment($first_line, $linenr)) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1360 | CHK("memory barrier without comment\n" . $herecurr); |
Andy Whitcroft | 4a0df2e | 2007-06-08 13:46:39 -0700 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | # check of hardware specific defines |
Andy Whitcroft | 22f2a2e | 2007-08-10 13:01:03 -0700 | [diff] [blame] | 1364 | if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1365 | CHK("architecture specific defines should be avoided\n" . $herecurr); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1366 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1367 | |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1368 | # check the location of the inline attribute, that it is between |
| 1369 | # storage class and type. |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1370 | if ($line =~ /\b$Type\s+$Inline\b/ || |
| 1371 | $line =~ /\b$Inline\s+$Storage\b/) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1372 | ERROR("inline keyword should sit between storage class and type\n" . $herecurr); |
| 1373 | } |
| 1374 | |
| 1375 | # check for new externs in .c files. |
| 1376 | if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) { |
| 1377 | WARN("externs should be avoided in .c files\n" . $herecurr); |
| 1378 | } |
| 1379 | |
| 1380 | # checks for new __setup's |
| 1381 | if ($rawline =~ /\b__setup\("([^"]*)"/) { |
| 1382 | my $name = $1; |
| 1383 | |
| 1384 | if (!grep(/$name/, @setup_docs)) { |
| 1385 | CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); |
| 1386 | } |
Andy Whitcroft | 653d487 | 2007-06-23 17:16:34 -0700 | [diff] [blame] | 1387 | } |
Andy Whitcroft | 9c0ca6f | 2007-10-16 23:29:38 -0700 | [diff] [blame] | 1388 | |
| 1389 | # check for pointless casting of kmalloc return |
| 1390 | if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) { |
| 1391 | WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); |
| 1392 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | if ($chk_patch && !$is_patch) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1396 | ERROR("Does not appear to be a unified-diff format patch\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1397 | } |
| 1398 | if ($is_patch && $chk_signoff && $signoff == 0) { |
Andy Whitcroft | de7d4f0 | 2007-07-15 23:37:22 -0700 | [diff] [blame] | 1399 | ERROR("Missing Signed-off-by: line(s)\n"); |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1402 | if ($clean == 0 && ($chk_patch || $is_patch)) { |
| 1403 | print report_dump(); |
Andy Whitcroft | 6c72ffa | 2007-10-18 03:05:08 -0700 | [diff] [blame] | 1404 | if ($quiet < 2) { |
| 1405 | print "total: $cnt_error errors, $cnt_warn warnings, " . |
| 1406 | (($check)? "$cnt_chk checks, " : "") . |
| 1407 | "$cnt_lines lines checked\n"; |
| 1408 | } |
Andy Whitcroft | f0a594c | 2007-07-19 01:48:34 -0700 | [diff] [blame] | 1409 | } |
Andy Whitcroft | 0a920b5 | 2007-06-01 00:46:48 -0700 | [diff] [blame] | 1410 | if ($clean == 1 && $quiet == 0) { |
| 1411 | print "Your patch has no obvious style problems and is ready for submission.\n" |
| 1412 | } |
| 1413 | if ($clean == 0 && $quiet == 0) { |
| 1414 | print "Your patch has style problems, please review. If any of these errors\n"; |
| 1415 | print "are false positives report them to the maintainer, see\n"; |
| 1416 | print "CHECKPATCH in MAINTAINERS.\n"; |
| 1417 | } |
| 1418 | return $clean; |
| 1419 | } |