blob: 545471a99eea1ff5e9feb9bfe28445fcf6575be8 [file] [log] [blame]
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001#!/usr/bin/perl -w
2# (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
Andy Whitcroft00df344f2007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft0a920b5b2007-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 Whitcroft00df344f2007-06-08 13:47:06 -070010$P =~ s@.*/@@g;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070011
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080012my $V = '0.13';
Andy Whitcroft0a920b5b2007-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 Whitcroft6c72ffa2007-10-18 03:05:08 -070027my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080028my %debug;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070029GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070031 'tree!' => \$tree,
32 'signoff!' => \$chk_signoff,
33 'patch!' => \$chk_patch,
Andy Whitcroft653d4872007-06-23 17:16:34 -070034 'test-type!' => \$tst_type,
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 Whitcroftc2fdda02008-02-08 04:20:54 -080043 'debug=s' => \%debug,
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070044) or exit;
45
46my $exit = 0;
47
48if ($#ARGV < 0) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -070049 print "usage: $P [options] patchfile\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070050 print "version: $V\n";
51 print "options: -q => quiet\n";
52 print " --no-tree => run without a kernel tree\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -080053 print " --terse => one line per report\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070054 print " --emacs => emacs compile window format\n";
55 print " --file => check a source file\n";
56 print " --strict => enable more subjective tests\n";
57 print " --root => path to the kernel tree root\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070058 exit(1);
59}
60
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080061my $dbg_values = 0;
62my $dbg_possible = 0;
63for my $key (keys %debug) {
64 eval "\${dbg_$key} = '$debug{$key}';"
65}
66
Andy Whitcroft8905a672007-11-28 16:21:06 -080067if ($terse) {
68 $emacs = 1;
69 $quiet++;
70}
71
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070072if ($tree) {
73 if (defined $root) {
74 if (!top_of_kernel_tree($root)) {
75 die "$P: $root: --root does not point at a valid tree\n";
76 }
77 } else {
78 if (top_of_kernel_tree('.')) {
79 $root = '.';
80 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
81 top_of_kernel_tree($1)) {
82 $root = $1;
83 }
84 }
85
86 if (!defined $root) {
87 print "Must be run from the top-level dir. of a kernel tree\n";
88 exit(2);
89 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -070090}
91
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070092my $emitted_corrupt = 0;
93
94our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
95our $Storage = qr{extern|static|asmlinkage};
96our $Sparse = qr{
97 __user|
98 __kernel|
99 __force|
100 __iomem|
101 __must_check|
102 __init_refok|
103 __kprobes|
104 fastcall
105 }x;
106our $Attribute = qr{
107 const|
108 __read_mostly|
109 __kprobes|
110 __(?:mem|cpu|dev|)(?:initdata|init)
111 }x;
112our $Inline = qr{inline|__always_inline|noinline};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700113our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
114our $Lval = qr{$Ident(?:$Member)*};
115
116our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
117our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
118our $Operators = qr{
119 <=|>=|==|!=|
120 =>|->|<<|>>|<|>|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800121 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700122 }x;
123
Andy Whitcroft8905a672007-11-28 16:21:06 -0800124our $NonptrType;
125our $Type;
126our $Declare;
127
128our @typeList = (
129 qr{void},
130 qr{char},
131 qr{short},
132 qr{int},
133 qr{long},
134 qr{unsigned},
135 qr{float},
136 qr{double},
137 qr{bool},
138 qr{long\s+int},
139 qr{long\s+long},
140 qr{long\s+long\s+int},
141 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
142 qr{struct\s+$Ident},
143 qr{union\s+$Ident},
144 qr{enum\s+$Ident},
145 qr{${Ident}_t},
146 qr{${Ident}_handler},
147 qr{${Ident}_handler_fn},
148);
149
150sub build_types {
151 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
152 $NonptrType = qr{
153 \b
154 (?:const\s+)?
155 (?:unsigned\s+)?
156 $all
157 (?:\s+$Sparse|\s+const)*
158 \b
159 }x;
160 $Type = qr{
161 \b$NonptrType\b
162 (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800163 (?:\s+$Inline|\s+$Sparse|\s+$Attribute)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800164 }x;
165 $Declare = qr{(?:$Storage\s+)?$Type};
166}
167build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700168
169$chk_signoff = 0 if ($file);
170
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700171my @dep_includes = ();
172my @dep_functions = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700173my $removal = "Documentation/feature-removal-schedule.txt";
174if ($tree && -f "$root/$removal") {
175 open(REMOVE, "<$root/$removal") ||
176 die "$P: $removal: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700177 while (<REMOVE>) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700178 if (/^Check:\s+(.*\S)/) {
179 for my $entry (split(/[, ]+/, $1)) {
180 if ($entry =~ m@include/(.*)@) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700181 push(@dep_includes, $1);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700182
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700183 } elsif ($entry !~ m@/@) {
184 push(@dep_functions, $entry);
185 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700186 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700187 }
188 }
189}
190
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700191my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800192my @lines = ();
193my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700194for my $filename (@ARGV) {
195 if ($file) {
196 open(FILE, "diff -u /dev/null $filename|") ||
197 die "$P: $filename: diff failed - $!\n";
198 } else {
199 open(FILE, "<$filename") ||
200 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700201 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800202 if ($filename eq '-') {
203 $vname = 'Your patch';
204 } else {
205 $vname = $filename;
206 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700207 while (<FILE>) {
208 chomp;
209 push(@rawlines, $_);
210 }
211 close(FILE);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800212 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700213 $exit = 1;
214 }
215 @rawlines = ();
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700216}
217
218exit($exit);
219
220sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700221 my ($root) = @_;
222
223 my @tree_check = (
224 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
225 "README", "Documentation", "arch", "include", "drivers",
226 "fs", "init", "ipc", "kernel", "lib", "scripts",
227 );
228
229 foreach my $check (@tree_check) {
230 if (! -e $root . '/' . $check) {
231 return 0;
232 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700233 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700234 return 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700235}
236
237sub expand_tabs {
238 my ($str) = @_;
239
240 my $res = '';
241 my $n = 0;
242 for my $c (split(//, $str)) {
243 if ($c eq "\t") {
244 $res .= ' ';
245 $n++;
246 for (; ($n % 8) != 0; $n++) {
247 $res .= ' ';
248 }
249 next;
250 }
251 $res .= $c;
252 $n++;
253 }
254
255 return $res;
256}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700257sub copy_spacing {
258 my ($str) = @_;
259
260 my $res = '';
261 for my $c (split(//, $str)) {
262 if ($c eq "\t") {
263 $res .= $c;
264 } else {
265 $res .= ' ';
266 }
267 }
268
269 return $res;
270}
Andy Whitcroft0a920b5b2007-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 Whitcroft00df344f2007-06-08 13:47:06 -0700285sub sanitise_line {
286 my ($line) = @_;
287
288 my $res = '';
289 my $l = '';
290
291 my $quote = '';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800292 my $qlen = 0;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700293
294 foreach my $c (split(//, $line)) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800295 # The second backslash of a pair is not a "quote".
296 if ($l eq "\\" && $c eq "\\") {
297 $c = 'X';
298 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700299 if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
300 if ($quote eq '') {
301 $quote = $c;
302 $res .= $c;
303 $l = $c;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800304 $qlen = 0;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700305 next;
306 } elsif ($quote eq $c) {
307 $quote = '';
308 }
309 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800310 if ($quote eq "'" && $qlen > 1) {
311 $quote = '';
312 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700313 if ($quote && $c ne "\t") {
314 $res .= "X";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800315 $qlen++;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700316 } else {
317 $res .= $c;
318 }
319
320 $l = $c;
321 }
322
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800323 # Clear out the comments.
324 while ($res =~ m@(/\*.*?\*/)@) {
325 substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]);
326 }
327 if ($res =~ m@(/\*.*)@) {
328 substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]);
329 }
330 if ($res =~ m@^.(.*\*/)@) {
331 substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]);
332 }
333
334 # The pathname on a #include may be surrounded by '<' and '>'.
335 if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
336 my $clean = 'X' x length($1);
337 $res =~ s@\<.*\>@<$clean>@;
338
339 # The whole of a #error is a string.
340 } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
341 my $clean = 'X' x length($1);
342 $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@;
343 }
344
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700345 return $res;
346}
347
Andy Whitcroft8905a672007-11-28 16:21:06 -0800348sub ctx_statement_block {
349 my ($linenr, $remain, $off) = @_;
350 my $line = $linenr - 1;
351 my $blk = '';
352 my $soff = $off;
353 my $coff = $off - 1;
354
355 my $type = '';
356 my $level = 0;
357 my $c;
358 my $len = 0;
359 while (1) {
360 #warn "CSB: blk<$blk>\n";
361 # If we are about to drop off the end, pull in more
362 # context.
363 if ($off >= $len) {
364 for (; $remain > 0; $line++) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800365 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800366 $remain--;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800367 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -0800368 $len = length($blk);
369 $line++;
370 last;
371 }
372 # Bail if there is no further context.
373 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
374 if ($off == $len) {
375 last;
376 }
377 }
378 $c = substr($blk, $off, 1);
379
380 #warn "CSB: c<$c> type<$type> level<$level>\n";
381 # Statement ends at the ';' or a close '}' at the
382 # outermost level.
383 if ($level == 0 && $c eq ';') {
384 last;
385 }
386
387 if (($type eq '' || $type eq '(') && $c eq '(') {
388 $level++;
389 $type = '(';
390 }
391 if ($type eq '(' && $c eq ')') {
392 $level--;
393 $type = ($level != 0)? '(' : '';
394
395 if ($level == 0 && $coff < $soff) {
396 $coff = $off;
397 }
398 }
399 if (($type eq '' || $type eq '{') && $c eq '{') {
400 $level++;
401 $type = '{';
402 }
403 if ($type eq '{' && $c eq '}') {
404 $level--;
405 $type = ($level != 0)? '{' : '';
406
407 if ($level == 0) {
408 last;
409 }
410 }
411 $off++;
412 }
413
414 my $statement = substr($blk, $soff, $off - $soff + 1);
415 my $condition = substr($blk, $soff, $coff - $soff + 1);
416
417 #warn "STATEMENT<$statement>\n";
418 #warn "CONDITION<$condition>\n";
419
420 return ($statement, $condition);
421}
422
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700423sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700424 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700425 my $line;
426 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700427 my $blk = '';
428 my @o;
429 my @c;
430 my @res = ();
431
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700432 my $level = 0;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700433 for ($line = $start; $remain > 0; $line++) {
434 next if ($rawlines[$line] =~ /^-/);
435 $remain--;
436
437 $blk .= $rawlines[$line];
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700438 foreach my $c (split(//, $rawlines[$line])) {
439 ##print "C<$c>L<$level><$open$close>O<$off>\n";
440 if ($off > 0) {
441 $off--;
442 next;
443 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700444
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700445 if ($c eq $close && $level > 0) {
446 $level--;
447 last if ($level == 0);
448 } elsif ($c eq $open) {
449 $level++;
450 }
451 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700452
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700453 if (!$outer || $level <= 1) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700454 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700455 }
456
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700457 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700458 }
459
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700460 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700461}
462sub ctx_block_outer {
463 my ($linenr, $remain) = @_;
464
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700465 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
466 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700467}
468sub ctx_block {
469 my ($linenr, $remain) = @_;
470
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700471 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
472 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -0700473}
474sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700475 my ($linenr, $remain, $off) = @_;
476
477 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
478 return @r;
479}
480sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -0700481 my ($linenr, $remain) = @_;
482
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700483 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700484}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700485sub ctx_statement_level {
486 my ($linenr, $remain, $off) = @_;
487
488 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
489}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700490
491sub ctx_locate_comment {
492 my ($first_line, $end_line) = @_;
493
494 # Catch a comment on the end of the line itself.
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700495 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700496 return $current_comment if (defined $current_comment);
497
498 # Look through the context and try and figure out if there is a
499 # comment.
500 my $in_comment = 0;
501 $current_comment = '';
502 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700503 my $line = $rawlines[$linenr - 1];
504 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700505 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
506 $in_comment = 1;
507 }
508 if ($line =~ m@/\*@) {
509 $in_comment = 1;
510 }
511 if (!$in_comment && $current_comment ne '') {
512 $current_comment = '';
513 }
514 $current_comment .= $line . "\n" if ($in_comment);
515 if ($line =~ m@\*/@) {
516 $in_comment = 0;
517 }
518 }
519
520 chomp($current_comment);
521 return($current_comment);
522}
523sub ctx_has_comment {
524 my ($first_line, $end_line) = @_;
525 my $cmt = ctx_locate_comment($first_line, $end_line);
526
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700527 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700528 ##print "CMMT: $cmt\n";
529
530 return ($cmt ne '');
531}
532
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700533sub cat_vet {
534 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700535 my ($res, $coded);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700536
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700537 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700538 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
539 $res .= $1;
540 if ($2 ne '') {
541 $coded = sprintf("^%c", unpack('C', $2) + 64);
542 $res .= $coded;
543 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700544 }
545 $res =~ s/$/\$/;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700546
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700547 return $res;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700548}
549
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800550my $av_preprocessor = 0;
551my $av_paren = 0;
552my @av_paren_type;
553
554sub annotate_reset {
555 $av_preprocessor = 0;
556 $av_paren = 0;
557 @av_paren_type = ();
558}
559
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700560sub annotate_values {
561 my ($stream, $type) = @_;
562
563 my $res;
564 my $cur = $stream;
565
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800566 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700567
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700568 while (length($cur)) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800569 print " <$type> " if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700570 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800571 print "WS($1)\n" if ($dbg_values > 1);
572 if ($1 =~ /\n/ && $av_preprocessor) {
573 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700574 $type = 'N';
575 }
576
Andy Whitcroft8905a672007-11-28 16:21:06 -0800577 } elsif ($cur =~ /^($Type)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800578 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700579 $type = 'T';
580
581 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800582 print "DEFINE($1)\n" if ($dbg_values > 1);
583 $av_preprocessor = 1;
584 $av_paren_type[$av_paren] = 'N';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700585
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800586 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|elif|endif))/o) {
587 print "PRE($1)\n" if ($dbg_values > 1);
588 $av_preprocessor = 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700589 $type = 'N';
590
591 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800592 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700593
594 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800595 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700596 if (defined $2) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800597 $av_paren_type[$av_paren] = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700598 }
599 $type = 'N';
600
Andy Whitcroft8905a672007-11-28 16:21:06 -0800601 } elsif ($cur =~ /^(if|while|typeof|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800602 print "COND($1)\n" if ($dbg_values > 1);
603 $av_paren_type[$av_paren] = 'N';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700604 $type = 'N';
605
606 } elsif ($cur =~/^(return|case|else)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800607 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700608 $type = 'N';
609
610 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800611 print "PAREN('$1')\n" if ($dbg_values > 1);
612 $av_paren++;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700613 $type = 'N';
614
615 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800616 $av_paren-- if ($av_paren > 0);
617 if (defined $av_paren_type[$av_paren]) {
618 $type = $av_paren_type[$av_paren];
619 undef $av_paren_type[$av_paren];
620 print "PAREN('$1') -> $type\n"
621 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700622 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800623 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700624 }
625
626 } elsif ($cur =~ /^($Ident)\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800627 print "FUNC($1)\n" if ($dbg_values > 1);
628 $av_paren_type[$av_paren] = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700629
630 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800631 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700632 $type = 'V';
633
634 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800635 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700636 $type = 'N';
637
638 } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800639 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700640 $type = 'N';
641
642 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800643 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700644 if ($1 ne '++' && $1 ne '--') {
645 $type = 'N';
646 }
647
648 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800649 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700650 }
651 if (defined $1) {
652 $cur = substr($cur, length($1));
653 $res .= $type x length($1);
654 }
655 }
656
657 return $res;
658}
659
Andy Whitcroft8905a672007-11-28 16:21:06 -0800660sub possible {
661 my ($possible) = @_;
662
663 #print "CHECK<$possible>\n";
664 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
665 $possible ne 'goto' && $possible ne 'return' &&
666 $possible ne 'struct' && $possible ne 'enum' &&
667 $possible ne 'case' && $possible ne 'else' &&
668 $possible ne 'typedef') {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800669 warn "POSSIBLE: $possible\n" if ($dbg_possible);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800670 push(@typeList, $possible);
671 build_types();
672 }
673}
674
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700675my $prefix = '';
676
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700677my @report = ();
678sub report {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800679 my $line = $prefix . $_[0];
680
681 $line = (split('\n', $line))[0] . "\n" if ($terse);
682
683 push(@report, $line);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700684}
685sub report_dump {
686 @report;
687}
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700688sub ERROR {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700689 report("ERROR: $_[0]\n");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700690 our $clean = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700691 our $cnt_error++;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700692}
693sub WARN {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -0700694 report("WARNING: $_[0]\n");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700695 our $clean = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700696 our $cnt_warn++;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700697}
698sub CHK {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700699 if ($check) {
700 report("CHECK: $_[0]\n");
701 our $clean = 0;
702 our $cnt_chk++;
703 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700704}
705
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700706sub process {
707 my $filename = shift;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700708
709 my $linenr=0;
710 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800711 my $prevrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700712 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800713 my $stashrawline="";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700714
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700715 my $length;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700716 my $indent;
717 my $previndent=0;
718 my $stashindent=0;
719
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700720 our $clean = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700721 my $signoff = 0;
722 my $is_patch = 0;
723
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700724 our $cnt_lines = 0;
725 our $cnt_error = 0;
726 our $cnt_warn = 0;
727 our $cnt_chk = 0;
728
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700729 # Trace the real file/line as we go.
730 my $realfile = '';
731 my $realline = 0;
732 my $realcnt = 0;
733 my $here = '';
734 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800735 my $comment_edge = 0;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700736 my $first_line = 0;
737
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700738 my $prev_values = 'N';
Andy Whitcroft653d4872007-06-23 17:16:34 -0700739
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800740 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700741 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800742 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700743 my @setup_docs = ();
744 my $setup_docs = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800745 my $line;
746 foreach my $rawline (@rawlines) {
747 # Standardise the strings and chars within the input to
748 # simplify matching.
749 $line = sanitise_line($rawline);
750 push(@lines, $line);
751
752 ##print "==>$rawline\n";
753 ##print "-->$line\n";
754
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700755 if ($line=~/^\+\+\+\s+(\S+)/) {
756 $setup_docs = 0;
757 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
758 $setup_docs = 1;
759 }
760 next;
761 }
762
763 if ($setup_docs && $line =~ /^\+/) {
764 push(@setup_docs, $line);
765 }
766 }
767
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700768 $prefix = '';
769
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700770 foreach my $line (@lines) {
771 $linenr++;
772
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800773 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700774
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700775#extract the filename as it passes
776 if ($line=~/^\+\+\+\s+(\S+)/) {
777 $realfile=$1;
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700778 $realfile =~ s@^[^/]*/@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700779 $in_comment = 0;
780 next;
781 }
782#extract the line range in the file after the patch is applied
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700783 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700784 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700785 $first_line = $linenr + 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700786 $in_comment = 0;
787 $realline=$1-1;
788 if (defined $2) {
789 $realcnt=$3+1;
790 } else {
791 $realcnt=1+1;
792 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800793 annotate_reset();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700794 $prev_values = 'N';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700795 next;
796 }
797
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700798# track the line number as we move through the hunk, note that
799# new versions of GNU diff omit the leading space on completely
800# blank context lines so we need to count that too.
801 if ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700802 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700803 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700804
Andy Whitcroft8905a672007-11-28 16:21:06 -0800805 # Guestimate if this is a continuing comment. Run
806 # the context looking for a comment "edge". If this
807 # edge is a close comment then we must be in a comment
808 # at context start.
809 if ($linenr == $first_line) {
810 my $edge;
811 for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800812 ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
Andy Whitcroft8905a672007-11-28 16:21:06 -0800813 last if (defined $edge);
814 }
815 if (defined $edge && $edge eq '*/') {
816 $in_comment = 1;
817 }
818 }
819
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700820 # Guestimate if this is a continuing comment. If this
821 # is the start of a diff block and this line starts
822 # ' *' then it is very likely a comment.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800823 if ($linenr == $first_line and $rawline =~ m@^.\s* \*(?:\s|$)@) {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700824 $in_comment = 1;
825 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800826
827 # Find the last comment edge on _this_ line.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800828 $comment_edge = 0;
829 while (($rawline =~ m@(/\*|\*/)@g)) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800830 if ($1 eq '/*') {
831 $in_comment = 1;
832 } else {
833 $in_comment = 0;
834 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800835 $comment_edge = 1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700836 }
837
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700838 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800839 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700840
841 # Track the previous line.
842 ($prevline, $stashline) = ($stashline, $line);
843 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800844 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
845
846 #warn "ic<$in_comment> ce<$comment_edge> line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700847
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700848 } elsif ($realcnt == 1) {
849 $realcnt--;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700850 }
851
852#make up the handle for any error we report on this line
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700853 $here = "#$linenr: " if (!$file);
854 $here = "#$realline: " if ($file);
Randy Dunlap389834b2007-06-08 13:47:03 -0700855 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700856
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800857 my $hereline = "$here\n$rawline\n";
858 my $herecurr = "$here\n$rawline\n";
859 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700860
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700861 $prefix = "$filename:$realline: " if ($emacs && $file);
862 $prefix = "$filename:$linenr: " if ($emacs && !$file);
863 $cnt_lines++ if ($realcnt != 0);
864
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700865#check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -0700866 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -0700867 # This is a signoff, if ugly, so do not double report.
868 $signoff++;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700869 if (!($line =~ /^\s*Signed-off-by:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700870 WARN("Signed-off-by: is the preferred form\n" .
871 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700872 }
873 if ($line =~ /^\s*signed-off-by:\S/i) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700874 WARN("need space after Signed-off-by:\n" .
875 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700876 }
877 }
878
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700879# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -0800880 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700881 ERROR("patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700882 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700883 }
884
885# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
886 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800887 !($rawline =~ m/^(
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700888 [\x09\x0A\x0D\x20-\x7E] # ASCII
889 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
890 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
891 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
892 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
893 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
894 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
895 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
896 )*$/x )) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800897 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700898 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700899
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700900#ignore lines being removed
901 if ($line=~/^-/) {next;}
902
903# check we are in a valid source file if not then ignore this hunk
904 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700905
906#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700907 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800908 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700909 ERROR("DOS line endings\n" . $herevet);
910
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800911 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
912 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700913 ERROR("trailing whitespace\n" . $herevet);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700914 }
915#80 column limit
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800916 if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700917 WARN("line over 80 characters\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700918 }
919
Andy Whitcroft8905a672007-11-28 16:21:06 -0800920# check for adding lines without a newline.
921 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
922 WARN("adding a line without newline at end of file\n" . $herecurr);
923 }
924
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700925# check we are in a valid source file *.[hc] if not then ignore this hunk
926 next if ($realfile !~ /\.[hc]$/);
927
928# at the beginning of a line any tabs must come first and anything
929# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800930 if ($rawline =~ /^\+\s* \t\s*\S/ ||
931 $rawline =~ /^\+\s* \s*/) {
932 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -0700933 ERROR("use tabs not spaces\n" . $herevet);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -0700934 }
935
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800936# check for RCS/CVS revision markers
937 if ($rawline =~ /\$(Revision|Log|Id)(?:\$|)/) {
938 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
939 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -0700940
941# The rest of our checks refer specifically to C style
942# only apply those _outside_ comments. Only skip
943# lines in the middle of comments.
944 next if (!$comment_edge && $in_comment);
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700945
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700946# Check for potential 'bare' types
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800947 if ($realcnt) {
948 # Ignore goto labels.
949 if ($line =~ /$Ident:\*$/) {
950
951 # Ignore functions being called
952 } elsif ($line =~ /^.\s*$Ident\s*\(/) {
953
Andy Whitcroft8905a672007-11-28 16:21:06 -0800954 # definitions in global scope can only start with types
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800955 } elsif ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800956 possible($1);
957
958 # declarations always start with types
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800959 } elsif ($prev_values eq 'N' && $line =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=)/) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800960 possible($1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800961 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800962
963 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800964 while ($line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800965 possible($1);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700966 }
Andy Whitcroft8905a672007-11-28 16:21:06 -0800967
968 # Check for any sort of function declaration.
969 # int foo(something bar, other baz);
970 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800971 if ($prev_values eq 'N' && $line =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
Andy Whitcroft8905a672007-11-28 16:21:06 -0800972 my ($name_len) = length($1);
973 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, $name_len);
974 my $ctx = join("\n", @ctx);
975
976 $ctx =~ s/\n.//;
977 substr($ctx, 0, $name_len + 1) = '';
978 $ctx =~ s/\)[^\)]*$//;
979 for my $arg (split(/\s*,\s*/, $ctx)) {
980 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
981
982 possible($1);
983 }
984 }
985 }
986
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -0700987 }
988
Andy Whitcroft653d4872007-06-23 17:16:34 -0700989#
990# Checks which may be anchored in the context.
991#
992
993# Check for switch () and associated case and default
994# statements should be at the same indent.
Andy Whitcroft00df344f2007-06-08 13:47:06 -0700995 if ($line=~/\bswitch\s*\(.*\)/) {
996 my $err = '';
997 my $sep = '';
998 my @ctx = ctx_block_outer($linenr, $realcnt);
999 shift(@ctx);
1000 for my $ctx (@ctx) {
1001 my ($clen, $cindent) = line_stats($ctx);
1002 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1003 $indent != $cindent) {
1004 $err .= "$sep$ctx\n";
1005 $sep = '';
1006 } else {
1007 $sep = "[...]\n";
1008 }
1009 }
1010 if ($err ne '') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001011 ERROR("switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001012 }
1013 }
1014
1015# if/while/etc brace do not go on next line, unless defining a do while loop,
1016# or if that brace on the next line is for something else
1017 if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001018 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001019 my $ctx_ln = $linenr + $#ctx + 1;
1020 my $ctx_cnt = $realcnt - $#ctx - 1;
1021 my $ctx = join("\n", @ctx);
1022
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001023 # Skip over any removed lines in the context following statement.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001024 while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
1025 $ctx_ln++;
1026 $ctx_cnt--;
1027 }
1028 ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
1029
1030 if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001031 ERROR("That open brace { should be on the previous line\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001032 "$here\n$ctx\n$lines[$ctx_ln - 1]");
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001033 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001034 if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) {
1035 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1036 if ($nindent > $indent) {
1037 WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" .
1038 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1039 }
1040 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001041 }
1042
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001043 # Track the 'values' across context and added lines.
1044 my $opline = $line; $opline =~ s/^./ /;
1045 my $curr_values = annotate_values($opline . "\n", $prev_values);
1046 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001047 if ($dbg_values) {
1048 my $outline = $opline; $outline =~ s/\t/ /g;
1049 warn "--> .$outline\n";
1050 warn "--> $curr_values\n";
1051 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001052 $prev_values = substr($curr_values, -1);
1053
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001054#ignore lines not being added
1055 if ($line=~/^[^\+]/) {next;}
1056
Andy Whitcroft653d4872007-06-23 17:16:34 -07001057# TEST: allow direct testing of the type matcher.
1058 if ($tst_type && $line =~ /^.$Declare$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001059 ERROR("TEST: is type $Declare\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001060 next;
1061 }
1062
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001063# check for initialisation to aggregates open brace on the next line
1064 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1065 $line =~ /^.\s*{/) {
1066 ERROR("That open brace { should be on the previous line\n" . $hereprev);
1067 }
1068
Andy Whitcroft653d4872007-06-23 17:16:34 -07001069#
1070# Checks which are anchored on the added line.
1071#
1072
1073# check for malformed paths in #include statements (uses RAW line)
1074 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1075 my $path = $1;
1076 if ($path =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001077 ERROR("malformed #include filename\n" .
1078 $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001079 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001080 }
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001081
1082# no C99 // comments
1083 if ($line =~ m{//}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001084 ERROR("do not use C99 // comments\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001085 }
1086 # Remove C99 comments.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001087 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001088 $opline =~ s@//.*@@;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001089
1090#EXPORT_SYMBOL should immediately follow its function closing }.
Andy Whitcroft653d4872007-06-23 17:16:34 -07001091 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1092 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1093 my $name = $1;
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001094 if (($prevline !~ /^}/) &&
1095 ($prevline !~ /^\+}/) &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001096 ($prevline !~ /^ }/) &&
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001097 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001098 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001099 }
1100 }
1101
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001102# check for external initialisers.
1103 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
1104 ERROR("do not initialise externals to 0 or NULL\n" .
1105 $herecurr);
1106 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001107# check for static initialisers.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001108 if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001109 ERROR("do not initialise statics to 0 or NULL\n" .
1110 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001111 }
1112
Andy Whitcroft653d4872007-06-23 17:16:34 -07001113# check for new typedefs, only function parameters and sparse annotations
1114# make sense.
1115 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001116 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07001117 $line !~ /\b__bitwise(?:__|)\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001118 WARN("do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001119 }
1120
1121# * goes on variable not on type
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001122 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001123 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1124 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001125
1126 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001127 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1128 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001129
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001130 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001131 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1132 $herecurr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001133
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001134 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001135 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1136 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001137 }
1138
1139# # no BUG() or BUG_ON()
1140# if ($line =~ /\b(BUG|BUG_ON)\b/) {
1141# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1142# print "$herecurr";
1143# $clean = 0;
1144# }
1145
Andy Whitcroft8905a672007-11-28 16:21:06 -08001146 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001147 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 -08001148 }
1149
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001150# printk should use KERN_* levels. Note that follow on printk's on the
1151# same line do not need a level, so we use the current block context
1152# to try and find and validate the current printk. In summary the current
1153# printk includes all preceeding printk's which have no newline on the end.
1154# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001155 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001156 my $ok = 0;
1157 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1158 #print "CHECK<$lines[$ln - 1]\n";
1159 # we have a preceeding printk if it ends
1160 # with "\n" ignore it, else it is to blame
1161 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1162 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1163 $ok = 1;
1164 }
1165 last;
1166 }
1167 }
1168 if ($ok == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001169 WARN("printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001170 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001171 }
1172
Andy Whitcroft653d4872007-06-23 17:16:34 -07001173# function brace can't be on same line, except for #defines of do while,
1174# or if closed on same line
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001175 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001176 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001177 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001178 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001179
Andy Whitcroft8905a672007-11-28 16:21:06 -08001180# open braces for enum, union and struct go on the same line.
1181 if ($line =~ /^.\s*{/ &&
1182 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1183 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1184 }
1185
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001186# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001187 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001188 my $name = $1;
1189 my $ctx = substr($line, 0, $-[1]);
1190
1191 # Ignore those directives where spaces _are_ permitted.
1192 if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case)$/) {
1193
1194 # cpp #define statements have non-optional spaces, ie
1195 # if there is a space between the name and the open
1196 # parenthesis it is simply not a parameter group.
1197 } elsif ($ctx =~ /^.\#\s*define\s*$/) {
1198
1199 # If this whole things ends with a type its most
1200 # likely a typedef for a function.
1201 } elsif ("$ctx$name" =~ /$Type$/) {
1202
1203 } else {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001204 WARN("no space between function name and open parenthesis '('\n" . $herecurr);
1205 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001206 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001207# Check operator spacing.
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001208 if (!($line=~/\#\s*include/)) {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001209 my $ops = qr{
1210 <<=|>>=|<=|>=|==|!=|
1211 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1212 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001213 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001214 }x;
1215 my @elements = split(/($ops|;)/, $opline);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001216 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001217
1218 my $blank = copy_spacing($opline);
1219
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001220 for (my $n = 0; $n < $#elements; $n += 2) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001221 $off += length($elements[$n]);
1222
1223 my $a = '';
1224 $a = 'V' if ($elements[$n] ne '');
1225 $a = 'W' if ($elements[$n] =~ /\s$/);
1226 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1227 $a = 'O' if ($elements[$n] eq '');
1228 $a = 'E' if ($elements[$n] eq '' && $n == 0);
1229
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001230 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001231
1232 my $c = '';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001233 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001234 $c = 'V' if ($elements[$n + 2] ne '');
1235 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1236 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1237 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001238 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001239 } else {
1240 $c = 'E';
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001241 }
1242
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001243 # Pick up the preceeding and succeeding characters.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001244 my $ca = substr($opline, 0, $off);
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001245 my $cc = '';
Andy Whitcroft653d4872007-06-23 17:16:34 -07001246 if (length($opline) >= ($off + length($elements[$n + 1]))) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001247 $cc = substr($opline, $off + length($elements[$n + 1]));
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001248 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001249 my $cb = "$ca$;$cc";
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001250
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001251 my $ctx = "${a}x${c}";
1252
1253 my $at = "(ctx:$ctx)";
1254
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001255 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001256 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001257
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001258 # Classify operators into binary, unary, or
1259 # definitions (* only) where they have more
1260 # than one mode.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001261 my $op_type = substr($curr_values, $off + 1, 1);
1262 my $op_left = substr($curr_values, $off, 1);
1263 my $is_unary;
1264 if ($op_type eq 'T') {
1265 $is_unary = 2;
1266 } elsif ($op_left eq 'V') {
1267 $is_unary = 0;
1268 } else {
1269 $is_unary = 1;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001270 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001271 #if ($op eq '-' || $op eq '&' || $op eq '*') {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001272 # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001273 #}
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001274
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001275 # ; should have either the end of line or a space or \ after it
1276 if ($op eq ';') {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001277 if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
1278 $cc !~ /^;/) {
1279 ERROR("need space after that '$op' $at\n" . $hereptr);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001280 }
1281
1282 # // is a comment
1283 } elsif ($op eq '//') {
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001284
1285 # -> should have no spaces
1286 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001287 if ($ctx =~ /Wx.|.xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001288 ERROR("no spaces around that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001289 }
1290
1291 # , must have a space on the right.
1292 } elsif ($op eq ',') {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001293 if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001294 ERROR("need space after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001295 }
1296
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001297 # '*' as part of a type definition -- reported already.
1298 } elsif ($op eq '*' && $is_unary == 2) {
1299 #warn "'*' is part of type\n";
1300
1301 # unary operators should have a space before and
1302 # none after. May be left adjacent to another
1303 # unary operator, or a cast
1304 } elsif ($op eq '!' || $op eq '~' ||
1305 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
1306 if ($ctx !~ /[WEB]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001307 ERROR("need space before that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001308 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001309 if ($ctx =~ /.xW/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001310 ERROR("no space after that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001311 }
1312
1313 # unary ++ and unary -- are allowed no space on one side.
1314 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001315 if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001316 ERROR("need space one side of that '$op' $at\n" . $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001317 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001318 if ($ctx =~ /Wx./ && $cc =~ /^;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001319 ERROR("no space before that '$op' $at\n" . $hereptr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07001320 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001321
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001322 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001323 } elsif ($op eq '<<' or $op eq '>>' or
1324 $op eq '&' or $op eq '^' or $op eq '|' or
1325 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001326 $op eq '*' or $op eq '/' or
1327 $op eq '%')
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001328 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001329 if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001330 ERROR("need consistent spacing around '$op' $at\n" .
1331 $hereptr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001332 }
1333
1334 # All the others need spaces both sides.
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001335 } elsif ($ctx !~ /[EW]x[WE]/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001336 # Ignore email addresses <foo@bar>
1337 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1338 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1339 ERROR("need spaces around that '$op' $at\n" . $hereptr);
1340 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001341 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001342 $off += length($elements[$n + 1]);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001343 }
1344 }
1345
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001346# check for multiple assignments
1347 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001348 CHK("multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001349 }
1350
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001351## # check for multiple declarations, allowing for a function declaration
1352## # continuation.
1353## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1354## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1355##
1356## # Remove any bracketed sections to ensure we do not
1357## # falsly report the parameters of functions.
1358## my $ln = $line;
1359## while ($ln =~ s/\([^\(\)]*\)//g) {
1360## }
1361## if ($ln =~ /,/) {
1362## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1363## }
1364## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001365
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001366#need space before brace following if, while, etc
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001367 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1368 $line =~ /do{/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001369 ERROR("need a space before the open brace '{'\n" . $herecurr);
1370 }
1371
1372# closing brace should have a space following it when it has anything
1373# on the line
1374 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1375 ERROR("need a space after that close brace '}'\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001376 }
1377
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001378# check spacing on square brackets
1379 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1380 ERROR("no space after that open square bracket '['\n" . $herecurr);
1381 }
1382 if ($line =~ /\s\]/) {
1383 ERROR("no space before that close square bracket ']'\n" . $herecurr);
1384 }
1385
1386# check spacing on paretheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001387 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1388 $line !~ /for\s*\(\s+;/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001389 ERROR("no space after that open parenthesis '('\n" . $herecurr);
1390 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001391 if ($line =~ /\s\)/ && $line !~ /^.\s*\)/ &&
1392 $line !~ /for\s*\(.*;\s+\)/) {
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001393 ERROR("no space before that close parenthesis ')'\n" . $herecurr);
1394 }
1395
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001396#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001397 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001398 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001399 WARN("labels should not be indented\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001400 }
1401
1402# Need a space before open parenthesis after if, while etc
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001403 if ($line=~/\b(if|while|for|switch)\(/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001404 ERROR("need a space before the open parenthesis '('\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001405 }
1406
1407# Check for illegal assignment in if conditional.
Andy Whitcroft8905a672007-11-28 16:21:06 -08001408 if ($line =~ /\bif\s*\(/) {
1409 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1410
1411 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001412 ERROR("do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001413 }
1414
1415 # Find out what is on the end of the line after the
1416 # conditional.
1417 substr($s, 0, length($c)) = '';
1418 $s =~ s/\n.*//g;
1419
1420 if (length($c) && $s !~ /^\s*({|;|\/\*.*\*\/)?\s*\\*\s*$/) {
1421 ERROR("trailing statements should be on next line\n" . $herecurr);
1422 }
1423 }
1424
1425# if and else should not have general statements after it
1426 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/ &&
1427 $1 !~ /^\s*(?:\sif|{|\\|$)/) {
1428 ERROR("trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001429 }
1430
1431 # Check for }<nl>else {, these must be at the same
1432 # indent level to be relevant to each other.
1433 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
1434 $previndent == $indent) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001435 ERROR("else should follow close brace '}'\n" . $hereprev);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001436 }
1437
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001438 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1439 $previndent == $indent) {
1440 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1441
1442 # Find out what is on the end of the line after the
1443 # conditional.
1444 substr($s, 0, length($c)) = '';
1445 $s =~ s/\n.*//g;
1446
1447 if ($s =~ /^\s*;/) {
1448 ERROR("while should follow close brace '}'\n" . $hereprev);
1449 }
1450 }
1451
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001452#studly caps, commented out until figure out how to distinguish between use of existing and adding new
1453# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
1454# print "No studly caps, use _\n";
1455# print "$herecurr";
1456# $clean = 0;
1457# }
1458
1459#no spaces allowed after \ in define
1460 if ($line=~/\#define.*\\\s$/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001461 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001462 }
1463
Andy Whitcroft653d4872007-06-23 17:16:34 -07001464#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1465 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001466 my $checkfile = "$root/include/linux/$1.h";
1467 if (-f $checkfile && $1 ne 'irq.h') {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001468 CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1469 $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001470 }
1471 }
1472
Andy Whitcroft653d4872007-06-23 17:16:34 -07001473# multi-statement macros should be enclosed in a do while loop, grab the
1474# first statement and ensure its the whole macro if its not enclosed
1475# in a known goot container
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001476 if ($prevline =~ /\#define.*\\/ &&
1477 $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1478 $line !~ /(?:do\s+{|\(\{|\{)/ &&
1479 $line !~ /^.\s*$Declare\s/) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001480 # Grab the first statement, if that is the entire macro
1481 # its ok. This may start either on the #define line
1482 # or the one below.
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001483 my $ln = $linenr;
1484 my $cnt = $realcnt;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001485 my $off = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001486
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001487 # If the macro starts on the define line start
1488 # grabbing the statement after the identifier
1489 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1490 ##print "1<$1> 2<$2>\n";
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001491 if (defined $2 && $2 ne '') {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001492 $off = length($1);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001493 $ln--;
1494 $cnt++;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001495 while ($lines[$ln - 1] =~ /^-/) {
1496 $ln--;
1497 $cnt++;
1498 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001499 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001500 my @ctx = ctx_statement($ln, $cnt, $off);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001501 my $ctx_ln = $ln + $#ctx + 1;
1502 my $ctx = join("\n", @ctx);
1503
1504 # Pull in any empty extension lines.
1505 while ($ctx =~ /\\$/ &&
1506 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1507 $ctx .= $lines[$ctx_ln - 1];
1508 $ctx_ln++;
1509 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001510
1511 if ($ctx =~ /\\$/) {
1512 if ($ctx =~ /;/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001513 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 -07001514 } else {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001515 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07001516 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001517 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001518 }
1519
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001520# check for redundant bracing round if etc
1521 if ($line =~ /\b(if|while|for|else)\b/) {
1522 # Locate the end of the opening statement.
1523 my @control = ctx_statement($linenr, $realcnt, 0);
1524 my $nr = $linenr + (scalar(@control) - 1);
1525 my $cnt = $realcnt - (scalar(@control) - 1);
1526
1527 my $off = $realcnt - $cnt;
1528 #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n";
1529
1530 # If this is is a braced statement group check it
1531 if ($lines[$nr - 1] =~ /{\s*$/) {
1532 my ($lvl, @block) = ctx_block_level($nr, $cnt);
1533
Andy Whitcroft8905a672007-11-28 16:21:06 -08001534 my $stmt = join("\n", @block);
1535 # Drop the diff line leader.
1536 $stmt =~ s/\n./\n/g;
1537 # Drop the code outside the block.
1538 $stmt =~ s/(^[^{]*){\s*//;
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001539 my $before = $1;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001540 $stmt =~ s/\s*}([^}]*$)//;
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001541 my $after = $1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001542
1543 #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n";
1544 #print "stmt<$stmt>\n\n";
1545
Andy Whitcroft8905a672007-11-28 16:21:06 -08001546 # Count the newlines, if there is only one
1547 # then the block should not have {}'s.
1548 my @lines = ($stmt =~ /\n/g);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001549 my @statements = ($stmt =~ /;/g);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001550 #print "lines<" . scalar(@lines) . ">\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001551 #print "statements<" . scalar(@statements) . ">\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001552 if ($lvl == 0 && scalar(@lines) == 0 &&
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001553 scalar(@statements) < 2 &&
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001554 $stmt !~ /{/ && $stmt !~ /\bif\b/ &&
1555 $before !~ /}/ && $after !~ /{/) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001556 my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n";
1557 shift(@block);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001558 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001559 }
1560 }
1561 }
1562
Andy Whitcroft653d4872007-06-23 17:16:34 -07001563# don't include deprecated include files (uses RAW line)
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001564 for my $inc (@dep_includes) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001565 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001566 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001567 }
1568 }
1569
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001570# don't use deprecated functions
1571 for my $func (@dep_functions) {
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001572 if ($line =~ /\b$func\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001573 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001574 }
1575 }
1576
1577# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001578 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
1579 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001580 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001581 }
1582
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001583# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
1584 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
1585 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
1586 }
1587
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001588# warn about #if 0
1589 if ($line =~ /^.#\s*if\s+0\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001590 CHK("if this code is redundant consider removing it\n" .
1591 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001592 }
1593
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001594# check for needless kfree() checks
1595 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1596 my $expr = $1;
1597 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1598 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1599 }
1600 }
1601
Andy Whitcroft00df344f2007-06-08 13:47:06 -07001602# warn about #ifdefs in C files
1603# if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1604# print "#ifdef in C files should be avoided\n";
1605# print "$herecurr";
1606# $clean = 0;
1607# }
1608
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001609# warn about spacing in #ifdefs
1610 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1611 ERROR("exactly one space required after that #$1\n" . $herecurr);
1612 }
1613
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001614# check for spinlock_t definitions without a comment.
1615 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
1616 my $which = $1;
1617 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001618 CHK("$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001619 }
1620 }
1621# check for memory barriers without a comment.
1622 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
1623 if (!ctx_has_comment($first_line, $linenr)) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001624 CHK("memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001625 }
1626 }
1627# check of hardware specific defines
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07001628 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001629 CHK("architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001630 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001631
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001632# check the location of the inline attribute, that it is between
1633# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001634 if ($line =~ /\b$Type\s+$Inline\b/ ||
1635 $line =~ /\b$Inline\s+$Storage\b/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001636 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1637 }
1638
Andy Whitcroft8905a672007-11-28 16:21:06 -08001639# Check for __inline__ and __inline, prefer inline
1640 if ($line =~ /\b(__inline__|__inline)\b/) {
1641 WARN("plain inline is preferred over $1\n" . $herecurr);
1642 }
1643
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001644# check for new externs in .c files.
1645 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
1646 WARN("externs should be avoided in .c files\n" . $herecurr);
1647 }
1648
1649# checks for new __setup's
1650 if ($rawline =~ /\b__setup\("([^"]*)"/) {
1651 my $name = $1;
1652
1653 if (!grep(/$name/, @setup_docs)) {
1654 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
1655 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07001656 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001657
1658# check for pointless casting of kmalloc return
1659 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
1660 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
1661 }
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001662 }
1663
Andy Whitcroft8905a672007-11-28 16:21:06 -08001664 # In mailback mode only produce a report in the negative, for
1665 # things that appear to be patches.
1666 if ($mailback && ($clean == 1 || !$is_patch)) {
1667 exit(0);
1668 }
1669
1670 # This is not a patch, and we are are in 'no-patch' mode so
1671 # just keep quiet.
1672 if (!$chk_patch && !$is_patch) {
1673 exit(0);
1674 }
1675
1676 if (!$is_patch) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001677 ERROR("Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001678 }
1679 if ($is_patch && $chk_signoff && $signoff == 0) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001680 ERROR("Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001681 }
1682
Andy Whitcroft8905a672007-11-28 16:21:06 -08001683 print report_dump();
1684 if ($summary) {
1685 print "total: $cnt_error errors, $cnt_warn warnings, " .
1686 (($check)? "$cnt_chk checks, " : "") .
1687 "$cnt_lines lines checked\n";
1688 print "\n" if ($quiet == 0);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001689 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001690
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001691 if ($clean == 1 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001692 print "$vname has no obvious style problems and is ready for submission.\n"
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001693 }
1694 if ($clean == 0 && $quiet == 0) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001695 print "$vname has style problems, please review. If any of these errors\n";
Andy Whitcroft0a920b5b2007-06-01 00:46:48 -07001696 print "are false positives report them to the maintainer, see\n";
1697 print "CHECKPATCH in MAINTAINERS.\n";
1698 }
1699 return $clean;
1700}