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