blob: 58a94947d6557e0783ef1043b77cb7e8a57bc30d [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 Whitcroft773647a2008-03-28 14:15:58 -070012my $V = '0.16';
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
134our @typeList = (
135 qr{void},
136 qr{char},
137 qr{short},
138 qr{int},
139 qr{long},
140 qr{unsigned},
141 qr{float},
142 qr{double},
143 qr{bool},
144 qr{long\s+int},
145 qr{long\s+long},
146 qr{long\s+long\s+int},
147 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
148 qr{struct\s+$Ident},
149 qr{union\s+$Ident},
150 qr{enum\s+$Ident},
151 qr{${Ident}_t},
152 qr{${Ident}_handler},
153 qr{${Ident}_handler_fn},
154);
155
156sub build_types {
157 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
158 $NonptrType = qr{
159 \b
160 (?:const\s+)?
161 (?:unsigned\s+)?
Andy Whitcroftcf655042008-03-04 14:28:20 -0800162 (?:
163 $all|
164 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)
165 )
Andy Whitcroft8905a672007-11-28 16:21:06 -0800166 (?:\s+$Sparse|\s+const)*
167 \b
168 }x;
169 $Type = qr{
170 \b$NonptrType\b
171 (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800172 (?:\s+$Inline|\s+$Sparse|\s+$Attribute)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800173 }x;
174 $Declare = qr{(?:$Storage\s+)?$Type};
175}
176build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700177
178$chk_signoff = 0 if ($file);
179
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700180my @dep_includes = ();
181my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700182my $removal = "Documentation/feature-removal-schedule.txt";
183if ($tree && -f "$root/$removal") {
184 open(REMOVE, "<$root/$removal") ||
185 die "$P: $removal: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700186 while (<REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700187 if (/^Check:\s+(.*\S)/) {
188 for my $entry (split(/[, ]+/, $1)) {
189 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700190 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700191
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700192 } elsif ($entry !~ m@/@) {
193 push(@dep_functions, $entry);
194 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700195 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700196 }
197 }
198}
199
Andy Whitcroft00df3442007-06-08 13:47:06 -0700200my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800201my @lines = ();
202my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700203for my $filename (@ARGV) {
204 if ($file) {
205 open(FILE, "diff -u /dev/null $filename|") ||
206 die "$P: $filename: diff failed - $!\n";
207 } else {
208 open(FILE, "<$filename") ||
209 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700210 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800211 if ($filename eq '-') {
212 $vname = 'Your patch';
213 } else {
214 $vname = $filename;
215 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700216 while (<FILE>) {
217 chomp;
218 push(@rawlines, $_);
219 }
220 close(FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800221 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700222 $exit = 1;
223 }
224 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800225 @lines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700226}
227
228exit($exit);
229
230sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700231 my ($root) = @_;
232
233 my @tree_check = (
234 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
235 "README", "Documentation", "arch", "include", "drivers",
236 "fs", "init", "ipc", "kernel", "lib", "scripts",
237 );
238
239 foreach my $check (@tree_check) {
240 if (! -e $root . '/' . $check) {
241 return 0;
242 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700243 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700244 return 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700245}
246
247sub expand_tabs {
248 my ($str) = @_;
249
250 my $res = '';
251 my $n = 0;
252 for my $c (split(//, $str)) {
253 if ($c eq "\t") {
254 $res .= ' ';
255 $n++;
256 for (; ($n % 8) != 0; $n++) {
257 $res .= ' ';
258 }
259 next;
260 }
261 $res .= $c;
262 $n++;
263 }
264
265 return $res;
266}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700267sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700268 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700269 return $res;
270}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700271
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700272sub line_stats {
273 my ($line) = @_;
274
275 # Drop the diff line leader and expand tabs
276 $line =~ s/^.//;
277 $line = expand_tabs($line);
278
279 # Pick the indent from the front of the line.
280 my ($white) = ($line =~ /^(\s*)/);
281
282 return (length($line), length($white));
283}
284
Andy Whitcroft773647a2008-03-28 14:15:58 -0700285my $sanitise_quote = '';
286
287sub sanitise_line_reset {
288 my ($in_comment) = @_;
289
290 if ($in_comment) {
291 $sanitise_quote = '*/';
292 } else {
293 $sanitise_quote = '';
294 }
295}
Andy Whitcroft00df3442007-06-08 13:47:06 -0700296sub sanitise_line {
297 my ($line) = @_;
298
299 my $res = '';
300 my $l = '';
301
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800302 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700303 my $off = 0;
304 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700305
Andy Whitcroft773647a2008-03-28 14:15:58 -0700306 # Always copy over the diff marker.
307 $res = substr($line, 0, 1);
308
309 for ($off = 1; $off < length($line); $off++) {
310 $c = substr($line, $off, 1);
311
312 # Comments we are wacking completly including the begin
313 # and end, all to $;.
314 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
315 $sanitise_quote = '*/';
316
317 substr($res, $off, 2, "$;$;");
318 $off++;
319 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800320 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700321 if (substr($line, $off, 2) eq $sanitise_quote) {
322 $sanitise_quote = '';
323 substr($res, $off, 2, "$;$;");
324 $off++;
325 next;
326 }
327
328 # A \ in a string means ignore the next character.
329 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
330 $c eq "\\") {
331 substr($res, $off, 2, 'XX');
332 $off++;
333 next;
334 }
335 # Regular quotes.
336 if ($c eq "'" || $c eq '"') {
337 if ($sanitise_quote eq '') {
338 $sanitise_quote = $c;
339
340 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700341 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700342 } elsif ($sanitise_quote eq $c) {
343 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -0700344 }
345 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700346
347 #print "SQ:$sanitise_quote\n";
348 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
349 substr($res, $off, 1, $;);
350 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
351 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -0700352 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700353 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -0700354 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800355 }
356
357 # The pathname on a #include may be surrounded by '<' and '>'.
358 if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
359 my $clean = 'X' x length($1);
360 $res =~ s@\<.*\>@<$clean>@;
361
362 # The whole of a #error is a string.
363 } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
364 my $clean = 'X' x length($1);
365 $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@;
366 }
367
Andy Whitcroft00df3442007-06-08 13:47:06 -0700368 return $res;
369}
370
Andy Whitcroft8905a672007-11-28 16:21:06 -0800371sub ctx_statement_block {
372 my ($linenr, $remain, $off) = @_;
373 my $line = $linenr - 1;
374 my $blk = '';
375 my $soff = $off;
376 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700377 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800378
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800379 my $loff = 0;
380
Andy Whitcroft8905a672007-11-28 16:21:06 -0800381 my $type = '';
382 my $level = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800383 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800384 my $c;
385 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800386
387 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800388 while (1) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700389 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800390 # If we are about to drop off the end, pull in more
391 # context.
392 if ($off >= $len) {
393 for (; $remain > 0; $line++) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800394 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800395 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800396 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800397 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800398 $len = length($blk);
399 $line++;
400 last;
401 }
402 # Bail if there is no further context.
403 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800404 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800405 last;
406 }
407 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800408 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800409 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800410 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800411
Andy Whitcroft773647a2008-03-28 14:15:58 -0700412 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800413 # Statement ends at the ';' or a close '}' at the
414 # outermost level.
415 if ($level == 0 && $c eq ';') {
416 last;
417 }
418
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800419 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -0700420 if ($level == 0 && $coff_set == 0 &&
421 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
422 $remainder =~ /^(else)(?:\s|{)/ &&
423 $remainder !~ /^else\s+if\b/) {
424 $coff = $off + length($1) - 1;
425 $coff_set = 1;
426 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
427 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800428 }
429
Andy Whitcroft8905a672007-11-28 16:21:06 -0800430 if (($type eq '' || $type eq '(') && $c eq '(') {
431 $level++;
432 $type = '(';
433 }
434 if ($type eq '(' && $c eq ')') {
435 $level--;
436 $type = ($level != 0)? '(' : '';
437
438 if ($level == 0 && $coff < $soff) {
439 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700440 $coff_set = 1;
441 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800442 }
443 }
444 if (($type eq '' || $type eq '{') && $c eq '{') {
445 $level++;
446 $type = '{';
447 }
448 if ($type eq '{' && $c eq '}') {
449 $level--;
450 $type = ($level != 0)? '{' : '';
451
452 if ($level == 0) {
453 last;
454 }
455 }
456 $off++;
457 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800458 if ($off == $len) {
459 $line++;
460 $remain--;
461 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800462
463 my $statement = substr($blk, $soff, $off - $soff + 1);
464 my $condition = substr($blk, $soff, $coff - $soff + 1);
465
466 #warn "STATEMENT<$statement>\n";
467 #warn "CONDITION<$condition>\n";
468
Andy Whitcroft773647a2008-03-28 14:15:58 -0700469 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800470
471 return ($statement, $condition,
472 $line, $remain + 1, $off - $loff + 1, $level);
473}
474
Andy Whitcroftcf655042008-03-04 14:28:20 -0800475sub statement_lines {
476 my ($stmt) = @_;
477
478 # Strip the diff line prefixes and rip blank lines at start and end.
479 $stmt =~ s/(^|\n)./$1/g;
480 $stmt =~ s/^\s*//;
481 $stmt =~ s/\s*$//;
482
483 my @stmt_lines = ($stmt =~ /\n/g);
484
485 return $#stmt_lines + 2;
486}
487
488sub statement_rawlines {
489 my ($stmt) = @_;
490
491 my @stmt_lines = ($stmt =~ /\n/g);
492
493 return $#stmt_lines + 2;
494}
495
496sub statement_block_size {
497 my ($stmt) = @_;
498
499 $stmt =~ s/(^|\n)./$1/g;
500 $stmt =~ s/^\s*{//;
501 $stmt =~ s/}\s*$//;
502 $stmt =~ s/^\s*//;
503 $stmt =~ s/\s*$//;
504
505 my @stmt_lines = ($stmt =~ /\n/g);
506 my @stmt_statements = ($stmt =~ /;/g);
507
508 my $stmt_lines = $#stmt_lines + 2;
509 my $stmt_statements = $#stmt_statements + 1;
510
511 if ($stmt_lines > $stmt_statements) {
512 return $stmt_lines;
513 } else {
514 return $stmt_statements;
515 }
516}
517
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800518sub ctx_statement_full {
519 my ($linenr, $remain, $off) = @_;
520 my ($statement, $condition, $level);
521
522 my (@chunks);
523
Andy Whitcroftcf655042008-03-04 14:28:20 -0800524 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800525 ($statement, $condition, $linenr, $remain, $off, $level) =
526 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700527 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800528 push(@chunks, [ $condition, $statement ]);
529 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
530 return ($level, $linenr, @chunks);
531 }
532
533 # Pull in the following conditional/block pairs and see if they
534 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800535 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800536 ($statement, $condition, $linenr, $remain, $off, $level) =
537 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800538 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -0700539 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -0800540 #print "C: push\n";
541 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800542 }
543
544 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800545}
546
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700547sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700548 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700549 my $line;
550 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700551 my $blk = '';
552 my @o;
553 my @c;
554 my @res = ();
555
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700556 my $level = 0;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700557 for ($line = $start; $remain > 0; $line++) {
558 next if ($rawlines[$line] =~ /^-/);
559 $remain--;
560
561 $blk .= $rawlines[$line];
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700562 foreach my $c (split(//, $rawlines[$line])) {
563 ##print "C<$c>L<$level><$open$close>O<$off>\n";
564 if ($off > 0) {
565 $off--;
566 next;
567 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700568
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700569 if ($c eq $close && $level > 0) {
570 $level--;
571 last if ($level == 0);
572 } elsif ($c eq $open) {
573 $level++;
574 }
575 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700576
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700577 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700578 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700579 }
580
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700581 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700582 }
583
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700584 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700585}
586sub ctx_block_outer {
587 my ($linenr, $remain) = @_;
588
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700589 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
590 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700591}
592sub ctx_block {
593 my ($linenr, $remain) = @_;
594
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700595 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
596 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700597}
598sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700599 my ($linenr, $remain, $off) = @_;
600
601 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
602 return @r;
603}
604sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700605 my ($linenr, $remain) = @_;
606
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700607 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700608}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700609sub ctx_statement_level {
610 my ($linenr, $remain, $off) = @_;
611
612 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
613}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700614
615sub ctx_locate_comment {
616 my ($first_line, $end_line) = @_;
617
618 # Catch a comment on the end of the line itself.
Andy Whitcroft00df3442007-06-08 13:47:06 -0700619 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700620 return $current_comment if (defined $current_comment);
621
622 # Look through the context and try and figure out if there is a
623 # comment.
624 my $in_comment = 0;
625 $current_comment = '';
626 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700627 my $line = $rawlines[$linenr - 1];
628 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700629 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
630 $in_comment = 1;
631 }
632 if ($line =~ m@/\*@) {
633 $in_comment = 1;
634 }
635 if (!$in_comment && $current_comment ne '') {
636 $current_comment = '';
637 }
638 $current_comment .= $line . "\n" if ($in_comment);
639 if ($line =~ m@\*/@) {
640 $in_comment = 0;
641 }
642 }
643
644 chomp($current_comment);
645 return($current_comment);
646}
647sub ctx_has_comment {
648 my ($first_line, $end_line) = @_;
649 my $cmt = ctx_locate_comment($first_line, $end_line);
650
Andy Whitcroft00df3442007-06-08 13:47:06 -0700651 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700652 ##print "CMMT: $cmt\n";
653
654 return ($cmt ne '');
655}
656
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700657sub cat_vet {
658 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700659 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700660
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700661 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700662 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
663 $res .= $1;
664 if ($2 ne '') {
665 $coded = sprintf("^%c", unpack('C', $2) + 64);
666 $res .= $coded;
667 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700668 }
669 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700670
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700671 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700672}
673
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800674my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800675my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800676my @av_paren_type;
677
678sub annotate_reset {
679 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800680 $av_pending = '_';
681 @av_paren_type = ('E');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800682}
683
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700684sub annotate_values {
685 my ($stream, $type) = @_;
686
687 my $res;
688 my $cur = $stream;
689
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800690 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700691
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700692 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700693 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800694 print " <" . join('', @av_paren_type) .
695 "> <$type> " if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700696 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800697 print "WS($1)\n" if ($dbg_values > 1);
698 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800699 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800700 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700701 }
702
Andy Whitcroft8905a672007-11-28 16:21:06 -0800703 } elsif ($cur =~ /^($Type)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800704 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700705 $type = 'T';
706
707 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800708 print "DEFINE($1)\n" if ($dbg_values > 1);
709 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800710 $av_pending = 'N';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700711
Andy Whitcroftcf655042008-03-04 14:28:20 -0800712 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
713 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800714 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800715
716 push(@av_paren_type, $type);
717 push(@av_paren_type, $type);
718 $type = 'N';
719
720 } elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
721 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
722 $av_preprocessor = 1;
723
724 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
725
726 $type = 'N';
727
728 } elsif ($cur =~ /^(#\s*(?:endif))/o) {
729 print "PRE_END($1)\n" if ($dbg_values > 1);
730
731 $av_preprocessor = 1;
732
733 # Assume all arms of the conditional end as this
734 # one does, and continue as if the #endif was not here.
735 pop(@av_paren_type);
736 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700737 $type = 'N';
738
739 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800740 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700741
742 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800743 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700744 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800745 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700746 }
747 $type = 'N';
748
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800749 } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800750 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800751 $av_pending = 'N';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700752 $type = 'N';
753
754 } elsif ($cur =~/^(return|case|else)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800755 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700756 $type = 'N';
757
758 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800759 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800760 push(@av_paren_type, $av_pending);
761 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700762 $type = 'N';
763
764 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800765 my $new_type = pop(@av_paren_type);
766 if ($new_type ne '_') {
767 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800768 print "PAREN('$1') -> $type\n"
769 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700770 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800771 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700772 }
773
774 } elsif ($cur =~ /^($Ident)\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800775 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800776 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700777
778 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800779 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700780 $type = 'V';
781
782 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800783 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700784 $type = 'N';
785
Andy Whitcroftcf655042008-03-04 14:28:20 -0800786 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800787 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800788 $type = 'E';
789
Andy Whitcroftcf655042008-03-04 14:28:20 -0800790 } elsif ($cur =~ /^(;|\?|:|\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800791 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700792 $type = 'N';
793
794 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800795 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700796 if ($1 ne '++' && $1 ne '--') {
797 $type = 'N';
798 }
799
800 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800801 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700802 }
803 if (defined $1) {
804 $cur = substr($cur, length($1));
805 $res .= $type x length($1);
806 }
807 }
808
809 return $res;
810}
811
Andy Whitcroft8905a672007-11-28 16:21:06 -0800812sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800813 my ($possible, $line) = @_;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800814
815 #print "CHECK<$possible>\n";
816 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
817 $possible ne 'goto' && $possible ne 'return' &&
818 $possible ne 'struct' && $possible ne 'enum' &&
819 $possible ne 'case' && $possible ne 'else' &&
820 $possible ne 'typedef') {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800821 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800822 push(@typeList, $possible);
823 build_types();
824 }
825}
826
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700827my $prefix = '';
828
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700829sub report {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700830 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
831 return 0;
832 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800833 my $line = $prefix . $_[0];
834
835 $line = (split('\n', $line))[0] . "\n" if ($terse);
836
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800837 push(our @report, $line);
Andy Whitcroft773647a2008-03-28 14:15:58 -0700838
839 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700840}
841sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800842 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700843}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700844sub ERROR {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700845 if (report("ERROR: $_[0]\n")) {
846 our $clean = 0;
847 our $cnt_error++;
848 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700849}
850sub WARN {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700851 if (report("WARNING: $_[0]\n")) {
852 our $clean = 0;
853 our $cnt_warn++;
854 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700855}
856sub CHK {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700857 if ($check && report("CHECK: $_[0]\n")) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700858 our $clean = 0;
859 our $cnt_chk++;
860 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700861}
862
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700863sub process {
864 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700865
866 my $linenr=0;
867 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800868 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700869 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800870 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700871
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700872 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700873 my $indent;
874 my $previndent=0;
875 my $stashindent=0;
876
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700877 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700878 my $signoff = 0;
879 my $is_patch = 0;
880
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800881 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700882 our $cnt_lines = 0;
883 our $cnt_error = 0;
884 our $cnt_warn = 0;
885 our $cnt_chk = 0;
886
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700887 # Trace the real file/line as we go.
888 my $realfile = '';
889 my $realline = 0;
890 my $realcnt = 0;
891 my $here = '';
892 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800893 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700894 my $first_line = 0;
895
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800896 my $prev_values = 'E';
897
898 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -0700899 my %suppress_ifbraces;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700900
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800901 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700902 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800903 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700904 my @setup_docs = ();
905 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -0700906
907 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800908 my $line;
909 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -0700910 $linenr++;
911 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800912
Andy Whitcroft773647a2008-03-28 14:15:58 -0700913 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700914 $setup_docs = 0;
915 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
916 $setup_docs = 1;
917 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700918 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700919 }
Andy Whitcroft773647a2008-03-28 14:15:58 -0700920 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
921 $realline=$1-1;
922 if (defined $2) {
923 $realcnt=$3+1;
924 } else {
925 $realcnt=1+1;
926 }
927
928 # Guestimate if this is a continuing comment. Run
929 # the context looking for a comment "edge". If this
930 # edge is a close comment then we must be in a comment
931 # at context start.
932 my $edge;
933 for (my $ln = $linenr; $ln < ($linenr + $realcnt); $ln++) {
934 next if ($line =~ /^-/);
935 ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
936 last if (defined $edge);
937 }
938 if (defined $edge && $edge eq '*/') {
939 $in_comment = 1;
940 }
941
942 # Guestimate if this is a continuing comment. If this
943 # is the start of a diff block and this line starts
944 # ' *' then it is very likely a comment.
945 if (!defined $edge &&
946 $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
947 {
948 $in_comment = 1;
949 }
950
951 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
952 sanitise_line_reset($in_comment);
953
954 } elsif ($realcnt) {
955 # Standardise the strings and chars within the input to
956 # simplify matching.
957 $line = sanitise_line($rawline);
958 }
959 push(@lines, $line);
960
961 if ($realcnt > 1) {
962 $realcnt-- if ($line =~ /^(?:\+| |$)/);
963 } else {
964 $realcnt = 0;
965 }
966
967 #print "==>$rawline\n";
968 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700969
970 if ($setup_docs && $line =~ /^\+/) {
971 push(@setup_docs, $line);
972 }
973 }
974
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700975 $prefix = '';
976
Andy Whitcroft773647a2008-03-28 14:15:58 -0700977 $realcnt = 0;
978 $linenr = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700979 foreach my $line (@lines) {
980 $linenr++;
981
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800982 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700983
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700984#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700985 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700986 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700987 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700988 $realline=$1-1;
989 if (defined $2) {
990 $realcnt=$3+1;
991 } else {
992 $realcnt=1+1;
993 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800994 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800995 $prev_values = 'E';
996
Andy Whitcroft773647a2008-03-28 14:15:58 -0700997 %suppress_ifbraces = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700998 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700999
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001000# track the line number as we move through the hunk, note that
1001# new versions of GNU diff omit the leading space on completely
1002# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001003 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001004 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001005 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001006
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001007 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001008 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001009
1010 # Track the previous line.
1011 ($prevline, $stashline) = ($stashline, $line);
1012 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001013 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1014
Andy Whitcroft773647a2008-03-28 14:15:58 -07001015 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001016
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001017 } elsif ($realcnt == 1) {
1018 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001019 }
1020
1021#make up the handle for any error we report on this line
Andy Whitcroft773647a2008-03-28 14:15:58 -07001022 $prefix = "$filename:$realline: " if ($emacs && $file);
1023 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1024
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001025 $here = "#$linenr: " if (!$file);
1026 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001027
1028 # extract the filename as it passes
1029 if ($line=~/^\+\+\+\s+(\S+)/) {
1030 $realfile = $1;
1031 $realfile =~ s@^[^/]*/@@;
1032
1033 if ($realfile =~ m@include/asm/@) {
1034 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1035 }
1036 next;
1037 }
1038
Randy Dunlap389834b2007-06-08 13:47:03 -07001039 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001040
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001041 my $hereline = "$here\n$rawline\n";
1042 my $herecurr = "$here\n$rawline\n";
1043 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001044
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001045 $cnt_lines++ if ($realcnt != 0);
1046
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001047#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001048 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001049 # This is a signoff, if ugly, so do not double report.
1050 $signoff++;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001051 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001052 WARN("Signed-off-by: is the preferred form\n" .
1053 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001054 }
1055 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001056 WARN("space required after Signed-off-by:\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001057 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001058 }
1059 }
1060
Andy Whitcroft00df3442007-06-08 13:47:06 -07001061# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001062 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001063 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001064 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001065 }
1066
1067# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1068 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001069 !($rawline =~ m/^(
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001070 [\x09\x0A\x0D\x20-\x7E] # ASCII
1071 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
1072 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
1073 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
1074 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
1075 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
1076 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
1077 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
1078 )*$/x )) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001079 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001080 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001081
Andy Whitcroft00df3442007-06-08 13:47:06 -07001082#ignore lines being removed
1083 if ($line=~/^-/) {next;}
1084
1085# check we are in a valid source file if not then ignore this hunk
1086 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001087
1088#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001089 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001090 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001091 ERROR("DOS line endings\n" . $herevet);
1092
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001093 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1094 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001095 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001096 }
1097#80 column limit
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001098 if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001099 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001100 }
1101
Andy Whitcroft8905a672007-11-28 16:21:06 -08001102# check for adding lines without a newline.
1103 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1104 WARN("adding a line without newline at end of file\n" . $herecurr);
1105 }
1106
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001107# check we are in a valid source file *.[hc] if not then ignore this hunk
1108 next if ($realfile !~ /\.[hc]$/);
1109
1110# at the beginning of a line any tabs must come first and anything
1111# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001112 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1113 $rawline =~ /^\+\s* \s*/) {
1114 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001115 ERROR("use tabs not spaces\n" . $herevet);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001116 }
1117
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001118# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001119 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001120 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1121 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001122
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001123# Check for potential 'bare' types
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001124 if ($realcnt) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001125 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1126 $s =~ s/\n./ /g;
1127 $s =~ s/{.*$//;
1128
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001129 # Ignore goto labels.
Andy Whitcroftcf655042008-03-04 14:28:20 -08001130 if ($s =~ /$Ident:\*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001131
1132 # Ignore functions being called
Andy Whitcroftcf655042008-03-04 14:28:20 -08001133 } elsif ($s =~ /^.\s*$Ident\s*\(/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001134
Andy Whitcroft8905a672007-11-28 16:21:06 -08001135 # definitions in global scope can only start with types
Andy Whitcroftcf655042008-03-04 14:28:20 -08001136 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
1137 possible($1, $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001138
1139 # declarations always start with types
Andy Whitcroftcf655042008-03-04 14:28:20 -08001140 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) {
1141 possible($1, $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001142 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001143
1144 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroftcf655042008-03-04 14:28:20 -08001145 while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
1146 possible($1, $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001147 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001148
1149 # Check for any sort of function declaration.
1150 # int foo(something bar, other baz);
1151 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001152 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001153 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001154
Andy Whitcroftcf655042008-03-04 14:28:20 -08001155 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001156 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001157 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001158
Andy Whitcroft8905a672007-11-28 16:21:06 -08001159 for my $arg (split(/\s*,\s*/, $ctx)) {
1160 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
1161
Andy Whitcroftcf655042008-03-04 14:28:20 -08001162 possible($1, $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001163 }
1164 }
1165 }
1166
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001167 }
1168
Andy Whitcroft653d4872007-06-23 17:16:34 -07001169#
1170# Checks which may be anchored in the context.
1171#
1172
1173# Check for switch () and associated case and default
1174# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07001175 if ($line=~/\bswitch\s*\(.*\)/) {
1176 my $err = '';
1177 my $sep = '';
1178 my @ctx = ctx_block_outer($linenr, $realcnt);
1179 shift(@ctx);
1180 for my $ctx (@ctx) {
1181 my ($clen, $cindent) = line_stats($ctx);
1182 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1183 $indent != $cindent) {
1184 $err .= "$sep$ctx\n";
1185 $sep = '';
1186 } else {
1187 $sep = "[...]\n";
1188 }
1189 }
1190 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001191 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001192 }
1193 }
1194
1195# if/while/etc brace do not go on next line, unless defining a do while loop,
1196# or if that brace on the next line is for something else
Andy Whitcroft773647a2008-03-28 14:15:58 -07001197 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
1198 my $pre_ctx = "$1$2";
1199
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001200 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001201 my $ctx_ln = $linenr + $#ctx + 1;
1202 my $ctx_cnt = $realcnt - $#ctx - 1;
1203 my $ctx = join("\n", @ctx);
1204
Andy Whitcroft773647a2008-03-28 14:15:58 -07001205 ##warn "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001206
Andy Whitcroft773647a2008-03-28 14:15:58 -07001207 # Skip over any removed lines in the context following statement.
1208 while (defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^-/) {
1209 $ctx_ln++;
1210 }
1211 ##warn "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1212
1213 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1214 ERROR("that open brace { should be on the previous line\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001215 "$here\n$ctx\n$lines[$ctx_ln - 1]");
Andy Whitcroft00df3442007-06-08 13:47:06 -07001216 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001217 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1218 $ctx =~ /\)\s*\;\s*$/ &&
1219 defined $lines[$ctx_ln - 1])
1220 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001221 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1222 if ($nindent > $indent) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001223 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001224 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1225 }
1226 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001227 }
1228
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001229 # Track the 'values' across context and added lines.
1230 my $opline = $line; $opline =~ s/^./ /;
1231 my $curr_values = annotate_values($opline . "\n", $prev_values);
1232 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001233 if ($dbg_values) {
1234 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001235 print "$linenr > .$outline\n";
1236 print "$linenr > $curr_values\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001237 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001238 $prev_values = substr($curr_values, -1);
1239
Andy Whitcroft00df3442007-06-08 13:47:06 -07001240#ignore lines not being added
1241 if ($line=~/^[^\+]/) {next;}
1242
Andy Whitcroft653d4872007-06-23 17:16:34 -07001243# TEST: allow direct testing of the type matcher.
1244 if ($tst_type && $line =~ /^.$Declare$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001245 ERROR("TEST: is type $Declare\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001246 next;
1247 }
1248
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001249# check for initialisation to aggregates open brace on the next line
1250 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1251 $line =~ /^.\s*{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001252 ERROR("that open brace { should be on the previous line\n" . $hereprev);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001253 }
1254
Andy Whitcroft653d4872007-06-23 17:16:34 -07001255#
1256# Checks which are anchored on the added line.
1257#
1258
1259# check for malformed paths in #include statements (uses RAW line)
1260 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1261 my $path = $1;
1262 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001263 ERROR("malformed #include filename\n" .
1264 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001265 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001266 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001267
1268# no C99 // comments
1269 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001270 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001271 }
1272 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001273 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001274 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001275
1276#EXPORT_SYMBOL should immediately follow its function closing }.
Andy Whitcroft653d4872007-06-23 17:16:34 -07001277 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1278 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1279 my $name = $1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001280 if (($prevline !~ /^}/) &&
1281 ($prevline !~ /^\+}/) &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001282 ($prevline !~ /^ }/) &&
Andy Whitcroftcf655042008-03-04 14:28:20 -08001283 ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1284 ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1285 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001286 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001287 }
1288 }
1289
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001290# check for external initialisers.
1291 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
1292 ERROR("do not initialise externals to 0 or NULL\n" .
1293 $herecurr);
1294 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001295# check for static initialisers.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001296 if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001297 ERROR("do not initialise statics to 0 or NULL\n" .
1298 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001299 }
1300
Andy Whitcroft653d4872007-06-23 17:16:34 -07001301# check for new typedefs, only function parameters and sparse annotations
1302# make sense.
1303 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001304 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001305 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001306 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001307 }
1308
1309# * goes on variable not on type
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001310 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001311 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1312 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001313
1314 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001315 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1316 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001317
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001318 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001319 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1320 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001321
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001322 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001323 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1324 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001325 }
1326
1327# # no BUG() or BUG_ON()
1328# if ($line =~ /\b(BUG|BUG_ON)\b/) {
1329# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1330# print "$herecurr";
1331# $clean = 0;
1332# }
1333
Andy Whitcroft8905a672007-11-28 16:21:06 -08001334 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001335 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 -08001336 }
1337
Andy Whitcroft00df3442007-06-08 13:47:06 -07001338# printk should use KERN_* levels. Note that follow on printk's on the
1339# same line do not need a level, so we use the current block context
1340# to try and find and validate the current printk. In summary the current
1341# printk includes all preceeding printk's which have no newline on the end.
1342# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001343 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001344 my $ok = 0;
1345 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1346 #print "CHECK<$lines[$ln - 1]\n";
1347 # we have a preceeding printk if it ends
1348 # with "\n" ignore it, else it is to blame
1349 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1350 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1351 $ok = 1;
1352 }
1353 last;
1354 }
1355 }
1356 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001357 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001358 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001359 }
1360
Andy Whitcroft653d4872007-06-23 17:16:34 -07001361# function brace can't be on same line, except for #defines of do while,
1362# or if closed on same line
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001363 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001364 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001365 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001366 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001367
Andy Whitcroft8905a672007-11-28 16:21:06 -08001368# open braces for enum, union and struct go on the same line.
1369 if ($line =~ /^.\s*{/ &&
1370 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1371 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1372 }
1373
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001374# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001375 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001376 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001377 my $ctx_before = substr($line, 0, $-[1]);
1378 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001379
1380 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001381 if ($name =~ /^(?:
1382 if|for|while|switch|return|case|
1383 volatile|__volatile__|
1384 __attribute__|format|__extension__|
1385 asm|__asm__)$/x)
1386 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001387
1388 # cpp #define statements have non-optional spaces, ie
1389 # if there is a space between the name and the open
1390 # parenthesis it is simply not a parameter group.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001391 } elsif ($ctx_before =~ /^.\#\s*define\s*$/) {
1392
1393 # cpp #elif statement condition may start with a (
1394 } elsif ($ctx =~ /^.\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001395
1396 # If this whole things ends with a type its most
1397 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001398 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001399
1400 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001401 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001402 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001403 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001404# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001405 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001406 my $ops = qr{
1407 <<=|>>=|<=|>=|==|!=|
1408 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1409 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001410 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001411 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001412 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001413 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001414
1415 my $blank = copy_spacing($opline);
1416
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001417 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001418 $off += length($elements[$n]);
1419
Andy Whitcroft773647a2008-03-28 14:15:58 -07001420 # Pick up the preceeding and succeeding characters.
1421 my $ca = substr($opline, 0, $off);
1422 my $cc = '';
1423 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1424 $cc = substr($opline, $off + length($elements[$n + 1]));
1425 }
1426 my $cb = "$ca$;$cc";
1427
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001428 my $a = '';
1429 $a = 'V' if ($elements[$n] ne '');
1430 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001431 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001432 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1433 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07001434 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001435
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001436 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001437
1438 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001439 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001440 $c = 'V' if ($elements[$n + 2] ne '');
1441 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001442 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001443 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1444 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001445 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001446 } else {
1447 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001448 }
1449
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001450 my $ctx = "${a}x${c}";
1451
1452 my $at = "(ctx:$ctx)";
1453
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001454 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001455 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001456
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001457 # Classify operators into binary, unary, or
1458 # definitions (* only) where they have more
1459 # than one mode.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001460 my $op_type = substr($curr_values, $off + 1, 1);
1461 my $op_left = substr($curr_values, $off, 1);
1462 my $is_unary;
1463 if ($op_type eq 'T') {
1464 $is_unary = 2;
1465 } elsif ($op_left eq 'V') {
1466 $is_unary = 0;
1467 } else {
1468 $is_unary = 1;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001469 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001470 #if ($op eq '-' || $op eq '&' || $op eq '*') {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001471 # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001472 #}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001473
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001474 # Ignore operators passed as parameters.
1475 if ($op_type ne 'V' &&
1476 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1477
Andy Whitcroftcf655042008-03-04 14:28:20 -08001478# # Ignore comments
1479# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001480
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001481 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001482 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001483 if ($ctx !~ /.x[WEBC]/ &&
1484 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001485 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001486 }
1487
1488 # // is a comment
1489 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001490
1491 # -> should have no spaces
1492 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001493 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001494 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001495 }
1496
1497 # , must have a space on the right.
1498 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001499 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001500 ERROR("space required after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001501 }
1502
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001503 # '*' as part of a type definition -- reported already.
1504 } elsif ($op eq '*' && $is_unary == 2) {
1505 #warn "'*' is part of type\n";
1506
1507 # unary operators should have a space before and
1508 # none after. May be left adjacent to another
1509 # unary operator, or a cast
1510 } elsif ($op eq '!' || $op eq '~' ||
1511 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001512 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001513 ERROR("space required before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001514 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001515 if ($ctx =~ /.xW/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001516 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001517 }
1518
1519 # unary ++ and unary -- are allowed no space on one side.
1520 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001521 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1522 ERROR("space required one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001523 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001524 if ($ctx =~ /Wx[BE]/ ||
1525 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1526 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001527 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001528 if ($ctx =~ /ExW/) {
1529 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1530 }
1531
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001532
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001533 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001534 } elsif ($op eq '<<' or $op eq '>>' or
1535 $op eq '&' or $op eq '^' or $op eq '|' or
1536 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001537 $op eq '*' or $op eq '/' or
1538 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001539 {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001540 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001541 ERROR("need consistent spacing around '$op' $at\n" .
1542 $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001543 }
1544
1545 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08001546 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001547 # Ignore email addresses <foo@bar>
1548 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1549 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001550 ERROR("spaces required around that '$op' $at\n" . $hereptr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001551 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001552 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001553 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001554 }
1555 }
1556
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001557# check for multiple assignments
1558 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001559 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001560 }
1561
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001562## # check for multiple declarations, allowing for a function declaration
1563## # continuation.
1564## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1565## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1566##
1567## # Remove any bracketed sections to ensure we do not
1568## # falsly report the parameters of functions.
1569## my $ln = $line;
1570## while ($ln =~ s/\([^\(\)]*\)//g) {
1571## }
1572## if ($ln =~ /,/) {
1573## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1574## }
1575## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001576
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001577#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001578 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1579 $line =~ /do{/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001580 ERROR("space required before the open brace '{'\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001581 }
1582
1583# closing brace should have a space following it when it has anything
1584# on the line
1585 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001586 ERROR("space required after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001587 }
1588
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001589# check spacing on square brackets
1590 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001591 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001592 }
1593 if ($line =~ /\s\]/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001594 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001595 }
1596
1597# check spacing on paretheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001598 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1599 $line !~ /for\s*\(\s+;/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001600 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001601 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001602 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001603 $line !~ /for\s*\(.*;\s+\)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001604 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001605 }
1606
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001607#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001608 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001609 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001610 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001611 }
1612
1613# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001614 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001615 ERROR("space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001616 }
1617
1618# Check for illegal assignment in if conditional.
Andy Whitcroft8905a672007-11-28 16:21:06 -08001619 if ($line =~ /\bif\s*\(/) {
1620 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1621
1622 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001623 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001624 }
1625
1626 # Find out what is on the end of the line after the
1627 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001628 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08001629 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001630 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft773647a2008-03-28 14:15:58 -07001631 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/ &&
1632 $c !~ /^.\#\s*if/)
1633 {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001634 ERROR("trailing statements should be on next line\n" . $herecurr);
1635 }
1636 }
1637
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001638# Check for bitwise tests written as boolean
1639 if ($line =~ /
1640 (?:
1641 (?:\[|\(|\&\&|\|\|)
1642 \s*0[xX][0-9]+\s*
1643 (?:\&\&|\|\|)
1644 |
1645 (?:\&\&|\|\|)
1646 \s*0[xX][0-9]+\s*
1647 (?:\&\&|\|\||\)|\])
1648 )/x)
1649 {
1650 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
1651 }
1652
Andy Whitcroft8905a672007-11-28 16:21:06 -08001653# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001654 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
1655 my $s = $1;
1656 $s =~ s/$;//g; # Remove any comments
1657 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
1658 ERROR("trailing statements should be on next line\n" . $herecurr);
1659 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001660 }
1661
1662 # Check for }<nl>else {, these must be at the same
1663 # indent level to be relevant to each other.
1664 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
1665 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001666 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001667 }
1668
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001669 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1670 $previndent == $indent) {
1671 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1672
1673 # Find out what is on the end of the line after the
1674 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07001675 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001676 $s =~ s/\n.*//g;
1677
1678 if ($s =~ /^\s*;/) {
1679 ERROR("while should follow close brace '}'\n" . $hereprev);
1680 }
1681 }
1682
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001683#studly caps, commented out until figure out how to distinguish between use of existing and adding new
1684# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
1685# print "No studly caps, use _\n";
1686# print "$herecurr";
1687# $clean = 0;
1688# }
1689
1690#no spaces allowed after \ in define
1691 if ($line=~/\#define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001692 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001693 }
1694
Andy Whitcroft653d4872007-06-23 17:16:34 -07001695#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1696 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001697 my $checkfile = "$root/include/linux/$1.h";
1698 if (-f $checkfile && $1 ne 'irq.h') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001699 WARN("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001700 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001701 }
1702 }
1703
Andy Whitcroft653d4872007-06-23 17:16:34 -07001704# multi-statement macros should be enclosed in a do while loop, grab the
1705# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08001706# in a known good container
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001707 if ($prevline =~ /\#define.*\\/ &&
1708 $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1709 $line !~ /(?:do\s+{|\(\{|\{)/ &&
1710 $line !~ /^.\s*$Declare\s/) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001711 # Grab the first statement, if that is the entire macro
1712 # its ok. This may start either on the #define line
1713 # or the one below.
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001714 my $ln = $linenr;
1715 my $cnt = $realcnt;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001716 my $off = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001717
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001718 # If the macro starts on the define line start
1719 # grabbing the statement after the identifier
1720 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1721 ##print "1<$1> 2<$2>\n";
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001722 if (defined $2 && $2 ne '') {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001723 $off = length($1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001724 $ln--;
1725 $cnt++;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001726 while ($lines[$ln - 1] =~ /^-/) {
1727 $ln--;
1728 $cnt++;
1729 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001730 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001731 my @ctx = ctx_statement($ln, $cnt, $off);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001732 my $ctx_ln = $ln + $#ctx + 1;
1733 my $ctx = join("\n", @ctx);
1734
1735 # Pull in any empty extension lines.
1736 while ($ctx =~ /\\$/ &&
1737 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1738 $ctx .= $lines[$ctx_ln - 1];
1739 $ctx_ln++;
1740 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001741
1742 if ($ctx =~ /\\$/) {
1743 if ($ctx =~ /;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001744 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 -07001745 } else {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001746 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001747 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001748 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001749 }
1750
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001751# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001752 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
1753 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08001754 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001755 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001756 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1757 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001758 my $allowed = 0;
1759 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001760 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001761 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001762 for my $chunk (@chunks) {
1763 my ($cond, $block) = @{$chunk};
1764
Andy Whitcroft773647a2008-03-28 14:15:58 -07001765 # If the condition carries leading newlines, then count those as offsets.
1766 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
1767 my $offset = statement_rawlines($whitespace) - 1;
1768
1769 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
1770
1771 # We have looked at and allowed this specific line.
1772 $suppress_ifbraces{$ln + $offset} = 1;
1773
1774 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001775 $ln += statement_rawlines($block) - 1;
1776
Andy Whitcroft773647a2008-03-28 14:15:58 -07001777 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001778
1779 $seen++ if ($block =~ /^\s*{/);
1780
Andy Whitcroftcf655042008-03-04 14:28:20 -08001781 #print "cond<$cond> block<$block> allowed<$allowed>\n";
1782 if (statement_lines($cond) > 1) {
1783 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001784 $allowed = 1;
1785 }
1786 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001787 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001788 $allowed = 1;
1789 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001790 if (statement_block_size($block) > 1) {
1791 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001792 $allowed = 1;
1793 }
1794 }
1795 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001796 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001797 }
1798 }
1799 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001800 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001801 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001802 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001803
Andy Whitcroftcf655042008-03-04 14:28:20 -08001804 # Check the pre-context.
1805 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
1806 #print "APW: ALLOWED: pre<$1>\n";
1807 $allowed = 1;
1808 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001809
1810 my ($level, $endln, @chunks) =
1811 ctx_statement_full($linenr, $realcnt, $-[0]);
1812
Andy Whitcroftcf655042008-03-04 14:28:20 -08001813 # Check the condition.
1814 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07001815 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001816 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001817 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08001818 }
1819 if (statement_lines($cond) > 1) {
1820 #print "APW: ALLOWED: cond<$cond>\n";
1821 $allowed = 1;
1822 }
1823 if ($block =~/\b(?:if|for|while)\b/) {
1824 #print "APW: ALLOWED: block<$block>\n";
1825 $allowed = 1;
1826 }
1827 if (statement_block_size($block) > 1) {
1828 #print "APW: ALLOWED: lines block<$block>\n";
1829 $allowed = 1;
1830 }
1831 # Check the post-context.
1832 if (defined $chunks[1]) {
1833 my ($cond, $block) = @{$chunks[1]};
1834 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001835 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001836 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001837 if ($block =~ /^\s*\{/) {
1838 #print "APW: ALLOWED: chunk-1 block<$block>\n";
1839 $allowed = 1;
1840 }
1841 }
1842 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
1843 my $herectx = $here . "\n";;
1844 my $end = $linenr + statement_rawlines($block) - 1;
1845
1846 for (my $ln = $linenr - 1; $ln < $end; $ln++) {
1847 $herectx .= $rawlines[$ln] . "\n";;
1848 }
1849
1850 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001851 }
1852 }
1853
Andy Whitcroft653d4872007-06-23 17:16:34 -07001854# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001855 for my $inc (@dep_includes) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001856 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001857 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001858 }
1859 }
1860
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001861# don't use deprecated functions
1862 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001863 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001864 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001865 }
1866 }
1867
1868# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001869 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
1870 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001871 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001872 }
1873
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001874# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
1875 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
1876 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
1877 }
1878
Andy Whitcroft00df3442007-06-08 13:47:06 -07001879# warn about #if 0
1880 if ($line =~ /^.#\s*if\s+0\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001881 CHK("if this code is redundant consider removing it\n" .
1882 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001883 }
1884
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001885# check for needless kfree() checks
1886 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1887 my $expr = $1;
1888 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1889 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1890 }
1891 }
1892
Andy Whitcroft00df3442007-06-08 13:47:06 -07001893# warn about #ifdefs in C files
1894# if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1895# print "#ifdef in C files should be avoided\n";
1896# print "$herecurr";
1897# $clean = 0;
1898# }
1899
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001900# warn about spacing in #ifdefs
1901 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1902 ERROR("exactly one space required after that #$1\n" . $herecurr);
1903 }
1904
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001905# check for spinlock_t definitions without a comment.
1906 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
1907 my $which = $1;
1908 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001909 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001910 }
1911 }
1912# check for memory barriers without a comment.
1913 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
1914 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001915 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001916 }
1917 }
1918# check of hardware specific defines
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001919 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001920 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001921 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001922
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001923# check the location of the inline attribute, that it is between
1924# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001925 if ($line =~ /\b$Type\s+$Inline\b/ ||
1926 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001927 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1928 }
1929
Andy Whitcroft8905a672007-11-28 16:21:06 -08001930# Check for __inline__ and __inline, prefer inline
1931 if ($line =~ /\b(__inline__|__inline)\b/) {
1932 WARN("plain inline is preferred over $1\n" . $herecurr);
1933 }
1934
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001935# check for new externs in .c files.
1936 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
1937 WARN("externs should be avoided in .c files\n" . $herecurr);
1938 }
1939
1940# checks for new __setup's
1941 if ($rawline =~ /\b__setup\("([^"]*)"/) {
1942 my $name = $1;
1943
1944 if (!grep(/$name/, @setup_docs)) {
1945 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
1946 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001947 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001948
1949# check for pointless casting of kmalloc return
1950 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
1951 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
1952 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001953
1954# check for gcc specific __FUNCTION__
1955 if ($line =~ /__FUNCTION__/) {
1956 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
1957 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001958
1959# check for semaphores used as mutexes
1960 if ($line =~ /\b(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
1961 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
1962 }
1963# check for semaphores used as mutexes
1964 if ($line =~ /\binit_MUTEX_LOCKED\s*\(/) {
1965 WARN("consider using a completion\n" . $herecurr);
1966 }
1967# recommend strict_strto* over simple_strto*
1968 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
1969 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
1970 }
1971
1972# use of NR_CPUS is usually wrong
1973# ignore definitions of NR_CPUS and usage to define arrays as likely right
1974 if ($line =~ /\bNR_CPUS\b/ &&
1975 $line !~ /^.#\s*define\s+NR_CPUS\s+/ &&
1976 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/)
1977 {
1978 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
1979 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001980 }
1981
1982 # If we have no input at all, then there is nothing to report on
1983 # so just keep quiet.
1984 if ($#rawlines == -1) {
1985 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001986 }
1987
Andy Whitcroft8905a672007-11-28 16:21:06 -08001988 # In mailback mode only produce a report in the negative, for
1989 # things that appear to be patches.
1990 if ($mailback && ($clean == 1 || !$is_patch)) {
1991 exit(0);
1992 }
1993
1994 # This is not a patch, and we are are in 'no-patch' mode so
1995 # just keep quiet.
1996 if (!$chk_patch && !$is_patch) {
1997 exit(0);
1998 }
1999
2000 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002001 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002002 }
2003 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002004 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002005 }
2006
Andy Whitcroft8905a672007-11-28 16:21:06 -08002007 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002008 if ($summary && !($clean == 1 && $quiet == 1)) {
2009 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002010 print "total: $cnt_error errors, $cnt_warn warnings, " .
2011 (($check)? "$cnt_chk checks, " : "") .
2012 "$cnt_lines lines checked\n";
2013 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07002014 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08002015
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002016 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002017 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002018 }
2019 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002020 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002021 print "are false positives report them to the maintainer, see\n";
2022 print "CHECKPATCH in MAINTAINERS.\n";
2023 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002024
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002025 return $clean;
2026}