2 # (c) 2001, Dave Jones. (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 # (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5 # (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6 # Licensed under the terms of the GNU GPL License version 2
12 use Term::ANSIColor qw(:constants);
15 my $D = dirname(abs_path($P));
19 use Getopt::Long qw(:config no_auto_abbrev);
49 my $configuration_file = ".checkpatch.conf";
50 my $max_line_length = 80;
51 my $ignore_perl_version = 0;
52 my $minimum_perl_version = 5.10.0;
53 my $min_conf_desc_length = 4;
54 my $spelling_file = "$D/spelling.txt";
56 my $codespellfile = "/usr/share/codespell/dictionary.txt";
57 my $conststructsfile = "$D/const_structs.checkpatch";
58 my $typedefsfile = "";
60 my $allow_c99_comments = 1;
66 Usage: $P [OPTION]... [FILE]...
71 --no-tree run without a kernel tree
72 --no-signoff do not check for 'Signed-off-by' line
73 --patch treat FILE as patchfile (default)
74 --emacs emacs compile window format
75 --terse one line per report
76 --showfile emit diffed file position, not input file position
77 -g, --git treat FILE as a single commit or git revision range
78 single git commit with:
82 multiple git commits with:
86 git merges are ignored
87 -f, --file treat FILE as regular source file
88 --subjective, --strict enable more subjective tests
89 --list-types list the possible message types
90 --types TYPE(,TYPE2...) show only these comma separated message types
91 --ignore TYPE(,TYPE2...) ignore various comma separated message types
92 --show-types show the specific message type in the output
93 --max-line-length=n set the maximum line length, if exceeded, warn
94 --min-conf-desc-length=n set the min description length, if shorter, warn
95 --root=PATH PATH to the kernel tree root
96 --no-summary suppress the per-file summary
97 --mailback only produce a report in case of warnings/errors
98 --summary-file include the filename in summary
99 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
100 'values', 'possible', 'type', and 'attr' (default
102 --test-only=WORD report only warnings/errors containing WORD
104 --fix EXPERIMENTAL - may create horrible results
105 If correctable single-line errors exist, create
106 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
107 with potential errors corrected to the preferred
109 --fix-inplace EXPERIMENTAL - may create horrible results
110 Is the same as --fix, but overwrites the input
111 file. It's your fault if there's no backup or git
112 --ignore-perl-version override checking of perl version. expect
114 --codespell Use the codespell dictionary for spelling/typos
115 (default:/usr/share/codespell/dictionary.txt)
116 --codespellfile Use this codespell dictionary
117 --typedefsfile Read additional types from this file
118 --color Use colors when output is STDOUT (default: on)
119 -h, --help, --version display this help and exit
121 When FILE is - read standard input.
129 return grep { !$seen{$_}++ } @_;
139 open(my $script, '<', abs_path($P)) or
140 die "$P: Can't read '$P' $!\n";
142 my $text = <$script>;
146 for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
149 @types = sort(uniq(@types));
150 print("#\tMessage type\n\n");
151 foreach my $type (@types) {
152 print(++$count . "\t" . $type . "\n");
158 my $conf = which_conf($configuration_file);
161 open(my $conffile, '<', "$conf")
162 or warn "$P: Can't find a readable $configuration_file file $!\n";
164 while (<$conffile>) {
167 $line =~ s/\s*\n?$//g;
171 next if ($line =~ m/^\s*#/);
172 next if ($line =~ m/^\s*$/);
174 my @words = split(" ", $line);
175 foreach my $word (@words) {
176 last if ($word =~ m/^#/);
177 push (@conf_args, $word);
181 unshift(@ARGV, @conf_args) if @conf_args;
185 'q|quiet+' => \$quiet,
187 'signoff!' => \$chk_signoff,
188 'patch!' => \$chk_patch,
191 'showfile!' => \$showfile,
194 'subjective!' => \$check,
195 'strict!' => \$check,
196 'ignore=s' => \@ignore,
198 'show-types!' => \$show_types,
199 'list-types!' => \$list_types,
200 'max-line-length=i' => \$max_line_length,
201 'min-conf-desc-length=i' => \$min_conf_desc_length,
203 'summary!' => \$summary,
204 'mailback!' => \$mailback,
205 'summary-file!' => \$summary_file,
207 'fix-inplace!' => \$fix_inplace,
208 'ignore-perl-version!' => \$ignore_perl_version,
209 'debug=s' => \%debug,
210 'test-only=s' => \$tst_only,
211 'codespell!' => \$codespell,
212 'codespellfile=s' => \$codespellfile,
213 'typedefsfile=s' => \$typedefsfile,
221 list_types(0) if ($list_types);
223 $fix = 1 if ($fix_inplace);
224 $check_orig = $check;
228 if ($^V && $^V lt $minimum_perl_version) {
229 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
230 if (!$ignore_perl_version) {
235 #if no filenames are given, push '-' to read patch from stdin
240 sub hash_save_array_words {
241 my ($hashRef, $arrayRef) = @_;
243 my @array = split(/,/, join(',', @$arrayRef));
244 foreach my $word (@array) {
245 $word =~ s/\s*\n?$//g;
248 $word =~ tr/[a-z]/[A-Z]/;
250 next if ($word =~ m/^\s*#/);
251 next if ($word =~ m/^\s*$/);
257 sub hash_show_words {
258 my ($hashRef, $prefix) = @_;
260 if (keys %$hashRef) {
261 print "\nNOTE: $prefix message types:";
262 foreach my $word (sort keys %$hashRef) {
269 hash_save_array_words(\%ignore_type, \@ignore);
270 hash_save_array_words(\%use_type, \@use);
273 my $dbg_possible = 0;
276 for my $key (keys %debug) {
278 eval "\${dbg_$key} = '$debug{$key}';";
282 my $rpt_cleaners = 0;
291 if (!top_of_kernel_tree($root)) {
292 die "$P: $root: --root does not point at a valid tree\n";
295 if (top_of_kernel_tree('.')) {
297 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
298 top_of_kernel_tree($1)) {
303 if (!defined $root) {
304 print "Must be run from the top-level dir. of a kernel tree\n";
309 my $emitted_corrupt = 0;
312 [A-Za-z_][A-Za-z\d_]*
313 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
315 our $Storage = qr{extern|static|asmlinkage};
328 our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
329 our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
330 our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
331 our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
332 our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
334 # Notes to $Attribute:
335 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
356 ____cacheline_aligned|
357 ____cacheline_aligned_in_smp|
358 ____cacheline_internodealigned_in_smp|
362 our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
363 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
364 our $Lval = qr{$Ident(?:$Member)*};
366 our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
367 our $Binary = qr{(?i)0b[01]+$Int_type?};
368 our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
369 our $Int = qr{[0-9]+$Int_type?};
370 our $Octal = qr{0[0-7]+$Int_type?};
371 our $String = qr{"[X\t]*"};
372 our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
373 our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
374 our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
375 our $Float = qr{$Float_hex|$Float_dec|$Float_int};
376 our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
377 our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
378 our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
379 our $Arithmetic = qr{\+|-|\*|\/|%};
383 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
386 our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
390 our $NonptrTypeMisordered;
391 our $NonptrTypeWithAttr;
395 our $DeclareMisordered;
397 our $NON_ASCII_UTF8 = qr{
398 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
399 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
400 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
401 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
402 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
403 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
404 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
408 [\x09\x0A\x0D\x20-\x7E] # ASCII
412 our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
413 our $typeOtherOSTypedefs = qr{(?x:
414 u_(?:char|short|int|long) | # bsd
415 u(?:nchar|short|int|long) # sysv
417 our $typeKernelTypedefs = qr{(?x:
418 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
421 our $typeTypedefs = qr{(?x:
423 $typeOtherOSTypedefs\b|
424 $typeKernelTypedefs\b
427 our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
429 our $logFunctions = qr{(?x:
430 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
431 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
432 WARN(?:_RATELIMIT|_ONCE|)|
435 seq_vprintf|seq_printf|seq_puts
438 our $signature_tags = qr{(?xi:
449 our @typeListMisordered = (
450 qr{char\s+(?:un)?signed},
451 qr{int\s+(?:(?:un)?signed\s+)?short\s},
452 qr{int\s+short(?:\s+(?:un)?signed)},
453 qr{short\s+int(?:\s+(?:un)?signed)},
454 qr{(?:un)?signed\s+int\s+short},
455 qr{short\s+(?:un)?signed},
456 qr{long\s+int\s+(?:un)?signed},
457 qr{int\s+long\s+(?:un)?signed},
458 qr{long\s+(?:un)?signed\s+int},
459 qr{int\s+(?:un)?signed\s+long},
460 qr{int\s+(?:un)?signed},
461 qr{int\s+long\s+long\s+(?:un)?signed},
462 qr{long\s+long\s+int\s+(?:un)?signed},
463 qr{long\s+long\s+(?:un)?signed\s+int},
464 qr{long\s+long\s+(?:un)?signed},
465 qr{long\s+(?:un)?signed},
470 qr{(?:(?:un)?signed\s+)?char},
471 qr{(?:(?:un)?signed\s+)?short\s+int},
472 qr{(?:(?:un)?signed\s+)?short},
473 qr{(?:(?:un)?signed\s+)?int},
474 qr{(?:(?:un)?signed\s+)?long\s+int},
475 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
476 qr{(?:(?:un)?signed\s+)?long\s+long},
477 qr{(?:(?:un)?signed\s+)?long},
486 qr{${Ident}_handler},
487 qr{${Ident}_handler_fn},
491 our $C90_int_types = qr{(?x:
492 long\s+long\s+int\s+(?:un)?signed|
493 long\s+long\s+(?:un)?signed\s+int|
494 long\s+long\s+(?:un)?signed|
495 (?:(?:un)?signed\s+)?long\s+long\s+int|
496 (?:(?:un)?signed\s+)?long\s+long|
497 int\s+long\s+long\s+(?:un)?signed|
498 int\s+(?:(?:un)?signed\s+)?long\s+long|
500 long\s+int\s+(?:un)?signed|
501 long\s+(?:un)?signed\s+int|
502 long\s+(?:un)?signed|
503 (?:(?:un)?signed\s+)?long\s+int|
504 (?:(?:un)?signed\s+)?long|
505 int\s+long\s+(?:un)?signed|
506 int\s+(?:(?:un)?signed\s+)?long|
509 (?:(?:un)?signed\s+)?int
512 our @typeListFile = ();
513 our @typeListWithAttr = (
515 qr{struct\s+$InitAttribute\s+$Ident},
516 qr{union\s+$InitAttribute\s+$Ident},
519 our @modifierList = (
522 our @modifierListFile = ();
524 our @mode_permission_funcs = (
526 ["module_param_(?:array|named|string)", 4],
527 ["module_param_array_named", 5],
528 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
529 ["proc_create(?:_data|)", 2],
530 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
531 ["IIO_DEV_ATTR_[A-Z_]+", 1],
532 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
533 ["SENSOR_TEMPLATE(?:_2|)", 3],
537 #Create a search pattern for all these functions to speed up a loop below
538 our $mode_perms_search = "";
539 foreach my $entry (@mode_permission_funcs) {
540 $mode_perms_search .= '|' if ($mode_perms_search ne "");
541 $mode_perms_search .= $entry->[0];
544 our $mode_perms_world_writable = qr{
552 our %mode_permission_string_types = (
571 #Create a search pattern for all these strings to speed up a loop below
572 our $mode_perms_string_search = "";
573 foreach my $entry (keys %mode_permission_string_types) {
574 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
575 $mode_perms_string_search .= $entry;
578 our $allowed_asm_includes = qr{(?x:
584 # memory.h: ARM has a custom one
586 # Load common spelling mistakes and build regular expression list.
590 if (open(my $spelling, '<', $spelling_file)) {
591 while (<$spelling>) {
594 $line =~ s/\s*\n?$//g;
597 next if ($line =~ m/^\s*#/);
598 next if ($line =~ m/^\s*$/);
600 my ($suspect, $fix) = split(/\|\|/, $line);
602 $spelling_fix{$suspect} = $fix;
606 warn "No typos will be found - file '$spelling_file': $!\n";
610 if (open(my $spelling, '<', $codespellfile)) {
611 while (<$spelling>) {
614 $line =~ s/\s*\n?$//g;
617 next if ($line =~ m/^\s*#/);
618 next if ($line =~ m/^\s*$/);
619 next if ($line =~ m/, disabled/i);
623 my ($suspect, $fix) = split(/->/, $line);
625 $spelling_fix{$suspect} = $fix;
629 warn "No codespell typos will be found - file '$codespellfile': $!\n";
633 $misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
636 my ($wordsRef, $file) = @_;
638 if (open(my $words, '<', $file)) {
642 $line =~ s/\s*\n?$//g;
645 next if ($line =~ m/^\s*#/);
646 next if ($line =~ m/^\s*$/);
648 print("$file: '$line' invalid - ignored\n");
652 $$wordsRef .= '|' if ($$wordsRef ne "");
662 my $const_structs = "";
663 read_words(\$const_structs, $conststructsfile)
664 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
666 my $typeOtherTypedefs = "";
667 if (length($typedefsfile)) {
668 read_words(\$typeOtherTypedefs, $typedefsfile)
669 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
671 $typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
674 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
675 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
676 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
677 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
678 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
684 (?:$Modifier\s+|const\s+)*
686 (?:typeof|__typeof__)\s*\([^\)]*\)|
690 (?:\s+$Modifier|\s+const)*
692 $NonptrTypeMisordered = qr{
693 (?:$Modifier\s+|const\s+)*
697 (?:\s+$Modifier|\s+const)*
699 $NonptrTypeWithAttr = qr{
700 (?:$Modifier\s+|const\s+)*
702 (?:typeof|__typeof__)\s*\([^\)]*\)|
706 (?:\s+$Modifier|\s+const)*
710 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
711 (?:\s+$Inline|\s+$Modifier)*
713 $TypeMisordered = qr{
714 $NonptrTypeMisordered
715 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
716 (?:\s+$Inline|\s+$Modifier)*
718 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
719 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
723 our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
725 # Using $balanced_parens, $LvalOrFunc, or $FuncArg
726 # requires at least perl version v5.10.0
727 # Any use must be runtime checked with $^V
729 our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
730 our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
731 our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
733 our $declaration_macros = qr{(?x:
734 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
735 (?:$Storage\s+)?LIST_HEAD\s*\(|
736 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
741 return "" if (!defined($string));
743 while ($string =~ /^\s*\(.*\)\s*$/) {
744 $string =~ s@^\s*\(\s*@@;
745 $string =~ s@\s*\)\s*$@@;
748 $string =~ s@\s+@ @g;
753 sub seed_camelcase_file {
756 return if (!(-f $file));
760 open(my $include_file, '<', "$file")
761 or warn "$P: Can't read '$file' $!\n";
762 my $text = <$include_file>;
763 close($include_file);
765 my @lines = split('\n', $text);
767 foreach my $line (@lines) {
768 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
769 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
771 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
773 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
779 sub is_maintained_obsolete {
782 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
784 my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
786 return $status =~ /obsolete/i;
789 my $camelcase_seeded = 0;
790 sub seed_camelcase_includes {
791 return if ($camelcase_seeded);
794 my $camelcase_cache = "";
795 my @include_files = ();
797 $camelcase_seeded = 1;
800 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
801 chomp $git_last_include_commit;
802 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
804 my $last_mod_date = 0;
805 $files = `find $root/include -name "*.h"`;
806 @include_files = split('\n', $files);
807 foreach my $file (@include_files) {
808 my $date = POSIX::strftime("%Y%m%d%H%M",
809 localtime((stat $file)[9]));
810 $last_mod_date = $date if ($last_mod_date < $date);
812 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
815 if ($camelcase_cache ne "" && -f $camelcase_cache) {
816 open(my $camelcase_file, '<', "$camelcase_cache")
817 or warn "$P: Can't read '$camelcase_cache' $!\n";
818 while (<$camelcase_file>) {
822 close($camelcase_file);
828 $files = `git ls-files "include/*.h"`;
829 @include_files = split('\n', $files);
832 foreach my $file (@include_files) {
833 seed_camelcase_file($file);
836 if ($camelcase_cache ne "") {
837 unlink glob ".checkpatch-camelcase.*";
838 open(my $camelcase_file, '>', "$camelcase_cache")
839 or warn "$P: Can't write '$camelcase_cache' $!\n";
840 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
841 print $camelcase_file ("$_\n");
843 close($camelcase_file);
847 sub git_commit_info {
848 my ($commit, $id, $desc) = @_;
850 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
852 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
853 $output =~ s/^\s*//gm;
854 my @lines = split("\n", $output);
856 return ($id, $desc) if ($#lines < 0);
858 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
859 # Maybe one day convert this block of bash into something that returns
860 # all matching commit ids, but it's very slow...
862 # echo "checking commits $1..."
863 # git rev-list --remotes | grep -i "^$1" |
864 # while read line ; do
865 # git log --format='%H %s' -1 $line |
866 # echo "commit $(cut -c 1-12,41-)"
868 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
870 $id = substr($lines[0], 0, 12);
871 $desc = substr($lines[0], 41);
877 $chk_signoff = 0 if ($file);
882 my @fixed_inserted = ();
883 my @fixed_deleted = ();
886 # If input is git commits, extract all commits from the commit expressions.
887 # For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
888 die "$P: No git repository found\n" if ($git && !-e ".git");
892 foreach my $commit_expr (@ARGV) {
894 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
895 $git_range = "-$2 $1";
896 } elsif ($commit_expr =~ m/\.\./) {
897 $git_range = "$commit_expr";
899 $git_range = "-1 $commit_expr";
901 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
902 foreach my $line (split(/\n/, $lines)) {
903 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
904 next if (!defined($1) || !defined($2));
907 unshift(@commits, $sha1);
908 $git_commits{$sha1} = $subject;
911 die "$P: no git commits after extraction!\n" if (@commits == 0);
916 for my $filename (@ARGV) {
919 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
920 die "$P: $filename: git format-patch failed - $!\n";
922 open($FILE, '-|', "diff -u /dev/null $filename") ||
923 die "$P: $filename: diff failed - $!\n";
924 } elsif ($filename eq '-') {
925 open($FILE, '<&STDIN');
927 open($FILE, '<', "$filename") ||
928 die "$P: $filename: open failed - $!\n";
930 if ($filename eq '-') {
931 $vname = 'Your patch';
933 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
943 if ($#ARGV > 0 && $quiet == 0) {
944 print '-' x length($vname) . "\n";
946 print '-' x length($vname) . "\n";
949 if (!process($filename)) {
955 @fixed_inserted = ();
958 @modifierListFile = ();
964 hash_show_words(\%use_type, "Used");
965 hash_show_words(\%ignore_type, "Ignored");
970 NOTE: perl $^V is not modern enough to detect all possible issues.
971 An upgrade to at least perl v5.10.0 is suggested.
977 NOTE: If any of the errors are false positives, please report
978 them to the maintainer, see CHECKPATCH in MAINTAINERS.
985 sub top_of_kernel_tree {
989 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
990 "README", "Documentation", "arch", "include", "drivers",
991 "fs", "init", "ipc", "kernel", "lib", "scripts",
994 foreach my $check (@tree_check) {
995 if (! -e $root . '/' . $check) {
1003 my ($formatted_email) = @_;
1009 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1012 $comment = $3 if defined $3;
1013 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1015 $comment = $2 if defined $2;
1016 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1018 $comment = $2 if defined $2;
1019 $formatted_email =~ s/$address.*$//;
1020 $name = $formatted_email;
1021 $name = trim($name);
1022 $name =~ s/^\"|\"$//g;
1023 # If there's a name left after stripping spaces and
1024 # leading quotes, and the address doesn't have both
1025 # leading and trailing angle brackets, the address
1027 # "joe smith joe@smith.com" bad
1028 # "joe smith <joe@smith.com" bad
1029 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1036 $name = trim($name);
1037 $name =~ s/^\"|\"$//g;
1038 $address = trim($address);
1039 $address =~ s/^\<|\>$//g;
1041 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1042 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1043 $name = "\"$name\"";
1046 return ($name, $address, $comment);
1050 my ($name, $address) = @_;
1052 my $formatted_email;
1054 $name = trim($name);
1055 $name =~ s/^\"|\"$//g;
1056 $address = trim($address);
1058 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1059 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1060 $name = "\"$name\"";
1063 if ("$name" eq "") {
1064 $formatted_email = "$address";
1066 $formatted_email = "$name <$address>";
1069 return $formatted_email;
1075 foreach my $path (split(/:/, $ENV{PATH})) {
1076 if (-e "$path/$bin") {
1077 return "$path/$bin";
1087 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1088 if (-e "$path/$conf") {
1089 return "$path/$conf";
1101 for my $c (split(//, $str)) {
1105 for (; ($n % 8) != 0; $n++) {
1117 (my $res = shift) =~ tr/\t/ /c;
1124 # Drop the diff line leader and expand tabs
1126 $line = expand_tabs($line);
1128 # Pick the indent from the front of the line.
1129 my ($white) = ($line =~ /^(\s*)/);
1131 return (length($line), length($white));
1134 my $sanitise_quote = '';
1136 sub sanitise_line_reset {
1137 my ($in_comment) = @_;
1140 $sanitise_quote = '*/';
1142 $sanitise_quote = '';
1155 # Always copy over the diff marker.
1156 $res = substr($line, 0, 1);
1158 for ($off = 1; $off < length($line); $off++) {
1159 $c = substr($line, $off, 1);
1161 # Comments we are wacking completly including the begin
1162 # and end, all to $;.
1163 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1164 $sanitise_quote = '*/';
1166 substr($res, $off, 2, "$;$;");
1170 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1171 $sanitise_quote = '';
1172 substr($res, $off, 2, "$;$;");
1176 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1177 $sanitise_quote = '//';
1179 substr($res, $off, 2, $sanitise_quote);
1184 # A \ in a string means ignore the next character.
1185 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1187 substr($res, $off, 2, 'XX');
1192 if ($c eq "'" || $c eq '"') {
1193 if ($sanitise_quote eq '') {
1194 $sanitise_quote = $c;
1196 substr($res, $off, 1, $c);
1198 } elsif ($sanitise_quote eq $c) {
1199 $sanitise_quote = '';
1203 #print "c<$c> SQ<$sanitise_quote>\n";
1204 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1205 substr($res, $off, 1, $;);
1206 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1207 substr($res, $off, 1, $;);
1208 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1209 substr($res, $off, 1, 'X');
1211 substr($res, $off, 1, $c);
1215 if ($sanitise_quote eq '//') {
1216 $sanitise_quote = '';
1219 # The pathname on a #include may be surrounded by '<' and '>'.
1220 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1221 my $clean = 'X' x length($1);
1222 $res =~ s@\<.*\>@<$clean>@;
1224 # The whole of a #error is a string.
1225 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1226 my $clean = 'X' x length($1);
1227 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1230 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1232 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1238 sub get_quoted_string {
1239 my ($line, $rawline) = @_;
1241 return "" if ($line !~ m/($String)/g);
1242 return substr($rawline, $-[0], $+[0] - $-[0]);
1245 sub ctx_statement_block {
1246 my ($linenr, $remain, $off) = @_;
1247 my $line = $linenr - 1;
1250 my $coff = $off - 1;
1264 @stack = (['', 0]) if ($#stack == -1);
1266 #warn "CSB: blk<$blk> remain<$remain>\n";
1267 # If we are about to drop off the end, pull in more
1270 for (; $remain > 0; $line++) {
1271 last if (!defined $lines[$line]);
1272 next if ($lines[$line] =~ /^-/);
1275 $blk .= $lines[$line] . "\n";
1276 $len = length($blk);
1280 # Bail if there is no further context.
1281 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
1285 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1291 $c = substr($blk, $off, 1);
1292 $remainder = substr($blk, $off);
1294 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1296 # Handle nested #if/#else.
1297 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1298 push(@stack, [ $type, $level ]);
1299 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1300 ($type, $level) = @{$stack[$#stack - 1]};
1301 } elsif ($remainder =~ /^#\s*endif\b/) {
1302 ($type, $level) = @{pop(@stack)};
1305 # Statement ends at the ';' or a close '}' at the
1307 if ($level == 0 && $c eq ';') {
1311 # An else is really a conditional as long as its not else if
1312 if ($level == 0 && $coff_set == 0 &&
1313 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1314 $remainder =~ /^(else)(?:\s|{)/ &&
1315 $remainder !~ /^else\s+if\b/) {
1316 $coff = $off + length($1) - 1;
1318 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1319 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1322 if (($type eq '' || $type eq '(') && $c eq '(') {
1326 if ($type eq '(' && $c eq ')') {
1328 $type = ($level != 0)? '(' : '';
1330 if ($level == 0 && $coff < $soff) {
1333 #warn "CSB: mark coff<$coff>\n";
1336 if (($type eq '' || $type eq '{') && $c eq '{') {
1340 if ($type eq '{' && $c eq '}') {
1342 $type = ($level != 0)? '{' : '';
1345 if (substr($blk, $off + 1, 1) eq ';') {
1351 # Preprocessor commands end at the newline unless escaped.
1352 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1360 # We are truly at the end, so shuffle to the next line.
1367 my $statement = substr($blk, $soff, $off - $soff + 1);
1368 my $condition = substr($blk, $soff, $coff - $soff + 1);
1370 #warn "STATEMENT<$statement>\n";
1371 #warn "CONDITION<$condition>\n";
1373 #print "coff<$coff> soff<$off> loff<$loff>\n";
1375 return ($statement, $condition,
1376 $line, $remain + 1, $off - $loff + 1, $level);
1379 sub statement_lines {
1382 # Strip the diff line prefixes and rip blank lines at start and end.
1383 $stmt =~ s/(^|\n)./$1/g;
1387 my @stmt_lines = ($stmt =~ /\n/g);
1389 return $#stmt_lines + 2;
1392 sub statement_rawlines {
1395 my @stmt_lines = ($stmt =~ /\n/g);
1397 return $#stmt_lines + 2;
1400 sub statement_block_size {
1403 $stmt =~ s/(^|\n)./$1/g;
1409 my @stmt_lines = ($stmt =~ /\n/g);
1410 my @stmt_statements = ($stmt =~ /;/g);
1412 my $stmt_lines = $#stmt_lines + 2;
1413 my $stmt_statements = $#stmt_statements + 1;
1415 if ($stmt_lines > $stmt_statements) {
1418 return $stmt_statements;
1422 sub ctx_statement_full {
1423 my ($linenr, $remain, $off) = @_;
1424 my ($statement, $condition, $level);
1428 # Grab the first conditional/block pair.
1429 ($statement, $condition, $linenr, $remain, $off, $level) =
1430 ctx_statement_block($linenr, $remain, $off);
1431 #print "F: c<$condition> s<$statement> remain<$remain>\n";
1432 push(@chunks, [ $condition, $statement ]);
1433 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1434 return ($level, $linenr, @chunks);
1437 # Pull in the following conditional/block pairs and see if they
1438 # could continue the statement.
1440 ($statement, $condition, $linenr, $remain, $off, $level) =
1441 ctx_statement_block($linenr, $remain, $off);
1442 #print "C: c<$condition> s<$statement> remain<$remain>\n";
1443 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1445 push(@chunks, [ $condition, $statement ]);
1448 return ($level, $linenr, @chunks);
1452 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1454 my $start = $linenr - 1;
1461 my @stack = ($level);
1462 for ($line = $start; $remain > 0; $line++) {
1463 next if ($rawlines[$line] =~ /^-/);
1466 $blk .= $rawlines[$line];
1468 # Handle nested #if/#else.
1469 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1470 push(@stack, $level);
1471 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1472 $level = $stack[$#stack - 1];
1473 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1474 $level = pop(@stack);
1477 foreach my $c (split(//, $lines[$line])) {
1478 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1484 if ($c eq $close && $level > 0) {
1486 last if ($level == 0);
1487 } elsif ($c eq $open) {
1492 if (!$outer || $level <= 1) {
1493 push(@res, $rawlines[$line]);
1496 last if ($level == 0);
1499 return ($level, @res);
1501 sub ctx_block_outer {
1502 my ($linenr, $remain) = @_;
1504 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1508 my ($linenr, $remain) = @_;
1510 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1514 my ($linenr, $remain, $off) = @_;
1516 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1519 sub ctx_block_level {
1520 my ($linenr, $remain) = @_;
1522 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1524 sub ctx_statement_level {
1525 my ($linenr, $remain, $off) = @_;
1527 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1530 sub ctx_locate_comment {
1531 my ($first_line, $end_line) = @_;
1533 # Catch a comment on the end of the line itself.
1534 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1535 return $current_comment if (defined $current_comment);
1537 # Look through the context and try and figure out if there is a
1540 $current_comment = '';
1541 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1542 my $line = $rawlines[$linenr - 1];
1544 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1547 if ($line =~ m@/\*@) {
1550 if (!$in_comment && $current_comment ne '') {
1551 $current_comment = '';
1553 $current_comment .= $line . "\n" if ($in_comment);
1554 if ($line =~ m@\*/@) {
1559 chomp($current_comment);
1560 return($current_comment);
1562 sub ctx_has_comment {
1563 my ($first_line, $end_line) = @_;
1564 my $cmt = ctx_locate_comment($first_line, $end_line);
1566 ##print "LINE: $rawlines[$end_line - 1 ]\n";
1567 ##print "CMMT: $cmt\n";
1569 return ($cmt ne '');
1573 my ($linenr, $cnt) = @_;
1575 my $offset = $linenr - 1;
1580 $line = $rawlines[$offset++];
1581 next if (defined($line) && $line =~ /^-/);
1593 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1596 $coded = sprintf("^%c", unpack('C', $2) + 64);
1605 my $av_preprocessor = 0;
1610 sub annotate_reset {
1611 $av_preprocessor = 0;
1613 @av_paren_type = ('E');
1614 $av_pend_colon = 'O';
1617 sub annotate_values {
1618 my ($stream, $type) = @_;
1621 my $var = '_' x length($stream);
1624 print "$stream\n" if ($dbg_values > 1);
1626 while (length($cur)) {
1627 @av_paren_type = ('E') if ($#av_paren_type < 0);
1628 print " <" . join('', @av_paren_type) .
1629 "> <$type> <$av_pending>" if ($dbg_values > 1);
1630 if ($cur =~ /^(\s+)/o) {
1631 print "WS($1)\n" if ($dbg_values > 1);
1632 if ($1 =~ /\n/ && $av_preprocessor) {
1633 $type = pop(@av_paren_type);
1634 $av_preprocessor = 0;
1637 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
1638 print "CAST($1)\n" if ($dbg_values > 1);
1639 push(@av_paren_type, $type);
1642 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1643 print "DECLARE($1)\n" if ($dbg_values > 1);
1646 } elsif ($cur =~ /^($Modifier)\s*/) {
1647 print "MODIFIER($1)\n" if ($dbg_values > 1);
1650 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1651 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1652 $av_preprocessor = 1;
1653 push(@av_paren_type, $type);
1659 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1660 print "UNDEF($1)\n" if ($dbg_values > 1);
1661 $av_preprocessor = 1;
1662 push(@av_paren_type, $type);
1664 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1665 print "PRE_START($1)\n" if ($dbg_values > 1);
1666 $av_preprocessor = 1;
1668 push(@av_paren_type, $type);
1669 push(@av_paren_type, $type);
1672 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1673 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1674 $av_preprocessor = 1;
1676 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1680 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1681 print "PRE_END($1)\n" if ($dbg_values > 1);
1683 $av_preprocessor = 1;
1685 # Assume all arms of the conditional end as this
1686 # one does, and continue as if the #endif was not here.
1687 pop(@av_paren_type);
1688 push(@av_paren_type, $type);
1691 } elsif ($cur =~ /^(\\\n)/o) {
1692 print "PRECONT($1)\n" if ($dbg_values > 1);
1694 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1695 print "ATTR($1)\n" if ($dbg_values > 1);
1696 $av_pending = $type;
1699 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1700 print "SIZEOF($1)\n" if ($dbg_values > 1);
1706 } elsif ($cur =~ /^(if|while|for)\b/o) {
1707 print "COND($1)\n" if ($dbg_values > 1);
1711 } elsif ($cur =~/^(case)/o) {
1712 print "CASE($1)\n" if ($dbg_values > 1);
1713 $av_pend_colon = 'C';
1716 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1717 print "KEYWORD($1)\n" if ($dbg_values > 1);
1720 } elsif ($cur =~ /^(\()/o) {
1721 print "PAREN('$1')\n" if ($dbg_values > 1);
1722 push(@av_paren_type, $av_pending);
1726 } elsif ($cur =~ /^(\))/o) {
1727 my $new_type = pop(@av_paren_type);
1728 if ($new_type ne '_') {
1730 print "PAREN('$1') -> $type\n"
1731 if ($dbg_values > 1);
1733 print "PAREN('$1')\n" if ($dbg_values > 1);
1736 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1737 print "FUNC($1)\n" if ($dbg_values > 1);
1741 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1742 if (defined $2 && $type eq 'C' || $type eq 'T') {
1743 $av_pend_colon = 'B';
1744 } elsif ($type eq 'E') {
1745 $av_pend_colon = 'L';
1747 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1750 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1751 print "IDENT($1)\n" if ($dbg_values > 1);
1754 } elsif ($cur =~ /^($Assignment)/o) {
1755 print "ASSIGN($1)\n" if ($dbg_values > 1);
1758 } elsif ($cur =~/^(;|{|})/) {
1759 print "END($1)\n" if ($dbg_values > 1);
1761 $av_pend_colon = 'O';
1763 } elsif ($cur =~/^(,)/) {
1764 print "COMMA($1)\n" if ($dbg_values > 1);
1767 } elsif ($cur =~ /^(\?)/o) {
1768 print "QUESTION($1)\n" if ($dbg_values > 1);
1771 } elsif ($cur =~ /^(:)/o) {
1772 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1774 substr($var, length($res), 1, $av_pend_colon);
1775 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1780 $av_pend_colon = 'O';
1782 } elsif ($cur =~ /^(\[)/o) {
1783 print "CLOSE($1)\n" if ($dbg_values > 1);
1786 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1789 print "OPV($1)\n" if ($dbg_values > 1);
1796 substr($var, length($res), 1, $variant);
1799 } elsif ($cur =~ /^($Operators)/o) {
1800 print "OP($1)\n" if ($dbg_values > 1);
1801 if ($1 ne '++' && $1 ne '--') {
1805 } elsif ($cur =~ /(^.)/o) {
1806 print "C($1)\n" if ($dbg_values > 1);
1809 $cur = substr($cur, length($1));
1810 $res .= $type x length($1);
1814 return ($res, $var);
1818 my ($possible, $line) = @_;
1819 my $notPermitted = qr{(?:
1836 ^(?:typedef|struct|enum)\b
1838 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1839 if ($possible !~ $notPermitted) {
1840 # Check for modifiers.
1841 $possible =~ s/\s*$Storage\s*//g;
1842 $possible =~ s/\s*$Sparse\s*//g;
1843 if ($possible =~ /^\s*$/) {
1845 } elsif ($possible =~ /\s/) {
1846 $possible =~ s/\s*$Type\s*//g;
1847 for my $modifier (split(' ', $possible)) {
1848 if ($modifier !~ $notPermitted) {
1849 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1850 push(@modifierListFile, $modifier);
1855 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1856 push(@typeListFile, $possible);
1860 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1869 $type =~ tr/[a-z]/[A-Z]/;
1871 return defined $use_type{$type} if (scalar keys %use_type > 0);
1873 return !defined $ignore_type{$type};
1877 my ($level, $type, $msg) = @_;
1879 if (!show_type($type) ||
1880 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1884 if (-t STDOUT && $color) {
1885 if ($level eq 'ERROR') {
1887 } elsif ($level eq 'WARNING') {
1893 $output .= $prefix . $level . ':';
1895 $output .= BLUE if (-t STDOUT && $color);
1896 $output .= "$type:";
1898 $output .= RESET if (-t STDOUT && $color);
1899 $output .= ' ' . $msg . "\n";
1902 my @lines = split("\n", $output, -1);
1903 splice(@lines, 1, 1);
1904 $output = join("\n", @lines);
1906 $output = (split('\n', $output))[0] . "\n" if ($terse);
1908 push(our @report, $output);
1917 sub fixup_current_range {
1918 my ($lineRef, $offset, $length) = @_;
1920 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1923 my $no = $o + $offset;
1924 my $nl = $l + $length;
1925 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1929 sub fix_inserted_deleted_lines {
1930 my ($linesRef, $insertedRef, $deletedRef) = @_;
1932 my $range_last_linenr = 0;
1933 my $delta_offset = 0;
1938 my $next_insert = 0;
1939 my $next_delete = 0;
1943 my $inserted = @{$insertedRef}[$next_insert++];
1944 my $deleted = @{$deletedRef}[$next_delete++];
1946 foreach my $old_line (@{$linesRef}) {
1948 my $line = $old_line; #don't modify the array
1949 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
1951 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1952 $range_last_linenr = $new_linenr;
1953 fixup_current_range(\$line, $delta_offset, 0);
1956 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1957 $deleted = @{$deletedRef}[$next_delete++];
1959 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1962 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1963 push(@lines, ${$inserted}{'LINE'});
1964 $inserted = @{$insertedRef}[$next_insert++];
1966 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1970 push(@lines, $line);
1980 sub fix_insert_line {
1981 my ($linenr, $line) = @_;
1987 push(@fixed_inserted, $inserted);
1990 sub fix_delete_line {
1991 my ($linenr, $line) = @_;
1998 push(@fixed_deleted, $deleted);
2002 my ($type, $msg) = @_;
2004 if (report("ERROR", $type, $msg)) {
2012 my ($type, $msg) = @_;
2014 if (report("WARNING", $type, $msg)) {
2022 my ($type, $msg) = @_;
2024 if ($check && report("CHECK", $type, $msg)) {
2032 sub check_absolute_file {
2033 my ($absolute, $herecurr) = @_;
2034 my $file = $absolute;
2036 ##print "absolute<$absolute>\n";
2038 # See if any suffix of this path is a path within the tree.
2039 while ($file =~ s@^[^/]*/@@) {
2040 if (-f "$root/$file") {
2041 ##print "file<$file>\n";
2049 # It is, so see if the prefix is acceptable.
2050 my $prefix = $absolute;
2051 substr($prefix, -length($file)) = '';
2053 ##print "prefix<$prefix>\n";
2054 if ($prefix ne ".../") {
2055 WARN("USE_RELATIVE_PATH",
2056 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2063 $string =~ s/^\s+|\s+$//g;
2071 $string =~ s/^\s+//;
2079 $string =~ s/\s+$//;
2084 sub string_find_replace {
2085 my ($string, $find, $replace) = @_;
2087 $string =~ s/$find/$replace/g;
2095 my $source_indent = 8;
2096 my $max_spaces_before_tab = $source_indent - 1;
2097 my $spaces_to_tab = " " x $source_indent;
2099 #convert leading spaces to tabs
2100 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2101 #Remove spaces before a tab
2102 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2107 sub pos_last_openparen {
2112 my $opens = $line =~ tr/\(/\(/;
2113 my $closes = $line =~ tr/\)/\)/;
2115 my $last_openparen = 0;
2117 if (($opens == 0) || ($closes >= $opens)) {
2121 my $len = length($line);
2123 for ($pos = 0; $pos < $len; $pos++) {
2124 my $string = substr($line, $pos);
2125 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2126 $pos += length($1) - 1;
2127 } elsif (substr($line, $pos, 1) eq '(') {
2128 $last_openparen = $pos;
2129 } elsif (index($string, '(') == -1) {
2134 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2138 my $filename = shift;
2144 my $stashrawline="";
2154 my $in_header_lines = $file ? 0 : 1;
2155 my $in_commit_log = 0; #Scanning lines before patch
2156 my $has_commit_log = 0; #Encountered lines before patch
2157 my $commit_log_possible_stack_dump = 0;
2158 my $commit_log_long_line = 0;
2159 my $commit_log_has_diff = 0;
2160 my $reported_maintainer_file = 0;
2161 my $non_utf8_charset = 0;
2163 my $last_blank_line = 0;
2164 my $last_coalesced_string_linenr = -1;
2172 # Trace the real file/line as we go.
2177 my $context_function; #undef'd unless there's a known function
2179 my $comment_edge = 0;
2183 my $prev_values = 'E';
2186 my %suppress_ifbraces;
2187 my %suppress_whiletrailers;
2188 my %suppress_export;
2189 my $suppress_statement = 0;
2191 my %signatures = ();
2193 # Pre-scan the patch sanitizing the lines.
2194 # Pre-scan the patch looking for any __setup documentation.
2196 my @setup_docs = ();
2199 my $camelcase_file_seeded = 0;
2201 sanitise_line_reset();
2203 foreach my $rawline (@rawlines) {
2207 push(@fixed, $rawline) if ($fix);
2209 if ($rawline=~/^\+\+\+\s+(\S+)/) {
2211 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2216 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2225 # Guestimate if this is a continuing comment. Run
2226 # the context looking for a comment "edge". If this
2227 # edge is a close comment then we must be in a comment
2231 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2232 next if (defined $rawlines[$ln - 1] &&
2233 $rawlines[$ln - 1] =~ /^-/);
2235 #print "RAW<$rawlines[$ln - 1]>\n";
2236 last if (!defined $rawlines[$ln - 1]);
2237 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2238 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2243 if (defined $edge && $edge eq '*/') {
2247 # Guestimate if this is a continuing comment. If this
2248 # is the start of a diff block and this line starts
2249 # ' *' then it is very likely a comment.
2250 if (!defined $edge &&
2251 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2256 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2257 sanitise_line_reset($in_comment);
2259 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2260 # Standardise the strings and chars within the input to
2261 # simplify matching -- only bother with positive lines.
2262 $line = sanitise_line($rawline);
2264 push(@lines, $line);
2267 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2272 #print "==>$rawline\n";
2273 #print "-->$line\n";
2275 if ($setup_docs && $line =~ /^\+/) {
2276 push(@setup_docs, $line);
2285 foreach my $line (@lines) {
2288 my $sline = $line; #copy of $line
2289 $sline =~ s/$;/ /g; #with comments as spaces
2291 my $rawline = $rawlines[$linenr - 1];
2293 #extract the line range in the file after the patch is applied
2294 if (!$in_commit_log &&
2295 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2298 $first_line = $linenr + 1;
2308 %suppress_ifbraces = ();
2309 %suppress_whiletrailers = ();
2310 %suppress_export = ();
2311 $suppress_statement = 0;
2312 if ($context =~ /\b(\w+)\s*\(/) {
2313 $context_function = $1;
2315 undef $context_function;
2319 # track the line number as we move through the hunk, note that
2320 # new versions of GNU diff omit the leading space on completely
2321 # blank context lines so we need to count that too.
2322 } elsif ($line =~ /^( |\+|$)/) {
2324 $realcnt-- if ($realcnt != 0);
2326 # Measure the line length and indent.
2327 ($length, $indent) = line_stats($rawline);
2329 # Track the previous line.
2330 ($prevline, $stashline) = ($stashline, $line);
2331 ($previndent, $stashindent) = ($stashindent, $indent);
2332 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2334 #warn "line<$line>\n";
2336 } elsif ($realcnt == 1) {
2340 my $hunk_line = ($realcnt != 0);
2342 $here = "#$linenr: " if (!$file);
2343 $here = "#$realline: " if ($file);
2346 # extract the filename as it passes
2347 if ($line =~ /^diff --git.*?(\S+)$/) {
2349 $realfile =~ s@^([^/]*)/@@ if (!$file);
2352 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2354 $realfile =~ s@^([^/]*)/@@ if (!$file);
2358 if (!$file && $tree && $p1_prefix ne '' &&
2359 -e "$root/$p1_prefix") {
2360 WARN("PATCH_PREFIX",
2361 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2364 if ($realfile =~ m@^include/asm/@) {
2365 ERROR("MODIFIED_INCLUDE_ASM",
2366 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2371 #make up the handle for any error we report on this line
2373 $prefix = "$realfile:$realline: "
2376 $prefix = "$filename:$realline: ";
2378 $prefix = "$filename:$linenr: ";
2383 if (is_maintained_obsolete($realfile)) {
2385 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2387 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
2390 $check = $check_orig;
2395 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2397 my $hereline = "$here\n$rawline\n";
2398 my $herecurr = "$here\n$rawline\n";
2399 my $hereprev = "$here\n$prevrawline\n$rawline\n";
2401 $cnt_lines++ if ($realcnt != 0);
2403 # Check if the commit log has what seems like a diff which can confuse patch
2404 if ($in_commit_log && !$commit_log_has_diff &&
2405 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2406 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2407 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2408 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2409 ERROR("DIFF_IN_COMMIT_MSG",
2410 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2411 $commit_log_has_diff = 1;
2414 # Check for incorrect file permissions
2415 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2416 my $permhere = $here . "FILE: $realfile\n";
2417 if ($realfile !~ m@scripts/@ &&
2418 $realfile !~ /\.(py|pl|awk|sh)$/) {
2419 ERROR("EXECUTE_PERMISSIONS",
2420 "do not set execute permissions for source files\n" . $permhere);
2424 # Check the patch for a signoff:
2425 if ($line =~ /^\s*signed-off-by:/i) {
2430 # Check if MAINTAINERS is being updated. If so, there's probably no need to
2431 # emit the "does MAINTAINERS need updating?" message on file add/move/delete
2432 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2433 $reported_maintainer_file = 1;
2436 # Check signature styles
2437 if (!$in_header_lines &&
2438 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2439 my $space_before = $1;
2441 my $space_after = $3;
2443 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2445 if ($sign_off !~ /$signature_tags/) {
2446 WARN("BAD_SIGN_OFF",
2447 "Non-standard signature: $sign_off\n" . $herecurr);
2449 if (defined $space_before && $space_before ne "") {
2450 if (WARN("BAD_SIGN_OFF",
2451 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2453 $fixed[$fixlinenr] =
2454 "$ucfirst_sign_off $email";
2457 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2458 if (WARN("BAD_SIGN_OFF",
2459 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2461 $fixed[$fixlinenr] =
2462 "$ucfirst_sign_off $email";
2466 if (!defined $space_after || $space_after ne " ") {
2467 if (WARN("BAD_SIGN_OFF",
2468 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2470 $fixed[$fixlinenr] =
2471 "$ucfirst_sign_off $email";
2475 my ($email_name, $email_address, $comment) = parse_email($email);
2476 my $suggested_email = format_email(($email_name, $email_address));
2477 if ($suggested_email eq "") {
2478 ERROR("BAD_SIGN_OFF",
2479 "Unrecognized email address: '$email'\n" . $herecurr);
2481 my $dequoted = $suggested_email;
2482 $dequoted =~ s/^"//;
2483 $dequoted =~ s/" </ </;
2484 # Don't force email to have quotes
2485 # Allow just an angle bracketed address
2486 if ("$dequoted$comment" ne $email &&
2487 "<$email_address>$comment" ne $email &&
2488 "$suggested_email$comment" ne $email) {
2489 WARN("BAD_SIGN_OFF",
2490 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
2494 # Check for duplicate signatures
2495 my $sig_nospace = $line;
2496 $sig_nospace =~ s/\s//g;
2497 $sig_nospace = lc($sig_nospace);
2498 if (defined $signatures{$sig_nospace}) {
2499 WARN("BAD_SIGN_OFF",
2500 "Duplicate signature\n" . $herecurr);
2502 $signatures{$sig_nospace} = 1;
2506 # Check email subject for common tools that don't need to be mentioned
2507 if ($in_header_lines &&
2508 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2509 WARN("EMAIL_SUBJECT",
2510 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2513 # Check for old stable address
2514 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2515 ERROR("STABLE_ADDRESS",
2516 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2519 # Check for unwanted Gerrit info
2520 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2521 ERROR("GERRIT_CHANGE_ID",
2522 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2525 # Check if the commit log is in a possible stack dump
2526 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2527 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2528 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2530 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2531 # stack dump address
2532 $commit_log_possible_stack_dump = 1;
2535 # Check for line lengths > 75 in commit log, warn once
2536 if ($in_commit_log && !$commit_log_long_line &&
2537 length($line) > 75 &&
2538 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2539 # file delta changes
2540 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2542 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2543 # A Fixes: or Link: line
2544 $commit_log_possible_stack_dump)) {
2545 WARN("COMMIT_LOG_LONG_LINE",
2546 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2547 $commit_log_long_line = 1;
2550 # Reset possible stack dump if a blank line is found
2551 if ($in_commit_log && $commit_log_possible_stack_dump &&
2553 $commit_log_possible_stack_dump = 0;
2556 # Check for git id commit length and improperly formed commit descriptions
2557 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2558 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2559 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2560 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2561 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2562 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2563 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2564 my $init_char = "c";
2565 my $orig_commit = "";
2572 my $id = '0123456789ab';
2573 my $orig_desc = "commit description";
2574 my $description = "";
2576 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2578 $orig_commit = lc($2);
2579 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2580 $orig_commit = lc($1);
2583 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2584 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2585 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2586 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2587 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2590 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2591 defined $rawlines[$linenr] &&
2592 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2595 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2596 defined $rawlines[$linenr] &&
2597 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2598 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2600 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2601 $orig_desc .= " " . $1;
2605 ($id, $description) = git_commit_info($orig_commit,
2608 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
2609 ERROR("GIT_COMMIT_ID",
2610 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2614 # Check for added, moved or deleted files
2615 if (!$reported_maintainer_file && !$in_commit_log &&
2616 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2617 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2618 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2619 (defined($1) || defined($2))))) {
2621 $reported_maintainer_file = 1;
2622 WARN("FILE_PATH_CHANGES",
2623 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2626 # Check for wrappage within a valid hunk of the file
2627 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2628 ERROR("CORRUPTED_PATCH",
2629 "patch seems to be corrupt (line wrapped?)\n" .
2630 $herecurr) if (!$emitted_corrupt++);
2633 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2634 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2635 $rawline !~ m/^$UTF8*$/) {
2636 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2638 my $blank = copy_spacing($rawline);
2639 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2640 my $hereptr = "$hereline$ptr\n";
2643 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
2646 # Check if it's the start of a commit log
2647 # (not a header line and we haven't seen the patch filename)
2648 if ($in_header_lines && $realfile =~ /^$/ &&
2649 !($rawline =~ /^\s+(?:\S|$)/ ||
2650 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
2651 $in_header_lines = 0;
2653 $has_commit_log = 1;
2656 # Check if there is UTF-8 in a commit log when a mail header has explicitly
2657 # declined it, i.e defined some charset where it is missing.
2658 if ($in_header_lines &&
2659 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2661 $non_utf8_charset = 1;
2664 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
2665 $rawline =~ /$NON_ASCII_UTF8/) {
2666 WARN("UTF8_BEFORE_PATCH",
2667 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2670 # Check for absolute kernel paths in commit message
2671 if ($tree && $in_commit_log) {
2672 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2675 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2676 check_absolute_file($1, $herecurr)) {
2679 check_absolute_file($file, $herecurr);
2684 # Check for various typo / spelling mistakes
2685 if (defined($misspellings) &&
2686 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2687 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
2689 my $typo_fix = $spelling_fix{lc($typo)};
2690 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2691 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2692 my $msg_type = \&WARN;
2693 $msg_type = \&CHK if ($file);
2694 if (&{$msg_type}("TYPO_SPELLING",
2695 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2697 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2702 # ignore non-hunk lines and lines being removed
2703 next if (!$hunk_line || $line =~ /^-/);
2705 #trailing whitespace
2706 if ($line =~ /^\+.*\015/) {
2707 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2708 if (ERROR("DOS_LINE_ENDINGS",
2709 "DOS line endings\n" . $herevet) &&
2711 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
2713 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2714 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2715 if (ERROR("TRAILING_WHITESPACE",
2716 "trailing whitespace\n" . $herevet) &&
2718 $fixed[$fixlinenr] =~ s/\s+$//;
2724 # Check for FSF mailing addresses.
2725 if ($rawline =~ /\bwrite to the Free/i ||
2726 $rawline =~ /\b675\s+Mass\s+Ave/i ||
2727 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2728 $rawline =~ /\b51\s+Franklin\s+St/i) {
2729 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2730 my $msg_type = \&ERROR;
2731 $msg_type = \&CHK if ($file);
2732 &{$msg_type}("FSF_MAILING_ADDRESS",
2733 "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)
2736 # check for Kconfig help text having a real description
2737 # Only applies when adding the entry originally, after that we do not have
2738 # sufficient context to determine whether it is indeed long enough.
2739 if ($realfile =~ /Kconfig/ &&
2740 $line =~ /^\+\s*config\s+/) {
2743 my $ln = $linenr + 1;
2747 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
2748 $f = $lines[$ln - 1];
2749 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2750 $is_end = $lines[$ln - 1] =~ /^\+/;
2752 next if ($f =~ /^-/);
2753 last if (!$file && $f =~ /^\@\@/);
2755 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2757 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2764 next if ($f =~ /^$/);
2765 if ($f =~ /^\s*config\s/) {
2771 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2772 WARN("CONFIG_DESCRIPTION",
2773 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2775 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2778 # discourage the use of boolean for type definition attributes of Kconfig options
2779 if ($realfile =~ /Kconfig/ &&
2780 $line =~ /^\+\s*\bboolean\b/) {
2781 WARN("CONFIG_TYPE_BOOLEAN",
2782 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2785 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2786 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2789 'EXTRA_AFLAGS' => 'asflags-y',
2790 'EXTRA_CFLAGS' => 'ccflags-y',
2791 'EXTRA_CPPFLAGS' => 'cppflags-y',
2792 'EXTRA_LDFLAGS' => 'ldflags-y',
2795 WARN("DEPRECATED_VARIABLE",
2796 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2799 # check for DT compatible documentation
2800 if (defined $root &&
2801 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2802 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2804 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2806 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2807 my $vp_file = $dt_path . "vendor-prefixes.txt";
2809 foreach my $compat (@compats) {
2810 my $compat2 = $compat;
2811 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2812 my $compat3 = $compat;
2813 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2814 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2816 WARN("UNDOCUMENTED_DT_STRING",
2817 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2820 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2822 `grep -Eq "^$vendor\\b" $vp_file`;
2824 WARN("UNDOCUMENTED_DT_STRING",
2825 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2830 # check we are in a valid source file if not then ignore this hunk
2831 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
2833 # line length limit (with some exclusions)
2835 # There are a few types of lines that may extend beyond $max_line_length:
2836 # logging functions like pr_info that end in a string
2837 # lines with a single string
2838 # #defines that are a single string
2840 # There are 3 different line length message types:
2841 # LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2842 # LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2843 # LONG_LINE all other lines longer than $max_line_length
2845 # if LONG_LINE is ignored, the other 2 types are also ignored
2848 if ($line =~ /^\+/ && $length > $max_line_length) {
2849 my $msg_type = "LONG_LINE";
2851 # Check the allowed long line types first
2853 # logging functions that end in a string that starts
2854 # before $max_line_length
2855 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2856 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2859 # lines with only strings (w/ possible termination)
2860 # #defines with only strings
2861 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2862 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2865 # EFI_GUID is another special case
2866 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2869 # Otherwise set the alternate message types
2871 # a comment starts before $max_line_length
2872 } elsif ($line =~ /($;[\s$;]*)$/ &&
2873 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2874 $msg_type = "LONG_LINE_COMMENT"
2876 # a quoted string starts before $max_line_length
2877 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2878 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2879 $msg_type = "LONG_LINE_STRING"
2882 if ($msg_type ne "" &&
2883 (show_type("LONG_LINE") || show_type($msg_type))) {
2885 "line over $max_line_length characters\n" . $herecurr);
2889 # check for adding lines without a newline.
2890 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2891 WARN("MISSING_EOF_NEWLINE",
2892 "adding a line without newline at end of file\n" . $herecurr);
2895 # Blackfin: use hi/lo macros
2896 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2897 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2898 my $herevet = "$here\n" . cat_vet($line) . "\n";
2900 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
2902 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2903 my $herevet = "$here\n" . cat_vet($line) . "\n";
2905 "use the HI() macro, not (... >> 16)\n" . $herevet);
2909 # check we are in a valid source file C or perl if not then ignore this hunk
2910 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
2912 # at the beginning of a line any tabs must come first and anything
2913 # more than 8 must use tabs.
2914 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2915 $rawline =~ /^\+\s* \s*/) {
2916 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2918 if (ERROR("CODE_INDENT",
2919 "code indent should use tabs where possible\n" . $herevet) &&
2921 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
2925 # check for space before tabs.
2926 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2927 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2928 if (WARN("SPACE_BEFORE_TAB",
2929 "please, no space before tabs\n" . $herevet) &&
2931 while ($fixed[$fixlinenr] =~
2932 s/(^\+.*) {8,8}\t/$1\t\t/) {}
2933 while ($fixed[$fixlinenr] =~
2934 s/(^\+.*) +\t/$1\t/) {}
2938 # check for && or || at the start of a line
2939 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2940 CHK("LOGICAL_CONTINUATIONS",
2941 "Logical continuations should be on the previous line\n" . $hereprev);
2944 # check indentation starts on a tab stop
2945 if ($^V && $^V ge 5.10.0 &&
2946 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2947 my $indent = length($1);
2950 "Statements should start on a tabstop\n" . $herecurr) &&
2952 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2957 # check multi-line statement indentation matches previous line
2958 if ($^V && $^V ge 5.10.0 &&
2959 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2960 $prevline =~ /^\+(\t*)(.*)$/;
2964 my $pos = pos_last_openparen($rest);
2966 $line =~ /^(\+| )([ \t]*)/;
2969 my $goodtabindent = $oldindent .
2972 my $goodspaceindent = $oldindent . " " x $pos;
2974 if ($newindent ne $goodtabindent &&
2975 $newindent ne $goodspaceindent) {
2977 if (CHK("PARENTHESIS_ALIGNMENT",
2978 "Alignment should match open parenthesis\n" . $hereprev) &&
2979 $fix && $line =~ /^\+/) {
2980 $fixed[$fixlinenr] =~
2981 s/^\+[ \t]*/\+$goodtabindent/;
2987 # check for space after cast like "(int) foo" or "(struct foo) bar"
2988 # avoid checking a few false positives:
2989 # "sizeof(<type>)" or "__alignof__(<type>)"
2990 # function pointer declarations like "(*foo)(int) = bar;"
2991 # structure definitions like "(struct foo) { 0 };"
2992 # multiline macros that define functions
2993 # known attributes or the __attribute__ keyword
2994 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
2995 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
2997 "No space is necessary after a cast\n" . $herecurr) &&
2999 $fixed[$fixlinenr] =~
3000 s/(\(\s*$Type\s*\))[ \t]+/$1/;
3004 # Block comment styles
3005 # Networking with an initial /*
3006 if ($realfile =~ m@^(drivers/net/|net/)@ &&
3007 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
3008 $rawline =~ /^\+[ \t]*\*/ &&
3010 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3011 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3014 # Block comments use * on subsequent lines
3015 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3016 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
3017 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
3018 $rawline =~ /^\+/ && #line is new
3019 $rawline !~ /^\+[ \t]*\*/) { #no leading *
3020 WARN("BLOCK_COMMENT_STYLE",
3021 "Block comments use * on subsequent lines\n" . $hereprev);
3024 # Block comments use */ on trailing lines
3025 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
3026 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3027 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3028 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
3029 WARN("BLOCK_COMMENT_STYLE",
3030 "Block comments use a trailing */ on a separate line\n" . $herecurr);
3033 # Block comment * alignment
3034 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3035 $line =~ /^\+[ \t]*$;/ && #leading comment
3036 $rawline =~ /^\+[ \t]*\*/ && #leading *
3037 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
3038 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
3039 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3041 $prevrawline =~ m@^\+([ \t]*/?)\*@;
3043 $oldindent = expand_tabs($1);
3045 $prevrawline =~ m@^\+(.*/?)\*@;
3046 $oldindent = expand_tabs($1);
3048 $rawline =~ m@^\+([ \t]*)\*@;
3050 $newindent = expand_tabs($newindent);
3051 if (length($oldindent) ne length($newindent)) {
3052 WARN("BLOCK_COMMENT_STYLE",
3053 "Block comments should align the * on each line\n" . $hereprev);
3057 # check for missing blank lines after struct/union declarations
3058 # with exceptions for various attributes and macros
3059 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3061 !($line =~ /^\+\s*$/ ||
3062 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3063 $line =~ /^\+\s*MODULE_/i ||
3064 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3065 $line =~ /^\+[a-z_]*init/ ||
3066 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3067 $line =~ /^\+\s*DECLARE/ ||
3068 $line =~ /^\+\s*__setup/)) {
3069 if (CHK("LINE_SPACING",
3070 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3072 fix_insert_line($fixlinenr, "\+");
3076 # check for multiple consecutive blank lines
3077 if ($prevline =~ /^[\+ ]\s*$/ &&
3078 $line =~ /^\+\s*$/ &&
3079 $last_blank_line != ($linenr - 1)) {
3080 if (CHK("LINE_SPACING",
3081 "Please don't use multiple blank lines\n" . $hereprev) &&
3083 fix_delete_line($fixlinenr, $rawline);
3086 $last_blank_line = $linenr;
3089 # check for missing blank lines after declarations
3090 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3091 # actual declarations
3092 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3093 # function pointer declarations
3094 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3095 # foo bar; where foo is some local typedef or #define
3096 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3097 # known declaration macros
3098 $prevline =~ /^\+\s+$declaration_macros/) &&
3099 # for "else if" which can look like "$Ident $Ident"
3100 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3101 # other possible extensions of declaration lines
3102 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3103 # not starting a section or a macro "\" extended line
3104 $prevline =~ /(?:\{\s*|\\)$/) &&
3105 # looks like a declaration
3106 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3107 # function pointer declarations
3108 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3109 # foo bar; where foo is some local typedef or #define
3110 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3111 # known declaration macros
3112 $sline =~ /^\+\s+$declaration_macros/ ||
3113 # start of struct or union or enum
3114 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
3115 # start or end of block or continuation of declaration
3116 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3117 # bitfield continuation
3118 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3119 # other possible extensions of declaration lines
3120 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3121 # indentation of previous and current line are the same
3122 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3123 if (WARN("LINE_SPACING",
3124 "Missing a blank line after declarations\n" . $hereprev) &&
3126 fix_insert_line($fixlinenr, "\+");
3130 # check for spaces at the beginning of a line.
3132 # 1) within comments
3133 # 2) indented preprocessor commands
3135 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
3136 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3137 if (WARN("LEADING_SPACE",
3138 "please, no spaces at the start of a line\n" . $herevet) &&
3140 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3144 # check we are in a valid C source file if not then ignore this hunk
3145 next if ($realfile !~ /\.(h|c)$/);
3147 # check if this appears to be the start function declaration, save the name
3148 if ($sline =~ /^\+\{\s*$/ &&
3149 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3150 $context_function = $1;
3153 # check if this appears to be the end of function declaration
3154 if ($sline =~ /^\+\}\s*$/) {
3155 undef $context_function;
3158 # check indentation of any line with a bare else
3159 # (but not if it is a multiple line "if (foo) return bar; else return baz;")
3160 # if the previous line is a break or return and is indented 1 tab more...
3161 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3162 my $tabs = length($1) + 1;
3163 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3164 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3165 defined $lines[$linenr] &&
3166 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3167 WARN("UNNECESSARY_ELSE",
3168 "else is not generally useful after a break or return\n" . $hereprev);
3172 # check indentation of a line with a break;
3173 # if the previous line is a goto or return and is indented the same # of tabs
3174 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3176 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3177 WARN("UNNECESSARY_BREAK",
3178 "break is not useful after a goto or return\n" . $hereprev);
3182 # check for RCS/CVS revision markers
3183 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3185 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3188 # Blackfin: don't use __builtin_bfin_[cs]sync
3189 if ($line =~ /__builtin_bfin_csync/) {
3190 my $herevet = "$here\n" . cat_vet($line) . "\n";
3192 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
3194 if ($line =~ /__builtin_bfin_ssync/) {
3195 my $herevet = "$here\n" . cat_vet($line) . "\n";
3197 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
3200 # check for old HOTPLUG __dev<foo> section markings
3201 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3202 WARN("HOTPLUG_SECTION",
3203 "Using $1 is unnecessary\n" . $herecurr);
3206 # Check for potential 'bare' types
3207 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3209 #print "LINE<$line>\n";
3210 if ($linenr >= $suppress_statement &&
3211 $realcnt && $sline =~ /.\s*\S/) {
3212 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3213 ctx_statement_block($linenr, $realcnt, 0);
3214 $stat =~ s/\n./\n /g;
3215 $cond =~ s/\n./\n /g;
3217 #print "linenr<$linenr> <$stat>\n";
3218 # If this statement has no statement boundaries within
3219 # it there is no point in retrying a statement scan
3220 # until we hit end of it.
3221 my $frag = $stat; $frag =~ s/;+\s*$//;
3222 if ($frag !~ /(?:{|;)/) {
3223 #print "skip<$line_nr_next>\n";
3224 $suppress_statement = $line_nr_next;
3227 # Find the real next line.
3228 $realline_next = $line_nr_next;
3229 if (defined $realline_next &&
3230 (!defined $lines[$realline_next - 1] ||
3231 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3238 # Ignore goto labels.
3239 if ($s =~ /$Ident:\*$/s) {
3241 # Ignore functions being called
3242 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3244 } elsif ($s =~ /^.\s*else\b/s) {
3246 # declarations always start with types
3247 } 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) {
3250 possible($type, "A:" . $s);
3252 # definitions in global scope can only start with types
3253 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3254 possible($1, "B:" . $s);
3257 # any (foo ... *) is a pointer cast, and foo is a type
3258 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3259 possible($1, "C:" . $s);
3262 # Check for any sort of function declaration.
3263 # int foo(something bar, other baz);
3264 # void (*store_gdt)(x86_descr_ptr *);
3265 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
3266 my ($name_len) = length($1);
3269 substr($ctx, 0, $name_len + 1, '');
3270 $ctx =~ s/\)[^\)]*$//;
3272 for my $arg (split(/\s*,\s*/, $ctx)) {
3273 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
3275 possible($1, "D:" . $s);
3283 # Checks which may be anchored in the context.
3286 # Check for switch () and associated case and default
3287 # statements should be at the same indent.
3288 if ($line=~/\bswitch\s*\(.*\)/) {
3291 my @ctx = ctx_block_outer($linenr, $realcnt);
3293 for my $ctx (@ctx) {
3294 my ($clen, $cindent) = line_stats($ctx);
3295 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3296 $indent != $cindent) {
3297 $err .= "$sep$ctx\n";
3304 ERROR("SWITCH_CASE_INDENT_LEVEL",
3305 "switch and case should be at the same indent\n$hereline$err");
3309 # if/while/etc brace do not go on next line, unless defining a do while loop,
3310 # or if that brace on the next line is for something else
3311 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3312 my $pre_ctx = "$1$2";
3314 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
3316 if ($line =~ /^\+\t{6,}/) {
3317 WARN("DEEP_INDENTATION",
3318 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3321 my $ctx_cnt = $realcnt - $#ctx - 1;
3322 my $ctx = join("\n", @ctx);
3324 my $ctx_ln = $linenr;
3325 my $ctx_skip = $realcnt;
3327 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3328 defined $lines[$ctx_ln - 1] &&
3329 $lines[$ctx_ln - 1] =~ /^-/)) {
3330 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3331 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3335 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3336 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3338 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3340 "that open brace { should be on the previous line\n" .
3341 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3343 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3344 $ctx =~ /\)\s*\;\s*$/ &&
3345 defined $lines[$ctx_ln - 1])
3347 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3348 if ($nindent > $indent) {
3349 WARN("TRAILING_SEMICOLON",
3350 "trailing semicolon indicates no statements, indent implies otherwise\n" .
3351 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
3356 # Check relative indent for conditionals and blocks.
3357 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
3358 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3359 ctx_statement_block($linenr, $realcnt, 0)
3360 if (!defined $stat);
3361 my ($s, $c) = ($stat, $cond);
3363 substr($s, 0, length($c), '');
3365 # remove inline comments
3369 # Find out how long the conditional actually is.
3370 my @newlines = ($c =~ /\n/gs);
3371 my $cond_lines = 1 + $#newlines;
3373 # Make sure we remove the line prefixes as we have
3374 # none on the first line, and are going to readd them
3377 while ($s =~ /\n\s+\\\n/) {
3378 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3381 # We want to check the first line inside the block
3382 # starting at the end of the conditional, so remove:
3383 # 1) any blank line termination
3384 # 2) any opening brace { on end of the line
3386 my $continuation = 0;
3388 $s =~ s/^.*\bdo\b//;
3390 if ($s =~ s/^\s*\\//) {
3393 if ($s =~ s/^\s*?\n//) {
3398 # Also ignore a loop construct at the end of a
3399 # preprocessor statement.
3400 if (($prevline =~ /^.\s*#\s*define\s/ ||
3401 $prevline =~ /\\\s*$/) && $continuation == 0) {
3407 while ($cond_ptr != $cond_lines) {
3408 $cond_ptr = $cond_lines;
3410 # If we see an #else/#elif then the code
3412 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3417 # 1) blank lines, they should be at 0,
3418 # 2) preprocessor lines, and
3420 if ($continuation ||
3422 $s =~ /^\s*#\s*?/ ||
3423 $s =~ /^\s*$Ident\s*:/) {
3424 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
3425 if ($s =~ s/^.*?\n//) {
3431 my (undef, $sindent) = line_stats("+" . $s);
3432 my $stat_real = raw_line($linenr, $cond_lines);
3434 # Check if either of these lines are modified, else
3435 # this is not this patch's fault.
3436 if (!defined($stat_real) ||
3437 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3440 if (defined($stat_real) && $cond_lines > 1) {
3441 $stat_real = "[...]\n$stat_real";
3444 #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";
3446 if ($check && $s ne '' &&
3447 (($sindent % 8) != 0 ||
3448 ($sindent < $indent) ||
3449 ($sindent == $indent &&
3450 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
3451 ($sindent > $indent + 8))) {
3452 WARN("SUSPECT_CODE_INDENT",
3453 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
3457 # Track the 'values' across context and added lines.
3458 my $opline = $line; $opline =~ s/^./ /;
3459 my ($curr_values, $curr_vars) =
3460 annotate_values($opline . "\n", $prev_values);
3461 $curr_values = $prev_values . $curr_values;
3463 my $outline = $opline; $outline =~ s/\t/ /g;
3464 print "$linenr > .$outline\n";
3465 print "$linenr > $curr_values\n";
3466 print "$linenr > $curr_vars\n";
3468 $prev_values = substr($curr_values, -1);
3470 #ignore lines not being added
3471 next if ($line =~ /^[^\+]/);
3473 # check for dereferences that span multiple lines
3474 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3475 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3476 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3478 $line =~ /^.\s*($Lval)/;
3481 WARN("MULTILINE_DEREFERENCE",
3482 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3485 # check for declarations of signed or unsigned without int
3486 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3489 $var = "" if (!defined $var);
3490 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3494 $pointer = "" if (!defined $pointer);
3496 if (WARN("UNSPECIFIED_INT",
3497 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3499 my $decl = trim($sign) . " int ";
3500 my $comp_pointer = $pointer;
3501 $comp_pointer =~ s/\s//g;
3502 $decl .= $comp_pointer;
3503 $decl = rtrim($decl) if ($var eq "");
3504 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3509 # TEST: allow direct testing of the type matcher.
3511 if ($line =~ /^.\s*$Declare\s*$/) {
3513 "TEST: is type\n" . $herecurr);
3514 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3515 ERROR("TEST_NOT_TYPE",
3516 "TEST: is not type ($1 is)\n". $herecurr);
3520 # TEST: allow direct testing of the attribute matcher.
3522 if ($line =~ /^.\s*$Modifier\s*$/) {
3524 "TEST: is attr\n" . $herecurr);
3525 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3526 ERROR("TEST_NOT_ATTR",
3527 "TEST: is not attr ($1 is)\n". $herecurr);
3532 # check for initialisation to aggregates open brace on the next line
3533 if ($line =~ /^.\s*{/ &&
3534 $prevline =~ /(?:^|[^=])=\s*$/) {
3535 if (ERROR("OPEN_BRACE",
3536 "that open brace { should be on the previous line\n" . $hereprev) &&
3537 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3538 fix_delete_line($fixlinenr - 1, $prevrawline);
3539 fix_delete_line($fixlinenr, $rawline);
3540 my $fixedline = $prevrawline;
3541 $fixedline =~ s/\s*=\s*$/ = {/;
3542 fix_insert_line($fixlinenr, $fixedline);
3544 $fixedline =~ s/^(.\s*){\s*/$1/;
3545 fix_insert_line($fixlinenr, $fixedline);
3550 # Checks which are anchored on the added line.
3553 # check for malformed paths in #include statements (uses RAW line)
3554 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3556 if ($path =~ m{//}) {
3557 ERROR("MALFORMED_INCLUDE",
3558 "malformed #include filename\n" . $herecurr);
3560 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3561 ERROR("UAPI_INCLUDE",
3562 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3566 # no C99 // comments
3567 if ($line =~ m{//}) {
3568 if (ERROR("C99_COMMENTS",
3569 "do not use C99 // comments\n" . $herecurr) &&
3571 my $line = $fixed[$fixlinenr];
3572 if ($line =~ /\/\/(.*)$/) {
3573 my $comment = trim($1);
3574 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
3578 # Remove C99 comments.
3580 $opline =~ s@//.*@@;
3582 # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3583 # the whole statement.
3584 #print "APW <$lines[$realline_next - 1]>\n";
3585 if (defined $realline_next &&
3586 exists $lines[$realline_next - 1] &&
3587 !defined $suppress_export{$realline_next} &&
3588 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3589 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3590 # Handle definitions which produce identifiers with
3593 # EXPORT_SYMBOL(something_foo);
3595 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
3596 $name =~ /^${Ident}_$2/) {
3597 #print "FOO C name<$name>\n";
3598 $suppress_export{$realline_next} = 1;
3600 } elsif ($stat !~ /(?:
3602 ^.DEFINE_$Ident\(\Q$name\E\)|
3603 ^.DECLARE_$Ident\(\Q$name\E\)|
3604 ^.LIST_HEAD\(\Q$name\E\)|
3605 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3606 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
3608 #print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3609 $suppress_export{$realline_next} = 2;
3611 $suppress_export{$realline_next} = 1;
3614 if (!defined $suppress_export{$linenr} &&
3615 $prevline =~ /^.\s*$/ &&
3616 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3617 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3618 #print "FOO B <$lines[$linenr - 1]>\n";
3619 $suppress_export{$linenr} = 2;
3621 if (defined $suppress_export{$linenr} &&
3622 $suppress_export{$linenr} == 2) {
3623 WARN("EXPORT_SYMBOL",
3624 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
3627 # check for global initialisers.
3628 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3629 if (ERROR("GLOBAL_INITIALISERS",
3630 "do not initialise globals to $1\n" . $herecurr) &&
3632 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3635 # check for static initialisers.
3636 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3637 if (ERROR("INITIALISED_STATIC",
3638 "do not initialise statics to $1\n" .
3641 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3645 # check for misordered declarations of char/short/int/long with signed/unsigned
3646 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3648 WARN("MISORDERED_TYPE",
3649 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3652 # check for static const char * arrays.
3653 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3654 WARN("STATIC_CONST_CHAR_ARRAY",
3655 "static const char * array should probably be static const char * const\n" .
3659 # check for static char foo[] = "bar" declarations.
3660 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3661 WARN("STATIC_CONST_CHAR_ARRAY",
3662 "static char array declaration should probably be static const char\n" .
3666 # check for const <foo> const where <foo> is not a pointer or array type
3667 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3669 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3671 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3672 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3674 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3678 # check for non-global char *foo[] = {"bar", ...} declarations.
3679 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3680 WARN("STATIC_CONST_CHAR_ARRAY",
3681 "char * array declaration might be better as static const\n" .
3685 # check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3686 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3688 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3690 if (WARN("ARRAY_SIZE",
3691 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3693 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3698 # check for function declarations without arguments like "int foo()"
3699 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3700 if (ERROR("FUNCTION_WITHOUT_ARGS",
3701 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3703 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3707 # check for new typedefs, only function parameters and sparse annotations
3709 if ($line =~ /\btypedef\s/ &&
3710 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3711 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
3712 $line !~ /\b$typeTypedefs\b/ &&
3713 $line !~ /\b__bitwise\b/) {
3714 WARN("NEW_TYPEDEFS",
3715 "do not add new typedefs\n" . $herecurr);
3718 # * goes on variable not on type
3720 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3722 my ($ident, $from, $to) = ($1, $2, $2);
3724 # Should start with a space.
3725 $to =~ s/^(\S)/ $1/;
3726 # Should not end with a space.
3728 # '*'s should not have spaces between.
3729 while ($to =~ s/\*\s+\*/\*\*/) {
3732 ## print "1: from<$from> to<$to> ident<$ident>\n";
3734 if (ERROR("POINTER_LOCATION",
3735 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3737 my $sub_from = $ident;
3738 my $sub_to = $ident;
3739 $sub_to =~ s/\Q$from\E/$to/;
3740 $fixed[$fixlinenr] =~
3741 s@\Q$sub_from\E@$sub_to@;
3745 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3747 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3749 # Should start with a space.
3750 $to =~ s/^(\S)/ $1/;
3751 # Should not end with a space.
3753 # '*'s should not have spaces between.
3754 while ($to =~ s/\*\s+\*/\*\*/) {
3756 # Modifiers should have spaces.
3757 $to =~ s/(\b$Modifier$)/$1 /;
3759 ## print "2: from<$from> to<$to> ident<$ident>\n";
3760 if ($from ne $to && $ident !~ /^$Modifier$/) {
3761 if (ERROR("POINTER_LOCATION",
3762 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3765 my $sub_from = $match;
3766 my $sub_to = $match;
3767 $sub_to =~ s/\Q$from\E/$to/;
3768 $fixed[$fixlinenr] =~
3769 s@\Q$sub_from\E@$sub_to@;
3774 # avoid BUG() or BUG_ON()
3775 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3776 my $msg_type = \&WARN;
3777 $msg_type = \&CHK if ($file);
3778 &{$msg_type}("AVOID_BUG",
3779 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3782 # avoid LINUX_VERSION_CODE
3783 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3784 WARN("LINUX_VERSION_CODE",
3785 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
3788 # check for uses of printk_ratelimit
3789 if ($line =~ /\bprintk_ratelimit\s*\(/) {
3790 WARN("PRINTK_RATELIMITED",
3791 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
3794 # printk should use KERN_* levels. Note that follow on printk's on the
3795 # same line do not need a level, so we use the current block context
3796 # to try and find and validate the current printk. In summary the current
3797 # printk includes all preceding printk's which have no newline on the end.
3798 # we assume the first bad printk is the one to report.
3799 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
3801 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3802 #print "CHECK<$lines[$ln - 1]\n";
3803 # we have a preceding printk if it ends
3804 # with "\n" ignore it, else it is to blame
3805 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3806 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3813 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3814 "printk() should include KERN_ facility level\n" . $herecurr);
3818 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3820 my $level = lc($orig);
3821 $level = "warn" if ($level eq "warning");
3822 my $level2 = $level;
3823 $level2 = "dbg" if ($level eq "debug");
3824 WARN("PREFER_PR_LEVEL",
3825 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
3828 if ($line =~ /\bpr_warning\s*\(/) {
3829 if (WARN("PREFER_PR_LEVEL",
3830 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3832 $fixed[$fixlinenr] =~
3833 s/\bpr_warning\b/pr_warn/;
3837 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3839 my $level = lc($orig);
3840 $level = "warn" if ($level eq "warning");
3841 $level = "dbg" if ($level eq "debug");
3842 WARN("PREFER_DEV_LEVEL",
3843 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3846 # ENOSYS means "bad syscall nr" and nothing else. This will have a small
3847 # number of false positives, but assembly files are not checked, so at
3848 # least the arch entry code will not trigger this warning.
3849 if ($line =~ /\bENOSYS\b/) {
3851 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3854 # function brace can't be on same line, except for #defines of do while,
3855 # or if closed on same line
3856 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
3857 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
3858 if (ERROR("OPEN_BRACE",
3859 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3861 fix_delete_line($fixlinenr, $rawline);
3862 my $fixed_line = $rawline;
3863 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3866 fix_insert_line($fixlinenr, ltrim($line1));
3867 fix_insert_line($fixlinenr, "\+{");
3868 if ($line2 !~ /^\s*$/) {
3869 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3874 # open braces for enum, union and struct go on the same line.
3875 if ($line =~ /^.\s*{/ &&
3876 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
3877 if (ERROR("OPEN_BRACE",
3878 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3879 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3880 fix_delete_line($fixlinenr - 1, $prevrawline);
3881 fix_delete_line($fixlinenr, $rawline);
3882 my $fixedline = rtrim($prevrawline) . " {";
3883 fix_insert_line($fixlinenr, $fixedline);
3884 $fixedline = $rawline;
3885 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3886 if ($fixedline !~ /^\+\s*$/) {
3887 fix_insert_line($fixlinenr, $fixedline);
3892 # missing space after union, struct or enum definition
3893 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3895 "missing space after $1 definition\n" . $herecurr) &&
3897 $fixed[$fixlinenr] =~
3898 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3902 # Function pointer declarations
3903 # check spacing between type, funcptr, and args
3904 # canonical declaration is "type (*funcptr)(args...)"
3905 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
3907 my $pre_pointer_space = $2;
3908 my $post_pointer_space = $3;
3910 my $post_funcname_space = $5;
3911 my $pre_args_space = $6;
3913 # the $Declare variable will capture all spaces after the type
3914 # so check it for a missing trailing missing space but pointer return types
3915 # don't need a space so don't warn for those.
3916 my $post_declare_space = "";
3917 if ($declare =~ /(\s+)$/) {
3918 $post_declare_space = $1;
3919 $declare = rtrim($declare);
3921 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
3923 "missing space after return type\n" . $herecurr);
3924 $post_declare_space = " ";
3927 # unnecessary space "type (*funcptr)(args...)"
3928 # This test is not currently implemented because these declarations are
3930 # int foo(int bar, ...)
3931 # and this is form shouldn't/doesn't generate a checkpatch warning.
3933 # elsif ($declare =~ /\s{2,}$/) {
3935 # "Multiple spaces after return type\n" . $herecurr);
3938 # unnecessary space "type ( *funcptr)(args...)"
3939 if (defined $pre_pointer_space &&
3940 $pre_pointer_space =~ /^\s/) {
3942 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3945 # unnecessary space "type (* funcptr)(args...)"
3946 if (defined $post_pointer_space &&
3947 $post_pointer_space =~ /^\s/) {
3949 "Unnecessary space before function pointer name\n" . $herecurr);
3952 # unnecessary space "type (*funcptr )(args...)"
3953 if (defined $post_funcname_space &&
3954 $post_funcname_space =~ /^\s/) {
3956 "Unnecessary space after function pointer name\n" . $herecurr);
3959 # unnecessary space "type (*funcptr) (args...)"
3960 if (defined $pre_args_space &&
3961 $pre_args_space =~ /^\s/) {
3963 "Unnecessary space before function pointer arguments\n" . $herecurr);
3966 if (show_type("SPACING") && $fix) {
3967 $fixed[$fixlinenr] =~
3968 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
3972 # check for spacing round square brackets; allowed:
3973 # 1. with a type on the left -- int [] a;
3974 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3975 # 3. inside a curly brace -- = { [0...10] = 5 }
3976 while ($line =~ /(.*?\s)\[/g) {
3977 my ($where, $prefix) = ($-[1], $1);
3978 if ($prefix !~ /$Type\s+$/ &&
3979 ($where != 0 || $prefix !~ /^.\s+$/) &&
3980 $prefix !~ /[{,]\s+$/) {
3981 if (ERROR("BRACKET_SPACE",
3982 "space prohibited before open square bracket '['\n" . $herecurr) &&
3984 $fixed[$fixlinenr] =~
3985 s/^(\+.*?)\s+\[/$1\[/;
3990 # check for spaces between functions and their parentheses.
3991 while ($line =~ /($Ident)\s+\(/g) {
3993 my $ctx_before = substr($line, 0, $-[1]);
3994 my $ctx = "$ctx_before$name";
3996 # Ignore those directives where spaces _are_ permitted.
3998 if|for|while|switch|return|case|
3999 volatile|__volatile__|
4000 __attribute__|format|__extension__|
4003 # cpp #define statements have non-optional spaces, ie
4004 # if there is a space between the name and the open
4005 # parenthesis it is simply not a parameter group.
4006 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4008 # cpp #elif statement condition may start with a (
4009 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4011 # If this whole things ends with a type its most
4012 # likely a typedef for a function.
4013 } elsif ($ctx =~ /$Type$/) {
4017 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4019 $fixed[$fixlinenr] =~
4020 s/\b$name\s+\(/$name\(/;
4025 # Check operator spacing.
4026 if (!($line=~/\#\s*include/)) {
4027 my $fixed_line = "";
4031 <<=|>>=|<=|>=|==|!=|
4032 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4033 =>|->|<<|>>|<|>|=|!|~|
4034 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
4037 my @elements = split(/($ops|;)/, $opline);
4039 ## print("element count: <" . $#elements . ">\n");
4040 ## foreach my $el (@elements) {
4041 ## print("el: <$el>\n");
4044 my @fix_elements = ();
4047 foreach my $el (@elements) {
4048 push(@fix_elements, substr($rawline, $off, length($el)));
4049 $off += length($el);
4054 my $blank = copy_spacing($opline);
4055 my $last_after = -1;
4057 for (my $n = 0; $n < $#elements; $n += 2) {
4059 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4061 ## print("n: <$n> good: <$good>\n");
4063 $off += length($elements[$n]);
4065 # Pick up the preceding and succeeding characters.
4066 my $ca = substr($opline, 0, $off);
4068 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4069 $cc = substr($opline, $off + length($elements[$n + 1]));
4071 my $cb = "$ca$;$cc";
4074 $a = 'V' if ($elements[$n] ne '');
4075 $a = 'W' if ($elements[$n] =~ /\s$/);
4076 $a = 'C' if ($elements[$n] =~ /$;$/);
4077 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4078 $a = 'O' if ($elements[$n] eq '');
4079 $a = 'E' if ($ca =~ /^\s*$/);
4081 my $op = $elements[$n + 1];
4084 if (defined $elements[$n + 2]) {
4085 $c = 'V' if ($elements[$n + 2] ne '');
4086 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
4087 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
4088 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4089 $c = 'O' if ($elements[$n + 2] eq '');
4090 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4095 my $ctx = "${a}x${c}";
4097 my $at = "(ctx:$ctx)";
4099 my $ptr = substr($blank, 0, $off) . "^";
4100 my $hereptr = "$hereline$ptr\n";
4102 # Pull out the value of this operator.
4103 my $op_type = substr($curr_values, $off + 1, 1);
4105 # Get the full operator variant.
4106 my $opv = $op . substr($curr_vars, $off, 1);
4108 # Ignore operators passed as parameters.
4109 if ($op_type ne 'V' &&
4110 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
4113 # } elsif ($op =~ /^$;+$/) {
4115 # ; should have either the end of line or a space or \ after it
4116 } elsif ($op eq ';') {
4117 if ($ctx !~ /.x[WEBC]/ &&
4118 $cc !~ /^\\/ && $cc !~ /^;/) {
4119 if (ERROR("SPACING",
4120 "space required after that '$op' $at\n" . $hereptr)) {
4121 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4127 } elsif ($op eq '//') {
4129 # : when part of a bitfield
4130 } elsif ($opv eq ':B') {
4131 # skip the bitfield test for now
4135 } elsif ($op eq '->') {
4136 if ($ctx =~ /Wx.|.xW/) {
4137 if (ERROR("SPACING",
4138 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4139 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4140 if (defined $fix_elements[$n + 2]) {
4141 $fix_elements[$n + 2] =~ s/^\s+//;
4147 # , must not have a space before and must have a space on the right.
4148 } elsif ($op eq ',') {
4149 my $rtrim_before = 0;
4150 my $space_after = 0;
4151 if ($ctx =~ /Wx./) {
4152 if (ERROR("SPACING",
4153 "space prohibited before that '$op' $at\n" . $hereptr)) {
4158 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
4159 if (ERROR("SPACING",
4160 "space required after that '$op' $at\n" . $hereptr)) {
4166 if ($rtrim_before || $space_after) {
4167 if ($rtrim_before) {
4168 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4170 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4177 # '*' as part of a type definition -- reported already.
4178 } elsif ($opv eq '*_') {
4179 #warn "'*' is part of type\n";
4181 # unary operators should have a space before and
4182 # none after. May be left adjacent to another
4183 # unary operator, or a cast
4184 } elsif ($op eq '!' || $op eq '~' ||
4185 $opv eq '*U' || $opv eq '-U' ||
4186 $opv eq '&U' || $opv eq '&&U') {
4187 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
4188 if (ERROR("SPACING",
4189 "space required before that '$op' $at\n" . $hereptr)) {
4190 if ($n != $last_after + 2) {
4191 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4196 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4197 # A unary '*' may be const
4199 } elsif ($ctx =~ /.xW/) {
4200 if (ERROR("SPACING",
4201 "space prohibited after that '$op' $at\n" . $hereptr)) {
4202 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
4203 if (defined $fix_elements[$n + 2]) {
4204 $fix_elements[$n + 2] =~ s/^\s+//;
4210 # unary ++ and unary -- are allowed no space on one side.
4211 } elsif ($op eq '++' or $op eq '--') {
4212 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
4213 if (ERROR("SPACING",
4214 "space required one side of that '$op' $at\n" . $hereptr)) {
4215 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
4219 if ($ctx =~ /Wx[BE]/ ||
4220 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
4221 if (ERROR("SPACING",
4222 "space prohibited before that '$op' $at\n" . $hereptr)) {
4223 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4227 if ($ctx =~ /ExW/) {
4228 if (ERROR("SPACING",
4229 "space prohibited after that '$op' $at\n" . $hereptr)) {
4230 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4231 if (defined $fix_elements[$n + 2]) {
4232 $fix_elements[$n + 2] =~ s/^\s+//;
4238 # << and >> may either have or not have spaces both sides
4239 } elsif ($op eq '<<' or $op eq '>>' or
4240 $op eq '&' or $op eq '^' or $op eq '|' or
4241 $op eq '+' or $op eq '-' or
4242 $op eq '*' or $op eq '/' or
4246 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4248 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4249 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4250 $fix_elements[$n + 2] =~ s/^\s+//;
4253 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4255 "space preferred before that '$op' $at\n" . $hereptr)) {
4256 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4260 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
4261 if (ERROR("SPACING",
4262 "need consistent spacing around '$op' $at\n" . $hereptr)) {
4263 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4264 if (defined $fix_elements[$n + 2]) {
4265 $fix_elements[$n + 2] =~ s/^\s+//;
4271 # A colon needs no spaces before when it is
4272 # terminating a case value or a label.
4273 } elsif ($opv eq ':C' || $opv eq ':L') {
4274 if ($ctx =~ /Wx./) {
4275 if (ERROR("SPACING",
4276 "space prohibited before that '$op' $at\n" . $hereptr)) {
4277 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4282 # All the others need spaces both sides.
4283 } elsif ($ctx !~ /[EWC]x[CWE]/) {
4286 # Ignore email addresses <foo@bar>
4288 $cc =~ /^\S+\@\S+>/) ||
4290 $ca =~ /<\S+\@\S+$/))
4295 # for asm volatile statements
4296 # ignore a colon with another
4297 # colon immediately before or after
4299 ($ca =~ /:$/ || $cc =~ /^:/)) {
4303 # messages are ERROR, but ?: are CHK
4305 my $msg_type = \&ERROR;
4306 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4308 if (&{$msg_type}("SPACING",
4309 "spaces required around that '$op' $at\n" . $hereptr)) {
4310 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4311 if (defined $fix_elements[$n + 2]) {
4312 $fix_elements[$n + 2] =~ s/^\s+//;
4318 $off += length($elements[$n + 1]);
4320 ## print("n: <$n> GOOD: <$good>\n");
4322 $fixed_line = $fixed_line . $good;
4325 if (($#elements % 2) == 0) {
4326 $fixed_line = $fixed_line . $fix_elements[$#elements];
4329 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4330 $fixed[$fixlinenr] = $fixed_line;
4336 # check for whitespace before a non-naked semicolon
4337 if ($line =~ /^\+.*\S\s+;\s*$/) {
4339 "space prohibited before semicolon\n" . $herecurr) &&
4341 1 while $fixed[$fixlinenr] =~
4342 s/^(\+.*\S)\s+;/$1;/;
4346 # check for multiple assignments
4347 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4348 CHK("MULTIPLE_ASSIGNMENTS",
4349 "multiple assignments should be avoided\n" . $herecurr);
4352 ## # check for multiple declarations, allowing for a function declaration
4354 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4355 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4357 ## # Remove any bracketed sections to ensure we do not
4358 ## # falsly report the parameters of functions.
4360 ## while ($ln =~ s/\([^\(\)]*\)//g) {
4362 ## if ($ln =~ /,/) {
4363 ## WARN("MULTIPLE_DECLARATION",
4364 ## "declaring multiple variables together should be avoided\n" . $herecurr);
4368 #need space before brace following if, while, etc
4369 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
4371 if (ERROR("SPACING",
4372 "space required before the open brace '{'\n" . $herecurr) &&
4374 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
4378 ## # check for blank lines before declarations
4379 ## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4380 ## $prevrawline =~ /^.\s*$/) {
4382 ## "No blank lines before declarations\n" . $hereprev);
4386 # closing brace should have a space following it when it has anything
4388 if ($line =~ /}(?!(?:,|;|\)))\S/) {
4389 if (ERROR("SPACING",
4390 "space required after that close brace '}'\n" . $herecurr) &&
4392 $fixed[$fixlinenr] =~
4393 s/}((?!(?:,|;|\)))\S)/} $1/;
4397 # check spacing on square brackets
4398 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
4399 if (ERROR("SPACING",
4400 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4402 $fixed[$fixlinenr] =~
4406 if ($line =~ /\s\]/) {
4407 if (ERROR("SPACING",
4408 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4410 $fixed[$fixlinenr] =~
4415 # check spacing on parentheses
4416 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4417 $line !~ /for\s*\(\s+;/) {
4418 if (ERROR("SPACING",
4419 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4421 $fixed[$fixlinenr] =~
4425 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4426 $line !~ /for\s*\(.*;\s+\)/ &&
4427 $line !~ /:\s+\)/) {
4428 if (ERROR("SPACING",
4429 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4431 $fixed[$fixlinenr] =~
4436 # check unnecessary parentheses around addressof/dereference single $Lvals
4437 # ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4439 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4441 if (CHK("UNNECESSARY_PARENTHESES",
4442 "Unnecessary parentheses around $var\n" . $herecurr) &&
4444 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4448 # check for unnecessary parentheses around function pointer uses
4449 # ie: (foo->bar)(); should be foo->bar();
4450 # but not "if (foo->bar) (" to avoid some false positives
4451 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4453 if (CHK("UNNECESSARY_PARENTHESES",
4454 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4456 my $var2 = deparenthesize($var);
4458 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4462 #goto labels aren't indented, allow a single space however
4463 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
4464 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
4465 if (WARN("INDENTED_LABEL",
4466 "labels should not be indented\n" . $herecurr) &&
4468 $fixed[$fixlinenr] =~
4473 # return is not a function
4474 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4476 if ($^V && $^V ge 5.10.0 &&
4477 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4479 $value = deparenthesize($value);
4480 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4481 ERROR("RETURN_PARENTHESES",
4482 "return is not a function, parentheses are not required\n" . $herecurr);
4484 } elsif ($spacing !~ /\s+/) {
4486 "space required before the open parenthesis '('\n" . $herecurr);
4490 # unnecessary return in a void function
4491 # at end-of-function, with the previous line a single leading tab, then return;
4492 # and the line before that not a goto label target like "out:"
4493 if ($sline =~ /^[ \+]}\s*$/ &&
4494 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4496 $lines[$linenr - 3] =~ /^[ +]/ &&
4497 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
4499 "void function return statements are not generally useful\n" . $hereprev);
4502 # if statements using unnecessary parentheses - ie: if ((foo == bar))
4503 if ($^V && $^V ge 5.10.0 &&
4504 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4505 my $openparens = $1;
4506 my $count = $openparens =~ tr@\(@\(@;
4508 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4509 my $comp = $4; #Not $1 because of $LvalOrFunc
4510 $msg = " - maybe == should be = ?" if ($comp eq "==");
4511 WARN("UNNECESSARY_PARENTHESES",
4512 "Unnecessary parentheses$msg\n" . $herecurr);
4516 # comparisons with a constant or upper case identifier on the left
4517 # avoid cases like "foo + BAR < baz"
4518 # only fix matches surrounded by parentheses to avoid incorrect
4519 # conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4520 if ($^V && $^V ge 5.10.0 &&
4521 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4526 my $newcomp = $comp;
4527 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4528 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4529 WARN("CONSTANT_COMPARISON",
4530 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4534 } elsif ($comp eq "<=") {
4536 } elsif ($comp eq ">") {
4538 } elsif ($comp eq ">=") {
4541 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4545 # Return of what appears to be an errno should normally be negative
4546 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
4548 if ($name ne 'EOF' && $name ne 'ERROR') {
4549 WARN("USE_NEGATIVE_ERRNO",
4550 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
4554 # Need a space before open parenthesis after if, while etc
4555 if ($line =~ /\b(if|while|for|switch)\(/) {
4556 if (ERROR("SPACING",
4557 "space required before the open parenthesis '('\n" . $herecurr) &&
4559 $fixed[$fixlinenr] =~
4560 s/\b(if|while|for|switch)\(/$1 \(/;
4564 # Check for illegal assignment in if conditional -- and check for trailing
4565 # statements after the conditional.
4566 if ($line =~ /do\s*(?!{)/) {
4567 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4568 ctx_statement_block($linenr, $realcnt, 0)
4569 if (!defined $stat);
4570 my ($stat_next) = ctx_statement_block($line_nr_next,
4571 $remain_next, $off_next);
4572 $stat_next =~ s/\n./\n /g;
4573 ##print "stat<$stat> stat_next<$stat_next>\n";
4575 if ($stat_next =~ /^\s*while\b/) {
4576 # If the statement carries leading newlines,
4577 # then count those as offsets.
4579 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4581 statement_rawlines($whitespace) - 1;
4583 $suppress_whiletrailers{$line_nr_next +
4587 if (!defined $suppress_whiletrailers{$linenr} &&
4588 defined($stat) && defined($cond) &&
4589 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4590 my ($s, $c) = ($stat, $cond);
4592 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4593 ERROR("ASSIGN_IN_IF",
4594 "do not use assignment in if condition\n" . $herecurr);
4597 # Find out what is on the end of the line after the
4599 substr($s, 0, length($c), '');
4601 $s =~ s/$;//g; # Remove any comments
4602 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4603 $c !~ /}\s*while\s*/)
4605 # Find out how long the conditional actually is.
4606 my @newlines = ($c =~ /\n/gs);
4607 my $cond_lines = 1 + $#newlines;
4610 $stat_real = raw_line($linenr, $cond_lines)
4611 . "\n" if ($cond_lines);
4612 if (defined($stat_real) && $cond_lines > 1) {
4613 $stat_real = "[...]\n$stat_real";
4616 ERROR("TRAILING_STATEMENTS",
4617 "trailing statements should be on next line\n" . $herecurr . $stat_real);
4621 # Check for bitwise tests written as boolean
4633 WARN("HEXADECIMAL_BOOLEAN_TEST",
4634 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
4637 # if and else should not have general statements after it
4638 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4640 $s =~ s/$;//g; # Remove any comments
4641 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4642 ERROR("TRAILING_STATEMENTS",
4643 "trailing statements should be on next line\n" . $herecurr);
4646 # if should not continue a brace
4647 if ($line =~ /}\s*if\b/) {
4648 ERROR("TRAILING_STATEMENTS",
4649 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
4652 # case and default should not have general statements after them
4653 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4655 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4659 ERROR("TRAILING_STATEMENTS",
4660 "trailing statements should be on next line\n" . $herecurr);
4663 # Check for }<nl>else {, these must be at the same
4664 # indent level to be relevant to each other.
4665 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4666 $previndent == $indent) {
4667 if (ERROR("ELSE_AFTER_BRACE",
4668 "else should follow close brace '}'\n" . $hereprev) &&
4669 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4670 fix_delete_line($fixlinenr - 1, $prevrawline);
4671 fix_delete_line($fixlinenr, $rawline);
4672 my $fixedline = $prevrawline;
4673 $fixedline =~ s/}\s*$//;
4674 if ($fixedline !~ /^\+\s*$/) {
4675 fix_insert_line($fixlinenr, $fixedline);
4677 $fixedline = $rawline;
4678 $fixedline =~ s/^(.\s*)else/$1} else/;
4679 fix_insert_line($fixlinenr, $fixedline);
4683 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4684 $previndent == $indent) {
4685 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4687 # Find out what is on the end of the line after the
4689 substr($s, 0, length($c), '');
4692 if ($s =~ /^\s*;/) {
4693 if (ERROR("WHILE_AFTER_BRACE",
4694 "while should follow close brace '}'\n" . $hereprev) &&
4695 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4696 fix_delete_line($fixlinenr - 1, $prevrawline);
4697 fix_delete_line($fixlinenr, $rawline);
4698 my $fixedline = $prevrawline;
4699 my $trailing = $rawline;
4700 $trailing =~ s/^\+//;
4701 $trailing = trim($trailing);
4702 $fixedline =~ s/}\s*$/} $trailing/;
4703 fix_insert_line($fixlinenr, $fixedline);
4708 #Specific variable tests
4709 while ($line =~ m{($Constant|$Lval)}g) {
4712 #gcc binary extension
4713 if ($var =~ /^$Binary$/) {
4714 if (WARN("GCC_BINARY_CONSTANT",
4715 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4717 my $hexval = sprintf("0x%x", oct($var));
4718 $fixed[$fixlinenr] =~
4719 s/\b$var\b/$hexval/;
4724 if ($var !~ /^$Constant$/ &&
4725 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
4726 #Ignore Page<foo> variants
4727 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
4728 #Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4729 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4730 #Ignore some three character SI units explicitly, like MiB and KHz
4731 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
4732 while ($var =~ m{($Ident)}g) {
4734 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4736 seed_camelcase_includes();
4737 if (!$file && !$camelcase_file_seeded) {
4738 seed_camelcase_file($realfile);
4739 $camelcase_file_seeded = 1;
4742 if (!defined $camelcase{$word}) {
4743 $camelcase{$word} = 1;
4745 "Avoid CamelCase: <$word>\n" . $herecurr);
4751 #no spaces allowed after \ in define
4752 if ($line =~ /\#\s*define.*\\\s+$/) {
4753 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4754 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4756 $fixed[$fixlinenr] =~ s/\s+$//;
4760 # warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4761 # itself <asm/foo.h> (uses RAW line)
4762 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4764 my $checkfile = "include/linux/$file";
4765 if (-f "$root/$checkfile" &&
4766 $realfile ne $checkfile &&
4767 $1 !~ /$allowed_asm_includes/)
4769 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4770 if ($asminclude > 0) {
4771 if ($realfile =~ m{^arch/}) {
4772 CHK("ARCH_INCLUDE_LINUX",
4773 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4775 WARN("INCLUDE_LINUX",
4776 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4782 # multi-statement macros should be enclosed in a do while loop, grab the
4783 # first statement and ensure its the whole macro if its not enclosed
4784 # in a known good container
4785 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4786 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4789 my ($off, $dstat, $dcond, $rest);
4791 my $has_flow_statement = 0;
4792 my $has_arg_concat = 0;
4793 ($dstat, $dcond, $ln, $cnt, $off) =
4794 ctx_statement_block($linenr, $realcnt, 0);
4796 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4797 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4799 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4800 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
4802 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4803 my $define_args = $1;
4804 my $define_stmt = $dstat;
4807 if (defined $define_args && $define_args ne "") {
4808 $define_args = substr($define_args, 1, length($define_args) - 2);
4809 $define_args =~ s/\s*//g;
4810 @def_args = split(",", $define_args);
4814 $dstat =~ s/\\\n.//g;
4815 $dstat =~ s/^\s*//s;
4816 $dstat =~ s/\s*$//s;
4818 # Flatten any parentheses and braces
4819 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4820 $dstat =~ s/\{[^\{\}]*\}/1/ ||
4821 $dstat =~ s/.\[[^\[\]]*\]/1/)
4825 # Flatten any obvious string concatentation.
4826 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4827 $dstat =~ s/$Ident\s*($String)/$1/)
4831 # Make asm volatile uses seem like a generic function
4832 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4834 my $exceptions = qr{
4847 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4850 my $herectx = $here . "\n";
4851 my $stmt_cnt = statement_rawlines($ctx);
4853 for (my $n = 0; $n < $stmt_cnt; $n++) {
4854 $herectx .= raw_line($linenr, $n) . "\n";
4858 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4859 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
4860 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4861 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
4862 $dstat !~ /$exceptions/ &&
4863 $dstat !~ /^\.$Ident\s*=/ && # .foo =
4864 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
4865 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
4866 $dstat !~ /^for\s*$Constant$/ && # for (...)
4867 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4868 $dstat !~ /^do\s*{/ && # do {...
4869 $dstat !~ /^\(\{/ && # ({...
4870 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4872 if ($dstat =~ /^\s*if\b/) {
4873 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4874 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
4875 } elsif ($dstat =~ /;/) {
4876 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4877 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4879 ERROR("COMPLEX_MACRO",
4880 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4885 # Make $define_stmt single line, comment-free, etc
4886 my @stmt_array = split('\n', $define_stmt);
4889 foreach my $l (@stmt_array) {
4894 } elsif ($l =~ /^[\+ ]/) {
4895 $define_stmt .= substr($l, 1);
4898 $define_stmt =~ s/$;//g;
4899 $define_stmt =~ s/\s+/ /g;
4900 $define_stmt = trim($define_stmt);
4902 # check if any macro arguments are reused (ignore '...' and 'type')
4903 foreach my $arg (@def_args) {
4904 next if ($arg =~ /\.\.\./);
4905 next if ($arg =~ /^type$/i);
4906 my $tmp = $define_stmt;
4907 $tmp =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
4908 $tmp =~ s/\#+\s*$arg\b//g;
4909 $tmp =~ s/\b$arg\s*\#\#//g;
4910 my $use_cnt = $tmp =~ s/\b$arg\b//g;
4912 CHK("MACRO_ARG_REUSE",
4913 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
4915 # check if any macro arguments may have other precedence issues
4916 if ($define_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
4917 ((defined($1) && $1 ne ',') ||
4918 (defined($2) && $2 ne ','))) {
4919 CHK("MACRO_ARG_PRECEDENCE",
4920 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
4924 # check for macros with flow control, but without ## concatenation
4925 # ## concatenation is commonly a macro that defines a function so ignore those
4926 if ($has_flow_statement && !$has_arg_concat) {
4927 my $herectx = $here . "\n";
4928 my $cnt = statement_rawlines($ctx);
4930 for (my $n = 0; $n < $cnt; $n++) {
4931 $herectx .= raw_line($linenr, $n) . "\n";
4933 WARN("MACRO_WITH_FLOW_CONTROL",
4934 "Macros with flow control statements should be avoided\n" . "$herectx");
4937 # check for line continuations outside of #defines, preprocessor #, and asm
4940 if ($prevline !~ /^..*\\$/ &&
4941 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4942 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
4943 $line =~ /^\+.*\\$/) {
4944 WARN("LINE_CONTINUATIONS",
4945 "Avoid unnecessary line continuations\n" . $herecurr);
4949 # do {} while (0) macro tests:
4950 # single-statement macros do not need to be enclosed in do while (0) loop,
4951 # macro should not end with a semicolon
4952 if ($^V && $^V ge 5.10.0 &&
4953 $realfile !~ m@/vmlinux.lds.h$@ &&
4954 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4957 my ($off, $dstat, $dcond, $rest);
4959 ($dstat, $dcond, $ln, $cnt, $off) =
4960 ctx_statement_block($linenr, $realcnt, 0);
4963 $dstat =~ s/\\\n.//g;
4966 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4971 my $cnt = statement_rawlines($ctx);
4972 my $herectx = $here . "\n";
4974 for (my $n = 0; $n < $cnt; $n++) {
4975 $herectx .= raw_line($linenr, $n) . "\n";
4978 if (($stmts =~ tr/;/;/) == 1 &&
4979 $stmts !~ /^\s*(if|while|for|switch)\b/) {
4980 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4981 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4983 if (defined $semis && $semis ne "") {
4984 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4985 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4987 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4989 my $cnt = statement_rawlines($ctx);
4990 my $herectx = $here . "\n";
4992 for (my $n = 0; $n < $cnt; $n++) {
4993 $herectx .= raw_line($linenr, $n) . "\n";
4996 WARN("TRAILING_SEMICOLON",
4997 "macros should not use a trailing semicolon\n" . "$herectx");
5001 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5002 # all assignments may have only one of the following with an assignment:
5005 # VMLINUX_SYMBOL(...)
5006 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
5007 WARN("MISSING_VMLINUX_SYMBOL",
5008 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
5011 # check for redundant bracing round if etc
5012 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5013 my ($level, $endln, @chunks) =
5014 ctx_statement_full($linenr, $realcnt, 1);
5015 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5016 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5017 if ($#chunks > 0 && $level == 0) {
5021 my $herectx = $here . "\n";
5022 my $ln = $linenr - 1;
5023 for my $chunk (@chunks) {
5024 my ($cond, $block) = @{$chunk};
5026 # If the condition carries leading newlines, then count those as offsets.
5027 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5028 my $offset = statement_rawlines($whitespace) - 1;
5030 $allowed[$allow] = 0;
5031 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5033 # We have looked at and allowed this specific line.
5034 $suppress_ifbraces{$ln + $offset} = 1;
5036 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5037 $ln += statement_rawlines($block) - 1;
5039 substr($block, 0, length($cond), '');
5041 $seen++ if ($block =~ /^\s*{/);
5043 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5044 if (statement_lines($cond) > 1) {
5045 #print "APW: ALLOWED: cond<$cond>\n";
5046 $allowed[$allow] = 1;
5048 if ($block =~/\b(?:if|for|while)\b/) {
5049 #print "APW: ALLOWED: block<$block>\n";
5050 $allowed[$allow] = 1;
5052 if (statement_block_size($block) > 1) {
5053 #print "APW: ALLOWED: lines block<$block>\n";
5054 $allowed[$allow] = 1;
5059 my $sum_allowed = 0;
5060 foreach (@allowed) {
5063 if ($sum_allowed == 0) {
5065 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5066 } elsif ($sum_allowed != $allow &&
5069 "braces {} should be used on all arms of this statement\n" . $herectx);
5074 if (!defined $suppress_ifbraces{$linenr - 1} &&
5075 $line =~ /\b(if|while|for|else)\b/) {
5078 # Check the pre-context.
5079 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5080 #print "APW: ALLOWED: pre<$1>\n";
5084 my ($level, $endln, @chunks) =
5085 ctx_statement_full($linenr, $realcnt, $-[0]);
5087 # Check the condition.
5088 my ($cond, $block) = @{$chunks[0]};
5089 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5090 if (defined $cond) {
5091 substr($block, 0, length($cond), '');
5093 if (statement_lines($cond) > 1) {
5094 #print "APW: ALLOWED: cond<$cond>\n";
5097 if ($block =~/\b(?:if|for|while)\b/) {
5098 #print "APW: ALLOWED: block<$block>\n";
5101 if (statement_block_size($block) > 1) {
5102 #print "APW: ALLOWED: lines block<$block>\n";
5105 # Check the post-context.
5106 if (defined $chunks[1]) {
5107 my ($cond, $block) = @{$chunks[1]};
5108 if (defined $cond) {
5109 substr($block, 0, length($cond), '');
5111 if ($block =~ /^\s*\{/) {
5112 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5116 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5117 my $herectx = $here . "\n";
5118 my $cnt = statement_rawlines($block);
5120 for (my $n = 0; $n < $cnt; $n++) {
5121 $herectx .= raw_line($linenr, $n) . "\n";
5125 "braces {} are not necessary for single statement blocks\n" . $herectx);
5129 # check for single line unbalanced braces
5130 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5131 $sline =~ /^.\s*else\s*\{\s*$/) {
5132 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5135 # check for unnecessary blank lines around braces
5136 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5138 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5139 $fix && $prevrawline =~ /^\+/) {
5140 fix_delete_line($fixlinenr - 1, $prevrawline);
5143 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5145 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5147 fix_delete_line($fixlinenr, $rawline);
5151 # no volatiles please
5152 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5153 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5155 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
5158 # Check for user-visible strings broken across lines, which breaks the ability
5159 # to grep for the string. Make exceptions when the previous string ends in a
5160 # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5161 # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
5162 if ($line =~ /^\+\s*$String/ &&
5163 $prevline =~ /"\s*$/ &&
5164 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5165 if (WARN("SPLIT_STRING",
5166 "quoted string split across lines\n" . $hereprev) &&
5168 $prevrawline =~ /^\+.*"\s*$/ &&
5169 $last_coalesced_string_linenr != $linenr - 1) {
5170 my $extracted_string = get_quoted_string($line, $rawline);
5171 my $comma_close = "";
5172 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5176 fix_delete_line($fixlinenr - 1, $prevrawline);
5177 fix_delete_line($fixlinenr, $rawline);
5178 my $fixedline = $prevrawline;
5179 $fixedline =~ s/"\s*$//;
5180 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5181 fix_insert_line($fixlinenr - 1, $fixedline);
5182 $fixedline = $rawline;
5183 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5184 if ($fixedline !~ /\+\s*$/) {
5185 fix_insert_line($fixlinenr, $fixedline);
5187 $last_coalesced_string_linenr = $linenr;
5191 # check for missing a space in a string concatenation
5192 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5193 WARN('MISSING_SPACE',
5194 "break quoted strings at a space character\n" . $hereprev);
5197 # check for an embedded function name in a string when the function is known
5198 # This does not work very well for -f --file checking as it depends on patch
5199 # context providing the function name or a single line form for in-file
5200 # function declarations
5201 if ($line =~ /^\+.*$String/ &&
5202 defined($context_function) &&
5203 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5204 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
5205 WARN("EMBEDDED_FUNCTION_NAME",
5206 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
5209 # check for spaces before a quoted newline
5210 if ($rawline =~ /^.*\".*\s\\n/) {
5211 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5212 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5214 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5219 # concatenated string without spaces between elements
5220 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5221 CHK("CONCATENATED_STRING",
5222 "Concatenated strings should use spaces between elements\n" . $herecurr);
5225 # uncoalesced string fragments
5226 if ($line =~ /$String\s*"/) {
5227 WARN("STRING_FRAGMENTS",
5228 "Consecutive strings are generally better as a single string\n" . $herecurr);
5231 # check for non-standard and hex prefixed decimal printf formats
5232 my $show_L = 1; #don't show the same defect twice
5234 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5235 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
5236 $string =~ s/%%/__/g;
5238 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
5240 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5244 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5246 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5249 # check for 0x<decimal>
5250 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5251 ERROR("PRINTF_0XDECIMAL",
5252 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5256 # check for line continuations in quoted strings with odd counts of "
5257 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5258 WARN("LINE_CONTINUATIONS",
5259 "Avoid line continuations in quoted strings\n" . $herecurr);
5263 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
5264 CHK("REDUNDANT_CODE",
5265 "if this code is redundant consider removing it\n" .
5269 # check for needless "if (<foo>) fn(<foo>)" uses
5270 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5271 my $tested = quotemeta($1);
5272 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5273 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5275 if (WARN('NEEDLESS_IF',
5276 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5279 my $leading_tabs = "";
5280 my $new_leading_tabs = "";
5281 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5286 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5287 $new_leading_tabs = $1;
5288 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5295 fix_delete_line($fixlinenr - 1, $prevrawline);
5296 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5302 # check for unnecessary "Out of Memory" messages
5303 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5304 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5305 (defined $1 || defined $3) &&
5308 my $testline = $lines[$linenr - 3];
5310 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5311 # print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5313 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
5315 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5319 # check for logging functions with KERN_<LEVEL>
5320 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5321 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5323 if (WARN("UNNECESSARY_KERN_LEVEL",
5324 "Possible unnecessary $level\n" . $herecurr) &&
5326 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5330 # check for logging continuations
5331 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5332 WARN("LOGGING_CONTINUATION",
5333 "Avoid logging continuation uses where feasible\n" . $herecurr);
5336 # check for mask then right shift without a parentheses
5337 if ($^V && $^V ge 5.10.0 &&
5338 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5339 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5340 WARN("MASK_THEN_SHIFT",
5341 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5344 # check for pointer comparisons to NULL
5345 if ($^V && $^V ge 5.10.0) {
5346 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5349 $equal = "" if ($4 eq "!=");
5350 if (CHK("COMPARISON_TO_NULL",
5351 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5353 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5358 # check for bad placement of section $InitAttribute (e.g.: __initdata)
5359 if ($line =~ /(\b$InitAttribute\b)/) {
5361 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5364 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5365 ERROR("MISPLACED_INIT",
5366 "$attr should be placed after $var\n" . $herecurr)) ||
5367 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5368 WARN("MISPLACED_INIT",
5369 "$attr should be placed after $var\n" . $herecurr))) &&
5371 $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;
5376 # check for $InitAttributeData (ie: __initdata) with const
5377 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5379 $attr =~ /($InitAttributePrefix)(.*)/;
5380 my $attr_prefix = $1;
5382 if (ERROR("INIT_ATTRIBUTE",
5383 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5385 $fixed[$fixlinenr] =~
5386 s/$InitAttributeData/${attr_prefix}initconst/;
5390 # check for $InitAttributeConst (ie: __initconst) without const
5391 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5393 if (ERROR("INIT_ATTRIBUTE",
5394 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5396 my $lead = $fixed[$fixlinenr] =~
5397 /(^\+\s*(?:static\s+))/;
5399 $lead = "$lead " if ($lead !~ /^\+$/);
5400 $lead = "${lead}const ";
5401 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5405 # check for __read_mostly with const non-pointer (should just be const)
5406 if ($line =~ /\b__read_mostly\b/ &&
5407 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5408 if (ERROR("CONST_READ_MOSTLY",
5409 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5411 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5415 # don't use __constant_<foo> functions outside of include/uapi/
5416 if ($realfile !~ m@^include/uapi/@ &&
5417 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5418 my $constant_func = $1;
5419 my $func = $constant_func;
5420 $func =~ s/^__constant_//;
5421 if (WARN("CONSTANT_CONVERSION",
5422 "$constant_func should be $func\n" . $herecurr) &&
5424 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5428 # prefer usleep_range over udelay
5429 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
5431 # ignore udelay's < 10, however
5432 if (! ($delay < 10) ) {
5434 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5436 if ($delay > 2000) {
5438 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
5442 # warn about unexpectedly long msleep's
5443 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5446 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5450 # check for comparisons of jiffies
5451 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5452 WARN("JIFFIES_COMPARISON",
5453 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5456 # check for comparisons of get_jiffies_64()
5457 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5458 WARN("JIFFIES_COMPARISON",
5459 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5462 # warn about #ifdefs in C files
5463 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
5464 # print "#ifdef in C files should be avoided\n";
5465 # print "$herecurr";
5469 # warn about spacing in #ifdefs
5470 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
5471 if (ERROR("SPACING",
5472 "exactly one space required after that #$1\n" . $herecurr) &&
5474 $fixed[$fixlinenr] =~
5475 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5480 # check for spinlock_t definitions without a comment.
5481 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5482 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
5484 if (!ctx_has_comment($first_line, $linenr)) {
5485 CHK("UNCOMMENTED_DEFINITION",
5486 "$1 definition without comment\n" . $herecurr);
5489 # check for memory barriers without a comment.
5495 read_barrier_depends
5497 my $barrier_stems = qr{
5505 my $all_barriers = qr{
5507 smp_(?:$barrier_stems)|
5508 virt_(?:$barrier_stems)
5511 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
5512 if (!ctx_has_comment($first_line, $linenr)) {
5513 WARN("MEMORY_BARRIER",
5514 "memory barrier without comment\n" . $herecurr);
5518 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5520 if ($realfile !~ m@^include/asm-generic/@ &&
5521 $realfile !~ m@/barrier\.h$@ &&
5522 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5523 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5524 WARN("MEMORY_BARRIER",
5525 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5528 # check for waitqueue_active without a comment.
5529 if ($line =~ /\bwaitqueue_active\s*\(/) {
5530 if (!ctx_has_comment($first_line, $linenr)) {
5531 WARN("WAITQUEUE_ACTIVE",
5532 "waitqueue_active without comment\n" . $herecurr);
5536 # Check for expedited grace periods that interrupt non-idle non-nohz
5537 # online CPUs. These expedited can therefore degrade real-time response
5538 # if used carelessly, and should be avoided where not absolutely
5539 # needed. It is always OK to use synchronize_rcu_expedited() and
5540 # synchronize_sched_expedited() at boot time (before real-time applications
5541 # start) and in error situations where real-time response is compromised in
5542 # any case. Note that synchronize_srcu_expedited() does -not- interrupt
5543 # other CPUs, so don't warn on uses of synchronize_srcu_expedited().
5544 # Of course, nothing comes for free, and srcu_read_lock() and
5545 # srcu_read_unlock() do contain full memory barriers in payment for
5546 # synchronize_srcu_expedited() non-interruption properties.
5547 if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
5548 WARN("EXPEDITED_RCU_GRACE_PERIOD",
5549 "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
5553 # check of hardware specific defines
5554 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5556 "architecture specific defines should be avoided\n" . $herecurr);
5559 # Check that the storage class is at the beginning of a declaration
5560 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
5561 WARN("STORAGE_CLASS",
5562 "storage class should be at the beginning of the declaration\n" . $herecurr)
5565 # check the location of the inline attribute, that it is between
5566 # storage class and type.
5567 if ($line =~ /\b$Type\s+$Inline\b/ ||
5568 $line =~ /\b$Inline\s+$Storage\b/) {
5569 ERROR("INLINE_LOCATION",
5570 "inline keyword should sit between storage class and type\n" . $herecurr);
5573 # Check for __inline__ and __inline, prefer inline
5574 if ($realfile !~ m@\binclude/uapi/@ &&
5575 $line =~ /\b(__inline__|__inline)\b/) {
5577 "plain inline is preferred over $1\n" . $herecurr) &&
5579 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5584 # Check for __attribute__ packed, prefer __packed
5585 if ($realfile !~ m@\binclude/uapi/@ &&
5586 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5587 WARN("PREFER_PACKED",
5588 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
5591 # Check for __attribute__ aligned, prefer __aligned
5592 if ($realfile !~ m@\binclude/uapi/@ &&
5593 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5594 WARN("PREFER_ALIGNED",
5595 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
5598 # Check for __attribute__ format(printf, prefer __printf
5599 if ($realfile !~ m@\binclude/uapi/@ &&
5600 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5601 if (WARN("PREFER_PRINTF",
5602 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5604 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5609 # Check for __attribute__ format(scanf, prefer __scanf
5610 if ($realfile !~ m@\binclude/uapi/@ &&
5611 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5612 if (WARN("PREFER_SCANF",
5613 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5615 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5619 # Check for __attribute__ weak, or __weak declarations (may have link issues)
5620 if ($^V && $^V ge 5.10.0 &&
5621 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5622 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5623 $line =~ /\b__weak\b/)) {
5624 ERROR("WEAK_DECLARATION",
5625 "Using weak declarations can have unintended link defects\n" . $herecurr);
5628 # check for c99 types like uint8_t used outside of uapi/ and tools/
5629 if ($realfile !~ m@\binclude/uapi/@ &&
5630 $realfile !~ m@\btools/@ &&
5631 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5633 if ($type =~ /\b($typeC99Typedefs)\b/) {
5635 my $kernel_type = 'u';
5636 $kernel_type = 's' if ($type =~ /^_*[si]/);
5639 if (CHK("PREFER_KERNEL_TYPES",
5640 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5642 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5647 # check for cast of C90 native int or longer types constants
5648 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5651 if (WARN("TYPECAST_INT_CONSTANT",
5652 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5655 my $newconst = $const;
5656 $newconst =~ s/${Int_type}$//;
5657 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5658 if ($cast =~ /\blong\s+long\b/) {
5660 } elsif ($cast =~ /\blong\b/) {
5663 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5667 # check for sizeof(&)
5668 if ($line =~ /\bsizeof\s*\(\s*\&/) {
5669 WARN("SIZEOF_ADDRESS",
5670 "sizeof(& should be avoided\n" . $herecurr);
5673 # check for sizeof without parenthesis
5674 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5675 if (WARN("SIZEOF_PARENTHESIS",
5676 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5678 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5682 # check for struct spinlock declarations
5683 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5684 WARN("USE_SPINLOCK_T",
5685 "struct spinlock should be spinlock_t\n" . $herecurr);
5688 # check for seq_printf uses that could be seq_puts
5689 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5690 my $fmt = get_quoted_string($line, $rawline);
5693 if (WARN("PREFER_SEQ_PUTS",
5694 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5696 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5701 # check for vsprintf extension %p<foo> misuses
5702 if ($^V && $^V ge 5.10.0 &&
5704 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5705 $1 !~ /^_*volatile_*$/) {
5706 my $bad_extension = "";
5707 my $lc = $stat =~ tr@\n@@;
5708 $lc = $lc + $linenr;
5709 for (my $count = $linenr; $count <= $lc; $count++) {
5710 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5712 if ($fmt =~ /(\%[\*\d\.]*p(?![\WFfSsBKRraEhMmIiUDdgVCbGN]).)/) {
5713 $bad_extension = $1;
5717 if ($bad_extension ne "") {
5718 my $stat_real = raw_line($linenr, 0);
5719 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5720 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5722 WARN("VSPRINTF_POINTER_EXTENSION",
5723 "Invalid vsprintf pointer extension '$bad_extension'\n" . "$here\n$stat_real\n");
5727 # Check for misused memsets
5728 if ($^V && $^V ge 5.10.0 &&
5730 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5736 if ($ms_size =~ /^(0x|)0$/i) {
5738 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5739 } elsif ($ms_size =~ /^(0x|)1$/i) {
5741 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5745 # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5746 # if ($^V && $^V ge 5.10.0 &&
5748 # $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5749 # if (WARN("PREFER_ETHER_ADDR_COPY",
5750 # "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5752 # $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5756 # Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5757 # if ($^V && $^V ge 5.10.0 &&
5759 # $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5760 # WARN("PREFER_ETHER_ADDR_EQUAL",
5761 # "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5764 # check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5765 # check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
5766 # if ($^V && $^V ge 5.10.0 &&
5768 # $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5772 # if ($ms_val =~ /^(?:0x|)0+$/i) {
5773 # if (WARN("PREFER_ETH_ZERO_ADDR",
5774 # "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5776 # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5778 # } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5779 # if (WARN("PREFER_ETH_BROADCAST_ADDR",
5780 # "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5782 # $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5787 # typecasts on min/max could be min_t/max_t
5788 if ($^V && $^V ge 5.10.0 &&
5790 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
5791 if (defined $2 || defined $7) {
5793 my $cast1 = deparenthesize($2);
5795 my $cast2 = deparenthesize($7);
5799 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
5800 $cast = "$cast1 or $cast2";
5801 } elsif ($cast1 ne "") {
5807 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5811 # check usleep_range arguments
5812 if ($^V && $^V ge 5.10.0 &&
5814 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5818 WARN("USLEEP_RANGE",
5819 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5820 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5822 WARN("USLEEP_RANGE",
5823 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5827 # check for naked sscanf
5828 if ($^V && $^V ge 5.10.0 &&
5830 $line =~ /\bsscanf\b/ &&
5831 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5832 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5833 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5834 my $lc = $stat =~ tr@\n@@;
5835 $lc = $lc + $linenr;
5836 my $stat_real = raw_line($linenr, 0);
5837 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5838 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5840 WARN("NAKED_SSCANF",
5841 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5844 # check for simple sscanf that should be kstrto<foo>
5845 if ($^V && $^V ge 5.10.0 &&
5847 $line =~ /\bsscanf\b/) {
5848 my $lc = $stat =~ tr@\n@@;
5849 $lc = $lc + $linenr;
5850 my $stat_real = raw_line($linenr, 0);
5851 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5852 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5854 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5856 my $count = $format =~ tr@%@%@;
5858 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5859 WARN("SSCANF_TO_KSTRTO",
5860 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5865 # check for new externs in .h files.
5866 if ($realfile =~ /\.h$/ &&
5867 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5868 if (CHK("AVOID_EXTERNS",
5869 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
5871 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
5875 # check for new externs in .c files.
5876 if ($realfile =~ /\.c$/ && defined $stat &&
5877 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5879 my $function_name = $1;
5880 my $paren_space = $2;
5883 if (defined $cond) {
5884 substr($s, 0, length($cond), '');
5886 if ($s =~ /^\s*;/ &&
5887 $function_name ne 'uninitialized_var')
5889 WARN("AVOID_EXTERNS",
5890 "externs should be avoided in .c files\n" . $herecurr);
5893 if ($paren_space =~ /\n/) {
5894 WARN("FUNCTION_ARGUMENTS",
5895 "arguments for function declarations should follow identifier\n" . $herecurr);
5898 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5899 $stat =~ /^.\s*extern\s+/)
5901 WARN("AVOID_EXTERNS",
5902 "externs should be avoided in .c files\n" . $herecurr);
5905 if ($realfile =~ /\.[ch]$/ && defined $stat &&
5906 $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
5908 my $args = trim($1);
5909 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
5911 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
5912 WARN("FUNCTION_ARGUMENTS",
5913 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
5918 # checks for new __setup's
5919 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5922 if (!grep(/$name/, @setup_docs)) {
5923 CHK("UNDOCUMENTED_SETUP",
5924 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
5928 # check for pointless casting of kmalloc return
5929 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5930 WARN("UNNECESSARY_CASTS",
5931 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
5935 # p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5936 if ($^V && $^V ge 5.10.0 &&
5937 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5938 CHK("ALLOC_SIZEOF_STRUCT",
5939 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5942 # check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5943 if ($^V && $^V ge 5.10.0 &&
5945 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
5949 my $newfunc = "kmalloc_array";
5950 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
5953 if ($a1 =~ /^sizeof\s*\S/) {
5957 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5958 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5960 my $herectx = $here . "\n";
5961 my $cnt = statement_rawlines($stat);
5962 for (my $n = 0; $n < $cnt; $n++) {
5963 $herectx .= raw_line($linenr, $n) . "\n";
5965 if (WARN("ALLOC_WITH_MULTIPLY",
5966 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
5969 $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;
5974 # check for krealloc arg reuse
5975 if ($^V && $^V ge 5.10.0 &&
5976 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5977 WARN("KREALLOC_ARG_REUSE",
5978 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5981 # check for alloc argument mismatch
5982 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5983 WARN("ALLOC_ARRAY_ARGS",
5984 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5987 # check for multiple semicolons
5988 if ($line =~ /;\s*;\s*$/) {
5989 if (WARN("ONE_SEMICOLON",
5990 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5992 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
5996 # check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
5997 if ($realfile !~ m@^include/uapi/@ &&
5998 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
6000 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6001 if (CHK("BIT_MACRO",
6002 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6004 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6008 # check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6009 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6011 if (WARN("PREFER_IS_ENABLED",
6012 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6014 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6018 # check for case / default statements not preceded by break/fallthrough/switch
6019 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6021 my $has_statement = 0;
6023 my $prevline = $linenr;
6024 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6026 my $rline = $rawlines[$prevline - 1];
6027 my $fline = $lines[$prevline - 1];
6028 last if ($fline =~ /^\@\@/);
6029 next if ($fline =~ /^\-/);
6030 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6031 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6032 next if ($fline =~ /^.[\s$;]*$/);
6035 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
6037 if (!$has_break && $has_statement) {
6038 WARN("MISSING_BREAK",
6039 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6043 # check for switch/default statements without a break;
6044 if ($^V && $^V ge 5.10.0 &&
6046 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6048 my $herectx = $here . "\n";
6049 my $cnt = statement_rawlines($stat);
6050 for (my $n = 0; $n < $cnt; $n++) {
6051 $herectx .= raw_line($linenr, $n) . "\n";
6053 WARN("DEFAULT_NO_BREAK",
6054 "switch default: should use break\n" . $herectx);
6057 # check for gcc specific __FUNCTION__
6058 if ($line =~ /\b__FUNCTION__\b/) {
6059 if (WARN("USE_FUNC",
6060 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6062 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6066 # check for uses of __DATE__, __TIME__, __TIMESTAMP__
6067 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6069 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6072 # check for use of yield()
6073 if ($line =~ /\byield\s*\(\s*\)/) {
6075 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6078 # check for comparisons against true and false
6079 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6087 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6089 my $type = lc($otype);
6090 if ($type =~ /^(?:true|false)$/) {
6091 if (("$test" eq "==" && "$type" eq "true") ||
6092 ("$test" eq "!=" && "$type" eq "false")) {
6096 CHK("BOOL_COMPARISON",
6097 "Using comparison to $otype is error prone\n" . $herecurr);
6099 ## maybe suggesting a correct construct would better
6100 ## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6105 # check for semaphores initialized locked
6106 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6107 WARN("CONSIDER_COMPLETION",
6108 "consider using a completion\n" . $herecurr);
6111 # recommend kstrto* over simple_strto* and strict_strto*
6112 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6113 WARN("CONSIDER_KSTRTO",
6114 "$1 is obsolete, use k$3 instead\n" . $herecurr);
6117 # check for __initcall(), use device_initcall() explicitly or more appropriate function please
6118 if ($line =~ /^.\s*__initcall\s*\(/) {
6119 WARN("USE_DEVICE_INITCALL",
6120 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6123 # check for various structs that are normally const (ops, kgdb, device_tree)
6124 # and avoid what seem like struct definitions 'struct foo {'
6125 if ($line !~ /\bconst\b/ &&
6126 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6127 WARN("CONST_STRUCT",
6128 "struct $1 should normally be const\n" . $herecurr);
6131 # use of NR_CPUS is usually wrong
6132 # ignore definitions of NR_CPUS and usage to define arrays as likely right
6133 if ($line =~ /\bNR_CPUS\b/ &&
6134 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6135 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6136 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6137 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6138 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6141 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6144 # Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6145 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6146 ERROR("DEFINE_ARCH_HAS",
6147 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6150 # likely/unlikely comparisons similar to "(likely(foo) > 0)"
6151 if ($^V && $^V ge 5.10.0 &&
6152 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6153 WARN("LIKELY_MISUSE",
6154 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6157 # whine mightly about in_atomic
6158 if ($line =~ /\bin_atomic\s*\(/) {
6159 if ($realfile =~ m@^drivers/@) {
6161 "do not use in_atomic in drivers\n" . $herecurr);
6162 } elsif ($realfile !~ m@^kernel/@) {
6164 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6168 # whine about ACCESS_ONCE
6169 if ($^V && $^V ge 5.10.0 &&
6170 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6174 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6176 if (WARN("PREFER_WRITE_ONCE",
6177 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6179 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6182 if (WARN("PREFER_READ_ONCE",
6183 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6185 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6190 # check for mutex_trylock_recursive usage
6191 if ($line =~ /mutex_trylock_recursive/) {
6193 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6196 # check for lockdep_set_novalidate_class
6197 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6198 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6199 if ($realfile !~ m@^kernel/lockdep@ &&
6200 $realfile !~ m@^include/linux/lockdep@ &&
6201 $realfile !~ m@^drivers/base/core@) {
6203 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
6207 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6208 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6209 WARN("EXPORTED_WORLD_WRITABLE",
6210 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
6213 # Mode permission misuses where it seems decimal should be octal
6214 # This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6215 if ($^V && $^V ge 5.10.0 &&
6217 $line =~ /$mode_perms_search/) {
6218 foreach my $entry (@mode_permission_funcs) {
6219 my $func = $entry->[0];
6220 my $arg_pos = $entry->[1];
6222 my $lc = $stat =~ tr@\n@@;
6223 $lc = $lc + $linenr;
6224 my $stat_real = raw_line($linenr, 0);
6225 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6226 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6232 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6234 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6235 if ($stat =~ /$test/) {
6237 $val = $6 if ($skip_args ne "");
6238 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6239 ($val =~ /^$Octal$/ && length($val) ne 4)) {
6240 ERROR("NON_OCTAL_PERMISSIONS",
6241 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6243 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6244 ERROR("EXPORTED_WORLD_WRITABLE",
6245 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
6251 # check for uses of S_<PERMS> that could be octal for readability
6252 if ($line =~ /\b$mode_perms_string_search\b/) {
6258 while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6259 $curpos = pos($line);
6262 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6264 $to |= $mode_permission_string_types{$match};
6265 $val .= '\s*\|\s*' if ($val ne "");
6269 $oval =~ s/^\s*\|\s*//;
6270 $oval =~ s/\s*\|\s*$//;
6271 my $octal = sprintf("%04o", $to);
6272 if (WARN("SYMBOLIC_PERMS",
6273 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6275 $fixed[$fixlinenr] =~ s/$val/$octal/;
6279 # validate content of MODULE_LICENSE against list from include/linux/module.h
6280 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6281 my $extracted_string = get_quoted_string($line, $rawline);
6282 my $valid_licenses = qr{
6285 GPL\ and\ additional\ rights|
6291 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6292 WARN("MODULE_LICENSE",
6293 "unknown module license " . $extracted_string . "\n" . $herecurr);
6298 # If we have no input at all, then there is nothing to report on
6299 # so just keep quiet.
6300 if ($#rawlines == -1) {
6304 # In mailback mode only produce a report in the negative, for
6305 # things that appear to be patches.
6306 if ($mailback && ($clean == 1 || !$is_patch)) {
6310 # This is not a patch, and we are are in 'no-patch' mode so
6312 if (!$chk_patch && !$is_patch) {
6316 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
6317 ERROR("NOT_UNIFIED_DIFF",
6318 "Does not appear to be a unified-diff format patch\n");
6320 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
6321 ERROR("MISSING_SIGN_OFF",
6322 "Missing Signed-off-by: line(s)\n");
6325 print report_dump();
6326 if ($summary && !($clean == 1 && $quiet == 1)) {
6327 print "$filename " if ($summary_file);
6328 print "total: $cnt_error errors, $cnt_warn warnings, " .
6329 (($check)? "$cnt_chk checks, " : "") .
6330 "$cnt_lines lines checked\n";
6334 # If there were any defects found and not already fixing them
6335 if (!$clean and !$fix) {
6338 NOTE: For some of the reported defects, checkpatch may be able to
6339 mechanically convert to the typical style using --fix or --fix-inplace.
6342 # If there were whitespace errors which cleanpatch can fix
6343 # then suggest that.
6344 if ($rpt_cleaners) {
6348 NOTE: Whitespace errors detected.
6349 You may wish to use scripts/cleanpatch or scripts/cleanfile
6354 if ($clean == 0 && $fix &&
6355 ("@rawlines" ne "@fixed" ||
6356 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
6357 my $newfile = $filename;
6358 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
6362 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6364 open($f, '>', $newfile)
6365 or die "$P: Can't open $newfile for write\n";
6366 foreach my $fixed_line (@fixed) {
6369 if ($linecount > 3) {
6370 $fixed_line =~ s/^\+//;
6371 print $f $fixed_line . "\n";
6374 print $f $fixed_line . "\n";
6382 Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6384 Do _NOT_ trust the results written to this file.
6385 Do _NOT_ submit these changes without inspecting them for correctness.
6387 This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6388 No warranties, expressed or implied...
6396 print "$vname has no obvious style problems and is ready for submission.\n";
6398 print "$vname has style problems, please review.\n";