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