]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - scripts/checkpatch.pl
checkpatch: refactor 'allowed asm includes' and add memory.h
[mirror_ubuntu-artful-kernel.git] / scripts / checkpatch.pl
CommitLineData
0a920b5b 1#!/usr/bin/perl -w
dbf004d7 2# (c) 2001, Dave Jones. (the file handling bit)
00df344f 3# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
2a5a2c25 4# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
131edb34 5# (c) 2008,2009, Andy Whitcroft <apw@canonical.com>
0a920b5b
AW
6# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
9
10my $P = $0;
00df344f 11$P =~ s@.*/@@g;
0a920b5b 12
5e8d8f6f 13my $V = '0.30';
0a920b5b
AW
14
15use Getopt::Long qw(:config no_auto_abbrev);
16
17my $quiet = 0;
18my $tree = 1;
19my $chk_signoff = 1;
20my $chk_patch = 1;
773647a0 21my $tst_only;
6c72ffaa 22my $emacs = 0;
8905a67c 23my $terse = 0;
6c72ffaa
AW
24my $file = 0;
25my $check = 0;
8905a67c
AW
26my $summary = 1;
27my $mailback = 0;
13214adf 28my $summary_file = 0;
6c72ffaa 29my $root;
c2fdda0d 30my %debug;
77f5b10a
HE
31my $help = 0;
32
33sub help {
34 my ($exitcode) = @_;
35
36 print << "EOM";
37Usage: $P [OPTION]... [FILE]...
38Version: $V
39
40Options:
41 -q, --quiet quiet
42 --no-tree run without a kernel tree
43 --no-signoff do not check for 'Signed-off-by' line
44 --patch treat FILE as patchfile (default)
45 --emacs emacs compile window format
46 --terse one line per report
47 -f, --file treat FILE as regular source file
48 --subjective, --strict enable more subjective tests
49 --root=PATH PATH to the kernel tree root
50 --no-summary suppress the per-file summary
51 --mailback only produce a report in case of warnings/errors
52 --summary-file include the filename in summary
53 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
54 'values', 'possible', 'type', and 'attr' (default
55 is all off)
56 --test-only=WORD report only warnings/errors containing WORD
57 literally
58 -h, --help, --version display this help and exit
59
60When FILE is - read standard input.
61EOM
62
63 exit($exitcode);
64}
65
0a920b5b 66GetOptions(
6c72ffaa 67 'q|quiet+' => \$quiet,
0a920b5b
AW
68 'tree!' => \$tree,
69 'signoff!' => \$chk_signoff,
70 'patch!' => \$chk_patch,
6c72ffaa 71 'emacs!' => \$emacs,
8905a67c 72 'terse!' => \$terse,
77f5b10a 73 'f|file!' => \$file,
6c72ffaa
AW
74 'subjective!' => \$check,
75 'strict!' => \$check,
76 'root=s' => \$root,
8905a67c
AW
77 'summary!' => \$summary,
78 'mailback!' => \$mailback,
13214adf
AW
79 'summary-file!' => \$summary_file,
80
c2fdda0d 81 'debug=s' => \%debug,
773647a0 82 'test-only=s' => \$tst_only,
77f5b10a
HE
83 'h|help' => \$help,
84 'version' => \$help
85) or help(1);
86
87help(0) if ($help);
0a920b5b
AW
88
89my $exit = 0;
90
91if ($#ARGV < 0) {
77f5b10a 92 print "$P: no input files\n";
0a920b5b
AW
93 exit(1);
94}
95
c2fdda0d
AW
96my $dbg_values = 0;
97my $dbg_possible = 0;
7429c690 98my $dbg_type = 0;
a1ef277e 99my $dbg_attr = 0;
c2fdda0d 100for my $key (keys %debug) {
21caa13c
AW
101 ## no critic
102 eval "\${dbg_$key} = '$debug{$key}';";
103 die "$@" if ($@);
c2fdda0d
AW
104}
105
8905a67c
AW
106if ($terse) {
107 $emacs = 1;
108 $quiet++;
109}
110
6c72ffaa
AW
111if ($tree) {
112 if (defined $root) {
113 if (!top_of_kernel_tree($root)) {
114 die "$P: $root: --root does not point at a valid tree\n";
115 }
116 } else {
117 if (top_of_kernel_tree('.')) {
118 $root = '.';
119 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
120 top_of_kernel_tree($1)) {
121 $root = $1;
122 }
123 }
124
125 if (!defined $root) {
126 print "Must be run from the top-level dir. of a kernel tree\n";
127 exit(2);
128 }
0a920b5b
AW
129}
130
6c72ffaa
AW
131my $emitted_corrupt = 0;
132
2ceb532b
AW
133our $Ident = qr{
134 [A-Za-z_][A-Za-z\d_]*
135 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
136 }x;
6c72ffaa
AW
137our $Storage = qr{extern|static|asmlinkage};
138our $Sparse = qr{
139 __user|
140 __kernel|
141 __force|
142 __iomem|
143 __must_check|
144 __init_refok|
417495ed
AW
145 __kprobes|
146 __ref
6c72ffaa 147 }x;
52131292
WS
148
149# Notes to $Attribute:
150# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
6c72ffaa
AW
151our $Attribute = qr{
152 const|
153 __read_mostly|
154 __kprobes|
52131292 155 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
24e1d81a
AW
156 ____cacheline_aligned|
157 ____cacheline_aligned_in_smp|
5fe3af11
AW
158 ____cacheline_internodealigned_in_smp|
159 __weak
6c72ffaa 160 }x;
c45dcabd 161our $Modifier;
6c72ffaa 162our $Inline = qr{inline|__always_inline|noinline};
6c72ffaa
AW
163our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
164our $Lval = qr{$Ident(?:$Member)*};
165
166our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
167our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
86f9d059 168our $Compare = qr{<=|>=|==|!=|<|>};
6c72ffaa
AW
169our $Operators = qr{
170 <=|>=|==|!=|
171 =>|->|<<|>>|<|>|!|~|
c2fdda0d 172 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
6c72ffaa
AW
173 }x;
174
8905a67c
AW
175our $NonptrType;
176our $Type;
177our $Declare;
178
171ae1a4
AW
179our $UTF8 = qr {
180 [\x09\x0A\x0D\x20-\x7E] # ASCII
181 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
182 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
183 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
184 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
185 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
186 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
187 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
188}x;
189
8ed22cad 190our $typeTypedefs = qr{(?x:
fb9e9096 191 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
8ed22cad
AW
192 atomic_t
193)};
194
691e669b
JP
195our $logFunctions = qr{(?x:
196 printk|
197 pr_(debug|dbg|vdbg|devel|info|warning|err|notice|alert|crit|emerg|cont)|
198 dev_(printk|dbg|vdbg|info|warn|err|notice|alert|crit|emerg|WARN)|
199 WARN|
200 panic
201)};
202
8905a67c
AW
203our @typeList = (
204 qr{void},
c45dcabd
AW
205 qr{(?:unsigned\s+)?char},
206 qr{(?:unsigned\s+)?short},
207 qr{(?:unsigned\s+)?int},
208 qr{(?:unsigned\s+)?long},
209 qr{(?:unsigned\s+)?long\s+int},
210 qr{(?:unsigned\s+)?long\s+long},
211 qr{(?:unsigned\s+)?long\s+long\s+int},
8905a67c
AW
212 qr{unsigned},
213 qr{float},
214 qr{double},
215 qr{bool},
8905a67c
AW
216 qr{struct\s+$Ident},
217 qr{union\s+$Ident},
218 qr{enum\s+$Ident},
219 qr{${Ident}_t},
220 qr{${Ident}_handler},
221 qr{${Ident}_handler_fn},
222);
c45dcabd
AW
223our @modifierList = (
224 qr{fastcall},
225);
8905a67c 226
7840a94c
WS
227our $allowed_asm_includes = qr{(?x:
228 irq|
229 memory
230)};
231# memory.h: ARM has a custom one
232
8905a67c 233sub build_types {
d2172eb5
AW
234 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
235 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
c8cb2ca3 236 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
8905a67c 237 $NonptrType = qr{
d2172eb5 238 (?:$Modifier\s+|const\s+)*
cf655043 239 (?:
c45dcabd 240 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
8ed22cad 241 (?:$typeTypedefs\b)|
c45dcabd 242 (?:${all}\b)
cf655043 243 )
c8cb2ca3 244 (?:\s+$Modifier|\s+const)*
8905a67c
AW
245 }x;
246 $Type = qr{
c45dcabd 247 $NonptrType
65863862 248 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
c8cb2ca3 249 (?:\s+$Inline|\s+$Modifier)*
8905a67c
AW
250 }x;
251 $Declare = qr{(?:$Storage\s+)?$Type};
252}
253build_types();
6c72ffaa
AW
254
255$chk_signoff = 0 if ($file);
256
4a0df2ef
AW
257my @dep_includes = ();
258my @dep_functions = ();
6c72ffaa
AW
259my $removal = "Documentation/feature-removal-schedule.txt";
260if ($tree && -f "$root/$removal") {
21caa13c 261 open(my $REMOVE, '<', "$root/$removal") ||
6c72ffaa 262 die "$P: $removal: open failed - $!\n";
21caa13c 263 while (<$REMOVE>) {
f0a594c1
AW
264 if (/^Check:\s+(.*\S)/) {
265 for my $entry (split(/[, ]+/, $1)) {
266 if ($entry =~ m@include/(.*)@) {
4a0df2ef 267 push(@dep_includes, $1);
4a0df2ef 268
f0a594c1
AW
269 } elsif ($entry !~ m@/@) {
270 push(@dep_functions, $entry);
271 }
4a0df2ef 272 }
0a920b5b
AW
273 }
274 }
21caa13c 275 close($REMOVE);
0a920b5b
AW
276}
277
00df344f 278my @rawlines = ();
c2fdda0d
AW
279my @lines = ();
280my $vname;
6c72ffaa 281for my $filename (@ARGV) {
21caa13c 282 my $FILE;
6c72ffaa 283 if ($file) {
21caa13c 284 open($FILE, '-|', "diff -u /dev/null $filename") ||
6c72ffaa 285 die "$P: $filename: diff failed - $!\n";
21caa13c
AW
286 } elsif ($filename eq '-') {
287 open($FILE, '<&STDIN');
6c72ffaa 288 } else {
21caa13c 289 open($FILE, '<', "$filename") ||
6c72ffaa 290 die "$P: $filename: open failed - $!\n";
0a920b5b 291 }
c2fdda0d
AW
292 if ($filename eq '-') {
293 $vname = 'Your patch';
294 } else {
295 $vname = $filename;
296 }
21caa13c 297 while (<$FILE>) {
6c72ffaa
AW
298 chomp;
299 push(@rawlines, $_);
300 }
21caa13c 301 close($FILE);
c2fdda0d 302 if (!process($filename)) {
6c72ffaa
AW
303 $exit = 1;
304 }
305 @rawlines = ();
13214adf 306 @lines = ();
0a920b5b
AW
307}
308
309exit($exit);
310
311sub top_of_kernel_tree {
6c72ffaa
AW
312 my ($root) = @_;
313
314 my @tree_check = (
315 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
316 "README", "Documentation", "arch", "include", "drivers",
317 "fs", "init", "ipc", "kernel", "lib", "scripts",
318 );
319
320 foreach my $check (@tree_check) {
321 if (! -e $root . '/' . $check) {
322 return 0;
323 }
0a920b5b 324 }
6c72ffaa 325 return 1;
0a920b5b
AW
326}
327
328sub expand_tabs {
329 my ($str) = @_;
330
331 my $res = '';
332 my $n = 0;
333 for my $c (split(//, $str)) {
334 if ($c eq "\t") {
335 $res .= ' ';
336 $n++;
337 for (; ($n % 8) != 0; $n++) {
338 $res .= ' ';
339 }
340 next;
341 }
342 $res .= $c;
343 $n++;
344 }
345
346 return $res;
347}
6c72ffaa 348sub copy_spacing {
773647a0 349 (my $res = shift) =~ tr/\t/ /c;
6c72ffaa
AW
350 return $res;
351}
0a920b5b 352
4a0df2ef
AW
353sub line_stats {
354 my ($line) = @_;
355
356 # Drop the diff line leader and expand tabs
357 $line =~ s/^.//;
358 $line = expand_tabs($line);
359
360 # Pick the indent from the front of the line.
361 my ($white) = ($line =~ /^(\s*)/);
362
363 return (length($line), length($white));
364}
365
773647a0
AW
366my $sanitise_quote = '';
367
368sub sanitise_line_reset {
369 my ($in_comment) = @_;
370
371 if ($in_comment) {
372 $sanitise_quote = '*/';
373 } else {
374 $sanitise_quote = '';
375 }
376}
00df344f
AW
377sub sanitise_line {
378 my ($line) = @_;
379
380 my $res = '';
381 my $l = '';
382
c2fdda0d 383 my $qlen = 0;
773647a0
AW
384 my $off = 0;
385 my $c;
00df344f 386
773647a0
AW
387 # Always copy over the diff marker.
388 $res = substr($line, 0, 1);
389
390 for ($off = 1; $off < length($line); $off++) {
391 $c = substr($line, $off, 1);
392
393 # Comments we are wacking completly including the begin
394 # and end, all to $;.
395 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
396 $sanitise_quote = '*/';
397
398 substr($res, $off, 2, "$;$;");
399 $off++;
400 next;
00df344f 401 }
81bc0e02 402 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
773647a0
AW
403 $sanitise_quote = '';
404 substr($res, $off, 2, "$;$;");
405 $off++;
406 next;
c2fdda0d 407 }
113f04a8
DW
408 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
409 $sanitise_quote = '//';
410
411 substr($res, $off, 2, $sanitise_quote);
412 $off++;
413 next;
414 }
773647a0
AW
415
416 # A \ in a string means ignore the next character.
417 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
418 $c eq "\\") {
419 substr($res, $off, 2, 'XX');
420 $off++;
421 next;
00df344f 422 }
773647a0
AW
423 # Regular quotes.
424 if ($c eq "'" || $c eq '"') {
425 if ($sanitise_quote eq '') {
426 $sanitise_quote = $c;
00df344f 427
773647a0
AW
428 substr($res, $off, 1, $c);
429 next;
430 } elsif ($sanitise_quote eq $c) {
431 $sanitise_quote = '';
432 }
433 }
00df344f 434
fae17dae 435 #print "c<$c> SQ<$sanitise_quote>\n";
773647a0
AW
436 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
437 substr($res, $off, 1, $;);
113f04a8
DW
438 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
439 substr($res, $off, 1, $;);
773647a0
AW
440 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
441 substr($res, $off, 1, 'X');
442 } else {
443 substr($res, $off, 1, $c);
444 }
c2fdda0d
AW
445 }
446
113f04a8
DW
447 if ($sanitise_quote eq '//') {
448 $sanitise_quote = '';
449 }
450
c2fdda0d 451 # The pathname on a #include may be surrounded by '<' and '>'.
c45dcabd 452 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
c2fdda0d
AW
453 my $clean = 'X' x length($1);
454 $res =~ s@\<.*\>@<$clean>@;
455
456 # The whole of a #error is a string.
c45dcabd 457 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
c2fdda0d 458 my $clean = 'X' x length($1);
c45dcabd 459 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
c2fdda0d
AW
460 }
461
00df344f
AW
462 return $res;
463}
464
8905a67c
AW
465sub ctx_statement_block {
466 my ($linenr, $remain, $off) = @_;
467 my $line = $linenr - 1;
468 my $blk = '';
469 my $soff = $off;
470 my $coff = $off - 1;
773647a0 471 my $coff_set = 0;
8905a67c 472
13214adf
AW
473 my $loff = 0;
474
8905a67c
AW
475 my $type = '';
476 my $level = 0;
a2750645 477 my @stack = ();
cf655043 478 my $p;
8905a67c
AW
479 my $c;
480 my $len = 0;
13214adf
AW
481
482 my $remainder;
8905a67c 483 while (1) {
a2750645
AW
484 @stack = (['', 0]) if ($#stack == -1);
485
773647a0 486 #warn "CSB: blk<$blk> remain<$remain>\n";
8905a67c
AW
487 # If we are about to drop off the end, pull in more
488 # context.
489 if ($off >= $len) {
490 for (; $remain > 0; $line++) {
dea33496 491 last if (!defined $lines[$line]);
c2fdda0d 492 next if ($lines[$line] =~ /^-/);
8905a67c 493 $remain--;
13214adf 494 $loff = $len;
c2fdda0d 495 $blk .= $lines[$line] . "\n";
8905a67c
AW
496 $len = length($blk);
497 $line++;
498 last;
499 }
500 # Bail if there is no further context.
501 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
13214adf 502 if ($off >= $len) {
8905a67c
AW
503 last;
504 }
505 }
cf655043 506 $p = $c;
8905a67c 507 $c = substr($blk, $off, 1);
13214adf 508 $remainder = substr($blk, $off);
8905a67c 509
773647a0 510 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
4635f4fb
AW
511
512 # Handle nested #if/#else.
513 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
514 push(@stack, [ $type, $level ]);
515 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
516 ($type, $level) = @{$stack[$#stack - 1]};
517 } elsif ($remainder =~ /^#\s*endif\b/) {
518 ($type, $level) = @{pop(@stack)};
519 }
520
8905a67c
AW
521 # Statement ends at the ';' or a close '}' at the
522 # outermost level.
523 if ($level == 0 && $c eq ';') {
524 last;
525 }
526
13214adf 527 # An else is really a conditional as long as its not else if
773647a0
AW
528 if ($level == 0 && $coff_set == 0 &&
529 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
530 $remainder =~ /^(else)(?:\s|{)/ &&
531 $remainder !~ /^else\s+if\b/) {
532 $coff = $off + length($1) - 1;
533 $coff_set = 1;
534 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
535 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
13214adf
AW
536 }
537
8905a67c
AW
538 if (($type eq '' || $type eq '(') && $c eq '(') {
539 $level++;
540 $type = '(';
541 }
542 if ($type eq '(' && $c eq ')') {
543 $level--;
544 $type = ($level != 0)? '(' : '';
545
546 if ($level == 0 && $coff < $soff) {
547 $coff = $off;
773647a0
AW
548 $coff_set = 1;
549 #warn "CSB: mark coff<$coff>\n";
8905a67c
AW
550 }
551 }
552 if (($type eq '' || $type eq '{') && $c eq '{') {
553 $level++;
554 $type = '{';
555 }
556 if ($type eq '{' && $c eq '}') {
557 $level--;
558 $type = ($level != 0)? '{' : '';
559
560 if ($level == 0) {
561 last;
562 }
563 }
564 $off++;
565 }
a3bb97a7 566 # We are truly at the end, so shuffle to the next line.
13214adf 567 if ($off == $len) {
a3bb97a7 568 $loff = $len + 1;
13214adf
AW
569 $line++;
570 $remain--;
571 }
8905a67c
AW
572
573 my $statement = substr($blk, $soff, $off - $soff + 1);
574 my $condition = substr($blk, $soff, $coff - $soff + 1);
575
576 #warn "STATEMENT<$statement>\n";
577 #warn "CONDITION<$condition>\n";
578
773647a0 579 #print "coff<$coff> soff<$off> loff<$loff>\n";
13214adf
AW
580
581 return ($statement, $condition,
582 $line, $remain + 1, $off - $loff + 1, $level);
583}
584
cf655043
AW
585sub statement_lines {
586 my ($stmt) = @_;
587
588 # Strip the diff line prefixes and rip blank lines at start and end.
589 $stmt =~ s/(^|\n)./$1/g;
590 $stmt =~ s/^\s*//;
591 $stmt =~ s/\s*$//;
592
593 my @stmt_lines = ($stmt =~ /\n/g);
594
595 return $#stmt_lines + 2;
596}
597
598sub statement_rawlines {
599 my ($stmt) = @_;
600
601 my @stmt_lines = ($stmt =~ /\n/g);
602
603 return $#stmt_lines + 2;
604}
605
606sub statement_block_size {
607 my ($stmt) = @_;
608
609 $stmt =~ s/(^|\n)./$1/g;
610 $stmt =~ s/^\s*{//;
611 $stmt =~ s/}\s*$//;
612 $stmt =~ s/^\s*//;
613 $stmt =~ s/\s*$//;
614
615 my @stmt_lines = ($stmt =~ /\n/g);
616 my @stmt_statements = ($stmt =~ /;/g);
617
618 my $stmt_lines = $#stmt_lines + 2;
619 my $stmt_statements = $#stmt_statements + 1;
620
621 if ($stmt_lines > $stmt_statements) {
622 return $stmt_lines;
623 } else {
624 return $stmt_statements;
625 }
626}
627
13214adf
AW
628sub ctx_statement_full {
629 my ($linenr, $remain, $off) = @_;
630 my ($statement, $condition, $level);
631
632 my (@chunks);
633
cf655043 634 # Grab the first conditional/block pair.
13214adf
AW
635 ($statement, $condition, $linenr, $remain, $off, $level) =
636 ctx_statement_block($linenr, $remain, $off);
773647a0 637 #print "F: c<$condition> s<$statement> remain<$remain>\n";
cf655043
AW
638 push(@chunks, [ $condition, $statement ]);
639 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
640 return ($level, $linenr, @chunks);
641 }
642
643 # Pull in the following conditional/block pairs and see if they
644 # could continue the statement.
13214adf 645 for (;;) {
13214adf
AW
646 ($statement, $condition, $linenr, $remain, $off, $level) =
647 ctx_statement_block($linenr, $remain, $off);
cf655043 648 #print "C: c<$condition> s<$statement> remain<$remain>\n";
773647a0 649 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
cf655043
AW
650 #print "C: push\n";
651 push(@chunks, [ $condition, $statement ]);
13214adf
AW
652 }
653
654 return ($level, $linenr, @chunks);
8905a67c
AW
655}
656
4a0df2ef 657sub ctx_block_get {
f0a594c1 658 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
4a0df2ef
AW
659 my $line;
660 my $start = $linenr - 1;
4a0df2ef
AW
661 my $blk = '';
662 my @o;
663 my @c;
664 my @res = ();
665
f0a594c1 666 my $level = 0;
4635f4fb 667 my @stack = ($level);
00df344f
AW
668 for ($line = $start; $remain > 0; $line++) {
669 next if ($rawlines[$line] =~ /^-/);
670 $remain--;
671
672 $blk .= $rawlines[$line];
4635f4fb
AW
673
674 # Handle nested #if/#else.
675 if ($rawlines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
676 push(@stack, $level);
677 } elsif ($rawlines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
678 $level = $stack[$#stack - 1];
679 } elsif ($rawlines[$line] =~ /^.\s*#\s*endif\b/) {
680 $level = pop(@stack);
681 }
682
f0a594c1
AW
683 foreach my $c (split(//, $rawlines[$line])) {
684 ##print "C<$c>L<$level><$open$close>O<$off>\n";
685 if ($off > 0) {
686 $off--;
687 next;
688 }
4a0df2ef 689
f0a594c1
AW
690 if ($c eq $close && $level > 0) {
691 $level--;
692 last if ($level == 0);
693 } elsif ($c eq $open) {
694 $level++;
695 }
696 }
4a0df2ef 697
f0a594c1 698 if (!$outer || $level <= 1) {
00df344f 699 push(@res, $rawlines[$line]);
4a0df2ef
AW
700 }
701
f0a594c1 702 last if ($level == 0);
4a0df2ef
AW
703 }
704
f0a594c1 705 return ($level, @res);
4a0df2ef
AW
706}
707sub ctx_block_outer {
708 my ($linenr, $remain) = @_;
709
f0a594c1
AW
710 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
711 return @r;
4a0df2ef
AW
712}
713sub ctx_block {
714 my ($linenr, $remain) = @_;
715
f0a594c1
AW
716 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
717 return @r;
653d4876
AW
718}
719sub ctx_statement {
f0a594c1
AW
720 my ($linenr, $remain, $off) = @_;
721
722 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
723 return @r;
724}
725sub ctx_block_level {
653d4876
AW
726 my ($linenr, $remain) = @_;
727
f0a594c1 728 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
4a0df2ef 729}
9c0ca6f9
AW
730sub ctx_statement_level {
731 my ($linenr, $remain, $off) = @_;
732
733 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
734}
4a0df2ef
AW
735
736sub ctx_locate_comment {
737 my ($first_line, $end_line) = @_;
738
739 # Catch a comment on the end of the line itself.
beae6332 740 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
4a0df2ef
AW
741 return $current_comment if (defined $current_comment);
742
743 # Look through the context and try and figure out if there is a
744 # comment.
745 my $in_comment = 0;
746 $current_comment = '';
747 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
00df344f
AW
748 my $line = $rawlines[$linenr - 1];
749 #warn " $line\n";
4a0df2ef
AW
750 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
751 $in_comment = 1;
752 }
753 if ($line =~ m@/\*@) {
754 $in_comment = 1;
755 }
756 if (!$in_comment && $current_comment ne '') {
757 $current_comment = '';
758 }
759 $current_comment .= $line . "\n" if ($in_comment);
760 if ($line =~ m@\*/@) {
761 $in_comment = 0;
762 }
763 }
764
765 chomp($current_comment);
766 return($current_comment);
767}
768sub ctx_has_comment {
769 my ($first_line, $end_line) = @_;
770 my $cmt = ctx_locate_comment($first_line, $end_line);
771
00df344f 772 ##print "LINE: $rawlines[$end_line - 1 ]\n";
4a0df2ef
AW
773 ##print "CMMT: $cmt\n";
774
775 return ($cmt ne '');
776}
777
4d001e4d
AW
778sub raw_line {
779 my ($linenr, $cnt) = @_;
780
781 my $offset = $linenr - 1;
782 $cnt++;
783
784 my $line;
785 while ($cnt) {
786 $line = $rawlines[$offset++];
787 next if (defined($line) && $line =~ /^-/);
788 $cnt--;
789 }
790
791 return $line;
792}
793
6c72ffaa
AW
794sub cat_vet {
795 my ($vet) = @_;
796 my ($res, $coded);
9c0ca6f9 797
6c72ffaa
AW
798 $res = '';
799 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
800 $res .= $1;
801 if ($2 ne '') {
802 $coded = sprintf("^%c", unpack('C', $2) + 64);
803 $res .= $coded;
9c0ca6f9
AW
804 }
805 }
6c72ffaa 806 $res =~ s/$/\$/;
9c0ca6f9 807
6c72ffaa 808 return $res;
9c0ca6f9
AW
809}
810
c2fdda0d 811my $av_preprocessor = 0;
cf655043 812my $av_pending;
c2fdda0d 813my @av_paren_type;
1f65f947 814my $av_pend_colon;
c2fdda0d
AW
815
816sub annotate_reset {
817 $av_preprocessor = 0;
cf655043
AW
818 $av_pending = '_';
819 @av_paren_type = ('E');
1f65f947 820 $av_pend_colon = 'O';
c2fdda0d
AW
821}
822
6c72ffaa
AW
823sub annotate_values {
824 my ($stream, $type) = @_;
0a920b5b 825
6c72ffaa 826 my $res;
1f65f947 827 my $var = '_' x length($stream);
6c72ffaa
AW
828 my $cur = $stream;
829
c2fdda0d 830 print "$stream\n" if ($dbg_values > 1);
6c72ffaa 831
6c72ffaa 832 while (length($cur)) {
773647a0 833 @av_paren_type = ('E') if ($#av_paren_type < 0);
cf655043 834 print " <" . join('', @av_paren_type) .
171ae1a4 835 "> <$type> <$av_pending>" if ($dbg_values > 1);
6c72ffaa 836 if ($cur =~ /^(\s+)/o) {
c2fdda0d
AW
837 print "WS($1)\n" if ($dbg_values > 1);
838 if ($1 =~ /\n/ && $av_preprocessor) {
cf655043 839 $type = pop(@av_paren_type);
c2fdda0d 840 $av_preprocessor = 0;
6c72ffaa
AW
841 }
842
8ea3eb9a 843 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
c2fdda0d 844 print "DECLARE($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
845 $type = 'T';
846
389a2fe5
AW
847 } elsif ($cur =~ /^($Modifier)\s*/) {
848 print "MODIFIER($1)\n" if ($dbg_values > 1);
849 $type = 'T';
850
c45dcabd 851 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
171ae1a4 852 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
c2fdda0d 853 $av_preprocessor = 1;
171ae1a4
AW
854 push(@av_paren_type, $type);
855 if ($2 ne '') {
856 $av_pending = 'N';
857 }
858 $type = 'E';
859
c45dcabd 860 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
171ae1a4
AW
861 print "UNDEF($1)\n" if ($dbg_values > 1);
862 $av_preprocessor = 1;
863 push(@av_paren_type, $type);
6c72ffaa 864
c45dcabd 865 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
cf655043 866 print "PRE_START($1)\n" if ($dbg_values > 1);
c2fdda0d 867 $av_preprocessor = 1;
cf655043
AW
868
869 push(@av_paren_type, $type);
870 push(@av_paren_type, $type);
171ae1a4 871 $type = 'E';
cf655043 872
c45dcabd 873 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
cf655043
AW
874 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
875 $av_preprocessor = 1;
876
877 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
878
171ae1a4 879 $type = 'E';
cf655043 880
c45dcabd 881 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
cf655043
AW
882 print "PRE_END($1)\n" if ($dbg_values > 1);
883
884 $av_preprocessor = 1;
885
886 # Assume all arms of the conditional end as this
887 # one does, and continue as if the #endif was not here.
888 pop(@av_paren_type);
889 push(@av_paren_type, $type);
171ae1a4 890 $type = 'E';
6c72ffaa
AW
891
892 } elsif ($cur =~ /^(\\\n)/o) {
c2fdda0d 893 print "PRECONT($1)\n" if ($dbg_values > 1);
6c72ffaa 894
171ae1a4
AW
895 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
896 print "ATTR($1)\n" if ($dbg_values > 1);
897 $av_pending = $type;
898 $type = 'N';
899
6c72ffaa 900 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
c2fdda0d 901 print "SIZEOF($1)\n" if ($dbg_values > 1);
6c72ffaa 902 if (defined $2) {
cf655043 903 $av_pending = 'V';
6c72ffaa
AW
904 }
905 $type = 'N';
906
14b111c1 907 } elsif ($cur =~ /^(if|while|for)\b/o) {
c2fdda0d 908 print "COND($1)\n" if ($dbg_values > 1);
14b111c1 909 $av_pending = 'E';
6c72ffaa
AW
910 $type = 'N';
911
1f65f947
AW
912 } elsif ($cur =~/^(case)/o) {
913 print "CASE($1)\n" if ($dbg_values > 1);
914 $av_pend_colon = 'C';
915 $type = 'N';
916
14b111c1 917 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
c2fdda0d 918 print "KEYWORD($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
919 $type = 'N';
920
921 } elsif ($cur =~ /^(\()/o) {
c2fdda0d 922 print "PAREN('$1')\n" if ($dbg_values > 1);
cf655043
AW
923 push(@av_paren_type, $av_pending);
924 $av_pending = '_';
6c72ffaa
AW
925 $type = 'N';
926
927 } elsif ($cur =~ /^(\))/o) {
cf655043
AW
928 my $new_type = pop(@av_paren_type);
929 if ($new_type ne '_') {
930 $type = $new_type;
c2fdda0d
AW
931 print "PAREN('$1') -> $type\n"
932 if ($dbg_values > 1);
6c72ffaa 933 } else {
c2fdda0d 934 print "PAREN('$1')\n" if ($dbg_values > 1);
6c72ffaa
AW
935 }
936
c8cb2ca3 937 } elsif ($cur =~ /^($Ident)\s*\(/o) {
c2fdda0d 938 print "FUNC($1)\n" if ($dbg_values > 1);
c8cb2ca3 939 $type = 'V';
cf655043 940 $av_pending = 'V';
6c72ffaa 941
8e761b04
AW
942 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
943 if (defined $2 && $type eq 'C' || $type eq 'T') {
1f65f947 944 $av_pend_colon = 'B';
8e761b04
AW
945 } elsif ($type eq 'E') {
946 $av_pend_colon = 'L';
1f65f947
AW
947 }
948 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
949 $type = 'V';
950
6c72ffaa 951 } elsif ($cur =~ /^($Ident|$Constant)/o) {
c2fdda0d 952 print "IDENT($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
953 $type = 'V';
954
955 } elsif ($cur =~ /^($Assignment)/o) {
c2fdda0d 956 print "ASSIGN($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
957 $type = 'N';
958
cf655043 959 } elsif ($cur =~/^(;|{|})/) {
c2fdda0d 960 print "END($1)\n" if ($dbg_values > 1);
13214adf 961 $type = 'E';
1f65f947
AW
962 $av_pend_colon = 'O';
963
8e761b04
AW
964 } elsif ($cur =~/^(,)/) {
965 print "COMMA($1)\n" if ($dbg_values > 1);
966 $type = 'C';
967
1f65f947
AW
968 } elsif ($cur =~ /^(\?)/o) {
969 print "QUESTION($1)\n" if ($dbg_values > 1);
970 $type = 'N';
971
972 } elsif ($cur =~ /^(:)/o) {
973 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
974
975 substr($var, length($res), 1, $av_pend_colon);
976 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
977 $type = 'E';
978 } else {
979 $type = 'N';
980 }
981 $av_pend_colon = 'O';
13214adf 982
8e761b04 983 } elsif ($cur =~ /^(\[)/o) {
13214adf 984 print "CLOSE($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
985 $type = 'N';
986
0d413866 987 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
74048ed8
AW
988 my $variant;
989
990 print "OPV($1)\n" if ($dbg_values > 1);
991 if ($type eq 'V') {
992 $variant = 'B';
993 } else {
994 $variant = 'U';
995 }
996
997 substr($var, length($res), 1, $variant);
998 $type = 'N';
999
6c72ffaa 1000 } elsif ($cur =~ /^($Operators)/o) {
c2fdda0d 1001 print "OP($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1002 if ($1 ne '++' && $1 ne '--') {
1003 $type = 'N';
1004 }
1005
1006 } elsif ($cur =~ /(^.)/o) {
c2fdda0d 1007 print "C($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1008 }
1009 if (defined $1) {
1010 $cur = substr($cur, length($1));
1011 $res .= $type x length($1);
1012 }
9c0ca6f9 1013 }
0a920b5b 1014
1f65f947 1015 return ($res, $var);
0a920b5b
AW
1016}
1017
8905a67c 1018sub possible {
13214adf 1019 my ($possible, $line) = @_;
9a974fdb 1020 my $notPermitted = qr{(?:
0776e594
AW
1021 ^(?:
1022 $Modifier|
1023 $Storage|
1024 $Type|
9a974fdb
AW
1025 DEFINE_\S+
1026 )$|
1027 ^(?:
0776e594
AW
1028 goto|
1029 return|
1030 case|
1031 else|
1032 asm|__asm__|
1033 do
9a974fdb 1034 )(?:\s|$)|
0776e594 1035 ^(?:typedef|struct|enum)\b
9a974fdb
AW
1036 )}x;
1037 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1038 if ($possible !~ $notPermitted) {
c45dcabd
AW
1039 # Check for modifiers.
1040 $possible =~ s/\s*$Storage\s*//g;
1041 $possible =~ s/\s*$Sparse\s*//g;
1042 if ($possible =~ /^\s*$/) {
1043
1044 } elsif ($possible =~ /\s/) {
1045 $possible =~ s/\s*$Type\s*//g;
d2506586 1046 for my $modifier (split(' ', $possible)) {
9a974fdb
AW
1047 if ($modifier !~ $notPermitted) {
1048 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1049 push(@modifierList, $modifier);
1050 }
d2506586 1051 }
c45dcabd
AW
1052
1053 } else {
1054 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1055 push(@typeList, $possible);
1056 }
8905a67c 1057 build_types();
0776e594
AW
1058 } else {
1059 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
8905a67c
AW
1060 }
1061}
1062
6c72ffaa
AW
1063my $prefix = '';
1064
f0a594c1 1065sub report {
773647a0
AW
1066 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1067 return 0;
1068 }
8905a67c
AW
1069 my $line = $prefix . $_[0];
1070
1071 $line = (split('\n', $line))[0] . "\n" if ($terse);
1072
13214adf 1073 push(our @report, $line);
773647a0
AW
1074
1075 return 1;
f0a594c1
AW
1076}
1077sub report_dump {
13214adf 1078 our @report;
f0a594c1 1079}
de7d4f0e 1080sub ERROR {
773647a0
AW
1081 if (report("ERROR: $_[0]\n")) {
1082 our $clean = 0;
1083 our $cnt_error++;
1084 }
de7d4f0e
AW
1085}
1086sub WARN {
773647a0
AW
1087 if (report("WARNING: $_[0]\n")) {
1088 our $clean = 0;
1089 our $cnt_warn++;
1090 }
de7d4f0e
AW
1091}
1092sub CHK {
773647a0 1093 if ($check && report("CHECK: $_[0]\n")) {
6c72ffaa
AW
1094 our $clean = 0;
1095 our $cnt_chk++;
1096 }
de7d4f0e
AW
1097}
1098
6ecd9674
AW
1099sub check_absolute_file {
1100 my ($absolute, $herecurr) = @_;
1101 my $file = $absolute;
1102
1103 ##print "absolute<$absolute>\n";
1104
1105 # See if any suffix of this path is a path within the tree.
1106 while ($file =~ s@^[^/]*/@@) {
1107 if (-f "$root/$file") {
1108 ##print "file<$file>\n";
1109 last;
1110 }
1111 }
1112 if (! -f _) {
1113 return 0;
1114 }
1115
1116 # It is, so see if the prefix is acceptable.
1117 my $prefix = $absolute;
1118 substr($prefix, -length($file)) = '';
1119
1120 ##print "prefix<$prefix>\n";
1121 if ($prefix ne ".../") {
1122 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1123 }
1124}
1125
0a920b5b
AW
1126sub process {
1127 my $filename = shift;
0a920b5b
AW
1128
1129 my $linenr=0;
1130 my $prevline="";
c2fdda0d 1131 my $prevrawline="";
0a920b5b 1132 my $stashline="";
c2fdda0d 1133 my $stashrawline="";
0a920b5b 1134
4a0df2ef 1135 my $length;
0a920b5b
AW
1136 my $indent;
1137 my $previndent=0;
1138 my $stashindent=0;
1139
de7d4f0e 1140 our $clean = 1;
0a920b5b
AW
1141 my $signoff = 0;
1142 my $is_patch = 0;
1143
13214adf 1144 our @report = ();
6c72ffaa
AW
1145 our $cnt_lines = 0;
1146 our $cnt_error = 0;
1147 our $cnt_warn = 0;
1148 our $cnt_chk = 0;
1149
0a920b5b
AW
1150 # Trace the real file/line as we go.
1151 my $realfile = '';
1152 my $realline = 0;
1153 my $realcnt = 0;
1154 my $here = '';
1155 my $in_comment = 0;
c2fdda0d 1156 my $comment_edge = 0;
0a920b5b 1157 my $first_line = 0;
1e855726 1158 my $p1_prefix = '';
0a920b5b 1159
13214adf
AW
1160 my $prev_values = 'E';
1161
1162 # suppression flags
773647a0 1163 my %suppress_ifbraces;
170d3a22 1164 my %suppress_whiletrailers;
2b474a1a 1165 my %suppress_export;
653d4876 1166
c2fdda0d 1167 # Pre-scan the patch sanitizing the lines.
de7d4f0e 1168 # Pre-scan the patch looking for any __setup documentation.
c2fdda0d 1169 #
de7d4f0e
AW
1170 my @setup_docs = ();
1171 my $setup_docs = 0;
773647a0
AW
1172
1173 sanitise_line_reset();
c2fdda0d
AW
1174 my $line;
1175 foreach my $rawline (@rawlines) {
773647a0
AW
1176 $linenr++;
1177 $line = $rawline;
c2fdda0d 1178
773647a0 1179 if ($rawline=~/^\+\+\+\s+(\S+)/) {
de7d4f0e
AW
1180 $setup_docs = 0;
1181 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1182 $setup_docs = 1;
1183 }
773647a0
AW
1184 #next;
1185 }
1186 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1187 $realline=$1-1;
1188 if (defined $2) {
1189 $realcnt=$3+1;
1190 } else {
1191 $realcnt=1+1;
1192 }
c45dcabd 1193 $in_comment = 0;
773647a0
AW
1194
1195 # Guestimate if this is a continuing comment. Run
1196 # the context looking for a comment "edge". If this
1197 # edge is a close comment then we must be in a comment
1198 # at context start.
1199 my $edge;
01fa9147
AW
1200 my $cnt = $realcnt;
1201 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1202 next if (defined $rawlines[$ln - 1] &&
1203 $rawlines[$ln - 1] =~ /^-/);
1204 $cnt--;
1205 #print "RAW<$rawlines[$ln - 1]>\n";
721c1cb6 1206 last if (!defined $rawlines[$ln - 1]);
fae17dae
AW
1207 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1208 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1209 ($edge) = $1;
1210 last;
1211 }
773647a0
AW
1212 }
1213 if (defined $edge && $edge eq '*/') {
1214 $in_comment = 1;
1215 }
1216
1217 # Guestimate if this is a continuing comment. If this
1218 # is the start of a diff block and this line starts
1219 # ' *' then it is very likely a comment.
1220 if (!defined $edge &&
83242e0c 1221 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
773647a0
AW
1222 {
1223 $in_comment = 1;
1224 }
1225
1226 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1227 sanitise_line_reset($in_comment);
1228
171ae1a4 1229 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
773647a0 1230 # Standardise the strings and chars within the input to
171ae1a4 1231 # simplify matching -- only bother with positive lines.
773647a0 1232 $line = sanitise_line($rawline);
de7d4f0e 1233 }
773647a0
AW
1234 push(@lines, $line);
1235
1236 if ($realcnt > 1) {
1237 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1238 } else {
1239 $realcnt = 0;
1240 }
1241
1242 #print "==>$rawline\n";
1243 #print "-->$line\n";
de7d4f0e
AW
1244
1245 if ($setup_docs && $line =~ /^\+/) {
1246 push(@setup_docs, $line);
1247 }
1248 }
1249
6c72ffaa
AW
1250 $prefix = '';
1251
773647a0
AW
1252 $realcnt = 0;
1253 $linenr = 0;
0a920b5b
AW
1254 foreach my $line (@lines) {
1255 $linenr++;
1256
c2fdda0d 1257 my $rawline = $rawlines[$linenr - 1];
6c72ffaa 1258
0a920b5b 1259#extract the line range in the file after the patch is applied
6c72ffaa 1260 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
0a920b5b 1261 $is_patch = 1;
4a0df2ef 1262 $first_line = $linenr + 1;
0a920b5b
AW
1263 $realline=$1-1;
1264 if (defined $2) {
1265 $realcnt=$3+1;
1266 } else {
1267 $realcnt=1+1;
1268 }
c2fdda0d 1269 annotate_reset();
13214adf
AW
1270 $prev_values = 'E';
1271
773647a0 1272 %suppress_ifbraces = ();
170d3a22 1273 %suppress_whiletrailers = ();
2b474a1a 1274 %suppress_export = ();
0a920b5b 1275 next;
0a920b5b 1276
4a0df2ef
AW
1277# track the line number as we move through the hunk, note that
1278# new versions of GNU diff omit the leading space on completely
1279# blank context lines so we need to count that too.
773647a0 1280 } elsif ($line =~ /^( |\+|$)/) {
0a920b5b 1281 $realline++;
d8aaf121 1282 $realcnt-- if ($realcnt != 0);
0a920b5b 1283
4a0df2ef 1284 # Measure the line length and indent.
c2fdda0d 1285 ($length, $indent) = line_stats($rawline);
0a920b5b
AW
1286
1287 # Track the previous line.
1288 ($prevline, $stashline) = ($stashline, $line);
1289 ($previndent, $stashindent) = ($stashindent, $indent);
c2fdda0d
AW
1290 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1291
773647a0 1292 #warn "line<$line>\n";
6c72ffaa 1293
d8aaf121
AW
1294 } elsif ($realcnt == 1) {
1295 $realcnt--;
0a920b5b
AW
1296 }
1297
cc77cdca
AW
1298 my $hunk_line = ($realcnt != 0);
1299
0a920b5b 1300#make up the handle for any error we report on this line
773647a0
AW
1301 $prefix = "$filename:$realline: " if ($emacs && $file);
1302 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1303
6c72ffaa
AW
1304 $here = "#$linenr: " if (!$file);
1305 $here = "#$realline: " if ($file);
773647a0
AW
1306
1307 # extract the filename as it passes
1308 if ($line=~/^\+\+\+\s+(\S+)/) {
1309 $realfile = $1;
1e855726
WS
1310 $realfile =~ s@^([^/]*)/@@;
1311
1312 $p1_prefix = $1;
e2f7aa4b
AW
1313 if (!$file && $tree && $p1_prefix ne '' &&
1314 -e "$root/$p1_prefix") {
1e855726
WS
1315 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1316 }
773647a0 1317
c1ab3326 1318 if ($realfile =~ m@^include/asm/@) {
773647a0
AW
1319 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1320 }
1321 next;
1322 }
1323
389834b6 1324 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
0a920b5b 1325
c2fdda0d
AW
1326 my $hereline = "$here\n$rawline\n";
1327 my $herecurr = "$here\n$rawline\n";
1328 my $hereprev = "$here\n$prevrawline\n$rawline\n";
0a920b5b 1329
6c72ffaa
AW
1330 $cnt_lines++ if ($realcnt != 0);
1331
0a920b5b 1332#check the patch for a signoff:
d8aaf121 1333 if ($line =~ /^\s*signed-off-by:/i) {
4a0df2ef
AW
1334 # This is a signoff, if ugly, so do not double report.
1335 $signoff++;
0a920b5b 1336 if (!($line =~ /^\s*Signed-off-by:/)) {
de7d4f0e
AW
1337 WARN("Signed-off-by: is the preferred form\n" .
1338 $herecurr);
0a920b5b
AW
1339 }
1340 if ($line =~ /^\s*signed-off-by:\S/i) {
773647a0 1341 WARN("space required after Signed-off-by:\n" .
de7d4f0e 1342 $herecurr);
0a920b5b
AW
1343 }
1344 }
1345
00df344f 1346# Check for wrappage within a valid hunk of the file
8905a67c 1347 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
de7d4f0e 1348 ERROR("patch seems to be corrupt (line wrapped?)\n" .
6c72ffaa 1349 $herecurr) if (!$emitted_corrupt++);
de7d4f0e
AW
1350 }
1351
6ecd9674
AW
1352# Check for absolute kernel paths.
1353 if ($tree) {
1354 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1355 my $file = $1;
1356
1357 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1358 check_absolute_file($1, $herecurr)) {
1359 #
1360 } else {
1361 check_absolute_file($file, $herecurr);
1362 }
1363 }
1364 }
1365
de7d4f0e
AW
1366# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1367 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
171ae1a4
AW
1368 $rawline !~ m/^$UTF8*$/) {
1369 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1370
1371 my $blank = copy_spacing($rawline);
1372 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1373 my $hereptr = "$hereline$ptr\n";
1374
1375 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
00df344f
AW
1376 }
1377
30670854
AW
1378# ignore non-hunk lines and lines being removed
1379 next if (!$hunk_line || $line =~ /^-/);
0a920b5b 1380
0a920b5b 1381#trailing whitespace
9c0ca6f9 1382 if ($line =~ /^\+.*\015/) {
c2fdda0d 1383 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
9c0ca6f9
AW
1384 ERROR("DOS line endings\n" . $herevet);
1385
c2fdda0d
AW
1386 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1387 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
de7d4f0e 1388 ERROR("trailing whitespace\n" . $herevet);
0a920b5b 1389 }
5368df20 1390
3354957a
AK
1391# check for Kconfig help text having a real description
1392 if ($realfile =~ /Kconfig/ &&
1393 $line =~ /\+?\s*(---)?help(---)?$/) {
1394 my $length = 0;
1395 for (my $l = $linenr; defined($lines[$l]); $l++) {
1396 my $f = $lines[$l];
1397 $f =~ s/#.*//;
1398 $f =~ s/^\s+//;
1399 next if ($f =~ /^$/);
1400 last if ($f =~ /^\s*config\s/);
1401 $length++;
1402 }
1403 WARN("please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($length < 4);
1404 }
1405
5368df20
AW
1406# check we are in a valid source file if not then ignore this hunk
1407 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1408
0a920b5b 1409#80 column limit
c45dcabd 1410 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
f4c014c0 1411 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
691e669b 1412 $line !~ /^\+\s*$logFunctions\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
f4c014c0 1413 $length > 80)
c45dcabd 1414 {
de7d4f0e 1415 WARN("line over 80 characters\n" . $herecurr);
0a920b5b
AW
1416 }
1417
5e79d96e
JP
1418# check for spaces before a quoted newline
1419 if ($rawline =~ /^.*\".*\s\\n/) {
1420 WARN("unnecessary whitespace before a quoted newline\n" . $herecurr);
1421 }
1422
8905a67c
AW
1423# check for adding lines without a newline.
1424 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1425 WARN("adding a line without newline at end of file\n" . $herecurr);
1426 }
1427
42e41c54
MF
1428# Blackfin: use hi/lo macros
1429 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1430 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1431 my $herevet = "$here\n" . cat_vet($line) . "\n";
1432 ERROR("use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
1433 }
1434 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1435 my $herevet = "$here\n" . cat_vet($line) . "\n";
1436 ERROR("use the HI() macro, not (... >> 16)\n" . $herevet);
1437 }
1438 }
1439
b9ea10d6
AW
1440# check we are in a valid source file C or perl if not then ignore this hunk
1441 next if ($realfile !~ /\.(h|c|pl)$/);
0a920b5b
AW
1442
1443# at the beginning of a line any tabs must come first and anything
1444# more than 8 must use tabs.
c2fdda0d
AW
1445 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1446 $rawline =~ /^\+\s* \s*/) {
1447 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
171ae1a4 1448 ERROR("code indent should use tabs where possible\n" . $herevet);
0a920b5b
AW
1449 }
1450
08e44365
AP
1451# check for space before tabs.
1452 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1453 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1454 WARN("please, no space before tabs\n" . $herevet);
1455 }
1456
b9ea10d6
AW
1457# check we are in a valid C source file if not then ignore this hunk
1458 next if ($realfile !~ /\.(h|c)$/);
1459
c2fdda0d 1460# check for RCS/CVS revision markers
cf655043 1461 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
c2fdda0d
AW
1462 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1463 }
22f2a2ef 1464
42e41c54
MF
1465# Blackfin: don't use __builtin_bfin_[cs]sync
1466 if ($line =~ /__builtin_bfin_csync/) {
1467 my $herevet = "$here\n" . cat_vet($line) . "\n";
1468 ERROR("use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
1469 }
1470 if ($line =~ /__builtin_bfin_ssync/) {
1471 my $herevet = "$here\n" . cat_vet($line) . "\n";
1472 ERROR("use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
1473 }
1474
9c0ca6f9 1475# Check for potential 'bare' types
2b474a1a
AW
1476 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1477 $realline_next);
9c9ba34e 1478 if ($realcnt && $line =~ /.\s*\S/) {
170d3a22 1479 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
f5fe35dd 1480 ctx_statement_block($linenr, $realcnt, 0);
171ae1a4
AW
1481 $stat =~ s/\n./\n /g;
1482 $cond =~ s/\n./\n /g;
1483
2b474a1a
AW
1484 # Find the real next line.
1485 $realline_next = $line_nr_next;
1486 if (defined $realline_next &&
1487 (!defined $lines[$realline_next - 1] ||
1488 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1489 $realline_next++;
1490 }
1491
171ae1a4
AW
1492 my $s = $stat;
1493 $s =~ s/{.*$//s;
cf655043 1494
c2fdda0d 1495 # Ignore goto labels.
171ae1a4 1496 if ($s =~ /$Ident:\*$/s) {
c2fdda0d
AW
1497
1498 # Ignore functions being called
171ae1a4 1499 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
c2fdda0d 1500
463f2864
AW
1501 } elsif ($s =~ /^.\s*else\b/s) {
1502
c45dcabd 1503 # declarations always start with types
d2506586 1504 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
c45dcabd
AW
1505 my $type = $1;
1506 $type =~ s/\s+/ /g;
1507 possible($type, "A:" . $s);
1508
8905a67c 1509 # definitions in global scope can only start with types
a6a84062 1510 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
c45dcabd 1511 possible($1, "B:" . $s);
c2fdda0d 1512 }
8905a67c
AW
1513
1514 # any (foo ... *) is a pointer cast, and foo is a type
65863862 1515 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
c45dcabd 1516 possible($1, "C:" . $s);
8905a67c
AW
1517 }
1518
1519 # Check for any sort of function declaration.
1520 # int foo(something bar, other baz);
1521 # void (*store_gdt)(x86_descr_ptr *);
171ae1a4 1522 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
8905a67c 1523 my ($name_len) = length($1);
8905a67c 1524
cf655043 1525 my $ctx = $s;
773647a0 1526 substr($ctx, 0, $name_len + 1, '');
8905a67c 1527 $ctx =~ s/\)[^\)]*$//;
cf655043 1528
8905a67c 1529 for my $arg (split(/\s*,\s*/, $ctx)) {
c45dcabd 1530 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
8905a67c 1531
c45dcabd 1532 possible($1, "D:" . $s);
8905a67c
AW
1533 }
1534 }
9c0ca6f9 1535 }
8905a67c 1536
9c0ca6f9
AW
1537 }
1538
653d4876
AW
1539#
1540# Checks which may be anchored in the context.
1541#
00df344f 1542
653d4876
AW
1543# Check for switch () and associated case and default
1544# statements should be at the same indent.
00df344f
AW
1545 if ($line=~/\bswitch\s*\(.*\)/) {
1546 my $err = '';
1547 my $sep = '';
1548 my @ctx = ctx_block_outer($linenr, $realcnt);
1549 shift(@ctx);
1550 for my $ctx (@ctx) {
1551 my ($clen, $cindent) = line_stats($ctx);
1552 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1553 $indent != $cindent) {
1554 $err .= "$sep$ctx\n";
1555 $sep = '';
1556 } else {
1557 $sep = "[...]\n";
1558 }
1559 }
1560 if ($err ne '') {
9c0ca6f9 1561 ERROR("switch and case should be at the same indent\n$hereline$err");
de7d4f0e
AW
1562 }
1563 }
1564
1565# if/while/etc brace do not go on next line, unless defining a do while loop,
1566# or if that brace on the next line is for something else
c45dcabd 1567 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
773647a0
AW
1568 my $pre_ctx = "$1$2";
1569
9c0ca6f9 1570 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
de7d4f0e
AW
1571 my $ctx_cnt = $realcnt - $#ctx - 1;
1572 my $ctx = join("\n", @ctx);
1573
548596d5
AW
1574 my $ctx_ln = $linenr;
1575 my $ctx_skip = $realcnt;
773647a0 1576
548596d5
AW
1577 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1578 defined $lines[$ctx_ln - 1] &&
1579 $lines[$ctx_ln - 1] =~ /^-/)) {
1580 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1581 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
de7d4f0e 1582 $ctx_ln++;
de7d4f0e 1583 }
548596d5 1584
53210168
AW
1585 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1586 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
de7d4f0e 1587
773647a0
AW
1588 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1589 ERROR("that open brace { should be on the previous line\n" .
c45dcabd 1590 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
00df344f 1591 }
773647a0
AW
1592 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1593 $ctx =~ /\)\s*\;\s*$/ &&
1594 defined $lines[$ctx_ln - 1])
1595 {
9c0ca6f9
AW
1596 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1597 if ($nindent > $indent) {
773647a0 1598 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
c45dcabd 1599 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
9c0ca6f9
AW
1600 }
1601 }
00df344f
AW
1602 }
1603
4d001e4d
AW
1604# Check relative indent for conditionals and blocks.
1605 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1606 my ($s, $c) = ($stat, $cond);
1607
1608 substr($s, 0, length($c), '');
1609
1610 # Make sure we remove the line prefixes as we have
1611 # none on the first line, and are going to readd them
1612 # where necessary.
1613 $s =~ s/\n./\n/gs;
1614
1615 # Find out how long the conditional actually is.
6f779c18
AW
1616 my @newlines = ($c =~ /\n/gs);
1617 my $cond_lines = 1 + $#newlines;
4d001e4d
AW
1618
1619 # We want to check the first line inside the block
1620 # starting at the end of the conditional, so remove:
1621 # 1) any blank line termination
1622 # 2) any opening brace { on end of the line
1623 # 3) any do (...) {
1624 my $continuation = 0;
1625 my $check = 0;
1626 $s =~ s/^.*\bdo\b//;
1627 $s =~ s/^\s*{//;
1628 if ($s =~ s/^\s*\\//) {
1629 $continuation = 1;
1630 }
9bd49efe 1631 if ($s =~ s/^\s*?\n//) {
4d001e4d
AW
1632 $check = 1;
1633 $cond_lines++;
1634 }
1635
1636 # Also ignore a loop construct at the end of a
1637 # preprocessor statement.
1638 if (($prevline =~ /^.\s*#\s*define\s/ ||
1639 $prevline =~ /\\\s*$/) && $continuation == 0) {
1640 $check = 0;
1641 }
1642
9bd49efe 1643 my $cond_ptr = -1;
740504c6 1644 $continuation = 0;
9bd49efe
AW
1645 while ($cond_ptr != $cond_lines) {
1646 $cond_ptr = $cond_lines;
1647
f16fa28f
AW
1648 # If we see an #else/#elif then the code
1649 # is not linear.
1650 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1651 $check = 0;
1652 }
1653
9bd49efe
AW
1654 # Ignore:
1655 # 1) blank lines, they should be at 0,
1656 # 2) preprocessor lines, and
1657 # 3) labels.
740504c6
AW
1658 if ($continuation ||
1659 $s =~ /^\s*?\n/ ||
9bd49efe
AW
1660 $s =~ /^\s*#\s*?/ ||
1661 $s =~ /^\s*$Ident\s*:/) {
740504c6 1662 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
30dad6eb
AW
1663 if ($s =~ s/^.*?\n//) {
1664 $cond_lines++;
1665 }
9bd49efe 1666 }
4d001e4d
AW
1667 }
1668
1669 my (undef, $sindent) = line_stats("+" . $s);
1670 my $stat_real = raw_line($linenr, $cond_lines);
1671
1672 # Check if either of these lines are modified, else
1673 # this is not this patch's fault.
1674 if (!defined($stat_real) ||
1675 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1676 $check = 0;
1677 }
1678 if (defined($stat_real) && $cond_lines > 1) {
1679 $stat_real = "[...]\n$stat_real";
1680 }
1681
9bd49efe 1682 #print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
4d001e4d
AW
1683
1684 if ($check && (($sindent % 8) != 0 ||
1685 ($sindent <= $indent && $s ne ''))) {
1686 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1687 }
1688 }
1689
6c72ffaa
AW
1690 # Track the 'values' across context and added lines.
1691 my $opline = $line; $opline =~ s/^./ /;
1f65f947
AW
1692 my ($curr_values, $curr_vars) =
1693 annotate_values($opline . "\n", $prev_values);
6c72ffaa 1694 $curr_values = $prev_values . $curr_values;
c2fdda0d
AW
1695 if ($dbg_values) {
1696 my $outline = $opline; $outline =~ s/\t/ /g;
cf655043
AW
1697 print "$linenr > .$outline\n";
1698 print "$linenr > $curr_values\n";
1f65f947 1699 print "$linenr > $curr_vars\n";
c2fdda0d 1700 }
6c72ffaa
AW
1701 $prev_values = substr($curr_values, -1);
1702
00df344f
AW
1703#ignore lines not being added
1704 if ($line=~/^[^\+]/) {next;}
1705
653d4876 1706# TEST: allow direct testing of the type matcher.
7429c690
AW
1707 if ($dbg_type) {
1708 if ($line =~ /^.\s*$Declare\s*$/) {
1709 ERROR("TEST: is type\n" . $herecurr);
1710 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1711 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1712 }
653d4876
AW
1713 next;
1714 }
a1ef277e
AW
1715# TEST: allow direct testing of the attribute matcher.
1716 if ($dbg_attr) {
9360b0e5 1717 if ($line =~ /^.\s*$Modifier\s*$/) {
a1ef277e 1718 ERROR("TEST: is attr\n" . $herecurr);
9360b0e5 1719 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
a1ef277e
AW
1720 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1721 }
1722 next;
1723 }
653d4876 1724
f0a594c1 1725# check for initialisation to aggregates open brace on the next line
99423c20
AW
1726 if ($line =~ /^.\s*{/ &&
1727 $prevline =~ /(?:^|[^=])=\s*$/) {
773647a0 1728 ERROR("that open brace { should be on the previous line\n" . $hereprev);
f0a594c1
AW
1729 }
1730
653d4876
AW
1731#
1732# Checks which are anchored on the added line.
1733#
1734
1735# check for malformed paths in #include statements (uses RAW line)
c45dcabd 1736 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
653d4876
AW
1737 my $path = $1;
1738 if ($path =~ m{//}) {
de7d4f0e
AW
1739 ERROR("malformed #include filename\n" .
1740 $herecurr);
653d4876 1741 }
653d4876 1742 }
00df344f 1743
0a920b5b 1744# no C99 // comments
00df344f 1745 if ($line =~ m{//}) {
de7d4f0e 1746 ERROR("do not use C99 // comments\n" . $herecurr);
0a920b5b 1747 }
00df344f 1748 # Remove C99 comments.
0a920b5b 1749 $line =~ s@//.*@@;
6c72ffaa 1750 $opline =~ s@//.*@@;
0a920b5b 1751
2b474a1a
AW
1752# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
1753# the whole statement.
1754#print "APW <$lines[$realline_next - 1]>\n";
1755 if (defined $realline_next &&
1756 exists $lines[$realline_next - 1] &&
1757 !defined $suppress_export{$realline_next} &&
1758 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1759 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
653d4876 1760 my $name = $1;
2b474a1a
AW
1761 if ($stat !~ /(?:
1762 \n.}\s*$|
48012058
AW
1763 ^.DEFINE_$Ident\(\Q$name\E\)|
1764 ^.DECLARE_$Ident\(\Q$name\E\)|
1765 ^.LIST_HEAD\(\Q$name\E\)|
2b474a1a
AW
1766 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1767 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
48012058 1768 )/x) {
2b474a1a
AW
1769#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
1770 $suppress_export{$realline_next} = 2;
1771 } else {
1772 $suppress_export{$realline_next} = 1;
0a920b5b
AW
1773 }
1774 }
2b474a1a
AW
1775 if (!defined $suppress_export{$linenr} &&
1776 $prevline =~ /^.\s*$/ &&
1777 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
1778 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1779#print "FOO B <$lines[$linenr - 1]>\n";
1780 $suppress_export{$linenr} = 2;
1781 }
1782 if (defined $suppress_export{$linenr} &&
1783 $suppress_export{$linenr} == 2) {
1784 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1785 }
0a920b5b 1786
f0a594c1 1787# check for external initialisers.
c45dcabd 1788 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
f0a594c1
AW
1789 ERROR("do not initialise externals to 0 or NULL\n" .
1790 $herecurr);
1791 }
653d4876 1792# check for static initialisers.
2d1bafd7 1793 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
de7d4f0e
AW
1794 ERROR("do not initialise statics to 0 or NULL\n" .
1795 $herecurr);
0a920b5b
AW
1796 }
1797
653d4876
AW
1798# check for new typedefs, only function parameters and sparse annotations
1799# make sense.
1800 if ($line =~ /\btypedef\s/ &&
8054576d 1801 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
c45dcabd 1802 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
8ed22cad 1803 $line !~ /\b$typeTypedefs\b/ &&
653d4876 1804 $line !~ /\b__bitwise(?:__|)\b/) {
de7d4f0e 1805 WARN("do not add new typedefs\n" . $herecurr);
0a920b5b
AW
1806 }
1807
1808# * goes on variable not on type
65863862 1809 # (char*[ const])
00ef4ece 1810 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
65863862
AW
1811 my ($from, $to) = ($1, $1);
1812
1813 # Should start with a space.
1814 $to =~ s/^(\S)/ $1/;
1815 # Should not end with a space.
1816 $to =~ s/\s+$//;
1817 # '*'s should not have spaces between.
f9a0b3d1 1818 while ($to =~ s/\*\s+\*/\*\*/) {
65863862 1819 }
d8aaf121 1820
65863862
AW
1821 #print "from<$from> to<$to>\n";
1822 if ($from ne $to) {
1823 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1824 }
00ef4ece 1825 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
65863862
AW
1826 my ($from, $to, $ident) = ($1, $1, $2);
1827
1828 # Should start with a space.
1829 $to =~ s/^(\S)/ $1/;
1830 # Should not end with a space.
1831 $to =~ s/\s+$//;
1832 # '*'s should not have spaces between.
f9a0b3d1 1833 while ($to =~ s/\*\s+\*/\*\*/) {
65863862
AW
1834 }
1835 # Modifiers should have spaces.
1836 $to =~ s/(\b$Modifier$)/$1 /;
d8aaf121 1837
667026e7
AW
1838 #print "from<$from> to<$to> ident<$ident>\n";
1839 if ($from ne $to && $ident !~ /^$Modifier$/) {
65863862
AW
1840 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1841 }
0a920b5b
AW
1842 }
1843
1844# # no BUG() or BUG_ON()
1845# if ($line =~ /\b(BUG|BUG_ON)\b/) {
1846# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1847# print "$herecurr";
1848# $clean = 0;
1849# }
1850
8905a67c 1851 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
c2fdda0d 1852 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
8905a67c
AW
1853 }
1854
00df344f
AW
1855# printk should use KERN_* levels. Note that follow on printk's on the
1856# same line do not need a level, so we use the current block context
1857# to try and find and validate the current printk. In summary the current
1858# printk includes all preceeding printk's which have no newline on the end.
1859# we assume the first bad printk is the one to report.
f0a594c1 1860 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
00df344f
AW
1861 my $ok = 0;
1862 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1863 #print "CHECK<$lines[$ln - 1]\n";
1864 # we have a preceeding printk if it ends
1865 # with "\n" ignore it, else it is to blame
1866 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1867 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1868 $ok = 1;
1869 }
1870 last;
1871 }
1872 }
1873 if ($ok == 0) {
de7d4f0e 1874 WARN("printk() should include KERN_ facility level\n" . $herecurr);
00df344f 1875 }
0a920b5b
AW
1876 }
1877
653d4876
AW
1878# function brace can't be on same line, except for #defines of do while,
1879# or if closed on same line
c45dcabd
AW
1880 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1881 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
de7d4f0e 1882 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
0a920b5b 1883 }
653d4876 1884
8905a67c
AW
1885# open braces for enum, union and struct go on the same line.
1886 if ($line =~ /^.\s*{/ &&
1887 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1888 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1889 }
1890
8d31cfce
AW
1891# check for spacing round square brackets; allowed:
1892# 1. with a type on the left -- int [] a;
fe2a7dbc
AW
1893# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1894# 3. inside a curly brace -- = { [0...10] = 5 }
8d31cfce
AW
1895 while ($line =~ /(.*?\s)\[/g) {
1896 my ($where, $prefix) = ($-[1], $1);
1897 if ($prefix !~ /$Type\s+$/ &&
fe2a7dbc
AW
1898 ($where != 0 || $prefix !~ /^.\s+$/) &&
1899 $prefix !~ /{\s+$/) {
8d31cfce
AW
1900 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1901 }
1902 }
1903
f0a594c1 1904# check for spaces between functions and their parentheses.
6c72ffaa 1905 while ($line =~ /($Ident)\s+\(/g) {
c2fdda0d 1906 my $name = $1;
773647a0
AW
1907 my $ctx_before = substr($line, 0, $-[1]);
1908 my $ctx = "$ctx_before$name";
c2fdda0d
AW
1909
1910 # Ignore those directives where spaces _are_ permitted.
773647a0
AW
1911 if ($name =~ /^(?:
1912 if|for|while|switch|return|case|
1913 volatile|__volatile__|
1914 __attribute__|format|__extension__|
1915 asm|__asm__)$/x)
1916 {
c2fdda0d
AW
1917
1918 # cpp #define statements have non-optional spaces, ie
1919 # if there is a space between the name and the open
1920 # parenthesis it is simply not a parameter group.
c45dcabd 1921 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
773647a0
AW
1922
1923 # cpp #elif statement condition may start with a (
c45dcabd 1924 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
c2fdda0d
AW
1925
1926 # If this whole things ends with a type its most
1927 # likely a typedef for a function.
773647a0 1928 } elsif ($ctx =~ /$Type$/) {
c2fdda0d
AW
1929
1930 } else {
773647a0 1931 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
6c72ffaa 1932 }
f0a594c1 1933 }
653d4876 1934# Check operator spacing.
0a920b5b 1935 if (!($line=~/\#\s*include/)) {
9c0ca6f9
AW
1936 my $ops = qr{
1937 <<=|>>=|<=|>=|==|!=|
1938 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1939 =>|->|<<|>>|<|>|=|!|~|
1f65f947
AW
1940 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1941 \?|:
9c0ca6f9 1942 }x;
cf655043 1943 my @elements = split(/($ops|;)/, $opline);
00df344f 1944 my $off = 0;
6c72ffaa
AW
1945
1946 my $blank = copy_spacing($opline);
1947
0a920b5b 1948 for (my $n = 0; $n < $#elements; $n += 2) {
4a0df2ef
AW
1949 $off += length($elements[$n]);
1950
773647a0
AW
1951 # Pick up the preceeding and succeeding characters.
1952 my $ca = substr($opline, 0, $off);
1953 my $cc = '';
1954 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1955 $cc = substr($opline, $off + length($elements[$n + 1]));
1956 }
1957 my $cb = "$ca$;$cc";
1958
4a0df2ef
AW
1959 my $a = '';
1960 $a = 'V' if ($elements[$n] ne '');
1961 $a = 'W' if ($elements[$n] =~ /\s$/);
cf655043 1962 $a = 'C' if ($elements[$n] =~ /$;$/);
4a0df2ef
AW
1963 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1964 $a = 'O' if ($elements[$n] eq '');
773647a0 1965 $a = 'E' if ($ca =~ /^\s*$/);
4a0df2ef 1966
0a920b5b 1967 my $op = $elements[$n + 1];
4a0df2ef
AW
1968
1969 my $c = '';
0a920b5b 1970 if (defined $elements[$n + 2]) {
4a0df2ef
AW
1971 $c = 'V' if ($elements[$n + 2] ne '');
1972 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
cf655043 1973 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4a0df2ef
AW
1974 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1975 $c = 'O' if ($elements[$n + 2] eq '');
8b1b3378 1976 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4a0df2ef
AW
1977 } else {
1978 $c = 'E';
0a920b5b
AW
1979 }
1980
4a0df2ef
AW
1981 my $ctx = "${a}x${c}";
1982
1983 my $at = "(ctx:$ctx)";
1984
6c72ffaa 1985 my $ptr = substr($blank, 0, $off) . "^";
de7d4f0e 1986 my $hereptr = "$hereline$ptr\n";
0a920b5b 1987
74048ed8 1988 # Pull out the value of this operator.
6c72ffaa 1989 my $op_type = substr($curr_values, $off + 1, 1);
0a920b5b 1990
1f65f947
AW
1991 # Get the full operator variant.
1992 my $opv = $op . substr($curr_vars, $off, 1);
1993
13214adf
AW
1994 # Ignore operators passed as parameters.
1995 if ($op_type ne 'V' &&
1996 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1997
cf655043
AW
1998# # Ignore comments
1999# } elsif ($op =~ /^$;+$/) {
13214adf 2000
d8aaf121 2001 # ; should have either the end of line or a space or \ after it
13214adf 2002 } elsif ($op eq ';') {
cf655043
AW
2003 if ($ctx !~ /.x[WEBC]/ &&
2004 $cc !~ /^\\/ && $cc !~ /^;/) {
773647a0 2005 ERROR("space required after that '$op' $at\n" . $hereptr);
d8aaf121
AW
2006 }
2007
2008 # // is a comment
2009 } elsif ($op eq '//') {
0a920b5b 2010
1f65f947
AW
2011 # No spaces for:
2012 # ->
2013 # : when part of a bitfield
2014 } elsif ($op eq '->' || $opv eq ':B') {
4a0df2ef 2015 if ($ctx =~ /Wx.|.xW/) {
773647a0 2016 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
0a920b5b
AW
2017 }
2018
2019 # , must have a space on the right.
2020 } elsif ($op eq ',') {
cf655043 2021 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
773647a0 2022 ERROR("space required after that '$op' $at\n" . $hereptr);
0a920b5b
AW
2023 }
2024
9c0ca6f9 2025 # '*' as part of a type definition -- reported already.
74048ed8 2026 } elsif ($opv eq '*_') {
9c0ca6f9
AW
2027 #warn "'*' is part of type\n";
2028
2029 # unary operators should have a space before and
2030 # none after. May be left adjacent to another
2031 # unary operator, or a cast
2032 } elsif ($op eq '!' || $op eq '~' ||
74048ed8 2033 $opv eq '*U' || $opv eq '-U' ||
0d413866 2034 $opv eq '&U' || $opv eq '&&U') {
cf655043 2035 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
773647a0 2036 ERROR("space required before that '$op' $at\n" . $hereptr);
0a920b5b 2037 }
a3340b35 2038 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
171ae1a4
AW
2039 # A unary '*' may be const
2040
2041 } elsif ($ctx =~ /.xW/) {
fb9e9096 2042 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
0a920b5b
AW
2043 }
2044
2045 # unary ++ and unary -- are allowed no space on one side.
2046 } elsif ($op eq '++' or $op eq '--') {
773647a0
AW
2047 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2048 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2049 }
2050 if ($ctx =~ /Wx[BE]/ ||
2051 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2052 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
0a920b5b 2053 }
773647a0
AW
2054 if ($ctx =~ /ExW/) {
2055 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
653d4876 2056 }
0a920b5b 2057
773647a0 2058
0a920b5b 2059 # << and >> may either have or not have spaces both sides
9c0ca6f9
AW
2060 } elsif ($op eq '<<' or $op eq '>>' or
2061 $op eq '&' or $op eq '^' or $op eq '|' or
2062 $op eq '+' or $op eq '-' or
c2fdda0d
AW
2063 $op eq '*' or $op eq '/' or
2064 $op eq '%')
0a920b5b 2065 {
773647a0 2066 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
de7d4f0e
AW
2067 ERROR("need consistent spacing around '$op' $at\n" .
2068 $hereptr);
0a920b5b
AW
2069 }
2070
1f65f947
AW
2071 # A colon needs no spaces before when it is
2072 # terminating a case value or a label.
2073 } elsif ($opv eq ':C' || $opv eq ':L') {
2074 if ($ctx =~ /Wx./) {
2075 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2076 }
2077
0a920b5b 2078 # All the others need spaces both sides.
cf655043 2079 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1f65f947
AW
2080 my $ok = 0;
2081
22f2a2ef 2082 # Ignore email addresses <foo@bar>
1f65f947
AW
2083 if (($op eq '<' &&
2084 $cc =~ /^\S+\@\S+>/) ||
2085 ($op eq '>' &&
2086 $ca =~ /<\S+\@\S+$/))
2087 {
2088 $ok = 1;
2089 }
2090
2091 # Ignore ?:
2092 if (($opv eq ':O' && $ca =~ /\?$/) ||
2093 ($op eq '?' && $cc =~ /^:/)) {
2094 $ok = 1;
2095 }
2096
2097 if ($ok == 0) {
773647a0 2098 ERROR("spaces required around that '$op' $at\n" . $hereptr);
22f2a2ef 2099 }
0a920b5b 2100 }
4a0df2ef 2101 $off += length($elements[$n + 1]);
0a920b5b
AW
2102 }
2103 }
2104
f0a594c1
AW
2105# check for multiple assignments
2106 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
6c72ffaa 2107 CHK("multiple assignments should be avoided\n" . $herecurr);
f0a594c1
AW
2108 }
2109
22f2a2ef
AW
2110## # check for multiple declarations, allowing for a function declaration
2111## # continuation.
2112## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2113## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2114##
2115## # Remove any bracketed sections to ensure we do not
2116## # falsly report the parameters of functions.
2117## my $ln = $line;
2118## while ($ln =~ s/\([^\(\)]*\)//g) {
2119## }
2120## if ($ln =~ /,/) {
2121## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
2122## }
2123## }
f0a594c1 2124
0a920b5b 2125#need space before brace following if, while, etc
22f2a2ef
AW
2126 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2127 $line =~ /do{/) {
773647a0 2128 ERROR("space required before the open brace '{'\n" . $herecurr);
de7d4f0e
AW
2129 }
2130
2131# closing brace should have a space following it when it has anything
2132# on the line
2133 if ($line =~ /}(?!(?:,|;|\)))\S/) {
773647a0 2134 ERROR("space required after that close brace '}'\n" . $herecurr);
0a920b5b
AW
2135 }
2136
22f2a2ef
AW
2137# check spacing on square brackets
2138 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
773647a0 2139 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
22f2a2ef
AW
2140 }
2141 if ($line =~ /\s\]/) {
773647a0 2142 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
22f2a2ef
AW
2143 }
2144
c45dcabd 2145# check spacing on parentheses
9c0ca6f9
AW
2146 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2147 $line !~ /for\s*\(\s+;/) {
773647a0 2148 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
22f2a2ef 2149 }
13214adf 2150 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
c45dcabd
AW
2151 $line !~ /for\s*\(.*;\s+\)/ &&
2152 $line !~ /:\s+\)/) {
773647a0 2153 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
22f2a2ef
AW
2154 }
2155
0a920b5b 2156#goto labels aren't indented, allow a single space however
4a0df2ef 2157 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
0a920b5b 2158 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
de7d4f0e 2159 WARN("labels should not be indented\n" . $herecurr);
0a920b5b
AW
2160 }
2161
c45dcabd
AW
2162# Return is not a function.
2163 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2164 my $spacing = $1;
2165 my $value = $2;
2166
86f9d059 2167 # Flatten any parentheses
fee61c47 2168 $value =~ s/\)\(/\) \(/g;
63f17f89
AW
2169 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2170 $value !~ /(?:$Ident|-?$Constant)\s*
2171 $Compare\s*
2172 (?:$Ident|-?$Constant)/x &&
2173 $value =~ s/\([^\(\)]*\)/1/) {
c45dcabd
AW
2174 }
2175
2176 if ($value =~ /^(?:$Ident|-?$Constant)$/) {
2177 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2178
2179 } elsif ($spacing !~ /\s+/) {
2180 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2181 }
2182 }
2183
0a920b5b 2184# Need a space before open parenthesis after if, while etc
4a0df2ef 2185 if ($line=~/\b(if|while|for|switch)\(/) {
773647a0 2186 ERROR("space required before the open parenthesis '('\n" . $herecurr);
0a920b5b
AW
2187 }
2188
f5fe35dd
AW
2189# Check for illegal assignment in if conditional -- and check for trailing
2190# statements after the conditional.
170d3a22
AW
2191 if ($line =~ /do\s*(?!{)/) {
2192 my ($stat_next) = ctx_statement_block($line_nr_next,
2193 $remain_next, $off_next);
2194 $stat_next =~ s/\n./\n /g;
2195 ##print "stat<$stat> stat_next<$stat_next>\n";
2196
2197 if ($stat_next =~ /^\s*while\b/) {
2198 # If the statement carries leading newlines,
2199 # then count those as offsets.
2200 my ($whitespace) =
2201 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2202 my $offset =
2203 statement_rawlines($whitespace) - 1;
2204
2205 $suppress_whiletrailers{$line_nr_next +
2206 $offset} = 1;
2207 }
2208 }
2209 if (!defined $suppress_whiletrailers{$linenr} &&
2210 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
171ae1a4 2211 my ($s, $c) = ($stat, $cond);
8905a67c 2212
b53c8e10 2213 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
c2fdda0d 2214 ERROR("do not use assignment in if condition\n" . $herecurr);
8905a67c
AW
2215 }
2216
2217 # Find out what is on the end of the line after the
2218 # conditional.
773647a0 2219 substr($s, 0, length($c), '');
8905a67c 2220 $s =~ s/\n.*//g;
13214adf 2221 $s =~ s/$;//g; # Remove any comments
53210168
AW
2222 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2223 $c !~ /}\s*while\s*/)
773647a0 2224 {
bb44ad39
AW
2225 # Find out how long the conditional actually is.
2226 my @newlines = ($c =~ /\n/gs);
2227 my $cond_lines = 1 + $#newlines;
42bdf74c 2228 my $stat_real = '';
bb44ad39 2229
42bdf74c
HS
2230 $stat_real = raw_line($linenr, $cond_lines)
2231 . "\n" if ($cond_lines);
bb44ad39
AW
2232 if (defined($stat_real) && $cond_lines > 1) {
2233 $stat_real = "[...]\n$stat_real";
2234 }
2235
2236 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
8905a67c
AW
2237 }
2238 }
2239
13214adf
AW
2240# Check for bitwise tests written as boolean
2241 if ($line =~ /
2242 (?:
2243 (?:\[|\(|\&\&|\|\|)
2244 \s*0[xX][0-9]+\s*
2245 (?:\&\&|\|\|)
2246 |
2247 (?:\&\&|\|\|)
2248 \s*0[xX][0-9]+\s*
2249 (?:\&\&|\|\||\)|\])
2250 )/x)
2251 {
2252 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2253 }
2254
8905a67c 2255# if and else should not have general statements after it
13214adf
AW
2256 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2257 my $s = $1;
2258 $s =~ s/$;//g; # Remove any comments
2259 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2260 ERROR("trailing statements should be on next line\n" . $herecurr);
2261 }
0a920b5b 2262 }
39667782
AW
2263# if should not continue a brace
2264 if ($line =~ /}\s*if\b/) {
2265 ERROR("trailing statements should be on next line\n" .
2266 $herecurr);
2267 }
a1080bf8
AW
2268# case and default should not have general statements after them
2269 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2270 $line !~ /\G(?:
3fef12d6 2271 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
a1080bf8
AW
2272 \s*return\s+
2273 )/xg)
2274 {
2275 ERROR("trailing statements should be on next line\n" . $herecurr);
2276 }
0a920b5b
AW
2277
2278 # Check for }<nl>else {, these must be at the same
2279 # indent level to be relevant to each other.
2280 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2281 $previndent == $indent) {
de7d4f0e 2282 ERROR("else should follow close brace '}'\n" . $hereprev);
0a920b5b
AW
2283 }
2284
c2fdda0d
AW
2285 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2286 $previndent == $indent) {
2287 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2288
2289 # Find out what is on the end of the line after the
2290 # conditional.
773647a0 2291 substr($s, 0, length($c), '');
c2fdda0d
AW
2292 $s =~ s/\n.*//g;
2293
2294 if ($s =~ /^\s*;/) {
2295 ERROR("while should follow close brace '}'\n" . $hereprev);
2296 }
2297 }
2298
0a920b5b
AW
2299#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2300# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2301# print "No studly caps, use _\n";
2302# print "$herecurr";
2303# $clean = 0;
2304# }
2305
2306#no spaces allowed after \ in define
c45dcabd 2307 if ($line=~/\#\s*define.*\\\s$/) {
de7d4f0e 2308 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
0a920b5b
AW
2309 }
2310
653d4876 2311#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
c45dcabd 2312 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
e09dec48
AW
2313 my $file = "$1.h";
2314 my $checkfile = "include/linux/$file";
2315 if (-f "$root/$checkfile" &&
2316 $realfile ne $checkfile &&
7840a94c 2317 $1 !~ /$allowed_asm_includes/)
c45dcabd 2318 {
e09dec48
AW
2319 if ($realfile =~ m{^arch/}) {
2320 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2321 } else {
2322 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2323 }
0a920b5b
AW
2324 }
2325 }
2326
653d4876
AW
2327# multi-statement macros should be enclosed in a do while loop, grab the
2328# first statement and ensure its the whole macro if its not enclosed
cf655043 2329# in a known good container
b8f96a31
AW
2330 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2331 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
d8aaf121
AW
2332 my $ln = $linenr;
2333 my $cnt = $realcnt;
c45dcabd
AW
2334 my ($off, $dstat, $dcond, $rest);
2335 my $ctx = '';
653d4876 2336
c45dcabd
AW
2337 my $args = defined($1);
2338
2339 # Find the end of the macro and limit our statement
2340 # search to that.
2341 while ($cnt > 0 && defined $lines[$ln - 1] &&
2342 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2343 {
2344 $ctx .= $rawlines[$ln - 1] . "\n";
a3bb97a7 2345 $cnt-- if ($lines[$ln - 1] !~ /^-/);
c45dcabd 2346 $ln++;
c45dcabd
AW
2347 }
2348 $ctx .= $rawlines[$ln - 1];
2349
2350 ($dstat, $dcond, $ln, $cnt, $off) =
2351 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2352 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
a3bb97a7 2353 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
c45dcabd
AW
2354
2355 # Extract the remainder of the define (if any) and
2356 # rip off surrounding spaces, and trailing \'s.
2357 $rest = '';
636d140a
AW
2358 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2359 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
a3bb97a7
AW
2360 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2361 $rest .= substr($lines[$ln - 1], $off) . "\n";
2362 $cnt--;
2363 }
c45dcabd 2364 $ln++;
c45dcabd
AW
2365 $off = 0;
2366 }
2367 $rest =~ s/\\\n.//g;
2368 $rest =~ s/^\s*//s;
2369 $rest =~ s/\s*$//s;
2370
2371 # Clean up the original statement.
2372 if ($args) {
2373 substr($dstat, 0, length($dcond), '');
2374 } else {
2375 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
d8aaf121 2376 }
292f1a9b 2377 $dstat =~ s/$;//g;
c45dcabd
AW
2378 $dstat =~ s/\\\n.//g;
2379 $dstat =~ s/^\s*//s;
2380 $dstat =~ s/\s*$//s;
de7d4f0e 2381
c45dcabd 2382 # Flatten any parentheses and braces
bf30d6ed
AW
2383 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2384 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2385 $dstat =~ s/\[[^\{\}]*\]/1/)
2386 {
de7d4f0e 2387 }
d8aaf121 2388
c45dcabd
AW
2389 my $exceptions = qr{
2390 $Declare|
2391 module_param_named|
2392 MODULE_PARAM_DESC|
2393 DECLARE_PER_CPU|
2394 DEFINE_PER_CPU|
383099fd 2395 __typeof__\(|
22fd2d3e
SS
2396 union|
2397 struct|
ea71a0a0
AW
2398 \.$Ident\s*=\s*|
2399 ^\"|\"$
c45dcabd 2400 }x;
383099fd 2401 #print "REST<$rest> dstat<$dstat>\n";
c45dcabd
AW
2402 if ($rest ne '') {
2403 if ($rest !~ /while\s*\(/ &&
2404 $dstat !~ /$exceptions/)
2405 {
de7d4f0e 2406 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
c45dcabd
AW
2407 }
2408
2409 } elsif ($ctx !~ /;/) {
2410 if ($dstat ne '' &&
2411 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2412 $dstat !~ /$exceptions/ &&
b132e5d5 2413 $dstat !~ /^\.$Ident\s*=/ &&
c45dcabd
AW
2414 $dstat =~ /$Operators/)
2415 {
de7d4f0e 2416 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
d8aaf121 2417 }
653d4876 2418 }
0a920b5b
AW
2419 }
2420
080ba929
MF
2421# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2422# all assignments may have only one of the following with an assignment:
2423# .
2424# ALIGN(...)
2425# VMLINUX_SYMBOL(...)
2426 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2427 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2428 }
2429
f0a594c1 2430# check for redundant bracing round if etc
13214adf
AW
2431 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2432 my ($level, $endln, @chunks) =
cf655043 2433 ctx_statement_full($linenr, $realcnt, 1);
13214adf 2434 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
cf655043
AW
2435 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2436 if ($#chunks > 0 && $level == 0) {
13214adf
AW
2437 my $allowed = 0;
2438 my $seen = 0;
773647a0 2439 my $herectx = $here . "\n";
cf655043 2440 my $ln = $linenr - 1;
13214adf
AW
2441 for my $chunk (@chunks) {
2442 my ($cond, $block) = @{$chunk};
2443
773647a0
AW
2444 # If the condition carries leading newlines, then count those as offsets.
2445 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2446 my $offset = statement_rawlines($whitespace) - 1;
2447
2448 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2449
2450 # We have looked at and allowed this specific line.
2451 $suppress_ifbraces{$ln + $offset} = 1;
2452
2453 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
cf655043
AW
2454 $ln += statement_rawlines($block) - 1;
2455
773647a0 2456 substr($block, 0, length($cond), '');
13214adf
AW
2457
2458 $seen++ if ($block =~ /^\s*{/);
2459
cf655043
AW
2460 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2461 if (statement_lines($cond) > 1) {
2462 #print "APW: ALLOWED: cond<$cond>\n";
13214adf
AW
2463 $allowed = 1;
2464 }
2465 if ($block =~/\b(?:if|for|while)\b/) {
cf655043 2466 #print "APW: ALLOWED: block<$block>\n";
13214adf
AW
2467 $allowed = 1;
2468 }
cf655043
AW
2469 if (statement_block_size($block) > 1) {
2470 #print "APW: ALLOWED: lines block<$block>\n";
13214adf
AW
2471 $allowed = 1;
2472 }
2473 }
2474 if ($seen && !$allowed) {
cf655043 2475 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
13214adf
AW
2476 }
2477 }
2478 }
773647a0 2479 if (!defined $suppress_ifbraces{$linenr - 1} &&
13214adf 2480 $line =~ /\b(if|while|for|else)\b/) {
cf655043
AW
2481 my $allowed = 0;
2482
2483 # Check the pre-context.
2484 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2485 #print "APW: ALLOWED: pre<$1>\n";
2486 $allowed = 1;
2487 }
773647a0
AW
2488
2489 my ($level, $endln, @chunks) =
2490 ctx_statement_full($linenr, $realcnt, $-[0]);
2491
cf655043
AW
2492 # Check the condition.
2493 my ($cond, $block) = @{$chunks[0]};
773647a0 2494 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
cf655043 2495 if (defined $cond) {
773647a0 2496 substr($block, 0, length($cond), '');
cf655043
AW
2497 }
2498 if (statement_lines($cond) > 1) {
2499 #print "APW: ALLOWED: cond<$cond>\n";
2500 $allowed = 1;
2501 }
2502 if ($block =~/\b(?:if|for|while)\b/) {
2503 #print "APW: ALLOWED: block<$block>\n";
2504 $allowed = 1;
2505 }
2506 if (statement_block_size($block) > 1) {
2507 #print "APW: ALLOWED: lines block<$block>\n";
2508 $allowed = 1;
2509 }
2510 # Check the post-context.
2511 if (defined $chunks[1]) {
2512 my ($cond, $block) = @{$chunks[1]};
2513 if (defined $cond) {
773647a0 2514 substr($block, 0, length($cond), '');
cf655043
AW
2515 }
2516 if ($block =~ /^\s*\{/) {
2517 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2518 $allowed = 1;
2519 }
2520 }
2521 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2522 my $herectx = $here . "\n";;
f055663c 2523 my $cnt = statement_rawlines($block);
cf655043 2524
f055663c
AW
2525 for (my $n = 0; $n < $cnt; $n++) {
2526 $herectx .= raw_line($linenr, $n) . "\n";;
f0a594c1 2527 }
cf655043
AW
2528
2529 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
f0a594c1
AW
2530 }
2531 }
2532
653d4876 2533# don't include deprecated include files (uses RAW line)
4a0df2ef 2534 for my $inc (@dep_includes) {
c45dcabd 2535 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
de7d4f0e 2536 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
0a920b5b
AW
2537 }
2538 }
2539
4a0df2ef
AW
2540# don't use deprecated functions
2541 for my $func (@dep_functions) {
00df344f 2542 if ($line =~ /\b$func\b/) {
de7d4f0e 2543 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
4a0df2ef
AW
2544 }
2545 }
2546
2547# no volatiles please
6c72ffaa
AW
2548 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2549 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
de7d4f0e 2550 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
4a0df2ef
AW
2551 }
2552
9c0ca6f9
AW
2553# SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2554 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2555 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2556 }
2557
00df344f 2558# warn about #if 0
c45dcabd 2559 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
de7d4f0e
AW
2560 CHK("if this code is redundant consider removing it\n" .
2561 $herecurr);
4a0df2ef
AW
2562 }
2563
f0a594c1
AW
2564# check for needless kfree() checks
2565 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2566 my $expr = $1;
2567 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
3c232147 2568 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
f0a594c1
AW
2569 }
2570 }
4c432a8f
GKH
2571# check for needless usb_free_urb() checks
2572 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2573 my $expr = $1;
2574 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2575 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2576 }
2577 }
f0a594c1 2578
00df344f 2579# warn about #ifdefs in C files
c45dcabd 2580# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
00df344f
AW
2581# print "#ifdef in C files should be avoided\n";
2582# print "$herecurr";
2583# $clean = 0;
2584# }
2585
22f2a2ef 2586# warn about spacing in #ifdefs
c45dcabd 2587 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
22f2a2ef
AW
2588 ERROR("exactly one space required after that #$1\n" . $herecurr);
2589 }
2590
4a0df2ef 2591# check for spinlock_t definitions without a comment.
171ae1a4
AW
2592 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2593 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
4a0df2ef
AW
2594 my $which = $1;
2595 if (!ctx_has_comment($first_line, $linenr)) {
de7d4f0e 2596 CHK("$1 definition without comment\n" . $herecurr);
4a0df2ef
AW
2597 }
2598 }
2599# check for memory barriers without a comment.
2600 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2601 if (!ctx_has_comment($first_line, $linenr)) {
de7d4f0e 2602 CHK("memory barrier without comment\n" . $herecurr);
4a0df2ef
AW
2603 }
2604 }
2605# check of hardware specific defines
c45dcabd 2606 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
de7d4f0e 2607 CHK("architecture specific defines should be avoided\n" . $herecurr);
0a920b5b 2608 }
653d4876 2609
d4977c78
TK
2610# Check that the storage class is at the beginning of a declaration
2611 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
2612 WARN("storage class should be at the beginning of the declaration\n" . $herecurr)
2613 }
2614
de7d4f0e
AW
2615# check the location of the inline attribute, that it is between
2616# storage class and type.
9c0ca6f9
AW
2617 if ($line =~ /\b$Type\s+$Inline\b/ ||
2618 $line =~ /\b$Inline\s+$Storage\b/) {
de7d4f0e
AW
2619 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2620 }
2621
8905a67c
AW
2622# Check for __inline__ and __inline, prefer inline
2623 if ($line =~ /\b(__inline__|__inline)\b/) {
2624 WARN("plain inline is preferred over $1\n" . $herecurr);
2625 }
2626
8f53a9b8
JP
2627# check for sizeof(&)
2628 if ($line =~ /\bsizeof\s*\(\s*\&/) {
2629 WARN("sizeof(& should be avoided\n" . $herecurr);
2630 }
2631
de7d4f0e 2632# check for new externs in .c files.
171ae1a4 2633 if ($realfile =~ /\.c$/ && defined $stat &&
c45dcabd 2634 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
171ae1a4 2635 {
c45dcabd
AW
2636 my $function_name = $1;
2637 my $paren_space = $2;
171ae1a4
AW
2638
2639 my $s = $stat;
2640 if (defined $cond) {
2641 substr($s, 0, length($cond), '');
2642 }
c45dcabd
AW
2643 if ($s =~ /^\s*;/ &&
2644 $function_name ne 'uninitialized_var')
2645 {
171ae1a4
AW
2646 WARN("externs should be avoided in .c files\n" . $herecurr);
2647 }
2648
2649 if ($paren_space =~ /\n/) {
2650 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2651 }
9c9ba34e
AW
2652
2653 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2654 $stat =~ /^.\s*extern\s+/)
2655 {
2656 WARN("externs should be avoided in .c files\n" . $herecurr);
de7d4f0e
AW
2657 }
2658
2659# checks for new __setup's
2660 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2661 my $name = $1;
2662
2663 if (!grep(/$name/, @setup_docs)) {
2664 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2665 }
653d4876 2666 }
9c0ca6f9
AW
2667
2668# check for pointless casting of kmalloc return
2669 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2670 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2671 }
13214adf
AW
2672
2673# check for gcc specific __FUNCTION__
2674 if ($line =~ /__FUNCTION__/) {
2675 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2676 }
773647a0
AW
2677
2678# check for semaphores used as mutexes
171ae1a4 2679 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
773647a0
AW
2680 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2681 }
2682# check for semaphores used as mutexes
171ae1a4 2683 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
773647a0 2684 WARN("consider using a completion\n" . $herecurr);
1704f47b 2685
773647a0
AW
2686 }
2687# recommend strict_strto* over simple_strto*
2688 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2689 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2690 }
f3db6639
ME
2691# check for __initcall(), use device_initcall() explicitly please
2692 if ($line =~ /^.\s*__initcall\s*\(/) {
2693 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2694 }
79404849
ER
2695# check for various ops structs, ensure they are const.
2696 my $struct_ops = qr{acpi_dock_ops|
2697 address_space_operations|
2698 backlight_ops|
2699 block_device_operations|
2700 dentry_operations|
2701 dev_pm_ops|
2702 dma_map_ops|
2703 extent_io_ops|
2704 file_lock_operations|
2705 file_operations|
2706 hv_ops|
2707 ide_dma_ops|
2708 intel_dvo_dev_ops|
2709 item_operations|
2710 iwl_ops|
2711 kgdb_arch|
2712 kgdb_io|
2713 kset_uevent_ops|
2714 lock_manager_operations|
2715 microcode_ops|
2716 mtrr_ops|
2717 neigh_ops|
2718 nlmsvc_binding|
2719 pci_raw_ops|
2720 pipe_buf_operations|
2721 platform_hibernation_ops|
2722 platform_suspend_ops|
2723 proto_ops|
2724 rpc_pipe_ops|
2725 seq_operations|
2726 snd_ac97_build_ops|
2727 soc_pcmcia_socket_ops|
2728 stacktrace_ops|
2729 sysfs_ops|
2730 tty_operations|
2731 usb_mon_operations|
2732 wd_ops}x;
6903ffb2 2733 if ($line !~ /\bconst\b/ &&
79404849 2734 $line =~ /\bstruct\s+($struct_ops)\b/) {
6903ffb2
AW
2735 WARN("struct $1 should normally be const\n" .
2736 $herecurr);
2b6db5cb 2737 }
773647a0
AW
2738
2739# use of NR_CPUS is usually wrong
2740# ignore definitions of NR_CPUS and usage to define arrays as likely right
2741 if ($line =~ /\bNR_CPUS\b/ &&
c45dcabd
AW
2742 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2743 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
171ae1a4
AW
2744 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2745 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2746 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
773647a0
AW
2747 {
2748 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2749 }
9c9ba34e
AW
2750
2751# check for %L{u,d,i} in strings
2752 my $string;
2753 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2754 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2a1bc5d5 2755 $string =~ s/%%/__/g;
9c9ba34e
AW
2756 if ($string =~ /(?<!%)%L[udi]/) {
2757 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2758 last;
2759 }
2760 }
691d77b6
AW
2761
2762# whine mightly about in_atomic
2763 if ($line =~ /\bin_atomic\s*\(/) {
2764 if ($realfile =~ m@^drivers/@) {
2765 ERROR("do not use in_atomic in drivers\n" . $herecurr);
f4a87736 2766 } elsif ($realfile !~ m@^kernel/@) {
691d77b6
AW
2767 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2768 }
2769 }
1704f47b
PZ
2770
2771# check for lockdep_set_novalidate_class
2772 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
2773 $line =~ /__lockdep_no_validate__\s*\)/ ) {
2774 if ($realfile !~ m@^kernel/lockdep@ &&
2775 $realfile !~ m@^include/linux/lockdep@ &&
2776 $realfile !~ m@^drivers/base/core@) {
2777 ERROR("lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
2778 }
2779 }
13214adf
AW
2780 }
2781
2782 # If we have no input at all, then there is nothing to report on
2783 # so just keep quiet.
2784 if ($#rawlines == -1) {
2785 exit(0);
0a920b5b
AW
2786 }
2787
8905a67c
AW
2788 # In mailback mode only produce a report in the negative, for
2789 # things that appear to be patches.
2790 if ($mailback && ($clean == 1 || !$is_patch)) {
2791 exit(0);
2792 }
2793
2794 # This is not a patch, and we are are in 'no-patch' mode so
2795 # just keep quiet.
2796 if (!$chk_patch && !$is_patch) {
2797 exit(0);
2798 }
2799
2800 if (!$is_patch) {
de7d4f0e 2801 ERROR("Does not appear to be a unified-diff format patch\n");
0a920b5b
AW
2802 }
2803 if ($is_patch && $chk_signoff && $signoff == 0) {
de7d4f0e 2804 ERROR("Missing Signed-off-by: line(s)\n");
0a920b5b
AW
2805 }
2806
8905a67c 2807 print report_dump();
13214adf
AW
2808 if ($summary && !($clean == 1 && $quiet == 1)) {
2809 print "$filename " if ($summary_file);
8905a67c
AW
2810 print "total: $cnt_error errors, $cnt_warn warnings, " .
2811 (($check)? "$cnt_chk checks, " : "") .
2812 "$cnt_lines lines checked\n";
2813 print "\n" if ($quiet == 0);
f0a594c1 2814 }
8905a67c 2815
0a920b5b 2816 if ($clean == 1 && $quiet == 0) {
c2fdda0d 2817 print "$vname has no obvious style problems and is ready for submission.\n"
0a920b5b
AW
2818 }
2819 if ($clean == 0 && $quiet == 0) {
c2fdda0d 2820 print "$vname has style problems, please review. If any of these errors\n";
0a920b5b
AW
2821 print "are false positives report them to the maintainer, see\n";
2822 print "CHECKPATCH in MAINTAINERS.\n";
2823 }
13214adf 2824
0a920b5b
AW
2825 return $clean;
2826}