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