2 # SPDX-License-Identifier: GPL-2.0
4 # (c) 2001, Dave Jones. (the file handling bit)
5 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
6 # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
7 # (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
8 # (c) 2010-2018 Joe Perches <joe@perches.com>
15 use Term::ANSIColor qw(:constants);
16 use Encode qw(decode encode);
19 my $D = dirname(abs_path($P));
23 use Getopt::Long qw(:config no_auto_abbrev);
53 my $configuration_file = ".checkpatch.conf";
54 my $max_line_length = 100;
55 my $ignore_perl_version = 0;
56 my $minimum_perl_version = 5.10.0;
57 my $min_conf_desc_length = 4;
58 my $spelling_file = "$D/spelling.txt";
60 my $codespellfile = "/usr/share/codespell/dictionary.txt";
61 my $conststructsfile = "$D/const_structs.checkpatch";
62 my $typedefsfile = "";
64 my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
65 # git output parsing needs US English output, so first set backtick child process LANGUAGE
66 my $git_command ='export LANGUAGE=en_US.UTF-8; git';
73 Usage: $P [OPTION]... [FILE]...
78 --no-tree run without a kernel tree
79 --no-signoff do not check for 'Signed-off-by' line
80 --patch treat FILE as patchfile (default)
81 --emacs emacs compile window format
82 --terse one line per report
83 --showfile emit diffed file position, not input file position
84 -g, --git treat FILE as a single commit or git revision range
85 single git commit with:
89 multiple git commits with:
93 git merges are ignored
94 -f, --file treat FILE as regular source file
95 --subjective, --strict enable more subjective tests
96 --list-types list the possible message types
97 --types TYPE(,TYPE2...) show only these comma separated message types
98 --ignore TYPE(,TYPE2...) ignore various comma separated message types
99 --show-types show the specific message type in the output
100 --max-line-length=n set the maximum line length, (default $max_line_length)
101 if exceeded, warn on patches
102 requires --strict for use with --file
103 --min-conf-desc-length=n set the min description length, if shorter, warn
104 --tab-size=n set the number of spaces for tab (default $tabsize)
105 --root=PATH PATH to the kernel tree root
106 --no-summary suppress the per-file summary
107 --mailback only produce a report in case of warnings/errors
108 --summary-file include the filename in summary
109 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
110 'values', 'possible', 'type', and 'attr' (default
112 --test-only=WORD report only warnings/errors containing WORD
114 --fix EXPERIMENTAL - may create horrible results
115 If correctable single-line errors exist, create
116 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
117 with potential errors corrected to the preferred
119 --fix-inplace EXPERIMENTAL - may create horrible results
120 Is the same as --fix, but overwrites the input
121 file. It's your fault if there's no backup or git
122 --ignore-perl-version override checking of perl version. expect
124 --codespell Use the codespell dictionary for spelling/typos
125 (default:/usr/share/codespell/dictionary.txt)
126 --codespellfile Use this codespell dictionary
127 --typedefsfile Read additional types from this file
128 --color[=WHEN] Use colors 'always', 'never', or only when output
129 is a terminal ('auto'). Default is 'auto'.
130 -h, --help, --version display this help and exit
132 When FILE is - read standard input.
140 return grep { !$seen{$_}++ } @_;
150 open(my $script, '<', abs_path($P)) or
151 die "$P: Can't read '$P' $!\n";
153 my $text = <$script>;
157 # Also catch when type or level is passed through a variable
158 for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
161 @types = sort(uniq(@types));
162 print("#\tMessage type\n\n");
163 foreach my $type (@types) {
164 print(++$count . "\t" . $type . "\n");
170 my $conf = which_conf($configuration_file);
173 open(my $conffile, '<', "$conf")
174 or warn "$P: Can't find a readable $configuration_file file $!\n";
176 while (<$conffile>) {
179 $line =~ s/\s*\n?$//g;
183 next if ($line =~ m/^\s*#/);
184 next if ($line =~ m/^\s*$/);
186 my @words = split(" ", $line);
187 foreach my $word (@words) {
188 last if ($word =~ m/^#/);
189 push (@conf_args, $word);
193 unshift(@ARGV, @conf_args) if @conf_args;
196 # Perl's Getopt::Long allows options to take optional arguments after a space.
197 # Prevent --color by itself from consuming other arguments
199 if ($_ eq "--color" || $_ eq "-color") {
200 $_ = "--color=$color";
205 'q|quiet+' => \$quiet,
207 'signoff!' => \$chk_signoff,
208 'patch!' => \$chk_patch,
211 'showfile!' => \$showfile,
214 'subjective!' => \$check,
215 'strict!' => \$check,
216 'ignore=s' => \@ignore,
218 'show-types!' => \$show_types,
219 'list-types!' => \$list_types,
220 'max-line-length=i' => \$max_line_length,
221 'min-conf-desc-length=i' => \$min_conf_desc_length,
222 'tab-size=i' => \$tabsize,
224 'summary!' => \$summary,
225 'mailback!' => \$mailback,
226 'summary-file!' => \$summary_file,
228 'fix-inplace!' => \$fix_inplace,
229 'ignore-perl-version!' => \$ignore_perl_version,
230 'debug=s' => \%debug,
231 'test-only=s' => \$tst_only,
232 'codespell!' => \$codespell,
233 'codespellfile=s' => \$codespellfile,
234 'typedefsfile=s' => \$typedefsfile,
235 'color=s' => \$color,
236 'no-color' => \$color, #keep old behaviors of -nocolor
237 'nocolor' => \$color, #keep old behaviors of -nocolor
244 list_types(0) if ($list_types);
246 $fix = 1 if ($fix_inplace);
247 $check_orig = $check;
249 die "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
253 my $perl_version_ok = 1;
254 if ($^V && $^V lt $minimum_perl_version) {
255 $perl_version_ok = 0;
256 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
257 exit(1) if (!$ignore_perl_version);
260 #if no filenames are given, push '-' to read patch from stdin
265 if ($color =~ /^[01]$/) {
267 } elsif ($color =~ /^always$/i) {
269 } elsif ($color =~ /^never$/i) {
271 } elsif ($color =~ /^auto$/i) {
272 $color = (-t STDOUT);
274 die "$P: Invalid color mode: $color\n";
277 # skip TAB size 1 to avoid additional checks on $tabsize - 1
278 die "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
280 sub hash_save_array_words {
281 my ($hashRef, $arrayRef) = @_;
283 my @array = split(/,/, join(',', @$arrayRef));
284 foreach my $word (@array) {
285 $word =~ s/\s*\n?$//g;
288 $word =~ tr/[a-z]/[A-Z]/;
290 next if ($word =~ m/^\s*#/);
291 next if ($word =~ m/^\s*$/);
297 sub hash_show_words {
298 my ($hashRef, $prefix) = @_;
300 if (keys %$hashRef) {
301 print "\nNOTE: $prefix message types:";
302 foreach my $word (sort keys %$hashRef) {
309 hash_save_array_words(\%ignore_type, \@ignore);
310 hash_save_array_words(\%use_type, \@use);
313 my $dbg_possible = 0;
316 for my $key (keys %debug) {
318 eval "\${dbg_$key} = '$debug{$key}';";
322 my $rpt_cleaners = 0;
331 if (!top_of_kernel_tree($root)) {
332 die "$P: $root: --root does not point at a valid tree\n";
335 if (top_of_kernel_tree('.')) {
337 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
338 top_of_kernel_tree($1)) {
343 if (!defined $root) {
344 print "Must be run from the top-level dir. of a kernel tree\n";
349 my $emitted_corrupt = 0;
352 [A-Za-z_][A-Za-z\d_]*
353 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
355 our $Storage = qr{extern|static|asmlinkage};
369 our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
370 our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
371 our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
372 our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
373 our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
375 # Notes to $Attribute:
376 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
398 ____cacheline_aligned|
399 ____cacheline_aligned_in_smp|
400 ____cacheline_internodealigned_in_smp|
404 our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
405 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
406 our $Lval = qr{$Ident(?:$Member)*};
408 our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
409 our $Binary = qr{(?i)0b[01]+$Int_type?};
410 our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
411 our $Int = qr{[0-9]+$Int_type?};
412 our $Octal = qr{0[0-7]+$Int_type?};
413 our $String = qr{"[X\t]*"};
414 our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
415 our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
416 our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
417 our $Float = qr{$Float_hex|$Float_dec|$Float_int};
418 our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
419 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
420 our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
421 our $Arithmetic = qr{\+|-|\*|\/|%};
425 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
428 our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
432 our $NonptrTypeMisordered;
433 our $NonptrTypeWithAttr;
437 our $DeclareMisordered;
439 our $NON_ASCII_UTF8 = qr{
440 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
441 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
442 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
443 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
444 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
445 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
446 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
450 [\x09\x0A\x0D\x20-\x7E] # ASCII
454 our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
455 our $typeOtherOSTypedefs = qr{(?x:
456 u_(?:char|short|int|long) | # bsd
457 u(?:nchar|short|int|long) # sysv
459 our $typeKernelTypedefs = qr{(?x:
460 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
463 our $typeTypedefs = qr{(?x:
465 $typeOtherOSTypedefs\b|
466 $typeKernelTypedefs\b
469 our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
471 our $logFunctions = qr{(?x:
472 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
473 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
475 WARN(?:_RATELIMIT|_ONCE|)|
478 seq_vprintf|seq_printf|seq_puts
481 our $allocFunctions = qr{(?x:
483 (?:kv|k|v)[czm]alloc(?:_node|_array)? |
486 (?:\w+)?alloc_skb(?:_ip_align)? |
487 # dev_alloc_skb/netdev_alloc_skb, et al
491 our $signature_tags = qr{(?xi:
503 our @typeListMisordered = (
504 qr{char\s+(?:un)?signed},
505 qr{int\s+(?:(?:un)?signed\s+)?short\s},
506 qr{int\s+short(?:\s+(?:un)?signed)},
507 qr{short\s+int(?:\s+(?:un)?signed)},
508 qr{(?:un)?signed\s+int\s+short},
509 qr{short\s+(?:un)?signed},
510 qr{long\s+int\s+(?:un)?signed},
511 qr{int\s+long\s+(?:un)?signed},
512 qr{long\s+(?:un)?signed\s+int},
513 qr{int\s+(?:un)?signed\s+long},
514 qr{int\s+(?:un)?signed},
515 qr{int\s+long\s+long\s+(?:un)?signed},
516 qr{long\s+long\s+int\s+(?:un)?signed},
517 qr{long\s+long\s+(?:un)?signed\s+int},
518 qr{long\s+long\s+(?:un)?signed},
519 qr{long\s+(?:un)?signed},
524 qr{(?:(?:un)?signed\s+)?char},
525 qr{(?:(?:un)?signed\s+)?short\s+int},
526 qr{(?:(?:un)?signed\s+)?short},
527 qr{(?:(?:un)?signed\s+)?int},
528 qr{(?:(?:un)?signed\s+)?long\s+int},
529 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
530 qr{(?:(?:un)?signed\s+)?long\s+long},
531 qr{(?:(?:un)?signed\s+)?long},
540 qr{${Ident}_handler},
541 qr{${Ident}_handler_fn},
545 our $C90_int_types = qr{(?x:
546 long\s+long\s+int\s+(?:un)?signed|
547 long\s+long\s+(?:un)?signed\s+int|
548 long\s+long\s+(?:un)?signed|
549 (?:(?:un)?signed\s+)?long\s+long\s+int|
550 (?:(?:un)?signed\s+)?long\s+long|
551 int\s+long\s+long\s+(?:un)?signed|
552 int\s+(?:(?:un)?signed\s+)?long\s+long|
554 long\s+int\s+(?:un)?signed|
555 long\s+(?:un)?signed\s+int|
556 long\s+(?:un)?signed|
557 (?:(?:un)?signed\s+)?long\s+int|
558 (?:(?:un)?signed\s+)?long|
559 int\s+long\s+(?:un)?signed|
560 int\s+(?:(?:un)?signed\s+)?long|
563 (?:(?:un)?signed\s+)?int
566 our @typeListFile = ();
567 our @typeListWithAttr = (
569 qr{struct\s+$InitAttribute\s+$Ident},
570 qr{union\s+$InitAttribute\s+$Ident},
573 our @modifierList = (
576 our @modifierListFile = ();
578 our @mode_permission_funcs = (
580 ["module_param_(?:array|named|string)", 4],
581 ["module_param_array_named", 5],
582 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
583 ["proc_create(?:_data|)", 2],
584 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
585 ["IIO_DEV_ATTR_[A-Z_]+", 1],
586 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
587 ["SENSOR_TEMPLATE(?:_2|)", 3],
591 #Create a search pattern for all these functions to speed up a loop below
592 our $mode_perms_search = "";
593 foreach my $entry (@mode_permission_funcs) {
594 $mode_perms_search .= '|' if ($mode_perms_search ne "");
595 $mode_perms_search .= $entry->[0];
597 $mode_perms_search = "(?:${mode_perms_search})";
599 our %deprecated_apis = (
600 "synchronize_rcu_bh" => "synchronize_rcu",
601 "synchronize_rcu_bh_expedited" => "synchronize_rcu_expedited",
602 "call_rcu_bh" => "call_rcu",
603 "rcu_barrier_bh" => "rcu_barrier",
604 "synchronize_sched" => "synchronize_rcu",
605 "synchronize_sched_expedited" => "synchronize_rcu_expedited",
606 "call_rcu_sched" => "call_rcu",
607 "rcu_barrier_sched" => "rcu_barrier",
608 "get_state_synchronize_sched" => "get_state_synchronize_rcu",
609 "cond_synchronize_sched" => "cond_synchronize_rcu",
612 #Create a search pattern for all these strings to speed up a loop below
613 our $deprecated_apis_search = "";
614 foreach my $entry (keys %deprecated_apis) {
615 $deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
616 $deprecated_apis_search .= $entry;
618 $deprecated_apis_search = "(?:${deprecated_apis_search})";
620 our $mode_perms_world_writable = qr{
628 our %mode_permission_string_types = (
647 #Create a search pattern for all these strings to speed up a loop below
648 our $mode_perms_string_search = "";
649 foreach my $entry (keys %mode_permission_string_types) {
650 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
651 $mode_perms_string_search .= $entry;
653 our $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
654 our $multi_mode_perms_string_search = qr{
655 ${single_mode_perms_string_search}
656 (?:\s*\|\s*${single_mode_perms_string_search})*
662 return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
669 while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
670 $curpos = pos($string);
673 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
675 $to |= $mode_permission_string_types{$match};
676 $val .= '\s*\|\s*' if ($val ne "");
680 $oval =~ s/^\s*\|\s*//;
681 $oval =~ s/\s*\|\s*$//;
682 return sprintf("%04o", $to);
685 our $allowed_asm_includes = qr{(?x:
691 # memory.h: ARM has a custom one
693 # Load common spelling mistakes and build regular expression list.
697 if (open(my $spelling, '<', $spelling_file)) {
698 while (<$spelling>) {
701 $line =~ s/\s*\n?$//g;
704 next if ($line =~ m/^\s*#/);
705 next if ($line =~ m/^\s*$/);
707 my ($suspect, $fix) = split(/\|\|/, $line);
709 $spelling_fix{$suspect} = $fix;
713 warn "No typos will be found - file '$spelling_file': $!\n";
717 if (open(my $spelling, '<', $codespellfile)) {
718 while (<$spelling>) {
721 $line =~ s/\s*\n?$//g;
724 next if ($line =~ m/^\s*#/);
725 next if ($line =~ m/^\s*$/);
726 next if ($line =~ m/, disabled/i);
730 my ($suspect, $fix) = split(/->/, $line);
732 $spelling_fix{$suspect} = $fix;
736 warn "No codespell typos will be found - file '$codespellfile': $!\n";
740 $misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
743 my ($wordsRef, $file) = @_;
745 if (open(my $words, '<', $file)) {
749 $line =~ s/\s*\n?$//g;
752 next if ($line =~ m/^\s*#/);
753 next if ($line =~ m/^\s*$/);
755 print("$file: '$line' invalid - ignored\n");
759 $$wordsRef .= '|' if ($$wordsRef ne "");
769 my $const_structs = "";
770 read_words(\$const_structs, $conststructsfile)
771 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
773 my $typeOtherTypedefs = "";
774 if (length($typedefsfile)) {
775 read_words(\$typeOtherTypedefs, $typedefsfile)
776 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
778 $typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
781 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
782 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
783 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
784 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
785 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
791 (?:$Modifier\s+|const\s+)*
793 (?:typeof|__typeof__)\s*\([^\)]*\)|
797 (?:\s+$Modifier|\s+const)*
799 $NonptrTypeMisordered = qr{
800 (?:$Modifier\s+|const\s+)*
804 (?:\s+$Modifier|\s+const)*
806 $NonptrTypeWithAttr = qr{
807 (?:$Modifier\s+|const\s+)*
809 (?:typeof|__typeof__)\s*\([^\)]*\)|
813 (?:\s+$Modifier|\s+const)*
817 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
818 (?:\s+$Inline|\s+$Modifier)*
820 $TypeMisordered = qr{
821 $NonptrTypeMisordered
822 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
823 (?:\s+$Inline|\s+$Modifier)*
825 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
826 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
830 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
832 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
833 # requires at least perl version v5.10.0
834 # Any use must be runtime checked with $^V
836 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
837 our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
838 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
840 our $declaration_macros = qr{(?x:
841 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
842 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
843 (?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
848 return "" if (!defined($string));
850 while ($string =~ /^\s*\(.*\)\s*$/) {
851 $string =~ s@^\s*\(\s*@@;
852 $string =~ s@\s*\)\s*$@@;
855 $string =~ s@\s+@ @g;
860 sub seed_camelcase_file {
863 return if (!(-f $file));
867 open(my $include_file, '<', "$file")
868 or warn "$P: Can't read '$file' $!\n";
869 my $text = <$include_file>;
870 close($include_file);
872 my @lines = split('\n', $text);
874 foreach my $line (@lines) {
875 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
876 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
878 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
880 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
886 our %maintained_status = ();
888 sub is_maintained_obsolete {
891 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
893 if (!exists($maintained_status{$filename})) {
894 $maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
897 return $maintained_status{$filename} =~ /obsolete/i;
900 sub is_SPDX_License_valid {
903 return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
905 my $root_path = abs_path($root);
906 my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
907 return 0 if ($status ne "");
911 my $camelcase_seeded = 0;
912 sub seed_camelcase_includes {
913 return if ($camelcase_seeded);
916 my $camelcase_cache = "";
917 my @include_files = ();
919 $camelcase_seeded = 1;
922 my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
923 chomp $git_last_include_commit;
924 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
926 my $last_mod_date = 0;
927 $files = `find $root/include -name "*.h"`;
928 @include_files = split('\n', $files);
929 foreach my $file (@include_files) {
930 my $date = POSIX::strftime("%Y%m%d%H%M",
931 localtime((stat $file)[9]));
932 $last_mod_date = $date if ($last_mod_date < $date);
934 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
937 if ($camelcase_cache ne "" && -f $camelcase_cache) {
938 open(my $camelcase_file, '<', "$camelcase_cache")
939 or warn "$P: Can't read '$camelcase_cache' $!\n";
940 while (<$camelcase_file>) {
944 close($camelcase_file);
950 $files = `${git_command} ls-files "include/*.h"`;
951 @include_files = split('\n', $files);
954 foreach my $file (@include_files) {
955 seed_camelcase_file($file);
958 if ($camelcase_cache ne "") {
959 unlink glob ".checkpatch-camelcase.*";
960 open(my $camelcase_file, '>', "$camelcase_cache")
961 or warn "$P: Can't write '$camelcase_cache' $!\n";
962 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
963 print $camelcase_file ("$_\n");
965 close($camelcase_file);
969 sub git_commit_info {
970 my ($commit, $id, $desc) = @_;
972 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
974 my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
975 $output =~ s/^\s*//gm;
976 my @lines = split("\n", $output);
978 return ($id, $desc) if ($#lines < 0);
980 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
981 # Maybe one day convert this block of bash into something that returns
982 # all matching commit ids, but it's very slow...
984 # echo "checking commits $1..."
985 # git rev-list --remotes | grep -i "^$1" |
986 # while read line ; do
987 # git log --format='%H %s' -1 $line |
988 # echo "commit $(cut -c 1-12,41-)"
990 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
993 $id = substr($lines[0], 0, 12);
994 $desc = substr($lines[0], 41);
1000 $chk_signoff = 0 if ($file);
1005 my @fixed_inserted = ();
1006 my @fixed_deleted = ();
1009 # If input is git commits, extract all commits from the commit expressions.
1010 # For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
1011 die "$P: No git repository found\n" if ($git && !-e ".git");
1015 foreach my $commit_expr (@ARGV) {
1017 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1018 $git_range = "-$2 $1";
1019 } elsif ($commit_expr =~ m/\.\./) {
1020 $git_range = "$commit_expr";
1022 $git_range = "-1 $commit_expr";
1024 my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
1025 foreach my $line (split(/\n/, $lines)) {
1026 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1027 next if (!defined($1) || !defined($2));
1030 unshift(@commits, $sha1);
1031 $git_commits{$sha1} = $subject;
1034 die "$P: no git commits after extraction!\n" if (@commits == 0);
1039 $allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
1040 for my $filename (@ARGV) {
1043 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1044 die "$P: $filename: git format-patch failed - $!\n";
1046 open($FILE, '-|', "diff -u /dev/null $filename") ||
1047 die "$P: $filename: diff failed - $!\n";
1048 } elsif ($filename eq '-') {
1049 open($FILE, '<&STDIN');
1051 open($FILE, '<', "$filename") ||
1052 die "$P: $filename: open failed - $!\n";
1054 if ($filename eq '-') {
1055 $vname = 'Your patch';
1057 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1063 push(@rawlines, $_);
1064 $vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
1068 if ($#ARGV > 0 && $quiet == 0) {
1069 print '-' x length($vname) . "\n";
1071 print '-' x length($vname) . "\n";
1074 if (!process($filename)) {
1080 @fixed_inserted = ();
1081 @fixed_deleted = ();
1083 @modifierListFile = ();
1089 hash_show_words(\%use_type, "Used");
1090 hash_show_words(\%ignore_type, "Ignored");
1092 if (!$perl_version_ok) {
1095 NOTE: perl $^V is not modern enough to detect all possible issues.
1096 An upgrade to at least perl $minimum_perl_version is suggested.
1102 NOTE: If any of the errors are false positives, please report
1103 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1110 sub top_of_kernel_tree {
1114 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1115 "README", "Documentation", "arch", "include", "drivers",
1116 "fs", "init", "ipc", "kernel", "lib", "scripts",
1119 foreach my $check (@tree_check) {
1120 if (! -e $root . '/' . $check) {
1128 my ($formatted_email) = @_;
1131 my $name_comment = "";
1135 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1138 $comment = $3 if defined $3;
1139 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1141 $comment = $2 if defined $2;
1142 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1144 $comment = $2 if defined $2;
1145 $formatted_email =~ s/\Q$address\E.*$//;
1146 $name = $formatted_email;
1147 $name = trim($name);
1148 $name =~ s/^\"|\"$//g;
1149 # If there's a name left after stripping spaces and
1150 # leading quotes, and the address doesn't have both
1151 # leading and trailing angle brackets, the address
1153 # "joe smith joe@smith.com" bad
1154 # "joe smith <joe@smith.com" bad
1155 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1162 $name = trim($name);
1163 $name =~ s/^\"|\"$//g;
1164 $name =~ s/(\s*\([^\)]+\))\s*//;
1166 $name_comment = trim($1);
1168 $address = trim($address);
1169 $address =~ s/^\<|\>$//g;
1171 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1172 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1173 $name = "\"$name\"";
1176 return ($name, $name_comment, $address, $comment);
1180 my ($name, $address) = @_;
1182 my $formatted_email;
1184 $name = trim($name);
1185 $name =~ s/^\"|\"$//g;
1186 $address = trim($address);
1188 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1189 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1190 $name = "\"$name\"";
1193 if ("$name" eq "") {
1194 $formatted_email = "$address";
1196 $formatted_email = "$name <$address>";
1199 return $formatted_email;
1202 sub reformat_email {
1205 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
1206 return format_email($email_name, $email_address);
1209 sub same_email_addresses {
1210 my ($email1, $email2) = @_;
1212 my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1213 my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1215 return $email1_name eq $email2_name &&
1216 $email1_address eq $email2_address;
1222 foreach my $path (split(/:/, $ENV{PATH})) {
1223 if (-e "$path/$bin") {
1224 return "$path/$bin";
1234 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1235 if (-e "$path/$conf") {
1236 return "$path/$conf";
1248 for my $c (split(//, $str)) {
1252 for (; ($n % $tabsize) != 0; $n++) {
1264 (my $res = shift) =~ tr/\t/ /c;
1271 # Drop the diff line leader and expand tabs
1273 $line = expand_tabs($line);
1275 # Pick the indent from the front of the line.
1276 my ($white) = ($line =~ /^(\s*)/);
1278 return (length($line), length($white));
1281 my $sanitise_quote = '';
1283 sub sanitise_line_reset {
1284 my ($in_comment) = @_;
1287 $sanitise_quote = '*/';
1289 $sanitise_quote = '';
1302 # Always copy over the diff marker.
1303 $res = substr($line, 0, 1);
1305 for ($off = 1; $off < length($line); $off++) {
1306 $c = substr($line, $off, 1);
1308 # Comments we are whacking completely including the begin
1309 # and end, all to $;.
1310 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1311 $sanitise_quote = '*/';
1313 substr($res, $off, 2, "$;$;");
1317 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1318 $sanitise_quote = '';
1319 substr($res, $off, 2, "$;$;");
1323 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1324 $sanitise_quote = '//';
1326 substr($res, $off, 2, $sanitise_quote);
1331 # A \ in a string means ignore the next character.
1332 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1334 substr($res, $off, 2, 'XX');
1339 if ($c eq "'" || $c eq '"') {
1340 if ($sanitise_quote eq '') {
1341 $sanitise_quote = $c;
1343 substr($res, $off, 1, $c);
1345 } elsif ($sanitise_quote eq $c) {
1346 $sanitise_quote = '';
1350 #print "c<$c> SQ<$sanitise_quote>\n";
1351 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1352 substr($res, $off, 1, $;);
1353 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1354 substr($res, $off, 1, $;);
1355 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1356 substr($res, $off, 1, 'X');
1358 substr($res, $off, 1, $c);
1362 if ($sanitise_quote eq '//') {
1363 $sanitise_quote = '';
1366 # The pathname on a #include may be surrounded by '<' and '>'.
1367 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1368 my $clean = 'X' x length($1);
1369 $res =~ s@\<.*\>@<$clean>@;
1371 # The whole of a #error is a string.
1372 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1373 my $clean = 'X' x length($1);
1374 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1377 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1379 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1385 sub get_quoted_string {
1386 my ($line, $rawline) = @_;
1388 return "" if (!defined($line) || !defined($rawline));
1389 return "" if ($line !~ m/($String)/g);
1390 return substr($rawline, $-[0], $+[0] - $-[0]);
1393 sub ctx_statement_block {
1394 my ($linenr, $remain, $off) = @_;
1395 my $line = $linenr - 1;
1398 my $coff = $off - 1;
1412 @stack = (['', 0]) if ($#stack == -1);
1414 #warn "CSB: blk<$blk> remain<$remain>\n";
1415 # If we are about to drop off the end, pull in more
1418 for (; $remain > 0; $line++) {
1419 last if (!defined $lines[$line]);
1420 next if ($lines[$line] =~ /^-/);
1423 $blk .= $lines[$line] . "\n";
1424 $len = length($blk);
1428 # Bail if there is no further context.
1429 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1433 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1439 $c = substr($blk, $off, 1);
1440 $remainder = substr($blk, $off);
1442 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1444 # Handle nested #if/#else.
1445 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1446 push(@stack, [ $type, $level ]);
1447 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1448 ($type, $level) = @{$stack[$#stack - 1]};
1449 } elsif ($remainder =~ /^#\s*endif\b/) {
1450 ($type, $level) = @{pop(@stack)};
1453 # Statement ends at the ';' or a close '}' at the
1455 if ($level == 0 && $c eq ';') {
1459 # An else is really a conditional as long as its not else if
1460 if ($level == 0 && $coff_set == 0 &&
1461 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1462 $remainder =~ /^(else)(?:\s|{)/ &&
1463 $remainder !~ /^else\s+if\b/) {
1464 $coff = $off + length($1) - 1;
1466 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1467 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1470 if (($type eq '' || $type eq '(') && $c eq '(') {
1474 if ($type eq '(' && $c eq ')') {
1476 $type = ($level != 0)? '(' : '';
1478 if ($level == 0 && $coff < $soff) {
1481 #warn "CSB: mark coff<$coff>\n";
1484 if (($type eq '' || $type eq '{') && $c eq '{') {
1488 if ($type eq '{' && $c eq '}') {
1490 $type = ($level != 0)? '{' : '';
1493 if (substr($blk, $off + 1, 1) eq ';') {
1499 # Preprocessor commands end at the newline unless escaped.
1500 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1508 # We are truly at the end, so shuffle to the next line.
1515 my $statement = substr($blk, $soff, $off - $soff + 1);
1516 my $condition = substr($blk, $soff, $coff - $soff + 1);
1518 #warn "STATEMENT<$statement>\n";
1519 #warn "CONDITION<$condition>\n";
1521 #print "coff<$coff> soff<$off> loff<$loff>\n";
1523 return ($statement, $condition,
1524 $line, $remain + 1, $off - $loff + 1, $level);
1527 sub statement_lines {
1530 # Strip the diff line prefixes and rip blank lines at start and end.
1531 $stmt =~ s/(^|\n)./$1/g;
1535 my @stmt_lines = ($stmt =~ /\n/g);
1537 return $#stmt_lines + 2;
1540 sub statement_rawlines {
1543 my @stmt_lines = ($stmt =~ /\n/g);
1545 return $#stmt_lines + 2;
1548 sub statement_block_size {
1551 $stmt =~ s/(^|\n)./$1/g;
1557 my @stmt_lines = ($stmt =~ /\n/g);
1558 my @stmt_statements = ($stmt =~ /;/g);
1560 my $stmt_lines = $#stmt_lines + 2;
1561 my $stmt_statements = $#stmt_statements + 1;
1563 if ($stmt_lines > $stmt_statements) {
1566 return $stmt_statements;
1570 sub ctx_statement_full {
1571 my ($linenr, $remain, $off) = @_;
1572 my ($statement, $condition, $level);
1576 # Grab the first conditional/block pair.
1577 ($statement, $condition, $linenr, $remain, $off, $level) =
1578 ctx_statement_block($linenr, $remain, $off);
1579 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1580 push(@chunks, [ $condition, $statement ]);
1581 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1582 return ($level, $linenr, @chunks);
1585 # Pull in the following conditional/block pairs and see if they
1586 # could continue the statement.
1588 ($statement, $condition, $linenr, $remain, $off, $level) =
1589 ctx_statement_block($linenr, $remain, $off);
1590 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1591 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1593 push(@chunks, [ $condition, $statement ]);
1596 return ($level, $linenr, @chunks);
1600 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1602 my $start = $linenr - 1;
1609 my @stack = ($level);
1610 for ($line = $start; $remain > 0; $line++) {
1611 next if ($rawlines[$line] =~ /^-/);
1614 $blk .= $rawlines[$line];
1616 # Handle nested #if/#else.
1617 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1618 push(@stack, $level);
1619 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1620 $level = $stack[$#stack - 1];
1621 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1622 $level = pop(@stack);
1625 foreach my $c (split(//, $lines[$line])) {
1626 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1632 if ($c eq $close && $level > 0) {
1634 last if ($level == 0);
1635 } elsif ($c eq $open) {
1640 if (!$outer || $level <= 1) {
1641 push(@res, $rawlines[$line]);
1644 last if ($level == 0);
1647 return ($level, @res);
1649 sub ctx_block_outer {
1650 my ($linenr, $remain) = @_;
1652 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1656 my ($linenr, $remain) = @_;
1658 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1662 my ($linenr, $remain, $off) = @_;
1664 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1667 sub ctx_block_level {
1668 my ($linenr, $remain) = @_;
1670 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1672 sub ctx_statement_level {
1673 my ($linenr, $remain, $off) = @_;
1675 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1678 sub ctx_locate_comment {
1679 my ($first_line, $end_line) = @_;
1681 # If c99 comment on the current line, or the line before or after
1682 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1683 return $current_comment if (defined $current_comment);
1684 ($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1685 return $current_comment if (defined $current_comment);
1686 ($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1687 return $current_comment if (defined $current_comment);
1689 # Catch a comment on the end of the line itself.
1690 ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1691 return $current_comment if (defined $current_comment);
1693 # Look through the context and try and figure out if there is a
1696 $current_comment = '';
1697 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1698 my $line = $rawlines[$linenr - 1];
1700 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1703 if ($line =~ m@/\*@) {
1706 if (!$in_comment && $current_comment ne '') {
1707 $current_comment = '';
1709 $current_comment .= $line . "\n" if ($in_comment);
1710 if ($line =~ m@\*/@) {
1715 chomp($current_comment);
1716 return($current_comment);
1718 sub ctx_has_comment {
1719 my ($first_line, $end_line) = @_;
1720 my $cmt = ctx_locate_comment($first_line, $end_line);
1722 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1723 ##print "CMMT: $cmt\n";
1725 return ($cmt ne '');
1729 my ($linenr, $cnt) = @_;
1731 my $offset = $linenr - 1;
1736 $line = $rawlines[$offset++];
1737 next if (defined($line) && $line =~ /^-/);
1745 my ($linenr, $lc) = @_;
1747 my $stat_real = raw_line($linenr, 0);
1748 for (my $count = $linenr + 1; $count <= $lc; $count++) {
1749 $stat_real = $stat_real . "\n" . raw_line($count, 0);
1756 my ($linenr, $cnt, $here) = @_;
1758 my $herectx = $here . "\n";
1759 for (my $n = 0; $n < $cnt; $n++) {
1760 $herectx .= raw_line($linenr, $n) . "\n";
1771 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1774 $coded = sprintf("^%c", unpack('C', $2) + 64);
1783 my $av_preprocessor = 0;
1788 sub annotate_reset {
1789 $av_preprocessor = 0;
1791 @av_paren_type = ('E');
1792 $av_pend_colon = 'O';
1795 sub annotate_values {
1796 my ($stream, $type) = @_;
1799 my $var = '_' x length($stream);
1802 print "$stream\n" if ($dbg_values > 1);
1804 while (length($cur)) {
1805 @av_paren_type = ('E') if ($#av_paren_type < 0);
1806 print " <" . join('', @av_paren_type) .
1807 "> <$type> <$av_pending>" if ($dbg_values > 1);
1808 if ($cur =~ /^(\s+)/o) {
1809 print "WS($1)\n" if ($dbg_values > 1);
1810 if ($1 =~ /\n/ && $av_preprocessor) {
1811 $type = pop(@av_paren_type);
1812 $av_preprocessor = 0;
1815 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1816 print "CAST($1)\n" if ($dbg_values > 1);
1817 push(@av_paren_type, $type);
1820 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1821 print "DECLARE($1)\n" if ($dbg_values > 1);
1824 } elsif ($cur =~ /^($Modifier)\s*/) {
1825 print "MODIFIER($1)\n" if ($dbg_values > 1);
1828 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1829 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1830 $av_preprocessor = 1;
1831 push(@av_paren_type, $type);
1837 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1838 print "UNDEF($1)\n" if ($dbg_values > 1);
1839 $av_preprocessor = 1;
1840 push(@av_paren_type, $type);
1842 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1843 print "PRE_START($1)\n" if ($dbg_values > 1);
1844 $av_preprocessor = 1;
1846 push(@av_paren_type, $type);
1847 push(@av_paren_type, $type);
1850 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1851 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1852 $av_preprocessor = 1;
1854 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1858 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1859 print "PRE_END($1)\n" if ($dbg_values > 1);
1861 $av_preprocessor = 1;
1863 # Assume all arms of the conditional end as this
1864 # one does, and continue as if the #endif was not here.
1865 pop(@av_paren_type);
1866 push(@av_paren_type, $type);
1869 } elsif ($cur =~ /^(\\\n)/o) {
1870 print "PRECONT($1)\n" if ($dbg_values > 1);
1872 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1873 print "ATTR($1)\n" if ($dbg_values > 1);
1874 $av_pending = $type;
1877 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1878 print "SIZEOF($1)\n" if ($dbg_values > 1);
1884 } elsif ($cur =~ /^(if|while|for)\b/o) {
1885 print "COND($1)\n" if ($dbg_values > 1);
1889 } elsif ($cur =~/^(case)/o) {
1890 print "CASE($1)\n" if ($dbg_values > 1);
1891 $av_pend_colon = 'C';
1894 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1895 print "KEYWORD($1)\n" if ($dbg_values > 1);
1898 } elsif ($cur =~ /^(\()/o) {
1899 print "PAREN('$1')\n" if ($dbg_values > 1);
1900 push(@av_paren_type, $av_pending);
1904 } elsif ($cur =~ /^(\))/o) {
1905 my $new_type = pop(@av_paren_type);
1906 if ($new_type ne '_') {
1908 print "PAREN('$1') -> $type\n"
1909 if ($dbg_values > 1);
1911 print "PAREN('$1')\n" if ($dbg_values > 1);
1914 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1915 print "FUNC($1)\n" if ($dbg_values > 1);
1919 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1920 if (defined $2 && $type eq 'C' || $type eq 'T') {
1921 $av_pend_colon = 'B';
1922 } elsif ($type eq 'E') {
1923 $av_pend_colon = 'L';
1925 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1928 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1929 print "IDENT($1)\n" if ($dbg_values > 1);
1932 } elsif ($cur =~ /^($Assignment)/o) {
1933 print "ASSIGN($1)\n" if ($dbg_values > 1);
1936 } elsif ($cur =~/^(;|{|})/) {
1937 print "END($1)\n" if ($dbg_values > 1);
1939 $av_pend_colon = 'O';
1941 } elsif ($cur =~/^(,)/) {
1942 print "COMMA($1)\n" if ($dbg_values > 1);
1945 } elsif ($cur =~ /^(\?)/o) {
1946 print "QUESTION($1)\n" if ($dbg_values > 1);
1949 } elsif ($cur =~ /^(:)/o) {
1950 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1952 substr($var, length($res), 1, $av_pend_colon);
1953 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1958 $av_pend_colon = 'O';
1960 } elsif ($cur =~ /^(\[)/o) {
1961 print "CLOSE($1)\n" if ($dbg_values > 1);
1964 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1967 print "OPV($1)\n" if ($dbg_values > 1);
1974 substr($var, length($res), 1, $variant);
1977 } elsif ($cur =~ /^($Operators)/o) {
1978 print "OP($1)\n" if ($dbg_values > 1);
1979 if ($1 ne '++' && $1 ne '--') {
1983 } elsif ($cur =~ /(^.)/o) {
1984 print "C($1)\n" if ($dbg_values > 1);
1987 $cur = substr($cur, length($1));
1988 $res .= $type x length($1);
1992 return ($res, $var);
1996 my ($possible, $line) = @_;
1997 my $notPermitted = qr{(?:
2014 ^(?:typedef|struct|enum)\b
2016 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2017 if ($possible !~ $notPermitted) {
2018 # Check for modifiers.
2019 $possible =~ s/\s*$Storage\s*//g;
2020 $possible =~ s/\s*$Sparse\s*//g;
2021 if ($possible =~ /^\s*$/) {
2023 } elsif ($possible =~ /\s/) {
2024 $possible =~ s/\s*$Type\s*//g;
2025 for my $modifier (split(' ', $possible)) {
2026 if ($modifier !~ $notPermitted) {
2027 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
2028 push(@modifierListFile, $modifier);
2033 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
2034 push(@typeListFile, $possible);
2038 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
2047 $type =~ tr/[a-z]/[A-Z]/;
2049 return defined $use_type{$type} if (scalar keys %use_type > 0);
2051 return !defined $ignore_type{$type};
2055 my ($level, $type, $msg) = @_;
2057 if (!show_type($type) ||
2058 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2063 if ($level eq 'ERROR') {
2065 } elsif ($level eq 'WARNING') {
2071 $output .= $prefix . $level . ':';
2073 $output .= BLUE if ($color);
2074 $output .= "$type:";
2076 $output .= RESET if ($color);
2077 $output .= ' ' . $msg . "\n";
2080 my @lines = split("\n", $output, -1);
2081 splice(@lines, 1, 1);
2082 $output = join("\n", @lines);
2084 $output = (split('\n', $output))[0] . "\n" if ($terse);
2086 push(our @report, $output);
2095 sub fixup_current_range {
2096 my ($lineRef, $offset, $length) = @_;
2098 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2101 my $no = $o + $offset;
2102 my $nl = $l + $length;
2103 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2107 sub fix_inserted_deleted_lines {
2108 my ($linesRef, $insertedRef, $deletedRef) = @_;
2110 my $range_last_linenr = 0;
2111 my $delta_offset = 0;
2116 my $next_insert = 0;
2117 my $next_delete = 0;
2121 my $inserted = @{$insertedRef}[$next_insert++];
2122 my $deleted = @{$deletedRef}[$next_delete++];
2124 foreach my $old_line (@{$linesRef}) {
2126 my $line = $old_line; #don't modify the array
2127 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
2129 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
2130 $range_last_linenr = $new_linenr;
2131 fixup_current_range(\$line, $delta_offset, 0);
2134 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2135 $deleted = @{$deletedRef}[$next_delete++];
2137 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2140 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2141 push(@lines, ${$inserted}{'LINE'});
2142 $inserted = @{$insertedRef}[$next_insert++];
2144 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2148 push(@lines, $line);
2158 sub fix_insert_line {
2159 my ($linenr, $line) = @_;
2165 push(@fixed_inserted, $inserted);
2168 sub fix_delete_line {
2169 my ($linenr, $line) = @_;
2176 push(@fixed_deleted, $deleted);
2180 my ($type, $msg) = @_;
2182 if (report("ERROR", $type, $msg)) {
2190 my ($type, $msg) = @_;
2192 if (report("WARNING", $type, $msg)) {
2200 my ($type, $msg) = @_;
2202 if ($check && report("CHECK", $type, $msg)) {
2210 sub check_absolute_file {
2211 my ($absolute, $herecurr) = @_;
2212 my $file = $absolute;
2214 ##print "absolute<$absolute>\n";
2216 # See if any suffix of this path is a path within the tree.
2217 while ($file =~ s@^[^/]*/@@) {
2218 if (-f "$root/$file") {
2219 ##print "file<$file>\n";
2227 # It is, so see if the prefix is acceptable.
2228 my $prefix = $absolute;
2229 substr($prefix, -length($file)) = '';
2231 ##print "prefix<$prefix>\n";
2232 if ($prefix ne ".../") {
2233 WARN("USE_RELATIVE_PATH",
2234 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2241 $string =~ s/^\s+|\s+$//g;
2249 $string =~ s/^\s+//;
2257 $string =~ s/\s+$//;
2262 sub string_find_replace {
2263 my ($string, $find, $replace) = @_;
2265 $string =~ s/$find/$replace/g;
2273 my $source_indent = $tabsize;
2274 my $max_spaces_before_tab = $source_indent - 1;
2275 my $spaces_to_tab = " " x $source_indent;
2277 #convert leading spaces to tabs
2278 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2279 #Remove spaces before a tab
2280 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2285 sub pos_last_openparen {
2290 my $opens = $line =~ tr/\(/\(/;
2291 my $closes = $line =~ tr/\)/\)/;
2293 my $last_openparen = 0;
2295 if (($opens == 0) || ($closes >= $opens)) {
2299 my $len = length($line);
2301 for ($pos = 0; $pos < $len; $pos++) {
2302 my $string = substr($line, $pos);
2303 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2304 $pos += length($1) - 1;
2305 } elsif (substr($line, $pos, 1) eq '(') {
2306 $last_openparen = $pos;
2307 } elsif (index($string, '(') == -1) {
2312 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2315 sub get_raw_comment {
2316 my ($line, $rawline) = @_;
2319 for my $i (0 .. (length($line) - 1)) {
2320 if (substr($line, $i, 1) eq "$;") {
2321 $comment .= substr($rawline, $i, 1);
2329 my $filename = shift;
2335 my $stashrawline="";
2345 my $authorsignoff = 0;
2347 my $is_binding_patch = -1;
2348 my $in_header_lines = $file ? 0 : 1;
2349 my $in_commit_log = 0; #Scanning lines before patch
2350 my $has_patch_separator = 0; #Found a --- line
2351 my $has_commit_log = 0; #Encountered lines before patch
2352 my $commit_log_lines = 0; #Number of commit log lines
2353 my $commit_log_possible_stack_dump = 0;
2354 my $commit_log_long_line = 0;
2355 my $commit_log_has_diff = 0;
2356 my $reported_maintainer_file = 0;
2357 my $non_utf8_charset = 0;
2359 my $last_blank_line = 0;
2360 my $last_coalesced_string_linenr = -1;
2368 # Trace the real file/line as we go.
2373 my $context_function; #undef'd unless there's a known function
2375 my $comment_edge = 0;
2379 my $prev_values = 'E';
2382 my %suppress_ifbraces;
2383 my %suppress_whiletrailers;
2384 my %suppress_export;
2385 my $suppress_statement = 0;
2387 my %signatures = ();
2389 # Pre-scan the patch sanitizing the lines.
2390 # Pre-scan the patch looking for any __setup documentation.
2392 my @setup_docs = ();
2395 my $camelcase_file_seeded = 0;
2397 my $checklicenseline = 1;
2399 sanitise_line_reset();
2401 foreach my $rawline (@rawlines) {
2405 push(@fixed, $rawline) if ($fix);
2407 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2409 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
2414 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2423 # Guestimate if this is a continuing comment. Run
2424 # the context looking for a comment "edge". If this
2425 # edge is a close comment then we must be in a comment
2429 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2430 next if (defined $rawlines[$ln - 1] &&
2431 $rawlines[$ln - 1] =~ /^-/);
2433 #print "RAW<$rawlines[$ln - 1]>\n";
2434 last if (!defined $rawlines[$ln - 1]);
2435 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2436 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2441 if (defined $edge && $edge eq '*/') {
2445 # Guestimate if this is a continuing comment. If this
2446 # is the start of a diff block and this line starts
2447 # ' *' then it is very likely a comment.
2448 if (!defined $edge &&
2449 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2454 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2455 sanitise_line_reset($in_comment);
2457 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2458 # Standardise the strings and chars within the input to
2459 # simplify matching -- only bother with positive lines.
2460 $line = sanitise_line($rawline);
2462 push(@lines, $line);
2465 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2470 #print "==>$rawline\n";
2471 #print "-->$line\n";
2473 if ($setup_docs && $line =~ /^\+/) {
2474 push(@setup_docs, $line);
2483 foreach my $line (@lines) {
2486 my $sline = $line; #copy of $line
2487 $sline =~ s/$;/ /g; #with comments as spaces
2489 my $rawline = $rawlines[$linenr - 1];
2490 my $raw_comment = get_raw_comment($line, $rawline);
2492 # check if it's a mode change, rename or start of a patch
2493 if (!$in_commit_log &&
2494 ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2495 ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2496 $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2500 #extract the line range in the file after the patch is applied
2501 if (!$in_commit_log &&
2502 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2505 $first_line = $linenr + 1;
2515 %suppress_ifbraces = ();
2516 %suppress_whiletrailers = ();
2517 %suppress_export = ();
2518 $suppress_statement = 0;
2519 if ($context =~ /\b(\w+)\s*\(/) {
2520 $context_function = $1;
2522 undef $context_function;
2526 # track the line number as we move through the hunk, note that
2527 # new versions of GNU diff omit the leading space on completely
2528 # blank context lines so we need to count that too.
2529 } elsif ($line =~ /^( |\+|$)/) {
2531 $realcnt-- if ($realcnt != 0);
2533 # Measure the line length and indent.
2534 ($length, $indent) = line_stats($rawline);
2536 # Track the previous line.
2537 ($prevline, $stashline) = ($stashline, $line);
2538 ($previndent, $stashindent) = ($stashindent, $indent);
2539 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2541 #warn "line<$line>\n";
2543 } elsif ($realcnt == 1) {
2547 my $hunk_line = ($realcnt != 0);
2549 $here = "#$linenr: " if (!$file);
2550 $here = "#$realline: " if ($file);
2553 # extract the filename as it passes
2554 if ($line =~ /^diff --git.*?(\S+)$/) {
2556 $realfile =~ s@^([^/]*)/@@ if (!$file);
2559 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2561 $realfile =~ s@^([^/]*)/@@ if (!$file);
2565 if (!$file && $tree && $p1_prefix ne '' &&
2566 -e "$root/$p1_prefix") {
2567 WARN("PATCH_PREFIX",
2568 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2571 if ($realfile =~ m@^include/asm/@) {
2572 ERROR("MODIFIED_INCLUDE_ASM",
2573 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2578 #make up the handle for any error we report on this line
2580 $prefix = "$realfile:$realline: "
2583 $prefix = "$filename:$realline: ";
2585 $prefix = "$filename:$linenr: ";
2590 if (is_maintained_obsolete($realfile)) {
2592 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2594 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
2597 $check = $check_orig;
2599 $checklicenseline = 1;
2601 if ($realfile !~ /^MAINTAINERS/) {
2602 my $last_binding_patch = $is_binding_patch;
2604 $is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2606 if (($last_binding_patch != -1) &&
2607 ($last_binding_patch ^ $is_binding_patch)) {
2608 WARN("DT_SPLIT_BINDING_PATCH",
2609 "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
2616 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2618 my $hereline = "$here\n$rawline\n";
2619 my $herecurr = "$here\n$rawline\n";
2620 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2622 $cnt_lines++ if ($realcnt != 0);
2624 # Verify the existence of a commit log if appropriate
2625 # 2 is used because a $signature is counted in $commit_log_lines
2626 if ($in_commit_log) {
2627 if ($line !~ /^\s*$/) {
2628 $commit_log_lines++; #could be a $signature
2630 } elsif ($has_commit_log && $commit_log_lines < 2) {
2631 WARN("COMMIT_MESSAGE",
2632 "Missing commit description - Add an appropriate one\n");
2633 $commit_log_lines = 2; #warn only once
2636 # Check if the commit log has what seems like a diff which can confuse patch
2637 if ($in_commit_log && !$commit_log_has_diff &&
2638 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2639 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2640 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2641 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2642 ERROR("DIFF_IN_COMMIT_MSG",
2643 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2644 $commit_log_has_diff = 1;
2647 # Check for incorrect file permissions
2648 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2649 my $permhere = $here . "FILE: $realfile\n";
2650 if ($realfile !~ m@scripts/@ &&
2651 $realfile !~ /\.(py|pl|awk|sh)$/) {
2652 ERROR("EXECUTE_PERMISSIONS",
2653 "do not set execute permissions for source files\n" . $permhere);
2657 # Check the patch for a From:
2658 if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2660 $author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2662 $author = reformat_email($author);
2665 # Check the patch for a signoff:
2666 if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
2669 if ($author ne '') {
2670 if (same_email_addresses($1, $author)) {
2676 # Check for patch separator
2677 if ($line =~ /^---$/) {
2678 $has_patch_separator = 1;
2682 # Check if MAINTAINERS is being updated. If so, there's probably no need to
2683 # emit the "does MAINTAINERS need updating?" message on file add/move/delete
2684 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2685 $reported_maintainer_file = 1;
2688 # Check signature styles
2689 if (!$in_header_lines &&
2690 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2691 my $space_before = $1;
2693 my $space_after = $3;
2695 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2697 if ($sign_off !~ /$signature_tags/) {
2698 WARN("BAD_SIGN_OFF",
2699 "Non-standard signature: $sign_off\n" . $herecurr);
2701 if (defined $space_before && $space_before ne "") {
2702 if (WARN("BAD_SIGN_OFF",
2703 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2705 $fixed[$fixlinenr] =
2706 "$ucfirst_sign_off $email";
2709 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2710 if (WARN("BAD_SIGN_OFF",
2711 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2713 $fixed[$fixlinenr] =
2714 "$ucfirst_sign_off $email";
2718 if (!defined $space_after || $space_after ne " ") {
2719 if (WARN("BAD_SIGN_OFF",
2720 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2722 $fixed[$fixlinenr] =
2723 "$ucfirst_sign_off $email";
2727 my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
2728 my $suggested_email = format_email(($email_name, $email_address));
2729 if ($suggested_email eq "") {
2730 ERROR("BAD_SIGN_OFF",
2731 "Unrecognized email address: '$email'\n" . $herecurr);
2733 my $dequoted = $suggested_email;
2734 $dequoted =~ s/^"//;
2735 $dequoted =~ s/" </ </;
2736 # Don't force email to have quotes
2737 # Allow just an angle bracketed address
2738 if (!same_email_addresses($email, $suggested_email)) {
2739 WARN("BAD_SIGN_OFF",
2740 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2744 # Check for duplicate signatures
2745 my $sig_nospace = $line;
2746 $sig_nospace =~ s/\s//g;
2747 $sig_nospace = lc($sig_nospace);
2748 if (defined $signatures{$sig_nospace}) {
2749 WARN("BAD_SIGN_OFF",
2750 "Duplicate signature\n" . $herecurr);
2752 $signatures{$sig_nospace} = 1;
2755 # Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
2756 if ($sign_off =~ /^co-developed-by:$/i) {
2757 if ($email eq $author) {
2758 WARN("BAD_SIGN_OFF",
2759 "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
2761 if (!defined $lines[$linenr]) {
2762 WARN("BAD_SIGN_OFF",
2763 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
2764 } elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
2765 WARN("BAD_SIGN_OFF",
2766 "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2767 } elsif ($1 ne $email) {
2768 WARN("BAD_SIGN_OFF",
2769 "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
2774 # Check email subject for common tools that don't need to be mentioned
2775 if ($in_header_lines &&
2776 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2777 WARN("EMAIL_SUBJECT",
2778 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2781 # Check for Gerrit Change-Ids not in any patch context
2782 if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
2783 ERROR("GERRIT_CHANGE_ID",
2784 "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
2787 # Check if the commit log is in a possible stack dump
2788 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2789 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2790 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2792 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2793 $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2794 $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2795 # stack dump address styles
2796 $commit_log_possible_stack_dump = 1;
2799 # Check for line lengths > 75 in commit log, warn once
2800 if ($in_commit_log && !$commit_log_long_line &&
2801 length($line) > 75 &&
2802 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2803 # file delta changes
2804 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2806 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2807 # A Fixes: or Link: line
2808 $commit_log_possible_stack_dump)) {
2809 WARN("COMMIT_LOG_LONG_LINE",
2810 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2811 $commit_log_long_line = 1;
2814 # Reset possible stack dump if a blank line is found
2815 if ($in_commit_log && $commit_log_possible_stack_dump &&
2817 $commit_log_possible_stack_dump = 0;
2820 # Check for git id commit length and improperly formed commit descriptions
2821 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2822 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
2823 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2824 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2825 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2826 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2827 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2828 my $init_char = "c";
2829 my $orig_commit = "";
2836 my $id = '0123456789ab';
2837 my $orig_desc = "commit description";
2838 my $description = "";
2840 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2842 $orig_commit = lc($2);
2843 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2844 $orig_commit = lc($1);
2847 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2848 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2849 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2850 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2851 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2854 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2855 defined $rawlines[$linenr] &&
2856 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2859 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2860 defined $rawlines[$linenr] &&
2861 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2862 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2864 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2865 $orig_desc .= " " . $1;
2869 ($id, $description) = git_commit_info($orig_commit,
2873 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2874 ERROR("GIT_COMMIT_ID",
2875 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2879 # Check for added, moved or deleted files
2880 if (!$reported_maintainer_file && !$in_commit_log &&
2881 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2882 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2883 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2884 (defined($1) || defined($2))))) {
2886 $reported_maintainer_file = 1;
2887 WARN("FILE_PATH_CHANGES",
2888 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2891 # Check for adding new DT bindings not in schema format
2892 if (!$in_commit_log &&
2893 ($line =~ /^new file mode\s*\d+\s*$/) &&
2894 ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2895 WARN("DT_SCHEMA_BINDING_PATCH",
2896 "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2899 # Check for wrappage within a valid hunk of the file
2900 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2901 ERROR("CORRUPTED_PATCH",
2902 "patch seems to be corrupt (line wrapped?)\n" .
2903 $herecurr) if (!$emitted_corrupt++);
2906 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2907 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2908 $rawline !~ m/^$UTF8*$/) {
2909 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2911 my $blank = copy_spacing($rawline);
2912 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2913 my $hereptr = "$hereline$ptr\n";
2916 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2919 # Check if it's the start of a commit log
2920 # (not a header line and we haven't seen the patch filename)
2921 if ($in_header_lines && $realfile =~ /^$/ &&
2922 !($rawline =~ /^\s+(?:\S|$)/ ||
2923 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
2924 $in_header_lines = 0;
2926 $has_commit_log = 1;
2929 # Check if there is UTF-8 in a commit log when a mail header has explicitly
2930 # declined it, i.e defined some charset where it is missing.
2931 if ($in_header_lines &&
2932 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2934 $non_utf8_charset = 1;
2937 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2938 $rawline =~ /$NON_ASCII_UTF8/) {
2939 WARN("UTF8_BEFORE_PATCH",
2940 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2943 # Check for absolute kernel paths in commit message
2944 if ($tree && $in_commit_log) {
2945 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2948 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2949 check_absolute_file($1, $herecurr)) {
2952 check_absolute_file($file, $herecurr);
2957 # Check for various typo / spelling mistakes
2958 if (defined($misspellings) &&
2959 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2960 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
2962 my $typo_fix = $spelling_fix{lc($typo)};
2963 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2964 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2965 my $msg_level = \&WARN;
2966 $msg_level = \&CHK if ($file);
2967 if (&{$msg_level}("TYPO_SPELLING",
2968 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2970 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2975 # check for invalid commit id
2976 if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
2979 ($id, $description) = git_commit_info($2, undef, undef);
2980 if (!defined($id)) {
2981 WARN("UNKNOWN_COMMIT_ID",
2982 "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
2986 # ignore non-hunk lines and lines being removed
2987 next if (!$hunk_line || $line =~ /^-/);
2989 #trailing whitespace
2990 if ($line =~ /^\+.*\015/) {
2991 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2992 if (ERROR("DOS_LINE_ENDINGS",
2993 "DOS line endings\n" . $herevet) &&
2995 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
2997 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2998 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2999 if (ERROR("TRAILING_WHITESPACE",
3000 "trailing whitespace\n" . $herevet) &&
3002 $fixed[$fixlinenr] =~ s/\s+$//;
3008 # Check for FSF mailing addresses.
3009 if ($rawline =~ /\bwrite to the Free/i ||
3010 $rawline =~ /\b675\s+Mass\s+Ave/i ||
3011 $rawline =~ /\b59\s+Temple\s+Pl/i ||
3012 $rawline =~ /\b51\s+Franklin\s+St/i) {
3013 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3014 my $msg_level = \&ERROR;
3015 $msg_level = \&CHK if ($file);
3016 &{$msg_level}("FSF_MAILING_ADDRESS",
3017 "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)
3020 # check for Kconfig help text having a real description
3021 # Only applies when adding the entry originally, after that we do not have
3022 # sufficient context to determine whether it is indeed long enough.
3023 if ($realfile =~ /Kconfig/ &&
3024 # 'choice' is usually the last thing on the line (though
3025 # Kconfig supports named choices), so use a word boundary
3026 # (\b) rather than a whitespace character (\s)
3027 $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
3030 my $ln = $linenr + 1;
3034 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
3035 $f = $lines[$ln - 1];
3036 $cnt-- if ($lines[$ln - 1] !~ /^-/);
3037 $is_end = $lines[$ln - 1] =~ /^\+/;
3039 next if ($f =~ /^-/);
3040 last if (!$file && $f =~ /^\@\@/);
3042 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3044 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
3045 if ($lines[$ln - 1] =~ "---help---") {
3046 WARN("CONFIG_DESCRIPTION",
3047 "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
3055 next if ($f =~ /^$/);
3057 # This only checks context lines in the patch
3058 # and so hopefully shouldn't trigger false
3059 # positives, even though some of these are
3060 # common words in help texts
3061 if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3062 if|endif|menu|endmenu|source)\b/x) {
3068 if ($is_start && $is_end && $length < $min_conf_desc_length) {
3069 WARN("CONFIG_DESCRIPTION",
3070 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3072 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
3075 # check MAINTAINERS entries
3076 if ($realfile =~ /^MAINTAINERS$/) {
3077 # check MAINTAINERS entries for the right form
3078 if ($rawline =~ /^\+[A-Z]:/ &&
3079 $rawline !~ /^\+[A-Z]:\t\S/) {
3080 if (WARN("MAINTAINERS_STYLE",
3081 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3083 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3086 # check MAINTAINERS entries for the right ordering too
3087 my $preferred_order = 'MRLSWQBCPTFXNK';
3088 if ($rawline =~ /^\+[A-Z]:/ &&
3089 $prevrawline =~ /^[\+ ][A-Z]:/) {
3090 $rawline =~ /^\+([A-Z]):\s*(.*)/;
3093 $prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3096 my $curindex = index($preferred_order, $cur);
3097 my $previndex = index($preferred_order, $prev);
3098 if ($curindex < 0) {
3099 WARN("MAINTAINERS_STYLE",
3100 "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3102 if ($previndex >= 0 && $curindex < $previndex) {
3103 WARN("MAINTAINERS_STYLE",
3104 "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3105 } elsif ((($prev eq 'F' && $cur eq 'F') ||
3106 ($prev eq 'X' && $cur eq 'X')) &&
3107 ($prevval cmp $curval) > 0) {
3108 WARN("MAINTAINERS_STYLE",
3109 "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3115 # discourage the use of boolean for type definition attributes of Kconfig options
3116 if ($realfile =~ /Kconfig/ &&
3117 $line =~ /^\+\s*\bboolean\b/) {
3118 WARN("CONFIG_TYPE_BOOLEAN",
3119 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3122 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3123 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3126 'EXTRA_AFLAGS' => 'asflags-y',
3127 'EXTRA_CFLAGS' => 'ccflags-y',
3128 'EXTRA_CPPFLAGS' => 'cppflags-y',
3129 'EXTRA_LDFLAGS' => 'ldflags-y',
3132 WARN("DEPRECATED_VARIABLE",
3133 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3136 # check for DT compatible documentation
3137 if (defined $root &&
3138 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3139 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3141 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3143 my $dt_path = $root . "/Documentation/devicetree/bindings/";
3144 my $vp_file = $dt_path . "vendor-prefixes.yaml";
3146 foreach my $compat (@compats) {
3147 my $compat2 = $compat;
3148 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3149 my $compat3 = $compat;
3150 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3151 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3153 WARN("UNDOCUMENTED_DT_STRING",
3154 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3157 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3159 `grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3161 WARN("UNDOCUMENTED_DT_STRING",
3162 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3167 # check for using SPDX license tag at beginning of files
3168 if ($realline == $checklicenseline) {
3169 if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3170 $checklicenseline = 2;
3171 } elsif ($rawline =~ /^\+/) {
3173 if ($realfile =~ /\.(h|s|S)$/) {
3175 } elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3177 } elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
3179 } elsif ($realfile =~ /\.rst$/) {
3183 # check SPDX comment style for .[chsS] files
3184 if ($realfile =~ /\.[chsS]$/ &&
3185 $rawline =~ /SPDX-License-Identifier:/ &&
3186 $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3187 WARN("SPDX_LICENSE_TAG",
3188 "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3191 if ($comment !~ /^$/ &&
3192 $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3193 WARN("SPDX_LICENSE_TAG",
3194 "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
3195 } elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
3196 my $spdx_license = $1;
3197 if (!is_SPDX_License_valid($spdx_license)) {
3198 WARN("SPDX_LICENSE_TAG",
3199 "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3201 if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3202 not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3203 my $msg_level = \&WARN;
3204 $msg_level = \&CHK if ($file);
3205 if (&{$msg_level}("SPDX_LICENSE_TAG",
3207 "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3209 $fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3216 # check we are in a valid source file if not then ignore this hunk
3217 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
3219 # check for using SPDX-License-Identifier on the wrong line number
3220 if ($realline != $checklicenseline &&
3221 $rawline =~ /\bSPDX-License-Identifier:/ &&
3222 substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3223 WARN("SPDX_LICENSE_TAG",
3224 "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3227 # line length limit (with some exclusions)
3229 # There are a few types of lines that may extend beyond $max_line_length:
3230 # logging functions like pr_info that end in a string
3231 # lines with a single string
3232 # #defines that are a single string
3233 # lines with an RFC3986 like URL
3235 # There are 3 different line length message types:
3236 # LONG_LINE_COMMENT a comment starts before but extends beyond $max_line_length
3237 # LONG_LINE_STRING a string starts before but extends beyond $max_line_length
3238 # LONG_LINE all other lines longer than $max_line_length
3240 # if LONG_LINE is ignored, the other 2 types are also ignored
3243 if ($line =~ /^\+/ && $length > $max_line_length) {
3244 my $msg_type = "LONG_LINE";
3246 # Check the allowed long line types first
3248 # logging functions that end in a string that starts
3249 # before $max_line_length
3250 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3251 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3254 # lines with only strings (w/ possible termination)
3255 # #defines with only strings
3256 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3257 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3260 # More special cases
3261 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3262 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3265 # URL ($rawline is used in case the URL is in a comment)
3266 } elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3269 # Otherwise set the alternate message types
3271 # a comment starts before $max_line_length
3272 } elsif ($line =~ /($;[\s$;]*)$/ &&
3273 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3274 $msg_type = "LONG_LINE_COMMENT"
3276 # a quoted string starts before $max_line_length
3277 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3278 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3279 $msg_type = "LONG_LINE_STRING"
3282 if ($msg_type ne "" &&
3283 (show_type("LONG_LINE") || show_type($msg_type))) {
3284 my $msg_level = \&WARN;
3285 $msg_level = \&CHK if ($file);
3286 &{$msg_level}($msg_type,
3287 "line length of $length exceeds $max_line_length columns\n" . $herecurr);
3291 # check for adding lines without a newline.
3292 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3293 WARN("MISSING_EOF_NEWLINE",
3294 "adding a line without newline at end of file\n" . $herecurr);
3297 # check we are in a valid source file C or perl if not then ignore this hunk
3298 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
3300 # at the beginning of a line any tabs must come first and anything
3301 # more than $tabsize must use tabs.
3302 if ($rawline =~ /^\+\s* \t\s*\S/ ||
3303 $rawline =~ /^\+\s* \s*/) {
3304 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3306 if (ERROR("CODE_INDENT",
3307 "code indent should use tabs where possible\n" . $herevet) &&
3309 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3313 # check for space before tabs.
3314 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3315 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3316 if (WARN("SPACE_BEFORE_TAB",
3317 "please, no space before tabs\n" . $herevet) &&
3319 while ($fixed[$fixlinenr] =~
3320 s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3321 while ($fixed[$fixlinenr] =~
3322 s/(^\+.*) +\t/$1\t/) {}
3326 # check for assignments on the start of a line
3327 if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3328 CHK("ASSIGNMENT_CONTINUATIONS",
3329 "Assignment operator '$1' should be on the previous line\n" . $hereprev);
3332 # check for && or || at the start of a line
3333 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3334 CHK("LOGICAL_CONTINUATIONS",
3335 "Logical continuations should be on the previous line\n" . $hereprev);
3338 # check indentation starts on a tab stop
3339 if ($perl_version_ok &&
3340 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3341 my $indent = length($1);
3342 if ($indent % $tabsize) {
3344 "Statements should start on a tabstop\n" . $herecurr) &&
3346 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
3351 # check multi-line statement indentation matches previous line
3352 if ($perl_version_ok &&
3353 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3354 $prevline =~ /^\+(\t*)(.*)$/;
3358 my $pos = pos_last_openparen($rest);
3360 $line =~ /^(\+| )([ \t]*)/;
3363 my $goodtabindent = $oldindent .
3364 "\t" x ($pos / $tabsize) .
3365 " " x ($pos % $tabsize);
3366 my $goodspaceindent = $oldindent . " " x $pos;
3368 if ($newindent ne $goodtabindent &&
3369 $newindent ne $goodspaceindent) {
3371 if (CHK("PARENTHESIS_ALIGNMENT",
3372 "Alignment should match open parenthesis\n" . $hereprev) &&
3373 $fix && $line =~ /^\+/) {
3374 $fixed[$fixlinenr] =~
3375 s/^\+[ \t]*/\+$goodtabindent/;
3381 # check for space after cast like "(int) foo" or "(struct foo) bar"
3382 # avoid checking a few false positives:
3383 # "sizeof(<type>)" or "__alignof__(<type>)"
3384 # function pointer declarations like "(*foo)(int) = bar;"
3385 # structure definitions like "(struct foo) { 0 };"
3386 # multiline macros that define functions
3387 # known attributes or the __attribute__ keyword
3388 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3389 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
3391 "No space is necessary after a cast\n" . $herecurr) &&
3393 $fixed[$fixlinenr] =~
3394 s/(\(\s*$Type\s*\))[ \t]+/$1/;
3398 # Block comment styles
3399 # Networking with an initial /*
3400 if ($realfile =~ m@^(drivers/net/|net/)@ &&
3401 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
3402 $rawline =~ /^\+[ \t]*\*/ &&
3404 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3405 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3408 # Block comments use * on subsequent lines
3409 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3410 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
3411 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
3412 $rawline =~ /^\+/ && #line is new
3413 $rawline !~ /^\+[ \t]*\*/) { #no leading *
3414 WARN("BLOCK_COMMENT_STYLE",
3415 "Block comments use * on subsequent lines\n" . $hereprev);
3418 # Block comments use */ on trailing lines
3419 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
3420 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3421 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3422 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
3423 WARN("BLOCK_COMMENT_STYLE",
3424 "Block comments use a trailing */ on a separate line\n" . $herecurr);
3427 # Block comment * alignment
3428 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3429 $line =~ /^\+[ \t]*$;/ && #leading comment
3430 $rawline =~ /^\+[ \t]*\*/ && #leading *
3431 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
3432 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
3433 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3435 $prevrawline =~ m@^\+([ \t]*/?)\*@;
3437 $oldindent = expand_tabs($1);
3439 $prevrawline =~ m@^\+(.*/?)\*@;
3440 $oldindent = expand_tabs($1);
3442 $rawline =~ m@^\+([ \t]*)\*@;
3444 $newindent = expand_tabs($newindent);
3445 if (length($oldindent) ne length($newindent)) {
3446 WARN("BLOCK_COMMENT_STYLE",
3447 "Block comments should align the * on each line\n" . $hereprev);
3451 # check for missing blank lines after struct/union declarations
3452 # with exceptions for various attributes and macros
3453 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3455 !($line =~ /^\+\s*$/ ||
3456 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3457 $line =~ /^\+\s*MODULE_/i ||
3458 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3459 $line =~ /^\+[a-z_]*init/ ||
3460 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3461 $line =~ /^\+\s*DECLARE/ ||
3462 $line =~ /^\+\s*builtin_[\w_]*driver/ ||
3463 $line =~ /^\+\s*__setup/)) {
3464 if (CHK("LINE_SPACING",
3465 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3467 fix_insert_line($fixlinenr, "\+");
3471 # check for multiple consecutive blank lines
3472 if ($prevline =~ /^[\+ ]\s*$/ &&
3473 $line =~ /^\+\s*$/ &&
3474 $last_blank_line != ($linenr - 1)) {
3475 if (CHK("LINE_SPACING",
3476 "Please don't use multiple blank lines\n" . $hereprev) &&
3478 fix_delete_line($fixlinenr, $rawline);
3481 $last_blank_line = $linenr;
3484 # check for missing blank lines after declarations
3485 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3486 # actual declarations
3487 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3488 # function pointer declarations
3489 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3490 # foo bar; where foo is some local typedef or #define
3491 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3492 # known declaration macros
3493 $prevline =~ /^\+\s+$declaration_macros/) &&
3494 # for "else if" which can look like "$Ident $Ident"
3495 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3496 # other possible extensions of declaration lines
3497 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3498 # not starting a section or a macro "\" extended line
3499 $prevline =~ /(?:\{\s*|\\)$/) &&
3500 # looks like a declaration
3501 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3502 # function pointer declarations
3503 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3504 # foo bar; where foo is some local typedef or #define
3505 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3506 # known declaration macros
3507 $sline =~ /^\+\s+$declaration_macros/ ||
3508 # start of struct or union or enum
3509 $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
3510 # start or end of block or continuation of declaration
3511 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3512 # bitfield continuation
3513 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3514 # other possible extensions of declaration lines
3515 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3516 # indentation of previous and current line are the same
3517 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3518 if (WARN("LINE_SPACING",
3519 "Missing a blank line after declarations\n" . $hereprev) &&
3521 fix_insert_line($fixlinenr, "\+");
3525 # check for spaces at the beginning of a line.
3527 # 1) within comments
3528 # 2) indented preprocessor commands
3530 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
3531 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3532 if (WARN("LEADING_SPACE",
3533 "please, no spaces at the start of a line\n" . $herevet) &&
3535 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3539 # check we are in a valid C source file if not then ignore this hunk
3540 next if ($realfile !~ /\.(h|c)$/);
3542 # check for unusual line ending [ or (
3543 if ($line =~ /^\+.*([\[\(])\s*$/) {
3544 CHK("OPEN_ENDED_LINE",
3545 "Lines should not end with a '$1'\n" . $herecurr);
3548 # check if this appears to be the start function declaration, save the name
3549 if ($sline =~ /^\+\{\s*$/ &&
3550 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3551 $context_function = $1;
3554 # check if this appears to be the end of function declaration
3555 if ($sline =~ /^\+\}\s*$/) {
3556 undef $context_function;
3559 # check indentation of any line with a bare else
3560 # (but not if it is a multiple line "if (foo) return bar; else return baz;")
3561 # if the previous line is a break or return and is indented 1 tab more...
3562 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3563 my $tabs = length($1) + 1;
3564 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3565 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3566 defined $lines[$linenr] &&
3567 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3568 WARN("UNNECESSARY_ELSE",
3569 "else is not generally useful after a break or return\n" . $hereprev);
3573 # check indentation of a line with a break;
3574 # if the previous line is a goto or return and is indented the same # of tabs
3575 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3577 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3578 WARN("UNNECESSARY_BREAK",
3579 "break is not useful after a goto or return\n" . $hereprev);
3583 # check for RCS/CVS revision markers
3584 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3586 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3589 # check for old HOTPLUG __dev<foo> section markings
3590 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3591 WARN("HOTPLUG_SECTION",
3592 "Using $1 is unnecessary\n" . $herecurr);
3595 # Check for potential 'bare' types
3596 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3598 #print "LINE<$line>\n";
3599 if ($linenr > $suppress_statement &&
3600 $realcnt && $sline =~ /.\s*\S/) {
3601 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3602 ctx_statement_block($linenr, $realcnt, 0);
3603 $stat =~ s/\n./\n /g;
3604 $cond =~ s/\n./\n /g;
3606 #print "linenr<$linenr> <$stat>\n";
3607 # If this statement has no statement boundaries within
3608 # it there is no point in retrying a statement scan
3609 # until we hit end of it.
3610 my $frag = $stat; $frag =~ s/;+\s*$//;
3611 if ($frag !~ /(?:{|;)/) {
3612 #print "skip<$line_nr_next>\n";
3613 $suppress_statement = $line_nr_next;
3616 # Find the real next line.
3617 $realline_next = $line_nr_next;
3618 if (defined $realline_next &&
3619 (!defined $lines[$realline_next - 1] ||
3620 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3627 # Ignore goto labels.
3628 if ($s =~ /$Ident:\*$/s) {
3630 # Ignore functions being called
3631 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3633 } elsif ($s =~ /^.\s*else\b/s) {
3635 # declarations always start with types
3636 } 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) {
3639 possible($type, "A:" . $s);
3641 # definitions in global scope can only start with types
3642 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3643 possible($1, "B:" . $s);
3646 # any (foo ... *) is a pointer cast, and foo is a type
3647 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3648 possible($1, "C:" . $s);
3651 # Check for any sort of function declaration.
3652 # int foo(something bar, other baz);
3653 # void (*store_gdt)(x86_descr_ptr *);
3654 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
3655 my ($name_len) = length($1);
3658 substr($ctx, 0, $name_len + 1, '');
3659 $ctx =~ s/\)[^\)]*$//;
3661 for my $arg (split(/\s*,\s*/, $ctx)) {
3662 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
3664 possible($1, "D:" . $s);
3672 # Checks which may be anchored in the context.
3675 # Check for switch () and associated case and default
3676 # statements should be at the same indent.
3677 if ($line=~/\bswitch\s*\(.*\)/) {
3680 my @ctx = ctx_block_outer($linenr, $realcnt);
3682 for my $ctx (@ctx) {
3683 my ($clen, $cindent) = line_stats($ctx);
3684 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3685 $indent != $cindent) {
3686 $err .= "$sep$ctx\n";
3693 ERROR("SWITCH_CASE_INDENT_LEVEL",
3694 "switch and case should be at the same indent\n$hereline$err");
3698 # if/while/etc brace do not go on next line, unless defining a do while loop,
3699 # or if that brace on the next line is for something else
3700 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3701 my $pre_ctx = "$1$2";
3703 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
3705 if ($line =~ /^\+\t{6,}/) {
3706 WARN("DEEP_INDENTATION",
3707 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3710 my $ctx_cnt = $realcnt - $#ctx - 1;
3711 my $ctx = join("\n", @ctx);
3713 my $ctx_ln = $linenr;
3714 my $ctx_skip = $realcnt;
3716 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3717 defined $lines[$ctx_ln - 1] &&
3718 $lines[$ctx_ln - 1] =~ /^-/)) {
3719 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3720 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3724 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3725 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3727 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3729 "that open brace { should be on the previous line\n" .
3730 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3732 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3733 $ctx =~ /\)\s*\;\s*$/ &&
3734 defined $lines[$ctx_ln - 1])
3736 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3737 if ($nindent > $indent) {
3738 WARN("TRAILING_SEMICOLON",
3739 "trailing semicolon indicates no statements, indent implies otherwise\n" .
3740 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3745 # Check relative indent for conditionals and blocks.
3746 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
3747 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3748 ctx_statement_block($linenr, $realcnt, 0)
3749 if (!defined $stat);
3750 my ($s, $c) = ($stat, $cond);
3752 substr($s, 0, length($c), '');
3754 # remove inline comments
3758 # Find out how long the conditional actually is.
3759 my @newlines = ($c =~ /\n/gs);
3760 my $cond_lines = 1 + $#newlines;
3762 # Make sure we remove the line prefixes as we have
3763 # none on the first line, and are going to readd them
3766 while ($s =~ /\n\s+\\\n/) {
3767 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3770 # We want to check the first line inside the block
3771 # starting at the end of the conditional, so remove:
3772 # 1) any blank line termination
3773 # 2) any opening brace { on end of the line
3775 my $continuation = 0;
3777 $s =~ s/^.*\bdo\b//;
3779 if ($s =~ s/^\s*\\//) {
3782 if ($s =~ s/^\s*?\n//) {
3787 # Also ignore a loop construct at the end of a
3788 # preprocessor statement.
3789 if (($prevline =~ /^.\s*#\s*define\s/ ||
3790 $prevline =~ /\\\s*$/) && $continuation == 0) {
3796 while ($cond_ptr != $cond_lines) {
3797 $cond_ptr = $cond_lines;
3799 # If we see an #else/#elif then the code
3801 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3806 # 1) blank lines, they should be at 0,
3807 # 2) preprocessor lines, and
3809 if ($continuation ||
3811 $s =~ /^\s*#\s*?/ ||
3812 $s =~ /^\s*$Ident\s*:/) {
3813 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
3814 if ($s =~ s/^.*?\n//) {
3820 my (undef, $sindent) = line_stats("+" . $s);
3821 my $stat_real = raw_line($linenr, $cond_lines);
3823 # Check if either of these lines are modified, else
3824 # this is not this patch's fault.
3825 if (!defined($stat_real) ||
3826 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3829 if (defined($stat_real) && $cond_lines > 1) {
3830 $stat_real = "[...]\n$stat_real";
3833 #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";
3835 if ($check && $s ne '' &&
3836 (($sindent % $tabsize) != 0 ||
3837 ($sindent < $indent) ||
3838 ($sindent == $indent &&
3839 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
3840 ($sindent > $indent + $tabsize))) {
3841 WARN("SUSPECT_CODE_INDENT",
3842 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
3846 # Track the 'values' across context and added lines.
3847 my $opline = $line; $opline =~ s/^./ /;
3848 my ($curr_values, $curr_vars) =
3849 annotate_values($opline . "\n", $prev_values);
3850 $curr_values = $prev_values . $curr_values;
3852 my $outline = $opline; $outline =~ s/\t/ /g;
3853 print "$linenr > .$outline\n";
3854 print "$linenr > $curr_values\n";
3855 print "$linenr > $curr_vars\n";
3857 $prev_values = substr($curr_values, -1);
3859 #ignore lines not being added
3860 next if ($line =~ /^[^\+]/);
3862 # check for dereferences that span multiple lines
3863 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3864 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3865 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3867 $line =~ /^.\s*($Lval)/;
3870 WARN("MULTILINE_DEREFERENCE",
3871 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3874 # check for declarations of signed or unsigned without int
3875 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3878 $var = "" if (!defined $var);
3879 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3883 $pointer = "" if (!defined $pointer);
3885 if (WARN("UNSPECIFIED_INT",
3886 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3888 my $decl = trim($sign) . " int ";
3889 my $comp_pointer = $pointer;
3890 $comp_pointer =~ s/\s//g;
3891 $decl .= $comp_pointer;
3892 $decl = rtrim($decl) if ($var eq "");
3893 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3898 # TEST: allow direct testing of the type matcher.
3900 if ($line =~ /^.\s*$Declare\s*$/) {
3902 "TEST: is type\n" . $herecurr);
3903 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3904 ERROR("TEST_NOT_TYPE",
3905 "TEST: is not type ($1 is)\n". $herecurr);
3909 # TEST: allow direct testing of the attribute matcher.
3911 if ($line =~ /^.\s*$Modifier\s*$/) {
3913 "TEST: is attr\n" . $herecurr);
3914 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3915 ERROR("TEST_NOT_ATTR",
3916 "TEST: is not attr ($1 is)\n". $herecurr);
3921 # check for initialisation to aggregates open brace on the next line
3922 if ($line =~ /^.\s*{/ &&
3923 $prevline =~ /(?:^|[^=])=\s*$/) {
3924 if (ERROR("OPEN_BRACE",
3925 "that open brace { should be on the previous line\n" . $hereprev) &&
3926 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3927 fix_delete_line($fixlinenr - 1, $prevrawline);
3928 fix_delete_line($fixlinenr, $rawline);
3929 my $fixedline = $prevrawline;
3930 $fixedline =~ s/\s*=\s*$/ = {/;
3931 fix_insert_line($fixlinenr, $fixedline);
3933 $fixedline =~ s/^(.\s*)\{\s*/$1/;
3934 fix_insert_line($fixlinenr, $fixedline);
3939 # Checks which are anchored on the added line.
3942 # check for malformed paths in #include statements (uses RAW line)
3943 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3945 if ($path =~ m{//}) {
3946 ERROR("MALFORMED_INCLUDE",
3947 "malformed #include filename\n" . $herecurr);
3949 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3950 ERROR("UAPI_INCLUDE",
3951 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3955 # no C99 // comments
3956 if ($line =~ m{//}) {
3957 if (ERROR("C99_COMMENTS",
3958 "do not use C99 // comments\n" . $herecurr) &&
3960 my $line = $fixed[$fixlinenr];
3961 if ($line =~ /\/\/(.*)$/) {
3962 my $comment = trim($1);
3963 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
3967 # Remove C99 comments.
3969 $opline =~ s@//.*@@;
3971 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3972 # the whole statement.
3973 #print "APW <$lines[$realline_next - 1]>\n";
3974 if (defined $realline_next &&
3975 exists $lines[$realline_next - 1] &&
3976 !defined $suppress_export{$realline_next} &&
3977 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3978 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3979 # Handle definitions which produce identifiers with
3982 # EXPORT_SYMBOL(something_foo);
3984 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
3985 $name =~ /^${Ident}_$2/) {
3986 #print "FOO C name<$name>\n";
3987 $suppress_export{$realline_next} = 1;
3989 } elsif ($stat !~ /(?:
3991 ^.DEFINE_$Ident\(\Q$name\E\)|
3992 ^.DECLARE_$Ident\(\Q$name\E\)|
3993 ^.LIST_HEAD\(\Q$name\E\)|
3994 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3995 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
3997 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3998 $suppress_export{$realline_next} = 2;
4000 $suppress_export{$realline_next} = 1;
4003 if (!defined $suppress_export{$linenr} &&
4004 $prevline =~ /^.\s*$/ &&
4005 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
4006 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
4007 #print "FOO B <$lines[$linenr - 1]>\n";
4008 $suppress_export{$linenr} = 2;
4010 if (defined $suppress_export{$linenr} &&
4011 $suppress_export{$linenr} == 2) {
4012 WARN("EXPORT_SYMBOL",
4013 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
4016 # check for global initialisers.
4017 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
4018 if (ERROR("GLOBAL_INITIALISERS",
4019 "do not initialise globals to $1\n" . $herecurr) &&
4021 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
4024 # check for static initialisers.
4025 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
4026 if (ERROR("INITIALISED_STATIC",
4027 "do not initialise statics to $1\n" .
4030 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
4034 # check for misordered declarations of char/short/int/long with signed/unsigned
4035 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4037 WARN("MISORDERED_TYPE",
4038 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4041 # check for unnecessary <signed> int declarations of short/long/long long
4042 while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4043 my $type = trim($1);
4044 next if ($type !~ /\bint\b/);
4045 next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4046 my $new_type = $type;
4047 $new_type =~ s/\b\s*int\s*\b/ /;
4048 $new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4049 $new_type =~ s/^const\s+//;
4050 $new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4051 $new_type = "const $new_type" if ($type =~ /^const\b/);
4052 $new_type =~ s/\s+/ /g;
4053 $new_type = trim($new_type);
4054 if (WARN("UNNECESSARY_INT",
4055 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4057 $fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4061 # check for static const char * arrays.
4062 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4063 WARN("STATIC_CONST_CHAR_ARRAY",
4064 "static const char * array should probably be static const char * const\n" .
4068 # check for initialized const char arrays that should be static const
4069 if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4070 if (WARN("STATIC_CONST_CHAR_ARRAY",
4071 "const array should probably be static const\n" . $herecurr) &&
4073 $fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4077 # check for static char foo[] = "bar" declarations.
4078 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4079 WARN("STATIC_CONST_CHAR_ARRAY",
4080 "static char array declaration should probably be static const char\n" .
4084 # check for const <foo> const where <foo> is not a pointer or array type
4085 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4087 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4089 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4090 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4092 "'const $found const' should probably be 'const $found'\n" . $herecurr);
4096 # check for non-global char *foo[] = {"bar", ...} declarations.
4097 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4098 WARN("STATIC_CONST_CHAR_ARRAY",
4099 "char * array declaration might be better as static const\n" .
4103 # check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4104 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4106 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4108 if (WARN("ARRAY_SIZE",
4109 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4111 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4116 # check for function declarations without arguments like "int foo()"
4117 if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
4118 if (ERROR("FUNCTION_WITHOUT_ARGS",
4119 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4121 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4125 # check for new typedefs, only function parameters and sparse annotations
4127 if ($line =~ /\btypedef\s/ &&
4128 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4129 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
4130 $line !~ /\b$typeTypedefs\b/ &&
4131 $line !~ /\b__bitwise\b/) {
4132 WARN("NEW_TYPEDEFS",
4133 "do not add new typedefs\n" . $herecurr);
4136 # * goes on variable not on type
4138 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4140 my ($ident, $from, $to) = ($1, $2, $2);
4142 # Should start with a space.
4143 $to =~ s/^(\S)/ $1/;
4144 # Should not end with a space.
4146 # '*'s should not have spaces between.
4147 while ($to =~ s/\*\s+\*/\*\*/) {
4150 ## print "1: from<$from> to<$to> ident<$ident>\n";
4152 if (ERROR("POINTER_LOCATION",
4153 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
4155 my $sub_from = $ident;
4156 my $sub_to = $ident;
4157 $sub_to =~ s/\Q$from\E/$to/;
4158 $fixed[$fixlinenr] =~
4159 s@\Q$sub_from\E@$sub_to@;
4163 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4165 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4167 # Should start with a space.
4168 $to =~ s/^(\S)/ $1/;
4169 # Should not end with a space.
4171 # '*'s should not have spaces between.
4172 while ($to =~ s/\*\s+\*/\*\*/) {
4174 # Modifiers should have spaces.
4175 $to =~ s/(\b$Modifier$)/$1 /;
4177 ## print "2: from<$from> to<$to> ident<$ident>\n";
4178 if ($from ne $to && $ident !~ /^$Modifier$/) {
4179 if (ERROR("POINTER_LOCATION",
4180 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
4183 my $sub_from = $match;
4184 my $sub_to = $match;
4185 $sub_to =~ s/\Q$from\E/$to/;
4186 $fixed[$fixlinenr] =~
4187 s@\Q$sub_from\E@$sub_to@;
4192 # avoid BUG() or BUG_ON()
4193 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
4194 my $msg_level = \&WARN;
4195 $msg_level = \&CHK if ($file);
4196 &{$msg_level}("AVOID_BUG",
4197 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
4200 # avoid LINUX_VERSION_CODE
4201 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4202 WARN("LINUX_VERSION_CODE",
4203 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
4206 # check for uses of printk_ratelimit
4207 if ($line =~ /\bprintk_ratelimit\s*\(/) {
4208 WARN("PRINTK_RATELIMITED",
4209 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
4212 # printk should use KERN_* levels
4213 if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4214 WARN("PRINTK_WITHOUT_KERN_LEVEL",
4215 "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
4218 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4220 my $level = lc($orig);
4221 $level = "warn" if ($level eq "warning");
4222 my $level2 = $level;
4223 $level2 = "dbg" if ($level eq "debug");
4224 WARN("PREFER_PR_LEVEL",
4225 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
4228 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4230 my $level = lc($orig);
4231 $level = "warn" if ($level eq "warning");
4232 $level = "dbg" if ($level eq "debug");
4233 WARN("PREFER_DEV_LEVEL",
4234 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4237 # ENOSYS means "bad syscall nr" and nothing else. This will have a small
4238 # number of false positives, but assembly files are not checked, so at
4239 # least the arch entry code will not trigger this warning.
4240 if ($line =~ /\bENOSYS\b/) {
4242 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4245 # ENOTSUPP is not a standard error code and should be avoided in new patches.
4246 # Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4247 # Similarly to ENOSYS warning a small number of false positives is expected.
4248 if (!$file && $line =~ /\bENOTSUPP\b/) {
4249 if (WARN("ENOTSUPP",
4250 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4252 $fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4256 # function brace can't be on same line, except for #defines of do while,
4257 # or if closed on same line
4258 if ($perl_version_ok &&
4259 $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4260 $sline !~ /\#\s*define\b.*do\s*\{/ &&
4262 if (ERROR("OPEN_BRACE",
4263 "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
4265 fix_delete_line($fixlinenr, $rawline);
4266 my $fixed_line = $rawline;
4267 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
4270 fix_insert_line($fixlinenr, ltrim($line1));
4271 fix_insert_line($fixlinenr, "\+{");
4272 if ($line2 !~ /^\s*$/) {
4273 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4278 # open braces for enum, union and struct go on the same line.
4279 if ($line =~ /^.\s*{/ &&
4280 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
4281 if (ERROR("OPEN_BRACE",
4282 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4283 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4284 fix_delete_line($fixlinenr - 1, $prevrawline);
4285 fix_delete_line($fixlinenr, $rawline);
4286 my $fixedline = rtrim($prevrawline) . " {";
4287 fix_insert_line($fixlinenr, $fixedline);
4288 $fixedline = $rawline;
4289 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
4290 if ($fixedline !~ /^\+\s*$/) {
4291 fix_insert_line($fixlinenr, $fixedline);
4296 # missing space after union, struct or enum definition
4297 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4299 "missing space after $1 definition\n" . $herecurr) &&
4301 $fixed[$fixlinenr] =~
4302 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4306 # Function pointer declarations
4307 # check spacing between type, funcptr, and args
4308 # canonical declaration is "type (*funcptr)(args...)"
4309 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
4311 my $pre_pointer_space = $2;
4312 my $post_pointer_space = $3;
4314 my $post_funcname_space = $5;
4315 my $pre_args_space = $6;
4317 # the $Declare variable will capture all spaces after the type
4318 # so check it for a missing trailing missing space but pointer return types
4319 # don't need a space so don't warn for those.
4320 my $post_declare_space = "";
4321 if ($declare =~ /(\s+)$/) {
4322 $post_declare_space = $1;
4323 $declare = rtrim($declare);
4325 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
4327 "missing space after return type\n" . $herecurr);
4328 $post_declare_space = " ";
4331 # unnecessary space "type (*funcptr)(args...)"
4332 # This test is not currently implemented because these declarations are
4334 # int foo(int bar, ...)
4335 # and this is form shouldn't/doesn't generate a checkpatch warning.
4337 # elsif ($declare =~ /\s{2,}$/) {
4339 # "Multiple spaces after return type\n" . $herecurr);
4342 # unnecessary space "type ( *funcptr)(args...)"
4343 if (defined $pre_pointer_space &&
4344 $pre_pointer_space =~ /^\s/) {
4346 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4349 # unnecessary space "type (* funcptr)(args...)"
4350 if (defined $post_pointer_space &&
4351 $post_pointer_space =~ /^\s/) {
4353 "Unnecessary space before function pointer name\n" . $herecurr);
4356 # unnecessary space "type (*funcptr )(args...)"
4357 if (defined $post_funcname_space &&
4358 $post_funcname_space =~ /^\s/) {
4360 "Unnecessary space after function pointer name\n" . $herecurr);
4363 # unnecessary space "type (*funcptr) (args...)"
4364 if (defined $pre_args_space &&
4365 $pre_args_space =~ /^\s/) {
4367 "Unnecessary space before function pointer arguments\n" . $herecurr);
4370 if (show_type("SPACING") && $fix) {
4371 $fixed[$fixlinenr] =~
4372 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
4376 # check for spacing round square brackets; allowed:
4377 # 1. with a type on the left -- int [] a;
4378 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4379 # 3. inside a curly brace -- = { [0...10] = 5 }
4380 while ($line =~ /(.*?\s)\[/g) {
4381 my ($where, $prefix) = ($-[1], $1);
4382 if ($prefix !~ /$Type\s+$/ &&
4383 ($where != 0 || $prefix !~ /^.\s+$/) &&
4384 $prefix !~ /[{,:]\s+$/) {
4385 if (ERROR("BRACKET_SPACE",
4386 "space prohibited before open square bracket '['\n" . $herecurr) &&
4388 $fixed[$fixlinenr] =~
4389 s/^(\+.*?)\s+\[/$1\[/;
4394 # check for spaces between functions and their parentheses.
4395 while ($line =~ /($Ident)\s+\(/g) {
4397 my $ctx_before = substr($line, 0, $-[1]);
4398 my $ctx = "$ctx_before$name";
4400 # Ignore those directives where spaces _are_ permitted.
4402 if|for|while|switch|return|case|
4403 volatile|__volatile__|
4404 __attribute__|format|__extension__|
4407 # cpp #define statements have non-optional spaces, ie
4408 # if there is a space between the name and the open
4409 # parenthesis it is simply not a parameter group.
4410 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4412 # cpp #elif statement condition may start with a (
4413 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4415 # If this whole things ends with a type its most
4416 # likely a typedef for a function.
4417 } elsif ($ctx =~ /$Type$/) {
4421 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4423 $fixed[$fixlinenr] =~
4424 s/\b$name\s+\(/$name\(/;
4429 # Check operator spacing.
4430 if (!($line=~/\#\s*include/)) {
4431 my $fixed_line = "";
4435 <<=|>>=|<=|>=|==|!=|
4436 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4437 =>|->|<<|>>|<|>|=|!|~|
4438 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
4441 my @elements = split(/($ops|;)/, $opline);
4443 ## print("element count: <" . $#elements . ">\n");
4444 ## foreach my $el (@elements) {
4445 ## print("el: <$el>\n");
4448 my @fix_elements = ();
4451 foreach my $el (@elements) {
4452 push(@fix_elements, substr($rawline, $off, length($el)));
4453 $off += length($el);
4458 my $blank = copy_spacing($opline);
4459 my $last_after = -1;
4461 for (my $n = 0; $n < $#elements; $n += 2) {
4463 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4465 ## print("n: <$n> good: <$good>\n");
4467 $off += length($elements[$n]);
4469 # Pick up the preceding and succeeding characters.
4470 my $ca = substr($opline, 0, $off);
4472 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4473 $cc = substr($opline, $off + length($elements[$n + 1]));
4475 my $cb = "$ca$;$cc";
4478 $a = 'V' if ($elements[$n] ne '');
4479 $a = 'W' if ($elements[$n] =~ /\s$/);
4480 $a = 'C' if ($elements[$n] =~ /$;$/);
4481 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4482 $a = 'O' if ($elements[$n] eq '');
4483 $a = 'E' if ($ca =~ /^\s*$/);
4485 my $op = $elements[$n + 1];
4488 if (defined $elements[$n + 2]) {
4489 $c = 'V' if ($elements[$n + 2] ne '');
4490 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
4491 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4492 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4493 $c = 'O' if ($elements[$n + 2] eq '');
4494 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4499 my $ctx = "${a}x${c}";
4501 my $at = "(ctx:$ctx)";
4503 my $ptr = substr($blank, 0, $off) . "^";
4504 my $hereptr = "$hereline$ptr\n";
4506 # Pull out the value of this operator.
4507 my $op_type = substr($curr_values, $off + 1, 1);
4509 # Get the full operator variant.
4510 my $opv = $op . substr($curr_vars, $off, 1);
4512 # Ignore operators passed as parameters.
4513 if ($op_type ne 'V' &&
4514 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
4517 # } elsif ($op =~ /^$;+$/) {
4519 # ; should have either the end of line or a space or \ after it
4520 } elsif ($op eq ';') {
4521 if ($ctx !~ /.x[WEBC]/ &&
4522 $cc !~ /^\\/ && $cc !~ /^;/) {
4523 if (ERROR("SPACING",
4524 "space required after that '$op' $at\n" . $hereptr)) {
4525 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4531 } elsif ($op eq '//') {
4533 # : when part of a bitfield
4534 } elsif ($opv eq ':B') {
4535 # skip the bitfield test for now
4539 } elsif ($op eq '->') {
4540 if ($ctx =~ /Wx.|.xW/) {
4541 if (ERROR("SPACING",
4542 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4543 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4544 if (defined $fix_elements[$n + 2]) {
4545 $fix_elements[$n + 2] =~ s/^\s+//;
4551 # , must not have a space before and must have a space on the right.
4552 } elsif ($op eq ',') {
4553 my $rtrim_before = 0;
4554 my $space_after = 0;
4555 if ($ctx =~ /Wx./) {
4556 if (ERROR("SPACING",
4557 "space prohibited before that '$op' $at\n" . $hereptr)) {
4562 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
4563 if (ERROR("SPACING",
4564 "space required after that '$op' $at\n" . $hereptr)) {
4570 if ($rtrim_before || $space_after) {
4571 if ($rtrim_before) {
4572 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4574 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4581 # '*' as part of a type definition -- reported already.
4582 } elsif ($opv eq '*_') {
4583 #warn "'*' is part of type\n";
4585 # unary operators should have a space before and
4586 # none after. May be left adjacent to another
4587 # unary operator, or a cast
4588 } elsif ($op eq '!' || $op eq '~' ||
4589 $opv eq '*U' || $opv eq '-U' ||
4590 $opv eq '&U' || $opv eq '&&U') {
4591 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
4592 if (ERROR("SPACING",
4593 "space required before that '$op' $at\n" . $hereptr)) {
4594 if ($n != $last_after + 2) {
4595 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4600 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4601 # A unary '*' may be const
4603 } elsif ($ctx =~ /.xW/) {
4604 if (ERROR("SPACING",
4605 "space prohibited after that '$op' $at\n" . $hereptr)) {
4606 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
4607 if (defined $fix_elements[$n + 2]) {
4608 $fix_elements[$n + 2] =~ s/^\s+//;
4614 # unary ++ and unary -- are allowed no space on one side.
4615 } elsif ($op eq '++' or $op eq '--') {
4616 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
4617 if (ERROR("SPACING",
4618 "space required one side of that '$op' $at\n" . $hereptr)) {
4619 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4623 if ($ctx =~ /Wx[BE]/ ||
4624 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
4625 if (ERROR("SPACING",
4626 "space prohibited before that '$op' $at\n" . $hereptr)) {
4627 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4631 if ($ctx =~ /ExW/) {
4632 if (ERROR("SPACING",
4633 "space prohibited after that '$op' $at\n" . $hereptr)) {
4634 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4635 if (defined $fix_elements[$n + 2]) {
4636 $fix_elements[$n + 2] =~ s/^\s+//;
4642 # << and >> may either have or not have spaces both sides
4643 } elsif ($op eq '<<' or $op eq '>>' or
4644 $op eq '&' or $op eq '^' or $op eq '|' or
4645 $op eq '+' or $op eq '-' or
4646 $op eq '*' or $op eq '/' or
4650 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4652 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4653 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4654 $fix_elements[$n + 2] =~ s/^\s+//;
4657 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4659 "space preferred before that '$op' $at\n" . $hereptr)) {
4660 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4664 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
4665 if (ERROR("SPACING",
4666 "need consistent spacing around '$op' $at\n" . $hereptr)) {
4667 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4668 if (defined $fix_elements[$n + 2]) {
4669 $fix_elements[$n + 2] =~ s/^\s+//;
4675 # A colon needs no spaces before when it is
4676 # terminating a case value or a label.
4677 } elsif ($opv eq ':C' || $opv eq ':L') {
4678 if ($ctx =~ /Wx./) {
4679 if (ERROR("SPACING",
4680 "space prohibited before that '$op' $at\n" . $hereptr)) {
4681 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4686 # All the others need spaces both sides.
4687 } elsif ($ctx !~ /[EWC]x[CWE]/) {
4690 # Ignore email addresses <foo@bar>
4692 $cc =~ /^\S+\@\S+>/) ||
4694 $ca =~ /<\S+\@\S+$/))
4699 # for asm volatile statements
4700 # ignore a colon with another
4701 # colon immediately before or after
4703 ($ca =~ /:$/ || $cc =~ /^:/)) {
4707 # messages are ERROR, but ?: are CHK
4709 my $msg_level = \&ERROR;
4710 $msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4712 if (&{$msg_level}("SPACING",
4713 "spaces required around that '$op' $at\n" . $hereptr)) {
4714 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4715 if (defined $fix_elements[$n + 2]) {
4716 $fix_elements[$n + 2] =~ s/^\s+//;
4722 $off += length($elements[$n + 1]);
4724 ## print("n: <$n> GOOD: <$good>\n");
4726 $fixed_line = $fixed_line . $good;
4729 if (($#elements % 2) == 0) {
4730 $fixed_line = $fixed_line . $fix_elements[$#elements];
4733 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4734 $fixed[$fixlinenr] = $fixed_line;
4740 # check for whitespace before a non-naked semicolon
4741 if ($line =~ /^\+.*\S\s+;\s*$/) {
4743 "space prohibited before semicolon\n" . $herecurr) &&
4745 1 while $fixed[$fixlinenr] =~
4746 s/^(\+.*\S)\s+;/$1;/;
4750 # check for multiple assignments
4751 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4752 CHK("MULTIPLE_ASSIGNMENTS",
4753 "multiple assignments should be avoided\n" . $herecurr);
4756 ## # check for multiple declarations, allowing for a function declaration
4758 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4759 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4761 ## # Remove any bracketed sections to ensure we do not
4762 ## # falsly report the parameters of functions.
4764 ## while ($ln =~ s/\([^\(\)]*\)//g) {
4766 ## if ($ln =~ /,/) {
4767 ## WARN("MULTIPLE_DECLARATION",
4768 ## "declaring multiple variables together should be avoided\n" . $herecurr);
4772 #need space before brace following if, while, etc
4773 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
4774 $line =~ /\b(?:else|do)\{/) {
4775 if (ERROR("SPACING",
4776 "space required before the open brace '{'\n" . $herecurr) &&
4778 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
4782 ## # check for blank lines before declarations
4783 ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4784 ## $prevrawline =~ /^.\s*$/) {
4786 ## "No blank lines before declarations\n" . $hereprev);
4790 # closing brace should have a space following it when it has anything
4792 if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
4793 if (ERROR("SPACING",
4794 "space required after that close brace '}'\n" . $herecurr) &&
4796 $fixed[$fixlinenr] =~
4797 s/}((?!(?:,|;|\)))\S)/} $1/;
4801 # check spacing on square brackets
4802 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
4803 if (ERROR("SPACING",
4804 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4806 $fixed[$fixlinenr] =~
4810 if ($line =~ /\s\]/) {
4811 if (ERROR("SPACING",
4812 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4814 $fixed[$fixlinenr] =~
4819 # check spacing on parentheses
4820 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4821 $line !~ /for\s*\(\s+;/) {
4822 if (ERROR("SPACING",
4823 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4825 $fixed[$fixlinenr] =~
4829 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4830 $line !~ /for\s*\(.*;\s+\)/ &&
4831 $line !~ /:\s+\)/) {
4832 if (ERROR("SPACING",
4833 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4835 $fixed[$fixlinenr] =~
4840 # check unnecessary parentheses around addressof/dereference single $Lvals
4841 # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4843 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4845 if (CHK("UNNECESSARY_PARENTHESES",
4846 "Unnecessary parentheses around $var\n" . $herecurr) &&
4848 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4852 # check for unnecessary parentheses around function pointer uses
4853 # ie: (foo->bar)(); should be foo->bar();
4854 # but not "if (foo->bar) (" to avoid some false positives
4855 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4857 if (CHK("UNNECESSARY_PARENTHESES",
4858 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4860 my $var2 = deparenthesize($var);
4862 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4866 # check for unnecessary parentheses around comparisons in if uses
4867 # when !drivers/staging or command-line uses --strict
4868 if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
4869 $perl_version_ok && defined($stat) &&
4870 $stat =~ /(^.\s*if\s*($balanced_parens))/) {
4872 my $test = substr($2, 1, -1);
4874 while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
4876 # avoid parentheses around potential macro args
4877 next if ($match =~ /^\s*\w+\s*$/);
4878 if (!defined($herectx)) {
4879 $herectx = $here . "\n";
4880 my $cnt = statement_rawlines($if_stat);
4881 for (my $n = 0; $n < $cnt; $n++) {
4882 my $rl = raw_line($linenr, $n);
4883 $herectx .= $rl . "\n";
4884 last if $rl =~ /^[ \+].*\{/;
4887 CHK("UNNECESSARY_PARENTHESES",
4888 "Unnecessary parentheses around '$match'\n" . $herectx);
4892 #goto labels aren't indented, allow a single space however
4893 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
4894 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
4895 if (WARN("INDENTED_LABEL",
4896 "labels should not be indented\n" . $herecurr) &&
4898 $fixed[$fixlinenr] =~
4903 # return is not a function
4904 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4906 if ($perl_version_ok &&
4907 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4909 $value = deparenthesize($value);
4910 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4911 ERROR("RETURN_PARENTHESES",
4912 "return is not a function, parentheses are not required\n" . $herecurr);
4914 } elsif ($spacing !~ /\s+/) {
4916 "space required before the open parenthesis '('\n" . $herecurr);
4920 # unnecessary return in a void function
4921 # at end-of-function, with the previous line a single leading tab, then return;
4922 # and the line before that not a goto label target like "out:"
4923 if ($sline =~ /^[ \+]}\s*$/ &&
4924 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4926 $lines[$linenr - 3] =~ /^[ +]/ &&
4927 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
4929 "void function return statements are not generally useful\n" . $hereprev);
4932 # if statements using unnecessary parentheses - ie: if ((foo == bar))
4933 if ($perl_version_ok &&
4934 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4935 my $openparens = $1;
4936 my $count = $openparens =~ tr@\(@\(@;
4938 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4939 my $comp = $4; #Not $1 because of $LvalOrFunc
4940 $msg = " - maybe == should be = ?" if ($comp eq "==");
4941 WARN("UNNECESSARY_PARENTHESES",
4942 "Unnecessary parentheses$msg\n" . $herecurr);
4946 # comparisons with a constant or upper case identifier on the left
4947 # avoid cases like "foo + BAR < baz"
4948 # only fix matches surrounded by parentheses to avoid incorrect
4949 # conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4950 if ($perl_version_ok &&
4951 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4956 my $newcomp = $comp;
4957 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4958 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4959 WARN("CONSTANT_COMPARISON",
4960 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4964 } elsif ($comp eq "<=") {
4966 } elsif ($comp eq ">") {
4968 } elsif ($comp eq ">=") {
4971 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4975 # Return of what appears to be an errno should normally be negative
4976 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
4978 if ($name ne 'EOF' && $name ne 'ERROR') {
4979 WARN("USE_NEGATIVE_ERRNO",
4980 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
4984 # Need a space before open parenthesis after if, while etc
4985 if ($line =~ /\b(if|while|for|switch)\(/) {
4986 if (ERROR("SPACING",
4987 "space required before the open parenthesis '('\n" . $herecurr) &&
4989 $fixed[$fixlinenr] =~
4990 s/\b(if|while|for|switch)\(/$1 \(/;
4994 # Check for illegal assignment in if conditional -- and check for trailing
4995 # statements after the conditional.
4996 if ($line =~ /do\s*(?!{)/) {
4997 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4998 ctx_statement_block($linenr, $realcnt, 0)
4999 if (!defined $stat);
5000 my ($stat_next) = ctx_statement_block($line_nr_next,
5001 $remain_next, $off_next);
5002 $stat_next =~ s/\n./\n /g;
5003 ##print "stat<$stat> stat_next<$stat_next>\n";
5005 if ($stat_next =~ /^\s*while\b/) {
5006 # If the statement carries leading newlines,
5007 # then count those as offsets.
5009 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5011 statement_rawlines($whitespace) - 1;
5013 $suppress_whiletrailers{$line_nr_next +
5017 if (!defined $suppress_whiletrailers{$linenr} &&
5018 defined($stat) && defined($cond) &&
5019 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5020 my ($s, $c) = ($stat, $cond);
5022 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
5023 ERROR("ASSIGN_IN_IF",
5024 "do not use assignment in if condition\n" . $herecurr);
5027 # Find out what is on the end of the line after the
5029 substr($s, 0, length($c), '');
5031 $s =~ s/$;//g; # Remove any comments
5032 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5033 $c !~ /}\s*while\s*/)
5035 # Find out how long the conditional actually is.
5036 my @newlines = ($c =~ /\n/gs);
5037 my $cond_lines = 1 + $#newlines;
5040 $stat_real = raw_line($linenr, $cond_lines)
5041 . "\n" if ($cond_lines);
5042 if (defined($stat_real) && $cond_lines > 1) {
5043 $stat_real = "[...]\n$stat_real";
5046 ERROR("TRAILING_STATEMENTS",
5047 "trailing statements should be on next line\n" . $herecurr . $stat_real);
5051 # Check for bitwise tests written as boolean
5063 WARN("HEXADECIMAL_BOOLEAN_TEST",
5064 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
5067 # if and else should not have general statements after it
5068 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5070 $s =~ s/$;//g; # Remove any comments
5071 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5072 ERROR("TRAILING_STATEMENTS",
5073 "trailing statements should be on next line\n" . $herecurr);
5076 # if should not continue a brace
5077 if ($line =~ /}\s*if\b/) {
5078 ERROR("TRAILING_STATEMENTS",
5079 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
5082 # case and default should not have general statements after them
5083 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5085 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5089 ERROR("TRAILING_STATEMENTS",
5090 "trailing statements should be on next line\n" . $herecurr);
5093 # Check for }<nl>else {, these must be at the same
5094 # indent level to be relevant to each other.
5095 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5096 $previndent == $indent) {
5097 if (ERROR("ELSE_AFTER_BRACE",
5098 "else should follow close brace '}'\n" . $hereprev) &&
5099 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5100 fix_delete_line($fixlinenr - 1, $prevrawline);
5101 fix_delete_line($fixlinenr, $rawline);
5102 my $fixedline = $prevrawline;
5103 $fixedline =~ s/}\s*$//;
5104 if ($fixedline !~ /^\+\s*$/) {
5105 fix_insert_line($fixlinenr, $fixedline);
5107 $fixedline = $rawline;
5108 $fixedline =~ s/^(.\s*)else/$1} else/;
5109 fix_insert_line($fixlinenr, $fixedline);
5113 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5114 $previndent == $indent) {
5115 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5117 # Find out what is on the end of the line after the
5119 substr($s, 0, length($c), '');
5122 if ($s =~ /^\s*;/) {
5123 if (ERROR("WHILE_AFTER_BRACE",
5124 "while should follow close brace '}'\n" . $hereprev) &&
5125 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5126 fix_delete_line($fixlinenr - 1, $prevrawline);
5127 fix_delete_line($fixlinenr, $rawline);
5128 my $fixedline = $prevrawline;
5129 my $trailing = $rawline;
5130 $trailing =~ s/^\+//;
5131 $trailing = trim($trailing);
5132 $fixedline =~ s/}\s*$/} $trailing/;
5133 fix_insert_line($fixlinenr, $fixedline);
5138 #Specific variable tests
5139 while ($line =~ m{($Constant|$Lval)}g) {
5143 if ($var !~ /^$Constant$/ &&
5144 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
5145 #Ignore Page<foo> variants
5146 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5147 #Ignore SI style variants like nS, mV and dB
5148 #(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5149 $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5150 #Ignore some three character SI units explicitly, like MiB and KHz
5151 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
5152 while ($var =~ m{($Ident)}g) {
5154 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5156 seed_camelcase_includes();
5157 if (!$file && !$camelcase_file_seeded) {
5158 seed_camelcase_file($realfile);
5159 $camelcase_file_seeded = 1;
5162 if (!defined $camelcase{$word}) {
5163 $camelcase{$word} = 1;
5165 "Avoid CamelCase: <$word>\n" . $herecurr);
5171 #no spaces allowed after \ in define
5172 if ($line =~ /\#\s*define.*\\\s+$/) {
5173 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5174 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5176 $fixed[$fixlinenr] =~ s/\s+$//;
5180 # warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5181 # itself <asm/foo.h> (uses RAW line)
5182 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5184 my $checkfile = "include/linux/$file";
5185 if (-f "$root/$checkfile" &&
5186 $realfile ne $checkfile &&
5187 $1 !~ /$allowed_asm_includes/)
5189 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5190 if ($asminclude > 0) {
5191 if ($realfile =~ m{^arch/}) {
5192 CHK("ARCH_INCLUDE_LINUX",
5193 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5195 WARN("INCLUDE_LINUX",
5196 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5202 # multi-statement macros should be enclosed in a do while loop, grab the
5203 # first statement and ensure its the whole macro if its not enclosed
5204 # in a known good container
5205 if ($realfile !~ m@/vmlinux.lds.h$@ &&
5206 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5209 my ($off, $dstat, $dcond, $rest);
5211 my $has_flow_statement = 0;
5212 my $has_arg_concat = 0;
5213 ($dstat, $dcond, $ln, $cnt, $off) =
5214 ctx_statement_block($linenr, $realcnt, 0);
5216 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5217 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5219 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
5220 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
5222 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5223 my $define_args = $1;
5224 my $define_stmt = $dstat;
5227 if (defined $define_args && $define_args ne "") {
5228 $define_args = substr($define_args, 1, length($define_args) - 2);
5229 $define_args =~ s/\s*//g;
5230 $define_args =~ s/\\\+?//g;
5231 @def_args = split(",", $define_args);
5235 $dstat =~ s/\\\n.//g;
5236 $dstat =~ s/^\s*//s;
5237 $dstat =~ s/\s*$//s;
5239 # Flatten any parentheses and braces
5240 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5241 $dstat =~ s/\{[^\{\}]*\}/1/ ||
5242 $dstat =~ s/.\[[^\[\]]*\]/1/)
5246 # Flatten any obvious string concatenation.
5247 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5248 $dstat =~ s/$Ident\s*($String)/$1/)
5252 # Make asm volatile uses seem like a generic function
5253 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5255 my $exceptions = qr{
5268 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5271 my $stmt_cnt = statement_rawlines($ctx);
5272 my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5275 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
5276 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
5277 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5278 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
5279 $dstat !~ /$exceptions/ &&
5280 $dstat !~ /^\.$Ident\s*=/ && # .foo =
5281 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
5282 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
5283 $dstat !~ /^for\s*$Constant$/ && # for (...)
5284 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
5285 $dstat !~ /^do\s*{/ && # do {...
5286 $dstat !~ /^\(\{/ && # ({...
5287 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5289 if ($dstat =~ /^\s*if\b/) {
5290 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5291 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5292 } elsif ($dstat =~ /;/) {
5293 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5294 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5296 ERROR("COMPLEX_MACRO",
5297 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5302 # Make $define_stmt single line, comment-free, etc
5303 my @stmt_array = split('\n', $define_stmt);
5306 foreach my $l (@stmt_array) {
5311 } elsif ($l =~ /^[\+ ]/) {
5312 $define_stmt .= substr($l, 1);
5315 $define_stmt =~ s/$;//g;
5316 $define_stmt =~ s/\s+/ /g;
5317 $define_stmt = trim($define_stmt);
5319 # check if any macro arguments are reused (ignore '...' and 'type')
5320 foreach my $arg (@def_args) {
5321 next if ($arg =~ /\.\.\./);
5322 next if ($arg =~ /^type$/i);
5323 my $tmp_stmt = $define_stmt;
5324 $tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
5325 $tmp_stmt =~ s/\#+\s*$arg\b//g;
5326 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
5327 my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5329 CHK("MACRO_ARG_REUSE",
5330 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5332 # check if any macro arguments may have other precedence issues
5333 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5334 ((defined($1) && $1 ne ',') ||
5335 (defined($2) && $2 ne ','))) {
5336 CHK("MACRO_ARG_PRECEDENCE",
5337 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
5341 # check for macros with flow control, but without ## concatenation
5342 # ## concatenation is commonly a macro that defines a function so ignore those
5343 if ($has_flow_statement && !$has_arg_concat) {
5344 my $cnt = statement_rawlines($ctx);
5345 my $herectx = get_stat_here($linenr, $cnt, $here);
5347 WARN("MACRO_WITH_FLOW_CONTROL",
5348 "Macros with flow control statements should be avoided\n" . "$herectx");
5351 # check for line continuations outside of #defines, preprocessor #, and asm
5354 if ($prevline !~ /^..*\\$/ &&
5355 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
5356 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
5357 $line =~ /^\+.*\\$/) {
5358 WARN("LINE_CONTINUATIONS",
5359 "Avoid unnecessary line continuations\n" . $herecurr);
5363 # do {} while (0) macro tests:
5364 # single-statement macros do not need to be enclosed in do while (0) loop,
5365 # macro should not end with a semicolon
5366 if ($perl_version_ok &&
5367 $realfile !~ m@/vmlinux.lds.h$@ &&
5368 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5371 my ($off, $dstat, $dcond, $rest);
5373 ($dstat, $dcond, $ln, $cnt, $off) =
5374 ctx_statement_block($linenr, $realcnt, 0);
5377 $dstat =~ s/\\\n.//g;
5380 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5385 my $cnt = statement_rawlines($ctx);
5386 my $herectx = get_stat_here($linenr, $cnt, $here);
5388 if (($stmts =~ tr/;/;/) == 1 &&
5389 $stmts !~ /^\s*(if|while|for|switch)\b/) {
5390 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5391 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5393 if (defined $semis && $semis ne "") {
5394 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5395 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5397 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5399 my $cnt = statement_rawlines($ctx);
5400 my $herectx = get_stat_here($linenr, $cnt, $here);
5402 WARN("TRAILING_SEMICOLON",
5403 "macros should not use a trailing semicolon\n" . "$herectx");
5407 # check for redundant bracing round if etc
5408 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5409 my ($level, $endln, @chunks) =
5410 ctx_statement_full($linenr, $realcnt, 1);
5411 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5412 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5413 if ($#chunks > 0 && $level == 0) {
5417 my $herectx = $here . "\n";
5418 my $ln = $linenr - 1;
5419 for my $chunk (@chunks) {
5420 my ($cond, $block) = @{$chunk};
5422 # If the condition carries leading newlines, then count those as offsets.
5423 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5424 my $offset = statement_rawlines($whitespace) - 1;
5426 $allowed[$allow] = 0;
5427 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5429 # We have looked at and allowed this specific line.
5430 $suppress_ifbraces{$ln + $offset} = 1;
5432 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5433 $ln += statement_rawlines($block) - 1;
5435 substr($block, 0, length($cond), '');
5437 $seen++ if ($block =~ /^\s*{/);
5439 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5440 if (statement_lines($cond) > 1) {
5441 #print "APW: ALLOWED: cond<$cond>\n";
5442 $allowed[$allow] = 1;
5444 if ($block =~/\b(?:if|for|while)\b/) {
5445 #print "APW: ALLOWED: block<$block>\n";
5446 $allowed[$allow] = 1;
5448 if (statement_block_size($block) > 1) {
5449 #print "APW: ALLOWED: lines block<$block>\n";
5450 $allowed[$allow] = 1;
5455 my $sum_allowed = 0;
5456 foreach (@allowed) {
5459 if ($sum_allowed == 0) {
5461 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5462 } elsif ($sum_allowed != $allow &&
5465 "braces {} should be used on all arms of this statement\n" . $herectx);
5470 if (!defined $suppress_ifbraces{$linenr - 1} &&
5471 $line =~ /\b(if|while|for|else)\b/) {
5474 # Check the pre-context.
5475 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5476 #print "APW: ALLOWED: pre<$1>\n";
5480 my ($level, $endln, @chunks) =
5481 ctx_statement_full($linenr, $realcnt, $-[0]);
5483 # Check the condition.
5484 my ($cond, $block) = @{$chunks[0]};
5485 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5486 if (defined $cond) {
5487 substr($block, 0, length($cond), '');
5489 if (statement_lines($cond) > 1) {
5490 #print "APW: ALLOWED: cond<$cond>\n";
5493 if ($block =~/\b(?:if|for|while)\b/) {
5494 #print "APW: ALLOWED: block<$block>\n";
5497 if (statement_block_size($block) > 1) {
5498 #print "APW: ALLOWED: lines block<$block>\n";
5501 # Check the post-context.
5502 if (defined $chunks[1]) {
5503 my ($cond, $block) = @{$chunks[1]};
5504 if (defined $cond) {
5505 substr($block, 0, length($cond), '');
5507 if ($block =~ /^\s*\{/) {
5508 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5512 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5513 my $cnt = statement_rawlines($block);
5514 my $herectx = get_stat_here($linenr, $cnt, $here);
5517 "braces {} are not necessary for single statement blocks\n" . $herectx);
5521 # check for single line unbalanced braces
5522 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5523 $sline =~ /^.\s*else\s*\{\s*$/) {
5524 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5527 # check for unnecessary blank lines around braces
5528 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5530 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5531 $fix && $prevrawline =~ /^\+/) {
5532 fix_delete_line($fixlinenr - 1, $prevrawline);
5535 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5537 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5539 fix_delete_line($fixlinenr, $rawline);
5543 # no volatiles please
5544 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5545 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5547 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
5550 # Check for user-visible strings broken across lines, which breaks the ability
5551 # to grep for the string. Make exceptions when the previous string ends in a
5552 # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5553 # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
5554 if ($line =~ /^\+\s*$String/ &&
5555 $prevline =~ /"\s*$/ &&
5556 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5557 if (WARN("SPLIT_STRING",
5558 "quoted string split across lines\n" . $hereprev) &&
5560 $prevrawline =~ /^\+.*"\s*$/ &&
5561 $last_coalesced_string_linenr != $linenr - 1) {
5562 my $extracted_string = get_quoted_string($line, $rawline);
5563 my $comma_close = "";
5564 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5568 fix_delete_line($fixlinenr - 1, $prevrawline);
5569 fix_delete_line($fixlinenr, $rawline);
5570 my $fixedline = $prevrawline;
5571 $fixedline =~ s/"\s*$//;
5572 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5573 fix_insert_line($fixlinenr - 1, $fixedline);
5574 $fixedline = $rawline;
5575 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5576 if ($fixedline !~ /\+\s*$/) {
5577 fix_insert_line($fixlinenr, $fixedline);
5579 $last_coalesced_string_linenr = $linenr;
5583 # check for missing a space in a string concatenation
5584 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5585 WARN('MISSING_SPACE',
5586 "break quoted strings at a space character\n" . $hereprev);
5589 # check for an embedded function name in a string when the function is known
5590 # This does not work very well for -f --file checking as it depends on patch
5591 # context providing the function name or a single line form for in-file
5592 # function declarations
5593 if ($line =~ /^\+.*$String/ &&
5594 defined($context_function) &&
5595 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5596 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
5597 WARN("EMBEDDED_FUNCTION_NAME",
5598 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
5601 # check for spaces before a quoted newline
5602 if ($rawline =~ /^.*\".*\s\\n/) {
5603 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5604 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5606 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5611 # concatenated string without spaces between elements
5612 if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5613 if (CHK("CONCATENATED_STRING",
5614 "Concatenated strings should use spaces between elements\n" . $herecurr) &&
5616 while ($line =~ /($String)/g) {
5617 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5618 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
5619 $fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
5624 # uncoalesced string fragments
5625 if ($line =~ /$String\s*"/) {
5626 if (WARN("STRING_FRAGMENTS",
5627 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
5629 while ($line =~ /($String)(?=\s*")/g) {
5630 my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
5631 $fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
5636 # check for non-standard and hex prefixed decimal printf formats
5637 my $show_L = 1; #don't show the same defect twice
5639 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5640 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
5641 $string =~ s/%%/__/g;
5643 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
5645 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5649 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5651 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5654 # check for 0x<decimal>
5655 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5656 ERROR("PRINTF_0XDECIMAL",
5657 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5661 # check for line continuations in quoted strings with odd counts of "
5662 if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
5663 WARN("LINE_CONTINUATIONS",
5664 "Avoid line continuations in quoted strings\n" . $herecurr);
5668 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
5670 "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
5674 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5676 "Consider removing the #if 1 and its #endif\n" . $herecurr);
5679 # check for needless "if (<foo>) fn(<foo>)" uses
5680 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5681 my $tested = quotemeta($1);
5682 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5683 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5685 if (WARN('NEEDLESS_IF',
5686 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5689 my $leading_tabs = "";
5690 my $new_leading_tabs = "";
5691 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5696 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5697 $new_leading_tabs = $1;
5698 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5705 fix_delete_line($fixlinenr - 1, $prevrawline);
5706 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5712 # check for unnecessary "Out of Memory" messages
5713 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5714 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5715 (defined $1 || defined $3) &&
5718 my $testline = $lines[$linenr - 3];
5720 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5721 # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5723 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5724 $s !~ /\b__GFP_NOWARN\b/ ) {
5726 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5730 # check for logging functions with KERN_<LEVEL>
5731 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5732 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5734 if (WARN("UNNECESSARY_KERN_LEVEL",
5735 "Possible unnecessary $level\n" . $herecurr) &&
5737 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5741 # check for logging continuations
5742 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5743 WARN("LOGGING_CONTINUATION",
5744 "Avoid logging continuation uses where feasible\n" . $herecurr);
5747 # check for mask then right shift without a parentheses
5748 if ($perl_version_ok &&
5749 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5750 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5751 WARN("MASK_THEN_SHIFT",
5752 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5755 # check for pointer comparisons to NULL
5756 if ($perl_version_ok) {
5757 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5760 $equal = "" if ($4 eq "!=");
5761 if (CHK("COMPARISON_TO_NULL",
5762 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5764 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5769 # check for bad placement of section $InitAttribute (e.g.: __initdata)
5770 if ($line =~ /(\b$InitAttribute\b)/) {
5772 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5775 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5776 ERROR("MISPLACED_INIT",
5777 "$attr should be placed after $var\n" . $herecurr)) ||
5778 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5779 WARN("MISPLACED_INIT",
5780 "$attr should be placed after $var\n" . $herecurr))) &&
5782 $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;
5787 # check for $InitAttributeData (ie: __initdata) with const
5788 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5790 $attr =~ /($InitAttributePrefix)(.*)/;
5791 my $attr_prefix = $1;
5793 if (ERROR("INIT_ATTRIBUTE",
5794 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5796 $fixed[$fixlinenr] =~
5797 s/$InitAttributeData/${attr_prefix}initconst/;
5801 # check for $InitAttributeConst (ie: __initconst) without const
5802 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5804 if (ERROR("INIT_ATTRIBUTE",
5805 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5807 my $lead = $fixed[$fixlinenr] =~
5808 /(^\+\s*(?:static\s+))/;
5810 $lead = "$lead " if ($lead !~ /^\+$/);
5811 $lead = "${lead}const ";
5812 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5816 # check for __read_mostly with const non-pointer (should just be const)
5817 if ($line =~ /\b__read_mostly\b/ &&
5818 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5819 if (ERROR("CONST_READ_MOSTLY",
5820 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5822 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5826 # don't use __constant_<foo> functions outside of include/uapi/
5827 if ($realfile !~ m@^include/uapi/@ &&
5828 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5829 my $constant_func = $1;
5830 my $func = $constant_func;
5831 $func =~ s/^__constant_//;
5832 if (WARN("CONSTANT_CONVERSION",
5833 "$constant_func should be $func\n" . $herecurr) &&
5835 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5839 # prefer usleep_range over udelay
5840 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
5842 # ignore udelay's < 10, however
5843 if (! ($delay < 10) ) {
5845 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
5847 if ($delay > 2000) {
5849 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
5853 # warn about unexpectedly long msleep's
5854 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5857 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
5861 # check for comparisons of jiffies
5862 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5863 WARN("JIFFIES_COMPARISON",
5864 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5867 # check for comparisons of get_jiffies_64()
5868 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5869 WARN("JIFFIES_COMPARISON",
5870 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5873 # warn about #ifdefs in C files
5874 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
5875 # print "#ifdef in C files should be avoided\n";
5876 # print "$herecurr";
5880 # warn about spacing in #ifdefs
5881 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
5882 if (ERROR("SPACING",
5883 "exactly one space required after that #$1\n" . $herecurr) &&
5885 $fixed[$fixlinenr] =~
5886 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5891 # check for spinlock_t definitions without a comment.
5892 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5893 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
5895 if (!ctx_has_comment($first_line, $linenr)) {
5896 CHK("UNCOMMENTED_DEFINITION",
5897 "$1 definition without comment\n" . $herecurr);
5900 # check for memory barriers without a comment.
5907 my $barrier_stems = qr{
5915 my $all_barriers = qr{
5917 smp_(?:$barrier_stems)|
5918 virt_(?:$barrier_stems)
5921 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
5922 if (!ctx_has_comment($first_line, $linenr)) {
5923 WARN("MEMORY_BARRIER",
5924 "memory barrier without comment\n" . $herecurr);
5928 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5930 if ($realfile !~ m@^include/asm-generic/@ &&
5931 $realfile !~ m@/barrier\.h$@ &&
5932 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5933 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5934 WARN("MEMORY_BARRIER",
5935 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5938 # check for waitqueue_active without a comment.
5939 if ($line =~ /\bwaitqueue_active\s*\(/) {
5940 if (!ctx_has_comment($first_line, $linenr)) {
5941 WARN("WAITQUEUE_ACTIVE",
5942 "waitqueue_active without comment\n" . $herecurr);
5946 # check for data_race without a comment.
5947 if ($line =~ /\bdata_race\s*\(/) {
5948 if (!ctx_has_comment($first_line, $linenr)) {
5950 "data_race without comment\n" . $herecurr);
5954 # check of hardware specific defines
5955 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5957 "architecture specific defines should be avoided\n" . $herecurr);
5960 # check that the storage class is not after a type
5961 if ($line =~ /\b($Type)\s+($Storage)\b/) {
5962 WARN("STORAGE_CLASS",
5963 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5965 # Check that the storage class is at the beginning of a declaration
5966 if ($line =~ /\b$Storage\b/ &&
5967 $line !~ /^.\s*$Storage/ &&
5968 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5969 $1 !~ /[\,\)]\s*$/) {
5970 WARN("STORAGE_CLASS",
5971 "storage class should be at the beginning of the declaration\n" . $herecurr);
5974 # check the location of the inline attribute, that it is between
5975 # storage class and type.
5976 if ($line =~ /\b$Type\s+$Inline\b/ ||
5977 $line =~ /\b$Inline\s+$Storage\b/) {
5978 ERROR("INLINE_LOCATION",
5979 "inline keyword should sit between storage class and type\n" . $herecurr);
5982 # Check for __inline__ and __inline, prefer inline
5983 if ($realfile !~ m@\binclude/uapi/@ &&
5984 $line =~ /\b(__inline__|__inline)\b/) {
5986 "plain inline is preferred over $1\n" . $herecurr) &&
5988 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5993 # Check for __attribute__ packed, prefer __packed
5994 if ($realfile !~ m@\binclude/uapi/@ &&
5995 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5996 WARN("PREFER_PACKED",
5997 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
6000 # Check for __attribute__ aligned, prefer __aligned
6001 if ($realfile !~ m@\binclude/uapi/@ &&
6002 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
6003 WARN("PREFER_ALIGNED",
6004 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
6007 # Check for __attribute__ section, prefer __section
6008 if ($realfile !~ m@\binclude/uapi/@ &&
6009 $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
6010 my $old = substr($rawline, $-[1], $+[1] - $-[1]);
6011 my $new = substr($old, 1, -1);
6012 if (WARN("PREFER_SECTION",
6013 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
6015 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
6019 # Check for __attribute__ format(printf, prefer __printf
6020 if ($realfile !~ m@\binclude/uapi/@ &&
6021 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
6022 if (WARN("PREFER_PRINTF",
6023 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
6025 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
6030 # Check for __attribute__ format(scanf, prefer __scanf
6031 if ($realfile !~ m@\binclude/uapi/@ &&
6032 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
6033 if (WARN("PREFER_SCANF",
6034 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
6036 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
6040 # Check for __attribute__ weak, or __weak declarations (may have link issues)
6041 if ($perl_version_ok &&
6042 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6043 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6044 $line =~ /\b__weak\b/)) {
6045 ERROR("WEAK_DECLARATION",
6046 "Using weak declarations can have unintended link defects\n" . $herecurr);
6049 # check for c99 types like uint8_t used outside of uapi/ and tools/
6050 if ($realfile !~ m@\binclude/uapi/@ &&
6051 $realfile !~ m@\btools/@ &&
6052 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6054 if ($type =~ /\b($typeC99Typedefs)\b/) {
6056 my $kernel_type = 'u';
6057 $kernel_type = 's' if ($type =~ /^_*[si]/);
6060 if (CHK("PREFER_KERNEL_TYPES",
6061 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6063 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6068 # check for cast of C90 native int or longer types constants
6069 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6072 if (WARN("TYPECAST_INT_CONSTANT",
6073 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
6076 my $newconst = $const;
6077 $newconst =~ s/${Int_type}$//;
6078 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6079 if ($cast =~ /\blong\s+long\b/) {
6081 } elsif ($cast =~ /\blong\b/) {
6084 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6088 # check for sizeof(&)
6089 if ($line =~ /\bsizeof\s*\(\s*\&/) {
6090 WARN("SIZEOF_ADDRESS",
6091 "sizeof(& should be avoided\n" . $herecurr);
6094 # check for sizeof without parenthesis
6095 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6096 if (WARN("SIZEOF_PARENTHESIS",
6097 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6099 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6103 # check for struct spinlock declarations
6104 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6105 WARN("USE_SPINLOCK_T",
6106 "struct spinlock should be spinlock_t\n" . $herecurr);
6109 # check for seq_printf uses that could be seq_puts
6110 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6111 my $fmt = get_quoted_string($line, $rawline);
6114 if (WARN("PREFER_SEQ_PUTS",
6115 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6117 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6122 # check for vsprintf extension %p<foo> misuses
6123 if ($perl_version_ok &&
6125 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6126 $1 !~ /^_*volatile_*$/) {
6129 my $lc = $stat =~ tr@\n@@;
6130 $lc = $lc + $linenr;
6131 for (my $count = $linenr; $count <= $lc; $count++) {
6135 my $bad_specifier = "";
6136 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6139 while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6143 if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
6144 ($extension eq "f" &&
6145 defined $qualifier && $qualifier !~ /^w/)) {
6146 $bad_specifier = $specifier;
6149 if ($extension eq "x" && !defined($stat_real)) {
6150 if (!defined($stat_real)) {
6151 $stat_real = get_stat_real($linenr, $lc);
6153 WARN("VSPRINTF_SPECIFIER_PX",
6154 "Using vsprintf specifier '\%px' potentially exposes the kernel memory layout, if you don't really need the address please consider using '\%p'.\n" . "$here\n$stat_real\n");
6157 if ($bad_specifier ne "") {
6158 my $stat_real = get_stat_real($linenr, $lc);
6159 my $ext_type = "Invalid";
6161 if ($bad_specifier =~ /p[Ff]/) {
6162 $use = " - use %pS instead";
6163 $use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6166 WARN("VSPRINTF_POINTER_EXTENSION",
6167 "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6172 # Check for misused memsets
6173 if ($perl_version_ok &&
6175 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6181 if ($ms_size =~ /^(0x|)0$/i) {
6183 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6184 } elsif ($ms_size =~ /^(0x|)1$/i) {
6186 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6190 # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
6191 # if ($perl_version_ok &&
6193 # $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6194 # if (WARN("PREFER_ETHER_ADDR_COPY",
6195 # "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6197 # $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6201 # Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
6202 # if ($perl_version_ok &&
6204 # $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6205 # WARN("PREFER_ETHER_ADDR_EQUAL",
6206 # "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6209 # check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6210 # check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
6211 # if ($perl_version_ok &&
6213 # $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6217 # if ($ms_val =~ /^(?:0x|)0+$/i) {
6218 # if (WARN("PREFER_ETH_ZERO_ADDR",
6219 # "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6221 # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6223 # } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6224 # if (WARN("PREFER_ETH_BROADCAST_ADDR",
6225 # "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6227 # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6232 # typecasts on min/max could be min_t/max_t
6233 if ($perl_version_ok &&
6235 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6236 if (defined $2 || defined $7) {
6238 my $cast1 = deparenthesize($2);
6240 my $cast2 = deparenthesize($7);
6244 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6245 $cast = "$cast1 or $cast2";
6246 } elsif ($cast1 ne "") {
6252 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6256 # check usleep_range arguments
6257 if ($perl_version_ok &&
6259 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6263 WARN("USLEEP_RANGE",
6264 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
6265 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6267 WARN("USLEEP_RANGE",
6268 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
6272 # check for naked sscanf
6273 if ($perl_version_ok &&
6275 $line =~ /\bsscanf\b/ &&
6276 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6277 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6278 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6279 my $lc = $stat =~ tr@\n@@;
6280 $lc = $lc + $linenr;
6281 my $stat_real = get_stat_real($linenr, $lc);
6282 WARN("NAKED_SSCANF",
6283 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6286 # check for simple sscanf that should be kstrto<foo>
6287 if ($perl_version_ok &&
6289 $line =~ /\bsscanf\b/) {
6290 my $lc = $stat =~ tr@\n@@;
6291 $lc = $lc + $linenr;
6292 my $stat_real = get_stat_real($linenr, $lc);
6293 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6295 my $count = $format =~ tr@%@%@;
6297 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6298 WARN("SSCANF_TO_KSTRTO",
6299 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6304 # check for new externs in .h files.
6305 if ($realfile =~ /\.h$/ &&
6306 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6307 if (CHK("AVOID_EXTERNS",
6308 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
6310 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
6314 # check for new externs in .c files.
6315 if ($realfile =~ /\.c$/ && defined $stat &&
6316 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6318 my $function_name = $1;
6319 my $paren_space = $2;
6322 if (defined $cond) {
6323 substr($s, 0, length($cond), '');
6327 WARN("AVOID_EXTERNS",
6328 "externs should be avoided in .c files\n" . $herecurr);
6331 if ($paren_space =~ /\n/) {
6332 WARN("FUNCTION_ARGUMENTS",
6333 "arguments for function declarations should follow identifier\n" . $herecurr);
6336 } elsif ($realfile =~ /\.c$/ && defined $stat &&
6337 $stat =~ /^.\s*extern\s+/)
6339 WARN("AVOID_EXTERNS",
6340 "externs should be avoided in .c files\n" . $herecurr);
6343 # check for function declarations that have arguments without identifier names
6344 if (defined $stat &&
6345 $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6347 my $args = trim($1);
6348 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6350 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6351 WARN("FUNCTION_ARGUMENTS",
6352 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6357 # check for function definitions
6358 if ($perl_version_ok &&
6360 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6361 $context_function = $1;
6363 # check for multiline function definition with misplaced open brace
6365 my $cnt = statement_rawlines($stat);
6366 my $herectx = $here . "\n";
6367 for (my $n = 0; $n < $cnt; $n++) {
6368 my $rl = raw_line($linenr, $n);
6369 $herectx .= $rl . "\n";
6370 $ok = 1 if ($rl =~ /^[ \+]\{/);
6371 $ok = 1 if ($rl =~ /\{/ && $n == 0);
6372 last if $rl =~ /^[ \+].*\{/;
6376 "open brace '{' following function definitions go on the next line\n" . $herectx);
6380 # checks for new __setup's
6381 if ($rawline =~ /\b__setup\("([^"]*)"/) {
6384 if (!grep(/$name/, @setup_docs)) {
6385 CHK("UNDOCUMENTED_SETUP",
6386 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
6390 # check for pointless casting of alloc functions
6391 if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
6392 WARN("UNNECESSARY_CASTS",
6393 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
6397 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
6398 if ($perl_version_ok &&
6399 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6400 CHK("ALLOC_SIZEOF_STRUCT",
6401 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6404 # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
6405 if ($perl_version_ok &&
6407 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
6411 my $newfunc = "kmalloc_array";
6412 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
6415 if ($a1 =~ /^sizeof\s*\S/) {
6419 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6420 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
6421 my $cnt = statement_rawlines($stat);
6422 my $herectx = get_stat_here($linenr, $cnt, $here);
6424 if (WARN("ALLOC_WITH_MULTIPLY",
6425 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6428 $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;
6433 # check for krealloc arg reuse
6434 if ($perl_version_ok &&
6435 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
6437 WARN("KREALLOC_ARG_REUSE",
6438 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6441 # check for alloc argument mismatch
6442 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6443 WARN("ALLOC_ARRAY_ARGS",
6444 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6447 # check for multiple semicolons
6448 if ($line =~ /;\s*;\s*$/) {
6449 if (WARN("ONE_SEMICOLON",
6450 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6452 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6456 # check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6457 if ($realfile !~ m@^include/uapi/@ &&
6458 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
6460 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6461 if (CHK("BIT_MACRO",
6462 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6464 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6468 # check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6469 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6471 if (WARN("PREFER_IS_ENABLED",
6472 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6474 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6478 # check for case / default statements not preceded by break/fallthrough/switch
6479 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6481 my $has_statement = 0;
6483 my $prevline = $linenr;
6484 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6486 my $rline = $rawlines[$prevline - 1];
6487 my $fline = $lines[$prevline - 1];
6488 last if ($fline =~ /^\@\@/);
6489 next if ($fline =~ /^\-/);
6490 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6491 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6492 next if ($fline =~ /^.[\s$;]*$/);
6495 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
6497 if (!$has_break && $has_statement) {
6498 WARN("MISSING_BREAK",
6499 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6503 # check for /* fallthrough */ like comment, prefer fallthrough;
6504 my @fallthroughs = (
6507 'lint -fallthrough[ \t]*',
6508 'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
6509 '(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
6510 'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6511 'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
6513 if ($raw_comment ne '') {
6514 foreach my $ft (@fallthroughs) {
6515 if ($raw_comment =~ /$ft/) {
6516 my $msg_level = \&WARN;
6517 $msg_level = \&CHK if ($file);
6518 &{$msg_level}("PREFER_FALLTHROUGH",
6519 "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
6525 # check for switch/default statements without a break;
6526 if ($perl_version_ok &&
6528 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6529 my $cnt = statement_rawlines($stat);
6530 my $herectx = get_stat_here($linenr, $cnt, $here);
6532 WARN("DEFAULT_NO_BREAK",
6533 "switch default: should use break\n" . $herectx);
6536 # check for gcc specific __FUNCTION__
6537 if ($line =~ /\b__FUNCTION__\b/) {
6538 if (WARN("USE_FUNC",
6539 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6541 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6545 # check for uses of __DATE__, __TIME__, __TIMESTAMP__
6546 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6548 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6551 # check for use of yield()
6552 if ($line =~ /\byield\s*\(\s*\)/) {
6554 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6557 # check for comparisons against true and false
6558 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6566 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6568 my $type = lc($otype);
6569 if ($type =~ /^(?:true|false)$/) {
6570 if (("$test" eq "==" && "$type" eq "true") ||
6571 ("$test" eq "!=" && "$type" eq "false")) {
6575 CHK("BOOL_COMPARISON",
6576 "Using comparison to $otype is error prone\n" . $herecurr);
6578 ## maybe suggesting a correct construct would better
6579 ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6584 # check for semaphores initialized locked
6585 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6586 WARN("CONSIDER_COMPLETION",
6587 "consider using a completion\n" . $herecurr);
6590 # recommend kstrto* over simple_strto* and strict_strto*
6591 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6592 WARN("CONSIDER_KSTRTO",
6593 "$1 is obsolete, use k$3 instead\n" . $herecurr);
6596 # check for __initcall(), use device_initcall() explicitly or more appropriate function please
6597 if ($line =~ /^.\s*__initcall\s*\(/) {
6598 WARN("USE_DEVICE_INITCALL",
6599 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6602 # check for spin_is_locked(), suggest lockdep instead
6603 if ($line =~ /\bspin_is_locked\(/) {
6605 "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
6608 # check for deprecated apis
6609 if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
6610 my $deprecated_api = $1;
6611 my $new_api = $deprecated_apis{$deprecated_api};
6612 WARN("DEPRECATED_API",
6613 "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
6616 # check for various structs that are normally const (ops, kgdb, device_tree)
6617 # and avoid what seem like struct definitions 'struct foo {'
6618 if ($line !~ /\bconst\b/ &&
6619 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6620 WARN("CONST_STRUCT",
6621 "struct $1 should normally be const\n" . $herecurr);
6624 # use of NR_CPUS is usually wrong
6625 # ignore definitions of NR_CPUS and usage to define arrays as likely right
6626 if ($line =~ /\bNR_CPUS\b/ &&
6627 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6628 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6629 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6630 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6631 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6634 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6637 # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6638 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6639 ERROR("DEFINE_ARCH_HAS",
6640 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6643 # likely/unlikely comparisons similar to "(likely(foo) > 0)"
6644 if ($perl_version_ok &&
6645 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6646 WARN("LIKELY_MISUSE",
6647 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6650 # nested likely/unlikely calls
6651 if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6652 WARN("LIKELY_MISUSE",
6653 "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6656 # whine mightly about in_atomic
6657 if ($line =~ /\bin_atomic\s*\(/) {
6658 if ($realfile =~ m@^drivers/@) {
6660 "do not use in_atomic in drivers\n" . $herecurr);
6661 } elsif ($realfile !~ m@^kernel/@) {
6663 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6667 # check for mutex_trylock_recursive usage
6668 if ($line =~ /mutex_trylock_recursive/) {
6670 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6673 # check for lockdep_set_novalidate_class
6674 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6675 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6676 if ($realfile !~ m@^kernel/lockdep@ &&
6677 $realfile !~ m@^include/linux/lockdep@ &&
6678 $realfile !~ m@^drivers/base/core@) {
6680 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
6684 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6685 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6686 WARN("EXPORTED_WORLD_WRITABLE",
6687 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
6690 # check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
6691 # and whether or not function naming is typical and if
6692 # DEVICE_ATTR permissions uses are unusual too
6693 if ($perl_version_ok &&
6695 $stat =~ /\bDEVICE_ATTR\s*\(\s*(\w+)\s*,\s*\(?\s*(\s*(?:${multi_mode_perms_string_search}|0[0-7]{3,3})\s*)\s*\)?\s*,\s*(\w+)\s*,\s*(\w+)\s*\)/) {
6700 my $octal_perms = perms_to_octal($perms);
6701 if ($show =~ /^${var}_show$/ &&
6702 $store =~ /^${var}_store$/ &&
6703 $octal_perms eq "0644") {
6704 if (WARN("DEVICE_ATTR_RW",
6705 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
6707 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
6709 } elsif ($show =~ /^${var}_show$/ &&
6710 $store =~ /^NULL$/ &&
6711 $octal_perms eq "0444") {
6712 if (WARN("DEVICE_ATTR_RO",
6713 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
6715 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
6717 } elsif ($show =~ /^NULL$/ &&
6718 $store =~ /^${var}_store$/ &&
6719 $octal_perms eq "0200") {
6720 if (WARN("DEVICE_ATTR_WO",
6721 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
6723 $fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
6725 } elsif ($octal_perms eq "0644" ||
6726 $octal_perms eq "0444" ||
6727 $octal_perms eq "0200") {
6728 my $newshow = "$show";
6729 $newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
6730 my $newstore = $store;
6731 $newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
6733 if ($show ne $newshow) {
6734 $rename .= " '$show' to '$newshow'";
6736 if ($store ne $newstore) {
6737 $rename .= " '$store' to '$newstore'";
6739 WARN("DEVICE_ATTR_FUNCTIONS",
6740 "Consider renaming function(s)$rename\n" . $herecurr);
6742 WARN("DEVICE_ATTR_PERMS",
6743 "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
6747 # Mode permission misuses where it seems decimal should be octal
6748 # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6749 # o Ignore module_param*(...) uses with a decimal 0 permission as that has a
6750 # specific definition of not visible in sysfs.
6751 # o Ignore proc_create*(...) uses with a decimal 0 permission as that means
6752 # use the default permissions
6753 if ($perl_version_ok &&
6755 $line =~ /$mode_perms_search/) {
6756 foreach my $entry (@mode_permission_funcs) {
6757 my $func = $entry->[0];
6758 my $arg_pos = $entry->[1];
6760 my $lc = $stat =~ tr@\n@@;
6761 $lc = $lc + $linenr;
6762 my $stat_real = get_stat_real($linenr, $lc);
6767 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6769 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6770 if ($stat =~ /$test/) {
6772 $val = $6 if ($skip_args ne "");
6773 if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
6774 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6775 ($val =~ /^$Octal$/ && length($val) ne 4))) {
6776 ERROR("NON_OCTAL_PERMISSIONS",
6777 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6779 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6780 ERROR("EXPORTED_WORLD_WRITABLE",
6781 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
6787 # check for uses of S_<PERMS> that could be octal for readability
6788 while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
6790 my $octal = perms_to_octal($oval);
6791 if (WARN("SYMBOLIC_PERMS",
6792 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6794 $fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
6798 # validate content of MODULE_LICENSE against list from include/linux/module.h
6799 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6800 my $extracted_string = get_quoted_string($line, $rawline);
6801 my $valid_licenses = qr{
6804 GPL\ and\ additional\ rights|
6810 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6811 WARN("MODULE_LICENSE",
6812 "unknown module license " . $extracted_string . "\n" . $herecurr);
6816 # check for sysctl duplicate constants
6817 if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
6818 WARN("DUPLICATED_SYSCTL_CONST",
6819 "duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
6823 # If we have no input at all, then there is nothing to report on
6824 # so just keep quiet.
6825 if ($#rawlines == -1) {
6829 # In mailback mode only produce a report in the negative, for
6830 # things that appear to be patches.
6831 if ($mailback && ($clean == 1 || !$is_patch)) {
6835 # This is not a patch, and we are are in 'no-patch' mode so
6837 if (!$chk_patch && !$is_patch) {
6841 if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
6842 ERROR("NOT_UNIFIED_DIFF",
6843 "Does not appear to be a unified-diff format patch\n");
6845 if ($is_patch && $has_commit_log && $chk_signoff) {
6846 if ($signoff == 0) {
6847 ERROR("MISSING_SIGN_OFF",
6848 "Missing Signed-off-by: line(s)\n");
6849 } elsif (!$authorsignoff) {
6850 WARN("NO_AUTHOR_SIGN_OFF",
6851 "Missing Signed-off-by: line by nominal patch author '$author'\n");
6855 print report_dump();
6856 if ($summary && !($clean == 1 && $quiet == 1)) {
6857 print "$filename " if ($summary_file);
6858 print "total: $cnt_error errors, $cnt_warn warnings, " .
6859 (($check)? "$cnt_chk checks, " : "") .
6860 "$cnt_lines lines checked\n";
6864 # If there were any defects found and not already fixing them
6865 if (!$clean and !$fix) {
6868 NOTE: For some of the reported defects, checkpatch may be able to
6869 mechanically convert to the typical style using --fix or --fix-inplace.
6872 # If there were whitespace errors which cleanpatch can fix
6873 # then suggest that.
6874 if ($rpt_cleaners) {
6878 NOTE: Whitespace errors detected.
6879 You may wish to use scripts/cleanpatch or scripts/cleanfile
6884 if ($clean == 0 && $fix &&
6885 ("@rawlines" ne "@fixed" ||
6886 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
6887 my $newfile = $filename;
6888 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
6892 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6894 open($f, '>', $newfile)
6895 or die "$P: Can't open $newfile for write\n";
6896 foreach my $fixed_line (@fixed) {
6899 if ($linecount > 3) {
6900 $fixed_line =~ s/^\+//;
6901 print $f $fixed_line . "\n";
6904 print $f $fixed_line . "\n";
6912 Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6914 Do _NOT_ trust the results written to this file.
6915 Do _NOT_ submit these changes without inspecting them for correctness.
6917 This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6918 No warranties, expressed or implied...
6926 print "$vname has no obvious style problems and is ready for submission.\n";
6928 print "$vname has style problems, please review.\n";