blob: c0d3d96b077d2d4877c4aa683461286705bb3bb8 [file] [log] [blame]
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01001#!/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
8use strict;
9use POSIX;
10use File::Basename;
11use Cwd 'abs_path';
12
13my $P = $0;
14my $D = dirname(abs_path($P));
15
16my $V = '0.32';
17
18use Getopt::Long qw(:config no_auto_abbrev);
19
20my $quiet = 0;
21my $tree = 1;
22my $chk_signoff = 1;
23my $chk_patch = 1;
24my $tst_only;
25my $emacs = 0;
26my $terse = 0;
27my $file = 0;
28my $check = 0;
29my $check_orig = 0;
30my $summary = 1;
31my $mailback = 0;
32my $summary_file = 0;
33my $show_types = 0;
34my $fix = 0;
35my $fix_inplace = 0;
36my $root;
37my %debug;
38my %camelcase = ();
39my %use_type = ();
40my @use = ();
41my %ignore_type = ();
42my @ignore = ();
43my $help = 0;
44my $configuration_file = ".checkpatch.conf";
45my $max_line_length = 80;
46my $ignore_perl_version = 0;
47my $minimum_perl_version = 5.10.0;
48my $min_conf_desc_length = 4;
49my $spelling_file = "$D/spelling.txt";
50
51sub help {
52 my ($exitcode) = @_;
53
54 print << "EOM";
55Usage: $P [OPTION]... [FILE]...
56Version: $V
57
58Options:
59 -q, --quiet quiet
60 --no-tree run without a kernel tree
61 --no-signoff do not check for 'Signed-off-by' line
62 --patch treat FILE as patchfile (default)
63 --emacs emacs compile window format
64 --terse one line per report
65 -f, --file treat FILE as regular source file
66 --subjective, --strict enable more subjective tests
67 --types TYPE(,TYPE2...) show only these comma separated message types
68 --ignore TYPE(,TYPE2...) ignore various comma separated message types
69 --max-line-length=n set the maximum line length, if exceeded, warn
70 --min-conf-desc-length=n set the min description length, if shorter, warn
71 --show-types show the message "types" in the output
72 --root=PATH PATH to the kernel tree root
73 --no-summary suppress the per-file summary
74 --mailback only produce a report in case of warnings/errors
75 --summary-file include the filename in summary
76 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
77 'values', 'possible', 'type', and 'attr' (default
78 is all off)
79 --test-only=WORD report only warnings/errors containing WORD
80 literally
81 --fix EXPERIMENTAL - may create horrible results
82 If correctable single-line errors exist, create
83 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
84 with potential errors corrected to the preferred
85 checkpatch style
86 --fix-inplace EXPERIMENTAL - may create horrible results
87 Is the same as --fix, but overwrites the input
88 file. It's your fault if there's no backup or git
89 --ignore-perl-version override checking of perl version. expect
90 runtime errors.
91 -h, --help, --version display this help and exit
92
93When FILE is - read standard input.
94EOM
95
96 exit($exitcode);
97}
98
99my $conf = which_conf($configuration_file);
100if (-f $conf) {
101 my @conf_args;
102 open(my $conffile, '<', "$conf")
103 or warn "$P: Can't find a readable $configuration_file file $!\n";
104
105 while (<$conffile>) {
106 my $line = $_;
107
108 $line =~ s/\s*\n?$//g;
109 $line =~ s/^\s*//g;
110 $line =~ s/\s+/ /g;
111
112 next if ($line =~ m/^\s*#/);
113 next if ($line =~ m/^\s*$/);
114
115 my @words = split(" ", $line);
116 foreach my $word (@words) {
117 last if ($word =~ m/^#/);
118 push (@conf_args, $word);
119 }
120 }
121 close($conffile);
122 unshift(@ARGV, @conf_args) if @conf_args;
123}
124
125GetOptions(
126 'q|quiet+' => \$quiet,
127 'tree!' => \$tree,
128 'signoff!' => \$chk_signoff,
129 'patch!' => \$chk_patch,
130 'emacs!' => \$emacs,
131 'terse!' => \$terse,
132 'f|file!' => \$file,
133 'subjective!' => \$check,
134 'strict!' => \$check,
135 'ignore=s' => \@ignore,
136 'types=s' => \@use,
137 'show-types!' => \$show_types,
138 'max-line-length=i' => \$max_line_length,
139 'min-conf-desc-length=i' => \$min_conf_desc_length,
140 'root=s' => \$root,
141 'summary!' => \$summary,
142 'mailback!' => \$mailback,
143 'summary-file!' => \$summary_file,
144 'fix!' => \$fix,
145 'fix-inplace!' => \$fix_inplace,
146 'ignore-perl-version!' => \$ignore_perl_version,
147 'debug=s' => \%debug,
148 'test-only=s' => \$tst_only,
149 'h|help' => \$help,
150 'version' => \$help
151) or help(1);
152
153help(0) if ($help);
154
155$fix = 1 if ($fix_inplace);
156$check_orig = $check;
157
158my $exit = 0;
159
160if ($^V && $^V lt $minimum_perl_version) {
161 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
162 if (!$ignore_perl_version) {
163 exit(1);
164 }
165}
166
167if ($#ARGV < 0) {
168 print "$P: no input files\n";
169 exit(1);
170}
171
172sub hash_save_array_words {
173 my ($hashRef, $arrayRef) = @_;
174
175 my @array = split(/,/, join(',', @$arrayRef));
176 foreach my $word (@array) {
177 $word =~ s/\s*\n?$//g;
178 $word =~ s/^\s*//g;
179 $word =~ s/\s+/ /g;
180 $word =~ tr/[a-z]/[A-Z]/;
181
182 next if ($word =~ m/^\s*#/);
183 next if ($word =~ m/^\s*$/);
184
185 $hashRef->{$word}++;
186 }
187}
188
189sub hash_show_words {
190 my ($hashRef, $prefix) = @_;
191
192 if ($quiet == 0 && keys %$hashRef) {
193 print "NOTE: $prefix message types:";
194 foreach my $word (sort keys %$hashRef) {
195 print " $word";
196 }
197 print "\n\n";
198 }
199}
200
201hash_save_array_words(\%ignore_type, \@ignore);
202hash_save_array_words(\%use_type, \@use);
203
204my $dbg_values = 0;
205my $dbg_possible = 0;
206my $dbg_type = 0;
207my $dbg_attr = 0;
208for my $key (keys %debug) {
209 ## no critic
210 eval "\${dbg_$key} = '$debug{$key}';";
211 die "$@" if ($@);
212}
213
214my $rpt_cleaners = 0;
215
216if ($terse) {
217 $emacs = 1;
218 $quiet++;
219}
220
221if ($tree) {
222 if (defined $root) {
223 if (!top_of_kernel_tree($root)) {
224 die "$P: $root: --root does not point at a valid tree\n";
225 }
226 } else {
227 if (top_of_kernel_tree('.')) {
228 $root = '.';
Stefan Reinauer42412b62015-02-11 01:51:32 +0100229 } elsif ($0 =~ m@(.*)/util/lint/[^/]*$@ &&
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100230 top_of_kernel_tree($1)) {
231 $root = $1;
232 }
233 }
234
235 if (!defined $root) {
236 print "Must be run from the top-level dir. of a kernel tree\n";
237 exit(2);
238 }
239}
240
241my $emitted_corrupt = 0;
242
243our $Ident = qr{
244 [A-Za-z_][A-Za-z\d_]*
245 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
246 }x;
247our $Storage = qr{extern|static|asmlinkage};
248our $Sparse = qr{
249 __user|
250 __kernel|
251 __force|
252 __iomem|
253 __must_check|
254 __init_refok|
255 __kprobes|
256 __ref|
257 __rcu
258 }x;
259our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
260our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
261our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
262our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
263our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
264
265# Notes to $Attribute:
266# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
267our $Attribute = qr{
268 const|
269 __percpu|
270 __nocast|
271 __safe|
272 __bitwise__|
273 __packed__|
274 __packed2__|
275 __naked|
276 __maybe_unused|
277 __always_unused|
278 __noreturn|
279 __used|
280 __cold|
281 __noclone|
282 __deprecated|
283 __read_mostly|
284 __kprobes|
285 $InitAttribute|
286 ____cacheline_aligned|
287 ____cacheline_aligned_in_smp|
288 ____cacheline_internodealigned_in_smp|
289 __weak
290 }x;
291our $Modifier;
292our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
293our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
294our $Lval = qr{$Ident(?:$Member)*};
295
296our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
297our $Binary = qr{(?i)0b[01]+$Int_type?};
298our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
299our $Int = qr{[0-9]+$Int_type?};
300our $Octal = qr{0[0-7]+$Int_type?};
301our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
302our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
303our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
304our $Float = qr{$Float_hex|$Float_dec|$Float_int};
305our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
306our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
307our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
308our $Arithmetic = qr{\+|-|\*|\/|%};
309our $Operators = qr{
310 <=|>=|==|!=|
311 =>|->|<<|>>|<|>|!|~|
312 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
313 }x;
314
315our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
316
317our $NonptrType;
318our $NonptrTypeMisordered;
319our $NonptrTypeWithAttr;
320our $Type;
321our $TypeMisordered;
322our $Declare;
323our $DeclareMisordered;
324
325our $NON_ASCII_UTF8 = qr{
326 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
327 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
328 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
329 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
330 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
331 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
332 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
333}x;
334
335our $UTF8 = qr{
336 [\x09\x0A\x0D\x20-\x7E] # ASCII
337 | $NON_ASCII_UTF8
338}x;
339
340our $typeTypedefs = qr{(?x:
341 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
342 atomic_t
343)};
344
345our $logFunctions = qr{(?x:
346 printk(?:_ratelimited|_once|)|
347 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
348 WARN(?:_RATELIMIT|_ONCE|)|
349 panic|
350 MODULE_[A-Z_]+|
351 seq_vprintf|seq_printf|seq_puts
352)};
353
354our $signature_tags = qr{(?xi:
355 Signed-off-by:|
356 Acked-by:|
357 Tested-by:|
358 Reviewed-by:|
359 Reported-by:|
360 Suggested-by:|
361 To:|
362 Cc:
363)};
364
365our @typeListMisordered = (
366 qr{char\s+(?:un)?signed},
367 qr{int\s+(?:(?:un)?signed\s+)?short\s},
368 qr{int\s+short(?:\s+(?:un)?signed)},
369 qr{short\s+int(?:\s+(?:un)?signed)},
370 qr{(?:un)?signed\s+int\s+short},
371 qr{short\s+(?:un)?signed},
372 qr{long\s+int\s+(?:un)?signed},
373 qr{int\s+long\s+(?:un)?signed},
374 qr{long\s+(?:un)?signed\s+int},
375 qr{int\s+(?:un)?signed\s+long},
376 qr{int\s+(?:un)?signed},
377 qr{int\s+long\s+long\s+(?:un)?signed},
378 qr{long\s+long\s+int\s+(?:un)?signed},
379 qr{long\s+long\s+(?:un)?signed\s+int},
380 qr{long\s+long\s+(?:un)?signed},
381 qr{long\s+(?:un)?signed},
382);
383
384our @typeList = (
385 qr{void},
386 qr{(?:(?:un)?signed\s+)?char},
387 qr{(?:(?:un)?signed\s+)?short\s+int},
388 qr{(?:(?:un)?signed\s+)?short},
389 qr{(?:(?:un)?signed\s+)?int},
390 qr{(?:(?:un)?signed\s+)?long\s+int},
391 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
392 qr{(?:(?:un)?signed\s+)?long\s+long},
393 qr{(?:(?:un)?signed\s+)?long},
394 qr{(?:un)?signed},
395 qr{float},
396 qr{double},
397 qr{bool},
398 qr{struct\s+$Ident},
399 qr{union\s+$Ident},
400 qr{enum\s+$Ident},
401 qr{${Ident}_t},
402 qr{${Ident}_handler},
403 qr{${Ident}_handler_fn},
404 @typeListMisordered,
405);
406our @typeListWithAttr = (
407 @typeList,
408 qr{struct\s+$InitAttribute\s+$Ident},
409 qr{union\s+$InitAttribute\s+$Ident},
410);
411
412our @modifierList = (
413 qr{fastcall},
414);
415
416our @mode_permission_funcs = (
417 ["module_param", 3],
418 ["module_param_(?:array|named|string)", 4],
419 ["module_param_array_named", 5],
420 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
421 ["proc_create(?:_data|)", 2],
422 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
423);
424
425#Create a search pattern for all these functions to speed up a loop below
426our $mode_perms_search = "";
427foreach my $entry (@mode_permission_funcs) {
428 $mode_perms_search .= '|' if ($mode_perms_search ne "");
429 $mode_perms_search .= $entry->[0];
430}
431
432our $allowed_asm_includes = qr{(?x:
433 irq|
434 memory|
435 time|
436 reboot
437)};
438# memory.h: ARM has a custom one
439
440# Load common spelling mistakes and build regular expression list.
441my $misspellings;
442my %spelling_fix;
443
444if (open(my $spelling, '<', $spelling_file)) {
445 my @spelling_list;
446 while (<$spelling>) {
447 my $line = $_;
448
449 $line =~ s/\s*\n?$//g;
450 $line =~ s/^\s*//g;
451
452 next if ($line =~ m/^\s*#/);
453 next if ($line =~ m/^\s*$/);
454
455 my ($suspect, $fix) = split(/\|\|/, $line);
456
457 push(@spelling_list, $suspect);
458 $spelling_fix{$suspect} = $fix;
459 }
460 close($spelling);
461 $misspellings = join("|", @spelling_list);
462} else {
463 warn "No typos will be found - file '$spelling_file': $!\n";
464}
465
466sub build_types {
467 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
468 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
469 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
470 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
471 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
472 $NonptrType = qr{
473 (?:$Modifier\s+|const\s+)*
474 (?:
475 (?:typeof|__typeof__)\s*\([^\)]*\)|
476 (?:$typeTypedefs\b)|
477 (?:${all}\b)
478 )
479 (?:\s+$Modifier|\s+const)*
480 }x;
481 $NonptrTypeMisordered = qr{
482 (?:$Modifier\s+|const\s+)*
483 (?:
484 (?:${Misordered}\b)
485 )
486 (?:\s+$Modifier|\s+const)*
487 }x;
488 $NonptrTypeWithAttr = qr{
489 (?:$Modifier\s+|const\s+)*
490 (?:
491 (?:typeof|__typeof__)\s*\([^\)]*\)|
492 (?:$typeTypedefs\b)|
493 (?:${allWithAttr}\b)
494 )
495 (?:\s+$Modifier|\s+const)*
496 }x;
497 $Type = qr{
498 $NonptrType
499 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
500 (?:\s+$Inline|\s+$Modifier)*
501 }x;
502 $TypeMisordered = qr{
503 $NonptrTypeMisordered
504 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
505 (?:\s+$Inline|\s+$Modifier)*
506 }x;
507 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
508 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
509}
510build_types();
511
512our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
513
514# Using $balanced_parens, $LvalOrFunc, or $FuncArg
515# requires at least perl version v5.10.0
516# Any use must be runtime checked with $^V
517
518our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
519our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
520our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
521
522our $declaration_macros = qr{(?x:
523 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(|
524 (?:$Storage\s+)?LIST_HEAD\s*\(|
525 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
526)};
527
528sub deparenthesize {
529 my ($string) = @_;
530 return "" if (!defined($string));
531
532 while ($string =~ /^\s*\(.*\)\s*$/) {
533 $string =~ s@^\s*\(\s*@@;
534 $string =~ s@\s*\)\s*$@@;
535 }
536
537 $string =~ s@\s+@ @g;
538
539 return $string;
540}
541
542sub seed_camelcase_file {
543 my ($file) = @_;
544
545 return if (!(-f $file));
546
547 local $/;
548
549 open(my $include_file, '<', "$file")
550 or warn "$P: Can't read '$file' $!\n";
551 my $text = <$include_file>;
552 close($include_file);
553
554 my @lines = split('\n', $text);
555
556 foreach my $line (@lines) {
557 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
558 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
559 $camelcase{$1} = 1;
560 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
561 $camelcase{$1} = 1;
562 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
563 $camelcase{$1} = 1;
564 }
565 }
566}
567
568my $camelcase_seeded = 0;
569sub seed_camelcase_includes {
570 return if ($camelcase_seeded);
571
572 my $files;
573 my $camelcase_cache = "";
574 my @include_files = ();
575
576 $camelcase_seeded = 1;
577
578 if (-e ".git") {
579 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
580 chomp $git_last_include_commit;
581 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
582 } else {
583 my $last_mod_date = 0;
584 $files = `find $root/include -name "*.h"`;
585 @include_files = split('\n', $files);
586 foreach my $file (@include_files) {
587 my $date = POSIX::strftime("%Y%m%d%H%M",
588 localtime((stat $file)[9]));
589 $last_mod_date = $date if ($last_mod_date < $date);
590 }
591 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
592 }
593
594 if ($camelcase_cache ne "" && -f $camelcase_cache) {
595 open(my $camelcase_file, '<', "$camelcase_cache")
596 or warn "$P: Can't read '$camelcase_cache' $!\n";
597 while (<$camelcase_file>) {
598 chomp;
599 $camelcase{$_} = 1;
600 }
601 close($camelcase_file);
602
603 return;
604 }
605
606 if (-e ".git") {
607 $files = `git ls-files "include/*.h"`;
608 @include_files = split('\n', $files);
609 }
610
611 foreach my $file (@include_files) {
612 seed_camelcase_file($file);
613 }
614
615 if ($camelcase_cache ne "") {
616 unlink glob ".checkpatch-camelcase.*";
617 open(my $camelcase_file, '>', "$camelcase_cache")
618 or warn "$P: Can't write '$camelcase_cache' $!\n";
619 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
620 print $camelcase_file ("$_\n");
621 }
622 close($camelcase_file);
623 }
624}
625
626sub git_commit_info {
627 my ($commit, $id, $desc) = @_;
628
629 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
630
631 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
632 $output =~ s/^\s*//gm;
633 my @lines = split("\n", $output);
634
635 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
636# Maybe one day convert this block of bash into something that returns
637# all matching commit ids, but it's very slow...
638#
639# echo "checking commits $1..."
640# git rev-list --remotes | grep -i "^$1" |
641# while read line ; do
642# git log --format='%H %s' -1 $line |
643# echo "commit $(cut -c 1-12,41-)"
644# done
645 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
646 } else {
647 $id = substr($lines[0], 0, 12);
648 $desc = substr($lines[0], 41);
649 }
650
651 return ($id, $desc);
652}
653
654$chk_signoff = 0 if ($file);
655
656my @rawlines = ();
657my @lines = ();
658my @fixed = ();
659my @fixed_inserted = ();
660my @fixed_deleted = ();
661my $fixlinenr = -1;
662
663my $vname;
664for my $filename (@ARGV) {
665 my $FILE;
666 if ($file) {
667 open($FILE, '-|', "diff -u /dev/null $filename") ||
668 die "$P: $filename: diff failed - $!\n";
669 } elsif ($filename eq '-') {
670 open($FILE, '<&STDIN');
671 } else {
672 open($FILE, '<', "$filename") ||
673 die "$P: $filename: open failed - $!\n";
674 }
675 if ($filename eq '-') {
676 $vname = 'Your patch';
677 } else {
678 $vname = $filename;
679 }
680 while (<$FILE>) {
681 chomp;
682 push(@rawlines, $_);
683 }
684 close($FILE);
685 if (!process($filename)) {
686 $exit = 1;
687 }
688 @rawlines = ();
689 @lines = ();
690 @fixed = ();
691 @fixed_inserted = ();
692 @fixed_deleted = ();
693 $fixlinenr = -1;
694}
695
696exit($exit);
697
698sub top_of_kernel_tree {
699 my ($root) = @_;
700
701 my @tree_check = (
Stefan Reinauer42412b62015-02-11 01:51:32 +0100702 "COPYING", "README", "Makefile", "Makefile.inc",
703 "src", "documentation", "util", "payloads",
Stefan Reinauer44d0fd92015-02-11 01:49:00 +0100704 );
705
706 foreach my $check (@tree_check) {
707 if (! -e $root . '/' . $check) {
708 return 0;
709 }
710 }
711 return 1;
712}
713
714sub parse_email {
715 my ($formatted_email) = @_;
716
717 my $name = "";
718 my $address = "";
719 my $comment = "";
720
721 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
722 $name = $1;
723 $address = $2;
724 $comment = $3 if defined $3;
725 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
726 $address = $1;
727 $comment = $2 if defined $2;
728 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
729 $address = $1;
730 $comment = $2 if defined $2;
731 $formatted_email =~ s/$address.*$//;
732 $name = $formatted_email;
733 $name = trim($name);
734 $name =~ s/^\"|\"$//g;
735 # If there's a name left after stripping spaces and
736 # leading quotes, and the address doesn't have both
737 # leading and trailing angle brackets, the address
738 # is invalid. ie:
739 # "joe smith joe@smith.com" bad
740 # "joe smith <joe@smith.com" bad
741 if ($name ne "" && $address !~ /^<[^>]+>$/) {
742 $name = "";
743 $address = "";
744 $comment = "";
745 }
746 }
747
748 $name = trim($name);
749 $name =~ s/^\"|\"$//g;
750 $address = trim($address);
751 $address =~ s/^\<|\>$//g;
752
753 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
754 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
755 $name = "\"$name\"";
756 }
757
758 return ($name, $address, $comment);
759}
760
761sub format_email {
762 my ($name, $address) = @_;
763
764 my $formatted_email;
765
766 $name = trim($name);
767 $name =~ s/^\"|\"$//g;
768 $address = trim($address);
769
770 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
771 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
772 $name = "\"$name\"";
773 }
774
775 if ("$name" eq "") {
776 $formatted_email = "$address";
777 } else {
778 $formatted_email = "$name <$address>";
779 }
780
781 return $formatted_email;
782}
783
784sub which {
785 my ($bin) = @_;
786
787 foreach my $path (split(/:/, $ENV{PATH})) {
788 if (-e "$path/$bin") {
789 return "$path/$bin";
790 }
791 }
792
793 return "";
794}
795
796sub which_conf {
797 my ($conf) = @_;
798
799 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
800 if (-e "$path/$conf") {
801 return "$path/$conf";
802 }
803 }
804
805 return "";
806}
807
808sub expand_tabs {
809 my ($str) = @_;
810
811 my $res = '';
812 my $n = 0;
813 for my $c (split(//, $str)) {
814 if ($c eq "\t") {
815 $res .= ' ';
816 $n++;
817 for (; ($n % 8) != 0; $n++) {
818 $res .= ' ';
819 }
820 next;
821 }
822 $res .= $c;
823 $n++;
824 }
825
826 return $res;
827}
828sub copy_spacing {
829 (my $res = shift) =~ tr/\t/ /c;
830 return $res;
831}
832
833sub line_stats {
834 my ($line) = @_;
835
836 # Drop the diff line leader and expand tabs
837 $line =~ s/^.//;
838 $line = expand_tabs($line);
839
840 # Pick the indent from the front of the line.
841 my ($white) = ($line =~ /^(\s*)/);
842
843 return (length($line), length($white));
844}
845
846my $sanitise_quote = '';
847
848sub sanitise_line_reset {
849 my ($in_comment) = @_;
850
851 if ($in_comment) {
852 $sanitise_quote = '*/';
853 } else {
854 $sanitise_quote = '';
855 }
856}
857sub sanitise_line {
858 my ($line) = @_;
859
860 my $res = '';
861 my $l = '';
862
863 my $qlen = 0;
864 my $off = 0;
865 my $c;
866
867 # Always copy over the diff marker.
868 $res = substr($line, 0, 1);
869
870 for ($off = 1; $off < length($line); $off++) {
871 $c = substr($line, $off, 1);
872
873 # Comments we are wacking completly including the begin
874 # and end, all to $;.
875 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
876 $sanitise_quote = '*/';
877
878 substr($res, $off, 2, "$;$;");
879 $off++;
880 next;
881 }
882 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
883 $sanitise_quote = '';
884 substr($res, $off, 2, "$;$;");
885 $off++;
886 next;
887 }
888 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
889 $sanitise_quote = '//';
890
891 substr($res, $off, 2, $sanitise_quote);
892 $off++;
893 next;
894 }
895
896 # A \ in a string means ignore the next character.
897 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
898 $c eq "\\") {
899 substr($res, $off, 2, 'XX');
900 $off++;
901 next;
902 }
903 # Regular quotes.
904 if ($c eq "'" || $c eq '"') {
905 if ($sanitise_quote eq '') {
906 $sanitise_quote = $c;
907
908 substr($res, $off, 1, $c);
909 next;
910 } elsif ($sanitise_quote eq $c) {
911 $sanitise_quote = '';
912 }
913 }
914
915 #print "c<$c> SQ<$sanitise_quote>\n";
916 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
917 substr($res, $off, 1, $;);
918 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
919 substr($res, $off, 1, $;);
920 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
921 substr($res, $off, 1, 'X');
922 } else {
923 substr($res, $off, 1, $c);
924 }
925 }
926
927 if ($sanitise_quote eq '//') {
928 $sanitise_quote = '';
929 }
930
931 # The pathname on a #include may be surrounded by '<' and '>'.
932 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
933 my $clean = 'X' x length($1);
934 $res =~ s@\<.*\>@<$clean>@;
935
936 # The whole of a #error is a string.
937 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
938 my $clean = 'X' x length($1);
939 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
940 }
941
942 return $res;
943}
944
945sub get_quoted_string {
946 my ($line, $rawline) = @_;
947
948 return "" if ($line !~ m/(\"[X\t]+\")/g);
949 return substr($rawline, $-[0], $+[0] - $-[0]);
950}
951
952sub ctx_statement_block {
953 my ($linenr, $remain, $off) = @_;
954 my $line = $linenr - 1;
955 my $blk = '';
956 my $soff = $off;
957 my $coff = $off - 1;
958 my $coff_set = 0;
959
960 my $loff = 0;
961
962 my $type = '';
963 my $level = 0;
964 my @stack = ();
965 my $p;
966 my $c;
967 my $len = 0;
968
969 my $remainder;
970 while (1) {
971 @stack = (['', 0]) if ($#stack == -1);
972
973 #warn "CSB: blk<$blk> remain<$remain>\n";
974 # If we are about to drop off the end, pull in more
975 # context.
976 if ($off >= $len) {
977 for (; $remain > 0; $line++) {
978 last if (!defined $lines[$line]);
979 next if ($lines[$line] =~ /^-/);
980 $remain--;
981 $loff = $len;
982 $blk .= $lines[$line] . "\n";
983 $len = length($blk);
984 $line++;
985 last;
986 }
987 # Bail if there is no further context.
988 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
989 if ($off >= $len) {
990 last;
991 }
992 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
993 $level++;
994 $type = '#';
995 }
996 }
997 $p = $c;
998 $c = substr($blk, $off, 1);
999 $remainder = substr($blk, $off);
1000
1001 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1002
1003 # Handle nested #if/#else.
1004 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1005 push(@stack, [ $type, $level ]);
1006 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1007 ($type, $level) = @{$stack[$#stack - 1]};
1008 } elsif ($remainder =~ /^#\s*endif\b/) {
1009 ($type, $level) = @{pop(@stack)};
1010 }
1011
1012 # Statement ends at the ';' or a close '}' at the
1013 # outermost level.
1014 if ($level == 0 && $c eq ';') {
1015 last;
1016 }
1017
1018 # An else is really a conditional as long as its not else if
1019 if ($level == 0 && $coff_set == 0 &&
1020 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1021 $remainder =~ /^(else)(?:\s|{)/ &&
1022 $remainder !~ /^else\s+if\b/) {
1023 $coff = $off + length($1) - 1;
1024 $coff_set = 1;
1025 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1026 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1027 }
1028
1029 if (($type eq '' || $type eq '(') && $c eq '(') {
1030 $level++;
1031 $type = '(';
1032 }
1033 if ($type eq '(' && $c eq ')') {
1034 $level--;
1035 $type = ($level != 0)? '(' : '';
1036
1037 if ($level == 0 && $coff < $soff) {
1038 $coff = $off;
1039 $coff_set = 1;
1040 #warn "CSB: mark coff<$coff>\n";
1041 }
1042 }
1043 if (($type eq '' || $type eq '{') && $c eq '{') {
1044 $level++;
1045 $type = '{';
1046 }
1047 if ($type eq '{' && $c eq '}') {
1048 $level--;
1049 $type = ($level != 0)? '{' : '';
1050
1051 if ($level == 0) {
1052 if (substr($blk, $off + 1, 1) eq ';') {
1053 $off++;
1054 }
1055 last;
1056 }
1057 }
1058 # Preprocessor commands end at the newline unless escaped.
1059 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1060 $level--;
1061 $type = '';
1062 $off++;
1063 last;
1064 }
1065 $off++;
1066 }
1067 # We are truly at the end, so shuffle to the next line.
1068 if ($off == $len) {
1069 $loff = $len + 1;
1070 $line++;
1071 $remain--;
1072 }
1073
1074 my $statement = substr($blk, $soff, $off - $soff + 1);
1075 my $condition = substr($blk, $soff, $coff - $soff + 1);
1076
1077 #warn "STATEMENT<$statement>\n";
1078 #warn "CONDITION<$condition>\n";
1079
1080 #print "coff<$coff> soff<$off> loff<$loff>\n";
1081
1082 return ($statement, $condition,
1083 $line, $remain + 1, $off - $loff + 1, $level);
1084}
1085
1086sub statement_lines {
1087 my ($stmt) = @_;
1088
1089 # Strip the diff line prefixes and rip blank lines at start and end.
1090 $stmt =~ s/(^|\n)./$1/g;
1091 $stmt =~ s/^\s*//;
1092 $stmt =~ s/\s*$//;
1093
1094 my @stmt_lines = ($stmt =~ /\n/g);
1095
1096 return $#stmt_lines + 2;
1097}
1098
1099sub statement_rawlines {
1100 my ($stmt) = @_;
1101
1102 my @stmt_lines = ($stmt =~ /\n/g);
1103
1104 return $#stmt_lines + 2;
1105}
1106
1107sub statement_block_size {
1108 my ($stmt) = @_;
1109
1110 $stmt =~ s/(^|\n)./$1/g;
1111 $stmt =~ s/^\s*{//;
1112 $stmt =~ s/}\s*$//;
1113 $stmt =~ s/^\s*//;
1114 $stmt =~ s/\s*$//;
1115
1116 my @stmt_lines = ($stmt =~ /\n/g);
1117 my @stmt_statements = ($stmt =~ /;/g);
1118
1119 my $stmt_lines = $#stmt_lines + 2;
1120 my $stmt_statements = $#stmt_statements + 1;
1121
1122 if ($stmt_lines > $stmt_statements) {
1123 return $stmt_lines;
1124 } else {
1125 return $stmt_statements;
1126 }
1127}
1128
1129sub ctx_statement_full {
1130 my ($linenr, $remain, $off) = @_;
1131 my ($statement, $condition, $level);
1132
1133 my (@chunks);
1134
1135 # Grab the first conditional/block pair.
1136 ($statement, $condition, $linenr, $remain, $off, $level) =
1137 ctx_statement_block($linenr, $remain, $off);
1138 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1139 push(@chunks, [ $condition, $statement ]);
1140 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1141 return ($level, $linenr, @chunks);
1142 }
1143
1144 # Pull in the following conditional/block pairs and see if they
1145 # could continue the statement.
1146 for (;;) {
1147 ($statement, $condition, $linenr, $remain, $off, $level) =
1148 ctx_statement_block($linenr, $remain, $off);
1149 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1150 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1151 #print "C: push\n";
1152 push(@chunks, [ $condition, $statement ]);
1153 }
1154
1155 return ($level, $linenr, @chunks);
1156}
1157
1158sub ctx_block_get {
1159 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1160 my $line;
1161 my $start = $linenr - 1;
1162 my $blk = '';
1163 my @o;
1164 my @c;
1165 my @res = ();
1166
1167 my $level = 0;
1168 my @stack = ($level);
1169 for ($line = $start; $remain > 0; $line++) {
1170 next if ($rawlines[$line] =~ /^-/);
1171 $remain--;
1172
1173 $blk .= $rawlines[$line];
1174
1175 # Handle nested #if/#else.
1176 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1177 push(@stack, $level);
1178 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1179 $level = $stack[$#stack - 1];
1180 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1181 $level = pop(@stack);
1182 }
1183
1184 foreach my $c (split(//, $lines[$line])) {
1185 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1186 if ($off > 0) {
1187 $off--;
1188 next;
1189 }
1190
1191 if ($c eq $close && $level > 0) {
1192 $level--;
1193 last if ($level == 0);
1194 } elsif ($c eq $open) {
1195 $level++;
1196 }
1197 }
1198
1199 if (!$outer || $level <= 1) {
1200 push(@res, $rawlines[$line]);
1201 }
1202
1203 last if ($level == 0);
1204 }
1205
1206 return ($level, @res);
1207}
1208sub ctx_block_outer {
1209 my ($linenr, $remain) = @_;
1210
1211 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1212 return @r;
1213}
1214sub ctx_block {
1215 my ($linenr, $remain) = @_;
1216
1217 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1218 return @r;
1219}
1220sub ctx_statement {
1221 my ($linenr, $remain, $off) = @_;
1222
1223 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1224 return @r;
1225}
1226sub ctx_block_level {
1227 my ($linenr, $remain) = @_;
1228
1229 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1230}
1231sub ctx_statement_level {
1232 my ($linenr, $remain, $off) = @_;
1233
1234 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1235}
1236
1237sub ctx_locate_comment {
1238 my ($first_line, $end_line) = @_;
1239
1240 # Catch a comment on the end of the line itself.
1241 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1242 return $current_comment if (defined $current_comment);
1243
1244 # Look through the context and try and figure out if there is a
1245 # comment.
1246 my $in_comment = 0;
1247 $current_comment = '';
1248 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1249 my $line = $rawlines[$linenr - 1];
1250 #warn " $line\n";
1251 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1252 $in_comment = 1;
1253 }
1254 if ($line =~ m@/\*@) {
1255 $in_comment = 1;
1256 }
1257 if (!$in_comment && $current_comment ne '') {
1258 $current_comment = '';
1259 }
1260 $current_comment .= $line . "\n" if ($in_comment);
1261 if ($line =~ m@\*/@) {
1262 $in_comment = 0;
1263 }
1264 }
1265
1266 chomp($current_comment);
1267 return($current_comment);
1268}
1269sub ctx_has_comment {
1270 my ($first_line, $end_line) = @_;
1271 my $cmt = ctx_locate_comment($first_line, $end_line);
1272
1273 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1274 ##print "CMMT: $cmt\n";
1275
1276 return ($cmt ne '');
1277}
1278
1279sub raw_line {
1280 my ($linenr, $cnt) = @_;
1281
1282 my $offset = $linenr - 1;
1283 $cnt++;
1284
1285 my $line;
1286 while ($cnt) {
1287 $line = $rawlines[$offset++];
1288 next if (defined($line) && $line =~ /^-/);
1289 $cnt--;
1290 }
1291
1292 return $line;
1293}
1294
1295sub cat_vet {
1296 my ($vet) = @_;
1297 my ($res, $coded);
1298
1299 $res = '';
1300 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1301 $res .= $1;
1302 if ($2 ne '') {
1303 $coded = sprintf("^%c", unpack('C', $2) + 64);
1304 $res .= $coded;
1305 }
1306 }
1307 $res =~ s/$/\$/;
1308
1309 return $res;
1310}
1311
1312my $av_preprocessor = 0;
1313my $av_pending;
1314my @av_paren_type;
1315my $av_pend_colon;
1316
1317sub annotate_reset {
1318 $av_preprocessor = 0;
1319 $av_pending = '_';
1320 @av_paren_type = ('E');
1321 $av_pend_colon = 'O';
1322}
1323
1324sub annotate_values {
1325 my ($stream, $type) = @_;
1326
1327 my $res;
1328 my $var = '_' x length($stream);
1329 my $cur = $stream;
1330
1331 print "$stream\n" if ($dbg_values > 1);
1332
1333 while (length($cur)) {
1334 @av_paren_type = ('E') if ($#av_paren_type < 0);
1335 print " <" . join('', @av_paren_type) .
1336 "> <$type> <$av_pending>" if ($dbg_values > 1);
1337 if ($cur =~ /^(\s+)/o) {
1338 print "WS($1)\n" if ($dbg_values > 1);
1339 if ($1 =~ /\n/ && $av_preprocessor) {
1340 $type = pop(@av_paren_type);
1341 $av_preprocessor = 0;
1342 }
1343
1344 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1345 print "CAST($1)\n" if ($dbg_values > 1);
1346 push(@av_paren_type, $type);
1347 $type = 'c';
1348
1349 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1350 print "DECLARE($1)\n" if ($dbg_values > 1);
1351 $type = 'T';
1352
1353 } elsif ($cur =~ /^($Modifier)\s*/) {
1354 print "MODIFIER($1)\n" if ($dbg_values > 1);
1355 $type = 'T';
1356
1357 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1358 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1359 $av_preprocessor = 1;
1360 push(@av_paren_type, $type);
1361 if ($2 ne '') {
1362 $av_pending = 'N';
1363 }
1364 $type = 'E';
1365
1366 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1367 print "UNDEF($1)\n" if ($dbg_values > 1);
1368 $av_preprocessor = 1;
1369 push(@av_paren_type, $type);
1370
1371 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1372 print "PRE_START($1)\n" if ($dbg_values > 1);
1373 $av_preprocessor = 1;
1374
1375 push(@av_paren_type, $type);
1376 push(@av_paren_type, $type);
1377 $type = 'E';
1378
1379 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1380 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1381 $av_preprocessor = 1;
1382
1383 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1384
1385 $type = 'E';
1386
1387 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1388 print "PRE_END($1)\n" if ($dbg_values > 1);
1389
1390 $av_preprocessor = 1;
1391
1392 # Assume all arms of the conditional end as this
1393 # one does, and continue as if the #endif was not here.
1394 pop(@av_paren_type);
1395 push(@av_paren_type, $type);
1396 $type = 'E';
1397
1398 } elsif ($cur =~ /^(\\\n)/o) {
1399 print "PRECONT($1)\n" if ($dbg_values > 1);
1400
1401 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1402 print "ATTR($1)\n" if ($dbg_values > 1);
1403 $av_pending = $type;
1404 $type = 'N';
1405
1406 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1407 print "SIZEOF($1)\n" if ($dbg_values > 1);
1408 if (defined $2) {
1409 $av_pending = 'V';
1410 }
1411 $type = 'N';
1412
1413 } elsif ($cur =~ /^(if|while|for)\b/o) {
1414 print "COND($1)\n" if ($dbg_values > 1);
1415 $av_pending = 'E';
1416 $type = 'N';
1417
1418 } elsif ($cur =~/^(case)/o) {
1419 print "CASE($1)\n" if ($dbg_values > 1);
1420 $av_pend_colon = 'C';
1421 $type = 'N';
1422
1423 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1424 print "KEYWORD($1)\n" if ($dbg_values > 1);
1425 $type = 'N';
1426
1427 } elsif ($cur =~ /^(\()/o) {
1428 print "PAREN('$1')\n" if ($dbg_values > 1);
1429 push(@av_paren_type, $av_pending);
1430 $av_pending = '_';
1431 $type = 'N';
1432
1433 } elsif ($cur =~ /^(\))/o) {
1434 my $new_type = pop(@av_paren_type);
1435 if ($new_type ne '_') {
1436 $type = $new_type;
1437 print "PAREN('$1') -> $type\n"
1438 if ($dbg_values > 1);
1439 } else {
1440 print "PAREN('$1')\n" if ($dbg_values > 1);
1441 }
1442
1443 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1444 print "FUNC($1)\n" if ($dbg_values > 1);
1445 $type = 'V';
1446 $av_pending = 'V';
1447
1448 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1449 if (defined $2 && $type eq 'C' || $type eq 'T') {
1450 $av_pend_colon = 'B';
1451 } elsif ($type eq 'E') {
1452 $av_pend_colon = 'L';
1453 }
1454 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1455 $type = 'V';
1456
1457 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1458 print "IDENT($1)\n" if ($dbg_values > 1);
1459 $type = 'V';
1460
1461 } elsif ($cur =~ /^($Assignment)/o) {
1462 print "ASSIGN($1)\n" if ($dbg_values > 1);
1463 $type = 'N';
1464
1465 } elsif ($cur =~/^(;|{|})/) {
1466 print "END($1)\n" if ($dbg_values > 1);
1467 $type = 'E';
1468 $av_pend_colon = 'O';
1469
1470 } elsif ($cur =~/^(,)/) {
1471 print "COMMA($1)\n" if ($dbg_values > 1);
1472 $type = 'C';
1473
1474 } elsif ($cur =~ /^(\?)/o) {
1475 print "QUESTION($1)\n" if ($dbg_values > 1);
1476 $type = 'N';
1477
1478 } elsif ($cur =~ /^(:)/o) {
1479 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1480
1481 substr($var, length($res), 1, $av_pend_colon);
1482 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1483 $type = 'E';
1484 } else {
1485 $type = 'N';
1486 }
1487 $av_pend_colon = 'O';
1488
1489 } elsif ($cur =~ /^(\[)/o) {
1490 print "CLOSE($1)\n" if ($dbg_values > 1);
1491 $type = 'N';
1492
1493 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1494 my $variant;
1495
1496 print "OPV($1)\n" if ($dbg_values > 1);
1497 if ($type eq 'V') {
1498 $variant = 'B';
1499 } else {
1500 $variant = 'U';
1501 }
1502
1503 substr($var, length($res), 1, $variant);
1504 $type = 'N';
1505
1506 } elsif ($cur =~ /^($Operators)/o) {
1507 print "OP($1)\n" if ($dbg_values > 1);
1508 if ($1 ne '++' && $1 ne '--') {
1509 $type = 'N';
1510 }
1511
1512 } elsif ($cur =~ /(^.)/o) {
1513 print "C($1)\n" if ($dbg_values > 1);
1514 }
1515 if (defined $1) {
1516 $cur = substr($cur, length($1));
1517 $res .= $type x length($1);
1518 }
1519 }
1520
1521 return ($res, $var);
1522}
1523
1524sub possible {
1525 my ($possible, $line) = @_;
1526 my $notPermitted = qr{(?:
1527 ^(?:
1528 $Modifier|
1529 $Storage|
1530 $Type|
1531 DEFINE_\S+
1532 )$|
1533 ^(?:
1534 goto|
1535 return|
1536 case|
1537 else|
1538 asm|__asm__|
1539 do|
1540 \#|
1541 \#\#|
1542 )(?:\s|$)|
1543 ^(?:typedef|struct|enum)\b
1544 )}x;
1545 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1546 if ($possible !~ $notPermitted) {
1547 # Check for modifiers.
1548 $possible =~ s/\s*$Storage\s*//g;
1549 $possible =~ s/\s*$Sparse\s*//g;
1550 if ($possible =~ /^\s*$/) {
1551
1552 } elsif ($possible =~ /\s/) {
1553 $possible =~ s/\s*$Type\s*//g;
1554 for my $modifier (split(' ', $possible)) {
1555 if ($modifier !~ $notPermitted) {
1556 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1557 push(@modifierList, $modifier);
1558 }
1559 }
1560
1561 } else {
1562 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1563 push(@typeList, $possible);
1564 }
1565 build_types();
1566 } else {
1567 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1568 }
1569}
1570
1571my $prefix = '';
1572
1573sub show_type {
1574 my ($type) = @_;
1575
1576 return defined $use_type{$type} if (scalar keys %use_type > 0);
1577
1578 return !defined $ignore_type{$type};
1579}
1580
1581sub report {
1582 my ($level, $type, $msg) = @_;
1583
1584 if (!show_type($type) ||
1585 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1586 return 0;
1587 }
1588 my $line;
1589 if ($show_types) {
1590 $line = "$prefix$level:$type: $msg\n";
1591 } else {
1592 $line = "$prefix$level: $msg\n";
1593 }
1594 $line = (split('\n', $line))[0] . "\n" if ($terse);
1595
1596 push(our @report, $line);
1597
1598 return 1;
1599}
1600
1601sub report_dump {
1602 our @report;
1603}
1604
1605sub fixup_current_range {
1606 my ($lineRef, $offset, $length) = @_;
1607
1608 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1609 my $o = $1;
1610 my $l = $2;
1611 my $no = $o + $offset;
1612 my $nl = $l + $length;
1613 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1614 }
1615}
1616
1617sub fix_inserted_deleted_lines {
1618 my ($linesRef, $insertedRef, $deletedRef) = @_;
1619
1620 my $range_last_linenr = 0;
1621 my $delta_offset = 0;
1622
1623 my $old_linenr = 0;
1624 my $new_linenr = 0;
1625
1626 my $next_insert = 0;
1627 my $next_delete = 0;
1628
1629 my @lines = ();
1630
1631 my $inserted = @{$insertedRef}[$next_insert++];
1632 my $deleted = @{$deletedRef}[$next_delete++];
1633
1634 foreach my $old_line (@{$linesRef}) {
1635 my $save_line = 1;
1636 my $line = $old_line; #don't modify the array
1637 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename
1638 $delta_offset = 0;
1639 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1640 $range_last_linenr = $new_linenr;
1641 fixup_current_range(\$line, $delta_offset, 0);
1642 }
1643
1644 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1645 $deleted = @{$deletedRef}[$next_delete++];
1646 $save_line = 0;
1647 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1648 }
1649
1650 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1651 push(@lines, ${$inserted}{'LINE'});
1652 $inserted = @{$insertedRef}[$next_insert++];
1653 $new_linenr++;
1654 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1655 }
1656
1657 if ($save_line) {
1658 push(@lines, $line);
1659 $new_linenr++;
1660 }
1661
1662 $old_linenr++;
1663 }
1664
1665 return @lines;
1666}
1667
1668sub fix_insert_line {
1669 my ($linenr, $line) = @_;
1670
1671 my $inserted = {
1672 LINENR => $linenr,
1673 LINE => $line,
1674 };
1675 push(@fixed_inserted, $inserted);
1676}
1677
1678sub fix_delete_line {
1679 my ($linenr, $line) = @_;
1680
1681 my $deleted = {
1682 LINENR => $linenr,
1683 LINE => $line,
1684 };
1685
1686 push(@fixed_deleted, $deleted);
1687}
1688
1689sub ERROR {
1690 my ($type, $msg) = @_;
1691
1692 if (report("ERROR", $type, $msg)) {
1693 our $clean = 0;
1694 our $cnt_error++;
1695 return 1;
1696 }
1697 return 0;
1698}
1699sub WARN {
1700 my ($type, $msg) = @_;
1701
1702 if (report("WARNING", $type, $msg)) {
1703 our $clean = 0;
1704 our $cnt_warn++;
1705 return 1;
1706 }
1707 return 0;
1708}
1709sub CHK {
1710 my ($type, $msg) = @_;
1711
1712 if ($check && report("CHECK", $type, $msg)) {
1713 our $clean = 0;
1714 our $cnt_chk++;
1715 return 1;
1716 }
1717 return 0;
1718}
1719
1720sub check_absolute_file {
1721 my ($absolute, $herecurr) = @_;
1722 my $file = $absolute;
1723
1724 ##print "absolute<$absolute>\n";
1725
1726 # See if any suffix of this path is a path within the tree.
1727 while ($file =~ s@^[^/]*/@@) {
1728 if (-f "$root/$file") {
1729 ##print "file<$file>\n";
1730 last;
1731 }
1732 }
1733 if (! -f _) {
1734 return 0;
1735 }
1736
1737 # It is, so see if the prefix is acceptable.
1738 my $prefix = $absolute;
1739 substr($prefix, -length($file)) = '';
1740
1741 ##print "prefix<$prefix>\n";
1742 if ($prefix ne ".../") {
1743 WARN("USE_RELATIVE_PATH",
1744 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
1745 }
1746}
1747
1748sub trim {
1749 my ($string) = @_;
1750
1751 $string =~ s/^\s+|\s+$//g;
1752
1753 return $string;
1754}
1755
1756sub ltrim {
1757 my ($string) = @_;
1758
1759 $string =~ s/^\s+//;
1760
1761 return $string;
1762}
1763
1764sub rtrim {
1765 my ($string) = @_;
1766
1767 $string =~ s/\s+$//;
1768
1769 return $string;
1770}
1771
1772sub string_find_replace {
1773 my ($string, $find, $replace) = @_;
1774
1775 $string =~ s/$find/$replace/g;
1776
1777 return $string;
1778}
1779
1780sub tabify {
1781 my ($leading) = @_;
1782
1783 my $source_indent = 8;
1784 my $max_spaces_before_tab = $source_indent - 1;
1785 my $spaces_to_tab = " " x $source_indent;
1786
1787 #convert leading spaces to tabs
1788 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
1789 #Remove spaces before a tab
1790 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
1791
1792 return "$leading";
1793}
1794
1795sub pos_last_openparen {
1796 my ($line) = @_;
1797
1798 my $pos = 0;
1799
1800 my $opens = $line =~ tr/\(/\(/;
1801 my $closes = $line =~ tr/\)/\)/;
1802
1803 my $last_openparen = 0;
1804
1805 if (($opens == 0) || ($closes >= $opens)) {
1806 return -1;
1807 }
1808
1809 my $len = length($line);
1810
1811 for ($pos = 0; $pos < $len; $pos++) {
1812 my $string = substr($line, $pos);
1813 if ($string =~ /^($FuncArg|$balanced_parens)/) {
1814 $pos += length($1) - 1;
1815 } elsif (substr($line, $pos, 1) eq '(') {
1816 $last_openparen = $pos;
1817 } elsif (index($string, '(') == -1) {
1818 last;
1819 }
1820 }
1821
1822 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1823}
1824
1825sub process {
1826 my $filename = shift;
1827
1828 my $linenr=0;
1829 my $prevline="";
1830 my $prevrawline="";
1831 my $stashline="";
1832 my $stashrawline="";
1833
1834 my $length;
1835 my $indent;
1836 my $previndent=0;
1837 my $stashindent=0;
1838
1839 our $clean = 1;
1840 my $signoff = 0;
1841 my $is_patch = 0;
1842
1843 my $in_header_lines = $file ? 0 : 1;
1844 my $in_commit_log = 0; #Scanning lines before patch
1845 my $reported_maintainer_file = 0;
1846 my $non_utf8_charset = 0;
1847
1848 my $last_blank_line = 0;
1849 my $last_coalesced_string_linenr = -1;
1850
1851 our @report = ();
1852 our $cnt_lines = 0;
1853 our $cnt_error = 0;
1854 our $cnt_warn = 0;
1855 our $cnt_chk = 0;
1856
1857 # Trace the real file/line as we go.
1858 my $realfile = '';
1859 my $realline = 0;
1860 my $realcnt = 0;
1861 my $here = '';
1862 my $in_comment = 0;
1863 my $comment_edge = 0;
1864 my $first_line = 0;
1865 my $p1_prefix = '';
1866
1867 my $prev_values = 'E';
1868
1869 # suppression flags
1870 my %suppress_ifbraces;
1871 my %suppress_whiletrailers;
1872 my %suppress_export;
1873 my $suppress_statement = 0;
1874
1875 my %signatures = ();
1876
1877 # Pre-scan the patch sanitizing the lines.
1878 # Pre-scan the patch looking for any __setup documentation.
1879 #
1880 my @setup_docs = ();
1881 my $setup_docs = 0;
1882
1883 my $camelcase_file_seeded = 0;
1884
1885 sanitise_line_reset();
1886 my $line;
1887 foreach my $rawline (@rawlines) {
1888 $linenr++;
1889 $line = $rawline;
1890
1891 push(@fixed, $rawline) if ($fix);
1892
1893 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1894 $setup_docs = 0;
1895 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1896 $setup_docs = 1;
1897 }
1898 #next;
1899 }
1900 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1901 $realline=$1-1;
1902 if (defined $2) {
1903 $realcnt=$3+1;
1904 } else {
1905 $realcnt=1+1;
1906 }
1907 $in_comment = 0;
1908
1909 # Guestimate if this is a continuing comment. Run
1910 # the context looking for a comment "edge". If this
1911 # edge is a close comment then we must be in a comment
1912 # at context start.
1913 my $edge;
1914 my $cnt = $realcnt;
1915 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1916 next if (defined $rawlines[$ln - 1] &&
1917 $rawlines[$ln - 1] =~ /^-/);
1918 $cnt--;
1919 #print "RAW<$rawlines[$ln - 1]>\n";
1920 last if (!defined $rawlines[$ln - 1]);
1921 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1922 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1923 ($edge) = $1;
1924 last;
1925 }
1926 }
1927 if (defined $edge && $edge eq '*/') {
1928 $in_comment = 1;
1929 }
1930
1931 # Guestimate if this is a continuing comment. If this
1932 # is the start of a diff block and this line starts
1933 # ' *' then it is very likely a comment.
1934 if (!defined $edge &&
1935 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1936 {
1937 $in_comment = 1;
1938 }
1939
1940 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1941 sanitise_line_reset($in_comment);
1942
1943 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1944 # Standardise the strings and chars within the input to
1945 # simplify matching -- only bother with positive lines.
1946 $line = sanitise_line($rawline);
1947 }
1948 push(@lines, $line);
1949
1950 if ($realcnt > 1) {
1951 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1952 } else {
1953 $realcnt = 0;
1954 }
1955
1956 #print "==>$rawline\n";
1957 #print "-->$line\n";
1958
1959 if ($setup_docs && $line =~ /^\+/) {
1960 push(@setup_docs, $line);
1961 }
1962 }
1963
1964 $prefix = '';
1965
1966 $realcnt = 0;
1967 $linenr = 0;
1968 $fixlinenr = -1;
1969 foreach my $line (@lines) {
1970 $linenr++;
1971 $fixlinenr++;
1972 my $sline = $line; #copy of $line
1973 $sline =~ s/$;/ /g; #with comments as spaces
1974
1975 my $rawline = $rawlines[$linenr - 1];
1976
1977#extract the line range in the file after the patch is applied
1978 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1979 $is_patch = 1;
1980 $first_line = $linenr + 1;
1981 $realline=$1-1;
1982 if (defined $2) {
1983 $realcnt=$3+1;
1984 } else {
1985 $realcnt=1+1;
1986 }
1987 annotate_reset();
1988 $prev_values = 'E';
1989
1990 %suppress_ifbraces = ();
1991 %suppress_whiletrailers = ();
1992 %suppress_export = ();
1993 $suppress_statement = 0;
1994 next;
1995
1996# track the line number as we move through the hunk, note that
1997# new versions of GNU diff omit the leading space on completely
1998# blank context lines so we need to count that too.
1999 } elsif ($line =~ /^( |\+|$)/) {
2000 $realline++;
2001 $realcnt-- if ($realcnt != 0);
2002
2003 # Measure the line length and indent.
2004 ($length, $indent) = line_stats($rawline);
2005
2006 # Track the previous line.
2007 ($prevline, $stashline) = ($stashline, $line);
2008 ($previndent, $stashindent) = ($stashindent, $indent);
2009 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2010
2011 #warn "line<$line>\n";
2012
2013 } elsif ($realcnt == 1) {
2014 $realcnt--;
2015 }
2016
2017 my $hunk_line = ($realcnt != 0);
2018
2019#make up the handle for any error we report on this line
2020 $prefix = "$filename:$realline: " if ($emacs && $file);
2021 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2022
2023 $here = "#$linenr: " if (!$file);
2024 $here = "#$realline: " if ($file);
2025
2026 my $found_file = 0;
2027 # extract the filename as it passes
2028 if ($line =~ /^diff --git.*?(\S+)$/) {
2029 $realfile = $1;
2030 $realfile =~ s@^([^/]*)/@@ if (!$file);
2031 $in_commit_log = 0;
2032 $found_file = 1;
2033 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2034 $realfile = $1;
2035 $realfile =~ s@^([^/]*)/@@ if (!$file);
2036 $in_commit_log = 0;
2037
2038 $p1_prefix = $1;
2039 if (!$file && $tree && $p1_prefix ne '' &&
2040 -e "$root/$p1_prefix") {
2041 WARN("PATCH_PREFIX",
2042 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2043 }
2044
2045 if ($realfile =~ m@^include/asm/@) {
2046 ERROR("MODIFIED_INCLUDE_ASM",
2047 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2048 }
2049 $found_file = 1;
2050 }
2051
2052 if ($found_file) {
2053 if ($realfile =~ m@^(drivers/net/|net/)@) {
2054 $check = 1;
2055 } else {
2056 $check = $check_orig;
2057 }
2058 next;
2059 }
2060
2061 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2062
2063 my $hereline = "$here\n$rawline\n";
2064 my $herecurr = "$here\n$rawline\n";
2065 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2066
2067 $cnt_lines++ if ($realcnt != 0);
2068
2069# Check for incorrect file permissions
2070 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2071 my $permhere = $here . "FILE: $realfile\n";
2072 if ($realfile !~ m@scripts/@ &&
2073 $realfile !~ /\.(py|pl|awk|sh)$/) {
2074 ERROR("EXECUTE_PERMISSIONS",
2075 "do not set execute permissions for source files\n" . $permhere);
2076 }
2077 }
2078
2079# Check the patch for a signoff:
2080 if ($line =~ /^\s*signed-off-by:/i) {
2081 $signoff++;
2082 $in_commit_log = 0;
2083 }
2084
2085# Check if MAINTAINERS is being updated. If so, there's probably no need to
2086# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2087 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2088 $reported_maintainer_file = 1;
2089 }
2090
2091# Check signature styles
2092 if (!$in_header_lines &&
2093 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2094 my $space_before = $1;
2095 my $sign_off = $2;
2096 my $space_after = $3;
2097 my $email = $4;
2098 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2099
2100 if ($sign_off !~ /$signature_tags/) {
2101 WARN("BAD_SIGN_OFF",
2102 "Non-standard signature: $sign_off\n" . $herecurr);
2103 }
2104 if (defined $space_before && $space_before ne "") {
2105 if (WARN("BAD_SIGN_OFF",
2106 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2107 $fix) {
2108 $fixed[$fixlinenr] =
2109 "$ucfirst_sign_off $email";
2110 }
2111 }
2112 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2113 if (WARN("BAD_SIGN_OFF",
2114 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2115 $fix) {
2116 $fixed[$fixlinenr] =
2117 "$ucfirst_sign_off $email";
2118 }
2119
2120 }
2121 if (!defined $space_after || $space_after ne " ") {
2122 if (WARN("BAD_SIGN_OFF",
2123 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2124 $fix) {
2125 $fixed[$fixlinenr] =
2126 "$ucfirst_sign_off $email";
2127 }
2128 }
2129
2130 my ($email_name, $email_address, $comment) = parse_email($email);
2131 my $suggested_email = format_email(($email_name, $email_address));
2132 if ($suggested_email eq "") {
2133 ERROR("BAD_SIGN_OFF",
2134 "Unrecognized email address: '$email'\n" . $herecurr);
2135 } else {
2136 my $dequoted = $suggested_email;
2137 $dequoted =~ s/^"//;
2138 $dequoted =~ s/" </ </;
2139 # Don't force email to have quotes
2140 # Allow just an angle bracketed address
2141 if ("$dequoted$comment" ne $email &&
2142 "<$email_address>$comment" ne $email &&
2143 "$suggested_email$comment" ne $email) {
2144 WARN("BAD_SIGN_OFF",
2145 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2146 }
2147 }
2148
2149# Check for duplicate signatures
2150 my $sig_nospace = $line;
2151 $sig_nospace =~ s/\s//g;
2152 $sig_nospace = lc($sig_nospace);
2153 if (defined $signatures{$sig_nospace}) {
2154 WARN("BAD_SIGN_OFF",
2155 "Duplicate signature\n" . $herecurr);
2156 } else {
2157 $signatures{$sig_nospace} = 1;
2158 }
2159 }
2160
2161# Check for old stable address
2162 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2163 ERROR("STABLE_ADDRESS",
2164 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2165 }
2166
2167# Check for unwanted Gerrit info
2168 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2169 ERROR("GERRIT_CHANGE_ID",
2170 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2171 }
2172
2173# Check for improperly formed commit descriptions
2174 if ($in_commit_log &&
2175 $line =~ /\bcommit\s+[0-9a-f]{5,}/i &&
2176 !($line =~ /\b[Cc]ommit [0-9a-f]{12,40} \("/ ||
2177 ($line =~ /\b[Cc]ommit [0-9a-f]{12,40}\s*$/ &&
2178 defined $rawlines[$linenr] &&
2179 $rawlines[$linenr] =~ /^\s*\("/))) {
2180 $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i;
2181 my $init_char = $1;
2182 my $orig_commit = lc($2);
2183 my $id = '01234567890ab';
2184 my $desc = 'commit description';
2185 ($id, $desc) = git_commit_info($orig_commit, $id, $desc);
2186 ERROR("GIT_COMMIT_ID",
2187 "Please use 12 or more chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr);
2188 }
2189
2190# Check for added, moved or deleted files
2191 if (!$reported_maintainer_file && !$in_commit_log &&
2192 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2193 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2194 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2195 (defined($1) || defined($2))))) {
2196 $reported_maintainer_file = 1;
2197 WARN("FILE_PATH_CHANGES",
2198 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2199 }
2200
2201# Check for wrappage within a valid hunk of the file
2202 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2203 ERROR("CORRUPTED_PATCH",
2204 "patch seems to be corrupt (line wrapped?)\n" .
2205 $herecurr) if (!$emitted_corrupt++);
2206 }
2207
2208# Check for absolute kernel paths.
2209 if ($tree) {
2210 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2211 my $file = $1;
2212
2213 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2214 check_absolute_file($1, $herecurr)) {
2215 #
2216 } else {
2217 check_absolute_file($file, $herecurr);
2218 }
2219 }
2220 }
2221
2222# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2223 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2224 $rawline !~ m/^$UTF8*$/) {
2225 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2226
2227 my $blank = copy_spacing($rawline);
2228 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2229 my $hereptr = "$hereline$ptr\n";
2230
2231 CHK("INVALID_UTF8",
2232 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2233 }
2234
2235# Check if it's the start of a commit log
2236# (not a header line and we haven't seen the patch filename)
2237 if ($in_header_lines && $realfile =~ /^$/ &&
2238 !($rawline =~ /^\s+\S/ ||
2239 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
2240 $in_header_lines = 0;
2241 $in_commit_log = 1;
2242 }
2243
2244# Check if there is UTF-8 in a commit log when a mail header has explicitly
2245# declined it, i.e defined some charset where it is missing.
2246 if ($in_header_lines &&
2247 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2248 $1 !~ /utf-8/i) {
2249 $non_utf8_charset = 1;
2250 }
2251
2252 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2253 $rawline =~ /$NON_ASCII_UTF8/) {
2254 WARN("UTF8_BEFORE_PATCH",
2255 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2256 }
2257
2258# Check for various typo / spelling mistakes
2259 if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) {
2260 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
2261 my $typo = $1;
2262 my $typo_fix = $spelling_fix{lc($typo)};
2263 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2264 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2265 my $msg_type = \&WARN;
2266 $msg_type = \&CHK if ($file);
2267 if (&{$msg_type}("TYPO_SPELLING",
2268 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2269 $fix) {
2270 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2271 }
2272 }
2273 }
2274
2275# ignore non-hunk lines and lines being removed
2276 next if (!$hunk_line || $line =~ /^-/);
2277
2278#trailing whitespace
2279 if ($line =~ /^\+.*\015/) {
2280 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2281 if (ERROR("DOS_LINE_ENDINGS",
2282 "DOS line endings\n" . $herevet) &&
2283 $fix) {
2284 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
2285 }
2286 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2287 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2288 if (ERROR("TRAILING_WHITESPACE",
2289 "trailing whitespace\n" . $herevet) &&
2290 $fix) {
2291 $fixed[$fixlinenr] =~ s/\s+$//;
2292 }
2293
2294 $rpt_cleaners = 1;
2295 }
2296
2297# Check for FSF mailing addresses.
2298 if ($rawline =~ /\bwrite to the Free/i ||
2299 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2300 $rawline =~ /\b51\s+Franklin\s+St/i) {
2301 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2302 my $msg_type = \&ERROR;
2303 $msg_type = \&CHK if ($file);
2304 &{$msg_type}("FSF_MAILING_ADDRESS",
2305 "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
2306 }
2307
2308# check for Kconfig help text having a real description
2309# Only applies when adding the entry originally, after that we do not have
2310# sufficient context to determine whether it is indeed long enough.
2311 if ($realfile =~ /Kconfig/ &&
2312 $line =~ /^\+\s*config\s+/) {
2313 my $length = 0;
2314 my $cnt = $realcnt;
2315 my $ln = $linenr + 1;
2316 my $f;
2317 my $is_start = 0;
2318 my $is_end = 0;
2319 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
2320 $f = $lines[$ln - 1];
2321 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2322 $is_end = $lines[$ln - 1] =~ /^\+/;
2323
2324 next if ($f =~ /^-/);
2325 last if (!$file && $f =~ /^\@\@/);
2326
2327 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2328 $is_start = 1;
2329 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2330 $length = -1;
2331 }
2332
2333 $f =~ s/^.//;
2334 $f =~ s/#.*//;
2335 $f =~ s/^\s+//;
2336 next if ($f =~ /^$/);
2337 if ($f =~ /^\s*config\s/) {
2338 $is_end = 1;
2339 last;
2340 }
2341 $length++;
2342 }
2343 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2344 WARN("CONFIG_DESCRIPTION",
2345 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2346 }
2347 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2348 }
2349
2350# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2351 if ($realfile =~ /Kconfig/ &&
2352 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2353 WARN("CONFIG_EXPERIMENTAL",
2354 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2355 }
2356
2357 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2358 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2359 my $flag = $1;
2360 my $replacement = {
2361 'EXTRA_AFLAGS' => 'asflags-y',
2362 'EXTRA_CFLAGS' => 'ccflags-y',
2363 'EXTRA_CPPFLAGS' => 'cppflags-y',
2364 'EXTRA_LDFLAGS' => 'ldflags-y',
2365 };
2366
2367 WARN("DEPRECATED_VARIABLE",
2368 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2369 }
2370
2371# check for DT compatible documentation
2372 if (defined $root &&
2373 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2374 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2375
2376 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2377
2378 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2379 my $vp_file = $dt_path . "vendor-prefixes.txt";
2380
2381 foreach my $compat (@compats) {
2382 my $compat2 = $compat;
2383 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2384 my $compat3 = $compat;
2385 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2386 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2387 if ( $? >> 8 ) {
2388 WARN("UNDOCUMENTED_DT_STRING",
2389 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2390 }
2391
2392 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2393 my $vendor = $1;
2394 `grep -Eq "^$vendor\\b" $vp_file`;
2395 if ( $? >> 8 ) {
2396 WARN("UNDOCUMENTED_DT_STRING",
2397 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2398 }
2399 }
2400 }
2401
2402# check we are in a valid source file if not then ignore this hunk
2403 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
2404
2405#line length limit
2406 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2407 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
2408 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
2409 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
2410 $length > $max_line_length)
2411 {
2412 WARN("LONG_LINE",
2413 "line over $max_line_length characters\n" . $herecurr);
2414 }
2415
2416# check for adding lines without a newline.
2417 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2418 WARN("MISSING_EOF_NEWLINE",
2419 "adding a line without newline at end of file\n" . $herecurr);
2420 }
2421
2422# Blackfin: use hi/lo macros
2423 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2424 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2425 my $herevet = "$here\n" . cat_vet($line) . "\n";
2426 ERROR("LO_MACRO",
2427 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
2428 }
2429 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2430 my $herevet = "$here\n" . cat_vet($line) . "\n";
2431 ERROR("HI_MACRO",
2432 "use the HI() macro, not (... >> 16)\n" . $herevet);
2433 }
2434 }
2435
2436# check we are in a valid source file C or perl if not then ignore this hunk
2437 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
2438
2439# at the beginning of a line any tabs must come first and anything
2440# more than 8 must use tabs.
2441 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2442 $rawline =~ /^\+\s* \s*/) {
2443 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2444 $rpt_cleaners = 1;
2445 if (ERROR("CODE_INDENT",
2446 "code indent should use tabs where possible\n" . $herevet) &&
2447 $fix) {
2448 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2449 }
2450 }
2451
2452# check for space before tabs.
2453 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2454 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2455 if (WARN("SPACE_BEFORE_TAB",
2456 "please, no space before tabs\n" . $herevet) &&
2457 $fix) {
2458 while ($fixed[$fixlinenr] =~
2459 s/(^\+.*) {8,8}\t/$1\t\t/) {}
2460 while ($fixed[$fixlinenr] =~
2461 s/(^\+.*) +\t/$1\t/) {}
2462 }
2463 }
2464
2465# check for && or || at the start of a line
2466 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2467 CHK("LOGICAL_CONTINUATIONS",
2468 "Logical continuations should be on the previous line\n" . $hereprev);
2469 }
2470
2471# check multi-line statement indentation matches previous line
2472 if ($^V && $^V ge 5.10.0 &&
2473 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2474 $prevline =~ /^\+(\t*)(.*)$/;
2475 my $oldindent = $1;
2476 my $rest = $2;
2477
2478 my $pos = pos_last_openparen($rest);
2479 if ($pos >= 0) {
2480 $line =~ /^(\+| )([ \t]*)/;
2481 my $newindent = $2;
2482
2483 my $goodtabindent = $oldindent .
2484 "\t" x ($pos / 8) .
2485 " " x ($pos % 8);
2486 my $goodspaceindent = $oldindent . " " x $pos;
2487
2488 if ($newindent ne $goodtabindent &&
2489 $newindent ne $goodspaceindent) {
2490
2491 if (CHK("PARENTHESIS_ALIGNMENT",
2492 "Alignment should match open parenthesis\n" . $hereprev) &&
2493 $fix && $line =~ /^\+/) {
2494 $fixed[$fixlinenr] =~
2495 s/^\+[ \t]*/\+$goodtabindent/;
2496 }
2497 }
2498 }
2499 }
2500
2501 if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;\({\[\<\>])/ &&
2502 (!defined($1) || $1 !~ /sizeof\s*/)) {
2503 if (CHK("SPACING",
2504 "No space is necessary after a cast\n" . $herecurr) &&
2505 $fix) {
2506 $fixed[$fixlinenr] =~
2507 s/(\(\s*$Type\s*\))[ \t]+/$1/;
2508 }
2509 }
2510
2511 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2512 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
2513 $rawline =~ /^\+[ \t]*\*/ &&
2514 $realline > 2) {
2515 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2516 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2517 }
2518
2519 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2520 $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /*
2521 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
2522 $rawline =~ /^\+/ && #line is new
2523 $rawline !~ /^\+[ \t]*\*/) { #no leading *
2524 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2525 "networking block comments start with * on subsequent lines\n" . $hereprev);
2526 }
2527
2528 if ($realfile =~ m@^(drivers/net/|net/)@ &&
2529 $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
2530 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2531 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2532 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
2533 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2534 "networking block comments put the trailing */ on a separate line\n" . $herecurr);
2535 }
2536
2537# check for missing blank lines after struct/union declarations
2538# with exceptions for various attributes and macros
2539 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2540 $line =~ /^\+/ &&
2541 !($line =~ /^\+\s*$/ ||
2542 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2543 $line =~ /^\+\s*MODULE_/i ||
2544 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2545 $line =~ /^\+[a-z_]*init/ ||
2546 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2547 $line =~ /^\+\s*DECLARE/ ||
2548 $line =~ /^\+\s*__setup/)) {
2549 if (CHK("LINE_SPACING",
2550 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2551 $fix) {
2552 fix_insert_line($fixlinenr, "\+");
2553 }
2554 }
2555
2556# check for multiple consecutive blank lines
2557 if ($prevline =~ /^[\+ ]\s*$/ &&
2558 $line =~ /^\+\s*$/ &&
2559 $last_blank_line != ($linenr - 1)) {
2560 if (CHK("LINE_SPACING",
2561 "Please don't use multiple blank lines\n" . $hereprev) &&
2562 $fix) {
2563 fix_delete_line($fixlinenr, $rawline);
2564 }
2565
2566 $last_blank_line = $linenr;
2567 }
2568
2569# check for missing blank lines after declarations
2570 if ($sline =~ /^\+\s+\S/ && #Not at char 1
2571 # actual declarations
2572 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
2573 # function pointer declarations
2574 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
2575 # foo bar; where foo is some local typedef or #define
2576 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2577 # known declaration macros
2578 $prevline =~ /^\+\s+$declaration_macros/) &&
2579 # for "else if" which can look like "$Ident $Ident"
2580 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
2581 # other possible extensions of declaration lines
2582 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
2583 # not starting a section or a macro "\" extended line
2584 $prevline =~ /(?:\{\s*|\\)$/) &&
2585 # looks like a declaration
2586 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
2587 # function pointer declarations
2588 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
2589 # foo bar; where foo is some local typedef or #define
2590 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
2591 # known declaration macros
2592 $sline =~ /^\+\s+$declaration_macros/ ||
2593 # start of struct or union or enum
2594 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
2595 # start or end of block or continuation of declaration
2596 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
2597 # bitfield continuation
2598 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
2599 # other possible extensions of declaration lines
2600 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
2601 # indentation of previous and current line are the same
2602 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2603 if (WARN("LINE_SPACING",
2604 "Missing a blank line after declarations\n" . $hereprev) &&
2605 $fix) {
2606 fix_insert_line($fixlinenr, "\+");
2607 }
2608 }
2609
2610# check for spaces at the beginning of a line.
2611# Exceptions:
2612# 1) within comments
2613# 2) indented preprocessor commands
2614# 3) hanging labels
2615 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
2616 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2617 if (WARN("LEADING_SPACE",
2618 "please, no spaces at the start of a line\n" . $herevet) &&
2619 $fix) {
2620 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2621 }
2622 }
2623
2624# check we are in a valid C source file if not then ignore this hunk
2625 next if ($realfile !~ /\.(h|c)$/);
2626
2627# check indentation of any line with a bare else
2628# (but not if it is a multiple line "if (foo) return bar; else return baz;")
2629# if the previous line is a break or return and is indented 1 tab more...
2630 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2631 my $tabs = length($1) + 1;
2632 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2633 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2634 defined $lines[$linenr] &&
2635 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
2636 WARN("UNNECESSARY_ELSE",
2637 "else is not generally useful after a break or return\n" . $hereprev);
2638 }
2639 }
2640
2641# check indentation of a line with a break;
2642# if the previous line is a goto or return and is indented the same # of tabs
2643 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2644 my $tabs = $1;
2645 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2646 WARN("UNNECESSARY_BREAK",
2647 "break is not useful after a goto or return\n" . $hereprev);
2648 }
2649 }
2650
2651# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
2652 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
2653 WARN("CONFIG_EXPERIMENTAL",
2654 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2655 }
2656
2657# check for RCS/CVS revision markers
2658 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
2659 WARN("CVS_KEYWORD",
2660 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
2661 }
2662
2663# Blackfin: don't use __builtin_bfin_[cs]sync
2664 if ($line =~ /__builtin_bfin_csync/) {
2665 my $herevet = "$here\n" . cat_vet($line) . "\n";
2666 ERROR("CSYNC",
2667 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
2668 }
2669 if ($line =~ /__builtin_bfin_ssync/) {
2670 my $herevet = "$here\n" . cat_vet($line) . "\n";
2671 ERROR("SSYNC",
2672 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
2673 }
2674
2675# check for old HOTPLUG __dev<foo> section markings
2676 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
2677 WARN("HOTPLUG_SECTION",
2678 "Using $1 is unnecessary\n" . $herecurr);
2679 }
2680
2681# Check for potential 'bare' types
2682 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
2683 $realline_next);
2684#print "LINE<$line>\n";
2685 if ($linenr >= $suppress_statement &&
2686 $realcnt && $sline =~ /.\s*\S/) {
2687 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2688 ctx_statement_block($linenr, $realcnt, 0);
2689 $stat =~ s/\n./\n /g;
2690 $cond =~ s/\n./\n /g;
2691
2692#print "linenr<$linenr> <$stat>\n";
2693 # If this statement has no statement boundaries within
2694 # it there is no point in retrying a statement scan
2695 # until we hit end of it.
2696 my $frag = $stat; $frag =~ s/;+\s*$//;
2697 if ($frag !~ /(?:{|;)/) {
2698#print "skip<$line_nr_next>\n";
2699 $suppress_statement = $line_nr_next;
2700 }
2701
2702 # Find the real next line.
2703 $realline_next = $line_nr_next;
2704 if (defined $realline_next &&
2705 (!defined $lines[$realline_next - 1] ||
2706 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
2707 $realline_next++;
2708 }
2709
2710 my $s = $stat;
2711 $s =~ s/{.*$//s;
2712
2713 # Ignore goto labels.
2714 if ($s =~ /$Ident:\*$/s) {
2715
2716 # Ignore functions being called
2717 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
2718
2719 } elsif ($s =~ /^.\s*else\b/s) {
2720
2721 # declarations always start with types
2722 } 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) {
2723 my $type = $1;
2724 $type =~ s/\s+/ /g;
2725 possible($type, "A:" . $s);
2726
2727 # definitions in global scope can only start with types
2728 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
2729 possible($1, "B:" . $s);
2730 }
2731
2732 # any (foo ... *) is a pointer cast, and foo is a type
2733 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
2734 possible($1, "C:" . $s);
2735 }
2736
2737 # Check for any sort of function declaration.
2738 # int foo(something bar, other baz);
2739 # void (*store_gdt)(x86_descr_ptr *);
2740 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
2741 my ($name_len) = length($1);
2742
2743 my $ctx = $s;
2744 substr($ctx, 0, $name_len + 1, '');
2745 $ctx =~ s/\)[^\)]*$//;
2746
2747 for my $arg (split(/\s*,\s*/, $ctx)) {
2748 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
2749
2750 possible($1, "D:" . $s);
2751 }
2752 }
2753 }
2754
2755 }
2756
2757#
2758# Checks which may be anchored in the context.
2759#
2760
2761# Check for switch () and associated case and default
2762# statements should be at the same indent.
2763 if ($line=~/\bswitch\s*\(.*\)/) {
2764 my $err = '';
2765 my $sep = '';
2766 my @ctx = ctx_block_outer($linenr, $realcnt);
2767 shift(@ctx);
2768 for my $ctx (@ctx) {
2769 my ($clen, $cindent) = line_stats($ctx);
2770 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
2771 $indent != $cindent) {
2772 $err .= "$sep$ctx\n";
2773 $sep = '';
2774 } else {
2775 $sep = "[...]\n";
2776 }
2777 }
2778 if ($err ne '') {
2779 ERROR("SWITCH_CASE_INDENT_LEVEL",
2780 "switch and case should be at the same indent\n$hereline$err");
2781 }
2782 }
2783
2784# if/while/etc brace do not go on next line, unless defining a do while loop,
2785# or if that brace on the next line is for something else
2786 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
2787 my $pre_ctx = "$1$2";
2788
2789 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
2790
2791 if ($line =~ /^\+\t{6,}/) {
2792 WARN("DEEP_INDENTATION",
2793 "Too many leading tabs - consider code refactoring\n" . $herecurr);
2794 }
2795
2796 my $ctx_cnt = $realcnt - $#ctx - 1;
2797 my $ctx = join("\n", @ctx);
2798
2799 my $ctx_ln = $linenr;
2800 my $ctx_skip = $realcnt;
2801
2802 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
2803 defined $lines[$ctx_ln - 1] &&
2804 $lines[$ctx_ln - 1] =~ /^-/)) {
2805 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
2806 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
2807 $ctx_ln++;
2808 }
2809
2810 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
2811 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
2812
2813 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
2814 ERROR("OPEN_BRACE",
2815 "that open brace { should be on the previous line\n" .
2816 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2817 }
2818 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
2819 $ctx =~ /\)\s*\;\s*$/ &&
2820 defined $lines[$ctx_ln - 1])
2821 {
2822 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
2823 if ($nindent > $indent) {
2824 WARN("TRAILING_SEMICOLON",
2825 "trailing semicolon indicates no statements, indent implies otherwise\n" .
2826 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
2827 }
2828 }
2829 }
2830
2831# Check relative indent for conditionals and blocks.
2832 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
2833 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
2834 ctx_statement_block($linenr, $realcnt, 0)
2835 if (!defined $stat);
2836 my ($s, $c) = ($stat, $cond);
2837
2838 substr($s, 0, length($c), '');
2839
2840 # Make sure we remove the line prefixes as we have
2841 # none on the first line, and are going to readd them
2842 # where necessary.
2843 $s =~ s/\n./\n/gs;
2844
2845 # Find out how long the conditional actually is.
2846 my @newlines = ($c =~ /\n/gs);
2847 my $cond_lines = 1 + $#newlines;
2848
2849 # We want to check the first line inside the block
2850 # starting at the end of the conditional, so remove:
2851 # 1) any blank line termination
2852 # 2) any opening brace { on end of the line
2853 # 3) any do (...) {
2854 my $continuation = 0;
2855 my $check = 0;
2856 $s =~ s/^.*\bdo\b//;
2857 $s =~ s/^\s*{//;
2858 if ($s =~ s/^\s*\\//) {
2859 $continuation = 1;
2860 }
2861 if ($s =~ s/^\s*?\n//) {
2862 $check = 1;
2863 $cond_lines++;
2864 }
2865
2866 # Also ignore a loop construct at the end of a
2867 # preprocessor statement.
2868 if (($prevline =~ /^.\s*#\s*define\s/ ||
2869 $prevline =~ /\\\s*$/) && $continuation == 0) {
2870 $check = 0;
2871 }
2872
2873 my $cond_ptr = -1;
2874 $continuation = 0;
2875 while ($cond_ptr != $cond_lines) {
2876 $cond_ptr = $cond_lines;
2877
2878 # If we see an #else/#elif then the code
2879 # is not linear.
2880 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
2881 $check = 0;
2882 }
2883
2884 # Ignore:
2885 # 1) blank lines, they should be at 0,
2886 # 2) preprocessor lines, and
2887 # 3) labels.
2888 if ($continuation ||
2889 $s =~ /^\s*?\n/ ||
2890 $s =~ /^\s*#\s*?/ ||
2891 $s =~ /^\s*$Ident\s*:/) {
2892 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
2893 if ($s =~ s/^.*?\n//) {
2894 $cond_lines++;
2895 }
2896 }
2897 }
2898
2899 my (undef, $sindent) = line_stats("+" . $s);
2900 my $stat_real = raw_line($linenr, $cond_lines);
2901
2902 # Check if either of these lines are modified, else
2903 # this is not this patch's fault.
2904 if (!defined($stat_real) ||
2905 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
2906 $check = 0;
2907 }
2908 if (defined($stat_real) && $cond_lines > 1) {
2909 $stat_real = "[...]\n$stat_real";
2910 }
2911
2912 #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";
2913
2914 if ($check && (($sindent % 8) != 0 ||
2915 ($sindent <= $indent && $s ne ''))) {
2916 WARN("SUSPECT_CODE_INDENT",
2917 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
2918 }
2919 }
2920
2921 # Track the 'values' across context and added lines.
2922 my $opline = $line; $opline =~ s/^./ /;
2923 my ($curr_values, $curr_vars) =
2924 annotate_values($opline . "\n", $prev_values);
2925 $curr_values = $prev_values . $curr_values;
2926 if ($dbg_values) {
2927 my $outline = $opline; $outline =~ s/\t/ /g;
2928 print "$linenr > .$outline\n";
2929 print "$linenr > $curr_values\n";
2930 print "$linenr > $curr_vars\n";
2931 }
2932 $prev_values = substr($curr_values, -1);
2933
2934#ignore lines not being added
2935 next if ($line =~ /^[^\+]/);
2936
2937# TEST: allow direct testing of the type matcher.
2938 if ($dbg_type) {
2939 if ($line =~ /^.\s*$Declare\s*$/) {
2940 ERROR("TEST_TYPE",
2941 "TEST: is type\n" . $herecurr);
2942 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
2943 ERROR("TEST_NOT_TYPE",
2944 "TEST: is not type ($1 is)\n". $herecurr);
2945 }
2946 next;
2947 }
2948# TEST: allow direct testing of the attribute matcher.
2949 if ($dbg_attr) {
2950 if ($line =~ /^.\s*$Modifier\s*$/) {
2951 ERROR("TEST_ATTR",
2952 "TEST: is attr\n" . $herecurr);
2953 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
2954 ERROR("TEST_NOT_ATTR",
2955 "TEST: is not attr ($1 is)\n". $herecurr);
2956 }
2957 next;
2958 }
2959
2960# check for initialisation to aggregates open brace on the next line
2961 if ($line =~ /^.\s*{/ &&
2962 $prevline =~ /(?:^|[^=])=\s*$/) {
2963 if (ERROR("OPEN_BRACE",
2964 "that open brace { should be on the previous line\n" . $hereprev) &&
2965 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
2966 fix_delete_line($fixlinenr - 1, $prevrawline);
2967 fix_delete_line($fixlinenr, $rawline);
2968 my $fixedline = $prevrawline;
2969 $fixedline =~ s/\s*=\s*$/ = {/;
2970 fix_insert_line($fixlinenr, $fixedline);
2971 $fixedline = $line;
2972 $fixedline =~ s/^(.\s*){\s*/$1/;
2973 fix_insert_line($fixlinenr, $fixedline);
2974 }
2975 }
2976
2977#
2978# Checks which are anchored on the added line.
2979#
2980
2981# check for malformed paths in #include statements (uses RAW line)
2982 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
2983 my $path = $1;
2984 if ($path =~ m{//}) {
2985 ERROR("MALFORMED_INCLUDE",
2986 "malformed #include filename\n" . $herecurr);
2987 }
2988 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2989 ERROR("UAPI_INCLUDE",
2990 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2991 }
2992 }
2993
2994# no C99 // comments
2995 if ($line =~ m{//}) {
Stefan Reinauer42412b62015-02-11 01:51:32 +01002996 if (CHK("C99_COMMENTS",
Stefan Reinauer44d0fd92015-02-11 01:49:00 +01002997 "do not use C99 // comments\n" . $herecurr) &&
2998 $fix) {
2999 my $line = $fixed[$fixlinenr];
3000 if ($line =~ /\/\/(.*)$/) {
3001 my $comment = trim($1);
3002 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
3003 }
3004 }
3005 }
3006 # Remove C99 comments.
3007 $line =~ s@//.*@@;
3008 $opline =~ s@//.*@@;
3009
3010# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3011# the whole statement.
3012#print "APW <$lines[$realline_next - 1]>\n";
3013 if (defined $realline_next &&
3014 exists $lines[$realline_next - 1] &&
3015 !defined $suppress_export{$realline_next} &&
3016 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3017 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3018 # Handle definitions which produce identifiers with
3019 # a prefix:
3020 # XXX(foo);
3021 # EXPORT_SYMBOL(something_foo);
3022 my $name = $1;
3023 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
3024 $name =~ /^${Ident}_$2/) {
3025#print "FOO C name<$name>\n";
3026 $suppress_export{$realline_next} = 1;
3027
3028 } elsif ($stat !~ /(?:
3029 \n.}\s*$|
3030 ^.DEFINE_$Ident\(\Q$name\E\)|
3031 ^.DECLARE_$Ident\(\Q$name\E\)|
3032 ^.LIST_HEAD\(\Q$name\E\)|
3033 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3034 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
3035 )/x) {
3036#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3037 $suppress_export{$realline_next} = 2;
3038 } else {
3039 $suppress_export{$realline_next} = 1;
3040 }
3041 }
3042 if (!defined $suppress_export{$linenr} &&
3043 $prevline =~ /^.\s*$/ &&
3044 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3045 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3046#print "FOO B <$lines[$linenr - 1]>\n";
3047 $suppress_export{$linenr} = 2;
3048 }
3049 if (defined $suppress_export{$linenr} &&
3050 $suppress_export{$linenr} == 2) {
3051 WARN("EXPORT_SYMBOL",
3052 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
3053 }
3054
3055# check for global initialisers.
3056 if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) {
3057 if (ERROR("GLOBAL_INITIALISERS",
3058 "do not initialise globals to 0 or NULL\n" .
3059 $herecurr) &&
3060 $fix) {
3061 $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/;
3062 }
3063 }
3064# check for static initialisers.
3065 if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
3066 if (ERROR("INITIALISED_STATIC",
3067 "do not initialise statics to 0 or NULL\n" .
3068 $herecurr) &&
3069 $fix) {
3070 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/;
3071 }
3072 }
3073
3074# check for misordered declarations of char/short/int/long with signed/unsigned
3075 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3076 my $tmp = trim($1);
3077 WARN("MISORDERED_TYPE",
3078 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3079 }
3080
3081# check for static const char * arrays.
3082 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3083 WARN("STATIC_CONST_CHAR_ARRAY",
3084 "static const char * array should probably be static const char * const\n" .
3085 $herecurr);
3086 }
3087
3088# check for static char foo[] = "bar" declarations.
3089 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3090 WARN("STATIC_CONST_CHAR_ARRAY",
3091 "static char array declaration should probably be static const char\n" .
3092 $herecurr);
3093 }
3094
3095# check for non-global char *foo[] = {"bar", ...} declarations.
3096 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3097 WARN("STATIC_CONST_CHAR_ARRAY",
3098 "char * array declaration might be better as static const\n" .
3099 $herecurr);
3100 }
3101
3102# check for function declarations without arguments like "int foo()"
3103 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3104 if (ERROR("FUNCTION_WITHOUT_ARGS",
3105 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3106 $fix) {
3107 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3108 }
3109 }
3110
3111# check for uses of DEFINE_PCI_DEVICE_TABLE
3112 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3113 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3114 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3115 $fix) {
3116 $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
3117 }
3118 }
3119
3120# check for new typedefs, only function parameters and sparse annotations
3121# make sense.
3122 if ($line =~ /\btypedef\s/ &&
3123 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3124 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
3125 $line !~ /\b$typeTypedefs\b/ &&
3126 $line !~ /\b__bitwise(?:__|)\b/) {
3127 WARN("NEW_TYPEDEFS",
3128 "do not add new typedefs\n" . $herecurr);
3129 }
3130
3131# * goes on variable not on type
3132 # (char*[ const])
3133 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3134 #print "AA<$1>\n";
3135 my ($ident, $from, $to) = ($1, $2, $2);
3136
3137 # Should start with a space.
3138 $to =~ s/^(\S)/ $1/;
3139 # Should not end with a space.
3140 $to =~ s/\s+$//;
3141 # '*'s should not have spaces between.
3142 while ($to =~ s/\*\s+\*/\*\*/) {
3143 }
3144
3145## print "1: from<$from> to<$to> ident<$ident>\n";
3146 if ($from ne $to) {
3147 if (ERROR("POINTER_LOCATION",
3148 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3149 $fix) {
3150 my $sub_from = $ident;
3151 my $sub_to = $ident;
3152 $sub_to =~ s/\Q$from\E/$to/;
3153 $fixed[$fixlinenr] =~
3154 s@\Q$sub_from\E@$sub_to@;
3155 }
3156 }
3157 }
3158 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3159 #print "BB<$1>\n";
3160 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3161
3162 # Should start with a space.
3163 $to =~ s/^(\S)/ $1/;
3164 # Should not end with a space.
3165 $to =~ s/\s+$//;
3166 # '*'s should not have spaces between.
3167 while ($to =~ s/\*\s+\*/\*\*/) {
3168 }
3169 # Modifiers should have spaces.
3170 $to =~ s/(\b$Modifier$)/$1 /;
3171
3172## print "2: from<$from> to<$to> ident<$ident>\n";
3173 if ($from ne $to && $ident !~ /^$Modifier$/) {
3174 if (ERROR("POINTER_LOCATION",
3175 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3176 $fix) {
3177
3178 my $sub_from = $match;
3179 my $sub_to = $match;
3180 $sub_to =~ s/\Q$from\E/$to/;
3181 $fixed[$fixlinenr] =~
3182 s@\Q$sub_from\E@$sub_to@;
3183 }
3184 }
3185 }
3186
3187# # no BUG() or BUG_ON()
3188# if ($line =~ /\b(BUG|BUG_ON)\b/) {
3189# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
3190# print "$herecurr";
3191# $clean = 0;
3192# }
3193
3194 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3195 WARN("LINUX_VERSION_CODE",
3196 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
3197 }
3198
3199# check for uses of printk_ratelimit
3200 if ($line =~ /\bprintk_ratelimit\s*\(/) {
3201 WARN("PRINTK_RATELIMITED",
3202"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
3203 }
3204
3205# printk should use KERN_* levels. Note that follow on printk's on the
3206# same line do not need a level, so we use the current block context
3207# to try and find and validate the current printk. In summary the current
3208# printk includes all preceding printk's which have no newline on the end.
3209# we assume the first bad printk is the one to report.
3210 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
3211 my $ok = 0;
3212 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3213 #print "CHECK<$lines[$ln - 1]\n";
3214 # we have a preceding printk if it ends
3215 # with "\n" ignore it, else it is to blame
3216 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3217 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3218 $ok = 1;
3219 }
3220 last;
3221 }
3222 }
3223 if ($ok == 0) {
3224 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3225 "printk() should include KERN_ facility level\n" . $herecurr);
3226 }
3227 }
3228
3229 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3230 my $orig = $1;
3231 my $level = lc($orig);
3232 $level = "warn" if ($level eq "warning");
3233 my $level2 = $level;
3234 $level2 = "dbg" if ($level eq "debug");
3235 WARN("PREFER_PR_LEVEL",
3236 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
3237 }
3238
3239 if ($line =~ /\bpr_warning\s*\(/) {
3240 if (WARN("PREFER_PR_LEVEL",
3241 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3242 $fix) {
3243 $fixed[$fixlinenr] =~
3244 s/\bpr_warning\b/pr_warn/;
3245 }
3246 }
3247
3248 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3249 my $orig = $1;
3250 my $level = lc($orig);
3251 $level = "warn" if ($level eq "warning");
3252 $level = "dbg" if ($level eq "debug");
3253 WARN("PREFER_DEV_LEVEL",
3254 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3255 }
3256
3257# function brace can't be on same line, except for #defines of do while,
3258# or if closed on same line
3259 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3260 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
3261 if (ERROR("OPEN_BRACE",
3262 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3263 $fix) {
3264 fix_delete_line($fixlinenr, $rawline);
3265 my $fixed_line = $rawline;
3266 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3267 my $line1 = $1;
3268 my $line2 = $2;
3269 fix_insert_line($fixlinenr, ltrim($line1));
3270 fix_insert_line($fixlinenr, "\+{");
3271 if ($line2 !~ /^\s*$/) {
3272 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3273 }
3274 }
3275 }
3276
3277# open braces for enum, union and struct go on the same line.
3278 if ($line =~ /^.\s*{/ &&
3279 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
3280 if (ERROR("OPEN_BRACE",
3281 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3282 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3283 fix_delete_line($fixlinenr - 1, $prevrawline);
3284 fix_delete_line($fixlinenr, $rawline);
3285 my $fixedline = rtrim($prevrawline) . " {";
3286 fix_insert_line($fixlinenr, $fixedline);
3287 $fixedline = $rawline;
3288 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3289 if ($fixedline !~ /^\+\s*$/) {
3290 fix_insert_line($fixlinenr, $fixedline);
3291 }
3292 }
3293 }
3294
3295# missing space after union, struct or enum definition
3296 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3297 if (WARN("SPACING",
3298 "missing space after $1 definition\n" . $herecurr) &&
3299 $fix) {
3300 $fixed[$fixlinenr] =~
3301 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3302 }
3303 }
3304
3305# Function pointer declarations
3306# check spacing between type, funcptr, and args
3307# canonical declaration is "type (*funcptr)(args...)"
3308 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
3309 my $declare = $1;
3310 my $pre_pointer_space = $2;
3311 my $post_pointer_space = $3;
3312 my $funcname = $4;
3313 my $post_funcname_space = $5;
3314 my $pre_args_space = $6;
3315
3316# the $Declare variable will capture all spaces after the type
3317# so check it for a missing trailing missing space but pointer return types
3318# don't need a space so don't warn for those.
3319 my $post_declare_space = "";
3320 if ($declare =~ /(\s+)$/) {
3321 $post_declare_space = $1;
3322 $declare = rtrim($declare);
3323 }
3324 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
3325 WARN("SPACING",
3326 "missing space after return type\n" . $herecurr);
3327 $post_declare_space = " ";
3328 }
3329
3330# unnecessary space "type (*funcptr)(args...)"
3331# This test is not currently implemented because these declarations are
3332# equivalent to
3333# int foo(int bar, ...)
3334# and this is form shouldn't/doesn't generate a checkpatch warning.
3335#
3336# elsif ($declare =~ /\s{2,}$/) {
3337# WARN("SPACING",
3338# "Multiple spaces after return type\n" . $herecurr);
3339# }
3340
3341# unnecessary space "type ( *funcptr)(args...)"
3342 if (defined $pre_pointer_space &&
3343 $pre_pointer_space =~ /^\s/) {
3344 WARN("SPACING",
3345 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3346 }
3347
3348# unnecessary space "type (* funcptr)(args...)"
3349 if (defined $post_pointer_space &&
3350 $post_pointer_space =~ /^\s/) {
3351 WARN("SPACING",
3352 "Unnecessary space before function pointer name\n" . $herecurr);
3353 }
3354
3355# unnecessary space "type (*funcptr )(args...)"
3356 if (defined $post_funcname_space &&
3357 $post_funcname_space =~ /^\s/) {
3358 WARN("SPACING",
3359 "Unnecessary space after function pointer name\n" . $herecurr);
3360 }
3361
3362# unnecessary space "type (*funcptr) (args...)"
3363 if (defined $pre_args_space &&
3364 $pre_args_space =~ /^\s/) {
3365 WARN("SPACING",
3366 "Unnecessary space before function pointer arguments\n" . $herecurr);
3367 }
3368
3369 if (show_type("SPACING") && $fix) {
3370 $fixed[$fixlinenr] =~
3371 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
3372 }
3373 }
3374
3375# check for spacing round square brackets; allowed:
3376# 1. with a type on the left -- int [] a;
3377# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3378# 3. inside a curly brace -- = { [0...10] = 5 }
3379 while ($line =~ /(.*?\s)\[/g) {
3380 my ($where, $prefix) = ($-[1], $1);
3381 if ($prefix !~ /$Type\s+$/ &&
3382 ($where != 0 || $prefix !~ /^.\s+$/) &&
3383 $prefix !~ /[{,]\s+$/) {
3384 if (ERROR("BRACKET_SPACE",
3385 "space prohibited before open square bracket '['\n" . $herecurr) &&
3386 $fix) {
3387 $fixed[$fixlinenr] =~
3388 s/^(\+.*?)\s+\[/$1\[/;
3389 }
3390 }
3391 }
3392
3393# check for spaces between functions and their parentheses.
3394 while ($line =~ /($Ident)\s+\(/g) {
3395 my $name = $1;
3396 my $ctx_before = substr($line, 0, $-[1]);
3397 my $ctx = "$ctx_before$name";
3398
3399 # Ignore those directives where spaces _are_ permitted.
3400 if ($name =~ /^(?:
3401 if|for|while|switch|return|case|
3402 volatile|__volatile__|
3403 __attribute__|format|__extension__|
3404 asm|__asm__)$/x)
3405 {
3406 # cpp #define statements have non-optional spaces, ie
3407 # if there is a space between the name and the open
3408 # parenthesis it is simply not a parameter group.
3409 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3410
3411 # cpp #elif statement condition may start with a (
3412 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3413
3414 # If this whole things ends with a type its most
3415 # likely a typedef for a function.
3416 } elsif ($ctx =~ /$Type$/) {
3417
3418 } else {
3419 if (WARN("SPACING",
3420 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3421 $fix) {
3422 $fixed[$fixlinenr] =~
3423 s/\b$name\s+\(/$name\(/;
3424 }
3425 }
3426 }
3427
3428# Check operator spacing.
3429 if (!($line=~/\#\s*include/)) {
3430 my $fixed_line = "";
3431 my $line_fixed = 0;
3432
3433 my $ops = qr{
3434 <<=|>>=|<=|>=|==|!=|
3435 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3436 =>|->|<<|>>|<|>|=|!|~|
3437 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
3438 \?:|\?|:
3439 }x;
3440 my @elements = split(/($ops|;)/, $opline);
3441
3442## print("element count: <" . $#elements . ">\n");
3443## foreach my $el (@elements) {
3444## print("el: <$el>\n");
3445## }
3446
3447 my @fix_elements = ();
3448 my $off = 0;
3449
3450 foreach my $el (@elements) {
3451 push(@fix_elements, substr($rawline, $off, length($el)));
3452 $off += length($el);
3453 }
3454
3455 $off = 0;
3456
3457 my $blank = copy_spacing($opline);
3458 my $last_after = -1;
3459
3460 for (my $n = 0; $n < $#elements; $n += 2) {
3461
3462 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3463
3464## print("n: <$n> good: <$good>\n");
3465
3466 $off += length($elements[$n]);
3467
3468 # Pick up the preceding and succeeding characters.
3469 my $ca = substr($opline, 0, $off);
3470 my $cc = '';
3471 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3472 $cc = substr($opline, $off + length($elements[$n + 1]));
3473 }
3474 my $cb = "$ca$;$cc";
3475
3476 my $a = '';
3477 $a = 'V' if ($elements[$n] ne '');
3478 $a = 'W' if ($elements[$n] =~ /\s$/);
3479 $a = 'C' if ($elements[$n] =~ /$;$/);
3480 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3481 $a = 'O' if ($elements[$n] eq '');
3482 $a = 'E' if ($ca =~ /^\s*$/);
3483
3484 my $op = $elements[$n + 1];
3485
3486 my $c = '';
3487 if (defined $elements[$n + 2]) {
3488 $c = 'V' if ($elements[$n + 2] ne '');
3489 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
3490 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
3491 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
3492 $c = 'O' if ($elements[$n + 2] eq '');
3493 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
3494 } else {
3495 $c = 'E';
3496 }
3497
3498 my $ctx = "${a}x${c}";
3499
3500 my $at = "(ctx:$ctx)";
3501
3502 my $ptr = substr($blank, 0, $off) . "^";
3503 my $hereptr = "$hereline$ptr\n";
3504
3505 # Pull out the value of this operator.
3506 my $op_type = substr($curr_values, $off + 1, 1);
3507
3508 # Get the full operator variant.
3509 my $opv = $op . substr($curr_vars, $off, 1);
3510
3511 # Ignore operators passed as parameters.
3512 if ($op_type ne 'V' &&
3513 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
3514
3515# # Ignore comments
3516# } elsif ($op =~ /^$;+$/) {
3517
3518 # ; should have either the end of line or a space or \ after it
3519 } elsif ($op eq ';') {
3520 if ($ctx !~ /.x[WEBC]/ &&
3521 $cc !~ /^\\/ && $cc !~ /^;/) {
3522 if (ERROR("SPACING",
3523 "space required after that '$op' $at\n" . $hereptr)) {
3524 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3525 $line_fixed = 1;
3526 }
3527 }
3528
3529 # // is a comment
3530 } elsif ($op eq '//') {
3531
3532 # : when part of a bitfield
3533 } elsif ($opv eq ':B') {
3534 # skip the bitfield test for now
3535
3536 # No spaces for:
3537 # ->
3538 } elsif ($op eq '->') {
3539 if ($ctx =~ /Wx.|.xW/) {
3540 if (ERROR("SPACING",
3541 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3542 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3543 if (defined $fix_elements[$n + 2]) {
3544 $fix_elements[$n + 2] =~ s/^\s+//;
3545 }
3546 $line_fixed = 1;
3547 }
3548 }
3549
3550 # , must not have a space before and must have a space on the right.
3551 } elsif ($op eq ',') {
3552 my $rtrim_before = 0;
3553 my $space_after = 0;
3554 if ($ctx =~ /Wx./) {
3555 if (ERROR("SPACING",
3556 "space prohibited before that '$op' $at\n" . $hereptr)) {
3557 $line_fixed = 1;
3558 $rtrim_before = 1;
3559 }
3560 }
3561 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
3562 if (ERROR("SPACING",
3563 "space required after that '$op' $at\n" . $hereptr)) {
3564 $line_fixed = 1;
3565 $last_after = $n;
3566 $space_after = 1;
3567 }
3568 }
3569 if ($rtrim_before || $space_after) {
3570 if ($rtrim_before) {
3571 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3572 } else {
3573 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3574 }
3575 if ($space_after) {
3576 $good .= " ";
3577 }
3578 }
3579
3580 # '*' as part of a type definition -- reported already.
3581 } elsif ($opv eq '*_') {
3582 #warn "'*' is part of type\n";
3583
3584 # unary operators should have a space before and
3585 # none after. May be left adjacent to another
3586 # unary operator, or a cast
3587 } elsif ($op eq '!' || $op eq '~' ||
3588 $opv eq '*U' || $opv eq '-U' ||
3589 $opv eq '&U' || $opv eq '&&U') {
3590 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
3591 if (ERROR("SPACING",
3592 "space required before that '$op' $at\n" . $hereptr)) {
3593 if ($n != $last_after + 2) {
3594 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
3595 $line_fixed = 1;
3596 }
3597 }
3598 }
3599 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
3600 # A unary '*' may be const
3601
3602 } elsif ($ctx =~ /.xW/) {
3603 if (ERROR("SPACING",
3604 "space prohibited after that '$op' $at\n" . $hereptr)) {
3605 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
3606 if (defined $fix_elements[$n + 2]) {
3607 $fix_elements[$n + 2] =~ s/^\s+//;
3608 }
3609 $line_fixed = 1;
3610 }
3611 }
3612
3613 # unary ++ and unary -- are allowed no space on one side.
3614 } elsif ($op eq '++' or $op eq '--') {
3615 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
3616 if (ERROR("SPACING",
3617 "space required one side of that '$op' $at\n" . $hereptr)) {
3618 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3619 $line_fixed = 1;
3620 }
3621 }
3622 if ($ctx =~ /Wx[BE]/ ||
3623 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
3624 if (ERROR("SPACING",
3625 "space prohibited before that '$op' $at\n" . $hereptr)) {
3626 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3627 $line_fixed = 1;
3628 }
3629 }
3630 if ($ctx =~ /ExW/) {
3631 if (ERROR("SPACING",
3632 "space prohibited after that '$op' $at\n" . $hereptr)) {
3633 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3634 if (defined $fix_elements[$n + 2]) {
3635 $fix_elements[$n + 2] =~ s/^\s+//;
3636 }
3637 $line_fixed = 1;
3638 }
3639 }
3640
3641 # << and >> may either have or not have spaces both sides
3642 } elsif ($op eq '<<' or $op eq '>>' or
3643 $op eq '&' or $op eq '^' or $op eq '|' or
3644 $op eq '+' or $op eq '-' or
3645 $op eq '*' or $op eq '/' or
3646 $op eq '%')
3647 {
3648 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
3649 if (ERROR("SPACING",
3650 "need consistent spacing around '$op' $at\n" . $hereptr)) {
3651 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3652 if (defined $fix_elements[$n + 2]) {
3653 $fix_elements[$n + 2] =~ s/^\s+//;
3654 }
3655 $line_fixed = 1;
3656 }
3657 }
3658
3659 # A colon needs no spaces before when it is
3660 # terminating a case value or a label.
3661 } elsif ($opv eq ':C' || $opv eq ':L') {
3662 if ($ctx =~ /Wx./) {
3663 if (ERROR("SPACING",
3664 "space prohibited before that '$op' $at\n" . $hereptr)) {
3665 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3666 $line_fixed = 1;
3667 }
3668 }
3669
3670 # All the others need spaces both sides.
3671 } elsif ($ctx !~ /[EWC]x[CWE]/) {
3672 my $ok = 0;
3673
3674 # Ignore email addresses <foo@bar>
3675 if (($op eq '<' &&
3676 $cc =~ /^\S+\@\S+>/) ||
3677 ($op eq '>' &&
3678 $ca =~ /<\S+\@\S+$/))
3679 {
3680 $ok = 1;
3681 }
3682
3683 # messages are ERROR, but ?: are CHK
3684 if ($ok == 0) {
3685 my $msg_type = \&ERROR;
3686 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3687
3688 if (&{$msg_type}("SPACING",
3689 "spaces required around that '$op' $at\n" . $hereptr)) {
3690 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3691 if (defined $fix_elements[$n + 2]) {
3692 $fix_elements[$n + 2] =~ s/^\s+//;
3693 }
3694 $line_fixed = 1;
3695 }
3696 }
3697 }
3698 $off += length($elements[$n + 1]);
3699
3700## print("n: <$n> GOOD: <$good>\n");
3701
3702 $fixed_line = $fixed_line . $good;
3703 }
3704
3705 if (($#elements % 2) == 0) {
3706 $fixed_line = $fixed_line . $fix_elements[$#elements];
3707 }
3708
3709 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
3710 $fixed[$fixlinenr] = $fixed_line;
3711 }
3712
3713
3714 }
3715
3716# check for whitespace before a non-naked semicolon
3717 if ($line =~ /^\+.*\S\s+;\s*$/) {
3718 if (WARN("SPACING",
3719 "space prohibited before semicolon\n" . $herecurr) &&
3720 $fix) {
3721 1 while $fixed[$fixlinenr] =~
3722 s/^(\+.*\S)\s+;/$1;/;
3723 }
3724 }
3725
3726# check for multiple assignments
3727 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
3728 CHK("MULTIPLE_ASSIGNMENTS",
3729 "multiple assignments should be avoided\n" . $herecurr);
3730 }
3731
3732## # check for multiple declarations, allowing for a function declaration
3733## # continuation.
3734## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
3735## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
3736##
3737## # Remove any bracketed sections to ensure we do not
3738## # falsly report the parameters of functions.
3739## my $ln = $line;
3740## while ($ln =~ s/\([^\(\)]*\)//g) {
3741## }
3742## if ($ln =~ /,/) {
3743## WARN("MULTIPLE_DECLARATION",
3744## "declaring multiple variables together should be avoided\n" . $herecurr);
3745## }
3746## }
3747
3748#need space before brace following if, while, etc
3749 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
3750 $line =~ /do{/) {
3751 if (ERROR("SPACING",
3752 "space required before the open brace '{'\n" . $herecurr) &&
3753 $fix) {
3754 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
3755 }
3756 }
3757
3758## # check for blank lines before declarations
3759## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
3760## $prevrawline =~ /^.\s*$/) {
3761## WARN("SPACING",
3762## "No blank lines before declarations\n" . $hereprev);
3763## }
3764##
3765
3766# closing brace should have a space following it when it has anything
3767# on the line
3768 if ($line =~ /}(?!(?:,|;|\)))\S/) {
3769 if (ERROR("SPACING",
3770 "space required after that close brace '}'\n" . $herecurr) &&
3771 $fix) {
3772 $fixed[$fixlinenr] =~
3773 s/}((?!(?:,|;|\)))\S)/} $1/;
3774 }
3775 }
3776
3777# check spacing on square brackets
3778 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
3779 if (ERROR("SPACING",
3780 "space prohibited after that open square bracket '['\n" . $herecurr) &&
3781 $fix) {
3782 $fixed[$fixlinenr] =~
3783 s/\[\s+/\[/;
3784 }
3785 }
3786 if ($line =~ /\s\]/) {
3787 if (ERROR("SPACING",
3788 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
3789 $fix) {
3790 $fixed[$fixlinenr] =~
3791 s/\s+\]/\]/;
3792 }
3793 }
3794
3795# check spacing on parentheses
3796 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
3797 $line !~ /for\s*\(\s+;/) {
3798 if (ERROR("SPACING",
3799 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
3800 $fix) {
3801 $fixed[$fixlinenr] =~
3802 s/\(\s+/\(/;
3803 }
3804 }
3805 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
3806 $line !~ /for\s*\(.*;\s+\)/ &&
3807 $line !~ /:\s+\)/) {
3808 if (ERROR("SPACING",
3809 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3810 $fix) {
3811 $fixed[$fixlinenr] =~
3812 s/\s+\)/\)/;
3813 }
3814 }
3815
3816# check unnecessary parentheses around addressof/dereference single $Lvals
3817# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3818
3819 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3820 my $var = $1;
3821 if (CHK("UNNECESSARY_PARENTHESES",
3822 "Unnecessary parentheses around $var\n" . $herecurr) &&
3823 $fix) {
3824 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3825 }
3826 }
3827
3828# check for unnecessary parentheses around function pointer uses
3829# ie: (foo->bar)(); should be foo->bar();
3830# but not "if (foo->bar) (" to avoid some false positives
3831 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3832 my $var = $2;
3833 if (CHK("UNNECESSARY_PARENTHESES",
3834 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3835 $fix) {
3836 my $var2 = deparenthesize($var);
3837 $var2 =~ s/\s//g;
3838 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3839 }
3840 }
3841
3842#goto labels aren't indented, allow a single space however
3843 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
3844 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
3845 if (WARN("INDENTED_LABEL",
3846 "labels should not be indented\n" . $herecurr) &&
3847 $fix) {
3848 $fixed[$fixlinenr] =~
3849 s/^(.)\s+/$1/;
3850 }
3851 }
3852
3853# return is not a function
3854 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3855 my $spacing = $1;
3856 if ($^V && $^V ge 5.10.0 &&
3857 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
3858 my $value = $1;
3859 $value = deparenthesize($value);
3860 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
3861 ERROR("RETURN_PARENTHESES",
3862 "return is not a function, parentheses are not required\n" . $herecurr);
3863 }
3864 } elsif ($spacing !~ /\s+/) {
3865 ERROR("SPACING",
3866 "space required before the open parenthesis '('\n" . $herecurr);
3867 }
3868 }
3869
3870# unnecessary return in a void function
3871# at end-of-function, with the previous line a single leading tab, then return;
3872# and the line before that not a goto label target like "out:"
3873 if ($sline =~ /^[ \+]}\s*$/ &&
3874 $prevline =~ /^\+\treturn\s*;\s*$/ &&
3875 $linenr >= 3 &&
3876 $lines[$linenr - 3] =~ /^[ +]/ &&
3877 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
3878 WARN("RETURN_VOID",
3879 "void function return statements are not generally useful\n" . $hereprev);
3880 }
3881
3882# if statements using unnecessary parentheses - ie: if ((foo == bar))
3883 if ($^V && $^V ge 5.10.0 &&
3884 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3885 my $openparens = $1;
3886 my $count = $openparens =~ tr@\(@\(@;
3887 my $msg = "";
3888 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3889 my $comp = $4; #Not $1 because of $LvalOrFunc
3890 $msg = " - maybe == should be = ?" if ($comp eq "==");
3891 WARN("UNNECESSARY_PARENTHESES",
3892 "Unnecessary parentheses$msg\n" . $herecurr);
3893 }
3894 }
3895
3896# Return of what appears to be an errno should normally be -'ve
3897 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3898 my $name = $1;
3899 if ($name ne 'EOF' && $name ne 'ERROR') {
3900 WARN("USE_NEGATIVE_ERRNO",
3901 "return of an errno should typically be -ve (return -$1)\n" . $herecurr);
3902 }
3903 }
3904
3905# Need a space before open parenthesis after if, while etc
3906 if ($line =~ /\b(if|while|for|switch)\(/) {
3907 if (ERROR("SPACING",
3908 "space required before the open parenthesis '('\n" . $herecurr) &&
3909 $fix) {
3910 $fixed[$fixlinenr] =~
3911 s/\b(if|while|for|switch)\(/$1 \(/;
3912 }
3913 }
3914
3915# Check for illegal assignment in if conditional -- and check for trailing
3916# statements after the conditional.
3917 if ($line =~ /do\s*(?!{)/) {
3918 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3919 ctx_statement_block($linenr, $realcnt, 0)
3920 if (!defined $stat);
3921 my ($stat_next) = ctx_statement_block($line_nr_next,
3922 $remain_next, $off_next);
3923 $stat_next =~ s/\n./\n /g;
3924 ##print "stat<$stat> stat_next<$stat_next>\n";
3925
3926 if ($stat_next =~ /^\s*while\b/) {
3927 # If the statement carries leading newlines,
3928 # then count those as offsets.
3929 my ($whitespace) =
3930 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
3931 my $offset =
3932 statement_rawlines($whitespace) - 1;
3933
3934 $suppress_whiletrailers{$line_nr_next +
3935 $offset} = 1;
3936 }
3937 }
3938 if (!defined $suppress_whiletrailers{$linenr} &&
3939 defined($stat) && defined($cond) &&
3940 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3941 my ($s, $c) = ($stat, $cond);
3942
3943 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
3944 ERROR("ASSIGN_IN_IF",
3945 "do not use assignment in if condition\n" . $herecurr);
3946 }
3947
3948 # Find out what is on the end of the line after the
3949 # conditional.
3950 substr($s, 0, length($c), '');
3951 $s =~ s/\n.*//g;
3952 $s =~ s/$;//g; # Remove any comments
3953 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
3954 $c !~ /}\s*while\s*/)
3955 {
3956 # Find out how long the conditional actually is.
3957 my @newlines = ($c =~ /\n/gs);
3958 my $cond_lines = 1 + $#newlines;
3959 my $stat_real = '';
3960
3961 $stat_real = raw_line($linenr, $cond_lines)
3962 . "\n" if ($cond_lines);
3963 if (defined($stat_real) && $cond_lines > 1) {
3964 $stat_real = "[...]\n$stat_real";
3965 }
3966
3967 ERROR("TRAILING_STATEMENTS",
3968 "trailing statements should be on next line\n" . $herecurr . $stat_real);
3969 }
3970 }
3971
3972# Check for bitwise tests written as boolean
3973 if ($line =~ /
3974 (?:
3975 (?:\[|\(|\&\&|\|\|)
3976 \s*0[xX][0-9]+\s*
3977 (?:\&\&|\|\|)
3978 |
3979 (?:\&\&|\|\|)
3980 \s*0[xX][0-9]+\s*
3981 (?:\&\&|\|\||\)|\])
3982 )/x)
3983 {
3984 WARN("HEXADECIMAL_BOOLEAN_TEST",
3985 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
3986 }
3987
3988# if and else should not have general statements after it
3989 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
3990 my $s = $1;
3991 $s =~ s/$;//g; # Remove any comments
3992 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
3993 ERROR("TRAILING_STATEMENTS",
3994 "trailing statements should be on next line\n" . $herecurr);
3995 }
3996 }
3997# if should not continue a brace
3998 if ($line =~ /}\s*if\b/) {
3999 ERROR("TRAILING_STATEMENTS",
4000 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
4001 $herecurr);
4002 }
4003# case and default should not have general statements after them
4004 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4005 $line !~ /\G(?:
4006 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4007 \s*return\s+
4008 )/xg)
4009 {
4010 ERROR("TRAILING_STATEMENTS",
4011 "trailing statements should be on next line\n" . $herecurr);
4012 }
4013
4014 # Check for }<nl>else {, these must be at the same
4015 # indent level to be relevant to each other.
4016 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4017 $previndent == $indent) {
4018 if (ERROR("ELSE_AFTER_BRACE",
4019 "else should follow close brace '}'\n" . $hereprev) &&
4020 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4021 fix_delete_line($fixlinenr - 1, $prevrawline);
4022 fix_delete_line($fixlinenr, $rawline);
4023 my $fixedline = $prevrawline;
4024 $fixedline =~ s/}\s*$//;
4025 if ($fixedline !~ /^\+\s*$/) {
4026 fix_insert_line($fixlinenr, $fixedline);
4027 }
4028 $fixedline = $rawline;
4029 $fixedline =~ s/^(.\s*)else/$1} else/;
4030 fix_insert_line($fixlinenr, $fixedline);
4031 }
4032 }
4033
4034 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4035 $previndent == $indent) {
4036 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4037
4038 # Find out what is on the end of the line after the
4039 # conditional.
4040 substr($s, 0, length($c), '');
4041 $s =~ s/\n.*//g;
4042
4043 if ($s =~ /^\s*;/) {
4044 if (ERROR("WHILE_AFTER_BRACE",
4045 "while should follow close brace '}'\n" . $hereprev) &&
4046 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4047 fix_delete_line($fixlinenr - 1, $prevrawline);
4048 fix_delete_line($fixlinenr, $rawline);
4049 my $fixedline = $prevrawline;
4050 my $trailing = $rawline;
4051 $trailing =~ s/^\+//;
4052 $trailing = trim($trailing);
4053 $fixedline =~ s/}\s*$/} $trailing/;
4054 fix_insert_line($fixlinenr, $fixedline);
4055 }
4056 }
4057 }
4058
4059#Specific variable tests
4060 while ($line =~ m{($Constant|$Lval)}g) {
4061 my $var = $1;
4062
4063#gcc binary extension
4064 if ($var =~ /^$Binary$/) {
4065 if (WARN("GCC_BINARY_CONSTANT",
4066 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4067 $fix) {
4068 my $hexval = sprintf("0x%x", oct($var));
4069 $fixed[$fixlinenr] =~
4070 s/\b$var\b/$hexval/;
4071 }
4072 }
4073
4074#CamelCase
4075 if ($var !~ /^$Constant$/ &&
4076 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
4077#Ignore Page<foo> variants
4078 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
4079#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4080 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4081#Ignore some three character SI units explicitly, like MiB and KHz
4082 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
4083 while ($var =~ m{($Ident)}g) {
4084 my $word = $1;
4085 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4086 if ($check) {
4087 seed_camelcase_includes();
4088 if (!$file && !$camelcase_file_seeded) {
4089 seed_camelcase_file($realfile);
4090 $camelcase_file_seeded = 1;
4091 }
4092 }
4093 if (!defined $camelcase{$word}) {
4094 $camelcase{$word} = 1;
4095 CHK("CAMELCASE",
4096 "Avoid CamelCase: <$word>\n" . $herecurr);
4097 }
4098 }
4099 }
4100 }
4101
4102#no spaces allowed after \ in define
4103 if ($line =~ /\#\s*define.*\\\s+$/) {
4104 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4105 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4106 $fix) {
4107 $fixed[$fixlinenr] =~ s/\s+$//;
4108 }
4109 }
4110
4111#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
4112 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4113 my $file = "$1.h";
4114 my $checkfile = "include/linux/$file";
4115 if (-f "$root/$checkfile" &&
4116 $realfile ne $checkfile &&
4117 $1 !~ /$allowed_asm_includes/)
4118 {
4119 if ($realfile =~ m{^arch/}) {
4120 CHK("ARCH_INCLUDE_LINUX",
4121 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4122 } else {
4123 WARN("INCLUDE_LINUX",
4124 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4125 }
4126 }
4127 }
4128
4129# multi-statement macros should be enclosed in a do while loop, grab the
4130# first statement and ensure its the whole macro if its not enclosed
4131# in a known good container
4132 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4133 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4134 my $ln = $linenr;
4135 my $cnt = $realcnt;
4136 my ($off, $dstat, $dcond, $rest);
4137 my $ctx = '';
4138 my $has_flow_statement = 0;
4139 my $has_arg_concat = 0;
4140 ($dstat, $dcond, $ln, $cnt, $off) =
4141 ctx_statement_block($linenr, $realcnt, 0);
4142 $ctx = $dstat;
4143 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4144 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4145
4146 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4147 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4148
4149 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4150 $dstat =~ s/$;//g;
4151 $dstat =~ s/\\\n.//g;
4152 $dstat =~ s/^\s*//s;
4153 $dstat =~ s/\s*$//s;
4154
4155 # Flatten any parentheses and braces
4156 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4157 $dstat =~ s/\{[^\{\}]*\}/1/ ||
4158 $dstat =~ s/\[[^\[\]]*\]/1/)
4159 {
4160 }
4161
4162 # Flatten any obvious string concatentation.
4163 while ($dstat =~ s/("X*")\s*$Ident/$1/ ||
4164 $dstat =~ s/$Ident\s*("X*")/$1/)
4165 {
4166 }
4167
4168 my $exceptions = qr{
4169 $Declare|
4170 module_param_named|
4171 MODULE_PARM_DESC|
4172 DECLARE_PER_CPU|
4173 DEFINE_PER_CPU|
4174 __typeof__\(|
4175 union|
4176 struct|
4177 \.$Ident\s*=\s*|
4178 ^\"|\"$
4179 }x;
4180 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4181 if ($dstat ne '' &&
4182 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4183 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
4184 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4185 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
4186 $dstat !~ /$exceptions/ &&
4187 $dstat !~ /^\.$Ident\s*=/ && # .foo =
4188 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
4189 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
4190 $dstat !~ /^for\s*$Constant$/ && # for (...)
4191 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4192 $dstat !~ /^do\s*{/ && # do {...
4193 $dstat !~ /^\({/ && # ({...
4194 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4195 {
4196 $ctx =~ s/\n*$//;
4197 my $herectx = $here . "\n";
4198 my $cnt = statement_rawlines($ctx);
4199
4200 for (my $n = 0; $n < $cnt; $n++) {
4201 $herectx .= raw_line($linenr, $n) . "\n";
4202 }
4203
4204 if ($dstat =~ /;/) {
4205 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4206 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4207 } else {
4208 ERROR("COMPLEX_MACRO",
4209 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4210 }
4211 }
4212
4213# check for macros with flow control, but without ## concatenation
4214# ## concatenation is commonly a macro that defines a function so ignore those
4215 if ($has_flow_statement && !$has_arg_concat) {
4216 my $herectx = $here . "\n";
4217 my $cnt = statement_rawlines($ctx);
4218
4219 for (my $n = 0; $n < $cnt; $n++) {
4220 $herectx .= raw_line($linenr, $n) . "\n";
4221 }
4222 WARN("MACRO_WITH_FLOW_CONTROL",
4223 "Macros with flow control statements should be avoided\n" . "$herectx");
4224 }
4225
4226# check for line continuations outside of #defines, preprocessor #, and asm
4227
4228 } else {
4229 if ($prevline !~ /^..*\\$/ &&
4230 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4231 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
4232 $line =~ /^\+.*\\$/) {
4233 WARN("LINE_CONTINUATIONS",
4234 "Avoid unnecessary line continuations\n" . $herecurr);
4235 }
4236 }
4237
4238# do {} while (0) macro tests:
4239# single-statement macros do not need to be enclosed in do while (0) loop,
4240# macro should not end with a semicolon
4241 if ($^V && $^V ge 5.10.0 &&
4242 $realfile !~ m@/vmlinux.lds.h$@ &&
4243 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4244 my $ln = $linenr;
4245 my $cnt = $realcnt;
4246 my ($off, $dstat, $dcond, $rest);
4247 my $ctx = '';
4248 ($dstat, $dcond, $ln, $cnt, $off) =
4249 ctx_statement_block($linenr, $realcnt, 0);
4250 $ctx = $dstat;
4251
4252 $dstat =~ s/\\\n.//g;
4253
4254 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4255 my $stmts = $2;
4256 my $semis = $3;
4257
4258 $ctx =~ s/\n*$//;
4259 my $cnt = statement_rawlines($ctx);
4260 my $herectx = $here . "\n";
4261
4262 for (my $n = 0; $n < $cnt; $n++) {
4263 $herectx .= raw_line($linenr, $n) . "\n";
4264 }
4265
4266 if (($stmts =~ tr/;/;/) == 1 &&
4267 $stmts !~ /^\s*(if|while|for|switch)\b/) {
4268 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4269 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4270 }
4271 if (defined $semis && $semis ne "") {
4272 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4273 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4274 }
4275 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4276 $ctx =~ s/\n*$//;
4277 my $cnt = statement_rawlines($ctx);
4278 my $herectx = $here . "\n";
4279
4280 for (my $n = 0; $n < $cnt; $n++) {
4281 $herectx .= raw_line($linenr, $n) . "\n";
4282 }
4283
4284 WARN("TRAILING_SEMICOLON",
4285 "macros should not use a trailing semicolon\n" . "$herectx");
4286 }
4287 }
4288
4289# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4290# all assignments may have only one of the following with an assignment:
4291# .
4292# ALIGN(...)
4293# VMLINUX_SYMBOL(...)
4294 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4295 WARN("MISSING_VMLINUX_SYMBOL",
4296 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4297 }
4298
4299# check for redundant bracing round if etc
4300 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4301 my ($level, $endln, @chunks) =
4302 ctx_statement_full($linenr, $realcnt, 1);
4303 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4304 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4305 if ($#chunks > 0 && $level == 0) {
4306 my @allowed = ();
4307 my $allow = 0;
4308 my $seen = 0;
4309 my $herectx = $here . "\n";
4310 my $ln = $linenr - 1;
4311 for my $chunk (@chunks) {
4312 my ($cond, $block) = @{$chunk};
4313
4314 # If the condition carries leading newlines, then count those as offsets.
4315 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4316 my $offset = statement_rawlines($whitespace) - 1;
4317
4318 $allowed[$allow] = 0;
4319 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4320
4321 # We have looked at and allowed this specific line.
4322 $suppress_ifbraces{$ln + $offset} = 1;
4323
4324 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4325 $ln += statement_rawlines($block) - 1;
4326
4327 substr($block, 0, length($cond), '');
4328
4329 $seen++ if ($block =~ /^\s*{/);
4330
4331 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4332 if (statement_lines($cond) > 1) {
4333 #print "APW: ALLOWED: cond<$cond>\n";
4334 $allowed[$allow] = 1;
4335 }
4336 if ($block =~/\b(?:if|for|while)\b/) {
4337 #print "APW: ALLOWED: block<$block>\n";
4338 $allowed[$allow] = 1;
4339 }
4340 if (statement_block_size($block) > 1) {
4341 #print "APW: ALLOWED: lines block<$block>\n";
4342 $allowed[$allow] = 1;
4343 }
4344 $allow++;
4345 }
4346 if ($seen) {
4347 my $sum_allowed = 0;
4348 foreach (@allowed) {
4349 $sum_allowed += $_;
4350 }
4351 if ($sum_allowed == 0) {
4352 WARN("BRACES",
4353 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4354 } elsif ($sum_allowed != $allow &&
4355 $seen != $allow) {
4356 CHK("BRACES",
4357 "braces {} should be used on all arms of this statement\n" . $herectx);
4358 }
4359 }
4360 }
4361 }
4362 if (!defined $suppress_ifbraces{$linenr - 1} &&
4363 $line =~ /\b(if|while|for|else)\b/) {
4364 my $allowed = 0;
4365
4366 # Check the pre-context.
4367 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4368 #print "APW: ALLOWED: pre<$1>\n";
4369 $allowed = 1;
4370 }
4371
4372 my ($level, $endln, @chunks) =
4373 ctx_statement_full($linenr, $realcnt, $-[0]);
4374
4375 # Check the condition.
4376 my ($cond, $block) = @{$chunks[0]};
4377 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4378 if (defined $cond) {
4379 substr($block, 0, length($cond), '');
4380 }
4381 if (statement_lines($cond) > 1) {
4382 #print "APW: ALLOWED: cond<$cond>\n";
4383 $allowed = 1;
4384 }
4385 if ($block =~/\b(?:if|for|while)\b/) {
4386 #print "APW: ALLOWED: block<$block>\n";
4387 $allowed = 1;
4388 }
4389 if (statement_block_size($block) > 1) {
4390 #print "APW: ALLOWED: lines block<$block>\n";
4391 $allowed = 1;
4392 }
4393 # Check the post-context.
4394 if (defined $chunks[1]) {
4395 my ($cond, $block) = @{$chunks[1]};
4396 if (defined $cond) {
4397 substr($block, 0, length($cond), '');
4398 }
4399 if ($block =~ /^\s*\{/) {
4400 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4401 $allowed = 1;
4402 }
4403 }
4404 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
4405 my $herectx = $here . "\n";
4406 my $cnt = statement_rawlines($block);
4407
4408 for (my $n = 0; $n < $cnt; $n++) {
4409 $herectx .= raw_line($linenr, $n) . "\n";
4410 }
4411
4412 WARN("BRACES",
4413 "braces {} are not necessary for single statement blocks\n" . $herectx);
4414 }
4415 }
4416
4417# check for unnecessary blank lines around braces
4418 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
4419 CHK("BRACES",
4420 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
4421 }
4422 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
4423 CHK("BRACES",
4424 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
4425 }
4426
4427# no volatiles please
4428 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
4429 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4430 WARN("VOLATILE",
4431 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
4432 }
4433
4434# Check for user-visible strings broken across lines, which breaks the ability
4435# to grep for the string. Make exceptions when the previous string ends in a
4436# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4437# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
4438 if ($line =~ /^\+\s*"[X\t]*"/ &&
4439 $prevline =~ /"\s*$/ &&
4440 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4441 if (WARN("SPLIT_STRING",
4442 "quoted string split across lines\n" . $hereprev) &&
4443 $fix &&
4444 $prevrawline =~ /^\+.*"\s*$/ &&
4445 $last_coalesced_string_linenr != $linenr - 1) {
4446 my $extracted_string = get_quoted_string($line, $rawline);
4447 my $comma_close = "";
4448 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
4449 $comma_close = $1;
4450 }
4451
4452 fix_delete_line($fixlinenr - 1, $prevrawline);
4453 fix_delete_line($fixlinenr, $rawline);
4454 my $fixedline = $prevrawline;
4455 $fixedline =~ s/"\s*$//;
4456 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
4457 fix_insert_line($fixlinenr - 1, $fixedline);
4458 $fixedline = $rawline;
4459 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
4460 if ($fixedline !~ /\+\s*$/) {
4461 fix_insert_line($fixlinenr, $fixedline);
4462 }
4463 $last_coalesced_string_linenr = $linenr;
4464 }
4465 }
4466
4467# check for missing a space in a string concatenation
4468 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
4469 WARN('MISSING_SPACE',
4470 "break quoted strings at a space character\n" . $hereprev);
4471 }
4472
4473# check for spaces before a quoted newline
4474 if ($rawline =~ /^.*\".*\s\\n/) {
4475 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
4476 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
4477 $fix) {
4478 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
4479 }
4480
4481 }
4482
4483# concatenated string without spaces between elements
4484 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4485 CHK("CONCATENATED_STRING",
4486 "Concatenated strings should use spaces between elements\n" . $herecurr);
4487 }
4488
4489# uncoalesced string fragments
4490 if ($line =~ /"X*"\s*"/) {
4491 WARN("STRING_FRAGMENTS",
4492 "Consecutive strings are generally better as a single string\n" . $herecurr);
4493 }
4494
4495# check for %L{u,d,i} in strings
4496 my $string;
4497 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4498 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4499 $string =~ s/%%/__/g;
4500 if ($string =~ /(?<!%)%L[udi]/) {
4501 WARN("PRINTF_L",
4502 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4503 last;
4504 }
4505 }
4506
4507# check for line continuations in quoted strings with odd counts of "
4508 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4509 WARN("LINE_CONTINUATIONS",
4510 "Avoid line continuations in quoted strings\n" . $herecurr);
4511 }
4512
4513# warn about #if 0
4514 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4515 CHK("REDUNDANT_CODE",
4516 "if this code is redundant consider removing it\n" .
4517 $herecurr);
4518 }
4519
4520# check for needless "if (<foo>) fn(<foo>)" uses
4521 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
4522 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4523 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4524 WARN('NEEDLESS_IF',
4525 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
4526 }
4527 }
4528
4529# check for unnecessary "Out of Memory" messages
4530 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
4531 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
4532 (defined $1 || defined $3) &&
4533 $linenr > 3) {
4534 my $testval = $2;
4535 my $testline = $lines[$linenr - 3];
4536
4537 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
4538# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
4539
4540 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
4541 WARN("OOM_MESSAGE",
4542 "Possible unnecessary 'out of memory' message\n" . $hereprev);
4543 }
4544 }
4545
4546# check for logging functions with KERN_<LEVEL>
4547 if ($line !~ /printk\s*\(/ &&
4548 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4549 my $level = $1;
4550 if (WARN("UNNECESSARY_KERN_LEVEL",
4551 "Possible unnecessary $level\n" . $herecurr) &&
4552 $fix) {
4553 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4554 }
4555 }
4556
4557# check for mask then right shift without a parentheses
4558 if ($^V && $^V ge 5.10.0 &&
4559 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4560 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4561 WARN("MASK_THEN_SHIFT",
4562 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4563 }
4564
4565# check for pointer comparisons to NULL
4566 if ($^V && $^V ge 5.10.0) {
4567 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4568 my $val = $1;
4569 my $equal = "!";
4570 $equal = "" if ($4 eq "!=");
4571 if (CHK("COMPARISON_TO_NULL",
4572 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4573 $fix) {
4574 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4575 }
4576 }
4577 }
4578
4579# check for bad placement of section $InitAttribute (e.g.: __initdata)
4580 if ($line =~ /(\b$InitAttribute\b)/) {
4581 my $attr = $1;
4582 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
4583 my $ptr = $1;
4584 my $var = $2;
4585 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
4586 ERROR("MISPLACED_INIT",
4587 "$attr should be placed after $var\n" . $herecurr)) ||
4588 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
4589 WARN("MISPLACED_INIT",
4590 "$attr should be placed after $var\n" . $herecurr))) &&
4591 $fix) {
4592 $fixed[$fixlinenr] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e;
4593 }
4594 }
4595 }
4596
4597# check for $InitAttributeData (ie: __initdata) with const
4598 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
4599 my $attr = $1;
4600 $attr =~ /($InitAttributePrefix)(.*)/;
4601 my $attr_prefix = $1;
4602 my $attr_type = $2;
4603 if (ERROR("INIT_ATTRIBUTE",
4604 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
4605 $fix) {
4606 $fixed[$fixlinenr] =~
4607 s/$InitAttributeData/${attr_prefix}initconst/;
4608 }
4609 }
4610
4611# check for $InitAttributeConst (ie: __initconst) without const
4612 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
4613 my $attr = $1;
4614 if (ERROR("INIT_ATTRIBUTE",
4615 "Use of $attr requires a separate use of const\n" . $herecurr) &&
4616 $fix) {
4617 my $lead = $fixed[$fixlinenr] =~
4618 /(^\+\s*(?:static\s+))/;
4619 $lead = rtrim($1);
4620 $lead = "$lead " if ($lead !~ /^\+$/);
4621 $lead = "${lead}const ";
4622 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
4623 }
4624 }
4625
4626# don't use __constant_<foo> functions outside of include/uapi/
4627 if ($realfile !~ m@^include/uapi/@ &&
4628 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
4629 my $constant_func = $1;
4630 my $func = $constant_func;
4631 $func =~ s/^__constant_//;
4632 if (WARN("CONSTANT_CONVERSION",
4633 "$constant_func should be $func\n" . $herecurr) &&
4634 $fix) {
4635 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
4636 }
4637 }
4638
4639# prefer usleep_range over udelay
4640 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
4641 my $delay = $1;
4642 # ignore udelay's < 10, however
4643 if (! ($delay < 10) ) {
4644 CHK("USLEEP_RANGE",
4645 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4646 }
4647 if ($delay > 2000) {
4648 WARN("LONG_UDELAY",
4649 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
4650 }
4651 }
4652
4653# warn about unexpectedly long msleep's
4654 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
4655 if ($1 < 20) {
4656 WARN("MSLEEP",
4657 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
4658 }
4659 }
4660
4661# check for comparisons of jiffies
4662 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
4663 WARN("JIFFIES_COMPARISON",
4664 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
4665 }
4666
4667# check for comparisons of get_jiffies_64()
4668 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
4669 WARN("JIFFIES_COMPARISON",
4670 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
4671 }
4672
4673# warn about #ifdefs in C files
4674# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
4675# print "#ifdef in C files should be avoided\n";
4676# print "$herecurr";
4677# $clean = 0;
4678# }
4679
4680# warn about spacing in #ifdefs
4681 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
4682 if (ERROR("SPACING",
4683 "exactly one space required after that #$1\n" . $herecurr) &&
4684 $fix) {
4685 $fixed[$fixlinenr] =~
4686 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
4687 }
4688
4689 }
4690
4691# check for spinlock_t definitions without a comment.
4692 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
4693 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
4694 my $which = $1;
4695 if (!ctx_has_comment($first_line, $linenr)) {
4696 CHK("UNCOMMENTED_DEFINITION",
4697 "$1 definition without comment\n" . $herecurr);
4698 }
4699 }
4700# check for memory barriers without a comment.
4701 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
4702 if (!ctx_has_comment($first_line, $linenr)) {
4703 WARN("MEMORY_BARRIER",
4704 "memory barrier without comment\n" . $herecurr);
4705 }
4706 }
4707# check of hardware specific defines
4708 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4709 CHK("ARCH_DEFINES",
4710 "architecture specific defines should be avoided\n" . $herecurr);
4711 }
4712
4713# Check that the storage class is at the beginning of a declaration
4714 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
4715 WARN("STORAGE_CLASS",
4716 "storage class should be at the beginning of the declaration\n" . $herecurr)
4717 }
4718
4719# check the location of the inline attribute, that it is between
4720# storage class and type.
4721 if ($line =~ /\b$Type\s+$Inline\b/ ||
4722 $line =~ /\b$Inline\s+$Storage\b/) {
4723 ERROR("INLINE_LOCATION",
4724 "inline keyword should sit between storage class and type\n" . $herecurr);
4725 }
4726
4727# Check for __inline__ and __inline, prefer inline
4728 if ($realfile !~ m@\binclude/uapi/@ &&
4729 $line =~ /\b(__inline__|__inline)\b/) {
4730 if (WARN("INLINE",
4731 "plain inline is preferred over $1\n" . $herecurr) &&
4732 $fix) {
4733 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
4734
4735 }
4736 }
4737
4738# Check for __attribute__ packed, prefer __packed
4739 if ($realfile !~ m@\binclude/uapi/@ &&
4740 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
4741 WARN("PREFER_PACKED",
4742 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
4743 }
4744
4745# Check for __attribute__ aligned, prefer __aligned
4746 if ($realfile !~ m@\binclude/uapi/@ &&
4747 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
4748 WARN("PREFER_ALIGNED",
4749 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
4750 }
4751
4752# Check for __attribute__ format(printf, prefer __printf
4753 if ($realfile !~ m@\binclude/uapi/@ &&
4754 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
4755 if (WARN("PREFER_PRINTF",
4756 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
4757 $fix) {
4758 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
4759
4760 }
4761 }
4762
4763# Check for __attribute__ format(scanf, prefer __scanf
4764 if ($realfile !~ m@\binclude/uapi/@ &&
4765 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
4766 if (WARN("PREFER_SCANF",
4767 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
4768 $fix) {
4769 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
4770 }
4771 }
4772
4773# Check for __attribute__ weak, or __weak declarations (may have link issues)
4774 if ($^V && $^V ge 5.10.0 &&
4775 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4776 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4777 $line =~ /\b__weak\b/)) {
4778 ERROR("WEAK_DECLARATION",
4779 "Using weak declarations can have unintended link defects\n" . $herecurr);
4780 }
4781
4782# check for sizeof(&)
4783 if ($line =~ /\bsizeof\s*\(\s*\&/) {
4784 WARN("SIZEOF_ADDRESS",
4785 "sizeof(& should be avoided\n" . $herecurr);
4786 }
4787
4788# check for sizeof without parenthesis
4789 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
4790 if (WARN("SIZEOF_PARENTHESIS",
4791 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
4792 $fix) {
4793 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
4794 }
4795 }
4796
4797# check for struct spinlock declarations
4798 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4799 WARN("USE_SPINLOCK_T",
4800 "struct spinlock should be spinlock_t\n" . $herecurr);
4801 }
4802
4803# check for seq_printf uses that could be seq_puts
4804 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
4805 my $fmt = get_quoted_string($line, $rawline);
4806 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
4807 if (WARN("PREFER_SEQ_PUTS",
4808 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
4809 $fix) {
4810 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
4811 }
4812 }
4813 }
4814
4815# Check for misused memsets
4816 if ($^V && $^V ge 5.10.0 &&
4817 defined $stat &&
4818 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
4819
4820 my $ms_addr = $2;
4821 my $ms_val = $7;
4822 my $ms_size = $12;
4823
4824 if ($ms_size =~ /^(0x|)0$/i) {
4825 ERROR("MEMSET",
4826 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
4827 } elsif ($ms_size =~ /^(0x|)1$/i) {
4828 WARN("MEMSET",
4829 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
4830 }
4831 }
4832
4833# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4834 if ($^V && $^V ge 5.10.0 &&
4835 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4836 if (WARN("PREFER_ETHER_ADDR_COPY",
4837 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4838 $fix) {
4839 $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
4840 }
4841 }
4842
4843# typecasts on min/max could be min_t/max_t
4844 if ($^V && $^V ge 5.10.0 &&
4845 defined $stat &&
4846 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
4847 if (defined $2 || defined $7) {
4848 my $call = $1;
4849 my $cast1 = deparenthesize($2);
4850 my $arg1 = $3;
4851 my $cast2 = deparenthesize($7);
4852 my $arg2 = $8;
4853 my $cast;
4854
4855 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
4856 $cast = "$cast1 or $cast2";
4857 } elsif ($cast1 ne "") {
4858 $cast = $cast1;
4859 } else {
4860 $cast = $cast2;
4861 }
4862 WARN("MINMAX",
4863 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
4864 }
4865 }
4866
4867# check usleep_range arguments
4868 if ($^V && $^V ge 5.10.0 &&
4869 defined $stat &&
4870 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
4871 my $min = $1;
4872 my $max = $7;
4873 if ($min eq $max) {
4874 WARN("USLEEP_RANGE",
4875 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4876 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
4877 $min > $max) {
4878 WARN("USLEEP_RANGE",
4879 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
4880 }
4881 }
4882
4883# check for naked sscanf
4884 if ($^V && $^V ge 5.10.0 &&
4885 defined $stat &&
4886 $line =~ /\bsscanf\b/ &&
4887 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4888 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4889 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4890 my $lc = $stat =~ tr@\n@@;
4891 $lc = $lc + $linenr;
4892 my $stat_real = raw_line($linenr, 0);
4893 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4894 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4895 }
4896 WARN("NAKED_SSCANF",
4897 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4898 }
4899
4900# check for simple sscanf that should be kstrto<foo>
4901 if ($^V && $^V ge 5.10.0 &&
4902 defined $stat &&
4903 $line =~ /\bsscanf\b/) {
4904 my $lc = $stat =~ tr@\n@@;
4905 $lc = $lc + $linenr;
4906 my $stat_real = raw_line($linenr, 0);
4907 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4908 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4909 }
4910 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
4911 my $format = $6;
4912 my $count = $format =~ tr@%@%@;
4913 if ($count == 1 &&
4914 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
4915 WARN("SSCANF_TO_KSTRTO",
4916 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
4917 }
4918 }
4919 }
4920
4921# check for new externs in .h files.
4922 if ($realfile =~ /\.h$/ &&
4923 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
4924 if (CHK("AVOID_EXTERNS",
4925 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
4926 $fix) {
4927 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
4928 }
4929 }
4930
4931# check for new externs in .c files.
4932 if ($realfile =~ /\.c$/ && defined $stat &&
4933 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
4934 {
4935 my $function_name = $1;
4936 my $paren_space = $2;
4937
4938 my $s = $stat;
4939 if (defined $cond) {
4940 substr($s, 0, length($cond), '');
4941 }
4942 if ($s =~ /^\s*;/ &&
4943 $function_name ne 'uninitialized_var')
4944 {
4945 WARN("AVOID_EXTERNS",
4946 "externs should be avoided in .c files\n" . $herecurr);
4947 }
4948
4949 if ($paren_space =~ /\n/) {
4950 WARN("FUNCTION_ARGUMENTS",
4951 "arguments for function declarations should follow identifier\n" . $herecurr);
4952 }
4953
4954 } elsif ($realfile =~ /\.c$/ && defined $stat &&
4955 $stat =~ /^.\s*extern\s+/)
4956 {
4957 WARN("AVOID_EXTERNS",
4958 "externs should be avoided in .c files\n" . $herecurr);
4959 }
4960
4961# checks for new __setup's
4962 if ($rawline =~ /\b__setup\("([^"]*)"/) {
4963 my $name = $1;
4964
4965 if (!grep(/$name/, @setup_docs)) {
4966 CHK("UNDOCUMENTED_SETUP",
4967 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
4968 }
4969 }
4970
4971# check for pointless casting of kmalloc return
4972 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
4973 WARN("UNNECESSARY_CASTS",
4974 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
4975 }
4976
4977# alloc style
4978# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
4979 if ($^V && $^V ge 5.10.0 &&
4980 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
4981 CHK("ALLOC_SIZEOF_STRUCT",
4982 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
4983 }
4984
4985# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
4986 if ($^V && $^V ge 5.10.0 &&
4987 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
4988 my $oldfunc = $3;
4989 my $a1 = $4;
4990 my $a2 = $10;
4991 my $newfunc = "kmalloc_array";
4992 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
4993 my $r1 = $a1;
4994 my $r2 = $a2;
4995 if ($a1 =~ /^sizeof\s*\S/) {
4996 $r1 = $a2;
4997 $r2 = $a1;
4998 }
4999 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5000 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5001 if (WARN("ALLOC_WITH_MULTIPLY",
5002 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5003 $fix) {
5004 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
5005
5006 }
5007 }
5008 }
5009
5010# check for krealloc arg reuse
5011 if ($^V && $^V ge 5.10.0 &&
5012 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5013 WARN("KREALLOC_ARG_REUSE",
5014 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5015 }
5016
5017# check for alloc argument mismatch
5018 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5019 WARN("ALLOC_ARRAY_ARGS",
5020 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5021 }
5022
5023# check for multiple semicolons
5024 if ($line =~ /;\s*;\s*$/) {
5025 if (WARN("ONE_SEMICOLON",
5026 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5027 $fix) {
5028 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
5029 }
5030 }
5031
5032# check for #defines like: 1 << <digit> that could be BIT(digit)
5033 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
5034 my $ull = "";
5035 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5036 if (CHK("BIT_MACRO",
5037 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5038 $fix) {
5039 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5040 }
5041 }
5042
5043# check for case / default statements not preceded by break/fallthrough/switch
5044 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5045 my $has_break = 0;
5046 my $has_statement = 0;
5047 my $count = 0;
5048 my $prevline = $linenr;
5049 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
5050 $prevline--;
5051 my $rline = $rawlines[$prevline - 1];
5052 my $fline = $lines[$prevline - 1];
5053 last if ($fline =~ /^\@\@/);
5054 next if ($fline =~ /^\-/);
5055 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5056 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5057 next if ($fline =~ /^.[\s$;]*$/);
5058 $has_statement = 1;
5059 $count++;
5060 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5061 }
5062 if (!$has_break && $has_statement) {
5063 WARN("MISSING_BREAK",
5064 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5065 }
5066 }
5067
5068# check for switch/default statements without a break;
5069 if ($^V && $^V ge 5.10.0 &&
5070 defined $stat &&
5071 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5072 my $ctx = '';
5073 my $herectx = $here . "\n";
5074 my $cnt = statement_rawlines($stat);
5075 for (my $n = 0; $n < $cnt; $n++) {
5076 $herectx .= raw_line($linenr, $n) . "\n";
5077 }
5078 WARN("DEFAULT_NO_BREAK",
5079 "switch default: should use break\n" . $herectx);
5080 }
5081
5082# check for gcc specific __FUNCTION__
5083 if ($line =~ /\b__FUNCTION__\b/) {
5084 if (WARN("USE_FUNC",
5085 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5086 $fix) {
5087 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
5088 }
5089 }
5090
5091# check for use of yield()
5092 if ($line =~ /\byield\s*\(\s*\)/) {
5093 WARN("YIELD",
5094 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5095 }
5096
5097# check for comparisons against true and false
5098 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5099 my $lead = $1;
5100 my $arg = $2;
5101 my $test = $3;
5102 my $otype = $4;
5103 my $trail = $5;
5104 my $op = "!";
5105
5106 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5107
5108 my $type = lc($otype);
5109 if ($type =~ /^(?:true|false)$/) {
5110 if (("$test" eq "==" && "$type" eq "true") ||
5111 ("$test" eq "!=" && "$type" eq "false")) {
5112 $op = "";
5113 }
5114
5115 CHK("BOOL_COMPARISON",
5116 "Using comparison to $otype is error prone\n" . $herecurr);
5117
5118## maybe suggesting a correct construct would better
5119## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5120
5121 }
5122 }
5123
5124# check for semaphores initialized locked
5125 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
5126 WARN("CONSIDER_COMPLETION",
5127 "consider using a completion\n" . $herecurr);
5128 }
5129
5130# recommend kstrto* over simple_strto* and strict_strto*
5131 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
5132 WARN("CONSIDER_KSTRTO",
5133 "$1 is obsolete, use k$3 instead\n" . $herecurr);
5134 }
5135
5136# check for __initcall(), use device_initcall() explicitly or more appropriate function please
5137 if ($line =~ /^.\s*__initcall\s*\(/) {
5138 WARN("USE_DEVICE_INITCALL",
5139 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
5140 }
5141
5142# check for various ops structs, ensure they are const.
5143 my $struct_ops = qr{acpi_dock_ops|
5144 address_space_operations|
5145 backlight_ops|
5146 block_device_operations|
5147 dentry_operations|
5148 dev_pm_ops|
5149 dma_map_ops|
5150 extent_io_ops|
5151 file_lock_operations|
5152 file_operations|
5153 hv_ops|
5154 ide_dma_ops|
5155 intel_dvo_dev_ops|
5156 item_operations|
5157 iwl_ops|
5158 kgdb_arch|
5159 kgdb_io|
5160 kset_uevent_ops|
5161 lock_manager_operations|
5162 microcode_ops|
5163 mtrr_ops|
5164 neigh_ops|
5165 nlmsvc_binding|
5166 pci_raw_ops|
5167 pipe_buf_operations|
5168 platform_hibernation_ops|
5169 platform_suspend_ops|
5170 proto_ops|
5171 rpc_pipe_ops|
5172 seq_operations|
5173 snd_ac97_build_ops|
5174 soc_pcmcia_socket_ops|
5175 stacktrace_ops|
5176 sysfs_ops|
5177 tty_operations|
5178 usb_mon_operations|
5179 wd_ops}x;
5180 if ($line !~ /\bconst\b/ &&
5181 $line =~ /\bstruct\s+($struct_ops)\b/) {
5182 WARN("CONST_STRUCT",
5183 "struct $1 should normally be const\n" .
5184 $herecurr);
5185 }
5186
5187# use of NR_CPUS is usually wrong
5188# ignore definitions of NR_CPUS and usage to define arrays as likely right
5189 if ($line =~ /\bNR_CPUS\b/ &&
5190 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5191 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
5192 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5193 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5194 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
5195 {
5196 WARN("NR_CPUS",
5197 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
5198 }
5199
5200# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
5201 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
5202 ERROR("DEFINE_ARCH_HAS",
5203 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
5204 }
5205
5206# whine mightly about in_atomic
5207 if ($line =~ /\bin_atomic\s*\(/) {
5208 if ($realfile =~ m@^drivers/@) {
5209 ERROR("IN_ATOMIC",
5210 "do not use in_atomic in drivers\n" . $herecurr);
5211 } elsif ($realfile !~ m@^kernel/@) {
5212 WARN("IN_ATOMIC",
5213 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5214 }
5215 }
5216
5217# check for lockdep_set_novalidate_class
5218 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
5219 $line =~ /__lockdep_no_validate__\s*\)/ ) {
5220 if ($realfile !~ m@^kernel/lockdep@ &&
5221 $realfile !~ m@^include/linux/lockdep@ &&
5222 $realfile !~ m@^drivers/base/core@) {
5223 ERROR("LOCKDEP",
5224 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
5225 }
5226 }
5227
5228 if ($line =~ /debugfs_create_file.*S_IWUGO/ ||
5229 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) {
5230 WARN("EXPORTED_WORLD_WRITABLE",
5231 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
5232 }
5233
5234# Mode permission misuses where it seems decimal should be octal
5235# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5236 if ($^V && $^V ge 5.10.0 &&
5237 $line =~ /$mode_perms_search/) {
5238 foreach my $entry (@mode_permission_funcs) {
5239 my $func = $entry->[0];
5240 my $arg_pos = $entry->[1];
5241
5242 my $skip_args = "";
5243 if ($arg_pos > 1) {
5244 $arg_pos--;
5245 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
5246 }
5247 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5248 if ($line =~ /$test/) {
5249 my $val = $1;
5250 $val = $6 if ($skip_args ne "");
5251
5252 if ($val !~ /^0$/ &&
5253 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
5254 length($val) ne 4)) {
5255 ERROR("NON_OCTAL_PERMISSIONS",
5256 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5257 }
5258 }
5259 }
5260 }
5261 }
5262
5263 # If we have no input at all, then there is nothing to report on
5264 # so just keep quiet.
5265 if ($#rawlines == -1) {
5266 exit(0);
5267 }
5268
5269 # In mailback mode only produce a report in the negative, for
5270 # things that appear to be patches.
5271 if ($mailback && ($clean == 1 || !$is_patch)) {
5272 exit(0);
5273 }
5274
5275 # This is not a patch, and we are are in 'no-patch' mode so
5276 # just keep quiet.
5277 if (!$chk_patch && !$is_patch) {
5278 exit(0);
5279 }
5280
5281 if (!$is_patch) {
5282 ERROR("NOT_UNIFIED_DIFF",
5283 "Does not appear to be a unified-diff format patch\n");
5284 }
5285 if ($is_patch && $chk_signoff && $signoff == 0) {
5286 ERROR("MISSING_SIGN_OFF",
5287 "Missing Signed-off-by: line(s)\n");
5288 }
5289
5290 print report_dump();
5291 if ($summary && !($clean == 1 && $quiet == 1)) {
5292 print "$filename " if ($summary_file);
5293 print "total: $cnt_error errors, $cnt_warn warnings, " .
5294 (($check)? "$cnt_chk checks, " : "") .
5295 "$cnt_lines lines checked\n";
5296 print "\n" if ($quiet == 0);
5297 }
5298
5299 if ($quiet == 0) {
5300
5301 if ($^V lt 5.10.0) {
5302 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5303 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5304 }
5305
5306 # If there were whitespace errors which cleanpatch can fix
5307 # then suggest that.
5308 if ($rpt_cleaners) {
5309 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5310 print " scripts/cleanfile\n\n";
5311 $rpt_cleaners = 0;
5312 }
5313 }
5314
5315 hash_show_words(\%use_type, "Used");
5316 hash_show_words(\%ignore_type, "Ignored");
5317
5318 if ($clean == 0 && $fix &&
5319 ("@rawlines" ne "@fixed" ||
5320 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
5321 my $newfile = $filename;
5322 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
5323 my $linecount = 0;
5324 my $f;
5325
5326 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
5327
5328 open($f, '>', $newfile)
5329 or die "$P: Can't open $newfile for write\n";
5330 foreach my $fixed_line (@fixed) {
5331 $linecount++;
5332 if ($file) {
5333 if ($linecount > 3) {
5334 $fixed_line =~ s/^\+//;
5335 print $f $fixed_line . "\n";
5336 }
5337 } else {
5338 print $f $fixed_line . "\n";
5339 }
5340 }
5341 close($f);
5342
5343 if (!$quiet) {
5344 print << "EOM";
5345Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5346
5347Do _NOT_ trust the results written to this file.
5348Do _NOT_ submit these changes without inspecting them for correctness.
5349
5350This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5351No warranties, expressed or implied...
5352
5353EOM
5354 }
5355 }
5356
5357 if ($clean == 1 && $quiet == 0) {
5358 print "$vname has no obvious style problems and is ready for submission.\n"
5359 }
5360 if ($clean == 0 && $quiet == 0) {
5361 print << "EOM";
5362$vname has style problems, please review.
5363
5364If any of these errors are false positives, please report
5365them to the maintainer, see CHECKPATCH in MAINTAINERS.
5366EOM
5367 }
5368
5369 return $clean;
5370}