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