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