checkpatch: move test for space before semicolon after operator spacing
[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
000d1cc1 13my $V = '0.32';
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;
000d1cc1 29my $show_types = 0;
3705ce5b 30my $fix = 0;
6c72ffaa 31my $root;
c2fdda0d 32my %debug;
000d1cc1
JP
33my %ignore_type = ();
34my @ignore = ();
77f5b10a 35my $help = 0;
000d1cc1 36my $configuration_file = ".checkpatch.conf";
6cd7f386 37my $max_line_length = 80;
77f5b10a
HE
38
39sub help {
40 my ($exitcode) = @_;
41
42 print << "EOM";
43Usage: $P [OPTION]... [FILE]...
44Version: $V
45
46Options:
47 -q, --quiet quiet
48 --no-tree run without a kernel tree
49 --no-signoff do not check for 'Signed-off-by' line
50 --patch treat FILE as patchfile (default)
51 --emacs emacs compile window format
52 --terse one line per report
53 -f, --file treat FILE as regular source file
54 --subjective, --strict enable more subjective tests
000d1cc1 55 --ignore TYPE(,TYPE2...) ignore various comma separated message types
6cd7f386 56 --max-line-length=n set the maximum line length, if exceeded, warn
000d1cc1 57 --show-types show the message "types" in the output
77f5b10a
HE
58 --root=PATH PATH to the kernel tree root
59 --no-summary suppress the per-file summary
60 --mailback only produce a report in case of warnings/errors
61 --summary-file include the filename in summary
62 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
63 'values', 'possible', 'type', and 'attr' (default
64 is all off)
65 --test-only=WORD report only warnings/errors containing WORD
66 literally
3705ce5b
JP
67 --fix EXPERIMENTAL - may create horrible results
68 If correctable single-line errors exist, create
69 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
70 with potential errors corrected to the preferred
71 checkpatch style
77f5b10a
HE
72 -h, --help, --version display this help and exit
73
74When FILE is - read standard input.
75EOM
76
77 exit($exitcode);
78}
79
000d1cc1
JP
80my $conf = which_conf($configuration_file);
81if (-f $conf) {
82 my @conf_args;
83 open(my $conffile, '<', "$conf")
84 or warn "$P: Can't find a readable $configuration_file file $!\n";
85
86 while (<$conffile>) {
87 my $line = $_;
88
89 $line =~ s/\s*\n?$//g;
90 $line =~ s/^\s*//g;
91 $line =~ s/\s+/ /g;
92
93 next if ($line =~ m/^\s*#/);
94 next if ($line =~ m/^\s*$/);
95
96 my @words = split(" ", $line);
97 foreach my $word (@words) {
98 last if ($word =~ m/^#/);
99 push (@conf_args, $word);
100 }
101 }
102 close($conffile);
103 unshift(@ARGV, @conf_args) if @conf_args;
104}
105
0a920b5b 106GetOptions(
6c72ffaa 107 'q|quiet+' => \$quiet,
0a920b5b
AW
108 'tree!' => \$tree,
109 'signoff!' => \$chk_signoff,
110 'patch!' => \$chk_patch,
6c72ffaa 111 'emacs!' => \$emacs,
8905a67c 112 'terse!' => \$terse,
77f5b10a 113 'f|file!' => \$file,
6c72ffaa
AW
114 'subjective!' => \$check,
115 'strict!' => \$check,
000d1cc1
JP
116 'ignore=s' => \@ignore,
117 'show-types!' => \$show_types,
6cd7f386 118 'max-line-length=i' => \$max_line_length,
6c72ffaa 119 'root=s' => \$root,
8905a67c
AW
120 'summary!' => \$summary,
121 'mailback!' => \$mailback,
13214adf 122 'summary-file!' => \$summary_file,
3705ce5b 123 'fix!' => \$fix,
c2fdda0d 124 'debug=s' => \%debug,
773647a0 125 'test-only=s' => \$tst_only,
77f5b10a
HE
126 'h|help' => \$help,
127 'version' => \$help
128) or help(1);
129
130help(0) if ($help);
0a920b5b
AW
131
132my $exit = 0;
133
134if ($#ARGV < 0) {
77f5b10a 135 print "$P: no input files\n";
0a920b5b
AW
136 exit(1);
137}
138
000d1cc1
JP
139@ignore = split(/,/, join(',',@ignore));
140foreach my $word (@ignore) {
141 $word =~ s/\s*\n?$//g;
142 $word =~ s/^\s*//g;
143 $word =~ s/\s+/ /g;
144 $word =~ tr/[a-z]/[A-Z]/;
145
146 next if ($word =~ m/^\s*#/);
147 next if ($word =~ m/^\s*$/);
148
149 $ignore_type{$word}++;
150}
151
c2fdda0d
AW
152my $dbg_values = 0;
153my $dbg_possible = 0;
7429c690 154my $dbg_type = 0;
a1ef277e 155my $dbg_attr = 0;
c2fdda0d 156for my $key (keys %debug) {
21caa13c
AW
157 ## no critic
158 eval "\${dbg_$key} = '$debug{$key}';";
159 die "$@" if ($@);
c2fdda0d
AW
160}
161
d2c0a235
AW
162my $rpt_cleaners = 0;
163
8905a67c
AW
164if ($terse) {
165 $emacs = 1;
166 $quiet++;
167}
168
6c72ffaa
AW
169if ($tree) {
170 if (defined $root) {
171 if (!top_of_kernel_tree($root)) {
172 die "$P: $root: --root does not point at a valid tree\n";
173 }
174 } else {
175 if (top_of_kernel_tree('.')) {
176 $root = '.';
177 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
178 top_of_kernel_tree($1)) {
179 $root = $1;
180 }
181 }
182
183 if (!defined $root) {
184 print "Must be run from the top-level dir. of a kernel tree\n";
185 exit(2);
186 }
0a920b5b
AW
187}
188
6c72ffaa
AW
189my $emitted_corrupt = 0;
190
2ceb532b
AW
191our $Ident = qr{
192 [A-Za-z_][A-Za-z\d_]*
193 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
194 }x;
6c72ffaa
AW
195our $Storage = qr{extern|static|asmlinkage};
196our $Sparse = qr{
197 __user|
198 __kernel|
199 __force|
200 __iomem|
201 __must_check|
202 __init_refok|
417495ed 203 __kprobes|
165e72a6
SE
204 __ref|
205 __rcu
6c72ffaa 206 }x;
52131292
WS
207
208# Notes to $Attribute:
209# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
6c72ffaa
AW
210our $Attribute = qr{
211 const|
03f1df7d
JP
212 __percpu|
213 __nocast|
214 __safe|
215 __bitwise__|
216 __packed__|
217 __packed2__|
218 __naked|
219 __maybe_unused|
220 __always_unused|
221 __noreturn|
222 __used|
223 __cold|
224 __noclone|
225 __deprecated|
6c72ffaa
AW
226 __read_mostly|
227 __kprobes|
52131292 228 __(?:mem|cpu|dev|)(?:initdata|initconst|init\b)|
24e1d81a
AW
229 ____cacheline_aligned|
230 ____cacheline_aligned_in_smp|
5fe3af11
AW
231 ____cacheline_internodealigned_in_smp|
232 __weak
6c72ffaa 233 }x;
c45dcabd 234our $Modifier;
6c72ffaa 235our $Inline = qr{inline|__always_inline|noinline};
6c72ffaa
AW
236our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
237our $Lval = qr{$Ident(?:$Member)*};
238
95e2c602
JP
239our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
240our $Binary = qr{(?i)0b[01]+$Int_type?};
241our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
242our $Int = qr{[0-9]+$Int_type?};
326b1ffc
JP
243our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
244our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
245our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
74349bcc 246our $Float = qr{$Float_hex|$Float_dec|$Float_int};
95e2c602 247our $Constant = qr{$Float|$Binary|$Hex|$Int};
326b1ffc 248our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
86f9d059 249our $Compare = qr{<=|>=|==|!=|<|>};
23f780c9 250our $Arithmetic = qr{\+|-|\*|\/|%};
6c72ffaa
AW
251our $Operators = qr{
252 <=|>=|==|!=|
253 =>|->|<<|>>|<|>|!|~|
23f780c9 254 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
6c72ffaa
AW
255 }x;
256
8905a67c
AW
257our $NonptrType;
258our $Type;
259our $Declare;
260
15662b3e
JP
261our $NON_ASCII_UTF8 = qr{
262 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
171ae1a4
AW
263 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
264 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
265 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
266 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
267 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
268 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
269}x;
270
15662b3e
JP
271our $UTF8 = qr{
272 [\x09\x0A\x0D\x20-\x7E] # ASCII
273 | $NON_ASCII_UTF8
274}x;
275
8ed22cad 276our $typeTypedefs = qr{(?x:
fb9e9096 277 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
8ed22cad
AW
278 atomic_t
279)};
280
691e669b 281our $logFunctions = qr{(?x:
6e60c02e
JP
282 printk(?:_ratelimited|_once|)|
283 [a-z0-9]+_(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
284 WARN(?:_RATELIMIT|_ONCE|)|
b0531722
JP
285 panic|
286 MODULE_[A-Z_]+
691e669b
JP
287)};
288
20112475
JP
289our $signature_tags = qr{(?xi:
290 Signed-off-by:|
291 Acked-by:|
292 Tested-by:|
293 Reviewed-by:|
294 Reported-by:|
8543ae12 295 Suggested-by:|
20112475
JP
296 To:|
297 Cc:
298)};
299
8905a67c
AW
300our @typeList = (
301 qr{void},
c45dcabd
AW
302 qr{(?:unsigned\s+)?char},
303 qr{(?:unsigned\s+)?short},
304 qr{(?:unsigned\s+)?int},
305 qr{(?:unsigned\s+)?long},
306 qr{(?:unsigned\s+)?long\s+int},
307 qr{(?:unsigned\s+)?long\s+long},
308 qr{(?:unsigned\s+)?long\s+long\s+int},
8905a67c
AW
309 qr{unsigned},
310 qr{float},
311 qr{double},
312 qr{bool},
8905a67c
AW
313 qr{struct\s+$Ident},
314 qr{union\s+$Ident},
315 qr{enum\s+$Ident},
316 qr{${Ident}_t},
317 qr{${Ident}_handler},
318 qr{${Ident}_handler_fn},
319);
c45dcabd
AW
320our @modifierList = (
321 qr{fastcall},
322);
8905a67c 323
7840a94c
WS
324our $allowed_asm_includes = qr{(?x:
325 irq|
326 memory
327)};
328# memory.h: ARM has a custom one
329
8905a67c 330sub build_types {
d2172eb5
AW
331 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
332 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
c8cb2ca3 333 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
8905a67c 334 $NonptrType = qr{
d2172eb5 335 (?:$Modifier\s+|const\s+)*
cf655043 336 (?:
6b48db24 337 (?:typeof|__typeof__)\s*\([^\)]*\)|
8ed22cad 338 (?:$typeTypedefs\b)|
c45dcabd 339 (?:${all}\b)
cf655043 340 )
c8cb2ca3 341 (?:\s+$Modifier|\s+const)*
8905a67c
AW
342 }x;
343 $Type = qr{
c45dcabd 344 $NonptrType
b337d8b8 345 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)?
c8cb2ca3 346 (?:\s+$Inline|\s+$Modifier)*
8905a67c
AW
347 }x;
348 $Declare = qr{(?:$Storage\s+)?$Type};
349}
350build_types();
6c72ffaa 351
7d2367af
JP
352
353our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
d1fe9c09
JP
354
355# Using $balanced_parens, $LvalOrFunc, or $FuncArg
356# requires at least perl version v5.10.0
357# Any use must be runtime checked with $^V
358
359our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
360our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
d7c76ba7 361our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
7d2367af
JP
362
363sub deparenthesize {
364 my ($string) = @_;
365 return "" if (!defined($string));
366 $string =~ s@^\s*\(\s*@@g;
367 $string =~ s@\s*\)\s*$@@g;
368 $string =~ s@\s+@ @g;
369 return $string;
370}
371
6c72ffaa
AW
372$chk_signoff = 0 if ($file);
373
00df344f 374my @rawlines = ();
c2fdda0d 375my @lines = ();
3705ce5b 376my @fixed = ();
c2fdda0d 377my $vname;
6c72ffaa 378for my $filename (@ARGV) {
21caa13c 379 my $FILE;
6c72ffaa 380 if ($file) {
21caa13c 381 open($FILE, '-|', "diff -u /dev/null $filename") ||
6c72ffaa 382 die "$P: $filename: diff failed - $!\n";
21caa13c
AW
383 } elsif ($filename eq '-') {
384 open($FILE, '<&STDIN');
6c72ffaa 385 } else {
21caa13c 386 open($FILE, '<', "$filename") ||
6c72ffaa 387 die "$P: $filename: open failed - $!\n";
0a920b5b 388 }
c2fdda0d
AW
389 if ($filename eq '-') {
390 $vname = 'Your patch';
391 } else {
392 $vname = $filename;
393 }
21caa13c 394 while (<$FILE>) {
6c72ffaa
AW
395 chomp;
396 push(@rawlines, $_);
397 }
21caa13c 398 close($FILE);
c2fdda0d 399 if (!process($filename)) {
6c72ffaa
AW
400 $exit = 1;
401 }
402 @rawlines = ();
13214adf 403 @lines = ();
3705ce5b 404 @fixed = ();
0a920b5b
AW
405}
406
407exit($exit);
408
409sub top_of_kernel_tree {
6c72ffaa
AW
410 my ($root) = @_;
411
412 my @tree_check = (
413 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
414 "README", "Documentation", "arch", "include", "drivers",
415 "fs", "init", "ipc", "kernel", "lib", "scripts",
416 );
417
418 foreach my $check (@tree_check) {
419 if (! -e $root . '/' . $check) {
420 return 0;
421 }
0a920b5b 422 }
6c72ffaa 423 return 1;
8f26b837 424}
0a920b5b 425
20112475
JP
426sub parse_email {
427 my ($formatted_email) = @_;
428
429 my $name = "";
430 my $address = "";
431 my $comment = "";
432
433 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
434 $name = $1;
435 $address = $2;
436 $comment = $3 if defined $3;
437 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
438 $address = $1;
439 $comment = $2 if defined $2;
440 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
441 $address = $1;
442 $comment = $2 if defined $2;
443 $formatted_email =~ s/$address.*$//;
444 $name = $formatted_email;
3705ce5b 445 $name = trim($name);
20112475
JP
446 $name =~ s/^\"|\"$//g;
447 # If there's a name left after stripping spaces and
448 # leading quotes, and the address doesn't have both
449 # leading and trailing angle brackets, the address
450 # is invalid. ie:
451 # "joe smith joe@smith.com" bad
452 # "joe smith <joe@smith.com" bad
453 if ($name ne "" && $address !~ /^<[^>]+>$/) {
454 $name = "";
455 $address = "";
456 $comment = "";
457 }
458 }
459
3705ce5b 460 $name = trim($name);
20112475 461 $name =~ s/^\"|\"$//g;
3705ce5b 462 $address = trim($address);
20112475
JP
463 $address =~ s/^\<|\>$//g;
464
465 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
466 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
467 $name = "\"$name\"";
468 }
469
470 return ($name, $address, $comment);
471}
472
473sub format_email {
474 my ($name, $address) = @_;
475
476 my $formatted_email;
477
3705ce5b 478 $name = trim($name);
20112475 479 $name =~ s/^\"|\"$//g;
3705ce5b 480 $address = trim($address);
20112475
JP
481
482 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
483 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
484 $name = "\"$name\"";
485 }
486
487 if ("$name" eq "") {
488 $formatted_email = "$address";
489 } else {
490 $formatted_email = "$name <$address>";
491 }
492
493 return $formatted_email;
494}
495
000d1cc1
JP
496sub which_conf {
497 my ($conf) = @_;
498
499 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
500 if (-e "$path/$conf") {
501 return "$path/$conf";
502 }
503 }
504
505 return "";
506}
507
0a920b5b
AW
508sub expand_tabs {
509 my ($str) = @_;
510
511 my $res = '';
512 my $n = 0;
513 for my $c (split(//, $str)) {
514 if ($c eq "\t") {
515 $res .= ' ';
516 $n++;
517 for (; ($n % 8) != 0; $n++) {
518 $res .= ' ';
519 }
520 next;
521 }
522 $res .= $c;
523 $n++;
524 }
525
526 return $res;
527}
6c72ffaa 528sub copy_spacing {
773647a0 529 (my $res = shift) =~ tr/\t/ /c;
6c72ffaa
AW
530 return $res;
531}
0a920b5b 532
4a0df2ef
AW
533sub line_stats {
534 my ($line) = @_;
535
536 # Drop the diff line leader and expand tabs
537 $line =~ s/^.//;
538 $line = expand_tabs($line);
539
540 # Pick the indent from the front of the line.
541 my ($white) = ($line =~ /^(\s*)/);
542
543 return (length($line), length($white));
544}
545
773647a0
AW
546my $sanitise_quote = '';
547
548sub sanitise_line_reset {
549 my ($in_comment) = @_;
550
551 if ($in_comment) {
552 $sanitise_quote = '*/';
553 } else {
554 $sanitise_quote = '';
555 }
556}
00df344f
AW
557sub sanitise_line {
558 my ($line) = @_;
559
560 my $res = '';
561 my $l = '';
562
c2fdda0d 563 my $qlen = 0;
773647a0
AW
564 my $off = 0;
565 my $c;
00df344f 566
773647a0
AW
567 # Always copy over the diff marker.
568 $res = substr($line, 0, 1);
569
570 for ($off = 1; $off < length($line); $off++) {
571 $c = substr($line, $off, 1);
572
573 # Comments we are wacking completly including the begin
574 # and end, all to $;.
575 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
576 $sanitise_quote = '*/';
577
578 substr($res, $off, 2, "$;$;");
579 $off++;
580 next;
00df344f 581 }
81bc0e02 582 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
773647a0
AW
583 $sanitise_quote = '';
584 substr($res, $off, 2, "$;$;");
585 $off++;
586 next;
c2fdda0d 587 }
113f04a8
DW
588 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
589 $sanitise_quote = '//';
590
591 substr($res, $off, 2, $sanitise_quote);
592 $off++;
593 next;
594 }
773647a0
AW
595
596 # A \ in a string means ignore the next character.
597 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
598 $c eq "\\") {
599 substr($res, $off, 2, 'XX');
600 $off++;
601 next;
00df344f 602 }
773647a0
AW
603 # Regular quotes.
604 if ($c eq "'" || $c eq '"') {
605 if ($sanitise_quote eq '') {
606 $sanitise_quote = $c;
00df344f 607
773647a0
AW
608 substr($res, $off, 1, $c);
609 next;
610 } elsif ($sanitise_quote eq $c) {
611 $sanitise_quote = '';
612 }
613 }
00df344f 614
fae17dae 615 #print "c<$c> SQ<$sanitise_quote>\n";
773647a0
AW
616 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
617 substr($res, $off, 1, $;);
113f04a8
DW
618 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
619 substr($res, $off, 1, $;);
773647a0
AW
620 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
621 substr($res, $off, 1, 'X');
622 } else {
623 substr($res, $off, 1, $c);
624 }
c2fdda0d
AW
625 }
626
113f04a8
DW
627 if ($sanitise_quote eq '//') {
628 $sanitise_quote = '';
629 }
630
c2fdda0d 631 # The pathname on a #include may be surrounded by '<' and '>'.
c45dcabd 632 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
c2fdda0d
AW
633 my $clean = 'X' x length($1);
634 $res =~ s@\<.*\>@<$clean>@;
635
636 # The whole of a #error is a string.
c45dcabd 637 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
c2fdda0d 638 my $clean = 'X' x length($1);
c45dcabd 639 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
c2fdda0d
AW
640 }
641
00df344f
AW
642 return $res;
643}
644
a6962d72
JP
645sub get_quoted_string {
646 my ($line, $rawline) = @_;
647
648 return "" if ($line !~ m/(\"[X]+\")/g);
649 return substr($rawline, $-[0], $+[0] - $-[0]);
650}
651
8905a67c
AW
652sub ctx_statement_block {
653 my ($linenr, $remain, $off) = @_;
654 my $line = $linenr - 1;
655 my $blk = '';
656 my $soff = $off;
657 my $coff = $off - 1;
773647a0 658 my $coff_set = 0;
8905a67c 659
13214adf
AW
660 my $loff = 0;
661
8905a67c
AW
662 my $type = '';
663 my $level = 0;
a2750645 664 my @stack = ();
cf655043 665 my $p;
8905a67c
AW
666 my $c;
667 my $len = 0;
13214adf
AW
668
669 my $remainder;
8905a67c 670 while (1) {
a2750645
AW
671 @stack = (['', 0]) if ($#stack == -1);
672
773647a0 673 #warn "CSB: blk<$blk> remain<$remain>\n";
8905a67c
AW
674 # If we are about to drop off the end, pull in more
675 # context.
676 if ($off >= $len) {
677 for (; $remain > 0; $line++) {
dea33496 678 last if (!defined $lines[$line]);
c2fdda0d 679 next if ($lines[$line] =~ /^-/);
8905a67c 680 $remain--;
13214adf 681 $loff = $len;
c2fdda0d 682 $blk .= $lines[$line] . "\n";
8905a67c
AW
683 $len = length($blk);
684 $line++;
685 last;
686 }
687 # Bail if there is no further context.
688 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
13214adf 689 if ($off >= $len) {
8905a67c
AW
690 last;
691 }
f74bd194
AW
692 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
693 $level++;
694 $type = '#';
695 }
8905a67c 696 }
cf655043 697 $p = $c;
8905a67c 698 $c = substr($blk, $off, 1);
13214adf 699 $remainder = substr($blk, $off);
8905a67c 700
773647a0 701 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
4635f4fb
AW
702
703 # Handle nested #if/#else.
704 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
705 push(@stack, [ $type, $level ]);
706 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
707 ($type, $level) = @{$stack[$#stack - 1]};
708 } elsif ($remainder =~ /^#\s*endif\b/) {
709 ($type, $level) = @{pop(@stack)};
710 }
711
8905a67c
AW
712 # Statement ends at the ';' or a close '}' at the
713 # outermost level.
714 if ($level == 0 && $c eq ';') {
715 last;
716 }
717
13214adf 718 # An else is really a conditional as long as its not else if
773647a0
AW
719 if ($level == 0 && $coff_set == 0 &&
720 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
721 $remainder =~ /^(else)(?:\s|{)/ &&
722 $remainder !~ /^else\s+if\b/) {
723 $coff = $off + length($1) - 1;
724 $coff_set = 1;
725 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
726 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
13214adf
AW
727 }
728
8905a67c
AW
729 if (($type eq '' || $type eq '(') && $c eq '(') {
730 $level++;
731 $type = '(';
732 }
733 if ($type eq '(' && $c eq ')') {
734 $level--;
735 $type = ($level != 0)? '(' : '';
736
737 if ($level == 0 && $coff < $soff) {
738 $coff = $off;
773647a0
AW
739 $coff_set = 1;
740 #warn "CSB: mark coff<$coff>\n";
8905a67c
AW
741 }
742 }
743 if (($type eq '' || $type eq '{') && $c eq '{') {
744 $level++;
745 $type = '{';
746 }
747 if ($type eq '{' && $c eq '}') {
748 $level--;
749 $type = ($level != 0)? '{' : '';
750
751 if ($level == 0) {
b998e001
PP
752 if (substr($blk, $off + 1, 1) eq ';') {
753 $off++;
754 }
8905a67c
AW
755 last;
756 }
757 }
f74bd194
AW
758 # Preprocessor commands end at the newline unless escaped.
759 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
760 $level--;
761 $type = '';
762 $off++;
763 last;
764 }
8905a67c
AW
765 $off++;
766 }
a3bb97a7 767 # We are truly at the end, so shuffle to the next line.
13214adf 768 if ($off == $len) {
a3bb97a7 769 $loff = $len + 1;
13214adf
AW
770 $line++;
771 $remain--;
772 }
8905a67c
AW
773
774 my $statement = substr($blk, $soff, $off - $soff + 1);
775 my $condition = substr($blk, $soff, $coff - $soff + 1);
776
777 #warn "STATEMENT<$statement>\n";
778 #warn "CONDITION<$condition>\n";
779
773647a0 780 #print "coff<$coff> soff<$off> loff<$loff>\n";
13214adf
AW
781
782 return ($statement, $condition,
783 $line, $remain + 1, $off - $loff + 1, $level);
784}
785
cf655043
AW
786sub statement_lines {
787 my ($stmt) = @_;
788
789 # Strip the diff line prefixes and rip blank lines at start and end.
790 $stmt =~ s/(^|\n)./$1/g;
791 $stmt =~ s/^\s*//;
792 $stmt =~ s/\s*$//;
793
794 my @stmt_lines = ($stmt =~ /\n/g);
795
796 return $#stmt_lines + 2;
797}
798
799sub statement_rawlines {
800 my ($stmt) = @_;
801
802 my @stmt_lines = ($stmt =~ /\n/g);
803
804 return $#stmt_lines + 2;
805}
806
807sub statement_block_size {
808 my ($stmt) = @_;
809
810 $stmt =~ s/(^|\n)./$1/g;
811 $stmt =~ s/^\s*{//;
812 $stmt =~ s/}\s*$//;
813 $stmt =~ s/^\s*//;
814 $stmt =~ s/\s*$//;
815
816 my @stmt_lines = ($stmt =~ /\n/g);
817 my @stmt_statements = ($stmt =~ /;/g);
818
819 my $stmt_lines = $#stmt_lines + 2;
820 my $stmt_statements = $#stmt_statements + 1;
821
822 if ($stmt_lines > $stmt_statements) {
823 return $stmt_lines;
824 } else {
825 return $stmt_statements;
826 }
827}
828
13214adf
AW
829sub ctx_statement_full {
830 my ($linenr, $remain, $off) = @_;
831 my ($statement, $condition, $level);
832
833 my (@chunks);
834
cf655043 835 # Grab the first conditional/block pair.
13214adf
AW
836 ($statement, $condition, $linenr, $remain, $off, $level) =
837 ctx_statement_block($linenr, $remain, $off);
773647a0 838 #print "F: c<$condition> s<$statement> remain<$remain>\n";
cf655043
AW
839 push(@chunks, [ $condition, $statement ]);
840 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
841 return ($level, $linenr, @chunks);
842 }
843
844 # Pull in the following conditional/block pairs and see if they
845 # could continue the statement.
13214adf 846 for (;;) {
13214adf
AW
847 ($statement, $condition, $linenr, $remain, $off, $level) =
848 ctx_statement_block($linenr, $remain, $off);
cf655043 849 #print "C: c<$condition> s<$statement> remain<$remain>\n";
773647a0 850 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
cf655043
AW
851 #print "C: push\n";
852 push(@chunks, [ $condition, $statement ]);
13214adf
AW
853 }
854
855 return ($level, $linenr, @chunks);
8905a67c
AW
856}
857
4a0df2ef 858sub ctx_block_get {
f0a594c1 859 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
4a0df2ef
AW
860 my $line;
861 my $start = $linenr - 1;
4a0df2ef
AW
862 my $blk = '';
863 my @o;
864 my @c;
865 my @res = ();
866
f0a594c1 867 my $level = 0;
4635f4fb 868 my @stack = ($level);
00df344f
AW
869 for ($line = $start; $remain > 0; $line++) {
870 next if ($rawlines[$line] =~ /^-/);
871 $remain--;
872
873 $blk .= $rawlines[$line];
4635f4fb
AW
874
875 # Handle nested #if/#else.
01464f30 876 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
4635f4fb 877 push(@stack, $level);
01464f30 878 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
4635f4fb 879 $level = $stack[$#stack - 1];
01464f30 880 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
4635f4fb
AW
881 $level = pop(@stack);
882 }
883
01464f30 884 foreach my $c (split(//, $lines[$line])) {
f0a594c1
AW
885 ##print "C<$c>L<$level><$open$close>O<$off>\n";
886 if ($off > 0) {
887 $off--;
888 next;
889 }
4a0df2ef 890
f0a594c1
AW
891 if ($c eq $close && $level > 0) {
892 $level--;
893 last if ($level == 0);
894 } elsif ($c eq $open) {
895 $level++;
896 }
897 }
4a0df2ef 898
f0a594c1 899 if (!$outer || $level <= 1) {
00df344f 900 push(@res, $rawlines[$line]);
4a0df2ef
AW
901 }
902
f0a594c1 903 last if ($level == 0);
4a0df2ef
AW
904 }
905
f0a594c1 906 return ($level, @res);
4a0df2ef
AW
907}
908sub ctx_block_outer {
909 my ($linenr, $remain) = @_;
910
f0a594c1
AW
911 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
912 return @r;
4a0df2ef
AW
913}
914sub ctx_block {
915 my ($linenr, $remain) = @_;
916
f0a594c1
AW
917 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
918 return @r;
653d4876
AW
919}
920sub ctx_statement {
f0a594c1
AW
921 my ($linenr, $remain, $off) = @_;
922
923 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
924 return @r;
925}
926sub ctx_block_level {
653d4876
AW
927 my ($linenr, $remain) = @_;
928
f0a594c1 929 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
4a0df2ef 930}
9c0ca6f9
AW
931sub ctx_statement_level {
932 my ($linenr, $remain, $off) = @_;
933
934 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
935}
4a0df2ef
AW
936
937sub ctx_locate_comment {
938 my ($first_line, $end_line) = @_;
939
940 # Catch a comment on the end of the line itself.
beae6332 941 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
4a0df2ef
AW
942 return $current_comment if (defined $current_comment);
943
944 # Look through the context and try and figure out if there is a
945 # comment.
946 my $in_comment = 0;
947 $current_comment = '';
948 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
00df344f
AW
949 my $line = $rawlines[$linenr - 1];
950 #warn " $line\n";
4a0df2ef
AW
951 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
952 $in_comment = 1;
953 }
954 if ($line =~ m@/\*@) {
955 $in_comment = 1;
956 }
957 if (!$in_comment && $current_comment ne '') {
958 $current_comment = '';
959 }
960 $current_comment .= $line . "\n" if ($in_comment);
961 if ($line =~ m@\*/@) {
962 $in_comment = 0;
963 }
964 }
965
966 chomp($current_comment);
967 return($current_comment);
968}
969sub ctx_has_comment {
970 my ($first_line, $end_line) = @_;
971 my $cmt = ctx_locate_comment($first_line, $end_line);
972
00df344f 973 ##print "LINE: $rawlines[$end_line - 1 ]\n";
4a0df2ef
AW
974 ##print "CMMT: $cmt\n";
975
976 return ($cmt ne '');
977}
978
4d001e4d
AW
979sub raw_line {
980 my ($linenr, $cnt) = @_;
981
982 my $offset = $linenr - 1;
983 $cnt++;
984
985 my $line;
986 while ($cnt) {
987 $line = $rawlines[$offset++];
988 next if (defined($line) && $line =~ /^-/);
989 $cnt--;
990 }
991
992 return $line;
993}
994
6c72ffaa
AW
995sub cat_vet {
996 my ($vet) = @_;
997 my ($res, $coded);
9c0ca6f9 998
6c72ffaa
AW
999 $res = '';
1000 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1001 $res .= $1;
1002 if ($2 ne '') {
1003 $coded = sprintf("^%c", unpack('C', $2) + 64);
1004 $res .= $coded;
9c0ca6f9
AW
1005 }
1006 }
6c72ffaa 1007 $res =~ s/$/\$/;
9c0ca6f9 1008
6c72ffaa 1009 return $res;
9c0ca6f9
AW
1010}
1011
c2fdda0d 1012my $av_preprocessor = 0;
cf655043 1013my $av_pending;
c2fdda0d 1014my @av_paren_type;
1f65f947 1015my $av_pend_colon;
c2fdda0d
AW
1016
1017sub annotate_reset {
1018 $av_preprocessor = 0;
cf655043
AW
1019 $av_pending = '_';
1020 @av_paren_type = ('E');
1f65f947 1021 $av_pend_colon = 'O';
c2fdda0d
AW
1022}
1023
6c72ffaa
AW
1024sub annotate_values {
1025 my ($stream, $type) = @_;
0a920b5b 1026
6c72ffaa 1027 my $res;
1f65f947 1028 my $var = '_' x length($stream);
6c72ffaa
AW
1029 my $cur = $stream;
1030
c2fdda0d 1031 print "$stream\n" if ($dbg_values > 1);
6c72ffaa 1032
6c72ffaa 1033 while (length($cur)) {
773647a0 1034 @av_paren_type = ('E') if ($#av_paren_type < 0);
cf655043 1035 print " <" . join('', @av_paren_type) .
171ae1a4 1036 "> <$type> <$av_pending>" if ($dbg_values > 1);
6c72ffaa 1037 if ($cur =~ /^(\s+)/o) {
c2fdda0d
AW
1038 print "WS($1)\n" if ($dbg_values > 1);
1039 if ($1 =~ /\n/ && $av_preprocessor) {
cf655043 1040 $type = pop(@av_paren_type);
c2fdda0d 1041 $av_preprocessor = 0;
6c72ffaa
AW
1042 }
1043
c023e473 1044 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
9446ef56
AW
1045 print "CAST($1)\n" if ($dbg_values > 1);
1046 push(@av_paren_type, $type);
addcdcea 1047 $type = 'c';
9446ef56 1048
e91b6e26 1049 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
c2fdda0d 1050 print "DECLARE($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1051 $type = 'T';
1052
389a2fe5
AW
1053 } elsif ($cur =~ /^($Modifier)\s*/) {
1054 print "MODIFIER($1)\n" if ($dbg_values > 1);
1055 $type = 'T';
1056
c45dcabd 1057 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
171ae1a4 1058 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
c2fdda0d 1059 $av_preprocessor = 1;
171ae1a4
AW
1060 push(@av_paren_type, $type);
1061 if ($2 ne '') {
1062 $av_pending = 'N';
1063 }
1064 $type = 'E';
1065
c45dcabd 1066 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
171ae1a4
AW
1067 print "UNDEF($1)\n" if ($dbg_values > 1);
1068 $av_preprocessor = 1;
1069 push(@av_paren_type, $type);
6c72ffaa 1070
c45dcabd 1071 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
cf655043 1072 print "PRE_START($1)\n" if ($dbg_values > 1);
c2fdda0d 1073 $av_preprocessor = 1;
cf655043
AW
1074
1075 push(@av_paren_type, $type);
1076 push(@av_paren_type, $type);
171ae1a4 1077 $type = 'E';
cf655043 1078
c45dcabd 1079 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
cf655043
AW
1080 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1081 $av_preprocessor = 1;
1082
1083 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1084
171ae1a4 1085 $type = 'E';
cf655043 1086
c45dcabd 1087 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
cf655043
AW
1088 print "PRE_END($1)\n" if ($dbg_values > 1);
1089
1090 $av_preprocessor = 1;
1091
1092 # Assume all arms of the conditional end as this
1093 # one does, and continue as if the #endif was not here.
1094 pop(@av_paren_type);
1095 push(@av_paren_type, $type);
171ae1a4 1096 $type = 'E';
6c72ffaa
AW
1097
1098 } elsif ($cur =~ /^(\\\n)/o) {
c2fdda0d 1099 print "PRECONT($1)\n" if ($dbg_values > 1);
6c72ffaa 1100
171ae1a4
AW
1101 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1102 print "ATTR($1)\n" if ($dbg_values > 1);
1103 $av_pending = $type;
1104 $type = 'N';
1105
6c72ffaa 1106 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
c2fdda0d 1107 print "SIZEOF($1)\n" if ($dbg_values > 1);
6c72ffaa 1108 if (defined $2) {
cf655043 1109 $av_pending = 'V';
6c72ffaa
AW
1110 }
1111 $type = 'N';
1112
14b111c1 1113 } elsif ($cur =~ /^(if|while|for)\b/o) {
c2fdda0d 1114 print "COND($1)\n" if ($dbg_values > 1);
14b111c1 1115 $av_pending = 'E';
6c72ffaa
AW
1116 $type = 'N';
1117
1f65f947
AW
1118 } elsif ($cur =~/^(case)/o) {
1119 print "CASE($1)\n" if ($dbg_values > 1);
1120 $av_pend_colon = 'C';
1121 $type = 'N';
1122
14b111c1 1123 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
c2fdda0d 1124 print "KEYWORD($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1125 $type = 'N';
1126
1127 } elsif ($cur =~ /^(\()/o) {
c2fdda0d 1128 print "PAREN('$1')\n" if ($dbg_values > 1);
cf655043
AW
1129 push(@av_paren_type, $av_pending);
1130 $av_pending = '_';
6c72ffaa
AW
1131 $type = 'N';
1132
1133 } elsif ($cur =~ /^(\))/o) {
cf655043
AW
1134 my $new_type = pop(@av_paren_type);
1135 if ($new_type ne '_') {
1136 $type = $new_type;
c2fdda0d
AW
1137 print "PAREN('$1') -> $type\n"
1138 if ($dbg_values > 1);
6c72ffaa 1139 } else {
c2fdda0d 1140 print "PAREN('$1')\n" if ($dbg_values > 1);
6c72ffaa
AW
1141 }
1142
c8cb2ca3 1143 } elsif ($cur =~ /^($Ident)\s*\(/o) {
c2fdda0d 1144 print "FUNC($1)\n" if ($dbg_values > 1);
c8cb2ca3 1145 $type = 'V';
cf655043 1146 $av_pending = 'V';
6c72ffaa 1147
8e761b04
AW
1148 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1149 if (defined $2 && $type eq 'C' || $type eq 'T') {
1f65f947 1150 $av_pend_colon = 'B';
8e761b04
AW
1151 } elsif ($type eq 'E') {
1152 $av_pend_colon = 'L';
1f65f947
AW
1153 }
1154 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1155 $type = 'V';
1156
6c72ffaa 1157 } elsif ($cur =~ /^($Ident|$Constant)/o) {
c2fdda0d 1158 print "IDENT($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1159 $type = 'V';
1160
1161 } elsif ($cur =~ /^($Assignment)/o) {
c2fdda0d 1162 print "ASSIGN($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1163 $type = 'N';
1164
cf655043 1165 } elsif ($cur =~/^(;|{|})/) {
c2fdda0d 1166 print "END($1)\n" if ($dbg_values > 1);
13214adf 1167 $type = 'E';
1f65f947
AW
1168 $av_pend_colon = 'O';
1169
8e761b04
AW
1170 } elsif ($cur =~/^(,)/) {
1171 print "COMMA($1)\n" if ($dbg_values > 1);
1172 $type = 'C';
1173
1f65f947
AW
1174 } elsif ($cur =~ /^(\?)/o) {
1175 print "QUESTION($1)\n" if ($dbg_values > 1);
1176 $type = 'N';
1177
1178 } elsif ($cur =~ /^(:)/o) {
1179 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1180
1181 substr($var, length($res), 1, $av_pend_colon);
1182 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1183 $type = 'E';
1184 } else {
1185 $type = 'N';
1186 }
1187 $av_pend_colon = 'O';
13214adf 1188
8e761b04 1189 } elsif ($cur =~ /^(\[)/o) {
13214adf 1190 print "CLOSE($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1191 $type = 'N';
1192
0d413866 1193 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
74048ed8
AW
1194 my $variant;
1195
1196 print "OPV($1)\n" if ($dbg_values > 1);
1197 if ($type eq 'V') {
1198 $variant = 'B';
1199 } else {
1200 $variant = 'U';
1201 }
1202
1203 substr($var, length($res), 1, $variant);
1204 $type = 'N';
1205
6c72ffaa 1206 } elsif ($cur =~ /^($Operators)/o) {
c2fdda0d 1207 print "OP($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1208 if ($1 ne '++' && $1 ne '--') {
1209 $type = 'N';
1210 }
1211
1212 } elsif ($cur =~ /(^.)/o) {
c2fdda0d 1213 print "C($1)\n" if ($dbg_values > 1);
6c72ffaa
AW
1214 }
1215 if (defined $1) {
1216 $cur = substr($cur, length($1));
1217 $res .= $type x length($1);
1218 }
9c0ca6f9 1219 }
0a920b5b 1220
1f65f947 1221 return ($res, $var);
0a920b5b
AW
1222}
1223
8905a67c 1224sub possible {
13214adf 1225 my ($possible, $line) = @_;
9a974fdb 1226 my $notPermitted = qr{(?:
0776e594
AW
1227 ^(?:
1228 $Modifier|
1229 $Storage|
1230 $Type|
9a974fdb
AW
1231 DEFINE_\S+
1232 )$|
1233 ^(?:
0776e594
AW
1234 goto|
1235 return|
1236 case|
1237 else|
1238 asm|__asm__|
89a88353
AW
1239 do|
1240 \#|
1241 \#\#|
9a974fdb 1242 )(?:\s|$)|
0776e594 1243 ^(?:typedef|struct|enum)\b
9a974fdb
AW
1244 )}x;
1245 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1246 if ($possible !~ $notPermitted) {
c45dcabd
AW
1247 # Check for modifiers.
1248 $possible =~ s/\s*$Storage\s*//g;
1249 $possible =~ s/\s*$Sparse\s*//g;
1250 if ($possible =~ /^\s*$/) {
1251
1252 } elsif ($possible =~ /\s/) {
1253 $possible =~ s/\s*$Type\s*//g;
d2506586 1254 for my $modifier (split(' ', $possible)) {
9a974fdb
AW
1255 if ($modifier !~ $notPermitted) {
1256 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1257 push(@modifierList, $modifier);
1258 }
d2506586 1259 }
c45dcabd
AW
1260
1261 } else {
1262 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1263 push(@typeList, $possible);
1264 }
8905a67c 1265 build_types();
0776e594
AW
1266 } else {
1267 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
8905a67c
AW
1268 }
1269}
1270
6c72ffaa
AW
1271my $prefix = '';
1272
000d1cc1
JP
1273sub show_type {
1274 return !defined $ignore_type{$_[0]};
1275}
1276
f0a594c1 1277sub report {
000d1cc1
JP
1278 if (!show_type($_[1]) ||
1279 (defined $tst_only && $_[2] !~ /\Q$tst_only\E/)) {
773647a0
AW
1280 return 0;
1281 }
000d1cc1
JP
1282 my $line;
1283 if ($show_types) {
1284 $line = "$prefix$_[0]:$_[1]: $_[2]\n";
1285 } else {
1286 $line = "$prefix$_[0]: $_[2]\n";
1287 }
8905a67c
AW
1288 $line = (split('\n', $line))[0] . "\n" if ($terse);
1289
13214adf 1290 push(our @report, $line);
773647a0
AW
1291
1292 return 1;
f0a594c1
AW
1293}
1294sub report_dump {
13214adf 1295 our @report;
f0a594c1 1296}
000d1cc1 1297
de7d4f0e 1298sub ERROR {
000d1cc1 1299 if (report("ERROR", $_[0], $_[1])) {
773647a0
AW
1300 our $clean = 0;
1301 our $cnt_error++;
3705ce5b 1302 return 1;
773647a0 1303 }
3705ce5b 1304 return 0;
de7d4f0e
AW
1305}
1306sub WARN {
000d1cc1 1307 if (report("WARNING", $_[0], $_[1])) {
773647a0
AW
1308 our $clean = 0;
1309 our $cnt_warn++;
3705ce5b 1310 return 1;
773647a0 1311 }
3705ce5b 1312 return 0;
de7d4f0e
AW
1313}
1314sub CHK {
000d1cc1 1315 if ($check && report("CHECK", $_[0], $_[1])) {
6c72ffaa
AW
1316 our $clean = 0;
1317 our $cnt_chk++;
3705ce5b 1318 return 1;
6c72ffaa 1319 }
3705ce5b 1320 return 0;
de7d4f0e
AW
1321}
1322
6ecd9674
AW
1323sub check_absolute_file {
1324 my ($absolute, $herecurr) = @_;
1325 my $file = $absolute;
1326
1327 ##print "absolute<$absolute>\n";
1328
1329 # See if any suffix of this path is a path within the tree.
1330 while ($file =~ s@^[^/]*/@@) {
1331 if (-f "$root/$file") {
1332 ##print "file<$file>\n";
1333 last;
1334 }
1335 }
1336 if (! -f _) {
1337 return 0;
1338 }
1339
1340 # It is, so see if the prefix is acceptable.
1341 my $prefix = $absolute;
1342 substr($prefix, -length($file)) = '';
1343
1344 ##print "prefix<$prefix>\n";
1345 if ($prefix ne ".../") {
000d1cc1
JP
1346 WARN("USE_RELATIVE_PATH",
1347 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
6ecd9674
AW
1348 }
1349}
1350
3705ce5b
JP
1351sub trim {
1352 my ($string) = @_;
1353
1354 $string =~ s/(^\s+|\s+$)//g;
1355
1356 return $string;
1357}
1358
1359sub tabify {
1360 my ($leading) = @_;
1361
1362 my $source_indent = 8;
1363 my $max_spaces_before_tab = $source_indent - 1;
1364 my $spaces_to_tab = " " x $source_indent;
1365
1366 #convert leading spaces to tabs
1367 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1368 #Remove spaces before a tab
1369 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1370
1371 return "$leading";
1372}
1373
d1fe9c09
JP
1374sub pos_last_openparen {
1375 my ($line) = @_;
1376
1377 my $pos = 0;
1378
1379 my $opens = $line =~ tr/\(/\(/;
1380 my $closes = $line =~ tr/\)/\)/;
1381
1382 my $last_openparen = 0;
1383
1384 if (($opens == 0) || ($closes >= $opens)) {
1385 return -1;
1386 }
1387
1388 my $len = length($line);
1389
1390 for ($pos = 0; $pos < $len; $pos++) {
1391 my $string = substr($line, $pos);
1392 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1393 $pos += length($1) - 1;
1394 } elsif (substr($line, $pos, 1) eq '(') {
1395 $last_openparen = $pos;
1396 } elsif (index($string, '(') == -1) {
1397 last;
1398 }
1399 }
1400
1401 return $last_openparen + 1;
1402}
1403
0a920b5b
AW
1404sub process {
1405 my $filename = shift;
0a920b5b
AW
1406
1407 my $linenr=0;
1408 my $prevline="";
c2fdda0d 1409 my $prevrawline="";
0a920b5b 1410 my $stashline="";
c2fdda0d 1411 my $stashrawline="";
0a920b5b 1412
4a0df2ef 1413 my $length;
0a920b5b
AW
1414 my $indent;
1415 my $previndent=0;
1416 my $stashindent=0;
1417
de7d4f0e 1418 our $clean = 1;
0a920b5b
AW
1419 my $signoff = 0;
1420 my $is_patch = 0;
1421
15662b3e
JP
1422 my $in_header_lines = 1;
1423 my $in_commit_log = 0; #Scanning lines before patch
1424
fa64205d
PS
1425 my $non_utf8_charset = 0;
1426
13214adf 1427 our @report = ();
6c72ffaa
AW
1428 our $cnt_lines = 0;
1429 our $cnt_error = 0;
1430 our $cnt_warn = 0;
1431 our $cnt_chk = 0;
1432
0a920b5b
AW
1433 # Trace the real file/line as we go.
1434 my $realfile = '';
1435 my $realline = 0;
1436 my $realcnt = 0;
1437 my $here = '';
1438 my $in_comment = 0;
c2fdda0d 1439 my $comment_edge = 0;
0a920b5b 1440 my $first_line = 0;
1e855726 1441 my $p1_prefix = '';
0a920b5b 1442
13214adf
AW
1443 my $prev_values = 'E';
1444
1445 # suppression flags
773647a0 1446 my %suppress_ifbraces;
170d3a22 1447 my %suppress_whiletrailers;
2b474a1a 1448 my %suppress_export;
3e469cdc 1449 my $suppress_statement = 0;
653d4876 1450
323c1260
JP
1451 my %camelcase = ();
1452
c2fdda0d 1453 # Pre-scan the patch sanitizing the lines.
de7d4f0e 1454 # Pre-scan the patch looking for any __setup documentation.
c2fdda0d 1455 #
de7d4f0e
AW
1456 my @setup_docs = ();
1457 my $setup_docs = 0;
773647a0
AW
1458
1459 sanitise_line_reset();
c2fdda0d
AW
1460 my $line;
1461 foreach my $rawline (@rawlines) {
773647a0
AW
1462 $linenr++;
1463 $line = $rawline;
c2fdda0d 1464
3705ce5b
JP
1465 push(@fixed, $rawline) if ($fix);
1466
773647a0 1467 if ($rawline=~/^\+\+\+\s+(\S+)/) {
de7d4f0e
AW
1468 $setup_docs = 0;
1469 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1470 $setup_docs = 1;
1471 }
773647a0
AW
1472 #next;
1473 }
1474 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1475 $realline=$1-1;
1476 if (defined $2) {
1477 $realcnt=$3+1;
1478 } else {
1479 $realcnt=1+1;
1480 }
c45dcabd 1481 $in_comment = 0;
773647a0
AW
1482
1483 # Guestimate if this is a continuing comment. Run
1484 # the context looking for a comment "edge". If this
1485 # edge is a close comment then we must be in a comment
1486 # at context start.
1487 my $edge;
01fa9147
AW
1488 my $cnt = $realcnt;
1489 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1490 next if (defined $rawlines[$ln - 1] &&
1491 $rawlines[$ln - 1] =~ /^-/);
1492 $cnt--;
1493 #print "RAW<$rawlines[$ln - 1]>\n";
721c1cb6 1494 last if (!defined $rawlines[$ln - 1]);
fae17dae
AW
1495 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1496 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1497 ($edge) = $1;
1498 last;
1499 }
773647a0
AW
1500 }
1501 if (defined $edge && $edge eq '*/') {
1502 $in_comment = 1;
1503 }
1504
1505 # Guestimate if this is a continuing comment. If this
1506 # is the start of a diff block and this line starts
1507 # ' *' then it is very likely a comment.
1508 if (!defined $edge &&
83242e0c 1509 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
773647a0
AW
1510 {
1511 $in_comment = 1;
1512 }
1513
1514 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1515 sanitise_line_reset($in_comment);
1516
171ae1a4 1517 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
773647a0 1518 # Standardise the strings and chars within the input to
171ae1a4 1519 # simplify matching -- only bother with positive lines.
773647a0 1520 $line = sanitise_line($rawline);
de7d4f0e 1521 }
773647a0
AW
1522 push(@lines, $line);
1523
1524 if ($realcnt > 1) {
1525 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1526 } else {
1527 $realcnt = 0;
1528 }
1529
1530 #print "==>$rawline\n";
1531 #print "-->$line\n";
de7d4f0e
AW
1532
1533 if ($setup_docs && $line =~ /^\+/) {
1534 push(@setup_docs, $line);
1535 }
1536 }
1537
6c72ffaa
AW
1538 $prefix = '';
1539
773647a0
AW
1540 $realcnt = 0;
1541 $linenr = 0;
0a920b5b
AW
1542 foreach my $line (@lines) {
1543 $linenr++;
1544
c2fdda0d 1545 my $rawline = $rawlines[$linenr - 1];
6c72ffaa 1546
0a920b5b 1547#extract the line range in the file after the patch is applied
6c72ffaa 1548 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
0a920b5b 1549 $is_patch = 1;
4a0df2ef 1550 $first_line = $linenr + 1;
0a920b5b
AW
1551 $realline=$1-1;
1552 if (defined $2) {
1553 $realcnt=$3+1;
1554 } else {
1555 $realcnt=1+1;
1556 }
c2fdda0d 1557 annotate_reset();
13214adf
AW
1558 $prev_values = 'E';
1559
773647a0 1560 %suppress_ifbraces = ();
170d3a22 1561 %suppress_whiletrailers = ();
2b474a1a 1562 %suppress_export = ();
3e469cdc 1563 $suppress_statement = 0;
0a920b5b 1564 next;
0a920b5b 1565
4a0df2ef
AW
1566# track the line number as we move through the hunk, note that
1567# new versions of GNU diff omit the leading space on completely
1568# blank context lines so we need to count that too.
773647a0 1569 } elsif ($line =~ /^( |\+|$)/) {
0a920b5b 1570 $realline++;
d8aaf121 1571 $realcnt-- if ($realcnt != 0);
0a920b5b 1572
4a0df2ef 1573 # Measure the line length and indent.
c2fdda0d 1574 ($length, $indent) = line_stats($rawline);
0a920b5b
AW
1575
1576 # Track the previous line.
1577 ($prevline, $stashline) = ($stashline, $line);
1578 ($previndent, $stashindent) = ($stashindent, $indent);
c2fdda0d
AW
1579 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1580
773647a0 1581 #warn "line<$line>\n";
6c72ffaa 1582
d8aaf121
AW
1583 } elsif ($realcnt == 1) {
1584 $realcnt--;
0a920b5b
AW
1585 }
1586
cc77cdca
AW
1587 my $hunk_line = ($realcnt != 0);
1588
0a920b5b 1589#make up the handle for any error we report on this line
773647a0
AW
1590 $prefix = "$filename:$realline: " if ($emacs && $file);
1591 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1592
6c72ffaa
AW
1593 $here = "#$linenr: " if (!$file);
1594 $here = "#$realline: " if ($file);
773647a0
AW
1595
1596 # extract the filename as it passes
3bf9a009
RV
1597 if ($line =~ /^diff --git.*?(\S+)$/) {
1598 $realfile = $1;
1599 $realfile =~ s@^([^/]*)/@@;
270c49a0 1600 $in_commit_log = 0;
3bf9a009 1601 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
773647a0 1602 $realfile = $1;
1e855726 1603 $realfile =~ s@^([^/]*)/@@;
270c49a0 1604 $in_commit_log = 0;
1e855726
WS
1605
1606 $p1_prefix = $1;
e2f7aa4b
AW
1607 if (!$file && $tree && $p1_prefix ne '' &&
1608 -e "$root/$p1_prefix") {
000d1cc1
JP
1609 WARN("PATCH_PREFIX",
1610 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1e855726 1611 }
773647a0 1612
c1ab3326 1613 if ($realfile =~ m@^include/asm/@) {
000d1cc1
JP
1614 ERROR("MODIFIED_INCLUDE_ASM",
1615 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
773647a0
AW
1616 }
1617 next;
1618 }
1619
389834b6 1620 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
0a920b5b 1621
c2fdda0d
AW
1622 my $hereline = "$here\n$rawline\n";
1623 my $herecurr = "$here\n$rawline\n";
1624 my $hereprev = "$here\n$prevrawline\n$rawline\n";
0a920b5b 1625
6c72ffaa
AW
1626 $cnt_lines++ if ($realcnt != 0);
1627
3bf9a009
RV
1628# Check for incorrect file permissions
1629 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1630 my $permhere = $here . "FILE: $realfile\n";
04db4d25
JP
1631 if ($realfile !~ m@scripts/@ &&
1632 $realfile !~ /\.(py|pl|awk|sh)$/) {
000d1cc1
JP
1633 ERROR("EXECUTE_PERMISSIONS",
1634 "do not set execute permissions for source files\n" . $permhere);
3bf9a009
RV
1635 }
1636 }
1637
20112475 1638# Check the patch for a signoff:
d8aaf121 1639 if ($line =~ /^\s*signed-off-by:/i) {
4a0df2ef 1640 $signoff++;
15662b3e 1641 $in_commit_log = 0;
20112475
JP
1642 }
1643
1644# Check signature styles
270c49a0 1645 if (!$in_header_lines &&
ce0338df 1646 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
20112475
JP
1647 my $space_before = $1;
1648 my $sign_off = $2;
1649 my $space_after = $3;
1650 my $email = $4;
1651 my $ucfirst_sign_off = ucfirst(lc($sign_off));
1652
ce0338df
JP
1653 if ($sign_off !~ /$signature_tags/) {
1654 WARN("BAD_SIGN_OFF",
1655 "Non-standard signature: $sign_off\n" . $herecurr);
1656 }
20112475 1657 if (defined $space_before && $space_before ne "") {
3705ce5b
JP
1658 if (WARN("BAD_SIGN_OFF",
1659 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
1660 $fix) {
1661 $fixed[$linenr - 1] =
1662 "$ucfirst_sign_off $email";
1663 }
20112475
JP
1664 }
1665 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
3705ce5b
JP
1666 if (WARN("BAD_SIGN_OFF",
1667 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
1668 $fix) {
1669 $fixed[$linenr - 1] =
1670 "$ucfirst_sign_off $email";
1671 }
1672
20112475
JP
1673 }
1674 if (!defined $space_after || $space_after ne " ") {
3705ce5b
JP
1675 if (WARN("BAD_SIGN_OFF",
1676 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
1677 $fix) {
1678 $fixed[$linenr - 1] =
1679 "$ucfirst_sign_off $email";
1680 }
0a920b5b 1681 }
20112475
JP
1682
1683 my ($email_name, $email_address, $comment) = parse_email($email);
1684 my $suggested_email = format_email(($email_name, $email_address));
1685 if ($suggested_email eq "") {
000d1cc1
JP
1686 ERROR("BAD_SIGN_OFF",
1687 "Unrecognized email address: '$email'\n" . $herecurr);
20112475
JP
1688 } else {
1689 my $dequoted = $suggested_email;
1690 $dequoted =~ s/^"//;
1691 $dequoted =~ s/" </ </;
1692 # Don't force email to have quotes
1693 # Allow just an angle bracketed address
1694 if ("$dequoted$comment" ne $email &&
1695 "<$email_address>$comment" ne $email &&
1696 "$suggested_email$comment" ne $email) {
000d1cc1
JP
1697 WARN("BAD_SIGN_OFF",
1698 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
20112475 1699 }
0a920b5b
AW
1700 }
1701 }
1702
00df344f 1703# Check for wrappage within a valid hunk of the file
8905a67c 1704 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
000d1cc1
JP
1705 ERROR("CORRUPTED_PATCH",
1706 "patch seems to be corrupt (line wrapped?)\n" .
6c72ffaa 1707 $herecurr) if (!$emitted_corrupt++);
de7d4f0e
AW
1708 }
1709
6ecd9674
AW
1710# Check for absolute kernel paths.
1711 if ($tree) {
1712 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1713 my $file = $1;
1714
1715 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1716 check_absolute_file($1, $herecurr)) {
1717 #
1718 } else {
1719 check_absolute_file($file, $herecurr);
1720 }
1721 }
1722 }
1723
de7d4f0e
AW
1724# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1725 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
171ae1a4
AW
1726 $rawline !~ m/^$UTF8*$/) {
1727 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1728
1729 my $blank = copy_spacing($rawline);
1730 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1731 my $hereptr = "$hereline$ptr\n";
1732
34d99219
JP
1733 CHK("INVALID_UTF8",
1734 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
00df344f
AW
1735 }
1736
15662b3e
JP
1737# Check if it's the start of a commit log
1738# (not a header line and we haven't seen the patch filename)
1739 if ($in_header_lines && $realfile =~ /^$/ &&
270c49a0 1740 $rawline !~ /^(commit\b|from\b|[\w-]+:).+$/i) {
15662b3e
JP
1741 $in_header_lines = 0;
1742 $in_commit_log = 1;
1743 }
1744
fa64205d
PS
1745# Check if there is UTF-8 in a commit log when a mail header has explicitly
1746# declined it, i.e defined some charset where it is missing.
1747 if ($in_header_lines &&
1748 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
1749 $1 !~ /utf-8/i) {
1750 $non_utf8_charset = 1;
1751 }
1752
1753 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
15662b3e 1754 $rawline =~ /$NON_ASCII_UTF8/) {
fa64205d 1755 WARN("UTF8_BEFORE_PATCH",
15662b3e
JP
1756 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
1757 }
1758
30670854
AW
1759# ignore non-hunk lines and lines being removed
1760 next if (!$hunk_line || $line =~ /^-/);
0a920b5b 1761
0a920b5b 1762#trailing whitespace
9c0ca6f9 1763 if ($line =~ /^\+.*\015/) {
c2fdda0d 1764 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
000d1cc1
JP
1765 ERROR("DOS_LINE_ENDINGS",
1766 "DOS line endings\n" . $herevet);
9c0ca6f9 1767
c2fdda0d
AW
1768 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1769 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3705ce5b
JP
1770 if (ERROR("TRAILING_WHITESPACE",
1771 "trailing whitespace\n" . $herevet) &&
1772 $fix) {
1773 $fixed[$linenr - 1] =~ s/^(\+.*?)\s+$/$1/;
1774 }
1775
d2c0a235 1776 $rpt_cleaners = 1;
0a920b5b 1777 }
5368df20 1778
3354957a 1779# check for Kconfig help text having a real description
9fe287d7
AW
1780# Only applies when adding the entry originally, after that we do not have
1781# sufficient context to determine whether it is indeed long enough.
3354957a 1782 if ($realfile =~ /Kconfig/ &&
a1385803 1783 $line =~ /.\s*config\s+/) {
3354957a 1784 my $length = 0;
9fe287d7
AW
1785 my $cnt = $realcnt;
1786 my $ln = $linenr + 1;
1787 my $f;
a1385803 1788 my $is_start = 0;
9fe287d7 1789 my $is_end = 0;
a1385803 1790 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
9fe287d7
AW
1791 $f = $lines[$ln - 1];
1792 $cnt-- if ($lines[$ln - 1] !~ /^-/);
1793 $is_end = $lines[$ln - 1] =~ /^\+/;
9fe287d7
AW
1794
1795 next if ($f =~ /^-/);
a1385803
AW
1796
1797 if ($lines[$ln - 1] =~ /.\s*(?:bool|tristate)\s*\"/) {
1798 $is_start = 1;
1799 } elsif ($lines[$ln - 1] =~ /.\s*(?:---)?help(?:---)?$/) {
1800 $length = -1;
1801 }
1802
9fe287d7 1803 $f =~ s/^.//;
3354957a
AK
1804 $f =~ s/#.*//;
1805 $f =~ s/^\s+//;
1806 next if ($f =~ /^$/);
9fe287d7
AW
1807 if ($f =~ /^\s*config\s/) {
1808 $is_end = 1;
1809 last;
1810 }
3354957a
AK
1811 $length++;
1812 }
000d1cc1 1813 WARN("CONFIG_DESCRIPTION",
a1385803
AW
1814 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4);
1815 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
3354957a
AK
1816 }
1817
1ba8dfd1
KC
1818# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1819 if ($realfile =~ /Kconfig/ &&
1820 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1821 WARN("CONFIG_EXPERIMENTAL",
1822 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1823 }
1824
c68e5878
AL
1825 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1826 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1827 my $flag = $1;
1828 my $replacement = {
1829 'EXTRA_AFLAGS' => 'asflags-y',
1830 'EXTRA_CFLAGS' => 'ccflags-y',
1831 'EXTRA_CPPFLAGS' => 'cppflags-y',
1832 'EXTRA_LDFLAGS' => 'ldflags-y',
1833 };
1834
1835 WARN("DEPRECATED_VARIABLE",
1836 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
1837 }
1838
5368df20
AW
1839# check we are in a valid source file if not then ignore this hunk
1840 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1841
6cd7f386 1842#line length limit
c45dcabd 1843 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
f4c014c0 1844 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
0fccc622 1845 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
8bbea968 1846 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
6cd7f386 1847 $length > $max_line_length)
c45dcabd 1848 {
000d1cc1 1849 WARN("LONG_LINE",
6cd7f386 1850 "line over $max_line_length characters\n" . $herecurr);
0a920b5b
AW
1851 }
1852
ca56dc09
JT
1853# Check for user-visible strings broken across lines, which breaks the ability
1854# to grep for the string. Limited to strings used as parameters (those
1855# following an open parenthesis), which almost completely eliminates false
1856# positives, as well as warning only once per parameter rather than once per
1857# line of the string. Make an exception when the previous string ends in a
1858# newline (multiple lines in one string constant) or \n\t (common in inline
1859# assembly to indent the instruction on the following line).
1860 if ($line =~ /^\+\s*"/ &&
1861 $prevline =~ /"\s*$/ &&
1862 $prevline =~ /\(/ &&
1863 $prevrawline !~ /\\n(?:\\t)*"\s*$/) {
1864 WARN("SPLIT_STRING",
1865 "quoted string split across lines\n" . $hereprev);
1866 }
1867
5e79d96e
JP
1868# check for spaces before a quoted newline
1869 if ($rawline =~ /^.*\".*\s\\n/) {
3705ce5b
JP
1870 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
1871 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
1872 $fix) {
1873 $fixed[$linenr - 1] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
1874 }
1875
5e79d96e
JP
1876 }
1877
8905a67c
AW
1878# check for adding lines without a newline.
1879 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
000d1cc1
JP
1880 WARN("MISSING_EOF_NEWLINE",
1881 "adding a line without newline at end of file\n" . $herecurr);
8905a67c
AW
1882 }
1883
42e41c54
MF
1884# Blackfin: use hi/lo macros
1885 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
1886 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
1887 my $herevet = "$here\n" . cat_vet($line) . "\n";
000d1cc1
JP
1888 ERROR("LO_MACRO",
1889 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
42e41c54
MF
1890 }
1891 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
1892 my $herevet = "$here\n" . cat_vet($line) . "\n";
000d1cc1
JP
1893 ERROR("HI_MACRO",
1894 "use the HI() macro, not (... >> 16)\n" . $herevet);
42e41c54
MF
1895 }
1896 }
1897
b9ea10d6
AW
1898# check we are in a valid source file C or perl if not then ignore this hunk
1899 next if ($realfile !~ /\.(h|c|pl)$/);
0a920b5b
AW
1900
1901# at the beginning of a line any tabs must come first and anything
1902# more than 8 must use tabs.
c2fdda0d
AW
1903 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1904 $rawline =~ /^\+\s* \s*/) {
1905 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
d2c0a235 1906 $rpt_cleaners = 1;
3705ce5b
JP
1907 if (ERROR("CODE_INDENT",
1908 "code indent should use tabs where possible\n" . $herevet) &&
1909 $fix) {
1910 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
1911 }
0a920b5b
AW
1912 }
1913
08e44365
AP
1914# check for space before tabs.
1915 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
1916 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3705ce5b
JP
1917 if (WARN("SPACE_BEFORE_TAB",
1918 "please, no space before tabs\n" . $herevet) &&
1919 $fix) {
1920 $fixed[$linenr - 1] =~
1921 s/(^\+.*) +\t/$1\t/;
1922 }
08e44365
AP
1923 }
1924
d1fe9c09
JP
1925# check for && or || at the start of a line
1926 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1927 CHK("LOGICAL_CONTINUATIONS",
1928 "Logical continuations should be on the previous line\n" . $hereprev);
1929 }
1930
1931# check multi-line statement indentation matches previous line
1932 if ($^V && $^V ge 5.10.0 &&
1933 $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1934 $prevline =~ /^\+(\t*)(.*)$/;
1935 my $oldindent = $1;
1936 my $rest = $2;
1937
1938 my $pos = pos_last_openparen($rest);
1939 if ($pos >= 0) {
b34a26f3
JP
1940 $line =~ /^(\+| )([ \t]*)/;
1941 my $newindent = $2;
d1fe9c09
JP
1942
1943 my $goodtabindent = $oldindent .
1944 "\t" x ($pos / 8) .
1945 " " x ($pos % 8);
1946 my $goodspaceindent = $oldindent . " " x $pos;
1947
1948 if ($newindent ne $goodtabindent &&
1949 $newindent ne $goodspaceindent) {
3705ce5b
JP
1950
1951 if (CHK("PARENTHESIS_ALIGNMENT",
1952 "Alignment should match open parenthesis\n" . $hereprev) &&
1953 $fix && $line =~ /^\+/) {
1954 $fixed[$linenr - 1] =~
1955 s/^\+[ \t]*/\+$goodtabindent/;
1956 }
d1fe9c09
JP
1957 }
1958 }
1959 }
1960
23f780c9 1961 if ($line =~ /^\+.*\*[ \t]*\)[ \t]+(?!$Assignment|$Arithmetic)/) {
3705ce5b
JP
1962 if (CHK("SPACING",
1963 "No space is necessary after a cast\n" . $hereprev) &&
1964 $fix) {
1965 $fixed[$linenr - 1] =~
1966 s/^(\+.*\*[ \t]*\))[ \t]+/$1/;
1967 }
aad4f614
JP
1968 }
1969
05880600 1970 if ($realfile =~ m@^(drivers/net/|net/)@ &&
fdb4bcd6
JP
1971 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
1972 $rawline =~ /^\+[ \t]*\*/) {
05880600
JP
1973 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1974 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
1975 }
1976
1977 if ($realfile =~ m@^(drivers/net/|net/)@ &&
a605e32e
JP
1978 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
1979 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
1980 $rawline !~ /^\+[ \t]*\*/) { #no leading *
1981 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1982 "networking block comments start with * on subsequent lines\n" . $hereprev);
1983 }
1984
1985 if ($realfile =~ m@^(drivers/net/|net/)@ &&
c24f9f19
JP
1986 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
1987 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
1988 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
1989 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
05880600
JP
1990 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
1991 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
1992 }
1993
5f7ddae6 1994# check for spaces at the beginning of a line.
6b4c5beb
AW
1995# Exceptions:
1996# 1) within comments
1997# 2) indented preprocessor commands
1998# 3) hanging labels
3705ce5b 1999 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
5f7ddae6 2000 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3705ce5b
JP
2001 if (WARN("LEADING_SPACE",
2002 "please, no spaces at the start of a line\n" . $herevet) &&
2003 $fix) {
2004 $fixed[$linenr - 1] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2005 }
5f7ddae6
RR
2006 }
2007
b9ea10d6
AW
2008# check we are in a valid C source file if not then ignore this hunk
2009 next if ($realfile !~ /\.(h|c)$/);
2010
1ba8dfd1
KC
2011# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2012 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2013 WARN("CONFIG_EXPERIMENTAL",
2014 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2015 }
2016
c2fdda0d 2017# check for RCS/CVS revision markers
cf655043 2018 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
000d1cc1
JP
2019 WARN("CVS_KEYWORD",
2020 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
c2fdda0d 2021 }
22f2a2ef 2022
42e41c54
MF
2023# Blackfin: don't use __builtin_bfin_[cs]sync
2024 if ($line =~ /__builtin_bfin_csync/) {
2025 my $herevet = "$here\n" . cat_vet($line) . "\n";
000d1cc1
JP
2026 ERROR("CSYNC",
2027 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
42e41c54
MF
2028 }
2029 if ($line =~ /__builtin_bfin_ssync/) {
2030 my $herevet = "$here\n" . cat_vet($line) . "\n";
000d1cc1
JP
2031 ERROR("SSYNC",
2032 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
42e41c54
MF
2033 }
2034
56e77d70
JP
2035# check for old HOTPLUG __dev<foo> section markings
2036 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2037 WARN("HOTPLUG_SECTION",
2038 "Using $1 is unnecessary\n" . $herecurr);
2039 }
2040
9c0ca6f9 2041# Check for potential 'bare' types
2b474a1a
AW
2042 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2043 $realline_next);
3e469cdc
AW
2044#print "LINE<$line>\n";
2045 if ($linenr >= $suppress_statement &&
2046 $realcnt && $line =~ /.\s*\S/) {
170d3a22 2047 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
f5fe35dd 2048 ctx_statement_block($linenr, $realcnt, 0);
171ae1a4
AW
2049 $stat =~ s/\n./\n /g;
2050 $cond =~ s/\n./\n /g;
2051
3e469cdc
AW
2052#print "linenr<$linenr> <$stat>\n";
2053 # If this statement has no statement boundaries within
2054 # it there is no point in retrying a statement scan
2055 # until we hit end of it.
2056 my $frag = $stat; $frag =~ s/;+\s*$//;
2057 if ($frag !~ /(?:{|;)/) {
2058#print "skip<$line_nr_next>\n";
2059 $suppress_statement = $line_nr_next;
2060 }
f74bd194 2061
2b474a1a
AW
2062 # Find the real next line.
2063 $realline_next = $line_nr_next;
2064 if (defined $realline_next &&
2065 (!defined $lines[$realline_next - 1] ||
2066 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2067 $realline_next++;
2068 }
2069
171ae1a4
AW
2070 my $s = $stat;
2071 $s =~ s/{.*$//s;
cf655043 2072
c2fdda0d 2073 # Ignore goto labels.
171ae1a4 2074 if ($s =~ /$Ident:\*$/s) {
c2fdda0d
AW
2075
2076 # Ignore functions being called
171ae1a4 2077 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
c2fdda0d 2078
463f2864
AW
2079 } elsif ($s =~ /^.\s*else\b/s) {
2080
c45dcabd 2081 # declarations always start with types
d2506586 2082 } 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
2083 my $type = $1;
2084 $type =~ s/\s+/ /g;
2085 possible($type, "A:" . $s);
2086
8905a67c 2087 # definitions in global scope can only start with types
a6a84062 2088 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
c45dcabd 2089 possible($1, "B:" . $s);
c2fdda0d 2090 }
8905a67c
AW
2091
2092 # any (foo ... *) is a pointer cast, and foo is a type
65863862 2093 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
c45dcabd 2094 possible($1, "C:" . $s);
8905a67c
AW
2095 }
2096
2097 # Check for any sort of function declaration.
2098 # int foo(something bar, other baz);
2099 # void (*store_gdt)(x86_descr_ptr *);
171ae1a4 2100 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
8905a67c 2101 my ($name_len) = length($1);
8905a67c 2102
cf655043 2103 my $ctx = $s;
773647a0 2104 substr($ctx, 0, $name_len + 1, '');
8905a67c 2105 $ctx =~ s/\)[^\)]*$//;
cf655043 2106
8905a67c 2107 for my $arg (split(/\s*,\s*/, $ctx)) {
c45dcabd 2108 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
8905a67c 2109
c45dcabd 2110 possible($1, "D:" . $s);
8905a67c
AW
2111 }
2112 }
9c0ca6f9 2113 }
8905a67c 2114
9c0ca6f9
AW
2115 }
2116
653d4876
AW
2117#
2118# Checks which may be anchored in the context.
2119#
00df344f 2120
653d4876
AW
2121# Check for switch () and associated case and default
2122# statements should be at the same indent.
00df344f
AW
2123 if ($line=~/\bswitch\s*\(.*\)/) {
2124 my $err = '';
2125 my $sep = '';
2126 my @ctx = ctx_block_outer($linenr, $realcnt);
2127 shift(@ctx);
2128 for my $ctx (@ctx) {
2129 my ($clen, $cindent) = line_stats($ctx);
2130 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2131 $indent != $cindent) {
2132 $err .= "$sep$ctx\n";
2133 $sep = '';
2134 } else {
2135 $sep = "[...]\n";
2136 }
2137 }
2138 if ($err ne '') {
000d1cc1
JP
2139 ERROR("SWITCH_CASE_INDENT_LEVEL",
2140 "switch and case should be at the same indent\n$hereline$err");
de7d4f0e
AW
2141 }
2142 }
2143
2144# if/while/etc brace do not go on next line, unless defining a do while loop,
2145# or if that brace on the next line is for something else
c45dcabd 2146 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
773647a0
AW
2147 my $pre_ctx = "$1$2";
2148
9c0ca6f9 2149 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
8eef05dd
JP
2150
2151 if ($line =~ /^\+\t{6,}/) {
2152 WARN("DEEP_INDENTATION",
2153 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2154 }
2155
de7d4f0e
AW
2156 my $ctx_cnt = $realcnt - $#ctx - 1;
2157 my $ctx = join("\n", @ctx);
2158
548596d5
AW
2159 my $ctx_ln = $linenr;
2160 my $ctx_skip = $realcnt;
773647a0 2161
548596d5
AW
2162 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2163 defined $lines[$ctx_ln - 1] &&
2164 $lines[$ctx_ln - 1] =~ /^-/)) {
2165 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2166 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
de7d4f0e 2167 $ctx_ln++;
de7d4f0e 2168 }
548596d5 2169
53210168
AW
2170 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2171 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
de7d4f0e 2172
773647a0 2173 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
000d1cc1
JP
2174 ERROR("OPEN_BRACE",
2175 "that open brace { should be on the previous line\n" .
01464f30 2176 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
00df344f 2177 }
773647a0
AW
2178 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2179 $ctx =~ /\)\s*\;\s*$/ &&
2180 defined $lines[$ctx_ln - 1])
2181 {
9c0ca6f9
AW
2182 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2183 if ($nindent > $indent) {
000d1cc1
JP
2184 WARN("TRAILING_SEMICOLON",
2185 "trailing semicolon indicates no statements, indent implies otherwise\n" .
01464f30 2186 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
9c0ca6f9
AW
2187 }
2188 }
00df344f
AW
2189 }
2190
4d001e4d
AW
2191# Check relative indent for conditionals and blocks.
2192 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
3e469cdc
AW
2193 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2194 ctx_statement_block($linenr, $realcnt, 0)
2195 if (!defined $stat);
4d001e4d
AW
2196 my ($s, $c) = ($stat, $cond);
2197
2198 substr($s, 0, length($c), '');
2199
2200 # Make sure we remove the line prefixes as we have
2201 # none on the first line, and are going to readd them
2202 # where necessary.
2203 $s =~ s/\n./\n/gs;
2204
2205 # Find out how long the conditional actually is.
6f779c18
AW
2206 my @newlines = ($c =~ /\n/gs);
2207 my $cond_lines = 1 + $#newlines;
4d001e4d
AW
2208
2209 # We want to check the first line inside the block
2210 # starting at the end of the conditional, so remove:
2211 # 1) any blank line termination
2212 # 2) any opening brace { on end of the line
2213 # 3) any do (...) {
2214 my $continuation = 0;
2215 my $check = 0;
2216 $s =~ s/^.*\bdo\b//;
2217 $s =~ s/^\s*{//;
2218 if ($s =~ s/^\s*\\//) {
2219 $continuation = 1;
2220 }
9bd49efe 2221 if ($s =~ s/^\s*?\n//) {
4d001e4d
AW
2222 $check = 1;
2223 $cond_lines++;
2224 }
2225
2226 # Also ignore a loop construct at the end of a
2227 # preprocessor statement.
2228 if (($prevline =~ /^.\s*#\s*define\s/ ||
2229 $prevline =~ /\\\s*$/) && $continuation == 0) {
2230 $check = 0;
2231 }
2232
9bd49efe 2233 my $cond_ptr = -1;
740504c6 2234 $continuation = 0;
9bd49efe
AW
2235 while ($cond_ptr != $cond_lines) {
2236 $cond_ptr = $cond_lines;
2237
f16fa28f
AW
2238 # If we see an #else/#elif then the code
2239 # is not linear.
2240 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2241 $check = 0;
2242 }
2243
9bd49efe
AW
2244 # Ignore:
2245 # 1) blank lines, they should be at 0,
2246 # 2) preprocessor lines, and
2247 # 3) labels.
740504c6
AW
2248 if ($continuation ||
2249 $s =~ /^\s*?\n/ ||
9bd49efe
AW
2250 $s =~ /^\s*#\s*?/ ||
2251 $s =~ /^\s*$Ident\s*:/) {
740504c6 2252 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
30dad6eb
AW
2253 if ($s =~ s/^.*?\n//) {
2254 $cond_lines++;
2255 }
9bd49efe 2256 }
4d001e4d
AW
2257 }
2258
2259 my (undef, $sindent) = line_stats("+" . $s);
2260 my $stat_real = raw_line($linenr, $cond_lines);
2261
2262 # Check if either of these lines are modified, else
2263 # this is not this patch's fault.
2264 if (!defined($stat_real) ||
2265 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2266 $check = 0;
2267 }
2268 if (defined($stat_real) && $cond_lines > 1) {
2269 $stat_real = "[...]\n$stat_real";
2270 }
2271
9bd49efe 2272 #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
2273
2274 if ($check && (($sindent % 8) != 0 ||
2275 ($sindent <= $indent && $s ne ''))) {
000d1cc1
JP
2276 WARN("SUSPECT_CODE_INDENT",
2277 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
4d001e4d
AW
2278 }
2279 }
2280
6c72ffaa
AW
2281 # Track the 'values' across context and added lines.
2282 my $opline = $line; $opline =~ s/^./ /;
1f65f947
AW
2283 my ($curr_values, $curr_vars) =
2284 annotate_values($opline . "\n", $prev_values);
6c72ffaa 2285 $curr_values = $prev_values . $curr_values;
c2fdda0d
AW
2286 if ($dbg_values) {
2287 my $outline = $opline; $outline =~ s/\t/ /g;
cf655043
AW
2288 print "$linenr > .$outline\n";
2289 print "$linenr > $curr_values\n";
1f65f947 2290 print "$linenr > $curr_vars\n";
c2fdda0d 2291 }
6c72ffaa
AW
2292 $prev_values = substr($curr_values, -1);
2293
00df344f 2294#ignore lines not being added
3705ce5b 2295 next if ($line =~ /^[^\+]/);
00df344f 2296
653d4876 2297# TEST: allow direct testing of the type matcher.
7429c690
AW
2298 if ($dbg_type) {
2299 if ($line =~ /^.\s*$Declare\s*$/) {
000d1cc1
JP
2300 ERROR("TEST_TYPE",
2301 "TEST: is type\n" . $herecurr);
7429c690 2302 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
000d1cc1
JP
2303 ERROR("TEST_NOT_TYPE",
2304 "TEST: is not type ($1 is)\n". $herecurr);
7429c690 2305 }
653d4876
AW
2306 next;
2307 }
a1ef277e
AW
2308# TEST: allow direct testing of the attribute matcher.
2309 if ($dbg_attr) {
9360b0e5 2310 if ($line =~ /^.\s*$Modifier\s*$/) {
000d1cc1
JP
2311 ERROR("TEST_ATTR",
2312 "TEST: is attr\n" . $herecurr);
9360b0e5 2313 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
000d1cc1
JP
2314 ERROR("TEST_NOT_ATTR",
2315 "TEST: is not attr ($1 is)\n". $herecurr);
a1ef277e
AW
2316 }
2317 next;
2318 }
653d4876 2319
f0a594c1 2320# check for initialisation to aggregates open brace on the next line
99423c20
AW
2321 if ($line =~ /^.\s*{/ &&
2322 $prevline =~ /(?:^|[^=])=\s*$/) {
000d1cc1
JP
2323 ERROR("OPEN_BRACE",
2324 "that open brace { should be on the previous line\n" . $hereprev);
f0a594c1
AW
2325 }
2326
653d4876
AW
2327#
2328# Checks which are anchored on the added line.
2329#
2330
2331# check for malformed paths in #include statements (uses RAW line)
c45dcabd 2332 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
653d4876
AW
2333 my $path = $1;
2334 if ($path =~ m{//}) {
000d1cc1 2335 ERROR("MALFORMED_INCLUDE",
495e9d84
JP
2336 "malformed #include filename\n" . $herecurr);
2337 }
2338 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2339 ERROR("UAPI_INCLUDE",
2340 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
653d4876 2341 }
653d4876 2342 }
00df344f 2343
0a920b5b 2344# no C99 // comments
00df344f 2345 if ($line =~ m{//}) {
3705ce5b
JP
2346 if (ERROR("C99_COMMENTS",
2347 "do not use C99 // comments\n" . $herecurr) &&
2348 $fix) {
2349 my $line = $fixed[$linenr - 1];
2350 if ($line =~ /\/\/(.*)$/) {
2351 my $comment = trim($1);
2352 $fixed[$linenr - 1] =~ s@\/\/(.*)$@/\* $comment \*/@;
2353 }
2354 }
0a920b5b 2355 }
00df344f 2356 # Remove C99 comments.
0a920b5b 2357 $line =~ s@//.*@@;
6c72ffaa 2358 $opline =~ s@//.*@@;
0a920b5b 2359
2b474a1a
AW
2360# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
2361# the whole statement.
2362#print "APW <$lines[$realline_next - 1]>\n";
2363 if (defined $realline_next &&
2364 exists $lines[$realline_next - 1] &&
2365 !defined $suppress_export{$realline_next} &&
2366 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2367 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3cbf62df
AW
2368 # Handle definitions which produce identifiers with
2369 # a prefix:
2370 # XXX(foo);
2371 # EXPORT_SYMBOL(something_foo);
653d4876 2372 my $name = $1;
87a53877 2373 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
3cbf62df
AW
2374 $name =~ /^${Ident}_$2/) {
2375#print "FOO C name<$name>\n";
2376 $suppress_export{$realline_next} = 1;
2377
2378 } elsif ($stat !~ /(?:
2b474a1a 2379 \n.}\s*$|
48012058
AW
2380 ^.DEFINE_$Ident\(\Q$name\E\)|
2381 ^.DECLARE_$Ident\(\Q$name\E\)|
2382 ^.LIST_HEAD\(\Q$name\E\)|
2b474a1a
AW
2383 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
2384 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
48012058 2385 )/x) {
2b474a1a
AW
2386#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
2387 $suppress_export{$realline_next} = 2;
2388 } else {
2389 $suppress_export{$realline_next} = 1;
0a920b5b
AW
2390 }
2391 }
2b474a1a
AW
2392 if (!defined $suppress_export{$linenr} &&
2393 $prevline =~ /^.\s*$/ &&
2394 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
2395 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
2396#print "FOO B <$lines[$linenr - 1]>\n";
2397 $suppress_export{$linenr} = 2;
2398 }
2399 if (defined $suppress_export{$linenr} &&
2400 $suppress_export{$linenr} == 2) {
000d1cc1
JP
2401 WARN("EXPORT_SYMBOL",
2402 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
2b474a1a 2403 }
0a920b5b 2404
5150bda4 2405# check for global initialisers.
c45dcabd 2406 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
000d1cc1
JP
2407 ERROR("GLOBAL_INITIALISERS",
2408 "do not initialise globals to 0 or NULL\n" .
f0a594c1
AW
2409 $herecurr);
2410 }
653d4876 2411# check for static initialisers.
2d1bafd7 2412 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
000d1cc1
JP
2413 ERROR("INITIALISED_STATIC",
2414 "do not initialise statics to 0 or NULL\n" .
de7d4f0e 2415 $herecurr);
0a920b5b
AW
2416 }
2417
cb710eca
JP
2418# check for static const char * arrays.
2419 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
000d1cc1
JP
2420 WARN("STATIC_CONST_CHAR_ARRAY",
2421 "static const char * array should probably be static const char * const\n" .
cb710eca
JP
2422 $herecurr);
2423 }
2424
2425# check for static char foo[] = "bar" declarations.
2426 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
000d1cc1
JP
2427 WARN("STATIC_CONST_CHAR_ARRAY",
2428 "static char array declaration should probably be static const char\n" .
cb710eca
JP
2429 $herecurr);
2430 }
2431
93ed0e2d
JP
2432# check for declarations of struct pci_device_id
2433 if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) {
000d1cc1
JP
2434 WARN("DEFINE_PCI_DEVICE_TABLE",
2435 "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr);
93ed0e2d
JP
2436 }
2437
653d4876
AW
2438# check for new typedefs, only function parameters and sparse annotations
2439# make sense.
2440 if ($line =~ /\btypedef\s/ &&
8054576d 2441 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
c45dcabd 2442 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
8ed22cad 2443 $line !~ /\b$typeTypedefs\b/ &&
653d4876 2444 $line !~ /\b__bitwise(?:__|)\b/) {
000d1cc1
JP
2445 WARN("NEW_TYPEDEFS",
2446 "do not add new typedefs\n" . $herecurr);
0a920b5b
AW
2447 }
2448
2449# * goes on variable not on type
65863862 2450 # (char*[ const])
bfcb2cc7
AW
2451 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
2452 #print "AA<$1>\n";
3705ce5b 2453 my ($ident, $from, $to) = ($1, $2, $2);
65863862
AW
2454
2455 # Should start with a space.
2456 $to =~ s/^(\S)/ $1/;
2457 # Should not end with a space.
2458 $to =~ s/\s+$//;
2459 # '*'s should not have spaces between.
f9a0b3d1 2460 while ($to =~ s/\*\s+\*/\*\*/) {
65863862 2461 }
d8aaf121 2462
3705ce5b 2463## print "1: from<$from> to<$to> ident<$ident>\n";
65863862 2464 if ($from ne $to) {
3705ce5b
JP
2465 if (ERROR("POINTER_LOCATION",
2466 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
2467 $fix) {
2468 my $sub_from = $ident;
2469 my $sub_to = $ident;
2470 $sub_to =~ s/\Q$from\E/$to/;
2471 $fixed[$linenr - 1] =~
2472 s@\Q$sub_from\E@$sub_to@;
2473 }
65863862 2474 }
bfcb2cc7
AW
2475 }
2476 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
2477 #print "BB<$1>\n";
3705ce5b 2478 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
65863862
AW
2479
2480 # Should start with a space.
2481 $to =~ s/^(\S)/ $1/;
2482 # Should not end with a space.
2483 $to =~ s/\s+$//;
2484 # '*'s should not have spaces between.
f9a0b3d1 2485 while ($to =~ s/\*\s+\*/\*\*/) {
65863862
AW
2486 }
2487 # Modifiers should have spaces.
2488 $to =~ s/(\b$Modifier$)/$1 /;
d8aaf121 2489
3705ce5b 2490## print "2: from<$from> to<$to> ident<$ident>\n";
667026e7 2491 if ($from ne $to && $ident !~ /^$Modifier$/) {
3705ce5b
JP
2492 if (ERROR("POINTER_LOCATION",
2493 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
2494 $fix) {
2495
2496 my $sub_from = $match;
2497 my $sub_to = $match;
2498 $sub_to =~ s/\Q$from\E/$to/;
2499 $fixed[$linenr - 1] =~
2500 s@\Q$sub_from\E@$sub_to@;
2501 }
65863862 2502 }
0a920b5b
AW
2503 }
2504
2505# # no BUG() or BUG_ON()
2506# if ($line =~ /\b(BUG|BUG_ON)\b/) {
2507# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
2508# print "$herecurr";
2509# $clean = 0;
2510# }
2511
8905a67c 2512 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
000d1cc1
JP
2513 WARN("LINUX_VERSION_CODE",
2514 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
8905a67c
AW
2515 }
2516
17441227
JP
2517# check for uses of printk_ratelimit
2518 if ($line =~ /\bprintk_ratelimit\s*\(/) {
000d1cc1
JP
2519 WARN("PRINTK_RATELIMITED",
2520"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
17441227
JP
2521 }
2522
00df344f
AW
2523# printk should use KERN_* levels. Note that follow on printk's on the
2524# same line do not need a level, so we use the current block context
2525# to try and find and validate the current printk. In summary the current
25985edc 2526# printk includes all preceding printk's which have no newline on the end.
00df344f 2527# we assume the first bad printk is the one to report.
f0a594c1 2528 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
00df344f
AW
2529 my $ok = 0;
2530 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
2531 #print "CHECK<$lines[$ln - 1]\n";
25985edc 2532 # we have a preceding printk if it ends
00df344f
AW
2533 # with "\n" ignore it, else it is to blame
2534 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
2535 if ($rawlines[$ln - 1] !~ m{\\n"}) {
2536 $ok = 1;
2537 }
2538 last;
2539 }
2540 }
2541 if ($ok == 0) {
000d1cc1
JP
2542 WARN("PRINTK_WITHOUT_KERN_LEVEL",
2543 "printk() should include KERN_ facility level\n" . $herecurr);
00df344f 2544 }
0a920b5b
AW
2545 }
2546
243f3803
JP
2547 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
2548 my $orig = $1;
2549 my $level = lc($orig);
2550 $level = "warn" if ($level eq "warning");
8f26b837
JP
2551 my $level2 = $level;
2552 $level2 = "dbg" if ($level eq "debug");
243f3803 2553 WARN("PREFER_PR_LEVEL",
8f26b837 2554 "Prefer netdev_$level2(netdev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
243f3803
JP
2555 }
2556
2557 if ($line =~ /\bpr_warning\s*\(/) {
2558 WARN("PREFER_PR_LEVEL",
2559 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr);
2560 }
2561
dc139313
JP
2562 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
2563 my $orig = $1;
2564 my $level = lc($orig);
2565 $level = "warn" if ($level eq "warning");
2566 $level = "dbg" if ($level eq "debug");
2567 WARN("PREFER_DEV_LEVEL",
2568 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
2569 }
2570
653d4876
AW
2571# function brace can't be on same line, except for #defines of do while,
2572# or if closed on same line
c45dcabd
AW
2573 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
2574 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
000d1cc1
JP
2575 ERROR("OPEN_BRACE",
2576 "open brace '{' following function declarations go on the next line\n" . $herecurr);
0a920b5b 2577 }
653d4876 2578
8905a67c
AW
2579# open braces for enum, union and struct go on the same line.
2580 if ($line =~ /^.\s*{/ &&
2581 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
000d1cc1
JP
2582 ERROR("OPEN_BRACE",
2583 "open brace '{' following $1 go on the same line\n" . $hereprev);
8905a67c
AW
2584 }
2585
0c73b4eb 2586# missing space after union, struct or enum definition
3705ce5b
JP
2587 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
2588 if (WARN("SPACING",
2589 "missing space after $1 definition\n" . $herecurr) &&
2590 $fix) {
2591 $fixed[$linenr - 1] =~
2592 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
2593 }
0c73b4eb
AW
2594 }
2595
8d31cfce
AW
2596# check for spacing round square brackets; allowed:
2597# 1. with a type on the left -- int [] a;
fe2a7dbc
AW
2598# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
2599# 3. inside a curly brace -- = { [0...10] = 5 }
8d31cfce
AW
2600 while ($line =~ /(.*?\s)\[/g) {
2601 my ($where, $prefix) = ($-[1], $1);
2602 if ($prefix !~ /$Type\s+$/ &&
fe2a7dbc 2603 ($where != 0 || $prefix !~ /^.\s+$/) &&
daebc534 2604 $prefix !~ /[{,]\s+$/) {
3705ce5b
JP
2605 if (ERROR("BRACKET_SPACE",
2606 "space prohibited before open square bracket '['\n" . $herecurr) &&
2607 $fix) {
2608 $fixed[$linenr - 1] =~
2609 s/^(\+.*?)\s+\[/$1\[/;
2610 }
8d31cfce
AW
2611 }
2612 }
2613
f0a594c1 2614# check for spaces between functions and their parentheses.
6c72ffaa 2615 while ($line =~ /($Ident)\s+\(/g) {
c2fdda0d 2616 my $name = $1;
773647a0
AW
2617 my $ctx_before = substr($line, 0, $-[1]);
2618 my $ctx = "$ctx_before$name";
c2fdda0d
AW
2619
2620 # Ignore those directives where spaces _are_ permitted.
773647a0
AW
2621 if ($name =~ /^(?:
2622 if|for|while|switch|return|case|
2623 volatile|__volatile__|
2624 __attribute__|format|__extension__|
2625 asm|__asm__)$/x)
2626 {
c2fdda0d
AW
2627 # cpp #define statements have non-optional spaces, ie
2628 # if there is a space between the name and the open
2629 # parenthesis it is simply not a parameter group.
c45dcabd 2630 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
773647a0
AW
2631
2632 # cpp #elif statement condition may start with a (
c45dcabd 2633 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
c2fdda0d
AW
2634
2635 # If this whole things ends with a type its most
2636 # likely a typedef for a function.
773647a0 2637 } elsif ($ctx =~ /$Type$/) {
c2fdda0d
AW
2638
2639 } else {
3705ce5b
JP
2640 if (WARN("SPACING",
2641 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
2642 $fix) {
2643 $fixed[$linenr - 1] =~
2644 s/\b$name\s+\(/$name\(/;
2645 }
6c72ffaa 2646 }
f0a594c1 2647 }
9a4cad4e 2648
653d4876 2649# Check operator spacing.
0a920b5b 2650 if (!($line=~/\#\s*include/)) {
3705ce5b
JP
2651 my $fixed_line = "";
2652 my $line_fixed = 0;
2653
9c0ca6f9
AW
2654 my $ops = qr{
2655 <<=|>>=|<=|>=|==|!=|
2656 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2657 =>|->|<<|>>|<|>|=|!|~|
1f65f947
AW
2658 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2659 \?|:
9c0ca6f9 2660 }x;
cf655043 2661 my @elements = split(/($ops|;)/, $opline);
3705ce5b
JP
2662
2663## print("element count: <" . $#elements . ">\n");
2664## foreach my $el (@elements) {
2665## print("el: <$el>\n");
2666## }
2667
2668 my @fix_elements = ();
00df344f 2669 my $off = 0;
6c72ffaa 2670
3705ce5b
JP
2671 foreach my $el (@elements) {
2672 push(@fix_elements, substr($rawline, $off, length($el)));
2673 $off += length($el);
2674 }
2675
2676 $off = 0;
2677
6c72ffaa
AW
2678 my $blank = copy_spacing($opline);
2679
0a920b5b 2680 for (my $n = 0; $n < $#elements; $n += 2) {
3705ce5b
JP
2681
2682 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
2683
2684## print("n: <$n> good: <$good>\n");
2685
4a0df2ef
AW
2686 $off += length($elements[$n]);
2687
25985edc 2688 # Pick up the preceding and succeeding characters.
773647a0
AW
2689 my $ca = substr($opline, 0, $off);
2690 my $cc = '';
2691 if (length($opline) >= ($off + length($elements[$n + 1]))) {
2692 $cc = substr($opline, $off + length($elements[$n + 1]));
2693 }
2694 my $cb = "$ca$;$cc";
2695
4a0df2ef
AW
2696 my $a = '';
2697 $a = 'V' if ($elements[$n] ne '');
2698 $a = 'W' if ($elements[$n] =~ /\s$/);
cf655043 2699 $a = 'C' if ($elements[$n] =~ /$;$/);
4a0df2ef
AW
2700 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
2701 $a = 'O' if ($elements[$n] eq '');
773647a0 2702 $a = 'E' if ($ca =~ /^\s*$/);
4a0df2ef 2703
0a920b5b 2704 my $op = $elements[$n + 1];
4a0df2ef
AW
2705
2706 my $c = '';
0a920b5b 2707 if (defined $elements[$n + 2]) {
4a0df2ef
AW
2708 $c = 'V' if ($elements[$n + 2] ne '');
2709 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
cf655043 2710 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4a0df2ef
AW
2711 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
2712 $c = 'O' if ($elements[$n + 2] eq '');
8b1b3378 2713 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4a0df2ef
AW
2714 } else {
2715 $c = 'E';
0a920b5b
AW
2716 }
2717
4a0df2ef
AW
2718 my $ctx = "${a}x${c}";
2719
2720 my $at = "(ctx:$ctx)";
2721
6c72ffaa 2722 my $ptr = substr($blank, 0, $off) . "^";
de7d4f0e 2723 my $hereptr = "$hereline$ptr\n";
0a920b5b 2724
74048ed8 2725 # Pull out the value of this operator.
6c72ffaa 2726 my $op_type = substr($curr_values, $off + 1, 1);
0a920b5b 2727
1f65f947
AW
2728 # Get the full operator variant.
2729 my $opv = $op . substr($curr_vars, $off, 1);
2730
13214adf
AW
2731 # Ignore operators passed as parameters.
2732 if ($op_type ne 'V' &&
2733 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
2734
cf655043
AW
2735# # Ignore comments
2736# } elsif ($op =~ /^$;+$/) {
13214adf 2737
d8aaf121 2738 # ; should have either the end of line or a space or \ after it
13214adf 2739 } elsif ($op eq ';') {
cf655043
AW
2740 if ($ctx !~ /.x[WEBC]/ &&
2741 $cc !~ /^\\/ && $cc !~ /^;/) {
3705ce5b
JP
2742 if (ERROR("SPACING",
2743 "space required after that '$op' $at\n" . $hereptr)) {
2744 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2745 $line_fixed = 1;
2746 }
d8aaf121
AW
2747 }
2748
2749 # // is a comment
2750 } elsif ($op eq '//') {
0a920b5b 2751
1f65f947
AW
2752 # No spaces for:
2753 # ->
2754 # : when part of a bitfield
2755 } elsif ($op eq '->' || $opv eq ':B') {
4a0df2ef 2756 if ($ctx =~ /Wx.|.xW/) {
3705ce5b
JP
2757 if (ERROR("SPACING",
2758 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
2759 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2760 $line_fixed = 1;
2761 if (defined $fix_elements[$n + 2]) {
2762 $fix_elements[$n + 2] =~ s/^\s+//;
2763 }
2764 }
0a920b5b
AW
2765 }
2766
2767 # , must have a space on the right.
2768 } elsif ($op eq ',') {
cf655043 2769 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
3705ce5b
JP
2770 if (ERROR("SPACING",
2771 "space required after that '$op' $at\n" . $hereptr)) {
2772 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2773 $line_fixed = 1;
2774 }
0a920b5b
AW
2775 }
2776
9c0ca6f9 2777 # '*' as part of a type definition -- reported already.
74048ed8 2778 } elsif ($opv eq '*_') {
9c0ca6f9
AW
2779 #warn "'*' is part of type\n";
2780
2781 # unary operators should have a space before and
2782 # none after. May be left adjacent to another
2783 # unary operator, or a cast
2784 } elsif ($op eq '!' || $op eq '~' ||
74048ed8 2785 $opv eq '*U' || $opv eq '-U' ||
0d413866 2786 $opv eq '&U' || $opv eq '&&U') {
cf655043 2787 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
3705ce5b
JP
2788 if (ERROR("SPACING",
2789 "space required before that '$op' $at\n" . $hereptr)) {
2790 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
2791 $line_fixed = 1;
2792 }
0a920b5b 2793 }
a3340b35 2794 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
171ae1a4
AW
2795 # A unary '*' may be const
2796
2797 } elsif ($ctx =~ /.xW/) {
3705ce5b
JP
2798 if (ERROR("SPACING",
2799 "space prohibited after that '$op' $at\n" . $hereptr)) {
2800 $fixed_line =~ s/\s+$//;
2801 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2802 $line_fixed = 1;
2803 if (defined $fix_elements[$n + 2]) {
2804 $fix_elements[$n + 2] =~ s/^\s+//;
2805 }
2806 }
0a920b5b
AW
2807 }
2808
2809 # unary ++ and unary -- are allowed no space on one side.
2810 } elsif ($op eq '++' or $op eq '--') {
773647a0 2811 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
3705ce5b
JP
2812 if (ERROR("SPACING",
2813 "space required one side of that '$op' $at\n" . $hereptr)) {
2814 $fixed_line =~ s/\s+$//;
2815 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]) . " ";
2816 $line_fixed = 1;
2817 }
773647a0
AW
2818 }
2819 if ($ctx =~ /Wx[BE]/ ||
2820 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
3705ce5b
JP
2821 if (ERROR("SPACING",
2822 "space prohibited before that '$op' $at\n" . $hereptr)) {
2823 $fixed_line =~ s/\s+$//;
2824 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2825 $line_fixed = 1;
2826 }
0a920b5b 2827 }
773647a0 2828 if ($ctx =~ /ExW/) {
3705ce5b
JP
2829 if (ERROR("SPACING",
2830 "space prohibited after that '$op' $at\n" . $hereptr)) {
2831 $fixed_line =~ s/\s+$//;
2832 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2833 $line_fixed = 1;
2834 if (defined $fix_elements[$n + 2]) {
2835 $fix_elements[$n + 2] =~ s/^\s+//;
2836 }
2837 }
653d4876 2838 }
0a920b5b 2839
0a920b5b 2840 # << and >> may either have or not have spaces both sides
9c0ca6f9
AW
2841 } elsif ($op eq '<<' or $op eq '>>' or
2842 $op eq '&' or $op eq '^' or $op eq '|' or
2843 $op eq '+' or $op eq '-' or
c2fdda0d
AW
2844 $op eq '*' or $op eq '/' or
2845 $op eq '%')
0a920b5b 2846 {
773647a0 2847 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
3705ce5b
JP
2848 if (ERROR("SPACING",
2849 "need consistent spacing around '$op' $at\n" . $hereptr)) {
2850 $fixed_line =~ s/\s+$//;
2851 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2852 $line_fixed = 1;
2853 }
0a920b5b
AW
2854 }
2855
1f65f947
AW
2856 # A colon needs no spaces before when it is
2857 # terminating a case value or a label.
2858 } elsif ($opv eq ':C' || $opv eq ':L') {
2859 if ($ctx =~ /Wx./) {
3705ce5b
JP
2860 if (ERROR("SPACING",
2861 "space prohibited before that '$op' $at\n" . $hereptr)) {
2862 $good = trim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
2863 $line_fixed = 1;
2864 }
1f65f947
AW
2865 }
2866
0a920b5b 2867 # All the others need spaces both sides.
cf655043 2868 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1f65f947
AW
2869 my $ok = 0;
2870
22f2a2ef 2871 # Ignore email addresses <foo@bar>
1f65f947
AW
2872 if (($op eq '<' &&
2873 $cc =~ /^\S+\@\S+>/) ||
2874 ($op eq '>' &&
2875 $ca =~ /<\S+\@\S+$/))
2876 {
2877 $ok = 1;
2878 }
2879
2880 # Ignore ?:
2881 if (($opv eq ':O' && $ca =~ /\?$/) ||
2882 ($op eq '?' && $cc =~ /^:/)) {
2883 $ok = 1;
2884 }
2885
2886 if ($ok == 0) {
3705ce5b
JP
2887 if (ERROR("SPACING",
2888 "spaces required around that '$op' $at\n" . $hereptr)) {
2889 $good = trim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
2890 $good = $fix_elements[$n] . " " . trim($fix_elements[$n + 1]) . " ";
2891 $line_fixed = 1;
2892 }
22f2a2ef 2893 }
0a920b5b 2894 }
4a0df2ef 2895 $off += length($elements[$n + 1]);
3705ce5b
JP
2896
2897## print("n: <$n> GOOD: <$good>\n");
2898
2899 $fixed_line = $fixed_line . $good;
2900 }
2901
2902 if (($#elements % 2) == 0) {
2903 $fixed_line = $fixed_line . $fix_elements[$#elements];
0a920b5b 2904 }
3705ce5b
JP
2905
2906 if ($fix && $line_fixed && $fixed_line ne $fixed[$linenr - 1]) {
2907 $fixed[$linenr - 1] = $fixed_line;
2908 }
2909
2910
0a920b5b
AW
2911 }
2912
786b6326
JP
2913# check for whitespace before a non-naked semicolon
2914 if ($line =~ /^\+.*\S\s+;/) {
2915 if (WARN("SPACING",
2916 "space prohibited before semicolon\n" . $herecurr) &&
2917 $fix) {
2918 1 while $fixed[$linenr - 1] =~
2919 s/^(\+.*\S)\s+;/$1;/;
2920 }
2921 }
2922
f0a594c1
AW
2923# check for multiple assignments
2924 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
000d1cc1
JP
2925 CHK("MULTIPLE_ASSIGNMENTS",
2926 "multiple assignments should be avoided\n" . $herecurr);
f0a594c1
AW
2927 }
2928
22f2a2ef
AW
2929## # check for multiple declarations, allowing for a function declaration
2930## # continuation.
2931## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
2932## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
2933##
2934## # Remove any bracketed sections to ensure we do not
2935## # falsly report the parameters of functions.
2936## my $ln = $line;
2937## while ($ln =~ s/\([^\(\)]*\)//g) {
2938## }
2939## if ($ln =~ /,/) {
000d1cc1
JP
2940## WARN("MULTIPLE_DECLARATION",
2941## "declaring multiple variables together should be avoided\n" . $herecurr);
22f2a2ef
AW
2942## }
2943## }
f0a594c1 2944
0a920b5b 2945#need space before brace following if, while, etc
22f2a2ef
AW
2946 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
2947 $line =~ /do{/) {
3705ce5b
JP
2948 if (ERROR("SPACING",
2949 "space required before the open brace '{'\n" . $herecurr) &&
2950 $fix) {
2951 $fixed[$linenr - 1] =~
2952 s/^(\+.*(?:do|\))){/$1 {/;
2953 }
de7d4f0e
AW
2954 }
2955
c4a62ef9
JP
2956## # check for blank lines before declarations
2957## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
2958## $prevrawline =~ /^.\s*$/) {
2959## WARN("SPACING",
2960## "No blank lines before declarations\n" . $hereprev);
2961## }
2962##
2963
de7d4f0e
AW
2964# closing brace should have a space following it when it has anything
2965# on the line
2966 if ($line =~ /}(?!(?:,|;|\)))\S/) {
000d1cc1
JP
2967 ERROR("SPACING",
2968 "space required after that close brace '}'\n" . $herecurr);
0a920b5b
AW
2969 }
2970
22f2a2ef
AW
2971# check spacing on square brackets
2972 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
3705ce5b
JP
2973 if (ERROR("SPACING",
2974 "space prohibited after that open square bracket '['\n" . $herecurr) &&
2975 $fix) {
2976 $fixed[$linenr - 1] =~
2977 s/\[\s+/\[/;
2978 }
22f2a2ef
AW
2979 }
2980 if ($line =~ /\s\]/) {
3705ce5b
JP
2981 if (ERROR("SPACING",
2982 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
2983 $fix) {
2984 $fixed[$linenr - 1] =~
2985 s/\s+\]/\]/;
2986 }
22f2a2ef
AW
2987 }
2988
c45dcabd 2989# check spacing on parentheses
9c0ca6f9
AW
2990 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2991 $line !~ /for\s*\(\s+;/) {
3705ce5b
JP
2992 if (ERROR("SPACING",
2993 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
2994 $fix) {
2995 $fixed[$linenr - 1] =~
2996 s/\(\s+/\(/;
2997 }
22f2a2ef 2998 }
13214adf 2999 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
c45dcabd
AW
3000 $line !~ /for\s*\(.*;\s+\)/ &&
3001 $line !~ /:\s+\)/) {
3705ce5b
JP
3002 if (ERROR("SPACING",
3003 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3004 $fix) {
3005 $fixed[$linenr - 1] =~
3006 s/\s+\)/\)/;
3007 }
22f2a2ef
AW
3008 }
3009
0a920b5b 3010#goto labels aren't indented, allow a single space however
4a0df2ef 3011 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
0a920b5b 3012 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
3705ce5b
JP
3013 if (WARN("INDENTED_LABEL",
3014 "labels should not be indented\n" . $herecurr) &&
3015 $fix) {
3016 $fixed[$linenr - 1] =~
3017 s/^(.)\s+/$1/;
3018 }
0a920b5b
AW
3019 }
3020
c45dcabd
AW
3021# Return is not a function.
3022 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
3023 my $spacing = $1;
3024 my $value = $2;
3025
86f9d059 3026 # Flatten any parentheses
fb2d2c1b
AW
3027 $value =~ s/\(/ \(/g;
3028 $value =~ s/\)/\) /g;
e01886ad 3029 while ($value =~ s/\[[^\[\]]*\]/1/ ||
63f17f89
AW
3030 $value !~ /(?:$Ident|-?$Constant)\s*
3031 $Compare\s*
3032 (?:$Ident|-?$Constant)/x &&
3033 $value =~ s/\([^\(\)]*\)/1/) {
c45dcabd 3034 }
fb2d2c1b
AW
3035#print "value<$value>\n";
3036 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
000d1cc1
JP
3037 ERROR("RETURN_PARENTHESES",
3038 "return is not a function, parentheses are not required\n" . $herecurr);
c45dcabd
AW
3039
3040 } elsif ($spacing !~ /\s+/) {
000d1cc1
JP
3041 ERROR("SPACING",
3042 "space required before the open parenthesis '('\n" . $herecurr);
c45dcabd
AW
3043 }
3044 }
53a3c448
AW
3045# Return of what appears to be an errno should normally be -'ve
3046 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3047 my $name = $1;
3048 if ($name ne 'EOF' && $name ne 'ERROR') {
000d1cc1
JP
3049 WARN("USE_NEGATIVE_ERRNO",
3050 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
53a3c448
AW
3051 }
3052 }
c45dcabd 3053
0a920b5b 3054# Need a space before open parenthesis after if, while etc
3705ce5b
JP
3055 if ($line =~ /\b(if|while|for|switch)\(/) {
3056 if (ERROR("SPACING",
3057 "space required before the open parenthesis '('\n" . $herecurr) &&
3058 $fix) {
3059 $fixed[$linenr - 1] =~
3060 s/\b(if|while|for|switch)\(/$1 \(/;
3061 }
0a920b5b
AW
3062 }
3063
f5fe35dd
AW
3064# Check for illegal assignment in if conditional -- and check for trailing
3065# statements after the conditional.
170d3a22 3066 if ($line =~ /do\s*(?!{)/) {
3e469cdc
AW
3067 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3068 ctx_statement_block($linenr, $realcnt, 0)
3069 if (!defined $stat);
170d3a22
AW
3070 my ($stat_next) = ctx_statement_block($line_nr_next,
3071 $remain_next, $off_next);
3072 $stat_next =~ s/\n./\n /g;
3073 ##print "stat<$stat> stat_next<$stat_next>\n";
3074
3075 if ($stat_next =~ /^\s*while\b/) {
3076 # If the statement carries leading newlines,
3077 # then count those as offsets.
3078 my ($whitespace) =
3079 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3080 my $offset =
3081 statement_rawlines($whitespace) - 1;
3082
3083 $suppress_whiletrailers{$line_nr_next +
3084 $offset} = 1;
3085 }
3086 }
3087 if (!defined $suppress_whiletrailers{$linenr} &&
3088 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
171ae1a4 3089 my ($s, $c) = ($stat, $cond);
8905a67c 3090
b53c8e10 3091 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
000d1cc1
JP
3092 ERROR("ASSIGN_IN_IF",
3093 "do not use assignment in if condition\n" . $herecurr);
8905a67c
AW
3094 }
3095
3096 # Find out what is on the end of the line after the
3097 # conditional.
773647a0 3098 substr($s, 0, length($c), '');
8905a67c 3099 $s =~ s/\n.*//g;
13214adf 3100 $s =~ s/$;//g; # Remove any comments
53210168
AW
3101 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3102 $c !~ /}\s*while\s*/)
773647a0 3103 {
bb44ad39
AW
3104 # Find out how long the conditional actually is.
3105 my @newlines = ($c =~ /\n/gs);
3106 my $cond_lines = 1 + $#newlines;
42bdf74c 3107 my $stat_real = '';
bb44ad39 3108
42bdf74c
HS
3109 $stat_real = raw_line($linenr, $cond_lines)
3110 . "\n" if ($cond_lines);
bb44ad39
AW
3111 if (defined($stat_real) && $cond_lines > 1) {
3112 $stat_real = "[...]\n$stat_real";
3113 }
3114
000d1cc1
JP
3115 ERROR("TRAILING_STATEMENTS",
3116 "trailing statements should be on next line\n" . $herecurr . $stat_real);
8905a67c
AW
3117 }
3118 }
3119
13214adf
AW
3120# Check for bitwise tests written as boolean
3121 if ($line =~ /
3122 (?:
3123 (?:\[|\(|\&\&|\|\|)
3124 \s*0[xX][0-9]+\s*
3125 (?:\&\&|\|\|)
3126 |
3127 (?:\&\&|\|\|)
3128 \s*0[xX][0-9]+\s*
3129 (?:\&\&|\|\||\)|\])
3130 )/x)
3131 {
000d1cc1
JP
3132 WARN("HEXADECIMAL_BOOLEAN_TEST",
3133 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
13214adf
AW
3134 }
3135
8905a67c 3136# if and else should not have general statements after it
13214adf
AW
3137 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3138 my $s = $1;
3139 $s =~ s/$;//g; # Remove any comments
3140 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
000d1cc1
JP
3141 ERROR("TRAILING_STATEMENTS",
3142 "trailing statements should be on next line\n" . $herecurr);
13214adf 3143 }
0a920b5b 3144 }
39667782
AW
3145# if should not continue a brace
3146 if ($line =~ /}\s*if\b/) {
000d1cc1
JP
3147 ERROR("TRAILING_STATEMENTS",
3148 "trailing statements should be on next line\n" .
39667782
AW
3149 $herecurr);
3150 }
a1080bf8
AW
3151# case and default should not have general statements after them
3152 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
3153 $line !~ /\G(?:
3fef12d6 3154 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
a1080bf8
AW
3155 \s*return\s+
3156 )/xg)
3157 {
000d1cc1
JP
3158 ERROR("TRAILING_STATEMENTS",
3159 "trailing statements should be on next line\n" . $herecurr);
a1080bf8 3160 }
0a920b5b
AW
3161
3162 # Check for }<nl>else {, these must be at the same
3163 # indent level to be relevant to each other.
3164 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
3165 $previndent == $indent) {
000d1cc1
JP
3166 ERROR("ELSE_AFTER_BRACE",
3167 "else should follow close brace '}'\n" . $hereprev);
0a920b5b
AW
3168 }
3169
c2fdda0d
AW
3170 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
3171 $previndent == $indent) {
3172 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
3173
3174 # Find out what is on the end of the line after the
3175 # conditional.
773647a0 3176 substr($s, 0, length($c), '');
c2fdda0d
AW
3177 $s =~ s/\n.*//g;
3178
3179 if ($s =~ /^\s*;/) {
000d1cc1
JP
3180 ERROR("WHILE_AFTER_BRACE",
3181 "while should follow close brace '}'\n" . $hereprev);
c2fdda0d
AW
3182 }
3183 }
3184
95e2c602 3185#Specific variable tests
323c1260
JP
3186 while ($line =~ m{($Constant|$Lval)}g) {
3187 my $var = $1;
95e2c602
JP
3188
3189#gcc binary extension
3190 if ($var =~ /^$Binary$/) {
3191 WARN("GCC_BINARY_CONSTANT",
3192 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr);
3193 }
3194
3195#CamelCase
807bd26c 3196 if ($var !~ /^$Constant$/ &&
be79794b 3197 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
807bd26c 3198 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
323c1260
JP
3199 !defined $camelcase{$var}) {
3200 $camelcase{$var} = 1;
be79794b
JP
3201 CHK("CAMELCASE",
3202 "Avoid CamelCase: <$var>\n" . $herecurr);
323c1260
JP
3203 }
3204 }
0a920b5b
AW
3205
3206#no spaces allowed after \ in define
c45dcabd 3207 if ($line=~/\#\s*define.*\\\s$/) {
000d1cc1
JP
3208 WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
3209 "Whitepspace after \\ makes next lines useless\n" . $herecurr);
0a920b5b
AW
3210 }
3211
653d4876 3212#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
c45dcabd 3213 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
e09dec48
AW
3214 my $file = "$1.h";
3215 my $checkfile = "include/linux/$file";
3216 if (-f "$root/$checkfile" &&
3217 $realfile ne $checkfile &&
7840a94c 3218 $1 !~ /$allowed_asm_includes/)
c45dcabd 3219 {
e09dec48 3220 if ($realfile =~ m{^arch/}) {
000d1cc1
JP
3221 CHK("ARCH_INCLUDE_LINUX",
3222 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
e09dec48 3223 } else {
000d1cc1
JP
3224 WARN("INCLUDE_LINUX",
3225 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
e09dec48 3226 }
0a920b5b
AW
3227 }
3228 }
3229
653d4876
AW
3230# multi-statement macros should be enclosed in a do while loop, grab the
3231# first statement and ensure its the whole macro if its not enclosed
cf655043 3232# in a known good container
b8f96a31
AW
3233 if ($realfile !~ m@/vmlinux.lds.h$@ &&
3234 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
d8aaf121
AW
3235 my $ln = $linenr;
3236 my $cnt = $realcnt;
c45dcabd
AW
3237 my ($off, $dstat, $dcond, $rest);
3238 my $ctx = '';
c45dcabd 3239 ($dstat, $dcond, $ln, $cnt, $off) =
f74bd194
AW
3240 ctx_statement_block($linenr, $realcnt, 0);
3241 $ctx = $dstat;
c45dcabd 3242 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
a3bb97a7 3243 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
c45dcabd 3244
f74bd194 3245 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
292f1a9b 3246 $dstat =~ s/$;//g;
c45dcabd
AW
3247 $dstat =~ s/\\\n.//g;
3248 $dstat =~ s/^\s*//s;
3249 $dstat =~ s/\s*$//s;
de7d4f0e 3250
c45dcabd 3251 # Flatten any parentheses and braces
bf30d6ed
AW
3252 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
3253 $dstat =~ s/\{[^\{\}]*\}/1/ ||
c81769fd 3254 $dstat =~ s/\[[^\[\]]*\]/1/)
bf30d6ed 3255 {
de7d4f0e 3256 }
d8aaf121 3257
e45bab8e
AW
3258 # Flatten any obvious string concatentation.
3259 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
3260 $dstat =~ s/$Ident\s*("X*")/$1/)
3261 {
3262 }
3263
c45dcabd
AW
3264 my $exceptions = qr{
3265 $Declare|
3266 module_param_named|
a0a0a7a9 3267 MODULE_PARM_DESC|
c45dcabd
AW
3268 DECLARE_PER_CPU|
3269 DEFINE_PER_CPU|
383099fd 3270 __typeof__\(|
22fd2d3e
SS
3271 union|
3272 struct|
ea71a0a0
AW
3273 \.$Ident\s*=\s*|
3274 ^\"|\"$
c45dcabd 3275 }x;
5eaa20b9 3276 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
f74bd194
AW
3277 if ($dstat ne '' &&
3278 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
3279 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
3cc4b1c3 3280 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
b9df76ac 3281 $dstat !~ /^'X'$/ && # character constants
f74bd194
AW
3282 $dstat !~ /$exceptions/ &&
3283 $dstat !~ /^\.$Ident\s*=/ && # .foo =
e942e2c3 3284 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
72f115f9 3285 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
f74bd194
AW
3286 $dstat !~ /^for\s*$Constant$/ && # for (...)
3287 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
3288 $dstat !~ /^do\s*{/ && # do {...
3289 $dstat !~ /^\({/) # ({...
3290 {
3291 $ctx =~ s/\n*$//;
3292 my $herectx = $here . "\n";
3293 my $cnt = statement_rawlines($ctx);
3294
3295 for (my $n = 0; $n < $cnt; $n++) {
3296 $herectx .= raw_line($linenr, $n) . "\n";
c45dcabd
AW
3297 }
3298
f74bd194
AW
3299 if ($dstat =~ /;/) {
3300 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
3301 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
3302 } else {
000d1cc1 3303 ERROR("COMPLEX_MACRO",
f74bd194 3304 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
d8aaf121 3305 }
653d4876 3306 }
5023d347 3307
481eb486 3308# check for line continuations outside of #defines, preprocessor #, and asm
5023d347
JP
3309
3310 } else {
3311 if ($prevline !~ /^..*\\$/ &&
481eb486
JP
3312 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3313 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
5023d347
JP
3314 $line =~ /^\+.*\\$/) {
3315 WARN("LINE_CONTINUATIONS",
3316 "Avoid unnecessary line continuations\n" . $herecurr);
3317 }
0a920b5b
AW
3318 }
3319
b13edf7f
JP
3320# do {} while (0) macro tests:
3321# single-statement macros do not need to be enclosed in do while (0) loop,
3322# macro should not end with a semicolon
3323 if ($^V && $^V ge 5.10.0 &&
3324 $realfile !~ m@/vmlinux.lds.h$@ &&
3325 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
3326 my $ln = $linenr;
3327 my $cnt = $realcnt;
3328 my ($off, $dstat, $dcond, $rest);
3329 my $ctx = '';
3330 ($dstat, $dcond, $ln, $cnt, $off) =
3331 ctx_statement_block($linenr, $realcnt, 0);
3332 $ctx = $dstat;
3333
3334 $dstat =~ s/\\\n.//g;
3335
3336 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
3337 my $stmts = $2;
3338 my $semis = $3;
3339
3340 $ctx =~ s/\n*$//;
3341 my $cnt = statement_rawlines($ctx);
3342 my $herectx = $here . "\n";
3343
3344 for (my $n = 0; $n < $cnt; $n++) {
3345 $herectx .= raw_line($linenr, $n) . "\n";
3346 }
3347
ac8e97f8
JP
3348 if (($stmts =~ tr/;/;/) == 1 &&
3349 $stmts !~ /^\s*(if|while|for|switch)\b/) {
b13edf7f
JP
3350 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
3351 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
3352 }
3353 if (defined $semis && $semis ne "") {
3354 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
3355 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
3356 }
3357 }
3358 }
3359
080ba929
MF
3360# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
3361# all assignments may have only one of the following with an assignment:
3362# .
3363# ALIGN(...)
3364# VMLINUX_SYMBOL(...)
3365 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
000d1cc1
JP
3366 WARN("MISSING_VMLINUX_SYMBOL",
3367 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
080ba929
MF
3368 }
3369
f0a594c1 3370# check for redundant bracing round if etc
13214adf
AW
3371 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
3372 my ($level, $endln, @chunks) =
cf655043 3373 ctx_statement_full($linenr, $realcnt, 1);
13214adf 3374 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
cf655043
AW
3375 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
3376 if ($#chunks > 0 && $level == 0) {
aad4f614
JP
3377 my @allowed = ();
3378 my $allow = 0;
13214adf 3379 my $seen = 0;
773647a0 3380 my $herectx = $here . "\n";
cf655043 3381 my $ln = $linenr - 1;
13214adf
AW
3382 for my $chunk (@chunks) {
3383 my ($cond, $block) = @{$chunk};
3384
773647a0
AW
3385 # If the condition carries leading newlines, then count those as offsets.
3386 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
3387 my $offset = statement_rawlines($whitespace) - 1;
3388
aad4f614 3389 $allowed[$allow] = 0;
773647a0
AW
3390 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
3391
3392 # We have looked at and allowed this specific line.
3393 $suppress_ifbraces{$ln + $offset} = 1;
3394
3395 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
cf655043
AW
3396 $ln += statement_rawlines($block) - 1;
3397
773647a0 3398 substr($block, 0, length($cond), '');
13214adf
AW
3399
3400 $seen++ if ($block =~ /^\s*{/);
3401
aad4f614 3402 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
cf655043
AW
3403 if (statement_lines($cond) > 1) {
3404 #print "APW: ALLOWED: cond<$cond>\n";
aad4f614 3405 $allowed[$allow] = 1;
13214adf
AW
3406 }
3407 if ($block =~/\b(?:if|for|while)\b/) {
cf655043 3408 #print "APW: ALLOWED: block<$block>\n";
aad4f614 3409 $allowed[$allow] = 1;
13214adf 3410 }
cf655043
AW
3411 if (statement_block_size($block) > 1) {
3412 #print "APW: ALLOWED: lines block<$block>\n";
aad4f614 3413 $allowed[$allow] = 1;
13214adf 3414 }
aad4f614 3415 $allow++;
13214adf 3416 }
aad4f614
JP
3417 if ($seen) {
3418 my $sum_allowed = 0;
3419 foreach (@allowed) {
3420 $sum_allowed += $_;
3421 }
3422 if ($sum_allowed == 0) {
3423 WARN("BRACES",
3424 "braces {} are not necessary for any arm of this statement\n" . $herectx);
3425 } elsif ($sum_allowed != $allow &&
3426 $seen != $allow) {
3427 CHK("BRACES",
3428 "braces {} should be used on all arms of this statement\n" . $herectx);
3429 }
13214adf
AW
3430 }
3431 }
3432 }
773647a0 3433 if (!defined $suppress_ifbraces{$linenr - 1} &&
13214adf 3434 $line =~ /\b(if|while|for|else)\b/) {
cf655043
AW
3435 my $allowed = 0;
3436
3437 # Check the pre-context.
3438 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
3439 #print "APW: ALLOWED: pre<$1>\n";
3440 $allowed = 1;
3441 }
773647a0
AW
3442
3443 my ($level, $endln, @chunks) =
3444 ctx_statement_full($linenr, $realcnt, $-[0]);
3445
cf655043
AW
3446 # Check the condition.
3447 my ($cond, $block) = @{$chunks[0]};
773647a0 3448 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
cf655043 3449 if (defined $cond) {
773647a0 3450 substr($block, 0, length($cond), '');
cf655043
AW
3451 }
3452 if (statement_lines($cond) > 1) {
3453 #print "APW: ALLOWED: cond<$cond>\n";
3454 $allowed = 1;
3455 }
3456 if ($block =~/\b(?:if|for|while)\b/) {
3457 #print "APW: ALLOWED: block<$block>\n";
3458 $allowed = 1;
3459 }
3460 if (statement_block_size($block) > 1) {
3461 #print "APW: ALLOWED: lines block<$block>\n";
3462 $allowed = 1;
3463 }
3464 # Check the post-context.
3465 if (defined $chunks[1]) {
3466 my ($cond, $block) = @{$chunks[1]};
3467 if (defined $cond) {
773647a0 3468 substr($block, 0, length($cond), '');
cf655043
AW
3469 }
3470 if ($block =~ /^\s*\{/) {
3471 #print "APW: ALLOWED: chunk-1 block<$block>\n";
3472 $allowed = 1;
3473 }
3474 }
3475 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
69932487 3476 my $herectx = $here . "\n";
f055663c 3477 my $cnt = statement_rawlines($block);
cf655043 3478
f055663c 3479 for (my $n = 0; $n < $cnt; $n++) {
69932487 3480 $herectx .= raw_line($linenr, $n) . "\n";
f0a594c1 3481 }
cf655043 3482
000d1cc1
JP
3483 WARN("BRACES",
3484 "braces {} are not necessary for single statement blocks\n" . $herectx);
f0a594c1
AW
3485 }
3486 }
3487
0979ae66 3488# check for unnecessary blank lines around braces
77b9a53a 3489 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
0979ae66
JP
3490 CHK("BRACES",
3491 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3492 }
77b9a53a 3493 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
0979ae66
JP
3494 CHK("BRACES",
3495 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3496 }
3497
4a0df2ef 3498# no volatiles please
6c72ffaa
AW
3499 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
3500 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
000d1cc1
JP
3501 WARN("VOLATILE",
3502 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
4a0df2ef
AW
3503 }
3504
00df344f 3505# warn about #if 0
c45dcabd 3506 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
000d1cc1
JP
3507 CHK("REDUNDANT_CODE",
3508 "if this code is redundant consider removing it\n" .
de7d4f0e 3509 $herecurr);
4a0df2ef
AW
3510 }
3511
03df4b51
AW
3512# check for needless "if (<foo>) fn(<foo>)" uses
3513 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3514 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3515 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3516 WARN('NEEDLESS_IF',
3517 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
4c432a8f
GKH
3518 }
3519 }
f0a594c1 3520
1a15a250 3521# prefer usleep_range over udelay
37581c28 3522 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
1a15a250 3523 # ignore udelay's < 10, however
37581c28 3524 if (! ($1 < 10) ) {
000d1cc1
JP
3525 CHK("USLEEP_RANGE",
3526 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $line);
1a15a250
PP
3527 }
3528 }
3529
09ef8725
PP
3530# warn about unexpectedly long msleep's
3531 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
3532 if ($1 < 20) {
000d1cc1
JP
3533 WARN("MSLEEP",
3534 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $line);
09ef8725
PP
3535 }
3536 }
3537
36ec1939
JP
3538# check for comparisons of jiffies
3539 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
3540 WARN("JIFFIES_COMPARISON",
3541 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
3542 }
3543
9d7a34a5
JP
3544# check for comparisons of get_jiffies_64()
3545 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
3546 WARN("JIFFIES_COMPARISON",
3547 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
3548 }
3549
00df344f 3550# warn about #ifdefs in C files
c45dcabd 3551# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
00df344f
AW
3552# print "#ifdef in C files should be avoided\n";
3553# print "$herecurr";
3554# $clean = 0;
3555# }
3556
22f2a2ef 3557# warn about spacing in #ifdefs
c45dcabd 3558 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
3705ce5b
JP
3559 if (ERROR("SPACING",
3560 "exactly one space required after that #$1\n" . $herecurr) &&
3561 $fix) {
3562 $fixed[$linenr - 1] =~
3563 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
3564 }
3565
22f2a2ef
AW
3566 }
3567
4a0df2ef 3568# check for spinlock_t definitions without a comment.
171ae1a4
AW
3569 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
3570 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
4a0df2ef
AW
3571 my $which = $1;
3572 if (!ctx_has_comment($first_line, $linenr)) {
000d1cc1
JP
3573 CHK("UNCOMMENTED_DEFINITION",
3574 "$1 definition without comment\n" . $herecurr);
4a0df2ef
AW
3575 }
3576 }
3577# check for memory barriers without a comment.
3578 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3579 if (!ctx_has_comment($first_line, $linenr)) {
000d1cc1
JP
3580 CHK("MEMORY_BARRIER",
3581 "memory barrier without comment\n" . $herecurr);
4a0df2ef
AW
3582 }
3583 }
3584# check of hardware specific defines
c45dcabd 3585 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
000d1cc1
JP
3586 CHK("ARCH_DEFINES",
3587 "architecture specific defines should be avoided\n" . $herecurr);
0a920b5b 3588 }
653d4876 3589
d4977c78
TK
3590# Check that the storage class is at the beginning of a declaration
3591 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
000d1cc1
JP
3592 WARN("STORAGE_CLASS",
3593 "storage class should be at the beginning of the declaration\n" . $herecurr)
d4977c78
TK
3594 }
3595
de7d4f0e
AW
3596# check the location of the inline attribute, that it is between
3597# storage class and type.
9c0ca6f9
AW
3598 if ($line =~ /\b$Type\s+$Inline\b/ ||
3599 $line =~ /\b$Inline\s+$Storage\b/) {
000d1cc1
JP
3600 ERROR("INLINE_LOCATION",
3601 "inline keyword should sit between storage class and type\n" . $herecurr);
de7d4f0e
AW
3602 }
3603
8905a67c
AW
3604# Check for __inline__ and __inline, prefer inline
3605 if ($line =~ /\b(__inline__|__inline)\b/) {
000d1cc1
JP
3606 WARN("INLINE",
3607 "plain inline is preferred over $1\n" . $herecurr);
8905a67c
AW
3608 }
3609
3d130fd0
JP
3610# Check for __attribute__ packed, prefer __packed
3611 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
000d1cc1
JP
3612 WARN("PREFER_PACKED",
3613 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3d130fd0
JP
3614 }
3615
39b7e287
JP
3616# Check for __attribute__ aligned, prefer __aligned
3617 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
000d1cc1
JP
3618 WARN("PREFER_ALIGNED",
3619 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
39b7e287
JP
3620 }
3621
5f14d3bd
JP
3622# Check for __attribute__ format(printf, prefer __printf
3623 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3624 WARN("PREFER_PRINTF",
3625 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr);
3626 }
3627
6061d949
JP
3628# Check for __attribute__ format(scanf, prefer __scanf
3629 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3630 WARN("PREFER_SCANF",
3631 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr);
3632 }
3633
8f53a9b8
JP
3634# check for sizeof(&)
3635 if ($line =~ /\bsizeof\s*\(\s*\&/) {
000d1cc1
JP
3636 WARN("SIZEOF_ADDRESS",
3637 "sizeof(& should be avoided\n" . $herecurr);
8f53a9b8
JP
3638 }
3639
66c80b60
JP
3640# check for sizeof without parenthesis
3641 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
3642 WARN("SIZEOF_PARENTHESIS",
3643 "sizeof $1 should be sizeof($1)\n" . $herecurr);
3644 }
3645
428e2fdc
JP
3646# check for line continuations in quoted strings with odd counts of "
3647 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
000d1cc1
JP
3648 WARN("LINE_CONTINUATIONS",
3649 "Avoid line continuations in quoted strings\n" . $herecurr);
428e2fdc
JP
3650 }
3651
88982fea
JP
3652# check for struct spinlock declarations
3653 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3654 WARN("USE_SPINLOCK_T",
3655 "struct spinlock should be spinlock_t\n" . $herecurr);
3656 }
3657
a6962d72
JP
3658# check for seq_printf uses that could be seq_puts
3659 if ($line =~ /\bseq_printf\s*\(/) {
3660 my $fmt = get_quoted_string($line, $rawline);
3661 if ($fmt !~ /[^\\]\%/) {
3662 WARN("PREFER_SEQ_PUTS",
3663 "Prefer seq_puts to seq_printf\n" . $herecurr);
3664 }
3665 }
3666
554e165c 3667# Check for misused memsets
d1fe9c09
JP
3668 if ($^V && $^V ge 5.10.0 &&
3669 defined $stat &&
d7c76ba7
JP
3670 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
3671
3672 my $ms_addr = $2;
d1fe9c09
JP
3673 my $ms_val = $7;
3674 my $ms_size = $12;
554e165c 3675
554e165c
AW
3676 if ($ms_size =~ /^(0x|)0$/i) {
3677 ERROR("MEMSET",
d7c76ba7 3678 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
554e165c
AW
3679 } elsif ($ms_size =~ /^(0x|)1$/i) {
3680 WARN("MEMSET",
d7c76ba7
JP
3681 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
3682 }
3683 }
3684
3685# typecasts on min/max could be min_t/max_t
d1fe9c09
JP
3686 if ($^V && $^V ge 5.10.0 &&
3687 defined $stat &&
d7c76ba7 3688 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
d1fe9c09 3689 if (defined $2 || defined $7) {
d7c76ba7
JP
3690 my $call = $1;
3691 my $cast1 = deparenthesize($2);
3692 my $arg1 = $3;
d1fe9c09
JP
3693 my $cast2 = deparenthesize($7);
3694 my $arg2 = $8;
d7c76ba7
JP
3695 my $cast;
3696
d1fe9c09 3697 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
d7c76ba7
JP
3698 $cast = "$cast1 or $cast2";
3699 } elsif ($cast1 ne "") {
3700 $cast = $cast1;
3701 } else {
3702 $cast = $cast2;
3703 }
3704 WARN("MINMAX",
3705 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
554e165c
AW
3706 }
3707 }
3708
4a273195
JP
3709# check usleep_range arguments
3710 if ($^V && $^V ge 5.10.0 &&
3711 defined $stat &&
3712 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
3713 my $min = $1;
3714 my $max = $7;
3715 if ($min eq $max) {
3716 WARN("USLEEP_RANGE",
3717 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3718 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
3719 $min > $max) {
3720 WARN("USLEEP_RANGE",
3721 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
3722 }
3723 }
3724
de7d4f0e 3725# check for new externs in .c files.
171ae1a4 3726 if ($realfile =~ /\.c$/ && defined $stat &&
c45dcabd 3727 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
171ae1a4 3728 {
c45dcabd
AW
3729 my $function_name = $1;
3730 my $paren_space = $2;
171ae1a4
AW
3731
3732 my $s = $stat;
3733 if (defined $cond) {
3734 substr($s, 0, length($cond), '');
3735 }
c45dcabd
AW
3736 if ($s =~ /^\s*;/ &&
3737 $function_name ne 'uninitialized_var')
3738 {
000d1cc1
JP
3739 WARN("AVOID_EXTERNS",
3740 "externs should be avoided in .c files\n" . $herecurr);
171ae1a4
AW
3741 }
3742
3743 if ($paren_space =~ /\n/) {
000d1cc1
JP
3744 WARN("FUNCTION_ARGUMENTS",
3745 "arguments for function declarations should follow identifier\n" . $herecurr);
171ae1a4 3746 }
9c9ba34e
AW
3747
3748 } elsif ($realfile =~ /\.c$/ && defined $stat &&
3749 $stat =~ /^.\s*extern\s+/)
3750 {
000d1cc1
JP
3751 WARN("AVOID_EXTERNS",
3752 "externs should be avoided in .c files\n" . $herecurr);
de7d4f0e
AW
3753 }
3754
3755# checks for new __setup's
3756 if ($rawline =~ /\b__setup\("([^"]*)"/) {
3757 my $name = $1;
3758
3759 if (!grep(/$name/, @setup_docs)) {
000d1cc1
JP
3760 CHK("UNDOCUMENTED_SETUP",
3761 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
de7d4f0e 3762 }
653d4876 3763 }
9c0ca6f9
AW
3764
3765# check for pointless casting of kmalloc return
caf2a54f 3766 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
000d1cc1
JP
3767 WARN("UNNECESSARY_CASTS",
3768 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
9c0ca6f9 3769 }
13214adf 3770
a640d25c
JP
3771# alloc style
3772# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
3773 if ($^V && $^V ge 5.10.0 &&
3774 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
3775 CHK("ALLOC_SIZEOF_STRUCT",
3776 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
3777 }
3778
972fdea2
JP
3779# check for krealloc arg reuse
3780 if ($^V && $^V ge 5.10.0 &&
3781 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
3782 WARN("KREALLOC_ARG_REUSE",
3783 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
3784 }
3785
5ce59ae0
JP
3786# check for alloc argument mismatch
3787 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
3788 WARN("ALLOC_ARRAY_ARGS",
3789 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
3790 }
3791
caf2a54f
JP
3792# check for multiple semicolons
3793 if ($line =~ /;\s*;\s*$/) {
d1e2ad07
JP
3794 WARN("ONE_SEMICOLON",
3795 "Statements terminations use 1 semicolon\n" . $herecurr);
3796 }
3797
3798# check for switch/default statements without a break;
3799 if ($^V && $^V ge 5.10.0 &&
3800 defined $stat &&
3801 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3802 my $ctx = '';
3803 my $herectx = $here . "\n";
3804 my $cnt = statement_rawlines($stat);
3805 for (my $n = 0; $n < $cnt; $n++) {
3806 $herectx .= raw_line($linenr, $n) . "\n";
3807 }
3808 WARN("DEFAULT_NO_BREAK",
3809 "switch default: should use break\n" . $herectx);
caf2a54f
JP
3810 }
3811
13214adf
AW
3812# check for gcc specific __FUNCTION__
3813 if ($line =~ /__FUNCTION__/) {
000d1cc1
JP
3814 WARN("USE_FUNC",
3815 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
13214adf 3816 }
773647a0 3817
2c92488a
JP
3818# check for use of yield()
3819 if ($line =~ /\byield\s*\(\s*\)/) {
3820 WARN("YIELD",
3821 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
3822 }
3823
179f8f40
JP
3824# check for comparisons against true and false
3825 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
3826 my $lead = $1;
3827 my $arg = $2;
3828 my $test = $3;
3829 my $otype = $4;
3830 my $trail = $5;
3831 my $op = "!";
3832
3833 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
3834
3835 my $type = lc($otype);
3836 if ($type =~ /^(?:true|false)$/) {
3837 if (("$test" eq "==" && "$type" eq "true") ||
3838 ("$test" eq "!=" && "$type" eq "false")) {
3839 $op = "";
3840 }
3841
3842 CHK("BOOL_COMPARISON",
3843 "Using comparison to $otype is error prone\n" . $herecurr);
3844
3845## maybe suggesting a correct construct would better
3846## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
3847
3848 }
3849 }
3850
4882720b
TG
3851# check for semaphores initialized locked
3852 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
000d1cc1
JP
3853 WARN("CONSIDER_COMPLETION",
3854 "consider using a completion\n" . $herecurr);
773647a0 3855 }
6712d858 3856
67d0a075
JP
3857# recommend kstrto* over simple_strto* and strict_strto*
3858 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
000d1cc1 3859 WARN("CONSIDER_KSTRTO",
67d0a075 3860 "$1 is obsolete, use k$3 instead\n" . $herecurr);
773647a0 3861 }
6712d858 3862
f3db6639
ME
3863# check for __initcall(), use device_initcall() explicitly please
3864 if ($line =~ /^.\s*__initcall\s*\(/) {
000d1cc1
JP
3865 WARN("USE_DEVICE_INITCALL",
3866 "please use device_initcall() instead of __initcall()\n" . $herecurr);
f3db6639 3867 }
6712d858 3868
79404849
ER
3869# check for various ops structs, ensure they are const.
3870 my $struct_ops = qr{acpi_dock_ops|
3871 address_space_operations|
3872 backlight_ops|
3873 block_device_operations|
3874 dentry_operations|
3875 dev_pm_ops|
3876 dma_map_ops|
3877 extent_io_ops|
3878 file_lock_operations|
3879 file_operations|
3880 hv_ops|
3881 ide_dma_ops|
3882 intel_dvo_dev_ops|
3883 item_operations|
3884 iwl_ops|
3885 kgdb_arch|
3886 kgdb_io|
3887 kset_uevent_ops|
3888 lock_manager_operations|
3889 microcode_ops|
3890 mtrr_ops|
3891 neigh_ops|
3892 nlmsvc_binding|
3893 pci_raw_ops|
3894 pipe_buf_operations|
3895 platform_hibernation_ops|
3896 platform_suspend_ops|
3897 proto_ops|
3898 rpc_pipe_ops|
3899 seq_operations|
3900 snd_ac97_build_ops|
3901 soc_pcmcia_socket_ops|
3902 stacktrace_ops|
3903 sysfs_ops|
3904 tty_operations|
3905 usb_mon_operations|
3906 wd_ops}x;
6903ffb2 3907 if ($line !~ /\bconst\b/ &&
79404849 3908 $line =~ /\bstruct\s+($struct_ops)\b/) {
000d1cc1
JP
3909 WARN("CONST_STRUCT",
3910 "struct $1 should normally be const\n" .
6903ffb2 3911 $herecurr);
2b6db5cb 3912 }
773647a0
AW
3913
3914# use of NR_CPUS is usually wrong
3915# ignore definitions of NR_CPUS and usage to define arrays as likely right
3916 if ($line =~ /\bNR_CPUS\b/ &&
c45dcabd
AW
3917 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
3918 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
171ae1a4
AW
3919 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
3920 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
3921 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
773647a0 3922 {
000d1cc1
JP
3923 WARN("NR_CPUS",
3924 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
773647a0 3925 }
9c9ba34e
AW
3926
3927# check for %L{u,d,i} in strings
3928 my $string;
3929 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
3930 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2a1bc5d5 3931 $string =~ s/%%/__/g;
9c9ba34e 3932 if ($string =~ /(?<!%)%L[udi]/) {
000d1cc1
JP
3933 WARN("PRINTF_L",
3934 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
9c9ba34e
AW
3935 last;
3936 }
3937 }
691d77b6
AW
3938
3939# whine mightly about in_atomic
3940 if ($line =~ /\bin_atomic\s*\(/) {
3941 if ($realfile =~ m@^drivers/@) {
000d1cc1
JP
3942 ERROR("IN_ATOMIC",
3943 "do not use in_atomic in drivers\n" . $herecurr);
f4a87736 3944 } elsif ($realfile !~ m@^kernel/@) {
000d1cc1
JP
3945 WARN("IN_ATOMIC",
3946 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
691d77b6
AW
3947 }
3948 }
1704f47b
PZ
3949
3950# check for lockdep_set_novalidate_class
3951 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
3952 $line =~ /__lockdep_no_validate__\s*\)/ ) {
3953 if ($realfile !~ m@^kernel/lockdep@ &&
3954 $realfile !~ m@^include/linux/lockdep@ &&
3955 $realfile !~ m@^drivers/base/core@) {
000d1cc1
JP
3956 ERROR("LOCKDEP",
3957 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
1704f47b
PZ
3958 }
3959 }
88f8831c
DJ
3960
3961 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
3962 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
000d1cc1
JP
3963 WARN("EXPORTED_WORLD_WRITABLE",
3964 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
88f8831c 3965 }
13214adf
AW
3966 }
3967
3968 # If we have no input at all, then there is nothing to report on
3969 # so just keep quiet.
3970 if ($#rawlines == -1) {
3971 exit(0);
0a920b5b
AW
3972 }
3973
8905a67c
AW
3974 # In mailback mode only produce a report in the negative, for
3975 # things that appear to be patches.
3976 if ($mailback && ($clean == 1 || !$is_patch)) {
3977 exit(0);
3978 }
3979
3980 # This is not a patch, and we are are in 'no-patch' mode so
3981 # just keep quiet.
3982 if (!$chk_patch && !$is_patch) {
3983 exit(0);
3984 }
3985
3986 if (!$is_patch) {
000d1cc1
JP
3987 ERROR("NOT_UNIFIED_DIFF",
3988 "Does not appear to be a unified-diff format patch\n");
0a920b5b
AW
3989 }
3990 if ($is_patch && $chk_signoff && $signoff == 0) {
000d1cc1
JP
3991 ERROR("MISSING_SIGN_OFF",
3992 "Missing Signed-off-by: line(s)\n");
0a920b5b
AW
3993 }
3994
8905a67c 3995 print report_dump();
13214adf
AW
3996 if ($summary && !($clean == 1 && $quiet == 1)) {
3997 print "$filename " if ($summary_file);
8905a67c
AW
3998 print "total: $cnt_error errors, $cnt_warn warnings, " .
3999 (($check)? "$cnt_chk checks, " : "") .
4000 "$cnt_lines lines checked\n";
4001 print "\n" if ($quiet == 0);
f0a594c1 4002 }
8905a67c 4003
d2c0a235 4004 if ($quiet == 0) {
d1fe9c09
JP
4005
4006 if ($^V lt 5.10.0) {
4007 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
4008 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
4009 }
4010
d2c0a235
AW
4011 # If there were whitespace errors which cleanpatch can fix
4012 # then suggest that.
4013 if ($rpt_cleaners) {
4014 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
4015 print " scripts/cleanfile\n\n";
b0781216 4016 $rpt_cleaners = 0;
d2c0a235
AW
4017 }
4018 }
4019
11232688 4020 if ($quiet == 0 && keys %ignore_type) {
000d1cc1
JP
4021 print "NOTE: Ignored message types:";
4022 foreach my $ignore (sort keys %ignore_type) {
4023 print " $ignore";
4024 }
11232688 4025 print "\n\n";
000d1cc1
JP
4026 }
4027
3705ce5b
JP
4028 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4029 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes";
4030 my $linecount = 0;
4031 my $f;
4032
4033 open($f, '>', $newfile)
4034 or die "$P: Can't open $newfile for write\n";
4035 foreach my $fixed_line (@fixed) {
4036 $linecount++;
4037 if ($file) {
4038 if ($linecount > 3) {
4039 $fixed_line =~ s/^\+//;
4040 print $f $fixed_line. "\n";
4041 }
4042 } else {
4043 print $f $fixed_line . "\n";
4044 }
4045 }
4046 close($f);
4047
4048 if (!$quiet) {
4049 print << "EOM";
4050Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
4051
4052Do _NOT_ trust the results written to this file.
4053Do _NOT_ submit these changes without inspecting them for correctness.
4054
4055This EXPERIMENTAL file is simply a convenience to help rewrite patches.
4056No warranties, expressed or implied...
4057
4058EOM
4059 }
4060 }
4061
0a920b5b 4062 if ($clean == 1 && $quiet == 0) {
c2fdda0d 4063 print "$vname has no obvious style problems and is ready for submission.\n"
0a920b5b
AW
4064 }
4065 if ($clean == 0 && $quiet == 0) {
000d1cc1
JP
4066 print << "EOM";
4067$vname has style problems, please review.
4068
4069If any of these errors are false positives, please report
4070them to the maintainer, see CHECKPATCH in MAINTAINERS.
4071EOM
0a920b5b 4072 }
13214adf 4073
0a920b5b
AW
4074 return $clean;
4075}