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