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