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