blob: 2a7cef9726e4892b3454e7198dca4c529aeed891 [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 Whitcroftcf655042008-03-04 14:28:20 -080012my $V = '0.15';
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 Whitcroft6c72ffa2007-10-18 03:05:08 -070021my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080022my $terse = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070023my $file = 0;
24my $check = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080025my $summary = 1;
26my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080027my $summary_file = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070028my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080029my %debug;
Andy Whitcroft0a920b52007-06-01 00:46:48 -070030GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070031 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -070032 'tree!' => \$tree,
33 'signoff!' => \$chk_signoff,
34 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070035 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -080036 'terse!' => \$terse,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070037 'file!' => \$file,
38 'subjective!' => \$check,
39 'strict!' => \$check,
40 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -080041 'summary!' => \$summary,
42 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -080043 'summary-file!' => \$summary_file,
44
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080045 'debug=s' => \%debug,
Andy Whitcroft13214ad2008-02-08 04:22:03 -080046 'test-type!' => \$tst_type,
Andy Whitcroft0a920b52007-06-01 00:46:48 -070047) or exit;
48
49my $exit = 0;
50
51if ($#ARGV < 0) {
Andy Whitcroft00df3442007-06-08 13:47:06 -070052 print "usage: $P [options] patchfile\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -070053 print "version: $V\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -080054 print "options: -q => quiet\n";
55 print " --no-tree => run without a kernel tree\n";
56 print " --terse => one line per report\n";
57 print " --emacs => emacs compile window format\n";
58 print " --file => check a source file\n";
59 print " --strict => enable more subjective tests\n";
60 print " --root => path to the kernel tree root\n";
61 print " --no-summary => suppress the per-file summary\n";
62 print " --summary-file => include the filename in summary\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -070063 exit(1);
64}
65
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080066my $dbg_values = 0;
67my $dbg_possible = 0;
68for my $key (keys %debug) {
69 eval "\${dbg_$key} = '$debug{$key}';"
70}
71
Andy Whitcroft8905a672007-11-28 16:21:06 -080072if ($terse) {
73 $emacs = 1;
74 $quiet++;
75}
76
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070077if ($tree) {
78 if (defined $root) {
79 if (!top_of_kernel_tree($root)) {
80 die "$P: $root: --root does not point at a valid tree\n";
81 }
82 } else {
83 if (top_of_kernel_tree('.')) {
84 $root = '.';
85 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
86 top_of_kernel_tree($1)) {
87 $root = $1;
88 }
89 }
90
91 if (!defined $root) {
92 print "Must be run from the top-level dir. of a kernel tree\n";
93 exit(2);
94 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -070095}
96
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070097my $emitted_corrupt = 0;
98
99our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
100our $Storage = qr{extern|static|asmlinkage};
101our $Sparse = qr{
102 __user|
103 __kernel|
104 __force|
105 __iomem|
106 __must_check|
107 __init_refok|
Andy Whitcroftcf655042008-03-04 14:28:20 -0800108 __kprobes
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700109 }x;
110our $Attribute = qr{
111 const|
112 __read_mostly|
113 __kprobes|
114 __(?:mem|cpu|dev|)(?:initdata|init)
115 }x;
116our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700117our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
118our $Lval = qr{$Ident(?:$Member)*};
119
120our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
121our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
122our $Operators = qr{
123 <=|>=|==|!=|
124 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800125 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700126 }x;
127
Andy Whitcroft8905a672007-11-28 16:21:06 -0800128our $NonptrType;
129our $Type;
130our $Declare;
131
132our @typeList = (
133 qr{void},
134 qr{char},
135 qr{short},
136 qr{int},
137 qr{long},
138 qr{unsigned},
139 qr{float},
140 qr{double},
141 qr{bool},
142 qr{long\s+int},
143 qr{long\s+long},
144 qr{long\s+long\s+int},
145 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
146 qr{struct\s+$Ident},
147 qr{union\s+$Ident},
148 qr{enum\s+$Ident},
149 qr{${Ident}_t},
150 qr{${Ident}_handler},
151 qr{${Ident}_handler_fn},
152);
153
154sub build_types {
155 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
156 $NonptrType = qr{
157 \b
158 (?:const\s+)?
159 (?:unsigned\s+)?
Andy Whitcroftcf655042008-03-04 14:28:20 -0800160 (?:
161 $all|
162 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)
163 )
Andy Whitcroft8905a672007-11-28 16:21:06 -0800164 (?:\s+$Sparse|\s+const)*
165 \b
166 }x;
167 $Type = qr{
168 \b$NonptrType\b
169 (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800170 (?:\s+$Inline|\s+$Sparse|\s+$Attribute)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800171 }x;
172 $Declare = qr{(?:$Storage\s+)?$Type};
173}
174build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700175
176$chk_signoff = 0 if ($file);
177
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700178my @dep_includes = ();
179my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700180my $removal = "Documentation/feature-removal-schedule.txt";
181if ($tree && -f "$root/$removal") {
182 open(REMOVE, "<$root/$removal") ||
183 die "$P: $removal: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700184 while (<REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700185 if (/^Check:\s+(.*\S)/) {
186 for my $entry (split(/[, ]+/, $1)) {
187 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700188 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700189
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700190 } elsif ($entry !~ m@/@) {
191 push(@dep_functions, $entry);
192 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700193 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700194 }
195 }
196}
197
Andy Whitcroft00df3442007-06-08 13:47:06 -0700198my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800199my @lines = ();
200my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700201for my $filename (@ARGV) {
202 if ($file) {
203 open(FILE, "diff -u /dev/null $filename|") ||
204 die "$P: $filename: diff failed - $!\n";
205 } else {
206 open(FILE, "<$filename") ||
207 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700208 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800209 if ($filename eq '-') {
210 $vname = 'Your patch';
211 } else {
212 $vname = $filename;
213 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700214 while (<FILE>) {
215 chomp;
216 push(@rawlines, $_);
217 }
218 close(FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800219 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700220 $exit = 1;
221 }
222 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800223 @lines = ();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700224}
225
226exit($exit);
227
228sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700229 my ($root) = @_;
230
231 my @tree_check = (
232 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
233 "README", "Documentation", "arch", "include", "drivers",
234 "fs", "init", "ipc", "kernel", "lib", "scripts",
235 );
236
237 foreach my $check (@tree_check) {
238 if (! -e $root . '/' . $check) {
239 return 0;
240 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700241 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700242 return 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700243}
244
245sub expand_tabs {
246 my ($str) = @_;
247
248 my $res = '';
249 my $n = 0;
250 for my $c (split(//, $str)) {
251 if ($c eq "\t") {
252 $res .= ' ';
253 $n++;
254 for (; ($n % 8) != 0; $n++) {
255 $res .= ' ';
256 }
257 next;
258 }
259 $res .= $c;
260 $n++;
261 }
262
263 return $res;
264}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700265sub copy_spacing {
266 my ($str) = @_;
267
268 my $res = '';
269 for my $c (split(//, $str)) {
270 if ($c eq "\t") {
271 $res .= $c;
272 } else {
273 $res .= ' ';
274 }
275 }
276
277 return $res;
278}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700279
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700280sub line_stats {
281 my ($line) = @_;
282
283 # Drop the diff line leader and expand tabs
284 $line =~ s/^.//;
285 $line = expand_tabs($line);
286
287 # Pick the indent from the front of the line.
288 my ($white) = ($line =~ /^(\s*)/);
289
290 return (length($line), length($white));
291}
292
Andy Whitcroft00df3442007-06-08 13:47:06 -0700293sub sanitise_line {
294 my ($line) = @_;
295
296 my $res = '';
297 my $l = '';
298
299 my $quote = '';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800300 my $qlen = 0;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700301
302 foreach my $c (split(//, $line)) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800303 # The second backslash of a pair is not a "quote".
304 if ($l eq "\\" && $c eq "\\") {
305 $c = 'X';
306 }
Andy Whitcroft00df3442007-06-08 13:47:06 -0700307 if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
308 if ($quote eq '') {
309 $quote = $c;
310 $res .= $c;
311 $l = $c;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800312 $qlen = 0;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700313 next;
314 } elsif ($quote eq $c) {
315 $quote = '';
316 }
317 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800318 if ($quote eq "'" && $qlen > 1) {
319 $quote = '';
320 }
Andy Whitcroft00df3442007-06-08 13:47:06 -0700321 if ($quote && $c ne "\t") {
322 $res .= "X";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800323 $qlen++;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700324 } else {
325 $res .= $c;
326 }
327
328 $l = $c;
329 }
330
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800331 # Clear out the comments.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800332 while ($res =~ m@(/\*.*?\*/)@g) {
333 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800334 }
335 if ($res =~ m@(/\*.*)@) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800336 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800337 }
338 if ($res =~ m@^.(.*\*/)@) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800339 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800340 }
341
342 # The pathname on a #include may be surrounded by '<' and '>'.
343 if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
344 my $clean = 'X' x length($1);
345 $res =~ s@\<.*\>@<$clean>@;
346
347 # The whole of a #error is a string.
348 } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
349 my $clean = 'X' x length($1);
350 $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@;
351 }
352
Andy Whitcroft00df3442007-06-08 13:47:06 -0700353 return $res;
354}
355
Andy Whitcroft8905a672007-11-28 16:21:06 -0800356sub ctx_statement_block {
357 my ($linenr, $remain, $off) = @_;
358 my $line = $linenr - 1;
359 my $blk = '';
360 my $soff = $off;
361 my $coff = $off - 1;
362
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800363 my $loff = 0;
364
Andy Whitcroft8905a672007-11-28 16:21:06 -0800365 my $type = '';
366 my $level = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800367 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800368 my $c;
369 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800370
371 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800372 while (1) {
373 #warn "CSB: blk<$blk>\n";
374 # If we are about to drop off the end, pull in more
375 # context.
376 if ($off >= $len) {
377 for (; $remain > 0; $line++) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800378 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800379 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800380 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800381 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800382 $len = length($blk);
383 $line++;
384 last;
385 }
386 # Bail if there is no further context.
387 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800388 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800389 last;
390 }
391 }
Andy Whitcroftcf655042008-03-04 14:28:20 -0800392 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800393 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800394 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800395
396 #warn "CSB: c<$c> type<$type> level<$level>\n";
397 # Statement ends at the ';' or a close '}' at the
398 # outermost level.
399 if ($level == 0 && $c eq ';') {
400 last;
401 }
402
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800403 # An else is really a conditional as long as its not else if
Andy Whitcroftcf655042008-03-04 14:28:20 -0800404 if ($level == 0 && (!defined($p) || $p =~ /(?:\s|\})/) &&
405 $remainder =~ /(else)(?:\s|{)/ &&
406 $remainder !~ /else\s+if\b/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800407 $coff = $off + length($1);
408 }
409
Andy Whitcroft8905a672007-11-28 16:21:06 -0800410 if (($type eq '' || $type eq '(') && $c eq '(') {
411 $level++;
412 $type = '(';
413 }
414 if ($type eq '(' && $c eq ')') {
415 $level--;
416 $type = ($level != 0)? '(' : '';
417
418 if ($level == 0 && $coff < $soff) {
419 $coff = $off;
420 }
421 }
422 if (($type eq '' || $type eq '{') && $c eq '{') {
423 $level++;
424 $type = '{';
425 }
426 if ($type eq '{' && $c eq '}') {
427 $level--;
428 $type = ($level != 0)? '{' : '';
429
430 if ($level == 0) {
431 last;
432 }
433 }
434 $off++;
435 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800436 if ($off == $len) {
437 $line++;
438 $remain--;
439 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800440
441 my $statement = substr($blk, $soff, $off - $soff + 1);
442 my $condition = substr($blk, $soff, $coff - $soff + 1);
443
444 #warn "STATEMENT<$statement>\n";
445 #warn "CONDITION<$condition>\n";
446
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800447 #print "off<$off> loff<$loff>\n";
448
449 return ($statement, $condition,
450 $line, $remain + 1, $off - $loff + 1, $level);
451}
452
Andy Whitcroftcf655042008-03-04 14:28:20 -0800453sub statement_lines {
454 my ($stmt) = @_;
455
456 # Strip the diff line prefixes and rip blank lines at start and end.
457 $stmt =~ s/(^|\n)./$1/g;
458 $stmt =~ s/^\s*//;
459 $stmt =~ s/\s*$//;
460
461 my @stmt_lines = ($stmt =~ /\n/g);
462
463 return $#stmt_lines + 2;
464}
465
466sub statement_rawlines {
467 my ($stmt) = @_;
468
469 my @stmt_lines = ($stmt =~ /\n/g);
470
471 return $#stmt_lines + 2;
472}
473
474sub statement_block_size {
475 my ($stmt) = @_;
476
477 $stmt =~ s/(^|\n)./$1/g;
478 $stmt =~ s/^\s*{//;
479 $stmt =~ s/}\s*$//;
480 $stmt =~ s/^\s*//;
481 $stmt =~ s/\s*$//;
482
483 my @stmt_lines = ($stmt =~ /\n/g);
484 my @stmt_statements = ($stmt =~ /;/g);
485
486 my $stmt_lines = $#stmt_lines + 2;
487 my $stmt_statements = $#stmt_statements + 1;
488
489 if ($stmt_lines > $stmt_statements) {
490 return $stmt_lines;
491 } else {
492 return $stmt_statements;
493 }
494}
495
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800496sub ctx_statement_full {
497 my ($linenr, $remain, $off) = @_;
498 my ($statement, $condition, $level);
499
500 my (@chunks);
501
Andy Whitcroftcf655042008-03-04 14:28:20 -0800502 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800503 ($statement, $condition, $linenr, $remain, $off, $level) =
504 ctx_statement_block($linenr, $remain, $off);
505 #print "F: c<$condition> s<$statement>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -0800506 push(@chunks, [ $condition, $statement ]);
507 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
508 return ($level, $linenr, @chunks);
509 }
510
511 # Pull in the following conditional/block pairs and see if they
512 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800513 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800514 ($statement, $condition, $linenr, $remain, $off, $level) =
515 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800516 #print "C: c<$condition> s<$statement> remain<$remain>\n";
517 last if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:else|do)\b/s));
518 #print "C: push\n";
519 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800520 }
521
522 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800523}
524
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700525sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700526 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700527 my $line;
528 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700529 my $blk = '';
530 my @o;
531 my @c;
532 my @res = ();
533
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700534 my $level = 0;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700535 for ($line = $start; $remain > 0; $line++) {
536 next if ($rawlines[$line] =~ /^-/);
537 $remain--;
538
539 $blk .= $rawlines[$line];
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700540 foreach my $c (split(//, $rawlines[$line])) {
541 ##print "C<$c>L<$level><$open$close>O<$off>\n";
542 if ($off > 0) {
543 $off--;
544 next;
545 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700546
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700547 if ($c eq $close && $level > 0) {
548 $level--;
549 last if ($level == 0);
550 } elsif ($c eq $open) {
551 $level++;
552 }
553 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700554
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700555 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700556 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700557 }
558
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700559 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700560 }
561
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700562 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700563}
564sub ctx_block_outer {
565 my ($linenr, $remain) = @_;
566
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700567 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
568 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700569}
570sub ctx_block {
571 my ($linenr, $remain) = @_;
572
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700573 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
574 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700575}
576sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700577 my ($linenr, $remain, $off) = @_;
578
579 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
580 return @r;
581}
582sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700583 my ($linenr, $remain) = @_;
584
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700585 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700586}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700587sub ctx_statement_level {
588 my ($linenr, $remain, $off) = @_;
589
590 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
591}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700592
593sub ctx_locate_comment {
594 my ($first_line, $end_line) = @_;
595
596 # Catch a comment on the end of the line itself.
Andy Whitcroft00df3442007-06-08 13:47:06 -0700597 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700598 return $current_comment if (defined $current_comment);
599
600 # Look through the context and try and figure out if there is a
601 # comment.
602 my $in_comment = 0;
603 $current_comment = '';
604 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -0700605 my $line = $rawlines[$linenr - 1];
606 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700607 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
608 $in_comment = 1;
609 }
610 if ($line =~ m@/\*@) {
611 $in_comment = 1;
612 }
613 if (!$in_comment && $current_comment ne '') {
614 $current_comment = '';
615 }
616 $current_comment .= $line . "\n" if ($in_comment);
617 if ($line =~ m@\*/@) {
618 $in_comment = 0;
619 }
620 }
621
622 chomp($current_comment);
623 return($current_comment);
624}
625sub ctx_has_comment {
626 my ($first_line, $end_line) = @_;
627 my $cmt = ctx_locate_comment($first_line, $end_line);
628
Andy Whitcroft00df3442007-06-08 13:47:06 -0700629 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700630 ##print "CMMT: $cmt\n";
631
632 return ($cmt ne '');
633}
634
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700635sub cat_vet {
636 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700637 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700638
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700639 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700640 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
641 $res .= $1;
642 if ($2 ne '') {
643 $coded = sprintf("^%c", unpack('C', $2) + 64);
644 $res .= $coded;
645 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700646 }
647 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700648
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700649 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700650}
651
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800652my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800653my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800654my @av_paren_type;
655
656sub annotate_reset {
657 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800658 $av_pending = '_';
659 @av_paren_type = ('E');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800660}
661
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700662sub annotate_values {
663 my ($stream, $type) = @_;
664
665 my $res;
666 my $cur = $stream;
667
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800668 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700669
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700670 while (length($cur)) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800671 print " <" . join('', @av_paren_type) .
672 "> <$type> " if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700673 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800674 print "WS($1)\n" if ($dbg_values > 1);
675 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800676 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800677 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700678 }
679
Andy Whitcroft8905a672007-11-28 16:21:06 -0800680 } elsif ($cur =~ /^($Type)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800681 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700682 $type = 'T';
683
684 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800685 print "DEFINE($1)\n" if ($dbg_values > 1);
686 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800687 $av_pending = 'N';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700688
Andy Whitcroftcf655042008-03-04 14:28:20 -0800689 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
690 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800691 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -0800692
693 push(@av_paren_type, $type);
694 push(@av_paren_type, $type);
695 $type = 'N';
696
697 } elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
698 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
699 $av_preprocessor = 1;
700
701 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
702
703 $type = 'N';
704
705 } elsif ($cur =~ /^(#\s*(?:endif))/o) {
706 print "PRE_END($1)\n" if ($dbg_values > 1);
707
708 $av_preprocessor = 1;
709
710 # Assume all arms of the conditional end as this
711 # one does, and continue as if the #endif was not here.
712 pop(@av_paren_type);
713 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700714 $type = 'N';
715
716 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800717 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700718
719 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800720 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700721 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800722 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700723 }
724 $type = 'N';
725
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800726 } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800727 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800728 $av_pending = 'N';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700729 $type = 'N';
730
731 } elsif ($cur =~/^(return|case|else)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800732 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700733 $type = 'N';
734
735 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800736 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800737 push(@av_paren_type, $av_pending);
738 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700739 $type = 'N';
740
741 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -0800742 my $new_type = pop(@av_paren_type);
743 if ($new_type ne '_') {
744 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800745 print "PAREN('$1') -> $type\n"
746 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700747 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800748 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700749 }
750
751 } elsif ($cur =~ /^($Ident)\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800752 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -0800753 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700754
755 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800756 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700757 $type = 'V';
758
759 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800760 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700761 $type = 'N';
762
Andy Whitcroftcf655042008-03-04 14:28:20 -0800763 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800764 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800765 $type = 'E';
766
Andy Whitcroftcf655042008-03-04 14:28:20 -0800767 } elsif ($cur =~ /^(;|\?|:|\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800768 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700769 $type = 'N';
770
771 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800772 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700773 if ($1 ne '++' && $1 ne '--') {
774 $type = 'N';
775 }
776
777 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800778 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700779 }
780 if (defined $1) {
781 $cur = substr($cur, length($1));
782 $res .= $type x length($1);
783 }
784 }
785
786 return $res;
787}
788
Andy Whitcroft8905a672007-11-28 16:21:06 -0800789sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800790 my ($possible, $line) = @_;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800791
792 #print "CHECK<$possible>\n";
793 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
794 $possible ne 'goto' && $possible ne 'return' &&
795 $possible ne 'struct' && $possible ne 'enum' &&
796 $possible ne 'case' && $possible ne 'else' &&
797 $possible ne 'typedef') {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800798 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800799 push(@typeList, $possible);
800 build_types();
801 }
802}
803
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700804my $prefix = '';
805
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700806sub report {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800807 my $line = $prefix . $_[0];
808
809 $line = (split('\n', $line))[0] . "\n" if ($terse);
810
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800811 push(our @report, $line);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700812}
813sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800814 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700815}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700816sub ERROR {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700817 report("ERROR: $_[0]\n");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700818 our $clean = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700819 our $cnt_error++;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700820}
821sub WARN {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700822 report("WARNING: $_[0]\n");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700823 our $clean = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700824 our $cnt_warn++;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700825}
826sub CHK {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700827 if ($check) {
828 report("CHECK: $_[0]\n");
829 our $clean = 0;
830 our $cnt_chk++;
831 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700832}
833
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700834sub process {
835 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700836
837 my $linenr=0;
838 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800839 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700840 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800841 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700842
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700843 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700844 my $indent;
845 my $previndent=0;
846 my $stashindent=0;
847
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700848 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700849 my $signoff = 0;
850 my $is_patch = 0;
851
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800852 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700853 our $cnt_lines = 0;
854 our $cnt_error = 0;
855 our $cnt_warn = 0;
856 our $cnt_chk = 0;
857
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700858 # Trace the real file/line as we go.
859 my $realfile = '';
860 my $realline = 0;
861 my $realcnt = 0;
862 my $here = '';
863 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800864 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700865 my $first_line = 0;
866
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800867 my $prev_values = 'E';
868
869 # suppression flags
870 my $suppress_ifbraces = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700871
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800872 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700873 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800874 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700875 my @setup_docs = ();
876 my $setup_docs = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800877 my $line;
878 foreach my $rawline (@rawlines) {
879 # Standardise the strings and chars within the input to
880 # simplify matching.
881 $line = sanitise_line($rawline);
882 push(@lines, $line);
883
884 ##print "==>$rawline\n";
885 ##print "-->$line\n";
886
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700887 if ($line=~/^\+\+\+\s+(\S+)/) {
888 $setup_docs = 0;
889 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
890 $setup_docs = 1;
891 }
892 next;
893 }
894
895 if ($setup_docs && $line =~ /^\+/) {
896 push(@setup_docs, $line);
897 }
898 }
899
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700900 $prefix = '';
901
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700902 foreach my $line (@lines) {
903 $linenr++;
904
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800905 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700906
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700907#extract the filename as it passes
908 if ($line=~/^\+\+\+\s+(\S+)/) {
909 $realfile=$1;
Andy Whitcroft00df3442007-06-08 13:47:06 -0700910 $realfile =~ s@^[^/]*/@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700911 $in_comment = 0;
912 next;
913 }
914#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700915 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700916 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700917 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700918 $in_comment = 0;
919 $realline=$1-1;
920 if (defined $2) {
921 $realcnt=$3+1;
922 } else {
923 $realcnt=1+1;
924 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800925 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800926 $prev_values = 'E';
927
928 $suppress_ifbraces = $linenr - 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700929 next;
930 }
931
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700932# track the line number as we move through the hunk, note that
933# new versions of GNU diff omit the leading space on completely
934# blank context lines so we need to count that too.
935 if ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700936 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700937 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700938
Andy Whitcroft8905a672007-11-28 16:21:06 -0800939 # Guestimate if this is a continuing comment. Run
940 # the context looking for a comment "edge". If this
941 # edge is a close comment then we must be in a comment
942 # at context start.
943 if ($linenr == $first_line) {
944 my $edge;
945 for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800946 ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800947 last if (defined $edge);
948 }
949 if (defined $edge && $edge eq '*/') {
950 $in_comment = 1;
951 }
952 }
953
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700954 # Guestimate if this is a continuing comment. If this
955 # is the start of a diff block and this line starts
956 # ' *' then it is very likely a comment.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800957 if ($linenr == $first_line and $rawline =~ m@^.\s* \*(?:\s|$)@) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700958 $in_comment = 1;
959 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800960
961 # Find the last comment edge on _this_ line.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800962 $comment_edge = 0;
963 while (($rawline =~ m@(/\*|\*/)@g)) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800964 if ($1 eq '/*') {
965 $in_comment = 1;
966 } else {
967 $in_comment = 0;
968 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800969 $comment_edge = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700970 }
971
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700972 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800973 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700974
975 # Track the previous line.
976 ($prevline, $stashline) = ($stashline, $line);
977 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800978 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
979
980 #warn "ic<$in_comment> ce<$comment_edge> line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700981
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700982 } elsif ($realcnt == 1) {
983 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700984 }
985
986#make up the handle for any error we report on this line
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700987 $here = "#$linenr: " if (!$file);
988 $here = "#$realline: " if ($file);
Randy Dunlap389834b2007-06-08 13:47:03 -0700989 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700990
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800991 my $hereline = "$here\n$rawline\n";
992 my $herecurr = "$here\n$rawline\n";
993 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700994
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700995 $prefix = "$filename:$realline: " if ($emacs && $file);
996 $prefix = "$filename:$linenr: " if ($emacs && !$file);
997 $cnt_lines++ if ($realcnt != 0);
998
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700999#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001000 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001001 # This is a signoff, if ugly, so do not double report.
1002 $signoff++;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001003 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001004 WARN("Signed-off-by: is the preferred form\n" .
1005 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001006 }
1007 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001008 WARN("need space after Signed-off-by:\n" .
1009 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001010 }
1011 }
1012
Andy Whitcroft00df3442007-06-08 13:47:06 -07001013# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08001014 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001015 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001016 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001017 }
1018
1019# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1020 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001021 !($rawline =~ m/^(
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001022 [\x09\x0A\x0D\x20-\x7E] # ASCII
1023 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
1024 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
1025 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
1026 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
1027 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
1028 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
1029 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
1030 )*$/x )) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001031 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001032 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001033
Andy Whitcroft00df3442007-06-08 13:47:06 -07001034#ignore lines being removed
1035 if ($line=~/^-/) {next;}
1036
1037# check we are in a valid source file if not then ignore this hunk
1038 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001039
1040#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001041 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001042 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001043 ERROR("DOS line endings\n" . $herevet);
1044
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001045 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1046 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001047 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001048 }
1049#80 column limit
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001050 if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001051 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001052 }
1053
Andy Whitcroft8905a672007-11-28 16:21:06 -08001054# check for adding lines without a newline.
1055 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1056 WARN("adding a line without newline at end of file\n" . $herecurr);
1057 }
1058
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001059# check we are in a valid source file *.[hc] if not then ignore this hunk
1060 next if ($realfile !~ /\.[hc]$/);
1061
1062# at the beginning of a line any tabs must come first and anything
1063# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001064 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1065 $rawline =~ /^\+\s* \s*/) {
1066 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001067 ERROR("use tabs not spaces\n" . $herevet);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001068 }
1069
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001070# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08001071 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001072 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1073 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001074
1075# The rest of our checks refer specifically to C style
1076# only apply those _outside_ comments. Only skip
1077# lines in the middle of comments.
1078 next if (!$comment_edge && $in_comment);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001079
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001080# Check for potential 'bare' types
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001081 if ($realcnt) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001082 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1083 $s =~ s/\n./ /g;
1084 $s =~ s/{.*$//;
1085
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001086 # Ignore goto labels.
Andy Whitcroftcf655042008-03-04 14:28:20 -08001087 if ($s =~ /$Ident:\*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001088
1089 # Ignore functions being called
Andy Whitcroftcf655042008-03-04 14:28:20 -08001090 } elsif ($s =~ /^.\s*$Ident\s*\(/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001091
Andy Whitcroft8905a672007-11-28 16:21:06 -08001092 # definitions in global scope can only start with types
Andy Whitcroftcf655042008-03-04 14:28:20 -08001093 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
1094 possible($1, $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001095
1096 # declarations always start with types
Andy Whitcroftcf655042008-03-04 14:28:20 -08001097 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) {
1098 possible($1, $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001099 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001100
1101 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroftcf655042008-03-04 14:28:20 -08001102 while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
1103 possible($1, $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001104 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001105
1106 # Check for any sort of function declaration.
1107 # int foo(something bar, other baz);
1108 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001109 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 -08001110 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001111
Andy Whitcroftcf655042008-03-04 14:28:20 -08001112 my $ctx = $s;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001113 substr($ctx, 0, $name_len + 1) = '';
1114 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001115
Andy Whitcroft8905a672007-11-28 16:21:06 -08001116 for my $arg (split(/\s*,\s*/, $ctx)) {
1117 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
1118
Andy Whitcroftcf655042008-03-04 14:28:20 -08001119 possible($1, $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001120 }
1121 }
1122 }
1123
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001124 }
1125
Andy Whitcroft653d4872007-06-23 17:16:34 -07001126#
1127# Checks which may be anchored in the context.
1128#
1129
1130# Check for switch () and associated case and default
1131# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07001132 if ($line=~/\bswitch\s*\(.*\)/) {
1133 my $err = '';
1134 my $sep = '';
1135 my @ctx = ctx_block_outer($linenr, $realcnt);
1136 shift(@ctx);
1137 for my $ctx (@ctx) {
1138 my ($clen, $cindent) = line_stats($ctx);
1139 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1140 $indent != $cindent) {
1141 $err .= "$sep$ctx\n";
1142 $sep = '';
1143 } else {
1144 $sep = "[...]\n";
1145 }
1146 }
1147 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001148 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001149 }
1150 }
1151
1152# if/while/etc brace do not go on next line, unless defining a do while loop,
1153# or if that brace on the next line is for something else
1154 if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001155 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001156 my $ctx_ln = $linenr + $#ctx + 1;
1157 my $ctx_cnt = $realcnt - $#ctx - 1;
1158 my $ctx = join("\n", @ctx);
1159
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001160 # Skip over any removed lines in the context following statement.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001161 while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
1162 $ctx_ln++;
1163 $ctx_cnt--;
1164 }
1165 ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
1166
1167 if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001168 ERROR("That open brace { should be on the previous line\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001169 "$here\n$ctx\n$lines[$ctx_ln - 1]");
Andy Whitcroft00df3442007-06-08 13:47:06 -07001170 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001171 if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) {
1172 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1173 if ($nindent > $indent) {
1174 WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" .
1175 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1176 }
1177 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001178 }
1179
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001180 # Track the 'values' across context and added lines.
1181 my $opline = $line; $opline =~ s/^./ /;
1182 my $curr_values = annotate_values($opline . "\n", $prev_values);
1183 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001184 if ($dbg_values) {
1185 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001186 print "$linenr > .$outline\n";
1187 print "$linenr > $curr_values\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001188 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001189 $prev_values = substr($curr_values, -1);
1190
Andy Whitcroft00df3442007-06-08 13:47:06 -07001191#ignore lines not being added
1192 if ($line=~/^[^\+]/) {next;}
1193
Andy Whitcroft653d4872007-06-23 17:16:34 -07001194# TEST: allow direct testing of the type matcher.
1195 if ($tst_type && $line =~ /^.$Declare$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001196 ERROR("TEST: is type $Declare\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001197 next;
1198 }
1199
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001200# check for initialisation to aggregates open brace on the next line
1201 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1202 $line =~ /^.\s*{/) {
1203 ERROR("That open brace { should be on the previous line\n" . $hereprev);
1204 }
1205
Andy Whitcroft653d4872007-06-23 17:16:34 -07001206#
1207# Checks which are anchored on the added line.
1208#
1209
1210# check for malformed paths in #include statements (uses RAW line)
1211 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1212 my $path = $1;
1213 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001214 ERROR("malformed #include filename\n" .
1215 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001216 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001217 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07001218
1219# no C99 // comments
1220 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001221 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001222 }
1223 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001224 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001225 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001226
1227#EXPORT_SYMBOL should immediately follow its function closing }.
Andy Whitcroft653d4872007-06-23 17:16:34 -07001228 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1229 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1230 my $name = $1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001231 if (($prevline !~ /^}/) &&
1232 ($prevline !~ /^\+}/) &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001233 ($prevline !~ /^ }/) &&
Andy Whitcroftcf655042008-03-04 14:28:20 -08001234 ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1235 ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1236 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001237 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001238 }
1239 }
1240
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001241# check for external initialisers.
1242 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
1243 ERROR("do not initialise externals to 0 or NULL\n" .
1244 $herecurr);
1245 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001246# check for static initialisers.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001247 if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001248 ERROR("do not initialise statics to 0 or NULL\n" .
1249 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001250 }
1251
Andy Whitcroft653d4872007-06-23 17:16:34 -07001252# check for new typedefs, only function parameters and sparse annotations
1253# make sense.
1254 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001255 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001256 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001257 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001258 }
1259
1260# * goes on variable not on type
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001261 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001262 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1263 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001264
1265 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001266 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1267 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001268
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001269 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001270 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1271 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001272
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001273 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001274 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1275 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001276 }
1277
1278# # no BUG() or BUG_ON()
1279# if ($line =~ /\b(BUG|BUG_ON)\b/) {
1280# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1281# print "$herecurr";
1282# $clean = 0;
1283# }
1284
Andy Whitcroft8905a672007-11-28 16:21:06 -08001285 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001286 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 -08001287 }
1288
Andy Whitcroft00df3442007-06-08 13:47:06 -07001289# printk should use KERN_* levels. Note that follow on printk's on the
1290# same line do not need a level, so we use the current block context
1291# to try and find and validate the current printk. In summary the current
1292# printk includes all preceeding printk's which have no newline on the end.
1293# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001294 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001295 my $ok = 0;
1296 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1297 #print "CHECK<$lines[$ln - 1]\n";
1298 # we have a preceeding printk if it ends
1299 # with "\n" ignore it, else it is to blame
1300 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1301 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1302 $ok = 1;
1303 }
1304 last;
1305 }
1306 }
1307 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001308 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001309 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001310 }
1311
Andy Whitcroft653d4872007-06-23 17:16:34 -07001312# function brace can't be on same line, except for #defines of do while,
1313# or if closed on same line
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001314 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001315 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001316 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001317 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001318
Andy Whitcroft8905a672007-11-28 16:21:06 -08001319# open braces for enum, union and struct go on the same line.
1320 if ($line =~ /^.\s*{/ &&
1321 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1322 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1323 }
1324
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001325# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001326 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001327 my $name = $1;
1328 my $ctx = substr($line, 0, $-[1]);
1329
1330 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001331 if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case|__asm__)$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001332
1333 # cpp #define statements have non-optional spaces, ie
1334 # if there is a space between the name and the open
1335 # parenthesis it is simply not a parameter group.
1336 } elsif ($ctx =~ /^.\#\s*define\s*$/) {
1337
1338 # If this whole things ends with a type its most
1339 # likely a typedef for a function.
1340 } elsif ("$ctx$name" =~ /$Type$/) {
1341
1342 } else {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001343 WARN("no space between function name and open parenthesis '('\n" . $herecurr);
1344 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001345 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001346# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001347 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001348 my $ops = qr{
1349 <<=|>>=|<=|>=|==|!=|
1350 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1351 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001352 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001353 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001354 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001355 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001356
1357 my $blank = copy_spacing($opline);
1358
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001359 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001360 $off += length($elements[$n]);
1361
1362 my $a = '';
1363 $a = 'V' if ($elements[$n] ne '');
1364 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001365 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001366 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1367 $a = 'O' if ($elements[$n] eq '');
1368 $a = 'E' if ($elements[$n] eq '' && $n == 0);
1369
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001370 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001371
1372 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001373 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001374 $c = 'V' if ($elements[$n + 2] ne '');
1375 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001376 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001377 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1378 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001379 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001380 } else {
1381 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001382 }
1383
Andy Whitcroft00df3442007-06-08 13:47:06 -07001384 # Pick up the preceeding and succeeding characters.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001385 my $ca = substr($opline, 0, $off);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001386 my $cc = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -07001387 if (length($opline) >= ($off + length($elements[$n + 1]))) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001388 $cc = substr($opline, $off + length($elements[$n + 1]));
Andy Whitcroft00df3442007-06-08 13:47:06 -07001389 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001390 my $cb = "$ca$;$cc";
Andy Whitcroft00df3442007-06-08 13:47:06 -07001391
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001392 my $ctx = "${a}x${c}";
1393
1394 my $at = "(ctx:$ctx)";
1395
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001396 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001397 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001398
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001399 # Classify operators into binary, unary, or
1400 # definitions (* only) where they have more
1401 # than one mode.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001402 my $op_type = substr($curr_values, $off + 1, 1);
1403 my $op_left = substr($curr_values, $off, 1);
1404 my $is_unary;
1405 if ($op_type eq 'T') {
1406 $is_unary = 2;
1407 } elsif ($op_left eq 'V') {
1408 $is_unary = 0;
1409 } else {
1410 $is_unary = 1;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001411 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001412 #if ($op eq '-' || $op eq '&' || $op eq '*') {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001413 # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001414 #}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001415
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001416 # Ignore operators passed as parameters.
1417 if ($op_type ne 'V' &&
1418 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1419
Andy Whitcroftcf655042008-03-04 14:28:20 -08001420# # Ignore comments
1421# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001422
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001423 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001424 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001425 if ($ctx !~ /.x[WEBC]/ &&
1426 $cc !~ /^\\/ && $cc !~ /^;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001427 ERROR("need space after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001428 }
1429
1430 # // is a comment
1431 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001432
1433 # -> should have no spaces
1434 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001435 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001436 ERROR("no spaces around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001437 }
1438
1439 # , must have a space on the right.
1440 } elsif ($op eq ',') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001441 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001442 ERROR("need space after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001443 }
1444
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001445 # '*' as part of a type definition -- reported already.
1446 } elsif ($op eq '*' && $is_unary == 2) {
1447 #warn "'*' is part of type\n";
1448
1449 # unary operators should have a space before and
1450 # none after. May be left adjacent to another
1451 # unary operator, or a cast
1452 } elsif ($op eq '!' || $op eq '~' ||
1453 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001454 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001455 ERROR("need space before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001456 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001457 if ($ctx =~ /.xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001458 ERROR("no space after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001459 }
1460
1461 # unary ++ and unary -- are allowed no space on one side.
1462 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001463 if ($ctx !~ /[WOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001464 ERROR("need space one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001465 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001466 if ($ctx =~ /WxB/ || ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001467 ERROR("no space before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001468 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001469
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001470 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001471 } elsif ($op eq '<<' or $op eq '>>' or
1472 $op eq '&' or $op eq '^' or $op eq '|' or
1473 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001474 $op eq '*' or $op eq '/' or
1475 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001476 {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001477 if ($ctx !~ /VxV|WxW|VxE|WxE|VxO|Cx.|.xC/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001478 ERROR("need consistent spacing around '$op' $at\n" .
1479 $hereptr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001480 }
1481
1482 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08001483 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001484 # Ignore email addresses <foo@bar>
1485 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1486 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1487 ERROR("need spaces around that '$op' $at\n" . $hereptr);
1488 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001489 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001490 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001491 }
1492 }
1493
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001494# check for multiple assignments
1495 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001496 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001497 }
1498
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001499## # check for multiple declarations, allowing for a function declaration
1500## # continuation.
1501## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1502## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1503##
1504## # Remove any bracketed sections to ensure we do not
1505## # falsly report the parameters of functions.
1506## my $ln = $line;
1507## while ($ln =~ s/\([^\(\)]*\)//g) {
1508## }
1509## if ($ln =~ /,/) {
1510## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1511## }
1512## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001513
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001514#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001515 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1516 $line =~ /do{/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001517 ERROR("need a space before the open brace '{'\n" . $herecurr);
1518 }
1519
1520# closing brace should have a space following it when it has anything
1521# on the line
1522 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1523 ERROR("need a space after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001524 }
1525
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001526# check spacing on square brackets
1527 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1528 ERROR("no space after that open square bracket '['\n" . $herecurr);
1529 }
1530 if ($line =~ /\s\]/) {
1531 ERROR("no space before that close square bracket ']'\n" . $herecurr);
1532 }
1533
1534# check spacing on paretheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001535 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1536 $line !~ /for\s*\(\s+;/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001537 ERROR("no space after that open parenthesis '('\n" . $herecurr);
1538 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001539 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001540 $line !~ /for\s*\(.*;\s+\)/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001541 ERROR("no space before that close parenthesis ')'\n" . $herecurr);
1542 }
1543
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001544#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001545 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001546 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001547 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001548 }
1549
1550# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001551 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001552 ERROR("need a space before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001553 }
1554
1555# Check for illegal assignment in if conditional.
Andy Whitcroft8905a672007-11-28 16:21:06 -08001556 if ($line =~ /\bif\s*\(/) {
1557 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1558
1559 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001560 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001561 }
1562
1563 # Find out what is on the end of the line after the
1564 # conditional.
1565 substr($s, 0, length($c)) = '';
1566 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001567 $s =~ s/$;//g; # Remove any comments
1568 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001569 ERROR("trailing statements should be on next line\n" . $herecurr);
1570 }
1571 }
1572
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001573# Check for bitwise tests written as boolean
1574 if ($line =~ /
1575 (?:
1576 (?:\[|\(|\&\&|\|\|)
1577 \s*0[xX][0-9]+\s*
1578 (?:\&\&|\|\|)
1579 |
1580 (?:\&\&|\|\|)
1581 \s*0[xX][0-9]+\s*
1582 (?:\&\&|\|\||\)|\])
1583 )/x)
1584 {
1585 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
1586 }
1587
Andy Whitcroft8905a672007-11-28 16:21:06 -08001588# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001589 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
1590 my $s = $1;
1591 $s =~ s/$;//g; # Remove any comments
1592 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
1593 ERROR("trailing statements should be on next line\n" . $herecurr);
1594 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001595 }
1596
1597 # Check for }<nl>else {, these must be at the same
1598 # indent level to be relevant to each other.
1599 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
1600 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001601 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001602 }
1603
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001604 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1605 $previndent == $indent) {
1606 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1607
1608 # Find out what is on the end of the line after the
1609 # conditional.
1610 substr($s, 0, length($c)) = '';
1611 $s =~ s/\n.*//g;
1612
1613 if ($s =~ /^\s*;/) {
1614 ERROR("while should follow close brace '}'\n" . $hereprev);
1615 }
1616 }
1617
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001618#studly caps, commented out until figure out how to distinguish between use of existing and adding new
1619# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
1620# print "No studly caps, use _\n";
1621# print "$herecurr";
1622# $clean = 0;
1623# }
1624
1625#no spaces allowed after \ in define
1626 if ($line=~/\#define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001627 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001628 }
1629
Andy Whitcroft653d4872007-06-23 17:16:34 -07001630#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1631 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001632 my $checkfile = "$root/include/linux/$1.h";
1633 if (-f $checkfile && $1 ne 'irq.h') {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001634 CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1635 $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001636 }
1637 }
1638
Andy Whitcroft653d4872007-06-23 17:16:34 -07001639# multi-statement macros should be enclosed in a do while loop, grab the
1640# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08001641# in a known good container
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001642 if ($prevline =~ /\#define.*\\/ &&
1643 $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1644 $line !~ /(?:do\s+{|\(\{|\{)/ &&
1645 $line !~ /^.\s*$Declare\s/) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001646 # Grab the first statement, if that is the entire macro
1647 # its ok. This may start either on the #define line
1648 # or the one below.
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001649 my $ln = $linenr;
1650 my $cnt = $realcnt;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001651 my $off = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001652
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001653 # If the macro starts on the define line start
1654 # grabbing the statement after the identifier
1655 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1656 ##print "1<$1> 2<$2>\n";
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001657 if (defined $2 && $2 ne '') {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001658 $off = length($1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001659 $ln--;
1660 $cnt++;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001661 while ($lines[$ln - 1] =~ /^-/) {
1662 $ln--;
1663 $cnt++;
1664 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001665 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001666 my @ctx = ctx_statement($ln, $cnt, $off);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001667 my $ctx_ln = $ln + $#ctx + 1;
1668 my $ctx = join("\n", @ctx);
1669
1670 # Pull in any empty extension lines.
1671 while ($ctx =~ /\\$/ &&
1672 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1673 $ctx .= $lines[$ctx_ln - 1];
1674 $ctx_ln++;
1675 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001676
1677 if ($ctx =~ /\\$/) {
1678 if ($ctx =~ /;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001679 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 -07001680 } else {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001681 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001682 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001683 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001684 }
1685
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001686# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001687 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
1688 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08001689 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001690 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001691 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1692 if ($#chunks > 0 && $level == 0) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001693 my $allowed = 0;
1694 my $seen = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001695 my $herectx = $here . "\n";;
1696 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001697 for my $chunk (@chunks) {
1698 my ($cond, $block) = @{$chunk};
1699
Andy Whitcroftcf655042008-03-04 14:28:20 -08001700 $herectx .= "$rawlines[$ln]\n[...]\n";
1701 $ln += statement_rawlines($block) - 1;
1702
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001703 substr($block, 0, length($cond)) = '';
1704
1705 $seen++ if ($block =~ /^\s*{/);
1706
Andy Whitcroftcf655042008-03-04 14:28:20 -08001707 #print "cond<$cond> block<$block> allowed<$allowed>\n";
1708 if (statement_lines($cond) > 1) {
1709 #print "APW: ALLOWED: cond<$cond>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001710 $allowed = 1;
1711 }
1712 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001713 #print "APW: ALLOWED: block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001714 $allowed = 1;
1715 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001716 if (statement_block_size($block) > 1) {
1717 #print "APW: ALLOWED: lines block<$block>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001718 $allowed = 1;
1719 }
1720 }
1721 if ($seen && !$allowed) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001722 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001723 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001724 # Either way we have looked over this whole
1725 # statement and said what needs to be said.
1726 $suppress_ifbraces = $endln;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001727 }
1728 }
1729 if ($linenr > $suppress_ifbraces &&
1730 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001731 my ($level, $endln, @chunks) =
1732 ctx_statement_full($linenr, $realcnt, $-[0]);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001733
Andy Whitcroftcf655042008-03-04 14:28:20 -08001734 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001735
Andy Whitcroftcf655042008-03-04 14:28:20 -08001736 # Check the pre-context.
1737 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
1738 #print "APW: ALLOWED: pre<$1>\n";
1739 $allowed = 1;
1740 }
1741 # Check the condition.
1742 my ($cond, $block) = @{$chunks[0]};
1743 if (defined $cond) {
1744 substr($block, 0, length($cond)) = '';
1745 }
1746 if (statement_lines($cond) > 1) {
1747 #print "APW: ALLOWED: cond<$cond>\n";
1748 $allowed = 1;
1749 }
1750 if ($block =~/\b(?:if|for|while)\b/) {
1751 #print "APW: ALLOWED: block<$block>\n";
1752 $allowed = 1;
1753 }
1754 if (statement_block_size($block) > 1) {
1755 #print "APW: ALLOWED: lines block<$block>\n";
1756 $allowed = 1;
1757 }
1758 # Check the post-context.
1759 if (defined $chunks[1]) {
1760 my ($cond, $block) = @{$chunks[1]};
1761 if (defined $cond) {
1762 substr($block, 0, length($cond)) = '';
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001763 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001764 if ($block =~ /^\s*\{/) {
1765 #print "APW: ALLOWED: chunk-1 block<$block>\n";
1766 $allowed = 1;
1767 }
1768 }
1769 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
1770 my $herectx = $here . "\n";;
1771 my $end = $linenr + statement_rawlines($block) - 1;
1772
1773 for (my $ln = $linenr - 1; $ln < $end; $ln++) {
1774 $herectx .= $rawlines[$ln] . "\n";;
1775 }
1776
1777 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001778 }
1779 }
1780
Andy Whitcroft653d4872007-06-23 17:16:34 -07001781# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001782 for my $inc (@dep_includes) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001783 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001784 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001785 }
1786 }
1787
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001788# don't use deprecated functions
1789 for my $func (@dep_functions) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001790 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001791 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001792 }
1793 }
1794
1795# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001796 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
1797 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001798 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001799 }
1800
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001801# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
1802 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
1803 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
1804 }
1805
Andy Whitcroft00df3442007-06-08 13:47:06 -07001806# warn about #if 0
1807 if ($line =~ /^.#\s*if\s+0\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001808 CHK("if this code is redundant consider removing it\n" .
1809 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001810 }
1811
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001812# check for needless kfree() checks
1813 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1814 my $expr = $1;
1815 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1816 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1817 }
1818 }
1819
Andy Whitcroft00df3442007-06-08 13:47:06 -07001820# warn about #ifdefs in C files
1821# if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1822# print "#ifdef in C files should be avoided\n";
1823# print "$herecurr";
1824# $clean = 0;
1825# }
1826
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001827# warn about spacing in #ifdefs
1828 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1829 ERROR("exactly one space required after that #$1\n" . $herecurr);
1830 }
1831
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001832# check for spinlock_t definitions without a comment.
1833 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
1834 my $which = $1;
1835 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001836 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001837 }
1838 }
1839# check for memory barriers without a comment.
1840 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
1841 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001842 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001843 }
1844 }
1845# check of hardware specific defines
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001846 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001847 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001848 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001849
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001850# check the location of the inline attribute, that it is between
1851# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001852 if ($line =~ /\b$Type\s+$Inline\b/ ||
1853 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001854 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1855 }
1856
Andy Whitcroft8905a672007-11-28 16:21:06 -08001857# Check for __inline__ and __inline, prefer inline
1858 if ($line =~ /\b(__inline__|__inline)\b/) {
1859 WARN("plain inline is preferred over $1\n" . $herecurr);
1860 }
1861
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001862# check for new externs in .c files.
1863 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
1864 WARN("externs should be avoided in .c files\n" . $herecurr);
1865 }
1866
1867# checks for new __setup's
1868 if ($rawline =~ /\b__setup\("([^"]*)"/) {
1869 my $name = $1;
1870
1871 if (!grep(/$name/, @setup_docs)) {
1872 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
1873 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001874 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001875
1876# check for pointless casting of kmalloc return
1877 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
1878 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
1879 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001880
1881# check for gcc specific __FUNCTION__
1882 if ($line =~ /__FUNCTION__/) {
1883 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
1884 }
1885 }
1886
1887 # If we have no input at all, then there is nothing to report on
1888 # so just keep quiet.
1889 if ($#rawlines == -1) {
1890 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001891 }
1892
Andy Whitcroft8905a672007-11-28 16:21:06 -08001893 # In mailback mode only produce a report in the negative, for
1894 # things that appear to be patches.
1895 if ($mailback && ($clean == 1 || !$is_patch)) {
1896 exit(0);
1897 }
1898
1899 # This is not a patch, and we are are in 'no-patch' mode so
1900 # just keep quiet.
1901 if (!$chk_patch && !$is_patch) {
1902 exit(0);
1903 }
1904
1905 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001906 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001907 }
1908 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001909 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001910 }
1911
Andy Whitcroft8905a672007-11-28 16:21:06 -08001912 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001913 if ($summary && !($clean == 1 && $quiet == 1)) {
1914 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001915 print "total: $cnt_error errors, $cnt_warn warnings, " .
1916 (($check)? "$cnt_chk checks, " : "") .
1917 "$cnt_lines lines checked\n";
1918 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001919 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001920
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001921 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001922 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001923 }
1924 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001925 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001926 print "are false positives report them to the maintainer, see\n";
1927 print "CHECKPATCH in MAINTAINERS.\n";
1928 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001929
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001930 return $clean;
1931}