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