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