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