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