blob: b6bbbcdc557eb05d7eeb55f11887a34915f722b1 [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
2# (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004# (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
7use strict;
8
9my $P = $0;
Andy Whitcroft00df3442007-06-08 13:47:06 -070010$P =~ s@.*/@@g;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070011
Andy Whitcroft9c9ba342008-04-29 00:59:33 -070012my $V = '0.18';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070013
14use Getopt::Long qw(:config no_auto_abbrev);
15
16my $quiet = 0;
17my $tree = 1;
18my $chk_signoff = 1;
19my $chk_patch = 1;
Andy Whitcroft653d4872007-06-23 17:16:34 -070020my $tst_type = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -070021my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070022my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080023my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070024my $file = 0;
25my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080026my $summary = 1;
27my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080028my $summary_file = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070029my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080030my %debug;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070031GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070032 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -070033 'tree!' => \$tree,
34 'signoff!' => \$chk_signoff,
35 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070036 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -080037 'terse!' => \$terse,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070038 'file!' => \$file,
39 'subjective!' => \$check,
40 'strict!' => \$check,
41 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -080042 'summary!' => \$summary,
43 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -080044 'summary-file!' => \$summary_file,
45
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080046 'debug=s' => \%debug,
Andy Whitcroft13214ad2008-02-08 04:22:03 -080047 'test-type!' => \$tst_type,
Andy Whitcroft773647a2008-03-28 14:15:58 -070048 'test-only=s' => \$tst_only,
Andy Whitcroft0a920b52007-06-01 00:46:48 -070049) or exit;
50
51my $exit = 0;
52
53if ($#ARGV < 0) {
Andy Whitcroft00df3442007-06-08 13:47:06 -070054 print "usage: $P [options] patchfile\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -070055 print "version: $V\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -080056 print "options: -q => quiet\n";
57 print " --no-tree => run without a kernel tree\n";
58 print " --terse => one line per report\n";
59 print " --emacs => emacs compile window format\n";
60 print " --file => check a source file\n";
61 print " --strict => enable more subjective tests\n";
62 print " --root => path to the kernel tree root\n";
63 print " --no-summary => suppress the per-file summary\n";
64 print " --summary-file => include the filename in summary\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -070065 exit(1);
66}
67
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080068my $dbg_values = 0;
69my $dbg_possible = 0;
70for my $key (keys %debug) {
71 eval "\${dbg_$key} = '$debug{$key}';"
72}
73
Andy Whitcroft8905a672007-11-28 16:21:06 -080074if ($terse) {
75 $emacs = 1;
76 $quiet++;
77}
78
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070079if ($tree) {
80 if (defined $root) {
81 if (!top_of_kernel_tree($root)) {
82 die "$P: $root: --root does not point at a valid tree\n";
83 }
84 } else {
85 if (top_of_kernel_tree('.')) {
86 $root = '.';
87 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
88 top_of_kernel_tree($1)) {
89 $root = $1;
90 }
91 }
92
93 if (!defined $root) {
94 print "Must be run from the top-level dir. of a kernel tree\n";
95 exit(2);
96 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -070097}
98
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070099my $emitted_corrupt = 0;
100
101our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
102our $Storage = qr{extern|static|asmlinkage};
103our $Sparse = qr{
104 __user|
105 __kernel|
106 __force|
107 __iomem|
108 __must_check|
109 __init_refok|
Andy Whitcroftcf655042008-03-04 14:28:20 -0800110 __kprobes
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700111 }x;
112our $Attribute = qr{
113 const|
114 __read_mostly|
115 __kprobes|
116 __(?:mem|cpu|dev|)(?:initdata|init)
117 }x;
118our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700119our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
120our $Lval = qr{$Ident(?:$Member)*};
121
122our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
123our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
124our $Operators = qr{
125 <=|>=|==|!=|
126 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800127 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700128 }x;
129
Andy Whitcroft8905a672007-11-28 16:21:06 -0800130our $NonptrType;
131our $Type;
132our $Declare;
133
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700134our $UTF8 = qr {
135 [\x09\x0A\x0D\x20-\x7E] # ASCII
136 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
137 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
138 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
139 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
140 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
141 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
142 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
143}x;
144
Andy Whitcroft8905a672007-11-28 16:21:06 -0800145our @typeList = (
146 qr{void},
147 qr{char},
148 qr{short},
149 qr{int},
150 qr{long},
151 qr{unsigned},
152 qr{float},
153 qr{double},
154 qr{bool},
155 qr{long\s+int},
156 qr{long\s+long},
157 qr{long\s+long\s+int},
158 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
159 qr{struct\s+$Ident},
160 qr{union\s+$Ident},
161 qr{enum\s+$Ident},
162 qr{${Ident}_t},
163 qr{${Ident}_handler},
164 qr{${Ident}_handler_fn},
165);
166
167sub build_types {
168 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
169 $NonptrType = qr{
170 \b
171 (?:const\s+)?
172 (?:unsigned\s+)?
Andy Whitcroftcf655042008-03-04 14:28:20 -0800173 (?:
174 $all|
175 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)
176 )
Andy Whitcroft8905a672007-11-28 16:21:06 -0800177 (?:\s+$Sparse|\s+const)*
178 \b
179 }x;
180 $Type = qr{
181 \b$NonptrType\b
182 (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800183 (?:\s+$Inline|\s+$Sparse|\s+$Attribute)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800184 }x;
185 $Declare = qr{(?:$Storage\s+)?$Type};
186}
187build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700188
189$chk_signoff = 0 if ($file);
190
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700191my @dep_includes = ();
192my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700193my $removal = "Documentation/feature-removal-schedule.txt";
194if ($tree && -f "$root/$removal") {
195 open(REMOVE, "<$root/$removal") ||
196 die "$P: $removal: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700197 while (<REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700198 if (/^Check:\s+(.*\S)/) {
199 for my $entry (split(/[, ]+/, $1)) {
200 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700201 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700202
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700203 } elsif ($entry !~ m@/@) {
204 push(@dep_functions, $entry);
205 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700206 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700207 }
208 }
209}
210
Andy Whitcroft00df3442007-06-08 13:47:06 -0700211my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800212my @lines = ();
213my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700214for my $filename (@ARGV) {
215 if ($file) {
216 open(FILE, "diff -u /dev/null $filename|") ||
217 die "$P: $filename: diff failed - $!\n";
218 } else {
219 open(FILE, "<$filename") ||
220 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700221 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800222 if ($filename eq '-') {
223 $vname = 'Your patch';
224 } else {
225 $vname = $filename;
226 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700227 while (<FILE>) {
228 chomp;
229 push(@rawlines, $_);
230 }
231 close(FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800232 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700233 $exit = 1;
234 }
235 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800236 @lines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700237}
238
239exit($exit);
240
241sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700242 my ($root) = @_;
243
244 my @tree_check = (
245 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
246 "README", "Documentation", "arch", "include", "drivers",
247 "fs", "init", "ipc", "kernel", "lib", "scripts",
248 );
249
250 foreach my $check (@tree_check) {
251 if (! -e $root . '/' . $check) {
252 return 0;
253 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700254 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700255 return 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700256}
257
258sub expand_tabs {
259 my ($str) = @_;
260
261 my $res = '';
262 my $n = 0;
263 for my $c (split(//, $str)) {
264 if ($c eq "\t") {
265 $res .= ' ';
266 $n++;
267 for (; ($n % 8) != 0; $n++) {
268 $res .= ' ';
269 }
270 next;
271 }
272 $res .= $c;
273 $n++;
274 }
275
276 return $res;
277}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700278sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700279 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700280 return $res;
281}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700282
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700283sub line_stats {
284 my ($line) = @_;
285
286 # Drop the diff line leader and expand tabs
287 $line =~ s/^.//;
288 $line = expand_tabs($line);
289
290 # Pick the indent from the front of the line.
291 my ($white) = ($line =~ /^(\s*)/);
292
293 return (length($line), length($white));
294}
295
Andy Whitcroft773647a2008-03-28 14:15:58 -0700296my $sanitise_quote = '';
297
298sub sanitise_line_reset {
299 my ($in_comment) = @_;
300
301 if ($in_comment) {
302 $sanitise_quote = '*/';
303 } else {
304 $sanitise_quote = '';
305 }
306}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700307sub sanitise_line {
308 my ($line) = @_;
309
310 my $res = '';
311 my $l = '';
312
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800313 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700314 my $off = 0;
315 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700316
Andy Whitcroft773647a2008-03-28 14:15:58 -0700317 # Always copy over the diff marker.
318 $res = substr($line, 0, 1);
319
320 for ($off = 1; $off < length($line); $off++) {
321 $c = substr($line, $off, 1);
322
323 # Comments we are wacking completly including the begin
324 # and end, all to $;.
325 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
326 $sanitise_quote = '*/';
327
328 substr($res, $off, 2, "$;$;");
329 $off++;
330 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800331 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700332 if (substr($line, $off, 2) eq $sanitise_quote) {
333 $sanitise_quote = '';
334 substr($res, $off, 2, "$;$;");
335 $off++;
336 next;
337 }
338
339 # A \ in a string means ignore the next character.
340 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
341 $c eq "\\") {
342 substr($res, $off, 2, 'XX');
343 $off++;
344 next;
345 }
346 # Regular quotes.
347 if ($c eq "'" || $c eq '"') {
348 if ($sanitise_quote eq '') {
349 $sanitise_quote = $c;
350
351 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700352 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700353 } elsif ($sanitise_quote eq $c) {
354 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700355 }
356 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700357
358 #print "SQ:$sanitise_quote\n";
359 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
360 substr($res, $off, 1, $;);
361 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
362 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700363 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700364 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700365 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800366 }
367
368 # The pathname on a #include may be surrounded by '<' and '>'.
369 if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
370 my $clean = 'X' x length($1);
371 $res =~ s@\<.*\>@<$clean>@;
372
373 # The whole of a #error is a string.
374 } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
375 my $clean = 'X' x length($1);
376 $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@;
377 }
378
Andy Whitcroft00df3442007-06-08 13:47:06 -0700379 return $res;
380}
381
Andy Whitcroft8905a672007-11-28 16:21:06 -0800382sub ctx_statement_block {
383 my ($linenr, $remain, $off) = @_;
384 my $line = $linenr - 1;
385 my $blk = '';
386 my $soff = $off;
387 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700388 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800389
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800390 my $loff = 0;
391
Andy Whitcroft8905a672007-11-28 16:21:06 -0800392 my $type = '';
393 my $level = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800394 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800395 my $c;
396 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800397
398 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800399 while (1) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700400 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800401 # If we are about to drop off the end, pull in more
402 # context.
403 if ($off >= $len) {
404 for (; $remain > 0; $line++) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800405 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800406 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800407 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800408 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800409 $len = length($blk);
410 $line++;
411 last;
412 }
413 # Bail if there is no further context.
414 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800415 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800416 last;
417 }
418 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800419 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800420 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800421 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800422
Andy Whitcroft773647a2008-03-28 14:15:58 -0700423 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800424 # Statement ends at the ';' or a close '}' at the
425 # outermost level.
426 if ($level == 0 && $c eq ';') {
427 last;
428 }
429
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800430 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700431 if ($level == 0 && $coff_set == 0 &&
432 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
433 $remainder =~ /^(else)(?:\s|{)/ &&
434 $remainder !~ /^else\s+if\b/) {
435 $coff = $off + length($1) - 1;
436 $coff_set = 1;
437 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
438 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800439 }
440
Andy Whitcroft8905a672007-11-28 16:21:06 -0800441 if (($type eq '' || $type eq '(') && $c eq '(') {
442 $level++;
443 $type = '(';
444 }
445 if ($type eq '(' && $c eq ')') {
446 $level--;
447 $type = ($level != 0)? '(' : '';
448
449 if ($level == 0 && $coff < $soff) {
450 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700451 $coff_set = 1;
452 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800453 }
454 }
455 if (($type eq '' || $type eq '{') && $c eq '{') {
456 $level++;
457 $type = '{';
458 }
459 if ($type eq '{' && $c eq '}') {
460 $level--;
461 $type = ($level != 0)? '{' : '';
462
463 if ($level == 0) {
464 last;
465 }
466 }
467 $off++;
468 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800469 if ($off == $len) {
470 $line++;
471 $remain--;
472 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800473
474 my $statement = substr($blk, $soff, $off - $soff + 1);
475 my $condition = substr($blk, $soff, $coff - $soff + 1);
476
477 #warn "STATEMENT<$statement>\n";
478 #warn "CONDITION<$condition>\n";
479
Andy Whitcroft773647a2008-03-28 14:15:58 -0700480 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800481
482 return ($statement, $condition,
483 $line, $remain + 1, $off - $loff + 1, $level);
484}
485
Andy Whitcroftcf655042008-03-04 14:28:20 -0800486sub statement_lines {
487 my ($stmt) = @_;
488
489 # Strip the diff line prefixes and rip blank lines at start and end.
490 $stmt =~ s/(^|\n)./$1/g;
491 $stmt =~ s/^\s*//;
492 $stmt =~ s/\s*$//;
493
494 my @stmt_lines = ($stmt =~ /\n/g);
495
496 return $#stmt_lines + 2;
497}
498
499sub statement_rawlines {
500 my ($stmt) = @_;
501
502 my @stmt_lines = ($stmt =~ /\n/g);
503
504 return $#stmt_lines + 2;
505}
506
507sub statement_block_size {
508 my ($stmt) = @_;
509
510 $stmt =~ s/(^|\n)./$1/g;
511 $stmt =~ s/^\s*{//;
512 $stmt =~ s/}\s*$//;
513 $stmt =~ s/^\s*//;
514 $stmt =~ s/\s*$//;
515
516 my @stmt_lines = ($stmt =~ /\n/g);
517 my @stmt_statements = ($stmt =~ /;/g);
518
519 my $stmt_lines = $#stmt_lines + 2;
520 my $stmt_statements = $#stmt_statements + 1;
521
522 if ($stmt_lines > $stmt_statements) {
523 return $stmt_lines;
524 } else {
525 return $stmt_statements;
526 }
527}
528
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800529sub ctx_statement_full {
530 my ($linenr, $remain, $off) = @_;
531 my ($statement, $condition, $level);
532
533 my (@chunks);
534
Andy Whitcroftcf655042008-03-04 14:28:20 -0800535 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800536 ($statement, $condition, $linenr, $remain, $off, $level) =
537 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700538 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800539 push(@chunks, [ $condition, $statement ]);
540 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
541 return ($level, $linenr, @chunks);
542 }
543
544 # Pull in the following conditional/block pairs and see if they
545 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800546 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800547 ($statement, $condition, $linenr, $remain, $off, $level) =
548 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800549 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700550 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800551 #print "C: push\n";
552 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800553 }
554
555 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800556}
557
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700558sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700559 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700560 my $line;
561 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700562 my $blk = '';
563 my @o;
564 my @c;
565 my @res = ();
566
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700567 my $level = 0;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700568 for ($line = $start; $remain > 0; $line++) {
569 next if ($rawlines[$line] =~ /^-/);
570 $remain--;
571
572 $blk .= $rawlines[$line];
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700573 foreach my $c (split(//, $rawlines[$line])) {
574 ##print "C<$c>L<$level><$open$close>O<$off>\n";
575 if ($off > 0) {
576 $off--;
577 next;
578 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700579
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700580 if ($c eq $close && $level > 0) {
581 $level--;
582 last if ($level == 0);
583 } elsif ($c eq $open) {
584 $level++;
585 }
586 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700587
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700588 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700589 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700590 }
591
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700592 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700593 }
594
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700595 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700596}
597sub ctx_block_outer {
598 my ($linenr, $remain) = @_;
599
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700600 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
601 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700602}
603sub ctx_block {
604 my ($linenr, $remain) = @_;
605
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700606 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
607 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700608}
609sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700610 my ($linenr, $remain, $off) = @_;
611
612 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
613 return @r;
614}
615sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700616 my ($linenr, $remain) = @_;
617
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700618 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700619}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700620sub ctx_statement_level {
621 my ($linenr, $remain, $off) = @_;
622
623 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
624}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700625
626sub ctx_locate_comment {
627 my ($first_line, $end_line) = @_;
628
629 # Catch a comment on the end of the line itself.
Andy Whitcroft00df3442007-06-08 13:47:06 -0700630 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700631 return $current_comment if (defined $current_comment);
632
633 # Look through the context and try and figure out if there is a
634 # comment.
635 my $in_comment = 0;
636 $current_comment = '';
637 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700638 my $line = $rawlines[$linenr - 1];
639 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700640 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
641 $in_comment = 1;
642 }
643 if ($line =~ m@/\*@) {
644 $in_comment = 1;
645 }
646 if (!$in_comment && $current_comment ne '') {
647 $current_comment = '';
648 }
649 $current_comment .= $line . "\n" if ($in_comment);
650 if ($line =~ m@\*/@) {
651 $in_comment = 0;
652 }
653 }
654
655 chomp($current_comment);
656 return($current_comment);
657}
658sub ctx_has_comment {
659 my ($first_line, $end_line) = @_;
660 my $cmt = ctx_locate_comment($first_line, $end_line);
661
Andy Whitcroft00df3442007-06-08 13:47:06 -0700662 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700663 ##print "CMMT: $cmt\n";
664
665 return ($cmt ne '');
666}
667
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700668sub cat_vet {
669 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700670 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700671
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700672 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700673 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
674 $res .= $1;
675 if ($2 ne '') {
676 $coded = sprintf("^%c", unpack('C', $2) + 64);
677 $res .= $coded;
678 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700679 }
680 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700681
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700682 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700683}
684
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800685my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800686my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800687my @av_paren_type;
688
689sub annotate_reset {
690 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800691 $av_pending = '_';
692 @av_paren_type = ('E');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800693}
694
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700695sub annotate_values {
696 my ($stream, $type) = @_;
697
698 my $res;
699 my $cur = $stream;
700
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800701 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700702
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700703 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700704 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800705 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700706 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700707 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800708 print "WS($1)\n" if ($dbg_values > 1);
709 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800710 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800711 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700712 }
713
Andy Whitcroft8905a672007-11-28 16:21:06 -0800714 } elsif ($cur =~ /^($Type)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800715 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700716 $type = 'T';
717
718 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700719 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800720 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700721 push(@av_paren_type, $type);
722 if ($2 ne '') {
723 $av_pending = 'N';
724 }
725 $type = 'E';
726
727 } elsif ($cur =~ /^(#\s*undef\s*$Ident)/o) {
728 print "UNDEF($1)\n" if ($dbg_values > 1);
729 $av_preprocessor = 1;
730 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700731
Andy Whitcroftcf655042008-03-04 14:28:20 -0800732 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
733 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800734 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800735
736 push(@av_paren_type, $type);
737 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700738 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800739
740 } elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
741 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
742 $av_preprocessor = 1;
743
744 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
745
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700746 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -0800747
748 } elsif ($cur =~ /^(#\s*(?:endif))/o) {
749 print "PRE_END($1)\n" if ($dbg_values > 1);
750
751 $av_preprocessor = 1;
752
753 # Assume all arms of the conditional end as this
754 # one does, and continue as if the #endif was not here.
755 pop(@av_paren_type);
756 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700757 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700758
759 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800760 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700761
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700762 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
763 print "ATTR($1)\n" if ($dbg_values > 1);
764 $av_pending = $type;
765 $type = 'N';
766
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700767 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800768 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700769 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800770 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700771 }
772 $type = 'N';
773
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800774 } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800775 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800776 $av_pending = 'N';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700777 $type = 'N';
778
779 } elsif ($cur =~/^(return|case|else)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800780 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700781 $type = 'N';
782
783 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800784 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800785 push(@av_paren_type, $av_pending);
786 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700787 $type = 'N';
788
789 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800790 my $new_type = pop(@av_paren_type);
791 if ($new_type ne '_') {
792 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800793 print "PAREN('$1') -> $type\n"
794 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700795 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800796 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700797 }
798
799 } elsif ($cur =~ /^($Ident)\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800800 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800801 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700802
803 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800804 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700805 $type = 'V';
806
807 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800808 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700809 $type = 'N';
810
Andy Whitcroftcf655042008-03-04 14:28:20 -0800811 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800812 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800813 $type = 'E';
814
Andy Whitcroftcf655042008-03-04 14:28:20 -0800815 } elsif ($cur =~ /^(;|\?|:|\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800816 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700817 $type = 'N';
818
819 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800820 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700821 if ($1 ne '++' && $1 ne '--') {
822 $type = 'N';
823 }
824
825 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800826 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700827 }
828 if (defined $1) {
829 $cur = substr($cur, length($1));
830 $res .= $type x length($1);
831 }
832 }
833
834 return $res;
835}
836
Andy Whitcroft8905a672007-11-28 16:21:06 -0800837sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800838 my ($possible, $line) = @_;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800839
840 #print "CHECK<$possible>\n";
841 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
842 $possible ne 'goto' && $possible ne 'return' &&
843 $possible ne 'struct' && $possible ne 'enum' &&
844 $possible ne 'case' && $possible ne 'else' &&
845 $possible ne 'typedef') {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800846 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800847 push(@typeList, $possible);
848 build_types();
849 }
850}
851
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700852my $prefix = '';
853
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700854sub report {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700855 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
856 return 0;
857 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800858 my $line = $prefix . $_[0];
859
860 $line = (split('\n', $line))[0] . "\n" if ($terse);
861
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800862 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700863
864 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700865}
866sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800867 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700868}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700869sub ERROR {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700870 if (report("ERROR: $_[0]\n")) {
871 our $clean = 0;
872 our $cnt_error++;
873 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700874}
875sub WARN {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700876 if (report("WARNING: $_[0]\n")) {
877 our $clean = 0;
878 our $cnt_warn++;
879 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700880}
881sub CHK {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700882 if ($check && report("CHECK: $_[0]\n")) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700883 our $clean = 0;
884 our $cnt_chk++;
885 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700886}
887
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700888sub process {
889 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700890
891 my $linenr=0;
892 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800893 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700894 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800895 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700896
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700897 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700898 my $indent;
899 my $previndent=0;
900 my $stashindent=0;
901
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700902 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700903 my $signoff = 0;
904 my $is_patch = 0;
905
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800906 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700907 our $cnt_lines = 0;
908 our $cnt_error = 0;
909 our $cnt_warn = 0;
910 our $cnt_chk = 0;
911
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700912 # Trace the real file/line as we go.
913 my $realfile = '';
914 my $realline = 0;
915 my $realcnt = 0;
916 my $here = '';
917 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800918 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700919 my $first_line = 0;
920
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800921 my $prev_values = 'E';
922
923 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -0700924 my %suppress_ifbraces;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700925
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800926 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700927 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800928 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700929 my @setup_docs = ();
930 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700931
932 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800933 my $line;
934 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700935 $linenr++;
936 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800937
Andy Whitcroft773647a2008-03-28 14:15:58 -0700938 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700939 $setup_docs = 0;
940 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
941 $setup_docs = 1;
942 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700943 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700944 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700945 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
946 $realline=$1-1;
947 if (defined $2) {
948 $realcnt=$3+1;
949 } else {
950 $realcnt=1+1;
951 }
952
953 # Guestimate if this is a continuing comment. Run
954 # the context looking for a comment "edge". If this
955 # edge is a close comment then we must be in a comment
956 # at context start.
957 my $edge;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700958 for (my $ln = $linenr + 1; $ln < ($linenr + $realcnt); $ln++) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700959 next if ($line =~ /^-/);
960 ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
961 last if (defined $edge);
962 }
963 if (defined $edge && $edge eq '*/') {
964 $in_comment = 1;
965 }
966
967 # Guestimate if this is a continuing comment. If this
968 # is the start of a diff block and this line starts
969 # ' *' then it is very likely a comment.
970 if (!defined $edge &&
971 $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
972 {
973 $in_comment = 1;
974 }
975
976 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
977 sanitise_line_reset($in_comment);
978
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700979 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700980 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700981 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -0700982 $line = sanitise_line($rawline);
983 }
984 push(@lines, $line);
985
986 if ($realcnt > 1) {
987 $realcnt-- if ($line =~ /^(?:\+| |$)/);
988 } else {
989 $realcnt = 0;
990 }
991
992 #print "==>$rawline\n";
993 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700994
995 if ($setup_docs && $line =~ /^\+/) {
996 push(@setup_docs, $line);
997 }
998 }
999
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001000 $prefix = '';
1001
Andy Whitcroft773647a2008-03-28 14:15:58 -07001002 $realcnt = 0;
1003 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001004 foreach my $line (@lines) {
1005 $linenr++;
1006
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001007 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001008
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001009#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001010 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001011 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001012 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001013 $realline=$1-1;
1014 if (defined $2) {
1015 $realcnt=$3+1;
1016 } else {
1017 $realcnt=1+1;
1018 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001019 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001020 $prev_values = 'E';
1021
Andy Whitcroft773647a2008-03-28 14:15:58 -07001022 %suppress_ifbraces = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001023 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001024
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001025# track the line number as we move through the hunk, note that
1026# new versions of GNU diff omit the leading space on completely
1027# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001028 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001029 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001030 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001031
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001032 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001033 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001034
1035 # Track the previous line.
1036 ($prevline, $stashline) = ($stashline, $line);
1037 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001038 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1039
Andy Whitcroft773647a2008-03-28 14:15:58 -07001040 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001041
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001042 } elsif ($realcnt == 1) {
1043 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001044 }
1045
1046#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001047 $prefix = "$filename:$realline: " if ($emacs && $file);
1048 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1049
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001050 $here = "#$linenr: " if (!$file);
1051 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001052
1053 # extract the filename as it passes
1054 if ($line=~/^\+\+\+\s+(\S+)/) {
1055 $realfile = $1;
1056 $realfile =~ s@^[^/]*/@@;
1057
1058 if ($realfile =~ m@include/asm/@) {
1059 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1060 }
1061 next;
1062 }
1063
Randy Dunlap389834b2007-06-08 13:47:03 -07001064 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001065
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001066 my $hereline = "$here\n$rawline\n";
1067 my $herecurr = "$here\n$rawline\n";
1068 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001069
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001070 $cnt_lines++ if ($realcnt != 0);
1071
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001072#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001073 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001074 # This is a signoff, if ugly, so do not double report.
1075 $signoff++;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001076 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001077 WARN("Signed-off-by: is the preferred form\n" .
1078 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001079 }
1080 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001081 WARN("space required after Signed-off-by:\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001082 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001083 }
1084 }
1085
Andy Whitcroft00df3442007-06-08 13:47:06 -07001086# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001087 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001088 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001089 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001090 }
1091
1092# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1093 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001094 $rawline !~ m/^$UTF8*$/) {
1095 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1096
1097 my $blank = copy_spacing($rawline);
1098 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1099 my $hereptr = "$hereline$ptr\n";
1100
1101 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001102 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001103
Andy Whitcroft00df3442007-06-08 13:47:06 -07001104#ignore lines being removed
1105 if ($line=~/^-/) {next;}
1106
1107# check we are in a valid source file if not then ignore this hunk
1108 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001109
1110#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001111 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001112 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001113 ERROR("DOS line endings\n" . $herevet);
1114
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001115 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1116 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001117 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001118 }
1119#80 column limit
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001120 if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001121 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001122 }
1123
Andy Whitcroft8905a672007-11-28 16:21:06 -08001124# check for adding lines without a newline.
1125 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1126 WARN("adding a line without newline at end of file\n" . $herecurr);
1127 }
1128
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001129# check we are in a valid source file *.[hc] if not then ignore this hunk
1130 next if ($realfile !~ /\.[hc]$/);
1131
1132# at the beginning of a line any tabs must come first and anything
1133# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001134 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1135 $rawline =~ /^\+\s* \s*/) {
1136 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001137 ERROR("code indent should use tabs where possible\n" . $herevet);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001138 }
1139
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001140# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001141 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001142 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1143 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001144
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001145# Check for potential 'bare' types
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001146 my ($stat, $cond);
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001147 if ($realcnt && $line =~ /.\s*\S/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001148 ($stat, $cond) = ctx_statement_block($linenr,
1149 $realcnt, 0);
1150 $stat =~ s/\n./\n /g;
1151 $cond =~ s/\n./\n /g;
1152
1153 my $s = $stat;
1154 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001155
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001156 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001157 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001158
1159 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001160 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001161
Andy Whitcroft8905a672007-11-28 16:21:06 -08001162 # definitions in global scope can only start with types
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001163 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/s) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001164 possible($1, $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001165
1166 # declarations always start with types
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001167 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/s) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001168 possible($1, $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001169 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001170
1171 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001172 while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001173 possible($1, $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001174 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001175
1176 # Check for any sort of function declaration.
1177 # int foo(something bar, other baz);
1178 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001179 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001180 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001181
Andy Whitcroftcf655042008-03-04 14:28:20 -08001182 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001183 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001184 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001185
Andy Whitcroft8905a672007-11-28 16:21:06 -08001186 for my $arg (split(/\s*,\s*/, $ctx)) {
1187 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
1188
Andy Whitcroftcf655042008-03-04 14:28:20 -08001189 possible($1, $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001190 }
1191 }
1192 }
1193
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001194 }
1195
Andy Whitcroft653d4872007-06-23 17:16:34 -07001196#
1197# Checks which may be anchored in the context.
1198#
1199
1200# Check for switch () and associated case and default
1201# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07001202 if ($line=~/\bswitch\s*\(.*\)/) {
1203 my $err = '';
1204 my $sep = '';
1205 my @ctx = ctx_block_outer($linenr, $realcnt);
1206 shift(@ctx);
1207 for my $ctx (@ctx) {
1208 my ($clen, $cindent) = line_stats($ctx);
1209 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1210 $indent != $cindent) {
1211 $err .= "$sep$ctx\n";
1212 $sep = '';
1213 } else {
1214 $sep = "[...]\n";
1215 }
1216 }
1217 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001218 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001219 }
1220 }
1221
1222# if/while/etc brace do not go on next line, unless defining a do while loop,
1223# or if that brace on the next line is for something else
Andy Whitcroft773647a2008-03-28 14:15:58 -07001224 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
1225 my $pre_ctx = "$1$2";
1226
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001227 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001228 my $ctx_ln = $linenr + $#ctx + 1;
1229 my $ctx_cnt = $realcnt - $#ctx - 1;
1230 my $ctx = join("\n", @ctx);
1231
Andy Whitcroft773647a2008-03-28 14:15:58 -07001232 ##warn "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001233
Andy Whitcroft773647a2008-03-28 14:15:58 -07001234 # Skip over any removed lines in the context following statement.
1235 while (defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^-/) {
1236 $ctx_ln++;
1237 }
1238 ##warn "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1239
1240 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1241 ERROR("that open brace { should be on the previous line\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001242 "$here\n$ctx\n$lines[$ctx_ln - 1]");
Andy Whitcroft00df3442007-06-08 13:47:06 -07001243 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001244 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1245 $ctx =~ /\)\s*\;\s*$/ &&
1246 defined $lines[$ctx_ln - 1])
1247 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001248 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1249 if ($nindent > $indent) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001250 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001251 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1252 }
1253 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001254 }
1255
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001256 # Track the 'values' across context and added lines.
1257 my $opline = $line; $opline =~ s/^./ /;
1258 my $curr_values = annotate_values($opline . "\n", $prev_values);
1259 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001260 if ($dbg_values) {
1261 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001262 print "$linenr > .$outline\n";
1263 print "$linenr > $curr_values\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001264 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001265 $prev_values = substr($curr_values, -1);
1266
Andy Whitcroft00df3442007-06-08 13:47:06 -07001267#ignore lines not being added
1268 if ($line=~/^[^\+]/) {next;}
1269
Andy Whitcroft653d4872007-06-23 17:16:34 -07001270# TEST: allow direct testing of the type matcher.
1271 if ($tst_type && $line =~ /^.$Declare$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001272 ERROR("TEST: is type $Declare\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001273 next;
1274 }
1275
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001276# check for initialisation to aggregates open brace on the next line
1277 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1278 $line =~ /^.\s*{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001279 ERROR("that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001280 }
1281
Andy Whitcroft653d4872007-06-23 17:16:34 -07001282#
1283# Checks which are anchored on the added line.
1284#
1285
1286# check for malformed paths in #include statements (uses RAW line)
1287 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1288 my $path = $1;
1289 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001290 ERROR("malformed #include filename\n" .
1291 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001292 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001293 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001294
1295# no C99 // comments
1296 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001297 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001298 }
1299 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001300 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001301 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001302
1303#EXPORT_SYMBOL should immediately follow its function closing }.
Andy Whitcroft653d4872007-06-23 17:16:34 -07001304 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1305 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1306 my $name = $1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001307 if (($prevline !~ /^}/) &&
1308 ($prevline !~ /^\+}/) &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001309 ($prevline !~ /^ }/) &&
Andy Whitcroftcf655042008-03-04 14:28:20 -08001310 ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1311 ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001312 ($prevline !~ /^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(/) &&
Andy Whitcroftcf655042008-03-04 14:28:20 -08001313 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001314 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001315 }
1316 }
1317
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001318# check for external initialisers.
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001319 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001320 ERROR("do not initialise externals to 0 or NULL\n" .
1321 $herecurr);
1322 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001323# check for static initialisers.
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001324 if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001325 ERROR("do not initialise statics to 0 or NULL\n" .
1326 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001327 }
1328
Andy Whitcroft653d4872007-06-23 17:16:34 -07001329# check for new typedefs, only function parameters and sparse annotations
1330# make sense.
1331 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001332 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001333 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001334 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001335 }
1336
1337# * goes on variable not on type
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001338 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001339 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1340 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001341
1342 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001343 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1344 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001345
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001346 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001347 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1348 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001349
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001350 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001351 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1352 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001353 }
1354
1355# # no BUG() or BUG_ON()
1356# if ($line =~ /\b(BUG|BUG_ON)\b/) {
1357# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1358# print "$herecurr";
1359# $clean = 0;
1360# }
1361
Andy Whitcroft8905a672007-11-28 16:21:06 -08001362 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001363 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001364 }
1365
Andy Whitcroft00df3442007-06-08 13:47:06 -07001366# printk should use KERN_* levels. Note that follow on printk's on the
1367# same line do not need a level, so we use the current block context
1368# to try and find and validate the current printk. In summary the current
1369# printk includes all preceeding printk's which have no newline on the end.
1370# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001371 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001372 my $ok = 0;
1373 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1374 #print "CHECK<$lines[$ln - 1]\n";
1375 # we have a preceeding printk if it ends
1376 # with "\n" ignore it, else it is to blame
1377 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1378 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1379 $ok = 1;
1380 }
1381 last;
1382 }
1383 }
1384 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001385 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001386 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001387 }
1388
Andy Whitcroft653d4872007-06-23 17:16:34 -07001389# function brace can't be on same line, except for #defines of do while,
1390# or if closed on same line
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001391 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001392 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001393 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001394 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001395
Andy Whitcroft8905a672007-11-28 16:21:06 -08001396# open braces for enum, union and struct go on the same line.
1397 if ($line =~ /^.\s*{/ &&
1398 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1399 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1400 }
1401
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001402# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001403 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001404 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001405 my $ctx_before = substr($line, 0, $-[1]);
1406 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001407
1408 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001409 if ($name =~ /^(?:
1410 if|for|while|switch|return|case|
1411 volatile|__volatile__|
1412 __attribute__|format|__extension__|
1413 asm|__asm__)$/x)
1414 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001415
1416 # cpp #define statements have non-optional spaces, ie
1417 # if there is a space between the name and the open
1418 # parenthesis it is simply not a parameter group.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001419 } elsif ($ctx_before =~ /^.\#\s*define\s*$/) {
1420
1421 # cpp #elif statement condition may start with a (
1422 } elsif ($ctx =~ /^.\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001423
1424 # If this whole things ends with a type its most
1425 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001426 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001427
1428 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001429 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001430 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001431 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001432# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001433 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001434 my $ops = qr{
1435 <<=|>>=|<=|>=|==|!=|
1436 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1437 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001438 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001439 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001440 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001441 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001442
1443 my $blank = copy_spacing($opline);
1444
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001445 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001446 $off += length($elements[$n]);
1447
Andy Whitcroft773647a2008-03-28 14:15:58 -07001448 # Pick up the preceeding and succeeding characters.
1449 my $ca = substr($opline, 0, $off);
1450 my $cc = '';
1451 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1452 $cc = substr($opline, $off + length($elements[$n + 1]));
1453 }
1454 my $cb = "$ca$;$cc";
1455
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001456 my $a = '';
1457 $a = 'V' if ($elements[$n] ne '');
1458 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001459 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001460 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1461 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07001462 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001463
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001464 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001465
1466 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001467 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001468 $c = 'V' if ($elements[$n + 2] ne '');
1469 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001470 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001471 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1472 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001473 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001474 } else {
1475 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001476 }
1477
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001478 my $ctx = "${a}x${c}";
1479
1480 my $at = "(ctx:$ctx)";
1481
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001482 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001483 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001484
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001485 # Classify operators into binary, unary, or
1486 # definitions (* only) where they have more
1487 # than one mode.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001488 my $op_type = substr($curr_values, $off + 1, 1);
1489 my $op_left = substr($curr_values, $off, 1);
1490 my $is_unary;
1491 if ($op_type eq 'T') {
1492 $is_unary = 2;
1493 } elsif ($op_left eq 'V') {
1494 $is_unary = 0;
1495 } else {
1496 $is_unary = 1;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001497 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001498 #if ($op eq '-' || $op eq '&' || $op eq '*') {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001499 # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001500 #}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001501
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001502 # Ignore operators passed as parameters.
1503 if ($op_type ne 'V' &&
1504 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1505
Andy Whitcroftcf655042008-03-04 14:28:20 -08001506# # Ignore comments
1507# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001508
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001509 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001510 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001511 if ($ctx !~ /.x[WEBC]/ &&
1512 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001513 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001514 }
1515
1516 # // is a comment
1517 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001518
1519 # -> should have no spaces
1520 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001521 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001522 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001523 }
1524
1525 # , must have a space on the right.
1526 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001527 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001528 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001529 }
1530
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001531 # '*' as part of a type definition -- reported already.
1532 } elsif ($op eq '*' && $is_unary == 2) {
1533 #warn "'*' is part of type\n";
1534
1535 # unary operators should have a space before and
1536 # none after. May be left adjacent to another
1537 # unary operator, or a cast
1538 } elsif ($op eq '!' || $op eq '~' ||
1539 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001540 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001541 ERROR("space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001542 }
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001543 if ($op eq '*' && $cc =~/\s*const\b/) {
1544 # A unary '*' may be const
1545
1546 } elsif ($ctx =~ /.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001547 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001548 }
1549
1550 # unary ++ and unary -- are allowed no space on one side.
1551 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001552 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1553 ERROR("space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001554 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001555 if ($ctx =~ /Wx[BE]/ ||
1556 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1557 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001558 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001559 if ($ctx =~ /ExW/) {
1560 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1561 }
1562
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001563
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001564 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001565 } elsif ($op eq '<<' or $op eq '>>' or
1566 $op eq '&' or $op eq '^' or $op eq '|' or
1567 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001568 $op eq '*' or $op eq '/' or
1569 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001570 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001571 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001572 ERROR("need consistent spacing around '$op' $at\n" .
1573 $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001574 }
1575
1576 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08001577 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001578 # Ignore email addresses <foo@bar>
1579 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1580 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001581 ERROR("spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001582 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001583 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001584 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001585 }
1586 }
1587
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001588# check for multiple assignments
1589 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001590 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001591 }
1592
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001593## # check for multiple declarations, allowing for a function declaration
1594## # continuation.
1595## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1596## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1597##
1598## # Remove any bracketed sections to ensure we do not
1599## # falsly report the parameters of functions.
1600## my $ln = $line;
1601## while ($ln =~ s/\([^\(\)]*\)//g) {
1602## }
1603## if ($ln =~ /,/) {
1604## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1605## }
1606## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001607
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001608#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001609 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1610 $line =~ /do{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001611 ERROR("space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001612 }
1613
1614# closing brace should have a space following it when it has anything
1615# on the line
1616 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001617 ERROR("space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001618 }
1619
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001620# check spacing on square brackets
1621 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001622 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001623 }
1624 if ($line =~ /\s\]/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001625 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001626 }
1627
1628# check spacing on paretheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001629 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1630 $line !~ /for\s*\(\s+;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001631 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001632 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001633 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001634 $line !~ /for\s*\(.*;\s+\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001635 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001636 }
1637
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001638#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001639 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001640 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001641 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001642 }
1643
1644# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001645 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001646 ERROR("space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001647 }
1648
1649# Check for illegal assignment in if conditional.
Andy Whitcroft8905a672007-11-28 16:21:06 -08001650 if ($line =~ /\bif\s*\(/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001651 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001652
1653 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001654 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001655 }
1656
1657 # Find out what is on the end of the line after the
1658 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001659 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001660 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001661 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft773647a2008-03-28 14:15:58 -07001662 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/ &&
1663 $c !~ /^.\#\s*if/)
1664 {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001665 ERROR("trailing statements should be on next line\n" . $herecurr);
1666 }
1667 }
1668
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001669# Check for bitwise tests written as boolean
1670 if ($line =~ /
1671 (?:
1672 (?:\[|\(|\&\&|\|\|)
1673 \s*0[xX][0-9]+\s*
1674 (?:\&\&|\|\|)
1675 |
1676 (?:\&\&|\|\|)
1677 \s*0[xX][0-9]+\s*
1678 (?:\&\&|\|\||\)|\])
1679 )/x)
1680 {
1681 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
1682 }
1683
Andy Whitcroft8905a672007-11-28 16:21:06 -08001684# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001685 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
1686 my $s = $1;
1687 $s =~ s/$;//g; # Remove any comments
1688 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
1689 ERROR("trailing statements should be on next line\n" . $herecurr);
1690 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001691 }
1692
1693 # Check for }<nl>else {, these must be at the same
1694 # indent level to be relevant to each other.
1695 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
1696 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001697 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001698 }
1699
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001700 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1701 $previndent == $indent) {
1702 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1703
1704 # Find out what is on the end of the line after the
1705 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001706 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001707 $s =~ s/\n.*//g;
1708
1709 if ($s =~ /^\s*;/) {
1710 ERROR("while should follow close brace '}'\n" . $hereprev);
1711 }
1712 }
1713
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001714#studly caps, commented out until figure out how to distinguish between use of existing and adding new
1715# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
1716# print "No studly caps, use _\n";
1717# print "$herecurr";
1718# $clean = 0;
1719# }
1720
1721#no spaces allowed after \ in define
1722 if ($line=~/\#define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001723 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001724 }
1725
Andy Whitcroft653d4872007-06-23 17:16:34 -07001726#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1727 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001728 my $checkfile = "$root/include/linux/$1.h";
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001729 if (-f $checkfile && $1 ne 'irq') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001730 WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001731 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001732 }
1733 }
1734
Andy Whitcroft653d4872007-06-23 17:16:34 -07001735# multi-statement macros should be enclosed in a do while loop, grab the
1736# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08001737# in a known good container
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001738 if ($prevline =~ /\#define.*\\/ &&
1739 $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1740 $line !~ /(?:do\s+{|\(\{|\{)/ &&
1741 $line !~ /^.\s*$Declare\s/) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001742 # Grab the first statement, if that is the entire macro
1743 # its ok. This may start either on the #define line
1744 # or the one below.
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001745 my $ln = $linenr;
1746 my $cnt = $realcnt;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001747 my $off = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001748
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001749 # If the macro starts on the define line start
1750 # grabbing the statement after the identifier
1751 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1752 ##print "1<$1> 2<$2>\n";
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001753 if (defined $2 && $2 ne '') {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001754 $off = length($1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001755 $ln--;
1756 $cnt++;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001757 while ($lines[$ln - 1] =~ /^-/) {
1758 $ln--;
1759 $cnt++;
1760 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001761 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001762 my @ctx = ctx_statement($ln, $cnt, $off);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001763 my $ctx_ln = $ln + $#ctx + 1;
1764 my $ctx = join("\n", @ctx);
1765
1766 # Pull in any empty extension lines.
1767 while ($ctx =~ /\\$/ &&
1768 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1769 $ctx .= $lines[$ctx_ln - 1];
1770 $ctx_ln++;
1771 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001772
1773 if ($ctx =~ /\\$/) {
1774 if ($ctx =~ /;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001775 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001776 } else {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001777 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001778 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001779 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001780 }
1781
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001782# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001783 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
1784 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08001785 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001786 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001787 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1788 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001789 my $allowed = 0;
1790 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001791 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001792 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001793 for my $chunk (@chunks) {
1794 my ($cond, $block) = @{$chunk};
1795
Andy Whitcroft773647a2008-03-28 14:15:58 -07001796 # If the condition carries leading newlines, then count those as offsets.
1797 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
1798 my $offset = statement_rawlines($whitespace) - 1;
1799
1800 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
1801
1802 # We have looked at and allowed this specific line.
1803 $suppress_ifbraces{$ln + $offset} = 1;
1804
1805 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001806 $ln += statement_rawlines($block) - 1;
1807
Andy Whitcroft773647a2008-03-28 14:15:58 -07001808 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001809
1810 $seen++ if ($block =~ /^\s*{/);
1811
Andy Whitcroftcf655042008-03-04 14:28:20 -08001812 #print "cond<$cond> block<$block> allowed<$allowed>\n";
1813 if (statement_lines($cond) > 1) {
1814 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001815 $allowed = 1;
1816 }
1817 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001818 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001819 $allowed = 1;
1820 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001821 if (statement_block_size($block) > 1) {
1822 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001823 $allowed = 1;
1824 }
1825 }
1826 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001827 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001828 }
1829 }
1830 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001831 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001832 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001833 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001834
Andy Whitcroftcf655042008-03-04 14:28:20 -08001835 # Check the pre-context.
1836 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
1837 #print "APW: ALLOWED: pre<$1>\n";
1838 $allowed = 1;
1839 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001840
1841 my ($level, $endln, @chunks) =
1842 ctx_statement_full($linenr, $realcnt, $-[0]);
1843
Andy Whitcroftcf655042008-03-04 14:28:20 -08001844 # Check the condition.
1845 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07001846 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001847 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001848 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08001849 }
1850 if (statement_lines($cond) > 1) {
1851 #print "APW: ALLOWED: cond<$cond>\n";
1852 $allowed = 1;
1853 }
1854 if ($block =~/\b(?:if|for|while)\b/) {
1855 #print "APW: ALLOWED: block<$block>\n";
1856 $allowed = 1;
1857 }
1858 if (statement_block_size($block) > 1) {
1859 #print "APW: ALLOWED: lines block<$block>\n";
1860 $allowed = 1;
1861 }
1862 # Check the post-context.
1863 if (defined $chunks[1]) {
1864 my ($cond, $block) = @{$chunks[1]};
1865 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001866 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001867 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001868 if ($block =~ /^\s*\{/) {
1869 #print "APW: ALLOWED: chunk-1 block<$block>\n";
1870 $allowed = 1;
1871 }
1872 }
1873 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
1874 my $herectx = $here . "\n";;
1875 my $end = $linenr + statement_rawlines($block) - 1;
1876
1877 for (my $ln = $linenr - 1; $ln < $end; $ln++) {
1878 $herectx .= $rawlines[$ln] . "\n";;
1879 }
1880
1881 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001882 }
1883 }
1884
Andy Whitcroft653d4872007-06-23 17:16:34 -07001885# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001886 for my $inc (@dep_includes) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001887 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001888 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001889 }
1890 }
1891
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001892# don't use deprecated functions
1893 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001894 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001895 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001896 }
1897 }
1898
1899# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001900 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
1901 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001902 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001903 }
1904
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001905# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
1906 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
1907 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
1908 }
1909
Andy Whitcroft00df3442007-06-08 13:47:06 -07001910# warn about #if 0
1911 if ($line =~ /^.#\s*if\s+0\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001912 CHK("if this code is redundant consider removing it\n" .
1913 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001914 }
1915
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001916# check for needless kfree() checks
1917 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1918 my $expr = $1;
1919 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1920 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1921 }
1922 }
Greg Kroah-Hartmanc2010a32008-04-14 14:17:29 -07001923# check for needless usb_free_urb() checks
1924 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1925 my $expr = $1;
1926 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
1927 WARN("usb_free_urb(NULL) is safe this check is probabally not required\n" . $hereprev);
1928 }
1929 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001930
Andy Whitcroft00df3442007-06-08 13:47:06 -07001931# warn about #ifdefs in C files
1932# if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1933# print "#ifdef in C files should be avoided\n";
1934# print "$herecurr";
1935# $clean = 0;
1936# }
1937
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001938# warn about spacing in #ifdefs
1939 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1940 ERROR("exactly one space required after that #$1\n" . $herecurr);
1941 }
1942
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001943# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001944 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
1945 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001946 my $which = $1;
1947 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001948 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001949 }
1950 }
1951# check for memory barriers without a comment.
1952 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
1953 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001954 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001955 }
1956 }
1957# check of hardware specific defines
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001958 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001959 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001960 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001961
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001962# check the location of the inline attribute, that it is between
1963# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001964 if ($line =~ /\b$Type\s+$Inline\b/ ||
1965 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001966 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1967 }
1968
Andy Whitcroft8905a672007-11-28 16:21:06 -08001969# Check for __inline__ and __inline, prefer inline
1970 if ($line =~ /\b(__inline__|__inline)\b/) {
1971 WARN("plain inline is preferred over $1\n" . $herecurr);
1972 }
1973
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001974# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001975 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001976 $stat =~ /^.\s*(?:extern\s+)?$Type\s+$Ident(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001977 {
1978 my $paren_space = $1;
1979
1980 my $s = $stat;
1981 if (defined $cond) {
1982 substr($s, 0, length($cond), '');
1983 }
1984 if ($s =~ /^\s*;/) {
1985 WARN("externs should be avoided in .c files\n" . $herecurr);
1986 }
1987
1988 if ($paren_space =~ /\n/) {
1989 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
1990 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07001991
1992 } elsif ($realfile =~ /\.c$/ && defined $stat &&
1993 $stat =~ /^.\s*extern\s+/)
1994 {
1995 WARN("externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001996 }
1997
1998# checks for new __setup's
1999 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2000 my $name = $1;
2001
2002 if (!grep(/$name/, @setup_docs)) {
2003 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2004 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07002005 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002006
2007# check for pointless casting of kmalloc return
2008 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2009 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2010 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002011
2012# check for gcc specific __FUNCTION__
2013 if ($line =~ /__FUNCTION__/) {
2014 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2015 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002016
2017# check for semaphores used as mutexes
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002018 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002019 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2020 }
2021# check for semaphores used as mutexes
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002022 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002023 WARN("consider using a completion\n" . $herecurr);
2024 }
2025# recommend strict_strto* over simple_strto*
2026 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2027 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2028 }
2029
2030# use of NR_CPUS is usually wrong
2031# ignore definitions of NR_CPUS and usage to define arrays as likely right
2032 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002033 $line !~ /^.#\s*if\b.*\bNR_CPUS\b/ &&
2034 $line !~ /^.#\s*define\b.*\bNR_CPUS\b/ &&
2035 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2036 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2037 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002038 {
2039 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2040 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07002041
2042# check for %L{u,d,i} in strings
2043 my $string;
2044 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2045 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2046 if ($string =~ /(?<!%)%L[udi]/) {
2047 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2048 last;
2049 }
2050 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002051 }
2052
2053 # If we have no input at all, then there is nothing to report on
2054 # so just keep quiet.
2055 if ($#rawlines == -1) {
2056 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002057 }
2058
Andy Whitcroft8905a672007-11-28 16:21:06 -08002059 # In mailback mode only produce a report in the negative, for
2060 # things that appear to be patches.
2061 if ($mailback && ($clean == 1 || !$is_patch)) {
2062 exit(0);
2063 }
2064
2065 # This is not a patch, and we are are in 'no-patch' mode so
2066 # just keep quiet.
2067 if (!$chk_patch && !$is_patch) {
2068 exit(0);
2069 }
2070
2071 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002072 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002073 }
2074 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002075 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002076 }
2077
Andy Whitcroft8905a672007-11-28 16:21:06 -08002078 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002079 if ($summary && !($clean == 1 && $quiet == 1)) {
2080 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002081 print "total: $cnt_error errors, $cnt_warn warnings, " .
2082 (($check)? "$cnt_chk checks, " : "") .
2083 "$cnt_lines lines checked\n";
2084 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002085 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002086
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002087 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002088 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002089 }
2090 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002091 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002092 print "are false positives report them to the maintainer, see\n";
2093 print "CHECKPATCH in MAINTAINERS.\n";
2094 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002095
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002096 return $clean;
2097}