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